627
627
takes_options = ['remember', 'overwrite', 'verbose',
628
Option('create-prefix',
628
Option('create-prefix',
629
629
help='Create the path leading up to the branch '
630
'if it does not already exist')]
630
'if it does not already exist'),
631
Option('use-existing-dir',
632
help='By default push will fail if the target'
633
' directory exists, but does not already'
634
' have a control directory. This flag will'
635
' allow push to proceed.'),
631
637
takes_args = ['location?']
632
638
encoding_type = 'replace'
634
640
def run(self, location=None, remember=False, overwrite=False,
635
create_prefix=False, verbose=False):
641
create_prefix=False, verbose=False, use_existing_dir=False):
636
642
# FIXME: Way too big! Put this into a function called from the
651
657
location_url = to_transport.base
662
br_to = repository_to = dir_to = None
655
dir_to = bzrdir.BzrDir.open(location_url)
656
br_to = dir_to.open_branch()
664
dir_to = bzrdir.BzrDir.open_from_transport(to_transport)
657
665
except errors.NotBranchError:
659
to_transport = to_transport.clone('..')
660
if not create_prefix:
666
pass # Didn't find anything
668
# If we can open a branch, use its direct repository, otherwise see
669
# if there is a repository without a branch.
671
br_to = dir_to.open_branch()
672
except errors.NotBranchError:
673
# Didn't find a branch, can we find a repository?
662
relurl = to_transport.relpath(location_url)
663
mutter('creating directory %s => %s', location_url, relurl)
664
to_transport.mkdir(relurl)
665
except errors.NoSuchFile:
666
raise errors.BzrCommandError("Parent directory of %s "
667
"does not exist." % location)
675
repository_to = dir_to.find_repository()
676
except errors.NoRepositoryPresent:
669
current = to_transport.base
670
needed = [(to_transport, to_transport.relpath(location_url))]
679
# Found a branch, so we must have found a repository
680
repository_to = br_to.repository
684
# XXX: Refactor the create_prefix/no_create_prefix code into a
685
# common helper function
687
to_transport.mkdir('.')
688
except errors.FileExists:
689
if not use_existing_dir:
690
raise errors.BzrCommandError("Target directory %s"
691
" already exists, but does not have a valid .bzr"
692
" directory. Supply --use-existing-dir to push"
693
" there anyway." % location)
694
except errors.NoSuchFile:
695
if not create_prefix:
696
raise errors.BzrCommandError("Parent directory of %s"
698
"\nYou may supply --create-prefix to create all"
699
" leading parent directories."
702
cur_transport = to_transport
703
needed = [cur_transport]
704
# Recurse upwards until we can create a directory successfully
706
new_transport = cur_transport.clone('..')
707
if new_transport.base == cur_transport.base:
708
raise errors.BzrCommandError("Failed to create path"
712
new_transport.mkdir('.')
713
except errors.NoSuchFile:
714
needed.append(new_transport)
715
cur_transport = new_transport
719
# Now we only need to create child directories
673
to_transport, relpath = needed[-1]
674
to_transport.mkdir(relpath)
676
except errors.NoSuchFile:
677
new_transport = to_transport.clone('..')
678
needed.append((new_transport,
679
new_transport.relpath(to_transport.base)))
680
if new_transport.base == to_transport.base:
681
raise errors.BzrCommandError("Could not create "
721
cur_transport = needed.pop()
722
cur_transport.mkdir('.')
724
# Now the target directory exists, but doesn't have a .bzr
725
# directory. So we need to create it, along with any work to create
726
# all of the dependent branches, etc.
683
727
dir_to = br_from.bzrdir.clone(location_url,
684
728
revision_id=br_from.last_revision())
685
729
br_to = dir_to.open_branch()
687
731
# We successfully created the target, remember it
688
732
if br_from.get_push_location() is None or remember:
689
733
br_from.set_push_location(br_to.base)
734
elif repository_to is None:
735
# we have a bzrdir but no branch or repository
736
# XXX: Figure out what to do other than complain.
737
raise errors.BzrCommandError("At %s you have a valid .bzr control"
738
" directory, but not a branch or repository. This is an"
739
" unsupported configuration. Please move the target directory"
740
" out of the way and try again."
743
# We have a repository but no branch, copy the revisions, and then
745
last_revision_id = br_from.last_revision()
746
repository_to.fetch(br_from.repository,
747
revision_id=last_revision_id)
748
br_to = br_from.clone(dir_to, revision_id=last_revision_id)
749
count = len(br_to.revision_history())
750
if br_from.get_push_location() is None or remember:
751
br_from.set_push_location(br_to.base)
752
else: # We have a valid to branch
691
753
# We were able to connect to the remote location, so remember it
692
754
# we don't need to successfully push because of possible divergence.
693
755
if br_from.get_push_location() is None or remember: