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)->action = INDEX;
357 (*qs)->commit = NULL;
359 (*qs)->folder = NULL;
360 (*qs)->headref = strdup("HEAD");
361 if ((*qs)->headref == NULL) {
362 return got_error_from_errno2("%s: strdup", __func__);
364 (*qs)->index_page = 0;
365 (*qs)->index_page_str = NULL;
371 static const struct got_error *
372 gotweb_parse_querystring(struct querystring **qs, char *qst)
374 const struct got_error *error = NULL;
375 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
376 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
383 return got_error_from_errno2("%s: strdup", __func__);
388 while (tok1_pair != NULL) {
389 strsep(&tok1_end, "&");
391 tok2 = strdup(tok1_pair);
394 return got_error_from_errno2("%s: strdup", __func__);
400 while (tok2_pair != NULL) {
401 strsep(&tok2_end, "=");
403 error = gotweb_assign_querystring(qs, tok2_pair,
408 tok2_pair = tok2_end;
411 tok1_pair = tok1_end;
421 static const struct got_error *
422 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
424 const struct got_error *error = NULL;
428 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
429 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
432 switch (querystring_keys[el_cnt].element) {
434 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
435 if (strcmp(value, action_keys[a_cnt].name) != 0)
437 else if (strcmp(value,
438 action_keys[a_cnt].name) == 0){
440 action_keys[a_cnt].action;
448 (*qs)->commit = strdup(value);
449 if ((*qs)->commit == NULL) {
450 error = got_error_from_errno2("%s: strdup",
456 (*qs)->file = strdup(value);
457 if ((*qs)->file == NULL) {
458 error = got_error_from_errno2("%s: strdup",
464 (*qs)->folder = strdup(value);
465 if ((*qs)->folder == NULL) {
466 error = got_error_from_errno2("%s: strdup",
472 (*qs)->headref = strdup(value);
473 if ((*qs)->headref == NULL) {
474 error = got_error_from_errno2("%s: strdup",
480 if (strlen(value) == 0)
482 (*qs)->index_page_str = strdup(value);
483 if ((*qs)->index_page_str == NULL) {
484 error = got_error_from_errno2("%s: strdup",
488 (*qs)->index_page = strtonum(value, INT64_MIN,
491 error = got_error_from_errno3("%s: strtonum %s",
495 if ((*qs)->index_page < 0) {
496 (*qs)->index_page = 0;
497 sprintf((*qs)->index_page_str, "%d", 0);
501 (*qs)->path = strdup(value);
502 if ((*qs)->path == NULL) {
503 error = got_error_from_errno2("%s: strdup",
509 if (strlen(value) == 0)
511 (*qs)->page_str = strdup(value);
512 if ((*qs)->page_str == NULL) {
513 error = got_error_from_errno2("%s: strdup",
517 (*qs)->page = strtonum(value, INT64_MIN,
520 error = got_error_from_errno3("%s: strtonum %s",
524 if ((*qs)->page < 0) {
526 sprintf((*qs)->page_str, "%d", 0);
538 gotweb_free_repo_tag(struct repo_tag *rt)
541 free(rt->commit_msg);
549 gotweb_free_repo_commit(struct repo_commit *rc)
559 free(rc->commit_msg);
565 gotweb_free_querystring(struct querystring *qs)
572 free(qs->index_page_str);
580 gotweb_free_repo_dir(struct repo_dir *repo_dir)
582 if (repo_dir != NULL) {
583 free(repo_dir->name);
584 free(repo_dir->owner);
585 free(repo_dir->description);
588 free(repo_dir->path);
594 gotweb_free_transport(struct transport *t)
596 struct repo_commit *rc = NULL, *trc = NULL;
597 struct repo_tag *rt = NULL, *trt = NULL;
599 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
600 TAILQ_REMOVE(&t->repo_commits, rc, entry);
601 gotweb_free_repo_commit(rc);
603 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
604 TAILQ_REMOVE(&t->repo_tags, rt, entry);
605 gotweb_free_repo_tag(rt);
607 gotweb_free_repo_dir(t->repo_dir);
608 gotweb_free_querystring(t->qs);
616 const struct got_error *
617 gotweb_render_content_type(struct request *c, const uint8_t *type)
619 const char *csp = "default-src 'self'; script-src 'none'; "
620 "object-src 'none';";
623 "Content-Security-Policy: %s\r\n"
624 "Content-Type: %s\r\n\r\n",
629 const struct got_error *
630 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
633 fcgi_printf(c, "Content-type: %s\r\n"
634 "Content-disposition: attachment; filename=%s\r\n\r\n",
639 static const struct got_error *
640 gotweb_render_header(struct request *c)
642 struct server *srv = c->srv;
643 struct querystring *qs = c->t->qs;
646 r = fcgi_printf(c, "<!doctype html>\n"
649 "<title>%s</title>\n"
650 "<meta charset='utf-8' />\n"
651 "<meta name='viewport' content='initial-scale=.75' />\n"
652 "<meta name='msapplication-TileColor' content='#da532c' />\n"
653 "<meta name='theme-color' content='#ffffff'/>\n"
654 "<link rel='apple-touch-icon' sizes='180x180'"
655 " href='/apple-touch-icon.png' />\n"
656 "<link rel='icon' type='image/png' sizes='32x32'"
657 " href='/favicon-32x32.png' />\n"
658 "<link rel='icon' type='image/png' sizes='16x16'"
659 " href='/favicon-16x16.png' />\n"
660 "<link rel='manifest' href='/site.webmanifest'/>\n"
661 "<link rel='mask-icon' href='/safari-pinned-tab.svg' />\n"
662 "<link rel='stylesheet' type='text/css' href='%s%s' />\n"
665 "<div id='gw_body'>\n"
666 "<div id='header'>\n"
667 "<div id='got_link'>"
668 "<a href='%s' target='_blank'>"
669 "<img src='%s%s' alt='logo' id='logo' />"
671 "</div>\n" /* #got_link */
672 "</div>\n" /* #header */
673 "<div id='site_path'>\n"
674 "<div id='site_link'>\n"
675 "<a href='?index_page=%d'>%s</a>",
677 c->script_name, srv->custom_css,
679 c->script_name, srv->logo,
680 qs->index_page, srv->site_link);
685 if (qs->path != NULL) {
686 r = fcgi_printf(c, " / "
687 "<a href='?index_page=%d&path=%s&action=summary'>"
689 qs->index_page, qs->path, qs->path);
693 if (qs->action != INDEX) {
694 const char *action = "";
696 switch (qs->action) {
723 if (fcgi_printf(c, " / %s", action) == -1)
728 fcgi_printf(c, "</div>\n" /* #site_path */
729 "</div>\n" /* #site_link */
730 "<div id='content'>\n");
736 static const struct got_error *
737 gotweb_render_footer(struct request *c)
739 const struct got_error *error = NULL;
740 struct server *srv = c->srv;
741 const char *siteowner = " ";
742 char *escaped_owner = NULL;
744 if (srv->show_site_owner) {
745 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
748 siteowner = escaped_owner;
751 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
752 "<div id='site_owner'>%s</div>\n"
753 "</div>\n" /* #site_owner_wrapper */
754 "</div>\n" /* #content */
755 "</div>\n" /* #gw_body */
756 "</body>\n</html>\n", siteowner);
762 static const struct got_error *
763 gotweb_render_navs(struct request *c)
765 const struct got_error *error = NULL;
766 struct transport *t = c->t;
767 struct querystring *qs = t->qs;
768 struct server *srv = c->srv;
769 char *nhref = NULL, *phref = NULL;
772 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
778 if (qs->index_page > 0) {
779 if (asprintf(&phref, "index_page=%d",
780 qs->index_page - 1) == -1) {
781 error = got_error_from_errno2("%s: asprintf",
789 if (t->prev_id && qs->commit != NULL &&
790 strcmp(qs->commit, t->prev_id) != 0) {
791 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
792 "&action=briefs&commit=%s&headref=%s",
793 qs->index_page, qs->path, qs->page - 1, t->prev_id,
794 qs->headref) == -1) {
795 error = got_error_from_errno2("%s: asprintf",
803 if (t->prev_id && qs->commit != NULL &&
804 strcmp(qs->commit, t->prev_id) != 0) {
805 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
806 "&action=commits&commit=%s&headref=%s&folder=%s"
808 qs->index_page, qs->path, qs->page - 1, t->prev_id,
809 qs->headref, qs->folder ? qs->folder : "",
810 qs->file ? qs->file : "") == -1) {
811 error = got_error_from_errno2("%s: asprintf",
819 if (t->prev_id && qs->commit != NULL &&
820 strcmp(qs->commit, t->prev_id) != 0) {
821 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
822 "&action=tags&commit=%s&headref=%s",
823 qs->index_page, qs->path, qs->page - 1, t->prev_id,
824 qs->headref) == -1) {
825 error = got_error_from_errno2("%s: asprintf",
838 r = fcgi_printf(c, "<a href='?%s'>Previous</a>", phref);
843 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
844 "<div id='nav_next'>");
851 if (t->next_disp == srv->max_repos_display &&
852 t->repos_total != (qs->index_page + 1) *
853 srv->max_repos_display) {
854 if (asprintf(&nhref, "index_page=%d",
855 qs->index_page + 1) == -1) {
856 error = got_error_from_errno2("%s: asprintf",
865 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
866 "&action=briefs&commit=%s&headref=%s",
867 qs->index_page, qs->path, qs->page + 1, t->next_id,
868 qs->headref) == -1) {
869 error = got_error_from_errno2("%s: asprintf",
878 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
879 "&action=commits&commit=%s&headref=%s&folder=%s"
881 qs->index_page, qs->path, qs->page + 1, t->next_id,
882 qs->headref, qs->folder ? qs->folder : "",
883 qs->file ? qs->file : "") == -1) {
884 error = got_error_from_errno2("%s: asprintf",
893 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
894 "&action=tags&commit=%s&headref=%s",
895 qs->index_page, qs->path, qs->page + 1, t->next_id,
896 qs->headref) == -1) {
897 error = got_error_from_errno2("%s: asprintf",
909 r = fcgi_printf(c, "<a href='?%s'>Next</a>", nhref);
913 fcgi_printf(c, "</div>\n"); /* #nav_next */
914 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
925 static const struct got_error *
926 gotweb_render_index(struct request *c)
928 const struct got_error *error = NULL;
929 struct server *srv = c->srv;
930 struct transport *t = c->t;
931 struct querystring *qs = t->qs;
932 struct repo_dir *repo_dir = NULL;
934 struct dirent **sd_dent;
935 const char *index_page_str;
938 unsigned int d_cnt, d_i, d_disp = 0;
941 index_page_str = qs->index_page_str ? qs->index_page_str : "";
943 d = opendir(srv->repos_path);
945 error = got_error_from_errno2("opendir", srv->repos_path);
949 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
951 error = got_error_from_errno2("scandir", srv->repos_path);
955 /* get total count of repos */
956 for (d_i = 0; d_i < d_cnt; d_i++) {
957 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
958 strcmp(sd_dent[d_i]->d_name, "..") == 0)
961 if (asprintf(&c_path, "%s/%s", srv->repos_path,
962 sd_dent[d_i]->d_name) == -1) {
963 error = got_error_from_errno("asprintf");
967 if (lstat(c_path, &st) == 0 && S_ISDIR(st.st_mode) &&
968 !got_path_dir_is_empty(c_path))
974 r = fcgi_printf(c, "<div id='index_header'>\n"
975 "<div id='index_header_project'>Project</div>\n");
979 if (srv->show_repo_description)
980 if (fcgi_printf(c, "<div id='index_header_description'>"
981 "Description</div>\n") == -1)
983 if (srv->show_repo_owner)
984 if (fcgi_printf(c, "<div id='index_header_owner'>"
985 "Owner</div>\n") == -1)
987 if (srv->show_repo_age)
988 if (fcgi_printf(c, "<div id='index_header_age'>"
989 "Last Change</div>\n") == -1)
991 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
994 for (d_i = 0; d_i < d_cnt; d_i++) {
995 if (srv->max_repos > 0 && (d_i - 2) == srv->max_repos)
996 break; /* account for parent and self */
998 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
999 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1002 if (qs->index_page > 0 && (qs->index_page *
1003 srv->max_repos_display) > t->prev_disp) {
1008 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1012 error = gotweb_load_got_path(c, repo_dir);
1013 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1017 else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1020 if (lstat(repo_dir->path, &st) == 0 &&
1021 S_ISDIR(st.st_mode) &&
1022 !got_path_dir_is_empty(repo_dir->path))
1025 gotweb_free_repo_dir(repo_dir);
1033 r = fcgi_printf(c, "<div class='index_wrapper'>\n"
1034 "<div class='index_project'>"
1035 "<a href='?index_page=%s&path=%s&action=summary'>"
1038 "</div>", /* .index_project */
1039 index_page_str, repo_dir->name,
1044 if (srv->show_repo_description) {
1046 "<div class='index_project_description'>\n"
1047 "%s</div>\n", repo_dir->description);
1052 if (srv->show_repo_owner) {
1053 r = fcgi_printf(c, "<div class='index_project_owner'>"
1054 "%s</div>\n", repo_dir->owner);
1059 if (srv->show_repo_age) {
1060 r = fcgi_printf(c, "<div class='index_project_age'>"
1061 "%s</div>\n", repo_dir->age);
1066 r = fcgi_printf(c, "<div class='navs_wrapper'>"
1067 "<div class='navs'>"
1068 "<a href='?index_page=%s&path=%s&action=summary'>"
1071 "<a href='?index_page=%s&path=%s&action=briefs'>"
1074 "<a href='?index_page=%s&path=%s&action=commits'>"
1077 "<a href='?index_page=%s&path=%s&action=tags'>"
1080 "<a href='?index_page=%s&path=%s&action=tree'>"
1083 "</div>" /* .navs */
1084 "<div class='dotted_line'></div>\n"
1085 "</div>\n" /* .navs_wrapper */
1086 "</div>\n", /* .index_wrapper */
1087 index_page_str, repo_dir->name,
1088 index_page_str, repo_dir->name,
1089 index_page_str, repo_dir->name,
1090 index_page_str, repo_dir->name,
1091 index_page_str, repo_dir->name);
1095 gotweb_free_repo_dir(repo_dir);
1098 if (d_disp == srv->max_repos_display)
1101 if (srv->max_repos_display == 0)
1103 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1105 if (t->repos_total <= srv->max_repos ||
1106 t->repos_total <= srv->max_repos_display)
1109 error = gotweb_render_navs(c);
1113 if (d != NULL && closedir(d) == EOF && error == NULL)
1114 error = got_error_from_errno("closedir");
1118 static const struct got_error *
1119 gotweb_render_blame(struct request *c)
1121 const struct got_error *error = NULL;
1122 struct transport *t = c->t;
1123 struct repo_commit *rc = NULL;
1124 char *age = NULL, *msg = NULL;
1127 error = got_get_repo_commits(c, 1);
1131 rc = TAILQ_FIRST(&t->repo_commits);
1133 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1136 error = gotweb_escape_html(&msg, rc->commit_msg);
1140 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1141 "<div id='blame_title'>Blame</div>\n"
1142 "</div>\n" /* #blame_title_wrapper */
1143 "<div id='blame_content'>\n"
1144 "<div id='blame_header_wrapper'>\n"
1145 "<div id='blame_header'>\n"
1146 "<div class='header_age_title'>Date:</div>\n"
1147 "<div class='header_age'>%s</div>\n"
1148 "<div id='header_commit_msg_title'>Message:</div>\n"
1149 "<div id='header_commit_msg'>%s</div>\n"
1150 "</div>\n" /* #blame_header */
1151 "</div>\n" /* #blame_header_wrapper */
1152 "<div class='dotted_line'></div>\n"
1153 "<div id='blame'>\n",
1159 error = got_output_file_blame(c);
1163 fcgi_printf(c, "</div>\n" /* #blame */
1164 "</div>\n"); /* #blame_content */
1171 static const struct got_error *
1172 gotweb_render_briefs(struct request *c)
1174 const struct got_error *error = NULL;
1175 struct repo_commit *rc = NULL;
1176 struct server *srv = c->srv;
1177 struct transport *t = c->t;
1178 struct querystring *qs = t->qs;
1179 struct repo_dir *repo_dir = t->repo_dir;
1180 const char *index_page_str;
1181 char *smallerthan, *newline;
1182 char *age = NULL, *author = NULL, *msg = NULL;
1185 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1187 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1188 "<div id='briefs_title'>Commit Briefs</div>\n"
1189 "</div>\n" /* #briefs_title_wrapper */
1190 "<div id='briefs_content'>\n");
1194 if (qs->action == SUMMARY) {
1195 qs->action = BRIEFS;
1196 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1198 error = got_get_repo_commits(c, srv->max_commits_display);
1202 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1203 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1207 smallerthan = strchr(rc->author, '<');
1209 *smallerthan = '\0';
1211 newline = strchr(rc->commit_msg, '\n');
1215 error = gotweb_escape_html(&author, rc->author);
1218 error = gotweb_escape_html(&msg, rc->commit_msg);
1222 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1223 "<div class='briefs_author'>%s</div>\n"
1224 "<div class='briefs_log'>"
1225 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1226 "&headref=%s'>%s</a>",
1229 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1237 error = gotweb_escape_html(&refs, rc->refs_str);
1241 " <span class='refs_str'>(%s)</span>", refs);
1246 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1249 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1250 "<div class='navs'>"
1251 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1252 "&headref=%s'>diff</a>"
1254 "<a href='?index_page=%s&path=%s&action=tree&commit=%s"
1255 "&headref=%s'>tree</a>"
1256 "</div>\n" /* .navs */
1257 "</div>\n" /* .navs_wrapper */
1258 "<div class='dotted_line'></div>\n",
1259 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1260 index_page_str, repo_dir->name, rc->commit_id, qs->headref);
1272 if (t->next_id || t->prev_id) {
1273 error = gotweb_render_navs(c);
1277 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1285 static const struct got_error *
1286 gotweb_render_commits(struct request *c)
1288 const struct got_error *error = NULL;
1289 struct repo_commit *rc = NULL;
1290 struct server *srv = c->srv;
1291 struct transport *t = c->t;
1292 struct querystring *qs = t->qs;
1293 struct repo_dir *repo_dir = t->repo_dir;
1294 const char *index_page_str;
1295 char *age = NULL, *author = NULL, *msg = NULL;
1298 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1300 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1301 "<div class='commits_title'>Commits</div>\n"
1302 "</div>\n" /* .commits_title_wrapper */
1303 "<div class='commits_content'>\n");
1307 error = got_get_repo_commits(c, srv->max_commits_display);
1311 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1312 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1315 error = gotweb_escape_html(&author, rc->author);
1318 error = gotweb_escape_html(&msg, rc->commit_msg);
1322 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1323 "<div class='commits_header'>\n"
1324 "<div class='header_commit_title'>Commit:</div>\n"
1325 "<div class='header_commit'>%s</div>\n"
1326 "<div class='header_author_title'>Author:</div>\n"
1327 "<div class='header_author'>%s</div>\n"
1328 "<div class='header_age_title'>Date:</div>\n"
1329 "<div class='header_age'>%s</div>\n"
1330 "</div>\n" /* .commits_header */
1331 "</div>\n" /* .commits_header_wrapper */
1332 "<div class='dotted_line'></div>\n"
1333 "<div class='commit'>\n%s</div>\n",
1341 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1342 "<div class='navs'>"
1343 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1346 "<a href='?index_page=%s&path=%s&action=tree&commit=%s'>"
1348 "</div>\n" /* .navs */
1349 "</div>\n" /* .navs_wrapper */
1350 "<div class='dotted_line'></div>\n",
1351 index_page_str, repo_dir->name, rc->commit_id,
1352 index_page_str, repo_dir->name, rc->commit_id);
1362 if (t->next_id || t->prev_id) {
1363 error = gotweb_render_navs(c);
1367 fcgi_printf(c, "</div>\n"); /* .commits_content */
1375 static const struct got_error *
1376 gotweb_render_branches(struct request *c)
1378 const struct got_error *error = NULL;
1379 struct got_reflist_head refs;
1380 struct got_reflist_entry *re;
1381 struct transport *t = c->t;
1382 struct querystring *qs = t->qs;
1383 struct got_repository *repo = t->repo;
1384 const char *index_page_str;
1388 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1392 error = got_ref_list(&refs, repo, "refs/heads",
1393 got_ref_cmp_by_name, NULL);
1397 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1398 "<div id='branches_title'>Branches</div>\n"
1399 "</div>\n" /* #branches_title_wrapper */
1400 "<div id='branches_content'>\n");
1404 TAILQ_FOREACH(re, &refs, entry) {
1405 const char *refname = NULL;
1406 char *escaped_refname = NULL;
1408 if (got_ref_is_symbolic(re->ref))
1411 refname = got_ref_get_name(re->ref);
1412 if (refname == NULL) {
1413 error = got_error_from_errno("strdup");
1416 if (strncmp(refname, "refs/heads/", 11) != 0)
1419 error = got_get_repo_age(&age, c, qs->path, refname,
1424 if (strncmp(refname, "refs/heads/", 11) == 0)
1426 error = gotweb_escape_html(&escaped_refname, refname);
1430 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1431 "<div class='branches_age'>%s</div>\n"
1432 "<div class='branches_space'> </div>\n"
1433 "<div class='branch'>"
1434 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1436 "</div>\n" /* .branch */
1437 "<div class='navs_wrapper'>\n"
1438 "<div class='navs'>"
1439 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1442 "<a href='?index_page=%s&path=%s&action=briefs&headref=%s'>"
1445 "<a href='?index_page=%s&path=%s&action=commits&headref=%s'>"
1447 "</div>\n" /* .navs */
1448 "</div>\n" /* .navs_wrapper */
1449 "<div class='dotted_line'></div>\n"
1450 "</div>\n", /* .branches_wrapper */
1452 index_page_str, qs->path, refname,
1454 index_page_str, qs->path, refname,
1455 index_page_str, qs->path, refname,
1456 index_page_str, qs->path, refname);
1457 free(escaped_refname);
1465 fcgi_printf(c, "</div>\n"); /* #branches_content */
1470 static const struct got_error *
1471 gotweb_render_tree(struct request *c)
1473 const struct got_error *error = NULL;
1474 struct transport *t = c->t;
1475 struct repo_commit *rc = NULL;
1476 char *age = NULL, *msg = NULL;
1479 error = got_get_repo_commits(c, 1);
1483 rc = TAILQ_FIRST(&t->repo_commits);
1485 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1489 error = gotweb_escape_html(&msg, rc->commit_msg);
1493 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1494 "<div id='tree_title'>Tree</div>\n"
1495 "</div>\n" /* #tree_title_wrapper */
1496 "<div id='tree_content'>\n"
1497 "<div id='tree_header_wrapper'>\n"
1498 "<div id='tree_header'>\n"
1499 "<div id='header_tree_title'>Tree:</div>\n"
1500 "<div id='header_tree'>%s</div>\n"
1501 "<div class='header_age_title'>Date:</div>\n"
1502 "<div class='header_age'>%s</div>\n"
1503 "<div id='header_commit_msg_title'>Message:</div>\n"
1504 "<div id='header_commit_msg'>%s</div>\n"
1505 "</div>\n" /* #tree_header */
1506 "</div>\n" /* #tree_header_wrapper */
1507 "<div class='dotted_line'></div>\n"
1508 "<div id='tree'>\n",
1515 error = got_output_repo_tree(c);
1519 fcgi_printf(c, "</div>\n"); /* #tree */
1520 fcgi_printf(c, "</div>\n"); /* #tree_content */
1527 static const struct got_error *
1528 gotweb_render_diff(struct request *c)
1530 const struct got_error *error = NULL;
1531 struct transport *t = c->t;
1532 struct repo_commit *rc = NULL;
1533 char *age = NULL, *author = NULL, *msg = NULL;
1536 error = got_get_repo_commits(c, 1);
1540 rc = TAILQ_FIRST(&t->repo_commits);
1542 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1545 error = gotweb_escape_html(&author, rc->author);
1548 error = gotweb_escape_html(&msg, rc->commit_msg);
1552 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1553 "<div id='diff_title'>Commit Diff</div>\n"
1554 "</div>\n" /* #diff_title_wrapper */
1555 "<div id='diff_content'>\n"
1556 "<div id='diff_header_wrapper'>\n"
1557 "<div id='diff_header'>\n"
1558 "<div id='header_diff_title'>Diff:</div>\n"
1559 "<div id='header_diff'>%s<br />%s</div>\n"
1560 "<div class='header_commit_title'>Commit:</div>\n"
1561 "<div class='header_commit'>%s</div>\n"
1562 "<div id='header_tree_title'>Tree:</div>\n"
1563 "<div id='header_tree'>%s</div>\n"
1564 "<div class='header_author_title'>Author:</div>\n"
1565 "<div class='header_author'>%s</div>\n"
1566 "<div class='header_age_title'>Date:</div>\n"
1567 "<div class='header_age'>%s</div>\n"
1568 "<div id='header_commit_msg_title'>Message:</div>\n"
1569 "<div id='header_commit_msg'>%s</div>\n"
1570 "</div>\n" /* #diff_header */
1571 "</div>\n" /* #diff_header_wrapper */
1572 "<div class='dotted_line'></div>\n"
1573 "<div id='diff'>\n",
1574 rc->parent_id, rc->commit_id,
1583 error = got_output_repo_diff(c);
1587 fcgi_printf(c, "</div>\n"); /* #diff */
1588 fcgi_printf(c, "</div>\n"); /* #diff_content */
1596 static const struct got_error *
1597 gotweb_render_summary(struct request *c)
1599 const struct got_error *error = NULL;
1600 struct transport *t = c->t;
1601 struct server *srv = c->srv;
1604 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1607 if (srv->show_repo_description) {
1609 "<div id='description_title'>Description:</div>\n"
1610 "<div id='description'>%s</div>\n",
1611 t->repo_dir->description ? t->repo_dir->description : "");
1616 if (srv->show_repo_owner) {
1618 "<div id='repo_owner_title'>Owner:</div>\n"
1619 "<div id='repo_owner'>%s</div>\n",
1620 t->repo_dir->owner ? t->repo_dir->owner : "");
1625 if (srv->show_repo_age) {
1627 "<div id='last_change_title'>Last Change:</div>\n"
1628 "<div id='last_change'>%s</div>\n",
1634 if (srv->show_repo_cloneurl) {
1636 "<div id='cloneurl_title'>Clone URL:</div>\n"
1637 "<div id='cloneurl'>%s</div>\n",
1638 t->repo_dir->url ? t->repo_dir->url : "");
1643 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1647 error = gotweb_render_briefs(c);
1649 log_warnx("%s: %s", __func__, error->msg);
1653 error = gotweb_render_tags(c);
1655 log_warnx("%s: %s", __func__, error->msg);
1659 error = gotweb_render_branches(c);
1661 log_warnx("%s: %s", __func__, error->msg);
1666 static const struct got_error *
1667 gotweb_render_tag(struct request *c)
1669 const struct got_error *error = NULL;
1670 struct repo_tag *rt = NULL;
1671 struct transport *t = c->t;
1672 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1674 error = got_get_repo_tags(c, 1);
1678 if (t->tag_count == 0) {
1679 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1684 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1686 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1689 error = gotweb_escape_html(&author, rt->tagger);
1692 error = gotweb_escape_html(&msg, rt->commit_msg);
1696 if (strncmp(rt->tag_name, "refs/", 5) == 0)
1698 error = gotweb_escape_html(&tagname, rt->tag_name);
1702 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1703 "<div id='tags_title'>Tag</div>\n"
1704 "</div>\n" /* #tags_title_wrapper */
1705 "<div id='tags_content'>\n"
1706 "<div id='tag_header_wrapper'>\n"
1707 "<div id='tag_header'>\n"
1708 "<div class='header_commit_title'>Commit:</div>\n"
1709 "<div class='header_commit'>%s"
1710 " <span class='refs_str'>(%s)</span></div>\n"
1711 "<div class='header_author_title'>Tagger:</div>\n"
1712 "<div class='header_author'>%s</div>\n"
1713 "<div class='header_age_title'>Date:</div>\n"
1714 "<div class='header_age'>%s</div>\n"
1715 "<div id='header_commit_msg_title'>Message:</div>\n"
1716 "<div id='header_commit_msg'>%s</div>\n"
1717 "</div>\n" /* #tag_header */
1718 "<div class='dotted_line'></div>\n"
1719 "<div id='tag_commit'>\n%s</div>"
1720 "</div>", /* tag_header_wrapper */
1735 static const struct got_error *
1736 gotweb_render_tags(struct request *c)
1738 const struct got_error *error = NULL;
1739 struct repo_tag *rt = NULL;
1740 struct server *srv = c->srv;
1741 struct transport *t = c->t;
1742 struct querystring *qs = t->qs;
1743 struct repo_dir *repo_dir = t->repo_dir;
1744 const char *index_page_str;
1745 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1746 int r, commit_found = 0;
1748 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1750 if (qs->action == BRIEFS) {
1752 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1754 error = got_get_repo_tags(c, srv->max_commits_display);
1758 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1759 "<div id='tags_title'>Tags</div>\n"
1760 "</div>\n" /* #tags_title_wrapper */
1761 "<div id='tags_content'>\n");
1765 if (t->tag_count == 0) {
1766 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1767 "This repository contains no tags");
1772 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1773 if (commit_found == 0 && qs->commit != NULL) {
1774 if (strcmp(qs->commit, rt->commit_id) != 0)
1779 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1783 if (strncmp(rt->tag_name, "refs/tags/", 10) == 0)
1785 error = gotweb_escape_html(&tagname, rt->tag_name);
1789 if (rt->tag_commit != NULL) {
1790 newline = strchr(rt->tag_commit, '\n');
1793 error = gotweb_escape_html(&msg, rt->tag_commit);
1798 r = fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1799 "<div class='tag'>%s</div>\n"
1800 "<div class='tag_log'>"
1801 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1803 "</div>\n" /* .tag_log */
1804 "<div class='navs_wrapper'>\n"
1805 "<div class='navs'>"
1806 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1809 "<a href='?index_page=%s&path=%s&action=briefs&commit=%s'>"
1812 "<a href='?index_page=%s&path=%s&action=commits&commit=%s'>"
1814 "</div>\n" /* .navs */
1815 "</div>\n" /* .navs_wrapper */
1816 "<div class='dotted_line'></div>\n",
1819 index_page_str, repo_dir->name, rt->commit_id,
1821 index_page_str, repo_dir->name, rt->commit_id,
1822 index_page_str, repo_dir->name, rt->commit_id,
1823 index_page_str, repo_dir->name, rt->commit_id);
1834 if (t->next_id || t->prev_id) {
1835 error = gotweb_render_navs(c);
1839 fcgi_printf(c, "</div>\n"); /* #tags_content */
1847 const struct got_error *
1848 gotweb_escape_html(char **escaped_html, const char *orig_html)
1850 const struct got_error *error = NULL;
1851 struct escape_pair {
1862 size_t orig_len, len;
1865 orig_len = strlen(orig_html);
1867 for (i = 0; i < orig_len; i++) {
1868 for (j = 0; j < nitems(esc); j++) {
1869 if (orig_html[i] != esc[j].c)
1871 len += strlen(esc[j].s) - 1 /* escaped char */;
1875 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1876 if (*escaped_html == NULL)
1877 return got_error_from_errno("calloc");
1880 for (i = 0; i < orig_len; i++) {
1882 for (j = 0; j < nitems(esc); j++) {
1883 if (orig_html[i] != esc[j].c)
1886 if (strlcat(*escaped_html, esc[j].s, len + 1)
1888 error = got_error(GOT_ERR_NO_SPACE);
1891 x += strlen(esc[j].s);
1896 (*escaped_html)[x] = orig_html[i];
1902 free(*escaped_html);
1903 *escaped_html = NULL;
1905 (*escaped_html)[x] = '\0';
1911 static struct got_repository *
1912 find_cached_repo(struct server *srv, const char *path)
1916 for (i = 0; i < srv->ncached_repos; i++) {
1917 if (strcmp(srv->cached_repos[i].path, path) == 0)
1918 return srv->cached_repos[i].repo;
1924 static const struct got_error *
1925 cache_repo(struct got_repository **new, struct server *srv,
1926 struct repo_dir *repo_dir, struct socket *sock)
1928 const struct got_error *error = NULL;
1929 struct got_repository *repo;
1930 struct cached_repo *cr;
1933 if (srv->ncached_repos >= nitems(srv->cached_repos)) {
1934 cr = &srv->cached_repos[srv->ncached_repos - 1];
1935 error = got_repo_close(cr->repo);
1936 memset(cr, 0, sizeof(*cr));
1937 srv->ncached_repos--;
1940 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1941 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1942 cr = &srv->cached_repos[0];
1945 cr = &srv->cached_repos[srv->ncached_repos];
1948 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1951 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1952 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1957 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1958 >= sizeof(cr->path)) {
1960 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1961 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1963 return got_error(GOT_ERR_NO_SPACE);
1967 srv->ncached_repos++;
1972 static const struct got_error *
1973 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1975 const struct got_error *error = NULL;
1976 struct socket *sock = c->sock;
1977 struct server *srv = c->srv;
1978 struct transport *t = c->t;
1979 struct got_repository *repo = NULL;
1983 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1984 GOTWEB_GIT_DIR) == -1)
1985 return got_error_from_errno("asprintf");
1987 dt = opendir(dir_test);
1991 repo_dir->path = dir_test;
1996 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1997 repo_dir->name) == -1)
1998 return got_error_from_errno("asprintf");
2000 dt = opendir(dir_test);
2002 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2005 repo_dir->path = dir_test;
2010 repo = find_cached_repo(srv, repo_dir->path);
2012 error = cache_repo(&repo, srv, repo_dir, sock);
2017 error = gotweb_get_repo_description(&repo_dir->description, srv,
2021 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
2024 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
2028 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2031 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2032 error = got_error_from_errno("closedir");
2036 static const struct got_error *
2037 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2039 const struct got_error *error;
2041 *repo_dir = calloc(1, sizeof(**repo_dir));
2042 if (*repo_dir == NULL)
2043 return got_error_from_errno("calloc");
2045 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2046 error = got_error_from_errno("asprintf");
2051 (*repo_dir)->owner = NULL;
2052 (*repo_dir)->description = NULL;
2053 (*repo_dir)->url = NULL;
2054 (*repo_dir)->age = NULL;
2055 (*repo_dir)->path = NULL;
2060 static const struct got_error *
2061 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2063 const struct got_error *error = NULL;
2065 char *d_file = NULL;
2069 *description = NULL;
2070 if (srv->show_repo_description == 0)
2073 if (asprintf(&d_file, "%s/description", dir) == -1)
2074 return got_error_from_errno("asprintf");
2076 f = fopen(d_file, "r");
2078 if (errno == ENOENT || errno == EACCES)
2080 error = got_error_from_errno2("fopen", d_file);
2084 if (fseek(f, 0, SEEK_END) == -1) {
2085 error = got_ferror(f, GOT_ERR_IO);
2090 error = got_ferror(f, GOT_ERR_IO);
2095 *description = strdup("");
2096 if (*description == NULL)
2097 return got_error_from_errno("strdup");
2101 if (fseek(f, 0, SEEK_SET) == -1) {
2102 error = got_ferror(f, GOT_ERR_IO);
2105 *description = calloc(len + 1, sizeof(**description));
2106 if (*description == NULL) {
2107 error = got_error_from_errno("calloc");
2111 n = fread(*description, 1, len, f);
2112 if (n == 0 && ferror(f))
2113 error = got_ferror(f, GOT_ERR_IO);
2115 if (f != NULL && fclose(f) == EOF && error == NULL)
2116 error = got_error_from_errno("fclose");
2121 static const struct got_error *
2122 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2124 const struct got_error *error = NULL;
2126 char *d_file = NULL;
2132 if (srv->show_repo_cloneurl == 0)
2135 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2136 return got_error_from_errno("asprintf");
2138 f = fopen(d_file, "r");
2140 if (errno != ENOENT && errno != EACCES)
2141 error = got_error_from_errno2("fopen", d_file);
2145 if (fseek(f, 0, SEEK_END) == -1) {
2146 error = got_ferror(f, GOT_ERR_IO);
2151 error = got_ferror(f, GOT_ERR_IO);
2157 if (fseek(f, 0, SEEK_SET) == -1) {
2158 error = got_ferror(f, GOT_ERR_IO);
2162 *url = calloc(len + 1, sizeof(**url));
2164 error = got_error_from_errno("calloc");
2168 n = fread(*url, 1, len, f);
2169 if (n == 0 && ferror(f))
2170 error = got_ferror(f, GOT_ERR_IO);
2172 if (f != NULL && fclose(f) == EOF && error == NULL)
2173 error = got_error_from_errno("fclose");
2178 const struct got_error *
2179 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2182 long long diff_time;
2183 const char *years = "years ago", *months = "months ago";
2184 const char *weeks = "weeks ago", *days = "days ago";
2185 const char *hours = "hours ago", *minutes = "minutes ago";
2186 const char *seconds = "seconds ago", *now = "right now";
2194 diff_time = time(NULL) - committer_time;
2195 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2196 if (asprintf(repo_age, "%lld %s",
2197 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2198 return got_error_from_errno("asprintf");
2199 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2200 if (asprintf(repo_age, "%lld %s",
2201 (diff_time / 60 / 60 / 24 / (365 / 12)),
2203 return got_error_from_errno("asprintf");
2204 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2205 if (asprintf(repo_age, "%lld %s",
2206 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2207 return got_error_from_errno("asprintf");
2208 } else if (diff_time > 60 * 60 * 24 * 2) {
2209 if (asprintf(repo_age, "%lld %s",
2210 (diff_time / 60 / 60 / 24), days) == -1)
2211 return got_error_from_errno("asprintf");
2212 } else if (diff_time > 60 * 60 * 2) {
2213 if (asprintf(repo_age, "%lld %s",
2214 (diff_time / 60 / 60), hours) == -1)
2215 return got_error_from_errno("asprintf");
2216 } else if (diff_time > 60 * 2) {
2217 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2219 return got_error_from_errno("asprintf");
2220 } else if (diff_time > 2) {
2221 if (asprintf(repo_age, "%lld %s", diff_time,
2223 return got_error_from_errno("asprintf");
2225 if (asprintf(repo_age, "%s", now) == -1)
2226 return got_error_from_errno("asprintf");
2230 if (gmtime_r(&committer_time, &tm) == NULL)
2231 return got_error_from_errno("gmtime_r");
2233 s = asctime_r(&tm, datebuf);
2235 return got_error_from_errno("asctime_r");
2237 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2238 return got_error_from_errno("asprintf");