comparison mercurial/patch.py @ 12669:b0fa39c68370

patch: remove unused flags from readgitpatch()
author Patrick Mezard <pmezard@gmail.com>
date Sat, 09 Oct 2010 15:13:08 -0500
parents d7452292f9d3
children d82d40ff9860
comparison
equal deleted inserted replaced
12668:89e35377dbe8 12669:b0fa39c68370
278 return None, message, user, date, branch, None, None, None 278 return None, message, user, date, branch, None, None, None
279 p1 = parents and parents.pop(0) or None 279 p1 = parents and parents.pop(0) or None
280 p2 = parents and parents.pop(0) or None 280 p2 = parents and parents.pop(0) or None
281 return tmpname, message, user, date, branch, nodeid, p1, p2 281 return tmpname, message, user, date, branch, nodeid, p1, p2
282 282
283 GP_PATCH = 1 << 0 # we have to run patch
284 GP_FILTER = 1 << 1 # there's some copy/rename operation
285 GP_BINARY = 1 << 2 # there's a binary patch
286
287 class patchmeta(object): 283 class patchmeta(object):
288 """Patched file metadata 284 """Patched file metadata
289 285
290 'op' is the performed operation within ADD, DELETE, RENAME, MODIFY 286 'op' is the performed operation within ADD, DELETE, RENAME, MODIFY
291 or COPY. 'path' is patched file path. 'oldpath' is set to the 287 or COPY. 'path' is patched file path. 'oldpath' is set to the
314 """extract git-style metadata about patches from <patchname>""" 310 """extract git-style metadata about patches from <patchname>"""
315 311
316 # Filter patch for git information 312 # Filter patch for git information
317 gp = None 313 gp = None
318 gitpatches = [] 314 gitpatches = []
319 # Can have a git patch with only metadata, causing patch to complain
320 dopatch = 0
321
322 lineno = 0 315 lineno = 0
323 for line in lr: 316 for line in lr:
324 lineno += 1 317 lineno += 1
325 line = line.rstrip(' \r\n') 318 line = line.rstrip(' \r\n')
326 if line.startswith('diff --git'): 319 if line.startswith('diff --git'):
331 dst = m.group(2) 324 dst = m.group(2)
332 gp = patchmeta(dst) 325 gp = patchmeta(dst)
333 gp.lineno = lineno 326 gp.lineno = lineno
334 elif gp: 327 elif gp:
335 if line.startswith('--- '): 328 if line.startswith('--- '):
336 if gp.op in ('COPY', 'RENAME'):
337 dopatch |= GP_FILTER
338 gitpatches.append(gp) 329 gitpatches.append(gp)
339 gp = None 330 gp = None
340 dopatch |= GP_PATCH
341 continue 331 continue
342 if line.startswith('rename from '): 332 if line.startswith('rename from '):
343 gp.op = 'RENAME' 333 gp.op = 'RENAME'
344 gp.oldpath = line[12:] 334 gp.oldpath = line[12:]
345 elif line.startswith('rename to '): 335 elif line.startswith('rename to '):
355 gp.op = 'ADD' 345 gp.op = 'ADD'
356 gp.setmode(int(line[-6:], 8)) 346 gp.setmode(int(line[-6:], 8))
357 elif line.startswith('new mode '): 347 elif line.startswith('new mode '):
358 gp.setmode(int(line[-6:], 8)) 348 gp.setmode(int(line[-6:], 8))
359 elif line.startswith('GIT binary patch'): 349 elif line.startswith('GIT binary patch'):
360 dopatch |= GP_BINARY
361 gp.binary = True 350 gp.binary = True
362 if gp: 351 if gp:
363 gitpatches.append(gp) 352 gitpatches.append(gp)
364 353
365 if not gitpatches: 354 return gitpatches
366 dopatch = GP_PATCH
367
368 return (dopatch, gitpatches)
369 355
370 class linereader(object): 356 class linereader(object):
371 # simple class to allow pushing lines back into the input stream 357 # simple class to allow pushing lines back into the input stream
372 def __init__(self, fp, textmode=False): 358 def __init__(self, fp, textmode=False):
373 self.fp = fp 359 self.fp = fp
984 fp = lr.fp 970 fp = lr.fp
985 except IOError: 971 except IOError:
986 fp = cStringIO.StringIO(lr.fp.read()) 972 fp = cStringIO.StringIO(lr.fp.read())
987 gitlr = linereader(fp, lr.textmode) 973 gitlr = linereader(fp, lr.textmode)
988 gitlr.push(firstline) 974 gitlr.push(firstline)
989 (dopatch, gitpatches) = readgitpatch(gitlr) 975 gitpatches = readgitpatch(gitlr)
990 fp.seek(pos) 976 fp.seek(pos)
991 return dopatch, gitpatches 977 return gitpatches
992 978
993 def iterhunks(ui, fp, sourcefile=None): 979 def iterhunks(ui, fp, sourcefile=None):
994 """Read a patch and yield the following events: 980 """Read a patch and yield the following events:
995 - ("file", afile, bfile, firsthunk): select a new target file. 981 - ("file", afile, bfile, firsthunk): select a new target file.
996 - ("hunk", hunk): a new hunk is ready to be applied, follows a 982 - ("hunk", hunk): a new hunk is ready to be applied, follows a
1060 gitworkdone = False 1046 gitworkdone = False
1061 if m: 1047 if m:
1062 afile, bfile = m.group(1, 2) 1048 afile, bfile = m.group(1, 2)
1063 if not git: 1049 if not git:
1064 git = True 1050 git = True
1065 gitpatches = scangitpatch(lr, x)[1] 1051 gitpatches = scangitpatch(lr, x)
1066 yield 'git', gitpatches 1052 yield 'git', gitpatches
1067 for gp in gitpatches: 1053 for gp in gitpatches:
1068 changed[gp.path] = gp 1054 changed[gp.path] = gp
1069 # else error? 1055 # else error?
1070 # copy/rename + modify should modify target, not source 1056 # copy/rename + modify should modify target, not source