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
30
from bzrlib.decorators import needs_read_lock, needs_write_lock
31
from bzrlib.repository import (
33
MetaDirRepositoryFormat,
37
import bzrlib.revision as _mod_revision
38
from bzrlib.store.versioned import VersionedFileStore
39
from bzrlib.trace import mutter, note, warning
42
class KnitRepository(MetaDirRepository):
43
"""Knit format repository."""
46
_serializer = xml5.serializer_v5
48
def _warn_if_deprecated(self):
49
# This class isn't deprecated
52
def _inventory_add_lines(self, inv_vf, revid, parents, lines):
53
inv_vf.add_lines_with_ghosts(revid, parents, lines)
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())
62
def fileid_involved_between_revs(self, from_revid, to_revid):
63
"""Find file_id(s) which are involved in the changes between revisions.
65
This determines the set of revisions which are involved, and then
66
finds all file ids affected by those revisions.
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)
74
def fileid_involved(self, last_revid=None):
75
"""Find all file_ids modified in the ancestry of last_revid.
77
:param last_revid: If None, last_revision() will be used.
80
changed = set(self.all_revision_ids())
82
changed = set(self.get_ancestry(last_revid))
85
return self._fileid_involved_by_set(changed)
88
def get_ancestry(self, revision_id):
89
"""Return a list of revision-ids integrated by a revision.
91
This is topologically sorted.
93
if revision_id is None:
95
vf = self._get_revision_vf()
97
return [None] + vf.get_ancestry(revision_id)
98
except errors.RevisionNotPresent:
99
raise errors.NoSuchRevision(self, revision_id)
102
def get_revision(self, revision_id):
103
"""Return the Revision object for a named revision"""
104
return self.get_revision_reconcile(revision_id)
107
def get_revision_graph(self, revision_id=None):
108
"""Return a dictionary containing the revision graph.
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.
115
# special case NULL_REVISION
116
if revision_id == _mod_revision.NULL_REVISION:
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)
125
# add what can be reached from revision_id
127
pending = set([revision_id])
128
while len(pending) > 0:
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)
137
def get_revision_graph_with_ghosts(self, revision_ids=None):
138
"""Return a graph of the revisions with ghosts marked as applicable.
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.
143
result = graph.Graph()
144
vf = self._get_revision_vf()
145
versions = set(vf.versions())
147
pending = set(self.all_revision_ids())
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)
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)
162
result.add_ghost(revision_id)
163
# mark it as done so we don't try for it again.
164
done.add(revision_id)
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):
172
pending.add(parent_id)
173
result.add_node(revision_id, parent_ids)
174
done.add(revision_id)
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())
182
def _get_history_vf(self):
183
"""Get a versionedfile whose history graph reflects all revisions.
185
For knit repositories, this is the revision knit.
187
return self._get_revision_vf()
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()
197
def revision_parents(self, revision_id):
198
return self._get_revision_vf().get_parents(revision_id)
201
class KnitRepository2(KnitRepository):
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
209
def deserialise_inventory(self, revision_id, xml):
210
"""Transform the xml into an inventory object.
212
:param revision_id: The expected revision id of the inventory.
213
:param xml: A serialised inventory.
215
result = self._serializer.read_inventory_from_string(xml)
216
assert result.root.revision is not None
219
def serialise_inventory(self, inv):
220
"""Transform the inventory object into XML text.
222
:param revision_id: The expected revision id of the inventory.
223
:param xml: A serialised inventory.
225
assert inv.revision_id is not None
226
assert inv.root.revision is not None
227
return KnitRepository.serialise_inventory(self, inv)
229
def get_commit_builder(self, branch, parents, config, timestamp=None,
230
timezone=None, committer=None, revprops=None,
232
"""Obtain a CommitBuilder for this repository.
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.
243
return RootCommitBuilder(self, parents, config, timestamp, timezone,
244
committer, revprops, revision_id)
247
class KnitRepository3(KnitRepository2):
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
256
class RepositoryFormatKnit(MetaDirRepositoryFormat):
257
"""Bzr repository knit format (generalized).
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
270
def _get_control_store(self, repo_transport, control_files):
271
"""Return the control store for this repository."""
272
return VersionedFileStore(
275
file_mode=control_files._file_mode,
276
versionedfile_class=knit.KnitVersionedFile,
277
versionedfile_kwargs={'factory':knit.KnitPlainFactory()},
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(
285
file_mode=control_files._file_mode,
288
versionedfile_class=knit.KnitVersionedFile,
289
versionedfile_kwargs={'delta':False,
290
'factory':knit.KnitPlainFactory(),
294
return KnitRevisionStore(versioned_file_store)
296
def _get_text_store(self, transport, control_files):
297
"""See RepositoryFormat._get_text_store()."""
298
return self._get_versioned_file_store('knits',
301
versionedfile_class=knit.KnitVersionedFile,
302
versionedfile_kwargs={
303
'create_parent_dir':True,
305
'dir_mode':control_files._dir_mode,
309
def initialize(self, a_bzrdir, shared=False):
310
"""Create a knit format 1 repository.
312
:param a_bzrdir: bzrdir to contain the new repository; must already
314
:param shared: If true the repository will be initialized as a shared
317
mutter('creating repository in %s.', a_bzrdir.transport.base)
318
dirs = ['revision-store', 'knits']
320
utf8_files = [('format', self.get_format_string())]
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
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)
337
def open(self, a_bzrdir, _found=False, _override_transport=None):
338
"""See RepositoryFormat.open().
340
:param _override_transport: INTERNAL USE ONLY. Allows opening the
341
repository at a slightly different url
342
than normal. I.e. during 'upgrade'.
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
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,
358
control_files=control_files,
359
_revision_store=_revision_store,
360
control_store=control_store,
361
text_store=text_store)
364
class RepositoryFormatKnit1(RepositoryFormatKnit):
365
"""Bzr repository knit format 1.
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
377
This format was introduced in bzr 0.8.
380
def __ne__(self, other):
381
return self.__class__ is not other.__class__
383
def get_format_string(self):
384
"""See RepositoryFormat.get_format_string()."""
385
return "Bazaar-NG Knit Repository Format 1"
387
def get_format_description(self):
388
"""See RepositoryFormat.get_format_description()."""
389
return "Knit repository format 1"
391
def check_conversion_target(self, target_format):
395
class RepositoryFormatKnit2(RepositoryFormatKnit):
396
"""Bzr repository knit format 2.
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
408
- Support for recording full info about the tree root
412
rich_root_data = True
413
repository_class = KnitRepository2
415
def get_format_string(self):
416
"""See RepositoryFormat.get_format_string()."""
417
return "Bazaar Knit Repository Format 2\n"
419
def get_format_description(self):
420
"""See RepositoryFormat.get_format_description()."""
421
return "Knit repository format 2"
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)
428
def open(self, a_bzrdir, _found=False, _override_transport=None):
429
"""See RepositoryFormat.open().
431
:param _override_transport: INTERNAL USE ONLY. Allows opening the
432
repository at a slightly different url
433
than normal. I.e. during 'upgrade'.
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
441
repo_transport = a_bzrdir.get_repository_transport(None)
442
control_files = lockable_files.LockableFiles(repo_transport, 'lock',
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,
449
control_files=control_files,
450
_revision_store=_revision_store,
451
control_store=control_store,
452
text_store=text_store)
455
class RepositoryFormatKnit3(RepositoryFormatKnit2):
456
"""Bzr repository knit format 2.
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
468
- support for recording full info about the tree root
469
- support for recording tree-references
472
repository_class = KnitRepository3
473
support_tree_reference = True
475
def _get_matching_bzrdir(self):
476
return bzrdir.format_registry.make_bzrdir('experimental-knit3')
478
def _ignore_setting_bzrdir(self, format):
481
_matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
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)
490
def get_format_string(self):
491
"""See RepositoryFormat.get_format_string()."""
492
return "Bazaar Knit Repository Format 3\n"
494
def get_format_description(self):
495
"""See RepositoryFormat.get_format_description()."""
496
return "Knit repository format 3"