2012-03-20 18:30:29 +01:00
|
|
|
package Gitolite::Easy;
|
|
|
|
|
|
|
|
# easy access to gitolite from external perl programs
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
# most/all functions in this module test $ENV{GL_USER}'s rights and
|
|
|
|
# permissions so it needs to be set.
|
|
|
|
|
|
|
|
#<<<
|
|
|
|
@EXPORT = qw(
|
|
|
|
is_admin
|
|
|
|
is_super_admin
|
|
|
|
in_group
|
2012-03-25 13:19:02 +02:00
|
|
|
|
2012-03-20 18:30:29 +01:00
|
|
|
owns
|
|
|
|
can_read
|
|
|
|
can_write
|
|
|
|
|
2012-03-25 13:19:02 +02:00
|
|
|
config
|
|
|
|
|
2012-03-20 18:30:29 +01:00
|
|
|
%rc
|
|
|
|
say
|
|
|
|
say2
|
|
|
|
_print
|
|
|
|
usage
|
|
|
|
);
|
|
|
|
#>>>
|
|
|
|
use Exporter 'import';
|
|
|
|
|
|
|
|
use Gitolite::Rc;
|
|
|
|
use Gitolite::Common;
|
|
|
|
use Gitolite::Conf::Load;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
my $user;
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
# shell equivalent
|
|
|
|
# if gitolite access -q gitolite-admin $GL_USER W; then ...
|
|
|
|
sub is_admin {
|
|
|
|
valid_user();
|
|
|
|
return not( access( 'gitolite-admin', $user, 'W', 'any' ) =~ /DENIED/ );
|
|
|
|
}
|
|
|
|
|
|
|
|
# shell equivalent
|
|
|
|
# if gitolite access -q gitolite-admin $GL_USER W VREF/NAME/; then ...
|
|
|
|
sub is_super_admin {
|
|
|
|
valid_user();
|
|
|
|
return not( access( 'gitolite-admin', $user, 'W', 'VREF/NAME/' ) =~ /DENIED/ );
|
|
|
|
}
|
|
|
|
|
|
|
|
# shell equivalent
|
|
|
|
# if gitolite list-memberships $GL_USER | grep -x $GROUPNAME >/dev/null; then ...
|
|
|
|
sub in_group {
|
|
|
|
valid_user();
|
2012-03-25 13:19:02 +02:00
|
|
|
my $g = "@" . +shift;
|
2012-03-20 18:30:29 +01:00
|
|
|
|
2012-03-25 13:19:02 +02:00
|
|
|
return grep { $_ eq $g } @{ Gitolite::Conf::Load::list_memberships($user) };
|
2012-03-20 18:30:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# shell equivalent
|
|
|
|
# if gitolite creator $REPONAME $GL_USER; then ...
|
|
|
|
sub owns {
|
|
|
|
valid_user();
|
|
|
|
my $r = shift;
|
|
|
|
|
|
|
|
# prevent unnecessary disclosure of repo existence info
|
|
|
|
return 0 if repo_missing($r);
|
|
|
|
|
|
|
|
return ( creator($r) eq $user );
|
|
|
|
}
|
|
|
|
|
|
|
|
# shell equivalent
|
|
|
|
# if gitolite access -q $REPONAME $GL_USER R; then ...
|
|
|
|
sub can_read {
|
|
|
|
valid_user();
|
|
|
|
my $r = shift;
|
|
|
|
return not( access( $r, $user, 'R', 'any' ) =~ /DENIED/ );
|
|
|
|
}
|
|
|
|
|
|
|
|
# shell equivalent
|
|
|
|
# if gitolite access -q $REPONAME $GL_USER W; then ...
|
|
|
|
sub can_write {
|
|
|
|
valid_user();
|
|
|
|
my $r = shift;
|
|
|
|
return not( access( $r, $user, 'W', 'any' ) =~ /DENIED/ );
|
|
|
|
}
|
|
|
|
|
2012-03-25 13:19:02 +02:00
|
|
|
# shell equivalent
|
|
|
|
# foo=$(gitolite git-config -r $REPONAME foo\\.bar)
|
|
|
|
sub config {
|
|
|
|
my $repo = shift;
|
|
|
|
my $key = shift;
|
|
|
|
|
|
|
|
return () if repo_missing($repo);
|
|
|
|
|
|
|
|
my $ret = git_config($repo, $key);
|
|
|
|
return %$ret;
|
|
|
|
}
|
|
|
|
|
2012-03-20 18:30:29 +01:00
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub valid_user {
|
|
|
|
_die "GL_USER not set" unless exists $ENV{GL_USER};
|
|
|
|
$user = $ENV{GL_USER};
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|