Call a mercurial command (“hg update”) from a python hook

≯℡__Kan透↙ 提交于 2019-12-07 11:23:58

问题


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?


回答1:


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.




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



来源:https://stackoverflow.com/questions/8972409/call-a-mercurial-command-hg-update-from-a-python-hook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!