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