diff --git a/scripts/check_version_match.py b/scripts/check_version_match.py index 5358083..9c3b7dd 100755 --- a/scripts/check_version_match.py +++ b/scripts/check_version_match.py @@ -1,24 +1,22 @@ #!/usr/bin/env python3 -"""Check that pyproject version matches the latest git tag.""" +"""Check that pyproject version matches the latest git tag. Optionally fix it by tagging.""" from pathlib import Path import subprocess import sys + if sys.version_info >= (3, 11): import tomllib as tomli else: import tomli - ROOT = Path(__file__).resolve().parents[1] - def pyproject_version() -> str: pyproject_path = ROOT / "pyproject.toml" with pyproject_path.open("rb") as f: data = tomli.load(f) return data["tool"]["poetry"]["version"] - def latest_tag() -> str: try: tag = subprocess.check_output( @@ -26,27 +24,39 @@ def latest_tag() -> str: ).strip() return tag.lstrip("v") except subprocess.CalledProcessError: - return "0.0.0" + return "" +def create_tag(version: str) -> None: + print(f"Tagging repository with version: v{version}") + subprocess.run(["git", "tag", f"v{version}"], cwd=ROOT, check=True) + subprocess.run(["git", "push", "origin", f"v{version}"], cwd=ROOT, check=True) + print(f"✅ Git tag v{version} created and pushed.") def main() -> int: - tag = latest_tag() + fix = "--fix" in sys.argv version = pyproject_version() + tag = latest_tag() if not tag: - print("No git tag found", file=sys.stderr) - return 1 + print("⚠️ No git tag found.", file=sys.stderr) + if fix: + create_tag(version) + return 0 + else: + return 1 if version != tag: print( - f"Version mismatch: pyproject.toml has {version} but latest tag is {tag}", + f"❌ Version mismatch: pyproject.toml has {version} but latest tag is {tag}", file=sys.stderr, ) + if fix: + create_tag(version) + return 0 return 1 - print(f"Version matches latest tag: {version}") + print(f"✔️ Version matches latest tag: {version}") return 0 - if __name__ == "__main__": sys.exit(main())