~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/send.py

  • Committer: Martin Packman
  • Date: 2011-10-06 16:41:45 UTC
  • mfrom: (6015.33.10 2.4)
  • mto: This revision was merged to the branch mainline in revision 6202.
  • Revision ID: martin.packman@canonical.com-20111006164145-o98oqn32440extgt
Merge 2.4 into dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009 Canonical Ltd
 
1
# Copyright (C) 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
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
 
18
import os
18
19
import time
19
20
 
20
21
from bzrlib import (
21
22
    bzrdir,
22
23
    errors,
23
 
    merge_directive,
24
24
    osutils,
25
25
    registry,
26
26
    trace,
27
27
    )
 
28
from bzrlib.i18n import gettext
28
29
from bzrlib.branch import (
29
30
    Branch,
30
31
    )
37
38
 
38
39
 
39
40
def send(submit_branch, revision, public_branch, remember, format,
40
 
         no_bundle, no_patch, output, from_, mail_to, message, body, 
41
 
         to_file):
 
41
         no_bundle, no_patch, output, from_, mail_to, message, body,
 
42
         to_file, strict=None):
42
43
    tree, branch = bzrdir.BzrDir.open_containing_tree_or_branch(from_)[:2]
43
44
    # we may need to write data into branch's repository to calculate
44
45
    # the data to send.
51
52
            mail_client = config.get_mail_client()
52
53
            if (not getattr(mail_client, 'supports_body', False)
53
54
                and body is not None):
54
 
                raise errors.BzrCommandError(
55
 
                    'Mail client "%s" does not support specifying body' %
 
55
                raise errors.BzrCommandError(gettext(
 
56
                    'Mail client "%s" does not support specifying body') %
56
57
                    mail_client.__class__.__name__)
57
58
        if remember and submit_branch is None:
58
 
            raise errors.BzrCommandError(
59
 
                '--remember requires a branch to be specified.')
 
59
            raise errors.BzrCommandError(gettext(
 
60
                '--remember requires a branch to be specified.'))
60
61
        stored_submit_branch = branch.get_submit_branch()
61
62
        remembered_submit_branch = None
62
63
        if submit_branch is None:
63
64
            submit_branch = stored_submit_branch
64
65
            remembered_submit_branch = "submit"
65
66
        else:
66
 
            if stored_submit_branch is None or remember:
 
67
            # Remembers if asked explicitly or no previous location is set
 
68
            if remember or (remember is None and stored_submit_branch is None):
67
69
                branch.set_submit_branch(submit_branch)
68
70
        if submit_branch is None:
69
71
            submit_branch = branch.get_parent()
70
72
            remembered_submit_branch = "parent"
71
73
        if submit_branch is None:
72
 
            raise errors.BzrCommandError('No submit branch known or'
73
 
                                         ' specified')
 
74
            raise errors.BzrCommandError(gettext('No submit branch known or'
 
75
                                         ' specified'))
74
76
        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)
 
77
            trace.note(gettext('Using saved {0} location "{1}" to determine '
 
78
                       'what changes to submit.').format(
 
79
                                    remembered_submit_branch, submit_branch))
78
80
 
79
81
        if mail_to is None or format is None:
 
82
            # TODO: jam 20090716 we open the submit_branch here, but we *don't*
 
83
            #       pass it down into the format creation, so it will have to
 
84
            #       open it again
80
85
            submit_br = Branch.open(submit_branch)
81
86
            submit_config = submit_br.get_config()
82
87
            if mail_to is None:
86
91
                try:
87
92
                    format = format_registry.get(formatname)
88
93
                except KeyError:
89
 
                    raise errors.BzrCommandError("No such send format '%s'." % 
 
94
                    raise errors.BzrCommandError(gettext("No such send format '%s'.") % 
90
95
                                                 formatname)
91
96
 
92
97
        stored_public_branch = branch.get_public_branch()
93
98
        if public_branch is None:
94
99
            public_branch = stored_public_branch
95
 
        elif stored_public_branch is None or remember:
 
100
        # Remembers if asked explicitly or no previous location is set
 
101
        elif (remember
 
102
              or (remember is None and stored_public_branch is None)):
96
103
            branch.set_public_branch(public_branch)
97
104
        if no_bundle and public_branch is None:
98
 
            raise errors.BzrCommandError('No public branch specified or'
99
 
                                         ' known')
 
105
            raise errors.BzrCommandError(gettext('No public branch specified or'
 
106
                                         ' known'))
100
107
        base_revision_id = None
101
108
        revision_id = None
102
109
        if revision is not None:
103
110
            if len(revision) > 2:
104
 
                raise errors.BzrCommandError('bzr send takes '
105
 
                    'at most two one revision identifiers')
 
111
                raise errors.BzrCommandError(gettext('bzr send takes '
 
112
                    'at most two one revision identifiers'))
106
113
            revision_id = revision[-1].as_revision_id(branch)
107
114
            if len(revision) == 2:
108
115
                base_revision_id = revision[0].as_revision_id(branch)
109
116
        if revision_id is None:
 
117
            if tree is not None:
 
118
                tree.check_changed_or_out_of_date(
 
119
                    strict, 'send_strict',
 
120
                    more_error='Use --no-strict to force the send.',
 
121
                    more_warning='Uncommitted changes will not be sent.')
110
122
            revision_id = branch.last_revision()
111
123
        if revision_id == NULL_REVISION:
112
 
            raise errors.BzrCommandError('No revisions to submit.')
 
124
            raise errors.BzrCommandError(gettext('No revisions to submit.'))
113
125
        if format is None:
114
 
            # TODO: Query submit branch for its preferred format
115
126
            format = format_registry.get()
116
 
        directive = format(branch, revision_id, submit_branch, 
 
127
        directive = format(branch, revision_id, submit_branch,
117
128
            public_branch, no_patch, no_bundle, message, base_revision_id)
118
129
        if output is None:
119
130
            directive.compose_merge_request(mail_client, mail_to, body,
120
131
                                            branch, tree)
121
132
        else:
122
 
            if output == '-':
123
 
                outfile = to_file
 
133
            if directive.multiple_output_files:
 
134
                if output == '-':
 
135
                    raise errors.BzrCommandError(gettext('- not supported for '
 
136
                        'merge directives that use more than one output file.'))
 
137
                if not os.path.exists(output):
 
138
                    os.mkdir(output, 0755)
 
139
                for (filename, lines) in directive.to_files():
 
140
                    path = os.path.join(output, filename)
 
141
                    outfile = open(path, 'wb')
 
142
                    try:
 
143
                        outfile.writelines(lines)
 
144
                    finally:
 
145
                        outfile.close()
124
146
            else:
125
 
                outfile = open(output, 'wb')
126
 
            try:
127
 
                outfile.writelines(directive.to_lines())
128
 
            finally:
129
 
                if outfile is not to_file:
130
 
                    outfile.close()
 
147
                if output == '-':
 
148
                    outfile = to_file
 
149
                else:
 
150
                    outfile = open(output, 'wb')
 
151
                try:
 
152
                    outfile.writelines(directive.to_lines())
 
153
                finally:
 
154
                    if outfile is not to_file:
 
155
                        outfile.close()
131
156
    finally:
132
157
        branch.unlock()
133
158
 
134
159
 
135
160
def _send_4(branch, revision_id, submit_branch, public_branch,
136
161
            no_patch, no_bundle, message, base_revision_id):
 
162
    from bzrlib import merge_directive
137
163
    return merge_directive.MergeDirective2.from_objects(
138
164
        branch.repository, revision_id, time.time(),
139
165
        osutils.local_time_offset(), submit_branch,
148
174
        if not no_patch:
149
175
            patch_type = 'bundle'
150
176
        else:
151
 
            raise errors.BzrCommandError('Format 0.9 does not'
152
 
                ' permit bundle with no patch')
 
177
            raise errors.BzrCommandError(gettext('Format 0.9 does not'
 
178
                ' permit bundle with no patch'))
153
179
    else:
154
180
        if not no_patch:
155
181
            patch_type = 'diff'
156
182
        else:
157
183
            patch_type = None
 
184
    from bzrlib import merge_directive
158
185
    return merge_directive.MergeDirective.from_objects(
159
186
        branch.repository, revision_id, time.time(),
160
187
        osutils.local_time_offset(), submit_branch,