##// END OF EJS Templates
Add new bdiff based unidiff generation.
mason@suse.com -
r1637:3b1b44b9 default
parent child Browse files
Show More
@@ -295,20 +295,26 b' def dodiff(fp, ui, repo, node1, node2, f'
295 295 mmap = repo.manifest.read(change[0])
296 296 date1 = util.datestr(change[2])
297 297
298 diffopts = ui.diffopts()
299 showfunc = diffopts['showfunc']
300 ignorews = diffopts['ignorews']
298 301 for f in modified:
299 302 to = None
300 303 if f in mmap:
301 304 to = repo.file(f).read(mmap[f])
302 305 tn = read(f)
303 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text))
306 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text,
307 showfunc=showfunc, ignorews=ignorews))
304 308 for f in added:
305 309 to = None
306 310 tn = read(f)
307 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text))
311 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text,
312 showfunc=showfunc, ignorews=ignorews))
308 313 for f in removed:
309 314 to = repo.file(f).read(mmap[f])
310 315 tn = None
311 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text))
316 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text,
317 showfunc=showfunc, ignorews=ignorews))
312 318
313 319 def trimuser(ui, name, rev, revcache):
314 320 """trim the name of the user who committed a change"""
@@ -270,18 +270,24 b' class hgweb(object):'
270 270 modified, added, removed = map(lambda x: filterfiles(files, x),
271 271 (modified, added, removed))
272 272
273 diffopts = self.repo.ui.diffopts()
274 showfunc = diffopts['showfunc']
275 ignorews = diffopts['ignorews']
273 276 for f in modified:
274 277 to = r.file(f).read(mmap1[f])
275 278 tn = r.file(f).read(mmap2[f])
276 yield diffblock(mdiff.unidiff(to, date1, tn, date2, f), f, tn)
279 yield diffblock(mdiff.unidiff(to, date1, tn, date2, f,
280 showfunc=showfunc, ignorews=ignorews), f, tn)
277 281 for f in added:
278 282 to = None
279 283 tn = r.file(f).read(mmap2[f])
280 yield diffblock(mdiff.unidiff(to, date1, tn, date2, f), f, tn)
284 yield diffblock(mdiff.unidiff(to, date1, tn, date2, f,
285 showfunc=showfunc, ignorews=ignorews), f, tn)
281 286 for f in removed:
282 287 to = r.file(f).read(mmap1[f])
283 288 tn = None
284 yield diffblock(mdiff.unidiff(to, date1, tn, date2, f), f, tn)
289 yield diffblock(mdiff.unidiff(to, date1, tn, date2, f,
290 showfunc=showfunc, ignorews=ignorews), f, tn)
285 291
286 292 def changelog(self, pos):
287 293 def changenav(**map):
@@ -5,9 +5,13 b''
5 5 # This software may be used and distributed according to the terms
6 6 # of the GNU General Public License, incorporated herein by reference.
7 7
8 import difflib, struct, bdiff, util, mpatch
8 from demandload import demandload
9 import struct, bdiff, util, mpatch
10 demandload(globals(), "re")
9 11
10 def unidiff(a, ad, b, bd, fn, r=None, text=False):
12
13 def unidiff(a, ad, b, bd, fn, r=None, text=False,
14 showfunc=False, ignorews=False):
11 15
12 16 if not a and not b: return ""
13 17 epoch = util.datestr((0, 0))
@@ -27,9 +31,10 b' def unidiff(a, ad, b, bd, fn, r=None, te'
27 31 l3 = "@@ -1,%d +0,0 @@\n" % len(a)
28 32 l = [l1, l2, l3] + ["-" + e for e in a]
29 33 else:
30 a = a.splitlines(1)
31 b = b.splitlines(1)
32 l = list(difflib.unified_diff(a, b, "a/" + fn, "b/" + fn))
34 al = a.splitlines(1)
35 bl = b.splitlines(1)
36 l = list(bunidiff(a, b, al, bl, "a/" + fn, "b/" + fn,
37 showfunc=showfunc, ignorews=ignorews))
33 38 if not l: return ""
34 39 # difflib uses a space, rather than a tab
35 40 l[0] = "%s\t%s\n" % (l[0][:-2], ad)
@@ -45,6 +50,128 b' def unidiff(a, ad, b, bd, fn, r=None, te'
45 50
46 51 return "".join(l)
47 52
53 # somewhat self contained replacement for difflib.unified_diff
54 # t1 and t2 are the text to be diffed
55 # l1 and l2 are the text broken up into lines
56 # header1 and header2 are the filenames for the diff output
57 # context is the number of context lines
58 # showfunc enables diff -p output
59 # ignorews ignores all whitespace changes in the diff
60 def bunidiff(t1, t2, l1, l2, header1, header2, context=3, showfunc=False,
61 ignorews=False):
62 def contextend(l, len):
63 ret = l + context
64 if ret > len:
65 ret = len
66 return ret
67
68 def contextstart(l):
69 ret = l - context
70 if ret < 0:
71 return 0
72 return ret
73
74 def yieldhunk(hunk, header):
75 if header:
76 for x in header:
77 yield x
78 (astart, a2, bstart, b2, delta) = hunk
79 aend = contextend(a2, len(l1))
80 alen = aend - astart
81 blen = b2 - bstart + aend - a2
82
83 func = ""
84 if showfunc:
85 # walk backwards from the start of the context
86 # to find a line starting with an alphanumeric char.
87 for x in xrange(astart, -1, -1):
88 t = l1[x].rstrip()
89 if funcre.match(t):
90 func = ' ' + t[:40]
91 break
92
93 yield "@@ -%d,%d +%d,%d @@%s\n" % (astart + 1, alen,
94 bstart + 1, blen, func)
95 for x in delta:
96 yield x
97 for x in xrange(a2, aend):
98 yield ' ' + l1[x]
99
100 header = [ "--- %s\t\n" % header1, "+++ %s\t\n" % header2 ]
101
102 if showfunc:
103 funcre = re.compile('\w')
104 if ignorews:
105 wsre = re.compile('[ \t]')
106
107 # bdiff.blocks gives us the matching sequences in the files. The loop
108 # below finds the spaces between those matching sequences and translates
109 # them into diff output.
110 #
111 diff = bdiff.blocks(t1, t2)
112 hunk = None
113 for i in xrange(len(diff)):
114 # The first match is special.
115 # we've either found a match starting at line 0 or a match later
116 # in the file. If it starts later, old and new below will both be
117 # empty and we'll continue to the next match.
118 if i > 0:
119 s = diff[i-1]
120 else:
121 s = [0, 0, 0, 0]
122 delta = []
123 s1 = diff[i]
124 a1 = s[1]
125 a2 = s1[0]
126 b1 = s[3]
127 b2 = s1[2]
128
129 old = l1[a1:a2]
130 new = l2[b1:b2]
131
132 # bdiff sometimes gives huge matches past eof, this check eats them,
133 # and deals with the special first match case described above
134 if not old and not new:
135 continue
136
137 if ignorews:
138 wsold = wsre.sub('', "".join(old))
139 wsnew = wsre.sub('', "".join(new))
140 if wsold == wsnew:
141 continue
142
143 astart = contextstart(a1)
144 bstart = contextstart(b1)
145 prev = None
146 if hunk:
147 # join with the previous hunk if it falls inside the context
148 if astart < hunk[1] + context + 1:
149 prev = hunk
150 astart = hunk[1]
151 bstart = hunk[3]
152 else:
153 for x in yieldhunk(hunk, header):
154 yield x
155 # we only want to yield the header if the files differ, and
156 # we only want to yield it once.
157 header = None
158 if prev:
159 # we've joined the previous hunk, record the new ending points.
160 hunk[1] = a2
161 hunk[3] = b2
162 delta = hunk[4]
163 else:
164 # create a new hunk
165 hunk = [ astart, a2, bstart, b2, delta ]
166
167 delta[len(delta):] = [ ' ' + x for x in l1[astart:a1] ]
168 delta[len(delta):] = [ '-' + x for x in old ]
169 delta[len(delta):] = [ '+' + x for x in new ]
170
171 if hunk:
172 for x in yieldhunk(hunk, header):
173 yield x
174
48 175 def patchtext(bin):
49 176 pos = 0
50 177 t = []
@@ -23,6 +23,7 b' class ui(object):'
23 23 self.interactive = self.configbool("ui", "interactive", True)
24 24
25 25 self.updateopts(verbose, debug, quiet, interactive)
26 self.diffcache = None
26 27
27 28 def updateopts(self, verbose=False, debug=False, quiet=False,
28 29 interactive=True):
@@ -76,6 +77,23 b' class ui(object):'
76 77 def extensions(self):
77 78 return self.configitems("extensions")
78 79
80 def diffopts(self):
81 if self.diffcache:
82 return self.diffcache
83 ret = { 'showfunc' : True, 'ignorews' : False}
84 for x in self.configitems("diff"):
85 k = x[0].lower()
86 v = x[1]
87 if v:
88 v = v.lower()
89 if v == 'true':
90 value = True
91 else:
92 value = False
93 ret[k] = value
94 self.diffcache = ret
95 return ret
96
79 97 def username(self):
80 98 return (os.environ.get("HGUSER") or
81 99 self.config("ui", "username") or
@@ -11,7 +11,7 b' merging file1 failed!'
11 11 diff -r f4d7a8c73d23 file1
12 12 --- a/file1
13 13 +++ b/file1
14 @@ -1,3 +1,7 @@
14 @@ -1,3 +1,7 @@ added file1
15 15 added file1
16 16 another line of text
17 17 +<<<<<<<
@@ -2,7 +2,7 b' adding a'
2 2 diff -r c19d34741b0a a
3 3 --- a/a
4 4 +++ b/a
5 @@ -1,1 +1,1 @@
5 @@ -1,1 +1,1 @@ a
6 6 -a
7 7 +abc
8 8 adding b
@@ -47,6 +47,6 b' 1'
47 47 diff -r 1e71731e6fbb a
48 48 --- a/a
49 49 +++ b/a
50 @@ -1,1 +1,1 @@
50 @@ -1,1 +1,1 @@ a2
51 51 -a2
52 52 +abc
General Comments 0
You need to be logged in to leave comments. Login now