fix(vcs): diff the selected commit's changes with Show Diff from the Git Log list (#7818)

Show Diff with focus in the Git Log's commit list said "Select a changed file to show its diff": the diff actions ask the focused view for its diff source and the commit list never registered one. The list now registers the changes pane's source: the pane's file selection when it has one, else the commit's first file with every file of the commit in the chain. A file-history list keeps its file-focused diff.

Closes #7810

Co-authored-by: Inf1mum <[email protected]>
Цей коміт міститься в:
Vadim LazukтаInf1mum authored and GitHub committed 2026-12-03 23:01:01 +08:00
1 джерело 0920f189d6
коміт b9014f34cf
2 files changed
+150

No files matched your search

+130
Переглянути файл
@@ -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<typeof import("@/features/vcs/diffInput")>()),
openVcsDiff: mocks.openVcsDiff,
openDiff: mocks.openDiff,
}));
vi.mock("@/workbench/contributions", async (original) => ({
...(await original<typeof import("@/workbench/contributions")>()),
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(<CommitList />);
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(<CommitList />);
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();
});
});
+20
Переглянути файл
@@ -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<HTMLDivElement>(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;