Show More
@@ -1,2951 +1,2866 | |||
|
1 | 1 | # patch.py - patch file parsing routines |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006 Brendan Cully <brendan@kublai.com> |
|
4 | 4 | # Copyright 2007 Chris Mason <chris.mason@oracle.com> |
|
5 | 5 | # |
|
6 | 6 | # This software may be used and distributed according to the terms of the |
|
7 | 7 | # GNU General Public License version 2 or any later version. |
|
8 | 8 | |
|
9 | 9 | from __future__ import absolute_import, print_function |
|
10 | 10 | |
|
11 | 11 | import collections |
|
12 | 12 | import contextlib |
|
13 | 13 | import copy |
|
14 | 14 | import email |
|
15 | 15 | import errno |
|
16 | 16 | import hashlib |
|
17 | 17 | import os |
|
18 | 18 | import posixpath |
|
19 | 19 | import re |
|
20 | 20 | import shutil |
|
21 | 21 | import zlib |
|
22 | 22 | |
|
23 | 23 | from .i18n import _ |
|
24 | 24 | from .node import ( |
|
25 | 25 | hex, |
|
26 | 26 | short, |
|
27 | 27 | ) |
|
28 | 28 | from . import ( |
|
29 | 29 | copies, |
|
30 | 30 | diffhelper, |
|
31 | 31 | encoding, |
|
32 | 32 | error, |
|
33 | 33 | mail, |
|
34 | 34 | mdiff, |
|
35 | 35 | pathutil, |
|
36 | 36 | pycompat, |
|
37 | 37 | scmutil, |
|
38 | 38 | similar, |
|
39 | 39 | util, |
|
40 | 40 | vfs as vfsmod, |
|
41 | 41 | ) |
|
42 | 42 | from .utils import ( |
|
43 | 43 | dateutil, |
|
44 | diffutil, | |
|
44 | 45 | procutil, |
|
45 | 46 | stringutil, |
|
46 | 47 | ) |
|
47 | 48 | |
|
48 | 49 | stringio = util.stringio |
|
49 | 50 | |
|
50 | 51 | gitre = re.compile(br'diff --git a/(.*) b/(.*)') |
|
51 | 52 | tabsplitter = re.compile(br'(\t+|[^\t]+)') |
|
52 | 53 | wordsplitter = re.compile(br'(\t+| +|[a-zA-Z0-9_\x80-\xff]+|' |
|
53 | 54 | b'[^ \ta-zA-Z0-9_\x80-\xff])') |
|
54 | 55 | |
|
55 | 56 | PatchError = error.PatchError |
|
56 | 57 | |
|
57 | 58 | # public functions |
|
58 | 59 | |
|
59 | 60 | def split(stream): |
|
60 | 61 | '''return an iterator of individual patches from a stream''' |
|
61 | 62 | def isheader(line, inheader): |
|
62 | 63 | if inheader and line.startswith((' ', '\t')): |
|
63 | 64 | # continuation |
|
64 | 65 | return True |
|
65 | 66 | if line.startswith((' ', '-', '+')): |
|
66 | 67 | # diff line - don't check for header pattern in there |
|
67 | 68 | return False |
|
68 | 69 | l = line.split(': ', 1) |
|
69 | 70 | return len(l) == 2 and ' ' not in l[0] |
|
70 | 71 | |
|
71 | 72 | def chunk(lines): |
|
72 | 73 | return stringio(''.join(lines)) |
|
73 | 74 | |
|
74 | 75 | def hgsplit(stream, cur): |
|
75 | 76 | inheader = True |
|
76 | 77 | |
|
77 | 78 | for line in stream: |
|
78 | 79 | if not line.strip(): |
|
79 | 80 | inheader = False |
|
80 | 81 | if not inheader and line.startswith('# HG changeset patch'): |
|
81 | 82 | yield chunk(cur) |
|
82 | 83 | cur = [] |
|
83 | 84 | inheader = True |
|
84 | 85 | |
|
85 | 86 | cur.append(line) |
|
86 | 87 | |
|
87 | 88 | if cur: |
|
88 | 89 | yield chunk(cur) |
|
89 | 90 | |
|
90 | 91 | def mboxsplit(stream, cur): |
|
91 | 92 | for line in stream: |
|
92 | 93 | if line.startswith('From '): |
|
93 | 94 | for c in split(chunk(cur[1:])): |
|
94 | 95 | yield c |
|
95 | 96 | cur = [] |
|
96 | 97 | |
|
97 | 98 | cur.append(line) |
|
98 | 99 | |
|
99 | 100 | if cur: |
|
100 | 101 | for c in split(chunk(cur[1:])): |
|
101 | 102 | yield c |
|
102 | 103 | |
|
103 | 104 | def mimesplit(stream, cur): |
|
104 | 105 | def msgfp(m): |
|
105 | 106 | fp = stringio() |
|
106 | 107 | g = email.Generator.Generator(fp, mangle_from_=False) |
|
107 | 108 | g.flatten(m) |
|
108 | 109 | fp.seek(0) |
|
109 | 110 | return fp |
|
110 | 111 | |
|
111 | 112 | for line in stream: |
|
112 | 113 | cur.append(line) |
|
113 | 114 | c = chunk(cur) |
|
114 | 115 | |
|
115 | 116 | m = mail.parse(c) |
|
116 | 117 | if not m.is_multipart(): |
|
117 | 118 | yield msgfp(m) |
|
118 | 119 | else: |
|
119 | 120 | ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') |
|
120 | 121 | for part in m.walk(): |
|
121 | 122 | ct = part.get_content_type() |
|
122 | 123 | if ct not in ok_types: |
|
123 | 124 | continue |
|
124 | 125 | yield msgfp(part) |
|
125 | 126 | |
|
126 | 127 | def headersplit(stream, cur): |
|
127 | 128 | inheader = False |
|
128 | 129 | |
|
129 | 130 | for line in stream: |
|
130 | 131 | if not inheader and isheader(line, inheader): |
|
131 | 132 | yield chunk(cur) |
|
132 | 133 | cur = [] |
|
133 | 134 | inheader = True |
|
134 | 135 | if inheader and not isheader(line, inheader): |
|
135 | 136 | inheader = False |
|
136 | 137 | |
|
137 | 138 | cur.append(line) |
|
138 | 139 | |
|
139 | 140 | if cur: |
|
140 | 141 | yield chunk(cur) |
|
141 | 142 | |
|
142 | 143 | def remainder(cur): |
|
143 | 144 | yield chunk(cur) |
|
144 | 145 | |
|
145 | 146 | class fiter(object): |
|
146 | 147 | def __init__(self, fp): |
|
147 | 148 | self.fp = fp |
|
148 | 149 | |
|
149 | 150 | def __iter__(self): |
|
150 | 151 | return self |
|
151 | 152 | |
|
152 | 153 | def next(self): |
|
153 | 154 | l = self.fp.readline() |
|
154 | 155 | if not l: |
|
155 | 156 | raise StopIteration |
|
156 | 157 | return l |
|
157 | 158 | |
|
158 | 159 | __next__ = next |
|
159 | 160 | |
|
160 | 161 | inheader = False |
|
161 | 162 | cur = [] |
|
162 | 163 | |
|
163 | 164 | mimeheaders = ['content-type'] |
|
164 | 165 | |
|
165 | 166 | if not util.safehasattr(stream, 'next'): |
|
166 | 167 | # http responses, for example, have readline but not next |
|
167 | 168 | stream = fiter(stream) |
|
168 | 169 | |
|
169 | 170 | for line in stream: |
|
170 | 171 | cur.append(line) |
|
171 | 172 | if line.startswith('# HG changeset patch'): |
|
172 | 173 | return hgsplit(stream, cur) |
|
173 | 174 | elif line.startswith('From '): |
|
174 | 175 | return mboxsplit(stream, cur) |
|
175 | 176 | elif isheader(line, inheader): |
|
176 | 177 | inheader = True |
|
177 | 178 | if line.split(':', 1)[0].lower() in mimeheaders: |
|
178 | 179 | # let email parser handle this |
|
179 | 180 | return mimesplit(stream, cur) |
|
180 | 181 | elif line.startswith('--- ') and inheader: |
|
181 | 182 | # No evil headers seen by diff start, split by hand |
|
182 | 183 | return headersplit(stream, cur) |
|
183 | 184 | # Not enough info, keep reading |
|
184 | 185 | |
|
185 | 186 | # if we are here, we have a very plain patch |
|
186 | 187 | return remainder(cur) |
|
187 | 188 | |
|
188 | 189 | ## Some facility for extensible patch parsing: |
|
189 | 190 | # list of pairs ("header to match", "data key") |
|
190 | 191 | patchheadermap = [('Date', 'date'), |
|
191 | 192 | ('Branch', 'branch'), |
|
192 | 193 | ('Node ID', 'nodeid'), |
|
193 | 194 | ] |
|
194 | 195 | |
|
195 | 196 | @contextlib.contextmanager |
|
196 | 197 | def extract(ui, fileobj): |
|
197 | 198 | '''extract patch from data read from fileobj. |
|
198 | 199 | |
|
199 | 200 | patch can be a normal patch or contained in an email message. |
|
200 | 201 | |
|
201 | 202 | return a dictionary. Standard keys are: |
|
202 | 203 | - filename, |
|
203 | 204 | - message, |
|
204 | 205 | - user, |
|
205 | 206 | - date, |
|
206 | 207 | - branch, |
|
207 | 208 | - node, |
|
208 | 209 | - p1, |
|
209 | 210 | - p2. |
|
210 | 211 | Any item can be missing from the dictionary. If filename is missing, |
|
211 | 212 | fileobj did not contain a patch. Caller must unlink filename when done.''' |
|
212 | 213 | |
|
213 | 214 | fd, tmpname = pycompat.mkstemp(prefix='hg-patch-') |
|
214 | 215 | tmpfp = os.fdopen(fd, r'wb') |
|
215 | 216 | try: |
|
216 | 217 | yield _extract(ui, fileobj, tmpname, tmpfp) |
|
217 | 218 | finally: |
|
218 | 219 | tmpfp.close() |
|
219 | 220 | os.unlink(tmpname) |
|
220 | 221 | |
|
221 | 222 | def _extract(ui, fileobj, tmpname, tmpfp): |
|
222 | 223 | |
|
223 | 224 | # attempt to detect the start of a patch |
|
224 | 225 | # (this heuristic is borrowed from quilt) |
|
225 | 226 | diffre = re.compile(br'^(?:Index:[ \t]|diff[ \t]-|RCS file: |' |
|
226 | 227 | br'retrieving revision [0-9]+(\.[0-9]+)*$|' |
|
227 | 228 | br'---[ \t].*?^\+\+\+[ \t]|' |
|
228 | 229 | br'\*\*\*[ \t].*?^---[ \t])', |
|
229 | 230 | re.MULTILINE | re.DOTALL) |
|
230 | 231 | |
|
231 | 232 | data = {} |
|
232 | 233 | |
|
233 | 234 | msg = mail.parse(fileobj) |
|
234 | 235 | |
|
235 | 236 | subject = msg[r'Subject'] and mail.headdecode(msg[r'Subject']) |
|
236 | 237 | data['user'] = msg[r'From'] and mail.headdecode(msg[r'From']) |
|
237 | 238 | if not subject and not data['user']: |
|
238 | 239 | # Not an email, restore parsed headers if any |
|
239 | 240 | subject = '\n'.join(': '.join(map(encoding.strtolocal, h)) |
|
240 | 241 | for h in msg.items()) + '\n' |
|
241 | 242 | |
|
242 | 243 | # should try to parse msg['Date'] |
|
243 | 244 | parents = [] |
|
244 | 245 | |
|
245 | 246 | if subject: |
|
246 | 247 | if subject.startswith('[PATCH'): |
|
247 | 248 | pend = subject.find(']') |
|
248 | 249 | if pend >= 0: |
|
249 | 250 | subject = subject[pend + 1:].lstrip() |
|
250 | 251 | subject = re.sub(br'\n[ \t]+', ' ', subject) |
|
251 | 252 | ui.debug('Subject: %s\n' % subject) |
|
252 | 253 | if data['user']: |
|
253 | 254 | ui.debug('From: %s\n' % data['user']) |
|
254 | 255 | diffs_seen = 0 |
|
255 | 256 | ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') |
|
256 | 257 | message = '' |
|
257 | 258 | for part in msg.walk(): |
|
258 | 259 | content_type = pycompat.bytestr(part.get_content_type()) |
|
259 | 260 | ui.debug('Content-Type: %s\n' % content_type) |
|
260 | 261 | if content_type not in ok_types: |
|
261 | 262 | continue |
|
262 | 263 | payload = part.get_payload(decode=True) |
|
263 | 264 | m = diffre.search(payload) |
|
264 | 265 | if m: |
|
265 | 266 | hgpatch = False |
|
266 | 267 | hgpatchheader = False |
|
267 | 268 | ignoretext = False |
|
268 | 269 | |
|
269 | 270 | ui.debug('found patch at byte %d\n' % m.start(0)) |
|
270 | 271 | diffs_seen += 1 |
|
271 | 272 | cfp = stringio() |
|
272 | 273 | for line in payload[:m.start(0)].splitlines(): |
|
273 | 274 | if line.startswith('# HG changeset patch') and not hgpatch: |
|
274 | 275 | ui.debug('patch generated by hg export\n') |
|
275 | 276 | hgpatch = True |
|
276 | 277 | hgpatchheader = True |
|
277 | 278 | # drop earlier commit message content |
|
278 | 279 | cfp.seek(0) |
|
279 | 280 | cfp.truncate() |
|
280 | 281 | subject = None |
|
281 | 282 | elif hgpatchheader: |
|
282 | 283 | if line.startswith('# User '): |
|
283 | 284 | data['user'] = line[7:] |
|
284 | 285 | ui.debug('From: %s\n' % data['user']) |
|
285 | 286 | elif line.startswith("# Parent "): |
|
286 | 287 | parents.append(line[9:].lstrip()) |
|
287 | 288 | elif line.startswith("# "): |
|
288 | 289 | for header, key in patchheadermap: |
|
289 | 290 | prefix = '# %s ' % header |
|
290 | 291 | if line.startswith(prefix): |
|
291 | 292 | data[key] = line[len(prefix):] |
|
292 | 293 | else: |
|
293 | 294 | hgpatchheader = False |
|
294 | 295 | elif line == '---': |
|
295 | 296 | ignoretext = True |
|
296 | 297 | if not hgpatchheader and not ignoretext: |
|
297 | 298 | cfp.write(line) |
|
298 | 299 | cfp.write('\n') |
|
299 | 300 | message = cfp.getvalue() |
|
300 | 301 | if tmpfp: |
|
301 | 302 | tmpfp.write(payload) |
|
302 | 303 | if not payload.endswith('\n'): |
|
303 | 304 | tmpfp.write('\n') |
|
304 | 305 | elif not diffs_seen and message and content_type == 'text/plain': |
|
305 | 306 | message += '\n' + payload |
|
306 | 307 | |
|
307 | 308 | if subject and not message.startswith(subject): |
|
308 | 309 | message = '%s\n%s' % (subject, message) |
|
309 | 310 | data['message'] = message |
|
310 | 311 | tmpfp.close() |
|
311 | 312 | if parents: |
|
312 | 313 | data['p1'] = parents.pop(0) |
|
313 | 314 | if parents: |
|
314 | 315 | data['p2'] = parents.pop(0) |
|
315 | 316 | |
|
316 | 317 | if diffs_seen: |
|
317 | 318 | data['filename'] = tmpname |
|
318 | 319 | |
|
319 | 320 | return data |
|
320 | 321 | |
|
321 | 322 | class patchmeta(object): |
|
322 | 323 | """Patched file metadata |
|
323 | 324 | |
|
324 | 325 | 'op' is the performed operation within ADD, DELETE, RENAME, MODIFY |
|
325 | 326 | or COPY. 'path' is patched file path. 'oldpath' is set to the |
|
326 | 327 | origin file when 'op' is either COPY or RENAME, None otherwise. If |
|
327 | 328 | file mode is changed, 'mode' is a tuple (islink, isexec) where |
|
328 | 329 | 'islink' is True if the file is a symlink and 'isexec' is True if |
|
329 | 330 | the file is executable. Otherwise, 'mode' is None. |
|
330 | 331 | """ |
|
331 | 332 | def __init__(self, path): |
|
332 | 333 | self.path = path |
|
333 | 334 | self.oldpath = None |
|
334 | 335 | self.mode = None |
|
335 | 336 | self.op = 'MODIFY' |
|
336 | 337 | self.binary = False |
|
337 | 338 | |
|
338 | 339 | def setmode(self, mode): |
|
339 | 340 | islink = mode & 0o20000 |
|
340 | 341 | isexec = mode & 0o100 |
|
341 | 342 | self.mode = (islink, isexec) |
|
342 | 343 | |
|
343 | 344 | def copy(self): |
|
344 | 345 | other = patchmeta(self.path) |
|
345 | 346 | other.oldpath = self.oldpath |
|
346 | 347 | other.mode = self.mode |
|
347 | 348 | other.op = self.op |
|
348 | 349 | other.binary = self.binary |
|
349 | 350 | return other |
|
350 | 351 | |
|
351 | 352 | def _ispatchinga(self, afile): |
|
352 | 353 | if afile == '/dev/null': |
|
353 | 354 | return self.op == 'ADD' |
|
354 | 355 | return afile == 'a/' + (self.oldpath or self.path) |
|
355 | 356 | |
|
356 | 357 | def _ispatchingb(self, bfile): |
|
357 | 358 | if bfile == '/dev/null': |
|
358 | 359 | return self.op == 'DELETE' |
|
359 | 360 | return bfile == 'b/' + self.path |
|
360 | 361 | |
|
361 | 362 | def ispatching(self, afile, bfile): |
|
362 | 363 | return self._ispatchinga(afile) and self._ispatchingb(bfile) |
|
363 | 364 | |
|
364 | 365 | def __repr__(self): |
|
365 | 366 | return "<patchmeta %s %r>" % (self.op, self.path) |
|
366 | 367 | |
|
367 | 368 | def readgitpatch(lr): |
|
368 | 369 | """extract git-style metadata about patches from <patchname>""" |
|
369 | 370 | |
|
370 | 371 | # Filter patch for git information |
|
371 | 372 | gp = None |
|
372 | 373 | gitpatches = [] |
|
373 | 374 | for line in lr: |
|
374 | 375 | line = line.rstrip(' \r\n') |
|
375 | 376 | if line.startswith('diff --git a/'): |
|
376 | 377 | m = gitre.match(line) |
|
377 | 378 | if m: |
|
378 | 379 | if gp: |
|
379 | 380 | gitpatches.append(gp) |
|
380 | 381 | dst = m.group(2) |
|
381 | 382 | gp = patchmeta(dst) |
|
382 | 383 | elif gp: |
|
383 | 384 | if line.startswith('--- '): |
|
384 | 385 | gitpatches.append(gp) |
|
385 | 386 | gp = None |
|
386 | 387 | continue |
|
387 | 388 | if line.startswith('rename from '): |
|
388 | 389 | gp.op = 'RENAME' |
|
389 | 390 | gp.oldpath = line[12:] |
|
390 | 391 | elif line.startswith('rename to '): |
|
391 | 392 | gp.path = line[10:] |
|
392 | 393 | elif line.startswith('copy from '): |
|
393 | 394 | gp.op = 'COPY' |
|
394 | 395 | gp.oldpath = line[10:] |
|
395 | 396 | elif line.startswith('copy to '): |
|
396 | 397 | gp.path = line[8:] |
|
397 | 398 | elif line.startswith('deleted file'): |
|
398 | 399 | gp.op = 'DELETE' |
|
399 | 400 | elif line.startswith('new file mode '): |
|
400 | 401 | gp.op = 'ADD' |
|
401 | 402 | gp.setmode(int(line[-6:], 8)) |
|
402 | 403 | elif line.startswith('new mode '): |
|
403 | 404 | gp.setmode(int(line[-6:], 8)) |
|
404 | 405 | elif line.startswith('GIT binary patch'): |
|
405 | 406 | gp.binary = True |
|
406 | 407 | if gp: |
|
407 | 408 | gitpatches.append(gp) |
|
408 | 409 | |
|
409 | 410 | return gitpatches |
|
410 | 411 | |
|
411 | 412 | class linereader(object): |
|
412 | 413 | # simple class to allow pushing lines back into the input stream |
|
413 | 414 | def __init__(self, fp): |
|
414 | 415 | self.fp = fp |
|
415 | 416 | self.buf = [] |
|
416 | 417 | |
|
417 | 418 | def push(self, line): |
|
418 | 419 | if line is not None: |
|
419 | 420 | self.buf.append(line) |
|
420 | 421 | |
|
421 | 422 | def readline(self): |
|
422 | 423 | if self.buf: |
|
423 | 424 | l = self.buf[0] |
|
424 | 425 | del self.buf[0] |
|
425 | 426 | return l |
|
426 | 427 | return self.fp.readline() |
|
427 | 428 | |
|
428 | 429 | def __iter__(self): |
|
429 | 430 | return iter(self.readline, '') |
|
430 | 431 | |
|
431 | 432 | class abstractbackend(object): |
|
432 | 433 | def __init__(self, ui): |
|
433 | 434 | self.ui = ui |
|
434 | 435 | |
|
435 | 436 | def getfile(self, fname): |
|
436 | 437 | """Return target file data and flags as a (data, (islink, |
|
437 | 438 | isexec)) tuple. Data is None if file is missing/deleted. |
|
438 | 439 | """ |
|
439 | 440 | raise NotImplementedError |
|
440 | 441 | |
|
441 | 442 | def setfile(self, fname, data, mode, copysource): |
|
442 | 443 | """Write data to target file fname and set its mode. mode is a |
|
443 | 444 | (islink, isexec) tuple. If data is None, the file content should |
|
444 | 445 | be left unchanged. If the file is modified after being copied, |
|
445 | 446 | copysource is set to the original file name. |
|
446 | 447 | """ |
|
447 | 448 | raise NotImplementedError |
|
448 | 449 | |
|
449 | 450 | def unlink(self, fname): |
|
450 | 451 | """Unlink target file.""" |
|
451 | 452 | raise NotImplementedError |
|
452 | 453 | |
|
453 | 454 | def writerej(self, fname, failed, total, lines): |
|
454 | 455 | """Write rejected lines for fname. total is the number of hunks |
|
455 | 456 | which failed to apply and total the total number of hunks for this |
|
456 | 457 | files. |
|
457 | 458 | """ |
|
458 | 459 | |
|
459 | 460 | def exists(self, fname): |
|
460 | 461 | raise NotImplementedError |
|
461 | 462 | |
|
462 | 463 | def close(self): |
|
463 | 464 | raise NotImplementedError |
|
464 | 465 | |
|
465 | 466 | class fsbackend(abstractbackend): |
|
466 | 467 | def __init__(self, ui, basedir): |
|
467 | 468 | super(fsbackend, self).__init__(ui) |
|
468 | 469 | self.opener = vfsmod.vfs(basedir) |
|
469 | 470 | |
|
470 | 471 | def getfile(self, fname): |
|
471 | 472 | if self.opener.islink(fname): |
|
472 | 473 | return (self.opener.readlink(fname), (True, False)) |
|
473 | 474 | |
|
474 | 475 | isexec = False |
|
475 | 476 | try: |
|
476 | 477 | isexec = self.opener.lstat(fname).st_mode & 0o100 != 0 |
|
477 | 478 | except OSError as e: |
|
478 | 479 | if e.errno != errno.ENOENT: |
|
479 | 480 | raise |
|
480 | 481 | try: |
|
481 | 482 | return (self.opener.read(fname), (False, isexec)) |
|
482 | 483 | except IOError as e: |
|
483 | 484 | if e.errno != errno.ENOENT: |
|
484 | 485 | raise |
|
485 | 486 | return None, None |
|
486 | 487 | |
|
487 | 488 | def setfile(self, fname, data, mode, copysource): |
|
488 | 489 | islink, isexec = mode |
|
489 | 490 | if data is None: |
|
490 | 491 | self.opener.setflags(fname, islink, isexec) |
|
491 | 492 | return |
|
492 | 493 | if islink: |
|
493 | 494 | self.opener.symlink(data, fname) |
|
494 | 495 | else: |
|
495 | 496 | self.opener.write(fname, data) |
|
496 | 497 | if isexec: |
|
497 | 498 | self.opener.setflags(fname, False, True) |
|
498 | 499 | |
|
499 | 500 | def unlink(self, fname): |
|
500 | 501 | rmdir = self.ui.configbool('experimental', 'removeemptydirs') |
|
501 | 502 | self.opener.unlinkpath(fname, ignoremissing=True, rmdir=rmdir) |
|
502 | 503 | |
|
503 | 504 | def writerej(self, fname, failed, total, lines): |
|
504 | 505 | fname = fname + ".rej" |
|
505 | 506 | self.ui.warn( |
|
506 | 507 | _("%d out of %d hunks FAILED -- saving rejects to file %s\n") % |
|
507 | 508 | (failed, total, fname)) |
|
508 | 509 | fp = self.opener(fname, 'w') |
|
509 | 510 | fp.writelines(lines) |
|
510 | 511 | fp.close() |
|
511 | 512 | |
|
512 | 513 | def exists(self, fname): |
|
513 | 514 | return self.opener.lexists(fname) |
|
514 | 515 | |
|
515 | 516 | class workingbackend(fsbackend): |
|
516 | 517 | def __init__(self, ui, repo, similarity): |
|
517 | 518 | super(workingbackend, self).__init__(ui, repo.root) |
|
518 | 519 | self.repo = repo |
|
519 | 520 | self.similarity = similarity |
|
520 | 521 | self.removed = set() |
|
521 | 522 | self.changed = set() |
|
522 | 523 | self.copied = [] |
|
523 | 524 | |
|
524 | 525 | def _checkknown(self, fname): |
|
525 | 526 | if self.repo.dirstate[fname] == '?' and self.exists(fname): |
|
526 | 527 | raise PatchError(_('cannot patch %s: file is not tracked') % fname) |
|
527 | 528 | |
|
528 | 529 | def setfile(self, fname, data, mode, copysource): |
|
529 | 530 | self._checkknown(fname) |
|
530 | 531 | super(workingbackend, self).setfile(fname, data, mode, copysource) |
|
531 | 532 | if copysource is not None: |
|
532 | 533 | self.copied.append((copysource, fname)) |
|
533 | 534 | self.changed.add(fname) |
|
534 | 535 | |
|
535 | 536 | def unlink(self, fname): |
|
536 | 537 | self._checkknown(fname) |
|
537 | 538 | super(workingbackend, self).unlink(fname) |
|
538 | 539 | self.removed.add(fname) |
|
539 | 540 | self.changed.add(fname) |
|
540 | 541 | |
|
541 | 542 | def close(self): |
|
542 | 543 | wctx = self.repo[None] |
|
543 | 544 | changed = set(self.changed) |
|
544 | 545 | for src, dst in self.copied: |
|
545 | 546 | scmutil.dirstatecopy(self.ui, self.repo, wctx, src, dst) |
|
546 | 547 | if self.removed: |
|
547 | 548 | wctx.forget(sorted(self.removed)) |
|
548 | 549 | for f in self.removed: |
|
549 | 550 | if f not in self.repo.dirstate: |
|
550 | 551 | # File was deleted and no longer belongs to the |
|
551 | 552 | # dirstate, it was probably marked added then |
|
552 | 553 | # deleted, and should not be considered by |
|
553 | 554 | # marktouched(). |
|
554 | 555 | changed.discard(f) |
|
555 | 556 | if changed: |
|
556 | 557 | scmutil.marktouched(self.repo, changed, self.similarity) |
|
557 | 558 | return sorted(self.changed) |
|
558 | 559 | |
|
559 | 560 | class filestore(object): |
|
560 | 561 | def __init__(self, maxsize=None): |
|
561 | 562 | self.opener = None |
|
562 | 563 | self.files = {} |
|
563 | 564 | self.created = 0 |
|
564 | 565 | self.maxsize = maxsize |
|
565 | 566 | if self.maxsize is None: |
|
566 | 567 | self.maxsize = 4*(2**20) |
|
567 | 568 | self.size = 0 |
|
568 | 569 | self.data = {} |
|
569 | 570 | |
|
570 | 571 | def setfile(self, fname, data, mode, copied=None): |
|
571 | 572 | if self.maxsize < 0 or (len(data) + self.size) <= self.maxsize: |
|
572 | 573 | self.data[fname] = (data, mode, copied) |
|
573 | 574 | self.size += len(data) |
|
574 | 575 | else: |
|
575 | 576 | if self.opener is None: |
|
576 | 577 | root = pycompat.mkdtemp(prefix='hg-patch-') |
|
577 | 578 | self.opener = vfsmod.vfs(root) |
|
578 | 579 | # Avoid filename issues with these simple names |
|
579 | 580 | fn = '%d' % self.created |
|
580 | 581 | self.opener.write(fn, data) |
|
581 | 582 | self.created += 1 |
|
582 | 583 | self.files[fname] = (fn, mode, copied) |
|
583 | 584 | |
|
584 | 585 | def getfile(self, fname): |
|
585 | 586 | if fname in self.data: |
|
586 | 587 | return self.data[fname] |
|
587 | 588 | if not self.opener or fname not in self.files: |
|
588 | 589 | return None, None, None |
|
589 | 590 | fn, mode, copied = self.files[fname] |
|
590 | 591 | return self.opener.read(fn), mode, copied |
|
591 | 592 | |
|
592 | 593 | def close(self): |
|
593 | 594 | if self.opener: |
|
594 | 595 | shutil.rmtree(self.opener.base) |
|
595 | 596 | |
|
596 | 597 | class repobackend(abstractbackend): |
|
597 | 598 | def __init__(self, ui, repo, ctx, store): |
|
598 | 599 | super(repobackend, self).__init__(ui) |
|
599 | 600 | self.repo = repo |
|
600 | 601 | self.ctx = ctx |
|
601 | 602 | self.store = store |
|
602 | 603 | self.changed = set() |
|
603 | 604 | self.removed = set() |
|
604 | 605 | self.copied = {} |
|
605 | 606 | |
|
606 | 607 | def _checkknown(self, fname): |
|
607 | 608 | if fname not in self.ctx: |
|
608 | 609 | raise PatchError(_('cannot patch %s: file is not tracked') % fname) |
|
609 | 610 | |
|
610 | 611 | def getfile(self, fname): |
|
611 | 612 | try: |
|
612 | 613 | fctx = self.ctx[fname] |
|
613 | 614 | except error.LookupError: |
|
614 | 615 | return None, None |
|
615 | 616 | flags = fctx.flags() |
|
616 | 617 | return fctx.data(), ('l' in flags, 'x' in flags) |
|
617 | 618 | |
|
618 | 619 | def setfile(self, fname, data, mode, copysource): |
|
619 | 620 | if copysource: |
|
620 | 621 | self._checkknown(copysource) |
|
621 | 622 | if data is None: |
|
622 | 623 | data = self.ctx[fname].data() |
|
623 | 624 | self.store.setfile(fname, data, mode, copysource) |
|
624 | 625 | self.changed.add(fname) |
|
625 | 626 | if copysource: |
|
626 | 627 | self.copied[fname] = copysource |
|
627 | 628 | |
|
628 | 629 | def unlink(self, fname): |
|
629 | 630 | self._checkknown(fname) |
|
630 | 631 | self.removed.add(fname) |
|
631 | 632 | |
|
632 | 633 | def exists(self, fname): |
|
633 | 634 | return fname in self.ctx |
|
634 | 635 | |
|
635 | 636 | def close(self): |
|
636 | 637 | return self.changed | self.removed |
|
637 | 638 | |
|
638 | 639 | # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1 |
|
639 | 640 | unidesc = re.compile('@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@') |
|
640 | 641 | contextdesc = re.compile('(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)') |
|
641 | 642 | eolmodes = ['strict', 'crlf', 'lf', 'auto'] |
|
642 | 643 | |
|
643 | 644 | class patchfile(object): |
|
644 | 645 | def __init__(self, ui, gp, backend, store, eolmode='strict'): |
|
645 | 646 | self.fname = gp.path |
|
646 | 647 | self.eolmode = eolmode |
|
647 | 648 | self.eol = None |
|
648 | 649 | self.backend = backend |
|
649 | 650 | self.ui = ui |
|
650 | 651 | self.lines = [] |
|
651 | 652 | self.exists = False |
|
652 | 653 | self.missing = True |
|
653 | 654 | self.mode = gp.mode |
|
654 | 655 | self.copysource = gp.oldpath |
|
655 | 656 | self.create = gp.op in ('ADD', 'COPY', 'RENAME') |
|
656 | 657 | self.remove = gp.op == 'DELETE' |
|
657 | 658 | if self.copysource is None: |
|
658 | 659 | data, mode = backend.getfile(self.fname) |
|
659 | 660 | else: |
|
660 | 661 | data, mode = store.getfile(self.copysource)[:2] |
|
661 | 662 | if data is not None: |
|
662 | 663 | self.exists = self.copysource is None or backend.exists(self.fname) |
|
663 | 664 | self.missing = False |
|
664 | 665 | if data: |
|
665 | 666 | self.lines = mdiff.splitnewlines(data) |
|
666 | 667 | if self.mode is None: |
|
667 | 668 | self.mode = mode |
|
668 | 669 | if self.lines: |
|
669 | 670 | # Normalize line endings |
|
670 | 671 | if self.lines[0].endswith('\r\n'): |
|
671 | 672 | self.eol = '\r\n' |
|
672 | 673 | elif self.lines[0].endswith('\n'): |
|
673 | 674 | self.eol = '\n' |
|
674 | 675 | if eolmode != 'strict': |
|
675 | 676 | nlines = [] |
|
676 | 677 | for l in self.lines: |
|
677 | 678 | if l.endswith('\r\n'): |
|
678 | 679 | l = l[:-2] + '\n' |
|
679 | 680 | nlines.append(l) |
|
680 | 681 | self.lines = nlines |
|
681 | 682 | else: |
|
682 | 683 | if self.create: |
|
683 | 684 | self.missing = False |
|
684 | 685 | if self.mode is None: |
|
685 | 686 | self.mode = (False, False) |
|
686 | 687 | if self.missing: |
|
687 | 688 | self.ui.warn(_("unable to find '%s' for patching\n") % self.fname) |
|
688 | 689 | self.ui.warn(_("(use '--prefix' to apply patch relative to the " |
|
689 | 690 | "current directory)\n")) |
|
690 | 691 | |
|
691 | 692 | self.hash = {} |
|
692 | 693 | self.dirty = 0 |
|
693 | 694 | self.offset = 0 |
|
694 | 695 | self.skew = 0 |
|
695 | 696 | self.rej = [] |
|
696 | 697 | self.fileprinted = False |
|
697 | 698 | self.printfile(False) |
|
698 | 699 | self.hunks = 0 |
|
699 | 700 | |
|
700 | 701 | def writelines(self, fname, lines, mode): |
|
701 | 702 | if self.eolmode == 'auto': |
|
702 | 703 | eol = self.eol |
|
703 | 704 | elif self.eolmode == 'crlf': |
|
704 | 705 | eol = '\r\n' |
|
705 | 706 | else: |
|
706 | 707 | eol = '\n' |
|
707 | 708 | |
|
708 | 709 | if self.eolmode != 'strict' and eol and eol != '\n': |
|
709 | 710 | rawlines = [] |
|
710 | 711 | for l in lines: |
|
711 | 712 | if l and l.endswith('\n'): |
|
712 | 713 | l = l[:-1] + eol |
|
713 | 714 | rawlines.append(l) |
|
714 | 715 | lines = rawlines |
|
715 | 716 | |
|
716 | 717 | self.backend.setfile(fname, ''.join(lines), mode, self.copysource) |
|
717 | 718 | |
|
718 | 719 | def printfile(self, warn): |
|
719 | 720 | if self.fileprinted: |
|
720 | 721 | return |
|
721 | 722 | if warn or self.ui.verbose: |
|
722 | 723 | self.fileprinted = True |
|
723 | 724 | s = _("patching file %s\n") % self.fname |
|
724 | 725 | if warn: |
|
725 | 726 | self.ui.warn(s) |
|
726 | 727 | else: |
|
727 | 728 | self.ui.note(s) |
|
728 | 729 | |
|
729 | 730 | |
|
730 | 731 | def findlines(self, l, linenum): |
|
731 | 732 | # looks through the hash and finds candidate lines. The |
|
732 | 733 | # result is a list of line numbers sorted based on distance |
|
733 | 734 | # from linenum |
|
734 | 735 | |
|
735 | 736 | cand = self.hash.get(l, []) |
|
736 | 737 | if len(cand) > 1: |
|
737 | 738 | # resort our list of potentials forward then back. |
|
738 | 739 | cand.sort(key=lambda x: abs(x - linenum)) |
|
739 | 740 | return cand |
|
740 | 741 | |
|
741 | 742 | def write_rej(self): |
|
742 | 743 | # our rejects are a little different from patch(1). This always |
|
743 | 744 | # creates rejects in the same form as the original patch. A file |
|
744 | 745 | # header is inserted so that you can run the reject through patch again |
|
745 | 746 | # without having to type the filename. |
|
746 | 747 | if not self.rej: |
|
747 | 748 | return |
|
748 | 749 | base = os.path.basename(self.fname) |
|
749 | 750 | lines = ["--- %s\n+++ %s\n" % (base, base)] |
|
750 | 751 | for x in self.rej: |
|
751 | 752 | for l in x.hunk: |
|
752 | 753 | lines.append(l) |
|
753 | 754 | if l[-1:] != '\n': |
|
754 | 755 | lines.append("\n\ No newline at end of file\n") |
|
755 | 756 | self.backend.writerej(self.fname, len(self.rej), self.hunks, lines) |
|
756 | 757 | |
|
757 | 758 | def apply(self, h): |
|
758 | 759 | if not h.complete(): |
|
759 | 760 | raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") % |
|
760 | 761 | (h.number, h.desc, len(h.a), h.lena, len(h.b), |
|
761 | 762 | h.lenb)) |
|
762 | 763 | |
|
763 | 764 | self.hunks += 1 |
|
764 | 765 | |
|
765 | 766 | if self.missing: |
|
766 | 767 | self.rej.append(h) |
|
767 | 768 | return -1 |
|
768 | 769 | |
|
769 | 770 | if self.exists and self.create: |
|
770 | 771 | if self.copysource: |
|
771 | 772 | self.ui.warn(_("cannot create %s: destination already " |
|
772 | 773 | "exists\n") % self.fname) |
|
773 | 774 | else: |
|
774 | 775 | self.ui.warn(_("file %s already exists\n") % self.fname) |
|
775 | 776 | self.rej.append(h) |
|
776 | 777 | return -1 |
|
777 | 778 | |
|
778 | 779 | if isinstance(h, binhunk): |
|
779 | 780 | if self.remove: |
|
780 | 781 | self.backend.unlink(self.fname) |
|
781 | 782 | else: |
|
782 | 783 | l = h.new(self.lines) |
|
783 | 784 | self.lines[:] = l |
|
784 | 785 | self.offset += len(l) |
|
785 | 786 | self.dirty = True |
|
786 | 787 | return 0 |
|
787 | 788 | |
|
788 | 789 | horig = h |
|
789 | 790 | if (self.eolmode in ('crlf', 'lf') |
|
790 | 791 | or self.eolmode == 'auto' and self.eol): |
|
791 | 792 | # If new eols are going to be normalized, then normalize |
|
792 | 793 | # hunk data before patching. Otherwise, preserve input |
|
793 | 794 | # line-endings. |
|
794 | 795 | h = h.getnormalized() |
|
795 | 796 | |
|
796 | 797 | # fast case first, no offsets, no fuzz |
|
797 | 798 | old, oldstart, new, newstart = h.fuzzit(0, False) |
|
798 | 799 | oldstart += self.offset |
|
799 | 800 | orig_start = oldstart |
|
800 | 801 | # if there's skew we want to emit the "(offset %d lines)" even |
|
801 | 802 | # when the hunk cleanly applies at start + skew, so skip the |
|
802 | 803 | # fast case code |
|
803 | 804 | if self.skew == 0 and diffhelper.testhunk(old, self.lines, oldstart): |
|
804 | 805 | if self.remove: |
|
805 | 806 | self.backend.unlink(self.fname) |
|
806 | 807 | else: |
|
807 | 808 | self.lines[oldstart:oldstart + len(old)] = new |
|
808 | 809 | self.offset += len(new) - len(old) |
|
809 | 810 | self.dirty = True |
|
810 | 811 | return 0 |
|
811 | 812 | |
|
812 | 813 | # ok, we couldn't match the hunk. Lets look for offsets and fuzz it |
|
813 | 814 | self.hash = {} |
|
814 | 815 | for x, s in enumerate(self.lines): |
|
815 | 816 | self.hash.setdefault(s, []).append(x) |
|
816 | 817 | |
|
817 | 818 | for fuzzlen in xrange(self.ui.configint("patch", "fuzz") + 1): |
|
818 | 819 | for toponly in [True, False]: |
|
819 | 820 | old, oldstart, new, newstart = h.fuzzit(fuzzlen, toponly) |
|
820 | 821 | oldstart = oldstart + self.offset + self.skew |
|
821 | 822 | oldstart = min(oldstart, len(self.lines)) |
|
822 | 823 | if old: |
|
823 | 824 | cand = self.findlines(old[0][1:], oldstart) |
|
824 | 825 | else: |
|
825 | 826 | # Only adding lines with no or fuzzed context, just |
|
826 | 827 | # take the skew in account |
|
827 | 828 | cand = [oldstart] |
|
828 | 829 | |
|
829 | 830 | for l in cand: |
|
830 | 831 | if not old or diffhelper.testhunk(old, self.lines, l): |
|
831 | 832 | self.lines[l : l + len(old)] = new |
|
832 | 833 | self.offset += len(new) - len(old) |
|
833 | 834 | self.skew = l - orig_start |
|
834 | 835 | self.dirty = True |
|
835 | 836 | offset = l - orig_start - fuzzlen |
|
836 | 837 | if fuzzlen: |
|
837 | 838 | msg = _("Hunk #%d succeeded at %d " |
|
838 | 839 | "with fuzz %d " |
|
839 | 840 | "(offset %d lines).\n") |
|
840 | 841 | self.printfile(True) |
|
841 | 842 | self.ui.warn(msg % |
|
842 | 843 | (h.number, l + 1, fuzzlen, offset)) |
|
843 | 844 | else: |
|
844 | 845 | msg = _("Hunk #%d succeeded at %d " |
|
845 | 846 | "(offset %d lines).\n") |
|
846 | 847 | self.ui.note(msg % (h.number, l + 1, offset)) |
|
847 | 848 | return fuzzlen |
|
848 | 849 | self.printfile(True) |
|
849 | 850 | self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start)) |
|
850 | 851 | self.rej.append(horig) |
|
851 | 852 | return -1 |
|
852 | 853 | |
|
853 | 854 | def close(self): |
|
854 | 855 | if self.dirty: |
|
855 | 856 | self.writelines(self.fname, self.lines, self.mode) |
|
856 | 857 | self.write_rej() |
|
857 | 858 | return len(self.rej) |
|
858 | 859 | |
|
859 | 860 | class header(object): |
|
860 | 861 | """patch header |
|
861 | 862 | """ |
|
862 | 863 | diffgit_re = re.compile('diff --git a/(.*) b/(.*)$') |
|
863 | 864 | diff_re = re.compile('diff -r .* (.*)$') |
|
864 | 865 | allhunks_re = re.compile('(?:index|deleted file) ') |
|
865 | 866 | pretty_re = re.compile('(?:new file|deleted file) ') |
|
866 | 867 | special_re = re.compile('(?:index|deleted|copy|rename) ') |
|
867 | 868 | newfile_re = re.compile('(?:new file)') |
|
868 | 869 | |
|
869 | 870 | def __init__(self, header): |
|
870 | 871 | self.header = header |
|
871 | 872 | self.hunks = [] |
|
872 | 873 | |
|
873 | 874 | def binary(self): |
|
874 | 875 | return any(h.startswith('index ') for h in self.header) |
|
875 | 876 | |
|
876 | 877 | def pretty(self, fp): |
|
877 | 878 | for h in self.header: |
|
878 | 879 | if h.startswith('index '): |
|
879 | 880 | fp.write(_('this modifies a binary file (all or nothing)\n')) |
|
880 | 881 | break |
|
881 | 882 | if self.pretty_re.match(h): |
|
882 | 883 | fp.write(h) |
|
883 | 884 | if self.binary(): |
|
884 | 885 | fp.write(_('this is a binary file\n')) |
|
885 | 886 | break |
|
886 | 887 | if h.startswith('---'): |
|
887 | 888 | fp.write(_('%d hunks, %d lines changed\n') % |
|
888 | 889 | (len(self.hunks), |
|
889 | 890 | sum([max(h.added, h.removed) for h in self.hunks]))) |
|
890 | 891 | break |
|
891 | 892 | fp.write(h) |
|
892 | 893 | |
|
893 | 894 | def write(self, fp): |
|
894 | 895 | fp.write(''.join(self.header)) |
|
895 | 896 | |
|
896 | 897 | def allhunks(self): |
|
897 | 898 | return any(self.allhunks_re.match(h) for h in self.header) |
|
898 | 899 | |
|
899 | 900 | def files(self): |
|
900 | 901 | match = self.diffgit_re.match(self.header[0]) |
|
901 | 902 | if match: |
|
902 | 903 | fromfile, tofile = match.groups() |
|
903 | 904 | if fromfile == tofile: |
|
904 | 905 | return [fromfile] |
|
905 | 906 | return [fromfile, tofile] |
|
906 | 907 | else: |
|
907 | 908 | return self.diff_re.match(self.header[0]).groups() |
|
908 | 909 | |
|
909 | 910 | def filename(self): |
|
910 | 911 | return self.files()[-1] |
|
911 | 912 | |
|
912 | 913 | def __repr__(self): |
|
913 | 914 | return '<header %s>' % (' '.join(map(repr, self.files()))) |
|
914 | 915 | |
|
915 | 916 | def isnewfile(self): |
|
916 | 917 | return any(self.newfile_re.match(h) for h in self.header) |
|
917 | 918 | |
|
918 | 919 | def special(self): |
|
919 | 920 | # Special files are shown only at the header level and not at the hunk |
|
920 | 921 | # level for example a file that has been deleted is a special file. |
|
921 | 922 | # The user cannot change the content of the operation, in the case of |
|
922 | 923 | # the deleted file he has to take the deletion or not take it, he |
|
923 | 924 | # cannot take some of it. |
|
924 | 925 | # Newly added files are special if they are empty, they are not special |
|
925 | 926 | # if they have some content as we want to be able to change it |
|
926 | 927 | nocontent = len(self.header) == 2 |
|
927 | 928 | emptynewfile = self.isnewfile() and nocontent |
|
928 | 929 | return emptynewfile or \ |
|
929 | 930 | any(self.special_re.match(h) for h in self.header) |
|
930 | 931 | |
|
931 | 932 | class recordhunk(object): |
|
932 | 933 | """patch hunk |
|
933 | 934 | |
|
934 | 935 | XXX shouldn't we merge this with the other hunk class? |
|
935 | 936 | """ |
|
936 | 937 | |
|
937 | 938 | def __init__(self, header, fromline, toline, proc, before, hunk, after, |
|
938 | 939 | maxcontext=None): |
|
939 | 940 | def trimcontext(lines, reverse=False): |
|
940 | 941 | if maxcontext is not None: |
|
941 | 942 | delta = len(lines) - maxcontext |
|
942 | 943 | if delta > 0: |
|
943 | 944 | if reverse: |
|
944 | 945 | return delta, lines[delta:] |
|
945 | 946 | else: |
|
946 | 947 | return delta, lines[:maxcontext] |
|
947 | 948 | return 0, lines |
|
948 | 949 | |
|
949 | 950 | self.header = header |
|
950 | 951 | trimedbefore, self.before = trimcontext(before, True) |
|
951 | 952 | self.fromline = fromline + trimedbefore |
|
952 | 953 | self.toline = toline + trimedbefore |
|
953 | 954 | _trimedafter, self.after = trimcontext(after, False) |
|
954 | 955 | self.proc = proc |
|
955 | 956 | self.hunk = hunk |
|
956 | 957 | self.added, self.removed = self.countchanges(self.hunk) |
|
957 | 958 | |
|
958 | 959 | def __eq__(self, v): |
|
959 | 960 | if not isinstance(v, recordhunk): |
|
960 | 961 | return False |
|
961 | 962 | |
|
962 | 963 | return ((v.hunk == self.hunk) and |
|
963 | 964 | (v.proc == self.proc) and |
|
964 | 965 | (self.fromline == v.fromline) and |
|
965 | 966 | (self.header.files() == v.header.files())) |
|
966 | 967 | |
|
967 | 968 | def __hash__(self): |
|
968 | 969 | return hash((tuple(self.hunk), |
|
969 | 970 | tuple(self.header.files()), |
|
970 | 971 | self.fromline, |
|
971 | 972 | self.proc)) |
|
972 | 973 | |
|
973 | 974 | def countchanges(self, hunk): |
|
974 | 975 | """hunk -> (n+,n-)""" |
|
975 | 976 | add = len([h for h in hunk if h.startswith('+')]) |
|
976 | 977 | rem = len([h for h in hunk if h.startswith('-')]) |
|
977 | 978 | return add, rem |
|
978 | 979 | |
|
979 | 980 | def reversehunk(self): |
|
980 | 981 | """return another recordhunk which is the reverse of the hunk |
|
981 | 982 | |
|
982 | 983 | If this hunk is diff(A, B), the returned hunk is diff(B, A). To do |
|
983 | 984 | that, swap fromline/toline and +/- signs while keep other things |
|
984 | 985 | unchanged. |
|
985 | 986 | """ |
|
986 | 987 | m = {'+': '-', '-': '+', '\\': '\\'} |
|
987 | 988 | hunk = ['%s%s' % (m[l[0:1]], l[1:]) for l in self.hunk] |
|
988 | 989 | return recordhunk(self.header, self.toline, self.fromline, self.proc, |
|
989 | 990 | self.before, hunk, self.after) |
|
990 | 991 | |
|
991 | 992 | def write(self, fp): |
|
992 | 993 | delta = len(self.before) + len(self.after) |
|
993 | 994 | if self.after and self.after[-1] == '\\ No newline at end of file\n': |
|
994 | 995 | delta -= 1 |
|
995 | 996 | fromlen = delta + self.removed |
|
996 | 997 | tolen = delta + self.added |
|
997 | 998 | fp.write('@@ -%d,%d +%d,%d @@%s\n' % |
|
998 | 999 | (self.fromline, fromlen, self.toline, tolen, |
|
999 | 1000 | self.proc and (' ' + self.proc))) |
|
1000 | 1001 | fp.write(''.join(self.before + self.hunk + self.after)) |
|
1001 | 1002 | |
|
1002 | 1003 | pretty = write |
|
1003 | 1004 | |
|
1004 | 1005 | def filename(self): |
|
1005 | 1006 | return self.header.filename() |
|
1006 | 1007 | |
|
1007 | 1008 | def __repr__(self): |
|
1008 | 1009 | return '<hunk %r@%d>' % (self.filename(), self.fromline) |
|
1009 | 1010 | |
|
1010 | 1011 | def getmessages(): |
|
1011 | 1012 | return { |
|
1012 | 1013 | 'multiple': { |
|
1013 | 1014 | 'apply': _("apply change %d/%d to '%s'?"), |
|
1014 | 1015 | 'discard': _("discard change %d/%d to '%s'?"), |
|
1015 | 1016 | 'record': _("record change %d/%d to '%s'?"), |
|
1016 | 1017 | }, |
|
1017 | 1018 | 'single': { |
|
1018 | 1019 | 'apply': _("apply this change to '%s'?"), |
|
1019 | 1020 | 'discard': _("discard this change to '%s'?"), |
|
1020 | 1021 | 'record': _("record this change to '%s'?"), |
|
1021 | 1022 | }, |
|
1022 | 1023 | 'help': { |
|
1023 | 1024 | 'apply': _('[Ynesfdaq?]' |
|
1024 | 1025 | '$$ &Yes, apply this change' |
|
1025 | 1026 | '$$ &No, skip this change' |
|
1026 | 1027 | '$$ &Edit this change manually' |
|
1027 | 1028 | '$$ &Skip remaining changes to this file' |
|
1028 | 1029 | '$$ Apply remaining changes to this &file' |
|
1029 | 1030 | '$$ &Done, skip remaining changes and files' |
|
1030 | 1031 | '$$ Apply &all changes to all remaining files' |
|
1031 | 1032 | '$$ &Quit, applying no changes' |
|
1032 | 1033 | '$$ &? (display help)'), |
|
1033 | 1034 | 'discard': _('[Ynesfdaq?]' |
|
1034 | 1035 | '$$ &Yes, discard this change' |
|
1035 | 1036 | '$$ &No, skip this change' |
|
1036 | 1037 | '$$ &Edit this change manually' |
|
1037 | 1038 | '$$ &Skip remaining changes to this file' |
|
1038 | 1039 | '$$ Discard remaining changes to this &file' |
|
1039 | 1040 | '$$ &Done, skip remaining changes and files' |
|
1040 | 1041 | '$$ Discard &all changes to all remaining files' |
|
1041 | 1042 | '$$ &Quit, discarding no changes' |
|
1042 | 1043 | '$$ &? (display help)'), |
|
1043 | 1044 | 'record': _('[Ynesfdaq?]' |
|
1044 | 1045 | '$$ &Yes, record this change' |
|
1045 | 1046 | '$$ &No, skip this change' |
|
1046 | 1047 | '$$ &Edit this change manually' |
|
1047 | 1048 | '$$ &Skip remaining changes to this file' |
|
1048 | 1049 | '$$ Record remaining changes to this &file' |
|
1049 | 1050 | '$$ &Done, skip remaining changes and files' |
|
1050 | 1051 | '$$ Record &all changes to all remaining files' |
|
1051 | 1052 | '$$ &Quit, recording no changes' |
|
1052 | 1053 | '$$ &? (display help)'), |
|
1053 | 1054 | } |
|
1054 | 1055 | } |
|
1055 | 1056 | |
|
1056 | 1057 | def filterpatch(ui, headers, operation=None): |
|
1057 | 1058 | """Interactively filter patch chunks into applied-only chunks""" |
|
1058 | 1059 | messages = getmessages() |
|
1059 | 1060 | |
|
1060 | 1061 | if operation is None: |
|
1061 | 1062 | operation = 'record' |
|
1062 | 1063 | |
|
1063 | 1064 | def prompt(skipfile, skipall, query, chunk): |
|
1064 | 1065 | """prompt query, and process base inputs |
|
1065 | 1066 | |
|
1066 | 1067 | - y/n for the rest of file |
|
1067 | 1068 | - y/n for the rest |
|
1068 | 1069 | - ? (help) |
|
1069 | 1070 | - q (quit) |
|
1070 | 1071 | |
|
1071 | 1072 | Return True/False and possibly updated skipfile and skipall. |
|
1072 | 1073 | """ |
|
1073 | 1074 | newpatches = None |
|
1074 | 1075 | if skipall is not None: |
|
1075 | 1076 | return skipall, skipfile, skipall, newpatches |
|
1076 | 1077 | if skipfile is not None: |
|
1077 | 1078 | return skipfile, skipfile, skipall, newpatches |
|
1078 | 1079 | while True: |
|
1079 | 1080 | resps = messages['help'][operation] |
|
1080 | 1081 | r = ui.promptchoice("%s %s" % (query, resps)) |
|
1081 | 1082 | ui.write("\n") |
|
1082 | 1083 | if r == 8: # ? |
|
1083 | 1084 | for c, t in ui.extractchoices(resps)[1]: |
|
1084 | 1085 | ui.write('%s - %s\n' % (c, encoding.lower(t))) |
|
1085 | 1086 | continue |
|
1086 | 1087 | elif r == 0: # yes |
|
1087 | 1088 | ret = True |
|
1088 | 1089 | elif r == 1: # no |
|
1089 | 1090 | ret = False |
|
1090 | 1091 | elif r == 2: # Edit patch |
|
1091 | 1092 | if chunk is None: |
|
1092 | 1093 | ui.write(_('cannot edit patch for whole file')) |
|
1093 | 1094 | ui.write("\n") |
|
1094 | 1095 | continue |
|
1095 | 1096 | if chunk.header.binary(): |
|
1096 | 1097 | ui.write(_('cannot edit patch for binary file')) |
|
1097 | 1098 | ui.write("\n") |
|
1098 | 1099 | continue |
|
1099 | 1100 | # Patch comment based on the Git one (based on comment at end of |
|
1100 | 1101 | # https://mercurial-scm.org/wiki/RecordExtension) |
|
1101 | 1102 | phelp = '---' + _(""" |
|
1102 | 1103 | To remove '-' lines, make them ' ' lines (context). |
|
1103 | 1104 | To remove '+' lines, delete them. |
|
1104 | 1105 | Lines starting with # will be removed from the patch. |
|
1105 | 1106 | |
|
1106 | 1107 | If the patch applies cleanly, the edited hunk will immediately be |
|
1107 | 1108 | added to the record list. If it does not apply cleanly, a rejects |
|
1108 | 1109 | file will be generated: you can use that when you try again. If |
|
1109 | 1110 | all lines of the hunk are removed, then the edit is aborted and |
|
1110 | 1111 | the hunk is left unchanged. |
|
1111 | 1112 | """) |
|
1112 | 1113 | (patchfd, patchfn) = pycompat.mkstemp(prefix="hg-editor-", |
|
1113 | 1114 | suffix=".diff") |
|
1114 | 1115 | ncpatchfp = None |
|
1115 | 1116 | try: |
|
1116 | 1117 | # Write the initial patch |
|
1117 | 1118 | f = util.nativeeolwriter(os.fdopen(patchfd, r'wb')) |
|
1118 | 1119 | chunk.header.write(f) |
|
1119 | 1120 | chunk.write(f) |
|
1120 | 1121 | f.write('\n'.join(['# ' + i for i in phelp.splitlines()])) |
|
1121 | 1122 | f.close() |
|
1122 | 1123 | # Start the editor and wait for it to complete |
|
1123 | 1124 | editor = ui.geteditor() |
|
1124 | 1125 | ret = ui.system("%s \"%s\"" % (editor, patchfn), |
|
1125 | 1126 | environ={'HGUSER': ui.username()}, |
|
1126 | 1127 | blockedtag='filterpatch') |
|
1127 | 1128 | if ret != 0: |
|
1128 | 1129 | ui.warn(_("editor exited with exit code %d\n") % ret) |
|
1129 | 1130 | continue |
|
1130 | 1131 | # Remove comment lines |
|
1131 | 1132 | patchfp = open(patchfn, r'rb') |
|
1132 | 1133 | ncpatchfp = stringio() |
|
1133 | 1134 | for line in util.iterfile(patchfp): |
|
1134 | 1135 | line = util.fromnativeeol(line) |
|
1135 | 1136 | if not line.startswith('#'): |
|
1136 | 1137 | ncpatchfp.write(line) |
|
1137 | 1138 | patchfp.close() |
|
1138 | 1139 | ncpatchfp.seek(0) |
|
1139 | 1140 | newpatches = parsepatch(ncpatchfp) |
|
1140 | 1141 | finally: |
|
1141 | 1142 | os.unlink(patchfn) |
|
1142 | 1143 | del ncpatchfp |
|
1143 | 1144 | # Signal that the chunk shouldn't be applied as-is, but |
|
1144 | 1145 | # provide the new patch to be used instead. |
|
1145 | 1146 | ret = False |
|
1146 | 1147 | elif r == 3: # Skip |
|
1147 | 1148 | ret = skipfile = False |
|
1148 | 1149 | elif r == 4: # file (Record remaining) |
|
1149 | 1150 | ret = skipfile = True |
|
1150 | 1151 | elif r == 5: # done, skip remaining |
|
1151 | 1152 | ret = skipall = False |
|
1152 | 1153 | elif r == 6: # all |
|
1153 | 1154 | ret = skipall = True |
|
1154 | 1155 | elif r == 7: # quit |
|
1155 | 1156 | raise error.Abort(_('user quit')) |
|
1156 | 1157 | return ret, skipfile, skipall, newpatches |
|
1157 | 1158 | |
|
1158 | 1159 | seen = set() |
|
1159 | 1160 | applied = {} # 'filename' -> [] of chunks |
|
1160 | 1161 | skipfile, skipall = None, None |
|
1161 | 1162 | pos, total = 1, sum(len(h.hunks) for h in headers) |
|
1162 | 1163 | for h in headers: |
|
1163 | 1164 | pos += len(h.hunks) |
|
1164 | 1165 | skipfile = None |
|
1165 | 1166 | fixoffset = 0 |
|
1166 | 1167 | hdr = ''.join(h.header) |
|
1167 | 1168 | if hdr in seen: |
|
1168 | 1169 | continue |
|
1169 | 1170 | seen.add(hdr) |
|
1170 | 1171 | if skipall is None: |
|
1171 | 1172 | h.pretty(ui) |
|
1172 | 1173 | msg = (_('examine changes to %s?') % |
|
1173 | 1174 | _(' and ').join("'%s'" % f for f in h.files())) |
|
1174 | 1175 | r, skipfile, skipall, np = prompt(skipfile, skipall, msg, None) |
|
1175 | 1176 | if not r: |
|
1176 | 1177 | continue |
|
1177 | 1178 | applied[h.filename()] = [h] |
|
1178 | 1179 | if h.allhunks(): |
|
1179 | 1180 | applied[h.filename()] += h.hunks |
|
1180 | 1181 | continue |
|
1181 | 1182 | for i, chunk in enumerate(h.hunks): |
|
1182 | 1183 | if skipfile is None and skipall is None: |
|
1183 | 1184 | chunk.pretty(ui) |
|
1184 | 1185 | if total == 1: |
|
1185 | 1186 | msg = messages['single'][operation] % chunk.filename() |
|
1186 | 1187 | else: |
|
1187 | 1188 | idx = pos - len(h.hunks) + i |
|
1188 | 1189 | msg = messages['multiple'][operation] % (idx, total, |
|
1189 | 1190 | chunk.filename()) |
|
1190 | 1191 | r, skipfile, skipall, newpatches = prompt(skipfile, |
|
1191 | 1192 | skipall, msg, chunk) |
|
1192 | 1193 | if r: |
|
1193 | 1194 | if fixoffset: |
|
1194 | 1195 | chunk = copy.copy(chunk) |
|
1195 | 1196 | chunk.toline += fixoffset |
|
1196 | 1197 | applied[chunk.filename()].append(chunk) |
|
1197 | 1198 | elif newpatches is not None: |
|
1198 | 1199 | for newpatch in newpatches: |
|
1199 | 1200 | for newhunk in newpatch.hunks: |
|
1200 | 1201 | if fixoffset: |
|
1201 | 1202 | newhunk.toline += fixoffset |
|
1202 | 1203 | applied[newhunk.filename()].append(newhunk) |
|
1203 | 1204 | else: |
|
1204 | 1205 | fixoffset += chunk.removed - chunk.added |
|
1205 | 1206 | return (sum([h for h in applied.itervalues() |
|
1206 | 1207 | if h[0].special() or len(h) > 1], []), {}) |
|
1207 | 1208 | class hunk(object): |
|
1208 | 1209 | def __init__(self, desc, num, lr, context): |
|
1209 | 1210 | self.number = num |
|
1210 | 1211 | self.desc = desc |
|
1211 | 1212 | self.hunk = [desc] |
|
1212 | 1213 | self.a = [] |
|
1213 | 1214 | self.b = [] |
|
1214 | 1215 | self.starta = self.lena = None |
|
1215 | 1216 | self.startb = self.lenb = None |
|
1216 | 1217 | if lr is not None: |
|
1217 | 1218 | if context: |
|
1218 | 1219 | self.read_context_hunk(lr) |
|
1219 | 1220 | else: |
|
1220 | 1221 | self.read_unified_hunk(lr) |
|
1221 | 1222 | |
|
1222 | 1223 | def getnormalized(self): |
|
1223 | 1224 | """Return a copy with line endings normalized to LF.""" |
|
1224 | 1225 | |
|
1225 | 1226 | def normalize(lines): |
|
1226 | 1227 | nlines = [] |
|
1227 | 1228 | for line in lines: |
|
1228 | 1229 | if line.endswith('\r\n'): |
|
1229 | 1230 | line = line[:-2] + '\n' |
|
1230 | 1231 | nlines.append(line) |
|
1231 | 1232 | return nlines |
|
1232 | 1233 | |
|
1233 | 1234 | # Dummy object, it is rebuilt manually |
|
1234 | 1235 | nh = hunk(self.desc, self.number, None, None) |
|
1235 | 1236 | nh.number = self.number |
|
1236 | 1237 | nh.desc = self.desc |
|
1237 | 1238 | nh.hunk = self.hunk |
|
1238 | 1239 | nh.a = normalize(self.a) |
|
1239 | 1240 | nh.b = normalize(self.b) |
|
1240 | 1241 | nh.starta = self.starta |
|
1241 | 1242 | nh.startb = self.startb |
|
1242 | 1243 | nh.lena = self.lena |
|
1243 | 1244 | nh.lenb = self.lenb |
|
1244 | 1245 | return nh |
|
1245 | 1246 | |
|
1246 | 1247 | def read_unified_hunk(self, lr): |
|
1247 | 1248 | m = unidesc.match(self.desc) |
|
1248 | 1249 | if not m: |
|
1249 | 1250 | raise PatchError(_("bad hunk #%d") % self.number) |
|
1250 | 1251 | self.starta, self.lena, self.startb, self.lenb = m.groups() |
|
1251 | 1252 | if self.lena is None: |
|
1252 | 1253 | self.lena = 1 |
|
1253 | 1254 | else: |
|
1254 | 1255 | self.lena = int(self.lena) |
|
1255 | 1256 | if self.lenb is None: |
|
1256 | 1257 | self.lenb = 1 |
|
1257 | 1258 | else: |
|
1258 | 1259 | self.lenb = int(self.lenb) |
|
1259 | 1260 | self.starta = int(self.starta) |
|
1260 | 1261 | self.startb = int(self.startb) |
|
1261 | 1262 | try: |
|
1262 | 1263 | diffhelper.addlines(lr, self.hunk, self.lena, self.lenb, |
|
1263 | 1264 | self.a, self.b) |
|
1264 | 1265 | except error.ParseError as e: |
|
1265 | 1266 | raise PatchError(_("bad hunk #%d: %s") % (self.number, e)) |
|
1266 | 1267 | # if we hit eof before finishing out the hunk, the last line will |
|
1267 | 1268 | # be zero length. Lets try to fix it up. |
|
1268 | 1269 | while len(self.hunk[-1]) == 0: |
|
1269 | 1270 | del self.hunk[-1] |
|
1270 | 1271 | del self.a[-1] |
|
1271 | 1272 | del self.b[-1] |
|
1272 | 1273 | self.lena -= 1 |
|
1273 | 1274 | self.lenb -= 1 |
|
1274 | 1275 | self._fixnewline(lr) |
|
1275 | 1276 | |
|
1276 | 1277 | def read_context_hunk(self, lr): |
|
1277 | 1278 | self.desc = lr.readline() |
|
1278 | 1279 | m = contextdesc.match(self.desc) |
|
1279 | 1280 | if not m: |
|
1280 | 1281 | raise PatchError(_("bad hunk #%d") % self.number) |
|
1281 | 1282 | self.starta, aend = m.groups() |
|
1282 | 1283 | self.starta = int(self.starta) |
|
1283 | 1284 | if aend is None: |
|
1284 | 1285 | aend = self.starta |
|
1285 | 1286 | self.lena = int(aend) - self.starta |
|
1286 | 1287 | if self.starta: |
|
1287 | 1288 | self.lena += 1 |
|
1288 | 1289 | for x in xrange(self.lena): |
|
1289 | 1290 | l = lr.readline() |
|
1290 | 1291 | if l.startswith('---'): |
|
1291 | 1292 | # lines addition, old block is empty |
|
1292 | 1293 | lr.push(l) |
|
1293 | 1294 | break |
|
1294 | 1295 | s = l[2:] |
|
1295 | 1296 | if l.startswith('- ') or l.startswith('! '): |
|
1296 | 1297 | u = '-' + s |
|
1297 | 1298 | elif l.startswith(' '): |
|
1298 | 1299 | u = ' ' + s |
|
1299 | 1300 | else: |
|
1300 | 1301 | raise PatchError(_("bad hunk #%d old text line %d") % |
|
1301 | 1302 | (self.number, x)) |
|
1302 | 1303 | self.a.append(u) |
|
1303 | 1304 | self.hunk.append(u) |
|
1304 | 1305 | |
|
1305 | 1306 | l = lr.readline() |
|
1306 | 1307 | if l.startswith('\ '): |
|
1307 | 1308 | s = self.a[-1][:-1] |
|
1308 | 1309 | self.a[-1] = s |
|
1309 | 1310 | self.hunk[-1] = s |
|
1310 | 1311 | l = lr.readline() |
|
1311 | 1312 | m = contextdesc.match(l) |
|
1312 | 1313 | if not m: |
|
1313 | 1314 | raise PatchError(_("bad hunk #%d") % self.number) |
|
1314 | 1315 | self.startb, bend = m.groups() |
|
1315 | 1316 | self.startb = int(self.startb) |
|
1316 | 1317 | if bend is None: |
|
1317 | 1318 | bend = self.startb |
|
1318 | 1319 | self.lenb = int(bend) - self.startb |
|
1319 | 1320 | if self.startb: |
|
1320 | 1321 | self.lenb += 1 |
|
1321 | 1322 | hunki = 1 |
|
1322 | 1323 | for x in xrange(self.lenb): |
|
1323 | 1324 | l = lr.readline() |
|
1324 | 1325 | if l.startswith('\ '): |
|
1325 | 1326 | # XXX: the only way to hit this is with an invalid line range. |
|
1326 | 1327 | # The no-eol marker is not counted in the line range, but I |
|
1327 | 1328 | # guess there are diff(1) out there which behave differently. |
|
1328 | 1329 | s = self.b[-1][:-1] |
|
1329 | 1330 | self.b[-1] = s |
|
1330 | 1331 | self.hunk[hunki - 1] = s |
|
1331 | 1332 | continue |
|
1332 | 1333 | if not l: |
|
1333 | 1334 | # line deletions, new block is empty and we hit EOF |
|
1334 | 1335 | lr.push(l) |
|
1335 | 1336 | break |
|
1336 | 1337 | s = l[2:] |
|
1337 | 1338 | if l.startswith('+ ') or l.startswith('! '): |
|
1338 | 1339 | u = '+' + s |
|
1339 | 1340 | elif l.startswith(' '): |
|
1340 | 1341 | u = ' ' + s |
|
1341 | 1342 | elif len(self.b) == 0: |
|
1342 | 1343 | # line deletions, new block is empty |
|
1343 | 1344 | lr.push(l) |
|
1344 | 1345 | break |
|
1345 | 1346 | else: |
|
1346 | 1347 | raise PatchError(_("bad hunk #%d old text line %d") % |
|
1347 | 1348 | (self.number, x)) |
|
1348 | 1349 | self.b.append(s) |
|
1349 | 1350 | while True: |
|
1350 | 1351 | if hunki >= len(self.hunk): |
|
1351 | 1352 | h = "" |
|
1352 | 1353 | else: |
|
1353 | 1354 | h = self.hunk[hunki] |
|
1354 | 1355 | hunki += 1 |
|
1355 | 1356 | if h == u: |
|
1356 | 1357 | break |
|
1357 | 1358 | elif h.startswith('-'): |
|
1358 | 1359 | continue |
|
1359 | 1360 | else: |
|
1360 | 1361 | self.hunk.insert(hunki - 1, u) |
|
1361 | 1362 | break |
|
1362 | 1363 | |
|
1363 | 1364 | if not self.a: |
|
1364 | 1365 | # this happens when lines were only added to the hunk |
|
1365 | 1366 | for x in self.hunk: |
|
1366 | 1367 | if x.startswith('-') or x.startswith(' '): |
|
1367 | 1368 | self.a.append(x) |
|
1368 | 1369 | if not self.b: |
|
1369 | 1370 | # this happens when lines were only deleted from the hunk |
|
1370 | 1371 | for x in self.hunk: |
|
1371 | 1372 | if x.startswith('+') or x.startswith(' '): |
|
1372 | 1373 | self.b.append(x[1:]) |
|
1373 | 1374 | # @@ -start,len +start,len @@ |
|
1374 | 1375 | self.desc = "@@ -%d,%d +%d,%d @@\n" % (self.starta, self.lena, |
|
1375 | 1376 | self.startb, self.lenb) |
|
1376 | 1377 | self.hunk[0] = self.desc |
|
1377 | 1378 | self._fixnewline(lr) |
|
1378 | 1379 | |
|
1379 | 1380 | def _fixnewline(self, lr): |
|
1380 | 1381 | l = lr.readline() |
|
1381 | 1382 | if l.startswith('\ '): |
|
1382 | 1383 | diffhelper.fixnewline(self.hunk, self.a, self.b) |
|
1383 | 1384 | else: |
|
1384 | 1385 | lr.push(l) |
|
1385 | 1386 | |
|
1386 | 1387 | def complete(self): |
|
1387 | 1388 | return len(self.a) == self.lena and len(self.b) == self.lenb |
|
1388 | 1389 | |
|
1389 | 1390 | def _fuzzit(self, old, new, fuzz, toponly): |
|
1390 | 1391 | # this removes context lines from the top and bottom of list 'l'. It |
|
1391 | 1392 | # checks the hunk to make sure only context lines are removed, and then |
|
1392 | 1393 | # returns a new shortened list of lines. |
|
1393 | 1394 | fuzz = min(fuzz, len(old)) |
|
1394 | 1395 | if fuzz: |
|
1395 | 1396 | top = 0 |
|
1396 | 1397 | bot = 0 |
|
1397 | 1398 | hlen = len(self.hunk) |
|
1398 | 1399 | for x in xrange(hlen - 1): |
|
1399 | 1400 | # the hunk starts with the @@ line, so use x+1 |
|
1400 | 1401 | if self.hunk[x + 1].startswith(' '): |
|
1401 | 1402 | top += 1 |
|
1402 | 1403 | else: |
|
1403 | 1404 | break |
|
1404 | 1405 | if not toponly: |
|
1405 | 1406 | for x in xrange(hlen - 1): |
|
1406 | 1407 | if self.hunk[hlen - bot - 1].startswith(' '): |
|
1407 | 1408 | bot += 1 |
|
1408 | 1409 | else: |
|
1409 | 1410 | break |
|
1410 | 1411 | |
|
1411 | 1412 | bot = min(fuzz, bot) |
|
1412 | 1413 | top = min(fuzz, top) |
|
1413 | 1414 | return old[top:len(old) - bot], new[top:len(new) - bot], top |
|
1414 | 1415 | return old, new, 0 |
|
1415 | 1416 | |
|
1416 | 1417 | def fuzzit(self, fuzz, toponly): |
|
1417 | 1418 | old, new, top = self._fuzzit(self.a, self.b, fuzz, toponly) |
|
1418 | 1419 | oldstart = self.starta + top |
|
1419 | 1420 | newstart = self.startb + top |
|
1420 | 1421 | # zero length hunk ranges already have their start decremented |
|
1421 | 1422 | if self.lena and oldstart > 0: |
|
1422 | 1423 | oldstart -= 1 |
|
1423 | 1424 | if self.lenb and newstart > 0: |
|
1424 | 1425 | newstart -= 1 |
|
1425 | 1426 | return old, oldstart, new, newstart |
|
1426 | 1427 | |
|
1427 | 1428 | class binhunk(object): |
|
1428 | 1429 | 'A binary patch file.' |
|
1429 | 1430 | def __init__(self, lr, fname): |
|
1430 | 1431 | self.text = None |
|
1431 | 1432 | self.delta = False |
|
1432 | 1433 | self.hunk = ['GIT binary patch\n'] |
|
1433 | 1434 | self._fname = fname |
|
1434 | 1435 | self._read(lr) |
|
1435 | 1436 | |
|
1436 | 1437 | def complete(self): |
|
1437 | 1438 | return self.text is not None |
|
1438 | 1439 | |
|
1439 | 1440 | def new(self, lines): |
|
1440 | 1441 | if self.delta: |
|
1441 | 1442 | return [applybindelta(self.text, ''.join(lines))] |
|
1442 | 1443 | return [self.text] |
|
1443 | 1444 | |
|
1444 | 1445 | def _read(self, lr): |
|
1445 | 1446 | def getline(lr, hunk): |
|
1446 | 1447 | l = lr.readline() |
|
1447 | 1448 | hunk.append(l) |
|
1448 | 1449 | return l.rstrip('\r\n') |
|
1449 | 1450 | |
|
1450 | 1451 | size = 0 |
|
1451 | 1452 | while True: |
|
1452 | 1453 | line = getline(lr, self.hunk) |
|
1453 | 1454 | if not line: |
|
1454 | 1455 | raise PatchError(_('could not extract "%s" binary data') |
|
1455 | 1456 | % self._fname) |
|
1456 | 1457 | if line.startswith('literal '): |
|
1457 | 1458 | size = int(line[8:].rstrip()) |
|
1458 | 1459 | break |
|
1459 | 1460 | if line.startswith('delta '): |
|
1460 | 1461 | size = int(line[6:].rstrip()) |
|
1461 | 1462 | self.delta = True |
|
1462 | 1463 | break |
|
1463 | 1464 | dec = [] |
|
1464 | 1465 | line = getline(lr, self.hunk) |
|
1465 | 1466 | while len(line) > 1: |
|
1466 | 1467 | l = line[0:1] |
|
1467 | 1468 | if l <= 'Z' and l >= 'A': |
|
1468 | 1469 | l = ord(l) - ord('A') + 1 |
|
1469 | 1470 | else: |
|
1470 | 1471 | l = ord(l) - ord('a') + 27 |
|
1471 | 1472 | try: |
|
1472 | 1473 | dec.append(util.b85decode(line[1:])[:l]) |
|
1473 | 1474 | except ValueError as e: |
|
1474 | 1475 | raise PatchError(_('could not decode "%s" binary patch: %s') |
|
1475 | 1476 | % (self._fname, stringutil.forcebytestr(e))) |
|
1476 | 1477 | line = getline(lr, self.hunk) |
|
1477 | 1478 | text = zlib.decompress(''.join(dec)) |
|
1478 | 1479 | if len(text) != size: |
|
1479 | 1480 | raise PatchError(_('"%s" length is %d bytes, should be %d') |
|
1480 | 1481 | % (self._fname, len(text), size)) |
|
1481 | 1482 | self.text = text |
|
1482 | 1483 | |
|
1483 | 1484 | def parsefilename(str): |
|
1484 | 1485 | # --- filename \t|space stuff |
|
1485 | 1486 | s = str[4:].rstrip('\r\n') |
|
1486 | 1487 | i = s.find('\t') |
|
1487 | 1488 | if i < 0: |
|
1488 | 1489 | i = s.find(' ') |
|
1489 | 1490 | if i < 0: |
|
1490 | 1491 | return s |
|
1491 | 1492 | return s[:i] |
|
1492 | 1493 | |
|
1493 | 1494 | def reversehunks(hunks): |
|
1494 | 1495 | '''reverse the signs in the hunks given as argument |
|
1495 | 1496 | |
|
1496 | 1497 | This function operates on hunks coming out of patch.filterpatch, that is |
|
1497 | 1498 | a list of the form: [header1, hunk1, hunk2, header2...]. Example usage: |
|
1498 | 1499 | |
|
1499 | 1500 | >>> rawpatch = b"""diff --git a/folder1/g b/folder1/g |
|
1500 | 1501 | ... --- a/folder1/g |
|
1501 | 1502 | ... +++ b/folder1/g |
|
1502 | 1503 | ... @@ -1,7 +1,7 @@ |
|
1503 | 1504 | ... +firstline |
|
1504 | 1505 | ... c |
|
1505 | 1506 | ... 1 |
|
1506 | 1507 | ... 2 |
|
1507 | 1508 | ... + 3 |
|
1508 | 1509 | ... -4 |
|
1509 | 1510 | ... 5 |
|
1510 | 1511 | ... d |
|
1511 | 1512 | ... +lastline""" |
|
1512 | 1513 | >>> hunks = parsepatch([rawpatch]) |
|
1513 | 1514 | >>> hunkscomingfromfilterpatch = [] |
|
1514 | 1515 | >>> for h in hunks: |
|
1515 | 1516 | ... hunkscomingfromfilterpatch.append(h) |
|
1516 | 1517 | ... hunkscomingfromfilterpatch.extend(h.hunks) |
|
1517 | 1518 | |
|
1518 | 1519 | >>> reversedhunks = reversehunks(hunkscomingfromfilterpatch) |
|
1519 | 1520 | >>> from . import util |
|
1520 | 1521 | >>> fp = util.stringio() |
|
1521 | 1522 | >>> for c in reversedhunks: |
|
1522 | 1523 | ... c.write(fp) |
|
1523 | 1524 | >>> fp.seek(0) or None |
|
1524 | 1525 | >>> reversedpatch = fp.read() |
|
1525 | 1526 | >>> print(pycompat.sysstr(reversedpatch)) |
|
1526 | 1527 | diff --git a/folder1/g b/folder1/g |
|
1527 | 1528 | --- a/folder1/g |
|
1528 | 1529 | +++ b/folder1/g |
|
1529 | 1530 | @@ -1,4 +1,3 @@ |
|
1530 | 1531 | -firstline |
|
1531 | 1532 | c |
|
1532 | 1533 | 1 |
|
1533 | 1534 | 2 |
|
1534 | 1535 | @@ -2,6 +1,6 @@ |
|
1535 | 1536 | c |
|
1536 | 1537 | 1 |
|
1537 | 1538 | 2 |
|
1538 | 1539 | - 3 |
|
1539 | 1540 | +4 |
|
1540 | 1541 | 5 |
|
1541 | 1542 | d |
|
1542 | 1543 | @@ -6,3 +5,2 @@ |
|
1543 | 1544 | 5 |
|
1544 | 1545 | d |
|
1545 | 1546 | -lastline |
|
1546 | 1547 | |
|
1547 | 1548 | ''' |
|
1548 | 1549 | |
|
1549 | 1550 | newhunks = [] |
|
1550 | 1551 | for c in hunks: |
|
1551 | 1552 | if util.safehasattr(c, 'reversehunk'): |
|
1552 | 1553 | c = c.reversehunk() |
|
1553 | 1554 | newhunks.append(c) |
|
1554 | 1555 | return newhunks |
|
1555 | 1556 | |
|
1556 | 1557 | def parsepatch(originalchunks, maxcontext=None): |
|
1557 | 1558 | """patch -> [] of headers -> [] of hunks |
|
1558 | 1559 | |
|
1559 | 1560 | If maxcontext is not None, trim context lines if necessary. |
|
1560 | 1561 | |
|
1561 | 1562 | >>> rawpatch = b'''diff --git a/folder1/g b/folder1/g |
|
1562 | 1563 | ... --- a/folder1/g |
|
1563 | 1564 | ... +++ b/folder1/g |
|
1564 | 1565 | ... @@ -1,8 +1,10 @@ |
|
1565 | 1566 | ... 1 |
|
1566 | 1567 | ... 2 |
|
1567 | 1568 | ... -3 |
|
1568 | 1569 | ... 4 |
|
1569 | 1570 | ... 5 |
|
1570 | 1571 | ... 6 |
|
1571 | 1572 | ... +6.1 |
|
1572 | 1573 | ... +6.2 |
|
1573 | 1574 | ... 7 |
|
1574 | 1575 | ... 8 |
|
1575 | 1576 | ... +9''' |
|
1576 | 1577 | >>> out = util.stringio() |
|
1577 | 1578 | >>> headers = parsepatch([rawpatch], maxcontext=1) |
|
1578 | 1579 | >>> for header in headers: |
|
1579 | 1580 | ... header.write(out) |
|
1580 | 1581 | ... for hunk in header.hunks: |
|
1581 | 1582 | ... hunk.write(out) |
|
1582 | 1583 | >>> print(pycompat.sysstr(out.getvalue())) |
|
1583 | 1584 | diff --git a/folder1/g b/folder1/g |
|
1584 | 1585 | --- a/folder1/g |
|
1585 | 1586 | +++ b/folder1/g |
|
1586 | 1587 | @@ -2,3 +2,2 @@ |
|
1587 | 1588 | 2 |
|
1588 | 1589 | -3 |
|
1589 | 1590 | 4 |
|
1590 | 1591 | @@ -6,2 +5,4 @@ |
|
1591 | 1592 | 6 |
|
1592 | 1593 | +6.1 |
|
1593 | 1594 | +6.2 |
|
1594 | 1595 | 7 |
|
1595 | 1596 | @@ -8,1 +9,2 @@ |
|
1596 | 1597 | 8 |
|
1597 | 1598 | +9 |
|
1598 | 1599 | """ |
|
1599 | 1600 | class parser(object): |
|
1600 | 1601 | """patch parsing state machine""" |
|
1601 | 1602 | def __init__(self): |
|
1602 | 1603 | self.fromline = 0 |
|
1603 | 1604 | self.toline = 0 |
|
1604 | 1605 | self.proc = '' |
|
1605 | 1606 | self.header = None |
|
1606 | 1607 | self.context = [] |
|
1607 | 1608 | self.before = [] |
|
1608 | 1609 | self.hunk = [] |
|
1609 | 1610 | self.headers = [] |
|
1610 | 1611 | |
|
1611 | 1612 | def addrange(self, limits): |
|
1612 | 1613 | fromstart, fromend, tostart, toend, proc = limits |
|
1613 | 1614 | self.fromline = int(fromstart) |
|
1614 | 1615 | self.toline = int(tostart) |
|
1615 | 1616 | self.proc = proc |
|
1616 | 1617 | |
|
1617 | 1618 | def addcontext(self, context): |
|
1618 | 1619 | if self.hunk: |
|
1619 | 1620 | h = recordhunk(self.header, self.fromline, self.toline, |
|
1620 | 1621 | self.proc, self.before, self.hunk, context, maxcontext) |
|
1621 | 1622 | self.header.hunks.append(h) |
|
1622 | 1623 | self.fromline += len(self.before) + h.removed |
|
1623 | 1624 | self.toline += len(self.before) + h.added |
|
1624 | 1625 | self.before = [] |
|
1625 | 1626 | self.hunk = [] |
|
1626 | 1627 | self.context = context |
|
1627 | 1628 | |
|
1628 | 1629 | def addhunk(self, hunk): |
|
1629 | 1630 | if self.context: |
|
1630 | 1631 | self.before = self.context |
|
1631 | 1632 | self.context = [] |
|
1632 | 1633 | self.hunk = hunk |
|
1633 | 1634 | |
|
1634 | 1635 | def newfile(self, hdr): |
|
1635 | 1636 | self.addcontext([]) |
|
1636 | 1637 | h = header(hdr) |
|
1637 | 1638 | self.headers.append(h) |
|
1638 | 1639 | self.header = h |
|
1639 | 1640 | |
|
1640 | 1641 | def addother(self, line): |
|
1641 | 1642 | pass # 'other' lines are ignored |
|
1642 | 1643 | |
|
1643 | 1644 | def finished(self): |
|
1644 | 1645 | self.addcontext([]) |
|
1645 | 1646 | return self.headers |
|
1646 | 1647 | |
|
1647 | 1648 | transitions = { |
|
1648 | 1649 | 'file': {'context': addcontext, |
|
1649 | 1650 | 'file': newfile, |
|
1650 | 1651 | 'hunk': addhunk, |
|
1651 | 1652 | 'range': addrange}, |
|
1652 | 1653 | 'context': {'file': newfile, |
|
1653 | 1654 | 'hunk': addhunk, |
|
1654 | 1655 | 'range': addrange, |
|
1655 | 1656 | 'other': addother}, |
|
1656 | 1657 | 'hunk': {'context': addcontext, |
|
1657 | 1658 | 'file': newfile, |
|
1658 | 1659 | 'range': addrange}, |
|
1659 | 1660 | 'range': {'context': addcontext, |
|
1660 | 1661 | 'hunk': addhunk}, |
|
1661 | 1662 | 'other': {'other': addother}, |
|
1662 | 1663 | } |
|
1663 | 1664 | |
|
1664 | 1665 | p = parser() |
|
1665 | 1666 | fp = stringio() |
|
1666 | 1667 | fp.write(''.join(originalchunks)) |
|
1667 | 1668 | fp.seek(0) |
|
1668 | 1669 | |
|
1669 | 1670 | state = 'context' |
|
1670 | 1671 | for newstate, data in scanpatch(fp): |
|
1671 | 1672 | try: |
|
1672 | 1673 | p.transitions[state][newstate](p, data) |
|
1673 | 1674 | except KeyError: |
|
1674 | 1675 | raise PatchError('unhandled transition: %s -> %s' % |
|
1675 | 1676 | (state, newstate)) |
|
1676 | 1677 | state = newstate |
|
1677 | 1678 | del fp |
|
1678 | 1679 | return p.finished() |
|
1679 | 1680 | |
|
1680 | 1681 | def pathtransform(path, strip, prefix): |
|
1681 | 1682 | '''turn a path from a patch into a path suitable for the repository |
|
1682 | 1683 | |
|
1683 | 1684 | prefix, if not empty, is expected to be normalized with a / at the end. |
|
1684 | 1685 | |
|
1685 | 1686 | Returns (stripped components, path in repository). |
|
1686 | 1687 | |
|
1687 | 1688 | >>> pathtransform(b'a/b/c', 0, b'') |
|
1688 | 1689 | ('', 'a/b/c') |
|
1689 | 1690 | >>> pathtransform(b' a/b/c ', 0, b'') |
|
1690 | 1691 | ('', ' a/b/c') |
|
1691 | 1692 | >>> pathtransform(b' a/b/c ', 2, b'') |
|
1692 | 1693 | ('a/b/', 'c') |
|
1693 | 1694 | >>> pathtransform(b'a/b/c', 0, b'd/e/') |
|
1694 | 1695 | ('', 'd/e/a/b/c') |
|
1695 | 1696 | >>> pathtransform(b' a//b/c ', 2, b'd/e/') |
|
1696 | 1697 | ('a//b/', 'd/e/c') |
|
1697 | 1698 | >>> pathtransform(b'a/b/c', 3, b'') |
|
1698 | 1699 | Traceback (most recent call last): |
|
1699 | 1700 | PatchError: unable to strip away 1 of 3 dirs from a/b/c |
|
1700 | 1701 | ''' |
|
1701 | 1702 | pathlen = len(path) |
|
1702 | 1703 | i = 0 |
|
1703 | 1704 | if strip == 0: |
|
1704 | 1705 | return '', prefix + path.rstrip() |
|
1705 | 1706 | count = strip |
|
1706 | 1707 | while count > 0: |
|
1707 | 1708 | i = path.find('/', i) |
|
1708 | 1709 | if i == -1: |
|
1709 | 1710 | raise PatchError(_("unable to strip away %d of %d dirs from %s") % |
|
1710 | 1711 | (count, strip, path)) |
|
1711 | 1712 | i += 1 |
|
1712 | 1713 | # consume '//' in the path |
|
1713 | 1714 | while i < pathlen - 1 and path[i:i + 1] == '/': |
|
1714 | 1715 | i += 1 |
|
1715 | 1716 | count -= 1 |
|
1716 | 1717 | return path[:i].lstrip(), prefix + path[i:].rstrip() |
|
1717 | 1718 | |
|
1718 | 1719 | def makepatchmeta(backend, afile_orig, bfile_orig, hunk, strip, prefix): |
|
1719 | 1720 | nulla = afile_orig == "/dev/null" |
|
1720 | 1721 | nullb = bfile_orig == "/dev/null" |
|
1721 | 1722 | create = nulla and hunk.starta == 0 and hunk.lena == 0 |
|
1722 | 1723 | remove = nullb and hunk.startb == 0 and hunk.lenb == 0 |
|
1723 | 1724 | abase, afile = pathtransform(afile_orig, strip, prefix) |
|
1724 | 1725 | gooda = not nulla and backend.exists(afile) |
|
1725 | 1726 | bbase, bfile = pathtransform(bfile_orig, strip, prefix) |
|
1726 | 1727 | if afile == bfile: |
|
1727 | 1728 | goodb = gooda |
|
1728 | 1729 | else: |
|
1729 | 1730 | goodb = not nullb and backend.exists(bfile) |
|
1730 | 1731 | missing = not goodb and not gooda and not create |
|
1731 | 1732 | |
|
1732 | 1733 | # some diff programs apparently produce patches where the afile is |
|
1733 | 1734 | # not /dev/null, but afile starts with bfile |
|
1734 | 1735 | abasedir = afile[:afile.rfind('/') + 1] |
|
1735 | 1736 | bbasedir = bfile[:bfile.rfind('/') + 1] |
|
1736 | 1737 | if (missing and abasedir == bbasedir and afile.startswith(bfile) |
|
1737 | 1738 | and hunk.starta == 0 and hunk.lena == 0): |
|
1738 | 1739 | create = True |
|
1739 | 1740 | missing = False |
|
1740 | 1741 | |
|
1741 | 1742 | # If afile is "a/b/foo" and bfile is "a/b/foo.orig" we assume the |
|
1742 | 1743 | # diff is between a file and its backup. In this case, the original |
|
1743 | 1744 | # file should be patched (see original mpatch code). |
|
1744 | 1745 | isbackup = (abase == bbase and bfile.startswith(afile)) |
|
1745 | 1746 | fname = None |
|
1746 | 1747 | if not missing: |
|
1747 | 1748 | if gooda and goodb: |
|
1748 | 1749 | if isbackup: |
|
1749 | 1750 | fname = afile |
|
1750 | 1751 | else: |
|
1751 | 1752 | fname = bfile |
|
1752 | 1753 | elif gooda: |
|
1753 | 1754 | fname = afile |
|
1754 | 1755 | |
|
1755 | 1756 | if not fname: |
|
1756 | 1757 | if not nullb: |
|
1757 | 1758 | if isbackup: |
|
1758 | 1759 | fname = afile |
|
1759 | 1760 | else: |
|
1760 | 1761 | fname = bfile |
|
1761 | 1762 | elif not nulla: |
|
1762 | 1763 | fname = afile |
|
1763 | 1764 | else: |
|
1764 | 1765 | raise PatchError(_("undefined source and destination files")) |
|
1765 | 1766 | |
|
1766 | 1767 | gp = patchmeta(fname) |
|
1767 | 1768 | if create: |
|
1768 | 1769 | gp.op = 'ADD' |
|
1769 | 1770 | elif remove: |
|
1770 | 1771 | gp.op = 'DELETE' |
|
1771 | 1772 | return gp |
|
1772 | 1773 | |
|
1773 | 1774 | def scanpatch(fp): |
|
1774 | 1775 | """like patch.iterhunks, but yield different events |
|
1775 | 1776 | |
|
1776 | 1777 | - ('file', [header_lines + fromfile + tofile]) |
|
1777 | 1778 | - ('context', [context_lines]) |
|
1778 | 1779 | - ('hunk', [hunk_lines]) |
|
1779 | 1780 | - ('range', (-start,len, +start,len, proc)) |
|
1780 | 1781 | """ |
|
1781 | 1782 | lines_re = re.compile(br'@@ -(\d+),(\d+) \+(\d+),(\d+) @@\s*(.*)') |
|
1782 | 1783 | lr = linereader(fp) |
|
1783 | 1784 | |
|
1784 | 1785 | def scanwhile(first, p): |
|
1785 | 1786 | """scan lr while predicate holds""" |
|
1786 | 1787 | lines = [first] |
|
1787 | 1788 | for line in iter(lr.readline, ''): |
|
1788 | 1789 | if p(line): |
|
1789 | 1790 | lines.append(line) |
|
1790 | 1791 | else: |
|
1791 | 1792 | lr.push(line) |
|
1792 | 1793 | break |
|
1793 | 1794 | return lines |
|
1794 | 1795 | |
|
1795 | 1796 | for line in iter(lr.readline, ''): |
|
1796 | 1797 | if line.startswith('diff --git a/') or line.startswith('diff -r '): |
|
1797 | 1798 | def notheader(line): |
|
1798 | 1799 | s = line.split(None, 1) |
|
1799 | 1800 | return not s or s[0] not in ('---', 'diff') |
|
1800 | 1801 | header = scanwhile(line, notheader) |
|
1801 | 1802 | fromfile = lr.readline() |
|
1802 | 1803 | if fromfile.startswith('---'): |
|
1803 | 1804 | tofile = lr.readline() |
|
1804 | 1805 | header += [fromfile, tofile] |
|
1805 | 1806 | else: |
|
1806 | 1807 | lr.push(fromfile) |
|
1807 | 1808 | yield 'file', header |
|
1808 | 1809 | elif line.startswith(' '): |
|
1809 | 1810 | cs = (' ', '\\') |
|
1810 | 1811 | yield 'context', scanwhile(line, lambda l: l.startswith(cs)) |
|
1811 | 1812 | elif line.startswith(('-', '+')): |
|
1812 | 1813 | cs = ('-', '+', '\\') |
|
1813 | 1814 | yield 'hunk', scanwhile(line, lambda l: l.startswith(cs)) |
|
1814 | 1815 | else: |
|
1815 | 1816 | m = lines_re.match(line) |
|
1816 | 1817 | if m: |
|
1817 | 1818 | yield 'range', m.groups() |
|
1818 | 1819 | else: |
|
1819 | 1820 | yield 'other', line |
|
1820 | 1821 | |
|
1821 | 1822 | def scangitpatch(lr, firstline): |
|
1822 | 1823 | """ |
|
1823 | 1824 | Git patches can emit: |
|
1824 | 1825 | - rename a to b |
|
1825 | 1826 | - change b |
|
1826 | 1827 | - copy a to c |
|
1827 | 1828 | - change c |
|
1828 | 1829 | |
|
1829 | 1830 | We cannot apply this sequence as-is, the renamed 'a' could not be |
|
1830 | 1831 | found for it would have been renamed already. And we cannot copy |
|
1831 | 1832 | from 'b' instead because 'b' would have been changed already. So |
|
1832 | 1833 | we scan the git patch for copy and rename commands so we can |
|
1833 | 1834 | perform the copies ahead of time. |
|
1834 | 1835 | """ |
|
1835 | 1836 | pos = 0 |
|
1836 | 1837 | try: |
|
1837 | 1838 | pos = lr.fp.tell() |
|
1838 | 1839 | fp = lr.fp |
|
1839 | 1840 | except IOError: |
|
1840 | 1841 | fp = stringio(lr.fp.read()) |
|
1841 | 1842 | gitlr = linereader(fp) |
|
1842 | 1843 | gitlr.push(firstline) |
|
1843 | 1844 | gitpatches = readgitpatch(gitlr) |
|
1844 | 1845 | fp.seek(pos) |
|
1845 | 1846 | return gitpatches |
|
1846 | 1847 | |
|
1847 | 1848 | def iterhunks(fp): |
|
1848 | 1849 | """Read a patch and yield the following events: |
|
1849 | 1850 | - ("file", afile, bfile, firsthunk): select a new target file. |
|
1850 | 1851 | - ("hunk", hunk): a new hunk is ready to be applied, follows a |
|
1851 | 1852 | "file" event. |
|
1852 | 1853 | - ("git", gitchanges): current diff is in git format, gitchanges |
|
1853 | 1854 | maps filenames to gitpatch records. Unique event. |
|
1854 | 1855 | """ |
|
1855 | 1856 | afile = "" |
|
1856 | 1857 | bfile = "" |
|
1857 | 1858 | state = None |
|
1858 | 1859 | hunknum = 0 |
|
1859 | 1860 | emitfile = newfile = False |
|
1860 | 1861 | gitpatches = None |
|
1861 | 1862 | |
|
1862 | 1863 | # our states |
|
1863 | 1864 | BFILE = 1 |
|
1864 | 1865 | context = None |
|
1865 | 1866 | lr = linereader(fp) |
|
1866 | 1867 | |
|
1867 | 1868 | for x in iter(lr.readline, ''): |
|
1868 | 1869 | if state == BFILE and ( |
|
1869 | 1870 | (not context and x.startswith('@')) |
|
1870 | 1871 | or (context is not False and x.startswith('***************')) |
|
1871 | 1872 | or x.startswith('GIT binary patch')): |
|
1872 | 1873 | gp = None |
|
1873 | 1874 | if (gitpatches and |
|
1874 | 1875 | gitpatches[-1].ispatching(afile, bfile)): |
|
1875 | 1876 | gp = gitpatches.pop() |
|
1876 | 1877 | if x.startswith('GIT binary patch'): |
|
1877 | 1878 | h = binhunk(lr, gp.path) |
|
1878 | 1879 | else: |
|
1879 | 1880 | if context is None and x.startswith('***************'): |
|
1880 | 1881 | context = True |
|
1881 | 1882 | h = hunk(x, hunknum + 1, lr, context) |
|
1882 | 1883 | hunknum += 1 |
|
1883 | 1884 | if emitfile: |
|
1884 | 1885 | emitfile = False |
|
1885 | 1886 | yield 'file', (afile, bfile, h, gp and gp.copy() or None) |
|
1886 | 1887 | yield 'hunk', h |
|
1887 | 1888 | elif x.startswith('diff --git a/'): |
|
1888 | 1889 | m = gitre.match(x.rstrip(' \r\n')) |
|
1889 | 1890 | if not m: |
|
1890 | 1891 | continue |
|
1891 | 1892 | if gitpatches is None: |
|
1892 | 1893 | # scan whole input for git metadata |
|
1893 | 1894 | gitpatches = scangitpatch(lr, x) |
|
1894 | 1895 | yield 'git', [g.copy() for g in gitpatches |
|
1895 | 1896 | if g.op in ('COPY', 'RENAME')] |
|
1896 | 1897 | gitpatches.reverse() |
|
1897 | 1898 | afile = 'a/' + m.group(1) |
|
1898 | 1899 | bfile = 'b/' + m.group(2) |
|
1899 | 1900 | while gitpatches and not gitpatches[-1].ispatching(afile, bfile): |
|
1900 | 1901 | gp = gitpatches.pop() |
|
1901 | 1902 | yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy()) |
|
1902 | 1903 | if not gitpatches: |
|
1903 | 1904 | raise PatchError(_('failed to synchronize metadata for "%s"') |
|
1904 | 1905 | % afile[2:]) |
|
1905 | 1906 | gp = gitpatches[-1] |
|
1906 | 1907 | newfile = True |
|
1907 | 1908 | elif x.startswith('---'): |
|
1908 | 1909 | # check for a unified diff |
|
1909 | 1910 | l2 = lr.readline() |
|
1910 | 1911 | if not l2.startswith('+++'): |
|
1911 | 1912 | lr.push(l2) |
|
1912 | 1913 | continue |
|
1913 | 1914 | newfile = True |
|
1914 | 1915 | context = False |
|
1915 | 1916 | afile = parsefilename(x) |
|
1916 | 1917 | bfile = parsefilename(l2) |
|
1917 | 1918 | elif x.startswith('***'): |
|
1918 | 1919 | # check for a context diff |
|
1919 | 1920 | l2 = lr.readline() |
|
1920 | 1921 | if not l2.startswith('---'): |
|
1921 | 1922 | lr.push(l2) |
|
1922 | 1923 | continue |
|
1923 | 1924 | l3 = lr.readline() |
|
1924 | 1925 | lr.push(l3) |
|
1925 | 1926 | if not l3.startswith("***************"): |
|
1926 | 1927 | lr.push(l2) |
|
1927 | 1928 | continue |
|
1928 | 1929 | newfile = True |
|
1929 | 1930 | context = True |
|
1930 | 1931 | afile = parsefilename(x) |
|
1931 | 1932 | bfile = parsefilename(l2) |
|
1932 | 1933 | |
|
1933 | 1934 | if newfile: |
|
1934 | 1935 | newfile = False |
|
1935 | 1936 | emitfile = True |
|
1936 | 1937 | state = BFILE |
|
1937 | 1938 | hunknum = 0 |
|
1938 | 1939 | |
|
1939 | 1940 | while gitpatches: |
|
1940 | 1941 | gp = gitpatches.pop() |
|
1941 | 1942 | yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy()) |
|
1942 | 1943 | |
|
1943 | 1944 | def applybindelta(binchunk, data): |
|
1944 | 1945 | """Apply a binary delta hunk |
|
1945 | 1946 | The algorithm used is the algorithm from git's patch-delta.c |
|
1946 | 1947 | """ |
|
1947 | 1948 | def deltahead(binchunk): |
|
1948 | 1949 | i = 0 |
|
1949 | 1950 | for c in pycompat.bytestr(binchunk): |
|
1950 | 1951 | i += 1 |
|
1951 | 1952 | if not (ord(c) & 0x80): |
|
1952 | 1953 | return i |
|
1953 | 1954 | return i |
|
1954 | 1955 | out = "" |
|
1955 | 1956 | s = deltahead(binchunk) |
|
1956 | 1957 | binchunk = binchunk[s:] |
|
1957 | 1958 | s = deltahead(binchunk) |
|
1958 | 1959 | binchunk = binchunk[s:] |
|
1959 | 1960 | i = 0 |
|
1960 | 1961 | while i < len(binchunk): |
|
1961 | 1962 | cmd = ord(binchunk[i:i + 1]) |
|
1962 | 1963 | i += 1 |
|
1963 | 1964 | if (cmd & 0x80): |
|
1964 | 1965 | offset = 0 |
|
1965 | 1966 | size = 0 |
|
1966 | 1967 | if (cmd & 0x01): |
|
1967 | 1968 | offset = ord(binchunk[i:i + 1]) |
|
1968 | 1969 | i += 1 |
|
1969 | 1970 | if (cmd & 0x02): |
|
1970 | 1971 | offset |= ord(binchunk[i:i + 1]) << 8 |
|
1971 | 1972 | i += 1 |
|
1972 | 1973 | if (cmd & 0x04): |
|
1973 | 1974 | offset |= ord(binchunk[i:i + 1]) << 16 |
|
1974 | 1975 | i += 1 |
|
1975 | 1976 | if (cmd & 0x08): |
|
1976 | 1977 | offset |= ord(binchunk[i:i + 1]) << 24 |
|
1977 | 1978 | i += 1 |
|
1978 | 1979 | if (cmd & 0x10): |
|
1979 | 1980 | size = ord(binchunk[i:i + 1]) |
|
1980 | 1981 | i += 1 |
|
1981 | 1982 | if (cmd & 0x20): |
|
1982 | 1983 | size |= ord(binchunk[i:i + 1]) << 8 |
|
1983 | 1984 | i += 1 |
|
1984 | 1985 | if (cmd & 0x40): |
|
1985 | 1986 | size |= ord(binchunk[i:i + 1]) << 16 |
|
1986 | 1987 | i += 1 |
|
1987 | 1988 | if size == 0: |
|
1988 | 1989 | size = 0x10000 |
|
1989 | 1990 | offset_end = offset + size |
|
1990 | 1991 | out += data[offset:offset_end] |
|
1991 | 1992 | elif cmd != 0: |
|
1992 | 1993 | offset_end = i + cmd |
|
1993 | 1994 | out += binchunk[i:offset_end] |
|
1994 | 1995 | i += cmd |
|
1995 | 1996 | else: |
|
1996 | 1997 | raise PatchError(_('unexpected delta opcode 0')) |
|
1997 | 1998 | return out |
|
1998 | 1999 | |
|
1999 | 2000 | def applydiff(ui, fp, backend, store, strip=1, prefix='', eolmode='strict'): |
|
2000 | 2001 | """Reads a patch from fp and tries to apply it. |
|
2001 | 2002 | |
|
2002 | 2003 | Returns 0 for a clean patch, -1 if any rejects were found and 1 if |
|
2003 | 2004 | there was any fuzz. |
|
2004 | 2005 | |
|
2005 | 2006 | If 'eolmode' is 'strict', the patch content and patched file are |
|
2006 | 2007 | read in binary mode. Otherwise, line endings are ignored when |
|
2007 | 2008 | patching then normalized according to 'eolmode'. |
|
2008 | 2009 | """ |
|
2009 | 2010 | return _applydiff(ui, fp, patchfile, backend, store, strip=strip, |
|
2010 | 2011 | prefix=prefix, eolmode=eolmode) |
|
2011 | 2012 | |
|
2012 | 2013 | def _canonprefix(repo, prefix): |
|
2013 | 2014 | if prefix: |
|
2014 | 2015 | prefix = pathutil.canonpath(repo.root, repo.getcwd(), prefix) |
|
2015 | 2016 | if prefix != '': |
|
2016 | 2017 | prefix += '/' |
|
2017 | 2018 | return prefix |
|
2018 | 2019 | |
|
2019 | 2020 | def _applydiff(ui, fp, patcher, backend, store, strip=1, prefix='', |
|
2020 | 2021 | eolmode='strict'): |
|
2021 | 2022 | prefix = _canonprefix(backend.repo, prefix) |
|
2022 | 2023 | def pstrip(p): |
|
2023 | 2024 | return pathtransform(p, strip - 1, prefix)[1] |
|
2024 | 2025 | |
|
2025 | 2026 | rejects = 0 |
|
2026 | 2027 | err = 0 |
|
2027 | 2028 | current_file = None |
|
2028 | 2029 | |
|
2029 | 2030 | for state, values in iterhunks(fp): |
|
2030 | 2031 | if state == 'hunk': |
|
2031 | 2032 | if not current_file: |
|
2032 | 2033 | continue |
|
2033 | 2034 | ret = current_file.apply(values) |
|
2034 | 2035 | if ret > 0: |
|
2035 | 2036 | err = 1 |
|
2036 | 2037 | elif state == 'file': |
|
2037 | 2038 | if current_file: |
|
2038 | 2039 | rejects += current_file.close() |
|
2039 | 2040 | current_file = None |
|
2040 | 2041 | afile, bfile, first_hunk, gp = values |
|
2041 | 2042 | if gp: |
|
2042 | 2043 | gp.path = pstrip(gp.path) |
|
2043 | 2044 | if gp.oldpath: |
|
2044 | 2045 | gp.oldpath = pstrip(gp.oldpath) |
|
2045 | 2046 | else: |
|
2046 | 2047 | gp = makepatchmeta(backend, afile, bfile, first_hunk, strip, |
|
2047 | 2048 | prefix) |
|
2048 | 2049 | if gp.op == 'RENAME': |
|
2049 | 2050 | backend.unlink(gp.oldpath) |
|
2050 | 2051 | if not first_hunk: |
|
2051 | 2052 | if gp.op == 'DELETE': |
|
2052 | 2053 | backend.unlink(gp.path) |
|
2053 | 2054 | continue |
|
2054 | 2055 | data, mode = None, None |
|
2055 | 2056 | if gp.op in ('RENAME', 'COPY'): |
|
2056 | 2057 | data, mode = store.getfile(gp.oldpath)[:2] |
|
2057 | 2058 | if data is None: |
|
2058 | 2059 | # This means that the old path does not exist |
|
2059 | 2060 | raise PatchError(_("source file '%s' does not exist") |
|
2060 | 2061 | % gp.oldpath) |
|
2061 | 2062 | if gp.mode: |
|
2062 | 2063 | mode = gp.mode |
|
2063 | 2064 | if gp.op == 'ADD': |
|
2064 | 2065 | # Added files without content have no hunk and |
|
2065 | 2066 | # must be created |
|
2066 | 2067 | data = '' |
|
2067 | 2068 | if data or mode: |
|
2068 | 2069 | if (gp.op in ('ADD', 'RENAME', 'COPY') |
|
2069 | 2070 | and backend.exists(gp.path)): |
|
2070 | 2071 | raise PatchError(_("cannot create %s: destination " |
|
2071 | 2072 | "already exists") % gp.path) |
|
2072 | 2073 | backend.setfile(gp.path, data, mode, gp.oldpath) |
|
2073 | 2074 | continue |
|
2074 | 2075 | try: |
|
2075 | 2076 | current_file = patcher(ui, gp, backend, store, |
|
2076 | 2077 | eolmode=eolmode) |
|
2077 | 2078 | except PatchError as inst: |
|
2078 | 2079 | ui.warn(str(inst) + '\n') |
|
2079 | 2080 | current_file = None |
|
2080 | 2081 | rejects += 1 |
|
2081 | 2082 | continue |
|
2082 | 2083 | elif state == 'git': |
|
2083 | 2084 | for gp in values: |
|
2084 | 2085 | path = pstrip(gp.oldpath) |
|
2085 | 2086 | data, mode = backend.getfile(path) |
|
2086 | 2087 | if data is None: |
|
2087 | 2088 | # The error ignored here will trigger a getfile() |
|
2088 | 2089 | # error in a place more appropriate for error |
|
2089 | 2090 | # handling, and will not interrupt the patching |
|
2090 | 2091 | # process. |
|
2091 | 2092 | pass |
|
2092 | 2093 | else: |
|
2093 | 2094 | store.setfile(path, data, mode) |
|
2094 | 2095 | else: |
|
2095 | 2096 | raise error.Abort(_('unsupported parser state: %s') % state) |
|
2096 | 2097 | |
|
2097 | 2098 | if current_file: |
|
2098 | 2099 | rejects += current_file.close() |
|
2099 | 2100 | |
|
2100 | 2101 | if rejects: |
|
2101 | 2102 | return -1 |
|
2102 | 2103 | return err |
|
2103 | 2104 | |
|
2104 | 2105 | def _externalpatch(ui, repo, patcher, patchname, strip, files, |
|
2105 | 2106 | similarity): |
|
2106 | 2107 | """use <patcher> to apply <patchname> to the working directory. |
|
2107 | 2108 | returns whether patch was applied with fuzz factor.""" |
|
2108 | 2109 | |
|
2109 | 2110 | fuzz = False |
|
2110 | 2111 | args = [] |
|
2111 | 2112 | cwd = repo.root |
|
2112 | 2113 | if cwd: |
|
2113 | 2114 | args.append('-d %s' % procutil.shellquote(cwd)) |
|
2114 | 2115 | cmd = ('%s %s -p%d < %s' |
|
2115 | 2116 | % (patcher, ' '.join(args), strip, procutil.shellquote(patchname))) |
|
2116 | 2117 | ui.debug('Using external patch tool: %s\n' % cmd) |
|
2117 | 2118 | fp = procutil.popen(cmd, 'rb') |
|
2118 | 2119 | try: |
|
2119 | 2120 | for line in util.iterfile(fp): |
|
2120 | 2121 | line = line.rstrip() |
|
2121 | 2122 | ui.note(line + '\n') |
|
2122 | 2123 | if line.startswith('patching file '): |
|
2123 | 2124 | pf = util.parsepatchoutput(line) |
|
2124 | 2125 | printed_file = False |
|
2125 | 2126 | files.add(pf) |
|
2126 | 2127 | elif line.find('with fuzz') >= 0: |
|
2127 | 2128 | fuzz = True |
|
2128 | 2129 | if not printed_file: |
|
2129 | 2130 | ui.warn(pf + '\n') |
|
2130 | 2131 | printed_file = True |
|
2131 | 2132 | ui.warn(line + '\n') |
|
2132 | 2133 | elif line.find('saving rejects to file') >= 0: |
|
2133 | 2134 | ui.warn(line + '\n') |
|
2134 | 2135 | elif line.find('FAILED') >= 0: |
|
2135 | 2136 | if not printed_file: |
|
2136 | 2137 | ui.warn(pf + '\n') |
|
2137 | 2138 | printed_file = True |
|
2138 | 2139 | ui.warn(line + '\n') |
|
2139 | 2140 | finally: |
|
2140 | 2141 | if files: |
|
2141 | 2142 | scmutil.marktouched(repo, files, similarity) |
|
2142 | 2143 | code = fp.close() |
|
2143 | 2144 | if code: |
|
2144 | 2145 | raise PatchError(_("patch command failed: %s") % |
|
2145 | 2146 | procutil.explainexit(code)) |
|
2146 | 2147 | return fuzz |
|
2147 | 2148 | |
|
2148 | 2149 | def patchbackend(ui, backend, patchobj, strip, prefix, files=None, |
|
2149 | 2150 | eolmode='strict'): |
|
2150 | 2151 | if files is None: |
|
2151 | 2152 | files = set() |
|
2152 | 2153 | if eolmode is None: |
|
2153 | 2154 | eolmode = ui.config('patch', 'eol') |
|
2154 | 2155 | if eolmode.lower() not in eolmodes: |
|
2155 | 2156 | raise error.Abort(_('unsupported line endings type: %s') % eolmode) |
|
2156 | 2157 | eolmode = eolmode.lower() |
|
2157 | 2158 | |
|
2158 | 2159 | store = filestore() |
|
2159 | 2160 | try: |
|
2160 | 2161 | fp = open(patchobj, 'rb') |
|
2161 | 2162 | except TypeError: |
|
2162 | 2163 | fp = patchobj |
|
2163 | 2164 | try: |
|
2164 | 2165 | ret = applydiff(ui, fp, backend, store, strip=strip, prefix=prefix, |
|
2165 | 2166 | eolmode=eolmode) |
|
2166 | 2167 | finally: |
|
2167 | 2168 | if fp != patchobj: |
|
2168 | 2169 | fp.close() |
|
2169 | 2170 | files.update(backend.close()) |
|
2170 | 2171 | store.close() |
|
2171 | 2172 | if ret < 0: |
|
2172 | 2173 | raise PatchError(_('patch failed to apply')) |
|
2173 | 2174 | return ret > 0 |
|
2174 | 2175 | |
|
2175 | 2176 | def internalpatch(ui, repo, patchobj, strip, prefix='', files=None, |
|
2176 | 2177 | eolmode='strict', similarity=0): |
|
2177 | 2178 | """use builtin patch to apply <patchobj> to the working directory. |
|
2178 | 2179 | returns whether patch was applied with fuzz factor.""" |
|
2179 | 2180 | backend = workingbackend(ui, repo, similarity) |
|
2180 | 2181 | return patchbackend(ui, backend, patchobj, strip, prefix, files, eolmode) |
|
2181 | 2182 | |
|
2182 | 2183 | def patchrepo(ui, repo, ctx, store, patchobj, strip, prefix, files=None, |
|
2183 | 2184 | eolmode='strict'): |
|
2184 | 2185 | backend = repobackend(ui, repo, ctx, store) |
|
2185 | 2186 | return patchbackend(ui, backend, patchobj, strip, prefix, files, eolmode) |
|
2186 | 2187 | |
|
2187 | 2188 | def patch(ui, repo, patchname, strip=1, prefix='', files=None, eolmode='strict', |
|
2188 | 2189 | similarity=0): |
|
2189 | 2190 | """Apply <patchname> to the working directory. |
|
2190 | 2191 | |
|
2191 | 2192 | 'eolmode' specifies how end of lines should be handled. It can be: |
|
2192 | 2193 | - 'strict': inputs are read in binary mode, EOLs are preserved |
|
2193 | 2194 | - 'crlf': EOLs are ignored when patching and reset to CRLF |
|
2194 | 2195 | - 'lf': EOLs are ignored when patching and reset to LF |
|
2195 | 2196 | - None: get it from user settings, default to 'strict' |
|
2196 | 2197 | 'eolmode' is ignored when using an external patcher program. |
|
2197 | 2198 | |
|
2198 | 2199 | Returns whether patch was applied with fuzz factor. |
|
2199 | 2200 | """ |
|
2200 | 2201 | patcher = ui.config('ui', 'patch') |
|
2201 | 2202 | if files is None: |
|
2202 | 2203 | files = set() |
|
2203 | 2204 | if patcher: |
|
2204 | 2205 | return _externalpatch(ui, repo, patcher, patchname, strip, |
|
2205 | 2206 | files, similarity) |
|
2206 | 2207 | return internalpatch(ui, repo, patchname, strip, prefix, files, eolmode, |
|
2207 | 2208 | similarity) |
|
2208 | 2209 | |
|
2209 | 2210 | def changedfiles(ui, repo, patchpath, strip=1, prefix=''): |
|
2210 | 2211 | backend = fsbackend(ui, repo.root) |
|
2211 | 2212 | prefix = _canonprefix(repo, prefix) |
|
2212 | 2213 | with open(patchpath, 'rb') as fp: |
|
2213 | 2214 | changed = set() |
|
2214 | 2215 | for state, values in iterhunks(fp): |
|
2215 | 2216 | if state == 'file': |
|
2216 | 2217 | afile, bfile, first_hunk, gp = values |
|
2217 | 2218 | if gp: |
|
2218 | 2219 | gp.path = pathtransform(gp.path, strip - 1, prefix)[1] |
|
2219 | 2220 | if gp.oldpath: |
|
2220 | 2221 | gp.oldpath = pathtransform(gp.oldpath, strip - 1, |
|
2221 | 2222 | prefix)[1] |
|
2222 | 2223 | else: |
|
2223 | 2224 | gp = makepatchmeta(backend, afile, bfile, first_hunk, strip, |
|
2224 | 2225 | prefix) |
|
2225 | 2226 | changed.add(gp.path) |
|
2226 | 2227 | if gp.op == 'RENAME': |
|
2227 | 2228 | changed.add(gp.oldpath) |
|
2228 | 2229 | elif state not in ('hunk', 'git'): |
|
2229 | 2230 | raise error.Abort(_('unsupported parser state: %s') % state) |
|
2230 | 2231 | return changed |
|
2231 | 2232 | |
|
2232 | 2233 | class GitDiffRequired(Exception): |
|
2233 | 2234 | pass |
|
2234 | 2235 | |
|
2235 | def diffallopts(ui, opts=None, untrusted=False, section='diff'): | |
|
2236 | '''return diffopts with all features supported and parsed''' | |
|
2237 | return difffeatureopts(ui, opts=opts, untrusted=untrusted, section=section, | |
|
2238 | git=True, whitespace=True, formatchanging=True) | |
|
2239 | ||
|
2240 | diffopts = diffallopts | |
|
2241 | ||
|
2242 | def difffeatureopts(ui, opts=None, untrusted=False, section='diff', git=False, | |
|
2243 | whitespace=False, formatchanging=False): | |
|
2244 | '''return diffopts with only opted-in features parsed | |
|
2245 | ||
|
2246 | Features: | |
|
2247 | - git: git-style diffs | |
|
2248 | - whitespace: whitespace options like ignoreblanklines and ignorews | |
|
2249 | - formatchanging: options that will likely break or cause correctness issues | |
|
2250 | with most diff parsers | |
|
2251 | ''' | |
|
2252 | def get(key, name=None, getter=ui.configbool, forceplain=None): | |
|
2253 | if opts: | |
|
2254 | v = opts.get(key) | |
|
2255 | # diffopts flags are either None-default (which is passed | |
|
2256 | # through unchanged, so we can identify unset values), or | |
|
2257 | # some other falsey default (eg --unified, which defaults | |
|
2258 | # to an empty string). We only want to override the config | |
|
2259 | # entries from hgrc with command line values if they | |
|
2260 | # appear to have been set, which is any truthy value, | |
|
2261 | # True, or False. | |
|
2262 | if v or isinstance(v, bool): | |
|
2263 | return v | |
|
2264 | if forceplain is not None and ui.plain(): | |
|
2265 | return forceplain | |
|
2266 | return getter(section, name or key, untrusted=untrusted) | |
|
2267 | ||
|
2268 | # core options, expected to be understood by every diff parser | |
|
2269 | buildopts = { | |
|
2270 | 'nodates': get('nodates'), | |
|
2271 | 'showfunc': get('show_function', 'showfunc'), | |
|
2272 | 'context': get('unified', getter=ui.config), | |
|
2273 | } | |
|
2274 | buildopts['worddiff'] = ui.configbool('experimental', 'worddiff') | |
|
2275 | buildopts['xdiff'] = ui.configbool('experimental', 'xdiff') | |
|
2276 | ||
|
2277 | if git: | |
|
2278 | buildopts['git'] = get('git') | |
|
2279 | ||
|
2280 | # since this is in the experimental section, we need to call | |
|
2281 | # ui.configbool directory | |
|
2282 | buildopts['showsimilarity'] = ui.configbool('experimental', | |
|
2283 | 'extendedheader.similarity') | |
|
2284 | ||
|
2285 | # need to inspect the ui object instead of using get() since we want to | |
|
2286 | # test for an int | |
|
2287 | hconf = ui.config('experimental', 'extendedheader.index') | |
|
2288 | if hconf is not None: | |
|
2289 | hlen = None | |
|
2290 | try: | |
|
2291 | # the hash config could be an integer (for length of hash) or a | |
|
2292 | # word (e.g. short, full, none) | |
|
2293 | hlen = int(hconf) | |
|
2294 | if hlen < 0 or hlen > 40: | |
|
2295 | msg = _("invalid length for extendedheader.index: '%d'\n") | |
|
2296 | ui.warn(msg % hlen) | |
|
2297 | except ValueError: | |
|
2298 | # default value | |
|
2299 | if hconf == 'short' or hconf == '': | |
|
2300 | hlen = 12 | |
|
2301 | elif hconf == 'full': | |
|
2302 | hlen = 40 | |
|
2303 | elif hconf != 'none': | |
|
2304 | msg = _("invalid value for extendedheader.index: '%s'\n") | |
|
2305 | ui.warn(msg % hconf) | |
|
2306 | finally: | |
|
2307 | buildopts['index'] = hlen | |
|
2308 | ||
|
2309 | if whitespace: | |
|
2310 | buildopts['ignorews'] = get('ignore_all_space', 'ignorews') | |
|
2311 | buildopts['ignorewsamount'] = get('ignore_space_change', | |
|
2312 | 'ignorewsamount') | |
|
2313 | buildopts['ignoreblanklines'] = get('ignore_blank_lines', | |
|
2314 | 'ignoreblanklines') | |
|
2315 | buildopts['ignorewseol'] = get('ignore_space_at_eol', 'ignorewseol') | |
|
2316 | if formatchanging: | |
|
2317 | buildopts['text'] = opts and opts.get('text') | |
|
2318 | binary = None if opts is None else opts.get('binary') | |
|
2319 | buildopts['nobinary'] = (not binary if binary is not None | |
|
2320 | else get('nobinary', forceplain=False)) | |
|
2321 | buildopts['noprefix'] = get('noprefix', forceplain=False) | |
|
2322 | ||
|
2323 | return mdiff.diffopts(**pycompat.strkwargs(buildopts)) | |
|
2236 | diffopts = diffutil.diffopts | |
|
2237 | diffallopts = diffutil.diffallopts | |
|
2238 | difffeatureopts = diffutil.difffeatureopts | |
|
2324 | 2239 | |
|
2325 | 2240 | def diff(repo, node1=None, node2=None, match=None, changes=None, |
|
2326 | 2241 | opts=None, losedatafn=None, prefix='', relroot='', copy=None, |
|
2327 | 2242 | hunksfilterfn=None): |
|
2328 | 2243 | '''yields diff of changes to files between two nodes, or node and |
|
2329 | 2244 | working directory. |
|
2330 | 2245 | |
|
2331 | 2246 | if node1 is None, use first dirstate parent instead. |
|
2332 | 2247 | if node2 is None, compare node1 with working directory. |
|
2333 | 2248 | |
|
2334 | 2249 | losedatafn(**kwarg) is a callable run when opts.upgrade=True and |
|
2335 | 2250 | every time some change cannot be represented with the current |
|
2336 | 2251 | patch format. Return False to upgrade to git patch format, True to |
|
2337 | 2252 | accept the loss or raise an exception to abort the diff. It is |
|
2338 | 2253 | called with the name of current file being diffed as 'fn'. If set |
|
2339 | 2254 | to None, patches will always be upgraded to git format when |
|
2340 | 2255 | necessary. |
|
2341 | 2256 | |
|
2342 | 2257 | prefix is a filename prefix that is prepended to all filenames on |
|
2343 | 2258 | display (used for subrepos). |
|
2344 | 2259 | |
|
2345 | 2260 | relroot, if not empty, must be normalized with a trailing /. Any match |
|
2346 | 2261 | patterns that fall outside it will be ignored. |
|
2347 | 2262 | |
|
2348 | 2263 | copy, if not empty, should contain mappings {dst@y: src@x} of copy |
|
2349 | 2264 | information. |
|
2350 | 2265 | |
|
2351 | 2266 | hunksfilterfn, if not None, should be a function taking a filectx and |
|
2352 | 2267 | hunks generator that may yield filtered hunks. |
|
2353 | 2268 | ''' |
|
2354 | 2269 | for fctx1, fctx2, hdr, hunks in diffhunks( |
|
2355 | 2270 | repo, node1=node1, node2=node2, |
|
2356 | 2271 | match=match, changes=changes, opts=opts, |
|
2357 | 2272 | losedatafn=losedatafn, prefix=prefix, relroot=relroot, copy=copy, |
|
2358 | 2273 | ): |
|
2359 | 2274 | if hunksfilterfn is not None: |
|
2360 | 2275 | # If the file has been removed, fctx2 is None; but this should |
|
2361 | 2276 | # not occur here since we catch removed files early in |
|
2362 | 2277 | # logcmdutil.getlinerangerevs() for 'hg log -L'. |
|
2363 | 2278 | assert fctx2 is not None, \ |
|
2364 | 2279 | 'fctx2 unexpectly None in diff hunks filtering' |
|
2365 | 2280 | hunks = hunksfilterfn(fctx2, hunks) |
|
2366 | 2281 | text = ''.join(sum((list(hlines) for hrange, hlines in hunks), [])) |
|
2367 | 2282 | if hdr and (text or len(hdr) > 1): |
|
2368 | 2283 | yield '\n'.join(hdr) + '\n' |
|
2369 | 2284 | if text: |
|
2370 | 2285 | yield text |
|
2371 | 2286 | |
|
2372 | 2287 | def diffhunks(repo, node1=None, node2=None, match=None, changes=None, |
|
2373 | 2288 | opts=None, losedatafn=None, prefix='', relroot='', copy=None): |
|
2374 | 2289 | """Yield diff of changes to files in the form of (`header`, `hunks`) tuples |
|
2375 | 2290 | where `header` is a list of diff headers and `hunks` is an iterable of |
|
2376 | 2291 | (`hunkrange`, `hunklines`) tuples. |
|
2377 | 2292 | |
|
2378 | 2293 | See diff() for the meaning of parameters. |
|
2379 | 2294 | """ |
|
2380 | 2295 | |
|
2381 | 2296 | if opts is None: |
|
2382 | 2297 | opts = mdiff.defaultopts |
|
2383 | 2298 | |
|
2384 | 2299 | if not node1 and not node2: |
|
2385 | 2300 | node1 = repo.dirstate.p1() |
|
2386 | 2301 | |
|
2387 | 2302 | def lrugetfilectx(): |
|
2388 | 2303 | cache = {} |
|
2389 | 2304 | order = collections.deque() |
|
2390 | 2305 | def getfilectx(f, ctx): |
|
2391 | 2306 | fctx = ctx.filectx(f, filelog=cache.get(f)) |
|
2392 | 2307 | if f not in cache: |
|
2393 | 2308 | if len(cache) > 20: |
|
2394 | 2309 | del cache[order.popleft()] |
|
2395 | 2310 | cache[f] = fctx.filelog() |
|
2396 | 2311 | else: |
|
2397 | 2312 | order.remove(f) |
|
2398 | 2313 | order.append(f) |
|
2399 | 2314 | return fctx |
|
2400 | 2315 | return getfilectx |
|
2401 | 2316 | getfilectx = lrugetfilectx() |
|
2402 | 2317 | |
|
2403 | 2318 | ctx1 = repo[node1] |
|
2404 | 2319 | ctx2 = repo[node2] |
|
2405 | 2320 | |
|
2406 | 2321 | relfiltered = False |
|
2407 | 2322 | if relroot != '' and match.always(): |
|
2408 | 2323 | # as a special case, create a new matcher with just the relroot |
|
2409 | 2324 | pats = [relroot] |
|
2410 | 2325 | match = scmutil.match(ctx2, pats, default='path') |
|
2411 | 2326 | relfiltered = True |
|
2412 | 2327 | |
|
2413 | 2328 | if not changes: |
|
2414 | 2329 | changes = repo.status(ctx1, ctx2, match=match) |
|
2415 | 2330 | modified, added, removed = changes[:3] |
|
2416 | 2331 | |
|
2417 | 2332 | if not modified and not added and not removed: |
|
2418 | 2333 | return [] |
|
2419 | 2334 | |
|
2420 | 2335 | if repo.ui.debugflag: |
|
2421 | 2336 | hexfunc = hex |
|
2422 | 2337 | else: |
|
2423 | 2338 | hexfunc = short |
|
2424 | 2339 | revs = [hexfunc(node) for node in [ctx1.node(), ctx2.node()] if node] |
|
2425 | 2340 | |
|
2426 | 2341 | if copy is None: |
|
2427 | 2342 | copy = {} |
|
2428 | 2343 | if opts.git or opts.upgrade: |
|
2429 | 2344 | copy = copies.pathcopies(ctx1, ctx2, match=match) |
|
2430 | 2345 | |
|
2431 | 2346 | if relroot is not None: |
|
2432 | 2347 | if not relfiltered: |
|
2433 | 2348 | # XXX this would ideally be done in the matcher, but that is |
|
2434 | 2349 | # generally meant to 'or' patterns, not 'and' them. In this case we |
|
2435 | 2350 | # need to 'and' all the patterns from the matcher with relroot. |
|
2436 | 2351 | def filterrel(l): |
|
2437 | 2352 | return [f for f in l if f.startswith(relroot)] |
|
2438 | 2353 | modified = filterrel(modified) |
|
2439 | 2354 | added = filterrel(added) |
|
2440 | 2355 | removed = filterrel(removed) |
|
2441 | 2356 | relfiltered = True |
|
2442 | 2357 | # filter out copies where either side isn't inside the relative root |
|
2443 | 2358 | copy = dict(((dst, src) for (dst, src) in copy.iteritems() |
|
2444 | 2359 | if dst.startswith(relroot) |
|
2445 | 2360 | and src.startswith(relroot))) |
|
2446 | 2361 | |
|
2447 | 2362 | modifiedset = set(modified) |
|
2448 | 2363 | addedset = set(added) |
|
2449 | 2364 | removedset = set(removed) |
|
2450 | 2365 | for f in modified: |
|
2451 | 2366 | if f not in ctx1: |
|
2452 | 2367 | # Fix up added, since merged-in additions appear as |
|
2453 | 2368 | # modifications during merges |
|
2454 | 2369 | modifiedset.remove(f) |
|
2455 | 2370 | addedset.add(f) |
|
2456 | 2371 | for f in removed: |
|
2457 | 2372 | if f not in ctx1: |
|
2458 | 2373 | # Merged-in additions that are then removed are reported as removed. |
|
2459 | 2374 | # They are not in ctx1, so We don't want to show them in the diff. |
|
2460 | 2375 | removedset.remove(f) |
|
2461 | 2376 | modified = sorted(modifiedset) |
|
2462 | 2377 | added = sorted(addedset) |
|
2463 | 2378 | removed = sorted(removedset) |
|
2464 | 2379 | for dst, src in list(copy.items()): |
|
2465 | 2380 | if src not in ctx1: |
|
2466 | 2381 | # Files merged in during a merge and then copied/renamed are |
|
2467 | 2382 | # reported as copies. We want to show them in the diff as additions. |
|
2468 | 2383 | del copy[dst] |
|
2469 | 2384 | |
|
2470 | 2385 | prefetchmatch = scmutil.matchfiles( |
|
2471 | 2386 | repo, list(modifiedset | addedset | removedset)) |
|
2472 | 2387 | scmutil.prefetchfiles(repo, [ctx1.rev(), ctx2.rev()], prefetchmatch) |
|
2473 | 2388 | |
|
2474 | 2389 | def difffn(opts, losedata): |
|
2475 | 2390 | return trydiff(repo, revs, ctx1, ctx2, modified, added, removed, |
|
2476 | 2391 | copy, getfilectx, opts, losedata, prefix, relroot) |
|
2477 | 2392 | if opts.upgrade and not opts.git: |
|
2478 | 2393 | try: |
|
2479 | 2394 | def losedata(fn): |
|
2480 | 2395 | if not losedatafn or not losedatafn(fn=fn): |
|
2481 | 2396 | raise GitDiffRequired |
|
2482 | 2397 | # Buffer the whole output until we are sure it can be generated |
|
2483 | 2398 | return list(difffn(opts.copy(git=False), losedata)) |
|
2484 | 2399 | except GitDiffRequired: |
|
2485 | 2400 | return difffn(opts.copy(git=True), None) |
|
2486 | 2401 | else: |
|
2487 | 2402 | return difffn(opts, None) |
|
2488 | 2403 | |
|
2489 | 2404 | def diffsinglehunk(hunklines): |
|
2490 | 2405 | """yield tokens for a list of lines in a single hunk""" |
|
2491 | 2406 | for line in hunklines: |
|
2492 | 2407 | # chomp |
|
2493 | 2408 | chompline = line.rstrip('\n') |
|
2494 | 2409 | # highlight tabs and trailing whitespace |
|
2495 | 2410 | stripline = chompline.rstrip() |
|
2496 | 2411 | if line.startswith('-'): |
|
2497 | 2412 | label = 'diff.deleted' |
|
2498 | 2413 | elif line.startswith('+'): |
|
2499 | 2414 | label = 'diff.inserted' |
|
2500 | 2415 | else: |
|
2501 | 2416 | raise error.ProgrammingError('unexpected hunk line: %s' % line) |
|
2502 | 2417 | for token in tabsplitter.findall(stripline): |
|
2503 | 2418 | if token.startswith('\t'): |
|
2504 | 2419 | yield (token, 'diff.tab') |
|
2505 | 2420 | else: |
|
2506 | 2421 | yield (token, label) |
|
2507 | 2422 | |
|
2508 | 2423 | if chompline != stripline: |
|
2509 | 2424 | yield (chompline[len(stripline):], 'diff.trailingwhitespace') |
|
2510 | 2425 | if chompline != line: |
|
2511 | 2426 | yield (line[len(chompline):], '') |
|
2512 | 2427 | |
|
2513 | 2428 | def diffsinglehunkinline(hunklines): |
|
2514 | 2429 | """yield tokens for a list of lines in a single hunk, with inline colors""" |
|
2515 | 2430 | # prepare deleted, and inserted content |
|
2516 | 2431 | a = '' |
|
2517 | 2432 | b = '' |
|
2518 | 2433 | for line in hunklines: |
|
2519 | 2434 | if line[0] == '-': |
|
2520 | 2435 | a += line[1:] |
|
2521 | 2436 | elif line[0] == '+': |
|
2522 | 2437 | b += line[1:] |
|
2523 | 2438 | else: |
|
2524 | 2439 | raise error.ProgrammingError('unexpected hunk line: %s' % line) |
|
2525 | 2440 | # fast path: if either side is empty, use diffsinglehunk |
|
2526 | 2441 | if not a or not b: |
|
2527 | 2442 | for t in diffsinglehunk(hunklines): |
|
2528 | 2443 | yield t |
|
2529 | 2444 | return |
|
2530 | 2445 | # re-split the content into words |
|
2531 | 2446 | al = wordsplitter.findall(a) |
|
2532 | 2447 | bl = wordsplitter.findall(b) |
|
2533 | 2448 | # re-arrange the words to lines since the diff algorithm is line-based |
|
2534 | 2449 | aln = [s if s == '\n' else s + '\n' for s in al] |
|
2535 | 2450 | bln = [s if s == '\n' else s + '\n' for s in bl] |
|
2536 | 2451 | an = ''.join(aln) |
|
2537 | 2452 | bn = ''.join(bln) |
|
2538 | 2453 | # run the diff algorithm, prepare atokens and btokens |
|
2539 | 2454 | atokens = [] |
|
2540 | 2455 | btokens = [] |
|
2541 | 2456 | blocks = mdiff.allblocks(an, bn, lines1=aln, lines2=bln) |
|
2542 | 2457 | for (a1, a2, b1, b2), btype in blocks: |
|
2543 | 2458 | changed = btype == '!' |
|
2544 | 2459 | for token in mdiff.splitnewlines(''.join(al[a1:a2])): |
|
2545 | 2460 | atokens.append((changed, token)) |
|
2546 | 2461 | for token in mdiff.splitnewlines(''.join(bl[b1:b2])): |
|
2547 | 2462 | btokens.append((changed, token)) |
|
2548 | 2463 | |
|
2549 | 2464 | # yield deleted tokens, then inserted ones |
|
2550 | 2465 | for prefix, label, tokens in [('-', 'diff.deleted', atokens), |
|
2551 | 2466 | ('+', 'diff.inserted', btokens)]: |
|
2552 | 2467 | nextisnewline = True |
|
2553 | 2468 | for changed, token in tokens: |
|
2554 | 2469 | if nextisnewline: |
|
2555 | 2470 | yield (prefix, label) |
|
2556 | 2471 | nextisnewline = False |
|
2557 | 2472 | # special handling line end |
|
2558 | 2473 | isendofline = token.endswith('\n') |
|
2559 | 2474 | if isendofline: |
|
2560 | 2475 | chomp = token[:-1] # chomp |
|
2561 | 2476 | token = chomp.rstrip() # detect spaces at the end |
|
2562 | 2477 | endspaces = chomp[len(token):] |
|
2563 | 2478 | # scan tabs |
|
2564 | 2479 | for maybetab in tabsplitter.findall(token): |
|
2565 | 2480 | if '\t' == maybetab[0]: |
|
2566 | 2481 | currentlabel = 'diff.tab' |
|
2567 | 2482 | else: |
|
2568 | 2483 | if changed: |
|
2569 | 2484 | currentlabel = label + '.changed' |
|
2570 | 2485 | else: |
|
2571 | 2486 | currentlabel = label + '.unchanged' |
|
2572 | 2487 | yield (maybetab, currentlabel) |
|
2573 | 2488 | if isendofline: |
|
2574 | 2489 | if endspaces: |
|
2575 | 2490 | yield (endspaces, 'diff.trailingwhitespace') |
|
2576 | 2491 | yield ('\n', '') |
|
2577 | 2492 | nextisnewline = True |
|
2578 | 2493 | |
|
2579 | 2494 | def difflabel(func, *args, **kw): |
|
2580 | 2495 | '''yields 2-tuples of (output, label) based on the output of func()''' |
|
2581 | 2496 | if kw.get(r'opts') and kw[r'opts'].worddiff: |
|
2582 | 2497 | dodiffhunk = diffsinglehunkinline |
|
2583 | 2498 | else: |
|
2584 | 2499 | dodiffhunk = diffsinglehunk |
|
2585 | 2500 | headprefixes = [('diff', 'diff.diffline'), |
|
2586 | 2501 | ('copy', 'diff.extended'), |
|
2587 | 2502 | ('rename', 'diff.extended'), |
|
2588 | 2503 | ('old', 'diff.extended'), |
|
2589 | 2504 | ('new', 'diff.extended'), |
|
2590 | 2505 | ('deleted', 'diff.extended'), |
|
2591 | 2506 | ('index', 'diff.extended'), |
|
2592 | 2507 | ('similarity', 'diff.extended'), |
|
2593 | 2508 | ('---', 'diff.file_a'), |
|
2594 | 2509 | ('+++', 'diff.file_b')] |
|
2595 | 2510 | textprefixes = [('@', 'diff.hunk'), |
|
2596 | 2511 | # - and + are handled by diffsinglehunk |
|
2597 | 2512 | ] |
|
2598 | 2513 | head = False |
|
2599 | 2514 | |
|
2600 | 2515 | # buffers a hunk, i.e. adjacent "-", "+" lines without other changes. |
|
2601 | 2516 | hunkbuffer = [] |
|
2602 | 2517 | def consumehunkbuffer(): |
|
2603 | 2518 | if hunkbuffer: |
|
2604 | 2519 | for token in dodiffhunk(hunkbuffer): |
|
2605 | 2520 | yield token |
|
2606 | 2521 | hunkbuffer[:] = [] |
|
2607 | 2522 | |
|
2608 | 2523 | for chunk in func(*args, **kw): |
|
2609 | 2524 | lines = chunk.split('\n') |
|
2610 | 2525 | linecount = len(lines) |
|
2611 | 2526 | for i, line in enumerate(lines): |
|
2612 | 2527 | if head: |
|
2613 | 2528 | if line.startswith('@'): |
|
2614 | 2529 | head = False |
|
2615 | 2530 | else: |
|
2616 | 2531 | if line and not line.startswith((' ', '+', '-', '@', '\\')): |
|
2617 | 2532 | head = True |
|
2618 | 2533 | diffline = False |
|
2619 | 2534 | if not head and line and line.startswith(('+', '-')): |
|
2620 | 2535 | diffline = True |
|
2621 | 2536 | |
|
2622 | 2537 | prefixes = textprefixes |
|
2623 | 2538 | if head: |
|
2624 | 2539 | prefixes = headprefixes |
|
2625 | 2540 | if diffline: |
|
2626 | 2541 | # buffered |
|
2627 | 2542 | bufferedline = line |
|
2628 | 2543 | if i + 1 < linecount: |
|
2629 | 2544 | bufferedline += "\n" |
|
2630 | 2545 | hunkbuffer.append(bufferedline) |
|
2631 | 2546 | else: |
|
2632 | 2547 | # unbuffered |
|
2633 | 2548 | for token in consumehunkbuffer(): |
|
2634 | 2549 | yield token |
|
2635 | 2550 | stripline = line.rstrip() |
|
2636 | 2551 | for prefix, label in prefixes: |
|
2637 | 2552 | if stripline.startswith(prefix): |
|
2638 | 2553 | yield (stripline, label) |
|
2639 | 2554 | if line != stripline: |
|
2640 | 2555 | yield (line[len(stripline):], |
|
2641 | 2556 | 'diff.trailingwhitespace') |
|
2642 | 2557 | break |
|
2643 | 2558 | else: |
|
2644 | 2559 | yield (line, '') |
|
2645 | 2560 | if i + 1 < linecount: |
|
2646 | 2561 | yield ('\n', '') |
|
2647 | 2562 | for token in consumehunkbuffer(): |
|
2648 | 2563 | yield token |
|
2649 | 2564 | |
|
2650 | 2565 | def diffui(*args, **kw): |
|
2651 | 2566 | '''like diff(), but yields 2-tuples of (output, label) for ui.write()''' |
|
2652 | 2567 | return difflabel(diff, *args, **kw) |
|
2653 | 2568 | |
|
2654 | 2569 | def _filepairs(modified, added, removed, copy, opts): |
|
2655 | 2570 | '''generates tuples (f1, f2, copyop), where f1 is the name of the file |
|
2656 | 2571 | before and f2 is the the name after. For added files, f1 will be None, |
|
2657 | 2572 | and for removed files, f2 will be None. copyop may be set to None, 'copy' |
|
2658 | 2573 | or 'rename' (the latter two only if opts.git is set).''' |
|
2659 | 2574 | gone = set() |
|
2660 | 2575 | |
|
2661 | 2576 | copyto = dict([(v, k) for k, v in copy.items()]) |
|
2662 | 2577 | |
|
2663 | 2578 | addedset, removedset = set(added), set(removed) |
|
2664 | 2579 | |
|
2665 | 2580 | for f in sorted(modified + added + removed): |
|
2666 | 2581 | copyop = None |
|
2667 | 2582 | f1, f2 = f, f |
|
2668 | 2583 | if f in addedset: |
|
2669 | 2584 | f1 = None |
|
2670 | 2585 | if f in copy: |
|
2671 | 2586 | if opts.git: |
|
2672 | 2587 | f1 = copy[f] |
|
2673 | 2588 | if f1 in removedset and f1 not in gone: |
|
2674 | 2589 | copyop = 'rename' |
|
2675 | 2590 | gone.add(f1) |
|
2676 | 2591 | else: |
|
2677 | 2592 | copyop = 'copy' |
|
2678 | 2593 | elif f in removedset: |
|
2679 | 2594 | f2 = None |
|
2680 | 2595 | if opts.git: |
|
2681 | 2596 | # have we already reported a copy above? |
|
2682 | 2597 | if (f in copyto and copyto[f] in addedset |
|
2683 | 2598 | and copy[copyto[f]] == f): |
|
2684 | 2599 | continue |
|
2685 | 2600 | yield f1, f2, copyop |
|
2686 | 2601 | |
|
2687 | 2602 | def trydiff(repo, revs, ctx1, ctx2, modified, added, removed, |
|
2688 | 2603 | copy, getfilectx, opts, losedatafn, prefix, relroot): |
|
2689 | 2604 | '''given input data, generate a diff and yield it in blocks |
|
2690 | 2605 | |
|
2691 | 2606 | If generating a diff would lose data like flags or binary data and |
|
2692 | 2607 | losedatafn is not None, it will be called. |
|
2693 | 2608 | |
|
2694 | 2609 | relroot is removed and prefix is added to every path in the diff output. |
|
2695 | 2610 | |
|
2696 | 2611 | If relroot is not empty, this function expects every path in modified, |
|
2697 | 2612 | added, removed and copy to start with it.''' |
|
2698 | 2613 | |
|
2699 | 2614 | def gitindex(text): |
|
2700 | 2615 | if not text: |
|
2701 | 2616 | text = "" |
|
2702 | 2617 | l = len(text) |
|
2703 | 2618 | s = hashlib.sha1('blob %d\0' % l) |
|
2704 | 2619 | s.update(text) |
|
2705 | 2620 | return hex(s.digest()) |
|
2706 | 2621 | |
|
2707 | 2622 | if opts.noprefix: |
|
2708 | 2623 | aprefix = bprefix = '' |
|
2709 | 2624 | else: |
|
2710 | 2625 | aprefix = 'a/' |
|
2711 | 2626 | bprefix = 'b/' |
|
2712 | 2627 | |
|
2713 | 2628 | def diffline(f, revs): |
|
2714 | 2629 | revinfo = ' '.join(["-r %s" % rev for rev in revs]) |
|
2715 | 2630 | return 'diff %s %s' % (revinfo, f) |
|
2716 | 2631 | |
|
2717 | 2632 | def isempty(fctx): |
|
2718 | 2633 | return fctx is None or fctx.size() == 0 |
|
2719 | 2634 | |
|
2720 | 2635 | date1 = dateutil.datestr(ctx1.date()) |
|
2721 | 2636 | date2 = dateutil.datestr(ctx2.date()) |
|
2722 | 2637 | |
|
2723 | 2638 | gitmode = {'l': '120000', 'x': '100755', '': '100644'} |
|
2724 | 2639 | |
|
2725 | 2640 | if relroot != '' and (repo.ui.configbool('devel', 'all-warnings') |
|
2726 | 2641 | or repo.ui.configbool('devel', 'check-relroot')): |
|
2727 | 2642 | for f in modified + added + removed + list(copy) + list(copy.values()): |
|
2728 | 2643 | if f is not None and not f.startswith(relroot): |
|
2729 | 2644 | raise AssertionError( |
|
2730 | 2645 | "file %s doesn't start with relroot %s" % (f, relroot)) |
|
2731 | 2646 | |
|
2732 | 2647 | for f1, f2, copyop in _filepairs(modified, added, removed, copy, opts): |
|
2733 | 2648 | content1 = None |
|
2734 | 2649 | content2 = None |
|
2735 | 2650 | fctx1 = None |
|
2736 | 2651 | fctx2 = None |
|
2737 | 2652 | flag1 = None |
|
2738 | 2653 | flag2 = None |
|
2739 | 2654 | if f1: |
|
2740 | 2655 | fctx1 = getfilectx(f1, ctx1) |
|
2741 | 2656 | if opts.git or losedatafn: |
|
2742 | 2657 | flag1 = ctx1.flags(f1) |
|
2743 | 2658 | if f2: |
|
2744 | 2659 | fctx2 = getfilectx(f2, ctx2) |
|
2745 | 2660 | if opts.git or losedatafn: |
|
2746 | 2661 | flag2 = ctx2.flags(f2) |
|
2747 | 2662 | # if binary is True, output "summary" or "base85", but not "text diff" |
|
2748 | 2663 | if opts.text: |
|
2749 | 2664 | binary = False |
|
2750 | 2665 | else: |
|
2751 | 2666 | binary = any(f.isbinary() for f in [fctx1, fctx2] if f is not None) |
|
2752 | 2667 | |
|
2753 | 2668 | if losedatafn and not opts.git: |
|
2754 | 2669 | if (binary or |
|
2755 | 2670 | # copy/rename |
|
2756 | 2671 | f2 in copy or |
|
2757 | 2672 | # empty file creation |
|
2758 | 2673 | (not f1 and isempty(fctx2)) or |
|
2759 | 2674 | # empty file deletion |
|
2760 | 2675 | (isempty(fctx1) and not f2) or |
|
2761 | 2676 | # create with flags |
|
2762 | 2677 | (not f1 and flag2) or |
|
2763 | 2678 | # change flags |
|
2764 | 2679 | (f1 and f2 and flag1 != flag2)): |
|
2765 | 2680 | losedatafn(f2 or f1) |
|
2766 | 2681 | |
|
2767 | 2682 | path1 = f1 or f2 |
|
2768 | 2683 | path2 = f2 or f1 |
|
2769 | 2684 | path1 = posixpath.join(prefix, path1[len(relroot):]) |
|
2770 | 2685 | path2 = posixpath.join(prefix, path2[len(relroot):]) |
|
2771 | 2686 | header = [] |
|
2772 | 2687 | if opts.git: |
|
2773 | 2688 | header.append('diff --git %s%s %s%s' % |
|
2774 | 2689 | (aprefix, path1, bprefix, path2)) |
|
2775 | 2690 | if not f1: # added |
|
2776 | 2691 | header.append('new file mode %s' % gitmode[flag2]) |
|
2777 | 2692 | elif not f2: # removed |
|
2778 | 2693 | header.append('deleted file mode %s' % gitmode[flag1]) |
|
2779 | 2694 | else: # modified/copied/renamed |
|
2780 | 2695 | mode1, mode2 = gitmode[flag1], gitmode[flag2] |
|
2781 | 2696 | if mode1 != mode2: |
|
2782 | 2697 | header.append('old mode %s' % mode1) |
|
2783 | 2698 | header.append('new mode %s' % mode2) |
|
2784 | 2699 | if copyop is not None: |
|
2785 | 2700 | if opts.showsimilarity: |
|
2786 | 2701 | sim = similar.score(ctx1[path1], ctx2[path2]) * 100 |
|
2787 | 2702 | header.append('similarity index %d%%' % sim) |
|
2788 | 2703 | header.append('%s from %s' % (copyop, path1)) |
|
2789 | 2704 | header.append('%s to %s' % (copyop, path2)) |
|
2790 | 2705 | elif revs and not repo.ui.quiet: |
|
2791 | 2706 | header.append(diffline(path1, revs)) |
|
2792 | 2707 | |
|
2793 | 2708 | # fctx.is | diffopts | what to | is fctx.data() |
|
2794 | 2709 | # binary() | text nobinary git index | output? | outputted? |
|
2795 | 2710 | # ------------------------------------|---------------------------- |
|
2796 | 2711 | # yes | no no no * | summary | no |
|
2797 | 2712 | # yes | no no yes * | base85 | yes |
|
2798 | 2713 | # yes | no yes no * | summary | no |
|
2799 | 2714 | # yes | no yes yes 0 | summary | no |
|
2800 | 2715 | # yes | no yes yes >0 | summary | semi [1] |
|
2801 | 2716 | # yes | yes * * * | text diff | yes |
|
2802 | 2717 | # no | * * * * | text diff | yes |
|
2803 | 2718 | # [1]: hash(fctx.data()) is outputted. so fctx.data() cannot be faked |
|
2804 | 2719 | if binary and (not opts.git or (opts.git and opts.nobinary and not |
|
2805 | 2720 | opts.index)): |
|
2806 | 2721 | # fast path: no binary content will be displayed, content1 and |
|
2807 | 2722 | # content2 are only used for equivalent test. cmp() could have a |
|
2808 | 2723 | # fast path. |
|
2809 | 2724 | if fctx1 is not None: |
|
2810 | 2725 | content1 = b'\0' |
|
2811 | 2726 | if fctx2 is not None: |
|
2812 | 2727 | if fctx1 is not None and not fctx1.cmp(fctx2): |
|
2813 | 2728 | content2 = b'\0' # not different |
|
2814 | 2729 | else: |
|
2815 | 2730 | content2 = b'\0\0' |
|
2816 | 2731 | else: |
|
2817 | 2732 | # normal path: load contents |
|
2818 | 2733 | if fctx1 is not None: |
|
2819 | 2734 | content1 = fctx1.data() |
|
2820 | 2735 | if fctx2 is not None: |
|
2821 | 2736 | content2 = fctx2.data() |
|
2822 | 2737 | |
|
2823 | 2738 | if binary and opts.git and not opts.nobinary: |
|
2824 | 2739 | text = mdiff.b85diff(content1, content2) |
|
2825 | 2740 | if text: |
|
2826 | 2741 | header.append('index %s..%s' % |
|
2827 | 2742 | (gitindex(content1), gitindex(content2))) |
|
2828 | 2743 | hunks = (None, [text]), |
|
2829 | 2744 | else: |
|
2830 | 2745 | if opts.git and opts.index > 0: |
|
2831 | 2746 | flag = flag1 |
|
2832 | 2747 | if flag is None: |
|
2833 | 2748 | flag = flag2 |
|
2834 | 2749 | header.append('index %s..%s %s' % |
|
2835 | 2750 | (gitindex(content1)[0:opts.index], |
|
2836 | 2751 | gitindex(content2)[0:opts.index], |
|
2837 | 2752 | gitmode[flag])) |
|
2838 | 2753 | |
|
2839 | 2754 | uheaders, hunks = mdiff.unidiff(content1, date1, |
|
2840 | 2755 | content2, date2, |
|
2841 | 2756 | path1, path2, |
|
2842 | 2757 | binary=binary, opts=opts) |
|
2843 | 2758 | header.extend(uheaders) |
|
2844 | 2759 | yield fctx1, fctx2, header, hunks |
|
2845 | 2760 | |
|
2846 | 2761 | def diffstatsum(stats): |
|
2847 | 2762 | maxfile, maxtotal, addtotal, removetotal, binary = 0, 0, 0, 0, False |
|
2848 | 2763 | for f, a, r, b in stats: |
|
2849 | 2764 | maxfile = max(maxfile, encoding.colwidth(f)) |
|
2850 | 2765 | maxtotal = max(maxtotal, a + r) |
|
2851 | 2766 | addtotal += a |
|
2852 | 2767 | removetotal += r |
|
2853 | 2768 | binary = binary or b |
|
2854 | 2769 | |
|
2855 | 2770 | return maxfile, maxtotal, addtotal, removetotal, binary |
|
2856 | 2771 | |
|
2857 | 2772 | def diffstatdata(lines): |
|
2858 | 2773 | diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$') |
|
2859 | 2774 | |
|
2860 | 2775 | results = [] |
|
2861 | 2776 | filename, adds, removes, isbinary = None, 0, 0, False |
|
2862 | 2777 | |
|
2863 | 2778 | def addresult(): |
|
2864 | 2779 | if filename: |
|
2865 | 2780 | results.append((filename, adds, removes, isbinary)) |
|
2866 | 2781 | |
|
2867 | 2782 | # inheader is used to track if a line is in the |
|
2868 | 2783 | # header portion of the diff. This helps properly account |
|
2869 | 2784 | # for lines that start with '--' or '++' |
|
2870 | 2785 | inheader = False |
|
2871 | 2786 | |
|
2872 | 2787 | for line in lines: |
|
2873 | 2788 | if line.startswith('diff'): |
|
2874 | 2789 | addresult() |
|
2875 | 2790 | # starting a new file diff |
|
2876 | 2791 | # set numbers to 0 and reset inheader |
|
2877 | 2792 | inheader = True |
|
2878 | 2793 | adds, removes, isbinary = 0, 0, False |
|
2879 | 2794 | if line.startswith('diff --git a/'): |
|
2880 | 2795 | filename = gitre.search(line).group(2) |
|
2881 | 2796 | elif line.startswith('diff -r'): |
|
2882 | 2797 | # format: "diff -r ... -r ... filename" |
|
2883 | 2798 | filename = diffre.search(line).group(1) |
|
2884 | 2799 | elif line.startswith('@@'): |
|
2885 | 2800 | inheader = False |
|
2886 | 2801 | elif line.startswith('+') and not inheader: |
|
2887 | 2802 | adds += 1 |
|
2888 | 2803 | elif line.startswith('-') and not inheader: |
|
2889 | 2804 | removes += 1 |
|
2890 | 2805 | elif (line.startswith('GIT binary patch') or |
|
2891 | 2806 | line.startswith('Binary file')): |
|
2892 | 2807 | isbinary = True |
|
2893 | 2808 | addresult() |
|
2894 | 2809 | return results |
|
2895 | 2810 | |
|
2896 | 2811 | def diffstat(lines, width=80): |
|
2897 | 2812 | output = [] |
|
2898 | 2813 | stats = diffstatdata(lines) |
|
2899 | 2814 | maxname, maxtotal, totaladds, totalremoves, hasbinary = diffstatsum(stats) |
|
2900 | 2815 | |
|
2901 | 2816 | countwidth = len(str(maxtotal)) |
|
2902 | 2817 | if hasbinary and countwidth < 3: |
|
2903 | 2818 | countwidth = 3 |
|
2904 | 2819 | graphwidth = width - countwidth - maxname - 6 |
|
2905 | 2820 | if graphwidth < 10: |
|
2906 | 2821 | graphwidth = 10 |
|
2907 | 2822 | |
|
2908 | 2823 | def scale(i): |
|
2909 | 2824 | if maxtotal <= graphwidth: |
|
2910 | 2825 | return i |
|
2911 | 2826 | # If diffstat runs out of room it doesn't print anything, |
|
2912 | 2827 | # which isn't very useful, so always print at least one + or - |
|
2913 | 2828 | # if there were at least some changes. |
|
2914 | 2829 | return max(i * graphwidth // maxtotal, int(bool(i))) |
|
2915 | 2830 | |
|
2916 | 2831 | for filename, adds, removes, isbinary in stats: |
|
2917 | 2832 | if isbinary: |
|
2918 | 2833 | count = 'Bin' |
|
2919 | 2834 | else: |
|
2920 | 2835 | count = '%d' % (adds + removes) |
|
2921 | 2836 | pluses = '+' * scale(adds) |
|
2922 | 2837 | minuses = '-' * scale(removes) |
|
2923 | 2838 | output.append(' %s%s | %*s %s%s\n' % |
|
2924 | 2839 | (filename, ' ' * (maxname - encoding.colwidth(filename)), |
|
2925 | 2840 | countwidth, count, pluses, minuses)) |
|
2926 | 2841 | |
|
2927 | 2842 | if stats: |
|
2928 | 2843 | output.append(_(' %d files changed, %d insertions(+), ' |
|
2929 | 2844 | '%d deletions(-)\n') |
|
2930 | 2845 | % (len(stats), totaladds, totalremoves)) |
|
2931 | 2846 | |
|
2932 | 2847 | return ''.join(output) |
|
2933 | 2848 | |
|
2934 | 2849 | def diffstatui(*args, **kw): |
|
2935 | 2850 | '''like diffstat(), but yields 2-tuples of (output, label) for |
|
2936 | 2851 | ui.write() |
|
2937 | 2852 | ''' |
|
2938 | 2853 | |
|
2939 | 2854 | for line in diffstat(*args, **kw).splitlines(): |
|
2940 | 2855 | if line and line[-1] in '+-': |
|
2941 | 2856 | name, graph = line.rsplit(' ', 1) |
|
2942 | 2857 | yield (name + ' ', '') |
|
2943 | 2858 | m = re.search(br'\++', graph) |
|
2944 | 2859 | if m: |
|
2945 | 2860 | yield (m.group(0), 'diffstat.inserted') |
|
2946 | 2861 | m = re.search(br'-+', graph) |
|
2947 | 2862 | if m: |
|
2948 | 2863 | yield (m.group(0), 'diffstat.deleted') |
|
2949 | 2864 | else: |
|
2950 | 2865 | yield (line, '') |
|
2951 | 2866 | yield ('\n', '') |
This diff has been collapsed as it changes many lines, (2860 lines changed) Show them Hide them | |||
@@ -1,2951 +1,107 | |||
|
1 | # patch.py - patch file parsing routines | |
|
1 | # diffutil.py - utility functions related to diff and patch | |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006 Brendan Cully <brendan@kublai.com> |
|
4 | 4 | # Copyright 2007 Chris Mason <chris.mason@oracle.com> |
|
5 | # Copyright 2018 Octobus <octobus@octobus.net> | |
|
5 | 6 | # |
|
6 | 7 | # This software may be used and distributed according to the terms of the |
|
7 | 8 | # GNU General Public License version 2 or any later version. |
|
8 | 9 | |
|
9 |
from __future__ import absolute_import |
|
|
10 | ||
|
11 | import collections | |
|
12 | import contextlib | |
|
13 | import copy | |
|
14 | import email | |
|
15 | import errno | |
|
16 | import hashlib | |
|
17 | import os | |
|
18 | import posixpath | |
|
19 | import re | |
|
20 | import shutil | |
|
21 | import zlib | |
|
22 | ||
|
23 | from .i18n import _ | |
|
24 | from .node import ( | |
|
25 | hex, | |
|
26 | short, | |
|
27 | ) | |
|
28 | from . import ( | |
|
29 | copies, | |
|
30 | diffhelper, | |
|
31 | encoding, | |
|
32 | error, | |
|
33 | mail, | |
|
34 | mdiff, | |
|
35 | pathutil, | |
|
36 | pycompat, | |
|
37 | scmutil, | |
|
38 | similar, | |
|
39 | util, | |
|
40 | vfs as vfsmod, | |
|
41 | ) | |
|
42 | from .utils import ( | |
|
43 | dateutil, | |
|
44 | procutil, | |
|
45 | stringutil, | |
|
46 | ) | |
|
47 | ||
|
48 | stringio = util.stringio | |
|
49 | ||
|
50 | gitre = re.compile(br'diff --git a/(.*) b/(.*)') | |
|
51 | tabsplitter = re.compile(br'(\t+|[^\t]+)') | |
|
52 | wordsplitter = re.compile(br'(\t+| +|[a-zA-Z0-9_\x80-\xff]+|' | |
|
53 | b'[^ \ta-zA-Z0-9_\x80-\xff])') | |
|
54 | ||
|
55 | PatchError = error.PatchError | |
|
56 | ||
|
57 | # public functions | |
|
58 | ||
|
59 | def split(stream): | |
|
60 | '''return an iterator of individual patches from a stream''' | |
|
61 | def isheader(line, inheader): | |
|
62 | if inheader and line.startswith((' ', '\t')): | |
|
63 | # continuation | |
|
64 | return True | |
|
65 | if line.startswith((' ', '-', '+')): | |
|
66 | # diff line - don't check for header pattern in there | |
|
67 | return False | |
|
68 | l = line.split(': ', 1) | |
|
69 | return len(l) == 2 and ' ' not in l[0] | |
|
70 | ||
|
71 | def chunk(lines): | |
|
72 | return stringio(''.join(lines)) | |
|
73 | ||
|
74 | def hgsplit(stream, cur): | |
|
75 | inheader = True | |
|
76 | ||
|
77 | for line in stream: | |
|
78 | if not line.strip(): | |
|
79 | inheader = False | |
|
80 | if not inheader and line.startswith('# HG changeset patch'): | |
|
81 | yield chunk(cur) | |
|
82 | cur = [] | |
|
83 | inheader = True | |
|
84 | ||
|
85 | cur.append(line) | |
|
86 | ||
|
87 | if cur: | |
|
88 | yield chunk(cur) | |
|
89 | ||
|
90 | def mboxsplit(stream, cur): | |
|
91 | for line in stream: | |
|
92 | if line.startswith('From '): | |
|
93 | for c in split(chunk(cur[1:])): | |
|
94 | yield c | |
|
95 | cur = [] | |
|
96 | ||
|
97 | cur.append(line) | |
|
98 | ||
|
99 | if cur: | |
|
100 | for c in split(chunk(cur[1:])): | |
|
101 | yield c | |
|
102 | ||
|
103 | def mimesplit(stream, cur): | |
|
104 | def msgfp(m): | |
|
105 | fp = stringio() | |
|
106 | g = email.Generator.Generator(fp, mangle_from_=False) | |
|
107 | g.flatten(m) | |
|
108 | fp.seek(0) | |
|
109 | return fp | |
|
110 | ||
|
111 | for line in stream: | |
|
112 | cur.append(line) | |
|
113 | c = chunk(cur) | |
|
114 | ||
|
115 | m = mail.parse(c) | |
|
116 | if not m.is_multipart(): | |
|
117 | yield msgfp(m) | |
|
118 | else: | |
|
119 | ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') | |
|
120 | for part in m.walk(): | |
|
121 | ct = part.get_content_type() | |
|
122 | if ct not in ok_types: | |
|
123 | continue | |
|
124 | yield msgfp(part) | |
|
125 | ||
|
126 | def headersplit(stream, cur): | |
|
127 | inheader = False | |
|
128 | ||
|
129 | for line in stream: | |
|
130 | if not inheader and isheader(line, inheader): | |
|
131 | yield chunk(cur) | |
|
132 | cur = [] | |
|
133 | inheader = True | |
|
134 | if inheader and not isheader(line, inheader): | |
|
135 | inheader = False | |
|
136 | ||
|
137 | cur.append(line) | |
|
138 | ||
|
139 | if cur: | |
|
140 | yield chunk(cur) | |
|
141 | ||
|
142 | def remainder(cur): | |
|
143 | yield chunk(cur) | |
|
144 | ||
|
145 | class fiter(object): | |
|
146 | def __init__(self, fp): | |
|
147 | self.fp = fp | |
|
148 | ||
|
149 | def __iter__(self): | |
|
150 | return self | |
|
151 | ||
|
152 | def next(self): | |
|
153 | l = self.fp.readline() | |
|
154 | if not l: | |
|
155 | raise StopIteration | |
|
156 | return l | |
|
157 | ||
|
158 | __next__ = next | |
|
159 | ||
|
160 | inheader = False | |
|
161 | cur = [] | |
|
162 | ||
|
163 | mimeheaders = ['content-type'] | |
|
164 | ||
|
165 | if not util.safehasattr(stream, 'next'): | |
|
166 | # http responses, for example, have readline but not next | |
|
167 | stream = fiter(stream) | |
|
168 | ||
|
169 | for line in stream: | |
|
170 | cur.append(line) | |
|
171 | if line.startswith('# HG changeset patch'): | |
|
172 | return hgsplit(stream, cur) | |
|
173 | elif line.startswith('From '): | |
|
174 | return mboxsplit(stream, cur) | |
|
175 | elif isheader(line, inheader): | |
|
176 | inheader = True | |
|
177 | if line.split(':', 1)[0].lower() in mimeheaders: | |
|
178 | # let email parser handle this | |
|
179 | return mimesplit(stream, cur) | |
|
180 | elif line.startswith('--- ') and inheader: | |
|
181 | # No evil headers seen by diff start, split by hand | |
|
182 | return headersplit(stream, cur) | |
|
183 | # Not enough info, keep reading | |
|
184 | ||
|
185 | # if we are here, we have a very plain patch | |
|
186 | return remainder(cur) | |
|
187 | ||
|
188 | ## Some facility for extensible patch parsing: | |
|
189 | # list of pairs ("header to match", "data key") | |
|
190 | patchheadermap = [('Date', 'date'), | |
|
191 | ('Branch', 'branch'), | |
|
192 | ('Node ID', 'nodeid'), | |
|
193 | ] | |
|
194 | ||
|
195 | @contextlib.contextmanager | |
|
196 | def extract(ui, fileobj): | |
|
197 | '''extract patch from data read from fileobj. | |
|
198 | ||
|
199 | patch can be a normal patch or contained in an email message. | |
|
200 | ||
|
201 | return a dictionary. Standard keys are: | |
|
202 | - filename, | |
|
203 | - message, | |
|
204 | - user, | |
|
205 | - date, | |
|
206 | - branch, | |
|
207 | - node, | |
|
208 | - p1, | |
|
209 | - p2. | |
|
210 | Any item can be missing from the dictionary. If filename is missing, | |
|
211 | fileobj did not contain a patch. Caller must unlink filename when done.''' | |
|
212 | ||
|
213 | fd, tmpname = pycompat.mkstemp(prefix='hg-patch-') | |
|
214 | tmpfp = os.fdopen(fd, r'wb') | |
|
215 | try: | |
|
216 | yield _extract(ui, fileobj, tmpname, tmpfp) | |
|
217 | finally: | |
|
218 | tmpfp.close() | |
|
219 | os.unlink(tmpname) | |
|
220 | ||
|
221 | def _extract(ui, fileobj, tmpname, tmpfp): | |
|
222 | ||
|
223 | # attempt to detect the start of a patch | |
|
224 | # (this heuristic is borrowed from quilt) | |
|
225 | diffre = re.compile(br'^(?:Index:[ \t]|diff[ \t]-|RCS file: |' | |
|
226 | br'retrieving revision [0-9]+(\.[0-9]+)*$|' | |
|
227 | br'---[ \t].*?^\+\+\+[ \t]|' | |
|
228 | br'\*\*\*[ \t].*?^---[ \t])', | |
|
229 | re.MULTILINE | re.DOTALL) | |
|
230 | ||
|
231 | data = {} | |
|
232 | ||
|
233 | msg = mail.parse(fileobj) | |
|
234 | ||
|
235 | subject = msg[r'Subject'] and mail.headdecode(msg[r'Subject']) | |
|
236 | data['user'] = msg[r'From'] and mail.headdecode(msg[r'From']) | |
|
237 | if not subject and not data['user']: | |
|
238 | # Not an email, restore parsed headers if any | |
|
239 | subject = '\n'.join(': '.join(map(encoding.strtolocal, h)) | |
|
240 | for h in msg.items()) + '\n' | |
|
241 | ||
|
242 | # should try to parse msg['Date'] | |
|
243 | parents = [] | |
|
244 | ||
|
245 | if subject: | |
|
246 | if subject.startswith('[PATCH'): | |
|
247 | pend = subject.find(']') | |
|
248 | if pend >= 0: | |
|
249 | subject = subject[pend + 1:].lstrip() | |
|
250 | subject = re.sub(br'\n[ \t]+', ' ', subject) | |
|
251 | ui.debug('Subject: %s\n' % subject) | |
|
252 | if data['user']: | |
|
253 | ui.debug('From: %s\n' % data['user']) | |
|
254 | diffs_seen = 0 | |
|
255 | ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') | |
|
256 | message = '' | |
|
257 | for part in msg.walk(): | |
|
258 | content_type = pycompat.bytestr(part.get_content_type()) | |
|
259 | ui.debug('Content-Type: %s\n' % content_type) | |
|
260 | if content_type not in ok_types: | |
|
261 | continue | |
|
262 | payload = part.get_payload(decode=True) | |
|
263 | m = diffre.search(payload) | |
|
264 | if m: | |
|
265 | hgpatch = False | |
|
266 | hgpatchheader = False | |
|
267 | ignoretext = False | |
|
268 | ||
|
269 | ui.debug('found patch at byte %d\n' % m.start(0)) | |
|
270 | diffs_seen += 1 | |
|
271 | cfp = stringio() | |
|
272 | for line in payload[:m.start(0)].splitlines(): | |
|
273 | if line.startswith('# HG changeset patch') and not hgpatch: | |
|
274 | ui.debug('patch generated by hg export\n') | |
|
275 | hgpatch = True | |
|
276 | hgpatchheader = True | |
|
277 | # drop earlier commit message content | |
|
278 | cfp.seek(0) | |
|
279 | cfp.truncate() | |
|
280 | subject = None | |
|
281 | elif hgpatchheader: | |
|
282 | if line.startswith('# User '): | |
|
283 | data['user'] = line[7:] | |
|
284 | ui.debug('From: %s\n' % data['user']) | |
|
285 | elif line.startswith("# Parent "): | |
|
286 | parents.append(line[9:].lstrip()) | |
|
287 | elif line.startswith("# "): | |
|
288 | for header, key in patchheadermap: | |
|
289 | prefix = '# %s ' % header | |
|
290 | if line.startswith(prefix): | |
|
291 | data[key] = line[len(prefix):] | |
|
292 | else: | |
|
293 | hgpatchheader = False | |
|
294 | elif line == '---': | |
|
295 | ignoretext = True | |
|
296 | if not hgpatchheader and not ignoretext: | |
|
297 | cfp.write(line) | |
|
298 | cfp.write('\n') | |
|
299 | message = cfp.getvalue() | |
|
300 | if tmpfp: | |
|
301 | tmpfp.write(payload) | |
|
302 | if not payload.endswith('\n'): | |
|
303 | tmpfp.write('\n') | |
|
304 | elif not diffs_seen and message and content_type == 'text/plain': | |
|
305 | message += '\n' + payload | |
|
306 | ||
|
307 | if subject and not message.startswith(subject): | |
|
308 | message = '%s\n%s' % (subject, message) | |
|
309 | data['message'] = message | |
|
310 | tmpfp.close() | |
|
311 | if parents: | |
|
312 | data['p1'] = parents.pop(0) | |
|
313 | if parents: | |
|
314 | data['p2'] = parents.pop(0) | |
|
315 | ||
|
316 | if diffs_seen: | |
|
317 | data['filename'] = tmpname | |
|
318 | ||
|
319 | return data | |
|
320 | ||
|
321 | class patchmeta(object): | |
|
322 | """Patched file metadata | |
|
323 | ||
|
324 | 'op' is the performed operation within ADD, DELETE, RENAME, MODIFY | |
|
325 | or COPY. 'path' is patched file path. 'oldpath' is set to the | |
|
326 | origin file when 'op' is either COPY or RENAME, None otherwise. If | |
|
327 | file mode is changed, 'mode' is a tuple (islink, isexec) where | |
|
328 | 'islink' is True if the file is a symlink and 'isexec' is True if | |
|
329 | the file is executable. Otherwise, 'mode' is None. | |
|
330 | """ | |
|
331 | def __init__(self, path): | |
|
332 | self.path = path | |
|
333 | self.oldpath = None | |
|
334 | self.mode = None | |
|
335 | self.op = 'MODIFY' | |
|
336 | self.binary = False | |
|
337 | ||
|
338 | def setmode(self, mode): | |
|
339 | islink = mode & 0o20000 | |
|
340 | isexec = mode & 0o100 | |
|
341 | self.mode = (islink, isexec) | |
|
342 | ||
|
343 | def copy(self): | |
|
344 | other = patchmeta(self.path) | |
|
345 | other.oldpath = self.oldpath | |
|
346 | other.mode = self.mode | |
|
347 | other.op = self.op | |
|
348 | other.binary = self.binary | |
|
349 | return other | |
|
350 | ||
|
351 | def _ispatchinga(self, afile): | |
|
352 | if afile == '/dev/null': | |
|
353 | return self.op == 'ADD' | |
|
354 | return afile == 'a/' + (self.oldpath or self.path) | |
|
355 | ||
|
356 | def _ispatchingb(self, bfile): | |
|
357 | if bfile == '/dev/null': | |
|
358 | return self.op == 'DELETE' | |
|
359 | return bfile == 'b/' + self.path | |
|
360 | ||
|
361 | def ispatching(self, afile, bfile): | |
|
362 | return self._ispatchinga(afile) and self._ispatchingb(bfile) | |
|
363 | ||
|
364 | def __repr__(self): | |
|
365 | return "<patchmeta %s %r>" % (self.op, self.path) | |
|
366 | ||
|
367 | def readgitpatch(lr): | |
|
368 | """extract git-style metadata about patches from <patchname>""" | |
|
369 | ||
|
370 | # Filter patch for git information | |
|
371 | gp = None | |
|
372 | gitpatches = [] | |
|
373 | for line in lr: | |
|
374 | line = line.rstrip(' \r\n') | |
|
375 | if line.startswith('diff --git a/'): | |
|
376 | m = gitre.match(line) | |
|
377 | if m: | |
|
378 | if gp: | |
|
379 | gitpatches.append(gp) | |
|
380 | dst = m.group(2) | |
|
381 | gp = patchmeta(dst) | |
|
382 | elif gp: | |
|
383 | if line.startswith('--- '): | |
|
384 | gitpatches.append(gp) | |
|
385 | gp = None | |
|
386 | continue | |
|
387 | if line.startswith('rename from '): | |
|
388 | gp.op = 'RENAME' | |
|
389 | gp.oldpath = line[12:] | |
|
390 | elif line.startswith('rename to '): | |
|
391 | gp.path = line[10:] | |
|
392 | elif line.startswith('copy from '): | |
|
393 | gp.op = 'COPY' | |
|
394 | gp.oldpath = line[10:] | |
|
395 | elif line.startswith('copy to '): | |
|
396 | gp.path = line[8:] | |
|
397 | elif line.startswith('deleted file'): | |
|
398 | gp.op = 'DELETE' | |
|
399 | elif line.startswith('new file mode '): | |
|
400 | gp.op = 'ADD' | |
|
401 | gp.setmode(int(line[-6:], 8)) | |
|
402 | elif line.startswith('new mode '): | |
|
403 | gp.setmode(int(line[-6:], 8)) | |
|
404 | elif line.startswith('GIT binary patch'): | |
|
405 | gp.binary = True | |
|
406 | if gp: | |
|
407 | gitpatches.append(gp) | |
|
408 | ||
|
409 | return gitpatches | |
|
410 | ||
|
411 | class linereader(object): | |
|
412 | # simple class to allow pushing lines back into the input stream | |
|
413 | def __init__(self, fp): | |
|
414 | self.fp = fp | |
|
415 | self.buf = [] | |
|
416 | ||
|
417 | def push(self, line): | |
|
418 | if line is not None: | |
|
419 | self.buf.append(line) | |
|
420 | ||
|
421 | def readline(self): | |
|
422 | if self.buf: | |
|
423 | l = self.buf[0] | |
|
424 | del self.buf[0] | |
|
425 | return l | |
|
426 | return self.fp.readline() | |
|
427 | ||
|
428 | def __iter__(self): | |
|
429 | return iter(self.readline, '') | |
|
430 | ||
|
431 | class abstractbackend(object): | |
|
432 | def __init__(self, ui): | |
|
433 | self.ui = ui | |
|
434 | ||
|
435 | def getfile(self, fname): | |
|
436 | """Return target file data and flags as a (data, (islink, | |
|
437 | isexec)) tuple. Data is None if file is missing/deleted. | |
|
438 | """ | |
|
439 | raise NotImplementedError | |
|
440 | ||
|
441 | def setfile(self, fname, data, mode, copysource): | |
|
442 | """Write data to target file fname and set its mode. mode is a | |
|
443 | (islink, isexec) tuple. If data is None, the file content should | |
|
444 | be left unchanged. If the file is modified after being copied, | |
|
445 | copysource is set to the original file name. | |
|
446 | """ | |
|
447 | raise NotImplementedError | |
|
448 | ||
|
449 | def unlink(self, fname): | |
|
450 | """Unlink target file.""" | |
|
451 | raise NotImplementedError | |
|
452 | ||
|
453 | def writerej(self, fname, failed, total, lines): | |
|
454 | """Write rejected lines for fname. total is the number of hunks | |
|
455 | which failed to apply and total the total number of hunks for this | |
|
456 | files. | |
|
457 | """ | |
|
458 | ||
|
459 | def exists(self, fname): | |
|
460 | raise NotImplementedError | |
|
461 | ||
|
462 | def close(self): | |
|
463 | raise NotImplementedError | |
|
464 | ||
|
465 | class fsbackend(abstractbackend): | |
|
466 | def __init__(self, ui, basedir): | |
|
467 | super(fsbackend, self).__init__(ui) | |
|
468 | self.opener = vfsmod.vfs(basedir) | |
|
469 | ||
|
470 | def getfile(self, fname): | |
|
471 | if self.opener.islink(fname): | |
|
472 | return (self.opener.readlink(fname), (True, False)) | |
|
473 | ||
|
474 | isexec = False | |
|
475 | try: | |
|
476 | isexec = self.opener.lstat(fname).st_mode & 0o100 != 0 | |
|
477 | except OSError as e: | |
|
478 | if e.errno != errno.ENOENT: | |
|
479 | raise | |
|
480 | try: | |
|
481 | return (self.opener.read(fname), (False, isexec)) | |
|
482 | except IOError as e: | |
|
483 | if e.errno != errno.ENOENT: | |
|
484 | raise | |
|
485 | return None, None | |
|
486 | ||
|
487 | def setfile(self, fname, data, mode, copysource): | |
|
488 | islink, isexec = mode | |
|
489 | if data is None: | |
|
490 | self.opener.setflags(fname, islink, isexec) | |
|
491 | return | |
|
492 | if islink: | |
|
493 | self.opener.symlink(data, fname) | |
|
494 | else: | |
|
495 | self.opener.write(fname, data) | |
|
496 | if isexec: | |
|
497 | self.opener.setflags(fname, False, True) | |
|
498 | ||
|
499 | def unlink(self, fname): | |
|
500 | rmdir = self.ui.configbool('experimental', 'removeemptydirs') | |
|
501 | self.opener.unlinkpath(fname, ignoremissing=True, rmdir=rmdir) | |
|
502 | ||
|
503 | def writerej(self, fname, failed, total, lines): | |
|
504 | fname = fname + ".rej" | |
|
505 | self.ui.warn( | |
|
506 | _("%d out of %d hunks FAILED -- saving rejects to file %s\n") % | |
|
507 | (failed, total, fname)) | |
|
508 | fp = self.opener(fname, 'w') | |
|
509 | fp.writelines(lines) | |
|
510 | fp.close() | |
|
511 | ||
|
512 | def exists(self, fname): | |
|
513 | return self.opener.lexists(fname) | |
|
514 | ||
|
515 | class workingbackend(fsbackend): | |
|
516 | def __init__(self, ui, repo, similarity): | |
|
517 | super(workingbackend, self).__init__(ui, repo.root) | |
|
518 | self.repo = repo | |
|
519 | self.similarity = similarity | |
|
520 | self.removed = set() | |
|
521 | self.changed = set() | |
|
522 | self.copied = [] | |
|
523 | ||
|
524 | def _checkknown(self, fname): | |
|
525 | if self.repo.dirstate[fname] == '?' and self.exists(fname): | |
|
526 | raise PatchError(_('cannot patch %s: file is not tracked') % fname) | |
|
527 | ||
|
528 | def setfile(self, fname, data, mode, copysource): | |
|
529 | self._checkknown(fname) | |
|
530 | super(workingbackend, self).setfile(fname, data, mode, copysource) | |
|
531 | if copysource is not None: | |
|
532 | self.copied.append((copysource, fname)) | |
|
533 | self.changed.add(fname) | |
|
534 | ||
|
535 | def unlink(self, fname): | |
|
536 | self._checkknown(fname) | |
|
537 | super(workingbackend, self).unlink(fname) | |
|
538 | self.removed.add(fname) | |
|
539 | self.changed.add(fname) | |
|
10 | from __future__ import absolute_import | |
|
540 | 11 | |
|
541 | def close(self): | |
|
542 | wctx = self.repo[None] | |
|
543 | changed = set(self.changed) | |
|
544 | for src, dst in self.copied: | |
|
545 | scmutil.dirstatecopy(self.ui, self.repo, wctx, src, dst) | |
|
546 | if self.removed: | |
|
547 | wctx.forget(sorted(self.removed)) | |
|
548 | for f in self.removed: | |
|
549 | if f not in self.repo.dirstate: | |
|
550 | # File was deleted and no longer belongs to the | |
|
551 | # dirstate, it was probably marked added then | |
|
552 | # deleted, and should not be considered by | |
|
553 | # marktouched(). | |
|
554 | changed.discard(f) | |
|
555 | if changed: | |
|
556 | scmutil.marktouched(self.repo, changed, self.similarity) | |
|
557 | return sorted(self.changed) | |
|
558 | ||
|
559 | class filestore(object): | |
|
560 | def __init__(self, maxsize=None): | |
|
561 | self.opener = None | |
|
562 | self.files = {} | |
|
563 | self.created = 0 | |
|
564 | self.maxsize = maxsize | |
|
565 | if self.maxsize is None: | |
|
566 | self.maxsize = 4*(2**20) | |
|
567 | self.size = 0 | |
|
568 | self.data = {} | |
|
569 | ||
|
570 | def setfile(self, fname, data, mode, copied=None): | |
|
571 | if self.maxsize < 0 or (len(data) + self.size) <= self.maxsize: | |
|
572 | self.data[fname] = (data, mode, copied) | |
|
573 | self.size += len(data) | |
|
574 | else: | |
|
575 | if self.opener is None: | |
|
576 | root = pycompat.mkdtemp(prefix='hg-patch-') | |
|
577 | self.opener = vfsmod.vfs(root) | |
|
578 | # Avoid filename issues with these simple names | |
|
579 | fn = '%d' % self.created | |
|
580 | self.opener.write(fn, data) | |
|
581 | self.created += 1 | |
|
582 | self.files[fname] = (fn, mode, copied) | |
|
583 | ||
|
584 | def getfile(self, fname): | |
|
585 | if fname in self.data: | |
|
586 | return self.data[fname] | |
|
587 | if not self.opener or fname not in self.files: | |
|
588 | return None, None, None | |
|
589 | fn, mode, copied = self.files[fname] | |
|
590 | return self.opener.read(fn), mode, copied | |
|
591 | ||
|
592 | def close(self): | |
|
593 | if self.opener: | |
|
594 | shutil.rmtree(self.opener.base) | |
|
595 | ||
|
596 | class repobackend(abstractbackend): | |
|
597 | def __init__(self, ui, repo, ctx, store): | |
|
598 | super(repobackend, self).__init__(ui) | |
|
599 | self.repo = repo | |
|
600 | self.ctx = ctx | |
|
601 | self.store = store | |
|
602 | self.changed = set() | |
|
603 | self.removed = set() | |
|
604 | self.copied = {} | |
|
605 | ||
|
606 | def _checkknown(self, fname): | |
|
607 | if fname not in self.ctx: | |
|
608 | raise PatchError(_('cannot patch %s: file is not tracked') % fname) | |
|
609 | ||
|
610 | def getfile(self, fname): | |
|
611 | try: | |
|
612 | fctx = self.ctx[fname] | |
|
613 | except error.LookupError: | |
|
614 | return None, None | |
|
615 | flags = fctx.flags() | |
|
616 | return fctx.data(), ('l' in flags, 'x' in flags) | |
|
617 | ||
|
618 | def setfile(self, fname, data, mode, copysource): | |
|
619 | if copysource: | |
|
620 | self._checkknown(copysource) | |
|
621 | if data is None: | |
|
622 | data = self.ctx[fname].data() | |
|
623 | self.store.setfile(fname, data, mode, copysource) | |
|
624 | self.changed.add(fname) | |
|
625 | if copysource: | |
|
626 | self.copied[fname] = copysource | |
|
627 | ||
|
628 | def unlink(self, fname): | |
|
629 | self._checkknown(fname) | |
|
630 | self.removed.add(fname) | |
|
631 | ||
|
632 | def exists(self, fname): | |
|
633 | return fname in self.ctx | |
|
634 | ||
|
635 | def close(self): | |
|
636 | return self.changed | self.removed | |
|
637 | ||
|
638 | # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1 | |
|
639 | unidesc = re.compile('@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@') | |
|
640 | contextdesc = re.compile('(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)') | |
|
641 | eolmodes = ['strict', 'crlf', 'lf', 'auto'] | |
|
642 | ||
|
643 | class patchfile(object): | |
|
644 | def __init__(self, ui, gp, backend, store, eolmode='strict'): | |
|
645 | self.fname = gp.path | |
|
646 | self.eolmode = eolmode | |
|
647 | self.eol = None | |
|
648 | self.backend = backend | |
|
649 | self.ui = ui | |
|
650 | self.lines = [] | |
|
651 | self.exists = False | |
|
652 | self.missing = True | |
|
653 | self.mode = gp.mode | |
|
654 | self.copysource = gp.oldpath | |
|
655 | self.create = gp.op in ('ADD', 'COPY', 'RENAME') | |
|
656 | self.remove = gp.op == 'DELETE' | |
|
657 | if self.copysource is None: | |
|
658 | data, mode = backend.getfile(self.fname) | |
|
659 | else: | |
|
660 | data, mode = store.getfile(self.copysource)[:2] | |
|
661 | if data is not None: | |
|
662 | self.exists = self.copysource is None or backend.exists(self.fname) | |
|
663 | self.missing = False | |
|
664 | if data: | |
|
665 | self.lines = mdiff.splitnewlines(data) | |
|
666 | if self.mode is None: | |
|
667 | self.mode = mode | |
|
668 | if self.lines: | |
|
669 | # Normalize line endings | |
|
670 | if self.lines[0].endswith('\r\n'): | |
|
671 | self.eol = '\r\n' | |
|
672 | elif self.lines[0].endswith('\n'): | |
|
673 | self.eol = '\n' | |
|
674 | if eolmode != 'strict': | |
|
675 | nlines = [] | |
|
676 | for l in self.lines: | |
|
677 | if l.endswith('\r\n'): | |
|
678 | l = l[:-2] + '\n' | |
|
679 | nlines.append(l) | |
|
680 | self.lines = nlines | |
|
681 | else: | |
|
682 | if self.create: | |
|
683 | self.missing = False | |
|
684 | if self.mode is None: | |
|
685 | self.mode = (False, False) | |
|
686 | if self.missing: | |
|
687 | self.ui.warn(_("unable to find '%s' for patching\n") % self.fname) | |
|
688 | self.ui.warn(_("(use '--prefix' to apply patch relative to the " | |
|
689 | "current directory)\n")) | |
|
690 | ||
|
691 | self.hash = {} | |
|
692 | self.dirty = 0 | |
|
693 | self.offset = 0 | |
|
694 | self.skew = 0 | |
|
695 | self.rej = [] | |
|
696 | self.fileprinted = False | |
|
697 | self.printfile(False) | |
|
698 | self.hunks = 0 | |
|
699 | ||
|
700 | def writelines(self, fname, lines, mode): | |
|
701 | if self.eolmode == 'auto': | |
|
702 | eol = self.eol | |
|
703 | elif self.eolmode == 'crlf': | |
|
704 | eol = '\r\n' | |
|
705 | else: | |
|
706 | eol = '\n' | |
|
707 | ||
|
708 | if self.eolmode != 'strict' and eol and eol != '\n': | |
|
709 | rawlines = [] | |
|
710 | for l in lines: | |
|
711 | if l and l.endswith('\n'): | |
|
712 | l = l[:-1] + eol | |
|
713 | rawlines.append(l) | |
|
714 | lines = rawlines | |
|
715 | ||
|
716 | self.backend.setfile(fname, ''.join(lines), mode, self.copysource) | |
|
717 | ||
|
718 | def printfile(self, warn): | |
|
719 | if self.fileprinted: | |
|
720 | return | |
|
721 | if warn or self.ui.verbose: | |
|
722 | self.fileprinted = True | |
|
723 | s = _("patching file %s\n") % self.fname | |
|
724 | if warn: | |
|
725 | self.ui.warn(s) | |
|
726 | else: | |
|
727 | self.ui.note(s) | |
|
728 | ||
|
729 | ||
|
730 | def findlines(self, l, linenum): | |
|
731 | # looks through the hash and finds candidate lines. The | |
|
732 | # result is a list of line numbers sorted based on distance | |
|
733 | # from linenum | |
|
734 | ||
|
735 | cand = self.hash.get(l, []) | |
|
736 | if len(cand) > 1: | |
|
737 | # resort our list of potentials forward then back. | |
|
738 | cand.sort(key=lambda x: abs(x - linenum)) | |
|
739 | return cand | |
|
740 | ||
|
741 | def write_rej(self): | |
|
742 | # our rejects are a little different from patch(1). This always | |
|
743 | # creates rejects in the same form as the original patch. A file | |
|
744 | # header is inserted so that you can run the reject through patch again | |
|
745 | # without having to type the filename. | |
|
746 | if not self.rej: | |
|
747 | return | |
|
748 | base = os.path.basename(self.fname) | |
|
749 | lines = ["--- %s\n+++ %s\n" % (base, base)] | |
|
750 | for x in self.rej: | |
|
751 | for l in x.hunk: | |
|
752 | lines.append(l) | |
|
753 | if l[-1:] != '\n': | |
|
754 | lines.append("\n\ No newline at end of file\n") | |
|
755 | self.backend.writerej(self.fname, len(self.rej), self.hunks, lines) | |
|
756 | ||
|
757 | def apply(self, h): | |
|
758 | if not h.complete(): | |
|
759 | raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") % | |
|
760 | (h.number, h.desc, len(h.a), h.lena, len(h.b), | |
|
761 | h.lenb)) | |
|
762 | ||
|
763 | self.hunks += 1 | |
|
764 | ||
|
765 | if self.missing: | |
|
766 | self.rej.append(h) | |
|
767 | return -1 | |
|
768 | ||
|
769 | if self.exists and self.create: | |
|
770 | if self.copysource: | |
|
771 | self.ui.warn(_("cannot create %s: destination already " | |
|
772 | "exists\n") % self.fname) | |
|
773 | else: | |
|
774 | self.ui.warn(_("file %s already exists\n") % self.fname) | |
|
775 | self.rej.append(h) | |
|
776 | return -1 | |
|
777 | ||
|
778 | if isinstance(h, binhunk): | |
|
779 | if self.remove: | |
|
780 | self.backend.unlink(self.fname) | |
|
781 | else: | |
|
782 | l = h.new(self.lines) | |
|
783 | self.lines[:] = l | |
|
784 | self.offset += len(l) | |
|
785 | self.dirty = True | |
|
786 | return 0 | |
|
787 | ||
|
788 | horig = h | |
|
789 | if (self.eolmode in ('crlf', 'lf') | |
|
790 | or self.eolmode == 'auto' and self.eol): | |
|
791 | # If new eols are going to be normalized, then normalize | |
|
792 | # hunk data before patching. Otherwise, preserve input | |
|
793 | # line-endings. | |
|
794 | h = h.getnormalized() | |
|
795 | ||
|
796 | # fast case first, no offsets, no fuzz | |
|
797 | old, oldstart, new, newstart = h.fuzzit(0, False) | |
|
798 | oldstart += self.offset | |
|
799 | orig_start = oldstart | |
|
800 | # if there's skew we want to emit the "(offset %d lines)" even | |
|
801 | # when the hunk cleanly applies at start + skew, so skip the | |
|
802 | # fast case code | |
|
803 | if self.skew == 0 and diffhelper.testhunk(old, self.lines, oldstart): | |
|
804 | if self.remove: | |
|
805 | self.backend.unlink(self.fname) | |
|
806 | else: | |
|
807 | self.lines[oldstart:oldstart + len(old)] = new | |
|
808 | self.offset += len(new) - len(old) | |
|
809 | self.dirty = True | |
|
810 | return 0 | |
|
811 | ||
|
812 | # ok, we couldn't match the hunk. Lets look for offsets and fuzz it | |
|
813 | self.hash = {} | |
|
814 | for x, s in enumerate(self.lines): | |
|
815 | self.hash.setdefault(s, []).append(x) | |
|
816 | ||
|
817 | for fuzzlen in xrange(self.ui.configint("patch", "fuzz") + 1): | |
|
818 | for toponly in [True, False]: | |
|
819 | old, oldstart, new, newstart = h.fuzzit(fuzzlen, toponly) | |
|
820 | oldstart = oldstart + self.offset + self.skew | |
|
821 | oldstart = min(oldstart, len(self.lines)) | |
|
822 | if old: | |
|
823 | cand = self.findlines(old[0][1:], oldstart) | |
|
824 | else: | |
|
825 | # Only adding lines with no or fuzzed context, just | |
|
826 | # take the skew in account | |
|
827 | cand = [oldstart] | |
|
828 | ||
|
829 | for l in cand: | |
|
830 | if not old or diffhelper.testhunk(old, self.lines, l): | |
|
831 | self.lines[l : l + len(old)] = new | |
|
832 | self.offset += len(new) - len(old) | |
|
833 | self.skew = l - orig_start | |
|
834 | self.dirty = True | |
|
835 | offset = l - orig_start - fuzzlen | |
|
836 | if fuzzlen: | |
|
837 | msg = _("Hunk #%d succeeded at %d " | |
|
838 | "with fuzz %d " | |
|
839 | "(offset %d lines).\n") | |
|
840 | self.printfile(True) | |
|
841 | self.ui.warn(msg % | |
|
842 | (h.number, l + 1, fuzzlen, offset)) | |
|
843 | else: | |
|
844 | msg = _("Hunk #%d succeeded at %d " | |
|
845 | "(offset %d lines).\n") | |
|
846 | self.ui.note(msg % (h.number, l + 1, offset)) | |
|
847 | return fuzzlen | |
|
848 | self.printfile(True) | |
|
849 | self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start)) | |
|
850 | self.rej.append(horig) | |
|
851 | return -1 | |
|
852 | ||
|
853 | def close(self): | |
|
854 | if self.dirty: | |
|
855 | self.writelines(self.fname, self.lines, self.mode) | |
|
856 | self.write_rej() | |
|
857 | return len(self.rej) | |
|
858 | ||
|
859 | class header(object): | |
|
860 | """patch header | |
|
861 | """ | |
|
862 | diffgit_re = re.compile('diff --git a/(.*) b/(.*)$') | |
|
863 | diff_re = re.compile('diff -r .* (.*)$') | |
|
864 | allhunks_re = re.compile('(?:index|deleted file) ') | |
|
865 | pretty_re = re.compile('(?:new file|deleted file) ') | |
|
866 | special_re = re.compile('(?:index|deleted|copy|rename) ') | |
|
867 | newfile_re = re.compile('(?:new file)') | |
|
868 | ||
|
869 | def __init__(self, header): | |
|
870 | self.header = header | |
|
871 | self.hunks = [] | |
|
872 | ||
|
873 | def binary(self): | |
|
874 | return any(h.startswith('index ') for h in self.header) | |
|
875 | ||
|
876 | def pretty(self, fp): | |
|
877 | for h in self.header: | |
|
878 | if h.startswith('index '): | |
|
879 | fp.write(_('this modifies a binary file (all or nothing)\n')) | |
|
880 | break | |
|
881 | if self.pretty_re.match(h): | |
|
882 | fp.write(h) | |
|
883 | if self.binary(): | |
|
884 | fp.write(_('this is a binary file\n')) | |
|
885 | break | |
|
886 | if h.startswith('---'): | |
|
887 | fp.write(_('%d hunks, %d lines changed\n') % | |
|
888 | (len(self.hunks), | |
|
889 | sum([max(h.added, h.removed) for h in self.hunks]))) | |
|
890 | break | |
|
891 | fp.write(h) | |
|
892 | ||
|
893 | def write(self, fp): | |
|
894 | fp.write(''.join(self.header)) | |
|
895 | ||
|
896 | def allhunks(self): | |
|
897 | return any(self.allhunks_re.match(h) for h in self.header) | |
|
898 | ||
|
899 | def files(self): | |
|
900 | match = self.diffgit_re.match(self.header[0]) | |
|
901 | if match: | |
|
902 | fromfile, tofile = match.groups() | |
|
903 | if fromfile == tofile: | |
|
904 | return [fromfile] | |
|
905 | return [fromfile, tofile] | |
|
906 | else: | |
|
907 | return self.diff_re.match(self.header[0]).groups() | |
|
908 | ||
|
909 | def filename(self): | |
|
910 | return self.files()[-1] | |
|
911 | ||
|
912 | def __repr__(self): | |
|
913 | return '<header %s>' % (' '.join(map(repr, self.files()))) | |
|
914 | ||
|
915 | def isnewfile(self): | |
|
916 | return any(self.newfile_re.match(h) for h in self.header) | |
|
917 | ||
|
918 | def special(self): | |
|
919 | # Special files are shown only at the header level and not at the hunk | |
|
920 | # level for example a file that has been deleted is a special file. | |
|
921 | # The user cannot change the content of the operation, in the case of | |
|
922 | # the deleted file he has to take the deletion or not take it, he | |
|
923 | # cannot take some of it. | |
|
924 | # Newly added files are special if they are empty, they are not special | |
|
925 | # if they have some content as we want to be able to change it | |
|
926 | nocontent = len(self.header) == 2 | |
|
927 | emptynewfile = self.isnewfile() and nocontent | |
|
928 | return emptynewfile or \ | |
|
929 | any(self.special_re.match(h) for h in self.header) | |
|
930 | ||
|
931 | class recordhunk(object): | |
|
932 | """patch hunk | |
|
933 | ||
|
934 | XXX shouldn't we merge this with the other hunk class? | |
|
935 | """ | |
|
936 | ||
|
937 | def __init__(self, header, fromline, toline, proc, before, hunk, after, | |
|
938 | maxcontext=None): | |
|
939 | def trimcontext(lines, reverse=False): | |
|
940 | if maxcontext is not None: | |
|
941 | delta = len(lines) - maxcontext | |
|
942 | if delta > 0: | |
|
943 | if reverse: | |
|
944 | return delta, lines[delta:] | |
|
945 | else: | |
|
946 | return delta, lines[:maxcontext] | |
|
947 | return 0, lines | |
|
948 | ||
|
949 | self.header = header | |
|
950 | trimedbefore, self.before = trimcontext(before, True) | |
|
951 | self.fromline = fromline + trimedbefore | |
|
952 | self.toline = toline + trimedbefore | |
|
953 | _trimedafter, self.after = trimcontext(after, False) | |
|
954 | self.proc = proc | |
|
955 | self.hunk = hunk | |
|
956 | self.added, self.removed = self.countchanges(self.hunk) | |
|
957 | ||
|
958 | def __eq__(self, v): | |
|
959 | if not isinstance(v, recordhunk): | |
|
960 | return False | |
|
961 | ||
|
962 | return ((v.hunk == self.hunk) and | |
|
963 | (v.proc == self.proc) and | |
|
964 | (self.fromline == v.fromline) and | |
|
965 | (self.header.files() == v.header.files())) | |
|
966 | ||
|
967 | def __hash__(self): | |
|
968 | return hash((tuple(self.hunk), | |
|
969 | tuple(self.header.files()), | |
|
970 | self.fromline, | |
|
971 | self.proc)) | |
|
972 | ||
|
973 | def countchanges(self, hunk): | |
|
974 | """hunk -> (n+,n-)""" | |
|
975 | add = len([h for h in hunk if h.startswith('+')]) | |
|
976 | rem = len([h for h in hunk if h.startswith('-')]) | |
|
977 | return add, rem | |
|
978 | ||
|
979 | def reversehunk(self): | |
|
980 | """return another recordhunk which is the reverse of the hunk | |
|
981 | ||
|
982 | If this hunk is diff(A, B), the returned hunk is diff(B, A). To do | |
|
983 | that, swap fromline/toline and +/- signs while keep other things | |
|
984 | unchanged. | |
|
985 | """ | |
|
986 | m = {'+': '-', '-': '+', '\\': '\\'} | |
|
987 | hunk = ['%s%s' % (m[l[0:1]], l[1:]) for l in self.hunk] | |
|
988 | return recordhunk(self.header, self.toline, self.fromline, self.proc, | |
|
989 | self.before, hunk, self.after) | |
|
990 | ||
|
991 | def write(self, fp): | |
|
992 | delta = len(self.before) + len(self.after) | |
|
993 | if self.after and self.after[-1] == '\\ No newline at end of file\n': | |
|
994 | delta -= 1 | |
|
995 | fromlen = delta + self.removed | |
|
996 | tolen = delta + self.added | |
|
997 | fp.write('@@ -%d,%d +%d,%d @@%s\n' % | |
|
998 | (self.fromline, fromlen, self.toline, tolen, | |
|
999 | self.proc and (' ' + self.proc))) | |
|
1000 | fp.write(''.join(self.before + self.hunk + self.after)) | |
|
1001 | ||
|
1002 | pretty = write | |
|
1003 | ||
|
1004 | def filename(self): | |
|
1005 | return self.header.filename() | |
|
1006 | ||
|
1007 | def __repr__(self): | |
|
1008 | return '<hunk %r@%d>' % (self.filename(), self.fromline) | |
|
1009 | ||
|
1010 | def getmessages(): | |
|
1011 | return { | |
|
1012 | 'multiple': { | |
|
1013 | 'apply': _("apply change %d/%d to '%s'?"), | |
|
1014 | 'discard': _("discard change %d/%d to '%s'?"), | |
|
1015 | 'record': _("record change %d/%d to '%s'?"), | |
|
1016 | }, | |
|
1017 | 'single': { | |
|
1018 | 'apply': _("apply this change to '%s'?"), | |
|
1019 | 'discard': _("discard this change to '%s'?"), | |
|
1020 | 'record': _("record this change to '%s'?"), | |
|
1021 | }, | |
|
1022 | 'help': { | |
|
1023 | 'apply': _('[Ynesfdaq?]' | |
|
1024 | '$$ &Yes, apply this change' | |
|
1025 | '$$ &No, skip this change' | |
|
1026 | '$$ &Edit this change manually' | |
|
1027 | '$$ &Skip remaining changes to this file' | |
|
1028 | '$$ Apply remaining changes to this &file' | |
|
1029 | '$$ &Done, skip remaining changes and files' | |
|
1030 | '$$ Apply &all changes to all remaining files' | |
|
1031 | '$$ &Quit, applying no changes' | |
|
1032 | '$$ &? (display help)'), | |
|
1033 | 'discard': _('[Ynesfdaq?]' | |
|
1034 | '$$ &Yes, discard this change' | |
|
1035 | '$$ &No, skip this change' | |
|
1036 | '$$ &Edit this change manually' | |
|
1037 | '$$ &Skip remaining changes to this file' | |
|
1038 | '$$ Discard remaining changes to this &file' | |
|
1039 | '$$ &Done, skip remaining changes and files' | |
|
1040 | '$$ Discard &all changes to all remaining files' | |
|
1041 | '$$ &Quit, discarding no changes' | |
|
1042 | '$$ &? (display help)'), | |
|
1043 | 'record': _('[Ynesfdaq?]' | |
|
1044 | '$$ &Yes, record this change' | |
|
1045 | '$$ &No, skip this change' | |
|
1046 | '$$ &Edit this change manually' | |
|
1047 | '$$ &Skip remaining changes to this file' | |
|
1048 | '$$ Record remaining changes to this &file' | |
|
1049 | '$$ &Done, skip remaining changes and files' | |
|
1050 | '$$ Record &all changes to all remaining files' | |
|
1051 | '$$ &Quit, recording no changes' | |
|
1052 | '$$ &? (display help)'), | |
|
1053 | } | |
|
1054 | } | |
|
1055 | ||
|
1056 | def filterpatch(ui, headers, operation=None): | |
|
1057 | """Interactively filter patch chunks into applied-only chunks""" | |
|
1058 | messages = getmessages() | |
|
1059 | ||
|
1060 | if operation is None: | |
|
1061 | operation = 'record' | |
|
1062 | ||
|
1063 | def prompt(skipfile, skipall, query, chunk): | |
|
1064 | """prompt query, and process base inputs | |
|
1065 | ||
|
1066 | - y/n for the rest of file | |
|
1067 | - y/n for the rest | |
|
1068 | - ? (help) | |
|
1069 | - q (quit) | |
|
1070 | ||
|
1071 | Return True/False and possibly updated skipfile and skipall. | |
|
1072 | """ | |
|
1073 | newpatches = None | |
|
1074 | if skipall is not None: | |
|
1075 | return skipall, skipfile, skipall, newpatches | |
|
1076 | if skipfile is not None: | |
|
1077 | return skipfile, skipfile, skipall, newpatches | |
|
1078 | while True: | |
|
1079 | resps = messages['help'][operation] | |
|
1080 | r = ui.promptchoice("%s %s" % (query, resps)) | |
|
1081 | ui.write("\n") | |
|
1082 | if r == 8: # ? | |
|
1083 | for c, t in ui.extractchoices(resps)[1]: | |
|
1084 | ui.write('%s - %s\n' % (c, encoding.lower(t))) | |
|
1085 | continue | |
|
1086 | elif r == 0: # yes | |
|
1087 | ret = True | |
|
1088 | elif r == 1: # no | |
|
1089 | ret = False | |
|
1090 | elif r == 2: # Edit patch | |
|
1091 | if chunk is None: | |
|
1092 | ui.write(_('cannot edit patch for whole file')) | |
|
1093 | ui.write("\n") | |
|
1094 | continue | |
|
1095 | if chunk.header.binary(): | |
|
1096 | ui.write(_('cannot edit patch for binary file')) | |
|
1097 | ui.write("\n") | |
|
1098 | continue | |
|
1099 | # Patch comment based on the Git one (based on comment at end of | |
|
1100 | # https://mercurial-scm.org/wiki/RecordExtension) | |
|
1101 | phelp = '---' + _(""" | |
|
1102 | To remove '-' lines, make them ' ' lines (context). | |
|
1103 | To remove '+' lines, delete them. | |
|
1104 | Lines starting with # will be removed from the patch. | |
|
12 | from ..i18n import _ | |
|
1105 | 13 | |
|
1106 | If the patch applies cleanly, the edited hunk will immediately be | |
|
1107 | added to the record list. If it does not apply cleanly, a rejects | |
|
1108 | file will be generated: you can use that when you try again. If | |
|
1109 | all lines of the hunk are removed, then the edit is aborted and | |
|
1110 | the hunk is left unchanged. | |
|
1111 | """) | |
|
1112 | (patchfd, patchfn) = pycompat.mkstemp(prefix="hg-editor-", | |
|
1113 | suffix=".diff") | |
|
1114 | ncpatchfp = None | |
|
1115 | try: | |
|
1116 | # Write the initial patch | |
|
1117 | f = util.nativeeolwriter(os.fdopen(patchfd, r'wb')) | |
|
1118 | chunk.header.write(f) | |
|
1119 | chunk.write(f) | |
|
1120 | f.write('\n'.join(['# ' + i for i in phelp.splitlines()])) | |
|
1121 | f.close() | |
|
1122 | # Start the editor and wait for it to complete | |
|
1123 | editor = ui.geteditor() | |
|
1124 | ret = ui.system("%s \"%s\"" % (editor, patchfn), | |
|
1125 | environ={'HGUSER': ui.username()}, | |
|
1126 | blockedtag='filterpatch') | |
|
1127 | if ret != 0: | |
|
1128 | ui.warn(_("editor exited with exit code %d\n") % ret) | |
|
1129 | continue | |
|
1130 | # Remove comment lines | |
|
1131 | patchfp = open(patchfn, r'rb') | |
|
1132 | ncpatchfp = stringio() | |
|
1133 | for line in util.iterfile(patchfp): | |
|
1134 | line = util.fromnativeeol(line) | |
|
1135 | if not line.startswith('#'): | |
|
1136 | ncpatchfp.write(line) | |
|
1137 | patchfp.close() | |
|
1138 | ncpatchfp.seek(0) | |
|
1139 | newpatches = parsepatch(ncpatchfp) | |
|
1140 | finally: | |
|
1141 | os.unlink(patchfn) | |
|
1142 | del ncpatchfp | |
|
1143 | # Signal that the chunk shouldn't be applied as-is, but | |
|
1144 | # provide the new patch to be used instead. | |
|
1145 | ret = False | |
|
1146 | elif r == 3: # Skip | |
|
1147 | ret = skipfile = False | |
|
1148 | elif r == 4: # file (Record remaining) | |
|
1149 | ret = skipfile = True | |
|
1150 | elif r == 5: # done, skip remaining | |
|
1151 | ret = skipall = False | |
|
1152 | elif r == 6: # all | |
|
1153 | ret = skipall = True | |
|
1154 | elif r == 7: # quit | |
|
1155 | raise error.Abort(_('user quit')) | |
|
1156 | return ret, skipfile, skipall, newpatches | |
|
1157 | ||
|
1158 | seen = set() | |
|
1159 | applied = {} # 'filename' -> [] of chunks | |
|
1160 | skipfile, skipall = None, None | |
|
1161 | pos, total = 1, sum(len(h.hunks) for h in headers) | |
|
1162 | for h in headers: | |
|
1163 | pos += len(h.hunks) | |
|
1164 | skipfile = None | |
|
1165 | fixoffset = 0 | |
|
1166 | hdr = ''.join(h.header) | |
|
1167 | if hdr in seen: | |
|
1168 | continue | |
|
1169 | seen.add(hdr) | |
|
1170 | if skipall is None: | |
|
1171 | h.pretty(ui) | |
|
1172 | msg = (_('examine changes to %s?') % | |
|
1173 | _(' and ').join("'%s'" % f for f in h.files())) | |
|
1174 | r, skipfile, skipall, np = prompt(skipfile, skipall, msg, None) | |
|
1175 | if not r: | |
|
1176 | continue | |
|
1177 | applied[h.filename()] = [h] | |
|
1178 | if h.allhunks(): | |
|
1179 | applied[h.filename()] += h.hunks | |
|
1180 | continue | |
|
1181 | for i, chunk in enumerate(h.hunks): | |
|
1182 | if skipfile is None and skipall is None: | |
|
1183 | chunk.pretty(ui) | |
|
1184 | if total == 1: | |
|
1185 | msg = messages['single'][operation] % chunk.filename() | |
|
1186 | else: | |
|
1187 | idx = pos - len(h.hunks) + i | |
|
1188 | msg = messages['multiple'][operation] % (idx, total, | |
|
1189 | chunk.filename()) | |
|
1190 | r, skipfile, skipall, newpatches = prompt(skipfile, | |
|
1191 | skipall, msg, chunk) | |
|
1192 | if r: | |
|
1193 | if fixoffset: | |
|
1194 | chunk = copy.copy(chunk) | |
|
1195 | chunk.toline += fixoffset | |
|
1196 | applied[chunk.filename()].append(chunk) | |
|
1197 | elif newpatches is not None: | |
|
1198 | for newpatch in newpatches: | |
|
1199 | for newhunk in newpatch.hunks: | |
|
1200 | if fixoffset: | |
|
1201 | newhunk.toline += fixoffset | |
|
1202 | applied[newhunk.filename()].append(newhunk) | |
|
1203 | else: | |
|
1204 | fixoffset += chunk.removed - chunk.added | |
|
1205 | return (sum([h for h in applied.itervalues() | |
|
1206 | if h[0].special() or len(h) > 1], []), {}) | |
|
1207 | class hunk(object): | |
|
1208 | def __init__(self, desc, num, lr, context): | |
|
1209 | self.number = num | |
|
1210 | self.desc = desc | |
|
1211 | self.hunk = [desc] | |
|
1212 | self.a = [] | |
|
1213 | self.b = [] | |
|
1214 | self.starta = self.lena = None | |
|
1215 | self.startb = self.lenb = None | |
|
1216 | if lr is not None: | |
|
1217 | if context: | |
|
1218 | self.read_context_hunk(lr) | |
|
1219 | else: | |
|
1220 | self.read_unified_hunk(lr) | |
|
1221 | ||
|
1222 | def getnormalized(self): | |
|
1223 | """Return a copy with line endings normalized to LF.""" | |
|
1224 | ||
|
1225 | def normalize(lines): | |
|
1226 | nlines = [] | |
|
1227 | for line in lines: | |
|
1228 | if line.endswith('\r\n'): | |
|
1229 | line = line[:-2] + '\n' | |
|
1230 | nlines.append(line) | |
|
1231 | return nlines | |
|
1232 | ||
|
1233 | # Dummy object, it is rebuilt manually | |
|
1234 | nh = hunk(self.desc, self.number, None, None) | |
|
1235 | nh.number = self.number | |
|
1236 | nh.desc = self.desc | |
|
1237 | nh.hunk = self.hunk | |
|
1238 | nh.a = normalize(self.a) | |
|
1239 | nh.b = normalize(self.b) | |
|
1240 | nh.starta = self.starta | |
|
1241 | nh.startb = self.startb | |
|
1242 | nh.lena = self.lena | |
|
1243 | nh.lenb = self.lenb | |
|
1244 | return nh | |
|
1245 | ||
|
1246 | def read_unified_hunk(self, lr): | |
|
1247 | m = unidesc.match(self.desc) | |
|
1248 | if not m: | |
|
1249 | raise PatchError(_("bad hunk #%d") % self.number) | |
|
1250 | self.starta, self.lena, self.startb, self.lenb = m.groups() | |
|
1251 | if self.lena is None: | |
|
1252 | self.lena = 1 | |
|
1253 | else: | |
|
1254 | self.lena = int(self.lena) | |
|
1255 | if self.lenb is None: | |
|
1256 | self.lenb = 1 | |
|
1257 | else: | |
|
1258 | self.lenb = int(self.lenb) | |
|
1259 | self.starta = int(self.starta) | |
|
1260 | self.startb = int(self.startb) | |
|
1261 | try: | |
|
1262 | diffhelper.addlines(lr, self.hunk, self.lena, self.lenb, | |
|
1263 | self.a, self.b) | |
|
1264 | except error.ParseError as e: | |
|
1265 | raise PatchError(_("bad hunk #%d: %s") % (self.number, e)) | |
|
1266 | # if we hit eof before finishing out the hunk, the last line will | |
|
1267 | # be zero length. Lets try to fix it up. | |
|
1268 | while len(self.hunk[-1]) == 0: | |
|
1269 | del self.hunk[-1] | |
|
1270 | del self.a[-1] | |
|
1271 | del self.b[-1] | |
|
1272 | self.lena -= 1 | |
|
1273 | self.lenb -= 1 | |
|
1274 | self._fixnewline(lr) | |
|
1275 | ||
|
1276 | def read_context_hunk(self, lr): | |
|
1277 | self.desc = lr.readline() | |
|
1278 | m = contextdesc.match(self.desc) | |
|
1279 | if not m: | |
|
1280 | raise PatchError(_("bad hunk #%d") % self.number) | |
|
1281 | self.starta, aend = m.groups() | |
|
1282 | self.starta = int(self.starta) | |
|
1283 | if aend is None: | |
|
1284 | aend = self.starta | |
|
1285 | self.lena = int(aend) - self.starta | |
|
1286 | if self.starta: | |
|
1287 | self.lena += 1 | |
|
1288 | for x in xrange(self.lena): | |
|
1289 | l = lr.readline() | |
|
1290 | if l.startswith('---'): | |
|
1291 | # lines addition, old block is empty | |
|
1292 | lr.push(l) | |
|
1293 | break | |
|
1294 | s = l[2:] | |
|
1295 | if l.startswith('- ') or l.startswith('! '): | |
|
1296 | u = '-' + s | |
|
1297 | elif l.startswith(' '): | |
|
1298 | u = ' ' + s | |
|
1299 | else: | |
|
1300 | raise PatchError(_("bad hunk #%d old text line %d") % | |
|
1301 | (self.number, x)) | |
|
1302 | self.a.append(u) | |
|
1303 | self.hunk.append(u) | |
|
1304 | ||
|
1305 | l = lr.readline() | |
|
1306 | if l.startswith('\ '): | |
|
1307 | s = self.a[-1][:-1] | |
|
1308 | self.a[-1] = s | |
|
1309 | self.hunk[-1] = s | |
|
1310 | l = lr.readline() | |
|
1311 | m = contextdesc.match(l) | |
|
1312 | if not m: | |
|
1313 | raise PatchError(_("bad hunk #%d") % self.number) | |
|
1314 | self.startb, bend = m.groups() | |
|
1315 | self.startb = int(self.startb) | |
|
1316 | if bend is None: | |
|
1317 | bend = self.startb | |
|
1318 | self.lenb = int(bend) - self.startb | |
|
1319 | if self.startb: | |
|
1320 | self.lenb += 1 | |
|
1321 | hunki = 1 | |
|
1322 | for x in xrange(self.lenb): | |
|
1323 | l = lr.readline() | |
|
1324 | if l.startswith('\ '): | |
|
1325 | # XXX: the only way to hit this is with an invalid line range. | |
|
1326 | # The no-eol marker is not counted in the line range, but I | |
|
1327 | # guess there are diff(1) out there which behave differently. | |
|
1328 | s = self.b[-1][:-1] | |
|
1329 | self.b[-1] = s | |
|
1330 | self.hunk[hunki - 1] = s | |
|
1331 | continue | |
|
1332 | if not l: | |
|
1333 | # line deletions, new block is empty and we hit EOF | |
|
1334 | lr.push(l) | |
|
1335 | break | |
|
1336 | s = l[2:] | |
|
1337 | if l.startswith('+ ') or l.startswith('! '): | |
|
1338 | u = '+' + s | |
|
1339 | elif l.startswith(' '): | |
|
1340 | u = ' ' + s | |
|
1341 | elif len(self.b) == 0: | |
|
1342 | # line deletions, new block is empty | |
|
1343 | lr.push(l) | |
|
1344 | break | |
|
1345 | else: | |
|
1346 | raise PatchError(_("bad hunk #%d old text line %d") % | |
|
1347 | (self.number, x)) | |
|
1348 | self.b.append(s) | |
|
1349 | while True: | |
|
1350 | if hunki >= len(self.hunk): | |
|
1351 | h = "" | |
|
1352 | else: | |
|
1353 | h = self.hunk[hunki] | |
|
1354 | hunki += 1 | |
|
1355 | if h == u: | |
|
1356 | break | |
|
1357 | elif h.startswith('-'): | |
|
1358 | continue | |
|
1359 | else: | |
|
1360 | self.hunk.insert(hunki - 1, u) | |
|
1361 | break | |
|
1362 | ||
|
1363 | if not self.a: | |
|
1364 | # this happens when lines were only added to the hunk | |
|
1365 | for x in self.hunk: | |
|
1366 | if x.startswith('-') or x.startswith(' '): | |
|
1367 | self.a.append(x) | |
|
1368 | if not self.b: | |
|
1369 | # this happens when lines were only deleted from the hunk | |
|
1370 | for x in self.hunk: | |
|
1371 | if x.startswith('+') or x.startswith(' '): | |
|
1372 | self.b.append(x[1:]) | |
|
1373 | # @@ -start,len +start,len @@ | |
|
1374 | self.desc = "@@ -%d,%d +%d,%d @@\n" % (self.starta, self.lena, | |
|
1375 | self.startb, self.lenb) | |
|
1376 | self.hunk[0] = self.desc | |
|
1377 | self._fixnewline(lr) | |
|
1378 | ||
|
1379 | def _fixnewline(self, lr): | |
|
1380 | l = lr.readline() | |
|
1381 | if l.startswith('\ '): | |
|
1382 | diffhelper.fixnewline(self.hunk, self.a, self.b) | |
|
1383 | else: | |
|
1384 | lr.push(l) | |
|
1385 | ||
|
1386 | def complete(self): | |
|
1387 | return len(self.a) == self.lena and len(self.b) == self.lenb | |
|
1388 | ||
|
1389 | def _fuzzit(self, old, new, fuzz, toponly): | |
|
1390 | # this removes context lines from the top and bottom of list 'l'. It | |
|
1391 | # checks the hunk to make sure only context lines are removed, and then | |
|
1392 | # returns a new shortened list of lines. | |
|
1393 | fuzz = min(fuzz, len(old)) | |
|
1394 | if fuzz: | |
|
1395 | top = 0 | |
|
1396 | bot = 0 | |
|
1397 | hlen = len(self.hunk) | |
|
1398 | for x in xrange(hlen - 1): | |
|
1399 | # the hunk starts with the @@ line, so use x+1 | |
|
1400 | if self.hunk[x + 1].startswith(' '): | |
|
1401 | top += 1 | |
|
1402 | else: | |
|
1403 | break | |
|
1404 | if not toponly: | |
|
1405 | for x in xrange(hlen - 1): | |
|
1406 | if self.hunk[hlen - bot - 1].startswith(' '): | |
|
1407 | bot += 1 | |
|
1408 | else: | |
|
1409 | break | |
|
1410 | ||
|
1411 | bot = min(fuzz, bot) | |
|
1412 | top = min(fuzz, top) | |
|
1413 | return old[top:len(old) - bot], new[top:len(new) - bot], top | |
|
1414 | return old, new, 0 | |
|
1415 | ||
|
1416 | def fuzzit(self, fuzz, toponly): | |
|
1417 | old, new, top = self._fuzzit(self.a, self.b, fuzz, toponly) | |
|
1418 | oldstart = self.starta + top | |
|
1419 | newstart = self.startb + top | |
|
1420 | # zero length hunk ranges already have their start decremented | |
|
1421 | if self.lena and oldstart > 0: | |
|
1422 | oldstart -= 1 | |
|
1423 | if self.lenb and newstart > 0: | |
|
1424 | newstart -= 1 | |
|
1425 | return old, oldstart, new, newstart | |
|
1426 | ||
|
1427 | class binhunk(object): | |
|
1428 | 'A binary patch file.' | |
|
1429 | def __init__(self, lr, fname): | |
|
1430 | self.text = None | |
|
1431 | self.delta = False | |
|
1432 | self.hunk = ['GIT binary patch\n'] | |
|
1433 | self._fname = fname | |
|
1434 | self._read(lr) | |
|
1435 | ||
|
1436 | def complete(self): | |
|
1437 | return self.text is not None | |
|
1438 | ||
|
1439 | def new(self, lines): | |
|
1440 | if self.delta: | |
|
1441 | return [applybindelta(self.text, ''.join(lines))] | |
|
1442 | return [self.text] | |
|
1443 | ||
|
1444 | def _read(self, lr): | |
|
1445 | def getline(lr, hunk): | |
|
1446 | l = lr.readline() | |
|
1447 | hunk.append(l) | |
|
1448 | return l.rstrip('\r\n') | |
|
1449 | ||
|
1450 | size = 0 | |
|
1451 | while True: | |
|
1452 | line = getline(lr, self.hunk) | |
|
1453 | if not line: | |
|
1454 | raise PatchError(_('could not extract "%s" binary data') | |
|
1455 | % self._fname) | |
|
1456 | if line.startswith('literal '): | |
|
1457 | size = int(line[8:].rstrip()) | |
|
1458 | break | |
|
1459 | if line.startswith('delta '): | |
|
1460 | size = int(line[6:].rstrip()) | |
|
1461 | self.delta = True | |
|
1462 | break | |
|
1463 | dec = [] | |
|
1464 | line = getline(lr, self.hunk) | |
|
1465 | while len(line) > 1: | |
|
1466 | l = line[0:1] | |
|
1467 | if l <= 'Z' and l >= 'A': | |
|
1468 | l = ord(l) - ord('A') + 1 | |
|
1469 | else: | |
|
1470 | l = ord(l) - ord('a') + 27 | |
|
1471 | try: | |
|
1472 | dec.append(util.b85decode(line[1:])[:l]) | |
|
1473 | except ValueError as e: | |
|
1474 | raise PatchError(_('could not decode "%s" binary patch: %s') | |
|
1475 | % (self._fname, stringutil.forcebytestr(e))) | |
|
1476 | line = getline(lr, self.hunk) | |
|
1477 | text = zlib.decompress(''.join(dec)) | |
|
1478 | if len(text) != size: | |
|
1479 | raise PatchError(_('"%s" length is %d bytes, should be %d') | |
|
1480 | % (self._fname, len(text), size)) | |
|
1481 | self.text = text | |
|
1482 | ||
|
1483 | def parsefilename(str): | |
|
1484 | # --- filename \t|space stuff | |
|
1485 | s = str[4:].rstrip('\r\n') | |
|
1486 | i = s.find('\t') | |
|
1487 | if i < 0: | |
|
1488 | i = s.find(' ') | |
|
1489 | if i < 0: | |
|
1490 | return s | |
|
1491 | return s[:i] | |
|
1492 | ||
|
1493 | def reversehunks(hunks): | |
|
1494 | '''reverse the signs in the hunks given as argument | |
|
1495 | ||
|
1496 | This function operates on hunks coming out of patch.filterpatch, that is | |
|
1497 | a list of the form: [header1, hunk1, hunk2, header2...]. Example usage: | |
|
1498 | ||
|
1499 | >>> rawpatch = b"""diff --git a/folder1/g b/folder1/g | |
|
1500 | ... --- a/folder1/g | |
|
1501 | ... +++ b/folder1/g | |
|
1502 | ... @@ -1,7 +1,7 @@ | |
|
1503 | ... +firstline | |
|
1504 | ... c | |
|
1505 | ... 1 | |
|
1506 | ... 2 | |
|
1507 | ... + 3 | |
|
1508 | ... -4 | |
|
1509 | ... 5 | |
|
1510 | ... d | |
|
1511 | ... +lastline""" | |
|
1512 | >>> hunks = parsepatch([rawpatch]) | |
|
1513 | >>> hunkscomingfromfilterpatch = [] | |
|
1514 | >>> for h in hunks: | |
|
1515 | ... hunkscomingfromfilterpatch.append(h) | |
|
1516 | ... hunkscomingfromfilterpatch.extend(h.hunks) | |
|
1517 | ||
|
1518 | >>> reversedhunks = reversehunks(hunkscomingfromfilterpatch) | |
|
1519 | >>> from . import util | |
|
1520 | >>> fp = util.stringio() | |
|
1521 | >>> for c in reversedhunks: | |
|
1522 | ... c.write(fp) | |
|
1523 | >>> fp.seek(0) or None | |
|
1524 | >>> reversedpatch = fp.read() | |
|
1525 | >>> print(pycompat.sysstr(reversedpatch)) | |
|
1526 | diff --git a/folder1/g b/folder1/g | |
|
1527 | --- a/folder1/g | |
|
1528 | +++ b/folder1/g | |
|
1529 | @@ -1,4 +1,3 @@ | |
|
1530 | -firstline | |
|
1531 | c | |
|
1532 | 1 | |
|
1533 | 2 | |
|
1534 | @@ -2,6 +1,6 @@ | |
|
1535 | c | |
|
1536 | 1 | |
|
1537 | 2 | |
|
1538 | - 3 | |
|
1539 | +4 | |
|
1540 | 5 | |
|
1541 | d | |
|
1542 | @@ -6,3 +5,2 @@ | |
|
1543 | 5 | |
|
1544 | d | |
|
1545 | -lastline | |
|
1546 | ||
|
1547 | ''' | |
|
1548 | ||
|
1549 | newhunks = [] | |
|
1550 | for c in hunks: | |
|
1551 | if util.safehasattr(c, 'reversehunk'): | |
|
1552 | c = c.reversehunk() | |
|
1553 | newhunks.append(c) | |
|
1554 | return newhunks | |
|
1555 | ||
|
1556 | def parsepatch(originalchunks, maxcontext=None): | |
|
1557 | """patch -> [] of headers -> [] of hunks | |
|
1558 | ||
|
1559 | If maxcontext is not None, trim context lines if necessary. | |
|
1560 | ||
|
1561 | >>> rawpatch = b'''diff --git a/folder1/g b/folder1/g | |
|
1562 | ... --- a/folder1/g | |
|
1563 | ... +++ b/folder1/g | |
|
1564 | ... @@ -1,8 +1,10 @@ | |
|
1565 | ... 1 | |
|
1566 | ... 2 | |
|
1567 | ... -3 | |
|
1568 | ... 4 | |
|
1569 | ... 5 | |
|
1570 | ... 6 | |
|
1571 | ... +6.1 | |
|
1572 | ... +6.2 | |
|
1573 | ... 7 | |
|
1574 | ... 8 | |
|
1575 | ... +9''' | |
|
1576 | >>> out = util.stringio() | |
|
1577 | >>> headers = parsepatch([rawpatch], maxcontext=1) | |
|
1578 | >>> for header in headers: | |
|
1579 | ... header.write(out) | |
|
1580 | ... for hunk in header.hunks: | |
|
1581 | ... hunk.write(out) | |
|
1582 | >>> print(pycompat.sysstr(out.getvalue())) | |
|
1583 | diff --git a/folder1/g b/folder1/g | |
|
1584 | --- a/folder1/g | |
|
1585 | +++ b/folder1/g | |
|
1586 | @@ -2,3 +2,2 @@ | |
|
1587 | 2 | |
|
1588 | -3 | |
|
1589 | 4 | |
|
1590 | @@ -6,2 +5,4 @@ | |
|
1591 | 6 | |
|
1592 | +6.1 | |
|
1593 | +6.2 | |
|
1594 | 7 | |
|
1595 | @@ -8,1 +9,2 @@ | |
|
1596 | 8 | |
|
1597 | +9 | |
|
1598 | """ | |
|
1599 | class parser(object): | |
|
1600 | """patch parsing state machine""" | |
|
1601 | def __init__(self): | |
|
1602 | self.fromline = 0 | |
|
1603 | self.toline = 0 | |
|
1604 | self.proc = '' | |
|
1605 | self.header = None | |
|
1606 | self.context = [] | |
|
1607 | self.before = [] | |
|
1608 | self.hunk = [] | |
|
1609 | self.headers = [] | |
|
1610 | ||
|
1611 | def addrange(self, limits): | |
|
1612 | fromstart, fromend, tostart, toend, proc = limits | |
|
1613 | self.fromline = int(fromstart) | |
|
1614 | self.toline = int(tostart) | |
|
1615 | self.proc = proc | |
|
1616 | ||
|
1617 | def addcontext(self, context): | |
|
1618 | if self.hunk: | |
|
1619 | h = recordhunk(self.header, self.fromline, self.toline, | |
|
1620 | self.proc, self.before, self.hunk, context, maxcontext) | |
|
1621 | self.header.hunks.append(h) | |
|
1622 | self.fromline += len(self.before) + h.removed | |
|
1623 | self.toline += len(self.before) + h.added | |
|
1624 | self.before = [] | |
|
1625 | self.hunk = [] | |
|
1626 | self.context = context | |
|
1627 | ||
|
1628 | def addhunk(self, hunk): | |
|
1629 | if self.context: | |
|
1630 | self.before = self.context | |
|
1631 | self.context = [] | |
|
1632 | self.hunk = hunk | |
|
1633 | ||
|
1634 | def newfile(self, hdr): | |
|
1635 | self.addcontext([]) | |
|
1636 | h = header(hdr) | |
|
1637 | self.headers.append(h) | |
|
1638 | self.header = h | |
|
1639 | ||
|
1640 | def addother(self, line): | |
|
1641 | pass # 'other' lines are ignored | |
|
1642 | ||
|
1643 | def finished(self): | |
|
1644 | self.addcontext([]) | |
|
1645 | return self.headers | |
|
1646 | ||
|
1647 | transitions = { | |
|
1648 | 'file': {'context': addcontext, | |
|
1649 | 'file': newfile, | |
|
1650 | 'hunk': addhunk, | |
|
1651 | 'range': addrange}, | |
|
1652 | 'context': {'file': newfile, | |
|
1653 | 'hunk': addhunk, | |
|
1654 | 'range': addrange, | |
|
1655 | 'other': addother}, | |
|
1656 | 'hunk': {'context': addcontext, | |
|
1657 | 'file': newfile, | |
|
1658 | 'range': addrange}, | |
|
1659 | 'range': {'context': addcontext, | |
|
1660 | 'hunk': addhunk}, | |
|
1661 | 'other': {'other': addother}, | |
|
1662 | } | |
|
1663 | ||
|
1664 | p = parser() | |
|
1665 | fp = stringio() | |
|
1666 | fp.write(''.join(originalchunks)) | |
|
1667 | fp.seek(0) | |
|
1668 | ||
|
1669 | state = 'context' | |
|
1670 | for newstate, data in scanpatch(fp): | |
|
1671 | try: | |
|
1672 | p.transitions[state][newstate](p, data) | |
|
1673 | except KeyError: | |
|
1674 | raise PatchError('unhandled transition: %s -> %s' % | |
|
1675 | (state, newstate)) | |
|
1676 | state = newstate | |
|
1677 | del fp | |
|
1678 | return p.finished() | |
|
1679 | ||
|
1680 | def pathtransform(path, strip, prefix): | |
|
1681 | '''turn a path from a patch into a path suitable for the repository | |
|
1682 | ||
|
1683 | prefix, if not empty, is expected to be normalized with a / at the end. | |
|
1684 | ||
|
1685 | Returns (stripped components, path in repository). | |
|
1686 | ||
|
1687 | >>> pathtransform(b'a/b/c', 0, b'') | |
|
1688 | ('', 'a/b/c') | |
|
1689 | >>> pathtransform(b' a/b/c ', 0, b'') | |
|
1690 | ('', ' a/b/c') | |
|
1691 | >>> pathtransform(b' a/b/c ', 2, b'') | |
|
1692 | ('a/b/', 'c') | |
|
1693 | >>> pathtransform(b'a/b/c', 0, b'd/e/') | |
|
1694 | ('', 'd/e/a/b/c') | |
|
1695 | >>> pathtransform(b' a//b/c ', 2, b'd/e/') | |
|
1696 | ('a//b/', 'd/e/c') | |
|
1697 | >>> pathtransform(b'a/b/c', 3, b'') | |
|
1698 | Traceback (most recent call last): | |
|
1699 | PatchError: unable to strip away 1 of 3 dirs from a/b/c | |
|
1700 | ''' | |
|
1701 | pathlen = len(path) | |
|
1702 | i = 0 | |
|
1703 | if strip == 0: | |
|
1704 | return '', prefix + path.rstrip() | |
|
1705 | count = strip | |
|
1706 | while count > 0: | |
|
1707 | i = path.find('/', i) | |
|
1708 | if i == -1: | |
|
1709 | raise PatchError(_("unable to strip away %d of %d dirs from %s") % | |
|
1710 | (count, strip, path)) | |
|
1711 | i += 1 | |
|
1712 | # consume '//' in the path | |
|
1713 | while i < pathlen - 1 and path[i:i + 1] == '/': | |
|
1714 | i += 1 | |
|
1715 | count -= 1 | |
|
1716 | return path[:i].lstrip(), prefix + path[i:].rstrip() | |
|
1717 | ||
|
1718 | def makepatchmeta(backend, afile_orig, bfile_orig, hunk, strip, prefix): | |
|
1719 | nulla = afile_orig == "/dev/null" | |
|
1720 | nullb = bfile_orig == "/dev/null" | |
|
1721 | create = nulla and hunk.starta == 0 and hunk.lena == 0 | |
|
1722 | remove = nullb and hunk.startb == 0 and hunk.lenb == 0 | |
|
1723 | abase, afile = pathtransform(afile_orig, strip, prefix) | |
|
1724 | gooda = not nulla and backend.exists(afile) | |
|
1725 | bbase, bfile = pathtransform(bfile_orig, strip, prefix) | |
|
1726 | if afile == bfile: | |
|
1727 | goodb = gooda | |
|
1728 | else: | |
|
1729 | goodb = not nullb and backend.exists(bfile) | |
|
1730 | missing = not goodb and not gooda and not create | |
|
1731 | ||
|
1732 | # some diff programs apparently produce patches where the afile is | |
|
1733 | # not /dev/null, but afile starts with bfile | |
|
1734 | abasedir = afile[:afile.rfind('/') + 1] | |
|
1735 | bbasedir = bfile[:bfile.rfind('/') + 1] | |
|
1736 | if (missing and abasedir == bbasedir and afile.startswith(bfile) | |
|
1737 | and hunk.starta == 0 and hunk.lena == 0): | |
|
1738 | create = True | |
|
1739 | missing = False | |
|
1740 | ||
|
1741 | # If afile is "a/b/foo" and bfile is "a/b/foo.orig" we assume the | |
|
1742 | # diff is between a file and its backup. In this case, the original | |
|
1743 | # file should be patched (see original mpatch code). | |
|
1744 | isbackup = (abase == bbase and bfile.startswith(afile)) | |
|
1745 | fname = None | |
|
1746 | if not missing: | |
|
1747 | if gooda and goodb: | |
|
1748 | if isbackup: | |
|
1749 | fname = afile | |
|
1750 | else: | |
|
1751 | fname = bfile | |
|
1752 | elif gooda: | |
|
1753 | fname = afile | |
|
1754 | ||
|
1755 | if not fname: | |
|
1756 | if not nullb: | |
|
1757 | if isbackup: | |
|
1758 | fname = afile | |
|
1759 | else: | |
|
1760 | fname = bfile | |
|
1761 | elif not nulla: | |
|
1762 | fname = afile | |
|
1763 | else: | |
|
1764 | raise PatchError(_("undefined source and destination files")) | |
|
1765 | ||
|
1766 | gp = patchmeta(fname) | |
|
1767 | if create: | |
|
1768 | gp.op = 'ADD' | |
|
1769 | elif remove: | |
|
1770 | gp.op = 'DELETE' | |
|
1771 | return gp | |
|
1772 | ||
|
1773 | def scanpatch(fp): | |
|
1774 | """like patch.iterhunks, but yield different events | |
|
1775 | ||
|
1776 | - ('file', [header_lines + fromfile + tofile]) | |
|
1777 | - ('context', [context_lines]) | |
|
1778 | - ('hunk', [hunk_lines]) | |
|
1779 | - ('range', (-start,len, +start,len, proc)) | |
|
1780 | """ | |
|
1781 | lines_re = re.compile(br'@@ -(\d+),(\d+) \+(\d+),(\d+) @@\s*(.*)') | |
|
1782 | lr = linereader(fp) | |
|
1783 | ||
|
1784 | def scanwhile(first, p): | |
|
1785 | """scan lr while predicate holds""" | |
|
1786 | lines = [first] | |
|
1787 | for line in iter(lr.readline, ''): | |
|
1788 | if p(line): | |
|
1789 | lines.append(line) | |
|
1790 | else: | |
|
1791 | lr.push(line) | |
|
1792 | break | |
|
1793 | return lines | |
|
1794 | ||
|
1795 | for line in iter(lr.readline, ''): | |
|
1796 | if line.startswith('diff --git a/') or line.startswith('diff -r '): | |
|
1797 | def notheader(line): | |
|
1798 | s = line.split(None, 1) | |
|
1799 | return not s or s[0] not in ('---', 'diff') | |
|
1800 | header = scanwhile(line, notheader) | |
|
1801 | fromfile = lr.readline() | |
|
1802 | if fromfile.startswith('---'): | |
|
1803 | tofile = lr.readline() | |
|
1804 | header += [fromfile, tofile] | |
|
1805 | else: | |
|
1806 | lr.push(fromfile) | |
|
1807 | yield 'file', header | |
|
1808 | elif line.startswith(' '): | |
|
1809 | cs = (' ', '\\') | |
|
1810 | yield 'context', scanwhile(line, lambda l: l.startswith(cs)) | |
|
1811 | elif line.startswith(('-', '+')): | |
|
1812 | cs = ('-', '+', '\\') | |
|
1813 | yield 'hunk', scanwhile(line, lambda l: l.startswith(cs)) | |
|
1814 | else: | |
|
1815 | m = lines_re.match(line) | |
|
1816 | if m: | |
|
1817 | yield 'range', m.groups() | |
|
1818 | else: | |
|
1819 | yield 'other', line | |
|
1820 | ||
|
1821 | def scangitpatch(lr, firstline): | |
|
1822 | """ | |
|
1823 | Git patches can emit: | |
|
1824 | - rename a to b | |
|
1825 | - change b | |
|
1826 | - copy a to c | |
|
1827 | - change c | |
|
1828 | ||
|
1829 | We cannot apply this sequence as-is, the renamed 'a' could not be | |
|
1830 | found for it would have been renamed already. And we cannot copy | |
|
1831 | from 'b' instead because 'b' would have been changed already. So | |
|
1832 | we scan the git patch for copy and rename commands so we can | |
|
1833 | perform the copies ahead of time. | |
|
1834 | """ | |
|
1835 | pos = 0 | |
|
1836 | try: | |
|
1837 | pos = lr.fp.tell() | |
|
1838 | fp = lr.fp | |
|
1839 | except IOError: | |
|
1840 | fp = stringio(lr.fp.read()) | |
|
1841 | gitlr = linereader(fp) | |
|
1842 | gitlr.push(firstline) | |
|
1843 | gitpatches = readgitpatch(gitlr) | |
|
1844 | fp.seek(pos) | |
|
1845 | return gitpatches | |
|
1846 | ||
|
1847 | def iterhunks(fp): | |
|
1848 | """Read a patch and yield the following events: | |
|
1849 | - ("file", afile, bfile, firsthunk): select a new target file. | |
|
1850 | - ("hunk", hunk): a new hunk is ready to be applied, follows a | |
|
1851 | "file" event. | |
|
1852 | - ("git", gitchanges): current diff is in git format, gitchanges | |
|
1853 | maps filenames to gitpatch records. Unique event. | |
|
1854 | """ | |
|
1855 | afile = "" | |
|
1856 | bfile = "" | |
|
1857 | state = None | |
|
1858 | hunknum = 0 | |
|
1859 | emitfile = newfile = False | |
|
1860 | gitpatches = None | |
|
1861 | ||
|
1862 | # our states | |
|
1863 | BFILE = 1 | |
|
1864 | context = None | |
|
1865 | lr = linereader(fp) | |
|
1866 | ||
|
1867 | for x in iter(lr.readline, ''): | |
|
1868 | if state == BFILE and ( | |
|
1869 | (not context and x.startswith('@')) | |
|
1870 | or (context is not False and x.startswith('***************')) | |
|
1871 | or x.startswith('GIT binary patch')): | |
|
1872 | gp = None | |
|
1873 | if (gitpatches and | |
|
1874 | gitpatches[-1].ispatching(afile, bfile)): | |
|
1875 | gp = gitpatches.pop() | |
|
1876 | if x.startswith('GIT binary patch'): | |
|
1877 | h = binhunk(lr, gp.path) | |
|
1878 | else: | |
|
1879 | if context is None and x.startswith('***************'): | |
|
1880 | context = True | |
|
1881 | h = hunk(x, hunknum + 1, lr, context) | |
|
1882 | hunknum += 1 | |
|
1883 | if emitfile: | |
|
1884 | emitfile = False | |
|
1885 | yield 'file', (afile, bfile, h, gp and gp.copy() or None) | |
|
1886 | yield 'hunk', h | |
|
1887 | elif x.startswith('diff --git a/'): | |
|
1888 | m = gitre.match(x.rstrip(' \r\n')) | |
|
1889 | if not m: | |
|
1890 | continue | |
|
1891 | if gitpatches is None: | |
|
1892 | # scan whole input for git metadata | |
|
1893 | gitpatches = scangitpatch(lr, x) | |
|
1894 | yield 'git', [g.copy() for g in gitpatches | |
|
1895 | if g.op in ('COPY', 'RENAME')] | |
|
1896 | gitpatches.reverse() | |
|
1897 | afile = 'a/' + m.group(1) | |
|
1898 | bfile = 'b/' + m.group(2) | |
|
1899 | while gitpatches and not gitpatches[-1].ispatching(afile, bfile): | |
|
1900 | gp = gitpatches.pop() | |
|
1901 | yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy()) | |
|
1902 | if not gitpatches: | |
|
1903 | raise PatchError(_('failed to synchronize metadata for "%s"') | |
|
1904 | % afile[2:]) | |
|
1905 | gp = gitpatches[-1] | |
|
1906 | newfile = True | |
|
1907 | elif x.startswith('---'): | |
|
1908 | # check for a unified diff | |
|
1909 | l2 = lr.readline() | |
|
1910 | if not l2.startswith('+++'): | |
|
1911 | lr.push(l2) | |
|
1912 | continue | |
|
1913 | newfile = True | |
|
1914 | context = False | |
|
1915 | afile = parsefilename(x) | |
|
1916 | bfile = parsefilename(l2) | |
|
1917 | elif x.startswith('***'): | |
|
1918 | # check for a context diff | |
|
1919 | l2 = lr.readline() | |
|
1920 | if not l2.startswith('---'): | |
|
1921 | lr.push(l2) | |
|
1922 | continue | |
|
1923 | l3 = lr.readline() | |
|
1924 | lr.push(l3) | |
|
1925 | if not l3.startswith("***************"): | |
|
1926 | lr.push(l2) | |
|
1927 | continue | |
|
1928 | newfile = True | |
|
1929 | context = True | |
|
1930 | afile = parsefilename(x) | |
|
1931 | bfile = parsefilename(l2) | |
|
1932 | ||
|
1933 | if newfile: | |
|
1934 | newfile = False | |
|
1935 | emitfile = True | |
|
1936 | state = BFILE | |
|
1937 | hunknum = 0 | |
|
1938 | ||
|
1939 | while gitpatches: | |
|
1940 | gp = gitpatches.pop() | |
|
1941 | yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy()) | |
|
1942 | ||
|
1943 | def applybindelta(binchunk, data): | |
|
1944 | """Apply a binary delta hunk | |
|
1945 | The algorithm used is the algorithm from git's patch-delta.c | |
|
1946 | """ | |
|
1947 | def deltahead(binchunk): | |
|
1948 | i = 0 | |
|
1949 | for c in pycompat.bytestr(binchunk): | |
|
1950 | i += 1 | |
|
1951 | if not (ord(c) & 0x80): | |
|
1952 | return i | |
|
1953 | return i | |
|
1954 | out = "" | |
|
1955 | s = deltahead(binchunk) | |
|
1956 | binchunk = binchunk[s:] | |
|
1957 | s = deltahead(binchunk) | |
|
1958 | binchunk = binchunk[s:] | |
|
1959 | i = 0 | |
|
1960 | while i < len(binchunk): | |
|
1961 | cmd = ord(binchunk[i:i + 1]) | |
|
1962 | i += 1 | |
|
1963 | if (cmd & 0x80): | |
|
1964 | offset = 0 | |
|
1965 | size = 0 | |
|
1966 | if (cmd & 0x01): | |
|
1967 | offset = ord(binchunk[i:i + 1]) | |
|
1968 | i += 1 | |
|
1969 | if (cmd & 0x02): | |
|
1970 | offset |= ord(binchunk[i:i + 1]) << 8 | |
|
1971 | i += 1 | |
|
1972 | if (cmd & 0x04): | |
|
1973 | offset |= ord(binchunk[i:i + 1]) << 16 | |
|
1974 | i += 1 | |
|
1975 | if (cmd & 0x08): | |
|
1976 | offset |= ord(binchunk[i:i + 1]) << 24 | |
|
1977 | i += 1 | |
|
1978 | if (cmd & 0x10): | |
|
1979 | size = ord(binchunk[i:i + 1]) | |
|
1980 | i += 1 | |
|
1981 | if (cmd & 0x20): | |
|
1982 | size |= ord(binchunk[i:i + 1]) << 8 | |
|
1983 | i += 1 | |
|
1984 | if (cmd & 0x40): | |
|
1985 | size |= ord(binchunk[i:i + 1]) << 16 | |
|
1986 | i += 1 | |
|
1987 | if size == 0: | |
|
1988 | size = 0x10000 | |
|
1989 | offset_end = offset + size | |
|
1990 | out += data[offset:offset_end] | |
|
1991 | elif cmd != 0: | |
|
1992 | offset_end = i + cmd | |
|
1993 | out += binchunk[i:offset_end] | |
|
1994 | i += cmd | |
|
1995 | else: | |
|
1996 | raise PatchError(_('unexpected delta opcode 0')) | |
|
1997 | return out | |
|
1998 | ||
|
1999 | def applydiff(ui, fp, backend, store, strip=1, prefix='', eolmode='strict'): | |
|
2000 | """Reads a patch from fp and tries to apply it. | |
|
2001 | ||
|
2002 | Returns 0 for a clean patch, -1 if any rejects were found and 1 if | |
|
2003 | there was any fuzz. | |
|
2004 | ||
|
2005 | If 'eolmode' is 'strict', the patch content and patched file are | |
|
2006 | read in binary mode. Otherwise, line endings are ignored when | |
|
2007 | patching then normalized according to 'eolmode'. | |
|
2008 | """ | |
|
2009 | return _applydiff(ui, fp, patchfile, backend, store, strip=strip, | |
|
2010 | prefix=prefix, eolmode=eolmode) | |
|
2011 | ||
|
2012 | def _canonprefix(repo, prefix): | |
|
2013 | if prefix: | |
|
2014 | prefix = pathutil.canonpath(repo.root, repo.getcwd(), prefix) | |
|
2015 | if prefix != '': | |
|
2016 | prefix += '/' | |
|
2017 | return prefix | |
|
2018 | ||
|
2019 | def _applydiff(ui, fp, patcher, backend, store, strip=1, prefix='', | |
|
2020 | eolmode='strict'): | |
|
2021 | prefix = _canonprefix(backend.repo, prefix) | |
|
2022 | def pstrip(p): | |
|
2023 | return pathtransform(p, strip - 1, prefix)[1] | |
|
2024 | ||
|
2025 | rejects = 0 | |
|
2026 | err = 0 | |
|
2027 | current_file = None | |
|
2028 | ||
|
2029 | for state, values in iterhunks(fp): | |
|
2030 | if state == 'hunk': | |
|
2031 | if not current_file: | |
|
2032 | continue | |
|
2033 | ret = current_file.apply(values) | |
|
2034 | if ret > 0: | |
|
2035 | err = 1 | |
|
2036 | elif state == 'file': | |
|
2037 | if current_file: | |
|
2038 | rejects += current_file.close() | |
|
2039 | current_file = None | |
|
2040 | afile, bfile, first_hunk, gp = values | |
|
2041 | if gp: | |
|
2042 | gp.path = pstrip(gp.path) | |
|
2043 | if gp.oldpath: | |
|
2044 | gp.oldpath = pstrip(gp.oldpath) | |
|
2045 | else: | |
|
2046 | gp = makepatchmeta(backend, afile, bfile, first_hunk, strip, | |
|
2047 | prefix) | |
|
2048 | if gp.op == 'RENAME': | |
|
2049 | backend.unlink(gp.oldpath) | |
|
2050 | if not first_hunk: | |
|
2051 | if gp.op == 'DELETE': | |
|
2052 | backend.unlink(gp.path) | |
|
2053 | continue | |
|
2054 | data, mode = None, None | |
|
2055 | if gp.op in ('RENAME', 'COPY'): | |
|
2056 | data, mode = store.getfile(gp.oldpath)[:2] | |
|
2057 | if data is None: | |
|
2058 | # This means that the old path does not exist | |
|
2059 | raise PatchError(_("source file '%s' does not exist") | |
|
2060 | % gp.oldpath) | |
|
2061 | if gp.mode: | |
|
2062 | mode = gp.mode | |
|
2063 | if gp.op == 'ADD': | |
|
2064 | # Added files without content have no hunk and | |
|
2065 | # must be created | |
|
2066 | data = '' | |
|
2067 | if data or mode: | |
|
2068 | if (gp.op in ('ADD', 'RENAME', 'COPY') | |
|
2069 | and backend.exists(gp.path)): | |
|
2070 | raise PatchError(_("cannot create %s: destination " | |
|
2071 | "already exists") % gp.path) | |
|
2072 | backend.setfile(gp.path, data, mode, gp.oldpath) | |
|
2073 | continue | |
|
2074 | try: | |
|
2075 | current_file = patcher(ui, gp, backend, store, | |
|
2076 | eolmode=eolmode) | |
|
2077 | except PatchError as inst: | |
|
2078 | ui.warn(str(inst) + '\n') | |
|
2079 | current_file = None | |
|
2080 | rejects += 1 | |
|
2081 | continue | |
|
2082 | elif state == 'git': | |
|
2083 | for gp in values: | |
|
2084 | path = pstrip(gp.oldpath) | |
|
2085 | data, mode = backend.getfile(path) | |
|
2086 | if data is None: | |
|
2087 | # The error ignored here will trigger a getfile() | |
|
2088 | # error in a place more appropriate for error | |
|
2089 | # handling, and will not interrupt the patching | |
|
2090 | # process. | |
|
2091 | pass | |
|
2092 | else: | |
|
2093 | store.setfile(path, data, mode) | |
|
2094 | else: | |
|
2095 | raise error.Abort(_('unsupported parser state: %s') % state) | |
|
2096 | ||
|
2097 | if current_file: | |
|
2098 | rejects += current_file.close() | |
|
2099 | ||
|
2100 | if rejects: | |
|
2101 | return -1 | |
|
2102 | return err | |
|
2103 | ||
|
2104 | def _externalpatch(ui, repo, patcher, patchname, strip, files, | |
|
2105 | similarity): | |
|
2106 | """use <patcher> to apply <patchname> to the working directory. | |
|
2107 | returns whether patch was applied with fuzz factor.""" | |
|
2108 | ||
|
2109 | fuzz = False | |
|
2110 | args = [] | |
|
2111 | cwd = repo.root | |
|
2112 | if cwd: | |
|
2113 | args.append('-d %s' % procutil.shellquote(cwd)) | |
|
2114 | cmd = ('%s %s -p%d < %s' | |
|
2115 | % (patcher, ' '.join(args), strip, procutil.shellquote(patchname))) | |
|
2116 | ui.debug('Using external patch tool: %s\n' % cmd) | |
|
2117 | fp = procutil.popen(cmd, 'rb') | |
|
2118 | try: | |
|
2119 | for line in util.iterfile(fp): | |
|
2120 | line = line.rstrip() | |
|
2121 | ui.note(line + '\n') | |
|
2122 | if line.startswith('patching file '): | |
|
2123 | pf = util.parsepatchoutput(line) | |
|
2124 | printed_file = False | |
|
2125 | files.add(pf) | |
|
2126 | elif line.find('with fuzz') >= 0: | |
|
2127 | fuzz = True | |
|
2128 | if not printed_file: | |
|
2129 | ui.warn(pf + '\n') | |
|
2130 | printed_file = True | |
|
2131 | ui.warn(line + '\n') | |
|
2132 | elif line.find('saving rejects to file') >= 0: | |
|
2133 | ui.warn(line + '\n') | |
|
2134 | elif line.find('FAILED') >= 0: | |
|
2135 | if not printed_file: | |
|
2136 | ui.warn(pf + '\n') | |
|
2137 | printed_file = True | |
|
2138 | ui.warn(line + '\n') | |
|
2139 | finally: | |
|
2140 | if files: | |
|
2141 | scmutil.marktouched(repo, files, similarity) | |
|
2142 | code = fp.close() | |
|
2143 | if code: | |
|
2144 | raise PatchError(_("patch command failed: %s") % | |
|
2145 | procutil.explainexit(code)) | |
|
2146 | return fuzz | |
|
2147 | ||
|
2148 | def patchbackend(ui, backend, patchobj, strip, prefix, files=None, | |
|
2149 | eolmode='strict'): | |
|
2150 | if files is None: | |
|
2151 | files = set() | |
|
2152 | if eolmode is None: | |
|
2153 | eolmode = ui.config('patch', 'eol') | |
|
2154 | if eolmode.lower() not in eolmodes: | |
|
2155 | raise error.Abort(_('unsupported line endings type: %s') % eolmode) | |
|
2156 | eolmode = eolmode.lower() | |
|
2157 | ||
|
2158 | store = filestore() | |
|
2159 | try: | |
|
2160 | fp = open(patchobj, 'rb') | |
|
2161 | except TypeError: | |
|
2162 | fp = patchobj | |
|
2163 | try: | |
|
2164 | ret = applydiff(ui, fp, backend, store, strip=strip, prefix=prefix, | |
|
2165 | eolmode=eolmode) | |
|
2166 | finally: | |
|
2167 | if fp != patchobj: | |
|
2168 | fp.close() | |
|
2169 | files.update(backend.close()) | |
|
2170 | store.close() | |
|
2171 | if ret < 0: | |
|
2172 | raise PatchError(_('patch failed to apply')) | |
|
2173 | return ret > 0 | |
|
2174 | ||
|
2175 | def internalpatch(ui, repo, patchobj, strip, prefix='', files=None, | |
|
2176 | eolmode='strict', similarity=0): | |
|
2177 | """use builtin patch to apply <patchobj> to the working directory. | |
|
2178 | returns whether patch was applied with fuzz factor.""" | |
|
2179 | backend = workingbackend(ui, repo, similarity) | |
|
2180 | return patchbackend(ui, backend, patchobj, strip, prefix, files, eolmode) | |
|
2181 | ||
|
2182 | def patchrepo(ui, repo, ctx, store, patchobj, strip, prefix, files=None, | |
|
2183 | eolmode='strict'): | |
|
2184 | backend = repobackend(ui, repo, ctx, store) | |
|
2185 | return patchbackend(ui, backend, patchobj, strip, prefix, files, eolmode) | |
|
2186 | ||
|
2187 | def patch(ui, repo, patchname, strip=1, prefix='', files=None, eolmode='strict', | |
|
2188 | similarity=0): | |
|
2189 | """Apply <patchname> to the working directory. | |
|
2190 | ||
|
2191 | 'eolmode' specifies how end of lines should be handled. It can be: | |
|
2192 | - 'strict': inputs are read in binary mode, EOLs are preserved | |
|
2193 | - 'crlf': EOLs are ignored when patching and reset to CRLF | |
|
2194 | - 'lf': EOLs are ignored when patching and reset to LF | |
|
2195 | - None: get it from user settings, default to 'strict' | |
|
2196 | 'eolmode' is ignored when using an external patcher program. | |
|
2197 | ||
|
2198 | Returns whether patch was applied with fuzz factor. | |
|
2199 | """ | |
|
2200 | patcher = ui.config('ui', 'patch') | |
|
2201 | if files is None: | |
|
2202 | files = set() | |
|
2203 | if patcher: | |
|
2204 | return _externalpatch(ui, repo, patcher, patchname, strip, | |
|
2205 | files, similarity) | |
|
2206 | return internalpatch(ui, repo, patchname, strip, prefix, files, eolmode, | |
|
2207 | similarity) | |
|
2208 | ||
|
2209 | def changedfiles(ui, repo, patchpath, strip=1, prefix=''): | |
|
2210 | backend = fsbackend(ui, repo.root) | |
|
2211 | prefix = _canonprefix(repo, prefix) | |
|
2212 | with open(patchpath, 'rb') as fp: | |
|
2213 | changed = set() | |
|
2214 | for state, values in iterhunks(fp): | |
|
2215 | if state == 'file': | |
|
2216 | afile, bfile, first_hunk, gp = values | |
|
2217 | if gp: | |
|
2218 | gp.path = pathtransform(gp.path, strip - 1, prefix)[1] | |
|
2219 | if gp.oldpath: | |
|
2220 | gp.oldpath = pathtransform(gp.oldpath, strip - 1, | |
|
2221 | prefix)[1] | |
|
2222 | else: | |
|
2223 | gp = makepatchmeta(backend, afile, bfile, first_hunk, strip, | |
|
2224 | prefix) | |
|
2225 | changed.add(gp.path) | |
|
2226 | if gp.op == 'RENAME': | |
|
2227 | changed.add(gp.oldpath) | |
|
2228 | elif state not in ('hunk', 'git'): | |
|
2229 | raise error.Abort(_('unsupported parser state: %s') % state) | |
|
2230 | return changed | |
|
2231 | ||
|
2232 | class GitDiffRequired(Exception): | |
|
2233 | pass | |
|
14 | from .. import ( | |
|
15 | mdiff, | |
|
16 | pycompat, | |
|
17 | ) | |
|
2234 | 18 | |
|
2235 | 19 | def diffallopts(ui, opts=None, untrusted=False, section='diff'): |
|
2236 | 20 | '''return diffopts with all features supported and parsed''' |
|
2237 | 21 | return difffeatureopts(ui, opts=opts, untrusted=untrusted, section=section, |
|
2238 | 22 | git=True, whitespace=True, formatchanging=True) |
|
2239 | 23 | |
|
2240 | 24 | diffopts = diffallopts |
|
2241 | 25 | |
|
2242 | 26 | def difffeatureopts(ui, opts=None, untrusted=False, section='diff', git=False, |
|
2243 | 27 | whitespace=False, formatchanging=False): |
|
2244 | 28 | '''return diffopts with only opted-in features parsed |
|
2245 | 29 | |
|
2246 | 30 | Features: |
|
2247 | 31 | - git: git-style diffs |
|
2248 | 32 | - whitespace: whitespace options like ignoreblanklines and ignorews |
|
2249 | 33 | - formatchanging: options that will likely break or cause correctness issues |
|
2250 | 34 | with most diff parsers |
|
2251 | 35 | ''' |
|
2252 | 36 | def get(key, name=None, getter=ui.configbool, forceplain=None): |
|
2253 | 37 | if opts: |
|
2254 | 38 | v = opts.get(key) |
|
2255 | 39 | # diffopts flags are either None-default (which is passed |
|
2256 | 40 | # through unchanged, so we can identify unset values), or |
|
2257 | 41 | # some other falsey default (eg --unified, which defaults |
|
2258 | 42 | # to an empty string). We only want to override the config |
|
2259 | 43 | # entries from hgrc with command line values if they |
|
2260 | 44 | # appear to have been set, which is any truthy value, |
|
2261 | 45 | # True, or False. |
|
2262 | 46 | if v or isinstance(v, bool): |
|
2263 | 47 | return v |
|
2264 | 48 | if forceplain is not None and ui.plain(): |
|
2265 | 49 | return forceplain |
|
2266 | 50 | return getter(section, name or key, untrusted=untrusted) |
|
2267 | 51 | |
|
2268 | 52 | # core options, expected to be understood by every diff parser |
|
2269 | 53 | buildopts = { |
|
2270 | 54 | 'nodates': get('nodates'), |
|
2271 | 55 | 'showfunc': get('show_function', 'showfunc'), |
|
2272 | 56 | 'context': get('unified', getter=ui.config), |
|
2273 | 57 | } |
|
2274 | 58 | buildopts['worddiff'] = ui.configbool('experimental', 'worddiff') |
|
2275 | 59 | buildopts['xdiff'] = ui.configbool('experimental', 'xdiff') |
|
2276 | 60 | |
|
2277 | 61 | if git: |
|
2278 | 62 | buildopts['git'] = get('git') |
|
2279 | 63 | |
|
2280 | 64 | # since this is in the experimental section, we need to call |
|
2281 | 65 | # ui.configbool directory |
|
2282 | 66 | buildopts['showsimilarity'] = ui.configbool('experimental', |
|
2283 | 67 | 'extendedheader.similarity') |
|
2284 | 68 | |
|
2285 | 69 | # need to inspect the ui object instead of using get() since we want to |
|
2286 | 70 | # test for an int |
|
2287 | 71 | hconf = ui.config('experimental', 'extendedheader.index') |
|
2288 | 72 | if hconf is not None: |
|
2289 | 73 | hlen = None |
|
2290 | 74 | try: |
|
2291 | 75 | # the hash config could be an integer (for length of hash) or a |
|
2292 | 76 | # word (e.g. short, full, none) |
|
2293 | 77 | hlen = int(hconf) |
|
2294 | 78 | if hlen < 0 or hlen > 40: |
|
2295 | 79 | msg = _("invalid length for extendedheader.index: '%d'\n") |
|
2296 | 80 | ui.warn(msg % hlen) |
|
2297 | 81 | except ValueError: |
|
2298 | 82 | # default value |
|
2299 | 83 | if hconf == 'short' or hconf == '': |
|
2300 | 84 | hlen = 12 |
|
2301 | 85 | elif hconf == 'full': |
|
2302 | 86 | hlen = 40 |
|
2303 | 87 | elif hconf != 'none': |
|
2304 | 88 | msg = _("invalid value for extendedheader.index: '%s'\n") |
|
2305 | 89 | ui.warn(msg % hconf) |
|
2306 | 90 | finally: |
|
2307 | 91 | buildopts['index'] = hlen |
|
2308 | 92 | |
|
2309 | 93 | if whitespace: |
|
2310 | 94 | buildopts['ignorews'] = get('ignore_all_space', 'ignorews') |
|
2311 | 95 | buildopts['ignorewsamount'] = get('ignore_space_change', |
|
2312 | 96 | 'ignorewsamount') |
|
2313 | 97 | buildopts['ignoreblanklines'] = get('ignore_blank_lines', |
|
2314 | 98 | 'ignoreblanklines') |
|
2315 | 99 | buildopts['ignorewseol'] = get('ignore_space_at_eol', 'ignorewseol') |
|
2316 | 100 | if formatchanging: |
|
2317 | 101 | buildopts['text'] = opts and opts.get('text') |
|
2318 | 102 | binary = None if opts is None else opts.get('binary') |
|
2319 | 103 | buildopts['nobinary'] = (not binary if binary is not None |
|
2320 | 104 | else get('nobinary', forceplain=False)) |
|
2321 | 105 | buildopts['noprefix'] = get('noprefix', forceplain=False) |
|
2322 | 106 | |
|
2323 | 107 | return mdiff.diffopts(**pycompat.strkwargs(buildopts)) |
|
2324 | ||
|
2325 | def diff(repo, node1=None, node2=None, match=None, changes=None, | |
|
2326 | opts=None, losedatafn=None, prefix='', relroot='', copy=None, | |
|
2327 | hunksfilterfn=None): | |
|
2328 | '''yields diff of changes to files between two nodes, or node and | |
|
2329 | working directory. | |
|
2330 | ||
|
2331 | if node1 is None, use first dirstate parent instead. | |
|
2332 | if node2 is None, compare node1 with working directory. | |
|
2333 | ||
|
2334 | losedatafn(**kwarg) is a callable run when opts.upgrade=True and | |
|
2335 | every time some change cannot be represented with the current | |
|
2336 | patch format. Return False to upgrade to git patch format, True to | |
|
2337 | accept the loss or raise an exception to abort the diff. It is | |
|
2338 | called with the name of current file being diffed as 'fn'. If set | |
|
2339 | to None, patches will always be upgraded to git format when | |
|
2340 | necessary. | |
|
2341 | ||
|
2342 | prefix is a filename prefix that is prepended to all filenames on | |
|
2343 | display (used for subrepos). | |
|
2344 | ||
|
2345 | relroot, if not empty, must be normalized with a trailing /. Any match | |
|
2346 | patterns that fall outside it will be ignored. | |
|
2347 | ||
|
2348 | copy, if not empty, should contain mappings {dst@y: src@x} of copy | |
|
2349 | information. | |
|
2350 | ||
|
2351 | hunksfilterfn, if not None, should be a function taking a filectx and | |
|
2352 | hunks generator that may yield filtered hunks. | |
|
2353 | ''' | |
|
2354 | for fctx1, fctx2, hdr, hunks in diffhunks( | |
|
2355 | repo, node1=node1, node2=node2, | |
|
2356 | match=match, changes=changes, opts=opts, | |
|
2357 | losedatafn=losedatafn, prefix=prefix, relroot=relroot, copy=copy, | |
|
2358 | ): | |
|
2359 | if hunksfilterfn is not None: | |
|
2360 | # If the file has been removed, fctx2 is None; but this should | |
|
2361 | # not occur here since we catch removed files early in | |
|
2362 | # logcmdutil.getlinerangerevs() for 'hg log -L'. | |
|
2363 | assert fctx2 is not None, \ | |
|
2364 | 'fctx2 unexpectly None in diff hunks filtering' | |
|
2365 | hunks = hunksfilterfn(fctx2, hunks) | |
|
2366 | text = ''.join(sum((list(hlines) for hrange, hlines in hunks), [])) | |
|
2367 | if hdr and (text or len(hdr) > 1): | |
|
2368 | yield '\n'.join(hdr) + '\n' | |
|
2369 | if text: | |
|
2370 | yield text | |
|
2371 | ||
|
2372 | def diffhunks(repo, node1=None, node2=None, match=None, changes=None, | |
|
2373 | opts=None, losedatafn=None, prefix='', relroot='', copy=None): | |
|
2374 | """Yield diff of changes to files in the form of (`header`, `hunks`) tuples | |
|
2375 | where `header` is a list of diff headers and `hunks` is an iterable of | |
|
2376 | (`hunkrange`, `hunklines`) tuples. | |
|
2377 | ||
|
2378 | See diff() for the meaning of parameters. | |
|
2379 | """ | |
|
2380 | ||
|
2381 | if opts is None: | |
|
2382 | opts = mdiff.defaultopts | |
|
2383 | ||
|
2384 | if not node1 and not node2: | |
|
2385 | node1 = repo.dirstate.p1() | |
|
2386 | ||
|
2387 | def lrugetfilectx(): | |
|
2388 | cache = {} | |
|
2389 | order = collections.deque() | |
|
2390 | def getfilectx(f, ctx): | |
|
2391 | fctx = ctx.filectx(f, filelog=cache.get(f)) | |
|
2392 | if f not in cache: | |
|
2393 | if len(cache) > 20: | |
|
2394 | del cache[order.popleft()] | |
|
2395 | cache[f] = fctx.filelog() | |
|
2396 | else: | |
|
2397 | order.remove(f) | |
|
2398 | order.append(f) | |
|
2399 | return fctx | |
|
2400 | return getfilectx | |
|
2401 | getfilectx = lrugetfilectx() | |
|
2402 | ||
|
2403 | ctx1 = repo[node1] | |
|
2404 | ctx2 = repo[node2] | |
|
2405 | ||
|
2406 | relfiltered = False | |
|
2407 | if relroot != '' and match.always(): | |
|
2408 | # as a special case, create a new matcher with just the relroot | |
|
2409 | pats = [relroot] | |
|
2410 | match = scmutil.match(ctx2, pats, default='path') | |
|
2411 | relfiltered = True | |
|
2412 | ||
|
2413 | if not changes: | |
|
2414 | changes = repo.status(ctx1, ctx2, match=match) | |
|
2415 | modified, added, removed = changes[:3] | |
|
2416 | ||
|
2417 | if not modified and not added and not removed: | |
|
2418 | return [] | |
|
2419 | ||
|
2420 | if repo.ui.debugflag: | |
|
2421 | hexfunc = hex | |
|
2422 | else: | |
|
2423 | hexfunc = short | |
|
2424 | revs = [hexfunc(node) for node in [ctx1.node(), ctx2.node()] if node] | |
|
2425 | ||
|
2426 | if copy is None: | |
|
2427 | copy = {} | |
|
2428 | if opts.git or opts.upgrade: | |
|
2429 | copy = copies.pathcopies(ctx1, ctx2, match=match) | |
|
2430 | ||
|
2431 | if relroot is not None: | |
|
2432 | if not relfiltered: | |
|
2433 | # XXX this would ideally be done in the matcher, but that is | |
|
2434 | # generally meant to 'or' patterns, not 'and' them. In this case we | |
|
2435 | # need to 'and' all the patterns from the matcher with relroot. | |
|
2436 | def filterrel(l): | |
|
2437 | return [f for f in l if f.startswith(relroot)] | |
|
2438 | modified = filterrel(modified) | |
|
2439 | added = filterrel(added) | |
|
2440 | removed = filterrel(removed) | |
|
2441 | relfiltered = True | |
|
2442 | # filter out copies where either side isn't inside the relative root | |
|
2443 | copy = dict(((dst, src) for (dst, src) in copy.iteritems() | |
|
2444 | if dst.startswith(relroot) | |
|
2445 | and src.startswith(relroot))) | |
|
2446 | ||
|
2447 | modifiedset = set(modified) | |
|
2448 | addedset = set(added) | |
|
2449 | removedset = set(removed) | |
|
2450 | for f in modified: | |
|
2451 | if f not in ctx1: | |
|
2452 | # Fix up added, since merged-in additions appear as | |
|
2453 | # modifications during merges | |
|
2454 | modifiedset.remove(f) | |
|
2455 | addedset.add(f) | |
|
2456 | for f in removed: | |
|
2457 | if f not in ctx1: | |
|
2458 | # Merged-in additions that are then removed are reported as removed. | |
|
2459 | # They are not in ctx1, so We don't want to show them in the diff. | |
|
2460 | removedset.remove(f) | |
|
2461 | modified = sorted(modifiedset) | |
|
2462 | added = sorted(addedset) | |
|
2463 | removed = sorted(removedset) | |
|
2464 | for dst, src in list(copy.items()): | |
|
2465 | if src not in ctx1: | |
|
2466 | # Files merged in during a merge and then copied/renamed are | |
|
2467 | # reported as copies. We want to show them in the diff as additions. | |
|
2468 | del copy[dst] | |
|
2469 | ||
|
2470 | prefetchmatch = scmutil.matchfiles( | |
|
2471 | repo, list(modifiedset | addedset | removedset)) | |
|
2472 | scmutil.prefetchfiles(repo, [ctx1.rev(), ctx2.rev()], prefetchmatch) | |
|
2473 | ||
|
2474 | def difffn(opts, losedata): | |
|
2475 | return trydiff(repo, revs, ctx1, ctx2, modified, added, removed, | |
|
2476 | copy, getfilectx, opts, losedata, prefix, relroot) | |
|
2477 | if opts.upgrade and not opts.git: | |
|
2478 | try: | |
|
2479 | def losedata(fn): | |
|
2480 | if not losedatafn or not losedatafn(fn=fn): | |
|
2481 | raise GitDiffRequired | |
|
2482 | # Buffer the whole output until we are sure it can be generated | |
|
2483 | return list(difffn(opts.copy(git=False), losedata)) | |
|
2484 | except GitDiffRequired: | |
|
2485 | return difffn(opts.copy(git=True), None) | |
|
2486 | else: | |
|
2487 | return difffn(opts, None) | |
|
2488 | ||
|
2489 | def diffsinglehunk(hunklines): | |
|
2490 | """yield tokens for a list of lines in a single hunk""" | |
|
2491 | for line in hunklines: | |
|
2492 | # chomp | |
|
2493 | chompline = line.rstrip('\n') | |
|
2494 | # highlight tabs and trailing whitespace | |
|
2495 | stripline = chompline.rstrip() | |
|
2496 | if line.startswith('-'): | |
|
2497 | label = 'diff.deleted' | |
|
2498 | elif line.startswith('+'): | |
|
2499 | label = 'diff.inserted' | |
|
2500 | else: | |
|
2501 | raise error.ProgrammingError('unexpected hunk line: %s' % line) | |
|
2502 | for token in tabsplitter.findall(stripline): | |
|
2503 | if token.startswith('\t'): | |
|
2504 | yield (token, 'diff.tab') | |
|
2505 | else: | |
|
2506 | yield (token, label) | |
|
2507 | ||
|
2508 | if chompline != stripline: | |
|
2509 | yield (chompline[len(stripline):], 'diff.trailingwhitespace') | |
|
2510 | if chompline != line: | |
|
2511 | yield (line[len(chompline):], '') | |
|
2512 | ||
|
2513 | def diffsinglehunkinline(hunklines): | |
|
2514 | """yield tokens for a list of lines in a single hunk, with inline colors""" | |
|
2515 | # prepare deleted, and inserted content | |
|
2516 | a = '' | |
|
2517 | b = '' | |
|
2518 | for line in hunklines: | |
|
2519 | if line[0] == '-': | |
|
2520 | a += line[1:] | |
|
2521 | elif line[0] == '+': | |
|
2522 | b += line[1:] | |
|
2523 | else: | |
|
2524 | raise error.ProgrammingError('unexpected hunk line: %s' % line) | |
|
2525 | # fast path: if either side is empty, use diffsinglehunk | |
|
2526 | if not a or not b: | |
|
2527 | for t in diffsinglehunk(hunklines): | |
|
2528 | yield t | |
|
2529 | return | |
|
2530 | # re-split the content into words | |
|
2531 | al = wordsplitter.findall(a) | |
|
2532 | bl = wordsplitter.findall(b) | |
|
2533 | # re-arrange the words to lines since the diff algorithm is line-based | |
|
2534 | aln = [s if s == '\n' else s + '\n' for s in al] | |
|
2535 | bln = [s if s == '\n' else s + '\n' for s in bl] | |
|
2536 | an = ''.join(aln) | |
|
2537 | bn = ''.join(bln) | |
|
2538 | # run the diff algorithm, prepare atokens and btokens | |
|
2539 | atokens = [] | |
|
2540 | btokens = [] | |
|
2541 | blocks = mdiff.allblocks(an, bn, lines1=aln, lines2=bln) | |
|
2542 | for (a1, a2, b1, b2), btype in blocks: | |
|
2543 | changed = btype == '!' | |
|
2544 | for token in mdiff.splitnewlines(''.join(al[a1:a2])): | |
|
2545 | atokens.append((changed, token)) | |
|
2546 | for token in mdiff.splitnewlines(''.join(bl[b1:b2])): | |
|
2547 | btokens.append((changed, token)) | |
|
2548 | ||
|
2549 | # yield deleted tokens, then inserted ones | |
|
2550 | for prefix, label, tokens in [('-', 'diff.deleted', atokens), | |
|
2551 | ('+', 'diff.inserted', btokens)]: | |
|
2552 | nextisnewline = True | |
|
2553 | for changed, token in tokens: | |
|
2554 | if nextisnewline: | |
|
2555 | yield (prefix, label) | |
|
2556 | nextisnewline = False | |
|
2557 | # special handling line end | |
|
2558 | isendofline = token.endswith('\n') | |
|
2559 | if isendofline: | |
|
2560 | chomp = token[:-1] # chomp | |
|
2561 | token = chomp.rstrip() # detect spaces at the end | |
|
2562 | endspaces = chomp[len(token):] | |
|
2563 | # scan tabs | |
|
2564 | for maybetab in tabsplitter.findall(token): | |
|
2565 | if '\t' == maybetab[0]: | |
|
2566 | currentlabel = 'diff.tab' | |
|
2567 | else: | |
|
2568 | if changed: | |
|
2569 | currentlabel = label + '.changed' | |
|
2570 | else: | |
|
2571 | currentlabel = label + '.unchanged' | |
|
2572 | yield (maybetab, currentlabel) | |
|
2573 | if isendofline: | |
|
2574 | if endspaces: | |
|
2575 | yield (endspaces, 'diff.trailingwhitespace') | |
|
2576 | yield ('\n', '') | |
|
2577 | nextisnewline = True | |
|
2578 | ||
|
2579 | def difflabel(func, *args, **kw): | |
|
2580 | '''yields 2-tuples of (output, label) based on the output of func()''' | |
|
2581 | if kw.get(r'opts') and kw[r'opts'].worddiff: | |
|
2582 | dodiffhunk = diffsinglehunkinline | |
|
2583 | else: | |
|
2584 | dodiffhunk = diffsinglehunk | |
|
2585 | headprefixes = [('diff', 'diff.diffline'), | |
|
2586 | ('copy', 'diff.extended'), | |
|
2587 | ('rename', 'diff.extended'), | |
|
2588 | ('old', 'diff.extended'), | |
|
2589 | ('new', 'diff.extended'), | |
|
2590 | ('deleted', 'diff.extended'), | |
|
2591 | ('index', 'diff.extended'), | |
|
2592 | ('similarity', 'diff.extended'), | |
|
2593 | ('---', 'diff.file_a'), | |
|
2594 | ('+++', 'diff.file_b')] | |
|
2595 | textprefixes = [('@', 'diff.hunk'), | |
|
2596 | # - and + are handled by diffsinglehunk | |
|
2597 | ] | |
|
2598 | head = False | |
|
2599 | ||
|
2600 | # buffers a hunk, i.e. adjacent "-", "+" lines without other changes. | |
|
2601 | hunkbuffer = [] | |
|
2602 | def consumehunkbuffer(): | |
|
2603 | if hunkbuffer: | |
|
2604 | for token in dodiffhunk(hunkbuffer): | |
|
2605 | yield token | |
|
2606 | hunkbuffer[:] = [] | |
|
2607 | ||
|
2608 | for chunk in func(*args, **kw): | |
|
2609 | lines = chunk.split('\n') | |
|
2610 | linecount = len(lines) | |
|
2611 | for i, line in enumerate(lines): | |
|
2612 | if head: | |
|
2613 | if line.startswith('@'): | |
|
2614 | head = False | |
|
2615 | else: | |
|
2616 | if line and not line.startswith((' ', '+', '-', '@', '\\')): | |
|
2617 | head = True | |
|
2618 | diffline = False | |
|
2619 | if not head and line and line.startswith(('+', '-')): | |
|
2620 | diffline = True | |
|
2621 | ||
|
2622 | prefixes = textprefixes | |
|
2623 | if head: | |
|
2624 | prefixes = headprefixes | |
|
2625 | if diffline: | |
|
2626 | # buffered | |
|
2627 | bufferedline = line | |
|
2628 | if i + 1 < linecount: | |
|
2629 | bufferedline += "\n" | |
|
2630 | hunkbuffer.append(bufferedline) | |
|
2631 | else: | |
|
2632 | # unbuffered | |
|
2633 | for token in consumehunkbuffer(): | |
|
2634 | yield token | |
|
2635 | stripline = line.rstrip() | |
|
2636 | for prefix, label in prefixes: | |
|
2637 | if stripline.startswith(prefix): | |
|
2638 | yield (stripline, label) | |
|
2639 | if line != stripline: | |
|
2640 | yield (line[len(stripline):], | |
|
2641 | 'diff.trailingwhitespace') | |
|
2642 | break | |
|
2643 | else: | |
|
2644 | yield (line, '') | |
|
2645 | if i + 1 < linecount: | |
|
2646 | yield ('\n', '') | |
|
2647 | for token in consumehunkbuffer(): | |
|
2648 | yield token | |
|
2649 | ||
|
2650 | def diffui(*args, **kw): | |
|
2651 | '''like diff(), but yields 2-tuples of (output, label) for ui.write()''' | |
|
2652 | return difflabel(diff, *args, **kw) | |
|
2653 | ||
|
2654 | def _filepairs(modified, added, removed, copy, opts): | |
|
2655 | '''generates tuples (f1, f2, copyop), where f1 is the name of the file | |
|
2656 | before and f2 is the the name after. For added files, f1 will be None, | |
|
2657 | and for removed files, f2 will be None. copyop may be set to None, 'copy' | |
|
2658 | or 'rename' (the latter two only if opts.git is set).''' | |
|
2659 | gone = set() | |
|
2660 | ||
|
2661 | copyto = dict([(v, k) for k, v in copy.items()]) | |
|
2662 | ||
|
2663 | addedset, removedset = set(added), set(removed) | |
|
2664 | ||
|
2665 | for f in sorted(modified + added + removed): | |
|
2666 | copyop = None | |
|
2667 | f1, f2 = f, f | |
|
2668 | if f in addedset: | |
|
2669 | f1 = None | |
|
2670 | if f in copy: | |
|
2671 | if opts.git: | |
|
2672 | f1 = copy[f] | |
|
2673 | if f1 in removedset and f1 not in gone: | |
|
2674 | copyop = 'rename' | |
|
2675 | gone.add(f1) | |
|
2676 | else: | |
|
2677 | copyop = 'copy' | |
|
2678 | elif f in removedset: | |
|
2679 | f2 = None | |
|
2680 | if opts.git: | |
|
2681 | # have we already reported a copy above? | |
|
2682 | if (f in copyto and copyto[f] in addedset | |
|
2683 | and copy[copyto[f]] == f): | |
|
2684 | continue | |
|
2685 | yield f1, f2, copyop | |
|
2686 | ||
|
2687 | def trydiff(repo, revs, ctx1, ctx2, modified, added, removed, | |
|
2688 | copy, getfilectx, opts, losedatafn, prefix, relroot): | |
|
2689 | '''given input data, generate a diff and yield it in blocks | |
|
2690 | ||
|
2691 | If generating a diff would lose data like flags or binary data and | |
|
2692 | losedatafn is not None, it will be called. | |
|
2693 | ||
|
2694 | relroot is removed and prefix is added to every path in the diff output. | |
|
2695 | ||
|
2696 | If relroot is not empty, this function expects every path in modified, | |
|
2697 | added, removed and copy to start with it.''' | |
|
2698 | ||
|
2699 | def gitindex(text): | |
|
2700 | if not text: | |
|
2701 | text = "" | |
|
2702 | l = len(text) | |
|
2703 | s = hashlib.sha1('blob %d\0' % l) | |
|
2704 | s.update(text) | |
|
2705 | return hex(s.digest()) | |
|
2706 | ||
|
2707 | if opts.noprefix: | |
|
2708 | aprefix = bprefix = '' | |
|
2709 | else: | |
|
2710 | aprefix = 'a/' | |
|
2711 | bprefix = 'b/' | |
|
2712 | ||
|
2713 | def diffline(f, revs): | |
|
2714 | revinfo = ' '.join(["-r %s" % rev for rev in revs]) | |
|
2715 | return 'diff %s %s' % (revinfo, f) | |
|
2716 | ||
|
2717 | def isempty(fctx): | |
|
2718 | return fctx is None or fctx.size() == 0 | |
|
2719 | ||
|
2720 | date1 = dateutil.datestr(ctx1.date()) | |
|
2721 | date2 = dateutil.datestr(ctx2.date()) | |
|
2722 | ||
|
2723 | gitmode = {'l': '120000', 'x': '100755', '': '100644'} | |
|
2724 | ||
|
2725 | if relroot != '' and (repo.ui.configbool('devel', 'all-warnings') | |
|
2726 | or repo.ui.configbool('devel', 'check-relroot')): | |
|
2727 | for f in modified + added + removed + list(copy) + list(copy.values()): | |
|
2728 | if f is not None and not f.startswith(relroot): | |
|
2729 | raise AssertionError( | |
|
2730 | "file %s doesn't start with relroot %s" % (f, relroot)) | |
|
2731 | ||
|
2732 | for f1, f2, copyop in _filepairs(modified, added, removed, copy, opts): | |
|
2733 | content1 = None | |
|
2734 | content2 = None | |
|
2735 | fctx1 = None | |
|
2736 | fctx2 = None | |
|
2737 | flag1 = None | |
|
2738 | flag2 = None | |
|
2739 | if f1: | |
|
2740 | fctx1 = getfilectx(f1, ctx1) | |
|
2741 | if opts.git or losedatafn: | |
|
2742 | flag1 = ctx1.flags(f1) | |
|
2743 | if f2: | |
|
2744 | fctx2 = getfilectx(f2, ctx2) | |
|
2745 | if opts.git or losedatafn: | |
|
2746 | flag2 = ctx2.flags(f2) | |
|
2747 | # if binary is True, output "summary" or "base85", but not "text diff" | |
|
2748 | if opts.text: | |
|
2749 | binary = False | |
|
2750 | else: | |
|
2751 | binary = any(f.isbinary() for f in [fctx1, fctx2] if f is not None) | |
|
2752 | ||
|
2753 | if losedatafn and not opts.git: | |
|
2754 | if (binary or | |
|
2755 | # copy/rename | |
|
2756 | f2 in copy or | |
|
2757 | # empty file creation | |
|
2758 | (not f1 and isempty(fctx2)) or | |
|
2759 | # empty file deletion | |
|
2760 | (isempty(fctx1) and not f2) or | |
|
2761 | # create with flags | |
|
2762 | (not f1 and flag2) or | |
|
2763 | # change flags | |
|
2764 | (f1 and f2 and flag1 != flag2)): | |
|
2765 | losedatafn(f2 or f1) | |
|
2766 | ||
|
2767 | path1 = f1 or f2 | |
|
2768 | path2 = f2 or f1 | |
|
2769 | path1 = posixpath.join(prefix, path1[len(relroot):]) | |
|
2770 | path2 = posixpath.join(prefix, path2[len(relroot):]) | |
|
2771 | header = [] | |
|
2772 | if opts.git: | |
|
2773 | header.append('diff --git %s%s %s%s' % | |
|
2774 | (aprefix, path1, bprefix, path2)) | |
|
2775 | if not f1: # added | |
|
2776 | header.append('new file mode %s' % gitmode[flag2]) | |
|
2777 | elif not f2: # removed | |
|
2778 | header.append('deleted file mode %s' % gitmode[flag1]) | |
|
2779 | else: # modified/copied/renamed | |
|
2780 | mode1, mode2 = gitmode[flag1], gitmode[flag2] | |
|
2781 | if mode1 != mode2: | |
|
2782 | header.append('old mode %s' % mode1) | |
|
2783 | header.append('new mode %s' % mode2) | |
|
2784 | if copyop is not None: | |
|
2785 | if opts.showsimilarity: | |
|
2786 | sim = similar.score(ctx1[path1], ctx2[path2]) * 100 | |
|
2787 | header.append('similarity index %d%%' % sim) | |
|
2788 | header.append('%s from %s' % (copyop, path1)) | |
|
2789 | header.append('%s to %s' % (copyop, path2)) | |
|
2790 | elif revs and not repo.ui.quiet: | |
|
2791 | header.append(diffline(path1, revs)) | |
|
2792 | ||
|
2793 | # fctx.is | diffopts | what to | is fctx.data() | |
|
2794 | # binary() | text nobinary git index | output? | outputted? | |
|
2795 | # ------------------------------------|---------------------------- | |
|
2796 | # yes | no no no * | summary | no | |
|
2797 | # yes | no no yes * | base85 | yes | |
|
2798 | # yes | no yes no * | summary | no | |
|
2799 | # yes | no yes yes 0 | summary | no | |
|
2800 | # yes | no yes yes >0 | summary | semi [1] | |
|
2801 | # yes | yes * * * | text diff | yes | |
|
2802 | # no | * * * * | text diff | yes | |
|
2803 | # [1]: hash(fctx.data()) is outputted. so fctx.data() cannot be faked | |
|
2804 | if binary and (not opts.git or (opts.git and opts.nobinary and not | |
|
2805 | opts.index)): | |
|
2806 | # fast path: no binary content will be displayed, content1 and | |
|
2807 | # content2 are only used for equivalent test. cmp() could have a | |
|
2808 | # fast path. | |
|
2809 | if fctx1 is not None: | |
|
2810 | content1 = b'\0' | |
|
2811 | if fctx2 is not None: | |
|
2812 | if fctx1 is not None and not fctx1.cmp(fctx2): | |
|
2813 | content2 = b'\0' # not different | |
|
2814 | else: | |
|
2815 | content2 = b'\0\0' | |
|
2816 | else: | |
|
2817 | # normal path: load contents | |
|
2818 | if fctx1 is not None: | |
|
2819 | content1 = fctx1.data() | |
|
2820 | if fctx2 is not None: | |
|
2821 | content2 = fctx2.data() | |
|
2822 | ||
|
2823 | if binary and opts.git and not opts.nobinary: | |
|
2824 | text = mdiff.b85diff(content1, content2) | |
|
2825 | if text: | |
|
2826 | header.append('index %s..%s' % | |
|
2827 | (gitindex(content1), gitindex(content2))) | |
|
2828 | hunks = (None, [text]), | |
|
2829 | else: | |
|
2830 | if opts.git and opts.index > 0: | |
|
2831 | flag = flag1 | |
|
2832 | if flag is None: | |
|
2833 | flag = flag2 | |
|
2834 | header.append('index %s..%s %s' % | |
|
2835 | (gitindex(content1)[0:opts.index], | |
|
2836 | gitindex(content2)[0:opts.index], | |
|
2837 | gitmode[flag])) | |
|
2838 | ||
|
2839 | uheaders, hunks = mdiff.unidiff(content1, date1, | |
|
2840 | content2, date2, | |
|
2841 | path1, path2, | |
|
2842 | binary=binary, opts=opts) | |
|
2843 | header.extend(uheaders) | |
|
2844 | yield fctx1, fctx2, header, hunks | |
|
2845 | ||
|
2846 | def diffstatsum(stats): | |
|
2847 | maxfile, maxtotal, addtotal, removetotal, binary = 0, 0, 0, 0, False | |
|
2848 | for f, a, r, b in stats: | |
|
2849 | maxfile = max(maxfile, encoding.colwidth(f)) | |
|
2850 | maxtotal = max(maxtotal, a + r) | |
|
2851 | addtotal += a | |
|
2852 | removetotal += r | |
|
2853 | binary = binary or b | |
|
2854 | ||
|
2855 | return maxfile, maxtotal, addtotal, removetotal, binary | |
|
2856 | ||
|
2857 | def diffstatdata(lines): | |
|
2858 | diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$') | |
|
2859 | ||
|
2860 | results = [] | |
|
2861 | filename, adds, removes, isbinary = None, 0, 0, False | |
|
2862 | ||
|
2863 | def addresult(): | |
|
2864 | if filename: | |
|
2865 | results.append((filename, adds, removes, isbinary)) | |
|
2866 | ||
|
2867 | # inheader is used to track if a line is in the | |
|
2868 | # header portion of the diff. This helps properly account | |
|
2869 | # for lines that start with '--' or '++' | |
|
2870 | inheader = False | |
|
2871 | ||
|
2872 | for line in lines: | |
|
2873 | if line.startswith('diff'): | |
|
2874 | addresult() | |
|
2875 | # starting a new file diff | |
|
2876 | # set numbers to 0 and reset inheader | |
|
2877 | inheader = True | |
|
2878 | adds, removes, isbinary = 0, 0, False | |
|
2879 | if line.startswith('diff --git a/'): | |
|
2880 | filename = gitre.search(line).group(2) | |
|
2881 | elif line.startswith('diff -r'): | |
|
2882 | # format: "diff -r ... -r ... filename" | |
|
2883 | filename = diffre.search(line).group(1) | |
|
2884 | elif line.startswith('@@'): | |
|
2885 | inheader = False | |
|
2886 | elif line.startswith('+') and not inheader: | |
|
2887 | adds += 1 | |
|
2888 | elif line.startswith('-') and not inheader: | |
|
2889 | removes += 1 | |
|
2890 | elif (line.startswith('GIT binary patch') or | |
|
2891 | line.startswith('Binary file')): | |
|
2892 | isbinary = True | |
|
2893 | addresult() | |
|
2894 | return results | |
|
2895 | ||
|
2896 | def diffstat(lines, width=80): | |
|
2897 | output = [] | |
|
2898 | stats = diffstatdata(lines) | |
|
2899 | maxname, maxtotal, totaladds, totalremoves, hasbinary = diffstatsum(stats) | |
|
2900 | ||
|
2901 | countwidth = len(str(maxtotal)) | |
|
2902 | if hasbinary and countwidth < 3: | |
|
2903 | countwidth = 3 | |
|
2904 | graphwidth = width - countwidth - maxname - 6 | |
|
2905 | if graphwidth < 10: | |
|
2906 | graphwidth = 10 | |
|
2907 | ||
|
2908 | def scale(i): | |
|
2909 | if maxtotal <= graphwidth: | |
|
2910 | return i | |
|
2911 | # If diffstat runs out of room it doesn't print anything, | |
|
2912 | # which isn't very useful, so always print at least one + or - | |
|
2913 | # if there were at least some changes. | |
|
2914 | return max(i * graphwidth // maxtotal, int(bool(i))) | |
|
2915 | ||
|
2916 | for filename, adds, removes, isbinary in stats: | |
|
2917 | if isbinary: | |
|
2918 | count = 'Bin' | |
|
2919 | else: | |
|
2920 | count = '%d' % (adds + removes) | |
|
2921 | pluses = '+' * scale(adds) | |
|
2922 | minuses = '-' * scale(removes) | |
|
2923 | output.append(' %s%s | %*s %s%s\n' % | |
|
2924 | (filename, ' ' * (maxname - encoding.colwidth(filename)), | |
|
2925 | countwidth, count, pluses, minuses)) | |
|
2926 | ||
|
2927 | if stats: | |
|
2928 | output.append(_(' %d files changed, %d insertions(+), ' | |
|
2929 | '%d deletions(-)\n') | |
|
2930 | % (len(stats), totaladds, totalremoves)) | |
|
2931 | ||
|
2932 | return ''.join(output) | |
|
2933 | ||
|
2934 | def diffstatui(*args, **kw): | |
|
2935 | '''like diffstat(), but yields 2-tuples of (output, label) for | |
|
2936 | ui.write() | |
|
2937 | ''' | |
|
2938 | ||
|
2939 | for line in diffstat(*args, **kw).splitlines(): | |
|
2940 | if line and line[-1] in '+-': | |
|
2941 | name, graph = line.rsplit(' ', 1) | |
|
2942 | yield (name + ' ', '') | |
|
2943 | m = re.search(br'\++', graph) | |
|
2944 | if m: | |
|
2945 | yield (m.group(0), 'diffstat.inserted') | |
|
2946 | m = re.search(br'-+', graph) | |
|
2947 | if m: | |
|
2948 | yield (m.group(0), 'diffstat.deleted') | |
|
2949 | else: | |
|
2950 | yield (line, '') | |
|
2951 | yield ('\n', '') |
General Comments 0
You need to be logged in to leave comments.
Login now