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ファイルには、フック定義の名前で呼び出される1つの関数のみが必要です。フック関数は、、、およびオブジェクトに渡さuirepoますhooktype。また、フックのタイプに基づいて追加のオブジェクトが渡されます。の場合、、、、およびpretrxncommitが渡されますが、関心があるのはノードのみであるため、残りはに集められます。オブジェクトは、ステータスメッセージとエラーメッセージを提供するために使用されます。nodeparent1parent2kwargsui

内容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_name。接尾辞は、同じフックに対して複数の応答を許可する方法です。pretxncommithgrc

于 2011-07-06T17:02:44.097 に答える