如何完全阻止/限制向远程主服务器的推送

问题描述 投票:-1回答:2
  • 我想限制从任何分支到主分支的任何类型的推送。
  • 我想从客户端执行所有这些操作
  • 例如,我有3个分支master mtest mtest2
  • 我不希望“ git push -u origin master”在任何分支机构中都能工作。
  • 如果我签出了mtest,但是如果我错误地运行了“ git push -u origin master”,那么这将不起作用。

我在下面写到了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分支的推送。但推送到其他分支机构必须照常进行。

任何方式。

git github hook githooks
2个回答
0
投票
  1. 打开GitHub存储库
  2. 单击Settings
  3. 单击侧边栏中的Branches
  4. 单击Add rule
  5. 启用Require pull request reviews before mergingRequire status checks to pass before merging

0
投票

请参阅有关预推钩子的文档,以及任何回购随附的.git/hooks/pre-push.sample

关于目标引用的信息是通过stdin传递的,而不是通过sys.argv传递的>

© www.soinside.com 2019 - 2024. All rights reserved.