#!/bin/perl -w

#
# Pre-commit hook to prevent people updating tags
#
# Based on the script seen at http://subversion.tigris.org/servlets/ReadMsg?list=users&msgNo=15763
#
# Original script:
# /usr/bin/svnlook changed -t $2 $1 | grep "^U\W*tags" && /bin/echo "Cannot commit to tags" 1>&2  && exit 1
#

# Location of Subversion binaries
use constant SVN_BIN => 'C:/Program Files/Subversion/bin';

# URL of Repository
use constant SVN_URL => 'file:///d:/svn-repos/';

use strict;
use warnings;
use diagnostics;

my $repository = $ARGV[0];
my $transaction = $ARGV[1];

my $command = '"'.SVN_BIN.'/svnlook" changed  -t '.$transaction.' '.$repository;

my @results = `$command`;

#
# Result is something like
# A   project-name/trunk/whatever
# D   project-name/tags/whatever
# U   project-name/branches/whatever

my %tagsChanged;

foreach my $line( @results ) {
    chomp( $line );
    my ( $project, $tag, $tagname ) = split( /\//, $line );
    if( $tag eq "tags" ) {
        (undef, $project) = split( /\s+/, $project ); # Drop the A/D/U at the beginning
        $tagsChanged{ $project."/tags/".$tagname } = 1;
    }
}

#use Data::Dumper; print Dumper( \%tagsChanged );

# Now, go through each of the tags and ensure it doesn't already exist ...
foreach my $tag ( keys %tagsChanged ) {

    $command = '"'.SVN_BIN.'/svn" list '.SVN_URL.$tag.' 2>&1';
    my $result = `$command`;
    chomp( $result );
    if( $result ne 'svn: URL non-existent in that revision' ) {
        print STDERR "Cannot alter existing tags. Please create a new one";
        exit 1;
    }

}

exit 0;