Questions regarding hooks

Martin Geisler mg at
Thu Jun 17 07:54:16 CDT 2010

Didly Bom <didlybom at> writes:

> Hi,
> I have two questions regarding hooks:
> I know that you can register a hook from a plugin by using
> ui.setconfig("hooks", HOOK_NAME, HOOK_FUNCTION)
> - Does it matter which ui object is used to register the hook? That
>   is, does it matter if I setup the hook using the global ui object
>   using the repo.ui object?

The hooks are stored by name in the ui object. When hooks are run, they
are triggered by code like this from dispatch.runcommand:

    # run pre-hook, and abort if it fails
    ret = hook.hook(lui, repo, "pre-%s" % cmd, False, args=" ".join(fullargs),
                    pats=cmdpats, opts=cmdoptions)

This code executed the pre-<cmd> hook configured in the lui object. So
it matters which ui object you use when configuring the hook. If you
start in, then you can trace your way through the code and
see which ui object is used where.

I find this confusing as well and I would love if you could document it
on the MercurialApi page on the Wiki.

> - Is it ok to setup the same hook multiple times? Will the hook simply
>   be set to the last hook function that was configured or will the
>   hook run multiple times? In particular:
>     - is it ok if I setup the same hook from the repo.ui object and
>       then also from the global ui object?

There are not a lot of internal rules about hooks -- they are just
executed. Sometimes, their return value is used to signal if the
operation should proceed or if it should be aborted.

In any case, the code that triggers a hook will trigger it based on a
single ui object only. So if you register a hook on two ui objects, then
only one will actually be executed.

>     - is it ok to setup the same hook programatically using one of the
>       ui objects and also using the [hooks] section on the
>       mercurial.ini file?

As long as they use different names, then there is no conflicts. So one
could use 'update.x' and another could use 'update.y'. Otherwise the one
configured last will overwrite the first.

> P.S.- I am not sure whether I should have sent this to the mercurial
> list rather than the mercurial-devel list.

This list is the right one -- you are asking questions about Mercurial
internals and about how to write code for Mercurial.

Martin Geisler

aragost Trifork
Professional Mercurial support

More information about the Mercurial-devel mailing list