Skip to content

Commit

Permalink
Support git_auto_clone for empty CASDIR/NEEDLES_DIR
Browse files Browse the repository at this point in the history
  • Loading branch information
perlpunk committed Jul 31, 2024
1 parent f7f8498 commit d9bb715
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
68 changes: 50 additions & 18 deletions lib/OpenQA/Task/Git/Clone.pm
Original file line number Diff line number Diff line change
Expand Up @@ -83,35 +83,67 @@ sub _git_fetch ($path, $branch_arg) {
die "Failed to fetch from '$branch_arg': $r->{stderr}" unless $r->{status};
}

sub _git_gc ($path) {
my $r = run_cmd_with_log_return_error(_ssh_git_cmd(['-C', $path, 'gc', '--auto', '--quiet']));
die "Failed to gc '$path': $r->{stderr}" unless $r->{status};
}

sub _git_diff_index ($path) {
my $r = run_cmd_with_log_return_error(_ssh_git_cmd(['-C', $path, 'diff-index', 'HEAD', '--exit-code']));
unless ($r->{status}) {
# dirty git status
return "diff-index: $r->{stdout}" if $r->{return_code} >> 8 == 1;
die "Failed to gc '$path': $r->{stderr}";
}
return 0;
}

sub _git_reset_hard ($path, $branch) {
my $r = run_cmd_with_log_return_error(['git', '-C', $path, 'reset', '--hard', "origin/$branch"]);
die "Failed to reset to 'origin/$branch': $r->{stderr}" unless $r->{status};
}

sub _git_clone ($job, $ctx, $path, $url) {
$ctx->debug(qq{Updating $path to $url});
$url = Mojo::URL->new($url);
my $requested_branch = $url->fragment;
$url->fragment(undef);
my $remote_default_branch = _get_remote_default_branch($url);
$requested_branch ||= $remote_default_branch;
$ctx->debug(qq{Remote default branch $remote_default_branch});
die "Unable to detect remote default branch for '$url'" unless $remote_default_branch;

if (!-d $path) {
_git_clone_url_to_path($url, $path);
# update local branch to latest remote branch version
_git_fetch($path, "$requested_branch:$requested_branch")
if ($requested_branch ne $remote_default_branch);
my $requested_branch = '';
if ($url) {
# given a git url, do initial clone
$ctx->debug(qq{Updating $path to $url});
$url = Mojo::URL->new($url);
$requested_branch = $url->fragment;
$url->fragment(undef);
my $remote_default_branch = _get_remote_default_branch($url);
$requested_branch ||= $remote_default_branch;
$ctx->debug(qq{Remote default branch $remote_default_branch});
die "Unable to detect remote default branch for '$url'" unless $remote_default_branch;

if (!-d $path) {
_git_clone_url_to_path($url, $path);
# update local branch to latest remote branch version
_git_fetch($path, "$requested_branch:$requested_branch")
if ($requested_branch ne $remote_default_branch);
}

my $origin_url = _git_get_origin_url($path);
if ($url ne $origin_url) {
$ctx->warn("Local checkout at $path has origin $origin_url but requesting to clone from $url");
return;
}
}

my $origin_url = _git_get_origin_url($path);
if ($url ne $origin_url) {
$ctx->warn("Local checkout at $path has origin $origin_url but requesting to clone from $url");
else {
# look for existing clone in the given path
my $origin_url = _git_get_origin_url($path);
$ctx->debug(qq{Updating $path to $origin_url});
$requested_branch = _get_remote_default_branch($origin_url);
}
if (my $is_dirty = _git_diff_index($path)) {
$ctx->warn("Clone at '$path' has dirty status, not updating: $is_dirty");
return;
}

_git_gc($path);
my $current_branch = _get_current_branch($path);
# TODO deal with conflicts because of save_needle
# see fetchneedles
if ($requested_branch eq $current_branch) {
# updating default branch (including checkout)
_git_fetch($path, $requested_branch);
Expand Down
4 changes: 4 additions & 0 deletions lib/OpenQA/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,10 @@ sub create_downloads_list ($job_settings) {

sub create_git_clone_list ($job_settings, $clones = {}) {
my $distri = $job_settings->{DISTRI};
# Potential existing git clones to update
not $job_settings->{CASEDIR} and $clones->{testcasedir($distri)} = undef;
not $job_settings->{NEEDLES_DIR} and $clones->{needledir($distri)} = undef;

my $case_url = Mojo::URL->new($job_settings->{CASEDIR} // '');
my $needles_url = Mojo::URL->new($job_settings->{NEEDLES_DIR} // '');
if ($case_url->scheme) {
Expand Down

0 comments on commit d9bb715

Please sign in to comment.