1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
 
3
# This program is free software; you can redistribute it and/or modify
 
 
4
# it under the terms of the GNU General Public License as published by
 
 
5
# the Free Software Foundation; either version 2 of the License, or
 
 
6
# (at your option) any later version.
 
 
8
# This program is distributed in the hope that it will be useful,
 
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
11
# GNU General Public License for more details.
 
 
13
# You should have received a copy of the GNU General Public License
 
 
14
# along with this program; if not, write to the Free Software
 
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
18
"""Old weave-based repository formats"""
 
 
20
from StringIO import StringIO
 
 
30
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
 
31
from bzrlib.repository import (
 
 
33
    MetaDirRepositoryFormat,
 
 
37
from bzrlib.store.text import TextStore
 
 
38
from bzrlib.trace import mutter
 
 
41
class AllInOneRepository(Repository):
 
 
42
    """Legacy support - the repository behaviour for all-in-one branches."""
 
 
44
    _serializer = xml5.serializer_v5
 
 
46
    def __init__(self, _format, a_bzrdir, _revision_store, control_store, text_store):
 
 
47
        # we reuse one control files instance.
 
 
48
        dir_mode = a_bzrdir._control_files._dir_mode
 
 
49
        file_mode = a_bzrdir._control_files._file_mode
 
 
51
        def get_store(name, compressed=True, prefixed=False):
 
 
52
            # FIXME: This approach of assuming stores are all entirely compressed
 
 
53
            # or entirely uncompressed is tidy, but breaks upgrade from 
 
 
54
            # some existing branches where there's a mixture; we probably 
 
 
55
            # still want the option to look for both.
 
 
56
            relpath = a_bzrdir._control_files._escape(name)
 
 
57
            store = TextStore(a_bzrdir._control_files._transport.clone(relpath),
 
 
58
                              prefixed=prefixed, compressed=compressed,
 
 
61
            #if self._transport.should_cache():
 
 
62
            #    cache_path = os.path.join(self.cache_root, name)
 
 
63
            #    os.mkdir(cache_path)
 
 
64
            #    store = bzrlib.store.CachedStore(store, cache_path)
 
 
67
        # not broken out yet because the controlweaves|inventory_store
 
 
68
        # and text_store | weave_store bits are still different.
 
 
69
        if isinstance(_format, RepositoryFormat4):
 
 
70
            # cannot remove these - there is still no consistent api 
 
 
71
            # which allows access to this old info.
 
 
72
            self.inventory_store = get_store('inventory-store')
 
 
73
            text_store = get_store('text-store')
 
 
74
        super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, _revision_store, control_store, text_store)
 
 
76
    def get_commit_builder(self, branch, parents, config, timestamp=None,
 
 
77
                           timezone=None, committer=None, revprops=None,
 
 
79
        self._check_ascii_revisionid(revision_id, self.get_commit_builder)
 
 
80
        return Repository.get_commit_builder(self, branch, parents, config,
 
 
81
            timestamp, timezone, committer, revprops, revision_id)
 
 
85
        """AllInOne repositories cannot be shared."""
 
 
89
    def set_make_working_trees(self, new_value):
 
 
90
        """Set the policy flag for making working trees when creating branches.
 
 
92
        This only applies to branches that use this repository.
 
 
94
        The default is 'True'.
 
 
95
        :param new_value: True to restore the default, False to disable making
 
 
98
        raise NotImplementedError(self.set_make_working_trees)
 
 
100
    def make_working_trees(self):
 
 
101
        """Returns the policy for making working trees on new branches."""
 
 
105
class WeaveMetaDirRepository(MetaDirRepository):
 
 
106
    """A subclass of MetaDirRepository to set weave specific policy."""
 
 
108
    _serializer = xml5.serializer_v5
 
 
110
    def get_commit_builder(self, branch, parents, config, timestamp=None,
 
 
111
                           timezone=None, committer=None, revprops=None,
 
 
113
        self._check_ascii_revisionid(revision_id, self.get_commit_builder)
 
 
114
        return MetaDirRepository.get_commit_builder(self, branch, parents,
 
 
115
            config, timestamp, timezone, committer, revprops, revision_id)
 
 
118
class PreSplitOutRepositoryFormat(RepositoryFormat):
 
 
119
    """Base class for the pre split out repository formats."""
 
 
121
    rich_root_data = False
 
 
122
    supports_tree_reference = False
 
 
124
    def initialize(self, a_bzrdir, shared=False, _internal=False):
 
 
125
        """Create a weave repository.
 
 
127
        TODO: when creating split out bzr branch formats, move this to a common
 
 
128
        base for Format5, Format6. or something like that.
 
 
131
            raise errors.IncompatibleFormat(self, a_bzrdir._format)
 
 
134
            # always initialized when the bzrdir is.
 
 
135
            return self.open(a_bzrdir, _found=True)
 
 
137
        # Create an empty weave
 
 
139
        weavefile.write_weave_v5(weave.Weave(), sio)
 
 
140
        empty_weave = sio.getvalue()
 
 
142
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
 
143
        dirs = ['revision-store', 'weaves']
 
 
144
        files = [('inventory.weave', StringIO(empty_weave)),
 
 
147
        # FIXME: RBC 20060125 don't peek under the covers
 
 
148
        # NB: no need to escape relative paths that are url safe.
 
 
149
        control_files = lockable_files.LockableFiles(a_bzrdir.transport,
 
 
150
                                'branch-lock', lockable_files.TransportLock)
 
 
151
        control_files.create_lock()
 
 
152
        control_files.lock_write()
 
 
153
        control_files._transport.mkdir_multi(dirs,
 
 
154
                mode=control_files._dir_mode)
 
 
156
            for file, content in files:
 
 
157
                control_files.put(file, content)
 
 
159
            control_files.unlock()
 
 
160
        return self.open(a_bzrdir, _found=True)
 
 
162
    def _get_control_store(self, repo_transport, control_files):
 
 
163
        """Return the control store for this repository."""
 
 
164
        return self._get_versioned_file_store('',
 
 
169
    def _get_text_store(self, transport, control_files):
 
 
170
        """Get a store for file texts for this format."""
 
 
171
        raise NotImplementedError(self._get_text_store)
 
 
173
    def open(self, a_bzrdir, _found=False):
 
 
174
        """See RepositoryFormat.open()."""
 
 
176
            # we are being called directly and must probe.
 
 
177
            raise NotImplementedError
 
 
179
        repo_transport = a_bzrdir.get_repository_transport(None)
 
 
180
        control_files = a_bzrdir._control_files
 
 
181
        text_store = self._get_text_store(repo_transport, control_files)
 
 
182
        control_store = self._get_control_store(repo_transport, control_files)
 
 
183
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
 
184
        return AllInOneRepository(_format=self,
 
 
186
                                  _revision_store=_revision_store,
 
 
187
                                  control_store=control_store,
 
 
188
                                  text_store=text_store)
 
 
190
    def check_conversion_target(self, target_format):
 
 
194
class RepositoryFormat4(PreSplitOutRepositoryFormat):
 
 
195
    """Bzr repository format 4.
 
 
197
    This repository format has:
 
 
199
     - TextStores for texts, inventories,revisions.
 
 
201
    This format is deprecated: it indexes texts using a text id which is
 
 
202
    removed in format 5; initialization and write support for this format
 
 
206
    _matchingbzrdir = bzrdir.BzrDirFormat4()
 
 
209
        super(RepositoryFormat4, self).__init__()
 
 
211
    def get_format_description(self):
 
 
212
        """See RepositoryFormat.get_format_description()."""
 
 
213
        return "Repository format 4"
 
 
215
    def initialize(self, url, shared=False, _internal=False):
 
 
216
        """Format 4 branches cannot be created."""
 
 
217
        raise errors.UninitializableFormat(self)
 
 
219
    def is_supported(self):
 
 
220
        """Format 4 is not supported.
 
 
222
        It is not supported because the model changed from 4 to 5 and the
 
 
223
        conversion logic is expensive - so doing it on the fly was not 
 
 
228
    def _get_control_store(self, repo_transport, control_files):
 
 
229
        """Format 4 repositories have no formal control store at this point.
 
 
231
        This will cause any control-file-needing apis to fail - this is desired.
 
 
235
    def _get_revision_store(self, repo_transport, control_files):
 
 
236
        """See RepositoryFormat._get_revision_store()."""
 
 
237
        from bzrlib.xml4 import serializer_v4
 
 
238
        return self._get_text_rev_store(repo_transport,
 
 
241
                                        serializer=serializer_v4)
 
 
243
    def _get_text_store(self, transport, control_files):
 
 
244
        """See RepositoryFormat._get_text_store()."""
 
 
247
class RepositoryFormat5(PreSplitOutRepositoryFormat):
 
 
248
    """Bzr control format 5.
 
 
250
    This repository format has:
 
 
251
     - weaves for file texts and inventory
 
 
253
     - TextStores for revisions and signatures.
 
 
256
    _versionedfile_class = weave.WeaveFile
 
 
257
    _matchingbzrdir = bzrdir.BzrDirFormat5()
 
 
260
        super(RepositoryFormat5, self).__init__()
 
 
262
    def get_format_description(self):
 
 
263
        """See RepositoryFormat.get_format_description()."""
 
 
264
        return "Weave repository format 5"
 
 
266
    def _get_revision_store(self, repo_transport, control_files):
 
 
267
        """See RepositoryFormat._get_revision_store()."""
 
 
268
        """Return the revision store object for this a_bzrdir."""
 
 
269
        return self._get_text_rev_store(repo_transport,
 
 
274
    def _get_text_store(self, transport, control_files):
 
 
275
        """See RepositoryFormat._get_text_store()."""
 
 
276
        return self._get_versioned_file_store('weaves', transport, control_files, prefixed=False)
 
 
279
class RepositoryFormat6(PreSplitOutRepositoryFormat):
 
 
280
    """Bzr control format 6.
 
 
282
    This repository format has:
 
 
283
     - weaves for file texts and inventory
 
 
284
     - hash subdirectory based stores.
 
 
285
     - TextStores for revisions and signatures.
 
 
288
    _versionedfile_class = weave.WeaveFile
 
 
289
    _matchingbzrdir = bzrdir.BzrDirFormat6()
 
 
292
        super(RepositoryFormat6, self).__init__()
 
 
294
    def get_format_description(self):
 
 
295
        """See RepositoryFormat.get_format_description()."""
 
 
296
        return "Weave repository format 6"
 
 
298
    def _get_revision_store(self, repo_transport, control_files):
 
 
299
        """See RepositoryFormat._get_revision_store()."""
 
 
300
        return self._get_text_rev_store(repo_transport,
 
 
306
    def _get_text_store(self, transport, control_files):
 
 
307
        """See RepositoryFormat._get_text_store()."""
 
 
308
        return self._get_versioned_file_store('weaves', transport, control_files)
 
 
311
class RepositoryFormat7(MetaDirRepositoryFormat):
 
 
314
    This repository format has:
 
 
315
     - weaves for file texts and inventory
 
 
316
     - hash subdirectory based stores.
 
 
317
     - TextStores for revisions and signatures.
 
 
318
     - a format marker of its own
 
 
319
     - an optional 'shared-storage' flag
 
 
320
     - an optional 'no-working-trees' flag
 
 
323
    _versionedfile_class = weave.WeaveFile
 
 
325
    def _get_control_store(self, repo_transport, control_files):
 
 
326
        """Return the control store for this repository."""
 
 
327
        return self._get_versioned_file_store('',
 
 
332
    def get_format_string(self):
 
 
333
        """See RepositoryFormat.get_format_string()."""
 
 
334
        return "Bazaar-NG Repository format 7"
 
 
336
    def get_format_description(self):
 
 
337
        """See RepositoryFormat.get_format_description()."""
 
 
338
        return "Weave repository format 7"
 
 
340
    def check_conversion_target(self, target_format):
 
 
343
    def _get_revision_store(self, repo_transport, control_files):
 
 
344
        """See RepositoryFormat._get_revision_store()."""
 
 
345
        return self._get_text_rev_store(repo_transport,
 
 
352
    def _get_text_store(self, transport, control_files):
 
 
353
        """See RepositoryFormat._get_text_store()."""
 
 
354
        return self._get_versioned_file_store('weaves',
 
 
358
    def initialize(self, a_bzrdir, shared=False):
 
 
359
        """Create a weave repository.
 
 
361
        :param shared: If true the repository will be initialized as a shared
 
 
364
        # Create an empty weave
 
 
366
        weavefile.write_weave_v5(weave.Weave(), sio)
 
 
367
        empty_weave = sio.getvalue()
 
 
369
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
 
370
        dirs = ['revision-store', 'weaves']
 
 
371
        files = [('inventory.weave', StringIO(empty_weave)), 
 
 
373
        utf8_files = [('format', self.get_format_string())]
 
 
375
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
 
 
376
        return self.open(a_bzrdir=a_bzrdir, _found=True)
 
 
378
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
 
379
        """See RepositoryFormat.open().
 
 
381
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
 
382
                                    repository at a slightly different url
 
 
383
                                    than normal. I.e. during 'upgrade'.
 
 
386
            format = RepositoryFormat.find_format(a_bzrdir)
 
 
387
            assert format.__class__ ==  self.__class__
 
 
388
        if _override_transport is not None:
 
 
389
            repo_transport = _override_transport
 
 
391
            repo_transport = a_bzrdir.get_repository_transport(None)
 
 
392
        control_files = lockable_files.LockableFiles(repo_transport,
 
 
393
                                'lock', lockdir.LockDir)
 
 
394
        text_store = self._get_text_store(repo_transport, control_files)
 
 
395
        control_store = self._get_control_store(repo_transport, control_files)
 
 
396
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
 
397
        return WeaveMetaDirRepository(_format=self,
 
 
399
            control_files=control_files,
 
 
400
            _revision_store=_revision_store,
 
 
401
            control_store=control_store,
 
 
402
            text_store=text_store)
 
 
405
_legacy_formats = [RepositoryFormat4(),