feat(vcs): get files from the revision in the Compare tool window

Show Diff with Working Tree from the Branches pane opens the comparison in the Compare tool window, which had no way to take a file from the compared revision; only the Compare with Branch dialog had Get. A working-tree comparison's toolbar now has Get too: the selected file takes its content from the revision after the same confirmation, and the list reloads.
Цей коміт міститься в:
l0ng-ai committed 2026-12-06 04:24:21 +08:00
1 джерело 201ab6f54b
коміт 033ee60609
2 files changed
+55 -3

No files matched your search

+37
Переглянути файл
@@ -12,6 +12,11 @@ const mocks = vi.hoisted(() => ({
diffWindow: vi.fn(),
openComparison: vi.fn(),
comparePaths: vi.fn(),
getFiles: vi.fn(),
}));
vi.mock("../logChanges", async (original) => ({
...(await original<typeof import("../logChanges")>()),
getFilesFromRevision: mocks.getFiles,
}));
vi.mock("../DiffWindow", () => ({ showDiffWindow: mocks.diffWindow }));
vi.mock("@/platform/ipc", async (original) => {
@@ -286,6 +291,38 @@ describe("CompareToolWindow", () => {
expect(mocks.openDiff).toHaveBeenLastCalledWith(expect.objectContaining({ path: "/workspace/client/src/app.ts" }));
});
it("gets the selected file from the compared revision and reloads the list", async () => {
mocks.getFiles.mockResolvedValue(true);
render(<CompareToolWindow />);
const get = screen.getByRole("button", { name: "Get" });
expect(get).toBeDisabled();
fireEvent.mouseDown(await screen.findByText("app.ts"), { button: 0 });
expect(get).toBeEnabled();
expect(mocks.worktree).toHaveBeenCalledTimes(1);
fireEvent.click(get);
await waitFor(() => expect(mocks.worktree).toHaveBeenCalledTimes(2));
expect(mocks.getFiles).toHaveBeenCalledWith([
{ path: "/workspace/client/src/app.ts", from: { path: "/workspace/client/src/app.ts", rev: "main" } },
]);
expect(screen.getByRole("button", { name: "Get" })).toBeDisabled();
});
it("keeps the list when Get is cancelled, and has no Get between two revisions", async () => {
mocks.getFiles.mockResolvedValue(false);
render(<CompareToolWindow />);
fireEvent.mouseDown(await screen.findByText("app.ts"), { button: 0 });
fireEvent.click(screen.getByRole("button", { name: "Get" }));
await waitFor(() => expect(mocks.getFiles).toHaveBeenCalledOnce());
expect(mocks.worktree).toHaveBeenCalledTimes(1);
expect(screen.getByRole("button", { name: "Get" })).toBeEnabled();
cleanup();
mocks.revisions.mockResolvedValue({ status: "ok", data: [modified] });
act(() => useCompareTool.getState().show(from, { rev: "next", label: "next" }));
render(<CompareToolWindow />);
expect(await screen.findByText("app.ts")).toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Get" })).toBeNull();
});
it("has no Jump to Source for a file deleted from the working tree", async () => {
render(<CompareToolWindow />);
fireEvent.mouseDown(await screen.findByText("old.ts"), { button: 0 });
+18 -3
Переглянути файл
@@ -1,6 +1,6 @@
/**
* The Compare tool window: `Show Diff with Working Tree` shows the worktree diff
* in the sidebar instead of a dialog, with Jump to Source and Swap Sides (#2442).
* in the sidebar instead of a dialog, with Jump to Source, Get and Swap Sides (#2442).
* Branch and revision comparisons open here too (one closable tab per comparison,
* titled after what is compared, as in IDEA's VCS changes tool window): Compare with
* Branch…/Revision… on a folder or multi-selection, two-commit Compare Versions from
@@ -8,7 +8,7 @@
* file keeps its diff editor tab, and Compare with Current keeps the two-pane
* BranchComparisonEditor.
*/
import { useCallback, useEffect, useRef } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import { create } from "zustand";
import { Button, Tabs, showContextMenu } from "@/ui";
import { tx } from "@/platform/i18n";
@@ -17,9 +17,11 @@ import { maybeEditorService, notifications, registerToolWindow, type OpenEditorT
import { showToolWindow } from "@/workbench/toolwindows/store";
import { COMBINED_DIFF_KIND, type CombinedDiffData } from "./allInOneDiff";
import { combinedDiffInput, DIFF_KIND, diffInput, openVcsDiff, withChain, type DiffData } from "./diffInput";
import { getFilesFromRevision } from "./logChanges";
import {
ChangesBetweenDialog,
comparisonDiffData,
comparisonRevisionFiles,
type ComparisonFile,
type Revision,
type ScopedComparison,
@@ -190,6 +192,8 @@ export function CompareToolWindow() {
const close = useCompareTool((s) => s.close);
const active = tabs.find((tab) => tab.id === activeId) ?? null;
const filesRef = useRef<readonly ComparisonFile[]>([]);
// Bumped after Get so the list reloads without the files that now match the revision.
const [reloads, setReloads] = useState(0);
const handleFiles = useCallback((files: readonly ComparisonFile[]) => {
filesRef.current = files;
}, []);
@@ -201,6 +205,12 @@ export function CompareToolWindow() {
if (!current || current.kind === "deleted") return;
void maybeEditorService()?.openFile(repositoryPath(current.root, current.path), { focus: true });
};
// Against the working tree only: the selected file takes its content from the compared revision.
const getFile = async () => {
if (!current || !(await getFilesFromRevision(comparisonRevisionFiles(current)))) return;
select(null);
setReloads((n) => n + 1);
};
const showDiff = (data: DiffData) => {
void openVcsDiff(data).catch((error) =>
notifications().notify({ level: "error", title: "Could not show diff", body: String(error) }),
@@ -269,13 +279,18 @@ export function CompareToolWindow() {
<Button size="small" onClick={jumpToSource} disabled={!current || current.kind === "deleted"}>
Jump to Source
</Button>
{!active.to && (
<Button size="small" onClick={() => void getFile()} disabled={!current}>
Get
</Button>
)}
<Button size="small" variant={swapped ? "primary" : "secondary"} onClick={handleSwapSides}>
Swap Sides
</Button>
</div>
<div className={styles.comparisonWindowBody}>
<ChangesBetweenDialog
key={active.id}
key={`${active.id}:${reloads}`}
from={active.from}
to={active.to}
scope={active.scope}