Adds a Copy Branch Name action that copies the current branch of the active repository and shows the usual Copied hint. It is available from Search Everywhere, a shortcut, and VCS Operations after Branches.

Closes #5853

Co-authored-by: Rhombicdodecahedron <[email protected]>
Цей коміт міститься в:
Alexis authored and GitHub committed 2026-11-26 01:38:22 +08:00
1 джерело 0a53c7b864
коміт e3dc7e950b
7 files changed
+102 -2

No files matched your search

+64
Переглянути файл
@@ -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");
});
});
+1
Переглянути файл
@@ -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" },
+30
Переглянути файл
@@ -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);
},
},
];
+3 -2
Переглянути файл
@@ -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);
},
},
];
+2
Переглянути файл
@@ -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(),
+1
Переглянути файл
@@ -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([]);
+1
Переглянути файл
@@ -128,6 +128,7 @@ export const IDEA_ACTIONS: Readonly<Record<string, string>> = {
"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",