我想限制某些用户将更改集推送到存储库的默认分支。如果有可能,你会怎么做?
答案 0 :(得分:4)
ACL extension应该对你有用。但是,您需要考虑以下因素:
必须在服务器存储库中启用扩展。也就是说,每个服务存储库的hgrc
文件应该定义ACL设置:
[extensions]
acl =
[hooks]
pretxnchangegroup.acl = python:hgext.acl.hook
[acl]
sources = serve
[acl.deny.branches]
default = user1, user2, user3
推送被拒绝的用户是系统用户。也就是说,用户名取自您的案例中Web服务器提供的凭据。它与提交元数据中的Author:
字段有无。
你也可以编写自己的pretxnchangegroup
钩子,但是你的能力不会超过ACL扩展。
答案 1 :(得分:0)
当前的答案将在您按下按钮时进行检查(检查是在服务器端,正如我从acl代码中得出的那样)。您要检查对本地存储库的提交。为此,您应该制作一个“ pretxncommit”钩子(请注意,有多种钩子可作用于不同的事件)。
执行以下操作:
根据A successful Git branching model,主服务器上不应有直接提交,只能合并。为此,我们可以在mercurial上添加一个回调钩子以检查提交,如果它们直接在master上则禁止它们。为此,在项目的.hg / hgrc中添加以下代码行:
[hooks]
pretxncommit.nocommittomasterhook = python:%USERPROFILE%\hgnocommittomaster.py:nocommittomaster
在Windows主目录中,使用内容(original example)创建文件'hgnocommittomaster.py
':
from mercurial.node import bin, nullid
from mercurial import util
# Documentation is here: https://www.mercurial-scm.org/wiki/MercurialApi
# Script based on: https://www.mercurial-scm.org/wiki/HookExamples
def nocommittomaster(ui, repo, node, **kwargs):
n = bin(node)
start = repo.changelog.rev(n)
end = len(repo.changelog)
failed = False
for rev in xrange(start, end):
n = repo.changelog.node(rev)
ctx = repo[n]
p = ctx.parents()
if ctx.branch() == 'master' and len(p) == 1:
if p[0].branch() != 'master':
# commit that creates the branch, allowed
continue
if len(ctx.files()) == 0 and len(ctx.tags()) == 1: # will not hit?, '.hgtags' always changed?
continue # Only a tag is added; allowed.
elif len(ctx.files()) == 1 and len(ctx.tags()) == 1:
if ctx.files()[0] == '.hgtags':
continue # Only a tag is added; allowed.
ui.warn(' - changeset rev=%d (%s) on stable branch and is not a merge !\n' % (rev,ctx))
failed = True
if failed:
ui.warn('* Please strip the offending changeset(s)\n'
'* and re-do them, if needed, on another branch!\n')
return True
这篇文章的灵感来自:Mercurial pre commit hook和Mercurial Pre-Commit Hook: How to hook to python program in current directory?