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