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