import re
import sys
MAX_SUBJECT = 72
GIT_GENERATED = re.compile(r"^(Merge |Revert \"|fixup! |squash! |amend! )")
CONVENTIONAL = re.compile(r"^[a-z]+(\([^)]*\))?!?: ")
PAST_TENSE_OK = {"embed", "feed", "need", "proceed", "seed", "shed", "speed", "succeed", "exceed", "bleed"}
THIRD_PERSON = {
"adds", "allows", "bumps", "changes", "cleans", "creates", "deletes", "documents", "drops",
"enables", "disables", "ensures", "extracts", "fixes", "handles", "implements", "improves",
"introduces", "makes", "moves", "prevents", "refactors", "removes", "renames", "replaces",
"simplifies", "splits", "supports", "updates", "uses",
}
def subject_problems(subject):
problems = []
first = subject.split(" ", 1)[0].rstrip(":,").lower()
if CONVENTIONAL.match(subject):
problems.append("no Conventional Commits prefix; write e.g. 'Add parser for X'")
if subject[:1].islower():
problems.append("start with a capital letter")
if first.endswith("ed") and first not in PAST_TENSE_OK:
problems.append(f"use the imperative ('{first[:-2] or first}...'), not past tense '{first}'")
if first in THIRD_PERSON:
problems.append(f"use the imperative, not '{first}'")
if len(subject) > MAX_SUBJECT:
problems.append(f"subject is {len(subject)} characters (max {MAX_SUBJECT})")
if subject.endswith("."):
problems.append("no trailing period")
return problems
def main():
with open(sys.argv[1], encoding="utf-8") as handle:
lines = [line.rstrip("\n") for line in handle if not line.startswith("#")]
if not lines or not lines[0].strip():
sys.exit("commit message is empty")
subject = lines[0].strip()
if GIT_GENERATED.match(subject):
return
problems = subject_problems(subject)
if len(lines) > 1 and lines[1].strip():
problems.append("leave a blank line between subject and body")
if problems:
print(f"commit subject: {subject!r}")
print("\n".join(f" - {p}" for p in problems))
sys.exit(1)
if __name__ == "__main__":
main()