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 <[email protected]>
Цей коміт міститься в:
Muuтаmuutot authored and GitHub committed 2026-11-29 16:40:43 +08:00
1 джерело d815a8ef44
коміт 7c9f343ca4
7 files changed
+140 -2

No files matched your search

+27
Переглянути файл
@@ -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" });
});
+75 -2
Переглянути файл
@@ -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<typeof import("../branchOps")>()),
deleteBranches,
}));
vi.mock("../revisionInput", async (original) => ({
...(await original<typeof import("../revisionInput")>()),
openRevisionFile,
}));
vi.mock("@/platform/ipc", async (importOriginal) => {
const real = await importOriginal<typeof import("@/platform/ipc")>();
@@ -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<typeof toLogChanges>) =>
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(
[
+7
Переглянути файл
@@ -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",
+8
Переглянути файл
@@ -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.
+19
Переглянути файл
@@ -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",
+2
Переглянути файл
@@ -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} 個檔案?它們的本機更改將遺失。",
+2
Переглянути файл
@@ -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} 个文件?它们的本地更改将丢失。",