Skip to content

Commit 09ca4c7

Browse files
committed
Add support for multiple search filter
1 parent 864cb27 commit 09ca4c7

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/lib/GrepCpan/Grep.pm

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,21 @@ sub _load_cache ( $self, $cache_file ) {
585585
return Sereal::read_sereal($cache_file);
586586
}
587587

588+
sub _parse_query_filetype ( $self, $query_filetype ) {
589+
590+
return unless defined $query_filetype;
591+
return unless length $query_filetype;
592+
593+
my @filetypes = split( /\s*,\s*/, $query_filetype );
594+
@filetypes
595+
= grep { length($_) && m{^ [a-zA-Z0-9_\-\.\*]+ $}x } @filetypes;
596+
597+
# ignore rules using '..'
598+
return if grep {m{\.\.}} @filetypes;
599+
600+
return \@filetypes;
601+
}
602+
588603
# convert a string of patterns (file to exclude) to a list of git rules to ignore the path
589604
# t/*, *.md, *.json, *.yaml, *.yml, *.conf, cpanfile, LICENSE, MANIFEST, INSTALL, Changes, Makefile.PL, Build.PL, Copying, *.SKIP, *.ini, README
590605
sub _parse_ignore_files ( $self, $ignore_files ) {
@@ -653,12 +668,21 @@ sub _get_match_cache(
653668
}
654669

655670
# filter on some type files distro + query filetype
656-
if ( defined $query_filetype
657-
&& length $query_filetype
658-
&& $query_filetype =~ qr{^[0-9\.\-\*_a-zA-Z]+$} )
659-
{
660-
# append to the distros search
661-
$git_cmd[-1] .= '*' . $query_filetype;
671+
if ( my $rules = $self->_parse_query_filetype($query_filetype) ) {
672+
my $base_search = $git_cmd[-1];
673+
my $is_first_rule = 1;
674+
foreach my $rule (@$rules) {
675+
676+
my $search = $base_search . '*' . $rule;
677+
678+
if ($is_first_rule) {
679+
$git_cmd[-1] = $search;
680+
$is_first_rule = 0;
681+
next;
682+
}
683+
684+
push @git_cmd, $search;
685+
}
662686
}
663687

664688
if ( my $rules = $self->_parse_ignore_files($ignore_files) ) {
@@ -868,7 +892,7 @@ sub run_git_cmd_limit ( $self, %opts ) {
868892
exit 42;
869893
}
870894

871-
note "Running in kid command: " . join( ' ', @$cmd );
895+
note "Running in kid command: " . join( ' ', 'git', @$cmd );
872896
note "KID is caching to file ", $cache_file;
873897

874898
my $to_cache;
@@ -898,7 +922,7 @@ sub run_git_cmd_limit ( $self, %opts ) {
898922
print {$to_cache} END_OF_FILE_MARKER() . qq{\n} if $cache_file;
899923
print {$CW} END_OF_FILE_MARKER() . qq{\n} if $can_write_to_pipe;
900924
note "-- Request finished by kid: $counter lines - "
901-
. join( ' ', @$cmd );
925+
. join( ' ', 'git', @$cmd );
902926
exit $?;
903927
}
904928

0 commit comments

Comments
 (0)