Questions regarding hooks

Martin Geisler mg at lazybytes.net
Fri Jun 18 06:02:18 CDT 2010


Didly Bom <didlybom at gmail.com> writes:

> On Thu, Jun 17, 2010 at 2:54 PM, Martin Geisler <mg at aragost.com> wrote:
>
>> Didly Bom <didlybom at gmail.com> 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 dispatch.py, 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.
>
>
> Martin, I missed this part of your replay on your previous email. What
> exactly would you like to see documented on the Wiki? Are you
> referring to the method to register hooks through extensions as
> discussed in this thread? Or are you thinking about documenting which
> ui object is used where? I don't feel qualified enough to answer this
> last question, although I definitely see the value of having that
> properly documented, since it is quite confusing.

Documenting that ui.setconfig('hooks', ...) works for an extension would
be a good start -- some people may expect to use some other API.

I was hoping you could do some experiments to become qualified to answer
the second question :-) Like adding "print ui" statements to uisetup,
reposetup, extsetup and document what's going on.

Okay, I just did the experiment with this extension:

  def uisetup(ui):
      print "uisetup:", ui

  def extsetup(ui):
      print "extsetup:", ui

  def reposetup(ui, repo):
      print "reposetup:", ui

and got this output:

  uisetup: <mercurial.ui.ui object at 0xb737760c>
  extsetup: <mercurial.ui.ui object at 0xb737760c>
  reposetup: <mercurial.ui.ui object at 0x84f4a8c>

Someone should add it to the wiki with a suitable explanation about why
there is a different ui object for the repository. This also explains
where you need to register the hooks -- perhaps you could add a list
that maps the hooks listed here

  http://www.selenic.com/mercurial/hgrc.5.html#hooks

to the right ui object.

I guess the reason this isn't documented yet is that it doesn't matter
when you configure things in a hgrc file. Or maybe it does matter if you
configure a hook in a .hg/hgrc file... It would be super if you could
test that and document if such a hook is present in repo.ui only.

-- 
Martin Geisler

Mercurial links: http://mercurial.ch/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://selenic.com/pipermail/mercurial-devel/attachments/20100618/85274350/attachment.pgp>


More information about the Mercurial-devel mailing list