Skip to content

Instantly share code, notes, and snippets.

@freyes
Created January 31, 2013 20:05
Show Gist options
  • Save freyes/4685953 to your computer and use it in GitHub Desktop.
Save freyes/4685953 to your computer and use it in GitHub Desktop.
A simple mercurial plugin to prepare a mail template with the diff of a given revision (by default tip) and then the mail is sent via localhost
# one line to give the program's name and an idea of what it does.
# Copyright (C) 2013 Felipe Reyes <freyes@tty.cl>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import os
import tempfile
import mercurial
import email
import smtplib
from mercurial import util
from mercurial.i18n import gettext as _
HEADER_TPL = """From: %(from)s
To: %(to)s
Cc:
Bcc:
Subject: Diff Review: %(subject)s
Reply-To:
"""
def review(ui, repo, **opts):
ctx = repo[opts["rev"]]
tmpf = tempfile.NamedTemporaryFile()
tplvars = {"subject": ctx.description(),
"to": ctx.user(),
"from": ui.username()}
tmpf.write(HEADER_TPL % tplvars)
lines = ""
for l in ctx.diff():
lines += l
lines = lines.split("\n")
for line in lines:
tmpf.write("> ")
tmpf.write(line)
tmpf.write("\n")
tmpf.flush()
tmpf.seek(0)
modified = ui.edit(tmpf.read(), ui.username())
tmpf.seek(0)
if modified == tmpf.read():
raise util.Abort("no changes made, aborting")
# choice = ui.promptchoice("Send email?", "((&y), (&n))", default="n")
# if choice == "n":
# return 1
tmpf.seek(0)
sendmail(tmpf.read())
def sendmail(raw_msg):
msg = email.message_from_string(raw_msg)
# TODO: use configuration file
server = smtplib.SMTP('localhost')
server.sendmail(msg["From"], msg["To"], raw_msg)
server.quit()
cmdtable = {
"codereview": (review, [
('r', 'rev', 'tip', _('revision that you want to review'), 'string'),
])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment