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