2 * Copyright (c) 2019, 2020 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>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
51 #include "gotweb_ui.h"
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
72 unsigned int repos_total;
87 TAILQ_ENTRY(gw_dir) entry;
133 static const char *const templs[TEMPL__MAX] = {
143 static const struct kvalid gw_keys[KEY__MAX] = {
144 { kvalid_stringne, "path" },
145 { kvalid_stringne, "action" },
146 { kvalid_stringne, "commit" },
147 { kvalid_stringne, "file" },
148 { kvalid_int, "page" },
149 { kvalid_stringne, "headref" },
152 static struct gw_dir *gw_init_gw_dir(char *);
154 static char *gw_get_repo_description(struct trans *,
156 static char *gw_get_repo_owner(struct trans *,
158 static char *gw_get_time_str(time_t, int);
159 static char *gw_get_repo_age(struct trans *,
160 char *, char *, int);
161 static char *gw_get_repo_log(struct trans *, const char *,
163 static char *gw_get_repo_tags(struct trans *, int, int);
164 static char *gw_get_repo_heads(struct trans *);
165 static char *gw_get_clone_url(struct trans *, char *);
166 static char *gw_get_got_link(struct trans *);
167 static char *gw_get_site_link(struct trans *);
168 static char *gw_html_escape(const char *);
170 static void gw_display_open(struct trans *, enum khttp,
172 static void gw_display_index(struct trans *,
173 const struct got_error *);
175 static int gw_template(size_t, void *);
177 static const struct got_error* apply_unveil(const char *, const char *);
178 static const struct got_error* cmp_tags(void *, int *,
179 struct got_reference *,
180 struct got_reference *);
181 static const struct got_error* gw_load_got_paths(struct trans *);
182 static const struct got_error* gw_load_got_path(struct trans *,
184 static const struct got_error* gw_parse_querystring(struct trans *);
185 static const struct got_error* match_logmsg(int *, struct got_object_id *,
186 struct got_commit_object *, regex_t *);
188 static const struct got_error* gw_blame(struct trans *);
189 static const struct got_error* gw_blob(struct trans *);
190 static const struct got_error* gw_blobdiff(struct trans *);
191 static const struct got_error* gw_commit(struct trans *);
192 static const struct got_error* gw_commitdiff(struct trans *);
193 static const struct got_error* gw_history(struct trans *);
194 static const struct got_error* gw_index(struct trans *);
195 static const struct got_error* gw_log(struct trans *);
196 static const struct got_error* gw_raw(struct trans *);
197 static const struct got_error* gw_logbriefs(struct trans *);
198 static const struct got_error* gw_snapshot(struct trans *);
199 static const struct got_error* gw_summary(struct trans *);
200 static const struct got_error* gw_tag(struct trans *);
201 static const struct got_error* gw_tree(struct trans *);
203 struct gw_query_action {
204 unsigned int func_id;
205 const char *func_name;
206 const struct got_error *(*func_main)(struct trans *);
210 enum gw_query_actions {
228 static struct gw_query_action gw_query_funcs[] = {
229 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
230 { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
231 { GW_BLOBDIFF, "blobdiff", gw_blobdiff, "gw_tmpl/index.tmpl" },
232 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
233 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
234 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
235 { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
236 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
237 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
238 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
239 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
240 { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
241 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
242 { GW_TAG, "tag", gw_tag, "gw_tmpl/index.tmpl" },
243 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
246 static const struct got_error *
247 apply_unveil(const char *repo_path, const char *repo_file)
249 const struct got_error *err;
251 if (repo_path && repo_file) {
253 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
254 return got_error_from_errno("asprintf unveil");
255 if (unveil(full_path, "r") != 0)
256 return got_error_from_errno2("unveil", full_path);
259 if (repo_path && unveil(repo_path, "r") != 0)
260 return got_error_from_errno2("unveil", repo_path);
262 if (unveil("/tmp", "rwc") != 0)
263 return got_error_from_errno2("unveil", "/tmp");
265 err = got_privsep_unveil_exec_helpers();
269 if (unveil(NULL, NULL) != 0)
270 return got_error_from_errno("unveil");
275 static const struct got_error *
276 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
277 struct got_reference *ref2)
279 const struct got_error *err = NULL;
280 struct got_repository *repo = arg;
281 struct got_object_id *id1, *id2 = NULL;
282 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
287 err = got_ref_resolve(&id1, repo, ref1);
290 err = got_object_open_as_tag(&tag1, repo, id1);
294 err = got_ref_resolve(&id2, repo, ref2);
297 err = got_object_open_as_tag(&tag2, repo, id2);
301 time1 = got_object_tag_get_tagger_time(tag1);
302 time2 = got_object_tag_get_tagger_time(tag2);
304 /* Put latest tags first. */
307 else if (time1 > time2)
310 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
315 got_object_tag_close(tag1);
317 got_object_tag_close(tag2);
321 static const struct got_error *
322 gw_blame(struct trans *gw_trans)
324 const struct got_error *error = NULL;
329 static const struct got_error *
330 gw_blob(struct trans *gw_trans)
332 const struct got_error *error = NULL;
337 static const struct got_error *
338 gw_blobdiff(struct trans *gw_trans)
340 const struct got_error *error = NULL;
345 static const struct got_error *
346 gw_commit(struct trans *gw_trans)
348 const struct got_error *error = NULL;
349 char *log, *log_html;
351 error = apply_unveil(gw_trans->gw_dir->path, NULL);
355 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
357 if (log != NULL && strcmp(log, "") != 0) {
358 if ((asprintf(&log_html, log_commit, log)) == -1)
359 return got_error_from_errno("asprintf");
360 khttp_puts(gw_trans->gw_req, log_html);
367 static const struct got_error *
368 gw_commitdiff(struct trans *gw_trans)
370 const struct got_error *error = NULL;
371 char *log, *log_html;
373 error = apply_unveil(gw_trans->gw_dir->path, NULL);
377 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
379 if (log != NULL && strcmp(log, "") != 0) {
380 if ((asprintf(&log_html, log_diff, log)) == -1)
381 return got_error_from_errno("asprintf");
382 khttp_puts(gw_trans->gw_req, log_html);
389 static const struct got_error *
390 gw_history(struct trans *gw_trans)
392 const struct got_error *error = NULL;
397 static const struct got_error *
398 gw_index(struct trans *gw_trans)
400 const struct got_error *error = NULL;
401 struct gw_dir *gw_dir = NULL;
402 char *html, *navs, *next, *prev;
403 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
405 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
409 error = gw_load_got_paths(gw_trans);
413 khttp_puts(gw_trans->gw_req, index_projects_header);
415 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
418 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
419 if (gw_trans->page > 0 && (gw_trans->page *
420 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
426 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
427 gw_dir->name, gw_dir->name)) == -1)
428 return got_error_from_errno("asprintf");
430 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
431 gw_dir->description, gw_dir->owner, gw_dir->age,
433 return got_error_from_errno("asprintf");
435 khttp_puts(gw_trans->gw_req, html);
440 if (gw_trans->gw_conf->got_max_repos_display == 0)
443 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
444 khttp_puts(gw_trans->gw_req, np_wrapper_start);
445 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
446 (gw_trans->page > 0) &&
447 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
448 prev_disp == gw_trans->repos_total))
449 khttp_puts(gw_trans->gw_req, np_wrapper_start);
451 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
452 (gw_trans->page > 0) &&
453 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
454 prev_disp == gw_trans->repos_total)) {
455 if ((asprintf(&prev, nav_prev,
456 gw_trans->page - 1)) == -1)
457 return got_error_from_errno("asprintf");
458 khttp_puts(gw_trans->gw_req, prev);
462 khttp_puts(gw_trans->gw_req, div_end);
464 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
465 next_disp == gw_trans->gw_conf->got_max_repos_display &&
466 dir_c != (gw_trans->page + 1) *
467 gw_trans->gw_conf->got_max_repos_display) {
468 if ((asprintf(&next, nav_next,
469 gw_trans->page + 1)) == -1)
470 return got_error_from_errno("calloc");
471 khttp_puts(gw_trans->gw_req, next);
472 khttp_puts(gw_trans->gw_req, div_end);
478 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
479 (gw_trans->page > 0) &&
480 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
481 prev_disp == gw_trans->repos_total))
482 khttp_puts(gw_trans->gw_req, div_end);
489 static const struct got_error *
490 gw_log(struct trans *gw_trans)
492 const struct got_error *error = NULL;
493 char *log, *log_html;
495 error = apply_unveil(gw_trans->gw_dir->path, NULL);
499 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
500 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
502 if (log != NULL && strcmp(log, "") != 0) {
503 if ((asprintf(&log_html, logs, log)) == -1)
504 return got_error_from_errno("asprintf");
505 khttp_puts(gw_trans->gw_req, log_html);
512 static const struct got_error *
513 gw_raw(struct trans *gw_trans)
515 const struct got_error *error = NULL;
520 static const struct got_error *
521 gw_logbriefs(struct trans *gw_trans)
523 const struct got_error *error = NULL;
524 char *log, *log_html;
526 error = apply_unveil(gw_trans->gw_dir->path, NULL);
530 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
531 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
533 if (log != NULL && strcmp(log, "") != 0) {
534 if ((asprintf(&log_html, summary_logbriefs,
536 return got_error_from_errno("asprintf");
537 khttp_puts(gw_trans->gw_req, log_html);
544 static const struct got_error *
545 gw_snapshot(struct trans *gw_trans)
547 const struct got_error *error = NULL;
552 static const struct got_error *
553 gw_summary(struct trans *gw_trans)
555 const struct got_error *error = NULL;
556 char *description_html, *repo_owner_html, *repo_age_html,
557 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
560 error = apply_unveil(gw_trans->gw_dir->path, NULL);
564 khttp_puts(gw_trans->gw_req, summary_wrapper);
565 if (gw_trans->gw_conf->got_show_repo_description) {
566 if (gw_trans->gw_dir->description != NULL &&
567 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
568 if ((asprintf(&description_html, description,
569 gw_trans->gw_dir->description)) == -1)
570 return got_error_from_errno("asprintf");
572 khttp_puts(gw_trans->gw_req, description_html);
573 free(description_html);
577 if (gw_trans->gw_conf->got_show_repo_owner) {
578 if (gw_trans->gw_dir->owner != NULL &&
579 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
580 if ((asprintf(&repo_owner_html, repo_owner,
581 gw_trans->gw_dir->owner)) == -1)
582 return got_error_from_errno("asprintf");
584 khttp_puts(gw_trans->gw_req, repo_owner_html);
585 free(repo_owner_html);
589 if (gw_trans->gw_conf->got_show_repo_age) {
590 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
591 "refs/heads", TM_LONG);
592 if (age != NULL && (strcmp(age, "") != 0)) {
593 if ((asprintf(&repo_age_html, last_change, age)) == -1)
594 return got_error_from_errno("asprintf");
596 khttp_puts(gw_trans->gw_req, repo_age_html);
602 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
603 if (gw_trans->gw_dir->url != NULL &&
604 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
605 if ((asprintf(&cloneurl_html, cloneurl,
606 gw_trans->gw_dir->url)) == -1)
607 return got_error_from_errno("asprintf");
609 khttp_puts(gw_trans->gw_req, cloneurl_html);
613 khttp_puts(gw_trans->gw_req, div_end);
615 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
616 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
617 heads = gw_get_repo_heads(gw_trans);
619 if (log != NULL && strcmp(log, "") != 0) {
620 if ((asprintf(&log_html, summary_logbriefs,
622 return got_error_from_errno("asprintf");
623 khttp_puts(gw_trans->gw_req, log_html);
628 if (tags != NULL && strcmp(tags, "") != 0) {
629 if ((asprintf(&tags_html, summary_tags,
631 return got_error_from_errno("asprintf");
632 khttp_puts(gw_trans->gw_req, tags_html);
637 if (heads != NULL && strcmp(heads, "") != 0) {
638 if ((asprintf(&heads_html, summary_heads,
640 return got_error_from_errno("asprintf");
641 khttp_puts(gw_trans->gw_req, heads_html);
648 static const struct got_error *
649 gw_tag(struct trans *gw_trans)
651 const struct got_error *error = NULL;
652 char *log, *log_html;
654 error = apply_unveil(gw_trans->gw_dir->path, NULL);
658 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTAG);
660 if (log != NULL && strcmp(log, "") != 0) {
661 if ((asprintf(&log_html, log_tag, log)) == -1)
662 return got_error_from_errno("asprintf");
663 khttp_puts(gw_trans->gw_req, log_html);
670 static const struct got_error *
671 gw_tree(struct trans *gw_trans)
673 const struct got_error *error = NULL;
674 char *log, *log_html;
676 error = apply_unveil(gw_trans->gw_dir->path, NULL);
680 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
682 if (log != NULL && strcmp(log, "") != 0) {
683 if ((asprintf(&log_html, log_tree, log)) == -1)
684 return got_error_from_errno("asprintf");
685 khttp_puts(gw_trans->gw_req, log_html);
692 static const struct got_error *
693 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
695 const struct got_error *error = NULL;
700 if ((asprintf(&dir_test, "%s/%s/%s",
701 gw_trans->gw_conf->got_repos_path, gw_dir->name,
702 GOTWEB_GIT_DIR)) == -1)
703 return got_error_from_errno("asprintf");
705 dt = opendir(dir_test);
709 gw_dir->path = strdup(dir_test);
714 if ((asprintf(&dir_test, "%s/%s/%s",
715 gw_trans->gw_conf->got_repos_path, gw_dir->name,
716 GOTWEB_GOT_DIR)) == -1)
717 return got_error_from_errno("asprintf");
719 dt = opendir(dir_test);
724 error = got_error(GOT_ERR_NOT_GIT_REPO);
728 if ((asprintf(&dir_test, "%s/%s",
729 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
730 return got_error_from_errno("asprintf");
732 gw_dir->path = strdup(dir_test);
735 gw_dir->description = gw_get_repo_description(gw_trans,
737 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
738 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
740 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
749 static const struct got_error *
750 gw_load_got_paths(struct trans *gw_trans)
752 const struct got_error *error = NULL;
754 struct dirent **sd_dent;
755 struct gw_dir *gw_dir;
757 unsigned int d_cnt, d_i;
759 d = opendir(gw_trans->gw_conf->got_repos_path);
761 error = got_error_from_errno2("opendir",
762 gw_trans->gw_conf->got_repos_path);
766 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
769 error = got_error_from_errno2("scandir",
770 gw_trans->gw_conf->got_repos_path);
774 for (d_i = 0; d_i < d_cnt; d_i++) {
775 if (gw_trans->gw_conf->got_max_repos > 0 &&
776 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
777 break; /* account for parent and self */
779 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
780 strcmp(sd_dent[d_i]->d_name, "..") == 0)
783 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
784 return got_error_from_errno("gw_dir malloc");
786 error = gw_load_got_path(gw_trans, gw_dir);
787 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
792 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
793 !got_path_dir_is_empty(gw_dir->path)) {
794 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
796 gw_trans->repos_total++;
804 static const struct got_error *
805 gw_parse_querystring(struct trans *gw_trans)
807 const struct got_error *error = NULL;
809 struct gw_query_action *action = NULL;
812 if (gw_trans->gw_req->fieldnmap[0]) {
813 error = got_error_from_errno("bad parse");
815 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
816 /* define gw_trans->repo_path */
817 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
818 return got_error_from_errno("asprintf");
820 if ((asprintf(&gw_trans->repo_path, "%s/%s",
821 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
822 return got_error_from_errno("asprintf");
824 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
825 if ((asprintf(&gw_trans->commit, "%s",
827 return got_error_from_errno("asprintf");
829 /* get action and set function */
830 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
831 for (i = 0; i < nitems(gw_query_funcs); i++) {
832 action = &gw_query_funcs[i];
833 if (action->func_name == NULL)
836 if (strcmp(action->func_name,
838 gw_trans->action = i;
839 if ((asprintf(&gw_trans->action_name,
840 "%s", action->func_name)) == -1)
842 got_error_from_errno(
851 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
852 if ((asprintf(&gw_trans->repo_file, "%s",
854 return got_error_from_errno("asprintf");
856 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
857 if ((asprintf(&gw_trans->headref, "%s",
859 return got_error_from_errno("asprintf");
861 if (action == NULL) {
862 error = got_error_from_errno("invalid action");
865 if ((gw_trans->gw_dir =
866 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
867 return got_error_from_errno("gw_dir malloc");
869 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
873 gw_trans->action = GW_INDEX;
875 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
876 gw_trans->page = p->parsed.i;
878 if (gw_trans->action == GW_RAW)
879 gw_trans->mime = KMIME_TEXT_PLAIN;
884 static struct gw_dir *
885 gw_init_gw_dir(char *dir)
887 struct gw_dir *gw_dir;
889 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
892 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
898 static const struct got_error*
899 match_logmsg(int *have_match, struct got_object_id *id,
900 struct got_commit_object *commit, regex_t *regex)
902 const struct got_error *err = NULL;
904 char *id_str = NULL, *logmsg = NULL;
908 err = got_object_id_str(&id_str, id);
912 err = got_object_commit_get_logmsg(&logmsg, commit);
916 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
925 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
927 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
928 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
930 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
932 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
933 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
934 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
935 khttp_body(gw_trans->gw_req);
939 gw_display_index(struct trans *gw_trans, const struct got_error *err)
941 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
942 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
945 khttp_puts(gw_trans->gw_req, err->msg);
947 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
948 gw_query_funcs[gw_trans->action].template);
950 khtml_close(gw_trans->gw_html_req);
954 gw_template(size_t key, void *arg)
956 const struct got_error *error = NULL;
957 struct trans *gw_trans = arg;
958 char *gw_got_link, *gw_site_link;
959 char *site_owner_name, *site_owner_name_h;
963 khttp_puts(gw_trans->gw_req, head);
966 gw_got_link = gw_get_got_link(gw_trans);
967 if (gw_got_link != NULL)
968 khttp_puts(gw_trans->gw_req, gw_got_link);
972 case (TEMPL_SITEPATH):
973 gw_site_link = gw_get_site_link(gw_trans);
974 if (gw_site_link != NULL)
975 khttp_puts(gw_trans->gw_req, gw_site_link);
980 if (gw_trans->gw_conf->got_site_name != NULL)
981 khtml_puts(gw_trans->gw_html_req,
982 gw_trans->gw_conf->got_site_name);
986 khttp_puts(gw_trans->gw_req, search);
988 case(TEMPL_SITEOWNER):
989 if (gw_trans->gw_conf->got_site_owner != NULL &&
990 gw_trans->gw_conf->got_show_site_owner) {
992 gw_html_escape(gw_trans->gw_conf->got_site_owner);
993 if ((asprintf(&site_owner_name_h, site_owner,
998 khttp_puts(gw_trans->gw_req, site_owner_name_h);
999 free(site_owner_name);
1000 free(site_owner_name_h);
1003 case(TEMPL_CONTENT):
1004 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1006 khttp_puts(gw_trans->gw_req, error->msg);
1017 gw_get_repo_description(struct trans *gw_trans, char *dir)
1020 char *description = NULL, *d_file = NULL;
1023 if (gw_trans->gw_conf->got_show_repo_description == false)
1026 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1029 if ((f = fopen(d_file, "r")) == NULL)
1032 fseek(f, 0, SEEK_END);
1034 fseek(f, 0, SEEK_SET);
1035 if ((description = calloc(len, sizeof(char *))) == NULL)
1038 fread(description, 1, len, f);
1043 if ((asprintf(&description, "%s", "")) == -1)
1050 gw_get_time_str(time_t committer_time, int ref_tm)
1054 char *years = "years ago", *months = "months ago";
1055 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1056 char *minutes = "minutes ago", *seconds = "seconds ago";
1057 char *now = "right now";
1063 diff_time = time(NULL) - committer_time;
1064 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1065 if ((asprintf(&repo_age, "%lld %s",
1066 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1068 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1069 if ((asprintf(&repo_age, "%lld %s",
1070 (diff_time / 60 / 60 / 24 / (365 / 12)),
1073 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1074 if ((asprintf(&repo_age, "%lld %s",
1075 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1077 } else if (diff_time > 60 * 60 * 24 * 2) {
1078 if ((asprintf(&repo_age, "%lld %s",
1079 (diff_time / 60 / 60 / 24), days)) == -1)
1081 } else if (diff_time > 60 * 60 * 2) {
1082 if ((asprintf(&repo_age, "%lld %s",
1083 (diff_time / 60 / 60), hours)) == -1)
1085 } else if (diff_time > 60 * 2) {
1086 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1089 } else if (diff_time > 2) {
1090 if ((asprintf(&repo_age, "%lld %s", diff_time,
1094 if ((asprintf(&repo_age, "%s", now)) == -1)
1099 if (gmtime_r(&committer_time, &tm) == NULL)
1102 s = asctime_r(&tm, datebuf);
1106 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1114 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
1116 const struct got_error *error = NULL;
1117 struct got_object_id *id = NULL;
1118 struct got_repository *repo = NULL;
1119 struct got_commit_object *commit = NULL;
1120 struct got_reflist_head refs;
1121 struct got_reflist_entry *re;
1122 struct got_reference *head_ref;
1124 time_t committer_time = 0, cmp_time = 0;
1125 const char *refname;
1126 char *repo_age = NULL;
1128 if (repo_ref == NULL)
1131 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1134 SIMPLEQ_INIT(&refs);
1135 if (gw_trans->gw_conf->got_show_repo_age == false) {
1136 if ((asprintf(&repo_age, "")) == -1)
1141 error = got_repo_open(&repo, dir, NULL);
1146 error = got_ref_list(&refs, repo, "refs/heads",
1147 got_ref_cmp_by_name, NULL);
1149 error = got_ref_list(&refs, repo, repo_ref,
1150 got_ref_cmp_by_name, NULL);
1154 SIMPLEQ_FOREACH(re, &refs, entry) {
1156 refname = strdup(repo_ref);
1158 refname = got_ref_get_name(re->ref);
1159 error = got_ref_open(&head_ref, repo, refname, 0);
1163 error = got_ref_resolve(&id, repo, head_ref);
1164 got_ref_close(head_ref);
1168 error = got_object_open_as_commit(&commit, repo, id);
1173 got_object_commit_get_committer_time(commit);
1175 if (cmp_time < committer_time)
1176 cmp_time = committer_time;
1179 if (cmp_time != 0) {
1180 committer_time = cmp_time;
1181 repo_age = gw_get_time_str(committer_time, ref_tm);
1183 if ((asprintf(&repo_age, "")) == -1)
1185 got_ref_list_free(&refs);
1189 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1196 gw_get_repo_owner(struct trans *gw_trans, char *dir)
1199 char *owner = NULL, *d_file = NULL;
1200 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1201 char *comp, *pos, *buf;
1204 if (gw_trans->gw_conf->got_show_repo_owner == false)
1207 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1210 if ((f = fopen(d_file, "r")) == NULL)
1213 if ((buf = calloc(128, sizeof(char *))) == NULL)
1216 while ((fgets(buf, 128, f)) != NULL) {
1217 if ((pos = strstr(buf, gotweb)) != NULL)
1220 if ((pos = strstr(buf, gitweb)) != NULL)
1229 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1234 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1237 for (i = 0; i < 2; i++) {
1238 owner = strsep(&buf, "\"");
1248 if ((asprintf(&owner, "%s", "")) == -1)
1255 gw_get_clone_url(struct trans *gw_trans, char *dir)
1258 char *url = NULL, *d_file = NULL;
1261 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1264 if ((f = fopen(d_file, "r")) == NULL)
1267 fseek(f, 0, SEEK_END);
1269 fseek(f, 0, SEEK_SET);
1271 if ((url = calloc(len, sizeof(char *))) == NULL)
1274 fread(url, 1, len, f);
1281 gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1282 char *start_commit, int limit, int log_type)
1284 const struct got_error *error;
1285 struct got_repository *repo = NULL;
1286 struct got_reflist_head refs;
1287 struct got_reflist_entry *re;
1288 struct got_commit_object *commit = NULL;
1289 struct got_object_id *id1 = NULL, *id2 = NULL;
1290 struct got_object_qid *parent_id;
1291 struct got_commit_graph *graph = NULL;
1292 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1293 *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1294 *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1295 *commit_commit_disp = NULL, *commit_age_diff = NULL,
1296 *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1297 *commit_age_long_disp = NULL, *commit_author = NULL,
1298 *commit_author_disp = NULL, *commit_committer = NULL,
1299 *commit_committer_disp = NULL, *commit_log = NULL,
1300 *commit_log_disp = NULL, *commit_parent = NULL,
1301 *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1302 *log_tree_html = NULL, *log_commit_html = NULL,
1303 *log_diff_html = NULL, *commit_tree = NULL,
1304 *commit_tree_disp = NULL, *log_tag_html = NULL;
1305 char *commit_log0, *newline;
1309 struct buf *diffbuf = NULL;
1310 time_t committer_time;
1312 error = buf_alloc(&diffbuf, 0);
1316 if (search_pattern &&
1317 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB |
1321 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1325 SIMPLEQ_INIT(&refs);
1327 if (start_commit == NULL) {
1328 struct got_reference *head_ref;
1329 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1333 error = got_ref_resolve(&id1, repo, head_ref);
1334 got_ref_close(head_ref);
1338 error = got_object_open_as_commit(&commit, repo, id1);
1340 struct got_reference *ref;
1341 error = got_ref_open(&ref, repo, start_commit, 0);
1342 if (error == NULL) {
1344 error = got_ref_resolve(&id1, repo, ref);
1348 error = got_object_get_type(&obj_type, repo, id1);
1351 if (obj_type == GOT_OBJ_TYPE_TAG) {
1352 struct got_tag_object *tag;
1353 error = got_object_open_as_tag(&tag, repo, id1);
1356 if (got_object_tag_get_object_type(tag) !=
1357 GOT_OBJ_TYPE_COMMIT) {
1358 got_object_tag_close(tag);
1359 error = got_error(GOT_ERR_OBJ_TYPE);
1363 id1 = got_object_id_dup(
1364 got_object_tag_get_object_id(tag));
1366 error = got_error_from_errno(
1367 "got_object_id_dup");
1368 got_object_tag_close(tag);
1371 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1372 error = got_error(GOT_ERR_OBJ_TYPE);
1375 error = got_object_open_as_commit(&commit, repo, id1);
1379 if (commit == NULL) {
1380 error = got_repo_match_object_id_prefix(&id1,
1381 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1385 error = got_repo_match_object_id_prefix(&id1,
1386 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1394 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1400 path = in_repo_path;
1403 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1407 error = got_commit_graph_open(&graph, id1, path, 0, repo);
1411 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1416 error = got_commit_graph_iter_next(&id1, graph);
1418 if (error->code == GOT_ERR_ITER_COMPLETED) {
1422 if (error->code != GOT_ERR_ITER_NEED_MORE)
1424 error = got_commit_graph_fetch_commits(graph, 1, repo,
1434 error = got_object_open_as_commit(&commit, repo, id1);
1438 if (search_pattern) {
1439 error = match_logmsg(&have_match, id1, commit,
1442 got_object_commit_close(commit);
1445 if (have_match == 0) {
1446 got_object_commit_close(commit);
1451 SIMPLEQ_FOREACH(re, &refs, entry) {
1454 struct got_tag_object *tag = NULL;
1457 name = got_ref_get_name(re->ref);
1458 if (strcmp(name, GOT_REF_HEAD) == 0)
1460 if (strncmp(name, "refs/", 5) == 0)
1462 if (strncmp(name, "got/", 4) == 0)
1464 if (strncmp(name, "heads/", 6) == 0)
1466 if (strncmp(name, "remotes/", 8) == 0)
1468 if (strncmp(name, "tags/", 5) == 0) {
1469 error = got_object_open_as_tag(&tag, repo,
1472 if (error->code != GOT_ERR_OBJ_TYPE)
1475 * Ref points at something other
1482 cmp = got_object_id_cmp(tag ?
1483 got_object_tag_get_object_id(tag) : re->id, id1);
1485 got_object_tag_close(tag);
1489 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1490 s ? ", " : "", name)) == -1) {
1491 error = got_error_from_errno("asprintf");
1498 if (refs_str == NULL)
1499 refs_str_disp = strdup("");
1501 if ((asprintf(&refs_str_disp, "(%s)",
1503 error = got_error_from_errno("asprintf");
1509 error = got_object_id_str(&id_str1, id1);
1513 error = got_object_id_str(&treeid,
1514 got_object_commit_get_tree_id(commit));
1518 if (gw_trans->action == GW_COMMIT ||
1519 gw_trans->action == GW_COMMITDIFF) {
1522 got_object_commit_get_parent_ids(commit));
1523 if (parent_id != NULL) {
1524 id2 = got_object_id_dup(parent_id->id);
1526 error = got_object_id_str(&id_str2, id2);
1531 id_str2 = strdup("/dev/null");
1535 got_object_commit_get_committer_time(commit);
1537 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1538 error = got_error_from_errno("asprintf");
1542 if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1543 error = got_error_from_errno("asprintf");
1547 if ((asprintf(&commit_tree_disp, commit_tree_html,
1549 error = got_error_from_errno("asprintf");
1553 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1555 error = got_error_from_errno("asprintf");
1559 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1560 error = got_error_from_errno("asprintf");
1564 if ((asprintf(&commit_commit_disp, commit_commit_html,
1565 commit_commit, refs_str_disp)) == -1) {
1566 error = got_error_from_errno("asprintf");
1570 if ((asprintf(&commit_age_long, "%s",
1571 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1572 error = got_error_from_errno("asprintf");
1576 if ((asprintf(&commit_age_long_disp, commit_age_html,
1577 commit_age_long)) == -1) {
1578 error = got_error_from_errno("asprintf");
1582 if ((asprintf(&commit_age_diff, "%s",
1583 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1584 error = got_error_from_errno("asprintf");
1588 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1589 commit_age_diff)) == -1) {
1590 error = got_error_from_errno("asprintf");
1594 if ((asprintf(&commit_author, "%s",
1595 got_object_commit_get_author(commit))) == -1) {
1596 error = got_error_from_errno("asprintf");
1600 if ((asprintf(&commit_author_disp, commit_author_html,
1601 gw_html_escape(commit_author))) == -1) {
1602 error = got_error_from_errno("asprintf");
1606 if ((asprintf(&commit_committer, "%s",
1607 got_object_commit_get_committer(commit))) == -1) {
1608 error = got_error_from_errno("asprintf");
1612 if ((asprintf(&commit_committer_disp, commit_committer_html,
1613 gw_html_escape(commit_committer))) == -1) {
1614 error = got_error_from_errno("asprintf");
1618 if (strcmp(commit_author, commit_committer) == 0) {
1619 free(commit_committer_disp);
1620 commit_committer_disp = strdup("");
1623 error = got_object_commit_get_logmsg(&commit_log0, commit);
1627 commit_log = commit_log0;
1628 while (*commit_log == '\n')
1633 newline = strchr(commit_log, '\n');
1637 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1638 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1639 id_str1, gw_trans->repo_name, id_str1,
1640 gw_trans->repo_name, id_str1)) == -1) {
1641 error = got_error_from_errno("asprintf");
1645 if ((asprintf(&commit_row, logbriefs_row,
1646 commit_age_diff, commit_author, commit_log,
1647 logbriefs_navs_html)) == -1) {
1648 error = got_error_from_errno("asprintf");
1652 free(logbriefs_navs_html);
1653 logbriefs_navs_html = NULL;
1656 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1657 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1658 id_str1, gw_trans->repo_name, id_str1,
1659 gw_trans->repo_name, id_str1)) == -1) {
1660 error = got_error_from_errno("asprintf");
1664 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1665 commit_author_disp, commit_committer_disp,
1666 commit_age_long_disp, gw_html_escape(commit_log),
1667 logbriefs_navs_html)) == -1) {
1668 error = got_error_from_errno("asprintf");
1672 free(logbriefs_navs_html);
1673 logbriefs_navs_html = NULL;
1676 log_tag_html = strdup("tag log here");
1678 if ((asprintf(&commit_row, log_tag_row,
1679 gw_html_escape(commit_log), log_tag_html)) == -1) {
1680 error = got_error_from_errno("asprintf");
1687 log_tree_html = strdup("log tree here");
1689 if ((asprintf(&commit_row, log_tree_row,
1690 gw_html_escape(commit_log), log_tree_html)) == -1) {
1691 error = got_error_from_errno("asprintf");
1695 free(log_tree_html);
1698 if ((asprintf(&commit_log_disp, commit_log_html,
1699 gw_html_escape(commit_log))) == -1) {
1700 error = got_error_from_errno("asprintf");
1704 log_commit_html = strdup("commit here");
1706 if ((asprintf(&commit_row, log_commit_row,
1707 commit_diff_disp, commit_commit_disp,
1708 commit_tree_disp, commit_author_disp,
1709 commit_committer_disp, commit_age_long_disp,
1710 commit_log_disp, log_commit_html)) == -1) {
1711 error = got_error_from_errno("asprintf");
1714 free(commit_log_disp);
1715 free(log_commit_html);
1719 if ((asprintf(&commit_log_disp, commit_log_html,
1720 gw_html_escape(commit_log))) == -1) {
1721 error = got_error_from_errno("asprintf");
1725 log_diff_html = strdup("diff here");
1727 if ((asprintf(&commit_row, log_diff_row,
1728 commit_diff_disp, commit_commit_disp,
1729 commit_tree_disp, commit_author_disp,
1730 commit_committer_disp, commit_age_long_disp,
1731 commit_log_disp, log_diff_html)) == -1) {
1732 error = got_error_from_errno("asprintf");
1735 free(commit_log_disp);
1736 free(log_diff_html);
1743 error = buf_puts(&newsize, diffbuf, commit_row);
1745 free(commit_parent);
1746 free(commit_diff_disp);
1747 free(commit_tree_disp);
1748 free(commit_age_diff);
1749 free(commit_age_diff_disp);
1750 free(commit_age_long);
1751 free(commit_age_long_disp);
1752 free(commit_author);
1753 free(commit_author_disp);
1754 free(commit_committer);
1755 free(commit_committer_disp);
1758 free(refs_str_disp);
1766 if (error || (limit && --limit == 0))
1773 if (buf_len(diffbuf) > 0) {
1774 error = buf_putc(diffbuf, '\0');
1775 logs = strdup(buf_get(diffbuf));
1780 got_object_commit_close(commit);
1784 got_commit_graph_close(graph);
1786 error = got_repo_close(repo);
1791 khttp_puts(gw_trans->gw_req, "Error: ");
1792 khttp_puts(gw_trans->gw_req, error->msg);
1799 gw_get_repo_tags(struct trans *gw_trans, int limit, int tag_type)
1801 const struct got_error *error = NULL;
1802 struct got_repository *repo = NULL;
1803 struct got_reflist_head refs;
1804 struct got_reflist_entry *re;
1805 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1808 struct buf *diffbuf = NULL;
1811 error = buf_alloc(&diffbuf, 0);
1814 SIMPLEQ_INIT(&refs);
1816 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1820 error = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
1824 SIMPLEQ_FOREACH(re, &refs, entry) {
1825 const char *refname;
1826 char *refstr, *tag_log0, *tag_log, *id_str;
1828 struct got_object_id *id;
1829 struct got_tag_object *tag;
1831 refname = got_ref_get_name(re->ref);
1832 if (strncmp(refname, "refs/tags/", 10) != 0)
1835 refstr = got_ref_to_str(re->ref);
1836 if (refstr == NULL) {
1837 error = got_error_from_errno("got_ref_to_str");
1841 error = got_ref_resolve(&id, repo, re->ref);
1844 error = got_object_open_as_tag(&tag, repo, id);
1849 tagger_time = got_object_tag_get_tagger_time(tag);
1851 error = got_object_id_str(&id_str,
1852 got_object_tag_get_object_id(tag));
1856 tag_log0 = strdup(got_object_tag_get_message(tag));
1858 if (tag_log0 == NULL) {
1859 error = got_error_from_errno("strdup");
1864 while (*tag_log == '\n')
1869 newline = strchr(tag_log, '\n');
1873 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1875 error = got_error_from_errno("asprintf");
1879 if ((asprintf(&tags_navs_disp, tags_navs,
1880 gw_trans->repo_name, id_str, gw_trans->repo_name,
1881 id_str, gw_trans->repo_name, id_str,
1882 gw_trans->repo_name, id_str)) == -1) {
1883 error = got_error_from_errno("asprintf");
1887 if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
1888 tags_navs_disp)) == -1) {
1889 error = got_error_from_errno("asprintf");
1893 free(tags_navs_disp);
1901 got_object_tag_close(tag);
1903 error = buf_puts(&newsize, diffbuf, tag_row);
1911 if (error || (limit && --limit == 0))
1915 if (buf_len(diffbuf) > 0) {
1916 error = buf_putc(diffbuf, '\0');
1917 tags = strdup(buf_get(diffbuf));
1921 got_ref_list_free(&refs);
1923 got_repo_close(repo);
1931 gw_get_repo_heads(struct trans *gw_trans)
1933 const struct got_error *error = NULL;
1934 struct got_repository *repo = NULL;
1935 struct got_reflist_head refs;
1936 struct got_reflist_entry *re;
1937 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
1938 struct buf *diffbuf = NULL;
1941 error = buf_alloc(&diffbuf, 0);
1945 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1949 SIMPLEQ_INIT(&refs);
1950 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
1955 SIMPLEQ_FOREACH(re, &refs, entry) {
1958 refname = strdup(got_ref_get_name(re->ref));
1959 if (refname == NULL) {
1960 error = got_error_from_errno("got_ref_to_str");
1964 if (strncmp(refname, "refs/heads/", 11) != 0) {
1969 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
1972 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
1973 refname, gw_trans->repo_name, refname,
1974 gw_trans->repo_name, refname, gw_trans->repo_name,
1976 error = got_error_from_errno("asprintf");
1980 if (strncmp(refname, "refs/heads/", 11) == 0)
1983 if ((asprintf(&head_row, heads_row, age, refname,
1984 head_navs_disp)) == -1) {
1985 error = got_error_from_errno("asprintf");
1989 error = buf_puts(&newsize, diffbuf, head_row);
1991 free(head_navs_disp);
1995 if (buf_len(diffbuf) > 0) {
1996 error = buf_putc(diffbuf, '\0');
1997 heads = strdup(buf_get(diffbuf));
2001 got_ref_list_free(&refs);
2003 got_repo_close(repo);
2011 gw_get_got_link(struct trans *gw_trans)
2015 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2016 gw_trans->gw_conf->got_logo)) == -1)
2023 gw_get_site_link(struct trans *gw_trans)
2025 char *link, *repo = "", *action = "";
2027 if (gw_trans->repo_name != NULL)
2028 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2029 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2032 if (gw_trans->action_name != NULL)
2033 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2036 if ((asprintf(&link, site_link, GOTWEB,
2037 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2044 gw_html_escape(const char *html)
2046 char *escaped_str = NULL, *buf;
2048 size_t sz, i, buff_sz = 2048;
2050 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2056 if ((sz = strlen(html)) == 0)
2059 /* only work with buff_sz */
2063 for (i = 0; i < sz; i++) {
2067 strcat(buf, ">");
2070 strcat(buf, "&");
2073 strcat(buf, "<");
2076 strcat(buf, """);
2079 strcat(buf, "'");
2082 strcat(buf, "<br />");
2090 asprintf(&escaped_str, "%s", buf);
2098 const struct got_error *error = NULL;
2099 struct trans *gw_trans;
2100 struct gw_dir *dir = NULL, *tdir;
2101 const char *page = "index";
2104 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
2107 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2110 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2113 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2116 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
2118 errx(1, "khttp_parse");
2120 if ((gw_trans->gw_conf =
2121 malloc(sizeof(struct gotweb_conf))) == NULL) {
2123 error = got_error_from_errno("malloc");
2127 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
2128 error = got_error_from_errno("pledge");
2132 TAILQ_INIT(&gw_trans->gw_dirs);
2135 gw_trans->repos_total = 0;
2136 gw_trans->repo_path = NULL;
2137 gw_trans->commit = NULL;
2138 gw_trans->headref = strdup(GOT_REF_HEAD);
2139 gw_trans->mime = KMIME_TEXT_HTML;
2140 gw_trans->gw_tmpl->key = templs;
2141 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2142 gw_trans->gw_tmpl->arg = gw_trans;
2143 gw_trans->gw_tmpl->cb = gw_template;
2144 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2148 gw_trans->mime = KMIME_TEXT_PLAIN;
2149 gw_trans->action = GW_ERR;
2150 gw_display_index(gw_trans, error);
2154 error = gw_parse_querystring(gw_trans);
2158 gw_display_index(gw_trans, error);
2162 free(gw_trans->gw_conf->got_repos_path);
2163 free(gw_trans->gw_conf->got_www_path);
2164 free(gw_trans->gw_conf->got_site_name);
2165 free(gw_trans->gw_conf->got_site_owner);
2166 free(gw_trans->gw_conf->got_site_link);
2167 free(gw_trans->gw_conf->got_logo);
2168 free(gw_trans->gw_conf->got_logo_url);
2169 free(gw_trans->gw_conf);
2170 free(gw_trans->commit);
2171 free(gw_trans->repo_path);
2172 free(gw_trans->repo_name);
2173 free(gw_trans->repo_file);
2174 free(gw_trans->action_name);
2175 free(gw_trans->headref);
2177 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2179 free(dir->description);
2188 khttp_free(gw_trans->gw_req);
2189 return EXIT_SUCCESS;