Useful Mercurial Hooks

My favorite hook for formal repositories is the one that refuses multiple heads. It's great when you've got a continuous integration system that needs a post-merge tip to build automatically.

A few examples are here: MercurialWiki: TipsAndTricks - prevent a push that would create multiple heads

I use this version from Netbeans:

# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.
#
# To forbid pushes which creates two or more headss
#
# [hooks]
# pretxnchangegroup.forbid_2heads = python:forbid2_head.forbid_2heads

from mercurial import ui
from mercurial.i18n import gettext as _

def forbid_2heads(ui, repo, hooktype, node, **kwargs):
    if len(repo.heads()) > 1:
        ui.warn(_('Trying to push more than one head, try run "hg merge" before it.\n'))
        return True

Another good hook is this one. It allows multiple heads, but only if they are in different branches.

Single head per branch

def hook(ui, repo, **kwargs):
    for b in repo.branchtags():
        if len(repo.branchheads(b)) > 1:
            print "Two heads detected on branch '%s'" % b
            print "Only one head per branch is allowed!"
            return 1
    return 0

I've just created a small pretxncommit hook that checks for tabs and trailing whitespace and reports it rather nicely to the user. It also provides a command for cleaning up those files (or all files).

See the CheckFiles extension.