13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""UI helper for the push command."""
22
revision as _mod_revision,
25
from bzrlib.trace import (
29
from bzrlib.i18n import gettext
32
class PushResult(object):
33
"""Result of a push operation.
35
:ivar branch_push_result: Result of a push between branches
36
:ivar target_branch: The target branch
37
:ivar stacked_on: URL of the branch on which the result is stacked
38
:ivar workingtree_updated: Whether or not the target workingtree was updated.
42
self.branch_push_result = None
43
self.stacked_on = None
44
self.workingtree_updated = None
45
self.target_branch = None
47
def report(self, to_file):
48
"""Write a human-readable description of the result."""
49
if self.branch_push_result is None:
50
if self.stacked_on is not None:
51
note(gettext('Created new stacked branch referring to %s.') %
54
note(gettext('Created new branch.'))
56
self.branch_push_result.report(to_file)
19
from bzrlib import builtins, bzrdir, errors, transport
20
from bzrlib.trace import note, warning
59
23
def _show_push_branch(br_from, revision_id, location, to_file, verbose=False,
60
24
overwrite=False, remember=False, stacked_on=None, create_prefix=False,
61
use_existing_dir=False, no_tree=False):
25
use_existing_dir=False):
62
26
"""Push a branch to a location.
64
28
:param br_from: the source branch
78
42
directory exists without a current .bzr directory in it
80
44
to_transport = transport.get_transport(location)
45
br_to = repository_to = dir_to = None
82
47
dir_to = bzrdir.BzrDir.open_from_transport(to_transport)
83
48
except errors.NotBranchError:
84
# Didn't find anything
49
pass # Didn't find anything
51
# If we can open a branch, use its direct repository, otherwise see
52
# if there is a repository without a branch.
54
br_to = dir_to.open_branch()
55
except errors.NotBranchError:
56
# Didn't find a branch, can we find a repository?
58
repository_to = dir_to.find_repository()
59
except errors.NoRepositoryPresent:
62
# Found a branch, so we must have found a repository
63
repository_to = br_to.repository
69
# The destination doesn't exist; create it.
70
# XXX: Refactor the create_prefix/no_create_prefix code into a
71
# common helper function
73
def make_directory(transport):
77
def redirected(transport, e, redirection_notice):
78
note(redirection_notice)
79
return transport._redirected_to(e.source, e.target)
89
br_to = br_from.create_clone_on_transport(to_transport,
90
revision_id=revision_id, stacked_on=stacked_on,
91
create_prefix=create_prefix, use_existing_dir=use_existing_dir,
93
except errors.FileExists, err:
94
if err.path.endswith('/.bzr'):
95
raise errors.BzrCommandError(gettext(
96
"Target directory %s already contains a .bzr directory, "
97
"but it is not valid.") % (location,))
82
to_transport = transport.do_catching_redirections(
83
make_directory, to_transport, redirected)
84
except errors.FileExists:
98
85
if not use_existing_dir:
99
raise errors.BzrCommandError(gettext("Target directory %s"
100
" already exists, but does not have a .bzr"
86
raise errors.BzrCommandError("Target directory %s"
87
" already exists, but does not have a valid .bzr"
101
88
" directory. Supply --use-existing-dir to push"
102
" there anyway.") % location)
103
# This shouldn't occur, but if it does the FileExists error will be
104
# more informative than an UnboundLocalError for br_to.
89
" there anyway." % location)
106
90
except errors.NoSuchFile:
107
91
if not create_prefix:
108
raise errors.BzrCommandError(gettext("Parent directory of %s"
92
raise errors.BzrCommandError("Parent directory of %s"
109
93
" does not exist."
110
94
"\nYou may supply --create-prefix to create all"
111
" leading parent directories.")
95
" leading parent directories."
113
# This shouldn't occur (because create_prefix is true, so
114
# create_clone_on_transport should be catching NoSuchFile and
115
# creating the missing directories) but if it does the original
116
# NoSuchFile error will be more informative than an
117
# UnboundLocalError for br_to.
97
builtins._create_prefix(to_transport)
119
98
except errors.TooManyRedirections:
120
raise errors.BzrCommandError(gettext("Too many redirections trying "
121
"to make %s.") % location)
122
push_result = PushResult()
99
raise errors.BzrCommandError("Too many redirections trying "
100
"to make %s." % location)
102
# Now the target directory exists, but doesn't have a .bzr
103
# directory. So we need to create it, along with any work to create
104
# all of the dependent branches, etc.
105
dir_to = br_from.bzrdir.clone_on_transport(to_transport,
106
revision_id=revision_id, stacked_on=stacked_on)
107
br_to = dir_to.open_branch()
123
108
# TODO: Some more useful message about what was copied
125
push_result.stacked_on = br_to.get_stacked_on_url()
110
finally_stacked_on = br_to.get_stacked_on_url()
126
111
except (errors.UnstackableBranchFormat,
127
112
errors.UnstackableRepositoryFormat,
128
113
errors.NotStacked):
129
push_result.stacked_on = None
130
push_result.target_branch = br_to
131
push_result.old_revid = _mod_revision.NULL_REVISION
132
push_result.old_revno = 0
133
# Remembers if asked explicitly or no previous location is set
135
or (remember is None and br_from.get_push_location() is None)):
114
finally_stacked_on = None
115
if finally_stacked_on is not None:
116
note('Created new stacked branch referring to %s.' %
119
note('Created new branch.')
120
# We successfully created the target, remember it
121
if br_from.get_push_location() is None or remember:
136
122
br_from.set_push_location(br_to.base)
123
elif repository_to is None:
124
# we have a bzrdir but no branch or repository
125
# XXX: Figure out what to do other than complain.
126
raise errors.BzrCommandError("At %s you have a valid .bzr control"
127
" directory, but not a branch or repository. This is an"
128
" unsupported configuration. Please move the target directory"
129
" out of the way and try again."
132
# We have a repository but no branch, copy the revisions, and then
138
134
if stacked_on is not None:
139
135
warning("Ignoring request for a stacked branch as repository "
140
136
"already exists at the destination location.")
137
repository_to.fetch(br_from.repository, revision_id=revision_id)
138
br_to = br_from.clone(dir_to, revision_id=revision_id)
139
note('Created new branch.')
140
if br_from.get_push_location() is None or remember:
141
br_from.set_push_location(br_to.base)
142
else: # We have a valid to branch
143
if stacked_on is not None:
144
warning("Ignoring request for a stacked branch as branch "
145
"already exists at the destination location.")
146
# We were able to connect to the remote location, so remember it.
147
# (We don't need to successfully push because of possible divergence.)
148
if br_from.get_push_location() is None or remember:
149
br_from.set_push_location(br_to.base)
151
old_rh = br_to.revision_history()
142
push_result = dir_to.push_branch(br_from, revision_id, overwrite,
143
remember, create_prefix)
154
tree_to = dir_to.open_workingtree()
155
except errors.NotLocalUrl:
156
warning("This transport does not update the working "
157
"tree of: %s. See 'bzr help working-trees' for "
158
"more information." % br_to.base)
159
push_result = br_from.push(br_to, overwrite,
160
stop_revision=revision_id)
161
except errors.NoWorkingTree:
162
push_result = br_from.push(br_to, overwrite,
163
stop_revision=revision_id)
167
push_result = br_from.push(tree_to.branch, overwrite,
168
stop_revision=revision_id)
144
172
except errors.DivergedBranches:
145
raise errors.BzrCommandError(gettext('These branches have diverged.'
146
' See "bzr help diverged-branches"'
147
' for more information.'))
148
except errors.NoRoundtrippingSupport, e:
149
raise errors.BzrCommandError(gettext("It is not possible to losslessly "
150
"push to %s. You may want to use dpush instead.") %
151
e.target_branch.mapping.vcs.abbreviation)
152
except errors.NoRepositoryPresent:
153
# we have a bzrdir but no branch or repository
154
# XXX: Figure out what to do other than complain.
155
raise errors.BzrCommandError(gettext("At %s you have a valid .bzr"
156
" control directory, but not a branch or repository. This"
157
" is an unsupported configuration. Please move the target"
158
" directory out of the way and try again.") % location)
159
if push_result.workingtree_updated == False:
160
warning("This transport does not update the working "
161
"tree of: %s. See 'bzr help working-trees' for "
162
"more information." % push_result.target_branch.base)
163
push_result.report(to_file)
165
br_to = push_result.target_branch
168
from bzrlib.log import show_branch_change
169
show_branch_change(br_to, to_file, push_result.old_revno,
170
push_result.old_revid)
173
raise errors.BzrCommandError('These branches have diverged.'
174
' Try using "merge" and then "push".')
175
if push_result is not None:
176
push_result.report(to_file)
178
new_rh = br_to.revision_history()
181
from bzrlib.log import show_changed_revisions
182
show_changed_revisions(br_to, old_rh, new_rh,
185
# we probably did a clone rather than a push, so a message was