2014-05-16 13:26:49 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use Config::Simple;
|
|
|
|
use DBI;
|
|
|
|
use File::Basename;
|
|
|
|
|
2014-06-23 14:57:07 +00:00
|
|
|
#use SVN::Core;
|
|
|
|
#use SVN::Repos;
|
|
|
|
#use SVN::Fs;
|
|
|
|
|
2014-05-16 13:26:49 +00:00
|
|
|
my $CFG_FILE = '@CFGDIR@/codepot.ini';
|
2014-06-23 14:57:07 +00:00
|
|
|
my $REPOFS = $ARGV[0];
|
|
|
|
my $REPOBASE = basename($REPOFS);
|
2014-05-16 13:26:49 +00:00
|
|
|
my $REVISION= $ARGV[1];
|
|
|
|
my $USER = $ARGV[2];
|
|
|
|
my $PROPNAME = $ARGV[3];
|
|
|
|
my $ACTION = $ARGV[4];
|
|
|
|
|
|
|
|
sub get_config
|
|
|
|
{
|
2014-06-23 14:57:07 +00:00
|
|
|
my $cfg = new Config::Simple();
|
|
|
|
|
|
|
|
if (!$cfg->read ($CFG_FILE))
|
|
|
|
{
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $config = {
|
|
|
|
database_hostname => $cfg->param ("database_hostname"),
|
2015-04-28 05:12:06 +00:00
|
|
|
database_port => $cfg->param ("database_port"),
|
2014-06-23 14:57:07 +00:00
|
|
|
database_username => $cfg->param ("database_username"),
|
|
|
|
database_password => $cfg->param ("database_password"),
|
|
|
|
database_name => $cfg->param ("database_name"),
|
|
|
|
database_driver => $cfg->param ("database_driver"),
|
|
|
|
database_prefix => $cfg->param ("database_prefix"),
|
|
|
|
|
|
|
|
svn_min_commit_message_length => $cfg->param ('svn_min_commit_message_length')
|
|
|
|
};
|
|
|
|
|
|
|
|
return $config;
|
2014-05-16 13:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub open_database
|
|
|
|
{
|
|
|
|
my ($cfg) = @_;
|
|
|
|
|
|
|
|
my $dbtype = $cfg->{database_driver};
|
|
|
|
my $dbname = $cfg->{database_name};
|
|
|
|
my $dbhost = $cfg->{database_hostname};
|
2015-04-28 05:12:06 +00:00
|
|
|
my $dbport = $cfg->{database_port};
|
|
|
|
|
|
|
|
if ($dbtype eq 'postgre') { $dbtype = 'Pg'; }
|
|
|
|
|
|
|
|
my $dbstr = "DBI:$dbtype:database=$dbname;";
|
|
|
|
if (length($dbhost) > 0) { $dbstr .= "host=$dbhost;"; }
|
|
|
|
if (length($dbport) > 0) { $dbstr .= "port=$dbport;"; }
|
2014-05-16 13:26:49 +00:00
|
|
|
|
|
|
|
my $dbh = DBI->connect(
|
2015-04-28 05:12:06 +00:00
|
|
|
$dbstr,
|
2014-05-16 13:26:49 +00:00
|
|
|
$cfg->{database_username},
|
|
|
|
$cfg->{database_password},
|
|
|
|
{ RaiseError => 0, PrintError => 0, AutoCommit => 0 }
|
|
|
|
);
|
|
|
|
|
|
|
|
return $dbh;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub close_database
|
|
|
|
{
|
|
|
|
my ($dbh) = @_;
|
|
|
|
$dbh->disconnect ();
|
|
|
|
}
|
|
|
|
|
|
|
|
sub is_project_member
|
|
|
|
{
|
|
|
|
my ($dbh, $prefix, $projectid, $userid) = @_;
|
|
|
|
|
|
|
|
my $query = $dbh->prepare ("SELECT projectid FROM ${prefix}project_membership WHERE userid=? AND projectid=?");
|
|
|
|
if (!$query || !$query->execute ($userid, $projectid))
|
|
|
|
{
|
|
|
|
return (-1, $dbh->errstr());
|
|
|
|
}
|
|
|
|
|
|
|
|
my @row = $query->fetchrow_array;
|
2014-05-16 16:11:33 +00:00
|
|
|
$query->finish ();
|
2014-05-16 13:26:49 +00:00
|
|
|
return (((scalar(@row) > 0)? 1: 0), undef);
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:57:07 +00:00
|
|
|
#sub check_commit_message
|
|
|
|
#{
|
|
|
|
# my ($minlen, $newmsg) = @_;
|
|
|
|
#
|
|
|
|
# my $pool = SVN::Pool->new(undef);
|
|
|
|
# my $svn = eval { SVN::Repos::open ($REPOFS, $pool) };
|
|
|
|
# if (!defined($svn))
|
|
|
|
# {
|
|
|
|
# print (STDERR "Cannot open svn - $REPOFS\n");
|
|
|
|
# return -1; # error
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# my $fs = $svn->fs ();
|
|
|
|
# if (!defined($fs))
|
|
|
|
# {
|
|
|
|
# print (STDERR "Cannot open fs - $REPOFS\n");
|
|
|
|
# return -1; # error
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# my $log = $fs->revision_prop ($REVISION, 'svn:log');
|
|
|
|
# $log =~ s/^\s+|\s+$//g; # trim leading spaces and trailing spaces
|
|
|
|
# if (length($log) < $minlen)
|
|
|
|
# {
|
|
|
|
# print (STDERR "[$log] Commit message too short. must be >= $minlen\n");
|
|
|
|
# return 0;
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# return 1;
|
|
|
|
#}
|
|
|
|
|
2014-05-16 13:26:49 +00:00
|
|
|
#------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#------------------------------------------------------------
|
|
|
|
|
2014-06-23 14:57:07 +00:00
|
|
|
my $newauthor = undef;
|
|
|
|
|
2014-05-16 13:26:49 +00:00
|
|
|
my $cfg = get_config ();
|
|
|
|
if (!defined($cfg))
|
|
|
|
{
|
|
|
|
print (STDERR "Cannot load codepot configuration file\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:57:07 +00:00
|
|
|
|
|
|
|
if ($ACTION eq 'D')
|
|
|
|
{
|
|
|
|
if ($PROPNAME eq 'svn:log' || $PROPNAME eq 'svn:author' || $PROPNAME eq 'svn:date')
|
|
|
|
{
|
|
|
|
print (STDERR "Not allowed to delete $PROPNAME\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ($ACTION eq 'M' || $ACTION eq 'A')
|
|
|
|
{
|
|
|
|
if ($PROPNAME eq 'svn:author')
|
|
|
|
{
|
|
|
|
$newauthor = do { local $/; <STDIN> }; # read <STDIN> as a whole
|
|
|
|
$newauthor =~ s/^\s+|\s+$//g; # trim leading spaces and trailing spaces
|
|
|
|
if ($newauthor eq '')
|
|
|
|
{
|
|
|
|
print (STDERR "Not allowed to empty the author\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ($PROPNAME eq 'svn:log')
|
|
|
|
{
|
|
|
|
my $minlen = $cfg->{svn_min_commit_message_length};
|
|
|
|
my $newmsg = do { local $/; <STDIN> }; # read <STDIN> as a whole
|
|
|
|
$newmsg =~ s/^\s+|\s+$//g; # trim leading spaces and trailing spaces
|
|
|
|
if (length($newmsg) < $minlen)
|
|
|
|
{
|
|
|
|
print (STDERR "Commit message too short. must be >= $minlen\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-16 13:26:49 +00:00
|
|
|
my $dbh = open_database ($cfg);
|
|
|
|
if (!defined($dbh))
|
|
|
|
{
|
2015-04-28 05:12:06 +00:00
|
|
|
printf (STDERR "Cannot open database - %s\n", $DBI::errstr);
|
2014-05-16 13:26:49 +00:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($member, $errstr) = is_project_member ($dbh, $cfg->{database_prefix}, $REPOBASE, $USER);
|
|
|
|
|
|
|
|
|
|
|
|
if ($member <= -1)
|
|
|
|
{
|
2014-06-23 14:57:07 +00:00
|
|
|
close_database ($dbh);
|
|
|
|
print (STDERR "Cannot check membership of [$USER] in the $REPOBASE project - $errstr\n");
|
2014-05-16 13:26:49 +00:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
elsif ($member == 0)
|
|
|
|
{
|
2014-06-23 14:57:07 +00:00
|
|
|
close_database ($dbh);
|
|
|
|
print (STDERR "[$USER] doesn't belong to the $REPOBASE project\n");
|
2014-05-16 13:26:49 +00:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:57:07 +00:00
|
|
|
if (defined($newauthor))
|
|
|
|
{
|
|
|
|
# the new author to set must be a member of the project.
|
|
|
|
|
|
|
|
my ($member, $errstr) = is_project_member ($dbh, $cfg->{database_prefix}, $REPOBASE, $newauthor);
|
|
|
|
if ($member <= -1)
|
|
|
|
{
|
|
|
|
close_database ($dbh);
|
|
|
|
print (STDERR "Cannot check membership of [$newauthor] in the $REPOBASE project - $errstr\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
elsif ($member == 0)
|
|
|
|
{
|
|
|
|
close_database ($dbh);
|
|
|
|
print (STDERR "[$newauthor] doesn't belong to the $REPOBASE project\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close_database ($dbh);
|
2014-05-16 13:26:49 +00:00
|
|
|
exit (0);
|