Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6098281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:08:24+00:00 2026-05-23T13:08:24+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:08:25+00:00Added an answer on May 23, 2026 at 1:08 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a simple application, preferably a cross-platform one, that enables sending of files
I've been looking for a C++ SQL library implementation that is simple to hook
I need to write a OS X login hook script that is aware of
I have this simple hook on /hooks/post-update inside a bare repository: #!/bin/sh git-update-server-info GIT_WORK_TREE=/home/user/apps/application-site/dev
I have a post commit hook that will save the svn log as an
I need a simple template engine, that only does variable names replacement (I don't
How do I create unique key value in PHP? I need simple unique keys
I need a simple way to monitor multiple text log files distributed over a
I need a simple table with a user name and password field in MySQL.
I need a simple app to edit database tables. Are there any code generators

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.