@@ -11,6 +11,33 @@ import * as path from 'path'
1111import { execSync } from 'child_process'
1212import { handle_accept_commit_message } from '../handle-accept-commit-message'
1313
14+ function unstage_changes ( repository : GitRepository ) {
15+ let has_commits = false
16+ try {
17+ execSync ( 'git rev-parse HEAD' , {
18+ cwd : repository . rootUri . fsPath ,
19+ stdio : 'ignore'
20+ } )
21+ has_commits = true
22+ } catch ( e ) {
23+ has_commits = false
24+ }
25+
26+ try {
27+ if ( has_commits ) {
28+ execSync ( 'git reset' , { cwd : repository . rootUri . fsPath } )
29+ } else {
30+ execSync ( 'git rm --cached -r .' , { cwd : repository . rootUri . fsPath } )
31+ }
32+ } catch ( error ) {
33+ Logger . error ( {
34+ function_name : 'unstage_changes' ,
35+ message : 'Failed to unstage changes' ,
36+ data : error
37+ } )
38+ }
39+ }
40+
1441async function proceed_with_commit_generation (
1542 panel_provider : PanelProvider ,
1643 repository : GitRepository ,
@@ -20,7 +47,7 @@ async function proceed_with_commit_generation(
2047 const api_config = await get_commit_message_config ( panel_provider . context )
2148 if ( ! api_config ) {
2249 if ( was_empty_stage ) {
23- await vscode . commands . executeCommand ( 'git.unstageAll' )
50+ unstage_changes ( repository )
2451 }
2552 panel_provider . send_message ( { command : 'COMMIT_PROCESS_CANCELLED' } )
2653 return
@@ -35,7 +62,7 @@ async function proceed_with_commit_generation(
3562 dictionary . information_message . NO_CHANGES_TO_COMMIT
3663 )
3764 if ( was_empty_stage ) {
38- await vscode . commands . executeCommand ( 'git.unstageAll' )
65+ unstage_changes ( repository )
3966 }
4067 panel_provider . send_message ( { command : 'COMMIT_PROCESS_CANCELLED' } )
4168 return
@@ -55,7 +82,7 @@ async function proceed_with_commit_generation(
5582
5683 if ( ! commit_message ) {
5784 if ( panel_provider . commit_was_staged_by_script ) {
58- await vscode . commands . executeCommand ( 'git.unstageAll' )
85+ unstage_changes ( repository )
5986 }
6087 panel_provider . send_message ( { command : 'COMMIT_PROCESS_CANCELLED' } )
6188 panel_provider . commit_was_staged_by_script = false
@@ -79,7 +106,7 @@ async function proceed_with_commit_generation(
79106 }
80107 } catch ( error ) {
81108 if ( panel_provider . commit_was_staged_by_script ) {
82- await vscode . commands . executeCommand ( 'git.unstageAll' )
109+ unstage_changes ( repository )
83110 }
84111 panel_provider . send_message ( { command : 'COMMIT_PROCESS_CANCELLED' } )
85112 panel_provider . commit_was_staged_by_script = false
@@ -99,12 +126,14 @@ export const handle_commit_changes = async (
99126) : Promise < void > => {
100127 await vscode . workspace . saveAll ( )
101128
102- const repository = get_git_repository ( )
129+ const repository = await get_git_repository ( )
103130 if ( ! repository ) {
104131 panel_provider . send_message ( { command : 'COMMIT_PROCESS_CANCELLED' } )
105132 return
106133 }
107134
135+ ; ( panel_provider as any ) . _selected_repository = repository
136+
108137 await repository . status ( )
109138
110139 panel_provider . commit_was_staged_by_script = false
@@ -138,12 +167,15 @@ export const handle_proceed_with_commit = async (
138167 panel_provider : PanelProvider ,
139168 files_to_stage : string [ ]
140169) : Promise < void > => {
141- const repository = get_git_repository ( )
170+ const repository : GitRepository | undefined = ( panel_provider as any )
171+ . _selected_repository
142172 if ( ! repository ) {
173+ vscode . window . showErrorMessage (
174+ 'No repository selected for committing changes.'
175+ )
143176 panel_provider . send_message ( { command : 'COMMIT_PROCESS_CANCELLED' } )
144177 return
145178 }
146-
147179 if ( files_to_stage . length === 0 ) {
148180 panel_provider . send_message ( { command : 'COMMIT_PROCESS_CANCELLED' } )
149181 return
0 commit comments