Question

I have Mercurial hgweb set up on Windows 2008 64 bit and IIS. The location of the repositories is a network share.

I want to create a hook on the repository to issue an "hg update" command on a changeroup. I cannot use the external hook as this would start cmd.exe with the network share as the working directory (and cmd.exe does not support network shares).

Therefore, I'm looking to find an example of a python hook that calls a mercurial command. I notice that there is a mercurial.commands module, but I cannot find any examples on the webs and I'm not very experienced with Python.

Are there any examples to call a mercurial command using a Python hook - and is it possible to do this all in the hgrc, or do I need an external .py file?

Was it helpful?

Solution 2

Inspired by Martin's answer, I thought I would attempt to write some Python, and here is how I managed to get it working. I am using Mercurial 2.0.2 and the mercurial.commands module (which, AFAIK, is included in the Mercurial Python package).

I created a myhook.py file on the server:

import mercurial.commands

def update(ui, repo, **kwargs):
    mercurial.commands.update(ui, repo)

Then, in my .hg/hgrc file on the server, I added the following:

[hooks]
changegroup = python:C:\path\to\my\myhook.py:update

I would change the line where the command is executed to specifically update to the 'tip'. If you use named branches then as it is above the command will have no effect. I believe this would be better: commands.update(ui, repo, repo['tip'])

OTHER TIPS

You need an external .py file for a Python extension. To use the internal API as if Mercurial was invoked from the command line, then use

 from mercurial.dispatch import dispatch, request
 dispatch(request(['update']))

This is the syntax after Mercurial 1.9. In earlier versions you would use

 from mercurial.dispatch import dispatch
 dispatch(['update'])

The list you pass to request or dispatch is the arguments following hg on the command line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top