cgit

commit 184c5655b2e350dbd0dd8be75d3f370f22aa6dee

Author: Jason A. Donenfeld <Jason@zx2c4.com>

ui-repolist: Case insensitive sorting and age sort

Add two options, one for doing the ordinary name sorts in a
case-insensitive manner, and another for choosing to sort repos in each
section by age instead of by name.

 cgit.c | 6 ++++++
 cgit.h | 2 ++
 cgitrc.5.txt | 12 +++++++++++-
 ui-repolist.c | 22 ++++++++++++++++++----


diff --git a/cgit.c b/cgit.c
index 4656ed66e6fb7932e439e5957972de3368d4c922..24ef8e0f89c2fb598706adba2e8be066892f6e1e 100644
--- a/cgit.c
+++ b/cgit.c
@@ -199,6 +199,8 @@ 	else if (!strcmp(name, "cache-static-ttl"))
 		ctx.cfg.cache_static_ttl = atoi(value);
 	else if (!strcmp(name, "cache-dynamic-ttl"))
 		ctx.cfg.cache_dynamic_ttl = atoi(value);
+	else if (!strcmp(name, "case-sensitive-sort"))
+		ctx.cfg.case_sensitive_sort = atoi(value);
 	else if (!strcmp(name, "about-filter"))
 		ctx.cfg.about_filter = new_filter(value, ABOUT);
 	else if (!strcmp(name, "commit-filter"))
@@ -231,6 +233,8 @@ 	else if (!strcmp(name, "scan-hidden-path"))
 		ctx.cfg.scan_hidden_path = atoi(value);
 	else if (!strcmp(name, "section-from-path"))
 		ctx.cfg.section_from_path = atoi(value);
+	else if (!strcmp(name, "section-sort"))
+		ctx.cfg.section_sort = xstrdup(value);
 	else if (!strcmp(name, "source-filter"))
 		ctx.cfg.source_filter = new_filter(value, SOURCE);
 	else if (!strcmp(name, "summary-log"))
@@ -335,6 +339,7 @@ 	ctx->cfg.cache_root = CGIT_CACHE_ROOT;
 	ctx->cfg.cache_root_ttl = 5;
 	ctx->cfg.cache_scanrc_ttl = 15;
 	ctx->cfg.cache_static_ttl = -1;
+	ctx->cfg.case_sensitive_sort = 1;
 	ctx->cfg.css = "/cgit.css";
 	ctx->cfg.logo = "/cgit.png";
 	ctx->cfg.local_time = 0;
@@ -359,6 +364,7 @@ 	ctx->cfg.root_desc = "a fast webinterface for the git dscm";
 	ctx->cfg.scan_hidden_path = 0;
 	ctx->cfg.script_name = CGIT_SCRIPT_NAME;
 	ctx->cfg.section = "";
+	ctx->cfg.section_sort = "name";
 	ctx->cfg.summary_branches = 10;
 	ctx->cfg.summary_log = 10;
 	ctx->cfg.summary_tags = 10;




diff --git a/cgit.h b/cgit.h
index 91b4d722f4e48bccd97925bef2c686a0590483a0..79ba7adffe63d8c875205dd00e88aa6bb59fa355 100644
--- a/cgit.h
+++ b/cgit.h
@@ -187,6 +187,7 @@ 	char *root_desc;
 	char *root_readme;
 	char *script_name;
 	char *section;
+	char *section_sort;
 	char *virtual_root;
 	char *strict_export;
 	int cache_size;
@@ -196,6 +197,7 @@ 	int cache_repo_ttl;
 	int cache_root_ttl;
 	int cache_scanrc_ttl;
 	int cache_static_ttl;
+	int case_sensitive_sort;
 	int embedded;
 	int enable_filter_overrides;
 	int enable_gitweb_owner;




diff --git a/cgitrc.5.txt b/cgitrc.5.txt
index d1a90c5a8c5088d05269ed9ba48a8b56f2caa69e..902fff3e6674115e708cfe2b3f917632468f0070 100644
--- a/cgitrc.5.txt
+++ b/cgitrc.5.txt
@@ -70,6 +70,10 @@ 	Number which specifies the time-to-live, in minutes, for the cached
 	version of repository pages accessed with a fixed SHA1. Default value:
 	"5".
 
+case-sensitive-sort::
+	Sort items in the repo list case sensitively. Default value: "1".
+	See also: section-sort.
+
 clone-prefix::
 	Space-separated list of common prefixes which, when combined with a
 	repository url, generates valid clone urls for the repository. This
@@ -333,11 +337,17 @@ 	The name of the current repository section - all repositories defined
 	after this option will inherit the current section name. Default value:
 	none.
 
+section-sort::
+	The way in which repositories in each section are sorted. Valid values
+	are "name" for sorting by the repo name or "age" for sorting by the
+	most recently updated repository. Default value: "name". See also:
+	section, case-sensitive-sort.
+
 section-from-path::
 	A number which, if specified before scan-path, specifies how many
 	path elements from each repo path to use as a default section name.
 	If negative, cgit will discard the specified number of path elements
-	above the repo directory. Default value: 0.
+	above the repo directory. Default value: "0".
 
 side-by-side-diffs::
 	If set to "1" shows side-by-side diffs instead of unidiffs per




diff --git a/ui-repolist.c b/ui-repolist.c
index d946f32cd5354021c45637a0af24e1a7653dff92..4e2bdf4d0167455feebb5ab214523935da01cce1 100644
--- a/ui-repolist.c
+++ b/ui-repolist.c
@@ -9,6 +9,7 @@
 #include "cgit.h"
 #include "html.h"
 #include "ui-shared.h"
+#include <strings.h>
 
 time_t read_agefile(char *path)
 {
@@ -131,8 +132,12 @@ }
 
 static int cmp(const char *s1, const char *s2)
 {
-	if (s1 && s2)
-		return strcmp(s1, s2);
+	if (s1 && s2) {
+		if (ctx.cfg.case_sensitive_sort)
+			return strcmp(s1, s2);
+		else
+			return strcasecmp(s1, s2);
+	}
 	if (s1 && !s2)
 		return -1;
 	if (s2 && !s1)
@@ -145,10 +150,19 @@ {
 	const struct cgit_repo *r1 = a;
 	const struct cgit_repo *r2 = b;
 	int result;
+	time_t t;
 
 	result = cmp(r1->section, r2->section);
-	if (!result)
-		result = cmp(r1->name, r2->name);
+	if (!result) {
+		if (!strcmp(ctx.cfg.section_sort, "age")) {
+			// get_repo_modtime caches the value in r->mtime, so we don't
+			// have to worry about inefficiencies here.
+			if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
+				result = r2->mtime - r1->mtime;
+		}
+		if (!result)
+			result = cmp(r1->name, r2->name);
+	}
 	return result;
 }