Subversion pre-commit hook

When you commit a file or files to your repository it’s not, by default, required to fill in a message or comment. If you want to force your users to do so. then you can use the pre-commit hook. A pre-commit hook is a script which will be evaluated and if everything is fine then it will exit with code 0 and commits the file(s). This script is very powerfull and can be written in different languages like Python, Perl or Bash. This article describes a simple pre-commit hook.

After installing Subversion a folder /volume1/svn/repositories/hooks was created. This folder contains several template files (ending with .tmpl). If you want you can use one of these pre-defined templates. We are going to adjust the pre-commit.tmpl in such way that it won’t allow users to commit files without a comment.

1. Go to cd /volume1/svn/repositories/hooks
2. Type cp pre-commit.tmpl pre-commit to copy the template file to a new file without the .tmpl extension.
3. Open the file with the build-in VI editor. To do this type vi pre-commit
4. Search for a string which looks like $SVNLOOK log -t "$TXN" "$REPOS" | \ grep "[a-zA-Z0-9]" > /dev/null || exit 1 . You can use the arrows. This piece of code already checks whether a comment has been filled in but won’t give you the reason why it couldn’t commit.
5. Now press (SHIFT + A). This will jump to the end of the line. VI editor is now in “edit” mode.
6. Change that line to

$SVNLOOK log -t "$TXN" "$REPOS" | \
grep "[a-zA-Z0-9]" > /dev/null || { echo "<<Empty Log Message>>" >&2; exit 1;} 

It will check if a comment is filled in, otherwise it will exit with exit code 1. If all checks passed then the script will exit with exit code 0.
7. Press ESC to get out the “edit” mode.
8. Type :wq to save the file and exit the VI editor.
9. Type chmod u+x pre-commit to set the right permissions, otherwise you will get an exit code 255, instead of the desired exit code 1.
10. Now try to commit without a comment you will get something like the screenshot below.

Force Comment

You can also download more examples or pre-written scripts, here and
here.

Complete script (Without comments)

#!/bin/sh

REPOS="$1"
TXN="$2"

# Make sure that the log message contains some text.
SVNLOOK=/opt/bin/svnlook
$SVNLOOK log -t "$TXN" "$REPOS" | \
   grep "[a-zA-Z0-9]" > /dev/null || { echo "<<Empty log message>>" >& 2; exit 1; }

# All checks passed, so allow the commit.
exit 0

2 comments to Subversion pre-commit hook

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>