#!/bin/sh
# Gitolite permission manipulation helper script
-# Copyright 2011, Michal Sojka <wsh@2x.cz>
+# Copyright 2011, 2014, 2017, Michal Sojka <wsh@2x.cz>
# License: GNU GPLv2+
+case "$1" in
+ -h|-help)
+cat <<EOF
+Usage: $0 Print current permissions
+ $0 + READERS <user> Allow <user> to read the repo (@all for all users)
+ $0 - READERS <user> Remove <user> from the list of readers
+ $0 + WRITERS <user> Allow <user> to write the repo
+EOF
+ exit
+ ;;
+esac
curr_branch=$(git symbolic-ref -q HEAD)
-curr_branch_short="${curr_branch#refs/heads/}"
-remote=$(git config --get branch.$curr_branch_short.remote)
-url=$(git config --get remote.$remote.url) || url=$remote
+curr_branch_short=${curr_branch#refs/heads/}
+remote=$(git config --get "branch.$curr_branch_short.remote")
+url=$(git config --get "remote.$remote.url") || url=$remote
url_sans_ssh=${url#ssh://}
-if test x$url_sans_ssh != x$url; then
+if [ x"$url_sans_ssh" != x"$url" ]; then
userhost=${url_sans_ssh%%/*}
repo=${url_sans_ssh#*/}
else
userhost=${url%%:*}
repo=${url#*:}
fi
+repo=${repo%.git}
-bn=$(basename $0)
-if test x$bn = xgl-perm; then
- cmd=$1; shift
-else
- cmd=$bn
-fi
-
-case $cmd in
- gl-getperms|get)
- if test $# -gt 0; then echo >&2 "Command $cmd does not accept any arguments"; exit 1; fi
- ssh $userhost getperms $repo;;
- gl-setperms|set)
- if test $# -gt 0; then echo >&2 "Command $cmd does not accept any arguments - it reads from stdin"; exit 1; fi
- ssh $userhost setperms $repo;;
- gl-addperm|add)
- perms=$(ssh $userhost getperms $repo)
- ( test "$perms" && echo "$perms"; echo $* ) | ssh $userhost setperms $repo;;
- gl-rmperm|rm)
- user=$1
- perms=$(ssh $userhost getperms $repo) # setperms does not work when executed in the same pipe with getperms
- echo "$perms" | sed -e "s/[[:space:]]*\\b$user\\b//g" -e "/^[[:alnum:]]\+[[:space:]]*$/d" | ssh $userhost setperms $repo;;
+case "$(basename "$0")" in
+ gl-perm|gl-perm-list)
+ set -- "$repo" -l;;
+ gl-perm-add-writer)
+ set -- "$repo" + WRITERS "${1?Missing user name}";;
+ gl-perm-add-reader)
+ set -- "$repo" + READERS "${1?Missing user name}";;
+ gl-perm-rm-writer)
+ set -- "$repo" - WRITERS "${1?Missing user name}";;
+ gl-perm-rm-reader)
+ set -- "$repo" - READERS "${1?Missing user name}";;
esac
+
+(set -x; ssh "$userhost" perms "$@")