-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
generateCommitMessage.ts
61 lines (50 loc) · 2.08 KB
/
generateCommitMessage.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import type { TextEditor, Uri } from 'vscode';
import { ProgressLocation, window } from 'vscode';
import { Commands } from '../constants';
import type { Container } from '../container';
import { GitUri } from '../git/gitUri';
import { showGenericErrorMessage } from '../messages';
import { getBestRepositoryOrShowPicker } from '../quickpicks/repositoryPicker';
import { command, executeCoreCommand } from '../system/command';
import { Logger } from '../system/logger';
import { ActiveEditorCommand, getCommandUri } from './base';
export interface GenerateCommitMessageCommandArgs {
repoPath?: string;
}
@command()
export class GenerateCommitMessageCommand extends ActiveEditorCommand {
constructor(private readonly container: Container) {
super(Commands.GenerateCommitMessage);
}
async execute(editor?: TextEditor, uri?: Uri, args?: GenerateCommitMessageCommandArgs) {
args = { ...args };
let repository;
if (args.repoPath != null) {
repository = this.container.git.getRepository(args.repoPath);
} else {
uri = getCommandUri(uri, editor);
const gitUri = uri != null ? await GitUri.fromUri(uri) : undefined;
repository = await getBestRepositoryOrShowPicker(gitUri, editor, 'Generate Commit Message');
}
if (repository == null) return;
const scmRepo = await this.container.git.getScmRepository(repository.path);
if (scmRepo == null) return;
try {
const currentMessage = scmRepo.inputBox.value;
const message = await this.container.ai.generateCommitMessage(repository, {
context: currentMessage,
progress: { location: ProgressLocation.Notification, title: 'Generating commit message...' },
});
if (message == null) return;
void executeCoreCommand('workbench.view.scm');
scmRepo.inputBox.value = currentMessage ? `${currentMessage}\n\n${message}` : message;
} catch (ex) {
Logger.error(ex, 'GenerateCommitMessageCommand');
if (ex instanceof Error && ex.message.startsWith('No changes')) {
void window.showInformationMessage('No changes to generate a commit message from.');
return;
}
void showGenericErrorMessage(ex.message);
}
}
}