Last active
December 14, 2015 05:49
-
-
Save iamsilvio/5038318 to your computer and use it in GitHub Desktop.
Mercurial commit message hook
B: fixed Changegroup handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def checkMessage(message): | |
""" | |
Checks if the message content matches one of the message rules | |
B: <message> | |
R: <message> | |
F: <message> | |
S: <message> | |
Merge | |
Added tag v\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} for changeset \.{12} | |
""" | |
message_pattern = re.compile( | |
"^((B|F|S|R)(:\s)(.)*)|" | |
"^(Merge)$|" | |
"^(Added tag v\d{1,3}\.\d{1,3}\.\d{1,5}\.\d{1,5} for changeset .{12})$|" | |
"^(Removed tag v\d{1,3}\.\d{1,3}\.\d{1,5}\.\d{1,5})$", | |
re.IGNORECASE) | |
if message_pattern.match(message): | |
return False | |
return True | |
def get_revisions(repo, node): | |
node_rev = repo[node].rev() | |
tip_rev = repo['tip'].rev() | |
return range(tip_rev, node_rev - 1, -1) | |
def printUsage(ui, msg): | |
""" | |
Print all allowed message formats | |
Added tag v1.4.3.6 for changeset 91ed85071cd4 | |
Removed tag v1.4.3.6 | |
""" | |
valid_messages = {'B: <message>': 'Bug', 'F: <message>': 'Feature', | |
'R: <message>': 'Refactoring', | |
'S: <message>': 'Specification'} | |
out = 'Your commit message :: %s :: is not valid \n' \ | |
'the following message formats are allowed\n\n' % msg | |
out += '{0:20} {1:20}\n'.format('Message Format', 'Description') | |
for msg, desc in valid_messages.items(): | |
out += '{0:20} = {1:20}\n'.format(msg, desc) | |
out += '{0:20} = {1:20}\n'.format('Merge', 'Merge') | |
ui.warn(out + '\n') | |
return | |
def checkCommitMessage(ui, repo, hooktype, node=None, **kwargs): | |
""" | |
Checks a single commit message for adherence to commit message rules. | |
To use add the following to your project .hg/hgrc for each | |
project you want to check, or to your user hgrc to apply to all projects. | |
[hooks] | |
#Unix | |
pretxncommit = python:path/to/script/enforce-message.py:checkCommitMessage | |
#windows | |
pretxncommit = python:D:\\path\\enforce-message.py:checkCommitMessage | |
""" | |
if hooktype != 'pretxncommit': | |
ui.warn('hook type %s not supported' % hooktype) | |
return True | |
rev = repo['tip'] | |
message = rev.description() | |
if checkMessage(message): | |
printUsage(ui, message) | |
return True | |
else: | |
return False | |
def checkAllCommitMessage(ui, repo, hooktype, node=None, **kwargs): | |
""" | |
Checks all inbound changeset messages from a push for adherence to | |
the commit message rules. | |
[hooks] | |
pretxnchangegroup = python:path/enforce-message.py:checkAllCommitMessage | |
""" | |
hook_types = ['pretxnchangegroup'] | |
if not hooktype in hook_types: | |
ui.warn('hook type %s not supported\nby this script\n' % hooktype) | |
return True | |
for revision in get_revisions(repo, node): | |
rev = repo[revision] | |
message = rev.description() | |
if checkMessage(message): | |
ui.warn("Revision " + str(revision) + " commit message:[" + message + | |
"] does not adhere to commit message rules\n") | |
printUsage(ui, message) | |
return True | |
else: | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment