@@ -419,6 +419,55 @@ static int getchar_with_timeout(int timeout)
419419 return getchar ();
420420}
421421
422+ static char * shell_prompt (const char * prompt , int echo )
423+ {
424+ const char * read_input [] = {
425+ /* Note: call 'bash' explicitly, as 'read -s' is bash-specific */
426+ "bash" , "-c" , echo ?
427+ "cat >/dev/tty && read -r line </dev/tty && echo \"$line\"" :
428+ "cat >/dev/tty && read -r -s line </dev/tty && echo \"$line\" && echo >/dev/tty" ,
429+ NULL
430+ };
431+ struct child_process child = CHILD_PROCESS_INIT ;
432+ static struct strbuf buffer = STRBUF_INIT ;
433+ int prompt_len = strlen (prompt ), len = -1 , code ;
434+
435+ strvec_pushv (& child .args , read_input );
436+ child .in = -1 ;
437+ child .out = -1 ;
438+ child .silent_exec_failure = 1 ;
439+
440+ if (start_command (& child ))
441+ return NULL ;
442+
443+ if (write_in_full (child .in , prompt , prompt_len ) != prompt_len ) {
444+ error ("could not write to prompt script" );
445+ close (child .in );
446+ goto ret ;
447+ }
448+ close (child .in );
449+
450+ strbuf_reset (& buffer );
451+ len = strbuf_read (& buffer , child .out , 1024 );
452+ if (len < 0 ) {
453+ error ("could not read from prompt script" );
454+ goto ret ;
455+ }
456+
457+ strbuf_strip_suffix (& buffer , "\n" );
458+ strbuf_strip_suffix (& buffer , "\r" );
459+
460+ ret :
461+ close (child .out );
462+ code = finish_command (& child );
463+ if (code ) {
464+ error ("failed to execute prompt script (exit code %d)" , code );
465+ return NULL ;
466+ }
467+
468+ return len < 0 ? NULL : buffer .buf ;
469+ }
470+
422471#endif
423472
424473#ifndef FORCE_TEXT
@@ -431,6 +480,15 @@ char *git_terminal_prompt(const char *prompt, int echo)
431480 int r ;
432481 FILE * input_fh , * output_fh ;
433482
483+ #ifdef GIT_WINDOWS_NATIVE
484+
485+ /* try shell_prompt first, fall back to CONIN/OUT if bash is missing */
486+ char * result = shell_prompt (prompt , echo );
487+ if (result )
488+ return result ;
489+
490+ #endif
491+
434492 input_fh = fopen (INPUT_PATH , "r" FORCE_TEXT );
435493 if (!input_fh )
436494 return NULL ;
0 commit comments