##// END OF EJS Templates
record: move parsepatch from record to patch...
Laurent Charignon -
r24265:dc655360 default
parent child Browse files
Show More
@@ -16,87 +16,6 b' cmdtable = {}'
16 command = cmdutil.command(cmdtable)
16 command = cmdutil.command(cmdtable)
17 testedwith = 'internal'
17 testedwith = 'internal'
18
18
19
20 def parsepatch(fp):
21 """patch -> [] of headers -> [] of hunks """
22 class parser(object):
23 """patch parsing state machine"""
24 def __init__(self):
25 self.fromline = 0
26 self.toline = 0
27 self.proc = ''
28 self.header = None
29 self.context = []
30 self.before = []
31 self.hunk = []
32 self.headers = []
33
34 def addrange(self, limits):
35 fromstart, fromend, tostart, toend, proc = limits
36 self.fromline = int(fromstart)
37 self.toline = int(tostart)
38 self.proc = proc
39
40 def addcontext(self, context):
41 if self.hunk:
42 h = patch.recordhunk(self.header, self.fromline, self.toline,
43 self.proc, self.before, self.hunk, context)
44 self.header.hunks.append(h)
45 self.fromline += len(self.before) + h.removed
46 self.toline += len(self.before) + h.added
47 self.before = []
48 self.hunk = []
49 self.proc = ''
50 self.context = context
51
52 def addhunk(self, hunk):
53 if self.context:
54 self.before = self.context
55 self.context = []
56 self.hunk = hunk
57
58 def newfile(self, hdr):
59 self.addcontext([])
60 h = patch.header(hdr)
61 self.headers.append(h)
62 self.header = h
63
64 def addother(self, line):
65 pass # 'other' lines are ignored
66
67 def finished(self):
68 self.addcontext([])
69 return self.headers
70
71 transitions = {
72 'file': {'context': addcontext,
73 'file': newfile,
74 'hunk': addhunk,
75 'range': addrange},
76 'context': {'file': newfile,
77 'hunk': addhunk,
78 'range': addrange,
79 'other': addother},
80 'hunk': {'context': addcontext,
81 'file': newfile,
82 'range': addrange},
83 'range': {'context': addcontext,
84 'hunk': addhunk},
85 'other': {'other': addother},
86 }
87
88 p = parser()
89
90 state = 'context'
91 for newstate, data in patch.scanpatch(fp):
92 try:
93 p.transitions[state][newstate](p, data)
94 except KeyError:
95 raise patch.PatchError('unhandled transition: %s -> %s' %
96 (state, newstate))
97 state = newstate
98 return p.finished()
99
100 def filterpatch(ui, headers):
19 def filterpatch(ui, headers):
101 """Interactively filter patch chunks into applied-only chunks"""
20 """Interactively filter patch chunks into applied-only chunks"""
102
21
@@ -181,7 +100,7 b' the hunk is left unchanged.'
181 ncpatchfp.write(line)
100 ncpatchfp.write(line)
182 patchfp.close()
101 patchfp.close()
183 ncpatchfp.seek(0)
102 ncpatchfp.seek(0)
184 newpatches = parsepatch(ncpatchfp)
103 newpatches = patch.parsepatch(ncpatchfp)
185 finally:
104 finally:
186 os.unlink(patchfn)
105 os.unlink(patchfn)
187 del ncpatchfp
106 del ncpatchfp
@@ -371,7 +290,7 b' def dorecord(ui, repo, commitfunc, cmdsu'
371
290
372 # 1. filter patch, so we have intending-to apply subset of it
291 # 1. filter patch, so we have intending-to apply subset of it
373 try:
292 try:
374 chunks = filterpatch(ui, parsepatch(fp))
293 chunks = filterpatch(ui, patch.parsepatch(fp))
375 except patch.PatchError, err:
294 except patch.PatchError, err:
376 raise util.Abort(_('error parsing patch: %s') % err)
295 raise util.Abort(_('error parsing patch: %s') % err)
377
296
@@ -1191,6 +1191,86 b' def parsefilename(str):'
1191 return s
1191 return s
1192 return s[:i]
1192 return s[:i]
1193
1193
1194 def parsepatch(fp):
1195 """patch -> [] of headers -> [] of hunks """
1196 class parser(object):
1197 """patch parsing state machine"""
1198 def __init__(self):
1199 self.fromline = 0
1200 self.toline = 0
1201 self.proc = ''
1202 self.header = None
1203 self.context = []
1204 self.before = []
1205 self.hunk = []
1206 self.headers = []
1207
1208 def addrange(self, limits):
1209 fromstart, fromend, tostart, toend, proc = limits
1210 self.fromline = int(fromstart)
1211 self.toline = int(tostart)
1212 self.proc = proc
1213
1214 def addcontext(self, context):
1215 if self.hunk:
1216 h = recordhunk(self.header, self.fromline, self.toline,
1217 self.proc, self.before, self.hunk, context)
1218 self.header.hunks.append(h)
1219 self.fromline += len(self.before) + h.removed
1220 self.toline += len(self.before) + h.added
1221 self.before = []
1222 self.hunk = []
1223 self.proc = ''
1224 self.context = context
1225
1226 def addhunk(self, hunk):
1227 if self.context:
1228 self.before = self.context
1229 self.context = []
1230 self.hunk = hunk
1231
1232 def newfile(self, hdr):
1233 self.addcontext([])
1234 h = header(hdr)
1235 self.headers.append(h)
1236 self.header = h
1237
1238 def addother(self, line):
1239 pass # 'other' lines are ignored
1240
1241 def finished(self):
1242 self.addcontext([])
1243 return self.headers
1244
1245 transitions = {
1246 'file': {'context': addcontext,
1247 'file': newfile,
1248 'hunk': addhunk,
1249 'range': addrange},
1250 'context': {'file': newfile,
1251 'hunk': addhunk,
1252 'range': addrange,
1253 'other': addother},
1254 'hunk': {'context': addcontext,
1255 'file': newfile,
1256 'range': addrange},
1257 'range': {'context': addcontext,
1258 'hunk': addhunk},
1259 'other': {'other': addother},
1260 }
1261
1262 p = parser()
1263
1264 state = 'context'
1265 for newstate, data in scanpatch(fp):
1266 try:
1267 p.transitions[state][newstate](p, data)
1268 except KeyError:
1269 raise PatchError('unhandled transition: %s -> %s' %
1270 (state, newstate))
1271 state = newstate
1272 return p.finished()
1273
1194 def pathtransform(path, strip, prefix):
1274 def pathtransform(path, strip, prefix):
1195 '''turn a path from a patch into a path suitable for the repository
1275 '''turn a path from a patch into a path suitable for the repository
1196
1276
General Comments 0
You need to be logged in to leave comments. Login now