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