SVN hooks – receiving HTML formatted emails when SVN commits are made

While setting up Trac as our company source-code repository yesterday, the question arose of “What about cute HTML emails when SVN commits are made?”.

Hmmmm.  Thank goodness for SVN hooks – a wonderfully understated feature of subversion!

Go to your subversion directory (that created by “svnadmin create”), and enter the “hooks” directory.

Create a file, executable and owned by your webserver, called “post-commit”:

touch post-commit
sudo chown www-data:www-data post_commit     # RH and other distros will want apache:apache here
chmod +x post_commit

Enter the following as it’s content (this is a fairly self explanatory shell script):

#!/bin/sh set -e REPOS="$1" REV="$2" TO="[email protected]" FROM="[email protected]" BY=`svnlook author $REPOS -r$REV` MSG=`svnlook log $REPOS -r$REV` CHANGED=`svnlook changed $REPOS -r$REV` DIFF=`svnlook diff $REPOS -r$REV | pygmentize -l diff -f html` STYLE="borland" CSS=`pygmentize -S $STYLE -f html` echo "To: $TO\nFrom: $FROM\nSubject: SVN commit r$REV - ($BY) \"$MSG\"\nContent-Type: text/html; charset=us-ascii\n\n<html>\n<style type='text/css'>\n$CSS</style>\n<body>\n<p><a href='https://your.trac.url.com/trac/changeset/$REV/'>View changeset r$REV on Trac</a></p>\n<h3>Files changed:</h3><pre>$CHANGED</pre>\n<h3>Changes made:</h3><p>$DIFF</p>\n</body>\n</html>" | /usr/sbin/sendmail -t

You’ll need to apt-get or yum install pygmentizer (package named python-pygments on Debian/Ubuntu) to get the cool diff colouring.

And that’s it!  (The “sendmail -t” reference works with Exim on Debian Squeeze, your mileage as always, may vary.)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.