#!/usr/bin/env bash # Make a SHALLOW checkout deep enough for an honest three-dot diff. # # scripts/ci/ensure-merge-base.sh [--event ] [max-depth] # # THERE IS NO MERGE BASE ON A PUSH, AND NOTHING READS ONE. `verify-main.yml` also # runs on pushes to `main` — a cache-warming run, not a gate — and on that event # `github.base_ref` is EMPTY, so every job called this script with `origin/`. # The script did what it is built to do: deepened to the ceiling, unshallowed, # and failed loudly that `'origin/'` and HEAD share no merge base. Every push to # main was red for it, which is worse than useless — a permanently red main # teaches everyone to stop reading main's checkmark, and the next real breakage # arrives on a bar nobody looks at. # # The fix is not a fallback base (that would silently diff against a guess). It # is that the gate this script feeds ALREADY knows a push reads no diff: # `ci-detect-changes.sh --event push` short-circuits to a fixed answer before # `git diff` is reached. So `--event` is passed to BOTH, and on a push this one # exits 0 having fetched nothing — the two scripts agree about the same event # rather than one of them working on a ref the other will never use. # # WHY. Every job in `verify-main.yml` decides what to run from # `git diff --name-only "origin/...HEAD"` — three dots, so the answer is # "everything this branch changed" and an empty re-trigger commit still reports # the branch's real change set (`ci-detect-changes.sh` carries that reasoning). # Three dots needs the MERGE BASE, and the cheap way to guarantee one is # `fetch-depth: 0`: a full clone. # # That is 122 MB of pack, FOUR TIMES per push — `checks` on Linux, `rust` on # Linux, `swift` and `ios` on the Macs — and not one of those jobs reads history # for anything else. What they need is a merge base, which on this repo is a few # commits back: `main` takes squash merges only, and a branch lives for one PR. # # So the checkout is shallow and this deepens it until `git merge-base` answers, # rather than guessing a depth that is right until the day it is not. THE # FAILURE MODE THIS EXISTS TO PREVENT is not slowness — it is a `git diff` that # silently reports the wrong file list on a long branch, which lands as a gate # answering `false` for a job that needed to run. So: # # * it fetches the base ref explicitly (a shallow PR checkout does not carry # the base branch at all); # * it DOUBLES the depth until a merge base exists, and the last attempt is # `--unshallow` — a full clone, the old behaviour, reached only when the # branch really is that long; # * and it EXITS NON-ZERO, loudly, if there is still no merge base. A caller # that ran the three-dot diff anyway would get `fatal: no merge base` from # git, which is at least loud; a caller that fell back to two dots would get # a plausible wrong answer, which is the one outcome nothing can catch. # # WHICH JOBS KEEP `fetch-depth: 0`, AND WHY. `build-main.yml`'s `gate` job # walks main backwards by TIME (`git rev-list -1 --before=...`), and # `rust-deep.yml` diffs from `github.event.before`; neither is a merge base, # and no deepening rule expressed in commits can bound "25 hours of main". # Those two stay full and say so at the checkout step. (`build-prod-dryrun.yml` # had such a gate too, inverted from the nightly TestFlight cron; both went on # 28 Sep 2026 and the dry-run reads HEAD alone.) # # Idempotent, and free on a checkout that is already complete: a non-shallow # clone that can already answer takes the first branch and fetches nothing. # # Guard: the ci-shallow-checkout test. set -euo pipefail EVENT="" positional=() while [ $# -gt 0 ]; do case "$1" in --event) EVENT="${2-}"; shift 2 ;; --event=*) EVENT="${1#--event=}"; shift ;; *) positional+=("$1"); shift ;; esac done set -- ${positional[@]+"${positional[@]}"} BASE="${1-}" MAX_DEPTH="${2:-2000}" # A push has no base ref and its gate reads no diff — see the block at the top. # Checked BEFORE the base is validated, because on a push there is deliberately # nothing there to validate. if [ "$EVENT" = push ]; then echo "ensure-merge-base: event is 'push' — no base branch, and ci-detect-changes.sh reads no diff on this event; nothing to deepen" exit 0 fi if [ -z "$BASE" ]; then echo "ensure-merge-base: no base ref given — refusing to guess" >&2 echo "usage: ensure-merge-base.sh [--event ] [max-depth]" >&2 exit 2 fi # `origin/main` names a remote-tracking ref; the fetch below wants the BRANCH, # and it has to be asked for with an explicit refspec. # # `actions/checkout` leaves a SINGLE-BRANCH clone: `remote.origin.fetch` is # `+refs/heads/:refs/remotes/origin/` and nothing else. A bare # `git fetch origin main` there downloads the objects and creates NO # `origin/main`, so `git merge-base origin/main HEAD` keeps failing while every # fetch reports success — the exact shape of a loop that runs to its ceiling and # then declares no merge base exists. Naming both sides of the refspec is what # makes the remote-tracking ref appear. REMOTE_REF="${BASE#origin/}" REFSPEC="+refs/heads/$REMOTE_REF:refs/remotes/origin/$REMOTE_REF" have_base() { git merge-base "$BASE" HEAD >/dev/null 2>&1 } if have_base; then echo "ensure-merge-base: $BASE and HEAD already share a merge base" exit 0 fi depth=50 while [ "$depth" -le "$MAX_DEPTH" ]; do echo "ensure-merge-base: no merge base yet — deepening to $depth" git fetch --no-tags --depth="$depth" origin "$REFSPEC" >/dev/null 2>&1 || true # The head side has to grow too: a shallow PR checkout truncates both, and a # merge base needs history on BOTH sides to be reachable. git fetch --no-tags --deepen="$depth" origin >/dev/null 2>&1 || true if have_base; then echo "ensure-merge-base: $BASE and HEAD share a merge base at depth $depth" exit 0 fi depth=$(( depth * 2 )) done echo "ensure-merge-base: no merge base within $MAX_DEPTH commits — unshallowing" git fetch --no-tags --unshallow origin "$REFSPEC" >/dev/null 2>&1 || true git fetch --no-tags origin "$REFSPEC" >/dev/null 2>&1 || true # And the HEAD side, which `--unshallow` above only reaches when the clone still # has a shallow file; a clone deepened past its own root is no longer shallow. git fetch --no-tags --unshallow origin >/dev/null 2>&1 || true if have_base; then echo "ensure-merge-base: $BASE and HEAD share a merge base after a full fetch" exit 0 fi echo "::error::ensure-merge-base: '$BASE' and HEAD share no merge base even after a full fetch." >&2 echo "The three-dot diff every relevance gate reads cannot be computed, and a two-dot fallback would answer a DIFFERENT question plausibly. Check that '$BASE' is really this PR's base branch." >&2 exit 1