5 # If you are having trouble with this access control hook script
6 # you can try setting this to true. It will tell you exactly
7 # why a user is being allowed/denied access.
11 # Default shell globbing messes things up downstream
15 $verbose && echo >&2 "-Grant- $1"
21 $verbose && echo >&2 "-Deny- $1"
27 $verbose && echo >&2 "-Info- $1"
32 # Implement generic branch and tag policies.
33 # - Tags should not be updated once created.
34 # - Branches should only be fast-forwarded.
37 [ -f "$GIT_DIR/$1" ] &&
38 deny
>/dev
/null
"You can't overwrite an existing tag"
41 # No rebasing or rewinding
42 if expr "$2" : '0*$' >/dev
/null
; then
43 info
"The branch '$1' is new..."
45 # updating -- make sure it is a fast forward
46 mb
=$
(git-merge-base
"$2" "$3")
48 "$2,$mb") info
"Update is fast-forward" ;;
49 *) deny
>/dev
/null
"This is not a fast-forward update." ;;
55 "Branch is not under refs/heads or refs/tags. What are you trying to do?"
59 # Implement per-branch controls based on username
60 allowed_users_file
=$GIT_DIR/info
/allowed-users
63 if [ -f "$allowed_users_file" ]; then
64 rc
=$
(cat $allowed_users_file |
grep -v '^#' |
grep -v '^$' |
65 while read head_pattern user_patterns
; do
66 matchlen
=$
(expr "$1" : "$head_pattern")
67 if [ "$matchlen" == "${#1}" ]; then
68 info
"Found matching head pattern: '$head_pattern'"
69 for user_pattern
in $user_patterns; do
70 info
"Checking user: '$username' against pattern: '$user_pattern'"
71 matchlen
=$
(expr "$username" : "$user_pattern")
72 if [ "$matchlen" == "${#username}" ]; then
73 grant
"Allowing user: '$username' with pattern: '$user_pattern'"
76 deny
"The user is not in the access list for this branch"
81 grant
) grant
>/dev
/null
"Granting access based on $allowed_users_file" ;;
82 deny
) deny
>/dev
/null
"Denying access based on $allowed_users_file" ;;
88 allowed_groups_file
=$GIT_DIR/info
/allowed-groups
90 info
"The user belongs to the following groups:"
93 if [ -f "$allowed_groups_file" ]; then
94 rc
=$
(cat $allowed_groups_file |
grep -v '^#' |
grep -v '^$' |
95 while read head_pattern group_patterns
; do
96 matchlen
=$
(expr "$1" : "$head_pattern")
97 if [ "$matchlen" == "${#1}" ]; then
98 info
"Found matching head pattern: '$head_pattern'"
99 for group_pattern
in $group_patterns; do
100 for groupname
in $groups; do
101 info
"Checking group: '$groupname' against pattern: '$group_pattern'"
102 matchlen
=$
(expr "$groupname" : "$group_pattern")
103 if [ "$matchlen" == "${#groupname}" ]; then
104 grant
"Allowing group: '$groupname' with pattern: '$group_pattern'"
108 deny
"None of the user's groups are in the access list for this branch"
113 grant
) grant
>/dev
/null
"Granting access based on $allowed_groups_file" ;;
114 deny
) deny
>/dev
/null
"Denying access based on $allowed_groups_file" ;;
119 # Who can create tags
120 tagmatch
=$
(expr "$1" : "refs/tags")
121 info
"\$1 = $1, tagmatch = $tagmatch"
123 if [ "$tagmatch" == "9" ]; then
124 # We are creating a tag
125 if [ -f "$allowed_users_file" ]; then
126 rc
=$
(cat $allowed_users_file |
grep -v '^#' |
grep -v '^$' |
127 while read head_pattern user_patterns
; do
128 matchlen
=$
(expr "refs/tags" : "$head_pattern")
129 if [ "$matchlen" == "9" ]; then
130 info
"Found matching tag pattern: '$head_pattern'"
131 for user_pattern
in $user_patterns; do
132 info
"Checking user: '$username' against pattern: '$user_pattern'"
133 matchlen
=$
(expr "$username" : "$user_pattern")
134 if [ "$matchlen" == "${#username}" ]; then
135 grant
"Allowing user: '$username' with pattern: '$user_pattern'"
138 deny
"The user is not in the access list for refs/tags "
143 grant
) grant
>/dev
/null
"Granting access based on $allowed_users_file" ;;
144 deny
) deny
>/dev
/null
"Denying access based on $allowed_users_file" ;;
150 deny
>/dev
/null
"There are no more rules to check. Denying access"