diff --git a/docs/en/guide/vcs.mdx b/docs/en/guide/vcs.mdx index 1cb089dbbe..2ab616a9cf 100644 --- a/docs/en/guide/vcs.mdx +++ b/docs/en/guide/vcs.mdx @@ -202,7 +202,7 @@ The **Log** tab of the Git tool window shows the commit graph. - The details pane shows the changed files, the commit message, who committed it, which branches contain it and its signature. - In the **Signature** column, hover over a badge to see the verification result, signer, and key fingerprint when available. After changing your verification keys or trust settings, click **Refresh** in the log toolbar to check again. -Right-click a commit for **Copy Revision Number** (`⌥⇧⌘C` / `Ctrl+Alt+Shift+C`), **Create Patch…**, **Cherry-Pick**, **Checkout Revision**, **Show Repository at Revision**, **Compare with Local**, **Reset Current Branch to Here…**, **Revert Commit**, **Undo Commit…**, **New Branch…**, **New Tag…**, **View in Browser** and more. With two commits selected, **Compare Versions** diffs them. With the commit list focused, `F2` edits the commit message, `⌥⌘N` / `Ctrl+Alt+N` creates a branch, `←` / `→` go to the child or parent commit, and `F3` / `F11` toggles a commit bookmark (`⌥F3` / `Ctrl+F11` with a mnemonic). +Right-click a commit for **Copy Revision Number** (`⌥⇧⌘C` / `Ctrl+Alt+Shift+C`), **Create Patch…**, **Cherry-Pick**, **Checkout Revision**, **Show Repository at Revision**, **Compare with Local**, **Reset Current Branch to Here…**, **Revert Commit**, **Undo Commit…**, **New Branch…**, **New Tag…**, **View in Browser** and more. With two commits selected, **Compare Versions** diffs them. With the commit list focused, `F2` edits the commit message, `⌥⌘N` / `Ctrl+Alt+N` creates a branch, `←` / `→` go to the child or parent commit, and `F3` / `F11` toggles a commit bookmark (`⌥F3` / `Ctrl+F11` with a mnemonic). **Copy Commit Subject**, run from **Find Action** or a shortcut you assign in **Settings › Keymap**, copies the subject lines of the selected commits, one per line. In the changed-files pane, use **Apply Selected Changes** to bring only the selected files' changes into the working tree. Use **Extract Selected Changes** on part of a commit to move those files' changes into a new commit immediately after it. The operation preserves the tip's tree, local edits, and staged changes. The success notification offers **Undo** while HEAD still points to the rewritten tip. diff --git a/docs/zh/guide/vcs.mdx b/docs/zh/guide/vcs.mdx index f89dd94699..fe423f239b 100644 --- a/docs/zh/guide/vcs.mdx +++ b/docs/zh/guide/vcs.mdx @@ -200,7 +200,7 @@ Git 工具窗口的 **日志** 标签页显示提交图。 - 详情面板显示变更文件、提交消息、提交者、包含该提交的分支以及签名。 - 将鼠标悬停在 **签名** 列的图标上,可查看验签结果,以及可用的签名者和密钥指纹。更改验签密钥或信任设置后,点击日志工具栏的 **刷新** 重新验签。 -右键提交可以选择 **复制修订号**(`⌥⇧⌘C` / `Ctrl+Alt+Shift+C`)、**创建补丁…**、**拣选**、**签出修订**、**显示此修订版本的仓库**、**与本地比较**、**将当前分支重置到此处…**、**还原提交**、**撤消提交…**、**新建分支…**、**新建标签…**、**在浏览器中查看** 等。选中两个提交时,**比较版本** 会比较它们。提交列表获得焦点时,`F2` 编辑提交消息,`⌥⌘N` / `Ctrl+Alt+N` 新建分支,`←` / `→` 跳到子提交或父提交,`F3` / `F11` 切换提交书签(`⌥F3` / `Ctrl+F11` 带助记符)。 +右键提交可以选择 **复制修订号**(`⌥⇧⌘C` / `Ctrl+Alt+Shift+C`)、**创建补丁…**、**拣选**、**签出修订**、**显示此修订版本的仓库**、**与本地比较**、**将当前分支重置到此处…**、**还原提交**、**撤消提交…**、**新建分支…**、**新建标签…**、**在浏览器中查看** 等。选中两个提交时,**比较版本** 会比较它们。提交列表获得焦点时,`F2` 编辑提交消息,`⌥⌘N` / `Ctrl+Alt+N` 新建分支,`←` / `→` 跳到子提交或父提交,`F3` / `F11` 切换提交书签(`⌥F3` / `Ctrl+F11` 带助记符)。从 **查找操作** 或在 **设置 › 按键映射** 中分配的快捷键执行 **复制提交主题**,会复制所选提交的主题行,每行一个。 ### 改写历史 diff --git a/src/features/vcs/__tests__/copyCommitSubject.test.ts b/src/features/vcs/__tests__/copyCommitSubject.test.ts new file mode 100644 index 0000000000..3024644383 --- /dev/null +++ b/src/features/vcs/__tests__/copyCommitSubject.test.ts @@ -0,0 +1,90 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import type { Commit } from "@/bindings"; +import { executeAction, getAction } from "@/platform/actions"; +import { setContext } from "@/platform/context"; +import { IDEA_ACTIONS } from "@/platform/ideaActions"; +import { useGitLog } from "../logStore"; +import { useVcs } from "../store"; + +const mocks = vi.hoisted(() => ({ writeClipboardText: vi.fn(), notify: vi.fn() })); + +vi.mock("@/platform/clipboard", async (importOriginal) => ({ + ...(await importOriginal()), + writeClipboardText: mocks.writeClipboardText, +})); +vi.mock("@/workbench/contributions", async (importOriginal) => ({ + ...(await importOriginal()), + notifications: () => ({ notify: mocks.notify }), +})); + +import { registerHistoryActions } from "../historyActions"; + +const commit = (id: string, summary: string, message = summary): Commit => ({ + id: id.repeat(40), + shortId: id.repeat(8), + parents: [], + author: "A", + email: "a@example.com", + timeMs: 0, + summary, + message, + refs: [], +}); + +const newer = commit("b", "Fix the parser", "Fix the parser\n\nThe body is not copied."); +const older = commit("a", "Add a parser"); + +const originalLog = useGitLog.getState(); +const originalVcs = useVcs.getState(); +let unregister: (() => void) | undefined; + +beforeEach(() => { + mocks.writeClipboardText.mockReset(); + mocks.notify.mockReset(); + useGitLog.setState({ commits: [newer, older], selected: null, selection: [], shownLog: "log", logTabs: [] }); + useVcs.setState({ root: "/repo", repo: "/repo", roots: ["/repo"], branchesLoaded: true }); + setContext("vcsAvailable", true); + unregister = registerHistoryActions(); +}); + +afterEach(() => { + unregister?.(); + unregister = undefined; + useGitLog.setState(originalLog); + useVcs.setState(originalVcs); + setContext("vcsAvailable", false); +}); + +describe("Copy Commit Subject", () => { + it("is a Git Log action that IDEA's Vcs.CopyCommitSubjectAction maps to", () => { + expect(getAction("vcs.log.copyCommitSubject")).toMatchObject({ + title: "Copy Commit Subject", + category: "Git Log", + }); + expect(IDEA_ACTIONS["Vcs.CopyCommitSubjectAction"]).toBe("vcs.log.copyCommitSubject"); + }); + + it("copies the subjects of the selected commits, one per line, newest first", async () => { + useGitLog.setState({ selected: older.id, selection: [older.id, newer.id] }); + await executeAction("vcs.log.copyCommitSubject"); + await vi.waitFor(() => expect(mocks.writeClipboardText).toHaveBeenCalledOnce()); + expect(mocks.writeClipboardText).toHaveBeenCalledWith("Fix the parser\nAdd a parser"); + }); + + it("copies the subject, not the message body", async () => { + useGitLog.setState({ selected: newer.id, selection: [newer.id] }); + await executeAction("vcs.log.copyCommitSubject"); + await vi.waitFor(() => expect(mocks.writeClipboardText).toHaveBeenCalledWith("Fix the parser")); + }); + + it("copies the commit it is given and works on read-only commits", async () => { + await executeAction("vcs.log.copyCommitSubject", { ...older, readOnly: true }); + await vi.waitFor(() => expect(mocks.writeClipboardText).toHaveBeenCalledWith("Add a parser")); + }); + + it("copies nothing without a selected commit", async () => { + await executeAction("vcs.log.copyCommitSubject"); + expect(mocks.writeClipboardText).not.toHaveBeenCalled(); + expect(mocks.notify).toHaveBeenCalledWith(expect.objectContaining({ title: "Select a commit in the Git log" })); + }); +}); diff --git a/src/features/vcs/historyActions.tsx b/src/features/vcs/historyActions.tsx index 17913da94b..8e93ccf718 100644 --- a/src/features/vcs/historyActions.tsx +++ b/src/features/vcs/historyActions.tsx @@ -406,6 +406,15 @@ export function registerHistoryActions(): () => void { keyWhen: IN_GIT_LOG, run: withCommits((cs) => writeClipboardText(cs.map((c) => c.id).join(" ")), { allowReadOnly: true }), }, + { + id: "vcs.log.copyCommitSubject", + title: "Copy Commit Subject", + category: "Git Log", + icon: "copy", + when: "vcsAvailable || projectOpen || vcsLogFocus", + keyWhen: IN_GIT_LOG, + run: withCommits((cs) => writeClipboardText(cs.map((c) => c.summary).join("\n")), { allowReadOnly: true }), + }, { id: "vcs.log.createPatch", title: "Create Patch…", diff --git a/src/platform/ideaActions.ts b/src/platform/ideaActions.ts index 3488a1cfbe..ac8f8d33a7 100644 --- a/src/platform/ideaActions.ts +++ b/src/platform/ideaActions.ts @@ -133,4 +133,5 @@ export const IDEA_ACTIONS: Readonly> = { "Compare.SameVersion": "vcs.showDiff", "ChangesView.ShelveSilently": "vcs.shelveSilently", "ChangesView.UnshelveSilently": "vcs.unshelveSilently", + "Vcs.CopyCommitSubjectAction": "vcs.log.copyCommitSubject", }; diff --git a/src/platform/keymaps/intellij-macos.ts b/src/platform/keymaps/intellij-macos.ts index 8654accf83..a2cca96be2 100644 --- a/src/platform/keymaps/intellij-macos.ts +++ b/src/platform/keymaps/intellij-macos.ts @@ -356,6 +356,7 @@ export const intellijMacos: Record = { "vcs.unshelveSilently": ["Meta+Alt+U"], "vcs.changelist.edit": ["F2", "Shift+F6"], "vcs.log.copyRevision": ["Meta+Alt+Shift+C"], + "vcs.log.copyCommitSubject": [], "vcs.log.goToRef": ["Meta+F"], "vcs.log.editMessage": ["F2", "Shift+F6"], "vcs.log.newBranch": ["Meta+Alt+N"], diff --git a/src/platform/keymaps/intellij-windows.ts b/src/platform/keymaps/intellij-windows.ts index f4e62622d9..3be6b10534 100644 --- a/src/platform/keymaps/intellij-windows.ts +++ b/src/platform/keymaps/intellij-windows.ts @@ -354,6 +354,7 @@ export const intellijWindows: Record = { "vcs.unshelveSilently": ["Ctrl+Alt+U"], "vcs.changelist.edit": ["F2", "Shift+F6"], "vcs.log.copyRevision": ["Ctrl+Alt+Shift+C"], + "vcs.log.copyCommitSubject": [], "vcs.log.goToRef": ["Ctrl+F"], "vcs.log.editMessage": ["F2", "Shift+F6"], "vcs.log.newBranch": ["Ctrl+Alt+N"], diff --git a/src/platform/keymaps/vscode-macos.ts b/src/platform/keymaps/vscode-macos.ts index 2afa8023be..581b8da3ec 100644 --- a/src/platform/keymaps/vscode-macos.ts +++ b/src/platform/keymaps/vscode-macos.ts @@ -365,6 +365,7 @@ export const vscodeMacos: Record = { "vcs.unshelveSilently": [], "vcs.changelist.edit": [], "vcs.log.copyRevision": [], + "vcs.log.copyCommitSubject": [], "vcs.log.goToRef": [], "vcs.log.editMessage": [], "vcs.log.newBranch": [], diff --git a/src/platform/locales/text/zh-TW.json b/src/platform/locales/text/zh-TW.json index 69b7753d5b..cc0569ffda 100644 --- a/src/platform/locales/text/zh-TW.json +++ b/src/platform/locales/text/zh-TW.json @@ -1190,6 +1190,7 @@ "Copy": "複製", "Copy / Paste Special": "複製/選擇性貼上", "Copy All": "全部複製", + "Copy Commit Subject": "複製提交主題", "Copy Configuration": "複製組態", "Copy Container ID": "複製容器 ID", "Copy Coordinates": "複製座標", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 85b8e7d8d7..00a721fc8b 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -1555,6 +1555,7 @@ "Copy / Paste Special": "复制/选择性粘贴", "Copy All": "全部复制", "Copy Command": "复制命令", + "Copy Commit Subject": "复制提交主题", "Copy Configuration": "复制配置", "Copy Container ID": "复制容器 ID", "Copy Coordinates": "复制坐标",