~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/repository.py

  • Committer: Martin Pool
  • Date: 2007-02-12 07:18:26 UTC
  • mto: (2220.2.19 tags)
  • mto: This revision was merged to the branch mainline in revision 2309.
  • Revision ID: mbp@sourcefrog.net-20070212071826-5x37ixz8r6cylk75
(broken) start moving things to branches

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
 
73
73
 
74
74
######################################################################
75
 
# tag storage
76
 
 
77
 
 
78
 
class _TagStore(object):
79
 
    def __init__(self, repository):
80
 
        self.repository = repository
81
 
 
82
 
class _DisabledTagStore(_TagStore):
83
 
    """Tag storage that refuses to store anything.
84
 
 
85
 
    This is used by older formats that can't store tags.
86
 
    """
87
 
 
88
 
    def _not_supported(self, *a, **k):
89
 
        raise errors.TagsNotSupported(self.repository)
90
 
 
91
 
    def supports_tags(self):
92
 
        return False
93
 
 
94
 
    set_tag = _not_supported
95
 
    get_tag_dict = _not_supported
96
 
    _set_tag_dict = _not_supported
97
 
    lookup_tag = _not_supported
98
 
 
99
 
 
100
 
class _BasicTagStore(_TagStore):
101
 
    """Tag storage in an unversioned repository control file.
102
 
    """
103
 
 
104
 
    def supports_tags(self):
105
 
        return True
106
 
 
107
 
    def set_tag(self, tag_name, tag_target):
108
 
        """Add a tag definition to the repository.
109
 
 
110
 
        Behaviour if the tag is already present is not defined (yet).
111
 
        """
112
 
        # all done with a write lock held, so this looks atomic
113
 
        self.repository.lock_write()
114
 
        try:
115
 
            td = self.get_tag_dict()
116
 
            td[tag_name] = tag_target
117
 
            self._set_tag_dict(td)
118
 
        finally:
119
 
            self.repository.unlock()
120
 
 
121
 
    def lookup_tag(self, tag_name):
122
 
        """Return the referent string of a tag"""
123
 
        td = self.get_tag_dict()
124
 
        try:
125
 
            return td[tag_name]
126
 
        except KeyError:
127
 
            raise errors.NoSuchTag(tag_name)
128
 
 
129
 
    def get_tag_dict(self):
130
 
        self.repository.lock_read()
131
 
        try:
132
 
            tag_content = self.repository.control_files.get_utf8('tags').read()
133
 
            return self._deserialize_tag_dict(tag_content)
134
 
        finally:
135
 
            self.repository.unlock()
136
 
 
137
 
    def _set_tag_dict(self, new_dict):
138
 
        """Replace all tag definitions
139
 
 
140
 
        :param new_dict: Dictionary from tag name to target.
141
 
        """
142
 
        self.repository.lock_read()
143
 
        try:
144
 
            self.repository.control_files.put_utf8('tags',
145
 
                self._serialize_tag_dict(new_dict))
146
 
        finally:
147
 
            self.repository.unlock()
148
 
 
149
 
    def _serialize_tag_dict(self, tag_dict):
150
 
        s = []
151
 
        for tag, target in sorted(tag_dict.items()):
152
 
            # TODO: check that tag names and targets are acceptable
153
 
            s.append(tag + '\t' + target + '\n')
154
 
        return ''.join(s)
155
 
 
156
 
    def _deserialize_tag_dict(self, tag_content):
157
 
        """Convert the tag file into a dictionary of tags"""
158
 
        d = {}
159
 
        for l in tag_content.splitlines():
160
 
            tag, target = l.split('\t', 1)
161
 
            d[tag] = target
162
 
        return d
163
 
 
164
 
 
165
 
######################################################################
166
75
# Repositories
167
76
 
168
77
class Repository(object):
177
86
    remote) disk.
178
87
    """
179
88
 
180
 
    # override this to set the strategy for storing tags
181
 
    _tag_store_class = _DisabledTagStore
182
 
 
183
89
    _file_ids_altered_regex = lazy_regex.lazy_compile(
184
90
        r'file_id="(?P<file_id>[^"]+)"'
185
91
        r'.*revision="(?P<revision_id>[^"]+)"'
319
225
        # on whether escaping is required.
320
226
        self._warn_if_deprecated()
321
227
        self._serializer = xml5.serializer_v5
322
 
        self._tag_store = self._tag_store_class(self)
323
228
 
324
229
    def __repr__(self):
325
230
        return '%s(%r)' % (self.__class__.__name__, 
873
778
                except UnicodeEncodeError:
874
779
                    raise errors.NonAsciiRevisionId(method, self)
875
780
 
876
 
    def set_tag(self, tag_name, tag_target):
877
 
        self._tag_store.set_tag(tag_name, tag_target)
878
 
 
879
 
    def lookup_tag(self, tag_name):
880
 
        return self._tag_store.lookup_tag(tag_name)
881
 
 
882
 
    def get_tag_dict(self):
883
 
        return self._tag_store.get_tag_dict()
884
 
 
885
 
    def _set_tag_dict(self, new_dict):
886
 
        return self._tag_store._set_tag_dict(new_dict)
887
 
 
888
 
    def supports_tags(self):
889
 
        return self._tag_store.supports_tags()
890
 
 
891
 
    def copy_tags_to(self, to_repository):
892
 
        """Copy tags to another repository.
893
 
 
894
 
        Subclasses should not override this, but rather customize copying 
895
 
        through the InterRepository mechanism.
896
 
        """
897
 
        return InterRepository.get(self, to_repository).copy_tags()
898
 
 
899
781
 
900
782
class AllInOneRepository(Repository):
901
783
    """Legacy support - the repository behaviour for all-in-one branches."""
1213
1095
 
1214
1096
    # corresponds to RepositoryFormatKnit2
1215
1097
    
1216
 
    # TODO: within a lock scope, we could keep the tags in memory...
1217
 
    
1218
 
    _tag_store_class = _BasicTagStore
1219
 
 
1220
1098
    def __init__(self, _format, a_bzrdir, control_files, _revision_store,
1221
1099
                 control_store, text_store):
1222
1100
        KnitRepository.__init__(self, _format, a_bzrdir, control_files,
1418
1296
        assert klass._formats[format.get_format_string()] is format
1419
1297
        del klass._formats[format.get_format_string()]
1420
1298
 
1421
 
    def supports_tags(self):
1422
 
        """True if this format supports tags stored in the repository"""
1423
 
        return False  # by default
1424
 
 
1425
1299
 
1426
1300
class PreSplitOutRepositoryFormat(RepositoryFormat):
1427
1301
    """Base class for the pre split out repository formats."""
1887
1761
     - an optional 'no-working-trees' flag
1888
1762
     - a LockDir lock
1889
1763
     - Support for recording full info about the tree root
1890
 
     - A tag dictionary stored in the repository, pointing to revisions
1891
1764
    """
1892
1765
    
1893
1766
    rich_root_data = True