| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Copyright 2014 Olivia Mackall <[email protected]> |
| 4 |
# |
| 5 |
# A tool/hook to run basic sanity checks on commits/patches for |
| 6 |
# submission to Mercurial. Install by adding the following to your |
| 7 |
# .hg/hgrc: |
| 8 |
# |
| 9 |
# [hooks] |
| 10 |
# pretxncommit = contrib/check-commit |
| 11 |
# |
| 12 |
# The hook can be temporarily bypassed with: |
| 13 |
# |
| 14 |
# $ BYPASS= hg commit |
| 15 |
# |
| 16 |
# See also: https://mercurial-scm.org/wiki/ContributingChanges |
| 17 |
|
| 18 |
|
| 19 |
import os |
| 20 |
import re |
| 21 |
import sys |
| 22 |
|
| 23 |
commitheader = r"^(?:# [^\n]*\n)*" |
| 24 |
afterheader = commitheader + r"(?!#)" |
| 25 |
beforepatch = afterheader + r"(?!\n(?!@@))" |
| 26 |
|
| 27 |
errors = [ |
| 28 |
(beforepatch + r".*[(]bc[)]", "(BC) needs to be uppercase"), |
| 29 |
( |
| 30 |
beforepatch + r".*[(]issue \d\d\d", |
| 31 |
"no space allowed between issue and number", |
| 32 |
), |
| 33 |
(beforepatch + r".*[(]bug(\d|\s)", "use (issueDDDD) instead of bug"), |
| 34 |
(commitheader + r"# User [^@\n]+\n", "username is not an email address"), |
| 35 |
( |
| 36 |
commitheader + r"(?!merge with )[^#]\S+[^:] ", |
| 37 |
"summary line doesn't start with 'topic: '", |
| 38 |
), |
| 39 |
(afterheader + r"[A-Z][a-z]\S+", "don't capitalize summary lines"), |
| 40 |
(afterheader + r"^\S+: *[A-Z][a-z]\S+", "don't capitalize summary lines"), |
| 41 |
( |
| 42 |
afterheader + r"\S*[^A-Za-z0-9-_]\S*: ", |
| 43 |
"summary keyword should be most user-relevant one-word command or topic", |
| 44 |
), |
| 45 |
(afterheader + r".*\.\s*\n", "don't add trailing period on summary line"), |
| 46 |
(afterheader + r".{79,}", "summary line too long (limit is 78)"), |
| 47 |
] |
| 48 |
|
| 49 |
word = re.compile(r'\S') |
| 50 |
|
| 51 |
|
| 52 |
def nonempty(first, second): |
| 53 |
if word.search(first): |
| 54 |
return first |
| 55 |
return second |
| 56 |
|
| 57 |
|
| 58 |
def checkcommit(commit, node=None): |
| 59 |
exitcode = 0 |
| 60 |
printed = node is None |
| 61 |
hits = [] |
| 62 |
signtag = ( |
| 63 |
afterheader + r'Added (tag [^ ]+|signature) for changeset [a-f0-9]{12}' |
| 64 |
) |
| 65 |
if re.search(signtag, commit): |
| 66 |
return 0 |
| 67 |
for exp, msg in errors: |
| 68 |
for m in re.finditer(exp, commit): |
| 69 |
end = m.end() |
| 70 |
trailing = re.search(r'(\\n)+$', exp) |
| 71 |
if trailing: |
| 72 |
end -= len(trailing.group()) / 2 |
| 73 |
hits.append((end, exp, msg)) |
| 74 |
if hits: |
| 75 |
hits.sort() |
| 76 |
pos = 0 |
| 77 |
last = '' |
| 78 |
for n, l in enumerate(commit.splitlines(True)): |
| 79 |
pos += len(l) |
| 80 |
while len(hits): |
| 81 |
end, exp, msg = hits[0] |
| 82 |
if pos < end: |
| 83 |
break |
| 84 |
if not printed: |
| 85 |
printed = True |
| 86 |
print("node: %s" % node) |
| 87 |
print("%d: %s" % (n, msg)) |
| 88 |
print(" %s" % nonempty(l, last)[:-1]) |
| 89 |
if "BYPASS" not in os.environ: |
| 90 |
exitcode = 1 |
| 91 |
del hits[0] |
| 92 |
last = nonempty(l, last) |
| 93 |
|
| 94 |
return exitcode |
| 95 |
|
| 96 |
|
| 97 |
def readcommit(node): |
| 98 |
return os.popen("hg export %s" % node).read() |
| 99 |
|
| 100 |
|
| 101 |
if __name__ == "__main__": |
| 102 |
exitcode = 0 |
| 103 |
node = os.environ.get("HG_NODE") |
| 104 |
|
| 105 |
if node: |
| 106 |
commit = readcommit(node) |
| 107 |
exitcode = checkcommit(commit) |
| 108 |
elif sys.argv[1:]: |
| 109 |
for node in sys.argv[1:]: |
| 110 |
exitcode |= checkcommit(readcommit(node), node) |
| 111 |
else: |
| 112 |
commit = sys.stdin.read() |
| 113 |
exitcode = checkcommit(commit) |
| 114 |
sys.exit(exitcode) |