##// END OF EJS Templates
mdiff: let unidiff return (diffheader, hunks)...
Denis Laxalde -
r31273:92714858 default
parent child Browse files
Show More
@@ -196,12 +196,12 b' def allblocks(text1, text2, opts=None, l'
196 196 yield s1, '='
197 197
198 198 def unidiff(a, ad, b, bd, fn1, fn2, opts=defaultopts):
199 """Return a unified diff as a (headers, hunkstext) tuple.
199 """Return a unified diff as a (headers, hunks) tuple.
200 200
201 201 If the diff is not null, `headers` is a list with unified diff header
202 lines "--- <original>" and "+++ <new>" and `hunkstext` is a string
203 containing diff hunks. Otherwise, both `headers` and `hunkstext` are
204 empty.
202 lines "--- <original>" and "+++ <new>" and `hunks` is a generator yielding
203 (hunkrange, hunklines) coming from _unidiff().
204 Otherwise, `headers` and `hunks` are empty.
205 205 """
206 206 def datetag(date, fn=None):
207 207 if not opts.git and not opts.nodates:
@@ -210,7 +210,7 b' def unidiff(a, ad, b, bd, fn1, fn2, opts'
210 210 return '\t'
211 211 return ''
212 212
213 sentinel = [], ""
213 sentinel = [], ()
214 214 if not a and not b:
215 215 return sentinel
216 216
@@ -235,7 +235,7 b' def unidiff(a, ad, b, bd, fn1, fn2, opts'
235 235 if a and b and len(a) == len(b) and a == b:
236 236 return sentinel
237 237 headerlines = []
238 l = ['Binary file %s has changed\n' % fn1]
238 hunks = (None, ['Binary file %s has changed\n' % fn1]),
239 239 elif not a:
240 240 b = splitnewlines(b)
241 241 if a is None:
@@ -244,7 +244,10 b' def unidiff(a, ad, b, bd, fn1, fn2, opts'
244 244 l1 = "--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1))
245 245 l2 = "+++ %s%s" % (bprefix + fn2, datetag(bd, fn2))
246 246 headerlines = [l1, l2]
247 l = ["@@ -0,0 +1,%d @@\n" % len(b)] + ["+" + e for e in b]
247 size = len(b)
248 hunkrange = (0, 0, 1, size)
249 hunklines = ["@@ -0,0 +1,%d @@\n" % size] + ["+" + e for e in b]
250 hunks = (hunkrange, checknonewline(hunklines)),
248 251 elif not b:
249 252 a = splitnewlines(a)
250 253 l1 = "--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1))
@@ -253,18 +256,29 b' def unidiff(a, ad, b, bd, fn1, fn2, opts'
253 256 else:
254 257 l2 = "+++ %s%s%s" % (bprefix, fn2, datetag(bd, fn2))
255 258 headerlines = [l1, l2]
256 l = ["@@ -1,%d +0,0 @@\n" % len(a)] + ["-" + e for e in a]
259 size = len(a)
260 hunkrange = (1, size, 0, 0)
261 hunklines = ["@@ -1,%d +0,0 @@\n" % size] + ["-" + e for e in a]
262 hunks = (hunkrange, checknonewline(hunklines)),
257 263 else:
258 l = sum((hlines for hrange, hlines in _unidiff(a, b, opts=opts)), [])
259 if not l:
264 diffhunks = _unidiff(a, b, opts=opts)
265 try:
266 hunkrange, hunklines = next(diffhunks)
267 except StopIteration:
260 268 return sentinel
261 269
262 270 headerlines = [
263 271 "--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1)),
264 272 "+++ %s%s%s" % (bprefix, fn2, datetag(bd, fn2)),
265 273 ]
274 def rewindhunks():
275 yield hunkrange, checknonewline(hunklines)
276 for hr, hl in diffhunks:
277 yield hr, checknonewline(hl)
266 278
267 return headerlines, "".join(checknonewline(l))
279 hunks = rewindhunks()
280
281 return headerlines, hunks
268 282
269 283 def _unidiff(t1, t2, opts=defaultopts):
270 284 """Yield hunks of a headerless unified diff from t1 and t2 texts.
@@ -2549,10 +2549,11 b' def trydiff(repo, revs, ctx1, ctx2, modi'
2549 2549 gitindex(content2)[0:opts.index],
2550 2550 gitmode[flag]))
2551 2551
2552 uheaders, text = mdiff.unidiff(content1, date1,
2553 content2, date2,
2554 path1, path2, opts=opts)
2552 uheaders, hunks = mdiff.unidiff(content1, date1,
2553 content2, date2,
2554 path1, path2, opts=opts)
2555 2555 header.extend(uheaders)
2556 text = ''.join(sum((list(hlines) for hrange, hlines in hunks), []))
2556 2557 if header and (text or len(header) > 1):
2557 2558 yield '\n'.join(header) + '\n'
2558 2559 if text:
General Comments 0
You need to be logged in to leave comments. Login now