blib/lib/Git/Repository.pm

filter: Statementstmt 97.7% 132 / 135 Branchbran 100% 64 / 64 Conditioncond 75.0% 24 / 32 MC/DCmcdc 44.4% 8 / 18 Subroutinesub 93.7% 30 / 32 Podpod 57.1% 4 / 7 totaltotal 90.9% 262 / 288 CC 45 SCAR 22.1
CC
45
Cov
95%
new
24.0
import
5.0
__ANON__
3.0
CRAP 9.1
« blib/lib/Git/Repository/Command.pm ↑ Summary blib/lib/Git/Repository/Plugin.pm »
1 package Git::Repository;
2 18 $Git::Repository::VERSION = '1.326';
3 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use Cwd qw( cwd realpath );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 18 ▶use Git::Repository::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 ▶use Git::Version::Compare qw( :ops );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # helper function
15 127 ▶sub _abs_path {
Subroutine _abs_path: called CC=1 SCAR=0.0
16 127 my ( $path, $base ) = @_;
17 127 my $abs_path = File::Spec->rel2abs( $path, $base );
18  
19 # normalize, but don't die on Win32 if the path doesn't exist
20 127 eval { $abs_path = realpath($abs_path); };
21 126 return $abs_path;
22 }
23  
24 18 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # a few simple accessors
27 18 for my $attr (qw( git_dir work_tree options )) {
28 18 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 508 ▶ *$attr = sub { return ref $_[0] ? $_[0]{$attr} : () };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 507 1 508
30 }
31  
32 # backward compatible aliases
33 0 ▶sub repo_path {
Subroutine repo_path: not called CC=1 SCAR=6.9
Pod undocumented
34 0 croak "repo_path() is obsolete, please use git_dir() instead";
35 }
36 0 ▶sub wc_path {
Subroutine wc_path: not called CC=1 SCAR=6.9
Pod undocumented
37 0 croak "wc_path() is obsolete, please use work_tree() instead";
38 }
39  
40 #
41 # support for loading plugins
42 #
43 34 ▶sub import {
Subroutine import: called CC=5 SCAR=16.1
44 34 my ( $class, @plugins ) = @_;
45  
46 34 for my $plugin (@plugins) {
47 5 ▶ ( $plugin, my @names ) = @$plugin if ref $plugin;
Branch: 1 branch
BranchTrueFalseTotal
if ref $plugin 2 3 5
48 5 ▶ $plugin
Branch: 1 branch
BranchTrueFalseTotal
substr($plugin, 0, 1) eq '+' ? : 1 4 5
49 = substr( $plugin, 0, 1 ) eq '+'
50 ? substr( $plugin, 1 )
51 : "Git::Repository::Plugin::$plugin";
52 0 ▶ eval "use $plugin; 1;" or croak $@;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "use $plugin; 1;" or croak($@) 4 1 5
53 4 $plugin->install(@names);
54 }
55 }
56  
57 #
58 # constructor-related methods
59 #
60  
61 55 ▶sub new {
Subroutine new: called CC=24 SCAR=31.8
Pod documented
62 55 my ( $class, @arg ) = @_;
63  
64 # create the object
65 55 my $self = bless {}, $class;
66  
67 # take out the option hash
68 55 my ( $options, %arg );
69 {
70 55 my @o;
71 55 ▶ %arg = grep !( ref eq 'HASH' ? push @o, $_ : 0 ), @arg;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 24 114 138
72 55 ▶ croak "Too many option hashes given: @o" if @o > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @o > 1 1 54 55
73 54 ▶ $options = $self->{options} = shift @o || {};
Condition100%2/2
shift @o || {}
l!l
2232
MC/DC100%1/1
shift @o || {}
shift @o
Truth table100%2/2
A
A= shift @o
Aresult
11
00
74 }
75  
76 # ignore 'input' and 'fatal' options during object creation
77 54 my $input = delete $options->{input};
78 54 my $fatal = delete $options->{fatal};
79  
80 # die if deprecated parameters are given
81 croak "repository is obsolete, please use git_dir instead"
82 54 ▶ if defined delete $arg{repository};
Branch: 1 branch
BranchTrueFalseTotal
if defined delete $arg{"repository"} 4 50 54
83 croak "working_copy is obsolete, please use work_tree instead"
84 50 ▶ if defined delete $arg{working_copy};
Branch: 1 branch
BranchTrueFalseTotal
if defined delete $arg{"working_copy"} 2 48 50
85  
86 # setup default options
87 48 my $git_dir = delete $arg{git_dir};
88 48 my $work_tree = delete $arg{work_tree};
89  
90 48 ▶ croak "Unknown parameters: @{[keys %arg]}" if keys %arg;
Branch: 1 branch
BranchTrueFalseTotal
if keys %arg 1 47 48
91  
92 # compute the various paths
93 47 ▶ my $cwd = defined $options->{cwd} ? $options->{cwd} : cwd();
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'cwd'} ? : 4 43 47
94  
95 # if work_tree or git_dir are relative, they are relative to cwd
96 47 ▶ -d ( $git_dir = _abs_path( $git_dir, $cwd ) )
Branch: 1 branch
BranchTrueFalseTotal
if defined $git_dir 23 24 47
Condition80%4/5
defined $git_dir && (-d ($git_dir = _abs_path($git_dir, $cwd)) || croak("directory not found: $git_dir"))
!ll&&!rl&&r
24230
-d ($git_dir = _abs_path($git_dir, $cwd)) or croak("directory not found: $git_dir")
l!l
221
MC/DC0%0/2
defined $git_dir && (-d ($git_dir = _abs_path($git_dir, $cwd)) || croak("directory not found: $git_dir"))
defined $git_dir -d ($git_dir = _abs_path($git_dir, $cwd))
Truth table66%2/3
A && B
A= defined $git_dirB= -d ($git_dir = _abs_path($git_dir, $cwd))
ABresult
0X0
100
111
97 or croak "directory not found: $git_dir"
98 if defined $git_dir;
99 46 ▶ -d ( $work_tree = _abs_path( $work_tree, $cwd ) )
Branch: 1 branch
BranchTrueFalseTotal
if defined $work_tree 17 29 46
Condition80%4/5
defined $work_tree && (-d ($work_tree = _abs_path($work_tree, $cwd)) || croak("directory not found: $work_tree"))
!ll&&!rl&&r
29170
-d ($work_tree = _abs_path($work_tree, $cwd)) or croak("directory not found: $work_tree")
l!l
161
MC/DC0%0/2
defined $work_tree && (-d ($work_tree = _abs_path($work_tree, $cwd)) || croak("directory not found: $work_tree"))
defined $work_tree -d ($work_tree = _abs_path($work_tree, $cwd))
Truth table66%2/3
A && B
A= defined $work_treeB= -d ($work_tree = _abs_path($work_tree, $cwd))
ABresult
0X0
100
111
100 or croak "directory not found: $work_tree"
101 if defined $work_tree;
102  
103 # if no cwd option given, assume we want to work in work_tree
104 $cwd = defined $options->{cwd} ? $options->{cwd}
105 45 ▶ : defined $work_tree ? $work_tree
Branch: 2 branches
BranchTrueFalseTotal
defined $options->{'cwd'} ? : 4 41 45
defined $work_tree ? : 15 26 41
106 : cwd();
107  
108 # we'll always have to compute it if not defined
109 45 ▶ $self->{git_dir} = _abs_path(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $git_dir 23 22 45
110 Git::Repository->run(
111 qw( rev-parse --git-dir ),
112 { %$options, cwd => $cwd }
113 ),
114 $cwd
115 ) if !defined $git_dir;
116  
117 # there are 4 possible cases
118 43 ▶ if ( !defined $work_tree ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $work_tree) { } 29 14 43
119  
120 # 1) no path defined: trust git with the values
121 # $self->{git_dir} already computed
122  
123 # 2) only git_dir was given: trust it
124 29 ▶ $self->{git_dir} = $git_dir if defined $git_dir;
Branch: 1 branch
BranchTrueFalseTotal
if defined $git_dir 20 9 29
125  
126 # in a non-bare repository, the work tree is just above the gitdir
127 29 ▶ if ( $self->run(qw( config --bool core.bare )) ne 'true' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->run("config", "--bool", "core.bare") ne "true") 23 6 29
128 $self->{work_tree}
129 23 = _abs_path( File::Spec->updir, $self->{git_dir} );
130 }
131 }
132 else {
133  
134 # 3) only work_tree defined:
135 14 ▶ if ( !defined $git_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $git_dir) { } 12 2 14
136  
137 # $self->{git_dir} already computed
138  
139 # check work_tree is the top-level work tree, and not a subdir
140 12 my $cdup = Git::Repository->run( qw( rev-parse --show-cdup ),
141 { %$options, cwd => $cwd } );
142 $self->{work_tree}
143 12 ▶ = $cdup ? _abs_path( $cdup, $work_tree ) : $work_tree;
Branch: 1 branch
BranchTrueFalseTotal
$cdup ? : 1 11 12
144 }
145  
146 # 4) both path defined: trust the values
147 else {
148 2 $self->{git_dir} = $git_dir;
149 2 $self->{work_tree} = $work_tree;
150 }
151 }
152  
153 # sanity check
154 my $gitdir
155 43 ▶ = eval { _abs_path( $self->run(qw( rev-parse --git-dir )), $cwd ) }
Condition100%2/2
eval { do { _abs_path($self->run('rev-parse', '--git-dir'), $cwd) } } || ''
l!l
412
MC/DC100%1/1
eval { do { _abs_path($self->run('rev-parse', '--git-dir'), $cwd) } } || ''
eval { do { _abs_path($self->run('rev-parse', '--git-dir'), $cwd) } }
Truth table100%2/2
A
A= eval { do { _abs_path($self->run('rev-parse', '--git-dir'), $cwd) } }
Aresult
11
00
156 || '';
157 croak "fatal: not a git repository: $self->{git_dir}"
158 43 ▶ if $self->{git_dir} ne $gitdir;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"git_dir"} ne $gitdir 2 41 43
159  
160 # put back the ignored options
161 41 ▶ $options->{input} = $input if defined $input;
Branch: 1 branch
BranchTrueFalseTotal
if defined $input 1 40 41
162 41 ▶ $options->{fatal} = $fatal if defined $fatal;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fatal 3 38 41
163  
164 41 return $self;
165 }
166  
167 # create() is now fully deprecated
168 1 ▶sub create {
Subroutine create: called CC=1 SCAR=0.0
Pod undocumented
169 1 croak "create() is deprecated, see Git::Repository::Tutorial for better alternatives";
170 }
171  
172 #
173 # command-related methods
174 #
175  
176 # return a Git::Repository::Command object
177 4 ▶sub command {
Subroutine command: called CC=2 SCAR=6.9
Pod documented
178 4 ▶ shift @_ if !ref $_[0]; # remove class name if called as class method
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 2 2 4
179 4 return Git::Repository::Command->new(@_);
180 }
181  
182 # run a command, returns the output
183 # die with errput if any
184 351 ▶sub run {
Subroutine run: called CC=3 SCAR=11.0
Pod documented
185 351 my ( $self, @cmd ) = @_;
186  
187 # split the args to get the optional callbacks
188 351 my @cb;
189 351 ▶ @cmd = grep { ref eq 'CODE' ? !push @cb, $_ : 1 } @cmd;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'CODE' ? : 3 806 809
190  
191 351 local $Carp::CarpLevel = 1;
192  
193 # run the command (pass the instance if called as an instance method)
194 351 ▶ my $command
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 140 211 351
195 = Git::Repository::Command->new( ref $self ? $self : (), @cmd );
196  
197 # return the output or die
198 342 return $command->final_output(@cb);
199 }
200  
201 #
202 # version comparison methods
203 #
204  
205 # NOTE: it doesn't make sense to try to cache the results of version():
206 # - yes, it will make faster benchmarks, but
207 # - the 'git' option allows to change the git binary anytime
208 # - version comparison is usually done once anyway
209 129 ▶sub version {
Subroutine version: called CC=1 SCAR=0.0
Pod documented
210 return (
211 129 shift->run( '--version', grep { ref eq 'HASH' } @_ )
212 =~ /git version (.*)/g )[0];
213 }
214  
215 18 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
216 18 for my $op ( qw( lt gt le ge eq ne ) ) {
217 18 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
218 108 ▶ *{"version_$op"} = eval << "OP";
Subroutine __ANON__: called CC=3 SCAR=11.0
Subroutine __ANON__: called CC=3 SCAR=11.0
Subroutine __ANON__: called CC=3 SCAR=11.0
Subroutine __ANON__: called CC=3 SCAR=11.0
Subroutine __ANON__: called CC=3 SCAR=11.0
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 6 branches
BranchTrueFalseTotal
ref $_ && ref $_ eq 'HASH' ? : 3 61 64
ref $_ && ref $_ eq 'HASH' ? : 6 46 52
ref $_ && ref $_ eq 'HASH' ? : 6 51 57
ref $_ && ref $_ eq 'HASH' ? : 12 67 79
ref $_ && ref $_ eq 'HASH' ? : 6 30 36
ref $_ && ref $_ eq 'HASH' ? : 6 30 36
Condition66%12/18
ref $_ and ref $_ eq "HASH"
!ll&&!rl&&r
6103
ref $_ and ref $_ eq "HASH"
!ll&&!rl&&r
4606
ref $_ and ref $_ eq "HASH"
!ll&&!rl&&r
5106
ref $_ and ref $_ eq "HASH"
!ll&&!rl&&r
67012
ref $_ and ref $_ eq "HASH"
!ll&&!rl&&r
3006
ref $_ and ref $_ eq "HASH"
!ll&&!rl&&r
3006
MC/DC50%1/2
ref $_ and ref $_ eq "HASH"
ref $_ ref $_ eq "HASH"
MC/DC50%1/2
ref $_ and ref $_ eq "HASH"
ref $_ ref $_ eq "HASH"
MC/DC50%1/2
ref $_ and ref $_ eq "HASH"
ref $_ ref $_ eq "HASH"
MC/DC50%1/2
ref $_ and ref $_ eq "HASH"
ref $_ ref $_ eq "HASH"
MC/DC50%1/2
ref $_ and ref $_ eq "HASH"
ref $_ ref $_ eq "HASH"
MC/DC50%1/2
ref $_ and ref $_ eq "HASH"
ref $_ ref $_ eq "HASH"
Truth table66%2/3
A and B
A= ref $_B= ref $_ eq "HASH"
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= ref $_B= ref $_ eq "HASH"
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= ref $_B= ref $_ eq "HASH"
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= ref $_B= ref $_ eq "HASH"
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= ref $_B= ref $_ eq "HASH"
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= ref $_B= ref $_ eq "HASH"
ABresult
0X0
100
111
219 sub {
220 my \$r = shift;
221 my \@o;
222 my (\$v) = grep !( ref && ref eq 'HASH' ? push \@o, \$_ : 0 ), \@_;
223 return ${op}_git( \$r->version(\@o), \$v );
224 }
225 OP
226 }
227 }
228  
229 18 1;
230  
231 __END__
« blib/lib/Git/Repository/Command.pm ↑ Summary blib/lib/Git/Repository/Plugin.pm »