comparison mercurial/patch.py @ 5035:a675f6d5d069

patch: make internal code a bit friendlier to use
author Bryan O'Sullivan <bos@serpentine.com>
date Tue, 31 Jul 2007 16:28:05 -0700
parents 1b07668b8cc3
children 35d47b06d4e3
comparison
equal deleted inserted replaced
5034:c0417a319e39 5035:a675f6d5d069
140 140
141 GP_PATCH = 1 << 0 # we have to run patch 141 GP_PATCH = 1 << 0 # we have to run patch
142 GP_FILTER = 1 << 1 # there's some copy/rename operation 142 GP_FILTER = 1 << 1 # there's some copy/rename operation
143 GP_BINARY = 1 << 2 # there's a binary patch 143 GP_BINARY = 1 << 2 # there's a binary patch
144 144
145 def readgitpatch(fp, firstline): 145 def readgitpatch(fp, firstline=None):
146 """extract git-style metadata about patches from <patchname>""" 146 """extract git-style metadata about patches from <patchname>"""
147 class gitpatch: 147 class gitpatch:
148 "op is one of ADD, DELETE, RENAME, MODIFY or COPY" 148 "op is one of ADD, DELETE, RENAME, MODIFY or COPY"
149 def __init__(self, path): 149 def __init__(self, path):
150 self.path = path 150 self.path = path
154 self.copymod = False 154 self.copymod = False
155 self.lineno = 0 155 self.lineno = 0
156 self.binary = False 156 self.binary = False
157 157
158 def reader(fp, firstline): 158 def reader(fp, firstline):
159 yield firstline 159 if firstline is not None:
160 yield firstline
160 for line in fp: 161 for line in fp:
161 yield line 162 yield line
162 163
163 # Filter patch for git information 164 # Filter patch for git information
164 gitre = re.compile('diff --git a/(.*) b/(.*)') 165 gitre = re.compile('diff --git a/(.*) b/(.*)')
276 if code: 277 if code:
277 raise PatchError(_("patch command failed: %s") % 278 raise PatchError(_("patch command failed: %s") %
278 util.explain_exit(code)[0]) 279 util.explain_exit(code)[0])
279 return fuzz 280 return fuzz
280 281
281 def internalpatch(patchname, ui, strip, cwd, files): 282 def internalpatch(patchobj, ui, strip, cwd, files={}):
282 """use builtin patch to apply <patchname> to the working directory. 283 """use builtin patch to apply <patchobj> to the working directory.
283 returns whether patch was applied with fuzz factor.""" 284 returns whether patch was applied with fuzz factor."""
284 fp = file(patchname, 'rb') 285 try:
286 fp = file(patchobj, 'rb')
287 except TypeError:
288 fp = patchobj
285 if cwd: 289 if cwd:
286 curdir = os.getcwd() 290 curdir = os.getcwd()
287 os.chdir(cwd) 291 os.chdir(cwd)
288 try: 292 try:
289 ret = applydiff(ui, fp, files, strip=strip) 293 ret = applydiff(ui, fp, files, strip=strip)