From f7ab660c15b5e3cb7fee035191e0cd5160bf8299 Mon Sep 17 00:00:00 2001
From: Kay Sievers <kay@mam.(none)>
Date: Wed, 10 Aug 2005 03:53:09 +0200
Subject: [PATCH] allow sorting of index page by project path, owner and age

---
 gitweb.cgi | 100 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 64 insertions(+), 36 deletions(-)

diff --git a/gitweb.cgi b/gitweb.cgi
index 973702f094e..b0b515efbb5 100755
--- a/gitweb.cgi
+++ b/gitweb.cgi
@@ -15,7 +15,7 @@ use CGI::Carp qw(fatalsToBrowser);
 use Fcntl ':mode';
 
 my $cgi = new CGI;
-my $version =		"237";
+my $version =		"238";
 my $my_url =		$cgi->url();
 my $my_uri =		$cgi->url(-absolute => 1);
 my $rss_link = "";
@@ -56,6 +56,14 @@ if (defined $action) {
 	}
 }
 
+my $order = $cgi->param('o');
+if (defined $order) {
+	if ($order =~ m/[^a-zA-Z0-9_]/) {
+		undef $order;
+		die_error(undef, "Invalid order parameter.");
+	}
+}
+
 my $project = $cgi->param('p');
 if (defined $project) {
 	if ($project =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
@@ -314,7 +322,7 @@ sub git_footer_html {
 		}
 		print $cgi->a({-href => "$my_uri?p=$project;a=rss", -class => "rss_logo"}, "RSS") . "\n";
 	} else {
-		print $cgi->a({-href => "$my_uri?a=opml", -class => "rss_logo"}, "RSS") . "\n";
+		print $cgi->a({-href => "$my_uri?a=opml", -class => "rss_logo"}, "OPML") . "\n";
 	}
 	print "</div>\n" .
 	      "</body>\n" .
@@ -753,9 +761,30 @@ sub git_read_projects {
 
 sub git_project_list {
 	my @list = git_read_projects();
+	my @projects;
 	if (!@list) {
 		die_error(undef, "No project found.");
 	}
+	foreach my $pr (@list) {
+		my $head = git_read_hash("$pr->{'path'}/HEAD");
+		if (!defined $head) {
+			next;
+		}
+		$ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
+		my %co = git_read_commit($head);
+		if (!%co) {
+			next;
+		}
+		$pr->{'commit'} = \%co;
+		if (!defined $pr->{'descr'}) {
+			my $descr = git_read_description($pr->{'path'}) || "";
+			$pr->{'descr'} = chop_str($descr, 25, 5);
+		}
+		if (!defined $pr->{'owner'}) {
+			$pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
+		}
+		push @projects, $pr;
+	}
 	git_header_html();
 	if (-f $home_text) {
 		print "<div class=\"index_include\">\n";
@@ -765,53 +794,52 @@ sub git_project_list {
 		print "</div>\n";
 	}
 	print "<table cellspacing=\"0\">\n" .
-	      "<tr>\n" .
-	      "<th>Project</th>\n" .
-	      "<th>Description</th>\n" .
-	      "<th>Owner</th>\n" .
-	      "<th>last change</th>\n" .
-	      "<th></th>\n" .
+	      "<tr>\n";
+	if (defined($order) && ($order eq "project")) {
+		@projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
+		print "<th>Project</th>\n";
+	} else {
+		print "<th>" . $cgi->a({-class => "list", -href => "$my_uri?o=project"}, "Project") . "</th>\n";
+	}
+	print "<th>Description</th>\n";
+	if (defined($order) && ($order eq "owner")) {
+		@projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
+		print "<th>Owner</th>\n";
+	} else {
+		print "<th>" . $cgi->a({-class => "list", -href => "$my_uri?o=owner"}, "Owner") . "</th>\n";
+	}
+	if (defined($order) && ($order eq "age")) {
+		@projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
+		print "<th>last change</th>\n";
+	} else {
+		print "<th>" . $cgi->a({-class => "list", -href => "$my_uri?o=age"}, "last change") . "</th>\n";
+	}
+	print "<th></th>\n" .
 	      "</tr>\n";
 	my $alternate = 0;
-	foreach my $pr (@list) {
-		my %proj = %$pr;
-		my $head = git_read_hash("$proj{'path'}/HEAD");
-		if (!defined $head) {
-			next;
-		}
-		$ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
-		my %co = git_read_commit($head);
-		if (!%co) {
-			next;
-		}
-		my $descr = git_read_description($proj{'path'}) || "";
-		$descr = chop_str($descr, 25, 5);
-		# get directory owner if not already specified
-		if (!defined $proj{'owner'}) {
-			$proj{'owner'} = get_file_owner("$projectroot/$proj{'path'}") || "";
-		}
+	foreach my $pr (@projects) {
 		if ($alternate) {
 			print "<tr class=\"dark\">\n";
 		} else {
 			print "<tr class=\"light\">\n";
 		}
 		$alternate ^= 1;
-		print "<td>" . $cgi->a({-href => "$my_uri?p=$proj{'path'};a=summary", -class => "list"}, escapeHTML($proj{'path'})) . "</td>\n" .
-		      "<td>$descr</td>\n" .
-		      "<td><i>" . chop_str($proj{'owner'}, 15) . "</i></td>\n";
+		print "<td>" . $cgi->a({-href => "$my_uri?p=$pr->{'path'};a=summary", -class => "list"}, escapeHTML($pr->{'path'})) . "</td>\n" .
+		      "<td>$pr->{'descr'}</td>\n" .
+		      "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
 		my $colored_age;
-		if ($co{'age'} < 60*60*2) {
-			$colored_age = "<span style =\"color: #009900;\"><b><i>$co{'age_string'}</i></b></span>";
-		} elsif ($co{'age'} < 60*60*24*2) {
-			$colored_age = "<span style =\"color: #009900;\"><i>$co{'age_string'}</i></span>";
+		if ($pr->{'commit'}{'age'} < 60*60*2) {
+			$colored_age = "<span style =\"color: #009900;\"><b><i>$pr->{'commit'}{'age_string'}</i></b></span>";
+		} elsif ($pr->{'commit'}{'age'} < 60*60*24*2) {
+			$colored_age = "<span style =\"color: #009900;\"><i>$pr->{'commit'}{'age_string'}</i></span>";
 		} else {
-			$colored_age = "<i>$co{'age_string'}</i>";
+			$colored_age = "<i>$pr->{'commit'}{'age_string'}</i>";
 		}
 		print "<td>$colored_age</td>\n" .
 		      "<td class=\"link\">" .
-		      $cgi->a({-href => "$my_uri?p=$proj{'path'};a=summary"}, "summary") .
-		      " | " . $cgi->a({-href => "$my_uri?p=$proj{'path'};a=shortlog"}, "shortlog") .
-		      " | " . $cgi->a({-href => "$my_uri?p=$proj{'path'};a=log"}, "log") .
+		      $cgi->a({-href => "$my_uri?p=$pr->{'path'};a=summary"}, "summary") .
+		      " | " . $cgi->a({-href => "$my_uri?p=$pr->{'path'};a=shortlog"}, "shortlog") .
+		      " | " . $cgi->a({-href => "$my_uri?p=$pr->{'path'};a=log"}, "log") .
 		      "</td>\n" .
 		      "</tr>\n";
 	}