From ce4b2f9655bb33e566ad40562bf3e0f09bda9ca3 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Thu, 3 Dec 2026 00:41:16 +0800 Subject: [PATCH] feat(deployment): compare a file with its deployed version (#7580) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Compare with Deployed Version (default server) and Compare with Deployed Version on… (choose a server) open the diff viewer with the editable local file against a read-only snapshot of the deployed one; binary and oversized files show a notice. Fixes #7546 --- crates/ide-host/src/menu.rs | 2 + src/features/deployment/actions.test.tsx | 62 +++++++++++++++++++++++- src/features/deployment/actions.tsx | 62 +++++++++++++++++++++++- src/features/deployment/index.ts | 7 +-- src/features/deployment/model.test.ts | 10 ++++ src/features/deployment/model.ts | 10 +++- src/platform/locales/en.json | 2 + src/platform/locales/text/en.json | 3 ++ src/platform/locales/text/zh.json | 3 ++ src/platform/locales/zh.json | 2 + 10 files changed, 155 insertions(+), 8 deletions(-) diff --git a/crates/ide-host/src/menu.rs b/crates/ide-host/src/menu.rs index 854be6aae..5407e6e94 100644 --- a/crates/ide-host/src/menu.rs +++ b/crates/ide-host/src/menu.rs @@ -857,6 +857,8 @@ pub fn spec(os: Os) -> Vec { act("deployment.downloadFromDefault", "Download from Default Server"), act("deployment.downloadFrom", "Download from…"), act("deployment.syncWith", "Sync with Deployed…"), + act("deployment.compareWithDefault", "Compare with Deployed Version"), + act("deployment.compareWith", "Compare with Deployed Version on…"), act("deployment.deleteFromServer", "Delete from Server"), SEP, act("deployment.toggleAutoUpload", "Automatic Upload"), diff --git a/src/features/deployment/actions.test.tsx b/src/features/deployment/actions.test.tsx index a8be124ee..db9052d4c 100644 --- a/src/features/deployment/actions.test.tsx +++ b/src/features/deployment/actions.test.tsx @@ -12,6 +12,10 @@ const mocks = vi.hoisted(() => ({ notify: vi.fn(), upload: vi.fn(), compare: vi.fn(), + probe: vi.fn(), + remoteOpen: vi.fn(), + readText: vi.fn(), + execute: vi.fn(), sync: vi.fn(), save: vi.fn(), release: vi.fn(), @@ -25,7 +29,7 @@ vi.mock("@/editor/api", () => ({ attachDocument: async () => ({ save: mocks.save, release: mocks.release }), })); vi.mock("@/platform/actions", () => ({ - executeAction: vi.fn(), + executeAction: mocks.execute, registerActions: (actions: typeof mocks.actions) => { mocks.actions = actions; return () => {}; @@ -35,7 +39,7 @@ vi.mock("@/workbench/messages", () => ({ confirm: vi.fn() })); vi.mock("@/platform/openUrl", () => ({ openExternalUrl: vi.fn() })); vi.mock("@/platform/context", () => ({ getContext: () => ({}) })); vi.mock("@/platform/project", () => ({ - relativePath: (_: string, path: string) => path, + relativePath: (root: string, path: string) => path.replace(`${root}/`, ""), useProject: { getState: () => ({ project: { root: "/project" } }) }, })); vi.mock("@/platform/workflowSettings", () => ({ @@ -60,6 +64,9 @@ vi.mock("@/platform/ipc", () => ({ deployCoverage: async () => mocks.coverage, deployUpload: mocks.upload, deployCompare: mocks.compare, + fsProbe: mocks.probe, + remoteOpen: mocks.remoteOpen, + fsReadText: mocks.readText, deploySync: mocks.sync, }, })); @@ -90,6 +97,9 @@ beforeEach(() => { mocks.choose.mockResolvedValue(null); mocks.upload.mockResolvedValue({ files: 1, skipped: 1, errors: [] }); mocks.compare.mockResolvedValue([]); + mocks.probe.mockResolvedValue({ size: 8, binary: false, tooLarge: false }); + mocks.remoteOpen.mockResolvedValue("/cache/a"); + mocks.readText.mockResolvedValue("deployed"); mocks.sync.mockResolvedValue({ files: 1, skipped: 0, errors: [] }); mocks.save.mockResolvedValue(undefined); mocks.attachedPaths = []; @@ -299,3 +309,51 @@ describe("deployment eligibility", () => { expect(useDeployment.getState().sync?.error).toContain("Could not refresh comparison: a: Connection lost"); }); }); + +describe("Compare with Deployed Version", () => { + const entry: SyncEntry = { + path: "a", + remotePath: "/deployed/a", + local: { size: 5, mtime: 1 }, + remote: { size: 8, mtime: 2 }, + status: "remoteNewer", + }; + const compare = (id: string, paths = ["/project/a"]) => + mocks.actions.find((action) => action.id === id)!.run({ arg: paths }); + + it("opens a local file and a read-only snapshot from the default server in the standard diff viewer", async () => { + mocks.compare.mockResolvedValue([entry]); + await compare("deployment.compareWithDefault"); + expect(mocks.choose).not.toHaveBeenCalled(); + expect(mocks.compare).toHaveBeenCalledWith("/project/a", "one", 1); + expect(mocks.execute).toHaveBeenCalledWith("vcs.openDiff", { + path: "/project/a", + title: "a", + left: { type: "file", path: "/project/a", label: "Local: a" }, + right: { type: "text", text: "deployed", label: "one: /deployed/a" }, + }); + expect(mocks.sync).not.toHaveBeenCalled(); + }); + + it("compares against the server chosen for the file", async () => { + mocks.coverage[1]!.paths = ["/project/a"]; + mocks.choose.mockResolvedValue("two"); + mocks.compare.mockResolvedValue([entry]); + await compare("deployment.compareWith"); + expect(mocks.choose).toHaveBeenCalledWith({ title: "Compare with Deployed Version on…", items: ["one", "two"] }); + expect(mocks.compare).toHaveBeenCalledWith("/project/a", "two", 1); + expect(mocks.execute).toHaveBeenCalledWith( + "vcs.openDiff", + expect.objectContaining({ right: expect.objectContaining({ label: "two: /deployed/a" }) }), + ); + }); + + it("skips multiple selections, directories and a cancelled server choice", async () => { + await compare("deployment.compareWithDefault", ["/project/a", "/project/b"]); + mocks.probe.mockRejectedValueOnce(new Error("is a directory")); + await compare("deployment.compareWithDefault", ["/project/folder"]); + await compare("deployment.compareWith"); + expect(mocks.compare).not.toHaveBeenCalled(); + expect(mocks.execute).not.toHaveBeenCalled(); + }); +}); diff --git a/src/features/deployment/actions.tsx b/src/features/deployment/actions.tsx index e8e6947cb..97236c5f7 100644 --- a/src/features/deployment/actions.tsx +++ b/src/features/deployment/actions.tsx @@ -1,4 +1,4 @@ -/** Deployment actions: upload / download / delete / sync / open the deployed URL. */ +/** Deployment actions: upload, download, compare, delete, sync, and open the deployed URL. */ import { executeAction, registerActions } from "@/platform/actions"; import { attachDocument, editorsFor } from "@/editor/api"; import { getContext } from "@/platform/context"; @@ -35,6 +35,7 @@ import { withSsh } from "./ssh"; import { REMOTE_HOST_WINDOW, patchSync, useDeployment } from "./store"; import styles from "./Deployment.module.css"; import { showDeploymentComparison } from "./windowBridge"; +import { createDeploymentComparison } from "./comparison"; const GROUP = "tools"; const CATEGORY = "Deployment"; @@ -277,6 +278,49 @@ async function deleteRemote(paths: string[]) { } } +/** Opens the standard diff viewer for a single local file and its deployed version. */ +async function compareFile(arg: unknown, chooseServer: boolean) { + const root = projectRoot(); + const paths = actionTargets(arg); + const [path] = paths; + if (!root || paths.length !== 1 || !path || path === root) { + notifications().notify({ group: GROUP, level: "info", title: "Select a file to compare its contents" }); + return; + } + try { + await call(commands.fsProbe(path)); + } catch (error) { + notifications().notify({ group: GROUP, level: "error", title: "Cannot compare", body: errorText(error) }); + return; + } + if (projectRoot() !== root) return; + + const title = "Compare with Deployed Version on…"; + const server = chooseServer ? await pickServer(title, paths) : await defaultServer(title, paths); + if (!server || projectRoot() !== root) return; + try { + const entries = await withSsh(deploymentSecretKey(server), () => + call(commands.deployCompare(path, server.id, nextRequestId())), + ); + const entry = entries.find((item) => item.path === relativePath(root, path) && item.local); + if (!entry) return notifyNoCoverage(server); + const preview = await createDeploymentComparison(server, root).preview(entry, new AbortController().signal); + if (projectRoot() !== root) return; + if (preview.kind === "text") { + await executeAction("vcs.openDiff", preview.data); + } else { + notifications().notify({ + group: GROUP, + level: "info", + title: + preview.kind === "binary" ? "Binary files cannot be shown as text" : "This file is too large to compare.", + }); + } + } catch (error) { + showFailure("Cannot open comparison for {server}", server, error); + } +} + /** Compares every covered selection with `server`. */ /** Sync with Deployed to: its own window on desktop (the browser build keeps the in-page dialog). */ async function openComparison(server: DeploymentServerValid, paths: string[]) { @@ -567,6 +611,22 @@ export function registerDeploymentActions(): () => void { keywords: ["deploy", "sftp", "download", "remote"], run: withTargets(async (paths) => transfer("download", await pickServer("Download from", paths), paths)), }, + { + id: "deployment.compareWithDefault", + title: "Compare with Deployed Version", + category: CATEGORY, + when, + keywords: ["deploy", "sftp", "remote", "diff"], + run: ({ arg }) => compareFile(arg, false), + }, + { + id: "deployment.compareWith", + title: "Compare with Deployed Version on…", + category: CATEGORY, + when, + keywords: ["deploy", "sftp", "remote", "diff"], + run: ({ arg }) => compareFile(arg, true), + }, { id: "deployment.syncWith", title: "Sync with Deployed to…", diff --git a/src/features/deployment/index.ts b/src/features/deployment/index.ts index 9c613be8f..fe50b01d2 100644 --- a/src/features/deployment/index.ts +++ b/src/features/deployment/index.ts @@ -20,13 +20,14 @@ import { installDeploymentEditorHost } from "./windowBridge"; /** Deployed URL of the path the last menu was prepared for. */ let webUrlFor: { path: string; url: string | null } | null = null; -function deploymentMenu(paths: string[]): MenuItem[] { +function deploymentMenu(paths: string[], dirs?: boolean[]): MenuItem[] { const root = useProject.getState().project?.root; const { servers } = workflowSettings().deployment; if (!root || !servers.length || !paths.length) return []; if (!paths.every((p) => p === root || p.startsWith(`${root}/`))) return []; const arg = { paths }; const single = paths.length === 1 ? paths[0]! : null; + const singleFile = single !== null && single !== root && dirs?.[0] !== true; const webUrl = single && webUrlFor?.path === single ? webUrlFor.url : null; return [ { @@ -34,7 +35,7 @@ function deploymentMenu(paths: string[]): MenuItem[] { label: "Deployment", icon: "web", submenu: [ - ...transferMenuEntries(workflowSettings().deployment).map(({ action, label }): MenuItem => ({ + ...transferMenuEntries(workflowSettings().deployment, singleFile).map(({ action, label }): MenuItem => ({ kind: "action", action, arg, @@ -87,7 +88,7 @@ export function activate(): () => void { id: "deployment", menus: ["projectView", "editor", "editorTab", "changes"], order: 60, - items: (_menu, { paths }) => deploymentMenu(paths), + items: (_menu, { paths, dirs }) => deploymentMenu(paths, dirs), prepare: async (_menu, { paths }) => { const single = paths.length === 1 ? paths[0]! : null; if (!single || !workflowSettings().deployment.servers.length) return; diff --git a/src/features/deployment/model.test.ts b/src/features/deployment/model.test.ts index e52d68df3..df36bab1f 100644 --- a/src/features/deployment/model.test.ts +++ b/src/features/deployment/model.test.ts @@ -152,6 +152,16 @@ describe("servers and reports", () => { expect(defaultGroup({ ...DEFAULT_DEPLOYMENT, servers: [a, b], groups, defaultServer: "a" })).toBeNull(); }); + it("offers both file comparisons when a default server is available", () => { + const settings = { ...DEFAULT_DEPLOYMENT, servers: [a, b], defaultServer: "a" }; + expect( + transferMenuEntries(settings, true) + .map((item) => item.action) + .slice(-2), + ).toEqual(["deployment.compareWithDefault", "deployment.compareWith"]); + expect(transferMenuEntries({ ...settings, defaultServer: "" }, true).at(-1)?.action).toBe("deployment.compareWith"); + }); + it("summarizes transfer reports", () => { expect(summarizeReport({ files: 3, skipped: 1, errors: [] }, "upload", "Staging")).toEqual({ level: "info", diff --git a/src/features/deployment/model.ts b/src/features/deployment/model.ts index 9fa6aaeb1..bc224ccc1 100644 --- a/src/features/deployment/model.ts +++ b/src/features/deployment/model.ts @@ -110,7 +110,11 @@ export function defaultGroup( } /** Transfer entries of the Deployment menu: named after the default server, choosers when there are several. */ -export function transferMenuEntries(settings: DeploymentSettingsValid): { action: string; label?: string }[] { +export function transferMenuEntries( + settings: DeploymentSettingsValid, + singleFile = false, +): { action: string; label?: string }[] { + const compare = singleFile ? [{ action: "deployment.compareWith" }] : []; // A default group uploads to each of its servers; downloads and syncs pick one server. const group = defaultGroup(settings); if (group) @@ -119,6 +123,7 @@ export function transferMenuEntries(settings: DeploymentSettingsValid): { action { action: "deployment.uploadTo" }, { action: "deployment.downloadFrom" }, { action: "deployment.syncWith" }, + ...compare, ]; const target = resolveServer(settings, null); const name = target && serverLabel(target); @@ -133,7 +138,8 @@ export function transferMenuEntries(settings: DeploymentSettingsValid): { action settings.servers.length > 1 || !target ? [{ action: "deployment.uploadTo" }, { action: "deployment.downloadFrom" }, { action: "deployment.syncWith" }] : []; - return [...named, ...choosers]; + const compareDefault = singleFile && target ? [{ action: "deployment.compareWithDefault" }] : []; + return [...named, ...choosers, ...compareDefault, ...compare]; } export interface ReportSummary { diff --git a/src/platform/locales/en.json b/src/platform/locales/en.json index ba3ed91f3..ca714fe1c 100644 --- a/src/platform/locales/en.json +++ b/src/platform/locales/en.json @@ -239,6 +239,8 @@ "menu.deployment.uploadTo": "Upload to…", "menu.deployment.downloadFromDefault": "Download from Default Server", "menu.deployment.downloadFrom": "Download from…", + "menu.deployment.compareWithDefault": "Compare with Deployed Version", + "menu.deployment.compareWith": "Compare with Deployed Version on…", "menu.deployment.syncWith": "Sync with Deployed…", "menu.deployment.deleteFromServer": "Delete from Server", "menu.deployment.toggleAutoUpload": "Automatic Upload", diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 250afafc3..5bf4932c0 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -1606,6 +1606,9 @@ "Builds or starts the project's dev container (devcontainer.json) with the Dev Containers CLI and opens the project in it.": "Builds or starts the project's dev container (devcontainer.json) with the Dev Containers CLI and opens the project in it.", "devcontainer up exited with {status}": "devcontainer up exited with {status}", "Select an item to view details": "Select an item to view details", + "Compare with Deployed Version": "Compare with Deployed Version", + "Compare with Deployed Version on…": "Compare with Deployed Version on…", + "This file is too large to compare.": "This file is too large to compare.", "Select files in the Project view, Commit tool window, or open a file in the editor": "Select files in the Project view, Commit tool window, or open a file in the editor", "Nothing to show": "Nothing to show", "IDE-managed Tomcat runtime directory must not overlap Tomcat home or base directory": "IDE-managed Tomcat runtime directory must not overlap Tomcat home or base directory", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 39382915f..3873e0450 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -6331,6 +6331,9 @@ "Edit the deployment servers and options": "编辑部署服务器和选项", "Configuration…": "配置…", "No deployment server configured": "未配置部署服务器", + "Compare with Deployed Version": "与已部署版本比较", + "Compare with Deployed Version on…": "与服务器上的已部署版本比较…", + "This file is too large to compare.": "此文件过大,无法比较。", "Select files in the Project view, Commit tool window, or open a file in the editor": "请在项目视图或提交工具窗口中选择文件,或在编辑器中打开文件", "Upload to": "上传到", "Download from": "下载自", diff --git a/src/platform/locales/zh.json b/src/platform/locales/zh.json index f830b4940..157082164 100644 --- a/src/platform/locales/zh.json +++ b/src/platform/locales/zh.json @@ -239,6 +239,8 @@ "menu.deployment.uploadTo": "上传到…", "menu.deployment.downloadFromDefault": "从默认服务器下载", "menu.deployment.downloadFrom": "下载自…", + "menu.deployment.compareWithDefault": "与已部署版本比较", + "menu.deployment.compareWith": "与服务器上的已部署版本比较…", "menu.deployment.syncWith": "与已部署版本同步…", "menu.deployment.deleteFromServer": "从服务器删除", "menu.deployment.toggleAutoUpload": "自动上传",