40
40
_smtplib_implementation = SMTPConnection
42
42
def __init__(self, branch, revision_id, config, local_branch=None,
44
44
self.config = config
45
45
self.branch = branch
46
46
self.repository = branch.repository
47
47
if (local_branch is not None and
48
local_branch.repository.has_revision(revision_id)):
48
local_branch.repository.has_revision(revision_id)):
49
49
self.repository = local_branch.repository
50
50
self._revision_id = revision_id
51
51
self.revision = None
126
126
revid_new = self.revision.revision_id
127
127
if self.revision.parent_ids:
128
128
revid_old = self.revision.parent_ids[0]
129
tree_new, tree_old = self.repository.revision_trees(
130
(revid_new, revid_old))
129
tree_new, tree_old = self.repository.revision_trees((revid_new, revid_old))
132
131
# revision_trees() doesn't allow None or 'null:' to be passed as a
133
132
# revision. So we need to call revision_tree() twice.
141
140
diff_content = StringIO()
142
141
diff_options = self.config.get('post_commit_diffoptions')
143
142
show_diff_trees(tree_old, tree_new, diff_content, None, diff_options)
144
numlines = diff_content.getvalue().count('\n') + 1
143
numlines = diff_content.getvalue().count('\n')+1
145
144
if numlines <= difflimit:
146
145
return diff_content.getvalue()
213
212
"""Spawn a 'mail' subprocess to send the email."""
214
213
# TODO think up a good test for this, but I think it needs
215
214
# a custom binary shipped with. RBC 20051021
216
with tempfile.NamedTemporaryFile() as msgfile:
215
msgfile = tempfile.NamedTemporaryFile()
217
217
msgfile.write(self.body().encode('utf8'))
218
218
diff = self.get_diff()
224
224
process = subprocess.Popen(self._command_line(),
225
stdin=msgfile.fileno())
225
stdin=msgfile.fileno())
227
227
rc = process.wait()
229
raise errors.BzrError(
230
"Failed to send email: exit status %s" % (rc,))
229
raise errors.BzrError("Failed to send email: exit status %s" % (rc,))
232
233
def _send_using_smtplib(self):
233
234
"""Use python's smtplib to send the email."""
268
269
def subject(self):
269
270
_subject = self.config.get('post_commit_subject')
270
271
if _subject is None:
271
_subject = ("Rev %d: %s in %s" %
273
self.revision.get_summary(),
272
_subject = ("Rev %d: %s in %s" %
274
self.revision.get_summary(),
275
276
return self._format(_subject)
277
278
def diff_filename(self):
281
282
opt_post_commit_body = Option("post_commit_body",
282
help="Body for post commit emails.")
283
help="Body for post commit emails.")
283
284
opt_post_commit_subject = Option("post_commit_subject",
284
help="Subject for post commit emails.")
285
help="Subject for post commit emails.")
285
286
opt_post_commit_log_format = Option('post_commit_log_format',
286
default='long', help="Log format for option.")
287
default='long', help="Log format for option.")
287
288
opt_post_commit_difflimit = Option('post_commit_difflimit',
288
default=1000, from_unicode=int_from_store,
289
help="Maximum number of lines in diffs.")
289
default=1000, from_unicode=int_from_store,
290
help="Maximum number of lines in diffs.")
290
291
opt_post_commit_push_pull = Option('post_commit_push_pull',
291
from_unicode=bool_from_store,
292
help="Whether to send emails on push and pull.")
292
from_unicode=bool_from_store,
293
help="Whether to send emails on push and pull.")
293
294
opt_post_commit_diffoptions = Option('post_commit_diffoptions',
294
help="Diff options to use.")
295
help="Diff options to use.")
295
296
opt_post_commit_sender = Option('post_commit_sender',
296
help='From address to use for emails.')
297
help='From address to use for emails.')
297
298
opt_post_commit_to = ListOption('post_commit_to',
298
help='Address to send commit emails to.')
299
help='Address to send commit emails to.')
299
300
opt_post_commit_mailer = Option('post_commit_mailer',
300
help='Mail client to use.', default='mail')
301
help='Mail client to use.', default='mail')
301
302
opt_post_commit_url = Option('post_commit_url',
302
help='URL to mention for branch in post commit messages.')
303
help='URL to mention for branch in post commit messages.')
303
304
opt_revision_mail_headers = ListOption('revision_mail_headers',
304
help="Extra revision headers.")
305
help="Extra revision headers.")