197
162
except errors.NotBranchError:
200
self.open_workingtree().clone(result)
165
self.open_workingtree().clone(result, basis=basis_tree)
201
166
except (errors.NoWorkingTree, errors.NotLocalUrl):
170
def _get_basis_components(self, basis):
171
"""Retrieve the basis components that are available at basis."""
173
return None, None, None
175
basis_tree = basis.open_workingtree()
176
basis_branch = basis_tree.branch
177
basis_repo = basis_branch.repository
178
except (errors.NoWorkingTree, errors.NotLocalUrl):
181
basis_branch = basis.open_branch()
182
basis_repo = basis_branch.repository
183
except errors.NotBranchError:
186
basis_repo = basis.open_repository()
187
except errors.NoRepositoryPresent:
189
return basis_repo, basis_branch, basis_tree
205
191
# TODO: This should be given a Transport, and should chdir up; otherwise
206
192
# this will open a new connection.
207
193
def _make_tail(self, url):
208
t = get_transport(url)
194
head, tail = urlutils.split(url)
195
if tail and tail != '.':
196
t = get_transport(head)
199
except errors.FileExists:
202
# TODO: Should take a Transport
212
def create(cls, base, format=None, possible_transports=None):
204
def create(cls, base):
213
205
"""Create a new BzrDir at the url 'base'.
215
207
This will call the current default formats initialize with base
216
208
as the only parameter.
218
:param format: If supplied, the format of branch to create. If not
219
supplied, the default is used.
220
:param possible_transports: If supplied, a list of transports that
221
can be reused to share a remote connection.
210
If you need a specific format, consider creating an instance
211
of that and calling initialize().
223
213
if cls is not BzrDir:
224
raise AssertionError("BzrDir.create always creates the default"
225
" format, not one of %r" % cls)
226
t = get_transport(base, possible_transports)
229
format = BzrDirFormat.get_default_format()
230
return format.initialize(base, possible_transports)
214
raise AssertionError("BzrDir.create always creates the default format, "
215
"not one of %r" % cls)
216
head, tail = urlutils.split(base)
217
if tail and tail != '.':
218
t = get_transport(head)
221
except errors.FileExists:
223
return BzrDirFormat.get_default_format().initialize(safe_unicode(base))
232
225
def create_branch(self):
233
226
"""Create a branch in this BzrDir.
692
596
workingtree and discards it, and that's somewhat expensive.)
695
self.open_workingtree(recommend_upgrade=False)
599
self.open_workingtree()
697
601
except errors.NoWorkingTree:
700
def _cloning_metadir(self):
604
def cloning_metadir(self, basis=None):
701
605
"""Produce a metadir suitable for cloning with"""
606
def related_repository(bzrdir):
608
branch = bzrdir.open_branch()
609
return branch.repository
610
except errors.NotBranchError:
612
return bzrdir.open_repository()
702
613
result_format = self._format.__class__()
705
branch = self.open_branch()
706
source_repository = branch.repository
707
except errors.NotBranchError:
709
source_repository = self.open_repository()
616
source_repository = related_repository(self)
617
except errors.NoRepositoryPresent:
620
source_repository = related_repository(self)
621
result_format.repository_format = source_repository._format
710
622
except errors.NoRepositoryPresent:
711
source_repository = None
713
# XXX TODO: This isinstance is here because we have not implemented
714
# the fix recommended in bug # 103195 - to delegate this choice the
716
repo_format = source_repository._format
717
if not isinstance(repo_format, remote.RemoteRepositoryFormat):
718
result_format.repository_format = repo_format
720
# TODO: Couldn't we just probe for the format in these cases,
721
# rather than opening the whole tree? It would be a little
722
# faster. mbp 20070401
723
tree = self.open_workingtree(recommend_upgrade=False)
724
except (errors.NoWorkingTree, errors.NotLocalUrl):
725
result_format.workingtree_format = None
727
result_format.workingtree_format = tree._format.__class__()
728
return result_format, source_repository
730
def cloning_metadir(self):
731
"""Produce a metadir suitable for cloning or sprouting with.
733
These operations may produce workingtrees (yes, even though they're
734
"cloning" something that doesn't have a tree, so a viable workingtree
735
format must be selected.
737
format, repository = self._cloning_metadir()
738
if format._workingtree_format is None:
739
if repository is None:
741
tree_format = repository._format._matchingbzrdir.workingtree_format
742
format.workingtree_format = tree_format.__class__()
745
def checkout_metadir(self):
746
return self.cloning_metadir()
748
def sprout(self, url, revision_id=None, force_new_repo=False,
749
recurse='down', possible_transports=None):
626
def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
750
627
"""Create a copy of this bzrdir prepared for use as a new line of
787
666
result.create_repository()
788
667
elif source_repository is not None and result_repo is None:
789
668
# have source, and want to make a new target repo
790
result_repo = source_repository.sprout(result,
791
revision_id=revision_id)
669
# we don't clone the repo because that preserves attributes
670
# like is_shared(), and we have not yet implemented a
671
# repository sprout().
672
result_repo = result.create_repository()
673
if result_repo is not None:
793
674
# fetch needed content into target.
676
# XXX FIXME RBC 20060214 need tests for this when the basis
678
result_repo.fetch(basis_repo, revision_id=revision_id)
794
679
if source_repository is not None:
796
# source_repository.copy_content_into(result_repo,
797
# revision_id=revision_id)
798
# so we can override the copy method
799
680
result_repo.fetch(source_repository, revision_id=revision_id)
800
681
if source_branch is not None:
801
682
source_branch.sprout(result, revision_id=revision_id)
803
684
result.create_branch()
804
if isinstance(target_transport, LocalTransport) and (
805
result_repo is None or result_repo.make_working_trees()):
685
# TODO: jam 20060426 we probably need a test in here in the
686
# case that the newly sprouted branch is a remote one
687
if result_repo is None or result_repo.make_working_trees():
806
688
wt = result.create_workingtree()
809
if wt.path2id('') is None:
811
wt.set_root_id(self.open_workingtree.get_root_id())
812
except errors.NoWorkingTree:
818
if recurse == 'down':
820
basis = wt.basis_tree()
822
subtrees = basis.iter_references()
823
recurse_branch = wt.branch
824
elif source_branch is not None:
825
basis = source_branch.basis_tree()
827
subtrees = basis.iter_references()
828
recurse_branch = source_branch
833
for path, file_id in subtrees:
834
target = urlutils.join(url, urlutils.escape(path))
835
sublocation = source_branch.reference_parent(file_id, path)
836
sublocation.bzrdir.sprout(target,
837
basis.get_reference_revision(file_id, path),
838
force_new_repo=force_new_repo, recurse=recurse)
840
if basis is not None:
689
if wt.inventory.root is None:
691
wt.set_root_id(self.open_workingtree.get_root_id())
692
except errors.NoWorkingTree:
2161
1934
self.pb.note('starting repository conversion')
2162
1935
converter = CopyConverter(self.target_format.repository_format)
2163
1936
converter.convert(repo, pb)
2165
branch = self.bzrdir.open_branch()
2166
except errors.NotBranchError:
2169
# TODO: conversions of Branch and Tree should be done by
2170
# InterXFormat lookups
2171
# Avoid circular imports
2172
from bzrlib import branch as _mod_branch
2173
if (branch._format.__class__ is _mod_branch.BzrBranchFormat5 and
2174
self.target_format.get_branch_format().__class__ is
2175
_mod_branch.BzrBranchFormat6):
2176
branch_converter = _mod_branch.Converter5to6()
2177
branch_converter.convert(branch)
2179
tree = self.bzrdir.open_workingtree(recommend_upgrade=False)
2180
except (errors.NoWorkingTree, errors.NotLocalUrl):
2183
# TODO: conversions of Branch and Tree should be done by
2184
# InterXFormat lookups
2185
if (isinstance(tree, workingtree.WorkingTree3) and
2186
not isinstance(tree, workingtree_4.WorkingTree4) and
2187
isinstance(self.target_format.workingtree_format,
2188
workingtree_4.WorkingTreeFormat4)):
2189
workingtree_4.Converter3to4().convert(tree)
2190
1937
return to_convert
2193
# This is not in remote.py because it's small, and needs to be registered.
2194
# Putting it in remote.py creates a circular import problem.
2195
# we can make it a lazy object if the control formats is turned into something
2197
class RemoteBzrDirFormat(BzrDirMetaFormat1):
2198
"""Format representing bzrdirs accessed via a smart server"""
2200
def get_format_description(self):
2201
return 'bzr remote bzrdir'
2204
def probe_transport(klass, transport):
2205
"""Return a RemoteBzrDirFormat object if it looks possible."""
2207
client = transport.get_smart_client()
2208
except (NotImplementedError, AttributeError,
2209
errors.TransportNotPossible):
2210
# no smart server, so not a branch for this format type.
2211
raise errors.NotBranchError(path=transport.base)
2213
# Send a 'hello' request in protocol version one, and decline to
2214
# open it if the server doesn't support our required version (2) so
2215
# that the VFS-based transport will do it.
2216
request = client.get_request()
2217
smart_protocol = protocol.SmartClientRequestProtocolOne(request)
2218
server_version = smart_protocol.query_version()
2219
if server_version != 2:
2220
raise errors.NotBranchError(path=transport.base)
2223
def initialize_on_transport(self, transport):
2225
# hand off the request to the smart server
2226
shared_medium = transport.get_shared_medium()
2227
except errors.NoSmartMedium:
2228
# TODO: lookup the local format from a server hint.
2229
local_dir_format = BzrDirMetaFormat1()
2230
return local_dir_format.initialize_on_transport(transport)
2231
client = _SmartClient(shared_medium)
2232
path = client.remote_path_from_transport(transport)
2233
response = _SmartClient(shared_medium).call('BzrDirFormat.initialize',
2235
assert response[0] in ('ok', ), 'unexpected response code %s' % (response,)
2236
return remote.RemoteBzrDir(transport)
2238
def _open(self, transport):
2239
return remote.RemoteBzrDir(transport)
2241
def __eq__(self, other):
2242
if not isinstance(other, RemoteBzrDirFormat):
2244
return self.get_format_description() == other.get_format_description()
2247
BzrDirFormat.register_control_server_format(RemoteBzrDirFormat)
2250
class BzrDirFormatInfo(object):
2252
def __init__(self, native, deprecated, hidden):
2253
self.deprecated = deprecated
2254
self.native = native
2255
self.hidden = hidden
2258
class BzrDirFormatRegistry(registry.Registry):
2259
"""Registry of user-selectable BzrDir subformats.
2261
Differs from BzrDirFormat._control_formats in that it provides sub-formats,
2262
e.g. BzrDirMeta1 with weave repository. Also, it's more user-oriented.
2265
def register_metadir(self, key,
2266
repository_format, help, native=True, deprecated=False,
2270
"""Register a metadir subformat.
2272
These all use a BzrDirMetaFormat1 bzrdir, but can be parameterized
2273
by the Repository format.
2275
:param repository_format: The fully-qualified repository format class
2277
:param branch_format: Fully-qualified branch format class name as
2279
:param tree_format: Fully-qualified tree format class name as
2282
# This should be expanded to support setting WorkingTree and Branch
2283
# formats, once BzrDirMetaFormat1 supports that.
2284
def _load(full_name):
2285
mod_name, factory_name = full_name.rsplit('.', 1)
2287
mod = __import__(mod_name, globals(), locals(),
2289
except ImportError, e:
2290
raise ImportError('failed to load %s: %s' % (full_name, e))
2292
factory = getattr(mod, factory_name)
2293
except AttributeError:
2294
raise AttributeError('no factory %s in module %r'
2299
bd = BzrDirMetaFormat1()
2300
if branch_format is not None:
2301
bd.set_branch_format(_load(branch_format))
2302
if tree_format is not None:
2303
bd.workingtree_format = _load(tree_format)
2304
if repository_format is not None:
2305
bd.repository_format = _load(repository_format)
2307
self.register(key, helper, help, native, deprecated, hidden)
2309
def register(self, key, factory, help, native=True, deprecated=False,
2311
"""Register a BzrDirFormat factory.
2313
The factory must be a callable that takes one parameter: the key.
2314
It must produce an instance of the BzrDirFormat when called.
2316
This function mainly exists to prevent the info object from being
2319
registry.Registry.register(self, key, factory, help,
2320
BzrDirFormatInfo(native, deprecated, hidden))
2322
def register_lazy(self, key, module_name, member_name, help, native=True,
2323
deprecated=False, hidden=False):
2324
registry.Registry.register_lazy(self, key, module_name, member_name,
2325
help, BzrDirFormatInfo(native, deprecated, hidden))
2327
def set_default(self, key):
2328
"""Set the 'default' key to be a clone of the supplied key.
2330
This method must be called once and only once.
2332
registry.Registry.register(self, 'default', self.get(key),
2333
self.get_help(key), info=self.get_info(key))
2335
def set_default_repository(self, key):
2336
"""Set the FormatRegistry default and Repository default.
2338
This is a transitional method while Repository.set_default_format
2341
if 'default' in self:
2342
self.remove('default')
2343
self.set_default(key)
2344
format = self.get('default')()
2345
assert isinstance(format, BzrDirMetaFormat1)
2347
def make_bzrdir(self, key):
2348
return self.get(key)()
2350
def help_topic(self, topic):
2351
output = textwrap.dedent("""\
2352
Bazaar directory formats
2353
------------------------
2355
These formats can be used for creating branches, working trees, and
2359
default_help = self.get_help('default')
2361
for key in self.keys():
2362
if key == 'default':
2364
help = self.get_help(key)
2365
if help == default_help:
2366
default_realkey = key
2368
help_pairs.append((key, help))
2370
def wrapped(key, help, info):
2372
help = '(native) ' + help
2373
return ' %s:\n%s\n\n' % (key,
2374
textwrap.fill(help, initial_indent=' ',
2375
subsequent_indent=' '))
2376
output += wrapped('%s/default' % default_realkey, default_help,
2377
self.get_info('default'))
2378
deprecated_pairs = []
2379
for key, help in help_pairs:
2380
info = self.get_info(key)
2383
elif info.deprecated:
2384
deprecated_pairs.append((key, help))
2386
output += wrapped(key, help, info)
2387
if len(deprecated_pairs) > 0:
2388
output += "Deprecated formats\n------------------\n\n"
2389
for key, help in deprecated_pairs:
2390
info = self.get_info(key)
2391
output += wrapped(key, help, info)
2396
format_registry = BzrDirFormatRegistry()
2397
format_registry.register('weave', BzrDirFormat6,
2398
'Pre-0.8 format. Slower than knit and does not'
2399
' support checkouts or shared repositories.',
2401
format_registry.register_metadir('knit',
2402
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2403
'Format using knits. Recommended for interoperation with bzr <= 0.14.',
2404
branch_format='bzrlib.branch.BzrBranchFormat5',
2405
tree_format='bzrlib.workingtree.WorkingTreeFormat3')
2406
format_registry.register_metadir('metaweave',
2407
'bzrlib.repofmt.weaverepo.RepositoryFormat7',
2408
'Transitional format in 0.8. Slower than knit.',
2409
branch_format='bzrlib.branch.BzrBranchFormat5',
2410
tree_format='bzrlib.workingtree.WorkingTreeFormat3',
2412
format_registry.register_metadir('dirstate',
2413
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2414
help='New in 0.15: Fast local operations. Compatible with bzr 0.8 and '
2415
'above when accessed over the network.',
2416
branch_format='bzrlib.branch.BzrBranchFormat5',
2417
# this uses bzrlib.workingtree.WorkingTreeFormat4 because importing
2418
# directly from workingtree_4 triggers a circular import.
2419
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2421
format_registry.register_metadir('dirstate-tags',
2422
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2423
help='New in 0.15: Fast local operations and improved scaling for '
2424
'network operations. Additionally adds support for tags.'
2425
' Incompatible with bzr < 0.15.',
2426
branch_format='bzrlib.branch.BzrBranchFormat6',
2427
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2429
format_registry.register_metadir('dirstate-with-subtree',
2430
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
2431
help='New in 0.15: Fast local operations and improved scaling for '
2432
'network operations. Additionally adds support for versioning nested '
2433
'bzr branches. Incompatible with bzr < 0.15.',
2434
branch_format='bzrlib.branch.BzrBranchFormat6',
2435
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2438
format_registry.set_default('dirstate')