我在下面写到了pre-push钩子,但是只有当当前分支是master时,它才停止推送如果您运行“ git push -u origin master”,并且您位于例如mtest分支,则此方法无效。我还尝试查看是否可以获取push参数,以便我们可以知道push定向到哪个分支,但是不幸的是,在pre-push钩接收到的参数中没有得到该信息。
#!/bin/python
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
import subprocess
import sys
import re
def run(cmd):
process=subprocess.Popen(cmd,stdout=subprocess.PIPE, stdin=subprocess.PIPE)
stdout,stderr= process.communicate()
status=process.poll()
return [status,stderr,stdout]
print(sys.argv)
version=run("git version")
curr_branch=run("git rev-parse --abbrev-ref HEAD")
c_branch=curr_branch[2].decode("utf-8").rstrip()
print("Git Version: {}".format(version[2].decode("utf-8").rstrip()))
print("Current Branch: {}".format(curr_branch[2].decode("utf-8").rstrip()))
if (c_branch == "master"):
print("Push to Master Branch is Restricted.. Exiting" )
exit(1)
#elif "origin" in sys.argv:
# print("Push is restricted")
# exit(1)
else:
exit(0)
在其他分支中并尝试推送到主节点时输出
U MINGW64 /c/python_stuff/my_repos/m_fullapp/m_full_app/.git/hooks (GIT_DIR!)
$ git push -u origin master
['C:/python_stuff/my_repos/m_fullapp/m_full_app/.git/hooks/pre-push', 'origin', '[email protected]:magic/m_full_app.git']
Git Version: git version 2.9.0.windows.1
Current Branch: mtest
Branch master set up to track remote branch master from origin.
Everything up-to-date
在master分支中时输出,在这里工作正常
$ git push -u origin master
Git Version: git version 2.9.0.windows.1
Current Branch: master
Push to Master Branch is Restricted.. Exiting
error: failed to push some refs to '[email protected]:magic/m_full_app.git'
我需要一种方法来完全阻止对master分支的推送。但推送到其他分支机构必须照常进行。
任何方式。
Settings
Branches
Add rule
Require pull request reviews before merging
或Require status checks to pass before merging
请参阅有关预推钩子的文档,以及任何回购随附的.git/hooks/pre-push.sample
:
关于目标引用的信息是通过stdin传递的,而不是通过sys.argv
传递的>