##// END OF EJS Templates
Improved binary diff from Christopher Li...
mpm@selenic.com -
r184:697f05bf default
parent child Browse files
Show More
@@ -1,71 +1,71 b''
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 10
11 11 for ln in xrange(len(l)):
12 12 if l[ln][-1] != '\n':
13 13 l[ln] += "\n\ No newline at end of file\n"
14 14
15 15 return "".join(l)
16 16
17 17 def textdiff(a, b):
18 18 return diff(a.splitlines(1), b.splitlines(1))
19 19
20 20 def sortdiff(a, b):
21 21 la = lb = 0
22
22 lena = len(a)
23 lenb = len(b)
23 24 while 1:
24 if la >= len(a) or lb >= len(b): break
25 if b[lb] < a[la]:
26 si = lb
27 while lb < len(b) and b[lb] < a[la] : lb += 1
28 yield "insert", la, la, si, lb
29 elif a[la] < b[lb]:
30 si = la
31 while la < len(a) and a[la] < b[lb]: la += 1
32 yield "delete", si, la, lb, lb
33 else:
25 am, bm, = la, lb
26 while lb < lenb and la < len and a[la] == b[lb] :
34 27 la += 1
35 28 lb += 1
36
37 if lb < len(b):
38 yield "insert", la, la, lb, len(b)
39
40 if la < len(a):
41 yield "delete", la, len(a), lb, lb
29 if la>am: yield (am, bm, la-am)
30 while lb < lenb and b[lb] < a[la]: lb += 1
31 if lb>=lenb: break
32 while la < lena and b[lb] > a[la]: la += 1
33 if la>=lena: break
34 yield (lena, lenb, 0)
42 35
43 36 def diff(a, b, sorted=0):
37 if not a:
38 s = "".join(b)
39 return s and (struct.pack(">lll", 0, 0, len(s)) + s)
40
44 41 bin = []
45 42 p = [0]
46 43 for i in a: p.append(p[-1] + len(i))
47 44
48 45 if sorted:
49 46 d = sortdiff(a, b)
50 47 else:
51 d = difflib.SequenceMatcher(None, a, b).get_opcodes()
52
53 for o, m, n, s, t in d:
54 if o == 'equal': continue
55 s = "".join(b[s:t])
56 bin.append(struct.pack(">lll", p[m], p[n], len(s)) + s)
57
48 d = difflib.SequenceMatcher(None, a, b).get_matching_blocks()
49 la = 0
50 lb = 0
51 for am, bm, size in d:
52 s = "".join(b[lb:bm])
53 if am > la or s:
54 bin.append(struct.pack(">lll", p[la], p[am], len(s)) + s)
55 la = am + size
56 lb = bm + size
57
58 58 return "".join(bin)
59 59
60 60 def patchtext(bin):
61 61 pos = 0
62 62 t = []
63 63 while pos < len(bin):
64 64 p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12])
65 65 pos += 12
66 66 t.append(bin[pos:pos + l])
67 67 pos += l
68 68 return "".join(t)
69 69
70 70 def patch(a, bin):
71 71 return patches(a, [bin])
General Comments 0
You need to be logged in to leave comments. Login now