~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/push.py

  • Committer: John Arbash Meinel
  • Date: 2005-09-15 21:35:53 UTC
  • mfrom: (907.1.57)
  • mto: (1393.2.1)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: john@arbash-meinel.com-20050915213552-a6c83a5ef1e20897
(broken) Transport work is merged in. Tests do not pass yet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""UI helper for the push command."""
18
 
 
19
 
from bzrlib import (builtins, bzrdir, errors, revision as _mod_revision,
20
 
                    transport)
21
 
from bzrlib.trace import note, warning
22
 
 
23
 
 
24
 
class PushResult(object):
25
 
    """Result of a push operation.
26
 
 
27
 
    :ivar branch_push_result: Result of a push between branches
28
 
    :ivar stacked_on: URL of the branch on which the result is stacked
29
 
    """
30
 
 
31
 
    def __init__(self):
32
 
        self.branch_push_result = None
33
 
        self.stacked_on = None
34
 
 
35
 
    def report(self, to_file):
36
 
        """Write a human-readable description of the result."""
37
 
        if self.branch_push_result is None:
38
 
            if self.stacked_on is not None:
39
 
                note('Created new stacked branch referring to %s.' %
40
 
                    self.stacked_on)
41
 
            else:
42
 
                note('Created new branch.')
43
 
        else:
44
 
            self.branch_push_result.report(to_file)
45
 
 
46
 
 
47
 
def _show_push_branch(br_from, revision_id, location, to_file, verbose=False,
48
 
    overwrite=False, remember=False, stacked_on=None, create_prefix=False,
49
 
    use_existing_dir=False):
50
 
    """Push a branch to a location.
51
 
 
52
 
    :param br_from: the source branch
53
 
    :param revision_id: the revision-id to push up to
54
 
    :param location: the url of the destination
55
 
    :param to_file: the output stream
56
 
    :param verbose: if True, display more output than normal
57
 
    :param overwrite: if False, a current branch at the destination may not
58
 
        have diverged from the source, otherwise the push fails
59
 
    :param remember: if True, store the location as the push location for
60
 
        the source branch
61
 
    :param stacked_on: the url of the branch, if any, to stack on;
62
 
        if set, only the revisions not in that branch are pushed
63
 
    :param create_prefix: if True, create the necessary parent directories
64
 
        at the destination if they don't already exist
65
 
    :param use_existing_dir: if True, proceed even if the destination
66
 
        directory exists without a current .bzr directory in it
67
 
    """
68
 
    to_transport = transport.get_transport(location)
69
 
    br_to = repository_to = dir_to = None
70
 
    try:
71
 
        dir_to = bzrdir.BzrDir.open_from_transport(to_transport)
72
 
    except errors.NotBranchError:
73
 
        pass # Didn't find anything
74
 
    else:
75
 
        # If we can open a branch, use its direct repository, otherwise see
76
 
        # if there is a repository without a branch.
77
 
        try:
78
 
            br_to = dir_to.open_branch()
79
 
        except errors.NotBranchError:
80
 
            # Didn't find a branch, can we find a repository?
81
 
            try:
82
 
                repository_to = dir_to.find_repository()
83
 
            except errors.NoRepositoryPresent:
84
 
                pass
85
 
        else:
86
 
            # Found a branch, so we must have found a repository
87
 
            repository_to = br_to.repository
88
 
 
89
 
    push_result = PushResult()
90
 
    if dir_to is None:
91
 
        # The destination doesn't exist; create it.
92
 
        # XXX: Refactor the create_prefix/no_create_prefix code into a
93
 
        #      common helper function
94
 
 
95
 
        def make_directory(transport):
96
 
            transport.mkdir('.')
97
 
            return transport
98
 
 
99
 
        def redirected(transport, e, redirection_notice):
100
 
            note(redirection_notice)
101
 
            return transport._redirected_to(e.source, e.target)
102
 
 
103
 
        try:
104
 
            to_transport = transport.do_catching_redirections(
105
 
                make_directory, to_transport, redirected)
106
 
        except errors.FileExists:
107
 
            if not use_existing_dir:
108
 
                raise errors.BzrCommandError("Target directory %s"
109
 
                     " already exists, but does not have a valid .bzr"
110
 
                     " directory. Supply --use-existing-dir to push"
111
 
                     " there anyway." % location)
112
 
        except errors.NoSuchFile:
113
 
            if not create_prefix:
114
 
                raise errors.BzrCommandError("Parent directory of %s"
115
 
                    " does not exist."
116
 
                    "\nYou may supply --create-prefix to create all"
117
 
                    " leading parent directories."
118
 
                    % location)
119
 
            builtins._create_prefix(to_transport)
120
 
        except errors.TooManyRedirections:
121
 
            raise errors.BzrCommandError("Too many redirections trying "
122
 
                                         "to make %s." % location)
123
 
 
124
 
        # Now the target directory exists, but doesn't have a .bzr
125
 
        # directory. So we need to create it, along with any work to create
126
 
        # all of the dependent branches, etc.
127
 
        br_to = br_from.create_clone_on_transport(to_transport,
128
 
            revision_id=revision_id, stacked_on=stacked_on)
129
 
        # TODO: Some more useful message about what was copied
130
 
        try:
131
 
            push_result.stacked_on = br_to.get_stacked_on_url()
132
 
        except (errors.UnstackableBranchFormat,
133
 
                errors.UnstackableRepositoryFormat,
134
 
                errors.NotStacked):
135
 
            push_result.stacked_on = None
136
 
        # We successfully created the target, remember it
137
 
        if br_from.get_push_location() is None or remember:
138
 
            br_from.set_push_location(br_to.base)
139
 
    elif repository_to is None:
140
 
        # we have a bzrdir but no branch or repository
141
 
        # XXX: Figure out what to do other than complain.
142
 
        raise errors.BzrCommandError("At %s you have a valid .bzr control"
143
 
            " directory, but not a branch or repository. This is an"
144
 
            " unsupported configuration. Please move the target directory"
145
 
            " out of the way and try again."
146
 
            % location)
147
 
    elif br_to is None:
148
 
        # We have a repository but no branch, copy the revisions, and then
149
 
        # create a branch.
150
 
        if stacked_on is not None:
151
 
            warning("Ignoring request for a stacked branch as repository "
152
 
                    "already exists at the destination location.")
153
 
        repository_to.fetch(br_from.repository, revision_id=revision_id)
154
 
        br_to = br_from.clone(dir_to, revision_id=revision_id)
155
 
        if br_from.get_push_location() is None or remember:
156
 
            br_from.set_push_location(br_to.base)
157
 
    else: # We have a valid to branch
158
 
        if stacked_on is not None:
159
 
            warning("Ignoring request for a stacked branch as branch "
160
 
                    "already exists at the destination location.")
161
 
        # We were able to connect to the remote location, so remember it.
162
 
        # (We don't need to successfully push because of possible divergence.)
163
 
        if br_from.get_push_location() is None or remember:
164
 
            br_from.set_push_location(br_to.base)
165
 
        try:
166
 
            try:
167
 
                tree_to = dir_to.open_workingtree()
168
 
            except errors.NotLocalUrl:
169
 
                note("This transport does not update the working "
170
 
                        "tree of: %s. See 'bzr help working-trees' for "
171
 
                        "more information." % br_to.base)
172
 
                push_result.branch_push_result = br_from.push(br_to, overwrite,
173
 
                                                   stop_revision=revision_id)
174
 
            except errors.NoWorkingTree:
175
 
                push_result.branch_push_result = br_from.push(br_to, overwrite,
176
 
                                                   stop_revision=revision_id)
177
 
            else:
178
 
                tree_to.lock_write()
179
 
                try:
180
 
                    push_result.branch_push_result = br_from.push(tree_to.branch,
181
 
                        overwrite, stop_revision=revision_id)
182
 
                    tree_to.update()
183
 
                finally:
184
 
                    tree_to.unlock()
185
 
        except errors.DivergedBranches:
186
 
            raise errors.BzrCommandError('These branches have diverged.'
187
 
                                    '  Try using "merge" and then "push".')
188
 
    push_result.report(to_file)
189
 
    if push_result.branch_push_result is not None:
190
 
        old_revid = push_result.branch_push_result.old_revid
191
 
        old_revno = push_result.branch_push_result.old_revno
192
 
    else:
193
 
        old_revid = _mod_revision.NULL_REVISION
194
 
        old_revno = 0
195
 
    if verbose:
196
 
        br_to.lock_read()
197
 
        try:
198
 
            from bzrlib.log import show_branch_change
199
 
            show_branch_change(br_to, to_file, old_revno, old_revid)
200
 
        finally:
201
 
            br_to.unlock()