2

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 Answer 1

2

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
4
  • for some reason post-commit is not being trigger on commit. do you have any idea what the problem can be?
    – fiatux
    Oct 17, 2012 at 20:10
  • If this is Unix, the post-commit script must have execute permission and be executable by the user who runs the Subversion server. If you're on Windows, the file must be called post-commit.bat and PATHEXT must be set. As a test, put echo "POST COMMIT HOOK FAILED!" 2>&1 to print out something if the post-commit hook runs, but fails.
    – David W.
    Oct 17, 2012 at 20:15
  • 1
    it is a unix server and i tried chmod 777 on post-commit. still not being triggered.
    – fiatux
    Oct 17, 2012 at 22:46
  • If this is a shell script, put an exit 2 near the very top of the script. This will force the post-commit hook to fail. If the post-commit doesn't fail after doing this, then we know for sure the script isn't executing. Otherwise the script is executing, but not doing what you expect. If the hook fails because of the exit 2, put in some echo "FOO" 1>&2 lines through out the script and put the exit 2` in various places to see how far down the script is executing before failing. The echo statements will help you trace where in the script you are.
    – David W.
    Oct 18, 2012 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.