diff --git a/git-svn.perl b/git-svn.perl index eeb83d3759..ade88ae7e2 100755 --- a/git-svn.perl +++ b/git-svn.perl @@ -4415,25 +4415,7 @@ sub username { sub _read_password { my ($prompt, $realm) = @_; - my $password = ''; - if (exists $ENV{GIT_ASKPASS}) { - open(PH, "-|", $ENV{GIT_ASKPASS}, $prompt); - $password = ; - $password =~ s/[\012\015]//; # \n\r - close(PH); - } else { - print STDERR $prompt; - STDERR->flush; - require Term::ReadKey; - Term::ReadKey::ReadMode('noecho'); - while (defined(my $key = Term::ReadKey::ReadKey(0))) { - last if $key =~ /[\012\015]/; # \n\r - $password .= $key; - } - Term::ReadKey::ReadMode('restore'); - print STDERR "\n"; - STDERR->flush; - } + my $password = Git::prompt($prompt); $password; } diff --git a/perl/Git.pm b/perl/Git.pm index f7ce511bbb..46f11a89de 100644 --- a/perl/Git.pm +++ b/perl/Git.pm @@ -58,7 +58,7 @@ require Exporter; command_output_pipe command_input_pipe command_close_pipe command_bidi_pipe command_close_bidi_pipe version exec_path html_path hash_object git_cmd_try - remote_refs + remote_refs prompt temp_acquire temp_release temp_reset temp_path); @@ -512,6 +512,55 @@ C). Useful mostly only internally. sub html_path { command_oneline('--html-path') } +=item prompt ( PROMPT ) + +Query user C and return answer from user. + +If an external helper is specified via GIT_ASKPASS or SSH_ASKPASS, it +is used to interact with the user; otherwise the prompt is given to +and the answer is read from the terminal. + +=cut + +sub prompt { + my ($prompt) = @_; + my $ret; + if (!defined $ret) { + $ret = _prompt($ENV{'GIT_ASKPASS'}, $prompt); + } + if (!defined $ret) { + $ret = _prompt($ENV{'SSH_ASKPASS'}, $prompt); + } + if (!defined $ret) { + $ret = ''; + print STDERR $prompt; + STDERR->flush; + require Term::ReadKey; + Term::ReadKey::ReadMode('noecho'); + while (defined(my $key = Term::ReadKey::ReadKey(0))) { + last if $key =~ /[\012\015]/; # \n\r + $ret .= $key; + } + Term::ReadKey::ReadMode('restore'); + print STDERR "\n"; + STDERR->flush; + } + return $ret; +} + +sub _prompt { + my ($askpass, $prompt) = @_; + return unless ($askpass); + + open my $fh, "-|", $askpass, $prompt + or return; + my $ret = <$fh>; + $ret =~ s/[\012\015]//g; # \n\r + close ($fh); + return $ret; +} + + =item repo_path () Return path to the git repository. Must be called on a repository instance.