diff --git a/scripts/tag-plugin-releases.sh b/scripts/tag-plugin-releases.sh index 631d7a4..1f26cab 100755 --- a/scripts/tag-plugin-releases.sh +++ b/scripts/tag-plugin-releases.sh @@ -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 ...` 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 ...` 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 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 `

-v` 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 : over the current plugin's runs, print +# the target of the first valid run of before 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

-v 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[*]}" } diff --git a/tests/test-tag-plugin-releases.sh b/tests/test-tag-plugin-releases.sh index 5a5141b..183b09a 100755 --- a/tests/test-tag-plugin-releases.sh +++ b/tests/test-tag-plugin-releases.sh @@ -416,7 +416,7 @@ expect_rc "--apply refuses the local-main fallback" 2 sync_origin "$R" run "$R" --ref "$FEAT" --apply expect_rc "--apply --ref " 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 run "$R" --ref "$FEAT" 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 [[ -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 " 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 " 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" 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