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"
42 static int gotweb_render_blob_line(struct template *, const char *, size_t);
43 static int gotweb_render_tree_item(struct template *, struct got_tree_entry *);
44 static int blame_line(struct template *, const char *, struct blame_line *,
47 static inline int gotweb_render_more(struct template *, int);
49 static inline int diff_line(struct template *, char *);
50 static inline int tag_item(struct template *, struct repo_tag *);
51 static inline int branch(struct template *, struct got_reflist_entry *);
52 static inline int rss_tag_item(struct template *, struct repo_tag *);
53 static inline int rss_author(struct template *, char *);
57 {{ define gotweb_render_page(struct template *tp,
58 int (*body)(struct template *)) }}
60 struct request *c = tp->tp_arg;
61 struct server *srv = c->srv;
62 struct querystring *qs = c->t->qs;
63 struct gotweb_url u_path;
64 const char *prfx = c->document_uri;
65 const char *css = srv->custom_css;
67 memset(&u_path, 0, sizeof(u_path));
68 u_path.index_page = -1;
70 u_path.action = SUMMARY;
75 <meta charset="utf-8" />
76 <title>{{ srv->site_name }}</title>
77 <meta name="viewport" content="initial-scale=1.0" />
78 <meta name="msapplication-TileColor" content="#da532c" />
79 <meta name="theme-color" content="#ffffff"/>
80 <link rel="apple-touch-icon" sizes="180x180" href="{{ prfx }}apple-touch-icon.png" />
81 <link rel="icon" type="image/png" sizes="32x32" href="{{ prfx }}favicon-32x32.png" />
82 <link rel="icon" type="image/png" sizes="16x16" href="{{ prfx }}favicon-16x16.png" />
83 <link rel="manifest" href="{{ prfx }}site.webmanifest"/>
84 <link rel="mask-icon" href="{{ prfx }}safari-pinned-tab.svg" />
85 <link rel="stylesheet" type="text/css" href="{{ prfx }}{{ css }}" />
90 <a href="{{ srv->logo_url }}" target="_blank">
91 <img src="{{ prfx }}{{ srv->logo }}" />
97 <a href="?index_page={{ printf "%d", qs->index_page }}">
101 {! u_path.path = qs->path; !}
103 <a href="{{ render gotweb_render_url(tp->tp_arg, &u_path)}}">
107 {{ if qs->action != INDEX }}
108 {{ " / " }}{{ gotweb_action_name(qs->action) }}
113 {{ render body(tp) }}
115 <footer id="site_owner_wrapper">
117 {{ if srv->show_site_owner }}
118 {{ srv->site_owner }}
126 {{ define gotweb_render_error(struct template *tp) }}
128 struct request *c = tp->tp_arg;
129 struct transport *t = c->t;
131 <div id="err_content">
135 See daemon logs for details
140 {{ define gotweb_render_repo_table_hdr(struct template *tp) }}
142 struct request *c = tp->tp_arg;
143 struct server *srv = c->srv;
145 <div id="index_header">
146 <div class="index_project">
149 {{ if srv->show_repo_description }}
150 <div class="index_project_description">
154 {{ if srv->show_repo_owner }}
155 <div class="index_project_owner">
159 {{ if srv->show_repo_age }}
160 <div class="index_project_age">
167 {{ define gotweb_render_repo_fragment(struct template *tp, struct repo_dir *repo_dir) }}
169 struct request *c = tp->tp_arg;
170 struct server *srv = c->srv;
171 struct gotweb_url summary = {
175 .path = repo_dir->name,
180 .path = repo_dir->name,
185 .path = repo_dir->name,
190 .path = repo_dir->name,
195 .path = repo_dir->name,
200 .path = repo_dir->name,
203 <div class="index_wrapper">
204 <div class="index_project">
205 <a href="{{ render gotweb_render_url(tp->tp_arg, &summary) }}">{{ repo_dir->name }}</a>
207 {{ if srv->show_repo_description }}
208 <div class="index_project_description">
209 {{ repo_dir->description }}
212 {{ if srv->show_repo_owner }}
213 <div class="index_project_owner">
214 {{ repo_dir->owner }}
217 {{ if srv->show_repo_age }}
218 <div class="index_project_age">
219 {{ render gotweb_render_age(tp, repo_dir->age, TM_DIFF) }}
222 <div class="navs_wrapper">
224 <a href="{{ render gotweb_render_url(tp->tp_arg, &summary) }}">summary</a>
226 <a href="{{ render gotweb_render_url(tp->tp_arg, &briefs) }}">briefs</a>
228 <a href="{{ render gotweb_render_url(tp->tp_arg, &commits) }}">commits</a>
230 <a href="{{ render gotweb_render_url(tp->tp_arg, &tags) }}">tags</a>
232 <a href="{{ render gotweb_render_url(tp->tp_arg, &tree) }}">tree</a>
234 <a href="{{ render gotweb_render_url(tp->tp_arg, &rss) }}">rss</a>
241 {{ define gotweb_render_briefs(struct template *tp) }}
243 struct request *c = tp->tp_arg;
244 struct transport *t = c->t;
245 struct querystring *qs = c->t->qs;
246 struct repo_commit *rc;
247 struct repo_dir *repo_dir = t->repo_dir;
248 struct gotweb_url diff_url, tree_url;
251 diff_url = (struct gotweb_url){
255 .path = repo_dir->name,
256 .headref = qs->headref,
258 tree_url = (struct gotweb_url){
262 .path = repo_dir->name,
263 .headref = qs->headref,
266 <header class='subtitle'>
267 <h2>Commit Briefs</h2>
269 <div id="briefs_content">
270 {{ tailq-foreach rc &t->repo_commits entry }}
272 diff_url.commit = rc->commit_id;
273 tree_url.commit = rc->commit_id;
275 tmp = strchr(rc->committer, '<');
279 tmp = strchr(rc->commit_msg, '\n');
284 <p class='brief_meta'>
285 <span class='briefs_age'>
286 {{ render gotweb_render_age(tp, rc->committer_time, TM_DIFF) }}
289 <span class="briefs_author">
293 <p class="briefs_log">
294 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">
297 {{ if rc->refs_str }}
298 {{ " " }} <span class="refs_str">({{ rc->refs_str }})</span>
303 <div class="navs_wrapper">
305 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">diff</a>
307 <a href="{{ render gotweb_render_url(tp->tp_arg, &tree_url) }}">tree</a>
312 {{ render gotweb_render_more(tp, BRIEFS) }}
316 {{ define gotweb_render_more(struct template *tp, int action) }}
318 struct request *c = tp->tp_arg;
319 struct transport *t = c->t;
320 struct querystring *qs = t->qs;
321 struct gotweb_url more = {
325 .commit = t->more_id,
326 .headref = qs->headref,
331 <div id="np_wrapper">
333 <a href="{{ render gotweb_render_url(c, &more) }}">
341 {{ define gotweb_render_navs(struct template *tp) }}
343 struct request *c = tp->tp_arg;
344 struct transport *t = c->t;
345 struct gotweb_url prev, next;
346 int have_prev, have_next;
348 gotweb_get_navs(c, &prev, &have_prev, &next, &have_next);
350 <div id="np_wrapper">
353 <a href="{{ render gotweb_render_url(c, &prev) }}">
360 <a href="{{ render gotweb_render_url(c, &next) }}">
375 {{ define gotweb_render_commits(struct template *tp) }}
377 struct request *c = tp->tp_arg;
378 struct transport *t = c->t;
379 struct repo_dir *repo_dir = t->repo_dir;
380 struct repo_commit *rc;
381 struct gotweb_url diff, tree;
383 diff = (struct gotweb_url){
387 .path = repo_dir->name,
389 tree = (struct gotweb_url){
393 .path = repo_dir->name,
396 <header class="subtitle">
399 <div class="commits_content">
400 {{ tailq-foreach rc &t->repo_commits entry }}
402 diff.commit = rc->commit_id;
403 tree.commit = rc->commit_id;
405 <div class="commits_header_wrapper">
406 <dl class="commits_header">
408 <dd><code class="commit-id">{{ rc->commit_id }}</code></dd>
410 <dd>{{ rc->author }}</dd>
411 {{ if strcmp(rc->committer, rc->author) != 0 }}
413 <dd>{{ rc->committer }}</dd>
417 {{ render gotweb_render_age(tp, rc->committer_time, TM_LONG) }}
426 <div class="navs_wrapper">
428 <a href="{{ render gotweb_render_url(c, &diff) }}">diff</a>
430 <a href="{{ render gotweb_render_url(c, &tree) }}">tree</a>
435 {{ render gotweb_render_more(tp, COMMITS) }}
439 {{ define gotweb_render_blob(struct template *tp) }}
441 struct request *c = tp->tp_arg;
442 struct transport *t = c->t;
443 struct got_blob_object *blob = t->blob;
444 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
446 <header class="subtitle">
449 <div id="blob_content">
450 <div id="blob_header_wrapper">
451 <dl id="blob_header">
454 {{ render gotweb_render_age(tp, rc->committer_time, TM_LONG) }}
457 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
463 {{ render got_output_blob_by_lines(tp, blob, gotweb_render_blob_line) }}
469 {{ define gotweb_render_blob_line(struct template *tp, const char *line,
475 r = snprintf(lineno, sizeof(lineno), "%zu", no);
476 if (r < 0 || (size_t)r >= sizeof(lineno))
479 <div class="blob_line" id="line{{ lineno }}">
480 <a href="#line{{ lineno }}">{{ lineno }}</a>
481 <span class="blob_code">{{ line }}</span>
485 {{ define gotweb_render_tree(struct template *tp) }}
487 struct request *c = tp->tp_arg;
488 struct transport *t = c->t;
489 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
491 <header class='subtitle'>
494 <div id="tree_content">
495 <div id="tree_header_wrapper">
496 <dl id="tree_header">
498 <dd><code class="commit-id">{{ rc->tree_id }}</code></dd>
501 {{ render gotweb_render_age(tp, rc->committer_time, TM_LONG) }}
504 <dd class="commit-msg">{{ rc->commit_msg }}</d>
509 {{ render got_output_repo_tree(c, gotweb_render_tree_item) }}
514 {{ define gotweb_render_tree_item(struct template *tp,
515 struct got_tree_entry *te) }}
517 struct request *c = tp->tp_arg;
518 struct transport *t = c->t;
519 struct querystring *qs = t->qs;
520 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
521 const char *modestr = "";
526 struct gotweb_url url = {
529 .commit = rc->commit_id,
533 name = got_tree_entry_get_name(te);
534 mode = got_tree_entry_get_mode(te);
536 folder = qs->folder ? qs->folder : "";
538 if (asprintf(&dir, "%s/%s", folder, name) == -1)
549 if (got_object_tree_entry_is_submodule(te))
551 else if (S_ISLNK(mode))
553 else if (S_ISDIR(mode))
555 else if (mode & S_IXUSR)
558 <tr class="tree_wrapper">
559 {{ if S_ISDIR(mode) }}
560 <td class="tree_line" colspan=2>
561 <a href="{{ render gotweb_render_url(c, &url) }}">
562 {{ name }}{{ modestr }}
566 <td class="tree_line">
567 <a href="{{ render gotweb_render_url(c, &url) }}">
568 {{ name }}{{ modestr }}
571 <td class="tree_line_blank">
572 {! url.action = COMMITS; !}
573 <a href="{{ render gotweb_render_url(c, &url) }}">
577 {! url.action = BLAME; !}
578 <a href="{{ render gotweb_render_url(c, &url) }}">
590 {{ define gotweb_render_tags(struct template *tp) }}
592 struct request *c = tp->tp_arg;
593 struct transport *t = c->t;
594 struct querystring *qs = t->qs;
598 commit_found = qs->commit == NULL;
600 <header class='subtitle'>
603 <div id="tags_content">
604 {{ if t->tag_count == 0 }}
605 <div id="err_content">
606 This repository contains no tags
609 {{ tailq-foreach rt &t->repo_tags entry }}
610 {{ if commit_found || !strcmp(qs->commit, rt->commit_id) }}
611 {! commit_found = 1; !}
612 {{ render tag_item(tp, rt) }}
615 {{ if t->next_id || t->prev_id }}
616 {! qs->action = TAGS; !}
617 {{ render gotweb_render_navs(tp) }}
623 {{ define tag_item(struct template *tp, struct repo_tag *rt) }}
625 struct request *c = tp->tp_arg;
626 struct transport *t = c->t;
627 struct repo_dir *repo_dir = t->repo_dir;
628 char *tag_name = rt->tag_name;
629 char *msg = rt->tag_commit;
631 struct gotweb_url url = {
635 .path = repo_dir->name,
636 .commit = rt->commit_id,
639 if (strncmp(tag_name, "refs/tags/", 10) == 0)
643 nl = strchr(msg, '\n');
648 <div class="tag_age">
649 {{ render gotweb_render_age(tp, rt->tagger_time, TM_DIFF) }}
651 <div class="tag_name">{{ tag_name }}</div>
652 <div class="tag_log">
653 <a href="{{ render gotweb_render_url(c, &url) }}">
657 <div class="navs_wrapper">
659 <a href="{{ render gotweb_render_url(c, &url) }}">tag</a>
661 {! url.action = BRIEFS; !}
662 <a href="{{ render gotweb_render_url(c, &url) }}">commit briefs</a>
664 {! url.action = COMMITS; !}
665 <a href="{{ render gotweb_render_url(c, &url) }}">commits</a>
671 {{ define gotweb_render_tag(struct template *tp) }}
673 struct request *c = tp->tp_arg;
674 struct transport *t = c->t;
676 const char *tag_name;
678 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
679 tag_name = rt->tag_name;
681 if (strncmp(tag_name, "refs/", 5) == 0)
684 <header class="subtitle">
687 <div id="tags_content">
688 <div id="tag_header_wrapper">
692 <code class="commit-id">{{ rt->commit_id }}</code>
694 <span class="refs_str">({{ tag_name }})</span>
697 <dd>{{ rt->tagger }}</dd>
700 {{ render gotweb_render_age(tp, rt->tagger_time, TM_LONG)}}
703 <dd class="commit-msg">{{ rt->commit_msg }}</dd>
706 <pre id="tag_commit">
713 {{ define gotweb_render_diff(struct template *tp) }}
715 struct request *c = tp->tp_arg;
716 struct transport *t = c->t;
718 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
723 <header class="subtitle">
726 <div id="diff_content">
727 <div id="diff_header_wrapper">
728 <dl id="diff_header">
730 <dd><code class="commit-id">{{ rc->commit_id }}</code></dd>
732 <dd>{{ rc->author }}</dd>
733 {{ if strcmp(rc->committer, rc->author) != 0 }}
735 <dd>{{ rc->committer }}</dd>
739 {{ render gotweb_render_age(tp, rc->committer_time, TM_LONG) }}
742 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
747 {{ while (linelen = getline(&line, &linesize, fp)) != -1 }}
748 {{ render diff_line(tp, line) }}
756 {{ define diff_line(struct template *tp, char *line )}}
758 const char *color = NULL;
761 if (!strncmp(line, "-", 1))
762 color = "diff_minus";
763 else if (!strncmp(line, "+", 1))
765 else if (!strncmp(line, "@@", 2))
766 color = "diff_chunk_header";
767 else if (!strncmp(line, "commit +", 8) ||
768 !strncmp(line, "commit -", 8) ||
769 !strncmp(line, "blob +", 6) ||
770 !strncmp(line, "blob -", 6) ||
771 !strncmp(line, "file +", 6) ||
772 !strncmp(line, "file -", 6))
774 else if (!strncmp(line, "from:", 5) || !strncmp(line, "via:", 4))
775 color = "diff_author";
776 else if (!strncmp(line, "date:", 5))
779 nl = strchr(line, '\n');
783 <span class="diff_line {{ color }}">{{ line }}</span>{{"\n"}}
786 {{ define gotweb_render_branches(struct template *tp,
787 struct got_reflist_head *refs) }}
789 struct got_reflist_entry *re;
791 <header class='subtitle'>
794 <div id="branches_content">
795 {{ tailq-foreach re refs entry }}
796 {{ if !got_ref_is_symbolic(re->ref) }}
797 {{ render branch(tp, re) }}
803 {{ define branch(struct template *tp, struct got_reflist_entry *re) }}
805 const struct got_error *err;
806 struct request *c = tp->tp_arg;
807 struct querystring *qs = c->t->qs;
810 struct gotweb_url url = {
817 refname = got_ref_get_name(re->ref);
819 err = got_get_repo_age(&age, c, refname);
821 log_warnx("%s: %s", __func__, err->msg);
825 if (strncmp(refname, "refs/heads/", 11) == 0)
828 url.headref = refname;
830 <section class="branches_wrapper">
831 <div class="branches_age">
832 {{ render gotweb_render_age(tp, age, TM_DIFF) }}
835 <a href="{{ render gotweb_render_url(c, &url) }}">{{ refname }}</a>
837 <div class="navs_wrapper">
839 <a href="{{ render gotweb_render_url(c, &url) }}">summary</a>
841 {! url.action = BRIEFS; !}
842 <a href="{{ render gotweb_render_url(c, &url) }}">commit briefs</a>
844 {! url.action = COMMITS; !}
845 <a href="{{ render gotweb_render_url(c, &url) }}">commits</a>
852 {{ define gotweb_render_summary(struct template *tp) }}
854 struct request *c = tp->tp_arg;
855 struct server *srv = c->srv;
856 struct transport *t = c->t;
857 struct got_reflist_head *refs = &t->refs;
859 <dl id="summary_wrapper">
860 {{ if srv->show_repo_description }}
861 <dt>Description:</dt>
862 <dd>{{ t->repo_dir->description }}</dd>
864 {{ if srv->show_repo_owner }}
866 <dd>{{ t->repo_dir->owner }}</dd>
868 {{ if srv->show_repo_age }}
869 <dt>Last Change:</dt>
871 {{ render gotweb_render_age(tp, t->repo_dir->age, TM_DIFF) }}
874 {{ if srv->show_repo_cloneurl }}
876 <dd><pre class="clone-url">{{ t->repo_dir->url }}</pre></dd>
879 {{ render gotweb_render_briefs(tp) }}
880 {{ render gotweb_render_tags(tp) }}
881 {{ render gotweb_render_branches(tp, refs) }}
884 {{ define gotweb_render_blame(struct template *tp) }}
886 const struct got_error *err;
887 struct request *c = tp->tp_arg;
888 struct transport *t = c->t;
889 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
891 <header class="subtitle">
894 <div id="blame_content">
895 <div id="blame_header_wrapper">
896 <dl id="blame_header">
899 {{ render gotweb_render_age(tp, rc->committer_time, TM_LONG) }}
902 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
908 err = got_output_file_blame(c, &blame_line);
909 if (err && err->code != GOT_ERR_CANCELLED)
910 log_warnx("%s: got_output_file_blame: %s", __func__,
919 {{ define blame_line(struct template *tp, const char *line,
920 struct blame_line *bline, int lprec, int lcur) }}
922 struct request *c = tp->tp_arg;
923 struct transport *t = c->t;
924 struct repo_dir *repo_dir = t->repo_dir;
926 struct gotweb_url url = {
930 .path = repo_dir->name,
931 .commit = bline->id_str,
934 s = strchr(bline->committer, '<');
935 committer = s ? s + 1 : bline->committer;
937 s = strchr(committer, '@');
941 <div class="blame_wrapper">
942 <div class="blame_number">{{ printf "%.*d", lprec, lcur }}</div>
943 <div class="blame_hash">
944 <a href="{{ render gotweb_render_url(c, &url) }}">
945 {{ printf "%.8s", bline->id_str }}
948 <div class="blame_date">{{ bline->datebuf }}</div>
949 <div class="blame_author">{{ printf "%.9s", committer }}</div>
950 <div class="blame_code">{{ line }}</div>
954 {{ define gotweb_render_rss(struct template *tp) }}
956 struct request *c = tp->tp_arg;
957 struct server *srv = c->srv;
958 struct transport *t = c->t;
959 struct repo_dir *repo_dir = t->repo_dir;
961 struct gotweb_url summary = {
965 .path = repo_dir->name,
968 <?xml version="1.0" encoding="UTF-8"?>
969 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
971 <title>Tags of {{ repo_dir->name }}</title>
974 {{ render gotweb_render_absolute_url(c, &summary) }}
977 {{ if srv->show_repo_description }}
978 <description>{{ repo_dir->description }}</description>
980 {{ tailq-foreach rt &t->repo_tags entry }}
981 {{ render rss_tag_item(tp, rt) }}
987 {{ define rss_tag_item(struct template *tp, struct repo_tag *rt) }}
989 struct request *c = tp->tp_arg;
990 struct transport *t = c->t;
991 struct repo_dir *repo_dir = t->repo_dir;
992 char *tag_name = rt->tag_name;
993 struct gotweb_url tag = {
997 .path = repo_dir->name,
998 .commit = rt->commit_id,
1001 if (strncmp(tag_name, "refs/tags/", 10) == 0)
1005 <title>{{ repo_dir->name }} {{" "}} {{ tag_name }}</title>
1008 {{ render gotweb_render_absolute_url(c, &tag) }}
1012 <![CDATA[<pre>{{ rt->tag_commit }}</pre>]]>
1014 {{ render rss_author(tp, rt->tagger) }}
1015 <guid isPermaLink="false">{{ rt->commit_id }}</guid>
1017 {{ render gotweb_render_age(tp, rt->tagger_time, TM_RFC822) }}
1022 {{ define rss_author(struct template *tp, char *author) }}
1026 /* what to do if the author name contains a paren? */
1027 if (strchr(author, '(') != NULL || strchr(author, ')') != NULL)
1030 t = strchr(author, '<');
1036 while (isspace((unsigned char)*--t))
1039 t = strchr(mail, '>');
1045 {{ mail }} {{" "}} ({{ author }})