2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
5 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <netinet/in.h>
22 #include <sys/queue.h>
24 #include <sys/types.h>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_reference.h"
39 #include "got_repository.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
44 #include "got_commit_graph.h"
45 #include "got_blame.h"
46 #include "got_privsep.h"
56 static const struct querystring_keys querystring_keys[] = {
61 { "headref", HEADREF },
62 { "index_page", INDEX_PAGE },
67 static const struct action_keys action_keys[] = {
71 { "commits", COMMITS },
75 { "summary", SUMMARY },
81 static const struct got_error *gotweb_init_querystring(struct querystring **);
82 static const struct got_error *gotweb_parse_querystring(struct querystring **,
84 static const struct got_error *gotweb_assign_querystring(struct querystring **,
86 static const struct got_error *gotweb_render_header(struct request *);
87 static const struct got_error *gotweb_render_footer(struct request *);
88 static const struct got_error *gotweb_render_index(struct request *);
89 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
91 static const struct got_error *gotweb_load_got_path(struct request *c,
93 static const struct got_error *gotweb_get_repo_description(char **,
94 struct server *, char *);
95 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
97 static const struct got_error *gotweb_render_navs(struct request *);
98 static const struct got_error *gotweb_render_blame(struct request *);
99 static const struct got_error *gotweb_render_briefs(struct request *);
100 static const struct got_error *gotweb_render_commits(struct request *);
101 static const struct got_error *gotweb_render_diff(struct request *);
102 static const struct got_error *gotweb_render_summary(struct request *);
103 static const struct got_error *gotweb_render_tag(struct request *);
104 static const struct got_error *gotweb_render_tags(struct request *);
105 static const struct got_error *gotweb_render_tree(struct request *);
106 static const struct got_error *gotweb_render_branches(struct request *);
108 static void gotweb_free_querystring(struct querystring *);
109 static void gotweb_free_repo_dir(struct repo_dir *);
111 struct server *gotweb_get_server(uint8_t *, uint8_t *);
114 gotweb_process_request(struct request *c)
116 const struct got_error *error = NULL, *error2 = NULL;
117 struct server *srv = NULL;
118 struct querystring *qs = NULL;
119 struct repo_dir *repo_dir = NULL;
120 uint8_t err[] = "gotwebd experienced an error: ";
123 /* init the transport */
124 error = gotweb_init_transport(&c->t);
126 log_warnx("%s: %s", __func__, error->msg);
129 /* don't process any further if client disconnected */
130 if (c->sock->client_status == CLIENT_DISCONNECT)
132 /* get the gotwebd server */
133 srv = gotweb_get_server(c->server_name, c->http_host);
135 log_warnx("%s: error server is NULL", __func__);
139 /* parse our querystring */
140 error = gotweb_init_querystring(&qs);
142 log_warnx("%s: %s", __func__, error->msg);
146 error = gotweb_parse_querystring(&qs, c->querystring);
148 log_warnx("%s: %s", __func__, error->msg);
153 * certain actions require a commit id in the querystring. this stops
154 * bad actors from exploiting this by manually manipulating the
158 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
159 qs->action == DIFF)) {
160 error2 = got_error(GOT_ERR_QUERYSTRING);
164 if (qs->action != INDEX) {
165 error = gotweb_init_repo_dir(&repo_dir, qs->path);
168 error = gotweb_load_got_path(c, repo_dir);
169 c->t->repo_dir = repo_dir;
170 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
174 /* render top of page */
175 if (qs != NULL && qs->action == BLOB) {
176 error = got_get_repo_commits(c, 1);
179 error = got_output_file_blob(c);
181 log_warnx("%s: %s", __func__, error->msg);
187 error = gotweb_render_content_type(c, "text/html");
189 log_warnx("%s: %s", __func__, error->msg);
195 error = gotweb_render_header(c);
197 log_warnx("%s: %s", __func__, error->msg);
208 error = gotweb_render_blame(c);
210 log_warnx("%s: %s", __func__, error->msg);
215 error = gotweb_render_briefs(c);
217 log_warnx("%s: %s", __func__, error->msg);
222 error = gotweb_render_commits(c);
224 log_warnx("%s: %s", __func__, error->msg);
229 error = gotweb_render_diff(c);
231 log_warnx("%s: %s", __func__, error->msg);
236 error = gotweb_render_index(c);
238 log_warnx("%s: %s", __func__, error->msg);
243 error = gotweb_render_summary(c);
245 log_warnx("%s: %s", __func__, error->msg);
250 error = gotweb_render_tag(c);
252 log_warnx("%s: %s", __func__, error->msg);
257 error = gotweb_render_tags(c);
259 log_warnx("%s: %s", __func__, error->msg);
264 error = gotweb_render_tree(c);
266 log_warnx("%s: %s", __func__, error->msg);
272 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
273 "Erorr: Bad Querystring");
281 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
283 if (fcgi_printf(c, "%s", err) == -1)
286 if (fcgi_printf(c, "%s", error->msg) == -1)
289 if (fcgi_printf(c, "see daemon logs for details") == -1)
292 if (html && fcgi_printf(c, "</div>\n") == -1)
295 if (html && srv != NULL)
296 gotweb_render_footer(c);
300 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
302 struct server *srv = NULL;
304 /* check against the server name first */
305 if (strlen(server_name) > 0)
306 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
307 if (strcmp(srv->name, server_name) == 0)
310 /* check against subdomain second */
311 if (strlen(subdomain) > 0)
312 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
313 if (strcmp(srv->name, subdomain) == 0)
316 /* if those fail, send first server */
317 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
324 const struct got_error *
325 gotweb_init_transport(struct transport **t)
327 const struct got_error *error = NULL;
329 *t = calloc(1, sizeof(**t));
331 return got_error_from_errno2("%s: calloc", __func__);
333 TAILQ_INIT(&(*t)->repo_commits);
334 TAILQ_INIT(&(*t)->repo_tags);
337 (*t)->repo_dir = NULL;
339 (*t)->next_id = NULL;
340 (*t)->prev_id = NULL;
347 static const struct got_error *
348 gotweb_init_querystring(struct querystring **qs)
350 const struct got_error *error = NULL;
352 *qs = calloc(1, sizeof(**qs));
354 return got_error_from_errno2("%s: calloc", __func__);
356 (*qs)->headref = strdup("HEAD");
357 if ((*qs)->headref == NULL) {
360 return got_error_from_errno2("%s: strdup", __func__);
363 (*qs)->action = INDEX;
364 (*qs)->commit = NULL;
366 (*qs)->folder = NULL;
367 (*qs)->index_page = 0;
368 (*qs)->index_page_str = NULL;
374 static const struct got_error *
375 gotweb_parse_querystring(struct querystring **qs, char *qst)
377 const struct got_error *error = NULL;
378 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
379 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
386 return got_error_from_errno2("%s: strdup", __func__);
391 while (tok1_pair != NULL) {
392 strsep(&tok1_end, "&");
394 tok2 = strdup(tok1_pair);
397 return got_error_from_errno2("%s: strdup", __func__);
403 while (tok2_pair != NULL) {
404 strsep(&tok2_end, "=");
406 error = gotweb_assign_querystring(qs, tok2_pair,
411 tok2_pair = tok2_end;
414 tok1_pair = tok1_end;
424 static const struct got_error *
425 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
427 const struct got_error *error = NULL;
431 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
432 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
435 switch (querystring_keys[el_cnt].element) {
437 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
438 if (strcmp(value, action_keys[a_cnt].name) != 0)
440 else if (strcmp(value,
441 action_keys[a_cnt].name) == 0){
443 action_keys[a_cnt].action;
451 (*qs)->commit = strdup(value);
452 if ((*qs)->commit == NULL) {
453 error = got_error_from_errno2("%s: strdup",
459 (*qs)->file = strdup(value);
460 if ((*qs)->file == NULL) {
461 error = got_error_from_errno2("%s: strdup",
467 (*qs)->folder = strdup(value);
468 if ((*qs)->folder == NULL) {
469 error = got_error_from_errno2("%s: strdup",
475 (*qs)->headref = strdup(value);
476 if ((*qs)->headref == NULL) {
477 error = got_error_from_errno2("%s: strdup",
483 if (strlen(value) == 0)
485 (*qs)->index_page_str = strdup(value);
486 if ((*qs)->index_page_str == NULL) {
487 error = got_error_from_errno2("%s: strdup",
491 (*qs)->index_page = strtonum(value, INT64_MIN,
494 error = got_error_from_errno3("%s: strtonum %s",
498 if ((*qs)->index_page < 0) {
499 (*qs)->index_page = 0;
500 sprintf((*qs)->index_page_str, "%d", 0);
504 (*qs)->path = strdup(value);
505 if ((*qs)->path == NULL) {
506 error = got_error_from_errno2("%s: strdup",
512 if (strlen(value) == 0)
514 (*qs)->page_str = strdup(value);
515 if ((*qs)->page_str == NULL) {
516 error = got_error_from_errno2("%s: strdup",
520 (*qs)->page = strtonum(value, INT64_MIN,
523 error = got_error_from_errno3("%s: strtonum %s",
527 if ((*qs)->page < 0) {
529 sprintf((*qs)->page_str, "%d", 0);
541 gotweb_free_repo_tag(struct repo_tag *rt)
546 free(rt->tag_commit);
547 free(rt->commit_msg);
554 gotweb_free_repo_commit(struct repo_commit *rc)
564 free(rc->commit_msg);
570 gotweb_free_querystring(struct querystring *qs)
577 free(qs->index_page_str);
585 gotweb_free_repo_dir(struct repo_dir *repo_dir)
587 if (repo_dir != NULL) {
588 free(repo_dir->name);
589 free(repo_dir->owner);
590 free(repo_dir->description);
593 free(repo_dir->path);
599 gotweb_free_transport(struct transport *t)
601 struct repo_commit *rc = NULL, *trc = NULL;
602 struct repo_tag *rt = NULL, *trt = NULL;
604 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
605 TAILQ_REMOVE(&t->repo_commits, rc, entry);
606 gotweb_free_repo_commit(rc);
608 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
609 TAILQ_REMOVE(&t->repo_tags, rt, entry);
610 gotweb_free_repo_tag(rt);
612 gotweb_free_repo_dir(t->repo_dir);
613 gotweb_free_querystring(t->qs);
619 const struct got_error *
620 gotweb_render_content_type(struct request *c, const uint8_t *type)
622 const char *csp = "default-src 'self'; script-src 'none'; "
623 "object-src 'none';";
626 "Content-Security-Policy: %s\r\n"
627 "Content-Type: %s\r\n\r\n",
632 const struct got_error *
633 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
636 fcgi_printf(c, "Content-type: %s\r\n"
637 "Content-disposition: attachment; filename=%s\r\n\r\n",
642 static const struct got_error *
643 gotweb_render_header(struct request *c)
645 struct server *srv = c->srv;
646 struct querystring *qs = c->t->qs;
649 r = fcgi_printf(c, "<!doctype html>\n"
652 "<title>%s</title>\n"
653 "<meta charset='utf-8' />\n"
654 "<meta name='viewport' content='initial-scale=.75' />\n"
655 "<meta name='msapplication-TileColor' content='#da532c' />\n"
656 "<meta name='theme-color' content='#ffffff'/>\n"
657 "<link rel='apple-touch-icon' sizes='180x180'"
658 " href='%sapple-touch-icon.png' />\n"
659 "<link rel='icon' type='image/png' sizes='32x32'"
660 " href='%sfavicon-32x32.png' />\n"
661 "<link rel='icon' type='image/png' sizes='16x16'"
662 " href='%sfavicon-16x16.png' />\n"
663 "<link rel='manifest' href='%ssite.webmanifest'/>\n"
664 "<link rel='mask-icon' href='%ssafari-pinned-tab.svg' />\n"
665 "<link rel='stylesheet' type='text/css' href='%s%s' />\n"
668 "<div id='gw_body'>\n"
669 "<div id='header'>\n"
670 "<div id='got_link'>"
671 "<a href='%s' target='_blank'>"
672 "<img src='%s%s' alt='logo' id='logo' />"
674 "</div>\n" /* #got_link */
675 "</div>\n" /* #header */
676 "<div id='site_path'>\n"
677 "<div id='site_link'>\n"
678 "<a href='?index_page=%d'>%s</a>",
685 c->script_name, srv->custom_css,
687 c->script_name, srv->logo,
688 qs->index_page, srv->site_link);
693 if (qs->path != NULL) {
694 r = fcgi_printf(c, " / "
695 "<a href='?index_page=%d&path=%s&action=summary'>"
697 qs->index_page, qs->path, qs->path);
701 if (qs->action != INDEX) {
702 const char *action = "";
704 switch (qs->action) {
731 if (fcgi_printf(c, " / %s", action) == -1)
736 fcgi_printf(c, "</div>\n" /* #site_path */
737 "</div>\n" /* #site_link */
738 "<div id='content'>\n");
744 static const struct got_error *
745 gotweb_render_footer(struct request *c)
747 const struct got_error *error = NULL;
748 struct server *srv = c->srv;
749 const char *siteowner = " ";
750 char *escaped_owner = NULL;
752 if (srv->show_site_owner) {
753 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
756 siteowner = escaped_owner;
759 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
760 "<div id='site_owner'>%s</div>\n"
761 "</div>\n" /* #site_owner_wrapper */
762 "</div>\n" /* #content */
763 "</div>\n" /* #gw_body */
764 "</body>\n</html>\n", siteowner);
770 static const struct got_error *
771 gotweb_render_navs(struct request *c)
773 const struct got_error *error = NULL;
774 struct transport *t = c->t;
775 struct querystring *qs = t->qs;
776 struct server *srv = c->srv;
777 char *nhref = NULL, *phref = NULL;
780 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
786 if (qs->index_page > 0) {
787 if (asprintf(&phref, "index_page=%d",
788 qs->index_page - 1) == -1) {
789 error = got_error_from_errno2("%s: asprintf",
797 if (t->prev_id && qs->commit != NULL &&
798 strcmp(qs->commit, t->prev_id) != 0) {
799 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
800 "&action=briefs&commit=%s&headref=%s",
801 qs->index_page, qs->path, qs->page - 1, t->prev_id,
802 qs->headref) == -1) {
803 error = got_error_from_errno2("%s: asprintf",
811 if (t->prev_id && qs->commit != NULL &&
812 strcmp(qs->commit, t->prev_id) != 0) {
813 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
814 "&action=commits&commit=%s&headref=%s&folder=%s"
816 qs->index_page, qs->path, qs->page - 1, t->prev_id,
817 qs->headref, qs->folder ? qs->folder : "",
818 qs->file ? qs->file : "") == -1) {
819 error = got_error_from_errno2("%s: asprintf",
827 if (t->prev_id && qs->commit != NULL &&
828 strcmp(qs->commit, t->prev_id) != 0) {
829 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
830 "&action=tags&commit=%s&headref=%s",
831 qs->index_page, qs->path, qs->page - 1, t->prev_id,
832 qs->headref) == -1) {
833 error = got_error_from_errno2("%s: asprintf",
846 r = fcgi_printf(c, "<a href='?%s'>Previous</a>", phref);
851 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
852 "<div id='nav_next'>");
859 if (t->next_disp == srv->max_repos_display &&
860 t->repos_total != (qs->index_page + 1) *
861 srv->max_repos_display) {
862 if (asprintf(&nhref, "index_page=%d",
863 qs->index_page + 1) == -1) {
864 error = got_error_from_errno2("%s: asprintf",
873 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
874 "&action=briefs&commit=%s&headref=%s",
875 qs->index_page, qs->path, qs->page + 1, t->next_id,
876 qs->headref) == -1) {
877 error = got_error_from_errno2("%s: asprintf",
886 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
887 "&action=commits&commit=%s&headref=%s&folder=%s"
889 qs->index_page, qs->path, qs->page + 1, t->next_id,
890 qs->headref, qs->folder ? qs->folder : "",
891 qs->file ? qs->file : "") == -1) {
892 error = got_error_from_errno2("%s: asprintf",
901 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
902 "&action=tags&commit=%s&headref=%s",
903 qs->index_page, qs->path, qs->page + 1, t->next_id,
904 qs->headref) == -1) {
905 error = got_error_from_errno2("%s: asprintf",
917 r = fcgi_printf(c, "<a href='?%s'>Next</a>", nhref);
921 fcgi_printf(c, "</div>\n"); /* #nav_next */
922 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
933 static const struct got_error *
934 gotweb_render_index(struct request *c)
936 const struct got_error *error = NULL;
937 struct server *srv = c->srv;
938 struct transport *t = c->t;
939 struct querystring *qs = t->qs;
940 struct repo_dir *repo_dir = NULL;
942 struct dirent **sd_dent;
943 const char *index_page_str;
946 unsigned int d_cnt, d_i, d_disp = 0;
949 index_page_str = qs->index_page_str ? qs->index_page_str : "";
951 d = opendir(srv->repos_path);
953 error = got_error_from_errno2("opendir", srv->repos_path);
957 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
959 error = got_error_from_errno2("scandir", srv->repos_path);
963 /* get total count of repos */
964 for (d_i = 0; d_i < d_cnt; d_i++) {
965 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
966 strcmp(sd_dent[d_i]->d_name, "..") == 0)
969 if (asprintf(&c_path, "%s/%s", srv->repos_path,
970 sd_dent[d_i]->d_name) == -1) {
971 error = got_error_from_errno("asprintf");
975 if (lstat(c_path, &st) == 0 && S_ISDIR(st.st_mode) &&
976 !got_path_dir_is_empty(c_path))
982 r = fcgi_printf(c, "<div id='index_header'>\n"
983 "<div id='index_header_project'>Project</div>\n");
987 if (srv->show_repo_description)
988 if (fcgi_printf(c, "<div id='index_header_description'>"
989 "Description</div>\n") == -1)
991 if (srv->show_repo_owner)
992 if (fcgi_printf(c, "<div id='index_header_owner'>"
993 "Owner</div>\n") == -1)
995 if (srv->show_repo_age)
996 if (fcgi_printf(c, "<div id='index_header_age'>"
997 "Last Change</div>\n") == -1)
999 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1002 for (d_i = 0; d_i < d_cnt; d_i++) {
1003 if (srv->max_repos > 0 && (d_i - 2) == srv->max_repos)
1004 break; /* account for parent and self */
1006 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1007 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1010 if (qs->index_page > 0 && (qs->index_page *
1011 srv->max_repos_display) > t->prev_disp) {
1016 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1020 error = gotweb_load_got_path(c, repo_dir);
1021 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1025 else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1028 if (lstat(repo_dir->path, &st) == 0 &&
1029 S_ISDIR(st.st_mode) &&
1030 !got_path_dir_is_empty(repo_dir->path))
1033 gotweb_free_repo_dir(repo_dir);
1041 r = fcgi_printf(c, "<div class='index_wrapper'>\n"
1042 "<div class='index_project'>"
1043 "<a href='?index_page=%s&path=%s&action=summary'>"
1046 "</div>", /* .index_project */
1047 index_page_str, repo_dir->name,
1052 if (srv->show_repo_description) {
1054 "<div class='index_project_description'>\n"
1055 "%s</div>\n", repo_dir->description);
1060 if (srv->show_repo_owner) {
1061 r = fcgi_printf(c, "<div class='index_project_owner'>"
1062 "%s</div>\n", repo_dir->owner);
1067 if (srv->show_repo_age) {
1068 r = fcgi_printf(c, "<div class='index_project_age'>"
1069 "%s</div>\n", repo_dir->age);
1074 r = fcgi_printf(c, "<div class='navs_wrapper'>"
1075 "<div class='navs'>"
1076 "<a href='?index_page=%s&path=%s&action=summary'>"
1079 "<a href='?index_page=%s&path=%s&action=briefs'>"
1082 "<a href='?index_page=%s&path=%s&action=commits'>"
1085 "<a href='?index_page=%s&path=%s&action=tags'>"
1088 "<a href='?index_page=%s&path=%s&action=tree'>"
1091 "</div>" /* .navs */
1092 "<div class='dotted_line'></div>\n"
1093 "</div>\n" /* .navs_wrapper */
1094 "</div>\n", /* .index_wrapper */
1095 index_page_str, repo_dir->name,
1096 index_page_str, repo_dir->name,
1097 index_page_str, repo_dir->name,
1098 index_page_str, repo_dir->name,
1099 index_page_str, repo_dir->name);
1103 gotweb_free_repo_dir(repo_dir);
1106 if (d_disp == srv->max_repos_display)
1109 if (srv->max_repos_display == 0)
1111 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1113 if (t->repos_total <= srv->max_repos ||
1114 t->repos_total <= srv->max_repos_display)
1117 error = gotweb_render_navs(c);
1121 if (d != NULL && closedir(d) == EOF && error == NULL)
1122 error = got_error_from_errno("closedir");
1126 static const struct got_error *
1127 gotweb_render_blame(struct request *c)
1129 const struct got_error *error = NULL;
1130 struct transport *t = c->t;
1131 struct repo_commit *rc = NULL;
1132 char *age = NULL, *msg = NULL;
1135 error = got_get_repo_commits(c, 1);
1139 rc = TAILQ_FIRST(&t->repo_commits);
1141 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1144 error = gotweb_escape_html(&msg, rc->commit_msg);
1148 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1149 "<div id='blame_title'>Blame</div>\n"
1150 "</div>\n" /* #blame_title_wrapper */
1151 "<div id='blame_content'>\n"
1152 "<div id='blame_header_wrapper'>\n"
1153 "<div id='blame_header'>\n"
1154 "<div class='header_age_title'>Date:</div>\n"
1155 "<div class='header_age'>%s</div>\n"
1156 "<div id='header_commit_msg_title'>Message:</div>\n"
1157 "<div id='header_commit_msg'>%s</div>\n"
1158 "</div>\n" /* #blame_header */
1159 "</div>\n" /* #blame_header_wrapper */
1160 "<div class='dotted_line'></div>\n"
1161 "<div id='blame'>\n",
1167 error = got_output_file_blame(c);
1171 fcgi_printf(c, "</div>\n" /* #blame */
1172 "</div>\n"); /* #blame_content */
1179 static const struct got_error *
1180 gotweb_render_briefs(struct request *c)
1182 const struct got_error *error = NULL;
1183 struct repo_commit *rc = NULL;
1184 struct server *srv = c->srv;
1185 struct transport *t = c->t;
1186 struct querystring *qs = t->qs;
1187 struct repo_dir *repo_dir = t->repo_dir;
1188 const char *index_page_str;
1189 char *smallerthan, *newline;
1190 char *age = NULL, *author = NULL, *msg = NULL;
1193 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1195 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1196 "<div id='briefs_title'>Commit Briefs</div>\n"
1197 "</div>\n" /* #briefs_title_wrapper */
1198 "<div id='briefs_content'>\n");
1202 if (qs->action == SUMMARY) {
1203 qs->action = BRIEFS;
1204 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1206 error = got_get_repo_commits(c, srv->max_commits_display);
1210 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1211 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1215 smallerthan = strchr(rc->author, '<');
1217 *smallerthan = '\0';
1219 newline = strchr(rc->commit_msg, '\n');
1223 error = gotweb_escape_html(&author, rc->author);
1226 error = gotweb_escape_html(&msg, rc->commit_msg);
1230 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1231 "<div class='briefs_author'>%s</div>\n"
1232 "<div class='briefs_log'>"
1233 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1234 "&headref=%s'>%s</a>",
1237 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1245 error = gotweb_escape_html(&refs, rc->refs_str);
1249 " <span class='refs_str'>(%s)</span>", refs);
1254 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1257 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1258 "<div class='navs'>"
1259 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1260 "&headref=%s'>diff</a>"
1262 "<a href='?index_page=%s&path=%s&action=tree&commit=%s"
1263 "&headref=%s'>tree</a>"
1264 "</div>\n" /* .navs */
1265 "</div>\n" /* .navs_wrapper */
1266 "<div class='dotted_line'></div>\n",
1267 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1268 index_page_str, repo_dir->name, rc->commit_id, qs->headref);
1280 if (t->next_id || t->prev_id) {
1281 error = gotweb_render_navs(c);
1285 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1293 static const struct got_error *
1294 gotweb_render_commits(struct request *c)
1296 const struct got_error *error = NULL;
1297 struct repo_commit *rc = NULL;
1298 struct server *srv = c->srv;
1299 struct transport *t = c->t;
1300 struct querystring *qs = t->qs;
1301 struct repo_dir *repo_dir = t->repo_dir;
1302 const char *index_page_str;
1303 char *age = NULL, *author = NULL, *msg = NULL;
1306 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1308 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1309 "<div class='commits_title'>Commits</div>\n"
1310 "</div>\n" /* .commits_title_wrapper */
1311 "<div class='commits_content'>\n");
1315 error = got_get_repo_commits(c, srv->max_commits_display);
1319 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1320 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1323 error = gotweb_escape_html(&author, rc->author);
1326 error = gotweb_escape_html(&msg, rc->commit_msg);
1330 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1331 "<div class='commits_header'>\n"
1332 "<div class='header_commit_title'>Commit:</div>\n"
1333 "<div class='header_commit'>%s</div>\n"
1334 "<div class='header_author_title'>Author:</div>\n"
1335 "<div class='header_author'>%s</div>\n"
1336 "<div class='header_age_title'>Date:</div>\n"
1337 "<div class='header_age'>%s</div>\n"
1338 "</div>\n" /* .commits_header */
1339 "</div>\n" /* .commits_header_wrapper */
1340 "<div class='dotted_line'></div>\n"
1341 "<div class='commit'>\n%s</div>\n",
1349 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1350 "<div class='navs'>"
1351 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1354 "<a href='?index_page=%s&path=%s&action=tree&commit=%s'>"
1356 "</div>\n" /* .navs */
1357 "</div>\n" /* .navs_wrapper */
1358 "<div class='dotted_line'></div>\n",
1359 index_page_str, repo_dir->name, rc->commit_id,
1360 index_page_str, repo_dir->name, rc->commit_id);
1372 if (t->next_id || t->prev_id) {
1373 error = gotweb_render_navs(c);
1377 fcgi_printf(c, "</div>\n"); /* .commits_content */
1385 static const struct got_error *
1386 gotweb_render_branches(struct request *c)
1388 const struct got_error *error = NULL;
1389 struct got_reflist_head refs;
1390 struct got_reflist_entry *re;
1391 struct transport *t = c->t;
1392 struct querystring *qs = t->qs;
1393 struct got_repository *repo = t->repo;
1394 const char *index_page_str;
1398 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1402 error = got_ref_list(&refs, repo, "refs/heads",
1403 got_ref_cmp_by_name, NULL);
1407 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1408 "<div id='branches_title'>Branches</div>\n"
1409 "</div>\n" /* #branches_title_wrapper */
1410 "<div id='branches_content'>\n");
1414 TAILQ_FOREACH(re, &refs, entry) {
1415 const char *refname = NULL;
1416 char *escaped_refname = NULL;
1418 if (got_ref_is_symbolic(re->ref))
1421 refname = got_ref_get_name(re->ref);
1422 if (refname == NULL) {
1423 error = got_error_from_errno("strdup");
1426 if (strncmp(refname, "refs/heads/", 11) != 0)
1429 error = got_get_repo_age(&age, c, qs->path, refname,
1434 if (strncmp(refname, "refs/heads/", 11) == 0)
1436 error = gotweb_escape_html(&escaped_refname, refname);
1440 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1441 "<div class='branches_age'>%s</div>\n"
1442 "<div class='branches_space'> </div>\n"
1443 "<div class='branch'>"
1444 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1446 "</div>\n" /* .branch */
1447 "<div class='navs_wrapper'>\n"
1448 "<div class='navs'>"
1449 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1452 "<a href='?index_page=%s&path=%s&action=briefs&headref=%s'>"
1455 "<a href='?index_page=%s&path=%s&action=commits&headref=%s'>"
1457 "</div>\n" /* .navs */
1458 "</div>\n" /* .navs_wrapper */
1459 "<div class='dotted_line'></div>\n"
1460 "</div>\n", /* .branches_wrapper */
1462 index_page_str, qs->path, refname,
1464 index_page_str, qs->path, refname,
1465 index_page_str, qs->path, refname,
1466 index_page_str, qs->path, refname);
1467 free(escaped_refname);
1475 fcgi_printf(c, "</div>\n"); /* #branches_content */
1480 static const struct got_error *
1481 gotweb_render_tree(struct request *c)
1483 const struct got_error *error = NULL;
1484 struct transport *t = c->t;
1485 struct repo_commit *rc = NULL;
1486 char *age = NULL, *msg = NULL;
1489 error = got_get_repo_commits(c, 1);
1493 rc = TAILQ_FIRST(&t->repo_commits);
1495 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1499 error = gotweb_escape_html(&msg, rc->commit_msg);
1503 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1504 "<div id='tree_title'>Tree</div>\n"
1505 "</div>\n" /* #tree_title_wrapper */
1506 "<div id='tree_content'>\n"
1507 "<div id='tree_header_wrapper'>\n"
1508 "<div id='tree_header'>\n"
1509 "<div id='header_tree_title'>Tree:</div>\n"
1510 "<div id='header_tree'>%s</div>\n"
1511 "<div class='header_age_title'>Date:</div>\n"
1512 "<div class='header_age'>%s</div>\n"
1513 "<div id='header_commit_msg_title'>Message:</div>\n"
1514 "<div id='header_commit_msg'>%s</div>\n"
1515 "</div>\n" /* #tree_header */
1516 "</div>\n" /* #tree_header_wrapper */
1517 "<div class='dotted_line'></div>\n"
1518 "<div id='tree'>\n",
1525 error = got_output_repo_tree(c);
1529 fcgi_printf(c, "</div>\n"); /* #tree */
1530 fcgi_printf(c, "</div>\n"); /* #tree_content */
1537 static const struct got_error *
1538 gotweb_render_diff(struct request *c)
1540 const struct got_error *error = NULL;
1541 struct transport *t = c->t;
1542 struct repo_commit *rc = NULL;
1543 char *age = NULL, *author = NULL, *msg = NULL;
1546 error = got_get_repo_commits(c, 1);
1550 rc = TAILQ_FIRST(&t->repo_commits);
1552 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1555 error = gotweb_escape_html(&author, rc->author);
1558 error = gotweb_escape_html(&msg, rc->commit_msg);
1562 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1563 "<div id='diff_title'>Commit Diff</div>\n"
1564 "</div>\n" /* #diff_title_wrapper */
1565 "<div id='diff_content'>\n"
1566 "<div id='diff_header_wrapper'>\n"
1567 "<div id='diff_header'>\n"
1568 "<div id='header_diff_title'>Diff:</div>\n"
1569 "<div id='header_diff'>%s<br />%s</div>\n"
1570 "<div class='header_commit_title'>Commit:</div>\n"
1571 "<div class='header_commit'>%s</div>\n"
1572 "<div id='header_tree_title'>Tree:</div>\n"
1573 "<div id='header_tree'>%s</div>\n"
1574 "<div class='header_author_title'>Author:</div>\n"
1575 "<div class='header_author'>%s</div>\n"
1576 "<div class='header_age_title'>Date:</div>\n"
1577 "<div class='header_age'>%s</div>\n"
1578 "<div id='header_commit_msg_title'>Message:</div>\n"
1579 "<div id='header_commit_msg'>%s</div>\n"
1580 "</div>\n" /* #diff_header */
1581 "</div>\n" /* #diff_header_wrapper */
1582 "<div class='dotted_line'></div>\n"
1583 "<div id='diff'>\n",
1584 rc->parent_id, rc->commit_id,
1593 error = got_output_repo_diff(c);
1597 fcgi_printf(c, "</div>\n"); /* #diff */
1598 fcgi_printf(c, "</div>\n"); /* #diff_content */
1606 static const struct got_error *
1607 gotweb_render_summary(struct request *c)
1609 const struct got_error *error = NULL;
1610 struct transport *t = c->t;
1611 struct server *srv = c->srv;
1614 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1617 if (srv->show_repo_description) {
1619 "<div id='description_title'>Description:</div>\n"
1620 "<div id='description'>%s</div>\n",
1621 t->repo_dir->description ? t->repo_dir->description : "");
1626 if (srv->show_repo_owner) {
1628 "<div id='repo_owner_title'>Owner:</div>\n"
1629 "<div id='repo_owner'>%s</div>\n",
1630 t->repo_dir->owner ? t->repo_dir->owner : "");
1635 if (srv->show_repo_age) {
1637 "<div id='last_change_title'>Last Change:</div>\n"
1638 "<div id='last_change'>%s</div>\n",
1644 if (srv->show_repo_cloneurl) {
1646 "<div id='cloneurl_title'>Clone URL:</div>\n"
1647 "<div id='cloneurl'>%s</div>\n",
1648 t->repo_dir->url ? t->repo_dir->url : "");
1653 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1657 error = gotweb_render_briefs(c);
1659 log_warnx("%s: %s", __func__, error->msg);
1663 error = gotweb_render_tags(c);
1665 log_warnx("%s: %s", __func__, error->msg);
1669 error = gotweb_render_branches(c);
1671 log_warnx("%s: %s", __func__, error->msg);
1676 static const struct got_error *
1677 gotweb_render_tag(struct request *c)
1679 const struct got_error *error = NULL;
1680 struct repo_tag *rt = NULL;
1681 struct transport *t = c->t;
1682 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1684 error = got_get_repo_tags(c, 1);
1688 if (t->tag_count == 0) {
1689 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1694 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1696 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1699 error = gotweb_escape_html(&author, rt->tagger);
1702 error = gotweb_escape_html(&msg, rt->commit_msg);
1706 tagname = rt->tag_name;
1707 if (strncmp(tagname, "refs/", 5) == 0)
1709 error = gotweb_escape_html(&tagname, tagname);
1713 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1714 "<div id='tags_title'>Tag</div>\n"
1715 "</div>\n" /* #tags_title_wrapper */
1716 "<div id='tags_content'>\n"
1717 "<div id='tag_header_wrapper'>\n"
1718 "<div id='tag_header'>\n"
1719 "<div class='header_commit_title'>Commit:</div>\n"
1720 "<div class='header_commit'>%s"
1721 " <span class='refs_str'>(%s)</span></div>\n"
1722 "<div class='header_author_title'>Tagger:</div>\n"
1723 "<div class='header_author'>%s</div>\n"
1724 "<div class='header_age_title'>Date:</div>\n"
1725 "<div class='header_age'>%s</div>\n"
1726 "<div id='header_commit_msg_title'>Message:</div>\n"
1727 "<div id='header_commit_msg'>%s</div>\n"
1728 "</div>\n" /* #tag_header */
1729 "<div class='dotted_line'></div>\n"
1730 "<div id='tag_commit'>\n%s</div>"
1731 "</div>", /* tag_header_wrapper */
1746 static const struct got_error *
1747 gotweb_render_tags(struct request *c)
1749 const struct got_error *error = NULL;
1750 struct repo_tag *rt = NULL;
1751 struct server *srv = c->srv;
1752 struct transport *t = c->t;
1753 struct querystring *qs = t->qs;
1754 struct repo_dir *repo_dir = t->repo_dir;
1755 const char *index_page_str;
1756 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1757 int r, commit_found = 0;
1759 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1761 if (qs->action == BRIEFS) {
1763 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1765 error = got_get_repo_tags(c, srv->max_commits_display);
1769 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1770 "<div id='tags_title'>Tags</div>\n"
1771 "</div>\n" /* #tags_title_wrapper */
1772 "<div id='tags_content'>\n");
1776 if (t->tag_count == 0) {
1777 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1778 "This repository contains no tags");
1783 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1784 if (commit_found == 0 && qs->commit != NULL) {
1785 if (strcmp(qs->commit, rt->commit_id) != 0)
1790 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1794 tagname = rt->tag_name;
1795 if (strncmp(tagname, "refs/tags/", 10) == 0)
1797 error = gotweb_escape_html(&tagname, tagname);
1801 if (rt->tag_commit != NULL) {
1802 newline = strchr(rt->tag_commit, '\n');
1805 error = gotweb_escape_html(&msg, rt->tag_commit);
1810 r = fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1811 "<div class='tag'>%s</div>\n"
1812 "<div class='tag_log'>"
1813 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1815 "</div>\n" /* .tag_log */
1816 "<div class='navs_wrapper'>\n"
1817 "<div class='navs'>"
1818 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1821 "<a href='?index_page=%s&path=%s&action=briefs&commit=%s'>"
1824 "<a href='?index_page=%s&path=%s&action=commits&commit=%s'>"
1826 "</div>\n" /* .navs */
1827 "</div>\n" /* .navs_wrapper */
1828 "<div class='dotted_line'></div>\n",
1831 index_page_str, repo_dir->name, rt->commit_id,
1833 index_page_str, repo_dir->name, rt->commit_id,
1834 index_page_str, repo_dir->name, rt->commit_id,
1835 index_page_str, repo_dir->name, rt->commit_id);
1846 if (t->next_id || t->prev_id) {
1847 error = gotweb_render_navs(c);
1851 fcgi_printf(c, "</div>\n"); /* #tags_content */
1859 const struct got_error *
1860 gotweb_escape_html(char **escaped_html, const char *orig_html)
1862 const struct got_error *error = NULL;
1863 struct escape_pair {
1874 size_t orig_len, len;
1877 orig_len = strlen(orig_html);
1879 for (i = 0; i < orig_len; i++) {
1880 for (j = 0; j < nitems(esc); j++) {
1881 if (orig_html[i] != esc[j].c)
1883 len += strlen(esc[j].s) - 1 /* escaped char */;
1887 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1888 if (*escaped_html == NULL)
1889 return got_error_from_errno("calloc");
1892 for (i = 0; i < orig_len; i++) {
1894 for (j = 0; j < nitems(esc); j++) {
1895 if (orig_html[i] != esc[j].c)
1898 if (strlcat(*escaped_html, esc[j].s, len + 1)
1900 error = got_error(GOT_ERR_NO_SPACE);
1903 x += strlen(esc[j].s);
1908 (*escaped_html)[x] = orig_html[i];
1914 free(*escaped_html);
1915 *escaped_html = NULL;
1917 (*escaped_html)[x] = '\0';
1923 static struct got_repository *
1924 find_cached_repo(struct server *srv, const char *path)
1928 for (i = 0; i < srv->ncached_repos; i++) {
1929 if (strcmp(srv->cached_repos[i].path, path) == 0)
1930 return srv->cached_repos[i].repo;
1936 static const struct got_error *
1937 cache_repo(struct got_repository **new, struct server *srv,
1938 struct repo_dir *repo_dir, struct socket *sock)
1940 const struct got_error *error = NULL;
1941 struct got_repository *repo;
1942 struct cached_repo *cr;
1945 if (srv->ncached_repos >= nitems(srv->cached_repos)) {
1946 cr = &srv->cached_repos[srv->ncached_repos - 1];
1947 error = got_repo_close(cr->repo);
1948 memset(cr, 0, sizeof(*cr));
1949 srv->ncached_repos--;
1952 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1953 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1954 cr = &srv->cached_repos[0];
1957 cr = &srv->cached_repos[srv->ncached_repos];
1960 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1963 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1964 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1969 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1970 >= sizeof(cr->path)) {
1972 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1973 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1975 return got_error(GOT_ERR_NO_SPACE);
1979 srv->ncached_repos++;
1984 static const struct got_error *
1985 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1987 const struct got_error *error = NULL;
1988 struct socket *sock = c->sock;
1989 struct server *srv = c->srv;
1990 struct transport *t = c->t;
1991 struct got_repository *repo = NULL;
1995 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1996 GOTWEB_GIT_DIR) == -1)
1997 return got_error_from_errno("asprintf");
1999 dt = opendir(dir_test);
2003 repo_dir->path = dir_test;
2008 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
2009 repo_dir->name) == -1)
2010 return got_error_from_errno("asprintf");
2012 dt = opendir(dir_test);
2014 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2017 repo_dir->path = dir_test;
2022 repo = find_cached_repo(srv, repo_dir->path);
2024 error = cache_repo(&repo, srv, repo_dir, sock);
2029 error = gotweb_get_repo_description(&repo_dir->description, srv,
2033 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
2036 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
2040 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2043 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2044 error = got_error_from_errno("closedir");
2048 static const struct got_error *
2049 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2051 const struct got_error *error;
2053 *repo_dir = calloc(1, sizeof(**repo_dir));
2054 if (*repo_dir == NULL)
2055 return got_error_from_errno("calloc");
2057 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2058 error = got_error_from_errno("asprintf");
2063 (*repo_dir)->owner = NULL;
2064 (*repo_dir)->description = NULL;
2065 (*repo_dir)->url = NULL;
2066 (*repo_dir)->age = NULL;
2067 (*repo_dir)->path = NULL;
2072 static const struct got_error *
2073 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2075 const struct got_error *error = NULL;
2077 char *d_file = NULL;
2081 *description = NULL;
2082 if (srv->show_repo_description == 0)
2085 if (asprintf(&d_file, "%s/description", dir) == -1)
2086 return got_error_from_errno("asprintf");
2088 f = fopen(d_file, "r");
2090 if (errno != ENOENT && errno != EACCES)
2091 error = got_error_from_errno2("fopen", d_file);
2095 if (fseek(f, 0, SEEK_END) == -1) {
2096 error = got_ferror(f, GOT_ERR_IO);
2101 error = got_ferror(f, GOT_ERR_IO);
2106 *description = strdup("");
2107 if (*description == NULL)
2108 error = got_error_from_errno("strdup");
2112 if (fseek(f, 0, SEEK_SET) == -1) {
2113 error = got_ferror(f, GOT_ERR_IO);
2116 *description = calloc(len + 1, sizeof(**description));
2117 if (*description == NULL) {
2118 error = got_error_from_errno("calloc");
2122 n = fread(*description, 1, len, f);
2123 if (n == 0 && ferror(f))
2124 error = got_ferror(f, GOT_ERR_IO);
2126 if (f != NULL && fclose(f) == EOF && error == NULL)
2127 error = got_error_from_errno("fclose");
2132 static const struct got_error *
2133 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2135 const struct got_error *error = NULL;
2137 char *d_file = NULL;
2143 if (srv->show_repo_cloneurl == 0)
2146 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2147 return got_error_from_errno("asprintf");
2149 f = fopen(d_file, "r");
2151 if (errno != ENOENT && errno != EACCES)
2152 error = got_error_from_errno2("fopen", d_file);
2156 if (fseek(f, 0, SEEK_END) == -1) {
2157 error = got_ferror(f, GOT_ERR_IO);
2162 error = got_ferror(f, GOT_ERR_IO);
2168 if (fseek(f, 0, SEEK_SET) == -1) {
2169 error = got_ferror(f, GOT_ERR_IO);
2173 *url = calloc(len + 1, sizeof(**url));
2175 error = got_error_from_errno("calloc");
2179 n = fread(*url, 1, len, f);
2180 if (n == 0 && ferror(f))
2181 error = got_ferror(f, GOT_ERR_IO);
2183 if (f != NULL && fclose(f) == EOF && error == NULL)
2184 error = got_error_from_errno("fclose");
2189 const struct got_error *
2190 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2193 long long diff_time;
2194 const char *years = "years ago", *months = "months ago";
2195 const char *weeks = "weeks ago", *days = "days ago";
2196 const char *hours = "hours ago", *minutes = "minutes ago";
2197 const char *seconds = "seconds ago", *now = "right now";
2205 diff_time = time(NULL) - committer_time;
2206 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2207 if (asprintf(repo_age, "%lld %s",
2208 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2209 return got_error_from_errno("asprintf");
2210 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2211 if (asprintf(repo_age, "%lld %s",
2212 (diff_time / 60 / 60 / 24 / (365 / 12)),
2214 return got_error_from_errno("asprintf");
2215 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2216 if (asprintf(repo_age, "%lld %s",
2217 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2218 return got_error_from_errno("asprintf");
2219 } else if (diff_time > 60 * 60 * 24 * 2) {
2220 if (asprintf(repo_age, "%lld %s",
2221 (diff_time / 60 / 60 / 24), days) == -1)
2222 return got_error_from_errno("asprintf");
2223 } else if (diff_time > 60 * 60 * 2) {
2224 if (asprintf(repo_age, "%lld %s",
2225 (diff_time / 60 / 60), hours) == -1)
2226 return got_error_from_errno("asprintf");
2227 } else if (diff_time > 60 * 2) {
2228 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2230 return got_error_from_errno("asprintf");
2231 } else if (diff_time > 2) {
2232 if (asprintf(repo_age, "%lld %s", diff_time,
2234 return got_error_from_errno("asprintf");
2236 if (asprintf(repo_age, "%s", now) == -1)
2237 return got_error_from_errno("asprintf");
2241 if (gmtime_r(&committer_time, &tm) == NULL)
2242 return got_error_from_errno("gmtime_r");
2244 s = asctime_r(&tm, datebuf);
2246 return got_error_from_errno("asctime_r");
2248 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2249 return got_error_from_errno("asprintf");