package Hydra::Plugin::GitInput;
use strict;
use warnings;
use parent 'Hydra::Plugin';
use Digest::SHA qw(sha256_hex);
use File::Path;
use Hydra::Helper::Nix;
use Nix::Store;
use Encode;
use Fcntl qw(:flock);
use Env;
use Data::Dumper;
my $CONFIG_SECTION = "git-input";
sub supportedInputTypes {
my ($self, $inputTypes) = @_;
$inputTypes->{'git'} = 'Git checkout';
}
sub _isHash {
my ($rev) = @_;
return length($rev) == 40 && $rev =~ /^[0-9a-f]+$/;
}
sub _parseValue {
my ($value) = @_;
my @parts = split ' ', $value;
(my $uri, my $branch, my $deepClone) = @parts;
$branch = defined $branch ? $branch : "master";
my $options = {};
my $start_options = 3;
if (defined($deepClone) && index($deepClone, "=") != -1) {
undef $deepClone;
$start_options = 2;
}
foreach my $option (@parts[$start_options .. $#parts]) {
(my $key, my $value) = split('=', $option);
$options->{$key} = $value;
}
return ($uri, $branch, $deepClone, $options);
}
sub _printIfDebug {
my ($msg) = @_;
print STDERR "GitInput: $msg" if $ENV{'HYDRA_DEBUG'};
}
sub _pluginConfig {
my ($main_config, $project_name, $jobset_name, $input_name) = @_;
my $cfg = $main_config->{$CONFIG_SECTION};
my $values = {
timeout => 600,
};
my $input_block = "$project_name:$jobset_name:$input_name";
unless (defined $cfg) {
_printIfDebug "Unable to load $CONFIG_SECTION section\n";
_printIfDebug "Using default values\n";
return $values;
} else {
_printIfDebug "Parsing plugin configuration:\n";
_printIfDebug Dumper($cfg);
}
if (defined $cfg->{$input_block} and %{$cfg->{$input_block}}) {
_printIfDebug "Merging sections from $input_block\n";
$cfg = {%{$cfg}, %{$cfg->{$input_block}}};
}
if (exists $cfg->{timeout}) {
$values->{timeout} = int($cfg->{timeout});
_printIfDebug "Using custom timeout for $input_block:\n";
} else {
_printIfDebug "Using default timeout for $input_block:\n";
}
_printIfDebug "$values->{timeout}\n";
return $values;
}
sub fetchInput {
my ($self, $type, $name, $value, $project, $jobset) = @_;
return undef if $type ne "git";
my ($uri, $branch, $deepClone, $options) = _parseValue($value);
my $cfg = _pluginConfig($self->{config},
$project->get_column('name'),
$jobset->get_column('name'),
$name);
foreach my $opt_name (keys %{$options}) {
my $opt_value = $options->{$opt_name};
if ($opt_value =~ /^[+-]?\d+\z/) {
$opt_value = int($opt_value);
}
$cfg->{$opt_name} = $opt_value;
_printIfDebug "'$name': override '$opt_name' with input value: $opt_value\n";
}
my $cacheDir = getSCMCacheDir . "/git";
mkpath($cacheDir);
my $clonePath = $cacheDir . "/" . sha256_hex($uri);
open(my $lock, ">", "$clonePath.lock") or die;
flock($lock, LOCK_EX) or die;
my $res;
if (! -d $clonePath) {
$res = run(cmd => ["git", "init", $clonePath]);
$res = run(cmd => ["git", "remote", "add", "origin", "--", $uri], dir => $clonePath) unless $res->{status};
die "error creating git repo in `$clonePath':\n$res->{stderr}" if $res->{status};
}
my $localBranch = _isHash($branch) ? "_hydra_tmp" : $branch;
$res = run(cmd => ["git", "fetch", "-fu", "origin", "+$branch:$localBranch"], dir => $clonePath,
timeout => $cfg->{timeout});
$res = run(cmd => ["git", "fetch", "-fu", "origin"], dir => $clonePath, timeout => $cfg->{timeout}) if $res->{status};
die "error fetching latest change from git repo at `$uri':\n$res->{stderr}" if $res->{status};
if (defined $deepClone) {
$res = run(cmd => ["git", "ls-tree", "-r", "$branch", ".topgit"], dir => $clonePath);
if ($res->{stdout} ne "") {
$res = run(cmd => ["git", "checkout", "--force", "$branch"], dir => $clonePath);
die "error checking out Git branch '$branch' at `$uri':\n$res->{stderr}" if $res->{status};
$res = run(cmd => ["tg", "remote", "--populate", "origin"], dir => $clonePath, timeout => $cfg->{timeout});
print STDERR "warning: `tg remote --populate origin' failed:\n$res->{stderr}" if $res->{status};
}
}
my $timestamp = time;
my $sha256;
my $storePath;
my $revision = _isHash($branch) ? $branch
: grab(cmd => ["git", "rev-parse", "$branch"], dir => $clonePath, chomp => 1);
die "did not get a well-formated revision number of Git branch '$branch' at `$uri'"
unless $revision =~ /^[0-9a-fA-F]+$/;
my $cachedInput;
($cachedInput) = $self->{db}->resultset('CachedGitInputs')->search(
{uri => $uri, branch => $branch, revision => $revision, isdeepclone => defined($deepClone) ? 1 : 0},
{rows => 1});
addTempRoot($cachedInput->storepath) if defined $cachedInput;
if (defined $cachedInput && isValidPath($cachedInput->storepath)) {
$storePath = $cachedInput->storepath;
$sha256 = $cachedInput->sha256hash;
$revision = $cachedInput->revision;
} else {
print STDERR "checking out Git branch $branch from $uri\n";
$ENV{"NIX_HASH_ALGO"} = "sha256";
$ENV{"PRINT_PATH"} = "1";
$ENV{"NIX_PREFETCH_GIT_LEAVE_DOT_GIT"} = "0";
$ENV{"NIX_PREFETCH_GIT_DEEP_CLONE"} = "";
if (defined $deepClone) {
$ENV{"NIX_PREFETCH_GIT_LEAVE_DOT_GIT"} = "1";
$ENV{"NIX_PREFETCH_GIT_DEEP_CLONE"} = "1";
}
($sha256, $storePath) = split ' ', grab(cmd => ["nix-prefetch-git", $clonePath, $revision], chomp => 1);
addTempRoot($storePath);
$self->{db}->txn_do(sub {
$self->{db}->resultset('CachedGitInputs')->update_or_create(
{ uri => $uri
, branch => $branch
, revision => $revision
, isdeepclone => defined($deepClone) ? 1 : 0
, sha256hash => $sha256
, storepath => $storePath
});
});
}
my $revCount = grab(cmd => ["git", "rev-list", "--count", "$revision"], dir => $clonePath, chomp => 1);
my $gitTag = grab(cmd => ["git", "describe", "--always", "$revision"], dir => $clonePath, chomp => 1);
my $shortRev = grab(cmd => ["git", "rev-parse", "--short", "$revision"], dir => $clonePath, chomp => 1);
return
{ uri => $uri
, storePath => $storePath
, sha256hash => $sha256
, revision => $revision
, revCount => int($revCount)
, gitTag => $gitTag
, shortRev => $shortRev
};
}
sub getCommits {
my ($self, $type, $value, $rev1, $rev2) = @_;
return [] if $type ne "git";
return [] unless $rev1 =~ /^[0-9a-f]+$/;
return [] unless $rev2 =~ /^[0-9a-f]+$/;
my ($uri, $branch, $deepClone) = _parseValue($value);
my $clonePath = getSCMCacheDir . "/git/" . sha256_hex($uri);
my $out = grab(cmd => ["git", "log", "--pretty=format:%H%x09%an%x09%ae%x09%at", "$rev1..$rev2"], dir => $clonePath);
my $res = [];
foreach my $line (split /\n/, $out) {
my ($revision, $author, $email, $date) = split "\t", $line;
push @$res, { revision => $revision, author => decode("utf-8", $author), email => $email };
}
return $res;
}
1;