fix(scripts): gate --apply on main's first-parent line and reject later-landing tags

Why: a commit of an already-merged feature branch is an ancestor of
origin/main, so the ancestor check let --apply walk that branch's
first-parent history and tag a feature commit (git-v1.3.9 on 025ad4a).
A valid annotated tag on a later run of a recurring version was also
accepted as the start point, hiding the versions between its first
landing and that run.

Implementation Notes:
- --apply now requires the ref on origin/main's first-parent list;
  otherwise exit 2 and no tags.
- A tag must peel to the FIRST run of its version; one on a later run
  is an ERROR row and never the start point.
- apm.yml pathspecs use :(literal), so a plugin name is never a glob.
- Repeated --plugin values are de-duplicated (bash 3.2-safe).
- The printed push hint says the push runs only on the maintainer's
  explicit yes to that exact line.

Impact: tests cover all but the literal pathspec, which has no
observable effect on run grouping today and is defensive.

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:34:20 +00:00
1 parent c8853c7a44
commit 03bacd6e5a
2 files changed
+106 -20

No files matched your search

+57 -19
View File
@@ -10,10 +10,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. Requires
# origin/main to resolve and the ref to be on it (an ancestor
# of, or equal to, origin/main); otherwise exit 2.
# `git push origin <tag>...` line. This script never pushes and
# never fetches; the push runs only on the maintainer's explicit
# yes to that exact line (ADR-0033). Requires origin/main to
# resolve and the ref to be on origin/main's FIRST-PARENT
# history (a commit of a merged feature branch is not);
# 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).
@@ -37,8 +39,9 @@ set -euo pipefail
# commit: the merge or squash commit on main where the version first
# landed, never a feature-branch commit.
# - 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
# annotated and peel to the target of the FIRST run carrying its own
# version. Any other such tag (including one on a later run of a recurring
# version) 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
@@ -51,7 +54,8 @@ set -euo pipefail
# 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, invalid version in history, or a tag name git rejects).
# 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.
# RELEASED never blocks other rows and never changes the exit code.
@@ -66,8 +70,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,
# --apply off origin/main, missing python3/PyYAML, or a `git tag` failure
# during --apply.
# --apply off origin/main's first-parent history, missing python3/PyYAML, or a
# `git tag` failure during --apply.
export LC_ALL=C
@@ -89,7 +93,13 @@ while [[ $# -gt 0 ]]; do
REF="$2"; shift 2 ;;
--plugin)
[[ $# -ge 2 && -n "$2" ]] || { usage; exit 2; }
PLUGINS+=("$2"); shift 2 ;;
# A repeat would make --apply try to create the same tag twice.
dup=false
for q in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
[[ "$q" == "$2" ]] && { dup=true; break; }
done
$dup || PLUGINS+=("$2")
shift 2 ;;
-h|--help) usage; exit 0 ;;
*) echo "FAIL: unknown argument: $1" >&2; usage; exit 2 ;;
esac
@@ -138,15 +148,19 @@ 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.
# --apply tags only commits on origin/main's FIRST-PARENT history: never a
# feature-branch commit (not even one of a branch already merged, which is an
# ancestor of origin/main but not on its first-parent line), 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
# grep reads the whole list from a process substitution, so an early match
# cannot fail the pipeline under pipefail.
if ! grep -qxF "$REF_COMMIT" < <(git rev-list --first-parent "$ORIGIN_MAIN"); then
echo "FAIL: --apply: $REF (${REF_COMMIT:0:7}) is not on origin/main's first-parent history; release tags go only on main's own commits." >&2
exit 2
fi
fi
@@ -240,15 +254,28 @@ 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() {
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
done
return 1
}
for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
apm="plugins/$p/apm.yml"
# Literal pathspec: a plugin name is never read as a glob or pathspec magic.
apm_spec=":(literal)$apm"
# 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.
COMMITS=()
while IFS= read -r c; do COMMITS+=("$c"); done \
< <(git rev-list --first-parent --reverse "$REF_COMMIT" -- "$apm")
< <(git rev-list --first-parent --reverse "$REF_COMMIT" -- "$apm_spec")
LINES=()
while IFS= read -r l; do LINES+=("$l"); done \
@@ -292,9 +319,10 @@ for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
nruns=${#RUN_VER[@]}
# 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.
# Every such tag is validated first: annotated, and peeling to the target of
# the FIRST run carrying its own version (a version is released once, at its
# first landing). 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
@@ -321,6 +349,11 @@ 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
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*")
@@ -371,6 +404,11 @@ 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
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
fi
add_row "$p" "$v" "$target" OK ""
continue
fi
@@ -452,7 +490,7 @@ fi
CREATED=()
print_push() {
[[ ${#CREATED[@]} -gt 0 ]] || return 0
echo "Created ${#CREATED[@]} tag(s) locally. Push them (maintainer's step, ADR-0033):"
echo "Created ${#CREATED[@]} tag(s) locally. Push them with exactly this line, only on the maintainer's explicit yes (ADR-0033):"
echo "git push origin ${CREATED[*]}"
}