721
721
' directory exists, but does not already'
722
722
' have a control directory. This flag will'
723
723
' allow push to proceed.'),
725
help='Create a stacked branch that refers to another branch '
726
'for the commit history. Only the work not present in the '
727
'referenced branch is included in the branch created.',
730
help='Create a stacked branch that references the public location '
731
'of the parent branch. See --reference for more information.'),
725
733
takes_args = ['location?']
726
734
encoding_type = 'replace'
728
736
def run(self, location=None, remember=False, overwrite=False,
729
create_prefix=False, verbose=False, revision=None,
730
use_existing_dir=False,
732
# FIXME: Way too big! Put this into a function called from the
737
create_prefix=False, verbose=False, revision=None,
738
use_existing_dir=False, directory=None, reference=None, stacked=False):
739
from bzrlib.push import _show_push_branch
741
# Get the source branch and revision_id
734
742
if directory is None:
736
744
br_from = Branch.open_containing(directory)[0]
737
stored_loc = br_from.get_push_location()
739
if stored_loc is None:
740
raise errors.BzrCommandError("No push location known or specified.")
742
display_url = urlutils.unescape_for_display(stored_loc,
744
self.outf.write("Using saved location: %s\n" % display_url)
745
location = stored_loc
747
to_transport = transport.get_transport(location)
749
br_to = repository_to = dir_to = None
751
dir_to = bzrdir.BzrDir.open_from_transport(to_transport)
752
except errors.NotBranchError:
753
pass # Didn't find anything
755
# If we can open a branch, use its direct repository, otherwise see
756
# if there is a repository without a branch.
758
br_to = dir_to.open_branch()
759
except errors.NotBranchError:
760
# Didn't find a branch, can we find a repository?
762
repository_to = dir_to.find_repository()
763
except errors.NoRepositoryPresent:
766
# Found a branch, so we must have found a repository
767
repository_to = br_to.repository
769
745
if revision is not None:
770
746
if len(revision) == 1:
771
747
revision_id = revision[0].in_history(br_from).rev_id
776
752
revision_id = br_from.last_revision()
782
# The destination doesn't exist; create it.
783
# XXX: Refactor the create_prefix/no_create_prefix code into a
784
# common helper function
786
def make_directory(transport):
790
def redirected(redirected_transport, e, redirection_notice):
791
return transport.get_transport(e.get_target_url())
794
to_transport = transport.do_catching_redirections(
795
make_directory, to_transport, redirected)
796
except errors.FileExists:
797
if not use_existing_dir:
798
raise errors.BzrCommandError("Target directory %s"
799
" already exists, but does not have a valid .bzr"
800
" directory. Supply --use-existing-dir to push"
801
" there anyway." % location)
802
except errors.NoSuchFile:
803
if not create_prefix:
804
raise errors.BzrCommandError("Parent directory of %s"
806
"\nYou may supply --create-prefix to create all"
807
" leading parent directories."
809
_create_prefix(to_transport)
810
except errors.TooManyRedirections:
811
raise errors.BzrCommandError("Too many redirections trying "
812
"to make %s." % location)
814
# Now the target directory exists, but doesn't have a .bzr
815
# directory. So we need to create it, along with any work to create
816
# all of the dependent branches, etc.
817
dir_to = br_from.bzrdir.clone_on_transport(to_transport,
818
revision_id=revision_id)
819
br_to = dir_to.open_branch()
820
# TODO: Some more useful message about what was copied
821
note('Created new branch.')
822
# We successfully created the target, remember it
823
if br_from.get_push_location() is None or remember:
824
br_from.set_push_location(br_to.base)
825
elif repository_to is None:
826
# we have a bzrdir but no branch or repository
827
# XXX: Figure out what to do other than complain.
828
raise errors.BzrCommandError("At %s you have a valid .bzr control"
829
" directory, but not a branch or repository. This is an"
830
" unsupported configuration. Please move the target directory"
831
" out of the way and try again."
834
# We have a repository but no branch, copy the revisions, and then
836
repository_to.fetch(br_from.repository, revision_id=revision_id)
837
br_to = br_from.clone(dir_to, revision_id=revision_id)
838
note('Created new branch.')
839
if br_from.get_push_location() is None or remember:
840
br_from.set_push_location(br_to.base)
841
else: # We have a valid to branch
842
# We were able to connect to the remote location, so remember it
843
# we don't need to successfully push because of possible divergence.
844
if br_from.get_push_location() is None or remember:
845
br_from.set_push_location(br_to.base)
847
old_rh = br_to.revision_history()
850
tree_to = dir_to.open_workingtree()
851
except errors.NotLocalUrl:
852
warning("This transport does not update the working "
853
"tree of: %s. See 'bzr help working-trees' for "
854
"more information." % br_to.base)
855
push_result = br_from.push(br_to, overwrite,
856
stop_revision=revision_id)
857
except errors.NoWorkingTree:
858
push_result = br_from.push(br_to, overwrite,
859
stop_revision=revision_id)
863
push_result = br_from.push(tree_to.branch, overwrite,
864
stop_revision=revision_id)
868
except errors.DivergedBranches:
869
raise errors.BzrCommandError('These branches have diverged.'
870
' Try using "merge" and then "push".')
871
if push_result is not None:
872
push_result.report(self.outf)
874
new_rh = br_to.revision_history()
877
from bzrlib.log import show_changed_revisions
878
show_changed_revisions(br_to, old_rh, new_rh,
881
# we probably did a clone rather than a push, so a message was
754
# Get the reference branch, if any
755
if reference is not None:
756
reference = urlutils.normalize_url(reference)
759
parent_url = br_from.get_parent()
761
parent = Branch.open(parent_url)
762
reference = parent.get_public_branch()
764
# I considered excluding non-http url's here, thus forcing
765
# 'public' branches only, but that only works for some
766
# users, so it's best to just depend on the user spotting an
767
# error by the feedback given to them. RBC 20080227.
768
reference = parent_url
770
raise errors.BzrCommandError(
771
"Could not determine branch to refer to.")
773
# Get the destination location
775
stored_loc = br_from.get_push_location()
776
if stored_loc is None:
777
raise errors.BzrCommandError(
778
"No push location known or specified.")
780
display_url = urlutils.unescape_for_display(stored_loc,
782
self.outf.write("Using saved location: %s\n" % display_url)
783
location = stored_loc
785
_show_push_branch(br_from, revision_id, location, self.outf,
786
verbose=verbose, overwrite=overwrite, remember=remember,
787
reference=reference, create_prefix=create_prefix,
788
use_existing_dir=use_existing_dir)
886
791
class cmd_branch(Command):
939
849
dir = br_from.bzrdir.sprout(to_transport.base, revision_id,
940
850
possible_transports=[to_transport],
941
851
accelerator_tree=accelerator_tree,
852
hardlink=hardlink, stacked=stacked)
943
853
branch = dir.open_branch()
944
854
except errors.NoSuchRevision:
945
855
to_transport.delete_tree('.')
946
msg = "The branch %s has no revision %s." % (from_location, revision[0])
856
msg = "The branch %s has no revision %s." % (from_location,
947
858
raise errors.BzrCommandError(msg)
948
859
_merge_tags_if_possible(br_from, branch)
949
note('Branched %d revision(s).' % branch.revno())
860
# If the source branch is stacked, the new branch may
861
# be stacked whether we asked for that explicitly or not.
862
# We therefore need a try/except here and not just 'if stacked:'
864
note('Created new stacked branch referring to %s.' %
865
branch.get_stacked_on())
866
except (errors.NotStacked, errors.UnstackableBranchFormat,
867
errors.UnstackableRepositoryFormat), e:
868
note('Branched %d revision(s).' % branch.revno())