@@ -7,6 +7,7 @@ import { display_token_count } from '../../utils/display-token-count'
77import { get_commit_message_config } from './utils/get-commit-message-config'
88import { build_commit_message_prompt } from './utils/build-commit-message-prompt'
99import { generate_commit_message_with_api } from './utils/generate-commit-message-with-api'
10+ import { ModelProvidersManager } from '@/services/model-providers-manager'
1011
1112export const generate_commit_message_command = (
1213 context : vscode . ExtensionContext
@@ -31,73 +32,128 @@ export const generate_commit_message_command = (
3132 source_control : vscode . SourceControl | undefined ,
3233 should_commit : boolean
3334 ) => {
34- const data = await get_prompt_data ( source_control )
35- if ( ! data ) return
36- const { repository, was_empty_stage, message_prompt } = data
35+ let files_staged_by_action = false
36+ const api_providers_manager = new ModelProvidersManager ( context )
3737
38- const api_config_data = await get_commit_message_config ( context )
38+ while ( true ) {
39+ const data = await get_prompt_data ( source_control )
40+ if ( ! data ) return
41+ const { repository, message_prompt } = data
42+ let { was_empty_stage } = data
3943
40- if ( ! api_config_data ) {
4144 if ( was_empty_stage ) {
42- await vscode . commands . executeCommand ( 'git.unstageAll' )
45+ files_staged_by_action = true
46+ } else if ( files_staged_by_action ) {
47+ was_empty_stage = true
4348 }
44- return
45- }
4649
47- const commit_message = await generate_commit_message_with_api ( {
48- endpoint_url : api_config_data . endpoint_url ,
49- provider : api_config_data . provider ,
50- config : api_config_data . config ,
51- message : message_prompt
52- } )
53-
54- if ( commit_message ) {
55- if ( should_commit ) {
56- const edited_message = await new Promise < string | undefined > (
57- ( resolve ) => {
58- const input_box = vscode . window . createInputBox ( )
59- input_box . value = commit_message
60- input_box . title = 'Commit Message'
61- input_box . prompt = 'Review the commit message.'
62-
63- const close_button = {
64- iconPath : new vscode . ThemeIcon ( 'close' ) ,
65- tooltip : 'Close'
66- }
50+ const default_config =
51+ await api_providers_manager . get_default_commit_messages_config ( )
52+ const has_default_config = ! ! default_config
53+
54+ const api_config_data = await get_commit_message_config (
55+ context ,
56+ was_empty_stage
57+ )
6758
68- input_box . buttons = [ close_button ]
59+ if ( api_config_data === 'back' ) {
60+ if ( was_empty_stage ) {
61+ await vscode . commands . executeCommand ( 'git.unstageAll' )
62+ files_staged_by_action = false
63+ continue
64+ }
65+ return
66+ }
6967
70- input_box . onDidTriggerButton ( ( button ) => {
71- if ( button == close_button ) {
72- input_box . hide ( )
68+ if ( ! api_config_data ) {
69+ if ( was_empty_stage ) {
70+ await vscode . commands . executeCommand ( 'git.unstageAll' )
71+ }
72+ return
73+ }
74+
75+ const commit_message = await generate_commit_message_with_api ( {
76+ endpoint_url : api_config_data . endpoint_url ,
77+ provider : api_config_data . provider ,
78+ config : api_config_data . config ,
79+ message : message_prompt
80+ } )
81+
82+ if ( commit_message ) {
83+ if ( should_commit ) {
84+ const edited_message = await new Promise < string | 'back' | undefined > (
85+ ( resolve ) => {
86+ const input_box = vscode . window . createInputBox ( )
87+ input_box . value = commit_message
88+ input_box . title = 'Commit Message'
89+ input_box . prompt = 'Review the commit message.'
90+
91+ if ( has_default_config && ! files_staged_by_action ) {
92+ input_box . buttons = [
93+ {
94+ iconPath : new vscode . ThemeIcon ( 'close' ) ,
95+ tooltip : 'Close'
96+ }
97+ ]
98+ } else {
99+ input_box . buttons = [ vscode . QuickInputButtons . Back ]
73100 }
74- } )
75101
76- input_box . onDidAccept ( ( ) => {
77- resolve ( input_box . value )
78- input_box . hide ( )
79- } )
102+ let is_resolved = false
103+
104+ input_box . onDidTriggerButton ( ( button ) => {
105+ if (
106+ button === vscode . QuickInputButtons . Back ||
107+ button . tooltip == 'Close'
108+ ) {
109+ is_resolved = true
110+ resolve ( 'back' )
111+ input_box . hide ( )
112+ }
113+ } )
114+
115+ input_box . onDidAccept ( ( ) => {
116+ is_resolved = true
117+ resolve ( input_box . value )
118+ input_box . hide ( )
119+ } )
80120
81- input_box . onDidHide ( ( ) => {
82- resolve ( undefined )
83- input_box . dispose ( )
84- } )
121+ input_box . onDidHide ( ( ) => {
122+ if ( ! is_resolved ) {
123+ resolve ( 'back' )
124+ }
125+ input_box . dispose ( )
126+ } )
85127
86- input_box . show ( )
128+ input_box . show ( )
129+ }
130+ )
131+
132+ if ( edited_message === 'back' ) {
133+ if ( has_default_config ) {
134+ if ( files_staged_by_action ) {
135+ await vscode . commands . executeCommand ( 'git.unstageAll' )
136+ } else {
137+ return
138+ }
139+ }
140+ continue
87141 }
88- )
89142
90- if ( edited_message ) {
91- repository . inputBox . value = edited_message
92- await vscode . commands . executeCommand ( 'git.commit' , repository )
93- } else if ( was_empty_stage ) {
94- await vscode . commands . executeCommand ( 'git.unstageAll' )
143+ if ( edited_message ) {
144+ repository . inputBox . value = edited_message
145+ await vscode . commands . executeCommand ( 'git.commit' , repository )
146+ } else if ( was_empty_stage ) {
147+ await vscode . commands . executeCommand ( 'git.unstageAll' )
148+ }
149+ } else {
150+ repository . inputBox . value = commit_message
95151 }
96- } else {
97- repository . inputBox . value = commit_message
152+ } else if ( was_empty_stage ) {
153+ await vscode . commands . executeCommand ( 'git.unstageAll' )
98154 }
99- } else if ( was_empty_stage ) {
100- await vscode . commands . executeCommand ( 'git.unstageAll' )
155+
156+ break
101157 }
102158 }
103159
0 commit comments