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

Show on the map →

Called by none

Calls violations

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
lines 107–107paths = list(sys.argv[1:] if argv is None else argv)lines 108–108◇ if not pathslines 109–109print("commit-msg guard: no message file given; the hook is misconfigured", file=sys.stderr)lines 110–110return 1lines 112–112failed = Falselines 113–113◇ for name in pathslines 114–114path = Path(name)lines 115–115trylines 116–116message = path.read_text("utf-8", errors="replace")lines 117–117except OSError as exclines 118–118print(f"commit-msg guard: cannot read {name}: {exc}", file=sys.stderr)lines 119–119failed = Truelines 120–120continuelines 121–121hits = violations(message)lines 122–122◇ if not hitslines 123–123continuelines 124–124failed = Truelines 125–125print(f"REFUSED — {name} carries {len(hits)} forbidden line(s):", file=sys.stderr)lines 126–126◇ for label, reason, text in hitslines 127–127print(f" {label}: {text!r}", file=sys.stderr)lines 128–128print(f" {reason}", file=sys.stderr)lines 129–133print( "\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, )lines 134–134return 1 if failed else 0

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

Show on the map →

Called by main

Calls none

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

Activity · 6 steps
lines 82–82found: list[tuple[str, str, str]] = []lines 83–83◇ for name, pattern, reason in _FORBIDDENlines 84–84match = pattern.search(message)lines 85–85◇ if matchlines 86–86found.append((name, reason, match.group(0).strip()))lines 87–87return found

Flows