codepot/codepot/etc/post-commit.in

114 lines
2.8 KiB
Plaintext
Raw Normal View History

2014-05-16 15:49:16 +00:00
#!/usr/bin/perl
use strict;
use Config::Simple;
use DBI;
use File::Basename;
use POSIX qw(strftime);
my $CFG_FILE = '@CFGDIR@/codepot.ini';
my $REPO = $ARGV[0];
my $REPOBASE = basename($REPO);
my $REV = $ARGV[1];
sub get_config
{
my $cfg = new Config::Simple();
if (!$cfg->read ($CFG_FILE))
{
return undef;
}
my $config = {
database_hostname => $cfg->param ("database_hostname"),
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")
};
return $config;
}
sub open_database
{
my ($cfg) = @_;
my $dbtype = $cfg->{database_driver};
my $dbname = $cfg->{database_name};
my $dbhost = $cfg->{database_hostname};
my $dbh = DBI->connect(
"DBI:$dbtype:$dbname:$dbhost",
$cfg->{database_username},
$cfg->{database_password},
{ RaiseError => 0, PrintError => 0, AutoCommit => 0 }
);
return $dbh;
}
sub close_database
{
my ($dbh) = @_;
$dbh->disconnect ();
}
sub write_commit_log
{
my ($dbh, $prefix, $projectid, $revision, $userid) = @_;
#+------+---------+-----------+---------------------------+---------------------+---------------+-----------------+
#| id | type | projectid | message | createdon | action | userid |
#+------+---------+-----------+---------------------------+---------------------+---------------+-----------------+
#| 895 | code | codepot | svn,codepot,72 | 2011-10-10 14:26:43 | commit | hyunghwan.chung |
my $message = "svn,$projectid,$revision";
my $createdon = strftime ('%Y-%m-%d %H:%M:%S', localtime());
$dbh->begin_work ();
my $query = $dbh->prepare ("INSERT INTO ${prefix}log (type,projectid,message,createdon,action,userid) VALUES (?,?,?,?,?,?)");
2014-05-16 15:49:16 +00:00
if (!$query || !$query->execute ('code', $projectid, $message, $createdon, 'commit', $userid))
{
my $errstr = $dbh->errstr();
$query->finish ();
2014-05-16 15:49:16 +00:00
$dbh->rollback ();
return (-1, $errstr);
2014-05-16 15:49:16 +00:00
}
$query->finish ();
2014-05-16 15:49:16 +00:00
$dbh->commit ();
return (0, undef);
2014-05-16 15:49:16 +00:00
}
#------------------------------------------------------------
# MAIN
#------------------------------------------------------------
my $AUTHOR = `svnlook author -r "${REV}" "${REPO}"`;
chomp ($AUTHOR);
my $cfg = get_config ();
if (!defined($cfg))
{
print (STDERR "Cannot load codepot configuration file\n");
exit (1);
}
my $dbh = open_database ($cfg);
if (!defined($dbh))
{
print (STDERR "Cannot open database\n");
exit (1);
}
write_commit_log ($dbh, $cfg->{database_prefix}, $REPOBASE, $REV, $AUTHOR);
close_database ($dbh);
exit (0);