diff --git a/docs/en/guide/vcs.mdx b/docs/en/guide/vcs.mdx index 30bf5fc0f6..8287aea8f8 100644 --- a/docs/en/guide/vcs.mdx +++ b/docs/en/guide/vcs.mdx @@ -248,6 +248,7 @@ Turn on **Combine stashes and shelves in one tab** in the Git settings to show b - **Annotate with Git Blame** shows author and date per line in the gutter (also from the gutter's context menu). Click an entry to open the commit in the log. **Show inline blame for the current line** in the Git settings shows the author at the end of the caret line; a click on it opens the commit unless **Open the commit when the inline blame is clicked** is off. - **Options › Names** in the gutter's context menu shows authors as **Initials**, **Last Name** (the default), **First Name** or **Full Name** in every annotated editor. +- **Annotate Revision** in an annotated line's menu opens the file as it was at that commit, annotated, next to the working copy. A file renamed since then is read under its name at that commit. - **Annotate Previous Revision** in an annotated line's menu opens the file as it was before that commit, annotated, next to the commit's version. - **Annotate at Revision…** picks any revision the file has and shows it annotated next to the working copy. - **Show History** opens the file's history in a tab of the Git tool window. diff --git a/docs/zh/guide/vcs.mdx b/docs/zh/guide/vcs.mdx index 20baa49380..d8b0f8ba62 100644 --- a/docs/zh/guide/vcs.mdx +++ b/docs/zh/guide/vcs.mdx @@ -246,6 +246,7 @@ Git 工具窗口的 **日志** 标签页显示提交图。 - **使用 Git Blame 注解** 在边栏中逐行显示作者和日期(也可以从边栏右键菜单打开)。点击某条记录会在日志中打开对应提交。在 Git 设置中打开 **为当前行显示内联 Git Blame**,会在光标所在行末尾显示作者;点击它会打开对应提交,关闭 **点击内联 Git Blame 时打开提交** 后则不会。 - 边栏右键菜单的 **选项 › 姓名** 可让所有已注解的编辑器以 **首字母缩写**、**姓**(默认)、**名** 或 **全名**显示作者。 +- 注解行的右键菜单中,**注解此修订版** 会打开该提交时的文件(已注解),与工作区文件并排显示。此后重命名过的文件按它在该提交中的名称读取。 - 注解行的右键菜单中,**标注上一版本** 会打开该提交之前版本的文件(已注解),与这个提交的版本并排显示。 - **标注指定版本…** 可选择文件的任意版本,把该版本(已注解)与工作区文件并排显示。 - **显示历史记录** 在 Git 工具窗口的标签页中打开文件历史。 diff --git a/src/features/vcs/__tests__/annotateRevision.test.ts b/src/features/vcs/__tests__/annotateRevision.test.ts index d2b76c6b00..61b79b6ae3 100644 --- a/src/features/vcs/__tests__/annotateRevision.test.ts +++ b/src/features/vcs/__tests__/annotateRevision.test.ts @@ -4,6 +4,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import type { Commit } from "@/bindings"; import { annotateAtRevision, + annotateLineRevision, annotatePreviousRevision, previousRevisionDiffData, revisionDiffData, @@ -136,6 +137,35 @@ describe("annotate at a chosen revision", () => { }); }); +describe("annotate the line's revision from the annotate gutter", () => { + const left = (path: string) => ({ type: "rev", rev: sha, label: "f122f5af", path }); + + it("shows the file at the line's commit against the working file", async () => { + await annotateLineRevision("/repo/a.py", sha); + expect(mocks.log).not.toHaveBeenCalled(); + expect(mocks.openDiff).toHaveBeenCalledWith( + { path: "/repo/a.py", left: left("/repo/a.py"), right: { type: "worktree" }, annotateLeft: true }, + { preview: false }, + ); + }); + + it("reads the file under the name it had at that commit", async () => { + await annotateLineRevision("/repo/a.py", sha, "/repo/old/a.py"); + expect(mocks.openDiff).toHaveBeenCalledWith( + expect.objectContaining({ path: "/repo/a.py", left: left("/repo/old/a.py") }), + { preview: false }, + ); + }); + + it("tells when the revision cannot be read", async () => { + mocks.openDiff.mockRejectedValue(new Error("bad revision")); + await annotateLineRevision("/repo/a.py", sha, "/repo/a.py"); + expect(mocks.notify).toHaveBeenCalledWith( + expect.objectContaining({ level: "error", title: "Cannot annotate", body: "bad revision" }), + ); + }); +}); + describe("show diff from the annotate gutter", () => { const absent = (label: string) => ({ type: "text", text: "", label, absent: true }); diff --git a/src/features/vcs/__tests__/blame.test.tsx b/src/features/vcs/__tests__/blame.test.tsx index d57dedb094..38a6f801d8 100644 --- a/src/features/vcs/__tests__/blame.test.tsx +++ b/src/features/vcs/__tests__/blame.test.tsx @@ -182,6 +182,7 @@ describe("git blame gutter and commit popup", () => { "Copy Revision Number", undefined, "Annotate at Revision…", + "Annotate Revision", "Annotate Previous Revision", "Show Commit in Git Log", "Ignore This Commit", @@ -201,6 +202,35 @@ describe("git blame gutter and commit popup", () => { ); }); + it.each([ + [undefined, "/repo/src/main.py"], + ["/repo/src/original.py", "/repo/src/original.py"], + ])("annotates the file as of the line's commit (origin %s)", async (originPath, revisionPath) => { + mocks.blame.mockResolvedValue({ status: "ok", data: lines.map((line) => ({ ...line, originPath })) }); + view = new EditorView({ + parent: document.body, + state: EditorState.create({ doc: "first\nsecond\nthird", extensions: [blameSlot()] }), + }); + await toggleAnnotations(view, "/repo/src/main.py"); + const firstLine = [...view.dom.querySelectorAll(".cm-vcs-blame .cm-gutterElement")].find((el) => + el.querySelector(`[data-commit="${sha}"]`), + )!; + fireEvent.contextMenu(firstLine); + const items = mocks.menu.mock.calls[0]![1] as { label?: string; run?: () => void }[]; + items.find((i) => i.label === "Annotate Revision")!.run!(); + await vi.waitFor(() => expect(mocks.openDiff).toHaveBeenCalled()); + expect(mocks.log).not.toHaveBeenCalled(); + expect(mocks.openDiff).toHaveBeenCalledWith( + { + path: "/repo/src/main.py", + left: { type: "rev", rev: sha, label: sha.slice(0, 8), path: revisionPath }, + right: { type: "worktree" }, + annotateLeft: true, + }, + { preview: false }, + ); + }); + it("offers no Show Diff on a line that is not committed yet", async () => { const zero = "0".repeat(40); mocks.blame.mockResolvedValue({ diff --git a/src/features/vcs/annotateRevision.ts b/src/features/vcs/annotateRevision.ts index 0bf6346eae..e5a4eee9fb 100644 --- a/src/features/vcs/annotateRevision.ts +++ b/src/features/vcs/annotateRevision.ts @@ -69,6 +69,24 @@ export async function annotateAtRevision( } } +/** + * The annotate gutter's Annotate Revision (IDEA's): the file as of the line's commit, annotated. + * `origin` is the file's path at that commit when it had another name. + */ +export async function annotateLineRevision(path: string, commit: string, origin?: string | null): Promise { + const data: DiffData = { + path, + left: { type: "rev", rev: commit, label: commit.slice(0, 8), path: origin ?? path }, + right: { type: "worktree" }, + annotateLeft: true, + }; + try { + await openDiff(data, { preview: false }); + } catch (e) { + notifyFailure(e); + } +} + /** * Opens the previous revision of `commit`, annotated: the line's history before the change. The * diff opens at its first change: the annotated line's number belongs to the working copy, not to diff --git a/src/features/vcs/blame.ts b/src/features/vcs/blame.ts index 0a3cfc61f6..0599d40669 100644 --- a/src/features/vcs/blame.ts +++ b/src/features/vcs/blame.ts @@ -560,6 +560,14 @@ function blameExtension(lines: BlameLine[], source: BlameSource): Extension { icon: "clock", run: () => void import("./FileRevisionChooser").then((m) => m.annotateFileRevision(path)), }, + { + label: tx("Annotate Revision"), + icon: "history", + run: () => + void import("./annotateRevision").then((m) => + m.annotateLineRevision(path, l.commit, l.originPath), + ), + }, { label: tx("Annotate Previous Revision"), icon: "history", diff --git a/src/platform/locales/text/ru.json b/src/platform/locales/text/ru.json index 392743a049..43dcb208f6 100644 --- a/src/platform/locales/text/ru.json +++ b/src/platform/locales/text/ru.json @@ -1153,6 +1153,7 @@ "Analyze Stack Trace": "Анализировать трассировку стека", "Analyze Stack Trace or Thread Dump…": "Анализировать трассировку стека или дамп потоков…", "Analyze code": "Анализировать код", + "Annotate Revision": "Аннотировать ревизию", "Annotate Previous Revision": "Аннотировать предыдущую ревизию", "Annotate at Revision…": "Аннотировать в ревизии…", "Annotate with Git Blame": "Аннотировать с помощью Git Blame", diff --git a/src/platform/locales/text/zh-TW.json b/src/platform/locales/text/zh-TW.json index dae702baac..225376d734 100644 --- a/src/platform/locales/text/zh-TW.json +++ b/src/platform/locales/text/zh-TW.json @@ -850,6 +850,7 @@ "Analyze Stack Trace": "分析堆疊跟蹤", "Analyze Stack Trace or Thread Dump…": "分析堆疊跟蹤或執行緒傾印…", "Analyze code": "分析程式碼", + "Annotate Revision": "註解此修訂版", "Annotate Previous Revision": "註解上一修訂版", "Annotate at Revision…": "註解指定修訂版…", "Annotate with Git Blame": "使用 Git Blame 註解", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 74662062f9..11bcc3fe01 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -1184,6 +1184,7 @@ "Analyze Stack Trace": "分析堆栈跟踪", "Analyze Stack Trace or Thread Dump…": "分析堆栈跟踪或线程转储…", "Analyze code": "分析代码", + "Annotate Revision": "注解此修订版", "Annotate Previous Revision": "注解上一修订版", "Annotate at Revision…": "注解指定修订版…", "Annotate with Git Blame": "使用 Git Blame 注解",