File Coverage

lib/Rex/SCM/Git.pm
Criterion Covered Total %
statement 39 87 44.8
branch 7 32 21.8
condition 1 5 20.0
subroutine 9 9 100.0
pod 0 2 0.0
total 56 135 41.4


line stmt bran cond sub pod time code
1             package Rex::SCM::Git;
2              
3 2     2   39 use v5.12.5;
  2         14  
4 2     2   24 use warnings;
  2         17  
  2         160  
5              
6             our $VERSION = '1.14.3'; # VERSION
7              
8 2     2   20 use Cwd qw(getcwd);
  2         15  
  2         194  
9 2     2   28 use Rex::Commands::Fs;
  2         12  
  2         60  
10 2     2   28 use File::Basename;
  2         13  
  2         231  
11 2     2   21 use Rex::Helper::Run;
  2         19  
  2         211  
12              
13 2     2   31 use vars qw($CHECKOUT_BRANCH_COMMAND $CHECKOUT_TAG_COMMAND $CLONE_COMMAND);
  2         8  
  2         2003  
14              
15             $CLONE_COMMAND = "git clone %s %s %s";
16             $CHECKOUT_BRANCH_COMMAND = "git checkout -B %s origin/%s";
17             $CHECKOUT_TAG_COMMAND = "git checkout -B %s %s";
18              
19             sub new {
20 2     2 0 11 my $that = shift;
21 2   33     41 my $proto = ref($that) || $that;
22 2         12 my $self = {@_};
23              
24 2         9 bless( $self, $proto );
25              
26 2         6 return $self;
27             }
28              
29             sub checkout {
30 2     2 0 7 my ( $self, $repo_info, $checkout_to, $checkout_opt ) = @_;
31              
32 2         3 my %run_opt;
33 2 50       6 $run_opt{env} = $checkout_opt->{env} if ( $checkout_opt->{env} );
34 2 50       4 my $clone_args = join( " ", @{ $checkout_opt->{clone_args} || [''] } );
  2         26  
35              
36 2 50       35 if ( is_dir("$checkout_to/.git") ) {
37 0   0     0 my $branch = $checkout_opt->{"branch"} || "master";
38             Rex::Logger::info( "Pulling "
39 0 0       0 . $repo_info->{"url"} . " to "
40             . ( $checkout_to ? $checkout_to : "." ) );
41              
42 0 0       0 my $rebase = $checkout_opt->{"rebase"} ? '--rebase' : '';
43 0         0 my $out = i_run "git pull $rebase origin $branch",
44             cwd => $checkout_to,
45             fail_ok => 1,
46             %run_opt;
47              
48 0 0       0 unless ( $? == 0 ) {
49 0         0 Rex::Logger::info( "Error pulling.", "warn" );
50 0         0 Rex::Logger::info($out);
51 0         0 die("Error pulling.");
52             }
53             else {
54 0         0 Rex::Logger::debug($out);
55             }
56              
57 0 0       0 if ( exists $checkout_opt->{"tag"} ) {
58 0         0 my $tag = $checkout_opt->{tag};
59 0         0 my $checkout_cmd = sprintf( $CHECKOUT_TAG_COMMAND, $tag, $tag );
60 0         0 Rex::Logger::info( "Switching to tag " . $tag );
61 0         0 $out = i_run "git fetch origin",
62             cwd => $checkout_to,
63             fail_ok => 1,
64             %run_opt;
65              
66 0 0       0 unless ( $? == 0 ) {
67 0         0 Rex::Logger::info( "Error switching to tag.", "warn" );
68 0         0 Rex::Logger::info($out);
69 0         0 die("Error switching to tag.");
70             }
71             else {
72 0         0 Rex::Logger::debug($out);
73             }
74 0         0 $out = i_run "$checkout_cmd", cwd => $checkout_to, fail_ok => 1, %run_opt;
75 0 0       0 unless ( $? == 0 ) {
76 0         0 Rex::Logger::info( "Error switching to tag.", "warn" );
77 0         0 Rex::Logger::info($out);
78 0         0 die("Error switching to tag.");
79             }
80 0         0 Rex::Logger::debug($out);
81             }
82             }
83             else {
84             my $clone_cmd = sprintf( $CLONE_COMMAND,
85 2         212 $clone_args, $repo_info->{"url"}, basename($checkout_to) );
86 2         85 Rex::Logger::debug(
87             "clone_cmd: $clone_cmd (cwd: " . dirname($checkout_to) . ")" );
88              
89             Rex::Logger::info( "Cloning "
90 2 50       33 . $repo_info->{"url"} . " to "
91             . ( $checkout_to ? $checkout_to : "." ) );
92 2         146 my $out = i_run "$clone_cmd",
93             cwd => dirname($checkout_to),
94             fail_ok => 1,
95             %run_opt;
96 2 50       86 unless ( $? == 0 ) {
97 0         0 Rex::Logger::info( "Error cloning repository.", "warn" );
98 0         0 Rex::Logger::info($out);
99 0         0 die("Error cloning repository.");
100             }
101              
102 2         45 Rex::Logger::debug($out);
103              
104 2 50       52 if ( exists $checkout_opt->{"branch"} ) {
105 0 0       0 unless ($checkout_to) {
106 0         0 $checkout_to = [ split( /\//, $repo_info->{"url"} ) ]->[-1];
107 0         0 $checkout_to =~ s/\.git$//;
108             }
109              
110             my $checkout_cmd = sprintf(
111             $CHECKOUT_BRANCH_COMMAND,
112             $checkout_opt->{"branch"},
113 0         0 $checkout_opt->{"branch"}
114             );
115 0         0 Rex::Logger::debug("checkout_cmd: $checkout_cmd");
116              
117 0         0 Rex::Logger::info( "Switching to branch " . $checkout_opt->{"branch"} );
118              
119 0         0 $out = i_run "$checkout_cmd", cwd => $checkout_to, fail_ok => 1, %run_opt;
120 0 0       0 unless ( $? == 0 ) {
121 0         0 Rex::Logger::info( "Error switching to branch.", "warn" );
122 0         0 Rex::Logger::info($out);
123 0         0 die("Error switching to branch.");
124             }
125 0         0 Rex::Logger::debug($out);
126             }
127              
128 2 50       333 if ( exists $checkout_opt->{"tag"} ) {
129             my $checkout_cmd = sprintf(
130             $CHECKOUT_TAG_COMMAND,
131             $checkout_opt->{"tag"},
132 0           $checkout_opt->{"tag"}
133             );
134              
135 0           Rex::Logger::info( "Switching to tag " . $checkout_opt->{"tag"} );
136 0           $out = i_run "$checkout_cmd", cwd => $checkout_to, fail_ok => 1, %run_opt;
137 0 0         unless ( $? == 0 ) {
138 0           Rex::Logger::info( "Error switching to tag.", "warn" );
139 0           Rex::Logger::info($out);
140 0           die("Error switching to tag.");
141             }
142 0           Rex::Logger::debug($out);
143             }
144             }
145             }
146              
147             1;