diff --git a/README.md b/README.md index e0732d7..3a79f3e 100644 --- a/README.md +++ b/README.md @@ -596,6 +596,8 @@ Additionally, these integrations provide commands to copy the url of or open, fi - Adds a _Copy SHA_ command (`gitlens.copyShaToClipboard`) to copy the commit SHA of the current line to the clipboard or from the most recent commit to the current branch, if there is no current editor - Adds a _Copy Message_ command (`gitlens.copyMessageToClipboard`) to copy the commit message of the current line to the clipboard or from the most recent commit to the current branch, if there is no current editor +- Adds a _Copy Current Branch_ command (`gitlens.copyCurrentBranch`) to copy the name of the current branch to the clipboard + - Adds a _Switch to Another Branch_ (`gitlens.views.switchToAnotherBranch`) command — to quickly switch the current branch - Adds a _Compare References..._ command (`gitlens.compareWith`) to compare two selected references diff --git a/package.json b/package.json index 93a9148..b7d90aa 100644 --- a/package.json +++ b/package.json @@ -185,6 +185,7 @@ "onCommand:gitlens.fetchRepositories", "onCommand:gitlens.pullRepositories", "onCommand:gitlens.pushRepositories", + "oncommand:gitlens.copyCurrentBranch", "onStartupFinished" ], "contributes": { @@ -4693,6 +4694,11 @@ "command": "gitlens.views.tags.setShowAvatarsOff", "title": "Hide Avatars", "category": "GitLens" + }, + { + "command": "gitlens.copyCurrentBranch", + "title": "Copy Current Branch", + "category": "GitLens" } ], "menus": { diff --git a/src/commands.ts b/src/commands.ts index 8742be3..a3c1116 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -6,6 +6,7 @@ export * from './commands/closeUnchangedFiles'; export * from './commands/closeView'; export * from './commands/common'; export * from './commands/compareWith'; +export * from './commands/copyCurrentBranch'; export * from './commands/copyMessageToClipboard'; export * from './commands/copyShaToClipboard'; export * from './commands/openDirectoryCompare'; diff --git a/src/commands/common.ts b/src/commands/common.ts index 6a71da3..f6ab1c3 100644 --- a/src/commands/common.ts +++ b/src/commands/common.ts @@ -41,6 +41,7 @@ export enum Commands { CompareWorkingWith = 'gitlens.compareWorkingWith', ComputingFileAnnotations = 'gitlens.computingFileAnnotations', ConnectRemoteProvider = 'gitlens.connectRemoteProvider', + CopyCurrentBranch = 'gitlens.copyCurrentBranch', CopyMessageToClipboard = 'gitlens.copyMessageToClipboard', CopyRemoteBranchesUrl = 'gitlens.copyRemoteBranchesUrl', CopyRemoteBranchUrl = 'gitlens.copyRemoteBranchUrl', diff --git a/src/commands/copyCurrentBranch.ts b/src/commands/copyCurrentBranch.ts new file mode 100644 index 0000000..8fee349 --- /dev/null +++ b/src/commands/copyCurrentBranch.ts @@ -0,0 +1,33 @@ +'use strict'; + +import { env , TextEditor , Uri , window } from 'vscode'; +import { GitService } from '../git/gitService'; +import { GitUri } from '../git/gitUri'; +import { Logger } from '../logger'; +import { ActiveEditorCommand, command, Commands, getCommandUri, getRepoPathOrActiveOrPrompt } from './common'; + +@command() +export class CopyCurrentBranch extends ActiveEditorCommand { + constructor() { + super(Commands.CopyCurrentBranch); + } + + async execute(editor?: TextEditor, uri?: Uri) { + uri = getCommandUri(uri, editor); + + const gitUri = uri != null ? await GitUri.fromUri(uri) : undefined; + + const repoPath = await getRepoPathOrActiveOrPrompt(gitUri, editor, 'Copy Current Branch'); + if (!repoPath) return; + + const service = new GitService(); + + try { + const gitBranch = await service.getBranch(repoPath); + if (gitBranch?.name) await env.clipboard.writeText(gitBranch?.name); + } catch (ex) { + Logger.error(ex, 'CopyCurrentBranch'); + void window.showErrorMessage('Unable to copy current branch. See output channel for more details'); + } + } +}