Block changes to a tag in Subversion with pre-commit hooks

Start by going to the hooks directory in your repository and copying pre-commit.tmpl as pre-commit. Then add the line to disallow changes to tags.

#!/bin/sh
REPOS=”$1″
TXN=”$2″
SVNLOOK=/usr/bin/svnlook

# Don’t allow changes to tags
$SVNLOOK changed -t “$TXN” “$REPOS” | grep “^U\W.*\/tags\/” && /bin/echo “You cannot commit to a tag!” 1>&2 && exit 1

# Don’t allow adding new files to tags
$SVNLOOK changed -t “$TXN” “$REPOS” | grep “^A” && /bin/echo “You can’t add new files to a tag!” 1>&2 && exit 1

# Don’t allow deleting files in tags
$SVNLOOK changed -t “$TXN” “$REPOS” | grep “^D” && /bin/echo “You can’t delete files from a tag!” 1>&2 && exit 1

# Make sure that the log message contains some text.
$SVNLOOK log -t “$TXN” “$REPOS” | \
grep “[a-zA-Z0-9]” > /dev/null || exit 1

# Check that the author of this commit has the rights to perform the commit on the files and directories being modified.
commit-access-control.pl “$REPOS” “$TXN” commit-access-control.cfg || exit 1

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

Now run it by trying to check something into a tag to make sure it works.

$ svn commit install.sh -m “testing pre-commit hook”
Sending install.sh
Transmitting file data .svn: Commit failed (details follow):
svn: ‘pre-commit’ hook failed with error output:

All is well; the change didn’t get committed, right? What just happened was the commit failed because the pre-commit wasn’t marked executable! I wouldn’t have noticed this if I didn’t echo some output to tell the user that adding, deleting, and modifying in a tag is not allowed.

$ cd svn/myrepo/hooks
$ chmod +x pre-commit

Now when you try to commit, you will see a different message:

$ svn commit install.sh -m ” testing pre-commit hook”
Sending install.sh
Transmitting file data .svn: Commit failed (details follow):
svn: ‘pre-commit’ hook failed with error output:
You cannot commit to a tag!

So don’t forget to mark it executable!!!

Posted by admica   @   3 September 2009

Related Posts

Like this post? Share it!

Digg Twitter StumbleUpon Delicious Technorati Facebook RSS

2 Comments

Comments
Aug 20, 2010
1:24 am
#1 coudenysj :

Nice article!

You can also add –copy-info to the svnlook command and detect if a commit to the tag is a copy or not and deny the commit if it is not a copy.

I’ve added example output in my blog post (http://blog.jachim.be/2010/08/disallow-changes-in-subversion-tags/).

Aug 6, 2011
5:14 am
#2 sophie :

Do you have this script for windows?

Leave a Comment

Name

Email

Website

Previous Post
« Knocking or Pinging in an Internal Combustion Engine
Next Post
RFID attack and defense tool - The ProxPick »
Powered by Wordpress   |   Lunated designed by ZenVerse