pythonmercurialhookmercurial-hook

ignore certain mercurial commands in mercurial hook


I have a mercurial hook like so:

[hooks]
pretxncommit.myhook = python:path/to/file:myhook

with the code looking like this:

def myhook(ui, repo, **kwargs):
    #do some stuff

but this hook runs on commands that use the commit logic to do something else, in my case hg shelve. is there a way to get the command that the user has input to avoid running the hook on that command?

perhaps something like this:

def myhook(ui, repo, command, **kwargs):
      if command is "hg shelve"
           return 0
      #do some stuff

Solution

  • Unfortunately the answer seems to be no. I just debugged into the hook mechanism of hg 3.1, and the information about which command was issued is not propagated into the hook function. The only way I can think of is to hack something ugly with the debugger api to extract informations from the call stack.

    Another hack would be to inspect sys.argv, but I fear that this is also very unreliable (as it can't detect if something was executed via the Command Server).

    BTW I used this snippet to attach a debugger:

    def myhook(ui, repo, **kwargs):
        print kwargs
        from pdb import set_trace
        set_trace()