fix(scripts): use one whole-history first landing for release tags

Why: the untagged-run branch looked for an earlier run of a version only
from the start of the wanted range, while tag validation used the first
landing across all history. After a backfill, a revert to a pre-backfill
version was tagged on the revert commit, and the next dry run reported
that tag as a later landing: exit 2, blocking --apply for every plugin.

Implementation Notes:
- first_run() returns the first valid run of a version over the whole
  first-parent history; tag validation, the tagged-elsewhere check, the
  backfill start and the untagged-run check all use it.
- An untagged version whose first landing lies before the wanted range
  reports RELEASED with a "before the tagged range; not tagged" note; no
  tag is created and the exit code is unaffected.
- Preflight rejects a plugin name whose tag git check-ref-format refuses
  (e.g. a..b, a*) before any history is read, so a glob character never
  reaches a pathspec or ref pattern; tags are listed once and filtered by
  exact prefix instead of a for-each-ref glob.
- git rev-list failure and an empty run list now exit 2 with a FAIL
  message instead of being read as no history or crashing.
- The error footer addresses agents (stop and report, AGENTS.md) and the
  maintainer (fix by hand, ADR-0033) separately.

Impact: ADR-0033 defines first landing precisely, documents the preflight,
and documents the maintainer escape from an INVALID run (hand-cut tag on
the next valid version; --apply stays all-or-nothing). New tests cover the
revert repro, a backfill range holding a pre-range version, the INVALID
escape, rev-list failure/empty history, a glob plugin name, and the footer.

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 19:25:16 +00:00
1 parent 0a52c8597d
commit 62650543ec
3 files changed
+219 -52

No files matched your search

+89 -46
View File
@@ -47,18 +47,29 @@ set -euo pipefail
# 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.
# - A version is released once, at its FIRST landing: the oldest valid run
# of that version anywhere in the ref's first-parent history. Validating
# existing tags and choosing what to create use this one definition. When
# a version recurs (a revert, or apm.yml deleted and re-added), the later
# run is never tagged: it reports RELEASED. If the version is untagged and
# its first landing lies before the wanted runs, it shipped before the
# tagging start point: the later run still reports RELEASED (with a note
# naming the first landing) and no tag is created for it.
# - Status per wanted run: MISSING (no tag), OK (annotated, peels to the
# target), RELEASED (the tag peels to an earlier run of the same version,
# 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 or to a later landing than its first, 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.
# target), RELEASED (a later run of a version whose first landing is
# earlier, tagged or not), ERROR (lightweight, peels to anything but the
# version's first landing, or an invalid version in the wanted runs).
# RELEASED never blocks other rows and never changes the exit code.
# - An INVALID run inside the wanted runs is an ERROR, so --apply creates
# nothing for any plugin until it is resolved. The way out is the
# maintainer's (ADR-0033): cut by hand the annotated tag of the first valid
# version after the invalid run, on that version's first landing; it
# becomes the start point and the invalid run is no longer wanted.
#
# Preflight (exit 2 before any history is read): a selected plugin directory
# whose name starts with `-` (its tag would parse as an option) or whose
# `<p>-v<version>` git check-ref-format rejects (e.g. `a..b`, `a*`): no such
# tag can ever exist, so the plugin can never be released under this scheme.
#
# Tag matching is anchored: `<p>-v` followed by a strict semver and nothing
# else, so git, gitea and gitops never claim each other's tags.
@@ -195,14 +206,34 @@ if [[ ${#PLUGINS[@]} -eq 0 ]]; then
exit 2
fi
# A leading dash would make the tag name `-x-v1.0.0` parse as an option.
# A leading dash would make the tag name `-x-v1.0.0` parse as an option. A
# name git rejects as a ref (`..`, `*`, `[`, `?`, `\`, ...) can never be
# tagged. Both are rejected before any history is read, so such a name never
# reaches a pathspec or a ref pattern. The version part is strict semver
# (digits and single dots), so the name alone decides validity.
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
if ! git check-ref-format "refs/tags/$p-v0.0.0"; then
echo "ERROR: plugins/$p: '$p-v<version>' is not a valid tag name (git check-ref-format); rename the plugin directory." >&2
exit 2
fi
done
# Every tag, listed once. A plugin's tags are picked out below by an exact
# string prefix in the shell, never by a for-each-ref pattern, so nothing in a
# plugin name is ever read as a glob.
if ! tag_list="$(git for-each-ref --format='%(refname:strip=2)' refs/tags/)"; then
echo "FAIL: git for-each-ref could not list tags." >&2
exit 2
fi
ALL_TAGS=()
if [[ -n "$tag_list" ]]; then
while IFS= read -r t; do ALL_TAGS+=("$t"); done <<< "$tag_list"
fi
# 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
@@ -254,13 +285,16 @@ add_row() {
ROW_PLUGIN+=("$1"); ROW_VERSION+=("$2"); ROW_COMMIT+=("$3"); ROW_STATUS+=("$4"); ROW_DETAIL+=("$5")
}
# first_landing <version> <run index>: over the current plugin's runs, print
# the target of the first valid run of <version> before <run index> and
# succeed, or fail when that run is itself the version's first landing.
first_landing() {
# first_run <version> <run index>: over ALL the current plugin's runs (the
# whole first-parent history, not only the wanted ones), print the index of the
# first valid run of <version> before <run index> and succeed, or fail when
# there is none (that run is itself the version's first landing). Every "where
# did this version first land" question, whether validating a tag or deciding
# what to create, goes through this one function.
first_run() {
local j
for ((j = 0; j < $2; j++)); do
if [[ "${RUN_VALID[j]}" == true && "${RUN_VER[j]}" == "$1" ]]; then echo "${RUN_TARGET[j]}"; return 0; fi
if [[ "${RUN_VALID[j]}" == true && "${RUN_VER[j]}" == "$1" ]]; then echo "$j"; return 0; fi
done
return 1
}
@@ -273,9 +307,15 @@ for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
# Oldest first. --first-parent with a path keeps each first-parent commit
# whose apm.yml differs from its first parent's: on main that is the merge
# or squash commit that brought the change in.
# Captured first, so a git failure is caught rather than read as no history.
if ! revs="$(git rev-list --first-parent --reverse "$REF_COMMIT" -- "$apm_spec")"; then
echo "FAIL: git rev-list could not read $apm history at $REF (see error above)." >&2
exit 2
fi
COMMITS=()
while IFS= read -r c; do COMMITS+=("$c"); done \
< <(git rev-list --first-parent --reverse "$REF_COMMIT" -- "$apm_spec")
if [[ -n "$revs" ]]; then
while IFS= read -r c; do COMMITS+=("$c"); done <<< "$revs"
fi
LINES=()
while IFS= read -r l; do LINES+=("$l"); done \
@@ -317,6 +357,12 @@ for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
fi
done
nruns=${#RUN_VER[@]}
# apm.yml exists at the ref, so its history has at least one run; none means
# the history read is wrong, and every rule below would be meaningless.
if [[ $nruns -eq 0 ]]; then
echo "FAIL: $apm exists at $REF but no commit on its first-parent history carries it; cannot compute release targets." >&2
exit 2
fi
# Start point: the newest run a VALID reachable <p>-v<semver> tag peels to.
# Every such tag is validated first: annotated, and peeling to the target of
@@ -325,7 +371,7 @@ for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
# 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
for tagname in ${ALL_TAGS[@]+"${ALL_TAGS[@]}"}; do
[[ "$tagname" == "$p-v"* ]] || continue
tv="${tagname#"$p-v"}"
[[ "$tv" =~ $SEMVER_RE ]] || continue
@@ -349,13 +395,14 @@ for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
BAD_DETAIL+=("$tagname peels to ${peeled:0:7}, which is no landing of $tv on the first-parent history")
continue
fi
if fl="$(first_landing "$tv" "$pos")"; then
if fj="$(first_run "$tv" "$pos")"; then
fl="${RUN_TARGET[fj]}"
BAD_TAG+=("$tagname"); BAD_VER+=("$tv"); BAD_COMMIT+=("$peeled")
BAD_DETAIL+=("$tagname peels to ${peeled:0:7}, a later landing of $tv; it first landed at ${fl:0:7}")
continue
fi
[[ $pos -gt $anchor ]] && anchor=$pos
done < <(git for-each-ref --format='%(refname:strip=2)' "refs/tags/$p-v*")
done
if [[ $anchor -ge 0 ]]; then
first=$anchor
@@ -364,9 +411,7 @@ for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
# 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
if fj="$(first_run "$cur_ver" "$nruns")"; then first=$fj; fi
fi
# Tags the run loop below reports itself, so the bad-tag rows skip them.
@@ -380,18 +425,18 @@ for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
fi
tag="$p-v$v"
if ! git rev-parse --verify -q "refs/tags/$tag" > /dev/null; then
# Untagged: the version is released at its first landing in the wanted
# set, so a later run of the same version is not tagged again.
earlier=""
for ((j = first; j < i; j++)); do
if [[ "${RUN_VALID[j]}" == true && "${RUN_VER[j]}" == "$v" ]]; then earlier="${RUN_TARGET[j]}"; break; fi
done
if [[ -n "$earlier" ]]; then
add_row "$p" "$v" "$target" RELEASED "$v is released at its first landing ${earlier:0:7}"
continue
fi
if ! git check-ref-format "refs/tags/$tag"; then
add_row "$p" "$v" "$target" ERROR "'$tag' is not a valid tag name (git check-ref-format)"
# Untagged. Only the version's first landing anywhere in history is
# ever tagged. Inside the wanted runs, that run is MISSING and this later
# one is RELEASED. Before them, the version shipped before the tagging
# start point: it stays untagged like the rest of that history, and
# tagging this later run would only be reported as a later landing.
if fj="$(first_run "$v" "$i")"; then
fl="${RUN_TARGET[fj]}"
if [[ $fj -lt $first ]]; then
add_row "$p" "$v" "$target" RELEASED "$v first landed at ${fl:0:7}, before the tagged range; not tagged"
else
add_row "$p" "$v" "$target" RELEASED "$v is released at its first landing ${fl:0:7}"
fi
continue
fi
add_row "$p" "$v" "$target" MISSING ""
@@ -404,7 +449,8 @@ for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
fi
peeled="$(git rev-parse --verify -q "refs/tags/$tag^{commit}" || true)"
if [[ "$peeled" == "$target" ]]; then
if fl="$(first_landing "$v" "$i")"; then
if fj="$(first_run "$v" "$i")"; then
fl="${RUN_TARGET[fj]}"
add_row "$p" "$v" "$target" ERROR "$tag peels to ${target:0:7}, a later landing of $v; it first landed at ${fl:0:7}"
REPORTED+=("$tag")
continue
@@ -412,17 +458,13 @@ for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
add_row "$p" "$v" "$target" OK ""
continue
fi
# Tagged elsewhere: fine only if it peels to an EARLIER run of the same
# version (the first landing, e.g. before a revert brought $v back).
landing=-1
for ((j = 0; j < i; j++)); do
if [[ "${RUN_TARGET[j]}" == "$peeled" && "${RUN_VALID[j]}" == true && "${RUN_VER[j]}" == "$v" ]]; then landing=$j; fi
done
if [[ $landing -ge 0 ]]; then
# Tagged elsewhere: fine only if it peels to the version's FIRST landing
# (e.g. before a revert brought $v back).
if fj="$(first_run "$v" "$i")" && [[ "${RUN_TARGET[fj]}" == "$peeled" ]]; then
add_row "$p" "$v" "$target" RELEASED "$tag already released at ${peeled:0:7}"
continue
fi
add_row "$p" "$v" "$target" ERROR "$tag peels to ${peeled:0:7}, not ${target:0:7} or any earlier landing of $v"
add_row "$p" "$v" "$target" ERROR "$tag peels to ${peeled:0:7}, which is not the first landing of $v"
REPORTED+=("$tag")
done
@@ -465,7 +507,8 @@ report_errors() {
for ((i = 0; i < ${#ROW_STATUS[@]}; i++)); do
[[ "${ROW_STATUS[i]}" == ERROR ]] && echo "ERROR: ${ROW_PLUGIN[i]}: ${ROW_DETAIL[i]}" >&2
done
echo "Tags are immutable: fix by hand only after deciding which commit each version belongs to (ADR-0033)." >&2
echo "Agents: stop and report this to the maintainer; never hand-fix, move or delete a tag (AGENTS.md)." >&2
echo "Maintainer: tags are immutable; fix by hand only after deciding which commit each version belongs to (ADR-0033)." >&2
}
if ! $APPLY; then