~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/push.py

  • Committer: Jelmer Vernooij
  • Date: 2009-04-10 15:58:09 UTC
  • mto: This revision was merged to the branch mainline in revision 4284.
  • Revision ID: jelmer@samba.org-20090410155809-kdibzcjvp7pdb83f
Fix missing import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
"""UI helper for the push command."""
18
18
 
19
19
from bzrlib import (
 
20
    builtins,
 
21
    branch,
20
22
    bzrdir,
21
23
    errors,
22
24
    revision as _mod_revision,
57
59
 
58
60
def _show_push_branch(br_from, revision_id, location, to_file, verbose=False,
59
61
    overwrite=False, remember=False, stacked_on=None, create_prefix=False,
60
 
    use_existing_dir=False, no_tree=False):
 
62
    use_existing_dir=False):
61
63
    """Push a branch to a location.
62
64
 
63
65
    :param br_from: the source branch
77
79
        directory exists without a current .bzr directory in it
78
80
    """
79
81
    to_transport = transport.get_transport(location)
 
82
    br_to = repository_to = dir_to = None
80
83
    try:
81
84
        dir_to = bzrdir.BzrDir.open_from_transport(to_transport)
82
85
    except errors.NotBranchError:
83
 
        # Didn't find anything
84
 
        dir_to = None
 
86
        pass # Didn't find anything
85
87
 
 
88
    push_result = PushResult()
86
89
    if dir_to is None:
 
90
        # The destination doesn't exist; create it.
 
91
        # XXX: Refactor the create_prefix/no_create_prefix code into a
 
92
        #      common helper function
 
93
 
 
94
        def make_directory(transport):
 
95
            transport.mkdir('.')
 
96
            return transport
 
97
 
 
98
        def redirected(transport, e, redirection_notice):
 
99
            note(redirection_notice)
 
100
            return transport._redirected_to(e.source, e.target)
 
101
 
87
102
        try:
88
 
            br_to = br_from.create_clone_on_transport(to_transport,
89
 
                revision_id=revision_id, stacked_on=stacked_on,
90
 
                create_prefix=create_prefix, use_existing_dir=use_existing_dir,
91
 
                no_tree=no_tree)
92
 
        except errors.FileExists, err:
93
 
            if err.path.endswith('/.bzr'):
94
 
                raise errors.BzrCommandError(
95
 
                    "Target directory %s already contains a .bzr directory, "
96
 
                    "but it is not valid." % (location,))
 
103
            to_transport = transport.do_catching_redirections(
 
104
                make_directory, to_transport, redirected)
 
105
        except errors.FileExists:
97
106
            if not use_existing_dir:
98
107
                raise errors.BzrCommandError("Target directory %s"
99
 
                     " already exists, but does not have a .bzr"
 
108
                     " already exists, but does not have a valid .bzr"
100
109
                     " directory. Supply --use-existing-dir to push"
101
110
                     " there anyway." % location)
102
 
            # This shouldn't occur, but if it does the FileExists error will be
103
 
            # more informative than an UnboundLocalError for br_to.
104
 
            raise
105
111
        except errors.NoSuchFile:
106
112
            if not create_prefix:
107
113
                raise errors.BzrCommandError("Parent directory of %s"
109
115
                    "\nYou may supply --create-prefix to create all"
110
116
                    " leading parent directories."
111
117
                    % location)
112
 
            # This shouldn't occur (because create_prefix is true, so
113
 
            # create_clone_on_transport should be catching NoSuchFile and
114
 
            # creating the missing directories) but if it does the original
115
 
            # NoSuchFile error will be more informative than an
116
 
            # UnboundLocalError for br_to.
117
 
            raise
 
118
            builtins._create_prefix(to_transport)
118
119
        except errors.TooManyRedirections:
119
120
            raise errors.BzrCommandError("Too many redirections trying "
120
121
                                         "to make %s." % location)
121
 
        push_result = PushResult()
 
122
 
 
123
        # Now the target directory exists, but doesn't have a .bzr
 
124
        # directory. So we need to create it, along with any work to create
 
125
        # all of the dependent branches, etc.
 
126
        br_to = br_from.create_clone_on_transport(to_transport,
 
127
            revision_id=revision_id, stacked_on=stacked_on)
122
128
        # TODO: Some more useful message about what was copied
123
129
        try:
124
130
            push_result.stacked_on = br_to.get_stacked_on_url()
129
135
        push_result.target_branch = br_to
130
136
        push_result.old_revid = _mod_revision.NULL_REVISION
131
137
        push_result.old_revno = 0
132
 
        # Remembers if asked explicitly or no previous location is set
133
 
        if (remember
134
 
            or (remember is None and br_from.get_push_location() is None)):
 
138
        if br_from.get_push_location() is None or remember:
135
139
            br_from.set_push_location(br_to.base)
136
140
    else:
137
141
        if stacked_on is not None:
139
143
                    "already exists at the destination location.")
140
144
        try:
141
145
            push_result = dir_to.push_branch(br_from, revision_id, overwrite, 
142
 
                remember, create_prefix)
 
146
                remember)
143
147
        except errors.DivergedBranches:
144
148
            raise errors.BzrCommandError('These branches have diverged.'
145
 
                                    '  See "bzr help diverged-branches"'
146
 
                                    ' for more information.')
147
 
        except errors.NoRoundtrippingSupport, e:
148
 
            raise errors.BzrCommandError("It is not possible to losslessly "
149
 
                "push to %s. You may want to use dpush instead." % 
150
 
                    e.target_branch.mapping.vcs.abbreviation)
 
149
                                    '  Try using "merge" and then "push".')
151
150
        except errors.NoRepositoryPresent:
152
151
            # we have a bzrdir but no branch or repository
153
152
            # XXX: Figure out what to do other than complain.