[PATCH 1/4] cgit: add_repo: Assign default branch from git HEAD to, repo->defbranch if undefined

Sven Göthel sgothel at jausoft.com
Sun Jul 19 17:03:45 UTC 2026


From 5ccffc927460e651da10a6b0be2e9786129355bb Mon Sep 17 00:00:00 2001
From: Sven Göthel <sgothel at jausoft.com>
Date: Sat, 30 May 2026 00:56:55 +0200
Subject: cgit: add_repo: Assign default branch from git HEAD to
 repo->defbranch if undefined
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Currently, if defbranch is not `master` and no manual config performed,
repo age is undefined.

This patch reads the default branch from git HEAD if not configured via cgitrc
and uses it in all commands like repo-list age, selected at log etc.

add_repo is called from scan_path early when gathering and caching server repositories.

Signed-off-by: Sven Göthel <sgothel at jausoft.com>

diff --git a/scan-tree.c b/scan-tree.c
index c120efe..b701d00 100644
--- a/scan-tree.c
+++ b/scan-tree.c
@@ -79,6 +79,32 @@ static char *xstrrchr(char *s, char *from, int c)
 	return from < s ? NULL : from;
 }
 
+static char *read_git_head_branch(const char *repo_path) {
+	struct strbuf path = STRBUF_INIT;
+	size_t size;
+	char *line = NULL;
+
+	strbuf_addf(&path, "%s/HEAD", repo_path);
+
+	if (read_first_line(path.buf, &line, &size)) {
+		strbuf_release(&path);
+		free(line);
+		return NULL;
+	}
+	strbuf_release(&path);
+
+	const char *refname;
+	char *result = NULL;
+
+	if (!line || !skip_prefix(line, "ref: refs/heads/", &refname)) {
+		free(line);
+		return NULL;
+	}
+	result = xstrdup(refname);
+	free(line);
+	return result;
+}
+
 static void add_repo(const char *base, struct strbuf *path)
 {
 	struct stat st;
@@ -180,6 +206,9 @@ static void add_repo(const char *base, struct strbuf *path)
 	if (!stat(path->buf, &st))
 		parse_configfile(path->buf, &scan_tree_repo_config);
 
+	if (!repo->defbranch)
+		repo->defbranch = read_git_head_branch(repo->path);
+
 	strbuf_release(&rel);
 }
 



More information about the CGit mailing list