Mercurial trac commit hook

Having searched a lot around google, it does not seem that anyone has published their trac commit hooks for mercurial. Since I had to use just that, I’ve cooked up a little hook which is based on the hook from the timingandestimationplugin. I’ve created it as a changegroup hook, and it’s probably filled with bugs, but it seems to work and it catches the fixes #42 and such.

To use the hook you must place it somewhere inside your PYTHONPATH and tell mercurial to use it (I placed it in a module called trachook — don’t call your module trac):

[hooks]
changegroup = python:trachook.hook

And tell the hook where to find your trac installation:

[trac-hook]
root = /path/to/trac
url = http://url/to/trac

Grab your own copy of the source, and happy coding. And a big thank you to Jesper Nøhr.

Also, if you need a place to host your mercurial repository but don’t wan’t to set it up yourself, check out bitbucket.

12 comments.

  1. The builtin trac-post-commit-hook worked just fine for me.

    (Don’t have version numbers offhand, I think this was 0.11 with the TracMercurial plugin…)

  2. That may be, but I couldn’t really find any documentation on it or anyone writing about it.

    This should also be quite a bit faster since it runs on changegroup, and what you are suggesting would be running as the incoming hook(?).

  3. The hook works fine, but it isn’t case independent on commands. so ‘Fixes’ or ‘See’ doesn’t work.

    Easy to add by appending .lower() on the call:

    cmd_groups = command_re.findall(self.msg.lower())
  4. That’s something that the orginal hook does as well. I’ll update my version later tonight, as it’s a very annoying “feature”.

    Thanks.

  5. There, updated at bitbucket. I’ve added an re.I to the match, which should fix it.

  6. I had to move to hg-ssh in cygwin to make things work, so then I got:

    TypeError: ‘_demandmod’ object is not iterable

    I just disabled demandimport at the top of trachook.py, and this made things work again:

    from mercurial import demandimport demandimport.disable()

    One could probably be more selective in the disabling, but this seem to work for now.

  7. Another fix. Starting at v1.1, Mercurial’s changelog do not have a count() function anymore. Rather, you need to do use the len() built-in function.

    I’ve patched my version for this. Some logic is probably needed to make it work for different versions of Hg.

  8. I’ve pushed a new version of the hook now – just changed the count() to len(). Let me know if it should break something (it shouldn’t).

    Thanks Marcus!

  9. For others who end up on this page, I’ve cloned Mads repo (as he knows) and changed some bits. It’s now available here and I’ll try to maintain it until the Trac-devs integrate a hook into trac releases.

    http://bitbucket.org/marcusl/mercurial-trac-hook/

    Comnments welcome. :)

  10. I’ve givin you write acess to the repository, should you want to maintain it (better than moving it imo) :)

  11. I kept getting import errors on trachook, eve though i could import successfully into a python shell. I think it had something to do with my PYTHONPATH not being the same as the webserver user’s one.

    I fixed the import problem by using a more explicit syntax: In ~/.hgrc:

    [extensions] trachook = /full/path/to/trachook.py

    then in your repository-specific hgrc, follow the syntax exactly as shown in the original entry.

  12. Hmm, just copying the trachook.py to the place gave me an permission error, that normal users which checkin doesn’t have the permission to change /srv/trac/myProject files. How did you solved this??

Post a comment.