Skip to content

Commit

Permalink
Merge c1bd6be into 3a57c93
Browse files Browse the repository at this point in the history
  • Loading branch information
ferki committed Mar 11, 2023
2 parents 3a57c93 + c1bd6be commit f82840e
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 52 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Revision history for Rex
[API CHANGES]

[BUG FIXES]
- Fix git cloning into an existing empty directory

[DOCUMENTATION]

Expand Down
100 changes: 48 additions & 52 deletions lib/Rex/SCM/Git.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,54 @@ sub checkout {
$run_opt{env} = $checkout_opt->{env} if ( $checkout_opt->{env} );
my $clone_args = join( " ", @{ $checkout_opt->{clone_args} || [''] } );

if ( !is_dir($checkout_to) ) {
if ( is_dir("$checkout_to/.git") ) {
my $branch = $checkout_opt->{"branch"} || "master";
Rex::Logger::info( "Pulling "
. $repo_info->{"url"} . " to "
. ( $checkout_to ? $checkout_to : "." ) );

my $rebase = $checkout_opt->{"rebase"} ? '--rebase' : '';
my $out = i_run "git pull $rebase origin $branch",
cwd => $checkout_to,
fail_ok => 1,
%run_opt;

unless ( $? == 0 ) {
Rex::Logger::info( "Error pulling.", "warn" );
Rex::Logger::info($out);
die("Error pulling.");
}
else {
Rex::Logger::debug($out);
}

if ( exists $checkout_opt->{"tag"} ) {
my $tag = $checkout_opt->{tag};
my $checkout_cmd = sprintf( $CHECKOUT_TAG_COMMAND, $tag, $tag );
Rex::Logger::info( "Switching to tag " . $tag );
$out = i_run "git fetch origin",
cwd => $checkout_to,
fail_ok => 1,
%run_opt;

unless ( $? == 0 ) {
Rex::Logger::info( "Error switching to tag.", "warn" );
Rex::Logger::info($out);
die("Error switching to tag.");
}
else {
Rex::Logger::debug($out);
}
$out = i_run "$checkout_cmd", cwd => $checkout_to, fail_ok => 1, %run_opt;
unless ( $? == 0 ) {
Rex::Logger::info( "Error switching to tag.", "warn" );
Rex::Logger::info($out);
die("Error switching to tag.");
}
Rex::Logger::debug($out);
}
}
else {
my $clone_cmd = sprintf( $CLONE_COMMAND,
$clone_args, $repo_info->{"url"}, basename($checkout_to) );
Rex::Logger::debug(
Expand Down Expand Up @@ -95,57 +142,6 @@ sub checkout {
Rex::Logger::debug($out);
}
}
elsif ( is_dir("$checkout_to/.git") ) {
my $branch = $checkout_opt->{"branch"} || "master";
Rex::Logger::info( "Pulling "
. $repo_info->{"url"} . " to "
. ( $checkout_to ? $checkout_to : "." ) );

my $rebase = $checkout_opt->{"rebase"} ? '--rebase' : '';
my $out = i_run "git pull $rebase origin $branch",
cwd => $checkout_to,
fail_ok => 1,
%run_opt;

unless ( $? == 0 ) {
Rex::Logger::info( "Error pulling.", "warn" );
Rex::Logger::info($out);
die("Error pulling.");
}
else {
Rex::Logger::debug($out);
}

if ( exists $checkout_opt->{"tag"} ) {
my $tag = $checkout_opt->{tag};
my $checkout_cmd = sprintf( $CHECKOUT_TAG_COMMAND, $tag, $tag );
Rex::Logger::info( "Switching to tag " . $tag );
$out = i_run "git fetch origin",
cwd => $checkout_to,
fail_ok => 1,
%run_opt;

unless ( $? == 0 ) {
Rex::Logger::info( "Error switching to tag.", "warn" );
Rex::Logger::info($out);
die("Error switching to tag.");
}
else {
Rex::Logger::debug($out);
}
$out = i_run "$checkout_cmd", cwd => $checkout_to, fail_ok => 1, %run_opt;
unless ( $? == 0 ) {
Rex::Logger::info( "Error switching to tag.", "warn" );
Rex::Logger::info($out);
die("Error switching to tag.");
}
Rex::Logger::debug($out);
}
}
else {
Rex::Logger::info( "Error checking out repository.", "warn" );
die("Error checking out repository.");
}
}

1;
77 changes: 77 additions & 0 deletions t/scm/git.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env perl

use v5.12.5;
use warnings;

our $VERSION = '9999.99.99_99'; # VERSION

use Test::More tests => 5;
use Test::Exception;

use File::Spec;
use File::Temp qw(tempdir);
use Rex::Commands;
use Rex::Commands::Run;
use Rex::Commands::SCM;
use Rex::Helper::Run;

$::QUIET = 1;

my $git = can_run('git');

if ( !defined $git ) {
plan skip_all => 'Can not find git command';
}

ok( $git, "Found git command at $git" );

my $git_version = i_run "$git version";
ok( $git_version, qq(Git version returned as '$git_version') );

my $test_repo_dir = tempdir( CLEANUP => 1 );
ok( -d $test_repo_dir, "$test_repo_dir is the test repo directory now" );

prepare_test_repo($test_repo_dir);

subtest 'clone into existing directory', sub {
plan tests => 3;

my $test_repo_name = 'test_repo';
my $clone_target_dir = tempdir( CLEANUP => 1 );

set repository => $test_repo_name, url => "file://$test_repo_dir";

ok( -d $clone_target_dir,
"$clone_target_dir is the clone target directory now" );

lives_ok { checkout $test_repo_name, path => $clone_target_dir }
'cloning into to existing directory';

git_repo_ok($clone_target_dir);
};

sub prepare_test_repo {
my $directory = shift;

i_run 'git init', cwd => $directory;

git_repo_ok($directory);

i_run q(git config user.name 'Rex Test Suite'), cwd => $directory;
i_run q(git config user.email 'noreply@rexify.org'), cwd => $directory;

i_run q(git commit --allow-empty -m 'Initial commit'), cwd => $directory;

return;
}

sub git_repo_ok {
my $directory = shift;

ok(
-d File::Spec->join( $directory, '.git' ),
"$directory looks like a git repository now"
);

return;
}

0 comments on commit f82840e

Please sign in to comment.