2 * Copyright (c) 2019 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2014, 2015, 2017 Kristaps Dzonsons <kristaps@bsd.lv>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
21 #include <sys/types.h>
33 #include <got_object.h>
34 #include <got_reference.h>
35 #include <got_repository.h>
37 #include <got_cancel.h>
38 #include <got_worktree.h>
40 #include <got_commit_graph.h>
41 #include <got_blame.h>
42 #include <got_privsep.h>
43 #include <got_opentemp.h>
49 #include "gotweb_ui.h"
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
57 struct gotweb_conf *gw_conf;
58 struct ktemplate *gw_tmpl;
59 struct khtmlreq *gw_html_req;
68 unsigned int repos_total;
82 TAILQ_ENTRY(gw_dir) entry;
111 static const char *const templs[TEMPL__MAX] = {
125 static const struct kvalid gw_keys[KEY__MAX] = {
126 { kvalid_stringne, "path" },
127 { kvalid_stringne, "action" },
128 { kvalid_stringne, "commit" },
129 { kvalid_stringne, "file" },
130 { kvalid_int, "page" },
133 static struct gw_dir *gw_init_gw_dir(char *);
135 static char *gw_get_repo_description(struct trans *,
137 static char *gw_get_repo_owner(struct trans *,
139 static char *gw_get_repo_age(struct trans *,
140 char *, char *, int);
141 static char *gw_get_clone_url(struct trans *, char *);
142 static char *gw_get_got_link(struct trans *);
143 static char *gw_get_site_link(struct trans *);
144 static char *gw_html_escape(const char *);
146 static void gw_display_open(struct trans *, enum khttp,
148 static void gw_display_index(struct trans *,
149 const struct got_error *);
151 static int gw_template(size_t, void *);
153 static const struct got_error* apply_unveil(const char *, const char *);
154 static const struct got_error* gw_load_got_paths(struct trans *);
155 static const struct got_error* gw_load_got_path(struct trans *,
157 static const struct got_error* gw_parse_querystring(struct trans *);
159 static const struct got_error* gw_blame(struct trans *);
160 static const struct got_error* gw_blob(struct trans *);
161 static const struct got_error* gw_blob_diff(struct trans *);
162 static const struct got_error* gw_commit(struct trans *);
163 static const struct got_error* gw_commit_diff(struct trans *);
164 static const struct got_error* gw_heads(struct trans *);
165 static const struct got_error* gw_history(struct trans *);
166 static const struct got_error* gw_index(struct trans *);
167 static const struct got_error* gw_log(struct trans *);
168 static const struct got_error* gw_raw(struct trans *);
169 static const struct got_error* gw_shortlog(struct trans *);
170 static const struct got_error* gw_snapshot(struct trans *);
171 static const struct got_error* gw_summary(struct trans *);
172 static const struct got_error* gw_tags(struct trans *);
173 static const struct got_error* gw_tree(struct trans *);
175 struct gw_query_action {
176 unsigned int func_id;
177 const char *func_name;
178 const struct got_error *(*func_main)(struct trans *);
182 enum gw_query_actions {
199 static struct gw_query_action gw_query_funcs[] = {
200 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
201 { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
202 { GW_BLOBDIFF, "blobdiff", gw_blob_diff, "gw_tmpl/index.tmpl" },
203 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
204 { GW_COMMITDIFF, "commit_diff", gw_commit_diff, "gw_tmpl/index.tmpl" },
205 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
206 { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
207 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
208 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
209 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
210 { GW_SHORTLOG, "shortlog", gw_shortlog, "gw_tmpl/index.tmpl" },
211 { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
212 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summary.tmpl" },
213 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
216 static const struct got_error *
217 apply_unveil(const char *repo_path, const char *repo_file)
219 const struct got_error *err;
221 if (repo_path && repo_file) {
223 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
224 return got_error_from_errno("asprintf unveil");
225 if (unveil(full_path, "r") != 0)
226 return got_error_from_errno2("unveil", full_path);
229 if (repo_path && unveil(repo_path, "r") != 0)
230 return got_error_from_errno2("unveil", repo_path);
232 if (unveil("/tmp", "rwc") != 0)
233 return got_error_from_errno2("unveil", "/tmp");
235 err = got_privsep_unveil_exec_helpers();
239 if (unveil(NULL, NULL) != 0)
240 return got_error_from_errno("unveil");
245 static const struct got_error *
246 gw_blame(struct trans *gw_trans)
248 const struct got_error *error = NULL;
253 static const struct got_error *
254 gw_blob(struct trans *gw_trans)
256 const struct got_error *error = NULL;
261 static const struct got_error *
262 gw_blob_diff(struct trans *gw_trans)
264 const struct got_error *error = NULL;
269 static const struct got_error *
270 gw_commit(struct trans *gw_trans)
272 const struct got_error *error = NULL;
277 static const struct got_error *
278 gw_commit_diff(struct trans *gw_trans)
280 const struct got_error *error = NULL;
285 static const struct got_error *
286 gw_heads(struct trans *gw_trans)
288 const struct got_error *error = NULL;
293 static const struct got_error *
294 gw_history(struct trans *gw_trans)
296 const struct got_error *error = NULL;
301 static const struct got_error *
302 gw_index(struct trans *gw_trans)
304 const struct got_error *error = NULL;
305 struct gw_dir *dir = NULL;
306 char *html, *navs, *next, *prev;
307 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
309 error = gw_load_got_paths(gw_trans);
310 if (error && error->code != GOT_ERR_OK)
313 khttp_puts(gw_trans->gw_req, index_projects_header);
315 TAILQ_FOREACH(dir, &gw_trans->gw_dirs, entry)
318 TAILQ_FOREACH(dir, &gw_trans->gw_dirs, entry) {
319 if (gw_trans->page > 0 && (gw_trans->page *
320 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
326 if((asprintf(&navs, index_navs, dir->name, dir->name, dir->name,
328 return got_error_from_errno("asprintf");
330 if ((asprintf(&html, index_projects, dir->name, dir->name,
331 dir->description, dir->owner, dir->age, navs)) == -1)
332 return got_error_from_errno("asprintf");
334 khttp_puts(gw_trans->gw_req, html);
339 if (gw_trans->gw_conf->got_max_repos_display == 0)
342 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
343 khttp_puts(gw_trans->gw_req, np_wrapper_start);
344 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
345 (gw_trans->page > 0) &&
346 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
347 prev_disp == gw_trans->repos_total))
348 khttp_puts(gw_trans->gw_req, np_wrapper_start);
350 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
351 (gw_trans->page > 0) &&
352 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
353 prev_disp == gw_trans->repos_total)) {
354 if ((asprintf(&prev, nav_prev,
355 gw_trans->page - 1)) == -1)
356 return got_error_from_errno("asprintf");
357 khttp_puts(gw_trans->gw_req, prev);
361 khttp_puts(gw_trans->gw_req, div_end);
363 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
364 next_disp == gw_trans->gw_conf->got_max_repos_display &&
365 dir_c != (gw_trans->page + 1) *
366 gw_trans->gw_conf->got_max_repos_display) {
367 if ((asprintf(&next, nav_next,
368 gw_trans->page + 1)) == -1)
369 return got_error_from_errno("calloc");
370 khttp_puts(gw_trans->gw_req, next);
371 khttp_puts(gw_trans->gw_req, div_end);
377 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
378 (gw_trans->page > 0) &&
379 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
380 prev_disp == gw_trans->repos_total))
381 khttp_puts(gw_trans->gw_req, div_end);
388 static const struct got_error *
389 gw_log(struct trans *gw_trans)
391 const struct got_error *error = NULL;
396 static const struct got_error *
397 gw_raw(struct trans *gw_trans)
399 const struct got_error *error = NULL;
404 static const struct got_error *
405 gw_shortlog(struct trans *gw_trans)
407 const struct got_error *error = NULL;
412 static const struct got_error *
413 gw_snapshot(struct trans *gw_trans)
415 const struct got_error *error = NULL;
420 static const struct got_error *
421 gw_summary(struct trans *gw_trans)
423 const struct got_error *error = NULL;
425 error = gw_shortlog(gw_trans);
426 error = gw_tags(gw_trans);
427 error = gw_heads(gw_trans);
428 khttp_puts(gw_trans->gw_req, summary_shortlog);
429 khttp_puts(gw_trans->gw_req, summary_tags);
430 khttp_puts(gw_trans->gw_req, summary_heads);
434 static const struct got_error *
435 gw_tags(struct trans *gw_trans)
437 const struct got_error *error = NULL;
442 static const struct got_error *
443 gw_tree(struct trans *gw_trans)
445 const struct got_error *error = NULL;
450 static const struct got_error *
451 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
453 const struct got_error *error = NULL;
458 if ((asprintf(&dir_test, "%s/%s/%s",
459 gw_trans->gw_conf->got_repos_path, gw_dir->name,
460 GOTWEB_GIT_DIR)) == -1)
461 return got_error_from_errno("asprintf");
463 dt = opendir(dir_test);
467 gw_dir->path = strdup(dir_test);
472 if ((asprintf(&dir_test, "%s/%s/%s",
473 gw_trans->gw_conf->got_repos_path, gw_dir->name,
474 GOTWEB_GOT_DIR)) == -1)
475 return got_error_from_errno("asprintf");
477 dt = opendir(dir_test);
482 error = got_error(GOT_ERR_NOT_GIT_REPO);
486 if ((asprintf(&dir_test, "%s/%s",
487 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
488 return got_error_from_errno("asprintf");
490 gw_dir->path = strdup(dir_test);
493 gw_dir->description = gw_get_repo_description(gw_trans,
495 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
496 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
498 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
507 static const struct got_error *
508 gw_load_got_paths(struct trans *gw_trans)
510 const struct got_error *error = NULL;
512 struct dirent **sd_dent;
513 struct gw_dir *gw_dir;
515 unsigned int d_cnt, d_i;
517 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
518 error = got_error_from_errno("pledge");
522 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
526 d = opendir(gw_trans->gw_conf->got_repos_path);
528 error = got_error_from_errno2("opendir",
529 gw_trans->gw_conf->got_repos_path);
533 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
536 error = got_error_from_errno2("scandir",
537 gw_trans->gw_conf->got_repos_path);
541 for (d_i = 0; d_i < d_cnt; d_i++) {
542 if (gw_trans->gw_conf->got_max_repos > 0 &&
543 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
544 break; /* account for parent and self */
546 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
547 strcmp(sd_dent[d_i]->d_name, "..") == 0)
550 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
551 return got_error_from_errno("gw_dir malloc");
553 error = gw_load_got_path(gw_trans, gw_dir);
554 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
559 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
560 !got_path_dir_is_empty(gw_dir->path)) {
561 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
563 gw_trans->repos_total++;
571 static const struct got_error *
572 gw_parse_querystring(struct trans *gw_trans)
574 const struct got_error *error = NULL;
576 struct gw_query_action *action = NULL;
579 if (gw_trans->gw_req->fieldnmap[0]) {
580 error = got_error_from_errno("bad parse");
582 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
583 /* define gw_trans->repo_path */
584 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
585 return got_error_from_errno("asprintf");
587 if ((asprintf(&gw_trans->repo_path, "%s/%s",
588 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
589 return got_error_from_errno("asprintf");
591 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
592 if ((asprintf(&gw_trans->commit, "%s",
594 return got_error_from_errno("asprintf");
596 /* get action and set function */
597 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
598 for (i = 0; i < nitems(gw_query_funcs); i++) {
599 action = &gw_query_funcs[i];
600 if (action->func_name == NULL)
603 if (strcmp(action->func_name,
605 gw_trans->action = i;
606 if ((asprintf(&gw_trans->action_name,
607 "%s", action->func_name)) == -1)
609 got_error_from_errno(
618 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
619 if ((asprintf(&gw_trans->repo_file, "%s",
621 return got_error_from_errno("asprintf");
623 if (action == NULL) {
624 error = got_error_from_errno("invalid action");
628 gw_trans->action = GW_INDEX;
630 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
631 gw_trans->page = p->parsed.i;
633 if (gw_trans->action == GW_RAW)
634 gw_trans->mime = KMIME_TEXT_PLAIN;
639 static struct gw_dir *
640 gw_init_gw_dir(char *dir)
642 struct gw_dir *gw_dir;
644 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
647 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
654 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
656 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
657 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
659 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
661 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
662 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
663 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
664 khttp_body(gw_trans->gw_req);
668 gw_display_index(struct trans *gw_trans, const struct got_error *err)
670 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
671 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
674 khttp_puts(gw_trans->gw_req, err->msg);
676 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
677 gw_query_funcs[gw_trans->action].template);
679 khtml_close(gw_trans->gw_html_req);
683 gw_template(size_t key, void *arg)
685 const struct got_error *error = NULL;
686 struct trans *gw_trans = arg;
687 char *gw_got_link, *gw_site_link;
688 char *site_owner_name, *site_owner_name_h;
689 char *description, *description_h;
690 char *repo_owner, *repo_owner_h;
691 char *repo_age, *repo_age_h;
692 char *cloneurl, *cloneurl_h;
696 khttp_puts(gw_trans->gw_req, head);
699 gw_got_link = gw_get_got_link(gw_trans);
700 if (gw_got_link != NULL)
701 khttp_puts(gw_trans->gw_req, gw_got_link);
705 case (TEMPL_SITEPATH):
706 gw_site_link = gw_get_site_link(gw_trans);
707 if (gw_site_link != NULL)
708 khttp_puts(gw_trans->gw_req, gw_site_link);
713 if (gw_trans->gw_conf->got_site_name != NULL)
714 khtml_puts(gw_trans->gw_html_req,
715 gw_trans->gw_conf->got_site_name);
719 khttp_puts(gw_trans->gw_req, search);
721 case(TEMPL_DESCRIPTION):
722 if (gw_trans->gw_conf->got_show_repo_description) {
723 description = gw_html_escape(
724 gw_get_repo_description(gw_trans,
725 gw_trans->repo_path));
726 if (description != NULL &&
727 (strcmp(description, "") != 0)) {
728 if ((asprintf(&description_h,
729 summary_description, description)) == -1)
732 khttp_puts(gw_trans->gw_req, description_h);
738 case(TEMPL_SITEOWNER):
739 if (gw_trans->gw_conf->got_site_owner != NULL &&
740 gw_trans->gw_conf->got_show_site_owner) {
742 gw_html_escape(gw_trans->gw_conf->got_site_owner);
743 if ((asprintf(&site_owner_name_h, site_owner,
748 khttp_puts(gw_trans->gw_req, site_owner_name_h);
749 free(site_owner_name);
750 free(site_owner_name_h);
754 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
756 khttp_puts(gw_trans->gw_req, error->msg);
759 case(TEMPL_REPO_OWNER):
760 if (gw_trans->gw_conf->got_show_repo_owner) {
761 repo_owner = gw_html_escape(gw_get_repo_owner(gw_trans,
762 gw_trans->repo_path));
763 if ((asprintf(&repo_owner_h, summary_repo_owner,
767 if (repo_owner != NULL &&
768 (strcmp(repo_owner, "") != 0)) {
769 khttp_puts(gw_trans->gw_req, repo_owner_h);
775 case(TEMPL_REPO_AGE):
776 if (gw_trans->gw_conf->got_show_repo_age) {
777 repo_age = gw_get_repo_age(gw_trans,
778 gw_trans->repo_path, "refs/heads", TM_LONG);
779 if (repo_age != NULL) {
780 if ((asprintf(&repo_age_h, summary_last_change,
783 khttp_puts(gw_trans->gw_req, repo_age_h);
789 case(TEMPL_CLONEURL):
790 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
791 cloneurl = gw_html_escape(gw_get_clone_url(gw_trans,
792 gw_trans->repo_path));
793 if (cloneurl != NULL) {
794 if ((asprintf(&cloneurl_h,
795 summary_cloneurl, cloneurl)) == -1)
798 khttp_puts(gw_trans->gw_req, cloneurl_h);
813 gw_get_repo_description(struct trans *gw_trans, char *dir)
816 char *description = NULL, *d_file = NULL;
819 if (gw_trans->gw_conf->got_show_repo_description == false)
822 if ((asprintf(&d_file, "%s/description", dir)) == -1)
825 if ((f = fopen(d_file, "r")) == NULL)
828 fseek(f, 0, SEEK_END);
830 fseek(f, 0, SEEK_SET);
831 if ((description = calloc(len, sizeof(char *))) == NULL)
834 fread(description, 1, len, f);
839 if ((asprintf(&description, "%s", "")) == -1)
846 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
848 const struct got_error *error = NULL;
849 struct got_object_id *id = NULL;
850 struct got_repository *repo = NULL;
851 struct got_commit_object *commit = NULL;
852 struct got_reflist_head refs;
853 struct got_reflist_entry *re;
854 struct got_reference *head_ref;
856 time_t committer_time = 0, cmp_time = 0, diff_time;
857 char *repo_age = NULL, *years = "years ago", *months = "months ago";
858 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
859 char *minutes = "minutes ago", *seconds = "seconds ago";
860 char *now = "right now";
862 char datebuf[BUFFER_SIZE];
864 if (repo_ref == NULL)
868 if (gw_trans->gw_conf->got_show_repo_age == false) {
869 asprintf(&repo_age, "");
872 error = got_repo_open(&repo, dir, NULL);
876 error = got_ref_list(&refs, repo, repo_ref, got_ref_cmp_by_name,
882 SIMPLEQ_FOREACH(re, &refs, entry) {
883 refname = got_ref_get_name(re->ref);
884 error = got_ref_open(&head_ref, repo, refname, 0);
888 error = got_ref_resolve(&id, repo, head_ref);
889 got_ref_close(head_ref);
893 /* here is what breaks tags, so adjust */
894 error = got_object_open_as_commit(&commit, repo, id);
899 got_object_commit_get_committer_time(commit);
901 if (cmp_time < committer_time)
902 cmp_time = committer_time;
906 committer_time = cmp_time;
910 diff_time = time(NULL) - committer_time;
911 if (diff_time > 60 * 60 * 24 * 365 * 2) {
912 if ((asprintf(&repo_age, "%lld %s",
913 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
915 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
916 if ((asprintf(&repo_age, "%lld %s",
917 (diff_time / 60 / 60 / 24 / (365 / 12)),
920 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
921 if ((asprintf(&repo_age, "%lld %s",
922 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
924 } else if (diff_time > 60 * 60 * 24 * 2) {
925 if ((asprintf(&repo_age, "%lld %s",
926 (diff_time / 60 / 60 / 24), days)) == -1)
928 } else if (diff_time > 60 * 60 * 2) {
929 if ((asprintf(&repo_age, "%lld %s",
930 (diff_time / 60 / 60), hours)) == -1)
932 } else if (diff_time > 60 * 2) {
933 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
936 } else if (diff_time > 2) {
937 if ((asprintf(&repo_age, "%lld %s", diff_time,
941 if ((asprintf(&repo_age, "%s", now)) == -1)
947 if (gmtime_r(&committer_time, &tm) == NULL)
950 s = asctime_r(&tm, datebuf);
954 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
957 if ((asprintf(&repo_age, "")) == -1)
964 got_ref_list_free(&refs);
968 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
975 gw_get_repo_owner(struct trans *gw_trans, char *dir)
978 char *owner = NULL, *d_file = NULL;
979 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
980 char *comp, *pos, *buf;
983 if (gw_trans->gw_conf->got_show_repo_owner == false)
986 if ((asprintf(&d_file, "%s/config", dir)) == -1)
989 if ((f = fopen(d_file, "r")) == NULL)
992 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
995 while ((fgets(buf, BUFFER_SIZE, f)) != NULL) {
996 if ((pos = strstr(buf, gotweb)) != NULL)
999 if ((pos = strstr(buf, gitweb)) != NULL)
1007 fgets(buf, BUFFER_SIZE, f);
1008 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1013 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1016 for (i = 0; i < 2; i++) {
1017 owner = strsep(&buf, "\"");
1027 if ((asprintf(&owner, "%s", "")) == -1)
1034 gw_get_clone_url(struct trans *gw_trans, char *dir)
1037 char *url = NULL, *d_file = NULL;
1040 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1043 if ((f = fopen(d_file, "r")) == NULL)
1046 fseek(f, 0, SEEK_END);
1048 fseek(f, 0, SEEK_SET);
1050 if ((url = calloc(len, sizeof(char *))) == NULL)
1053 fread(url, 1, len, f);
1060 gw_get_got_link(struct trans *gw_trans)
1064 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
1065 gw_trans->gw_conf->got_logo)) == -1)
1072 gw_get_site_link(struct trans *gw_trans)
1074 char *link, *repo = "", *action = "";
1076 if (gw_trans->repo_name != NULL)
1077 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
1078 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
1081 if (gw_trans->action_name != NULL)
1082 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
1085 if ((asprintf(&link, site_link, GOTWEB,
1086 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
1093 gw_html_escape(const char *html)
1095 char *escaped_str = NULL, *buf;
1099 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1105 if ((sz = strlen(html)) == 0)
1108 /* only work with BUFFER_SIZE */
1109 if (BUFFER_SIZE < sz)
1112 for (i = 0; i < sz; i++) {
1116 strcat(buf, ">");
1119 strcat(buf, "&");
1122 strcat(buf, "<");
1125 strcat(buf, """);
1128 strcat(buf, "'");
1131 strcat(buf, "<br />");
1137 asprintf(&escaped_str, "%s", buf);
1145 const struct got_error *error = NULL;
1146 struct trans *gw_trans;
1147 struct gw_dir *dir = NULL, *tdir;
1148 const char *page = "index";
1149 bool gw_malloc = true;
1151 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
1154 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
1157 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
1160 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
1163 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
1165 errx(1, "khttp_parse");
1167 if ((gw_trans->gw_conf =
1168 malloc(sizeof(struct gotweb_conf))) == NULL) {
1170 error = got_error_from_errno("malloc");
1174 TAILQ_INIT(&gw_trans->gw_dirs);
1177 gw_trans->repos_total = 0;
1178 gw_trans->repo_path = NULL;
1179 gw_trans->commit = NULL;
1180 gw_trans->mime = KMIME_TEXT_HTML;
1181 gw_trans->gw_tmpl->key = templs;
1182 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
1183 gw_trans->gw_tmpl->arg = gw_trans;
1184 gw_trans->gw_tmpl->cb = gw_template;
1185 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
1189 gw_trans->mime = KMIME_TEXT_PLAIN;
1190 gw_trans->action = GW_ERR;
1191 gw_display_index(gw_trans, error);
1195 error = gw_parse_querystring(gw_trans);
1199 gw_display_index(gw_trans, error);
1203 free(gw_trans->gw_conf->got_repos_path);
1204 free(gw_trans->gw_conf->got_www_path);
1205 free(gw_trans->gw_conf->got_site_name);
1206 free(gw_trans->gw_conf->got_site_owner);
1207 free(gw_trans->gw_conf->got_site_link);
1208 free(gw_trans->gw_conf->got_logo);
1209 free(gw_trans->gw_conf->got_logo_url);
1210 free(gw_trans->gw_conf);
1211 free(gw_trans->commit);
1212 free(gw_trans->repo_path);
1213 free(gw_trans->repo_name);
1214 free(gw_trans->repo_file);
1215 free(gw_trans->action_name);
1217 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
1219 free(dir->description);
1228 khttp_free(gw_trans->gw_req);
1229 return EXIT_SUCCESS;