Show More
@@ -1,189 +1,195 | |||||
1 | # mdiff.py - diff and patch routines for mercurial |
|
1 | # mdiff.py - diff and patch routines for mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2005 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2005 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms |
|
5 | # This software may be used and distributed according to the terms | |
6 | # of the GNU General Public License, incorporated herein by reference. |
|
6 | # of the GNU General Public License, incorporated herein by reference. | |
7 |
|
7 | |||
8 | from demandload import demandload |
|
8 | from demandload import demandload | |
9 | import struct, bdiff, util, mpatch |
|
9 | import struct, bdiff, util, mpatch | |
10 | demandload(globals(), "re") |
|
10 | demandload(globals(), "re") | |
11 |
|
11 | |||
12 |
|
12 | |||
13 | def unidiff(a, ad, b, bd, fn, r=None, text=False, |
|
13 | def unidiff(a, ad, b, bd, fn, r=None, text=False, | |
14 | showfunc=False, ignorews=False): |
|
14 | showfunc=False, ignorews=False): | |
15 |
|
15 | |||
16 | if not a and not b: return "" |
|
16 | if not a and not b: return "" | |
17 | epoch = util.datestr((0, 0)) |
|
17 | epoch = util.datestr((0, 0)) | |
18 |
|
18 | |||
19 | if not text and (util.binary(a) or util.binary(b)): |
|
19 | if not text and (util.binary(a) or util.binary(b)): | |
20 | l = ['Binary file %s has changed\n' % fn] |
|
20 | l = ['Binary file %s has changed\n' % fn] | |
21 |
elif a |
|
21 | elif not a: | |
22 | b = b.splitlines(1) |
|
22 | b = b.splitlines(1) | |
23 | l1 = "--- %s\t%s\n" % ("/dev/null", epoch) |
|
23 | if a is None: | |
|
24 | l1 = "--- %s\t%s\n" % ("/dev/null", epoch) | |||
|
25 | else: | |||
|
26 | l1 = "--- %s\t%s\n" % ("a/" + fn, ad) | |||
24 | l2 = "+++ %s\t%s\n" % ("b/" + fn, bd) |
|
27 | l2 = "+++ %s\t%s\n" % ("b/" + fn, bd) | |
25 | l3 = "@@ -0,0 +1,%d @@\n" % len(b) |
|
28 | l3 = "@@ -0,0 +1,%d @@\n" % len(b) | |
26 | l = [l1, l2, l3] + ["+" + e for e in b] |
|
29 | l = [l1, l2, l3] + ["+" + e for e in b] | |
27 |
elif b |
|
30 | elif not b: | |
28 | a = a.splitlines(1) |
|
31 | a = a.splitlines(1) | |
29 | l1 = "--- %s\t%s\n" % ("a/" + fn, ad) |
|
32 | l1 = "--- %s\t%s\n" % ("a/" + fn, ad) | |
30 | l2 = "+++ %s\t%s\n" % ("/dev/null", epoch) |
|
33 | if b is None: | |
|
34 | l2 = "+++ %s\t%s\n" % ("/dev/null", epoch) | |||
|
35 | else: | |||
|
36 | l2 = "+++ %s\t%s\n" % ("b/" + fn, bd) | |||
31 | l3 = "@@ -1,%d +0,0 @@\n" % len(a) |
|
37 | l3 = "@@ -1,%d +0,0 @@\n" % len(a) | |
32 | l = [l1, l2, l3] + ["-" + e for e in a] |
|
38 | l = [l1, l2, l3] + ["-" + e for e in a] | |
33 | else: |
|
39 | else: | |
34 | al = a.splitlines(1) |
|
40 | al = a.splitlines(1) | |
35 | bl = b.splitlines(1) |
|
41 | bl = b.splitlines(1) | |
36 | l = list(bunidiff(a, b, al, bl, "a/" + fn, "b/" + fn, |
|
42 | l = list(bunidiff(a, b, al, bl, "a/" + fn, "b/" + fn, | |
37 | showfunc=showfunc, ignorews=ignorews)) |
|
43 | showfunc=showfunc, ignorews=ignorews)) | |
38 | if not l: return "" |
|
44 | if not l: return "" | |
39 | # difflib uses a space, rather than a tab |
|
45 | # difflib uses a space, rather than a tab | |
40 | l[0] = "%s\t%s\n" % (l[0][:-2], ad) |
|
46 | l[0] = "%s\t%s\n" % (l[0][:-2], ad) | |
41 | l[1] = "%s\t%s\n" % (l[1][:-2], bd) |
|
47 | l[1] = "%s\t%s\n" % (l[1][:-2], bd) | |
42 |
|
48 | |||
43 | for ln in xrange(len(l)): |
|
49 | for ln in xrange(len(l)): | |
44 | if l[ln][-1] != '\n': |
|
50 | if l[ln][-1] != '\n': | |
45 | l[ln] += "\n\ No newline at end of file\n" |
|
51 | l[ln] += "\n\ No newline at end of file\n" | |
46 |
|
52 | |||
47 | if r: |
|
53 | if r: | |
48 | l.insert(0, "diff %s %s\n" % |
|
54 | l.insert(0, "diff %s %s\n" % | |
49 | (' '.join(["-r %s" % rev for rev in r]), fn)) |
|
55 | (' '.join(["-r %s" % rev for rev in r]), fn)) | |
50 |
|
56 | |||
51 | return "".join(l) |
|
57 | return "".join(l) | |
52 |
|
58 | |||
53 | # somewhat self contained replacement for difflib.unified_diff |
|
59 | # somewhat self contained replacement for difflib.unified_diff | |
54 | # t1 and t2 are the text to be diffed |
|
60 | # t1 and t2 are the text to be diffed | |
55 | # l1 and l2 are the text broken up into lines |
|
61 | # l1 and l2 are the text broken up into lines | |
56 | # header1 and header2 are the filenames for the diff output |
|
62 | # header1 and header2 are the filenames for the diff output | |
57 | # context is the number of context lines |
|
63 | # context is the number of context lines | |
58 | # showfunc enables diff -p output |
|
64 | # showfunc enables diff -p output | |
59 | # ignorews ignores all whitespace changes in the diff |
|
65 | # ignorews ignores all whitespace changes in the diff | |
60 | def bunidiff(t1, t2, l1, l2, header1, header2, context=3, showfunc=False, |
|
66 | def bunidiff(t1, t2, l1, l2, header1, header2, context=3, showfunc=False, | |
61 | ignorews=False): |
|
67 | ignorews=False): | |
62 | def contextend(l, len): |
|
68 | def contextend(l, len): | |
63 | ret = l + context |
|
69 | ret = l + context | |
64 | if ret > len: |
|
70 | if ret > len: | |
65 | ret = len |
|
71 | ret = len | |
66 | return ret |
|
72 | return ret | |
67 |
|
73 | |||
68 | def contextstart(l): |
|
74 | def contextstart(l): | |
69 | ret = l - context |
|
75 | ret = l - context | |
70 | if ret < 0: |
|
76 | if ret < 0: | |
71 | return 0 |
|
77 | return 0 | |
72 | return ret |
|
78 | return ret | |
73 |
|
79 | |||
74 | def yieldhunk(hunk, header): |
|
80 | def yieldhunk(hunk, header): | |
75 | if header: |
|
81 | if header: | |
76 | for x in header: |
|
82 | for x in header: | |
77 | yield x |
|
83 | yield x | |
78 | (astart, a2, bstart, b2, delta) = hunk |
|
84 | (astart, a2, bstart, b2, delta) = hunk | |
79 | aend = contextend(a2, len(l1)) |
|
85 | aend = contextend(a2, len(l1)) | |
80 | alen = aend - astart |
|
86 | alen = aend - astart | |
81 | blen = b2 - bstart + aend - a2 |
|
87 | blen = b2 - bstart + aend - a2 | |
82 |
|
88 | |||
83 | func = "" |
|
89 | func = "" | |
84 | if showfunc: |
|
90 | if showfunc: | |
85 | # walk backwards from the start of the context |
|
91 | # walk backwards from the start of the context | |
86 | # to find a line starting with an alphanumeric char. |
|
92 | # to find a line starting with an alphanumeric char. | |
87 | for x in xrange(astart, -1, -1): |
|
93 | for x in xrange(astart, -1, -1): | |
88 | t = l1[x].rstrip() |
|
94 | t = l1[x].rstrip() | |
89 | if funcre.match(t): |
|
95 | if funcre.match(t): | |
90 | func = ' ' + t[:40] |
|
96 | func = ' ' + t[:40] | |
91 | break |
|
97 | break | |
92 |
|
98 | |||
93 | yield "@@ -%d,%d +%d,%d @@%s\n" % (astart + 1, alen, |
|
99 | yield "@@ -%d,%d +%d,%d @@%s\n" % (astart + 1, alen, | |
94 | bstart + 1, blen, func) |
|
100 | bstart + 1, blen, func) | |
95 | for x in delta: |
|
101 | for x in delta: | |
96 | yield x |
|
102 | yield x | |
97 | for x in xrange(a2, aend): |
|
103 | for x in xrange(a2, aend): | |
98 | yield ' ' + l1[x] |
|
104 | yield ' ' + l1[x] | |
99 |
|
105 | |||
100 | header = [ "--- %s\t\n" % header1, "+++ %s\t\n" % header2 ] |
|
106 | header = [ "--- %s\t\n" % header1, "+++ %s\t\n" % header2 ] | |
101 |
|
107 | |||
102 | if showfunc: |
|
108 | if showfunc: | |
103 | funcre = re.compile('\w') |
|
109 | funcre = re.compile('\w') | |
104 | if ignorews: |
|
110 | if ignorews: | |
105 | wsre = re.compile('[ \t]') |
|
111 | wsre = re.compile('[ \t]') | |
106 |
|
112 | |||
107 | # bdiff.blocks gives us the matching sequences in the files. The loop |
|
113 | # bdiff.blocks gives us the matching sequences in the files. The loop | |
108 | # below finds the spaces between those matching sequences and translates |
|
114 | # below finds the spaces between those matching sequences and translates | |
109 | # them into diff output. |
|
115 | # them into diff output. | |
110 | # |
|
116 | # | |
111 | diff = bdiff.blocks(t1, t2) |
|
117 | diff = bdiff.blocks(t1, t2) | |
112 | hunk = None |
|
118 | hunk = None | |
113 | for i in xrange(len(diff)): |
|
119 | for i in xrange(len(diff)): | |
114 | # The first match is special. |
|
120 | # The first match is special. | |
115 | # we've either found a match starting at line 0 or a match later |
|
121 | # we've either found a match starting at line 0 or a match later | |
116 | # in the file. If it starts later, old and new below will both be |
|
122 | # in the file. If it starts later, old and new below will both be | |
117 | # empty and we'll continue to the next match. |
|
123 | # empty and we'll continue to the next match. | |
118 | if i > 0: |
|
124 | if i > 0: | |
119 | s = diff[i-1] |
|
125 | s = diff[i-1] | |
120 | else: |
|
126 | else: | |
121 | s = [0, 0, 0, 0] |
|
127 | s = [0, 0, 0, 0] | |
122 | delta = [] |
|
128 | delta = [] | |
123 | s1 = diff[i] |
|
129 | s1 = diff[i] | |
124 | a1 = s[1] |
|
130 | a1 = s[1] | |
125 | a2 = s1[0] |
|
131 | a2 = s1[0] | |
126 | b1 = s[3] |
|
132 | b1 = s[3] | |
127 | b2 = s1[2] |
|
133 | b2 = s1[2] | |
128 |
|
134 | |||
129 | old = l1[a1:a2] |
|
135 | old = l1[a1:a2] | |
130 | new = l2[b1:b2] |
|
136 | new = l2[b1:b2] | |
131 |
|
137 | |||
132 | # bdiff sometimes gives huge matches past eof, this check eats them, |
|
138 | # bdiff sometimes gives huge matches past eof, this check eats them, | |
133 | # and deals with the special first match case described above |
|
139 | # and deals with the special first match case described above | |
134 | if not old and not new: |
|
140 | if not old and not new: | |
135 | continue |
|
141 | continue | |
136 |
|
142 | |||
137 | if ignorews: |
|
143 | if ignorews: | |
138 | wsold = wsre.sub('', "".join(old)) |
|
144 | wsold = wsre.sub('', "".join(old)) | |
139 | wsnew = wsre.sub('', "".join(new)) |
|
145 | wsnew = wsre.sub('', "".join(new)) | |
140 | if wsold == wsnew: |
|
146 | if wsold == wsnew: | |
141 | continue |
|
147 | continue | |
142 |
|
148 | |||
143 | astart = contextstart(a1) |
|
149 | astart = contextstart(a1) | |
144 | bstart = contextstart(b1) |
|
150 | bstart = contextstart(b1) | |
145 | prev = None |
|
151 | prev = None | |
146 | if hunk: |
|
152 | if hunk: | |
147 | # join with the previous hunk if it falls inside the context |
|
153 | # join with the previous hunk if it falls inside the context | |
148 | if astart < hunk[1] + context + 1: |
|
154 | if astart < hunk[1] + context + 1: | |
149 | prev = hunk |
|
155 | prev = hunk | |
150 | astart = hunk[1] |
|
156 | astart = hunk[1] | |
151 | bstart = hunk[3] |
|
157 | bstart = hunk[3] | |
152 | else: |
|
158 | else: | |
153 | for x in yieldhunk(hunk, header): |
|
159 | for x in yieldhunk(hunk, header): | |
154 | yield x |
|
160 | yield x | |
155 | # we only want to yield the header if the files differ, and |
|
161 | # we only want to yield the header if the files differ, and | |
156 | # we only want to yield it once. |
|
162 | # we only want to yield it once. | |
157 | header = None |
|
163 | header = None | |
158 | if prev: |
|
164 | if prev: | |
159 | # we've joined the previous hunk, record the new ending points. |
|
165 | # we've joined the previous hunk, record the new ending points. | |
160 | hunk[1] = a2 |
|
166 | hunk[1] = a2 | |
161 | hunk[3] = b2 |
|
167 | hunk[3] = b2 | |
162 | delta = hunk[4] |
|
168 | delta = hunk[4] | |
163 | else: |
|
169 | else: | |
164 | # create a new hunk |
|
170 | # create a new hunk | |
165 | hunk = [ astart, a2, bstart, b2, delta ] |
|
171 | hunk = [ astart, a2, bstart, b2, delta ] | |
166 |
|
172 | |||
167 | delta[len(delta):] = [ ' ' + x for x in l1[astart:a1] ] |
|
173 | delta[len(delta):] = [ ' ' + x for x in l1[astart:a1] ] | |
168 | delta[len(delta):] = [ '-' + x for x in old ] |
|
174 | delta[len(delta):] = [ '-' + x for x in old ] | |
169 | delta[len(delta):] = [ '+' + x for x in new ] |
|
175 | delta[len(delta):] = [ '+' + x for x in new ] | |
170 |
|
176 | |||
171 | if hunk: |
|
177 | if hunk: | |
172 | for x in yieldhunk(hunk, header): |
|
178 | for x in yieldhunk(hunk, header): | |
173 | yield x |
|
179 | yield x | |
174 |
|
180 | |||
175 | def patchtext(bin): |
|
181 | def patchtext(bin): | |
176 | pos = 0 |
|
182 | pos = 0 | |
177 | t = [] |
|
183 | t = [] | |
178 | while pos < len(bin): |
|
184 | while pos < len(bin): | |
179 | p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) |
|
185 | p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) | |
180 | pos += 12 |
|
186 | pos += 12 | |
181 | t.append(bin[pos:pos + l]) |
|
187 | t.append(bin[pos:pos + l]) | |
182 | pos += l |
|
188 | pos += l | |
183 | return "".join(t) |
|
189 | return "".join(t) | |
184 |
|
190 | |||
185 | def patch(a, bin): |
|
191 | def patch(a, bin): | |
186 | return mpatch.patches(a, [bin]) |
|
192 | return mpatch.patches(a, [bin]) | |
187 |
|
193 | |||
188 | patches = mpatch.patches |
|
194 | patches = mpatch.patches | |
189 | textdiff = bdiff.bdiff |
|
195 | textdiff = bdiff.bdiff |
@@ -1,14 +1,18 | |||||
1 | #!/bin/sh |
|
1 | #!/bin/sh | |
2 |
|
2 | |||
3 | hg init |
|
3 | hg init | |
4 | touch a |
|
4 | touch a | |
5 | hg add a |
|
5 | hg add a | |
6 | hg ci -m "a" -d "0 0" |
|
6 | hg ci -m "a" -d "0 0" | |
7 |
|
7 | |||
8 | echo 123 > b |
|
8 | echo 123 > b | |
9 | hg add b |
|
9 | hg add b | |
10 | hg diff | sed -e "s/\(+++ [a-zA-Z0-9_/.-]*\).*/\1/" \ |
|
10 | hg diff | sed -e "s/\(+++ [a-zA-Z0-9_/.-]*\).*/\1/" \ | |
11 | -e "s/\(--- [a-zA-Z0-9_/.-]*\).*/\1/" |
|
11 | -e "s/\(--- [a-zA-Z0-9_/.-]*\).*/\1/" | |
12 |
|
12 | |||
13 | hg diff -r tip | sed -e "s/\(+++ [a-zA-Z0-9_/.-]*\).*/\1/" \ |
|
13 | hg diff -r tip | sed -e "s/\(+++ [a-zA-Z0-9_/.-]*\).*/\1/" \ | |
14 | -e "s/\(--- [a-zA-Z0-9_/.-]*\).*/\1/" |
|
14 | -e "s/\(--- [a-zA-Z0-9_/.-]*\).*/\1/" | |
|
15 | ||||
|
16 | echo foo > a | |||
|
17 | hg diff | sed -e "s/\(+++ [a-zA-Z0-9_/.-]*\).*/\1/" \ | |||
|
18 | -e "s/\(--- [a-zA-Z0-9_/.-]*\).*/\1/" |
@@ -1,10 +1,20 | |||||
1 | diff -r 3903775176ed b |
|
1 | diff -r 3903775176ed b | |
2 | --- /dev/null |
|
2 | --- /dev/null | |
3 | +++ b/b |
|
3 | +++ b/b | |
4 | @@ -0,0 +1,1 @@ |
|
4 | @@ -0,0 +1,1 @@ | |
5 | +123 |
|
5 | +123 | |
6 | diff -r 3903775176ed b |
|
6 | diff -r 3903775176ed b | |
7 | --- /dev/null |
|
7 | --- /dev/null | |
8 | +++ b/b |
|
8 | +++ b/b | |
9 | @@ -0,0 +1,1 @@ |
|
9 | @@ -0,0 +1,1 @@ | |
10 | +123 |
|
10 | +123 | |
|
11 | diff -r 3903775176ed a | |||
|
12 | --- a/a | |||
|
13 | +++ b/a | |||
|
14 | @@ -0,0 +1,1 @@ | |||
|
15 | +foo | |||
|
16 | diff -r 3903775176ed b | |||
|
17 | --- /dev/null | |||
|
18 | +++ b/b | |||
|
19 | @@ -0,0 +1,1 @@ | |||
|
20 | +123 |
General Comments 0
You need to be logged in to leave comments.
Login now