Mercurial Pre-Commit挂钩:如何挂钩到当前目录中的python程序? [英] Mercurial Pre-Commit Hook: How to hook to python program in current directory?

查看:85
本文介绍了Mercurial Pre-Commit挂钩:如何挂钩到当前目录中的python程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Mercurial挂钩,该挂钩在将提交推送到主存储库时运行.我创建了如下所示的python脚本:

I'm trying to create a Mercurial hook that runs when the commits are being pushed to the main repository. I created a python script as given below:


# 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)

对于每个分支,此commit.py被复制,因为它包含在将代码压入主存储库之前需要运行的机制.仅当这些预推机制通过后,推才能成功.用户可以修改其本地commit.py,以便仅根据其所从事的项目来运行这些预推操作的子集,并且每个人一次可以从事多个项目.因此,commit.py不能是可以驻留在.hg文件夹中的全局python脚本.

For each branch, this commit.py is duplicated as it contains mechanisms that needs to run before code gets pushed onto the main respository. 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. And so, commit.py cannot be a global python script that can reside in .hg folder.

要使mercurial运行本地commit.py,在我的mercurial.ini文件(位于C:\ Users \ UserName \ mercurial.ini中)中,添加了以下语句:

To make mercurial run the local commit.py, in my mercurial.ini file (in C:\Users\UserName\mercurial.ini), I added the following statement:


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

如果我将python脚本放在.hg文件夹中,则会运行python脚本,但执行此操作时不会运行.谁能帮助我阐明这个问题?非常感谢.

The python script runs if I place it inside .hg folder, but not when I do this. Can anyone help me shed light on this issue? Many thanks.

推荐答案

我通过IRC for Mercurial获得了此解决方案.如我的评论之一所述,该挂钩的脚本应指定为绝对路径,或者应在PYTHONPATH中使用python模块.因此,pmezard在IRC上建议我应该有一个调用本地commit.py的固定脚本.可以如下所示完成该操作:

I got this solution over IRC for Mercurial. 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. Hence, I was suggested by pmezard over IRC that I should have a fixed script that invokes the local commit.py. This can be done as shown below:

mercurial.ini中,钩接到位于用户家的.hg目录中的全局" python脚本,如下所示:

In mercurial.ini, hook to a 'global' python-script that resides in .hg directory of the user's home as shown below:


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

全局" python脚本commit.py看起来像这样:

The 'global' python-script, commit.py looks something like this:


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

然后,存储库目录中的localcommit.pyglobal提交脚本运行,因此每个存储库都可以维护自己的自定义提交脚本.

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.

这篇关于Mercurial Pre-Commit挂钩:如何挂钩到当前目录中的python程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆