197
145
except errors.NotBranchError:
200
self.open_workingtree().clone(result)
148
self.open_workingtree().clone(result, basis=basis_tree)
201
149
except (errors.NoWorkingTree, errors.NotLocalUrl):
153
def _get_basis_components(self, basis):
154
"""Retrieve the basis components that are available at basis."""
156
return None, None, None
158
basis_tree = basis.open_workingtree()
159
basis_branch = basis_tree.branch
160
basis_repo = basis_branch.repository
161
except (errors.NoWorkingTree, errors.NotLocalUrl):
164
basis_branch = basis.open_branch()
165
basis_repo = basis_branch.repository
166
except errors.NotBranchError:
169
basis_repo = basis.open_repository()
170
except errors.NoRepositoryPresent:
172
return basis_repo, basis_branch, basis_tree
205
174
# TODO: This should be given a Transport, and should chdir up; otherwise
206
175
# this will open a new connection.
207
176
def _make_tail(self, url):
208
t = get_transport(url)
177
head, tail = urlutils.split(url)
178
if tail and tail != '.':
179
t = bzrlib.transport.get_transport(head)
182
except errors.FileExists:
185
# TODO: Should take a Transport
212
def create(cls, base, format=None, possible_transports=None):
187
def create(cls, base):
213
188
"""Create a new BzrDir at the url 'base'.
215
190
This will call the current default formats initialize with base
216
191
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.
193
If you need a specific format, consider creating an instance
194
of that and calling initialize().
223
196
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)
197
raise AssertionError("BzrDir.create always creates the default format, "
198
"not one of %r" % cls)
199
head, tail = urlutils.split(base)
200
if tail and tail != '.':
201
t = bzrlib.transport.get_transport(head)
204
except errors.FileExists:
206
return BzrDirFormat.get_default_format().initialize(safe_unicode(base))
232
208
def create_branch(self):
233
209
"""Create a branch in this BzrDir.
528
460
_unsupported is a private parameter to the BzrDir class.
530
462
t = get_transport(base)
531
return BzrDir.open_from_transport(t, _unsupported=_unsupported)
534
def open_from_transport(transport, _unsupported=False,
535
_server_formats=True):
536
"""Open a bzrdir within a particular directory.
538
:param transport: Transport containing the bzrdir.
539
:param _unsupported: private.
541
base = transport.base
543
def find_format(transport):
544
return transport, BzrDirFormat.find_format(
545
transport, _server_formats=_server_formats)
547
def redirected(transport, e, redirection_notice):
548
qualified_source = e.get_source_url()
549
relpath = transport.relpath(qualified_source)
550
if not e.target.endswith(relpath):
551
# Not redirected to a branch-format, not a branch
552
raise errors.NotBranchError(path=e.target)
553
target = e.target[:-len(relpath)]
554
note('%s is%s redirected to %s',
555
transport.base, e.permanently, target)
556
# Let's try with a new transport
557
qualified_target = e.get_target_url()[:-len(relpath)]
558
# FIXME: If 'transport' has a qualifier, this should
559
# be applied again to the new transport *iff* the
560
# schemes used are the same. It's a bit tricky to
561
# verify, so I'll punt for now
563
return get_transport(target)
566
transport, format = do_catching_redirections(find_format,
569
except errors.TooManyRedirections:
570
raise errors.NotBranchError(base)
463
mutter("trying to open %r with transport %r", base, t)
464
format = BzrDirFormat.find_format(t)
572
465
BzrDir._check_supported(format, _unsupported)
573
return format.open(transport, _found=True)
466
return format.open(t, _found=True)
575
468
def open_branch(self, unsupported=False):
576
469
"""Open the branch object at this BzrDir if one is present.
692
558
workingtree and discards it, and that's somewhat expensive.)
695
self.open_workingtree(recommend_upgrade=False)
561
self.open_workingtree()
697
563
except errors.NoWorkingTree:
700
def _cloning_metadir(self):
701
"""Produce a metadir suitable for cloning with"""
702
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()
710
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):
566
def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
750
567
"""Create a copy of this bzrdir prepared for use as a new line of
787
605
result.create_repository()
788
606
elif source_repository is not None and result_repo is None:
789
607
# have source, and want to make a new target repo
790
result_repo = source_repository.sprout(result,
791
revision_id=revision_id)
608
# we don't clone the repo because that preserves attributes
609
# like is_shared(), and we have not yet implemented a
610
# repository sprout().
611
result_repo = result.create_repository()
612
if result_repo is not None:
793
613
# fetch needed content into target.
794
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
result_repo.fetch(source_repository, revision_id=revision_id)
615
# XXX FIXME RBC 20060214 need tests for this when the basis
617
result_repo.fetch(basis_repo, revision_id=revision_id)
618
result_repo.fetch(source_repository, revision_id=revision_id)
800
619
if source_branch is not None:
801
620
source_branch.sprout(result, revision_id=revision_id)
803
622
result.create_branch()
804
if isinstance(target_transport, LocalTransport) and (
805
result_repo is None or result_repo.make_working_trees()):
806
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:
623
# TODO: jam 20060426 we probably need a test in here in the
624
# case that the newly sprouted branch is a remote one
625
if result_repo is None or result_repo.make_working_trees():
626
result.create_workingtree()
2060
1731
# we hard code the formats here because we are converting into
2061
1732
# the meta format. The meta format upgrader can take this to a
2062
1733
# future format within each component.
2063
self.put_format('repository', RepositoryFormat7())
1734
self.put_format('repository', bzrlib.repository.RepositoryFormat7())
2064
1735
for entry in repository_names:
2065
1736
self.move_entry('repository', entry)
2067
1738
self.step('Upgrading branch ')
2068
1739
self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
2069
1740
self.make_lock('branch')
2070
self.put_format('branch', BzrBranchFormat5())
1741
self.put_format('branch', bzrlib.branch.BzrBranchFormat5())
2071
1742
branch_files = [('revision-history', True),
2072
1743
('branch-name', True),
2073
1744
('parent', False)]
2074
1745
for entry in branch_files:
2075
1746
self.move_entry('branch', entry)
1748
self.step('Upgrading working tree')
1749
self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
1750
self.make_lock('checkout')
1751
self.put_format('checkout', bzrlib.workingtree.WorkingTreeFormat3())
1752
self.bzrdir.transport.delete_multi(self.garbage_inventories, self.pb)
2077
1753
checkout_files = [('pending-merges', True),
2078
1754
('inventory', True),
2079
1755
('stat-cache', False)]
2080
# If a mandatory checkout file is not present, the branch does not have
2081
# a functional checkout. Do not create a checkout in the converted
2083
for name, mandatory in checkout_files:
2084
if mandatory and name not in bzrcontents:
2085
has_checkout = False
2089
if not has_checkout:
2090
self.pb.note('No working tree.')
2091
# If some checkout files are there, we may as well get rid of them.
2092
for name, mandatory in checkout_files:
2093
if name in bzrcontents:
2094
self.bzrdir.transport.delete(name)
2096
from bzrlib.workingtree import WorkingTreeFormat3
2097
self.step('Upgrading working tree')
2098
self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
2099
self.make_lock('checkout')
2101
'checkout', WorkingTreeFormat3())
2102
self.bzrdir.transport.delete_multi(
2103
self.garbage_inventories, self.pb)
2104
for entry in checkout_files:
2105
self.move_entry('checkout', entry)
2106
if last_revision is not None:
2107
self.bzrdir._control_files.put_utf8(
2108
'checkout/last-revision', last_revision)
2109
self.bzrdir._control_files.put_utf8(
2110
'branch-format', BzrDirMetaFormat1().get_format_string())
1756
for entry in checkout_files:
1757
self.move_entry('checkout', entry)
1758
if last_revision is not None:
1759
self.bzrdir._control_files.put_utf8('checkout/last-revision',
1761
self.bzrdir._control_files.put_utf8('branch-format', BzrDirMetaFormat1().get_format_string())
2111
1762
return BzrDir.open(self.bzrdir.root_transport.base)
2113
1764
def make_lock(self, name):
2114
1765
"""Make a lock for the new control dir name."""
2115
1766
self.step('Make %s lock' % name)
2116
ld = lockdir.LockDir(self.bzrdir.transport,
2118
file_modebits=self.file_mode,
2119
dir_modebits=self.dir_mode)
1767
ld = LockDir(self.bzrdir.transport,
1769
file_modebits=self.file_mode,
1770
dir_modebits=self.dir_mode)
2122
1773
def move_entry(self, new_dir, entry):
2161
1812
self.pb.note('starting repository conversion')
2162
1813
converter = CopyConverter(self.target_format.repository_format)
2163
1814
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
1815
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')