61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Select site configuration release tags; version X is edited by hand."""
|
|
|
|
import argparse
|
|
from pathlib import Path
|
|
import re
|
|
import subprocess
|
|
|
|
|
|
FINAL = re.compile(r"v(\d+)\.(\d+)\.(\d+)$")
|
|
RC = re.compile(r"v(\d+)\.(\d+)\.(\d+)-rc\.(\d+)$")
|
|
|
|
|
|
def git(*args: str) -> str:
|
|
return subprocess.check_output(["git", *args], text=True).strip()
|
|
|
|
|
|
def release_tag(branch: str, tags: list[str], changed: list[str], major: int) -> str | None:
|
|
finals = sorted((tuple(map(int, match.groups())), tag)
|
|
for tag in tags if (match := FINAL.fullmatch(tag)))
|
|
latest = finals[-1] if finals else None
|
|
if latest and latest[0][0] > major:
|
|
raise ValueError("manual major version is behind the latest release")
|
|
if not changed:
|
|
return None
|
|
if latest and latest[0][0] == major:
|
|
_, minor, patch = latest[0]
|
|
if "ci/validate_site.py" in changed:
|
|
minor, patch = minor + 1, 0
|
|
else:
|
|
patch += 1
|
|
else:
|
|
minor, patch = (1, 0) if major == 0 else (0, 0)
|
|
base = f"v{major}.{minor}.{patch}"
|
|
if branch == "main":
|
|
return base
|
|
numbers = [int(match.group(4)) for tag in tags
|
|
if (match := RC.fullmatch(tag)) and tag.startswith(base + "-rc.")]
|
|
return f"{base}-rc.{max(numbers, default=0) + 1}"
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("branch", choices=("main", "next"))
|
|
args = parser.parse_args()
|
|
major = int(Path(__file__).with_name("version-x.txt").read_text().strip())
|
|
if major < 0:
|
|
raise ValueError("major version must be nonnegative")
|
|
tags = git("tag", "-l", "v*").splitlines()
|
|
finals = sorted((tuple(map(int, match.groups())), tag)
|
|
for tag in tags if (match := FINAL.fullmatch(tag)))
|
|
changed = git("diff", "--name-only", finals[-1][1], "HEAD").splitlines() if finals else ["initial"]
|
|
tag = release_tag(args.branch, tags, changed, major)
|
|
print(f"release={'true' if tag else 'false'}")
|
|
if tag:
|
|
print(f"tag={tag}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|