How to show hook output in Tortoise Hg log window?

I need a simple hook for mercurial that checks the commit of a comment using a template. 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 an error message 'Comment does not match pattern. You must start it with "Issue 12323:" or "No Issue:"'when I take it from the console. But when I try to make a transaction with Tortoise Hg Workbench, only displayed system message abort: pretxncommit.check_comment hook exited with status 1.

I need to tell the user what is wrong. Is there a way to get Tortoise Hg to display output with a hook?

+5
source share
2 answers

, in-process hook, . hooks -.

-, python , . hook 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_name pretxncommit , , hgrc, . - , .

+5

, , , , hgserve:
Python pretxnchangegroup script

  • 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 
0

All Articles