使用Git Hooks钩子可以用来做一些很灵活的限制,例如限制只有某些用户可以往某些分支提交代码。

这个是Gogs测试的限制某个用户提交的hook:

#!/bin/sh 
# protect branch from pushes by non-authorized users
# Volker Kuhlmann (2017)

# protected branch name 
protected_branch="refs/heads/master" 
# list of authorized users
authorized_users="tangxiaoping,test"
userArr=$(echo $authorized_users|tr "," "\n")

# return value : 0 is OK 
return_value=0 

# test the query branch against the protected branch
while read oldrev newrev refname ; do 
  if test "$refname" = "$protected_branch" 
  then 
    echo "CHECKS authorization for protected branch" $protected_branch
    for user in $userArr; do
        # test the current user against list of authorized users
      if [[ "$GOGS_AUTH_USER_NAME" == "$user" ]]; 
      then
         # return value : 1 is NOT OK 
         return_value=1 
      else
           return_value=0
      fi
    done
  fi 
  if [[ "$return_value==1" ]]; 
     then
     # return value : 1 is NOT OK 
     echo "DENIES push to protected branch by" $GOGS_AUTH_USER_NAME
  fi
done 

exit $return_value 

相关参考链接:

  1. https://stackoverflow.com/questions/8781240/a-way-to-restrict-git-branch-access
文档更新时间: 2019-06-10 16:05   作者:nick