cgit

commit 03f6e34bb9d683723cfc4fe58ee5bb983b95e173

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

cgit: prepare repo before error pages

This fixes a crash when showing a list of all heads in the <select> box
in the header.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>

 cgit.c | 19 ++++++++++++-------


diff --git a/cgit.c b/cgit.c
index a3702c26a6781d1cbeeb888da8f9390ff966a61c..bd9cb3fb11a66383d56256b2c0426e0070e7208c 100644
--- a/cgit.c
+++ b/cgit.c
@@ -561,12 +561,8 @@         html_txt(url);
         html("</a></td></tr>\n");
 }
 
-static int prepare_repo_cmd(void)
+static void prepare_repo_env(int *nongit)
 {
-	struct object_id oid;
-	int nongit = 0;
-	int rc;
-
 	/* The path to the git repository. */
 	setenv("GIT_DIR", ctx.repo->path, 1);
 
@@ -579,8 +575,13 @@
 	/* Setup the git directory and initialize the notes system. Both of these
 	 * load local configuration from the git repository, so we do them both while
 	 * the HOME variables are unset. */
-	setup_git_directory_gently(&nongit);
+	setup_git_directory_gently(nongit);
 	init_display_notes(NULL);
+}
+static int prepare_repo_cmd(int nongit)
+{
+	struct object_id oid;
+	int rc;
 
 	if (nongit) {
 		const char *name = ctx.repo->name;
@@ -700,6 +701,7 @@
 static void process_request(void)
 {
 	struct cgit_cmd *cmd;
+	int nongit = 0;
 
 	/* If we're not yet authenticated, no matter what page we're on,
 	 * display the authentication body from the auth_filter. This should
@@ -714,6 +716,9 @@ 		cgit_close_filter(ctx.cfg.auth_filter);
 		cgit_print_docend();
 		return;
 	}
+
+	if (ctx.repo)
+		prepare_repo_env(&nongit);
 
 	cmd = cgit_get_cmd();
 	if (!cmd) {
@@ -740,7 +745,7 @@ 	 * Otherwise, no path limit is in effect (ctx.qry.vpath = NULL).
 	 */
 	ctx.qry.vpath = cmd->want_vpath ? ctx.qry.path : NULL;
 
-	if (ctx.repo && prepare_repo_cmd())
+	if (ctx.repo && prepare_repo_cmd(nongit))
 		return;
 
 	cmd->fn();