94
94
import sys, zlib, struct, mdiff, stat, os, sha
95
95
from binascii import hexlify, unhexlify
99
101
_HEADER = "bzr revfile v1\n"
215
217
assert self.idxfile.tell() == _RECORDSIZE * (idx + 1)
216
218
data_offset = self.datafile.tell()
218
assert isinstance(data, str) # not unicode or anything weird
220
assert isinstance(data, str) # not unicode or anything wierd
220
222
self.datafile.write(data)
221
223
self.datafile.flush()
241
243
return self._add_compressed(text_sha, text, _NO_RECORD, compress)
245
def _choose_base(self, seed, base):
247
if base == _NO_RECORD:
250
if idxrec[I_BASE] == _NO_RECORD:
253
base = idxrec[I_BASE]
256
return base # relative to this full text
260
246
def _add_delta(self, text, text_sha, base, compress):
261
247
"""Add a text stored relative to a previous text."""
262
248
self._check_index(base)
265
base_text = self.get(base, CHAIN_LIMIT)
251
base_text = self.get(base, recursion_limit=CHAIN_LIMIT)
266
252
except LimitHitException:
267
253
return self._add_full_text(text, text_sha, compress)
302
288
# it's the same, in case someone ever breaks SHA-1.
303
289
return idx # already present
305
# base = self._choose_base(ord(text_sha[0]), base)
307
291
if base == _NO_RECORD:
308
292
return self._add_full_text(text, text_sha, compress)
420
404
"""Read back all index records.
422
406
Do not seek the index file while this is underway!"""
423
## sys.stderr.write(" ** iter called ** \n")
407
sys.stderr.write(" ** iter called ** \n")
424
408
self._seek_index(0)
426
410
idxrec = self._read_next_index()
478
461
except IndexError:
479
sys.stderr.write("usage: revfile dump REVFILE\n"
480
" revfile add REVFILE < INPUT\n"
481
" revfile add-delta REVFILE BASE < INPUT\n"
482
" revfile add-series REVFILE BASE FILE...\n"
483
" revfile get REVFILE IDX\n"
484
" revfile find-sha REVFILE HEX\n"
485
" revfile total-text-size REVFILE\n"
486
" revfile last REVFILE\n")
462
sys.stderr.write("usage: revfile dump\n"
464
" revfile add-delta BASE\n"
466
" revfile find-sha HEX\n"
467
" revfile total-text-size\n"
490
return Revfile(filename, 'w')
472
return Revfile('testrev', 'w')
493
return Revfile(filename, 'r')
475
return Revfile('testrev', 'r')
496
478
print rw().add(sys.stdin.read())
497
479
elif cmd == 'add-delta':
498
print rw().add(sys.stdin.read(), int(argv[3]))
499
elif cmd == 'add-series':
504
rev = r.add(file(fn).read(), rev)
480
print rw().add(sys.stdin.read(), int(argv[2]))
505
481
elif cmd == 'dump':
507
483
elif cmd == 'get':
510
486
except IndexError:
511
sys.stderr.write("usage: revfile get FILE IDX\n")
487
sys.stderr.write("usage: revfile get IDX\n")
516
490
if idx < 0 or idx >= len(r):
517
491
sys.stderr.write("invalid index %r\n" % idx)
520
sys.stdout.write(r.get(idx))
494
sys.stdout.write(ro().get(idx))
521
495
elif cmd == 'find-sha':
523
s = unhexlify(argv[3])
497
s = unhexlify(argv[2])
524
498
except IndexError:
525
sys.stderr.write("usage: revfile find-sha FILE HEX\n")
499
sys.stderr.write("usage: revfile find-sha HEX\n")
528
502
idx = ro().find_sha(s)