From 51b93d25bd96d8ceb1c5e4e75ac8343d89ea96b3 Mon Sep 17 00:00:00 2001 From: audichuang <72485427+audichuang@users.noreply.github.com> Date: Mon, 30 Nov 2026 07:25:46 +0800 Subject: [PATCH] fix(github): check out the Review Mode branch in the repository the sync check uses (#6879) "Update Branch to Enable Review Mode..." ran gh pr checkout in connectedRoot() while the sync check and pull request lookup use reviewRoot() (#6622), so with a worktree or a second clone selected the update acted on another repository and could fail with "refusing to fetch into branch checked out at ...". checkoutPullRequest takes an optional root (default connectedRoot(), so other callers keep theirs); updateReviewBranch captures reviewRoot() before the details reload so a selection change during the reload cannot move the checkout. Co-authored-by: audichuang <72485427+audichuang@users.noreply.github.com> --- src/features/github/data/local.ts | 6 +-- .../github/reviewMode/reviewMode.test.tsx | 50 +++++++++++++++++++ src/features/github/reviewMode/state.ts | 4 +- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/features/github/data/local.ts b/src/features/github/data/local.ts index 46dd80ca1..9ab53f0d1 100644 --- a/src/features/github/data/local.ts +++ b/src/features/github/data/local.ts @@ -67,12 +67,12 @@ export function cancelBusy() { if (b) void commands.cancelRequest(b.request); } -/** "Checkout Branch": fetches and checks out the pull request's branch (gh's local name). */ -export async function checkoutPullRequest(number: number): Promise { +/** "Checkout Branch": fetches and checks out the pull request's branch (gh's local name) in `root`. */ +export async function checkoutPullRequest(number: number, root: string | null = connectedRoot()): Promise { // The command resolves with nothing (`null`), which `withBusy` reports as a failure. const op = { title: tx`Checking out #${number}…`, failure: tx`Couldn't check out #${number}` }; const done = await withBusy(op, async (r, out) => { - await call(commands.ghPrCheckout(connectedRoot(), r, number, out)); + await call(commands.ghPrCheckout(root, r, number, out)); return true; }); if (done === null) return false; diff --git a/src/features/github/reviewMode/reviewMode.test.tsx b/src/features/github/reviewMode/reviewMode.test.tsx index cc846a659..6d6e3cce4 100644 --- a/src/features/github/reviewMode/reviewMode.test.tsx +++ b/src/features/github/reviewMode/reviewMode.test.tsx @@ -536,6 +536,56 @@ describe("the pull request of the current branch", () => { expect(checkout).toHaveBeenCalledTimes(1); await waitFor(() => expect(useReviewBranch.getState().updateRequired).toBe(false)); }); + + it("Update to Enable Review Mode checks out in the repository the sync check uses", async () => { + await selectSecondCheckout(); + config["branch.feature.remote"] = "origin"; + config["branch.feature.merge"] = "refs/heads/feature"; + await refreshCurrentBranchPullRequest(); + expect(new Set(gitRepos)).toEqual(new Set(["/p/wt"])); + ghApi.mockImplementation((req: { endpoint: string; body?: string }) => + Promise.resolve( + req.endpoint === "graphql" && /\.\.\.pullRequestInfo/.test((JSON.parse(req.body!) as { query: string }).query) + ? { + status: "ok", + data: JSON.stringify({ + data: { repository: { pullRequest: { id: "PR1", number: N, headRefOid: "head" } } }, + }), + } + : respond(req), + ), + ); + await updateReviewBranch(); + expect(checkout).toHaveBeenCalledTimes(1); + expect(checkout.mock.calls[0]![0]).toBe("/p/wt"); + }); + + it("Update to Enable Review Mode keeps the repository it started in when the selection changes during the reload", async () => { + await selectSecondCheckout(); + config["branch.feature.remote"] = "origin"; + config["branch.feature.merge"] = "refs/heads/feature"; + await refreshCurrentBranchPullRequest(); + let release!: () => void; + const pending = new Promise((r) => (release = r)); + ghApi.mockImplementation(async (req: { endpoint: string; body?: string }) => { + if ( + req.endpoint !== "graphql" || + !/\.\.\.pullRequestInfo/.test((JSON.parse(req.body!) as { query: string }).query) + ) + return respond(req); + await pending; + return { + status: "ok", + data: JSON.stringify({ data: { repository: { pullRequest: { id: "PR1", number: N, headRefOid: "head" } } } }), + }; + }); + const update = updateReviewBranch(); + useVcs.setState({ repo: "/p" }); + release(); + await update; + expect(checkout).toHaveBeenCalledTimes(1); + expect(checkout.mock.calls[0]![0]).toBe("/p/wt"); + }); }); // --------------------------------------------------------------------------- diff --git a/src/features/github/reviewMode/state.ts b/src/features/github/reviewMode/state.ts index 1e1277a43..96a46eb8d 100644 --- a/src/features/github/reviewMode/state.ts +++ b/src/features/github/reviewMode/state.ts @@ -114,6 +114,8 @@ export async function refreshSyncState(): Promise { export async function updateReviewBranch(): Promise { const n = useReviewBranch.getState().number; if (n === null) return; + // The repository the sync check ran in, even if the selection changes during the reload. + const root = reviewRoot(); try { await reloadPullRequestData(n, "details"); } catch (e) { @@ -126,7 +128,7 @@ export async function updateReviewBranch(): Promise { }); return; } - if (await checkoutPullRequest(n)) await refreshSyncState(); + if (await checkoutPullRequest(n, root)) await refreshSyncState(); } /** Whether the changes of the pull request are loaded, loading or failed (the widget's tooltip). */