Check and fix commit messages against Conventional Commits 1.0.0 with ten rules (C001-C010) and safe auto-fix suggestions. Use when the user asks to write, review or validate a commit message, or to set up commit message rules.
95%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
Low
Low-risk findings worth noting
#!/usr/bin/env python3
"""Lint a commit message against Conventional Commits 1.0.0 (rules C001-C010).
Usage:
python3 scripts/commit_lint.py FILE [--fix]
python3 scripts/commit_lint.py --message "feat: add parser"
git log -1 --format=%B | python3 scripts/commit_lint.py -
Options: --types a,b,c --scopes a,b --require-scope --max-header N --no-ignore-merges
Exit codes: 0 clean, 1 findings, 2 usage or I/O error. Standard library only; it never
runs git and never touches anything except the FILE you pass with --fix.
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from commit_msg_lint import CommitLintError, Config, lint, suggest
MAX_BYTES = 65_536
def _csv(value: str) -> tuple[str, ...]:
return tuple(item.strip() for item in value.split(",") if item.strip())
def _build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="commit_lint.py",
description="Validate a commit message against Conventional Commits 1.0.0.",
)
parser.add_argument("path", nargs="?", help="message file, or '-' for standard input")
parser.add_argument("--message", help="the commit message text to lint")
parser.add_argument("--fix", action="store_true", help="rewrite FILE when the fix lints clean")
parser.add_argument("--types", type=_csv, help="comma-separated allowed types")
parser.add_argument("--scopes", type=_csv, help="comma-separated allowed scopes")
parser.add_argument("--require-scope", action="store_true", help="a scope is mandatory")
parser.add_argument("--max-header", type=int, default=72, help="maximum header length")
parser.add_argument("--no-ignore-merges", action="store_true", help="lint merge commits too")
return parser
def _read(args: argparse.Namespace) -> str:
if args.message is not None:
return str(args.message)
if args.path == "-":
return sys.stdin.read()
raw = Path(args.path).read_bytes()
if len(raw) > MAX_BYTES:
raise CommitLintError("Input exceeds maximum allowed size")
return raw.decode("utf-8")
def main(argv: list[str] | None = None) -> int:
parser = _build_parser()
try:
args = parser.parse_args(argv)
except SystemExit as exc:
return 0 if exc.code == 0 else 2
if (args.path is None) == (args.message is None):
sys.stderr.write("Error: give exactly one of FILE or --message\n")
return 2
if args.fix and args.path in (None, "-"):
sys.stderr.write("Error: --fix needs a FILE path\n")
return 2
defaults = Config()
config = Config(
types=args.types or defaults.types,
scopes=args.scopes or defaults.scopes,
require_scope=args.require_scope,
max_header=args.max_header,
ignore_merges=not args.no_ignore_merges,
)
try:
text = _read(args)
findings = lint(text, config)
if not findings:
return 0
fixed = suggest(text, config)
except (OSError, UnicodeDecodeError, CommitLintError) as err:
sys.stderr.write(f"Error: {str(err)[:200]}\n")
return 2
if args.fix and fixed is not None:
try:
Path(args.path).write_text(fixed, encoding="utf-8")
except OSError as err:
sys.stderr.write(f"Error writing fix to file: {err}\n")
return 2
return 0
for finding in findings:
sys.stdout.write(f"{finding}\n")
if fixed is not None:
sys.stdout.write("suggestion:\n")
for line in fixed.splitlines():
sys.stdout.write(f" {line}\n")
return 1
if __name__ == "__main__":
sys.exit(main())