A small Python checker for Git commit messages that enforces the 50/72 rule:
- 50: the subject line (the first line Git keeps) is at most 50 characters so one-line summaries stay readable in logs and
git log --oneline. - 72: every following line is at most 72 characters so wrapped text fits common terminals and tools.
Comment lines (optional leading whitespace, then #) are ignored, consistent with how Git strips them from the final message.
- Python 3.9+ (uses
list[str]typing; adjust if you need older Python)
Git’s pre-commit hook runs before the commit is recorded and does not receive the final message file the same way. Message checks belong on the commit-msg hook, which receives the path to the proposed log message as the first argument.
If you use the pre-commit framework, register this script on the commit-msg stage (see below).
This repo includes .gitmessage: when you run git commit, your editor can open with a short checklist (as commented lines Git strips out). Point Git at it from the repository root:
git config commit.template .gitmessageOr set it only for this clone: git config --local commit.template .gitmessage.
From your repository root (adjust the path if you move the script):
mkdir -p .git/hooks
cat > .git/hooks/commit-msg <<'EOF'
#!/bin/sh
exec python3 "$(git rev-parse --show-toplevel)/5072.py" "$1"
EOF
chmod +x .git/hooks/commit-msgTo share hooks with the team, use core.hooksPath pointing at a versioned hooks/ directory and commit a commit-msg wrapper there.
Add to .pre-commit-config.yaml:
repos:
- repo: local
hooks:
- id: commit-msg-50-72
name: 50/72 commit message
entry: python3 5072.py
language: system
stages: [commit-msg]
always_run: trueThen run pre-commit install --hook-type commit-msg.
Add retry backoff for API client
When the upstream returns 429, sleep using Retry-After before
retrying. Keeps logs quieter and avoids hammering the service.
The first line is the subject (≤ 50 characters). A blank line separates the body; each body line should be ≤ 72 characters.
printf '%s\n' 'Short subject' > /tmp/msg.txt
python3 5072.py /tmp/msg.txt && echo OKThe script exits 0 if the message is valid and 1 if not, printing details to stderr.