~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/send.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 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
 
 
18
import time
 
19
 
 
20
from bzrlib import (
 
21
    bzrdir,
 
22
    errors,
 
23
    merge_directive,
 
24
    osutils,
 
25
    registry,
 
26
    trace,
 
27
    )
 
28
from bzrlib.branch import (
 
29
    Branch,
 
30
    )
 
31
from bzrlib.revision import (
 
32
    NULL_REVISION,
 
33
    )
 
34
 
 
35
 
 
36
format_registry = registry.Registry()
 
37
 
 
38
 
 
39
def send(submit_branch, revision, public_branch, remember, format,
 
40
         no_bundle, no_patch, output, from_, mail_to, message, body,
 
41
         to_file, strict=None):
 
42
    tree, branch = bzrdir.BzrDir.open_containing_tree_or_branch(from_)[:2]
 
43
    # we may need to write data into branch's repository to calculate
 
44
    # the data to send.
 
45
    branch.lock_write()
 
46
    try:
 
47
        if output is None:
 
48
            config = branch.get_config()
 
49
            if mail_to is None:
 
50
                mail_to = config.get_user_option('submit_to')
 
51
            mail_client = config.get_mail_client()
 
52
            if (not getattr(mail_client, 'supports_body', False)
 
53
                and body is not None):
 
54
                raise errors.BzrCommandError(
 
55
                    'Mail client "%s" does not support specifying body' %
 
56
                    mail_client.__class__.__name__)
 
57
        if remember and submit_branch is None:
 
58
            raise errors.BzrCommandError(
 
59
                '--remember requires a branch to be specified.')
 
60
        stored_submit_branch = branch.get_submit_branch()
 
61
        remembered_submit_branch = None
 
62
        if submit_branch is None:
 
63
            submit_branch = stored_submit_branch
 
64
            remembered_submit_branch = "submit"
 
65
        else:
 
66
            if stored_submit_branch is None or remember:
 
67
                branch.set_submit_branch(submit_branch)
 
68
        if submit_branch is None:
 
69
            submit_branch = branch.get_parent()
 
70
            remembered_submit_branch = "parent"
 
71
        if submit_branch is None:
 
72
            raise errors.BzrCommandError('No submit branch known or'
 
73
                                         ' specified')
 
74
        if remembered_submit_branch is not None:
 
75
            trace.note('Using saved %s location "%s" to determine what '
 
76
                       'changes to submit.', remembered_submit_branch,
 
77
                       submit_branch)
 
78
 
 
79
        if mail_to is None or format is None:
 
80
            # TODO: jam 20090716 we open the submit_branch here, but we *don't*
 
81
            #       pass it down into the format creation, so it will have to
 
82
            #       open it again
 
83
            submit_br = Branch.open(submit_branch)
 
84
            submit_config = submit_br.get_config()
 
85
            if mail_to is None:
 
86
                mail_to = submit_config.get_user_option("child_submit_to")
 
87
            if format is None:
 
88
                formatname = submit_br.get_child_submit_format()
 
89
                try:
 
90
                    format = format_registry.get(formatname)
 
91
                except KeyError:
 
92
                    raise errors.BzrCommandError("No such send format '%s'." % 
 
93
                                                 formatname)
 
94
 
 
95
        stored_public_branch = branch.get_public_branch()
 
96
        if public_branch is None:
 
97
            public_branch = stored_public_branch
 
98
        elif stored_public_branch is None or remember:
 
99
            branch.set_public_branch(public_branch)
 
100
        if no_bundle and public_branch is None:
 
101
            raise errors.BzrCommandError('No public branch specified or'
 
102
                                         ' known')
 
103
        base_revision_id = None
 
104
        revision_id = None
 
105
        if revision is not None:
 
106
            if len(revision) > 2:
 
107
                raise errors.BzrCommandError('bzr send takes '
 
108
                    'at most two one revision identifiers')
 
109
            revision_id = revision[-1].as_revision_id(branch)
 
110
            if len(revision) == 2:
 
111
                base_revision_id = revision[0].as_revision_id(branch)
 
112
        if revision_id is None:
 
113
            if strict is None:
 
114
                strict = branch.get_config(
 
115
                    ).get_user_option_as_bool('send_strict')
 
116
            if strict is None: strict = True # default value
 
117
            if strict and tree is not None:
 
118
                if (tree.has_changes(tree.basis_tree())
 
119
                    or len(tree.get_parent_ids()) > 1):
 
120
                    raise errors.UncommittedChanges(
 
121
                        tree, more='Use --no-strict to force the send.')
 
122
                if tree.last_revision() != tree.branch.last_revision():
 
123
                    # The tree has lost sync with its branch, there is little
 
124
                    # chance that the user is aware of it but he can still force
 
125
                    # the push with --no-strict
 
126
                    raise errors.OutOfDateTree(
 
127
                        tree, more='Use --no-strict to force the send.')
 
128
            revision_id = branch.last_revision()
 
129
        if revision_id == NULL_REVISION:
 
130
            raise errors.BzrCommandError('No revisions to submit.')
 
131
        if format is None:
 
132
            format = format_registry.get()
 
133
        directive = format(branch, revision_id, submit_branch,
 
134
            public_branch, no_patch, no_bundle, message, base_revision_id)
 
135
        if output is None:
 
136
            directive.compose_merge_request(mail_client, mail_to, body,
 
137
                                            branch, tree)
 
138
        else:
 
139
            if output == '-':
 
140
                outfile = to_file
 
141
            else:
 
142
                outfile = open(output, 'wb')
 
143
            try:
 
144
                outfile.writelines(directive.to_lines())
 
145
            finally:
 
146
                if outfile is not to_file:
 
147
                    outfile.close()
 
148
    finally:
 
149
        branch.unlock()
 
150
 
 
151
 
 
152
def _send_4(branch, revision_id, submit_branch, public_branch,
 
153
            no_patch, no_bundle, message, base_revision_id):
 
154
    return merge_directive.MergeDirective2.from_objects(
 
155
        branch.repository, revision_id, time.time(),
 
156
        osutils.local_time_offset(), submit_branch,
 
157
        public_branch=public_branch, include_patch=not no_patch,
 
158
        include_bundle=not no_bundle, message=message,
 
159
        base_revision_id=base_revision_id)
 
160
 
 
161
 
 
162
def _send_0_9(branch, revision_id, submit_branch, public_branch,
 
163
              no_patch, no_bundle, message, base_revision_id):
 
164
    if not no_bundle:
 
165
        if not no_patch:
 
166
            patch_type = 'bundle'
 
167
        else:
 
168
            raise errors.BzrCommandError('Format 0.9 does not'
 
169
                ' permit bundle with no patch')
 
170
    else:
 
171
        if not no_patch:
 
172
            patch_type = 'diff'
 
173
        else:
 
174
            patch_type = None
 
175
    return merge_directive.MergeDirective.from_objects(
 
176
        branch.repository, revision_id, time.time(),
 
177
        osutils.local_time_offset(), submit_branch,
 
178
        public_branch=public_branch, patch_type=patch_type,
 
179
        message=message)
 
180
 
 
181
 
 
182
format_registry.register('4', 
 
183
    _send_4, 'Bundle format 4, Merge Directive 2 (default)')
 
184
format_registry.register('0.9',
 
185
    _send_0_9, 'Bundle format 0.9, Merge Directive 1')
 
186
format_registry.default_key = '4'