~bzr-pqm/bzr/bzr.dev

198 by mbp at sourcefrog
- experimental compressed Revfile support
1
#! /usr/bin/env python
2
200 by mbp at sourcefrog
revfile: fix up __getitem__ to allow simple iteration
3
# (C) 2005 Canonical Ltd
198 by mbp at sourcefrog
- experimental compressed Revfile support
4
200 by mbp at sourcefrog
revfile: fix up __getitem__ to allow simple iteration
5
# based on an idea by Matt Mackall
198 by mbp at sourcefrog
- experimental compressed Revfile support
6
# modified to squish into bzr by Martin Pool
7
8
# This program is free software; you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation; either version 2 of the License, or
11
# (at your option) any later version.
12
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
# GNU General Public License for more details.
17
18
# You should have received a copy of the GNU General Public License
19
# along with this program; if not, write to the Free Software
20
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
22
23
"""Packed file revision storage.
24
25
A Revfile holds the text history of a particular source file, such
26
as Makefile.  It can represent a tree of text versions for that
27
file, allowing for microbranches within a single repository.
28
29
This is stored on disk as two files: an index file, and a data file.
30
The index file is short and always read completely into memory; the
31
data file is much longer and only the relevant bits of it,
32
identified by the index file, need to be read.
33
34
Each text version is identified by the SHA-1 of the full text of
35
that version.  It also has a sequence number within the file.
36
37
The index file has a short header and then a sequence of fixed-length
38
records:
39
40
* byte[20]    SHA-1 of text (as binary, not hex)
41
* uint32      sequence number this is based on, or -1 for full text
42
* uint32      flags: 1=zlib compressed
43
* uint32      offset in text file of start
44
* uint32      length of compressed delta in text file
45
* uint32[3]   reserved
46
47
total 48 bytes.
48
199 by mbp at sourcefrog
- use -1 for no_base in revfile
49
The header is also 48 bytes for tidyness and easy calculation.
198 by mbp at sourcefrog
- experimental compressed Revfile support
50
51
Both the index and the text are only ever appended to; a consequence
52
is that sequence numbers are stable references.  But not every
53
repository in the world will assign the same sequence numbers,
54
therefore the SHA-1 is the only universally unique reference.
55
56
This is meant to scale to hold 100,000 revisions of a single file, by
57
which time the index file will be ~4.8MB and a bit big to read
58
sequentially.
59
60
Some of the reserved fields could be used to implement a (semi?)
61
balanced tree indexed by SHA1 so we can much more efficiently find the
62
index associated with a particular hash.  For 100,000 revs we would be
63
able to find it in about 17 random reads, which is not too bad.
224 by mbp at sourcefrog
doc
64
65
This performs pretty well except when trying to calculate deltas of
66
really large files.  For that the main thing would be to plug in
67
something faster than difflib, which is after all pure Python.
68
Another approach is to just store the gzipped full text of big files,
69
though perhaps that's too perverse?
231 by mbp at sourcefrog
revfile doc
70
71
The iter method here will generally read through the whole index file
72
in one go.  With readahead in the kernel and python/libc (typically
73
128kB) this means that there should be no seeks and often only one
74
read() call to get everything into memory.
198 by mbp at sourcefrog
- experimental compressed Revfile support
75
"""
76
 
77
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
78
# TODO: Something like pread() would make this slightly simpler and
79
# perhaps more efficient.
80
219 by mbp at sourcefrog
todo
81
# TODO: Could also try to mmap things...  Might be faster for the
82
# index in particular?
83
84
# TODO: Some kind of faster lookup of SHAs?  The bad thing is that probably means
85
# rewriting existing records, which is not so nice.
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
86
224 by mbp at sourcefrog
doc
87
# TODO: Something to check that regions identified in the index file
88
# completely butt up and do not overlap.  Strictly it's not a problem
89
# if there are gaps and that can happen if we're interrupted while
90
# writing to the datafile.  Overlapping would be very bad though.
91
92
198 by mbp at sourcefrog
- experimental compressed Revfile support
93
94
import sys, zlib, struct, mdiff, stat, os, sha
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
95
from binascii import hexlify, unhexlify
198 by mbp at sourcefrog
- experimental compressed Revfile support
96
97
factor = 10
98
99
_RECORDSIZE = 48
100
101
_HEADER = "bzr revfile v1\n"
102
_HEADER = _HEADER + ('\xff' * (_RECORDSIZE - len(_HEADER)))
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
103
_NO_RECORD = 0xFFFFFFFFL
198 by mbp at sourcefrog
- experimental compressed Revfile support
104
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
105
# fields in the index record
106
I_SHA = 0
107
I_BASE = 1
108
I_FLAGS = 2
109
I_OFFSET = 3
110
I_LEN = 4
111
207 by mbp at sourcefrog
Revfile: compress data going into datafile if that would be worthwhile
112
FL_GZIP = 1
113
220 by mbp at sourcefrog
limit the number of chained patches
114
# maximum number of patches in a row before recording a whole text.
227 by mbp at sourcefrog
increase patch chaining limit
115
CHAIN_LIMIT = 50
220 by mbp at sourcefrog
limit the number of chained patches
116
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
117
198 by mbp at sourcefrog
- experimental compressed Revfile support
118
class RevfileError(Exception):
119
    pass
120
220 by mbp at sourcefrog
limit the number of chained patches
121
class LimitHitException(Exception):
122
    pass
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
123
198 by mbp at sourcefrog
- experimental compressed Revfile support
124
class Revfile:
229 by mbp at sourcefrog
Allow opening revision file read-only
125
    def __init__(self, basename, mode):
202 by mbp at sourcefrog
Revfile:
126
        # TODO: Lock file  while open
127
128
        # TODO: advise of random access
129
198 by mbp at sourcefrog
- experimental compressed Revfile support
130
        self.basename = basename
229 by mbp at sourcefrog
Allow opening revision file read-only
131
132
        if mode not in ['r', 'w']:
133
            raise RevfileError("invalid open mode %r" % mode)
134
        self.mode = mode
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
135
        
136
        idxname = basename + '.irev'
137
        dataname = basename + '.drev'
138
139
        idx_exists = os.path.exists(idxname)
140
        data_exists = os.path.exists(dataname)
141
142
        if idx_exists != data_exists:
143
            raise RevfileError("half-assed revfile")
144
        
145
        if not idx_exists:
229 by mbp at sourcefrog
Allow opening revision file read-only
146
            if mode == 'r':
147
                raise RevfileError("Revfile %r does not exist" % basename)
148
            
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
149
            self.idxfile = open(idxname, 'w+b')
150
            self.datafile = open(dataname, 'w+b')
151
            
198 by mbp at sourcefrog
- experimental compressed Revfile support
152
            print 'init empty file'
153
            self.idxfile.write(_HEADER)
154
            self.idxfile.flush()
155
        else:
229 by mbp at sourcefrog
Allow opening revision file read-only
156
            if mode == 'r':
157
                diskmode = 'rb'
158
            else:
159
                diskmode = 'r+b'
160
                
161
            self.idxfile = open(idxname, diskmode)
162
            self.datafile = open(dataname, diskmode)
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
163
            
198 by mbp at sourcefrog
- experimental compressed Revfile support
164
            h = self.idxfile.read(_RECORDSIZE)
165
            if h != _HEADER:
166
                raise RevfileError("bad header %r in index of %r"
167
                                   % (h, self.basename))
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
168
169
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
170
    def _check_index(self, idx):
171
        if idx < 0 or idx > len(self):
172
            raise RevfileError("invalid index %r" % idx)
173
229 by mbp at sourcefrog
Allow opening revision file read-only
174
    def _check_write(self):
175
        if self.mode != 'w':
176
            raise RevfileError("%r is open readonly" % self.basename)
177
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
178
179
    def find_sha(self, s):
180
        assert isinstance(s, str)
181
        assert len(s) == 20
182
        
183
        for idx, idxrec in enumerate(self):
184
            if idxrec[I_SHA] == s:
185
                return idx
186
        else:
217 by mbp at sourcefrog
Revfile: make compression optional, in case people are storing files they know won't compress
187
            return _NO_RECORD
188
189
190
191
    def _add_compressed(self, text_sha, data, base, compress):
192
        # well, maybe compress
193
        flags = 0
194
        if compress:
195
            data_len = len(data)
196
            if data_len > 50:
197
                # don't do compression if it's too small; it's unlikely to win
198
                # enough to be worthwhile
199
                compr_data = zlib.compress(data)
200
                compr_len = len(compr_data)
201
                if compr_len < data_len:
202
                    data = compr_data
203
                    flags = FL_GZIP
204
                    ##print '- compressed %d -> %d, %.1f%%' \
205
                    ##      % (data_len, compr_len, float(compr_len)/float(data_len) * 100.0)
206
        return self._add_raw(text_sha, data, base, flags)
207
        
208
209
210
    def _add_raw(self, text_sha, data, base, flags):
207 by mbp at sourcefrog
Revfile: compress data going into datafile if that would be worthwhile
211
        """Add pre-processed data, can be either full text or delta.
212
213
        This does the compression if that makes sense."""
203 by mbp at sourcefrog
revfile:
214
        idx = len(self)
198 by mbp at sourcefrog
- experimental compressed Revfile support
215
        self.datafile.seek(0, 2)        # to end
216
        self.idxfile.seek(0, 2)
202 by mbp at sourcefrog
Revfile:
217
        assert self.idxfile.tell() == _RECORDSIZE * (idx + 1)
198 by mbp at sourcefrog
- experimental compressed Revfile support
218
        data_offset = self.datafile.tell()
219
254 by Martin Pool
- Doc cleanups from Magnus Therning
220
        assert isinstance(data, str) # not unicode or anything weird
198 by mbp at sourcefrog
- experimental compressed Revfile support
221
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
222
        self.datafile.write(data)
198 by mbp at sourcefrog
- experimental compressed Revfile support
223
        self.datafile.flush()
224
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
225
        assert isinstance(text_sha, str)
226
        entry = text_sha
227
        entry += struct.pack(">IIII12x", base, flags, data_offset, len(data))
198 by mbp at sourcefrog
- experimental compressed Revfile support
228
        assert len(entry) == _RECORDSIZE
229
230
        self.idxfile.write(entry)
231
        self.idxfile.flush()
232
233
        return idx
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
234
        
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
235
236
220 by mbp at sourcefrog
limit the number of chained patches
237
    def _add_full_text(self, text, text_sha, compress):
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
238
        """Add a full text to the file.
239
240
        This is not compressed against any reference version.
241
242
        Returns the index for that text."""
217 by mbp at sourcefrog
Revfile: make compression optional, in case people are storing files they know won't compress
243
        return self._add_compressed(text_sha, text, _NO_RECORD, compress)
244
245
246
    def _add_delta(self, text, text_sha, base, compress):
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
247
        """Add a text stored relative to a previous text."""
248
        self._check_index(base)
220 by mbp at sourcefrog
limit the number of chained patches
249
        
250
        try:
251
            base_text = self.get(base, recursion_limit=CHAIN_LIMIT)
252
        except LimitHitException:
253
            return self._add_full_text(text, text_sha, compress)
254
        
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
255
        data = mdiff.bdiff(base_text, text)
213 by mbp at sourcefrog
Revfile: don't store deltas if they'd be larger than just storing the whole text
256
        
257
        # If the delta is larger than the text, we might as well just
258
        # store the text.  (OK, the delta might be more compressible,
259
        # but the overhead of applying it probably still makes it
214 by mbp at sourcefrog
doc
260
        # bad, and I don't want to compress both of them to find out.)
213 by mbp at sourcefrog
Revfile: don't store deltas if they'd be larger than just storing the whole text
261
        if len(data) >= len(text):
217 by mbp at sourcefrog
Revfile: make compression optional, in case people are storing files they know won't compress
262
            return self._add_full_text(text, text_sha, compress)
213 by mbp at sourcefrog
Revfile: don't store deltas if they'd be larger than just storing the whole text
263
        else:
217 by mbp at sourcefrog
Revfile: make compression optional, in case people are storing files they know won't compress
264
            return self._add_compressed(text_sha, data, base, compress)
265
266
267
    def add(self, text, base=_NO_RECORD, compress=True):
215 by mbp at sourcefrog
Doc
268
        """Add a new text to the revfile.
269
270
        If the text is already present them its existing id is
271
        returned and the file is not changed.
272
217 by mbp at sourcefrog
Revfile: make compression optional, in case people are storing files they know won't compress
273
        If compress is true then gzip compression will be used if it
274
        reduces the size.
275
215 by mbp at sourcefrog
Doc
276
        If a base index is specified, that text *may* be used for
277
        delta compression of the new text.  Delta compression will
278
        only be used if it would be a size win and if the existing
279
        base is not at too long of a delta chain already.
280
        """
229 by mbp at sourcefrog
Allow opening revision file read-only
281
        self._check_write()
282
        
206 by mbp at sourcefrog
new Revfile.add() dwim
283
        text_sha = sha.new(text).digest()
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
284
206 by mbp at sourcefrog
new Revfile.add() dwim
285
        idx = self.find_sha(text_sha)
286
        if idx != _NO_RECORD:
215 by mbp at sourcefrog
Doc
287
            # TODO: Optional paranoid mode where we read out that record and make sure
288
            # it's the same, in case someone ever breaks SHA-1.
206 by mbp at sourcefrog
new Revfile.add() dwim
289
            return idx                  # already present
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
290
        
206 by mbp at sourcefrog
new Revfile.add() dwim
291
        if base == _NO_RECORD:
217 by mbp at sourcefrog
Revfile: make compression optional, in case people are storing files they know won't compress
292
            return self._add_full_text(text, text_sha, compress)
206 by mbp at sourcefrog
new Revfile.add() dwim
293
        else:
217 by mbp at sourcefrog
Revfile: make compression optional, in case people are storing files they know won't compress
294
            return self._add_delta(text, text_sha, base, compress)
206 by mbp at sourcefrog
new Revfile.add() dwim
295
296
297
220 by mbp at sourcefrog
limit the number of chained patches
298
    def get(self, idx, recursion_limit=None):
299
        """Retrieve text of a previous revision.
300
301
        If recursion_limit is an integer then walk back at most that
302
        many revisions and then raise LimitHitException, indicating
303
        that we ought to record a new file text instead of another
304
        delta.  Don't use this when trying to get out an existing
305
        revision."""
306
        
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
307
        idxrec = self[idx]
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
308
        base = idxrec[I_BASE]
309
        if base == _NO_RECORD:
310
            text = self._get_full_text(idx, idxrec)
311
        else:
220 by mbp at sourcefrog
limit the number of chained patches
312
            text = self._get_patched(idx, idxrec, recursion_limit)
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
313
314
        if sha.new(text).digest() != idxrec[I_SHA]:
315
            raise RevfileError("corrupt SHA-1 digest on record %d"
316
                               % idx)
317
318
        return text
319
320
321
322
    def _get_raw(self, idx, idxrec):
209 by mbp at sourcefrog
Revfile: handle decompression
323
        flags = idxrec[I_FLAGS]
324
        if flags & ~FL_GZIP:
325
            raise RevfileError("unsupported index flags %#x on index %d"
326
                               % (flags, idx))
327
        
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
328
        l = idxrec[I_LEN]
329
        if l == 0:
330
            return ''
331
332
        self.datafile.seek(idxrec[I_OFFSET])
333
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
334
        data = self.datafile.read(l)
335
        if len(data) != l:
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
336
            raise RevfileError("short read %d of %d "
337
                               "getting text for record %d in %r"
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
338
                               % (len(data), l, idx, self.basename))
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
339
209 by mbp at sourcefrog
Revfile: handle decompression
340
        if flags & FL_GZIP:
341
            data = zlib.decompress(data)
342
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
343
        return data
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
344
        
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
345
346
    def _get_full_text(self, idx, idxrec):
347
        assert idxrec[I_BASE] == _NO_RECORD
348
349
        text = self._get_raw(idx, idxrec)
350
351
        return text
352
353
220 by mbp at sourcefrog
limit the number of chained patches
354
    def _get_patched(self, idx, idxrec, recursion_limit):
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
355
        base = idxrec[I_BASE]
356
        assert base >= 0
357
        assert base < idx    # no loops!
358
220 by mbp at sourcefrog
limit the number of chained patches
359
        if recursion_limit == None:
360
            sub_limit = None
361
        else:
362
            sub_limit = recursion_limit - 1
363
            if sub_limit < 0:
364
                raise LimitHitException()
365
            
366
        base_text = self.get(base, sub_limit)
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
367
        patch = self._get_raw(idx, idxrec)
368
369
        text = mdiff.bpatch(base_text, patch)
370
371
        return text
372
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
373
374
198 by mbp at sourcefrog
- experimental compressed Revfile support
375
    def __len__(self):
203 by mbp at sourcefrog
revfile:
376
        """Return number of revisions."""
377
        l = os.fstat(self.idxfile.fileno())[stat.ST_SIZE]
378
        if l % _RECORDSIZE:
379
            raise RevfileError("bad length %d on index of %r" % (l, self.basename))
380
        if l < _RECORDSIZE:
381
            raise RevfileError("no header present in index of %r" % (self.basename))
382
        return int(l / _RECORDSIZE) - 1
198 by mbp at sourcefrog
- experimental compressed Revfile support
383
200 by mbp at sourcefrog
revfile: fix up __getitem__ to allow simple iteration
384
198 by mbp at sourcefrog
- experimental compressed Revfile support
385
    def __getitem__(self, idx):
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
386
        """Index by sequence id returns the index field"""
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
387
        ## TODO: Can avoid seek if we just moved there...
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
388
        self._seek_index(idx)
230 by mbp at sourcefrog
Revfile: better __iter__ method that reads the whole index file in one go!
389
        idxrec = self._read_next_index()
390
        if idxrec == None:
391
            raise IndexError()
392
        else:
393
            return idxrec
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
394
395
396
    def _seek_index(self, idx):
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
397
        if idx < 0:
398
            raise RevfileError("invalid index %r" % idx)
198 by mbp at sourcefrog
- experimental compressed Revfile support
399
        self.idxfile.seek((idx + 1) * _RECORDSIZE)
230 by mbp at sourcefrog
Revfile: better __iter__ method that reads the whole index file in one go!
400
401
402
403
    def __iter__(self):
404
        """Read back all index records.
405
406
        Do not seek the index file while this is underway!"""
407
        sys.stderr.write(" ** iter called ** \n")
408
        self._seek_index(0)
409
        while True:
410
            idxrec = self._read_next_index()
411
            if not idxrec:
412
                break
413
            yield idxrec
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
414
        
415
416
    def _read_next_index(self):
198 by mbp at sourcefrog
- experimental compressed Revfile support
417
        rec = self.idxfile.read(_RECORDSIZE)
200 by mbp at sourcefrog
revfile: fix up __getitem__ to allow simple iteration
418
        if not rec:
230 by mbp at sourcefrog
Revfile: better __iter__ method that reads the whole index file in one go!
419
            return None
200 by mbp at sourcefrog
revfile: fix up __getitem__ to allow simple iteration
420
        elif len(rec) != _RECORDSIZE:
198 by mbp at sourcefrog
- experimental compressed Revfile support
421
            raise RevfileError("short read of %d bytes getting index %d from %r"
422
                               % (len(rec), idx, self.basename))
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
423
        
199 by mbp at sourcefrog
- use -1 for no_base in revfile
424
        return struct.unpack(">20sIIII12x", rec)
198 by mbp at sourcefrog
- experimental compressed Revfile support
425
426
        
199 by mbp at sourcefrog
- use -1 for no_base in revfile
427
    def dump(self, f=sys.stdout):
428
        f.write('%-8s %-40s %-8s %-8s %-8s %-8s\n' 
429
                % tuple('idx sha1 base flags offset len'.split()))
430
        f.write('-------- ---------------------------------------- ')
431
        f.write('-------- -------- -------- --------\n')
432
200 by mbp at sourcefrog
revfile: fix up __getitem__ to allow simple iteration
433
        for i, rec in enumerate(self):
199 by mbp at sourcefrog
- use -1 for no_base in revfile
434
            f.write("#%-7d %40s " % (i, hexlify(rec[0])))
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
435
            if rec[1] == _NO_RECORD:
199 by mbp at sourcefrog
- use -1 for no_base in revfile
436
                f.write("(none)   ")
437
            else:
438
                f.write("#%-7d " % rec[1])
439
                
440
            f.write("%8x %8d %8d\n" % (rec[2], rec[3], rec[4]))
222 by mbp at sourcefrog
refactor total_text_size
441
223 by mbp at sourcefrog
doc
442
222 by mbp at sourcefrog
refactor total_text_size
443
    def total_text_size(self):
223 by mbp at sourcefrog
doc
444
        """Return the sum of sizes of all file texts.
445
446
        This is how much space they would occupy if they were stored without
447
        delta and gzip compression.
448
449
        As a side effect this completely validates the Revfile, checking that all
450
        texts can be reproduced with the correct SHA-1."""
222 by mbp at sourcefrog
refactor total_text_size
451
        t = 0L
452
        for idx in range(len(self)):
453
            t += len(self.get(idx))
454
        return t
198 by mbp at sourcefrog
- experimental compressed Revfile support
455
        
456
457
458
def main(argv):
203 by mbp at sourcefrog
revfile:
459
    try:
460
        cmd = argv[1]
461
    except IndexError:
198 by mbp at sourcefrog
- experimental compressed Revfile support
462
        sys.stderr.write("usage: revfile dump\n"
201 by mbp at sourcefrog
Revfile: - get full text from a record- fix creation of files if they don't exist- protect against half-assed storage
463
                         "       revfile add\n"
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
464
                         "       revfile add-delta BASE\n"
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
465
                         "       revfile get IDX\n"
221 by mbp at sourcefrog
Revfile: new command total-text-size
466
                         "       revfile find-sha HEX\n"
226 by mbp at sourcefrog
revf: new command 'last'
467
                         "       revfile total-text-size\n"
468
                         "       revfile last\n")
203 by mbp at sourcefrog
revfile:
469
        return 1
218 by mbp at sourcefrog
todo
470
229 by mbp at sourcefrog
Allow opening revision file read-only
471
    def rw():
472
        return Revfile('testrev', 'w')
473
474
    def ro():
475
        return Revfile('testrev', 'r')
476
203 by mbp at sourcefrog
revfile:
477
    if cmd == 'add':
229 by mbp at sourcefrog
Allow opening revision file read-only
478
        print rw().add(sys.stdin.read())
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
479
    elif cmd == 'add-delta':
229 by mbp at sourcefrog
Allow opening revision file read-only
480
        print rw().add(sys.stdin.read(), int(argv[2]))
203 by mbp at sourcefrog
revfile:
481
    elif cmd == 'dump':
229 by mbp at sourcefrog
Allow opening revision file read-only
482
        ro().dump()
203 by mbp at sourcefrog
revfile:
483
    elif cmd == 'get':
202 by mbp at sourcefrog
Revfile:
484
        try:
203 by mbp at sourcefrog
revfile:
485
            idx = int(argv[2])
202 by mbp at sourcefrog
Revfile:
486
        except IndexError:
203 by mbp at sourcefrog
revfile:
487
            sys.stderr.write("usage: revfile get IDX\n")
488
            return 1
489
490
        if idx < 0 or idx >= len(r):
491
            sys.stderr.write("invalid index %r\n" % idx)
492
            return 1
493
229 by mbp at sourcefrog
Allow opening revision file read-only
494
        sys.stdout.write(ro().get(idx))
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
495
    elif cmd == 'find-sha':
496
        try:
497
            s = unhexlify(argv[2])
498
        except IndexError:
499
            sys.stderr.write("usage: revfile find-sha HEX\n")
500
            return 1
501
229 by mbp at sourcefrog
Allow opening revision file read-only
502
        idx = ro().find_sha(s)
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
503
        if idx == _NO_RECORD:
504
            sys.stderr.write("no such record\n")
505
            return 1
506
        else:
507
            print idx
221 by mbp at sourcefrog
Revfile: new command total-text-size
508
    elif cmd == 'total-text-size':
229 by mbp at sourcefrog
Allow opening revision file read-only
509
        print ro().total_text_size()
226 by mbp at sourcefrog
revf: new command 'last'
510
    elif cmd == 'last':
229 by mbp at sourcefrog
Allow opening revision file read-only
511
        print len(ro())-1
198 by mbp at sourcefrog
- experimental compressed Revfile support
512
    else:
203 by mbp at sourcefrog
revfile:
513
        sys.stderr.write("unknown command %r\n" % cmd)
514
        return 1
198 by mbp at sourcefrog
- experimental compressed Revfile support
515
    
516
517
if __name__ == '__main__':
518
    import sys
203 by mbp at sourcefrog
revfile:
519
    sys.exit(main(sys.argv) or 0)