151
154
format.get_format_description(),
154
def clone(self, url, revision_id=None, force_new_repo=False,
155
preserve_stacking=False):
157
def clone(self, url, revision_id=None, force_new_repo=False):
156
158
"""Clone this bzrdir and its contents to url verbatim.
158
:param url: The url create the clone at. If url's last component does
159
not exist, it will be created.
160
:param revision_id: The tip revision-id to use for any branch or
161
working tree. If not None, then the clone operation may tune
160
If url's last component does not exist, it will be created.
162
if revision_id is not None, then the clone operation may tune
162
163
itself to download less data.
163
:param force_new_repo: Do not use a shared repository for the target
164
:param force_new_repo: Do not use a shared repository for the target
164
165
even if one is available.
165
:param preserve_stacking: When cloning a stacked branch, stack the
166
new branch on top of the other branch's stacked-on branch.
168
167
return self.clone_on_transport(get_transport(url),
169
168
revision_id=revision_id,
170
force_new_repo=force_new_repo,
171
preserve_stacking=preserve_stacking)
169
force_new_repo=force_new_repo)
173
171
def clone_on_transport(self, transport, revision_id=None,
174
force_new_repo=False, preserve_stacking=False,
172
force_new_repo=False):
176
173
"""Clone this bzrdir and its contents to transport verbatim.
178
:param transport: The transport for the location to produce the clone
179
at. If the target directory does not exist, it will be created.
180
:param revision_id: The tip revision-id to use for any branch or
181
working tree. If not None, then the clone operation may tune
175
If the target directory does not exist, it will be created.
177
if revision_id is not None, then the clone operation may tune
182
178
itself to download less data.
183
:param force_new_repo: Do not use a shared repository for the target,
179
:param force_new_repo: Do not use a shared repository for the target
184
180
even if one is available.
185
:param preserve_stacking: When cloning a stacked branch, stack the
186
new branch on top of the other branch's stacked-on branch.
188
182
transport.ensure_base()
189
require_stacking = (stacked_on is not None)
190
metadir = self.cloning_metadir(require_stacking)
191
result = metadir.initialize_on_transport(transport)
192
repository_policy = None
183
result = self._format.initialize_on_transport(transport)
194
185
local_repo = self.find_repository()
195
186
except errors.NoRepositoryPresent:
196
187
local_repo = None
198
local_branch = self.open_branch()
199
except errors.NotBranchError:
202
# enable fallbacks when branch is not a branch reference
203
if local_branch.repository.has_same_location(local_repo):
204
local_repo = local_branch.repository
205
if preserve_stacking:
207
stacked_on = local_branch.get_stacked_on_url()
208
except (errors.UnstackableBranchFormat,
209
errors.UnstackableRepositoryFormat,
214
189
# may need to copy content in
215
repository_policy = result.determine_repository_policy(
216
force_new_repo, stacked_on, self.root_transport.base,
217
require_stacking=require_stacking)
218
make_working_trees = local_repo.make_working_trees()
219
result_repo = repository_policy.acquire_repository(
220
make_working_trees, local_repo.is_shared())
221
result_repo.fetch(local_repo, revision_id=revision_id)
191
result_repo = local_repo.clone(
193
revision_id=revision_id)
194
result_repo.set_make_working_trees(local_repo.make_working_trees())
197
result_repo = result.find_repository()
198
# fetch content this dir needs.
199
result_repo.fetch(local_repo, revision_id=revision_id)
200
except errors.NoRepositoryPresent:
201
# needed to make one anyway.
202
result_repo = local_repo.clone(
204
revision_id=revision_id)
205
result_repo.set_make_working_trees(local_repo.make_working_trees())
224
206
# 1 if there is a branch present
225
207
# make sure its content is available in the target repository
227
if local_branch is not None:
228
result_branch = local_branch.clone(result, revision_id=revision_id)
229
if repository_policy is not None:
230
repository_policy.configure_branch(result_branch)
210
self.open_branch().clone(result, revision_id=revision_id)
211
except errors.NotBranchError:
214
result_repo = result.find_repository()
215
except errors.NoRepositoryPresent:
231
217
if result_repo is None or result_repo.make_working_trees():
233
219
self.open_workingtree().clone(result)
369
356
bzrdir._find_or_create_repository(force_new_repo)
370
357
return bzrdir.create_branch()
372
def determine_repository_policy(self, force_new_repo=False, stack_on=None,
373
stack_on_pwd=None, require_stacking=False):
374
"""Return an object representing a policy to use.
376
This controls whether a new repository is created, or a shared
377
repository used instead.
379
If stack_on is supplied, will not seek a containing shared repo.
381
:param force_new_repo: If True, require a new repository to be created.
382
:param stack_on: If supplied, the location to stack on. If not
383
supplied, a default_stack_on location may be used.
384
:param stack_on_pwd: If stack_on is relative, the location it is
387
def repository_policy(found_bzrdir):
390
config = found_bzrdir.get_config()
392
if config is not None:
393
stack_on = config.get_default_stack_on()
394
if stack_on is not None:
395
stack_on_pwd = found_bzrdir.root_transport.base
397
note('Using default stacking branch %s at %s', stack_on,
399
# does it have a repository ?
401
repository = found_bzrdir.open_repository()
402
except errors.NoRepositoryPresent:
405
if ((found_bzrdir.root_transport.base !=
406
self.root_transport.base) and not repository.is_shared()):
413
return UseExistingRepository(repository, stack_on,
414
stack_on_pwd, require_stacking=require_stacking), True
416
return CreateRepository(self, stack_on, stack_on_pwd,
417
require_stacking=require_stacking), True
419
if not force_new_repo:
421
policy = self._find_containing(repository_policy)
422
if policy is not None:
426
return UseExistingRepository(self.open_repository(),
427
stack_on, stack_on_pwd,
428
require_stacking=require_stacking)
429
except errors.NoRepositoryPresent:
431
return CreateRepository(self, stack_on, stack_on_pwd,
432
require_stacking=require_stacking)
434
359
def _find_or_create_repository(self, force_new_repo):
435
360
"""Create a new repository if needed, returning the repository."""
436
policy = self.determine_repository_policy(force_new_repo)
437
return policy.acquire_repository()
362
return self.create_repository()
364
return self.find_repository()
365
except errors.NoRepositoryPresent:
366
return self.create_repository()
440
369
def create_branch_convenience(base, force_new_repo=False,
441
370
force_new_tree=None, format=None,
414
@deprecated_function(zero_ninetyone)
415
def create_repository(base, shared=False, format=None):
416
"""Create a new BzrDir and Repository at the url 'base'.
418
If no format is supplied, this will default to the current default
419
BzrDirFormat by default, and use whatever repository format that that
420
uses for bzrdirformat.create_repository.
422
:param shared: Create a shared repository rather than a standalone
424
The Repository object is returned.
426
This must be overridden as an instance method in child classes, where
427
it should take no parameters and construct whatever repository format
428
that child class desires.
430
This method is deprecated, please call create_repository on a bzrdir
433
bzrdir = BzrDir.create(base, format)
434
return bzrdir.create_repository(shared)
485
437
def create_standalone_workingtree(base, format=None):
486
438
"""Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
557
509
raise NotImplementedError(self.destroy_workingtree_metadata)
559
def _find_containing(self, evaluate):
560
"""Find something in a containing control directory.
562
This method will scan containing control dirs, until it finds what
563
it is looking for, decides that it will never find it, or runs out
564
of containing control directories to check.
566
It is used to implement find_repository and
567
determine_repository_policy.
569
:param evaluate: A function returning (value, stop). If stop is True,
570
the value will be returned.
574
result, stop = evaluate(found_bzrdir)
577
next_transport = found_bzrdir.root_transport.clone('..')
578
if (found_bzrdir.root_transport.base == next_transport.base):
579
# top of the file system
581
# find the next containing bzrdir
583
found_bzrdir = BzrDir.open_containing_from_transport(
585
except errors.NotBranchError:
588
511
def find_repository(self):
589
512
"""Find the repository that should be used.
592
515
new branches as well as to hook existing branches up to their
595
def usable_repository(found_bzrdir):
519
return self.open_repository()
520
except errors.NoRepositoryPresent:
522
next_transport = self.root_transport.clone('..')
524
# find the next containing bzrdir
526
found_bzrdir = BzrDir.open_containing_from_transport(
528
except errors.NotBranchError:
530
raise errors.NoRepositoryPresent(self)
596
531
# does it have a repository ?
598
533
repository = found_bzrdir.open_repository()
599
534
except errors.NoRepositoryPresent:
601
if found_bzrdir.root_transport.base == self.root_transport.base:
602
return repository, True
603
elif repository.is_shared():
604
return repository, True
535
next_transport = found_bzrdir.root_transport.clone('..')
536
if (found_bzrdir.root_transport.base == next_transport.base):
537
# top of the file system
541
if ((found_bzrdir.root_transport.base ==
542
self.root_transport.base) or repository.is_shared()):
608
found_repo = self._find_containing(usable_repository)
609
if found_repo is None:
610
raise errors.NoRepositoryPresent(self)
545
raise errors.NoRepositoryPresent(self)
546
raise errors.NoRepositoryPresent(self)
613
548
def get_branch_reference(self):
614
549
"""Return the referenced URL for the branch in this bzrdir.
631
566
guaranteed to point to an existing directory ready for use.
633
568
raise NotImplementedError(self.get_branch_transport)
635
def _find_creation_modes(self):
636
"""Determine the appropriate modes for files and directories.
638
They're always set to be consistent with the base directory,
639
assuming that this transport allows setting modes.
641
# TODO: Do we need or want an option (maybe a config setting) to turn
642
# this off or override it for particular locations? -- mbp 20080512
643
if self._mode_check_done:
645
self._mode_check_done = True
647
st = self.transport.stat('.')
648
except errors.TransportNotPossible:
649
self._dir_mode = None
650
self._file_mode = None
652
# Check the directory mode, but also make sure the created
653
# directories and files are read-write for this user. This is
654
# mostly a workaround for filesystems which lie about being able to
655
# write to a directory (cygwin & win32)
656
if (st.st_mode & 07777 == 00000):
657
# FTP allows stat but does not return dir/file modes
658
self._dir_mode = None
659
self._file_mode = None
661
self._dir_mode = (st.st_mode & 07777) | 00700
662
# Remove the sticky and execute bits for files
663
self._file_mode = self._dir_mode & ~07111
665
def _get_file_mode(self):
666
"""Return Unix mode for newly created files, or None.
668
if not self._mode_check_done:
669
self._find_creation_modes()
670
return self._file_mode
672
def _get_dir_mode(self):
673
"""Return Unix mode for newly created directories, or None.
675
if not self._mode_check_done:
676
self._find_creation_modes()
677
return self._dir_mode
679
570
def get_repository_transport(self, repository_format):
680
571
"""Get the transport for use by repository format in this BzrDir.
902
787
tree, branch = bzrdir._get_tree_branch()
903
788
return tree, branch, relpath
906
def open_containing_tree_branch_or_repository(klass, location):
907
"""Return the working tree, branch and repo contained by a location.
909
Returns (tree, branch, repository, relpath).
910
If there is no tree containing the location, tree will be None.
911
If there is no branch containing the location, branch will be None.
912
If there is no repository containing the location, repository will be
914
relpath is the portion of the path that is contained by the innermost
917
If no tree, branch or repository is found, a NotBranchError is raised.
919
bzrdir, relpath = klass.open_containing(location)
921
tree, branch = bzrdir._get_tree_branch()
922
except errors.NotBranchError:
924
repo = bzrdir.find_repository()
925
return None, None, repo, relpath
926
except (errors.NoRepositoryPresent):
927
raise errors.NotBranchError(location)
928
return tree, branch, branch.repository, relpath
930
790
def open_repository(self, _unsupported=False):
931
791
"""Open the repository object at this BzrDir if one is present.
1016
870
result_format.workingtree_format = tree._format.__class__()
1017
871
return result_format, source_repository
1019
def cloning_metadir(self, require_stacking=False):
873
def cloning_metadir(self):
1020
874
"""Produce a metadir suitable for cloning or sprouting with.
1022
876
These operations may produce workingtrees (yes, even though they're
1023
877
"cloning" something that doesn't have a tree), so a viable workingtree
1024
878
format must be selected.
1026
:require_stacking: If True, non-stackable formats will be upgraded
1027
to similar stackable formats.
1028
:returns: a BzrDirFormat with all component formats either set
1029
appropriately or set to None if that component should not be
1032
880
format, repository = self._cloning_metadir()
1033
881
if format._workingtree_format is None:
1036
884
tree_format = repository._format._matchingbzrdir.workingtree_format
1037
885
format.workingtree_format = tree_format.__class__()
1038
if (require_stacking and not
1039
format.get_branch_format().supports_stacking()):
1040
# We need to make a stacked branch, but the default format for the
1041
# target doesn't support stacking. So force a branch that *can*
1043
from bzrlib.branch import BzrBranchFormat7
1044
format._branch_format = BzrBranchFormat7()
1045
mutter("using %r for stacking" % (format._branch_format,))
1046
from bzrlib.repofmt import pack_repo
1047
if format.repository_format.rich_root_data:
1048
bzrdir_format_name = '1.6.1-rich-root'
1049
repo_format = pack_repo.RepositoryFormatKnitPack5RichRoot()
1051
bzrdir_format_name = '1.6'
1052
repo_format = pack_repo.RepositoryFormatKnitPack5()
1053
note('Source format does not support stacking, using format:'
1055
bzrdir_format_name, repo_format.get_format_description())
1056
format.repository_format = repo_format
1059
888
def checkout_metadir(self):
1078
907
contents more quickly than the revision tree, i.e. a workingtree.
1079
908
The revision tree will be used for cases where accelerator_tree's
1080
909
content is different.
1081
:param hardlink: If true, hard-link files from accelerator_tree,
1083
:param stacked: If true, create a stacked branch referring to the
1084
location of this control directory.
1086
911
target_transport = get_transport(url, possible_transports)
1087
912
target_transport.ensure_base()
1088
cloning_format = self.cloning_metadir(stacked)
1089
# Create/update the result branch
913
cloning_format = self.cloning_metadir()
1090
914
result = cloning_format.initialize_on_transport(target_transport)
1092
916
source_branch = self.open_branch()
1093
917
source_repository = source_branch.repository
1095
stacked_branch_url = self.root_transport.base
1097
# if a stacked branch wasn't requested, we don't create one
1098
# even if the origin was stacked
1099
stacked_branch_url = None
1100
918
except errors.NotBranchError:
1101
919
source_branch = None
1103
921
source_repository = self.open_repository()
1104
922
except errors.NoRepositoryPresent:
1105
923
source_repository = None
1106
stacked_branch_url = None
1107
repository_policy = result.determine_repository_policy(
1108
force_new_repo, stacked_branch_url, require_stacking=stacked)
1109
result_repo = repository_policy.acquire_repository()
1110
if source_repository is not None:
1111
# Fetch while stacked to prevent unstacked fetch from
1113
result_repo.fetch(source_repository, revision_id=revision_id)
1115
if source_branch is None:
1116
# this is for sprouting a bzrdir without a branch; is that
1118
# Not especially, but it's part of the contract.
1119
result_branch = result.create_branch()
1121
# Force NULL revision to avoid using repository before stacking
1123
result_branch = source_branch.sprout(
1124
result, revision_id=_mod_revision.NULL_REVISION)
1125
parent_location = result_branch.get_parent()
1126
mutter("created new branch %r" % (result_branch,))
1127
repository_policy.configure_branch(result_branch)
928
result_repo = result.find_repository()
929
except errors.NoRepositoryPresent:
931
if source_repository is None and result_repo is not None:
933
elif source_repository is None and result_repo is None:
934
# no repo available, make a new one
935
result.create_repository()
936
elif source_repository is not None and result_repo is None:
937
# have source, and want to make a new target repo
938
result_repo = source_repository.sprout(result,
939
revision_id=revision_id)
941
# fetch needed content into target.
942
if source_repository is not None:
944
# source_repository.copy_content_into(result_repo,
945
# revision_id=revision_id)
946
# so we can override the copy method
947
result_repo.fetch(source_repository, revision_id=revision_id)
1128
948
if source_branch is not None:
1129
source_branch.copy_content_into(result_branch, revision_id)
1130
# Override copy_content_into
1131
result_branch.set_parent(parent_location)
1133
# Create/update the result working tree
1134
if isinstance(target_transport, local.LocalTransport) and (
949
source_branch.sprout(result, revision_id=revision_id)
951
result.create_branch()
952
if isinstance(target_transport, LocalTransport) and (
1135
953
result_repo is None or result_repo.make_working_trees()):
1136
wt = result.create_workingtree(accelerator_tree=accelerator_tree,
954
wt = result.create_workingtree(accelerator_tree=accelerator_tree)
1140
957
if wt.path2id('') is None:
1191
1007
"""Pre-splitout bzrdirs do not suffer from stale locks."""
1192
1008
raise NotImplementedError(self.break_lock)
1194
def cloning_metadir(self, require_stacking=False):
1195
"""Produce a metadir suitable for cloning with."""
1196
if require_stacking:
1197
return format_registry.make_bzrdir('1.6')
1198
return self._format.__class__()
1200
def clone(self, url, revision_id=None, force_new_repo=False,
1201
preserve_stacking=False):
1202
"""See BzrDir.clone().
1204
force_new_repo has no effect, since this family of formats always
1205
require a new repository.
1206
preserve_stacking has no effect, since no source branch using this
1207
family of formats can be stacked, so there is no stacking to preserve.
1010
def clone(self, url, revision_id=None, force_new_repo=False):
1011
"""See BzrDir.clone()."""
1012
from bzrlib.workingtree import WorkingTreeFormat2
1209
1013
self._make_tail(url)
1210
1014
result = self._format._initialize_for_clone(url)
1211
1015
self.open_repository().clone(result, revision_id=revision_id)
1212
1016
from_branch = self.open_branch()
1213
1017
from_branch.clone(result, revision_id=revision_id)
1215
tree = self.open_workingtree()
1019
self.open_workingtree().clone(result)
1216
1020
except errors.NotLocalUrl:
1217
1021
# make a new one, this format always has to have one.
1218
result._init_workingtree()
1023
WorkingTreeFormat2().initialize(result)
1024
except errors.NotLocalUrl:
1025
# but we cannot do it for remote trees.
1026
to_branch = result.open_branch()
1027
WorkingTreeFormat2().stub_initialize_remote(to_branch.control_files)
1223
1030
def create_branch(self):
1224
1031
"""See BzrDir.create_branch."""
1225
return self._format.get_branch_format().initialize(self)
1032
return self.open_branch()
1227
1034
def destroy_branch(self):
1228
1035
"""See BzrDir.destroy_branch."""
1696
1479
# FIXME: RBC 20060121 don't peek under
1698
1481
mode=temp_control._dir_mode)
1699
if sys.platform == 'win32' and isinstance(transport, local.LocalTransport):
1482
if sys.platform == 'win32' and isinstance(transport, LocalTransport):
1700
1483
win32utils.set_file_attr_hidden(transport._abspath('.bzr'))
1701
1484
file_mode = temp_control._file_mode
1702
1485
del temp_control
1703
bzrdir_transport = transport.clone('.bzr')
1704
utf8_files = [('README',
1705
"This is a Bazaar control directory.\n"
1706
"Do not change any files in this directory.\n"
1707
"See http://bazaar-vcs.org/ for more information about Bazaar.\n"),
1486
mutter('created control directory in ' + transport.base)
1487
control = transport.clone('.bzr')
1488
utf8_files = [('README',
1489
"This is a Bazaar-NG control directory.\n"
1490
"Do not change any files in this directory.\n"),
1708
1491
('branch-format', self.get_format_string()),
1710
1493
# NB: no need to escape relative paths that are url safe.
1711
control_files = lockable_files.LockableFiles(bzrdir_transport,
1712
self._lock_file_name, self._lock_class)
1494
control_files = lockable_files.LockableFiles(control,
1495
self._lock_file_name, self._lock_class)
1713
1496
control_files.create_lock()
1714
1497
control_files.lock_write()
1716
for (filename, content) in utf8_files:
1717
bzrdir_transport.put_bytes(filename, content,
1499
for file, content in utf8_files:
1500
control_files.put_utf8(file, content)
1720
1502
control_files.unlock()
1721
1503
return self.open(transport, _found=True)
2204
1993
self.bzrdir.transport.mkdir('revision-store')
2205
1994
revision_transport = self.bzrdir.transport.clone('revision-store')
2206
1995
# TODO permissions
2207
from bzrlib.xml5 import serializer_v5
2208
from bzrlib.repofmt.weaverepo import RevisionTextStore
2209
revision_store = RevisionTextStore(revision_transport,
2210
serializer_v5, False, versionedfile.PrefixMapper(),
2211
lambda:True, lambda:True)
1996
_revision_store = TextRevisionStore(TextStore(revision_transport,
2000
transaction = WriteTransaction()
2213
2001
for i, rev_id in enumerate(self.converted_revs):
2214
2002
self.pb.update('write revision', i, len(self.converted_revs))
2215
text = serializer_v5.write_revision_to_string(
2216
self.revisions[rev_id])
2218
revision_store.add_lines(key, None, osutils.split_lines(text))
2003
_revision_store.add_revision(self.revisions[rev_id], transaction)
2220
2005
self.pb.clear()
2235
2020
self.absent_revisions.add(rev_id)
2237
rev = self.branch.repository.get_revision(rev_id)
2022
rev = self.branch.repository._revision_store.get_revision(rev_id,
2023
self.branch.repository.get_transaction())
2238
2024
for parent_id in rev.parent_ids:
2239
2025
self.known_revisions.add(parent_id)
2240
2026
self.to_read.append(parent_id)
2241
2027
self.revisions[rev_id] = rev
2243
2029
def _load_old_inventory(self, rev_id):
2030
assert rev_id not in self.converted_revs
2244
2031
old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
2245
2032
inv = xml4.serializer_v4.read_inventory_from_string(old_inv_xml)
2246
2033
inv.revision_id = rev_id
2247
2034
rev = self.revisions[rev_id]
2035
if rev.inventory_sha1:
2036
assert rev.inventory_sha1 == sha_string(old_inv_xml), \
2037
'inventory sha mismatch for {%s}' % rev_id
2250
2040
def _load_updated_inventory(self, rev_id):
2041
assert rev_id in self.converted_revs
2251
2042
inv_xml = self.inv_weave.get_text(rev_id)
2252
2043
inv = xml5.serializer_v5.read_inventory_from_string(inv_xml, rev_id)
2384
2190
if (filename.endswith(".weave") or
2385
2191
filename.endswith(".gz") or
2386
2192
filename.endswith(".sig")):
2387
file_id, suffix = os.path.splitext(filename)
2193
file_id = os.path.splitext(filename)[0]
2389
2195
file_id = filename
2391
new_name = store._mapper.map((file_id,)) + suffix
2196
prefix_dir = store.hash_prefix(file_id)
2392
2197
# FIXME keep track of the dirs made RBC 20060121
2394
store_transport.move(filename, new_name)
2199
store_transport.move(filename, prefix_dir + '/' + filename)
2395
2200
except errors.NoSuchFile: # catches missing dirs strangely enough
2396
store_transport.mkdir(osutils.dirname(new_name))
2397
store_transport.move(filename, new_name)
2398
self.bzrdir.transport.put_bytes(
2400
BzrDirFormat6().get_format_string(),
2401
mode=self.bzrdir._get_file_mode())
2201
store_transport.mkdir(prefix_dir)
2202
store_transport.move(filename, prefix_dir + '/' + filename)
2203
self.bzrdir._control_files.put_utf8('branch-format', BzrDirFormat6().get_format_string())
2404
2206
class ConvertBzrDir6ToMeta(Converter):
2558
2353
# TODO: conversions of Branch and Tree should be done by
2559
# InterXFormat lookups/some sort of registry.
2354
# InterXFormat lookups
2560
2355
# Avoid circular imports
2561
2356
from bzrlib import branch as _mod_branch
2562
old = branch._format.__class__
2563
new = self.target_format.get_branch_format().__class__
2565
if (old == _mod_branch.BzrBranchFormat5 and
2566
new in (_mod_branch.BzrBranchFormat6,
2567
_mod_branch.BzrBranchFormat7)):
2568
branch_converter = _mod_branch.Converter5to6()
2569
elif (old == _mod_branch.BzrBranchFormat6 and
2570
new == _mod_branch.BzrBranchFormat7):
2571
branch_converter = _mod_branch.Converter6to7()
2573
raise errors.BadConversionTarget("No converter", new)
2357
if (branch._format.__class__ is _mod_branch.BzrBranchFormat5 and
2358
self.target_format.get_branch_format().__class__ is
2359
_mod_branch.BzrBranchFormat6):
2360
branch_converter = _mod_branch.Converter5to6()
2574
2361
branch_converter.convert(branch)
2575
branch = self.bzrdir.open_branch()
2576
old = branch._format.__class__
2578
2363
tree = self.bzrdir.open_workingtree(recommend_upgrade=False)
2579
2364
except (errors.NoWorkingTree, errors.NotLocalUrl):
2603
2388
def probe_transport(klass, transport):
2604
2389
"""Return a RemoteBzrDirFormat object if it looks possible."""
2606
medium = transport.get_smart_medium()
2391
client = transport.get_smart_client()
2607
2392
except (NotImplementedError, AttributeError,
2608
errors.TransportNotPossible, errors.NoSmartMedium,
2609
errors.SmartProtocolError):
2393
errors.TransportNotPossible):
2610
2394
# no smart server, so not a branch for this format type.
2611
2395
raise errors.NotBranchError(path=transport.base)
2613
# Decline to open it if the server doesn't support our required
2614
# version (3) so that the VFS-based transport will do it.
2615
if medium.should_probe():
2617
server_version = medium.protocol_version()
2618
except errors.SmartProtocolError:
2619
# Apparently there's no usable smart server there, even though
2620
# the medium supports the smart protocol.
2621
raise errors.NotBranchError(path=transport.base)
2622
if server_version != '2':
2623
raise errors.NotBranchError(path=transport.base)
2397
# Send a 'hello' request in protocol version one, and decline to
2398
# open it if the server doesn't support our required version (2) so
2399
# that the VFS-based transport will do it.
2400
request = client.get_request()
2401
smart_protocol = protocol.SmartClientRequestProtocolOne(request)
2402
server_version = smart_protocol.query_version()
2403
if server_version != 2:
2404
raise errors.NotBranchError(path=transport.base)
2626
2407
def initialize_on_transport(self, transport):
2628
2409
# hand off the request to the smart server
2629
client_medium = transport.get_smart_medium()
2410
shared_medium = transport.get_shared_medium()
2630
2411
except errors.NoSmartMedium:
2631
2412
# TODO: lookup the local format from a server hint.
2632
2413
local_dir_format = BzrDirMetaFormat1()
2633
2414
return local_dir_format.initialize_on_transport(transport)
2634
client = _SmartClient(client_medium)
2415
client = _SmartClient(shared_medium)
2635
2416
path = client.remote_path_from_transport(transport)
2636
response = client.call('BzrDirFormat.initialize', path)
2637
if response[0] != 'ok':
2638
raise errors.SmartProtocolError('unexpected response code %s' % (response,))
2417
response = _SmartClient(shared_medium).call('BzrDirFormat.initialize',
2419
assert response[0] in ('ok', ), 'unexpected response code %s' % (response,)
2639
2420
return remote.RemoteBzrDir(transport)
2641
2422
def _open(self, transport):
2732
2503
This function mainly exists to prevent the info object from being
2733
2504
supplied directly.
2735
registry.Registry.register(self, key, factory, help,
2506
registry.Registry.register(self, key, factory, help,
2736
2507
BzrDirFormatInfo(native, deprecated, hidden, experimental))
2738
self._aliases.add(key)
2740
2509
def register_lazy(self, key, module_name, member_name, help, native=True,
2741
deprecated=False, hidden=False, experimental=False, alias=False):
2742
registry.Registry.register_lazy(self, key, module_name, member_name,
2510
deprecated=False, hidden=False, experimental=False):
2511
registry.Registry.register_lazy(self, key, module_name, member_name,
2743
2512
help, BzrDirFormatInfo(native, deprecated, hidden, experimental))
2745
self._aliases.add(key)
2747
2514
def set_default(self, key):
2748
2515
"""Set the 'default' key to be a clone of the supplied key.
2750
2517
This method must be called once and only once.
2752
registry.Registry.register(self, 'default', self.get(key),
2519
registry.Registry.register(self, 'default', self.get(key),
2753
2520
self.get_help(key), info=self.get_info(key))
2754
self._aliases.add('default')
2756
2522
def set_default_repository(self, key):
2757
2523
"""Set the FormatRegistry default and Repository default.
2823
class RepositoryAcquisitionPolicy(object):
2824
"""Abstract base class for repository acquisition policies.
2826
A repository acquisition policy decides how a BzrDir acquires a repository
2827
for a branch that is being created. The most basic policy decision is
2828
whether to create a new repository or use an existing one.
2830
def __init__(self, stack_on, stack_on_pwd, require_stacking):
2833
:param stack_on: A location to stack on
2834
:param stack_on_pwd: If stack_on is relative, the location it is
2836
:param require_stacking: If True, it is a failure to not stack.
2838
self._stack_on = stack_on
2839
self._stack_on_pwd = stack_on_pwd
2840
self._require_stacking = require_stacking
2842
def configure_branch(self, branch):
2843
"""Apply any configuration data from this policy to the branch.
2845
Default implementation sets repository stacking.
2847
if self._stack_on is None:
2849
if self._stack_on_pwd is None:
2850
stack_on = self._stack_on
2853
stack_on = urlutils.rebase_url(self._stack_on,
2855
branch.bzrdir.root_transport.base)
2856
except errors.InvalidRebaseURLs:
2857
stack_on = self._get_full_stack_on()
2859
branch.set_stacked_on_url(stack_on)
2860
except errors.UnstackableBranchFormat:
2861
if self._require_stacking:
2864
def _get_full_stack_on(self):
2865
"""Get a fully-qualified URL for the stack_on location."""
2866
if self._stack_on is None:
2868
if self._stack_on_pwd is None:
2869
return self._stack_on
2871
return urlutils.join(self._stack_on_pwd, self._stack_on)
2873
def _add_fallback(self, repository):
2874
"""Add a fallback to the supplied repository, if stacking is set."""
2875
stack_on = self._get_full_stack_on()
2876
if stack_on is None:
2878
stacked_dir = BzrDir.open(stack_on)
2880
stacked_repo = stacked_dir.open_branch().repository
2881
except errors.NotBranchError:
2882
stacked_repo = stacked_dir.open_repository()
2884
repository.add_fallback_repository(stacked_repo)
2885
except errors.UnstackableRepositoryFormat:
2886
if self._require_stacking:
2889
def acquire_repository(self, make_working_trees=None, shared=False):
2890
"""Acquire a repository for this bzrdir.
2892
Implementations may create a new repository or use a pre-exising
2894
:param make_working_trees: If creating a repository, set
2895
make_working_trees to this value (if non-None)
2896
:param shared: If creating a repository, make it shared if True
2897
:return: A repository
2899
raise NotImplemented(RepositoryAcquisitionPolicy.acquire_repository)
2902
class CreateRepository(RepositoryAcquisitionPolicy):
2903
"""A policy of creating a new repository"""
2905
def __init__(self, bzrdir, stack_on=None, stack_on_pwd=None,
2906
require_stacking=False):
2909
:param bzrdir: The bzrdir to create the repository on.
2910
:param stack_on: A location to stack on
2911
:param stack_on_pwd: If stack_on is relative, the location it is
2914
RepositoryAcquisitionPolicy.__init__(self, stack_on, stack_on_pwd,
2916
self._bzrdir = bzrdir
2918
def acquire_repository(self, make_working_trees=None, shared=False):
2919
"""Implementation of RepositoryAcquisitionPolicy.acquire_repository
2921
Creates the desired repository in the bzrdir we already have.
2923
repository = self._bzrdir.create_repository(shared=shared)
2924
self._add_fallback(repository)
2925
if make_working_trees is not None:
2926
repository.set_make_working_trees(make_working_trees)
2930
class UseExistingRepository(RepositoryAcquisitionPolicy):
2931
"""A policy of reusing an existing repository"""
2933
def __init__(self, repository, stack_on=None, stack_on_pwd=None,
2934
require_stacking=False):
2937
:param repository: The repository to use.
2938
:param stack_on: A location to stack on
2939
:param stack_on_pwd: If stack_on is relative, the location it is
2942
RepositoryAcquisitionPolicy.__init__(self, stack_on, stack_on_pwd,
2944
self._repository = repository
2946
def acquire_repository(self, make_working_trees=None, shared=False):
2947
"""Implementation of RepositoryAcquisitionPolicy.acquire_repository
2949
Returns an existing repository to use
2951
self._add_fallback(self._repository)
2952
return self._repository
2955
2590
format_registry = BzrDirFormatRegistry()
2956
2591
format_registry.register('weave', BzrDirFormat6,
2957
2592
'Pre-0.8 format. Slower than knit and does not'
3034
2669
branch_format='bzrlib.branch.BzrBranchFormat6',
3035
2670
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3037
format_registry.register_metadir('1.6',
3038
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack5',
3039
help='A branch and pack based repository that supports stacking. ',
3040
branch_format='bzrlib.branch.BzrBranchFormat7',
3041
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3043
format_registry.register_metadir('1.6.1-rich-root',
3044
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack5RichRoot',
3045
help='A branch and pack based repository that supports stacking '
3046
'and rich root data (needed for bzr-svn). ',
3047
branch_format='bzrlib.branch.BzrBranchFormat7',
3048
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3050
# The following two formats should always just be aliases.
3051
format_registry.register_metadir('development',
3052
'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment2',
3053
help='Current development format. Can convert data to and from pack-0.92 '
3054
'(and anything compatible with pack-0.92) format repositories. '
3055
'Repositories and branches in this format can only be read by bzr.dev. '
3057
'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3059
branch_format='bzrlib.branch.BzrBranchFormat7',
3060
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3064
format_registry.register_metadir('development-subtree',
3065
'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment2Subtree',
3066
help='Current development format, subtree variant. Can convert data to and '
3067
'from pack-0.92-subtree (and anything compatible with '
3068
'pack-0.92-subtree) format repositories. Repositories and branches in '
3069
'this format can only be read by bzr.dev. Please read '
3070
'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3072
branch_format='bzrlib.branch.BzrBranchFormat7',
3073
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3077
# And the development formats above will have aliased one of the following:
3078
format_registry.register_metadir('development2',
3079
'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment2',
3080
help='1.6.1 with B+Tree based index. '
3082
'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3084
branch_format='bzrlib.branch.BzrBranchFormat7',
3085
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3089
format_registry.register_metadir('development2-subtree',
3090
'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment2Subtree',
3091
help='1.6.1-subtree with B+Tree based index. '
3093
'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3095
branch_format='bzrlib.branch.BzrBranchFormat7',
3096
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3100
# The current format that is made on 'bzr init'.
3101
2672
format_registry.set_default('pack-0.92')