Mercurial > hg
annotate mercurial/mdiff.py @ 4718:934275cd4526
backout: report short hash in commit message
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Mon, 25 Jun 2007 12:44:48 -0500 |
parents | 826659bd8053 |
children | 372d93f03d3a |
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 # |
2859 | 3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
239
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 |
4108 | 8 import bdiff, mpatch, re, struct, util, md5 |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
9 |
2251
35fb62a3a673
fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2248
diff
changeset
|
10 def splitnewlines(text): |
2248
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
11 '''like str.splitlines, but only split on newlines.''' |
2251
35fb62a3a673
fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2248
diff
changeset
|
12 lines = [l + '\n' for l in text.split('\n')] |
35fb62a3a673
fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2248
diff
changeset
|
13 if lines: |
35fb62a3a673
fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2248
diff
changeset
|
14 if lines[-1] == '\n': |
35fb62a3a673
fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2248
diff
changeset
|
15 lines.pop() |
35fb62a3a673
fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2248
diff
changeset
|
16 else: |
35fb62a3a673
fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2248
diff
changeset
|
17 lines[-1] = lines[-1][:-1] |
35fb62a3a673
fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2248
diff
changeset
|
18 return lines |
2248
b914f0557832
fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2078
diff
changeset
|
19 |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
20 class diffopts(object): |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
21 '''context is the number of context lines |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
22 text treats all files as text |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
23 showfunc enables diff -p output |
2907 | 24 git enables the git extended patch format |
3199
096f1c73cdc3
Add -D/--nodates options to hg diff/export that removes dates from diff headers
Stephen Darnell <stephen@darnell.plus.com>
parents:
3026
diff
changeset
|
25 nodates removes dates from diff headers |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
26 ignorews ignores all whitespace changes in the diff |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
27 ignorewsamount ignores changes in the amount of whitespace |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
28 ignoreblanklines ignores changes whose lines are all blank''' |
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 |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
30 defaults = { |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
31 'context': 3, |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
32 'text': False, |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
33 'showfunc': True, |
2907 | 34 'git': False, |
3199
096f1c73cdc3
Add -D/--nodates options to hg diff/export that removes dates from diff headers
Stephen Darnell <stephen@darnell.plus.com>
parents:
3026
diff
changeset
|
35 'nodates': False, |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
36 'ignorews': False, |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
37 'ignorewsamount': False, |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
38 'ignoreblanklines': False, |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
39 } |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
40 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
41 __slots__ = defaults.keys() |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
42 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
43 def __init__(self, **opts): |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
44 for k in self.__slots__: |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
45 v = opts.get(k) |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
46 if v is None: |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
47 v = self.defaults[k] |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
48 setattr(self, k, v) |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
49 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
50 defaultopts = diffopts() |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
51 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
52 def unidiff(a, ad, b, bd, fn, r=None, opts=defaultopts): |
4679
826659bd8053
git patches: correct handling of filenames with spaces
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4361
diff
changeset
|
53 def datetag(date, addtab=True): |
826659bd8053
git patches: correct handling of filenames with spaces
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4361
diff
changeset
|
54 if not opts.git and not opts.nodates: |
826659bd8053
git patches: correct handling of filenames with spaces
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4361
diff
changeset
|
55 return '\t%s\n' % date |
826659bd8053
git patches: correct handling of filenames with spaces
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4361
diff
changeset
|
56 if addtab and ' ' in fn: |
826659bd8053
git patches: correct handling of filenames with spaces
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4361
diff
changeset
|
57 return '\t\n' |
826659bd8053
git patches: correct handling of filenames with spaces
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4361
diff
changeset
|
58 return '\n' |
3026
d838bfac668d
Remove dates from git export file lines - they confuse git-apply
Brendan Cully <brendan@kublai.com>
parents:
2907
diff
changeset
|
59 |
35 | 60 if not a and not b: return "" |
1379 | 61 epoch = util.datestr((0, 0)) |
264
4c1d7072d5cd
Attempt to make diff deal with null sources properly
mpm@selenic.com
parents:
249
diff
changeset
|
62 |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
63 if not opts.text and (util.binary(a) or util.binary(b)): |
4103
544838cc1158
Don't lie that "binary file has changed"
tailgunner@smtp.ru
parents:
3199
diff
changeset
|
64 def h(v): |
544838cc1158
Don't lie that "binary file has changed"
tailgunner@smtp.ru
parents:
3199
diff
changeset
|
65 # md5 is used instead of sha1 because md5 is supposedly faster |
544838cc1158
Don't lie that "binary file has changed"
tailgunner@smtp.ru
parents:
3199
diff
changeset
|
66 return md5.new(v).digest() |
544838cc1158
Don't lie that "binary file has changed"
tailgunner@smtp.ru
parents:
3199
diff
changeset
|
67 if a and b and len(a) == len(b) and h(a) == h(b): |
544838cc1158
Don't lie that "binary file has changed"
tailgunner@smtp.ru
parents:
3199
diff
changeset
|
68 return "" |
1015
22571b8d35d3
Add automatic binary file detection to diff and export
mpm@selenic.com
parents:
582
diff
changeset
|
69 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
|
70 elif not a: |
2251
35fb62a3a673
fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2248
diff
changeset
|
71 b = splitnewlines(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
|
72 if a is None: |
4679
826659bd8053
git patches: correct handling of filenames with spaces
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4361
diff
changeset
|
73 l1 = '--- /dev/null%s' % datetag(epoch, False) |
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
|
74 else: |
3026
d838bfac668d
Remove dates from git export file lines - they confuse git-apply
Brendan Cully <brendan@kublai.com>
parents:
2907
diff
changeset
|
75 l1 = "--- %s%s" % ("a/" + fn, datetag(ad)) |
d838bfac668d
Remove dates from git export file lines - they confuse git-apply
Brendan Cully <brendan@kublai.com>
parents:
2907
diff
changeset
|
76 l2 = "+++ %s%s" % ("b/" + fn, datetag(bd)) |
264
4c1d7072d5cd
Attempt to make diff deal with null sources properly
mpm@selenic.com
parents:
249
diff
changeset
|
77 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
|
78 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
|
79 elif not b: |
2251
35fb62a3a673
fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2248
diff
changeset
|
80 a = splitnewlines(a) |
3026
d838bfac668d
Remove dates from git export file lines - they confuse git-apply
Brendan Cully <brendan@kublai.com>
parents:
2907
diff
changeset
|
81 l1 = "--- %s%s" % ("a/" + fn, datetag(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
|
82 if b is None: |
4679
826659bd8053
git patches: correct handling of filenames with spaces
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4361
diff
changeset
|
83 l2 = '+++ /dev/null%s' % datetag(epoch, False) |
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
|
84 else: |
3026
d838bfac668d
Remove dates from git export file lines - they confuse git-apply
Brendan Cully <brendan@kublai.com>
parents:
2907
diff
changeset
|
85 l2 = "+++ %s%s" % ("b/" + fn, datetag(bd)) |
264
4c1d7072d5cd
Attempt to make diff deal with null sources properly
mpm@selenic.com
parents:
249
diff
changeset
|
86 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
|
87 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
|
88 else: |
2251
35fb62a3a673
fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2248
diff
changeset
|
89 al = splitnewlines(a) |
35fb62a3a673
fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2248
diff
changeset
|
90 bl = splitnewlines(b) |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
91 l = list(bunidiff(a, b, al, bl, "a/" + fn, "b/" + fn, opts=opts)) |
278
777e388c06d6
unidiff: handle empty diffs more gracefully
mpm@selenic.com
parents:
272
diff
changeset
|
92 if not l: return "" |
272
467cea2bf2d8
diff: use tab to separate date from filename
mpm@selenic.com
parents:
264
diff
changeset
|
93 # difflib uses a space, rather than a tab |
3026
d838bfac668d
Remove dates from git export file lines - they confuse git-apply
Brendan Cully <brendan@kublai.com>
parents:
2907
diff
changeset
|
94 l[0] = "%s%s" % (l[0][:-2], datetag(ad)) |
d838bfac668d
Remove dates from git export file lines - they confuse git-apply
Brendan Cully <brendan@kublai.com>
parents:
2907
diff
changeset
|
95 l[1] = "%s%s" % (l[1][:-2], datetag(bd)) |
170 | 96 |
97 for ln in xrange(len(l)): | |
98 if l[ln][-1] != '\n': | |
99 l[ln] += "\n\ No newline at end of file\n" | |
100 | |
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
|
101 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
|
102 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
|
103 (' '.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
|
104 |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
105 return "".join(l) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
106 |
1637 | 107 # somewhat self contained replacement for difflib.unified_diff |
108 # t1 and t2 are the text to be diffed | |
109 # l1 and l2 are the text broken up into lines | |
110 # header1 and header2 are the filenames for the diff output | |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
111 def bunidiff(t1, t2, l1, l2, header1, header2, opts=defaultopts): |
1637 | 112 def contextend(l, len): |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
113 ret = l + opts.context |
1637 | 114 if ret > len: |
115 ret = len | |
116 return ret | |
117 | |
118 def contextstart(l): | |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
119 ret = l - opts.context |
1637 | 120 if ret < 0: |
121 return 0 | |
122 return ret | |
123 | |
124 def yieldhunk(hunk, header): | |
125 if header: | |
126 for x in header: | |
127 yield x | |
128 (astart, a2, bstart, b2, delta) = hunk | |
129 aend = contextend(a2, len(l1)) | |
130 alen = aend - astart | |
131 blen = b2 - bstart + aend - a2 | |
132 | |
133 func = "" | |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
134 if opts.showfunc: |
1637 | 135 # walk backwards from the start of the context |
136 # to find a line starting with an alphanumeric char. | |
137 for x in xrange(astart, -1, -1): | |
138 t = l1[x].rstrip() | |
139 if funcre.match(t): | |
140 func = ' ' + t[:40] | |
141 break | |
142 | |
143 yield "@@ -%d,%d +%d,%d @@%s\n" % (astart + 1, alen, | |
144 bstart + 1, blen, func) | |
145 for x in delta: | |
146 yield x | |
147 for x in xrange(a2, aend): | |
148 yield ' ' + l1[x] | |
149 | |
150 header = [ "--- %s\t\n" % header1, "+++ %s\t\n" % header2 ] | |
151 | |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
152 if opts.showfunc: |
1637 | 153 funcre = re.compile('\w') |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
154 if opts.ignorewsamount: |
2580
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
155 wsamountre = re.compile('[ \t]+') |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
156 wsappendedre = re.compile(' \n') |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
157 if opts.ignoreblanklines: |
2580
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
158 wsblanklinesre = re.compile('\n') |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
159 if opts.ignorews: |
1637 | 160 wsre = re.compile('[ \t]') |
161 | |
162 # bdiff.blocks gives us the matching sequences in the files. The loop | |
163 # below finds the spaces between those matching sequences and translates | |
164 # them into diff output. | |
165 # | |
166 diff = bdiff.blocks(t1, t2) | |
167 hunk = None | |
168 for i in xrange(len(diff)): | |
169 # The first match is special. | |
170 # we've either found a match starting at line 0 or a match later | |
171 # in the file. If it starts later, old and new below will both be | |
172 # empty and we'll continue to the next match. | |
173 if i > 0: | |
174 s = diff[i-1] | |
175 else: | |
176 s = [0, 0, 0, 0] | |
177 delta = [] | |
178 s1 = diff[i] | |
179 a1 = s[1] | |
180 a2 = s1[0] | |
181 b1 = s[3] | |
182 b2 = s1[2] | |
183 | |
184 old = l1[a1:a2] | |
185 new = l2[b1:b2] | |
186 | |
187 # bdiff sometimes gives huge matches past eof, this check eats them, | |
188 # and deals with the special first match case described above | |
189 if not old and not new: | |
190 continue | |
191 | |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
192 if opts.ignoreblanklines: |
2580
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
193 wsold = wsblanklinesre.sub('', "".join(old)) |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
194 wsnew = wsblanklinesre.sub('', "".join(new)) |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
195 if wsold == wsnew: |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
196 continue |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
197 |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
198 if opts.ignorewsamount: |
2580
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
199 wsold = wsamountre.sub(' ', "".join(old)) |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
200 wsold = wsappendedre.sub('\n', wsold) |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
201 wsnew = wsamountre.sub(' ', "".join(new)) |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
202 wsnew = wsappendedre.sub('\n', wsnew) |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
203 if wsold == wsnew: |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
204 continue |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
205 |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
206 if opts.ignorews: |
1637 | 207 wsold = wsre.sub('', "".join(old)) |
208 wsnew = wsre.sub('', "".join(new)) | |
209 if wsold == wsnew: | |
210 continue | |
211 | |
212 astart = contextstart(a1) | |
213 bstart = contextstart(b1) | |
214 prev = None | |
215 if hunk: | |
216 # join with the previous hunk if it falls inside the context | |
2874
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2859
diff
changeset
|
217 if astart < hunk[1] + opts.context + 1: |
1637 | 218 prev = hunk |
219 astart = hunk[1] | |
220 bstart = hunk[3] | |
221 else: | |
222 for x in yieldhunk(hunk, header): | |
223 yield x | |
224 # we only want to yield the header if the files differ, and | |
225 # we only want to yield it once. | |
226 header = None | |
227 if prev: | |
228 # we've joined the previous hunk, record the new ending points. | |
229 hunk[1] = a2 | |
230 hunk[3] = b2 | |
231 delta = hunk[4] | |
232 else: | |
233 # create a new hunk | |
234 hunk = [ astart, a2, bstart, b2, delta ] | |
235 | |
236 delta[len(delta):] = [ ' ' + x for x in l1[astart:a1] ] | |
237 delta[len(delta):] = [ '-' + x for x in old ] | |
238 delta[len(delta):] = [ '+' + x for x in new ] | |
239 | |
240 if hunk: | |
241 for x in yieldhunk(hunk, header): | |
242 yield x | |
243 | |
120
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
244 def patchtext(bin): |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
245 pos = 0 |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
246 t = [] |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
247 while pos < len(bin): |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
248 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
|
249 pos += 12 |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
250 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
|
251 pos += l |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
252 return "".join(t) |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
253 |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
254 def patch(a, bin): |
1379 | 255 return mpatch.patches(a, [bin]) |
432 | 256 |
4361
99c853a1408c
add mdiff.get_matching_blocks
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4108
diff
changeset
|
257 # similar to difflib.SequenceMatcher.get_matching_blocks |
99c853a1408c
add mdiff.get_matching_blocks
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4108
diff
changeset
|
258 def get_matching_blocks(a, b): |
99c853a1408c
add mdiff.get_matching_blocks
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4108
diff
changeset
|
259 return [(d[0], d[2], d[1] - d[0]) for d in bdiff.blocks(a, b)] |
99c853a1408c
add mdiff.get_matching_blocks
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4108
diff
changeset
|
260 |
1379 | 261 patches = mpatch.patches |
2078
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1723
diff
changeset
|
262 patchedsize = mpatch.patchedsize |
432 | 263 textdiff = bdiff.bdiff |