~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/mdiff.py

  • Committer: Martin Pool
  • Date: 2005-08-12 15:41:44 UTC
  • Revision ID: mbp@sourcefrog.net-20050812154144-bc98570a78b8f633
- merge in deferred revfile work

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# (C) 2005 Matt Mackall
 
2
# (C) 2005 Canonical Ltd
 
3
 
 
4
# based on code by Matt Mackall, hacked by Martin Pool
 
5
 
 
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.
 
9
 
 
10
 
 
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.
 
15
 
 
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.
 
20
 
 
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
 
24
 
 
25
 
 
26
# TODO: maybe work on files not strings?
 
27
 
 
28
 
 
29
import difflib, sys, struct
 
30
from cStringIO import StringIO
 
31
 
 
32
def linesplit(a):
 
33
    """Split into two lists: content and line positions"""
 
34
    al, ap = [], []
 
35
    last = 0
 
36
 
 
37
    n = a.find("\n") + 1
 
38
    while n > 0:
 
39
        ap.append(last)
 
40
        al.append(a[last:n])
 
41
        last = n
 
42
        n = a.find("\n", n) + 1
 
43
 
 
44
    # position at the end
 
45
    ap.append(len(a))
 
46
 
 
47
    return (al, ap)
 
48
 
 
49
 
 
50
def diff(a, b):
 
51
    # TODO: Use different splits, perhaps rsync-like, for binary files?
 
52
    
 
53
    (al, ap) = linesplit(a)
 
54
    (bl, bp) = linesplit(b)
 
55
 
 
56
    d = difflib.SequenceMatcher(None, al, bl)
 
57
    
 
58
    ## sys.stderr.write('  ~ real_quick_ratio: %.4f\n' % d.real_quick_ratio())
 
59
    
 
60
    for o, m, n, s, t in d.get_opcodes():
 
61
        if o == 'equal': continue
 
62
        # a[m:n] should be replaced by b[s:t]
 
63
        if s == t:
 
64
            yield ap[m], ap[n], ''
 
65
        else:
 
66
            yield ap[m], ap[n], ''.join(bl[s:t])
 
67
 
 
68
 
 
69
def tobinary(ops):
 
70
    b = StringIO()
 
71
    for f in ops:
 
72
        b.write(struct.pack(">III", f[0], f[1], len(f[2])))
 
73
        b.write(f[2])
 
74
    return b.getvalue()
 
75
 
 
76
 
 
77
def bdiff(a, b):
 
78
    return tobinary(diff(a, b))
 
79
 
 
80
 
 
81
def patch(t, ops):
 
82
    last = 0
 
83
    b = StringIO()
 
84
 
 
85
    for m, n, r in ops:
 
86
        b.write(t[last:m])
 
87
        if r:
 
88
            b.write(r)
 
89
        last = n
 
90
        
 
91
    b.write(t[last:])
 
92
    return b.getvalue()
 
93
 
 
94
 
 
95
def frombinary(b):
 
96
    bin = StringIO(b)
 
97
    while True:
 
98
        p = bin.read(12)
 
99
        if not p:
 
100
            break
 
101
 
 
102
        m, n, l = struct.unpack(">III", p)
 
103
        
 
104
        if l == 0:
 
105
            r = ''
 
106
        else:
 
107
            r = bin.read(l)
 
108
            if len(r) != l:
 
109
                raise Exception("truncated patch data")
 
110
            
 
111
        yield m, n, r
 
112
 
 
113
 
 
114
def bpatch(t, b):
 
115
    return patch(t, frombinary(b))
 
116
 
 
117
 
 
118
 
 
119