124
99
source_repo_format.check_conversion_target(target_repo_format)
127
def _check_supported(format, allow_unsupported,
128
recommend_upgrade=True,
130
"""Give an error or warning on old formats.
132
:param format: may be any kind of format - workingtree, branch,
135
:param allow_unsupported: If true, allow opening
136
formats that are strongly deprecated, and which may
137
have limited functionality.
139
:param recommend_upgrade: If true (default), warn
140
the user through the ui object that they may wish
141
to upgrade the object.
102
def _check_supported(format, allow_unsupported):
103
"""Check whether format is a supported format.
105
If allow_unsupported is True, this is a no-op.
143
# TODO: perhaps move this into a base Format class; it's not BzrDir
144
# specific. mbp 20070323
145
107
if not allow_unsupported and not format.is_supported():
146
108
# see open_downlevel to open legacy branches.
147
109
raise errors.UnsupportedFormatError(format=format)
148
if recommend_upgrade \
149
and getattr(format, 'upgrade_recommended', False):
150
ui.ui_factory.recommend_upgrade(
151
format.get_format_description(),
154
def clone(self, url, revision_id=None, force_new_repo=False,
155
preserve_stacking=False):
111
def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
156
112
"""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
162
itself to download less data.
163
:param force_new_repo: Do not use a shared repository for the target
164
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
return self.clone_on_transport(get_transport(url),
169
revision_id=revision_id,
170
force_new_repo=force_new_repo,
171
preserve_stacking=preserve_stacking)
173
def clone_on_transport(self, transport, revision_id=None,
174
force_new_repo=False, preserve_stacking=False,
176
"""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
182
itself to download less data.
183
:param force_new_repo: Do not use a shared repository for the target,
184
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
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
114
If urls last component does not exist, it will be created.
116
if revision_id is not None, then the clone operation may tune
117
itself to download less data.
118
:param force_new_repo: Do not use a shared repository for the target
119
even if one is available.
122
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
123
result = self._format.initialize(url)
194
125
local_repo = self.find_repository()
195
126
except errors.NoRepositoryPresent:
196
127
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
129
# 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
if not require_stacking and repository_policy._require_stacking:
222
require_stacking = True
223
result._format.require_stacking()
224
result_repo.fetch(local_repo, revision_id=revision_id)
131
result_repo = local_repo.clone(
133
revision_id=revision_id,
135
result_repo.set_make_working_trees(local_repo.make_working_trees())
138
result_repo = result.find_repository()
139
# fetch content this dir needs.
141
# XXX FIXME RBC 20060214 need tests for this when the basis
143
result_repo.fetch(basis_repo, revision_id=revision_id)
144
result_repo.fetch(local_repo, revision_id=revision_id)
145
except errors.NoRepositoryPresent:
146
# needed to make one anyway.
147
result_repo = local_repo.clone(
149
revision_id=revision_id,
151
result_repo.set_make_working_trees(local_repo.make_working_trees())
227
152
# 1 if there is a branch present
228
153
# make sure its content is available in the target repository
230
if local_branch is not None:
231
result_branch = local_branch.clone(result, revision_id=revision_id)
232
if repository_policy is not None:
233
repository_policy.configure_branch(result_branch)
234
if result_repo is None or result_repo.make_working_trees():
156
self.open_branch().clone(result, revision_id=revision_id)
157
except errors.NotBranchError:
160
self.open_workingtree().clone(result, basis=basis_tree)
161
except (errors.NoWorkingTree, errors.NotLocalUrl):
165
def _get_basis_components(self, basis):
166
"""Retrieve the basis components that are available at basis."""
168
return None, None, None
170
basis_tree = basis.open_workingtree()
171
basis_branch = basis_tree.branch
172
basis_repo = basis_branch.repository
173
except (errors.NoWorkingTree, errors.NotLocalUrl):
236
self.open_workingtree().clone(result)
237
except (errors.NoWorkingTree, errors.NotLocalUrl):
176
basis_branch = basis.open_branch()
177
basis_repo = basis_branch.repository
178
except errors.NotBranchError:
181
basis_repo = basis.open_repository()
182
except errors.NoRepositoryPresent:
184
return basis_repo, basis_branch, basis_tree
241
186
# TODO: This should be given a Transport, and should chdir up; otherwise
242
187
# this will open a new connection.
243
188
def _make_tail(self, url):
244
t = get_transport(url)
189
head, tail = urlutils.split(url)
190
if tail and tail != '.':
191
t = bzrlib.transport.get_transport(head)
194
except errors.FileExists:
197
# TODO: Should take a Transport
248
def create(cls, base, format=None, possible_transports=None):
199
def create(cls, base):
249
200
"""Create a new BzrDir at the url 'base'.
251
:param format: If supplied, the format of branch to create. If not
252
supplied, the default is used.
253
:param possible_transports: If supplied, a list of transports that
254
can be reused to share a remote connection.
202
This will call the current default formats initialize with base
203
as the only parameter.
205
If you need a specific format, consider creating an instance
206
of that and calling initialize().
256
208
if cls is not BzrDir:
257
raise AssertionError("BzrDir.create always creates the default"
258
" format, not one of %r" % cls)
259
t = get_transport(base, possible_transports)
262
format = BzrDirFormat.get_default_format()
263
return format.initialize_on_transport(t)
266
def find_bzrdirs(transport, evaluate=None, list_current=None):
267
"""Find bzrdirs recursively from current location.
269
This is intended primarily as a building block for more sophisticated
270
functionality, like finding trees under a directory, or finding
271
branches that use a given repository.
272
:param evaluate: An optional callable that yields recurse, value,
273
where recurse controls whether this bzrdir is recursed into
274
and value is the value to yield. By default, all bzrdirs
275
are recursed into, and the return value is the bzrdir.
276
:param list_current: if supplied, use this function to list the current
277
directory, instead of Transport.list_dir
278
:return: a generator of found bzrdirs, or whatever evaluate returns.
280
if list_current is None:
281
def list_current(transport):
282
return transport.list_dir('')
284
def evaluate(bzrdir):
287
pending = [transport]
288
while len(pending) > 0:
289
current_transport = pending.pop()
292
bzrdir = BzrDir.open_from_transport(current_transport)
293
except errors.NotBranchError:
296
recurse, value = evaluate(bzrdir)
299
subdirs = list_current(current_transport)
300
except errors.NoSuchFile:
303
for subdir in sorted(subdirs, reverse=True):
304
pending.append(current_transport.clone(subdir))
307
def find_branches(transport):
308
"""Find all branches under a transport.
310
This will find all branches below the transport, including branches
311
inside other branches. Where possible, it will use
312
Repository.find_branches.
314
To list all the branches that use a particular Repository, see
315
Repository.find_branches
317
def evaluate(bzrdir):
319
repository = bzrdir.open_repository()
320
except errors.NoRepositoryPresent:
323
return False, (None, repository)
325
branch = bzrdir.open_branch()
326
except errors.NotBranchError:
327
return True, (None, None)
329
return True, (branch, None)
331
for branch, repo in BzrDir.find_bzrdirs(transport, evaluate=evaluate):
333
branches.extend(repo.find_branches())
334
if branch is not None:
335
branches.append(branch)
338
def destroy_repository(self):
339
"""Destroy the repository in this BzrDir"""
340
raise NotImplementedError(self.destroy_repository)
209
raise AssertionError("BzrDir.create always creates the default format, "
210
"not one of %r" % cls)
211
head, tail = urlutils.split(base)
212
if tail and tail != '.':
213
t = bzrlib.transport.get_transport(head)
216
except errors.FileExists:
218
return BzrDirFormat.get_default_format().initialize(safe_unicode(base))
342
220
def create_branch(self):
343
221
"""Create a branch in this BzrDir.
345
The bzrdir's format will control what branch format is created.
223
The bzrdirs format will control what branch format is created.
346
224
For more control see BranchFormatXX.create(a_bzrdir).
348
226
raise NotImplementedError(self.create_branch)
350
def destroy_branch(self):
351
"""Destroy the branch in this BzrDir"""
352
raise NotImplementedError(self.destroy_branch)
355
def create_branch_and_repo(base, force_new_repo=False, format=None):
229
def create_branch_and_repo(base, force_new_repo=False):
356
230
"""Create a new BzrDir, Branch and Repository at the url 'base'.
358
This will use the current default BzrDirFormat unless one is
359
specified, and use whatever
232
This will use the current default BzrDirFormat, and use whatever
360
233
repository format that that uses via bzrdir.create_branch and
361
234
create_repository. If a shared repository is available that is used
465
276
:param force_new_repo: If True a new repository is always created.
466
277
:param force_new_tree: If True or False force creation of a tree or
467
278
prevent such creation respectively.
468
:param format: Override for the bzrdir format to create.
469
:param possible_transports: An optional reusable transports list.
279
:param format: Override for the for the bzrdir format to create
471
281
if force_new_tree:
472
282
# check for non local urls
473
t = get_transport(base, possible_transports)
474
if not isinstance(t, local.LocalTransport):
283
t = get_transport(safe_unicode(base))
284
if not isinstance(t, LocalTransport):
475
285
raise errors.NotLocalUrl(base)
476
bzrdir = BzrDir.create(base, format, possible_transports)
287
bzrdir = BzrDir.create(base)
289
bzrdir = format.initialize(base)
477
290
repo = bzrdir._find_or_create_repository(force_new_repo)
478
291
result = bzrdir.create_branch()
479
if force_new_tree or (repo.make_working_trees() and
292
if force_new_tree or (repo.make_working_trees() and
480
293
force_new_tree is None):
482
295
bzrdir.create_workingtree()
483
296
except errors.NotLocalUrl:
488
def create_standalone_workingtree(base, format=None):
301
def create_repository(base, shared=False):
302
"""Create a new BzrDir and Repository at the url 'base'.
304
This will use the current default BzrDirFormat, and use whatever
305
repository format that that uses for bzrdirformat.create_repository.
307
:param shared: Create a shared repository rather than a standalone
309
The Repository object is returned.
311
This must be overridden as an instance method in child classes, where
312
it should take no parameters and construct whatever repository format
313
that child class desires.
315
bzrdir = BzrDir.create(base)
316
return bzrdir.create_repository(shared)
319
def create_standalone_workingtree(base):
489
320
"""Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
491
322
'base' must be a local path or a file:// url.
493
This will use the current default BzrDirFormat unless one is
494
specified, and use whatever
324
This will use the current default BzrDirFormat, and use whatever
495
325
repository format that that uses for bzrdirformat.create_workingtree,
496
326
create_branch and create_repository.
498
:param format: Override for the bzrdir format to create.
499
328
:return: The WorkingTree object.
501
t = get_transport(base)
502
if not isinstance(t, local.LocalTransport):
330
t = get_transport(safe_unicode(base))
331
if not isinstance(t, LocalTransport):
503
332
raise errors.NotLocalUrl(base)
504
bzrdir = BzrDir.create_branch_and_repo(base,
506
format=format).bzrdir
333
bzrdir = BzrDir.create_branch_and_repo(safe_unicode(base),
334
force_new_repo=True).bzrdir
507
335
return bzrdir.create_workingtree()
509
def create_workingtree(self, revision_id=None, from_branch=None,
510
accelerator_tree=None, hardlink=False):
337
def create_workingtree(self, revision_id=None):
511
338
"""Create a working tree at this BzrDir.
513
:param revision_id: create it as of this revision id.
514
:param from_branch: override bzrdir branch (for lightweight checkouts)
515
:param accelerator_tree: A tree which can be used for retrieving file
516
contents more quickly than the revision tree, i.e. a workingtree.
517
The revision tree will be used for cases where accelerator_tree's
518
content is different.
340
revision_id: create it as of this revision id.
520
342
raise NotImplementedError(self.create_workingtree)
522
def backup_bzrdir(self):
523
"""Backup this bzr control directory.
525
:return: Tuple with old path name and new path name
344
def find_repository(self):
345
"""Find the repository that should be used for a_bzrdir.
347
This does not require a branch as we use it to find the repo for
348
new branches as well as to hook existing branches up to their
527
pb = ui.ui_factory.nested_progress_bar()
529
# FIXME: bug 300001 -- the backup fails if the backup directory
530
# already exists, but it should instead either remove it or make
531
# a new backup directory.
533
# FIXME: bug 262450 -- the backup directory should have the same
534
# permissions as the .bzr directory (probably a bug in copy_tree)
535
old_path = self.root_transport.abspath('.bzr')
536
new_path = self.root_transport.abspath('backup.bzr')
537
pb.note('making backup of %s' % (old_path,))
538
pb.note(' to %s' % (new_path,))
539
self.root_transport.copy_tree('.bzr', 'backup.bzr')
540
return (old_path, new_path)
544
def retire_bzrdir(self, limit=10000):
545
"""Permanently disable the bzrdir.
547
This is done by renaming it to give the user some ability to recover
548
if there was a problem.
550
This will have horrible consequences if anyone has anything locked or
552
:param limit: number of times to retry
557
to_path = '.bzr.retired.%d' % i
558
self.root_transport.rename('.bzr', to_path)
559
note("renamed %s to %s"
560
% (self.root_transport.abspath('.bzr'), to_path))
562
except (errors.TransportError, IOError, errors.PathError):
569
def destroy_workingtree(self):
570
"""Destroy the working tree at this BzrDir.
572
Formats that do not support this may raise UnsupportedOperation.
574
raise NotImplementedError(self.destroy_workingtree)
576
def destroy_workingtree_metadata(self):
577
"""Destroy the control files for the working tree at this BzrDir.
579
The contents of working tree files are not affected.
580
Formats that do not support this may raise UnsupportedOperation.
582
raise NotImplementedError(self.destroy_workingtree_metadata)
584
def _find_containing(self, evaluate):
585
"""Find something in a containing control directory.
587
This method will scan containing control dirs, until it finds what
588
it is looking for, decides that it will never find it, or runs out
589
of containing control directories to check.
591
It is used to implement find_repository and
592
determine_repository_policy.
594
:param evaluate: A function returning (value, stop). If stop is True,
595
the value will be returned.
599
result, stop = evaluate(found_bzrdir)
602
next_transport = found_bzrdir.root_transport.clone('..')
603
if (found_bzrdir.root_transport.base == next_transport.base):
604
# top of the file system
352
return self.open_repository()
353
except errors.NoRepositoryPresent:
355
next_transport = self.root_transport.clone('..')
606
357
# find the next containing bzrdir
608
359
found_bzrdir = BzrDir.open_containing_from_transport(
609
360
next_transport)[0]
610
361
except errors.NotBranchError:
613
def find_repository(self):
614
"""Find the repository that should be used.
616
This does not require a branch as we use it to find the repo for
617
new branches as well as to hook existing branches up to their
620
def usable_repository(found_bzrdir):
363
raise errors.NoRepositoryPresent(self)
621
364
# does it have a repository ?
623
366
repository = found_bzrdir.open_repository()
624
367
except errors.NoRepositoryPresent:
626
if found_bzrdir.root_transport.base == self.root_transport.base:
627
return repository, True
628
elif repository.is_shared():
629
return repository, True
368
next_transport = found_bzrdir.root_transport.clone('..')
369
if (found_bzrdir.root_transport.base == next_transport.base):
370
# top of the file system
374
if ((found_bzrdir.root_transport.base ==
375
self.root_transport.base) or repository.is_shared()):
633
found_repo = self._find_containing(usable_repository)
634
if found_repo is None:
635
raise errors.NoRepositoryPresent(self)
638
def get_branch_reference(self):
639
"""Return the referenced URL for the branch in this bzrdir.
641
:raises NotBranchError: If there is no Branch.
642
:return: The URL the branch in this bzrdir references if it is a
643
reference branch, or None for regular branches.
378
raise errors.NoRepositoryPresent(self)
379
raise errors.NoRepositoryPresent(self)
647
381
def get_branch_transport(self, branch_format):
648
382
"""Get the transport for use by branch format in this BzrDir.
1075
617
if revision_id is not None, then the clone operation may tune
1076
618
itself to download less data.
1077
:param accelerator_tree: A tree which can be used for retrieving file
1078
contents more quickly than the revision tree, i.e. a workingtree.
1079
The revision tree will be used for cases where accelerator_tree's
1080
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.
1085
:param create_tree_if_local: If true, a working-tree will be created
1086
when working locally.
1088
target_transport = get_transport(url, possible_transports)
1089
target_transport.ensure_base()
1090
cloning_format = self.cloning_metadir(stacked)
1091
# Create/update the result branch
1092
result = cloning_format.initialize_on_transport(target_transport)
1093
# if a stacked branch wasn't requested, we don't create one
1094
# even if the origin was stacked
1095
stacked_branch_url = None
1096
if source_branch is not None:
1098
stacked_branch_url = self.root_transport.base
621
cloning_format = self.cloning_metadir(basis)
622
result = cloning_format.initialize(url)
623
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
625
source_branch = self.open_branch()
1099
626
source_repository = source_branch.repository
1102
source_branch = self.open_branch()
1103
source_repository = source_branch.repository
1105
stacked_branch_url = self.root_transport.base
1106
except errors.NotBranchError:
1107
source_branch = None
1109
source_repository = self.open_repository()
1110
except errors.NoRepositoryPresent:
1111
source_repository = None
1112
repository_policy = result.determine_repository_policy(
1113
force_new_repo, stacked_branch_url, require_stacking=stacked)
1114
result_repo = repository_policy.acquire_repository()
1115
if source_repository is not None:
1116
# Fetch while stacked to prevent unstacked fetch from
1118
result_repo.fetch(source_repository, revision_id=revision_id)
1120
if source_branch is None:
1121
# this is for sprouting a bzrdir without a branch; is that
1123
# Not especially, but it's part of the contract.
1124
result_branch = result.create_branch()
1126
# Force NULL revision to avoid using repository before stacking
1128
result_branch = source_branch.sprout(
1129
result, revision_id=_mod_revision.NULL_REVISION)
1130
parent_location = result_branch.get_parent()
1131
mutter("created new branch %r" % (result_branch,))
1132
repository_policy.configure_branch(result_branch)
627
except errors.NotBranchError:
630
source_repository = self.open_repository()
631
except errors.NoRepositoryPresent:
632
# copy the entire basis one if there is one
633
# but there is no repository.
634
source_repository = basis_repo
639
result_repo = result.find_repository()
640
except errors.NoRepositoryPresent:
642
if source_repository is None and result_repo is not None:
644
elif source_repository is None and result_repo is None:
645
# no repo available, make a new one
646
result.create_repository()
647
elif source_repository is not None and result_repo is None:
648
# have source, and want to make a new target repo
649
# we don't clone the repo because that preserves attributes
650
# like is_shared(), and we have not yet implemented a
651
# repository sprout().
652
result_repo = result.create_repository()
653
if result_repo is not None:
654
# fetch needed content into target.
656
# XXX FIXME RBC 20060214 need tests for this when the basis
658
result_repo.fetch(basis_repo, revision_id=revision_id)
659
if source_repository is not None:
660
result_repo.fetch(source_repository, revision_id=revision_id)
1133
661
if source_branch is not None:
1134
source_branch.copy_content_into(result_branch, revision_id)
1135
# Override copy_content_into
1136
result_branch.set_parent(parent_location)
1138
# Create/update the result working tree
1139
if (create_tree_if_local and
1140
isinstance(target_transport, local.LocalTransport) and
1141
(result_repo is None or result_repo.make_working_trees())):
1142
wt = result.create_workingtree(accelerator_tree=accelerator_tree,
1146
if wt.path2id('') is None:
1148
wt.set_root_id(self.open_workingtree.get_root_id())
1149
except errors.NoWorkingTree:
662
source_branch.sprout(result, revision_id=revision_id)
1155
if recurse == 'down':
1157
basis = wt.basis_tree()
1159
subtrees = basis.iter_references()
1160
elif result_branch is not None:
1161
basis = result_branch.basis_tree()
1163
subtrees = basis.iter_references()
1164
elif source_branch is not None:
1165
basis = source_branch.basis_tree()
1167
subtrees = basis.iter_references()
1172
for path, file_id in subtrees:
1173
target = urlutils.join(url, urlutils.escape(path))
1174
sublocation = source_branch.reference_parent(file_id, path)
1175
sublocation.bzrdir.sprout(target,
1176
basis.get_reference_revision(file_id, path),
1177
force_new_repo=force_new_repo, recurse=recurse,
1180
if basis is not None:
664
result.create_branch()
665
# TODO: jam 20060426 we probably need a test in here in the
666
# case that the newly sprouted branch is a remote one
667
if result_repo is None or result_repo.make_working_trees():
668
result.create_workingtree()
2588
1884
self.pb.note('starting repository conversion')
2589
1885
converter = CopyConverter(self.target_format.repository_format)
2590
1886
converter.convert(repo, pb)
2592
branch = self.bzrdir.open_branch()
2593
except errors.NotBranchError:
2596
# TODO: conversions of Branch and Tree should be done by
2597
# InterXFormat lookups/some sort of registry.
2598
# Avoid circular imports
2599
from bzrlib import branch as _mod_branch
2600
old = branch._format.__class__
2601
new = self.target_format.get_branch_format().__class__
2603
if (old == _mod_branch.BzrBranchFormat5 and
2604
new in (_mod_branch.BzrBranchFormat6,
2605
_mod_branch.BzrBranchFormat7)):
2606
branch_converter = _mod_branch.Converter5to6()
2607
elif (old == _mod_branch.BzrBranchFormat6 and
2608
new == _mod_branch.BzrBranchFormat7):
2609
branch_converter = _mod_branch.Converter6to7()
2611
raise errors.BadConversionTarget("No converter", new)
2612
branch_converter.convert(branch)
2613
branch = self.bzrdir.open_branch()
2614
old = branch._format.__class__
2616
tree = self.bzrdir.open_workingtree(recommend_upgrade=False)
2617
except (errors.NoWorkingTree, errors.NotLocalUrl):
2620
# TODO: conversions of Branch and Tree should be done by
2621
# InterXFormat lookups
2622
if (isinstance(tree, workingtree.WorkingTree3) and
2623
not isinstance(tree, workingtree_4.DirStateWorkingTree) and
2624
isinstance(self.target_format.workingtree_format,
2625
workingtree_4.DirStateWorkingTreeFormat)):
2626
workingtree_4.Converter3to4().convert(tree)
2627
if (isinstance(tree, workingtree_4.DirStateWorkingTree) and
2628
not isinstance(tree, workingtree_4.WorkingTree5) and
2629
isinstance(self.target_format.workingtree_format,
2630
workingtree_4.WorkingTreeFormat5)):
2631
workingtree_4.Converter4to5().convert(tree)
2632
1887
return to_convert
2635
# This is not in remote.py because it's small, and needs to be registered.
2636
# Putting it in remote.py creates a circular import problem.
2637
# we can make it a lazy object if the control formats is turned into something
2639
class RemoteBzrDirFormat(BzrDirMetaFormat1):
2640
"""Format representing bzrdirs accessed via a smart server"""
2642
def get_format_description(self):
2643
return 'bzr remote bzrdir'
2646
def probe_transport(klass, transport):
2647
"""Return a RemoteBzrDirFormat object if it looks possible."""
2649
medium = transport.get_smart_medium()
2650
except (NotImplementedError, AttributeError,
2651
errors.TransportNotPossible, errors.NoSmartMedium,
2652
errors.SmartProtocolError):
2653
# no smart server, so not a branch for this format type.
2654
raise errors.NotBranchError(path=transport.base)
2656
# Decline to open it if the server doesn't support our required
2657
# version (3) so that the VFS-based transport will do it.
2658
if medium.should_probe():
2660
server_version = medium.protocol_version()
2661
except errors.SmartProtocolError:
2662
# Apparently there's no usable smart server there, even though
2663
# the medium supports the smart protocol.
2664
raise errors.NotBranchError(path=transport.base)
2665
if server_version != '2':
2666
raise errors.NotBranchError(path=transport.base)
2669
def initialize_on_transport(self, transport):
2671
# hand off the request to the smart server
2672
client_medium = transport.get_smart_medium()
2673
except errors.NoSmartMedium:
2674
# TODO: lookup the local format from a server hint.
2675
local_dir_format = BzrDirMetaFormat1()
2676
return local_dir_format.initialize_on_transport(transport)
2677
client = _SmartClient(client_medium)
2678
path = client.remote_path_from_transport(transport)
2679
response = client.call('BzrDirFormat.initialize', path)
2680
if response[0] != 'ok':
2681
raise errors.SmartProtocolError('unexpected response code %s' % (response,))
2682
return remote.RemoteBzrDir(transport)
2684
def _open(self, transport):
2685
return remote.RemoteBzrDir(transport)
2687
def __eq__(self, other):
2688
if not isinstance(other, RemoteBzrDirFormat):
2690
return self.get_format_description() == other.get_format_description()
2693
def repository_format(self):
2694
# Using a property to avoid early loading of remote
2695
return remote.RemoteRepositoryFormat()
2698
BzrDirFormat.register_control_server_format(RemoteBzrDirFormat)
2701
class BzrDirFormatInfo(object):
2703
def __init__(self, native, deprecated, hidden, experimental):
2704
self.deprecated = deprecated
2705
self.native = native
2706
self.hidden = hidden
2707
self.experimental = experimental
2710
class BzrDirFormatRegistry(registry.Registry):
2711
"""Registry of user-selectable BzrDir subformats.
2713
Differs from BzrDirFormat._control_formats in that it provides sub-formats,
2714
e.g. BzrDirMeta1 with weave repository. Also, it's more user-oriented.
2718
"""Create a BzrDirFormatRegistry."""
2719
self._aliases = set()
2720
self._registration_order = list()
2721
super(BzrDirFormatRegistry, self).__init__()
2724
"""Return a set of the format names which are aliases."""
2725
return frozenset(self._aliases)
2727
def register_metadir(self, key,
2728
repository_format, help, native=True, deprecated=False,
2734
"""Register a metadir subformat.
2736
These all use a BzrDirMetaFormat1 bzrdir, but can be parameterized
2737
by the Repository format.
2739
:param repository_format: The fully-qualified repository format class
2741
:param branch_format: Fully-qualified branch format class name as
2743
:param tree_format: Fully-qualified tree format class name as
2746
# This should be expanded to support setting WorkingTree and Branch
2747
# formats, once BzrDirMetaFormat1 supports that.
2748
def _load(full_name):
2749
mod_name, factory_name = full_name.rsplit('.', 1)
2751
mod = __import__(mod_name, globals(), locals(),
2753
except ImportError, e:
2754
raise ImportError('failed to load %s: %s' % (full_name, e))
2756
factory = getattr(mod, factory_name)
2757
except AttributeError:
2758
raise AttributeError('no factory %s in module %r'
2763
bd = BzrDirMetaFormat1()
2764
if branch_format is not None:
2765
bd.set_branch_format(_load(branch_format))
2766
if tree_format is not None:
2767
bd.workingtree_format = _load(tree_format)
2768
if repository_format is not None:
2769
bd.repository_format = _load(repository_format)
2771
self.register(key, helper, help, native, deprecated, hidden,
2772
experimental, alias)
2774
def register(self, key, factory, help, native=True, deprecated=False,
2775
hidden=False, experimental=False, alias=False):
2776
"""Register a BzrDirFormat factory.
2778
The factory must be a callable that takes one parameter: the key.
2779
It must produce an instance of the BzrDirFormat when called.
2781
This function mainly exists to prevent the info object from being
2784
registry.Registry.register(self, key, factory, help,
2785
BzrDirFormatInfo(native, deprecated, hidden, experimental))
2787
self._aliases.add(key)
2788
self._registration_order.append(key)
2790
def register_lazy(self, key, module_name, member_name, help, native=True,
2791
deprecated=False, hidden=False, experimental=False, alias=False):
2792
registry.Registry.register_lazy(self, key, module_name, member_name,
2793
help, BzrDirFormatInfo(native, deprecated, hidden, experimental))
2795
self._aliases.add(key)
2796
self._registration_order.append(key)
2798
def set_default(self, key):
2799
"""Set the 'default' key to be a clone of the supplied key.
2801
This method must be called once and only once.
2803
registry.Registry.register(self, 'default', self.get(key),
2804
self.get_help(key), info=self.get_info(key))
2805
self._aliases.add('default')
2807
def set_default_repository(self, key):
2808
"""Set the FormatRegistry default and Repository default.
2810
This is a transitional method while Repository.set_default_format
2813
if 'default' in self:
2814
self.remove('default')
2815
self.set_default(key)
2816
format = self.get('default')()
2818
def make_bzrdir(self, key):
2819
return self.get(key)()
2821
def help_topic(self, topic):
2823
default_realkey = None
2824
default_help = self.get_help('default')
2826
for key in self._registration_order:
2827
if key == 'default':
2829
help = self.get_help(key)
2830
if help == default_help:
2831
default_realkey = key
2833
help_pairs.append((key, help))
2835
def wrapped(key, help, info):
2837
help = '(native) ' + help
2838
return ':%s:\n%s\n\n' % (key,
2839
textwrap.fill(help, initial_indent=' ',
2840
subsequent_indent=' '))
2841
if default_realkey is not None:
2842
output += wrapped(default_realkey, '(default) %s' % default_help,
2843
self.get_info('default'))
2844
deprecated_pairs = []
2845
experimental_pairs = []
2846
for key, help in help_pairs:
2847
info = self.get_info(key)
2850
elif info.deprecated:
2851
deprecated_pairs.append((key, help))
2852
elif info.experimental:
2853
experimental_pairs.append((key, help))
2855
output += wrapped(key, help, info)
2856
output += "\nSee ``bzr help formats`` for more about storage formats."
2858
if len(experimental_pairs) > 0:
2859
other_output += "Experimental formats are shown below.\n\n"
2860
for key, help in experimental_pairs:
2861
info = self.get_info(key)
2862
other_output += wrapped(key, help, info)
2865
"No experimental formats are available.\n\n"
2866
if len(deprecated_pairs) > 0:
2867
other_output += "\nDeprecated formats are shown below.\n\n"
2868
for key, help in deprecated_pairs:
2869
info = self.get_info(key)
2870
other_output += wrapped(key, help, info)
2873
"\nNo deprecated formats are available.\n\n"
2875
"\nSee ``bzr help formats`` for more about storage formats."
2877
if topic == 'other-formats':
2883
class RepositoryAcquisitionPolicy(object):
2884
"""Abstract base class for repository acquisition policies.
2886
A repository acquisition policy decides how a BzrDir acquires a repository
2887
for a branch that is being created. The most basic policy decision is
2888
whether to create a new repository or use an existing one.
2890
def __init__(self, stack_on, stack_on_pwd, require_stacking):
2893
:param stack_on: A location to stack on
2894
:param stack_on_pwd: If stack_on is relative, the location it is
2896
:param require_stacking: If True, it is a failure to not stack.
2898
self._stack_on = stack_on
2899
self._stack_on_pwd = stack_on_pwd
2900
self._require_stacking = require_stacking
2902
def configure_branch(self, branch):
2903
"""Apply any configuration data from this policy to the branch.
2905
Default implementation sets repository stacking.
2907
if self._stack_on is None:
2909
if self._stack_on_pwd is None:
2910
stack_on = self._stack_on
2913
stack_on = urlutils.rebase_url(self._stack_on,
2915
branch.bzrdir.root_transport.base)
2916
except errors.InvalidRebaseURLs:
2917
stack_on = self._get_full_stack_on()
2919
branch.set_stacked_on_url(stack_on)
2920
except errors.UnstackableBranchFormat:
2921
if self._require_stacking:
2924
def _get_full_stack_on(self):
2925
"""Get a fully-qualified URL for the stack_on location."""
2926
if self._stack_on is None:
2928
if self._stack_on_pwd is None:
2929
return self._stack_on
2931
return urlutils.join(self._stack_on_pwd, self._stack_on)
2933
def _add_fallback(self, repository, possible_transports=None):
2934
"""Add a fallback to the supplied repository, if stacking is set."""
2935
stack_on = self._get_full_stack_on()
2936
if stack_on is None:
2938
stacked_dir = BzrDir.open(stack_on,
2939
possible_transports=possible_transports)
2941
stacked_repo = stacked_dir.open_branch().repository
2942
except errors.NotBranchError:
2943
stacked_repo = stacked_dir.open_repository()
2945
repository.add_fallback_repository(stacked_repo)
2946
except errors.UnstackableRepositoryFormat:
2947
if self._require_stacking:
2950
self._require_stacking = True
2952
def acquire_repository(self, make_working_trees=None, shared=False):
2953
"""Acquire a repository for this bzrdir.
2955
Implementations may create a new repository or use a pre-exising
2957
:param make_working_trees: If creating a repository, set
2958
make_working_trees to this value (if non-None)
2959
:param shared: If creating a repository, make it shared if True
2960
:return: A repository
2962
raise NotImplemented(RepositoryAcquisitionPolicy.acquire_repository)
2965
class CreateRepository(RepositoryAcquisitionPolicy):
2966
"""A policy of creating a new repository"""
2968
def __init__(self, bzrdir, stack_on=None, stack_on_pwd=None,
2969
require_stacking=False):
2972
:param bzrdir: The bzrdir to create the repository on.
2973
:param stack_on: A location to stack on
2974
:param stack_on_pwd: If stack_on is relative, the location it is
2977
RepositoryAcquisitionPolicy.__init__(self, stack_on, stack_on_pwd,
2979
self._bzrdir = bzrdir
2981
def acquire_repository(self, make_working_trees=None, shared=False):
2982
"""Implementation of RepositoryAcquisitionPolicy.acquire_repository
2984
Creates the desired repository in the bzrdir we already have.
2986
repository = self._bzrdir.create_repository(shared=shared)
2987
self._add_fallback(repository,
2988
possible_transports=[self._bzrdir.transport])
2989
if make_working_trees is not None:
2990
repository.set_make_working_trees(make_working_trees)
2994
class UseExistingRepository(RepositoryAcquisitionPolicy):
2995
"""A policy of reusing an existing repository"""
2997
def __init__(self, repository, stack_on=None, stack_on_pwd=None,
2998
require_stacking=False):
3001
:param repository: The repository to use.
3002
:param stack_on: A location to stack on
3003
:param stack_on_pwd: If stack_on is relative, the location it is
3006
RepositoryAcquisitionPolicy.__init__(self, stack_on, stack_on_pwd,
3008
self._repository = repository
3010
def acquire_repository(self, make_working_trees=None, shared=False):
3011
"""Implementation of RepositoryAcquisitionPolicy.acquire_repository
3013
Returns an existing repository to use
3015
self._add_fallback(self._repository,
3016
possible_transports=[self._repository.bzrdir.transport])
3017
return self._repository
3020
# Please register new formats after old formats so that formats
3021
# appear in chronological order and format descriptions can build
3023
format_registry = BzrDirFormatRegistry()
3024
format_registry.register('weave', BzrDirFormat6,
3025
'Pre-0.8 format. Slower than knit and does not'
3026
' support checkouts or shared repositories.',
3028
format_registry.register_metadir('metaweave',
3029
'bzrlib.repofmt.weaverepo.RepositoryFormat7',
3030
'Transitional format in 0.8. Slower than knit.',
3031
branch_format='bzrlib.branch.BzrBranchFormat5',
3032
tree_format='bzrlib.workingtree.WorkingTreeFormat3',
3034
format_registry.register_metadir('knit',
3035
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
3036
'Format using knits. Recommended for interoperation with bzr <= 0.14.',
3037
branch_format='bzrlib.branch.BzrBranchFormat5',
3038
tree_format='bzrlib.workingtree.WorkingTreeFormat3',
3040
format_registry.register_metadir('dirstate',
3041
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
3042
help='New in 0.15: Fast local operations. Compatible with bzr 0.8 and '
3043
'above when accessed over the network.',
3044
branch_format='bzrlib.branch.BzrBranchFormat5',
3045
# this uses bzrlib.workingtree.WorkingTreeFormat4 because importing
3046
# directly from workingtree_4 triggers a circular import.
3047
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3049
format_registry.register_metadir('dirstate-tags',
3050
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
3051
help='New in 0.15: Fast local operations and improved scaling for '
3052
'network operations. Additionally adds support for tags.'
3053
' Incompatible with bzr < 0.15.',
3054
branch_format='bzrlib.branch.BzrBranchFormat6',
3055
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3057
format_registry.register_metadir('rich-root',
3058
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit4',
3059
help='New in 1.0. Better handling of tree roots. Incompatible with'
3061
branch_format='bzrlib.branch.BzrBranchFormat6',
3062
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3064
format_registry.register_metadir('dirstate-with-subtree',
3065
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
3066
help='New in 0.15: Fast local operations and improved scaling for '
3067
'network operations. Additionally adds support for versioning nested '
3068
'bzr branches. Incompatible with bzr < 0.15.',
3069
branch_format='bzrlib.branch.BzrBranchFormat6',
3070
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3074
format_registry.register_metadir('pack-0.92',
3075
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack1',
3076
help='New in 0.92: Pack-based format with data compatible with '
3077
'dirstate-tags format repositories. Interoperates with '
3078
'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
3079
'Previously called knitpack-experimental. '
3080
'For more information, see '
3081
'http://doc.bazaar-vcs.org/latest/developers/packrepo.html.',
3082
branch_format='bzrlib.branch.BzrBranchFormat6',
3083
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3085
format_registry.register_metadir('pack-0.92-subtree',
3086
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack3',
3087
help='New in 0.92: Pack-based format with data compatible with '
3088
'dirstate-with-subtree format repositories. Interoperates with '
3089
'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
3090
'Previously called knitpack-experimental. '
3091
'For more information, see '
3092
'http://doc.bazaar-vcs.org/latest/developers/packrepo.html.',
3093
branch_format='bzrlib.branch.BzrBranchFormat6',
3094
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3098
format_registry.register_metadir('rich-root-pack',
3099
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack4',
3100
help='New in 1.0: A variant of pack-0.92 that supports rich-root data '
3101
'(needed for bzr-svn).',
3102
branch_format='bzrlib.branch.BzrBranchFormat6',
3103
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3105
format_registry.register_metadir('1.6',
3106
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack5',
3107
help='A format that allows a branch to indicate that there is another '
3108
'(stacked) repository that should be used to access data that is '
3109
'not present locally.',
3110
branch_format='bzrlib.branch.BzrBranchFormat7',
3111
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3113
format_registry.register_metadir('1.6.1-rich-root',
3114
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack5RichRoot',
3115
help='A variant of 1.6 that supports rich-root data '
3116
'(needed for bzr-svn).',
3117
branch_format='bzrlib.branch.BzrBranchFormat7',
3118
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3120
format_registry.register_metadir('1.9',
3121
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6',
3122
help='A repository format using B+tree indexes. These indexes '
3123
'are smaller in size, have smarter caching and provide faster '
3124
'performance for most operations.',
3125
branch_format='bzrlib.branch.BzrBranchFormat7',
3126
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3128
format_registry.register_metadir('1.9-rich-root',
3129
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6RichRoot',
3130
help='A variant of 1.9 that supports rich-root data '
3131
'(needed for bzr-svn).',
3132
branch_format='bzrlib.branch.BzrBranchFormat7',
3133
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3135
format_registry.register_metadir('development-wt5',
3136
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6',
3137
help='A working-tree format that supports views and content filtering.',
3138
branch_format='bzrlib.branch.BzrBranchFormat7',
3139
tree_format='bzrlib.workingtree.WorkingTreeFormat5',
3142
format_registry.register_metadir('development-wt5-rich-root',
3143
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6RichRoot',
3144
help='A variant of development-wt5 that supports rich-root data '
3145
'(needed for bzr-svn).',
3146
branch_format='bzrlib.branch.BzrBranchFormat7',
3147
tree_format='bzrlib.workingtree.WorkingTreeFormat5',
3150
# The following two formats should always just be aliases.
3151
format_registry.register_metadir('development',
3152
'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment2',
3153
help='Current development format. Can convert data to and from pack-0.92 '
3154
'(and anything compatible with pack-0.92) format repositories. '
3155
'Repositories and branches in this format can only be read by bzr.dev. '
3157
'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3159
branch_format='bzrlib.branch.BzrBranchFormat7',
3160
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3164
format_registry.register_metadir('development-subtree',
3165
'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment2Subtree',
3166
help='Current development format, subtree variant. Can convert data to and '
3167
'from pack-0.92-subtree (and anything compatible with '
3168
'pack-0.92-subtree) format repositories. Repositories and branches in '
3169
'this format can only be read by bzr.dev. Please read '
3170
'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3172
branch_format='bzrlib.branch.BzrBranchFormat7',
3173
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3177
# And the development formats above will have aliased one of the following:
3178
format_registry.register_metadir('development2',
3179
'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment2',
3180
help='1.6.1 with B+Tree based index. '
3182
'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3184
branch_format='bzrlib.branch.BzrBranchFormat7',
3185
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3189
format_registry.register_metadir('development2-subtree',
3190
'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment2Subtree',
3191
help='1.6.1-subtree with B+Tree based index. '
3193
'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3195
branch_format='bzrlib.branch.BzrBranchFormat7',
3196
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3200
# The current format that is made on 'bzr init'.
3201
format_registry.set_default('pack-0.92')