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
39
from bzrlib.decorators import needs_read_lock, needs_write_lock
40
from bzrlib.repository import (
43
MetaDirRepositoryFormat,
47
import bzrlib.revision as _mod_revision
48
from bzrlib.store.versioned import VersionedFileStore
49
from bzrlib.trace import mutter, mutter_callsite
50
from bzrlib.util import bencode
53
class _KnitParentsProvider(object):
55
def __init__(self, knit):
59
return 'KnitParentsProvider(%r)' % self._knit
61
@symbol_versioning.deprecated_method(symbol_versioning.one_one)
62
def get_parents(self, revision_ids):
63
"""See graph._StackedParentsProvider.get_parents"""
64
parent_map = self.get_parent_map(revision_ids)
65
return [parent_map.get(r, None) for r in revision_ids]
67
def get_parent_map(self, keys):
68
"""See graph._StackedParentsProvider.get_parent_map"""
70
for revision_id in keys:
71
if revision_id is None:
72
raise ValueError('get_parent_map(None) is not valid')
73
if revision_id == _mod_revision.NULL_REVISION:
74
parent_map[revision_id] = ()
78
self._knit.get_parents_with_ghosts(revision_id))
79
except errors.RevisionNotPresent:
83
parents = (_mod_revision.NULL_REVISION,)
84
parent_map[revision_id] = parents
88
class KnitRepository(MetaDirRepository):
89
"""Knit format repository."""
91
# These attributes are inherited from the Repository base class. Setting
92
# them to None ensures that if the constructor is changed to not initialize
93
# them, or a subclass fails to call the constructor, that an error will
94
# occur rather than the system working but generating incorrect data.
95
_commit_builder_class = None
98
def __init__(self, _format, a_bzrdir, control_files, _revision_store,
99
control_store, text_store, _commit_builder_class, _serializer):
100
MetaDirRepository.__init__(self, _format, a_bzrdir, control_files,
101
_revision_store, control_store, text_store)
102
self._commit_builder_class = _commit_builder_class
103
self._serializer = _serializer
104
self._reconcile_fixes_text_parents = True
105
control_store.get_scope = self.get_transaction
106
text_store.get_scope = self.get_transaction
107
_revision_store.get_scope = self.get_transaction
109
def _warn_if_deprecated(self):
110
# This class isn't deprecated
113
def _inventory_add_lines(self, inv_vf, revid, parents, lines, check_content):
114
return inv_vf.add_lines_with_ghosts(revid, parents, lines,
115
check_content=check_content)[0]
118
def _all_revision_ids(self):
119
"""See Repository.all_revision_ids()."""
120
# Knits get the revision graph from the index of the revision knit, so
121
# it's always possible even if they're on an unlistable transport.
122
return self._revision_store.all_revision_ids(self.get_transaction())
124
def fileid_involved_between_revs(self, from_revid, to_revid):
125
"""Find file_id(s) which are involved in the changes between revisions.
127
This determines the set of revisions which are involved, and then
128
finds all file ids affected by those revisions.
130
vf = self._get_revision_vf()
131
from_set = set(vf.get_ancestry(from_revid))
132
to_set = set(vf.get_ancestry(to_revid))
133
changed = to_set.difference(from_set)
134
return self._fileid_involved_by_set(changed)
136
def fileid_involved(self, last_revid=None):
137
"""Find all file_ids modified in the ancestry of last_revid.
139
:param last_revid: If None, last_revision() will be used.
142
changed = set(self.all_revision_ids())
144
changed = set(self.get_ancestry(last_revid))
147
return self._fileid_involved_by_set(changed)
150
def get_ancestry(self, revision_id, topo_sorted=True):
151
"""Return a list of revision-ids integrated by a revision.
153
This is topologically sorted, unless 'topo_sorted' is specified as
156
if _mod_revision.is_null(revision_id):
158
vf = self._get_revision_vf()
160
return [None] + vf.get_ancestry(revision_id, topo_sorted)
161
except errors.RevisionNotPresent:
162
raise errors.NoSuchRevision(self, revision_id)
164
@symbol_versioning.deprecated_method(symbol_versioning.one_two)
165
def get_data_stream(self, revision_ids):
166
"""See Repository.get_data_stream.
168
Deprecated in 1.2 for get_data_stream_for_search.
170
search_result = self.revision_ids_to_search_result(set(revision_ids))
171
return self.get_data_stream_for_search(search_result)
173
def get_data_stream_for_search(self, search):
174
"""See Repository.get_data_stream_for_search."""
175
item_keys = self.item_keys_introduced_by(search.get_keys())
176
for knit_kind, file_id, versions in item_keys:
178
if knit_kind == 'file':
179
name = ('file', file_id)
180
knit = self.weave_store.get_weave_or_empty(
181
file_id, self.get_transaction())
182
elif knit_kind == 'inventory':
183
knit = self.get_inventory_weave()
184
elif knit_kind == 'revisions':
185
knit = self._revision_store.get_revision_file(
186
self.get_transaction())
187
elif knit_kind == 'signatures':
188
knit = self._revision_store.get_signature_file(
189
self.get_transaction())
191
raise AssertionError('Unknown knit kind %r' % (knit_kind,))
192
yield name, _get_stream_as_bytes(knit, versions)
195
def get_revision(self, revision_id):
196
"""Return the Revision object for a named revision"""
197
revision_id = osutils.safe_revision_id(revision_id)
198
return self.get_revision_reconcile(revision_id)
200
def _get_revision_vf(self):
201
""":return: a versioned file containing the revisions."""
202
vf = self._revision_store.get_revision_file(self.get_transaction())
205
def has_revisions(self, revision_ids):
206
"""See Repository.has_revisions()."""
208
transaction = self.get_transaction()
209
for revision_id in revision_ids:
210
if self._revision_store.has_revision_id(revision_id, transaction):
211
result.add(revision_id)
215
def reconcile(self, other=None, thorough=False):
216
"""Reconcile this repository."""
217
from bzrlib.reconcile import KnitReconciler
218
reconciler = KnitReconciler(self, thorough=thorough)
219
reconciler.reconcile()
222
def _make_parents_provider(self):
223
return _KnitParentsProvider(self._get_revision_vf())
225
def _find_inconsistent_revision_parents(self):
226
"""Find revisions with different parent lists in the revision object
227
and in the index graph.
229
:returns: an iterator yielding tuples of (revison-id, parents-in-index,
230
parents-in-revision).
232
if not self.is_locked():
233
raise AssertionError()
234
vf = self._get_revision_vf()
235
for index_version in vf.versions():
236
parents_according_to_index = tuple(vf.get_parents_with_ghosts(
238
revision = self.get_revision(index_version)
239
parents_according_to_revision = tuple(revision.parent_ids)
240
if parents_according_to_index != parents_according_to_revision:
241
yield (index_version, parents_according_to_index,
242
parents_according_to_revision)
244
def _check_for_inconsistent_revision_parents(self):
245
inconsistencies = list(self._find_inconsistent_revision_parents())
247
raise errors.BzrCheckError(
248
"Revision knit has inconsistent parents.")
250
def revision_graph_can_have_wrong_parents(self):
251
# The revision.kndx could potentially claim a revision has a different
252
# parent to the revision text.
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
# Set this attribute in derived classes to control the repository class
271
# created by open and initialize.
272
repository_class = None
273
# Set this attribute in derived classes to control the
274
# _commit_builder_class that the repository objects will have passed to
276
_commit_builder_class = None
277
# Set this attribute in derived clases to control the _serializer that the
278
# repository objects will have passed to their constructor.
279
_serializer = xml5.serializer_v5
280
# Knit based repositories handle ghosts reasonably well.
281
supports_ghosts = True
282
# External lookups are not supported in this format.
283
supports_external_lookups = False
285
def _get_control_store(self, repo_transport, control_files):
286
"""Return the control store for this repository."""
287
return VersionedFileStore(
290
file_mode=control_files._file_mode,
291
versionedfile_class=knit.make_file_knit,
292
versionedfile_kwargs={'factory':knit.KnitPlainFactory()},
295
def _get_revision_store(self, repo_transport, control_files):
296
"""See RepositoryFormat._get_revision_store()."""
297
versioned_file_store = VersionedFileStore(
299
file_mode=control_files._file_mode,
302
versionedfile_class=knit.make_file_knit,
303
versionedfile_kwargs={'delta':False,
304
'factory':knit.KnitPlainFactory(),
308
return KnitRevisionStore(versioned_file_store)
310
def _get_text_store(self, transport, control_files):
311
"""See RepositoryFormat._get_text_store()."""
312
return self._get_versioned_file_store('knits',
315
versionedfile_class=knit.make_file_knit,
316
versionedfile_kwargs={
317
'create_parent_dir':True,
319
'dir_mode':control_files._dir_mode,
323
def initialize(self, a_bzrdir, shared=False):
324
"""Create a knit format 1 repository.
326
:param a_bzrdir: bzrdir to contain the new repository; must already
328
:param shared: If true the repository will be initialized as a shared
331
mutter('creating repository in %s.', a_bzrdir.transport.base)
334
utf8_files = [('format', self.get_format_string())]
336
self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
337
repo_transport = a_bzrdir.get_repository_transport(None)
338
control_files = lockable_files.LockableFiles(repo_transport,
339
'lock', lockdir.LockDir)
340
control_store = self._get_control_store(repo_transport, control_files)
341
transaction = transactions.WriteTransaction()
342
# trigger a write of the inventory store.
343
control_store.get_weave_or_empty('inventory', transaction)
344
_revision_store = self._get_revision_store(repo_transport, control_files)
345
# the revision id here is irrelevant: it will not be stored, and cannot
347
_revision_store.has_revision_id('A', transaction)
348
_revision_store.get_signature_file(transaction)
349
return self.open(a_bzrdir=a_bzrdir, _found=True)
351
def open(self, a_bzrdir, _found=False, _override_transport=None):
352
"""See RepositoryFormat.open().
354
:param _override_transport: INTERNAL USE ONLY. Allows opening the
355
repository at a slightly different url
356
than normal. I.e. during 'upgrade'.
359
format = RepositoryFormat.find_format(a_bzrdir)
360
if _override_transport is not None:
361
repo_transport = _override_transport
363
repo_transport = a_bzrdir.get_repository_transport(None)
364
control_files = lockable_files.LockableFiles(repo_transport,
365
'lock', lockdir.LockDir)
366
text_store = self._get_text_store(repo_transport, control_files)
367
control_store = self._get_control_store(repo_transport, control_files)
368
_revision_store = self._get_revision_store(repo_transport, control_files)
369
return self.repository_class(_format=self,
371
control_files=control_files,
372
_revision_store=_revision_store,
373
control_store=control_store,
374
text_store=text_store,
375
_commit_builder_class=self._commit_builder_class,
376
_serializer=self._serializer)
379
class RepositoryFormatKnit1(RepositoryFormatKnit):
380
"""Bzr repository knit format 1.
382
This repository format has:
383
- knits for file texts and inventory
384
- hash subdirectory based stores.
385
- knits for revisions and signatures
386
- TextStores for revisions and signatures.
387
- a format marker of its own
388
- an optional 'shared-storage' flag
389
- an optional 'no-working-trees' flag
392
This format was introduced in bzr 0.8.
395
repository_class = KnitRepository
396
_commit_builder_class = CommitBuilder
397
_serializer = xml5.serializer_v5
399
def __ne__(self, other):
400
return self.__class__ is not other.__class__
402
def get_format_string(self):
403
"""See RepositoryFormat.get_format_string()."""
404
return "Bazaar-NG Knit Repository Format 1"
406
def get_format_description(self):
407
"""See RepositoryFormat.get_format_description()."""
408
return "Knit repository format 1"
410
def check_conversion_target(self, target_format):
414
class RepositoryFormatKnit3(RepositoryFormatKnit):
415
"""Bzr repository knit format 3.
417
This repository format has:
418
- knits for file texts and inventory
419
- hash subdirectory based stores.
420
- knits for revisions and signatures
421
- TextStores for revisions and signatures.
422
- a format marker of its own
423
- an optional 'shared-storage' flag
424
- an optional 'no-working-trees' flag
426
- support for recording full info about the tree root
427
- support for recording tree-references
430
repository_class = KnitRepository
431
_commit_builder_class = RootCommitBuilder
432
rich_root_data = True
433
supports_tree_reference = True
434
_serializer = xml7.serializer_v7
436
def _get_matching_bzrdir(self):
437
return bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
439
def _ignore_setting_bzrdir(self, format):
442
_matchingbzrdir = property(_get_matching_bzrdir, _ignore_setting_bzrdir)
444
def check_conversion_target(self, target_format):
445
if not target_format.rich_root_data:
446
raise errors.BadConversionTarget(
447
'Does not support rich root data.', target_format)
448
if not getattr(target_format, 'supports_tree_reference', False):
449
raise errors.BadConversionTarget(
450
'Does not support nested trees', target_format)
452
def get_format_string(self):
453
"""See RepositoryFormat.get_format_string()."""
454
return "Bazaar Knit Repository Format 3 (bzr 0.15)\n"
456
def get_format_description(self):
457
"""See RepositoryFormat.get_format_description()."""
458
return "Knit repository format 3"
461
class RepositoryFormatKnit4(RepositoryFormatKnit):
462
"""Bzr repository knit format 4.
464
This repository format has everything in format 3, except for
466
- knits for file texts and inventory
467
- hash subdirectory based stores.
468
- knits for revisions and signatures
469
- TextStores for revisions and signatures.
470
- a format marker of its own
471
- an optional 'shared-storage' flag
472
- an optional 'no-working-trees' flag
474
- support for recording full info about the tree root
477
repository_class = KnitRepository
478
_commit_builder_class = RootCommitBuilder
479
rich_root_data = True
480
supports_tree_reference = False
481
_serializer = xml6.serializer_v6
483
def _get_matching_bzrdir(self):
484
return bzrdir.format_registry.make_bzrdir('rich-root')
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
if not target_format.rich_root_data:
493
raise errors.BadConversionTarget(
494
'Does not support rich root data.', target_format)
496
def get_format_string(self):
497
"""See RepositoryFormat.get_format_string()."""
498
return 'Bazaar Knit Repository Format 4 (bzr 1.0)\n'
500
def get_format_description(self):
501
"""See RepositoryFormat.get_format_description()."""
502
return "Knit repository format 4"
505
def _get_stream_as_bytes(knit, required_versions):
506
"""Generate a serialised data stream.
508
The format is a bencoding of a list. The first element of the list is a
509
string of the format signature, then each subsequent element is a list
510
corresponding to a record. Those lists contain:
517
:returns: a bencoded list.
519
knit_stream = knit.get_data_stream(required_versions)
520
format_signature, data_list, callable = knit_stream
522
data.append(format_signature)
523
for version, options, length, parents in data_list:
524
data.append([version, options, parents, callable(length)])
525
return bencode.bencode(data)