/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/weave.py

  • Committer: Martin Pool
  • Date: 2005-07-18 11:41:06 UTC
  • Revision ID: mbp@sourcefrog.net-20050718114106-1130a9c7b902dd34
- allow for parents specified to Weave.add to be a set

Show diffs side-by-side

added added

removed removed

Lines of Context:
71
71
# properly nested, that there is no text outside of an insertion, that
72
72
# insertions or deletions are not repeated, etc.
73
73
 
 
74
# TODO: Make the info command just show info, not extract everything:
 
75
# it can be much faster.
 
76
 
 
77
# TODO: Perhaps use long integers as sets instead of set objects; may
 
78
# be faster.
 
79
 
74
80
# TODO: Parallel-extract that passes back each line along with a
75
81
# description of which revisions include it.  Nice for checking all
76
82
# shas in parallel.
107
113
    the version-id is used to reference it in the larger world.
108
114
 
109
115
    The weave is represented as a list mixing edit instructions and
110
 
    literal text.  Each entry in _weave can be either a string (or
 
116
    literal text.  Each entry in _l can be either a string (or
111
117
    unicode), or a tuple.  If a string, it means that the given line
112
118
    should be output in the currently active revisions.
113
119
 
151
157
      should be no way to get an earlier version deleting a later
152
158
      version.
153
159
 
154
 
    _weave
155
 
        Text of the weave; list of control instruction tuples and strings.
 
160
    _l
 
161
        Text of the weave.
156
162
 
157
 
    _parents
 
163
    _v
158
164
        List of parents, indexed by version number.
159
165
        It is only necessary to store the minimal set of parents for
160
166
        each version; the parent's parents are implied.
163
169
        List of hex SHA-1 of each version, or None if not recorded.
164
170
    """
165
171
 
166
 
    __slots__ = ['_weave', '_parents', '_sha1s']
 
172
    ## __slots__ = ['_l', '_v', '_sha1s']
167
173
    
168
174
    def __init__(self):
169
 
        self._weave = []
170
 
        self._parents = []
 
175
        self._l = []
 
176
        self._v = []
171
177
        self._sha1s = []
172
178
 
173
179
 
174
180
    def __eq__(self, other):
175
181
        if not isinstance(other, Weave):
176
182
            return False
177
 
        return self._parents == other._parents \
178
 
               and self._weave == other._weave
 
183
        return self._v == other._v \
 
184
               and self._l == other._l
179
185
    
180
186
 
181
187
    def __ne__(self, other):
195
201
 
196
202
        self._check_versions(parents)
197
203
        ## self._check_lines(text)
198
 
        new_version = len(self._parents)
 
204
        new_version = len(self._v)
199
205
 
200
206
        import sha
201
207
        s = sha.new()
204
210
        del s
205
211
 
206
212
        # if we abort after here the weave will be corrupt
207
 
        self._parents.append(frozenset(parents))
 
213
        self._v.append(frozenset(parents))
208
214
        self._sha1s.append(sha1)
209
215
 
210
216
            
214
220
            # even more specially, if we're adding an empty text we
215
221
            # need do nothing at all.
216
222
            if text:
217
 
                self._weave.append(('{', new_version))
218
 
                self._weave.extend(text)
219
 
                self._weave.append(('}', new_version))
 
223
                self._l.append(('{', new_version))
 
224
                self._l.extend(text)
 
225
                self._l.append(('}', new_version))
220
226
        
221
227
            return new_version
222
228
 
229
235
 
230
236
        ancestors = self.inclusions(parents)
231
237
 
232
 
        l = self._weave
 
238
        l = self._l
233
239
 
234
240
        # basis a list of (origin, lineno, line)
235
241
        basis_lineno = []
243
249
            return new_version            
244
250
 
245
251
        # add a sentinal, because we can also match against the final line
246
 
        basis_lineno.append(len(self._weave))
 
252
        basis_lineno.append(len(self._l))
247
253
 
248
254
        # XXX: which line of the weave should we really consider
249
255
        # matches the end of the file?  the current code says it's the
277
283
            # the deletion and insertion are handled separately.
278
284
            # first delete the region.
279
285
            if i1 != i2:
280
 
                self._weave.insert(i1+offset, ('[', new_version))
281
 
                self._weave.insert(i2+offset+1, (']', new_version))
 
286
                self._l.insert(i1+offset, ('[', new_version))
 
287
                self._l.insert(i2+offset+1, (']', new_version))
282
288
                offset += 2
283
289
 
284
290
            if j1 != j2:
286
292
                # i2; we want to insert after this region to make sure
287
293
                # we don't destroy ourselves
288
294
                i = i2 + offset
289
 
                self._weave[i:i] = ([('{', new_version)] 
 
295
                self._l[i:i] = ([('{', new_version)] 
290
296
                                + text[j1:j2] 
291
297
                                + [('}', new_version)])
292
298
                offset += 2 + (j2 - j1)
302
308
            while v >= 0:
303
309
                if v in i:
304
310
                    # include all its parents
305
 
                    i.update(self._parents[v])
 
311
                    i.update(self._v[v])
306
312
                v -= 1
307
313
            return i
308
314
        except IndexError:
311
317
 
312
318
    def minimal_parents(self, version):
313
319
        """Find the minimal set of parents for the version."""
314
 
        included = self._parents[version]
 
320
        included = self._v[version]
315
321
        if not included:
316
322
            return []
317
323
        
347
353
        """Check everything in the sequence of indexes is valid"""
348
354
        for i in indexes:
349
355
            try:
350
 
                self._parents[i]
 
356
                self._v[i]
351
357
            except IndexError:
352
358
                raise IndexError("invalid version number %r" % i)
353
359
 
377
383
 
378
384
        lineno = 0         # line of weave, 0-based
379
385
 
380
 
        for l in self._weave:
 
386
        for l in self._l:
381
387
            if isinstance(l, tuple):
382
388
                c, v = l
383
389
                isactive = None
423
429
 
424
430
        WFE = WeaveFormatError
425
431
 
426
 
        for l in self._weave:
 
432
        for l in self._l:
427
433
            if isinstance(l, tuple):
428
434
                c, v = l
429
435
                isactive = None
479
485
 
480
486
    def dump(self, to_file):
481
487
        from pprint import pprint
482
 
        print >>to_file, "Weave._weave = ",
483
 
        pprint(self._weave, to_file)
484
 
        print >>to_file, "Weave._parents = ",
485
 
        pprint(self._parents, to_file)
 
488
        print >>to_file, "Weave._l = ",
 
489
        pprint(self._l, to_file)
 
490
        print >>to_file, "Weave._v = ",
 
491
        pprint(self._v, to_file)
486
492
 
487
493
 
488
494
 
489
495
    def numversions(self):
490
 
        l = len(self._parents)
 
496
        l = len(self._v)
491
497
        assert l == len(self._sha1s)
492
498
        return l
493
499
 
494
500
 
495
 
    def __len__(self):
496
 
        return self.numversions()
497
 
 
498
 
 
499
501
    def check(self, progress_bar=None):
500
502
        # check no circular inclusions
501
503
        for version in range(self.numversions()):
502
 
            inclusions = list(self._parents[version])
 
504
            inclusions = list(self._v[version])
503
505
            if inclusions:
504
506
                inclusions.sort()
505
507
                if inclusions[-1] >= version:
670
672
 
671
673
 
672
674
 
673
 
def weave_info(w):
 
675
def weave_info(filename, out):
674
676
    """Show some text information about the weave."""
675
 
    print '%6s %40s %20s' % ('ver', 'sha1', 'parents')
676
 
    for i in (6, 40, 20):
677
 
        print '-' * i,
678
 
    print
679
 
    for i in range(w.numversions()):
680
 
        sha1 = w._sha1s[i]
681
 
        print '%6d %40s %s' % (i, sha1, ' '.join(map(str, w._parents[i])))
682
 
 
683
 
 
684
 
 
685
 
def weave_stats(weave_file):
686
 
    from bzrlib.progress import ProgressBar
687
 
    from bzrlib.weavefile import read_weave
688
 
 
689
 
    pb = ProgressBar()
690
 
 
691
 
    wf = file(weave_file, 'rb')
 
677
    from weavefile import read_weave
 
678
    wf = file(filename, 'rb')
692
679
    w = read_weave(wf)
693
680
    # FIXME: doesn't work on pipes
694
681
    weave_size = wf.tell()
 
682
    print >>out, "weave file size %d bytes" % weave_size
 
683
    print >>out, "weave contains %d versions" % len(w._v)
695
684
 
696
685
    total = 0
697
 
    vers = len(w)
698
 
    for i in range(vers):
699
 
        pb.update('checking sizes', i, vers)
700
 
        for line in w.get_iter(i):
701
 
            total += len(line)
702
 
 
703
 
    pb.clear()
704
 
 
705
 
    print 'versions          %9d' % vers
706
 
    print 'weave file        %9d bytes' % weave_size
707
 
    print 'total contents    %9d bytes' % total
708
 
    print 'compression ratio %9.2fx' % (float(total) / float(weave_size))
709
 
 
 
686
    print '%6s %6s %8s %40s %20s' % ('ver', 'lines', 'bytes', 'sha1', 'parents')
 
687
    for i in (6, 6, 8, 40, 20):
 
688
        print '-' * i,
 
689
    print
 
690
    for i in range(len(w._v)):
 
691
        text = w.get(i)
 
692
        lines = len(text)
 
693
        bytes = sum((len(a) for a in text))
 
694
        sha1 = w._sha1s[i]
 
695
        print '%6d %6d %8d %40s' % (i, lines, bytes, sha1),
 
696
        for pv in w._v[i]:
 
697
            print pv,
 
698
        print
 
699
        total += bytes
 
700
 
 
701
    print >>out, "versions total %d bytes" % total
 
702
    print >>out, "compression ratio %.3f" % (float(total)/float(weave_size))
710
703
 
711
704
 
712
705
def usage():
810
803
                lasto = origin
811
804
                
812
805
    elif cmd == 'info':
813
 
        weave_info(readit())
814
 
 
815
 
    elif cmd == 'stats':
816
 
        weave_stats(argv[2])
 
806
        weave_info(argv[2], sys.stdout)
817
807
        
818
808
    elif cmd == 'check':
819
809
        w = readit()
828
818
 
829
819
    elif cmd == 'parents':
830
820
        w = readit()
831
 
        print ' '.join(map(str, w._parents[int(argv[3])]))
 
821
        print ' '.join(map(str, w._v[int(argv[3])]))
832
822
 
833
823
    elif cmd == 'plan-merge':
834
824
        w = readit()