commit d6aafba8ef79a513680838efe5739bb56e116c3d from: Omar Polo date: Fri Dec 01 16:51:48 2023 UTC change got_open_blob_for_output to not look at the querystring the implicit dependency on the various variables inside the querystring makes reusing the code in got_operations.c a bit harder. Accessing a blob in the tree view for instance requires a few workaround. Instead, just take the folder^W directory, file and commit id as arguments, so they can be easily changed without hacks in the proper places. While here, support passing a NULL commit to mean "HEAD". commit - 17e0f8afea3d95e8055318764a21d75fd8acb938 commit + d6aafba8ef79a513680838efe5739bb56e116c3d blob - 667de1ed8c7587e8a3413be3fbbca4f143b31d90 blob + 2a53abe53d796a041d06addd056ad983393fc759 --- gotwebd/got_operations.c +++ gotwebd/got_operations.c @@ -798,12 +798,11 @@ got_output_repo_tree(struct request *c, char **readme, 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; @@ -816,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; } @@ -826,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 = "HEAD"; + + error = got_repo_match_object_id(&commit_id, NULL, commitstr, GOT_OBJ_TYPE_COMMIT, &refs, repo); if (error) goto done; @@ -878,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 - 5a9e5219a1429b6a682151e03ff69c6f506f26b2 blob + 48e5696942f0483cf0bac6ae07df6e03d218ee3e --- gotwebd/gotweb.c +++ gotwebd/gotweb.c @@ -212,7 +212,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 - c480587a135af9f8131850e847e558875813395f blob + 4efd134dfaaf502699cb7769db9b07549b2ee41b --- gotwebd/gotwebd.h +++ gotwebd/gotwebd.h @@ -521,7 +521,7 @@ const struct got_error *got_open_diff_for_output(FILE 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 - 71b947250c088a0e8583d92979d83beeb5f851d2 blob + ccfb72d73ff3acccb9daee5236e0ef30544a3d88 --- gotwebd/pages.tmpl +++ gotwebd/pages.tmpl @@ -660,6 +660,7 @@ nextsep(char *s, char **t) 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; @@ -687,14 +688,10 @@ nextsep(char *s, char **t) {{ render got_output_repo_tree(c, &readme, gotweb_render_tree_item) }}
- {!/* a commit id is *required* */!} - {{ if readme && t->qs->commit }} + {{ if readme }} {! - /* sigh... */ - t->qs->file = readme; - error = got_open_blob_for_output(&t->blob, &t->fd, &binary, c); - t->qs->file = NULL; - + error = got_open_blob_for_output(&t->blob, &t->fd, &binary, c, + qs->folder, readme, qs->commit); if (error) { free(readme); return (-1);