svn pre-commit hook to ensure a valid trac ticket is referenced

In my previous post I configured the svn post-commit hook so that tickets referenced in the svn commit message had a link to the changes and the commit message added to the ticket comments. This post describes how to configure the svn to prevent commits that do not reference a valid ticket.

The trac community have done all the hard work, so I downloaded trac-pre-commit-hook from http://trac.edgewall.org/browser/trunk/contrib/trac-pre-commit-hook, and placed it in a new directory /var/trac/hooks.

I set the ownership of the script to www-data and made sure it was executable

sudo chown www-data:www-data -R /var/trac/hooks
sudo chmod +x /var/trac/hooks/trac-pre-commit-hook

All that remains to be done is to call this from the svn pre-commit hook. I created a file/var/svn/repos/play/hooks/pre-commit with this content

REPOS=”$1″
TXN=”$2″
TRAC_ENV=”/var/trac/projects/play”
LOG=`/usr/bin/svnlook log -t “$TXN” “$REPOS”`
/usr/bin/python /var/trac/hooks/trac-pre-commit-hook “$TRAC_ENV” “$LOG” || exit 1

If a commit fails to reference an open ticket I now get a message like this

svn commit -m “Some message referencing a closed ticket refs #1” test.txt 
Sending        test.txt
Transmitting file data .svn: E165001: Commit failed (details follow):
svn: E165001: Commit blocked by pre-commit hook (exit code 1) with output:

At least one open ticket must be mentioned in the log message.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top