From 7c9f343ca4ab5bc12bd2c8e7a08d59d81f44c4bf Mon Sep 17 00:00:00 2001 From: Muu Date: Sun, 29 Nov 2026 16:40:43 +0800 Subject: [PATCH] feat(vcs): open changed files at their repository revision (#6816) Adds Open Repository Version to the Git Log changed-files menu: each selected file opens read-only at its revision after the change through the existing revision editor. Deleted files and directory symlinks are skipped in both enablement and execution, read-only working-tree files still qualify, and grouped directory selections contribute their files. Includes zh and zh-TW translations. Co-authored-by: muutot <92625353+muutot@users.noreply.github.com> --- e2e/gitFileHistoryLayout.spec.ts | 27 +++++++ src/features/vcs/__tests__/logPanes.test.tsx | 77 +++++++++++++++++++- src/features/vcs/log/CommitDetails.tsx | 7 ++ src/features/vcs/logChanges.ts | 8 ++ src/features/vcs/logPaneActions.ts | 19 +++++ src/platform/locales/text/zh-TW.json | 2 + src/platform/locales/text/zh.json | 2 + 7 files changed, 140 insertions(+), 2 deletions(-) diff --git a/e2e/gitFileHistoryLayout.spec.ts b/e2e/gitFileHistoryLayout.spec.ts index 5eb6bf224..55e8c9b96 100644 --- a/e2e/gitFileHistoryLayout.spec.ts +++ b/e2e/gitFileHistoryLayout.spec.ts @@ -92,4 +92,31 @@ test("file history puts commit details below the revisions and keeps directory h await expect(earlier).toHaveAttribute("aria-selected", "true"); await expect.poll(async () => (await revisions.boundingBox())!.height).toBeCloseTo(resized, 0); await expect(details).toContainText("Add the weekly total"); + + await page.evaluate(async () => { + const servicesModule = "/src/workbench/contributions.ts"; + const { editorService } = await import(/* @vite-ignore */ servicesModule); + const service = editorService(); + service.openFile = async (path: string) => { + (window as unknown as { __revisionOpen: string }).__revisionOpen = path; + return null; + }; + }); + const changedFile = page.getByRole("tree", { name: "Changed files" }).getByRole("treeitem", { + name: "build.gradle.kts", + }); + await changedFile.click({ button: "right" }); + const openRevision = page.getByRole("menuitem", { name: "Open Repository Version" }); + await expect(openRevision).toBeEnabled(); + await openRevision.click(); + await expect + .poll(async () => + page.evaluate(async () => { + const virtualDocsModule = "/src/platform/virtualDocs.ts"; + const { gitRevisionOf } = await import(/* @vite-ignore */ virtualDocsModule); + const uri = (window as unknown as { __revisionOpen?: string }).__revisionOpen; + return uri ? gitRevisionOf(uri) : null; + }), + ) + .toEqual({ repo: "/dev/acme-api", revision: "2".padStart(40, "0"), path: "build.gradle.kts" }); }); diff --git a/src/features/vcs/__tests__/logPanes.test.tsx b/src/features/vcs/__tests__/logPanes.test.tsx index d7f3f2100..a0fede6be 100644 --- a/src/features/vcs/__tests__/logPanes.test.tsx +++ b/src/features/vcs/__tests__/logPanes.test.tsx @@ -1,15 +1,20 @@ import type { Branch, Commit } from "@/bindings"; +import { getAction } from "@/platform/actions"; import { fireEvent, render, screen, waitFor, within } from "@testing-library/react"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; const vcsCommitFiles = vi.fn(); const vcsDiffNumstat = vi.fn(); const vcsTags = vi.fn(); -const { deleteBranches } = vi.hoisted(() => ({ deleteBranches: vi.fn() })); +const { deleteBranches, openRevisionFile } = vi.hoisted(() => ({ deleteBranches: vi.fn(), openRevisionFile: vi.fn() })); vi.mock("../branchOps", async (original) => ({ ...(await original()), deleteBranches, })); +vi.mock("../revisionInput", async (original) => ({ + ...(await original()), + openRevisionFile, +})); vi.mock("@/platform/ipc", async (importOriginal) => { const real = await importOriginal(); @@ -45,7 +50,7 @@ import { useLogChanges, } from "../logChanges"; import { DEFAULT_LOG_OPTIONS, useLogOptions } from "../logOptions"; -import { canUpdateBranch, useBranchesPane } from "../logPaneActions"; +import { canUpdateBranch, registerLogPaneActions, useBranchesPane } from "../logPaneActions"; import { emptyFilters, useGitLog } from "../logStore"; import { renameFrom } from "../model"; import { useVcs } from "../store"; @@ -85,6 +90,8 @@ afterEach(() => { vcsDiffNumstat.mockReset(); vcsTags.mockReset(); deleteBranches.mockReset(); + openRevisionFile.mockReset(); + useLogChanges.setState({ commits: [], all: [], selected: [] }); }); describe("changed files model", () => { @@ -157,6 +164,72 @@ describe("changed files model", () => { expect(item).toMatchObject({ disabled: true }); }); + it("enables Open Repository Version for any file with an after revision, including read-only files", () => { + const [deleted, directory, readOnly] = toLogChanges( + [ + { path: "deleted.ts", kind: "deleted", oldPath: null }, + { path: "directory-link", kind: "added", oldPath: null, link: "dir" }, + { path: "read-only.ts", kind: "modified", oldPath: null }, + ], + commit, + "/repo", + true, + ); + const id = "vcs.log.changes.openRepositoryVersion"; + const item = (changes: ReturnType) => + changesMenu(changes).find((entry) => "action" in entry && entry.action === id); + expect(item([])).toMatchObject({ disabled: true }); + expect(item([deleted!])).toMatchObject({ disabled: true }); + expect(item([directory!])).toMatchObject({ disabled: true }); + expect(item([readOnly!])).toMatchObject({ arg: [readOnly], disabled: false }); + expect(item([deleted!, readOnly!])).toMatchObject({ disabled: false }); + + const dispose = registerLogPaneActions(); + try { + const action = getAction(id)!; + expect(action).toMatchObject({ + title: "Open Repository Version", + hidden: true, + when: "vcsAvailable || projectOpen || vcsLogFocus", + keyWhen: "vcsLogChangesFocus", + }); + expect(action.enabled!({ ctx: {}, arg: [] })).toBe(false); + expect(action.enabled!({ ctx: {}, arg: [deleted!] })).toBe(false); + expect(action.enabled!({ ctx: {}, arg: [directory!] })).toBe(false); + expect(action.enabled!({ ctx: {}, arg: [readOnly!] })).toBe(true); + } finally { + dispose(); + } + }); + + it("opens every valid after revision under selected directories and skips missing revisions", async () => { + const before = "1".repeat(40); + const firstAfter = "a".repeat(40); + const files = toLogChanges( + [ + { path: "src/a.ts", kind: "modified", oldPath: null, before, after: firstAfter }, + { path: "src/deleted.ts", kind: "deleted", oldPath: null, before, after: null }, + { path: "src/dir-link", kind: "added", oldPath: null, before: null, after: "b".repeat(40), link: "dir" }, + { path: "src/invalid.ts", kind: "modified", oldPath: null, before, after: null }, + ], + null, + "/repo", + true, + ); + // A grouped directory contributes its files, never a directory entry of its own. + const selected = filesUnder( + buildFileTree(files, (change) => change.path), + new Set(["d:src"]), + ); + const dispose = registerLogPaneActions(); + try { + await getAction("vcs.log.changes.openRepositoryVersion")!.run({ ctx: {}, arg: selected }); + } finally { + dispose(); + } + expect(openRevisionFile.mock.calls).toEqual([["/repo", firstAfter, "src/a.ts"]]); + }); + it("offers file history edits only for part of one editable commit", () => { const files = toLogChanges( [ diff --git a/src/features/vcs/log/CommitDetails.tsx b/src/features/vcs/log/CommitDetails.tsx index cf0c52a4b..f618a79b9 100644 --- a/src/features/vcs/log/CommitDetails.tsx +++ b/src/features/vcs/log/CommitDetails.tsx @@ -40,6 +40,7 @@ import { buildFileTree, dirKeys, type FileTreeNode } from "../fileTree"; import styles from "../GitLog.module.css"; import { canApplySelectedChanges, + canOpenRepositoryVersion, absolutePath, changesArg, changesDiffData, @@ -574,6 +575,12 @@ export function changesMenu(changes: LogChange[], source?: { commit: Commit; tot arg: changes, disabled: !changes.some((c) => c.after && !c.readOnly), }, + { + kind: "action", + action: "vcs.log.changes.openRepositoryVersion", + arg: changes, + disabled: !canOpenRepositoryVersion(changes), + }, { kind: "separator" }, { kind: "action", diff --git a/src/features/vcs/logChanges.ts b/src/features/vcs/logChanges.ts index 6dd9d4479..f05bf3387 100644 --- a/src/features/vcs/logChanges.ts +++ b/src/features/vcs/logChanges.ts @@ -301,6 +301,14 @@ export async function openChangeSource(change: LogChange) { await editorService().openFile(absolutePath(change.path, change.repo), { focus: true }); } +/** + * Open Repository Version: files that exist after the change. Directory rows are flattened + * to their files before actions run; readOnly prevents working-tree edits, not revision viewing. + */ +export function canOpenRepositoryVersion(changes: readonly LogChange[]): boolean { + return changes.some((change) => !!change.after && change.link !== "dir" && !!(change.repo ?? currentRepoRoot())); +} + /** * Get from Revision: the working files take their content after the change; a file the * change deleted is deleted. diff --git a/src/features/vcs/logPaneActions.ts b/src/features/vcs/logPaneActions.ts index 8b4b24908..93e022245 100644 --- a/src/features/vcs/logPaneActions.ts +++ b/src/features/vcs/logPaneActions.ts @@ -18,6 +18,7 @@ import { import { isFavoriteBranch } from "./branches"; import { applySelectedChanges, + canOpenRepositoryVersion, changesArg, compareChangesWithLocal, getFromRevision, @@ -31,6 +32,7 @@ import { logOptions } from "./logOptions"; import { goToRef, selectedCommits, toggleCherryHighlight, updateLogOptions, useGitLog } from "./logStore"; import { commitKey, commitRepo } from "./multiRoot"; import { dropSelectedChanges, extractSelectedChanges } from "./rebase"; +import { openRevisionFile } from "./revisionInput"; import { currentRepoRoot, focusRepo } from "./store"; import { showCompareBranches, type BranchComparisonTarget, type BranchComparisonData } from "./CompareBranches"; import { showCompareRevisions } from "./operations"; @@ -282,6 +284,23 @@ export function registerLogPaneActions(): () => void { for (const c of changesArg(arg)) if (c.after) void openChangeSource(c); }, }, + { + id: "vcs.log.changes.openRepositoryVersion", + title: "Open Repository Version", + description: "Open the selected files as they exist after the change", + category: "Git Log", + when: LOG, + hidden: true, + keyWhen: "vcsLogChangesFocus", + enabled: ({ arg }) => canOpenRepositoryVersion(changesArg(arg)), + run: ({ arg }) => { + for (const change of changesArg(arg)) { + if (!change.after || change.link === "dir") continue; + const repo = change.repo ?? currentRepoRoot(); + if (repo) openRevisionFile(repo, change.after, change.path); + } + }, + }, { id: "vcs.log.changes.getFromRevision", title: "Get from Revision", diff --git a/src/platform/locales/text/zh-TW.json b/src/platform/locales/text/zh-TW.json index 342dbd1c1..71ceaf11e 100644 --- a/src/platform/locales/text/zh-TW.json +++ b/src/platform/locales/text/zh-TW.json @@ -5840,6 +5840,8 @@ "Show commit time rather than author time": "顯示提交時間而非作者時間", "Show references on the left": "在左側顯示引用", "Visible columns": "可見欄", + "Open Repository Version": "開啟存放庫版本", + "Open the selected files as they exist after the change": "開啟所選檔案在該變更之後的版本", "Get from Revision": "從修訂取得", "Replace '{path}' with its content from the revision? Local changes to it are lost.": "用修訂中的內容取代「{path}」?其本機更改將遺失。", "Replace {length} files with their content from the revision? Local changes to them are lost.": "用修訂中的內容取代 {length} 個檔案?它們的本機更改將遺失。", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index ef8b1ecd8..d0abfa44a 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -6658,6 +6658,8 @@ "Show commit time rather than author time": "显示提交时间而非作者时间", "Show references on the left": "在左侧显示引用", "Visible columns": "可见列", + "Open Repository Version": "打开仓库版本", + "Open the selected files as they exist after the change": "打开所选文件在该更改之后的版本", "Get from Revision": "从修订获取", "Replace '{path}' with its content from the revision? Local changes to it are lost.": "用修订中的内容替换“{path}”?其本地更改将丢失。", "Replace {length} files with their content from the revision? Local changes to them are lost.": "用修订中的内容替换 {length} 个文件?它们的本地更改将丢失。",