Skip to content

Commit e0fcdb2

Browse files
committed
Start ignoring some files
1 parent 6b51bee commit e0fcdb2

File tree

7 files changed

+108
-61
lines changed

7 files changed

+108
-61
lines changed

lib/GrepCpan/Grep.pm

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ use Digest::MD5 qw( md5_hex );
3838
use constant END_OF_FILE_MARKER => qq{##______END_OF_FILE_MARKER______##};
3939
use constant TOO_BUSY_MARKER => qq{##______TOO_BUSY_MARKER______##};
4040

41+
use constant CACHE_IS_ENABLED => 1;
42+
4143
sub _build_git {
4244
my $self = shift;
4345

@@ -239,14 +241,14 @@ sub _get_git_grep_flavor {
239241

240242
# idea use git rev-parse HEAD to include it in the cache name
241243

242-
sub do_search {
243-
my ( $self, %opts ) = @_;
244+
sub do_search ( $self, %opts ) {
244245

245-
my ( $search, $search_distro, $search_file, $filetype, $caseinsensitive, )
246+
my ( $search, $search_distro, $search_file, $filetype,
247+
$caseinsensitive, $ignore_files, )
246248
= (
247-
$opts{search}, $opts{search_distro},
248-
$opts{search_file}, $opts{filetype},
249-
$opts{caseinsensitive},
249+
$opts{search}, $opts{search_distro},
250+
$opts{search_file}, $opts{filetype},
251+
$opts{caseinsensitive}, $opts{ignore_files},
250252
);
251253

252254
my $t0 = [Time::HiRes::gettimeofday];
@@ -275,21 +277,21 @@ sub do_search {
275277
};
276278
}
277279

278-
sub _do_search {
279-
my ( $self, %opts ) = @_;
280+
sub _do_search ( $self, %opts ) {
280281

281282
my ( $search, $page, $search_distro, $search_file,
282-
$filetype, $caseinsensitive, )
283+
$filetype, $caseinsensitive, $ignore_files )
283284
= (
284285
$opts{search}, $opts{page}, $opts{search_distro}, $opts{search_file},
285-
$opts{filetype}, $opts{caseinsensitive},
286+
$opts{filetype}, $opts{caseinsensitive}, $opts{ignore_files}
286287
);
287288

288289
$page //= 0;
289290
$page = 0 if $page < 0;
290291

291-
my $cache = $self->get_match_cache( $search, $search_distro,
292-
$filetype, $caseinsensitive );
292+
#
293+
my $cache = $self->get_match_cache( $search, $search_distro, $filetype,
294+
$caseinsensitive, $ignore_files );
293295

294296
my $is_a_known_distro
295297
= defined $search_distro
@@ -533,8 +535,7 @@ sub get_list_of_files_to_search {
533535
return \@short;
534536
}
535537

536-
sub _save_cache {
537-
my ( $self, $cache_file, $cache ) = @_;
538+
sub _save_cache ( $self, $cache_file, $cache ) {
538539

539540
# cache is disabled
540541
return if $self->config()->{nocache};
@@ -547,8 +548,7 @@ sub _save_cache {
547548
return;
548549
}
549550

550-
sub _get_cache_file {
551-
my ( $self, $keys, $type ) = @_;
551+
sub _get_cache_file ( $self, $keys, $type = undef ) {
552552

553553
$type //= q[/search-ls];
554554
$type .= '-';
@@ -562,8 +562,9 @@ sub _get_cache_file {
562562
return $cache_file;
563563
}
564564

565-
sub _load_cache {
566-
my ( $self, $cache_file ) = @_;
565+
sub _load_cache ( $self, $cache_file ) {
566+
567+
return unless CACHE_IS_ENABLED;
567568

568569
# cache is disabled
569570
return if $self->config()->{nocache};
@@ -572,8 +573,30 @@ sub _load_cache {
572573
return Sereal::read_sereal($cache_file);
573574
}
574575

576+
# convert a string of patterns (file to exclude) to a list of git rules to ignore the path
577+
# t/*, *.md, *.json, *.yaml, *.yml, *.conf, cpanfile, LICENSE, MANIFEST, INSTALL, Changes, Makefile.PL, Build.PL, Copying, *.SKIP, *.ini, README
578+
sub _parse_ignore_files ( $self, $ignore_files ) {
579+
580+
my @ignorelist = grep {m{^ [a-zA-Z0-9_\-\.\*/]+ $}x}
581+
grep { length $_ } split( /\s*,\s*/, $ignore_files );
582+
583+
# ignore rules using '..'
584+
return if grep {m{\.\.}} @ignorelist;
585+
586+
return unless scalar @ignorelist;
587+
588+
my @rules;
589+
foreach my $ignore (@ignorelist) {
590+
$ignore = '/*' . $ignore unless $ignore =~ m{^\*};
591+
push @rules, qq[:!$ignore];
592+
}
593+
594+
return \@rules;
595+
}
596+
575597
sub get_match_cache {
576-
my ( $self, $search, $search_distro, $query_filetype, $caseinsensitive )
598+
my ( $self, $search, $search_distro, $query_filetype, $caseinsensitive,
599+
$ignore_files )
577600
= @_;
578601

579602
$caseinsensitive //= 0;
@@ -586,6 +609,10 @@ sub get_match_cache {
586609
push @git_cmd, q{-i} if $caseinsensitive;
587610
push @git_cmd, $flavor, '-e', $search, q{--}, q{distros/};
588611

612+
if ( my $rules = $self->_parse_ignore_files($ignore_files) ) {
613+
push @git_cmd, $rules->@*;
614+
}
615+
589616
# use the full cache when available -- need to filter it later
590617
my $request_cache_file = $self->_get_cache_file(
591618
[ $search, $search_distro, $query_filetype, $caseinsensitive ] );
@@ -686,17 +713,15 @@ sub get_match_cache {
686713
return $cache;
687714
}
688715

689-
sub massage_filepath {
690-
my $line = shift;
716+
sub massage_filepath ($line) {
691717
my ( $where, $letter, $distro, $shortpath ) = split( q{/}, $line, 4 );
692718
$where //= '';
693719
$letter //= '';
694720
$where .= '/' . $letter;
695721
return ( $where, $distro, $shortpath );
696722
}
697723

698-
sub run_git_cmd_limit {
699-
my ( $self, %opts ) = @_;
724+
sub run_git_cmd_limit ( $self, %opts ) {
700725

701726
my $cache_file = $opts{cache_file};
702727
my $cmd = $opts{cmd} // die;

lib/grepcpan.pm

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,23 @@ get '/search/:legacy' => sub { # need to disable it from js
6060
};
6161

6262
get '/search' => sub {
63-
my $q = param('q'); # FIXME clean query
64-
my $filetype = param('qft');
65-
my $qdistro = param('qd');
66-
my $qci = param('qci'); # case insensitive
67-
my $qls = param('qls'); # only list files
68-
my $page = param('p') || 1;
69-
my $file = param('f');
70-
my $query = $grep->do_search(
63+
my $q = param('q'); # FIXME clean query
64+
my $filetype = param('qft');
65+
my $qdistro = param('qd');
66+
my $qci = param('qci'); # case insensitive
67+
my $qls = param('qls'); # only list files
68+
my $page = param('p') || 1;
69+
my $file = param('f');
70+
my $ignore_files = param('qifl');
71+
my $query = $grep->do_search(
7172
search => $q,
7273
page => $page - 1,
7374
search_distro => $qdistro, # filter on a distribution
7475
search_file => $file,
7576
filetype => $filetype,
7677
caseinsensitive => $qci,
7778
list_files => $qls, # not used for now, only impact the view
79+
ignore_files => $ignore_files
7880
);
7981

8082
my $nopagination = defined $file && length $file ? 1 : 0;
@@ -95,24 +97,27 @@ get '/search' => sub {
9597
qd => $qdistro, #$qdistro // q{},
9698
qls => $qls,
9799
qci => $qci,
100+
qifl => $ignore_files,
98101
};
99102
};
100103

101104
### API routes
102105
get '/api/search' => sub {
103-
my $q = param('q');
104-
my $filetype = param('qft');
105-
my $qdistro = param('qd');
106-
my $qci = param('qci'); # case insensitive
107-
my $page = param('p') || 1;
108-
my $file = param('f');
106+
my $q = param('q');
107+
my $filetype = param('qft');
108+
my $qdistro = param('qd');
109+
my $qci = param('qci'); # case insensitive
110+
my $page = param('p') || 1;
111+
my $file = param('f');
112+
my $ignore_files = param('qifl');
109113

110114
my $query = $grep->do_search(
111115
search => $q,
112116
page => $page - 1,
113-
search_distro => $qdistro, # filter on a distribution
117+
search_distro => $qdistro, # filter on a distribution
114118
filetype => $filetype,
115119
caseinsensitive => $qci,
120+
ignore_files => $ignore_files,
116121
);
117122

118123
content_type 'application/json';

public/_assets/20230427152712-510a05c940bec575d4a5edfd45e2668f.css

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8628,7 +8628,7 @@ h3,
86288628
font-size: 16px;
86298629
padding: 4px 12px;
86308630
height: 30px;
8631-
width: 280px;
8631+
width: 250px;
86328632
}
86338633

86348634
input#search-distro-input-results {
@@ -8645,6 +8645,12 @@ input#search-filetype-input-results{
86458645
width: 60px;
86468646
}
86478647

8648+
input#search-ignore-files{
8649+
font-size: 16px;
8650+
padding: 4px 12px;
8651+
height: 30px;
8652+
width: 250px;
8653+
}
86488654

86498655
.toolbar-search {
86508656
margin-bottom: 2px;
@@ -10372,6 +10378,12 @@ body .syntaxhighlighter .container .pod-placeholder button {
1037210378
font-size: 1.4em;
1037310379
}
1037410380

10381+
.home-search-ignore-files {
10382+
font-size: 1.4em;
10383+
padding: 4px 12px;
10384+
float:left;
10385+
}
10386+
1037510387
.optional-input:placeholder-shown {
1037610388
font-style: italic;
1037710389
}

views/_display.tt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
%>
88
<% END %>
99
<div class="container-fluid">
10-
11-
1210
<div class="row hidden-phone">
1311
<div class="head-small-logo col-md-3">
1412
<a href="/" class="small-logo"></a>
@@ -20,6 +18,7 @@
2018
<input type="text" name="q" size="41" autofocus="autofocus" id="search-input" class="form-control search-input-results" style="width:280px;" value="<% search | html_entity %>" autocomplete="off" placeholder="search with Perl Compatible RegEx">
2119
<input type="text" name="qd" size="35" id="search-distro-input-results" class="form-control optional-input" value="<% qd | html_entity %>" autocomplete="off" placeholder="in distro like ^App-.*">
2220
<input type="text" name="qft" size="6" id="search-filetype-input-results" class="form-control optional-input" value="<% qft | html_entity %>" autocomplete="off" placeholder="*.pm">
21+
<input type="text" name="qifl" size="35" id="search-ignore-files" class="form-control optional-input" value="<% qifl | html_entity %>" autocomplete="off" placeholder="ignore files like: *.t, ppport.h">
2322
<span class="input-group-btn">
2423
<button class="btn search-btn" type="submit">Search</button>
2524
</span>
@@ -32,7 +31,7 @@
3231
</form>
3332
</div>
3433
</div>
35-
34+
3635
<div class="row">
3736
<div class="main-content col-md-12">
3837
<ul class="nav-list slidepanel sticky-panel-top">
@@ -45,7 +44,7 @@
4544
<% END %>
4645

4746
<div class="content search-results">
48-
47+
4948
<% IF show_sumup %>
5049
<div class="module-result">
5150
<big><strong>Result: </strong></big>
@@ -62,7 +61,7 @@
6261
<% ELSE %>
6362
found <b><% query.match.distros %></b> distributions and <b><% query.match.files %></b> files matching your query !
6463
<% END %>
65-
64+
6665
<% END # END of search_in_progress %>
6766
<% html_time_elapsed %>
6867
</div>

views/index.tt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
<input type="hidden" name="_bb" id="cache-buster" value="<% Math.int( Math.rand(999999999) ) %>">
77
<div class="form-group" style="overflow: hidden; display: table;">
88
<!-- FIXME to move to css -->
9-
<input type="text" name="q" size="35" autofocus="autofocus" id="search-input" class="form-control home-search-input" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" placeholder="search with Perl Compatible RegEx">
10-
<br><input type="text" name="qd" size="35" id="xsearch-input" class="form-control home-search-distro-input optional-input" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" placeholder="in distro like ^App-.*">
11-
<input type="text" name="qft" size="6" id="xysearch-input" class="form-control home-search-filetype-input optional-input" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" placeholder="*.pm">
9+
<input type="text" name="q" size="35" autofocus="autofocus" id="search-input" class="form-control home-search-input" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" placeholder="search with Perl Compatible RegEx">
10+
<br>
11+
<input type="text" name="qd" size="35" id="xsearch-input" class="form-control home-search-distro-input optional-input" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" placeholder="in distro like ^App-.*">
12+
<input type="text" name="qft" size="6" id="xysearch-input" class="form-control home-search-filetype-input optional-input" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" placeholder="*.pm">
13+
<br>
14+
<input type="text" name="qifl" size="25" id="ignore-files-input" class="form-control home-search-ignore-files optional-input" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" placeholder="ignore files like: *.t, ppport.h, ...">
15+
<br>
1216
<!-- .class + .class { width: 100px } -->
1317
<div style="text-align:left; font-size: 14px;">
1418
<label for="ci-input" style="font-weight: normal;"><input type="checkbox" name="qci" id="ci-input">&nbsp;case&nbsp;insensitive&nbsp;search</label>

views/show-ls.tt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<% FOREACH item IN query.results %>
22
<!-- beginning of module -->
33

4-
<%
5-
l2d_qci = qci | html_entity;
6-
l2d_q = search | html_entity;
7-
l2d_qd = item.distro | html_entity;
8-
l2d_qft = qft | html_entity;
9-
l2d_qls = qls | html_entity;
10-
11-
SET link_to_distro = "/search?qci=${l2d_qci}&amp;q=${l2d_q}&amp;qft=${l2d_qft}&amp;qd=${l2d_qd}&amp;qls=${l2d_qls}";
12-
SET link_withmatch = "/search?qci=${l2d_qci}&amp;q=${l2d_q}&amp;qft=${l2d_qft}&amp;qd=${l2d_qd}&amp;qls=0";
4+
<%
5+
l2d_qci = qci | html_entity;
6+
l2d_q = search | html_entity;
7+
l2d_qd = item.distro | html_entity;
8+
l2d_qft = qft | html_entity;
9+
l2d_qls = qls | html_entity;
10+
l2d_qifl = qifl | html_entity;
11+
12+
SET link_to_distro = "/search?qci=${l2d_qci}&amp;q=${l2d_q}&amp;qft=${l2d_qft}&amp;qd=${l2d_qd}&amp;qls=${l2d_qls}&amp;qifl=${l2d_qifl}";
13+
SET link_withmatch = "/search?qci=${l2d_qci}&amp;q=${l2d_q}&amp;qft=${l2d_qft}&amp;qd=${l2d_qd}&amp;qls=0&amp;qifl=${l2d_qifl}";
1314
%>
1415
<div class="module-result">
1516
<big><strong>
@@ -19,7 +20,7 @@
1920
<a href="<% link_to_distro %>" class="favorite highlight">
2021
<span><%= item.files.size %></span> <%= item.files.size <= 1 ? 'match' : 'match' %></a>
2122
</div>
22-
23+
2324
<br>
2425
<p class="description">
2526

@@ -28,11 +29,11 @@
2829
<% FOREACH match IN item.matches %>
2930
<p class="description">
3031
<a class="author" href="<% link_withmatch %>&amp;f=<% match.file | html_entity %>" title="grep on this file"><% match.file %></a>
31-
</p>
32+
</p>
3233
<% END %>
3334

3435
<% END # fi %>
3536

3637
</div>
3738

38-
<% END # foreach query.results %>
39+
<% END # foreach query.results %>

views/show-search.tt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
l2d_q = search | uri;
66
l2d_qd = item.distro | uri;
77
l2d_qft = qft | uri;
8+
l2d_qifl = qifl | html_entity;
89

9-
SET link_to_distro = "/search?qci=${l2d_qci}&amp;q=${l2d_q}&amp;qft=${l2d_qft}&amp;qd=${l2d_qd}";
10+
SET link_to_distro = "/search?qci=${l2d_qci}&amp;q=${l2d_q}&amp;qft=${l2d_qft}&amp;qd=${l2d_qd}&amp;=l2d_qifl=${l2d_qifl}";
1011
%>
1112
<div class="module-result">
1213
<big><strong>
@@ -53,4 +54,4 @@
5354
</div>
5455
<br>
5556

56-
<% END # foreach query.results %>
57+
<% END # foreach query.results %>

0 commit comments

Comments
 (0)