##// END OF EJS Templates
mdiff.py: kill #! line, add copyright notice...
mpm@selenic.com -
r239:75840796 default
parent child Browse files
Show More
@@ -1,71 +1,77 b''
1 #!/usr/bin/python
1 # mdiff.py - diff and patch routines for mercurial
2 #
3 # Copyright 2005 Matt Mackall <mpm@selenic.com>
4 #
5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference.
7
2 import difflib, struct, mmap
8 import difflib, struct, mmap
3 from mercurial.mpatch import *
9 from mercurial.mpatch import *
4
10
5 def unidiff(a, ad, b, bd, fn):
11 def unidiff(a, ad, b, bd, fn):
6 if not a and not b: return ""
12 if not a and not b: return ""
7 a = a.splitlines(1)
13 a = a.splitlines(1)
8 b = b.splitlines(1)
14 b = b.splitlines(1)
9 l = list(difflib.unified_diff(a, b, "a/" + fn, "b/" + fn, ad, bd))
15 l = list(difflib.unified_diff(a, b, "a/" + fn, "b/" + fn, ad, bd))
10
16
11 for ln in xrange(len(l)):
17 for ln in xrange(len(l)):
12 if l[ln][-1] != '\n':
18 if l[ln][-1] != '\n':
13 l[ln] += "\n\ No newline at end of file\n"
19 l[ln] += "\n\ No newline at end of file\n"
14
20
15 return "".join(l)
21 return "".join(l)
16
22
17 def textdiff(a, b):
23 def textdiff(a, b):
18 return diff(a.splitlines(1), b.splitlines(1))
24 return diff(a.splitlines(1), b.splitlines(1))
19
25
20 def sortdiff(a, b):
26 def sortdiff(a, b):
21 la = lb = 0
27 la = lb = 0
22 lena = len(a)
28 lena = len(a)
23 lenb = len(b)
29 lenb = len(b)
24 while 1:
30 while 1:
25 am, bm, = la, lb
31 am, bm, = la, lb
26 while lb < lenb and la < len and a[la] == b[lb] :
32 while lb < lenb and la < len and a[la] == b[lb] :
27 la += 1
33 la += 1
28 lb += 1
34 lb += 1
29 if la>am: yield (am, bm, la-am)
35 if la>am: yield (am, bm, la-am)
30 while lb < lenb and b[lb] < a[la]: lb += 1
36 while lb < lenb and b[lb] < a[la]: lb += 1
31 if lb>=lenb: break
37 if lb>=lenb: break
32 while la < lena and b[lb] > a[la]: la += 1
38 while la < lena and b[lb] > a[la]: la += 1
33 if la>=lena: break
39 if la>=lena: break
34 yield (lena, lenb, 0)
40 yield (lena, lenb, 0)
35
41
36 def diff(a, b, sorted=0):
42 def diff(a, b, sorted=0):
37 if not a:
43 if not a:
38 s = "".join(b)
44 s = "".join(b)
39 return s and (struct.pack(">lll", 0, 0, len(s)) + s)
45 return s and (struct.pack(">lll", 0, 0, len(s)) + s)
40
46
41 bin = []
47 bin = []
42 p = [0]
48 p = [0]
43 for i in a: p.append(p[-1] + len(i))
49 for i in a: p.append(p[-1] + len(i))
44
50
45 if sorted:
51 if sorted:
46 d = sortdiff(a, b)
52 d = sortdiff(a, b)
47 else:
53 else:
48 d = difflib.SequenceMatcher(None, a, b).get_matching_blocks()
54 d = difflib.SequenceMatcher(None, a, b).get_matching_blocks()
49 la = 0
55 la = 0
50 lb = 0
56 lb = 0
51 for am, bm, size in d:
57 for am, bm, size in d:
52 s = "".join(b[lb:bm])
58 s = "".join(b[lb:bm])
53 if am > la or s:
59 if am > la or s:
54 bin.append(struct.pack(">lll", p[la], p[am], len(s)) + s)
60 bin.append(struct.pack(">lll", p[la], p[am], len(s)) + s)
55 la = am + size
61 la = am + size
56 lb = bm + size
62 lb = bm + size
57
63
58 return "".join(bin)
64 return "".join(bin)
59
65
60 def patchtext(bin):
66 def patchtext(bin):
61 pos = 0
67 pos = 0
62 t = []
68 t = []
63 while pos < len(bin):
69 while pos < len(bin):
64 p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12])
70 p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12])
65 pos += 12
71 pos += 12
66 t.append(bin[pos:pos + l])
72 t.append(bin[pos:pos + l])
67 pos += l
73 pos += l
68 return "".join(t)
74 return "".join(t)
69
75
70 def patch(a, bin):
76 def patch(a, bin):
71 return patches(a, [bin])
77 return patches(a, [bin])
General Comments 0
You need to be logged in to leave comments. Login now