5 use lib
$ENV{GL_LIBDIR
};
11 perms -- list or set permissions for user-created ("wild") repo.
14 ssh git@host perms <repo> -l
15 # list current permissions on repo
16 ssh git@host perms <repo> -lr
17 # list available roles and their access rights
19 ssh git@host perms <repo> + <rolename> <username>
20 # change permissions: add a user to a role
21 ssh git@host perms <repo> - <rolename> <username>
22 # change permissions: remove a user from a role
25 ssh git@host perms my/repo + READERS alice
26 ssh git@host perms my/repo + WRITERS bob
29 There is also a batch mode useful for scripting and bulk loading. Do not
30 combine this with the +/- mode above. This mode also accepts an optional "-c"
31 flag to create the repo if it does not already exist (assuming $GL_USER has
32 permissions to create it).
35 cat copy-of-backed-up-gl-perms | ssh git@host perms <repo>
36 cat copy-of-backed-up-gl-perms | ssh git@host perms -c <repo>
39 usage
() if not @ARGV or $ARGV[0] eq '-h';
41 $ENV{GL_USER
} or _die
"GL_USER not set";
43 my $generic_error = "repo does not exist, or you are not authorised";
45 if ( @ARGV >= 2 and $ARGV[1] eq '-l' ) {
46 getperms
($ARGV[0]); # doesn't return
49 # auto-create the repo if -c passed and repo doesn't exist
50 if ( $ARGV[0] eq '-c' ) {
52 my $repo = $ARGV[0] or usage
();
53 _die
"invalid repo '$repo'" unless $repo =~ $REPONAME_PATT;
55 if ( not -d
"$rc{GL_REPO_BASE}/$repo.git" ) {
56 my $ret = Gitolite
::Conf
::Load
::access
( $repo, $ENV{GL_USER
}, '^C', 'any' );
57 _die
$generic_error if $ret =~ /DENIED/;
59 require Gitolite
::Conf
::Store
;
60 Gitolite
::Conf
::Store
->import;
61 new_wild_repo
( $repo, $ENV{GL_USER
}, 'perms-c' );
62 gl_log
( 'create', $repo, $ENV{GL_USER
}, 'perms-c' );
68 if ( @ARGV and $ARGV[0] eq '-lr' ) {
77 require Gitolite
::Cache
;
78 Gitolite
::Cache
::cache_control
('flush', $repo);
81 _system
( "gitolite", "trigger", "POST_CREATE", $repo, $ENV{GL_USER
}, 'perms' );
83 # ----------------------------------------------------------------------
87 _die
$generic_error if not owns
($repo);
88 my $pf = "$rc{GL_REPO_BASE}/$repo.git/gl-perms";
90 print slurp
($pf) if -f
$pf;
96 _die
$generic_error if not owns
($repo);
97 my $pf = "$rc{GL_REPO_BASE}/$repo.git/gl-perms";
100 # legacy mode; pipe data in
101 print STDERR
"'batch' mode started, waiting for input (run with '-h' for details).\n";
102 print STDERR
"Please enter 'cancel' to abort if you did not intend to do this.\n";
106 _die
"CANCELLED" if /^\s*cancel\s*$/i;
107 invalid_role
($1) if /(\S+)/ and not $rc{ROLES
}{$1};
115 _die
"Invalid syntax. Please re-run with '-h' for detailed usage" if @_ != 3;
116 my ( $op, $role, $user ) = @_;
117 _die
"Invalid syntax. Please re-run with '-h' for detailed usage" if $op ne '+' and $op ne '-';
118 _die
"Invalid user '$user'" if not $user =~ $USERNAME_PATT;
121 my @text = slurp
($pf) if -f
$pf;
123 my $present = grep { $_ eq "$role $user\n" } @text;
126 if ( not $present ) {
127 _warn
"'$role $user' was not present in file";
129 @text = grep { $_ ne "$role $user\n" } @text;
130 _print
( $pf, @text );
133 invalid_role
($role) unless grep { $_->[3] eq $role } load_roles
();
135 _warn
"'$role $user' already present in file";
137 push @text, "$role $user\n";
139 _print
( $pf, @text );
147 return @rules if @rules;
149 require Gitolite
::Conf
::Load
;
150 Gitolite
::Conf
::Load
::load
($repo);
152 my %repos = %Gitolite::Conf
::Load
::repos
;
153 my @repo_memberships = Gitolite
::Conf
::Load
::memberships
('repo', $repo);
155 for my $rp (@repo_memberships) {
156 my $hr = $repos{$rp};
157 for my $r ( keys %$hr ) {
158 next unless $r =~ s/^@//;
159 next unless $rc{ROLES
}{$r};
160 map { $_->[3] = $r } @
{ $hr->{"\@$r"} };
161 push @rules, @
{ $hr->{"\@$r"} };
170 print STDERR
"Invalid role '$role'; valid roles for this repo:\n";
171 open(STDOUT
, '>&', \
*STDERR
); # make list_roles print to STDERR
178 my @rules = sort { $a->[0] <=> $b->[0] } load_roles
();
181 $_->[2] =~ s
(^refs
/heads/)();
182 $_->[2] = '--any--' if $_->[2] eq 'refs/.*';
186 map { $max = $_ if $_ > $max } map { length($_->[2]) } @rules;
187 printf("\t%s\t%*s\t \t%s\n", "perm", -$max, "ref", "role");
188 printf("\t%s\t%*s\t \t%s\n", "----", -$max, "---", "----");
189 printf("\t%s\t%*s\t=\t%s\n", $_->[1], -$max, $_->[2], $_->[3]) for @rules;