Mercurial Commit Hook与Python主要功能

时间:2012-03-20 22:53:33

标签: python mercurial mercurial-hook mercurial-commit

我正在尝试在python中创建一个复杂的mercurial提交钩子。我也希望允许使用OptionParser传递参数。以下是我到目前为止的要点:

.hg / hgrc config:

[hooks]
commit = python:/mydir/pythonFile.py:main
# using python:/mydir/pythonFile.py doesn't work for some reason either

pythonFile.py:

def main(ui, repo, **kwargs):
    from optparse import OptionParser

    parser = OptionParser()
    parser.add_option('--test-dir', action='store', type="string",
                  dest='test_dir', default='otherdir/',
                  help='help info')
    (options, args) = parser.parse_args()

    # do some stuff here
    someFunc(options.test_dir)

if __name__ == '__main__':
    import sys
    main(sys.argv[0], sys.argv[1], sys.argv[2:])

当我运行hg commit -m 'message'时出现错误:“用法:hg [options] hg:错误:没有这样的选项:-m”。当我尝试hg commit --test-dir '/somedir'时,我收到一个错误:“hg commit:option --test-dir not recognized”。

最后我尝试在hgrc配置中指定commit = python:/mydir/pythonFile.py:main --test-dir '/somedir',我收到此错误:“AttributeError:'module'对象没有属性'main --test-dir'/ somedir''”

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我认为您的问题可能在于尝试导入不属于使用mercurial打包的python的内容。 如果你需要的是将附加信息传递给钩子,以便你可以为不同的repos / branches等配置它,你可以使用

param_value= ui.config('ini_section', 'param_key', default='', untrusted=False)

其中ini_section是mercurial.ini / .hgrc文件中[]的位,param_key是条目的名称 像

这样的东西
[my_hook_params]
test-dir=/somedir

然后使用

test_dir = ui.config('my_hook_params', 'test-dir', default='otherdir/', untrusted=False)
相关问题