Files
alkane-os/ci/version.py
T
noah 8672507fa4
source-pr / source (pull_request) Successful in 42s
Preserve queued OS merge builds and guard reruns
2026-10-19 14:45:35 -06:00

64 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Select a release tag from Git history. The major version is manual."""
import argparse
import pathlib
import re
import subprocess
def git(*args):
return subprocess.check_output(["git", *args], text=True).strip()
def versions(branch):
suffix = r"" if branch == "main" else r"-rc\.(\d+)"
pattern = re.compile(r"^v(\d+)\.(\d+)\.(\d+)" + suffix + r"$")
result = []
for tag in git("tag", "-l", "v*").splitlines():
match = pattern.fullmatch(tag)
if match:
result.append((tuple(map(int, match.groups())), tag))
return sorted(result)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("branch", choices=("main", "next"))
args = parser.parse_args()
major = int(pathlib.Path("ci/version-x.txt").read_text().strip())
# A rerun of a commit already covered by a published tag must not issue
# a newer candidate number for older source.
tagged_descendants = set(git("tag", "--contains", "HEAD", "-l", "v*").splitlines())
known_tags = {tag for _, tag in versions("main") + versions("next")}
if tagged_descendants & known_tags:
print("release=false")
return
finals = versions("main")
previous = finals[-1] if finals else None
if previous and previous[0][0] > major:
raise SystemExit("manual major version is behind the latest release")
if previous and previous[0][0] == major:
_, minor, patch = previous[0]
changed = git("diff", "--name-only", previous[1], "HEAD").splitlines()
if not changed:
print("release=false")
return
feature = any(path.startswith("src/") or path == "Cargo.toml" for path in changed)
minor, patch = (minor + 1, 0) if feature else (minor, patch + 1)
else:
minor, patch = (1, 0) if major == 0 else (0, 0)
base = f"v{major}.{minor}.{patch}"
if args.branch == "next":
candidates = [parts[0][3] for parts in versions("next") if parts[0][:3] == (major, minor, patch)]
tag = f"{base}-rc.{max(candidates, default=0) + 1}"
else:
tag = base
print("release=true")
print(f"tag={tag}")
print(f"version={tag[1:]}")
if __name__ == "__main__":
main()