gitweb: Make git_search_* subroutines render whole pages

Move git_header_html() and git_footer_html() invocation from git_search() to individual git_search_* subroutines. While at it, reorganize search-related code a bit, moving invoking of git commands before any output is generated. This has the following advantages: * gitweb now shows an error page if there was unknown search type (evaluate_and_validate_params checks only that it looks sanely); remember that we shouldn't call die_error after any output. * git_search_message is now safe agains die_error in parse_commits (though this is very unlikely). * gitweb now can check errors while invoking git commands and show error page (again, quite unlikely). Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jakub Narebski committed Jun 22, 2011 at 17:28 UTC 1ae05be4aa448163a381e131396fd52dcf6f83eb
1 file changed +26 -12
gitweb/gitweb.perl
+26 -12
@@ -5291,6 +5291,8 @@ sub git_search_message {
5291 $paging_nav .= " &sdot; next";
5292 }
5293
5294 + git_header_html();
5295 +
5296 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5297 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5298 if ($page == 0 && !@commitlist) {
@@ -5298,20 +5300,26 @@ sub git_search_message {
5300 } else {
5301 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5302 }
5303 +
5304 + git_footer_html();
5305 }
5306
5307 sub git_search_changes {
5308 my %co = @_;
5309
5310 + local $/ = "\n";
5311 + open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5312 + '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5313 + ($search_use_regexp ? '--pickaxe-regex' : ())
5314 + or die_error(500, "Open git-log failed");
5315 +
5316 + git_header_html();
5317 +
5318 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5319 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5320
5321 print "<table class=\"pickaxe search\">\n";
5322 my $alternate = 1;
5311 - local $/ = "\n";
5312 - open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5313 - '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5314 - ($search_use_regexp ? '--pickaxe-regex' : ());
5323 undef %co;
5324 my @files;
5325 while (my $line = <$fd>) {
@@ -5375,21 +5383,27 @@ sub git_search_changes {
5383 }
5384
5385 print "</table>\n";
5386 +
5387 + git_footer_html();
5388 }
5389
5390 sub git_search_files {
5391 my %co = @_;
5392
5393 + local $/ = "\n";
5394 + open my $fd, "-|", git_cmd(), 'grep', '-n',
5395 + $search_use_regexp ? ('-E', '-i') : '-F',
5396 + $searchtext, $co{'tree'}
5397 + or die_error(500, "Open git-grep failed");
5398 +
5399 + git_header_html();
5400 +
5401 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5402 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5403
5404 print "<table class=\"grep_search\">\n";
5405 my $alternate = 1;
5406 my $matches = 0;
5389 - local $/ = "\n";
5390 - open my $fd, "-|", git_cmd(), 'grep', '-n',
5391 - $search_use_regexp ? ('-E', '-i') : '-F',
5392 - $searchtext, $co{'tree'};
5407 my $lastfile = '';
5408 while (my $line = <$fd>) {
5409 chomp $line;
@@ -5446,6 +5460,8 @@ sub git_search_files {
5460 close $fd;
5461
5462 print "</table>\n";
5463 +
5464 + git_footer_html();
5465 }
5466
5467 sub git_search_grep_body {
@@ -7016,8 +7032,6 @@ sub git_search {
7032 $page = 0;
7033 }
7034
7019 - git_header_html();
7020 -
7035 if ($searchtype eq 'commit' ||
7036 $searchtype eq 'author' ||
7037 $searchtype eq 'committer') {
@@ -7026,9 +7040,9 @@ sub git_search {
7040 git_search_changes(%co);
7041 } elsif ($searchtype eq 'grep') {
7042 git_search_files(%co);
7043 + } else {
7044 + die_error(400, "Unknown search type");
7045 }
7030 -
7031 - git_footer_html();
7046 }
7047
7048 sub git_search_help {