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

+91 -23
View File
@@ -11,9 +11,12 @@ set -euo pipefail
# (no flags) dry run: print the table, create nothing.
# --apply create every MISSING tag locally, then print the exact
# `git push origin <tag>...` line. Pushing is the maintainer's
# step; this script never pushes and never fetches.
# --ref <rev> the history to read (default origin/main, falling back to the
# local `main` with a staleness note).
# step; this script never pushes and never fetches. Requires
# origin/main to resolve and the ref to be on it (an ancestor
# of, or equal to, origin/main); otherwise exit 2.
# --ref <rev> the history to read (default origin/main; a dry run falls
# back to the local `main` with a staleness note, --apply
# never does).
# --plugin <n> limit to these plugins (repeatable; default: every plugin).
#
# Run `git fetch origin --tags` first. The script reads only local refs, so a
@@ -33,12 +36,14 @@ set -euo pipefail
# the current run; it never starts a new one. A run's target is its oldest
# commit: the merge or squash commit on main where the version first
# landed, never a feature-branch commit.
# - Wanted runs: with no `<p>-v<semver>` tag reachable from the ref, only the
# current run (the backfill). Otherwise every run from the newest such tag's
# run onward. A tag's run is the run it targets; a tag targeting no run
# counts as the last run whose target is its ancestor, so a mis-cut tag
# widens the wanted set rather than hiding runs. The anchor run itself is
# re-checked, so a mis-cut tag surfaces as an ERROR.
# - Every `<p>-v<semver>` tag reachable from the ref is validated: it must be
# annotated and peel to a run target carrying its own version. Any other
# such tag is an ERROR row naming it, and it never serves as the start
# point, so a mis-cut tag can neither hide runs nor go unreported.
# - Wanted runs: every run from the newest validly tagged run onward. With
# no valid tag (the backfill), every run from the FIRST landing of the
# current version onward; when the current version never recurred, that is
# the current run alone. Older history is not tagged retroactively.
# - A version is released once, at its FIRST landing. When a version recurs
# (a revert, or apm.yml deleted and re-added), the later run is not tagged
# again: it reports RELEASED.
@@ -47,6 +52,8 @@ set -euo pipefail
# or the version is untagged and an earlier wanted run carries it, so that
# run is the one tagged), ERROR (lightweight, peels to no landing of that
# version, invalid version in history, or a tag name git rejects).
# - A plugin directory whose name starts with `-` is rejected (exit 2): its
# tag name would parse as an option.
# RELEASED never blocks other rows and never changes the exit code.
#
# Tag matching is anchored: `<p>-v` followed by a strict semver and nothing
@@ -59,7 +66,8 @@ set -euo pipefail
#
# Exit: 0 nothing missing (or --apply created all of it); 1 dry run found
# MISSING tags; 2 any ERROR, bad input, shallow clone, unresolvable ref,
# missing python3/PyYAML, or a `git tag` failure during --apply.
# --apply off origin/main, missing python3/PyYAML, or a `git tag` failure
# during --apply.
export LC_ALL=C
@@ -111,6 +119,8 @@ if [[ "$(git rev-parse --is-shallow-repository)" == "true" ]]; then
exit 2
fi
is_ancestor() { git merge-base --is-ancestor "$1" "$2" 2>/dev/null; }
if [[ -n "$REF" ]]; then
if ! REF_COMMIT="$(git rev-parse --verify -q "$REF^{commit}")"; then
echo "FAIL: --ref $REF does not resolve to a commit." >&2
@@ -128,14 +138,28 @@ else
fi
fi
# --apply tags only commits on origin/main: never a feature-branch commit, and
# never the local `main`, which may hold unpushed work.
if $APPLY; then
if ! ORIGIN_MAIN="$(git rev-parse --verify -q "origin/main^{commit}")"; then
echo "FAIL: --apply needs origin/main, which does not resolve; run git fetch origin --tags first." >&2
exit 2
fi
if ! is_ancestor "$REF_COMMIT" "$ORIGIN_MAIN"; then
echo "FAIL: --apply: $REF (${REF_COMMIT:0:7}) is not on origin/main; release tags go only on main's history." >&2
exit 2
fi
fi
# Plugin directories at the ref: trees under plugins/ that carry apm.yml.
# NUL-separated, so unusual names are read verbatim, never quoted.
ALL_PLUGINS=()
while IFS= read -r path; do
while IFS= read -r -d '' path; do
p="${path#plugins/}"
if git cat-file -e "$REF_COMMIT:plugins/$p/apm.yml" 2>/dev/null; then
ALL_PLUGINS+=("$p")
fi
done < <(git ls-tree -d --name-only "$REF_COMMIT" plugins/)
done < <(git ls-tree -z -d --name-only "$REF_COMMIT" plugins/)
if [[ ${#PLUGINS[@]} -eq 0 ]]; then
PLUGINS=(${ALL_PLUGINS[@]+"${ALL_PLUGINS[@]}"})
@@ -157,6 +181,14 @@ if [[ ${#PLUGINS[@]} -eq 0 ]]; then
exit 2
fi
# A leading dash would make the tag name `-x-v1.0.0` parse as an option.
for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
if [[ "$p" == -* ]]; then
echo "ERROR: plugins/$p: a plugin name must not start with '-' (its tag would parse as an option)." >&2
exit 2
fi
done
# Reads "<commit>" lines on stdin and, for plugin path $1, prints one line per
# commit: "<commit> OK <name> <version>", "<commit> INVALID <name|-> -" when
# the version is not strict semver or the YAML does not parse, or
@@ -191,7 +223,7 @@ for c in commits:
name = "-" if name is None else (str(name) or "-")
if re.search(r"\s", name):
name = "<whitespace>"
ver = None if ver is None else str(ver).strip("\x27\"")
ver = None if ver is None else str(ver)
if ver is not None and SEMVER.fullmatch(ver):
print(c, "OK", name, ver)
else:
@@ -202,8 +234,6 @@ p.stdin.close(); p.wait()
SEMVER_RE='^(0|[1-9][0-9]{0,8})\.(0|[1-9][0-9]{0,8})\.(0|[1-9][0-9]{0,8})$'
is_ancestor() { git merge-base --is-ancestor "$1" "$2" 2>/dev/null; }
# Result rows, index-matched (bash 3.2 has no declare -A).
ROW_PLUGIN=(); ROW_VERSION=(); ROW_COMMIT=(); ROW_STATUS=(); ROW_DETAIL=()
add_row() {
@@ -261,26 +291,53 @@ for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
done
nruns=${#RUN_VER[@]}
# Anchor: the newest run a reachable <p>-v<semver> tag belongs to.
# Start point: the newest run a VALID reachable <p>-v<semver> tag peels to.
# Every such tag is validated first: annotated, and peeling to a run target
# carrying its own version. Any other is recorded as bad: it is reported and
# never serves as the start point, so it cannot hide the runs before it.
anchor=-1
BAD_TAG=(); BAD_VER=(); BAD_COMMIT=(); BAD_DETAIL=()
while IFS= read -r tagname; do
[[ "$tagname" == "$p-v"* ]] || continue
[[ "${tagname#"$p-v"}" =~ $SEMVER_RE ]] || continue
peeled="$(git rev-parse --verify -q "refs/tags/$tagname^{commit}")" || continue
tv="${tagname#"$p-v"}"
[[ "$tv" =~ $SEMVER_RE ]] || continue
if ! peeled="$(git rev-parse --verify -q "refs/tags/$tagname^{commit}")"; then
BAD_TAG+=("$tagname"); BAD_VER+=("$tv"); BAD_COMMIT+=("-")
BAD_DETAIL+=("$tagname does not peel to a commit")
continue
fi
is_ancestor "$peeled" "$REF_COMMIT" || continue
if [[ "$(git cat-file -t "refs/tags/$tagname")" != "tag" ]]; then
BAD_TAG+=("$tagname"); BAD_VER+=("$tv"); BAD_COMMIT+=("$peeled")
BAD_DETAIL+=("$tagname is a lightweight tag; it must be annotated")
continue
fi
pos=-1
for ((i = 0; i < nruns; i++)); do
[[ "${RUN_TARGET[i]}" == "$peeled" ]] && pos=$i
if [[ "${RUN_TARGET[i]}" == "$peeled" && "${RUN_VALID[i]}" == true && "${RUN_VER[i]}" == "$tv" ]]; then pos=$i; fi
done
if [[ $pos -lt 0 ]]; then
for ((i = 0; i < nruns; i++)); do
is_ancestor "${RUN_TARGET[i]}" "$peeled" && pos=$i
done
BAD_TAG+=("$tagname"); BAD_VER+=("$tv"); BAD_COMMIT+=("$peeled")
BAD_DETAIL+=("$tagname peels to ${peeled:0:7}, which is no landing of $tv on the first-parent history")
continue
fi
[[ $pos -gt $anchor ]] && anchor=$pos
done < <(git for-each-ref --format='%(refname:strip=2)' "refs/tags/$p-v*")
if [[ $anchor -lt 0 ]]; then first=$((nruns - 1)); else first=$anchor; fi
if [[ $anchor -ge 0 ]]; then
first=$anchor
else
# Backfill: start at the current version's first landing, so it is
# released once, there; a later run of it then reports RELEASED.
cur_ver="${RUN_VER[nruns - 1]}"
first=$((nruns - 1))
for ((i = 0; i < nruns; i++)); do
if [[ "${RUN_VALID[i]}" == true && "${RUN_VER[i]}" == "$cur_ver" ]]; then first=$i; break; fi
done
fi
# Tags the run loop below reports itself, so the bad-tag rows skip them.
REPORTED=()
for ((i = first; i < nruns; i++)); do
v="${RUN_VER[i]}"; target="${RUN_TARGET[i]}"
@@ -309,6 +366,7 @@ for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
fi
if [[ "$(git cat-file -t "refs/tags/$tag")" != "tag" ]]; then
add_row "$p" "$v" "$target" ERROR "$tag is a lightweight tag; it must be annotated"
REPORTED+=("$tag")
continue
fi
peeled="$(git rev-parse --verify -q "refs/tags/$tag^{commit}" || true)"
@@ -327,6 +385,16 @@ for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
continue
fi
add_row "$p" "$v" "$target" ERROR "$tag peels to ${peeled:0:7}, not ${target:0:7} or any earlier landing of $v"
REPORTED+=("$tag")
done
# Bad tags no wanted run reported: one ERROR row each, naming the tag.
for ((k = 0; k < ${#BAD_TAG[@]}; k++)); do
seen=false
for r in ${REPORTED[@]+"${REPORTED[@]}"}; do
[[ "$r" == "${BAD_TAG[k]}" ]] && { seen=true; break; }
done
$seen || add_row "$p" "${BAD_VER[k]}" "${BAD_COMMIT[k]}" ERROR "${BAD_DETAIL[k]}"
done
done