lib/Rex/SCM/Git.pm

filter: Statementstmt 64.2% 61 / 95 Branchbran 50.0% 15 / 30 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 59.7% 89 / 149 CC 19 SCAR 37.2
CC
19
Cov
60%
checkout
61.0
new
2.1
BEGIN
1.0
CRAP 41.1
« lib/Rex/Commands/Box.pm ↑ Summary lib/Rex/Cloud/OpenStack.pm »
1 package Rex::SCM::Git;
2  
3 2 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '1.16.1'; # VERSION
7  
8 2 ▶use Cwd qw(getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use vars qw($CHECKOUT_BRANCH_COMMAND $CHECKOUT_TAG_COMMAND $CLONE_COMMAND);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 $CLONE_COMMAND = "git clone %s %s %s";
16 2 $CHECKOUT_BRANCH_COMMAND = "git checkout -B %s origin/%s";
17 2 $CHECKOUT_TAG_COMMAND = "git checkout -B %s %s";
18  
19 13 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
20 13 my $that = shift;
21 13 ▶ my $proto = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
0130
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
22 13 my $self = {@_};
23  
24 13 bless( $self, $proto );
25  
26 13 return $self;
27 }
28  
29 13 ▶sub checkout {
Subroutine checkout: called CC=18 SCAR=41.1
Pod undocumented
30 13 my ( $self, $repo_info, $checkout_to, $checkout_opt ) = @_;
31  
32 13 my %run_opt;
33 13 ▶ $run_opt{env} = $checkout_opt->{env} if ( $checkout_opt->{env} );
Branch: 1 branch
BranchTrueFalseTotal
if $checkout_opt->{"env"} 0 13 13
34 13 ▶ my $clone_args = join( " ", @{ $checkout_opt->{clone_args} || [''] } );
Condition50%1/2
$checkout_opt->{'clone_args'} || ['']
l!l
013
MC/DC0%0/1
$checkout_opt->{'clone_args'} || ['']
$checkout_opt->{'clone_args'}
Truth table50%1/2
A
A= $checkout_opt->{'clone_args'}
Aresult
11
00
35  
36 13 ▶ if ( is_dir("$checkout_to/.git") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_dir("$checkout_to/.git")) { } 5 8 13
37 5 my $head_ref = i_run 'git symbolic-ref refs/remotes/origin/HEAD',
38 cwd => $checkout_to,
39 %run_opt;
40  
41 5 ( my $default_origin_branch = $head_ref ) =~ s{refs/remotes/origin/}{}msx;
42  
43 5 ▶ my $branch = $checkout_opt->{"branch"} || $default_origin_branch;
Condition66%2/3
$checkout_opt->{'branch'} || $default_origin_branch
l!l&&r!l&&!r
320
MC/DC0%0/2
$checkout_opt->{'branch'} || $default_origin_branch
$checkout_opt->{'branch'} $default_origin_branch
Truth table66%2/3
A || B
A= $checkout_opt->{'branch'}B= $default_origin_branch
ABresult
1X1
011
000
44  
45 5 i_run "git checkout -B $branch", cwd => $checkout_to, %run_opt;
46  
47 Rex::Logger::info( "Pulling "
48 5 ▶ . $repo_info->{"url"} . " to "
Branch: 1 branch
BranchTrueFalseTotal
$checkout_to ? : 5 0 5
49 . ( $checkout_to ? $checkout_to : "." ) );
50  
51 5 ▶ my $rebase = $checkout_opt->{"rebase"} ? '--rebase' : '';
Branch: 1 branch
BranchTrueFalseTotal
$checkout_opt->{'rebase'} ? : 2 3 5
52 5 my $out = i_run "git pull $rebase origin $branch",
53 cwd => $checkout_to,
54 fail_ok => 1,
55 %run_opt;
56  
57 5 ▶ unless ( $? == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) { } 5 0 5
58 0 Rex::Logger::info( "Error pulling.", "warn" );
59 0 Rex::Logger::info($out);
60 0 die("Error pulling.");
61 }
62 else {
63 5 Rex::Logger::debug($out);
64 }
65  
66 5 ▶ if ( exists $checkout_opt->{"tag"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $checkout_opt->{"tag"}) 0 5 5
67 0 my $tag = $checkout_opt->{tag};
68 0 my $checkout_cmd = sprintf( $CHECKOUT_TAG_COMMAND, $tag, $tag );
69 0 Rex::Logger::info( "Switching to tag " . $tag );
70 0 $out = i_run "git fetch origin",
71 cwd => $checkout_to,
72 fail_ok => 1,
73 %run_opt;
74  
75 0 ▶ unless ( $? == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) { } 0 0 0
76 0 Rex::Logger::info( "Error switching to tag.", "warn" );
77 0 Rex::Logger::info($out);
78 0 die("Error switching to tag.");
79 }
80 else {
81 0 Rex::Logger::debug($out);
82 }
83 0 $out = i_run "$checkout_cmd", cwd => $checkout_to, fail_ok => 1, %run_opt;
84 0 ▶ unless ( $? == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? == 0) 0 0 0
85 0 Rex::Logger::info( "Error switching to tag.", "warn" );
86 0 Rex::Logger::info($out);
87 0 die("Error switching to tag.");
88 }
89 0 Rex::Logger::debug($out);
90 }
91 }
92 else {
93 my $clone_cmd = sprintf( $CLONE_COMMAND,
94 8 $clone_args, $repo_info->{"url"}, basename($checkout_to) );
95 8 Rex::Logger::debug(
96 "clone_cmd: $clone_cmd (cwd: " . dirname($checkout_to) . ")" );
97  
98 Rex::Logger::info( "Cloning "
99 8 ▶ . $repo_info->{"url"} . " to "
Branch: 1 branch
BranchTrueFalseTotal
$checkout_to ? : 8 0 8
100 . ( $checkout_to ? $checkout_to : "." ) );
101 8 my $out = i_run "$clone_cmd",
102 cwd => dirname($checkout_to),
103 fail_ok => 1,
104 %run_opt;
105 8 ▶ unless ( $? == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? == 0) 0 8 8
106 0 Rex::Logger::info( "Error cloning repository.", "warn" );
107 0 Rex::Logger::info($out);
108 0 die("Error cloning repository.");
109 }
110  
111 8 Rex::Logger::debug($out);
112  
113 8 ▶ if ( exists $checkout_opt->{"branch"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $checkout_opt->{"branch"}) 1 7 8
114 1 ▶ unless ($checkout_to) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($checkout_to) 0 1 1
115 0 $checkout_to = [ split( /\//, $repo_info->{"url"} ) ]->[-1];
116 0 $checkout_to =~ s/\.git$//;
117 }
118  
119 my $checkout_cmd = sprintf(
120 $CHECKOUT_BRANCH_COMMAND,
121 $checkout_opt->{"branch"},
122 1 $checkout_opt->{"branch"}
123 );
124 1 Rex::Logger::debug("checkout_cmd: $checkout_cmd");
125  
126 1 Rex::Logger::info( "Switching to branch " . $checkout_opt->{"branch"} );
127  
128 1 $out = i_run "$checkout_cmd", cwd => $checkout_to, fail_ok => 1, %run_opt;
129 1 ▶ unless ( $? == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? == 0) 0 1 1
130 0 Rex::Logger::info( "Error switching to branch.", "warn" );
131 0 Rex::Logger::info($out);
132 0 die("Error switching to branch.");
133 }
134 1 Rex::Logger::debug($out);
135 }
136  
137 8 ▶ if ( exists $checkout_opt->{"tag"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $checkout_opt->{"tag"}) 0 8 8
138 my $checkout_cmd = sprintf(
139 $CHECKOUT_TAG_COMMAND,
140 $checkout_opt->{"tag"},
141 0 $checkout_opt->{"tag"}
142 );
143  
144 0 Rex::Logger::info( "Switching to tag " . $checkout_opt->{"tag"} );
145 0 $out = i_run "$checkout_cmd", cwd => $checkout_to, fail_ok => 1, %run_opt;
146 0 ▶ unless ( $? == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? == 0) 0 0 0
147 0 Rex::Logger::info( "Error switching to tag.", "warn" );
148 0 Rex::Logger::info($out);
149 0 die("Error switching to tag.");
150 }
151 0 Rex::Logger::debug($out);
152 }
153 }
154 }
155  
156 2 1;
« lib/Rex/Commands/Box.pm ↑ Summary lib/Rex/Cloud/OpenStack.pm »