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; see the
30 source code of the perms command for details.
33 # BATCH MODE: DO NOT combine this with the +/- mode above. This mode also
34 # creates the repo if it does not already exist (assuming $GL_USER has
35 # permissions to create it).
38 # cat copy-of-backed-up-gl-perms | ssh git@host perms -c <repo>
40 usage
() if not @ARGV or $ARGV[0] eq '-h' or @ARGV < 2;
42 $ENV{GL_USER
} or _die
"GL_USER not set";
44 my $generic_error = "repo does not exist, or you are not authorised";
46 if ( $ARGV[1] eq '-l' ) {
47 getperms
($ARGV[0]); # doesn't return
50 # auto-create the repo if -c passed and repo doesn't exist
51 if ( $ARGV[0] eq '-c' ) {
53 my $repo = $ARGV[0] or usage
();
54 _die
"invalid repo '$repo'" unless $repo =~ $REPONAME_PATT;
56 if ( not -d
"$rc{GL_REPO_BASE}/$repo.git" ) {
57 unless ($ENV{GL_BYPASS_CREATOR_CHECK
}) {
58 my $ret = Gitolite
::Conf
::Load
::access
( $repo, $ENV{GL_USER
}, '^C', 'any' );
59 _die
$generic_error if $ret =~ /DENIED/;
62 require Gitolite
::Conf
::Store
;
63 Gitolite
::Conf
::Store
->import;
64 new_wild_repo
( $repo, $ENV{GL_USER
}, 'perms-c' );
65 gl_log
( 'create', $repo, $ENV{GL_USER
}, 'perms-c' );
71 if ( @ARGV and $ARGV[0] eq '-lr' ) {
80 require Gitolite
::Cache
;
81 Gitolite
::Cache
::cache_control
('flush', $repo);
84 _system
( "gitolite", "trigger", "POST_CREATE", $repo, $ENV{GL_USER
}, 'perms' );
86 # ----------------------------------------------------------------------
90 _die
$generic_error if not owns
($repo);
91 my $pf = "$rc{GL_REPO_BASE}/$repo.git/gl-perms";
93 print slurp
($pf) if -f
$pf;
99 _die
$generic_error if not owns
($repo);
100 my $pf = "$rc{GL_REPO_BASE}/$repo.git/gl-perms";
103 # legacy mode; pipe data in
104 print STDERR
"'batch' mode started, waiting for input (run with '-h' for details).\n";
105 print STDERR
"Please enter 'cancel' to abort if you did not intend to do this.\n";
109 _die
"CANCELLED" if /^\s*cancel\s*$/i;
110 invalid_role
($1) if /(\S+)/ and not $rc{ROLES
}{$1};
118 _die
"Invalid syntax. Please re-run with '-h' for detailed usage" if @_ != 3;
119 my ( $op, $role, $user ) = @_;
120 _die
"Invalid syntax. Please re-run with '-h' for detailed usage" if $op ne '+' and $op ne '-';
121 _die
"Invalid user '$user'" if not $user =~ $USERNAME_PATT;
124 my @text = slurp
($pf) if -f
$pf;
126 my $present = grep { $_ eq "$role $user\n" } @text;
129 if ( not $present ) {
130 _warn
"'$role $user' was not present in file";
132 @text = grep { $_ ne "$role $user\n" } @text;
133 _print
( $pf, @text );
136 invalid_role
($role) unless grep { $_->[3] eq $role } load_roles
();
138 _warn
"'$role $user' already present in file";
140 push @text, "$role $user\n";
142 _print
( $pf, @text );
150 return @rules if @rules;
152 require Gitolite
::Conf
::Load
;
153 Gitolite
::Conf
::Load
::load
($repo);
155 my %repos = %Gitolite::Conf
::Load
::repos
;
156 my @repo_memberships = Gitolite
::Conf
::Load
::memberships
('repo', $repo);
158 for my $rp (@repo_memberships) {
159 my $hr = $repos{$rp};
160 for my $r ( keys %$hr ) {
161 next unless $r =~ s/^@//;
162 next unless $rc{ROLES
}{$r};
163 map { $_->[3] = $r } @
{ $hr->{"\@$r"} };
164 push @rules, @
{ $hr->{"\@$r"} };
173 print STDERR
"Invalid role '$role'; valid roles for this repo:\n";
174 open(STDOUT
, '>&', \
*STDERR
); # make list_roles print to STDERR
181 my @rules = sort { $a->[0] <=> $b->[0] } load_roles
();
184 $_->[2] =~ s
(^refs
/heads/)();
185 $_->[2] = '--any--' if $_->[2] eq 'refs/.*';
189 map { $max = $_ if $_ > $max } map { length($_->[2]) } @rules;
190 printf("\t%s\t%*s\t \t%s\n", "perm", -$max, "ref", "role");
191 printf("\t%s\t%*s\t \t%s\n", "----", -$max, "---", "----");
192 printf("\t%s\t%*s\t=\t%s\n", $_->[1], -$max, $_->[2], $_->[3]) for @rules;