0

Implementing a Subversion Hook

I'm working on configuring a brand-spanking new Subversion repository and the first thing I like to do is install a few useful hooks to validate certain actions. Since I haven't done this for a while, I've been kind of feeling my way around a bit and I've hit a few simple, but not-necessarily-obvious snags that I thought it might be worth detailing the process for the next time.

Creating a Hook

A hook is really nothing more than a shell script that is triggered by some pre-defined event on a Subversion repository. When installed, Subversion bundles a hook template for each of these pre-defined events. Creating an active hook is as simple as copying the appropriate template to the same directory and removing the templates .tmpl extension.

# sudo cp pre-commit.tmpl pre-commit

Subversion expects these hooks to be named according to the event, so don't get fancy. When copying, just remove the extension and don't change any other property of the script name. Once the file is copied - pay attention, this is one of those not-necessarily-obvious snags that held me up longer than it should have - you'll need to tweak the permissions a bit.

# sudo chmod 744 pre-commit

By default, the hook templates are not executable, but an active script must be. This assumes that your hook script is owned by the appropriate entity. I use Apache to access my repository so the script is owned by apache:apache and the apache user must have execute (x) privileges to the script.

Modifying the Script

Although Subversion is bundled with example scripts, they're not always ready for primetime. In my case, I'm installing a pre-commit hook to ensure that users enter a message (hopefully a meaningful message) when they attempt to commit a change. This particular task is included in the pre-commit template, but does not display an error message if no commit message is included. I like to tell my users what the problem is, so I need to make that change myself. That snippet from my updated script looks like this:

# Make sure that the log message contains some text.
# Introspect the current transaction using svnlook, pipe
# the content through a grep for any alphanumeric characters
# and extract the byte count.

SVNLOOK=/usr/bin/svnlook
LOGMSG=`$SVNLOOK log -t "$TXN" "$REPOS" | grep "[a-zA-Z0-9]" | wc -c`

# Prepare an error-message
# If the byte count is 0, display an error message

if [ "$LOGMSG" -lt 1 ]; then
echo -e "Please provide a meaningful comment when committing changes." 1>&2
exit 1
fi

Notice the "1>&2" at the end of the echo statement? That redirects the standard output of the script (the echo string, represented as 1) to the standard error (represented as 2) which can be displayed by a Subversion client (TortoiseSVN, in my case). Without that, no error message will appear. Another not-necessarily-obvious snag.

C'est Fini

That's it. Now try committing a change without entering a message and the commit should fail with your error message displayed.

tags:
HowTo
zeal said:
 
cool, thx for your post. it really solved my problem about the displaying of error messages
 
posted 800 days ago
Add Comment Reply to: this comment OR this thread
 
 
Glad it helped. Took me a while to track it down, so at least I saved someone else a little time. :-)
 
posted 800 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Rob  Wilkerson