##// END OF EJS Templates
record: move scanpatch from record to patch...
Laurent Charignon -
r24264:c4205452 default
parent child Browse files
Show More
@@ -10,65 +10,12 b''
10 10 from mercurial.i18n import _
11 11 from mercurial import cmdutil, commands, extensions, hg, patch
12 12 from mercurial import util
13 import copy, cStringIO, errno, os, re, shutil, tempfile
13 import copy, cStringIO, errno, os, shutil, tempfile
14 14
15 15 cmdtable = {}
16 16 command = cmdutil.command(cmdtable)
17 17 testedwith = 'internal'
18 18
19 lines_re = re.compile(r'@@ -(\d+),(\d+) \+(\d+),(\d+) @@\s*(.*)')
20
21 def scanpatch(fp):
22 """like patch.iterhunks, but yield different events
23
24 - ('file', [header_lines + fromfile + tofile])
25 - ('context', [context_lines])
26 - ('hunk', [hunk_lines])
27 - ('range', (-start,len, +start,len, proc))
28 """
29 lr = patch.linereader(fp)
30
31 def scanwhile(first, p):
32 """scan lr while predicate holds"""
33 lines = [first]
34 while True:
35 line = lr.readline()
36 if not line:
37 break
38 if p(line):
39 lines.append(line)
40 else:
41 lr.push(line)
42 break
43 return lines
44
45 while True:
46 line = lr.readline()
47 if not line:
48 break
49 if line.startswith('diff --git a/') or line.startswith('diff -r '):
50 def notheader(line):
51 s = line.split(None, 1)
52 return not s or s[0] not in ('---', 'diff')
53 header = scanwhile(line, notheader)
54 fromfile = lr.readline()
55 if fromfile.startswith('---'):
56 tofile = lr.readline()
57 header += [fromfile, tofile]
58 else:
59 lr.push(fromfile)
60 yield 'file', header
61 elif line[0] == ' ':
62 yield 'context', scanwhile(line, lambda l: l[0] in ' \\')
63 elif line[0] in '-+':
64 yield 'hunk', scanwhile(line, lambda l: l[0] in '-+\\')
65 else:
66 m = lines_re.match(line)
67 if m:
68 yield 'range', m.groups()
69 else:
70 yield 'other', line
71
72 19
73 20 def parsepatch(fp):
74 21 """patch -> [] of headers -> [] of hunks """
@@ -141,7 +88,7 b' def parsepatch(fp):'
141 88 p = parser()
142 89
143 90 state = 'context'
144 for newstate, data in scanpatch(fp):
91 for newstate, data in patch.scanpatch(fp):
145 92 try:
146 93 p.transitions[state][newstate](p, data)
147 94 except KeyError:
@@ -1276,6 +1276,58 b' def makepatchmeta(backend, afile_orig, b'
1276 1276 gp.op = 'DELETE'
1277 1277 return gp
1278 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
1330
1279 1331 def scangitpatch(lr, firstline):
1280 1332 """
1281 1333 Git patches can emit:
General Comments 0
You need to be logged in to leave comments. Login now