-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
copyCurrentBranch.ts
32 lines (27 loc) · 1.06 KB
/
copyCurrentBranch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'use strict';
import { env, TextEditor, Uri, window } from 'vscode';
import type { Container } from '../container';
import { GitUri } from '../git/gitUri';
import { Logger } from '../logger';
import { ActiveEditorCommand, command, Commands, getCommandUri, getRepoPathOrActiveOrPrompt } from './common';
@command()
export class CopyCurrentBranchCommand extends ActiveEditorCommand {
constructor(private readonly container: Container) {
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 Name');
if (!repoPath) return;
try {
const branch = await this.container.git.getBranch(repoPath);
if (branch?.name) {
await env.clipboard.writeText(branch.name);
}
} catch (ex) {
Logger.error(ex, 'CopyCurrentBranchCommand');
void window.showErrorMessage('Unable to copy current branch name. See output channel for more details');
}
}
}