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 (c->t->repo != NULL && qs && qs->action != INDEX)
296 got_repo_close(c->t->repo);
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)->action = INDEX;
359 (*qs)->commit = NULL;
361 (*qs)->folder = NULL;
362 (*qs)->headref = strdup("HEAD");
363 if ((*qs)->headref == NULL) {
364 return got_error_from_errno2("%s: strdup", __func__);
366 (*qs)->index_page = 0;
367 (*qs)->index_page_str = NULL;
373 static const struct got_error *
374 gotweb_parse_querystring(struct querystring **qs, char *qst)
376 const struct got_error *error = NULL;
377 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
378 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
385 return got_error_from_errno2("%s: strdup", __func__);
390 while (tok1_pair != NULL) {
391 strsep(&tok1_end, "&");
393 tok2 = strdup(tok1_pair);
396 return got_error_from_errno2("%s: strdup", __func__);
402 while (tok2_pair != NULL) {
403 strsep(&tok2_end, "=");
405 error = gotweb_assign_querystring(qs, tok2_pair,
410 tok2_pair = tok2_end;
413 tok1_pair = tok1_end;
423 static const struct got_error *
424 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
426 const struct got_error *error = NULL;
430 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
431 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
434 switch (querystring_keys[el_cnt].element) {
436 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
437 if (strcmp(value, action_keys[a_cnt].name) != 0)
439 else if (strcmp(value,
440 action_keys[a_cnt].name) == 0){
442 action_keys[a_cnt].action;
450 (*qs)->commit = strdup(value);
451 if ((*qs)->commit == NULL) {
452 error = got_error_from_errno2("%s: strdup",
458 (*qs)->file = strdup(value);
459 if ((*qs)->file == NULL) {
460 error = got_error_from_errno2("%s: strdup",
466 (*qs)->folder = strdup(value);
467 if ((*qs)->folder == NULL) {
468 error = got_error_from_errno2("%s: strdup",
474 (*qs)->headref = strdup(value);
475 if ((*qs)->headref == NULL) {
476 error = got_error_from_errno2("%s: strdup",
482 if (strlen(value) == 0)
484 (*qs)->index_page_str = strdup(value);
485 if ((*qs)->index_page_str == NULL) {
486 error = got_error_from_errno2("%s: strdup",
490 (*qs)->index_page = strtonum(value, INT64_MIN,
493 error = got_error_from_errno3("%s: strtonum %s",
497 if ((*qs)->index_page < 0) {
498 (*qs)->index_page = 0;
499 sprintf((*qs)->index_page_str, "%d", 0);
503 (*qs)->path = strdup(value);
504 if ((*qs)->path == NULL) {
505 error = got_error_from_errno2("%s: strdup",
511 if (strlen(value) == 0)
513 (*qs)->page_str = strdup(value);
514 if ((*qs)->page_str == NULL) {
515 error = got_error_from_errno2("%s: strdup",
519 (*qs)->page = strtonum(value, INT64_MIN,
522 error = got_error_from_errno3("%s: strtonum %s",
526 if ((*qs)->page < 0) {
528 sprintf((*qs)->page_str, "%d", 0);
540 gotweb_free_repo_tag(struct repo_tag *rt)
543 free(rt->commit_msg);
551 gotweb_free_repo_commit(struct repo_commit *rc)
561 free(rc->commit_msg);
567 gotweb_free_querystring(struct querystring *qs)
574 free(qs->index_page_str);
582 gotweb_free_repo_dir(struct repo_dir *repo_dir)
584 if (repo_dir != NULL) {
585 free(repo_dir->name);
586 free(repo_dir->owner);
587 free(repo_dir->description);
590 free(repo_dir->path);
596 gotweb_free_transport(struct transport *t)
598 struct repo_commit *rc = NULL, *trc = NULL;
599 struct repo_tag *rt = NULL, *trt = NULL;
601 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
602 TAILQ_REMOVE(&t->repo_commits, rc, entry);
603 gotweb_free_repo_commit(rc);
605 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
606 TAILQ_REMOVE(&t->repo_tags, rt, entry);
607 gotweb_free_repo_tag(rt);
609 gotweb_free_repo_dir(t->repo_dir);
610 gotweb_free_querystring(t->qs);
618 const struct got_error *
619 gotweb_render_content_type(struct request *c, const uint8_t *type)
621 const char *csp = "default-src 'self'; script-src 'none'; "
622 "object-src 'none';";
625 "Content-Security-Policy: %s\r\n"
626 "Content-Type: %s\r\n\r\n",
631 const struct got_error *
632 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
635 fcgi_printf(c, "Content-type: %s\r\n"
636 "Content-disposition: attachment; filename=%s\r\n\r\n",
641 static const struct got_error *
642 gotweb_render_header(struct request *c)
644 struct server *srv = c->srv;
645 struct querystring *qs = c->t->qs;
648 r = fcgi_printf(c, "<!doctype html>\n"
651 "<title>%s</title>\n"
652 "<meta charset='utf-8' />\n"
653 "<meta name='viewport' content='initial-scale=.75' />\n"
654 "<meta name='msapplication-TileColor' content='#da532c' />\n"
655 "<meta name='theme-color' content='#ffffff'/>\n"
656 "<link rel='apple-touch-icon' sizes='180x180'"
657 " href='/apple-touch-icon.png' />\n"
658 "<link rel='icon' type='image/png' sizes='32x32'"
659 " href='/favicon-32x32.png' />\n"
660 "<link rel='icon' type='image/png' sizes='16x16'"
661 " href='/favicon-16x16.png' />\n"
662 "<link rel='manifest' href='/site.webmanifest'/>\n"
663 "<link rel='mask-icon' href='/safari-pinned-tab.svg' />\n"
664 "<link rel='stylesheet' type='text/css' href='%s%s' />\n"
667 "<div id='gw_body'>\n"
668 "<div id='header'>\n"
669 "<div id='got_link'>"
670 "<a href='%s' target='_blank'>"
671 "<img src='%s%s' alt='logo' id='logo' />"
673 "</div>\n" /* #got_link */
674 "</div>\n" /* #header */
675 "<div id='site_path'>\n"
676 "<div id='site_link'>\n"
677 "<a href='?index_page=%d'>%s</a>",
679 c->script_name, srv->custom_css,
681 c->script_name, srv->logo,
682 qs->index_page, srv->site_link);
687 if (qs->path != NULL) {
688 r = fcgi_printf(c, " / "
689 "<a href='?index_page=%d&path=%s&action=summary'>"
691 qs->index_page, qs->path, qs->path);
695 if (qs->action != INDEX) {
696 const char *action = "";
698 switch (qs->action) {
725 if (fcgi_printf(c, " / %s", action) == -1)
730 fcgi_printf(c, "</div>\n" /* #site_path */
731 "</div>\n" /* #site_link */
732 "<div id='content'>\n");
738 static const struct got_error *
739 gotweb_render_footer(struct request *c)
741 const struct got_error *error = NULL;
742 struct server *srv = c->srv;
743 const char *siteowner = " ";
744 char *escaped_owner = NULL;
746 if (srv->show_site_owner) {
747 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
750 siteowner = escaped_owner;
753 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
754 "<div id='site_owner'>%s</div>\n"
755 "</div>\n" /* #site_owner_wrapper */
756 "</div>\n" /* #content */
757 "</div>\n" /* #gw_body */
758 "</body>\n</html>\n", siteowner);
764 static const struct got_error *
765 gotweb_render_navs(struct request *c)
767 const struct got_error *error = NULL;
768 struct transport *t = c->t;
769 struct querystring *qs = t->qs;
770 struct server *srv = c->srv;
771 char *nhref = NULL, *phref = NULL;
774 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
780 if (qs->index_page > 0) {
781 if (asprintf(&phref, "index_page=%d",
782 qs->index_page - 1) == -1) {
783 error = got_error_from_errno2("%s: asprintf",
791 if (t->prev_id && qs->commit != NULL &&
792 strcmp(qs->commit, t->prev_id) != 0) {
793 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
794 "&action=briefs&commit=%s&headref=%s",
795 qs->index_page, qs->path, qs->page - 1, t->prev_id,
796 qs->headref) == -1) {
797 error = got_error_from_errno2("%s: asprintf",
805 if (t->prev_id && qs->commit != NULL &&
806 strcmp(qs->commit, t->prev_id) != 0) {
807 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
808 "&action=commits&commit=%s&headref=%s&folder=%s"
810 qs->index_page, qs->path, qs->page - 1, t->prev_id,
811 qs->headref, qs->folder ? qs->folder : "",
812 qs->file ? qs->file : "") == -1) {
813 error = got_error_from_errno2("%s: asprintf",
821 if (t->prev_id && qs->commit != NULL &&
822 strcmp(qs->commit, t->prev_id) != 0) {
823 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
824 "&action=tags&commit=%s&headref=%s",
825 qs->index_page, qs->path, qs->page - 1, t->prev_id,
826 qs->headref) == -1) {
827 error = got_error_from_errno2("%s: asprintf",
840 r = fcgi_printf(c, "<a href='?%s'>Previous</a>", phref);
845 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
846 "<div id='nav_next'>");
853 if (t->next_disp == srv->max_repos_display &&
854 t->repos_total != (qs->index_page + 1) *
855 srv->max_repos_display) {
856 if (asprintf(&nhref, "index_page=%d",
857 qs->index_page + 1) == -1) {
858 error = got_error_from_errno2("%s: asprintf",
867 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
868 "&action=briefs&commit=%s&headref=%s",
869 qs->index_page, qs->path, qs->page + 1, t->next_id,
870 qs->headref) == -1) {
871 error = got_error_from_errno2("%s: asprintf",
880 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
881 "&action=commits&commit=%s&headref=%s&folder=%s"
883 qs->index_page, qs->path, qs->page + 1, t->next_id,
884 qs->headref, qs->folder ? qs->folder : "",
885 qs->file ? qs->file : "") == -1) {
886 error = got_error_from_errno2("%s: asprintf",
895 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
896 "&action=tags&commit=%s&headref=%s",
897 qs->index_page, qs->path, qs->page + 1, t->next_id,
898 qs->headref) == -1) {
899 error = got_error_from_errno2("%s: asprintf",
911 r = fcgi_printf(c, "<a href='?%s'>Next</a>", nhref);
915 fcgi_printf(c, "</div>\n"); /* #nav_next */
916 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
927 static const struct got_error *
928 gotweb_render_index(struct request *c)
930 const struct got_error *error = NULL;
931 struct server *srv = c->srv;
932 struct transport *t = c->t;
933 struct querystring *qs = t->qs;
934 struct repo_dir *repo_dir = NULL;
936 struct dirent **sd_dent;
937 const char *index_page_str;
940 unsigned int d_cnt, d_i, d_disp = 0;
943 index_page_str = qs->index_page_str ? qs->index_page_str : "";
945 d = opendir(srv->repos_path);
947 error = got_error_from_errno2("opendir", srv->repos_path);
951 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
953 error = got_error_from_errno2("scandir", srv->repos_path);
957 /* get total count of repos */
958 for (d_i = 0; d_i < d_cnt; d_i++) {
959 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
960 strcmp(sd_dent[d_i]->d_name, "..") == 0)
963 if (asprintf(&c_path, "%s/%s", srv->repos_path,
964 sd_dent[d_i]->d_name) == -1) {
965 error = got_error_from_errno("asprintf");
969 if (lstat(c_path, &st) == 0 && S_ISDIR(st.st_mode) &&
970 !got_path_dir_is_empty(c_path))
976 r = fcgi_printf(c, "<div id='index_header'>\n"
977 "<div id='index_header_project'>Project</div>\n");
981 if (srv->show_repo_description)
982 if (fcgi_printf(c, "<div id='index_header_description'>"
983 "Description</div>\n") == -1)
985 if (srv->show_repo_owner)
986 if (fcgi_printf(c, "<div id='index_header_owner'>"
987 "Owner</div>\n") == -1)
989 if (srv->show_repo_age)
990 if (fcgi_printf(c, "<div id='index_header_age'>"
991 "Last Change</div>\n") == -1)
993 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
996 for (d_i = 0; d_i < d_cnt; d_i++) {
997 if (srv->max_repos > 0 && (d_i - 2) == srv->max_repos)
998 break; /* account for parent and self */
1000 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1001 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1004 if (qs->index_page > 0 && (qs->index_page *
1005 srv->max_repos_display) > t->prev_disp) {
1010 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1014 error = gotweb_load_got_path(c, repo_dir);
1015 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1019 else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1022 if (lstat(repo_dir->path, &st) == 0 &&
1023 S_ISDIR(st.st_mode) &&
1024 !got_path_dir_is_empty(repo_dir->path))
1027 gotweb_free_repo_dir(repo_dir);
1035 r = fcgi_printf(c, "<div class='index_wrapper'>\n"
1036 "<div class='index_project'>"
1037 "<a href='?index_page=%s&path=%s&action=summary'>"
1040 "</div>", /* .index_project */
1041 index_page_str, repo_dir->name,
1046 if (srv->show_repo_description) {
1048 "<div class='index_project_description'>\n"
1049 "%s</div>\n", repo_dir->description);
1054 if (srv->show_repo_owner) {
1055 r = fcgi_printf(c, "<div class='index_project_owner'>"
1056 "%s</div>\n", repo_dir->owner);
1061 if (srv->show_repo_age) {
1062 r = fcgi_printf(c, "<div class='index_project_age'>"
1063 "%s</div>\n", repo_dir->age);
1068 r = fcgi_printf(c, "<div class='navs_wrapper'>"
1069 "<div class='navs'>"
1070 "<a href='?index_page=%s&path=%s&action=summary'>"
1073 "<a href='?index_page=%s&path=%s&action=briefs'>"
1076 "<a href='?index_page=%s&path=%s&action=commits'>"
1079 "<a href='?index_page=%s&path=%s&action=tags'>"
1082 "<a href='?index_page=%s&path=%s&action=tree'>"
1085 "</div>" /* .navs */
1086 "<div class='dotted_line'></div>\n"
1087 "</div>\n" /* .navs_wrapper */
1088 "</div>\n", /* .index_wrapper */
1089 index_page_str, repo_dir->name,
1090 index_page_str, repo_dir->name,
1091 index_page_str, repo_dir->name,
1092 index_page_str, repo_dir->name,
1093 index_page_str, repo_dir->name);
1097 gotweb_free_repo_dir(repo_dir);
1099 error = got_repo_close(t->repo);
1103 if (d_disp == srv->max_repos_display)
1106 if (srv->max_repos_display == 0)
1108 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1110 if (t->repos_total <= srv->max_repos ||
1111 t->repos_total <= srv->max_repos_display)
1114 error = gotweb_render_navs(c);
1118 if (d != NULL && closedir(d) == EOF && error == NULL)
1119 error = got_error_from_errno("closedir");
1123 static const struct got_error *
1124 gotweb_render_blame(struct request *c)
1126 const struct got_error *error = NULL;
1127 struct transport *t = c->t;
1128 struct repo_commit *rc = NULL;
1129 char *age = NULL, *msg = NULL;
1132 error = got_get_repo_commits(c, 1);
1136 rc = TAILQ_FIRST(&t->repo_commits);
1138 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1141 error = gotweb_escape_html(&msg, rc->commit_msg);
1145 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1146 "<div id='blame_title'>Blame</div>\n"
1147 "</div>\n" /* #blame_title_wrapper */
1148 "<div id='blame_content'>\n"
1149 "<div id='blame_header_wrapper'>\n"
1150 "<div id='blame_header'>\n"
1151 "<div class='header_age_title'>Date:</div>\n"
1152 "<div class='header_age'>%s</div>\n"
1153 "<div id='header_commit_msg_title'>Message:</div>\n"
1154 "<div id='header_commit_msg'>%s</div>\n"
1155 "</div>\n" /* #blame_header */
1156 "</div>\n" /* #blame_header_wrapper */
1157 "<div class='dotted_line'></div>\n"
1158 "<div id='blame'>\n",
1164 error = got_output_file_blame(c);
1168 fcgi_printf(c, "</div>\n" /* #blame */
1169 "</div>\n"); /* #blame_content */
1176 static const struct got_error *
1177 gotweb_render_briefs(struct request *c)
1179 const struct got_error *error = NULL;
1180 struct repo_commit *rc = NULL;
1181 struct server *srv = c->srv;
1182 struct transport *t = c->t;
1183 struct querystring *qs = t->qs;
1184 struct repo_dir *repo_dir = t->repo_dir;
1185 const char *index_page_str;
1186 char *smallerthan, *newline;
1187 char *age = NULL, *author = NULL, *msg = NULL;
1190 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1192 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1193 "<div id='briefs_title'>Commit Briefs</div>\n"
1194 "</div>\n" /* #briefs_title_wrapper */
1195 "<div id='briefs_content'>\n");
1199 if (qs->action == SUMMARY) {
1200 qs->action = BRIEFS;
1201 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1203 error = got_get_repo_commits(c, srv->max_commits_display);
1207 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1208 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1212 smallerthan = strchr(rc->author, '<');
1214 *smallerthan = '\0';
1216 newline = strchr(rc->commit_msg, '\n');
1220 error = gotweb_escape_html(&author, rc->author);
1223 error = gotweb_escape_html(&msg, rc->commit_msg);
1227 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1228 "<div class='briefs_author'>%s</div>\n"
1229 "<div class='briefs_log'>"
1230 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1231 "&headref=%s'>%s</a>",
1234 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1242 error = gotweb_escape_html(&refs, rc->refs_str);
1246 " <span class='refs_str'>(%s)</span>", refs);
1251 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1254 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1255 "<div class='navs'>"
1256 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1257 "&headref=%s'>diff</a>"
1259 "<a href='?index_page=%s&path=%s&action=tree&commit=%s"
1260 "&headref=%s'>tree</a>"
1261 "</div>\n" /* .navs */
1262 "</div>\n" /* .navs_wrapper */
1263 "<div class='dotted_line'></div>\n",
1264 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1265 index_page_str, repo_dir->name, rc->commit_id, qs->headref);
1277 if (t->next_id || t->prev_id) {
1278 error = gotweb_render_navs(c);
1282 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1290 static const struct got_error *
1291 gotweb_render_commits(struct request *c)
1293 const struct got_error *error = NULL;
1294 struct repo_commit *rc = NULL;
1295 struct server *srv = c->srv;
1296 struct transport *t = c->t;
1297 struct querystring *qs = t->qs;
1298 struct repo_dir *repo_dir = t->repo_dir;
1299 const char *index_page_str;
1300 char *age = NULL, *author = NULL, *msg = NULL;
1303 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1305 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1306 "<div class='commits_title'>Commits</div>\n"
1307 "</div>\n" /* .commits_title_wrapper */
1308 "<div class='commits_content'>\n");
1312 error = got_get_repo_commits(c, srv->max_commits_display);
1316 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1317 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1320 error = gotweb_escape_html(&author, rc->author);
1323 error = gotweb_escape_html(&msg, rc->commit_msg);
1327 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1328 "<div class='commits_header'>\n"
1329 "<div class='header_commit_title'>Commit:</div>\n"
1330 "<div class='header_commit'>%s</div>\n"
1331 "<div class='header_author_title'>Author:</div>\n"
1332 "<div class='header_author'>%s</div>\n"
1333 "<div class='header_age_title'>Date:</div>\n"
1334 "<div class='header_age'>%s</div>\n"
1335 "</div>\n" /* .commits_header */
1336 "</div>\n" /* .commits_header_wrapper */
1337 "<div class='dotted_line'></div>\n"
1338 "<div class='commit'>\n%s</div>\n",
1346 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1347 "<div class='navs'>"
1348 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1351 "<a href='?index_page=%s&path=%s&action=tree&commit=%s'>"
1353 "</div>\n" /* .navs */
1354 "</div>\n" /* .navs_wrapper */
1355 "<div class='dotted_line'></div>\n",
1356 index_page_str, repo_dir->name, rc->commit_id,
1357 index_page_str, repo_dir->name, rc->commit_id);
1367 if (t->next_id || t->prev_id) {
1368 error = gotweb_render_navs(c);
1372 fcgi_printf(c, "</div>\n"); /* .commits_content */
1380 static const struct got_error *
1381 gotweb_render_branches(struct request *c)
1383 const struct got_error *error = NULL;
1384 struct got_reflist_head refs;
1385 struct got_reflist_entry *re;
1386 struct transport *t = c->t;
1387 struct querystring *qs = t->qs;
1388 struct got_repository *repo = t->repo;
1389 const char *index_page_str;
1393 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1397 error = got_ref_list(&refs, repo, "refs/heads",
1398 got_ref_cmp_by_name, NULL);
1402 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1403 "<div id='branches_title'>Branches</div>\n"
1404 "</div>\n" /* #branches_title_wrapper */
1405 "<div id='branches_content'>\n");
1409 TAILQ_FOREACH(re, &refs, entry) {
1410 const char *refname = NULL;
1411 char *escaped_refname = NULL;
1413 if (got_ref_is_symbolic(re->ref))
1416 refname = got_ref_get_name(re->ref);
1417 if (refname == NULL) {
1418 error = got_error_from_errno("strdup");
1421 if (strncmp(refname, "refs/heads/", 11) != 0)
1424 error = got_get_repo_age(&age, c, qs->path, refname,
1429 if (strncmp(refname, "refs/heads/", 11) == 0)
1431 error = gotweb_escape_html(&escaped_refname, refname);
1435 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1436 "<div class='branches_age'>%s</div>\n"
1437 "<div class='branches_space'> </div>\n"
1438 "<div class='branch'>"
1439 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1441 "</div>\n" /* .branch */
1442 "<div class='navs_wrapper'>\n"
1443 "<div class='navs'>"
1444 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1447 "<a href='?index_page=%s&path=%s&action=briefs&headref=%s'>"
1450 "<a href='?index_page=%s&path=%s&action=commits&headref=%s'>"
1452 "</div>\n" /* .navs */
1453 "</div>\n" /* .navs_wrapper */
1454 "<div class='dotted_line'></div>\n"
1455 "</div>\n", /* .branches_wrapper */
1457 index_page_str, qs->path, refname,
1459 index_page_str, qs->path, refname,
1460 index_page_str, qs->path, refname,
1461 index_page_str, qs->path, refname);
1462 free(escaped_refname);
1470 fcgi_printf(c, "</div>\n"); /* #branches_content */
1475 static const struct got_error *
1476 gotweb_render_tree(struct request *c)
1478 const struct got_error *error = NULL;
1479 struct transport *t = c->t;
1480 struct repo_commit *rc = NULL;
1481 char *age = NULL, *msg = NULL;
1484 error = got_get_repo_commits(c, 1);
1488 rc = TAILQ_FIRST(&t->repo_commits);
1490 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1494 error = gotweb_escape_html(&msg, rc->commit_msg);
1498 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1499 "<div id='tree_title'>Tree</div>\n"
1500 "</div>\n" /* #tree_title_wrapper */
1501 "<div id='tree_content'>\n"
1502 "<div id='tree_header_wrapper'>\n"
1503 "<div id='tree_header'>\n"
1504 "<div id='header_tree_title'>Tree:</div>\n"
1505 "<div id='header_tree'>%s</div>\n"
1506 "<div class='header_age_title'>Date:</div>\n"
1507 "<div class='header_age'>%s</div>\n"
1508 "<div id='header_commit_msg_title'>Message:</div>\n"
1509 "<div id='header_commit_msg'>%s</div>\n"
1510 "</div>\n" /* #tree_header */
1511 "</div>\n" /* #tree_header_wrapper */
1512 "<div class='dotted_line'></div>\n"
1513 "<div id='tree'>\n",
1520 error = got_output_repo_tree(c);
1524 fcgi_printf(c, "</div>\n"); /* #tree */
1525 fcgi_printf(c, "</div>\n"); /* #tree_content */
1532 static const struct got_error *
1533 gotweb_render_diff(struct request *c)
1535 const struct got_error *error = NULL;
1536 struct transport *t = c->t;
1537 struct repo_commit *rc = NULL;
1538 char *age = NULL, *author = NULL, *msg = NULL;
1541 error = got_get_repo_commits(c, 1);
1545 rc = TAILQ_FIRST(&t->repo_commits);
1547 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1550 error = gotweb_escape_html(&author, rc->author);
1553 error = gotweb_escape_html(&msg, rc->commit_msg);
1557 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1558 "<div id='diff_title'>Commit Diff</div>\n"
1559 "</div>\n" /* #diff_title_wrapper */
1560 "<div id='diff_content'>\n"
1561 "<div id='diff_header_wrapper'>\n"
1562 "<div id='diff_header'>\n"
1563 "<div id='header_diff_title'>Diff:</div>\n"
1564 "<div id='header_diff'>%s<br />%s</div>\n"
1565 "<div class='header_commit_title'>Commit:</div>\n"
1566 "<div class='header_commit'>%s</div>\n"
1567 "<div id='header_tree_title'>Tree:</div>\n"
1568 "<div id='header_tree'>%s</div>\n"
1569 "<div class='header_author_title'>Author:</div>\n"
1570 "<div class='header_author'>%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" /* #diff_header */
1576 "</div>\n" /* #diff_header_wrapper */
1577 "<div class='dotted_line'></div>\n"
1578 "<div id='diff'>\n",
1579 rc->parent_id, rc->commit_id,
1588 error = got_output_repo_diff(c);
1592 fcgi_printf(c, "</div>\n"); /* #diff */
1593 fcgi_printf(c, "</div>\n"); /* #diff_content */
1601 static const struct got_error *
1602 gotweb_render_summary(struct request *c)
1604 const struct got_error *error = NULL;
1605 struct transport *t = c->t;
1606 struct server *srv = c->srv;
1609 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1612 if (srv->show_repo_description) {
1614 "<div id='description_title'>Description:</div>\n"
1615 "<div id='description'>%s</div>\n",
1616 t->repo_dir->description ? t->repo_dir->description : "");
1621 if (srv->show_repo_owner) {
1623 "<div id='repo_owner_title'>Owner:</div>\n"
1624 "<div id='repo_owner'>%s</div>\n",
1625 t->repo_dir->owner ? t->repo_dir->owner : "");
1630 if (srv->show_repo_age) {
1632 "<div id='last_change_title'>Last Change:</div>\n"
1633 "<div id='last_change'>%s</div>\n",
1639 if (srv->show_repo_cloneurl) {
1641 "<div id='cloneurl_title'>Clone URL:</div>\n"
1642 "<div id='cloneurl'>%s</div>\n",
1643 t->repo_dir->url ? t->repo_dir->url : "");
1648 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1652 error = gotweb_render_briefs(c);
1654 log_warnx("%s: %s", __func__, error->msg);
1658 error = gotweb_render_tags(c);
1660 log_warnx("%s: %s", __func__, error->msg);
1664 error = gotweb_render_branches(c);
1666 log_warnx("%s: %s", __func__, error->msg);
1671 static const struct got_error *
1672 gotweb_render_tag(struct request *c)
1674 const struct got_error *error = NULL;
1675 struct repo_tag *rt = NULL;
1676 struct transport *t = c->t;
1677 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1679 error = got_get_repo_tags(c, 1);
1683 if (t->tag_count == 0) {
1684 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1689 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1691 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1694 error = gotweb_escape_html(&author, rt->tagger);
1697 error = gotweb_escape_html(&msg, rt->commit_msg);
1701 if (strncmp(rt->tag_name, "refs/", 5) == 0)
1703 error = gotweb_escape_html(&tagname, rt->tag_name);
1707 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1708 "<div id='tags_title'>Tag</div>\n"
1709 "</div>\n" /* #tags_title_wrapper */
1710 "<div id='tags_content'>\n"
1711 "<div id='tag_header_wrapper'>\n"
1712 "<div id='tag_header'>\n"
1713 "<div class='header_commit_title'>Commit:</div>\n"
1714 "<div class='header_commit'>%s"
1715 " <span class='refs_str'>(%s)</span></div>\n"
1716 "<div class='header_author_title'>Tagger:</div>\n"
1717 "<div class='header_author'>%s</div>\n"
1718 "<div class='header_age_title'>Date:</div>\n"
1719 "<div class='header_age'>%s</div>\n"
1720 "<div id='header_commit_msg_title'>Message:</div>\n"
1721 "<div id='header_commit_msg'>%s</div>\n"
1722 "</div>\n" /* #tag_header */
1723 "<div class='dotted_line'></div>\n"
1724 "<div id='tag_commit'>\n%s</div>"
1725 "</div>", /* tag_header_wrapper */
1740 static const struct got_error *
1741 gotweb_render_tags(struct request *c)
1743 const struct got_error *error = NULL;
1744 struct repo_tag *rt = NULL;
1745 struct server *srv = c->srv;
1746 struct transport *t = c->t;
1747 struct querystring *qs = t->qs;
1748 struct repo_dir *repo_dir = t->repo_dir;
1749 const char *index_page_str;
1750 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1751 int r, commit_found = 0;
1753 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1755 if (qs->action == BRIEFS) {
1757 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1759 error = got_get_repo_tags(c, srv->max_commits_display);
1763 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1764 "<div id='tags_title'>Tags</div>\n"
1765 "</div>\n" /* #tags_title_wrapper */
1766 "<div id='tags_content'>\n");
1770 if (t->tag_count == 0) {
1771 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1772 "This repository contains no tags");
1777 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1778 if (commit_found == 0 && qs->commit != NULL) {
1779 if (strcmp(qs->commit, rt->commit_id) != 0)
1784 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1788 if (strncmp(rt->tag_name, "refs/tags/", 10) == 0)
1790 error = gotweb_escape_html(&tagname, rt->tag_name);
1794 if (rt->tag_commit != NULL) {
1795 newline = strchr(rt->tag_commit, '\n');
1798 error = gotweb_escape_html(&msg, rt->tag_commit);
1803 r = fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1804 "<div class='tag'>%s</div>\n"
1805 "<div class='tag_log'>"
1806 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1808 "</div>\n" /* .tag_log */
1809 "<div class='navs_wrapper'>\n"
1810 "<div class='navs'>"
1811 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1814 "<a href='?index_page=%s&path=%s&action=briefs&commit=%s'>"
1817 "<a href='?index_page=%s&path=%s&action=commits&commit=%s'>"
1819 "</div>\n" /* .navs */
1820 "</div>\n" /* .navs_wrapper */
1821 "<div class='dotted_line'></div>\n",
1824 index_page_str, repo_dir->name, rt->commit_id,
1826 index_page_str, repo_dir->name, rt->commit_id,
1827 index_page_str, repo_dir->name, rt->commit_id,
1828 index_page_str, repo_dir->name, rt->commit_id);
1839 if (t->next_id || t->prev_id) {
1840 error = gotweb_render_navs(c);
1844 fcgi_printf(c, "</div>\n"); /* #tags_content */
1852 const struct got_error *
1853 gotweb_escape_html(char **escaped_html, const char *orig_html)
1855 const struct got_error *error = NULL;
1856 struct escape_pair {
1867 size_t orig_len, len;
1870 orig_len = strlen(orig_html);
1872 for (i = 0; i < orig_len; i++) {
1873 for (j = 0; j < nitems(esc); j++) {
1874 if (orig_html[i] != esc[j].c)
1876 len += strlen(esc[j].s) - 1 /* escaped char */;
1880 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1881 if (*escaped_html == NULL)
1882 return got_error_from_errno("calloc");
1885 for (i = 0; i < orig_len; i++) {
1887 for (j = 0; j < nitems(esc); j++) {
1888 if (orig_html[i] != esc[j].c)
1891 if (strlcat(*escaped_html, esc[j].s, len + 1)
1893 error = got_error(GOT_ERR_NO_SPACE);
1896 x += strlen(esc[j].s);
1901 (*escaped_html)[x] = orig_html[i];
1907 free(*escaped_html);
1908 *escaped_html = NULL;
1910 (*escaped_html)[x] = '\0';
1916 static const struct got_error *
1917 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1919 const struct got_error *error = NULL;
1920 struct socket *sock = c->sock;
1921 struct server *srv = c->srv;
1922 struct transport *t = c->t;
1927 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1928 GOTWEB_GIT_DIR) == -1)
1929 return got_error_from_errno("asprintf");
1931 dt = opendir(dir_test);
1935 repo_dir->path = strdup(dir_test);
1936 if (repo_dir->path == NULL) {
1938 error = got_error_from_errno("strdup");
1945 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1946 GOTWEB_GOT_DIR) == -1) {
1948 error = got_error_from_errno("asprintf");
1952 dt = opendir(dir_test);
1957 error = got_error(GOT_ERR_NOT_GIT_REPO);
1961 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1962 repo_dir->name) == -1) {
1963 error = got_error_from_errno("asprintf");
1968 repo_dir->path = strdup(dir_test);
1969 if (repo_dir->path == NULL) {
1971 error = got_error_from_errno("strdup");
1975 dt = opendir(dir_test);
1977 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1982 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1985 error = gotweb_get_repo_description(&repo_dir->description, srv,
1989 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
1992 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
1996 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2000 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2001 error = got_error_from_errno("closedir");
2005 static const struct got_error *
2006 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2008 const struct got_error *error;
2010 *repo_dir = calloc(1, sizeof(**repo_dir));
2011 if (*repo_dir == NULL)
2012 return got_error_from_errno("calloc");
2014 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2015 error = got_error_from_errno("asprintf");
2020 (*repo_dir)->owner = NULL;
2021 (*repo_dir)->description = NULL;
2022 (*repo_dir)->url = NULL;
2023 (*repo_dir)->age = NULL;
2024 (*repo_dir)->path = NULL;
2029 static const struct got_error *
2030 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2032 const struct got_error *error = NULL;
2034 char *d_file = NULL;
2038 *description = NULL;
2039 if (srv->show_repo_description == 0)
2042 if (asprintf(&d_file, "%s/description", dir) == -1)
2043 return got_error_from_errno("asprintf");
2045 f = fopen(d_file, "r");
2047 if (errno == ENOENT || errno == EACCES)
2049 error = got_error_from_errno2("fopen", d_file);
2053 if (fseek(f, 0, SEEK_END) == -1) {
2054 error = got_ferror(f, GOT_ERR_IO);
2059 error = got_ferror(f, GOT_ERR_IO);
2064 *description = strdup("");
2065 if (*description == NULL)
2066 return got_error_from_errno("strdup");
2070 if (fseek(f, 0, SEEK_SET) == -1) {
2071 error = got_ferror(f, GOT_ERR_IO);
2074 *description = calloc(len + 1, sizeof(**description));
2075 if (*description == NULL) {
2076 error = got_error_from_errno("calloc");
2080 n = fread(*description, 1, len, f);
2081 if (n == 0 && ferror(f))
2082 error = got_ferror(f, GOT_ERR_IO);
2084 if (f != NULL && fclose(f) == EOF && error == NULL)
2085 error = got_error_from_errno("fclose");
2090 static const struct got_error *
2091 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2093 const struct got_error *error = NULL;
2095 char *d_file = NULL;
2101 if (srv->show_repo_cloneurl == 0)
2104 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2105 return got_error_from_errno("asprintf");
2107 f = fopen(d_file, "r");
2109 if (errno != ENOENT && errno != EACCES)
2110 error = got_error_from_errno2("fopen", d_file);
2114 if (fseek(f, 0, SEEK_END) == -1) {
2115 error = got_ferror(f, GOT_ERR_IO);
2120 error = got_ferror(f, GOT_ERR_IO);
2126 if (fseek(f, 0, SEEK_SET) == -1) {
2127 error = got_ferror(f, GOT_ERR_IO);
2131 *url = calloc(len + 1, sizeof(**url));
2133 error = got_error_from_errno("calloc");
2137 n = fread(*url, 1, len, f);
2138 if (n == 0 && ferror(f))
2139 error = got_ferror(f, GOT_ERR_IO);
2141 if (f != NULL && fclose(f) == EOF && error == NULL)
2142 error = got_error_from_errno("fclose");
2147 const struct got_error *
2148 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2151 long long diff_time;
2152 const char *years = "years ago", *months = "months ago";
2153 const char *weeks = "weeks ago", *days = "days ago";
2154 const char *hours = "hours ago", *minutes = "minutes ago";
2155 const char *seconds = "seconds ago", *now = "right now";
2163 diff_time = time(NULL) - committer_time;
2164 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2165 if (asprintf(repo_age, "%lld %s",
2166 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2167 return got_error_from_errno("asprintf");
2168 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2169 if (asprintf(repo_age, "%lld %s",
2170 (diff_time / 60 / 60 / 24 / (365 / 12)),
2172 return got_error_from_errno("asprintf");
2173 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2174 if (asprintf(repo_age, "%lld %s",
2175 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2176 return got_error_from_errno("asprintf");
2177 } else if (diff_time > 60 * 60 * 24 * 2) {
2178 if (asprintf(repo_age, "%lld %s",
2179 (diff_time / 60 / 60 / 24), days) == -1)
2180 return got_error_from_errno("asprintf");
2181 } else if (diff_time > 60 * 60 * 2) {
2182 if (asprintf(repo_age, "%lld %s",
2183 (diff_time / 60 / 60), hours) == -1)
2184 return got_error_from_errno("asprintf");
2185 } else if (diff_time > 60 * 2) {
2186 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2188 return got_error_from_errno("asprintf");
2189 } else if (diff_time > 2) {
2190 if (asprintf(repo_age, "%lld %s", diff_time,
2192 return got_error_from_errno("asprintf");
2194 if (asprintf(repo_age, "%s", now) == -1)
2195 return got_error_from_errno("asprintf");
2199 if (gmtime_r(&committer_time, &tm) == NULL)
2200 return got_error_from_errno("gmtime_r");
2202 s = asctime_r(&tm, datebuf);
2204 return got_error_from_errno("asctime_r");
2206 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2207 return got_error_from_errno("asprintf");