从 python 钩子调用一个 mercurial 命令 ( "hg update" )



我在Windows 2008 64位和IIS上安装了Mercurial hgweb。存储库的位置是一个网络共享。

我想在存储库上创建一个钩子,以便在changegroup上发出"hgupdate"命令。我无法使用外部挂钩,因为这将以网络共享作为工作目录启动cmd.exe(并且cmd.exe不支持网络共享)。

因此,我想找到一个python钩子的例子,它调用一个mercurial命令。我注意到有一个mercurial.commands模块,但我在网上找不到任何例子,而且我对Python没有太多经验。

有没有使用Python钩子调用mercurial命令的例子?有没有可能在hgrc中完成这一切,或者我需要一个外部.py文件?

您需要一个外部.py文件作为Python扩展名。要使用内部API,就好像从命令行调用了Mercurial一样,请使用

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

这是Mercurial 1.9之后的语法。在早期版本中,您将使用

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

传递给requestdispatch的列表是命令行上hg后面的参数。

受Martin回答的启发,我想我应该尝试编写一些Python,下面是我如何使其工作的。我使用的是Mercurial 2.0.2和Mercurial.commands模块(AFAIK包含在Mercurial Python包中)。

我在服务器上创建了一个myhook.py文件:

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

然后,在服务器上的.hg/hgrc文件中,我添加了以下内容:

[hooks]
changegroup = python:C:pathtomymyhook.py:update

我会更改执行命令的行,以专门更新为"提示"。如果使用命名分支,则由于它在上面,该命令将不起作用。我相信这样会更好:commands.update(ui,repo,repo['tip'])

最新更新