如何在 Tortoise Hg 日志窗口中显示钩子输出?
我需要一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过将其设置为进程内挂钩而不是外部挂钩来使其工作。然而,进程内 hooks 的定义却截然不同。
首先,Python 文件只需要一个函数,该函数将通过钩子定义中的名称进行调用。钩子函数传递
ui
、repo
和hooktype
对象。它还会根据钩子的类型传递附加对象。对于pretrxncommit
,它传递的是node
、parent1
和parent2
,但您只对 Node 感兴趣,所以其余的都集中在kwargs
中。ui
对象用于提供状态和错误消息。check_comment.py
的内容:在
hgrc
中,钩子将使用python:/path/to/file.py:function_name
定义,像这样: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
, andhooktype
objects. It is also passed additional objects based on the type of hook. Forpretrxncommit
, it is passednode
,parent1
, andparent2
, but you're only interested in node, so the rest are gathered inkwargs
. Theui
object is used to give the status and error messages.Contents of
check_comment.py
:In the
hgrc
, the hook would be defined withpython:/path/to/file.py:function_name
, like this:The
.suffix_name
onpretxncommit
is to avoid overriding any globally defined hook, especially if this is defined in the repository'shgrc
rather than the global one. Suffixes are how one allows multiple responses to the same hook.如果挂钩在通过 hgserve 等服务的存储库上运行:
我在
pretxnchangegroup
脚本中使用这个小型 Python 函数,显示相同的输出:
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: