lib/App/GitFind.pm

filter: Statementstmt 50.0% 58 / 116 Branchbran 13.8% 5 / 36 Conditioncond 21.7% 5 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 73.9% 17 / 23 Podpod 100% 2 / 2 totaltotal 40.6% 87 / 214 CC 29 SCAR 32.6
CC
29
Cov
39%
BUILD
73.0
_process_options
35.6
_find_repo
20.0
CRAP 26.2
« lib/App/GitFind/Searcher/Git.pm ↑ Summary lib/App/GitFind/Base.pm »
1 package App::GitFind;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use App::GitFind::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use parent 'App::GitFind::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Class::Tiny _qwc <<'EOT';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 argv # the args we parse
12 _expr # the expression (AST from parsing)
13 _revs # the revs in the search scope, or ']]' for filesystem
14 _repo # The Git::Raw::Repository of the superproject we are in
15 _repotop # The working directory of _repo. Path::Class::Dir.
16 _searchbase # Path::Class::Dir with respect to which results
17 # are reported.
18 _searcher # App::GitFind::Searcher subclass that finds entries
19 _scan_submodules # If truthy, scan submodules.
20 EOT
21  
22 2 ▶use App::GitFind::Actions;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use App::GitFind::cmdline;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use App::GitFind::FileProcessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Getopt::Long 2.34 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Git::Raw;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use Iterator::Simple qw(ichain iflatten igrep imap iter iterator);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 #use Path::Class; # TODO see if we can do without Path::Class for speed
30 2 ▶use App::GitFind::PathClassMicro;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 our $VERSION = '0.000002';
33  
34 # === Documentation === {{{1
35  
36 =head1 NAME
37  
38 App::GitFind - Find files anywhere in a Git repository
39  
40 =head1 SYNOPSIS
41  
42 This is the implementation of the L<git-find> command (q.v.). To use it
43 from Perl code:
44  
45 use App::GitFind;
46 exit App::GitFind->new(\@ARGV)->run;
47  
48 =head1 SUBROUTINES/METHODS
49  
50 =cut
51  
52 # }}}1
53  
54 # Later items:
55 # TODO add an option to report absolute paths instead of relative
56 # TODO skip .git and .gitignored files unless -u
57 # TODO optimization: if possible, add a filter function
58 # (e.g., for a top-level -type filter)
59  
60 =head2 BUILD
61  
62 Process the arguments. Usage:
63  
64 my $gf = App::GitFind->new(-argv => \@ARGV, -searchbase => Cwd::getcwd);
65  
66 May modify the provided array. May C<exit()>, e.g., on C<--help>.
67  
68 =cut
69  
70 1 ▶sub BUILD {
Subroutine BUILD: called CC=10 SCAR=42.9
Pod documented
71 1 my ($self, $hrArgs) = @_;
72 1 ▶ croak "Need a -argv arrayref" unless ref $hrArgs->{argv} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hrArgs->{"argv"} eq "ARRAY" 0 1 1
73 1 ▶ croak "Need a -searchbase" unless defined $hrArgs->{searchbase};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hrArgs->{"searchbase"} 0 1 1
74  
75 1 my $details = _process_options($hrArgs->{argv});
76  
77 # Handle default -print
78 0 ▶ if(!$details->{expr}) { # Default: -print
Branch: 2 branches
BranchTrueFalseTotal
if (not $details->{'expr'}) { } 0 0 0
elsif (not $details->{'saw_nonprune_action'}) { } 0 0 0
79 0 $details->{expr} = App::GitFind::Actions::argdetails('print');
80  
81 } elsif(!$details->{saw_nonprune_action}) { # With an expr: -print unless an action
82 # other than -prune was given
83 $details->{expr} = +{
84 0 AND => [ $details->{expr}, App::GitFind::Actions::argdetails('print') ]
85 };
86 }
87  
88 # Add default for missing revs
89 0 ▶ unless($details->{revs}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($details->{"revs"}) 0 0 0
90 0 $details->{revs} = [undef];
91 0 ▶ $details->{saw_non_rr} ||= true;
Condition0%0/2
$details->{"saw_non_rr"} ||= !!1
l!l
00
MC/DC0%0/1
$details->{"saw_non_rr"} ||= !!1
$details->{"saw_non_rr"}
Truth table0%0/2
A
A= $details->{"saw_non_rr"}
Aresult
11
00
92 }
93 0 ▶ if(@{$details->{revs}} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$details->{"revs"};} > 1) 0 0 0
94 0 require List::SomeUtils;
95 0 $details->{revs} = [List::SomeUtils::uniq(@{$details->{revs}})];
96 }
97  
98 0 ▶ vlog { "Options:", ddc $details } 2;
Subroutine __ANON__: not called CC=1 SCAR=6.9
99  
100 # Check the scope. TODO permit searching both ]] and non-]] in one run
101 0 ▶ if($details->{saw_rr} && $details->{saw_non_rr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($details->{"saw_rr"} and $details->{"saw_non_rr"}) 0 0 0
Condition0%0/3
$details->{"saw_rr"} and $details->{"saw_non_rr"}
!ll&&!rl&&r
000
MC/DC0%0/2
$details->{"saw_rr"} and $details->{"saw_non_rr"}
$details->{"saw_rr"} $details->{"saw_non_rr"}
Truth table0%0/3
A and B
A= $details->{"saw_rr"}B= $details->{"saw_non_rr"}
ABresult
0X0
100
111
102 0 die "I don't know how to search both ']]' and a Git rev at once."
103 }
104  
105 # Copy information into our instance fields
106 0 $self->_expr($details->{expr});
107 0 $self->_revs($details->{revs});
108 $self->_searchbase(
109 App::GitFind::PathClassMicro::Dir->new($hrArgs->{searchbase})
110 0 );
111  
112 0 $self->_find_repo;
113  
114 } #BUILD()
115  
116 # Initialize _repo and _repotop. Dies on error.
117 0 ▶sub _find_repo {
Subroutine _find_repo: not called CC=4 SCAR=30.0
118 0 my $self = shift;
119 # Find the repo we're in. If we're in a submodule, that will be the
120 # repo of that submodule.
121 0 my $repo = eval { Git::Raw::Repository->discover('.'); };
122 0 ▶ die "Not in a Git repository: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
123 0 $self->_repo($repo);
124  
125 0 $self->_repotop( App::GitFind::PathClassMicro::Dir->new($self->_repo->workdir) );
126 # $repo->path is .git/
127 0 ▶ vlog {
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 "Repository:", $self->_repo->path,
129 "\nWorking dir:", $self->_repotop,
130 "\nSearch base:", $self->_searchbase,
131 0 };
132  
133 # Are we in a submodule? If so, move outward to the parent
134 0 ▶ if($self->_repo->path =~ qr{^(.+?[\\\/]\.git)[\\\/]modules[\\\/]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_repo->path =~ qr"^(.+?[\\\/]\.git)[\\\/]modules[\\\/]") 0 0 0
135 0 my $parent_path = $1;
136 0 $repo = eval { Git::Raw::Repository->open($parent_path); };
137 0 ▶ die "Could not open parent Git repository in $parent_path: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
138 0 $self->_repo($repo);
139 0 ▶ vlog { 'Moved to outer repo', $parent_path };
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 }
141 } #_find_repo()
142  
143 =head2 run
144  
145 Does the work. Call as C<< exit($obj->run()) >>. Returns a shell exit code.
146  
147 =cut
148  
149 0 ▶sub run {
Subroutine run: not called CC=3 SCAR=24.8
Pod documented
150 0 my $self = shift;
151 0 my $runner = App::GitFind::FileProcessor->new(-expr => $self->_expr);
152 0 my $callback = $runner->callback($VERBOSE>=3);
153  
154 0 ▶ if($VERBOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::GitFind::VERBOSE) 0 0 0
155 0 STDOUT->autoflush(true);
156 0 STDERR->autoflush(true);
157 }
158  
159 0 for my $rev (@{$self->_revs}) {
160 0 my $searcher = $self->_make_searcher($rev, $self->_repo);
161 # TODO? deduplicate?
162 0 $searcher->run($callback);
163 # TODO? early stop?
164 }
165  
166 0 return 0; # TODO? return 1 if any -exec failed?
167 } #run()
168  
169 =head1 INTERNALS
170  
171 =head2 _make_searcher
172  
173 Create an iterator for the entries to be processed. Returns an
174 L<App::GitFind::Searcher>. Usage:
175  
176 my $searcher = $self->_make_searcher('rev', -in => $repo);
177  
178 =cut
179  
180 0 ▶sub _make_searcher {
Subroutine _make_searcher: not called CC=3 SCAR=24.8
181 0 my ($self, %args) = getparameters('self', [qw(rev in)], @_);
182 0 my $rev = $args{rev};
183 0 my $repo = $args{in};
184  
185 # TODO find files in scope $self->_revs, repo $repo
186  
187 0 ▶ if(!defined $rev) { # The index of the current repo
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $rev) { } 0 0 0
elsif ($rev eq ']]') { } 0 0 0
188 0 require App::GitFind::Searcher::Git;
189 0 return App::GitFind::Searcher::Git->new(
190 -repo => $repo,
191 -searchbase=>$self->_searchbase
192 );
193  
194 } elsif($rev eq ']]') { # The current working directory
195 0 require App::GitFind::Searcher::FileSystem;
196 0 return App::GitFind::Searcher::FileSystem->new(
197 -repo => $repo,
198 -searchbase=>$self->_searchbase
199 );
200  
201 } else {
202 0 die "I don't yet know how to search through rev $_";
203 }
204  
205 } #_make_searcher
206  
207 =head2 _process_options
208  
209 Process the options and return a hashref. Any remaining arguments are
210 stored under key C<_>.
211  
212 =cut
213  
214 1 ▶sub _process_options {
Subroutine _process_options: called CC=12 SCAR=35.7
215 1 ▶ my $lrArgv = shift // [];
Condition50%1/2
shift() // []
l!l
10
MC/DC0%0/1
shift() // []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
216 1 my $hrOpts;
217 1 ▶ local *have = sub { $hrOpts->{switches}->{$_[0] // $_} };
Subroutine __ANON__: called CC=2 SCAR=8.1
Condition33%1/3
$_[0] // $_
l!l&&r!l&&!r
200
MC/DC0%0/2
$_[0] // $_
$_[0] $_
Truth table33%1/3
A // B
A= $_[0]B= $_
ABresult
1X1
011
000
218  
219 1 ▶ $hrOpts = App::GitFind::cmdline::Parse($lrArgv)
Branch: 1 branch
BranchTrueFalseTotal
$hrOpts = App::GitFind::cmdline::Parse($lrArgv) or die "Could not parse options successfully" 1 0 1
220 or die 'Could not parse options successfully';
221  
222 1 ▶ $VERBOSE = scalar @{$hrOpts->{switches}->{v} // []};
Condition50%1/2
$hrOpts->{"switches"}{"v"} // []
l!l
01
MC/DC0%0/1
$hrOpts->{"switches"}{"v"} // []
$hrOpts->{"switches"}{"v"}
Truth table50%1/2
A
A= $hrOpts->{"switches"}{"v"}
Aresult
11
00
223 1 ▶ $QUIET = scalar @{$hrOpts->{switches}->{q} // []};
Condition50%1/2
$hrOpts->{"switches"}{"q"} // []
l!l
01
MC/DC0%0/1
$hrOpts->{"switches"}{"q"} // []
$hrOpts->{"switches"}{"q"}
Truth table50%1/2
A
A= $hrOpts->{"switches"}{"q"}
Aresult
11
00
224  
225 1 ▶ Getopt::Long::HelpMessage(-exitval => 0, -verbose => 2) if have('man');
Branch: 1 branch
BranchTrueFalseTotal
if have("man") 0 1 1
226 1 ▶ Getopt::Long::HelpMessage(-exitval => 0, -verbose => 1)
Branch: 1 branch
BranchTrueFalseTotal
if have("h") or have("help") 1 0 1
Condition33%1/3
have("h") or have("help")
l!l&&r!l&&!r
100
MC/DC0%0/2
have("h") or have("help")
have("h") have("help")
Truth table33%1/3
A or B
A= have("h")B= have("help")
ABresult
1X1
011
000
227 if have('h') || have('help');
228 0 ▶ Getopt::Long::HelpMessage(-exitval => 0) if have('?') || have('usage');
Branch: 1 branch
BranchTrueFalseTotal
if have("?") or have("usage") 0 0 0
Condition0%0/3
have("?") or have("usage")
l!l&&r!l&&!r
000
MC/DC0%0/2
have("?") or have("usage")
have("?") have("usage")
Truth table0%0/3
A or B
A= have("?")B= have("usage")
ABresult
1X1
011
000
229 0 ▶ Getopt::Long::VersionMessage(-exitval => 0) if have('V')||have('version');
Branch: 1 branch
BranchTrueFalseTotal
if have("V") or have("version") 0 0 0
Condition0%0/3
have("V") or have("version")
l!l&&r!l&&!r
000
MC/DC0%0/2
have("V") or have("version")
have("V") have("version")
Truth table0%0/3
A or B
A= have("V")B= have("version")
ABresult
1X1
011
000
230  
231 0 return $hrOpts;
232 } #_process_options
233  
234 2 1; # End of App::GitFind
235 __END__
« lib/App/GitFind/Searcher/Git.pm ↑ Summary lib/App/GitFind/Base.pm »