Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/patch.py @ 7389:72f0e4ebd9e6
patch: remove useless unlink() call
author | Patrick Mezard <pmezard@gmail.com> |
---|---|
date | Wed, 19 Nov 2008 13:27:57 +0100 |
parents | eae1767cc6a8 |
children | 27d304c8cc03 |
rev | line source |
---|---|
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
1 # patch.py - patch file parsing routines |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
2 # |
2865
71e78f2ca5ae
merge git patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2863
diff
changeset
|
3 # Copyright 2006 Brendan Cully <brendan@kublai.com> |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
4 # Copyright 2007 Chris Mason <chris.mason@oracle.com> |
2865
71e78f2ca5ae
merge git patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2863
diff
changeset
|
5 # |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
6 # This software may be used and distributed according to the terms |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
7 # of the GNU General Public License, incorporated herein by reference. |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
8 |
3891 | 9 from i18n import _ |
6211
f89fd07fc51d
Expand import * to allow Pyflakes to find problems
Joel Rosdahl <joel@rosdahl.net>
parents:
6179
diff
changeset
|
10 from node import hex, nullid, short |
6738
336fda65759a
use repo.changectx rather than context.changectx
Matt Mackall <mpm@selenic.com>
parents:
6603
diff
changeset
|
11 import base85, cmdutil, mdiff, util, revlog, diffhelpers, copies |
7106
4674706b5b95
python2.6: use subprocess if available
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7090
diff
changeset
|
12 import cStringIO, email.Parser, os, re, errno |
3877
abaee83ce0a6
Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
3717
diff
changeset
|
13 import sys, tempfile, zlib |
2866
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
14 |
7199
dd891d0d97a3
patch: consolidate two different regexes for parsing of git diffs
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7198
diff
changeset
|
15 gitre = re.compile('diff --git a/(.*) b/(.*)') |
dd891d0d97a3
patch: consolidate two different regexes for parsing of git diffs
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7198
diff
changeset
|
16 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
17 class PatchError(Exception): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
18 pass |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
19 |
4900
e56c7e05c7e6
patch.py: re-add the ability to use an external patch program
Bryan O'Sullivan <bos@serpentine.com>
parents:
4899
diff
changeset
|
20 class NoHunks(PatchError): |
e56c7e05c7e6
patch.py: re-add the ability to use an external patch program
Bryan O'Sullivan <bos@serpentine.com>
parents:
4899
diff
changeset
|
21 pass |
e56c7e05c7e6
patch.py: re-add the ability to use an external patch program
Bryan O'Sullivan <bos@serpentine.com>
parents:
4899
diff
changeset
|
22 |
2933
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
23 # helper functions |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
24 |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
25 def copyfile(src, dst, basedir=None): |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
26 if not basedir: |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
27 basedir = os.getcwd() |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
28 |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
29 abssrc, absdst = [os.path.join(basedir, n) for n in (src, dst)] |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
30 if os.path.exists(absdst): |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
31 raise util.Abort(_("cannot create %s: destination already exists") % |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
32 dst) |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
33 |
7244
127a624406b4
patch: remove double variables in copyfile() func
Bartosz SKOWRON <getxsick at gmail dot com>
parents:
7200
diff
changeset
|
34 if not os.path.isdir(basedir): |
127a624406b4
patch: remove double variables in copyfile() func
Bartosz SKOWRON <getxsick at gmail dot com>
parents:
7200
diff
changeset
|
35 os.makedirs(basedir) |
3629
4cfb72bcb978
util: add copyfile function
Matt Mackall <mpm@selenic.com>
parents:
3588
diff
changeset
|
36 |
4cfb72bcb978
util: add copyfile function
Matt Mackall <mpm@selenic.com>
parents:
3588
diff
changeset
|
37 util.copyfile(abssrc, absdst) |
2933
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
38 |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
39 # public functions |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
40 |
2866
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
41 def extract(ui, fileobj): |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
42 '''extract patch from data read from fileobj. |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
43 |
4263 | 44 patch can be a normal patch or contained in an email message. |
2866
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
45 |
4263 | 46 return tuple (filename, message, user, date, node, p1, p2). |
47 Any item in the returned tuple can be None. If filename is None, | |
48 fileobj did not contain a patch. Caller must unlink filename when done.''' | |
2866
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
49 |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
50 # attempt to detect the start of a patch |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
51 # (this heuristic is borrowed from quilt) |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
52 diffre = re.compile(r'^(?:Index:[ \t]|diff[ \t]|RCS file: |' + |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
53 'retrieving revision [0-9]+(\.[0-9]+)*$|' + |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
54 '(---|\*\*\*)[ \t])', re.MULTILINE) |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
55 |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
56 fd, tmpname = tempfile.mkstemp(prefix='hg-patch-') |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
57 tmpfp = os.fdopen(fd, 'w') |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
58 try: |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
59 msg = email.Parser.Parser().parse(fileobj) |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
60 |
4777
5ee5cbfceff3
patch.extract: do not prepend subject if the description already starts with it
Brendan Cully <brendan@kublai.com>
parents:
4659
diff
changeset
|
61 subject = msg['Subject'] |
2866
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
62 user = msg['From'] |
5418
9b469bdb1ce1
patch: fix git sendmail handling without proper mail headers
Patrick Mezard <pmezard@gmail.com>
parents:
5403
diff
changeset
|
63 gitsendmail = 'git-send-email' in msg.get('X-Mailer', '') |
2866
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
64 # should try to parse msg['Date'] |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
65 date = None |
4263 | 66 nodeid = None |
4443
eff2eefdb65a
Add ability to parse branch information to hg import
Eric Hopper <hopper@omnifarious.org>
parents:
4436
diff
changeset
|
67 branch = None |
4263 | 68 parents = [] |
2866
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
69 |
4777
5ee5cbfceff3
patch.extract: do not prepend subject if the description already starts with it
Brendan Cully <brendan@kublai.com>
parents:
4659
diff
changeset
|
70 if subject: |
5ee5cbfceff3
patch.extract: do not prepend subject if the description already starts with it
Brendan Cully <brendan@kublai.com>
parents:
4659
diff
changeset
|
71 if subject.startswith('[PATCH'): |
5ee5cbfceff3
patch.extract: do not prepend subject if the description already starts with it
Brendan Cully <brendan@kublai.com>
parents:
4659
diff
changeset
|
72 pend = subject.find(']') |
4208
bd9b84b9a84b
Make [PATCH] removal slightly more robust
Brendan Cully <brendan@kublai.com>
parents:
4201
diff
changeset
|
73 if pend >= 0: |
4777
5ee5cbfceff3
patch.extract: do not prepend subject if the description already starts with it
Brendan Cully <brendan@kublai.com>
parents:
4659
diff
changeset
|
74 subject = subject[pend+1:].lstrip() |
5ee5cbfceff3
patch.extract: do not prepend subject if the description already starts with it
Brendan Cully <brendan@kublai.com>
parents:
4659
diff
changeset
|
75 subject = subject.replace('\n\t', ' ') |
5ee5cbfceff3
patch.extract: do not prepend subject if the description already starts with it
Brendan Cully <brendan@kublai.com>
parents:
4659
diff
changeset
|
76 ui.debug('Subject: %s\n' % subject) |
2866
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
77 if user: |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
78 ui.debug('From: %s\n' % user) |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
79 diffs_seen = 0 |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
80 ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') |
4900
e56c7e05c7e6
patch.py: re-add the ability to use an external patch program
Bryan O'Sullivan <bos@serpentine.com>
parents:
4899
diff
changeset
|
81 message = '' |
2866
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
82 for part in msg.walk(): |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
83 content_type = part.get_content_type() |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
84 ui.debug('Content-Type: %s\n' % content_type) |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
85 if content_type not in ok_types: |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
86 continue |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
87 payload = part.get_payload(decode=True) |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
88 m = diffre.search(payload) |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
89 if m: |
4220
1253703853a8
git-send-email compatibility: stop reading changelog after ^---$
Brendan Cully <brendan@kublai.com>
parents:
4208
diff
changeset
|
90 hgpatch = False |
1253703853a8
git-send-email compatibility: stop reading changelog after ^---$
Brendan Cully <brendan@kublai.com>
parents:
4208
diff
changeset
|
91 ignoretext = False |
1253703853a8
git-send-email compatibility: stop reading changelog after ^---$
Brendan Cully <brendan@kublai.com>
parents:
4208
diff
changeset
|
92 |
2866
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
93 ui.debug(_('found patch at byte %d\n') % m.start(0)) |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
94 diffs_seen += 1 |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
95 cfp = cStringIO.StringIO() |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
96 for line in payload[:m.start(0)].splitlines(): |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
97 if line.startswith('# HG changeset patch'): |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
98 ui.debug(_('patch generated by hg export\n')) |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
99 hgpatch = True |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
100 # drop earlier commit message content |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
101 cfp.seek(0) |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
102 cfp.truncate() |
4778
e321f16f4eac
patch.extract: fix test-import breakage introduced in the previous changeset
Brendan Cully <brendan@kublai.com>
parents:
4777
diff
changeset
|
103 subject = None |
2866
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
104 elif hgpatch: |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
105 if line.startswith('# User '): |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
106 user = line[7:] |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
107 ui.debug('From: %s\n' % user) |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
108 elif line.startswith("# Date "): |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
109 date = line[7:] |
4443
eff2eefdb65a
Add ability to parse branch information to hg import
Eric Hopper <hopper@omnifarious.org>
parents:
4436
diff
changeset
|
110 elif line.startswith("# Branch "): |
eff2eefdb65a
Add ability to parse branch information to hg import
Eric Hopper <hopper@omnifarious.org>
parents:
4436
diff
changeset
|
111 branch = line[9:] |
4263 | 112 elif line.startswith("# Node ID "): |
113 nodeid = line[10:] | |
114 elif line.startswith("# Parent "): | |
115 parents.append(line[10:]) | |
5418
9b469bdb1ce1
patch: fix git sendmail handling without proper mail headers
Patrick Mezard <pmezard@gmail.com>
parents:
5403
diff
changeset
|
116 elif line == '---' and gitsendmail: |
4220
1253703853a8
git-send-email compatibility: stop reading changelog after ^---$
Brendan Cully <brendan@kublai.com>
parents:
4208
diff
changeset
|
117 ignoretext = True |
1253703853a8
git-send-email compatibility: stop reading changelog after ^---$
Brendan Cully <brendan@kublai.com>
parents:
4208
diff
changeset
|
118 if not line.startswith('# ') and not ignoretext: |
2866
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
119 cfp.write(line) |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
120 cfp.write('\n') |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
121 message = cfp.getvalue() |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
122 if tmpfp: |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
123 tmpfp.write(payload) |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
124 if not payload.endswith('\n'): |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
125 tmpfp.write('\n') |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
126 elif not diffs_seen and message and content_type == 'text/plain': |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
127 message += '\n' + payload |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
128 except: |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
129 tmpfp.close() |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
130 os.unlink(tmpname) |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
131 raise |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
132 |
4777
5ee5cbfceff3
patch.extract: do not prepend subject if the description already starts with it
Brendan Cully <brendan@kublai.com>
parents:
4659
diff
changeset
|
133 if subject and not message.startswith(subject): |
5ee5cbfceff3
patch.extract: do not prepend subject if the description already starts with it
Brendan Cully <brendan@kublai.com>
parents:
4659
diff
changeset
|
134 message = '%s\n%s' % (subject, message) |
2866
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
135 tmpfp.close() |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
136 if not diffs_seen: |
2893e51407a4
commands.import: refactor patch parsing into patch.extract.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2865
diff
changeset
|
137 os.unlink(tmpname) |
4443
eff2eefdb65a
Add ability to parse branch information to hg import
Eric Hopper <hopper@omnifarious.org>
parents:
4436
diff
changeset
|
138 return None, message, user, date, branch, None, None, None |
4263 | 139 p1 = parents and parents.pop(0) or None |
140 p2 = parents and parents.pop(0) or None | |
4443
eff2eefdb65a
Add ability to parse branch information to hg import
Eric Hopper <hopper@omnifarious.org>
parents:
4436
diff
changeset
|
141 return tmpname, message, user, date, branch, nodeid, p1, p2 |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
142 |
3716
ab5600428b08
handle files with both git binary patches and copy/rename ops
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3702
diff
changeset
|
143 GP_PATCH = 1 << 0 # we have to run patch |
ab5600428b08
handle files with both git binary patches and copy/rename ops
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3702
diff
changeset
|
144 GP_FILTER = 1 << 1 # there's some copy/rename operation |
ab5600428b08
handle files with both git binary patches and copy/rename ops
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3702
diff
changeset
|
145 GP_BINARY = 1 << 2 # there's a binary patch |
ab5600428b08
handle files with both git binary patches and copy/rename ops
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3702
diff
changeset
|
146 |
7148
7d84e5b00e29
patch: extract and rename gitpatch into patchmeta, document
Patrick Mezard <pmezard@gmail.com>
parents:
7147
diff
changeset
|
147 class patchmeta: |
7d84e5b00e29
patch: extract and rename gitpatch into patchmeta, document
Patrick Mezard <pmezard@gmail.com>
parents:
7147
diff
changeset
|
148 """Patched file metadata |
7d84e5b00e29
patch: extract and rename gitpatch into patchmeta, document
Patrick Mezard <pmezard@gmail.com>
parents:
7147
diff
changeset
|
149 |
7d84e5b00e29
patch: extract and rename gitpatch into patchmeta, document
Patrick Mezard <pmezard@gmail.com>
parents:
7147
diff
changeset
|
150 'op' is the performed operation within ADD, DELETE, RENAME, MODIFY |
7d84e5b00e29
patch: extract and rename gitpatch into patchmeta, document
Patrick Mezard <pmezard@gmail.com>
parents:
7147
diff
changeset
|
151 or COPY. 'path' is patched file path. 'oldpath' is set to the |
7149
01a056c54385
patch: patchmeta gives (islink, isexec) tuple instead of int mode
Patrick Mezard <pmezard@gmail.com>
parents:
7148
diff
changeset
|
152 origin file when 'op' is either COPY or RENAME, None otherwise. If |
01a056c54385
patch: patchmeta gives (islink, isexec) tuple instead of int mode
Patrick Mezard <pmezard@gmail.com>
parents:
7148
diff
changeset
|
153 file mode is changed, 'mode' is a tuple (islink, isexec) where |
01a056c54385
patch: patchmeta gives (islink, isexec) tuple instead of int mode
Patrick Mezard <pmezard@gmail.com>
parents:
7148
diff
changeset
|
154 'islink' is True if the file is a symlink and 'isexec' is True if |
01a056c54385
patch: patchmeta gives (islink, isexec) tuple instead of int mode
Patrick Mezard <pmezard@gmail.com>
parents:
7148
diff
changeset
|
155 the file is executable. Otherwise, 'mode' is None. |
7148
7d84e5b00e29
patch: extract and rename gitpatch into patchmeta, document
Patrick Mezard <pmezard@gmail.com>
parents:
7147
diff
changeset
|
156 """ |
7d84e5b00e29
patch: extract and rename gitpatch into patchmeta, document
Patrick Mezard <pmezard@gmail.com>
parents:
7147
diff
changeset
|
157 def __init__(self, path): |
7d84e5b00e29
patch: extract and rename gitpatch into patchmeta, document
Patrick Mezard <pmezard@gmail.com>
parents:
7147
diff
changeset
|
158 self.path = path |
7d84e5b00e29
patch: extract and rename gitpatch into patchmeta, document
Patrick Mezard <pmezard@gmail.com>
parents:
7147
diff
changeset
|
159 self.oldpath = None |
7d84e5b00e29
patch: extract and rename gitpatch into patchmeta, document
Patrick Mezard <pmezard@gmail.com>
parents:
7147
diff
changeset
|
160 self.mode = None |
7d84e5b00e29
patch: extract and rename gitpatch into patchmeta, document
Patrick Mezard <pmezard@gmail.com>
parents:
7147
diff
changeset
|
161 self.op = 'MODIFY' |
7d84e5b00e29
patch: extract and rename gitpatch into patchmeta, document
Patrick Mezard <pmezard@gmail.com>
parents:
7147
diff
changeset
|
162 self.lineno = 0 |
7d84e5b00e29
patch: extract and rename gitpatch into patchmeta, document
Patrick Mezard <pmezard@gmail.com>
parents:
7147
diff
changeset
|
163 self.binary = False |
7d84e5b00e29
patch: extract and rename gitpatch into patchmeta, document
Patrick Mezard <pmezard@gmail.com>
parents:
7147
diff
changeset
|
164 |
7149
01a056c54385
patch: patchmeta gives (islink, isexec) tuple instead of int mode
Patrick Mezard <pmezard@gmail.com>
parents:
7148
diff
changeset
|
165 def setmode(self, mode): |
01a056c54385
patch: patchmeta gives (islink, isexec) tuple instead of int mode
Patrick Mezard <pmezard@gmail.com>
parents:
7148
diff
changeset
|
166 islink = mode & 020000 |
01a056c54385
patch: patchmeta gives (islink, isexec) tuple instead of int mode
Patrick Mezard <pmezard@gmail.com>
parents:
7148
diff
changeset
|
167 isexec = mode & 0100 |
01a056c54385
patch: patchmeta gives (islink, isexec) tuple instead of int mode
Patrick Mezard <pmezard@gmail.com>
parents:
7148
diff
changeset
|
168 self.mode = (islink, isexec) |
01a056c54385
patch: patchmeta gives (islink, isexec) tuple instead of int mode
Patrick Mezard <pmezard@gmail.com>
parents:
7148
diff
changeset
|
169 |
7152
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
170 def readgitpatch(lr): |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
171 """extract git-style metadata about patches from <patchname>""" |
3223
53e843840349
Whitespace/Tab cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3199
diff
changeset
|
172 |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
173 # Filter patch for git information |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
174 gp = None |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
175 gitpatches = [] |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
176 # Can have a git patch with only metadata, causing patch to complain |
3716
ab5600428b08
handle files with both git binary patches and copy/rename ops
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3702
diff
changeset
|
177 dopatch = 0 |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
178 |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
179 lineno = 0 |
7152
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
180 for line in lr: |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
181 lineno += 1 |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
182 if line.startswith('diff --git'): |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
183 m = gitre.match(line) |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
184 if m: |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
185 if gp: |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
186 gitpatches.append(gp) |
3673
eb0b4a2d70a9
white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3629
diff
changeset
|
187 src, dst = m.group(1, 2) |
7148
7d84e5b00e29
patch: extract and rename gitpatch into patchmeta, document
Patrick Mezard <pmezard@gmail.com>
parents:
7147
diff
changeset
|
188 gp = patchmeta(dst) |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
189 gp.lineno = lineno |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
190 elif gp: |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
191 if line.startswith('--- '): |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
192 if gp.op in ('COPY', 'RENAME'): |
3716
ab5600428b08
handle files with both git binary patches and copy/rename ops
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3702
diff
changeset
|
193 dopatch |= GP_FILTER |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
194 gitpatches.append(gp) |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
195 gp = None |
3716
ab5600428b08
handle files with both git binary patches and copy/rename ops
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3702
diff
changeset
|
196 dopatch |= GP_PATCH |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
197 continue |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
198 if line.startswith('rename from '): |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
199 gp.op = 'RENAME' |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
200 gp.oldpath = line[12:].rstrip() |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
201 elif line.startswith('rename to '): |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
202 gp.path = line[10:].rstrip() |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
203 elif line.startswith('copy from '): |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
204 gp.op = 'COPY' |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
205 gp.oldpath = line[10:].rstrip() |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
206 elif line.startswith('copy to '): |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
207 gp.path = line[8:].rstrip() |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
208 elif line.startswith('deleted file'): |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
209 gp.op = 'DELETE' |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
210 elif line.startswith('new file mode '): |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
211 gp.op = 'ADD' |
7149
01a056c54385
patch: patchmeta gives (islink, isexec) tuple instead of int mode
Patrick Mezard <pmezard@gmail.com>
parents:
7148
diff
changeset
|
212 gp.setmode(int(line.rstrip()[-6:], 8)) |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
213 elif line.startswith('new mode '): |
7149
01a056c54385
patch: patchmeta gives (islink, isexec) tuple instead of int mode
Patrick Mezard <pmezard@gmail.com>
parents:
7148
diff
changeset
|
214 gp.setmode(int(line.rstrip()[-6:], 8)) |
3367
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
215 elif line.startswith('GIT binary patch'): |
3716
ab5600428b08
handle files with both git binary patches and copy/rename ops
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3702
diff
changeset
|
216 dopatch |= GP_BINARY |
3367
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
217 gp.binary = True |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
218 if gp: |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
219 gitpatches.append(gp) |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
220 |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
221 if not gitpatches: |
3716
ab5600428b08
handle files with both git binary patches and copy/rename ops
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3702
diff
changeset
|
222 dopatch = GP_PATCH |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
223 |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
224 return (dopatch, gitpatches) |
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
225 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
226 # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
227 unidesc = re.compile('@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@') |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
228 contextdesc = re.compile('(---|\*\*\*) (\d+)(,(\d+))? (---|\*\*\*)') |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
229 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
230 class patchfile: |
5652
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
231 def __init__(self, ui, fname, missing=False): |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
232 self.fname = fname |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
233 self.ui = ui |
5652
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
234 self.lines = [] |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
235 self.exists = False |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
236 self.missing = missing |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
237 if not missing: |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
238 try: |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
239 fp = file(fname, 'rb') |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
240 self.lines = fp.readlines() |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
241 self.exists = True |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
242 except IOError: |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
243 pass |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
244 else: |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
245 self.ui.warn(_("unable to find '%s' for patching\n") % self.fname) |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
246 |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
247 if not self.exists: |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
248 dirname = os.path.dirname(fname) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
249 if dirname and not os.path.isdir(dirname): |
5653
1b35bc1c1968
patch: simplify directory creation
Patrick Mezard <pmezard@gmail.com>
parents:
5652
diff
changeset
|
250 os.makedirs(dirname) |
5143
d4fa6bafc43a
Remove trailing spaces, fix indentation
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5116
diff
changeset
|
251 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
252 self.hash = {} |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
253 self.dirty = 0 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
254 self.offset = 0 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
255 self.rej = [] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
256 self.fileprinted = False |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
257 self.printfile(False) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
258 self.hunks = 0 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
259 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
260 def printfile(self, warn): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
261 if self.fileprinted: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
262 return |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
263 if warn or self.ui.verbose: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
264 self.fileprinted = True |
4898
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
265 s = _("patching file %s\n") % self.fname |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
266 if warn: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
267 self.ui.warn(s) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
268 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
269 self.ui.note(s) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
270 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
271 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
272 def findlines(self, l, linenum): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
273 # looks through the hash and finds candidate lines. The |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
274 # result is a list of line numbers sorted based on distance |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
275 # from linenum |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
276 def sorter(a, b): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
277 vala = abs(a - linenum) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
278 valb = abs(b - linenum) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
279 return cmp(vala, valb) |
5143
d4fa6bafc43a
Remove trailing spaces, fix indentation
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5116
diff
changeset
|
280 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
281 try: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
282 cand = self.hash[l] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
283 except: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
284 return [] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
285 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
286 if len(cand) > 1: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
287 # resort our list of potentials forward then back. |
5547
777996744942
patch: fix sort() comparator argument
Jim Hague <jim.hague@acm.org>
parents:
5482
diff
changeset
|
288 cand.sort(sorter) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
289 return cand |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
290 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
291 def hashlines(self): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
292 self.hash = {} |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
293 for x in xrange(len(self.lines)): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
294 s = self.lines[x] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
295 self.hash.setdefault(s, []).append(x) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
296 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
297 def write_rej(self): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
298 # our rejects are a little different from patch(1). This always |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
299 # creates rejects in the same form as the original patch. A file |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
300 # header is inserted so that you can run the reject through patch again |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
301 # without having to type the filename. |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
302 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
303 if not self.rej: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
304 return |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
305 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
306 fname = self.fname + ".rej" |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
307 self.ui.warn( |
6952
3fffba1c87d0
i18n: avoid naive plural tricks
Martin Geisler <mg@daimi.au.dk>
parents:
6948
diff
changeset
|
308 _("%d out of %d hunks FAILED -- saving rejects to file %s\n") % |
3fffba1c87d0
i18n: avoid naive plural tricks
Martin Geisler <mg@daimi.au.dk>
parents:
6948
diff
changeset
|
309 (len(self.rej), self.hunks, fname)) |
4923
59b8ff35c4ed
patch: patches should be read and written in binary mode when possible.
Patrick Mezard <pmezard@gmail.com>
parents:
4922
diff
changeset
|
310 fp = file(fname, 'wb') |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
311 base = os.path.basename(self.fname) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
312 fp.write("--- %s\n+++ %s\n" % (base, base)) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
313 for x in self.rej: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
314 for l in x.hunk: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
315 fp.write(l) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
316 if l[-1] != '\n': |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
317 fp.write("\n\ No newline at end of file\n") |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
318 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
319 def write(self, dest=None): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
320 if self.dirty: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
321 if not dest: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
322 dest = self.fname |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
323 st = None |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
324 try: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
325 st = os.lstat(dest) |
5477
bff41174563f
Only set mode of new patch if the target file was removed before.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5418
diff
changeset
|
326 except OSError, inst: |
bff41174563f
Only set mode of new patch if the target file was removed before.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5418
diff
changeset
|
327 if inst.errno != errno.ENOENT: |
bff41174563f
Only set mode of new patch if the target file was removed before.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5418
diff
changeset
|
328 raise |
bff41174563f
Only set mode of new patch if the target file was removed before.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5418
diff
changeset
|
329 if st and st.st_nlink > 1: |
bff41174563f
Only set mode of new patch if the target file was removed before.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5418
diff
changeset
|
330 os.unlink(dest) |
4923
59b8ff35c4ed
patch: patches should be read and written in binary mode when possible.
Patrick Mezard <pmezard@gmail.com>
parents:
4922
diff
changeset
|
331 fp = file(dest, 'wb') |
5477
bff41174563f
Only set mode of new patch if the target file was removed before.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5418
diff
changeset
|
332 if st and st.st_nlink > 1: |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
333 os.chmod(dest, st.st_mode) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
334 fp.writelines(self.lines) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
335 fp.close() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
336 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
337 def close(self): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
338 self.write() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
339 self.write_rej() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
340 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
341 def apply(self, h, reverse): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
342 if not h.complete(): |
4898
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
343 raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") % |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
344 (h.number, h.desc, len(h.a), h.lena, len(h.b), |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
345 h.lenb)) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
346 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
347 self.hunks += 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
348 if reverse: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
349 h.reverse() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
350 |
5652
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
351 if self.missing: |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
352 self.rej.append(h) |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
353 return -1 |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
354 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
355 if self.exists and h.createfile(): |
4898
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
356 self.ui.warn(_("file %s already exists\n") % self.fname) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
357 self.rej.append(h) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
358 return -1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
359 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
360 if isinstance(h, binhunk): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
361 if h.rmfile(): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
362 os.unlink(self.fname) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
363 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
364 self.lines[:] = h.new() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
365 self.offset += len(h.new()) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
366 self.dirty = 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
367 return 0 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
368 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
369 # fast case first, no offsets, no fuzz |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
370 old = h.old() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
371 # patch starts counting at 1 unless we are adding the file |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
372 if h.starta == 0: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
373 start = 0 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
374 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
375 start = h.starta + self.offset - 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
376 orig_start = start |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
377 if diffhelpers.testhunk(old, self.lines, start) == 0: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
378 if h.rmfile(): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
379 os.unlink(self.fname) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
380 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
381 self.lines[start : start + h.lena] = h.new() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
382 self.offset += h.lenb - h.lena |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
383 self.dirty = 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
384 return 0 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
385 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
386 # ok, we couldn't match the hunk. Lets look for offsets and fuzz it |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
387 self.hashlines() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
388 if h.hunk[-1][0] != ' ': |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
389 # if the hunk tried to put something at the bottom of the file |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
390 # override the start line and use eof here |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
391 search_start = len(self.lines) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
392 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
393 search_start = orig_start |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
394 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
395 for fuzzlen in xrange(3): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
396 for toponly in [ True, False ]: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
397 old = h.old(fuzzlen, toponly) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
398 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
399 cand = self.findlines(old[0][1:], search_start) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
400 for l in cand: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
401 if diffhelpers.testhunk(old, self.lines, l) == 0: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
402 newlines = h.new(fuzzlen, toponly) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
403 self.lines[l : l + len(old)] = newlines |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
404 self.offset += len(newlines) - len(old) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
405 self.dirty = 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
406 if fuzzlen: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
407 fuzzstr = "with fuzz %d " % fuzzlen |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
408 f = self.ui.warn |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
409 self.printfile(True) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
410 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
411 fuzzstr = "" |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
412 f = self.ui.note |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
413 offset = l - orig_start - fuzzlen |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
414 if offset == 1: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
415 linestr = "line" |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
416 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
417 linestr = "lines" |
4898
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
418 f(_("Hunk #%d succeeded at %d %s(offset %d %s).\n") % |
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
419 (h.number, l+1, fuzzstr, offset, linestr)) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
420 return fuzzlen |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
421 self.printfile(True) |
4898
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
422 self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start)) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
423 self.rej.append(h) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
424 return -1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
425 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
426 class hunk: |
6280
9db24a36d182
patch: check filename is /dev/null for creation or deletion (issue 1033)
Patrick Mezard <pmezard@gmail.com>
parents:
6275
diff
changeset
|
427 def __init__(self, desc, num, lr, context, create=False, remove=False): |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
428 self.number = num |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
429 self.desc = desc |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
430 self.hunk = [ desc ] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
431 self.a = [] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
432 self.b = [] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
433 if context: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
434 self.read_context_hunk(lr) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
435 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
436 self.read_unified_hunk(lr) |
6280
9db24a36d182
patch: check filename is /dev/null for creation or deletion (issue 1033)
Patrick Mezard <pmezard@gmail.com>
parents:
6275
diff
changeset
|
437 self.create = create |
9db24a36d182
patch: check filename is /dev/null for creation or deletion (issue 1033)
Patrick Mezard <pmezard@gmail.com>
parents:
6275
diff
changeset
|
438 self.remove = remove and not create |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
439 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
440 def read_unified_hunk(self, lr): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
441 m = unidesc.match(self.desc) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
442 if not m: |
4898
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
443 raise PatchError(_("bad hunk #%d") % self.number) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
444 self.starta, foo, self.lena, self.startb, foo2, self.lenb = m.groups() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
445 if self.lena == None: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
446 self.lena = 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
447 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
448 self.lena = int(self.lena) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
449 if self.lenb == None: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
450 self.lenb = 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
451 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
452 self.lenb = int(self.lenb) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
453 self.starta = int(self.starta) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
454 self.startb = int(self.startb) |
7152
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
455 diffhelpers.addlines(lr, self.hunk, self.lena, self.lenb, self.a, self.b) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
456 # if we hit eof before finishing out the hunk, the last line will |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
457 # be zero length. Lets try to fix it up. |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
458 while len(self.hunk[-1]) == 0: |
6948
359e93ceee3a
fix double indentation and trailing whitespace
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6884
diff
changeset
|
459 del self.hunk[-1] |
359e93ceee3a
fix double indentation and trailing whitespace
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6884
diff
changeset
|
460 del self.a[-1] |
359e93ceee3a
fix double indentation and trailing whitespace
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6884
diff
changeset
|
461 del self.b[-1] |
359e93ceee3a
fix double indentation and trailing whitespace
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6884
diff
changeset
|
462 self.lena -= 1 |
359e93ceee3a
fix double indentation and trailing whitespace
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6884
diff
changeset
|
463 self.lenb -= 1 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
464 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
465 def read_context_hunk(self, lr): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
466 self.desc = lr.readline() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
467 m = contextdesc.match(self.desc) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
468 if not m: |
4898
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
469 raise PatchError(_("bad hunk #%d") % self.number) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
470 foo, self.starta, foo2, aend, foo3 = m.groups() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
471 self.starta = int(self.starta) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
472 if aend == None: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
473 aend = self.starta |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
474 self.lena = int(aend) - self.starta |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
475 if self.starta: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
476 self.lena += 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
477 for x in xrange(self.lena): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
478 l = lr.readline() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
479 if l.startswith('---'): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
480 lr.push(l) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
481 break |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
482 s = l[2:] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
483 if l.startswith('- ') or l.startswith('! '): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
484 u = '-' + s |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
485 elif l.startswith(' '): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
486 u = ' ' + s |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
487 else: |
4898
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
488 raise PatchError(_("bad hunk #%d old text line %d") % |
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
489 (self.number, x)) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
490 self.a.append(u) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
491 self.hunk.append(u) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
492 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
493 l = lr.readline() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
494 if l.startswith('\ '): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
495 s = self.a[-1][:-1] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
496 self.a[-1] = s |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
497 self.hunk[-1] = s |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
498 l = lr.readline() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
499 m = contextdesc.match(l) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
500 if not m: |
4898
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
501 raise PatchError(_("bad hunk #%d") % self.number) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
502 foo, self.startb, foo2, bend, foo3 = m.groups() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
503 self.startb = int(self.startb) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
504 if bend == None: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
505 bend = self.startb |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
506 self.lenb = int(bend) - self.startb |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
507 if self.startb: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
508 self.lenb += 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
509 hunki = 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
510 for x in xrange(self.lenb): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
511 l = lr.readline() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
512 if l.startswith('\ '): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
513 s = self.b[-1][:-1] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
514 self.b[-1] = s |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
515 self.hunk[hunki-1] = s |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
516 continue |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
517 if not l: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
518 lr.push(l) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
519 break |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
520 s = l[2:] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
521 if l.startswith('+ ') or l.startswith('! '): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
522 u = '+' + s |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
523 elif l.startswith(' '): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
524 u = ' ' + s |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
525 elif len(self.b) == 0: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
526 # this can happen when the hunk does not add any lines |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
527 lr.push(l) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
528 break |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
529 else: |
4898
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
530 raise PatchError(_("bad hunk #%d old text line %d") % |
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
531 (self.number, x)) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
532 self.b.append(s) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
533 while True: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
534 if hunki >= len(self.hunk): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
535 h = "" |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
536 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
537 h = self.hunk[hunki] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
538 hunki += 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
539 if h == u: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
540 break |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
541 elif h.startswith('-'): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
542 continue |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
543 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
544 self.hunk.insert(hunki-1, u) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
545 break |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
546 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
547 if not self.a: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
548 # this happens when lines were only added to the hunk |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
549 for x in self.hunk: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
550 if x.startswith('-') or x.startswith(' '): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
551 self.a.append(x) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
552 if not self.b: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
553 # this happens when lines were only deleted from the hunk |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
554 for x in self.hunk: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
555 if x.startswith('+') or x.startswith(' '): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
556 self.b.append(x[1:]) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
557 # @@ -start,len +start,len @@ |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
558 self.desc = "@@ -%d,%d +%d,%d @@\n" % (self.starta, self.lena, |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
559 self.startb, self.lenb) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
560 self.hunk[0] = self.desc |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
561 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
562 def reverse(self): |
6280
9db24a36d182
patch: check filename is /dev/null for creation or deletion (issue 1033)
Patrick Mezard <pmezard@gmail.com>
parents:
6275
diff
changeset
|
563 self.create, self.remove = self.remove, self.create |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
564 origlena = self.lena |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
565 origstarta = self.starta |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
566 self.lena = self.lenb |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
567 self.starta = self.startb |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
568 self.lenb = origlena |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
569 self.startb = origstarta |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
570 self.a = [] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
571 self.b = [] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
572 # self.hunk[0] is the @@ description |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
573 for x in xrange(1, len(self.hunk)): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
574 o = self.hunk[x] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
575 if o.startswith('-'): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
576 n = '+' + o[1:] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
577 self.b.append(o[1:]) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
578 elif o.startswith('+'): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
579 n = '-' + o[1:] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
580 self.a.append(n) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
581 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
582 n = o |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
583 self.b.append(o[1:]) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
584 self.a.append(o) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
585 self.hunk[x] = o |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
586 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
587 def fix_newline(self): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
588 diffhelpers.fix_newline(self.hunk, self.a, self.b) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
589 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
590 def complete(self): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
591 return len(self.a) == self.lena and len(self.b) == self.lenb |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
592 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
593 def createfile(self): |
6280
9db24a36d182
patch: check filename is /dev/null for creation or deletion (issue 1033)
Patrick Mezard <pmezard@gmail.com>
parents:
6275
diff
changeset
|
594 return self.starta == 0 and self.lena == 0 and self.create |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
595 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
596 def rmfile(self): |
6280
9db24a36d182
patch: check filename is /dev/null for creation or deletion (issue 1033)
Patrick Mezard <pmezard@gmail.com>
parents:
6275
diff
changeset
|
597 return self.startb == 0 and self.lenb == 0 and self.remove |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
598 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
599 def fuzzit(self, l, fuzz, toponly): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
600 # this removes context lines from the top and bottom of list 'l'. It |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
601 # checks the hunk to make sure only context lines are removed, and then |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
602 # returns a new shortened list of lines. |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
603 fuzz = min(fuzz, len(l)-1) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
604 if fuzz: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
605 top = 0 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
606 bot = 0 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
607 hlen = len(self.hunk) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
608 for x in xrange(hlen-1): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
609 # the hunk starts with the @@ line, so use x+1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
610 if self.hunk[x+1][0] == ' ': |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
611 top += 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
612 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
613 break |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
614 if not toponly: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
615 for x in xrange(hlen-1): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
616 if self.hunk[hlen-bot-1][0] == ' ': |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
617 bot += 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
618 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
619 break |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
620 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
621 # top and bot now count context in the hunk |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
622 # adjust them if either one is short |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
623 context = max(top, bot, 3) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
624 if bot < context: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
625 bot = max(0, fuzz - (context - bot)) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
626 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
627 bot = min(fuzz, bot) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
628 if top < context: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
629 top = max(0, fuzz - (context - top)) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
630 else: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
631 top = min(fuzz, top) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
632 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
633 return l[top:len(l)-bot] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
634 return l |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
635 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
636 def old(self, fuzz=0, toponly=False): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
637 return self.fuzzit(self.a, fuzz, toponly) |
5143
d4fa6bafc43a
Remove trailing spaces, fix indentation
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5116
diff
changeset
|
638 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
639 def newctrl(self): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
640 res = [] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
641 for x in self.hunk: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
642 c = x[0] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
643 if c == ' ' or c == '+': |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
644 res.append(x) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
645 return res |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
646 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
647 def new(self, fuzz=0, toponly=False): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
648 return self.fuzzit(self.b, fuzz, toponly) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
649 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
650 class binhunk: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
651 'A binary patch file. Only understands literals so far.' |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
652 def __init__(self, gitpatch): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
653 self.gitpatch = gitpatch |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
654 self.text = None |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
655 self.hunk = ['GIT binary patch\n'] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
656 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
657 def createfile(self): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
658 return self.gitpatch.op in ('ADD', 'RENAME', 'COPY') |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
659 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
660 def rmfile(self): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
661 return self.gitpatch.op == 'DELETE' |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
662 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
663 def complete(self): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
664 return self.text is not None |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
665 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
666 def new(self): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
667 return [self.text] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
668 |
7153
353141d74ca8
patch: pass linereader to binaryhunk.extract() instead of wrapped fp
Patrick Mezard <pmezard@gmail.com>
parents:
7152
diff
changeset
|
669 def extract(self, lr): |
353141d74ca8
patch: pass linereader to binaryhunk.extract() instead of wrapped fp
Patrick Mezard <pmezard@gmail.com>
parents:
7152
diff
changeset
|
670 line = lr.readline() |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
671 self.hunk.append(line) |
3367
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
672 while line and not line.startswith('literal '): |
7153
353141d74ca8
patch: pass linereader to binaryhunk.extract() instead of wrapped fp
Patrick Mezard <pmezard@gmail.com>
parents:
7152
diff
changeset
|
673 line = lr.readline() |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
674 self.hunk.append(line) |
3367
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
675 if not line: |
4898
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
676 raise PatchError(_('could not extract binary patch')) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
677 size = int(line[8:].rstrip()) |
3367
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
678 dec = [] |
7153
353141d74ca8
patch: pass linereader to binaryhunk.extract() instead of wrapped fp
Patrick Mezard <pmezard@gmail.com>
parents:
7152
diff
changeset
|
679 line = lr.readline() |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
680 self.hunk.append(line) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
681 while len(line) > 1: |
3374
fd43ff3b4442
Use line length field when extracting git binary patches
Brendan Cully <brendan@kublai.com>
parents:
3367
diff
changeset
|
682 l = line[0] |
fd43ff3b4442
Use line length field when extracting git binary patches
Brendan Cully <brendan@kublai.com>
parents:
3367
diff
changeset
|
683 if l <= 'Z' and l >= 'A': |
fd43ff3b4442
Use line length field when extracting git binary patches
Brendan Cully <brendan@kublai.com>
parents:
3367
diff
changeset
|
684 l = ord(l) - ord('A') + 1 |
fd43ff3b4442
Use line length field when extracting git binary patches
Brendan Cully <brendan@kublai.com>
parents:
3367
diff
changeset
|
685 else: |
fd43ff3b4442
Use line length field when extracting git binary patches
Brendan Cully <brendan@kublai.com>
parents:
3367
diff
changeset
|
686 l = ord(l) - ord('a') + 27 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
687 dec.append(base85.b85decode(line[1:-1])[:l]) |
7153
353141d74ca8
patch: pass linereader to binaryhunk.extract() instead of wrapped fp
Patrick Mezard <pmezard@gmail.com>
parents:
7152
diff
changeset
|
688 line = lr.readline() |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
689 self.hunk.append(line) |
3367
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
690 text = zlib.decompress(''.join(dec)) |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
691 if len(text) != size: |
4898
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
692 raise PatchError(_('binary patch is %d bytes, not %d') % |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
693 len(text), size) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
694 self.text = text |
3367
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
695 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
696 def parsefilename(str): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
697 # --- filename \t|space stuff |
5851
03f550f9b554
patch: remove CRLF when parsing file names
Patrick Mezard <pmezard@gmail.com>
parents:
5669
diff
changeset
|
698 s = str[4:].rstrip('\r\n') |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
699 i = s.find('\t') |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
700 if i < 0: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
701 i = s.find(' ') |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
702 if i < 0: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
703 return s |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
704 return s[:i] |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
705 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
706 def selectfile(afile_orig, bfile_orig, hunk, strip, reverse): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
707 def pathstrip(path, count=1): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
708 pathlen = len(path) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
709 i = 0 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
710 if count == 0: |
6520
ba0b2dacc623
fix import with -p0
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6467
diff
changeset
|
711 return '', path.rstrip() |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
712 while count > 0: |
4922
020ee9c781cf
patch: fix normalized paths separators.
Patrick Mezard <pmezard@gmail.com>
parents:
4900
diff
changeset
|
713 i = path.find('/', i) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
714 if i == -1: |
4898
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
715 raise PatchError(_("unable to strip away %d dirs from %s") % |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
716 (count, path)) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
717 i += 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
718 # consume '//' in the path |
4922
020ee9c781cf
patch: fix normalized paths separators.
Patrick Mezard <pmezard@gmail.com>
parents:
4900
diff
changeset
|
719 while i < pathlen - 1 and path[i] == '/': |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
720 i += 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
721 count -= 1 |
6295
bace1990ab12
patch: fix corner case with update + copy patch handling (issue 937)
Patrick Mezard <pmezard@gmail.com>
parents:
6280
diff
changeset
|
722 return path[:i].lstrip(), path[i:].rstrip() |
3367
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
723 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
724 nulla = afile_orig == "/dev/null" |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
725 nullb = bfile_orig == "/dev/null" |
6295
bace1990ab12
patch: fix corner case with update + copy patch handling (issue 937)
Patrick Mezard <pmezard@gmail.com>
parents:
6280
diff
changeset
|
726 abase, afile = pathstrip(afile_orig, strip) |
5651
e11940d84606
patch: avoid file existence tests when possible in selectfile()
Patrick Mezard <pmezard@gmail.com>
parents:
5650
diff
changeset
|
727 gooda = not nulla and os.path.exists(afile) |
6295
bace1990ab12
patch: fix corner case with update + copy patch handling (issue 937)
Patrick Mezard <pmezard@gmail.com>
parents:
6280
diff
changeset
|
728 bbase, bfile = pathstrip(bfile_orig, strip) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
729 if afile == bfile: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
730 goodb = gooda |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
731 else: |
5651
e11940d84606
patch: avoid file existence tests when possible in selectfile()
Patrick Mezard <pmezard@gmail.com>
parents:
5650
diff
changeset
|
732 goodb = not nullb and os.path.exists(bfile) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
733 createfunc = hunk.createfile |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
734 if reverse: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
735 createfunc = hunk.rmfile |
5652
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
736 missing = not goodb and not gooda and not createfunc() |
6295
bace1990ab12
patch: fix corner case with update + copy patch handling (issue 937)
Patrick Mezard <pmezard@gmail.com>
parents:
6280
diff
changeset
|
737 # If afile is "a/b/foo" and bfile is "a/b/foo.orig" we assume the |
bace1990ab12
patch: fix corner case with update + copy patch handling (issue 937)
Patrick Mezard <pmezard@gmail.com>
parents:
6280
diff
changeset
|
738 # diff is between a file and its backup. In this case, the original |
bace1990ab12
patch: fix corner case with update + copy patch handling (issue 937)
Patrick Mezard <pmezard@gmail.com>
parents:
6280
diff
changeset
|
739 # file should be patched (see original mpatch code). |
bace1990ab12
patch: fix corner case with update + copy patch handling (issue 937)
Patrick Mezard <pmezard@gmail.com>
parents:
6280
diff
changeset
|
740 isbackup = (abase == bbase and bfile.startswith(afile)) |
5652
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
741 fname = None |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
742 if not missing: |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
743 if gooda and goodb: |
6295
bace1990ab12
patch: fix corner case with update + copy patch handling (issue 937)
Patrick Mezard <pmezard@gmail.com>
parents:
6280
diff
changeset
|
744 fname = isbackup and afile or bfile |
5652
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
745 elif gooda: |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
746 fname = afile |
5760
0145f9afb0e7
Removed tabs and trailing whitespace in python files
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5706
diff
changeset
|
747 |
5652
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
748 if not fname: |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
749 if not nullb: |
6295
bace1990ab12
patch: fix corner case with update + copy patch handling (issue 937)
Patrick Mezard <pmezard@gmail.com>
parents:
6280
diff
changeset
|
750 fname = isbackup and afile or bfile |
5652
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
751 elif not nulla: |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
752 fname = afile |
5652
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
753 else: |
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
754 raise PatchError(_("undefined source and destination files")) |
5760
0145f9afb0e7
Removed tabs and trailing whitespace in python files
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5706
diff
changeset
|
755 |
5652
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
756 return fname, missing |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
757 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
758 class linereader: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
759 # simple class to allow pushing lines back into the input stream |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
760 def __init__(self, fp): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
761 self.fp = fp |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
762 self.buf = [] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
763 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
764 def push(self, line): |
7152
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
765 if line is not None: |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
766 self.buf.append(line) |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
767 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
768 def readline(self): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
769 if self.buf: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
770 l = self.buf[0] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
771 del self.buf[0] |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
772 return l |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
773 return self.fp.readline() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
774 |
7152
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
775 def __iter__(self): |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
776 while 1: |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
777 l = self.readline() |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
778 if not l: |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
779 break |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
780 yield l |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
781 |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
782 def scangitpatch(lr, firstline): |
7186
f77c8d8331ca
clean up trailing spaces, leading spaces in C
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7153
diff
changeset
|
783 """ |
7152
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
784 Git patches can emit: |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
785 - rename a to b |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
786 - change b |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
787 - copy a to c |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
788 - change c |
7186
f77c8d8331ca
clean up trailing spaces, leading spaces in C
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7153
diff
changeset
|
789 |
7152
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
790 We cannot apply this sequence as-is, the renamed 'a' could not be |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
791 found for it would have been renamed already. And we cannot copy |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
792 from 'b' instead because 'b' would have been changed already. So |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
793 we scan the git patch for copy and rename commands so we can |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
794 perform the copies ahead of time. |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
795 """ |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
796 pos = 0 |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
797 try: |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
798 pos = lr.fp.tell() |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
799 fp = lr.fp |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
800 except IOError: |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
801 fp = cStringIO.StringIO(lr.fp.read()) |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
802 gitlr = linereader(fp) |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
803 gitlr.push(firstline) |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
804 (dopatch, gitpatches) = readgitpatch(gitlr) |
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
805 fp.seek(pos) |
7153
353141d74ca8
patch: pass linereader to binaryhunk.extract() instead of wrapped fp
Patrick Mezard <pmezard@gmail.com>
parents:
7152
diff
changeset
|
806 return dopatch, gitpatches |
7152
f0055cec8446
patch: pass linereader to scangitpatch(), extract from iterhunks()
Patrick Mezard <pmezard@gmail.com>
parents:
7151
diff
changeset
|
807 |
5650
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
808 def iterhunks(ui, fp, sourcefile=None): |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
809 """Read a patch and yield the following events: |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
810 - ("file", afile, bfile, firsthunk): select a new target file. |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
811 - ("hunk", hunk): a new hunk is ready to be applied, follows a |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
812 "file" event. |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
813 - ("git", gitchanges): current diff is in git format, gitchanges |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
814 maps filenames to gitpatch records. Unique event. |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
815 """ |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
816 changed = {} |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
817 current_hunk = None |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
818 afile = "" |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
819 bfile = "" |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
820 state = None |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
821 hunknum = 0 |
5650
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
822 emitfile = False |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
823 git = False |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
824 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
825 # our states |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
826 BFILE = 1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
827 context = None |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
828 lr = linereader(fp) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
829 dopatch = True |
6179
36ab165abbe2
patch: fix iterhunks() with trailing binary file removal
Patrick Mezard <pmezard@gmail.com>
parents:
6042
diff
changeset
|
830 # gitworkdone is True if a git operation (copy, rename, ...) was |
36ab165abbe2
patch: fix iterhunks() with trailing binary file removal
Patrick Mezard <pmezard@gmail.com>
parents:
6042
diff
changeset
|
831 # performed already for the current file. Useful when the file |
36ab165abbe2
patch: fix iterhunks() with trailing binary file removal
Patrick Mezard <pmezard@gmail.com>
parents:
6042
diff
changeset
|
832 # section may have no hunk. |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
833 gitworkdone = False |
2861
0f08f2c042ec
Move patch-related code into its own module.
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
834 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
835 while True: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
836 newfile = False |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
837 x = lr.readline() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
838 if not x: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
839 break |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
840 if current_hunk: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
841 if x.startswith('\ '): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
842 current_hunk.fix_newline() |
5650
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
843 yield 'hunk', current_hunk |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
844 current_hunk = None |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
845 gitworkdone = False |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
846 if ((sourcefile or state == BFILE) and ((not context and x[0] == '@') or |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
847 ((context or context == None) and x.startswith('***************')))): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
848 try: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
849 if context == None and x.startswith('***************'): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
850 context = True |
7199
dd891d0d97a3
patch: consolidate two different regexes for parsing of git diffs
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7198
diff
changeset
|
851 gpatch = changed.get(bfile) |
6280
9db24a36d182
patch: check filename is /dev/null for creation or deletion (issue 1033)
Patrick Mezard <pmezard@gmail.com>
parents:
6275
diff
changeset
|
852 create = afile == '/dev/null' or gpatch and gpatch.op == 'ADD' |
9db24a36d182
patch: check filename is /dev/null for creation or deletion (issue 1033)
Patrick Mezard <pmezard@gmail.com>
parents:
6275
diff
changeset
|
853 remove = bfile == '/dev/null' or gpatch and gpatch.op == 'DELETE' |
9db24a36d182
patch: check filename is /dev/null for creation or deletion (issue 1033)
Patrick Mezard <pmezard@gmail.com>
parents:
6275
diff
changeset
|
854 current_hunk = hunk(x, hunknum + 1, lr, context, create, remove) |
4898
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
855 except PatchError, err: |
bc905a6c0e76
patch.py: fix some incorrect uses of _() for i18n
Bryan O'Sullivan <bos@serpentine.com>
parents:
4897
diff
changeset
|
856 ui.debug(err) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
857 current_hunk = None |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
858 continue |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
859 hunknum += 1 |
5650
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
860 if emitfile: |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
861 emitfile = False |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
862 yield 'file', (afile, bfile, current_hunk) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
863 elif state == BFILE and x.startswith('GIT binary patch'): |
7199
dd891d0d97a3
patch: consolidate two different regexes for parsing of git diffs
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7198
diff
changeset
|
864 current_hunk = binhunk(changed[bfile]) |
5581
8a8c341bd292
mq: missing target files do not make qpush to fail immediately (issue 835)
Patrick Mezard <pmezard@gmail.com>
parents:
5547
diff
changeset
|
865 hunknum += 1 |
5650
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
866 if emitfile: |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
867 emitfile = False |
7199
dd891d0d97a3
patch: consolidate two different regexes for parsing of git diffs
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7198
diff
changeset
|
868 yield 'file', ('a/' + afile, 'b/' + bfile, current_hunk) |
7153
353141d74ca8
patch: pass linereader to binaryhunk.extract() instead of wrapped fp
Patrick Mezard <pmezard@gmail.com>
parents:
7152
diff
changeset
|
869 current_hunk.extract(lr) |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
870 elif x.startswith('diff --git'): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
871 # check for git diff, scanning the whole patch file if needed |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
872 m = gitre.match(x) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
873 if m: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
874 afile, bfile = m.group(1, 2) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
875 if not git: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
876 git = True |
7153
353141d74ca8
patch: pass linereader to binaryhunk.extract() instead of wrapped fp
Patrick Mezard <pmezard@gmail.com>
parents:
7152
diff
changeset
|
877 dopatch, gitpatches = scangitpatch(lr, x) |
5650
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
878 yield 'git', gitpatches |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
879 for gp in gitpatches: |
7150
6d1d61bb2984
patch: map changed files to patchmeta directly
Patrick Mezard <pmezard@gmail.com>
parents:
7149
diff
changeset
|
880 changed[gp.path] = gp |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
881 # else error? |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
882 # copy/rename + modify should modify target, not source |
7199
dd891d0d97a3
patch: consolidate two different regexes for parsing of git diffs
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7198
diff
changeset
|
883 gp = changed.get(bfile) |
7150
6d1d61bb2984
patch: map changed files to patchmeta directly
Patrick Mezard <pmezard@gmail.com>
parents:
7149
diff
changeset
|
884 if gp and gp.op in ('COPY', 'DELETE', 'RENAME'): |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
885 afile = bfile |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
886 gitworkdone = True |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
887 newfile = True |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
888 elif x.startswith('---'): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
889 # check for a unified diff |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
890 l2 = lr.readline() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
891 if not l2.startswith('+++'): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
892 lr.push(l2) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
893 continue |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
894 newfile = True |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
895 context = False |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
896 afile = parsefilename(x) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
897 bfile = parsefilename(l2) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
898 elif x.startswith('***'): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
899 # check for a context diff |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
900 l2 = lr.readline() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
901 if not l2.startswith('---'): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
902 lr.push(l2) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
903 continue |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
904 l3 = lr.readline() |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
905 lr.push(l3) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
906 if not l3.startswith("***************"): |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
907 lr.push(l2) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
908 continue |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
909 newfile = True |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
910 context = True |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
911 afile = parsefilename(x) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
912 bfile = parsefilename(l2) |
3057
d16b93f4a6ca
unlink temporary patch files even when an exception is raised
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3056
diff
changeset
|
913 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
914 if newfile: |
5650
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
915 emitfile = True |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
916 state = BFILE |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
917 hunknum = 0 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
918 if current_hunk: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
919 if current_hunk.complete(): |
5650
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
920 yield 'hunk', current_hunk |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
921 else: |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
922 raise PatchError(_("malformed patch %s %s") % (afile, |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
923 current_hunk.desc)) |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
924 |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
925 if hunknum == 0 and dopatch and not gitworkdone: |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
926 raise NoHunks |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
927 |
7147
94cf0d1f48a3
patch: remove applydiff() useless updatedir and rejmerge arguments
Patrick Mezard <pmezard@gmail.com>
parents:
7106
diff
changeset
|
928 def applydiff(ui, fp, changed, strip=1, sourcefile=None, reverse=False): |
5650
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
929 """reads a patch from fp and tries to apply it. The dict 'changed' is |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
930 filled in with all of the filenames changed by the patch. Returns 0 |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
931 for a clean patch, -1 if any rejects were found and 1 if there was |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
932 any fuzz.""" |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
933 |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
934 rejects = 0 |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
935 err = 0 |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
936 current_file = None |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
937 gitpatches = None |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
938 |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
939 def closefile(): |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
940 if not current_file: |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
941 return 0 |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
942 current_file.close() |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
943 return len(current_file.rej) |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
944 |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
945 for state, values in iterhunks(ui, fp, sourcefile): |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
946 if state == 'hunk': |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
947 if not current_file: |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
948 continue |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
949 current_hunk = values |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
950 ret = current_file.apply(current_hunk, reverse) |
4899
1b7bbc4349e7
patch.py: don't mark files as changed unless they have actually been changed
Bryan O'Sullivan <bos@serpentine.com>
parents:
4898
diff
changeset
|
951 if ret >= 0: |
7150
6d1d61bb2984
patch: map changed files to patchmeta directly
Patrick Mezard <pmezard@gmail.com>
parents:
7149
diff
changeset
|
952 changed.setdefault(current_file.fname, None) |
4899
1b7bbc4349e7
patch.py: don't mark files as changed unless they have actually been changed
Bryan O'Sullivan <bos@serpentine.com>
parents:
4898
diff
changeset
|
953 if ret > 0: |
1b7bbc4349e7
patch.py: don't mark files as changed unless they have actually been changed
Bryan O'Sullivan <bos@serpentine.com>
parents:
4898
diff
changeset
|
954 err = 1 |
5650
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
955 elif state == 'file': |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
956 rejects += closefile() |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
957 afile, bfile, first_hunk = values |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
958 try: |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
959 if sourcefile: |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
960 current_file = patchfile(ui, sourcefile) |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
961 else: |
5652
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
962 current_file, missing = selectfile(afile, bfile, first_hunk, |
5650
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
963 strip, reverse) |
5652
e90e72c6b4c7
patch: write rej files for missing targets (issue 853)
Patrick Mezard <pmezard@gmail.com>
parents:
5651
diff
changeset
|
964 current_file = patchfile(ui, current_file, missing) |
5650
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
965 except PatchError, err: |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
966 ui.warn(str(err) + '\n') |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
967 current_file, current_hunk = None, None |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
968 rejects += 1 |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
969 continue |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
970 elif state == 'git': |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
971 gitpatches = values |
6791
bbd89c9e6012
Check that git patches only touch files under root
Brendan Cully <brendan@kublai.com>
parents:
6520
diff
changeset
|
972 cwd = os.getcwd() |
5650
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
973 for gp in gitpatches: |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
974 if gp.op in ('COPY', 'RENAME'): |
6791
bbd89c9e6012
Check that git patches only touch files under root
Brendan Cully <brendan@kublai.com>
parents:
6520
diff
changeset
|
975 src, dst = [util.canonpath(cwd, cwd, x) |
bbd89c9e6012
Check that git patches only touch files under root
Brendan Cully <brendan@kublai.com>
parents:
6520
diff
changeset
|
976 for x in [gp.oldpath, gp.path]] |
bbd89c9e6012
Check that git patches only touch files under root
Brendan Cully <brendan@kublai.com>
parents:
6520
diff
changeset
|
977 copyfile(src, dst) |
7150
6d1d61bb2984
patch: map changed files to patchmeta directly
Patrick Mezard <pmezard@gmail.com>
parents:
7149
diff
changeset
|
978 changed[gp.path] = gp |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
979 else: |
5650
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
980 raise util.Abort(_('unsupported parser state: %s') % state) |
5649
a583117b536a
patch: move NoHunk detection up with parsing code
Patrick Mezard <pmezard@gmail.com>
parents:
5581
diff
changeset
|
981 |
5650
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
982 rejects += closefile() |
5d3e2f918d65
patch: move diff parsing in iterhunks generator
Patrick Mezard <pmezard@gmail.com>
parents:
5649
diff
changeset
|
983 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
984 if rejects: |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
985 return -1 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4778
diff
changeset
|
986 return err |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
987 |
3554
da3ee7ca620f
add untrusted argument to patch.diffopts
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3473
diff
changeset
|
988 def diffopts(ui, opts={}, untrusted=False): |
6467
65029a3aafc2
Let --unified default to diff.unified (issue 1076)
Patrick Mezard <pmezard@gmail.com>
parents:
6295
diff
changeset
|
989 def get(key, name=None, getter=ui.configbool): |
3554
da3ee7ca620f
add untrusted argument to patch.diffopts
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3473
diff
changeset
|
990 return (opts.get(key) or |
6467
65029a3aafc2
Let --unified default to diff.unified (issue 1076)
Patrick Mezard <pmezard@gmail.com>
parents:
6295
diff
changeset
|
991 getter('diff', name or key, None, untrusted=untrusted)) |
2888
3848488244fc
Move ui.diffopts to patch.diffopts where it belongs
Matt Mackall <mpm@selenic.com>
parents:
2881
diff
changeset
|
992 return mdiff.diffopts( |
3848488244fc
Move ui.diffopts to patch.diffopts where it belongs
Matt Mackall <mpm@selenic.com>
parents:
2881
diff
changeset
|
993 text=opts.get('text'), |
3554
da3ee7ca620f
add untrusted argument to patch.diffopts
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3473
diff
changeset
|
994 git=get('git'), |
da3ee7ca620f
add untrusted argument to patch.diffopts
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3473
diff
changeset
|
995 nodates=get('nodates'), |
da3ee7ca620f
add untrusted argument to patch.diffopts
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3473
diff
changeset
|
996 showfunc=get('show_function', 'showfunc'), |
da3ee7ca620f
add untrusted argument to patch.diffopts
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3473
diff
changeset
|
997 ignorews=get('ignore_all_space', 'ignorews'), |
da3ee7ca620f
add untrusted argument to patch.diffopts
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3473
diff
changeset
|
998 ignorewsamount=get('ignore_space_change', 'ignorewsamount'), |
6040
1d0bfa4c75c0
commands.py, patch.py: add -U option to hg diff command
jorendorff@mozilla.com
parents:
4965
diff
changeset
|
999 ignoreblanklines=get('ignore_blank_lines', 'ignoreblanklines'), |
6467
65029a3aafc2
Let --unified default to diff.unified (issue 1076)
Patrick Mezard <pmezard@gmail.com>
parents:
6295
diff
changeset
|
1000 context=get('unified', getter=ui.config)) |
2888
3848488244fc
Move ui.diffopts to patch.diffopts where it belongs
Matt Mackall <mpm@selenic.com>
parents:
2881
diff
changeset
|
1001 |
4917
126f527b3ba3
Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents:
4900
diff
changeset
|
1002 def updatedir(ui, repo, patches): |
2933
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
1003 '''Update dirstate after patch application according to metadata''' |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
1004 if not patches: |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
1005 return |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
1006 copies = [] |
3701
05c8704a3743
handle git patches that rename a file to more than one destination
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3696
diff
changeset
|
1007 removes = {} |
2933
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
1008 cfiles = patches.keys() |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
1009 cwd = repo.getcwd() |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
1010 if cwd: |
4229
24c22a3f2ef8
pass repo.root to util.pathto() in preparation for the next patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4106
diff
changeset
|
1011 cfiles = [util.pathto(repo.root, cwd, f) for f in patches.keys()] |
2933
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
1012 for f in patches: |
7150
6d1d61bb2984
patch: map changed files to patchmeta directly
Patrick Mezard <pmezard@gmail.com>
parents:
7149
diff
changeset
|
1013 gp = patches[f] |
6d1d61bb2984
patch: map changed files to patchmeta directly
Patrick Mezard <pmezard@gmail.com>
parents:
7149
diff
changeset
|
1014 if not gp: |
6d1d61bb2984
patch: map changed files to patchmeta directly
Patrick Mezard <pmezard@gmail.com>
parents:
7149
diff
changeset
|
1015 continue |
6d1d61bb2984
patch: map changed files to patchmeta directly
Patrick Mezard <pmezard@gmail.com>
parents:
7149
diff
changeset
|
1016 if gp.op == 'RENAME': |
5403
477136fa6571
Always copy the necessary files before applying a git patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5265
diff
changeset
|
1017 copies.append((gp.oldpath, gp.path)) |
3701
05c8704a3743
handle git patches that rename a file to more than one destination
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3696
diff
changeset
|
1018 removes[gp.oldpath] = 1 |
7150
6d1d61bb2984
patch: map changed files to patchmeta directly
Patrick Mezard <pmezard@gmail.com>
parents:
7149
diff
changeset
|
1019 elif gp.op == 'COPY': |
5403
477136fa6571
Always copy the necessary files before applying a git patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5265
diff
changeset
|
1020 copies.append((gp.oldpath, gp.path)) |
7150
6d1d61bb2984
patch: map changed files to patchmeta directly
Patrick Mezard <pmezard@gmail.com>
parents:
7149
diff
changeset
|
1021 elif gp.op == 'DELETE': |
3701
05c8704a3743
handle git patches that rename a file to more than one destination
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3696
diff
changeset
|
1022 removes[gp.path] = 1 |
5403
477136fa6571
Always copy the necessary files before applying a git patch
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5265
diff
changeset
|
1023 for src, dst in copies: |
4917
126f527b3ba3
Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents:
4900
diff
changeset
|
1024 repo.copy(src, dst) |
3701
05c8704a3743
handle git patches that rename a file to more than one destination
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3696
diff
changeset
|
1025 removes = removes.keys() |
2933
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
1026 if removes: |
6762 | 1027 repo.remove(util.sort(removes), True) |
2933
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
1028 for f in patches: |
7150
6d1d61bb2984
patch: map changed files to patchmeta directly
Patrick Mezard <pmezard@gmail.com>
parents:
7149
diff
changeset
|
1029 gp = patches[f] |
2933
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
1030 if gp and gp.mode: |
7149
01a056c54385
patch: patchmeta gives (islink, isexec) tuple instead of int mode
Patrick Mezard <pmezard@gmail.com>
parents:
7148
diff
changeset
|
1031 islink, isexec = gp.mode |
2933
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
1032 dst = os.path.join(repo.root, gp.path) |
3588
45574a225632
git patch: create empty added files
Brendan Cully <brendan@kublai.com>
parents:
3554
diff
changeset
|
1033 # patch won't create empty files |
7150
6d1d61bb2984
patch: map changed files to patchmeta directly
Patrick Mezard <pmezard@gmail.com>
parents:
7149
diff
changeset
|
1034 if gp.op == 'ADD' and not os.path.exists(dst): |
7149
01a056c54385
patch: patchmeta gives (islink, isexec) tuple instead of int mode
Patrick Mezard <pmezard@gmail.com>
parents:
7148
diff
changeset
|
1035 flags = (isexec and 'x' or '') + (islink and 'l' or '') |
5706 | 1036 repo.wwrite(gp.path, '', flags) |
4006
67982d3ee76c
symlinks: add flags param to wwrite
Matt Mackall <mpm@selenic.com>
parents:
3996
diff
changeset
|
1037 else: |
7149
01a056c54385
patch: patchmeta gives (islink, isexec) tuple instead of int mode
Patrick Mezard <pmezard@gmail.com>
parents:
7148
diff
changeset
|
1038 util.set_flags(dst, islink, isexec) |
4917
126f527b3ba3
Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents:
4900
diff
changeset
|
1039 cmdutil.addremove(repo, cfiles) |
2933
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
1040 files = patches.keys() |
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
1041 files.extend([r for r in removes if r not in files]) |
6762 | 1042 return util.sort(files) |
2933
439fd013360d
Move import's working dir update code into patch.updatedir
Brendan Cully <brendan@kublai.com>
parents:
2922
diff
changeset
|
1043 |
7151
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1044 def externalpatch(patcher, args, patchname, ui, strip, cwd, files): |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1045 """use <patcher> to apply <patchname> to the working directory. |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1046 returns whether patch was applied with fuzz factor.""" |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1047 |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1048 fuzz = False |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1049 if cwd: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1050 args.append('-d %s' % util.shellquote(cwd)) |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1051 fp = util.popen('%s %s -p%d < %s' % (patcher, ' '.join(args), strip, |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1052 util.shellquote(patchname))) |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1053 |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1054 for line in fp: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1055 line = line.rstrip() |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1056 ui.note(line + '\n') |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1057 if line.startswith('patching file '): |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1058 pf = util.parse_patch_output(line) |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1059 printed_file = False |
7247
c4461ea8b4c8
patch: fix patched files records in externalpatcher()
Patrick Mezard <pmezard@gmail.com>
parents:
7244
diff
changeset
|
1060 files.setdefault(pf, None) |
7151
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1061 elif line.find('with fuzz') >= 0: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1062 fuzz = True |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1063 if not printed_file: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1064 ui.warn(pf + '\n') |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1065 printed_file = True |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1066 ui.warn(line + '\n') |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1067 elif line.find('saving rejects to file') >= 0: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1068 ui.warn(line + '\n') |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1069 elif line.find('FAILED') >= 0: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1070 if not printed_file: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1071 ui.warn(pf + '\n') |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1072 printed_file = True |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1073 ui.warn(line + '\n') |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1074 code = fp.close() |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1075 if code: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1076 raise PatchError(_("patch command failed: %s") % |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1077 util.explain_exit(code)[0]) |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1078 return fuzz |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1079 |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1080 def internalpatch(patchobj, ui, strip, cwd, files={}): |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1081 """use builtin patch to apply <patchobj> to the working directory. |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1082 returns whether patch was applied with fuzz factor.""" |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1083 try: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1084 fp = file(patchobj, 'rb') |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1085 except TypeError: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1086 fp = patchobj |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1087 if cwd: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1088 curdir = os.getcwd() |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1089 os.chdir(cwd) |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1090 try: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1091 ret = applydiff(ui, fp, files, strip=strip) |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1092 finally: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1093 if cwd: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1094 os.chdir(curdir) |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1095 if ret < 0: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1096 raise PatchError |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1097 return ret > 0 |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1098 |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1099 def patch(patchname, ui, strip=1, cwd=None, files={}): |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1100 """apply <patchname> to the working directory. |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1101 returns whether patch was applied with fuzz factor.""" |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1102 patcher = ui.config('ui', 'patch') |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1103 args = [] |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1104 try: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1105 if patcher: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1106 return externalpatch(patcher, args, patchname, ui, strip, cwd, |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1107 files) |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1108 else: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1109 try: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1110 return internalpatch(patchname, ui, strip, cwd, files) |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1111 except NoHunks: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1112 patcher = util.find_exe('gpatch') or util.find_exe('patch') |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1113 ui.debug(_('no valid hunks found; trying with %r instead\n') % |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1114 patcher) |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1115 if util.needbinarypatch(): |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1116 args.append('--binary') |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1117 return externalpatch(patcher, args, patchname, ui, strip, cwd, |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1118 files) |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1119 except PatchError, err: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1120 s = str(err) |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1121 if s: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1122 raise util.Abort(s) |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1123 else: |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1124 raise util.Abort(_('patch failed to apply')) |
b5bc5293021c
patch: change functions definition order for readability
Patrick Mezard <pmezard@gmail.com>
parents:
7150
diff
changeset
|
1125 |
5033
1b07668b8cc3
patch: remove unused parameter from b85diff
Bryan O'Sullivan <bos@serpentine.com>
parents:
4965
diff
changeset
|
1126 def b85diff(to, tn): |
3367
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1127 '''print base85-encoded binary diff''' |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1128 def gitindex(text): |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1129 if not text: |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1130 return '0' * 40 |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1131 l = len(text) |
6470
ac0bcd951c2c
python 2.6 compatibility: compatibility wrappers for hash functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6467
diff
changeset
|
1132 s = util.sha1('blob %d\0' % l) |
3367
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1133 s.update(text) |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1134 return s.hexdigest() |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1135 |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1136 def fmtline(line): |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1137 l = len(line) |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1138 if l <= 26: |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1139 l = chr(ord('A') + l - 1) |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1140 else: |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1141 l = chr(l - 26 + ord('a') - 1) |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1142 return '%c%s\n' % (l, base85.b85encode(line, True)) |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1143 |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1144 def chunk(text, csize=52): |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1145 l = len(text) |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1146 i = 0 |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1147 while i < l: |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1148 yield text[i:i+csize] |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1149 i += csize |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1150 |
4105
ed46895aa38c
git binary patches: use hashes to detect identical files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4092
diff
changeset
|
1151 tohash = gitindex(to) |
ed46895aa38c
git binary patches: use hashes to detect identical files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4092
diff
changeset
|
1152 tnhash = gitindex(tn) |
ed46895aa38c
git binary patches: use hashes to detect identical files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4092
diff
changeset
|
1153 if tohash == tnhash: |
4106
797dbdd4d7e1
git binary patches: don't print the header for identical files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4105
diff
changeset
|
1154 return "" |
797dbdd4d7e1
git binary patches: don't print the header for identical files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4105
diff
changeset
|
1155 |
3367
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1156 # TODO: deltas |
4106
797dbdd4d7e1
git binary patches: don't print the header for identical files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4105
diff
changeset
|
1157 ret = ['index %s..%s\nGIT binary patch\nliteral %s\n' % |
797dbdd4d7e1
git binary patches: don't print the header for identical files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4105
diff
changeset
|
1158 (tohash, tnhash, len(tn))] |
797dbdd4d7e1
git binary patches: don't print the header for identical files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4105
diff
changeset
|
1159 for l in chunk(zlib.compress(tn)): |
797dbdd4d7e1
git binary patches: don't print the header for identical files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4105
diff
changeset
|
1160 ret.append(fmtline(l)) |
797dbdd4d7e1
git binary patches: don't print the header for identical files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4105
diff
changeset
|
1161 ret.append('\n') |
797dbdd4d7e1
git binary patches: don't print the header for identical files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4105
diff
changeset
|
1162 return ''.join(ret) |
3367
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1163 |
7198
df79ee9b6278
patch: extract local function addmodehdr
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7186
diff
changeset
|
1164 def _addmodehdr(header, omode, nmode): |
df79ee9b6278
patch: extract local function addmodehdr
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7186
diff
changeset
|
1165 if omode != nmode: |
df79ee9b6278
patch: extract local function addmodehdr
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7186
diff
changeset
|
1166 header.append('old mode %s\n' % omode) |
df79ee9b6278
patch: extract local function addmodehdr
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7186
diff
changeset
|
1167 header.append('new mode %s\n' % nmode) |
df79ee9b6278
patch: extract local function addmodehdr
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7186
diff
changeset
|
1168 |
7308
b6f5490effbf
patch: turn patch.diff() into a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7267
diff
changeset
|
1169 def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None): |
b6f5490effbf
patch: turn patch.diff() into a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7267
diff
changeset
|
1170 '''yields diff of changes to files between two nodes, or node and |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1171 working directory. |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1172 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1173 if node1 is None, use first dirstate parent instead. |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1174 if node2 is None, compare node1 with working directory.''' |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1175 |
6602
a57a27b12965
match: remove files argument from patch.diff
Matt Mackall <mpm@selenic.com>
parents:
6532
diff
changeset
|
1176 if not match: |
a57a27b12965
match: remove files argument from patch.diff
Matt Mackall <mpm@selenic.com>
parents:
6532
diff
changeset
|
1177 match = cmdutil.matchall(repo) |
a57a27b12965
match: remove files argument from patch.diff
Matt Mackall <mpm@selenic.com>
parents:
6532
diff
changeset
|
1178 |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1179 if opts is None: |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1180 opts = mdiff.defaultopts |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1181 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1182 if not node1: |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1183 node1 = repo.dirstate.parents()[0] |
2934
2f190e998eb3
Teach mq about git patches
Brendan Cully <brendan@kublai.com>
parents:
2933
diff
changeset
|
1184 |
3967
dccb83241dd0
patch: use contexts for diff
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3963
diff
changeset
|
1185 flcache = {} |
dccb83241dd0
patch: use contexts for diff
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3963
diff
changeset
|
1186 def getfilectx(f, ctx): |
dccb83241dd0
patch: use contexts for diff
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3963
diff
changeset
|
1187 flctx = ctx.filectx(f, filelog=flcache.get(f)) |
dccb83241dd0
patch: use contexts for diff
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3963
diff
changeset
|
1188 if f not in flcache: |
dccb83241dd0
patch: use contexts for diff
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3963
diff
changeset
|
1189 flcache[f] = flctx._filelog |
dccb83241dd0
patch: use contexts for diff
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3963
diff
changeset
|
1190 return flctx |
2934
2f190e998eb3
Teach mq about git patches
Brendan Cully <brendan@kublai.com>
parents:
2933
diff
changeset
|
1191 |
6747
f6c00b17387c
use repo[changeid] to get a changectx
Matt Mackall <mpm@selenic.com>
parents:
6743
diff
changeset
|
1192 ctx1 = repo[node1] |
7090
7b5c063b0b94
diff: pass contexts to status
Matt Mackall <mpm@selenic.com>
parents:
6953
diff
changeset
|
1193 ctx2 = repo[node2] |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1194 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1195 if not changes: |
7090
7b5c063b0b94
diff: pass contexts to status
Matt Mackall <mpm@selenic.com>
parents:
6953
diff
changeset
|
1196 changes = repo.status(ctx1, ctx2, match=match) |
6760
4faaa0535ea7
status: clean up all users for unknown files
Matt Mackall <mpm@selenic.com>
parents:
6758
diff
changeset
|
1197 modified, added, removed = changes[:3] |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1198 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1199 if not modified and not added and not removed: |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1200 return |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1201 |
7090
7b5c063b0b94
diff: pass contexts to status
Matt Mackall <mpm@selenic.com>
parents:
6953
diff
changeset
|
1202 date1 = util.datestr(ctx1.date()) |
7b5c063b0b94
diff: pass contexts to status
Matt Mackall <mpm@selenic.com>
parents:
6953
diff
changeset
|
1203 man1 = ctx1.manifest() |
3967
dccb83241dd0
patch: use contexts for diff
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3963
diff
changeset
|
1204 |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1205 if repo.ui.quiet: |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1206 r = None |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1207 else: |
3387
2065789f6a3e
use short hashes with diff -v
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3378
diff
changeset
|
1208 hexfunc = repo.ui.debugflag and hex or short |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1209 r = [hexfunc(node) for node in [node1, node2] if node] |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1210 |
2907 | 1211 if opts.git: |
6747
f6c00b17387c
use repo[changeid] to get a changectx
Matt Mackall <mpm@selenic.com>
parents:
6743
diff
changeset
|
1212 copy, diverge = copies.copies(repo, ctx1, ctx2, repo[nullid]) |
6275
fda369b5779c
diff: use copy smarts from copies.py
Matt Mackall <mpm@selenic.com>
parents:
6211
diff
changeset
|
1213 for k, v in copy.items(): |
fda369b5779c
diff: use copy smarts from copies.py
Matt Mackall <mpm@selenic.com>
parents:
6211
diff
changeset
|
1214 copy[v] = k |
2907 | 1215 |
3702
70c3ee224c08
Don't generate git patches that rename a file to multiple destinations
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3701
diff
changeset
|
1216 gone = {} |
6743 | 1217 gitmode = {'l': '120000', 'x': '100755', '': '100644'} |
3996
c190df14338c
exec: add execfunc to simplify exec flag support on non-exec filesystems
Matt Mackall <mpm@selenic.com>
parents:
3970
diff
changeset
|
1218 |
6762 | 1219 for f in util.sort(modified + added + removed): |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1220 to = None |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1221 tn = None |
2907 | 1222 dodiff = True |
3329
319358e6bd96
Don't generate git diff header for empty diffs
Brendan Cully <brendan@kublai.com>
parents:
3231
diff
changeset
|
1223 header = [] |
3967
dccb83241dd0
patch: use contexts for diff
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3963
diff
changeset
|
1224 if f in man1: |
dccb83241dd0
patch: use contexts for diff
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3963
diff
changeset
|
1225 to = getfilectx(f, ctx1).data() |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1226 if f not in removed: |
3967
dccb83241dd0
patch: use contexts for diff
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3963
diff
changeset
|
1227 tn = getfilectx(f, ctx2).data() |
5482
e5eedd74e70f
Use both the from and to name in mdiff.unidiff.
Dustin Sallings <dustin@spy.net>
parents:
5481
diff
changeset
|
1228 a, b = f, f |
2907 | 1229 if opts.git: |
1230 if f in added: | |
6743 | 1231 mode = gitmode[ctx2.flags(f)] |
6275
fda369b5779c
diff: use copy smarts from copies.py
Matt Mackall <mpm@selenic.com>
parents:
6211
diff
changeset
|
1232 if f in copy: |
fda369b5779c
diff: use copy smarts from copies.py
Matt Mackall <mpm@selenic.com>
parents:
6211
diff
changeset
|
1233 a = copy[f] |
6743 | 1234 omode = gitmode[man1.flags(a)] |
7198
df79ee9b6278
patch: extract local function addmodehdr
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7186
diff
changeset
|
1235 _addmodehdr(header, omode, mode) |
3702
70c3ee224c08
Don't generate git patches that rename a file to multiple destinations
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3701
diff
changeset
|
1236 if a in removed and a not in gone: |
70c3ee224c08
Don't generate git patches that rename a file to multiple destinations
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3701
diff
changeset
|
1237 op = 'rename' |
70c3ee224c08
Don't generate git patches that rename a file to multiple destinations
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3701
diff
changeset
|
1238 gone[a] = 1 |
70c3ee224c08
Don't generate git patches that rename a file to multiple destinations
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3701
diff
changeset
|
1239 else: |
70c3ee224c08
Don't generate git patches that rename a file to multiple destinations
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3701
diff
changeset
|
1240 op = 'copy' |
2907 | 1241 header.append('%s from %s\n' % (op, a)) |
1242 header.append('%s to %s\n' % (op, f)) | |
3967
dccb83241dd0
patch: use contexts for diff
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3963
diff
changeset
|
1243 to = getfilectx(a, ctx1).data() |
2907 | 1244 else: |
1245 header.append('new file mode %s\n' % mode) | |
4092
4ced663bebf0
git patches: handle renames of binary files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3900
diff
changeset
|
1246 if util.binary(tn): |
4ced663bebf0
git patches: handle renames of binary files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3900
diff
changeset
|
1247 dodiff = 'binary' |
2907 | 1248 elif f in removed: |
6275
fda369b5779c
diff: use copy smarts from copies.py
Matt Mackall <mpm@selenic.com>
parents:
6211
diff
changeset
|
1249 # have we already reported a copy above? |
fda369b5779c
diff: use copy smarts from copies.py
Matt Mackall <mpm@selenic.com>
parents:
6211
diff
changeset
|
1250 if f in copy and copy[f] in added and copy[copy[f]] == f: |
2907 | 1251 dodiff = False |
1252 else: | |
6743 | 1253 header.append('deleted file mode %s\n' % |
1254 gitmode[man1.flags(f)]) | |
2907 | 1255 else: |
6743 | 1256 omode = gitmode[man1.flags(f)] |
1257 nmode = gitmode[ctx2.flags(f)] | |
7198
df79ee9b6278
patch: extract local function addmodehdr
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7186
diff
changeset
|
1258 _addmodehdr(header, omode, nmode) |
3367
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1259 if util.binary(to) or util.binary(tn): |
7f486971d263
Add git-1.4 binary patch support
Brendan Cully <brendan@kublai.com>
parents:
3329
diff
changeset
|
1260 dodiff = 'binary' |
2907 | 1261 r = None |
7200
ca5ac40949dc
patch/diff: use a separate function to write the first line of a file diff
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7199
diff
changeset
|
1262 header.insert(0, mdiff.diffline(r, a, b, opts)) |
4106
797dbdd4d7e1
git binary patches: don't print the header for identical files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4105
diff
changeset
|
1263 if dodiff: |
797dbdd4d7e1
git binary patches: don't print the header for identical files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4105
diff
changeset
|
1264 if dodiff == 'binary': |
5033
1b07668b8cc3
patch: remove unused parameter from b85diff
Bryan O'Sullivan <bos@serpentine.com>
parents:
4965
diff
changeset
|
1265 text = b85diff(to, tn) |
4106
797dbdd4d7e1
git binary patches: don't print the header for identical files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4105
diff
changeset
|
1266 else: |
4108 | 1267 text = mdiff.unidiff(to, date1, |
1268 # ctx2 date may be dynamic | |
1269 tn, util.datestr(ctx2.date()), | |
5482
e5eedd74e70f
Use both the from and to name in mdiff.unidiff.
Dustin Sallings <dustin@spy.net>
parents:
5481
diff
changeset
|
1270 a, b, r, opts=opts) |
7308
b6f5490effbf
patch: turn patch.diff() into a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7267
diff
changeset
|
1271 if header and (text or len(header) > 1): |
b6f5490effbf
patch: turn patch.diff() into a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7267
diff
changeset
|
1272 yield ''.join(header) |
b6f5490effbf
patch: turn patch.diff() into a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7267
diff
changeset
|
1273 if text: |
b6f5490effbf
patch: turn patch.diff() into a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7267
diff
changeset
|
1274 yield text |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1275 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1276 def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False, |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1277 opts=None): |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1278 '''export changesets as hg patches.''' |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1279 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1280 total = len(revs) |
3900
2b3175acb653
Don't use node length for calculating revision number length.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3899
diff
changeset
|
1281 revwidth = max([len(str(rev)) for rev in revs]) |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1282 |
3970
fff8a5345eb0
commands.py: use contexts in export
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3967
diff
changeset
|
1283 def single(rev, seqno, fp): |
6747
f6c00b17387c
use repo[changeid] to get a changectx
Matt Mackall <mpm@selenic.com>
parents:
6743
diff
changeset
|
1284 ctx = repo[rev] |
3970
fff8a5345eb0
commands.py: use contexts in export
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3967
diff
changeset
|
1285 node = ctx.node() |
fff8a5345eb0
commands.py: use contexts in export
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3967
diff
changeset
|
1286 parents = [p.node() for p in ctx.parents() if p] |
4436
a764edb6fc95
Add branch information to hg export.
Eric Hopper <hopper@omnifarious.org>
parents:
4435
diff
changeset
|
1287 branch = ctx.branch() |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1288 if switch_parent: |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1289 parents.reverse() |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1290 prev = (parents and parents[0]) or nullid |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1291 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1292 if not fp: |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1293 fp = cmdutil.make_file(repo, template, node, total=total, |
7319
eae1767cc6a8
export: fixed silent output file overwriting
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
7308
diff
changeset
|
1294 seqno=seqno, revwidth=revwidth, |
eae1767cc6a8
export: fixed silent output file overwriting
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
7308
diff
changeset
|
1295 mode='ab') |
4125
ef7c39ae5d4c
Suppress <stdout> before hg export -v (regression from previous patch).
Brendan Cully <brendan@kublai.com>
parents:
4124
diff
changeset
|
1296 if fp != sys.stdout and hasattr(fp, 'name'): |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1297 repo.ui.note("%s\n" % fp.name) |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1298 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1299 fp.write("# HG changeset patch\n") |
3970
fff8a5345eb0
commands.py: use contexts in export
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3967
diff
changeset
|
1300 fp.write("# User %s\n" % ctx.user()) |
fff8a5345eb0
commands.py: use contexts in export
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3967
diff
changeset
|
1301 fp.write("# Date %d %d\n" % ctx.date()) |
4436
a764edb6fc95
Add branch information to hg export.
Eric Hopper <hopper@omnifarious.org>
parents:
4435
diff
changeset
|
1302 if branch and (branch != 'default'): |
a764edb6fc95
Add branch information to hg export.
Eric Hopper <hopper@omnifarious.org>
parents:
4435
diff
changeset
|
1303 fp.write("# Branch %s\n" % branch) |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1304 fp.write("# Node ID %s\n" % hex(node)) |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1305 fp.write("# Parent %s\n" % hex(prev)) |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1306 if len(parents) > 1: |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1307 fp.write("# Parent %s\n" % hex(parents[1])) |
3970
fff8a5345eb0
commands.py: use contexts in export
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3967
diff
changeset
|
1308 fp.write(ctx.description().rstrip()) |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1309 fp.write("\n\n") |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1310 |
7308
b6f5490effbf
patch: turn patch.diff() into a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7267
diff
changeset
|
1311 for chunk in diff(repo, prev, node, opts=opts): |
b6f5490effbf
patch: turn patch.diff() into a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7267
diff
changeset
|
1312 fp.write(chunk) |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1313 if fp not in (sys.stdout, repo.ui): |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1314 fp.close() |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2868
diff
changeset
|
1315 |
3900
2b3175acb653
Don't use node length for calculating revision number length.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3899
diff
changeset
|
1316 for seqno, rev in enumerate(revs): |
3970
fff8a5345eb0
commands.py: use contexts in export
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
3967
diff
changeset
|
1317 single(rev, seqno+1, fp) |
3096
f422c8265ae5
Add support for diffstat in commit emails, and move diffstat from
Matt Doar <matt@xensource.com>
parents:
3066
diff
changeset
|
1318 |
f422c8265ae5
Add support for diffstat in commit emails, and move diffstat from
Matt Doar <matt@xensource.com>
parents:
3066
diff
changeset
|
1319 def diffstat(patchlines): |
4488
62019c4427e3
Introduce find_exe. Use instead of find_in_path for programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4436
diff
changeset
|
1320 if not util.find_exe('diffstat'): |
4316
6e4334be0aaa
Try to find diffstat in PATH before calling it
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4229
diff
changeset
|
1321 return |
7267
6a51ca1e05c3
patch: rewrite diffstat with util.filter()
Patrick Mezard <pmezard@gmail.com>
parents:
7247
diff
changeset
|
1322 output = util.filter('\n'.join(patchlines), |
6a51ca1e05c3
patch: rewrite diffstat with util.filter()
Patrick Mezard <pmezard@gmail.com>
parents:
7247
diff
changeset
|
1323 'diffstat -p1 -w79 2>%s' % util.nulldev) |
6a51ca1e05c3
patch: rewrite diffstat with util.filter()
Patrick Mezard <pmezard@gmail.com>
parents:
7247
diff
changeset
|
1324 stat = [l.lstrip() for l in output.splitlines(True)] |
6a51ca1e05c3
patch: rewrite diffstat with util.filter()
Patrick Mezard <pmezard@gmail.com>
parents:
7247
diff
changeset
|
1325 last = stat.pop() |
6a51ca1e05c3
patch: rewrite diffstat with util.filter()
Patrick Mezard <pmezard@gmail.com>
parents:
7247
diff
changeset
|
1326 stat.insert(0, last) |
6a51ca1e05c3
patch: rewrite diffstat with util.filter()
Patrick Mezard <pmezard@gmail.com>
parents:
7247
diff
changeset
|
1327 stat = ''.join(stat) |
6a51ca1e05c3
patch: rewrite diffstat with util.filter()
Patrick Mezard <pmezard@gmail.com>
parents:
7247
diff
changeset
|
1328 return stat |