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
# This program is free software; you can redistribute it and/or modify
 
 
12
# it under the terms of the GNU General Public License as published by
 
 
13
# the Free Software Foundation; either version 2 of the License, or
 
 
14
# (at your option) any later version.
 
 
16
# This program is distributed in the hope that it will be useful,
 
 
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
19
# GNU General Public License for more details.
 
 
21
# You should have received a copy of the GNU General Public License
 
 
22
# along with this program; if not, write to the Free Software
 
 
23
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
26
# TODO: maybe work on files not strings?
 
 
28
# FIXME: doesn't work properly on files without trailing newlines
 
 
34
from cStringIO import StringIO
 
 
38
    """Split into two lists: content and line positions.
 
 
40
    This returns (al, ap).
 
 
42
    al[i] is the string content of line i of the file, including its
 
 
45
    ap[i] is the byte position in the file where that line starts.
 
 
47
    ap[-1] is the byte position of the end of the file (i.e. the
 
 
50
    This transformation allows us to do a line-based diff and then map
 
 
51
    back to byte positions.
 
 
62
        n = a.find("\n", n) + 1
 
 
75
    # TODO: Use different splits, perhaps rsync-like, for binary files?
 
 
77
    (al, ap) = linesplit(a)
 
 
78
    (bl, bp) = linesplit(b)
 
 
80
    d = difflib.SequenceMatcher(None, al, bl)
 
 
82
    ## sys.stderr.write('  ~ real_quick_ratio: %.4f\n' % d.real_quick_ratio())
 
 
84
    for o, m, n, s, t in d.get_opcodes():
 
 
85
        if o == 'equal': continue
 
 
86
        # a[m:n] should be replaced by b[s:t]
 
 
88
            yield ap[m], ap[n], ''
 
 
90
            yield ap[m], ap[n], ''.join(bl[s:t])
 
 
96
        b.write(struct.pack(">III", f[0], f[1], len(f[2])))
 
 
102
    return tobinary(diff(a, b))
 
 
126
        m, n, l = struct.unpack(">III", p)
 
 
133
                raise Exception("truncated patch data")
 
 
139
    return patch(t, frombinary(b))
 
 
144
class TestDiffPatch(unittest.TestCase):
 
 
145
    def doDiffPatch(self, old, new):
 
 
146
        diff = bdiff(old, new)
 
 
147
        result = bpatch(old, diff)
 
 
148
        self.assertEquals(new, result)
 
 
151
    def testSimpleDiff(self):
 
 
152
        """Simply add a line at the end"""
 
 
153
        self.doDiffPatch('a\nb\n', 'a\nb\nc\n')
 
 
157
    def testTrailingLine(self):
 
 
158
        """Test diff that adds an unterminated line.
 
 
160
        (Old versions didn't do this properly.)"""
 
 
161
        self.doDiffPatch('a\nb\nc\n',
 
 
165
if __name__ == '__main__':