How to set up mercurial hooks in Kallithea

812 Views Asked by At

I've been at it for a while now and I can't seem to get it working.

As per Kallithea documentation:

To add another custom hook simply fill in the first textbox with <name>.<hook_type> and the second with the hook path. Example hooks can be found in kallithea.lib.hooks.

So my first attempt was to add a new method to hooks.py. Basically to test out the hook I want to prevent ALL push to the repo. So I'll use pretxnchangegroup and return non 0 non false value as Mercurial documentation states

A hook that executes successfully must exit with a status of zero if external, or return boolean “false” if in-process. Failure is indicated with a non-zero exit status from an external hook, or an in-process hook returning boolean “true”. If an in-process hook raises an exception, the hook is considered to have failed.

So I did this:

def myhook(ui, repo, **kwargs): return True

And I added the hook to the GUI in Kallithea hook options:

pretxnchangegroup <=> python:kallithea.lib.hooks.myhook

This however failed because for some reason the method can't be found

abort: pretxnchangegroup hook is invalid ("kallithea.lib.hooks.myhook" is not defined)

So I tried putting it in another file ( in the same 'lib' folder where hooks.py is ). Created a file called canpush.py and added the same method there. I changed the hook path to target the new file name:

pretxnchangegroup <=> python:kallithea.lib.hooks.myhook

However the hook does not trigger, and I can push to my repo without a problem. I plan to change the actual hook implementation in the future, push will be allowed, but first I need to get any hook functional with Kallithea.

What am I doing wrong here ?

Also, if someone knows how to use hgrc settings from individual repo within Kallithea an example would be great. Original question here.

2

There are 2 best solutions below

0
On

Answering my own question, but just to keep it as reference.

As it turned out the setup was fine, but in an act of desperation I decided to restart kallithea daemon ( which was nowhere in the documentation ), basically thinking 'what could go wrong' - and that did the trick!

I guess during the startup process things get compiled / cached and the hook definition methods are found and functional ( If someone has a better explanation as to what happens on kallithea restart please share it )

So bare in mind, after every change to the hooks files kallithea daemon must be restarted in order for hooks to have any effect.

sudo service kallithea restart

0
On

Something else that wasn't clear to me from reading the kallithea documentation is that the hooks are mercurial hooks, they're not really some kallithea/rhodecode API, it's mercurial all the way.

This means that the best source of documentation on how to write one is something like http://hgbook.red-bean.com/read/handling-repository-events-with-hooks.html