repostead

static page generator for VCS repositories

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


diff --git a/repostead.c b/repostead.c
index 97b6efc..12bbcd6 100644
--- a/repostead.c
+++ b/repostead.c
@@ -90,6 +90,10 @@ struct repostead_file_info {
 	char mode[RS_FILE_INFO_MODE_STRLEN];
 };
 
+static const char *const readme_files[] = {
+	"README", "README.md"
+};
+
 static const char *const html_ent[UCHAR_MAX + 1] = {
 	['<'] = "&lt;",
 	['>'] = "&gt;",
@@ -539,7 +543,7 @@ format_bytes(char *dest, size_t dsize, uint64_t bytes)
 	static const char *const units[] = {
 		"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"
 	};
-	const int max_unit = ((int)(sizeof units / sizeof units[0])) - 1;
+	const int max_unit = ((int)(sizeof(units) / sizeof(units[0]))) - 1;
 	double size;
 	int i, n;
 
@@ -777,6 +781,37 @@ process_file(struct repostead_file_info *fi,
 }
 
 static void
+tree_readme(FILE *f, const struct repostead_config *conf, const git_tree *tree)
+{
+	size_t i = 0;
+	const git_tree_entry *e = NULL;
+
+	for (i = 0; i < sizeof(readme_files) / sizeof(readme_files[0]); i++) {
+		e = git_tree_entry_byname(tree, readme_files[i]);
+		if (e && git_tree_entry_type(e) == GIT_OBJECT_BLOB) {
+			const git_oid *oid = git_tree_entry_id(e);
+			git_blob *blob = NULL;
+			const void *data = NULL;
+			git_object_size_t size = 0;
+
+			if (git_blob_lookup(&blob, conf->repo, oid) != 0) {
+				gitdie("git_blob_lookup");
+			}
+			data = git_blob_rawcontent(blob);
+			size = git_blob_rawsize(blob);
+			if (size > SIZE_MAX) {
+				die("blob too large");
+			}
+			fputs("<h2 id=\"readme\">", f);
+			html_esc(f, readme_files[i]);
+			fputs("</h2>", f);
+			print_listing(f, data, (size_t)size);
+			git_blob_free(blob);
+		}
+	}
+}
+
+static void
 process_tree(const struct repostead_config *conf,
 	const struct repostead_branch *br,
 	const git_tree *tree, const char *name, const char *path)
@@ -891,6 +926,8 @@ process_tree(const struct repostead_config *conf,
 	}
 	fputs("</table>", index_file);
 
+	tree_readme(index_file, conf, tree);
+
 	page_end(index_file, index_file_path);
 }
 
@@ -1106,6 +1143,10 @@ process_repo(struct repostead_config *conf)
 		gitdie("git_branch_next");
 	}
 
+	if (head_tree) {
+		tree_readme(index_file, conf, head_tree);
+	}
+
 	page_end(index_file, index_file_path);
 
 	if (head_tree) {