From: Omar Polo Subject: gotwebd: templateify gotweb_render_branches To: gameoftrees@openbsd.org Date: Wed, 11 Jan 2023 14:04:53 +0100 as usual, no functional change intended. my instance is running with this applied. The allocation of the reflist is moved to from gotweb_render_branches to gotweb_render_summary (its only caller.) We're almost done! With this only gotweb_render_summary and blame remains to convert still. diff /home/op/w/got commit - 5fa52d6cdcc6de872ce3c7f6639cc638fd83eeea path + /home/op/w/got blob - 539e06b6f8271d8b51ac64fbfe665a5ad7a4a1ae file + gotwebd/gotweb.c --- gotwebd/gotweb.c +++ gotwebd/gotweb.c @@ -96,7 +96,6 @@ static const struct got_error *gotweb_render_branches( static const struct got_error *gotweb_render_blame(struct request *); static const struct got_error *gotweb_render_summary(struct request *); static const struct got_error *gotweb_render_tags(struct request *); -static const struct got_error *gotweb_render_branches(struct request *); static void gotweb_free_querystring(struct querystring *); static void gotweb_free_repo_dir(struct repo_dir *); @@ -1012,16 +1011,13 @@ gotweb_render_branches(struct request *c) } static const struct got_error * -gotweb_render_branches(struct request *c) +gotweb_render_summary(struct request *c) { const struct got_error *error = NULL; struct got_reflist_head refs; - struct got_reflist_entry *re; struct transport *t = c->t; - struct querystring *qs = t->qs; struct got_repository *repo = t->repo; - char *escaped_refname = NULL; - char *age = NULL; + struct server *srv = c->srv; int r; TAILQ_INIT(&refs); @@ -1031,123 +1027,6 @@ gotweb_render_branches(struct request *c) if (error) goto done; - r = fcgi_printf(c, "
\n" - "
Branches
\n" - "
\n" /* #branches_title_wrapper */ - "
\n"); - if (r == -1) - goto done; - - TAILQ_FOREACH(re, &refs, entry) { - const char *refname = NULL; - - if (got_ref_is_symbolic(re->ref)) - continue; - - refname = got_ref_get_name(re->ref); - if (refname == NULL) { - error = got_error_from_errno("strdup"); - goto done; - } - if (strncmp(refname, "refs/heads/", 11) != 0) - continue; - - error = got_get_repo_age(&age, c, refname, TM_DIFF); - if (error) - goto done; - - if (strncmp(refname, "refs/heads/", 11) == 0) - refname += 11; - error = gotweb_escape_html(&escaped_refname, refname); - if (error) - goto done; - - r = fcgi_printf(c, "
\n" - "
%s
\n" - "
 
\n" - "
", age); - if (r == -1) - goto done; - - r = gotweb_link(c, &(struct gotweb_url){ - .action = SUMMARY, - .index_page = -1, - .page = -1, - .path = qs->path, - .headref = refname, - }, "%s", escaped_refname); - if (r == -1) - goto done; - - if (fcgi_printf(c, "
\n" /* .branch */ - "\n" /* .navs_wrapper */ - "
\n" - "
\n"); /* .branches_wrapper */ - if (r == -1) - goto done; - - free(age); - age = NULL; - free(escaped_refname); - escaped_refname = NULL; - } - fcgi_printf(c, "
\n"); /* #branches_content */ -done: - free(age); - free(escaped_refname); - got_ref_list_free(&refs); - return error; -} - -static const struct got_error * -gotweb_render_summary(struct request *c) -{ - const struct got_error *error = NULL; - struct transport *t = c->t; - struct server *srv = c->srv; - int r; - if (fcgi_printf(c, "
\n") == -1) goto done; @@ -1200,10 +1079,9 @@ gotweb_render_summary(struct request *c) goto done; } - error = gotweb_render_branches(c); - if (error) - log_warnx("%s: %s", __func__, error->msg); + gotweb_render_branches(c->tp, &refs); done: + got_ref_list_free(&refs); return error; } blob - e19e34593996941a09c53036aa3d995fb6867118 file + gotwebd/gotwebd.h --- gotwebd/gotwebd.h +++ gotwebd/gotwebd.h @@ -118,6 +118,7 @@ struct got_tree_entry; /* Forward declaration */ struct got_blob_object; struct got_tree_entry; +struct got_reflist_head; enum imsg_type { IMSG_CFG_SRV = IMSG_PROC_MAX, @@ -472,6 +473,7 @@ int gotweb_render_rss(struct template *); int gotweb_render_tags_tmpl(struct template *); int gotweb_render_tag(struct template *); int gotweb_render_diff(struct template *, FILE *); +int gotweb_render_branches(struct template *, struct got_reflist_head *); int gotweb_render_rss(struct template *); /* parse.y */ blob - 3501ce16a0c13174e3e6a1d4fc06358a7b345061 file + gotwebd/pages.tmpl --- gotwebd/pages.tmpl +++ gotwebd/pages.tmpl @@ -29,7 +29,9 @@ #include #include +#include "got_error.h" #include "got_object.h" +#include "got_reference.h" #include "proc.h" @@ -41,6 +43,7 @@ static inline int rss_tag_item(struct template *, stru static inline int diff_line(struct template *, char *); static inline int tag_item(struct template *, struct repo_tag *); +static inline int branch(struct template *, struct got_reflist_entry *); static inline int rss_tag_item(struct template *, struct repo_tag *); static inline int rss_author(struct template *, char *); @@ -770,6 +773,73 @@ gotweb_render_age(struct template *tp, time_t time, in
{{ line }}
{{ end }} +{{ define gotweb_render_branches(struct template *tp, + struct got_reflist_head *refs) }} +{! + struct got_reflist_entry *re; +!} +
+
Branches
+
+
+ {{ tailq-foreach re refs entry }} + {{ if !got_ref_is_symbolic(re->ref) }} + {{ render branch(tp, re) }} + {{ end }} + {{ end }} +
+{{ end }} + +{{ define branch(struct template *tp, struct got_reflist_entry *re) }} +{! + const struct got_error *err; + struct request *c = tp->tp_arg; + struct querystring *qs = c->t->qs; + const char *refname; + char *age = NULL; + struct gotweb_url url = { + .action = SUMMARY, + .index_page = -1, + .page = -1, + .path = qs->path, + }; + + refname = got_ref_get_name(re->ref); + + err = got_get_repo_age(&age, c, refname, TM_DIFF); + if (err) { + log_warnx("%s: %s", __func__, err->msg); + return -1; + } + + if (strncmp(refname, "refs/heads/", 11) == 0) + refname += 11; + + url.headref = refname; +!} +
+
{{ age }}
+
 
+ + +
+
+{{ finally }} +{! free(age); !} +{{ end }} + {{ define gotweb_render_rss(struct template *tp) }} {! struct request *c = tp->tp_arg;