From 026ef8794fd4472a0252d2940c13e9c1901587c6 Mon Sep 17 00:00:00 2001 From: Aevion <9879953+aevioni@users.noreply.github.com> Date: Sun, 22 Nov 2026 20:03:40 +0800 Subject: [PATCH] fix(vcs): Add to VCS stages the Project view's selection (#5210) Pressing Add to VCS (Option+Cmd+A) with a folder selected in the Project view reported "Nothing to add" instead of staging the unversioned files in it: the action resolved its targets from the active editor's file only. While the Project view has focus it now takes the Project view's selection; from the editor it still adds the open file. Co-authored-by: aevioni <9879953+aevioni@users.noreply.github.com> --- .../vcs/__tests__/addToVcsAction.test.ts | 84 +++++++++++++++++++ src/features/vcs/actions.tsx | 5 +- 2 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 src/features/vcs/__tests__/addToVcsAction.test.ts diff --git a/src/features/vcs/__tests__/addToVcsAction.test.ts b/src/features/vcs/__tests__/addToVcsAction.test.ts new file mode 100644 index 000000000..49f1c5130 --- /dev/null +++ b/src/features/vcs/__tests__/addToVcsAction.test.ts @@ -0,0 +1,84 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import type { FileStatus } from "@/bindings"; + +const stage = vi.hoisted(() => vi.fn(async () => ({ status: "ok", data: null }))); +const notify = vi.hoisted(() => vi.fn()); +const project = vi.hoisted(() => ({ paths: [] as string[] })); + +vi.mock("@/platform/ipc", async (importOriginal) => { + const real = await importOriginal(); + return { + ...real, + commands: { + ...real.commands, + vcsStage: stage, + vcsStatus: async () => ({ status: "ok", data: [] }), + vcsBranchInfo: async () => ({ status: "ok", data: null }), + vcsRoots: async () => ({ status: "ok", data: [{ root: "/repo", operation: null }] }), + }, + }; +}); +vi.mock("@/workbench/contributions", async (importOriginal) => ({ + ...(await importOriginal()), + projectViewSelection: () => project.paths, + notifications: () => ({ notify }), +})); +vi.mock("../comparePaths", () => ({ showDirectoryWorkingDiff: async () => false })); +vi.mock("../diffDialog", () => ({ openDiffDialog: vi.fn() })); +vi.mock("../DiffWindow", () => ({ showDiffWindow: vi.fn() })); + +import { getAction } from "@/platform/actions"; +import { registerVcsActions } from "../actions"; +import { useVcs } from "../store"; + +const fs = (relPath: string, unstaged: FileStatus["unstaged"]): FileStatus => ({ + path: `/repo/${relPath}`, + relPath, + staged: null, + unstaged, + renamedFrom: null, +}); + +let unregister: () => void; + +beforeEach(() => { + stage.mockClear(); + notify.mockClear(); + project.paths = []; + const files = [fs("dir/new.ts", "untracked"), fs("dir/sub/n2.ts", "untracked"), fs("dir/tracked.ts", "modified")]; + useVcs.setState({ + root: "/repo", + roots: ["/repo"], + status: files, + byPath: new Map(files.map((f) => [f.path, f])), + statusLoaded: true, + }); + unregister = registerVcsActions(); +}); + +afterEach(() => unregister()); + +const addToVcs = () => getAction("vcs.addToVcs")!; + +describe("Add to VCS", () => { + it("adds the unversioned files below the Project view's selected directory", async () => { + project.paths = ["/repo/dir"]; + await addToVcs().run({ arg: undefined, ctx: { vcsAvailable: true, projectViewFocus: true } }); + expect(stage).toHaveBeenCalledWith(["/repo/dir/new.ts", "/repo/dir/sub/n2.ts"]); + expect(notify).not.toHaveBeenCalled(); + }); + + it("leaves the Project view's selection alone while another view has focus", async () => { + project.paths = ["/repo/dir"]; + await addToVcs().run({ arg: undefined, ctx: { vcsAvailable: true } }); + expect(stage).not.toHaveBeenCalled(); + }); + + it("only adds the unversioned files under an explicit selection", async () => { + await addToVcs().run({ arg: ["/repo/dir/tracked.ts"], ctx: { vcsAvailable: true } }); + expect(stage).not.toHaveBeenCalled(); + expect(notify).toHaveBeenCalledWith( + expect.objectContaining({ title: "Nothing to add: the files are already under version control" }), + ); + }); +}); diff --git a/src/features/vcs/actions.tsx b/src/features/vcs/actions.tsx index 0f6d3e415..d8de2f1cc 100644 --- a/src/features/vcs/actions.tsx +++ b/src/features/vcs/actions.tsx @@ -726,9 +726,8 @@ export function registerVcsActions(): () => void { icon: "plus", when: VCS, keywords: ["git add", "stage"], - run: ({ arg }) => { - return addToVcs(contextPaths(arg)); - }, + // The Project view's selection only while it has focus: from the editor it is the open file. + run: ({ arg, ctx }) => addToVcs(ctx.projectViewFocus ? selectedPaths(arg) : contextPaths(arg)), }, { id: "vcs.stash",