fix(push-script): fetch PR HEAD sha before git worktree add (closes #966)#972
Open
fix(push-script): fetch PR HEAD sha before git worktree add (closes #966)#972
git worktree add (closes #966)#972Conversation
#966) Empirical hit on PR #950 run 24927483127: rebuild-stale-arm64 failed immediately with "fatal: invalid reference: <sha>" after correctly resolving STARTUP_SHA_FULL via .pull_request.head.sha. Root cause: actions/checkout@v4 with default settings on a pull_request event fetches refs/pull/<N>/merge as a shallow clone. The PR head sha is a known remote ref but is NOT in the local object store, so \`git worktree add --detach <DIR> <SHA>\` fails before it can build. Fix: gate \`git worktree add\` on \`git cat-file -e\` and fetch the missing commit if needed. Falls through full-history fetch when shallow fetch is rejected. Dev-machine path unchanged — cat-file -e always succeeds on local HEAD. Why amd64 didn't trip the bug on the same run: rebuild-stale-amd64 was SKIPPED because amd64 already matched HEAD (a prior native push aligned the revision label). The bug was latent for amd64 — would have fired on any push where amd64 actually drifted and CI had to rebuild. Closes #966. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes CI failures in rebuild-stale-arm64/rebuild-stale-amd64 where git worktree add --detach <dir> <sha> can fail on PR builds because the PR head SHA may not exist as a local object in the checkout.
Changes:
- Add a pre-
git worktree addguard that checks whetherSTARTUP_SHA_FULLexists locally viagit cat-file -e. - If missing, fetch the SHA from
origin(shallow first, then full fetch fallback) before creating the detached worktree.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+220
to
+222
| git -C "$REPO_ROOT" fetch --depth 1 origin "$STARTUP_SHA_FULL" 2>/dev/null \ | ||
| || git -C "$REPO_ROOT" fetch origin "$STARTUP_SHA_FULL" 2>/dev/null \ | ||
| || { echo "ERROR: cannot fetch sha $STARTUP_SHA_FULL from origin (not a real commit, or network/auth issue)" >&2; exit 1; } |
There was a problem hiding this comment.
The git fetch ... 2>/dev/null redirects will hide the underlying failure reason in CI logs (auth vs ref not found vs network). Consider keeping stderr for at least the final fetch attempt, or capturing and re-printing it when both fetch attempts fail, to make failures diagnosable.
Suggested change
| git -C "$REPO_ROOT" fetch --depth 1 origin "$STARTUP_SHA_FULL" 2>/dev/null \ | |
| || git -C "$REPO_ROOT" fetch origin "$STARTUP_SHA_FULL" 2>/dev/null \ | |
| || { echo "ERROR: cannot fetch sha $STARTUP_SHA_FULL from origin (not a real commit, or network/auth issue)" >&2; exit 1; } | |
| FETCH_ERR_LOG="$(mktemp)" | |
| if ! git -C "$REPO_ROOT" fetch --depth 1 origin "$STARTUP_SHA_FULL" 2>"$FETCH_ERR_LOG" \ | |
| && ! git -C "$REPO_ROOT" fetch origin "$STARTUP_SHA_FULL" 2>>"$FETCH_ERR_LOG"; then | |
| cat "$FETCH_ERR_LOG" >&2 | |
| rm -f "$FETCH_ERR_LOG" | |
| echo "ERROR: cannot fetch sha $STARTUP_SHA_FULL from origin (not a real commit, or network/auth issue)" >&2 | |
| exit 1 | |
| fi | |
| rm -f "$FETCH_ERR_LOG" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
rebuild-stale-arm64(and-amd64whenever it actually fires) on PR builds was hittingfatal: invalid reference: <sha>atgit worktree addtime, despiteSTARTUP_SHA_FULLbeing correctly resolved from.pull_request.head.sha.actions/checkout@v4shallow-clonesrefs/pull/<N>/merge, so the PR head commit is known as a remote ref but NOT as a local object.git worktree add --detach <DIR> <SHA>requires the commit to be locally available.worktree addongit cat-file -e; if the sha isn't local, fetch it from origin (shallow first, full-history fallback). One small new block inscripts/push-current-arch.sh:209-220.Empirical hit
PR #950 run 24927483127:
rebuild-stale-amd64was SKIPPED on the same run because amd64 already matched HEAD (a prior native push frombigmama-wslhad aligned the revision label) — so the bug was latent for amd64 and would have fired the next time CI actually had to rebuild it.Fix
Dev-machine path is unaffected —
cat-file -ealways succeeds on the local HEAD thatgit rev-parse HEADwould resolve to. Only fires in CI whenactions/checkout@v4's default fetch missed the sha.Test plan
bash -n scripts/push-current-arch.sh— clean (no syntax error from the new block).git worktree add(validates the fetch path). Will confirm by watchingrebuild-stale-amd64/rebuild-stale-arm64on the next push that triggers them.Closes
#966