comparison mercurial/patch.py @ 24264:c4205452f1b7

record: move scanpatch from record to patch Part of a series of patches to move record from hgext to core
author Laurent Charignon <lcharignon@fb.com>
date Mon, 09 Mar 2015 13:14:31 -0700
parents a45d1c51109e
children dc655360bccb
comparison
equal deleted inserted replaced
24263:a45d1c51109e 24264:c4205452f1b7
1273 if create: 1273 if create:
1274 gp.op = 'ADD' 1274 gp.op = 'ADD'
1275 elif remove: 1275 elif remove:
1276 gp.op = 'DELETE' 1276 gp.op = 'DELETE'
1277 return gp 1277 return gp
1278
1279 def scanpatch(fp):
1280 """like patch.iterhunks, but yield different events
1281
1282 - ('file', [header_lines + fromfile + tofile])
1283 - ('context', [context_lines])
1284 - ('hunk', [hunk_lines])
1285 - ('range', (-start,len, +start,len, proc))
1286 """
1287 lines_re = re.compile(r'@@ -(\d+),(\d+) \+(\d+),(\d+) @@\s*(.*)')
1288 lr = linereader(fp)
1289
1290 def scanwhile(first, p):
1291 """scan lr while predicate holds"""
1292 lines = [first]
1293 while True:
1294 line = lr.readline()
1295 if not line:
1296 break
1297 if p(line):
1298 lines.append(line)
1299 else:
1300 lr.push(line)
1301 break
1302 return lines
1303
1304 while True:
1305 line = lr.readline()
1306 if not line:
1307 break
1308 if line.startswith('diff --git a/') or line.startswith('diff -r '):
1309 def notheader(line):
1310 s = line.split(None, 1)
1311 return not s or s[0] not in ('---', 'diff')
1312 header = scanwhile(line, notheader)
1313 fromfile = lr.readline()
1314 if fromfile.startswith('---'):
1315 tofile = lr.readline()
1316 header += [fromfile, tofile]
1317 else:
1318 lr.push(fromfile)
1319 yield 'file', header
1320 elif line[0] == ' ':
1321 yield 'context', scanwhile(line, lambda l: l[0] in ' \\')
1322 elif line[0] in '-+':
1323 yield 'hunk', scanwhile(line, lambda l: l[0] in '-+\\')
1324 else:
1325 m = lines_re.match(line)
1326 if m:
1327 yield 'range', m.groups()
1328 else:
1329 yield 'other', line
1278 1330
1279 def scangitpatch(lr, firstline): 1331 def scangitpatch(lr, firstline):
1280 """ 1332 """
1281 Git patches can emit: 1333 Git patches can emit:
1282 - rename a to b 1334 - rename a to b