App-GitGerrit
view release on metacpan or search on metacpan
lib/App/GitGerrit.pm view on Meta::CPAN
* baseurl:
EOF
$config->{'git-gerrit'}{project} = [substr($path, length($prefix))];
} else {
$config->{'git-gerrit'}{project} = [$path];
}
}
$config->{'git-gerrit'}{project}[-1] =~ s:^/+::; # strip leading slashes
}
return $config;
}
# The config routine returns the last value associated with Git's
# git-gerrit.$var configuration variable, as output by the 'git config
# -l' command, or undef if the variable isn't defined.
sub config {
my ($var) = @_;
state $config = grok_config;
return exists $config->{'git-gerrit'}{$var} ? $config->{'git-gerrit'}{$var}[-1] : undef;
}
# The configs routine returns all values associated with Git's
# git-gerrit.$var configuration variable or the empty list if the
# variable isn't defined.
sub configs {
my ($var) = @_;
state $config = grok_config;
return exists $config->{'git-gerrit'}{$var} ? @{$config->{'git-gerrit'}{$var}} : ();
}
# The install_commit_msg_hook routine is invoked by a few of
# git-gerrit subcommands. It checks if the current repository already
# has a commit-msg hook installed. If not, it tries to download and
# install Gerrit's default commit-msg hook, which inserts Change-Ids
# in commits messages.
sub install_commit_msg_hook {
require File::Spec;
chomp(my $git_dir = qx/git rev-parse --git-dir/);
# Do nothing if it already exists
my $commit_msg = File::Spec->catfile($git_dir, 'hooks', 'commit-msg');
return if -e $commit_msg;
# Otherwise, check if we need to mkdir the hooks directory
my $hooks_dir = File::Spec->catdir($git_dir, 'hooks');
mkdir $hooks_dir unless -e $hooks_dir;
# Try to download and install the hook.
eval { require LWP::Simple };
if ($@) {
info "Cannot install $commit_msg hook because you don't have LWP::Simple installed";
} else {
info "Installing $commit_msg hook";
if (LWP::Simple::is_success(LWP::Simple::getstore(config('baseurl') . "/tools/hooks/commit-msg", $commit_msg))) {
chmod 0755, $commit_msg;
}
}
}
# The credential_* routines below use the git-credential command to
# get and set credentials for git commands and also for Gerrit REST
# interactions.
sub url_userinfo {
my ($url) = @_;
if (my $userinfo = $url->userinfo) {
return split /:/, $userinfo, 2;
} else {
return (undef, undef);
}
}
sub credential_description_file {
my ($baseurl, $password) = @_;
my %credential = (
protocol => $baseurl->scheme,
host => $baseurl->host,
path => $baseurl->path,
password => $password,
);
# Try to get the username from the baseurl
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 $!;
( run in 0.508 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )