Page MenuHomePhabricator
Paste P2048

Mercurial - Client-side enforce commit messages start with a Task #
ActivePublic

Authored by cspeckmim on May 5 2017, 2:36 PM.
import re
def enforce_commitmsg(ui, repo, hooktype, node=None, source=None, parent1=None, parent2=None, *args, **kwargs):
cmt = repo[node]
msg = cmt.description()
lower_msg = msg.lower()
if len(cmt.parents()) > 1 or lower_msg.startswith('backed out ') or lower_msg.startswith('back out') or lower_msg.startswith('backout'):
print "Merge or Backout - not enforcing commit message"
return None
task_re = re.compile("^(Task-|Task - |Task -|Task #|Task |Task # |#)([0-9]{1,5}).+?\s*", re.IGNORECASE)
if task_re.match(msg):
print "Legit-type commit for #%s" % task_re.match(msg).group(2)
return None
print
print
print "Your commit message '%s' does not have proper Task ID."
print "Please try again with a legit message."
print
print
return True

Event Timeline

NOTE: Installing this on a developer workstation means modifying a repository's .hg/hgrc file
[hooks]
pretxncommit.commitmsg=python:~/commit_enforcer.py:enforce_commitmsg

Also this should probably be using ui.status()/ui.warn() instead of print.