@@ -10,44 +10,91 @@ import { generate_commit_message_with_api } from './utils/generate-commit-messag
1010export 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}
0 commit comments