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

你说的曾经没有我的故事 提交于 2019-12-04 03:04:49

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:

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

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

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.

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