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
from bzrlib.lazy_import import lazy_import
 
 
18
lazy_import(globals(), """
 
 
22
from bzrlib.store import revision
 
 
23
from bzrlib.store.revision.knit import KnitRevisionStore
 
 
38
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
 
39
from bzrlib.repository import (
 
 
42
    MetaDirRepositoryFormat,
 
 
46
import bzrlib.revision as _mod_revision
 
 
47
from bzrlib.store.versioned import VersionedFileStore
 
 
48
from bzrlib.trace import mutter, mutter_callsite
 
 
49
from bzrlib.util import bencode
 
 
52
class _KnitParentsProvider(object):
 
 
54
    def __init__(self, knit):
 
 
58
        return 'KnitParentsProvider(%r)' % self._knit
 
 
60
    def get_parents(self, revision_ids):
 
 
62
        for revision_id in revision_ids:
 
 
63
            if revision_id == _mod_revision.NULL_REVISION:
 
 
67
                    parents = self._knit.get_parents_with_ghosts(revision_id)
 
 
68
                except errors.RevisionNotPresent:
 
 
72
                        parents = [_mod_revision.NULL_REVISION]
 
 
73
            parents_list.append(parents)
 
 
77
class KnitRepository(MetaDirRepository):
 
 
78
    """Knit format repository."""
 
 
80
    # These attributes are inherited from the Repository base class. Setting
 
 
81
    # them to None ensures that if the constructor is changed to not initialize
 
 
82
    # them, or a subclass fails to call the constructor, that an error will
 
 
83
    # occur rather than the system working but generating incorrect data.
 
 
84
    _commit_builder_class = None
 
 
87
    def __init__(self, _format, a_bzrdir, control_files, _revision_store,
 
 
88
        control_store, text_store, _commit_builder_class, _serializer):
 
 
89
        MetaDirRepository.__init__(self, _format, a_bzrdir, control_files,
 
 
90
            _revision_store, control_store, text_store)
 
 
91
        self._commit_builder_class = _commit_builder_class
 
 
92
        self._serializer = _serializer
 
 
93
        self._reconcile_fixes_text_parents = True
 
 
95
    def _warn_if_deprecated(self):
 
 
96
        # This class isn't deprecated
 
 
99
    def _inventory_add_lines(self, inv_vf, revid, parents, lines, check_content):
 
 
100
        return inv_vf.add_lines_with_ghosts(revid, parents, lines,
 
 
101
            check_content=check_content)[0]
 
 
104
    def _all_revision_ids(self):
 
 
105
        """See Repository.all_revision_ids()."""
 
 
106
        # Knits get the revision graph from the index of the revision knit, so
 
 
107
        # it's always possible even if they're on an unlistable transport.
 
 
108
        return self._revision_store.all_revision_ids(self.get_transaction())
 
 
110
    def fileid_involved_between_revs(self, from_revid, to_revid):
 
 
111
        """Find file_id(s) which are involved in the changes between revisions.
 
 
113
        This determines the set of revisions which are involved, and then
 
 
114
        finds all file ids affected by those revisions.
 
 
116
        vf = self._get_revision_vf()
 
 
117
        from_set = set(vf.get_ancestry(from_revid))
 
 
118
        to_set = set(vf.get_ancestry(to_revid))
 
 
119
        changed = to_set.difference(from_set)
 
 
120
        return self._fileid_involved_by_set(changed)
 
 
122
    def fileid_involved(self, last_revid=None):
 
 
123
        """Find all file_ids modified in the ancestry of last_revid.
 
 
125
        :param last_revid: If None, last_revision() will be used.
 
 
128
            changed = set(self.all_revision_ids())
 
 
130
            changed = set(self.get_ancestry(last_revid))
 
 
133
        return self._fileid_involved_by_set(changed)
 
 
136
    def get_ancestry(self, revision_id, topo_sorted=True):
 
 
137
        """Return a list of revision-ids integrated by a revision.
 
 
139
        This is topologically sorted, unless 'topo_sorted' is specified as
 
 
142
        if _mod_revision.is_null(revision_id):
 
 
144
        vf = self._get_revision_vf()
 
 
146
            return [None] + vf.get_ancestry(revision_id, topo_sorted)
 
 
147
        except errors.RevisionNotPresent:
 
 
148
            raise errors.NoSuchRevision(self, revision_id)
 
 
151
    def get_data_stream(self, revision_ids):
 
 
152
        """See Repository.get_data_stream."""
 
 
153
        item_keys = self.item_keys_introduced_by(revision_ids)
 
 
154
        for knit_kind, file_id, versions in item_keys:
 
 
156
            if knit_kind == 'file':
 
 
157
                name = ('file', file_id)
 
 
158
                knit = self.weave_store.get_weave_or_empty(
 
 
159
                    file_id, self.get_transaction())
 
 
160
            elif knit_kind == 'inventory':
 
 
161
                knit = self.get_inventory_weave()
 
 
162
            elif knit_kind == 'revisions':
 
 
163
                knit = self._revision_store.get_revision_file(
 
 
164
                    self.get_transaction())
 
 
165
            elif knit_kind == 'signatures':
 
 
166
                knit = self._revision_store.get_signature_file(
 
 
167
                    self.get_transaction())
 
 
169
                raise AssertionError('Unknown knit kind %r' % (knit_kind,))
 
 
170
            yield name, _get_stream_as_bytes(knit, versions)
 
 
173
    def get_revision(self, revision_id):
 
 
174
        """Return the Revision object for a named revision"""
 
 
175
        revision_id = osutils.safe_revision_id(revision_id)
 
 
176
        return self.get_revision_reconcile(revision_id)
 
 
179
    def get_revision_graph(self, revision_id=None):
 
 
180
        """Return a dictionary containing the revision graph.
 
 
182
        :param revision_id: The revision_id to get a graph from. If None, then
 
 
183
        the entire revision graph is returned. This is a deprecated mode of
 
 
184
        operation and will be removed in the future.
 
 
185
        :return: a dictionary of revision_id->revision_parents_list.
 
 
187
        if 'evil' in debug.debug_flags:
 
 
189
                "get_revision_graph scales with size of history.")
 
 
190
        # special case NULL_REVISION
 
 
191
        if revision_id == _mod_revision.NULL_REVISION:
 
 
193
        a_weave = self._get_revision_vf()
 
 
194
        if revision_id is None:
 
 
195
            return a_weave.get_graph()
 
 
196
        if revision_id not in a_weave:
 
 
197
            raise errors.NoSuchRevision(self, revision_id)
 
 
199
            # add what can be reached from revision_id
 
 
200
            return a_weave.get_graph([revision_id])
 
 
203
    def get_revision_graph_with_ghosts(self, revision_ids=None):
 
 
204
        """Return a graph of the revisions with ghosts marked as applicable.
 
 
206
        :param revision_ids: an iterable of revisions to graph or None for all.
 
 
207
        :return: a Graph object with the graph reachable from revision_ids.
 
 
209
        if 'evil' in debug.debug_flags:
 
 
211
                "get_revision_graph_with_ghosts scales with size of history.")
 
 
212
        result = deprecated_graph.Graph()
 
 
213
        vf = self._get_revision_vf()
 
 
214
        versions = set(vf.versions())
 
 
216
            pending = set(self.all_revision_ids())
 
 
219
            pending = set(revision_ids)
 
 
220
            # special case NULL_REVISION
 
 
221
            if _mod_revision.NULL_REVISION in pending:
 
 
222
                pending.remove(_mod_revision.NULL_REVISION)
 
 
223
            required = set(pending)
 
 
226
            revision_id = pending.pop()
 
 
227
            if not revision_id in versions:
 
 
228
                if revision_id in required:
 
 
229
                    raise errors.NoSuchRevision(self, revision_id)
 
 
231
                result.add_ghost(revision_id)
 
 
232
                # mark it as done so we don't try for it again.
 
 
233
                done.add(revision_id)
 
 
235
            parent_ids = vf.get_parents_with_ghosts(revision_id)
 
 
236
            for parent_id in parent_ids:
 
 
237
                # is this queued or done ?
 
 
238
                if (parent_id not in pending and
 
 
239
                    parent_id not in done):
 
 
241
                    pending.add(parent_id)
 
 
242
            result.add_node(revision_id, parent_ids)
 
 
243
            done.add(revision_id)
 
 
246
    def _get_revision_vf(self):
 
 
247
        """:return: a versioned file containing the revisions."""
 
 
248
        vf = self._revision_store.get_revision_file(self.get_transaction())
 
 
251
    def _get_history_vf(self):
 
 
252
        """Get a versionedfile whose history graph reflects all revisions.
 
 
254
        For knit repositories, this is the revision knit.
 
 
256
        return self._get_revision_vf()
 
 
259
    def reconcile(self, other=None, thorough=False):
 
 
260
        """Reconcile this repository."""
 
 
261
        from bzrlib.reconcile import KnitReconciler
 
 
262
        reconciler = KnitReconciler(self, thorough=thorough)
 
 
263
        reconciler.reconcile()
 
 
266
    def revision_parents(self, revision_id):
 
 
267
        return self._get_revision_vf().get_parents(revision_id)
 
 
269
    def _make_parents_provider(self):
 
 
270
        return _KnitParentsProvider(self._get_revision_vf())
 
 
272
    def _find_inconsistent_revision_parents(self):
 
 
273
        """Find revisions with different parent lists in the revision object
 
 
274
        and in the index graph.
 
 
276
        :returns: an iterator yielding tuples of (revison-id, parents-in-index,
 
 
277
            parents-in-revision).
 
 
279
        assert self.is_locked()
 
 
280
        vf = self._get_revision_vf()
 
 
281
        for index_version in vf.versions():
 
 
282
            parents_according_to_index = tuple(vf.get_parents_with_ghosts(
 
 
284
            revision = self.get_revision(index_version)
 
 
285
            parents_according_to_revision = tuple(revision.parent_ids)
 
 
286
            if parents_according_to_index != parents_according_to_revision:
 
 
287
                yield (index_version, parents_according_to_index,
 
 
288
                    parents_according_to_revision)
 
 
290
    def _check_for_inconsistent_revision_parents(self):
 
 
291
        inconsistencies = list(self._find_inconsistent_revision_parents())
 
 
293
            raise errors.BzrCheckError(
 
 
294
                "Revision knit has inconsistent parents.")
 
 
296
    def revision_graph_can_have_wrong_parents(self):
 
 
297
        # The revision.kndx could potentially claim a revision has a different
 
 
298
        # parent to the revision text.
 
 
302
class RepositoryFormatKnit(MetaDirRepositoryFormat):
 
 
303
    """Bzr repository knit format (generalized). 
 
 
305
    This repository format has:
 
 
306
     - knits for file texts and inventory
 
 
307
     - hash subdirectory based stores.
 
 
308
     - knits for revisions and signatures
 
 
309
     - TextStores for revisions and signatures.
 
 
310
     - a format marker of its own
 
 
311
     - an optional 'shared-storage' flag
 
 
312
     - an optional 'no-working-trees' flag
 
 
316
    # Set this attribute in derived classes to control the repository class
 
 
317
    # created by open and initialize.
 
 
318
    repository_class = None
 
 
319
    # Set this attribute in derived classes to control the
 
 
320
    # _commit_builder_class that the repository objects will have passed to
 
 
322
    _commit_builder_class = None
 
 
323
    # Set this attribute in derived clases to control the _serializer that the
 
 
324
    # repository objects will have passed to their constructor.
 
 
325
    _serializer = xml5.serializer_v5
 
 
326
    # Knit based repositories handle ghosts reasonably well.
 
 
327
    supports_ghosts = True
 
 
329
    def _get_control_store(self, repo_transport, control_files):
 
 
330
        """Return the control store for this repository."""
 
 
331
        return VersionedFileStore(
 
 
334
            file_mode=control_files._file_mode,
 
 
335
            versionedfile_class=knit.KnitVersionedFile,
 
 
336
            versionedfile_kwargs={'factory':knit.KnitPlainFactory()},
 
 
339
    def _get_revision_store(self, repo_transport, control_files):
 
 
340
        """See RepositoryFormat._get_revision_store()."""
 
 
341
        versioned_file_store = VersionedFileStore(
 
 
343
            file_mode=control_files._file_mode,
 
 
346
            versionedfile_class=knit.KnitVersionedFile,
 
 
347
            versionedfile_kwargs={'delta':False,
 
 
348
                                  'factory':knit.KnitPlainFactory(),
 
 
352
        return KnitRevisionStore(versioned_file_store)
 
 
354
    def _get_text_store(self, transport, control_files):
 
 
355
        """See RepositoryFormat._get_text_store()."""
 
 
356
        return self._get_versioned_file_store('knits',
 
 
359
                                  versionedfile_class=knit.KnitVersionedFile,
 
 
360
                                  versionedfile_kwargs={
 
 
361
                                      'create_parent_dir':True,
 
 
363
                                      'dir_mode':control_files._dir_mode,
 
 
367
    def initialize(self, a_bzrdir, shared=False):
 
 
368
        """Create a knit format 1 repository.
 
 
370
        :param a_bzrdir: bzrdir to contain the new repository; must already
 
 
372
        :param shared: If true the repository will be initialized as a shared
 
 
375
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
 
378
        utf8_files = [('format', self.get_format_string())]
 
 
380
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
 
 
381
        repo_transport = a_bzrdir.get_repository_transport(None)
 
 
382
        control_files = lockable_files.LockableFiles(repo_transport,
 
 
383
                                'lock', lockdir.LockDir)
 
 
384
        control_store = self._get_control_store(repo_transport, control_files)
 
 
385
        transaction = transactions.WriteTransaction()
 
 
386
        # trigger a write of the inventory store.
 
 
387
        control_store.get_weave_or_empty('inventory', transaction)
 
 
388
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
 
389
        # the revision id here is irrelevant: it will not be stored, and cannot
 
 
391
        _revision_store.has_revision_id('A', transaction)
 
 
392
        _revision_store.get_signature_file(transaction)
 
 
393
        return self.open(a_bzrdir=a_bzrdir, _found=True)
 
 
395
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
 
396
        """See RepositoryFormat.open().
 
 
398
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
 
399
                                    repository at a slightly different url
 
 
400
                                    than normal. I.e. during 'upgrade'.
 
 
403
            format = RepositoryFormat.find_format(a_bzrdir)
 
 
404
            assert format.__class__ ==  self.__class__
 
 
405
        if _override_transport is not None:
 
 
406
            repo_transport = _override_transport
 
 
408
            repo_transport = a_bzrdir.get_repository_transport(None)
 
 
409
        control_files = lockable_files.LockableFiles(repo_transport,
 
 
410
                                'lock', lockdir.LockDir)
 
 
411
        text_store = self._get_text_store(repo_transport, control_files)
 
 
412
        control_store = self._get_control_store(repo_transport, control_files)
 
 
413
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
 
414
        return self.repository_class(_format=self,
 
 
416
                              control_files=control_files,
 
 
417
                              _revision_store=_revision_store,
 
 
418
                              control_store=control_store,
 
 
419
                              text_store=text_store,
 
 
420
                              _commit_builder_class=self._commit_builder_class,
 
 
421
                              _serializer=self._serializer)
 
 
424
class RepositoryFormatKnit1(RepositoryFormatKnit):
 
 
425
    """Bzr repository knit format 1.
 
 
427
    This repository format has:
 
 
428
     - knits for file texts and inventory
 
 
429
     - hash subdirectory based stores.
 
 
430
     - knits for revisions and signatures
 
 
431
     - TextStores for revisions and signatures.
 
 
432
     - a format marker of its own
 
 
433
     - an optional 'shared-storage' flag
 
 
434
     - an optional 'no-working-trees' flag
 
 
437
    This format was introduced in bzr 0.8.
 
 
440
    repository_class = KnitRepository
 
 
441
    _commit_builder_class = CommitBuilder
 
 
442
    _serializer = xml5.serializer_v5
 
 
444
    def __ne__(self, other):
 
 
445
        return self.__class__ is not other.__class__
 
 
447
    def get_format_string(self):
 
 
448
        """See RepositoryFormat.get_format_string()."""
 
 
449
        return "Bazaar-NG Knit Repository Format 1"
 
 
451
    def get_format_description(self):
 
 
452
        """See RepositoryFormat.get_format_description()."""
 
 
453
        return "Knit repository format 1"
 
 
455
    def check_conversion_target(self, target_format):
 
 
459
class RepositoryFormatKnit3(RepositoryFormatKnit):
 
 
460
    """Bzr repository knit format 2.
 
 
462
    This repository format has:
 
 
463
     - knits for file texts and inventory
 
 
464
     - hash subdirectory based stores.
 
 
465
     - knits for revisions and signatures
 
 
466
     - TextStores for revisions and signatures.
 
 
467
     - a format marker of its own
 
 
468
     - an optional 'shared-storage' flag
 
 
469
     - an optional 'no-working-trees' flag
 
 
471
     - support for recording full info about the tree root
 
 
472
     - support for recording tree-references
 
 
475
    repository_class = KnitRepository
 
 
476
    _commit_builder_class = RootCommitBuilder
 
 
477
    rich_root_data = True
 
 
478
    supports_tree_reference = True
 
 
479
    _serializer = xml7.serializer_v7
 
 
481
    def _get_matching_bzrdir(self):
 
 
482
        return bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
 
 
484
    def _ignore_setting_bzrdir(self, format):
 
 
487
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
 
 
489
    def check_conversion_target(self, target_format):
 
 
490
        if not target_format.rich_root_data:
 
 
491
            raise errors.BadConversionTarget(
 
 
492
                'Does not support rich root data.', target_format)
 
 
493
        if not getattr(target_format, 'supports_tree_reference', False):
 
 
494
            raise errors.BadConversionTarget(
 
 
495
                'Does not support nested trees', target_format)
 
 
497
    def get_format_string(self):
 
 
498
        """See RepositoryFormat.get_format_string()."""
 
 
499
        return "Bazaar Knit Repository Format 3 (bzr 0.15)\n"
 
 
501
    def get_format_description(self):
 
 
502
        """See RepositoryFormat.get_format_description()."""
 
 
503
        return "Knit repository format 3"
 
 
506
def _get_stream_as_bytes(knit, required_versions):
 
 
507
    """Generate a serialised data stream.
 
 
509
    The format is a bencoding of a list.  The first element of the list is a
 
 
510
    string of the format signature, then each subsequent element is a list
 
 
511
    corresponding to a record.  Those lists contain:
 
 
518
    :returns: a bencoded list.
 
 
520
    knit_stream = knit.get_data_stream(required_versions)
 
 
521
    format_signature, data_list, callable = knit_stream
 
 
523
    data.append(format_signature)
 
 
524
    for version, options, length, parents in data_list:
 
 
525
        data.append([version, options, parents, callable(length)])
 
 
526
    return bencode.bencode(data)