Show More
@@ -1,1633 +1,1632 b'' | |||
|
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 | import cStringIO, email.Parser, os, re |
|
10 | 10 | import tempfile, zlib |
|
11 | 11 | |
|
12 | 12 | from i18n import _ |
|
13 | 13 | from node import hex, nullid, short |
|
14 | 14 | import base85, mdiff, util, diffhelpers, copies, encoding |
|
15 | 15 | |
|
16 | 16 | gitre = re.compile('diff --git a/(.*) b/(.*)') |
|
17 | 17 | |
|
18 | 18 | class PatchError(Exception): |
|
19 | 19 | pass |
|
20 | 20 | |
|
21 | 21 | # helper functions |
|
22 | 22 | |
|
23 | 23 | def copyfile(src, dst, basedir): |
|
24 | 24 | abssrc, absdst = [util.canonpath(basedir, basedir, x) for x in [src, dst]] |
|
25 | 25 | if os.path.lexists(absdst): |
|
26 | 26 | raise util.Abort(_("cannot create %s: destination already exists") % |
|
27 | 27 | dst) |
|
28 | 28 | |
|
29 | 29 | dstdir = os.path.dirname(absdst) |
|
30 | 30 | if dstdir and not os.path.isdir(dstdir): |
|
31 | 31 | try: |
|
32 | 32 | os.makedirs(dstdir) |
|
33 | 33 | except IOError: |
|
34 | 34 | raise util.Abort( |
|
35 | 35 | _("cannot create %s: unable to create destination directory") |
|
36 | 36 | % dst) |
|
37 | 37 | |
|
38 | 38 | util.copyfile(abssrc, absdst) |
|
39 | 39 | |
|
40 | 40 | # public functions |
|
41 | 41 | |
|
42 | 42 | def split(stream): |
|
43 | 43 | '''return an iterator of individual patches from a stream''' |
|
44 | 44 | def isheader(line, inheader): |
|
45 | 45 | if inheader and line[0] in (' ', '\t'): |
|
46 | 46 | # continuation |
|
47 | 47 | return True |
|
48 | 48 | if line[0] in (' ', '-', '+'): |
|
49 | 49 | # diff line - don't check for header pattern in there |
|
50 | 50 | return False |
|
51 | 51 | l = line.split(': ', 1) |
|
52 | 52 | return len(l) == 2 and ' ' not in l[0] |
|
53 | 53 | |
|
54 | 54 | def chunk(lines): |
|
55 | 55 | return cStringIO.StringIO(''.join(lines)) |
|
56 | 56 | |
|
57 | 57 | def hgsplit(stream, cur): |
|
58 | 58 | inheader = True |
|
59 | 59 | |
|
60 | 60 | for line in stream: |
|
61 | 61 | if not line.strip(): |
|
62 | 62 | inheader = False |
|
63 | 63 | if not inheader and line.startswith('# HG changeset patch'): |
|
64 | 64 | yield chunk(cur) |
|
65 | 65 | cur = [] |
|
66 | 66 | inheader = True |
|
67 | 67 | |
|
68 | 68 | cur.append(line) |
|
69 | 69 | |
|
70 | 70 | if cur: |
|
71 | 71 | yield chunk(cur) |
|
72 | 72 | |
|
73 | 73 | def mboxsplit(stream, cur): |
|
74 | 74 | for line in stream: |
|
75 | 75 | if line.startswith('From '): |
|
76 | 76 | for c in split(chunk(cur[1:])): |
|
77 | 77 | yield c |
|
78 | 78 | cur = [] |
|
79 | 79 | |
|
80 | 80 | cur.append(line) |
|
81 | 81 | |
|
82 | 82 | if cur: |
|
83 | 83 | for c in split(chunk(cur[1:])): |
|
84 | 84 | yield c |
|
85 | 85 | |
|
86 | 86 | def mimesplit(stream, cur): |
|
87 | 87 | def msgfp(m): |
|
88 | 88 | fp = cStringIO.StringIO() |
|
89 | 89 | g = email.Generator.Generator(fp, mangle_from_=False) |
|
90 | 90 | g.flatten(m) |
|
91 | 91 | fp.seek(0) |
|
92 | 92 | return fp |
|
93 | 93 | |
|
94 | 94 | for line in stream: |
|
95 | 95 | cur.append(line) |
|
96 | 96 | c = chunk(cur) |
|
97 | 97 | |
|
98 | 98 | m = email.Parser.Parser().parse(c) |
|
99 | 99 | if not m.is_multipart(): |
|
100 | 100 | yield msgfp(m) |
|
101 | 101 | else: |
|
102 | 102 | ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') |
|
103 | 103 | for part in m.walk(): |
|
104 | 104 | ct = part.get_content_type() |
|
105 | 105 | if ct not in ok_types: |
|
106 | 106 | continue |
|
107 | 107 | yield msgfp(part) |
|
108 | 108 | |
|
109 | 109 | def headersplit(stream, cur): |
|
110 | 110 | inheader = False |
|
111 | 111 | |
|
112 | 112 | for line in stream: |
|
113 | 113 | if not inheader and isheader(line, inheader): |
|
114 | 114 | yield chunk(cur) |
|
115 | 115 | cur = [] |
|
116 | 116 | inheader = True |
|
117 | 117 | if inheader and not isheader(line, inheader): |
|
118 | 118 | inheader = False |
|
119 | 119 | |
|
120 | 120 | cur.append(line) |
|
121 | 121 | |
|
122 | 122 | if cur: |
|
123 | 123 | yield chunk(cur) |
|
124 | 124 | |
|
125 | 125 | def remainder(cur): |
|
126 | 126 | yield chunk(cur) |
|
127 | 127 | |
|
128 | 128 | class fiter(object): |
|
129 | 129 | def __init__(self, fp): |
|
130 | 130 | self.fp = fp |
|
131 | 131 | |
|
132 | 132 | def __iter__(self): |
|
133 | 133 | return self |
|
134 | 134 | |
|
135 | 135 | def next(self): |
|
136 | 136 | l = self.fp.readline() |
|
137 | 137 | if not l: |
|
138 | 138 | raise StopIteration |
|
139 | 139 | return l |
|
140 | 140 | |
|
141 | 141 | inheader = False |
|
142 | 142 | cur = [] |
|
143 | 143 | |
|
144 | 144 | mimeheaders = ['content-type'] |
|
145 | 145 | |
|
146 | 146 | if not hasattr(stream, 'next'): |
|
147 | 147 | # http responses, for example, have readline but not next |
|
148 | 148 | stream = fiter(stream) |
|
149 | 149 | |
|
150 | 150 | for line in stream: |
|
151 | 151 | cur.append(line) |
|
152 | 152 | if line.startswith('# HG changeset patch'): |
|
153 | 153 | return hgsplit(stream, cur) |
|
154 | 154 | elif line.startswith('From '): |
|
155 | 155 | return mboxsplit(stream, cur) |
|
156 | 156 | elif isheader(line, inheader): |
|
157 | 157 | inheader = True |
|
158 | 158 | if line.split(':', 1)[0].lower() in mimeheaders: |
|
159 | 159 | # let email parser handle this |
|
160 | 160 | return mimesplit(stream, cur) |
|
161 | 161 | elif line.startswith('--- ') and inheader: |
|
162 | 162 | # No evil headers seen by diff start, split by hand |
|
163 | 163 | return headersplit(stream, cur) |
|
164 | 164 | # Not enough info, keep reading |
|
165 | 165 | |
|
166 | 166 | # if we are here, we have a very plain patch |
|
167 | 167 | return remainder(cur) |
|
168 | 168 | |
|
169 | 169 | def extract(ui, fileobj): |
|
170 | 170 | '''extract patch from data read from fileobj. |
|
171 | 171 | |
|
172 | 172 | patch can be a normal patch or contained in an email message. |
|
173 | 173 | |
|
174 | 174 | return tuple (filename, message, user, date, branch, node, p1, p2). |
|
175 | 175 | Any item in the returned tuple can be None. If filename is None, |
|
176 | 176 | fileobj did not contain a patch. Caller must unlink filename when done.''' |
|
177 | 177 | |
|
178 | 178 | # attempt to detect the start of a patch |
|
179 | 179 | # (this heuristic is borrowed from quilt) |
|
180 | 180 | diffre = re.compile(r'^(?:Index:[ \t]|diff[ \t]|RCS file: |' |
|
181 | 181 | r'retrieving revision [0-9]+(\.[0-9]+)*$|' |
|
182 | 182 | r'---[ \t].*?^\+\+\+[ \t]|' |
|
183 | 183 | r'\*\*\*[ \t].*?^---[ \t])', re.MULTILINE|re.DOTALL) |
|
184 | 184 | |
|
185 | 185 | fd, tmpname = tempfile.mkstemp(prefix='hg-patch-') |
|
186 | 186 | tmpfp = os.fdopen(fd, 'w') |
|
187 | 187 | try: |
|
188 | 188 | msg = email.Parser.Parser().parse(fileobj) |
|
189 | 189 | |
|
190 | 190 | subject = msg['Subject'] |
|
191 | 191 | user = msg['From'] |
|
192 | 192 | if not subject and not user: |
|
193 | 193 | # Not an email, restore parsed headers if any |
|
194 | 194 | subject = '\n'.join(': '.join(h) for h in msg.items()) + '\n' |
|
195 | 195 | |
|
196 | 196 | gitsendmail = 'git-send-email' in msg.get('X-Mailer', '') |
|
197 | 197 | # should try to parse msg['Date'] |
|
198 | 198 | date = None |
|
199 | 199 | nodeid = None |
|
200 | 200 | branch = None |
|
201 | 201 | parents = [] |
|
202 | 202 | |
|
203 | 203 | if subject: |
|
204 | 204 | if subject.startswith('[PATCH'): |
|
205 | 205 | pend = subject.find(']') |
|
206 | 206 | if pend >= 0: |
|
207 | 207 | subject = subject[pend + 1:].lstrip() |
|
208 | 208 | subject = subject.replace('\n\t', ' ') |
|
209 | 209 | ui.debug('Subject: %s\n' % subject) |
|
210 | 210 | if user: |
|
211 | 211 | ui.debug('From: %s\n' % user) |
|
212 | 212 | diffs_seen = 0 |
|
213 | 213 | ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') |
|
214 | 214 | message = '' |
|
215 | 215 | for part in msg.walk(): |
|
216 | 216 | content_type = part.get_content_type() |
|
217 | 217 | ui.debug('Content-Type: %s\n' % content_type) |
|
218 | 218 | if content_type not in ok_types: |
|
219 | 219 | continue |
|
220 | 220 | payload = part.get_payload(decode=True) |
|
221 | 221 | m = diffre.search(payload) |
|
222 | 222 | if m: |
|
223 | 223 | hgpatch = False |
|
224 | 224 | hgpatchheader = False |
|
225 | 225 | ignoretext = False |
|
226 | 226 | |
|
227 | 227 | ui.debug('found patch at byte %d\n' % m.start(0)) |
|
228 | 228 | diffs_seen += 1 |
|
229 | 229 | cfp = cStringIO.StringIO() |
|
230 | 230 | for line in payload[:m.start(0)].splitlines(): |
|
231 | 231 | if line.startswith('# HG changeset patch'): |
|
232 | 232 | ui.debug('patch generated by hg export\n') |
|
233 | 233 | hgpatchheader = True |
|
234 | 234 | # drop earlier commit message content |
|
235 | 235 | cfp.seek(0) |
|
236 | 236 | cfp.truncate() |
|
237 | 237 | subject = None |
|
238 | 238 | elif hgpatchheader: |
|
239 | 239 | if line.startswith('# User '): |
|
240 | 240 | user = line[7:] |
|
241 | 241 | ui.debug('From: %s\n' % user) |
|
242 | 242 | elif line.startswith("# Date "): |
|
243 | 243 | date = line[7:] |
|
244 | 244 | elif line.startswith("# Branch "): |
|
245 | 245 | branch = line[9:] |
|
246 | 246 | elif line.startswith("# Node ID "): |
|
247 | 247 | nodeid = line[10:] |
|
248 | 248 | elif line.startswith("# Parent "): |
|
249 | 249 | parents.append(line[10:]) |
|
250 | 250 | elif not line.startswith("# "): |
|
251 | 251 | hgpatchheader = False |
|
252 | 252 | hgpatch = True |
|
253 | 253 | elif line == '---' and gitsendmail: |
|
254 | 254 | ignoretext = True |
|
255 | 255 | if not hgpatchheader and not ignoretext: |
|
256 | 256 | cfp.write(line) |
|
257 | 257 | cfp.write('\n') |
|
258 | 258 | message = cfp.getvalue() |
|
259 | 259 | if tmpfp: |
|
260 | 260 | tmpfp.write(payload) |
|
261 | 261 | if not payload.endswith('\n'): |
|
262 | 262 | tmpfp.write('\n') |
|
263 | 263 | elif not diffs_seen and message and content_type == 'text/plain': |
|
264 | 264 | message += '\n' + payload |
|
265 | 265 | except: |
|
266 | 266 | tmpfp.close() |
|
267 | 267 | os.unlink(tmpname) |
|
268 | 268 | raise |
|
269 | 269 | |
|
270 | 270 | if subject and not message.startswith(subject): |
|
271 | 271 | message = '%s\n%s' % (subject, message) |
|
272 | 272 | tmpfp.close() |
|
273 | 273 | if not diffs_seen: |
|
274 | 274 | os.unlink(tmpname) |
|
275 | 275 | return None, message, user, date, branch, None, None, None |
|
276 | 276 | p1 = parents and parents.pop(0) or None |
|
277 | 277 | p2 = parents and parents.pop(0) or None |
|
278 | 278 | return tmpname, message, user, date, branch, nodeid, p1, p2 |
|
279 | 279 | |
|
280 | 280 | class patchmeta(object): |
|
281 | 281 | """Patched file metadata |
|
282 | 282 | |
|
283 | 283 | 'op' is the performed operation within ADD, DELETE, RENAME, MODIFY |
|
284 | 284 | or COPY. 'path' is patched file path. 'oldpath' is set to the |
|
285 | 285 | origin file when 'op' is either COPY or RENAME, None otherwise. If |
|
286 | 286 | file mode is changed, 'mode' is a tuple (islink, isexec) where |
|
287 | 287 | 'islink' is True if the file is a symlink and 'isexec' is True if |
|
288 | 288 | the file is executable. Otherwise, 'mode' is None. |
|
289 | 289 | """ |
|
290 | 290 | def __init__(self, path): |
|
291 | 291 | self.path = path |
|
292 | 292 | self.oldpath = None |
|
293 | 293 | self.mode = None |
|
294 | 294 | self.op = 'MODIFY' |
|
295 | 295 | self.binary = False |
|
296 | 296 | |
|
297 | 297 | def setmode(self, mode): |
|
298 | 298 | islink = mode & 020000 |
|
299 | 299 | isexec = mode & 0100 |
|
300 | 300 | self.mode = (islink, isexec) |
|
301 | 301 | |
|
302 | 302 | def __repr__(self): |
|
303 | 303 | return "<patchmeta %s %r>" % (self.op, self.path) |
|
304 | 304 | |
|
305 | 305 | def readgitpatch(lr): |
|
306 | 306 | """extract git-style metadata about patches from <patchname>""" |
|
307 | 307 | |
|
308 | 308 | # Filter patch for git information |
|
309 | 309 | gp = None |
|
310 | 310 | gitpatches = [] |
|
311 | 311 | for line in lr: |
|
312 | 312 | line = line.rstrip(' \r\n') |
|
313 | 313 | if line.startswith('diff --git'): |
|
314 | 314 | m = gitre.match(line) |
|
315 | 315 | if m: |
|
316 | 316 | if gp: |
|
317 | 317 | gitpatches.append(gp) |
|
318 | 318 | dst = m.group(2) |
|
319 | 319 | gp = patchmeta(dst) |
|
320 | 320 | elif gp: |
|
321 | 321 | if line.startswith('--- '): |
|
322 | 322 | gitpatches.append(gp) |
|
323 | 323 | gp = None |
|
324 | 324 | continue |
|
325 | 325 | if line.startswith('rename from '): |
|
326 | 326 | gp.op = 'RENAME' |
|
327 | 327 | gp.oldpath = line[12:] |
|
328 | 328 | elif line.startswith('rename to '): |
|
329 | 329 | gp.path = line[10:] |
|
330 | 330 | elif line.startswith('copy from '): |
|
331 | 331 | gp.op = 'COPY' |
|
332 | 332 | gp.oldpath = line[10:] |
|
333 | 333 | elif line.startswith('copy to '): |
|
334 | 334 | gp.path = line[8:] |
|
335 | 335 | elif line.startswith('deleted file'): |
|
336 | 336 | gp.op = 'DELETE' |
|
337 | 337 | elif line.startswith('new file mode '): |
|
338 | 338 | gp.op = 'ADD' |
|
339 | 339 | gp.setmode(int(line[-6:], 8)) |
|
340 | 340 | elif line.startswith('new mode '): |
|
341 | 341 | gp.setmode(int(line[-6:], 8)) |
|
342 | 342 | elif line.startswith('GIT binary patch'): |
|
343 | 343 | gp.binary = True |
|
344 | 344 | if gp: |
|
345 | 345 | gitpatches.append(gp) |
|
346 | 346 | |
|
347 | 347 | return gitpatches |
|
348 | 348 | |
|
349 | 349 | class linereader(object): |
|
350 | 350 | # simple class to allow pushing lines back into the input stream |
|
351 | 351 | def __init__(self, fp, textmode=False): |
|
352 | 352 | self.fp = fp |
|
353 | 353 | self.buf = [] |
|
354 | 354 | self.textmode = textmode |
|
355 | 355 | self.eol = None |
|
356 | 356 | |
|
357 | 357 | def push(self, line): |
|
358 | 358 | if line is not None: |
|
359 | 359 | self.buf.append(line) |
|
360 | 360 | |
|
361 | 361 | def readline(self): |
|
362 | 362 | if self.buf: |
|
363 | 363 | l = self.buf[0] |
|
364 | 364 | del self.buf[0] |
|
365 | 365 | return l |
|
366 | 366 | l = self.fp.readline() |
|
367 | 367 | if not self.eol: |
|
368 | 368 | if l.endswith('\r\n'): |
|
369 | 369 | self.eol = '\r\n' |
|
370 | 370 | elif l.endswith('\n'): |
|
371 | 371 | self.eol = '\n' |
|
372 | 372 | if self.textmode and l.endswith('\r\n'): |
|
373 | 373 | l = l[:-2] + '\n' |
|
374 | 374 | return l |
|
375 | 375 | |
|
376 | 376 | def __iter__(self): |
|
377 | 377 | while 1: |
|
378 | 378 | l = self.readline() |
|
379 | 379 | if not l: |
|
380 | 380 | break |
|
381 | 381 | yield l |
|
382 | 382 | |
|
383 | 383 | # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1 |
|
384 | 384 | unidesc = re.compile('@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@') |
|
385 | 385 | contextdesc = re.compile('(---|\*\*\*) (\d+)(,(\d+))? (---|\*\*\*)') |
|
386 | 386 | eolmodes = ['strict', 'crlf', 'lf', 'auto'] |
|
387 | 387 | |
|
388 | 388 | class patchfile(object): |
|
389 | 389 | def __init__(self, ui, fname, opener, missing=False, eolmode='strict'): |
|
390 | 390 | self.fname = fname |
|
391 | 391 | self.eolmode = eolmode |
|
392 | 392 | self.eol = None |
|
393 | 393 | self.opener = opener |
|
394 | 394 | self.ui = ui |
|
395 | 395 | self.lines = [] |
|
396 | 396 | self.exists = False |
|
397 | 397 | self.missing = missing |
|
398 | 398 | if not missing: |
|
399 | 399 | try: |
|
400 | 400 | self.lines = self.readlines(fname) |
|
401 | 401 | self.exists = True |
|
402 | 402 | except IOError: |
|
403 | 403 | pass |
|
404 | 404 | else: |
|
405 | 405 | self.ui.warn(_("unable to find '%s' for patching\n") % self.fname) |
|
406 | 406 | |
|
407 | 407 | self.hash = {} |
|
408 | 408 | self.dirty = 0 |
|
409 | 409 | self.offset = 0 |
|
410 | 410 | self.skew = 0 |
|
411 | 411 | self.rej = [] |
|
412 | 412 | self.fileprinted = False |
|
413 | 413 | self.printfile(False) |
|
414 | 414 | self.hunks = 0 |
|
415 | 415 | |
|
416 | 416 | def readlines(self, fname): |
|
417 | 417 | if os.path.islink(fname): |
|
418 | 418 | return [os.readlink(fname)] |
|
419 | 419 | fp = self.opener(fname, 'r') |
|
420 | 420 | try: |
|
421 | 421 | lr = linereader(fp, self.eolmode != 'strict') |
|
422 | 422 | lines = list(lr) |
|
423 | 423 | self.eol = lr.eol |
|
424 | 424 | return lines |
|
425 | 425 | finally: |
|
426 | 426 | fp.close() |
|
427 | 427 | |
|
428 | 428 | def writelines(self, fname, lines): |
|
429 | 429 | # Ensure supplied data ends in fname, being a regular file or |
|
430 | 430 | # a symlink. cmdutil.updatedir will -too magically- take care |
|
431 | 431 | # of setting it to the proper type afterwards. |
|
432 | 432 | islink = os.path.islink(fname) |
|
433 | 433 | if islink: |
|
434 | 434 | fp = cStringIO.StringIO() |
|
435 | 435 | else: |
|
436 | 436 | fp = self.opener(fname, 'w') |
|
437 | 437 | try: |
|
438 | 438 | if self.eolmode == 'auto': |
|
439 | 439 | eol = self.eol |
|
440 | 440 | elif self.eolmode == 'crlf': |
|
441 | 441 | eol = '\r\n' |
|
442 | 442 | else: |
|
443 | 443 | eol = '\n' |
|
444 | 444 | |
|
445 | 445 | if self.eolmode != 'strict' and eol and eol != '\n': |
|
446 | 446 | for l in lines: |
|
447 | 447 | if l and l[-1] == '\n': |
|
448 | 448 | l = l[:-1] + eol |
|
449 | 449 | fp.write(l) |
|
450 | 450 | else: |
|
451 | 451 | fp.writelines(lines) |
|
452 | 452 | if islink: |
|
453 | 453 | self.opener.symlink(fp.getvalue(), fname) |
|
454 | 454 | finally: |
|
455 | 455 | fp.close() |
|
456 | 456 | |
|
457 | 457 | def unlink(self, fname): |
|
458 | 458 | os.unlink(fname) |
|
459 | 459 | |
|
460 | 460 | def printfile(self, warn): |
|
461 | 461 | if self.fileprinted: |
|
462 | 462 | return |
|
463 | 463 | if warn or self.ui.verbose: |
|
464 | 464 | self.fileprinted = True |
|
465 | 465 | s = _("patching file %s\n") % self.fname |
|
466 | 466 | if warn: |
|
467 | 467 | self.ui.warn(s) |
|
468 | 468 | else: |
|
469 | 469 | self.ui.note(s) |
|
470 | 470 | |
|
471 | 471 | |
|
472 | 472 | def findlines(self, l, linenum): |
|
473 | 473 | # looks through the hash and finds candidate lines. The |
|
474 | 474 | # result is a list of line numbers sorted based on distance |
|
475 | 475 | # from linenum |
|
476 | 476 | |
|
477 | 477 | cand = self.hash.get(l, []) |
|
478 | 478 | if len(cand) > 1: |
|
479 | 479 | # resort our list of potentials forward then back. |
|
480 | 480 | cand.sort(key=lambda x: abs(x - linenum)) |
|
481 | 481 | return cand |
|
482 | 482 | |
|
483 | 483 | def hashlines(self): |
|
484 | 484 | self.hash = {} |
|
485 | 485 | for x, s in enumerate(self.lines): |
|
486 | 486 | self.hash.setdefault(s, []).append(x) |
|
487 | 487 | |
|
488 | 488 | def write_rej(self): |
|
489 | 489 | # our rejects are a little different from patch(1). This always |
|
490 | 490 | # creates rejects in the same form as the original patch. A file |
|
491 | 491 | # header is inserted so that you can run the reject through patch again |
|
492 | 492 | # without having to type the filename. |
|
493 | 493 | |
|
494 | 494 | if not self.rej: |
|
495 | 495 | return |
|
496 | 496 | |
|
497 | 497 | fname = self.fname + ".rej" |
|
498 | 498 | self.ui.warn( |
|
499 | 499 | _("%d out of %d hunks FAILED -- saving rejects to file %s\n") % |
|
500 | 500 | (len(self.rej), self.hunks, fname)) |
|
501 | 501 | |
|
502 | 502 | def rejlines(): |
|
503 | 503 | base = os.path.basename(self.fname) |
|
504 | 504 | yield "--- %s\n+++ %s\n" % (base, base) |
|
505 | 505 | for x in self.rej: |
|
506 | 506 | for l in x.hunk: |
|
507 | 507 | yield l |
|
508 | 508 | if l[-1] != '\n': |
|
509 | 509 | yield "\n\ No newline at end of file\n" |
|
510 | 510 | |
|
511 | 511 | self.writelines(fname, rejlines()) |
|
512 | 512 | |
|
513 | 513 | def apply(self, h): |
|
514 | 514 | if not h.complete(): |
|
515 | 515 | raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") % |
|
516 | 516 | (h.number, h.desc, len(h.a), h.lena, len(h.b), |
|
517 | 517 | h.lenb)) |
|
518 | 518 | |
|
519 | 519 | self.hunks += 1 |
|
520 | 520 | |
|
521 | 521 | if self.missing: |
|
522 | 522 | self.rej.append(h) |
|
523 | 523 | return -1 |
|
524 | 524 | |
|
525 | 525 | if self.exists and h.createfile(): |
|
526 | 526 | self.ui.warn(_("file %s already exists\n") % self.fname) |
|
527 | 527 | self.rej.append(h) |
|
528 | 528 | return -1 |
|
529 | 529 | |
|
530 | 530 | if isinstance(h, binhunk): |
|
531 | 531 | if h.rmfile(): |
|
532 | 532 | self.unlink(self.fname) |
|
533 | 533 | else: |
|
534 | 534 | self.lines[:] = h.new() |
|
535 | 535 | self.offset += len(h.new()) |
|
536 | 536 | self.dirty = 1 |
|
537 | 537 | return 0 |
|
538 | 538 | |
|
539 | 539 | horig = h |
|
540 | 540 | if (self.eolmode in ('crlf', 'lf') |
|
541 | 541 | or self.eolmode == 'auto' and self.eol): |
|
542 | 542 | # If new eols are going to be normalized, then normalize |
|
543 | 543 | # hunk data before patching. Otherwise, preserve input |
|
544 | 544 | # line-endings. |
|
545 | 545 | h = h.getnormalized() |
|
546 | 546 | |
|
547 | 547 | # fast case first, no offsets, no fuzz |
|
548 | 548 | old = h.old() |
|
549 | 549 | # patch starts counting at 1 unless we are adding the file |
|
550 | 550 | if h.starta == 0: |
|
551 | 551 | start = 0 |
|
552 | 552 | else: |
|
553 | 553 | start = h.starta + self.offset - 1 |
|
554 | 554 | orig_start = start |
|
555 | 555 | # if there's skew we want to emit the "(offset %d lines)" even |
|
556 | 556 | # when the hunk cleanly applies at start + skew, so skip the |
|
557 | 557 | # fast case code |
|
558 | 558 | if self.skew == 0 and diffhelpers.testhunk(old, self.lines, start) == 0: |
|
559 | 559 | if h.rmfile(): |
|
560 | 560 | self.unlink(self.fname) |
|
561 | 561 | else: |
|
562 | 562 | self.lines[start : start + h.lena] = h.new() |
|
563 | 563 | self.offset += h.lenb - h.lena |
|
564 | 564 | self.dirty = 1 |
|
565 | 565 | return 0 |
|
566 | 566 | |
|
567 | 567 | # ok, we couldn't match the hunk. Lets look for offsets and fuzz it |
|
568 | 568 | self.hashlines() |
|
569 | 569 | if h.hunk[-1][0] != ' ': |
|
570 | 570 | # if the hunk tried to put something at the bottom of the file |
|
571 | 571 | # override the start line and use eof here |
|
572 | 572 | search_start = len(self.lines) |
|
573 | 573 | else: |
|
574 | 574 | search_start = orig_start + self.skew |
|
575 | 575 | |
|
576 | 576 | for fuzzlen in xrange(3): |
|
577 | 577 | for toponly in [True, False]: |
|
578 | 578 | old = h.old(fuzzlen, toponly) |
|
579 | 579 | |
|
580 | 580 | cand = self.findlines(old[0][1:], search_start) |
|
581 | 581 | for l in cand: |
|
582 | 582 | if diffhelpers.testhunk(old, self.lines, l) == 0: |
|
583 | 583 | newlines = h.new(fuzzlen, toponly) |
|
584 | 584 | self.lines[l : l + len(old)] = newlines |
|
585 | 585 | self.offset += len(newlines) - len(old) |
|
586 | 586 | self.skew = l - orig_start |
|
587 | 587 | self.dirty = 1 |
|
588 | 588 | offset = l - orig_start - fuzzlen |
|
589 | 589 | if fuzzlen: |
|
590 | 590 | msg = _("Hunk #%d succeeded at %d " |
|
591 | 591 | "with fuzz %d " |
|
592 | 592 | "(offset %d lines).\n") |
|
593 | 593 | self.printfile(True) |
|
594 | 594 | self.ui.warn(msg % |
|
595 | 595 | (h.number, l + 1, fuzzlen, offset)) |
|
596 | 596 | else: |
|
597 | 597 | msg = _("Hunk #%d succeeded at %d " |
|
598 | 598 | "(offset %d lines).\n") |
|
599 | 599 | self.ui.note(msg % (h.number, l + 1, offset)) |
|
600 | 600 | return fuzzlen |
|
601 | 601 | self.printfile(True) |
|
602 | 602 | self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start)) |
|
603 | 603 | self.rej.append(horig) |
|
604 | 604 | return -1 |
|
605 | 605 | |
|
606 | 606 | class hunk(object): |
|
607 | 607 | def __init__(self, desc, num, lr, context, create=False, remove=False): |
|
608 | 608 | self.number = num |
|
609 | 609 | self.desc = desc |
|
610 | 610 | self.hunk = [desc] |
|
611 | 611 | self.a = [] |
|
612 | 612 | self.b = [] |
|
613 | 613 | self.starta = self.lena = None |
|
614 | 614 | self.startb = self.lenb = None |
|
615 | 615 | if lr is not None: |
|
616 | 616 | if context: |
|
617 | 617 | self.read_context_hunk(lr) |
|
618 | 618 | else: |
|
619 | 619 | self.read_unified_hunk(lr) |
|
620 | 620 | self.create = create |
|
621 | 621 | self.remove = remove and not create |
|
622 | 622 | |
|
623 | 623 | def getnormalized(self): |
|
624 | 624 | """Return a copy with line endings normalized to LF.""" |
|
625 | 625 | |
|
626 | 626 | def normalize(lines): |
|
627 | 627 | nlines = [] |
|
628 | 628 | for line in lines: |
|
629 | 629 | if line.endswith('\r\n'): |
|
630 | 630 | line = line[:-2] + '\n' |
|
631 | 631 | nlines.append(line) |
|
632 | 632 | return nlines |
|
633 | 633 | |
|
634 | 634 | # Dummy object, it is rebuilt manually |
|
635 | 635 | nh = hunk(self.desc, self.number, None, None, False, False) |
|
636 | 636 | nh.number = self.number |
|
637 | 637 | nh.desc = self.desc |
|
638 | 638 | nh.hunk = self.hunk |
|
639 | 639 | nh.a = normalize(self.a) |
|
640 | 640 | nh.b = normalize(self.b) |
|
641 | 641 | nh.starta = self.starta |
|
642 | 642 | nh.startb = self.startb |
|
643 | 643 | nh.lena = self.lena |
|
644 | 644 | nh.lenb = self.lenb |
|
645 | 645 | nh.create = self.create |
|
646 | 646 | nh.remove = self.remove |
|
647 | 647 | return nh |
|
648 | 648 | |
|
649 | 649 | def read_unified_hunk(self, lr): |
|
650 | 650 | m = unidesc.match(self.desc) |
|
651 | 651 | if not m: |
|
652 | 652 | raise PatchError(_("bad hunk #%d") % self.number) |
|
653 | 653 | self.starta, foo, self.lena, self.startb, foo2, self.lenb = m.groups() |
|
654 | 654 | if self.lena is None: |
|
655 | 655 | self.lena = 1 |
|
656 | 656 | else: |
|
657 | 657 | self.lena = int(self.lena) |
|
658 | 658 | if self.lenb is None: |
|
659 | 659 | self.lenb = 1 |
|
660 | 660 | else: |
|
661 | 661 | self.lenb = int(self.lenb) |
|
662 | 662 | self.starta = int(self.starta) |
|
663 | 663 | self.startb = int(self.startb) |
|
664 | 664 | diffhelpers.addlines(lr, self.hunk, self.lena, self.lenb, self.a, self.b) |
|
665 | 665 | # if we hit eof before finishing out the hunk, the last line will |
|
666 | 666 | # be zero length. Lets try to fix it up. |
|
667 | 667 | while len(self.hunk[-1]) == 0: |
|
668 | 668 | del self.hunk[-1] |
|
669 | 669 | del self.a[-1] |
|
670 | 670 | del self.b[-1] |
|
671 | 671 | self.lena -= 1 |
|
672 | 672 | self.lenb -= 1 |
|
673 | 673 | |
|
674 | 674 | def read_context_hunk(self, lr): |
|
675 | 675 | self.desc = lr.readline() |
|
676 | 676 | m = contextdesc.match(self.desc) |
|
677 | 677 | if not m: |
|
678 | 678 | raise PatchError(_("bad hunk #%d") % self.number) |
|
679 | 679 | foo, self.starta, foo2, aend, foo3 = m.groups() |
|
680 | 680 | self.starta = int(self.starta) |
|
681 | 681 | if aend is None: |
|
682 | 682 | aend = self.starta |
|
683 | 683 | self.lena = int(aend) - self.starta |
|
684 | 684 | if self.starta: |
|
685 | 685 | self.lena += 1 |
|
686 | 686 | for x in xrange(self.lena): |
|
687 | 687 | l = lr.readline() |
|
688 | 688 | if l.startswith('---'): |
|
689 | 689 | lr.push(l) |
|
690 | 690 | break |
|
691 | 691 | s = l[2:] |
|
692 | 692 | if l.startswith('- ') or l.startswith('! '): |
|
693 | 693 | u = '-' + s |
|
694 | 694 | elif l.startswith(' '): |
|
695 | 695 | u = ' ' + s |
|
696 | 696 | else: |
|
697 | 697 | raise PatchError(_("bad hunk #%d old text line %d") % |
|
698 | 698 | (self.number, x)) |
|
699 | 699 | self.a.append(u) |
|
700 | 700 | self.hunk.append(u) |
|
701 | 701 | |
|
702 | 702 | l = lr.readline() |
|
703 | 703 | if l.startswith('\ '): |
|
704 | 704 | s = self.a[-1][:-1] |
|
705 | 705 | self.a[-1] = s |
|
706 | 706 | self.hunk[-1] = s |
|
707 | 707 | l = lr.readline() |
|
708 | 708 | m = contextdesc.match(l) |
|
709 | 709 | if not m: |
|
710 | 710 | raise PatchError(_("bad hunk #%d") % self.number) |
|
711 | 711 | foo, self.startb, foo2, bend, foo3 = m.groups() |
|
712 | 712 | self.startb = int(self.startb) |
|
713 | 713 | if bend is None: |
|
714 | 714 | bend = self.startb |
|
715 | 715 | self.lenb = int(bend) - self.startb |
|
716 | 716 | if self.startb: |
|
717 | 717 | self.lenb += 1 |
|
718 | 718 | hunki = 1 |
|
719 | 719 | for x in xrange(self.lenb): |
|
720 | 720 | l = lr.readline() |
|
721 | 721 | if l.startswith('\ '): |
|
722 | 722 | s = self.b[-1][:-1] |
|
723 | 723 | self.b[-1] = s |
|
724 | 724 | self.hunk[hunki - 1] = s |
|
725 | 725 | continue |
|
726 | 726 | if not l: |
|
727 | 727 | lr.push(l) |
|
728 | 728 | break |
|
729 | 729 | s = l[2:] |
|
730 | 730 | if l.startswith('+ ') or l.startswith('! '): |
|
731 | 731 | u = '+' + s |
|
732 | 732 | elif l.startswith(' '): |
|
733 | 733 | u = ' ' + s |
|
734 | 734 | elif len(self.b) == 0: |
|
735 | 735 | # this can happen when the hunk does not add any lines |
|
736 | 736 | lr.push(l) |
|
737 | 737 | break |
|
738 | 738 | else: |
|
739 | 739 | raise PatchError(_("bad hunk #%d old text line %d") % |
|
740 | 740 | (self.number, x)) |
|
741 | 741 | self.b.append(s) |
|
742 | 742 | while True: |
|
743 | 743 | if hunki >= len(self.hunk): |
|
744 | 744 | h = "" |
|
745 | 745 | else: |
|
746 | 746 | h = self.hunk[hunki] |
|
747 | 747 | hunki += 1 |
|
748 | 748 | if h == u: |
|
749 | 749 | break |
|
750 | 750 | elif h.startswith('-'): |
|
751 | 751 | continue |
|
752 | 752 | else: |
|
753 | 753 | self.hunk.insert(hunki - 1, u) |
|
754 | 754 | break |
|
755 | 755 | |
|
756 | 756 | if not self.a: |
|
757 | 757 | # this happens when lines were only added to the hunk |
|
758 | 758 | for x in self.hunk: |
|
759 | 759 | if x.startswith('-') or x.startswith(' '): |
|
760 | 760 | self.a.append(x) |
|
761 | 761 | if not self.b: |
|
762 | 762 | # this happens when lines were only deleted from the hunk |
|
763 | 763 | for x in self.hunk: |
|
764 | 764 | if x.startswith('+') or x.startswith(' '): |
|
765 | 765 | self.b.append(x[1:]) |
|
766 | 766 | # @@ -start,len +start,len @@ |
|
767 | 767 | self.desc = "@@ -%d,%d +%d,%d @@\n" % (self.starta, self.lena, |
|
768 | 768 | self.startb, self.lenb) |
|
769 | 769 | self.hunk[0] = self.desc |
|
770 | 770 | |
|
771 | 771 | def fix_newline(self): |
|
772 | 772 | diffhelpers.fix_newline(self.hunk, self.a, self.b) |
|
773 | 773 | |
|
774 | 774 | def complete(self): |
|
775 | 775 | return len(self.a) == self.lena and len(self.b) == self.lenb |
|
776 | 776 | |
|
777 | 777 | def createfile(self): |
|
778 | 778 | return self.starta == 0 and self.lena == 0 and self.create |
|
779 | 779 | |
|
780 | 780 | def rmfile(self): |
|
781 | 781 | return self.startb == 0 and self.lenb == 0 and self.remove |
|
782 | 782 | |
|
783 | 783 | def fuzzit(self, l, fuzz, toponly): |
|
784 | 784 | # this removes context lines from the top and bottom of list 'l'. It |
|
785 | 785 | # checks the hunk to make sure only context lines are removed, and then |
|
786 | 786 | # returns a new shortened list of lines. |
|
787 | 787 | fuzz = min(fuzz, len(l)-1) |
|
788 | 788 | if fuzz: |
|
789 | 789 | top = 0 |
|
790 | 790 | bot = 0 |
|
791 | 791 | hlen = len(self.hunk) |
|
792 | 792 | for x in xrange(hlen - 1): |
|
793 | 793 | # the hunk starts with the @@ line, so use x+1 |
|
794 | 794 | if self.hunk[x + 1][0] == ' ': |
|
795 | 795 | top += 1 |
|
796 | 796 | else: |
|
797 | 797 | break |
|
798 | 798 | if not toponly: |
|
799 | 799 | for x in xrange(hlen - 1): |
|
800 | 800 | if self.hunk[hlen - bot - 1][0] == ' ': |
|
801 | 801 | bot += 1 |
|
802 | 802 | else: |
|
803 | 803 | break |
|
804 | 804 | |
|
805 | 805 | # top and bot now count context in the hunk |
|
806 | 806 | # adjust them if either one is short |
|
807 | 807 | context = max(top, bot, 3) |
|
808 | 808 | if bot < context: |
|
809 | 809 | bot = max(0, fuzz - (context - bot)) |
|
810 | 810 | else: |
|
811 | 811 | bot = min(fuzz, bot) |
|
812 | 812 | if top < context: |
|
813 | 813 | top = max(0, fuzz - (context - top)) |
|
814 | 814 | else: |
|
815 | 815 | top = min(fuzz, top) |
|
816 | 816 | |
|
817 | 817 | return l[top:len(l)-bot] |
|
818 | 818 | return l |
|
819 | 819 | |
|
820 | 820 | def old(self, fuzz=0, toponly=False): |
|
821 | 821 | return self.fuzzit(self.a, fuzz, toponly) |
|
822 | 822 | |
|
823 | 823 | def new(self, fuzz=0, toponly=False): |
|
824 | 824 | return self.fuzzit(self.b, fuzz, toponly) |
|
825 | 825 | |
|
826 | 826 | class binhunk: |
|
827 | 827 | 'A binary patch file. Only understands literals so far.' |
|
828 | 828 | def __init__(self, gitpatch): |
|
829 | 829 | self.gitpatch = gitpatch |
|
830 | 830 | self.text = None |
|
831 | 831 | self.hunk = ['GIT binary patch\n'] |
|
832 | 832 | |
|
833 | 833 | def createfile(self): |
|
834 | 834 | return self.gitpatch.op in ('ADD', 'RENAME', 'COPY') |
|
835 | 835 | |
|
836 | 836 | def rmfile(self): |
|
837 | 837 | return self.gitpatch.op == 'DELETE' |
|
838 | 838 | |
|
839 | 839 | def complete(self): |
|
840 | 840 | return self.text is not None |
|
841 | 841 | |
|
842 | 842 | def new(self): |
|
843 | 843 | return [self.text] |
|
844 | 844 | |
|
845 | 845 | def extract(self, lr): |
|
846 | 846 | line = lr.readline() |
|
847 | 847 | self.hunk.append(line) |
|
848 | 848 | while line and not line.startswith('literal '): |
|
849 | 849 | line = lr.readline() |
|
850 | 850 | self.hunk.append(line) |
|
851 | 851 | if not line: |
|
852 | 852 | raise PatchError(_('could not extract binary patch')) |
|
853 | 853 | size = int(line[8:].rstrip()) |
|
854 | 854 | dec = [] |
|
855 | 855 | line = lr.readline() |
|
856 | 856 | self.hunk.append(line) |
|
857 | 857 | while len(line) > 1: |
|
858 | 858 | l = line[0] |
|
859 | 859 | if l <= 'Z' and l >= 'A': |
|
860 | 860 | l = ord(l) - ord('A') + 1 |
|
861 | 861 | else: |
|
862 | 862 | l = ord(l) - ord('a') + 27 |
|
863 | 863 | dec.append(base85.b85decode(line[1:-1])[:l]) |
|
864 | 864 | line = lr.readline() |
|
865 | 865 | self.hunk.append(line) |
|
866 | 866 | text = zlib.decompress(''.join(dec)) |
|
867 | 867 | if len(text) != size: |
|
868 | 868 | raise PatchError(_('binary patch is %d bytes, not %d') % |
|
869 | 869 | len(text), size) |
|
870 | 870 | self.text = text |
|
871 | 871 | |
|
872 | 872 | def parsefilename(str): |
|
873 | 873 | # --- filename \t|space stuff |
|
874 | 874 | s = str[4:].rstrip('\r\n') |
|
875 | 875 | i = s.find('\t') |
|
876 | 876 | if i < 0: |
|
877 | 877 | i = s.find(' ') |
|
878 | 878 | if i < 0: |
|
879 | 879 | return s |
|
880 | 880 | return s[:i] |
|
881 | 881 | |
|
882 | 882 | def pathstrip(path, strip): |
|
883 | 883 | pathlen = len(path) |
|
884 | 884 | i = 0 |
|
885 | 885 | if strip == 0: |
|
886 | 886 | return '', path.rstrip() |
|
887 | 887 | count = strip |
|
888 | 888 | while count > 0: |
|
889 | 889 | i = path.find('/', i) |
|
890 | 890 | if i == -1: |
|
891 | 891 | raise PatchError(_("unable to strip away %d of %d dirs from %s") % |
|
892 | 892 | (count, strip, path)) |
|
893 | 893 | i += 1 |
|
894 | 894 | # consume '//' in the path |
|
895 | 895 | while i < pathlen - 1 and path[i] == '/': |
|
896 | 896 | i += 1 |
|
897 | 897 | count -= 1 |
|
898 | 898 | return path[:i].lstrip(), path[i:].rstrip() |
|
899 | 899 | |
|
900 | 900 | def selectfile(afile_orig, bfile_orig, hunk, strip): |
|
901 | 901 | nulla = afile_orig == "/dev/null" |
|
902 | 902 | nullb = bfile_orig == "/dev/null" |
|
903 | 903 | abase, afile = pathstrip(afile_orig, strip) |
|
904 | 904 | gooda = not nulla and os.path.lexists(afile) |
|
905 | 905 | bbase, bfile = pathstrip(bfile_orig, strip) |
|
906 | 906 | if afile == bfile: |
|
907 | 907 | goodb = gooda |
|
908 | 908 | else: |
|
909 | 909 | goodb = not nullb and os.path.lexists(bfile) |
|
910 | 910 | createfunc = hunk.createfile |
|
911 | 911 | missing = not goodb and not gooda and not createfunc() |
|
912 | 912 | |
|
913 | 913 | # some diff programs apparently produce patches where the afile is |
|
914 | 914 | # not /dev/null, but afile starts with bfile |
|
915 | 915 | abasedir = afile[:afile.rfind('/') + 1] |
|
916 | 916 | bbasedir = bfile[:bfile.rfind('/') + 1] |
|
917 | 917 | if missing and abasedir == bbasedir and afile.startswith(bfile): |
|
918 | 918 | # this isn't very pretty |
|
919 | 919 | hunk.create = True |
|
920 | 920 | if createfunc(): |
|
921 | 921 | missing = False |
|
922 | 922 | else: |
|
923 | 923 | hunk.create = False |
|
924 | 924 | |
|
925 | 925 | # If afile is "a/b/foo" and bfile is "a/b/foo.orig" we assume the |
|
926 | 926 | # diff is between a file and its backup. In this case, the original |
|
927 | 927 | # file should be patched (see original mpatch code). |
|
928 | 928 | isbackup = (abase == bbase and bfile.startswith(afile)) |
|
929 | 929 | fname = None |
|
930 | 930 | if not missing: |
|
931 | 931 | if gooda and goodb: |
|
932 | 932 | fname = isbackup and afile or bfile |
|
933 | 933 | elif gooda: |
|
934 | 934 | fname = afile |
|
935 | 935 | |
|
936 | 936 | if not fname: |
|
937 | 937 | if not nullb: |
|
938 | 938 | fname = isbackup and afile or bfile |
|
939 | 939 | elif not nulla: |
|
940 | 940 | fname = afile |
|
941 | 941 | else: |
|
942 | 942 | raise PatchError(_("undefined source and destination files")) |
|
943 | 943 | |
|
944 | 944 | return fname, missing |
|
945 | 945 | |
|
946 | 946 | def scangitpatch(lr, firstline): |
|
947 | 947 | """ |
|
948 | 948 | Git patches can emit: |
|
949 | 949 | - rename a to b |
|
950 | 950 | - change b |
|
951 | 951 | - copy a to c |
|
952 | 952 | - change c |
|
953 | 953 | |
|
954 | 954 | We cannot apply this sequence as-is, the renamed 'a' could not be |
|
955 | 955 | found for it would have been renamed already. And we cannot copy |
|
956 | 956 | from 'b' instead because 'b' would have been changed already. So |
|
957 | 957 | we scan the git patch for copy and rename commands so we can |
|
958 | 958 | perform the copies ahead of time. |
|
959 | 959 | """ |
|
960 | 960 | pos = 0 |
|
961 | 961 | try: |
|
962 | 962 | pos = lr.fp.tell() |
|
963 | 963 | fp = lr.fp |
|
964 | 964 | except IOError: |
|
965 | 965 | fp = cStringIO.StringIO(lr.fp.read()) |
|
966 | 966 | gitlr = linereader(fp, lr.textmode) |
|
967 | 967 | gitlr.push(firstline) |
|
968 | 968 | gitpatches = readgitpatch(gitlr) |
|
969 | 969 | fp.seek(pos) |
|
970 | 970 | return gitpatches |
|
971 | 971 | |
|
972 | 972 | def iterhunks(ui, fp, sourcefile=None): |
|
973 | 973 | """Read a patch and yield the following events: |
|
974 | 974 | - ("file", afile, bfile, firsthunk): select a new target file. |
|
975 | 975 | - ("hunk", hunk): a new hunk is ready to be applied, follows a |
|
976 | 976 | "file" event. |
|
977 | 977 | - ("git", gitchanges): current diff is in git format, gitchanges |
|
978 | 978 | maps filenames to gitpatch records. Unique event. |
|
979 | 979 | """ |
|
980 | 980 | changed = {} |
|
981 | 981 | current_hunk = None |
|
982 | 982 | afile = "" |
|
983 | 983 | bfile = "" |
|
984 | 984 | state = None |
|
985 | 985 | hunknum = 0 |
|
986 | 986 | emitfile = False |
|
987 | 987 | git = False |
|
988 | 988 | |
|
989 | 989 | # our states |
|
990 | 990 | BFILE = 1 |
|
991 | 991 | context = None |
|
992 | 992 | lr = linereader(fp) |
|
993 | 993 | # gitworkdone is True if a git operation (copy, rename, ...) was |
|
994 | 994 | # performed already for the current file. Useful when the file |
|
995 | 995 | # section may have no hunk. |
|
996 | 996 | gitworkdone = False |
|
997 | 997 | |
|
998 | 998 | while True: |
|
999 | 999 | newfile = newgitfile = False |
|
1000 | 1000 | x = lr.readline() |
|
1001 | 1001 | if not x: |
|
1002 | 1002 | break |
|
1003 | 1003 | if current_hunk: |
|
1004 | 1004 | if x.startswith('\ '): |
|
1005 | 1005 | current_hunk.fix_newline() |
|
1006 | 1006 | yield 'hunk', current_hunk |
|
1007 | 1007 | current_hunk = None |
|
1008 | 1008 | if ((sourcefile or state == BFILE) and ((not context and x[0] == '@') or |
|
1009 | 1009 | ((context is not False) and x.startswith('***************')))): |
|
1010 | 1010 | try: |
|
1011 | 1011 | if context is None and x.startswith('***************'): |
|
1012 | 1012 | context = True |
|
1013 | 1013 | gpatch = changed.get(bfile) |
|
1014 | 1014 | create = afile == '/dev/null' or gpatch and gpatch.op == 'ADD' |
|
1015 | 1015 | remove = bfile == '/dev/null' or gpatch and gpatch.op == 'DELETE' |
|
1016 | 1016 | current_hunk = hunk(x, hunknum + 1, lr, context, create, remove) |
|
1017 | 1017 | except PatchError, err: |
|
1018 | 1018 | ui.debug(err) |
|
1019 | 1019 | current_hunk = None |
|
1020 | 1020 | continue |
|
1021 | 1021 | hunknum += 1 |
|
1022 | 1022 | if emitfile: |
|
1023 | 1023 | emitfile = False |
|
1024 | 1024 | yield 'file', (afile, bfile, current_hunk) |
|
1025 | 1025 | elif state == BFILE and x.startswith('GIT binary patch'): |
|
1026 | 1026 | current_hunk = binhunk(changed[bfile]) |
|
1027 | 1027 | hunknum += 1 |
|
1028 | 1028 | if emitfile: |
|
1029 | 1029 | emitfile = False |
|
1030 | 1030 | yield 'file', ('a/' + afile, 'b/' + bfile, current_hunk) |
|
1031 | 1031 | current_hunk.extract(lr) |
|
1032 | 1032 | elif x.startswith('diff --git'): |
|
1033 | 1033 | # check for git diff, scanning the whole patch file if needed |
|
1034 | 1034 | m = gitre.match(x) |
|
1035 | 1035 | gitworkdone = False |
|
1036 | 1036 | if m: |
|
1037 | 1037 | afile, bfile = m.group(1, 2) |
|
1038 | 1038 | if not git: |
|
1039 | 1039 | git = True |
|
1040 | 1040 | gitpatches = scangitpatch(lr, x) |
|
1041 | 1041 | yield 'git', gitpatches |
|
1042 | 1042 | for gp in gitpatches: |
|
1043 | 1043 | changed[gp.path] = gp |
|
1044 | 1044 | # else error? |
|
1045 | 1045 | # copy/rename + modify should modify target, not source |
|
1046 | 1046 | gp = changed.get(bfile) |
|
1047 | 1047 | if gp and (gp.op in ('COPY', 'DELETE', 'RENAME', 'ADD') |
|
1048 | 1048 | or gp.mode): |
|
1049 | 1049 | afile = bfile |
|
1050 | 1050 | gitworkdone = True |
|
1051 | 1051 | newgitfile = True |
|
1052 | 1052 | elif x.startswith('---'): |
|
1053 | 1053 | # check for a unified diff |
|
1054 | 1054 | l2 = lr.readline() |
|
1055 | 1055 | if not l2.startswith('+++'): |
|
1056 | 1056 | lr.push(l2) |
|
1057 | 1057 | continue |
|
1058 | 1058 | newfile = True |
|
1059 | 1059 | context = False |
|
1060 | 1060 | afile = parsefilename(x) |
|
1061 | 1061 | bfile = parsefilename(l2) |
|
1062 | 1062 | elif x.startswith('***'): |
|
1063 | 1063 | # check for a context diff |
|
1064 | 1064 | l2 = lr.readline() |
|
1065 | 1065 | if not l2.startswith('---'): |
|
1066 | 1066 | lr.push(l2) |
|
1067 | 1067 | continue |
|
1068 | 1068 | l3 = lr.readline() |
|
1069 | 1069 | lr.push(l3) |
|
1070 | 1070 | if not l3.startswith("***************"): |
|
1071 | 1071 | lr.push(l2) |
|
1072 | 1072 | continue |
|
1073 | 1073 | newfile = True |
|
1074 | 1074 | context = True |
|
1075 | 1075 | afile = parsefilename(x) |
|
1076 | 1076 | bfile = parsefilename(l2) |
|
1077 | 1077 | |
|
1078 | 1078 | if newfile: |
|
1079 | 1079 | gitworkdone = False |
|
1080 | 1080 | |
|
1081 | 1081 | if newgitfile or newfile: |
|
1082 | 1082 | emitfile = True |
|
1083 | 1083 | state = BFILE |
|
1084 | 1084 | hunknum = 0 |
|
1085 | 1085 | if current_hunk: |
|
1086 | 1086 | if current_hunk.complete(): |
|
1087 | 1087 | yield 'hunk', current_hunk |
|
1088 | 1088 | else: |
|
1089 | 1089 | raise PatchError(_("malformed patch %s %s") % (afile, |
|
1090 | 1090 | current_hunk.desc)) |
|
1091 | 1091 | |
|
1092 | 1092 | def applydiff(ui, fp, changed, strip=1, sourcefile=None, eolmode='strict'): |
|
1093 | 1093 | """Reads a patch from fp and tries to apply it. |
|
1094 | 1094 | |
|
1095 | 1095 | The dict 'changed' is filled in with all of the filenames changed |
|
1096 | 1096 | by the patch. Returns 0 for a clean patch, -1 if any rejects were |
|
1097 | 1097 | found and 1 if there was any fuzz. |
|
1098 | 1098 | |
|
1099 | 1099 | If 'eolmode' is 'strict', the patch content and patched file are |
|
1100 | 1100 | read in binary mode. Otherwise, line endings are ignored when |
|
1101 | 1101 | patching then normalized according to 'eolmode'. |
|
1102 | 1102 | |
|
1103 | 1103 | Callers probably want to call 'cmdutil.updatedir' after this to |
|
1104 | 1104 | apply certain categories of changes not done by this function. |
|
1105 | 1105 | """ |
|
1106 | 1106 | return _applydiff( |
|
1107 | 1107 | ui, fp, patchfile, copyfile, |
|
1108 | 1108 | changed, strip=strip, sourcefile=sourcefile, eolmode=eolmode) |
|
1109 | 1109 | |
|
1110 | 1110 | |
|
1111 | 1111 | def _applydiff(ui, fp, patcher, copyfn, changed, strip=1, |
|
1112 | 1112 | sourcefile=None, eolmode='strict'): |
|
1113 | 1113 | rejects = 0 |
|
1114 | 1114 | err = 0 |
|
1115 | 1115 | current_file = None |
|
1116 | 1116 | cwd = os.getcwd() |
|
1117 | 1117 | opener = util.opener(cwd) |
|
1118 | 1118 | |
|
1119 | 1119 | def closefile(): |
|
1120 | 1120 | if not current_file: |
|
1121 | 1121 | return 0 |
|
1122 | 1122 | if current_file.dirty: |
|
1123 | 1123 | current_file.writelines(current_file.fname, current_file.lines) |
|
1124 | 1124 | current_file.write_rej() |
|
1125 | 1125 | return len(current_file.rej) |
|
1126 | 1126 | |
|
1127 | 1127 | for state, values in iterhunks(ui, fp, sourcefile): |
|
1128 | 1128 | if state == 'hunk': |
|
1129 | 1129 | if not current_file: |
|
1130 | 1130 | continue |
|
1131 | 1131 | ret = current_file.apply(values) |
|
1132 | 1132 | if ret >= 0: |
|
1133 | 1133 | changed.setdefault(current_file.fname, None) |
|
1134 | 1134 | if ret > 0: |
|
1135 | 1135 | err = 1 |
|
1136 | 1136 | elif state == 'file': |
|
1137 | 1137 | rejects += closefile() |
|
1138 | 1138 | afile, bfile, first_hunk = values |
|
1139 | 1139 | try: |
|
1140 | 1140 | if sourcefile: |
|
1141 | 1141 | current_file = patcher(ui, sourcefile, opener, |
|
1142 | 1142 | eolmode=eolmode) |
|
1143 | 1143 | else: |
|
1144 | 1144 | current_file, missing = selectfile(afile, bfile, |
|
1145 | 1145 | first_hunk, strip) |
|
1146 | 1146 | current_file = patcher(ui, current_file, opener, |
|
1147 | 1147 | missing=missing, eolmode=eolmode) |
|
1148 | 1148 | except PatchError, err: |
|
1149 | 1149 | ui.warn(str(err) + '\n') |
|
1150 | 1150 | current_file = None |
|
1151 | 1151 | rejects += 1 |
|
1152 | 1152 | continue |
|
1153 | 1153 | elif state == 'git': |
|
1154 | 1154 | for gp in values: |
|
1155 | 1155 | gp.path = pathstrip(gp.path, strip - 1)[1] |
|
1156 | 1156 | if gp.oldpath: |
|
1157 | 1157 | gp.oldpath = pathstrip(gp.oldpath, strip - 1)[1] |
|
1158 | 1158 | # Binary patches really overwrite target files, copying them |
|
1159 | 1159 | # will just make it fails with "target file exists" |
|
1160 | 1160 | if gp.op in ('COPY', 'RENAME') and not gp.binary: |
|
1161 | 1161 | copyfn(gp.oldpath, gp.path, cwd) |
|
1162 | 1162 | changed[gp.path] = gp |
|
1163 | 1163 | else: |
|
1164 | 1164 | raise util.Abort(_('unsupported parser state: %s') % state) |
|
1165 | 1165 | |
|
1166 | 1166 | rejects += closefile() |
|
1167 | 1167 | |
|
1168 | 1168 | if rejects: |
|
1169 | 1169 | return -1 |
|
1170 | 1170 | return err |
|
1171 | 1171 | |
|
1172 |
def externalpatch(patcher |
|
|
1172 | def externalpatch(patcher, patchname, ui, strip, cwd, files): | |
|
1173 | 1173 | """use <patcher> to apply <patchname> to the working directory. |
|
1174 | 1174 | returns whether patch was applied with fuzz factor.""" |
|
1175 | 1175 | |
|
1176 | 1176 | fuzz = False |
|
1177 | args = [] | |
|
1177 | 1178 | if cwd: |
|
1178 | 1179 | args.append('-d %s' % util.shellquote(cwd)) |
|
1179 | 1180 | fp = util.popen('%s %s -p%d < %s' % (patcher, ' '.join(args), strip, |
|
1180 | 1181 | util.shellquote(patchname))) |
|
1181 | 1182 | |
|
1182 | 1183 | for line in fp: |
|
1183 | 1184 | line = line.rstrip() |
|
1184 | 1185 | ui.note(line + '\n') |
|
1185 | 1186 | if line.startswith('patching file '): |
|
1186 | 1187 | pf = util.parse_patch_output(line) |
|
1187 | 1188 | printed_file = False |
|
1188 | 1189 | files.setdefault(pf, None) |
|
1189 | 1190 | elif line.find('with fuzz') >= 0: |
|
1190 | 1191 | fuzz = True |
|
1191 | 1192 | if not printed_file: |
|
1192 | 1193 | ui.warn(pf + '\n') |
|
1193 | 1194 | printed_file = True |
|
1194 | 1195 | ui.warn(line + '\n') |
|
1195 | 1196 | elif line.find('saving rejects to file') >= 0: |
|
1196 | 1197 | ui.warn(line + '\n') |
|
1197 | 1198 | elif line.find('FAILED') >= 0: |
|
1198 | 1199 | if not printed_file: |
|
1199 | 1200 | ui.warn(pf + '\n') |
|
1200 | 1201 | printed_file = True |
|
1201 | 1202 | ui.warn(line + '\n') |
|
1202 | 1203 | code = fp.close() |
|
1203 | 1204 | if code: |
|
1204 | 1205 | raise PatchError(_("patch command failed: %s") % |
|
1205 | 1206 | util.explain_exit(code)[0]) |
|
1206 | 1207 | return fuzz |
|
1207 | 1208 | |
|
1208 | 1209 | def internalpatch(patchobj, ui, strip, cwd, files=None, eolmode='strict'): |
|
1209 | 1210 | """use builtin patch to apply <patchobj> to the working directory. |
|
1210 | 1211 | returns whether patch was applied with fuzz factor.""" |
|
1211 | 1212 | |
|
1212 | 1213 | if files is None: |
|
1213 | 1214 | files = {} |
|
1214 | 1215 | if eolmode is None: |
|
1215 | 1216 | eolmode = ui.config('patch', 'eol', 'strict') |
|
1216 | 1217 | if eolmode.lower() not in eolmodes: |
|
1217 | 1218 | raise util.Abort(_('unsupported line endings type: %s') % eolmode) |
|
1218 | 1219 | eolmode = eolmode.lower() |
|
1219 | 1220 | |
|
1220 | 1221 | try: |
|
1221 | 1222 | fp = open(patchobj, 'rb') |
|
1222 | 1223 | except TypeError: |
|
1223 | 1224 | fp = patchobj |
|
1224 | 1225 | if cwd: |
|
1225 | 1226 | curdir = os.getcwd() |
|
1226 | 1227 | os.chdir(cwd) |
|
1227 | 1228 | try: |
|
1228 | 1229 | ret = applydiff(ui, fp, files, strip=strip, eolmode=eolmode) |
|
1229 | 1230 | finally: |
|
1230 | 1231 | if cwd: |
|
1231 | 1232 | os.chdir(curdir) |
|
1232 | 1233 | if fp != patchobj: |
|
1233 | 1234 | fp.close() |
|
1234 | 1235 | if ret < 0: |
|
1235 | 1236 | raise PatchError |
|
1236 | 1237 | return ret > 0 |
|
1237 | 1238 | |
|
1238 | 1239 | def patch(patchname, ui, strip=1, cwd=None, files=None, eolmode='strict'): |
|
1239 | 1240 | """Apply <patchname> to the working directory. |
|
1240 | 1241 | |
|
1241 | 1242 | 'eolmode' specifies how end of lines should be handled. It can be: |
|
1242 | 1243 | - 'strict': inputs are read in binary mode, EOLs are preserved |
|
1243 | 1244 | - 'crlf': EOLs are ignored when patching and reset to CRLF |
|
1244 | 1245 | - 'lf': EOLs are ignored when patching and reset to LF |
|
1245 | 1246 | - None: get it from user settings, default to 'strict' |
|
1246 | 1247 | 'eolmode' is ignored when using an external patcher program. |
|
1247 | 1248 | |
|
1248 | 1249 | Returns whether patch was applied with fuzz factor. |
|
1249 | 1250 | """ |
|
1250 | 1251 | patcher = ui.config('ui', 'patch') |
|
1251 | args = [] | |
|
1252 | 1252 | if files is None: |
|
1253 | 1253 | files = {} |
|
1254 | 1254 | try: |
|
1255 | 1255 | if patcher: |
|
1256 |
return externalpatch(patcher |
|
|
1257 | files) | |
|
1256 | return externalpatch(patcher, patchname, ui, strip, cwd, files) | |
|
1258 | 1257 | return internalpatch(patchname, ui, strip, cwd, files, eolmode) |
|
1259 | 1258 | except PatchError, err: |
|
1260 | 1259 | s = str(err) |
|
1261 | 1260 | if s: |
|
1262 | 1261 | raise util.Abort(s) |
|
1263 | 1262 | else: |
|
1264 | 1263 | raise util.Abort(_('patch failed to apply')) |
|
1265 | 1264 | |
|
1266 | 1265 | def b85diff(to, tn): |
|
1267 | 1266 | '''print base85-encoded binary diff''' |
|
1268 | 1267 | def gitindex(text): |
|
1269 | 1268 | if not text: |
|
1270 | 1269 | return hex(nullid) |
|
1271 | 1270 | l = len(text) |
|
1272 | 1271 | s = util.sha1('blob %d\0' % l) |
|
1273 | 1272 | s.update(text) |
|
1274 | 1273 | return s.hexdigest() |
|
1275 | 1274 | |
|
1276 | 1275 | def fmtline(line): |
|
1277 | 1276 | l = len(line) |
|
1278 | 1277 | if l <= 26: |
|
1279 | 1278 | l = chr(ord('A') + l - 1) |
|
1280 | 1279 | else: |
|
1281 | 1280 | l = chr(l - 26 + ord('a') - 1) |
|
1282 | 1281 | return '%c%s\n' % (l, base85.b85encode(line, True)) |
|
1283 | 1282 | |
|
1284 | 1283 | def chunk(text, csize=52): |
|
1285 | 1284 | l = len(text) |
|
1286 | 1285 | i = 0 |
|
1287 | 1286 | while i < l: |
|
1288 | 1287 | yield text[i:i + csize] |
|
1289 | 1288 | i += csize |
|
1290 | 1289 | |
|
1291 | 1290 | tohash = gitindex(to) |
|
1292 | 1291 | tnhash = gitindex(tn) |
|
1293 | 1292 | if tohash == tnhash: |
|
1294 | 1293 | return "" |
|
1295 | 1294 | |
|
1296 | 1295 | # TODO: deltas |
|
1297 | 1296 | ret = ['index %s..%s\nGIT binary patch\nliteral %s\n' % |
|
1298 | 1297 | (tohash, tnhash, len(tn))] |
|
1299 | 1298 | for l in chunk(zlib.compress(tn)): |
|
1300 | 1299 | ret.append(fmtline(l)) |
|
1301 | 1300 | ret.append('\n') |
|
1302 | 1301 | return ''.join(ret) |
|
1303 | 1302 | |
|
1304 | 1303 | class GitDiffRequired(Exception): |
|
1305 | 1304 | pass |
|
1306 | 1305 | |
|
1307 | 1306 | def diffopts(ui, opts=None, untrusted=False): |
|
1308 | 1307 | def get(key, name=None, getter=ui.configbool): |
|
1309 | 1308 | return ((opts and opts.get(key)) or |
|
1310 | 1309 | getter('diff', name or key, None, untrusted=untrusted)) |
|
1311 | 1310 | return mdiff.diffopts( |
|
1312 | 1311 | text=opts and opts.get('text'), |
|
1313 | 1312 | git=get('git'), |
|
1314 | 1313 | nodates=get('nodates'), |
|
1315 | 1314 | showfunc=get('show_function', 'showfunc'), |
|
1316 | 1315 | ignorews=get('ignore_all_space', 'ignorews'), |
|
1317 | 1316 | ignorewsamount=get('ignore_space_change', 'ignorewsamount'), |
|
1318 | 1317 | ignoreblanklines=get('ignore_blank_lines', 'ignoreblanklines'), |
|
1319 | 1318 | context=get('unified', getter=ui.config)) |
|
1320 | 1319 | |
|
1321 | 1320 | def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None, |
|
1322 | 1321 | losedatafn=None, prefix=''): |
|
1323 | 1322 | '''yields diff of changes to files between two nodes, or node and |
|
1324 | 1323 | working directory. |
|
1325 | 1324 | |
|
1326 | 1325 | if node1 is None, use first dirstate parent instead. |
|
1327 | 1326 | if node2 is None, compare node1 with working directory. |
|
1328 | 1327 | |
|
1329 | 1328 | losedatafn(**kwarg) is a callable run when opts.upgrade=True and |
|
1330 | 1329 | every time some change cannot be represented with the current |
|
1331 | 1330 | patch format. Return False to upgrade to git patch format, True to |
|
1332 | 1331 | accept the loss or raise an exception to abort the diff. It is |
|
1333 | 1332 | called with the name of current file being diffed as 'fn'. If set |
|
1334 | 1333 | to None, patches will always be upgraded to git format when |
|
1335 | 1334 | necessary. |
|
1336 | 1335 | |
|
1337 | 1336 | prefix is a filename prefix that is prepended to all filenames on |
|
1338 | 1337 | display (used for subrepos). |
|
1339 | 1338 | ''' |
|
1340 | 1339 | |
|
1341 | 1340 | if opts is None: |
|
1342 | 1341 | opts = mdiff.defaultopts |
|
1343 | 1342 | |
|
1344 | 1343 | if not node1 and not node2: |
|
1345 | 1344 | node1 = repo.dirstate.parents()[0] |
|
1346 | 1345 | |
|
1347 | 1346 | def lrugetfilectx(): |
|
1348 | 1347 | cache = {} |
|
1349 | 1348 | order = [] |
|
1350 | 1349 | def getfilectx(f, ctx): |
|
1351 | 1350 | fctx = ctx.filectx(f, filelog=cache.get(f)) |
|
1352 | 1351 | if f not in cache: |
|
1353 | 1352 | if len(cache) > 20: |
|
1354 | 1353 | del cache[order.pop(0)] |
|
1355 | 1354 | cache[f] = fctx.filelog() |
|
1356 | 1355 | else: |
|
1357 | 1356 | order.remove(f) |
|
1358 | 1357 | order.append(f) |
|
1359 | 1358 | return fctx |
|
1360 | 1359 | return getfilectx |
|
1361 | 1360 | getfilectx = lrugetfilectx() |
|
1362 | 1361 | |
|
1363 | 1362 | ctx1 = repo[node1] |
|
1364 | 1363 | ctx2 = repo[node2] |
|
1365 | 1364 | |
|
1366 | 1365 | if not changes: |
|
1367 | 1366 | changes = repo.status(ctx1, ctx2, match=match) |
|
1368 | 1367 | modified, added, removed = changes[:3] |
|
1369 | 1368 | |
|
1370 | 1369 | if not modified and not added and not removed: |
|
1371 | 1370 | return [] |
|
1372 | 1371 | |
|
1373 | 1372 | revs = None |
|
1374 | 1373 | if not repo.ui.quiet: |
|
1375 | 1374 | hexfunc = repo.ui.debugflag and hex or short |
|
1376 | 1375 | revs = [hexfunc(node) for node in [node1, node2] if node] |
|
1377 | 1376 | |
|
1378 | 1377 | copy = {} |
|
1379 | 1378 | if opts.git or opts.upgrade: |
|
1380 | 1379 | copy = copies.copies(repo, ctx1, ctx2, repo[nullid])[0] |
|
1381 | 1380 | |
|
1382 | 1381 | difffn = lambda opts, losedata: trydiff(repo, revs, ctx1, ctx2, |
|
1383 | 1382 | modified, added, removed, copy, getfilectx, opts, losedata, prefix) |
|
1384 | 1383 | if opts.upgrade and not opts.git: |
|
1385 | 1384 | try: |
|
1386 | 1385 | def losedata(fn): |
|
1387 | 1386 | if not losedatafn or not losedatafn(fn=fn): |
|
1388 | 1387 | raise GitDiffRequired() |
|
1389 | 1388 | # Buffer the whole output until we are sure it can be generated |
|
1390 | 1389 | return list(difffn(opts.copy(git=False), losedata)) |
|
1391 | 1390 | except GitDiffRequired: |
|
1392 | 1391 | return difffn(opts.copy(git=True), None) |
|
1393 | 1392 | else: |
|
1394 | 1393 | return difffn(opts, None) |
|
1395 | 1394 | |
|
1396 | 1395 | def difflabel(func, *args, **kw): |
|
1397 | 1396 | '''yields 2-tuples of (output, label) based on the output of func()''' |
|
1398 | 1397 | prefixes = [('diff', 'diff.diffline'), |
|
1399 | 1398 | ('copy', 'diff.extended'), |
|
1400 | 1399 | ('rename', 'diff.extended'), |
|
1401 | 1400 | ('old', 'diff.extended'), |
|
1402 | 1401 | ('new', 'diff.extended'), |
|
1403 | 1402 | ('deleted', 'diff.extended'), |
|
1404 | 1403 | ('---', 'diff.file_a'), |
|
1405 | 1404 | ('+++', 'diff.file_b'), |
|
1406 | 1405 | ('@@', 'diff.hunk'), |
|
1407 | 1406 | ('-', 'diff.deleted'), |
|
1408 | 1407 | ('+', 'diff.inserted')] |
|
1409 | 1408 | |
|
1410 | 1409 | for chunk in func(*args, **kw): |
|
1411 | 1410 | lines = chunk.split('\n') |
|
1412 | 1411 | for i, line in enumerate(lines): |
|
1413 | 1412 | if i != 0: |
|
1414 | 1413 | yield ('\n', '') |
|
1415 | 1414 | stripline = line |
|
1416 | 1415 | if line and line[0] in '+-': |
|
1417 | 1416 | # highlight trailing whitespace, but only in changed lines |
|
1418 | 1417 | stripline = line.rstrip() |
|
1419 | 1418 | for prefix, label in prefixes: |
|
1420 | 1419 | if stripline.startswith(prefix): |
|
1421 | 1420 | yield (stripline, label) |
|
1422 | 1421 | break |
|
1423 | 1422 | else: |
|
1424 | 1423 | yield (line, '') |
|
1425 | 1424 | if line != stripline: |
|
1426 | 1425 | yield (line[len(stripline):], 'diff.trailingwhitespace') |
|
1427 | 1426 | |
|
1428 | 1427 | def diffui(*args, **kw): |
|
1429 | 1428 | '''like diff(), but yields 2-tuples of (output, label) for ui.write()''' |
|
1430 | 1429 | return difflabel(diff, *args, **kw) |
|
1431 | 1430 | |
|
1432 | 1431 | |
|
1433 | 1432 | def _addmodehdr(header, omode, nmode): |
|
1434 | 1433 | if omode != nmode: |
|
1435 | 1434 | header.append('old mode %s\n' % omode) |
|
1436 | 1435 | header.append('new mode %s\n' % nmode) |
|
1437 | 1436 | |
|
1438 | 1437 | def trydiff(repo, revs, ctx1, ctx2, modified, added, removed, |
|
1439 | 1438 | copy, getfilectx, opts, losedatafn, prefix): |
|
1440 | 1439 | |
|
1441 | 1440 | def join(f): |
|
1442 | 1441 | return os.path.join(prefix, f) |
|
1443 | 1442 | |
|
1444 | 1443 | date1 = util.datestr(ctx1.date()) |
|
1445 | 1444 | man1 = ctx1.manifest() |
|
1446 | 1445 | |
|
1447 | 1446 | gone = set() |
|
1448 | 1447 | gitmode = {'l': '120000', 'x': '100755', '': '100644'} |
|
1449 | 1448 | |
|
1450 | 1449 | copyto = dict([(v, k) for k, v in copy.items()]) |
|
1451 | 1450 | |
|
1452 | 1451 | if opts.git: |
|
1453 | 1452 | revs = None |
|
1454 | 1453 | |
|
1455 | 1454 | for f in sorted(modified + added + removed): |
|
1456 | 1455 | to = None |
|
1457 | 1456 | tn = None |
|
1458 | 1457 | dodiff = True |
|
1459 | 1458 | header = [] |
|
1460 | 1459 | if f in man1: |
|
1461 | 1460 | to = getfilectx(f, ctx1).data() |
|
1462 | 1461 | if f not in removed: |
|
1463 | 1462 | tn = getfilectx(f, ctx2).data() |
|
1464 | 1463 | a, b = f, f |
|
1465 | 1464 | if opts.git or losedatafn: |
|
1466 | 1465 | if f in added: |
|
1467 | 1466 | mode = gitmode[ctx2.flags(f)] |
|
1468 | 1467 | if f in copy or f in copyto: |
|
1469 | 1468 | if opts.git: |
|
1470 | 1469 | if f in copy: |
|
1471 | 1470 | a = copy[f] |
|
1472 | 1471 | else: |
|
1473 | 1472 | a = copyto[f] |
|
1474 | 1473 | omode = gitmode[man1.flags(a)] |
|
1475 | 1474 | _addmodehdr(header, omode, mode) |
|
1476 | 1475 | if a in removed and a not in gone: |
|
1477 | 1476 | op = 'rename' |
|
1478 | 1477 | gone.add(a) |
|
1479 | 1478 | else: |
|
1480 | 1479 | op = 'copy' |
|
1481 | 1480 | header.append('%s from %s\n' % (op, join(a))) |
|
1482 | 1481 | header.append('%s to %s\n' % (op, join(f))) |
|
1483 | 1482 | to = getfilectx(a, ctx1).data() |
|
1484 | 1483 | else: |
|
1485 | 1484 | losedatafn(f) |
|
1486 | 1485 | else: |
|
1487 | 1486 | if opts.git: |
|
1488 | 1487 | header.append('new file mode %s\n' % mode) |
|
1489 | 1488 | elif ctx2.flags(f): |
|
1490 | 1489 | losedatafn(f) |
|
1491 | 1490 | # In theory, if tn was copied or renamed we should check |
|
1492 | 1491 | # if the source is binary too but the copy record already |
|
1493 | 1492 | # forces git mode. |
|
1494 | 1493 | if util.binary(tn): |
|
1495 | 1494 | if opts.git: |
|
1496 | 1495 | dodiff = 'binary' |
|
1497 | 1496 | else: |
|
1498 | 1497 | losedatafn(f) |
|
1499 | 1498 | if not opts.git and not tn: |
|
1500 | 1499 | # regular diffs cannot represent new empty file |
|
1501 | 1500 | losedatafn(f) |
|
1502 | 1501 | elif f in removed: |
|
1503 | 1502 | if opts.git: |
|
1504 | 1503 | # have we already reported a copy above? |
|
1505 | 1504 | if ((f in copy and copy[f] in added |
|
1506 | 1505 | and copyto[copy[f]] == f) or |
|
1507 | 1506 | (f in copyto and copyto[f] in added |
|
1508 | 1507 | and copy[copyto[f]] == f)): |
|
1509 | 1508 | dodiff = False |
|
1510 | 1509 | else: |
|
1511 | 1510 | header.append('deleted file mode %s\n' % |
|
1512 | 1511 | gitmode[man1.flags(f)]) |
|
1513 | 1512 | elif not to or util.binary(to): |
|
1514 | 1513 | # regular diffs cannot represent empty file deletion |
|
1515 | 1514 | losedatafn(f) |
|
1516 | 1515 | else: |
|
1517 | 1516 | oflag = man1.flags(f) |
|
1518 | 1517 | nflag = ctx2.flags(f) |
|
1519 | 1518 | binary = util.binary(to) or util.binary(tn) |
|
1520 | 1519 | if opts.git: |
|
1521 | 1520 | _addmodehdr(header, gitmode[oflag], gitmode[nflag]) |
|
1522 | 1521 | if binary: |
|
1523 | 1522 | dodiff = 'binary' |
|
1524 | 1523 | elif binary or nflag != oflag: |
|
1525 | 1524 | losedatafn(f) |
|
1526 | 1525 | if opts.git: |
|
1527 | 1526 | header.insert(0, mdiff.diffline(revs, join(a), join(b), opts)) |
|
1528 | 1527 | |
|
1529 | 1528 | if dodiff: |
|
1530 | 1529 | if dodiff == 'binary': |
|
1531 | 1530 | text = b85diff(to, tn) |
|
1532 | 1531 | else: |
|
1533 | 1532 | text = mdiff.unidiff(to, date1, |
|
1534 | 1533 | # ctx2 date may be dynamic |
|
1535 | 1534 | tn, util.datestr(ctx2.date()), |
|
1536 | 1535 | join(a), join(b), revs, opts=opts) |
|
1537 | 1536 | if header and (text or len(header) > 1): |
|
1538 | 1537 | yield ''.join(header) |
|
1539 | 1538 | if text: |
|
1540 | 1539 | yield text |
|
1541 | 1540 | |
|
1542 | 1541 | def diffstatdata(lines): |
|
1543 | 1542 | filename, adds, removes = None, 0, 0 |
|
1544 | 1543 | for line in lines: |
|
1545 | 1544 | if line.startswith('diff'): |
|
1546 | 1545 | if filename: |
|
1547 | 1546 | isbinary = adds == 0 and removes == 0 |
|
1548 | 1547 | yield (filename, adds, removes, isbinary) |
|
1549 | 1548 | # set numbers to 0 anyway when starting new file |
|
1550 | 1549 | adds, removes = 0, 0 |
|
1551 | 1550 | if line.startswith('diff --git'): |
|
1552 | 1551 | filename = gitre.search(line).group(1) |
|
1553 | 1552 | else: |
|
1554 | 1553 | # format: "diff -r ... -r ... filename" |
|
1555 | 1554 | filename = line.split(None, 5)[-1] |
|
1556 | 1555 | elif line.startswith('+') and not line.startswith('+++'): |
|
1557 | 1556 | adds += 1 |
|
1558 | 1557 | elif line.startswith('-') and not line.startswith('---'): |
|
1559 | 1558 | removes += 1 |
|
1560 | 1559 | if filename: |
|
1561 | 1560 | isbinary = adds == 0 and removes == 0 |
|
1562 | 1561 | yield (filename, adds, removes, isbinary) |
|
1563 | 1562 | |
|
1564 | 1563 | def diffstat(lines, width=80, git=False): |
|
1565 | 1564 | output = [] |
|
1566 | 1565 | stats = list(diffstatdata(lines)) |
|
1567 | 1566 | |
|
1568 | 1567 | maxtotal, maxname = 0, 0 |
|
1569 | 1568 | totaladds, totalremoves = 0, 0 |
|
1570 | 1569 | hasbinary = False |
|
1571 | 1570 | |
|
1572 | 1571 | sized = [(filename, adds, removes, isbinary, encoding.colwidth(filename)) |
|
1573 | 1572 | for filename, adds, removes, isbinary in stats] |
|
1574 | 1573 | |
|
1575 | 1574 | for filename, adds, removes, isbinary, namewidth in sized: |
|
1576 | 1575 | totaladds += adds |
|
1577 | 1576 | totalremoves += removes |
|
1578 | 1577 | maxname = max(maxname, namewidth) |
|
1579 | 1578 | maxtotal = max(maxtotal, adds + removes) |
|
1580 | 1579 | if isbinary: |
|
1581 | 1580 | hasbinary = True |
|
1582 | 1581 | |
|
1583 | 1582 | countwidth = len(str(maxtotal)) |
|
1584 | 1583 | if hasbinary and countwidth < 3: |
|
1585 | 1584 | countwidth = 3 |
|
1586 | 1585 | graphwidth = width - countwidth - maxname - 6 |
|
1587 | 1586 | if graphwidth < 10: |
|
1588 | 1587 | graphwidth = 10 |
|
1589 | 1588 | |
|
1590 | 1589 | def scale(i): |
|
1591 | 1590 | if maxtotal <= graphwidth: |
|
1592 | 1591 | return i |
|
1593 | 1592 | # If diffstat runs out of room it doesn't print anything, |
|
1594 | 1593 | # which isn't very useful, so always print at least one + or - |
|
1595 | 1594 | # if there were at least some changes. |
|
1596 | 1595 | return max(i * graphwidth // maxtotal, int(bool(i))) |
|
1597 | 1596 | |
|
1598 | 1597 | for filename, adds, removes, isbinary, namewidth in sized: |
|
1599 | 1598 | if git and isbinary: |
|
1600 | 1599 | count = 'Bin' |
|
1601 | 1600 | else: |
|
1602 | 1601 | count = adds + removes |
|
1603 | 1602 | pluses = '+' * scale(adds) |
|
1604 | 1603 | minuses = '-' * scale(removes) |
|
1605 | 1604 | output.append(' %s%s | %*s %s%s\n' % |
|
1606 | 1605 | (filename, ' ' * (maxname - namewidth), |
|
1607 | 1606 | countwidth, count, |
|
1608 | 1607 | pluses, minuses)) |
|
1609 | 1608 | |
|
1610 | 1609 | if stats: |
|
1611 | 1610 | output.append(_(' %d files changed, %d insertions(+), %d deletions(-)\n') |
|
1612 | 1611 | % (len(stats), totaladds, totalremoves)) |
|
1613 | 1612 | |
|
1614 | 1613 | return ''.join(output) |
|
1615 | 1614 | |
|
1616 | 1615 | def diffstatui(*args, **kw): |
|
1617 | 1616 | '''like diffstat(), but yields 2-tuples of (output, label) for |
|
1618 | 1617 | ui.write() |
|
1619 | 1618 | ''' |
|
1620 | 1619 | |
|
1621 | 1620 | for line in diffstat(*args, **kw).splitlines(): |
|
1622 | 1621 | if line and line[-1] in '+-': |
|
1623 | 1622 | name, graph = line.rsplit(' ', 1) |
|
1624 | 1623 | yield (name + ' ', '') |
|
1625 | 1624 | m = re.search(r'\++', graph) |
|
1626 | 1625 | if m: |
|
1627 | 1626 | yield (m.group(0), 'diffstat.inserted') |
|
1628 | 1627 | m = re.search(r'-+', graph) |
|
1629 | 1628 | if m: |
|
1630 | 1629 | yield (m.group(0), 'diffstat.deleted') |
|
1631 | 1630 | else: |
|
1632 | 1631 | yield (line, '') |
|
1633 | 1632 | yield ('\n', '') |
General Comments 0
You need to be logged in to leave comments.
Login now