commit 34875d49d76084b885a5ee82586965a633787bb2 from: Omar Polo via: Thomas Adam date: Fri Dec 08 12:30:59 2023 UTC gotwebd: render READMEs in the tree view ok tracey@ commit - e6c0de4de8004e5fd84119c0bc64da0e7cab9789 commit + 34875d49d76084b885a5ee82586965a633787bb2 blob - 3cffce10df8b3b0b5171ca588350df135c1cbf7f blob + b61f9292109e3986171a8d14865d128e2087e905 --- gotwebd/files/htdocs/gotwebd/gotweb.css +++ gotwebd/files/htdocs/gotwebd/gotweb.css @@ -360,6 +360,14 @@ header.subtitle h2 { .tree_line_blank { padding: 1px; width: 9.5em; +} +#tree_content h2 { + margin-top: 40px; + padding: 0 20px 0; +} +#tree_content pre { + padding: 0 20px 20px; + white-space: pre-wrap; } #diff { blob - a61c40244d702d5e9cb4633eb08abed90853f4f3 blob + ca882a110794c4d1ce6ac687786c23d5371cf6aa --- gotwebd/got_operations.c +++ gotwebd/got_operations.c @@ -704,7 +704,7 @@ got_get_repo_tags(struct request *c, size_t limit) } int -got_output_repo_tree(struct request *c, +got_output_repo_tree(struct request *c, char **readme, int (*cb)(struct template *, struct got_tree_entry *)) { const struct got_error *error = NULL; @@ -718,10 +718,13 @@ got_output_repo_tree(struct request *c, struct got_tree_object *tree = NULL; struct got_tree_entry *te; struct repo_dir *repo_dir = t->repo_dir; + const char *name; + mode_t mode; char *escaped_name = NULL, *path = NULL; int nentries, i; TAILQ_INIT(&refs); + *readme = NULL; rc = TAILQ_FIRST(&t->repo_commits); @@ -758,6 +761,16 @@ got_output_repo_tree(struct request *c, for (i = 0; i < nentries; i++) { te = got_object_tree_get_entry(tree, i); + + name = got_tree_entry_get_name(te); + mode = got_tree_entry_get_mode(te); + if (!S_ISDIR(mode) && (!strcasecmp(name, "README") || + !strcasecmp(name, "README.md") || + !strcasecmp(name, "README.txt"))) { + free(*readme); + *readme = strdup(name); + } + if (cb(c->tp, te) == -1) { error = got_error(GOT_ERR_CANCELLED); break; @@ -774,6 +787,8 @@ got_output_repo_tree(struct request *c, free(commit_id); free(tree_id); if (error) { + free(*readme); + *readme = NULL; if (error->code != GOT_ERR_CANCELLED) log_warnx("%s: %s", __func__, error->msg); return -1; @@ -783,12 +798,11 @@ got_output_repo_tree(struct request *c, const struct got_error * got_open_blob_for_output(struct got_blob_object **blob, int *fd, - int *binary, struct request *c) + int *binary, struct request *c, const char *directory, const char *file, + const char *commitstr) { const struct got_error *error = NULL; - struct transport *t = c->t; - struct got_repository *repo = t->repo; - struct querystring *qs = c->t->qs; + struct got_repository *repo = c->t->repo; struct got_commit_object *commit = NULL; struct got_object_id *commit_id = NULL; struct got_reflist_head refs; @@ -801,8 +815,13 @@ got_open_blob_for_output(struct got_blob_object **blob *fd = -1; *binary = 0; - if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "", - qs->folder ? "/" : "", qs->file) == -1) { + error = got_ref_list(&refs, repo, "refs/heads", + got_ref_cmp_by_name, NULL); + if (error) + goto done; + + if (asprintf(&path, "%s%s%s", directory ? directory : "", + directory ? "/" : "", file) == -1) { error = got_error_from_errno("asprintf"); goto done; } @@ -811,7 +830,10 @@ got_open_blob_for_output(struct got_blob_object **blob if (error) goto done; - error = got_repo_match_object_id(&commit_id, NULL, qs->commit, + if (commitstr == NULL) + commitstr = GOT_REF_HEAD; + + error = got_repo_match_object_id(&commit_id, NULL, commitstr, GOT_OBJ_TYPE_COMMIT, &refs, repo); if (error) goto done; @@ -863,6 +885,7 @@ got_open_blob_for_output(struct got_blob_object **blob *blob = NULL; } + got_ref_list_free(&refs); free(in_repo_path); free(commit_id); free(path); blob - 06ca65abbaad14c30a35126368ccc1133817c47c blob + c87440d5f88d32ddddc7ed30ed8a81d9c1f718b3 --- gotwebd/gotweb.c +++ gotwebd/gotweb.c @@ -211,7 +211,7 @@ gotweb_process_request(struct request *c) goto err; error = got_open_blob_for_output(&c->t->blob, &c->t->fd, - &binary, c); + &binary, c, qs->folder, qs->file, qs->commit); if (error) goto err; } blob - 0f8dabd8f5dbe97f51673c139c7ac29805124338 blob + e91974fe33e609ba0ca666d09c9df5b3956cfee4 --- gotwebd/gotwebd.h +++ gotwebd/gotwebd.h @@ -520,10 +520,10 @@ const struct got_error *got_get_repo_commits(struct re const struct got_error *got_get_repo_tags(struct request *, size_t); const struct got_error *got_get_repo_heads(struct request *); const struct got_error *got_open_diff_for_output(FILE **, struct request *); -int got_output_repo_tree(struct request *, +int got_output_repo_tree(struct request *, char **, int (*)(struct template *, struct got_tree_entry *)); const struct got_error *got_open_blob_for_output(struct got_blob_object **, - int *, int *, struct request *); + int *, int *, struct request *, const char *, const char *, const char *); int got_output_blob_by_lines(struct template *, struct got_blob_object *, int (*)(struct template *, const char *, size_t)); const struct got_error *got_output_file_blame(struct request *, blob - 1cff3e763111d70c8c50d451db284e850f8bd147 blob + de6548a91c86eca52e290437016e14b1cc46a059 --- gotwebd/pages.tmpl +++ gotwebd/pages.tmpl @@ -643,9 +643,16 @@ nextsep(char *s, char **t) {{ define gotweb_render_tree(struct template *tp) }} {! + const struct got_error *error; struct request *c = tp->tp_arg; struct transport *t = c->t; + struct querystring *qs = t->qs; struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits); + struct gotweb_url url; + char *readme = NULL; + int binary; + const uint8_t *buf; + size_t len; !}

Tree

@@ -665,9 +672,55 @@ nextsep(char *s, char **t)
- {{ render got_output_repo_tree(c, gotweb_render_tree_item) }} + {{ render got_output_repo_tree(c, &readme, gotweb_render_tree_item) }}
+ {{ if readme }} + {! + error = got_open_blob_for_output(&t->blob, &t->fd, &binary, c, + qs->folder, readme, qs->commit); + if (error) { + free(readme); + return (-1); + } + + memset(&url, 0, sizeof(url)); + url.index_page = -1; + url.page = -1; + url.action = BLOB; + url.path = t->qs->path; + url.file = readme; + url.folder = t->qs->folder; + url.commit = t->qs->commit; + !} + {{ if !binary }} +

+ + {{ readme }} + +

+
+        {!
+		for (;;) {
+			error = got_object_blob_read_block(&len, t->blob);
+			if (error) {
+				free(readme);
+				return (-1);
+			}
+			if (len == 0)
+				break;
+			buf = got_object_blob_get_read_buf(t->blob);
+			if (tp_write_htmlescape(tp, buf, len) == -1) {
+				free(readme);
+				return (-1);
+			}
+		}
+        !}
+      
+ {{ end }} + {{ end }} +{{ finally }} +{! free(readme); !} {{ end }} {{ define gotweb_render_tree_item(struct template *tp,