-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
copyCurrentBranch.ts
34 lines (29 loc) · 1.16 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
33
34
import { env, TextEditor, Uri, window } from 'vscode';
import { Commands } from '../constants';
import type { Container } from '../container';
import { GitUri } from '../git/gitUri';
import { Logger } from '../logger';
import { RepositoryPicker } from '../quickpicks/repositoryPicker';
import { command } from '../system/command';
import { ActiveEditorCommand, getCommandUri } from './base';
@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 repository = await RepositoryPicker.getBestRepositoryOrShow(gitUri, editor, 'Copy Current Branch Name');
if (repository == null) return;
try {
const branch = await repository.getBranch();
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');
}
}
}