Show More
@@ -1,91 +1,94 b'' | |||||
1 | # mdiff.py - diff and patch routines for mercurial |
|
1 | # mdiff.py - diff and patch routines for mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2005 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2005 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms |
|
5 | # This software may be used and distributed according to the terms | |
6 | # of the GNU General Public License, incorporated herein by reference. |
|
6 | # of the GNU General Public License, incorporated herein by reference. | |
7 |
|
7 | |||
8 | import difflib, struct |
|
8 | import difflib, struct | |
9 | from mercurial.mpatch import * |
|
9 | from mercurial.mpatch import * | |
10 |
|
10 | |||
11 | def unidiff(a, ad, b, bd, fn): |
|
11 | def unidiff(a, ad, b, bd, fn): | |
12 | if not a and not b: return "" |
|
12 | if not a and not b: return "" | |
13 |
|
13 | |||
14 | if a == None: |
|
14 | if a == None: | |
15 | b = b.splitlines(1) |
|
15 | b = b.splitlines(1) | |
16 | l1 = "--- %s\t%s\n" % ("/dev/null", ad) |
|
16 | l1 = "--- %s\t%s\n" % ("/dev/null", ad) | |
17 | l2 = "+++ %s\t%s\n" % ("b/" + fn, bd) |
|
17 | l2 = "+++ %s\t%s\n" % ("b/" + fn, bd) | |
18 | l3 = "@@ -0,0 +1,%d @@\n" % len(b) |
|
18 | l3 = "@@ -0,0 +1,%d @@\n" % len(b) | |
19 | l = [l1, l2, l3] + ["+" + e for e in b] |
|
19 | l = [l1, l2, l3] + ["+" + e for e in b] | |
20 | elif b == None: |
|
20 | elif b == None: | |
21 | a = a.splitlines(1) |
|
21 | a = a.splitlines(1) | |
22 | l1 = "--- %s\t%s\n" % ("a/" + fn, ad) |
|
22 | l1 = "--- %s\t%s\n" % ("a/" + fn, ad) | |
23 | l2 = "+++ %s\t%s\n" % ("/dev/null", bd) |
|
23 | l2 = "+++ %s\t%s\n" % ("/dev/null", bd) | |
24 | l3 = "@@ -1,%d +0,0 @@\n" % len(a) |
|
24 | l3 = "@@ -1,%d +0,0 @@\n" % len(a) | |
25 | l = [l1, l2, l3] + ["-" + e for e in a] |
|
25 | l = [l1, l2, l3] + ["-" + e for e in a] | |
26 | else: |
|
26 | else: | |
27 | a = a.splitlines(1) |
|
27 | a = a.splitlines(1) | |
28 | b = b.splitlines(1) |
|
28 | b = b.splitlines(1) | |
29 |
l = list(difflib.unified_diff(a, b, "a/" + fn, "b/" + fn |
|
29 | l = list(difflib.unified_diff(a, b, "a/" + fn, "b/" + fn)) | |
|
30 | # difflib uses a space, rather than a tab | |||
|
31 | l[0] = l[0][:-2] + "\t" + ad + "\n" | |||
|
32 | l[1] = l[1][:-2] + "\t" + bd + "\n" | |||
30 |
|
33 | |||
31 | for ln in xrange(len(l)): |
|
34 | for ln in xrange(len(l)): | |
32 | if l[ln][-1] != '\n': |
|
35 | if l[ln][-1] != '\n': | |
33 | l[ln] += "\n\ No newline at end of file\n" |
|
36 | l[ln] += "\n\ No newline at end of file\n" | |
34 |
|
37 | |||
35 | return "".join(l) |
|
38 | return "".join(l) | |
36 |
|
39 | |||
37 | def textdiff(a, b): |
|
40 | def textdiff(a, b): | |
38 | return diff(a.splitlines(1), b.splitlines(1)) |
|
41 | return diff(a.splitlines(1), b.splitlines(1)) | |
39 |
|
42 | |||
40 | def sortdiff(a, b): |
|
43 | def sortdiff(a, b): | |
41 | la = lb = 0 |
|
44 | la = lb = 0 | |
42 | lena = len(a) |
|
45 | lena = len(a) | |
43 | lenb = len(b) |
|
46 | lenb = len(b) | |
44 | while 1: |
|
47 | while 1: | |
45 | am, bm, = la, lb |
|
48 | am, bm, = la, lb | |
46 | while lb < lenb and la < len and a[la] == b[lb] : |
|
49 | while lb < lenb and la < len and a[la] == b[lb] : | |
47 | la += 1 |
|
50 | la += 1 | |
48 | lb += 1 |
|
51 | lb += 1 | |
49 | if la>am: yield (am, bm, la-am) |
|
52 | if la>am: yield (am, bm, la-am) | |
50 | while lb < lenb and b[lb] < a[la]: lb += 1 |
|
53 | while lb < lenb and b[lb] < a[la]: lb += 1 | |
51 | if lb>=lenb: break |
|
54 | if lb>=lenb: break | |
52 | while la < lena and b[lb] > a[la]: la += 1 |
|
55 | while la < lena and b[lb] > a[la]: la += 1 | |
53 | if la>=lena: break |
|
56 | if la>=lena: break | |
54 | yield (lena, lenb, 0) |
|
57 | yield (lena, lenb, 0) | |
55 |
|
58 | |||
56 | def diff(a, b, sorted=0): |
|
59 | def diff(a, b, sorted=0): | |
57 | if not a: |
|
60 | if not a: | |
58 | s = "".join(b) |
|
61 | s = "".join(b) | |
59 | return s and (struct.pack(">lll", 0, 0, len(s)) + s) |
|
62 | return s and (struct.pack(">lll", 0, 0, len(s)) + s) | |
60 |
|
63 | |||
61 | bin = [] |
|
64 | bin = [] | |
62 | p = [0] |
|
65 | p = [0] | |
63 | for i in a: p.append(p[-1] + len(i)) |
|
66 | for i in a: p.append(p[-1] + len(i)) | |
64 |
|
67 | |||
65 | if sorted: |
|
68 | if sorted: | |
66 | d = sortdiff(a, b) |
|
69 | d = sortdiff(a, b) | |
67 | else: |
|
70 | else: | |
68 | d = difflib.SequenceMatcher(None, a, b).get_matching_blocks() |
|
71 | d = difflib.SequenceMatcher(None, a, b).get_matching_blocks() | |
69 | la = 0 |
|
72 | la = 0 | |
70 | lb = 0 |
|
73 | lb = 0 | |
71 | for am, bm, size in d: |
|
74 | for am, bm, size in d: | |
72 | s = "".join(b[lb:bm]) |
|
75 | s = "".join(b[lb:bm]) | |
73 | if am > la or s: |
|
76 | if am > la or s: | |
74 | bin.append(struct.pack(">lll", p[la], p[am], len(s)) + s) |
|
77 | bin.append(struct.pack(">lll", p[la], p[am], len(s)) + s) | |
75 | la = am + size |
|
78 | la = am + size | |
76 | lb = bm + size |
|
79 | lb = bm + size | |
77 |
|
80 | |||
78 | return "".join(bin) |
|
81 | return "".join(bin) | |
79 |
|
82 | |||
80 | def patchtext(bin): |
|
83 | def patchtext(bin): | |
81 | pos = 0 |
|
84 | pos = 0 | |
82 | t = [] |
|
85 | t = [] | |
83 | while pos < len(bin): |
|
86 | while pos < len(bin): | |
84 | p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) |
|
87 | p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) | |
85 | pos += 12 |
|
88 | pos += 12 | |
86 | t.append(bin[pos:pos + l]) |
|
89 | t.append(bin[pos:pos + l]) | |
87 | pos += l |
|
90 | pos += l | |
88 | return "".join(t) |
|
91 | return "".join(t) | |
89 |
|
92 | |||
90 | def patch(a, bin): |
|
93 | def patch(a, bin): | |
91 | return patches(a, [bin]) |
|
94 | return patches(a, [bin]) |
General Comments 0
You need to be logged in to leave comments.
Login now