SVN Directory Lock

From Noah.org
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 commit will be allowed to the locked directory or to any directory below it. The only exception is for a commit that deletes the lock itself. This is to allow a directory to be unlocked.

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. The example above set the property to TRUE, but it could be set to a text message.

Delete a lock

To remove a lock delete the lock 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" />