feat(vcs): scope Commit… to the selected project directory (#5304)

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>
This commit is contained in:
langxp
2026-11-23 14:27:36 +08:00
committed by GitHub
co-authored by kradme
parent 735a0157c1
commit ceb3cef1af
4 changed files with 92 additions and 11 deletions
@@ -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({
+1 -1
View File
@@ -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 &&
@@ -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: [] });
+23 -10
View File
@@ -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<ActionContext, "arg"> = {}) {
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<string, CheckState> {
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<string, CheckState>();
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;
}