sapling/hgext3rd/checkmessagehook.py
Stanislau Hlebik 47e9e5588c hgext3rd: add checkcommitmessage
Summary:
Let's add a script that can be used as a commit hook to prevent bad commit
messages from happenning.

Test Plan:
Run the following perl script:
  system("echo 1 >> 1");
  system("hg add 1");
  system("hg ci -m"."\x80");
  system("hg ci -m"."\x01");
  system("hg ci -m ok");

Output:
  fbcode/1 already tracked!
  transaction abort!
  rollback completed
  abort: decoding near '': 'utf8' codec can't decode byte 0x80 in position 0: invalid start byte!
  non-printable characters in commit message
  transaction abort!
  rollback completed
  abort: pretxncommit hook failed

Make sure that commit is made with "ok" message, but there are no commits with
bad commit messages

Reviewers: #sourcecontrol, tja

Reviewed By: tja

Subscribers: tja, mjpieters

Differential Revision: https://phabricator.intern.facebook.com/D4597432

Tasks: 16212973

Signature: t1:4597432:1487761552:cf38eabf93374b0ec2feb653dd70033de25e6e0e
2017-02-22 04:50:24 -08:00

30 lines
875 B
Python

import string
from mercurial.i18n import _
def checkcommitmessage(ui, repo, **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]
pretxncommit = python:path/to/script/checkmessagehook.py:checkcommitmessage
"""
hg_commit_message = repo['tip'].description()
try:
hg_commit_message.decode('utf8')
except UnicodeDecodeError:
ui.warn(_('commit message is not utf-8\n'))
return True
printable = set(string.printable)
for c in hg_commit_message:
if ord(c) < 128 and c not in printable:
ui.warn(_('non-printable characters in commit message\n'))
return True
# False means success
return False