diff --git a/src/features/vcs/__tests__/copyBranchName.test.ts b/src/features/vcs/__tests__/copyBranchName.test.ts new file mode 100644 index 0000000000..1c40d38347 --- /dev/null +++ b/src/features/vcs/__tests__/copyBranchName.test.ts @@ -0,0 +1,64 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { executeAction, getAction, isActionEnabled, registerAction, registerActions } from "@/platform/actions"; +import { clearContext, setContext } from "@/platform/context"; +import { IDEA_ACTIONS } from "@/platform/ideaActions"; +import { provideNotificationService } from "@/workbench/contributions"; +import { copyBranchNameActions } from "../copyBranchName"; +import { useVcs } from "../store"; + +const writeText = vi.fn(async (_text: string) => undefined); +const hint = vi.fn(); +const disposers: Array<() => void> = []; + +beforeEach(() => { + vi.clearAllMocks(); + vi.stubGlobal("navigator", { clipboard: { writeText }, userAgent: navigator.userAgent }); + provideNotificationService({ notify: vi.fn() }); + setContext("vcsAvailable", true); + useVcs.setState({ + roots: ["/w/api", "/w/web"], + repo: "/w/api", + heads: { + "/w/api": { head: "feature/login", headCommit: "a1" }, + "/w/web": { head: "main", headCommit: "b2" }, + }, + }); + disposers.push( + registerActions(copyBranchNameActions), + registerAction({ id: "editor.showHint", title: "Hint", run: ({ arg }) => hint(arg) }), + ); +}); + +afterEach(() => { + disposers + .splice(0) + .reverse() + .forEach((dispose) => dispose()); + useVcs.setState({ roots: [], repo: null, heads: {} }); + clearContext("vcsAvailable"); + vi.unstubAllGlobals(); +}); + +describe("Copy Branch Name", () => { + it("copies the selected repository's current branch", async () => { + await executeAction("vcs.copyBranchName"); + await vi.waitFor(() => expect(hint).toHaveBeenCalledWith({ text: "Copied feature/login" })); + expect(writeText).toHaveBeenCalledExactlyOnceWith("feature/login"); + }); + + it("copies the branch of the repository the action was invoked for", async () => { + await executeAction("vcs.copyBranchName", { repo: "/w/web" }); + await vi.waitFor(() => expect(writeText).toHaveBeenCalledExactlyOnceWith("main")); + }); + + it("is disabled on a detached HEAD", () => { + useVcs.setState({ heads: { "/w/api": { head: null, headCommit: "a1" } } }); + expect(isActionEnabled(getAction("vcs.copyBranchName")!)).toBe(false); + useVcs.setState({ heads: { "/w/api": { head: "dev", headCommit: "a1" } } }); + expect(isActionEnabled(getAction("vcs.copyBranchName")!)).toBe(true); + }); + + it("is IntelliJ's Vcs.CopyCurrentBranchName", () => { + expect(IDEA_ACTIONS["Vcs.CopyCurrentBranchName"]).toBe("vcs.copyBranchName"); + }); +}); diff --git a/src/features/vcs/actions.tsx b/src/features/vcs/actions.tsx index 88b817d7a0..947228d1e7 100644 --- a/src/features/vcs/actions.tsx +++ b/src/features/vcs/actions.tsx @@ -337,6 +337,7 @@ const OPERATIONS: MenuItem[] = [ { kind: "action", action: "vcs.compareWithBranch" }, { kind: "separator" }, { kind: "action", action: "vcs.branches" }, + { kind: "action", action: "vcs.copyBranchName" }, { kind: "action", action: "vcs.push" }, { kind: "action", action: "vcs.stash" }, { kind: "action", action: "vcs.unstash" }, diff --git a/src/features/vcs/copyBranchName.ts b/src/features/vcs/copyBranchName.ts new file mode 100644 index 0000000000..bedf1b5c44 --- /dev/null +++ b/src/features/vcs/copyBranchName.ts @@ -0,0 +1,30 @@ +/** + * Copy Branch Name (IntelliJ's `Vcs.CopyCurrentBranchName`): the current branch of the + * repository the action applies to, from Search Everywhere, a shortcut or VCS Operations. + */ +import type { Action } from "@/platform/actions"; +import { operationRepo } from "./actions"; +import { copyWithHint } from "./copyRepositoryPath"; +import { currentRepoRoot, useVcs } from "./store"; + +/** The checked-out branch of the repository `arg` points at (else the active file's, else the selected one); null when detached. */ +export function branchToCopy(arg?: unknown): string | null { + const repo = operationRepo(arg) ?? currentRepoRoot(); + return (repo && useVcs.getState().heads[repo]?.head) || null; +} + +export const copyBranchNameActions: Action[] = [ + { + id: "vcs.copyBranchName", + title: "Copy Branch Name", + category: "Git", + icon: "copy", + when: "vcsAvailable", + keywords: ["git", "branch", "clipboard", "current branch"], + enabled: ({ arg }) => branchToCopy(arg) != null, + run: ({ arg }) => { + const branch = branchToCopy(arg); + if (branch) void copyWithHint(branch); + }, + }, +]; diff --git a/src/features/vcs/copyRepositoryPath.ts b/src/features/vcs/copyRepositoryPath.ts index d921ccc70a..6329a95cb9 100644 --- a/src/features/vcs/copyRepositoryPath.ts +++ b/src/features/vcs/copyRepositoryPath.ts @@ -21,7 +21,8 @@ function repositoryPaths(paths: readonly string[]): string | null { return relative.length ? relative.join("\n") : null; } -async function copy(text: string) { +/** Copies `text`, confirming with a hint (or an error notification when the clipboard is unavailable). */ +export async function copyWithHint(text: string) { try { await writeClipboardText(text); void executeAction("editor.showHint", { text: tx`Copied ${text}` }); @@ -44,7 +45,7 @@ export const copyRepositoryPathActions: Action[] = [ hidden: true, run: ({ arg }) => { const text = Array.isArray(arg) ? repositoryPaths(arg as string[]) : null; - if (text) void copy(text); + if (text) void copyWithHint(text); }, }, ]; diff --git a/src/features/vcs/index.ts b/src/features/vcs/index.ts index c54751a749..fffe8c57c4 100644 --- a/src/features/vcs/index.ts +++ b/src/features/vcs/index.ts @@ -80,6 +80,7 @@ import { import { installRootSync } from "./rootSync"; import { tx } from "@/platform/i18n"; import { openOnRemoteActions, registerOpenOnRemote } from "./openOnRemote"; +import { copyBranchNameActions } from "./copyBranchName"; import { copyRepositoryPathActions, registerCopyRepositoryPath } from "./copyRepositoryPath"; import { activateSettingsPages } from "./settingsPages"; import { registerStagedEditor } from "./StagedEditor"; @@ -270,6 +271,7 @@ export function activate(): () => void { registerActions(openOnRemoteActions), registerOpenOnRemote(), registerActions(copyRepositoryPathActions), + registerActions(copyBranchNameActions), registerCopyRepositoryPath(), registerHistoryActions(), registerLogPaneActions(), diff --git a/src/features/vim/__tests__/rc.test.ts b/src/features/vim/__tests__/rc.test.ts index c8583bf664..dd9c95cd82 100644 --- a/src/features/vim/__tests__/rc.test.ts +++ b/src/features/vim/__tests__/rc.test.ts @@ -201,6 +201,7 @@ describe(":action ids", () => { "editor.unsplit", "editor.splitDown", "vcs.update", + "vcs.copyBranchName", ]); const missing = Object.entries(IDEA_ACTIONS).filter(([, id]) => !known.has(id)); expect(missing).toEqual([]); diff --git a/src/platform/ideaActions.ts b/src/platform/ideaActions.ts index 082a81afc5..5ec1731ced 100644 --- a/src/platform/ideaActions.ts +++ b/src/platform/ideaActions.ts @@ -128,6 +128,7 @@ export const IDEA_ACTIONS: Readonly> = { "Vcs.Push": "vcs.push", "Vcs.UpdateProject": "vcs.update", "Git.Branches": "vcs.branches", + "Vcs.CopyCurrentBranchName": "vcs.copyBranchName", Annotate: "vcs.annotate", VcsShowNextChangeMarker: "vcs.nextChange", VcsShowPrevChangeMarker: "vcs.prevChange",