Call a mercurial command ("hg update") from a python hook
Asked Answered
V

2

4

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?

Vickery answered 23/1, 2012 at 13:31 Comment(0)
V
3

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'])

Vickery answered 23/1, 2012 at 17:10 Comment(0)
P
3

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.

Palanquin answered 23/1, 2012 at 13:39 Comment(2)
Thanks Martin - I have discovered that there is a mercurial.commands module that I can also use - and I managed to get it working - see my answer below.Vickery
@LeeAtkinson: That's also an elegant solution!Palanquin
V
3

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'])

Vickery answered 23/1, 2012 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.