Spaces:
Running
Running
File size: 4,408 Bytes
a9e27c0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | """Validate release intent against changed files and calculate one version bump."""
import argparse
import json
import re
import subprocess
import tomllib
from pathlib import Path
RELEASE_FILES = {".python-version", "pyproject.toml", "uv.lock"}
RELEASE_DIRS = ("assets/", "scripts/", "src/")
VERSION = re.compile(r"(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)")
PREFIX = re.compile(r"(patch|minor|major): \S[^\r\n]*")
RESERVED = re.compile(r"\s*(patch|minor|major)\s*[:\uff1a]", re.IGNORECASE)
def git(*args: str) -> str:
return subprocess.run(
["git", *args], check=True, capture_output=True, text=True, encoding="utf-8"
).stdout.rstrip("\r\n")
def changed_paths(base: str, head: str) -> list[str]:
return list(
filter(
None,
git("diff", "--no-renames", "--name-only", "-z", base, head).split("\0"),
)
)
def release_paths(paths: list[str]) -> list[str]:
return [
path for path in paths if path in RELEASE_FILES or path.startswith(RELEASE_DIRS)
]
def release_kind(title: str, paths: list[str]) -> str | None:
match = PREFIX.fullmatch(title)
released = release_paths(paths)
if not match and RESERVED.match(title):
raise ValueError(
"Use exactly patch: , minor: , or major: followed by a description"
)
if released and not match:
raise ValueError("Release changes require a patch: , minor: , or major: title")
if not released and match:
raise ValueError("A non-release PR must not use a release prefix")
return match[1] if match else None
def next_version(previous: str, kind: str) -> str:
match = VERSION.fullmatch(previous)
if not match:
raise ValueError(f"Invalid release version: {previous}")
major, minor, patch = map(int, match.groups())
if kind == "major":
return f"{major + 1}.0.0"
if kind == "minor":
return f"{major}.{minor + 1}.0"
if kind == "patch":
return f"{major}.{minor}.{patch + 1}"
raise ValueError(f"Invalid release kind: {kind}")
def check_packaging(head: str) -> None:
project = tomllib.loads(git("show", f"{head}:pyproject.toml"))["project"]
if (
project["name"] != "free-claude-code"
or "version" in project
or "version" not in project.get("dynamic", [])
):
raise ValueError("FCC must declare a dynamic version without project.version")
lock = tomllib.loads(git("show", f"{head}:uv.lock"))
roots = [
p
for p in lock["package"]
if p["name"] == "free-claude-code" and p.get("source") == {"editable": "."}
]
if len(roots) != 1 or "version" in roots[0]:
raise ValueError("uv.lock must have one editable FCC package without a version")
def check(base: str, head: str, title: str) -> None:
base = git("rev-parse", "--verify", "--end-of-options", f"{base}^{{commit}}")
head = git("rev-parse", "--verify", "--end-of-options", f"{head}^{{commit}}")
paths = changed_paths(git("merge-base", base, head), head)
print("Release changes: " + (", ".join(release_paths(paths)) or "none"))
kind = release_kind(title, paths)
check_packaging(head)
print(f"Version policy passed: {kind or 'no release'}")
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--base")
parser.add_argument("--head", required=True)
parser.add_argument("--title")
parser.add_argument("--pr-json", type=Path)
args = parser.parse_args()
try:
if args.pr_json:
pr = json.loads(args.pr_json.read_text(encoding="utf-8"))
if pr["head"]["sha"] != git(
"rev-parse", "--verify", "--end-of-options", f"{args.head}^{{commit}}"
):
raise ValueError("PR head changed; use the current policy run")
base, title = pr["base"]["sha"], pr["title"]
else:
base, title = args.base, args.title
if not base or title is None:
raise ValueError("Provide --pr-json or both --base and --title")
check(base, args.head, title)
except (
ValueError,
KeyError,
TypeError,
OSError,
subprocess.CalledProcessError,
) as error:
print(f"Version policy failed: {error}")
return 1
return 0
if __name__ == "__main__":
raise SystemExit(main())
|