Skip to content

Commit 344df5d

Browse files
committed
Add commit message generation and one-click commit functionality to Code Web Chat
1 parent 0fa2f00 commit 344df5d

4 files changed

Lines changed: 106 additions & 42 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ Meaningful summaries of changes adhering to your preffered style.
120120
- `Code Web Chat: Undo Context Selection` - Undo the last context change.
121121
- `Code Web Chat: Redo Context Selection` - Redo the last undone context change.
122122

123+
### Commit messages
124+
125+
- `Code Web Chat: Commit Changes` - Generate commit message and commit.
126+
123127
## Contributing
124128

125129
All contributions are welcome. Feel free to submit pull requests, feature requests and bug reports.

‎apps/editor/package.json‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "gemini-coder",
33
"displayName": "Code Web Chat",
44
"description": "Connect VS Code with chatbots (CWC)",
5-
"version": "1.773.0",
5+
"version": "1.774.0",
66
"scripts": {
77
"build": "npx vsce package --no-dependencies",
88
"vscode:prepublish": "rimraf out && npm run compile",
@@ -308,6 +308,17 @@
308308
"category": "Code Web Chat",
309309
"icon": "$(sparkle)"
310310
},
311+
{
312+
"command": "codeWebChat.copyCommitMessagePrompt",
313+
"title": "Copy Commit Message Prompt from CWC",
314+
"category": "Code Web Chat",
315+
"icon": "$(copy)"
316+
},
317+
{
318+
"command": "codeWebChat.generateCommitMessageAndCommit",
319+
"title": "Commit Changes",
320+
"category": "Code Web Chat"
321+
},
311322
{
312323
"command": "codeWebChat.setRange",
313324
"title": "Set Range",
@@ -430,6 +441,14 @@
430441
{
431442
"command": "codeWebChat.setRange",
432443
"when": "false"
444+
},
445+
{
446+
"command": "codeWebChat.generateCommitMessage",
447+
"when": "false"
448+
},
449+
{
450+
"command": "codeWebChat.copyCommitMessagePrompt",
451+
"when": "false"
433452
}
434453
],
435454
"view/title": [
@@ -524,6 +543,11 @@
524543
"command": "codeWebChat.generateCommitMessage",
525544
"group": "navigation",
526545
"when": "scmProvider == git"
546+
},
547+
{
548+
"command": "codeWebChat.copyCommitMessagePrompt",
549+
"group": "navigation",
550+
"when": "scmProvider == git"
527551
}
528552
],
529553
"view/item/context": [

‎apps/editor/src/commands/generate-commit-message-command/generate-commit-message-command.ts‎

Lines changed: 75 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,91 @@ import { generate_commit_message_with_api } from './utils/generate-commit-messag
1010
export const generate_commit_message_command = (
1111
context: vscode.ExtensionContext
1212
) => {
13-
return vscode.commands.registerCommand(
14-
'codeWebChat.generateCommitMessage',
15-
async (source_control?: vscode.SourceControl) => {
16-
const repository = await get_git_repository(source_control)
17-
if (!repository) return
13+
const get_prompt_data = async (source_control?: vscode.SourceControl) => {
14+
const repository = await get_git_repository(source_control)
15+
if (!repository) return null
1816

19-
await repository.status()
20-
const was_empty_stage = (repository.state.indexChanges || []).length == 0
17+
await repository.status()
18+
const was_empty_stage = (repository.state.indexChanges || []).length == 0
2119

22-
const diff = await prepare_staged_changes(repository)
20+
const diff = await prepare_staged_changes(repository)
2321

24-
if (!diff) return
22+
if (!diff) return null
2523

26-
const message_prompt = build_commit_message_prompt(diff)
24+
const message_prompt = build_commit_message_prompt(diff)
2725

28-
const api_config_data = await get_commit_message_config(
29-
context,
30-
message_prompt
31-
)
32-
if (!api_config_data) {
33-
if (was_empty_stage) {
34-
await vscode.commands.executeCommand('git.unstageAll')
35-
}
36-
return
26+
return { repository, was_empty_stage, message_prompt }
27+
}
28+
29+
const run_generate_action = async (
30+
source_control: vscode.SourceControl | undefined,
31+
should_commit: boolean
32+
) => {
33+
const data = await get_prompt_data(source_control)
34+
if (!data) return
35+
const { repository, was_empty_stage, message_prompt } = data
36+
37+
const api_config_data = await get_commit_message_config(context)
38+
39+
if (!api_config_data) {
40+
if (was_empty_stage) {
41+
await vscode.commands.executeCommand('git.unstageAll')
3742
}
43+
return
44+
}
45+
46+
const commit_message = await generate_commit_message_with_api({
47+
endpoint_url: api_config_data.endpoint_url,
48+
provider: api_config_data.provider,
49+
config: api_config_data.config,
50+
message: message_prompt
51+
})
52+
53+
if (commit_message) {
54+
repository.inputBox.value = commit_message
55+
if (should_commit) {
56+
await vscode.commands.executeCommand('git.commit', repository)
57+
}
58+
} else if (was_empty_stage) {
59+
await vscode.commands.executeCommand('git.unstageAll')
60+
}
61+
}
62+
63+
const generate_command = vscode.commands.registerCommand(
64+
'codeWebChat.generateCommitMessage',
65+
async (source_control?: vscode.SourceControl) => {
66+
await run_generate_action(source_control, false)
67+
}
68+
)
3869

39-
const commit_message = await generate_commit_message_with_api({
40-
endpoint_url: api_config_data.endpoint_url,
41-
provider: api_config_data.provider,
42-
config: api_config_data.config,
43-
message: message_prompt
44-
})
70+
const generate_and_commit_command = vscode.commands.registerCommand(
71+
'codeWebChat.generateCommitMessageAndCommit',
72+
async (source_control?: vscode.SourceControl) => {
73+
await run_generate_action(source_control, true)
74+
}
75+
)
4576

46-
if (commit_message) {
47-
repository.inputBox.value = commit_message
48-
} else if (was_empty_stage) {
77+
const copy_command = vscode.commands.registerCommand(
78+
'codeWebChat.copyCommitMessagePrompt',
79+
async (source_control?: vscode.SourceControl) => {
80+
const data = await get_prompt_data(source_control)
81+
if (!data) return
82+
const { was_empty_stage, message_prompt } = data
83+
84+
await vscode.env.clipboard.writeText(message_prompt)
85+
vscode.window.showInformationMessage(
86+
'Commit message prompt copied to clipboard'
87+
)
88+
89+
if (was_empty_stage) {
4990
await vscode.commands.executeCommand('git.unstageAll')
5091
}
5192
}
5293
)
94+
95+
return vscode.Disposable.from(
96+
generate_command,
97+
generate_and_commit_command,
98+
copy_command
99+
)
53100
}

‎apps/editor/src/commands/generate-commit-message-command/utils/get-commit-message-config.ts‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ export interface CommitMessageConfig {
1717
}
1818

1919
export const get_commit_message_config = async (
20-
context: vscode.ExtensionContext,
21-
message_prompt?: string
20+
context: vscode.ExtensionContext
2221
): Promise<{
2322
config: CommitMessageConfig
2423
provider: any
@@ -49,14 +48,7 @@ export const get_commit_message_config = async (
4948
tooltip: 'Close'
5049
}
5150

52-
const copy_button = {
53-
iconPath: new vscode.ThemeIcon('copy'),
54-
tooltip: 'Copy prompt to clipboard'
55-
}
56-
57-
quick_pick.buttons = message_prompt
58-
? [copy_button, close_button]
59-
: [close_button]
51+
quick_pick.buttons = [close_button]
6052
quick_pick.items = configs.map((config, index) => {
6153
const description_parts = [config.provider_name]
6254
if (config.reasoning_effort) {
@@ -98,9 +90,6 @@ export const get_commit_message_config = async (
9890
if (button == close_button) {
9991
quick_pick.hide()
10092
resolve(undefined)
101-
} else if (button == copy_button && message_prompt) {
102-
vscode.env.clipboard.writeText(message_prompt)
103-
vscode.window.showInformationMessage('Prompt copied to clipboard')
10493
}
10594
})
10695

0 commit comments

Comments
 (0)