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