130
108
source_repo_format.check_conversion_target(target_repo_format)
133
def _check_supported(format, allow_unsupported,
134
recommend_upgrade=True,
136
"""Give an error or warning on old formats.
138
:param format: may be any kind of format - workingtree, branch,
141
:param allow_unsupported: If true, allow opening
142
formats that are strongly deprecated, and which may
143
have limited functionality.
145
:param recommend_upgrade: If true (default), warn
146
the user through the ui object that they may wish
147
to upgrade the object.
111
def _check_supported(format, allow_unsupported):
112
"""Check whether format is a supported format.
114
If allow_unsupported is True, this is a no-op.
149
# TODO: perhaps move this into a base Format class; it's not BzrDir
150
# specific. mbp 20070323
151
116
if not allow_unsupported and not format.is_supported():
152
117
# see open_downlevel to open legacy branches.
153
118
raise errors.UnsupportedFormatError(format=format)
154
if recommend_upgrade \
155
and getattr(format, 'upgrade_recommended', False):
156
ui.ui_factory.recommend_upgrade(
157
format.get_format_description(),
160
def clone(self, url, revision_id=None, force_new_repo=False,
161
preserve_stacking=False):
120
def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
162
121
"""Clone this bzrdir and its contents to url verbatim.
164
:param url: The url create the clone at. If url's last component does
165
not exist, it will be created.
166
:param revision_id: The tip revision-id to use for any branch or
167
working tree. If not None, then the clone operation may tune
168
itself to download less data.
169
:param force_new_repo: Do not use a shared repository for the target
170
even if one is available.
171
:param preserve_stacking: When cloning a stacked branch, stack the
172
new branch on top of the other branch's stacked-on branch.
174
return self.clone_on_transport(get_transport(url),
175
revision_id=revision_id,
176
force_new_repo=force_new_repo,
177
preserve_stacking=preserve_stacking)
179
def clone_on_transport(self, transport, revision_id=None,
180
force_new_repo=False, preserve_stacking=False):
181
"""Clone this bzrdir and its contents to transport verbatim.
183
:param transport: The transport for the location to produce the clone
184
at. If the target directory does not exist, it will be created.
185
:param revision_id: The tip revision-id to use for any branch or
186
working tree. If not None, then the clone operation may tune
187
itself to download less data.
188
:param force_new_repo: Do not use a shared repository for the target,
189
even if one is available.
190
:param preserve_stacking: When cloning a stacked branch, stack the
191
new branch on top of the other branch's stacked-on branch.
193
transport.ensure_base()
194
result = self.cloning_metadir().initialize_on_transport(transport)
195
repository_policy = None
123
If urls last component does not exist, it will be created.
125
if revision_id is not None, then the clone operation may tune
126
itself to download less data.
127
:param force_new_repo: Do not use a shared repository for the target
128
even if one is available.
131
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
132
result = self._format.initialize(url)
198
134
local_repo = self.find_repository()
199
135
except errors.NoRepositoryPresent:
200
136
local_repo = None
202
local_branch = self.open_branch()
203
except errors.NotBranchError:
206
# enable fallbacks when branch is not a branch reference
207
if local_branch.repository.has_same_location(local_repo):
208
local_repo = local_branch.repository
209
if preserve_stacking:
211
stack_on = local_branch.get_stacked_on_url()
212
except (errors.UnstackableBranchFormat,
213
errors.UnstackableRepositoryFormat,
218
138
# may need to copy content in
219
repository_policy = result.determine_repository_policy(
220
force_new_repo, stack_on, self.root_transport.base)
221
make_working_trees = local_repo.make_working_trees()
222
result_repo = repository_policy.acquire_repository(
223
make_working_trees, local_repo.is_shared())
224
result_repo.fetch(local_repo, revision_id=revision_id)
140
result_repo = local_repo.clone(
142
revision_id=revision_id,
144
result_repo.set_make_working_trees(local_repo.make_working_trees())
147
result_repo = result.find_repository()
148
# fetch content this dir needs.
150
# XXX FIXME RBC 20060214 need tests for this when the basis
152
result_repo.fetch(basis_repo, revision_id=revision_id)
153
result_repo.fetch(local_repo, revision_id=revision_id)
154
except errors.NoRepositoryPresent:
155
# needed to make one anyway.
156
result_repo = local_repo.clone(
158
revision_id=revision_id,
160
result_repo.set_make_working_trees(local_repo.make_working_trees())
227
161
# 1 if there is a branch present
228
162
# 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():
165
self.open_branch().clone(result, revision_id=revision_id)
166
except errors.NotBranchError:
169
self.open_workingtree().clone(result, basis=basis_tree)
170
except (errors.NoWorkingTree, errors.NotLocalUrl):
174
def _get_basis_components(self, basis):
175
"""Retrieve the basis components that are available at basis."""
177
return None, None, None
179
basis_tree = basis.open_workingtree()
180
basis_branch = basis_tree.branch
181
basis_repo = basis_branch.repository
182
except (errors.NoWorkingTree, errors.NotLocalUrl):
236
self.open_workingtree().clone(result)
237
except (errors.NoWorkingTree, errors.NotLocalUrl):
185
basis_branch = basis.open_branch()
186
basis_repo = basis_branch.repository
187
except errors.NotBranchError:
190
basis_repo = basis.open_repository()
191
except errors.NoRepositoryPresent:
193
return basis_repo, basis_branch, basis_tree
241
195
# TODO: This should be given a Transport, and should chdir up; otherwise
242
196
# this will open a new connection.
243
197
def _make_tail(self, url):
244
t = get_transport(url)
198
head, tail = urlutils.split(url)
199
if tail and tail != '.':
200
t = get_transport(head)
203
except errors.FileExists:
206
# TODO: Should take a Transport
248
def create(cls, base, format=None, possible_transports=None):
208
def create(cls, base, format=None):
249
209
"""Create a new BzrDir at the url 'base'.
211
This will call the current default formats initialize with base
212
as the only parameter.
251
214
:param format: If supplied, the format of branch to create. If not
252
215
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.
256
217
if cls is not BzrDir:
257
218
raise AssertionError("BzrDir.create always creates the default"
258
219
" format, not one of %r" % cls)
259
t = get_transport(base, possible_transports)
220
head, tail = urlutils.split(base)
221
if tail and tail != '.':
222
t = get_transport(head)
225
except errors.FileExists:
261
227
if format is None:
262
228
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)
229
return format.initialize(safe_unicode(base))
342
231
def create_branch(self):
343
232
"""Create a branch in this BzrDir.
345
The bzrdir's format will control what branch format is created.
234
The bzrdirs format will control what branch format is created.
346
235
For more control see BranchFormatXX.create(a_bzrdir).
348
237
raise NotImplementedError(self.create_branch)
350
def destroy_branch(self):
351
"""Destroy the branch in this BzrDir"""
352
raise NotImplementedError(self.destroy_branch)
355
240
def create_branch_and_repo(base, force_new_repo=False, format=None):
356
241
"""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
243
This will use the current default BzrDirFormat, and use whatever
360
244
repository format that that uses via bzrdir.create_branch and
361
245
create_repository. If a shared repository is available that is used
366
250
:param base: The URL to create the branch at.
367
251
:param force_new_repo: If True a new repository is always created.
368
:param format: If supplied, the format of branch to create. If not
369
supplied, the default is used.
371
253
bzrdir = BzrDir.create(base, format)
372
254
bzrdir._find_or_create_repository(force_new_repo)
373
255
return bzrdir.create_branch()
375
def determine_repository_policy(self, force_new_repo=False, stack_on=None,
376
stack_on_pwd=None, require_stacking=False):
377
"""Return an object representing a policy to use.
379
This controls whether a new repository is created, or a shared
380
repository used instead.
382
If stack_on is supplied, will not seek a containing shared repo.
384
:param force_new_repo: If True, require a new repository to be created.
385
:param stack_on: If supplied, the location to stack on. If not
386
supplied, a default_stack_on location may be used.
387
:param stack_on_pwd: If stack_on is relative, the location it is
390
def repository_policy(found_bzrdir):
393
config = found_bzrdir.get_config()
395
if config is not None:
396
stack_on = config.get_default_stack_on()
397
if stack_on is not None:
398
stack_on_pwd = found_bzrdir.root_transport.base
400
note('Using default stacking branch %s at %s', stack_on,
402
# does it have a repository ?
404
repository = found_bzrdir.open_repository()
405
except errors.NoRepositoryPresent:
408
if ((found_bzrdir.root_transport.base !=
409
self.root_transport.base) and not repository.is_shared()):
416
return UseExistingRepository(repository, stack_on,
417
stack_on_pwd, require_stacking=require_stacking), True
419
return CreateRepository(self, stack_on, stack_on_pwd,
420
require_stacking=require_stacking), True
422
if not force_new_repo:
424
policy = self._find_containing(repository_policy)
425
if policy is not None:
429
return UseExistingRepository(self.open_repository(),
430
stack_on, stack_on_pwd,
431
require_stacking=require_stacking)
432
except errors.NoRepositoryPresent:
434
return CreateRepository(self, stack_on, stack_on_pwd,
435
require_stacking=require_stacking)
437
257
def _find_or_create_repository(self, force_new_repo):
438
258
"""Create a new repository if needed, returning the repository."""
439
policy = self.determine_repository_policy(force_new_repo)
440
return policy.acquire_repository()
260
return self.create_repository()
262
return self.find_repository()
263
except errors.NoRepositoryPresent:
264
return self.create_repository()
443
267
def create_branch_convenience(base, force_new_repo=False,
444
force_new_tree=None, format=None,
445
possible_transports=None):
268
force_new_tree=None, format=None):
446
269
"""Create a new BzrDir, Branch and Repository at the url 'base'.
448
271
This is a convenience function - it will use an existing repository
449
272
if possible, can be told explicitly whether to create a working tree or
452
This will use the current default BzrDirFormat unless one is
453
specified, and use whatever
275
This will use the current default BzrDirFormat, and use whatever
454
276
repository format that that uses via bzrdir.create_branch and
455
277
create_repository. If a shared repository is available that is used
456
278
preferentially. Whatever repository is used, its tree creation policy
560
367
raise NotImplementedError(self.destroy_workingtree_metadata)
562
def _find_containing(self, evaluate):
563
"""Find something in a containing control directory.
565
This method will scan containing control dirs, until it finds what
566
it is looking for, decides that it will never find it, or runs out
567
of containing control directories to check.
569
It is used to implement find_repository and
570
determine_repository_policy.
572
:param evaluate: A function returning (value, stop). If stop is True,
573
the value will be returned.
369
def find_repository(self):
370
"""Find the repository that should be used for a_bzrdir.
372
This does not require a branch as we use it to find the repo for
373
new branches as well as to hook existing branches up to their
377
return self.open_repository()
378
except errors.NoRepositoryPresent:
380
next_transport = self.root_transport.clone('..')
577
result, stop = evaluate(found_bzrdir)
580
next_transport = found_bzrdir.root_transport.clone('..')
581
if (found_bzrdir.root_transport.base == next_transport.base):
582
# top of the file system
584
382
# find the next containing bzrdir
586
384
found_bzrdir = BzrDir.open_containing_from_transport(
587
385
next_transport)[0]
588
386
except errors.NotBranchError:
591
def find_repository(self):
592
"""Find the repository that should be used.
594
This does not require a branch as we use it to find the repo for
595
new branches as well as to hook existing branches up to their
598
def usable_repository(found_bzrdir):
388
raise errors.NoRepositoryPresent(self)
599
389
# does it have a repository ?
601
391
repository = found_bzrdir.open_repository()
602
392
except errors.NoRepositoryPresent:
604
if found_bzrdir.root_transport.base == self.root_transport.base:
605
return repository, True
606
elif repository.is_shared():
607
return repository, True
393
next_transport = found_bzrdir.root_transport.clone('..')
394
if (found_bzrdir.root_transport.base == next_transport.base):
395
# top of the file system
399
if ((found_bzrdir.root_transport.base ==
400
self.root_transport.base) or repository.is_shared()):
611
found_repo = self._find_containing(usable_repository)
612
if found_repo is None:
613
raise errors.NoRepositoryPresent(self)
616
def get_branch_reference(self):
617
"""Return the referenced URL for the branch in this bzrdir.
619
:raises NotBranchError: If there is no Branch.
620
:return: The URL the branch in this bzrdir references if it is a
621
reference branch, or None for regular branches.
403
raise errors.NoRepositoryPresent(self)
404
raise errors.NoRepositoryPresent(self)
625
406
def get_branch_transport(self, branch_format):
626
407
"""Get the transport for use by branch format in this BzrDir.
897
569
relpath is the portion of the path that is contained by the branch.
899
571
bzrdir, relpath = klass.open_containing(location)
900
tree, branch = bzrdir._get_tree_branch()
573
tree = bzrdir.open_workingtree()
574
except (errors.NoWorkingTree, errors.NotLocalUrl):
576
branch = bzrdir.open_branch()
901
579
return tree, branch, relpath
904
def open_containing_tree_branch_or_repository(klass, location):
905
"""Return the working tree, branch and repo contained by a location.
907
Returns (tree, branch, repository, relpath).
908
If there is no tree containing the location, tree will be None.
909
If there is no branch containing the location, branch will be None.
910
If there is no repository containing the location, repository will be
912
relpath is the portion of the path that is contained by the innermost
915
If no tree, branch or repository is found, a NotBranchError is raised.
917
bzrdir, relpath = klass.open_containing(location)
919
tree, branch = bzrdir._get_tree_branch()
920
except errors.NotBranchError:
922
repo = bzrdir.find_repository()
923
return None, None, repo, relpath
924
except (errors.NoRepositoryPresent):
925
raise errors.NotBranchError(location)
926
return tree, branch, branch.repository, relpath
928
581
def open_repository(self, _unsupported=False):
929
582
"""Open the repository object at this BzrDir if one is present.
931
This will not follow the Branch object pointer - it's strictly a direct
584
This will not follow the Branch object pointer - its strictly a direct
932
585
open facility. Most client code should use open_branch().repository to
933
586
get at a repository.
935
:param _unsupported: a private parameter, not part of the api.
588
_unsupported is a private parameter, not part of the api.
936
589
TODO: static convenience version of this?
938
591
raise NotImplementedError(self.open_repository)
940
def open_workingtree(self, _unsupported=False,
941
recommend_upgrade=True, from_branch=None):
593
def open_workingtree(self, _unsupported=False):
942
594
"""Open the workingtree object at this BzrDir if one is present.
944
:param recommend_upgrade: Optional keyword parameter, when True (the
945
default), emit through the ui module a recommendation that the user
946
upgrade the working tree when the workingtree being opened is old
947
(but still fully supported).
948
:param from_branch: override bzrdir branch (for lightweight checkouts)
596
TODO: static convenience version of this?
950
598
raise NotImplementedError(self.open_workingtree)
973
621
workingtree and discards it, and that's somewhat expensive.)
976
self.open_workingtree(recommend_upgrade=False)
624
self.open_workingtree()
978
626
except errors.NoWorkingTree:
981
def _cloning_metadir(self):
982
"""Produce a metadir suitable for cloning with.
984
:returns: (destination_bzrdir_format, source_repository)
629
def cloning_metadir(self, basis=None):
630
"""Produce a metadir suitable for cloning with"""
631
def related_repository(bzrdir):
633
branch = bzrdir.open_branch()
634
return branch.repository
635
except errors.NotBranchError:
637
return bzrdir.open_repository()
986
638
result_format = self._format.__class__()
989
branch = self.open_branch()
990
source_repository = branch.repository
991
except errors.NotBranchError:
993
source_repository = self.open_repository()
641
source_repository = related_repository(self)
642
except errors.NoRepositoryPresent:
645
source_repository = related_repository(self)
646
result_format.repository_format = source_repository._format
994
647
except errors.NoRepositoryPresent:
995
source_repository = None
997
# XXX TODO: This isinstance is here because we have not implemented
998
# the fix recommended in bug # 103195 - to delegate this choice the
1000
repo_format = source_repository._format
1001
if not isinstance(repo_format, remote.RemoteRepositoryFormat):
1002
result_format.repository_format = repo_format
1004
# TODO: Couldn't we just probe for the format in these cases,
1005
# rather than opening the whole tree? It would be a little
1006
# faster. mbp 20070401
1007
tree = self.open_workingtree(recommend_upgrade=False)
1008
except (errors.NoWorkingTree, errors.NotLocalUrl):
1009
result_format.workingtree_format = None
1011
result_format.workingtree_format = tree._format.__class__()
1012
return result_format, source_repository
1014
def cloning_metadir(self):
1015
"""Produce a metadir suitable for cloning or sprouting with.
1017
These operations may produce workingtrees (yes, even though they're
1018
"cloning" something that doesn't have a tree), so a viable workingtree
1019
format must be selected.
1021
:returns: a BzrDirFormat with all component formats either set
1022
appropriately or set to None if that component should not be
1025
format, repository = self._cloning_metadir()
1026
if format._workingtree_format is None:
1027
if repository is None:
1029
tree_format = repository._format._matchingbzrdir.workingtree_format
1030
format.workingtree_format = tree_format.__class__()
1033
def checkout_metadir(self):
1034
return self.cloning_metadir()
1036
def sprout(self, url, revision_id=None, force_new_repo=False,
1037
recurse='down', possible_transports=None,
1038
accelerator_tree=None, hardlink=False, stacked=False):
651
def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
1039
652
"""Create a copy of this bzrdir prepared for use as a new line of
1042
If url's last component does not exist, it will be created.
655
If urls last component does not exist, it will be created.
1044
657
Attributes related to the identity of the source branch like
1045
658
branch nickname will be cleaned, a working tree is created
1049
662
if revision_id is not None, then the clone operation may tune
1050
663
itself to download less data.
1051
:param accelerator_tree: A tree which can be used for retrieving file
1052
contents more quickly than the revision tree, i.e. a workingtree.
1053
The revision tree will be used for cases where accelerator_tree's
1054
content is different.
1055
:param hardlink: If true, hard-link files from accelerator_tree,
1057
:param stacked: If true, create a stacked branch referring to the
1058
location of this control directory.
1060
target_transport = get_transport(url, possible_transports)
1061
target_transport.ensure_base()
1062
cloning_format = self.cloning_metadir()
1063
result = cloning_format.initialize_on_transport(target_transport)
666
cloning_format = self.cloning_metadir(basis)
667
result = cloning_format.initialize(url)
668
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
1065
670
source_branch = self.open_branch()
1066
671
source_repository = source_branch.repository
1068
stacked_branch_url = self.root_transport.base
1070
# if a stacked branch wasn't requested, we don't create one
1071
# even if the origin was stacked
1072
stacked_branch_url = None
1073
672
except errors.NotBranchError:
1074
673
source_branch = None
1076
675
source_repository = self.open_repository()
1077
676
except errors.NoRepositoryPresent:
1078
source_repository = None
1079
stacked_branch_url = None
1080
repository_policy = result.determine_repository_policy(
1081
force_new_repo, stacked_branch_url, require_stacking=stacked)
1082
result_repo = repository_policy.acquire_repository()
1083
if source_repository is not None:
1084
# XXX: Isn't this redundant with the copy_content_into used below
1085
# after creating the branch? -- mbp 20080724
1086
result_repo.fetch(source_repository, revision_id=revision_id)
1088
# Create/update the result branch
1090
or repository_policy._require_stacking
1091
or repository_policy._stack_on)
1092
and not result._format.get_branch_format().supports_stacking()):
1093
# force a branch that can support stacking
1094
from bzrlib.branch import BzrBranchFormat7
1095
format = BzrBranchFormat7()
1096
result_branch = format.initialize(result)
1097
mutter("using %r for stacking" % (format,))
1098
elif source_branch is None:
1099
# this is for sprouting a bzrdir without a branch; is that
1101
result_branch = result.create_branch()
677
# copy the entire basis one if there is one
678
# but there is no repository.
679
source_repository = basis_repo
1103
result_branch = source_branch._format.initialize(result)
1104
mutter("created new branch %r" % (result_branch,))
1105
repository_policy.configure_branch(result_branch)
684
result_repo = result.find_repository()
685
except errors.NoRepositoryPresent:
687
if source_repository is None and result_repo is not None:
689
elif source_repository is None and result_repo is None:
690
# no repo available, make a new one
691
result.create_repository()
692
elif source_repository is not None and result_repo is None:
693
# have source, and want to make a new target repo
694
# we don't clone the repo because that preserves attributes
695
# like is_shared(), and we have not yet implemented a
696
# repository sprout().
697
result_repo = result.create_repository()
698
if result_repo is not None:
699
# fetch needed content into target.
701
# XXX FIXME RBC 20060214 need tests for this when the basis
703
result_repo.fetch(basis_repo, revision_id=revision_id)
704
if source_repository is not None:
705
result_repo.fetch(source_repository, revision_id=revision_id)
1106
706
if source_branch is not None:
1107
# XXX: this duplicates Branch.sprout(); it probably belongs on an
1108
# InterBranch method? -- mbp 20080724
1109
source_branch.copy_content_into(result_branch,
1110
revision_id=revision_id)
1111
result_branch.set_parent(self.root_transport.base)
1113
# Create/update the result working tree
1114
if isinstance(target_transport, LocalTransport) and (
1115
result_repo is None or result_repo.make_working_trees()):
1116
wt = result.create_workingtree(accelerator_tree=accelerator_tree,
1120
if wt.path2id('') is None:
1122
wt.set_root_id(self.open_workingtree.get_root_id())
1123
except errors.NoWorkingTree:
707
source_branch.sprout(result, revision_id=revision_id)
1129
if recurse == 'down':
1131
basis = wt.basis_tree()
1133
subtrees = basis.iter_references()
1134
elif source_branch is not None:
1135
basis = source_branch.basis_tree()
1137
subtrees = basis.iter_references()
1142
for path, file_id in subtrees:
1143
target = urlutils.join(url, urlutils.escape(path))
1144
sublocation = source_branch.reference_parent(file_id, path)
1145
sublocation.bzrdir.sprout(target,
1146
basis.get_reference_revision(file_id, path),
1147
force_new_repo=force_new_repo, recurse=recurse,
1150
if basis is not None:
709
result.create_branch()
710
# TODO: jam 20060426 we probably need a test in here in the
711
# case that the newly sprouted branch is a remote one
712
if result_repo is None or result_repo.make_working_trees():
713
wt = result.create_workingtree()
714
if wt.inventory.root is None:
716
wt.set_root_id(self.open_workingtree.get_root_id())
717
except errors.NoWorkingTree:
1392
927
def create_branch(self):
1393
928
"""See BzrDir.create_branch."""
1394
return self._format.get_branch_format().initialize(self)
1396
def destroy_branch(self):
1397
"""See BzrDir.create_branch."""
1398
self.transport.delete_tree('branch')
929
from bzrlib.branch import BranchFormat
930
return BranchFormat.get_default_format().initialize(self)
1400
932
def create_repository(self, shared=False):
1401
933
"""See BzrDir.create_repository."""
1402
934
return self._format.repository_format.initialize(self, shared)
1404
def destroy_repository(self):
1405
"""See BzrDir.destroy_repository."""
1406
self.transport.delete_tree('repository')
1408
def create_workingtree(self, revision_id=None, from_branch=None,
1409
accelerator_tree=None, hardlink=False):
936
def create_workingtree(self, revision_id=None):
1410
937
"""See BzrDir.create_workingtree."""
1411
return self._format.workingtree_format.initialize(
1412
self, revision_id, from_branch=from_branch,
1413
accelerator_tree=accelerator_tree, hardlink=hardlink)
938
from bzrlib.workingtree import WorkingTreeFormat
939
return WorkingTreeFormat.get_default_format().initialize(self, revision_id)
1415
941
def destroy_workingtree(self):
1416
942
"""See BzrDir.destroy_workingtree."""
1417
wt = self.open_workingtree(recommend_upgrade=False)
943
wt = self.open_workingtree()
1418
944
repository = wt.branch.repository
1419
945
empty = repository.revision_tree(_mod_revision.NULL_REVISION)
1420
wt.revert(old_tree=empty)
946
wt.revert([], old_tree=empty)
1421
947
self.destroy_workingtree_metadata()
1423
949
def destroy_workingtree_metadata(self):
1424
950
self.transport.delete_tree('checkout')
1426
def find_branch_format(self):
1427
"""Find the branch 'format' for this bzrdir.
1429
This might be a synthetic object for e.g. RemoteBranch and SVN.
1431
from bzrlib.branch import BranchFormat
1432
return BranchFormat.find_format(self)
1434
952
def _get_mkdir_mode(self):
1435
953
"""Figure out the mode to use when creating a bzrdir subdir."""
1436
954
temp_control = lockable_files.LockableFiles(self.transport, '',
1437
955
lockable_files.TransportLock)
1438
956
return temp_control._dir_mode
1440
def get_branch_reference(self):
1441
"""See BzrDir.get_branch_reference()."""
1442
from bzrlib.branch import BranchFormat
1443
format = BranchFormat.find_format(self)
1444
return format.get_reference(self)
1446
958
def get_branch_transport(self, branch_format):
1447
959
"""See BzrDir.get_branch_transport()."""
1448
960
if branch_format is None:
2516
1970
self.pb.note('starting repository conversion')
2517
1971
converter = CopyConverter(self.target_format.repository_format)
2518
1972
converter.convert(repo, pb)
2520
branch = self.bzrdir.open_branch()
2521
except errors.NotBranchError:
2524
# TODO: conversions of Branch and Tree should be done by
2525
# InterXFormat lookups/some sort of registry.
2526
# Avoid circular imports
2527
from bzrlib import branch as _mod_branch
2528
old = branch._format.__class__
2529
new = self.target_format.get_branch_format().__class__
2531
if (old == _mod_branch.BzrBranchFormat5 and
2532
new in (_mod_branch.BzrBranchFormat6,
2533
_mod_branch.BzrBranchFormat7)):
2534
branch_converter = _mod_branch.Converter5to6()
2535
elif (old == _mod_branch.BzrBranchFormat6 and
2536
new == _mod_branch.BzrBranchFormat7):
2537
branch_converter = _mod_branch.Converter6to7()
2539
raise errors.BadConversionTarget("No converter", new)
2540
branch_converter.convert(branch)
2541
branch = self.bzrdir.open_branch()
2542
old = branch._format.__class__
2544
tree = self.bzrdir.open_workingtree(recommend_upgrade=False)
2545
except (errors.NoWorkingTree, errors.NotLocalUrl):
2548
# TODO: conversions of Branch and Tree should be done by
2549
# InterXFormat lookups
2550
if (isinstance(tree, workingtree.WorkingTree3) and
2551
not isinstance(tree, workingtree_4.WorkingTree4) and
2552
isinstance(self.target_format.workingtree_format,
2553
workingtree_4.WorkingTreeFormat4)):
2554
workingtree_4.Converter3to4().convert(tree)
2555
1973
return to_convert
2558
# This is not in remote.py because it's small, and needs to be registered.
2559
# Putting it in remote.py creates a circular import problem.
2560
# we can make it a lazy object if the control formats is turned into something
2562
class RemoteBzrDirFormat(BzrDirMetaFormat1):
2563
"""Format representing bzrdirs accessed via a smart server"""
2565
def get_format_description(self):
2566
return 'bzr remote bzrdir'
2569
def probe_transport(klass, transport):
2570
"""Return a RemoteBzrDirFormat object if it looks possible."""
2572
medium = transport.get_smart_medium()
2573
except (NotImplementedError, AttributeError,
2574
errors.TransportNotPossible, errors.NoSmartMedium,
2575
errors.SmartProtocolError):
2576
# no smart server, so not a branch for this format type.
2577
raise errors.NotBranchError(path=transport.base)
2579
# Decline to open it if the server doesn't support our required
2580
# version (3) so that the VFS-based transport will do it.
2581
if medium.should_probe():
2583
server_version = medium.protocol_version()
2584
except errors.SmartProtocolError:
2585
# Apparently there's no usable smart server there, even though
2586
# the medium supports the smart protocol.
2587
raise errors.NotBranchError(path=transport.base)
2588
if server_version != '2':
2589
raise errors.NotBranchError(path=transport.base)
2592
def initialize_on_transport(self, transport):
2594
# hand off the request to the smart server
2595
client_medium = transport.get_smart_medium()
2596
except errors.NoSmartMedium:
2597
# TODO: lookup the local format from a server hint.
2598
local_dir_format = BzrDirMetaFormat1()
2599
return local_dir_format.initialize_on_transport(transport)
2600
client = _SmartClient(client_medium)
2601
path = client.remote_path_from_transport(transport)
2602
response = client.call('BzrDirFormat.initialize', path)
2603
if response[0] != 'ok':
2604
raise errors.SmartProtocolError('unexpected response code %s' % (response,))
2605
return remote.RemoteBzrDir(transport)
2607
def _open(self, transport):
2608
return remote.RemoteBzrDir(transport)
2610
def __eq__(self, other):
2611
if not isinstance(other, RemoteBzrDirFormat):
2613
return self.get_format_description() == other.get_format_description()
2616
BzrDirFormat.register_control_server_format(RemoteBzrDirFormat)
2619
1976
class BzrDirFormatInfo(object):
2621
def __init__(self, native, deprecated, hidden, experimental):
1978
def __init__(self, native, deprecated):
2622
1979
self.deprecated = deprecated
2623
1980
self.native = native
2624
self.hidden = hidden
2625
self.experimental = experimental
2628
1983
class BzrDirFormatRegistry(registry.Registry):
2632
1987
e.g. BzrDirMeta1 with weave repository. Also, it's more user-oriented.
2636
"""Create a BzrDirFormatRegistry."""
2637
self._aliases = set()
2638
super(BzrDirFormatRegistry, self).__init__()
2641
"""Return a set of the format names which are aliases."""
2642
return frozenset(self._aliases)
2644
def register_metadir(self, key,
2645
repository_format, help, native=True, deprecated=False,
1990
def register_metadir(self, key, repo, help, native=True, deprecated=False):
2651
1991
"""Register a metadir subformat.
2653
These all use a BzrDirMetaFormat1 bzrdir, but can be parameterized
2654
by the Repository format.
2656
:param repository_format: The fully-qualified repository format class
2658
:param branch_format: Fully-qualified branch format class name as
2660
:param tree_format: Fully-qualified tree format class name as
1993
repo is the repository format name as a string.
2663
1995
# This should be expanded to support setting WorkingTree and Branch
2664
1996
# formats, once BzrDirMetaFormat1 supports that.
2665
def _load(full_name):
2666
mod_name, factory_name = full_name.rsplit('.', 1)
2668
mod = __import__(mod_name, globals(), locals(),
2670
except ImportError, e:
2671
raise ImportError('failed to load %s: %s' % (full_name, e))
2673
factory = getattr(mod, factory_name)
2674
except AttributeError:
2675
raise AttributeError('no factory %s in module %r'
1998
import bzrlib.repository
1999
repo_format = getattr(bzrlib.repository, repo)
2680
2000
bd = BzrDirMetaFormat1()
2681
if branch_format is not None:
2682
bd.set_branch_format(_load(branch_format))
2683
if tree_format is not None:
2684
bd.workingtree_format = _load(tree_format)
2685
if repository_format is not None:
2686
bd.repository_format = _load(repository_format)
2001
bd.repository_format = repo_format()
2688
self.register(key, helper, help, native, deprecated, hidden,
2689
experimental, alias)
2003
self.register(key, helper, help, native, deprecated)
2691
def register(self, key, factory, help, native=True, deprecated=False,
2692
hidden=False, experimental=False, alias=False):
2005
def register(self, key, factory, help, native=True, deprecated=False):
2693
2006
"""Register a BzrDirFormat factory.
2695
2008
The factory must be a callable that takes one parameter: the key.
2789
class RepositoryAcquisitionPolicy(object):
2790
"""Abstract base class for repository acquisition policies.
2792
A repository acquisition policy decides how a BzrDir acquires a repository
2793
for a branch that is being created. The most basic policy decision is
2794
whether to create a new repository or use an existing one.
2796
def __init__(self, stack_on, stack_on_pwd, require_stacking):
2799
:param stack_on: A location to stack on
2800
:param stack_on_pwd: If stack_on is relative, the location it is
2802
:param require_stacking: If True, it is a failure to not stack.
2804
self._stack_on = stack_on
2805
self._stack_on_pwd = stack_on_pwd
2806
self._require_stacking = require_stacking
2808
def configure_branch(self, branch):
2809
"""Apply any configuration data from this policy to the branch.
2811
Default implementation sets repository stacking.
2813
if self._stack_on is None:
2815
if self._stack_on_pwd is None:
2816
stack_on = self._stack_on
2819
stack_on = urlutils.rebase_url(self._stack_on,
2821
branch.bzrdir.root_transport.base)
2822
except errors.InvalidRebaseURLs:
2823
stack_on = self._get_full_stack_on()
2825
branch.set_stacked_on_url(stack_on)
2826
except errors.UnstackableBranchFormat:
2827
if self._require_stacking:
2830
def _get_full_stack_on(self):
2831
"""Get a fully-qualified URL for the stack_on location."""
2832
if self._stack_on is None:
2834
if self._stack_on_pwd is None:
2835
return self._stack_on
2837
return urlutils.join(self._stack_on_pwd, self._stack_on)
2839
def _add_fallback(self, repository):
2840
"""Add a fallback to the supplied repository, if stacking is set."""
2841
stack_on = self._get_full_stack_on()
2842
if stack_on is None:
2844
stacked_dir = BzrDir.open(stack_on)
2846
stacked_repo = stacked_dir.open_branch().repository
2847
except errors.NotBranchError:
2848
stacked_repo = stacked_dir.open_repository()
2850
repository.add_fallback_repository(stacked_repo)
2851
except errors.UnstackableRepositoryFormat:
2852
if self._require_stacking:
2855
def acquire_repository(self, make_working_trees=None, shared=False):
2856
"""Acquire a repository for this bzrdir.
2858
Implementations may create a new repository or use a pre-exising
2860
:param make_working_trees: If creating a repository, set
2861
make_working_trees to this value (if non-None)
2862
:param shared: If creating a repository, make it shared if True
2863
:return: A repository
2865
raise NotImplemented(RepositoryAcquisitionPolicy.acquire_repository)
2868
class CreateRepository(RepositoryAcquisitionPolicy):
2869
"""A policy of creating a new repository"""
2871
def __init__(self, bzrdir, stack_on=None, stack_on_pwd=None,
2872
require_stacking=False):
2875
:param bzrdir: The bzrdir to create the repository on.
2876
:param stack_on: A location to stack on
2877
:param stack_on_pwd: If stack_on is relative, the location it is
2880
RepositoryAcquisitionPolicy.__init__(self, stack_on, stack_on_pwd,
2882
self._bzrdir = bzrdir
2884
def acquire_repository(self, make_working_trees=None, shared=False):
2885
"""Implementation of RepositoryAcquisitionPolicy.acquire_repository
2887
Creates the desired repository in the bzrdir we already have.
2889
if self._stack_on or self._require_stacking:
2890
# we may be coming from a format that doesn't support stacking,
2891
# but we require it in the destination, so force creation of a new
2894
# TODO: perhaps this should be treated as a distinct repository
2895
# acquisition policy?
2896
repository_format = self._bzrdir._format.repository_format
2897
if not repository_format.supports_external_lookups:
2898
# should possibly be controlled by the registry rather than
2900
from bzrlib.repofmt import pack_repo
2901
if repository_format.rich_root_data:
2902
repository_format = \
2903
pack_repo.RepositoryFormatKnitPack5RichRoot()
2905
repository_format = pack_repo.RepositoryFormatKnitPack5()
2906
note("using %r for stacking" % (repository_format,))
2907
repository = repository_format.initialize(self._bzrdir,
2911
repository = self._bzrdir.create_repository(shared=shared)
2912
self._add_fallback(repository)
2913
if make_working_trees is not None:
2914
repository.set_make_working_trees(make_working_trees)
2918
class UseExistingRepository(RepositoryAcquisitionPolicy):
2919
"""A policy of reusing an existing repository"""
2921
def __init__(self, repository, stack_on=None, stack_on_pwd=None,
2922
require_stacking=False):
2925
:param repository: The repository to use.
2926
:param stack_on: A location to stack on
2927
:param stack_on_pwd: If stack_on is relative, the location it is
2930
RepositoryAcquisitionPolicy.__init__(self, stack_on, stack_on_pwd,
2932
self._repository = repository
2934
def acquire_repository(self, make_working_trees=None, shared=False):
2935
"""Implementation of RepositoryAcquisitionPolicy.acquire_repository
2937
Returns an existing repository to use
2939
self._add_fallback(self._repository)
2940
return self._repository
2943
2089
format_registry = BzrDirFormatRegistry()
2944
2090
format_registry.register('weave', BzrDirFormat6,
2945
2091
'Pre-0.8 format. Slower than knit and does not'
2946
' support checkouts or shared repositories.',
2948
format_registry.register_metadir('knit',
2949
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2950
'Format using knits. Recommended for interoperation with bzr <= 0.14.',
2951
branch_format='bzrlib.branch.BzrBranchFormat5',
2952
tree_format='bzrlib.workingtree.WorkingTreeFormat3')
2953
format_registry.register_metadir('metaweave',
2954
'bzrlib.repofmt.weaverepo.RepositoryFormat7',
2092
' support checkouts or shared repositories.', deprecated=True)
2093
format_registry.register_metadir('knit', 'RepositoryFormatKnit1',
2094
'Format using knits. Recommended.')
2095
format_registry.set_default('knit')
2096
format_registry.register_metadir('metaweave', 'RepositoryFormat7',
2955
2097
'Transitional format in 0.8. Slower than knit.',
2956
branch_format='bzrlib.branch.BzrBranchFormat5',
2957
tree_format='bzrlib.workingtree.WorkingTreeFormat3',
2958
2098
deprecated=True)
2959
format_registry.register_metadir('dirstate',
2960
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2961
help='New in 0.15: Fast local operations. Compatible with bzr 0.8 and '
2962
'above when accessed over the network.',
2963
branch_format='bzrlib.branch.BzrBranchFormat5',
2964
# this uses bzrlib.workingtree.WorkingTreeFormat4 because importing
2965
# directly from workingtree_4 triggers a circular import.
2966
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2968
format_registry.register_metadir('dirstate-tags',
2969
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2970
help='New in 0.15: Fast local operations and improved scaling for '
2971
'network operations. Additionally adds support for tags.'
2972
' Incompatible with bzr < 0.15.',
2973
branch_format='bzrlib.branch.BzrBranchFormat6',
2974
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2976
format_registry.register_metadir('rich-root',
2977
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit4',
2978
help='New in 1.0. Better handling of tree roots. Incompatible with'
2980
branch_format='bzrlib.branch.BzrBranchFormat6',
2981
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2983
format_registry.register_metadir('dirstate-with-subtree',
2984
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
2985
help='New in 0.15: Fast local operations and improved scaling for '
2986
'network operations. Additionally adds support for versioning nested '
2987
'bzr branches. Incompatible with bzr < 0.15.',
2988
branch_format='bzrlib.branch.BzrBranchFormat6',
2989
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2993
format_registry.register_metadir('pack-0.92',
2994
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack1',
2995
help='New in 0.92: Pack-based format with data compatible with '
2996
'dirstate-tags format repositories. Interoperates with '
2997
'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
2998
'Previously called knitpack-experimental. '
2999
'For more information, see '
3000
'http://doc.bazaar-vcs.org/latest/developers/packrepo.html.',
3001
branch_format='bzrlib.branch.BzrBranchFormat6',
3002
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3004
format_registry.register_metadir('pack-0.92-subtree',
3005
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack3',
3006
help='New in 0.92: Pack-based format with data compatible with '
3007
'dirstate-with-subtree format repositories. Interoperates with '
3008
'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
3009
'Previously called knitpack-experimental. '
3010
'For more information, see '
3011
'http://doc.bazaar-vcs.org/latest/developers/packrepo.html.',
3012
branch_format='bzrlib.branch.BzrBranchFormat6',
3013
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3017
format_registry.register_metadir('rich-root-pack',
3018
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack4',
3019
help='New in 1.0: Pack-based format with data compatible with '
3020
'rich-root format repositories. Incompatible with'
3022
branch_format='bzrlib.branch.BzrBranchFormat6',
3023
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3025
format_registry.register_metadir('1.6',
3026
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack5',
3027
help='A branch and pack based repository that supports stacking. ',
3028
branch_format='bzrlib.branch.BzrBranchFormat7',
3029
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3031
format_registry.register_metadir('1.6-rich-root',
3032
'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack5RichRoot',
3033
help='A branch and pack based repository that supports stacking '
3034
'and rich root data (needed for bzr-svn). ',
3035
branch_format='bzrlib.branch.BzrBranchFormat7',
3036
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3038
# The following two formats should always just be aliases.
3039
format_registry.register_metadir('development',
3040
'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment1',
3041
help='Current development format. Can convert data to and from pack-0.92 '
3042
'(and anything compatible with pack-0.92) format repositories. '
3043
'Repositories and branches in this format can only be read by bzr.dev. '
3045
'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3047
branch_format='bzrlib.branch.BzrBranchFormat7',
3048
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3052
format_registry.register_metadir('development-subtree',
3053
'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment1Subtree',
3054
help='Current development format, subtree variant. Can convert data to and '
3055
'from pack-0.92-subtree (and anything compatible with '
3056
'pack-0.92-subtree) format repositories. Repositories and branches in '
3057
'this format can only be read by bzr.dev. Please read '
3058
'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3060
branch_format='bzrlib.branch.BzrBranchFormat7',
3061
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3065
# And the development formats which the will have aliased one of follow:
3066
format_registry.register_metadir('development0',
3067
'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment0',
3068
help='Trivial rename of pack-0.92 to provide a development format. '
3070
'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3072
branch_format='bzrlib.branch.BzrBranchFormat6',
3073
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3077
format_registry.register_metadir('development0-subtree',
3078
'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment0Subtree',
3079
help='Trivial rename of pack-0.92-subtree to provide a development format. '
3081
'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3083
branch_format='bzrlib.branch.BzrBranchFormat6',
3084
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3088
format_registry.register_metadir('development1',
3089
'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment1',
3090
help='A branch and pack based repository that supports stacking. '
3092
'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3094
branch_format='bzrlib.branch.BzrBranchFormat7',
3095
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3099
format_registry.register_metadir('development1-subtree',
3100
'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment1Subtree',
3101
help='A branch and pack based repository that supports stacking. '
3103
'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3105
branch_format='bzrlib.branch.BzrBranchFormat7',
3106
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3110
# The current format that is made on 'bzr init'.
3111
format_registry.set_default('pack-0.92')
2099
format_registry.register_metadir('experimental-knit2', 'RepositoryFormatKnit2',
2100
'Experimental successor to knit. Use at your own risk.')