From 876b554fb511dd120f206839604615f732ae0b55 Mon Sep 17 00:00:00 2001 From: Sitaram Chamarty Date: Fri, 16 Mar 2012 09:25:39 +0530 Subject: [PATCH] changes to custom command invocation etc.; see below - 'post-compile' subdir moved under 'commands/' but only for sanity; has no real significance now - new, internal use only, gitolite command run-all, as in gitolite run-all POST_COMPILE which runs all the commands in @{ $rc{POST_COMPILE} } in sequence. You can sdo this for any section of course, though this is the only one in the rc right now. (Future candidates: PRE_GIT, POST_GIT, PRE_CREATE, POST_CREATE) --- src/Gitolite/Hooks/PostUpdate.pm | 20 +------ src/Gitolite/Rc.pm | 3 +- src/Gitolite/Setup.pm | 2 +- src/commands/help | 34 +++++++++++ src/{ => commands}/post-compile/ssh-authkeys | 8 ++- src/gitolite | 63 ++++++++++---------- t/ssh-authkeys.t | 6 +- t/ssh-basic.t | 2 +- 8 files changed, 79 insertions(+), 59 deletions(-) create mode 100755 src/commands/help rename src/{ => commands}/post-compile/ssh-authkeys (93%) diff --git a/src/Gitolite/Hooks/PostUpdate.pm b/src/Gitolite/Hooks/PostUpdate.pm index 49abdfb..2ce4bb5 100644 --- a/src/Gitolite/Hooks/PostUpdate.pm +++ b/src/Gitolite/Hooks/PostUpdate.pm @@ -30,25 +30,7 @@ sub post_update { tsh_try("git checkout -f --quiet master"); } _system("$ENV{GL_BINDIR}/gitolite compile"); - - # now run optional post-compile features - if ( exists $rc{POST_COMPILE} ) { - if ( ref( $rc{POST_COMPILE} ) ne 'ARRAY' ) { - _warn "bad syntax for specifying post compile scripts; see docs"; - } else { - for my $s ( @{ $rc{POST_COMPILE} } ) { - - # perl-ism; apart from keeping the full path separate from the - # simple name, this also protects %rc from change by implicit - # aliasing, which would happen if you touched $s itself - my $sfp = "$ENV{GL_BINDIR}/post-compile/$s"; - - _warn("skipped post-compile script '$s'"), next if not -x $sfp; - trace( 2, "post-compile $s" ); - _system( $sfp, @ARGV ); # they better all return with 0 exit codes! - } - } - } + _system("$ENV{GL_BINDIR}/gitolite run-all POST_COMPILE"); exit 0; } diff --git a/src/Gitolite/Rc.pm b/src/Gitolite/Rc.pm index 321db5e..cd828c2 100644 --- a/src/Gitolite/Rc.pm +++ b/src/Gitolite/Rc.pm @@ -196,13 +196,14 @@ __DATA__ # these will run in sequence after post-update POST_COMPILE => [ - 'ssh-authkeys', + 'post-compile/ssh-authkeys', ], # comment out or uncomment as needed # these are available to remote users COMMANDS => { + 'help' => 1, 'info' => 1, }, ); diff --git a/src/Gitolite/Setup.pm b/src/Gitolite/Setup.pm index 0d5f664..eb19a17 100644 --- a/src/Gitolite/Setup.pm +++ b/src/Gitolite/Setup.pm @@ -36,7 +36,7 @@ sub setup { setup_gladmin( $admin, $pubkey, $argv ); _system("$ENV{GL_BINDIR}/gitolite compile"); - _system("$ENV{GL_BINDIR}/gitolite post-compile ssh-authkeys") if $pubkey; + _system("$ENV{GL_BINDIR}/gitolite run-all POST_COMPILE"); hook_repos(); # all of them, just to be sure } diff --git a/src/commands/help b/src/commands/help new file mode 100755 index 0000000..acd9a77 --- /dev/null +++ b/src/commands/help @@ -0,0 +1,34 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use lib $ENV{GL_BINDIR}; +use Gitolite::Rc; +use Gitolite::Common; + +=for usage +Usage: gitolite help + +Prints a list of custom commands available at this gitolite installation. +=cut + +my $user = $ENV{GL_USER} || ''; +print "hello" . ( $user ? " $user" : "") . ", this is gitolite3 " . version() . " on git " . substr( `git --version`, 12 ) . "\n"; + +_chdir("$ENV{GL_BINDIR}/commands"); + +print "list of " . ($user ? "remote" : "gitolite" ) . " commands available:\n\n"; + +for my $c (`find . -type f|sort`) { + chomp($c); + $c =~ s(^./)(); + next unless -x $c; + + # if it's from a remote client, show only what he is allowed + next if $user and not $rc{COMMANDS}{$c}; + + print "\t$c\n"; +} +print "\n"; + +exit 0; diff --git a/src/post-compile/ssh-authkeys b/src/commands/post-compile/ssh-authkeys similarity index 93% rename from src/post-compile/ssh-authkeys rename to src/commands/post-compile/ssh-authkeys index a286a74..6f8f23f 100755 --- a/src/post-compile/ssh-authkeys +++ b/src/commands/post-compile/ssh-authkeys @@ -14,7 +14,8 @@ $|++; # arguments anyway, it hardly matters. my $ab = `gitolite query-rc -n GL_ADMIN_BASE`; -_warn("'keydir' not found in '$ab'"), exit if not -d "$ab/keydir"; +trace( 2, "'keydir' not found in '$ab'; exiting" ), exit if not -d "$ab/keydir"; +my $akdir = "$ENV{HOME}/.ssh"; my $akfile = "$ENV{HOME}/.ssh/authorized_keys"; my $glshell = `gitolite query-rc -n GL_BINDIR` . "/gitolite-shell"; # XXX gl-time not yet coded (GL_PERFLOGT) @@ -58,11 +59,14 @@ if (@gl_keys) { # ---------------------------------------------------------------------- sub sanity { - _warn "$akfile missing; creating a new one" if not -f $akfile; _die "$glshell not found; this should NOT happen..." if not -f $glshell; _die "$glshell found but not readable; this should NOT happen..." if not -r $glshell; _die "$glshell found but not executable; this should NOT happen..." if not -x $glshell; + _warn "$akdir missing; creating a new one" if not -d $akdir; + _warn "$akfile missing; creating a new one" if not -f $akfile; + + _mkdir($akdir, 0700) if not -d $akfile; if ( not -f $akfile ) { _print( $akfile, "" ); chmod 0700, $akfile; diff --git a/src/gitolite b/src/gitolite index e62543d..a934d00 100755 --- a/src/gitolite +++ b/src/gitolite @@ -6,14 +6,13 @@ =for args Usage: gitolite [sub-command] [options] -The following subcommands are available; they should all respond to '-h' if -you want further details on each: +The following built-in subcommands are available; they should all respond to +'-h' if you want further details on each: setup 1st run: initial setup; all runs: hook fixups compile compile gitolite.conf query-rc get values of rc variables - post-compile run a post-compile command list-groups list all group names in conf list-users list all users/user groups in conf @@ -26,6 +25,10 @@ Warnings: - list-users is disk bound and could take a while on sites with 1000s of repos - list-memberships does not check if the name is known; unknown names come back with 2 answers: the name itself and '@all' + +In addition, running 'gitolite help' should give you a list of custom commands +available. They may or may not respond to '-h', depending on how they were +written. =cut # ---------------------------------------------------------------------- @@ -65,8 +68,8 @@ if ( $command eq 'setup' ) { Gitolite::Conf->import; compile(@args); -} elsif ( $command eq 'post-compile' ) { - post_compile(@args); +} elsif ( $command eq 'run-all' ) { + run_all(@args); } elsif ( -x "$rc{GL_BINDIR}/commands/$command" ) { trace( 2, "attempting gitolite command $command" ); @@ -93,36 +96,32 @@ sub args { # ---------------------------------------------------------------------- -=for post_compile -Usage: gitolite post-compile [-l] [post-compile-scriptname] [script args...] - - -l list currently available post-compile scripts - -Run a post-compile script (which normally runs from the post-update hook in -the gitolite-admin repo). -=cut - -sub post_compile { - usage() if ( not @_ or $_[0] eq '-h' ); - - run_subdir( 'post-compile', @_ ); -} - sub run_command { - run_subdir( 'commands', @_ ); -} - -sub run_subdir { - my $subdir = shift; - if ( @_ and $_[0] eq '-l' ) { - _chdir("$ENV{GL_BINDIR}/$subdir"); - map { say2($_) } grep { -x } glob("*"); - exit 0; - } - my $pgm = shift; - my $fullpath = "$ENV{GL_BINDIR}/$subdir/$pgm"; + my $fullpath = "$ENV{GL_BINDIR}/commands/$pgm"; _die "$pgm not found or not executable" if not -x $fullpath; _system( $fullpath, @_ ); exit 0; } + +sub run_all { + my $rc_section = shift; + + if ( exists $rc{$rc_section} ) { + if ( ref( $rc{$rc_section} ) ne 'ARRAY' ) { + _warn "$rc_section section in rc file is not a perl list"; + } else { + for my $s ( @{ $rc{$rc_section} } ) { + + # perl-ism; apart from keeping the full path separate from the + # simple name, this also protects %rc from change by implicit + # aliasing, which would happen if you touched $s itself + my $sfp = "$ENV{GL_BINDIR}/commands/$s"; + + _warn("skipped command '$s'"), next if not -x $sfp; + trace( 2, "command: $s" ); + _system( $sfp, @ARGV ); # they better all return with 0 exit codes! + } + } + } +} diff --git a/t/ssh-authkeys.t b/t/ssh-authkeys.t index 64179b4..e4454ee 100755 --- a/t/ssh-authkeys.t +++ b/t/ssh-authkeys.t @@ -11,15 +11,15 @@ $ENV{GL_BINDIR} = "$ENV{PWD}/src"; my $ak = "$ENV{HOME}/.ssh/authorized_keys"; my $kd = `gitolite query-rc -n GL_ADMIN_BASE` . "/keydir"; -try "plan 50"; +try "plan 49"; -my $pgm = "gitolite post-compile ssh-authkeys"; +my $pgm = "gitolite post-compile/ssh-authkeys"; try " # prep rm -rf $ak; ok - $pgm; ok; /'keydir' not found/ + $pgm; ok mkdir $kd; ok cd $kd; ok $pgm; ok; /authorized_keys missing/ diff --git a/t/ssh-basic.t b/t/ssh-basic.t index 2747185..223d8ed 100755 --- a/t/ssh-basic.t +++ b/t/ssh-basic.t @@ -26,7 +26,7 @@ try " cp $bd/../t/keys/*.pub $ab/keydir; ok or die 6 "; -_system("gitolite post-compile ssh-authkeys"); +_system("gitolite post-compile/ssh-authkeys"); # basic tests # ----------------------------------------------------------------------