From 804b5b6172f1f8cda1c118c30cc257299bda8ee6 Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sun, 2 Aug 2026 09:47:00 -1000 Subject: Give empty repositories their own page --- source/cgit.c | 25 +++++++------------------ source/cgit.h | 3 +++ source/cgit.mk | 1 + source/ui-empty.c | 41 +++++++++++++++++++++++++++++++++++++++++ source/ui-empty.h | 6 ++++++ source/ui-shared.c | 14 ++++++++++++-- 6 files changed, 70 insertions(+), 20 deletions(-) create mode 100644 source/ui-empty.c create mode 100644 source/ui-empty.h diff --git a/source/cgit.c b/source/cgit.c index 412d5f3..1ab77a4 100644 --- a/source/cgit.c +++ b/source/cgit.c @@ -16,6 +16,7 @@ #include "ui-shared.h" #include "ui-stats.h" #include "ui-blob.h" +#include "ui-empty.h" #include "ui-summary.h" #include "scan-tree.h" @@ -603,17 +604,6 @@ static void choose_readme(struct cgit_repo *repo) string_list_append(&repo->readme, filename)->util = ref; } -static void print_no_repo_clone_urls(const char *url) -{ - html(""); - html_txt(url); - html("\n"); -} - static void prepare_repo_env(int *nongit) { /* The path to the git repository. */ @@ -656,16 +646,15 @@ static int prepare_repo_cmd(int nongit) } if (!ctx.qry.head) { + ctx.empty_repo = 1; + // Before the document starts, since the carries + // and those clone urls expand macros such + // as $CGIT_REPO_URL out of this environment. + cgit_prepare_repo_env(ctx.repo); cgit_print_http_headers(); cgit_print_docstart(); cgit_print_pageheader(); - cgit_print_error("Repository seems to be empty"); - if (!strcmp(ctx.qry.page, "summary")) { - html("\n"); - cgit_prepare_repo_env(ctx.repo); - cgit_add_clone_urls(print_no_repo_clone_urls); - html("
 
Clone
\n"); - } + cgit_print_empty_repo(); cgit_print_docend(); return 1; } diff --git a/source/cgit.h b/source/cgit.h index c41854e..d33a46b 100644 --- a/source/cgit.h +++ b/source/cgit.h @@ -318,6 +318,9 @@ struct cgit_context { struct cgit_config cfg; struct cgit_repo *repo; struct cgit_page page; + // Set once the repository is known to hold no commits, so the page + // header can drop the tabs that cannot render anything. + int empty_repo; }; typedef int (*write_archive_fn_t)(const char *, const char *); diff --git a/source/cgit.mk b/source/cgit.mk index b2d7dfc..3234148 100644 --- a/source/cgit.mk +++ b/source/cgit.mk @@ -106,6 +106,7 @@ CGIT_OBJ_NAMES += ui-blob.o CGIT_OBJ_NAMES += ui-clone.o CGIT_OBJ_NAMES += ui-commit.o CGIT_OBJ_NAMES += ui-diff.o +CGIT_OBJ_NAMES += ui-empty.o CGIT_OBJ_NAMES += ui-log.o CGIT_OBJ_NAMES += ui-patch.o CGIT_OBJ_NAMES += ui-plain.o diff --git a/source/ui-empty.c b/source/ui-empty.c new file mode 100644 index 0000000..cb91fbc --- /dev/null +++ b/source/ui-empty.c @@ -0,0 +1,41 @@ +/* + * ui-empty.c: page shown for a repository that holds no commits + * + * Copyright (C) 2006-2014 cgit Development Team + * + * Licensed under GNU General Public License v2 + * (see LICENSE.txt for full license text) + */ + +#include "cgit.h" +#include "ui-empty.h" +#include "html.h" +#include "ui-shared.h" + +static void print_clone_url(const char *url) +{ + html(""); + html_txt(url); + html("\n"); +} + +/* + * The caller has already prepared the repo environment, which the clone urls + * need for macro expansion. + */ +void cgit_print_empty_repo(void) +{ + cgit_print_error("Repository seems to be empty"); + + if (!ctx.repo->clone_url && !ctx.cfg.clone_prefix) + return; + + html(""); + html("\n"); + cgit_add_clone_urls(print_clone_url); + html("
Clone
\n"); +} diff --git a/source/ui-empty.h b/source/ui-empty.h new file mode 100644 index 0000000..dda303a --- /dev/null +++ b/source/ui-empty.h @@ -0,0 +1,6 @@ +#ifndef UI_EMPTY_H +#define UI_EMPTY_H + +extern void cgit_print_empty_repo(void); + +#endif /* UI_EMPTY_H */ diff --git a/source/ui-shared.c b/source/ui-shared.c index d023243..e53d1e5 100644 --- a/source/ui-shared.c +++ b/source/ui-shared.c @@ -1086,7 +1086,9 @@ static void print_header(void) html_txt(ctx.cfg.root_title); html("\n"); - if (ctx.repo && ctx.env.authenticated) { + // A repository with no commits has no branches to list, so the + // switcher would be an empty select next to a switch button. + if (ctx.repo && ctx.env.authenticated && !ctx.empty_repo) { html("
\n"); cgit_add_hidden_formfields(0, 1, ctx.qry.page); html("