1- #include "git-compat-util .h"
1+ #include "cache .h"
22#include "compat/terminal.h"
33#include "sigchain.h"
44#include "strbuf.h"
@@ -194,6 +194,55 @@ static int mingw_getchar(void)
194194}
195195#define getchar mingw_getchar
196196
197+ static char * shell_prompt (const char * prompt , int echo )
198+ {
199+ const char * read_input [] = {
200+ /* Note: call 'bash' explicitly, as 'read -s' is bash-specific */
201+ "bash" , "-c" , echo ?
202+ "cat >/dev/tty && read -r line </dev/tty && echo \"$line\"" :
203+ "cat >/dev/tty && read -r -s line </dev/tty && echo \"$line\" && echo >/dev/tty" ,
204+ NULL
205+ };
206+ struct child_process child = CHILD_PROCESS_INIT ;
207+ static struct strbuf buffer = STRBUF_INIT ;
208+ int prompt_len = strlen (prompt ), len = -1 , code ;
209+
210+ child .argv = read_input ;
211+ child .in = -1 ;
212+ child .out = -1 ;
213+ child .silent_exec_failure = 1 ;
214+
215+ if (start_command (& child ))
216+ return NULL ;
217+
218+ if (write_in_full (child .in , prompt , prompt_len ) != prompt_len ) {
219+ error ("could not write to prompt script" );
220+ close (child .in );
221+ goto ret ;
222+ }
223+ close (child .in );
224+
225+ strbuf_reset (& buffer );
226+ len = strbuf_read (& buffer , child .out , 1024 );
227+ if (len < 0 ) {
228+ error ("could not read from prompt script" );
229+ goto ret ;
230+ }
231+
232+ strbuf_strip_suffix (& buffer , "\n" );
233+ strbuf_strip_suffix (& buffer , "\r" );
234+
235+ ret :
236+ close (child .out );
237+ code = finish_command (& child );
238+ if (code ) {
239+ error ("failed to execute prompt script (exit code %d)" , code );
240+ return NULL ;
241+ }
242+
243+ return len < 0 ? NULL : buffer .buf ;
244+ }
245+
197246#endif
198247
199248#ifndef FORCE_TEXT
@@ -206,6 +255,15 @@ char *git_terminal_prompt(const char *prompt, int echo)
206255 int r ;
207256 FILE * input_fh , * output_fh ;
208257
258+ #ifdef GIT_WINDOWS_NATIVE
259+
260+ /* try shell_prompt first, fall back to CONIN/OUT if bash is missing */
261+ char * result = shell_prompt (prompt , echo );
262+ if (result )
263+ return result ;
264+
265+ #endif
266+
209267 input_fh = fopen (INPUT_PATH , "r" FORCE_TEXT );
210268 if (!input_fh )
211269 return NULL ;
0 commit comments