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

白昼怎懂夜的黑 提交于 2019-12-05 17:37:13

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.

Lee Atkinson

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

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