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