2
votes

I am trying to run a post-commit script for SVN on CentOS Server. I would like to run svn export --force from var/svn/project to /var/www/project after every commit to repository. Even though I tried many alternatives I was not able to get it working. I would appreciate if someone could post a script that will run without any user permission problems.

Example post-commit file under /var/svn/project/hooks:

#!/bin/sh
sudo /usr/bin/svn export --force svn://domain/var/svn/project /var/www/project

It works if I run the script directly from SSH by root user but not after real SVN commits.

1

1 Answers

2
votes

I have a feeling that sudo is failing.

Doesn't really matter because what you want to do is a bad idea. How big is that repository export? How long will it take. 20 seconds? 2 minutes?

Imagine you're a Subversion user, and you attempt to check in a change. You do a commit, then suddenly your terminal appears to freeze. Maybe for 20 seconds, maybe for two minutes. Whatever it takes for the post-commit hook to complete. Boy, you think to yourself, Subversion is so slow...

A better way to do this is with a crontab. And, if you have the ability to do things as root, you certainly are able to do a crontab.

Keep track of the last Subversion revision. Then, if you detect a change, you do an export. Simple script, easy to implement, and doesn't freeze up Subversion while waiting for the export.

By the way, what user is running the Subversion server process. That's the user that executes the hook. If you don't have that user in your /etc/sudoers file set not to require a password, your post-commit hook will fail. Also, Subversion hooks have no path, so they can't locate the sudo command, you'll have to prefix it with whatever directory it's in.

Still, even if you get it to work, it's still a bad idea.

Here's an attempt to create a crontab that will do what you want. Completely untested. I simply don't have time tonight. However, it'll give you the idea:

#! /bin/bash
REPO_URL="http://...."
DIR=...   #Where you want the export
LAST_REV_FILE=/var/run/svn.last.rev
SVN=/usr/bin/svn
SED=/usr/bin/sed
current_rev=$($SVN info $REPO_URL | $SED -n '/^Revision: /s/^Revision: //p')
if [ ! -f $LAST_REV_FILE ]
then
     echo $current_rev > $LAST_REV_FILE
     exit 0
fi

#You're here if there is a last.rev.file
last_rev=$(echo $LAST_REV_FILE)
[ $last_rev -ge $current_rev ] && exit 0

# New Revision
echo $current_rev > $LAST_REV_FILE
rm -rf $DIR
$SVN export $REPO_URL $DIR