Load WordPress Sites in as fast as 37ms!

Sending an email upon SVN-commit

When you work with a group of people on the same projects, the larger the group gets, the more difficult the task of keeping everybody informed. So why not do this automatically? Send an e-mail upon an SVN commit. Here’s how I do it.

First, we need a script to send the actual message. Now please keep in mind that I do all this on an OSX machine and Apple sometimes tinkers with available parameters to certain commands.

Also note that I am running my SVN server as Apache Module and following instruction apply to that situation.

In this script the command

is used and Apple’s implementation lacks the -a parameter. Here’s the script;

Shell-Script (download file)#!/bin/bash
REPOPATH=$1
REV=$2
SENDTO=send-to-me@mail.tld
SENDFROM=send-as-this@mail.tld
REPLYTO=replies-go-here@mail.tld

LIMITDIFF=200
CHANGELOG=`svnlook log -r $REV $REPOPATH`
AUTHOR=`svnlook author -r $REV $REPOPATH`
CHANGED=`svnlook changed -r $REV $REPOPATH`
DIFF=`svnlook diff -r $REV $REPOPATH | head -n $LIMITDIFF`
DATE=`date`
TMPFILE=/tmp/svn$REV-$RANDOM.message

SUBJECT="SVNCommit ($AUTHOR) $REPOPATH [$REV]"
echo "-------------------- SVN Commit Notification --------------------
Repository: $REPOPATH
Revision:   $REV
Author:     $AUTHOR
Date:       $DATE

-----------------------------------------------------------------
Log Message:
-----------------------------------------------------------------
$CHANGELOG

-----------------------------------------------------------------
Changes:
-----------------------------------------------------------------
$CHANGED

-----------------------------------------------------------------
Diff: (only first $LIMITDIFF lines shown)
-----------------------------------------------------------------
$DIFF
" > $TMPFILE

# Send email
# use hack as shown here: http://stackoverflow.com/a/11656356
# apple's mail command does not allow extra headers with -a, so we insert the headers into the subject
/usr/bin/mail -s "$(echo -e "$SUBJECT\nFrom: $SENDFROM\nReply-to: $REPLYTO\n")" "$SENDTO" < $TMPFILE

# Cleanup
rm $TMPFILE

Now, this script must be executable by the webserver., so this file should either be 750 or 755, depending on your apache configuration. In my case, Apache runs as _www:wheel, and my group is wheel as well, so 750 will do.

Second part is informing SVN to use this script.

This is a bit tricky, but with some investigation, it’s not hard.

We’ll need to know the subversion repository path. If you set it up yourself, you will know, in my case, I have one SVN repo at /Volumes/SVN/myrepository. This folder contains SVN files and folders, and one of those is hooks. If you take a look in this folder, you’ll see template files, like post-commit.tmpl. If you don’t, you’re in the wrong place!

Create a file there called post-commit (same as the template, but no extention) with the following contents:

Shell-Script (download file)#!/bin/sh
/path/to/your/svn_send_email.sh "$1" "$2"

Be sure this file is owned by the same user and group the repository is owned by, in my case _www:wheel. If you had to use sudo to create the file this is very important. Also give this file 750 or 755 depending on your configuration.

Third – and last – restart Apache.

Shell commandsudo apachectl restart

Please note:

You will have to replace some values and paths in both scripts.

Also note the use of a hack in the first script; as MoSs explains on StackOverflow. Thank you!

Author: Remon Pel

WebDeveloper though not WebDesigner

2 thoughts on “Sending an email upon SVN-commit”

  1. Next thing is to install the code base on a testing location and only allow the commit if certain selenium test cases succeed! :-)

    1. Great idea. Also been thinking of using the signal to figure out which project is changed and checking to see what developers worked on it and send those users a mail, instead of a fixed list.

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.