Show More
@@ -1,99 +1,99 | |||||
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 | if not l: return "" |
|
30 | if not l: return "" | |
31 | # difflib uses a space, rather than a tab |
|
31 | # difflib uses a space, rather than a tab | |
32 | l[0] = l[0][:-2] + "\t" + ad + "\n" |
|
32 | l[0] = l[0][:-2] + "\t" + ad + "\n" | |
33 | l[1] = l[1][:-2] + "\t" + bd + "\n" |
|
33 | l[1] = l[1][:-2] + "\t" + bd + "\n" | |
34 |
|
34 | |||
35 | for ln in xrange(len(l)): |
|
35 | for ln in xrange(len(l)): | |
36 | if l[ln][-1] != '\n': |
|
36 | if l[ln][-1] != '\n': | |
37 | l[ln] += "\n\ No newline at end of file\n" |
|
37 | l[ln] += "\n\ No newline at end of file\n" | |
38 |
|
38 | |||
39 | return "".join(l) |
|
39 | return "".join(l) | |
40 |
|
40 | |||
41 | def textdiff(a, b): |
|
41 | def textdiff(a, b): | |
42 | return diff(a.splitlines(1), b.splitlines(1)) |
|
42 | return diff(a.splitlines(1), b.splitlines(1)) | |
43 |
|
43 | |||
44 | def sortdiff(a, b): |
|
44 | def sortdiff(a, b): | |
45 | la = lb = 0 |
|
45 | la = lb = 0 | |
46 | lena = len(a) |
|
46 | ||
47 | lenb = len(b) |
|
|||
48 | while 1: |
|
47 | while 1: | |
49 | am, bm, = la, lb |
|
48 | if la >= len(a) or lb >= len(b): break | |
50 | while lb < lenb and la < len and a[la] == b[lb] : |
|
49 | if b[lb] < a[la]: | |
|
50 | si = lb | |||
|
51 | while lb < len(b) and b[lb] < a[la] : lb += 1 | |||
|
52 | yield "insert", la, la, si, lb | |||
|
53 | elif a[la] < b[lb]: | |||
|
54 | si = la | |||
|
55 | while la < len(a) and a[la] < b[lb]: la += 1 | |||
|
56 | yield "delete", si, la, lb, lb | |||
|
57 | else: | |||
51 | la += 1 |
|
58 | la += 1 | |
52 | lb += 1 |
|
59 | lb += 1 | |
53 | if la>am: yield (am, bm, la-am) |
|
60 | ||
54 | while lb < lenb and b[lb] < a[la]: lb += 1 |
|
61 | if lb < len(b): | |
55 | if lb>=lenb: break |
|
62 | yield "insert", la, la, lb, len(b) | |
56 | while la < lena and b[lb] > a[la]: la += 1 |
|
63 | ||
57 |
|
|
64 | if la < len(a): | |
58 |
yield |
|
65 | yield "delete", la, len(a), lb, lb | |
59 |
|
66 | |||
60 | def diff(a, b, sorted=0): |
|
67 | def diff(a, b, sorted=0): | |
61 | if not a: |
|
|||
62 | s = "".join(b) |
|
|||
63 | return s and (struct.pack(">lll", 0, 0, len(s)) + s) |
|
|||
64 |
|
||||
65 | bin = [] |
|
68 | bin = [] | |
66 | p = [0] |
|
69 | p = [0] | |
67 | for i in a: p.append(p[-1] + len(i)) |
|
70 | for i in a: p.append(p[-1] + len(i)) | |
68 |
|
71 | |||
69 | if sorted: |
|
72 | if sorted: | |
70 | try: |
|
73 | try: | |
71 | d = sortdiff(a, b) |
|
74 | d = sortdiff(a, b) | |
72 | except: |
|
75 | except: | |
73 | print a, b |
|
76 | print a, b | |
74 | raise |
|
77 | raise | |
75 | else: |
|
78 | else: | |
76 |
d = difflib.SequenceMatcher(None, a, b).get_ |
|
79 | d = difflib.SequenceMatcher(None, a, b).get_opcodes() | |
77 | la = 0 |
|
80 | ||
78 | lb = 0 |
|
81 | for o, m, n, s, t in d: | |
79 | for am, bm, size in d: |
|
82 | if o == 'equal': continue | |
80 |
s = "".join(b[ |
|
83 | s = "".join(b[s:t]) | |
81 | if am > la or s: |
|
84 | bin.append(struct.pack(">lll", p[m], p[n], len(s)) + s) | |
82 | bin.append(struct.pack(">lll", p[la], p[am], len(s)) + s) |
|
|||
83 | la = am + size |
|
|||
84 | lb = bm + size |
|
|||
85 |
|
85 | |||
86 | return "".join(bin) |
|
86 | return "".join(bin) | |
87 |
|
87 | |||
88 | def patchtext(bin): |
|
88 | def patchtext(bin): | |
89 | pos = 0 |
|
89 | pos = 0 | |
90 | t = [] |
|
90 | t = [] | |
91 | while pos < len(bin): |
|
91 | while pos < len(bin): | |
92 | p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) |
|
92 | p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) | |
93 | pos += 12 |
|
93 | pos += 12 | |
94 | t.append(bin[pos:pos + l]) |
|
94 | t.append(bin[pos:pos + l]) | |
95 | pos += l |
|
95 | pos += l | |
96 | return "".join(t) |
|
96 | return "".join(t) | |
97 |
|
97 | |||
98 | def patch(a, bin): |
|
98 | def patch(a, bin): | |
99 | return patches(a, [bin]) |
|
99 | return patches(a, [bin]) |
General Comments 0
You need to be logged in to leave comments.
Login now