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>
This commit is contained in:
Aevion
2026-11-22 20:03:40 +08:00
committed by GitHub
parent 7e77c270b7
commit 026ef8794f
2 changed files with 86 additions and 3 deletions
@@ -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<typeof import("@/platform/ipc")>();
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<typeof import("@/workbench/contributions")>()),
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" }),
);
});
});
+2 -3
View File
@@ -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",