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
"""Deprecated weave-based repository formats.
19
Weave based formats scaled linearly with history size and could not represent
23
from StringIO import StringIO
25
from bzrlib.lazy_import import lazy_import
26
lazy_import(globals(), """
38
revision as _mod_revision,
41
from bzrlib.decorators import needs_read_lock, needs_write_lock
42
from bzrlib.repository import (
45
MetaDirRepositoryFormat,
49
from bzrlib.store.text import TextStore
50
from bzrlib.trace import mutter
53
class AllInOneRepository(Repository):
54
"""Legacy support - the repository behaviour for all-in-one branches."""
57
def _serializer(self):
58
return xml5.serializer_v5
60
def __init__(self, _format, a_bzrdir, _revision_store, control_store, text_store):
61
# we reuse one control files instance.
62
dir_mode = a_bzrdir._control_files._dir_mode
63
file_mode = a_bzrdir._control_files._file_mode
65
def get_store(name, compressed=True, prefixed=False):
66
# FIXME: This approach of assuming stores are all entirely compressed
67
# or entirely uncompressed is tidy, but breaks upgrade from
68
# some existing branches where there's a mixture; we probably
69
# still want the option to look for both.
70
relpath = a_bzrdir._control_files._escape(name)
71
store = TextStore(a_bzrdir._control_files._transport.clone(relpath),
72
prefixed=prefixed, compressed=compressed,
77
# not broken out yet because the controlweaves|inventory_store
78
# and text_store | weave_store bits are still different.
79
if isinstance(_format, RepositoryFormat4):
80
# cannot remove these - there is still no consistent api
81
# which allows access to this old info.
82
self.inventory_store = get_store('inventory-store')
83
text_store = get_store('text-store')
84
super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, _revision_store, control_store, text_store)
87
def _all_possible_ids(self):
88
"""Return all the possible revisions that we could find."""
89
if 'evil' in debug.debug_flags:
90
mutter_callsite(3, "_all_possible_ids scales with size of history.")
91
return self.get_inventory_weave().versions()
94
def _all_revision_ids(self):
95
"""Returns a list of all the revision ids in the repository.
97
These are in as much topological order as the underlying store can
98
present: for weaves ghosts may lead to a lack of correctness until
99
the reweave updates the parents list.
101
if self._revision_store.text_store.listable():
102
return self._revision_store.all_revision_ids(self.get_transaction())
103
result = self._all_possible_ids()
104
# TODO: jam 20070210 Ensure that _all_possible_ids returns non-unicode
105
# ids. (It should, since _revision_store's API should change to
106
# return utf8 revision_ids)
107
return self._eliminate_revisions_not_present(result)
109
def _check_revision_parents(self, revision, inventory):
110
"""Private to Repository and Fetch.
112
This checks the parentage of revision in an inventory weave for
113
consistency and is only applicable to inventory-weave-for-ancestry
114
using repository formats & fetchers.
116
weave_parents = inventory.get_parents(revision.revision_id)
117
weave_names = inventory.versions()
118
for parent_id in revision.parent_ids:
119
if parent_id in weave_names:
120
# this parent must not be a ghost.
121
if not parent_id in weave_parents:
123
raise errors.CorruptRepository(self)
125
def get_commit_builder(self, branch, parents, config, timestamp=None,
126
timezone=None, committer=None, revprops=None,
128
self._check_ascii_revisionid(revision_id, self.get_commit_builder)
129
result = WeaveCommitBuilder(self, parents, config, timestamp, timezone,
130
committer, revprops, revision_id)
131
self.start_write_group()
135
def get_revisions(self, revision_ids):
136
revs = self._get_revisions(revision_ids)
137
# weave corruption can lead to absent revision markers that should be
139
# the following test is reasonably cheap (it needs a single weave read)
140
# and the weave is cached in read transactions. In write transactions
141
# it is not cached but typically we only read a small number of
142
# revisions. For knits when they are introduced we will probably want
143
# to ensure that caching write transactions are in use.
144
inv = self.get_inventory_weave()
146
self._check_revision_parents(rev, inv)
150
def get_revision_graph(self, revision_id=None):
151
"""Return a dictionary containing the revision graph.
153
:param revision_id: The revision_id to get a graph from. If None, then
154
the entire revision graph is returned. This is a deprecated mode of
155
operation and will be removed in the future.
156
:return: a dictionary of revision_id->revision_parents_list.
158
if 'evil' in debug.debug_flags:
160
"get_revision_graph scales with size of history.")
161
# special case NULL_REVISION
162
if revision_id == _mod_revision.NULL_REVISION:
164
a_weave = self.get_inventory_weave()
165
all_revisions = self._eliminate_revisions_not_present(
167
entire_graph = dict([(node, tuple(a_weave.get_parents(node))) for
168
node in all_revisions])
169
if revision_id is None:
171
elif revision_id not in entire_graph:
172
raise errors.NoSuchRevision(self, revision_id)
174
# add what can be reached from revision_id
176
pending = set([revision_id])
177
while len(pending) > 0:
179
result[node] = entire_graph[node]
180
for revision_id in result[node]:
181
if revision_id not in result:
182
pending.add(revision_id)
185
def has_revisions(self, revision_ids):
186
"""See Repository.has_revisions()."""
188
transaction = self.get_transaction()
189
for revision_id in revision_ids:
190
if self._revision_store.has_revision_id(revision_id, transaction):
191
result.add(revision_id)
196
"""AllInOne repositories cannot be shared."""
200
def set_make_working_trees(self, new_value):
201
"""Set the policy flag for making working trees when creating branches.
203
This only applies to branches that use this repository.
205
The default is 'True'.
206
:param new_value: True to restore the default, False to disable making
209
raise NotImplementedError(self.set_make_working_trees)
211
def make_working_trees(self):
212
"""Returns the policy for making working trees on new branches."""
215
def revision_graph_can_have_wrong_parents(self):
216
# XXX: This is an old format that we don't support full checking on, so
217
# just claim that checking for this inconsistency is not required.
221
class WeaveMetaDirRepository(MetaDirRepository):
222
"""A subclass of MetaDirRepository to set weave specific policy."""
225
def _serializer(self):
226
return xml5.serializer_v5
229
def _all_possible_ids(self):
230
"""Return all the possible revisions that we could find."""
231
if 'evil' in debug.debug_flags:
232
mutter_callsite(3, "_all_possible_ids scales with size of history.")
233
return self.get_inventory_weave().versions()
236
def _all_revision_ids(self):
237
"""Returns a list of all the revision ids in the repository.
239
These are in as much topological order as the underlying store can
240
present: for weaves ghosts may lead to a lack of correctness until
241
the reweave updates the parents list.
243
if self._revision_store.text_store.listable():
244
return self._revision_store.all_revision_ids(self.get_transaction())
245
result = self._all_possible_ids()
246
# TODO: jam 20070210 Ensure that _all_possible_ids returns non-unicode
247
# ids. (It should, since _revision_store's API should change to
248
# return utf8 revision_ids)
249
return self._eliminate_revisions_not_present(result)
251
def _check_revision_parents(self, revision, inventory):
252
"""Private to Repository and Fetch.
254
This checks the parentage of revision in an inventory weave for
255
consistency and is only applicable to inventory-weave-for-ancestry
256
using repository formats & fetchers.
258
weave_parents = inventory.get_parents(revision.revision_id)
259
weave_names = inventory.versions()
260
for parent_id in revision.parent_ids:
261
if parent_id in weave_names:
262
# this parent must not be a ghost.
263
if not parent_id in weave_parents:
265
raise errors.CorruptRepository(self)
267
def get_commit_builder(self, branch, parents, config, timestamp=None,
268
timezone=None, committer=None, revprops=None,
270
self._check_ascii_revisionid(revision_id, self.get_commit_builder)
271
result = WeaveCommitBuilder(self, parents, config, timestamp, timezone,
272
committer, revprops, revision_id)
273
self.start_write_group()
277
def get_revision(self, revision_id):
278
"""Return the Revision object for a named revision"""
279
# TODO: jam 20070210 get_revision_reconcile should do this for us
280
r = self.get_revision_reconcile(revision_id)
281
# weave corruption can lead to absent revision markers that should be
283
# the following test is reasonably cheap (it needs a single weave read)
284
# and the weave is cached in read transactions. In write transactions
285
# it is not cached but typically we only read a small number of
286
# revisions. For knits when they are introduced we will probably want
287
# to ensure that caching write transactions are in use.
288
inv = self.get_inventory_weave()
289
self._check_revision_parents(r, inv)
293
def get_revision_graph(self, revision_id=None):
294
"""Return a dictionary containing the revision graph.
296
:param revision_id: The revision_id to get a graph from. If None, then
297
the entire revision graph is returned. This is a deprecated mode of
298
operation and will be removed in the future.
299
:return: a dictionary of revision_id->revision_parents_list.
301
if 'evil' in debug.debug_flags:
303
"get_revision_graph scales with size of history.")
304
# special case NULL_REVISION
305
if revision_id == _mod_revision.NULL_REVISION:
307
a_weave = self.get_inventory_weave()
308
all_revisions = self._eliminate_revisions_not_present(
310
entire_graph = dict([(node, tuple(a_weave.get_parents(node))) for
311
node in all_revisions])
312
if revision_id is None:
314
elif revision_id not in entire_graph:
315
raise errors.NoSuchRevision(self, revision_id)
317
# add what can be reached from revision_id
319
pending = set([revision_id])
320
while len(pending) > 0:
322
result[node] = entire_graph[node]
323
for revision_id in result[node]:
324
if revision_id not in result:
325
pending.add(revision_id)
328
def has_revisions(self, revision_ids):
329
"""See Repository.has_revisions()."""
331
transaction = self.get_transaction()
332
for revision_id in revision_ids:
333
if self._revision_store.has_revision_id(revision_id, transaction):
334
result.add(revision_id)
337
def revision_graph_can_have_wrong_parents(self):
338
# XXX: This is an old format that we don't support full checking on, so
339
# just claim that checking for this inconsistency is not required.
343
class PreSplitOutRepositoryFormat(RepositoryFormat):
344
"""Base class for the pre split out repository formats."""
346
rich_root_data = False
347
supports_tree_reference = False
348
supports_ghosts = False
350
def initialize(self, a_bzrdir, shared=False, _internal=False):
351
"""Create a weave repository."""
353
raise errors.IncompatibleFormat(self, a_bzrdir._format)
356
# always initialized when the bzrdir is.
357
return self.open(a_bzrdir, _found=True)
359
# Create an empty weave
361
weavefile.write_weave_v5(weave.Weave(), sio)
362
empty_weave = sio.getvalue()
364
mutter('creating repository in %s.', a_bzrdir.transport.base)
365
dirs = ['revision-store', 'weaves']
366
files = [('inventory.weave', StringIO(empty_weave)),
369
# FIXME: RBC 20060125 don't peek under the covers
370
# NB: no need to escape relative paths that are url safe.
371
control_files = lockable_files.LockableFiles(a_bzrdir.transport,
372
'branch-lock', lockable_files.TransportLock)
373
control_files.create_lock()
374
control_files.lock_write()
375
control_files._transport.mkdir_multi(dirs,
376
mode=control_files._dir_mode)
378
for file, content in files:
379
control_files.put(file, content)
381
control_files.unlock()
382
return self.open(a_bzrdir, _found=True)
384
def _get_control_store(self, repo_transport, control_files):
385
"""Return the control store for this repository."""
386
return self._get_versioned_file_store('',
391
def _get_text_store(self, transport, control_files):
392
"""Get a store for file texts for this format."""
393
raise NotImplementedError(self._get_text_store)
395
def open(self, a_bzrdir, _found=False):
396
"""See RepositoryFormat.open()."""
398
# we are being called directly and must probe.
399
raise NotImplementedError
401
repo_transport = a_bzrdir.get_repository_transport(None)
402
control_files = a_bzrdir._control_files
403
text_store = self._get_text_store(repo_transport, control_files)
404
control_store = self._get_control_store(repo_transport, control_files)
405
_revision_store = self._get_revision_store(repo_transport, control_files)
406
return AllInOneRepository(_format=self,
408
_revision_store=_revision_store,
409
control_store=control_store,
410
text_store=text_store)
412
def check_conversion_target(self, target_format):
416
class RepositoryFormat4(PreSplitOutRepositoryFormat):
417
"""Bzr repository format 4.
419
This repository format has:
421
- TextStores for texts, inventories,revisions.
423
This format is deprecated: it indexes texts using a text id which is
424
removed in format 5; initialization and write support for this format
428
_matchingbzrdir = bzrdir.BzrDirFormat4()
431
super(RepositoryFormat4, self).__init__()
433
def get_format_description(self):
434
"""See RepositoryFormat.get_format_description()."""
435
return "Repository format 4"
437
def initialize(self, url, shared=False, _internal=False):
438
"""Format 4 branches cannot be created."""
439
raise errors.UninitializableFormat(self)
441
def is_supported(self):
442
"""Format 4 is not supported.
444
It is not supported because the model changed from 4 to 5 and the
445
conversion logic is expensive - so doing it on the fly was not
450
def _get_control_store(self, repo_transport, control_files):
451
"""Format 4 repositories have no formal control store at this point.
453
This will cause any control-file-needing apis to fail - this is desired.
457
def _get_revision_store(self, repo_transport, control_files):
458
"""See RepositoryFormat._get_revision_store()."""
459
from bzrlib.xml4 import serializer_v4
460
return self._get_text_rev_store(repo_transport,
463
serializer=serializer_v4)
465
def _get_text_store(self, transport, control_files):
466
"""See RepositoryFormat._get_text_store()."""
469
class RepositoryFormat5(PreSplitOutRepositoryFormat):
470
"""Bzr control format 5.
472
This repository format has:
473
- weaves for file texts and inventory
475
- TextStores for revisions and signatures.
478
_versionedfile_class = weave.WeaveFile
479
_matchingbzrdir = bzrdir.BzrDirFormat5()
482
super(RepositoryFormat5, self).__init__()
484
def get_format_description(self):
485
"""See RepositoryFormat.get_format_description()."""
486
return "Weave repository format 5"
488
def _get_revision_store(self, repo_transport, control_files):
489
"""See RepositoryFormat._get_revision_store()."""
490
"""Return the revision store object for this a_bzrdir."""
491
return self._get_text_rev_store(repo_transport,
496
def _get_text_store(self, transport, control_files):
497
"""See RepositoryFormat._get_text_store()."""
498
return self._get_versioned_file_store('weaves', transport, control_files, prefixed=False)
501
class RepositoryFormat6(PreSplitOutRepositoryFormat):
502
"""Bzr control format 6.
504
This repository format has:
505
- weaves for file texts and inventory
506
- hash subdirectory based stores.
507
- TextStores for revisions and signatures.
510
_versionedfile_class = weave.WeaveFile
511
_matchingbzrdir = bzrdir.BzrDirFormat6()
514
super(RepositoryFormat6, self).__init__()
516
def get_format_description(self):
517
"""See RepositoryFormat.get_format_description()."""
518
return "Weave repository format 6"
520
def _get_revision_store(self, repo_transport, control_files):
521
"""See RepositoryFormat._get_revision_store()."""
522
return self._get_text_rev_store(repo_transport,
528
def _get_text_store(self, transport, control_files):
529
"""See RepositoryFormat._get_text_store()."""
530
return self._get_versioned_file_store('weaves', transport, control_files)
532
class RepositoryFormat7(MetaDirRepositoryFormat):
535
This repository format has:
536
- weaves for file texts and inventory
537
- hash subdirectory based stores.
538
- TextStores for revisions and signatures.
539
- a format marker of its own
540
- an optional 'shared-storage' flag
541
- an optional 'no-working-trees' flag
544
_versionedfile_class = weave.WeaveFile
545
supports_ghosts = False
547
def _get_control_store(self, repo_transport, control_files):
548
"""Return the control store for this repository."""
549
return self._get_versioned_file_store('',
554
def get_format_string(self):
555
"""See RepositoryFormat.get_format_string()."""
556
return "Bazaar-NG Repository format 7"
558
def get_format_description(self):
559
"""See RepositoryFormat.get_format_description()."""
560
return "Weave repository format 7"
562
def check_conversion_target(self, target_format):
565
def _get_revision_store(self, repo_transport, control_files):
566
"""See RepositoryFormat._get_revision_store()."""
567
return self._get_text_rev_store(repo_transport,
574
def _get_text_store(self, transport, control_files):
575
"""See RepositoryFormat._get_text_store()."""
576
return self._get_versioned_file_store('weaves',
580
def initialize(self, a_bzrdir, shared=False):
581
"""Create a weave repository.
583
:param shared: If true the repository will be initialized as a shared
586
# Create an empty weave
588
weavefile.write_weave_v5(weave.Weave(), sio)
589
empty_weave = sio.getvalue()
591
mutter('creating repository in %s.', a_bzrdir.transport.base)
592
dirs = ['revision-store', 'weaves']
593
files = [('inventory.weave', StringIO(empty_weave)),
595
utf8_files = [('format', self.get_format_string())]
597
self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
598
return self.open(a_bzrdir=a_bzrdir, _found=True)
600
def open(self, a_bzrdir, _found=False, _override_transport=None):
601
"""See RepositoryFormat.open().
603
:param _override_transport: INTERNAL USE ONLY. Allows opening the
604
repository at a slightly different url
605
than normal. I.e. during 'upgrade'.
608
format = RepositoryFormat.find_format(a_bzrdir)
609
assert format.__class__ == self.__class__
610
if _override_transport is not None:
611
repo_transport = _override_transport
613
repo_transport = a_bzrdir.get_repository_transport(None)
614
control_files = lockable_files.LockableFiles(repo_transport,
615
'lock', lockdir.LockDir)
616
text_store = self._get_text_store(repo_transport, control_files)
617
control_store = self._get_control_store(repo_transport, control_files)
618
_revision_store = self._get_revision_store(repo_transport, control_files)
619
return WeaveMetaDirRepository(_format=self,
621
control_files=control_files,
622
_revision_store=_revision_store,
623
control_store=control_store,
624
text_store=text_store)
627
class WeaveCommitBuilder(CommitBuilder):
628
"""A builder for weave based repos that don't support ghosts."""
630
def _add_text_to_weave(self, file_id, new_lines, parents, nostore_sha):
631
versionedfile = self.repository.weave_store.get_weave_or_empty(
632
file_id, self.repository.get_transaction())
633
result = versionedfile.add_lines(
634
self._new_revision_id, parents, new_lines,
635
nostore_sha=nostore_sha)[0:2]
636
versionedfile.clear_cache()
640
_legacy_formats = [RepositoryFormat4(),