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 *, 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->document_root, 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 *document_root,
305 struct server *srv = NULL;
307 /* check against document_root first */
308 if (strlen(server_name) > 0)
309 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
310 if (strcmp(srv->name, server_name) == 0)
313 /* check against document_root second */
314 if (strlen(document_root) > 0)
315 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
316 if (strcmp(srv->name, document_root) == 0)
319 /* check against subdomain third */
320 if (strlen(subdomain) > 0)
321 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
322 if (strcmp(srv->name, subdomain) == 0)
325 /* if those fail, send first server */
326 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
333 const struct got_error *
334 gotweb_init_transport(struct transport **t)
336 const struct got_error *error = NULL;
338 *t = calloc(1, sizeof(**t));
340 return got_error_from_errno2("%s: calloc", __func__);
342 TAILQ_INIT(&(*t)->repo_commits);
343 TAILQ_INIT(&(*t)->repo_tags);
346 (*t)->repo_dir = NULL;
348 (*t)->next_id = NULL;
349 (*t)->prev_id = NULL;
356 static const struct got_error *
357 gotweb_init_querystring(struct querystring **qs)
359 const struct got_error *error = NULL;
361 *qs = calloc(1, sizeof(**qs));
363 return got_error_from_errno2("%s: calloc", __func__);
365 (*qs)->action = INDEX;
366 (*qs)->commit = NULL;
368 (*qs)->folder = NULL;
369 (*qs)->headref = strdup("HEAD");
370 if ((*qs)->headref == NULL) {
371 return got_error_from_errno2("%s: strdup", __func__);
373 (*qs)->index_page = 0;
374 (*qs)->index_page_str = NULL;
380 static const struct got_error *
381 gotweb_parse_querystring(struct querystring **qs, char *qst)
383 const struct got_error *error = NULL;
384 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
385 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
392 return got_error_from_errno2("%s: strdup", __func__);
397 while (tok1_pair != NULL) {
398 strsep(&tok1_end, "&");
400 tok2 = strdup(tok1_pair);
403 return got_error_from_errno2("%s: strdup", __func__);
409 while (tok2_pair != NULL) {
410 strsep(&tok2_end, "=");
412 error = gotweb_assign_querystring(qs, tok2_pair,
417 tok2_pair = tok2_end;
420 tok1_pair = tok1_end;
430 static const struct got_error *
431 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
433 const struct got_error *error = NULL;
437 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
438 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
441 switch (querystring_keys[el_cnt].element) {
443 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
444 if (strcmp(value, action_keys[a_cnt].name) != 0)
446 else if (strcmp(value,
447 action_keys[a_cnt].name) == 0){
449 action_keys[a_cnt].action;
457 (*qs)->commit = strdup(value);
458 if ((*qs)->commit == NULL) {
459 error = got_error_from_errno2("%s: strdup",
465 (*qs)->file = strdup(value);
466 if ((*qs)->file == NULL) {
467 error = got_error_from_errno2("%s: strdup",
473 (*qs)->folder = strdup(value);
474 if ((*qs)->folder == NULL) {
475 error = got_error_from_errno2("%s: strdup",
481 (*qs)->headref = strdup(value);
482 if ((*qs)->headref == NULL) {
483 error = got_error_from_errno2("%s: strdup",
489 if (strlen(value) == 0)
491 (*qs)->index_page_str = strdup(value);
492 if ((*qs)->index_page_str == NULL) {
493 error = got_error_from_errno2("%s: strdup",
497 (*qs)->index_page = strtonum(value, INT64_MIN,
500 error = got_error_from_errno3("%s: strtonum %s",
504 if ((*qs)->index_page < 0) {
505 (*qs)->index_page = 0;
506 sprintf((*qs)->index_page_str, "%d", 0);
510 (*qs)->path = strdup(value);
511 if ((*qs)->path == NULL) {
512 error = got_error_from_errno2("%s: strdup",
518 if (strlen(value) == 0)
520 (*qs)->page_str = strdup(value);
521 if ((*qs)->page_str == NULL) {
522 error = got_error_from_errno2("%s: strdup",
526 (*qs)->page = strtonum(value, INT64_MIN,
529 error = got_error_from_errno3("%s: strtonum %s",
533 if ((*qs)->page < 0) {
535 sprintf((*qs)->page_str, "%d", 0);
547 gotweb_free_repo_tag(struct repo_tag *rt)
550 free(rt->commit_msg);
558 gotweb_free_repo_commit(struct repo_commit *rc)
568 free(rc->commit_msg);
574 gotweb_free_querystring(struct querystring *qs)
581 free(qs->index_page_str);
589 gotweb_free_repo_dir(struct repo_dir *repo_dir)
591 if (repo_dir != NULL) {
592 free(repo_dir->name);
593 free(repo_dir->owner);
594 free(repo_dir->description);
597 free(repo_dir->path);
603 gotweb_free_transport(struct transport *t)
605 struct repo_commit *rc = NULL, *trc = NULL;
606 struct repo_tag *rt = NULL, *trt = NULL;
608 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
609 TAILQ_REMOVE(&t->repo_commits, rc, entry);
610 gotweb_free_repo_commit(rc);
612 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
613 TAILQ_REMOVE(&t->repo_tags, rt, entry);
614 gotweb_free_repo_tag(rt);
616 gotweb_free_repo_dir(t->repo_dir);
617 gotweb_free_querystring(t->qs);
625 const struct got_error *
626 gotweb_render_content_type(struct request *c, const uint8_t *type)
628 const char *csp = "default-src 'self'; script-src 'none'; "
629 "object-src 'none';";
632 "Content-Security-Policy: %s\r\n"
633 "Content-Type: %s\r\n\r\n",
638 const struct got_error *
639 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
642 fcgi_printf(c, "Content-type: %s\r\n"
643 "Content-disposition: attachment; filename=%s\r\n\r\n",
648 static const struct got_error *
649 gotweb_render_header(struct request *c)
651 struct server *srv = c->srv;
652 struct querystring *qs = c->t->qs;
653 char droot[PATH_MAX];
656 if (strlen(c->document_root) > 0) {
657 r = snprintf(droot, sizeof(droot), "/%s/", c->document_root);
658 if (r < 0 || (size_t)r >= sizeof(droot))
659 return got_error(GOT_ERR_NO_SPACE);
661 strlcpy(droot, "/", sizeof(droot));
663 r = fcgi_printf(c, "<!doctype html>\n"
666 "<title>%s</title>\n"
667 "<meta charset='utf-8' />\n"
668 "<meta name='viewport' content='initial-scale=.75' />\n"
669 "<meta name='msapplication-TileColor' content='#da532c' />\n"
670 "<meta name='theme-color' content='#ffffff'/>\n"
671 "<link rel='apple-touch-icon' sizes='180x180'"
672 " href='/apple-touch-icon.png' />\n"
673 "<link rel='icon' type='image/png' sizes='32x32'"
674 " href='/favicon-32x32.png' />\n"
675 "<link rel='icon' type='image/png' sizes='16x16'"
676 " href='/favicon-16x16.png' />\n"
677 "<link rel='manifest' href='/site.webmanifest'/>\n"
678 "<link rel='mask-icon' href='/safari-pinned-tab.svg' />\n"
679 "<link rel='stylesheet' type='text/css' href='%s%s' />\n"
682 "<div id='gw_body'>\n"
683 "<div id='header'>\n"
684 "<div id='got_link'>"
685 "<a href='%s' target='_blank'>"
686 "<img src='%s%s' alt='logo' id='logo' />"
688 "</div>\n" /* #got_link */
689 "</div>\n" /* #header */
690 "<div id='site_path'>\n"
691 "<div id='site_link'>\n"
692 "<a href='/%s?index_page=%d'>%s</a>",
694 droot, srv->custom_css,
697 c->document_root, qs->index_page, srv->site_link);
702 if (qs->path != NULL) {
703 r = fcgi_printf(c, " / "
704 "<a href='/%s?index_page=%d&path=%s&action=summary'>"
706 c->document_root, qs->index_page, qs->path,
711 if (qs->action != INDEX) {
712 const char *action = "";
714 switch (qs->action) {
741 if (fcgi_printf(c, " / %s", action) == -1)
746 fcgi_printf(c, "</div>\n" /* #site_path */
747 "</div>\n" /* #site_link */
748 "<div id='content'>\n");
754 static const struct got_error *
755 gotweb_render_footer(struct request *c)
757 const struct got_error *error = NULL;
758 struct server *srv = c->srv;
759 const char *siteowner = " ";
760 char *escaped_owner = NULL;
762 if (srv->show_site_owner) {
763 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
766 siteowner = escaped_owner;
769 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
770 "<div id='site_owner'>%s</div>\n"
771 "</div>\n" /* #site_owner_wrapper */
772 "</div>\n" /* #content */
773 "</div>\n" /* #gw_body */
774 "</body>\n</html>\n", siteowner);
780 static const struct got_error *
781 gotweb_render_navs(struct request *c)
783 const struct got_error *error = NULL;
784 struct transport *t = c->t;
785 struct querystring *qs = t->qs;
786 struct server *srv = c->srv;
787 char *nhref = NULL, *phref = NULL;
790 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
796 if (qs->index_page > 0) {
797 if (asprintf(&phref, "index_page=%d",
798 qs->index_page - 1) == -1) {
799 error = got_error_from_errno2("%s: asprintf",
807 if (t->prev_id && qs->commit != NULL &&
808 strcmp(qs->commit, t->prev_id) != 0) {
809 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
810 "&action=briefs&commit=%s&headref=%s",
811 qs->index_page, qs->path, qs->page - 1, t->prev_id,
812 qs->headref) == -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=commits&commit=%s&headref=%s&folder=%s"
826 qs->index_page, qs->path, qs->page - 1, t->prev_id,
827 qs->headref, qs->folder ? qs->folder : "",
828 qs->file ? qs->file : "") == -1) {
829 error = got_error_from_errno2("%s: asprintf",
837 if (t->prev_id && qs->commit != NULL &&
838 strcmp(qs->commit, t->prev_id) != 0) {
839 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
840 "&action=tags&commit=%s&headref=%s",
841 qs->index_page, qs->path, qs->page - 1, t->prev_id,
842 qs->headref) == -1) {
843 error = got_error_from_errno2("%s: asprintf",
856 r = fcgi_printf(c, "<a href='?%s'>Previous</a>", phref);
861 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
862 "<div id='nav_next'>");
869 if (t->next_disp == srv->max_repos_display &&
870 t->repos_total != (qs->index_page + 1) *
871 srv->max_repos_display) {
872 if (asprintf(&nhref, "index_page=%d",
873 qs->index_page + 1) == -1) {
874 error = got_error_from_errno2("%s: asprintf",
883 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
884 "&action=briefs&commit=%s&headref=%s",
885 qs->index_page, qs->path, qs->page + 1, t->next_id,
886 qs->headref) == -1) {
887 error = got_error_from_errno2("%s: asprintf",
896 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
897 "&action=commits&commit=%s&headref=%s&folder=%s"
899 qs->index_page, qs->path, qs->page + 1, t->next_id,
900 qs->headref, qs->folder ? qs->folder : "",
901 qs->file ? qs->file : "") == -1) {
902 error = got_error_from_errno2("%s: asprintf",
911 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
912 "&action=tags&commit=%s&headref=%s",
913 qs->index_page, qs->path, qs->page + 1, t->next_id,
914 qs->headref) == -1) {
915 error = got_error_from_errno2("%s: asprintf",
927 r = fcgi_printf(c, "<a href='?%s'>Next</a>", nhref);
931 fcgi_printf(c, "</div>\n"); /* #nav_next */
932 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
943 static const struct got_error *
944 gotweb_render_index(struct request *c)
946 const struct got_error *error = NULL;
947 struct server *srv = c->srv;
948 struct transport *t = c->t;
949 struct querystring *qs = t->qs;
950 struct repo_dir *repo_dir = NULL;
952 struct dirent **sd_dent;
953 const char *index_page_str;
956 unsigned int d_cnt, d_i, d_disp = 0;
959 index_page_str = qs->index_page_str ? qs->index_page_str : "";
961 d = opendir(srv->repos_path);
963 error = got_error_from_errno2("opendir", srv->repos_path);
967 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
969 error = got_error_from_errno2("scandir", srv->repos_path);
973 /* get total count of repos */
974 for (d_i = 0; d_i < d_cnt; d_i++) {
975 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
976 strcmp(sd_dent[d_i]->d_name, "..") == 0)
979 if (asprintf(&c_path, "%s/%s", srv->repos_path,
980 sd_dent[d_i]->d_name) == -1) {
981 error = got_error_from_errno("asprintf");
985 if (lstat(c_path, &st) == 0 && S_ISDIR(st.st_mode) &&
986 !got_path_dir_is_empty(c_path))
992 r = fcgi_printf(c, "<div id='index_header'>\n"
993 "<div id='index_header_project'>Project</div>\n");
997 if (srv->show_repo_description)
998 if (fcgi_printf(c, "<div id='index_header_description'>"
999 "Description</div>\n") == -1)
1001 if (srv->show_repo_owner)
1002 if (fcgi_printf(c, "<div id='index_header_owner'>"
1003 "Owner</div>\n") == -1)
1005 if (srv->show_repo_age)
1006 if (fcgi_printf(c, "<div id='index_header_age'>"
1007 "Last Change</div>\n") == -1)
1009 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1012 for (d_i = 0; d_i < d_cnt; d_i++) {
1013 if (srv->max_repos > 0 && (d_i - 2) == srv->max_repos)
1014 break; /* account for parent and self */
1016 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1017 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1020 if (qs->index_page > 0 && (qs->index_page *
1021 srv->max_repos_display) > t->prev_disp) {
1026 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1030 error = gotweb_load_got_path(c, repo_dir);
1031 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1035 else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1038 if (lstat(repo_dir->path, &st) == 0 &&
1039 S_ISDIR(st.st_mode) &&
1040 !got_path_dir_is_empty(repo_dir->path))
1043 gotweb_free_repo_dir(repo_dir);
1051 r = fcgi_printf(c, "<div class='index_wrapper'>\n"
1052 "<div class='index_project'>"
1053 "<a href='?index_page=%s&path=%s&action=summary'>"
1056 "</div>", /* .index_project */
1057 index_page_str, repo_dir->name,
1062 if (srv->show_repo_description) {
1064 "<div class='index_project_description'>\n"
1065 "%s</div>\n", repo_dir->description);
1070 if (srv->show_repo_owner) {
1071 r = fcgi_printf(c, "<div class='index_project_owner'>"
1072 "%s</div>\n", repo_dir->owner);
1077 if (srv->show_repo_age) {
1078 r = fcgi_printf(c, "<div class='index_project_age'>"
1079 "%s</div>\n", repo_dir->age);
1084 r = fcgi_printf(c, "<div class='navs_wrapper'>"
1085 "<div class='navs'>"
1086 "<a href='?index_page=%s&path=%s&action=summary'>"
1089 "<a href='?index_page=%s&path=%s&action=briefs'>"
1092 "<a href='?index_page=%s&path=%s&action=commits'>"
1095 "<a href='?index_page=%s&path=%s&action=tags'>"
1098 "<a href='?index_page=%s&path=%s&action=tree'>"
1101 "</div>" /* .navs */
1102 "<div class='dotted_line'></div>\n"
1103 "</div>\n" /* .navs_wrapper */
1104 "</div>\n", /* .index_wrapper */
1105 index_page_str, repo_dir->name,
1106 index_page_str, repo_dir->name,
1107 index_page_str, repo_dir->name,
1108 index_page_str, repo_dir->name,
1109 index_page_str, repo_dir->name);
1113 gotweb_free_repo_dir(repo_dir);
1115 error = got_repo_close(t->repo);
1119 if (d_disp == srv->max_repos_display)
1122 if (srv->max_repos_display == 0)
1124 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1126 if (t->repos_total <= srv->max_repos ||
1127 t->repos_total <= srv->max_repos_display)
1130 error = gotweb_render_navs(c);
1134 if (d != NULL && closedir(d) == EOF && error == NULL)
1135 error = got_error_from_errno("closedir");
1139 static const struct got_error *
1140 gotweb_render_blame(struct request *c)
1142 const struct got_error *error = NULL;
1143 struct transport *t = c->t;
1144 struct repo_commit *rc = NULL;
1145 char *age = NULL, *msg = NULL;
1148 error = got_get_repo_commits(c, 1);
1152 rc = TAILQ_FIRST(&t->repo_commits);
1154 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1157 error = gotweb_escape_html(&msg, rc->commit_msg);
1161 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1162 "<div id='blame_title'>Blame</div>\n"
1163 "</div>\n" /* #blame_title_wrapper */
1164 "<div id='blame_content'>\n"
1165 "<div id='blame_header_wrapper'>\n"
1166 "<div id='blame_header'>\n"
1167 "<div class='header_age_title'>Date:</div>\n"
1168 "<div class='header_age'>%s</div>\n"
1169 "<div id='header_commit_msg_title'>Message:</div>\n"
1170 "<div id='header_commit_msg'>%s</div>\n"
1171 "</div>\n" /* #blame_header */
1172 "</div>\n" /* #blame_header_wrapper */
1173 "<div class='dotted_line'></div>\n"
1174 "<div id='blame'>\n",
1180 error = got_output_file_blame(c);
1184 fcgi_printf(c, "</div>\n" /* #blame */
1185 "</div>\n"); /* #blame_content */
1191 static const struct got_error *
1192 gotweb_render_briefs(struct request *c)
1194 const struct got_error *error = NULL;
1195 struct repo_commit *rc = NULL;
1196 struct server *srv = c->srv;
1197 struct transport *t = c->t;
1198 struct querystring *qs = t->qs;
1199 struct repo_dir *repo_dir = t->repo_dir;
1200 const char *index_page_str;
1201 char *smallerthan, *newline;
1202 char *age = NULL, *author = NULL, *msg = NULL;
1205 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1207 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1208 "<div id='briefs_title'>Commit Briefs</div>\n"
1209 "</div>\n" /* #briefs_title_wrapper */
1210 "<div id='briefs_content'>\n");
1214 if (qs->action == SUMMARY) {
1215 qs->action = BRIEFS;
1216 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1218 error = got_get_repo_commits(c, srv->max_commits_display);
1222 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1223 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1227 smallerthan = strchr(rc->author, '<');
1229 *smallerthan = '\0';
1231 newline = strchr(rc->commit_msg, '\n');
1235 error = gotweb_escape_html(&author, rc->author);
1238 error = gotweb_escape_html(&msg, rc->commit_msg);
1242 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1243 "<div class='briefs_author'>%s</div>\n"
1244 "<div class='briefs_log'>"
1245 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1246 "&headref=%s'>%s</a>",
1249 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1257 error = gotweb_escape_html(&refs, rc->refs_str);
1261 " <span class='refs_str'>(%s)</span>", refs);
1266 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1269 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1270 "<div class='navs'>"
1271 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1272 "&headref=%s'>diff</a>"
1274 "<a href='?index_page=%s&path=%s&action=tree&commit=%s"
1275 "&headref=%s'>tree</a>"
1276 "</div>\n" /* .navs */
1277 "</div>\n" /* .navs_wrapper */
1278 "<div class='dotted_line'></div>\n",
1279 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1280 index_page_str, repo_dir->name, rc->commit_id, qs->headref);
1292 if (t->next_id || t->prev_id) {
1293 error = gotweb_render_navs(c);
1297 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1305 static const struct got_error *
1306 gotweb_render_commits(struct request *c)
1308 const struct got_error *error = NULL;
1309 struct repo_commit *rc = NULL;
1310 struct server *srv = c->srv;
1311 struct transport *t = c->t;
1312 struct querystring *qs = t->qs;
1313 struct repo_dir *repo_dir = t->repo_dir;
1314 const char *index_page_str;
1315 char *age = NULL, *author = NULL, *msg = NULL;
1318 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1320 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1321 "<div class='commits_title'>Commits</div>\n"
1322 "</div>\n" /* .commits_title_wrapper */
1323 "<div class='commits_content'>\n");
1327 error = got_get_repo_commits(c, srv->max_commits_display);
1331 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1332 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1335 error = gotweb_escape_html(&author, rc->author);
1338 error = gotweb_escape_html(&msg, rc->commit_msg);
1342 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1343 "<div class='commits_header'>\n"
1344 "<div class='header_commit_title'>Commit:</div>\n"
1345 "<div class='header_commit'>%s</div>\n"
1346 "<div class='header_author_title'>Author:</div>\n"
1347 "<div class='header_author'>%s</div>\n"
1348 "<div class='header_age_title'>Date:</div>\n"
1349 "<div class='header_age'>%s</div>\n"
1350 "</div>\n" /* .commits_header */
1351 "</div>\n" /* .commits_header_wrapper */
1352 "<div class='dotted_line'></div>\n"
1353 "<div class='commit'>\n%s</div>\n",
1361 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1362 "<div class='navs'>"
1363 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1366 "<a href='?index_page=%s&path=%s&action=tree&commit=%s'>"
1368 "</div>\n" /* .navs */
1369 "</div>\n" /* .navs_wrapper */
1370 "<div class='dotted_line'></div>\n",
1371 index_page_str, repo_dir->name, rc->commit_id,
1372 index_page_str, repo_dir->name, rc->commit_id);
1382 if (t->next_id || t->prev_id) {
1383 error = gotweb_render_navs(c);
1387 fcgi_printf(c, "</div>\n"); /* .commits_content */
1395 static const struct got_error *
1396 gotweb_render_branches(struct request *c)
1398 const struct got_error *error = NULL;
1399 struct got_reflist_head refs;
1400 struct got_reflist_entry *re;
1401 struct transport *t = c->t;
1402 struct querystring *qs = t->qs;
1403 struct got_repository *repo = t->repo;
1404 const char *index_page_str;
1408 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1412 error = got_ref_list(&refs, repo, "refs/heads",
1413 got_ref_cmp_by_name, NULL);
1417 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1418 "<div id='branches_title'>Branches</div>\n"
1419 "</div>\n" /* #branches_title_wrapper */
1420 "<div id='branches_content'>\n");
1424 TAILQ_FOREACH(re, &refs, entry) {
1425 const char *refname = NULL;
1426 char *escaped_refname = NULL;
1428 if (got_ref_is_symbolic(re->ref))
1431 refname = got_ref_get_name(re->ref);
1432 if (refname == NULL) {
1433 error = got_error_from_errno("strdup");
1436 if (strncmp(refname, "refs/heads/", 11) != 0)
1439 error = got_get_repo_age(&age, c, qs->path, refname,
1444 if (strncmp(refname, "refs/heads/", 11) == 0)
1446 error = gotweb_escape_html(&escaped_refname, refname);
1450 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1451 "<div class='branches_age'>%s</div>\n"
1452 "<div class='branches_space'> </div>\n"
1453 "<div class='branch'>"
1454 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1456 "</div>\n" /* .branch */
1457 "<div class='navs_wrapper'>\n"
1458 "<div class='navs'>"
1459 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1462 "<a href='?index_page=%s&path=%s&action=briefs&headref=%s'>"
1465 "<a href='?index_page=%s&path=%s&action=commits&headref=%s'>"
1467 "</div>\n" /* .navs */
1468 "</div>\n" /* .navs_wrapper */
1469 "<div class='dotted_line'></div>\n"
1470 "</div>\n", /* .branches_wrapper */
1472 index_page_str, qs->path, refname,
1474 index_page_str, qs->path, refname,
1475 index_page_str, qs->path, refname,
1476 index_page_str, qs->path, refname);
1477 free(escaped_refname);
1485 fcgi_printf(c, "</div>\n"); /* #branches_content */
1490 static const struct got_error *
1491 gotweb_render_tree(struct request *c)
1493 const struct got_error *error = NULL;
1494 struct transport *t = c->t;
1495 struct repo_commit *rc = NULL;
1496 char *age = NULL, *msg = NULL;
1499 error = got_get_repo_commits(c, 1);
1503 rc = TAILQ_FIRST(&t->repo_commits);
1505 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1509 error = gotweb_escape_html(&msg, rc->commit_msg);
1513 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1514 "<div id='tree_title'>Tree</div>\n"
1515 "</div>\n" /* #tree_title_wrapper */
1516 "<div id='tree_content'>\n"
1517 "<div id='tree_header_wrapper'>\n"
1518 "<div id='tree_header'>\n"
1519 "<div id='header_tree_title'>Tree:</div>\n"
1520 "<div id='header_tree'>%s</div>\n"
1521 "<div class='header_age_title'>Date:</div>\n"
1522 "<div class='header_age'>%s</div>\n"
1523 "<div id='header_commit_msg_title'>Message:</div>\n"
1524 "<div id='header_commit_msg'>%s</div>\n"
1525 "</div>\n" /* #tree_header */
1526 "</div>\n" /* #tree_header_wrapper */
1527 "<div class='dotted_line'></div>\n"
1528 "<div id='tree'>\n",
1535 error = got_output_repo_tree(c);
1539 fcgi_printf(c, "</div>\n"); /* #tree */
1540 fcgi_printf(c, "</div>\n"); /* #tree_content */
1546 static const struct got_error *
1547 gotweb_render_diff(struct request *c)
1549 const struct got_error *error = NULL;
1550 struct transport *t = c->t;
1551 struct repo_commit *rc = NULL;
1552 char *age = NULL, *author = NULL, *msg = NULL;
1555 error = got_get_repo_commits(c, 1);
1559 rc = TAILQ_FIRST(&t->repo_commits);
1561 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1564 error = gotweb_escape_html(&author, rc->author);
1567 error = gotweb_escape_html(&msg, rc->commit_msg);
1571 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1572 "<div id='diff_title'>Commit Diff</div>\n"
1573 "</div>\n" /* #diff_title_wrapper */
1574 "<div id='diff_content'>\n"
1575 "<div id='diff_header_wrapper'>\n"
1576 "<div id='diff_header'>\n"
1577 "<div id='header_diff_title'>Diff:</div>\n"
1578 "<div id='header_diff'>%s<br />%s</div>\n"
1579 "<div class='header_commit_title'>Commit:</div>\n"
1580 "<div class='header_commit'>%s</div>\n"
1581 "<div id='header_tree_title'>Tree:</div>\n"
1582 "<div id='header_tree'>%s</div>\n"
1583 "<div class='header_author_title'>Author:</div>\n"
1584 "<div class='header_author'>%s</div>\n"
1585 "<div class='header_age_title'>Date:</div>\n"
1586 "<div class='header_age'>%s</div>\n"
1587 "<div id='header_commit_msg_title'>Message:</div>\n"
1588 "<div id='header_commit_msg'>%s</div>\n"
1589 "</div>\n" /* #diff_header */
1590 "</div>\n" /* #diff_header_wrapper */
1591 "<div class='dotted_line'></div>\n"
1592 "<div id='diff'>\n",
1593 rc->parent_id, rc->commit_id,
1602 error = got_output_repo_diff(c);
1606 fcgi_printf(c, "</div>\n"); /* #diff */
1607 fcgi_printf(c, "</div>\n"); /* #diff_content */
1615 static const struct got_error *
1616 gotweb_render_summary(struct request *c)
1618 const struct got_error *error = NULL;
1619 struct transport *t = c->t;
1620 struct server *srv = c->srv;
1623 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1626 if (srv->show_repo_description) {
1628 "<div id='description_title'>Description:</div>\n"
1629 "<div id='description'>%s</div>\n",
1630 t->repo_dir->description ? t->repo_dir->description : "");
1635 if (srv->show_repo_owner) {
1637 "<div id='repo_owner_title'>Owner:</div>\n"
1638 "<div id='repo_owner'>%s</div>\n",
1639 t->repo_dir->owner ? t->repo_dir->owner : "");
1644 if (srv->show_repo_age) {
1646 "<div id='last_change_title'>Last Change:</div>\n"
1647 "<div id='last_change'>%s</div>\n",
1653 if (srv->show_repo_cloneurl) {
1655 "<div id='cloneurl_title'>Clone URL:</div>\n"
1656 "<div id='cloneurl'>%s</div>\n",
1657 t->repo_dir->url ? t->repo_dir->url : "");
1662 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1666 error = gotweb_render_briefs(c);
1668 log_warnx("%s: %s", __func__, error->msg);
1672 error = gotweb_render_tags(c);
1674 log_warnx("%s: %s", __func__, error->msg);
1678 error = gotweb_render_branches(c);
1680 log_warnx("%s: %s", __func__, error->msg);
1685 static const struct got_error *
1686 gotweb_render_tag(struct request *c)
1688 const struct got_error *error = NULL;
1689 struct repo_tag *rt = NULL;
1690 struct transport *t = c->t;
1691 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1693 error = got_get_repo_tags(c, 1);
1697 if (t->tag_count == 0) {
1698 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1703 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1705 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1708 error = gotweb_escape_html(&author, rt->tagger);
1711 error = gotweb_escape_html(&msg, rt->commit_msg);
1715 if (strncmp(rt->tag_name, "refs/", 5) == 0)
1717 error = gotweb_escape_html(&tagname, rt->tag_name);
1721 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1722 "<div id='tags_title'>Tag</div>\n"
1723 "</div>\n" /* #tags_title_wrapper */
1724 "<div id='tags_content'>\n"
1725 "<div id='tag_header_wrapper'>\n"
1726 "<div id='tag_header'>\n"
1727 "<div class='header_commit_title'>Commit:</div>\n"
1728 "<div class='header_commit'>%s"
1729 " <span class='refs_str'>(%s)</span></div>\n"
1730 "<div class='header_author_title'>Tagger:</div>\n"
1731 "<div class='header_author'>%s</div>\n"
1732 "<div class='header_age_title'>Date:</div>\n"
1733 "<div class='header_age'>%s</div>\n"
1734 "<div id='header_commit_msg_title'>Message:</div>\n"
1735 "<div id='header_commit_msg'>%s</div>\n"
1736 "</div>\n" /* #tag_header */
1737 "<div class='dotted_line'></div>\n"
1738 "<div id='tag_commit'>\n%s</div>"
1739 "</div>", /* tag_header_wrapper */
1754 static const struct got_error *
1755 gotweb_render_tags(struct request *c)
1757 const struct got_error *error = NULL;
1758 struct repo_tag *rt = NULL;
1759 struct server *srv = c->srv;
1760 struct transport *t = c->t;
1761 struct querystring *qs = t->qs;
1762 struct repo_dir *repo_dir = t->repo_dir;
1763 const char *index_page_str;
1764 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1765 int r, commit_found = 0;
1767 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1769 if (qs->action == BRIEFS) {
1771 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1773 error = got_get_repo_tags(c, srv->max_commits_display);
1777 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1778 "<div id='tags_title'>Tags</div>\n"
1779 "</div>\n" /* #tags_title_wrapper */
1780 "<div id='tags_content'>\n");
1784 if (t->tag_count == 0) {
1785 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1786 "This repository contains no tags");
1791 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1792 if (commit_found == 0 && qs->commit != NULL) {
1793 if (strcmp(qs->commit, rt->commit_id) != 0)
1798 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1802 if (strncmp(rt->tag_name, "refs/tags/", 10) == 0)
1804 error = gotweb_escape_html(&tagname, rt->tag_name);
1808 if (rt->tag_commit != NULL) {
1809 newline = strchr(rt->tag_commit, '\n');
1812 error = gotweb_escape_html(&msg, rt->tag_commit);
1817 r = fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1818 "<div class='tag'>%s</div>\n"
1819 "<div class='tag_log'>"
1820 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1822 "</div>\n" /* .tag_log */
1823 "<div class='navs_wrapper'>\n"
1824 "<div class='navs'>"
1825 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1828 "<a href='?index_page=%s&path=%s&action=briefs&commit=%s'>"
1831 "<a href='?index_page=%s&path=%s&action=commits&commit=%s'>"
1833 "</div>\n" /* .navs */
1834 "</div>\n" /* .navs_wrapper */
1835 "<div class='dotted_line'></div>\n",
1838 index_page_str, repo_dir->name, rt->commit_id,
1840 index_page_str, repo_dir->name, rt->commit_id,
1841 index_page_str, repo_dir->name, rt->commit_id,
1842 index_page_str, repo_dir->name, rt->commit_id);
1853 if (t->next_id || t->prev_id) {
1854 error = gotweb_render_navs(c);
1858 fcgi_printf(c, "</div>\n"); /* #tags_content */
1866 const struct got_error *
1867 gotweb_escape_html(char **escaped_html, const char *orig_html)
1869 const struct got_error *error = NULL;
1870 struct escape_pair {
1881 size_t orig_len, len;
1884 orig_len = strlen(orig_html);
1886 for (i = 0; i < orig_len; i++) {
1887 for (j = 0; j < nitems(esc); j++) {
1888 if (orig_html[i] != esc[j].c)
1890 len += strlen(esc[j].s) - 1 /* escaped char */;
1894 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1895 if (*escaped_html == NULL)
1896 return got_error_from_errno("calloc");
1899 for (i = 0; i < orig_len; i++) {
1901 for (j = 0; j < nitems(esc); j++) {
1902 if (orig_html[i] != esc[j].c)
1905 if (strlcat(*escaped_html, esc[j].s, len + 1)
1907 error = got_error(GOT_ERR_NO_SPACE);
1910 x += strlen(esc[j].s);
1915 (*escaped_html)[x] = orig_html[i];
1921 free(*escaped_html);
1922 *escaped_html = NULL;
1924 (*escaped_html)[x] = '\0';
1930 static const struct got_error *
1931 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1933 const struct got_error *error = NULL;
1934 struct socket *sock = c->sock;
1935 struct server *srv = c->srv;
1936 struct transport *t = c->t;
1941 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1942 GOTWEB_GIT_DIR) == -1)
1943 return got_error_from_errno("asprintf");
1945 dt = opendir(dir_test);
1949 repo_dir->path = strdup(dir_test);
1950 if (repo_dir->path == NULL) {
1952 error = got_error_from_errno("strdup");
1959 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1960 GOTWEB_GOT_DIR) == -1) {
1962 error = got_error_from_errno("asprintf");
1966 dt = opendir(dir_test);
1971 error = got_error(GOT_ERR_NOT_GIT_REPO);
1975 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1976 repo_dir->name) == -1) {
1977 error = got_error_from_errno("asprintf");
1982 repo_dir->path = strdup(dir_test);
1983 if (repo_dir->path == NULL) {
1985 error = got_error_from_errno("strdup");
1989 dt = opendir(dir_test);
1991 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1996 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1999 error = gotweb_get_repo_description(&repo_dir->description, srv,
2003 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
2006 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
2010 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2014 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2015 error = got_error_from_errno("closedir");
2019 static const struct got_error *
2020 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2022 const struct got_error *error;
2024 *repo_dir = calloc(1, sizeof(**repo_dir));
2025 if (*repo_dir == NULL)
2026 return got_error_from_errno("calloc");
2028 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2029 error = got_error_from_errno("asprintf");
2034 (*repo_dir)->owner = NULL;
2035 (*repo_dir)->description = NULL;
2036 (*repo_dir)->url = NULL;
2037 (*repo_dir)->age = NULL;
2038 (*repo_dir)->path = NULL;
2043 static const struct got_error *
2044 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2046 const struct got_error *error = NULL;
2048 char *d_file = NULL;
2052 *description = NULL;
2053 if (srv->show_repo_description == 0)
2056 if (asprintf(&d_file, "%s/description", dir) == -1)
2057 return got_error_from_errno("asprintf");
2059 f = fopen(d_file, "r");
2061 if (errno == ENOENT || errno == EACCES)
2063 error = got_error_from_errno2("fopen", d_file);
2067 if (fseek(f, 0, SEEK_END) == -1) {
2068 error = got_ferror(f, GOT_ERR_IO);
2073 error = got_ferror(f, GOT_ERR_IO);
2080 if (fseek(f, 0, SEEK_SET) == -1) {
2081 error = got_ferror(f, GOT_ERR_IO);
2084 *description = calloc(len + 1, sizeof(**description));
2085 if (*description == NULL) {
2086 error = got_error_from_errno("calloc");
2090 n = fread(*description, 1, len, f);
2091 if (n == 0 && ferror(f))
2092 error = got_ferror(f, GOT_ERR_IO);
2094 if (f != NULL && fclose(f) == EOF && error == NULL)
2095 error = got_error_from_errno("fclose");
2100 static const struct got_error *
2101 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2103 const struct got_error *error = NULL;
2105 char *d_file = NULL;
2111 if (srv->show_repo_cloneurl == 0)
2114 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2115 return got_error_from_errno("asprintf");
2117 f = fopen(d_file, "r");
2119 if (errno != ENOENT && errno != EACCES)
2120 error = got_error_from_errno2("fopen", d_file);
2124 if (fseek(f, 0, SEEK_END) == -1) {
2125 error = got_ferror(f, GOT_ERR_IO);
2130 error = got_ferror(f, GOT_ERR_IO);
2136 if (fseek(f, 0, SEEK_SET) == -1) {
2137 error = got_ferror(f, GOT_ERR_IO);
2141 *url = calloc(len + 1, sizeof(**url));
2143 error = got_error_from_errno("calloc");
2147 n = fread(*url, 1, len, f);
2148 if (n == 0 && ferror(f))
2149 error = got_ferror(f, GOT_ERR_IO);
2151 if (f != NULL && fclose(f) == EOF && error == NULL)
2152 error = got_error_from_errno("fclose");
2157 const struct got_error *
2158 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2161 long long diff_time;
2162 const char *years = "years ago", *months = "months ago";
2163 const char *weeks = "weeks ago", *days = "days ago";
2164 const char *hours = "hours ago", *minutes = "minutes ago";
2165 const char *seconds = "seconds ago", *now = "right now";
2173 diff_time = time(NULL) - committer_time;
2174 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2175 if (asprintf(repo_age, "%lld %s",
2176 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2177 return got_error_from_errno("asprintf");
2178 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2179 if (asprintf(repo_age, "%lld %s",
2180 (diff_time / 60 / 60 / 24 / (365 / 12)),
2182 return got_error_from_errno("asprintf");
2183 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2184 if (asprintf(repo_age, "%lld %s",
2185 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2186 return got_error_from_errno("asprintf");
2187 } else if (diff_time > 60 * 60 * 24 * 2) {
2188 if (asprintf(repo_age, "%lld %s",
2189 (diff_time / 60 / 60 / 24), days) == -1)
2190 return got_error_from_errno("asprintf");
2191 } else if (diff_time > 60 * 60 * 2) {
2192 if (asprintf(repo_age, "%lld %s",
2193 (diff_time / 60 / 60), hours) == -1)
2194 return got_error_from_errno("asprintf");
2195 } else if (diff_time > 60 * 2) {
2196 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2198 return got_error_from_errno("asprintf");
2199 } else if (diff_time > 2) {
2200 if (asprintf(repo_age, "%lld %s", diff_time,
2202 return got_error_from_errno("asprintf");
2204 if (asprintf(repo_age, "%s", now) == -1)
2205 return got_error_from_errno("asprintf");
2209 if (gmtime_r(&committer_time, &tm) == NULL)
2210 return got_error_from_errno("gmtime_r");
2212 s = asctime_r(&tm, datebuf);
2214 return got_error_from_errno("asctime_r");
2216 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2217 return got_error_from_errno("asprintf");