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 */
1175 static const struct got_error *
1176 gotweb_render_briefs(struct request *c)
1178 const struct got_error *error = NULL;
1179 struct repo_commit *rc = NULL;
1180 struct server *srv = c->srv;
1181 struct transport *t = c->t;
1182 struct querystring *qs = t->qs;
1183 struct repo_dir *repo_dir = t->repo_dir;
1184 const char *index_page_str;
1185 char *smallerthan, *newline;
1186 char *age = NULL, *author = NULL, *msg = NULL;
1189 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1191 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1192 "<div id='briefs_title'>Commit Briefs</div>\n"
1193 "</div>\n" /* #briefs_title_wrapper */
1194 "<div id='briefs_content'>\n");
1198 if (qs->action == SUMMARY) {
1199 qs->action = BRIEFS;
1200 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1202 error = got_get_repo_commits(c, srv->max_commits_display);
1206 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1207 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1211 smallerthan = strchr(rc->author, '<');
1213 *smallerthan = '\0';
1215 newline = strchr(rc->commit_msg, '\n');
1219 error = gotweb_escape_html(&author, rc->author);
1222 error = gotweb_escape_html(&msg, rc->commit_msg);
1226 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1227 "<div class='briefs_author'>%s</div>\n"
1228 "<div class='briefs_log'>"
1229 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1230 "&headref=%s'>%s</a>",
1233 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1241 error = gotweb_escape_html(&refs, rc->refs_str);
1245 " <span class='refs_str'>(%s)</span>", refs);
1250 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1253 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1254 "<div class='navs'>"
1255 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1256 "&headref=%s'>diff</a>"
1258 "<a href='?index_page=%s&path=%s&action=tree&commit=%s"
1259 "&headref=%s'>tree</a>"
1260 "</div>\n" /* .navs */
1261 "</div>\n" /* .navs_wrapper */
1262 "<div class='dotted_line'></div>\n",
1263 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1264 index_page_str, repo_dir->name, rc->commit_id, qs->headref);
1276 if (t->next_id || t->prev_id) {
1277 error = gotweb_render_navs(c);
1281 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1289 static const struct got_error *
1290 gotweb_render_commits(struct request *c)
1292 const struct got_error *error = NULL;
1293 struct repo_commit *rc = NULL;
1294 struct server *srv = c->srv;
1295 struct transport *t = c->t;
1296 struct querystring *qs = t->qs;
1297 struct repo_dir *repo_dir = t->repo_dir;
1298 const char *index_page_str;
1299 char *age = NULL, *author = NULL, *msg = NULL;
1302 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1304 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1305 "<div class='commits_title'>Commits</div>\n"
1306 "</div>\n" /* .commits_title_wrapper */
1307 "<div class='commits_content'>\n");
1311 error = got_get_repo_commits(c, srv->max_commits_display);
1315 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1316 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1319 error = gotweb_escape_html(&author, rc->author);
1322 error = gotweb_escape_html(&msg, rc->commit_msg);
1326 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1327 "<div class='commits_header'>\n"
1328 "<div class='header_commit_title'>Commit:</div>\n"
1329 "<div class='header_commit'>%s</div>\n"
1330 "<div class='header_author_title'>Author:</div>\n"
1331 "<div class='header_author'>%s</div>\n"
1332 "<div class='header_age_title'>Date:</div>\n"
1333 "<div class='header_age'>%s</div>\n"
1334 "</div>\n" /* .commits_header */
1335 "</div>\n" /* .commits_header_wrapper */
1336 "<div class='dotted_line'></div>\n"
1337 "<div class='commit'>\n%s</div>\n",
1345 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1346 "<div class='navs'>"
1347 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1350 "<a href='?index_page=%s&path=%s&action=tree&commit=%s'>"
1352 "</div>\n" /* .navs */
1353 "</div>\n" /* .navs_wrapper */
1354 "<div class='dotted_line'></div>\n",
1355 index_page_str, repo_dir->name, rc->commit_id,
1356 index_page_str, repo_dir->name, rc->commit_id);
1366 if (t->next_id || t->prev_id) {
1367 error = gotweb_render_navs(c);
1371 fcgi_printf(c, "</div>\n"); /* .commits_content */
1379 static const struct got_error *
1380 gotweb_render_branches(struct request *c)
1382 const struct got_error *error = NULL;
1383 struct got_reflist_head refs;
1384 struct got_reflist_entry *re;
1385 struct transport *t = c->t;
1386 struct querystring *qs = t->qs;
1387 struct got_repository *repo = t->repo;
1388 const char *index_page_str;
1392 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1396 error = got_ref_list(&refs, repo, "refs/heads",
1397 got_ref_cmp_by_name, NULL);
1401 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1402 "<div id='branches_title'>Branches</div>\n"
1403 "</div>\n" /* #branches_title_wrapper */
1404 "<div id='branches_content'>\n");
1408 TAILQ_FOREACH(re, &refs, entry) {
1409 const char *refname = NULL;
1410 char *escaped_refname = NULL;
1412 if (got_ref_is_symbolic(re->ref))
1415 refname = got_ref_get_name(re->ref);
1416 if (refname == NULL) {
1417 error = got_error_from_errno("strdup");
1420 if (strncmp(refname, "refs/heads/", 11) != 0)
1423 error = got_get_repo_age(&age, c, qs->path, refname,
1428 if (strncmp(refname, "refs/heads/", 11) == 0)
1430 error = gotweb_escape_html(&escaped_refname, refname);
1434 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1435 "<div class='branches_age'>%s</div>\n"
1436 "<div class='branches_space'> </div>\n"
1437 "<div class='branch'>"
1438 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1440 "</div>\n" /* .branch */
1441 "<div class='navs_wrapper'>\n"
1442 "<div class='navs'>"
1443 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1446 "<a href='?index_page=%s&path=%s&action=briefs&headref=%s'>"
1449 "<a href='?index_page=%s&path=%s&action=commits&headref=%s'>"
1451 "</div>\n" /* .navs */
1452 "</div>\n" /* .navs_wrapper */
1453 "<div class='dotted_line'></div>\n"
1454 "</div>\n", /* .branches_wrapper */
1456 index_page_str, qs->path, refname,
1458 index_page_str, qs->path, refname,
1459 index_page_str, qs->path, refname,
1460 index_page_str, qs->path, refname);
1461 free(escaped_refname);
1469 fcgi_printf(c, "</div>\n"); /* #branches_content */
1474 static const struct got_error *
1475 gotweb_render_tree(struct request *c)
1477 const struct got_error *error = NULL;
1478 struct transport *t = c->t;
1479 struct repo_commit *rc = NULL;
1480 char *age = NULL, *msg = NULL;
1483 error = got_get_repo_commits(c, 1);
1487 rc = TAILQ_FIRST(&t->repo_commits);
1489 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1493 error = gotweb_escape_html(&msg, rc->commit_msg);
1497 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1498 "<div id='tree_title'>Tree</div>\n"
1499 "</div>\n" /* #tree_title_wrapper */
1500 "<div id='tree_content'>\n"
1501 "<div id='tree_header_wrapper'>\n"
1502 "<div id='tree_header'>\n"
1503 "<div id='header_tree_title'>Tree:</div>\n"
1504 "<div id='header_tree'>%s</div>\n"
1505 "<div class='header_age_title'>Date:</div>\n"
1506 "<div class='header_age'>%s</div>\n"
1507 "<div id='header_commit_msg_title'>Message:</div>\n"
1508 "<div id='header_commit_msg'>%s</div>\n"
1509 "</div>\n" /* #tree_header */
1510 "</div>\n" /* #tree_header_wrapper */
1511 "<div class='dotted_line'></div>\n"
1512 "<div id='tree'>\n",
1519 error = got_output_repo_tree(c);
1523 fcgi_printf(c, "</div>\n"); /* #tree */
1524 fcgi_printf(c, "</div>\n"); /* #tree_content */
1530 static const struct got_error *
1531 gotweb_render_diff(struct request *c)
1533 const struct got_error *error = NULL;
1534 struct transport *t = c->t;
1535 struct repo_commit *rc = NULL;
1536 char *age = NULL, *author = NULL, *msg = NULL;
1539 error = got_get_repo_commits(c, 1);
1543 rc = TAILQ_FIRST(&t->repo_commits);
1545 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1548 error = gotweb_escape_html(&author, rc->author);
1551 error = gotweb_escape_html(&msg, rc->commit_msg);
1555 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1556 "<div id='diff_title'>Commit Diff</div>\n"
1557 "</div>\n" /* #diff_title_wrapper */
1558 "<div id='diff_content'>\n"
1559 "<div id='diff_header_wrapper'>\n"
1560 "<div id='diff_header'>\n"
1561 "<div id='header_diff_title'>Diff:</div>\n"
1562 "<div id='header_diff'>%s<br />%s</div>\n"
1563 "<div class='header_commit_title'>Commit:</div>\n"
1564 "<div class='header_commit'>%s</div>\n"
1565 "<div id='header_tree_title'>Tree:</div>\n"
1566 "<div id='header_tree'>%s</div>\n"
1567 "<div class='header_author_title'>Author:</div>\n"
1568 "<div class='header_author'>%s</div>\n"
1569 "<div class='header_age_title'>Date:</div>\n"
1570 "<div class='header_age'>%s</div>\n"
1571 "<div id='header_commit_msg_title'>Message:</div>\n"
1572 "<div id='header_commit_msg'>%s</div>\n"
1573 "</div>\n" /* #diff_header */
1574 "</div>\n" /* #diff_header_wrapper */
1575 "<div class='dotted_line'></div>\n"
1576 "<div id='diff'>\n",
1577 rc->parent_id, rc->commit_id,
1586 error = got_output_repo_diff(c);
1590 fcgi_printf(c, "</div>\n"); /* #diff */
1591 fcgi_printf(c, "</div>\n"); /* #diff_content */
1599 static const struct got_error *
1600 gotweb_render_summary(struct request *c)
1602 const struct got_error *error = NULL;
1603 struct transport *t = c->t;
1604 struct server *srv = c->srv;
1607 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1610 if (srv->show_repo_description) {
1612 "<div id='description_title'>Description:</div>\n"
1613 "<div id='description'>%s</div>\n",
1614 t->repo_dir->description ? t->repo_dir->description : "");
1619 if (srv->show_repo_owner) {
1621 "<div id='repo_owner_title'>Owner:</div>\n"
1622 "<div id='repo_owner'>%s</div>\n",
1623 t->repo_dir->owner ? t->repo_dir->owner : "");
1628 if (srv->show_repo_age) {
1630 "<div id='last_change_title'>Last Change:</div>\n"
1631 "<div id='last_change'>%s</div>\n",
1637 if (srv->show_repo_cloneurl) {
1639 "<div id='cloneurl_title'>Clone URL:</div>\n"
1640 "<div id='cloneurl'>%s</div>\n",
1641 t->repo_dir->url ? t->repo_dir->url : "");
1646 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1650 error = gotweb_render_briefs(c);
1652 log_warnx("%s: %s", __func__, error->msg);
1656 error = gotweb_render_tags(c);
1658 log_warnx("%s: %s", __func__, error->msg);
1662 error = gotweb_render_branches(c);
1664 log_warnx("%s: %s", __func__, error->msg);
1669 static const struct got_error *
1670 gotweb_render_tag(struct request *c)
1672 const struct got_error *error = NULL;
1673 struct repo_tag *rt = NULL;
1674 struct transport *t = c->t;
1675 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1677 error = got_get_repo_tags(c, 1);
1681 if (t->tag_count == 0) {
1682 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1687 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1689 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1692 error = gotweb_escape_html(&author, rt->tagger);
1695 error = gotweb_escape_html(&msg, rt->commit_msg);
1699 if (strncmp(rt->tag_name, "refs/", 5) == 0)
1701 error = gotweb_escape_html(&tagname, rt->tag_name);
1705 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1706 "<div id='tags_title'>Tag</div>\n"
1707 "</div>\n" /* #tags_title_wrapper */
1708 "<div id='tags_content'>\n"
1709 "<div id='tag_header_wrapper'>\n"
1710 "<div id='tag_header'>\n"
1711 "<div class='header_commit_title'>Commit:</div>\n"
1712 "<div class='header_commit'>%s"
1713 " <span class='refs_str'>(%s)</span></div>\n"
1714 "<div class='header_author_title'>Tagger:</div>\n"
1715 "<div class='header_author'>%s</div>\n"
1716 "<div class='header_age_title'>Date:</div>\n"
1717 "<div class='header_age'>%s</div>\n"
1718 "<div id='header_commit_msg_title'>Message:</div>\n"
1719 "<div id='header_commit_msg'>%s</div>\n"
1720 "</div>\n" /* #tag_header */
1721 "<div class='dotted_line'></div>\n"
1722 "<div id='tag_commit'>\n%s</div>"
1723 "</div>", /* tag_header_wrapper */
1738 static const struct got_error *
1739 gotweb_render_tags(struct request *c)
1741 const struct got_error *error = NULL;
1742 struct repo_tag *rt = NULL;
1743 struct server *srv = c->srv;
1744 struct transport *t = c->t;
1745 struct querystring *qs = t->qs;
1746 struct repo_dir *repo_dir = t->repo_dir;
1747 const char *index_page_str;
1748 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1749 int r, commit_found = 0;
1751 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1753 if (qs->action == BRIEFS) {
1755 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1757 error = got_get_repo_tags(c, srv->max_commits_display);
1761 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1762 "<div id='tags_title'>Tags</div>\n"
1763 "</div>\n" /* #tags_title_wrapper */
1764 "<div id='tags_content'>\n");
1768 if (t->tag_count == 0) {
1769 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1770 "This repository contains no tags");
1775 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1776 if (commit_found == 0 && qs->commit != NULL) {
1777 if (strcmp(qs->commit, rt->commit_id) != 0)
1782 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1786 if (strncmp(rt->tag_name, "refs/tags/", 10) == 0)
1788 error = gotweb_escape_html(&tagname, rt->tag_name);
1792 if (rt->tag_commit != NULL) {
1793 newline = strchr(rt->tag_commit, '\n');
1796 error = gotweb_escape_html(&msg, rt->tag_commit);
1801 r = fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1802 "<div class='tag'>%s</div>\n"
1803 "<div class='tag_log'>"
1804 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1806 "</div>\n" /* .tag_log */
1807 "<div class='navs_wrapper'>\n"
1808 "<div class='navs'>"
1809 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1812 "<a href='?index_page=%s&path=%s&action=briefs&commit=%s'>"
1815 "<a href='?index_page=%s&path=%s&action=commits&commit=%s'>"
1817 "</div>\n" /* .navs */
1818 "</div>\n" /* .navs_wrapper */
1819 "<div class='dotted_line'></div>\n",
1822 index_page_str, repo_dir->name, rt->commit_id,
1824 index_page_str, repo_dir->name, rt->commit_id,
1825 index_page_str, repo_dir->name, rt->commit_id,
1826 index_page_str, repo_dir->name, rt->commit_id);
1837 if (t->next_id || t->prev_id) {
1838 error = gotweb_render_navs(c);
1842 fcgi_printf(c, "</div>\n"); /* #tags_content */
1850 const struct got_error *
1851 gotweb_escape_html(char **escaped_html, const char *orig_html)
1853 const struct got_error *error = NULL;
1854 struct escape_pair {
1865 size_t orig_len, len;
1868 orig_len = strlen(orig_html);
1870 for (i = 0; i < orig_len; i++) {
1871 for (j = 0; j < nitems(esc); j++) {
1872 if (orig_html[i] != esc[j].c)
1874 len += strlen(esc[j].s) - 1 /* escaped char */;
1878 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1879 if (*escaped_html == NULL)
1880 return got_error_from_errno("calloc");
1883 for (i = 0; i < orig_len; i++) {
1885 for (j = 0; j < nitems(esc); j++) {
1886 if (orig_html[i] != esc[j].c)
1889 if (strlcat(*escaped_html, esc[j].s, len + 1)
1891 error = got_error(GOT_ERR_NO_SPACE);
1894 x += strlen(esc[j].s);
1899 (*escaped_html)[x] = orig_html[i];
1905 free(*escaped_html);
1906 *escaped_html = NULL;
1908 (*escaped_html)[x] = '\0';
1914 static const struct got_error *
1915 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1917 const struct got_error *error = NULL;
1918 struct socket *sock = c->sock;
1919 struct server *srv = c->srv;
1920 struct transport *t = c->t;
1925 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1926 GOTWEB_GIT_DIR) == -1)
1927 return got_error_from_errno("asprintf");
1929 dt = opendir(dir_test);
1933 repo_dir->path = strdup(dir_test);
1934 if (repo_dir->path == NULL) {
1936 error = got_error_from_errno("strdup");
1943 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1944 GOTWEB_GOT_DIR) == -1) {
1946 error = got_error_from_errno("asprintf");
1950 dt = opendir(dir_test);
1955 error = got_error(GOT_ERR_NOT_GIT_REPO);
1959 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1960 repo_dir->name) == -1) {
1961 error = got_error_from_errno("asprintf");
1966 repo_dir->path = strdup(dir_test);
1967 if (repo_dir->path == NULL) {
1969 error = got_error_from_errno("strdup");
1973 dt = opendir(dir_test);
1975 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1980 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1983 error = gotweb_get_repo_description(&repo_dir->description, srv,
1987 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
1990 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
1994 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
1998 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1999 error = got_error_from_errno("closedir");
2003 static const struct got_error *
2004 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2006 const struct got_error *error;
2008 *repo_dir = calloc(1, sizeof(**repo_dir));
2009 if (*repo_dir == NULL)
2010 return got_error_from_errno("calloc");
2012 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2013 error = got_error_from_errno("asprintf");
2018 (*repo_dir)->owner = NULL;
2019 (*repo_dir)->description = NULL;
2020 (*repo_dir)->url = NULL;
2021 (*repo_dir)->age = NULL;
2022 (*repo_dir)->path = NULL;
2027 static const struct got_error *
2028 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2030 const struct got_error *error = NULL;
2032 char *d_file = NULL;
2036 *description = NULL;
2037 if (srv->show_repo_description == 0)
2040 if (asprintf(&d_file, "%s/description", dir) == -1)
2041 return got_error_from_errno("asprintf");
2043 f = fopen(d_file, "r");
2045 if (errno == ENOENT || errno == EACCES)
2047 error = got_error_from_errno2("fopen", d_file);
2051 if (fseek(f, 0, SEEK_END) == -1) {
2052 error = got_ferror(f, GOT_ERR_IO);
2057 error = got_ferror(f, GOT_ERR_IO);
2064 if (fseek(f, 0, SEEK_SET) == -1) {
2065 error = got_ferror(f, GOT_ERR_IO);
2068 *description = calloc(len + 1, sizeof(**description));
2069 if (*description == NULL) {
2070 error = got_error_from_errno("calloc");
2074 n = fread(*description, 1, len, f);
2075 if (n == 0 && ferror(f))
2076 error = got_ferror(f, GOT_ERR_IO);
2078 if (f != NULL && fclose(f) == EOF && error == NULL)
2079 error = got_error_from_errno("fclose");
2084 static const struct got_error *
2085 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2087 const struct got_error *error = NULL;
2089 char *d_file = NULL;
2095 if (srv->show_repo_cloneurl == 0)
2098 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2099 return got_error_from_errno("asprintf");
2101 f = fopen(d_file, "r");
2103 if (errno != ENOENT && errno != EACCES)
2104 error = got_error_from_errno2("fopen", d_file);
2108 if (fseek(f, 0, SEEK_END) == -1) {
2109 error = got_ferror(f, GOT_ERR_IO);
2114 error = got_ferror(f, GOT_ERR_IO);
2120 if (fseek(f, 0, SEEK_SET) == -1) {
2121 error = got_ferror(f, GOT_ERR_IO);
2125 *url = calloc(len + 1, sizeof(**url));
2127 error = got_error_from_errno("calloc");
2131 n = fread(*url, 1, len, f);
2132 if (n == 0 && ferror(f))
2133 error = got_ferror(f, GOT_ERR_IO);
2135 if (f != NULL && fclose(f) == EOF && error == NULL)
2136 error = got_error_from_errno("fclose");
2141 const struct got_error *
2142 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2145 long long diff_time;
2146 const char *years = "years ago", *months = "months ago";
2147 const char *weeks = "weeks ago", *days = "days ago";
2148 const char *hours = "hours ago", *minutes = "minutes ago";
2149 const char *seconds = "seconds ago", *now = "right now";
2157 diff_time = time(NULL) - committer_time;
2158 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2159 if (asprintf(repo_age, "%lld %s",
2160 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2161 return got_error_from_errno("asprintf");
2162 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2163 if (asprintf(repo_age, "%lld %s",
2164 (diff_time / 60 / 60 / 24 / (365 / 12)),
2166 return got_error_from_errno("asprintf");
2167 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2168 if (asprintf(repo_age, "%lld %s",
2169 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2170 return got_error_from_errno("asprintf");
2171 } else if (diff_time > 60 * 60 * 24 * 2) {
2172 if (asprintf(repo_age, "%lld %s",
2173 (diff_time / 60 / 60 / 24), days) == -1)
2174 return got_error_from_errno("asprintf");
2175 } else if (diff_time > 60 * 60 * 2) {
2176 if (asprintf(repo_age, "%lld %s",
2177 (diff_time / 60 / 60), hours) == -1)
2178 return got_error_from_errno("asprintf");
2179 } else if (diff_time > 60 * 2) {
2180 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2182 return got_error_from_errno("asprintf");
2183 } else if (diff_time > 2) {
2184 if (asprintf(repo_age, "%lld %s", diff_time,
2186 return got_error_from_errno("asprintf");
2188 if (asprintf(repo_age, "%s", now) == -1)
2189 return got_error_from_errno("asprintf");
2193 if (gmtime_r(&committer_time, &tm) == NULL)
2194 return got_error_from_errno("gmtime_r");
2196 s = asctime_r(&tm, datebuf);
2198 return got_error_from_errno("asctime_r");
2200 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2201 return got_error_from_errno("asprintf");