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