#!/usr/bin/env bash # One local gate to run before a branch is squash-merged into the release branch # or the release PR is opened. Stops at the first failure, always prints a summary. # Usage: scripts/pre-integrate.sh [--web] [--base REF] # Base: --base, else $LEVELRAIL_PUSH_BASE, else the closest origin/release/* merge-base. # Env: PRE_INTEGRATE_NODE_PATH node bin dir (default /opt/homebrew/opt/node@22/bin) # PRE_INTEGRATE_NODE_HEAP MB for tsc (default 8192) # PRE_INTEGRATE_TAIL failure output lines shown (default 40) # PRE_INTEGRATE_MIGRATION_STRICT=1 fail on a number used by another remote branch # Works on bash 3.2 (no mapfile, no associative arrays). set -uo pipefail cd "$(git rev-parse --show-toplevel)" || exit 2 # Shared Go build cache across worktrees: trimpath keeps the package dir out of action IDs. case " ${GOFLAGS:-} " in *" -trimpath "*) ;; *) export GOFLAGS="${GOFLAGS:+$GOFLAGS }-trimpath" ;; esac web=0 base="" while [ $# -gt 0 ]; do case "$1" in --web) web=1 ;; --base) shift; base="${1:-}" ;; *) echo "usage: $0 [--web] [--base REF]" >&2; exit 2 ;; esac shift done node_path="${PRE_INTEGRATE_NODE_PATH:-/opt/homebrew/opt/node@22/bin}" node_heap="${PRE_INTEGRATE_NODE_HEAP:-8192}" tail_n="${PRE_INTEGRATE_TAIL:-40}" [ -d "$node_path" ] && export PATH="$node_path:$PATH" [ -n "$base" ] || base="${LEVELRAIL_PUSH_BASE:-}" if [ -z "$base" ]; then best_n="" for r in $(git for-each-ref --format='%(refname:short)' 'refs/remotes/origin/release/*') origin/main; do mb="$(git merge-base HEAD "$r" 2>/dev/null)" || continue n="$(git rev-list --count "$mb..HEAD")" if [ -z "$best_n" ] || [ "$n" -lt "$best_n" ]; then base="$r" best_n="$n" fi done base="${base:-origin/main}" fi mergebase="$(git merge-base HEAD "$base" 2>/dev/null || echo "$base")" # Changes since the merge-base, committed or not, plus new untracked files. changed="$({ git diff --name-only --diff-filter=ACMR "$mergebase"; git ls-files -o --exclude-standard; } | sort -u)" changed_go="$(printf '%s\n' "$changed" | grep -E '\.go$' || true)" export CHANGED_GO="$changed_go" NODE_HEAP="$node_heap" MIGRATION_STRICT="${PRE_INTEGRATE_MIGRATION_STRICT:-0}" has() { printf '%s\n' "$changed" | grep -Eq "$1"; } log="$(mktemp)" summary="" failed="" trap 'rm -f "$log"' EXIT add() { summary="$summary$(printf '%-40s %-6s %s' "$1" "$2" "$3") "; } # step