Automate Email Notifications for Subversion (SVN) Commits

The Problem

I want SVN to automatically send out notifications every time some one commits.

The Solution

I have used svn-mailer before and love its ability to configure the email contents as well as subject. This time I am interested in the DIY approach to learn more about SVN, so I did it without svn-mailer.

The first step is to go to my svn repository (/var/svn in this case) and create a script call post-commit in the hooks directory. Subversion looks for this file, so the name must be exact. The script must be executable. Here it the contents of /var/svn/hook/post-commit:

#!/bin/sh

REPOS="$1"
REV="$2"
AUTHOR=$(svnlook author -r $REV $REPOS)
DATE=$(svnlook date -r $REV $REPOS)

{
    echo "REPOSITORY:   $REPOS"
    echo "REVISION:     $REV"
    echo "COMMITTED BY: $AUTHOR"
    echo "DATE:         $DATE"

    echo ""
    echo "DESCRIPTION:"
    svnlook log -r $REV $REPOS

    echo ""
    echo "FILES:"
    svnlook changed -r $REV $REPOS

} | mail -s "PerfPortal Check In Rev $REV by $AUTHOR" email_alias...

Now, every time someone checks in, Subversion will invoke the post-commit script with the path to the repository as the first parameter and the revision number as the second.

Explanation

The script employs two tools to accomplish its objective: svnlook to retrieve various information regarding the committed revision and mail, the Unix command-line utility to send out email. I assume that your system is set up to allow sending email.

  • Line 3-4 identify the parameters Subversion passes to this script, namely the path to the repository and the revision number.
  • Line 5-6 determine the name of the person who committed the code and on which date
  • Line 9-20 display various information regarding the commitment. These information are piped into the mail command on line 22, thus become the body of the email.
  • Line 16 displays the comment the submitter entered when that person committed the code
  • Line 20 displays a list of files that are affected in this commitment.
  • Line 22 invokes the mail command to send out the notification. The -s flag specifies the subject. Replace email_alias with a list of email aliases separated by spaces.

Conclusion

Enabling Subversion commit notification is straight forward: all you have to do is to create a script which sends email with various information regarding the commitment. The script’s potentials do not limit to sending email, it can start unit tests modules, start build processes, update log files, and other tasks–use your imagination.

Another alternative to writing the script is to use tools such as svn-mailer. However, as with most of the tools, you do need to invest some initial time to learn about the tool to fully exploit its capacity

About these ads

7 thoughts on “Automate Email Notifications for Subversion (SVN) Commits

  1. eeek

    Hi I tried your approach after svn-mailer fail to work. It’s working great but how can I differentiate between different project under this repository? For eg.

    repo/
    – projectA/
    – src
    – …
    – projectB/
    – src
    – …

  2. Hai Post author

    @Georgekutty: I don’t use Windows, I am sorry I don’t know the answer to your question. I hope others can help you out.

  3. jay

    hi

    i noticed when i set the email notifications to .html, it generates logs into a log folder..is there a config setting to delete them after the notification?

    thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s