##// END OF EJS Templates
Start using bdiff for generating deltas...
mpm@selenic.com -
r432:3b9e3d3d default
parent child Browse files
Show More
@@ -1,120 +1,121 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, bdiff
9 from mercurial.mpatch import *
9 from mpatch import *
10
10
11 def unidiff(a, ad, b, bd, fn, r=None):
11 def unidiff(a, ad, b, bd, fn, r=None):
12
12
13 if not a and not b: return ""
13 if not a and not b: return ""
14
14
15 if a == None:
15 if a == None:
16 b = b.splitlines(1)
16 b = b.splitlines(1)
17 l1 = "--- %s\t%s\n" % ("/dev/null", ad)
17 l1 = "--- %s\t%s\n" % ("/dev/null", ad)
18 l2 = "+++ %s\t%s\n" % ("b/" + fn, bd)
18 l2 = "+++ %s\t%s\n" % ("b/" + fn, bd)
19 l3 = "@@ -0,0 +1,%d @@\n" % len(b)
19 l3 = "@@ -0,0 +1,%d @@\n" % len(b)
20 l = [l1, l2, l3] + ["+" + e for e in b]
20 l = [l1, l2, l3] + ["+" + e for e in b]
21 elif b == None:
21 elif b == None:
22 a = a.splitlines(1)
22 a = a.splitlines(1)
23 l1 = "--- %s\t%s\n" % ("a/" + fn, ad)
23 l1 = "--- %s\t%s\n" % ("a/" + fn, ad)
24 l2 = "+++ %s\t%s\n" % ("/dev/null", bd)
24 l2 = "+++ %s\t%s\n" % ("/dev/null", bd)
25 l3 = "@@ -1,%d +0,0 @@\n" % len(a)
25 l3 = "@@ -1,%d +0,0 @@\n" % len(a)
26 l = [l1, l2, l3] + ["-" + e for e in a]
26 l = [l1, l2, l3] + ["-" + e for e in a]
27 else:
27 else:
28 a = a.splitlines(1)
28 a = a.splitlines(1)
29 b = b.splitlines(1)
29 b = b.splitlines(1)
30 l = list(difflib.unified_diff(a, b, "a/" + fn, "b/" + fn))
30 l = list(difflib.unified_diff(a, b, "a/" + fn, "b/" + fn))
31 if not l: return ""
31 if not l: return ""
32 # difflib uses a space, rather than a tab
32 # difflib uses a space, rather than a tab
33 l[0] = l[0][:-2] + "\t" + ad + "\n"
33 l[0] = l[0][:-2] + "\t" + ad + "\n"
34 l[1] = l[1][:-2] + "\t" + bd + "\n"
34 l[1] = l[1][:-2] + "\t" + bd + "\n"
35
35
36 for ln in xrange(len(l)):
36 for ln in xrange(len(l)):
37 if l[ln][-1] != '\n':
37 if l[ln][-1] != '\n':
38 l[ln] += "\n\ No newline at end of file\n"
38 l[ln] += "\n\ No newline at end of file\n"
39
39
40 if r:
40 if r:
41 l.insert(0, "diff %s %s\n" %
41 l.insert(0, "diff %s %s\n" %
42 (' '.join(["-r %s" % rev for rev in r]), fn))
42 (' '.join(["-r %s" % rev for rev in r]), fn))
43
43
44 return "".join(l)
44 return "".join(l)
45
45
46 def textdiff(a, b):
47 return diff(a.splitlines(1), b.splitlines(1))
48
49 def sortdiff(a, b):
46 def sortdiff(a, b):
50 la = lb = 0
47 la = lb = 0
51 lena = len(a)
48 lena = len(a)
52 lenb = len(b)
49 lenb = len(b)
53
50
54 while 1:
51 while 1:
55 am, bm, = la, lb
52 am, bm, = la, lb
56
53
57 # walk over matching lines
54 # walk over matching lines
58 while lb < lenb and la < lena and a[la] == b[lb] :
55 while lb < lenb and la < lena and a[la] == b[lb] :
59 la += 1
56 la += 1
60 lb += 1
57 lb += 1
61
58
62 if la > am:
59 if la > am:
63 yield (am, bm, la - am) # return a match
60 yield (am, bm, la - am) # return a match
64
61
65 # skip mismatched lines from b
62 # skip mismatched lines from b
66 while la < lena and lb < lenb and b[lb] < a[la]:
63 while la < lena and lb < lenb and b[lb] < a[la]:
67 lb += 1
64 lb += 1
68
65
69 if lb >= lenb:
66 if lb >= lenb:
70 break
67 break
71
68
72 # skip mismatched lines from a
69 # skip mismatched lines from a
73 while la < lena and lb < lenb and b[lb] > a[la]:
70 while la < lena and lb < lenb and b[lb] > a[la]:
74 la += 1
71 la += 1
75
72
76 if la >= lena:
73 if la >= lena:
77 break
74 break
78
75
79 yield (lena, lenb, 0)
76 yield (lena, lenb, 0)
80
77
81 def diff(a, b, sorted=0):
78 def diff(a, b, sorted=0):
82 if not a:
79 if not a:
83 s = "".join(b)
80 s = "".join(b)
84 return s and (struct.pack(">lll", 0, 0, len(s)) + s)
81 return s and (struct.pack(">lll", 0, 0, len(s)) + s)
85
82
86 bin = []
83 bin = []
87 p = [0]
84 p = [0]
88 for i in a: p.append(p[-1] + len(i))
85 for i in a: p.append(p[-1] + len(i))
89
86
90 if sorted:
87 if sorted:
91 try:
88 try:
92 d = sortdiff(a, b)
89 d = sortdiff(a, b)
93 except:
90 except:
94 print a, b
91 print a, b
95 raise
92 raise
96 else:
93 else:
97 d = difflib.SequenceMatcher(None, a, b).get_matching_blocks()
94 d = difflib.SequenceMatcher(None, a, b).get_matching_blocks()
98 la = 0
95 la = 0
99 lb = 0
96 lb = 0
100 for am, bm, size in d:
97 for am, bm, size in d:
101 s = "".join(b[lb:bm])
98 s = "".join(b[lb:bm])
102 if am > la or s:
99 if am > la or s:
103 bin.append(struct.pack(">lll", p[la], p[am], len(s)) + s)
100 bin.append(struct.pack(">lll", p[la], p[am], len(s)) + s)
104 la = am + size
101 la = am + size
105 lb = bm + size
102 lb = bm + size
106
103
107 return "".join(bin)
104 return "".join(bin)
108
105
109 def patchtext(bin):
106 def patchtext(bin):
110 pos = 0
107 pos = 0
111 t = []
108 t = []
112 while pos < len(bin):
109 while pos < len(bin):
113 p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12])
110 p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12])
114 pos += 12
111 pos += 12
115 t.append(bin[pos:pos + l])
112 t.append(bin[pos:pos + l])
116 pos += l
113 pos += l
117 return "".join(t)
114 return "".join(t)
118
115
119 def patch(a, bin):
116 def patch(a, bin):
120 return patches(a, [bin])
117 return patches(a, [bin])
118
119 textdiff = bdiff.bdiff
120
121
General Comments 0
You need to be logged in to leave comments. Login now