repostead

static page generator for VCS repositories

git clone https://www.shemshak.org/~amin/repostead


diff --git a/repostead.c b/repostead.c
index 8ff269b..97b6efc 100644
--- a/repostead.c
+++ b/repostead.c
@@ -953,7 +953,8 @@ process_commits(const struct repostead_config *conf,
 	page_end(index_file, index_file_path);
 }
 
-static void
+NODISCARD
+static git_tree *
 process_branch(const struct repostead_config *conf,
 	const char *name, const char *dir, const git_oid *tip_oid)
 {
@@ -981,7 +982,7 @@ process_branch(const struct repostead_config *conf,
 	process_commits(conf, &br, tip_oid);
 
 	git_commit_free(tip);
-	git_tree_free(tree);
+	return tree;
 }
 
 static int
@@ -1034,6 +1035,7 @@ process_repo(struct repostead_config *conf)
 	git_branch_iterator *brit = NULL;
 	git_branch_t brty;
 	git_reference *ref = NULL;
+	git_tree *head_tree = NULL;
 	int error = 0;
 
 	if (git_libgit2_init() < 0) {
@@ -1060,37 +1062,42 @@ process_repo(struct repostead_config *conf)
 	while ((error = git_branch_next(&ref, &brty, brit)) == 0) {
 		const char *brnm = NULL;
 		const git_oid *tip = git_reference_target(ref);
+		bool is_head = (git_branch_is_head(ref) == 1);
 
 		if (git_branch_name(&brnm, ref) < 0) {
 			warn("failed to get branch name, skipping");
 		} else if (!branch_name_ok(brnm)) {
 			warn("unsafe branch name, skipping");
+		}
+		fputs("<tr>", index_file);
+		if (is_head) {
+			fputs("<td class=\"head\"><b>", index_file);
+			html_esc(index_file, brnm);
+			fputs("</b></td>", index_file);
 		} else {
-			fputs("<tr>", index_file);
-			if (git_branch_is_head(ref) == 1) {
-				fputs("<td class=\"head\"><b>", index_file);
-				html_esc(index_file, brnm);
-				fputs("</b></td>", index_file);
-			} else {
-				fputs("<td>", index_file);
-				html_esc(index_file, brnm);
-				fputs("</td>", index_file);
-			}
-			if (tip) {
-				fputs("<td><a href=\"", index_file);
-				url_enc(index_file, brnm);
-				fputc('/', index_file);
-				url_enc(index_file, RS_TREE_ROOT);
-				fputs("/\">tree</a></td>", index_file);
-				fputs("<td><a href=\"", index_file);
-				url_enc(index_file, brnm);
-				fputc('/', index_file);
-				url_enc(index_file, RS_LOG_ROOT);
-				fputs("/\">log</a></td>", index_file);
-				process_branch(conf, brnm, brnm, tip);
+			fputs("<td>", index_file);
+			html_esc(index_file, brnm);
+			fputs("</td>", index_file);
+		}
+		if (tip) {
+			fputs("<td><a href=\"", index_file);
+			url_enc(index_file, brnm);
+			fputc('/', index_file);
+			url_enc(index_file, RS_TREE_ROOT);
+			fputs("/\">tree</a></td>", index_file);
+			fputs("<td><a href=\"", index_file);
+			url_enc(index_file, brnm);
+			fputc('/', index_file);
+			url_enc(index_file, RS_LOG_ROOT);
+			fputs("/\">log</a></td>", index_file);
+
+			head_tree = process_branch(conf, brnm, brnm, tip);
+			if (!is_head) {
+				git_tree_free(head_tree);
+				head_tree = NULL;
 			}
-			fputs("</tr>", index_file);
 		}
+		fputs("</tr>", index_file);
 		git_reference_free(ref);
 	}
 	fputs("</table>", index_file);
@@ -1101,6 +1108,9 @@ process_repo(struct repostead_config *conf)
 
 	page_end(index_file, index_file_path);
 
+	if (head_tree) {
+		git_tree_free(head_tree);
+	}
 	git_branch_iterator_free(brit);
 	git_repository_free(conf->repo);
 	conf->repo = NULL;