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
This commit is contained in:
l0ng-ai
2026-11-17 15:05:41 +08:00
committed by GitHub
parent bbc5412605
commit 61ba70394f
6 changed files with 92 additions and 5 deletions
@@ -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();
}
});
});
+22 -2
View File
@@ -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<typeof import("@/workbench/contributions")>()),
notifications: () => ({ notify: mocks.notify, expire: vi.fn() }),
}));
vi.mock("@/platform/actions", async (original) => ({
...(await original<typeof import("@/platform/actions")>()),
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,
+20 -1
View File
@@ -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),
},
{
+10
View File
@@ -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({
+3 -1
View File
@@ -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."
}
+3 -1
View File
@@ -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 中没有已提交的历史。"
}