commit_msg_guard.py¶
scripts/commit_msg_guard.py
REFUSE A COMMIT MESSAGE THAT WOULD PUBLISH A SESSION IDENTIFIER OR AN ASSISTANT TRAILER.
main
int commit_msg_guard.main(list[str]|None argv=None)
Check a commit-message file and refuse it if it carries a forbidden line.
scripts/commit_msg_guard.py:95
Source · 42 lines
def main(argv: list[str] | None = None) -> int:
"""A MISSING FILE IS NOT A PASS. `commit-msg` is always handed a path, so a path that cannot be
read means the hook was wired wrong — and returning 0 there would make a broken hook
indistinguishable from a clean message, which is this repository's most-repeated defect shape.
Single exit point, by the returns-per-function limit this repository enforces.
@brief Guard one or more commit-message files.
@param argv The paths.
@return Process exit code.
@version 1
"""
paths = list(sys.argv[1:] if argv is None else argv)
if not paths:
print("commit-msg guard: no message file given; the hook is misconfigured", file=sys.stderr)
return 1
failed = False
for name in paths:
path = Path(name)
try:
message = path.read_text("utf-8", errors="replace")
except OSError as exc:
print(f"commit-msg guard: cannot read {name}: {exc}", file=sys.stderr)
failed = True
continue
hits = violations(message)
if not hits:
continue
failed = True
print(f"REFUSED — {name} carries {len(hits)} forbidden line(s):", file=sys.stderr)
for label, reason, text in hits:
print(f" {label}: {text!r}", file=sys.stderr)
print(f" {reason}", file=sys.stderr)
print(
"\nRemove them and commit again. These are refused by the repository, not by a "
"style preference — whatever tooling suggested the line, it does not get to add it.",
file=sys.stderr,
)
return 1 if failed else 0
Activity · 23 steps
Flows
violations
list[tuple[str, str, str]] commit_msg_guard.violations(str message)
Every forbidden pattern a message matches.
scripts/commit_msg_guard.py:69
Source · 21 lines
def violations(message: str) -> list[tuple[str, str, str]]:
"""RETURNS ALL OF THEM, not the first. A message carrying both a trailer and a prose link
should be reported once with both, so the author fixes it in one pass instead of discovering
the second on the next attempt.
The matched TEXT is returned so the message can quote what it found — a refusal naming only a
rule leaves the author hunting for the line.
@brief Forbidden patterns present in a message.
@param message The commit message.
@return Triples of name, reason and matched text.
@version 1
"""
found: list[tuple[str, str, str]] = []
for name, pattern, reason in _FORBIDDEN:
match = pattern.search(message)
if match:
found.append((name, reason, match.group(0).strip()))
return found