/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

Merge from bzr.ab.integration

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
# the possible relationships.
68
68
 
69
69
 
 
70
from copy import copy
70
71
from cStringIO import StringIO
71
72
from difflib import SequenceMatcher
72
73
import os
180
181
    __slots__ = ['_weave', '_parents', '_sha1s', '_names', '_name_map',
181
182
                 '_weave_name']
182
183
    
183
 
    def __init__(self, weave_name=None):
 
184
    def __init__(self, weave_name=None, access_mode='w'):
 
185
        super(Weave, self).__init__(access_mode)
184
186
        self._weave = []
185
187
        self._parents = []
186
188
        self._sha1s = []
278
280
        """Please use Weave.clone_text now."""
279
281
        return self.clone_text(new_rev_id, old_rev_id, parents)
280
282
 
281
 
    def add_lines(self, version_id, parents, lines):
 
283
    def _add_lines(self, version_id, parents, lines):
282
284
        """See VersionedFile.add_lines."""
283
285
        return self._add(version_id, lines, map(self._lookup, parents))
284
286
 
406
408
                offset += 2 + (j2 - j1)
407
409
        return new_version
408
410
 
409
 
    def clone_text(self, new_version_id, old_version_id, parents):
 
411
    def _clone_text(self, new_version_id, old_version_id, parents):
410
412
        """See VersionedFile.clone_text."""
411
413
        old_lines = self.get_text(old_version_id)
412
414
        self.add_lines(new_version_id, parents, old_lines)
492
494
 
493
495
    @deprecated_method(zero_eight)
494
496
    def _walk(self):
495
 
        """_walk has become walk, a supported api."""
496
 
        return self.walk()
497
 
 
 
497
        """_walk has become visit, a supported api."""
 
498
        return self._walk_internal()
 
499
 
 
500
    def iter_lines_added_or_present_in_versions(self, version_ids=None):
 
501
        """See VersionedFile.iter_lines_added_or_present_in_versions()."""
 
502
        if version_ids is None:
 
503
            version_ids = self.versions()
 
504
        version_ids = set(version_ids)
 
505
        for lineno, inserted, deletes, line in self._walk_internal(version_ids):
 
506
            # if inserted not in version_ids then it was inserted before the
 
507
            # versions we care about, but because weaves cannot represent ghosts
 
508
            # properly, we do not filter down to that
 
509
            # if inserted not in version_ids: continue
 
510
            if line[-1] != '\n':
 
511
                yield line + '\n'
 
512
            else:
 
513
                yield line
 
514
 
 
515
    #@deprecated_method(zero_eight)
498
516
    def walk(self, version_ids=None):
499
517
        """See VersionedFile.walk."""
 
518
        return self._walk_internal(version_ids)
 
519
 
 
520
    def _walk_internal(self, version_ids=None):
 
521
        """Helper method for weave actions."""
500
522
        
501
523
        istack = []
502
524
        dset = set()
705
727
            update_text = 'checking %s' % (short_name,)
706
728
            update_text = update_text[:25]
707
729
 
708
 
        for lineno, insert, deleteset, line in self.walk():
 
730
        for lineno, insert, deleteset, line in self._walk_internal():
709
731
            if progress_bar:
710
732
                progress_bar.update(update_text, lineno, nlines)
711
733
 
755
777
                         version in version_ids]
756
778
        for name in topo_sort(pending_graph):
757
779
            other_idx = other._name_map[name]
758
 
            self._check_version_consistent(other, other_idx, name)
759
 
            sha1 = other._sha1s[other_idx]
760
 
 
 
780
            # returns True if we have it, False if we need it.
 
781
            if not self._check_version_consistent(other, other_idx, name):
 
782
                names_to_join.append((other_idx, name))
761
783
            processed += 1
762
784
 
763
 
            if name in self._name_map:
764
 
                idx = self._lookup(name)
765
 
                n1 = set(map(other._idx_to_name, other._parents[other_idx]))
766
 
                n2 = set(map(self._idx_to_name, self._parents[idx]))
767
 
                if sha1 ==  self._sha1s[idx] and n1 == n2:
768
 
                        continue
769
 
 
770
 
            names_to_join.append((other_idx, name))
771
785
 
772
786
        if pb and not msg:
773
787
            msg = 'weave join'
845
859
        :param msg: An optional message for the progress
846
860
        """
847
861
        new_weave = _reweave(self, other, pb=pb, msg=msg)
 
862
        self._copy_weave_content(new_weave)
 
863
 
 
864
    def _copy_weave_content(self, otherweave):
 
865
        """adsorb the content from otherweave."""
848
866
        for attr in self.__slots__:
849
867
            if attr != '_weave_name':
850
 
                setattr(self, attr, getattr(new_weave, attr))
 
868
                setattr(self, attr, copy(getattr(otherweave, attr)))
851
869
 
852
870
 
853
871
class WeaveFile(Weave):
855
873
 
856
874
    WEAVE_SUFFIX = '.weave'
857
875
    
858
 
    def __init__(self, name, transport, mode=None, create=False):
 
876
    def __init__(self, name, transport, filemode=None, create=False, access_mode='w'):
859
877
        """Create a WeaveFile.
860
878
        
861
879
        :param create: If not True, only open an existing knit.
862
880
        """
863
 
        super(WeaveFile, self).__init__(name)
 
881
        super(WeaveFile, self).__init__(name, access_mode)
864
882
        self._transport = transport
865
 
        self._mode = mode
 
883
        self._filemode = filemode
866
884
        try:
867
885
            _read_weave_v5(self._transport.get(name + WeaveFile.WEAVE_SUFFIX), self)
868
886
        except errors.NoSuchFile:
871
889
            # new file, save it
872
890
            self._save()
873
891
 
874
 
    def add_lines(self, version_id, parents, lines):
 
892
    def _add_lines(self, version_id, parents, lines):
875
893
        """Add a version and save the weave."""
876
 
        super(WeaveFile, self).add_lines(version_id, parents, lines)
 
894
        super(WeaveFile, self)._add_lines(version_id, parents, lines)
877
895
        self._save()
878
896
 
 
897
    def _clone_text(self, new_version_id, old_version_id, parents):
 
898
        """See VersionedFile.clone_text."""
 
899
        super(WeaveFile, self)._clone_text(new_version_id, old_version_id, parents)
 
900
        self._save
 
901
 
879
902
    def copy_to(self, name, transport):
880
903
        """See VersionedFile.copy_to()."""
881
904
        # as we are all in memory always, just serialise to the new place.
882
905
        sio = StringIO()
883
906
        write_weave_v5(self, sio)
884
907
        sio.seek(0)
885
 
        transport.put(name + WeaveFile.WEAVE_SUFFIX, sio, self._mode)
 
908
        transport.put(name + WeaveFile.WEAVE_SUFFIX, sio, self._filemode)
886
909
 
887
 
    def create_empty(self, name, transport, mode=None):
888
 
        return WeaveFile(name, transport, mode, create=True)
 
910
    def create_empty(self, name, transport, filemode=None):
 
911
        return WeaveFile(name, transport, filemode, create=True)
889
912
 
890
913
    def _save(self):
891
914
        """Save the weave."""
 
915
        self._check_write_ok()
892
916
        sio = StringIO()
893
917
        write_weave_v5(self, sio)
894
918
        sio.seek(0)
895
919
        self._transport.put(self._weave_name + WeaveFile.WEAVE_SUFFIX,
896
920
                            sio,
897
 
                            self._mode)
 
921
                            self._filemode)
898
922
 
899
923
    @staticmethod
900
924
    def get_suffixes():
1232
1256
 
1233
1257
    def join(self, pb=None, msg=None, version_ids=None, ignore_missing=False):
1234
1258
        """See InterVersionedFile.join."""
 
1259
        if self.target.versions() == []:
 
1260
            # optimised copy
 
1261
            self.target._copy_weave_content(self.source)
 
1262
            return
1235
1263
        try:
1236
1264
            self.target._join(self.source, pb, msg, version_ids, ignore_missing)
1237
1265
        except errors.WeaveParentMismatch: