Show More
@@ -1,65 +1,66 b'' | |||||
1 | #!/usr/bin/python |
|
1 | #!/usr/bin/python | |
2 |
import difflib, struct, mmap |
|
2 | import difflib, struct, mmap | |
|
3 | from mercurial.mpatch import * | |||
3 |
|
4 | |||
4 | def unidiff(a, ad, b, bd, fn): |
|
5 | def unidiff(a, ad, b, bd, fn): | |
5 | if not a and not b: return "" |
|
6 | if not a and not b: return "" | |
6 | a = a.splitlines(1) |
|
7 | a = a.splitlines(1) | |
7 | b = b.splitlines(1) |
|
8 | b = b.splitlines(1) | |
8 | l = list(difflib.unified_diff(a, b, "a/" + fn, "b/" + fn, ad, bd)) |
|
9 | l = list(difflib.unified_diff(a, b, "a/" + fn, "b/" + fn, ad, bd)) | |
9 | return "".join(l) |
|
10 | return "".join(l) | |
10 |
|
11 | |||
11 | def textdiff(a, b): |
|
12 | def textdiff(a, b): | |
12 | return diff(a.splitlines(1), b.splitlines(1)) |
|
13 | return diff(a.splitlines(1), b.splitlines(1)) | |
13 |
|
14 | |||
14 | def sortdiff(a, b): |
|
15 | def sortdiff(a, b): | |
15 | la = lb = 0 |
|
16 | la = lb = 0 | |
16 |
|
17 | |||
17 | while 1: |
|
18 | while 1: | |
18 | if la >= len(a) or lb >= len(b): break |
|
19 | if la >= len(a) or lb >= len(b): break | |
19 | if b[lb] < a[la]: |
|
20 | if b[lb] < a[la]: | |
20 | si = lb |
|
21 | si = lb | |
21 | while lb < len(b) and b[lb] < a[la] : lb += 1 |
|
22 | while lb < len(b) and b[lb] < a[la] : lb += 1 | |
22 | yield "insert", la, la, si, lb |
|
23 | yield "insert", la, la, si, lb | |
23 | elif a[la] < b[lb]: |
|
24 | elif a[la] < b[lb]: | |
24 | si = la |
|
25 | si = la | |
25 | while la < len(a) and a[la] < b[lb]: la += 1 |
|
26 | while la < len(a) and a[la] < b[lb]: la += 1 | |
26 | yield "delete", si, la, lb, lb |
|
27 | yield "delete", si, la, lb, lb | |
27 | else: |
|
28 | else: | |
28 | la += 1 |
|
29 | la += 1 | |
29 | lb += 1 |
|
30 | lb += 1 | |
30 |
|
31 | |||
31 | if lb < len(b): |
|
32 | if lb < len(b): | |
32 | yield "insert", la, la, lb, len(b) |
|
33 | yield "insert", la, la, lb, len(b) | |
33 |
|
34 | |||
34 | if la < len(a): |
|
35 | if la < len(a): | |
35 | yield "delete", la, len(a), lb, lb |
|
36 | yield "delete", la, len(a), lb, lb | |
36 |
|
37 | |||
37 | def diff(a, b, sorted=0): |
|
38 | def diff(a, b, sorted=0): | |
38 | bin = [] |
|
39 | bin = [] | |
39 | p = [0] |
|
40 | p = [0] | |
40 | for i in a: p.append(p[-1] + len(i)) |
|
41 | for i in a: p.append(p[-1] + len(i)) | |
41 |
|
42 | |||
42 | if sorted: |
|
43 | if sorted: | |
43 | d = sortdiff(a, b) |
|
44 | d = sortdiff(a, b) | |
44 | else: |
|
45 | else: | |
45 | d = difflib.SequenceMatcher(None, a, b).get_opcodes() |
|
46 | d = difflib.SequenceMatcher(None, a, b).get_opcodes() | |
46 |
|
47 | |||
47 | for o, m, n, s, t in d: |
|
48 | for o, m, n, s, t in d: | |
48 | if o == 'equal': continue |
|
49 | if o == 'equal': continue | |
49 | s = "".join(b[s:t]) |
|
50 | s = "".join(b[s:t]) | |
50 | bin.append(struct.pack(">lll", p[m], p[n], len(s)) + s) |
|
51 | bin.append(struct.pack(">lll", p[m], p[n], len(s)) + s) | |
51 |
|
52 | |||
52 | return "".join(bin) |
|
53 | return "".join(bin) | |
53 |
|
54 | |||
54 | def patchtext(bin): |
|
55 | def patchtext(bin): | |
55 | pos = 0 |
|
56 | pos = 0 | |
56 | t = [] |
|
57 | t = [] | |
57 | while pos < len(bin): |
|
58 | while pos < len(bin): | |
58 | p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) |
|
59 | p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) | |
59 | pos += 12 |
|
60 | pos += 12 | |
60 | t.append(bin[pos:pos + l]) |
|
61 | t.append(bin[pos:pos + l]) | |
61 | pos += l |
|
62 | pos += l | |
62 | return "".join(t) |
|
63 | return "".join(t) | |
63 |
|
64 | |||
64 | def patch(a, bin): |
|
65 | def patch(a, bin): | |
65 | return patches(a, [bin]) |
|
66 | return patches(a, [bin]) |
General Comments 0
You need to be logged in to leave comments.
Login now