使用进程内挂钩的方法是以下代码。这些函数可以.hgrc
像这样在 Mercurial 存储库中使用。
pretxncommit.foo = python:mercurial_hooks.abort_commit_to_wrong_branch
pre-qfinish.bar = python:mercurial_hooks.qfinish_abort_commit_to_wrong_branch
abort_commit_to_wrong_branch
不允许正常提交到错误的分支,但允许 MQ 提交。qfinish_abort_commit_to_wrong_branch
阻止 qfinish 将错误分支上的 MQ 提交转换为常规提交。
我finish
在
https://bitbucket.org/mirror/mercurial/src/tip/hgext/mq.py?at=default#cl-3034中使用了该功能以
供参考。
def abort_commit_to_wrong_branch(ui, repo, **kwargs):
"""
Don't allow commits to 'debian' branch including files not
contained in the 'debian/' directory. Also don't allow commits to
non-'debian' branches including files contained in the 'debian/'
directory. Don't restrict MQ commits.
"""
# If repo has '_committingpatch' attribute, then it is an mq
# commit in progress, so return 'False'
import os
ctx = repo[kwargs['node']]
files = ctx.files()
branch = ctx.branch()
if hasattr(repo, "_committingpatch"):
for f in files:
d = os.path.dirname(f)
if branch == "debian" and d != "debian":
ui.warn("Warning: committing %s (file not in 'debian' directory) to 'debian' branch. Allowed since this ia an MQ commit.\n"%f)
if branch != "debian" and d == "debian":
ui.warn("Warning: committing %s (file in 'debian' directory) to non 'debian' branch. Allowed since this ia an MQ commit.\n"%f)
return False
for f in files:
d = os.path.dirname(f)
if branch == "debian" and d != "debian":
ui.warn("Error: cannot commit %s (file not in 'debian' directory) to 'debian' branch\n"%f)
return True
if branch != "debian" and d == "debian":
ui.warn("Error: cannot commit %s (file in 'debian' directory) to non 'debian' branch\n"%f)
return True
def qfinish_abort_commit_to_wrong_branch(ui, repo, **kwargs):
"""
Don't allow qfinish on 'debian' branch including files not
contained in the 'debian/' directory. Also don't allow qfinish on
non-'debian' branches including files contained in the 'debian/'
directory. Don't restrict MQ commits.
"""
from mercurial import scmutil
import os
if not repo.mq.applied:
ui.status(('no patches applied\n'))
return True
opts = kwargs['opts']
# case corresponding to `-a`. no revisions specified.
if opts.get('applied'):
revrange = ('qbase::qtip',)
# case where revision(s) specified
revrange = kwargs['pats']
revs = scmutil.revrange(repo, revrange)
# loop over revisions
for rev in revs:
ctx = repo[rev]
files = ctx.files()
branch = ctx.branch()
for f in files:
d = os.path.dirname(f)
if branch == "debian" and d != "debian":
ui.warn("Error: cannot commit %s (file not in 'debian' directory) to 'debian' branch\n"%f)
return True
if branch != "debian" and d == "debian":
ui.warn("Error: cannot commit %s (file in 'debian' directory) to non 'debian' branch\n"%f)
return True