~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-02-07 22:55:51 UTC
  • mfrom: (2227.3.5 fix_push_alt)
  • Revision ID: pqm@pqm.ubuntu.com-20070207225551-26a710f4cc2f19a6
(John Arbash Meinel) allow 'bzr push' to resume an interrupted push, and push into an existing directory (#30576, #45504)

Show diffs side-by-side

added added

removed removed

Lines of Context:
625
625
    """
626
626
 
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.'),
 
636
                     ]
631
637
    takes_args = ['location?']
632
638
    encoding_type = 'replace'
633
639
 
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
637
643
        # command.
638
644
        
651
657
        location_url = to_transport.base
652
658
 
653
659
        old_rh = []
 
660
        count = 0
 
661
 
 
662
        br_to = repository_to = dir_to = None
654
663
        try:
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:
658
 
            # create a branch.
659
 
            to_transport = to_transport.clone('..')
660
 
            if not create_prefix:
 
666
            pass # Didn't find anything
 
667
        else:
 
668
            # If we can open a branch, use its direct repository, otherwise see
 
669
            # if there is a repository without a branch.
 
670
            try:
 
671
                br_to = dir_to.open_branch()
 
672
            except errors.NotBranchError:
 
673
                # Didn't find a branch, can we find a repository?
661
674
                try:
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:
 
677
                    pass
668
678
            else:
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
 
681
 
 
682
        old_rh = []
 
683
        if dir_to is None:
 
684
            # XXX: Refactor the create_prefix/no_create_prefix code into a
 
685
            #      common helper function
 
686
            try:
 
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"
 
697
                        " does not exist."
 
698
                        "\nYou may supply --create-prefix to create all"
 
699
                        " leading parent directories."
 
700
                        % location)
 
701
 
 
702
                cur_transport = to_transport
 
703
                needed = [cur_transport]
 
704
                # Recurse upwards until we can create a directory successfully
 
705
                while True:
 
706
                    new_transport = cur_transport.clone('..')
 
707
                    if new_transport.base == cur_transport.base:
 
708
                        raise errors.BzrCommandError("Failed to create path"
 
709
                                                     " prefix for %s."
 
710
                                                     % location)
 
711
                    try:
 
712
                        new_transport.mkdir('.')
 
713
                    except errors.NoSuchFile:
 
714
                        needed.append(new_transport)
 
715
                        cur_transport = new_transport
 
716
                    else:
 
717
                        break
 
718
 
 
719
                # Now we only need to create child directories
671
720
                while needed:
672
 
                    try:
673
 
                        to_transport, relpath = needed[-1]
674
 
                        to_transport.mkdir(relpath)
675
 
                        needed.pop()
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 "
682
 
                                                         "path prefix.")
 
721
                    cur_transport = needed.pop()
 
722
                    cur_transport.mkdir('.')
 
723
            
 
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)
690
 
        else:
 
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."
 
741
                % location)
 
742
        elif br_to is None:
 
743
            # We have a repository but no branch, copy the revisions, and then
 
744
            # create a branch.
 
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: