Files
devplacepy/.githooks/pre-commit
T
retoorandClaude Sonnet 5 78023d36ab Add automatic patch-version bumping via a tracked git pre-commit hook
pyproject.toml's version has never moved past the 1.0.0 scaffold value
across 368 commits; there was no bump mechanism at all, Claude-driven or
otherwise. Add .githooks/pre-commit (stdlib Python, no dependencies) that
increments the patch version on every commit and stages it automatically,
deferring to a deliberate version edit already staged in the same commit
and skipping merge commits. Wire it in via `make install` (git config
core.hooksPath .githooks) so it activates for every clone without
requiring any change to existing workflows.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TwLhnueWrsK15wrieXE5m7
2026-09-07 15:22:23 +02:00

52 lines
1.4 KiB
Python
Executable File

#!/usr/bin/env python3
# retoor <retoor@molodetz.nl>
import re
import subprocess
import sys
from pathlib import Path
REPO_ROOT = Path(
subprocess.run(
["git", "rev-parse", "--show-toplevel"],
capture_output=True,
text=True,
check=True,
).stdout.strip()
)
PYPROJECT = REPO_ROOT / "pyproject.toml"
VERSION_LINE = re.compile(r'^version = "(\d+)\.(\d+)\.(\d+)"$', re.MULTILINE)
STAGED_VERSION_CHANGE = re.compile(r'^[+-]version = "\d+\.\d+\.\d+"$', re.MULTILINE)
def staged_diff(path: Path) -> str:
result = subprocess.run(
["git", "diff", "--cached", "--unified=0", "--", str(path)],
capture_output=True,
text=True,
cwd=REPO_ROOT,
)
return result.stdout
def main() -> int:
if (REPO_ROOT / ".git" / "MERGE_HEAD").exists():
return 0
if not PYPROJECT.is_file():
return 0
if STAGED_VERSION_CHANGE.search(staged_diff(PYPROJECT)):
return 0
text = PYPROJECT.read_text()
match = VERSION_LINE.search(text)
if not match:
return 0
major, minor, patch = (int(part) for part in match.groups())
bumped = f'version = "{major}.{minor}.{patch + 1}"'
PYPROJECT.write_text(VERSION_LINE.sub(bumped, text, count=1))
subprocess.run(["git", "add", str(PYPROJECT)], cwd=REPO_ROOT, check=True)
return 0
if __name__ == "__main__":
sys.exit(main())