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
27
from bzrlib.decorators import needs_read_lock, needs_write_lock
28
from bzrlib.repository import (
30
MetaDirRepositoryFormat,
34
import bzrlib.revision as _mod_revision
35
from bzrlib.store.versioned import VersionedFileStore
36
from bzrlib.trace import mutter, note, warning
39
class KnitRepository(MetaDirRepository):
40
"""Knit format repository."""
42
def _warn_if_deprecated(self):
43
# This class isn't deprecated
46
def _inventory_add_lines(self, inv_vf, revid, parents, lines):
47
inv_vf.add_lines_with_ghosts(revid, parents, lines)
50
def _all_revision_ids(self):
51
"""See Repository.all_revision_ids()."""
52
# Knits get the revision graph from the index of the revision knit, so
53
# it's always possible even if they're on an unlistable transport.
54
return self._revision_store.all_revision_ids(self.get_transaction())
56
def fileid_involved_between_revs(self, from_revid, to_revid):
57
"""Find file_id(s) which are involved in the changes between revisions.
59
This determines the set of revisions which are involved, and then
60
finds all file ids affected by those revisions.
62
vf = self._get_revision_vf()
63
from_set = set(vf.get_ancestry(from_revid))
64
to_set = set(vf.get_ancestry(to_revid))
65
changed = to_set.difference(from_set)
66
return self._fileid_involved_by_set(changed)
68
def fileid_involved(self, last_revid=None):
69
"""Find all file_ids modified in the ancestry of last_revid.
71
:param last_revid: If None, last_revision() will be used.
74
changed = set(self.all_revision_ids())
76
changed = set(self.get_ancestry(last_revid))
79
return self._fileid_involved_by_set(changed)
82
def get_ancestry(self, revision_id):
83
"""Return a list of revision-ids integrated by a revision.
85
This is topologically sorted.
87
if revision_id is None:
89
vf = self._get_revision_vf()
91
return [None] + vf.get_ancestry(revision_id)
92
except errors.RevisionNotPresent:
93
raise errors.NoSuchRevision(self, revision_id)
96
def get_revision(self, revision_id):
97
"""Return the Revision object for a named revision"""
98
return self.get_revision_reconcile(revision_id)
101
def get_revision_graph(self, revision_id=None):
102
"""Return a dictionary containing the revision graph.
104
:param revision_id: The revision_id to get a graph from. If None, then
105
the entire revision graph is returned. This is a deprecated mode of
106
operation and will be removed in the future.
107
:return: a dictionary of revision_id->revision_parents_list.
109
# special case NULL_REVISION
110
if revision_id == _mod_revision.NULL_REVISION:
112
a_weave = self._get_revision_vf()
113
entire_graph = a_weave.get_graph()
114
if revision_id is None:
115
return a_weave.get_graph()
116
elif revision_id not in a_weave:
117
raise errors.NoSuchRevision(self, revision_id)
119
# add what can be reached from revision_id
121
pending = set([revision_id])
122
while len(pending) > 0:
124
result[node] = a_weave.get_parents(node)
125
for revision_id in result[node]:
126
if revision_id not in result:
127
pending.add(revision_id)
131
def get_revision_graph_with_ghosts(self, revision_ids=None):
132
"""Return a graph of the revisions with ghosts marked as applicable.
134
:param revision_ids: an iterable of revisions to graph or None for all.
135
:return: a Graph object with the graph reachable from revision_ids.
137
result = graph.Graph()
138
vf = self._get_revision_vf()
139
versions = set(vf.versions())
141
pending = set(self.all_revision_ids())
144
pending = set(revision_ids)
145
# special case NULL_REVISION
146
if _mod_revision.NULL_REVISION in pending:
147
pending.remove(_mod_revision.NULL_REVISION)
148
required = set(pending)
151
revision_id = pending.pop()
152
if not revision_id in versions:
153
if revision_id in required:
154
raise errors.NoSuchRevision(self, revision_id)
156
result.add_ghost(revision_id)
157
# mark it as done so we don't try for it again.
158
done.add(revision_id)
160
parent_ids = vf.get_parents_with_ghosts(revision_id)
161
for parent_id in parent_ids:
162
# is this queued or done ?
163
if (parent_id not in pending and
164
parent_id not in done):
166
pending.add(parent_id)
167
result.add_node(revision_id, parent_ids)
168
done.add(revision_id)
171
def _get_revision_vf(self):
172
""":return: a versioned file containing the revisions."""
173
vf = self._revision_store.get_revision_file(self.get_transaction())
177
def reconcile(self, other=None, thorough=False):
178
"""Reconcile this repository."""
179
from bzrlib.reconcile import KnitReconciler
180
reconciler = KnitReconciler(self, thorough=thorough)
181
reconciler.reconcile()
184
def revision_parents(self, revision_id):
185
return self._get_revision_vf().get_parents(revision_id)
188
class KnitRepository2(KnitRepository):
190
def __init__(self, _format, a_bzrdir, control_files, _revision_store,
191
control_store, text_store):
192
KnitRepository.__init__(self, _format, a_bzrdir, control_files,
193
_revision_store, control_store, text_store)
194
self._serializer = xml6.serializer_v6
196
def deserialise_inventory(self, revision_id, xml):
197
"""Transform the xml into an inventory object.
199
:param revision_id: The expected revision id of the inventory.
200
:param xml: A serialised inventory.
202
result = self._serializer.read_inventory_from_string(xml)
203
assert result.root.revision is not None
206
def serialise_inventory(self, inv):
207
"""Transform the inventory object into XML text.
209
:param revision_id: The expected revision id of the inventory.
210
:param xml: A serialised inventory.
212
assert inv.revision_id is not None
213
assert inv.root.revision is not None
214
return KnitRepository.serialise_inventory(self, inv)
216
def get_commit_builder(self, branch, parents, config, timestamp=None,
217
timezone=None, committer=None, revprops=None,
219
"""Obtain a CommitBuilder for this repository.
221
:param branch: Branch to commit to.
222
:param parents: Revision ids of the parents of the new revision.
223
:param config: Configuration to use.
224
:param timestamp: Optional timestamp recorded for commit.
225
:param timezone: Optional timezone for timestamp.
226
:param committer: Optional committer to set for commit.
227
:param revprops: Optional dictionary of revision properties.
228
:param revision_id: Optional revision id.
230
return RootCommitBuilder(self, parents, config, timestamp, timezone,
231
committer, revprops, revision_id)
234
class RepositoryFormatKnit(MetaDirRepositoryFormat):
235
"""Bzr repository knit format (generalized).
237
This repository format has:
238
- knits for file texts and inventory
239
- hash subdirectory based stores.
240
- knits for revisions and signatures
241
- TextStores for revisions and signatures.
242
- a format marker of its own
243
- an optional 'shared-storage' flag
244
- an optional 'no-working-trees' flag
248
def _get_control_store(self, repo_transport, control_files):
249
"""Return the control store for this repository."""
250
return VersionedFileStore(
253
file_mode=control_files._file_mode,
254
versionedfile_class=knit.KnitVersionedFile,
255
versionedfile_kwargs={'factory':knit.KnitPlainFactory()},
258
def _get_revision_store(self, repo_transport, control_files):
259
"""See RepositoryFormat._get_revision_store()."""
260
from bzrlib.store.revision.knit import KnitRevisionStore
261
versioned_file_store = VersionedFileStore(
263
file_mode=control_files._file_mode,
266
versionedfile_class=knit.KnitVersionedFile,
267
versionedfile_kwargs={'delta':False,
268
'factory':knit.KnitPlainFactory(),
272
return KnitRevisionStore(versioned_file_store)
274
def _get_text_store(self, transport, control_files):
275
"""See RepositoryFormat._get_text_store()."""
276
return self._get_versioned_file_store('knits',
279
versionedfile_class=knit.KnitVersionedFile,
280
versionedfile_kwargs={
281
'create_parent_dir':True,
283
'dir_mode':control_files._dir_mode,
287
def initialize(self, a_bzrdir, shared=False):
288
"""Create a knit format 1 repository.
290
:param a_bzrdir: bzrdir to contain the new repository; must already
292
:param shared: If true the repository will be initialized as a shared
295
mutter('creating repository in %s.', a_bzrdir.transport.base)
296
dirs = ['revision-store', 'knits']
298
utf8_files = [('format', self.get_format_string())]
300
self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
301
repo_transport = a_bzrdir.get_repository_transport(None)
302
control_files = lockable_files.LockableFiles(repo_transport,
303
'lock', lockdir.LockDir)
304
control_store = self._get_control_store(repo_transport, control_files)
305
transaction = transactions.WriteTransaction()
306
# trigger a write of the inventory store.
307
control_store.get_weave_or_empty('inventory', transaction)
308
_revision_store = self._get_revision_store(repo_transport, control_files)
309
# the revision id here is irrelevant: it will not be stored, and cannot
311
_revision_store.has_revision_id('A', transaction)
312
_revision_store.get_signature_file(transaction)
313
return self.open(a_bzrdir=a_bzrdir, _found=True)
315
def open(self, a_bzrdir, _found=False, _override_transport=None):
316
"""See RepositoryFormat.open().
318
:param _override_transport: INTERNAL USE ONLY. Allows opening the
319
repository at a slightly different url
320
than normal. I.e. during 'upgrade'.
323
format = RepositoryFormat.find_format(a_bzrdir)
324
assert format.__class__ == self.__class__
325
if _override_transport is not None:
326
repo_transport = _override_transport
328
repo_transport = a_bzrdir.get_repository_transport(None)
329
control_files = lockable_files.LockableFiles(repo_transport,
330
'lock', lockdir.LockDir)
331
text_store = self._get_text_store(repo_transport, control_files)
332
control_store = self._get_control_store(repo_transport, control_files)
333
_revision_store = self._get_revision_store(repo_transport, control_files)
334
return KnitRepository(_format=self,
336
control_files=control_files,
337
_revision_store=_revision_store,
338
control_store=control_store,
339
text_store=text_store)
342
class RepositoryFormatKnit1(RepositoryFormatKnit):
343
"""Bzr repository knit format 1.
345
This repository format has:
346
- knits for file texts and inventory
347
- hash subdirectory based stores.
348
- knits for revisions and signatures
349
- TextStores for revisions and signatures.
350
- a format marker of its own
351
- an optional 'shared-storage' flag
352
- an optional 'no-working-trees' flag
355
This format was introduced in bzr 0.8.
357
def get_format_string(self):
358
"""See RepositoryFormat.get_format_string()."""
359
return "Bazaar-NG Knit Repository Format 1"
361
def get_format_description(self):
362
"""See RepositoryFormat.get_format_description()."""
363
return "Knit repository format 1"
365
def check_conversion_target(self, target_format):
369
class RepositoryFormatKnit2(RepositoryFormatKnit):
370
"""Bzr repository knit format 2.
372
THIS FORMAT IS EXPERIMENTAL
373
This repository format has:
374
- knits for file texts and inventory
375
- hash subdirectory based stores.
376
- knits for revisions and signatures
377
- TextStores for revisions and signatures.
378
- a format marker of its own
379
- an optional 'shared-storage' flag
380
- an optional 'no-working-trees' flag
382
- Support for recording full info about the tree root
386
rich_root_data = True
388
def get_format_string(self):
389
"""See RepositoryFormat.get_format_string()."""
390
return "Bazaar Knit Repository Format 2\n"
392
def get_format_description(self):
393
"""See RepositoryFormat.get_format_description()."""
394
return "Knit repository format 2"
396
def check_conversion_target(self, target_format):
397
if not target_format.rich_root_data:
398
raise errors.BadConversionTarget(
399
'Does not support rich root data.', target_format)
401
def open(self, a_bzrdir, _found=False, _override_transport=None):
402
"""See RepositoryFormat.open().
404
:param _override_transport: INTERNAL USE ONLY. Allows opening the
405
repository at a slightly different url
406
than normal. I.e. during 'upgrade'.
409
format = RepositoryFormat.find_format(a_bzrdir)
410
assert format.__class__ == self.__class__
411
if _override_transport is not None:
412
repo_transport = _override_transport
414
repo_transport = a_bzrdir.get_repository_transport(None)
415
control_files = lockable_files.LockableFiles(repo_transport, 'lock',
417
text_store = self._get_text_store(repo_transport, control_files)
418
control_store = self._get_control_store(repo_transport, control_files)
419
_revision_store = self._get_revision_store(repo_transport, control_files)
420
return KnitRepository2(_format=self,
422
control_files=control_files,
423
_revision_store=_revision_store,
424
control_store=control_store,
425
text_store=text_store)
428
RepositoryFormatKnit1_instance = RepositoryFormatKnit1()
429
RepositoryFormatKnit2_instance = RepositoryFormatKnit2()