blib/lib/Git/Repository/Command.pm

filter: Statementstmt 100% 102 / 102 Branchbran 91.9% 57 / 62 Conditioncond 82.1% 23 / 28 MC/DCmcdc 61.1% 11 / 18 Subroutinesub 100% 19 / 19 Podpod 100% 2 / 2 totaltotal 92.6% 214 / 231 CC 46 SCAR 24.2
CC
46
Cov
95%
new
19.0
_is_git
15.3
final_output
8.0
CRAP 11.3
« blib/lib/Test/Git.pm ↑ Summary blib/lib/Git/Repository.pm »
1 package Git::Repository::Command;
2 19 $Git::Repository::Command::VERSION = '1.326';
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 19 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use Cwd qw( cwd );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 19 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use System::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 19 our @ISA = qw( System::Command );
15  
16 # a few simple accessors
17 19 for my $attr (qw( pid stdin stdout stderr exit signal core )) {
18 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2856 ▶ *$attr = sub { return $_[0]{$attr} };
Subroutine __ANON__: called CC=1 SCAR=0.0
20 }
21 19 for my $attr (qw( cmdline )) {
22 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶ *$attr = sub { return @{ $_[0]{$attr} } };
Subroutine __ANON__: called CC=1 SCAR=0.0
24 }
25  
26 28 ▶sub _which {
Subroutine _which: called CC=7 SCAR=19.6
27 28 my @pathext = ('');
28 push @pathext,
29 $^O eq 'MSWin32' ? split ';', $ENV{PATHEXT}
30 28 ▶ : $^O eq 'cygwin' ? qw( .com .exe .bat )
Branch: 2 branches
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 28 28
$^O eq 'cygwin' ? : 0 28 28
31 : ();
32  
33 28 for my $path ( File::Spec->path ) {
34 175 for my $ext (@pathext) {
35 175 my $binary = File::Spec->catfile( $path, $_[0] . $ext );
36 175 ▶ return $binary if -x $binary && !-d _;
Branch: 1 branch
BranchTrueFalseTotal
if -x $binary and not -d _ 18 157 175
Condition100%3/3
-x $binary and not -d _
!ll&&!rl&&r
155218
MC/DC100%2/2
-x $binary and not -d _
-x $binary not -d _
Truth table100%3/3
A and not(B)
A= -x $binaryB= not -d _
ABresult
0X0
100
111
37 }
38 }
39  
40 # not found
41 10 return undef;
42 }
43  
44 # CAN I HAS GIT?
45 19 my %binary; # cache calls to _is_git
46 373 ▶sub _is_git {
Subroutine _is_git: called CC=15 SCAR=27.3
47 373 my ( $binary, @args ) = @_;
48 373 my $args = join "\0", @args;
49  
50 # git option might be an arrayref containing an executable with arguments
51 # Best that can be done is to check if the first part is executable
52 # and use the arguments as part of the cache key
53  
54 # compute cache key:
55 # - filename (path-rel): $CWD \0 $PATH
56 # - filename (path): $PATH
57 # - absolute path (abs): empty string
58 # - relative path (rel): dirname
59 373 ▶ my $path = defined $ENV{PATH} && length( $ENV{PATH} ) ? $ENV{PATH} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'PATH'} && length $ENV{'PATH'} ? : 371 2 373
Condition66%2/3
defined $ENV{"PATH"} and length $ENV{"PATH"}
!ll&&!rl&&r
20371
MC/DC50%1/2
defined $ENV{"PATH"} and length $ENV{"PATH"}
defined $ENV{"PATH"} length $ENV{"PATH"}
Truth table66%2/3
A and B
A= defined $ENV{"PATH"}B= length $ENV{"PATH"}
ABresult
0X0
100
111
60 373 ▶ my ( $type, $key ) =
Branch: 3 branches
BranchTrueFalseTotal
('File::Spec'->splitpath($binary))[2] eq $binary ? : 307 66 373
grep((!'File::Spec'->file_name_is_absolute($_)), 'File::Spec'->path) ? : 0 307 307
'File::Spec'->file_name_is_absolute($binary) ? : 63 3 66
61 ( File::Spec->splitpath($binary) )[2] eq $binary
62 ? grep( !File::Spec->file_name_is_absolute($_), File::Spec->path )
63 ? ( 'path-rel', join "\0", cwd(), $path )
64 : ( 'path', $path )
65 : File::Spec->file_name_is_absolute($binary) ? ( 'abs', '' )
66 : ( 'rel', cwd() );
67  
68 # This relatively complex cache key scheme allows PATH or cwd to change
69 # during the life of a program using Git::Repository, which is likely
70 # to happen. On the other hand, it completely ignores the possibility
71 # that any part of the cached path to a git binary could be a symlink
72 # which target may also change during the life of the program.
73  
74 # check the cache
75 return $binary{$type}{$key}{$binary}{$args}
76 373 ▶ if exists $binary{$type}{$key}{$binary}{$args};
Branch: 1 branch
BranchTrueFalseTotal
if exists $binary{$type}{$key}{$binary}{$args} 334 39 373
77  
78 # compute a list of candidate files (look in PATH if needed)
79 39 ▶ my $git = $type =~ /^path/
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /^path/ ? : 26 13 39
80 ? _which($binary)
81 : File::Spec->rel2abs($binary);
82 39 ▶ $git = File::Spec->rel2abs($git)
Branch: 1 branch
BranchTrueFalseTotal
if defined $git and $type eq "path-rel" 0 39 39
Condition66%2/3
defined $git and $type eq "path-rel"
!ll&&!rl&&r
10290
MC/DC0%0/2
defined $git and $type eq "path-rel"
defined $git $type eq "path-rel"
Truth table66%2/3
A and B
A= defined $gitB= $type eq "path-rel"
ABresult
0X0
100
111
83 if defined $git && $type eq 'path-rel';
84  
85 # if we can't find any, we're done
86 39 ▶ return $binary{$type}{$key}{$binary} = undef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $git and -x $git 14 25 39
Condition100%3/3
defined $git and -x $git
!ll&&!rl&&r
10425
MC/DC100%2/2
defined $git and -x $git
defined $git -x $git
Truth table100%3/3
A and B
A= defined $gitB= -x $git
ABresult
0X0
100
111
87 if !( defined $git && -x $git );
88  
89 # try to run it
90 25 my $cmd = System::Command->new( $git, @args, '--version' );
91 25 ▶ my $version = do { local $/ = "\n"; $cmd->stdout->getline; } || '';
Condition50%1/2
do { local $/ = "\n"; $cmd->stdout->getline } || ''
l!l
250
MC/DC0%0/1
do { local $/ = "\n"; $cmd->stdout->getline } || ''
do { local $/ = "\n"; $cmd->stdout->getline }
Truth table50%1/2
A
A= do { local $/ = "\n"; $cmd->stdout->getline }
Aresult
11
00
92 25 $cmd->close;
93  
94 # does it really look like git?
95 25 ▶ return $binary{$type}{$key}{$binary}{$args}
Branch: 2 branches
BranchTrueFalseTotal
$version =~ /^git version \d/ ? : 23 2 25
$type eq 'path' ? : 16 7 23
96 = $version =~ /^git version \d/
97 ? $type eq 'path'
98 ? $binary # leave the shell figure it out itself too
99 : $git
100 : undef;
101 }
102  
103 355 ▶sub new {
Subroutine new: called CC=19 SCAR=29.5
Pod documented
104 355 my ( $class, @cmd ) = @_;
105  
106 # split the args
107 355 my (@r, @o);
108 355 ▶ @cmd = # take out the first Git::Repository in $r, and options in @o
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($_) && $_->isa('Git::Repository') ? : 143 644 787
ref $_ eq 'HASH' ? : 174 787 961
Condition100%3/3
builtin::blessed($_) and $_->isa("Git::Repository")
!ll&&!rl&&r
6422143
MC/DC100%2/2
builtin::blessed($_) and $_->isa("Git::Repository")
builtin::blessed($_) $_->isa("Git::Repository")
Truth table100%3/3
A and B
A= builtin::blessed($_)B= $_->isa("Git::Repository")
ABresult
0X0
100
111
109 grep !( blessed $_ && $_->isa('Git::Repository') ? push @r, $_ : 0 ),
110 grep !( ref eq 'HASH' ? push @o, $_ : 0 ),
111 @cmd;
112  
113 # wouldn't know what to do with more than one Git::Repository object
114 355 ▶ croak "Too many Git::Repository objects given: @r" if @r > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @r > 1 1 354 355
115 354 my $r = shift @r;
116  
117 # keep changes to the environment local
118 354 local %ENV = %ENV;
119  
120 # a Git::Repository object will give more context
121 354 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 141 213 354
122  
123 # pick up repository options
124 141 unshift @o, $r->options;
125  
126 # get some useful paths
127 141 my ( $git_dir, $work_tree ) = ( $r->git_dir, $r->work_tree );
128 141 ▶ unshift @o, { cwd => $work_tree }
Branch: 1 branch
BranchTrueFalseTotal
if defined $work_tree and length $work_tree 106 35 141
Condition66%2/3
defined $work_tree and length $work_tree
!ll&&!rl&&r
350106
MC/DC50%1/2
defined $work_tree and length $work_tree
defined $work_tree length $work_tree
Truth table66%2/3
A and B
A= defined $work_treeB= length $work_tree
ABresult
0X0
100
111
129 if defined $work_tree && length $work_tree;
130  
131 # setup our %ENV
132 141 delete @ENV{qw( GIT_DIR GIT_WORK_TREE )};
133 141 $ENV{GIT_DIR} = $git_dir;
134 141 ▶ $ENV{GIT_WORK_TREE} = $work_tree
Branch: 1 branch
BranchTrueFalseTotal
if defined $work_tree 106 35 141
135 if defined $work_tree;
136 }
137  
138 # pick up the modified PATH, if any
139 exists $_->{env} and exists $_->{env}{PATH} and $ENV{PATH} = $_->{env}{PATH}
140 354 ▶ for @o;
Branch: 1 branch
BranchTrueFalseTotal
exists $_->{"env"} and exists $_->{"env"}{"PATH"} and $ENV{"PATH"} = $_->{"env"}{"PATH"} 0 421 421
Condition66%2/3
exists $_->{"env"} and exists $_->{"env"}{"PATH"}
!ll&&!rl&&r
396250
MC/DC0%0/2
exists $_->{"env"} and exists $_->{"env"}{"PATH"}
exists $_->{"env"} exists $_->{"env"}{"PATH"}
Truth table66%2/3
A and B
A= exists $_->{"env"}B= exists $_->{"env"}{"PATH"}
ABresult
0X0
100
111
141  
142 # extract and process the 'fatal' option
143 push @o, {
144 fatal => {
145 128 => 1, # fatal
146 129 => 1, # usage
147 map s/^-// ? ( $_ => '' ) : ( $_ => 1 ),
148 map /^!0$/ ? ( 1 .. 255 ) : $_,
149 map ref() ? @$_ : $_, grep defined, map $_->{fatal}, @o
150 354 ▶ }
Branch: 3 branches
BranchTrueFalseTotal
s/^-// ? : 35 2044 2079
/^!0$/ ? : 3 1314 1317
ref $_ ? : 34 7 41
151 };
152  
153 # get and check the git command
154 354 ▶ my $git_cmd = ( map { exists $_->{git} ? $_->{git} : () } @o )[-1];
Branch: 1 branch
BranchTrueFalseTotal
exists $_->{'git'} ? : 64 711 775
155  
156 # git option might be an arrayref containing an executable with arguments
157 # (e.g. [ qw( /usr/bin/sudo -u nobody git ) ] )
158 354 ▶ ( $git_cmd, my @args )
Branch: 2 branches
BranchTrueFalseTotal
defined $git_cmd ? : 64 290 354
ref $git_cmd ? : 2 62 64
159 = defined $git_cmd ? ref $git_cmd ? @$git_cmd : ($git_cmd) : ('git');
160 354 my $git = _is_git($git_cmd, @args);
161  
162 354 ▶ croak sprintf "git binary '%s' not available or broken",
Branch: 1 branch
BranchTrueFalseTotal
unless defined $git 7 347 354
163 join( ' ', $git_cmd, @args ) # show the full command given
164 if !defined $git;
165  
166 # turn us into a dumb terminal
167 347 delete $ENV{TERM};
168  
169 # spawn the command and re-bless the object in our class
170 347 return bless System::Command->new( $git, @args, @cmd, @o ), $class;
171 }
172  
173 342 ▶sub final_output {
Subroutine final_output: called CC=8 SCAR=20.8
Pod documented
174 342 my ($self, @cb) = @_;
175  
176 # get output / errput
177 my $input_record_separator =
178 exists $self->options->{input_record_separator}
179 ? $self->options->{input_record_separator}
180 342 ▶ : "\n";
Branch: 1 branch
BranchTrueFalseTotal
exists $self->options->{'input_record_separator'} ? : 1 341 342
181 342 my ( @output, @errput );
182 $self->loop_on(
183 input_record_separator => $input_record_separator,
184 298 ▶ stdout => sub { chomp( my $o = shift ); push @output, $o; },
Subroutine __ANON__: called CC=1 SCAR=0.0
185 153 ▶ stderr => sub { chomp( my $e = shift ); push @errput, $e; },
Subroutine __ANON__: called CC=1 SCAR=0.0
186 342 );
187  
188 # done with it
189 342 $self->close;
190  
191 # fatal exit codes set by the 'fatal' option
192 # when working with fatal => '!0' it's helpful to be able to show the exit status
193 # so that specific exit codes can be made non-fatal if desired.
194 342 ▶ if ( $self->options->{fatal}{ $self->exit } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->options->{"fatal"}{$self->exit}) 17 325 342
195 17 ▶ croak join( "\n", @errput ) || 'fatal: unknown git error, exit status '.$self->exit;
Condition100%2/2
join("\n", @errput) || 'fatal: unknown git error, exit status ' . $self->exit
l!l
125
MC/DC100%1/1
join("\n", @errput) || 'fatal: unknown git error, exit status ' . $self->exit
join("\n", @errput)
Truth table100%2/2
A
A= join("\n", @errput)
Aresult
11
00
196 }
197  
198 # something else's wrong
199 325 ▶ if ( @errput && !$self->options->{quiet} ) { carp join "\n", @errput; }
Branch: 1 branch
BranchTrueFalseTotal
if (@errput and not $self->options->{"quiet"}) 7 318 325
Condition100%3/3
@errput and not $self->options->{"quiet"}
!ll&&!rl&&r
31087
MC/DC100%2/2
@errput and not $self->options->{"quiet"}
@errput not $self->options->{"quiet"}
Truth table100%3/3
A and not(B)
A= @errputB= not $self->options->{"quiet"}
ABresult
0X0
100
111
200  
201 # process the output with the optional callbacks
202 325 for my $cb (@cb) {
203 3 @output = map $cb->($_), @output;
204 }
205  
206 # return the output
207 325 ▶ return wantarray ? @output : join "\n", @output;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 69 256 325
208 }
209  
210 19 1;
211  
212 __END__
« blib/lib/Test/Git.pm ↑ Summary blib/lib/Git/Repository.pm »