threads / patch / 13886

patch, 2 partsgitweb: Separate generating 'sort by' table header

Subject: [PATCH 2/2] gitweb: Separate generating 'sort by' table header

## tl;dr

3 messages between Jun 10, 2008 and Jun 10, 2008. Diffs are folded; open one to read it.

replies: 2people: 1as markdown or json

Jakub Narebski· Jun 10, 2008, 17:19 UTC · lore

[PATCH 0/2] gitweb: Refactor projects_list generation

These two patches make code generating 'projects_list" view better. This is resend of those patches, this time without following patches which add new features like allowing 'description' field in $projects_list file, searching for projects, and paginating projects list page.

Those two patches should also make it easier for Lea to plug-in caching in gitweb. That's why I'm sending them even in the rc/freeze period...

Shortlog:
=========
Jakub Narebski (2):
  gitweb: Separate filling list of projects info
  gitweb: Separate generating 'sort by' table header
Diffstat:
=========
 gitweb/gitweb.perl |  111 +++++++++++++++++++++++++++++-----------------------
 1 files changed, 62 insertions(+), 49 deletions(-)
-- 
Jakub Narebski
ShadeHawk on #git
Jakub Narebski· Jun 10, 2008, 17:21 UTC · re: Jakub Narebski · lore

[PATCH 1/2] gitweb: Separate filling list of projects info

Extract filling project list info, i.e. adding age, description, owner and forks information, into fill_project_list_info() subroutine. This is preparation for smart pagination and smart searching (to make it possible to calculate/generate info only for those projects which will be shown).

Small changes compared to original version to improve readability (comments, names of variables, named loops).

Additionally, store both full ('descr_long') and shortened ('descr') project description in Perl's internal form (using to_utf8).

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
It is meant to be minimally intrusive patch, although it corrects
some code (variable naming, ensuring storing data in Perl internal
form, comments, etc.).
 gitweb/gitweb.perl |   35 +++++++++++++++++++++++------------
 1 files changed, 23 insertions(+), 12 deletions(-)
Show changes to gitweb/gitweb.perl +23 −12
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 198772c..d7a9809 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -3559,21 +3559,24 @@ sub git_patchset_body {
 
 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
 
-sub git_project_list_body {
-	my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
-
-	my ($check_forks) = gitweb_check_feature('forks');
-
+# fills project list info (age, description, owner, forks) for each
+# project in the list, removing invalid projects from returned list
+# NOTE: modifies $projlist, but does not remove entries from it
+sub fill_project_list_info {
+	my ($projlist, $check_forks) = @_;
 	my @projects;
+
+ PROJECT:
 	foreach my $pr (@$projlist) {
-		my (@aa) = git_get_last_activity($pr->{'path'});
-		unless (@aa) {
-			next;
+		my (@activity) = git_get_last_activity($pr->{'path'});
+		unless (@activity) {
+			next PROJECT;
 		}
-		($pr->{'age'}, $pr->{'age_string'}) = @aa;
+		($pr->{'age'}, $pr->{'age_string'}) = @activity;
 		if (!defined $pr->{'descr'}) {
 			my $descr = git_get_project_description($pr->{'path'}) || "";
-			$pr->{'descr_long'} = to_utf8($descr);
+			$descr = to_utf8($descr);
+			$pr->{'descr_long'} = $descr;
 			$pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
 		}
 		if (!defined $pr->{'owner'}) {
@@ -3585,14 +3588,22 @@ sub git_project_list_body {
 			    ($pname !~ /\/$/) &&
 			    (-d "$projectroot/$pname")) {
 				$pr->{'forks'} = "-d $projectroot/$pname";
-			}
-			else {
+			}	else {
 				$pr->{'forks'} = 0;
 			}
 		}
 		push @projects, $pr;
 	}
 
+	return @projects;
+}
+
+sub git_project_list_body {
+	my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
+
+	my ($check_forks) = gitweb_check_feature('forks');
+	my @projects = fill_project_list_info($projlist, $check_forks);
+
 	$order ||= $default_projects_order;
 	$from = 0 unless defined $from;
 	$to = $#projects if (!defined $to || $#projects < $to);
-- 
1.5.5.3
Jakub Narebski· Jun 10, 2008, 17:21 UTC · re: Jakub Narebski · lore

Extract generating table header cell, for tables which can be sorted by its columns, into print_sort_th_str() and print_sort_th_num() subroutines, and print_sort_th() driver subroutine.

This avoids repetition, and should make further improvements (like JavaScript sorting) easier. The subroutine uses now "replay" link, so it is generic enough to be able to use it for other tables which can be sorted by column, like for example 'heads' and 'tags' view (sort by name, or sort by age).

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
For further code reusing, and current code clarity.
 gitweb/gitweb.perl |   76 ++++++++++++++++++++++++++-------------------------
 1 files changed, 39 insertions(+), 37 deletions(-)
Show changes to gitweb/gitweb.perl +39 −37
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index d7a9809..c7882f2 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -3598,6 +3598,36 @@ sub fill_project_list_info {
 	return @projects;
 }
 
+# print 'sort by' <th> element, either sorting by $key if $name eq $order
+# (changing $list), or generating 'sort by $name' replay link otherwise
+sub print_sort_th {
+	my ($str_sort, $name, $order, $key, $header, $list) = @_;
+	$key    ||= $name;
+	$header ||= ucfirst($name);
+
+	if ($order eq $name) {
+		if ($str_sort) {
+			@$list = sort {$a->{$key} cmp $b->{$key}} @$list;
+		} else {
+			@$list = sort {$a->{$key} <=> $b->{$key}} @$list;
+		}
+		print "<th>$header</th>\n";
+	} else {
+		print "<th>" .
+		      $cgi->a({-href => href(-replay=>1, order=>$name),
+		               -class => "header"}, $header) .
+		      "</th>\n";
+	}
+}
+
+sub print_sort_th_str {
+	print_sort_th(1, @_);
+}
+
+sub print_sort_th_num {
+	print_sort_th(0, @_);
+}
+
 sub git_project_list_body {
 	my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
 
@@ -3614,43 +3644,15 @@ sub git_project_list_body {
 		if ($check_forks) {
 			print "<th></th>\n";
 		}
-		if ($order eq "project") {
-			@projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
-			print "<th>Project</th>\n";
-		} else {
-			print "<th>" .
-			      $cgi->a({-href => href(project=>undef, order=>'project'),
-			               -class => "header"}, "Project") .
-			      "</th>\n";
-		}
-		if ($order eq "descr") {
-			@projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
-			print "<th>Description</th>\n";
-		} else {
-			print "<th>" .
-			      $cgi->a({-href => href(project=>undef, order=>'descr'),
-			               -class => "header"}, "Description") .
-			      "</th>\n";
-		}
-		if ($order eq "owner") {
-			@projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
-			print "<th>Owner</th>\n";
-		} else {
-			print "<th>" .
-			      $cgi->a({-href => href(project=>undef, order=>'owner'),
-			               -class => "header"}, "Owner") .
-			      "</th>\n";
-		}
-		if ($order eq "age") {
-			@projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
-			print "<th>Last Change</th>\n";
-		} else {
-			print "<th>" .
-			      $cgi->a({-href => href(project=>undef, order=>'age'),
-			               -class => "header"}, "Last Change") .
-			      "</th>\n";
-		}
-		print "<th></th>\n" .
+		print_sort_th_str('project', $order, 'path',
+		                  'Project', \@projects);
+		print_sort_th_str('descr', $order, 'descr_long',
+		                  'Description', \@projects);
+		print_sort_th_str('owner', $order, 'owner',
+		                  'Owner', \@projects);
+		print_sort_th_num('age', $order, 'age',
+		                  'Last Change', \@projects);
+		print "<th></th>\n" . # for links
 		      "</tr>\n";
 	}
 	my $alternate = 1;
-- 
1.5.5.3

← back to recent threads