~bzr-pqm/bzr/bzr.dev

3221.19.1 by Ian Clatworthy
refactor cmd_push to use a helper function
1
# Copyright (C) 2008 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""UI helper for the push command."""
18
3848.1.18 by Aaron Bentley
Update push to use show_branch_change
19
from bzrlib import (builtins, bzrdir, errors, revision as _mod_revision,
20
                    transport)
3221.19.1 by Ian Clatworthy
refactor cmd_push to use a helper function
21
from bzrlib.trace import note, warning
22
23
24
def _show_push_branch(br_from, revision_id, location, to_file, verbose=False,
3549.1.1 by Martin Pool
rename push --reference to --stacked-on
25
    overwrite=False, remember=False, stacked_on=None, create_prefix=False,
3221.19.1 by Ian Clatworthy
refactor cmd_push to use a helper function
26
    use_existing_dir=False):
27
    """Push a branch to a location.
28
29
    :param br_from: the source branch
30
    :param revision_id: the revision-id to push up to
31
    :param location: the url of the destination
32
    :param to_file: the output stream
33
    :param verbose: if True, display more output than normal
34
    :param overwrite: if False, a current branch at the destination may not
35
        have diverged from the source, otherwise the push fails
36
    :param remember: if True, store the location as the push location for
37
        the source branch
3549.1.1 by Martin Pool
rename push --reference to --stacked-on
38
    :param stacked_on: the url of the branch, if any, to stack on;
3221.19.1 by Ian Clatworthy
refactor cmd_push to use a helper function
39
        if set, only the revisions not in that branch are pushed
40
    :param create_prefix: if True, create the necessary parent directories
41
        at the destination if they don't already exist
42
    :param use_existing_dir: if True, proceed even if the destination
43
        directory exists without a current .bzr directory in it
44
    """
45
    to_transport = transport.get_transport(location)
46
    br_to = repository_to = dir_to = None
47
    try:
48
        dir_to = bzrdir.BzrDir.open_from_transport(to_transport)
49
    except errors.NotBranchError:
50
        pass # Didn't find anything
51
    else:
52
        # If we can open a branch, use its direct repository, otherwise see
53
        # if there is a repository without a branch.
54
        try:
55
            br_to = dir_to.open_branch()
56
        except errors.NotBranchError:
57
            # Didn't find a branch, can we find a repository?
58
            try:
59
                repository_to = dir_to.find_repository()
60
            except errors.NoRepositoryPresent:
61
                pass
62
        else:
63
            # Found a branch, so we must have found a repository
64
            repository_to = br_to.repository
65
66
    push_result = None
67
    if dir_to is None:
68
        # The destination doesn't exist; create it.
69
        # XXX: Refactor the create_prefix/no_create_prefix code into a
70
        #      common helper function
71
72
        def make_directory(transport):
73
            transport.mkdir('.')
74
            return transport
75
3878.4.4 by Vincent Ladeuil
Cleanup.
76
        def redirected(transport, e, redirection_notice):
77
            note(redirection_notice)
3878.4.5 by Vincent Ladeuil
Don't use the exception as a parameter for _redirected_to.
78
            return transport._redirected_to(e.source, e.target)
3221.19.1 by Ian Clatworthy
refactor cmd_push to use a helper function
79
80
        try:
81
            to_transport = transport.do_catching_redirections(
82
                make_directory, to_transport, redirected)
83
        except errors.FileExists:
84
            if not use_existing_dir:
85
                raise errors.BzrCommandError("Target directory %s"
86
                     " already exists, but does not have a valid .bzr"
87
                     " directory. Supply --use-existing-dir to push"
88
                     " there anyway." % location)
89
        except errors.NoSuchFile:
90
            if not create_prefix:
91
                raise errors.BzrCommandError("Parent directory of %s"
92
                    " does not exist."
93
                    "\nYou may supply --create-prefix to create all"
94
                    " leading parent directories."
95
                    % location)
96
            builtins._create_prefix(to_transport)
97
        except errors.TooManyRedirections:
98
            raise errors.BzrCommandError("Too many redirections trying "
99
                                         "to make %s." % location)
100
101
        # Now the target directory exists, but doesn't have a .bzr
102
        # directory. So we need to create it, along with any work to create
103
        # all of the dependent branches, etc.
4044.1.1 by Robert Collins
Create Branch.create_clone_on_transport helper method to combine bzr and branch creation for push.
104
        br_to = br_from.create_clone_on_transport(to_transport,
3650.5.1 by Aaron Bentley
Fix push to use clone all the time.
105
            revision_id=revision_id, stacked_on=stacked_on)
3221.19.1 by Ian Clatworthy
refactor cmd_push to use a helper function
106
        # TODO: Some more useful message about what was copied
3650.5.2 by Aaron Bentley
Always show stacked message if stacking done.
107
        try:
108
            finally_stacked_on = br_to.get_stacked_on_url()
109
        except (errors.UnstackableBranchFormat,
110
                errors.UnstackableRepositoryFormat,
111
                errors.NotStacked):
112
            finally_stacked_on = None
113
        if finally_stacked_on is not None:
3978.2.2 by Jelmer Vernooij
Write status messages during push to stderr rather than stdout.
114
            note('Created new stacked branch referring to %s.' %
3650.5.2 by Aaron Bentley
Always show stacked message if stacking done.
115
                 finally_stacked_on)
3221.19.1 by Ian Clatworthy
refactor cmd_push to use a helper function
116
        else:
3978.2.2 by Jelmer Vernooij
Write status messages during push to stderr rather than stdout.
117
            note('Created new branch.')
3221.19.1 by Ian Clatworthy
refactor cmd_push to use a helper function
118
        # We successfully created the target, remember it
119
        if br_from.get_push_location() is None or remember:
120
            br_from.set_push_location(br_to.base)
121
    elif repository_to is None:
122
        # we have a bzrdir but no branch or repository
123
        # XXX: Figure out what to do other than complain.
124
        raise errors.BzrCommandError("At %s you have a valid .bzr control"
125
            " directory, but not a branch or repository. This is an"
126
            " unsupported configuration. Please move the target directory"
127
            " out of the way and try again."
128
            % location)
129
    elif br_to is None:
130
        # We have a repository but no branch, copy the revisions, and then
131
        # create a branch.
3549.1.1 by Martin Pool
rename push --reference to --stacked-on
132
        if stacked_on is not None:
3221.19.4 by Ian Clatworthy
shallow -> stacked
133
            warning("Ignoring request for a stacked branch as repository "
3221.19.2 by Ian Clatworthy
tweaks to ui during review by igc
134
                    "already exists at the destination location.")
3221.19.1 by Ian Clatworthy
refactor cmd_push to use a helper function
135
        repository_to.fetch(br_from.repository, revision_id=revision_id)
136
        br_to = br_from.clone(dir_to, revision_id=revision_id)
137
        note('Created new branch.')
138
        if br_from.get_push_location() is None or remember:
139
            br_from.set_push_location(br_to.base)
140
    else: # We have a valid to branch
3549.1.1 by Martin Pool
rename push --reference to --stacked-on
141
        if stacked_on is not None:
3221.19.4 by Ian Clatworthy
shallow -> stacked
142
            warning("Ignoring request for a stacked branch as branch "
3221.19.2 by Ian Clatworthy
tweaks to ui during review by igc
143
                    "already exists at the destination location.")
144
        # We were able to connect to the remote location, so remember it.
145
        # (We don't need to successfully push because of possible divergence.)
3221.19.1 by Ian Clatworthy
refactor cmd_push to use a helper function
146
        if br_from.get_push_location() is None or remember:
147
            br_from.set_push_location(br_to.base)
148
        try:
149
            try:
150
                tree_to = dir_to.open_workingtree()
151
            except errors.NotLocalUrl:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
152
                warning("This transport does not update the working "
3221.19.1 by Ian Clatworthy
refactor cmd_push to use a helper function
153
                        "tree of: %s. See 'bzr help working-trees' for "
154
                        "more information." % br_to.base)
155
                push_result = br_from.push(br_to, overwrite,
156
                                           stop_revision=revision_id)
157
            except errors.NoWorkingTree:
158
                push_result = br_from.push(br_to, overwrite,
159
                                           stop_revision=revision_id)
160
            else:
161
                tree_to.lock_write()
162
                try:
163
                    push_result = br_from.push(tree_to.branch, overwrite,
164
                                               stop_revision=revision_id)
165
                    tree_to.update()
166
                finally:
167
                    tree_to.unlock()
168
        except errors.DivergedBranches:
169
            raise errors.BzrCommandError('These branches have diverged.'
170
                                    '  Try using "merge" and then "push".')
171
    if push_result is not None:
172
        push_result.report(to_file)
3848.1.19 by Aaron Bentley
Show log for non-initial push -v
173
        old_revid = push_result.old_revid
174
        old_revno = push_result.old_revno
175
    else:
176
        old_revid = _mod_revision.NULL_REVISION
177
        old_revno = 0
178
    if verbose:
3848.1.18 by Aaron Bentley
Update push to use show_branch_change
179
        br_to.lock_read()
180
        try:
181
            from bzrlib.log import show_branch_change
3848.1.19 by Aaron Bentley
Show log for non-initial push -v
182
            show_branch_change(br_to, to_file, old_revno, old_revid)
3848.1.18 by Aaron Bentley
Update push to use show_branch_change
183
        finally:
184
            br_to.unlock()