Email Notification of SVN Commits

To get an automatic email from a Subversion (SVN) server when a commit occurs on a repository you can use a post-commit hook on the SVN server. Hooks allow you (the server administrator) to execute a program or script when a particular event occurs in a repository on the SVN server (e.g. a commit, lock or revision properties change). So to get an email after a commit occurs, we need to call a program to create an email on the post-commit hook.

In the following examples my server has domain sandilands.info, is running Ubuntu Server 10.04 LTS and includes a Postfix email server. Change the directories, repositories and email addresses to suit your server.

Creating a Email Notify Hook

On the SVN server, example templates for hooks can be found in the hooks directory of the SVN repository. For example, my repositories are stored in the /var/svn directory. So for my Steve repository the templates are in the directory:

/var/svn/Steve/hooks/

To use an existing template, rename it, removing the .tmpl suffix:

$ sudo cp post-commit.tmpl post-commit

Now make the hook executable and owned by the web server:

$ sudo chown www-data.www-data post-commit
$ sudo chmod u+x post-commit

Now the main part is to edit the hook (which is just a Shell script) to call a program to send an email. If you install subversion-tools there is a Perl script that will do it for you: /usr/share/subversion/hook-scripts/commit-email.pl.

$ sudo apt-get install subversion-tools

Edit post-commit removing the example line calling mailer.py and adding a call to commit-email.pl. For example, the last 5 lines of my post-commit are:

REPOS="$1"
REV="$2"

# "$REPOS"/hooks/mailer.py commit "$REPOS" $REV "$REPOS"/mailer.conf
/usr/share/subversion/hook-scripts/commit-email.pl "$REPOS" "$REV" svn-commit-notify@sandilands.info -s "SVN Steve:" --from svn-administrator@sandilands.info

The first two lines above set variables, REPOS and REV, for the repository name and current revision, respectively. I commented out the example line that called mailer.py by starting the line with a #. Then the last line I added. It calls commit-email.pl which takes several input arguments, some of which are optional. The first two are the repository name and revision. The third is the destination email address. The remaining are optional. The -s "SVN Steve:" adds the given string to to the start of the email subject. The --from sets to from address in the email. For other options, look inside the Perl code itself in commit-email.pl: start at line 145 you can see the different options.

Now, when ever there is a commit on the repository, an email will be sent to the selected destination address summarising the commit, including revision number, log message and diff between the head and most recent revision.

Email Aliases

If you want to send an email to multiple people, then you can create an alias. Add a line like the following to the file /etc/aliases:

svn-commit-notify:      user1@example.com, user2@hotmail.com

and then run:

$ sudo newaliases

Now when the commit occurs an email is sent to svn-commit-notify@sandilands.info, but this expands to two email address, user1@example.com and user2@hotmail.com.