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:
1 parent
c8853c7a44
commit
03bacd6e5a
2 files changed
+106
-20
No files matched your search
@@ -10,10 +10,12 @@ set -euo pipefail
|
|||||||
#
|
#
|
||||||
# (no flags) dry run: print the table, create nothing.
|
# (no flags) dry run: print the table, create nothing.
|
||||||
# --apply create every MISSING tag locally, then print the exact
|
# --apply create every MISSING tag locally, then print the exact
|
||||||
# `git push origin <tag>...` line. Pushing is the maintainer's
|
# `git push origin <tag>...` line. This script never pushes and
|
||||||
# step; this script never pushes and never fetches. Requires
|
# never fetches; the push runs only on the maintainer's explicit
|
||||||
# origin/main to resolve and the ref to be on it (an ancestor
|
# yes to that exact line (ADR-0033). Requires origin/main to
|
||||||
# of, or equal to, origin/main); otherwise exit 2.
|
# 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
|
# --ref <rev> the history to read (default origin/main; a dry run falls
|
||||||
# back to the local `main` with a staleness note, --apply
|
# back to the local `main` with a staleness note, --apply
|
||||||
# never does).
|
# never does).
|
||||||
@@ -37,8 +39,9 @@ set -euo pipefail
|
|||||||
# commit: the merge or squash commit on main where the version first
|
# commit: the merge or squash commit on main where the version first
|
||||||
# landed, never a feature-branch commit.
|
# landed, never a feature-branch commit.
|
||||||
# - Every `<p>-v<semver>` tag reachable from the ref is validated: it must be
|
# - 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
|
# annotated and peel to the target of the FIRST run carrying its own
|
||||||
# such tag is an ERROR row naming it, and it never serves as the start
|
# 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.
|
# 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
|
# - 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
|
# 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,
|
# 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
|
# 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
|
# 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
|
# - A plugin directory whose name starts with `-` is rejected (exit 2): its
|
||||||
# tag name would parse as an option.
|
# tag name would parse as an option.
|
||||||
# RELEASED never blocks other rows and never changes the exit code.
|
# 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
|
# 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 tags; 2 any ERROR, bad input, shallow clone, unresolvable ref,
|
||||||
# --apply off origin/main, missing python3/PyYAML, or a `git tag` failure
|
# --apply off origin/main's first-parent history, missing python3/PyYAML, or a
|
||||||
# during --apply.
|
# `git tag` failure during --apply.
|
||||||
|
|
||||||
export LC_ALL=C
|
export LC_ALL=C
|
||||||
|
|
||||||
@@ -89,7 +93,13 @@ while [[ $# -gt 0 ]]; do
|
|||||||
REF="$2"; shift 2 ;;
|
REF="$2"; shift 2 ;;
|
||||||
--plugin)
|
--plugin)
|
||||||
[[ $# -ge 2 && -n "$2" ]] || { usage; exit 2; }
|
[[ $# -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 ;;
|
-h|--help) usage; exit 0 ;;
|
||||||
*) echo "FAIL: unknown argument: $1" >&2; usage; exit 2 ;;
|
*) echo "FAIL: unknown argument: $1" >&2; usage; exit 2 ;;
|
||||||
esac
|
esac
|
||||||
@@ -138,15 +148,19 @@ else
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# --apply tags only commits on origin/main: never a feature-branch commit, and
|
# --apply tags only commits on origin/main's FIRST-PARENT history: never a
|
||||||
# never the local `main`, which may hold unpushed work.
|
# 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 $APPLY; then
|
||||||
if ! ORIGIN_MAIN="$(git rev-parse --verify -q "origin/main^{commit}")"; 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
|
echo "FAIL: --apply needs origin/main, which does not resolve; run git fetch origin --tags first." >&2
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
if ! is_ancestor "$REF_COMMIT" "$ORIGIN_MAIN"; then
|
# grep reads the whole list from a process substitution, so an early match
|
||||||
echo "FAIL: --apply: $REF (${REF_COMMIT:0:7}) is not on origin/main; release tags go only on main's history." >&2
|
# 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
|
exit 2
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@@ -240,15 +254,28 @@ add_row() {
|
|||||||
ROW_PLUGIN+=("$1"); ROW_VERSION+=("$2"); ROW_COMMIT+=("$3"); ROW_STATUS+=("$4"); ROW_DETAIL+=("$5")
|
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
|
for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
|
||||||
apm="plugins/$p/apm.yml"
|
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
|
# 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
|
# whose apm.yml differs from its first parent's: on main that is the merge
|
||||||
# or squash commit that brought the change in.
|
# or squash commit that brought the change in.
|
||||||
COMMITS=()
|
COMMITS=()
|
||||||
while IFS= read -r c; do COMMITS+=("$c"); done \
|
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=()
|
LINES=()
|
||||||
while IFS= read -r l; do LINES+=("$l"); done \
|
while IFS= read -r l; do LINES+=("$l"); done \
|
||||||
@@ -292,9 +319,10 @@ for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
|
|||||||
nruns=${#RUN_VER[@]}
|
nruns=${#RUN_VER[@]}
|
||||||
|
|
||||||
# Start point: the newest run a VALID reachable <p>-v<semver> tag peels 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
|
# Every such tag is validated first: annotated, and peeling to the target of
|
||||||
# carrying its own version. Any other is recorded as bad: it is reported and
|
# the FIRST run carrying its own version (a version is released once, at its
|
||||||
# never serves as the start point, so it cannot hide the runs before it.
|
# 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
|
anchor=-1
|
||||||
BAD_TAG=(); BAD_VER=(); BAD_COMMIT=(); BAD_DETAIL=()
|
BAD_TAG=(); BAD_VER=(); BAD_COMMIT=(); BAD_DETAIL=()
|
||||||
while IFS= read -r tagname; do
|
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")
|
BAD_DETAIL+=("$tagname peels to ${peeled:0:7}, which is no landing of $tv on the first-parent history")
|
||||||
continue
|
continue
|
||||||
fi
|
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
|
[[ $pos -gt $anchor ]] && anchor=$pos
|
||||||
done < <(git for-each-ref --format='%(refname:strip=2)' "refs/tags/$p-v*")
|
done < <(git for-each-ref --format='%(refname:strip=2)' "refs/tags/$p-v*")
|
||||||
|
|
||||||
@@ -371,6 +404,11 @@ for p in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
|
|||||||
fi
|
fi
|
||||||
peeled="$(git rev-parse --verify -q "refs/tags/$tag^{commit}" || true)"
|
peeled="$(git rev-parse --verify -q "refs/tags/$tag^{commit}" || true)"
|
||||||
if [[ "$peeled" == "$target" ]]; then
|
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 ""
|
add_row "$p" "$v" "$target" OK ""
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
@@ -452,7 +490,7 @@ fi
|
|||||||
CREATED=()
|
CREATED=()
|
||||||
print_push() {
|
print_push() {
|
||||||
[[ ${#CREATED[@]} -gt 0 ]] || return 0
|
[[ ${#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[*]}"
|
echo "git push origin ${CREATED[*]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -416,7 +416,7 @@ expect_rc "--apply refuses the local-main fallback" 2
|
|||||||
sync_origin "$R"
|
sync_origin "$R"
|
||||||
run "$R" --ref "$FEAT" --apply
|
run "$R" --ref "$FEAT" --apply
|
||||||
expect_rc "--apply --ref <feature-branch commit>" 2
|
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 grep -q "not on origin/main's first-parent history" <<< "$OUT"; then pass "message names origin/main's first-parent history"; 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
|
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"
|
run "$R" --ref "$FEAT"
|
||||||
expect_rc "dry run may still read a feature-branch ref" 1
|
expect_rc "dry run may still read a feature-branch ref" 1
|
||||||
@@ -430,6 +430,54 @@ 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 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
|
if [[ -z "$(git -C "$R" tag -l)" ]]; then pass "nothing created"; else fail "tags created"; fi
|
||||||
|
|
||||||
|
echo "Case 12q: --apply refuses a commit of an already-merged feature branch"
|
||||||
|
R="$(new_repo)"
|
||||||
|
write_apm "$R" ya 1.0.0; commit "$R" a
|
||||||
|
(cd "$R" && git checkout -q -b feat)
|
||||||
|
write_apm "$R" ya 1.1.0; commit "$R" b; FEAT="$(head_sha "$R")"
|
||||||
|
(cd "$R" && git checkout -q main && git merge -q --no-ff -m "merge feat" feat)
|
||||||
|
MERGED="$(head_sha "$R")"
|
||||||
|
sync_origin "$R"
|
||||||
|
run "$R" --ref "$FEAT" --apply
|
||||||
|
expect_rc "--apply --ref <merged feature commit>" 2
|
||||||
|
if grep -q "not on origin/main's first-parent history" <<< "$OUT"; then pass "message names the first-parent history"; else fail "no first-parent message"; fi
|
||||||
|
if [[ -z "$(git -C "$R" tag -l)" ]]; then pass "no tag on the merged feature commit"; else fail "tags created: $(git -C "$R" tag -l | tr '\n' ' ')"; fi
|
||||||
|
run "$R" --ref "$MERGED" --apply
|
||||||
|
expect_rc "--apply --ref <the merge commit>" 0
|
||||||
|
if [[ "$(git -C "$R" rev-parse "ya-v1.1.0^{commit}" 2>/dev/null)" == "$MERGED" ]]; then pass "ya-v1.1.0 on the merge commit"; else fail "ya-v1.1.0 misplaced"; fi
|
||||||
|
|
||||||
|
echo "Case 12r: a tag on a later run of a recurring version is an ERROR, not a start point"
|
||||||
|
R="$(new_repo)"
|
||||||
|
write_apm "$R" za 1.0.0; commit "$R" a; Z1="$(head_sha "$R")"
|
||||||
|
write_apm "$R" za 1.1.0; commit "$R" b; Z2="$(head_sha "$R")"
|
||||||
|
write_apm "$R" za 1.0.0; commit "$R" revert; Z3="$(head_sha "$R")"
|
||||||
|
write_apm "$R" za 1.2.0; commit "$R" c
|
||||||
|
(cd "$R" && git tag -a za-v1.0.0 -m x "$Z3")
|
||||||
|
sync_origin "$R"
|
||||||
|
run "$R" --ref main
|
||||||
|
expect_rc "tag on a later landing" 2
|
||||||
|
expect_row "row names the later-landing tag" za 1.0.0 "$Z3" ERROR
|
||||||
|
if grep -q "za-v1.0.0 peels to ${Z3:0:7}, a later landing of 1.0.0; it first landed at ${Z1:0:7}" <<< "$OUT"; then pass "message names the first landing"; else fail "no later-landing message"; fi
|
||||||
|
if ! grep -q "${Z2:0:7}.*OK" <<< "$OUT"; then pass "1.1.0 is not hidden behind the bad tag as OK"; else fail "1.1.0 reported OK"; fi
|
||||||
|
run "$R" --ref main --apply
|
||||||
|
expect_rc "--apply blocked by the later-landing tag" 2
|
||||||
|
if [[ "$(git -C "$R" tag -l)" == "za-v1.0.0" ]]; then pass "nothing created"; else fail "tags created: $(git -C "$R" tag -l | tr '\n' ' ')"; fi
|
||||||
|
(cd "$R" && git tag -d za-v1.0.0 > /dev/null && git tag -a za-v1.0.0 -m x "$Z1")
|
||||||
|
run "$R" --ref main
|
||||||
|
expect_rc "tag moved to the first landing" 1
|
||||||
|
expect_row "first landing OK" za 1.0.0 "$Z1" OK
|
||||||
|
expect_row "1.1.0 wanted again" za 1.1.0 "$Z2" MISSING
|
||||||
|
expect_row "later landing RELEASED" za 1.0.0 "$Z3" RELEASED
|
||||||
|
|
||||||
|
echo "Case 12s: a repeated --plugin is de-duplicated"
|
||||||
|
R="$(new_repo)"
|
||||||
|
write_apm "$R" git 1.0.0; write_apm "$R" gitea 1.0.0; commit "$R" a
|
||||||
|
sync_origin "$R"
|
||||||
|
run "$R" --ref main --plugin git --plugin git --apply
|
||||||
|
expect_rc "--plugin git --plugin git --apply" 0
|
||||||
|
if [[ "$(git -C "$R" tag -l)" == "git-v1.0.0" ]]; then pass "one tag created"; else fail "wrong tags: $(git -C "$R" tag -l | tr '\n' ' ')"; fi
|
||||||
|
if grep -qx "git push origin git-v1.0.0" <<< "$OUT"; then pass "push line names the tag once"; else fail "push line wrong"; echo "$OUT" | sed 's/^/ /'; fi
|
||||||
|
|
||||||
echo "Case 12p: expect_row matches a version's dots literally"
|
echo "Case 12p: expect_row matches a version's dots literally"
|
||||||
OUT="zz 1x0x0 abcdef0 OK"
|
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
|
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
|
||||||
|
|||||||
Reference in new issue
Block a user