Mercurial > hg
comparison mercurial/mdiff.py @ 2248:b914f0557832
fix diffs containing embedded "\r".
add test to make sure fix stays fixed.
author | Vadim Gelfer <vadim.gelfer@gmail.com> |
---|---|
date | Wed, 10 May 2006 10:31:54 -0700 |
parents | 441ea218414e |
children | 35fb62a3a673 |
comparison
equal
deleted
inserted
replaced
2247:546c76e5a3e6 | 2248:b914f0557832 |
---|---|
8 from demandload import demandload | 8 from demandload import demandload |
9 import struct, bdiff, util, mpatch | 9 import struct, bdiff, util, mpatch |
10 demandload(globals(), "re") | 10 demandload(globals(), "re") |
11 | 11 |
12 | 12 |
13 def splitnewlines(text, keepends=False): | |
14 '''like str.splitlines, but only split on newlines.''' | |
15 i = 0 | |
16 lines = [] | |
17 while True: | |
18 n = text.find('\n', i) | |
19 if n == -1: | |
20 last = text[i:] | |
21 if last: | |
22 lines.append(last) | |
23 return lines | |
24 lines.append(text[i:keepends and n+1 or n]) | |
25 i = n + 1 | |
26 | |
13 def unidiff(a, ad, b, bd, fn, r=None, text=False, | 27 def unidiff(a, ad, b, bd, fn, r=None, text=False, |
14 showfunc=False, ignorews=False): | 28 showfunc=False, ignorews=False): |
15 | 29 |
16 if not a and not b: return "" | 30 if not a and not b: return "" |
17 epoch = util.datestr((0, 0)) | 31 epoch = util.datestr((0, 0)) |
18 | 32 |
19 if not text and (util.binary(a) or util.binary(b)): | 33 if not text and (util.binary(a) or util.binary(b)): |
20 l = ['Binary file %s has changed\n' % fn] | 34 l = ['Binary file %s has changed\n' % fn] |
21 elif not a: | 35 elif not a: |
22 b = b.splitlines(1) | 36 b = splitnewlines(b, keepends=True) |
23 if a is None: | 37 if a is None: |
24 l1 = "--- %s\t%s\n" % ("/dev/null", epoch) | 38 l1 = "--- %s\t%s\n" % ("/dev/null", epoch) |
25 else: | 39 else: |
26 l1 = "--- %s\t%s\n" % ("a/" + fn, ad) | 40 l1 = "--- %s\t%s\n" % ("a/" + fn, ad) |
27 l2 = "+++ %s\t%s\n" % ("b/" + fn, bd) | 41 l2 = "+++ %s\t%s\n" % ("b/" + fn, bd) |
28 l3 = "@@ -0,0 +1,%d @@\n" % len(b) | 42 l3 = "@@ -0,0 +1,%d @@\n" % len(b) |
29 l = [l1, l2, l3] + ["+" + e for e in b] | 43 l = [l1, l2, l3] + ["+" + e for e in b] |
30 elif not b: | 44 elif not b: |
31 a = a.splitlines(1) | 45 a = splitnewlines(a, keepends=True) |
32 l1 = "--- %s\t%s\n" % ("a/" + fn, ad) | 46 l1 = "--- %s\t%s\n" % ("a/" + fn, ad) |
33 if b is None: | 47 if b is None: |
34 l2 = "+++ %s\t%s\n" % ("/dev/null", epoch) | 48 l2 = "+++ %s\t%s\n" % ("/dev/null", epoch) |
35 else: | 49 else: |
36 l2 = "+++ %s\t%s\n" % ("b/" + fn, bd) | 50 l2 = "+++ %s\t%s\n" % ("b/" + fn, bd) |
37 l3 = "@@ -1,%d +0,0 @@\n" % len(a) | 51 l3 = "@@ -1,%d +0,0 @@\n" % len(a) |
38 l = [l1, l2, l3] + ["-" + e for e in a] | 52 l = [l1, l2, l3] + ["-" + e for e in a] |
39 else: | 53 else: |
40 al = a.splitlines(1) | 54 al = splitnewlines(a, keepends=True) |
41 bl = b.splitlines(1) | 55 bl = splitnewlines(b, keepends=True) |
42 l = list(bunidiff(a, b, al, bl, "a/" + fn, "b/" + fn, | 56 l = list(bunidiff(a, b, al, bl, "a/" + fn, "b/" + fn, |
43 showfunc=showfunc, ignorews=ignorews)) | 57 showfunc=showfunc, ignorews=ignorews)) |
44 if not l: return "" | 58 if not l: return "" |
45 # difflib uses a space, rather than a tab | 59 # difflib uses a space, rather than a tab |
46 l[0] = "%s\t%s\n" % (l[0][:-2], ad) | 60 l[0] = "%s\t%s\n" % (l[0][:-2], ad) |