tags:

views:

45

answers:

2

Is it possible to modify the behavior of a core Mercurial command (e.g. hg commit or hg status) by creating an extension?

For example, would it be possible to modify hg commit to ask the user to enter an issue tracking ID?

I understand that hook scripts could be used, but such scripts are not distributed via hg pull and need to be configured for every repository used.

+3  A: 

Answering my own question

The Mercurial API provides the extensions.wrapcommand(table, command, wrapper) method which seems to provide the desired feature.

From the source code:

Wrap the command named `command' in table.

Replace command in the command table with wrapper. The wrapped command will be inserted into the command table specified by the table argument. The wrapper will be called like wrapper(orig, *args, **kwargs) where orig is the original (wrapped) function, and *args, **kwargs are the arguments passed to it.

A couple examples:

  • The prompt extension by Steve Losh
  • The nobranch extension from Fog Creek
Tim Henigan
Beware when wrapping commands not to alter the output. Eclipse and other environments use the command line output for their integration and will choke if it varies.
Ry4an
+1  A: 

Just to note: both extensions and hooks have exactly the same mass-deployment limitations. In both cases you have to induce your internal users to download a piece of software, be it a hook or an extension, and then to enable it in either the hgrc in their homedir or within the repo.

For both hooks and extensions you can distribute the software using any mechanism and can enable them globally in /etc/mercurial/hgrc

Extensions have some advantages over hooks as to how deep they can dig in the mercurial internals, but deployment is identical.

Ry4an