2011-07-06 64 views
5

我需要使用模式来检查提交注释的mercurial的简单钩子。这是我的钩子:如何在Tortoise Hg日志窗口中显示钩子输出?

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

我需要通知用户出了什么问题。有什么办法可以迫使乌龟显示钩子的输出吗?

+0

只是一个猜测,但你有没有尝试写入sys.out而不是sys.err? – bbaja42

+0

是的。它没有帮助。 –

回答

5

我通过使它成为进程中钩子而不是外部钩子来实现它的工作。然而,正在处理的hooks定义完全不同。

首先,python文件只需要一个函数,该函数将在钩子定义中由名称调用。挂钩功能通过ui,repohooktype对象。它还根据钩子的类型传递其他对象。对于pretrxncommit,它通过nodeparent1parent2,但是你只对节点感兴趣,所以其余的收集在kwargsui对象用于给出状态和错误消息。

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_namepretxncommit是避免覆盖任何全局定义钩,特别是如果这是在存储库的hgrc而不是全局定义的。后缀是如何允许多个响应到同一个钩子。

0

如果挂钩在通过例如服务器提供的存储库上运行, hgserve:
我用这个小Python函数在pretxnchangegroup脚本来显示在服务器日志

  • 在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 
      
    相关问题