Skip to content

Commit 7be311a

Browse files
committed
GitInput: Determine default branch for repository
Up until now when no branch was defined the Git Plugin hardcoded the master branch. Now we try resolving the default branch with git ls-remote and allow a configurable fallback branch, that can be master or anything else.
1 parent cd235f7 commit 7be311a

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

subprojects/hydra/lib/Hydra/Plugin/GitInput.pm

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ sub _parseValue {
2828
my ($value) = @_;
2929
my @parts = split ' ', $value;
3030
(my $uri, my $branch, my $deepClone) = @parts;
31-
$branch = defined $branch ? $branch : "master";
3231
my $options = {};
3332
my $start_options = 3;
3433
# if deepClone has "=" then is considered an option
@@ -44,6 +43,23 @@ sub _parseValue {
4443
return ($uri, $branch, $deepClone, $options);
4544
}
4645

46+
sub _getDefaultBranch {
47+
my ($uri, $timeout) = @_;
48+
49+
my $res = runCommand(
50+
cmd => ["git", "ls-remote", "--symref", $uri, "HEAD"],
51+
timeout => $timeout,
52+
);
53+
54+
return undef if $res->{status};
55+
56+
for (split /\n/, $res->{stdout}) {
57+
return $1 if /^ref:\s+refs\/heads\/([^\s]+)/;
58+
}
59+
60+
return undef;
61+
}
62+
4763
sub _printIfDebug {
4864
my ($msg) = @_;
4965
print STDERR "GitInput: $msg" if $ENV{'HYDRA_DEBUG'};
@@ -55,7 +71,8 @@ Read the configuration from the main hydra config file.
5571
5672
The configuration is loaded from the "git-input" block.
5773
58-
Currently only the "timeout" variable is been looked up in the file.
74+
Currently the "fallback_branch" and a "timeout" variable are looked up in the
75+
file.
5976
6077
The variables defined directly in the input value will override
6178
the ones on the configuration file, to define the variables
@@ -67,6 +84,9 @@ Expected configuration format in the hydra config file:
6784
# general timeout
6885
timeout = 400
6986
87+
# branch, if no default branch can be determined and none is specified
88+
fallback_branch = main
89+
7090
<project:jobset:input-name>
7191
# specific timeout for a particular input
7292
timeout = 400
@@ -79,6 +99,7 @@ sub _pluginConfig {
7999
my $cfg = $main_config->{$CONFIG_SECTION};
80100
# default values
81101
my $values = {
102+
fallback_branch => "master",
82103
timeout => 600,
83104
};
84105
my $input_block = "$project_name:$jobset_name:$input_name";
@@ -126,6 +147,11 @@ sub fetchInput {
126147
_printIfDebug "'$name': override '$opt_name' with input value: $opt_value\n";
127148
}
128149

150+
# Determine a branch to check out, if none is explicitly defined
151+
if (!defined $branch) {
152+
$branch = _getDefaultBranch($uri, $cfg->{timeout}) // $cfg->{fallback_branch} // "master";
153+
}
154+
129155
# Clone or update a branch of the repository into our SCM cache.
130156
my $cacheDir = getSCMCacheDir . "/git";
131157
mkpath($cacheDir);

0 commit comments

Comments
 (0)