130
109
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.
112
def _check_supported(format, allow_unsupported):
113
"""Check whether format is a supported format.
115
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
117
if not allow_unsupported and not format.is_supported():
152
118
# see open_downlevel to open legacy branches.
153
119
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):
121
def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
162
122
"""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
124
If urls last component does not exist, it will be created.
126
if revision_id is not None, then the clone operation may tune
127
itself to download less data.
128
:param force_new_repo: Do not use a shared repository for the target
129
even if one is available.
132
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
133
result = self._format.initialize(url)
198
135
local_repo = self.find_repository()
199
136
except errors.NoRepositoryPresent:
200
137
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
139
# 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)
141
result_repo = local_repo.clone(
143
revision_id=revision_id,
145
result_repo.set_make_working_trees(local_repo.make_working_trees())
148
result_repo = result.find_repository()
149
# fetch content this dir needs.
151
# XXX FIXME RBC 20060214 need tests for this when the basis
153
result_repo.fetch(basis_repo, revision_id=revision_id)
154
result_repo.fetch(local_repo, revision_id=revision_id)
155
except errors.NoRepositoryPresent:
156
# needed to make one anyway.
157
result_repo = local_repo.clone(
159
revision_id=revision_id,
161
result_repo.set_make_working_trees(local_repo.make_working_trees())
227
162
# 1 if there is a branch present
228
163
# 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():
166
self.open_branch().clone(result, revision_id=revision_id)
167
except errors.NotBranchError:
170
self.open_workingtree().clone(result, basis=basis_tree)
171
except (errors.NoWorkingTree, errors.NotLocalUrl):
175
def _get_basis_components(self, basis):
176
"""Retrieve the basis components that are available at basis."""
178
return None, None, None
180
basis_tree = basis.open_workingtree()
181
basis_branch = basis_tree.branch
182
basis_repo = basis_branch.repository
183
except (errors.NoWorkingTree, errors.NotLocalUrl):
236
self.open_workingtree().clone(result)
237
except (errors.NoWorkingTree, errors.NotLocalUrl):
186
basis_branch = basis.open_branch()
187
basis_repo = basis_branch.repository
188
except errors.NotBranchError:
191
basis_repo = basis.open_repository()
192
except errors.NoRepositoryPresent:
194
return basis_repo, basis_branch, basis_tree
241
196
# TODO: This should be given a Transport, and should chdir up; otherwise
242
197
# this will open a new connection.
243
198
def _make_tail(self, url):
244
t = get_transport(url)
199
head, tail = urlutils.split(url)
200
if tail and tail != '.':
201
t = get_transport(head)
204
except errors.FileExists:
207
# TODO: Should take a Transport
248
def create(cls, base, format=None, possible_transports=None):
209
def create(cls, base, format=None):
249
210
"""Create a new BzrDir at the url 'base'.
212
This will call the current default formats initialize with base
213
as the only parameter.
251
215
:param format: If supplied, the format of branch to create. If not
252
216
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
218
if cls is not BzrDir:
257
219
raise AssertionError("BzrDir.create always creates the default"
258
220
" format, not one of %r" % cls)
259
t = get_transport(base, possible_transports)
221
head, tail = urlutils.split(base)
222
if tail and tail != '.':
223
t = get_transport(head)
226
except errors.FileExists:
261
228
if format is None:
262
229
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)
230
return format.initialize(safe_unicode(base))
342
232
def create_branch(self):
343
233
"""Create a branch in this BzrDir.
345
The bzrdir's format will control what branch format is created.
235
The bzrdirs format will control what branch format is created.
346
236
For more control see BranchFormatXX.create(a_bzrdir).
348
238
raise NotImplementedError(self.create_branch)
350
def destroy_branch(self):
351
"""Destroy the branch in this BzrDir"""
352
raise NotImplementedError(self.destroy_branch)
355
241
def create_branch_and_repo(base, force_new_repo=False, format=None):
356
242
"""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
244
This will use the current default BzrDirFormat, and use whatever
360
245
repository format that that uses via bzrdir.create_branch and
361
246
create_repository. If a shared repository is available that is used
366
251
:param base: The URL to create the branch at.
367
252
: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
254
bzrdir = BzrDir.create(base, format)
372
255
bzrdir._find_or_create_repository(force_new_repo)
373
256
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
258
def _find_or_create_repository(self, force_new_repo):
438
259
"""Create a new repository if needed, returning the repository."""
439
policy = self.determine_repository_policy(force_new_repo)
440
return policy.acquire_repository()
261
return self.create_repository()
263
return self.find_repository()
264
except errors.NoRepositoryPresent:
265
return self.create_repository()
443
268
def create_branch_convenience(base, force_new_repo=False,
444
force_new_tree=None, format=None,
445
possible_transports=None):
269
force_new_tree=None, format=None):
446
270
"""Create a new BzrDir, Branch and Repository at the url 'base'.
448
272
This is a convenience function - it will use an existing repository
449
273
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
276
This will use the current default BzrDirFormat, and use whatever
454
277
repository format that that uses via bzrdir.create_branch and
455
278
create_repository. If a shared repository is available that is used
456
279
preferentially. Whatever repository is used, its tree creation policy
560
387
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.
389
def find_repository(self):
390
"""Find the repository that should be used for a_bzrdir.
392
This does not require a branch as we use it to find the repo for
393
new branches as well as to hook existing branches up to their
397
return self.open_repository()
398
except errors.NoRepositoryPresent:
400
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
402
# find the next containing bzrdir
586
404
found_bzrdir = BzrDir.open_containing_from_transport(
587
405
next_transport)[0]
588
406
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):
408
raise errors.NoRepositoryPresent(self)
599
409
# does it have a repository ?
601
411
repository = found_bzrdir.open_repository()
602
412
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
413
next_transport = found_bzrdir.root_transport.clone('..')
414
if (found_bzrdir.root_transport.base == next_transport.base):
415
# top of the file system
419
if ((found_bzrdir.root_transport.base ==
420
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.
423
raise errors.NoRepositoryPresent(self)
424
raise errors.NoRepositoryPresent(self)
625
426
def get_branch_transport(self, branch_format):
626
427
"""Get the transport for use by branch format in this BzrDir.
755
511
return BzrDir.open(base, _unsupported=True)
758
def open(base, _unsupported=False, possible_transports=None):
759
"""Open an existing bzrdir, rooted at 'base' (url).
514
def open(base, _unsupported=False):
515
"""Open an existing bzrdir, rooted at 'base' (url)
761
:param _unsupported: a private parameter to the BzrDir class.
517
_unsupported is a private parameter to the BzrDir class.
763
t = get_transport(base, possible_transports=possible_transports)
519
t = get_transport(base)
764
520
return BzrDir.open_from_transport(t, _unsupported=_unsupported)
767
def open_from_transport(transport, _unsupported=False,
768
_server_formats=True):
523
def open_from_transport(transport, _unsupported=False):
769
524
"""Open a bzrdir within a particular directory.
771
526
:param transport: Transport containing the bzrdir.
772
527
:param _unsupported: private.
774
base = transport.base
776
def find_format(transport):
777
return transport, BzrDirFormat.find_format(
778
transport, _server_formats=_server_formats)
780
def redirected(transport, e, redirection_notice):
781
qualified_source = e.get_source_url()
782
relpath = transport.relpath(qualified_source)
783
if not e.target.endswith(relpath):
784
# Not redirected to a branch-format, not a branch
785
raise errors.NotBranchError(path=e.target)
786
target = e.target[:-len(relpath)]
787
note('%s is%s redirected to %s',
788
transport.base, e.permanently, target)
789
# Let's try with a new transport
790
# FIXME: If 'transport' has a qualifier, this should
791
# be applied again to the new transport *iff* the
792
# schemes used are the same. Uncomment this code
793
# once the function (and tests) exist.
795
#target = urlutils.copy_url_qualifiers(original, target)
796
return get_transport(target)
799
transport, format = do_catching_redirections(find_format,
802
except errors.TooManyRedirections:
803
raise errors.NotBranchError(base)
529
format = BzrDirFormat.find_format(transport)
805
530
BzrDir._check_supported(format, _unsupported)
806
531
return format.open(transport, _found=True)
897
589
relpath is the portion of the path that is contained by the branch.
899
591
bzrdir, relpath = klass.open_containing(location)
900
tree, branch = bzrdir._get_tree_branch()
593
tree = bzrdir.open_workingtree()
594
except (errors.NoWorkingTree, errors.NotLocalUrl):
596
branch = bzrdir.open_branch()
901
599
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
601
def open_repository(self, _unsupported=False):
929
602
"""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
604
This will not follow the Branch object pointer - its strictly a direct
932
605
open facility. Most client code should use open_branch().repository to
933
606
get at a repository.
935
:param _unsupported: a private parameter, not part of the api.
608
_unsupported is a private parameter, not part of the api.
936
609
TODO: static convenience version of this?
938
611
raise NotImplementedError(self.open_repository)
940
def open_workingtree(self, _unsupported=False,
941
recommend_upgrade=True, from_branch=None):
613
def open_workingtree(self, _unsupported=False):
942
614
"""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)
616
TODO: static convenience version of this?
950
618
raise NotImplementedError(self.open_workingtree)
973
641
workingtree and discards it, and that's somewhat expensive.)
976
self.open_workingtree(recommend_upgrade=False)
644
self.open_workingtree()
978
646
except errors.NoWorkingTree:
981
def _cloning_metadir(self):
982
"""Produce a metadir suitable for cloning with.
984
:returns: (destination_bzrdir_format, source_repository)
649
def _cloning_metadir(self, basis=None):
650
def related_repository(bzrdir):
652
branch = bzrdir.open_branch()
653
return branch.repository
654
except errors.NotBranchError:
656
return bzrdir.open_repository()
986
657
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()
660
source_repository = related_repository(self)
661
except errors.NoRepositoryPresent:
664
source_repository = related_repository(self)
665
result_format.repository_format = source_repository._format
994
666
except errors.NoRepositoryPresent:
995
667
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)
669
tree = self.open_workingtree()
1008
670
except (errors.NoWorkingTree, errors.NotLocalUrl):
1009
671
result_format.workingtree_format = None
1011
673
result_format.workingtree_format = tree._format.__class__()
1012
674
return result_format, source_repository
1014
def cloning_metadir(self):
676
def cloning_metadir(self, basis=None):
1015
677
"""Produce a metadir suitable for cloning or sprouting with.
1017
679
These operations may produce workingtrees (yes, even though they're
1018
"cloning" something that doesn't have a tree), so a viable workingtree
680
"cloning" something that doesn't have a tree, so a viable workingtree
1019
681
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
683
format, repository = self._cloning_metadir()
1026
684
if format._workingtree_format is None:
1049
706
if revision_id is not None, then the clone operation may tune
1050
707
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)
710
cloning_format = self.cloning_metadir(basis)
711
result = cloning_format.initialize(url)
712
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
1065
714
source_branch = self.open_branch()
1066
715
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
716
except errors.NotBranchError:
1074
717
source_branch = None
1076
719
source_repository = self.open_repository()
1077
720
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()
721
# copy the entire basis one if there is one
722
# but there is no repository.
723
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)
728
result_repo = result.find_repository()
729
except errors.NoRepositoryPresent:
731
if source_repository is None and result_repo is not None:
733
elif source_repository is None and result_repo is None:
734
# no repo available, make a new one
735
result.create_repository()
736
elif source_repository is not None and result_repo is None:
737
# have source, and want to make a new target repo
738
# we don't clone the repo because that preserves attributes
739
# like is_shared(), and we have not yet implemented a
740
# repository sprout().
741
result_repo = result.create_repository()
742
if result_repo is not None:
743
# fetch needed content into target.
745
# XXX FIXME RBC 20060214 need tests for this when the basis
747
result_repo.fetch(basis_repo, revision_id=revision_id)
748
if source_repository is not None:
749
result_repo.fetch(source_repository, revision_id=revision_id)
1106
750
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,
751
source_branch.sprout(result, revision_id=revision_id)
753
result.create_branch()
754
# TODO: jam 20060426 we probably need a test in here in the
755
# case that the newly sprouted branch is a remote one
756
if result_repo is None or result_repo.make_working_trees():
757
wt = result.create_workingtree()
1120
760
if wt.path2id('') is None:
1393
1002
"""See BzrDir.create_branch."""
1394
1003
return self._format.get_branch_format().initialize(self)
1396
def destroy_branch(self):
1397
"""See BzrDir.create_branch."""
1398
self.transport.delete_tree('branch')
1400
1005
def create_repository(self, shared=False):
1401
1006
"""See BzrDir.create_repository."""
1402
1007
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):
1009
def create_workingtree(self, revision_id=None):
1410
1010
"""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)
1011
from bzrlib.workingtree import WorkingTreeFormat
1012
return self._format.workingtree_format.initialize(self, revision_id)
1415
1014
def destroy_workingtree(self):
1416
1015
"""See BzrDir.destroy_workingtree."""
1417
wt = self.open_workingtree(recommend_upgrade=False)
1016
wt = self.open_workingtree()
1418
1017
repository = wt.branch.repository
1419
1018
empty = repository.revision_tree(_mod_revision.NULL_REVISION)
1420
wt.revert(old_tree=empty)
1019
wt.revert([], old_tree=empty)
1421
1020
self.destroy_workingtree_metadata()
1423
1022
def destroy_workingtree_metadata(self):
1424
1023
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
1025
def _get_mkdir_mode(self):
1435
1026
"""Figure out the mode to use when creating a bzrdir subdir."""
1436
1027
temp_control = lockable_files.LockableFiles(self.transport, '',
1437
1028
lockable_files.TransportLock)
1438
1029
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
1031
def get_branch_transport(self, branch_format):
1447
1032
"""See BzrDir.get_branch_transport()."""
1448
1033
if branch_format is None:
2555
2123
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
2126
class BzrDirFormatInfo(object):
2621
def __init__(self, native, deprecated, hidden, experimental):
2128
def __init__(self, native, deprecated, hidden):
2622
2129
self.deprecated = deprecated
2623
2130
self.native = native
2624
2131
self.hidden = hidden
2625
self.experimental = experimental
2628
2134
class BzrDirFormatRegistry(registry.Registry):
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
2272
format_registry = BzrDirFormatRegistry()
2944
2273
format_registry.register('weave', BzrDirFormat6,
2945
2274
'Pre-0.8 format. Slower than knit and does not'
2987
2309
'bzr branches. Incompatible with bzr < 0.15.',
2988
2310
branch_format='bzrlib.branch.BzrBranchFormat6',
2989
2311
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')
2314
format_registry.set_default('dirstate')