From d3cb8513beac46aa437999de81d2ccfea50ba0eb Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Wed, 2 Dec 2026 19:33:22 +0800 Subject: [PATCH] fix(vcs): show why checkout was blocked (#7520) When untracked files block a checkout, an error dialog lists them, says whether Git would overwrite or remove them, and the reason is recorded in the Git Console. Warnings and errors from explicit branch and worktree commands stay visible with background notifications muted, and Checkout and Update reports a failed HEAD read instead of silently skipping the update. --- src/features/vcs/NewWorktreeDialog.tsx | 1 + src/features/vcs/__tests__/branchOps.test.ts | 45 ++++++++----------- .../vcs/__tests__/updateBranches.test.ts | 11 +++++ src/features/vcs/branchOps.ts | 30 +++++++++---- src/features/vcs/ops.ts | 3 +- 5 files changed, 53 insertions(+), 37 deletions(-) diff --git a/src/features/vcs/NewWorktreeDialog.tsx b/src/features/vcs/NewWorktreeDialog.tsx index 4d72ad6ea..bd113c48d 100644 --- a/src/features/vcs/NewWorktreeDialog.tsx +++ b/src/features/vcs/NewWorktreeDialog.tsx @@ -233,6 +233,7 @@ function WorktreeForm({ level: "warning", title: tx("Some worktree configuration could not be copied"), body: result.copyWarning, + userInitiated: true, }); } if (result.setupWarning) { diff --git a/src/features/vcs/__tests__/branchOps.test.ts b/src/features/vcs/__tests__/branchOps.test.ts index a0a52e150..ad511597e 100644 --- a/src/features/vcs/__tests__/branchOps.test.ts +++ b/src/features/vcs/__tests__/branchOps.test.ts @@ -1,4 +1,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { createElement } from "react"; +import { fireEvent, render, screen, within } from "@testing-library/react"; +import { MessagesHost } from "@/workbench/messages"; import type { Branch, BranchInfo, ProjectInfo, Worktree } from "@/bindings"; const mocks = vi.hoisted(() => ({ prompt: vi.fn(), @@ -312,6 +315,7 @@ it("shows a partial failure without claiming that the rename failed", async () = expect(mocks.notify).toHaveBeenCalledExactlyOnceWith({ group: "vcs", level: "error", + userInitiated: true, title: "Rename branch topic", body: message, }); @@ -411,7 +415,9 @@ it("checks every affected repository before moving any HEAD", async () => { await checkoutRevision({ repos }); expect(mocks.hasCommit.mock.calls).toEqual(repos.map((root) => [root, "release"])); expect(mocks.checkout).not.toHaveBeenCalled(); - expect(mocks.notify).toHaveBeenCalledWith(expect.objectContaining({ body: "Revision was not found in nested" })); + expect(mocks.notify).toHaveBeenCalledWith( + expect.objectContaining({ userInitiated: true, body: "Revision was not found in nested" }), + ); mocks.hasCommit.mockResolvedValue(ok(true)); mocks.checkout.mockResolvedValue(ok({ kind: "done" })); @@ -478,32 +484,19 @@ it("does not open the worktree when the checkout offer is declined", async () => expect(mocks.executeAction).not.toHaveBeenCalled(); }); -it("names the files git would remove when untracked ones block the checkout", async () => { - mocks.checkout.mockResolvedValue(ok({ kind: "untrackedFiles", files: ["TEST.md"], removed: true })); - await checkoutBranch(local("feature/x")); +it.each([ + [true, "removed"], + [false, "overwritten"], +])("shows the files that checkout would affect in an error dialog (%s)", async (removed, verb) => { + mocks.checkout.mockResolvedValue(ok({ kind: "untrackedFiles", files: ["TEST.md", "dir/new.txt"], removed })); + render(createElement(MessagesHost)); + const checkout = checkoutBranch(local("feature/x")); + const dialog = await screen.findByRole("dialog", { name: "Untracked files prevent checkout" }); + expect(dialog).toHaveTextContent(`The following untracked working tree files would be ${verb} by checkout:`); + expect(dialog).toHaveTextContent("TEST.md dir/new.txt"); + fireEvent.click(within(dialog).getByRole("button", { name: "OK" })); + await checkout; expect(mocks.checkout).toHaveBeenCalledTimes(1); - expect(mocks.notify).toHaveBeenCalledWith( - expect.objectContaining({ - level: "error", - title: "Untracked files prevent checkout", - body: expect.stringContaining( - "The following untracked working tree files would be removed by checkout:\nTEST.md", - ), - }), - ); -}); - -it("still says overwritten when untracked files would be overwritten", async () => { - mocks.checkout.mockResolvedValue(ok({ kind: "untrackedFiles", files: ["dir/new.txt"], removed: false })); - await checkoutBranch(local("feature/x")); - expect(mocks.notify).toHaveBeenCalledWith( - expect.objectContaining({ - level: "error", - body: expect.stringContaining( - "The following untracked working tree files would be overwritten by checkout:\ndir/new.txt", - ), - }), - ); }); it("keeps the branch when the worktree question is turned down", async () => { diff --git a/src/features/vcs/__tests__/updateBranches.test.ts b/src/features/vcs/__tests__/updateBranches.test.ts index 90679fc92..49df94210 100644 --- a/src/features/vcs/__tests__/updateBranches.test.ts +++ b/src/features/vcs/__tests__/updateBranches.test.ts @@ -269,6 +269,17 @@ describe("Checkout and Update", () => { ]); }); + it("reports a failed HEAD read before the update", async () => { + mocks.checkout.mockResolvedValue({ status: "ok", data: { kind: "done" } }); + mocks.head.mockResolvedValue({ status: "error", error: { kind: "io", message: "Cannot read HEAD" } }); + await checkoutAndUpdate(branch("topic")); + expect(mocks.notify).toHaveBeenCalledWith( + expect.objectContaining({ level: "error", userInitiated: true, body: "Cannot read HEAD" }), + ); + expect(mocks.updateCurrent).not.toHaveBeenCalled(); + expect(mocks.update).not.toHaveBeenCalled(); + }); + it("updates nothing when the checkout did not happen", async () => { mocks.checkout.mockResolvedValue({ status: "error", error: { kind: "failed", message: "nope" } }); await checkoutAndUpdate(branch("topic")); diff --git a/src/features/vcs/branchOps.ts b/src/features/vcs/branchOps.ts index a29f91ae8..b3489d3c7 100644 --- a/src/features/vcs/branchOps.ts +++ b/src/features/vcs/branchOps.ts @@ -19,7 +19,7 @@ import { notifications } from "@/workbench/contributions"; import { openWorktree } from "./openWorktree"; import { githubHttpsFailureHelp } from "./githubHttpsFailure"; import { activeRepository } from "./activeRepository"; -import { ask, confirmChecked } from "@/workbench/messages"; +import { ask, confirmChecked, showError } from "@/workbench/messages"; import { branchNamePrefixes, isFavoriteBranch, isProtectedBranch, pushRecent, shortRemoteName } from "./branches"; import { showCommitRanges } from "./CompareBranches"; import type { AffectedFiles } from "./LocalChangesReviewDialog"; @@ -27,6 +27,7 @@ import { confirmDialog, promptText } from "./dialogs"; import { cleanUpBranchNameOnTyping } from "@/platform/gitRefName"; import { errorMessage, + logConsole, runBranchUpdates, runGitOperation, runOp, @@ -206,6 +207,7 @@ async function reviewCheckout( const shelved = workflowSettings().vcs.git.saveChangesPolicy === "shelve"; notifications().notify({ group: "vcs", + userInitiated: true, level: "warning", title: tx("Could not restore local changes after checkout"), body: outcome.message, @@ -221,6 +223,7 @@ async function reviewCheckout( const shelved = opts.mode === "smart" && workflowSettings().vcs.git.saveChangesPolicy === "shelve"; notifications().notify({ group: "vcs", + userInitiated: true, level: "warning", title: `${success()}, with conflicts`, body: outcome.files.join("\n"), @@ -241,12 +244,9 @@ async function reviewCheckout( const body = outcome.removed ? tx`The following untracked working tree files would be removed by checkout:\n${files}\nPlease move or remove them before you switch branches.` : tx`The following untracked working tree files would be overwritten by checkout:\n${files}\nPlease move or remove them before you switch branches.`; - notifications().notify({ - group: "vcs", - level: "error", - title: "Untracked files prevent checkout", - body, - }); + logConsole("err", body); + // oxlint-disable-next-line no-await-in-loop -- acknowledge the blocked checkout before a possible multi-root rollback + await showError(tx("Untracked files prevent checkout"), body); return undefined; } case "localChanges": { @@ -604,6 +604,7 @@ export async function checkoutRevision(scope?: CheckoutRevisionScope) { notifications().notify({ group: "vcs", level: "error", + userInitiated: true, title: tx`Could not check out ${label}`, body: names ? tx`Revision was not found in ${names}` : tx("Revision was not found"), }); @@ -784,7 +785,13 @@ export async function renameBranch(branch: Branch) { success: tx`Renamed branch ${name} to ${to}`, onError: (message) => { // Unsetting the upstream can fail after the rename has succeeded. - notifications().notify({ group: "vcs", level: "error", title: tx`Rename branch ${name}`, body: message }); + notifications().notify({ + group: "vcs", + userInitiated: true, + level: "error", + title: tx`Rename branch ${name}`, + body: message, + }); return true; }, }, @@ -1420,7 +1427,12 @@ export async function checkoutAndUpdate(branch: NamedBranch, roots?: readonly st if (useProject.getState().project !== project) return; const repos: readonly (string | null)[] = roots ?? [currentRepo()]; const heads = await Promise.all( - repos.map(async (repo) => [repo, await call(commands.vcsBranchInfo(repo)).catch(() => null)] as const), + repos.map(async (repo) => { + const head = await runOp(tx("Read repository HEAD"), () => call(commands.vcsBranchInfo(repo)), { + refresh: false, + }); + return [repo, head] as const; + }), ); if (useProject.getState().project !== project) return; const targets = heads diff --git a/src/features/vcs/ops.ts b/src/features/vcs/ops.ts index 7ea4330e7..12ebb0aae 100644 --- a/src/features/vcs/ops.ts +++ b/src/features/vcs/ops.ts @@ -139,8 +139,7 @@ export async function saveAll() { try { await saveDiffWindows(); } catch (error) { - notifications().notify({ - group: "vcs", + notify({ level: "error", title: tx("Could not save diff"), body: errorMessage(error),