SVN Post Commit

Steve Popoola —  May 24, 2012 — 1 Comment

When working with a team of developers on the same project, you want to be abreast of what others are doing. One nifty way of acheiving this is through the use of post commit hooks. These hooks are available in most Version Control Systems but in this blog, I will be showing you how to create one in Subversion.

In your svn repository directory is one named ‘hooks’. This is the where all the various hooks that subversion executes if enabled. One of the hooks we will be looking at is the post-commit hook. The file name should be, you guessed right, post-commit.

Let us set up a post-commit hook to send emails once a commit is made to the repository.

In your hooks directory is a file called post-commit.tmpl. Rename this to post-commit.

Replace the contents of the file with the following;
REPOS="$1" # repository path
REV="$2" # revision number
echo "successfully committed to $REV"

This will send the above message back to the client once the file(s) is/are successfully committed.

What if you wanted to send email notification? Easy, all you need is svnnotify.

For you to use svnnotify, it must be installed first. For Ubuntu Linux, you can do the following;

sudo apt-get install libsvn-notify-perl

You also need to ensure that you have sendmail set up. If you issue the command;

which sendmail

You should see something like /usr/sbin/sendmail. If not, then you need to install sendmail with sudo apt-get install sendmail
With that out of the way, we are now ready to add email notification to our post-commit hook
Open your post-commit file;

sudo vi post-commit

Add the following;

TO="me@myteam.com" # notifications email
FROM="myrep@myteam.com" # "FROM" fields
svnnotify --repos-path "$REPOS" --revision "$REV" --to "$TO" --from "$FROM" --with-diff --handler HTML::ColorDiff --subject-cx --subject-prefix 'MYREPO'

Voila! You have your post-commit email with a nice, well formatted diff of your files.

When I did this in my team, I noticed that svn commits were slow to complete because it was waiting for the post-commit to complete. I got around this by adding a delay to the email notification so that svnnotify runs after the commit is complete.

I did this by wrapping the svnnotify in a function call as follows;

delayed_notify( ) {
sleep 10
svnnotify --repos-path "$REPOS" --revision "$REV" --to "$TO" --from "$FROM" --with-diff --handler HTML::ColorDiff --subject-cx --subject-prefix 'MYREPO'
delayed_notify $1 $2 > /dev/null 2>&1 &

You can test by running the script from the command line. The number is the last revision of the repository;
./post-commit /var/svn/repos/myrepo/ 20

Steve Popoola

Posts Google+

One response to SVN Post Commit

  1. I have installed DraftSight on Ubuntu 11.10 (32bit) using tenirmal (the program installer stopped during installation). When I am starting the program the graphics window don’t show, however it seems like i can draw on the drawing i don’t see.1. how can i get the graphic window visible?2. how can i uninstall DraftSight in case i can’t make it work? I have tried but with no success

Leave a Reply

*

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>