Skip to content

fix(branch): avoid fork PR fetch collision with checked-out branch#1466

Open
syf2211 wants to merge 1 commit into
anthropics:mainfrom
syf2211:fix/fork-pr-branch-collision-1423
Open

fix(branch): avoid fork PR fetch collision with checked-out branch#1466
syf2211 wants to merge 1 commit into
anthropics:mainfrom
syf2211:fix/fork-pr-branch-collision-1423

Conversation

@syf2211

@syf2211 syf2211 commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes fork PR checkout failures in tag-mode workflows when the fork head branch name matches the branch already checked out on the runner (e.g. main).

Motivation

setupBranch fetched cross-repo PR heads with pull/<N>/head:<headRefName>, which writes into refs/heads/<headRefName>. If that branch is currently checked out, git refuses with:

fatal: refusing to fetch into branch 'refs/heads/main' checked out at '...'

This breaks issue_comment / tag-mode runs on fork PRs opened from the fork's default branch.

Fixes #1423

Changes

  • Fetch fork PR heads into a namespaced remote ref: refs/remotes/pull/<N>/head
  • Use force fetch (+) so reruns can update the ref safely
  • When the fork head name collides with the current branch, check out the PR head in detached mode
  • Otherwise create/update a local branch from the fetched PR head so git push keeps working for differently named fork heads
  • Add unit tests for the new checkout helpers

Tests

  • bun test test/fork-pr-branch-checkout.test.ts — pass
  • bun run typecheck — pass

Notes

Detached checkout is only used for the collision case described in #1423. Non-colliding fork PRs still get a local branch at headRefName.

For cross-repository PRs, fetch the pull head into a namespaced remote
ref instead of refs/heads/<headRefName>. When the fork head branch name
matches the runner's currently checked-out branch, check out the PR
head in detached mode to avoid repointing the local branch. Otherwise
create/update a local branch from the fetched PR head so git push keeps
working for differently named fork heads.

Fixes anthropics#1423
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fork PR whose head branch matches the runner's checked-out branch fails: "refusing to fetch into branch 'refs/heads/main'"

1 participant