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 <sys/types.h>
20 #include <sys/queue.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_reference.h"
45 static int datetime(struct template *, time_t, int);
46 static int gotweb_render_blob_line(struct template *, const char *, size_t);
47 static int gotweb_render_tree_item(struct template *, struct got_tree_entry *);
48 static int blame_line(struct template *, const char *, struct blame_line *,
51 static inline int gotweb_render_more(struct template *, int);
53 static inline int diff_line(struct template *, char *);
54 static inline int tag_item(struct template *, struct repo_tag *);
55 static inline int branch(struct template *, struct got_reflist_entry *);
56 static inline int rss_tag_item(struct template *, struct repo_tag *);
57 static inline int rss_author(struct template *, char *);
61 {{ define datetime(struct template *tp, time_t t, int fmt) }}
67 if (gmtime_r(&t, &tm) == NULL)
70 if (strftime(rfc3339, sizeof(rfc3339), "%FT%TZ", &tm) == 0)
73 if (fmt != TM_DIFF && asctime_r(&tm, datebuf) == NULL)
76 <time datetime="{{ rfc3339 }}">
77 {{ if fmt == TM_DIFF }}
78 {{ render gotweb_render_age(tp, t) }}
80 {{ datebuf }} {{ " UTC" }}
85 {{ define gotweb_render_page(struct template *tp,
86 int (*body)(struct template *)) }}
88 struct request *c = tp->tp_arg;
89 struct server *srv = c->srv;
90 struct querystring *qs = c->t->qs;
91 struct gotweb_url u_path;
92 const char *prfx = c->document_uri;
93 const char *css = srv->custom_css;
95 memset(&u_path, 0, sizeof(u_path));
96 u_path.index_page = -1;
98 u_path.action = SUMMARY;
103 <meta charset="utf-8" />
104 <title>{{ srv->site_name }}</title>
105 <meta name="viewport" content="initial-scale=1.0" />
106 <meta name="msapplication-TileColor" content="#da532c" />
107 <meta name="theme-color" content="#ffffff"/>
108 <link rel="apple-touch-icon" sizes="180x180" href="{{ prfx }}apple-touch-icon.png" />
109 <link rel="icon" type="image/png" sizes="32x32" href="{{ prfx }}favicon-32x32.png" />
110 <link rel="icon" type="image/png" sizes="16x16" href="{{ prfx }}favicon-16x16.png" />
111 <link rel="manifest" href="{{ prfx }}site.webmanifest"/>
112 <link rel="mask-icon" href="{{ prfx }}safari-pinned-tab.svg" />
113 <link rel="stylesheet" type="text/css" href="{{ prfx }}{{ css }}" />
118 <a href="{{ srv->logo_url }}" target="_blank">
119 <img src="{{ prfx }}{{ srv->logo }}" />
125 <a href="?index_page={{ printf "%d", qs->index_page }}">
129 {! u_path.path = qs->path; !}
131 <a href="{{ render gotweb_render_url(tp->tp_arg, &u_path)}}">
135 {{ if qs->action != INDEX }}
136 {{ " / " }}{{ gotweb_action_name(qs->action) }}
141 {{ render body(tp) }}
143 <footer id="site_owner_wrapper">
145 {{ if srv->show_site_owner }}
146 {{ srv->site_owner }}
154 {{ define gotweb_render_error(struct template *tp) }}
156 struct request *c = tp->tp_arg;
157 struct transport *t = c->t;
159 <div id="err_content">
163 See daemon logs for details
168 {{ define gotweb_render_repo_table_hdr(struct template *tp) }}
170 struct request *c = tp->tp_arg;
171 struct server *srv = c->srv;
173 <div id="index_header">
174 <div class="index_project">
177 {{ if srv->show_repo_description }}
178 <div class="index_project_description">
182 {{ if srv->show_repo_owner }}
183 <div class="index_project_owner">
187 {{ if srv->show_repo_age }}
188 <div class="index_project_age">
195 {{ define gotweb_render_repo_fragment(struct template *tp, struct repo_dir *repo_dir) }}
197 struct request *c = tp->tp_arg;
198 struct server *srv = c->srv;
199 struct gotweb_url summary = {
203 .path = repo_dir->name,
208 .path = repo_dir->name,
213 .path = repo_dir->name,
218 .path = repo_dir->name,
223 .path = repo_dir->name,
228 .path = repo_dir->name,
231 <div class="index_wrapper">
232 <div class="index_project">
233 <a href="{{ render gotweb_render_url(tp->tp_arg, &summary) }}">{{ repo_dir->name }}</a>
235 {{ if srv->show_repo_description }}
236 <div class="index_project_description">
237 {{ repo_dir->description }}
240 {{ if srv->show_repo_owner }}
241 <div class="index_project_owner">
242 {{ repo_dir->owner }}
245 {{ if srv->show_repo_age }}
246 <div class="index_project_age">
247 {{ render datetime(tp, repo_dir->age, TM_DIFF) }}
250 <div class="navs_wrapper">
252 <a href="{{ render gotweb_render_url(tp->tp_arg, &summary) }}">summary</a>
254 <a href="{{ render gotweb_render_url(tp->tp_arg, &briefs) }}">briefs</a>
256 <a href="{{ render gotweb_render_url(tp->tp_arg, &commits) }}">commits</a>
258 <a href="{{ render gotweb_render_url(tp->tp_arg, &tags) }}">tags</a>
260 <a href="{{ render gotweb_render_url(tp->tp_arg, &tree) }}">tree</a>
262 <a href="{{ render gotweb_render_url(tp->tp_arg, &rss) }}">rss</a>
269 {{ define gotweb_render_briefs(struct template *tp) }}
271 struct request *c = tp->tp_arg;
272 struct transport *t = c->t;
273 struct querystring *qs = c->t->qs;
274 struct repo_commit *rc;
275 struct repo_dir *repo_dir = t->repo_dir;
276 struct gotweb_url diff_url, tree_url;
279 diff_url = (struct gotweb_url){
283 .path = repo_dir->name,
284 .headref = qs->headref,
286 tree_url = (struct gotweb_url){
290 .path = repo_dir->name,
291 .headref = qs->headref,
294 <header class='subtitle'>
295 <h2>Commit Briefs</h2>
297 <div id="briefs_content">
298 {{ tailq-foreach rc &t->repo_commits entry }}
300 diff_url.commit = rc->commit_id;
301 tree_url.commit = rc->commit_id;
303 tmp = strchr(rc->committer, '<');
307 tmp = strchr(rc->commit_msg, '\n');
312 <p class='brief_meta'>
313 <span class='briefs_age'>
314 {{ render datetime(tp, rc->committer_time, TM_DIFF) }}
317 <span class="briefs_author">
321 <p class="briefs_log">
322 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">
325 {{ if rc->refs_str }}
326 {{ " " }} <span class="refs_str">({{ rc->refs_str }})</span>
331 <div class="navs_wrapper">
333 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">diff</a>
335 <a href="{{ render gotweb_render_url(tp->tp_arg, &tree_url) }}">tree</a>
340 {{ render gotweb_render_more(tp, BRIEFS) }}
344 {{ define gotweb_render_more(struct template *tp, int action) }}
346 struct request *c = tp->tp_arg;
347 struct transport *t = c->t;
348 struct querystring *qs = t->qs;
349 struct gotweb_url more = {
353 .commit = t->more_id,
354 .headref = qs->headref,
359 <div id="np_wrapper">
361 <a href="{{ render gotweb_render_url(c, &more) }}">
369 {{ define gotweb_render_navs(struct template *tp) }}
371 struct request *c = tp->tp_arg;
372 struct transport *t = c->t;
373 struct gotweb_url prev, next;
374 int have_prev, have_next;
376 gotweb_get_navs(c, &prev, &have_prev, &next, &have_next);
378 <div id="np_wrapper">
381 <a href="{{ render gotweb_render_url(c, &prev) }}">
388 <a href="{{ render gotweb_render_url(c, &next) }}">
403 {{ define gotweb_render_commits(struct template *tp) }}
405 struct request *c = tp->tp_arg;
406 struct transport *t = c->t;
407 struct repo_dir *repo_dir = t->repo_dir;
408 struct repo_commit *rc;
409 struct gotweb_url diff, tree;
411 diff = (struct gotweb_url){
415 .path = repo_dir->name,
417 tree = (struct gotweb_url){
421 .path = repo_dir->name,
424 <header class="subtitle">
427 <div class="commits_content">
428 {{ tailq-foreach rc &t->repo_commits entry }}
430 diff.commit = rc->commit_id;
431 tree.commit = rc->commit_id;
433 <div class="commits_header_wrapper">
434 <dl class="commits_header">
436 <dd><code class="commit-id">{{ rc->commit_id }}</code></dd>
438 <dd>{{ rc->author }}</dd>
439 {{ if strcmp(rc->committer, rc->author) != 0 }}
441 <dd>{{ rc->committer }}</dd>
445 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
454 <div class="navs_wrapper">
456 <a href="{{ render gotweb_render_url(c, &diff) }}">diff</a>
458 <a href="{{ render gotweb_render_url(c, &tree) }}">tree</a>
463 {{ render gotweb_render_more(tp, COMMITS) }}
467 {{ define gotweb_render_blob(struct template *tp) }}
469 struct request *c = tp->tp_arg;
470 struct transport *t = c->t;
471 struct got_blob_object *blob = t->blob;
472 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
474 <header class="subtitle">
477 <div id="blob_content">
478 <div id="blob_header_wrapper">
479 <dl id="blob_header">
482 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
485 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
491 {{ render got_output_blob_by_lines(tp, blob, gotweb_render_blob_line) }}
497 {{ define gotweb_render_blob_line(struct template *tp, const char *line,
503 r = snprintf(lineno, sizeof(lineno), "%zu", no);
504 if (r < 0 || (size_t)r >= sizeof(lineno))
507 <div class="blob_line" id="line{{ lineno }}">
508 <a href="#line{{ lineno }}">{{ lineno }}</a>
509 <span class="blob_code">{{ line }}</span>
513 {{ define gotweb_render_tree(struct template *tp) }}
515 struct request *c = tp->tp_arg;
516 struct transport *t = c->t;
517 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
519 <header class='subtitle'>
522 <div id="tree_content">
523 <div id="tree_header_wrapper">
524 <dl id="tree_header">
526 <dd><code class="commit-id">{{ rc->tree_id }}</code></dd>
529 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
532 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
537 {{ render got_output_repo_tree(c, gotweb_render_tree_item) }}
542 {{ define gotweb_render_tree_item(struct template *tp,
543 struct got_tree_entry *te) }}
545 struct request *c = tp->tp_arg;
546 struct transport *t = c->t;
547 struct querystring *qs = t->qs;
548 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
549 const char *modestr = "";
554 struct gotweb_url url = {
557 .commit = rc->commit_id,
561 name = got_tree_entry_get_name(te);
562 mode = got_tree_entry_get_mode(te);
564 folder = qs->folder ? qs->folder : "";
566 if (asprintf(&dir, "%s/%s", folder, name) == -1)
577 if (got_object_tree_entry_is_submodule(te))
579 else if (S_ISLNK(mode))
581 else if (S_ISDIR(mode))
583 else if (mode & S_IXUSR)
586 <tr class="tree_wrapper">
587 {{ if S_ISDIR(mode) }}
588 <td class="tree_line" colspan=2>
589 <a href="{{ render gotweb_render_url(c, &url) }}">
590 {{ name }}{{ modestr }}
594 <td class="tree_line">
595 <a href="{{ render gotweb_render_url(c, &url) }}">
596 {{ name }}{{ modestr }}
599 <td class="tree_line_blank">
600 {! url.action = COMMITS; !}
601 <a href="{{ render gotweb_render_url(c, &url) }}">
605 {! url.action = BLAME; !}
606 <a href="{{ render gotweb_render_url(c, &url) }}">
618 {{ define gotweb_render_tags(struct template *tp) }}
620 struct request *c = tp->tp_arg;
621 struct transport *t = c->t;
622 struct querystring *qs = t->qs;
626 commit_found = qs->commit == NULL;
628 <header class='subtitle'>
631 <div id="tags_content">
632 {{ if t->tag_count == 0 }}
633 <div id="err_content">
634 This repository contains no tags
637 {{ tailq-foreach rt &t->repo_tags entry }}
638 {{ if commit_found || !strcmp(qs->commit, rt->commit_id) }}
639 {! commit_found = 1; !}
640 {{ render tag_item(tp, rt) }}
643 {{ if t->next_id || t->prev_id }}
644 {! qs->action = TAGS; !}
645 {{ render gotweb_render_navs(tp) }}
651 {{ define tag_item(struct template *tp, struct repo_tag *rt) }}
653 struct request *c = tp->tp_arg;
654 struct transport *t = c->t;
655 struct repo_dir *repo_dir = t->repo_dir;
656 char *tag_name = rt->tag_name;
657 char *msg = rt->tag_commit;
659 struct gotweb_url url = {
663 .path = repo_dir->name,
664 .commit = rt->commit_id,
667 if (strncmp(tag_name, "refs/tags/", 10) == 0)
671 nl = strchr(msg, '\n');
676 <div class="tag_age">
677 {{ render datetime(tp, rt->tagger_time, TM_DIFF) }}
679 <div class="tag_name">{{ tag_name }}</div>
680 <div class="tag_log">
681 <a href="{{ render gotweb_render_url(c, &url) }}">
685 <div class="navs_wrapper">
687 <a href="{{ render gotweb_render_url(c, &url) }}">tag</a>
689 {! url.action = BRIEFS; !}
690 <a href="{{ render gotweb_render_url(c, &url) }}">commit briefs</a>
692 {! url.action = COMMITS; !}
693 <a href="{{ render gotweb_render_url(c, &url) }}">commits</a>
699 {{ define gotweb_render_tag(struct template *tp) }}
701 struct request *c = tp->tp_arg;
702 struct transport *t = c->t;
704 const char *tag_name;
706 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
707 tag_name = rt->tag_name;
709 if (strncmp(tag_name, "refs/", 5) == 0)
712 <header class="subtitle">
715 <div id="tags_content">
716 <div id="tag_header_wrapper">
720 <code class="commit-id">{{ rt->commit_id }}</code>
722 <span class="refs_str">({{ tag_name }})</span>
725 <dd>{{ rt->tagger }}</dd>
728 {{ render datetime(tp, rt->tagger_time, TM_LONG)}}
731 <dd class="commit-msg">{{ rt->commit_msg }}</dd>
734 <pre id="tag_commit">
741 {{ define gotweb_render_diff(struct template *tp) }}
743 struct request *c = tp->tp_arg;
744 struct transport *t = c->t;
746 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
751 <header class="subtitle">
754 <div id="diff_content">
755 <div id="diff_header_wrapper">
756 <dl id="diff_header">
758 <dd><code class="commit-id">{{ rc->commit_id }}</code></dd>
760 <dd>{{ rc->author }}</dd>
761 {{ if strcmp(rc->committer, rc->author) != 0 }}
763 <dd>{{ rc->committer }}</dd>
767 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
770 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
775 {{ while (linelen = getline(&line, &linesize, fp)) != -1 }}
776 {{ render diff_line(tp, line) }}
784 {{ define diff_line(struct template *tp, char *line )}}
786 const char *color = NULL;
789 if (!strncmp(line, "-", 1))
790 color = "diff_minus";
791 else if (!strncmp(line, "+", 1))
793 else if (!strncmp(line, "@@", 2))
794 color = "diff_chunk_header";
795 else if (!strncmp(line, "commit +", 8) ||
796 !strncmp(line, "commit -", 8) ||
797 !strncmp(line, "blob +", 6) ||
798 !strncmp(line, "blob -", 6) ||
799 !strncmp(line, "file +", 6) ||
800 !strncmp(line, "file -", 6))
802 else if (!strncmp(line, "from:", 5) || !strncmp(line, "via:", 4))
803 color = "diff_author";
804 else if (!strncmp(line, "date:", 5))
807 nl = strchr(line, '\n');
811 <span class="diff_line {{ color }}">{{ line }}</span>{{"\n"}}
814 {{ define gotweb_render_branches(struct template *tp,
815 struct got_reflist_head *refs) }}
817 struct got_reflist_entry *re;
819 <header class='subtitle'>
822 <div id="branches_content">
823 {{ tailq-foreach re refs entry }}
824 {{ if !got_ref_is_symbolic(re->ref) }}
825 {{ render branch(tp, re) }}
831 {{ define branch(struct template *tp, struct got_reflist_entry *re) }}
833 const struct got_error *err;
834 struct request *c = tp->tp_arg;
835 struct querystring *qs = c->t->qs;
838 struct gotweb_url url = {
845 refname = got_ref_get_name(re->ref);
847 err = got_get_repo_age(&age, c, refname);
849 log_warnx("%s: %s", __func__, err->msg);
853 if (strncmp(refname, "refs/heads/", 11) == 0)
856 url.headref = refname;
858 <section class="branches_wrapper">
859 <div class="branches_age">
860 {{ render datetime(tp, age, TM_DIFF) }}
863 <a href="{{ render gotweb_render_url(c, &url) }}">{{ refname }}</a>
865 <div class="navs_wrapper">
867 <a href="{{ render gotweb_render_url(c, &url) }}">summary</a>
869 {! url.action = BRIEFS; !}
870 <a href="{{ render gotweb_render_url(c, &url) }}">commit briefs</a>
872 {! url.action = COMMITS; !}
873 <a href="{{ render gotweb_render_url(c, &url) }}">commits</a>
880 {{ define gotweb_render_summary(struct template *tp) }}
882 struct request *c = tp->tp_arg;
883 struct server *srv = c->srv;
884 struct transport *t = c->t;
885 struct got_reflist_head *refs = &t->refs;
887 <dl id="summary_wrapper">
888 {{ if srv->show_repo_description }}
889 <dt>Description:</dt>
890 <dd>{{ t->repo_dir->description }}</dd>
892 {{ if srv->show_repo_owner }}
894 <dd>{{ t->repo_dir->owner }}</dd>
896 {{ if srv->show_repo_age }}
897 <dt>Last Change:</dt>
899 {{ render datetime(tp, t->repo_dir->age, TM_DIFF) }}
902 {{ if srv->show_repo_cloneurl }}
904 <dd><pre class="clone-url">{{ t->repo_dir->url }}</pre></dd>
907 {{ render gotweb_render_briefs(tp) }}
908 {{ render gotweb_render_tags(tp) }}
909 {{ render gotweb_render_branches(tp, refs) }}
912 {{ define gotweb_render_blame(struct template *tp) }}
914 const struct got_error *err;
915 struct request *c = tp->tp_arg;
916 struct transport *t = c->t;
917 struct repo_commit *rc = TAILQ_FIRST(&t->repo_commits);
919 <header class="subtitle">
922 <div id="blame_content">
923 <div id="blame_header_wrapper">
924 <dl id="blame_header">
927 {{ render datetime(tp, rc->committer_time, TM_LONG) }}
930 <dd class="commit-msg">{{ rc->commit_msg }}</dd>
936 err = got_output_file_blame(c, &blame_line);
937 if (err && err->code != GOT_ERR_CANCELLED)
938 log_warnx("%s: got_output_file_blame: %s", __func__,
947 {{ define blame_line(struct template *tp, const char *line,
948 struct blame_line *bline, int lprec, int lcur) }}
950 struct request *c = tp->tp_arg;
951 struct transport *t = c->t;
952 struct repo_dir *repo_dir = t->repo_dir;
954 struct gotweb_url url = {
958 .path = repo_dir->name,
959 .commit = bline->id_str,
962 s = strchr(bline->committer, '<');
963 committer = s ? s + 1 : bline->committer;
965 s = strchr(committer, '@');
969 <div class="blame_wrapper">
970 <div class="blame_number">{{ printf "%.*d", lprec, lcur }}</div>
971 <div class="blame_hash">
972 <a href="{{ render gotweb_render_url(c, &url) }}">
973 {{ printf "%.8s", bline->id_str }}
976 <div class="blame_date">{{ bline->datebuf }}</div>
977 <div class="blame_author">{{ printf "%.9s", committer }}</div>
978 <div class="blame_code">{{ line }}</div>
982 {{ define gotweb_render_rss(struct template *tp) }}
984 struct request *c = tp->tp_arg;
985 struct server *srv = c->srv;
986 struct transport *t = c->t;
987 struct repo_dir *repo_dir = t->repo_dir;
989 struct gotweb_url summary = {
993 .path = repo_dir->name,
996 <?xml version="1.0" encoding="UTF-8"?>
997 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
999 <title>Tags of {{ repo_dir->name }}</title>
1002 {{ render gotweb_render_absolute_url(c, &summary) }}
1005 {{ if srv->show_repo_description }}
1006 <description>{{ repo_dir->description }}</description>
1008 {{ tailq-foreach rt &t->repo_tags entry }}
1009 {{ render rss_tag_item(tp, rt) }}
1015 {{ define rss_tag_item(struct template *tp, struct repo_tag *rt) }}
1017 struct request *c = tp->tp_arg;
1018 struct transport *t = c->t;
1019 struct repo_dir *repo_dir = t->repo_dir;
1023 char *tag_name = rt->tag_name;
1024 struct gotweb_url tag = {
1028 .path = repo_dir->name,
1029 .commit = rt->commit_id,
1032 if (strncmp(tag_name, "refs/tags/", 10) == 0)
1035 if (gmtime_r(&rt->tagger_time, &tm) == NULL)
1037 r = strftime(rfc822, sizeof(rfc822), "%a, %d %b %Y %H:%M:%S GMT", &tm);
1042 <title>{{ repo_dir->name }} {{" "}} {{ tag_name }}</title>
1045 {{ render gotweb_render_absolute_url(c, &tag) }}
1049 <![CDATA[<pre>{{ rt->tag_commit }}</pre>]]>
1051 {{ render rss_author(tp, rt->tagger) }}
1052 <guid isPermaLink="false">{{ rt->commit_id }}</guid>
1059 {{ define rss_author(struct template *tp, char *author) }}
1063 /* what to do if the author name contains a paren? */
1064 if (strchr(author, '(') != NULL || strchr(author, ')') != NULL)
1067 t = strchr(author, '<');
1073 while (isspace((unsigned char)*--t))
1076 t = strchr(mail, '>');
1082 {{ mail }} {{" "}} ({{ author }})