Monthly Archives: March 2010

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

Restore Your SSH Session Working Directory

The Problem

I want to login to a remote Linux machine via SSH and to be in the same directory I was before my last log out.

The Solution

Since my login shell is bash, I present this solution in bash, but you can adapt it for your favorite shells. This tip relies on the two special files ~/.bash_profile and ~/.bash_logout. When a user logs out of a Linux system, the login shell (bash in this case) executes the ~/.bash_logout file, which is where we save our working directory:

# Contents of .bash_logout
rm -f $LASTDIRFILE
echo cd $PWD > $LASTDIRFILE

Likewise, we a user logs in, bash execute .bash_profile, so we put the instruction to restore the working directory there:

# Contents of .bash_profile
# ...

# Restore last directory
export LASTDIRFILE=~/.lastdir
test -f $LASTDIRFILE && source $LASTDIRFILE

Conclusion

The ~/.bash_logout is a wonderful file for saving your session’s details and its counterpart, ~/.bash_profile, is good for restoring them.