← All posts

Subversion. Automatically Updating a Project Using Hooks

subversion-hooks

In most cases, when developing a web project, you need to update the working version automatically. SVN hooks come to the rescue!

Let’s start with a web project that we need to create a repository for. I explained how to do this in the article “FreeBSD. Installing and Configuring Subversion”.

From now on, we’ll update our project only through the repository!

Preparing the Project

First, let’s import the web project into the repository:


# svn import /_project_ file:///svn/repos/_project_

Now let’s delete all the project files (don’t forget to make backups, just in case) and do a checkout:


# cd /_project
# rm -rf *
# svn checkout file:///svn/repos/_project_

Hooks

Hooks are triggered by various Subversion events. Here are some of them:

  • start-commit - runs before a transaction begins; can be used to check permissions.
  • pre-commit - runs at the end of a transaction, but before the commit; often used to validate data, for example, to check that log messages are not empty.
  • post-commit - runs after a transaction; can be used to send email or back up the repository.
  • pre-revprop-change - runs before changes to a revision; can be used to check access.
  • post-revprop-change - runs after changes to a revision; can be used to send email or back up changes.

There are also “post-lock”, “post-unlock”, “pre-lock”, and “pre-unlock”; as their names suggest, they are triggered by locking. I won’t go into them, because they haven’t caught my interest yet.

Post-Commit Hook

This hook is triggered after any commit to the repository. To activate it, we need to copy the template and give it execute permissions:


# cd /svn/repos/_project_/hooks
# cp post-commit.tmpl post-commit
# chmod u+x post-commit

Next, let’s edit the post-commit file, comment out everything we don’t need, and add our commands to update the project:


cd /_project_ && /usr/local/bin/svn update

The contents of your hook will end up looking something like this:


#!/bin/sh

#
# ... тут куча комментариев ...
#

REPOS="$1"
REV="$2"

cd /_project_ && /usr/local/bin/svn update

#commit-email.pl "$REPOS" "$REV" commit-watchers@example.org
#log-commit.py --repository "$REPOS" --revision "$REV"

Next, let’s check whether the hook works under the account that Subversion runs as by executing it:


# su www
# ./post-commit

If the response looks something like this:


At revision тут_номер_ревизии

then everything is fine!

If not, there’s an error in your hook. Leave a comment and I’ll help.

The hook itself is ready. Now we need to test it: make a commit and see whether your project has been updated. If it hasn’t, the problem is most likely with write permissions for your project’s files.

You need to grant write permissions for the web project’s files to the user/group that subversion runs as, which is most likely www.


# cd /_project
# chown -R www:www *
# chown -R www:www .*
# chmod -R 775 *
# chmod -R 775 .svn

P.S. Thanks to dudu for helping test the hook :)

Comments are closed. Read the original discussion in Russian →