Mercurial > hg > mercurial-crew-with-dirclash
annotate mercurial/mdiff.py @ 2878:66eff8355168
tests: new test for hg diff of a subdirectory only
author | Giorgos Keramidas <keramida@ceid.upatras.gr> |
---|---|
date | Sun, 13 Aug 2006 23:00:18 +0300 |
parents | 4ec58b157265 |
children | 8b02af865990 |
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 # |
2858 | 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 |
fe1689273f84
use demandload more.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2251
diff
changeset
|
10 demandload(globals(), "re struct util") |
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 |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
22 class diffopts(object): |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
23 '''context is the number of context lines |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
24 text treats all files as text |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
25 showfunc enables diff -p output |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
26 ignorews ignores all whitespace changes in the diff |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
27 ignorewsamount ignores changes in the amount of whitespace |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
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 |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
30 defaults = { |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
31 'context': 3, |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
32 'text': False, |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
33 'showfunc': True, |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
34 'ignorews': False, |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
35 'ignorewsamount': False, |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
36 'ignoreblanklines': False, |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
37 } |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
38 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
39 __slots__ = defaults.keys() |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
40 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
41 def __init__(self, **opts): |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
42 for k in self.__slots__: |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
43 v = opts.get(k) |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
44 if v is None: |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
45 v = self.defaults[k] |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
46 setattr(self, k, v) |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
47 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
48 defaultopts = diffopts() |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
49 |
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
50 def unidiff(a, ad, b, bd, fn, r=None, opts=defaultopts): |
35 | 51 if not a and not b: return "" |
1379 | 52 epoch = util.datestr((0, 0)) |
264
4c1d7072d5cd
Attempt to make diff deal with null sources properly
mpm@selenic.com
parents:
249
diff
changeset
|
53 |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
54 if not opts.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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 a = splitnewlines(a) |
264
4c1d7072d5cd
Attempt to make diff deal with null sources properly
mpm@selenic.com
parents:
249
diff
changeset
|
67 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
|
68 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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 else: |
2251
35fb62a3a673
fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2248
diff
changeset
|
75 al = splitnewlines(a) |
35fb62a3a673
fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2248
diff
changeset
|
76 bl = splitnewlines(b) |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
77 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
|
78 if not l: return "" |
272
467cea2bf2d8
diff: use tab to separate date from filename
mpm@selenic.com
parents:
264
diff
changeset
|
79 # difflib uses a space, rather than a tab |
1540
8ca9f5b17257
minor optimization: save some string trash
twaldmann@thinkmo.de
parents:
1452
diff
changeset
|
80 l[0] = "%s\t%s\n" % (l[0][:-2], ad) |
8ca9f5b17257
minor optimization: save some string trash
twaldmann@thinkmo.de
parents:
1452
diff
changeset
|
81 l[1] = "%s\t%s\n" % (l[1][:-2], bd) |
170 | 82 |
83 for ln in xrange(len(l)): | |
84 if l[ln][-1] != '\n': | |
85 l[ln] += "\n\ No newline at end of file\n" | |
86 | |
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
|
87 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
|
88 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
|
89 (' '.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
|
90 |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
91 return "".join(l) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
92 |
1637 | 93 # somewhat self contained replacement for difflib.unified_diff |
94 # t1 and t2 are the text to be diffed | |
95 # l1 and l2 are the text broken up into lines | |
96 # header1 and header2 are the filenames for the diff output | |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
97 def bunidiff(t1, t2, l1, l2, header1, header2, opts=defaultopts): |
1637 | 98 def contextend(l, len): |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
99 ret = l + opts.context |
1637 | 100 if ret > len: |
101 ret = len | |
102 return ret | |
103 | |
104 def contextstart(l): | |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
105 ret = l - opts.context |
1637 | 106 if ret < 0: |
107 return 0 | |
108 return ret | |
109 | |
110 def yieldhunk(hunk, header): | |
111 if header: | |
112 for x in header: | |
113 yield x | |
114 (astart, a2, bstart, b2, delta) = hunk | |
115 aend = contextend(a2, len(l1)) | |
116 alen = aend - astart | |
117 blen = b2 - bstart + aend - a2 | |
118 | |
119 func = "" | |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
120 if opts.showfunc: |
1637 | 121 # walk backwards from the start of the context |
122 # to find a line starting with an alphanumeric char. | |
123 for x in xrange(astart, -1, -1): | |
124 t = l1[x].rstrip() | |
125 if funcre.match(t): | |
126 func = ' ' + t[:40] | |
127 break | |
128 | |
129 yield "@@ -%d,%d +%d,%d @@%s\n" % (astart + 1, alen, | |
130 bstart + 1, blen, func) | |
131 for x in delta: | |
132 yield x | |
133 for x in xrange(a2, aend): | |
134 yield ' ' + l1[x] | |
135 | |
136 header = [ "--- %s\t\n" % header1, "+++ %s\t\n" % header2 ] | |
137 | |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
138 if opts.showfunc: |
1637 | 139 funcre = re.compile('\w') |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
140 if opts.ignorewsamount: |
2580
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
141 wsamountre = re.compile('[ \t]+') |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
142 wsappendedre = re.compile(' \n') |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
143 if opts.ignoreblanklines: |
2580
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
144 wsblanklinesre = re.compile('\n') |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
145 if opts.ignorews: |
1637 | 146 wsre = re.compile('[ \t]') |
147 | |
148 # bdiff.blocks gives us the matching sequences in the files. The loop | |
149 # below finds the spaces between those matching sequences and translates | |
150 # them into diff output. | |
151 # | |
152 diff = bdiff.blocks(t1, t2) | |
153 hunk = None | |
154 for i in xrange(len(diff)): | |
155 # The first match is special. | |
156 # we've either found a match starting at line 0 or a match later | |
157 # in the file. If it starts later, old and new below will both be | |
158 # empty and we'll continue to the next match. | |
159 if i > 0: | |
160 s = diff[i-1] | |
161 else: | |
162 s = [0, 0, 0, 0] | |
163 delta = [] | |
164 s1 = diff[i] | |
165 a1 = s[1] | |
166 a2 = s1[0] | |
167 b1 = s[3] | |
168 b2 = s1[2] | |
169 | |
170 old = l1[a1:a2] | |
171 new = l2[b1:b2] | |
172 | |
173 # bdiff sometimes gives huge matches past eof, this check eats them, | |
174 # and deals with the special first match case described above | |
175 if not old and not new: | |
176 continue | |
177 | |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
178 if opts.ignoreblanklines: |
2580
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
179 wsold = wsblanklinesre.sub('', "".join(old)) |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
180 wsnew = wsblanklinesre.sub('', "".join(new)) |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
181 if wsold == wsnew: |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
182 continue |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
183 |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
184 if opts.ignorewsamount: |
2580
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
185 wsold = wsamountre.sub(' ', "".join(old)) |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
186 wsold = wsappendedre.sub('\n', wsold) |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
187 wsnew = wsamountre.sub(' ', "".join(new)) |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
188 wsnew = wsappendedre.sub('\n', wsnew) |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
189 if wsold == wsnew: |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
190 continue |
a20a1bb0c396
diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents:
2470
diff
changeset
|
191 |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
192 if opts.ignorews: |
1637 | 193 wsold = wsre.sub('', "".join(old)) |
194 wsnew = wsre.sub('', "".join(new)) | |
195 if wsold == wsnew: | |
196 continue | |
197 | |
198 astart = contextstart(a1) | |
199 bstart = contextstart(b1) | |
200 prev = None | |
201 if hunk: | |
202 # join with the previous hunk if it falls inside the context | |
2873
4ec58b157265
refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2858
diff
changeset
|
203 if astart < hunk[1] + opts.context + 1: |
1637 | 204 prev = hunk |
205 astart = hunk[1] | |
206 bstart = hunk[3] | |
207 else: | |
208 for x in yieldhunk(hunk, header): | |
209 yield x | |
210 # we only want to yield the header if the files differ, and | |
211 # we only want to yield it once. | |
212 header = None | |
213 if prev: | |
214 # we've joined the previous hunk, record the new ending points. | |
215 hunk[1] = a2 | |
216 hunk[3] = b2 | |
217 delta = hunk[4] | |
218 else: | |
219 # create a new hunk | |
220 hunk = [ astart, a2, bstart, b2, delta ] | |
221 | |
222 delta[len(delta):] = [ ' ' + x for x in l1[astart:a1] ] | |
223 delta[len(delta):] = [ '-' + x for x in old ] | |
224 delta[len(delta):] = [ '+' + x for x in new ] | |
225 | |
226 if hunk: | |
227 for x in yieldhunk(hunk, header): | |
228 yield x | |
229 | |
120
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
230 def patchtext(bin): |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
231 pos = 0 |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
232 t = [] |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
233 while pos < len(bin): |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
234 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
|
235 pos += 12 |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
236 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
|
237 pos += l |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
238 return "".join(t) |
bae6f0328f63
Add a function to return the new text from a binary diff
mpm@selenic.com
parents:
75
diff
changeset
|
239 |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
240 def patch(a, bin): |
1379 | 241 return mpatch.patches(a, [bin]) |
432 | 242 |
1379 | 243 patches = mpatch.patches |
2078
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1723
diff
changeset
|
244 patchedsize = mpatch.patchedsize |
432 | 245 textdiff = bdiff.bdiff |