Search code examples
pythonmercurialmercurial-extensionmercurial-convert

How to extend another extension's command in Hg?


I want to add additional option to the hg convert command, which is provided by internal extension hgext.convert.

I try to do the following in my __init__.py:

def extsetup(ui):
    entry = extensions.wrapcommand(commands.table, 'convert', convert_cmd)
    entry[1].append(('', 'test', None, _("test option")))

def convert_cmd(...): ...

But after enabling my extension and running proper hg convert --test, I get the following error:

hg: unknown command 'convert'
(did you mean one of clone, recover, revert?)

If I print the keys of commands.table, I can see that there are no custom commands inside.

How can I get and extend custom command?


Solution

  • According to "Writing Mercurial Extensions":

    After extsetup, the cmdtable is copied into the global command table in Mercurial.

    So I need to modify the command inside the extension's cmdtable itself. It's a little bit hacky, but works for now:

    from hgext import convert
    
    def extsetup(ui):
        entry = extensions.wrapcommand(convert.cmdtable, 'convert', convert_cmd)
        entry[1].append(('', 'test', None, _("test option")))