##// END OF EJS Templates
mdiff: fix the fix...
mpm@selenic.com -
r326:23544366 default
parent child Browse files
Show More
@@ -1,115 +1,115 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
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 lena = len(a)
47 lenb = len(b)
47 lenb = len(b)
48
48
49 while 1:
49 while 1:
50 am, bm, = la, lb
50 am, bm, = la, lb
51
51
52 # walk over matching lines
52 # walk over matching lines
53 while lb < lenb and la < lenb and a[la] == b[lb] :
53 while lb < lenb and la < lena and a[la] == b[lb] :
54 la += 1
54 la += 1
55 lb += 1
55 lb += 1
56
56
57 if la > am:
57 if la > am:
58 yield (am, bm, la - am) # return a match
58 yield (am, bm, la - am) # return a match
59
59
60 # skip mismatched lines from b
60 # skip mismatched lines from b
61 while lb < lenb and b[lb] < a[la]:
61 while lb < lenb and b[lb] < a[la]:
62 lb += 1
62 lb += 1
63
63
64 if lb >= lenb:
64 if lb >= lenb:
65 break
65 break
66
66
67 # skip mismatched lines from a
67 # skip mismatched lines from a
68 while la < lena and b[lb] > a[la]:
68 while la < lena and b[lb] > a[la]:
69 la += 1
69 la += 1
70
70
71 if la >= lena:
71 if la >= lena:
72 break
72 break
73
73
74 yield (lena, lenb, 0)
74 yield (lena, lenb, 0)
75
75
76 def diff(a, b, sorted=0):
76 def diff(a, b, sorted=0):
77 if not a:
77 if not a:
78 s = "".join(b)
78 s = "".join(b)
79 return s and (struct.pack(">lll", 0, 0, len(s)) + s)
79 return s and (struct.pack(">lll", 0, 0, len(s)) + s)
80
80
81 bin = []
81 bin = []
82 p = [0]
82 p = [0]
83 for i in a: p.append(p[-1] + len(i))
83 for i in a: p.append(p[-1] + len(i))
84
84
85 if sorted:
85 if sorted:
86 try:
86 try:
87 d = sortdiff(a, b)
87 d = sortdiff(a, b)
88 except:
88 except:
89 print a, b
89 print a, b
90 raise
90 raise
91 else:
91 else:
92 d = difflib.SequenceMatcher(None, a, b).get_matching_blocks()
92 d = difflib.SequenceMatcher(None, a, b).get_matching_blocks()
93 la = 0
93 la = 0
94 lb = 0
94 lb = 0
95 for am, bm, size in d:
95 for am, bm, size in d:
96 s = "".join(b[lb:bm])
96 s = "".join(b[lb:bm])
97 if am > la or s:
97 if am > la or s:
98 bin.append(struct.pack(">lll", p[la], p[am], len(s)) + s)
98 bin.append(struct.pack(">lll", p[la], p[am], len(s)) + s)
99 la = am + size
99 la = am + size
100 lb = bm + size
100 lb = bm + size
101
101
102 return "".join(bin)
102 return "".join(bin)
103
103
104 def patchtext(bin):
104 def patchtext(bin):
105 pos = 0
105 pos = 0
106 t = []
106 t = []
107 while pos < len(bin):
107 while pos < len(bin):
108 p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12])
108 p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12])
109 pos += 12
109 pos += 12
110 t.append(bin[pos:pos + l])
110 t.append(bin[pos:pos + l])
111 pos += l
111 pos += l
112 return "".join(t)
112 return "".join(t)
113
113
114 def patch(a, bin):
114 def patch(a, bin):
115 return patches(a, [bin])
115 return patches(a, [bin])
General Comments 0
You need to be logged in to leave comments. Login now