| 1 | package App::GitFind; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use App::GitFind::Base; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use App::GitFind::cmdline; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use App::GitFind::Runner; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Getopt::Long 2.34 (); | ||
| 11 | use Git::Raw; | ||
| 12 | use Iterator::Simple; | ||
| 13 | use Path::Class; | ||
| 14 | |||
| 15 | our $VERSION = '0.000001'; # TRIAL | ||
| 16 | |||
| 17 | # === Documentation === {{{1 | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | App::GitFind - Find files anywhere in a Git repository | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | This is the implementation of the L<git-find> command (q.v.). To use it | ||
| 26 | from Perl code: | ||
| 27 | |||
| 28 | use App::GitFind; | ||
| 29 | exit App::GitFind->new(\@ARGV)->run; | ||
| 30 | |||
| 31 | =head1 SUBROUTINES/METHODS | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | # }}}1 | ||
| 36 | |||
| 37 | =head2 new | ||
| 38 | |||
| 39 | The constructor. Takes an arrayref of arguments, e.g., C<\@ARGV>. May | ||
| 40 | C<exit()>, e.g., on C<--help>. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | sub new { | ||
| 45 | my ($package, $lrArgv) = @_; | ||
| 46 | my $self = _process_options($lrArgv); | ||
| 47 | |||
| 48 | # Handle default -print | ||
| 49 | if(!$self->{expr}) { # Default: -print | ||
| 50 | $self->{expr} = 'print'; | ||
| 51 | |||
| 52 | } elsif(!$self->{sawnpa}) { # With an expr: -print unless an action | ||
| 53 | # other than -prune was given | ||
| 54 | $self->{expr} = +{ | ||
| 55 | AND => [ $self->{expr}, 'print' ] | ||
| 56 | }; | ||
| 57 | } | ||
| 58 | |||
| 59 | # Add default for missing revs | ||
| 60 | $self->{revs} = [undef] unless $self->{revs}; | ||
| 61 | |||
| 62 | print "Options: ", ddc $self if $TRACE>1; | ||
| 63 | |||
| 64 | # find the repo we're in | ||
| 65 | $self->{repo} = eval { Git::Raw::Repository->discover('.'); }; | ||
| 66 | die "Not in a Git repository: $@\n" if $@; | ||
| 67 | $self->{repotop} = dir($self->{repo}->commondir)->parent; | ||
| 68 | say "Repo in ", $self->{repo}->commondir if $TRACE; # .git dir | ||
| 69 | |||
| 70 | bless $self, $package; | ||
| 71 | } #new() | ||
| 72 | |||
| 73 | =head2 run | ||
| 74 | |||
| 75 | Does the work. Call as C<< exit($obj->run()) >>. Returns a shell exit code. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub run { | ||
| 80 | my $self = shift; | ||
| 81 | my $runner = App::GitFind::Runner->new(-expr => $self->{expr}); | ||
| 82 | |||
| 83 | my $iter = $self->_entry_iterator; | ||
| 84 | |||
| 85 | while (defined(my $entry = $iter->next)) { | ||
| 86 | print "$entry: " if $TRACE; | ||
| 87 | my $ok = $runner->process($entry); | ||
| 88 | print $ok ? 'passed' : 'failed', "\n" if $TRACE; | ||
| 89 | } | ||
| 90 | |||
| 91 | return 0; # TODO return 1 if anything failed. | ||
| 92 | } #run() | ||
| 93 | |||
| 94 | =head1 INTERNALS | ||
| 95 | |||
| 96 | =head2 _entry_iterator | ||
| 97 | |||
| 98 | Create an iterator for the entries to be processed. Returns an | ||
| 99 | L<Iterator::Simple>. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub _entry_iterator { | ||
| 104 | my $self = shift; | ||
| 105 | |||
| 106 | return Iterator::Simple::ichain( | ||
| 107 | map { $self->_iterator_for($_) } @{$self->{revs}} | ||
| 108 | ); | ||
| 109 | |||
| 110 | } #_entry_iterator | ||
| 111 | |||
| 112 | =head2 _iterator_for | ||
| 113 | |||
| 114 | Return an iterator for a particular rev. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | sub _iterator_for { | ||
| 119 | my ($self, $rev) = @_; | ||
| 120 | # TODO find files in scope $self->{revs}, repo $self->{repo} | ||
| 121 | |||
| 122 | if(!defined $rev) { # The index of the current repo | ||
| 123 | return Iterator::Simple::iter(['./TEST!!']); # DEBUG | ||
| 124 | |||
| 125 | } elsif($rev eq ']]') { # The current working directory | ||
| 126 | require File::Next; | ||
| 127 | return File::Next::everything($self->{repotop}->relative); | ||
| 128 | # TODO add an option to report absolute paths instead of relative | ||
| 129 | # TODO skip .git and .gitignored files unless -u | ||
| 130 | # TODO optimization: if -type f or -type d is at the top level, | ||
| 131 | # use File::Next::files() or dirs() respectively. | ||
| 132 | |||
| 133 | } else { | ||
| 134 | die "I don't yet know how to search through rev $_"; | ||
| 135 | } | ||
| 136 | |||
| 137 | } #_iterator_for | ||
| 138 | |||
| 139 | =head2 _process_options | ||
| 140 | |||
| 141 | Process the options and return a hashref. Any remaining arguments are | ||
| 142 | stored under key C<_>. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub _process_options { | ||
| 147 | my $lrArgv = shift // []; | ||
| 148 | my $hrOpts; | ||
| 149 | local *have = sub { $hrOpts->{switches}->{$_[0] // $_} }; | ||
| 150 | |||
| 151 | $hrOpts = App::GitFind::cmdline::Parse($lrArgv) | ||
| 152 | or die 'Could not parse options successfully'; | ||
| 153 | |||
| 154 | $TRACE = scalar @{$hrOpts->{switches}->{v} // []}; | ||
| 155 | |||
| 156 | Getopt::Long::HelpMessage(-exitval => 0, -verbose => 2) if have('man'); | ||
| 157 | Getopt::Long::HelpMessage(-exitval => 0, -verbose => 1) | ||
| 158 | if have('h') || have('help'); | ||
| 159 | Getopt::Long::HelpMessage(-exitval => 0) if have('?') || have('usage'); | ||
| 160 | Getopt::Long::VersionMessage(-exitval => 0) if have('V')||have('version'); | ||
| 161 | |||
| 162 | return $hrOpts; | ||
| 163 | } #_process_options | ||
| 164 | |||
| 165 | 1; # End of App::GitFind | ||
| 166 | __END__ | ||