Skip to content

Commit d173ffc

Browse files
committed
chore: version hooks
1 parent 9b69edd commit d173ffc

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎.pre-commit-config.yaml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ repos:
3131
types: [python]
3232
pass_filenames: false
3333
require_serial: true
34+
35+
- id: version-consistency
36+
name: version consistency (__version__ == pixi.toml)
37+
entry: python3 scripts/check_version.py
38+
language: system
39+
files: ^(metapang/__init__\.py|pixi\.toml)$
40+
pass_filenames: false

‎scripts/check_version.py‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python3
2+
"""Check that the version is consistent across the repo.
3+
4+
Fails if metapang.__version__ and the pixi.toml [workspace] version disagree.
5+
"""
6+
7+
import argparse
8+
import re
9+
import sys
10+
from pathlib import Path
11+
12+
ROOT = Path(__file__).resolve().parent.parent
13+
14+
15+
def _read(pattern: str, path: Path) -> str | None:
16+
match = re.search(pattern, path.read_text(), re.MULTILINE)
17+
return match.group(1) if match else None
18+
19+
20+
def main() -> int:
21+
parser = argparse.ArgumentParser()
22+
parser.add_argument("--tag", help="Expected version, e.g. v0.1.0 or 0.1.0")
23+
parser.add_argument(
24+
"--pre-push",
25+
action="store_true",
26+
help="Read git pre-push refs on stdin and validate any pushed version tags",
27+
)
28+
args = parser.parse_args()
29+
30+
init_v = _read(r'^__version__\s*=\s*"([^"]+)"', ROOT / "metapang" / "__init__.py")
31+
pixi_v = _read(r'^version\s*=\s*"([^"]+)"', ROOT / "pixi.toml")
32+
33+
if init_v is None or pixi_v is None:
34+
print("check_version: could not read the version", file=sys.stderr)
35+
return 1
36+
37+
if init_v != pixi_v:
38+
print(
39+
f"check_version: mismatch between metapang.__version__ ({init_v}) and "
40+
f"pixi.toml ({pixi_v}).",
41+
file=sys.stderr,
42+
)
43+
return 1
44+
45+
tags: list[str] = []
46+
if args.tag is not None:
47+
tags.append(args.tag)
48+
if args.pre_push:
49+
for line in sys.stdin:
50+
local_ref = line.split()[0] if line.split() else ""
51+
match = re.match(r"^refs/tags/(v?\d+\.\d+\.\d+)$", local_ref)
52+
if match:
53+
tags.append(match.group(1))
54+
55+
for tag in tags:
56+
if tag.lstrip("v") != init_v:
57+
print(
58+
f"check_version: tag {tag} does not match the repo version ({init_v}).",
59+
file=sys.stderr,
60+
)
61+
return 1
62+
63+
return 0
64+
65+
66+
if __name__ == "__main__":
67+
sys.exit(main())

‎scripts/git-pre-push‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
#
3+
# git pre-push hook: reject pushing a version tag (vX.Y.Z) that does not match
4+
# the repo version (metapang.__version__ / pixi.toml).
5+
6+
exec python3 "$(git rev-parse --show-toplevel)/scripts/check_version.py" --pre-push

0 commit comments

Comments
 (0)