2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
20 #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>
50 #include "gotweb_ui.h"
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct gw_dir *gw_dir;
59 struct gotweb_conf *gw_conf;
60 struct ktemplate *gw_tmpl;
61 struct khtmlreq *gw_html_req;
72 unsigned int repos_total;
88 TAILQ_ENTRY(gw_dir) entry;
134 static const char *const templs[TEMPL__MAX] = {
144 static const struct kvalid gw_keys[KEY__ZMAX] = {
145 { kvalid_stringne, "action" },
146 { kvalid_stringne, "commit" },
147 { kvalid_stringne, "file" },
148 { kvalid_stringne, "folder" },
149 { kvalid_stringne, "headref" },
150 { kvalid_int, "page" },
151 { kvalid_stringne, "path" },
154 int gw_get_repo_log_count(struct trans *, char *);
156 static struct gw_dir *gw_init_gw_dir(char *);
158 static char *gw_get_repo_description(struct trans *,
160 static char *gw_get_repo_owner(struct trans *,
162 static char *gw_get_time_str(time_t, int);
163 static char *gw_get_repo_age(struct trans *,
164 char *, char *, int);
165 static char *gw_get_repo_log(struct trans *, const char *,
167 static char *gw_get_file_blame(struct trans *, char *);
168 static char *gw_get_repo_tree(struct trans *, char *);
169 static char *gw_get_repo_diff(struct trans *, char *,
171 static char *gw_get_repo_tags(struct trans *, int, int);
172 static char *gw_get_repo_heads(struct trans *);
173 static char *gw_get_clone_url(struct trans *, char *);
174 static char *gw_get_got_link(struct trans *);
175 static char *gw_get_site_link(struct trans *);
176 static char *gw_html_escape(const char *);
177 static char *color_diff_line(char *);
179 static void gw_display_open(struct trans *, enum khttp,
181 static void gw_display_index(struct trans *,
182 const struct got_error *);
184 static int gw_template(size_t, void *);
186 static const struct got_error* apply_unveil(const char *, const char *);
187 static const struct got_error* cmp_tags(void *, int *,
188 struct got_reference *,
189 struct got_reference *);
190 static const struct got_error* resolve_commit_arg(struct got_object_id **,
191 const char *, struct got_repository *);
192 static const struct got_error* match_object_id(struct got_object_id **,
193 char **, const char *r, int, int,
194 struct got_repository *);
195 static const struct got_error* blame_cb(void *, int, int,
196 struct got_object_id *);
197 static const struct got_error* gw_load_got_paths(struct trans *);
198 static const struct got_error* gw_load_got_path(struct trans *,
200 static const struct got_error* gw_parse_querystring(struct trans *);
201 static const struct got_error* match_logmsg(int *, struct got_object_id *,
202 struct got_commit_object *, regex_t *);
204 static const struct got_error* gw_blame(struct trans *);
205 static const struct got_error* gw_blob(struct trans *);
206 static const struct got_error* gw_blobdiff(struct trans *);
207 static const struct got_error* gw_commit(struct trans *);
208 static const struct got_error* gw_commitdiff(struct trans *);
209 static const struct got_error* gw_history(struct trans *);
210 static const struct got_error* gw_index(struct trans *);
211 static const struct got_error* gw_log(struct trans *);
212 static const struct got_error* gw_raw(struct trans *);
213 static const struct got_error* gw_logbriefs(struct trans *);
214 static const struct got_error* gw_snapshot(struct trans *);
215 static const struct got_error* gw_summary(struct trans *);
216 static const struct got_error* gw_tag(struct trans *);
217 static const struct got_error* gw_tree(struct trans *);
219 struct gw_query_action {
220 unsigned int func_id;
221 const char *func_name;
222 const struct got_error *(*func_main)(struct trans *);
226 enum gw_query_actions {
244 static struct gw_query_action gw_query_funcs[] = {
245 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
246 { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
247 { GW_BLOBDIFF, "blobdiff", gw_blobdiff, "gw_tmpl/index.tmpl" },
248 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
249 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
250 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
251 { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
252 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
253 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
254 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
255 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
256 { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
257 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
258 { GW_TAG, "tag", gw_tag, "gw_tmpl/index.tmpl" },
259 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
262 static const struct got_error *
263 apply_unveil(const char *repo_path, const char *repo_file)
265 const struct got_error *err;
267 if (repo_path && repo_file) {
269 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
270 return got_error_from_errno("asprintf unveil");
271 if (unveil(full_path, "r") != 0)
272 return got_error_from_errno2("unveil", full_path);
275 if (repo_path && unveil(repo_path, "r") != 0)
276 return got_error_from_errno2("unveil", repo_path);
278 if (unveil("/tmp", "rwc") != 0)
279 return got_error_from_errno2("unveil", "/tmp");
281 err = got_privsep_unveil_exec_helpers();
285 if (unveil(NULL, NULL) != 0)
286 return got_error_from_errno("unveil");
291 static const struct got_error *
292 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
293 struct got_reference *ref2)
295 const struct got_error *err = NULL;
296 struct got_repository *repo = arg;
297 struct got_object_id *id1, *id2 = NULL;
298 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
303 err = got_ref_resolve(&id1, repo, ref1);
306 err = got_object_open_as_tag(&tag1, repo, id1);
310 err = got_ref_resolve(&id2, repo, ref2);
313 err = got_object_open_as_tag(&tag2, repo, id2);
317 time1 = got_object_tag_get_tagger_time(tag1);
318 time2 = got_object_tag_get_tagger_time(tag2);
320 /* Put latest tags first. */
323 else if (time1 > time2)
326 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
331 got_object_tag_close(tag1);
333 got_object_tag_close(tag2);
337 static const struct got_error *
338 resolve_commit_arg(struct got_object_id **commit_id,
339 const char *commit_id_arg, struct got_repository *repo)
341 const struct got_error *err;
342 struct got_reference *ref;
343 struct got_tag_object *tag;
345 err = got_repo_object_match_tag(&tag, commit_id_arg,
346 GOT_OBJ_TYPE_COMMIT, repo);
348 *commit_id = got_object_id_dup(
349 got_object_tag_get_object_id(tag));
350 if (*commit_id == NULL)
351 err = got_error_from_errno("got_object_id_dup");
352 got_object_tag_close(tag);
354 } else if (err->code != GOT_ERR_NO_OBJ)
357 err = got_ref_open(&ref, repo, commit_id_arg, 0);
359 err = got_ref_resolve(commit_id, repo, ref);
362 if (err->code != GOT_ERR_NOT_REF)
364 err = got_repo_match_object_id_prefix(commit_id,
365 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
370 static const struct got_error *
371 match_object_id(struct got_object_id **id, char **label,
372 const char *id_str, int obj_type, int resolve_tags,
373 struct got_repository *repo)
375 const struct got_error *err;
376 struct got_tag_object *tag;
377 struct got_reference *ref = NULL;
383 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
386 *id = got_object_id_dup(
387 got_object_tag_get_object_id(tag));
389 err = got_error_from_errno("got_object_id_dup");
390 else if (asprintf(label, "refs/tags/%s",
391 got_object_tag_get_name(tag)) == -1) {
392 err = got_error_from_errno("asprintf");
396 got_object_tag_close(tag);
398 } else if (err->code != GOT_ERR_NO_OBJ)
402 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
404 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
406 err = got_ref_open(&ref, repo, id_str, 0);
409 *label = strdup(got_ref_get_name(ref));
410 if (*label == NULL) {
411 err = got_error_from_errno("strdup");
414 err = got_ref_resolve(id, repo, ref);
416 err = got_object_id_str(label, *id);
417 if (*label == NULL) {
418 err = got_error_from_errno("strdup");
429 gw_get_repo_log_count(struct trans *gw_trans, char *start_commit)
431 const struct got_error *error;
432 struct got_repository *repo = NULL;
433 struct got_reflist_head refs;
434 struct got_commit_object *commit = NULL;
435 struct got_object_id *id = NULL;
436 struct got_commit_graph *graph = NULL;
437 char *in_repo_path = NULL, *path = NULL;
440 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
446 if (start_commit == NULL) {
447 struct got_reference *head_ref;
448 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
452 error = got_ref_resolve(&id, repo, head_ref);
453 got_ref_close(head_ref);
457 error = got_object_open_as_commit(&commit, repo, id);
459 struct got_reference *ref;
460 error = got_ref_open(&ref, repo, start_commit, 0);
463 error = got_ref_resolve(&id, repo, ref);
467 error = got_object_get_type(&obj_type, repo, id);
470 if (obj_type == GOT_OBJ_TYPE_TAG) {
471 struct got_tag_object *tag;
472 error = got_object_open_as_tag(&tag, repo, id);
475 if (got_object_tag_get_object_type(tag) !=
476 GOT_OBJ_TYPE_COMMIT) {
477 got_object_tag_close(tag);
478 error = got_error(GOT_ERR_OBJ_TYPE);
482 id = got_object_id_dup(
483 got_object_tag_get_object_id(tag));
485 error = got_error_from_errno(
486 "got_object_id_dup");
487 got_object_tag_close(tag);
490 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
491 error = got_error(GOT_ERR_OBJ_TYPE);
494 error = got_object_open_as_commit(&commit, repo, id);
498 if (commit == NULL) {
499 error = got_repo_match_object_id_prefix(&id,
500 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
504 error = got_repo_match_object_id_prefix(&id,
505 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
510 error = got_object_open_as_commit(&commit, repo, id);
514 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
523 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
527 error = got_commit_graph_open(&graph, path, 0);
531 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
536 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
539 if (error->code == GOT_ERR_ITER_COMPLETED)
553 got_commit_graph_close(graph);
555 error = got_repo_close(repo);
560 khttp_puts(gw_trans->gw_req, "Error: ");
561 khttp_puts(gw_trans->gw_req, error->msg);
567 static const struct got_error *
568 gw_blame(struct trans *gw_trans)
570 const struct got_error *error = NULL;
572 char *log, *log_html;
574 error = apply_unveil(gw_trans->gw_dir->path, NULL);
578 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGBLAME);
580 if (log != NULL && strcmp(log, "") != 0) {
581 if ((asprintf(&log_html, log_blame, log)) == -1)
582 return got_error_from_errno("asprintf");
583 khttp_puts(gw_trans->gw_req, log_html);
590 static const struct got_error *
591 gw_blob(struct trans *gw_trans)
593 const struct got_error *error = NULL;
598 static const struct got_error *
599 gw_blobdiff(struct trans *gw_trans)
601 const struct got_error *error = NULL;
606 static const struct got_error *
607 gw_commit(struct trans *gw_trans)
609 const struct got_error *error = NULL;
610 char *log, *log_html;
612 error = apply_unveil(gw_trans->gw_dir->path, NULL);
616 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
618 if (log != NULL && strcmp(log, "") != 0) {
619 if ((asprintf(&log_html, log_commit, log)) == -1)
620 return got_error_from_errno("asprintf");
621 khttp_puts(gw_trans->gw_req, log_html);
628 static const struct got_error *
629 gw_commitdiff(struct trans *gw_trans)
631 const struct got_error *error = NULL;
632 char *log, *log_html;
634 error = apply_unveil(gw_trans->gw_dir->path, NULL);
638 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
640 if (log != NULL && strcmp(log, "") != 0) {
641 if ((asprintf(&log_html, log_diff, log)) == -1)
642 return got_error_from_errno("asprintf");
643 khttp_puts(gw_trans->gw_req, log_html);
650 static const struct got_error *
651 gw_history(struct trans *gw_trans)
653 const struct got_error *error = NULL;
658 static const struct got_error *
659 gw_index(struct trans *gw_trans)
661 const struct got_error *error = NULL;
662 struct gw_dir *gw_dir = NULL;
663 char *html, *navs, *next, *prev;
664 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
666 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
670 error = gw_load_got_paths(gw_trans);
674 khttp_puts(gw_trans->gw_req, index_projects_header);
676 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
679 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
680 if (gw_trans->page > 0 && (gw_trans->page *
681 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
687 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
688 gw_dir->name, gw_dir->name)) == -1)
689 return got_error_from_errno("asprintf");
691 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
692 gw_dir->description, gw_dir->owner, gw_dir->age,
694 return got_error_from_errno("asprintf");
696 khttp_puts(gw_trans->gw_req, html);
701 if (gw_trans->gw_conf->got_max_repos_display == 0)
704 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
705 khttp_puts(gw_trans->gw_req, np_wrapper_start);
706 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
707 (gw_trans->page > 0) &&
708 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
709 prev_disp == gw_trans->repos_total))
710 khttp_puts(gw_trans->gw_req, np_wrapper_start);
712 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
713 (gw_trans->page > 0) &&
714 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
715 prev_disp == gw_trans->repos_total)) {
716 if ((asprintf(&prev, nav_prev,
717 gw_trans->page - 1)) == -1)
718 return got_error_from_errno("asprintf");
719 khttp_puts(gw_trans->gw_req, prev);
723 khttp_puts(gw_trans->gw_req, div_end);
725 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
726 next_disp == gw_trans->gw_conf->got_max_repos_display &&
727 dir_c != (gw_trans->page + 1) *
728 gw_trans->gw_conf->got_max_repos_display) {
729 if ((asprintf(&next, nav_next,
730 gw_trans->page + 1)) == -1)
731 return got_error_from_errno("calloc");
732 khttp_puts(gw_trans->gw_req, next);
733 khttp_puts(gw_trans->gw_req, div_end);
739 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
740 (gw_trans->page > 0) &&
741 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
742 prev_disp == gw_trans->repos_total))
743 khttp_puts(gw_trans->gw_req, div_end);
750 static const struct got_error *
751 gw_log(struct trans *gw_trans)
753 const struct got_error *error = NULL;
754 char *log, *log_html;
756 error = apply_unveil(gw_trans->gw_dir->path, NULL);
760 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
761 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
763 if (log != NULL && strcmp(log, "") != 0) {
764 if ((asprintf(&log_html, logs, log)) == -1)
765 return got_error_from_errno("asprintf");
766 khttp_puts(gw_trans->gw_req, log_html);
773 static const struct got_error *
774 gw_raw(struct trans *gw_trans)
776 const struct got_error *error = NULL;
781 static const struct got_error *
782 gw_logbriefs(struct trans *gw_trans)
784 const struct got_error *error = NULL;
785 char *log, *log_html;
787 error = apply_unveil(gw_trans->gw_dir->path, NULL);
791 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
792 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
794 if (log != NULL && strcmp(log, "") != 0) {
795 if ((asprintf(&log_html, summary_logbriefs,
797 return got_error_from_errno("asprintf");
798 khttp_puts(gw_trans->gw_req, log_html);
805 static const struct got_error *
806 gw_snapshot(struct trans *gw_trans)
808 const struct got_error *error = NULL;
813 static const struct got_error *
814 gw_summary(struct trans *gw_trans)
816 const struct got_error *error = NULL;
817 char *description_html, *repo_owner_html, *repo_age_html,
818 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
821 error = apply_unveil(gw_trans->gw_dir->path, NULL);
825 khttp_puts(gw_trans->gw_req, summary_wrapper);
826 if (gw_trans->gw_conf->got_show_repo_description) {
827 if (gw_trans->gw_dir->description != NULL &&
828 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
829 if ((asprintf(&description_html, description,
830 gw_trans->gw_dir->description)) == -1)
831 return got_error_from_errno("asprintf");
833 khttp_puts(gw_trans->gw_req, description_html);
834 free(description_html);
838 if (gw_trans->gw_conf->got_show_repo_owner) {
839 if (gw_trans->gw_dir->owner != NULL &&
840 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
841 if ((asprintf(&repo_owner_html, repo_owner,
842 gw_trans->gw_dir->owner)) == -1)
843 return got_error_from_errno("asprintf");
845 khttp_puts(gw_trans->gw_req, repo_owner_html);
846 free(repo_owner_html);
850 if (gw_trans->gw_conf->got_show_repo_age) {
851 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
852 "refs/heads", TM_LONG);
853 if (age != NULL && (strcmp(age, "") != 0)) {
854 if ((asprintf(&repo_age_html, last_change, age)) == -1)
855 return got_error_from_errno("asprintf");
857 khttp_puts(gw_trans->gw_req, repo_age_html);
863 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
864 if (gw_trans->gw_dir->url != NULL &&
865 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
866 if ((asprintf(&cloneurl_html, cloneurl,
867 gw_trans->gw_dir->url)) == -1)
868 return got_error_from_errno("asprintf");
870 khttp_puts(gw_trans->gw_req, cloneurl_html);
874 khttp_puts(gw_trans->gw_req, div_end);
876 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
877 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
878 heads = gw_get_repo_heads(gw_trans);
880 if (log != NULL && strcmp(log, "") != 0) {
881 if ((asprintf(&log_html, summary_logbriefs,
883 return got_error_from_errno("asprintf");
884 khttp_puts(gw_trans->gw_req, log_html);
889 if (tags != NULL && strcmp(tags, "") != 0) {
890 if ((asprintf(&tags_html, summary_tags,
892 return got_error_from_errno("asprintf");
893 khttp_puts(gw_trans->gw_req, tags_html);
898 if (heads != NULL && strcmp(heads, "") != 0) {
899 if ((asprintf(&heads_html, summary_heads,
901 return got_error_from_errno("asprintf");
902 khttp_puts(gw_trans->gw_req, heads_html);
909 static const struct got_error *
910 gw_tag(struct trans *gw_trans)
912 const struct got_error *error = NULL;
913 char *log, *log_html;
915 error = apply_unveil(gw_trans->gw_dir->path, NULL);
919 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTAG);
921 if (log != NULL && strcmp(log, "") != 0) {
922 if ((asprintf(&log_html, log_tag, log)) == -1)
923 return got_error_from_errno("asprintf");
924 khttp_puts(gw_trans->gw_req, log_html);
931 static const struct got_error *
932 gw_tree(struct trans *gw_trans)
934 const struct got_error *error = NULL;
935 char *log, *log_html;
937 error = apply_unveil(gw_trans->gw_dir->path, NULL);
941 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
943 if (log != NULL && strcmp(log, "") != 0) {
944 if ((asprintf(&log_html, log_tree, log)) == -1)
945 return got_error_from_errno("asprintf");
946 khttp_puts(gw_trans->gw_req, log_html);
953 static const struct got_error *
954 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
956 const struct got_error *error = NULL;
961 if ((asprintf(&dir_test, "%s/%s/%s",
962 gw_trans->gw_conf->got_repos_path, gw_dir->name,
963 GOTWEB_GIT_DIR)) == -1)
964 return got_error_from_errno("asprintf");
966 dt = opendir(dir_test);
970 gw_dir->path = strdup(dir_test);
975 if ((asprintf(&dir_test, "%s/%s/%s",
976 gw_trans->gw_conf->got_repos_path, gw_dir->name,
977 GOTWEB_GOT_DIR)) == -1)
978 return got_error_from_errno("asprintf");
980 dt = opendir(dir_test);
985 error = got_error(GOT_ERR_NOT_GIT_REPO);
989 if ((asprintf(&dir_test, "%s/%s",
990 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
991 return got_error_from_errno("asprintf");
993 gw_dir->path = strdup(dir_test);
996 gw_dir->description = gw_get_repo_description(gw_trans,
998 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
999 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
1001 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
1010 static const struct got_error *
1011 gw_load_got_paths(struct trans *gw_trans)
1013 const struct got_error *error = NULL;
1015 struct dirent **sd_dent;
1016 struct gw_dir *gw_dir;
1018 unsigned int d_cnt, d_i;
1020 d = opendir(gw_trans->gw_conf->got_repos_path);
1022 error = got_error_from_errno2("opendir",
1023 gw_trans->gw_conf->got_repos_path);
1027 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1030 error = got_error_from_errno2("scandir",
1031 gw_trans->gw_conf->got_repos_path);
1035 for (d_i = 0; d_i < d_cnt; d_i++) {
1036 if (gw_trans->gw_conf->got_max_repos > 0 &&
1037 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1038 break; /* account for parent and self */
1040 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1041 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1044 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1045 return got_error_from_errno("gw_dir malloc");
1047 error = gw_load_got_path(gw_trans, gw_dir);
1048 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1053 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1054 !got_path_dir_is_empty(gw_dir->path)) {
1055 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1057 gw_trans->repos_total++;
1065 static const struct got_error *
1066 gw_parse_querystring(struct trans *gw_trans)
1068 const struct got_error *error = NULL;
1070 struct gw_query_action *action = NULL;
1073 if (gw_trans->gw_req->fieldnmap[0]) {
1074 error = got_error_from_errno("bad parse");
1076 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1077 /* define gw_trans->repo_path */
1078 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
1079 return got_error_from_errno("asprintf");
1081 if ((asprintf(&gw_trans->repo_path, "%s/%s",
1082 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
1083 return got_error_from_errno("asprintf");
1085 /* get action and set function */
1086 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1087 for (i = 0; i < nitems(gw_query_funcs); i++) {
1088 action = &gw_query_funcs[i];
1089 if (action->func_name == NULL)
1092 if (strcmp(action->func_name,
1093 p->parsed.s) == 0) {
1094 gw_trans->action = i;
1095 if ((asprintf(&gw_trans->action_name,
1096 "%s", action->func_name)) == -1)
1098 got_error_from_errno(
1107 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1108 if ((asprintf(&gw_trans->commit, "%s",
1109 p->parsed.s)) == -1)
1110 return got_error_from_errno("asprintf");
1112 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1113 if ((asprintf(&gw_trans->repo_file, "%s",
1114 p->parsed.s)) == -1)
1115 return got_error_from_errno("asprintf");
1117 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1118 if ((asprintf(&gw_trans->repo_folder, "%s",
1119 p->parsed.s)) == -1)
1120 return got_error_from_errno("asprintf");
1122 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1123 if ((asprintf(&gw_trans->headref, "%s",
1124 p->parsed.s)) == -1)
1125 return got_error_from_errno("asprintf");
1127 if (action == NULL) {
1128 error = got_error_from_errno("invalid action");
1131 if ((gw_trans->gw_dir =
1132 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1133 return got_error_from_errno("gw_dir malloc");
1135 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1139 gw_trans->action = GW_INDEX;
1141 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1142 gw_trans->page = p->parsed.i;
1144 if (gw_trans->action == GW_RAW)
1145 gw_trans->mime = KMIME_TEXT_PLAIN;
1150 static struct gw_dir *
1151 gw_init_gw_dir(char *dir)
1153 struct gw_dir *gw_dir;
1155 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1158 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1164 static const struct got_error*
1165 match_logmsg(int *have_match, struct got_object_id *id,
1166 struct got_commit_object *commit, regex_t *regex)
1168 const struct got_error *err = NULL;
1169 regmatch_t regmatch;
1170 char *id_str = NULL, *logmsg = NULL;
1174 err = got_object_id_str(&id_str, id);
1178 err = got_object_commit_get_logmsg(&logmsg, commit);
1182 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1191 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
1193 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1194 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1196 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1198 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1199 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1200 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1201 khttp_body(gw_trans->gw_req);
1205 gw_display_index(struct trans *gw_trans, const struct got_error *err)
1207 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1208 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1211 khttp_puts(gw_trans->gw_req, err->msg);
1213 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1214 gw_query_funcs[gw_trans->action].template);
1216 khtml_close(gw_trans->gw_html_req);
1220 gw_template(size_t key, void *arg)
1222 const struct got_error *error = NULL;
1223 struct trans *gw_trans = arg;
1224 char *gw_got_link, *gw_site_link;
1225 char *site_owner_name, *site_owner_name_h;
1229 khttp_puts(gw_trans->gw_req, head);
1232 gw_got_link = gw_get_got_link(gw_trans);
1233 if (gw_got_link != NULL)
1234 khttp_puts(gw_trans->gw_req, gw_got_link);
1238 case (TEMPL_SITEPATH):
1239 gw_site_link = gw_get_site_link(gw_trans);
1240 if (gw_site_link != NULL)
1241 khttp_puts(gw_trans->gw_req, gw_site_link);
1246 if (gw_trans->gw_conf->got_site_name != NULL)
1247 khtml_puts(gw_trans->gw_html_req,
1248 gw_trans->gw_conf->got_site_name);
1251 case (TEMPL_SEARCH):
1252 khttp_puts(gw_trans->gw_req, search);
1254 case(TEMPL_SITEOWNER):
1255 if (gw_trans->gw_conf->got_site_owner != NULL &&
1256 gw_trans->gw_conf->got_show_site_owner) {
1258 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1259 if ((asprintf(&site_owner_name_h, site_owner,
1264 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1265 free(site_owner_name);
1266 free(site_owner_name_h);
1269 case(TEMPL_CONTENT):
1270 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1272 khttp_puts(gw_trans->gw_req, error->msg);
1283 gw_get_repo_description(struct trans *gw_trans, char *dir)
1286 char *description = NULL, *d_file = NULL;
1289 if (gw_trans->gw_conf->got_show_repo_description == false)
1292 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1295 if ((f = fopen(d_file, "r")) == NULL)
1298 fseek(f, 0, SEEK_END);
1300 fseek(f, 0, SEEK_SET);
1301 if ((description = calloc(len, sizeof(char *))) == NULL)
1304 fread(description, 1, len, f);
1309 if ((asprintf(&description, "%s", "")) == -1)
1316 gw_get_time_str(time_t committer_time, int ref_tm)
1320 char *years = "years ago", *months = "months ago";
1321 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1322 char *minutes = "minutes ago", *seconds = "seconds ago";
1323 char *now = "right now";
1329 diff_time = time(NULL) - committer_time;
1330 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1331 if ((asprintf(&repo_age, "%lld %s",
1332 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1334 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1335 if ((asprintf(&repo_age, "%lld %s",
1336 (diff_time / 60 / 60 / 24 / (365 / 12)),
1339 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1340 if ((asprintf(&repo_age, "%lld %s",
1341 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1343 } else if (diff_time > 60 * 60 * 24 * 2) {
1344 if ((asprintf(&repo_age, "%lld %s",
1345 (diff_time / 60 / 60 / 24), days)) == -1)
1347 } else if (diff_time > 60 * 60 * 2) {
1348 if ((asprintf(&repo_age, "%lld %s",
1349 (diff_time / 60 / 60), hours)) == -1)
1351 } else if (diff_time > 60 * 2) {
1352 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1355 } else if (diff_time > 2) {
1356 if ((asprintf(&repo_age, "%lld %s", diff_time,
1360 if ((asprintf(&repo_age, "%s", now)) == -1)
1365 if (gmtime_r(&committer_time, &tm) == NULL)
1368 s = asctime_r(&tm, datebuf);
1372 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1380 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
1382 const struct got_error *error = NULL;
1383 struct got_object_id *id = NULL;
1384 struct got_repository *repo = NULL;
1385 struct got_commit_object *commit = NULL;
1386 struct got_reflist_head refs;
1387 struct got_reflist_entry *re;
1388 struct got_reference *head_ref;
1390 time_t committer_time = 0, cmp_time = 0;
1391 const char *refname;
1392 char *repo_age = NULL;
1394 if (repo_ref == NULL)
1397 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1400 SIMPLEQ_INIT(&refs);
1401 if (gw_trans->gw_conf->got_show_repo_age == false) {
1402 if ((asprintf(&repo_age, "")) == -1)
1407 error = got_repo_open(&repo, dir, NULL);
1412 error = got_ref_list(&refs, repo, "refs/heads",
1413 got_ref_cmp_by_name, NULL);
1415 error = got_ref_list(&refs, repo, repo_ref,
1416 got_ref_cmp_by_name, NULL);
1420 SIMPLEQ_FOREACH(re, &refs, entry) {
1422 refname = strdup(repo_ref);
1424 refname = got_ref_get_name(re->ref);
1425 error = got_ref_open(&head_ref, repo, refname, 0);
1429 error = got_ref_resolve(&id, repo, head_ref);
1430 got_ref_close(head_ref);
1434 error = got_object_open_as_commit(&commit, repo, id);
1439 got_object_commit_get_committer_time(commit);
1441 if (cmp_time < committer_time)
1442 cmp_time = committer_time;
1445 if (cmp_time != 0) {
1446 committer_time = cmp_time;
1447 repo_age = gw_get_time_str(committer_time, ref_tm);
1449 if ((asprintf(&repo_age, "")) == -1)
1451 got_ref_list_free(&refs);
1455 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1462 gw_get_repo_diff(struct trans *gw_trans, char *id_str1, char *id_str2)
1464 const struct got_error *error;
1466 struct got_object_id *id1 = NULL, *id2 = NULL;
1467 struct got_repository *repo = NULL;
1468 struct buf *diffbuf = NULL;
1469 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1478 error = buf_alloc(&diffbuf, 0);
1482 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1486 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
1492 error = match_object_id(&id2, &label2, id_str2,
1493 GOT_OBJ_TYPE_ANY, 1, repo);
1497 error = got_object_get_type(&type2, repo, id2);
1502 error = got_object_get_type(&type1, repo, id1);
1506 if (id_str2 && type1 != type2) {
1507 error = got_error(GOT_ERR_OBJ_TYPE);
1512 case GOT_OBJ_TYPE_BLOB:
1513 error = got_diff_objects_as_blobs(id2, id1, NULL, NULL, 3, 0,
1516 case GOT_OBJ_TYPE_TREE:
1517 error = got_diff_objects_as_trees(id2, id1, "", "", 3, 0, repo,
1520 case GOT_OBJ_TYPE_COMMIT:
1521 error = got_diff_objects_as_commits(id2, id1, 3, 0, repo, f);
1524 error = got_error(GOT_ERR_OBJ_TYPE);
1527 if ((buf = calloc(128, sizeof(char *))) == NULL)
1530 fseek(f, 0, SEEK_SET);
1532 while ((fgets(buf, 128, f)) != NULL) {
1533 buf_color = color_diff_line(buf);
1534 error = buf_puts(&newsize, diffbuf, buf_color);
1538 error = buf_puts(&newsize, diffbuf, div_end);
1543 if (buf_len(diffbuf) > 0) {
1544 error = buf_putc(diffbuf, '\0');
1545 diff_html = strdup(buf_get(diffbuf));
1557 got_repo_close(repo);
1566 gw_get_repo_owner(struct trans *gw_trans, char *dir)
1569 char *owner = NULL, *d_file = NULL;
1570 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1571 char *comp, *pos, *buf;
1574 if (gw_trans->gw_conf->got_show_repo_owner == false)
1577 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1580 if ((f = fopen(d_file, "r")) == NULL)
1583 if ((buf = calloc(128, sizeof(char *))) == NULL)
1586 while ((fgets(buf, 128, f)) != NULL) {
1587 if ((pos = strstr(buf, gotweb)) != NULL)
1590 if ((pos = strstr(buf, gitweb)) != NULL)
1599 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1604 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1607 for (i = 0; i < 2; i++) {
1608 owner = strsep(&buf, "\"");
1618 if ((asprintf(&owner, "%s", "")) == -1)
1625 gw_get_clone_url(struct trans *gw_trans, char *dir)
1628 char *url = NULL, *d_file = NULL;
1631 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1634 if ((f = fopen(d_file, "r")) == NULL)
1637 fseek(f, 0, SEEK_END);
1639 fseek(f, 0, SEEK_SET);
1641 if ((url = calloc(len, sizeof(char *))) == NULL)
1644 fread(url, 1, len, f);
1651 gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1652 char *start_commit, int limit, int log_type)
1654 const struct got_error *error;
1655 struct got_repository *repo = NULL;
1656 struct got_reflist_head refs;
1657 struct got_reflist_entry *re;
1658 struct got_commit_object *commit = NULL;
1659 struct got_object_id *id1 = NULL, *id2 = NULL;
1660 struct got_object_qid *parent_id;
1661 struct got_commit_graph *graph = NULL;
1662 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1663 *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1664 *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1665 *commit_commit_disp = NULL, *commit_age_diff = NULL,
1666 *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1667 *commit_age_long_disp = NULL, *commit_author = NULL,
1668 *commit_author_disp = NULL, *commit_committer = NULL,
1669 *commit_committer_disp = NULL, *commit_log = NULL,
1670 *commit_log_disp = NULL, *commit_parent = NULL,
1671 *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1672 *log_tree_html = NULL, *log_commit_html = NULL,
1673 *log_diff_html = NULL, *commit_tree = NULL,
1674 *commit_tree_disp = NULL, *log_tag_html = NULL,
1675 *log_blame_html = NULL;
1676 char *commit_log0, *newline;
1678 int have_match, log_count = 0, has_parent = 1;
1680 struct buf *diffbuf = NULL;
1681 time_t committer_time;
1683 if (gw_trans->action == GW_LOG || gw_trans->action == GW_LOGBRIEFS)
1684 log_count = gw_get_repo_log_count(gw_trans, start_commit);
1686 error = buf_alloc(&diffbuf, 0);
1690 if (search_pattern &&
1691 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB |
1695 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1699 SIMPLEQ_INIT(&refs);
1701 if (start_commit == NULL) {
1702 struct got_reference *head_ref;
1703 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1707 error = got_ref_resolve(&id1, repo, head_ref);
1708 got_ref_close(head_ref);
1712 error = got_object_open_as_commit(&commit, repo, id1);
1714 struct got_reference *ref;
1715 error = got_ref_open(&ref, repo, start_commit, 0);
1716 if (error == NULL) {
1718 error = got_ref_resolve(&id1, repo, ref);
1722 error = got_object_get_type(&obj_type, repo, id1);
1725 if (obj_type == GOT_OBJ_TYPE_TAG) {
1726 struct got_tag_object *tag;
1727 error = got_object_open_as_tag(&tag, repo, id1);
1730 if (got_object_tag_get_object_type(tag) !=
1731 GOT_OBJ_TYPE_COMMIT) {
1732 got_object_tag_close(tag);
1733 error = got_error(GOT_ERR_OBJ_TYPE);
1737 id1 = got_object_id_dup(
1738 got_object_tag_get_object_id(tag));
1740 error = got_error_from_errno(
1741 "got_object_id_dup");
1742 got_object_tag_close(tag);
1745 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1746 error = got_error(GOT_ERR_OBJ_TYPE);
1749 error = got_object_open_as_commit(&commit, repo, id1);
1753 if (commit == NULL) {
1754 error = got_repo_match_object_id_prefix(&id1,
1755 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1759 error = got_repo_match_object_id_prefix(&id1,
1760 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1766 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1772 path = in_repo_path;
1775 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1779 error = got_commit_graph_open(&graph, path, 0);
1783 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1788 error = got_commit_graph_iter_next(&id1, graph, repo, NULL,
1791 if (error->code == GOT_ERR_ITER_COMPLETED)
1798 error = got_object_open_as_commit(&commit, repo, id1);
1802 if (search_pattern) {
1803 error = match_logmsg(&have_match, id1, commit,
1806 got_object_commit_close(commit);
1809 if (have_match == 0) {
1810 got_object_commit_close(commit);
1815 SIMPLEQ_FOREACH(re, &refs, entry) {
1818 struct got_tag_object *tag = NULL;
1821 name = got_ref_get_name(re->ref);
1822 if (strcmp(name, GOT_REF_HEAD) == 0)
1824 if (strncmp(name, "refs/", 5) == 0)
1826 if (strncmp(name, "got/", 4) == 0)
1828 if (strncmp(name, "heads/", 6) == 0)
1830 if (strncmp(name, "remotes/", 8) == 0)
1832 if (strncmp(name, "tags/", 5) == 0) {
1833 error = got_object_open_as_tag(&tag, repo,
1836 if (error->code != GOT_ERR_OBJ_TYPE)
1839 * Ref points at something other
1846 cmp = got_object_id_cmp(tag ?
1847 got_object_tag_get_object_id(tag) : re->id, id1);
1849 got_object_tag_close(tag);
1853 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1854 s ? ", " : "", name)) == -1) {
1855 error = got_error_from_errno("asprintf");
1862 if (refs_str == NULL)
1863 refs_str_disp = strdup("");
1865 if ((asprintf(&refs_str_disp, "(%s)",
1867 error = got_error_from_errno("asprintf");
1873 error = got_object_id_str(&id_str1, id1);
1877 error = got_object_id_str(&treeid,
1878 got_object_commit_get_tree_id(commit));
1882 if (gw_trans->action == GW_COMMIT ||
1883 gw_trans->action == GW_COMMITDIFF) {
1886 got_object_commit_get_parent_ids(commit));
1887 if (parent_id != NULL) {
1888 id2 = got_object_id_dup(parent_id->id);
1890 error = got_object_id_str(&id_str2, id2);
1896 id_str2 = strdup("/dev/null");
1901 got_object_commit_get_committer_time(commit);
1903 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1904 error = got_error_from_errno("asprintf");
1908 if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1909 error = got_error_from_errno("asprintf");
1913 if ((asprintf(&commit_tree_disp, commit_tree_html,
1915 error = got_error_from_errno("asprintf");
1919 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1921 error = got_error_from_errno("asprintf");
1925 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1926 error = got_error_from_errno("asprintf");
1930 if ((asprintf(&commit_commit_disp, commit_commit_html,
1931 commit_commit, refs_str_disp)) == -1) {
1932 error = got_error_from_errno("asprintf");
1936 if ((asprintf(&commit_age_long, "%s",
1937 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1938 error = got_error_from_errno("asprintf");
1942 if ((asprintf(&commit_age_long_disp, commit_age_html,
1943 commit_age_long)) == -1) {
1944 error = got_error_from_errno("asprintf");
1948 if ((asprintf(&commit_age_diff, "%s",
1949 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1950 error = got_error_from_errno("asprintf");
1954 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1955 commit_age_diff)) == -1) {
1956 error = got_error_from_errno("asprintf");
1960 if ((asprintf(&commit_author, "%s",
1961 got_object_commit_get_author(commit))) == -1) {
1962 error = got_error_from_errno("asprintf");
1966 if ((asprintf(&commit_author_disp, commit_author_html,
1967 gw_html_escape(commit_author))) == -1) {
1968 error = got_error_from_errno("asprintf");
1972 if ((asprintf(&commit_committer, "%s",
1973 got_object_commit_get_committer(commit))) == -1) {
1974 error = got_error_from_errno("asprintf");
1978 if ((asprintf(&commit_committer_disp, commit_committer_html,
1979 gw_html_escape(commit_committer))) == -1) {
1980 error = got_error_from_errno("asprintf");
1984 if (strcmp(commit_author, commit_committer) == 0) {
1985 free(commit_committer_disp);
1986 commit_committer_disp = strdup("");
1989 error = got_object_commit_get_logmsg(&commit_log0, commit);
1993 commit_log = commit_log0;
1994 while (*commit_log == '\n')
1999 newline = strchr(commit_log, '\n');
2003 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
2004 gw_trans->repo_name, id_str1, gw_trans->repo_name,
2005 id_str1, gw_trans->repo_name, id_str1,
2006 gw_trans->repo_name, id_str1)) == -1) {
2007 error = got_error_from_errno("asprintf");
2011 if ((asprintf(&commit_row, logbriefs_row,
2012 commit_age_diff, commit_author, commit_log,
2013 logbriefs_navs_html)) == -1) {
2014 error = got_error_from_errno("asprintf");
2018 free(logbriefs_navs_html);
2019 logbriefs_navs_html = NULL;
2022 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
2023 gw_trans->repo_name, id_str1, gw_trans->repo_name,
2024 id_str1, gw_trans->repo_name, id_str1,
2025 gw_trans->repo_name, id_str1)) == -1) {
2026 error = got_error_from_errno("asprintf");
2030 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
2031 commit_author_disp, commit_committer_disp,
2032 commit_age_long_disp, gw_html_escape(commit_log),
2033 logbriefs_navs_html)) == -1) {
2034 error = got_error_from_errno("asprintf");
2038 free(logbriefs_navs_html);
2039 logbriefs_navs_html = NULL;
2042 log_tag_html = strdup("tag log here");
2044 if ((asprintf(&commit_row, log_tag_row,
2045 gw_html_escape(commit_log), log_tag_html)) == -1) {
2046 error = got_error_from_errno("asprintf");
2053 log_blame_html = gw_get_file_blame(gw_trans,
2056 if ((asprintf(&commit_row, log_blame_row,
2057 gw_html_escape(commit_log), log_blame_html)) == -1) {
2058 error = got_error_from_errno("asprintf");
2062 free(log_blame_html);
2065 log_tree_html = gw_get_repo_tree(gw_trans,
2068 if ((asprintf(&commit_row, log_tree_row,
2069 gw_html_escape(commit_log), log_tree_html)) == -1) {
2070 error = got_error_from_errno("asprintf");
2074 free(log_tree_html);
2077 if ((asprintf(&commit_log_disp, commit_log_html,
2078 gw_html_escape(commit_log))) == -1) {
2079 error = got_error_from_errno("asprintf");
2083 log_commit_html = strdup("commit here");
2085 if ((asprintf(&commit_row, log_commit_row,
2086 commit_diff_disp, commit_commit_disp,
2087 commit_tree_disp, commit_author_disp,
2088 commit_committer_disp, commit_age_long_disp,
2089 commit_log_disp, log_commit_html)) == -1) {
2090 error = got_error_from_errno("asprintf");
2093 free(commit_log_disp);
2094 free(log_commit_html);
2098 if ((asprintf(&commit_log_disp, commit_log_html,
2099 gw_html_escape(commit_log))) == -1) {
2100 error = got_error_from_errno("asprintf");
2105 log_diff_html = gw_get_repo_diff(gw_trans,
2106 commit_commit, commit_parent);
2108 log_diff_html = gw_get_repo_diff(gw_trans,
2109 commit_commit, NULL);
2111 if ((asprintf(&commit_row, log_diff_row,
2112 commit_diff_disp, commit_commit_disp,
2113 commit_tree_disp, commit_author_disp,
2114 commit_committer_disp, commit_age_long_disp,
2115 commit_log_disp, log_diff_html)) == -1) {
2116 error = got_error_from_errno("asprintf");
2119 free(commit_log_disp);
2120 free(log_diff_html);
2127 error = buf_puts(&newsize, diffbuf, commit_row);
2129 free(commit_parent);
2130 free(commit_diff_disp);
2131 free(commit_tree_disp);
2132 free(commit_age_diff);
2133 free(commit_age_diff_disp);
2134 free(commit_age_long);
2135 free(commit_age_long_disp);
2136 free(commit_author);
2137 free(commit_author_disp);
2138 free(commit_committer);
2139 free(commit_committer_disp);
2142 free(refs_str_disp);
2150 if (error || (limit && --limit == 0))
2157 if (buf_len(diffbuf) > 0) {
2158 error = buf_putc(diffbuf, '\0');
2159 logs = strdup(buf_get(diffbuf));
2165 got_object_commit_close(commit);
2169 got_commit_graph_close(graph);
2171 error = got_repo_close(repo);
2176 khttp_puts(gw_trans->gw_req, "Error: ");
2177 khttp_puts(gw_trans->gw_req, error->msg);
2184 gw_get_repo_tags(struct trans *gw_trans, int limit, int tag_type)
2186 const struct got_error *error = NULL;
2187 struct got_repository *repo = NULL;
2188 struct got_reflist_head refs;
2189 struct got_reflist_entry *re;
2190 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
2193 struct buf *diffbuf = NULL;
2196 error = buf_alloc(&diffbuf, 0);
2199 SIMPLEQ_INIT(&refs);
2201 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2205 error = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
2209 SIMPLEQ_FOREACH(re, &refs, entry) {
2210 const char *refname;
2211 char *refstr, *tag_log0, *tag_log, *id_str;
2213 struct got_object_id *id;
2214 struct got_tag_object *tag;
2216 refname = got_ref_get_name(re->ref);
2217 if (strncmp(refname, "refs/tags/", 10) != 0)
2220 refstr = got_ref_to_str(re->ref);
2221 if (refstr == NULL) {
2222 error = got_error_from_errno("got_ref_to_str");
2226 error = got_ref_resolve(&id, repo, re->ref);
2229 error = got_object_open_as_tag(&tag, repo, id);
2234 tagger_time = got_object_tag_get_tagger_time(tag);
2236 error = got_object_id_str(&id_str,
2237 got_object_tag_get_object_id(tag));
2241 tag_log0 = strdup(got_object_tag_get_message(tag));
2243 if (tag_log0 == NULL) {
2244 error = got_error_from_errno("strdup");
2249 while (*tag_log == '\n')
2254 newline = strchr(tag_log, '\n');
2258 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
2260 error = got_error_from_errno("asprintf");
2264 if ((asprintf(&tags_navs_disp, tags_navs,
2265 gw_trans->repo_name, id_str, gw_trans->repo_name,
2266 id_str, gw_trans->repo_name, id_str,
2267 gw_trans->repo_name, id_str)) == -1) {
2268 error = got_error_from_errno("asprintf");
2272 if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
2273 tags_navs_disp)) == -1) {
2274 error = got_error_from_errno("asprintf");
2278 free(tags_navs_disp);
2286 got_object_tag_close(tag);
2288 error = buf_puts(&newsize, diffbuf, tag_row);
2296 if (error || (limit && --limit == 0))
2300 if (buf_len(diffbuf) > 0) {
2301 error = buf_putc(diffbuf, '\0');
2302 tags = strdup(buf_get(diffbuf));
2306 got_ref_list_free(&refs);
2308 got_repo_close(repo);
2319 char datebuf[11]; /* YYYY-MM-DD + NUL */
2322 struct blame_cb_args {
2323 struct blame_line *lines;
2327 off_t *line_offsets;
2329 struct got_repository *repo;
2330 struct trans *gw_trans;
2331 struct buf *blamebuf;
2334 static const struct got_error *
2335 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2337 const struct got_error *err = NULL;
2338 struct blame_cb_args *a = arg;
2339 struct blame_line *bline;
2341 size_t linesize = 0, newsize;
2342 struct got_commit_object *commit = NULL;
2345 time_t committer_time;
2347 if (nlines != a->nlines ||
2348 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2349 return got_error(GOT_ERR_RANGE);
2352 return NULL; /* no change in this commit */
2354 /* Annotate this line. */
2355 bline = &a->lines[lineno - 1];
2356 if (bline->annotated)
2358 err = got_object_id_str(&bline->id_str, id);
2362 err = got_object_open_as_commit(&commit, a->repo, id);
2366 bline->committer = strdup(got_object_commit_get_committer(commit));
2367 if (bline->committer == NULL) {
2368 err = got_error_from_errno("strdup");
2372 committer_time = got_object_commit_get_committer_time(commit);
2373 if (localtime_r(&committer_time, &tm) == NULL)
2374 return got_error_from_errno("localtime_r");
2375 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2376 &tm) >= sizeof(bline->datebuf)) {
2377 err = got_error(GOT_ERR_NO_SPACE);
2380 bline->annotated = 1;
2382 /* Print lines annotated so far. */
2383 bline = &a->lines[a->lineno_cur - 1];
2384 if (!bline->annotated)
2387 offset = a->line_offsets[a->lineno_cur - 1];
2388 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2389 err = got_error_from_errno("fseeko");
2393 while (bline->annotated) {
2394 char *smallerthan, *at, *nl, *committer, *blame_row = NULL;
2397 if (getline(&line, &linesize, a->f) == -1) {
2399 err = got_error_from_errno("getline");
2403 committer = bline->committer;
2404 smallerthan = strchr(committer, '<');
2405 if (smallerthan && smallerthan[1] != '\0')
2406 committer = smallerthan + 1;
2407 at = strchr(committer, '@');
2410 len = strlen(committer);
2412 committer[8] = '\0';
2414 nl = strchr(line, '\n');
2417 asprintf(&blame_row, log_blame_line, a->nlines_prec,
2418 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2421 err = buf_puts(&newsize, a->blamebuf, blame_row);
2425 bline = &a->lines[a->lineno_cur - 1];
2430 got_object_commit_close(commit);
2436 gw_get_file_blame(struct trans *gw_trans, char *commit_str)
2438 const struct got_error *error = NULL;
2439 struct got_repository *repo = NULL;
2440 struct got_object_id *obj_id = NULL;
2441 struct got_object_id *commit_id = NULL;
2442 struct got_blob_object *blob = NULL;
2443 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2444 *blame_row = NULL, *id_str, *folder = NULL;
2445 struct blame_cb_args bca;
2446 int nentries, i, obj_type;
2449 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2453 if (gw_trans->repo_folder != NULL) {
2454 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2455 error = got_error_from_errno("asprintf");
2459 folder = strdup("");
2461 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2462 error = got_error_from_errno("asprintf");
2467 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2471 error = resolve_commit_arg(&commit_id, commit_str, repo);
2475 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2479 if (obj_id == NULL) {
2480 error = got_error(GOT_ERR_NO_OBJ);
2484 error = got_object_get_type(&obj_type, repo, obj_id);
2488 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2489 error = got_error(GOT_ERR_OBJ_TYPE);
2493 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2497 error = buf_alloc(&bca.blamebuf, 0);
2501 bca.f = got_opentemp();
2502 if (bca.f == NULL) {
2503 error = got_error_from_errno("got_opentemp");
2506 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2507 &bca.line_offsets, bca.f, blob);
2508 if (error || bca.nlines == 0)
2511 /* Don't include \n at EOF in the blame line count. */
2512 if (bca.line_offsets[bca.nlines - 1] == filesize)
2515 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2516 if (bca.lines == NULL) {
2517 error = got_error_from_errno("calloc");
2521 bca.nlines_prec = 0;
2528 bca.gw_trans = gw_trans;
2530 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca, NULL,
2532 if (buf_len(bca.blamebuf) > 0) {
2533 error = buf_putc(bca.blamebuf, '\0');
2534 blame_html = strdup(buf_get(bca.blamebuf));
2544 error = got_object_blob_close(blob);
2546 error = got_repo_close(repo);
2550 for (i = 0; i < bca.nlines; i++) {
2551 struct blame_line *bline = &bca.lines[i];
2552 free(bline->id_str);
2553 free(bline->committer);
2557 free(bca.line_offsets);
2558 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2559 error = got_error_from_errno("fclose");
2567 gw_get_repo_tree(struct trans *gw_trans, char *commit_str)
2569 const struct got_error *error = NULL;
2570 struct got_repository *repo = NULL;
2571 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2572 struct got_tree_object *tree = NULL;
2573 struct buf *diffbuf = NULL;
2575 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2576 *tree_row = NULL, *id_str;
2579 error = buf_alloc(&diffbuf, 0);
2583 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2587 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2591 if (gw_trans->repo_folder != NULL)
2592 path = strdup(gw_trans->repo_folder);
2593 else if (in_repo_path) {
2595 path = in_repo_path;
2598 if (commit_str == NULL) {
2599 struct got_reference *head_ref;
2600 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2604 error = got_ref_resolve(&commit_id, repo, head_ref);
2605 got_ref_close(head_ref);
2608 error = resolve_commit_arg(&commit_id, commit_str, repo);
2612 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2616 error = got_object_open_as_tree(&tree, repo, tree_id);
2620 nentries = got_object_tree_get_nentries(tree);
2622 for (i = 0; i < nentries; i++) {
2623 struct got_tree_entry *te;
2624 const char *modestr = "";
2625 char *id = NULL, *url_html = NULL;
2627 te = got_object_tree_get_entry(tree, i);
2629 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2633 if ((asprintf(&id, "%s", id_str)) == -1) {
2634 error = got_error_from_errno("asprintf");
2639 mode_t mode = got_tree_entry_get_mode(te);
2641 if (got_object_tree_entry_is_submodule(te))
2643 else if (S_ISLNK(mode))
2645 else if (S_ISDIR(mode))
2647 else if (mode & S_IXUSR)
2650 char *build_folder = NULL;
2651 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2652 if (gw_trans->repo_folder != NULL) {
2653 if ((asprintf(&build_folder, "%s/%s",
2654 gw_trans->repo_folder,
2655 got_tree_entry_get_name(te))) == -1) {
2657 got_error_from_errno("asprintf");
2661 if (asprintf(&build_folder, "%s",
2662 got_tree_entry_get_name(te)) == -1)
2666 if ((asprintf(&url_html, folder_html,
2667 gw_trans->repo_name, gw_trans->action_name,
2668 gw_trans->commit, build_folder,
2669 got_tree_entry_get_name(te), modestr)) == -1) {
2670 error = got_error_from_errno("asprintf");
2674 if (gw_trans->repo_folder != NULL) {
2675 if ((asprintf(&build_folder, "%s",
2676 gw_trans->repo_folder)) == -1) {
2678 got_error_from_errno("asprintf");
2682 build_folder = strdup("");
2684 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2685 "blame", gw_trans->commit,
2686 got_tree_entry_get_name(te), build_folder,
2687 got_tree_entry_get_name(te), modestr)) == -1) {
2688 error = got_error_from_errno("asprintf");
2697 if ((asprintf(&tree_row, trees_row, "", url_html)) == -1) {
2698 error = got_error_from_errno("asprintf");
2701 error = buf_puts(&newsize, diffbuf, tree_row);
2711 if (buf_len(diffbuf) > 0) {
2712 error = buf_putc(diffbuf, '\0');
2713 tree_html = strdup(buf_get(diffbuf));
2717 got_object_tree_close(tree);
2719 got_repo_close(repo);
2731 gw_get_repo_heads(struct trans *gw_trans)
2733 const struct got_error *error = NULL;
2734 struct got_repository *repo = NULL;
2735 struct got_reflist_head refs;
2736 struct got_reflist_entry *re;
2737 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2738 struct buf *diffbuf = NULL;
2741 error = buf_alloc(&diffbuf, 0);
2745 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2749 SIMPLEQ_INIT(&refs);
2750 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2755 SIMPLEQ_FOREACH(re, &refs, entry) {
2758 refname = strdup(got_ref_get_name(re->ref));
2759 if (refname == NULL) {
2760 error = got_error_from_errno("got_ref_to_str");
2764 if (strncmp(refname, "refs/heads/", 11) != 0) {
2769 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2772 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2773 refname, gw_trans->repo_name, refname,
2774 gw_trans->repo_name, refname, gw_trans->repo_name,
2776 error = got_error_from_errno("asprintf");
2780 if (strncmp(refname, "refs/heads/", 11) == 0)
2783 if ((asprintf(&head_row, heads_row, age, refname,
2784 head_navs_disp)) == -1) {
2785 error = got_error_from_errno("asprintf");
2789 error = buf_puts(&newsize, diffbuf, head_row);
2791 free(head_navs_disp);
2795 if (buf_len(diffbuf) > 0) {
2796 error = buf_putc(diffbuf, '\0');
2797 heads = strdup(buf_get(diffbuf));
2801 got_ref_list_free(&refs);
2803 got_repo_close(repo);
2811 gw_get_got_link(struct trans *gw_trans)
2815 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2816 gw_trans->gw_conf->got_logo)) == -1)
2823 gw_get_site_link(struct trans *gw_trans)
2825 char *link, *repo = "", *action = "";
2827 if (gw_trans->repo_name != NULL)
2828 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2829 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2832 if (gw_trans->action_name != NULL)
2833 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2836 if ((asprintf(&link, site_link, GOTWEB,
2837 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2844 color_diff_line(char *buf)
2846 const struct got_error *error = NULL;
2847 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2848 struct buf *diffbuf = NULL;
2851 error = buf_alloc(&diffbuf, 0);
2855 if (strncmp(buf, "-", 1) == 0)
2856 color = "diff_minus";
2857 if (strncmp(buf, "+", 1) == 0)
2858 color = "diff_plus";
2859 if (strncmp(buf, "@@", 2) == 0)
2860 color = "diff_chunk_header";
2861 if (strncmp(buf, "@@", 2) == 0)
2862 color = "diff_chunk_header";
2863 if (strncmp(buf, "commit +", 8) == 0)
2864 color = "diff_meta";
2865 if (strncmp(buf, "commit -", 8) == 0)
2866 color = "diff_meta";
2867 if (strncmp(buf, "blob +", 6) == 0)
2868 color = "diff_meta";
2869 if (strncmp(buf, "blob -", 6) == 0)
2870 color = "diff_meta";
2871 if (strncmp(buf, "file +", 6) == 0)
2872 color = "diff_meta";
2873 if (strncmp(buf, "file -", 6) == 0)
2874 color = "diff_meta";
2875 if (strncmp(buf, "from:", 5) == 0)
2876 color = "diff_author";
2877 if (strncmp(buf, "via:", 4) == 0)
2878 color = "diff_author";
2879 if (strncmp(buf, "date:", 5) == 0)
2880 color = "diff_date";
2882 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2885 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2889 error = buf_puts(&newsize, diffbuf, buf);
2893 if (buf_len(diffbuf) > 0) {
2894 error = buf_putc(diffbuf, '\0');
2895 colorized_line = strdup(buf_get(diffbuf));
2899 free(div_diff_line_div);
2900 return colorized_line;
2904 gw_html_escape(const char *html)
2906 char *escaped_str = NULL, *buf;
2908 size_t sz, i, buff_sz = 2048;
2910 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2916 if ((sz = strlen(html)) == 0)
2919 /* only work with buff_sz */
2923 for (i = 0; i < sz; i++) {
2927 strcat(buf, ">");
2930 strcat(buf, "&");
2933 strcat(buf, "<");
2936 strcat(buf, """);
2939 strcat(buf, "'");
2942 strcat(buf, "<br />");
2948 asprintf(&escaped_str, "%s", buf);
2956 const struct got_error *error = NULL;
2957 struct trans *gw_trans;
2958 struct gw_dir *dir = NULL, *tdir;
2959 const char *page = "index";
2962 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
2965 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2968 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2971 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2974 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX,
2976 errx(1, "khttp_parse");
2978 if ((gw_trans->gw_conf =
2979 malloc(sizeof(struct gotweb_conf))) == NULL) {
2981 error = got_error_from_errno("malloc");
2985 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
2987 error = got_error_from_errno("pledge");
2991 TAILQ_INIT(&gw_trans->gw_dirs);
2994 gw_trans->repos_total = 0;
2995 gw_trans->repo_path = NULL;
2996 gw_trans->commit = NULL;
2997 gw_trans->headref = strdup(GOT_REF_HEAD);
2998 gw_trans->mime = KMIME_TEXT_HTML;
2999 gw_trans->gw_tmpl->key = templs;
3000 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3001 gw_trans->gw_tmpl->arg = gw_trans;
3002 gw_trans->gw_tmpl->cb = gw_template;
3003 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3007 gw_trans->mime = KMIME_TEXT_PLAIN;
3008 gw_trans->action = GW_ERR;
3009 gw_display_index(gw_trans, error);
3013 error = gw_parse_querystring(gw_trans);
3017 gw_display_index(gw_trans, error);
3021 free(gw_trans->gw_conf->got_repos_path);
3022 free(gw_trans->gw_conf->got_www_path);
3023 free(gw_trans->gw_conf->got_site_name);
3024 free(gw_trans->gw_conf->got_site_owner);
3025 free(gw_trans->gw_conf->got_site_link);
3026 free(gw_trans->gw_conf->got_logo);
3027 free(gw_trans->gw_conf->got_logo_url);
3028 free(gw_trans->gw_conf);
3029 free(gw_trans->commit);
3030 free(gw_trans->repo_path);
3031 free(gw_trans->repo_name);
3032 free(gw_trans->repo_file);
3033 free(gw_trans->action_name);
3034 free(gw_trans->headref);
3036 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3038 free(dir->description);
3047 khttp_free(gw_trans->gw_req);
3048 return EXIT_SUCCESS;