fix(scripts): validate every release tag and gate --apply on origin/main

A mis-cut tag could serve as the start point and silently drop the runs
before it: x-v9.9.9 or a lightweight x-v1.0.1 on a later landing hid an
untagged 1.1.0 and --apply succeeded. Every reachable <p>-v<semver> tag is
now validated (annotated, peels to a landing of its own version); an
invalid one is an ERROR row naming it, blocks --apply, and is never the
start point.

Also from review:
- --apply needs origin/main and a ref on it; no local-main fallback.
- A version with literal quotes is invalid, not stripped.
- Plugin dirs are listed NUL-separated; a leading-dash name exits 2.
- With no tags, the backfill starts at the current version's first
  landing, so a recurring version is released once, there (Case 2, 12d).
- expect_row ERE-escapes plugin and version.

ADR-0033 updated to match.

Refs: #223

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
This commit is contained in:
Defame1297andClaude Opus 5.5 committed 2026-11-27 18:14:23 +00:00
1 parent b3cdcc9bbb
commit b3fd1f2f0c
3 files changed
+194 -37

No files matched your search

+92 -8
View File
@@ -44,6 +44,8 @@ write_apm() {
commit() { (cd "$1" && git add -A && git commit -q -m "${2:-change}"); }
head_sha() { git -C "$1" rev-parse HEAD; }
# --apply needs origin/main; point it at local main (no remote needed).
sync_origin() { git -C "$1" update-ref refs/remotes/origin/main refs/heads/main; }
# run <repo> [args...]: stdout+stderr, exit code in RC.
run() {
@@ -57,10 +59,13 @@ expect_rc() {
if [[ "$RC" -eq "$want" ]]; then pass "$desc (exit $want)"; else fail "$desc: exit $RC, want $want"; echo "$OUT" | sed 's/^/ /'; fi
}
# ERE-escape $1 so a version's dots match only dots.
ere_quote() { sed 's/[][\\.*^$+?(){}|/]/\\&/g' <<< "$1"; }
# expect_row <desc> <plugin> <version> <sha> <status>
expect_row() {
local desc="$1" re
re="^$2 +$3 +${4:0:7} +$5\$"
re="^$(ere_quote "$2") +$(ere_quote "$3") +${4:0:7} +$5\$"
if grep -Eq "$re" <<< "$OUT"; then pass "$desc"; else fail "$desc: no row '$2 $3 ${4:0:7} $5'"; echo "$OUT" | sed 's/^/ /'; fi
}
@@ -95,16 +100,25 @@ run "$R" --ref main
expect_row "reads the ref, not the working tree" alpha 1.1.0 "$MERGE_A" MISSING
(cd "$R" && git checkout -q -- .)
echo "Case 2: version goes X -> Y -> X -- only the current run is backfilled"
echo "Case 2: version goes X -> Y -> X with no tags -- backfill starts at X's first landing"
R="$(new_repo)"
write_apm "$R" gamma 0.9.0; commit "$R" z; ZERO="$(head_sha "$R")"
write_apm "$R" gamma 1.0.0; commit "$R" a; FIRST="$(head_sha "$R")"
write_apm "$R" gamma 1.1.0; commit "$R" b
write_apm "$R" gamma 1.1.0; commit "$R" b; SECOND="$(head_sha "$R")"
write_apm "$R" gamma 1.0.0; commit "$R" c; THIRD="$(head_sha "$R")"
run "$R" --ref main
expect_rc "X->Y->X dry run" 1
expect_row "target is the third run's commit" gamma 1.0.0 "$THIRD" MISSING
expect_rows "exactly one wanted row" gamma 1
if ! grep -q "${FIRST:0:7}" <<< "$OUT"; then pass "first X run not named"; else fail "first X run named"; fi
expect_row "X is MISSING at its first landing" gamma 1.0.0 "$FIRST" MISSING
expect_row "Y after it is MISSING" gamma 1.1.0 "$SECOND" MISSING
expect_row "X's later run is RELEASED" gamma 1.0.0 "$THIRD" RELEASED
expect_rows "three wanted rows" gamma 3
if ! grep -q "${ZERO:0:7}" <<< "$OUT"; then pass "history before X's first landing not named"; else fail "older run named"; fi
sync_origin "$R"
run "$R" --ref main --apply
expect_rc "backfill --apply" 0
if [[ "$(git -C "$R" rev-parse "gamma-v1.0.0^{commit}" 2>/dev/null)" == "$FIRST" ]]; then pass "gamma-v1.0.0 on the first landing"; else fail "gamma-v1.0.0 misplaced"; fi
run "$R" --ref main
expect_rc "dry run after backfill is clean (idempotent)" 0
echo "Case 3: non-bump edit to apm.yml does not start a new run"
R="$(new_repo)"
@@ -184,6 +198,7 @@ expect_rows "older 1.0.0 not wanted" eta 3
if ! grep -q "${V10:0:7}" <<< "$OUT"; then pass "1.0.0 commit not named"; else fail "1.0.0 named"; fi
echo "Case 10: --apply creates annotated tags, prints explicit push line, is idempotent"
sync_origin "$R"
run "$R" --ref main --apply
expect_rc "--apply" 0
for pair in "1.2.0:$V12" "1.3.0:$V13"; do
@@ -205,6 +220,7 @@ R="$(new_repo)"
write_apm "$R" theta 1.0.0; write_apm "$R" iota 1.0.0; commit "$R" a; A="$(head_sha "$R")"
write_apm "$R" iota 1.0.0 iota "Reworded."; commit "$R" b
(cd "$R" && git tag iota-v1.0.0 "$A")
sync_origin "$R"
run "$R" --ref main --apply
expect_rc "ERROR blocks --apply" 2
if [[ -z "$(git -C "$R" tag -l 'theta-v*')" ]]; then pass "nothing created for the clean plugin"; else fail "theta tag created despite ERROR"; fi
@@ -222,6 +238,7 @@ expect_row "first landing of 1.0.0 is MISSING" kappa 1.0.0 "$KB" MISSING
expect_row "later landing of 1.0.0 is RELEASED" kappa 1.0.0 "$KD" RELEASED
expect_row "1.1.0 MISSING" kappa 1.1.0 "$KC" MISSING
if grep -q "first landing ${KB:0:7}" <<< "$OUT"; then pass "note names the first landing"; else fail "no first-landing note"; fi
sync_origin "$R"
run "$R" --ref main --apply
expect_rc "--apply with a recurring version" 0
if [[ "$(git -C "$R" rev-parse "kappa-v1.0.0^{commit}" 2>/dev/null)" == "$KB" ]]; then pass "kappa-v1.0.0 cut at the first landing"; else fail "kappa-v1.0.0 not on the first landing"; fi
@@ -242,6 +259,7 @@ expect_rc "dry run: only bb is missing" 1
expect_row "aa 1.1.0 OK" aa 1.1.0 "$A2" OK
expect_row "reverted aa 1.0.0 is RELEASED" aa 1.0.0 "$A3" RELEASED
expect_row "bb 1.2.0 MISSING" bb 1.2.0 "$B2" MISSING
sync_origin "$R"
run "$R" --ref main --apply
expect_rc "--apply after a revert" 0
if [[ "$(git -C "$R" rev-parse "bb-v1.2.0^{commit}" 2>/dev/null)" == "$B2" ]]; then pass "bb-v1.2.0 created"; else fail "bb-v1.2.0 not created"; fi
@@ -260,8 +278,9 @@ write_apm "$R" nu 1.0.0; write_apm "$R" xi 1.0.0; commit "$R" a; N1="$(head_sha
rm -r "$R/plugins/nu"; commit "$R" delete
write_apm "$R" nu 1.0.0; commit "$R" readd; N3="$(head_sha "$R")"
run "$R" --ref main --plugin nu
expect_rows "untagged: one wanted row" nu 1
expect_row "untagged: target is the re-add" nu 1.0.0 "$N3" MISSING
expect_rows "untagged: two wanted rows" nu 2
expect_row "untagged: first landing is MISSING" nu 1.0.0 "$N1" MISSING
expect_row "untagged: re-add is RELEASED" nu 1.0.0 "$N3" RELEASED
(cd "$R" && git tag -a nu-v1.0.0 -m x "$N1")
run "$R" --ref main --plugin nu
expect_rc "tagged before the delete" 0
@@ -301,10 +320,15 @@ if grep -q "whitespace" <<< "$OUT"; then pass "message names whitespace"; else f
printf 'name: mu\nversion: "1.0.0 "\n' > "$R/plugins/mu/apm.yml"; commit "$R" b
run "$R" --ref main
expect_rc "trailing space in version" 2
printf "name: mu\nversion: \"'1.0.0'\"\n" > "$R/plugins/mu/apm.yml"; commit "$R" c
run "$R" --ref main
expect_rc "literal quotes in version are invalid, not stripped" 2
if grep -q "no strict MAJOR.MINOR.PATCH" <<< "$OUT"; then pass "message names the invalid version"; else fail "no invalid-version message"; fi
echo "Case 12h: a tag name git rejects is an ERROR and nothing is created"
R="$(new_repo)"
write_apm "$R" rho 1.0.0; write_apm "$R" "a..b" 1.0.0; commit "$R" a
sync_origin "$R"
run "$R" --ref main --apply
expect_rc "invalid ref name blocks --apply" 2
if grep -q "check-ref-format" <<< "$OUT"; then pass "message names check-ref-format"; else fail "no check-ref-format message"; fi
@@ -314,6 +338,7 @@ echo "Case 12i: a git tag failure mid --apply reports what was created"
R="$(new_repo)"
write_apm "$R" sa 1.0.0; write_apm "$R" sb 1.0.0; commit "$R" a
mkdir -p "$R/.git/refs/tags" && : > "$R/.git/refs/tags/sb-v1.0.0.lock"
sync_origin "$R"
run "$R" --ref main --apply
expect_rc "mid-run git tag failure" 2
if [[ -n "$(git -C "$R" tag -l sa-v1.0.0)" ]]; then pass "sa-v1.0.0 was created first"; else fail "sa-v1.0.0 missing"; fi
@@ -322,6 +347,7 @@ if grep -qx "git push origin sa-v1.0.0" <<< "$OUT"; then pass "push line names w
echo "Case 12j: --apply with --plugin creates only that plugin's tag"
R="$(new_repo)"
write_apm "$R" ta 1.0.0; write_apm "$R" tb 1.0.0; commit "$R" a
sync_origin "$R"
run "$R" --ref main --plugin ta --apply
expect_rc "--apply --plugin ta" 0
if [[ -n "$(git -C "$R" tag -l ta-v1.0.0)" && -z "$(git -C "$R" tag -l 'tb-v*')" ]]; then pass "only ta tagged"; else fail "wrong tags: $(git -C "$R" tag -l | tr '\n' ' ')"; fi
@@ -330,6 +356,7 @@ echo "Case 12k: --ref <older> --plugin <p> --apply tags that plugin's older vers
R="$(new_repo)"
write_apm "$R" ua 1.0.0; write_apm "$R" ub 1.0.0; commit "$R" a; U1="$(head_sha "$R")"
write_apm "$R" ua 1.1.0; write_apm "$R" ub 1.1.0; commit "$R" b; U2="$(head_sha "$R")"
sync_origin "$R"
run "$R" --ref "$U1" --plugin ua --apply
expect_rc "one-off older tag" 0
if [[ "$(git -C "$R" tag -l)" == "ua-v1.0.0" ]]; then pass "only ua-v1.0.0 created"; else fail "wrong tags: $(git -C "$R" tag -l | tr '\n' ' ')"; fi
@@ -350,6 +377,63 @@ expect_rc "default ref" 1
expect_row "reads origin/main's version" va 1.0.0 "$V1" MISSING
if ! grep -q "reading local main" <<< "$OUT"; then pass "no staleness note when origin/main resolves"; else fail "staleness note printed"; fi
echo "Case 12m: an invalid tag is never the start point and is reported"
R="$(new_repo)"
write_apm "$R" x 1.0.0; commit "$R" a; X10="$(head_sha "$R")"
write_apm "$R" x 1.1.0; commit "$R" b; X11="$(head_sha "$R")"
write_apm "$R" x 1.2.0; commit "$R" c; X12="$(head_sha "$R")"
write_apm "$R" x 1.3.0; commit "$R" d
(cd "$R" && git tag -a x-v1.0.0 -m x "$X10" && git tag -a x-v9.9.9 -m x "$X12")
sync_origin "$R"
run "$R" --ref main
expect_rc "wrong-version tag on a later landing" 2
expect_row "row names the wrong-version tag" x 9.9.9 "$X12" ERROR
if grep -q "x-v9.9.9 peels to ${X12:0:7}" <<< "$OUT"; then pass "message names x-v9.9.9"; else fail "x-v9.9.9 not named"; fi
expect_row "1.1.0 is still wanted (start point is x-v1.0.0)" x 1.1.0 "$X11" MISSING
run "$R" --ref main --apply
expect_rc "--apply blocked by the wrong-version tag" 2
if [[ "$(git -C "$R" tag -l | tr '\n' ' ')" == "x-v1.0.0 x-v9.9.9 " ]]; then pass "nothing created"; else fail "tags created: $(git -C "$R" tag -l | tr '\n' ' ')"; fi
(cd "$R" && git tag -d x-v9.9.9 > /dev/null && git tag x-v1.0.1 "$X12")
run "$R" --ref main
expect_rc "lightweight tag as start point" 2
expect_row "row names the lightweight tag" x 1.0.1 "$X12" ERROR
if grep -q "x-v1.0.1 is a lightweight tag" <<< "$OUT"; then pass "message names x-v1.0.1"; else fail "x-v1.0.1 not named"; fi
expect_row "1.1.0 is still wanted" x 1.1.0 "$X11" MISSING
run "$R" --ref main --apply
expect_rc "--apply blocked by the lightweight tag" 2
echo "Case 12n: --apply only tags history on origin/main"
R="$(new_repo)"
write_apm "$R" wa 1.0.0; commit "$R" a
(cd "$R" && git checkout -q -b feat)
write_apm "$R" wa 1.1.0; commit "$R" b; FEAT="$(head_sha "$R")"
(cd "$R" && git checkout -q main)
run "$R" --ref "$FEAT" --apply
expect_rc "--apply with no origin/main" 2
if [[ -z "$(git -C "$R" tag -l)" ]]; then pass "no tag without origin/main"; else fail "tag created without origin/main"; fi
run "$R" --apply
expect_rc "--apply refuses the local-main fallback" 2
sync_origin "$R"
run "$R" --ref "$FEAT" --apply
expect_rc "--apply --ref <feature-branch commit>" 2
if grep -q "not on origin/main" <<< "$OUT"; then pass "message names origin/main"; else fail "no origin/main message"; fi
if [[ -z "$(git -C "$R" tag -l)" ]]; then pass "no tag on a feature-branch commit"; else fail "tag created off main"; fi
run "$R" --ref "$FEAT"
expect_rc "dry run may still read a feature-branch ref" 1
echo "Case 12o: a plugin name starting with '-' exits 2"
R="$(new_repo)"
write_apm "$R" ok 1.0.0; write_apm "$R" "-x" 1.0.0; commit "$R" a
sync_origin "$R"
run "$R" --ref main --apply
expect_rc "leading-dash plugin" 2
if grep -q "must not start with '-'" <<< "$OUT"; then pass "message names the leading dash"; else fail "no leading-dash message"; fi
if [[ -z "$(git -C "$R" tag -l)" ]]; then pass "nothing created"; else fail "tags created"; fi
echo "Case 12p: expect_row matches a version's dots literally"
OUT="zz 1x0x0 abcdef0 OK"
if ! grep -Eq "^zz +$(ere_quote 1.0.0) +abcdef0 +OK\$" <<< "$OUT"; then pass "1.0.0 does not match 1x0x0"; else fail "unescaped dots"; fi
echo "Case 13: bad input exits 2"
run "$R" --ref no-such-ref
expect_rc "unresolvable --ref" 2