GitInput.pm: Don't do a chdir to the Git clone

Doing a chdir in the parent is evil.  For instance, we had Hydra core
dumps ending up in the cloned directory.  Therefore, the function
‘run’ allows doing a chdir in the child.  The function ‘grab’ returns
the child's stdout and throws an exception if the child fails.
This commit is contained in:
Eelco Dolstra 2013-08-12 17:25:59 +02:00
parent 1481badf21
commit d96df42c03
2 changed files with 55 additions and 39 deletions

View file

@ -20,7 +20,7 @@ our @EXPORT = qw(
getMainOutput getMainOutput
getEvals getMachines getEvals getMachines
pathIsInsidePrefix pathIsInsidePrefix
captureStdoutStderr); captureStdoutStderr run grab);
sub getHydraHome { sub getHydraHome {
@ -472,4 +472,40 @@ sub captureStdoutStderr {
} }
sub run {
my (%args) = @_;
my $res = { stdout => "", stderr => "" };
my $stdin = "";
eval {
local $SIG{ALRM} = sub { die "timeout\n" }; # NB: \n required
alarm $args{timeout} if defined $args{timeout};
my @x = ($args{cmd}, \$stdin, \$res->{stdout});
push @x, \$res->{stderr} if $args{grabStderr} // 1;
IPC::Run::run(@x,
init => sub { chdir $args{dir} or die "changing to $args{dir}" if defined $args{dir}; });
alarm 0;
};
if ($@) {
die unless $@ eq "timeout\n"; # propagate unexpected errors
$res->{status} = -1;
$res->{stderr} = "timeout\n";
} else {
$res->{status} = $?;
chomp $res->{stdout} if $args{chomp} // 0;
}
return $res;
}
sub grab {
my (%args) = @_;
my $res = run(%args, grabStderr => 0);
die "command `@{$args{cmd}}' failed with exit status $res->{status}" if $res->{status};
return $res->{stdout};
}
1; 1;

View file

@ -20,39 +20,34 @@ sub _cloneRepo {
mkpath($cacheDir); mkpath($cacheDir);
my $clonePath = $cacheDir . "/" . sha256_hex($uri); my $clonePath = $cacheDir . "/" . sha256_hex($uri);
my $stdout = ""; my $stderr = ""; my $res; my $res;
if (! -d $clonePath) { if (! -d $clonePath) {
# Clone everything and fetch the branch. # Clone everything and fetch the branch.
# TODO: Optimize the first clone by using "git init $clonePath" and "git remote add origin $uri". # TODO: Optimize the first clone by using "git init $clonePath" and "git remote add origin $uri".
($res, $stdout, $stderr) = captureStdoutStderr(600, "git", "clone", "--branch", $branch, $uri, $clonePath); $res = run(cmd => ["git", "clone", "--branch", $branch, $uri, $clonePath], timeout => 600);
die "error cloning git repo at `$uri':\n$stderr" if $res; die "error cloning git repo at `$uri':\n$res->{stderr}" if $res->{status};
} }
chdir $clonePath or die $!; # !!! urgh, shouldn't do a chdir
# This command forces the update of the local branch to be in the same as # This command forces the update of the local branch to be in the same as
# the remote branch for whatever the repository state is. This command mirrors # the remote branch for whatever the repository state is. This command mirrors
# only one branch of the remote repository. # only one branch of the remote repository.
($res, $stdout, $stderr) = captureStdoutStderr(600, $res = run(cmd => ["git", "fetch", "-fu", "origin", "+$branch:$branch"], dir => $clonePath, timeout => 600);
"git", "fetch", "-fu", "origin", "+$branch:$branch"); $res = run(cmd => ["git", "fetch", "-fu", "origin"], dir => $clonePath, timeout => 600) if $res->{status};
($res, $stdout, $stderr) = captureStdoutStderr(600, die "error fetching latest change from git repo at `$uri':\n$res->{stderr}" if $res->{status};
"git", "fetch", "-fu", "origin") if $res;
die "error fetching latest change from git repo at `$uri':\n$stderr" if $res;
# If deepClone is defined, then we look at the content of the repository # If deepClone is defined, then we look at the content of the repository
# to determine if this is a top-git branch. # to determine if this is a top-git branch.
if (defined $deepClone) { if (defined $deepClone) {
# Checkout the branch to look at its content. # Checkout the branch to look at its content.
($res, $stdout, $stderr) = captureStdoutStderr(600, "git", "checkout", "$branch"); $res = run(cmd => ["git", "checkout", "$branch"], dir => $clonePath);
die "error checking out Git branch '$branch' at `$uri':\n$stderr" if $res; die "error checking out Git branch '$branch' at `$uri':\n$res->{stderr}" if $res->{status};
if (-f ".topdeps") { if (-f ".topdeps") {
# This is a TopGit branch. Fetch all the topic branches so # This is a TopGit branch. Fetch all the topic branches so
# that builders can run "tg patch" and similar. # that builders can run "tg patch" and similar.
($res, $stdout, $stderr) = captureStdoutStderr(600, $res = run(cmd => ["tg", "remote", "--populate", "origin"], dir => $clonePath, timeout => 600);
"tg", "remote", "--populate", "origin"); print STDERR "warning: `tg remote --populate origin' failed:\n$res->{stderr}" if $res->{status};
print STDERR "warning: `tg remote --populate origin' failed:\n$stderr" if $res;
} }
} }
@ -64,7 +59,6 @@ sub _parseValue {
(my $uri, my $branch, my $deepClone) = split ' ', $value; (my $uri, my $branch, my $deepClone) = split ' ', $value;
$branch = defined $branch ? $branch : "master"; $branch = defined $branch ? $branch : "master";
return ($uri, $branch, $deepClone); return ($uri, $branch, $deepClone);
} }
sub fetchInput { sub fetchInput {
@ -80,19 +74,13 @@ sub fetchInput {
my $sha256; my $sha256;
my $storePath; my $storePath;
my ($res, $stdout, $stderr) = captureStdoutStderr(600, my $revision = grab(cmd => ["git", "rev-parse", "$branch"], dir => $clonePath, chomp => 1);
("git", "rev-parse", "$branch")); die "did not get a well-formated revision number of Git branch '$branch' at `$uri'"
die "error getting revision number of Git branch '$branch' at `$uri':\n$stderr" if $res;
my ($revision) = split /\n/, $stdout;
die "error getting a well-formated revision number of Git branch '$branch' at `$uri':\n$stdout"
unless $revision =~ /^[0-9a-fA-F]+$/; unless $revision =~ /^[0-9a-fA-F]+$/;
my $ref = "refs/heads/$branch";
# Some simple caching: don't check a uri/branch/revision more than once. # Some simple caching: don't check a uri/branch/revision more than once.
# TODO: Fix case where the branch is reset to a previous commit. # TODO: Fix case where the branch is reset to a previous commit.
my $cachedInput ; my $cachedInput;
($cachedInput) = $self->{db}->resultset('CachedGitInputs')->search( ($cachedInput) = $self->{db}->resultset('CachedGitInputs')->search(
{uri => $uri, branch => $branch, revision => $revision}, {uri => $uri, branch => $branch, revision => $revision},
{rows => 1}); {rows => 1});
@ -123,10 +111,7 @@ sub fetchInput {
$ENV{"NIX_PREFETCH_GIT_DEEP_CLONE"} = "1"; $ENV{"NIX_PREFETCH_GIT_DEEP_CLONE"} = "1";
} }
($res, $stdout, $stderr) = captureStdoutStderr(600, "nix-prefetch-git", $clonePath, $revision); ($sha256, $storePath) = split ' ', grab(cmd => ["nix-prefetch-git", $clonePath, $revision], chomp => 1);
die "cannot check out Git repository branch '$branch' at `$uri':\n$stderr" if $res;
($sha256, $storePath) = split ' ', $stdout;
txn_do($self->{db}, sub { txn_do($self->{db}, sub {
$self->{db}->resultset('CachedGitInputs')->update_or_create( $self->{db}->resultset('CachedGitInputs')->update_or_create(
@ -143,12 +128,9 @@ sub fetchInput {
# number of commits in the history of this revision (revCount) # number of commits in the history of this revision (revCount)
# the output of git-describe (gitTag), and the abbreviated # the output of git-describe (gitTag), and the abbreviated
# revision (shortRev). # revision (shortRev).
my $revCount = `git rev-list $revision | wc -l`; chomp $revCount; my $revCount = scalar(split '\n', grab(cmd => ["git", "rev-list", "$revision"], dir => $clonePath));
die "git rev-list failed" if $? != 0; my $gitTag = grab(cmd => ["git", "describe", "--always", "$revision"], dir => $clonePath, chomp => 1);
my $gitTag = `git describe --always $revision`; chomp $gitTag; my $shortRev = grab(cmd => ["git", "rev-parse", "--short", "$revision"], dir => $clonePath, chomp => 1);
die "git describe failed" if $? != 0;
my $shortRev = `git rev-parse --short $revision`; chomp $shortRev;
die "git rev-parse failed" if $? != 0;
return return
{ uri => $uri { uri => $uri
@ -172,9 +154,7 @@ sub getCommits {
my $clonePath = $self->_cloneRepo($uri, $branch, $deepClone); my $clonePath = $self->_cloneRepo($uri, $branch, $deepClone);
my $out; my $out = grab(cmd => ["git", "log", "--pretty=format:%H%x09%an%x09%ae%x09%at", "$rev1..$rev2"], dir => $clonePath);
IPC::Run::run(["git", "log", "--pretty=format:%H%x09%an%x09%ae%x09%at", "$rev1..$rev2"], \undef, \$out)
or die "cannot get git logs: $?";
my $res = []; my $res = [];
foreach my $line (split /\n/, $out) { foreach my $line (split /\n/, $out) {