简体   繁体   English

Mercurial Pre-Commit Hook:如何挂钩当前目录中的python程序?

[英]Mercurial Pre-Commit Hook: How to hook to python program in current directory?

I'm trying to create a Mercurial hook that runs when the commits are being pushed to the main repository. 我正在尝试创建一个Mercurial钩子,当提交被推送到主存储库时运行。 I created a python script as given below: 我创建了一个python脚本,如下所示:

# commit.py

from mercurial import ui, hg
from mercurial.i18n import gettext as _

def getV1ID(ui, repo, **kwargs):
    ui.write("The hook works!!!")
    v1id = ui.prompt('Enter the VersionOne ID')
    ui.write('VersionOne ID: '+v1id)

For each branch, this commit.py is duplicated as it contains mechanisms that needs to run before code gets pushed onto the main respository. 对于每个分支,此commit.py是重复的,因为它包含在将代码推送到主存储库之前需要运行的机制。 The push should only be successful if those pre-push mechanisms pass. 只有那些预推机制通过才能推动推动。 Users can modify their local commit.py so that they only run a subset of those pre-push operations depending on the project their working on and each person could be working on more than one project at a time. 用户可以修改他们的本地commit.py,以便他们只运行这些预推送操作的子集,具体取决于他们所处理的项目,并且每个人可以一次处理多个项目。 And so, commit.py cannot be a global python script that can reside in .hg folder. 因此, commit.py不能是可以驻留在.hg文件夹中的全局python脚本。

To make mercurial run the local commit.py , in my mercurial.ini file (in C:\\Users\\UserName\\mercurial.ini), I added the following statement: 为了使mercurial运行本地commit.py ,在我的mercurial.ini文件中(在C:\\ Users \\ UserName \\ mercurial.ini中),我添加了以下语句:

[hooks]
prechangegroup = python:./commit.py:getV1ID

The python script runs if I place it inside .hg folder, but not when I do this. 如果我将它放在.hg文件夹中,python脚本会运行,但是当我这样做时它不会运行。 Can anyone help me shed light on this issue? 任何人都可以帮助我阐明这个问题吗? Many thanks. 非常感谢。

I got this solution over IRC for Mercurial. 我在Mercurial的IRC上得到了这个解决方案。 As stated in one of my comments, the script for the hook should be specified as an absolute path or it should a python module in PYTHONPATH. 正如我的一篇评论中所述,钩子的脚本应该被指定为绝对路径,或者应该是PYTHONPATH中的python模块。 Hence, I was suggested by pmezard over IRC that I should have a fixed script that invokes the local commit.py . 因此, pmezard对IRC建议我应该有一个调用本地commit.py的固定脚本。 This can be done as shown below: 这可以完成如下所示:

In mercurial.ini , hook to a 'global' python-script that resides in .hg directory of the user's home as shown below: mercurial.ini ,挂钩一个位于用户家的.hg目录中的“全局”python脚本,如下所示:

[hooks]
preoutgoing = python:%USERPROFILE%\.hg\commit.py:run

The 'global' python-script, commit.py looks something like this: ' commit.py脚本, commit.py看起来像这样:

from mercurial import ui, hg
import os

class Chdir:
    def __init__(self, newPath):
        self.savedPath = os.getcwd()
        os.chdir(newPath)

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):
        os.chdir(self.savedPath)

def run(ui, repo, **kwargs):
    if kwargs['source'] == 'push':
        with Chdir(repo.root) as dirchanged:
            import localcommit
            sys.exit(localcommit.main(ui, repo, **kwargs))

The localcommit.py in the repository's directory then gets run by the global commit-script and thus every repository can maintain its own customized commit-script. 然后,存储库目录中的localcommit.pyglobal提交脚本运行,因此每个存储库都可以维护自己的自定义提交脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM