Skip to content

Commit aa678a9

Browse files
committed
Adopt gptcommit 0.3.0
1 parent c2e1da3 commit aa678a9

File tree

7 files changed

+155
-98
lines changed

7 files changed

+155
-98
lines changed

package.json

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"activationEvents": [
2424
"onCommand:gptcommit.generateGitCommitMessage",
2525
"onCommand:gptcommit.setupOpenAIApiKey",
26-
"onCommand:gptcommit.tryDifferentOpenAIModel"
26+
"onCommand:gptcommit.useDifferentModel",
27+
"onCommand:gptcommit.setOutputLanguage"
2728
],
2829
"main": "./out/extension.js",
2930
"contributes": {
@@ -38,8 +39,12 @@
3839
"title": "GPTCommit: Setup OpenAI API Key"
3940
},
4041
{
41-
"command": "gptcommit.tryDifferentOpenAIModel",
42-
"title": "GPTCommit: Try a different OpenAI model"
42+
"command": "gptcommit.useDifferentModel",
43+
"title": "GPTCommit: Use a different OpenAI model"
44+
},
45+
{
46+
"command": "gptcommit.setOutputLanguage",
47+
"title": "GPTCommit: Set output language (default: en)"
4348
}
4449
],
4550
"menus": {
@@ -54,33 +59,33 @@
5459
"configuration": {
5560
"title": "GPTCommit",
5661
"properties": {
62+
"gptcommit.gptcommitPath": {
63+
"type": "string",
64+
"default": "",
65+
"description": "Path to gptcommit executable."
66+
},
5767
"gptcommit.expressMode": {
5868
"type": "boolean",
5969
"default": false,
6070
"description": "If true, generated message will be filled into the Source Control commit message input box directly, instead of opening a new editor."
6171
},
6272
"gptcommit.expressModeContent": {
6373
"type": "string",
64-
"default": "title + body",
74+
"default": "title + summary",
6575
"enum": [
6676
"title",
67-
"title + body",
77+
"title + summary",
6878
"title + breakdown",
69-
"title + body + breakdown"
79+
"title + summary + breakdown"
7080
],
7181
"enumDescriptions": [
72-
"Only the title of the commit message",
73-
"Title and body of the commit message",
74-
"Title and breakdown of the commit message",
75-
"Title, body and breakdown of the commit message"
82+
"Only the title",
83+
"Title and bullet-point summary",
84+
"Title and breakdown of each changed file",
85+
"Title, bullet-point summary and each changed file"
7686
],
7787
"description": "Content of the message to fill in the express mode."
7888
},
79-
"gptcommit.gptcommitPath": {
80-
"type": "string",
81-
"default": "",
82-
"description": "Path to gptcommit executable."
83-
},
8489
"gptcommit.onFiles": {
8590
"type": "string",
8691
"default": "tryStagedThenUnstaged",

src/commands/createCommandConfigs.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as vscode from 'vscode';
2+
import { getRepo, saveConfig } from '../utils';
3+
4+
export function setupOpenAIApiKey(context: vscode.ExtensionContext, channel: vscode.OutputChannel) {
5+
let apiCommand = vscode.commands.registerCommand('gptcommit.setupOpenAIApiKey', async (uri?: vscode.SourceControl) => {
6+
vscode.window.showInputBox({
7+
prompt: 'Enter your OpenAI API key',
8+
placeHolder: 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
9+
}).then((key) => {
10+
if (key) {
11+
saveConfig(
12+
'openai.api_key',
13+
'openai.api_key',
14+
channel,
15+
getRepo(uri),
16+
key,
17+
'OpenAI API key saved'
18+
);
19+
}
20+
});
21+
});
22+
23+
context.subscriptions.push(apiCommand);
24+
};
25+
26+
export function useDifferentModel(context: vscode.ExtensionContext, channel: vscode.OutputChannel) {
27+
let modelCommand = vscode.commands.registerCommand('gptcommit.useDifferentModel', async (uri?: vscode.SourceControl) => {
28+
vscode.window.showInputBox({
29+
prompt: 'Enter the model you want to use',
30+
placeHolder: 'gpt-3.5-turbo',
31+
}).then((model) => {
32+
if (model) {
33+
saveConfig(
34+
'openai.model',
35+
'openai.model',
36+
channel,
37+
getRepo(uri),
38+
model,
39+
'Model saved'
40+
);
41+
}
42+
});
43+
});
44+
45+
context.subscriptions.push(modelCommand);
46+
}
47+
48+
export function setOutputLanguage(context: vscode.ExtensionContext, channel: vscode.OutputChannel) {
49+
let languageCommand = vscode.commands.registerCommand('gptcommit.setOutputLanguage', async (uri?: vscode.SourceControl) => {
50+
vscode.window.showInputBox({
51+
prompt: 'Enter the language of the generated commit message',
52+
placeHolder: 'en',
53+
}).then((lang) => {
54+
if (lang) {
55+
saveConfig(
56+
'output.lang',
57+
'output.lang',
58+
channel,
59+
getRepo(uri),
60+
lang,
61+
'Output language saved'
62+
);
63+
}
64+
});
65+
});
66+
67+
context.subscriptions.push(languageCommand);
68+
}

src/commands/createCommandGenerateGitCommitMessage.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,18 @@
11
import * as vscode from 'vscode';
2-
import { getGitExtension, getCommitMessage } from '../utils';
3-
import { Repository } from '../@types/git';
2+
import { getCommitMessage } from '../utils';
3+
import { getRepo } from '../utils';
44

55
export default (context: vscode.ExtensionContext, channel: vscode.OutputChannel) => {
66
let command1 = vscode.commands.registerCommand(
77
'gptcommit.generateGitCommitMessage',
88
async (uri?: vscode.SourceControl) => {
9-
// The code you place here will be executed every time your command is executed
10-
// Display a message box to the user
11-
const git = getGitExtension();
12-
13-
if (!git) {
14-
vscode.window.showErrorMessage('Git extension not found');
9+
const repo = getRepo(uri);
10+
if (!repo) {
1511
return;
1612
}
1713

1814
const config = vscode.workspace.getConfiguration('gptcommit');
1915

20-
let repo: Repository | undefined;
21-
if (uri) {
22-
const uriPath = uri.rootUri?.path;
23-
repo = git.repositories.find(r => r.rootUri.path === uriPath);
24-
} else if (git.repositories.length === 1) {
25-
repo = git.repositories[0];
26-
} else if (git.repositories.length > 1) {
27-
repo = git.repositories[0];
28-
vscode.window.showWarningMessage('Multiple repositories found, using first one');
29-
} else {
30-
vscode.window.showErrorMessage('No repository found');
31-
return;
32-
}
33-
if (!repo) {
34-
vscode.window.showErrorMessage('No repository found');
35-
return;
36-
}
37-
3816
getCommitMessage(config, repo, context, channel).then((message) => {
3917
if (repo) {
4018
repo.inputBox.value = message;

src/commands/setupOpenAIApiKey.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/commands/tryDifferentOpenAIModel.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/extension.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// Import the module and reference it with the alias vscode in your code below
33
import * as vscode from 'vscode';
44
import createCommandGenerateGitCommitMessage from './commands/createCommandGenerateGitCommitMessage';
5-
import setupOpenAIApiKey from './commands/setupOpenAIApiKey';
6-
import tryDifferentOpenAIModel from './commands/tryDifferentOpenAIModel';
5+
import { setupOpenAIApiKey, useDifferentModel, setOutputLanguage } from './commands/createCommandConfigs';
76

87
// This method is called when your extension is activated
98
// Your extension is activated the very first time the command is executed
@@ -20,12 +19,9 @@ export function activate(context: vscode.ExtensionContext) {
2019
// Now provide the implementation of the command with registerCommand
2120
// The commandId parameter must match the command field in package.json
2221
createCommandGenerateGitCommitMessage(context, channel);
23-
24-
let command2 = vscode.commands.registerCommand('gptcommit.setupOpenAIApiKey', setupOpenAIApiKey);
25-
context.subscriptions.push(command2);
26-
27-
let command3 = vscode.commands.registerCommand('gptcommit.tryDifferentOpenAIModel', tryDifferentOpenAIModel);
28-
context.subscriptions.push(command3);
22+
setupOpenAIApiKey(context, channel);
23+
useDifferentModel(context, channel);
24+
setOutputLanguage(context, channel);
2925
}
3026

3127
// This method is called when your extension is deactivated

src/utils.ts

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export async function getCommitMessage(
100100
export function polishMessage(msg: string, content: string) {
101101
const lines = msg.split('\n');
102102
let title = lines[0];
103-
let body = [];
103+
let summary = [];
104104
let breakdown = [];
105105
let breakdownStarted = false;
106106
for (let line of lines.slice(1)) {
@@ -113,7 +113,7 @@ export function polishMessage(msg: string, content: string) {
113113
if (breakdownStarted) {
114114
breakdown.push(line);
115115
} else if (line.startsWith('- ')) {
116-
body.push(line);
116+
summary.push(line);
117117
}
118118
}
119119
if (/breakdown/.test(content)) {
@@ -122,11 +122,65 @@ export function polishMessage(msg: string, content: string) {
122122
if (content === 'title') {
123123
return title;
124124
}
125-
if (content === 'title + body') {
126-
return [title, '', ...body].join('\n');
125+
if (content === 'title + summary') {
126+
return [title, '', ...summary].join('\n');
127127
}
128128
if (content === 'title + breakdown') {
129129
return [title, ...breakdown].join('\n');
130130
}
131-
return [title, '', ...body, ...breakdown].join('\n');
131+
return [title, '', ...summary, ...breakdown].join('\n');
132+
}
133+
134+
export function getRepo(uri?: vscode.SourceControl): Repository | undefined {
135+
const git = getGitExtension();
136+
137+
if (!git) {
138+
vscode.window.showErrorMessage('Git extension not found');
139+
return undefined;
140+
}
141+
142+
if (uri) {
143+
const uriPath = uri.rootUri?.path;
144+
return git.repositories.find(r => r.rootUri.path === uriPath);
145+
}
146+
if (git.repositories.length === 1) {
147+
return git.repositories[0];
148+
}
149+
if (git.repositories.length > 1) {
150+
vscode.window.showWarningMessage('Multiple repositories found, using first one');
151+
return git.repositories[0];
152+
}
153+
vscode.window.showErrorMessage('No repository found');
154+
return undefined;
155+
}
156+
157+
export function saveConfig(
158+
key: string,
159+
gckey: string,
160+
channel: vscode.OutputChannel,
161+
repo: Repository | undefined,
162+
value: any | undefined = undefined,
163+
savedInfo: string | undefined = undefined,
164+
) {
165+
if (repo === undefined) {
166+
return;
167+
}
168+
const gptcommit = vscode.workspace.getConfiguration('gptcommit').gptcommitPath || 'gptcommit';
169+
if (value === undefined) {
170+
value = vscode.workspace.getConfiguration('gptcommit')[key];
171+
}
172+
const cmd = `${gptcommit} config set --local ${gckey} ${value}`;
173+
exec(cmd, { cwd: repo.rootUri.fsPath }, (err, stdout, stderr) => {
174+
if (err) {
175+
vscode.window.showErrorMessage(`Failed to save "${key}": ${err}`);
176+
channel.appendLine(`COMMAND: ${cmd}`);
177+
channel.appendLine(`STDOUT: ${stdout}`);
178+
channel.appendLine(`STDERR: ${stderr}`);
179+
exec("pwd", (err, stdout, stderr) => {
180+
vscode.window.showErrorMessage(`pwd: ${stdout} ${stderr} ${err}`)
181+
});
182+
} else if (savedInfo !== undefined) {
183+
vscode.window.showInformationMessage(savedInfo);
184+
}
185+
});
132186
}

0 commit comments

Comments
 (0)