/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
#! /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.
64
"""
65
 
66
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
67
# TODO: Something like pread() would make this slightly simpler and
68
# perhaps more efficient.
69
219 by mbp at sourcefrog
todo
70
# TODO: Could also try to mmap things...  Might be faster for the
71
# index in particular?
72
73
# TODO: Some kind of faster lookup of SHAs?  The bad thing is that probably means
74
# 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
75
198 by mbp at sourcefrog
- experimental compressed Revfile support
76
77
import sys, zlib, struct, mdiff, stat, os, sha
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
78
from binascii import hexlify, unhexlify
198 by mbp at sourcefrog
- experimental compressed Revfile support
79
80
factor = 10
81
82
_RECORDSIZE = 48
83
84
_HEADER = "bzr revfile v1\n"
85
_HEADER = _HEADER + ('\xff' * (_RECORDSIZE - len(_HEADER)))
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
86
_NO_RECORD = 0xFFFFFFFFL
198 by mbp at sourcefrog
- experimental compressed Revfile support
87
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
88
# fields in the index record
89
I_SHA = 0
90
I_BASE = 1
91
I_FLAGS = 2
92
I_OFFSET = 3
93
I_LEN = 4
94
207 by mbp at sourcefrog
Revfile: compress data going into datafile if that would be worthwhile
95
FL_GZIP = 1
96
220 by mbp at sourcefrog
limit the number of chained patches
97
# maximum number of patches in a row before recording a whole text.
98
# intentionally pretty low for testing purposes.
99
CHAIN_LIMIT = 2
100
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
101
198 by mbp at sourcefrog
- experimental compressed Revfile support
102
class RevfileError(Exception):
103
    pass
104
220 by mbp at sourcefrog
limit the number of chained patches
105
class LimitHitException(Exception):
106
    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
107
198 by mbp at sourcefrog
- experimental compressed Revfile support
108
class Revfile:
109
    def __init__(self, basename):
202 by mbp at sourcefrog
Revfile:
110
        # TODO: Option to open readonly
111
112
        # TODO: Lock file  while open
113
114
        # TODO: advise of random access
115
198 by mbp at sourcefrog
- experimental compressed Revfile support
116
        self.basename = 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
117
        
118
        idxname = basename + '.irev'
119
        dataname = basename + '.drev'
120
121
        idx_exists = os.path.exists(idxname)
122
        data_exists = os.path.exists(dataname)
123
124
        if idx_exists != data_exists:
125
            raise RevfileError("half-assed revfile")
126
        
127
        if not idx_exists:
128
            self.idxfile = open(idxname, 'w+b')
129
            self.datafile = open(dataname, 'w+b')
130
            
198 by mbp at sourcefrog
- experimental compressed Revfile support
131
            print 'init empty file'
132
            self.idxfile.write(_HEADER)
133
            self.idxfile.flush()
134
        else:
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
            self.idxfile = open(idxname, 'r+b')
202 by mbp at sourcefrog
Revfile:
136
            self.datafile = open(dataname, 'r+b')
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
137
            
198 by mbp at sourcefrog
- experimental compressed Revfile support
138
            h = self.idxfile.read(_RECORDSIZE)
139
            if h != _HEADER:
140
                raise RevfileError("bad header %r in index of %r"
141
                                   % (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
142
143
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
144
    def _check_index(self, idx):
145
        if idx < 0 or idx > len(self):
146
            raise RevfileError("invalid index %r" % idx)
147
148
149
    def find_sha(self, s):
150
        assert isinstance(s, str)
151
        assert len(s) == 20
152
        
153
        for idx, idxrec in enumerate(self):
154
            if idxrec[I_SHA] == s:
155
                return idx
156
        else:
217 by mbp at sourcefrog
Revfile: make compression optional, in case people are storing files they know won't compress
157
            return _NO_RECORD
158
159
160
161
    def _add_compressed(self, text_sha, data, base, compress):
162
        # well, maybe compress
163
        flags = 0
164
        if compress:
165
            data_len = len(data)
166
            if data_len > 50:
167
                # don't do compression if it's too small; it's unlikely to win
168
                # enough to be worthwhile
169
                compr_data = zlib.compress(data)
170
                compr_len = len(compr_data)
171
                if compr_len < data_len:
172
                    data = compr_data
173
                    flags = FL_GZIP
174
                    ##print '- compressed %d -> %d, %.1f%%' \
175
                    ##      % (data_len, compr_len, float(compr_len)/float(data_len) * 100.0)
176
        return self._add_raw(text_sha, data, base, flags)
177
        
178
179
180
    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
181
        """Add pre-processed data, can be either full text or delta.
182
183
        This does the compression if that makes sense."""
203 by mbp at sourcefrog
revfile:
184
        idx = len(self)
198 by mbp at sourcefrog
- experimental compressed Revfile support
185
        self.datafile.seek(0, 2)        # to end
186
        self.idxfile.seek(0, 2)
202 by mbp at sourcefrog
Revfile:
187
        assert self.idxfile.tell() == _RECORDSIZE * (idx + 1)
198 by mbp at sourcefrog
- experimental compressed Revfile support
188
        data_offset = self.datafile.tell()
189
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
190
        assert isinstance(data, str) # not unicode or anything wierd
198 by mbp at sourcefrog
- experimental compressed Revfile support
191
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
192
        self.datafile.write(data)
198 by mbp at sourcefrog
- experimental compressed Revfile support
193
        self.datafile.flush()
194
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
195
        assert isinstance(text_sha, str)
196
        entry = text_sha
197
        entry += struct.pack(">IIII12x", base, flags, data_offset, len(data))
198 by mbp at sourcefrog
- experimental compressed Revfile support
198
        assert len(entry) == _RECORDSIZE
199
200
        self.idxfile.write(entry)
201
        self.idxfile.flush()
202
203
        return idx
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
204
        
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
205
206
220 by mbp at sourcefrog
limit the number of chained patches
207
    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
208
        """Add a full text to the file.
209
210
        This is not compressed against any reference version.
211
212
        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
213
        return self._add_compressed(text_sha, text, _NO_RECORD, compress)
214
215
216
    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
217
        """Add a text stored relative to a previous text."""
218
        self._check_index(base)
220 by mbp at sourcefrog
limit the number of chained patches
219
        
220
        try:
221
            base_text = self.get(base, recursion_limit=CHAIN_LIMIT)
222
        except LimitHitException:
223
            return self._add_full_text(text, text_sha, compress)
224
        
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
225
        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
226
        
227
        # If the delta is larger than the text, we might as well just
228
        # store the text.  (OK, the delta might be more compressible,
229
        # but the overhead of applying it probably still makes it
214 by mbp at sourcefrog
doc
230
        # 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
231
        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
232
            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
233
        else:
217 by mbp at sourcefrog
Revfile: make compression optional, in case people are storing files they know won't compress
234
            return self._add_compressed(text_sha, data, base, compress)
235
236
237
    def add(self, text, base=_NO_RECORD, compress=True):
215 by mbp at sourcefrog
Doc
238
        """Add a new text to the revfile.
239
240
        If the text is already present them its existing id is
241
        returned and the file is not changed.
242
217 by mbp at sourcefrog
Revfile: make compression optional, in case people are storing files they know won't compress
243
        If compress is true then gzip compression will be used if it
244
        reduces the size.
245
215 by mbp at sourcefrog
Doc
246
        If a base index is specified, that text *may* be used for
247
        delta compression of the new text.  Delta compression will
248
        only be used if it would be a size win and if the existing
249
        base is not at too long of a delta chain already.
250
        """
206 by mbp at sourcefrog
new Revfile.add() dwim
251
        text_sha = sha.new(text).digest()
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
252
206 by mbp at sourcefrog
new Revfile.add() dwim
253
        idx = self.find_sha(text_sha)
254
        if idx != _NO_RECORD:
215 by mbp at sourcefrog
Doc
255
            # TODO: Optional paranoid mode where we read out that record and make sure
256
            # it's the same, in case someone ever breaks SHA-1.
206 by mbp at sourcefrog
new Revfile.add() dwim
257
            return idx                  # already present
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
258
        
206 by mbp at sourcefrog
new Revfile.add() dwim
259
        if base == _NO_RECORD:
217 by mbp at sourcefrog
Revfile: make compression optional, in case people are storing files they know won't compress
260
            return self._add_full_text(text, text_sha, compress)
206 by mbp at sourcefrog
new Revfile.add() dwim
261
        else:
217 by mbp at sourcefrog
Revfile: make compression optional, in case people are storing files they know won't compress
262
            return self._add_delta(text, text_sha, base, compress)
206 by mbp at sourcefrog
new Revfile.add() dwim
263
264
265
220 by mbp at sourcefrog
limit the number of chained patches
266
    def get(self, idx, recursion_limit=None):
267
        """Retrieve text of a previous revision.
268
269
        If recursion_limit is an integer then walk back at most that
270
        many revisions and then raise LimitHitException, indicating
271
        that we ought to record a new file text instead of another
272
        delta.  Don't use this when trying to get out an existing
273
        revision."""
274
        
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
275
        idxrec = self[idx]
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
276
        base = idxrec[I_BASE]
277
        if base == _NO_RECORD:
278
            text = self._get_full_text(idx, idxrec)
279
        else:
220 by mbp at sourcefrog
limit the number of chained patches
280
            text = self._get_patched(idx, idxrec, recursion_limit)
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
281
282
        if sha.new(text).digest() != idxrec[I_SHA]:
283
            raise RevfileError("corrupt SHA-1 digest on record %d"
284
                               % idx)
285
286
        return text
287
288
289
290
    def _get_raw(self, idx, idxrec):
209 by mbp at sourcefrog
Revfile: handle decompression
291
        flags = idxrec[I_FLAGS]
292
        if flags & ~FL_GZIP:
293
            raise RevfileError("unsupported index flags %#x on index %d"
294
                               % (flags, idx))
295
        
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
296
        l = idxrec[I_LEN]
297
        if l == 0:
298
            return ''
299
300
        self.datafile.seek(idxrec[I_OFFSET])
301
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
302
        data = self.datafile.read(l)
303
        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
304
            raise RevfileError("short read %d of %d "
305
                               "getting text for record %d in %r"
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
306
                               % (len(data), l, idx, self.basename))
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
307
209 by mbp at sourcefrog
Revfile: handle decompression
308
        if flags & FL_GZIP:
309
            data = zlib.decompress(data)
310
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
311
        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
312
        
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
313
314
    def _get_full_text(self, idx, idxrec):
315
        assert idxrec[I_BASE] == _NO_RECORD
316
317
        text = self._get_raw(idx, idxrec)
318
319
        return text
320
321
220 by mbp at sourcefrog
limit the number of chained patches
322
    def _get_patched(self, idx, idxrec, recursion_limit):
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
323
        base = idxrec[I_BASE]
324
        assert base >= 0
325
        assert base < idx    # no loops!
326
220 by mbp at sourcefrog
limit the number of chained patches
327
        if recursion_limit == None:
328
            sub_limit = None
329
        else:
330
            sub_limit = recursion_limit - 1
331
            if sub_limit < 0:
332
                raise LimitHitException()
333
            
334
        base_text = self.get(base, sub_limit)
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
335
        patch = self._get_raw(idx, idxrec)
336
337
        text = mdiff.bpatch(base_text, patch)
338
339
        return text
340
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
341
342
198 by mbp at sourcefrog
- experimental compressed Revfile support
343
    def __len__(self):
203 by mbp at sourcefrog
revfile:
344
        """Return number of revisions."""
345
        l = os.fstat(self.idxfile.fileno())[stat.ST_SIZE]
346
        if l % _RECORDSIZE:
347
            raise RevfileError("bad length %d on index of %r" % (l, self.basename))
348
        if l < _RECORDSIZE:
349
            raise RevfileError("no header present in index of %r" % (self.basename))
350
        return int(l / _RECORDSIZE) - 1
198 by mbp at sourcefrog
- experimental compressed Revfile support
351
200 by mbp at sourcefrog
revfile: fix up __getitem__ to allow simple iteration
352
198 by mbp at sourcefrog
- experimental compressed Revfile support
353
    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
354
        """Index by sequence id returns the index field"""
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
355
        ## 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
356
        self._seek_index(idx)
357
        return self._read_next_index()
358
359
360
    def _seek_index(self, idx):
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
361
        if idx < 0:
362
            raise RevfileError("invalid index %r" % idx)
198 by mbp at sourcefrog
- experimental compressed Revfile support
363
        self.idxfile.seek((idx + 1) * _RECORDSIZE)
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
364
        
365
366
    def _read_next_index(self):
198 by mbp at sourcefrog
- experimental compressed Revfile support
367
        rec = self.idxfile.read(_RECORDSIZE)
200 by mbp at sourcefrog
revfile: fix up __getitem__ to allow simple iteration
368
        if not rec:
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
369
            raise IndexError("end of index file")
200 by mbp at sourcefrog
revfile: fix up __getitem__ to allow simple iteration
370
        elif len(rec) != _RECORDSIZE:
198 by mbp at sourcefrog
- experimental compressed Revfile support
371
            raise RevfileError("short read of %d bytes getting index %d from %r"
372
                               % (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
373
        
199 by mbp at sourcefrog
- use -1 for no_base in revfile
374
        return struct.unpack(">20sIIII12x", rec)
198 by mbp at sourcefrog
- experimental compressed Revfile support
375
376
        
199 by mbp at sourcefrog
- use -1 for no_base in revfile
377
    def dump(self, f=sys.stdout):
378
        f.write('%-8s %-40s %-8s %-8s %-8s %-8s\n' 
379
                % tuple('idx sha1 base flags offset len'.split()))
380
        f.write('-------- ---------------------------------------- ')
381
        f.write('-------- -------- -------- --------\n')
382
200 by mbp at sourcefrog
revfile: fix up __getitem__ to allow simple iteration
383
        for i, rec in enumerate(self):
199 by mbp at sourcefrog
- use -1 for no_base in revfile
384
            f.write("#%-7d %40s " % (i, hexlify(rec[0])))
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
385
            if rec[1] == _NO_RECORD:
199 by mbp at sourcefrog
- use -1 for no_base in revfile
386
                f.write("(none)   ")
387
            else:
388
                f.write("#%-7d " % rec[1])
389
                
390
            f.write("%8x %8d %8d\n" % (rec[2], rec[3], rec[4]))
198 by mbp at sourcefrog
- experimental compressed Revfile support
391
        
392
393
394
def main(argv):
395
    r = Revfile("testrev")
203 by mbp at sourcefrog
revfile:
396
397
    try:
398
        cmd = argv[1]
399
    except IndexError:
198 by mbp at sourcefrog
- experimental compressed Revfile support
400
        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
401
                         "       revfile add\n"
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
402
                         "       revfile add-delta BASE\n"
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
403
                         "       revfile get IDX\n"
404
                         "       revfile find-sha HEX\n")
203 by mbp at sourcefrog
revfile:
405
        return 1
218 by mbp at sourcefrog
todo
406
407
408
    # TODO: Something to show compression ratio, e.g. total size of
409
    # all files vs size of index and data files.
198 by mbp at sourcefrog
- experimental compressed Revfile support
410
        
203 by mbp at sourcefrog
revfile:
411
412
    if cmd == 'add':
206 by mbp at sourcefrog
new Revfile.add() dwim
413
        new_idx = r.add(sys.stdin.read())
216 by mbp at sourcefrog
revfile add and add-delta commands print just the index for use by scripts
414
        print new_idx
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
415
    elif cmd == 'add-delta':
207 by mbp at sourcefrog
Revfile: compress data going into datafile if that would be worthwhile
416
        new_idx = r.add(sys.stdin.read(), int(argv[2]))
216 by mbp at sourcefrog
revfile add and add-delta commands print just the index for use by scripts
417
        print new_idx
203 by mbp at sourcefrog
revfile:
418
    elif cmd == 'dump':
198 by mbp at sourcefrog
- experimental compressed Revfile support
419
        r.dump()
203 by mbp at sourcefrog
revfile:
420
    elif cmd == 'get':
202 by mbp at sourcefrog
Revfile:
421
        try:
203 by mbp at sourcefrog
revfile:
422
            idx = int(argv[2])
202 by mbp at sourcefrog
Revfile:
423
        except IndexError:
203 by mbp at sourcefrog
revfile:
424
            sys.stderr.write("usage: revfile get IDX\n")
425
            return 1
426
427
        if idx < 0 or idx >= len(r):
428
            sys.stderr.write("invalid index %r\n" % idx)
429
            return 1
430
205 by mbp at sourcefrog
Revfile:- store and retrieve deltas!mdiff:- work on bytes not lines
431
        sys.stdout.write(r.get(idx))
204 by mbp at sourcefrog
Revfile:- new find-sha command and implementation- new _check_index helper
432
    elif cmd == 'find-sha':
433
        try:
434
            s = unhexlify(argv[2])
435
        except IndexError:
436
            sys.stderr.write("usage: revfile find-sha HEX\n")
437
            return 1
438
439
        idx = r.find_sha(s)
440
        if idx == _NO_RECORD:
441
            sys.stderr.write("no such record\n")
442
            return 1
443
        else:
444
            print idx
445
            
198 by mbp at sourcefrog
- experimental compressed Revfile support
446
    else:
203 by mbp at sourcefrog
revfile:
447
        sys.stderr.write("unknown command %r\n" % cmd)
448
        return 1
198 by mbp at sourcefrog
- experimental compressed Revfile support
449
    
450
451
if __name__ == '__main__':
452
    import sys
203 by mbp at sourcefrog
revfile:
453
    sys.exit(main(sys.argv) or 0)