~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/push.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
 
1
# Copyright (C) 2008, 2009, 2010 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,
22
20
    bzrdir,
23
21
    errors,
24
22
    revision as _mod_revision,
79
77
        directory exists without a current .bzr directory in it
80
78
    """
81
79
    to_transport = transport.get_transport(location)
82
 
    br_to = repository_to = dir_to = None
83
80
    try:
84
81
        dir_to = bzrdir.BzrDir.open_from_transport(to_transport)
85
82
    except errors.NotBranchError:
86
 
        pass # Didn't find anything
 
83
        # Didn't find anything
 
84
        dir_to = None
87
85
 
88
 
    push_result = PushResult()
89
86
    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
 
 
102
87
        try:
103
 
            to_transport = transport.do_catching_redirections(
104
 
                make_directory, to_transport, redirected)
105
 
        except errors.FileExists:
 
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
        except errors.FileExists, err:
 
92
            if err.path.endswith('/.bzr'):
 
93
                raise errors.BzrCommandError(
 
94
                    "Target directory %s already contains a .bzr directory, "
 
95
                    "but it is not valid." % (location,))
106
96
            if not use_existing_dir:
107
97
                raise errors.BzrCommandError("Target directory %s"
108
 
                     " already exists, but does not have a valid .bzr"
 
98
                     " already exists, but does not have a .bzr"
109
99
                     " directory. Supply --use-existing-dir to push"
110
100
                     " there anyway." % location)
 
101
            # This shouldn't occur, but if it does the FileExists error will be
 
102
            # more informative than an UnboundLocalError for br_to.
 
103
            raise
111
104
        except errors.NoSuchFile:
112
105
            if not create_prefix:
113
106
                raise errors.BzrCommandError("Parent directory of %s"
115
108
                    "\nYou may supply --create-prefix to create all"
116
109
                    " leading parent directories."
117
110
                    % location)
118
 
            builtins._create_prefix(to_transport)
 
111
            # This shouldn't occur (because create_prefix is true, so
 
112
            # create_clone_on_transport should be catching NoSuchFile and
 
113
            # creating the missing directories) but if it does the original
 
114
            # NoSuchFile error will be more informative than an
 
115
            # UnboundLocalError for br_to.
 
116
            raise
119
117
        except errors.TooManyRedirections:
120
118
            raise errors.BzrCommandError("Too many redirections trying "
121
119
                                         "to make %s." % location)
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)
 
120
        push_result = PushResult()
128
121
        # TODO: Some more useful message about what was copied
129
122
        try:
130
123
            push_result.stacked_on = br_to.get_stacked_on_url()
143
136
                    "already exists at the destination location.")
144
137
        try:
145
138
            push_result = dir_to.push_branch(br_from, revision_id, overwrite, 
146
 
                remember)
 
139
                remember, create_prefix)
147
140
        except errors.DivergedBranches:
148
141
            raise errors.BzrCommandError('These branches have diverged.'
149
 
                                    '  Try using "merge" and then "push".')
 
142
                                    '  See "bzr help diverged-branches"'
 
143
                                    ' for more information.')
 
144
        except errors.NoRoundtrippingSupport, e:
 
145
            raise errors.BzrCommandError("It is not possible to losslessly "
 
146
                "push to %s. You may want to use dpush instead." % 
 
147
                    e.target_branch.mapping.vcs.abbreviation)
150
148
        except errors.NoRepositoryPresent:
151
149
            # we have a bzrdir but no branch or repository
152
150
            # XXX: Figure out what to do other than complain.