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 <[email protected]>
Цей коміт міститься в:
audichuang authored and GitHub committed 2026-11-30 07:25:46 +08:00
1 джерело c0b5789dd6
коміт 51b93d25bd
3 files changed
+56 -4

No files matched your search

+3 -3
Переглянути файл
@@ -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<boolean> {
/** "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<boolean> {
// 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;
+50
Переглянути файл
@@ -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<void>((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");
});
});
// ---------------------------------------------------------------------------
+3 -1
Переглянути файл
@@ -114,6 +114,8 @@ export async function refreshSyncState(): Promise<void> {
export async function updateReviewBranch(): Promise<void> {
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<void> {
});
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). */