Recipe: SVN post-commit hooks in Perl

As the original SVN documentation about post-commit hooks has obviously been written by Trappist monks (larsbot, did you know they also have a special beer?) and the googlian brute force gives me cranky

64,200 useless results for SVN post-commit hooks

I spent some trial&error time for installing an SVN hook which is supposed to be triggered whenever a document is committed into the SVN repository.

Activating a hook itself is trivial: you just create in the directory

/on/the/server/svn/hooks/

an executable file named post-commit, in my case a Perl script:

#!/usr/bin/perl

my $path = shift @ARGV;
my $rev  = shift @ARGV;
...

The real problem is how does that know about the file name involved, or the content therein? That can be achieved with svnlook:

`/usr/bin/svnlook changed -r $rev $path` =~ /U\s+(.*)$/;
my $file = $1;

Note that you always have to use full path names for executables to be found. If you need the contents itself, then

my $contents = `/usr/bin/svnlook cat -r $rev $path $file`;

can help.

Sidenote: In pre-commit you do not get the committed revision but a transaction number you can follow up as well:

...
my $path = shift @ARGV;
my $txn  = shift @ARGV;

`/usr/bin/svnlook changed -t $txn $path`  =~ /U\s+(.*)$/;
....

Feeding the cranky kid.

Posted In

Note to myself:

I found recently:

http://search.cpan.org/perldoc?SVN::Hooks

which is definitely worth looking into.

Any experiences shared here are welcome!

rho | Wed, 10/08/2008 - 08:22

Really short but useful

Really short but useful howto.
Thanks.

Wesley Gibson (not verified) | Mon, 03/02/2009 - 15:27