git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC/PATCH] gitweb: Project search

From
Jakub Narebski <jnareb@gmail.com>
Date
May 9, 2008, 13:23 UTC
Message-ID
<200805091523.24591.jnareb@gmail.com>
In-Reply-To
<200805031103.14960.jnareb@gmail.com>

If the number of projects is greater than some number, currently for testing set to 1 (in production probably to 100), then projects listing is replaced by project search form. One can search by project name (project path relative to $projectroot), by project description, and by owner. There is also link to show all projects.

Before searching by some field the information we search for must be filled in. For this fill_project_list_info() was enhanced to take additional parameter which part of projects info to fill. This way we can limit doing expensive calculations (like running git-for-each-ref to get 'age' / "Last changed" info) only to projects which we will show as search results.

To make sorting by given column of project list / project search results, the 'sort by' links in print_sort_th() subroutine are generated as 'replay' links.

NOTE: currently match is _not_ highlighted in search results.
Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
This patch depends on earlier 
  "[RFC/PATCH] gitweb: Simplify git_project_list_body"
patch, and may (but should not) depend textually on
  "[RFC/PATCH] gitweb: Allow project description in project_index file"

As this is quick'n'dirty proof-of-concept patch the styling of project search form is done in HTML, instead of using CSS to do that.

This patch should reduce both server load and browser load for sites with very large number of projects.

NOTE 1: in production one would increase threshold above which project search is used instead of listing all projects.

NOTE 2: I think that for effective caching one would need to cache data, not final output (to J.H. and Lea Wiemann).

What do you think about this concept?
 gitweb/gitweb.perl |   92 ++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 79 insertions(+), 13 deletions(-)
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index b117364..7e1a9b4 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -472,7 +472,7 @@ if (defined $page) {
 
 our $searchtype = $cgi->param('st');
 if (defined $searchtype) {
-	if ($searchtype =~ m/[^a-z]/) {
+	if ($searchtype =~ m/[^a-z_]/) {
 		die_error(undef, "Invalid searchtype parameter");
 	}
 }
@@ -3589,29 +3589,61 @@ sub git_patchset_body {
 
 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
 
+sub git_project_search_form {
+	my ($projlist, $searchtype, $searchtext, $search_use_regexp);
+
+	$searchtype ||= 'descr';
+	print "\n<!-- START: project_list search form -->\n" .
+	      "<div style=\"text-align: center; margin-top: 20px;\">\n";
+	print $cgi->startform(-method => 'get', -action => $my_uri) .
+	      $cgi->hidden(-name => 'a', -value => 'project_list') . "\n" .
+	      $cgi->popup_menu(-name => 'st', -default => $searchtype,
+	                       -values => ['project', 'descr', 'owner'],
+	                       -labels => {'project' => 'Project',
+	                                   'descr' => 'Description',
+	                                   'owner' => 'Owner'}) . "\n" .
+	      $cgi->textfield(-name => 's', -value => $searchtext,
+	                      -size => 60) . "\n" .
+	      "<span title=\"Extended regular expression\">" .
+	      $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
+	                     -checked => $search_use_regexp) .
+	      "</span>\n" .
+	      $cgi->submit(-name => 'btnS', -value => 'Search') .
+	      $cgi->end_form() . "\n" .
+	      $cgi->a({-href => href(project => undef, searchtype => 'list_all')},
+	              'List all projects') . "\n";
+	print "</div>\n" .
+	      "<!-- END: project_list search form -->\n\n";
+}
+
 # fills project list info (age, description, owner, forks) for each
-# project in the list, removing invalid projects from returned list
+# project in the list, removing invalid projects from returned list,
+# or fill only specified info (removing only when filling 'age')
 sub fill_project_list_info {
-	my ($projlist, $check_forks) = @_;
+	my ($projlist, $check_forks, $fill_only) = @_;
 	my @projects;
 
  PROJECT:
 	foreach my $pr (@$projlist) {
-		my (@activity) = git_get_last_activity($pr->{'path'});
-		unless (@activity) {
-			next PROJECT;
-		}
-		($pr->{'age'}, $pr->{'age_string'}) = @activity;
-		if (!defined $pr->{'descr'}) {
+		if (!exists $pr->{'age'} &&
+		    (!defined $fill_only || $fill_only eq 'age')) {
+			my (@activity) = git_get_last_activity($pr->{'path'});
+			next PROJECT unless (@activity);
+			($pr->{'age'}, $pr->{'age_string'}) = @activity;
+		}
+		if (!defined $pr->{'descr'} &&
+		    (!defined $fill_only || $fill_only eq 'descr')) {
 			my $descr = git_get_project_description($pr->{'path'}) || "";
 			$descr = to_utf8($descr);
 			$pr->{'descr_long'} = $descr;
 			$pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
 		}
-		if (!defined $pr->{'owner'}) {
+		if (!defined $pr->{'owner'} &&
+		    (!defined $fill_only || $fill_only eq 'owner')) {
 			$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
 		}
-		if ($check_forks) {
+		if ($check_forks &&
+		    (!defined $fill_only || $fill_only eq 'forks')) {
 			my $pname = $pr->{'path'};
 			if (($pname =~ s/\.git$//) &&
 			    ($pname !~ /\/$/) &&
@@ -3627,6 +3659,25 @@ sub fill_project_list_info {
 	return @projects;
 }
 
+# show only projects which match what we search for
+sub filter_project_list {
+	my ($projlist, $searchtype, $search_regexp) = @_;
+	my %keyname = (
+		'project' => 'path',
+		'descr' => 'descr_long',
+		'owner' => 'owner',
+	);
+	my $key = $keyname{$searchtype};
+
+	# fill in the field we search on
+	@$projlist = fill_project_list_info($projlist, 0, $searchtype);
+	# filter projects list
+	@$projlist = grep
+		{	$_->{$key} =~ m/$search_regexp/i; } @$projlist;
+
+	return $projlist;
+}
+
 sub print_sort_th {
 	my ($str_sort, $name, $order, $key, $header, $projlist) = @_;
 	$key    ||= $name;
@@ -3641,7 +3692,7 @@ sub print_sort_th {
 		print "<th>$header</th>\n";
 	} else {
 		print "<th>" .
-		      $cgi->a({-href => href(project=>undef, order=>$name),
+		      $cgi->a({-href => href(-replay => 1, order=>$name),
 		               -class => "header"}, $header) .
 		      "</th>\n";
 	}
@@ -4005,6 +4056,10 @@ sub git_project_list {
 	if (defined $order && $order !~ m/none|project|descr|owner|age/) {
 		die_error(undef, "Unknown order parameter");
 	}
+	if (defined $searchtype &&
+	    $searchtype !~ m/^(project|descr|owner|list_all)$/x) {
+		die_error(undef, "Unknown searchtype parameter");
+	}
 
 	my @list = git_get_projects_list();
 	if (!@list) {
@@ -4019,7 +4074,18 @@ sub git_project_list {
 		close $fd;
 		print "</div>\n";
 	}
-	git_project_list_body(\@list, $order);
+	# 'defined $searchtype' serves as "was search performed" test
+	if (@list > 1 || defined $searchtype) {
+		git_project_search_form(\@list, $searchtype,
+		                        $searchtext, $search_use_regexp);
+		if (defined $searchtype) {
+			filter_project_list(\@list, $searchtype, $search_regexp)
+				unless ($searchtype eq 'list_all');
+			git_project_list_body(\@list, $order);
+		}
+	} else {
+		git_project_list_body(\@list, $order);
+	}
 	git_footer_html();
 }
 
-- 
1.5.5
Previous: Miklos VajnaNext: Jakub Narebski
Message 6 of 20 in “gitweb: Simplify git_project_list_body”
  1. gitweb: Simplify git_project_list_bodyJakub Narebski, May 1, 2008
  2. gitweb: Allow project description in project_index fileJakub Narebski, May 2, 2008
  3. Miklos VajnaMay 2, 2008
  4. gitweb: Allow project description in project_index fileJakub Narebski, May 3, 2008
  5. Miklos VajnaMay 4, 2008
  6. gitweb: Project searchJakub Narebski, May 9, 2008
  7. gitweb: Paginate project listJakub Narebski, May 10, 2008
  8. J.H.May 10, 2008
  9. Jakub NarebskiMay 10, 2008
  10. J.H.May 11, 2008
  11. Jakub NarebskiMay 11, 2008
  12. Jakub NarebskiMay 12, 2008
  13. Lars HjemliMay 12, 2008
  14. Jakub NarebskiMay 13, 2008
  15. Lars HjemliMay 13, 2008
  16. Jakub NarebskiMay 13, 2008
  17. Kristian HøgsbergMay 13, 2008
  18. Lars HjemliMay 13, 2008
  19. Jakub NarebskiMay 13, 2008
  20. Jakub NarebskiMay 14, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.