#! /usr/bin/perl -w use strict ; use Repocafe ; my $TEMPL = 'lib/post-commit' ; my $prog = substr($0,rindex($0,'/')+1) ; my $Usage = < option a : install new post-commit hooks for all repo's repo_id : install a new post-commit hooks for repo with id ---------------------------------------- - $prog creates a post-commit hook from template '$TEMPL', - either for one specified repo, or all repo's (-a). - It is called by the web application when a new repository is created. - Normally, $prog is not used by repocafe admins. ---------------------------------------- USAGE sub Usage { die "$_[0]$Usage" ; } sub Error { die "$prog: $_[0]\n" ; } sub Warn { warn "$prog: $_[0]\n" ; } # usage: &GetOptions(ARG,ARG,..) defines $opt_ID as 1 or user spec'ed value # usage: &GetOptions(\%opt,ARG,ARG,..) defines $opt{ID} as 1 or user value # ARG = 'ID' | 'ID=SPC' | 'ID:SPC' for no-arg, required-arg or optional-arg # ID = perl identifier # SPC = i|f|s for integer, fixedpoint real or string argument use Getopt::Long ; Getopt::Long::config('no_ignore_case') ; our %opt = () ; Usage('') unless GetOptions ( \%opt, qw(v q d f c=s a help) ) ; if ( $opt{help} ) { print $Usage ; exit 0 ; } Usage("Arg count\n") unless $opt{a} or @ARGV == 1 ; $opt{v} ||= $opt{d} ; Repocafe::set_opt ( %opt ) ; my $Cafe = Repocafe -> new ( $opt{c} ) ; Error "can't find post-commit file ($TEMPL)" unless -f $TEMPL ; my $www_usr = $Cafe -> conf -> www_user ; my $www_grp = $Cafe -> conf -> www_group ; my $dir_admin = $Cafe -> conf -> dir_admin ; open TEMPL, $TEMPL or Error "can't open $TEMPL ($!)" ; my $TEXT = join '', ; close TEMPL ; my $UID = $www_usr =~ /^\d+$/ ? $www_usr : getpwnam $www_usr ; my $GID = $www_grp =~ /^\d+$/ ? $www_grp : getgrnam $www_grp ; Error "can't find uid for www_user ($www_usr)" unless defined $UID ; Error "can't find gid for www_group ($www_grp)" unless defined $GID ; if ( $< != 0 and $< != $UID ) { my $msg = "run as root or $www_usr" ; if ( $opt{f} ) { Error "must $msg" ; } else { printf "Warning : should %s\n", $msg ; } } sub install_hook { my $hook = shift ; my $text = shift ; if ( $opt{v} ) { printf "copy to hook %s\n", $hook ; printf "chown $UID, $GID, $hook\n" ; printf "-----\n%s-----\n", $text if $opt{d} ; } if ( $opt{f} ) { open HOOK, ">$hook" or Error "can't write $hook ($!)" ; print HOOK $text ; close HOOK ; chown $UID, $GID, $hook or Error "can't chown $UID, $GID, $hook ($!)" ; chmod 0755, $hook or Error "can't chmod 0755 $hook ($!)" ; } } my $repo_sql = '' ; unless ( $opt{a} ) { my $rid = shift ; $repo_sql = "id = $rid" ; } my $repos = $Cafe -> repos -> get_by_qwe ( $repo_sql ) ; for my $repo ( sort { $a -> repo_name cmp $b -> repo_name } @$repos ) { my $HOOK = $repo -> path . '/hooks/post-commit' ; my $repo_id = $repo -> id ; my $text = $TEXT ; $text =~ s/%dir_admin%/$dir_admin/g ; $text =~ s/%repo_id%/$repo_id/g ; install_hook $HOOK, $text ; }