~bzr-pqm/bzr/bzr.dev

2241.1.5 by Martin Pool
Move KnitFormat2 into repofmt
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
2
#
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.
7
#
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.
12
#
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
16
17
from bzrlib import (
2100.3.31 by Aaron Bentley
Merged bzr.dev (17 tests failing)
18
    bzrdir,
2241.1.5 by Martin Pool
Move KnitFormat2 into repofmt
19
    errors,
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
20
    graph,
21
    knit,
2241.1.5 by Martin Pool
Move KnitFormat2 into repofmt
22
    lockable_files,
23
    lockdir,
2249.5.16 by John Arbash Meinel
[merge] bzr.dev 2283
24
    osutils,
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
25
    transactions,
2241.1.8 by Martin Pool
Set the repository's serializer in the places it's needed, not in the base class
26
    xml5,
2241.1.5 by Martin Pool
Move KnitFormat2 into repofmt
27
    xml6,
2100.3.31 by Aaron Bentley
Merged bzr.dev (17 tests failing)
28
    xml7,
2241.1.5 by Martin Pool
Move KnitFormat2 into repofmt
29
    )
30
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
31
from bzrlib.decorators import needs_read_lock, needs_write_lock
2241.1.5 by Martin Pool
Move KnitFormat2 into repofmt
32
from bzrlib.repository import (
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
33
    MetaDirRepository,
34
    MetaDirRepositoryFormat,
2241.1.5 by Martin Pool
Move KnitFormat2 into repofmt
35
    RepositoryFormat,
36
    RootCommitBuilder,
37
    )
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
38
import bzrlib.revision as _mod_revision
39
from bzrlib.store.versioned import VersionedFileStore
40
from bzrlib.trace import mutter, note, warning
41
42
43
class KnitRepository(MetaDirRepository):
44
    """Knit format repository."""
45
2241.1.8 by Martin Pool
Set the repository's serializer in the places it's needed, not in the base class
46
47
    _serializer = xml5.serializer_v5
48
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
49
    def _warn_if_deprecated(self):
50
        # This class isn't deprecated
51
        pass
52
53
    def _inventory_add_lines(self, inv_vf, revid, parents, lines):
54
        inv_vf.add_lines_with_ghosts(revid, parents, lines)
55
56
    @needs_read_lock
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())
62
63
    def fileid_involved_between_revs(self, from_revid, to_revid):
64
        """Find file_id(s) which are involved in the changes between revisions.
65
66
        This determines the set of revisions which are involved, and then
67
        finds all file ids affected by those revisions.
68
        """
2249.5.16 by John Arbash Meinel
[merge] bzr.dev 2283
69
        from_revid = osutils.safe_revision_id(from_revid)
70
        to_revid = osutils.safe_revision_id(to_revid)
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
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)
76
77
    def fileid_involved(self, last_revid=None):
78
        """Find all file_ids modified in the ancestry of last_revid.
79
80
        :param last_revid: If None, last_revision() will be used.
81
        """
82
        if not last_revid:
83
            changed = set(self.all_revision_ids())
84
        else:
85
            changed = set(self.get_ancestry(last_revid))
86
        if None in changed:
87
            changed.remove(None)
88
        return self._fileid_involved_by_set(changed)
89
90
    @needs_read_lock
91
    def get_ancestry(self, revision_id):
92
        """Return a list of revision-ids integrated by a revision.
93
        
94
        This is topologically sorted.
95
        """
96
        if revision_id is None:
97
            return [None]
2249.5.16 by John Arbash Meinel
[merge] bzr.dev 2283
98
        revision_id = osutils.safe_revision_id(revision_id)
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
99
        vf = self._get_revision_vf()
100
        try:
101
            return [None] + vf.get_ancestry(revision_id)
102
        except errors.RevisionNotPresent:
103
            raise errors.NoSuchRevision(self, revision_id)
104
105
    @needs_read_lock
106
    def get_revision(self, revision_id):
107
        """Return the Revision object for a named revision"""
2249.5.16 by John Arbash Meinel
[merge] bzr.dev 2283
108
        revision_id = osutils.safe_revision_id(revision_id)
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
109
        return self.get_revision_reconcile(revision_id)
110
111
    @needs_read_lock
112
    def get_revision_graph(self, revision_id=None):
113
        """Return a dictionary containing the revision graph.
114
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.
119
        """
120
        # special case NULL_REVISION
121
        if revision_id == _mod_revision.NULL_REVISION:
122
            return {}
2249.5.16 by John Arbash Meinel
[merge] bzr.dev 2283
123
        revision_id = osutils.safe_revision_id(revision_id)
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
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)
130
        else:
131
            # add what can be reached from revision_id
132
            result = {}
133
            pending = set([revision_id])
134
            while len(pending) > 0:
135
                node = pending.pop()
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)
140
            return result
141
142
    @needs_read_lock
143
    def get_revision_graph_with_ghosts(self, revision_ids=None):
144
        """Return a graph of the revisions with ghosts marked as applicable.
145
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.
148
        """
149
        result = graph.Graph()
150
        vf = self._get_revision_vf()
151
        versions = set(vf.versions())
152
        if not revision_ids:
153
            pending = set(self.all_revision_ids())
154
            required = set([])
155
        else:
2249.5.16 by John Arbash Meinel
[merge] bzr.dev 2283
156
            pending = set(osutils.safe_revision_id(r) for r in revision_ids)
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
157
            # special case NULL_REVISION
158
            if _mod_revision.NULL_REVISION in pending:
159
                pending.remove(_mod_revision.NULL_REVISION)
160
            required = set(pending)
161
        done = set([])
162
        while len(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)
167
                # a ghost
168
                result.add_ghost(revision_id)
169
                # mark it as done so we don't try for it again.
170
                done.add(revision_id)
171
                continue
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):
177
                    # no, queue it.
178
                    pending.add(parent_id)
179
            result.add_node(revision_id, parent_ids)
180
            done.add(revision_id)
181
        return result
182
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())
186
        return vf
187
2230.3.54 by Aaron Bentley
Move reverse history iteration to repository
188
    def _get_history_vf(self):
189
        """Get a versionedfile whose history graph reflects all revisions.
190
191
        For knit repositories, this is the revision knit.
192
        """
193
        return self._get_revision_vf()
194
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
195
    @needs_write_lock
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()
201
        return reconciler
202
    
203
    def revision_parents(self, revision_id):
2249.5.16 by John Arbash Meinel
[merge] bzr.dev 2283
204
        revision_id = osutils.safe_revision_id(revision_id)
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
205
        return self._get_revision_vf().get_parents(revision_id)
2241.1.5 by Martin Pool
Move KnitFormat2 into repofmt
206
207
208
class KnitRepository2(KnitRepository):
209
    """"""
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
215
216
    def deserialise_inventory(self, revision_id, xml):
217
        """Transform the xml into an inventory object. 
218
219
        :param revision_id: The expected revision id of the inventory.
220
        :param xml: A serialised inventory.
221
        """
222
        result = self._serializer.read_inventory_from_string(xml)
223
        assert result.root.revision is not None
224
        return result
225
226
    def serialise_inventory(self, inv):
227
        """Transform the inventory object into XML text.
228
229
        :param revision_id: The expected revision id of the inventory.
230
        :param xml: A serialised inventory.
231
        """
232
        assert inv.revision_id is not None
233
        assert inv.root.revision is not None
234
        return KnitRepository.serialise_inventory(self, inv)
235
236
    def get_commit_builder(self, branch, parents, config, timestamp=None,
237
                           timezone=None, committer=None, revprops=None,
238
                           revision_id=None):
239
        """Obtain a CommitBuilder for this repository.
240
        
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.
249
        """
2249.5.16 by John Arbash Meinel
[merge] bzr.dev 2283
250
        revision_id = osutils.safe_revision_id(revision_id)
2241.1.5 by Martin Pool
Move KnitFormat2 into repofmt
251
        return RootCommitBuilder(self, parents, config, timestamp, timezone,
252
                                 committer, revprops, revision_id)
253
254
2100.3.31 by Aaron Bentley
Merged bzr.dev (17 tests failing)
255
class KnitRepository3(KnitRepository2):
256
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
262
263
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
264
class RepositoryFormatKnit(MetaDirRepositoryFormat):
265
    """Bzr repository knit format (generalized). 
266
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
275
     - a LockDir lock
276
    """
277
278
    def _get_control_store(self, repo_transport, control_files):
279
        """Return the control store for this repository."""
280
        return VersionedFileStore(
281
            repo_transport,
282
            prefixed=False,
283
            file_mode=control_files._file_mode,
284
            versionedfile_class=knit.KnitVersionedFile,
285
            versionedfile_kwargs={'factory':knit.KnitPlainFactory()},
286
            )
287
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(
292
            repo_transport,
293
            file_mode=control_files._file_mode,
294
            prefixed=False,
295
            precious=True,
296
            versionedfile_class=knit.KnitVersionedFile,
297
            versionedfile_kwargs={'delta':False,
298
                                  'factory':knit.KnitPlainFactory(),
299
                                 },
300
            escaped=True,
301
            )
302
        return KnitRevisionStore(versioned_file_store)
303
304
    def _get_text_store(self, transport, control_files):
305
        """See RepositoryFormat._get_text_store()."""
306
        return self._get_versioned_file_store('knits',
307
                                  transport,
308
                                  control_files,
309
                                  versionedfile_class=knit.KnitVersionedFile,
310
                                  versionedfile_kwargs={
311
                                      'create_parent_dir':True,
312
                                      'delay_create':True,
313
                                      'dir_mode':control_files._dir_mode,
314
                                  },
315
                                  escaped=True)
316
317
    def initialize(self, a_bzrdir, shared=False):
318
        """Create a knit format 1 repository.
319
320
        :param a_bzrdir: bzrdir to contain the new repository; must already
321
            be initialized.
322
        :param shared: If true the repository will be initialized as a shared
323
                       repository.
324
        """
325
        mutter('creating repository in %s.', a_bzrdir.transport.base)
326
        dirs = ['revision-store', 'knits']
327
        files = []
328
        utf8_files = [('format', self.get_format_string())]
329
        
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
340
        # already exist.
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)
344
345
    def open(self, a_bzrdir, _found=False, _override_transport=None):
346
        """See RepositoryFormat.open().
347
        
348
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
349
                                    repository at a slightly different url
350
                                    than normal. I.e. during 'upgrade'.
351
        """
352
        if not _found:
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
357
        else:
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,
365
                              a_bzrdir=a_bzrdir,
366
                              control_files=control_files,
367
                              _revision_store=_revision_store,
368
                              control_store=control_store,
369
                              text_store=text_store)
370
371
372
class RepositoryFormatKnit1(RepositoryFormatKnit):
373
    """Bzr repository knit format 1.
374
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
383
     - a LockDir lock
384
385
    This format was introduced in bzr 0.8.
386
    """
2241.1.11 by Martin Pool
Get rid of RepositoryFormat*_instance objects. Instead the format
387
2100.3.35 by Aaron Bentley
equality operations on bzrdir
388
    def __ne__(self, other):
2100.3.31 by Aaron Bentley
Merged bzr.dev (17 tests failing)
389
        return self.__class__ is not other.__class__
390
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
391
    def get_format_string(self):
392
        """See RepositoryFormat.get_format_string()."""
393
        return "Bazaar-NG Knit Repository Format 1"
394
395
    def get_format_description(self):
396
        """See RepositoryFormat.get_format_description()."""
397
        return "Knit repository format 1"
398
399
    def check_conversion_target(self, target_format):
400
        pass
401
402
2241.1.5 by Martin Pool
Move KnitFormat2 into repofmt
403
class RepositoryFormatKnit2(RepositoryFormatKnit):
404
    """Bzr repository knit format 2.
405
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
415
     - a LockDir lock
416
     - Support for recording full info about the tree root
417
418
    """
419
    
420
    rich_root_data = True
2100.3.31 by Aaron Bentley
Merged bzr.dev (17 tests failing)
421
    repository_class = KnitRepository2
2241.1.5 by Martin Pool
Move KnitFormat2 into repofmt
422
423
    def get_format_string(self):
424
        """See RepositoryFormat.get_format_string()."""
425
        return "Bazaar Knit Repository Format 2\n"
426
427
    def get_format_description(self):
428
        """See RepositoryFormat.get_format_description()."""
429
        return "Knit repository format 2"
430
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)
435
436
    def open(self, a_bzrdir, _found=False, _override_transport=None):
437
        """See RepositoryFormat.open().
438
        
439
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
440
                                    repository at a slightly different url
441
                                    than normal. I.e. during 'upgrade'.
442
        """
443
        if not _found:
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
448
        else:
449
            repo_transport = a_bzrdir.get_repository_transport(None)
450
        control_files = lockable_files.LockableFiles(repo_transport, 'lock',
451
                                                     lockdir.LockDir)
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)
2100.3.31 by Aaron Bentley
Merged bzr.dev (17 tests failing)
455
        return self.repository_class(_format=self,
456
                                     a_bzrdir=a_bzrdir,
457
                                     control_files=control_files,
458
                                     _revision_store=_revision_store,
459
                                     control_store=control_store,
460
                                     text_store=text_store)
461
462
463
class RepositoryFormatKnit3(RepositoryFormatKnit2):
464
    """Bzr repository knit format 2.
465
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
475
     - a LockDir lock
476
     - support for recording full info about the tree root
477
     - support for recording tree-references
478
    """
479
480
    repository_class = KnitRepository3
481
    support_tree_reference = True
482
483
    def _get_matching_bzrdir(self):
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
484
        return bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
2100.3.31 by Aaron Bentley
Merged bzr.dev (17 tests failing)
485
486
    def _ignore_setting_bzrdir(self, format):
487
        pass
488
489
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
490
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)
496
            
497
498
    def get_format_string(self):
499
        """See RepositoryFormat.get_format_string()."""
500
        return "Bazaar Knit Repository Format 3\n"
501
502
    def get_format_description(self):
503
        """See RepositoryFormat.get_format_description()."""
504
        return "Knit repository format 3"
505
506