Thursday, May 8, 2008

The simplest (python based) pre-commit hook in Subversion

Hooks in Subversion are scripts or executables that are triggered by an event in the subversion version control life cycle. The following are the hooks supported by Subversion.

In the hooks directory of a repository we can find these template files:
post-commit.tmpl      pre-unlock.tmpl
post-lock.tmpl          pre-commit.tmpl
start-commit.tmpl     post-revprop-change.tmpl
pre-lock.tmpl          post-unlock.tmpl     pre-revprop-change.tmpl

start-commit
Before the commit transaction starts
pre-commit After the commit transaction starts but before the transaction is commited
post-commit After the commit transaction completes
pre-revprop-change Before a revision property is changed Repository Path,
post-revprop-change: After a revision property is changed Repository Path
pre-lock:  Before the lock being acquired
post-lock:  
After the lock being acquired

To test how a hook works, let’s create a repository in our Gnu/Linux Box.

svnadmin create hello

Now we have a repository called hello and it will contain files like
conf dav db format hooks joke.py locks pre-commit README.txt
Modify the conf/svnserve.conf to include passwd file and add users to the passwd file along with their passwords.

Then run the server as svnserve -d -r hello
To checkout :
svn co svn://127.0.0.1

Then lets move to the real work. We need to stop anybody who is trying to commit to our repository just because we are doing some important work on the server or we just dont want anybody to help us :)

To do that, let’s create a file called pre-commit in hooks folder in the repository (hello). The content of the pre-commit should be :

#!/usr/bin/env python
# ====================================================================
# The simplest pre-commit hook which prevents all the commits to the repository
# This can be useful(?) when you are doing the backup of your subversion repo.
# This small script will prevent the commit and will give a simple message
# to the person who is commiting the changes.
#
# USAGE:
# copy this script as pre-commit in the hooks directory of the svn server
#
# ====================================================================

import sys

MESSAGE=”"”
Dear Sir,

As we are doing some important work in the server,all those who are submitting
the changes will have to wait for some time.

Sorry for the inconvenience caused.

Regards,

Administrator.
“”"
# Messages written to the stderr will be shown to the person who is commiting.
sys.stderr.write(MESSAGE)

# Need to exit with anything other than 0 to fail the commit
sys.exit(2)

Let’s change the mode of the pre-commit to executable (chmod +x pre-commit)

To test this setup, lets create a sandbox and then try to commit our changes to the server.
svn co svn://127.0.0.1
cd 127.0.0.1
mkdir test
svn add test
svn commit -m “adding the test directory”

Now we will see the pre-commit hook in action:

Adding test
svn: Commit failed (details follow):
svn: ‘pre-commit’ hook failed with error output:

Dear Sir,

As we are doing some important work in the server,all those who are submitting
the changes will have to wait for some time.

Sorry for the inconvenience caused.

Regards,

Administrator.


This is the most simplest hook that can be implemented in Python. More complex hooks can be implemented in python using the svn module. More useful hooks examples are available from http://subversion.tigris.org/tools_contrib.html#hook_scripts

Posted by maxinbjohn in 05:39:11
Comments

2 Responses

  1. fag bearings says:

    I envy you,and i admire your artile very much.

  2. drivers tag says:

    you rock my world!!!

Leave a Reply