From b448ee94e50412b14f8cf48da1267d046255ad0b Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 23 Jun 2014 10:07:08 +0000 Subject: [PATCH] rewritten pre-commit and added svn_min_commit_message_length --- codepot/configure | 3 +- codepot/configure.ac | 1 + codepot/etc/Makefile.in | 6 +- codepot/etc/codepot.ini.in | 6 ++ codepot/etc/pre-commit | 29 ------ codepot/etc/pre-commit.in | 182 +++++++++++++++++++++++++++++++++++++ 6 files changed, 195 insertions(+), 32 deletions(-) delete mode 100755 codepot/etc/pre-commit create mode 100644 codepot/etc/pre-commit.in diff --git a/codepot/configure b/codepot/configure index 38c6e6de..82f78f92 100755 --- a/codepot/configure +++ b/codepot/configure @@ -2544,7 +2544,7 @@ cachedir=$cachedir - ac_config_files="$ac_config_files Makefile codepot.spec etc/Makefile etc/codepot.ini etc/codepot.a2ldap etc/codepot.httpd etc/start-commit etc/post-commit etc/pre-revprop-change etc/post-revprop-change sbin/Makefile sbin/codepot-user DEBIAN/Makefile DEBIAN/control DEBIAN/postinst DEBIAN/postrm src/config.php src/codepot/config/Makefile src/codepot/controllers/Makefile src/codepot/errors/Makefile src/codepot/helpers/Makefile src/codepot/hooks/Makefile src/codepot/language/english/Makefile src/codepot/language/indonesian/Makefile src/codepot/language/korean/Makefile src/codepot/language/Makefile src/codepot/libraries/Makefile src/codepot/models/Makefile src/codepot/views/Makefile src/codepot/Makefile src/css/images/Makefile src/css/Makefile src/js/prettify/Makefile src/js/Makefile src/system/cache/Makefile src/system/codeigniter/Makefile src/system/database/drivers/mssql/Makefile src/system/database/drivers/mysql/Makefile src/system/database/drivers/mysqli/Makefile src/system/database/drivers/oci8/Makefile src/system/database/drivers/odbc/Makefile src/system/database/drivers/postgre/Makefile src/system/database/drivers/sqlite/Makefile src/system/database/drivers/Makefile src/system/database/Makefile src/system/fonts/Makefile src/system/helpers/Makefile src/system/language/english/Makefile src/system/language/korean/Makefile src/system/language/Makefile src/system/libraries/Makefile src/system/logs/Makefile src/system/plugins/Makefile src/system/scaffolding/images/Makefile src/system/scaffolding/views/Makefile src/system/scaffolding/Makefile src/system/Makefile src/Makefile" + ac_config_files="$ac_config_files Makefile codepot.spec etc/Makefile etc/codepot.ini etc/codepot.a2ldap etc/codepot.httpd etc/pre-commit etc/start-commit etc/post-commit etc/pre-revprop-change etc/post-revprop-change sbin/Makefile sbin/codepot-user DEBIAN/Makefile DEBIAN/control DEBIAN/postinst DEBIAN/postrm src/config.php src/codepot/config/Makefile src/codepot/controllers/Makefile src/codepot/errors/Makefile src/codepot/helpers/Makefile src/codepot/hooks/Makefile src/codepot/language/english/Makefile src/codepot/language/indonesian/Makefile src/codepot/language/korean/Makefile src/codepot/language/Makefile src/codepot/libraries/Makefile src/codepot/models/Makefile src/codepot/views/Makefile src/codepot/Makefile src/css/images/Makefile src/css/Makefile src/js/prettify/Makefile src/js/Makefile src/system/cache/Makefile src/system/codeigniter/Makefile src/system/database/drivers/mssql/Makefile src/system/database/drivers/mysql/Makefile src/system/database/drivers/mysqli/Makefile src/system/database/drivers/oci8/Makefile src/system/database/drivers/odbc/Makefile src/system/database/drivers/postgre/Makefile src/system/database/drivers/sqlite/Makefile src/system/database/drivers/Makefile src/system/database/Makefile src/system/fonts/Makefile src/system/helpers/Makefile src/system/language/english/Makefile src/system/language/korean/Makefile src/system/language/Makefile src/system/libraries/Makefile src/system/logs/Makefile src/system/plugins/Makefile src/system/scaffolding/images/Makefile src/system/scaffolding/views/Makefile src/system/scaffolding/Makefile src/system/Makefile src/Makefile" cat >confcache <<\_ACEOF # This file is a shell script that caches the results of configure @@ -3077,6 +3077,7 @@ do "etc/codepot.ini" ) CONFIG_FILES="$CONFIG_FILES etc/codepot.ini" ;; "etc/codepot.a2ldap" ) CONFIG_FILES="$CONFIG_FILES etc/codepot.a2ldap" ;; "etc/codepot.httpd" ) CONFIG_FILES="$CONFIG_FILES etc/codepot.httpd" ;; + "etc/pre-commit" ) CONFIG_FILES="$CONFIG_FILES etc/pre-commit" ;; "etc/start-commit" ) CONFIG_FILES="$CONFIG_FILES etc/start-commit" ;; "etc/post-commit" ) CONFIG_FILES="$CONFIG_FILES etc/post-commit" ;; "etc/pre-revprop-change" ) CONFIG_FILES="$CONFIG_FILES etc/pre-revprop-change" ;; diff --git a/codepot/configure.ac b/codepot/configure.ac index ffd6116c..795492a7 100644 --- a/codepot/configure.ac +++ b/codepot/configure.ac @@ -87,6 +87,7 @@ AC_CONFIG_FILES([ etc/codepot.ini etc/codepot.a2ldap etc/codepot.httpd + etc/pre-commit etc/start-commit etc/post-commit etc/pre-revprop-change diff --git a/codepot/etc/Makefile.in b/codepot/etc/Makefile.in index 709e5b84..0421e66d 100644 --- a/codepot/etc/Makefile.in +++ b/codepot/etc/Makefile.in @@ -39,7 +39,7 @@ subdir = etc DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in \ $(srcdir)/codepot.a2ldap.in $(srcdir)/codepot.httpd.in \ $(srcdir)/codepot.ini.in $(srcdir)/post-commit.in \ - $(srcdir)/post-revprop-change.in \ + $(srcdir)/post-revprop-change.in $(srcdir)/pre-commit.in \ $(srcdir)/pre-revprop-change.in $(srcdir)/start-commit.in ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/ac/m4/as-ac-expand.m4 \ @@ -49,7 +49,7 @@ am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ mkinstalldirs = $(install_sh) -d CONFIG_HEADER = $(top_builddir)/./config.h CONFIG_CLEAN_FILES = codepot.ini codepot.a2ldap codepot.httpd \ - start-commit post-commit pre-revprop-change \ + pre-commit start-commit post-commit pre-revprop-change \ post-revprop-change am__installdirs = "$(DESTDIR)$(cfgdir)" "$(DESTDIR)$(perldir)" \ "$(DESTDIR)$(cfgdir)" @@ -193,6 +193,8 @@ codepot.a2ldap: $(top_builddir)/config.status $(srcdir)/codepot.a2ldap.in cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ codepot.httpd: $(top_builddir)/config.status $(srcdir)/codepot.httpd.in cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ +pre-commit: $(top_builddir)/config.status $(srcdir)/pre-commit.in + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ start-commit: $(top_builddir)/config.status $(srcdir)/start-commit.in cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ post-commit: $(top_builddir)/config.status $(srcdir)/post-commit.in diff --git a/codepot/etc/codepot.ini.in b/codepot/etc/codepot.ini.in index 0d542b46..ba30d727 100644 --- a/codepot/etc/codepot.ini.in +++ b/codepot/etc/codepot.ini.in @@ -195,6 +195,12 @@ signin_for_code_search = "yes" ;------------------------------------------------------------------------------ svn_read_access = "member" + +;------------------------------------------------------------------------------ +; The length of a commit message must be as long as this value. +;------------------------------------------------------------------------------ +svn_min_commit_message_length = "0" + ;------------------------------------------------------------------------------ ; customized footer ;------------------------------------------------------------------------------ diff --git a/codepot/etc/pre-commit b/codepot/etc/pre-commit deleted file mode 100755 index 72942a16..00000000 --- a/codepot/etc/pre-commit +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/sh - -REPO="$1" -TXN="$2" - -# TODO: need to check the policy on a commit message -# wget ... %API%/svnPolicy/ ... -# once done, update repo.sh to include pre-commit - -svnlook log -t "$TXN" "$REPO" | grep "[a-zA-Z0-9]" >/dev/null || { - echo "---------------------------------------------------------" >&2 - echo " Your commit message do not contain any meaningful text. " >&2 - echo "---------------------------------------------------------" >&2 - exit 1 -} - -# -# Subversion problem: -# Hard to detect the tagging patterns and update patterns on a tag. -# -#svnlook changed -t "$TXN" "$REPO" | grep '^tags/' >/dev/null && { -# echo "----------------------------------------------------" >&2 -# echo " You are not allowed to change tags/* " >&2 -# echo "----------------------------------------------------" >&2 -# exit 1 -#} -# - -exit 0 diff --git a/codepot/etc/pre-commit.in b/codepot/etc/pre-commit.in new file mode 100644 index 00000000..78306e51 --- /dev/null +++ b/codepot/etc/pre-commit.in @@ -0,0 +1,182 @@ +#!/usr/bin/perl + +use strict; + +use Config::Simple; +use DBI; +use File::Basename; + +use SVN::Core; +use SVN::Repos; +use SVN::Fs; + +my $CFG_FILE = '@CFGDIR@/codepot.ini'; +my $REPOFS = $ARGV[0]; +my $REPOBASE = basename($REPOFS); +my $TRANSACTION = $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'), + + svn_min_commit_message_length => $cfg->param ('svn_min_commit_message_length') + }; + + 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 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; + $query->finish (); + return (((scalar(@row) > 0)? 1: 0), undef); +} + +sub is_project_commitable +{ + my ($dbh, $prefix, $projectid) = @_; + + my $query = $dbh->prepare ("SELECT commitable FROM ${prefix}project WHERE id=?"); + if (!$query || !$query->execute ($projectid)) + { + return (-1, $dbh->errstr()); + } + + my @row = $query->fetchrow_array; + $query->finish (); + return (((scalar(@row) > 0 && $row[0] eq 'Y')? 1: 0), undef); +} + +#------------------------------------------------------------ +# MAIN +#------------------------------------------------------------ + +my $cfg = get_config (); +if (!defined($cfg)) +{ + print (STDERR "Cannot load codepot configuration file\n"); + exit (1); +} + +my $pool = SVN::Pool->new(undef); +#my $config = SVN::Core::config_get_config(undef); +#my $fs = eval { SVN::Fs::open ($REPOFS, $config, $pool) }; +my $svn = eval { SVN::Repos::open ($REPOFS, $pool) }; +if (!defined($svn)) +{ + print (STDERR "Cannot open svn - $REPOFS\n"); + exit (0); # just let go if the file system can't be opened +} + +my $fs = $svn->fs (); +if (!defined($fs)) +{ + print (STDERR "Cannot open fs - $REPOFS\n"); + exit (0); # just let go if the file system can't be opened +} + +my $txn = eval { $fs->open_txn ($TRANSACTION) }; +if (!defined($txn)) +{ + print (STDERR "Cannot open transaction - $TRANSACTION\n"); + exit (0); # just let go if the transation can't be opened +} + +my $log = $txn->prop ('svn:log'); +$log =~ s/^\s+|\s+$//g; # trim leading spaces and trailing spaces +if (length($log) < $cfg->{svn_min_commit_message_length}) { + print (STDERR "Commit message too short. must be >= $cfg->{svn_min_commit_message_length}\n"); + exit (1); # block +} + + + +#my $dbh = open_database ($cfg); +#if (!defined($dbh)) +#{ +# print (STDERR "Cannot open database\n"); +# exit (1); +#} +# +#my $member; +#my $commitable; +#my $errstr; +# +#($member, $errstr) = is_project_member ( +# $dbh, $cfg->{database_prefix}, $REPOBASE, $USER); +#if ($member <= -1) +#{ +# print (STDERR "Cannot check membership - $errstr\n"); +# close_database ($dbh); +# exit (1); +#} +# +#($commitable, $errstr) = is_project_commitable ( +# $dbh, $cfg->{database_prefix}, $REPOBASE); +#if ($commitable <= -1) +#{ +# print (STDERR "Cannot check commitability - $errstr\n"); +# close_database ($dbh); +# exit (1); +#} +# +#close_database ($dbh); +# +#if ($member == 0) +#{ +# print (STDERR "$USER doesn't belong to the $REPOBASE project\n"); +# exit (1); +#} +# +#if ($commitable == 0) +#{ +# print (STDERR "The $REPOBASE project is not commitable\n"); +# exit (1); +#} +# +exit (0);