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

[PATCH 1/2 v2] gitweb: sort projects by path.

From
GBGustavo Sverzut Barbieri <barbieri@profusion.mobi>
Date
Jul 29, 2008, 03:59 UTC
Message-ID
<1217303942-18526-1-git-send-email-barbieri@profusion.mobi>
In-Reply-To
<1217298868-16513-2-git-send-email-barbieri@profusion.mobi>

Projects are paths, so they should be sorted in pieces, not as a whole, so a/x will be come before a-b/x.

Signed-off-by: Gustavo Sverzut Barbieri <barbieri@profusion.mobi>
---

New version that fix cmp_paths. Unlike I though initially, $#list reports the last element index, not the list length. Nonetheless the last component must be compared only if the list lengths are the same, otherwise we'll get $root/a/a.git before $root/b.git and sections will look ugly (group elements of the same level together).

 gitweb/gitweb.perl |   42 +++++++++++++++++++++++++++++++++++++-----
 1 files changed, 37 insertions(+), 5 deletions(-)
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 90cd99b..06c9901 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -3574,17 +3574,45 @@ sub fill_project_list_info {
 	return @projects;
 }
 
+sub cmp_paths {
+	my ($a, $b) = @_;
+	my @la = split('/', $a);
+	my @lb = split('/', $b);
+	my $last;
+
+	if ($#la < $#lb) {
+		$last = $#la;
+	} else {
+		$last = $#lb;
+	}
+
+	for (my $i = 0; $i < $last; $i++) {
+		my $r = $la[$i] cmp $lb[$i];
+		if ($r != 0) {
+			return $r;
+		}
+	}
+
+	if ($#la == $#lb) {
+		return $la[$last] cmp $lb[$last];
+	} else {
+		return $#la <=> $#lb;
+	}
+}
+
 # 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) = @_;
+	my ($sort_mode, $name, $order, $key, $header, $list) = @_;
 	$key    ||= $name;
 	$header ||= ucfirst($name);
 
 	if ($order eq $name) {
-		if ($str_sort) {
+		if ($sort_mode == 2) {
+			@$list = sort {cmp_paths($a->{$key}, $b->{$key})} @$list;
+		} elsif ($sort_mode == 1) {
 			@$list = sort {$a->{$key} cmp $b->{$key}} @$list;
-		} else {
+		} elsif ($sort_mode == 0) {
 			@$list = sort {$a->{$key} <=> $b->{$key}} @$list;
 		}
 		print "<th>$header</th>\n";
@@ -3596,6 +3624,10 @@ sub print_sort_th {
 	}
 }
 
+sub print_sort_th_path {
+	print_sort_th(2, @_);
+}
+
 sub print_sort_th_str {
 	print_sort_th(1, @_);
 }
@@ -3620,8 +3652,8 @@ sub git_project_list_body {
 		if ($check_forks) {
 			print "<th></th>\n";
 		}
-		print_sort_th_str('project', $order, 'path',
-		                  'Project', \@projects);
+		print_sort_th_path('project', $order, 'path',
+		                   'Project', \@projects);
 		print_sort_th_str('descr', $order, 'descr_long',
 		                  'Description', \@projects);
 		print_sort_th_str('owner', $order, 'owner',
-- 
1.5.5.2
Previous: Gustavo Sverzut BarbieriNext: Gustavo Sverzut Barbieri
Message 4 of 8 in “gitweb use sections”
  1. 0/2 gitweb use sectionsGustavo Sverzut Barbieri, Jul 29, 2008
  2. 1/2 gitweb: sort projects by path.Gustavo Sverzut Barbieri, Jul 29, 2008
  3. 2/2 gitweb: add section support to gitweb project listing.Gustavo Sverzut Barbieri, Jul 29, 2008
  4. 1/2 gitweb: sort projects by path.Gustavo Sverzut Barbieri, Jul 29, 2008
  5. Gustavo Sverzut BarbieriJul 31, 2008
  6. Petr BaudisJul 31, 2008
  7. Gustavo Sverzut BarbieriJul 31, 2008
  8. Gustavo Sverzut BarbieriAug 10, 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.