repostead

static page generator for VCS repositories

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


diff --git a/repostead.c b/repostead.c
index 22afb4a..8ff269b 100644
--- a/repostead.c
+++ b/repostead.c
@@ -77,6 +77,12 @@ struct repostead_config {
 	git_repository *repo;
 };
 
+struct repostead_branch {
+	const char *name;
+	char dir[RS_PATH_MAX];
+	git_tree *tree;
+};
+
 struct repostead_file_info {
 	const char *name;
 	const char *dir;
@@ -477,18 +483,15 @@ path_across_roots(FILE *f, const char *from, const char *to,
 static void
 branch_path(char *dest, size_t dsize,
 	const struct repostead_config *conf,
-	const char *branch, const char *root, const char *path)
+	const struct repostead_branch *br, const char *root, const char *path)
 {
-	char branch_dir[RS_PATH_MAX] = "";
-
 	assert(root && *root && path);
-	xpath_join(branch_dir, sizeof(branch_dir), conf->out_path, branch);
 	if (*path) {
 		char root_dir[RS_PATH_MAX] = "";
-		xpath_join(root_dir, sizeof(root_dir), branch_dir, root);
+		xpath_join(root_dir, sizeof(root_dir), br->dir, root);
 		xpath_join(dest, dsize, root_dir, path);
 	} else {
-		xpath_join(dest, dsize, branch_dir, root);
+		xpath_join(dest, dsize, br->dir, root);
 	}
 }
 
@@ -704,7 +707,7 @@ print_listing(FILE *f, const char *s, size_t len)
 static void
 process_file(struct repostead_file_info *fi,
 	const struct repostead_config *conf,
-	const char *branch, const git_blob *blob)
+	const struct repostead_branch *br, const git_blob *blob)
 {
 	char html_dir[RS_PATH_MAX] = "";
 	char raw_dir[RS_PATH_MAX] = "";
@@ -718,9 +721,9 @@ process_file(struct repostead_file_info *fi,
 	git_object_size_t size = 0;
 
 	branch_path(html_dir, sizeof(html_dir), conf,
-		branch, RS_TREE_ROOT, fi->dir);
+		br, RS_TREE_ROOT, fi->dir);
 	branch_path(raw_dir, sizeof(raw_dir), conf,
-		branch, RS_RAW_ROOT, fi->dir);
+		br, RS_RAW_ROOT, fi->dir);
 
 	xpath_join(file_path, sizeof(file_path), raw_dir, fi->name);
 	xpath_join(file_html_base, sizeof(file_html_base), html_dir, fi->name);
@@ -756,7 +759,7 @@ process_file(struct repostead_file_info *fi,
 	}
 
 	fputs("<h2>", file_html);
-	path_breadcrumb(file_html, branch, RS_TREE_ROOT, file_rel_path, true);
+	path_breadcrumb(file_html, br->name, RS_TREE_ROOT, file_rel_path, true);
 	fprintf(file_html, " <small>(<span class=\"m\">%s</span>, ", fi->mode);
 	fprintf(file_html, "<span class=\"s\">%s</span>, ", fi->size);
 	fputs("<a class=\"raw\" href=\"", file_html);
@@ -774,7 +777,8 @@ process_file(struct repostead_file_info *fi,
 }
 
 static void
-process_tree(const struct repostead_config *conf, const char *branch,
+process_tree(const struct repostead_config *conf,
+	const struct repostead_branch *br,
 	const git_tree *tree, const char *name, const char *path)
 {
 	char tree_path[RS_PATH_MAX] = "";
@@ -784,11 +788,9 @@ process_tree(const struct repostead_config *conf, const char *branch,
 	FILE *index_file = NULL;
 	size_t i = 0, count = git_tree_entrycount(tree);
 
-	branch_path(tree_path, sizeof(tree_path), conf,
-		branch, RS_TREE_ROOT, path);
+	branch_path(tree_path, sizeof(tree_path), conf, br, RS_TREE_ROOT, path);
 	xmkdir(tree_path);
-	branch_path(raw_path, sizeof(raw_path), conf,
-		branch, RS_RAW_ROOT, path);
+	branch_path(raw_path, sizeof(raw_path), conf, br, RS_RAW_ROOT, path);
 	xmkdir(raw_path);
 
 	xpath_join(index_file_path, sizeof(index_file_path),
@@ -796,7 +798,7 @@ process_tree(const struct repostead_config *conf, const char *branch,
 	index_file = page_begin(conf, index_file_path, name, false);
 
 	fputs("<h2>", index_file);
-	path_breadcrumb(index_file, branch, RS_TREE_ROOT, path, false);
+	path_breadcrumb(index_file, br->name, RS_TREE_ROOT, path, false);
 	fputs("</h2>", index_file);
 
 	/* directories and submodules */
@@ -829,8 +831,7 @@ process_tree(const struct repostead_config *conf, const char *branch,
 			if (git_tree_lookup(&child, conf->repo, oid) != 0) {
 				gitdie("git_tree_lookup");
 			}
-			process_tree(conf, branch,
-				child, child_name, child_path);
+			process_tree(conf, br, child, child_name, child_path);
 			git_tree_free(child);
 		}
 		else if (git_tree_entry_filemode(e) == GIT_FILEMODE_COMMIT) {
@@ -872,7 +873,7 @@ process_tree(const struct repostead_config *conf, const char *branch,
 			}
 
 			git_filemode_str(fi.mode, git_tree_entry_filemode_raw(e));
-			process_file(&fi, conf, branch, blob);
+			process_file(&fi, conf, br, blob);
 			fprintf(index_file,
 				"<tr class=\"file\">"
 				"<td class=\"m\">%s</td>",
@@ -895,7 +896,7 @@ process_tree(const struct repostead_config *conf, const char *branch,
 
 static void
 process_commits(const struct repostead_config *conf,
-	const char *branch, const git_oid *commit_oid)
+	const struct repostead_branch *br, const git_oid *commit_oid)
 {
 	char log_path[RS_PATH_MAX] = "";
 	const char *index_file_name = "index.html";
@@ -905,12 +906,12 @@ process_commits(const struct repostead_config *conf,
 	git_oid oid;
 	int error = 0;
 
-	branch_path(log_path, sizeof(log_path), conf, branch, RS_LOG_ROOT, "");
+	branch_path(log_path, sizeof(log_path), conf, br, RS_LOG_ROOT, "");
 	xmkdir(log_path);
 
 	xpath_join(index_file_path, sizeof(index_file_path),
 		log_path, index_file_name);
-	index_file = page_begin(conf, index_file_path, branch, false);
+	index_file = page_begin(conf, index_file_path, br->name, false);
 
 	if (git_revwalk_new(&walker, conf->repo) < 0) {
 		gitdie("git_revwalk_new");
@@ -937,6 +938,8 @@ process_commits(const struct repostead_config *conf,
 			}
 			fputs("</tr>", index_file);
 			git_commit_free(c);
+		} else {
+			gitdie("git_commit_lookup");
 		}
 	}
 	fputs("</table>", index_file);
@@ -952,11 +955,13 @@ process_commits(const struct repostead_config *conf,
 
 static void
 process_branch(const struct repostead_config *conf,
-	const char *branch, const git_oid *tip_oid)
+	const char *name, const char *dir, const git_oid *tip_oid)
 {
 	git_commit *tip = NULL;
 	git_tree *tree = NULL;
-	char branch_path[RS_PATH_MAX] = "";
+	struct repostead_branch br;
+
+	memset(&br, 0, sizeof(br));
 
 	if (git_commit_lookup(&tip, conf->repo, tip_oid) < 0) {
 		gitdie("git_commit_lookup");
@@ -965,12 +970,15 @@ process_branch(const struct repostead_config *conf,
 		gitdie("git_commit_tree");
 	}
 
-	xpath_join(branch_path, sizeof(branch_path), conf->out_path, branch);
+	br.name = name;
+	br.tree = tree;
+	xpath_join(br.dir, sizeof(br.dir), conf->out_path, dir);
+
 	/* mkdirs in case branch name contains '/' */
-	xmkdirs(branch_path);
+	xmkdirs(br.dir);
 
-	process_tree(conf, branch, tree, branch, "");
-	process_commits(conf, branch, tip_oid);
+	process_tree(conf, &br, br.tree, br.name, "");
+	process_commits(conf, &br, tip_oid);
 
 	git_commit_free(tip);
 	git_tree_free(tree);
@@ -1079,7 +1087,7 @@ process_repo(struct repostead_config *conf)
 				fputc('/', index_file);
 				url_enc(index_file, RS_LOG_ROOT);
 				fputs("/\">log</a></td>", index_file);
-				process_branch(conf, brnm, tip);
+				process_branch(conf, brnm, brnm, tip);
 			}
 			fputs("</tr>", index_file);
 		}