2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <netinet/in.h>
23 #include <sys/queue.h>
25 #include <sys/types.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
58 static const struct querystring_keys querystring_keys[] = {
63 { "headref", HEADREF },
64 { "index_page", INDEX_PAGE },
69 static const struct action_keys action_keys[] = {
73 { "commits", COMMITS },
77 { "summary", SUMMARY },
83 static const struct got_error *gotweb_init_querystring(struct querystring **);
84 static const struct got_error *gotweb_parse_querystring(struct querystring **,
86 static const struct got_error *gotweb_assign_querystring(struct querystring **,
88 static const struct got_error *gotweb_render_header(struct request *);
89 static const struct got_error *gotweb_render_footer(struct request *);
90 static const struct got_error *gotweb_render_index(struct request *);
91 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
93 static const struct got_error *gotweb_load_got_path(struct request *c,
95 static const struct got_error *gotweb_get_repo_description(char **,
96 struct server *, char *);
97 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
99 static const struct got_error *gotweb_render_navs(struct request *);
100 static const struct got_error *gotweb_render_blame(struct request *);
101 static const struct got_error *gotweb_render_briefs(struct request *);
102 static const struct got_error *gotweb_render_commits(struct request *);
103 static const struct got_error *gotweb_render_diff(struct request *);
104 static const struct got_error *gotweb_render_summary(struct request *);
105 static const struct got_error *gotweb_render_tag(struct request *);
106 static const struct got_error *gotweb_render_tags(struct request *);
107 static const struct got_error *gotweb_render_tree(struct request *);
108 static const struct got_error *gotweb_render_branches(struct request *);
110 static void gotweb_free_querystring(struct querystring *);
111 static void gotweb_free_repo_dir(struct repo_dir *);
113 struct server *gotweb_get_server(uint8_t *, uint8_t *);
116 gotweb_process_request(struct request *c)
118 const struct got_error *error = NULL, *error2 = NULL;
119 struct server *srv = NULL;
120 struct querystring *qs = NULL;
121 struct repo_dir *repo_dir = NULL;
122 uint8_t err[] = "gotwebd experienced an error: ";
125 /* init the transport */
126 error = gotweb_init_transport(&c->t);
128 log_warnx("%s: %s", __func__, error->msg);
131 /* don't process any further if client disconnected */
132 if (c->sock->client_status == CLIENT_DISCONNECT)
134 /* get the gotwebd server */
135 srv = gotweb_get_server(c->server_name, c->http_host);
137 log_warnx("%s: error server is NULL", __func__);
141 /* parse our querystring */
142 error = gotweb_init_querystring(&qs);
144 log_warnx("%s: %s", __func__, error->msg);
148 error = gotweb_parse_querystring(&qs, c->querystring);
150 log_warnx("%s: %s", __func__, error->msg);
155 * certain actions require a commit id in the querystring. this stops
156 * bad actors from exploiting this by manually manipulating the
160 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
161 qs->action == DIFF)) {
162 error2 = got_error(GOT_ERR_QUERYSTRING);
166 if (qs->action != INDEX) {
167 error = gotweb_init_repo_dir(&repo_dir, qs->path);
170 error = gotweb_load_got_path(c, repo_dir);
171 c->t->repo_dir = repo_dir;
172 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
176 /* render top of page */
177 if (qs != NULL && qs->action == BLOB) {
178 error = got_get_repo_commits(c, 1);
181 error = got_output_file_blob(c);
183 log_warnx("%s: %s", __func__, error->msg);
189 error = gotweb_render_content_type(c, "text/html");
191 log_warnx("%s: %s", __func__, error->msg);
197 error = gotweb_render_header(c);
199 log_warnx("%s: %s", __func__, error->msg);
210 error = gotweb_render_blame(c);
212 log_warnx("%s: %s", __func__, error->msg);
217 error = gotweb_render_briefs(c);
219 log_warnx("%s: %s", __func__, error->msg);
224 error = gotweb_render_commits(c);
226 log_warnx("%s: %s", __func__, error->msg);
231 error = gotweb_render_diff(c);
233 log_warnx("%s: %s", __func__, error->msg);
238 error = gotweb_render_index(c);
240 log_warnx("%s: %s", __func__, error->msg);
245 error = gotweb_render_summary(c);
247 log_warnx("%s: %s", __func__, error->msg);
252 error = gotweb_render_tag(c);
254 log_warnx("%s: %s", __func__, error->msg);
259 error = gotweb_render_tags(c);
261 log_warnx("%s: %s", __func__, error->msg);
266 error = gotweb_render_tree(c);
268 log_warnx("%s: %s", __func__, error->msg);
274 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
275 "Erorr: Bad Querystring");
283 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
285 if (fcgi_printf(c, "%s", err) == -1)
288 if (fcgi_printf(c, "%s", error->msg) == -1)
291 if (fcgi_printf(c, "see daemon logs for details") == -1)
294 if (html && fcgi_printf(c, "</div>\n") == -1)
297 if (html && srv != NULL)
298 gotweb_render_footer(c);
302 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
304 struct server *srv = NULL;
306 /* check against the server name first */
307 if (strlen(server_name) > 0)
308 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
309 if (strcmp(srv->name, server_name) == 0)
312 /* check against subdomain second */
313 if (strlen(subdomain) > 0)
314 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
315 if (strcmp(srv->name, subdomain) == 0)
318 /* if those fail, send first server */
319 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
326 const struct got_error *
327 gotweb_init_transport(struct transport **t)
329 const struct got_error *error = NULL;
331 *t = calloc(1, sizeof(**t));
333 return got_error_from_errno2("%s: calloc", __func__);
335 TAILQ_INIT(&(*t)->repo_commits);
336 TAILQ_INIT(&(*t)->repo_tags);
339 (*t)->repo_dir = NULL;
341 (*t)->next_id = NULL;
342 (*t)->prev_id = NULL;
349 static const struct got_error *
350 gotweb_init_querystring(struct querystring **qs)
352 const struct got_error *error = NULL;
354 *qs = calloc(1, sizeof(**qs));
356 return got_error_from_errno2("%s: calloc", __func__);
358 (*qs)->headref = strdup("HEAD");
359 if ((*qs)->headref == NULL) {
362 return got_error_from_errno2("%s: strdup", __func__);
365 (*qs)->action = INDEX;
366 (*qs)->commit = NULL;
368 (*qs)->folder = NULL;
369 (*qs)->index_page = 0;
370 (*qs)->index_page_str = NULL;
376 static const struct got_error *
377 gotweb_parse_querystring(struct querystring **qs, char *qst)
379 const struct got_error *error = NULL;
380 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
381 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
388 return got_error_from_errno2("%s: strdup", __func__);
393 while (tok1_pair != NULL) {
394 strsep(&tok1_end, "&");
396 tok2 = strdup(tok1_pair);
399 return got_error_from_errno2("%s: strdup", __func__);
405 while (tok2_pair != NULL) {
406 strsep(&tok2_end, "=");
408 error = gotweb_assign_querystring(qs, tok2_pair,
413 tok2_pair = tok2_end;
416 tok1_pair = tok1_end;
427 * Adapted from usr.sbin/httpd/httpd.c url_decode.
429 static const struct got_error *
430 gotweb_urldecode(char *url)
442 /* Encoding character is followed by two hex chars */
443 if (!isxdigit((unsigned char)p[1]) ||
444 !isxdigit((unsigned char)p[2]) ||
445 (p[1] == '0' && p[2] == '0'))
446 return got_error(GOT_ERR_BAD_QUERYSTRING);
452 * We don't have to validate "hex" because it is
453 * guaranteed to include two hex chars followed by nul.
455 x = strtoul(hex, NULL, 16);
471 static const struct got_error *
472 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
474 const struct got_error *error = NULL;
478 error = gotweb_urldecode(value);
482 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
483 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
486 switch (querystring_keys[el_cnt].element) {
488 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
489 if (strcmp(value, action_keys[a_cnt].name) != 0)
491 else if (strcmp(value,
492 action_keys[a_cnt].name) == 0){
494 action_keys[a_cnt].action;
502 (*qs)->commit = strdup(value);
503 if ((*qs)->commit == NULL) {
504 error = got_error_from_errno2("%s: strdup",
510 (*qs)->file = strdup(value);
511 if ((*qs)->file == NULL) {
512 error = got_error_from_errno2("%s: strdup",
518 (*qs)->folder = strdup(value);
519 if ((*qs)->folder == NULL) {
520 error = got_error_from_errno2("%s: strdup",
526 free((*qs)->headref);
527 (*qs)->headref = strdup(value);
528 if ((*qs)->headref == NULL) {
529 error = got_error_from_errno2("%s: strdup",
535 if (strlen(value) == 0)
537 (*qs)->index_page_str = strdup(value);
538 if ((*qs)->index_page_str == NULL) {
539 error = got_error_from_errno2("%s: strdup",
543 (*qs)->index_page = strtonum(value, INT64_MIN,
546 error = got_error_from_errno3("%s: strtonum %s",
550 if ((*qs)->index_page < 0) {
551 (*qs)->index_page = 0;
552 sprintf((*qs)->index_page_str, "%d", 0);
556 (*qs)->path = strdup(value);
557 if ((*qs)->path == NULL) {
558 error = got_error_from_errno2("%s: strdup",
564 if (strlen(value) == 0)
566 (*qs)->page_str = strdup(value);
567 if ((*qs)->page_str == NULL) {
568 error = got_error_from_errno2("%s: strdup",
572 (*qs)->page = strtonum(value, INT64_MIN,
575 error = got_error_from_errno3("%s: strtonum %s",
579 if ((*qs)->page < 0) {
581 sprintf((*qs)->page_str, "%d", 0);
593 gotweb_free_repo_tag(struct repo_tag *rt)
598 free(rt->tag_commit);
599 free(rt->commit_msg);
606 gotweb_free_repo_commit(struct repo_commit *rc)
616 free(rc->commit_msg);
622 gotweb_free_querystring(struct querystring *qs)
629 free(qs->index_page_str);
637 gotweb_free_repo_dir(struct repo_dir *repo_dir)
639 if (repo_dir != NULL) {
640 free(repo_dir->name);
641 free(repo_dir->owner);
642 free(repo_dir->description);
645 free(repo_dir->path);
651 gotweb_free_transport(struct transport *t)
653 struct repo_commit *rc = NULL, *trc = NULL;
654 struct repo_tag *rt = NULL, *trt = NULL;
656 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
657 TAILQ_REMOVE(&t->repo_commits, rc, entry);
658 gotweb_free_repo_commit(rc);
660 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
661 TAILQ_REMOVE(&t->repo_tags, rt, entry);
662 gotweb_free_repo_tag(rt);
664 gotweb_free_repo_dir(t->repo_dir);
665 gotweb_free_querystring(t->qs);
671 const struct got_error *
672 gotweb_render_content_type(struct request *c, const uint8_t *type)
674 const char *csp = "default-src 'self'; script-src 'none'; "
675 "object-src 'none';";
678 "Content-Security-Policy: %s\r\n"
679 "Content-Type: %s\r\n\r\n",
684 const struct got_error *
685 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
688 fcgi_printf(c, "Content-type: %s\r\n"
689 "Content-disposition: attachment; filename=%s\r\n\r\n",
694 static const struct got_error *
695 gotweb_render_header(struct request *c)
697 struct server *srv = c->srv;
698 struct querystring *qs = c->t->qs;
701 r = fcgi_printf(c, "<!doctype html>\n"
704 "<title>%s</title>\n"
705 "<meta charset='utf-8' />\n"
706 "<meta name='viewport' content='initial-scale=.75' />\n"
707 "<meta name='msapplication-TileColor' content='#da532c' />\n"
708 "<meta name='theme-color' content='#ffffff'/>\n"
709 "<link rel='apple-touch-icon' sizes='180x180'"
710 " href='%sapple-touch-icon.png' />\n"
711 "<link rel='icon' type='image/png' sizes='32x32'"
712 " href='%sfavicon-32x32.png' />\n"
713 "<link rel='icon' type='image/png' sizes='16x16'"
714 " href='%sfavicon-16x16.png' />\n"
715 "<link rel='manifest' href='%ssite.webmanifest'/>\n"
716 "<link rel='mask-icon' href='%ssafari-pinned-tab.svg' />\n"
717 "<link rel='stylesheet' type='text/css' href='%s%s' />\n"
720 "<div id='gw_body'>\n"
721 "<div id='header'>\n"
722 "<div id='got_link'>"
723 "<a href='%s' target='_blank'>"
724 "<img src='%s%s' alt='logo' id='logo' />"
726 "</div>\n" /* #got_link */
727 "</div>\n" /* #header */
728 "<div id='site_path'>\n"
729 "<div id='site_link'>\n"
730 "<a href='?index_page=%d'>%s</a>",
737 c->script_name, srv->custom_css,
739 c->script_name, srv->logo,
740 qs->index_page, srv->site_link);
745 if (qs->path != NULL) {
746 r = fcgi_printf(c, " / "
747 "<a href='?index_page=%d&path=%s&action=summary'>"
749 qs->index_page, qs->path, qs->path);
753 if (qs->action != INDEX) {
754 const char *action = "";
756 switch (qs->action) {
783 if (fcgi_printf(c, " / %s", action) == -1)
788 fcgi_printf(c, "</div>\n" /* #site_path */
789 "</div>\n" /* #site_link */
790 "<div id='content'>\n");
796 static const struct got_error *
797 gotweb_render_footer(struct request *c)
799 const struct got_error *error = NULL;
800 struct server *srv = c->srv;
801 const char *siteowner = " ";
802 char *escaped_owner = NULL;
804 if (srv->show_site_owner) {
805 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
808 siteowner = escaped_owner;
811 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
812 "<div id='site_owner'>%s</div>\n"
813 "</div>\n" /* #site_owner_wrapper */
814 "</div>\n" /* #content */
815 "</div>\n" /* #gw_body */
816 "</body>\n</html>\n", siteowner);
822 static const struct got_error *
823 gotweb_render_navs(struct request *c)
825 const struct got_error *error = NULL;
826 struct transport *t = c->t;
827 struct querystring *qs = t->qs;
828 struct server *srv = c->srv;
829 char *nhref = NULL, *phref = NULL;
832 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
838 if (qs->index_page > 0) {
839 if (asprintf(&phref, "index_page=%d",
840 qs->index_page - 1) == -1) {
841 error = got_error_from_errno2("%s: asprintf",
849 if (t->prev_id && qs->commit != NULL &&
850 strcmp(qs->commit, t->prev_id) != 0) {
851 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
852 "&action=briefs&commit=%s&headref=%s",
853 qs->index_page, qs->path, qs->page - 1, t->prev_id,
854 qs->headref) == -1) {
855 error = got_error_from_errno2("%s: asprintf",
863 if (t->prev_id && qs->commit != NULL &&
864 strcmp(qs->commit, t->prev_id) != 0) {
865 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
866 "&action=commits&commit=%s&headref=%s&folder=%s"
868 qs->index_page, qs->path, qs->page - 1, t->prev_id,
869 qs->headref, qs->folder ? qs->folder : "",
870 qs->file ? qs->file : "") == -1) {
871 error = got_error_from_errno2("%s: asprintf",
879 if (t->prev_id && qs->commit != NULL &&
880 strcmp(qs->commit, t->prev_id) != 0) {
881 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
882 "&action=tags&commit=%s&headref=%s",
883 qs->index_page, qs->path, qs->page - 1, t->prev_id,
884 qs->headref) == -1) {
885 error = got_error_from_errno2("%s: asprintf",
898 r = fcgi_printf(c, "<a href='?%s'>Previous</a>", phref);
903 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
904 "<div id='nav_next'>");
911 if (t->next_disp == srv->max_repos_display &&
912 t->repos_total != (qs->index_page + 1) *
913 srv->max_repos_display) {
914 if (asprintf(&nhref, "index_page=%d",
915 qs->index_page + 1) == -1) {
916 error = got_error_from_errno2("%s: asprintf",
925 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
926 "&action=briefs&commit=%s&headref=%s",
927 qs->index_page, qs->path, qs->page + 1, t->next_id,
928 qs->headref) == -1) {
929 error = got_error_from_errno2("%s: asprintf",
938 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
939 "&action=commits&commit=%s&headref=%s&folder=%s"
941 qs->index_page, qs->path, qs->page + 1, t->next_id,
942 qs->headref, qs->folder ? qs->folder : "",
943 qs->file ? qs->file : "") == -1) {
944 error = got_error_from_errno2("%s: asprintf",
953 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
954 "&action=tags&commit=%s&headref=%s",
955 qs->index_page, qs->path, qs->page + 1, t->next_id,
956 qs->headref) == -1) {
957 error = got_error_from_errno2("%s: asprintf",
969 r = fcgi_printf(c, "<a href='?%s'>Next</a>", nhref);
973 fcgi_printf(c, "</div>\n"); /* #nav_next */
974 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
985 static const struct got_error *
986 gotweb_render_index(struct request *c)
988 const struct got_error *error = NULL;
989 struct server *srv = c->srv;
990 struct transport *t = c->t;
991 struct querystring *qs = t->qs;
992 struct repo_dir *repo_dir = NULL;
994 struct dirent **sd_dent = NULL;
995 const char *index_page_str;
998 unsigned int d_cnt, d_i, d_disp = 0;
1001 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1003 d = opendir(srv->repos_path);
1005 error = got_error_from_errno2("opendir", srv->repos_path);
1009 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
1012 error = got_error_from_errno2("scandir", srv->repos_path);
1016 /* get total count of repos */
1017 for (d_i = 0; d_i < d_cnt; d_i++) {
1018 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1019 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1022 if (asprintf(&c_path, "%s/%s", srv->repos_path,
1023 sd_dent[d_i]->d_name) == -1) {
1024 error = got_error_from_errno("asprintf");
1028 if (lstat(c_path, &st) == 0 && S_ISDIR(st.st_mode) &&
1029 !got_path_dir_is_empty(c_path))
1035 r = fcgi_printf(c, "<div id='index_header'>\n"
1036 "<div id='index_header_project'>Project</div>\n");
1040 if (srv->show_repo_description)
1041 if (fcgi_printf(c, "<div id='index_header_description'>"
1042 "Description</div>\n") == -1)
1044 if (srv->show_repo_owner)
1045 if (fcgi_printf(c, "<div id='index_header_owner'>"
1046 "Owner</div>\n") == -1)
1048 if (srv->show_repo_age)
1049 if (fcgi_printf(c, "<div id='index_header_age'>"
1050 "Last Change</div>\n") == -1)
1052 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1055 for (d_i = 0; d_i < d_cnt; d_i++) {
1056 if (srv->max_repos > 0 && (d_i - 2) == srv->max_repos)
1057 break; /* account for parent and self */
1059 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1060 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1063 if (qs->index_page > 0 && (qs->index_page *
1064 srv->max_repos_display) > t->prev_disp) {
1069 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1073 error = gotweb_load_got_path(c, repo_dir);
1074 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1078 else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1081 if (lstat(repo_dir->path, &st) == 0 &&
1082 S_ISDIR(st.st_mode) &&
1083 !got_path_dir_is_empty(repo_dir->path))
1086 gotweb_free_repo_dir(repo_dir);
1094 r = fcgi_printf(c, "<div class='index_wrapper'>\n"
1095 "<div class='index_project'>"
1096 "<a href='?index_page=%s&path=%s&action=summary'>"
1099 "</div>", /* .index_project */
1100 index_page_str, repo_dir->name,
1105 if (srv->show_repo_description) {
1107 "<div class='index_project_description'>\n"
1108 "%s</div>\n", repo_dir->description);
1113 if (srv->show_repo_owner) {
1114 r = fcgi_printf(c, "<div class='index_project_owner'>"
1115 "%s</div>\n", repo_dir->owner);
1120 if (srv->show_repo_age) {
1121 r = fcgi_printf(c, "<div class='index_project_age'>"
1122 "%s</div>\n", repo_dir->age);
1127 r = fcgi_printf(c, "<div class='navs_wrapper'>"
1128 "<div class='navs'>"
1129 "<a href='?index_page=%s&path=%s&action=summary'>"
1132 "<a href='?index_page=%s&path=%s&action=briefs'>"
1135 "<a href='?index_page=%s&path=%s&action=commits'>"
1138 "<a href='?index_page=%s&path=%s&action=tags'>"
1141 "<a href='?index_page=%s&path=%s&action=tree'>"
1144 "</div>" /* .navs */
1145 "<div class='dotted_line'></div>\n"
1146 "</div>\n" /* .navs_wrapper */
1147 "</div>\n", /* .index_wrapper */
1148 index_page_str, repo_dir->name,
1149 index_page_str, repo_dir->name,
1150 index_page_str, repo_dir->name,
1151 index_page_str, repo_dir->name,
1152 index_page_str, repo_dir->name);
1156 gotweb_free_repo_dir(repo_dir);
1159 if (d_disp == srv->max_repos_display)
1162 if (srv->max_repos_display == 0)
1164 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1166 if (t->repos_total <= srv->max_repos ||
1167 t->repos_total <= srv->max_repos_display)
1170 error = gotweb_render_navs(c);
1175 for (d_i = 0; d_i < d_cnt; d_i++)
1179 if (d != NULL && closedir(d) == EOF && error == NULL)
1180 error = got_error_from_errno("closedir");
1184 static const struct got_error *
1185 gotweb_render_blame(struct request *c)
1187 const struct got_error *error = NULL;
1188 struct transport *t = c->t;
1189 struct repo_commit *rc = NULL;
1190 char *age = NULL, *msg = NULL;
1193 error = got_get_repo_commits(c, 1);
1197 rc = TAILQ_FIRST(&t->repo_commits);
1199 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1202 error = gotweb_escape_html(&msg, rc->commit_msg);
1206 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1207 "<div id='blame_title'>Blame</div>\n"
1208 "</div>\n" /* #blame_title_wrapper */
1209 "<div id='blame_content'>\n"
1210 "<div id='blame_header_wrapper'>\n"
1211 "<div id='blame_header'>\n"
1212 "<div class='header_age_title'>Date:</div>\n"
1213 "<div class='header_age'>%s</div>\n"
1214 "<div id='header_commit_msg_title'>Message:</div>\n"
1215 "<div id='header_commit_msg'>%s</div>\n"
1216 "</div>\n" /* #blame_header */
1217 "</div>\n" /* #blame_header_wrapper */
1218 "<div class='dotted_line'></div>\n"
1219 "<div id='blame'>\n",
1225 error = got_output_file_blame(c);
1229 fcgi_printf(c, "</div>\n" /* #blame */
1230 "</div>\n"); /* #blame_content */
1237 static const struct got_error *
1238 gotweb_render_briefs(struct request *c)
1240 const struct got_error *error = NULL;
1241 struct repo_commit *rc = NULL;
1242 struct server *srv = c->srv;
1243 struct transport *t = c->t;
1244 struct querystring *qs = t->qs;
1245 struct repo_dir *repo_dir = t->repo_dir;
1246 const char *index_page_str;
1247 char *smallerthan, *newline;
1248 char *age = NULL, *author = NULL, *msg = NULL;
1251 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1253 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1254 "<div id='briefs_title'>Commit Briefs</div>\n"
1255 "</div>\n" /* #briefs_title_wrapper */
1256 "<div id='briefs_content'>\n");
1260 if (qs->action == SUMMARY) {
1261 qs->action = BRIEFS;
1262 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1264 error = got_get_repo_commits(c, srv->max_commits_display);
1268 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1269 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1273 smallerthan = strchr(rc->author, '<');
1275 *smallerthan = '\0';
1277 newline = strchr(rc->commit_msg, '\n');
1281 error = gotweb_escape_html(&author, rc->author);
1284 error = gotweb_escape_html(&msg, rc->commit_msg);
1288 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1289 "<div class='briefs_author'>%s</div>\n"
1290 "<div class='briefs_log'>"
1291 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1292 "&headref=%s'>%s</a>",
1295 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1303 error = gotweb_escape_html(&refs, rc->refs_str);
1307 " <span class='refs_str'>(%s)</span>", refs);
1312 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1315 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1316 "<div class='navs'>"
1317 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1318 "&headref=%s'>diff</a>"
1320 "<a href='?index_page=%s&path=%s&action=tree&commit=%s"
1321 "&headref=%s'>tree</a>"
1322 "</div>\n" /* .navs */
1323 "</div>\n" /* .navs_wrapper */
1324 "<div class='dotted_line'></div>\n",
1325 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1326 index_page_str, repo_dir->name, rc->commit_id, qs->headref);
1338 if (t->next_id || t->prev_id) {
1339 error = gotweb_render_navs(c);
1343 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1351 static const struct got_error *
1352 gotweb_render_commits(struct request *c)
1354 const struct got_error *error = NULL;
1355 struct repo_commit *rc = NULL;
1356 struct server *srv = c->srv;
1357 struct transport *t = c->t;
1358 struct querystring *qs = t->qs;
1359 struct repo_dir *repo_dir = t->repo_dir;
1360 const char *index_page_str;
1361 char *age = NULL, *author = NULL, *msg = NULL;
1364 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1366 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1367 "<div class='commits_title'>Commits</div>\n"
1368 "</div>\n" /* .commits_title_wrapper */
1369 "<div class='commits_content'>\n");
1373 error = got_get_repo_commits(c, srv->max_commits_display);
1377 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1378 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1381 error = gotweb_escape_html(&author, rc->author);
1384 error = gotweb_escape_html(&msg, rc->commit_msg);
1388 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1389 "<div class='commits_header'>\n"
1390 "<div class='header_commit_title'>Commit:</div>\n"
1391 "<div class='header_commit'>%s</div>\n"
1392 "<div class='header_author_title'>Author:</div>\n"
1393 "<div class='header_author'>%s</div>\n"
1394 "<div class='header_age_title'>Date:</div>\n"
1395 "<div class='header_age'>%s</div>\n"
1396 "</div>\n" /* .commits_header */
1397 "</div>\n" /* .commits_header_wrapper */
1398 "<div class='dotted_line'></div>\n"
1399 "<div class='commit'>\n%s</div>\n",
1407 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1408 "<div class='navs'>"
1409 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1412 "<a href='?index_page=%s&path=%s&action=tree&commit=%s'>"
1414 "</div>\n" /* .navs */
1415 "</div>\n" /* .navs_wrapper */
1416 "<div class='dotted_line'></div>\n",
1417 index_page_str, repo_dir->name, rc->commit_id,
1418 index_page_str, repo_dir->name, rc->commit_id);
1430 if (t->next_id || t->prev_id) {
1431 error = gotweb_render_navs(c);
1435 fcgi_printf(c, "</div>\n"); /* .commits_content */
1443 static const struct got_error *
1444 gotweb_render_branches(struct request *c)
1446 const struct got_error *error = NULL;
1447 struct got_reflist_head refs;
1448 struct got_reflist_entry *re;
1449 struct transport *t = c->t;
1450 struct querystring *qs = t->qs;
1451 struct got_repository *repo = t->repo;
1452 const char *index_page_str;
1456 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1460 error = got_ref_list(&refs, repo, "refs/heads",
1461 got_ref_cmp_by_name, NULL);
1465 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1466 "<div id='branches_title'>Branches</div>\n"
1467 "</div>\n" /* #branches_title_wrapper */
1468 "<div id='branches_content'>\n");
1472 TAILQ_FOREACH(re, &refs, entry) {
1473 const char *refname = NULL;
1474 char *escaped_refname = NULL;
1476 if (got_ref_is_symbolic(re->ref))
1479 refname = got_ref_get_name(re->ref);
1480 if (refname == NULL) {
1481 error = got_error_from_errno("strdup");
1484 if (strncmp(refname, "refs/heads/", 11) != 0)
1487 error = got_get_repo_age(&age, c, qs->path, refname,
1492 if (strncmp(refname, "refs/heads/", 11) == 0)
1494 error = gotweb_escape_html(&escaped_refname, refname);
1498 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1499 "<div class='branches_age'>%s</div>\n"
1500 "<div class='branches_space'> </div>\n"
1501 "<div class='branch'>"
1502 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1504 "</div>\n" /* .branch */
1505 "<div class='navs_wrapper'>\n"
1506 "<div class='navs'>"
1507 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1510 "<a href='?index_page=%s&path=%s&action=briefs&headref=%s'>"
1513 "<a href='?index_page=%s&path=%s&action=commits&headref=%s'>"
1515 "</div>\n" /* .navs */
1516 "</div>\n" /* .navs_wrapper */
1517 "<div class='dotted_line'></div>\n"
1518 "</div>\n", /* .branches_wrapper */
1520 index_page_str, qs->path, refname,
1522 index_page_str, qs->path, refname,
1523 index_page_str, qs->path, refname,
1524 index_page_str, qs->path, refname);
1525 free(escaped_refname);
1533 fcgi_printf(c, "</div>\n"); /* #branches_content */
1536 got_ref_list_free(&refs);
1540 static const struct got_error *
1541 gotweb_render_tree(struct request *c)
1543 const struct got_error *error = NULL;
1544 struct transport *t = c->t;
1545 struct repo_commit *rc = NULL;
1546 char *age = NULL, *msg = NULL;
1549 error = got_get_repo_commits(c, 1);
1553 rc = TAILQ_FIRST(&t->repo_commits);
1555 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1559 error = gotweb_escape_html(&msg, rc->commit_msg);
1563 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1564 "<div id='tree_title'>Tree</div>\n"
1565 "</div>\n" /* #tree_title_wrapper */
1566 "<div id='tree_content'>\n"
1567 "<div id='tree_header_wrapper'>\n"
1568 "<div id='tree_header'>\n"
1569 "<div id='header_tree_title'>Tree:</div>\n"
1570 "<div id='header_tree'>%s</div>\n"
1571 "<div class='header_age_title'>Date:</div>\n"
1572 "<div class='header_age'>%s</div>\n"
1573 "<div id='header_commit_msg_title'>Message:</div>\n"
1574 "<div id='header_commit_msg'>%s</div>\n"
1575 "</div>\n" /* #tree_header */
1576 "</div>\n" /* #tree_header_wrapper */
1577 "<div class='dotted_line'></div>\n"
1578 "<div id='tree'>\n",
1585 error = got_output_repo_tree(c);
1589 fcgi_printf(c, "</div>\n"); /* #tree */
1590 fcgi_printf(c, "</div>\n"); /* #tree_content */
1597 static const struct got_error *
1598 gotweb_render_diff(struct request *c)
1600 const struct got_error *error = NULL;
1601 struct transport *t = c->t;
1602 struct repo_commit *rc = NULL;
1603 char *age = NULL, *author = NULL, *msg = NULL;
1606 error = got_get_repo_commits(c, 1);
1610 rc = TAILQ_FIRST(&t->repo_commits);
1612 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1615 error = gotweb_escape_html(&author, rc->author);
1618 error = gotweb_escape_html(&msg, rc->commit_msg);
1622 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1623 "<div id='diff_title'>Commit Diff</div>\n"
1624 "</div>\n" /* #diff_title_wrapper */
1625 "<div id='diff_content'>\n"
1626 "<div id='diff_header_wrapper'>\n"
1627 "<div id='diff_header'>\n"
1628 "<div id='header_diff_title'>Diff:</div>\n"
1629 "<div id='header_diff'>%s<br />%s</div>\n"
1630 "<div class='header_commit_title'>Commit:</div>\n"
1631 "<div class='header_commit'>%s</div>\n"
1632 "<div id='header_tree_title'>Tree:</div>\n"
1633 "<div id='header_tree'>%s</div>\n"
1634 "<div class='header_author_title'>Author:</div>\n"
1635 "<div class='header_author'>%s</div>\n"
1636 "<div class='header_age_title'>Date:</div>\n"
1637 "<div class='header_age'>%s</div>\n"
1638 "<div id='header_commit_msg_title'>Message:</div>\n"
1639 "<div id='header_commit_msg'>%s</div>\n"
1640 "</div>\n" /* #diff_header */
1641 "</div>\n" /* #diff_header_wrapper */
1642 "<div class='dotted_line'></div>\n"
1643 "<div id='diff'>\n",
1644 rc->parent_id, rc->commit_id,
1653 error = got_output_repo_diff(c);
1657 fcgi_printf(c, "</div>\n"); /* #diff */
1658 fcgi_printf(c, "</div>\n"); /* #diff_content */
1666 static const struct got_error *
1667 gotweb_render_summary(struct request *c)
1669 const struct got_error *error = NULL;
1670 struct transport *t = c->t;
1671 struct server *srv = c->srv;
1674 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1677 if (srv->show_repo_description) {
1679 "<div id='description_title'>Description:</div>\n"
1680 "<div id='description'>%s</div>\n",
1681 t->repo_dir->description ? t->repo_dir->description : "");
1686 if (srv->show_repo_owner) {
1688 "<div id='repo_owner_title'>Owner:</div>\n"
1689 "<div id='repo_owner'>%s</div>\n",
1690 t->repo_dir->owner ? t->repo_dir->owner : "");
1695 if (srv->show_repo_age) {
1697 "<div id='last_change_title'>Last Change:</div>\n"
1698 "<div id='last_change'>%s</div>\n",
1704 if (srv->show_repo_cloneurl) {
1706 "<div id='cloneurl_title'>Clone URL:</div>\n"
1707 "<div id='cloneurl'>%s</div>\n",
1708 t->repo_dir->url ? t->repo_dir->url : "");
1713 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1717 error = gotweb_render_briefs(c);
1719 log_warnx("%s: %s", __func__, error->msg);
1723 error = gotweb_render_tags(c);
1725 log_warnx("%s: %s", __func__, error->msg);
1729 error = gotweb_render_branches(c);
1731 log_warnx("%s: %s", __func__, error->msg);
1736 static const struct got_error *
1737 gotweb_render_tag(struct request *c)
1739 const struct got_error *error = NULL;
1740 struct repo_tag *rt = NULL;
1741 struct transport *t = c->t;
1742 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1744 error = got_get_repo_tags(c, 1);
1748 if (t->tag_count == 0) {
1749 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1754 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1756 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1759 error = gotweb_escape_html(&author, rt->tagger);
1762 error = gotweb_escape_html(&msg, rt->commit_msg);
1766 tagname = rt->tag_name;
1767 if (strncmp(tagname, "refs/", 5) == 0)
1769 error = gotweb_escape_html(&tagname, tagname);
1773 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1774 "<div id='tags_title'>Tag</div>\n"
1775 "</div>\n" /* #tags_title_wrapper */
1776 "<div id='tags_content'>\n"
1777 "<div id='tag_header_wrapper'>\n"
1778 "<div id='tag_header'>\n"
1779 "<div class='header_commit_title'>Commit:</div>\n"
1780 "<div class='header_commit'>%s"
1781 " <span class='refs_str'>(%s)</span></div>\n"
1782 "<div class='header_author_title'>Tagger:</div>\n"
1783 "<div class='header_author'>%s</div>\n"
1784 "<div class='header_age_title'>Date:</div>\n"
1785 "<div class='header_age'>%s</div>\n"
1786 "<div id='header_commit_msg_title'>Message:</div>\n"
1787 "<div id='header_commit_msg'>%s</div>\n"
1788 "</div>\n" /* #tag_header */
1789 "<div class='dotted_line'></div>\n"
1790 "<div id='tag_commit'>\n%s</div>"
1791 "</div>", /* tag_header_wrapper */
1806 static const struct got_error *
1807 gotweb_render_tags(struct request *c)
1809 const struct got_error *error = NULL;
1810 struct repo_tag *rt = NULL;
1811 struct server *srv = c->srv;
1812 struct transport *t = c->t;
1813 struct querystring *qs = t->qs;
1814 struct repo_dir *repo_dir = t->repo_dir;
1815 const char *index_page_str;
1816 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1817 int r, commit_found = 0;
1819 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1821 if (qs->action == BRIEFS) {
1823 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1825 error = got_get_repo_tags(c, srv->max_commits_display);
1829 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1830 "<div id='tags_title'>Tags</div>\n"
1831 "</div>\n" /* #tags_title_wrapper */
1832 "<div id='tags_content'>\n");
1836 if (t->tag_count == 0) {
1837 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1838 "This repository contains no tags");
1843 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1844 if (commit_found == 0 && qs->commit != NULL) {
1845 if (strcmp(qs->commit, rt->commit_id) != 0)
1850 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1854 tagname = rt->tag_name;
1855 if (strncmp(tagname, "refs/tags/", 10) == 0)
1857 error = gotweb_escape_html(&tagname, tagname);
1861 if (rt->tag_commit != NULL) {
1862 newline = strchr(rt->tag_commit, '\n');
1865 error = gotweb_escape_html(&msg, rt->tag_commit);
1870 r = fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1871 "<div class='tag'>%s</div>\n"
1872 "<div class='tag_log'>"
1873 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1875 "</div>\n" /* .tag_log */
1876 "<div class='navs_wrapper'>\n"
1877 "<div class='navs'>"
1878 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1881 "<a href='?index_page=%s&path=%s&action=briefs&commit=%s'>"
1884 "<a href='?index_page=%s&path=%s&action=commits&commit=%s'>"
1886 "</div>\n" /* .navs */
1887 "</div>\n" /* .navs_wrapper */
1888 "<div class='dotted_line'></div>\n",
1891 index_page_str, repo_dir->name, rt->commit_id,
1893 index_page_str, repo_dir->name, rt->commit_id,
1894 index_page_str, repo_dir->name, rt->commit_id,
1895 index_page_str, repo_dir->name, rt->commit_id);
1906 if (t->next_id || t->prev_id) {
1907 error = gotweb_render_navs(c);
1911 fcgi_printf(c, "</div>\n"); /* #tags_content */
1919 const struct got_error *
1920 gotweb_escape_html(char **escaped_html, const char *orig_html)
1922 const struct got_error *error = NULL;
1923 struct escape_pair {
1934 size_t orig_len, len;
1937 orig_len = strlen(orig_html);
1939 for (i = 0; i < orig_len; i++) {
1940 for (j = 0; j < nitems(esc); j++) {
1941 if (orig_html[i] != esc[j].c)
1943 len += strlen(esc[j].s) - 1 /* escaped char */;
1947 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1948 if (*escaped_html == NULL)
1949 return got_error_from_errno("calloc");
1952 for (i = 0; i < orig_len; i++) {
1954 for (j = 0; j < nitems(esc); j++) {
1955 if (orig_html[i] != esc[j].c)
1958 if (strlcat(*escaped_html, esc[j].s, len + 1)
1960 error = got_error(GOT_ERR_NO_SPACE);
1963 x += strlen(esc[j].s);
1968 (*escaped_html)[x] = orig_html[i];
1974 free(*escaped_html);
1975 *escaped_html = NULL;
1977 (*escaped_html)[x] = '\0';
1983 static struct got_repository *
1984 find_cached_repo(struct server *srv, const char *path)
1988 for (i = 0; i < srv->ncached_repos; i++) {
1989 if (strcmp(srv->cached_repos[i].path, path) == 0)
1990 return srv->cached_repos[i].repo;
1996 static const struct got_error *
1997 cache_repo(struct got_repository **new, struct server *srv,
1998 struct repo_dir *repo_dir, struct socket *sock)
2000 const struct got_error *error = NULL;
2001 struct got_repository *repo;
2002 struct cached_repo *cr;
2005 if (srv->ncached_repos >= nitems(srv->cached_repos)) {
2006 cr = &srv->cached_repos[srv->ncached_repos - 1];
2007 error = got_repo_close(cr->repo);
2008 memset(cr, 0, sizeof(*cr));
2009 srv->ncached_repos--;
2012 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
2013 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2014 cr = &srv->cached_repos[0];
2017 cr = &srv->cached_repos[srv->ncached_repos];
2020 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
2023 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2024 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2029 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
2030 >= sizeof(cr->path)) {
2032 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2033 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2035 return got_error(GOT_ERR_NO_SPACE);
2039 srv->ncached_repos++;
2044 static const struct got_error *
2045 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
2047 const struct got_error *error = NULL;
2048 struct socket *sock = c->sock;
2049 struct server *srv = c->srv;
2050 struct transport *t = c->t;
2051 struct got_repository *repo = NULL;
2055 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
2056 GOTWEB_GIT_DIR) == -1)
2057 return got_error_from_errno("asprintf");
2059 dt = opendir(dir_test);
2063 repo_dir->path = dir_test;
2068 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
2069 repo_dir->name) == -1)
2070 return got_error_from_errno("asprintf");
2072 dt = opendir(dir_test);
2074 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2077 repo_dir->path = dir_test;
2082 repo = find_cached_repo(srv, repo_dir->path);
2084 error = cache_repo(&repo, srv, repo_dir, sock);
2089 error = gotweb_get_repo_description(&repo_dir->description, srv,
2093 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
2096 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
2100 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2103 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2104 error = got_error_from_errno("closedir");
2108 static const struct got_error *
2109 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2111 const struct got_error *error;
2113 *repo_dir = calloc(1, sizeof(**repo_dir));
2114 if (*repo_dir == NULL)
2115 return got_error_from_errno("calloc");
2117 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2118 error = got_error_from_errno("asprintf");
2123 (*repo_dir)->owner = NULL;
2124 (*repo_dir)->description = NULL;
2125 (*repo_dir)->url = NULL;
2126 (*repo_dir)->age = NULL;
2127 (*repo_dir)->path = NULL;
2132 static const struct got_error *
2133 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2135 const struct got_error *error = NULL;
2137 char *d_file = NULL;
2141 *description = NULL;
2142 if (srv->show_repo_description == 0)
2145 if (asprintf(&d_file, "%s/description", dir) == -1)
2146 return got_error_from_errno("asprintf");
2148 f = fopen(d_file, "r");
2150 if (errno != ENOENT && errno != EACCES)
2151 error = got_error_from_errno2("fopen", d_file);
2155 if (fseek(f, 0, SEEK_END) == -1) {
2156 error = got_ferror(f, GOT_ERR_IO);
2161 error = got_ferror(f, GOT_ERR_IO);
2166 *description = strdup("");
2167 if (*description == NULL)
2168 error = got_error_from_errno("strdup");
2172 if (fseek(f, 0, SEEK_SET) == -1) {
2173 error = got_ferror(f, GOT_ERR_IO);
2176 *description = calloc(len + 1, sizeof(**description));
2177 if (*description == NULL) {
2178 error = got_error_from_errno("calloc");
2182 n = fread(*description, 1, len, f);
2183 if (n == 0 && ferror(f))
2184 error = got_ferror(f, GOT_ERR_IO);
2186 if (f != NULL && fclose(f) == EOF && error == NULL)
2187 error = got_error_from_errno("fclose");
2192 static const struct got_error *
2193 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2195 const struct got_error *error = NULL;
2197 char *d_file = NULL;
2203 if (srv->show_repo_cloneurl == 0)
2206 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2207 return got_error_from_errno("asprintf");
2209 f = fopen(d_file, "r");
2211 if (errno != ENOENT && errno != EACCES)
2212 error = got_error_from_errno2("fopen", d_file);
2216 if (fseek(f, 0, SEEK_END) == -1) {
2217 error = got_ferror(f, GOT_ERR_IO);
2222 error = got_ferror(f, GOT_ERR_IO);
2228 if (fseek(f, 0, SEEK_SET) == -1) {
2229 error = got_ferror(f, GOT_ERR_IO);
2233 *url = calloc(len + 1, sizeof(**url));
2235 error = got_error_from_errno("calloc");
2239 n = fread(*url, 1, len, f);
2240 if (n == 0 && ferror(f))
2241 error = got_ferror(f, GOT_ERR_IO);
2243 if (f != NULL && fclose(f) == EOF && error == NULL)
2244 error = got_error_from_errno("fclose");
2249 const struct got_error *
2250 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2253 long long diff_time;
2254 const char *years = "years ago", *months = "months ago";
2255 const char *weeks = "weeks ago", *days = "days ago";
2256 const char *hours = "hours ago", *minutes = "minutes ago";
2257 const char *seconds = "seconds ago", *now = "right now";
2265 diff_time = time(NULL) - committer_time;
2266 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2267 if (asprintf(repo_age, "%lld %s",
2268 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2269 return got_error_from_errno("asprintf");
2270 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2271 if (asprintf(repo_age, "%lld %s",
2272 (diff_time / 60 / 60 / 24 / (365 / 12)),
2274 return got_error_from_errno("asprintf");
2275 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2276 if (asprintf(repo_age, "%lld %s",
2277 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2278 return got_error_from_errno("asprintf");
2279 } else if (diff_time > 60 * 60 * 24 * 2) {
2280 if (asprintf(repo_age, "%lld %s",
2281 (diff_time / 60 / 60 / 24), days) == -1)
2282 return got_error_from_errno("asprintf");
2283 } else if (diff_time > 60 * 60 * 2) {
2284 if (asprintf(repo_age, "%lld %s",
2285 (diff_time / 60 / 60), hours) == -1)
2286 return got_error_from_errno("asprintf");
2287 } else if (diff_time > 60 * 2) {
2288 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2290 return got_error_from_errno("asprintf");
2291 } else if (diff_time > 2) {
2292 if (asprintf(repo_age, "%lld %s", diff_time,
2294 return got_error_from_errno("asprintf");
2296 if (asprintf(repo_age, "%s", now) == -1)
2297 return got_error_from_errno("asprintf");
2301 if (gmtime_r(&committer_time, &tm) == NULL)
2302 return got_error_from_errno("gmtime_r");
2304 s = asctime_r(&tm, datebuf);
2306 return got_error_from_errno("asctime_r");
2308 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2309 return got_error_from_errno("asprintf");