/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
198 by mbp at sourcefrog
- experimental compressed Revfile support
1
# (C) 2005 Matt Mackall
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
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
198 by mbp at sourcefrog
- experimental compressed Revfile support
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
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
25
26
# TODO: maybe work on files not strings?
27
28
198 by mbp at sourcefrog
- experimental compressed Revfile support
29
import difflib, sys, struct
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
30
from cStringIO import StringIO
198 by mbp at sourcefrog
- experimental compressed Revfile support
31
32
def diff(a, b):
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
33
    d = difflib.SequenceMatcher(None, a, b)
198 by mbp at sourcefrog
- experimental compressed Revfile support
34
    for o, m, n, s, t in d.get_opcodes():
35
        if o == 'equal': continue
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
36
        # a[m:n] should be replaced by b[s:t]
37
        if s == t:
38
            yield m, n, ''
39
        else:
40
            yield m, n, b[s:t]
198 by mbp at sourcefrog
- experimental compressed Revfile support
41
42
43
def tobinary(ops):
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
44
    b = StringIO()
198 by mbp at sourcefrog
- experimental compressed Revfile support
45
    for f in ops:
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
46
        b.write(struct.pack(">III", f[0], f[1], len(f[2])))
47
        b.write(f[2])
48
    return b.getvalue()
49
198 by mbp at sourcefrog
- experimental compressed Revfile support
50
51
def bdiff(a, b):
52
    return tobinary(diff(a, b))
53
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
54
198 by mbp at sourcefrog
- experimental compressed Revfile support
55
def patch(t, ops):
56
    last = 0
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
57
    b = StringIO()
58
59
    for m, n, r in ops:
60
        b.write(t[last:m])
61
        if r:
62
            b.write(r)
63
        last = n
64
        
65
    b.write(t[last:])
66
    return b.getvalue()
67
198 by mbp at sourcefrog
- experimental compressed Revfile support
68
69
def frombinary(b):
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
70
    bin = StringIO(b)
71
    while True:
72
        p = bin.read(12)
73
        if not p:
74
            break
75
76
        m, n, l = struct.unpack(">III", p)
77
        
78
        if l == 0:
79
            r = ''
80
        else:
81
            r = bin.read(l)
82
            if len(r) != l:
83
                raise Exception("truncated patch data")
84
            
85
        yield m, n, r
86
198 by mbp at sourcefrog
- experimental compressed Revfile support
87
88
def bpatch(t, b):
89
    return patch(t, frombinary(b))
90
91
92
93