Skip to content

Commit

Permalink
version tag safeguard
Browse files Browse the repository at this point in the history
  • Loading branch information
gschnabel committed Jul 24, 2024
1 parent 37b094e commit 544ed51
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
repos:
- repo: local
hooks:
- id: check-tag-version
name: Check if git tag matches __version__
entry: python .pre-commit-hooks/check-tag-version.py
language: python
stages: [push]
always_run: true

- repo: local
hooks:
- id: update-last-modified-header
Expand Down
48 changes: 48 additions & 0 deletions .pre-commit-hooks/check_version_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import subprocess
import sys
import re
import os


def get_latest_version_tag():
tags = (
subprocess.check_output(["git", "tag", "--sort=-creatordate"], text=True)
.strip()
.split("\n")
)
version_tags = [tag for tag in tags if re.match(r"^v\d+\.\d+\.\d+$", tag)]
if version_tags:
return version_tags[0]
return None


def get_version():
version_file = "endf_parserpy/__init__.py"
if not os.path.exists(version_file):
raise RuntimeError(f"Could not find {version_file}.")
with open(version_file, "r") as file:
for line in file:
if line.strip().startswith("__version"):
version = line.split("=")[1].strip().strip('"')
return version
raise RuntimeError("Could not find version string.")


def main():
tag = get_latest_version_tag()
if not tag:
print("No version tag found. Skipping version check.")
sys.exit(0)

version = get_version_from_init()
tag_version = tag[1:]
if version != tag_version:
print(f"Tag '{tag}' does not match version '{version}' in __init__.py")
sys.exit(1)

print(f"Tag '{tag}' matches version '{version}' in __init__.py")
sys.exit(0)


if __name__ == "__main__":
main()

0 comments on commit 544ed51

Please sign in to comment.