/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/repofmt/knitrepo.py

  • Committer: Aaron Bentley
  • Date: 2007-02-21 06:06:06 UTC
  • mto: (2255.6.1 dirstate)
  • mto: This revision was merged to the branch mainline in revision 2322.
  • Revision ID: aaron.bentley@utoronto.ca-20070221060606-unjaailciijp12ab
rename working tree format 4 to AB1 everywhere

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 (
 
18
    bzrdir,
 
19
    errors,
 
20
    graph,
 
21
    knit,
 
22
    lockable_files,
 
23
    lockdir,
 
24
    transactions,
 
25
    xml5,
 
26
    xml6,
 
27
    xml7,
 
28
    )
 
29
 
 
30
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
31
from bzrlib.repository import (
 
32
    MetaDirRepository,
 
33
    MetaDirRepositoryFormat,
 
34
    RepositoryFormat,
 
35
    RootCommitBuilder,
 
36
    )
 
37
import bzrlib.revision as _mod_revision
 
38
from bzrlib.store.versioned import VersionedFileStore
 
39
from bzrlib.trace import mutter, note, warning
 
40
 
 
41
 
 
42
class KnitRepository(MetaDirRepository):
 
43
    """Knit format repository."""
 
44
 
 
45
 
 
46
    _serializer = xml5.serializer_v5
 
47
 
 
48
    def _warn_if_deprecated(self):
 
49
        # This class isn't deprecated
 
50
        pass
 
51
 
 
52
    def _inventory_add_lines(self, inv_vf, revid, parents, lines):
 
53
        inv_vf.add_lines_with_ghosts(revid, parents, lines)
 
54
 
 
55
    @needs_read_lock
 
56
    def _all_revision_ids(self):
 
57
        """See Repository.all_revision_ids()."""
 
58
        # Knits get the revision graph from the index of the revision knit, so
 
59
        # it's always possible even if they're on an unlistable transport.
 
60
        return self._revision_store.all_revision_ids(self.get_transaction())
 
61
 
 
62
    def fileid_involved_between_revs(self, from_revid, to_revid):
 
63
        """Find file_id(s) which are involved in the changes between revisions.
 
64
 
 
65
        This determines the set of revisions which are involved, and then
 
66
        finds all file ids affected by those revisions.
 
67
        """
 
68
        vf = self._get_revision_vf()
 
69
        from_set = set(vf.get_ancestry(from_revid))
 
70
        to_set = set(vf.get_ancestry(to_revid))
 
71
        changed = to_set.difference(from_set)
 
72
        return self._fileid_involved_by_set(changed)
 
73
 
 
74
    def fileid_involved(self, last_revid=None):
 
75
        """Find all file_ids modified in the ancestry of last_revid.
 
76
 
 
77
        :param last_revid: If None, last_revision() will be used.
 
78
        """
 
79
        if not last_revid:
 
80
            changed = set(self.all_revision_ids())
 
81
        else:
 
82
            changed = set(self.get_ancestry(last_revid))
 
83
        if None in changed:
 
84
            changed.remove(None)
 
85
        return self._fileid_involved_by_set(changed)
 
86
 
 
87
    @needs_read_lock
 
88
    def get_ancestry(self, revision_id):
 
89
        """Return a list of revision-ids integrated by a revision.
 
90
        
 
91
        This is topologically sorted.
 
92
        """
 
93
        if revision_id is None:
 
94
            return [None]
 
95
        vf = self._get_revision_vf()
 
96
        try:
 
97
            return [None] + vf.get_ancestry(revision_id)
 
98
        except errors.RevisionNotPresent:
 
99
            raise errors.NoSuchRevision(self, revision_id)
 
100
 
 
101
    @needs_read_lock
 
102
    def get_revision(self, revision_id):
 
103
        """Return the Revision object for a named revision"""
 
104
        return self.get_revision_reconcile(revision_id)
 
105
 
 
106
    @needs_read_lock
 
107
    def get_revision_graph(self, revision_id=None):
 
108
        """Return a dictionary containing the revision graph.
 
109
 
 
110
        :param revision_id: The revision_id to get a graph from. If None, then
 
111
        the entire revision graph is returned. This is a deprecated mode of
 
112
        operation and will be removed in the future.
 
113
        :return: a dictionary of revision_id->revision_parents_list.
 
114
        """
 
115
        # special case NULL_REVISION
 
116
        if revision_id == _mod_revision.NULL_REVISION:
 
117
            return {}
 
118
        a_weave = self._get_revision_vf()
 
119
        entire_graph = a_weave.get_graph()
 
120
        if revision_id is None:
 
121
            return a_weave.get_graph()
 
122
        elif revision_id not in a_weave:
 
123
            raise errors.NoSuchRevision(self, revision_id)
 
124
        else:
 
125
            # add what can be reached from revision_id
 
126
            result = {}
 
127
            pending = set([revision_id])
 
128
            while len(pending) > 0:
 
129
                node = pending.pop()
 
130
                result[node] = a_weave.get_parents(node)
 
131
                for revision_id in result[node]:
 
132
                    if revision_id not in result:
 
133
                        pending.add(revision_id)
 
134
            return result
 
135
 
 
136
    @needs_read_lock
 
137
    def get_revision_graph_with_ghosts(self, revision_ids=None):
 
138
        """Return a graph of the revisions with ghosts marked as applicable.
 
139
 
 
140
        :param revision_ids: an iterable of revisions to graph or None for all.
 
141
        :return: a Graph object with the graph reachable from revision_ids.
 
142
        """
 
143
        result = graph.Graph()
 
144
        vf = self._get_revision_vf()
 
145
        versions = set(vf.versions())
 
146
        if not revision_ids:
 
147
            pending = set(self.all_revision_ids())
 
148
            required = set([])
 
149
        else:
 
150
            pending = set(revision_ids)
 
151
            # special case NULL_REVISION
 
152
            if _mod_revision.NULL_REVISION in pending:
 
153
                pending.remove(_mod_revision.NULL_REVISION)
 
154
            required = set(pending)
 
155
        done = set([])
 
156
        while len(pending):
 
157
            revision_id = pending.pop()
 
158
            if not revision_id in versions:
 
159
                if revision_id in required:
 
160
                    raise errors.NoSuchRevision(self, revision_id)
 
161
                # a ghost
 
162
                result.add_ghost(revision_id)
 
163
                # mark it as done so we don't try for it again.
 
164
                done.add(revision_id)
 
165
                continue
 
166
            parent_ids = vf.get_parents_with_ghosts(revision_id)
 
167
            for parent_id in parent_ids:
 
168
                # is this queued or done ?
 
169
                if (parent_id not in pending and
 
170
                    parent_id not in done):
 
171
                    # no, queue it.
 
172
                    pending.add(parent_id)
 
173
            result.add_node(revision_id, parent_ids)
 
174
            done.add(revision_id)
 
175
        return result
 
176
 
 
177
    def _get_revision_vf(self):
 
178
        """:return: a versioned file containing the revisions."""
 
179
        vf = self._revision_store.get_revision_file(self.get_transaction())
 
180
        return vf
 
181
 
 
182
    def _get_history_vf(self):
 
183
        """Get a versionedfile whose history graph reflects all revisions.
 
184
 
 
185
        For knit repositories, this is the revision knit.
 
186
        """
 
187
        return self._get_revision_vf()
 
188
 
 
189
    @needs_write_lock
 
190
    def reconcile(self, other=None, thorough=False):
 
191
        """Reconcile this repository."""
 
192
        from bzrlib.reconcile import KnitReconciler
 
193
        reconciler = KnitReconciler(self, thorough=thorough)
 
194
        reconciler.reconcile()
 
195
        return reconciler
 
196
    
 
197
    def revision_parents(self, revision_id):
 
198
        return self._get_revision_vf().get_parents(revision_id)
 
199
 
 
200
 
 
201
class KnitRepository2(KnitRepository):
 
202
    """"""
 
203
    def __init__(self, _format, a_bzrdir, control_files, _revision_store,
 
204
                 control_store, text_store):
 
205
        KnitRepository.__init__(self, _format, a_bzrdir, control_files,
 
206
                              _revision_store, control_store, text_store)
 
207
        self._serializer = xml6.serializer_v6
 
208
 
 
209
    def deserialise_inventory(self, revision_id, xml):
 
210
        """Transform the xml into an inventory object. 
 
211
 
 
212
        :param revision_id: The expected revision id of the inventory.
 
213
        :param xml: A serialised inventory.
 
214
        """
 
215
        result = self._serializer.read_inventory_from_string(xml)
 
216
        assert result.root.revision is not None
 
217
        return result
 
218
 
 
219
    def serialise_inventory(self, inv):
 
220
        """Transform the inventory object into XML text.
 
221
 
 
222
        :param revision_id: The expected revision id of the inventory.
 
223
        :param xml: A serialised inventory.
 
224
        """
 
225
        assert inv.revision_id is not None
 
226
        assert inv.root.revision is not None
 
227
        return KnitRepository.serialise_inventory(self, inv)
 
228
 
 
229
    def get_commit_builder(self, branch, parents, config, timestamp=None,
 
230
                           timezone=None, committer=None, revprops=None,
 
231
                           revision_id=None):
 
232
        """Obtain a CommitBuilder for this repository.
 
233
        
 
234
        :param branch: Branch to commit to.
 
235
        :param parents: Revision ids of the parents of the new revision.
 
236
        :param config: Configuration to use.
 
237
        :param timestamp: Optional timestamp recorded for commit.
 
238
        :param timezone: Optional timezone for timestamp.
 
239
        :param committer: Optional committer to set for commit.
 
240
        :param revprops: Optional dictionary of revision properties.
 
241
        :param revision_id: Optional revision id.
 
242
        """
 
243
        return RootCommitBuilder(self, parents, config, timestamp, timezone,
 
244
                                 committer, revprops, revision_id)
 
245
 
 
246
 
 
247
class KnitRepository3(KnitRepository2):
 
248
 
 
249
    def __init__(self, _format, a_bzrdir, control_files, _revision_store,
 
250
                 control_store, text_store):
 
251
        KnitRepository2.__init__(self, _format, a_bzrdir, control_files,
 
252
                                 _revision_store, control_store, text_store)
 
253
        self._serializer = xml7.serializer_v7
 
254
 
 
255
 
 
256
class RepositoryFormatKnit(MetaDirRepositoryFormat):
 
257
    """Bzr repository knit format (generalized). 
 
258
 
 
259
    This repository format has:
 
260
     - knits for file texts and inventory
 
261
     - hash subdirectory based stores.
 
262
     - knits for revisions and signatures
 
263
     - TextStores for revisions and signatures.
 
264
     - a format marker of its own
 
265
     - an optional 'shared-storage' flag
 
266
     - an optional 'no-working-trees' flag
 
267
     - a LockDir lock
 
268
    """
 
269
 
 
270
    def _get_control_store(self, repo_transport, control_files):
 
271
        """Return the control store for this repository."""
 
272
        return VersionedFileStore(
 
273
            repo_transport,
 
274
            prefixed=False,
 
275
            file_mode=control_files._file_mode,
 
276
            versionedfile_class=knit.KnitVersionedFile,
 
277
            versionedfile_kwargs={'factory':knit.KnitPlainFactory()},
 
278
            )
 
279
 
 
280
    def _get_revision_store(self, repo_transport, control_files):
 
281
        """See RepositoryFormat._get_revision_store()."""
 
282
        from bzrlib.store.revision.knit import KnitRevisionStore
 
283
        versioned_file_store = VersionedFileStore(
 
284
            repo_transport,
 
285
            file_mode=control_files._file_mode,
 
286
            prefixed=False,
 
287
            precious=True,
 
288
            versionedfile_class=knit.KnitVersionedFile,
 
289
            versionedfile_kwargs={'delta':False,
 
290
                                  'factory':knit.KnitPlainFactory(),
 
291
                                 },
 
292
            escaped=True,
 
293
            )
 
294
        return KnitRevisionStore(versioned_file_store)
 
295
 
 
296
    def _get_text_store(self, transport, control_files):
 
297
        """See RepositoryFormat._get_text_store()."""
 
298
        return self._get_versioned_file_store('knits',
 
299
                                  transport,
 
300
                                  control_files,
 
301
                                  versionedfile_class=knit.KnitVersionedFile,
 
302
                                  versionedfile_kwargs={
 
303
                                      'create_parent_dir':True,
 
304
                                      'delay_create':True,
 
305
                                      'dir_mode':control_files._dir_mode,
 
306
                                  },
 
307
                                  escaped=True)
 
308
 
 
309
    def initialize(self, a_bzrdir, shared=False):
 
310
        """Create a knit format 1 repository.
 
311
 
 
312
        :param a_bzrdir: bzrdir to contain the new repository; must already
 
313
            be initialized.
 
314
        :param shared: If true the repository will be initialized as a shared
 
315
                       repository.
 
316
        """
 
317
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
318
        dirs = ['revision-store', 'knits']
 
319
        files = []
 
320
        utf8_files = [('format', self.get_format_string())]
 
321
        
 
322
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
 
323
        repo_transport = a_bzrdir.get_repository_transport(None)
 
324
        control_files = lockable_files.LockableFiles(repo_transport,
 
325
                                'lock', lockdir.LockDir)
 
326
        control_store = self._get_control_store(repo_transport, control_files)
 
327
        transaction = transactions.WriteTransaction()
 
328
        # trigger a write of the inventory store.
 
329
        control_store.get_weave_or_empty('inventory', transaction)
 
330
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
331
        # the revision id here is irrelevant: it will not be stored, and cannot
 
332
        # already exist.
 
333
        _revision_store.has_revision_id('A', transaction)
 
334
        _revision_store.get_signature_file(transaction)
 
335
        return self.open(a_bzrdir=a_bzrdir, _found=True)
 
336
 
 
337
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
338
        """See RepositoryFormat.open().
 
339
        
 
340
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
341
                                    repository at a slightly different url
 
342
                                    than normal. I.e. during 'upgrade'.
 
343
        """
 
344
        if not _found:
 
345
            format = RepositoryFormat.find_format(a_bzrdir)
 
346
            assert format.__class__ ==  self.__class__
 
347
        if _override_transport is not None:
 
348
            repo_transport = _override_transport
 
349
        else:
 
350
            repo_transport = a_bzrdir.get_repository_transport(None)
 
351
        control_files = lockable_files.LockableFiles(repo_transport,
 
352
                                'lock', lockdir.LockDir)
 
353
        text_store = self._get_text_store(repo_transport, control_files)
 
354
        control_store = self._get_control_store(repo_transport, control_files)
 
355
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
356
        return KnitRepository(_format=self,
 
357
                              a_bzrdir=a_bzrdir,
 
358
                              control_files=control_files,
 
359
                              _revision_store=_revision_store,
 
360
                              control_store=control_store,
 
361
                              text_store=text_store)
 
362
 
 
363
 
 
364
class RepositoryFormatKnit1(RepositoryFormatKnit):
 
365
    """Bzr repository knit format 1.
 
366
 
 
367
    This repository format has:
 
368
     - knits for file texts and inventory
 
369
     - hash subdirectory based stores.
 
370
     - knits for revisions and signatures
 
371
     - TextStores for revisions and signatures.
 
372
     - a format marker of its own
 
373
     - an optional 'shared-storage' flag
 
374
     - an optional 'no-working-trees' flag
 
375
     - a LockDir lock
 
376
 
 
377
    This format was introduced in bzr 0.8.
 
378
    """
 
379
 
 
380
    def __ne__(self, other):
 
381
        return self.__class__ is not other.__class__
 
382
 
 
383
    def get_format_string(self):
 
384
        """See RepositoryFormat.get_format_string()."""
 
385
        return "Bazaar-NG Knit Repository Format 1"
 
386
 
 
387
    def get_format_description(self):
 
388
        """See RepositoryFormat.get_format_description()."""
 
389
        return "Knit repository format 1"
 
390
 
 
391
    def check_conversion_target(self, target_format):
 
392
        pass
 
393
 
 
394
 
 
395
class RepositoryFormatKnit2(RepositoryFormatKnit):
 
396
    """Bzr repository knit format 2.
 
397
 
 
398
    THIS FORMAT IS EXPERIMENTAL
 
399
    This repository format has:
 
400
     - knits for file texts and inventory
 
401
     - hash subdirectory based stores.
 
402
     - knits for revisions and signatures
 
403
     - TextStores for revisions and signatures.
 
404
     - a format marker of its own
 
405
     - an optional 'shared-storage' flag
 
406
     - an optional 'no-working-trees' flag
 
407
     - a LockDir lock
 
408
     - Support for recording full info about the tree root
 
409
 
 
410
    """
 
411
    
 
412
    rich_root_data = True
 
413
    repository_class = KnitRepository2
 
414
 
 
415
    def get_format_string(self):
 
416
        """See RepositoryFormat.get_format_string()."""
 
417
        return "Bazaar Knit Repository Format 2\n"
 
418
 
 
419
    def get_format_description(self):
 
420
        """See RepositoryFormat.get_format_description()."""
 
421
        return "Knit repository format 2"
 
422
 
 
423
    def check_conversion_target(self, target_format):
 
424
        if not target_format.rich_root_data:
 
425
            raise errors.BadConversionTarget(
 
426
                'Does not support rich root data.', target_format)
 
427
 
 
428
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
429
        """See RepositoryFormat.open().
 
430
        
 
431
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
432
                                    repository at a slightly different url
 
433
                                    than normal. I.e. during 'upgrade'.
 
434
        """
 
435
        if not _found:
 
436
            format = RepositoryFormat.find_format(a_bzrdir)
 
437
            assert format.__class__ ==  self.__class__
 
438
        if _override_transport is not None:
 
439
            repo_transport = _override_transport
 
440
        else:
 
441
            repo_transport = a_bzrdir.get_repository_transport(None)
 
442
        control_files = lockable_files.LockableFiles(repo_transport, 'lock',
 
443
                                                     lockdir.LockDir)
 
444
        text_store = self._get_text_store(repo_transport, control_files)
 
445
        control_store = self._get_control_store(repo_transport, control_files)
 
446
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
447
        return self.repository_class(_format=self,
 
448
                                     a_bzrdir=a_bzrdir,
 
449
                                     control_files=control_files,
 
450
                                     _revision_store=_revision_store,
 
451
                                     control_store=control_store,
 
452
                                     text_store=text_store)
 
453
 
 
454
 
 
455
class RepositoryFormatKnit3(RepositoryFormatKnit2):
 
456
    """Bzr repository knit format 2.
 
457
 
 
458
    THIS FORMAT IS EXPERIMENTAL
 
459
    This repository format has:
 
460
     - knits for file texts and inventory
 
461
     - hash subdirectory based stores.
 
462
     - knits for revisions and signatures
 
463
     - TextStores for revisions and signatures.
 
464
     - a format marker of its own
 
465
     - an optional 'shared-storage' flag
 
466
     - an optional 'no-working-trees' flag
 
467
     - a LockDir lock
 
468
     - support for recording full info about the tree root
 
469
     - support for recording tree-references
 
470
    """
 
471
 
 
472
    repository_class = KnitRepository3
 
473
    support_tree_reference = True
 
474
 
 
475
    def _get_matching_bzrdir(self):
 
476
        return bzrdir.format_registry.make_bzrdir('experimental-knit3')
 
477
 
 
478
    def _ignore_setting_bzrdir(self, format):
 
479
        pass
 
480
 
 
481
    _matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
 
482
 
 
483
    def check_conversion_target(self, target_format):
 
484
        RepositoryFormatKnit2.check_conversion_target(self, target_format)
 
485
        if not getattr(target_format, 'support_tree_reference', False):
 
486
            raise errors.BadConversionTarget(
 
487
                'Does not support nested trees', target_format)
 
488
            
 
489
 
 
490
    def get_format_string(self):
 
491
        """See RepositoryFormat.get_format_string()."""
 
492
        return "Bazaar Knit Repository Format 3\n"
 
493
 
 
494
    def get_format_description(self):
 
495
        """See RepositoryFormat.get_format_description()."""
 
496
        return "Knit repository format 3"
 
497
 
 
498