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
 
 
17
"""Deprecated weave-based repository formats.
 
 
19
Weave based formats scaled linearly with history size and could not represent
 
 
23
from StringIO import StringIO
 
 
32
    revision as _mod_revision,
 
 
37
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
 
38
from bzrlib.repository import (
 
 
40
    MetaDirVersionedFileRepository,
 
 
41
    MetaDirRepositoryFormat,
 
 
45
from bzrlib.store.text import TextStore
 
 
46
from bzrlib.symbol_versioning import deprecated_method, one_four
 
 
47
from bzrlib.trace import mutter
 
 
50
class AllInOneRepository(Repository):
 
 
51
    """Legacy support - the repository behaviour for all-in-one branches."""
 
 
53
    _serializer = xml5.serializer_v5
 
 
55
    def __init__(self, _format, a_bzrdir, _revision_store, control_store, text_store):
 
 
56
        # we reuse one control files instance.
 
 
57
        dir_mode = a_bzrdir._control_files._dir_mode
 
 
58
        file_mode = a_bzrdir._control_files._file_mode
 
 
60
        def get_store(name, compressed=True, prefixed=False):
 
 
61
            # FIXME: This approach of assuming stores are all entirely compressed
 
 
62
            # or entirely uncompressed is tidy, but breaks upgrade from 
 
 
63
            # some existing branches where there's a mixture; we probably 
 
 
64
            # still want the option to look for both.
 
 
65
            relpath = a_bzrdir._control_files._escape(name)
 
 
66
            store = TextStore(a_bzrdir.transport.clone(relpath),
 
 
67
                              prefixed=prefixed, compressed=compressed,
 
 
72
        # not broken out yet because the controlweaves|inventory_store
 
 
73
        # and text_store | weave_store bits are still different.
 
 
74
        if isinstance(_format, RepositoryFormat4):
 
 
75
            # cannot remove these - there is still no consistent api 
 
 
76
            # which allows access to this old info.
 
 
77
            self.inventory_store = get_store('inventory-store')
 
 
78
            text_store = get_store('text-store')
 
 
79
        super(AllInOneRepository, self).__init__(_format,
 
 
80
            a_bzrdir, a_bzrdir._control_files, _revision_store, control_store, text_store)
 
 
81
        if control_store is not None:
 
 
82
            control_store.get_scope = self.get_transaction
 
 
83
        text_store.get_scope = self.get_transaction
 
 
86
    def _all_possible_ids(self):
 
 
87
        """Return all the possible revisions that we could find."""
 
 
88
        if 'evil' in debug.debug_flags:
 
 
89
            mutter_callsite(3, "_all_possible_ids scales with size of history.")
 
 
90
        return self.get_inventory_weave().versions()
 
 
93
    def _all_revision_ids(self):
 
 
94
        """Returns a list of all the revision ids in the repository. 
 
 
96
        These are in as much topological order as the underlying store can 
 
 
97
        present: for weaves ghosts may lead to a lack of correctness until
 
 
98
        the reweave updates the parents list.
 
 
100
        if self._revision_store.text_store.listable():
 
 
101
            return self._revision_store.all_revision_ids(self.get_transaction())
 
 
102
        result = self._all_possible_ids()
 
 
103
        # TODO: jam 20070210 Ensure that _all_possible_ids returns non-unicode
 
 
104
        #       ids. (It should, since _revision_store's API should change to
 
 
105
        #       return utf8 revision_ids)
 
 
106
        return self._eliminate_revisions_not_present(result)
 
 
108
    def _check_revision_parents(self, revision, inventory):
 
 
109
        """Private to Repository and Fetch.
 
 
111
        This checks the parentage of revision in an inventory weave for 
 
 
112
        consistency and is only applicable to inventory-weave-for-ancestry
 
 
113
        using repository formats & fetchers.
 
 
115
        weave_parents = inventory.get_parent_map(
 
 
116
            [revision.revision_id])[revision.revision_id]
 
 
117
        parent_map = inventory.get_parent_map(revision.parent_ids)
 
 
118
        for parent_id in revision.parent_ids:
 
 
119
            if parent_id in parent_map:
 
 
120
                # this parent must not be a ghost.
 
 
121
                if not parent_id in weave_parents:
 
 
123
                    raise errors.CorruptRepository(self)
 
 
125
    def get_commit_builder(self, branch, parents, config, timestamp=None,
 
 
126
                           timezone=None, committer=None, revprops=None,
 
 
128
        self._check_ascii_revisionid(revision_id, self.get_commit_builder)
 
 
129
        result = WeaveCommitBuilder(self, parents, config, timestamp, timezone,
 
 
130
                              committer, revprops, revision_id)
 
 
131
        self.start_write_group()
 
 
135
    def get_revisions(self, revision_ids):
 
 
136
        revs = self._get_revisions(revision_ids)
 
 
137
        # weave corruption can lead to absent revision markers that should be
 
 
139
        # the following test is reasonably cheap (it needs a single weave read)
 
 
140
        # and the weave is cached in read transactions. In write transactions
 
 
141
        # it is not cached but typically we only read a small number of
 
 
142
        # revisions. For knits when they are introduced we will probably want
 
 
143
        # to ensure that caching write transactions are in use.
 
 
144
        inv = self.get_inventory_weave()
 
 
146
            self._check_revision_parents(rev, inv)
 
 
149
    @deprecated_method(one_four)
 
 
151
    def get_revision_graph(self, revision_id=None):
 
 
152
        """Return a dictionary containing the revision graph.
 
 
154
        :param revision_id: The revision_id to get a graph from. If None, then
 
 
155
        the entire revision graph is returned. This is a deprecated mode of
 
 
156
        operation and will be removed in the future.
 
 
157
        :return: a dictionary of revision_id->revision_parents_list.
 
 
159
        if 'evil' in debug.debug_flags:
 
 
161
                "get_revision_graph scales with size of history.")
 
 
162
        # special case NULL_REVISION
 
 
163
        if revision_id == _mod_revision.NULL_REVISION:
 
 
165
        a_weave = self.get_inventory_weave()
 
 
166
        all_revisions = self._eliminate_revisions_not_present(
 
 
168
        entire_graph = a_weave.get_parent_map(all_revisions)
 
 
169
        if revision_id is None:
 
 
171
        elif revision_id not in entire_graph:
 
 
172
            raise errors.NoSuchRevision(self, revision_id)
 
 
174
            # add what can be reached from revision_id
 
 
176
            pending = set([revision_id])
 
 
177
            while len(pending) > 0:
 
 
179
                result[node] = entire_graph[node]
 
 
180
                for revision_id in result[node]:
 
 
181
                    if revision_id not in result:
 
 
182
                        pending.add(revision_id)
 
 
185
    def has_revisions(self, revision_ids):
 
 
186
        """See Repository.has_revisions()."""
 
 
188
        transaction = self.get_transaction()
 
 
189
        for revision_id in revision_ids:
 
 
190
            if self._revision_store.has_revision_id(revision_id, transaction):
 
 
191
                result.add(revision_id)
 
 
196
        """AllInOne repositories cannot be shared."""
 
 
200
    def set_make_working_trees(self, new_value):
 
 
201
        """Set the policy flag for making working trees when creating branches.
 
 
203
        This only applies to branches that use this repository.
 
 
205
        The default is 'True'.
 
 
206
        :param new_value: True to restore the default, False to disable making
 
 
209
        raise errors.RepositoryUpgradeRequired(self.bzrdir.root_transport.base)
 
 
211
    def make_working_trees(self):
 
 
212
        """Returns the policy for making working trees on new branches."""
 
 
215
    def revision_graph_can_have_wrong_parents(self):
 
 
216
        # XXX: This is an old format that we don't support full checking on, so
 
 
217
        # just claim that checking for this inconsistency is not required.
 
 
221
class WeaveMetaDirRepository(MetaDirVersionedFileRepository):
 
 
222
    """A subclass of MetaDirRepository to set weave specific policy."""
 
 
224
    _serializer = xml5.serializer_v5
 
 
227
    def _all_possible_ids(self):
 
 
228
        """Return all the possible revisions that we could find."""
 
 
229
        if 'evil' in debug.debug_flags:
 
 
230
            mutter_callsite(3, "_all_possible_ids scales with size of history.")
 
 
231
        return self.get_inventory_weave().versions()
 
 
234
    def _all_revision_ids(self):
 
 
235
        """Returns a list of all the revision ids in the repository. 
 
 
237
        These are in as much topological order as the underlying store can 
 
 
238
        present: for weaves ghosts may lead to a lack of correctness until
 
 
239
        the reweave updates the parents list.
 
 
241
        if self._revision_store.text_store.listable():
 
 
242
            return self._revision_store.all_revision_ids(self.get_transaction())
 
 
243
        result = self._all_possible_ids()
 
 
244
        # TODO: jam 20070210 Ensure that _all_possible_ids returns non-unicode
 
 
245
        #       ids. (It should, since _revision_store's API should change to
 
 
246
        #       return utf8 revision_ids)
 
 
247
        return self._eliminate_revisions_not_present(result)
 
 
249
    def _check_revision_parents(self, revision, inventory):
 
 
250
        """Private to Repository and Fetch.
 
 
252
        This checks the parentage of revision in an inventory weave for 
 
 
253
        consistency and is only applicable to inventory-weave-for-ancestry
 
 
254
        using repository formats & fetchers.
 
 
256
        weave_parents = inventory.get_parent_map(
 
 
257
            [revision.revision_id])[revision.revision_id]
 
 
258
        parent_map = inventory.get_parent_map(revision.parent_ids)
 
 
259
        for parent_id in revision.parent_ids:
 
 
260
            if parent_id in parent_map:
 
 
261
                # this parent must not be a ghost.
 
 
262
                if not parent_id in weave_parents:
 
 
264
                    raise errors.CorruptRepository(self)
 
 
266
    def get_commit_builder(self, branch, parents, config, timestamp=None,
 
 
267
                           timezone=None, committer=None, revprops=None,
 
 
269
        self._check_ascii_revisionid(revision_id, self.get_commit_builder)
 
 
270
        result = WeaveCommitBuilder(self, parents, config, timestamp, timezone,
 
 
271
                              committer, revprops, revision_id)
 
 
272
        self.start_write_group()
 
 
276
    def get_revision(self, revision_id):
 
 
277
        """Return the Revision object for a named revision"""
 
 
278
        # TODO: jam 20070210 get_revision_reconcile should do this for us
 
 
279
        r = self.get_revision_reconcile(revision_id)
 
 
280
        # weave corruption can lead to absent revision markers that should be
 
 
282
        # the following test is reasonably cheap (it needs a single weave read)
 
 
283
        # and the weave is cached in read transactions. In write transactions
 
 
284
        # it is not cached but typically we only read a small number of
 
 
285
        # revisions. For knits when they are introduced we will probably want
 
 
286
        # to ensure that caching write transactions are in use.
 
 
287
        inv = self.get_inventory_weave()
 
 
288
        self._check_revision_parents(r, inv)
 
 
291
    @deprecated_method(one_four)
 
 
293
    def get_revision_graph(self, revision_id=None):
 
 
294
        """Return a dictionary containing the revision graph.
 
 
296
        :param revision_id: The revision_id to get a graph from. If None, then
 
 
297
        the entire revision graph is returned. This is a deprecated mode of
 
 
298
        operation and will be removed in the future.
 
 
299
        :return: a dictionary of revision_id->revision_parents_list.
 
 
301
        if 'evil' in debug.debug_flags:
 
 
303
                "get_revision_graph scales with size of history.")
 
 
304
        # special case NULL_REVISION
 
 
305
        if revision_id == _mod_revision.NULL_REVISION:
 
 
307
        a_weave = self.get_inventory_weave()
 
 
308
        all_revisions = self._eliminate_revisions_not_present(
 
 
310
        entire_graph = a_weave.get_parent_map(all_revisions)
 
 
311
        if revision_id is None:
 
 
313
        elif revision_id not in entire_graph:
 
 
314
            raise errors.NoSuchRevision(self, revision_id)
 
 
316
            # add what can be reached from revision_id
 
 
318
            pending = set([revision_id])
 
 
319
            while len(pending) > 0:
 
 
321
                result[node] = entire_graph[node]
 
 
322
                for revision_id in result[node]:
 
 
323
                    if revision_id not in result:
 
 
324
                        pending.add(revision_id)
 
 
327
    def has_revisions(self, revision_ids):
 
 
328
        """See Repository.has_revisions()."""
 
 
330
        transaction = self.get_transaction()
 
 
331
        for revision_id in revision_ids:
 
 
332
            if self._revision_store.has_revision_id(revision_id, transaction):
 
 
333
                result.add(revision_id)
 
 
336
    def revision_graph_can_have_wrong_parents(self):
 
 
337
        # XXX: This is an old format that we don't support full checking on, so
 
 
338
        # just claim that checking for this inconsistency is not required.
 
 
342
class PreSplitOutRepositoryFormat(RepositoryFormat):
 
 
343
    """Base class for the pre split out repository formats."""
 
 
345
    rich_root_data = False
 
 
346
    supports_tree_reference = False
 
 
347
    supports_ghosts = False
 
 
348
    supports_external_lookups = False
 
 
350
    def initialize(self, a_bzrdir, shared=False, _internal=False):
 
 
351
        """Create a weave repository."""
 
 
353
            raise errors.IncompatibleFormat(self, a_bzrdir._format)
 
 
356
            # always initialized when the bzrdir is.
 
 
357
            return self.open(a_bzrdir, _found=True)
 
 
359
        # Create an empty weave
 
 
361
        weavefile.write_weave_v5(weave.Weave(), sio)
 
 
362
        empty_weave = sio.getvalue()
 
 
364
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
 
366
        # FIXME: RBC 20060125 don't peek under the covers
 
 
367
        # NB: no need to escape relative paths that are url safe.
 
 
368
        control_files = lockable_files.LockableFiles(a_bzrdir.transport,
 
 
369
            'branch-lock', lockable_files.TransportLock)
 
 
370
        control_files.create_lock()
 
 
371
        control_files.lock_write()
 
 
372
        transport = a_bzrdir.transport
 
 
374
            transport.mkdir_multi(['revision-store', 'weaves'],
 
 
375
                mode=control_files._dir_mode)
 
 
376
            transport.put_bytes_non_atomic('inventory.weave', empty_weave)
 
 
378
            control_files.unlock()
 
 
379
        return self.open(a_bzrdir, _found=True)
 
 
381
    def _get_control_store(self, repo_transport, control_files):
 
 
382
        """Return the control store for this repository."""
 
 
383
        return self._get_versioned_file_store('',
 
 
388
    def _get_text_store(self, transport, control_files):
 
 
389
        """Get a store for file texts for this format."""
 
 
390
        raise NotImplementedError(self._get_text_store)
 
 
392
    def open(self, a_bzrdir, _found=False):
 
 
393
        """See RepositoryFormat.open()."""
 
 
395
            # we are being called directly and must probe.
 
 
396
            raise NotImplementedError
 
 
398
        repo_transport = a_bzrdir.get_repository_transport(None)
 
 
399
        control_files = a_bzrdir._control_files
 
 
400
        text_store = self._get_text_store(repo_transport, control_files)
 
 
401
        control_store = self._get_control_store(repo_transport, control_files)
 
 
402
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
 
403
        return AllInOneRepository(_format=self,
 
 
405
                                  _revision_store=_revision_store,
 
 
406
                                  control_store=control_store,
 
 
407
                                  text_store=text_store)
 
 
409
    def check_conversion_target(self, target_format):
 
 
413
class RepositoryFormat4(PreSplitOutRepositoryFormat):
 
 
414
    """Bzr repository format 4.
 
 
416
    This repository format has:
 
 
418
     - TextStores for texts, inventories,revisions.
 
 
420
    This format is deprecated: it indexes texts using a text id which is
 
 
421
    removed in format 5; initialization and write support for this format
 
 
425
    _matchingbzrdir = bzrdir.BzrDirFormat4()
 
 
428
        super(RepositoryFormat4, self).__init__()
 
 
430
    def get_format_description(self):
 
 
431
        """See RepositoryFormat.get_format_description()."""
 
 
432
        return "Repository format 4"
 
 
434
    def initialize(self, url, shared=False, _internal=False):
 
 
435
        """Format 4 branches cannot be created."""
 
 
436
        raise errors.UninitializableFormat(self)
 
 
438
    def is_supported(self):
 
 
439
        """Format 4 is not supported.
 
 
441
        It is not supported because the model changed from 4 to 5 and the
 
 
442
        conversion logic is expensive - so doing it on the fly was not 
 
 
447
    def _get_control_store(self, repo_transport, control_files):
 
 
448
        """Format 4 repositories have no formal control store at this point.
 
 
450
        This will cause any control-file-needing apis to fail - this is desired.
 
 
454
    def _get_revision_store(self, repo_transport, control_files):
 
 
455
        """See RepositoryFormat._get_revision_store()."""
 
 
456
        from bzrlib.xml4 import serializer_v4
 
 
457
        return self._get_text_rev_store(repo_transport,
 
 
460
                                        serializer=serializer_v4)
 
 
462
    def _get_text_store(self, transport, control_files):
 
 
463
        """See RepositoryFormat._get_text_store()."""
 
 
466
class RepositoryFormat5(PreSplitOutRepositoryFormat):
 
 
467
    """Bzr control format 5.
 
 
469
    This repository format has:
 
 
470
     - weaves for file texts and inventory
 
 
472
     - TextStores for revisions and signatures.
 
 
475
    _versionedfile_class = weave.WeaveFile
 
 
476
    _matchingbzrdir = bzrdir.BzrDirFormat5()
 
 
479
        super(RepositoryFormat5, self).__init__()
 
 
481
    def get_format_description(self):
 
 
482
        """See RepositoryFormat.get_format_description()."""
 
 
483
        return "Weave repository format 5"
 
 
485
    def _get_revision_store(self, repo_transport, control_files):
 
 
486
        """See RepositoryFormat._get_revision_store()."""
 
 
487
        """Return the revision store object for this a_bzrdir."""
 
 
488
        return self._get_text_rev_store(repo_transport,
 
 
493
    def _get_text_store(self, transport, control_files):
 
 
494
        """See RepositoryFormat._get_text_store()."""
 
 
495
        return self._get_versioned_file_store('weaves', transport, control_files, prefixed=False)
 
 
498
class RepositoryFormat6(PreSplitOutRepositoryFormat):
 
 
499
    """Bzr control format 6.
 
 
501
    This repository format has:
 
 
502
     - weaves for file texts and inventory
 
 
503
     - hash subdirectory based stores.
 
 
504
     - TextStores for revisions and signatures.
 
 
507
    _versionedfile_class = weave.WeaveFile
 
 
508
    _matchingbzrdir = bzrdir.BzrDirFormat6()
 
 
511
        super(RepositoryFormat6, self).__init__()
 
 
513
    def get_format_description(self):
 
 
514
        """See RepositoryFormat.get_format_description()."""
 
 
515
        return "Weave repository format 6"
 
 
517
    def _get_revision_store(self, repo_transport, control_files):
 
 
518
        """See RepositoryFormat._get_revision_store()."""
 
 
519
        return self._get_text_rev_store(repo_transport,
 
 
525
    def _get_text_store(self, transport, control_files):
 
 
526
        """See RepositoryFormat._get_text_store()."""
 
 
527
        return self._get_versioned_file_store('weaves', transport, control_files)
 
 
529
class RepositoryFormat7(MetaDirRepositoryFormat):
 
 
532
    This repository format has:
 
 
533
     - weaves for file texts and inventory
 
 
534
     - hash subdirectory based stores.
 
 
535
     - TextStores for revisions and signatures.
 
 
536
     - a format marker of its own
 
 
537
     - an optional 'shared-storage' flag
 
 
538
     - an optional 'no-working-trees' flag
 
 
541
    _versionedfile_class = weave.WeaveFile
 
 
542
    supports_ghosts = False
 
 
544
    def _get_control_store(self, repo_transport, control_files):
 
 
545
        """Return the control store for this repository."""
 
 
546
        return self._get_versioned_file_store('',
 
 
551
    def get_format_string(self):
 
 
552
        """See RepositoryFormat.get_format_string()."""
 
 
553
        return "Bazaar-NG Repository format 7"
 
 
555
    def get_format_description(self):
 
 
556
        """See RepositoryFormat.get_format_description()."""
 
 
557
        return "Weave repository format 7"
 
 
559
    def check_conversion_target(self, target_format):
 
 
562
    def _get_revision_store(self, repo_transport, control_files):
 
 
563
        """See RepositoryFormat._get_revision_store()."""
 
 
564
        return self._get_text_rev_store(repo_transport,
 
 
571
    def _get_text_store(self, transport, control_files):
 
 
572
        """See RepositoryFormat._get_text_store()."""
 
 
573
        return self._get_versioned_file_store('weaves',
 
 
577
    def initialize(self, a_bzrdir, shared=False):
 
 
578
        """Create a weave repository.
 
 
580
        :param shared: If true the repository will be initialized as a shared
 
 
583
        # Create an empty weave
 
 
585
        weavefile.write_weave_v5(weave.Weave(), sio)
 
 
586
        empty_weave = sio.getvalue()
 
 
588
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
 
589
        dirs = ['revision-store', 'weaves']
 
 
590
        files = [('inventory.weave', StringIO(empty_weave)), 
 
 
592
        utf8_files = [('format', self.get_format_string())]
 
 
594
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
 
 
595
        return self.open(a_bzrdir=a_bzrdir, _found=True)
 
 
597
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
 
598
        """See RepositoryFormat.open().
 
 
600
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
 
601
                                    repository at a slightly different url
 
 
602
                                    than normal. I.e. during 'upgrade'.
 
 
605
            format = RepositoryFormat.find_format(a_bzrdir)
 
 
606
        if _override_transport is not None:
 
 
607
            repo_transport = _override_transport
 
 
609
            repo_transport = a_bzrdir.get_repository_transport(None)
 
 
610
        control_files = lockable_files.LockableFiles(repo_transport,
 
 
611
                                'lock', lockdir.LockDir)
 
 
612
        text_store = self._get_text_store(repo_transport, control_files)
 
 
613
        control_store = self._get_control_store(repo_transport, control_files)
 
 
614
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
 
615
        return WeaveMetaDirRepository(_format=self,
 
 
617
            control_files=control_files,
 
 
618
            _revision_store=_revision_store,
 
 
619
            control_store=control_store,
 
 
620
            text_store=text_store)
 
 
623
class WeaveCommitBuilder(CommitBuilder):
 
 
624
    """A builder for weave based repos that don't support ghosts."""
 
 
626
    def _add_text_to_weave(self, file_id, new_lines, parents, nostore_sha):
 
 
627
        versionedfile = self.repository.weave_store.get_weave_or_empty(
 
 
628
            file_id, self.repository.get_transaction())
 
 
629
        result = versionedfile.add_lines(
 
 
630
            self._new_revision_id, parents, new_lines,
 
 
631
            nostore_sha=nostore_sha)[0:2]
 
 
635
_legacy_formats = [RepositoryFormat4(),