gitweb: Split body of git_search into subroutines

Create separate subroutines for handling each of aspects of searching the repository: * git_search_message ('commit', 'author', 'committer') * git_search_changes ('pickaxe') * git_search_content_of_files ('grep') Almost pure code movement (and unindent), which you can check e.g. via $ git blame -w --date=short -C -C HEAD^..HEAD -- gitweb/gitweb.perl | grep -C 3 -e '^[^^]' | less -S No functional changes intended. 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 16f20725bbcb7abbbc11e740050197b7fe8f843a
1 file changed +199 -182
gitweb/gitweb.perl
+199 -182
@@ -5254,6 +5254,197 @@ sub git_remotes_body {
5254 }
5255 }
5256
5257 +sub git_search_message {
5258 + my %co = @_;
5259 +
5260 + my $greptype;
5261 + if ($searchtype eq 'commit') {
5262 + $greptype = "--grep=";
5263 + } elsif ($searchtype eq 'author') {
5264 + $greptype = "--author=";
5265 + } elsif ($searchtype eq 'committer') {
5266 + $greptype = "--committer=";
5267 + }
5268 + $greptype .= $searchtext;
5269 + my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5270 + $greptype, '--regexp-ignore-case',
5271 + $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5272 +
5273 + my $paging_nav = '';
5274 + if ($page > 0) {
5275 + $paging_nav .=
5276 + $cgi->a({-href => href(action=>"search", hash=>$hash,
5277 + searchtext=>$searchtext,
5278 + searchtype=>$searchtype)},
5279 + "first");
5280 + $paging_nav .= " &sdot; " .
5281 + $cgi->a({-href => href(-replay=>1, page=>$page-1),
5282 + -accesskey => "p", -title => "Alt-p"}, "prev");
5283 + } else {
5284 + $paging_nav .= "first";
5285 + $paging_nav .= " &sdot; prev";
5286 + }
5287 + my $next_link = '';
5288 + if ($#commitlist >= 100) {
5289 + $next_link =
5290 + $cgi->a({-href => href(-replay=>1, page=>$page+1),
5291 + -accesskey => "n", -title => "Alt-n"}, "next");
5292 + $paging_nav .= " &sdot; $next_link";
5293 + } else {
5294 + $paging_nav .= " &sdot; next";
5295 + }
5296 +
5297 + git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5298 + git_print_header_div('commit', esc_html($co{'title'}), $hash);
5299 + if ($page == 0 && !@commitlist) {
5300 + print "<p>No match.</p>\n";
5301 + } else {
5302 + git_search_grep_body(\@commitlist, 0, 99, $next_link);
5303 + }
5304 +}
5305 +
5306 +sub git_search_changes {
5307 + my %co = @_;
5308 +
5309 + git_print_page_nav('','', $hash,$co{'tree'},$hash);
5310 + git_print_header_div('commit', esc_html($co{'title'}), $hash);
5311 +
5312 + print "<table class=\"pickaxe search\">\n";
5313 + my $alternate = 1;
5314 + local $/ = "\n";
5315 + open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5316 + '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5317 + ($search_use_regexp ? '--pickaxe-regex' : ());
5318 + undef %co;
5319 + my @files;
5320 + while (my $line = <$fd>) {
5321 + chomp $line;
5322 + next unless $line;
5323 +
5324 + my %set = parse_difftree_raw_line($line);
5325 + if (defined $set{'commit'}) {
5326 + # finish previous commit
5327 + if (%co) {
5328 + print "</td>\n" .
5329 + "<td class=\"link\">" .
5330 + $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5331 + " | " .
5332 + $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5333 + print "</td>\n" .
5334 + "</tr>\n";
5335 + }
5336 +
5337 + if ($alternate) {
5338 + print "<tr class=\"dark\">\n";
5339 + } else {
5340 + print "<tr class=\"light\">\n";
5341 + }
5342 + $alternate ^= 1;
5343 + %co = parse_commit($set{'commit'});
5344 + my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5345 + print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5346 + "<td><i>$author</i></td>\n" .
5347 + "<td>" .
5348 + $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5349 + -class => "list subject"},
5350 + chop_and_escape_str($co{'title'}, 50) . "<br/>");
5351 + } elsif (defined $set{'to_id'}) {
5352 + next if ($set{'to_id'} =~ m/^0{40}$/);
5353 +
5354 + print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5355 + hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5356 + -class => "list"},
5357 + "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5358 + "<br/>\n";
5359 + }
5360 + }
5361 + close $fd;
5362 +
5363 + # finish last commit (warning: repetition!)
5364 + if (%co) {
5365 + print "</td>\n" .
5366 + "<td class=\"link\">" .
5367 + $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5368 + " | " .
5369 + $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5370 + print "</td>\n" .
5371 + "</tr>\n";
5372 + }
5373 +
5374 + print "</table>\n";
5375 +}
5376 +
5377 +sub git_search_files {
5378 + my %co = @_;
5379 +
5380 + git_print_page_nav('','', $hash,$co{'tree'},$hash);
5381 + git_print_header_div('commit', esc_html($co{'title'}), $hash);
5382 +
5383 + print "<table class=\"grep_search\">\n";
5384 + my $alternate = 1;
5385 + my $matches = 0;
5386 + local $/ = "\n";
5387 + open my $fd, "-|", git_cmd(), 'grep', '-n',
5388 + $search_use_regexp ? ('-E', '-i') : '-F',
5389 + $searchtext, $co{'tree'};
5390 + my $lastfile = '';
5391 + while (my $line = <$fd>) {
5392 + chomp $line;
5393 + my ($file, $lno, $ltext, $binary);
5394 + last if ($matches++ > 1000);
5395 + if ($line =~ /^Binary file (.+) matches$/) {
5396 + $file = $1;
5397 + $binary = 1;
5398 + } else {
5399 + (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5400 + }
5401 + if ($file ne $lastfile) {
5402 + $lastfile and print "</td></tr>\n";
5403 + if ($alternate++) {
5404 + print "<tr class=\"dark\">\n";
5405 + } else {
5406 + print "<tr class=\"light\">\n";
5407 + }
5408 + print "<td class=\"list\">".
5409 + $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5410 + file_name=>"$file"),
5411 + -class => "list"}, esc_path($file));
5412 + print "</td><td>\n";
5413 + $lastfile = $file;
5414 + }
5415 + if ($binary) {
5416 + print "<div class=\"binary\">Binary file</div>\n";
5417 + } else {
5418 + $ltext = untabify($ltext);
5419 + if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5420 + $ltext = esc_html($1, -nbsp=>1);
5421 + $ltext .= '<span class="match">';
5422 + $ltext .= esc_html($2, -nbsp=>1);
5423 + $ltext .= '</span>';
5424 + $ltext .= esc_html($3, -nbsp=>1);
5425 + } else {
5426 + $ltext = esc_html($ltext, -nbsp=>1);
5427 + }
5428 + print "<div class=\"pre\">" .
5429 + $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5430 + file_name=>"$file").'#l'.$lno,
5431 + -class => "linenr"}, sprintf('%4i', $lno))
5432 + . ' ' . $ltext . "</div>\n";
5433 + }
5434 + }
5435 + if ($lastfile) {
5436 + print "</td></tr>\n";
5437 + if ($matches > 1000) {
5438 + print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5439 + }
5440 + } else {
5441 + print "<div class=\"diff nodifferences\">No matches found</div>\n";
5442 + }
5443 + close $fd;
5444 +
5445 + print "</table>\n";
5446 +}
5447 +
5448 sub git_search_grep_body {
5449 my ($commitlist, $from, $to, $extra) = @_;
5450 $from = 0 unless defined $from;
@@ -6824,190 +7015,16 @@ sub git_search {
7015
7016 git_header_html();
7017
6827 - if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
6828 - my $greptype;
6829 - if ($searchtype eq 'commit') {
6830 - $greptype = "--grep=";
6831 - } elsif ($searchtype eq 'author') {
6832 - $greptype = "--author=";
6833 - } elsif ($searchtype eq 'committer') {
6834 - $greptype = "--committer=";
6835 - }
6836 - $greptype .= $searchtext;
6837 - my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6838 - $greptype, '--regexp-ignore-case',
6839 - $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6840 -
6841 - my $paging_nav = '';
6842 - if ($page > 0) {
6843 - $paging_nav .=
6844 - $cgi->a({-href => href(action=>"search", hash=>$hash,
6845 - searchtext=>$searchtext,
6846 - searchtype=>$searchtype)},
6847 - "first");
6848 - $paging_nav .= " &sdot; " .
6849 - $cgi->a({-href => href(-replay=>1, page=>$page-1),
6850 - -accesskey => "p", -title => "Alt-p"}, "prev");
6851 - } else {
6852 - $paging_nav .= "first";
6853 - $paging_nav .= " &sdot; prev";
6854 - }
6855 - my $next_link = '';
6856 - if ($#commitlist >= 100) {
6857 - $next_link =
6858 - $cgi->a({-href => href(-replay=>1, page=>$page+1),
6859 - -accesskey => "n", -title => "Alt-n"}, "next");
6860 - $paging_nav .= " &sdot; $next_link";
6861 - } else {
6862 - $paging_nav .= " &sdot; next";
6863 - }
6864 -
6865 - git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6866 - git_print_header_div('commit', esc_html($co{'title'}), $hash);
6867 - if ($page == 0 && !@commitlist) {
6868 - print "<p>No match.</p>\n";
6869 - } else {
6870 - git_search_grep_body(\@commitlist, 0, 99, $next_link);
6871 - }
7018 + if ($searchtype eq 'commit' ||
7019 + $searchtype eq 'author' ||
7020 + $searchtype eq 'committer') {
7021 + git_search_message(%co);
7022 + } elsif ($searchtype eq 'pickaxe') {
7023 + git_search_changes(%co);
7024 + } elsif ($searchtype eq 'grep') {
7025 + git_search_files(%co);
7026 }
7027
6874 - if ($searchtype eq 'pickaxe') {
6875 - git_print_page_nav('','', $hash,$co{'tree'},$hash);
6876 - git_print_header_div('commit', esc_html($co{'title'}), $hash);
6877 -
6878 - print "<table class=\"pickaxe search\">\n";
6879 - my $alternate = 1;
6880 - local $/ = "\n";
6881 - open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6882 - '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6883 - ($search_use_regexp ? '--pickaxe-regex' : ());
6884 - undef %co;
6885 - my @files;
6886 - while (my $line = <$fd>) {
6887 - chomp $line;
6888 - next unless $line;
6889 -
6890 - my %set = parse_difftree_raw_line($line);
6891 - if (defined $set{'commit'}) {
6892 - # finish previous commit
6893 - if (%co) {
6894 - print "</td>\n" .
6895 - "<td class=\"link\">" .
6896 - $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6897 - " | " .
6898 - $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6899 - print "</td>\n" .
6900 - "</tr>\n";
6901 - }
6902 -
6903 - if ($alternate) {
6904 - print "<tr class=\"dark\">\n";
6905 - } else {
6906 - print "<tr class=\"light\">\n";
6907 - }
6908 - $alternate ^= 1;
6909 - %co = parse_commit($set{'commit'});
6910 - my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6911 - print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6912 - "<td><i>$author</i></td>\n" .
6913 - "<td>" .
6914 - $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6915 - -class => "list subject"},
6916 - chop_and_escape_str($co{'title'}, 50) . "<br/>");
6917 - } elsif (defined $set{'to_id'}) {
6918 - next if ($set{'to_id'} =~ m/^0{40}$/);
6919 -
6920 - print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6921 - hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6922 - -class => "list"},
6923 - "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6924 - "<br/>\n";
6925 - }
6926 - }
6927 - close $fd;
6928 -
6929 - # finish last commit (warning: repetition!)
6930 - if (%co) {
6931 - print "</td>\n" .
6932 - "<td class=\"link\">" .
6933 - $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6934 - " | " .
6935 - $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6936 - print "</td>\n" .
6937 - "</tr>\n";
6938 - }
6939 -
6940 - print "</table>\n";
6941 - }
6942 -
6943 - if ($searchtype eq 'grep') {
6944 - git_print_page_nav('','', $hash,$co{'tree'},$hash);
6945 - git_print_header_div('commit', esc_html($co{'title'}), $hash);
6946 -
6947 - print "<table class=\"grep_search\">\n";
6948 - my $alternate = 1;
6949 - my $matches = 0;
6950 - local $/ = "\n";
6951 - open my $fd, "-|", git_cmd(), 'grep', '-n',
6952 - $search_use_regexp ? ('-E', '-i') : '-F',
6953 - $searchtext, $co{'tree'};
6954 - my $lastfile = '';
6955 - while (my $line = <$fd>) {
6956 - chomp $line;
6957 - my ($file, $lno, $ltext, $binary);
6958 - last if ($matches++ > 1000);
6959 - if ($line =~ /^Binary file (.+) matches$/) {
6960 - $file = $1;
6961 - $binary = 1;
6962 - } else {
6963 - (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
6964 - }
6965 - if ($file ne $lastfile) {
6966 - $lastfile and print "</td></tr>\n";
6967 - if ($alternate++) {
6968 - print "<tr class=\"dark\">\n";
6969 - } else {
6970 - print "<tr class=\"light\">\n";
6971 - }
6972 - print "<td class=\"list\">".
6973 - $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6974 - file_name=>"$file"),
6975 - -class => "list"}, esc_path($file));
6976 - print "</td><td>\n";
6977 - $lastfile = $file;
6978 - }
6979 - if ($binary) {
6980 - print "<div class=\"binary\">Binary file</div>\n";
6981 - } else {
6982 - $ltext = untabify($ltext);
6983 - if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6984 - $ltext = esc_html($1, -nbsp=>1);
6985 - $ltext .= '<span class="match">';
6986 - $ltext .= esc_html($2, -nbsp=>1);
6987 - $ltext .= '</span>';
6988 - $ltext .= esc_html($3, -nbsp=>1);
6989 - } else {
6990 - $ltext = esc_html($ltext, -nbsp=>1);
6991 - }
6992 - print "<div class=\"pre\">" .
6993 - $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6994 - file_name=>"$file").'#l'.$lno,
6995 - -class => "linenr"}, sprintf('%4i', $lno))
6996 - . ' ' . $ltext . "</div>\n";
6997 - }
6998 - }
6999 - if ($lastfile) {
7000 - print "</td></tr>\n";
7001 - if ($matches > 1000) {
7002 - print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
7003 - }
7004 - } else {
7005 - print "<div class=\"diff nodifferences\">No matches found</div>\n";
7006 - }
7007 - close $fd;
7008 -
7009 - print "</table>\n";
7010 - }
7028 git_footer_html();
7029 }
7030