@matheusbsbs / matheus-bagliano / commits / e773855372
Fork 0

gitweb: Add support for grep searches

The 'grep' type of search greps the currently selected tree for given regexp and shows the results in a fancy table with links into blob view. The number of shown matches is limited to 1000 and the whole feature can be turned off (grepping linux-2.6.git already makes repo.or.cz a bit unhappy). This second revision makes it in documentation explicit that grep accepts regexps, and makes grep accept extended regexps instead of basic regexps. Signed-off-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <junkio@cox.net>

Petr Baudis committed May 17, 2007 at 04:31 UTC e7738553723dcff0237b1aed2771886c8d87d224
2 files changed +125 -2
gitweb/gitweb.css
+4
@@ -484,3 +484,7 @@ span.atnight {
484 span.match {
485 color: #e00000;
486 }
487 +
488 +div.binary {
489 + font-style: italic;
490 +}
gitweb/gitweb.perl
+121 -2
@@ -146,6 +146,19 @@ our %feature = (
146 'override' => 0,
147 'default' => [1]},
148
149 + # Enable grep search, which will list the files in currently selected
150 + # tree containing the given string. Enabled by default. This can be
151 + # potentially CPU-intensive, of course.
152 +
153 + # To enable system wide have in $GITWEB_CONFIG
154 + # $feature{'grep'}{'default'} = [1];
155 + # To have project specific config enable override in $GITWEB_CONFIG
156 + # $feature{'grep'}{'override'} = 1;
157 + # and in project config gitweb.grep = 0|1;
158 + 'grep' => {
159 + 'override' => 0,
160 + 'default' => [1]},
161 +
162 # Enable the pickaxe search, which will list the commits that modified
163 # a given string in a file. This can be practical and quite faster
164 # alternative to 'blame', but still potentially CPU-intensive.
@@ -245,6 +258,18 @@ sub gitweb_have_snapshot {
258 return $have_snapshot;
259 }
260
261 +sub feature_grep {
262 + my ($val) = git_get_project_config('grep', '--bool');
263 +
264 + if ($val eq 'true') {
265 + return (1);
266 + } elsif ($val eq 'false') {
267 + return (0);
268 + }
269 +
270 + return ($_[0]);
271 +}
272 +
273 sub feature_pickaxe {
274 my ($val) = git_get_project_config('pickaxe', '--bool');
275
@@ -364,10 +389,17 @@ if (defined $page) {
389 }
390 }
391
392 +our $searchtype = $cgi->param('st');
393 +if (defined $searchtype) {
394 + if ($searchtype =~ m/[^a-z]/) {
395 + die_error(undef, "Invalid searchtype parameter");
396 + }
397 +}
398 +
399 our $searchtext = $cgi->param('s');
400 our $search_regexp;
401 if (defined $searchtext) {
370 - if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
402 + if ($searchtype ne 'grep' and $searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
403 die_error(undef, "Invalid search parameter");
404 }
405 if (length($searchtext) < 2) {
@@ -1927,7 +1959,7 @@ EOF
1959 $cgi->hidden(-name => "a") . "\n" .
1960 $cgi->hidden(-name => "h") . "\n" .
1961 $cgi->popup_menu(-name => 'st', -default => 'commit',
1930 - -values => ['commit', 'author', 'committer', 'pickaxe']) .
1962 + -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
1963 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
1964 " search:\n",
1965 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
@@ -4626,6 +4658,12 @@ sub git_search {
4658 die_error('403 Permission denied', "Permission denied");
4659 }
4660 }
4661 + if ($searchtype eq 'grep') {
4662 + my ($have_grep) = gitweb_check_feature('grep');
4663 + if (!$have_grep) {
4664 + die_error('403 Permission denied', "Permission denied");
4665 + }
4666 + }
4667
4668 git_header_html();
4669
@@ -4742,6 +4780,73 @@ sub git_search {
4780
4781 print "</table>\n";
4782 }
4783 +
4784 + if ($searchtype eq 'grep') {
4785 + git_print_page_nav('','', $hash,$co{'tree'},$hash);
4786 + git_print_header_div('commit', esc_html($co{'title'}), $hash);
4787 +
4788 + print "<table cellspacing=\"0\">\n";
4789 + my $alternate = 1;
4790 + my $matches = 0;
4791 + $/ = "\n";
4792 + open my $fd, "-|", git_cmd(), 'grep', '-n', '-i', '-E', $searchtext, $co{'tree'};
4793 + my $lastfile = '';
4794 + while (my $line = <$fd>) {
4795 + chomp $line;
4796 + my ($file, $lno, $ltext, $binary);
4797 + last if ($matches++ > 1000);
4798 + if ($line =~ /^Binary file (.+) matches$/) {
4799 + $file = $1;
4800 + $binary = 1;
4801 + } else {
4802 + (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
4803 + }
4804 + if ($file ne $lastfile) {
4805 + $lastfile and print "</td></tr>\n";
4806 + if ($alternate++) {
4807 + print "<tr class=\"dark\">\n";
4808 + } else {
4809 + print "<tr class=\"light\">\n";
4810 + }
4811 + print "<td class=\"list\">".
4812 + $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
4813 + file_name=>"$file"),
4814 + -class => "list"}, esc_path($file));
4815 + print "</td><td>\n";
4816 + $lastfile = $file;
4817 + }
4818 + if ($binary) {
4819 + print "<div class=\"binary\">Binary file</div>\n";
4820 + } else {
4821 + $ltext = untabify($ltext);
4822 + if ($ltext =~ m/^(.*)($searchtext)(.*)$/i) {
4823 + $ltext = esc_html($1, -nbsp=>1);
4824 + $ltext .= '<span class="match">';
4825 + $ltext .= esc_html($2, -nbsp=>1);
4826 + $ltext .= '</span>';
4827 + $ltext .= esc_html($3, -nbsp=>1);
4828 + } else {
4829 + $ltext = esc_html($ltext, -nbsp=>1);
4830 + }
4831 + print "<div class=\"pre\">" .
4832 + $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
4833 + file_name=>"$file").'#l'.$lno,
4834 + -class => "linenr"}, sprintf('%4i', $lno))
4835 + . ' ' . $ltext . "</div>\n";
4836 + }
4837 + }
4838 + if ($lastfile) {
4839 + print "</td></tr>\n";
4840 + if ($matches > 1000) {
4841 + print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
4842 + }
4843 + } else {
4844 + print "<div class=\"diff nodifferences\">No matches found</div>\n";
4845 + }
4846 + close $fd;
4847 +
4848 + print "</table>\n";
4849 + }
4850 git_footer_html();
4851 }
4852
@@ -4752,6 +4857,20 @@ sub git_search_help {
4857 <dl>
4858 <dt><b>commit</b></dt>
4859 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
4860 +EOT
4861 + my ($have_grep) = gitweb_check_feature('grep');
4862 + if ($have_grep) {
4863 + print <<EOT;
4864 +<dt><b>grep</b></dt>
4865 +<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
4866 + a different one) are searched for the given
4867 +<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>
4868 +(POSIX extended) and the matches are listed. On large
4869 +trees, this search can take a while and put some strain on the server, so please use it with
4870 +some consideration.</dd>
4871 +EOT
4872 + }
4873 + print <<EOT;
4874 <dt><b>author</b></dt>
4875 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
4876 <dt><b>committer</b></dt>