Skip to content

Commit 27e243b

Browse files
ruangustavoRuan Gustavo Araujo da Silveira
andauthored
fix: fall back to FETCH_HEAD when gh pr checkout fails for branch names with / (#3232)
* fix: fall back to FETCH_HEAD checkout when gh pr checkout fails for branch names with / Fixes #3231 gh pr checkout internally runs `git checkout -b <branch> --track origin/<branch>`. When the branch name contains `/`, git cannot resolve the tracking ref inside a freshly created detached worktree, producing "starting point is not a branch". The fetch succeeds — only the tracking setup fails. Catch that specific error and fall back to `git checkout -b <localBranchName> --no-track FETCH_HEAD`. push.autoSetupRemote=true (already set after worktree creation) handles push tracking without needing --track. * fix: use -B flag to force-replace branch in FETCH_HEAD fallback checkout * fix: log fallback path when gh pr checkout fails with tracking error --------- Co-authored-by: Ruan Gustavo Araujo da Silveira <ruan.silveira@M4Pro.local>
1 parent 1b2fe39 commit 27e243b

1 file changed

Lines changed: 39 additions & 12 deletions

File tree

  • apps/desktop/src/lib/trpc/routers/workspaces/utils

apps/desktop/src/lib/trpc/routers/workspaces/utils/git.ts

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,18 +1777,45 @@ export async function createWorktreeFromPr({
17771777
});
17781778
}
17791779

1780-
await execWithShellEnv(
1781-
"gh",
1782-
[
1783-
"pr",
1784-
"checkout",
1785-
String(prInfo.number),
1786-
"--branch",
1787-
localBranchName,
1788-
"--force",
1789-
],
1790-
{ cwd: worktreePath, timeout: 120_000 },
1791-
);
1780+
try {
1781+
await execWithShellEnv(
1782+
"gh",
1783+
[
1784+
"pr",
1785+
"checkout",
1786+
String(prInfo.number),
1787+
"--branch",
1788+
localBranchName,
1789+
"--force",
1790+
],
1791+
{ cwd: worktreePath, timeout: 120_000 },
1792+
);
1793+
} catch (ghError) {
1794+
const ghMsg = ghError instanceof Error ? ghError.message : String(ghError);
1795+
// `gh pr checkout` can fail with "is not a branch" when the branch name
1796+
// contains '/' (e.g. "user/feature-branch"). Git has trouble resolving
1797+
// "origin/user/feature-branch" as a tracking ref inside a worktree.
1798+
// gh already fetched the remote successfully, so FETCH_HEAD points to
1799+
// the right commit — fall back to creating the branch without tracking.
1800+
if (!ghMsg.includes("is not a branch")) {
1801+
throw ghError;
1802+
}
1803+
console.log(
1804+
`[git] gh pr checkout failed with tracking error for PR #${prInfo.number}, falling back to FETCH_HEAD checkout`,
1805+
);
1806+
await execGitWithShellPath(
1807+
[
1808+
"-C",
1809+
worktreePath,
1810+
"checkout",
1811+
"-B",
1812+
localBranchName,
1813+
"--no-track",
1814+
"FETCH_HEAD",
1815+
],
1816+
{ timeout: 30_000 },
1817+
);
1818+
}
17921819

17931820
// Enable autoSetupRemote so `git push` just works without -u flag.
17941821
await execGitWithShellPath(

0 commit comments

Comments
 (0)