SVN Commit Email on Linux

If you are using subversion(svn), you may or may not know that it has a pluggable architecture that allows you to automate certain aspects of the workflow with hook scripts. These hook scripts are written in bash on Linux and batch files on Windows, today we will be focusing on bash. Below are some basic guidelines about hook scripts found on the Wandisco blog here.

What can hook scripts do?

  • Report on events, such as emailing alerts when a commit occurs, sending a report on the commit to a third-party.
  • Test for a pre-condition before allowing a commit to occur. For example, checking that a commit has been commented, or follows coding guidelines.
  • Block certain actions. Maybe prevent a commit that contains multi-gigabyte files that could block other SVN traffic for hours, or prevent locks from being stolen.

What hook scripts can’t (shouldn’t) do

  • Hook scripts shouldn’t modify files being committed. Right now, if a hook script actually changed a commit transaction, there’s no way of communicating the change back to the client. So while it might be tempting to write a script that automatically corrects errors or applies positive changes to a committed file, it will result in two different versions of the file in the same revision one version written to the repository and another in the user’s cache. The result of this loss of consistency can be as horrible as it is unpredictable.

Below is a script that will email your development team every time code is committed to a repository. Note that this is not the actual hook script, but it is a helper script that can be called by the post-commit hook script. On my setup, I like to have the subversion root at /srv/svn and store all my helper scripts at /srv/svn/scripts.  Below is the helper script svn_email_commit.

SENDFROM='SVN Automation <>'

CHANGELOG=`svnlook log -r $REV $REPOS`
AUTHOR=`svnlook author -r $REV $REPOS`
CHANGED=`svnlook changed -r $REV $REPOS`
DIFF=`svnlook diff -r $REV $REPOS | head --lines=$LIMITDIFF`

echo "-------------------- SVN Commit Notification --------------------
Repository: $REPOS
Revision:   $REV
Author:     $AUTHOR
Date:       $DATE

Log Message:


Diff: (only first $LIMITDIFF lines shown)

# Send email
cat $TMPFILE | mail -a "From: $SENDFROM" -s "$SUBJECT" "$SENDTO"

# Cleanup

Once you have the svn_email_commit script in the correct folder, you will need to ensure it is executable with the following command.

chmod +x /srv/svn/scripts/svn_email_commit

Next you will edit the script below, which is the post-commit hook, and place it in the hooks directory inside your subversion repository. Again this script will need to be made executable in order to run.


# Send it to people in SENTTO above
/srv/svn/scripts/svn_email_commit "$REPOS" "$REV" "$SENDTO"

Below you can download the zip containing both the email helper script and the post-commit hook.

download-icon SVN Email Download

Did you find this article useful? Why not share it with your friends?

Leave a Reply

Your email address will not be published.

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