1
# Copyright (C) 2005, 2006, 2007 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
17
"""Sending emails for commits and branch changes.
19
To have bzr send an email you need to configure an address to send mail
20
to for that branch. To do this set the configuration option ``post_commit_to``
21
and the address to send the mail from is read from the configuration option
22
``post_commit_sender`` (if not supplied defaults to the email address reported
25
By default, the diff for the commit will be included in the email if the
26
length is less than 1000 lines. This limit can be changed by setting the
27
configuration option 'post_commit_difflimit' to the number of lines you wish
28
it to be limited to. Set it to 0 to unconditionally disable sending of diffs.
30
By default bzr-email only emails when a commit occurs, not when a push or
31
pull operation occurs. To email on push or pull set post_commit_push_pull=True
34
If you are using a bzr release from before 0.15, you need to manually tell
35
bzr about the commit action, by setting
36
post_commit=breezy.plugins.email.post_commit in bazaar.conf or locations.conf.
38
The URL of the branch is determined from the following checks (in order):
39
- If the configuration value 'post_commit_url' is set, it is used.
40
- If the configuration value 'public_branch' is set, it is used.
41
- The URL of the branch itself.
43
Setting public_branch is highly recommended if you commit via a protocol which
44
has a private address (e.g. bzr+ssh but anonymous access might be bzr:// or
47
How emails are sent is determined by the value of the configuration option
49
- Unset: use ``/usr/bin/mail``.
50
- ``smtplib``: Use python's smtplib to send the mail. If you use 'smtplib' you
51
can also configure the settings "smtp_server=host[:port]",
52
"smtp_username=userid", "smtp_password". If "smtp_username" is set but
53
"smtp_password" is not, you will be prompted for a password.
55
Also, if using 'smtplib', the messages will be sent as a UTF-8 text message,
56
with a 8-bit text diff attached (rather than all-as-one). Work has also been
57
done to make sure usernames do not have to be ascii.
58
- Any other value: Run the value expecting it to behave like ``/usr/bin/mail``
59
- in particular supporting the -s and -a options.
61
When using smtplib, you can specify additional headers to be included in the
62
mail by setting the 'revision_mail_headers' configuration option - something
65
revision_mail_headers=X-Cheese: to the rescue!
68
from __future__ import absolute_import
70
from ...config import option_registry
71
from ...lazy_import import lazy_import
73
# lazy_import emailer so that it doesn't get loaded if it isn't used
74
lazy_import(globals(), """\
75
from breezy.plugins.email import emailer as _emailer
79
def post_commit(branch, revision_id):
80
"""This is the post_commit hook that should get run after commit."""
81
_emailer.EmailSender(branch, revision_id, branch.get_config_stack()).send_maybe()
84
def branch_commit_hook(local, master, old_revno, old_revid, new_revno, new_revid):
85
"""This is the post_commit hook that runs after commit."""
86
_emailer.EmailSender(master, new_revid, master.get_config_stack(),
87
local_branch=local).send_maybe()
90
def branch_post_change_hook(params):
91
"""This is the post_change_branch_tip hook."""
92
# (branch, old_revno, new_revno, old_revid, new_revid)
93
_emailer.EmailSender(params.branch, params.new_revid,
94
params.branch.get_config_stack(), local_branch=None, op='change').send_maybe()
98
from unittest import TestSuite
99
from .tests import test_suite
101
result.addTest(test_suite())
105
option_registry.register_lazy("post_commit_body",
106
"breezy.plugins.email.emailer", "opt_post_commit_body")
107
option_registry.register_lazy("post_commit_subject",
108
"breezy.plugins.email.emailer", "opt_post_commit_subject")
109
option_registry.register_lazy("post_commit_log_format",
110
"breezy.plugins.email.emailer", "opt_post_commit_log_format")
111
option_registry.register_lazy("post_commit_difflimit",
112
"breezy.plugins.email.emailer", "opt_post_commit_difflimit")
113
option_registry.register_lazy("post_commit_push_pull",
114
"breezy.plugins.email.emailer", "opt_post_commit_push_pull")
115
option_registry.register_lazy("post_commit_diffoptions",
116
"breezy.plugins.email.emailer", "opt_post_commit_diffoptions")
117
option_registry.register_lazy("post_commit_sender",
118
"breezy.plugins.email.emailer", "opt_post_commit_sender")
119
option_registry.register_lazy("post_commit_to",
120
"breezy.plugins.email.emailer", "opt_post_commit_to")
121
option_registry.register_lazy("post_commit_mailer",
122
"breezy.plugins.email.emailer", "opt_post_commit_mailer")
123
option_registry.register_lazy("revision_mail_headers",
124
"breezy.plugins.email.emailer", "opt_revision_mail_headers")
127
from ...hooks import install_lazy_named_hook
129
from ...branch import Branch
130
Branch.hooks.install_named_hook('post_commit', branch_commit_hook, 'bzr-email')
131
Branch.hooks.install_named_hook('post_change_branch_tip', branch_post_change_hook, 'bzr-email')
133
install_lazy_named_hook("breezy.branch", "Branch.hooks", 'post_commit',
134
branch_commit_hook, 'bzr-email')
135
install_lazy_named_hook("breezy.branch", "Branch.hooks",
136
'post_change_branch_tip', branch_post_change_hook, 'bzr-email')