所以我最终用 Mercurial 钩子解决了这个问题。具体来说,我创建了以下文件,我将其命名为 prevent_default_push.py 并放在我的克隆的 .hg 目录中。
# branches to prevent being pushed to by command line.
# Separate branches by spaces
restricted_branches = "default".lower().split()
def branch_name(repo):
return repo[None].branch().lower()
def is_restricted(branch):
return branch in restricted_branches
def prevent_default(ui, repo, *args, **kwargs):
if is_restricted(branch_name(repo)):
print("Preventing push to default branch")
return True
return False
def prevent_commit(ui, repo, *args, **kwargs):
branch = branch_name(repo)
if is_restricted(branch):
print("You're on a restricted branch (%s), are you sure you want to commit? [YN]" % branch)
res = raw_input().strip().lower()
return res != "y"
return False
然后编辑.hg/hgrc 文件以使用这些钩子:
[hooks]
pre-push = python:.hg/prevent_default_push.py:prevent_default
pre-commit = python:.hg/prevent_default_push.py:prevent_commit
然后,当您尝试在 default 分支上进行提交时,您会收到确认提示,如果您尝试向 default 推送,则会直接被拒绝。
示例运行:
$ hg commit
You're on a restricted branch (default), are you sure you want to commit? [YN]
n
abort: pre-commit hook failed
其中“n”是我输入的内容。
这样做的好处是,虽然您的 .hg 目录中的内容不受版本控制(因此克隆不会得到它),但我们可以将这些钩子自动放入我们的配置机制中放置在已配置的 VM 上。