Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/mdiff.py @ 2248:b914f0557832
fix diffs containing embedded "\r".
add test to make sure fix stays fixed.
author | Vadim Gelfer <vadim.gelfer@gmail.com> |
---|---|
date | Wed, 10 May 2006 10:31:54 -0700 |
parents | 441ea218414e |
children | 35fb62a3a673 |
rev | line source |
---|---|
239
75840796e8e2
mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents:
184
diff
changeset
|
1 # mdiff.py - diff and patch routines for mercurial |
75840796e8e2
mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents:
184
diff
changeset
|
2 # |
75840796e8e2
mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents:
184
diff
changeset
|
3 # Copyright 2005 Matt Mackall <mpm@selenic.com> |
75840796e8e2
mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents:
184
diff
changeset
|
4 # |
75840796e8e2
mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents:
184
diff
changeset
|
5 # This software may be used and distributed according to the terms |
75840796e8e2
mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents:
184
diff
changeset
|
6 # of the GNU General Public License, incorporated herein by reference. |
75840796e8e2
mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents:
184
diff
changeset
|
7 |
1637 | 8 from demandload import demandload |
9 import struct, bdiff, util, mpatch | |
10 demandload(globals(), "re") | |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
11 |
1637 | 12 |
2248
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
13 def splitnewlines(text, keepends=False): |
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
14 '''like str.splitlines, but only split on newlines.''' |
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
15 i = 0 |
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
16 lines = [] |
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
17 while True: |
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
18 n = text.find('\n', i) |
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
19 if n == -1: |
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
20 last = text[i:] |
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
21 if last: |
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
22 lines.append(last) |
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
23 return lines |
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
24 lines.append(text[i:keepends and n+1 or n]) |
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
25 i = n + 1 |
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
26 |
1637 | 27 def unidiff(a, ad, b, bd, fn, r=None, text=False, |
28 showfunc=False, ignorews=False): | |
396
8f8bb77d560e
Show revisions in diffs like CVS, based on a patch from Goffredo Baroncelli.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
361
diff
changeset
|
29 |
35 | 30 if not a and not b: return "" |
1379 | 31 epoch = util.datestr((0, 0)) |
264
4c1d7072d5cd
Attempt to make diff deal with null sources properly
mpm@selenic.com
parents:
249
diff
changeset
|
32 |
1379 | 33 if not text and (util.binary(a) or util.binary(b)): |
1015
22571b8d35d3
Add automatic binary file detection to diff and export
mpm@selenic.com
parents:
582
diff
changeset
|
34 l = ['Binary file %s has changed\n' % fn] |
1723
fde8fb2cbede
Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1637
diff
changeset
|
35 elif not a: |
2248
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
36 b = splitnewlines(b, keepends=True) |
1723
fde8fb2cbede
Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1637
diff
changeset
|
37 if a is None: |
fde8fb2cbede
Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1637
diff
changeset
|
38 l1 = "--- %s\t%s\n" % ("/dev/null", epoch) |
fde8fb2cbede
Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1637
diff
changeset
|
39 else: |
fde8fb2cbede
Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1637
diff
changeset
|
40 l1 = "--- %s\t%s\n" % ("a/" + fn, ad) |
264
4c1d7072d5cd
Attempt to make diff deal with null sources properly
mpm@selenic.com
parents:
249
diff
changeset
|
41 l2 = "+++ %s\t%s\n" % ("b/" + fn, bd) |
4c1d7072d5cd
Attempt to make diff deal with null sources properly
mpm@selenic.com
parents:
249
diff
changeset
|
42 l3 = "@@ -0,0 +1,%d @@\n" % len(b) |
4c1d7072d5cd
Attempt to make diff deal with null sources properly
mpm@selenic.com
parents:
249
diff
changeset
|
43 l = [l1, l2, l3] + ["+" + e for e in b] |
1723
fde8fb2cbede
Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1637
diff
changeset
|
44 elif not b: |
2248
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
45 a = splitnewlines(a, keepends=True) |
264
4c1d7072d5cd
Attempt to make diff deal with null sources properly
mpm@selenic.com
parents:
249
diff
changeset
|
46 l1 = "--- %s\t%s\n" % ("a/" + fn, ad) |
1723
fde8fb2cbede
Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1637
diff
changeset
|
47 if b is None: |
fde8fb2cbede
Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1637
diff
changeset
|
48 l2 = "+++ %s\t%s\n" % ("/dev/null", epoch) |
fde8fb2cbede
Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1637
diff
changeset
|
49 else: |
fde8fb2cbede
Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1637
diff
changeset
|
50 l2 = "+++ %s\t%s\n" % ("b/" + fn, bd) |
264
4c1d7072d5cd
Attempt to make diff deal with null sources properly
mpm@selenic.com
parents:
249
diff
changeset
|
51 l3 = "@@ -1,%d +0,0 @@\n" % len(a) |
4c1d7072d5cd
Attempt to make diff deal with null sources properly
mpm@selenic.com
parents:
249
diff
changeset
|
52 l = [l1, l2, l3] + ["-" + e for e in a] |
4c1d7072d5cd
Attempt to make diff deal with null sources properly
mpm@selenic.com
parents:
249
diff
changeset
|
53 else: |
2248
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
54 al = splitnewlines(a, keepends=True) |
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
55 bl = splitnewlines(b, keepends=True) |
1637 | 56 l = list(bunidiff(a, b, al, bl, "a/" + fn, "b/" + fn, |
57 showfunc=showfunc, ignorews=ignorews)) | |
278
777e388c06d6
unidiff: handle empty diffs more gracefully
mpm@selenic.com
parents:
272
diff
changeset
|
58 if not l: return "" |
272
467cea2bf2d8
diff: use tab to separate date from filename
mpm@selenic.com
parents:
264
diff
changeset
|
59 # difflib uses a space, rather than a tab |
1540
8ca9f5b17257
minor optimization: save some string trash
twaldmann@thinkmo.de
parents:
1452
diff
changeset
|
60 l[0] = "%s\t%s\n" % (l[0][:-2], ad) |
8ca9f5b17257
minor optimization: save some string trash
twaldmann@thinkmo.de
parents:
1452
diff
changeset
|
61 l[1] = "%s\t%s\n" % (l[1][:-2], bd) |
170 | 62 |
63 for ln in xrange(len(l)): | |
64 if l[ln][-1] != '\n': | |
65 l[ln] += "\n\ No newline at end of file\n" | |
66 | |
396
8f8bb77d560e
Show revisions in diffs like CVS, based on a patch from Goffredo Baroncelli.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
361
diff
changeset
|
67 if r: |
8f8bb77d560e
Show revisions in diffs like CVS, based on a patch from Goffredo Baroncelli.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
361
diff
changeset
|
68 l.insert(0, "diff %s %s\n" % |
8f8bb77d560e
Show revisions in diffs like CVS, based on a patch from Goffredo Baroncelli.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
361
diff
changeset
|
69 (' '.join(["-r %s" % rev for rev in r]), fn)) |
8f8bb77d560e
Show revisions in diffs like CVS, based on a patch from Goffredo Baroncelli.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
361
diff
changeset
|
70 |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
71 return "".join(l) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
72 |
1637 | 73 # somewhat self contained replacement for difflib.unified_diff |
74 # t1 and t2 are the text to be diffed | |
75 # l1 and l2 are the text broken up into lines | |
76 # header1 and header2 are the filenames for the diff output | |
77 # context is the number of context lines | |
78 # showfunc enables diff -p output | |
79 # ignorews ignores all whitespace changes in the diff | |
80 def bunidiff(t1, t2, l1, l2, header1, header2, context=3, showfunc=False, | |
81 ignorews=False): | |
82 def contextend(l, len): | |
83 ret = l + context | |
84 if ret > len: | |
85 ret = len | |
86 return ret | |
87 | |
88 def contextstart(l): | |
89 ret = l - context | |
90 if ret < 0: | |
91 return 0 | |
92 return ret | |
93 | |
94 def yieldhunk(hunk, header): | |
95 if header: | |
96 for x in header: | |
97 yield x | |
98 (astart, a2, bstart, b2, delta) = hunk | |
99 aend = contextend(a2, len(l1)) | |
100 alen = aend - astart | |
101 blen = b2 - bstart + aend - a2 | |
102 | |
103 func = "" | |
104 if showfunc: | |
105 # walk backwards from the start of the context | |
106 # to find a line starting with an alphanumeric char. | |
107 for x in xrange(astart, -1, -1): | |
108 t = l1[x].rstrip() | |
109 if funcre.match(t): | |
110 func = ' ' + t[:40] | |
111 break | |
112 | |
113 yield "@@ -%d,%d +%d,%d @@%s\n" % (astart + 1, alen, | |
114 bstart + 1, blen, func) | |
115 for x in delta: | |
116 yield x | |
117 for x in xrange(a2, aend): | |
118 yield ' ' + l1[x] | |
119 | |
120 header = [ "--- %s\t\n" % header1, "+++ %s\t\n" % header2 ] | |
121 | |
122 if showfunc: | |
123 funcre = re.compile('\w') | |
124 if ignorews: | |
125 wsre = re.compile('[ \t]') | |
126 | |
127 # bdiff.blocks gives us the matching sequences in the files. The loop | |
128 # below finds the spaces between those matching sequences and translates | |
129 # them into diff output. | |
130 # | |
131 diff = bdiff.blocks(t1, t2) | |
132 hunk = None | |
133 for i in xrange(len(diff)): | |
134 # The first match is special. | |
135 # we've either found a match starting at line 0 or a match later | |
136 # in the file. If it starts later, old and new below will both be | |
137 # empty and we'll continue to the next match. | |
138 if i > 0: | |
139 s = diff[i-1] | |
140 else: | |
141 s = [0, 0, 0, 0] | |
142 delta = [] | |
143 s1 = diff[i] | |
144 a1 = s[1] | |
145 a2 = s1[0] | |
146 b1 = s[3] | |
147 b2 = s1[2] | |
148 | |
149 old = l1[a1:a2] | |
150 new = l2[b1:b2] | |
151 | |
152 # bdiff sometimes gives huge matches past eof, this check eats them, | |
153 # and deals with the special first match case described above | |
154 if not old and not new: | |
155 continue | |
156 | |
157 if ignorews: | |
158 wsold = wsre.sub('', "".join(old)) | |
159 wsnew = wsre.sub('', "".join(new)) | |
160 if wsold == wsnew: | |
161 continue | |
162 | |
163 astart = contextstart(a1) | |
164 bstart = contextstart(b1) | |
165 prev = None | |
166 if hunk: | |
167 # join with the previous hunk if it falls inside the context | |
168 if astart < hunk[1] + context + 1: | |
169 prev = hunk | |
170 astart = hunk[1] | |
171 bstart = hunk[3] | |
172 else: | |
173 for x in yieldhunk(hunk, header): | |
174 yield x | |
175 # we only want to yield the header if the files differ, and | |
176 # we only want to yield it once. | |
177 header = None | |
178 if prev: | |
179 # we've joined the previous hunk, record the new ending points. | |
180 hunk[1] = a2 | |
181 hunk[3] = b2 | |
182 delta = hunk[4] | |
183 else: | |
184 # create a new hunk | |
185 hunk = [ astart, a2, bstart, b2, delta ] | |
186 | |
187 delta[len(delta):] = [ ' ' + x for x in l1[astart:a1] ] | |
188 delta[len(delta):] = [ '-' + x for x in old ] | |
189 delta[len(delta):] = [ '+' + x for x in new ] | |
190 | |
191 if hunk: | |
192 for x in yieldhunk(hunk, header): | |
193 yield x | |
194 | |
120
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
195 def patchtext(bin): |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
196 pos = 0 |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
197 t = [] |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
198 while pos < len(bin): |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
199 p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
200 pos += 12 |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
201 t.append(bin[pos:pos + l]) |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
202 pos += l |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
203 return "".join(t) |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
204 |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
205 def patch(a, bin): |
1379 | 206 return mpatch.patches(a, [bin]) |
432 | 207 |
1379 | 208 patches = mpatch.patches |
2078
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1723
diff
changeset
|
209 patchedsize = mpatch.patchedsize |
432 | 210 textdiff = bdiff.bdiff |