Skip to content

Commit ae257d1

Browse files
committed
Fix output parser to show more results
Fix #63 The git output parser was pretty naive and buggy when multiple chunks were found from the same file. This was resulting in skipping results we should have displayed to begin with. That changeset is fixing that issue and will now show more results. The summary was accurate but the output truncated due to poor man parsing reason.
1 parent ccbee78 commit ae257d1

File tree

1 file changed

+49
-33
lines changed

1 file changed

+49
-33
lines changed

src/lib/GrepCpan/Grep.pm

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ sub _do_search ( $self, %opts ) {
282282
$page = 0 if $page < 0;
283283

284284
#
285-
my $cache = $self->get_match_cache( $search, $search_distro, $filetype,
285+
my $cache = $self->_get_match_cache( $search, $search_distro, $filetype,
286286
$caseinsensitive, $ignore_files );
287287

288288
my $is_a_known_distro
@@ -362,41 +362,55 @@ sub _do_search ( $self, %opts ) {
362362
return;
363363
};
364364

365+
my $previous_file;
366+
my $qr_match_line = qr{^([0-9]+)([-:])};
367+
365368
foreach my $line (@$matches) {
366-
if ( !defined $current_file && $line !~ qr{^([0-9]+)[-:]} )
367-
{ # when more than one block match we are just going to have a -- separator
368-
$current_file = $line;
369+
if ( !defined $current_file ) {
370+
371+
# when more than one block match we are just going to have a -- separator
372+
if ( $line =~ m{^distros/} ) {
373+
$previous_file = $current_file = $line;
374+
next;
375+
}
376+
$current_file //= $previous_file;
369377
}
370-
elsif ( $line eq '--' ) {
378+
379+
if ( $line eq '--' ) {
380+
381+
# we found a new block, it's either from the current file or a new one
371382
$process_file->();
372-
undef $current_file;
383+
undef $current_file; # reset: could use previous or next file
373384
$diff = '';
374385
@diffblocks = ();
375386
undef $start_line;
376387
undef $line_number;
377388
@matching_lines = ();
389+
next;
378390
}
379-
elsif ( $line =~ s{^([0-9]+)([-:])}{} ) { # mainly else
380-
my $new_line = $1;
381-
my $prefix = $2;
382-
$start_line //= $new_line;
383-
if ( length($line) > 250 )
384-
{ # max length autorized ( js minified & co )
385-
$line = substr( $line, 0, 250 ) . '...';
386-
}
387-
if ( !defined $line_number || $new_line == $line_number + 1 ) {
388391

389-
# same block
390-
push @matching_lines, $new_line if $prefix eq ':';
391-
$diff .= $line . "\n";
392-
}
393-
else {
394-
# new block
395-
$add_block->();
396-
$diff = $line . "\n"; # reset the block
397-
}
398-
$line_number = $new_line;
392+
# matching the main part
393+
next unless $line =~ s/$qr_match_line//;
394+
my ( $new_line, $prefix ) = ( $1, $2 );
395+
396+
$start_line //= $new_line;
397+
if ( length($line) > 250 )
398+
{ # max length autorized ( js minified & co )
399+
$line = substr( $line, 0, 250 ) . '...';
399400
}
401+
if ( !defined $line_number || $new_line == $line_number + 1 ) {
402+
403+
# same block
404+
push @matching_lines, $new_line if $prefix eq ':';
405+
$diff .= $line . "\n";
406+
}
407+
else {
408+
# new block
409+
$add_block->();
410+
$diff = $line . "\n"; # reset the block
411+
}
412+
$line_number = $new_line;
413+
400414
}
401415
$process_file->(); # process the last block
402416

@@ -592,7 +606,7 @@ sub _parse_ignore_files ( $self, $ignore_files ) {
592606
return \@rules;
593607
}
594608

595-
sub get_match_cache(
609+
sub _get_match_cache(
596610
$self, $search, $search_distro, $query_filetype,
597611
$caseinsensitive = 0,
598612
$ignore_files = undef
@@ -601,6 +615,11 @@ sub get_match_cache(
601615

602616
$caseinsensitive //= 0;
603617

618+
my @keys_for_cache = (
619+
$search, $search_distro, $query_filetype,
620+
$caseinsensitive, $ignore_files // ''
621+
);
622+
604623
my $gitdir = $self->git()->work_tree;
605624
my $limit = $self->config()->{limit}->{files_per_search} or die;
606625

@@ -614,10 +633,8 @@ sub get_match_cache(
614633
}
615634

616635
# use the full cache when available -- need to filter it later
617-
my $request_cache_file = $self->_get_cache_file(
618-
[ $search, $search_distro, $query_filetype, $caseinsensitive, $ignore_files // '' ] );
619-
{
620-
my $load = $self->_load_cache($request_cache_file);
636+
my $request_cache_file = $self->_get_cache_file( \@keys_for_cache );
637+
if ( my $load = $self->_load_cache($request_cache_file) ) {
621638
return $load if $load;
622639
}
623640

@@ -647,8 +664,7 @@ sub get_match_cache(
647664

648665
# fallback to a shorter search ( and a different cache )
649666
my $cache_file = $self->_get_cache_file( [@git_cmd] );
650-
{
651-
my $load = $self->_load_cache($cache_file);
667+
if ( my $load = $self->_load_cache($cache_file) ) {
652668
return $load if $load;
653669
}
654670

@@ -699,7 +715,7 @@ sub get_match_cache(
699715

700716
$cache->{match} = {
701717
files => $match_files,
702-
distros => scalar keys %{ $cache->{distros} }
718+
distros => scalar keys $cache->{distros}->%*,
703719
};
704720

705721
if ( !$search_in_progress ) {

0 commit comments

Comments
 (0)