SVN Directory Lock

From Noah.org
Revision as of 10:33, 25 March 2010 by Root (talk | contribs)
Jump to navigationJump to search


SVN does not allow directories to be locked. You can lock individual files, but not entire directory trees. The following pre-commit hook will allow for advisory directory locks. That is, a directory lock will prevent a commit, but anyone can set or clear a lock.

Often it is useful to lock an entire directory to be sure that it remains consistent during testing or deployment.

This scripts requires that Pexpect be installed. Pexpect is a standard Debian/Ubuntu/RedHat package. This script does not use the pysvn library. The reason for this is because I got frustrated that the stupid library would not install on Red Hat Enterprise 4. I could get neither the RPM nor the source to install. It was easier to use Pexpect to script the command-line svn tools.

How to Use

When you create a lock no one will be able to commit to the locked directory or to any directory below it. The lock script will, of course, detect if you are committing a delete operation of an existing lock and the script will allow it.

Create a lock

To set a lock on a directory tree simply create a property on a directory called "lock":

 svn propset lock TRUE trunk/project_a

Note that the value of the lock can be anything. I set it to TRUE, but you could make it a message or comment if you want.

Delete a lock

To remove a lock simply delete the property:

 svn propdel lock trunk/project_a

Repository Installation

I put the svn_dir_lock.py in my hooks directory. I add the folloing code to the pre-commit shell script:

SVNLOOK=/usr/bin/svnlook
REPOS="$1"
TXN="$2"
python /home/svn/repository/hooks/svn_dir_lock.py -v "$TXN" "$REPOS" >&2
EXIT_STATUS=$?
if [ $EXIT_STATUS -ne 0 ]; then
    echo "Delete locks before you checkin." >&2
    exit 1
fi

svn_dir_lock.py Source

<include src="/var/www/usr/local/apache2/htdocs/engineering/src/python/svn_dir_lock.py" highlight="python" />