如何在Tortoise Hg日志窗口中显示钩子输出?

Vic*_*din 5 mercurial hook tortoisehg

我需要用于mercurial的简单钩子,它使用模式检查提交注释.这是我的钩子:

#!/usr/bin/env python
#
# save as .hg/check_whitespace.py and make executable

import re

def check_comment(comment):
    #
    print 'Checking comment...'
    pattern = '^((Issue \d+:)|(No Issue:)).+'
    if re.match(pattern, comment, flags=re.IGNORECASE):
        return 1
    else:
        print >> sys.stderr, 'Comment does not match pattern. You must start it with "Issue 12323:" or "No Issue:"'
        return 0

if __name__ == '__main__':
    import os, sys
    comment=os.popen('hg tip --template "{desc}"').read()
    if not check_comment(comment):
        sys.exit(1)
sys.exit(0)
Run Code Online (Sandbox Code Playgroud)

有用.'Comment does not match pattern. You must start it with "Issue 12323:" or "No Issue:"'当我从控制台提交时,它甚至会显示错误消息.但是当我尝试从Tortoise Hg Workbench提交时,只显示系统消息:abort: pretxncommit.check_comment hook exited with status 1.

我需要通知用户有什么问题.有没有办法强迫Tortoise Hg显示钩子的输出?

Joe*_*ant 5

我通过使它成为进程中的挂钩而不是外部挂钩来实现它.但是,进程中的钩子的定义完全不同.

首先,python文件只需要一个函数,该函数将由钩子定义中的名称调用.钩子函数传递ui,repohooktype对象.它还根据钩子的类型传递了其他对象.因为pretrxncommit,它被传递node,parent1parent2,但你只对节点感兴趣,所以其余的都被收集了kwargs.该ui对象用于提供状态和错误消息.

内容check_comment.py:

#!/usr/bin/env python

import re

def check_comment(ui, repo, hooktype, node=None, **kwargs):
    ui.status('Checking comment...\n')
    comment = repo[node].description()
    pattern = '^((Issue \d+:)|(No Issue:)).+'
    if not re.match(pattern, comment, flags=re.IGNORECASE):
        ui.warn('Comment does not match pattern. You must start it with "Issue 12323:" or "No Issue:"\n')
        return True
Run Code Online (Sandbox Code Playgroud)

在中hgrc,钩子将被定义为python:/path/to/file.py:function_name,如下所示:

[hooks]
pretxncommit.check_comment = python:/path/to/check_comment.py:check_comment
Run Code Online (Sandbox Code Playgroud)

.suffix_namepretxncommit是为了避免覆盖任何全局定义挂钩,尤其是如果这是在资源库中的定义hgrc,而不是全球性的.后缀是如何允许对同一个钩子的多个响应.