blib/lib/Git/Repository/Command.pm

filter: Statementstmt 100% 101 / 101 Branchbran 95.0% 57 / 60 Conditioncond 85.7% 24 / 28 MC/DCmcdc 72.2% 13 / 18 Subroutinesub 100% 19 / 19 Podpod 100% 2 / 2 totaltotal 94.7% 216 / 228 CC 45 SCAR 24.1
CC
45
Cov
96%
new
19.0
_is_git
15.0
_which
7.1
CRAP 11.1
« blib/lib/Test/Git.pm ↑ Summary blib/lib/Git/Repository.pm »
1 package Git::Repository::Command;
2 19 $Git::Repository::Command::VERSION = '1.324';
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 2945 ▶ *$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 sub _which {
27 30 ▶ my @pathext = ('');
Subroutine _which: called CC=7 SCAR=19.6
28 push @pathext,
29 $^O eq 'MSWin32' ? split ';', $ENV{PATHEXT}
30 30 ▶ : $^O eq 'cygwin' ? qw( .com .exe .bat )
Branch: 2 branches
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 30 30
$^O eq 'cygwin' ? : 0 30 30
31 : ();
32  
33 30 for my $path ( File::Spec->path ) {
34 158 for my $ext (@pathext) {
35 158 my $binary = File::Spec->catfile( $path, $_[0] . $ext );
36 158 ▶ return $binary if -x $binary && !-d _;
Branch: 1 branch
BranchTrueFalseTotal
if -x $binary and not -d _ 20 138 158
Condition100%3/3
-x $binary and not -d _
!ll&&!rl&&r
136220
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 sub _is_git {
47 358 ▶ my ( $binary, @args ) = @_;
Subroutine _is_git: called CC=15 SCAR=27.1
48 358 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 358 ▶ my $path = defined $ENV{PATH} && length( $ENV{PATH} ) ? $ENV{PATH} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'PATH'} && length $ENV{'PATH'} ? : 356 2 358
Condition66%2/3
defined $ENV{"PATH"} and length $ENV{"PATH"}
!ll&&!rl&&r
20356
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 358 ▶ my ( $type, $key ) =
Branch: 3 branches
BranchTrueFalseTotal
('File::Spec'->splitpath($binary))[2] eq $binary ? : 292 66 358
grep((!'File::Spec'->file_name_is_absolute($_)), 'File::Spec'->path) ? : 4 288 292
'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 358 ▶ if exists $binary{$type}{$key}{$binary}{$args};
Branch: 1 branch
BranchTrueFalseTotal
if exists $binary{$type}{$key}{$binary}{$args} 317 41 358
77  
78 # compute a list of candidate files (look in PATH if needed)
79 41 ▶ my $git = $type =~ /^path/
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /^path/ ? : 28 13 41
80 ? _which($binary)
81 : File::Spec->rel2abs($binary);
82 41 ▶ $git = File::Spec->rel2abs($git)
Branch: 1 branch
BranchTrueFalseTotal
if defined $git and $type eq "path-rel" 2 39 41
Condition100%3/3
defined $git and $type eq "path-rel"
!ll&&!rl&&r
10292
MC/DC100%2/2
defined $git and $type eq "path-rel"
defined $git $type eq "path-rel"
Truth table100%3/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 41 ▶ return $binary{$type}{$key}{$binary} = undef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $git and -x $git 14 27 41
Condition100%3/3
defined $git and -x $git
!ll&&!rl&&r
10427
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 27 my $cmd = System::Command->new( $git, @args, '--version' );
91 27 ▶ my $version = do { local $/ = "\n"; $cmd->stdout->getline; } || '';
Condition50%1/2
do { local $/ = "\n"; $cmd->stdout->getline } || ''
l!l
270
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 27 $cmd->close;
93  
94 # does it really look like git?
95 27 ▶ return $binary{$type}{$key}{$binary}{$args}
Branch: 2 branches
BranchTrueFalseTotal
$version =~ /^git version \d/ ? : 25 2 27
$type eq 'path' ? : 16 9 25
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 sub new {
104 340 ▶ my ( $class, @cmd ) = @_;
Subroutine new: called CC=19 SCAR=29.5
Pod documented
105  
106 # split the args
107 340 my (@r, @o);
108 340 ▶ @cmd = # take out the first Git::Repository in $r, and options in @o
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($_) && $_->isa('Git::Repository') ? : 139 602 741
ref $_ eq 'HASH' ? : 173 741 914
Condition100%3/3
builtin::blessed($_) and $_->isa("Git::Repository")
!ll&&!rl&&r
6002139
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 340 ▶ croak "Too many Git::Repository objects given: @r" if @r > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @r > 1 1 339 340
115 339 my $r = shift @r;
116  
117 # keep changes to the environment local
118 339 local %ENV = %ENV;
119  
120 # a Git::Repository object will give more context
121 339 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 137 202 339
122  
123 # pick up repository options
124 137 unshift @o, $r->options;
125  
126 # get some useful paths
127 137 my ( $git_dir, $work_tree ) = ( $r->git_dir, $r->work_tree );
128 137 ▶ unshift @o, { cwd => $work_tree }
Branch: 1 branch
BranchTrueFalseTotal
if defined $work_tree and length $work_tree 102 35 137
Condition66%2/3
defined $work_tree and length $work_tree
!ll&&!rl&&r
350102
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 137 delete @ENV{qw( GIT_DIR GIT_WORK_TREE )};
133 137 $ENV{GIT_DIR} = $git_dir;
134 137 ▶ $ENV{GIT_WORK_TREE} = $work_tree
Branch: 1 branch
BranchTrueFalseTotal
if defined $work_tree 102 35 137
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 339 ▶ for @o;
Branch: 1 branch
BranchTrueFalseTotal
exists $_->{"env"} and exists $_->{"env"}{"PATH"} and $ENV{"PATH"} = $_->{"env"}{"PATH"} 0 412 412
Condition66%2/3
exists $_->{"env"} and exists $_->{"env"}{"PATH"}
!ll&&!rl&&r
387250
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 339 ▶ }
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 339 ▶ my $git_cmd = ( map { exists $_->{git} ? $_->{git} : () } @o )[-1];
Branch: 1 branch
BranchTrueFalseTotal
exists $_->{'git'} ? : 64 687 751
155  
156 # git option might be an arrayref containing an executable with arguments
157 # (e.g. [ qw( /usr/bin/sudo -u nobody git ) ] )
158 339 ▶ ( $git_cmd, my @args )
Branch: 2 branches
BranchTrueFalseTotal
defined $git_cmd ? : 64 275 339
ref $git_cmd ? : 2 62 64
159 = defined $git_cmd ? ref $git_cmd ? @$git_cmd : ($git_cmd) : ('git');
160 339 my $git = _is_git($git_cmd, @args);
161  
162 339 ▶ croak sprintf "git binary '%s' not available or broken",
Branch: 1 branch
BranchTrueFalseTotal
unless defined $git 7 332 339
163 join( ' ', $git_cmd, @args ) # show the full command given
164 if !defined $git;
165  
166 # turn us into a dumb terminal
167 332 delete $ENV{TERM};
168  
169 # spawn the command and re-bless the object in our class
170 332 return bless System::Command->new( $git, @args, @cmd, @o ), $class;
171 }
172  
173 sub final_output {
174 327 ▶ my ($self, @cb) = @_;
Subroutine final_output: called CC=7 SCAR=19.5
Pod documented
175  
176 # get output / errput
177 327 my ( @output, @errput );
178 $self->loop_on(
179 input_record_separator => "\n",
180 301 ▶ stdout => sub { chomp( my $o = shift ); push @output, $o; },
Subroutine __ANON__: called CC=1 SCAR=0.0
181 344 ▶ stderr => sub { chomp( my $e = shift ); push @errput, $e; },
Subroutine __ANON__: called CC=1 SCAR=0.0
182 327 );
183  
184 # done with it
185 327 $self->close;
186  
187 # fatal exit codes set by the 'fatal' option
188 # when working with fatal => '!0' it's helpful to be able to show the exit status
189 # so that specific exit codes can be made non-fatal if desired.
190 327 ▶ if ( $self->options->{fatal}{ $self->exit } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->options->{"fatal"}{$self->exit}) 18 309 327
191 18 ▶ 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
135
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
192 }
193  
194 # something else's wrong
195 309 ▶ if ( @errput && !$self->options->{quiet} ) { carp join "\n", @errput; }
Branch: 1 branch
BranchTrueFalseTotal
if (@errput and not $self->options->{"quiet"}) 25 284 309
Condition100%3/3
@errput and not $self->options->{"quiet"}
!ll&&!rl&&r
276825
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
196  
197 # process the output with the optional callbacks
198 309 for my $cb (@cb) {
199 3 @output = map $cb->($_), @output;
200 }
201  
202 # return the output
203 309 ▶ return wantarray ? @output : join "\n", @output;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 68 241 309
204 }
205  
206 19 1;
207  
208 __END__
« blib/lib/Test/Git.pm ↑ Summary blib/lib/Git/Repository.pm »