Show More
@@ -15,9 +15,17 b' import copy, cStringIO, errno, operator,' | |||
|
15 | 15 | lines_re = re.compile(r'@@ -(\d+),(\d+) \+(\d+),(\d+) @@\s*(.*)') |
|
16 | 16 | |
|
17 | 17 | def scanpatch(fp): |
|
18 | """like patch.iterhunks, but yield different events | |
|
19 | ||
|
20 | - ('file', [header_lines + fromfile + tofile]) | |
|
21 | - ('context', [context_lines]) | |
|
22 | - ('hunk', [hunk_lines]) | |
|
23 | - ('range', (-start,len, +start,len, diffp)) | |
|
24 | """ | |
|
18 | 25 | lr = patch.linereader(fp) |
|
19 | 26 | |
|
20 | 27 | def scanwhile(first, p): |
|
28 | """scan lr while predicate holds""" | |
|
21 | 29 | lines = [first] |
|
22 | 30 | while True: |
|
23 | 31 | line = lr.readline() |
@@ -58,6 +66,10 b' def scanpatch(fp):' | |||
|
58 | 66 | raise patch.PatchError('unknown patch content: %r' % line) |
|
59 | 67 | |
|
60 | 68 | class header(object): |
|
69 | """patch header | |
|
70 | ||
|
71 | XXX shoudn't we move this to mercurial/patch.py ? | |
|
72 | """ | |
|
61 | 73 | diff_re = re.compile('diff --git a/(.*) b/(.*)$') |
|
62 | 74 | allhunks_re = re.compile('(?:index|new file|deleted file) ') |
|
63 | 75 | pretty_re = re.compile('(?:new file|deleted file) ') |
@@ -115,11 +127,16 b' class header(object):' | |||
|
115 | 127 | return True |
|
116 | 128 | |
|
117 | 129 | def countchanges(hunk): |
|
130 | """hunk -> (n+,n-)""" | |
|
118 | 131 | add = len([h for h in hunk if h[0] == '+']) |
|
119 | 132 | rem = len([h for h in hunk if h[0] == '-']) |
|
120 | 133 | return add, rem |
|
121 | 134 | |
|
122 | 135 | class hunk(object): |
|
136 | """patch hunk | |
|
137 | ||
|
138 | XXX shouldn't we merge this with patch.hunk ? | |
|
139 | """ | |
|
123 | 140 | maxcontext = 3 |
|
124 | 141 | |
|
125 | 142 | def __init__(self, header, fromline, toline, proc, before, hunk, after): |
@@ -154,7 +171,9 b' class hunk(object):' | |||
|
154 | 171 | return '<hunk %r@%d>' % (self.filename(), self.fromline) |
|
155 | 172 | |
|
156 | 173 | def parsepatch(fp): |
|
174 | """patch -> [] of hunks """ | |
|
157 | 175 | class parser(object): |
|
176 | """patch parsing state machine""" | |
|
158 | 177 | def __init__(self): |
|
159 | 178 | self.fromline = 0 |
|
160 | 179 | self.toline = 0 |
@@ -227,10 +246,14 b' def parsepatch(fp):' | |||
|
227 | 246 | return p.finished() |
|
228 | 247 | |
|
229 | 248 | def filterpatch(ui, chunks): |
|
249 | """Interactively filter patch chunks into applied-only chunks""" | |
|
230 | 250 | chunks = list(chunks) |
|
231 | 251 | chunks.reverse() |
|
232 | 252 | seen = {} |
|
233 | 253 | def consumefile(): |
|
254 | """fetch next portion from chunks until a 'header' is seen | |
|
255 | NB: header == new-file mark | |
|
256 | """ | |
|
234 | 257 | consumed = [] |
|
235 | 258 | while chunks: |
|
236 | 259 | if isinstance(chunks[-1], header): |
@@ -238,10 +261,20 b' def filterpatch(ui, chunks):' | |||
|
238 | 261 | else: |
|
239 | 262 | consumed.append(chunks.pop()) |
|
240 | 263 | return consumed |
|
241 | resp_all = [None] | |
|
242 | resp_file = [None] | |
|
243 | applied = {} | |
|
264 | ||
|
265 | resp_all = [None] # this two are changed from inside prompt, | |
|
266 | resp_file = [None] # so can't be usual variables | |
|
267 | applied = {} # 'filename' -> [] of chunks | |
|
244 | 268 | def prompt(query): |
|
269 | """prompt query, and process base inputs | |
|
270 | ||
|
271 | - y/n for the rest of file | |
|
272 | - y/n for the rest | |
|
273 | - ? (help) | |
|
274 | - q (quit) | |
|
275 | ||
|
276 | else, input is returned to the caller. | |
|
277 | """ | |
|
245 | 278 | if resp_all[0] is not None: |
|
246 | 279 | return resp_all[0] |
|
247 | 280 | if resp_file[0] is not None: |
@@ -268,6 +301,7 b' def filterpatch(ui, chunks):' | |||
|
268 | 301 | while chunks: |
|
269 | 302 | chunk = chunks.pop() |
|
270 | 303 | if isinstance(chunk, header): |
|
304 | # new-file mark | |
|
271 | 305 | resp_file = [None] |
|
272 | 306 | fixoffset = 0 |
|
273 | 307 | hdr = ''.join(chunk.header) |
@@ -286,6 +320,7 b' def filterpatch(ui, chunks):' | |||
|
286 | 320 | else: |
|
287 | 321 | consumefile() |
|
288 | 322 | else: |
|
323 | # new hunk | |
|
289 | 324 | if resp_file[0] is None and resp_all[0] is None: |
|
290 | 325 | chunk.pretty(ui) |
|
291 | 326 | r = prompt(_('record this change to %r?') % |
General Comments 0
You need to be logged in to leave comments.
Login now