~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weave.py

  • Committer: Jelmer Vernooij
  • Date: 2011-02-05 14:24:01 UTC
  • mto: (5582.12.2 weave-plugin)
  • mto: This revision was merged to the branch mainline in revision 5718.
  • Revision ID: jelmer@samba.org-20110205142401-yhslm4pbjov9aw4n
revert weavefile changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
# FIXME: the conflict markers should be *7* characters
68
68
 
69
69
from copy import copy
 
70
from cStringIO import StringIO
70
71
import os
71
72
 
72
73
from bzrlib.lazy_import import lazy_import
77
78
    errors,
78
79
    osutils,
79
80
    )
80
 
from bzrlib.errors import (
81
 
    WeaveError,
82
 
    WeaveFormatError,
83
 
    WeaveParentMismatch,
84
 
    RevisionAlreadyPresent,
85
 
    RevisionNotPresent,
86
 
    UnavailableRepresentation,
87
 
    )
88
 
from bzrlib.osutils import (
89
 
    sha,
90
 
    sha_strings,
91
 
    split_lines,
92
 
    )
 
81
from bzrlib.errors import (WeaveError, WeaveFormatError, WeaveParentMismatch,
 
82
        RevisionAlreadyPresent,
 
83
        RevisionNotPresent,
 
84
        UnavailableRepresentation,
 
85
        )
 
86
from bzrlib.osutils import dirname, sha, sha_strings, split_lines
93
87
import bzrlib.patiencediff
94
88
from bzrlib.revision import NULL_REVISION
95
89
from bzrlib.symbol_versioning import *
101
95
    sort_groupcompress,
102
96
    VersionedFile,
103
97
    )
 
98
from bzrlib.weavefile import _read_weave_v5, write_weave_v5
104
99
 
105
100
 
106
101
class WeaveContentFactory(ContentFactory):
910
905
                setattr(self, attr, copy(getattr(otherweave, attr)))
911
906
 
912
907
 
 
908
class WeaveFile(Weave):
 
909
    """A WeaveFile represents a Weave on disk and writes on change."""
 
910
 
 
911
    WEAVE_SUFFIX = '.weave'
 
912
 
 
913
    def __init__(self, name, transport, filemode=None, create=False, access_mode='w', get_scope=None):
 
914
        """Create a WeaveFile.
 
915
 
 
916
        :param create: If not True, only open an existing knit.
 
917
        """
 
918
        super(WeaveFile, self).__init__(name, access_mode, get_scope=get_scope,
 
919
            allow_reserved=False)
 
920
        self._transport = transport
 
921
        self._filemode = filemode
 
922
        try:
 
923
            _read_weave_v5(self._transport.get(name + WeaveFile.WEAVE_SUFFIX), self)
 
924
        except errors.NoSuchFile:
 
925
            if not create:
 
926
                raise
 
927
            # new file, save it
 
928
            self._save()
 
929
 
 
930
    def _add_lines(self, version_id, parents, lines, parent_texts,
 
931
        left_matching_blocks, nostore_sha, random_id, check_content):
 
932
        """Add a version and save the weave."""
 
933
        self.check_not_reserved_id(version_id)
 
934
        result = super(WeaveFile, self)._add_lines(version_id, parents, lines,
 
935
            parent_texts, left_matching_blocks, nostore_sha, random_id,
 
936
            check_content)
 
937
        self._save()
 
938
        return result
 
939
 
 
940
    def copy_to(self, name, transport):
 
941
        """See VersionedFile.copy_to()."""
 
942
        # as we are all in memory always, just serialise to the new place.
 
943
        sio = StringIO()
 
944
        write_weave_v5(self, sio)
 
945
        sio.seek(0)
 
946
        transport.put_file(name + WeaveFile.WEAVE_SUFFIX, sio, self._filemode)
 
947
 
 
948
    def _save(self):
 
949
        """Save the weave."""
 
950
        self._check_write_ok()
 
951
        sio = StringIO()
 
952
        write_weave_v5(self, sio)
 
953
        sio.seek(0)
 
954
        bytes = sio.getvalue()
 
955
        path = self._weave_name + WeaveFile.WEAVE_SUFFIX
 
956
        try:
 
957
            self._transport.put_bytes(path, bytes, self._filemode)
 
958
        except errors.NoSuchFile:
 
959
            self._transport.mkdir(dirname(path))
 
960
            self._transport.put_bytes(path, bytes, self._filemode)
 
961
 
 
962
    @staticmethod
 
963
    def get_suffixes():
 
964
        """See VersionedFile.get_suffixes()."""
 
965
        return [WeaveFile.WEAVE_SUFFIX]
 
966
 
 
967
    def insert_record_stream(self, stream):
 
968
        super(WeaveFile, self).insert_record_stream(stream)
 
969
        self._save()
 
970
 
 
971
 
913
972
def _reweave(wa, wb, pb=None, msg=None):
914
973
    """Combine two weaves and return the result.
915
974