14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Old weave-based repository formats"""
20
from StringIO import StringIO
17
"""Deprecated weave-based repository formats.
19
Weave based formats scaled linearly with history size and could not represent
24
from cStringIO import StringIO
27
from bzrlib.lazy_import import lazy_import
28
lazy_import(globals(), """
22
33
from bzrlib import (
40
revision as _mod_revision,
30
46
from bzrlib.decorators import needs_read_lock, needs_write_lock
31
47
from bzrlib.repository import (
49
MetaDirVersionedFileRepository,
33
50
MetaDirRepositoryFormat,
37
54
from bzrlib.store.text import TextStore
38
55
from bzrlib.trace import mutter
56
from bzrlib.tuned_gzip import GzipFile, bytes_to_gzip
57
from bzrlib.versionedfile import (
59
FulltextContentFactory,
41
64
class AllInOneRepository(Repository):
42
65
"""Legacy support - the repository behaviour for all-in-one branches."""
44
_serializer = xml5.serializer_v5
46
def __init__(self, _format, a_bzrdir, _revision_store, control_store, text_store):
68
def _serializer(self):
69
return xml5.serializer_v5
71
def _escape(self, file_or_path):
72
if not isinstance(file_or_path, basestring):
73
file_or_path = '/'.join(file_or_path)
74
if file_or_path == '':
76
return urlutils.escape(osutils.safe_unicode(file_or_path))
78
def __init__(self, _format, a_bzrdir):
47
79
# we reuse one control files instance.
48
dir_mode = a_bzrdir._control_files._dir_mode
49
file_mode = a_bzrdir._control_files._file_mode
80
dir_mode = a_bzrdir._get_dir_mode()
81
file_mode = a_bzrdir._get_file_mode()
51
83
def get_store(name, compressed=True, prefixed=False):
52
84
# FIXME: This approach of assuming stores are all entirely compressed
53
85
# or entirely uncompressed is tidy, but breaks upgrade from
54
86
# some existing branches where there's a mixture; we probably
55
87
# still want the option to look for both.
56
relpath = a_bzrdir._control_files._escape(name)
57
store = TextStore(a_bzrdir._control_files._transport.clone(relpath),
88
relpath = self._escape(name)
89
store = TextStore(a_bzrdir.transport.clone(relpath),
58
90
prefixed=prefixed, compressed=compressed,
60
92
file_mode=file_mode)
61
#if self._transport.should_cache():
62
# cache_path = os.path.join(self.cache_root, name)
63
# os.mkdir(cache_path)
64
# store = bzrlib.store.CachedStore(store, cache_path)
67
95
# not broken out yet because the controlweaves|inventory_store
68
# and text_store | weave_store bits are still different.
96
# and texts bits are still different.
69
97
if isinstance(_format, RepositoryFormat4):
70
98
# cannot remove these - there is still no consistent api
71
99
# which allows access to this old info.
72
100
self.inventory_store = get_store('inventory-store')
73
text_store = get_store('text-store')
74
super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, _revision_store, control_store, text_store)
101
self._text_store = get_store('text-store')
102
super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files)
103
self._fetch_order = 'topological'
104
self._fetch_reconcile = True
107
def _all_possible_ids(self):
108
"""Return all the possible revisions that we could find."""
109
if 'evil' in debug.debug_flags:
110
mutter_callsite(3, "_all_possible_ids scales with size of history.")
111
return [key[-1] for key in self.inventories.keys()]
114
def _all_revision_ids(self):
115
"""Returns a list of all the revision ids in the repository.
117
These are in as much topological order as the underlying store can
118
present: for weaves ghosts may lead to a lack of correctness until
119
the reweave updates the parents list.
121
return [key[-1] for key in self.revisions.keys()]
123
def _activate_new_inventory(self):
124
"""Put a replacement inventory.new into use as inventories."""
125
# Copy the content across
126
t = self.bzrdir._control_files._transport
127
t.copy('inventory.new.weave', 'inventory.weave')
128
# delete the temp inventory
129
t.delete('inventory.new.weave')
130
# Check we can parse the new weave properly as a sanity check
131
self.inventories.keys()
133
def _backup_inventory(self):
134
t = self.bzrdir._control_files._transport
135
t.copy('inventory.weave', 'inventory.backup.weave')
137
def _temp_inventories(self):
138
t = self.bzrdir._control_files._transport
139
return self._format._get_inventories(t, self, 'inventory.new')
76
141
def get_commit_builder(self, branch, parents, config, timestamp=None,
77
142
timezone=None, committer=None, revprops=None,
78
143
revision_id=None):
79
144
self._check_ascii_revisionid(revision_id, self.get_commit_builder)
80
return Repository.get_commit_builder(self, branch, parents, config,
81
timestamp, timezone, committer, revprops, revision_id)
145
result = CommitBuilder(self, parents, config, timestamp, timezone,
146
committer, revprops, revision_id)
147
self.start_write_group()
151
def get_revisions(self, revision_ids):
152
revs = self._get_revisions(revision_ids)
155
def _inventory_add_lines(self, revision_id, parents, lines,
157
"""Store lines in inv_vf and return the sha1 of the inventory."""
158
present_parents = self.get_graph().get_parent_map(parents)
160
for parent in parents:
161
if parent in present_parents:
162
final_parents.append((parent,))
163
return self.inventories.add_lines((revision_id,), final_parents, lines,
164
check_content=check_content)[0]
84
166
def is_shared(self):
85
167
"""AllInOne repositories cannot be shared."""
95
177
:param new_value: True to restore the default, False to disable making
98
raise NotImplementedError(self.set_make_working_trees)
180
raise errors.RepositoryUpgradeRequired(self.bzrdir.root_transport.base)
100
182
def make_working_trees(self):
101
183
"""Returns the policy for making working trees on new branches."""
105
class WeaveMetaDirRepository(MetaDirRepository):
186
def revision_graph_can_have_wrong_parents(self):
187
# XXX: This is an old format that we don't support full checking on, so
188
# just claim that checking for this inconsistency is not required.
192
class WeaveMetaDirRepository(MetaDirVersionedFileRepository):
106
193
"""A subclass of MetaDirRepository to set weave specific policy."""
108
_serializer = xml5.serializer_v5
196
def _serializer(self):
197
return xml5.serializer_v5
199
def __init__(self, _format, a_bzrdir, control_files):
200
super(WeaveMetaDirRepository, self).__init__(_format, a_bzrdir, control_files)
201
self._fetch_order = 'topological'
202
self._fetch_reconcile = True
205
def _all_possible_ids(self):
206
"""Return all the possible revisions that we could find."""
207
if 'evil' in debug.debug_flags:
208
mutter_callsite(3, "_all_possible_ids scales with size of history.")
209
return [key[-1] for key in self.inventories.keys()]
212
def _all_revision_ids(self):
213
"""Returns a list of all the revision ids in the repository.
215
These are in as much topological order as the underlying store can
216
present: for weaves ghosts may lead to a lack of correctness until
217
the reweave updates the parents list.
219
return [key[-1] for key in self.revisions.keys()]
221
def _activate_new_inventory(self):
222
"""Put a replacement inventory.new into use as inventories."""
223
# Copy the content across
225
t.copy('inventory.new.weave', 'inventory.weave')
226
# delete the temp inventory
227
t.delete('inventory.new.weave')
228
# Check we can parse the new weave properly as a sanity check
229
self.inventories.keys()
231
def _backup_inventory(self):
233
t.copy('inventory.weave', 'inventory.backup.weave')
235
def _temp_inventories(self):
237
return self._format._get_inventories(t, self, 'inventory.new')
110
239
def get_commit_builder(self, branch, parents, config, timestamp=None,
111
240
timezone=None, committer=None, revprops=None,
112
241
revision_id=None):
113
242
self._check_ascii_revisionid(revision_id, self.get_commit_builder)
114
return MetaDirRepository.get_commit_builder(self, branch, parents,
115
config, timestamp, timezone, committer, revprops, revision_id)
243
result = CommitBuilder(self, parents, config, timestamp, timezone,
244
committer, revprops, revision_id)
245
self.start_write_group()
249
def get_revision(self, revision_id):
250
"""Return the Revision object for a named revision"""
251
r = self.get_revision_reconcile(revision_id)
254
def _inventory_add_lines(self, revision_id, parents, lines,
256
"""Store lines in inv_vf and return the sha1 of the inventory."""
257
present_parents = self.get_graph().get_parent_map(parents)
259
for parent in parents:
260
if parent in present_parents:
261
final_parents.append((parent,))
262
return self.inventories.add_lines((revision_id,), final_parents, lines,
263
check_content=check_content)[0]
265
def revision_graph_can_have_wrong_parents(self):
118
269
class PreSplitOutRepositoryFormat(RepositoryFormat):
119
270
"""Base class for the pre split out repository formats."""
121
272
rich_root_data = False
273
supports_tree_reference = False
274
supports_ghosts = False
275
supports_external_lookups = False
123
277
def initialize(self, a_bzrdir, shared=False, _internal=False):
124
"""Create a weave repository.
126
TODO: when creating split out bzr branch formats, move this to a common
127
base for Format5, Format6. or something like that.
278
"""Create a weave repository."""
130
280
raise errors.IncompatibleFormat(self, a_bzrdir._format)
139
289
empty_weave = sio.getvalue()
141
291
mutter('creating repository in %s.', a_bzrdir.transport.base)
142
dirs = ['revision-store', 'weaves']
143
files = [('inventory.weave', StringIO(empty_weave)),
146
293
# FIXME: RBC 20060125 don't peek under the covers
147
294
# NB: no need to escape relative paths that are url safe.
148
295
control_files = lockable_files.LockableFiles(a_bzrdir.transport,
149
'branch-lock', lockable_files.TransportLock)
296
'branch-lock', lockable_files.TransportLock)
150
297
control_files.create_lock()
151
298
control_files.lock_write()
152
control_files._transport.mkdir_multi(dirs,
153
mode=control_files._dir_mode)
299
transport = a_bzrdir.transport
155
for file, content in files:
156
control_files.put(file, content)
301
transport.mkdir_multi(['revision-store', 'weaves'],
302
mode=a_bzrdir._get_dir_mode())
303
transport.put_bytes_non_atomic('inventory.weave', empty_weave)
158
305
control_files.unlock()
159
306
return self.open(a_bzrdir, _found=True)
161
def _get_control_store(self, repo_transport, control_files):
162
"""Return the control store for this repository."""
163
return self._get_versioned_file_store('',
168
def _get_text_store(self, transport, control_files):
169
"""Get a store for file texts for this format."""
170
raise NotImplementedError(self._get_text_store)
172
308
def open(self, a_bzrdir, _found=False):
173
309
"""See RepositoryFormat.open()."""
227
def _get_control_store(self, repo_transport, control_files):
228
"""Format 4 repositories have no formal control store at this point.
230
This will cause any control-file-needing apis to fail - this is desired.
363
def _get_inventories(self, repo_transport, repo, name='inventory'):
364
# No inventories store written so far.
234
def _get_revision_store(self, repo_transport, control_files):
235
"""See RepositoryFormat._get_revision_store()."""
367
def _get_revisions(self, repo_transport, repo):
236
368
from bzrlib.xml4 import serializer_v4
237
return self._get_text_rev_store(repo_transport,
240
serializer=serializer_v4)
242
def _get_text_store(self, transport, control_files):
243
"""See RepositoryFormat._get_text_store()."""
369
return RevisionTextStore(repo_transport.clone('revision-store'),
370
serializer_v4, True, versionedfile.PrefixMapper(),
371
repo.is_locked, repo.is_write_locked)
373
def _get_signatures(self, repo_transport, repo):
374
return SignatureTextStore(repo_transport.clone('revision-store'),
375
False, versionedfile.PrefixMapper(),
376
repo.is_locked, repo.is_write_locked)
378
def _get_texts(self, repo_transport, repo):
246
382
class RepositoryFormat5(PreSplitOutRepositoryFormat):
258
394
def __init__(self):
259
395
super(RepositoryFormat5, self).__init__()
396
self._fetch_order = 'topological'
397
self._fetch_reconcile = True
261
399
def get_format_description(self):
262
400
"""See RepositoryFormat.get_format_description()."""
263
401
return "Weave repository format 5"
265
def _get_revision_store(self, repo_transport, control_files):
266
"""See RepositoryFormat._get_revision_store()."""
267
"""Return the revision store object for this a_bzrdir."""
268
return self._get_text_rev_store(repo_transport,
273
def _get_text_store(self, transport, control_files):
274
"""See RepositoryFormat._get_text_store()."""
275
return self._get_versioned_file_store('weaves', transport, control_files, prefixed=False)
403
def _get_inventories(self, repo_transport, repo, name='inventory'):
404
mapper = versionedfile.ConstantMapper(name)
405
return versionedfile.ThunkedVersionedFiles(repo_transport,
406
weave.WeaveFile, mapper, repo.is_locked)
408
def _get_revisions(self, repo_transport, repo):
409
from bzrlib.xml5 import serializer_v5
410
return RevisionTextStore(repo_transport.clone('revision-store'),
411
serializer_v5, False, versionedfile.PrefixMapper(),
412
repo.is_locked, repo.is_write_locked)
414
def _get_signatures(self, repo_transport, repo):
415
return SignatureTextStore(repo_transport.clone('revision-store'),
416
False, versionedfile.PrefixMapper(),
417
repo.is_locked, repo.is_write_locked)
419
def _get_texts(self, repo_transport, repo):
420
mapper = versionedfile.PrefixMapper()
421
base_transport = repo_transport.clone('weaves')
422
return versionedfile.ThunkedVersionedFiles(base_transport,
423
weave.WeaveFile, mapper, repo.is_locked)
278
426
class RepositoryFormat6(PreSplitOutRepositoryFormat):
290
438
def __init__(self):
291
439
super(RepositoryFormat6, self).__init__()
440
self._fetch_order = 'topological'
441
self._fetch_reconcile = True
293
443
def get_format_description(self):
294
444
"""See RepositoryFormat.get_format_description()."""
295
445
return "Weave repository format 6"
297
def _get_revision_store(self, repo_transport, control_files):
298
"""See RepositoryFormat._get_revision_store()."""
299
return self._get_text_rev_store(repo_transport,
305
def _get_text_store(self, transport, control_files):
306
"""See RepositoryFormat._get_text_store()."""
307
return self._get_versioned_file_store('weaves', transport, control_files)
447
def _get_inventories(self, repo_transport, repo, name='inventory'):
448
mapper = versionedfile.ConstantMapper(name)
449
return versionedfile.ThunkedVersionedFiles(repo_transport,
450
weave.WeaveFile, mapper, repo.is_locked)
452
def _get_revisions(self, repo_transport, repo):
453
from bzrlib.xml5 import serializer_v5
454
return RevisionTextStore(repo_transport.clone('revision-store'),
455
serializer_v5, False, versionedfile.HashPrefixMapper(),
456
repo.is_locked, repo.is_write_locked)
458
def _get_signatures(self, repo_transport, repo):
459
return SignatureTextStore(repo_transport.clone('revision-store'),
460
False, versionedfile.HashPrefixMapper(),
461
repo.is_locked, repo.is_write_locked)
463
def _get_texts(self, repo_transport, repo):
464
mapper = versionedfile.HashPrefixMapper()
465
base_transport = repo_transport.clone('weaves')
466
return versionedfile.ThunkedVersionedFiles(base_transport,
467
weave.WeaveFile, mapper, repo.is_locked)
310
470
class RepositoryFormat7(MetaDirRepositoryFormat):
339
493
def check_conversion_target(self, target_format):
342
def _get_revision_store(self, repo_transport, control_files):
343
"""See RepositoryFormat._get_revision_store()."""
344
return self._get_text_rev_store(repo_transport,
351
def _get_text_store(self, transport, control_files):
352
"""See RepositoryFormat._get_text_store()."""
353
return self._get_versioned_file_store('weaves',
496
def _get_inventories(self, repo_transport, repo, name='inventory'):
497
mapper = versionedfile.ConstantMapper(name)
498
return versionedfile.ThunkedVersionedFiles(repo_transport,
499
weave.WeaveFile, mapper, repo.is_locked)
501
def _get_revisions(self, repo_transport, repo):
502
from bzrlib.xml5 import serializer_v5
503
return RevisionTextStore(repo_transport.clone('revision-store'),
504
serializer_v5, True, versionedfile.HashPrefixMapper(),
505
repo.is_locked, repo.is_write_locked)
507
def _get_signatures(self, repo_transport, repo):
508
return SignatureTextStore(repo_transport.clone('revision-store'),
509
True, versionedfile.HashPrefixMapper(),
510
repo.is_locked, repo.is_write_locked)
512
def _get_texts(self, repo_transport, repo):
513
mapper = versionedfile.HashPrefixMapper()
514
base_transport = repo_transport.clone('weaves')
515
return versionedfile.ThunkedVersionedFiles(base_transport,
516
weave.WeaveFile, mapper, repo.is_locked)
357
518
def initialize(self, a_bzrdir, shared=False):
358
519
"""Create a weave repository.
385
546
format = RepositoryFormat.find_format(a_bzrdir)
386
assert format.__class__ == self.__class__
387
547
if _override_transport is not None:
388
548
repo_transport = _override_transport
390
550
repo_transport = a_bzrdir.get_repository_transport(None)
391
551
control_files = lockable_files.LockableFiles(repo_transport,
392
552
'lock', lockdir.LockDir)
393
text_store = self._get_text_store(repo_transport, control_files)
394
control_store = self._get_control_store(repo_transport, control_files)
395
_revision_store = self._get_revision_store(repo_transport, control_files)
396
return WeaveMetaDirRepository(_format=self,
398
control_files=control_files,
399
_revision_store=_revision_store,
400
control_store=control_store,
401
text_store=text_store)
553
result = WeaveMetaDirRepository(_format=self, a_bzrdir=a_bzrdir,
554
control_files=control_files)
555
result.revisions = self._get_revisions(repo_transport, result)
556
result.signatures = self._get_signatures(repo_transport, result)
557
result.inventories = self._get_inventories(repo_transport, result)
558
result.texts = self._get_texts(repo_transport, result)
559
result._transport = repo_transport
563
class TextVersionedFiles(VersionedFiles):
564
"""Just-a-bunch-of-files based VersionedFile stores."""
566
def __init__(self, transport, compressed, mapper, is_locked, can_write):
567
self._compressed = compressed
568
self._transport = transport
569
self._mapper = mapper
574
self._is_locked = is_locked
575
self._can_write = can_write
577
def add_lines(self, key, parents, lines):
578
"""Add a revision to the store."""
579
if not self._is_locked():
580
raise errors.ObjectNotLocked(self)
581
if not self._can_write():
582
raise errors.ReadOnlyError(self)
584
raise ValueError('bad idea to put / in %r' % (key,))
585
text = ''.join(lines)
587
text = bytes_to_gzip(text)
588
path = self._map(key)
589
self._transport.put_bytes_non_atomic(path, text, create_parent_dir=True)
591
def insert_record_stream(self, stream):
593
for record in stream:
594
# Raise an error when a record is missing.
595
if record.storage_kind == 'absent':
596
raise errors.RevisionNotPresent([record.key[0]], self)
597
# adapt to non-tuple interface
598
if record.storage_kind == 'fulltext':
599
self.add_lines(record.key, None,
600
osutils.split_lines(record.get_bytes_as('fulltext')))
602
adapter_key = record.storage_kind, 'fulltext'
604
adapter = adapters[adapter_key]
606
adapter_factory = adapter_registry.get(adapter_key)
607
adapter = adapter_factory(self)
608
adapters[adapter_key] = adapter
609
lines = osutils.split_lines(adapter.get_bytes(
610
record, record.get_bytes_as(record.storage_kind)))
612
self.add_lines(record.key, None, lines)
613
except RevisionAlreadyPresent:
616
def _load_text(self, key):
617
if not self._is_locked():
618
raise errors.ObjectNotLocked(self)
619
path = self._map(key)
621
text = self._transport.get_bytes(path)
622
compressed = self._compressed
623
except errors.NoSuchFile:
625
# try without the .gz
628
text = self._transport.get_bytes(path)
630
except errors.NoSuchFile:
635
text = GzipFile(mode='rb', fileobj=StringIO(text)).read()
639
return self._mapper.map(key) + self._ext
642
class RevisionTextStore(TextVersionedFiles):
643
"""Legacy thunk for format 4 repositories."""
645
def __init__(self, transport, serializer, compressed, mapper, is_locked,
647
"""Create a RevisionTextStore at transport with serializer."""
648
TextVersionedFiles.__init__(self, transport, compressed, mapper,
649
is_locked, can_write)
650
self._serializer = serializer
652
def _load_text_parents(self, key):
653
text = self._load_text(key)
656
parents = self._serializer.read_revision_from_string(text).parent_ids
657
return text, tuple((parent,) for parent in parents)
659
def get_parent_map(self, keys):
662
parents = self._load_text_parents(key)[1]
665
result[key] = parents
668
def get_record_stream(self, keys, sort_order, include_delta_closure):
670
text, parents = self._load_text_parents(key)
672
yield AbsentContentFactory(key)
674
yield FulltextContentFactory(key, parents, None, text)
677
if not self._is_locked():
678
raise errors.ObjectNotLocked(self)
680
for quoted_relpath in self._transport.iter_files_recursive():
681
relpath = urllib.unquote(quoted_relpath)
682
path, ext = os.path.splitext(relpath)
685
if '.sig' not in relpath:
686
relpaths.add(relpath)
687
paths = list(relpaths)
688
return set([self._mapper.unmap(path) for path in paths])
691
class SignatureTextStore(TextVersionedFiles):
692
"""Legacy thunk for format 4-7 repositories."""
694
def __init__(self, transport, compressed, mapper, is_locked, can_write):
695
TextVersionedFiles.__init__(self, transport, compressed, mapper,
696
is_locked, can_write)
697
self._ext = '.sig' + self._ext
699
def get_parent_map(self, keys):
702
text = self._load_text(key)
708
def get_record_stream(self, keys, sort_order, include_delta_closure):
710
text = self._load_text(key)
712
yield AbsentContentFactory(key)
714
yield FulltextContentFactory(key, None, None, text)
717
if not self._is_locked():
718
raise errors.ObjectNotLocked(self)
720
for quoted_relpath in self._transport.iter_files_recursive():
721
relpath = urllib.unquote(quoted_relpath)
722
path, ext = os.path.splitext(relpath)
725
if not relpath.endswith('.sig'):
727
relpaths.add(relpath[:-4])
728
paths = list(relpaths)
729
return set([self._mapper.unmap(path) for path in paths])
404
731
_legacy_formats = [RepositoryFormat4(),
405
732
RepositoryFormat5(),