diff --git a/src/features/vcs/__tests__/logPanes.test.tsx b/src/features/vcs/__tests__/logPanes.test.tsx index 269cea458..aabfee7cb 100644 --- a/src/features/vcs/__tests__/logPanes.test.tsx +++ b/src/features/vcs/__tests__/logPanes.test.tsx @@ -1,11 +1,13 @@ import type { Branch, Commit } from "@/bindings"; import { getAction } from "@/platform/actions"; +import { maybeEditorService, provideEditorService } from "@/workbench/contributions"; 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 fsEntryKind = vi.fn(); const { deleteBranches, openRevisionFile } = vi.hoisted(() => ({ deleteBranches: vi.fn(), openRevisionFile: vi.fn() })); vi.mock("../branchOps", async (original) => ({ ...(await original()), @@ -22,6 +24,7 @@ vi.mock("@/platform/ipc", async (importOriginal) => { ...real, commands: { ...real.commands, + fsEntryKind: (path: string) => fsEntryKind(path), vcsCommitFiles: (repo: unknown, id: string) => vcsCommitFiles(repo, id), vcsDiffNumstat: (repo: unknown, from: string, to: string, paths: string[]) => vcsDiffNumstat(repo, from, to, paths), @@ -48,6 +51,7 @@ import { flatFileNodes, lineStatRanges, logPatchRanges, + openChangeSource, toLogChanges, useLogChanges, } from "../logChanges"; @@ -91,6 +95,7 @@ afterEach(() => { vcsCommitFiles.mockReset(); vcsDiffNumstat.mockReset(); vcsTags.mockReset(); + fsEntryKind.mockReset(); deleteBranches.mockReset(); openRevisionFile.mockReset(); useLogChanges.setState({ commits: [], all: [], selected: [] }); @@ -238,6 +243,29 @@ describe("changed files model", () => { expect(item).toMatchObject({ disabled: true }); }); + it("jumps only to existing working files from the Git Log", async () => { + const paths = ["docs/old-guide.md", "docs/guide.md", "replaced-by-directory.md"]; + const changes = toLogChanges( + paths.map((path) => ({ path, kind: "added", oldPath: null })), + commit, + "/repo", + ); + fsEntryKind + .mockResolvedValueOnce({ status: "error", error: { kind: "notFound", message: "File does not exist" } }) + .mockResolvedValueOnce({ status: "ok", data: "file" }) + .mockResolvedValueOnce({ status: "ok", data: "dir" }); + const previousService = maybeEditorService(); + const openFile = vi.fn(async () => {}); + provideEditorService({ ...previousService!, openFile }); + try { + await Promise.all(changes.map(openChangeSource)); + } finally { + provideEditorService(previousService); + } + expect(fsEntryKind.mock.calls).toEqual(paths.map((path) => [`/repo/${path}`])); + expect(openFile.mock.calls).toEqual([[`/repo/${paths[1]}`, { focus: true }]]); + }); + it("enables Open Repository Version for any file with an after revision, including read-only files", () => { const [deleted, directory, readOnly] = toLogChanges( [ diff --git a/src/features/vcs/logChanges.ts b/src/features/vcs/logChanges.ts index 58a871bf0..09608d894 100644 --- a/src/features/vcs/logChanges.ts +++ b/src/features/vcs/logChanges.ts @@ -358,7 +358,9 @@ export async function compareChangesWithLocal(changes: readonly LogChange[], sid /** Jump to Source: the working file. */ export async function openChangeSource(change: LogChange) { if (change.readOnly) return; - await editorService().openFile(absolutePath(change.path, change.repo), { focus: true }); + const path = absolutePath(change.path, change.repo); + const kind = await call(commands.fsEntryKind(path)).catch(() => null); + if (kind === "file") await editorService().openFile(path, { focus: true }); } /**