From 033ee6060901848e684d8fbbf329ca7a63bcad1c Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Sun, 6 Dec 2026 04:24:21 +0800 Subject: [PATCH] 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. --- .../vcs/__tests__/compareTool.test.tsx | 37 +++++++++++++++++++ src/features/vcs/compareTool.tsx | 21 +++++++++-- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/features/vcs/__tests__/compareTool.test.tsx b/src/features/vcs/__tests__/compareTool.test.tsx index 2622fdd288..2d56f26b27 100644 --- a/src/features/vcs/__tests__/compareTool.test.tsx +++ b/src/features/vcs/__tests__/compareTool.test.tsx @@ -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()), + 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(); + 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(); + 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(); + 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(); fireEvent.mouseDown(await screen.findByText("old.ts"), { button: 0 }); diff --git a/src/features/vcs/compareTool.tsx b/src/features/vcs/compareTool.tsx index 42de3c71f3..bec400d66e 100644 --- a/src/features/vcs/compareTool.tsx +++ b/src/features/vcs/compareTool.tsx @@ -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([]); + // 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() { + {!active.to && ( + + )}