Subversion post-commit hook

mars 8th, 2010
by Axel

Following my previous post, I decided to write a Perl script that will be executed on post-commit. Initially I had a Shell script to update automatically my development Apache document root every 3 min. Therefore each commit I make was visible within 3 minutes on my development site.

The former script was:

#!/bin/sh
repo="/var/www/mysite_dev/www"
svn=`which svn`
cd $repo
$svn up
chown -R www-data:www-data $repo

The new post-commit hook is now getting after each commit all the updated files and runs a « svn up » on them. This means that my development site is always up to date. The script is:

#!/usr/bin/perl
use strict;
use warnings;
my $file;
my $repo = '/var/svn/yourrepository';
my $site = '/var/www/yoursite';

my $path = shift @ARGV;
my $revision  = shift @ARGV;

my @files = `/usr/bin/svnlook changed -r $revision $repo | cut -b 5-`;
foreach (@files) {
        $file = $_;
        if ($file =~ m/trunk-2/i) {
                $file =~ s/trunk-2//i;

                system('/usr/bin/svn update --non-interactive '.$site.'/'.$file);
                system('chown www-data: '.$site.'/'.$file);
        }
}

PS: « trunk-2″ is my development branch. I want to update my development site on changes made in this branch only. Adapt this value according to your needs.

Tags: , , ,
Posted in Développement | Comments (2)

2 Responses to “Subversion post-commit hook”

  1. Britain Says:

    Very cool – I have tried and cannot get it to work however. If I su to my Subversion user and execute the script manually it runs perfectly. I added some logging in the script to verify the user it is running as and it is definitely running as the Subversion user. Most of the script executes as you would expect but the system calls don’t seem to do anything. Any ideas or suggestions for additional debug code I could add for testing?

  2. Axel Says:

    Hmmm did you check your binaries paths (perl, svn)?
    In your logs, does it loop in the foreach?
    Trunk-2 is my repo, maybe not yours. Did you adapt « trunk-2″ to your need (x2)?

Leave a Reply