cost-partition 0.2.0

Cost partitioning (saturated, max-over-orders) over arbitrary planning abstractions
Documentation
#!/usr/bin/env python3
"""Check a commit message against the imperative-mood baseline (commit-msg hook).

    python3 check-commit-msg.py .git/COMMIT_EDITMSG

Subject rules: imperative mood ("Add parser", not "Added parser" / "Adds parser" / "feat: add
parser"), capitalized, at most 72 characters, no trailing period; a blank line before the body.
Merge, revert, fixup and squash commits generated by git are accepted as they are.
A repo whose CONTRIBUTING.md defines another style should drop this hook.
"""

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):
    """Return the rule violations of a commit subject line."""
    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():
    """Entry point."""
    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()