~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weave.py

Merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
185
185
    __slots__ = ['_weave', '_parents', '_sha1s', '_names', '_name_map',
186
186
                 '_weave_name', '_matcher']
187
187
    
188
 
    def __init__(self, weave_name=None, access_mode='w', matcher=None):
 
188
    def __init__(self, weave_name=None, access_mode='w', matcher=None, get_scope=None):
 
189
        """Create a weave.
 
190
 
 
191
        :param get_scope: A callable that returns an opaque object to be used
 
192
            for detecting when this weave goes out of scope (should stop
 
193
            answering requests or allowing mutation).
 
194
        """
189
195
        super(Weave, self).__init__(access_mode)
190
196
        self._weave = []
191
197
        self._parents = []
197
203
            self._matcher = bzrlib.patiencediff.PatienceSequenceMatcher
198
204
        else:
199
205
            self._matcher = matcher
 
206
        if get_scope is None:
 
207
            get_scope = lambda:None
 
208
        self._get_scope = get_scope
 
209
        self._scope = get_scope()
 
210
        self._access_mode = access_mode
200
211
 
201
212
    def __repr__(self):
202
213
        return "Weave(%r)" % self._weave_name
203
214
 
 
215
    def _check_write_ok(self):
 
216
        """Is the versioned file marked as 'finished' ? Raise if it is."""
 
217
        if self._get_scope() != self._scope:
 
218
            raise errors.OutSideTransaction()
 
219
        if self._access_mode != 'w':
 
220
            raise errors.ReadOnlyObjectDirtiedError(self)
 
221
 
204
222
    def copy(self):
205
223
        """Return a deep copy of self.
206
224
        
401
419
                offset += 2 + (j2 - j1)
402
420
        return new_version
403
421
 
404
 
    def _clone_text(self, new_version_id, old_version_id, parents):
405
 
        """See VersionedFile.clone_text."""
406
 
        old_lines = self.get_text(old_version_id)
407
 
        self.add_lines(new_version_id, parents, old_lines)
408
 
 
409
422
    def _inclusions(self, versions):
410
423
        """Return set of all ancestors of given version(s)."""
411
424
        if not len(versions):
453
466
        """
454
467
        return len(other_parents.difference(my_parents)) == 0
455
468
 
456
 
    def annotate_iter(self, version_id):
457
 
        """Yield list of (version-id, line) pairs for the specified version.
 
469
    def annotate(self, version_id):
 
470
        """Return a list of (version-id, line) tuples for version_id.
458
471
 
459
472
        The index indicates when the line originated in the weave."""
460
473
        incls = [self._lookup(version_id)]
461
 
        for origin, lineno, text in self._extract(incls):
462
 
            yield self._idx_to_name(origin), text
 
474
        return [(self._idx_to_name(origin), text) for origin, lineno, text in
 
475
            self._extract(incls)]
463
476
 
464
477
    def iter_lines_added_or_present_in_versions(self, version_ids=None,
465
478
                                                pb=None):
670
683
                       expected_sha1, measured_sha1))
671
684
        return result
672
685
 
673
 
    def get_sha1(self, version_id):
674
 
        """See VersionedFile.get_sha1()."""
675
 
        return self._sha1s[self._lookup(version_id)]
676
 
 
677
686
    def get_sha1s(self, version_ids):
678
687
        """See VersionedFile.get_sha1s()."""
679
688
        return [self._sha1s[self._lookup(v)] for v in version_ids]
865
874
 
866
875
    WEAVE_SUFFIX = '.weave'
867
876
    
868
 
    def __init__(self, name, transport, filemode=None, create=False, access_mode='w'):
 
877
    def __init__(self, name, transport, filemode=None, create=False, access_mode='w', get_scope=None):
869
878
        """Create a WeaveFile.
870
879
        
871
880
        :param create: If not True, only open an existing knit.
872
881
        """
873
 
        super(WeaveFile, self).__init__(name, access_mode)
 
882
        super(WeaveFile, self).__init__(name, access_mode, get_scope=get_scope)
874
883
        self._transport = transport
875
884
        self._filemode = filemode
876
885
        try:
891
900
        self._save()
892
901
        return result
893
902
 
894
 
    def _clone_text(self, new_version_id, old_version_id, parents):
895
 
        """See VersionedFile.clone_text."""
896
 
        super(WeaveFile, self)._clone_text(new_version_id, old_version_id, parents)
897
 
        self._save
898
 
 
899
903
    def copy_to(self, name, transport):
900
904
        """See VersionedFile.copy_to()."""
901
905
        # as we are all in memory always, just serialise to the new place.
904
908
        sio.seek(0)
905
909
        transport.put_file(name + WeaveFile.WEAVE_SUFFIX, sio, self._filemode)
906
910
 
907
 
    def create_empty(self, name, transport, filemode=None):
908
 
        return WeaveFile(name, transport, filemode, create=True)
909
 
 
910
911
    def _save(self):
911
912
        """Save the weave."""
912
913
        self._check_write_ok()