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
17
from binascii import hexlify
18
from copy import deepcopy
17
19
from cStringIO import StringIO
19
from bzrlib.lazy_import import lazy_import
20
lazy_import(globals(), """
22
from unittest import TestSuite
38
revision as _mod_revision,
24
from bzrlib import bzrdir, check, delta, gpg, errors, xml5, ui, transactions, osutils
25
from bzrlib.decorators import needs_read_lock, needs_write_lock
26
from bzrlib.errors import InvalidRevisionId
27
from bzrlib.graph import Graph
28
from bzrlib.inter import InterObject
29
from bzrlib.inventory import Inventory
30
from bzrlib.knit import KnitVersionedFile, KnitPlainFactory
31
from bzrlib.lockable_files import LockableFiles, TransportLock
32
from bzrlib.lockdir import LockDir
33
from bzrlib.osutils import (safe_unicode, rand_bytes, compact_date,
35
from bzrlib.revision import NULL_REVISION, Revision
43
36
from bzrlib.revisiontree import RevisionTree
44
from bzrlib.store.versioned import VersionedFileStore
37
from bzrlib.store.versioned import VersionedFileStore, WeaveStore
45
38
from bzrlib.store.text import TextStore
39
from bzrlib.symbol_versioning import (deprecated_method,
46
42
from bzrlib.testament import Testament
50
from bzrlib.decorators import needs_read_lock, needs_write_lock
51
from bzrlib.inter import InterObject
52
from bzrlib.inventory import Inventory, InventoryDirectory, ROOT_ID
53
from bzrlib.symbol_versioning import (
57
from bzrlib.trace import mutter, note, warning
60
# Old formats display a warning, but only once
61
_deprecation_warning_done = False
64
######################################################################
43
from bzrlib.trace import mutter, note
44
from bzrlib.tsort import topo_sort
45
from bzrlib.weave import WeaveFile
67
48
class Repository(object):
68
49
"""Repository holding history for one or more branches.
79
_file_ids_altered_regex = lazy_regex.lazy_compile(
80
r'file_id="(?P<file_id>[^"]+)"'
81
r'.*revision="(?P<revision_id>[^"]+)"'
85
def add_inventory(self, revision_id, inv, parents):
86
"""Add the inventory inv to the repository as revision_id.
61
def add_inventory(self, revid, inv, parents):
62
"""Add the inventory inv to the repository as revid.
88
:param parents: The revision ids of the parents that revision_id
64
:param parents: The revision ids of the parents that revid
89
65
is known to have and are in the repository already.
91
67
returns the sha1 of the serialized inventory.
93
revision_id = osutils.safe_revision_id(revision_id)
94
_mod_revision.check_not_reserved_id(revision_id)
95
assert inv.revision_id is None or inv.revision_id == revision_id, \
69
assert inv.revision_id is None or inv.revision_id == revid, \
96
70
"Mismatch between inventory revision" \
97
" id and insertion revid (%r, %r)" % (inv.revision_id, revision_id)
98
assert inv.root is not None
99
inv_text = self.serialise_inventory(inv)
71
" id and insertion revid (%r, %r)" % (inv.revision_id, revid)
72
inv_text = xml5.serializer_v5.write_inventory_to_string(inv)
100
73
inv_sha1 = osutils.sha_string(inv_text)
101
74
inv_vf = self.control_weaves.get_weave('inventory',
102
75
self.get_transaction())
103
self._inventory_add_lines(inv_vf, revision_id, parents,
104
osutils.split_lines(inv_text))
76
self._inventory_add_lines(inv_vf, revid, parents, osutils.split_lines(inv_text))
107
def _inventory_add_lines(self, inv_vf, revision_id, parents, lines):
79
def _inventory_add_lines(self, inv_vf, revid, parents, lines):
108
80
final_parents = []
109
81
for parent in parents:
110
82
if parent in inv_vf:
111
83
final_parents.append(parent)
113
inv_vf.add_lines(revision_id, final_parents, lines)
85
inv_vf.add_lines(revid, final_parents, lines)
116
def add_revision(self, revision_id, rev, inv=None, config=None):
117
"""Add rev to the revision store as revision_id.
88
def add_revision(self, rev_id, rev, inv=None, config=None):
89
"""Add rev to the revision store as rev_id.
119
:param revision_id: the revision id to use.
91
:param rev_id: the revision id to use.
120
92
:param rev: The revision object.
121
93
:param inv: The inventory for the revision. if None, it will be looked
122
94
up in the inventory storer
257
205
def get_physical_lock_status(self):
258
206
return self.control_files.get_physical_lock_status()
260
def leave_lock_in_place(self):
261
"""Tell this repository not to release the physical lock when this
264
If lock_write doesn't return a token, then this method is not supported.
266
self.control_files.leave_in_place()
268
def dont_leave_lock_in_place(self):
269
"""Tell this repository to release the physical lock when this
270
object is unlocked, even if it didn't originally acquire it.
272
If lock_write doesn't return a token, then this method is not supported.
274
self.control_files.dont_leave_in_place()
277
def gather_stats(self, revid=None, committers=None):
278
"""Gather statistics from a revision id.
280
:param revid: The revision id to gather statistics from, if None, then
281
no revision specific statistics are gathered.
282
:param committers: Optional parameter controlling whether to grab
283
a count of committers from the revision specific statistics.
284
:return: A dictionary of statistics. Currently this contains:
285
committers: The number of committers if requested.
286
firstrev: A tuple with timestamp, timezone for the penultimate left
287
most ancestor of revid, if revid is not the NULL_REVISION.
288
latestrev: A tuple with timestamp, timezone for revid, if revid is
289
not the NULL_REVISION.
290
revisions: The total revision count in the repository.
291
size: An estimate disk size of the repository in bytes.
294
if revid and committers:
295
result['committers'] = 0
296
if revid and revid != _mod_revision.NULL_REVISION:
298
all_committers = set()
299
revisions = self.get_ancestry(revid)
300
# pop the leading None
302
first_revision = None
304
# ignore the revisions in the middle - just grab first and last
305
revisions = revisions[0], revisions[-1]
306
for revision in self.get_revisions(revisions):
307
if not first_revision:
308
first_revision = revision
310
all_committers.add(revision.committer)
311
last_revision = revision
313
result['committers'] = len(all_committers)
314
result['firstrev'] = (first_revision.timestamp,
315
first_revision.timezone)
316
result['latestrev'] = (last_revision.timestamp,
317
last_revision.timezone)
319
# now gather global repository information
320
if self.bzrdir.root_transport.listable():
321
c, t = self._revision_store.total_size(self.get_transaction())
322
result['revisions'] = c
327
209
def missing_revision_ids(self, other, revision_id=None):
328
210
"""Return the revision ids that other has that this does not.
379
255
:param revprops: Optional dictionary of revision properties.
380
256
:param revision_id: Optional revision id.
382
revision_id = osutils.safe_revision_id(revision_id)
383
return _CommitBuilder(self, parents, config, timestamp, timezone,
384
committer, revprops, revision_id)
258
return CommitBuilder(self, parents, config, timestamp, timezone,
259
committer, revprops, revision_id)
386
261
def unlock(self):
387
262
self.control_files.unlock()
390
def clone(self, a_bzrdir, revision_id=None):
265
def clone(self, a_bzrdir, revision_id=None, basis=None):
391
266
"""Clone this repository into a_bzrdir using the current format.
393
268
Currently no check is made that the format of this repository and
394
269
the bzrdir format are compatible. FIXME RBC 20060201.
396
:return: The newly created destination repository.
398
# TODO: deprecate after 0.16; cloning this with all its settings is
399
# probably not very useful -- mbp 20070423
400
dest_repo = self._create_sprouting_repo(a_bzrdir, shared=self.is_shared())
401
self.copy_content_into(dest_repo, revision_id)
405
def sprout(self, to_bzrdir, revision_id=None):
406
"""Create a descendent repository for new development.
408
Unlike clone, this does not copy the settings of the repository.
410
dest_repo = self._create_sprouting_repo(to_bzrdir, shared=False)
411
dest_repo.fetch(self, revision_id=revision_id)
414
def _create_sprouting_repo(self, a_bzrdir, shared):
415
271
if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
416
272
# use target default format.
417
dest_repo = a_bzrdir.create_repository()
273
result = a_bzrdir.create_repository()
274
# FIXME RBC 20060209 split out the repository type to avoid this check ?
275
elif isinstance(a_bzrdir._format,
276
(bzrdir.BzrDirFormat4,
277
bzrdir.BzrDirFormat5,
278
bzrdir.BzrDirFormat6)):
279
result = a_bzrdir.open_repository()
419
# Most control formats need the repository to be specifically
420
# created, but on some old all-in-one formats it's not needed
422
dest_repo = self._format.initialize(a_bzrdir, shared=shared)
423
except errors.UninitializableFormat:
424
dest_repo = a_bzrdir.open_repository()
281
result = self._format.initialize(a_bzrdir, shared=self.is_shared())
282
self.copy_content_into(result, revision_id, basis)
428
286
def has_revision(self, revision_id):
429
287
"""True if this repository has a copy of the revision."""
430
revision_id = osutils.safe_revision_id(revision_id)
431
288
return self._revision_store.has_revision_id(revision_id,
432
289
self.get_transaction())
565
410
# revisions. We don't need to see all lines in the inventory because
566
411
# only those added in an inventory in rev X can contain a revision=X
568
unescape_revid_cache = {}
569
unescape_fileid_cache = {}
571
# jam 20061218 In a big fetch, this handles hundreds of thousands
572
# of lines, so it has had a lot of inlining and optimizing done.
573
# Sorry that it is a little bit messy.
574
# Move several functions to be local variables, since this is a long
576
search = self._file_ids_altered_regex.search
577
unescape = _unescape_xml
578
setdefault = result.setdefault
579
pb = ui.ui_factory.nested_progress_bar()
581
for line in w.iter_lines_added_or_present_in_versions(
582
selected_revision_ids, pb=pb):
586
# One call to match.group() returning multiple items is quite a
587
# bit faster than 2 calls to match.group() each returning 1
588
file_id, revision_id = match.group('file_id', 'revision_id')
590
# Inlining the cache lookups helps a lot when you make 170,000
591
# lines and 350k ids, versus 8.4 unique ids.
592
# Using a cache helps in 2 ways:
593
# 1) Avoids unnecessary decoding calls
594
# 2) Re-uses cached strings, which helps in future set and
596
# (2) is enough that removing encoding entirely along with
597
# the cache (so we are using plain strings) results in no
598
# performance improvement.
600
revision_id = unescape_revid_cache[revision_id]
602
unescaped = unescape(revision_id)
603
unescape_revid_cache[revision_id] = unescaped
604
revision_id = unescaped
606
if revision_id in selected_revision_ids:
608
file_id = unescape_fileid_cache[file_id]
610
unescaped = unescape(file_id)
611
unescape_fileid_cache[file_id] = unescaped
613
setdefault(file_id, set()).add(revision_id)
413
for line in w.iter_lines_added_or_present_in_versions(selected_revision_ids):
414
start = line.find('file_id="')+9
415
if start < 9: continue
416
end = line.find('"', start)
418
file_id = _unescape_xml(line[start:end])
420
start = line.find('revision="')+10
421
if start < 10: continue
422
end = line.find('"', start)
424
revision_id = _unescape_xml(line[start:end])
425
if revision_id in selected_revision_ids:
426
result.setdefault(file_id, set()).add(revision_id)
921
def _warn_if_deprecated(self):
922
global _deprecation_warning_done
923
if _deprecation_warning_done:
925
_deprecation_warning_done = True
926
warning("Format %s for %s is deprecated - please use 'bzr upgrade' to get better performance"
927
% (self._format, self.bzrdir.transport.base))
929
def supports_rich_root(self):
930
return self._format.rich_root_data
932
def _check_ascii_revisionid(self, revision_id, method):
933
"""Private helper for ascii-only repositories."""
934
# weave repositories refuse to store revisionids that are non-ascii.
935
if revision_id is not None:
936
# weaves require ascii revision ids.
937
if isinstance(revision_id, unicode):
939
revision_id.encode('ascii')
940
except UnicodeEncodeError:
941
raise errors.NonAsciiRevisionId(method, self)
944
revision_id.decode('ascii')
945
except UnicodeDecodeError:
946
raise errors.NonAsciiRevisionId(method, self)
950
# remove these delegates a while after bzr 0.15
951
def __make_delegated(name, from_module):
952
def _deprecated_repository_forwarder():
953
symbol_versioning.warn('%s moved to %s in bzr 0.15'
954
% (name, from_module),
957
m = __import__(from_module, globals(), locals(), [name])
959
return getattr(m, name)
960
except AttributeError:
961
raise AttributeError('module %s has no name %s'
963
globals()[name] = _deprecated_repository_forwarder
966
'AllInOneRepository',
967
'WeaveMetaDirRepository',
968
'PreSplitOutRepositoryFormat',
974
__make_delegated(_name, 'bzrlib.repofmt.weaverepo')
978
'RepositoryFormatKnit',
979
'RepositoryFormatKnit1',
981
__make_delegated(_name, 'bzrlib.repofmt.knitrepo')
684
class AllInOneRepository(Repository):
685
"""Legacy support - the repository behaviour for all-in-one branches."""
687
def __init__(self, _format, a_bzrdir, _revision_store, control_store, text_store):
688
# we reuse one control files instance.
689
dir_mode = a_bzrdir._control_files._dir_mode
690
file_mode = a_bzrdir._control_files._file_mode
692
def get_store(name, compressed=True, prefixed=False):
693
# FIXME: This approach of assuming stores are all entirely compressed
694
# or entirely uncompressed is tidy, but breaks upgrade from
695
# some existing branches where there's a mixture; we probably
696
# still want the option to look for both.
697
relpath = a_bzrdir._control_files._escape(name)
698
store = TextStore(a_bzrdir._control_files._transport.clone(relpath),
699
prefixed=prefixed, compressed=compressed,
702
#if self._transport.should_cache():
703
# cache_path = os.path.join(self.cache_root, name)
704
# os.mkdir(cache_path)
705
# store = bzrlib.store.CachedStore(store, cache_path)
708
# not broken out yet because the controlweaves|inventory_store
709
# and text_store | weave_store bits are still different.
710
if isinstance(_format, RepositoryFormat4):
711
# cannot remove these - there is still no consistent api
712
# which allows access to this old info.
713
self.inventory_store = get_store('inventory-store')
714
text_store = get_store('text-store')
715
super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, _revision_store, control_store, text_store)
719
"""AllInOne repositories cannot be shared."""
723
def set_make_working_trees(self, new_value):
724
"""Set the policy flag for making working trees when creating branches.
726
This only applies to branches that use this repository.
728
The default is 'True'.
729
:param new_value: True to restore the default, False to disable making
732
raise NotImplementedError(self.set_make_working_trees)
734
def make_working_trees(self):
735
"""Returns the policy for making working trees on new branches."""
984
739
def install_revision(repository, rev, revision_tree):
1070
824
return not self.control_files._transport.has('no-working-trees')
1073
class RepositoryFormatRegistry(registry.Registry):
1074
"""Registry of RepositoryFormats.
1077
def get(self, format_string):
1078
r = registry.Registry.get(self, format_string)
827
class KnitRepository(MetaDirRepository):
828
"""Knit format repository."""
830
def _inventory_add_lines(self, inv_vf, revid, parents, lines):
831
inv_vf.add_lines_with_ghosts(revid, parents, lines)
834
def _all_revision_ids(self):
835
"""See Repository.all_revision_ids()."""
836
# Knits get the revision graph from the index of the revision knit, so
837
# it's always possible even if they're on an unlistable transport.
838
return self._revision_store.all_revision_ids(self.get_transaction())
840
def fileid_involved_between_revs(self, from_revid, to_revid):
841
"""Find file_id(s) which are involved in the changes between revisions.
843
This determines the set of revisions which are involved, and then
844
finds all file ids affected by those revisions.
846
vf = self._get_revision_vf()
847
from_set = set(vf.get_ancestry(from_revid))
848
to_set = set(vf.get_ancestry(to_revid))
849
changed = to_set.difference(from_set)
850
return self._fileid_involved_by_set(changed)
852
def fileid_involved(self, last_revid=None):
853
"""Find all file_ids modified in the ancestry of last_revid.
855
:param last_revid: If None, last_revision() will be used.
858
changed = set(self.all_revision_ids())
860
changed = set(self.get_ancestry(last_revid))
863
return self._fileid_involved_by_set(changed)
866
def get_ancestry(self, revision_id):
867
"""Return a list of revision-ids integrated by a revision.
869
This is topologically sorted.
871
if revision_id is None:
873
vf = self._get_revision_vf()
875
return [None] + vf.get_ancestry(revision_id)
876
except errors.RevisionNotPresent:
877
raise errors.NoSuchRevision(self, revision_id)
880
def get_revision(self, revision_id):
881
"""Return the Revision object for a named revision"""
882
return self.get_revision_reconcile(revision_id)
885
def get_revision_graph(self, revision_id=None):
886
"""Return a dictionary containing the revision graph.
888
:param revision_id: The revision_id to get a graph from. If None, then
889
the entire revision graph is returned. This is a deprecated mode of
890
operation and will be removed in the future.
891
:return: a dictionary of revision_id->revision_parents_list.
893
# special case NULL_REVISION
894
if revision_id == NULL_REVISION:
896
weave = self._get_revision_vf()
897
entire_graph = weave.get_graph()
898
if revision_id is None:
899
return weave.get_graph()
900
elif revision_id not in weave:
901
raise errors.NoSuchRevision(self, revision_id)
903
# add what can be reached from revision_id
905
pending = set([revision_id])
906
while len(pending) > 0:
908
result[node] = weave.get_parents(node)
909
for revision_id in result[node]:
910
if revision_id not in result:
911
pending.add(revision_id)
915
def get_revision_graph_with_ghosts(self, revision_ids=None):
916
"""Return a graph of the revisions with ghosts marked as applicable.
918
:param revision_ids: an iterable of revisions to graph or None for all.
919
:return: a Graph object with the graph reachable from revision_ids.
922
vf = self._get_revision_vf()
923
versions = set(vf.versions())
925
pending = set(self.all_revision_ids())
928
pending = set(revision_ids)
929
# special case NULL_REVISION
930
if NULL_REVISION in pending:
931
pending.remove(NULL_REVISION)
932
required = set(pending)
935
revision_id = pending.pop()
936
if not revision_id in versions:
937
if revision_id in required:
938
raise errors.NoSuchRevision(self, revision_id)
940
result.add_ghost(revision_id)
941
# mark it as done so we don't try for it again.
942
done.add(revision_id)
944
parent_ids = vf.get_parents_with_ghosts(revision_id)
945
for parent_id in parent_ids:
946
# is this queued or done ?
947
if (parent_id not in pending and
948
parent_id not in done):
950
pending.add(parent_id)
951
result.add_node(revision_id, parent_ids)
952
done.add(revision_id)
955
def _get_revision_vf(self):
956
""":return: a versioned file containing the revisions."""
957
vf = self._revision_store.get_revision_file(self.get_transaction())
961
def reconcile(self, other=None, thorough=False):
962
"""Reconcile this repository."""
963
from bzrlib.reconcile import KnitReconciler
964
reconciler = KnitReconciler(self, thorough=thorough)
965
reconciler.reconcile()
1084
format_registry = RepositoryFormatRegistry()
1085
"""Registry of formats, indexed by their identifying format string.
1087
This can contain either format instances themselves, or classes/factories that
1088
can be called to obtain one.
1092
#####################################################################
1093
# Repository Formats
968
def revision_parents(self, revision_id):
969
return self._get_revision_vf().get_parents(revision_id)
1095
972
class RepositoryFormat(object):
1096
973
"""A repository format.
1116
993
parameterisation.
1120
return "<%s>" % self.__class__.__name__
1122
def __eq__(self, other):
1123
# format objects are generally stateless
1124
return isinstance(other, self.__class__)
1126
def __ne__(self, other):
1127
return not self == other
996
_default_format = None
997
"""The default format used for new repositories."""
1000
"""The known formats."""
1130
1003
def find_format(klass, a_bzrdir):
1131
"""Return the format for the repository object in a_bzrdir.
1133
This is used by bzr native formats that have a "format" file in
1134
the repository. Other methods may be used by different types of
1004
"""Return the format for the repository object in a_bzrdir."""
1138
1006
transport = a_bzrdir.get_repository_transport(None)
1139
1007
format_string = transport.get("format").read()
1140
return format_registry.get(format_string)
1008
return klass._formats[format_string]
1141
1009
except errors.NoSuchFile:
1142
1010
raise errors.NoRepositoryPresent(a_bzrdir)
1143
1011
except KeyError:
1144
1012
raise errors.UnknownFormatError(format=format_string)
1147
def register_format(klass, format):
1148
format_registry.register(format.get_format_string(), format)
1151
def unregister_format(klass, format):
1152
format_registry.remove(format.get_format_string())
1014
def _get_control_store(self, repo_transport, control_files):
1015
"""Return the control store for this repository."""
1016
raise NotImplementedError(self._get_control_store)
1155
1019
def get_default_format(klass):
1156
1020
"""Return the current default format."""
1157
from bzrlib import bzrdir
1158
return bzrdir.format_registry.make_bzrdir('default').repository_format
1160
def _get_control_store(self, repo_transport, control_files):
1161
"""Return the control store for this repository."""
1162
raise NotImplementedError(self._get_control_store)
1021
return klass._default_format
1164
1023
def get_format_string(self):
1165
1024
"""Return the ASCII format string that identifies this format.
1254
1102
raise NotImplementedError(self.open)
1105
def register_format(klass, format):
1106
klass._formats[format.get_format_string()] = format
1109
def set_default_format(klass, format):
1110
klass._default_format = format
1113
def unregister_format(klass, format):
1114
assert klass._formats[format.get_format_string()] is format
1115
del klass._formats[format.get_format_string()]
1118
class PreSplitOutRepositoryFormat(RepositoryFormat):
1119
"""Base class for the pre split out repository formats."""
1121
def initialize(self, a_bzrdir, shared=False, _internal=False):
1122
"""Create a weave repository.
1124
TODO: when creating split out bzr branch formats, move this to a common
1125
base for Format5, Format6. or something like that.
1127
from bzrlib.weavefile import write_weave_v5
1128
from bzrlib.weave import Weave
1131
raise errors.IncompatibleFormat(self, a_bzrdir._format)
1134
# always initialized when the bzrdir is.
1135
return self.open(a_bzrdir, _found=True)
1137
# Create an empty weave
1139
write_weave_v5(Weave(), sio)
1140
empty_weave = sio.getvalue()
1142
mutter('creating repository in %s.', a_bzrdir.transport.base)
1143
dirs = ['revision-store', 'weaves']
1144
files = [('inventory.weave', StringIO(empty_weave)),
1147
# FIXME: RBC 20060125 don't peek under the covers
1148
# NB: no need to escape relative paths that are url safe.
1149
control_files = LockableFiles(a_bzrdir.transport, 'branch-lock',
1151
control_files.create_lock()
1152
control_files.lock_write()
1153
control_files._transport.mkdir_multi(dirs,
1154
mode=control_files._dir_mode)
1156
for file, content in files:
1157
control_files.put(file, content)
1159
control_files.unlock()
1160
return self.open(a_bzrdir, _found=True)
1162
def _get_control_store(self, repo_transport, control_files):
1163
"""Return the control store for this repository."""
1164
return self._get_versioned_file_store('',
1169
def _get_text_store(self, transport, control_files):
1170
"""Get a store for file texts for this format."""
1171
raise NotImplementedError(self._get_text_store)
1173
def open(self, a_bzrdir, _found=False):
1174
"""See RepositoryFormat.open()."""
1176
# we are being called directly and must probe.
1177
raise NotImplementedError
1179
repo_transport = a_bzrdir.get_repository_transport(None)
1180
control_files = a_bzrdir._control_files
1181
text_store = self._get_text_store(repo_transport, control_files)
1182
control_store = self._get_control_store(repo_transport, control_files)
1183
_revision_store = self._get_revision_store(repo_transport, control_files)
1184
return AllInOneRepository(_format=self,
1186
_revision_store=_revision_store,
1187
control_store=control_store,
1188
text_store=text_store)
1191
class RepositoryFormat4(PreSplitOutRepositoryFormat):
1192
"""Bzr repository format 4.
1194
This repository format has:
1196
- TextStores for texts, inventories,revisions.
1198
This format is deprecated: it indexes texts using a text id which is
1199
removed in format 5; initialization and write support for this format
1204
super(RepositoryFormat4, self).__init__()
1205
self._matchingbzrdir = bzrdir.BzrDirFormat4()
1207
def get_format_description(self):
1208
"""See RepositoryFormat.get_format_description()."""
1209
return "Repository format 4"
1211
def initialize(self, url, shared=False, _internal=False):
1212
"""Format 4 branches cannot be created."""
1213
raise errors.UninitializableFormat(self)
1215
def is_supported(self):
1216
"""Format 4 is not supported.
1218
It is not supported because the model changed from 4 to 5 and the
1219
conversion logic is expensive - so doing it on the fly was not
1224
def _get_control_store(self, repo_transport, control_files):
1225
"""Format 4 repositories have no formal control store at this point.
1227
This will cause any control-file-needing apis to fail - this is desired.
1231
def _get_revision_store(self, repo_transport, control_files):
1232
"""See RepositoryFormat._get_revision_store()."""
1233
from bzrlib.xml4 import serializer_v4
1234
return self._get_text_rev_store(repo_transport,
1237
serializer=serializer_v4)
1239
def _get_text_store(self, transport, control_files):
1240
"""See RepositoryFormat._get_text_store()."""
1243
class RepositoryFormat5(PreSplitOutRepositoryFormat):
1244
"""Bzr control format 5.
1246
This repository format has:
1247
- weaves for file texts and inventory
1249
- TextStores for revisions and signatures.
1253
super(RepositoryFormat5, self).__init__()
1254
self._matchingbzrdir = bzrdir.BzrDirFormat5()
1256
def get_format_description(self):
1257
"""See RepositoryFormat.get_format_description()."""
1258
return "Weave repository format 5"
1260
def _get_revision_store(self, repo_transport, control_files):
1261
"""See RepositoryFormat._get_revision_store()."""
1262
"""Return the revision store object for this a_bzrdir."""
1263
return self._get_text_rev_store(repo_transport,
1268
def _get_text_store(self, transport, control_files):
1269
"""See RepositoryFormat._get_text_store()."""
1270
return self._get_versioned_file_store('weaves', transport, control_files, prefixed=False)
1273
class RepositoryFormat6(PreSplitOutRepositoryFormat):
1274
"""Bzr control format 6.
1276
This repository format has:
1277
- weaves for file texts and inventory
1278
- hash subdirectory based stores.
1279
- TextStores for revisions and signatures.
1283
super(RepositoryFormat6, self).__init__()
1284
self._matchingbzrdir = bzrdir.BzrDirFormat6()
1286
def get_format_description(self):
1287
"""See RepositoryFormat.get_format_description()."""
1288
return "Weave repository format 6"
1290
def _get_revision_store(self, repo_transport, control_files):
1291
"""See RepositoryFormat._get_revision_store()."""
1292
return self._get_text_rev_store(repo_transport,
1298
def _get_text_store(self, transport, control_files):
1299
"""See RepositoryFormat._get_text_store()."""
1300
return self._get_versioned_file_store('weaves', transport, control_files)
1257
1303
class MetaDirRepositoryFormat(RepositoryFormat):
1258
1304
"""Common base class for the new repositories using the metadir layout."""
1260
rich_root_data = False
1261
supports_tree_reference = False
1262
_matchingbzrdir = bzrdir.BzrDirMetaFormat1()
1264
1306
def __init__(self):
1265
1307
super(MetaDirRepositoryFormat, self).__init__()
1308
self._matchingbzrdir = bzrdir.BzrDirMetaFormat1()
1267
1310
def _create_control_files(self, a_bzrdir):
1268
1311
"""Create the required files and the initial control_files object."""
1269
1312
# FIXME: RBC 20060125 don't peek under the covers
1270
1313
# NB: no need to escape relative paths that are url safe.
1271
1314
repository_transport = a_bzrdir.get_repository_transport(self)
1272
control_files = lockable_files.LockableFiles(repository_transport,
1273
'lock', lockdir.LockDir)
1315
control_files = LockableFiles(repository_transport, 'lock', LockDir)
1274
1316
control_files.create_lock()
1275
1317
return control_files
1291
1333
control_files.unlock()
1336
class RepositoryFormat7(MetaDirRepositoryFormat):
1337
"""Bzr repository 7.
1339
This repository format has:
1340
- weaves for file texts and inventory
1341
- hash subdirectory based stores.
1342
- TextStores for revisions and signatures.
1343
- a format marker of its own
1344
- an optional 'shared-storage' flag
1345
- an optional 'no-working-trees' flag
1348
def _get_control_store(self, repo_transport, control_files):
1349
"""Return the control store for this repository."""
1350
return self._get_versioned_file_store('',
1355
def get_format_string(self):
1356
"""See RepositoryFormat.get_format_string()."""
1357
return "Bazaar-NG Repository format 7"
1359
def get_format_description(self):
1360
"""See RepositoryFormat.get_format_description()."""
1361
return "Weave repository format 7"
1363
def _get_revision_store(self, repo_transport, control_files):
1364
"""See RepositoryFormat._get_revision_store()."""
1365
return self._get_text_rev_store(repo_transport,
1372
def _get_text_store(self, transport, control_files):
1373
"""See RepositoryFormat._get_text_store()."""
1374
return self._get_versioned_file_store('weaves',
1378
def initialize(self, a_bzrdir, shared=False):
1379
"""Create a weave repository.
1381
:param shared: If true the repository will be initialized as a shared
1384
from bzrlib.weavefile import write_weave_v5
1385
from bzrlib.weave import Weave
1387
# Create an empty weave
1389
write_weave_v5(Weave(), sio)
1390
empty_weave = sio.getvalue()
1392
mutter('creating repository in %s.', a_bzrdir.transport.base)
1393
dirs = ['revision-store', 'weaves']
1394
files = [('inventory.weave', StringIO(empty_weave)),
1396
utf8_files = [('format', self.get_format_string())]
1398
self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
1399
return self.open(a_bzrdir=a_bzrdir, _found=True)
1401
def open(self, a_bzrdir, _found=False, _override_transport=None):
1402
"""See RepositoryFormat.open().
1404
:param _override_transport: INTERNAL USE ONLY. Allows opening the
1405
repository at a slightly different url
1406
than normal. I.e. during 'upgrade'.
1409
format = RepositoryFormat.find_format(a_bzrdir)
1410
assert format.__class__ == self.__class__
1411
if _override_transport is not None:
1412
repo_transport = _override_transport
1414
repo_transport = a_bzrdir.get_repository_transport(None)
1415
control_files = LockableFiles(repo_transport, 'lock', LockDir)
1416
text_store = self._get_text_store(repo_transport, control_files)
1417
control_store = self._get_control_store(repo_transport, control_files)
1418
_revision_store = self._get_revision_store(repo_transport, control_files)
1419
return MetaDirRepository(_format=self,
1421
control_files=control_files,
1422
_revision_store=_revision_store,
1423
control_store=control_store,
1424
text_store=text_store)
1427
class RepositoryFormatKnit1(MetaDirRepositoryFormat):
1428
"""Bzr repository knit format 1.
1430
This repository format has:
1431
- knits for file texts and inventory
1432
- hash subdirectory based stores.
1433
- knits for revisions and signatures
1434
- TextStores for revisions and signatures.
1435
- a format marker of its own
1436
- an optional 'shared-storage' flag
1437
- an optional 'no-working-trees' flag
1440
This format was introduced in bzr 0.8.
1443
def _get_control_store(self, repo_transport, control_files):
1444
"""Return the control store for this repository."""
1445
return VersionedFileStore(
1448
file_mode=control_files._file_mode,
1449
versionedfile_class=KnitVersionedFile,
1450
versionedfile_kwargs={'factory':KnitPlainFactory()},
1453
def get_format_string(self):
1454
"""See RepositoryFormat.get_format_string()."""
1455
return "Bazaar-NG Knit Repository Format 1"
1457
def get_format_description(self):
1458
"""See RepositoryFormat.get_format_description()."""
1459
return "Knit repository format 1"
1461
def _get_revision_store(self, repo_transport, control_files):
1462
"""See RepositoryFormat._get_revision_store()."""
1463
from bzrlib.store.revision.knit import KnitRevisionStore
1464
versioned_file_store = VersionedFileStore(
1466
file_mode=control_files._file_mode,
1469
versionedfile_class=KnitVersionedFile,
1470
versionedfile_kwargs={'delta':False, 'factory':KnitPlainFactory()},
1473
return KnitRevisionStore(versioned_file_store)
1475
def _get_text_store(self, transport, control_files):
1476
"""See RepositoryFormat._get_text_store()."""
1477
return self._get_versioned_file_store('knits',
1480
versionedfile_class=KnitVersionedFile,
1483
def initialize(self, a_bzrdir, shared=False):
1484
"""Create a knit format 1 repository.
1486
:param a_bzrdir: bzrdir to contain the new repository; must already
1488
:param shared: If true the repository will be initialized as a shared
1491
mutter('creating repository in %s.', a_bzrdir.transport.base)
1492
dirs = ['revision-store', 'knits']
1494
utf8_files = [('format', self.get_format_string())]
1496
self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
1497
repo_transport = a_bzrdir.get_repository_transport(None)
1498
control_files = LockableFiles(repo_transport, 'lock', LockDir)
1499
control_store = self._get_control_store(repo_transport, control_files)
1500
transaction = transactions.WriteTransaction()
1501
# trigger a write of the inventory store.
1502
control_store.get_weave_or_empty('inventory', transaction)
1503
_revision_store = self._get_revision_store(repo_transport, control_files)
1504
_revision_store.has_revision_id('A', transaction)
1505
_revision_store.get_signature_file(transaction)
1506
return self.open(a_bzrdir=a_bzrdir, _found=True)
1508
def open(self, a_bzrdir, _found=False, _override_transport=None):
1509
"""See RepositoryFormat.open().
1511
:param _override_transport: INTERNAL USE ONLY. Allows opening the
1512
repository at a slightly different url
1513
than normal. I.e. during 'upgrade'.
1516
format = RepositoryFormat.find_format(a_bzrdir)
1517
assert format.__class__ == self.__class__
1518
if _override_transport is not None:
1519
repo_transport = _override_transport
1521
repo_transport = a_bzrdir.get_repository_transport(None)
1522
control_files = LockableFiles(repo_transport, 'lock', LockDir)
1523
text_store = self._get_text_store(repo_transport, control_files)
1524
control_store = self._get_control_store(repo_transport, control_files)
1525
_revision_store = self._get_revision_store(repo_transport, control_files)
1526
return KnitRepository(_format=self,
1528
control_files=control_files,
1529
_revision_store=_revision_store,
1530
control_store=control_store,
1531
text_store=text_store)
1294
1534
# formats which have no format string are not discoverable
1295
# and not independently creatable, so are not registered. They're
1296
# all in bzrlib.repofmt.weaverepo now. When an instance of one of these is
1297
# needed, it's constructed directly by the BzrDir. Non-native formats where
1298
# the repository is not separately opened are similar.
1300
format_registry.register_lazy(
1301
'Bazaar-NG Repository format 7',
1302
'bzrlib.repofmt.weaverepo',
1305
# KEEP in sync with bzrdir.format_registry default, which controls the overall
1306
# default control directory format
1308
format_registry.register_lazy(
1309
'Bazaar-NG Knit Repository Format 1',
1310
'bzrlib.repofmt.knitrepo',
1311
'RepositoryFormatKnit1',
1313
format_registry.default_key = 'Bazaar-NG Knit Repository Format 1'
1315
format_registry.register_lazy(
1316
'Bazaar Knit Repository Format 3 (bzr 0.15)\n',
1317
'bzrlib.repofmt.knitrepo',
1318
'RepositoryFormatKnit3',
1535
# and not independently creatable, so are not registered.
1536
RepositoryFormat.register_format(RepositoryFormat7())
1537
_default_format = RepositoryFormatKnit1()
1538
RepositoryFormat.register_format(_default_format)
1539
RepositoryFormat.set_default_format(_default_format)
1540
_legacy_formats = [RepositoryFormat4(),
1541
RepositoryFormat5(),
1542
RepositoryFormat6()]
1322
1545
class InterRepository(InterObject):
1379
1628
return [rev_id for rev_id in source_ids if rev_id in result_set]
1382
class InterSameDataRepository(InterRepository):
1383
"""Code for converting between repositories that represent the same data.
1385
Data format and model must match for this to work.
1389
def _get_repo_format_to_test(self):
1390
"""Repository format for testing with."""
1391
return RepositoryFormat.get_default_format()
1394
def is_compatible(source, target):
1395
if source.supports_rich_root() != target.supports_rich_root():
1397
if source._serializer != target._serializer:
1402
def copy_content(self, revision_id=None):
1403
"""Make a complete copy of the content in self into destination.
1405
This copies both the repository's revision data, and configuration information
1406
such as the make_working_trees setting.
1408
This is a destructive operation! Do not use it on existing
1411
:param revision_id: Only copy the content needed to construct
1412
revision_id and its parents.
1415
self.target.set_make_working_trees(self.source.make_working_trees())
1416
except NotImplementedError:
1418
# TODO: jam 20070210 This is fairly internal, so we should probably
1419
# just assert that revision_id is not unicode.
1420
revision_id = osutils.safe_revision_id(revision_id)
1421
# but don't bother fetching if we have the needed data now.
1422
if (revision_id not in (None, _mod_revision.NULL_REVISION) and
1423
self.target.has_revision(revision_id)):
1425
self.target.fetch(self.source, revision_id=revision_id)
1428
def fetch(self, revision_id=None, pb=None):
1429
"""See InterRepository.fetch()."""
1430
from bzrlib.fetch import GenericRepoFetcher
1431
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
1432
self.source, self.source._format, self.target,
1433
self.target._format)
1434
# TODO: jam 20070210 This should be an assert, not a translate
1435
revision_id = osutils.safe_revision_id(revision_id)
1436
f = GenericRepoFetcher(to_repository=self.target,
1437
from_repository=self.source,
1438
last_revision=revision_id,
1440
return f.count_copied, f.failed_revisions
1443
class InterWeaveRepo(InterSameDataRepository):
1631
class InterWeaveRepo(InterRepository):
1444
1632
"""Optimised code paths between Weave based repositories."""
1447
def _get_repo_format_to_test(self):
1448
from bzrlib.repofmt import weaverepo
1449
return weaverepo.RepositoryFormat7()
1634
_matching_repo_format = RepositoryFormat7()
1635
"""Repository format for testing with."""
1452
1638
def is_compatible(source, target):
1474
1655
@needs_write_lock
1475
def copy_content(self, revision_id=None):
1656
def copy_content(self, revision_id=None, basis=None):
1476
1657
"""See InterRepository.copy_content()."""
1477
1658
# weave specific optimised path:
1478
# TODO: jam 20070210 Internal, should be an assert, not translate
1479
revision_id = osutils.safe_revision_id(revision_id)
1481
self.target.set_make_working_trees(self.source.make_working_trees())
1482
except NotImplementedError:
1484
# FIXME do not peek!
1485
if self.source.control_files._transport.listable():
1486
pb = ui.ui_factory.nested_progress_bar()
1659
if basis is not None:
1660
# copy the basis in, then fetch remaining data.
1661
basis.copy_content_into(self.target, revision_id)
1662
# the basis copy_content_into could miss-set this.
1488
self.target.weave_store.copy_all_ids(
1489
self.source.weave_store,
1491
from_transaction=self.source.get_transaction(),
1492
to_transaction=self.target.get_transaction())
1493
pb.update('copying inventory', 0, 1)
1494
self.target.control_weaves.copy_multi(
1495
self.source.control_weaves, ['inventory'],
1496
from_transaction=self.source.get_transaction(),
1497
to_transaction=self.target.get_transaction())
1498
self.target._revision_store.text_store.copy_all_ids(
1499
self.source._revision_store.text_store,
1664
self.target.set_make_working_trees(self.source.make_working_trees())
1665
except NotImplementedError:
1504
1667
self.target.fetch(self.source, revision_id=revision_id)
1670
self.target.set_make_working_trees(self.source.make_working_trees())
1671
except NotImplementedError:
1673
# FIXME do not peek!
1674
if self.source.control_files._transport.listable():
1675
pb = ui.ui_factory.nested_progress_bar()
1677
self.target.weave_store.copy_all_ids(
1678
self.source.weave_store,
1680
from_transaction=self.source.get_transaction(),
1681
to_transaction=self.target.get_transaction())
1682
pb.update('copying inventory', 0, 1)
1683
self.target.control_weaves.copy_multi(
1684
self.source.control_weaves, ['inventory'],
1685
from_transaction=self.source.get_transaction(),
1686
to_transaction=self.target.get_transaction())
1687
self.target._revision_store.text_store.copy_all_ids(
1688
self.source._revision_store.text_store,
1693
self.target.fetch(self.source, revision_id=revision_id)
1506
1695
@needs_write_lock
1507
1696
def fetch(self, revision_id=None, pb=None):
1626
1808
# that against the revision records.
1627
1809
return self.source._eliminate_revisions_not_present(required_topo_revisions)
1630
class InterModel1and2(InterRepository):
1633
def _get_repo_format_to_test(self):
1637
def is_compatible(source, target):
1638
if not source.supports_rich_root() and target.supports_rich_root():
1644
def fetch(self, revision_id=None, pb=None):
1645
"""See InterRepository.fetch()."""
1646
from bzrlib.fetch import Model1toKnit2Fetcher
1647
# TODO: jam 20070210 This should be an assert, not a translate
1648
revision_id = osutils.safe_revision_id(revision_id)
1649
f = Model1toKnit2Fetcher(to_repository=self.target,
1650
from_repository=self.source,
1651
last_revision=revision_id,
1653
return f.count_copied, f.failed_revisions
1656
def copy_content(self, revision_id=None):
1657
"""Make a complete copy of the content in self into destination.
1659
This is a destructive operation! Do not use it on existing
1662
:param revision_id: Only copy the content needed to construct
1663
revision_id and its parents.
1666
self.target.set_make_working_trees(self.source.make_working_trees())
1667
except NotImplementedError:
1669
# TODO: jam 20070210 Internal, assert, don't translate
1670
revision_id = osutils.safe_revision_id(revision_id)
1671
# but don't bother fetching if we have the needed data now.
1672
if (revision_id not in (None, _mod_revision.NULL_REVISION) and
1673
self.target.has_revision(revision_id)):
1675
self.target.fetch(self.source, revision_id=revision_id)
1678
class InterKnit1and2(InterKnitRepo):
1681
def _get_repo_format_to_test(self):
1685
def is_compatible(source, target):
1686
"""Be compatible with Knit1 source and Knit3 target"""
1687
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit3
1689
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1, \
1690
RepositoryFormatKnit3
1691
return (isinstance(source._format, (RepositoryFormatKnit1)) and
1692
isinstance(target._format, (RepositoryFormatKnit3)))
1693
except AttributeError:
1697
def fetch(self, revision_id=None, pb=None):
1698
"""See InterRepository.fetch()."""
1699
from bzrlib.fetch import Knit1to2Fetcher
1700
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
1701
self.source, self.source._format, self.target,
1702
self.target._format)
1703
# TODO: jam 20070210 This should be an assert, not a translate
1704
revision_id = osutils.safe_revision_id(revision_id)
1705
f = Knit1to2Fetcher(to_repository=self.target,
1706
from_repository=self.source,
1707
last_revision=revision_id,
1709
return f.count_copied, f.failed_revisions
1712
class InterRemoteRepository(InterRepository):
1713
"""Code for converting between RemoteRepository objects.
1715
This just gets an non-remote repository from the RemoteRepository, and calls
1716
InterRepository.get again.
1719
def __init__(self, source, target):
1720
if isinstance(source, remote.RemoteRepository):
1721
source._ensure_real()
1722
real_source = source._real_repository
1724
real_source = source
1725
if isinstance(target, remote.RemoteRepository):
1726
target._ensure_real()
1727
real_target = target._real_repository
1729
real_target = target
1730
self.real_inter = InterRepository.get(real_source, real_target)
1733
def is_compatible(source, target):
1734
if isinstance(source, remote.RemoteRepository):
1736
if isinstance(target, remote.RemoteRepository):
1740
def copy_content(self, revision_id=None):
1741
self.real_inter.copy_content(revision_id=revision_id)
1743
def fetch(self, revision_id=None, pb=None):
1744
self.real_inter.fetch(revision_id=revision_id, pb=pb)
1747
def _get_repo_format_to_test(self):
1751
InterRepository.register_optimiser(InterSameDataRepository)
1752
1811
InterRepository.register_optimiser(InterWeaveRepo)
1753
1812
InterRepository.register_optimiser(InterKnitRepo)
1754
InterRepository.register_optimiser(InterModel1and2)
1755
InterRepository.register_optimiser(InterKnit1and2)
1756
InterRepository.register_optimiser(InterRemoteRepository)
1759
1815
class RepositoryTestProviderAdapter(object):