From a8d20ee46e04acf0cd08405d6234ce6a0eca22aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6gler?= Date: Sun, 7 Feb 2021 19:41:01 +0100 Subject: [PATCH] GitInput: Avoid fetch if rev is available Similar to libfetcher in Nix, avoid fetching if rev is already available. --- src/lib/Hydra/Plugin/GitInput.pm | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/lib/Hydra/Plugin/GitInput.pm b/src/lib/Hydra/Plugin/GitInput.pm index 3fbf50f0..f2b3a653 100644 --- a/src/lib/Hydra/Plugin/GitInput.pm +++ b/src/lib/Hydra/Plugin/GitInput.pm @@ -141,14 +141,19 @@ sub fetchInput { die "error creating git repo in `$clonePath':\n$res->{stderr}" if $res->{status}; } - # 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 - # only one branch of the remote repository. - 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}; + $res = run(cmd => ["git", "cat-file", "-t", $branch], dir => $clonePath); + if (! _isHash($branch) || $res->{status}) { + # 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 + # only one branch of the remote repository. + 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}; + } else { + $res = run(cmd => ["git", "branch", "-f", "_hydra_tmp", "$branch"], dir => $clonePath, timeout => $cfg->{timeout}); + } # If deepClone is defined, then we look at the content of the repository # to determine if this is a top-git branch.