~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weave.py

  • Committer: Martin Pool
  • Date: 2005-09-13 01:37:23 UTC
  • Revision ID: mbp@sourcefrog.net-20050913013723-7e0026b48cbf08ff
- BROKEN: start refactoring fetch code to work well with weaves

Show diffs side-by-side

added added

removed removed

Lines of Context:
87
87
 
88
88
 
89
89
import sha
90
 
 
91
90
from cStringIO import StringIO
92
91
 
93
 
from bzrlib.osutils import sha_strings
94
 
 
95
92
 
96
93
class WeaveError(Exception):
97
94
    """Exception in processing weave"""
214
211
        return not self.__eq__(other)
215
212
 
216
213
 
217
 
    def maybe_lookup(self, name_or_index):
218
 
        """Convert possible symbolic name to index, or pass through indexes."""
219
 
        if isinstance(name_or_index, (int, long)):
220
 
            return name_or_index
221
 
        else:
222
 
            return self.lookup(name_or_index)
223
 
 
224
 
        
225
214
    def lookup(self, name):
226
 
        """Convert symbolic version name to index."""
227
215
        try:
228
216
            return self._name_map[name]
229
217
        except KeyError:
230
 
            raise WeaveError("name %r not present in weave %r" %
 
218
            raise WeaveError("name %s not present in weave %s" %
231
219
                             (name, self._weave_name))
232
220
 
233
 
 
234
 
    def idx_to_name(self, version):
235
 
        return self._names[version]
236
 
 
237
 
 
238
 
    def _check_repeated_add(self, name, parents, text):
239
 
        """Check that a duplicated add is OK.
240
 
 
241
 
        If it is, return the (old) index; otherwise raise an exception.
242
 
        """
243
 
        idx = self.lookup(name)
244
 
        if sorted(self._parents[idx]) != sorted(parents):
245
 
            raise WeaveError("name \"%s\" already present in weave "
246
 
                             "with different parents" % name)
247
 
        new_sha1 = sha_strings(text)
248
 
        if new_sha1 != self._sha1s[idx]:
249
 
            raise WeaveError("name \"%s\" already present in weave "
250
 
                             "with different text" % name)            
251
 
        return idx
252
 
        
253
 
 
254
221
        
255
222
    def add(self, name, parents, text):
256
223
        """Add a single text on top of the weave.
269
236
 
270
237
        assert isinstance(name, basestring)
271
238
        if name in self._name_map:
272
 
            return self._check_repeated_add(name, parents, text)
273
 
 
274
 
        parents = map(self.maybe_lookup, parents)
 
239
            raise WeaveError("name %r already present in weave" % name)
 
240
        
275
241
        self._check_versions(parents)
276
242
        ## self._check_lines(text)
277
243
        new_version = len(self._parents)
278
244
 
279
 
        sha1 = sha_strings(text)
 
245
        s = sha.new()
 
246
        map(s.update, text)
 
247
        sha1 = s.hexdigest()
 
248
        del s
280
249
 
281
250
        # if we abort after here the (in-memory) weave will be corrupt because only
282
251
        # some fields are updated
388
357
            raise ValueError("version %d not present in weave" % v)
389
358
 
390
359
 
391
 
    def parents(self, version):
392
 
        return self._parents[version]
393
 
 
394
 
 
395
360
    def minimal_parents(self, version):
396
361
        """Find the minimal set of parents for the version."""
397
362
        included = self._parents[version]
435
400
                raise IndexError("invalid version number %r" % i)
436
401
 
437
402
    
438
 
    def annotate(self, name_or_index):
439
 
        return list(self.annotate_iter(name_or_index))
440
 
 
441
 
 
442
 
    def annotate_iter(self, name_or_index):
 
403
    def annotate(self, index):
 
404
        return list(self.annotate_iter(index))
 
405
 
 
406
 
 
407
    def annotate_iter(self, version):
443
408
        """Yield list of (index-id, line) pairs for the specified version.
444
409
 
445
410
        The index indicates when the line originated in the weave."""
446
 
        incls = [self.maybe_lookup(name_or_index)]
447
 
        for origin, lineno, text in self._extract(incls):
 
411
        for origin, lineno, text in self._extract([version]):
448
412
            yield origin, text
449
413
 
450
414
 
548
512
    
549
513
 
550
514
 
551
 
    def get_iter(self, name_or_index):
 
515
    def get_iter(self, version):
552
516
        """Yield lines for the specified version."""
553
 
        incls = [self.maybe_lookup(name_or_index)]
554
 
        for origin, lineno, line in self._extract(incls):
 
517
        for origin, lineno, line in self._extract([version]):
555
518
            yield line
556
519
 
557
520
 
562
525
        return s.getvalue()
563
526
 
564
527
 
565
 
    def get(self, name_or_index):
566
 
        return list(self.get_iter(name_or_index))
 
528
    def get(self, index):
 
529
        return list(self.get_iter(index))
567
530
 
568
531
 
569
532
    def mash_iter(self, included):