import re
import subprocess
import sys
SUBJECT_LIMIT = 72
TYPE_RX = re.compile(
r"^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)"
r"(\(.+\))?(!)?: .+"
)
MERGE_RX = re.compile(r"^Merge(?:\s|\b)")
REVERT_RX = re.compile(r'^Revert "')
CJK_RX = re.compile(r"[\u4e00-\u9fff\u3400-\u4dbf\uf900-\ufaff]")
BANNED = [
(r"\bB-\d+\b", "internal board id (letter-number form)"),
]
WARNINGS = [
("subject ends with a period", lambda s: s.rstrip().endswith(".")),
("subject does not start with an uppercase letter",
lambda s: s[:1].isalpha() and not s[:1].isupper()),
]
def _violations_for_message(text):
errors, warns = [], []
lines = text.splitlines()
subject = lines[0].rstrip() if lines else ""
body = "\n".join(lines[1:])
if MERGE_RX.match(subject) or REVERT_RX.match(subject):
return errors, warns
if not TYPE_RX.match(subject):
errors.append(
"subject must match '<type>(<scope>)?: <summary>' with type in "
"feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert "
"(merge/revert exempt); got: %s" % (subject[:80] or "<empty>"))
if len(subject) > SUBJECT_LIMIT:
errors.append("subject longer than %d chars (%d)" % (SUBJECT_LIMIT, len(subject)))
if CJK_RX.search(subject):
errors.append("subject contains CJK characters")
if CJK_RX.search(body):
errors.append("body/footer contains CJK characters")
for rx, label in BANNED:
if re.search(rx, text):
errors.append("banned token: %s" % label)
for label, fn in WARNINGS:
if fn(subject):
warns.append(label)
for ln in lines[1:]:
stripped = ln.lstrip()
if stripped.startswith("#"):
continue
if len(ln) > SUBJECT_LIMIT:
warns.append("body line longer than %d columns: %.40s..." % (SUBJECT_LIMIT, ln))
return errors, warns
def _format(sha, errors, warns):
out = []
if errors:
out.append("%s ERROR:" % sha)
out.extend(" " + e for e in errors)
if warns:
out.append("%s warn:" % sha)
out.extend(" " + w for w in warns)
return out
def scan_messages(messages):
errored = 0
for sha, text in messages:
errors, warns = _violations_for_message(text)
if errors or warns:
errored += 1 if errors else 0
for ln in _format(sha[:10], errors, warns):
print(ln)
if errored:
print("commit-lint: %d commit(s) with errors. Rewrite subject as "
"'type(scope): english summary', <=72 chars, no CJK, no board ids." % errored)
return 1
return 0
def git_messages(args):
out = subprocess.run(
["git", "log", "--no-merges", "--format=%H%x00%s%x00%b", "-z"] + args,
capture_output=True, text=True, encoding="utf-8", errors="replace",
)
if out.returncode != 0:
print("commit-lint: git log failed: %s" % out.stderr.strip())
sys.exit(2)
for record in out.stdout.split("\0"):
parts = record.split("\x00", 2)
if len(parts) < 2 or not parts[0].strip():
continue
yield parts[0], parts[1] + "\n" + (parts[2] if len(parts) > 2 else "")
def main(argv):
def value(flag):
return argv[argv.index(flag) + 1] if flag in argv else None
if "--message-file" in argv:
with open(value("--message-file"), "r", encoding="utf-8", errors="replace") as f:
return scan_messages([("HEAD", f.read())])
rng = value("--range")
if rng:
return scan_messages(git_messages([rng]))
since = value("--commits-since")
if since:
return scan_messages(git_messages(["--since=" + since]))
print("usage: commit_lint.py --message-file F | --range B..H | --commits-since DATE")
return 2
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))