57 lines
1.8 KiB
Python
57 lines
1.8 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())
|
|
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()
|