~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revfile.py

  • Committer: Robert Collins
  • Date: 2005-10-03 05:54:35 UTC
  • mto: (1393.1.30)
  • mto: This revision was merged to the branch mainline in revision 1400.
  • Revision ID: robertc@robertcollins.net-20051003055434-c8ebd30d1de10247
move exporting functionality into inventory.py - uncovers bug in symlink support

Show diffs side-by-side

added added

removed removed

Lines of Context:
89
89
# if there are gaps and that can happen if we're interrupted while
90
90
# writing to the datafile.  Overlapping would be very bad though.
91
91
 
92
 
 
93
 
 
94
 
import sys, zlib, struct, mdiff, stat, os, sha
 
92
# TODO: Shouldn't need to lock if we always write in append mode and
 
93
# then ftell after writing to see where it went.  In any case we
 
94
# assume the whole branch is protected by a lock.
 
95
 
 
96
import os
 
97
import sha
 
98
import stat
 
99
import struct
 
100
import sys
 
101
import zlib
95
102
from binascii import hexlify, unhexlify
96
103
 
 
104
import bzrlib.mdiff as mdiff
 
105
 
 
106
 
97
107
_RECORDSIZE = 48
98
108
 
99
109
_HEADER = "bzr revfile v1\n"
110
120
FL_GZIP = 1
111
121
 
112
122
# maximum number of patches in a row before recording a whole text.
113
 
CHAIN_LIMIT = 25
 
123
CHAIN_LIMIT = 10
114
124
 
115
125
 
116
126
class RevfileError(Exception):
147
157
            self.idxfile = open(idxname, 'w+b')
148
158
            self.datafile = open(dataname, 'w+b')
149
159
            
150
 
            print 'init empty file'
151
160
            self.idxfile.write(_HEADER)
152
161
            self.idxfile.flush()
153
162
        else:
267
276
            return self._add_full_text(text, text_sha, compress)
268
277
        
269
278
        data = mdiff.bdiff(base_text, text)
 
279
 
 
280
 
 
281
        if True: # paranoid early check for bad diff
 
282
            result = mdiff.bpatch(base_text, data)
 
283
            assert result == text
 
284
            
270
285
        
271
286
        # If the delta is larger than the text, we might as well just
272
287
        # store the text.  (OK, the delta might be more compressible,
278
293
            return self._add_compressed(text_sha, data, base, compress)
279
294
 
280
295
 
281
 
    def add(self, text, base=_NO_RECORD, compress=True):
 
296
    def add(self, text, base=None, compress=True):
282
297
        """Add a new text to the revfile.
283
298
 
284
299
        If the text is already present them its existing id is
292
307
        only be used if it would be a size win and if the existing
293
308
        base is not at too long of a delta chain already.
294
309
        """
 
310
        if base == None:
 
311
            base = _NO_RECORD
 
312
        
295
313
        self._check_write()
296
314
        
297
315
        text_sha = sha.new(text).digest()
328
346
            text = self._get_patched(idx, idxrec, recursion_limit)
329
347
 
330
348
        if sha.new(text).digest() != idxrec[I_SHA]:
331
 
            raise RevfileError("corrupt SHA-1 digest on record %d"
332
 
                               % idx)
 
349
            raise RevfileError("corrupt SHA-1 digest on record %d in %s"
 
350
                               % (idx, self.basename))
333
351
 
334
352
        return text
335
353
 
468
486
        for idx in range(len(self)):
469
487
            t += len(self.get(idx))
470
488
        return t
 
489
 
 
490
 
 
491
    def check(self, pb=None):
 
492
        """Extract every version and check its hash."""
 
493
        total = len(self)
 
494
        for i in range(total):
 
495
            if pb:
 
496
                pb.update("check revision", i, total)
 
497
            # the get method implicitly checks the SHA-1
 
498
            self.get(i)
 
499
        if pb:
 
500
            pb.clear()
471
501
        
472
502
 
473
503
 
486
516
                         "       revfile last REVFILE\n")
487
517
        return 1
488
518
 
 
519
    if filename.endswith('.drev') or filename.endswith('.irev'):
 
520
        filename = filename[:-5]
 
521
 
489
522
    def rw():
490
523
        return Revfile(filename, 'w')
491
524
 
535
568
        print ro().total_text_size()
536
569
    elif cmd == 'last':
537
570
        print len(ro())-1
 
571
    elif cmd == 'check':
 
572
        import bzrlib.progress
 
573
        pb = bzrlib.progress.ProgressBar()
 
574
        ro().check(pb)
538
575
    else:
539
576
        sys.stderr.write("unknown command %r\n" % cmd)
540
577
        return 1