/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/send.py

  • Committer: Robert Collins
  • Date: 2007-04-19 02:27:44 UTC
  • mto: This revision was merged to the branch mainline in revision 2426.
  • Revision ID: robertc@robertcollins.net-20070419022744-pfdqz42kp1wizh43
``make docs`` now creates a man page at ``man1/bzr.1`` fixing bug 107388.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 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
 
import os
18
 
import time
19
 
 
20
 
from . import (
21
 
    controldir,
22
 
    errors,
23
 
    osutils,
24
 
    registry,
25
 
    trace,
26
 
    )
27
 
from .i18n import gettext
28
 
from .branch import (
29
 
    Branch,
30
 
    )
31
 
from .revision import (
32
 
    NULL_REVISION,
33
 
    )
34
 
 
35
 
 
36
 
format_registry = registry.Registry()
37
 
 
38
 
 
39
 
def send(target_branch, revision, public_branch, remember,
40
 
         format, no_bundle, no_patch, output, from_, mail_to, message, body,
41
 
         to_file, strict=None):
42
 
    possible_transports = []
43
 
    tree, branch = controldir.ControlDir.open_containing_tree_or_branch(
44
 
        from_, possible_transports=possible_transports)[:2]
45
 
    # we may need to write data into branch's repository to calculate
46
 
    # the data to send.
47
 
    with branch.lock_write():
48
 
        if output is None:
49
 
            config_stack = branch.get_config_stack()
50
 
            if mail_to is None:
51
 
                mail_to = config_stack.get('submit_to')
52
 
            mail_client = config_stack.get('mail_client')(config_stack)
53
 
            if (not getattr(mail_client, 'supports_body', False)
54
 
                    and body is not None):
55
 
                raise errors.CommandError(gettext(
56
 
                    'Mail client "%s" does not support specifying body') %
57
 
                    mail_client.__class__.__name__)
58
 
        if remember and target_branch is None:
59
 
            raise errors.CommandError(gettext(
60
 
                '--remember requires a branch to be specified.'))
61
 
        stored_target_branch = branch.get_submit_branch()
62
 
        remembered_target_branch = None
63
 
        if target_branch is None:
64
 
            target_branch = stored_target_branch
65
 
            remembered_target_branch = "submit"
66
 
        else:
67
 
            # Remembers if asked explicitly or no previous location is set
68
 
            if remember or (
69
 
                    remember is None and stored_target_branch is None):
70
 
                branch.set_submit_branch(target_branch)
71
 
        if target_branch is None:
72
 
            target_branch = branch.get_parent()
73
 
            remembered_target_branch = "parent"
74
 
        if target_branch is None:
75
 
            raise errors.CommandError(gettext('No submit branch known or'
76
 
                                                 ' specified'))
77
 
        if remembered_target_branch is not None:
78
 
            trace.note(gettext('Using saved {0} location "{1}" to determine '
79
 
                               'what changes to submit.').format(
80
 
                remembered_target_branch,
81
 
                target_branch))
82
 
 
83
 
        submit_branch = Branch.open(target_branch,
84
 
                                    possible_transports=possible_transports)
85
 
        possible_transports.append(submit_branch.controldir.root_transport)
86
 
        if mail_to is None or format is None:
87
 
            if mail_to is None:
88
 
                mail_to = submit_branch.get_config_stack().get(
89
 
                    'child_submit_to')
90
 
            if format is None:
91
 
                formatname = submit_branch.get_child_submit_format()
92
 
                try:
93
 
                    format = format_registry.get(formatname)
94
 
                except KeyError:
95
 
                    raise errors.CommandError(
96
 
                        gettext("No such send format '%s'.") % formatname)
97
 
 
98
 
        stored_public_branch = branch.get_public_branch()
99
 
        if public_branch is None:
100
 
            public_branch = stored_public_branch
101
 
        # Remembers if asked explicitly or no previous location is set
102
 
        elif (remember
103
 
              or (remember is None and stored_public_branch is None)):
104
 
            branch.set_public_branch(public_branch)
105
 
        if no_bundle and public_branch is None:
106
 
            raise errors.CommandError(gettext('No public branch specified or'
107
 
                                                 ' known'))
108
 
        base_revision_id = None
109
 
        revision_id = None
110
 
        if revision is not None:
111
 
            if len(revision) > 2:
112
 
                raise errors.CommandError(gettext('bzr send takes '
113
 
                                                     'at most two one revision identifiers'))
114
 
            revision_id = revision[-1].as_revision_id(branch)
115
 
            if len(revision) == 2:
116
 
                base_revision_id = revision[0].as_revision_id(branch)
117
 
        if revision_id is None:
118
 
            if tree is not None:
119
 
                tree.check_changed_or_out_of_date(
120
 
                    strict, 'send_strict',
121
 
                    more_error='Use --no-strict to force the send.',
122
 
                    more_warning='Uncommitted changes will not be sent.')
123
 
            revision_id = branch.last_revision()
124
 
        if revision_id == NULL_REVISION:
125
 
            raise errors.CommandError(gettext('No revisions to submit.'))
126
 
        if format is None:
127
 
            format = format_registry.get()
128
 
        directive = format(branch, revision_id, target_branch,
129
 
                           public_branch, no_patch, no_bundle, message, base_revision_id,
130
 
                           submit_branch)
131
 
        if output is None:
132
 
            directive.compose_merge_request(mail_client, mail_to, body,
133
 
                                            branch, tree)
134
 
        else:
135
 
            if directive.multiple_output_files:
136
 
                if output == '-':
137
 
                    raise errors.CommandError(gettext('- not supported for '
138
 
                                                         'merge directives that use more than one output file.'))
139
 
                if not os.path.exists(output):
140
 
                    os.mkdir(output, 0o755)
141
 
                for (filename, lines) in directive.to_files():
142
 
                    path = os.path.join(output, filename)
143
 
                    with open(path, 'wb') as outfile:
144
 
                        outfile.writelines(lines)
145
 
            else:
146
 
                if output == '-':
147
 
                    outfile = to_file
148
 
                else:
149
 
                    outfile = open(output, 'wb')
150
 
                try:
151
 
                    outfile.writelines(directive.to_lines())
152
 
                finally:
153
 
                    if outfile is not to_file:
154
 
                        outfile.close()
155
 
 
156
 
 
157
 
def _send_4(branch, revision_id, target_branch, public_branch,
158
 
            no_patch, no_bundle, message,
159
 
            base_revision_id, local_target_branch=None):
160
 
    from breezy import merge_directive
161
 
    return merge_directive.MergeDirective2.from_objects(
162
 
        branch.repository, revision_id, time.time(),
163
 
        osutils.local_time_offset(), target_branch,
164
 
        public_branch=public_branch,
165
 
        include_patch=not no_patch,
166
 
        include_bundle=not no_bundle, message=message,
167
 
        base_revision_id=base_revision_id,
168
 
        local_target_branch=local_target_branch)
169
 
 
170
 
 
171
 
def _send_0_9(branch, revision_id, submit_branch, public_branch,
172
 
              no_patch, no_bundle, message,
173
 
              base_revision_id, local_target_branch=None):
174
 
    if not no_bundle:
175
 
        if not no_patch:
176
 
            patch_type = 'bundle'
177
 
        else:
178
 
            raise errors.CommandError(gettext('Format 0.9 does not'
179
 
                                                 ' permit bundle with no patch'))
180
 
    else:
181
 
        if not no_patch:
182
 
            patch_type = 'diff'
183
 
        else:
184
 
            patch_type = None
185
 
    from breezy import merge_directive
186
 
    return merge_directive.MergeDirective.from_objects(
187
 
        branch.repository, revision_id, time.time(),
188
 
        osutils.local_time_offset(), submit_branch,
189
 
        public_branch=public_branch, patch_type=patch_type,
190
 
        message=message, local_target_branch=local_target_branch)
191
 
 
192
 
 
193
 
format_registry.register('4',
194
 
                         _send_4, 'Bundle format 4, Merge Directive 2 (default)')
195
 
format_registry.register('0.9',
196
 
                         _send_0_9, 'Bundle format 0.9, Merge Directive 1')
197
 
format_registry.default_key = '4'