From 61ba70394f8ba81043a54311d82b0b15d3f8cac6 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Tue, 17 Nov 2026 15:05:41 +0800 Subject: [PATCH] fix(vcs): don't open an empty blame column for a file Git has no commit of (#3927) Annotate with Git Blame on an unversioned, ignored, or added-but-not-committed file opened a wide, empty annotation column: the backend blames every line of a file missing from HEAD as not committed. The action is now disabled for files the status lists as untracked, ignored or added, as in IDEA, and a blame without a single committed line leaves the column closed with a notification that the file has no committed history. Fixes #3900 --- .../vcs/__tests__/annotateEnabled.test.ts | 34 +++++++++++++++++++ src/features/vcs/__tests__/blame.test.tsx | 24 +++++++++++-- src/features/vcs/actions.tsx | 21 +++++++++++- src/features/vcs/blame.ts | 10 ++++++ src/platform/locales/text/en.json | 4 ++- src/platform/locales/text/zh.json | 4 ++- 6 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 src/features/vcs/__tests__/annotateEnabled.test.ts diff --git a/src/features/vcs/__tests__/annotateEnabled.test.ts b/src/features/vcs/__tests__/annotateEnabled.test.ts new file mode 100644 index 000000000..30ee43c3e --- /dev/null +++ b/src/features/vcs/__tests__/annotateEnabled.test.ts @@ -0,0 +1,34 @@ +import { afterEach, describe, expect, it } from "vitest"; +import { getAction, isActionEnabled } from "@/platform/actions"; +import { registerVcsActions } from "../actions"; +import { useVcs } from "../store"; + +afterEach(() => useVcs.setState({ byPath: new Map() })); + +const status = (path: string, staged: string | null, unstaged: string | null) => + [path, { path, relPath: path.slice(6), staged, unstaged, renamedFrom: null }] as const; + +describe("Annotate with Git Blame", () => { + it("is off for a file Git has no commit of, as in IDEA", () => { + const unregister = registerVcsActions(); + try { + useVcs.setState({ + byPath: new Map([ + status("/repo/new.py", null, "untracked"), + status("/repo/ignored_test.py", null, "ignored"), + status("/repo/added.py", "added", null), + status("/repo/edited.py", null, "modified"), + ]) as never, + }); + const action = getAction("vcs.annotate")!; + const enabled = (path: string) => isActionEnabled(action, { vcsAvailable: true }, path); + expect(enabled("/repo/new.py")).toBe(false); + expect(enabled("/repo/ignored_test.py")).toBe(false); + expect(enabled("/repo/added.py")).toBe(false); + expect(enabled("/repo/edited.py")).toBe(true); + expect(enabled("/repo/clean.py")).toBe(true); + } finally { + unregister(); + } + }); +}); diff --git a/src/features/vcs/__tests__/blame.test.tsx b/src/features/vcs/__tests__/blame.test.tsx index 9445360bc..b26832556 100644 --- a/src/features/vcs/__tests__/blame.test.tsx +++ b/src/features/vcs/__tests__/blame.test.tsx @@ -4,11 +4,15 @@ import { act, fireEvent, screen } from "@testing-library/react"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import type { BlameLine, Commit } from "@/bindings"; import { useI18n } from "@/platform/i18n"; -import { blameSlot, toggleAnnotations } from "../blame"; +import { annotationsShown, blameSlot, toggleAnnotations } from "../blame"; import { closeBlamePopup } from "../BlamePopup"; import { useVcs } from "../store"; -const mocks = vi.hoisted(() => ({ blame: vi.fn(), log: vi.fn(), action: vi.fn() })); +const mocks = vi.hoisted(() => ({ blame: vi.fn(), log: vi.fn(), action: vi.fn(), notify: vi.fn() })); +vi.mock("@/workbench/contributions", async (original) => ({ + ...(await original()), + notifications: () => ({ notify: mocks.notify, expire: vi.fn() }), +})); vi.mock("@/platform/actions", async (original) => ({ ...(await original()), executeAction: mocks.action, @@ -60,6 +64,22 @@ afterEach(() => { }); describe("git blame gutter and commit popup", () => { + it("opens no empty column for a file Git has no commit of", async () => { + const zero = "0".repeat(40); + mocks.blame.mockResolvedValue({ + status: "ok", + data: [1, 2].map(() => ({ commit: zero, author: "Not Committed Yet", timeMs: 0, summary: "" })), + }); + view = new EditorView({ + parent: document.body, + state: EditorState.create({ doc: "a\nb", extensions: [blameSlot()] }), + }); + await toggleAnnotations(view, "/repo/ignored_test.py"); + expect(annotationsShown(view)).toBe(false); + expect(view.dom.querySelector(".cm-vcs-blame .cm-gutterElement [data-commit]")).toBeNull(); + expect(mocks.notify).toHaveBeenCalledWith(expect.objectContaining({ title: "Nothing to annotate" })); + }); + it("highlights matching commit lines on hover, shows the commit popup, and jumps to Git Log on click", async () => { view = new EditorView({ parent: document.body, diff --git a/src/features/vcs/actions.tsx b/src/features/vcs/actions.tsx index 414f95521..9c4bfa54a 100644 --- a/src/features/vcs/actions.tsx +++ b/src/features/vcs/actions.tsx @@ -59,7 +59,7 @@ import { showRebaseDialog, showWorktrees, } from "./gitMenuDialogs"; -import { currentRepo, refreshNow, repoName, repoOf, useVcs } from "./store"; +import { currentRepo, refreshNow, repoName, repoOf, statusOf, useVcs } from "./store"; import { showSyncCommitsForCurrent } from "./SyncCommitsPopup"; import { tx } from "@/platform/i18n"; @@ -76,6 +76,16 @@ function argPath(arg: unknown): string | undefined { return undefined; } +/** Whether Git has no commit of the file: unversioned, ignored, or added and not committed yet. */ +function hasNoCommits(status: { staged: string | null; unstaged: string | null }): boolean { + return ( + status.staged === "added" || + status.unstaged === "untracked" || + status.unstaged === "ignored" || + status.staged === "ignored" + ); +} + function argPaths(arg: unknown): string[] | undefined { if (Array.isArray(arg)) return arg.filter((x): x is string => typeof x === "string"); const p = argPath(arg); @@ -559,6 +569,15 @@ export function registerVcsActions(): () => void { const ed = activeEditor(); return !!ed && annotationsShown(ed.view); }, + // As in IDEA, a file Git has no commit of (unversioned, ignored, or added and not yet + // committed) has nothing to annotate; shown annotations can always be closed. + enabled: ({ arg }) => { + const ed = activeEditor(); + const path = argPath(arg) ?? ed?.ctx.path; + if (!path || (ed?.ctx.path === path && annotationsShown(ed.view))) return true; + const status = statusOf(path); + return !status || !hasNoCommits(status); + }, run: ({ arg }) => annotate(arg), }, { diff --git a/src/features/vcs/blame.ts b/src/features/vcs/blame.ts index 2be3be8de..02c625642 100644 --- a/src/features/vcs/blame.ts +++ b/src/features/vcs/blame.ts @@ -352,6 +352,16 @@ export async function toggleAnnotations(view: EditorView, path: string, text?: s } try { const lines = await call(commands.vcsBlame(path, text ?? null, revision ?? null)); + // A file Git has no commit of (unversioned, ignored, added) would get an empty column. + if (revision === undefined && lines.every(isUncommitted)) { + notifications().notify({ + group: "vcs", + level: "info", + title: "Nothing to annotate", + body: "The file has no committed history in Git.", + }); + return; + } setAnnotations(view, lines, path, text === undefined); } catch (e) { notifications().notify({ diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index d5312ac82..bc1959399 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -2353,5 +2353,7 @@ "Adjust colors for red-green vision deficiency is on, so this scheme shows its red-green colors.": "Adjust colors for red-green vision deficiency is on, so this scheme shows its red-green colors.", "Appearance settings": "Appearance settings", "No such file or folder": "No such file or folder", - "Choose a folder": "Choose a folder" + "Choose a folder": "Choose a folder", + "Nothing to annotate": "Nothing to annotate", + "The file has no committed history in Git.": "The file has no committed history in Git." } diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index fcea21947..cdf9a65c2 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -8733,5 +8733,7 @@ "Adjust colors for red-green vision deficiency is on, so this scheme shows its red-green colors.": "已开启「针对红绿色觉缺陷调整颜色」,此方案显示的是红绿色觉变体的颜色。", "Appearance settings": "外观设置", "No such file or folder": "文件或文件夹不存在", - "Choose a folder": "请选择文件夹" + "Choose a folder": "请选择文件夹", + "Nothing to annotate": "没有可注解的内容", + "The file has no committed history in Git.": "此文件在 Git 中没有已提交的历史。" }