From ceb3cef1afd8900ac398ae72e0df207b610ec9b3 Mon Sep 17 00:00:00 2001 From: langxp Date: Mon, 23 Nov 2026 14:27:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(vcs):=20scope=20Commit=E2=80=A6=20to=20the?= =?UTF-8?q?=20selected=20project=20directory=20(#5304)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Git > Commit… from the Project view ignored the selection. It now passes the selected paths, and the commit interface (modal or not) opens with only the changes at or below them checked: tracked and unversioned files included, conflicts left out, the rest of the working tree still listed. Co-authored-by: kradme <17811095+kradme@users.noreply.github.com> --- src/features/project-tree/contextMenu.test.ts | 11 ++++ src/features/project-tree/contextMenu.ts | 2 +- src/features/vcs/__tests__/commitMode.test.ts | 57 +++++++++++++++++++ src/features/vcs/commitUi.ts | 33 +++++++---- 4 files changed, 92 insertions(+), 11 deletions(-) diff --git a/src/features/project-tree/contextMenu.test.ts b/src/features/project-tree/contextMenu.test.ts index feb77e9ad..5ff93c85b 100644 --- a/src/features/project-tree/contextMenu.test.ts +++ b/src/features/project-tree/contextMenu.test.ts @@ -115,6 +115,17 @@ describe("project view context menu", () => { } }); + it("passes the selected Project view directory to Commit…", () => { + const items = contextMenuItems([node("/p/module", true)]); + const git = items.find((item) => item.kind !== "separator" && item.kind !== "header" && item.label === "Git"); + const commit = + git && "submenu" in git + ? git.submenu?.find((item) => item.kind !== "separator" && item.kind !== "header" && item.label === "Commit…") + : undefined; + + expect(commit).toMatchObject({ kind: "action", action: "vcs.commit", arg: "/p/module" }); + }); + it("lists contributed links in Copy Path/Reference…", () => { disposers.push( registerMenuGroup({ diff --git a/src/features/project-tree/contextMenu.ts b/src/features/project-tree/contextMenu.ts index 3e098decf..32f93bf5d 100644 --- a/src/features/project-tree/contextMenu.ts +++ b/src/features/project-tree/contextMenu.ts @@ -378,7 +378,7 @@ export function contextMenuItems(nodes: ProjectNode[]): MenuItem[] { "git", "Git", [ - act("vcs.commit", undefined, dirs || nodes.length > 1 ? "Commit…" : "Commit File…"), + act("vcs.commit", arg, dirs || nodes.length > 1 ? "Commit…" : "Commit File…"), act("vcs.addToVcs", arg, "Add"), nonRoot.length === nodes.length && nodes.length > 0 && diff --git a/src/features/vcs/__tests__/commitMode.test.ts b/src/features/vcs/__tests__/commitMode.test.ts index 88e701fca..24811162f 100644 --- a/src/features/vcs/__tests__/commitMode.test.ts +++ b/src/features/vcs/__tests__/commitMode.test.ts @@ -25,6 +25,7 @@ vi.mock("@/platform/ipc", async (importOriginal) => { }); import { useProject } from "@/platform/project"; +import { provideToolWindowService, type ToolWindowService } from "@/workbench/contributions"; import { commitChangelist, commitFiles } from "../changelistsUi"; import { checksForList, initialChangelists, useChangelists } from "../changelists"; import { gitTabs, shownTab, stashTabOf, stashTabs } from "../gitTabs"; @@ -360,6 +361,62 @@ describe("Commit… from Local Changes with the modal interface", () => { }); }); +describe("Commit… from a Project view directory", () => { + const status: FileStatus[] = [ + { path: "/p/module/a.ts", relPath: "module/a.ts", staged: null, unstaged: "modified", renamedFrom: null }, + { path: "/p/module/sub/b.ts", relPath: "module/sub/b.ts", staged: null, unstaged: "modified", renamedFrom: null }, + { + path: "/p/module/new-file.ts", + relPath: "module/new-file.ts", + staged: null, + unstaged: "untracked", + renamedFrom: null, + }, + { path: "/p/module/new-dir/", relPath: "module/new-dir/", staged: null, unstaged: "untracked", renamedFrom: null }, + { + path: "/p/module/conflict.ts", + relPath: "module/conflict.ts", + staged: "conflicted", + unstaged: null, + renamedFrom: null, + }, + { path: "/p/module-old/a.ts", relPath: "module-old/a.ts", staged: null, unstaged: "modified", renamedFrom: null }, + { path: "/p/other.ts", relPath: "other.ts", staged: null, unstaged: "modified", renamedFrom: null }, + ]; + + beforeEach(() => { + showCommitDialog.mockClear(); + useProject.setState({ settings: { vcs: { commit: { nonModal: false } } } as Settings }); + useVcs.setState({ available: true, root: "/p", repo: "/p", roots: ["/p"], status }); + useCommit.setState({ checks: new Map([["/p/other.ts", "all"]]), message: "", selection: [] }); + }); + + it("opens the modal dialog with every tracked and unversioned change below the directory checked", async () => { + commitFromContext({ arg: "/p/module" }); + await vi.waitFor(() => expect(showCommitDialog).toHaveBeenCalled()); + + const checks = useCommit.getState().checks; + for (const path of ["/p/module/a.ts", "/p/module/sub/b.ts", "/p/module/new-file.ts", "/p/module/new-dir/"]) + expect(checks.get(path)).toBe("all"); + expect(checks.get("/p/module-old/a.ts")).toBe("none"); + expect(checks.get("/p/other.ts")).toBe("none"); + expect(checks.has("/p/module/conflict.ts")).toBe(false); + }); + + it("applies the same directory scope in the non-modal Commit tool window", () => { + const show = vi.fn(); + provideToolWindowService({ show } as unknown as ToolWindowService); + useProject.setState({ settings: { vcs: { commit: { nonModal: true } } } as Settings }); + + commitFromContext({ arg: "/p/module" }); + + expect(showCommitDialog).not.toHaveBeenCalled(); + expect(show).toHaveBeenCalledWith("commit", true); + expect(useCommit.getState().checks.get("/p/module/sub/b.ts")).toBe("all"); + expect(useCommit.getState().checks.get("/p/module-old/a.ts")).toBe("none"); + }); +}); + describe("log tabs of their own", () => { beforeEach(() => { vcsLog.mockReset().mockResolvedValue({ status: "ok", data: [] }); diff --git a/src/features/vcs/commitUi.ts b/src/features/vcs/commitUi.ts index f5f964828..661ac3540 100644 --- a/src/features/vcs/commitUi.ts +++ b/src/features/vcs/commitUi.ts @@ -3,6 +3,7 @@ * the Commit dialog with the local changes in the Git tool window (modal one, * Settings › Version Control › Commit). Also the push that follows Commit and Push…. */ +import type { ActionContext } from "@/platform/actions"; import { workflowSettings, type GitSettingsValid } from "@/platform/workflowSettings"; import { call, commands } from "@/platform/ipc"; import { toolWindowService } from "@/workbench/contributions"; @@ -12,6 +13,7 @@ import { commitsStagedPart, groupOf, type CheckState } from "./changes"; import { stashTabOf } from "./gitTabs"; import { selectGitTab } from "./logStore"; import { isProtectedBranch } from "./branches"; +import { stripSlash } from "./model"; import { pushTargets, showPushDialog } from "./remoteUi"; import { inspectPushTargets, selectedPushOperation } from "./pushInspection"; import { runRemoteRound } from "./ops"; @@ -39,26 +41,37 @@ export function openCommit() { } /** - * Commit… (⌘K) as an action: with the modal interface the dialog includes just the files - * selected in Local Changes when invoked from there, otherwise the default inclusion. + * Commit… (⌘K) as an action. An explicit path from the Project view scopes the inclusion + * for either commit interface; otherwise the modal dialog keeps its Local Changes rule. */ -export function commitFromContext() { - if (isModalCommit()) { - const fromLocalChanges = !!(document.activeElement as HTMLElement | null)?.closest?.('[data-changes-tree="local"]'); - const selection = fromLocalChanges ? useCommit.getState().selection : []; +export function commitFromContext({ arg }: Pick = {}) { + const explicit = pathArgs(arg); + const fromLocalChanges = !!(document.activeElement as HTMLElement | null)?.closest?.('[data-changes-tree="local"]'); + const selection = explicit.length ? explicit : fromLocalChanges ? useCommit.getState().selection : []; + if (explicit.length || isModalCommit()) useCommit.setState({ checks: selection.length ? includeOnly(useVcs.getState().status, selection) : new Map() }); - } openCommit(); } -/** Inclusion that commits only `paths` of `status` (files elsewhere left out, conflicts as they are). */ +/** Explicit Project view paths; keyboard and toolbar invocations have no scope of their own. */ +function pathArgs(arg: unknown): string[] { + return (Array.isArray(arg) ? arg : [arg]).filter( + (path): path is string => typeof path === "string" && path.length > 0, + ); +} + +/** Inclusion that commits only changes at or below `paths`; conflicts are left out. */ export function includeOnly(status: readonly FileStatus[], paths: readonly string[]): Map { - const chosen = new Set(paths); + const chosen = [...new Set(paths.map(stripSlash))]; + const atOrBelow = (path: string) => { + const p = stripSlash(path); + return chosen.some((root) => p === root || p.startsWith(`${root}/`)); + }; const checks = new Map(); for (const fs of status) { const group = groupOf(fs); if (!group || group === "conflicts") continue; - checks.set(fs.path, !chosen.has(fs.path) ? "none" : commitsStagedPart(fs) ? "staged" : "all"); + checks.set(fs.path, !atOrBelow(fs.path) ? "none" : commitsStagedPart(fs) ? "staged" : "all"); } return checks; }