1
1
# (C) 2005 Matt Mackall
2
# (C) 2005 Canonical Ltd
4
# based on code by Matt Mackall, hacked by Martin Pool
6
# mm's code works line-by-line; this just works on byte strings.
7
# Possibly slower; possibly gives better results for code not
8
# regularly separated by newlines and anyhow a bit simpler.
11
3
# This program is free software; you can redistribute it and/or modify
12
4
# it under the terms of the GNU General Public License as published by
22
14
# along with this program; if not, write to the Free Software
23
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
# TODO: maybe work on files not strings?
29
17
import difflib, sys, struct
30
from cStringIO import StringIO
28
n = a.find("\n", n) + 1
33
d = difflib.SequenceMatcher(None, a, b)
35
## sys.stderr.write(' ~ real_quick_ratio: %.4f\n' % d.real_quick_ratio())
33
(al, ap) = linesplit(a)
34
(bl, bp) = linesplit(b)
36
d = difflib.SequenceMatcher(None, al, bl)
37
38
for o, m, n, s, t in d.get_opcodes():
38
39
if o == 'equal': continue
39
# a[m:n] should be replaced by b[s:t]
40
ops.append((ap[m], ap[n], "".join(bl[s:t])))
49
b.write(struct.pack(">III", f[0], f[1], len(f[2])))
47
b += struct.pack(">lll", f[0], f[1], len(f[2])) + f[2]
55
51
return tobinary(diff(a, b))
57
for p1, p2, sub in ops:
79
m, n, l = struct.unpack(">III", p)
86
raise Exception("truncated patch data")
69
m, n, l = struct.unpack(">lll", p)
70
ops.append((m, n, b[12:12 + l]))
92
76
return patch(t, frombinary(b))