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

发布于 2024-11-18 14:14:23 字数 923 浏览 3 评论 0原文

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

它有效。它甚至显示错误消息'注释与模式不匹配。当我从控制台提交时,您必须以“Issue 12323:”或“No Issue:”开始。但是当我尝试从 Tortoise Hg Workbench 提交时,只显示系统消息:abort: pretxncommit.check_comment hook exited with status 1

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

I need simple hook for mercurial that checks commit comment using pattern. Here is my hook:

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

It works. It even shows error message 'Comment does not match pattern. You must start it with "Issue 12323:" or "No Issue:"' when I commit from console. But when I trying to commit from Tortoise Hg Workbench, only system message is shown: abort: pretxncommit.check_comment hook exited with status 1.

I need to inform user what is wrong. Is there any way to force Tortoise Hg to show output from hook?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最美不过初阳 2024-11-25 14:14:23

我通过将其设置为进程内挂钩而不是外部挂钩来使其工作。然而,进程内 hooks 的定义却截然不同。

首先,Python 文件只需要一个函数,该函数将通过钩子定义中的名称进行调用。钩子函数传递 uirepohooktype 对象。它还会根据钩子的类型传递附加对象。对于 pretrxncommit,它传递的是 nodeparent1parent2,但您只对 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

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

I got it to work by making it an in-process hook rather than an external hook. In-process hooks are defined quite differently, however.

First, the python file needs only a single function that will be called by the name in the hook definition. The hook function is passed ui, repo, and hooktype objects. It is also passed additional objects based on the type of hook. For pretrxncommit, it is passed node, parent1, and parent2, but you're only interested in node, so the rest are gathered in kwargs. The ui object is used to give the status and error messages.

Contents of 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

In the hgrc, the hook would be defined with python:/path/to/file.py:function_name, like this:

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

The .suffix_name on pretxncommit is to avoid overriding any globally defined hook, especially if this is defined in the repository's hgrc rather than the global one. Suffixes are how one allows multiple responses to the same hook.

乄_柒ぐ汐 2024-11-25 14:14:23

如果挂钩在通过 hgserve 等服务的存储库上运行:
我在 pretxnchangegroup 脚本中使用这个小型 Python 函数,

  • 服务器日志中
  • 在 TortoiseHg 工作台日志面板或 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 

In case the hook runs on a repository that is served via e.g. hgserve:
I use this small Python function in a pretxnchangegroup script to show the same output

  • in the server log
  • in the TortoiseHg workbench Log pan or the cmd line

:

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文