diff --git a/scripts/check-attribution-trailers.sh b/scripts/check-attribution-trailers.sh index 227139c6..9f5032f9 100755 --- a/scripts/check-attribution-trailers.sh +++ b/scripts/check-attribution-trailers.sh @@ -16,52 +16,22 @@ set -eu trailer_re='^[[:space:]]*co-authored-by:.*(claude|anthropic|noreply@anthropic\.com)' -branch="${GITHUB_BASE_REF:-main}" - -# Remote name varies by checkout — CI's actions/checkout@v3 names it -# `origin`, but an agent worktree renames its remote to `forge` (see the -# hive's subagent briefs). Hardcoding `origin/` here made the script -# unresolvable, and unresolvable, in the old code, silently read as clean -# (fetch-or-`true`, then a fallback to `HEAD~10`) — that fail-open was the -# actual bug. So: try `BASE_REF` if the caller set one, then every -# configured remote in a stable order (an `origin` remote first, to match -# CI, then the rest alphabetically), fetching a remote's copy of `branch` -# if it isn't already present locally. If none of that resolves a base, -# exit 1 with a clear message — never guess a commit range. -base="" -if [ -n "${BASE_REF:-}" ]; then - if git rev-parse --verify "$BASE_REF" >/dev/null 2>&1; then - base="$BASE_REF" - else - echo "check-attribution-trailers: BASE_REF=$BASE_REF does not resolve" >&2 - exit 1 - fi +if [ -n "${GITHUB_BASE_REF:-}" ]; then + base="origin/${GITHUB_BASE_REF}" else - remotes="$(git remote)" - ordered_remotes="" - if printf '%s\n' "$remotes" | grep -qx origin; then - ordered_remotes="origin" - fi - for r in $(printf '%s\n' "$remotes" | sort); do - [ "$r" = "origin" ] && continue - ordered_remotes="${ordered_remotes:+$ordered_remotes }$r" - done + base="origin/main" +fi - for remote in $ordered_remotes; do - candidate="refs/remotes/${remote}/${branch}" - if ! git rev-parse --verify "$candidate" >/dev/null 2>&1; then - # actions/checkout@v3 fetches the PR head but may not fetch the base branch - git fetch "$remote" "$branch" 2>/dev/null || true +# actions/checkout@v3 fetches the PR head but may not fetch the base branch +if ! git rev-parse --verify "$base" >/dev/null 2>&1; then + branch="${base#origin/}" + git fetch origin "$branch" 2>/dev/null || true + if ! git rev-parse --verify "$base" >/dev/null 2>&1; then + base="origin/main" + if ! git fetch origin main 2>/dev/null || ! git rev-parse --verify "$base" >/dev/null 2>&1; then + echo 'check-attribution-trailers: base ref unresolvable — cannot fetch origin/main to diff against, refusing to guess a commit range' >&2 + exit 1 fi - if git rev-parse --verify "$candidate" >/dev/null 2>&1; then - base="$candidate" - break - fi - done - - if [ -z "$base" ]; then - echo "check-attribution-trailers: base ref unresolvable — no configured remote has a '${branch}' ref (tried: ${remotes:-none}), refusing to guess a commit range" >&2 - exit 1 fi fi