annotate mercurial/mdiff.py @ 4108:226df1808f16

merge with crew-stable
author Thomas Arendsen Hein <thomas@intevation.de>
date Tue, 20 Feb 2007 20:55:23 +0100
parents abaee83ce0a6 544838cc1158
children 99c853a1408c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
345bac2bc4ec update copyrights.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2580
diff changeset
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
226df1808f16 merge with crew-stable
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3877 4103
diff changeset
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
8b02af865990 Add diff --git option
Brendan Cully <brendan@kublai.com>
parents: 2874
diff changeset
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
8b02af865990 Add diff --git option
Brendan Cully <brendan@kublai.com>
parents: 2874
diff changeset
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):
3026
d838bfac668d Remove dates from git export file lines - they confuse git-apply
Brendan Cully <brendan@kublai.com>
parents: 2907
diff changeset
53 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
54 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
55
35
9197c26a414b unidiff: punt on comparing empty files
mpm@selenic.com
parents: 0
diff changeset
56 if not a and not b: return ""
1379
8ee7ce877be2 Clean up mdiff imports
Matt Mackall <mpm@selenic.com>
parents: 1378
diff changeset
57 epoch = util.datestr((0, 0))
264
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
58
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
59 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
60 def h(v):
544838cc1158 Don't lie that "binary file has changed"
tailgunner@smtp.ru
parents: 3199
diff changeset
61 # 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
62 return md5.new(v).digest()
544838cc1158 Don't lie that "binary file has changed"
tailgunner@smtp.ru
parents: 3199
diff changeset
63 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
64 return ""
1015
22571b8d35d3 Add automatic binary file detection to diff and export
mpm@selenic.com
parents: 582
diff changeset
65 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
66 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
67 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
68 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
69 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
70 else:
3026
d838bfac668d Remove dates from git export file lines - they confuse git-apply
Brendan Cully <brendan@kublai.com>
parents: 2907
diff changeset
71 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
72 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
73 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
74 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
75 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
76 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
77 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
78 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
79 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
80 else:
3026
d838bfac668d Remove dates from git export file lines - they confuse git-apply
Brendan Cully <brendan@kublai.com>
parents: 2907
diff changeset
81 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
82 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
83 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
84 else:
2251
35fb62a3a673 fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2248
diff changeset
85 al = splitnewlines(a)
35fb62a3a673 fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2248
diff changeset
86 bl = splitnewlines(b)
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
87 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
88 if not l: return ""
272
467cea2bf2d8 diff: use tab to separate date from filename
mpm@selenic.com
parents: 264
diff changeset
89 # 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
90 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
91 l[1] = "%s%s" % (l[1][:-2], datetag(bd))
170
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
92
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
93 for ln in xrange(len(l)):
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
94 if l[ln][-1] != '\n':
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
95 l[ln] += "\n\ No newline at end of file\n"
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
96
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
97 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
98 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
99 (' '.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
100
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
101 return "".join(l)
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
102
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
103 # somewhat self contained replacement for difflib.unified_diff
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
104 # t1 and t2 are the text to be diffed
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
105 # l1 and l2 are the text broken up into lines
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
106 # 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
107 def bunidiff(t1, t2, l1, l2, header1, header2, opts=defaultopts):
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
108 def contextend(l, len):
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
109 ret = l + opts.context
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
110 if ret > len:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
111 ret = len
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
112 return ret
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
113
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
114 def contextstart(l):
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
115 ret = l - opts.context
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
116 if ret < 0:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
117 return 0
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
118 return ret
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
119
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
120 def yieldhunk(hunk, header):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
121 if header:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
122 for x in header:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
123 yield x
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
124 (astart, a2, bstart, b2, delta) = hunk
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
125 aend = contextend(a2, len(l1))
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
126 alen = aend - astart
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
127 blen = b2 - bstart + aend - a2
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
128
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
129 func = ""
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
130 if opts.showfunc:
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
131 # walk backwards from the start of the context
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
132 # to find a line starting with an alphanumeric char.
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
133 for x in xrange(astart, -1, -1):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
134 t = l1[x].rstrip()
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
135 if funcre.match(t):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
136 func = ' ' + t[:40]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
137 break
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
138
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
139 yield "@@ -%d,%d +%d,%d @@%s\n" % (astart + 1, alen,
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
140 bstart + 1, blen, func)
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
141 for x in delta:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
142 yield x
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
143 for x in xrange(a2, aend):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
144 yield ' ' + l1[x]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
145
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
146 header = [ "--- %s\t\n" % header1, "+++ %s\t\n" % header2 ]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
147
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
148 if opts.showfunc:
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
149 funcre = re.compile('\w')
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
150 if opts.ignorewsamount:
2580
a20a1bb0c396 diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents: 2470
diff changeset
151 wsamountre = re.compile('[ \t]+')
a20a1bb0c396 diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents: 2470
diff changeset
152 wsappendedre = re.compile(' \n')
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
153 if opts.ignoreblanklines:
2580
a20a1bb0c396 diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents: 2470
diff changeset
154 wsblanklinesre = re.compile('\n')
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
155 if opts.ignorews:
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
156 wsre = re.compile('[ \t]')
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
157
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
158 # bdiff.blocks gives us the matching sequences in the files. The loop
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
159 # below finds the spaces between those matching sequences and translates
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
160 # them into diff output.
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
161 #
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
162 diff = bdiff.blocks(t1, t2)
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
163 hunk = None
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
164 for i in xrange(len(diff)):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
165 # The first match is special.
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
166 # we've either found a match starting at line 0 or a match later
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
167 # in the file. If it starts later, old and new below will both be
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
168 # empty and we'll continue to the next match.
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
169 if i > 0:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
170 s = diff[i-1]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
171 else:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
172 s = [0, 0, 0, 0]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
173 delta = []
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
174 s1 = diff[i]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
175 a1 = s[1]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
176 a2 = s1[0]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
177 b1 = s[3]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
178 b2 = s1[2]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
179
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
180 old = l1[a1:a2]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
181 new = l2[b1:b2]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
182
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
183 # bdiff sometimes gives huge matches past eof, this check eats them,
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
184 # and deals with the special first match case described above
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
185 if not old and not new:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
186 continue
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
187
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
188 if opts.ignoreblanklines:
2580
a20a1bb0c396 diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents: 2470
diff changeset
189 wsold = wsblanklinesre.sub('', "".join(old))
a20a1bb0c396 diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents: 2470
diff changeset
190 wsnew = wsblanklinesre.sub('', "".join(new))
a20a1bb0c396 diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents: 2470
diff changeset
191 if wsold == wsnew:
a20a1bb0c396 diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents: 2470
diff changeset
192 continue
a20a1bb0c396 diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents: 2470
diff changeset
193
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
194 if opts.ignorewsamount:
2580
a20a1bb0c396 diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents: 2470
diff changeset
195 wsold = wsamountre.sub(' ', "".join(old))
a20a1bb0c396 diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents: 2470
diff changeset
196 wsold = wsappendedre.sub('\n', wsold)
a20a1bb0c396 diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents: 2470
diff changeset
197 wsnew = wsamountre.sub(' ', "".join(new))
a20a1bb0c396 diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents: 2470
diff changeset
198 wsnew = wsappendedre.sub('\n', wsnew)
a20a1bb0c396 diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents: 2470
diff changeset
199 if wsold == wsnew:
a20a1bb0c396 diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents: 2470
diff changeset
200 continue
a20a1bb0c396 diff: add -b/-B options
Haakon Riiser <haakon.riiser@fys.uio.no>
parents: 2470
diff changeset
201
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
202 if opts.ignorews:
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
203 wsold = wsre.sub('', "".join(old))
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
204 wsnew = wsre.sub('', "".join(new))
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
205 if wsold == wsnew:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
206 continue
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
207
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
208 astart = contextstart(a1)
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
209 bstart = contextstart(b1)
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
210 prev = None
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
211 if hunk:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
212 # 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
213 if astart < hunk[1] + opts.context + 1:
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
214 prev = hunk
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
215 astart = hunk[1]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
216 bstart = hunk[3]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
217 else:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
218 for x in yieldhunk(hunk, header):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
219 yield x
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
220 # we only want to yield the header if the files differ, and
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
221 # we only want to yield it once.
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
222 header = None
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
223 if prev:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
224 # we've joined the previous hunk, record the new ending points.
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
225 hunk[1] = a2
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
226 hunk[3] = b2
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
227 delta = hunk[4]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
228 else:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
229 # create a new hunk
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
230 hunk = [ astart, a2, bstart, b2, delta ]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
231
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
232 delta[len(delta):] = [ ' ' + x for x in l1[astart:a1] ]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
233 delta[len(delta):] = [ '-' + x for x in old ]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
234 delta[len(delta):] = [ '+' + x for x in new ]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
235
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
236 if hunk:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
237 for x in yieldhunk(hunk, header):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
238 yield x
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
239
120
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
240 def patchtext(bin):
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
241 pos = 0
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
242 t = []
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
243 while pos < len(bin):
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
244 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
245 pos += 12
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
246 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
247 pos += l
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
248 return "".join(t)
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
249
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
250 def patch(a, bin):
1379
8ee7ce877be2 Clean up mdiff imports
Matt Mackall <mpm@selenic.com>
parents: 1378
diff changeset
251 return mpatch.patches(a, [bin])
432
3b9e3d3d2810 Start using bdiff for generating deltas
mpm@selenic.com
parents: 396
diff changeset
252
1379
8ee7ce877be2 Clean up mdiff imports
Matt Mackall <mpm@selenic.com>
parents: 1378
diff changeset
253 patches = mpatch.patches
2078
441ea218414e Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents: 1723
diff changeset
254 patchedsize = mpatch.patchedsize
432
3b9e3d3d2810 Start using bdiff for generating deltas
mpm@selenic.com
parents: 396
diff changeset
255 textdiff = bdiff.bdiff