bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
198
by mbp at sourcefrog
- experimental compressed Revfile support |
1 |
# (C) 2005 Matt Mackall
|
2 |
||
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
||
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
||
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
import difflib, sys, struct |
|
18 |
||
19 |
def linesplit(a): |
|
20 |
al, ap = [], [] |
|
21 |
last = 0 |
|
22 |
||
23 |
n = a.find("\n") + 1 |
|
24 |
while n > 0: |
|
25 |
ap.append(last) |
|
26 |
al.append(a[last:n]) |
|
27 |
last = n |
|
28 |
n = a.find("\n", n) + 1 |
|
29 |
||
30 |
return (al, ap) |
|
31 |
||
32 |
def diff(a, b): |
|
33 |
(al, ap) = linesplit(a) |
|
34 |
(bl, bp) = linesplit(b) |
|
35 |
||
36 |
d = difflib.SequenceMatcher(None, al, bl) |
|
37 |
ops = [] |
|
38 |
for o, m, n, s, t in d.get_opcodes(): |
|
39 |
if o == 'equal': continue |
|
40 |
ops.append((ap[m], ap[n], "".join(bl[s:t]))) |
|
41 |
||
42 |
return ops |
|
43 |
||
44 |
def tobinary(ops): |
|
45 |
b = "" |
|
46 |
for f in ops: |
|
47 |
b += struct.pack(">lll", f[0], f[1], len(f[2])) + f[2] |
|
48 |
return b |
|
49 |
||
50 |
def bdiff(a, b): |
|
51 |
return tobinary(diff(a, b)) |
|
52 |
||
53 |
def patch(t, ops): |
|
54 |
last = 0 |
|
55 |
r = [] |
|
56 |
||
57 |
for p1, p2, sub in ops: |
|
58 |
r.append(t[last:p1]) |
|
59 |
r.append(sub) |
|
60 |
last = p2 |
|
61 |
||
62 |
r.append(t[last:]) |
|
63 |
return "".join(r) |
|
64 |
||
65 |
def frombinary(b): |
|
66 |
ops = [] |
|
67 |
while b: |
|
68 |
p = b[:12] |
|
69 |
m, n, l = struct.unpack(">lll", p) |
|
70 |
ops.append((m, n, b[12:12 + l])) |
|
71 |
b = b[12 + l:] |
|
72 |
||
73 |
return ops |
|
74 |
||
75 |
def bpatch(t, b): |
|
76 |
return patch(t, frombinary(b)) |
|
77 |
||
78 |
||
79 |
||
80 |