From 08b7f8028533c257c38e3033df1594b88bfd479d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tina=20M=C3=BCller?= Date: Wed, 31 Jul 2024 14:00:47 +0200 Subject: [PATCH] Support git_auto_clone for empty CASDIR/NEEDLES_DIR Issue: https://progress.opensuse.org/issues/162632 --- lib/OpenQA/Task/Git/Clone.pm | 68 ++++++++++++++++++++++++++---------- lib/OpenQA/Utils.pm | 4 +++ 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/lib/OpenQA/Task/Git/Clone.pm b/lib/OpenQA/Task/Git/Clone.pm index 9fc27698859..2b77844bb82 100644 --- a/lib/OpenQA/Task/Git/Clone.pm +++ b/lib/OpenQA/Task/Git/Clone.pm @@ -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 diff-index '$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); diff --git a/lib/OpenQA/Utils.pm b/lib/OpenQA/Utils.pm index e79a5727877..f6dc2aea601 100644 --- a/lib/OpenQA/Utils.pm +++ b/lib/OpenQA/Utils.pm @@ -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) {