I'm running the central mercurial repository, and I understand that the normal "push" command will stop if the remote user is trying to force multiple "head"s to my central repository. The intention is that the remote user should first pull and merge before trying to push again.

However, using hg push --force will override this. I would like to block this behavior.

I am currently using the hgwebdir.cgi plus some apache-auth stuff to limit users ability to pull and push.

EDIT: a pretxnchangegroup hook solved the problem. Hook worked:

# force-one-head
# add the following to <repository>/.hg/hgrc :
# [hooks]
# pretxnchangegroup.forceonehead = /path/to/force-one-head

if [[ `hg heads -q | wc -l` -gt 1 ]]; then
    echo "There are multiple heads."
    echo "Please 'hg pull' and get your repository up to date first."
    echo "Also, don't 'hg push --force' because that won't work either."
    exit 1
  • Did you arrive at a solution? – kmarsh Aug 24 '09 at 14:50
  • 1
    The "Also, don't 'hg push --force' because that won't work either." part of your script is not needed. If users do hg push, then Mercurial will abort before anything is pushed and your script is not executed. If they do hg push --force, then your script is executed but so the line is redundant. – Martin Geisler Dec 18 '11 at 10:52
  • I think most workflows would allow users to push a new head on a feature branch, but only want to prevent users pushing multiple heads to the default branch. For this, replace hg heads -q in the script above with hg heads -q default. – mo. Jun 25 '14 at 14:44

This is not an Apache change, but you have to set it in the Mercurial repository itself.

You can setup hooks that run a script before accepting a push into your repository. In the scripts triggered by the pretxncommit or pretxnchangegroup hooks you can check if those changes create a new head and refuse them if they do.

See the chapter on hooks in the Hg Book for more details.

  • 1
    looks like pretxncommit is the place to put this hook, but is there a pre-installed hook for this sort of test (like the acl or bugzilla hooks)? Will I have to create an external hook? If you have any suggestions for testing this, I'm all ears. – user9748 Jul 5 '09 at 18:36

There is no hook supplied that will create this behavior. You will need to write it yourself.


Consider putting the repository on an access-controlled server, and then implementing mercurial-server (formerly known as hg-admin-tools). Users push through SSH (from Windows or Linux) and are subject to your access controls.

There are two permissions files that will give you fine-grained control over who can read versus push. Multiple repositories can be set up under one management system, so you don't have to install it over and over again.

Not the answer you're looking for? Browse other questions tagged or ask your own question.