Show More
@@ -1,655 +1,662 b'' | |||
|
1 | 1 | # patch.py - patch file parsing routines |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006 Brendan Cully <brendan@kublai.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms |
|
6 | 6 | # of the GNU General Public License, incorporated herein by reference. |
|
7 | 7 | |
|
8 | 8 | from demandload import demandload |
|
9 | 9 | from i18n import gettext as _ |
|
10 | 10 | from node import * |
|
11 | 11 | demandload(globals(), "base85 cmdutil mdiff util") |
|
12 | 12 | demandload(globals(), "cStringIO email.Parser errno os popen2 re shutil sha") |
|
13 | 13 | demandload(globals(), "sys tempfile zlib") |
|
14 | 14 | |
|
15 | 15 | # helper functions |
|
16 | 16 | |
|
17 | 17 | def copyfile(src, dst, basedir=None): |
|
18 | 18 | if not basedir: |
|
19 | 19 | basedir = os.getcwd() |
|
20 | 20 | |
|
21 | 21 | abssrc, absdst = [os.path.join(basedir, n) for n in (src, dst)] |
|
22 | 22 | if os.path.exists(absdst): |
|
23 | 23 | raise util.Abort(_("cannot create %s: destination already exists") % |
|
24 | 24 | dst) |
|
25 | 25 | |
|
26 | 26 | targetdir = os.path.dirname(absdst) |
|
27 | 27 | if not os.path.isdir(targetdir): |
|
28 | 28 | os.makedirs(targetdir) |
|
29 | 29 | |
|
30 | 30 | util.copyfile(abssrc, absdst) |
|
31 | 31 | |
|
32 | 32 | # public functions |
|
33 | 33 | |
|
34 | 34 | def extract(ui, fileobj): |
|
35 | 35 | '''extract patch from data read from fileobj. |
|
36 | 36 | |
|
37 | 37 | patch can be normal patch or contained in email message. |
|
38 | 38 | |
|
39 | 39 | return tuple (filename, message, user, date). any item in returned |
|
40 | 40 | tuple can be None. if filename is None, fileobj did not contain |
|
41 | 41 | patch. caller must unlink filename when done.''' |
|
42 | 42 | |
|
43 | 43 | # attempt to detect the start of a patch |
|
44 | 44 | # (this heuristic is borrowed from quilt) |
|
45 | 45 | diffre = re.compile(r'^(?:Index:[ \t]|diff[ \t]|RCS file: |' + |
|
46 | 46 | 'retrieving revision [0-9]+(\.[0-9]+)*$|' + |
|
47 | 47 | '(---|\*\*\*)[ \t])', re.MULTILINE) |
|
48 | 48 | |
|
49 | 49 | fd, tmpname = tempfile.mkstemp(prefix='hg-patch-') |
|
50 | 50 | tmpfp = os.fdopen(fd, 'w') |
|
51 | 51 | try: |
|
52 | 52 | hgpatch = False |
|
53 | 53 | |
|
54 | 54 | msg = email.Parser.Parser().parse(fileobj) |
|
55 | 55 | |
|
56 | 56 | message = msg['Subject'] |
|
57 | 57 | user = msg['From'] |
|
58 | 58 | # should try to parse msg['Date'] |
|
59 | 59 | date = None |
|
60 | 60 | |
|
61 | 61 | if message: |
|
62 | 62 | message = message.replace('\n\t', ' ') |
|
63 | 63 | ui.debug('Subject: %s\n' % message) |
|
64 | 64 | if user: |
|
65 | 65 | ui.debug('From: %s\n' % user) |
|
66 | 66 | diffs_seen = 0 |
|
67 | 67 | ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') |
|
68 | 68 | |
|
69 | 69 | for part in msg.walk(): |
|
70 | 70 | content_type = part.get_content_type() |
|
71 | 71 | ui.debug('Content-Type: %s\n' % content_type) |
|
72 | 72 | if content_type not in ok_types: |
|
73 | 73 | continue |
|
74 | 74 | payload = part.get_payload(decode=True) |
|
75 | 75 | m = diffre.search(payload) |
|
76 | 76 | if m: |
|
77 | 77 | ui.debug(_('found patch at byte %d\n') % m.start(0)) |
|
78 | 78 | diffs_seen += 1 |
|
79 | 79 | cfp = cStringIO.StringIO() |
|
80 | 80 | if message: |
|
81 | 81 | cfp.write(message) |
|
82 | 82 | cfp.write('\n') |
|
83 | 83 | for line in payload[:m.start(0)].splitlines(): |
|
84 | 84 | if line.startswith('# HG changeset patch'): |
|
85 | 85 | ui.debug(_('patch generated by hg export\n')) |
|
86 | 86 | hgpatch = True |
|
87 | 87 | # drop earlier commit message content |
|
88 | 88 | cfp.seek(0) |
|
89 | 89 | cfp.truncate() |
|
90 | 90 | elif hgpatch: |
|
91 | 91 | if line.startswith('# User '): |
|
92 | 92 | user = line[7:] |
|
93 | 93 | ui.debug('From: %s\n' % user) |
|
94 | 94 | elif line.startswith("# Date "): |
|
95 | 95 | date = line[7:] |
|
96 | 96 | if not line.startswith('# '): |
|
97 | 97 | cfp.write(line) |
|
98 | 98 | cfp.write('\n') |
|
99 | 99 | message = cfp.getvalue() |
|
100 | 100 | if tmpfp: |
|
101 | 101 | tmpfp.write(payload) |
|
102 | 102 | if not payload.endswith('\n'): |
|
103 | 103 | tmpfp.write('\n') |
|
104 | 104 | elif not diffs_seen and message and content_type == 'text/plain': |
|
105 | 105 | message += '\n' + payload |
|
106 | 106 | except: |
|
107 | 107 | tmpfp.close() |
|
108 | 108 | os.unlink(tmpname) |
|
109 | 109 | raise |
|
110 | 110 | |
|
111 | 111 | tmpfp.close() |
|
112 | 112 | if not diffs_seen: |
|
113 | 113 | os.unlink(tmpname) |
|
114 | 114 | return None, message, user, date |
|
115 | 115 | return tmpname, message, user, date |
|
116 | 116 | |
|
117 | 117 | def readgitpatch(patchname): |
|
118 | 118 | """extract git-style metadata about patches from <patchname>""" |
|
119 | 119 | class gitpatch: |
|
120 | 120 | "op is one of ADD, DELETE, RENAME, MODIFY or COPY" |
|
121 | 121 | def __init__(self, path): |
|
122 | 122 | self.path = path |
|
123 | 123 | self.oldpath = None |
|
124 | 124 | self.mode = None |
|
125 | 125 | self.op = 'MODIFY' |
|
126 | 126 | self.copymod = False |
|
127 | 127 | self.lineno = 0 |
|
128 | 128 | self.binary = False |
|
129 | 129 | |
|
130 | 130 | # Filter patch for git information |
|
131 | 131 | gitre = re.compile('diff --git a/(.*) b/(.*)') |
|
132 | 132 | pf = file(patchname) |
|
133 | 133 | gp = None |
|
134 | 134 | gitpatches = [] |
|
135 | 135 | # Can have a git patch with only metadata, causing patch to complain |
|
136 | 136 | dopatch = False |
|
137 | 137 | |
|
138 | 138 | lineno = 0 |
|
139 | 139 | for line in pf: |
|
140 | 140 | lineno += 1 |
|
141 | 141 | if line.startswith('diff --git'): |
|
142 | 142 | m = gitre.match(line) |
|
143 | 143 | if m: |
|
144 | 144 | if gp: |
|
145 | 145 | gitpatches.append(gp) |
|
146 | 146 | src, dst = m.group(1, 2) |
|
147 | 147 | gp = gitpatch(dst) |
|
148 | 148 | gp.lineno = lineno |
|
149 | 149 | elif gp: |
|
150 | 150 | if line.startswith('--- '): |
|
151 | 151 | if gp.op in ('COPY', 'RENAME'): |
|
152 | 152 | gp.copymod = True |
|
153 | 153 | dopatch = 'filter' |
|
154 | 154 | gitpatches.append(gp) |
|
155 | 155 | gp = None |
|
156 | 156 | if not dopatch: |
|
157 | 157 | dopatch = True |
|
158 | 158 | continue |
|
159 | 159 | if line.startswith('rename from '): |
|
160 | 160 | gp.op = 'RENAME' |
|
161 | 161 | gp.oldpath = line[12:].rstrip() |
|
162 | 162 | elif line.startswith('rename to '): |
|
163 | 163 | gp.path = line[10:].rstrip() |
|
164 | 164 | elif line.startswith('copy from '): |
|
165 | 165 | gp.op = 'COPY' |
|
166 | 166 | gp.oldpath = line[10:].rstrip() |
|
167 | 167 | elif line.startswith('copy to '): |
|
168 | 168 | gp.path = line[8:].rstrip() |
|
169 | 169 | elif line.startswith('deleted file'): |
|
170 | 170 | gp.op = 'DELETE' |
|
171 | 171 | elif line.startswith('new file mode '): |
|
172 | 172 | gp.op = 'ADD' |
|
173 | 173 | gp.mode = int(line.rstrip()[-3:], 8) |
|
174 | 174 | elif line.startswith('new mode '): |
|
175 | 175 | gp.mode = int(line.rstrip()[-3:], 8) |
|
176 | 176 | elif line.startswith('GIT binary patch'): |
|
177 | 177 | if not dopatch: |
|
178 | 178 | dopatch = 'binary' |
|
179 | 179 | gp.binary = True |
|
180 | 180 | if gp: |
|
181 | 181 | gitpatches.append(gp) |
|
182 | 182 | |
|
183 | 183 | if not gitpatches: |
|
184 | 184 | dopatch = True |
|
185 | 185 | |
|
186 | 186 | return (dopatch, gitpatches) |
|
187 | 187 | |
|
188 | 188 | def dogitpatch(patchname, gitpatches, cwd=None): |
|
189 | 189 | """Preprocess git patch so that vanilla patch can handle it""" |
|
190 | 190 | def extractbin(fp): |
|
191 | 191 | line = fp.readline().rstrip() |
|
192 | 192 | while line and not line.startswith('literal '): |
|
193 | 193 | line = fp.readline().rstrip() |
|
194 | 194 | if not line: |
|
195 | 195 | return |
|
196 | 196 | size = int(line[8:]) |
|
197 | 197 | dec = [] |
|
198 | 198 | line = fp.readline().rstrip() |
|
199 | 199 | while line: |
|
200 | 200 | l = line[0] |
|
201 | 201 | if l <= 'Z' and l >= 'A': |
|
202 | 202 | l = ord(l) - ord('A') + 1 |
|
203 | 203 | else: |
|
204 | 204 | l = ord(l) - ord('a') + 27 |
|
205 | 205 | dec.append(base85.b85decode(line[1:])[:l]) |
|
206 | 206 | line = fp.readline().rstrip() |
|
207 | 207 | text = zlib.decompress(''.join(dec)) |
|
208 | 208 | if len(text) != size: |
|
209 | 209 | raise util.Abort(_('binary patch is %d bytes, not %d') % |
|
210 | 210 | (len(text), size)) |
|
211 | 211 | return text |
|
212 | 212 | |
|
213 | 213 | pf = file(patchname) |
|
214 | 214 | pfline = 1 |
|
215 | 215 | |
|
216 | 216 | fd, patchname = tempfile.mkstemp(prefix='hg-patch-') |
|
217 | 217 | tmpfp = os.fdopen(fd, 'w') |
|
218 | 218 | |
|
219 | 219 | try: |
|
220 | 220 | for i in xrange(len(gitpatches)): |
|
221 | 221 | p = gitpatches[i] |
|
222 | 222 | if not p.copymod and not p.binary: |
|
223 | 223 | continue |
|
224 | 224 | |
|
225 | 225 | # rewrite patch hunk |
|
226 | 226 | while pfline < p.lineno: |
|
227 | 227 | tmpfp.write(pf.readline()) |
|
228 | 228 | pfline += 1 |
|
229 | 229 | |
|
230 | 230 | if p.binary: |
|
231 | 231 | text = extractbin(pf) |
|
232 | 232 | if not text: |
|
233 | 233 | raise util.Abort(_('binary patch extraction failed')) |
|
234 | 234 | if not cwd: |
|
235 | 235 | cwd = os.getcwd() |
|
236 | 236 | absdst = os.path.join(cwd, p.path) |
|
237 | 237 | basedir = os.path.dirname(absdst) |
|
238 | 238 | if not os.path.isdir(basedir): |
|
239 | 239 | os.makedirs(basedir) |
|
240 | 240 | out = file(absdst, 'wb') |
|
241 | 241 | out.write(text) |
|
242 | 242 | out.close() |
|
243 | 243 | elif p.copymod: |
|
244 | 244 | copyfile(p.oldpath, p.path, basedir=cwd) |
|
245 | 245 | tmpfp.write('diff --git a/%s b/%s\n' % (p.path, p.path)) |
|
246 | 246 | line = pf.readline() |
|
247 | 247 | pfline += 1 |
|
248 | 248 | while not line.startswith('--- a/'): |
|
249 | 249 | tmpfp.write(line) |
|
250 | 250 | line = pf.readline() |
|
251 | 251 | pfline += 1 |
|
252 | 252 | tmpfp.write('--- a/%s\n' % p.path) |
|
253 | 253 | |
|
254 | 254 | line = pf.readline() |
|
255 | 255 | while line: |
|
256 | 256 | tmpfp.write(line) |
|
257 | 257 | line = pf.readline() |
|
258 | 258 | except: |
|
259 | 259 | tmpfp.close() |
|
260 | 260 | os.unlink(patchname) |
|
261 | 261 | raise |
|
262 | 262 | |
|
263 | 263 | tmpfp.close() |
|
264 | 264 | return patchname |
|
265 | 265 | |
|
266 | 266 | def patch(patchname, ui, strip=1, cwd=None, files={}): |
|
267 | 267 | """apply the patch <patchname> to the working directory. |
|
268 | 268 | a list of patched files is returned""" |
|
269 | 269 | |
|
270 | 270 | # helper function |
|
271 | 271 | def __patch(patchname): |
|
272 | 272 | """patch and updates the files and fuzz variables""" |
|
273 | 273 | fuzz = False |
|
274 | 274 | |
|
275 | 275 | patcher = util.find_in_path('gpatch', os.environ.get('PATH', ''), |
|
276 | 276 | 'patch') |
|
277 | 277 | args = [] |
|
278 | 278 | if cwd: |
|
279 | 279 | args.append('-d %s' % util.shellquote(cwd)) |
|
280 | 280 | fp = os.popen('%s %s -p%d < %s' % (patcher, ' '.join(args), strip, |
|
281 | 281 | util.shellquote(patchname))) |
|
282 | 282 | |
|
283 | 283 | for line in fp: |
|
284 | 284 | line = line.rstrip() |
|
285 | 285 | ui.note(line + '\n') |
|
286 | 286 | if line.startswith('patching file '): |
|
287 | 287 | pf = util.parse_patch_output(line) |
|
288 | 288 | printed_file = False |
|
289 | 289 | files.setdefault(pf, (None, None)) |
|
290 | 290 | elif line.find('with fuzz') >= 0: |
|
291 | 291 | fuzz = True |
|
292 | 292 | if not printed_file: |
|
293 | 293 | ui.warn(pf + '\n') |
|
294 | 294 | printed_file = True |
|
295 | 295 | ui.warn(line + '\n') |
|
296 | 296 | elif line.find('saving rejects to file') >= 0: |
|
297 | 297 | ui.warn(line + '\n') |
|
298 | 298 | elif line.find('FAILED') >= 0: |
|
299 | 299 | if not printed_file: |
|
300 | 300 | ui.warn(pf + '\n') |
|
301 | 301 | printed_file = True |
|
302 | 302 | ui.warn(line + '\n') |
|
303 | 303 | code = fp.close() |
|
304 | 304 | if code: |
|
305 | 305 | raise util.Abort(_("patch command failed: %s") % |
|
306 | 306 | util.explain_exit(code)[0]) |
|
307 | 307 | return fuzz |
|
308 | 308 | |
|
309 | 309 | (dopatch, gitpatches) = readgitpatch(patchname) |
|
310 | 310 | for gp in gitpatches: |
|
311 | 311 | files[gp.path] = (gp.op, gp) |
|
312 | 312 | |
|
313 | 313 | fuzz = False |
|
314 | 314 | if dopatch: |
|
315 | 315 | if dopatch in ('filter', 'binary'): |
|
316 | 316 | patchname = dogitpatch(patchname, gitpatches, cwd=cwd) |
|
317 | 317 | try: |
|
318 | 318 | if dopatch != 'binary': |
|
319 | 319 | fuzz = __patch(patchname) |
|
320 | 320 | finally: |
|
321 | 321 | if dopatch == 'filter': |
|
322 | 322 | os.unlink(patchname) |
|
323 | 323 | |
|
324 | 324 | return fuzz |
|
325 | 325 | |
|
326 | 326 | def diffopts(ui, opts={}, untrusted=False): |
|
327 | 327 | def get(key, name=None): |
|
328 | 328 | return (opts.get(key) or |
|
329 | 329 | ui.configbool('diff', name or key, None, untrusted=untrusted)) |
|
330 | 330 | return mdiff.diffopts( |
|
331 | 331 | text=opts.get('text'), |
|
332 | 332 | git=get('git'), |
|
333 | 333 | nodates=get('nodates'), |
|
334 | 334 | showfunc=get('show_function', 'showfunc'), |
|
335 | 335 | ignorews=get('ignore_all_space', 'ignorews'), |
|
336 | 336 | ignorewsamount=get('ignore_space_change', 'ignorewsamount'), |
|
337 | 337 | ignoreblanklines=get('ignore_blank_lines', 'ignoreblanklines')) |
|
338 | 338 | |
|
339 | 339 | def updatedir(ui, repo, patches, wlock=None): |
|
340 | 340 | '''Update dirstate after patch application according to metadata''' |
|
341 | 341 | if not patches: |
|
342 | 342 | return |
|
343 | 343 | copies = [] |
|
344 | 344 | removes = [] |
|
345 | 345 | cfiles = patches.keys() |
|
346 | 346 | cwd = repo.getcwd() |
|
347 | 347 | if cwd: |
|
348 | 348 | cfiles = [util.pathto(cwd, f) for f in patches.keys()] |
|
349 | 349 | for f in patches: |
|
350 | 350 | ctype, gp = patches[f] |
|
351 | 351 | if ctype == 'RENAME': |
|
352 | 352 | copies.append((gp.oldpath, gp.path, gp.copymod)) |
|
353 | 353 | removes.append(gp.oldpath) |
|
354 | 354 | elif ctype == 'COPY': |
|
355 | 355 | copies.append((gp.oldpath, gp.path, gp.copymod)) |
|
356 | 356 | elif ctype == 'DELETE': |
|
357 | 357 | removes.append(gp.path) |
|
358 | 358 | for src, dst, after in copies: |
|
359 | 359 | if not after: |
|
360 | 360 | copyfile(src, dst, repo.root) |
|
361 | 361 | repo.copy(src, dst, wlock=wlock) |
|
362 | 362 | if removes: |
|
363 | 363 | repo.remove(removes, True, wlock=wlock) |
|
364 | 364 | for f in patches: |
|
365 | 365 | ctype, gp = patches[f] |
|
366 | 366 | if gp and gp.mode: |
|
367 | 367 | x = gp.mode & 0100 != 0 |
|
368 | 368 | dst = os.path.join(repo.root, gp.path) |
|
369 | 369 | # patch won't create empty files |
|
370 | 370 | if ctype == 'ADD' and not os.path.exists(dst): |
|
371 | 371 | repo.wwrite(gp.path, '') |
|
372 | 372 | util.set_exec(dst, x) |
|
373 | 373 | cmdutil.addremove(repo, cfiles, wlock=wlock) |
|
374 | 374 | files = patches.keys() |
|
375 | 375 | files.extend([r for r in removes if r not in files]) |
|
376 | 376 | files.sort() |
|
377 | 377 | |
|
378 | 378 | return files |
|
379 | 379 | |
|
380 | 380 | def b85diff(fp, to, tn): |
|
381 | 381 | '''print base85-encoded binary diff''' |
|
382 | 382 | def gitindex(text): |
|
383 | 383 | if not text: |
|
384 | 384 | return '0' * 40 |
|
385 | 385 | l = len(text) |
|
386 | 386 | s = sha.new('blob %d\0' % l) |
|
387 | 387 | s.update(text) |
|
388 | 388 | return s.hexdigest() |
|
389 | 389 | |
|
390 | 390 | def fmtline(line): |
|
391 | 391 | l = len(line) |
|
392 | 392 | if l <= 26: |
|
393 | 393 | l = chr(ord('A') + l - 1) |
|
394 | 394 | else: |
|
395 | 395 | l = chr(l - 26 + ord('a') - 1) |
|
396 | 396 | return '%c%s\n' % (l, base85.b85encode(line, True)) |
|
397 | 397 | |
|
398 | 398 | def chunk(text, csize=52): |
|
399 | 399 | l = len(text) |
|
400 | 400 | i = 0 |
|
401 | 401 | while i < l: |
|
402 | 402 | yield text[i:i+csize] |
|
403 | 403 | i += csize |
|
404 | 404 | |
|
405 | 405 | # TODO: deltas |
|
406 | 406 | l = len(tn) |
|
407 | 407 | fp.write('index %s..%s\nGIT binary patch\nliteral %s\n' % |
|
408 | 408 | (gitindex(to), gitindex(tn), len(tn))) |
|
409 | 409 | |
|
410 | 410 | tn = ''.join([fmtline(l) for l in chunk(zlib.compress(tn))]) |
|
411 | 411 | fp.write(tn) |
|
412 | 412 | fp.write('\n') |
|
413 | 413 | |
|
414 | 414 | def diff(repo, node1=None, node2=None, files=None, match=util.always, |
|
415 | 415 | fp=None, changes=None, opts=None): |
|
416 | 416 | '''print diff of changes to files between two nodes, or node and |
|
417 | 417 | working directory. |
|
418 | 418 | |
|
419 | 419 | if node1 is None, use first dirstate parent instead. |
|
420 | 420 | if node2 is None, compare node1 with working directory.''' |
|
421 | 421 | |
|
422 | 422 | if opts is None: |
|
423 | 423 | opts = mdiff.defaultopts |
|
424 | 424 | if fp is None: |
|
425 | 425 | fp = repo.ui |
|
426 | 426 | |
|
427 | 427 | if not node1: |
|
428 | 428 | node1 = repo.dirstate.parents()[0] |
|
429 | 429 | |
|
430 | 430 | clcache = {} |
|
431 | 431 | def getchangelog(n): |
|
432 | 432 | if n not in clcache: |
|
433 | 433 | clcache[n] = repo.changelog.read(n) |
|
434 | 434 | return clcache[n] |
|
435 | 435 | mcache = {} |
|
436 | 436 | def getmanifest(n): |
|
437 | 437 | if n not in mcache: |
|
438 | 438 | mcache[n] = repo.manifest.read(n) |
|
439 | 439 | return mcache[n] |
|
440 | 440 | fcache = {} |
|
441 | 441 | def getfile(f): |
|
442 | 442 | if f not in fcache: |
|
443 | 443 | fcache[f] = repo.file(f) |
|
444 | 444 | return fcache[f] |
|
445 | 445 | |
|
446 | 446 | # reading the data for node1 early allows it to play nicely |
|
447 | 447 | # with repo.status and the revlog cache. |
|
448 | 448 | change = getchangelog(node1) |
|
449 | 449 | mmap = getmanifest(change[0]) |
|
450 | 450 | date1 = util.datestr(change[2]) |
|
451 | 451 | |
|
452 | 452 | if not changes: |
|
453 | 453 | changes = repo.status(node1, node2, files, match=match)[:5] |
|
454 | 454 | modified, added, removed, deleted, unknown = changes |
|
455 | 455 | if files: |
|
456 | 456 | def filterfiles(filters): |
|
457 | 457 | l = [x for x in filters if x in files] |
|
458 | 458 | |
|
459 | 459 | for t in files: |
|
460 | 460 | if not t.endswith("/"): |
|
461 | 461 | t += "/" |
|
462 | 462 | l += [x for x in filters if x.startswith(t)] |
|
463 | 463 | return l |
|
464 | 464 | |
|
465 | 465 | modified, added, removed = map(filterfiles, (modified, added, removed)) |
|
466 | 466 | |
|
467 | 467 | if not modified and not added and not removed: |
|
468 | 468 | return |
|
469 | 469 | |
|
470 | 470 | def renamedbetween(f, n1, n2): |
|
471 | 471 | r1, r2 = map(repo.changelog.rev, (n1, n2)) |
|
472 | orig = f | |
|
472 | 473 | src = None |
|
473 | 474 | while r2 > r1: |
|
474 | 475 | cl = getchangelog(n2) |
|
475 | 476 | if f in cl[3]: |
|
476 | 477 | m = getmanifest(cl[0]) |
|
477 | 478 | try: |
|
478 | 479 | src = getfile(f).renamed(m[f]) |
|
479 | 480 | except KeyError: |
|
480 | 481 | return None |
|
481 | 482 | if src: |
|
482 | 483 | f = src[0] |
|
483 | 484 | n2 = repo.changelog.parents(n2)[0] |
|
484 | 485 | r2 = repo.changelog.rev(n2) |
|
485 | return src | |
|
486 | if orig == f: | |
|
487 | return None | |
|
488 | cl = getchangelog(n1) | |
|
489 | m = getmanifest(cl[0]) | |
|
490 | if f not in m: | |
|
491 | return None | |
|
492 | return f, m[f] | |
|
486 | 493 | |
|
487 | 494 | if node2: |
|
488 | 495 | change = getchangelog(node2) |
|
489 | 496 | mmap2 = getmanifest(change[0]) |
|
490 | 497 | _date2 = util.datestr(change[2]) |
|
491 | 498 | def date2(f): |
|
492 | 499 | return _date2 |
|
493 | 500 | def read(f): |
|
494 | 501 | return getfile(f).read(mmap2[f]) |
|
495 | 502 | def renamed(f): |
|
496 | 503 | return renamedbetween(f, node1, node2) |
|
497 | 504 | else: |
|
498 | 505 | tz = util.makedate()[1] |
|
499 | 506 | _date2 = util.datestr() |
|
500 | 507 | def date2(f): |
|
501 | 508 | try: |
|
502 | 509 | return util.datestr((os.lstat(repo.wjoin(f)).st_mtime, tz)) |
|
503 | 510 | except OSError, err: |
|
504 | 511 | if err.errno != errno.ENOENT: raise |
|
505 | 512 | return _date2 |
|
506 | 513 | def read(f): |
|
507 | 514 | return repo.wread(f) |
|
508 | 515 | def renamed(f): |
|
509 | 516 | src = repo.dirstate.copied(f) |
|
510 | 517 | parent = repo.dirstate.parents()[0] |
|
511 | 518 | if src: |
|
512 | 519 | f = src[0] |
|
513 | 520 | of = renamedbetween(f, node1, parent) |
|
514 | 521 | if of: |
|
515 | 522 | return of |
|
516 | 523 | elif src: |
|
517 | 524 | cl = getchangelog(parent)[0] |
|
518 | 525 | return (src, getmanifest(cl)[src]) |
|
519 | 526 | else: |
|
520 | 527 | return None |
|
521 | 528 | |
|
522 | 529 | if repo.ui.quiet: |
|
523 | 530 | r = None |
|
524 | 531 | else: |
|
525 | 532 | hexfunc = repo.ui.debugflag and hex or short |
|
526 | 533 | r = [hexfunc(node) for node in [node1, node2] if node] |
|
527 | 534 | |
|
528 | 535 | if opts.git: |
|
529 | 536 | copied = {} |
|
530 | 537 | for f in added: |
|
531 | 538 | src = renamed(f) |
|
532 | 539 | if src: |
|
533 | 540 | copied[f] = src |
|
534 | 541 | srcs = [x[1][0] for x in copied.items()] |
|
535 | 542 | |
|
536 | 543 | all = modified + added + removed |
|
537 | 544 | all.sort() |
|
538 | 545 | for f in all: |
|
539 | 546 | to = None |
|
540 | 547 | tn = None |
|
541 | 548 | dodiff = True |
|
542 | 549 | header = [] |
|
543 | 550 | if f in mmap: |
|
544 | 551 | to = getfile(f).read(mmap[f]) |
|
545 | 552 | if f not in removed: |
|
546 | 553 | tn = read(f) |
|
547 | 554 | if opts.git: |
|
548 | 555 | def gitmode(x): |
|
549 | 556 | return x and '100755' or '100644' |
|
550 | 557 | def addmodehdr(header, omode, nmode): |
|
551 | 558 | if omode != nmode: |
|
552 | 559 | header.append('old mode %s\n' % omode) |
|
553 | 560 | header.append('new mode %s\n' % nmode) |
|
554 | 561 | |
|
555 | 562 | a, b = f, f |
|
556 | 563 | if f in added: |
|
557 | 564 | if node2: |
|
558 | 565 | mode = gitmode(mmap2.execf(f)) |
|
559 | 566 | else: |
|
560 | 567 | mode = gitmode(util.is_exec(repo.wjoin(f), None)) |
|
561 | 568 | if f in copied: |
|
562 | 569 | a, arev = copied[f] |
|
563 | 570 | omode = gitmode(mmap.execf(a)) |
|
564 | 571 | addmodehdr(header, omode, mode) |
|
565 | 572 | op = a in removed and 'rename' or 'copy' |
|
566 | 573 | header.append('%s from %s\n' % (op, a)) |
|
567 | 574 | header.append('%s to %s\n' % (op, f)) |
|
568 | 575 | to = getfile(a).read(arev) |
|
569 | 576 | else: |
|
570 | 577 | header.append('new file mode %s\n' % mode) |
|
571 | 578 | if util.binary(tn): |
|
572 | 579 | dodiff = 'binary' |
|
573 | 580 | elif f in removed: |
|
574 | 581 | if f in srcs: |
|
575 | 582 | dodiff = False |
|
576 | 583 | else: |
|
577 | 584 | mode = gitmode(mmap.execf(f)) |
|
578 | 585 | header.append('deleted file mode %s\n' % mode) |
|
579 | 586 | else: |
|
580 | 587 | omode = gitmode(mmap.execf(f)) |
|
581 | 588 | if node2: |
|
582 | 589 | nmode = gitmode(mmap2.execf(f)) |
|
583 | 590 | else: |
|
584 | 591 | nmode = gitmode(util.is_exec(repo.wjoin(f), mmap.execf(f))) |
|
585 | 592 | addmodehdr(header, omode, nmode) |
|
586 | 593 | if util.binary(to) or util.binary(tn): |
|
587 | 594 | dodiff = 'binary' |
|
588 | 595 | r = None |
|
589 | 596 | header.insert(0, 'diff --git a/%s b/%s\n' % (a, b)) |
|
590 | 597 | if dodiff == 'binary': |
|
591 | 598 | fp.write(''.join(header)) |
|
592 | 599 | b85diff(fp, to, tn) |
|
593 | 600 | elif dodiff: |
|
594 | 601 | text = mdiff.unidiff(to, date1, tn, date2(f), f, r, opts=opts) |
|
595 | 602 | if text or len(header) > 1: |
|
596 | 603 | fp.write(''.join(header)) |
|
597 | 604 | fp.write(text) |
|
598 | 605 | |
|
599 | 606 | def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False, |
|
600 | 607 | opts=None): |
|
601 | 608 | '''export changesets as hg patches.''' |
|
602 | 609 | |
|
603 | 610 | total = len(revs) |
|
604 | 611 | revwidth = max(map(len, revs)) |
|
605 | 612 | |
|
606 | 613 | def single(node, seqno, fp): |
|
607 | 614 | parents = [p for p in repo.changelog.parents(node) if p != nullid] |
|
608 | 615 | if switch_parent: |
|
609 | 616 | parents.reverse() |
|
610 | 617 | prev = (parents and parents[0]) or nullid |
|
611 | 618 | change = repo.changelog.read(node) |
|
612 | 619 | |
|
613 | 620 | if not fp: |
|
614 | 621 | fp = cmdutil.make_file(repo, template, node, total=total, |
|
615 | 622 | seqno=seqno, revwidth=revwidth) |
|
616 | 623 | if fp not in (sys.stdout, repo.ui): |
|
617 | 624 | repo.ui.note("%s\n" % fp.name) |
|
618 | 625 | |
|
619 | 626 | fp.write("# HG changeset patch\n") |
|
620 | 627 | fp.write("# User %s\n" % change[1]) |
|
621 | 628 | fp.write("# Date %d %d\n" % change[2]) |
|
622 | 629 | fp.write("# Node ID %s\n" % hex(node)) |
|
623 | 630 | fp.write("# Parent %s\n" % hex(prev)) |
|
624 | 631 | if len(parents) > 1: |
|
625 | 632 | fp.write("# Parent %s\n" % hex(parents[1])) |
|
626 | 633 | fp.write(change[4].rstrip()) |
|
627 | 634 | fp.write("\n\n") |
|
628 | 635 | |
|
629 | 636 | diff(repo, prev, node, fp=fp, opts=opts) |
|
630 | 637 | if fp not in (sys.stdout, repo.ui): |
|
631 | 638 | fp.close() |
|
632 | 639 | |
|
633 | 640 | for seqno, cset in enumerate(revs): |
|
634 | 641 | single(cset, seqno, fp) |
|
635 | 642 | |
|
636 | 643 | def diffstat(patchlines): |
|
637 | 644 | fd, name = tempfile.mkstemp(prefix="hg-patchbomb-", suffix=".txt") |
|
638 | 645 | try: |
|
639 | 646 | p = popen2.Popen3('diffstat -p1 -w79 2>/dev/null > ' + name) |
|
640 | 647 | try: |
|
641 | 648 | for line in patchlines: print >> p.tochild, line |
|
642 | 649 | p.tochild.close() |
|
643 | 650 | if p.wait(): return |
|
644 | 651 | fp = os.fdopen(fd, 'r') |
|
645 | 652 | stat = [] |
|
646 | 653 | for line in fp: stat.append(line.lstrip()) |
|
647 | 654 | last = stat.pop() |
|
648 | 655 | stat.insert(0, last) |
|
649 | 656 | stat = ''.join(stat) |
|
650 | 657 | if stat.startswith('0 files'): raise ValueError |
|
651 | 658 | return stat |
|
652 | 659 | except: raise |
|
653 | 660 | finally: |
|
654 | 661 | try: os.unlink(name) |
|
655 | 662 | except: pass |
@@ -1,75 +1,96 b'' | |||
|
1 | 1 | #!/bin/sh |
|
2 | 2 | |
|
3 | 3 | hg init a |
|
4 | 4 | cd a |
|
5 | 5 | |
|
6 | 6 | echo start > start |
|
7 | 7 | hg ci -Amstart -d '0 0' |
|
8 | 8 | echo new > new |
|
9 | 9 | hg ci -Amnew -d '0 0' |
|
10 | 10 | echo '% new file' |
|
11 | 11 | hg diff --git -r 0 |
|
12 | 12 | |
|
13 | 13 | hg cp new copy |
|
14 | 14 | hg ci -mcopy -d '0 0' |
|
15 | 15 | echo '% copy' |
|
16 | 16 | hg diff --git -r 1:tip |
|
17 | 17 | |
|
18 | 18 | hg mv copy rename |
|
19 | 19 | hg ci -mrename -d '0 0' |
|
20 | 20 | echo '% rename' |
|
21 | 21 | hg diff --git -r 2:tip |
|
22 | 22 | |
|
23 | 23 | hg rm rename |
|
24 | 24 | hg ci -mdelete -d '0 0' |
|
25 | 25 | echo '% delete' |
|
26 | 26 | hg diff --git -r 3:tip |
|
27 | 27 | |
|
28 | 28 | cat > src <<EOF |
|
29 | 29 | 1 |
|
30 | 30 | 2 |
|
31 | 31 | 3 |
|
32 | 32 | 4 |
|
33 | 33 | 5 |
|
34 | 34 | EOF |
|
35 | 35 | hg ci -Amsrc -d '0 0' |
|
36 | 36 | chmod +x src |
|
37 | 37 | hg ci -munexec -d '0 0' |
|
38 | 38 | echo '% chmod 644' |
|
39 | 39 | hg diff --git -r 5:tip |
|
40 | 40 | |
|
41 | 41 | hg mv src dst |
|
42 | 42 | chmod -x dst |
|
43 | 43 | echo a >> dst |
|
44 | 44 | hg ci -mrenamemod -d '0 0' |
|
45 | 45 | echo '% rename+mod+chmod' |
|
46 | 46 | hg diff --git -r 6:tip |
|
47 | 47 | |
|
48 | 48 | echo '% nonexistent in tip+chmod' |
|
49 | 49 | hg diff --git -r 5:6 |
|
50 | 50 | |
|
51 | 51 | echo '% binary diff' |
|
52 | 52 | cp $TESTDIR/binfile.bin . |
|
53 | 53 | hg add binfile.bin |
|
54 | 54 | hg diff --git > b.diff |
|
55 | 55 | cat b.diff |
|
56 | 56 | |
|
57 | 57 | echo '% import binary diff' |
|
58 | 58 | hg revert binfile.bin |
|
59 | 59 | rm binfile.bin |
|
60 | 60 | hg import -mfoo b.diff |
|
61 | 61 | cmp binfile.bin $TESTDIR/binfile.bin |
|
62 | 62 | |
|
63 | 63 | echo |
|
64 | 64 | echo '% diff across many revisions' |
|
65 | 65 | hg mv dst dst2 |
|
66 | 66 | hg ci -m 'mv dst dst2' -d '0 0' |
|
67 | 67 | |
|
68 | 68 | echo >> start |
|
69 | 69 | hg ci -m 'change start' -d '0 0' |
|
70 | 70 | |
|
71 | 71 | hg revert -r -2 start |
|
72 | 72 | hg mv dst2 dst3 |
|
73 | 73 | hg ci -m 'mv dst2 dst3; revert start' -d '0 0' |
|
74 | 74 | |
|
75 | 75 | hg diff --git -r 9:11 |
|
76 | ||
|
77 | echo a >> foo | |
|
78 | hg add foo | |
|
79 | hg ci -m 'add foo' | |
|
80 | echo b >> foo | |
|
81 | hg ci -m 'change foo' | |
|
82 | hg mv foo bar | |
|
83 | hg ci -m 'mv foo bar' | |
|
84 | echo c >> bar | |
|
85 | hg ci -m 'change bar' | |
|
86 | ||
|
87 | echo | |
|
88 | echo '% file created before r1 and renamed before r2' | |
|
89 | hg diff --git -r -3:-1 | |
|
90 | echo | |
|
91 | echo '% file created in r1 and renamed before r2' | |
|
92 | hg diff --git -r -4:-1 | |
|
93 | echo | |
|
94 | echo '% file created after r1 and renamed before r2' | |
|
95 | hg diff --git -r -5:-1 | |
|
96 |
@@ -1,72 +1,104 b'' | |||
|
1 | 1 | adding start |
|
2 | 2 | adding new |
|
3 | 3 | % new file |
|
4 | 4 | diff --git a/new b/new |
|
5 | 5 | new file mode 100644 |
|
6 | 6 | --- /dev/null |
|
7 | 7 | +++ b/new |
|
8 | 8 | @@ -0,0 +1,1 @@ |
|
9 | 9 | +new |
|
10 | 10 | % copy |
|
11 | 11 | diff --git a/new b/copy |
|
12 | 12 | copy from new |
|
13 | 13 | copy to copy |
|
14 | 14 | % rename |
|
15 | 15 | diff --git a/copy b/rename |
|
16 | 16 | rename from copy |
|
17 | 17 | rename to rename |
|
18 | 18 | % delete |
|
19 | 19 | diff --git a/rename b/rename |
|
20 | 20 | deleted file mode 100644 |
|
21 | 21 | --- a/rename |
|
22 | 22 | +++ /dev/null |
|
23 | 23 | @@ -1,1 +0,0 @@ |
|
24 | 24 | -new |
|
25 | 25 | adding src |
|
26 | 26 | % chmod 644 |
|
27 | 27 | diff --git a/src b/src |
|
28 | 28 | old mode 100644 |
|
29 | 29 | new mode 100755 |
|
30 | 30 | % rename+mod+chmod |
|
31 | 31 | diff --git a/src b/dst |
|
32 | 32 | old mode 100755 |
|
33 | 33 | new mode 100644 |
|
34 | 34 | rename from src |
|
35 | 35 | rename to dst |
|
36 | 36 | --- a/dst |
|
37 | 37 | +++ b/dst |
|
38 | 38 | @@ -3,3 +3,4 @@ 3 |
|
39 | 39 | 3 |
|
40 | 40 | 4 |
|
41 | 41 | 5 |
|
42 | 42 | +a |
|
43 | 43 | % nonexistent in tip+chmod |
|
44 | 44 | diff --git a/src b/src |
|
45 | 45 | old mode 100644 |
|
46 | 46 | new mode 100755 |
|
47 | 47 | % binary diff |
|
48 | 48 | diff --git a/binfile.bin b/binfile.bin |
|
49 | 49 | new file mode 100644 |
|
50 | 50 | index 0000000000000000000000000000000000000000..37ba3d1c6f17137d9c5f5776fa040caf5fe73ff9 |
|
51 | 51 | GIT binary patch |
|
52 | 52 | literal 593 |
|
53 | 53 | zc$@)I0<QguP)<h;3K|Lk000e1NJLTq000mG000mO0ssI2kdbIM00009a7bBm000XU |
|
54 | 54 | z000XU0RWnu7ytkO2XskIMF-Uh9TW;VpMjwv0005-Nkl<ZD9@FWPs=e;7{<>W$NUkd |
|
55 | 55 | zX$nnYLt$-$V!?uy+1V%`z&Eh=ah|duER<4|QWhju3gb^nF*8iYobxWG-qqXl=2~5M |
|
56 | 56 | z*IoDB)sG^CfNuoBmqLTVU^<;@nwHP!1wrWd`{(mHo6VNXWtyh{alzqmsH*yYzpvLT |
|
57 | 57 | zLdY<T=ks|woh-`&01!ej#(xbV1f|pI*=%;d-%F*E*X#ZH`4I%6SS+$EJDE&ct=8po |
|
58 | 58 | ziN#{?_j|kD%Cd|oiqds`xm@;oJ-^?NG3Gdqrs?5u*zI;{nogxsx~^|Fn^Y?Gdc6<; |
|
59 | 59 | zfMJ+iF1J`LMx&A2?dEwNW8ClebzPTbIh{@$hS6*`kH@1d%Lo7fA#}N1)oN7`gm$~V |
|
60 | 60 | z+wDx#)OFqMcE{s!JN0-xhG8ItAjVkJwEcb`3WWlJfU2r?;Pd%dmR+q@mSri5q9_W- |
|
61 | 61 | zaR2~ECX?B2w+zELozC0s*6Z~|QG^f{3I#<`?)Q7U-JZ|q5W;9Q8i_=pBuSzunx=U; |
|
62 | 62 | z9C)5jBoYw9^?EHyQl(M}1OlQcCX>lXB*ODN003Z&P17_@)3Pi=i0wb04<W?v-u}7K |
|
63 | 63 | zXmmQA+wDgE!qR9o8jr`%=ab_&uh(l?R=r;Tjiqon91I2-hIu?57~@*4h7h9uORK#= |
|
64 | 64 | fQItJW-{SoTm)8|5##k|m00000NkvXXu0mjf{mKw4 |
|
65 | 65 | |
|
66 | 66 | % import binary diff |
|
67 | 67 | applying b.diff |
|
68 | 68 | |
|
69 | 69 | % diff across many revisions |
|
70 | 70 | diff --git a/dst2 b/dst3 |
|
71 | 71 | rename from dst2 |
|
72 | 72 | rename to dst3 |
|
73 | ||
|
74 | % file created before r1 and renamed before r2 | |
|
75 | diff --git a/foo b/bar | |
|
76 | rename from foo | |
|
77 | rename to bar | |
|
78 | --- a/bar | |
|
79 | +++ b/bar | |
|
80 | @@ -1,2 +1,3 @@ a | |
|
81 | a | |
|
82 | b | |
|
83 | +c | |
|
84 | ||
|
85 | % file created in r1 and renamed before r2 | |
|
86 | diff --git a/foo b/bar | |
|
87 | rename from foo | |
|
88 | rename to bar | |
|
89 | --- a/bar | |
|
90 | +++ b/bar | |
|
91 | @@ -1,1 +1,3 @@ a | |
|
92 | a | |
|
93 | +b | |
|
94 | +c | |
|
95 | ||
|
96 | % file created after r1 and renamed before r2 | |
|
97 | diff --git a/bar b/bar | |
|
98 | new file mode 100644 | |
|
99 | --- /dev/null | |
|
100 | +++ b/bar | |
|
101 | @@ -0,0 +1,3 @@ | |
|
102 | +a | |
|
103 | +b | |
|
104 | +c |
General Comments 0
You need to be logged in to leave comments.
Login now