diff --git a/src/features/vcs/__tests__/addToVcsAction.test.ts b/src/features/vcs/__tests__/addToVcsAction.test.ts index 49f1c5130..552c26e9a 100644 --- a/src/features/vcs/__tests__/addToVcsAction.test.ts +++ b/src/features/vcs/__tests__/addToVcsAction.test.ts @@ -2,6 +2,7 @@ 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 ignore = vi.hoisted(() => vi.fn(async () => ({ status: "ok", data: [] }))); const notify = vi.hoisted(() => vi.fn()); const project = vi.hoisted(() => ({ paths: [] as string[] })); @@ -12,6 +13,7 @@ vi.mock("@/platform/ipc", async (importOriginal) => { commands: { ...real.commands, vcsStage: stage, + vcsAddToIgnore: ignore, vcsStatus: async () => ({ status: "ok", data: [] }), vcsBranchInfo: async () => ({ status: "ok", data: null }), vcsRoots: async () => ({ status: "ok", data: [{ root: "/repo", operation: null }] }), @@ -43,6 +45,7 @@ let unregister: () => void; beforeEach(() => { stage.mockClear(); + ignore.mockClear(); notify.mockClear(); project.paths = []; const files = [fs("dir/new.ts", "untracked"), fs("dir/sub/n2.ts", "untracked"), fs("dir/tracked.ts", "modified")]; @@ -82,3 +85,14 @@ describe("Add to VCS", () => { ); }); }); + +describe("Add to .gitignore", () => { + it("ignores the Project view's selection while it has focus, and not otherwise", async () => { + project.paths = ["/repo/dir/new.ts"]; + await getAction("vcs.ignoreShared")!.run({ arg: undefined, ctx: { vcsAvailable: true, projectViewFocus: true } }); + expect(ignore).toHaveBeenCalledWith(["/repo/dir/new.ts"], "gitignore"); + ignore.mockClear(); + await getAction("vcs.ignoreShared")!.run({ arg: undefined, ctx: { vcsAvailable: true } }); + expect(ignore).not.toHaveBeenCalledWith(["/repo/dir/new.ts"], "gitignore"); + }); +}); diff --git a/src/features/vcs/actions.tsx b/src/features/vcs/actions.tsx index d8de2f1cc..61af6244c 100644 --- a/src/features/vcs/actions.tsx +++ b/src/features/vcs/actions.tsx @@ -156,6 +156,11 @@ function selectedPaths(arg: unknown): string[] { return project.length ? project : contextPaths(arg); } +/** Paths a file action targets: the Project view's selection while it has focus, else the open file. */ +function focusedPaths(arg: unknown, ctx: { projectViewFocus?: unknown }): string[] { + return ctx.projectViewFocus ? selectedPaths(arg) : contextPaths(arg); +} + /** * The repository a repository-wide dialog (Pull, Merge, Rebase) starts on: the one holding the * action's file, else the Project view's selection while it has focus, else the active editor's @@ -661,8 +666,8 @@ export function registerVcsActions(): () => void { category: "Git", icon: "history", when: VCS, - run: ({ arg }) => { - const path = contextPaths(arg)[0]; + run: ({ arg, ctx }) => { + const path = focusedPaths(arg, ctx)[0]; if (!path) return; openLogTab("history", path); showGitWindow(); @@ -717,7 +722,7 @@ export function registerVcsActions(): () => void { category: "Git", when: VCS, keywords: ["ignore", "unversioned", "exclude"], - run: ({ arg }) => addToIgnore(contextPaths(arg), destination), + run: ({ arg, ctx }) => addToIgnore(focusedPaths(arg, ctx), destination), })), { id: "vcs.addToVcs", @@ -726,8 +731,7 @@ export function registerVcsActions(): () => void { icon: "plus", when: VCS, keywords: ["git add", "stage"], - // 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)), + run: ({ arg, ctx }) => addToVcs(focusedPaths(arg, ctx)), }, { id: "vcs.stash",