Skip to content

Commit 1097255

Browse files
committed
Added pre-commit hook to sync tag to version in file
1 parent a406371 commit 1097255

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

.hooks/sync_versions.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
import subprocess
3+
import pathlib
4+
import re
5+
import sys
6+
7+
PACKAGE_FILE = pathlib.Path("socketsecurity/__init__.py")
8+
VERSION_PATTERN = re.compile(r"__version__\s*=\s*['\"]([^'\"]+)['\"]")
9+
10+
def get_hatch_version():
11+
return subprocess.check_output(["hatch", "version"], text=True).strip()
12+
13+
def get_current_version():
14+
content = PACKAGE_FILE.read_text()
15+
match = VERSION_PATTERN.search(content)
16+
return match.group(1) if match else None
17+
18+
def update_version(new_version):
19+
content = PACKAGE_FILE.read_text()
20+
new_content = VERSION_PATTERN.sub(f"__version__ = '{new_version}'", content)
21+
PACKAGE_FILE.write_text(new_content)
22+
23+
def main():
24+
hatch_version = get_hatch_version()
25+
current_version = get_current_version()
26+
27+
if not current_version:
28+
print("❌ Couldn't find __version__ in", PACKAGE_FILE)
29+
return 1
30+
31+
if hatch_version != current_version:
32+
print(f"🔁 Syncing version: {current_version}{hatch_version}")
33+
update_version(hatch_version)
34+
return 1 # Exit 1 so pre-commit fails and shows diff
35+
36+
print(f"✅ Version is in sync: {hatch_version}")
37+
return 0
38+
39+
if __name__ == "__main__":
40+
sys.exit(main())

.pre-commit-config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
- id: sync-version
5+
name: Sync __version__ with hatch version
6+
entry: python .hooks/sync_version.py
7+
language: system
8+
types: [python]
9+
files: ^socketsecurity/__init__.py$

0 commit comments

Comments
 (0)