Prevent pushing of commits that add to closed branches

为君一笑 提交于 2019-12-05 03:27:29
pyfunc

There is an ACL extension that is distributed along with Mercurial. You should be able to specify the frozen branches by denying commit to every one except the administrator. I am not sure if named branches can leverage this facility.

Configuring acls:

[acl.deny.branches] 
frozen-branch = *

[acl.allow.branches]
branch_name = admin

A server can't restrict commits, but it can refuse to accept pushes that violate constraints. Here's a hook you can put on a server to reject any pushes that have any changesets that are on a closed branch:

#!/bin/sh
for thenode in $(hg log -r $HG_NODE:tip --template '{node}\n') ; do
     if hg branches --closed | grep -q "^$(hg id --branch -r $thenode).*\(closed\)"  ; then
          echo Commits to closed branches are not allowed -- bad changeset $thenode
          exit 1
     fi
done

You'd install that hook like this:

[hooks]
prechangegroup = /path/to/that.sh

There's almost certainly a way to do it using in-python hooks with the API you referenced, but shell hooks work out pretty well too.

Here's an in-process hook that should reject additional changesets on a closed branch.

from mercurial import context, ui
def run(ui, repo, node, **kwargs):
    ctx = repo[node]
    for rev in xrange(ctx.rev(), len(repo)):
        ctx = context.changectx(repo, rev)
        parent1 = ctx.parents()[0]
        if parent1 != None and parent1.extra().get('close'):
            ui.warn("Commit to closed branch is forbidden!\n")
            return True
    return False

The hook can run in pretxncommit mode (checked during a local commit transaction) or pretxnchangegroup mode (checked when changesets added from external repo) with the following hgrc entries:

[hooks]
pretxncommit.forbid_commit_closed_branch = python:/path/to/forbid_commit_closed_branch.py:run
pretxnchangegroup.forbid_commit_closed_branch = python:/path/to/forbid_commit_closed_branch.py:run

Not sure if this hook will work with Mercurial versions prior to 2.2.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!