From d82d775c8ade39ec686e566908e3460c97697d23 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 4 Oct 2026 17:22:08 +0300 Subject: ui-repolist: Read HEAD file when deciding on the default branch --- ui-repolist.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/ui-repolist.c b/ui-repolist.c index 87fc40c..57110ee 100644 --- a/ui-repolist.c +++ b/ui-repolist.c @@ -32,6 +32,33 @@ static time_t read_agefile(const char *path) return result; } +static void add_repo_defbranch(const struct cgit_repo *repo, struct strbuf *result_path) { + struct strbuf path = STRBUF_INIT; + struct stat s; + char *ref, *refname; + size_t ref_size; + + if (repo->defbranch) { + strbuf_addf(result_path, "%s", repo->defbranch); + goto add_repo_defbranch_end; + } + + strbuf_addf(&path, "%s/HEAD", repo->path); + if (stat(path.buf, &s) != 0 || read_first_line(path.buf, &ref, &ref_size) != 0) { + strbuf_addf(result_path, "%s", "master"); + goto add_repo_defbranch_end; + } + + if (!skip_prefix(ref, "ref: refs/heads/", &refname) && !skip_prefix(ref, "refs/heads/", &refname)) + strbuf_addf(result_path, "%s", "master"); + else + strbuf_addf(result_path, "%s", refname); + +add_repo_defbranch_end: + free(ref); + strbuf_release(&path); +} + static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime) { struct strbuf path = STRBUF_INIT; @@ -52,8 +79,8 @@ static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime) } strbuf_reset(&path); - strbuf_addf(&path, "%s/refs/heads/%s", repo->path, - repo->defbranch ? repo->defbranch : "master"); + strbuf_addf(&path, "%s/refs/heads/", repo->path); + add_repo_defbranch(repo, &path); if (stat(path.buf, &s) == 0) { *mtime = s.st_mtime; r->mtime = *mtime; -- cgit v1.3.1-23-gd82d