diff --git a/src/features/vcs/__tests__/logCommitListShowDiff.test.tsx b/src/features/vcs/__tests__/logCommitListShowDiff.test.tsx new file mode 100644 index 0000000000..443b34b967 --- /dev/null +++ b/src/features/vcs/__tests__/logCommitListShowDiff.test.tsx @@ -0,0 +1,130 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react"; + +const mocks = vi.hoisted(() => ({ + openVcsDiff: vi.fn(), + openDiff: vi.fn(), + notify: vi.fn(), +})); +vi.mock("@/features/vcs/diffInput", async (original) => ({ + ...(await original()), + openVcsDiff: mocks.openVcsDiff, + openDiff: mocks.openDiff, +})); +vi.mock("@/workbench/contributions", async (original) => ({ + ...(await original()), + notifications: () => ({ notify: mocks.notify, expire: vi.fn() }), +})); + +import type { Commit } from "@/bindings"; +import { registerVcsActions } from "@/features/vcs/actions"; +import { registerLogPaneActions } from "@/features/vcs/logPaneActions"; +import { useLogChanges, type LogChange } from "@/features/vcs/logChanges"; +import { CommitList } from "@/features/vcs/log/CommitList"; +import { useGitLog } from "@/features/vcs/logStore"; +import { useVcs } from "@/features/vcs/store"; +import { setContext } from "@/platform/context"; +import { installKeyboardDispatcher, useKeymap } from "@/platform/keymap"; +import { useProject } from "@/platform/project"; + +const ctrlD = { key: "d", code: "KeyD", ctrlKey: true }; +const original = useKeymap.getState().overrides; +const cleanups: (() => void)[] = []; + +const commit = { + id: "c0ffee01", + shortId: "c0ffee0", + summary: "one", + author: "A", + timeMs: 1_700_000_000_000, + parents: ["c0ffee00"], + refs: [], +} as unknown as Commit; + +const change = (path: string): LogChange => ({ + path, + oldPath: null, + kind: "modified", + before: "c0ffee00", + after: "c0ffee01", + statsFrom: "c0ffee00", + statsTo: "c0ffee01", + diff: { path: `/p/${path}`, left: { type: "rev", rev: "c0ffee00" }, right: { type: "rev", rev: "c0ffee01" } }, +}); + +beforeEach(() => { + vi.clearAllMocks(); + useProject.setState({ project: { root: "/p", name: "p" } as never }); + useVcs.setState({ available: true, root: "/p", roots: ["/p"], repo: "/p" }); + setContext("vcsAvailable", true); + cleanups.push(registerVcsActions(), registerLogPaneActions(), installKeyboardDispatcher()); + // One key on both actions, as a preset does: the focused view's context picks the diff. + useKeymap.setState({ + overrides: { ...original, "vcs.showDiff": ["Ctrl+D"], "vcs.log.changes.showDiff": ["Ctrl+D"] }, + }); +}); + +afterEach(() => { + while (cleanups.length) cleanups.pop()!(); + cleanup(); + useLogChanges.setState({ commits: [], all: [], selected: [] }); + useGitLog.setState({ commits: [], selection: [], logTabs: [] }); + setContext("vcsAvailable", false); + setContext("vcsLogRevisionFocus", false); + setContext("vcsLogCommitsFocus", false); + useKeymap.setState({ overrides: original }); + useProject.setState({ project: null }); + useVcs.setState({ available: false, root: null, roots: [], repo: null }); +}); + +/** Renders the log with `commit` selected and its changes loaded in the details pane. */ +function show(changes: LogChange[], selected: LogChange[] = []) { + useGitLog.setState({ commits: [commit], selection: ["c0ffee01"] }); + useLogChanges.setState({ commits: [commit], all: changes, selected }); + render(); + const list = screen.getByRole("listbox", { name: "Commits" }); + list.focus(); + return list; +} + +describe("Show Diff with focus in the Git Log commit list", () => { + it("opens the selected commit's changes with every file in the chain", async () => { + const list = show([change("src/a.ts"), change("src/b.ts")]); + fireEvent.keyDown(list, ctrlD); + await waitFor(() => expect(mocks.openVcsDiff).toHaveBeenCalledOnce()); + const data = mocks.openVcsDiff.mock.calls[0]![0] as { path: string; chain?: Array<{ path: string }> }; + expect(data.path).toBe("/p/src/a.ts"); + expect(data.chain?.map((d) => d.path)).toEqual(["/p/src/a.ts", "/p/src/b.ts"]); + expect(mocks.notify).not.toHaveBeenCalled(); + }); + + it("opens the changes pane's selected file, keeping the whole commit in the chain", async () => { + const changes = [change("src/a.ts"), change("src/b.ts")]; + const list = show(changes, [changes[1]!]); + fireEvent.keyDown(list, ctrlD); + await waitFor(() => expect(mocks.openVcsDiff).toHaveBeenCalledOnce()); + const data = mocks.openVcsDiff.mock.calls[0]![0] as { path: string; chain?: Array<{ path: string }> }; + expect(data.path).toBe("/p/src/b.ts"); + expect(data.chain?.map((d) => d.path)).toEqual(["/p/src/a.ts", "/p/src/b.ts"]); + expect(mocks.notify).not.toHaveBeenCalled(); + }); + + it("still diffs the history target's file when a file-history list has focus", async () => { + useGitLog.setState({ + commits: [commit], + selection: ["c0ffee01"], + logTabs: [{ id: "log", kind: "history", target: "src/b.ts" }], + }); + const changes = [change("src/a.ts"), change("src/b.ts")]; + useLogChanges.setState({ commits: [commit], all: changes, selected: [] }); + render(); + const list = screen.getByRole("listbox", { name: "Commits" }); + list.focus(); + fireEvent.keyDown(list, ctrlD); + await waitFor(() => expect(mocks.openVcsDiff).toHaveBeenCalledOnce()); + const data = mocks.openVcsDiff.mock.calls[0]![0] as { path: string; chain?: Array<{ path: string }> }; + expect(data.path).toBe("/p/src/b.ts"); + expect(data.chain).toBeUndefined(); + expect(mocks.notify).not.toHaveBeenCalled(); + }); +}); diff --git a/src/features/vcs/log/CommitList.tsx b/src/features/vcs/log/CommitList.tsx index 9c6747915b..df79ad3dba 100644 --- a/src/features/vcs/log/CommitList.tsx +++ b/src/features/vcs/log/CommitList.tsx @@ -20,6 +20,7 @@ import { type MouseEvent as ReactMouseEvent, } from "react"; import styles from "../GitLog.module.css"; +import { useDiffSource } from "../diffSource"; import { reachableFrom } from "../graph"; import { LogIdContext } from "../logContext"; import { loadLogDetails } from "../logDetails"; @@ -27,6 +28,7 @@ import { useLogOptions } from "../logOptions"; import { presentLogGraph } from "../logPresentation"; import { survivingRowShift } from "../logScrollAnchor"; import { nextSpeedMatch } from "../logSearch"; +import { changesDiffData, showChangesDiff, useLogChanges } from "../logChanges"; import { PAGE, extendCommitSelection, @@ -240,6 +242,24 @@ export function CommitList({ fileHistory = false }: { fileHistory?: boolean }) { const selectedSet = useMemo(() => new Set(selection), [selection]); const revealTick = useGitLog((s) => s.revealTick); const scrollRef = useRef(null); + // Show Diff with focus in the list diffs the selected commit's changes (#7810): the changes + // pane's file selection when it has one, else the commit's first file with every file in the + // chain (F7). A file-history list keeps its own diff: its context routes Show Diff to the + // target file's history (vcsLogRevisionFocus). + const commitListDiff = useMemo( + () => ({ + diff: () => { + const changes = useLogChanges.getState(); + return changesDiffData(changes.selected.length ? changes.selected : changes.all, changes.all); + }, + show: () => { + const changes = useLogChanges.getState(); + void showChangesDiff(changes.selected.length ? changes.selected : changes.all, false, changes.all); + }, + }), + [], + ); + useDiffSource(scrollRef, fileHistoryTarget ? null : commitListDiff); // While a speed search is active, F3 moves between its matches instead of toggling a bookmark. const speedSearchRef = useRef(speedSearch); speedSearchRef.current = speedSearch;