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
 
 
31
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
 
32
from bzrlib.repository import (
 
 
34
    MetaDirRepositoryFormat,
 
 
38
import bzrlib.revision as _mod_revision
 
 
39
from bzrlib.store.versioned import VersionedFileStore
 
 
40
from bzrlib.trace import mutter, note, warning
 
 
43
class KnitRepository(MetaDirRepository):
 
 
44
    """Knit format repository."""
 
 
47
    _serializer = xml5.serializer_v5
 
 
49
    def _warn_if_deprecated(self):
 
 
50
        # This class isn't deprecated
 
 
53
    def _inventory_add_lines(self, inv_vf, revid, parents, lines):
 
 
54
        inv_vf.add_lines_with_ghosts(revid, parents, lines)
 
 
57
    def _all_revision_ids(self):
 
 
58
        """See Repository.all_revision_ids()."""
 
 
59
        # Knits get the revision graph from the index of the revision knit, so
 
 
60
        # it's always possible even if they're on an unlistable transport.
 
 
61
        return self._revision_store.all_revision_ids(self.get_transaction())
 
 
63
    def fileid_involved_between_revs(self, from_revid, to_revid):
 
 
64
        """Find file_id(s) which are involved in the changes between revisions.
 
 
66
        This determines the set of revisions which are involved, and then
 
 
67
        finds all file ids affected by those revisions.
 
 
69
        from_revid = osutils.safe_revision_id(from_revid)
 
 
70
        to_revid = osutils.safe_revision_id(to_revid)
 
 
71
        vf = self._get_revision_vf()
 
 
72
        from_set = set(vf.get_ancestry(from_revid))
 
 
73
        to_set = set(vf.get_ancestry(to_revid))
 
 
74
        changed = to_set.difference(from_set)
 
 
75
        return self._fileid_involved_by_set(changed)
 
 
77
    def fileid_involved(self, last_revid=None):
 
 
78
        """Find all file_ids modified in the ancestry of last_revid.
 
 
80
        :param last_revid: If None, last_revision() will be used.
 
 
83
            changed = set(self.all_revision_ids())
 
 
85
            changed = set(self.get_ancestry(last_revid))
 
 
88
        return self._fileid_involved_by_set(changed)
 
 
91
    def get_ancestry(self, revision_id):
 
 
92
        """Return a list of revision-ids integrated by a revision.
 
 
94
        This is topologically sorted.
 
 
96
        if revision_id is None:
 
 
98
        revision_id = osutils.safe_revision_id(revision_id)
 
 
99
        vf = self._get_revision_vf()
 
 
101
            return [None] + vf.get_ancestry(revision_id)
 
 
102
        except errors.RevisionNotPresent:
 
 
103
            raise errors.NoSuchRevision(self, revision_id)
 
 
106
    def get_revision(self, revision_id):
 
 
107
        """Return the Revision object for a named revision"""
 
 
108
        revision_id = osutils.safe_revision_id(revision_id)
 
 
109
        return self.get_revision_reconcile(revision_id)
 
 
112
    def get_revision_graph(self, revision_id=None):
 
 
113
        """Return a dictionary containing the revision graph.
 
 
115
        :param revision_id: The revision_id to get a graph from. If None, then
 
 
116
        the entire revision graph is returned. This is a deprecated mode of
 
 
117
        operation and will be removed in the future.
 
 
118
        :return: a dictionary of revision_id->revision_parents_list.
 
 
120
        # special case NULL_REVISION
 
 
121
        if revision_id == _mod_revision.NULL_REVISION:
 
 
123
        revision_id = osutils.safe_revision_id(revision_id)
 
 
124
        a_weave = self._get_revision_vf()
 
 
125
        entire_graph = a_weave.get_graph()
 
 
126
        if revision_id is None:
 
 
127
            return a_weave.get_graph()
 
 
128
        elif revision_id not in a_weave:
 
 
129
            raise errors.NoSuchRevision(self, revision_id)
 
 
131
            # add what can be reached from revision_id
 
 
133
            pending = set([revision_id])
 
 
134
            while len(pending) > 0:
 
 
136
                result[node] = a_weave.get_parents(node)
 
 
137
                for revision_id in result[node]:
 
 
138
                    if revision_id not in result:
 
 
139
                        pending.add(revision_id)
 
 
143
    def get_revision_graph_with_ghosts(self, revision_ids=None):
 
 
144
        """Return a graph of the revisions with ghosts marked as applicable.
 
 
146
        :param revision_ids: an iterable of revisions to graph or None for all.
 
 
147
        :return: a Graph object with the graph reachable from revision_ids.
 
 
149
        result = graph.Graph()
 
 
150
        vf = self._get_revision_vf()
 
 
151
        versions = set(vf.versions())
 
 
153
            pending = set(self.all_revision_ids())
 
 
156
            pending = set(osutils.safe_revision_id(r) for r in revision_ids)
 
 
157
            # special case NULL_REVISION
 
 
158
            if _mod_revision.NULL_REVISION in pending:
 
 
159
                pending.remove(_mod_revision.NULL_REVISION)
 
 
160
            required = set(pending)
 
 
163
            revision_id = pending.pop()
 
 
164
            if not revision_id in versions:
 
 
165
                if revision_id in required:
 
 
166
                    raise errors.NoSuchRevision(self, revision_id)
 
 
168
                result.add_ghost(revision_id)
 
 
169
                # mark it as done so we don't try for it again.
 
 
170
                done.add(revision_id)
 
 
172
            parent_ids = vf.get_parents_with_ghosts(revision_id)
 
 
173
            for parent_id in parent_ids:
 
 
174
                # is this queued or done ?
 
 
175
                if (parent_id not in pending and
 
 
176
                    parent_id not in done):
 
 
178
                    pending.add(parent_id)
 
 
179
            result.add_node(revision_id, parent_ids)
 
 
180
            done.add(revision_id)
 
 
183
    def _get_revision_vf(self):
 
 
184
        """:return: a versioned file containing the revisions."""
 
 
185
        vf = self._revision_store.get_revision_file(self.get_transaction())
 
 
188
    def _get_history_vf(self):
 
 
189
        """Get a versionedfile whose history graph reflects all revisions.
 
 
191
        For knit repositories, this is the revision knit.
 
 
193
        return self._get_revision_vf()
 
 
196
    def reconcile(self, other=None, thorough=False):
 
 
197
        """Reconcile this repository."""
 
 
198
        from bzrlib.reconcile import KnitReconciler
 
 
199
        reconciler = KnitReconciler(self, thorough=thorough)
 
 
200
        reconciler.reconcile()
 
 
203
    def revision_parents(self, revision_id):
 
 
204
        revision_id = osutils.safe_revision_id(revision_id)
 
 
205
        return self._get_revision_vf().get_parents(revision_id)
 
 
208
class KnitRepository2(KnitRepository):
 
 
210
    def __init__(self, _format, a_bzrdir, control_files, _revision_store,
 
 
211
                 control_store, text_store):
 
 
212
        KnitRepository.__init__(self, _format, a_bzrdir, control_files,
 
 
213
                              _revision_store, control_store, text_store)
 
 
214
        self._serializer = xml6.serializer_v6
 
 
216
    def deserialise_inventory(self, revision_id, xml):
 
 
217
        """Transform the xml into an inventory object. 
 
 
219
        :param revision_id: The expected revision id of the inventory.
 
 
220
        :param xml: A serialised inventory.
 
 
222
        result = self._serializer.read_inventory_from_string(xml)
 
 
223
        assert result.root.revision is not None
 
 
226
    def serialise_inventory(self, inv):
 
 
227
        """Transform the inventory object into XML text.
 
 
229
        :param revision_id: The expected revision id of the inventory.
 
 
230
        :param xml: A serialised inventory.
 
 
232
        assert inv.revision_id is not None
 
 
233
        assert inv.root.revision is not None
 
 
234
        return KnitRepository.serialise_inventory(self, inv)
 
 
236
    def get_commit_builder(self, branch, parents, config, timestamp=None,
 
 
237
                           timezone=None, committer=None, revprops=None,
 
 
239
        """Obtain a CommitBuilder for this repository.
 
 
241
        :param branch: Branch to commit to.
 
 
242
        :param parents: Revision ids of the parents of the new revision.
 
 
243
        :param config: Configuration to use.
 
 
244
        :param timestamp: Optional timestamp recorded for commit.
 
 
245
        :param timezone: Optional timezone for timestamp.
 
 
246
        :param committer: Optional committer to set for commit.
 
 
247
        :param revprops: Optional dictionary of revision properties.
 
 
248
        :param revision_id: Optional revision id.
 
 
250
        revision_id = osutils.safe_revision_id(revision_id)
 
 
251
        return RootCommitBuilder(self, parents, config, timestamp, timezone,
 
 
252
                                 committer, revprops, revision_id)
 
 
255
class KnitRepository3(KnitRepository2):
 
 
257
    def __init__(self, _format, a_bzrdir, control_files, _revision_store,
 
 
258
                 control_store, text_store):
 
 
259
        KnitRepository2.__init__(self, _format, a_bzrdir, control_files,
 
 
260
                                 _revision_store, control_store, text_store)
 
 
261
        self._serializer = xml7.serializer_v7
 
 
264
class RepositoryFormatKnit(MetaDirRepositoryFormat):
 
 
265
    """Bzr repository knit format (generalized). 
 
 
267
    This repository format has:
 
 
268
     - knits for file texts and inventory
 
 
269
     - hash subdirectory based stores.
 
 
270
     - knits for revisions and signatures
 
 
271
     - TextStores for revisions and signatures.
 
 
272
     - a format marker of its own
 
 
273
     - an optional 'shared-storage' flag
 
 
274
     - an optional 'no-working-trees' flag
 
 
278
    def _get_control_store(self, repo_transport, control_files):
 
 
279
        """Return the control store for this repository."""
 
 
280
        return VersionedFileStore(
 
 
283
            file_mode=control_files._file_mode,
 
 
284
            versionedfile_class=knit.KnitVersionedFile,
 
 
285
            versionedfile_kwargs={'factory':knit.KnitPlainFactory()},
 
 
288
    def _get_revision_store(self, repo_transport, control_files):
 
 
289
        """See RepositoryFormat._get_revision_store()."""
 
 
290
        from bzrlib.store.revision.knit import KnitRevisionStore
 
 
291
        versioned_file_store = VersionedFileStore(
 
 
293
            file_mode=control_files._file_mode,
 
 
296
            versionedfile_class=knit.KnitVersionedFile,
 
 
297
            versionedfile_kwargs={'delta':False,
 
 
298
                                  'factory':knit.KnitPlainFactory(),
 
 
302
        return KnitRevisionStore(versioned_file_store)
 
 
304
    def _get_text_store(self, transport, control_files):
 
 
305
        """See RepositoryFormat._get_text_store()."""
 
 
306
        return self._get_versioned_file_store('knits',
 
 
309
                                  versionedfile_class=knit.KnitVersionedFile,
 
 
310
                                  versionedfile_kwargs={
 
 
311
                                      'create_parent_dir':True,
 
 
313
                                      'dir_mode':control_files._dir_mode,
 
 
317
    def initialize(self, a_bzrdir, shared=False):
 
 
318
        """Create a knit format 1 repository.
 
 
320
        :param a_bzrdir: bzrdir to contain the new repository; must already
 
 
322
        :param shared: If true the repository will be initialized as a shared
 
 
325
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
 
326
        dirs = ['revision-store', 'knits']
 
 
328
        utf8_files = [('format', self.get_format_string())]
 
 
330
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
 
 
331
        repo_transport = a_bzrdir.get_repository_transport(None)
 
 
332
        control_files = lockable_files.LockableFiles(repo_transport,
 
 
333
                                'lock', lockdir.LockDir)
 
 
334
        control_store = self._get_control_store(repo_transport, control_files)
 
 
335
        transaction = transactions.WriteTransaction()
 
 
336
        # trigger a write of the inventory store.
 
 
337
        control_store.get_weave_or_empty('inventory', transaction)
 
 
338
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
 
339
        # the revision id here is irrelevant: it will not be stored, and cannot
 
 
341
        _revision_store.has_revision_id('A', transaction)
 
 
342
        _revision_store.get_signature_file(transaction)
 
 
343
        return self.open(a_bzrdir=a_bzrdir, _found=True)
 
 
345
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
 
346
        """See RepositoryFormat.open().
 
 
348
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
 
349
                                    repository at a slightly different url
 
 
350
                                    than normal. I.e. during 'upgrade'.
 
 
353
            format = RepositoryFormat.find_format(a_bzrdir)
 
 
354
            assert format.__class__ ==  self.__class__
 
 
355
        if _override_transport is not None:
 
 
356
            repo_transport = _override_transport
 
 
358
            repo_transport = a_bzrdir.get_repository_transport(None)
 
 
359
        control_files = lockable_files.LockableFiles(repo_transport,
 
 
360
                                'lock', lockdir.LockDir)
 
 
361
        text_store = self._get_text_store(repo_transport, control_files)
 
 
362
        control_store = self._get_control_store(repo_transport, control_files)
 
 
363
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
 
364
        return KnitRepository(_format=self,
 
 
366
                              control_files=control_files,
 
 
367
                              _revision_store=_revision_store,
 
 
368
                              control_store=control_store,
 
 
369
                              text_store=text_store)
 
 
372
class RepositoryFormatKnit1(RepositoryFormatKnit):
 
 
373
    """Bzr repository knit format 1.
 
 
375
    This repository format has:
 
 
376
     - knits for file texts and inventory
 
 
377
     - hash subdirectory based stores.
 
 
378
     - knits for revisions and signatures
 
 
379
     - TextStores for revisions and signatures.
 
 
380
     - a format marker of its own
 
 
381
     - an optional 'shared-storage' flag
 
 
382
     - an optional 'no-working-trees' flag
 
 
385
    This format was introduced in bzr 0.8.
 
 
388
    def __ne__(self, other):
 
 
389
        return self.__class__ is not other.__class__
 
 
391
    def get_format_string(self):
 
 
392
        """See RepositoryFormat.get_format_string()."""
 
 
393
        return "Bazaar-NG Knit Repository Format 1"
 
 
395
    def get_format_description(self):
 
 
396
        """See RepositoryFormat.get_format_description()."""
 
 
397
        return "Knit repository format 1"
 
 
399
    def check_conversion_target(self, target_format):
 
 
403
class RepositoryFormatKnit2(RepositoryFormatKnit):
 
 
404
    """Bzr repository knit format 2.
 
 
406
    THIS FORMAT IS EXPERIMENTAL
 
 
407
    This repository format has:
 
 
408
     - knits for file texts and inventory
 
 
409
     - hash subdirectory based stores.
 
 
410
     - knits for revisions and signatures
 
 
411
     - TextStores for revisions and signatures.
 
 
412
     - a format marker of its own
 
 
413
     - an optional 'shared-storage' flag
 
 
414
     - an optional 'no-working-trees' flag
 
 
416
     - Support for recording full info about the tree root
 
 
420
    rich_root_data = True
 
 
421
    repository_class = KnitRepository2
 
 
423
    def get_format_string(self):
 
 
424
        """See RepositoryFormat.get_format_string()."""
 
 
425
        return "Bazaar Knit Repository Format 2\n"
 
 
427
    def get_format_description(self):
 
 
428
        """See RepositoryFormat.get_format_description()."""
 
 
429
        return "Knit repository format 2"
 
 
431
    def check_conversion_target(self, target_format):
 
 
432
        if not target_format.rich_root_data:
 
 
433
            raise errors.BadConversionTarget(
 
 
434
                'Does not support rich root data.', target_format)
 
 
436
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
 
437
        """See RepositoryFormat.open().
 
 
439
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
 
440
                                    repository at a slightly different url
 
 
441
                                    than normal. I.e. during 'upgrade'.
 
 
444
            format = RepositoryFormat.find_format(a_bzrdir)
 
 
445
            assert format.__class__ ==  self.__class__
 
 
446
        if _override_transport is not None:
 
 
447
            repo_transport = _override_transport
 
 
449
            repo_transport = a_bzrdir.get_repository_transport(None)
 
 
450
        control_files = lockable_files.LockableFiles(repo_transport, 'lock',
 
 
452
        text_store = self._get_text_store(repo_transport, control_files)
 
 
453
        control_store = self._get_control_store(repo_transport, control_files)
 
 
454
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
 
455
        return self.repository_class(_format=self,
 
 
457
                                     control_files=control_files,
 
 
458
                                     _revision_store=_revision_store,
 
 
459
                                     control_store=control_store,
 
 
460
                                     text_store=text_store)
 
 
463
class RepositoryFormatKnit3(RepositoryFormatKnit2):
 
 
464
    """Bzr repository knit format 2.
 
 
466
    THIS FORMAT IS EXPERIMENTAL
 
 
467
    This repository format has:
 
 
468
     - knits for file texts and inventory
 
 
469
     - hash subdirectory based stores.
 
 
470
     - knits for revisions and signatures
 
 
471
     - TextStores for revisions and signatures.
 
 
472
     - a format marker of its own
 
 
473
     - an optional 'shared-storage' flag
 
 
474
     - an optional 'no-working-trees' flag
 
 
476
     - support for recording full info about the tree root
 
 
477
     - support for recording tree-references
 
 
480
    repository_class = KnitRepository3
 
 
481
    support_tree_reference = True
 
 
483
    def _get_matching_bzrdir(self):
 
 
484
        return bzrdir.format_registry.make_bzrdir('experimental-knit3')
 
 
486
    def _ignore_setting_bzrdir(self, format):
 
 
489
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
 
 
491
    def check_conversion_target(self, target_format):
 
 
492
        RepositoryFormatKnit2.check_conversion_target(self, target_format)
 
 
493
        if not getattr(target_format, 'support_tree_reference', False):
 
 
494
            raise errors.BadConversionTarget(
 
 
495
                'Does not support nested trees', target_format)
 
 
498
    def get_format_string(self):
 
 
499
        """See RepositoryFormat.get_format_string()."""
 
 
500
        return "Bazaar Knit Repository Format 3\n"
 
 
502
    def get_format_description(self):
 
 
503
        """See RepositoryFormat.get_format_description()."""
 
 
504
        return "Knit repository format 3"