371
371
bzrdir._find_or_create_repository(force_new_repo)
372
372
return bzrdir.create_branch()
374
def determine_repository_policy(self, force_new_repo=False, stack_on=None):
374
def determine_repository_policy(self, force_new_repo=False, stack_on=None,
375
376
"""Return an object representing a policy to use.
377
378
This controls whether a new repository is created, or a shared
380
381
def repository_policy(found_bzrdir):
382
384
config = found_bzrdir.get_config()
384
386
if config is not None:
385
387
stack_on = config.get_default_stack_on()
386
388
if stack_on is not None:
387
stack_on = urlutils.rebase_url(stack_on,
388
found_bzrdir.root_transport.base,
389
self.root_transport.base)
389
stack_on_pwd = found_bzrdir.root_transport.base
391
391
# does it have a repository ?
403
403
return None, False
405
return UseExistingRepository(repository, stack_on), True
405
return UseExistingRepository(repository, stack_on,
407
return CreateRepository(self, stack_on), True
408
return CreateRepository(self, stack_on, stack_on_pwd), True
409
410
if not force_new_repo:
410
411
if stack_on is None:
416
417
return UseExistingRepository(self.open_repository(),
418
stack_on, stack_on_pwd)
418
419
except errors.NoRepositoryPresent:
420
return CreateRepository(self, stack_on)
421
return CreateRepository(self, stack_on, stack_on_pwd)
422
423
def _find_or_create_repository(self, force_new_repo):
423
424
"""Create a new repository if needed, returning the repository."""
1033
1034
except errors.NoRepositoryPresent:
1034
1035
source_repository = None
1035
1036
stacked_branch_url = None
1036
repository_policy = result.determine_repository_policy(force_new_repo)
1037
repository_policy = result.determine_repository_policy(
1038
force_new_repo, stacked_branch_url)
1037
1039
result_repo = repository_policy.acquire_repository()
1038
if stacked_branch_url is not None:
1039
stacked_dir = BzrDir.open(stacked_branch_url)
1041
stacked_repo = stacked_dir.open_branch().repository
1042
except errors.NotBranchError:
1043
stacked_repo = stacked_dir.open_repository()
1044
result_repo.add_fallback_repository(stacked_repo)
1040
if source_repository is not None:
1045
1041
result_repo.fetch(source_repository, revision_id=revision_id)
1046
elif result_repo is None:
1047
# have source, and want to make a new target repo
1048
result_repo = source_repository.sprout(result,
1049
revision_id=revision_id)
1051
# Fetch needed content into target.
1052
# Would rather do it this way ...
1053
# source_repository.copy_content_into(result_repo,
1054
# revision_id=revision_id)
1055
# so we can override the copy method
1056
if source_repository is not None:
1057
result_repo.fetch(source_repository, revision_id=revision_id)
1059
1043
# Create/update the result branch
1060
1044
if source_branch is not None:
1064
1048
result_branch = result.create_branch()
1065
1049
repository_policy.configure_branch(result_branch)
1066
if stacked_branch_url is not None:
1067
result_branch.set_stacked_on(stacked_branch_url)
1069
1051
# Create/update the result working tree
1070
1052
if isinstance(target_transport, LocalTransport) and (
2738
2720
for a branch that is being created. The most basic policy decision is
2739
2721
whether to create a new repository or use an existing one.
2741
def __init__(self, stack_on):
2723
def __init__(self, stack_on, stack_on_pwd):
2742
2724
self._stack_on = stack_on
2725
self._stack_on_pwd = stack_on_pwd
2744
2727
def configure_branch(self, branch):
2745
2728
"""Apply any configuration data from this policy to the branch.
2747
2730
Default implementation sets repository stacking.
2750
branch.set_stacked_on(self._stack_on)
2732
if self._stack_on is not None:
2733
if self._stack_on_pwd is None:
2734
stack_on = self._stack_on
2736
stack_on = urlutils.rebase_url(self._stack_on,
2738
branch.bzrdir.root_transport.base)
2739
branch.set_stacked_on(stack_on)
2752
2741
def _add_fallback(self, repository):
2753
2742
if self._stack_on is None:
2775
2764
class CreateRepository(RepositoryAcquisitionPolicy):
2776
2765
"""A policy of creating a new repository"""
2778
def __init__(self, bzrdir, stack_on=None):
2779
RepositoryAcquisitionPolicy.__init__(self, stack_on)
2767
def __init__(self, bzrdir, stack_on=None, stack_on_pwd=None):
2768
RepositoryAcquisitionPolicy.__init__(self, stack_on, stack_on_pwd)
2780
2769
self._bzrdir = bzrdir
2782
2771
def acquire_repository(self, make_working_trees=None, shared=False):
2794
2783
class UseExistingRepository(RepositoryAcquisitionPolicy):
2795
2784
"""A policy of reusing an existing repository"""
2797
def __init__(self, repository, stack_on=None):
2798
RepositoryAcquisitionPolicy.__init__(self, stack_on)
2786
def __init__(self, repository, stack_on=None, stack_on_pwd=None):
2787
RepositoryAcquisitionPolicy.__init__(self, stack_on, stack_on_pwd)
2799
2788
self._repository = repository
2801
2790
def acquire_repository(self, make_working_trees=None, shared=False):