Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof of Concept: Support git_auto_clone for empty CASDIR/NEEDLES_DIR #5808

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
perlpunk marked this conversation as resolved.
Show resolved Hide resolved
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
Loading