阻止开发人员创建新的git远程分支

时间:2015-04-24 09:56:53

标签: git git-branch

在集中式工作流中,有没有办法(使用本地或服务器配置)阻止开发人员在git中创建新的远程分支?

我们很乐意创建本地分支机构,但有时这个本地spikestest-feature分支机构会错误地远程访问。

如果不采用更有限的工作流程,是否可以避免这种情况?

3 个答案:

答案 0 :(得分:1)

您可以在服务器中使用挂钩,因此将扫描推送并拒绝任何新分支。

将此挂钩放在共享存储库的“hooks”文件夹中(脚本应命名为“update”)

#!/bin/env python
"""
Server side hook that checks if a branch being pushed already exists and rejects
the commit if not

It prevents the creation of new branches in a shared repository
"""

import sys
import os

branch_id = sys.argv[1]

# Tag pushes are not blocked
if branch_id.startswith('refs/tags'):
    exit(0)
elif not os.path.exists(branch_id):
    print("You are not allowed to create new branches")
    exit(1)

检查this link以了解更多有关git强制执行政策的信息

修改 根据jthill的反馈,有一个替代实现,而不是搜索分支ref文件,主动详细说明现有分支的列表(可以通过调用管道命令而不是调用“git branch”来改进它)正如我在这里做的那样)

#!/bin/env python

import sys
import os
from subprocess import check_output


def tags():
    return [i.lstrip(' *') for i in check_output(["git", "branch"]).split('\n') if i!='']


def main():
    branch_id = sys.argv[1]

    # Only ref/heads are being processed
    if branch_id.startswith('refs/heads/'):
        if not branch_id.lstrip('refs/heads/') in tags():
            exit(1)

if __name__ == '__main__':
    main()

答案 1 :(得分:1)

.git/hooks/pre-receive

#!/bin/sh
rc=0
while read old new refname; do
        if [[ $refname == refs/heads/* && $old != *[^0]* ]]; then
                rc=1
                echo "Refusing to create new branch $refname"
        fi
done
exit $rc

答案 2 :(得分:0)

你使用什么git服务器?

例如,在gitolite中,您可以根据分支/标记名称和用户"登录"设置ACL。 (参见文档here