3 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "got_compat.h"
21 #include <sys/types.h>
22 #include <sys/queue.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_reference.h"
47 static int datetime(struct template *, time_t, int);
48 static int gotweb_render_blob_line(struct template *, const char *, size_t);
49 static int gotweb_render_tree_item(struct template *, struct got_tree_entry *);
50 static int blame_line(struct template *, const char *, struct blame_line *,
53 static inline int gotweb_render_more(struct template *, int);
55 static inline int diff_line(struct template *, char *);
56 static inline int tag_item(struct template *, struct repo_tag *);
57 static inline int branch(struct template *, struct got_reflist_entry *);
58 static inline int rss_tag_item(struct template *, struct repo_tag *);
59 static inline int rss_author(struct template *, char *);
63 {{ define datetime(struct template *tp, time_t t, int fmt) }}
69 if (gmtime_r(&t, &tm) == NULL)
72 if (strftime(rfc3339, sizeof(rfc3339), "%FT%TZ", &tm) == 0)
75 if (fmt != TM_DIFF && asctime_r(&tm, datebuf) == NULL)
78 <time datetime="{{ rfc3339 }}">
79 {{ if fmt == TM_DIFF }}
80 {{ render gotweb_render_age(tp, t) }}
82 {{ datebuf }} {{ " UTC" }}
87 {{ define gotweb_render_page(struct template *tp,
88 int (*body)(struct template *)) }}
90 struct request *c = tp->tp_arg;
91 struct server *srv = c->srv;
92 struct querystring *qs = c->t->qs;
93 struct gotweb_url u_path;
94 const char *prfx = c->document_uri;
95 const char *css = srv->custom_css;
97 memset(&u_path, 0, sizeof(u_path));
98 u_path.index_page = -1;
100 u_path.action = SUMMARY;
105 <meta charset="utf-8" />
106 <title>{{ srv->site_name }}</title>
107 <meta name="viewport" content="initial-scale=1.0" />
108 <meta name="msapplication-TileColor" content="#da532c" />
109 <meta name="theme-color" content="#ffffff"/>
110 <link rel="apple-touch-icon" sizes="180x180" href="{{ prfx }}apple-touch-icon.png" />
111 <link rel="icon" type="image/png" sizes="32x32" href="{{ prfx }}favicon-32x32.png" />
112 <link rel="icon" type="image/png" sizes="16x16" href="{{ prfx }}favicon-16x16.png" />
113 <link rel="manifest" href="{{ prfx }}site.webmanifest"/>
114 <link rel="mask-icon" href="{{ prfx }}safari-pinned-tab.svg" />
115 <link rel="stylesheet" type="text/css" href="{{ prfx }}{{ css }}" />
120 <a href="{{ srv->logo_url }}" target="_blank">
121 <img src="{{ prfx }}{{ srv->logo }}" />
127 <a href="?index_page={{ printf "%d", qs->index_page }}">
131 {! u_path.path = qs->path; !}
133 <a href="{{ render gotweb_render_url(tp->tp_arg, &u_path)}}">
137 {{ if qs->action != INDEX }}
138 {{ " / " }}{{ gotweb_action_name(qs->action) }}
143 {{ render body(tp) }}
145 <footer id="site_owner_wrapper">
147 {{ if srv->show_site_owner }}
148 {{ srv->site_owner }}
156 {{ define gotweb_render_error(struct template *tp) }}
158 struct request *c = tp->tp_arg;
159 struct transport *t = c->t;
161 <div id="err_content">
165 See daemon logs for details
170 {{ define gotweb_render_repo_table_hdr(struct template *tp) }}
172 struct request *c = tp->tp_arg;
173 struct server *srv = c->srv;
175 <div id="index_header">
176 <div class="index_project">
179 {{ if srv->show_repo_description }}
180 <div class="index_project_description">
184 {{ if srv->show_repo_owner }}
185 <div class="index_project_owner">
189 {{ if srv->show_repo_age }}
190 <div class="index_project_age">
197 {{ define gotweb_render_repo_fragment(struct template *tp, struct repo_dir *repo_dir) }}
199 struct request *c = tp->tp_arg;
200 struct server *srv = c->srv;
201 struct gotweb_url summary = {
205 .path = repo_dir->name,
210 .path = repo_dir->name,
215 .path = repo_dir->name,
220 .path = repo_dir->name,
225 .path = repo_dir->name,
230 .path = repo_dir->name,
233 <div class="index_wrapper">
234 <div class="index_project">
235 <a href="{{ render gotweb_render_url(tp->tp_arg, &summary) }}">{{ repo_dir->name }}</a>
237 {{ if srv->show_repo_description }}
238 <div class="index_project_description">
239 {{ repo_dir->description }}
242 {{ if srv->show_repo_owner }}
243 <div class="index_project_owner">
244 {{ repo_dir->owner }}
247 {{ if srv->show_repo_age }}
248 <div class="index_project_age">
249 {{ render datetime(tp, repo_dir->age, TM_DIFF) }}
252 <div class="navs_wrapper">
254 <a href="{{ render gotweb_render_url(tp->tp_arg, &summary) }}">summary</a>
256 <a href="{{ render gotweb_render_url(tp->tp_arg, &briefs) }}">briefs</a>
258 <a href="{{ render gotweb_render_url(tp->tp_arg, &commits) }}">commits</a>
260 <a href="{{ render gotweb_render_url(tp->tp_arg, &tags) }}">tags</a>
262 <a href="{{ render gotweb_render_url(tp->tp_arg, &tree) }}">tree</a>
264 <a href="{{ render gotweb_render_url(tp->tp_arg, &rss) }}">rss</a>
271 {{ define gotweb_render_briefs(struct template *tp) }}
273 struct request *c = tp->tp_arg;
274 struct transport *t = c->t;
275 struct querystring *qs = c->t->qs;
276 struct repo_commit *rc;
277 struct repo_dir *repo_dir = t->repo_dir;
278 struct gotweb_url diff_url, tree_url;
281 diff_url = (struct gotweb_url){
285 .path = repo_dir->name,
286 .headref = qs->headref,
288 tree_url = (struct gotweb_url){
292 .path = repo_dir->name,
293 .headref = qs->headref,
296 <header class='subtitle'>
297 <h2>Commit Briefs</h2>
299 <div id="briefs_content">
300 {{ tailq-foreach rc &t->repo_commits entry }}
302 diff_url.commit = rc->commit_id;
303 tree_url.commit = rc->commit_id;
305 tmp = strchr(rc->committer, '<');
309 tmp = strchr(rc->commit_msg, '\n');
314 <p class='brief_meta'>
315 <span class='briefs_age'>
316 {{ render datetime(tp, rc->committer_time, TM_DIFF) }}
319 <span class="briefs_author">
323 <p class="briefs_log">
324 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">
327 {{ if rc->refs_str }}
328 {{ " " }} <span class="refs_str">({{ rc->refs_str }})</span>
333 <div class="navs_wrapper">
335 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">diff</a>
337 <a href="{{ render gotweb_render_url(tp->tp_arg, &tree_url) }}">tree</a>
342 {{ render gotweb_render_more(tp, BRIEFS) }}
346 {{ define gotweb_render_more(struct template *tp, int action) }}
348 struct request *c = tp->tp_arg;
349 struct transport *t = c->t;
350 struct querystring *qs = t->qs;
351 struct gotweb_url more = {
355 .commit = t->more_id,
356 .headref = qs->headref,
361 <div id="np_wrapper">
363 <a href="{{ render gotweb_render_url(c, &more) }}">
371 {{ define gotweb_render_navs(struct template *tp) }}
373 struct request *c = tp->tp_arg;
374 struct transport *t = c->t;
375 struct gotweb_url prev, next;
376 int have_prev, have_next;
378 gotweb_get_navs(c, &prev, &have_prev, &next, &have_next);
380 <div id="np_wrapper">
383 <a href="{{ render gotweb_render_url(c, &prev) }}">
390 <a href="{{ render gotweb_render_url(c, &next) }}">
405 {{ define gotweb_render_commits(struct template *tp) }}
407 struct request *c = tp->tp_arg;
408 struct transport *t = c->t;
409 struct repo_dir *repo_dir = t->repo_dir;
410 struct repo_commit *rc;
411 struct gotweb_url diff, tree;
413 diff = (struct gotweb_url){
417 .path = repo_dir->name,
419 tree = (struct gotweb_url){
423 .path = repo_dir->name,
426 <header class="subtitle">
429 <div class="commits_content">
430 {{ tailq-foreach rc &t->repo_commits entry }}
432 diff.commit = rc->commit_id;
433 tree.commit = rc->commit_id;
435 <div class="commits_header_wrapper">
436 <dl class="commits_header">
438 <dd><code class="commit-id">{{ rc->commit_id }}</code></dd>
440 <dd>{{ rc->author }}</dd>
441 {{ if strcmp(rc->committer, rc->author) != 0 }}
443 <dd>{{ rc->committer }}</dd>
447 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
456 <div class="navs_wrapper">
458 <a href="{{ render gotweb_render_url(c, &diff) }}">diff</a>
460 <a href="{{ render gotweb_render_url(c, &tree) }}">tree</a>
465 {{ render gotweb_render_more(tp, COMMITS) }}
469 {{ define gotweb_render_blob(struct template *tp) }}
471 struct request *c = tp->tp_arg;
472 struct transport *t = c->t;
473 struct got_blob_object *blob = t->blob;
474 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
476 <header class="subtitle">
479 <div id="blob_content">
480 <div id="blob_header_wrapper">
481 <dl id="blob_header">
484 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
487 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
493 {{ render got_output_blob_by_lines(tp, blob, gotweb_render_blob_line) }}
499 {{ define gotweb_render_blob_line(struct template *tp, const char *line,
505 r = snprintf(lineno, sizeof(lineno), "%zu", no);
506 if (r < 0 || (size_t)r >= sizeof(lineno))
509 <div class="blob_line" id="line{{ lineno }}">
510 <a href="#line{{ lineno }}">{{ lineno }}</a>
511 <span class="blob_code">{{ line }}</span>
515 {{ define gotweb_render_tree(struct template *tp) }}
517 struct request *c = tp->tp_arg;
518 struct transport *t = c->t;
519 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
521 <header class='subtitle'>
524 <div id="tree_content">
525 <div id="tree_header_wrapper">
526 <dl id="tree_header">
528 <dd><code class="commit-id">{{ rc->tree_id }}</code></dd>
531 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
534 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
539 {{ render got_output_repo_tree(c, gotweb_render_tree_item) }}
544 {{ define gotweb_render_tree_item(struct template *tp,
545 struct got_tree_entry *te) }}
547 struct request *c = tp->tp_arg;
548 struct transport *t = c->t;
549 struct querystring *qs = t->qs;
550 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
551 const char *modestr = "";
556 struct gotweb_url url = {
559 .commit = rc->commit_id,
563 name = got_tree_entry_get_name(te);
564 mode = got_tree_entry_get_mode(te);
566 folder = qs->folder ? qs->folder : "";
568 if (asprintf(&dir, "%s/%s", folder, name) == -1)
579 if (got_object_tree_entry_is_submodule(te))
581 else if (S_ISLNK(mode))
583 else if (S_ISDIR(mode))
585 else if (mode & S_IXUSR)
588 <tr class="tree_wrapper">
589 {{ if S_ISDIR(mode) }}
590 <td class="tree_line" colspan=2>
591 <a href="{{ render gotweb_render_url(c, &url) }}">
592 {{ name }}{{ modestr }}
596 <td class="tree_line">
597 <a href="{{ render gotweb_render_url(c, &url) }}">
598 {{ name }}{{ modestr }}
601 <td class="tree_line_blank">
602 {! url.action = COMMITS; !}
603 <a href="{{ render gotweb_render_url(c, &url) }}">
607 {! url.action = BLAME; !}
608 <a href="{{ render gotweb_render_url(c, &url) }}">
620 {{ define gotweb_render_tags(struct template *tp) }}
622 struct request *c = tp->tp_arg;
623 struct transport *t = c->t;
624 struct querystring *qs = t->qs;
628 commit_found = qs->commit == NULL;
630 <header class='subtitle'>
633 <div id="tags_content">
634 {{ if t->tag_count == 0 }}
635 <div id="err_content">
636 This repository contains no tags
639 {{ tailq-foreach rt &t->repo_tags entry }}
640 {{ if commit_found || !strcmp(qs->commit, rt->commit_id) }}
641 {! commit_found = 1; !}
642 {{ render tag_item(tp, rt) }}
645 {{ if t->next_id || t->prev_id }}
646 {! qs->action = TAGS; !}
647 {{ render gotweb_render_navs(tp) }}
653 {{ define tag_item(struct template *tp, struct repo_tag *rt) }}
655 struct request *c = tp->tp_arg;
656 struct transport *t = c->t;
657 struct repo_dir *repo_dir = t->repo_dir;
658 char *tag_name = rt->tag_name;
659 char *msg = rt->tag_commit;
661 struct gotweb_url url = {
665 .path = repo_dir->name,
666 .commit = rt->commit_id,
669 if (strncmp(tag_name, "refs/tags/", 10) == 0)
673 nl = strchr(msg, '\n');
678 <div class="tag_age">
679 {{ render datetime(tp, rt->tagger_time, TM_DIFF) }}
681 <div class="tag_name">{{ tag_name }}</div>
682 <div class="tag_log">
683 <a href="{{ render gotweb_render_url(c, &url) }}">
687 <div class="navs_wrapper">
689 <a href="{{ render gotweb_render_url(c, &url) }}">tag</a>
691 {! url.action = BRIEFS; !}
692 <a href="{{ render gotweb_render_url(c, &url) }}">commit briefs</a>
694 {! url.action = COMMITS; !}
695 <a href="{{ render gotweb_render_url(c, &url) }}">commits</a>
701 {{ define gotweb_render_tag(struct template *tp) }}
703 struct request *c = tp->tp_arg;
704 struct transport *t = c->t;
706 const char *tag_name;
708 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
709 tag_name = rt->tag_name;
711 if (strncmp(tag_name, "refs/", 5) == 0)
714 <header class="subtitle">
717 <div id="tags_content">
718 <div id="tag_header_wrapper">
722 <code class="commit-id">{{ rt->commit_id }}</code>
724 <span class="refs_str">({{ tag_name }})</span>
727 <dd>{{ rt->tagger }}</dd>
730 {{ render datetime(tp, rt->tagger_time, TM_LONG)}}
733 <dd class="commit-msg">{{ rt->commit_msg }}</dd>
736 <pre id="tag_commit">
743 {{ define gotweb_render_diff(struct template *tp) }}
745 struct request *c = tp->tp_arg;
746 struct transport *t = c->t;
748 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
753 <header class="subtitle">
756 <div id="diff_content">
757 <div id="diff_header_wrapper">
758 <dl id="diff_header">
760 <dd><code class="commit-id">{{ rc->commit_id }}</code></dd>
762 <dd>{{ rc->author }}</dd>
763 {{ if strcmp(rc->committer, rc->author) != 0 }}
765 <dd>{{ rc->committer }}</dd>
769 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
772 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
777 {{ while (linelen = getline(&line, &linesize, fp)) != -1 }}
778 {{ render diff_line(tp, line) }}
786 {{ define diff_line(struct template *tp, char *line )}}
788 const char *color = NULL;
791 if (!strncmp(line, "-", 1))
792 color = "diff_minus";
793 else if (!strncmp(line, "+", 1))
795 else if (!strncmp(line, "@@", 2))
796 color = "diff_chunk_header";
797 else if (!strncmp(line, "commit +", 8) ||
798 !strncmp(line, "commit -", 8) ||
799 !strncmp(line, "blob +", 6) ||
800 !strncmp(line, "blob -", 6) ||
801 !strncmp(line, "file +", 6) ||
802 !strncmp(line, "file -", 6))
804 else if (!strncmp(line, "from:", 5) || !strncmp(line, "via:", 4))
805 color = "diff_author";
806 else if (!strncmp(line, "date:", 5))
809 nl = strchr(line, '\n');
813 <span class="diff_line {{ color }}">{{ line }}</span>{{"\n"}}
816 {{ define gotweb_render_branches(struct template *tp,
817 struct got_reflist_head *refs) }}
819 struct got_reflist_entry *re;
821 <header class='subtitle'>
824 <div id="branches_content">
825 {{ tailq-foreach re refs entry }}
826 {{ if !got_ref_is_symbolic(re->ref) }}
827 {{ render branch(tp, re) }}
833 {{ define branch(struct template *tp, struct got_reflist_entry *re) }}
835 const struct got_error *err;
836 struct request *c = tp->tp_arg;
837 struct querystring *qs = c->t->qs;
840 struct gotweb_url url = {
847 refname = got_ref_get_name(re->ref);
849 err = got_get_repo_age(&age, c, refname);
851 log_warnx("%s: %s", __func__, err->msg);
855 if (strncmp(refname, "refs/heads/", 11) == 0)
858 url.headref = refname;
860 <section class="branches_wrapper">
861 <div class="branches_age">
862 {{ render datetime(tp, age, TM_DIFF) }}
865 <a href="{{ render gotweb_render_url(c, &url) }}">{{ refname }}</a>
867 <div class="navs_wrapper">
869 <a href="{{ render gotweb_render_url(c, &url) }}">summary</a>
871 {! url.action = BRIEFS; !}
872 <a href="{{ render gotweb_render_url(c, &url) }}">commit briefs</a>
874 {! url.action = COMMITS; !}
875 <a href="{{ render gotweb_render_url(c, &url) }}">commits</a>
882 {{ define gotweb_render_summary(struct template *tp) }}
884 struct request *c = tp->tp_arg;
885 struct server *srv = c->srv;
886 struct transport *t = c->t;
887 struct got_reflist_head *refs = &t->refs;
889 <dl id="summary_wrapper">
890 {{ if srv->show_repo_description }}
891 <dt>Description:</dt>
892 <dd>{{ t->repo_dir->description }}</dd>
894 {{ if srv->show_repo_owner }}
896 <dd>{{ t->repo_dir->owner }}</dd>
898 {{ if srv->show_repo_age }}
899 <dt>Last Change:</dt>
901 {{ render datetime(tp, t->repo_dir->age, TM_DIFF) }}
904 {{ if srv->show_repo_cloneurl }}
906 <dd><pre class="clone-url">{{ t->repo_dir->url }}</pre></dd>
909 {{ render gotweb_render_briefs(tp) }}
910 {{ render gotweb_render_tags(tp) }}
911 {{ render gotweb_render_branches(tp, refs) }}
914 {{ define gotweb_render_blame(struct template *tp) }}
916 const struct got_error *err;
917 struct request *c = tp->tp_arg;
918 struct transport *t = c->t;
919 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
921 <header class="subtitle">
924 <div id="blame_content">
925 <div id="blame_header_wrapper">
926 <dl id="blame_header">
929 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
932 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
938 err = got_output_file_blame(c, &blame_line);
939 if (err && err->code != GOT_ERR_CANCELLED)
940 log_warnx("%s: got_output_file_blame: %s", __func__,
949 {{ define blame_line(struct template *tp, const char *line,
950 struct blame_line *bline, int lprec, int lcur) }}
952 struct request *c = tp->tp_arg;
953 struct transport *t = c->t;
954 struct repo_dir *repo_dir = t->repo_dir;
956 struct gotweb_url url = {
960 .path = repo_dir->name,
961 .commit = bline->id_str,
964 s = strchr(bline->committer, '<');
965 committer = s ? s + 1 : bline->committer;
967 s = strchr(committer, '@');
971 <div class="blame_wrapper">
972 <div class="blame_number">{{ printf "%.*d", lprec, lcur }}</div>
973 <div class="blame_hash">
974 <a href="{{ render gotweb_render_url(c, &url) }}">
975 {{ printf "%.8s", bline->id_str }}
978 <div class="blame_date">{{ bline->datebuf }}</div>
979 <div class="blame_author">{{ printf "%.9s", committer }}</div>
980 <div class="blame_code">{{ line }}</div>
984 {{ define gotweb_render_rss(struct template *tp) }}
986 struct request *c = tp->tp_arg;
987 struct server *srv = c->srv;
988 struct transport *t = c->t;
989 struct repo_dir *repo_dir = t->repo_dir;
991 struct gotweb_url summary = {
995 .path = repo_dir->name,
998 <?xml version="1.0" encoding="UTF-8"?>
999 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
1001 <title>Tags of {{ repo_dir->name }}</title>
1004 {{ render gotweb_render_absolute_url(c, &summary) }}
1007 {{ if srv->show_repo_description }}
1008 <description>{{ repo_dir->description }}</description>
1010 {{ tailq-foreach rt &t->repo_tags entry }}
1011 {{ render rss_tag_item(tp, rt) }}
1017 {{ define rss_tag_item(struct template *tp, struct repo_tag *rt) }}
1019 struct request *c = tp->tp_arg;
1020 struct transport *t = c->t;
1021 struct repo_dir *repo_dir = t->repo_dir;
1025 char *tag_name = rt->tag_name;
1026 struct gotweb_url tag = {
1030 .path = repo_dir->name,
1031 .commit = rt->commit_id,
1034 if (strncmp(tag_name, "refs/tags/", 10) == 0)
1037 if (gmtime_r(&rt->tagger_time, &tm) == NULL)
1039 r = strftime(rfc822, sizeof(rfc822), "%a, %d %b %Y %H:%M:%S GMT", &tm);
1044 <title>{{ repo_dir->name }} {{" "}} {{ tag_name }}</title>
1047 {{ render gotweb_render_absolute_url(c, &tag) }}
1051 <![CDATA[<pre>{{ rt->tag_commit }}</pre>]]>
1053 {{ render rss_author(tp, rt->tagger) }}
1054 <guid isPermaLink="false">{{ rt->commit_id }}</guid>
1061 {{ define rss_author(struct template *tp, char *author) }}
1065 /* what to do if the author name contains a paren? */
1066 if (strchr(author, '(') != NULL || strchr(author, ')') != NULL)
1069 t = strchr(author, '<');
1075 while (isspace((unsigned char)*--t))
1078 t = strchr(mail, '>');
1084 {{ mail }} {{" "}} ({{ author }})