6

我需要简单的 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)

有用。'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 显示钩子的输出?

4

2 回答 2

5

我通过使其成为进程内挂钩而不是外部挂钩来使其工作。然而,进程内挂钩的定义完全不同。

首先,python 文件只需要一个函数,该函数将由钩子定义中的名称调用。钩子函数被传递ui, repo, 和hooktype对象。它还根据钩子的类型传递其他对象。因为pretrxncommit, 是通过node, parent1, 和parent2, 但是你只对 node 感兴趣, 所以剩下的都聚集在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

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

[hooks]
pretxncommit.check_comment = python:/path/to/check_comment.py:check_comment

.suffix_nameonpretxncommit是为了避免覆盖任何全局定义的钩子,特别是如果它是在存储库而不是全局钩子中定义的hgrc。后缀是允许对同一个钩子进行多个响应的方式。

于 2011-07-06T17:02:44.097 回答
0

如果钩子在通过例如 hgserve 服务的存储库上运行:
我在脚本中使用这个小的 Python 函数pretxnchangegroup来显示相同​​的输出

  • 在服务器日志中
  • 在 TortoiseHg 工作台 Log pan 或 cmd 行

def log(ui, string):
    print(string) # will show up as "remote:" on the client
    ui.status("{0}\n".format(string)) # will show up in the same hg process: hgserve ; append newline
    return 
于 2014-02-27T10:35:38.123 回答