App-GitGerrit
view release on metacpan or search on metacpan
lib/App/GitGerrit.pm view on Meta::CPAN
my ($username) = url_userinfo($baseurl);
$credential{username} = $username if $username;
require File::Temp;
my $fh = File::Temp->new();
while (my ($key, $value) = each %credential) {
$fh->print("$key=$value\n") if $value;
}
$fh->print("\n\n");
$fh->close();
return ($fh, $fh->filename);
}
my $git_credential_supported = 1;
sub get_credentials {
my $baseurl = URI->new(config('baseurl'));
my ($fh, $credfile) = credential_description_file($baseurl);
my %credentials;
debug "Get credentials from git-credential";
open my $pipe, '-|', "git credential fill <$credfile"
or error "Can't open pipe to git-credential: $!";
while (<$pipe>) {
chomp;
$credentials{$1} = $2 if /^([^=]+)=(.*)/;
}
unless (close $pipe) {
error "Can't close pipe to git-credential: $!" if $!;
# If we get here it is because the shell invoked by open
# above couldn't exec git-credential, which most probably
# means that we're using a pre-1.8 Git, which doesn't
# support git-credential yet.
$git_credential_supported = 0;
}
my ($username, $password) = @credentials{qw/username password/};
unless (defined $username && defined $password) {
debug "Get credentials from git-gerrit.baseurl";
($username, $password) = url_userinfo(config('baseurl'));
}
unless (defined $username && defined $password) {
debug "Get credentials from a .netrc file";
if (eval {require Net::Netrc}) {
if (my $mach = Net::Netrc->lookup(URI->new(config('baseurl'))->host, $username)) {
($username, $password) = ($mach->login, $mach->password);
}
} else {
debug "Failed to require Net::Netrc";
}
}
unless (defined $username && defined $password) {
debug "Prompt the user for the credentials";
if (eval {require Term::Prompt}) {
$username = Term::Prompt::prompt('x', 'Gerrit username: ', '', $ENV{USER});
$password = Term::Prompt::prompt('p', 'Gerrit password: ', '');
print "\n";
} else {
debug "Failed to require Term::Prompt";
}
}
defined $username or error "Couldn't get credential's username";
defined $password or error "Couldn't get credential's password";
return ($username, $password);
}
sub set_credentials {
my ($username, $password, $what) = @_;
return 1 unless $git_credential_supported;
$what =~ /^(?:approve|reject)$/
or error "set_credentials \$what argument ($what) must be either 'approve' or 'reject'";
my $baseurl = URI->new(config('baseurl'));
my ($fh, $credfile) = credential_description_file($baseurl, $password);
return system("git credential $what <$credfile") == 0;
}
# The get_message routine returns the message argument to the
# --message option. If the option is not present it invokes the git
# editor to let the user compose a message and returns it.
sub get_message {
return $Options{message} if exists $Options{message};
chomp(my $editor = qx/git var GIT_EDITOR/);
error "Please, read 'git help var' to know how to set up an editor for git messages."
unless $editor;
require File::Temp;
my $tmp = File::Temp->new();
my $filename = $tmp->filename;
{
open my $fh, '>', $filename
or error "Can't open file for writing ($filename): $!\n";
print $fh <<'EOF';
# Please enter the review message for this change. Lines starting
# with '#' will be ignored, and an empty message aborts the review.
EOF
close $fh;
}
cmd "$editor $filename"
or error "Aborting because I couldn't invoke '$editor $filename'.";
my $message;
{
open my $fh, '<', $filename
or error "Can't open file for reading ($filename): $!\n";
( run in 1.142 second using v1.01-cache-2.11-cpan-6aa56a78535 )