2005-06-04 01:44:34 +04:00
|
|
|
# mdiff.py - diff and patch routines for mercurial
|
|
|
|
#
|
|
|
|
# Copyright 2005 Matt Mackall <mpm@selenic.com>
|
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms
|
|
|
|
# of the GNU General Public License, incorporated herein by reference.
|
|
|
|
|
2005-10-04 22:25:48 +04:00
|
|
|
import difflib, struct, bdiff, util, mpatch
|
2005-05-04 01:16:10 +04:00
|
|
|
|
2005-08-24 06:58:46 +04:00
|
|
|
def unidiff(a, ad, b, bd, fn, r=None, text=False):
|
2005-06-17 23:37:23 +04:00
|
|
|
|
2005-05-08 04:10:50 +04:00
|
|
|
if not a and not b: return ""
|
2005-10-04 22:25:48 +04:00
|
|
|
epoch = util.datestr((0, 0))
|
2005-06-06 22:51:09 +04:00
|
|
|
|
2005-10-04 22:25:48 +04:00
|
|
|
if not text and (util.binary(a) or util.binary(b)):
|
2005-08-24 06:58:46 +04:00
|
|
|
l = ['Binary file %s has changed\n' % fn]
|
|
|
|
elif a == None:
|
2005-06-06 22:51:09 +04:00
|
|
|
b = b.splitlines(1)
|
2005-10-04 22:21:48 +04:00
|
|
|
l1 = "--- %s\t%s\n" % ("/dev/null", epoch)
|
2005-06-06 22:51:09 +04:00
|
|
|
l2 = "+++ %s\t%s\n" % ("b/" + fn, bd)
|
|
|
|
l3 = "@@ -0,0 +1,%d @@\n" % len(b)
|
|
|
|
l = [l1, l2, l3] + ["+" + e for e in b]
|
|
|
|
elif b == None:
|
|
|
|
a = a.splitlines(1)
|
|
|
|
l1 = "--- %s\t%s\n" % ("a/" + fn, ad)
|
2005-10-04 22:21:48 +04:00
|
|
|
l2 = "+++ %s\t%s\n" % ("/dev/null", epoch)
|
2005-06-06 22:51:09 +04:00
|
|
|
l3 = "@@ -1,%d +0,0 @@\n" % len(a)
|
|
|
|
l = [l1, l2, l3] + ["-" + e for e in a]
|
|
|
|
else:
|
|
|
|
a = a.splitlines(1)
|
|
|
|
b = b.splitlines(1)
|
2005-06-07 20:53:05 +04:00
|
|
|
l = list(difflib.unified_diff(a, b, "a/" + fn, "b/" + fn))
|
2005-06-08 22:47:10 +04:00
|
|
|
if not l: return ""
|
2005-06-07 20:53:05 +04:00
|
|
|
# difflib uses a space, rather than a tab
|
2005-11-14 03:30:19 +03:00
|
|
|
l[0] = "%s\t%s\n" % (l[0][:-2], ad)
|
|
|
|
l[1] = "%s\t%s\n" % (l[1][:-2], bd)
|
2005-05-27 01:02:28 +04:00
|
|
|
|
|
|
|
for ln in xrange(len(l)):
|
|
|
|
if l[ln][-1] != '\n':
|
|
|
|
l[ln] += "\n\ No newline at end of file\n"
|
|
|
|
|
2005-06-17 23:37:23 +04:00
|
|
|
if r:
|
|
|
|
l.insert(0, "diff %s %s\n" %
|
|
|
|
(' '.join(["-r %s" % rev for rev in r]), fn))
|
|
|
|
|
2005-05-04 01:16:10 +04:00
|
|
|
return "".join(l)
|
|
|
|
|
2005-05-21 05:42:29 +04:00
|
|
|
def patchtext(bin):
|
|
|
|
pos = 0
|
|
|
|
t = []
|
|
|
|
while pos < len(bin):
|
|
|
|
p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12])
|
|
|
|
pos += 12
|
|
|
|
t.append(bin[pos:pos + l])
|
|
|
|
pos += l
|
|
|
|
return "".join(t)
|
|
|
|
|
2005-05-04 01:16:10 +04:00
|
|
|
def patch(a, bin):
|
2005-10-04 22:25:48 +04:00
|
|
|
return mpatch.patches(a, [bin])
|
2005-06-22 23:23:01 +04:00
|
|
|
|
2005-10-04 22:25:48 +04:00
|
|
|
patches = mpatch.patches
|
2005-06-22 23:23:01 +04:00
|
|
|
textdiff = bdiff.bdiff
|