views:

114

answers:

2

've written two functions in a file commit_hooks.py that I want to run before any commit is made persistent, but I can't figure out how to write my hgrc to detect them.

The function headers are:

def precommit_bad_merge(ui, repo, parent1=None, parent2=None, **kwargs):
...

def precommit_bad_branching(ui, repo, **kwargs):
...

I've tried using this "guide", but the documentation is too "man pagey" for me. The following is an outcast which doesn't work.

[hooks]
precommit = ..\..\mno2\commit_hooks.py

Update! Rewriting the hook line to:

precommit = D:\environments\next\mno2\commit_hooks.py

make Mercurial detect the precommit hook, but it always exits with status 1 for some reason.

A: 

The "man pagey" documentation has a section on python hook syntax:

The syntax for Python hooks is as follows:

hookname = python:modulename.submodule.callable
hookname = python:/path/to/python/module.py:callable

Python hooks are run within the Mercurial process. Each hook is called with at least three keyword arguments: a ui object (keyword ui), a repository object (keyword repo), and a hooktype keyword that tells what kind of hook is used. Arguments listed as environment variables above are passed as keyword arguments, with no HG_ prefix, and names in lower case.

If a Python hook returns a "true" value or raises an exception, this is treated as a failure.

Geoffrey Zheng
+2  A: 

Set up your [hooks] section like this:

[hooks]
precommit.foo = python:D:\environments\next\mno2\commit_hooks.py:precommit_bad_merge
precommit.bar = python:D:\environments\next\mno2\commit_hooks.py:precommit_bad_branching

The syntax for the precommit line that you used is for external hooks, so it was treating your python file as a self-contained script (which I'm assuming it's not since you're using the function signatures for in-process hooks).

You may need to have the python executable in your path (I do).

For more information, see the definitive guide's section on in-process hooks; there's some useful information hidden in the comments.

Niall C.
Thank you that worked great! :-) It doesn't work in Eclipse though MercurialEclipse plugin (I just get access denied), but that's another problem, probably due to Eclipse workspace being on my local computer and the source code is on a remote server.
MdaG