Call mercury command ("hg update") from python

I have installed Mercurial hgweb on a 64-bit version of Windows 2008 and IIS. The location of the repositories is a network resource.

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

Therefore, I am looking to find an example python hook that calls the mercurial command. I noticed that there is a module mercurial.commands, but I can not find examples on the Internet, and I'm not very experienced with Python.

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

+4
source share
2 answers

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

I created the 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 in which the command is executed to specifically update the "prompt". If you use named branches, then since it is higher than the command, it will have no effect. I believe this will be better: commands.update (ui, repo, repo ['tip'])

+3
source

For the Python extension you will need an external .py file. To use the internal API as if Mercurial was called from the command line, 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 are the arguments following hg on the command line.

+3
source

Source: https://habr.com/ru/post/914008/


All Articles