1
# Copyright (C) 2005, 2006, 2007 by Canonical Ltd
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
"""Submit an email to a Patch Queue Manager"""
19
config as _mod_config,
25
from bzrlib.branch import Branch
26
from bzrlib.email_message import EmailMessage
27
from bzrlib.smtp_connection import SMTPConnection
28
from bzrlib.trace import note, warning
31
class BadCommitMessage(errors.BzrError):
33
_fmt = "The commit message %(msg)r cannot be used by pqm."
35
def __init__(self, message):
36
errors.BzrError.__init__(self)
40
class PQMEmailMessage(EmailMessage):
41
"""PQM doesn't support proper email subjects, so we hack around it."""
43
def __init__(self, from_address, to_address, subject, body=None):
44
EmailMessage.__init__(self, from_address=from_address,
45
to_address=to_address, subject=subject,
47
# Now override self.Subject to use raw utf-8
48
self._headers['Subject'] = osutils.safe_unicode(subject).encode('UTF-8')
51
class PQMSubmission(object):
52
"""A request to perform a PQM merge into a branch."""
54
def __init__(self, source_branch, public_location=None,
55
submit_location=None, message=None,
57
"""Create a PQMSubmission object.
59
:param source_branch: the source branch for the merge
60
:param public_location: the public location of the source branch
61
:param submit_location: the location of the target branch
62
:param message: The message to use when committing this merge
63
:param tree: A WorkingTree or None. If not None the WT will be checked
64
for uncommitted changes.
66
If any of public_location, submit_location or message are
67
omitted, they will be calculated from source_branch.
69
if source_branch is None:
70
raise errors.NoMergeSource()
71
self.source_branch = source_branch
74
if public_location is None:
75
public_location = self.source_branch.get_public_branch()
76
# Fall back to the old public_repository hack.
77
if public_location is None:
78
src_loc = source_branch.bzrdir.root_transport.local_abspath('.')
79
repository = source_branch.repository
80
repo_loc = repository.bzrdir.root_transport.local_abspath('.')
81
repo_config = _mod_config.LocationConfig(repo_loc)
82
public_repo = repo_config.get_user_option("public_repository")
83
if public_repo is not None:
84
warning("Please use public_branch, not public_repository, "
85
"to set the public location of branches.")
86
branch_relpath = osutils.relpath(repo_loc, src_loc)
87
public_location = urlutils.join(public_repo, branch_relpath)
89
if public_location is None:
90
raise errors.BzrCommandError(
91
'No public branch location given. Please specify with '
92
'--public-location or see "bzr help pqm-submit" to see how '
93
'to set it in ~/.bazaar/locations.conf')
94
self.public_location = public_location
96
if submit_location is None:
97
config = self.source_branch.get_config()
98
# First check the deprecated pqm_branch config key:
99
submit_location = config.get_user_option('pqm_branch')
100
if submit_location is not None:
101
warning("Please use submit_branch, not pqm_branch to set "
102
"the PQM merge target branch.")
104
# Otherwise, use the standard config key:
105
submit_location = self.source_branch.get_submit_branch()
107
if submit_location is None:
108
raise errors.NoSubmitBranch(self.source_branch)
109
# See if the submit_location has a public branch
111
submit_branch = Branch.open(submit_location)
112
except errors.NotBranchError:
115
submit_public_location = submit_branch.get_public_branch()
116
if submit_public_location is not None:
117
submit_location = submit_public_location
118
self.submit_location = submit_location
120
# Check that the message is okay to pass to PQM
122
repository = self.source_branch.repository
123
rev = repository.get_revision(self.source_branch.last_revision())
124
message = rev.message
125
self.message = message.encode('utf8')
126
if '\n' in self.message:
127
raise BadCommitMessage(self.message)
129
def check_tree(self):
130
"""Check that the working tree has no uncommitted changes."""
131
if self.tree is None:
133
note('Checking the working tree is clean ...')
134
self.tree.lock_read()
136
basis_tree = self.tree.basis_tree()
137
basis_tree.lock_read()
139
for change in self.tree._iter_changes(basis_tree):
140
# If we have any changes, the tree is not clean
141
raise errors.UncommittedChanges(self.tree)
147
def check_public_branch(self):
148
"""Check that the public branch is up to date with the local copy."""
149
note('Checking that the public branch is up to date ...')
150
local_revision = self.source_branch.last_revision()
151
public_revision = Branch.open(self.public_location).last_revision()
152
if local_revision != public_revision:
153
raise errors.PublicBranchOutOfDate(
154
self.public_location, local_revision)
157
"""Serialise as a list of lines."""
158
return ['star-merge %s %s\n' % (self.public_location, self.submit_location)]
161
"""Serialize as a signed string."""
162
unsigned_text = ''.join(self.to_lines())
163
unsigned_text = unsigned_text.encode('ascii') #URLs should be ascii
165
strategy = gpg.GPGStrategy(self.source_branch.get_config())
166
return strategy.sign(unsigned_text)
168
def to_email(self, mail_from, mail_to, sign=True):
169
"""Serialize as an email message.
171
:param mail_from: The from address for the message
172
:param mail_to: The address to send the message to
173
:param sign: If True, gpg-sign the email
174
:return: an email message
177
body = self.to_signed()
179
body = ''.join(self.to_lines())
180
message = PQMEmailMessage(mail_from, mail_to, self.message, body)
184
def submit(branch, message, dry_run=False, public_location=None,
185
submit_location=None, tree=None):
186
"""Submit the given branch to the pqm."""
187
config = branch.get_config()
189
submission = PQMSubmission(
190
source_branch=branch, public_location=public_location, message=message,
191
submit_location=submit_location,
194
mail_from = config.get_user_option('pqm_user_email')
196
mail_from = config.username()
197
mail_from = mail_from.encode('utf8') # Make sure this isn't unicode
198
mail_to = config.get_user_option('pqm_email')
200
raise errors.BzrCommandError('No PQM submission address specified '
202
mail_to = mail_to.encode('utf8') # same here
204
submission.check_tree()
205
submission.check_public_branch()
207
message = submission.to_email(mail_from, mail_to)
210
print message.as_string()
213
SMTPConnection(config).send_email(message)