问题
How can I configure a Mercurial server to restrict commits to a named branch once it has been closed? I only want the repository administrator to have the ability to reopen the branch.
https://www.mercurial-scm.org/wiki/PruningDeadBranches says that closed changesets can be identified by "close=1 in the changeset's extra field". It's not clear how to read a changeset's extra field using the Mercurial API.
回答1:
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.
- https://www.mercurial-scm.org/wiki/AclExtension
Configuring acls:
[acl.deny.branches]
frozen-branch = *
[acl.allow.branches]
branch_name = admin
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/3961548/prevent-pushing-of-commits-that-add-to-closed-branches