Blob


1 /*
2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <sys/queue.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <imsg.h>
32 #include <sha1.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_diff.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
50 #include "proc.h"
51 #include "gotwebd.h"
53 enum gotweb_ref_tm {
54 TM_DIFF,
55 TM_LONG,
56 };
58 static const struct querystring_keys querystring_keys[] = {
59 { "action", ACTION },
60 { "commit", COMMIT },
61 { "file", RFILE },
62 { "folder", FOLDER },
63 { "headref", HEADREF },
64 { "index_page", INDEX_PAGE },
65 { "path", PATH },
66 { "page", PAGE },
67 };
69 static const struct action_keys action_keys[] = {
70 { "blame", BLAME },
71 { "blob", BLOB },
72 { "briefs", BRIEFS },
73 { "commits", COMMITS },
74 { "diff", DIFF },
75 { "error", ERR },
76 { "index", INDEX },
77 { "summary", SUMMARY },
78 { "tag", TAG },
79 { "tags", TAGS },
80 { "tree", TREE },
81 };
83 static const struct got_error *gotweb_init_querystring(struct querystring **);
84 static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 char *);
86 static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 char *, char *);
88 static const struct got_error *gotweb_render_header(struct request *);
89 static const struct got_error *gotweb_render_footer(struct request *);
90 static const struct got_error *gotweb_render_index(struct request *);
91 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
92 const char *);
93 static const struct got_error *gotweb_load_got_path(struct request *c,
94 struct repo_dir *);
95 static const struct got_error *gotweb_get_repo_description(char **,
96 struct server *, char *);
97 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
98 char *);
99 static const struct got_error *gotweb_render_navs(struct request *);
100 static const struct got_error *gotweb_render_blame(struct request *);
101 static const struct got_error *gotweb_render_briefs(struct request *);
102 static const struct got_error *gotweb_render_commits(struct request *);
103 static const struct got_error *gotweb_render_diff(struct request *);
104 static const struct got_error *gotweb_render_summary(struct request *);
105 static const struct got_error *gotweb_render_tag(struct request *);
106 static const struct got_error *gotweb_render_tags(struct request *);
107 static const struct got_error *gotweb_render_tree(struct request *);
108 static const struct got_error *gotweb_render_branches(struct request *);
110 static void gotweb_free_querystring(struct querystring *);
111 static void gotweb_free_repo_dir(struct repo_dir *);
113 struct server *gotweb_get_server(uint8_t *, uint8_t *);
115 void
116 gotweb_process_request(struct request *c)
118 const struct got_error *error = NULL, *error2 = NULL;
119 struct server *srv = NULL;
120 struct querystring *qs = NULL;
121 struct repo_dir *repo_dir = NULL;
122 uint8_t err[] = "gotwebd experienced an error: ";
123 int r, html = 0;
125 /* init the transport */
126 error = gotweb_init_transport(&c->t);
127 if (error) {
128 log_warnx("%s: %s", __func__, error->msg);
129 return;
131 /* don't process any further if client disconnected */
132 if (c->sock->client_status == CLIENT_DISCONNECT)
133 return;
134 /* get the gotwebd server */
135 srv = gotweb_get_server(c->server_name, c->http_host);
136 if (srv == NULL) {
137 log_warnx("%s: error server is NULL", __func__);
138 goto err;
140 c->srv = srv;
141 /* parse our querystring */
142 error = gotweb_init_querystring(&qs);
143 if (error) {
144 log_warnx("%s: %s", __func__, error->msg);
145 goto err;
147 c->t->qs = qs;
148 error = gotweb_parse_querystring(&qs, c->querystring);
149 if (error) {
150 log_warnx("%s: %s", __func__, error->msg);
151 goto err;
154 /*
155 * certain actions require a commit id in the querystring. this stops
156 * bad actors from exploiting this by manually manipulating the
157 * querystring.
158 */
160 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
161 qs->action == DIFF)) {
162 error2 = got_error(GOT_ERR_QUERYSTRING);
163 goto render;
166 if (qs->action != INDEX) {
167 error = gotweb_init_repo_dir(&repo_dir, qs->path);
168 if (error)
169 goto done;
170 error = gotweb_load_got_path(c, repo_dir);
171 c->t->repo_dir = repo_dir;
172 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
173 goto err;
176 /* render top of page */
177 if (qs != NULL && qs->action == BLOB) {
178 error = got_get_repo_commits(c, 1);
179 if (error)
180 goto done;
181 error = got_output_file_blob(c);
182 if (error) {
183 log_warnx("%s: %s", __func__, error->msg);
184 goto err;
186 goto done;
187 } else {
188 render:
189 error = gotweb_render_content_type(c, "text/html");
190 if (error) {
191 log_warnx("%s: %s", __func__, error->msg);
192 goto err;
194 html = 1;
197 error = gotweb_render_header(c);
198 if (error) {
199 log_warnx("%s: %s", __func__, error->msg);
200 goto err;
203 if (error2) {
204 error = error2;
205 goto err;
208 switch(qs->action) {
209 case BLAME:
210 error = gotweb_render_blame(c);
211 if (error) {
212 log_warnx("%s: %s", __func__, error->msg);
213 goto err;
215 break;
216 case BRIEFS:
217 error = gotweb_render_briefs(c);
218 if (error) {
219 log_warnx("%s: %s", __func__, error->msg);
220 goto err;
222 break;
223 case COMMITS:
224 error = gotweb_render_commits(c);
225 if (error) {
226 log_warnx("%s: %s", __func__, error->msg);
227 goto err;
229 break;
230 case DIFF:
231 error = gotweb_render_diff(c);
232 if (error) {
233 log_warnx("%s: %s", __func__, error->msg);
234 goto err;
236 break;
237 case INDEX:
238 error = gotweb_render_index(c);
239 if (error) {
240 log_warnx("%s: %s", __func__, error->msg);
241 goto err;
243 break;
244 case SUMMARY:
245 error = gotweb_render_summary(c);
246 if (error) {
247 log_warnx("%s: %s", __func__, error->msg);
248 goto err;
250 break;
251 case TAG:
252 error = gotweb_render_tag(c);
253 if (error) {
254 log_warnx("%s: %s", __func__, error->msg);
255 goto err;
257 break;
258 case TAGS:
259 error = gotweb_render_tags(c);
260 if (error) {
261 log_warnx("%s: %s", __func__, error->msg);
262 goto err;
264 break;
265 case TREE:
266 error = gotweb_render_tree(c);
267 if (error) {
268 log_warnx("%s: %s", __func__, error->msg);
269 goto err;
271 break;
272 case ERR:
273 default:
274 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
275 "Erorr: Bad Querystring");
276 if (r == -1)
277 goto err;
278 break;
281 goto done;
282 err:
283 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
284 return;
285 if (fcgi_printf(c, "%s", err) == -1)
286 return;
287 if (error) {
288 if (fcgi_printf(c, "%s", error->msg) == -1)
289 return;
290 } else {
291 if (fcgi_printf(c, "see daemon logs for details") == -1)
292 return;
294 if (html && fcgi_printf(c, "</div>\n") == -1)
295 return;
296 done:
297 if (html && srv != NULL)
298 gotweb_render_footer(c);
301 struct server *
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)
310 goto done;
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)
316 goto done;
318 /* if those fail, send first server */
319 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
320 if (srv != NULL)
321 break;
322 done:
323 return srv;
324 };
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));
332 if (*t == NULL)
333 return got_error_from_errno2("%s: calloc", __func__);
335 TAILQ_INIT(&(*t)->repo_commits);
336 TAILQ_INIT(&(*t)->repo_tags);
338 (*t)->repo = NULL;
339 (*t)->repo_dir = NULL;
340 (*t)->qs = NULL;
341 (*t)->next_id = NULL;
342 (*t)->prev_id = NULL;
343 (*t)->next_disp = 0;
344 (*t)->prev_disp = 0;
346 return error;
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));
355 if (*qs == NULL)
356 return got_error_from_errno2("%s: calloc", __func__);
358 (*qs)->headref = strdup("HEAD");
359 if ((*qs)->headref == NULL) {
360 free(*qs);
361 *qs = NULL;
362 return got_error_from_errno2("%s: strdup", __func__);
365 (*qs)->action = INDEX;
366 (*qs)->commit = NULL;
367 (*qs)->file = NULL;
368 (*qs)->folder = NULL;
369 (*qs)->index_page = 0;
370 (*qs)->index_page_str = NULL;
371 (*qs)->path = NULL;
373 return error;
376 static const struct got_error *
377 gotweb_parse_querystring(struct querystring **qs, char *qst)
379 const struct got_error *error = NULL;
380 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
381 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
383 if (qst == NULL)
384 return error;
386 tok1 = strdup(qst);
387 if (tok1 == NULL)
388 return got_error_from_errno2("%s: strdup", __func__);
390 tok1_pair = tok1;
391 tok1_end = tok1;
393 while (tok1_pair != NULL) {
394 strsep(&tok1_end, "&");
396 tok2 = strdup(tok1_pair);
397 if (tok2 == NULL) {
398 free(tok1);
399 return got_error_from_errno2("%s: strdup", __func__);
402 tok2_pair = tok2;
403 tok2_end = tok2;
405 while (tok2_pair != NULL) {
406 strsep(&tok2_end, "=");
407 if (tok2_end) {
408 error = gotweb_assign_querystring(qs, tok2_pair,
409 tok2_end);
410 if (error)
411 goto err;
413 tok2_pair = tok2_end;
415 free(tok2);
416 tok1_pair = tok1_end;
418 free(tok1);
419 return error;
420 err:
421 free(tok2);
422 free(tok1);
423 return error;
426 /*
427 * Adapted from usr.sbin/httpd/httpd.c url_decode.
428 */
429 static const struct got_error *
430 gotweb_urldecode(char *url)
432 char *p, *q;
433 char hex[3];
434 unsigned long x;
436 hex[2] = '\0';
437 p = q = url;
439 while (*p != '\0') {
440 switch (*p) {
441 case '%':
442 /* Encoding character is followed by two hex chars */
443 if (!isxdigit((unsigned char)p[1]) ||
444 !isxdigit((unsigned char)p[2]) ||
445 (p[1] == '0' && p[2] == '0'))
446 return got_error(GOT_ERR_BAD_QUERYSTRING);
448 hex[0] = p[1];
449 hex[1] = p[2];
451 /*
452 * We don't have to validate "hex" because it is
453 * guaranteed to include two hex chars followed by nul.
454 */
455 x = strtoul(hex, NULL, 16);
456 *q = (char)x;
457 p += 2;
458 break;
459 default:
460 *q = *p;
461 break;
463 p++;
464 q++;
466 *q = '\0';
468 return NULL;
471 static const struct got_error *
472 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
474 const struct got_error *error = NULL;
475 const char *errstr;
476 int a_cnt, el_cnt;
478 error = gotweb_urldecode(value);
479 if (error)
480 return error;
482 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
483 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
484 continue;
486 switch (querystring_keys[el_cnt].element) {
487 case ACTION:
488 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
489 if (strcmp(value, action_keys[a_cnt].name) != 0)
490 continue;
491 else if (strcmp(value,
492 action_keys[a_cnt].name) == 0){
493 (*qs)->action =
494 action_keys[a_cnt].action;
495 goto qa_found;
498 (*qs)->action = ERR;
499 qa_found:
500 break;
501 case COMMIT:
502 (*qs)->commit = strdup(value);
503 if ((*qs)->commit == NULL) {
504 error = got_error_from_errno2("%s: strdup",
505 __func__);
506 goto done;
508 break;
509 case RFILE:
510 (*qs)->file = strdup(value);
511 if ((*qs)->file == NULL) {
512 error = got_error_from_errno2("%s: strdup",
513 __func__);
514 goto done;
516 break;
517 case FOLDER:
518 (*qs)->folder = strdup(value);
519 if ((*qs)->folder == NULL) {
520 error = got_error_from_errno2("%s: strdup",
521 __func__);
522 goto done;
524 break;
525 case HEADREF:
526 free((*qs)->headref);
527 (*qs)->headref = strdup(value);
528 if ((*qs)->headref == NULL) {
529 error = got_error_from_errno2("%s: strdup",
530 __func__);
531 goto done;
533 break;
534 case INDEX_PAGE:
535 if (strlen(value) == 0)
536 break;
537 (*qs)->index_page_str = strdup(value);
538 if ((*qs)->index_page_str == NULL) {
539 error = got_error_from_errno2("%s: strdup",
540 __func__);
541 goto done;
543 (*qs)->index_page = strtonum(value, INT64_MIN,
544 INT64_MAX, &errstr);
545 if (errstr) {
546 error = got_error_from_errno3("%s: strtonum %s",
547 __func__, errstr);
548 goto done;
550 if ((*qs)->index_page < 0) {
551 (*qs)->index_page = 0;
552 sprintf((*qs)->index_page_str, "%d", 0);
554 break;
555 case PATH:
556 (*qs)->path = strdup(value);
557 if ((*qs)->path == NULL) {
558 error = got_error_from_errno2("%s: strdup",
559 __func__);
560 goto done;
562 break;
563 case PAGE:
564 if (strlen(value) == 0)
565 break;
566 (*qs)->page_str = strdup(value);
567 if ((*qs)->page_str == NULL) {
568 error = got_error_from_errno2("%s: strdup",
569 __func__);
570 goto done;
572 (*qs)->page = strtonum(value, INT64_MIN,
573 INT64_MAX, &errstr);
574 if (errstr) {
575 error = got_error_from_errno3("%s: strtonum %s",
576 __func__, errstr);
577 goto done;
579 if ((*qs)->page < 0) {
580 (*qs)->page = 0;
581 sprintf((*qs)->page_str, "%d", 0);
583 break;
584 default:
585 break;
588 done:
589 return error;
592 void
593 gotweb_free_repo_tag(struct repo_tag *rt)
595 if (rt != NULL) {
596 free(rt->commit_id);
597 free(rt->tag_name);
598 free(rt->tag_commit);
599 free(rt->commit_msg);
600 free(rt->tagger);
602 free(rt);
605 void
606 gotweb_free_repo_commit(struct repo_commit *rc)
608 if (rc != NULL) {
609 free(rc->path);
610 free(rc->refs_str);
611 free(rc->commit_id);
612 free(rc->parent_id);
613 free(rc->tree_id);
614 free(rc->author);
615 free(rc->committer);
616 free(rc->commit_msg);
618 free(rc);
621 static void
622 gotweb_free_querystring(struct querystring *qs)
624 if (qs != NULL) {
625 free(qs->commit);
626 free(qs->file);
627 free(qs->folder);
628 free(qs->headref);
629 free(qs->index_page_str);
630 free(qs->path);
631 free(qs->page_str);
633 free(qs);
636 static void
637 gotweb_free_repo_dir(struct repo_dir *repo_dir)
639 if (repo_dir != NULL) {
640 free(repo_dir->name);
641 free(repo_dir->owner);
642 free(repo_dir->description);
643 free(repo_dir->url);
644 free(repo_dir->age);
645 free(repo_dir->path);
647 free(repo_dir);
650 void
651 gotweb_free_transport(struct transport *t)
653 struct repo_commit *rc = NULL, *trc = NULL;
654 struct repo_tag *rt = NULL, *trt = NULL;
656 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
657 TAILQ_REMOVE(&t->repo_commits, rc, entry);
658 gotweb_free_repo_commit(rc);
660 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
661 TAILQ_REMOVE(&t->repo_tags, rt, entry);
662 gotweb_free_repo_tag(rt);
664 gotweb_free_repo_dir(t->repo_dir);
665 gotweb_free_querystring(t->qs);
666 free(t->next_id);
667 free(t->prev_id);
668 free(t);
671 const struct got_error *
672 gotweb_render_content_type(struct request *c, const uint8_t *type)
674 const char *csp = "default-src 'self'; script-src 'none'; "
675 "object-src 'none';";
677 fcgi_printf(c,
678 "Content-Security-Policy: %s\r\n"
679 "Content-Type: %s\r\n\r\n",
680 csp, type);
681 return NULL;
684 const struct got_error *
685 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
686 char *file)
688 fcgi_printf(c, "Content-type: %s\r\n"
689 "Content-disposition: attachment; filename=%s\r\n\r\n",
690 type, file);
691 return NULL;
694 static const struct got_error *
695 gotweb_render_header(struct request *c)
697 struct server *srv = c->srv;
698 struct querystring *qs = c->t->qs;
699 int r;
701 r = fcgi_printf(c, "<!doctype html>\n"
702 "<html>\n"
703 "<head>\n"
704 "<title>%s</title>\n"
705 "<meta charset='utf-8' />\n"
706 "<meta name='viewport' content='initial-scale=.75' />\n"
707 "<meta name='msapplication-TileColor' content='#da532c' />\n"
708 "<meta name='theme-color' content='#ffffff'/>\n"
709 "<link rel='apple-touch-icon' sizes='180x180'"
710 " href='%sapple-touch-icon.png' />\n"
711 "<link rel='icon' type='image/png' sizes='32x32'"
712 " href='%sfavicon-32x32.png' />\n"
713 "<link rel='icon' type='image/png' sizes='16x16'"
714 " href='%sfavicon-16x16.png' />\n"
715 "<link rel='manifest' href='%ssite.webmanifest'/>\n"
716 "<link rel='mask-icon' href='%ssafari-pinned-tab.svg' />\n"
717 "<link rel='stylesheet' type='text/css' href='%s%s' />\n"
718 "</head>\n"
719 "<body>\n"
720 "<div id='gw_body'>\n"
721 "<div id='header'>\n"
722 "<div id='got_link'>"
723 "<a href='%s' target='_blank'>"
724 "<img src='%s%s' alt='logo' id='logo' />"
725 "</a>\n"
726 "</div>\n" /* #got_link */
727 "</div>\n" /* #header */
728 "<div id='site_path'>\n"
729 "<div id='site_link'>\n"
730 "<a href='?index_page=%d'>%s</a>",
731 srv->site_name,
732 c->script_name,
733 c->script_name,
734 c->script_name,
735 c->script_name,
736 c->script_name,
737 c->script_name, srv->custom_css,
738 srv->logo_url,
739 c->script_name, srv->logo,
740 qs->index_page, srv->site_link);
741 if (r == -1)
742 goto done;
744 if (qs != NULL) {
745 if (qs->path != NULL) {
746 r = fcgi_printf(c, " / "
747 "<a href='?index_page=%d&path=%s&action=summary'>"
748 "%s</a>",
749 qs->index_page, qs->path, qs->path);
750 if (r == -1)
751 goto done;
753 if (qs->action != INDEX) {
754 const char *action = "";
756 switch (qs->action) {
757 case BLAME:
758 action = "blame";
759 break;
760 case BRIEFS:
761 action = "briefs";
762 break;
763 case COMMITS:
764 action = "commits";
765 break;
766 case DIFF:
767 action = "diff";
768 break;
769 case SUMMARY:
770 action = "summary";
771 break;
772 case TAG:
773 action = "tag";
774 break;
775 case TAGS:
776 action = "tags";
777 break;
778 case TREE:
779 action = "tree";
780 break;
783 if (fcgi_printf(c, " / %s", action) == -1)
784 goto done;
788 fcgi_printf(c, "</div>\n" /* #site_path */
789 "</div>\n" /* #site_link */
790 "<div id='content'>\n");
792 done:
793 return NULL;
796 static const struct got_error *
797 gotweb_render_footer(struct request *c)
799 const struct got_error *error = NULL;
800 struct server *srv = c->srv;
801 const char *siteowner = "&nbsp;";
802 char *escaped_owner = NULL;
804 if (srv->show_site_owner) {
805 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
806 if (error)
807 return error;
808 siteowner = escaped_owner;
811 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
812 "<div id='site_owner'>%s</div>\n"
813 "</div>\n" /* #site_owner_wrapper */
814 "</div>\n" /* #content */
815 "</div>\n" /* #gw_body */
816 "</body>\n</html>\n", siteowner);
818 free(escaped_owner);
819 return NULL;
822 static const struct got_error *
823 gotweb_render_navs(struct request *c)
825 const struct got_error *error = NULL;
826 struct transport *t = c->t;
827 struct querystring *qs = t->qs;
828 struct server *srv = c->srv;
829 char *nhref = NULL, *phref = NULL;
830 int r, disp = 0;
832 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
833 if (r == -1)
834 goto done;
836 switch(qs->action) {
837 case INDEX:
838 if (qs->index_page > 0) {
839 if (asprintf(&phref, "index_page=%d",
840 qs->index_page - 1) == -1) {
841 error = got_error_from_errno2("%s: asprintf",
842 __func__);
843 goto done;
845 disp = 1;
847 break;
848 case BRIEFS:
849 if (t->prev_id && qs->commit != NULL &&
850 strcmp(qs->commit, t->prev_id) != 0) {
851 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
852 "&action=briefs&commit=%s&headref=%s",
853 qs->index_page, qs->path, qs->page - 1, t->prev_id,
854 qs->headref) == -1) {
855 error = got_error_from_errno2("%s: asprintf",
856 __func__);
857 goto done;
859 disp = 1;
861 break;
862 case COMMITS:
863 if (t->prev_id && qs->commit != NULL &&
864 strcmp(qs->commit, t->prev_id) != 0) {
865 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
866 "&action=commits&commit=%s&headref=%s&folder=%s"
867 "&file=%s",
868 qs->index_page, qs->path, qs->page - 1, t->prev_id,
869 qs->headref, qs->folder ? qs->folder : "",
870 qs->file ? qs->file : "") == -1) {
871 error = got_error_from_errno2("%s: asprintf",
872 __func__);
873 goto done;
875 disp = 1;
877 break;
878 case TAGS:
879 if (t->prev_id && qs->commit != NULL &&
880 strcmp(qs->commit, t->prev_id) != 0) {
881 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
882 "&action=tags&commit=%s&headref=%s",
883 qs->index_page, qs->path, qs->page - 1, t->prev_id,
884 qs->headref) == -1) {
885 error = got_error_from_errno2("%s: asprintf",
886 __func__);
887 goto done;
889 disp = 1;
891 break;
892 default:
893 disp = 0;
894 break;
897 if (disp) {
898 r = fcgi_printf(c, "<a href='?%s'>Previous</a>", phref);
899 if (r == -1)
900 goto done;
903 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
904 "<div id='nav_next'>");
905 if (r == -1)
906 goto done;
908 disp = 0;
909 switch(qs->action) {
910 case INDEX:
911 if (t->next_disp == srv->max_repos_display &&
912 t->repos_total != (qs->index_page + 1) *
913 srv->max_repos_display) {
914 if (asprintf(&nhref, "index_page=%d",
915 qs->index_page + 1) == -1) {
916 error = got_error_from_errno2("%s: asprintf",
917 __func__);
918 goto done;
920 disp = 1;
922 break;
923 case BRIEFS:
924 if (t->next_id) {
925 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
926 "&action=briefs&commit=%s&headref=%s",
927 qs->index_page, qs->path, qs->page + 1, t->next_id,
928 qs->headref) == -1) {
929 error = got_error_from_errno2("%s: asprintf",
930 __func__);
931 goto done;
933 disp = 1;
935 break;
936 case COMMITS:
937 if (t->next_id) {
938 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
939 "&action=commits&commit=%s&headref=%s&folder=%s"
940 "&file=%s",
941 qs->index_page, qs->path, qs->page + 1, t->next_id,
942 qs->headref, qs->folder ? qs->folder : "",
943 qs->file ? qs->file : "") == -1) {
944 error = got_error_from_errno2("%s: asprintf",
945 __func__);
946 goto done;
948 disp = 1;
950 break;
951 case TAGS:
952 if (t->next_id) {
953 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
954 "&action=tags&commit=%s&headref=%s",
955 qs->index_page, qs->path, qs->page + 1, t->next_id,
956 qs->headref) == -1) {
957 error = got_error_from_errno2("%s: asprintf",
958 __func__);
959 goto done;
961 disp = 1;
963 break;
964 default:
965 disp = 0;
966 break;
968 if (disp) {
969 r = fcgi_printf(c, "<a href='?%s'>Next</a>", nhref);
970 if (r == -1)
971 goto done;
973 fcgi_printf(c, "</div>\n"); /* #nav_next */
974 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
975 done:
976 free(t->next_id);
977 t->next_id = NULL;
978 free(t->prev_id);
979 t->prev_id = NULL;
980 free(phref);
981 free(nhref);
982 return error;
985 static const struct got_error *
986 gotweb_render_index(struct request *c)
988 const struct got_error *error = NULL;
989 struct server *srv = c->srv;
990 struct transport *t = c->t;
991 struct querystring *qs = t->qs;
992 struct repo_dir *repo_dir = NULL;
993 DIR *d;
994 struct dirent **sd_dent = NULL;
995 const char *index_page_str;
996 char *c_path = NULL;
997 struct stat st;
998 unsigned int d_cnt, d_i, d_disp = 0;
999 int r;
1001 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1003 d = opendir(srv->repos_path);
1004 if (d == NULL) {
1005 error = got_error_from_errno2("opendir", srv->repos_path);
1006 return error;
1009 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
1010 if (d_cnt == -1) {
1011 sd_dent = NULL;
1012 error = got_error_from_errno2("scandir", srv->repos_path);
1013 goto done;
1016 /* get total count of repos */
1017 for (d_i = 0; d_i < d_cnt; d_i++) {
1018 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1019 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1020 continue;
1022 if (asprintf(&c_path, "%s/%s", srv->repos_path,
1023 sd_dent[d_i]->d_name) == -1) {
1024 error = got_error_from_errno("asprintf");
1025 return error;
1028 if (lstat(c_path, &st) == 0 && S_ISDIR(st.st_mode) &&
1029 !got_path_dir_is_empty(c_path))
1030 t->repos_total++;
1031 free(c_path);
1032 c_path = NULL;
1035 r = fcgi_printf(c, "<div id='index_header'>\n"
1036 "<div id='index_header_project'>Project</div>\n");
1037 if (r == -1)
1038 goto done;
1040 if (srv->show_repo_description)
1041 if (fcgi_printf(c, "<div id='index_header_description'>"
1042 "Description</div>\n") == -1)
1043 goto done;
1044 if (srv->show_repo_owner)
1045 if (fcgi_printf(c, "<div id='index_header_owner'>"
1046 "Owner</div>\n") == -1)
1047 goto done;
1048 if (srv->show_repo_age)
1049 if (fcgi_printf(c, "<div id='index_header_age'>"
1050 "Last Change</div>\n") == -1)
1051 goto done;
1052 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1053 goto done;
1055 for (d_i = 0; d_i < d_cnt; d_i++) {
1056 if (srv->max_repos > 0 && (d_i - 2) == srv->max_repos)
1057 break; /* account for parent and self */
1059 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1060 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1061 continue;
1063 if (qs->index_page > 0 && (qs->index_page *
1064 srv->max_repos_display) > t->prev_disp) {
1065 t->prev_disp++;
1066 continue;
1069 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1070 if (error)
1071 goto done;
1073 error = gotweb_load_got_path(c, repo_dir);
1074 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1075 error = NULL;
1076 continue;
1078 else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1079 goto done;
1081 if (lstat(repo_dir->path, &st) == 0 &&
1082 S_ISDIR(st.st_mode) &&
1083 !got_path_dir_is_empty(repo_dir->path))
1084 goto render;
1085 else {
1086 gotweb_free_repo_dir(repo_dir);
1087 repo_dir = NULL;
1088 continue;
1090 render:
1091 d_disp++;
1092 t->prev_disp++;
1094 r = fcgi_printf(c, "<div class='index_wrapper'>\n"
1095 "<div class='index_project'>"
1096 "<a href='?index_page=%s&path=%s&action=summary'>"
1097 " %s "
1098 "</a>"
1099 "</div>", /* .index_project */
1100 index_page_str, repo_dir->name,
1101 repo_dir->name);
1102 if (r == -1)
1103 goto done;
1105 if (srv->show_repo_description) {
1106 r = fcgi_printf(c,
1107 "<div class='index_project_description'>\n"
1108 "%s</div>\n", repo_dir->description);
1109 if (r == -1)
1110 goto done;
1113 if (srv->show_repo_owner) {
1114 r = fcgi_printf(c, "<div class='index_project_owner'>"
1115 "%s</div>\n", repo_dir->owner);
1116 if (r == -1)
1117 goto done;
1120 if (srv->show_repo_age) {
1121 r = fcgi_printf(c, "<div class='index_project_age'>"
1122 "%s</div>\n", repo_dir->age);
1123 if (r == -1)
1124 goto done;
1127 r = fcgi_printf(c, "<div class='navs_wrapper'>"
1128 "<div class='navs'>"
1129 "<a href='?index_page=%s&path=%s&action=summary'>"
1130 "summary"
1131 "</a> | "
1132 "<a href='?index_page=%s&path=%s&action=briefs'>"
1133 "commit briefs"
1134 "</a> | "
1135 "<a href='?index_page=%s&path=%s&action=commits'>"
1136 "commits"
1137 "</a> | "
1138 "<a href='?index_page=%s&path=%s&action=tags'>"
1139 "tags"
1140 "</a> | "
1141 "<a href='?index_page=%s&path=%s&action=tree'>"
1142 "tree"
1143 "</a>"
1144 "</div>" /* .navs */
1145 "<div class='dotted_line'></div>\n"
1146 "</div>\n" /* .navs_wrapper */
1147 "</div>\n", /* .index_wrapper */
1148 index_page_str, repo_dir->name,
1149 index_page_str, repo_dir->name,
1150 index_page_str, repo_dir->name,
1151 index_page_str, repo_dir->name,
1152 index_page_str, repo_dir->name);
1153 if (r == -1)
1154 goto done;
1156 gotweb_free_repo_dir(repo_dir);
1157 repo_dir = NULL;
1158 t->next_disp++;
1159 if (d_disp == srv->max_repos_display)
1160 break;
1162 if (srv->max_repos_display == 0)
1163 goto done;
1164 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1165 goto done;
1166 if (t->repos_total <= srv->max_repos ||
1167 t->repos_total <= srv->max_repos_display)
1168 goto done;
1170 error = gotweb_render_navs(c);
1171 if (error)
1172 goto done;
1173 done:
1174 if (sd_dent) {
1175 for (d_i = 0; d_i < d_cnt; d_i++)
1176 free(sd_dent[d_i]);
1177 free(sd_dent);
1179 if (d != NULL && closedir(d) == EOF && error == NULL)
1180 error = got_error_from_errno("closedir");
1181 return error;
1184 static const struct got_error *
1185 gotweb_render_blame(struct request *c)
1187 const struct got_error *error = NULL;
1188 struct transport *t = c->t;
1189 struct repo_commit *rc = NULL;
1190 char *age = NULL, *msg = NULL;
1191 int r;
1193 error = got_get_repo_commits(c, 1);
1194 if (error)
1195 return error;
1197 rc = TAILQ_FIRST(&t->repo_commits);
1199 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1200 if (error)
1201 goto done;
1202 error = gotweb_escape_html(&msg, rc->commit_msg);
1203 if (error)
1204 goto done;
1206 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1207 "<div id='blame_title'>Blame</div>\n"
1208 "</div>\n" /* #blame_title_wrapper */
1209 "<div id='blame_content'>\n"
1210 "<div id='blame_header_wrapper'>\n"
1211 "<div id='blame_header'>\n"
1212 "<div class='header_age_title'>Date:</div>\n"
1213 "<div class='header_age'>%s</div>\n"
1214 "<div id='header_commit_msg_title'>Message:</div>\n"
1215 "<div id='header_commit_msg'>%s</div>\n"
1216 "</div>\n" /* #blame_header */
1217 "</div>\n" /* #blame_header_wrapper */
1218 "<div class='dotted_line'></div>\n"
1219 "<div id='blame'>\n",
1220 age,
1221 msg);
1222 if (r == -1)
1223 goto done;
1225 error = got_output_file_blame(c);
1226 if (error)
1227 goto done;
1229 fcgi_printf(c, "</div>\n" /* #blame */
1230 "</div>\n"); /* #blame_content */
1231 done:
1232 free(age);
1233 free(msg);
1234 return error;
1237 static const struct got_error *
1238 gotweb_render_briefs(struct request *c)
1240 const struct got_error *error = NULL;
1241 struct repo_commit *rc = NULL;
1242 struct server *srv = c->srv;
1243 struct transport *t = c->t;
1244 struct querystring *qs = t->qs;
1245 struct repo_dir *repo_dir = t->repo_dir;
1246 const char *index_page_str;
1247 char *smallerthan, *newline;
1248 char *age = NULL, *author = NULL, *msg = NULL;
1249 int r;
1251 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1253 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1254 "<div id='briefs_title'>Commit Briefs</div>\n"
1255 "</div>\n" /* #briefs_title_wrapper */
1256 "<div id='briefs_content'>\n");
1257 if (r == -1)
1258 goto done;
1260 if (qs->action == SUMMARY) {
1261 qs->action = BRIEFS;
1262 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1263 } else
1264 error = got_get_repo_commits(c, srv->max_commits_display);
1265 if (error)
1266 goto done;
1268 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1269 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1270 if (error)
1271 goto done;
1273 smallerthan = strchr(rc->author, '<');
1274 if (smallerthan)
1275 *smallerthan = '\0';
1277 newline = strchr(rc->commit_msg, '\n');
1278 if (newline)
1279 *newline = '\0';
1281 error = gotweb_escape_html(&author, rc->author);
1282 if (error)
1283 goto done;
1284 error = gotweb_escape_html(&msg, rc->commit_msg);
1285 if (error)
1286 goto done;
1288 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1289 "<div class='briefs_author'>%s</div>\n"
1290 "<div class='briefs_log'>"
1291 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1292 "&headref=%s'>%s</a>",
1293 age,
1294 author,
1295 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1296 msg);
1297 if (r == -1)
1298 goto done;
1300 if (rc->refs_str) {
1301 char *refs;
1303 error = gotweb_escape_html(&refs, rc->refs_str);
1304 if (error)
1305 goto done;
1306 r = fcgi_printf(c,
1307 " <span class='refs_str'>(%s)</span>", refs);
1308 free(refs);
1309 if (r == -1)
1310 goto done;
1312 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1313 goto done;
1315 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1316 "<div class='navs'>"
1317 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1318 "&headref=%s'>diff</a>"
1319 " | "
1320 "<a href='?index_page=%s&path=%s&action=tree&commit=%s"
1321 "&headref=%s'>tree</a>"
1322 "</div>\n" /* .navs */
1323 "</div>\n" /* .navs_wrapper */
1324 "<div class='dotted_line'></div>\n",
1325 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1326 index_page_str, repo_dir->name, rc->commit_id, qs->headref);
1327 if (r == -1)
1328 goto done;
1330 free(age);
1331 age = NULL;
1332 free(author);
1333 author = NULL;
1334 free(msg);
1335 msg = NULL;
1338 if (t->next_id || t->prev_id) {
1339 error = gotweb_render_navs(c);
1340 if (error)
1341 goto done;
1343 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1344 done:
1345 free(age);
1346 free(author);
1347 free(msg);
1348 return error;
1351 static const struct got_error *
1352 gotweb_render_commits(struct request *c)
1354 const struct got_error *error = NULL;
1355 struct repo_commit *rc = NULL;
1356 struct server *srv = c->srv;
1357 struct transport *t = c->t;
1358 struct querystring *qs = t->qs;
1359 struct repo_dir *repo_dir = t->repo_dir;
1360 const char *index_page_str;
1361 char *age = NULL, *author = NULL, *msg = NULL;
1362 int r;
1364 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1366 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1367 "<div class='commits_title'>Commits</div>\n"
1368 "</div>\n" /* .commits_title_wrapper */
1369 "<div class='commits_content'>\n");
1370 if (r == -1)
1371 goto done;
1373 error = got_get_repo_commits(c, srv->max_commits_display);
1374 if (error)
1375 goto done;
1377 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1378 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1379 if (error)
1380 goto done;
1381 error = gotweb_escape_html(&author, rc->author);
1382 if (error)
1383 goto done;
1384 error = gotweb_escape_html(&msg, rc->commit_msg);
1385 if (error)
1386 goto done;
1388 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1389 "<div class='commits_header'>\n"
1390 "<div class='header_commit_title'>Commit:</div>\n"
1391 "<div class='header_commit'>%s</div>\n"
1392 "<div class='header_author_title'>Author:</div>\n"
1393 "<div class='header_author'>%s</div>\n"
1394 "<div class='header_age_title'>Date:</div>\n"
1395 "<div class='header_age'>%s</div>\n"
1396 "</div>\n" /* .commits_header */
1397 "</div>\n" /* .commits_header_wrapper */
1398 "<div class='dotted_line'></div>\n"
1399 "<div class='commit'>\n%s</div>\n",
1400 rc->commit_id,
1401 author,
1402 age,
1403 msg);
1404 if (r == -1)
1405 goto done;
1407 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1408 "<div class='navs'>"
1409 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1410 "diff</a>"
1411 " | "
1412 "<a href='?index_page=%s&path=%s&action=tree&commit=%s'>"
1413 "tree</a>"
1414 "</div>\n" /* .navs */
1415 "</div>\n" /* .navs_wrapper */
1416 "<div class='dotted_line'></div>\n",
1417 index_page_str, repo_dir->name, rc->commit_id,
1418 index_page_str, repo_dir->name, rc->commit_id);
1419 if (r == -1)
1420 goto done;
1422 free(age);
1423 age = NULL;
1424 free(author);
1425 author = NULL;
1426 free(msg);
1427 msg = NULL;
1430 if (t->next_id || t->prev_id) {
1431 error = gotweb_render_navs(c);
1432 if (error)
1433 goto done;
1435 fcgi_printf(c, "</div>\n"); /* .commits_content */
1436 done:
1437 free(age);
1438 free(author);
1439 free(msg);
1440 return error;
1443 static const struct got_error *
1444 gotweb_render_branches(struct request *c)
1446 const struct got_error *error = NULL;
1447 struct got_reflist_head refs;
1448 struct got_reflist_entry *re;
1449 struct transport *t = c->t;
1450 struct querystring *qs = t->qs;
1451 struct got_repository *repo = t->repo;
1452 const char *index_page_str;
1453 char *age = NULL;
1454 int r;
1456 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1458 TAILQ_INIT(&refs);
1460 error = got_ref_list(&refs, repo, "refs/heads",
1461 got_ref_cmp_by_name, NULL);
1462 if (error)
1463 goto done;
1465 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1466 "<div id='branches_title'>Branches</div>\n"
1467 "</div>\n" /* #branches_title_wrapper */
1468 "<div id='branches_content'>\n");
1469 if (r == -1)
1470 goto done;
1472 TAILQ_FOREACH(re, &refs, entry) {
1473 const char *refname = NULL;
1474 char *escaped_refname = NULL;
1476 if (got_ref_is_symbolic(re->ref))
1477 continue;
1479 refname = got_ref_get_name(re->ref);
1480 if (refname == NULL) {
1481 error = got_error_from_errno("strdup");
1482 goto done;
1484 if (strncmp(refname, "refs/heads/", 11) != 0)
1485 continue;
1487 error = got_get_repo_age(&age, c, qs->path, refname,
1488 TM_DIFF);
1489 if (error)
1490 goto done;
1492 if (strncmp(refname, "refs/heads/", 11) == 0)
1493 refname += 11;
1494 error = gotweb_escape_html(&escaped_refname, refname);
1495 if (error)
1496 goto done;
1498 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1499 "<div class='branches_age'>%s</div>\n"
1500 "<div class='branches_space'>&nbsp;</div>\n"
1501 "<div class='branch'>"
1502 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1503 "%s</a>"
1504 "</div>\n" /* .branch */
1505 "<div class='navs_wrapper'>\n"
1506 "<div class='navs'>"
1507 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1508 "summary</a>"
1509 " | "
1510 "<a href='?index_page=%s&path=%s&action=briefs&headref=%s'>"
1511 "commit briefs</a>"
1512 " | "
1513 "<a href='?index_page=%s&path=%s&action=commits&headref=%s'>"
1514 "commits</a>"
1515 "</div>\n" /* .navs */
1516 "</div>\n" /* .navs_wrapper */
1517 "<div class='dotted_line'></div>\n"
1518 "</div>\n", /* .branches_wrapper */
1519 age ? age : "",
1520 index_page_str, qs->path, refname,
1521 escaped_refname,
1522 index_page_str, qs->path, refname,
1523 index_page_str, qs->path, refname,
1524 index_page_str, qs->path, refname);
1525 free(escaped_refname);
1526 if (r == -1)
1527 goto done;
1529 free(age);
1530 age = NULL;
1533 fcgi_printf(c, "</div>\n"); /* #branches_content */
1534 done:
1535 free(age);
1536 got_ref_list_free(&refs);
1537 return error;
1540 static const struct got_error *
1541 gotweb_render_tree(struct request *c)
1543 const struct got_error *error = NULL;
1544 struct transport *t = c->t;
1545 struct repo_commit *rc = NULL;
1546 char *age = NULL, *msg = NULL;
1547 int r;
1549 error = got_get_repo_commits(c, 1);
1550 if (error)
1551 return error;
1553 rc = TAILQ_FIRST(&t->repo_commits);
1555 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1556 if (error)
1557 goto done;
1559 error = gotweb_escape_html(&msg, rc->commit_msg);
1560 if (error)
1561 goto done;
1563 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1564 "<div id='tree_title'>Tree</div>\n"
1565 "</div>\n" /* #tree_title_wrapper */
1566 "<div id='tree_content'>\n"
1567 "<div id='tree_header_wrapper'>\n"
1568 "<div id='tree_header'>\n"
1569 "<div id='header_tree_title'>Tree:</div>\n"
1570 "<div id='header_tree'>%s</div>\n"
1571 "<div class='header_age_title'>Date:</div>\n"
1572 "<div class='header_age'>%s</div>\n"
1573 "<div id='header_commit_msg_title'>Message:</div>\n"
1574 "<div id='header_commit_msg'>%s</div>\n"
1575 "</div>\n" /* #tree_header */
1576 "</div>\n" /* #tree_header_wrapper */
1577 "<div class='dotted_line'></div>\n"
1578 "<div id='tree'>\n",
1579 rc->tree_id,
1580 age,
1581 msg);
1582 if (r == -1)
1583 goto done;
1585 error = got_output_repo_tree(c);
1586 if (error)
1587 goto done;
1589 fcgi_printf(c, "</div>\n"); /* #tree */
1590 fcgi_printf(c, "</div>\n"); /* #tree_content */
1591 done:
1592 free(age);
1593 free(msg);
1594 return error;
1597 static const struct got_error *
1598 gotweb_render_diff(struct request *c)
1600 const struct got_error *error = NULL;
1601 struct transport *t = c->t;
1602 struct repo_commit *rc = NULL;
1603 char *age = NULL, *author = NULL, *msg = NULL;
1604 int r;
1606 error = got_get_repo_commits(c, 1);
1607 if (error)
1608 return error;
1610 rc = TAILQ_FIRST(&t->repo_commits);
1612 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1613 if (error)
1614 goto done;
1615 error = gotweb_escape_html(&author, rc->author);
1616 if (error)
1617 goto done;
1618 error = gotweb_escape_html(&msg, rc->commit_msg);
1619 if (error)
1620 goto done;
1622 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1623 "<div id='diff_title'>Commit Diff</div>\n"
1624 "</div>\n" /* #diff_title_wrapper */
1625 "<div id='diff_content'>\n"
1626 "<div id='diff_header_wrapper'>\n"
1627 "<div id='diff_header'>\n"
1628 "<div id='header_diff_title'>Diff:</div>\n"
1629 "<div id='header_diff'>%s<br />%s</div>\n"
1630 "<div class='header_commit_title'>Commit:</div>\n"
1631 "<div class='header_commit'>%s</div>\n"
1632 "<div id='header_tree_title'>Tree:</div>\n"
1633 "<div id='header_tree'>%s</div>\n"
1634 "<div class='header_author_title'>Author:</div>\n"
1635 "<div class='header_author'>%s</div>\n"
1636 "<div class='header_age_title'>Date:</div>\n"
1637 "<div class='header_age'>%s</div>\n"
1638 "<div id='header_commit_msg_title'>Message:</div>\n"
1639 "<div id='header_commit_msg'>%s</div>\n"
1640 "</div>\n" /* #diff_header */
1641 "</div>\n" /* #diff_header_wrapper */
1642 "<div class='dotted_line'></div>\n"
1643 "<div id='diff'>\n",
1644 rc->parent_id, rc->commit_id,
1645 rc->commit_id,
1646 rc->tree_id,
1647 author,
1648 age,
1649 msg);
1650 if (r == -1)
1651 goto done;
1653 error = got_output_repo_diff(c);
1654 if (error)
1655 goto done;
1657 fcgi_printf(c, "</div>\n"); /* #diff */
1658 fcgi_printf(c, "</div>\n"); /* #diff_content */
1659 done:
1660 free(age);
1661 free(author);
1662 free(msg);
1663 return error;
1666 static const struct got_error *
1667 gotweb_render_summary(struct request *c)
1669 const struct got_error *error = NULL;
1670 struct transport *t = c->t;
1671 struct server *srv = c->srv;
1672 int r;
1674 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1675 goto done;
1677 if (srv->show_repo_description) {
1678 r = fcgi_printf(c,
1679 "<div id='description_title'>Description:</div>\n"
1680 "<div id='description'>%s</div>\n",
1681 t->repo_dir->description ? t->repo_dir->description : "");
1682 if (r == -1)
1683 goto done;
1686 if (srv->show_repo_owner) {
1687 r = fcgi_printf(c,
1688 "<div id='repo_owner_title'>Owner:</div>\n"
1689 "<div id='repo_owner'>%s</div>\n",
1690 t->repo_dir->owner ? t->repo_dir->owner : "");
1691 if (r == -1)
1692 goto done;
1695 if (srv->show_repo_age) {
1696 r = fcgi_printf(c,
1697 "<div id='last_change_title'>Last Change:</div>\n"
1698 "<div id='last_change'>%s</div>\n",
1699 t->repo_dir->age);
1700 if (r == -1)
1701 goto done;
1704 if (srv->show_repo_cloneurl) {
1705 r = fcgi_printf(c,
1706 "<div id='cloneurl_title'>Clone URL:</div>\n"
1707 "<div id='cloneurl'>%s</div>\n",
1708 t->repo_dir->url ? t->repo_dir->url : "");
1709 if (r == -1)
1710 goto done;
1713 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1714 if (r == -1)
1715 goto done;
1717 error = gotweb_render_briefs(c);
1718 if (error) {
1719 log_warnx("%s: %s", __func__, error->msg);
1720 goto done;
1723 error = gotweb_render_tags(c);
1724 if (error) {
1725 log_warnx("%s: %s", __func__, error->msg);
1726 goto done;
1729 error = gotweb_render_branches(c);
1730 if (error)
1731 log_warnx("%s: %s", __func__, error->msg);
1732 done:
1733 return error;
1736 static const struct got_error *
1737 gotweb_render_tag(struct request *c)
1739 const struct got_error *error = NULL;
1740 struct repo_tag *rt = NULL;
1741 struct transport *t = c->t;
1742 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1744 error = got_get_repo_tags(c, 1);
1745 if (error)
1746 goto done;
1748 if (t->tag_count == 0) {
1749 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1750 "bad commit id");
1751 goto done;
1754 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1756 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1757 if (error)
1758 goto done;
1759 error = gotweb_escape_html(&author, rt->tagger);
1760 if (error)
1761 goto done;
1762 error = gotweb_escape_html(&msg, rt->commit_msg);
1763 if (error)
1764 goto done;
1766 tagname = rt->tag_name;
1767 if (strncmp(tagname, "refs/", 5) == 0)
1768 tagname += 5;
1769 error = gotweb_escape_html(&tagname, tagname);
1770 if (error)
1771 goto done;
1773 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1774 "<div id='tags_title'>Tag</div>\n"
1775 "</div>\n" /* #tags_title_wrapper */
1776 "<div id='tags_content'>\n"
1777 "<div id='tag_header_wrapper'>\n"
1778 "<div id='tag_header'>\n"
1779 "<div class='header_commit_title'>Commit:</div>\n"
1780 "<div class='header_commit'>%s"
1781 " <span class='refs_str'>(%s)</span></div>\n"
1782 "<div class='header_author_title'>Tagger:</div>\n"
1783 "<div class='header_author'>%s</div>\n"
1784 "<div class='header_age_title'>Date:</div>\n"
1785 "<div class='header_age'>%s</div>\n"
1786 "<div id='header_commit_msg_title'>Message:</div>\n"
1787 "<div id='header_commit_msg'>%s</div>\n"
1788 "</div>\n" /* #tag_header */
1789 "<div class='dotted_line'></div>\n"
1790 "<div id='tag_commit'>\n%s</div>"
1791 "</div>", /* tag_header_wrapper */
1792 rt->commit_id,
1793 tagname,
1794 author,
1795 age,
1796 msg,
1797 rt->tag_commit);
1799 done:
1800 free(age);
1801 free(author);
1802 free(msg);
1803 return error;
1806 static const struct got_error *
1807 gotweb_render_tags(struct request *c)
1809 const struct got_error *error = NULL;
1810 struct repo_tag *rt = NULL;
1811 struct server *srv = c->srv;
1812 struct transport *t = c->t;
1813 struct querystring *qs = t->qs;
1814 struct repo_dir *repo_dir = t->repo_dir;
1815 const char *index_page_str;
1816 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1817 int r, commit_found = 0;
1819 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1821 if (qs->action == BRIEFS) {
1822 qs->action = TAGS;
1823 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1824 } else
1825 error = got_get_repo_tags(c, srv->max_commits_display);
1826 if (error)
1827 goto done;
1829 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1830 "<div id='tags_title'>Tags</div>\n"
1831 "</div>\n" /* #tags_title_wrapper */
1832 "<div id='tags_content'>\n");
1833 if (r == -1)
1834 goto done;
1836 if (t->tag_count == 0) {
1837 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1838 "This repository contains no tags");
1839 if (r == -1)
1840 goto done;
1843 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1844 if (commit_found == 0 && qs->commit != NULL) {
1845 if (strcmp(qs->commit, rt->commit_id) != 0)
1846 continue;
1847 else
1848 commit_found = 1;
1850 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1851 if (error)
1852 goto done;
1854 tagname = rt->tag_name;
1855 if (strncmp(tagname, "refs/tags/", 10) == 0)
1856 tagname += 10;
1857 error = gotweb_escape_html(&tagname, tagname);
1858 if (error)
1859 goto done;
1861 if (rt->tag_commit != NULL) {
1862 newline = strchr(rt->tag_commit, '\n');
1863 if (newline)
1864 *newline = '\0';
1865 error = gotweb_escape_html(&msg, rt->tag_commit);
1866 if (error)
1867 goto done;
1870 r = fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1871 "<div class='tag'>%s</div>\n"
1872 "<div class='tag_log'>"
1873 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1874 "%s</a>"
1875 "</div>\n" /* .tag_log */
1876 "<div class='navs_wrapper'>\n"
1877 "<div class='navs'>"
1878 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1879 "tag</a>"
1880 " | "
1881 "<a href='?index_page=%s&path=%s&action=briefs&commit=%s'>"
1882 "commit briefs</a>"
1883 " | "
1884 "<a href='?index_page=%s&path=%s&action=commits&commit=%s'>"
1885 "commits</a>"
1886 "</div>\n" /* .navs */
1887 "</div>\n" /* .navs_wrapper */
1888 "<div class='dotted_line'></div>\n",
1889 age,
1890 tagname,
1891 index_page_str, repo_dir->name, rt->commit_id,
1892 msg ? msg : "",
1893 index_page_str, repo_dir->name, rt->commit_id,
1894 index_page_str, repo_dir->name, rt->commit_id,
1895 index_page_str, repo_dir->name, rt->commit_id);
1896 if (r == -1)
1897 goto done;
1899 free(age);
1900 age = NULL;
1901 free(tagname);
1902 tagname = NULL;
1903 free(msg);
1904 msg = NULL;
1906 if (t->next_id || t->prev_id) {
1907 error = gotweb_render_navs(c);
1908 if (error)
1909 goto done;
1911 fcgi_printf(c, "</div>\n"); /* #tags_content */
1912 done:
1913 free(age);
1914 free(tagname);
1915 free(msg);
1916 return error;
1919 const struct got_error *
1920 gotweb_escape_html(char **escaped_html, const char *orig_html)
1922 const struct got_error *error = NULL;
1923 struct escape_pair {
1924 char c;
1925 const char *s;
1926 } esc[] = {
1927 { '>', "&gt;" },
1928 { '<', "&lt;" },
1929 { '&', "&amp;" },
1930 { '"', "&quot;" },
1931 { '\'', "&apos;" },
1932 { '\n', "<br />" },
1934 size_t orig_len, len;
1935 int i, j, x;
1937 orig_len = strlen(orig_html);
1938 len = orig_len;
1939 for (i = 0; i < orig_len; i++) {
1940 for (j = 0; j < nitems(esc); j++) {
1941 if (orig_html[i] != esc[j].c)
1942 continue;
1943 len += strlen(esc[j].s) - 1 /* escaped char */;
1947 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1948 if (*escaped_html == NULL)
1949 return got_error_from_errno("calloc");
1951 x = 0;
1952 for (i = 0; i < orig_len; i++) {
1953 int escaped = 0;
1954 for (j = 0; j < nitems(esc); j++) {
1955 if (orig_html[i] != esc[j].c)
1956 continue;
1958 if (strlcat(*escaped_html, esc[j].s, len + 1)
1959 >= len + 1) {
1960 error = got_error(GOT_ERR_NO_SPACE);
1961 goto done;
1963 x += strlen(esc[j].s);
1964 escaped = 1;
1965 break;
1967 if (!escaped) {
1968 (*escaped_html)[x] = orig_html[i];
1969 x++;
1972 done:
1973 if (error) {
1974 free(*escaped_html);
1975 *escaped_html = NULL;
1976 } else {
1977 (*escaped_html)[x] = '\0';
1980 return error;
1983 static struct got_repository *
1984 find_cached_repo(struct server *srv, const char *path)
1986 int i;
1988 for (i = 0; i < srv->ncached_repos; i++) {
1989 if (strcmp(srv->cached_repos[i].path, path) == 0)
1990 return srv->cached_repos[i].repo;
1993 return NULL;
1996 static const struct got_error *
1997 cache_repo(struct got_repository **new, struct server *srv,
1998 struct repo_dir *repo_dir, struct socket *sock)
2000 const struct got_error *error = NULL;
2001 struct got_repository *repo;
2002 struct cached_repo *cr;
2003 int evicted = 0;
2005 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
2006 cr = &srv->cached_repos[srv->ncached_repos - 1];
2007 error = got_repo_close(cr->repo);
2008 memset(cr, 0, sizeof(*cr));
2009 srv->ncached_repos--;
2010 if (error)
2011 return error;
2012 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
2013 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2014 cr = &srv->cached_repos[0];
2015 evicted = 1;
2016 } else {
2017 cr = &srv->cached_repos[srv->ncached_repos];
2020 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
2021 if (error) {
2022 if (evicted) {
2023 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2024 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2026 return error;
2029 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
2030 >= sizeof(cr->path)) {
2031 if (evicted) {
2032 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2033 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2035 return got_error(GOT_ERR_NO_SPACE);
2038 cr->repo = repo;
2039 srv->ncached_repos++;
2040 *new = repo;
2041 return NULL;
2044 static const struct got_error *
2045 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
2047 const struct got_error *error = NULL;
2048 struct socket *sock = c->sock;
2049 struct server *srv = c->srv;
2050 struct transport *t = c->t;
2051 struct got_repository *repo = NULL;
2052 DIR *dt;
2053 char *dir_test;
2055 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
2056 GOTWEB_GIT_DIR) == -1)
2057 return got_error_from_errno("asprintf");
2059 dt = opendir(dir_test);
2060 if (dt == NULL) {
2061 free(dir_test);
2062 } else {
2063 repo_dir->path = dir_test;
2064 dir_test = NULL;
2065 goto done;
2068 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
2069 repo_dir->name) == -1)
2070 return got_error_from_errno("asprintf");
2072 dt = opendir(dir_test);
2073 if (dt == NULL) {
2074 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2075 goto err;
2076 } else {
2077 repo_dir->path = dir_test;
2078 dir_test = NULL;
2081 done:
2082 repo = find_cached_repo(srv, repo_dir->path);
2083 if (repo == NULL) {
2084 error = cache_repo(&repo, srv, repo_dir, sock);
2085 if (error)
2086 goto err;
2088 t->repo = repo;
2089 error = gotweb_get_repo_description(&repo_dir->description, srv,
2090 repo_dir->path);
2091 if (error)
2092 goto err;
2093 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
2094 if (error)
2095 goto err;
2096 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
2097 NULL, TM_DIFF);
2098 if (error)
2099 goto err;
2100 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2101 err:
2102 free(dir_test);
2103 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2104 error = got_error_from_errno("closedir");
2105 return error;
2108 static const struct got_error *
2109 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2111 const struct got_error *error;
2113 *repo_dir = calloc(1, sizeof(**repo_dir));
2114 if (*repo_dir == NULL)
2115 return got_error_from_errno("calloc");
2117 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2118 error = got_error_from_errno("asprintf");
2119 free(*repo_dir);
2120 *repo_dir = NULL;
2121 return error;
2123 (*repo_dir)->owner = NULL;
2124 (*repo_dir)->description = NULL;
2125 (*repo_dir)->url = NULL;
2126 (*repo_dir)->age = NULL;
2127 (*repo_dir)->path = NULL;
2129 return NULL;
2132 static const struct got_error *
2133 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2135 const struct got_error *error = NULL;
2136 FILE *f = NULL;
2137 char *d_file = NULL;
2138 unsigned int len;
2139 size_t n;
2141 *description = NULL;
2142 if (srv->show_repo_description == 0)
2143 return NULL;
2145 if (asprintf(&d_file, "%s/description", dir) == -1)
2146 return got_error_from_errno("asprintf");
2148 f = fopen(d_file, "r");
2149 if (f == NULL) {
2150 if (errno != ENOENT && errno != EACCES)
2151 error = got_error_from_errno2("fopen", d_file);
2152 goto done;
2155 if (fseek(f, 0, SEEK_END) == -1) {
2156 error = got_ferror(f, GOT_ERR_IO);
2157 goto done;
2159 len = ftell(f);
2160 if (len == -1) {
2161 error = got_ferror(f, GOT_ERR_IO);
2162 goto done;
2165 if (len == 0) {
2166 *description = strdup("");
2167 if (*description == NULL)
2168 error = got_error_from_errno("strdup");
2169 goto done;
2172 if (fseek(f, 0, SEEK_SET) == -1) {
2173 error = got_ferror(f, GOT_ERR_IO);
2174 goto done;
2176 *description = calloc(len + 1, sizeof(**description));
2177 if (*description == NULL) {
2178 error = got_error_from_errno("calloc");
2179 goto done;
2182 n = fread(*description, 1, len, f);
2183 if (n == 0 && ferror(f))
2184 error = got_ferror(f, GOT_ERR_IO);
2185 done:
2186 if (f != NULL && fclose(f) == EOF && error == NULL)
2187 error = got_error_from_errno("fclose");
2188 free(d_file);
2189 return error;
2192 static const struct got_error *
2193 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2195 const struct got_error *error = NULL;
2196 FILE *f;
2197 char *d_file = NULL;
2198 unsigned int len;
2199 size_t n;
2201 *url = NULL;
2203 if (srv->show_repo_cloneurl == 0)
2204 return NULL;
2206 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2207 return got_error_from_errno("asprintf");
2209 f = fopen(d_file, "r");
2210 if (f == NULL) {
2211 if (errno != ENOENT && errno != EACCES)
2212 error = got_error_from_errno2("fopen", d_file);
2213 goto done;
2216 if (fseek(f, 0, SEEK_END) == -1) {
2217 error = got_ferror(f, GOT_ERR_IO);
2218 goto done;
2220 len = ftell(f);
2221 if (len == -1) {
2222 error = got_ferror(f, GOT_ERR_IO);
2223 goto done;
2225 if (len == 0)
2226 goto done;
2228 if (fseek(f, 0, SEEK_SET) == -1) {
2229 error = got_ferror(f, GOT_ERR_IO);
2230 goto done;
2233 *url = calloc(len + 1, sizeof(**url));
2234 if (*url == NULL) {
2235 error = got_error_from_errno("calloc");
2236 goto done;
2239 n = fread(*url, 1, len, f);
2240 if (n == 0 && ferror(f))
2241 error = got_ferror(f, GOT_ERR_IO);
2242 done:
2243 if (f != NULL && fclose(f) == EOF && error == NULL)
2244 error = got_error_from_errno("fclose");
2245 free(d_file);
2246 return error;
2249 const struct got_error *
2250 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2252 struct tm tm;
2253 long long diff_time;
2254 const char *years = "years ago", *months = "months ago";
2255 const char *weeks = "weeks ago", *days = "days ago";
2256 const char *hours = "hours ago", *minutes = "minutes ago";
2257 const char *seconds = "seconds ago", *now = "right now";
2258 char *s;
2259 char datebuf[29];
2261 *repo_age = NULL;
2263 switch (ref_tm) {
2264 case TM_DIFF:
2265 diff_time = time(NULL) - committer_time;
2266 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2267 if (asprintf(repo_age, "%lld %s",
2268 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2269 return got_error_from_errno("asprintf");
2270 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2271 if (asprintf(repo_age, "%lld %s",
2272 (diff_time / 60 / 60 / 24 / (365 / 12)),
2273 months) == -1)
2274 return got_error_from_errno("asprintf");
2275 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2276 if (asprintf(repo_age, "%lld %s",
2277 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2278 return got_error_from_errno("asprintf");
2279 } else if (diff_time > 60 * 60 * 24 * 2) {
2280 if (asprintf(repo_age, "%lld %s",
2281 (diff_time / 60 / 60 / 24), days) == -1)
2282 return got_error_from_errno("asprintf");
2283 } else if (diff_time > 60 * 60 * 2) {
2284 if (asprintf(repo_age, "%lld %s",
2285 (diff_time / 60 / 60), hours) == -1)
2286 return got_error_from_errno("asprintf");
2287 } else if (diff_time > 60 * 2) {
2288 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2289 minutes) == -1)
2290 return got_error_from_errno("asprintf");
2291 } else if (diff_time > 2) {
2292 if (asprintf(repo_age, "%lld %s", diff_time,
2293 seconds) == -1)
2294 return got_error_from_errno("asprintf");
2295 } else {
2296 if (asprintf(repo_age, "%s", now) == -1)
2297 return got_error_from_errno("asprintf");
2299 break;
2300 case TM_LONG:
2301 if (gmtime_r(&committer_time, &tm) == NULL)
2302 return got_error_from_errno("gmtime_r");
2304 s = asctime_r(&tm, datebuf);
2305 if (s == NULL)
2306 return got_error_from_errno("asctime_r");
2308 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2309 return got_error_from_errno("asprintf");
2310 break;
2312 return NULL;