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(headers, gw_header) gw_headers;
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;
73 unsigned int repos_total;
78 TAILQ_ENTRY(gw_header) entry;
79 struct got_repository *repo;
80 struct got_reflist_head refs;
81 struct got_commit_object *commit;
82 struct got_object_id *id;
86 char *commit_id; /* id_str1 */
87 char *parent_id; /* id_str2 */
92 time_t committer_time;
96 TAILQ_ENTRY(gw_dir) entry;
137 static const char *const gw_templs[TEMPL__MAX] = {
147 static const struct kvalid gw_keys[KEY__ZMAX] = {
148 { kvalid_stringne, "action" },
149 { kvalid_stringne, "commit" },
150 { kvalid_stringne, "file" },
151 { kvalid_stringne, "folder" },
152 { kvalid_stringne, "headref" },
153 { kvalid_int, "page" },
154 { kvalid_stringne, "path" },
157 static struct gw_dir *gw_init_gw_dir(char *);
158 static struct gw_header *gw_init_header(void);
160 static char *gw_get_repo_description(struct gw_trans *,
162 static char *gw_get_repo_owner(struct gw_trans *,
164 static char *gw_get_time_str(time_t, int);
165 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
166 char *, char *, int);
167 static char *gw_get_file_blame_blob(struct gw_trans *);
168 static char *gw_get_repo_tree(struct gw_trans *);
169 static char *gw_get_diff(struct gw_trans *,
171 static char *gw_get_repo_tags(struct gw_trans *,
172 struct gw_header *, int, int);
173 static char *gw_get_repo_heads(struct gw_trans *);
174 static char *gw_get_clone_url(struct gw_trans *, char *);
175 static char *gw_get_got_link(struct gw_trans *);
176 static char *gw_get_site_link(struct gw_trans *);
177 static char *gw_html_escape(const char *);
178 static char *gw_colordiff_line(char *);
180 static char *gw_gen_commit_header(char *, char*);
181 static char *gw_gen_diff_header(char *, char*);
182 static char *gw_gen_author_header(char *);
183 static char *gw_gen_committer_header(char *);
184 static char *gw_gen_age_header(char *);
185 static char *gw_gen_commit_msg_header(char *);
186 static char *gw_gen_tree_header(char *);
188 static void gw_free_headers(struct gw_header *);
189 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
191 static const struct got_error* gw_display_index(struct gw_trans *);
192 static void gw_display_error(struct gw_trans *,
193 const struct got_error *);
195 static int gw_template(size_t, void *);
197 static const struct got_error* gw_get_header(struct gw_trans *,
198 struct gw_header *, int);
199 static const struct got_error* gw_get_commits(struct gw_trans *,
200 struct gw_header *, int);
201 static const struct got_error* gw_get_commit(struct gw_trans *,
203 static const struct got_error* gw_apply_unveil(const char *, const char *);
204 static const struct got_error* gw_blame_cb(void *, int, int,
205 struct got_object_id *);
206 static const struct got_error* gw_load_got_paths(struct gw_trans *);
207 static const struct got_error* gw_load_got_path(struct gw_trans *,
209 static const struct got_error* gw_parse_querystring(struct gw_trans *);
211 static const struct got_error* gw_blame(struct gw_trans *);
212 static const struct got_error* gw_blob(struct gw_trans *);
213 static const struct got_error* gw_diff(struct gw_trans *);
214 static const struct got_error* gw_index(struct gw_trans *);
215 static const struct got_error* gw_commits(struct gw_trans *);
216 static const struct got_error* gw_briefs(struct gw_trans *);
217 static const struct got_error* gw_summary(struct gw_trans *);
218 static const struct got_error* gw_tree(struct gw_trans *);
219 static const struct got_error* gw_tag(struct gw_trans *);
221 struct gw_query_action {
222 unsigned int func_id;
223 const char *func_name;
224 const struct got_error *(*func_main)(struct gw_trans *);
228 enum gw_query_actions {
241 static struct gw_query_action gw_query_funcs[] = {
242 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
243 { GW_BLOB, "blob", gw_blob, "gw_tmpl/blob.tmpl" },
244 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
245 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
246 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
247 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
248 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
249 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
250 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
251 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
254 static const struct got_error *
255 gw_kcgi_error(enum kcgi_err kerr)
260 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
261 return got_error(GOT_ERR_CANCELLED);
263 if (kerr == KCGI_ENOMEM)
264 return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
266 if (kerr == KCGI_ENFILE)
267 return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
269 if (kerr == KCGI_EAGAIN)
270 return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
272 if (kerr == KCGI_FORM)
273 return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
275 return got_error_from_errno(kcgi_strerror(kerr));
278 static const struct got_error *
279 gw_apply_unveil(const char *repo_path, const char *repo_file)
281 const struct got_error *err;
283 if (repo_path && repo_file) {
285 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
286 return got_error_from_errno("asprintf unveil");
287 if (unveil(full_path, "r") != 0)
288 return got_error_from_errno2("unveil", full_path);
291 if (repo_path && unveil(repo_path, "r") != 0)
292 return got_error_from_errno2("unveil", repo_path);
294 if (unveil("/tmp", "rwc") != 0)
295 return got_error_from_errno2("unveil", "/tmp");
297 err = got_privsep_unveil_exec_helpers();
301 if (unveil(NULL, NULL) != 0)
302 return got_error_from_errno("unveil");
307 static const struct got_error *
308 gw_blame(struct gw_trans *gw_trans)
310 const struct got_error *error = NULL;
311 struct gw_header *header = NULL;
312 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
315 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
317 return got_error_from_errno("pledge");
319 if ((header = gw_init_header()) == NULL)
320 return got_error_from_errno("malloc");
322 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
326 error = gw_get_header(gw_trans, header, 1);
330 blame_html = gw_get_file_blame_blob(gw_trans);
332 if (blame_html == NULL) {
333 blame_html = strdup("");
334 if (blame_html == NULL) {
335 error = got_error_from_errno("strdup");
340 if (asprintf(&blame_html_disp, blame_header,
341 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
342 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
344 error = got_error_from_errno("asprintf");
348 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
349 error = got_error_from_errno("asprintf");
353 kerr = khttp_puts(gw_trans->gw_req, blame);
355 error = gw_kcgi_error(kerr);
357 got_ref_list_free(&header->refs);
358 gw_free_headers(header);
359 free(blame_html_disp);
365 static const struct got_error *
366 gw_blob(struct gw_trans *gw_trans)
368 const struct got_error *error = NULL;
369 struct gw_header *header = NULL;
373 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
375 return got_error_from_errno("pledge");
377 if ((header = gw_init_header()) == NULL)
378 return got_error_from_errno("malloc");
380 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
384 error = gw_get_header(gw_trans, header, 1);
388 blob = gw_get_file_blame_blob(gw_trans);
393 error = got_error_from_errno("strdup");
398 kerr = khttp_puts(gw_trans->gw_req, blob);
400 error = gw_kcgi_error(kerr);
402 got_ref_list_free(&header->refs);
403 gw_free_headers(header);
408 static const struct got_error *
409 gw_diff(struct gw_trans *gw_trans)
411 const struct got_error *error = NULL;
412 struct gw_header *header = NULL;
413 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
416 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
418 return got_error_from_errno("pledge");
420 if ((header = gw_init_header()) == NULL)
421 return got_error_from_errno("malloc");
423 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
427 error = gw_get_header(gw_trans, header, 1);
431 diff_html = gw_get_diff(gw_trans, header);
433 if (diff_html == NULL) {
434 diff_html = strdup("");
435 if (diff_html == NULL) {
436 error = got_error_from_errno("strdup");
441 if (asprintf(&diff_html_disp, diff_header,
442 gw_gen_diff_header(header->parent_id, header->commit_id),
443 gw_gen_commit_header(header->commit_id, header->refs_str),
444 gw_gen_tree_header(header->tree_id),
445 gw_gen_author_header(header->author),
446 gw_gen_committer_header(header->committer),
447 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
448 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
450 error = got_error_from_errno("asprintf");
454 if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
455 error = got_error_from_errno("asprintf");
459 kerr = khttp_puts(gw_trans->gw_req, diff);
461 error = gw_kcgi_error(kerr);
463 got_ref_list_free(&header->refs);
464 gw_free_headers(header);
465 free(diff_html_disp);
471 static const struct got_error *
472 gw_index(struct gw_trans *gw_trans)
474 const struct got_error *error = NULL;
475 struct gw_dir *gw_dir = NULL;
476 char *html, *navs, *next, *prev;
477 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
480 if (pledge("stdio rpath proc exec sendfd unveil",
482 error = got_error_from_errno("pledge");
486 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
490 error = gw_load_got_paths(gw_trans);
494 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
496 return gw_kcgi_error(kerr);
498 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
499 if (asprintf(&html, index_projects_empty,
500 gw_trans->gw_conf->got_repos_path) == -1)
501 return got_error_from_errno("asprintf");
502 kerr = khttp_puts(gw_trans->gw_req, html);
504 error = gw_kcgi_error(kerr);
509 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
512 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
513 if (gw_trans->page > 0 && (gw_trans->page *
514 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
523 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
524 gw_dir->name, gw_dir->name) == -1)
525 return got_error_from_errno("asprintf");
527 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
528 gw_dir->description, gw_dir->owner, gw_dir->age,
530 return got_error_from_errno("asprintf");
532 kerr = khttp_puts(gw_trans->gw_req, html);
536 return gw_kcgi_error(kerr);
538 if (gw_trans->gw_conf->got_max_repos_display == 0)
541 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
542 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
544 return gw_kcgi_error(kerr);
545 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
546 (gw_trans->page > 0) &&
547 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
548 prev_disp == gw_trans->repos_total)) {
549 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
551 return gw_kcgi_error(kerr);
554 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
555 (gw_trans->page > 0) &&
556 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
557 prev_disp == gw_trans->repos_total)) {
558 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
559 return got_error_from_errno("asprintf");
560 kerr = khttp_puts(gw_trans->gw_req, prev);
563 return gw_kcgi_error(kerr);
566 kerr = khttp_puts(gw_trans->gw_req, div_end);
568 return gw_kcgi_error(kerr);
570 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
571 next_disp == gw_trans->gw_conf->got_max_repos_display &&
572 dir_c != (gw_trans->page + 1) *
573 gw_trans->gw_conf->got_max_repos_display) {
574 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
575 return got_error_from_errno("calloc");
576 kerr = khttp_puts(gw_trans->gw_req, next);
579 return gw_kcgi_error(kerr);
580 kerr = khttp_puts(gw_trans->gw_req, div_end);
582 error = gw_kcgi_error(kerr);
587 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
588 (gw_trans->page > 0) &&
589 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
590 prev_disp == gw_trans->repos_total)) {
591 kerr = khttp_puts(gw_trans->gw_req, div_end);
593 return gw_kcgi_error(kerr);
601 static const struct got_error *
602 gw_commits(struct gw_trans *gw_trans)
604 const struct got_error *error = NULL;
605 char *commits_html, *commits_navs_html;
606 struct gw_header *header = NULL, *n_header = NULL;
609 if ((header = gw_init_header()) == NULL)
610 return got_error_from_errno("malloc");
612 if (pledge("stdio rpath proc exec sendfd unveil",
614 error = got_error_from_errno("pledge");
618 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
622 error = gw_get_header(gw_trans, header,
623 gw_trans->gw_conf->got_max_commits_display);
627 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
628 if (kerr != KCGI_OK) {
629 error = gw_kcgi_error(kerr);
632 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
633 if (asprintf(&commits_navs_html, commits_navs,
634 gw_trans->repo_name, n_header->commit_id,
635 gw_trans->repo_name, n_header->commit_id,
636 gw_trans->repo_name, n_header->commit_id) == -1) {
637 error = got_error_from_errno("asprintf");
641 if (asprintf(&commits_html, commits_line,
642 gw_gen_commit_header(n_header->commit_id,
644 gw_gen_author_header(n_header->author),
645 gw_gen_committer_header(n_header->committer),
646 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
647 TM_LONG)), gw_html_escape(n_header->commit_msg),
648 commits_navs_html) == -1) {
649 error = got_error_from_errno("asprintf");
652 kerr = khttp_puts(gw_trans->gw_req, commits_html);
653 if (kerr != KCGI_OK) {
654 error = gw_kcgi_error(kerr);
658 kerr = khttp_puts(gw_trans->gw_req, div_end);
660 error = gw_kcgi_error(kerr);
662 got_ref_list_free(&header->refs);
663 gw_free_headers(header);
664 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
665 gw_free_headers(n_header);
669 static const struct got_error *
670 gw_briefs(struct gw_trans *gw_trans)
672 const struct got_error *error = NULL;
673 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
674 struct gw_header *header = NULL, *n_header = NULL;
677 if ((header = gw_init_header()) == NULL)
678 return got_error_from_errno("malloc");
680 if (pledge("stdio rpath proc exec sendfd unveil",
682 error = got_error_from_errno("pledge");
686 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
690 if (gw_trans->action == GW_SUMMARY)
691 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
693 error = gw_get_header(gw_trans, header,
694 gw_trans->gw_conf->got_max_commits_display);
698 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
699 if (kerr != KCGI_OK) {
700 error = gw_kcgi_error(kerr);
704 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
705 if (asprintf(&briefs_navs_html, briefs_navs,
706 gw_trans->repo_name, n_header->commit_id,
707 gw_trans->repo_name, n_header->commit_id,
708 gw_trans->repo_name, n_header->commit_id) == -1) {
709 error = got_error_from_errno("asprintf");
712 newline = strchr(n_header->commit_msg, '\n');
715 if (asprintf(&briefs_html, briefs_line,
716 gw_get_time_str(n_header->committer_time, TM_DIFF),
717 n_header->author, gw_html_escape(n_header->commit_msg),
718 briefs_navs_html) == -1) {
719 error = got_error_from_errno("asprintf");
722 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
723 if (kerr != KCGI_OK) {
724 error = gw_kcgi_error(kerr);
728 kerr = khttp_puts(gw_trans->gw_req, div_end);
730 error = gw_kcgi_error(kerr);
732 got_ref_list_free(&header->refs);
733 gw_free_headers(header);
734 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
735 gw_free_headers(n_header);
739 static const struct got_error *
740 gw_summary(struct gw_trans *gw_trans)
742 const struct got_error *error = NULL;
743 char *description_html = NULL, *repo_owner_html = NULL;
744 char *age = NULL, *repo_age_html = NULL, *cloneurl_html = NULL;
745 char *tags = NULL, *tags_html = NULL;
746 char *heads = NULL, *heads_html = NULL;
749 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
750 return got_error_from_errno("pledge");
752 /* unveil is applied with gw_briefs below */
754 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
756 return gw_kcgi_error(kerr);
758 if (gw_trans->gw_conf->got_show_repo_description) {
759 if (gw_trans->gw_dir->description != NULL &&
760 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
761 if (asprintf(&description_html, description,
762 gw_trans->gw_dir->description) == -1) {
763 error = got_error_from_errno("asprintf");
767 kerr = khttp_puts(gw_trans->gw_req, description_html);
768 if (kerr != KCGI_OK) {
769 error = gw_kcgi_error(kerr);
775 if (gw_trans->gw_conf->got_show_repo_owner) {
776 if (gw_trans->gw_dir->owner != NULL &&
777 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
778 if (asprintf(&repo_owner_html, repo_owner,
779 gw_trans->gw_dir->owner) == -1) {
780 error = got_error_from_errno("asprintf");
784 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
785 if (kerr != KCGI_OK) {
786 error = gw_kcgi_error(kerr);
792 if (gw_trans->gw_conf->got_show_repo_age) {
793 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
794 "refs/heads", TM_LONG);
797 if (strcmp(age, "") != 0) {
798 if (asprintf(&repo_age_html, last_change, age) == -1) {
799 error = got_error_from_errno("asprintf");
803 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
804 if (kerr != KCGI_OK) {
805 error = gw_kcgi_error(kerr);
811 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
812 if (gw_trans->gw_dir->url != NULL &&
813 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
814 if (asprintf(&cloneurl_html, cloneurl,
815 gw_trans->gw_dir->url) == -1) {
816 error = got_error_from_errno("asprintf");
820 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
821 if (kerr != KCGI_OK) {
822 error = gw_kcgi_error(kerr);
827 kerr = khttp_puts(gw_trans->gw_req, div_end);
828 if (kerr != KCGI_OK) {
829 error = gw_kcgi_error(kerr);
833 error = gw_briefs(gw_trans);
837 tags = gw_get_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP, TAGBRIEF);
838 heads = gw_get_repo_heads(gw_trans);
840 if (tags != NULL && strcmp(tags, "") != 0) {
841 if (asprintf(&tags_html, summary_tags, tags) == -1) {
842 error = got_error_from_errno("asprintf");
845 kerr = khttp_puts(gw_trans->gw_req, tags_html);
846 if (kerr != KCGI_OK) {
847 error = gw_kcgi_error(kerr);
852 if (heads != NULL && strcmp(heads, "") != 0) {
853 if (asprintf(&heads_html, summary_heads, heads) == -1) {
854 error = got_error_from_errno("asprintf");
857 kerr = khttp_puts(gw_trans->gw_req, heads_html);
858 if (kerr != KCGI_OK) {
859 error = gw_kcgi_error(kerr);
864 free(description_html);
865 free(repo_owner_html);
876 static const struct got_error *
877 gw_tree(struct gw_trans *gw_trans)
879 const struct got_error *error = NULL;
880 struct gw_header *header = NULL;
881 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
884 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
885 return got_error_from_errno("pledge");
887 if ((header = gw_init_header()) == NULL)
888 return got_error_from_errno("malloc");
890 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
894 error = gw_get_header(gw_trans, header, 1);
898 tree_html = gw_get_repo_tree(gw_trans);
900 if (tree_html == NULL) {
901 tree_html = strdup("");
902 if (tree_html == NULL) {
903 error = got_error_from_errno("strdup");
908 if (asprintf(&tree_html_disp, tree_header,
909 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
910 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
912 error = got_error_from_errno("asprintf");
916 if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
917 error = got_error_from_errno("asprintf");
921 kerr = khttp_puts(gw_trans->gw_req, tree);
923 error = gw_kcgi_error(kerr);
925 got_ref_list_free(&header->refs);
926 gw_free_headers(header);
927 free(tree_html_disp);
933 static const struct got_error *
934 gw_tag(struct gw_trans *gw_trans)
936 const struct got_error *error = NULL;
937 struct gw_header *header = NULL;
938 char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
941 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
942 return got_error_from_errno("pledge");
944 if ((header = gw_init_header()) == NULL)
945 return got_error_from_errno("malloc");
947 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
951 error = gw_get_header(gw_trans, header, 1);
955 tag_html = gw_get_repo_tags(gw_trans, header, 1, TAGFULL);
956 if (tag_html == NULL) {
957 tag_html = strdup("");
958 if (tag_html == NULL) {
959 error = got_error_from_errno("strdup");
964 if (asprintf(&tag_html_disp, tag_header,
965 gw_gen_commit_header(header->commit_id, header->refs_str),
966 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
968 error = got_error_from_errno("asprintf");
972 if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
973 error = got_error_from_errno("asprintf");
977 kerr = khttp_puts(gw_trans->gw_req, tag);
979 error = gw_kcgi_error(kerr);
981 got_ref_list_free(&header->refs);
982 gw_free_headers(header);
989 static const struct got_error *
990 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
992 const struct got_error *error = NULL;
997 if (asprintf(&dir_test, "%s/%s/%s",
998 gw_trans->gw_conf->got_repos_path, gw_dir->name,
999 GOTWEB_GIT_DIR) == -1)
1000 return got_error_from_errno("asprintf");
1002 dt = opendir(dir_test);
1006 gw_dir->path = strdup(dir_test);
1011 if (asprintf(&dir_test, "%s/%s/%s",
1012 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1013 GOTWEB_GOT_DIR) == -1)
1014 return got_error_from_errno("asprintf");
1016 dt = opendir(dir_test);
1021 error = got_error(GOT_ERR_NOT_GIT_REPO);
1025 if (asprintf(&dir_test, "%s/%s",
1026 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1027 return got_error_from_errno("asprintf");
1029 gw_dir->path = strdup(dir_test);
1032 gw_dir->description = gw_get_repo_description(gw_trans,
1034 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
1035 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1036 "refs/heads", TM_DIFF);
1039 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
1048 static const struct got_error *
1049 gw_load_got_paths(struct gw_trans *gw_trans)
1051 const struct got_error *error = NULL;
1053 struct dirent **sd_dent;
1054 struct gw_dir *gw_dir;
1056 unsigned int d_cnt, d_i;
1058 d = opendir(gw_trans->gw_conf->got_repos_path);
1060 error = got_error_from_errno2("opendir",
1061 gw_trans->gw_conf->got_repos_path);
1065 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1068 error = got_error_from_errno2("scandir",
1069 gw_trans->gw_conf->got_repos_path);
1073 for (d_i = 0; d_i < d_cnt; d_i++) {
1074 if (gw_trans->gw_conf->got_max_repos > 0 &&
1075 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1076 break; /* account for parent and self */
1078 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1079 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1082 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1083 return got_error_from_errno("gw_dir malloc");
1085 error = gw_load_got_path(gw_trans, gw_dir);
1086 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1091 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1092 !got_path_dir_is_empty(gw_dir->path)) {
1093 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1095 gw_trans->repos_total++;
1103 static const struct got_error *
1104 gw_parse_querystring(struct gw_trans *gw_trans)
1106 const struct got_error *error = NULL;
1108 struct gw_query_action *action = NULL;
1111 if (gw_trans->gw_req->fieldnmap[0]) {
1112 error = got_error_from_errno("bad parse");
1114 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1115 /* define gw_trans->repo_path */
1116 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1117 return got_error_from_errno("asprintf");
1119 if (asprintf(&gw_trans->repo_path, "%s/%s",
1120 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1121 return got_error_from_errno("asprintf");
1123 /* get action and set function */
1124 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1125 for (i = 0; i < nitems(gw_query_funcs); i++) {
1126 action = &gw_query_funcs[i];
1127 if (action->func_name == NULL)
1130 if (strcmp(action->func_name,
1131 p->parsed.s) == 0) {
1132 gw_trans->action = i;
1133 if (asprintf(&gw_trans->action_name,
1134 "%s", action->func_name) == -1)
1136 got_error_from_errno(
1145 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1146 if (asprintf(&gw_trans->commit, "%s",
1148 return got_error_from_errno("asprintf");
1150 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1151 if (asprintf(&gw_trans->repo_file, "%s",
1153 return got_error_from_errno("asprintf");
1155 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1156 if (asprintf(&gw_trans->repo_folder, "%s",
1158 return got_error_from_errno("asprintf");
1160 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1161 if (asprintf(&gw_trans->headref, "%s",
1163 return got_error_from_errno("asprintf");
1165 if (action == NULL) {
1166 error = got_error_from_errno("invalid action");
1169 if ((gw_trans->gw_dir =
1170 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1171 return got_error_from_errno("gw_dir malloc");
1173 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1177 gw_trans->action = GW_INDEX;
1179 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1180 gw_trans->page = p->parsed.i;
1182 if (gw_trans->action == GW_BLOB)
1183 gw_trans->mime = KMIME_TEXT_PLAIN;
1188 static struct gw_dir *
1189 gw_init_gw_dir(char *dir)
1191 struct gw_dir *gw_dir;
1193 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1196 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1202 static const struct got_error *
1203 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1207 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1208 if (kerr != KCGI_OK)
1209 return gw_kcgi_error(kerr);
1210 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1212 if (kerr != KCGI_OK)
1213 return gw_kcgi_error(kerr);
1214 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1216 if (kerr != KCGI_OK)
1217 return gw_kcgi_error(kerr);
1218 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1219 if (kerr != KCGI_OK)
1220 return gw_kcgi_error(kerr);
1221 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1222 if (kerr != KCGI_OK)
1223 return gw_kcgi_error(kerr);
1224 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1225 if (kerr != KCGI_OK)
1226 return gw_kcgi_error(kerr);
1228 kerr = khttp_body(gw_trans->gw_req);
1229 return gw_kcgi_error(kerr);
1232 static const struct got_error *
1233 gw_display_index(struct gw_trans *gw_trans)
1235 const struct got_error *error;
1238 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1242 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1244 return gw_kcgi_error(kerr);
1246 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1247 gw_query_funcs[gw_trans->action].template);
1248 if (kerr != KCGI_OK) {
1249 khtml_close(gw_trans->gw_html_req);
1250 return gw_kcgi_error(kerr);
1253 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1257 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1259 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1262 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1265 khttp_puts(gw_trans->gw_req, err->msg);
1266 khtml_close(gw_trans->gw_html_req);
1270 gw_template(size_t key, void *arg)
1272 const struct got_error *error = NULL;
1273 struct gw_trans *gw_trans = arg;
1274 char *gw_got_link, *gw_site_link;
1275 char *site_owner_name, *site_owner_name_h;
1279 khttp_puts(gw_trans->gw_req, head);
1282 gw_got_link = gw_get_got_link(gw_trans);
1283 if (gw_got_link != NULL)
1284 khttp_puts(gw_trans->gw_req, gw_got_link);
1288 case (TEMPL_SITEPATH):
1289 gw_site_link = gw_get_site_link(gw_trans);
1290 if (gw_site_link != NULL)
1291 khttp_puts(gw_trans->gw_req, gw_site_link);
1296 if (gw_trans->gw_conf->got_site_name != NULL)
1297 khtml_puts(gw_trans->gw_html_req,
1298 gw_trans->gw_conf->got_site_name);
1301 case (TEMPL_SEARCH):
1302 khttp_puts(gw_trans->gw_req, search);
1304 case(TEMPL_SITEOWNER):
1305 if (gw_trans->gw_conf->got_site_owner != NULL &&
1306 gw_trans->gw_conf->got_show_site_owner) {
1308 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1309 if (asprintf(&site_owner_name_h, site_owner,
1310 site_owner_name) == -1)
1313 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1314 free(site_owner_name);
1315 free(site_owner_name_h);
1318 case(TEMPL_CONTENT):
1319 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1321 khttp_puts(gw_trans->gw_req, error->msg);
1330 gw_gen_commit_header(char *str1, char *str2)
1332 char *return_html = NULL, *ref_str = NULL;
1334 if (strcmp(str2, "") != 0) {
1335 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1336 return_html = strdup("");
1340 ref_str = strdup("");
1343 if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1344 return_html = strdup("");
1351 gw_gen_diff_header(char *str1, char *str2)
1353 char *return_html = NULL;
1355 if (asprintf(&return_html, header_diff_html, str1, str2) == -1)
1356 return_html = strdup("");
1362 gw_gen_author_header(char *str)
1364 char *return_html = NULL;
1366 if (asprintf(&return_html, header_author_html, str) == -1)
1367 return_html = strdup("");
1373 gw_gen_committer_header(char *str)
1375 char *return_html = NULL;
1377 if (asprintf(&return_html, header_committer_html, str) == -1)
1378 return_html = strdup("");
1384 gw_gen_age_header(char *str)
1386 char *return_html = NULL;
1388 if (asprintf(&return_html, header_age_html, str) == -1)
1389 return_html = strdup("");
1395 gw_gen_commit_msg_header(char *str)
1397 char *return_html = NULL;
1399 if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1400 return_html = strdup("");
1406 gw_gen_tree_header(char *str)
1408 char *return_html = NULL;
1410 if (asprintf(&return_html, header_tree_html, str) == -1)
1411 return_html = strdup("");
1417 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1420 char *description = NULL, *d_file = NULL;
1423 if (gw_trans->gw_conf->got_show_repo_description == 0)
1426 if (asprintf(&d_file, "%s/description", dir) == -1)
1429 if ((f = fopen(d_file, "r")) == NULL)
1432 fseek(f, 0, SEEK_END);
1434 fseek(f, 0, SEEK_SET);
1435 if ((description = calloc(len, sizeof(char *))) == NULL)
1438 fread(description, 1, len, f);
1447 gw_get_time_str(time_t committer_time, int ref_tm)
1451 char *years = "years ago", *months = "months ago";
1452 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1453 char *minutes = "minutes ago", *seconds = "seconds ago";
1454 char *now = "right now";
1460 diff_time = time(NULL) - committer_time;
1461 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1462 if (asprintf(&repo_age, "%lld %s",
1463 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1465 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1466 if (asprintf(&repo_age, "%lld %s",
1467 (diff_time / 60 / 60 / 24 / (365 / 12)),
1470 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1471 if (asprintf(&repo_age, "%lld %s",
1472 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1474 } else if (diff_time > 60 * 60 * 24 * 2) {
1475 if (asprintf(&repo_age, "%lld %s",
1476 (diff_time / 60 / 60 / 24), days) == -1)
1478 } else if (diff_time > 60 * 60 * 2) {
1479 if (asprintf(&repo_age, "%lld %s",
1480 (diff_time / 60 / 60), hours) == -1)
1482 } else if (diff_time > 60 * 2) {
1483 if (asprintf(&repo_age, "%lld %s", (diff_time / 60),
1486 } else if (diff_time > 2) {
1487 if (asprintf(&repo_age, "%lld %s", diff_time,
1491 if (asprintf(&repo_age, "%s", now) == -1)
1496 if (gmtime_r(&committer_time, &tm) == NULL)
1499 s = asctime_r(&tm, datebuf);
1503 if (asprintf(&repo_age, "%s UTC", datebuf) == -1)
1510 static const struct got_error *
1511 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
1512 char *repo_ref, int ref_tm)
1514 const struct got_error *error = NULL;
1515 struct got_object_id *id = NULL;
1516 struct got_repository *repo = NULL;
1517 struct got_commit_object *commit = NULL;
1518 struct got_reflist_head refs;
1519 struct got_reflist_entry *re;
1520 struct got_reference *head_ref;
1522 time_t committer_time = 0, cmp_time = 0;
1523 const char *refname;
1526 SIMPLEQ_INIT(&refs);
1528 if (repo_ref == NULL)
1531 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1534 if (gw_trans->gw_conf->got_show_repo_age == 0) {
1535 *repo_age = strdup("");
1536 if (*repo_age == NULL)
1537 return got_error_from_errno("strdup");
1541 error = got_repo_open(&repo, dir, NULL);
1546 error = got_ref_list(&refs, repo, "refs/heads",
1547 got_ref_cmp_by_name, NULL);
1549 error = got_ref_list(&refs, repo, repo_ref,
1550 got_ref_cmp_by_name, NULL);
1554 SIMPLEQ_FOREACH(re, &refs, entry) {
1556 refname = strdup(repo_ref);
1558 refname = got_ref_get_name(re->ref);
1559 error = got_ref_open(&head_ref, repo, refname, 0);
1563 error = got_ref_resolve(&id, repo, head_ref);
1564 got_ref_close(head_ref);
1568 error = got_object_open_as_commit(&commit, repo, id);
1573 got_object_commit_get_committer_time(commit);
1575 if (cmp_time < committer_time)
1576 cmp_time = committer_time;
1579 if (cmp_time != 0) {
1580 committer_time = cmp_time;
1581 *repo_age = gw_get_time_str(committer_time, ref_tm);
1583 *repo_age = strdup("");
1584 if (*repo_age == NULL)
1585 error = got_error_from_errno("strdup");
1588 got_ref_list_free(&refs);
1594 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1596 const struct got_error *error;
1598 struct got_object_id *id1 = NULL, *id2 = NULL;
1599 struct buf *diffbuf = NULL;
1600 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL;
1601 char *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1609 error = buf_alloc(&diffbuf, 0);
1613 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1617 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1618 error = got_repo_match_object_id(&id1, &label1,
1619 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1624 error = got_repo_match_object_id(&id2, &label2,
1625 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1629 error = got_object_get_type(&obj_type, header->repo, id2);
1633 case GOT_OBJ_TYPE_BLOB:
1634 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1637 case GOT_OBJ_TYPE_TREE:
1638 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1641 case GOT_OBJ_TYPE_COMMIT:
1642 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1646 error = got_error(GOT_ERR_OBJ_TYPE);
1649 if ((buf = calloc(128, sizeof(char *))) == NULL)
1652 fseek(f, 0, SEEK_SET);
1654 while ((fgets(buf, 2048, f)) != NULL) {
1656 while (*n_buf == '\n')
1658 newline = strchr(n_buf, '\n');
1662 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1663 if (buf_color == NULL)
1666 error = buf_puts(&newsize, diffbuf, buf_color);
1670 error = buf_puts(&newsize, diffbuf, div_end);
1675 if (buf_len(diffbuf) > 0) {
1676 error = buf_putc(diffbuf, '\0');
1677 diff_html = strdup(buf_get(diffbuf));
1696 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1699 char *owner = NULL, *d_file = NULL;
1700 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1701 char *comp, *pos, *buf;
1704 if (gw_trans->gw_conf->got_show_repo_owner == 0)
1707 if (asprintf(&d_file, "%s/config", dir) == -1)
1710 if ((f = fopen(d_file, "r")) == NULL)
1713 if ((buf = calloc(128, sizeof(char *))) == NULL)
1716 while ((fgets(buf, 128, f)) != NULL) {
1717 if ((pos = strstr(buf, gotweb)) != NULL)
1720 if ((pos = strstr(buf, gitweb)) != NULL)
1729 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1734 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1737 for (i = 0; i < 2; i++) {
1738 owner = strsep(&buf, "\"");
1752 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1755 char *url = NULL, *d_file = NULL;
1758 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
1761 if ((f = fopen(d_file, "r")) == NULL)
1764 fseek(f, 0, SEEK_END);
1766 fseek(f, 0, SEEK_SET);
1768 if ((url = calloc(len, sizeof(char *))) == NULL)
1771 fread(url, 1, len, f);
1778 gw_get_repo_tags(struct gw_trans *gw_trans, struct gw_header *header, int limit,
1781 const struct got_error *error = NULL;
1782 struct got_repository *repo = NULL;
1783 struct got_reflist_head refs;
1784 struct got_reflist_entry *re;
1785 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL;
1786 char *age = NULL, *newline;
1787 struct buf *diffbuf = NULL;
1790 SIMPLEQ_INIT(&refs);
1792 error = buf_alloc(&diffbuf, 0);
1796 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1800 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1804 SIMPLEQ_FOREACH(re, &refs, entry) {
1805 const char *refname;
1806 char *refstr, *tag_commit0, *tag_commit, *id_str;
1809 struct got_object_id *id;
1810 struct got_tag_object *tag;
1812 refname = got_ref_get_name(re->ref);
1813 if (strncmp(refname, "refs/tags/", 10) != 0)
1816 refstr = got_ref_to_str(re->ref);
1817 if (refstr == NULL) {
1818 error = got_error_from_errno("got_ref_to_str");
1822 error = got_ref_resolve(&id, repo, re->ref);
1825 error = got_object_open_as_tag(&tag, repo, id);
1830 tagger = got_object_tag_get_tagger(tag);
1831 tagger_time = got_object_tag_get_tagger_time(tag);
1833 error = got_object_id_str(&id_str,
1834 got_object_tag_get_object_id(tag));
1838 tag_commit0 = strdup(got_object_tag_get_message(tag));
1840 if (tag_commit0 == NULL) {
1841 error = got_error_from_errno("strdup");
1845 tag_commit = tag_commit0;
1846 while (*tag_commit == '\n')
1851 newline = strchr(tag_commit, '\n');
1855 if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1857 error = got_error_from_errno("asprintf");
1861 if (asprintf(&tags_navs_disp, tags_navs,
1862 gw_trans->repo_name, id_str, gw_trans->repo_name,
1863 id_str, gw_trans->repo_name, id_str,
1864 gw_trans->repo_name, id_str) == -1) {
1865 error = got_error_from_errno("asprintf");
1869 if (asprintf(&tag_row, tags_row, age, refname,
1870 tag_commit, tags_navs_disp) == -1) {
1871 error = got_error_from_errno("asprintf");
1875 free(tags_navs_disp);
1878 if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1880 error = got_error_from_errno("asprintf");
1883 if (asprintf(&tag_row, tag_info, age,
1884 gw_html_escape(tagger),
1885 gw_html_escape(tag_commit)) == -1) {
1886 error = got_error_from_errno("asprintf");
1894 got_object_tag_close(tag);
1896 error = buf_puts(&newsize, diffbuf, tag_row);
1904 if (error || (limit && --limit == 0))
1908 if (buf_len(diffbuf) > 0) {
1909 error = buf_putc(diffbuf, '\0');
1910 tags = strdup(buf_get(diffbuf));
1914 got_ref_list_free(&refs);
1916 got_repo_close(repo);
1924 gw_free_headers(struct gw_header *header)
1928 if (header->commit != NULL)
1929 got_object_commit_close(header->commit);
1931 got_repo_close(header->repo);
1932 free(header->refs_str);
1933 free(header->commit_id);
1934 free(header->parent_id);
1935 free(header->tree_id);
1936 free(header->author);
1937 free(header->committer);
1938 free(header->commit_msg);
1941 static struct gw_header *
1944 struct gw_header *header;
1946 header = malloc(sizeof(*header));
1950 header->repo = NULL;
1951 header->commit = NULL;
1953 header->path = NULL;
1954 SIMPLEQ_INIT(&header->refs);
1959 static const struct got_error *
1960 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1963 const struct got_error *error = NULL;
1964 struct got_commit_graph *graph = NULL;
1966 error = got_commit_graph_open(&graph, header->path, 0);
1970 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1976 error = got_commit_graph_iter_next(&header->id, graph,
1977 header->repo, NULL, NULL);
1979 if (error->code == GOT_ERR_ITER_COMPLETED)
1983 if (header->id == NULL)
1986 error = got_object_open_as_commit(&header->commit, header->repo,
1991 error = gw_get_commit(gw_trans, header);
1993 struct gw_header *n_header = NULL;
1994 if ((n_header = gw_init_header()) == NULL) {
1995 error = got_error_from_errno("malloc");
1999 n_header->refs_str = strdup(header->refs_str);
2000 n_header->commit_id = strdup(header->commit_id);
2001 n_header->parent_id = strdup(header->parent_id);
2002 n_header->tree_id = strdup(header->tree_id);
2003 n_header->author = strdup(header->author);
2004 n_header->committer = strdup(header->committer);
2005 n_header->commit_msg = strdup(header->commit_msg);
2006 n_header->committer_time = header->committer_time;
2007 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2010 if (error || (limit && --limit == 0))
2015 got_commit_graph_close(graph);
2019 static const struct got_error *
2020 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2022 const struct got_error *error = NULL;
2023 struct got_reflist_entry *re;
2024 struct got_object_id *id2 = NULL;
2025 struct got_object_qid *parent_id;
2026 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2029 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2032 struct got_tag_object *tag = NULL;
2035 name = got_ref_get_name(re->ref);
2036 if (strcmp(name, GOT_REF_HEAD) == 0)
2038 if (strncmp(name, "refs/", 5) == 0)
2040 if (strncmp(name, "got/", 4) == 0)
2042 if (strncmp(name, "heads/", 6) == 0)
2044 if (strncmp(name, "remotes/", 8) == 0)
2046 if (strncmp(name, "tags/", 5) == 0) {
2047 error = got_object_open_as_tag(&tag, header->repo,
2050 if (error->code != GOT_ERR_OBJ_TYPE)
2053 * Ref points at something other
2060 cmp = got_object_id_cmp(tag ?
2061 got_object_tag_get_object_id(tag) : re->id, header->id);
2063 got_object_tag_close(tag);
2067 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2068 s ? ", " : "", name) == -1) {
2069 error = got_error_from_errno("asprintf");
2073 header->refs_str = strdup(refs_str);
2077 if (refs_str == NULL)
2078 header->refs_str = strdup("");
2081 error = got_object_id_str(&header->commit_id, header->id);
2085 error = got_object_id_str(&header->tree_id,
2086 got_object_commit_get_tree_id(header->commit));
2090 if (gw_trans->action == GW_DIFF) {
2091 parent_id = SIMPLEQ_FIRST(
2092 got_object_commit_get_parent_ids(header->commit));
2093 if (parent_id != NULL) {
2094 id2 = got_object_id_dup(parent_id->id);
2096 error = got_object_id_str(&header->parent_id, id2);
2101 header->parent_id = strdup("/dev/null");
2103 header->parent_id = strdup("");
2105 header->committer_time =
2106 got_object_commit_get_committer_time(header->commit);
2108 if (gw_trans->action != GW_BRIEFS && gw_trans->action != GW_SUMMARY) {
2109 header->author = strdup(
2110 gw_html_escape(got_object_commit_get_author(header->commit))
2113 header->author = strdup(
2114 got_object_commit_get_author(header->commit)
2118 header->committer = strdup(
2119 gw_html_escape(got_object_commit_get_committer(header->commit))
2122 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2126 commit_msg = commit_msg0;
2127 while (*commit_msg == '\n')
2130 header->commit_msg = strdup(commit_msg);
2135 static const struct got_error *
2136 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2138 const struct got_error *error = NULL;
2139 char *in_repo_path = NULL;
2141 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2145 if (gw_trans->commit == NULL) {
2146 struct got_reference *head_ref;
2147 error = got_ref_open(&head_ref, header->repo,
2148 gw_trans->headref, 0);
2152 error = got_ref_resolve(&header->id, header->repo, head_ref);
2153 got_ref_close(head_ref);
2157 error = got_object_open_as_commit(&header->commit,
2158 header->repo, header->id);
2160 struct got_reference *ref;
2161 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2162 if (error == NULL) {
2164 error = got_ref_resolve(&header->id, header->repo, ref);
2168 error = got_object_get_type(&obj_type, header->repo,
2172 if (obj_type == GOT_OBJ_TYPE_TAG) {
2173 struct got_tag_object *tag;
2174 error = got_object_open_as_tag(&tag,
2175 header->repo, header->id);
2178 if (got_object_tag_get_object_type(tag) !=
2179 GOT_OBJ_TYPE_COMMIT) {
2180 got_object_tag_close(tag);
2181 error = got_error(GOT_ERR_OBJ_TYPE);
2185 header->id = got_object_id_dup(
2186 got_object_tag_get_object_id(tag));
2187 if (header->id == NULL)
2188 error = got_error_from_errno(
2189 "got_object_id_dup");
2190 got_object_tag_close(tag);
2193 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2194 error = got_error(GOT_ERR_OBJ_TYPE);
2197 error = got_object_open_as_commit(&header->commit,
2198 header->repo, header->id);
2202 if (header->commit == NULL) {
2203 error = got_repo_match_object_id_prefix(&header->id,
2204 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2209 error = got_repo_match_object_id_prefix(&header->id,
2210 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2214 error = got_repo_map_path(&in_repo_path, header->repo,
2215 gw_trans->repo_path, 1);
2220 header->path = strdup(in_repo_path);
2224 error = got_ref_list(&header->refs, header->repo, NULL,
2225 got_ref_cmp_by_name, NULL);
2229 error = gw_get_commits(gw_trans, header, limit);
2237 char datebuf[11]; /* YYYY-MM-DD + NUL */
2240 struct gw_blame_cb_args {
2241 struct blame_line *lines;
2245 off_t *line_offsets;
2247 struct got_repository *repo;
2248 struct gw_trans *gw_trans;
2249 struct buf *blamebuf;
2252 static const struct got_error *
2253 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2255 const struct got_error *err = NULL;
2256 struct gw_blame_cb_args *a = arg;
2257 struct blame_line *bline;
2259 size_t linesize = 0, newsize;
2260 struct got_commit_object *commit = NULL;
2263 time_t committer_time;
2265 if (nlines != a->nlines ||
2266 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2267 return got_error(GOT_ERR_RANGE);
2270 return NULL; /* no change in this commit */
2272 /* Annotate this line. */
2273 bline = &a->lines[lineno - 1];
2274 if (bline->annotated)
2276 err = got_object_id_str(&bline->id_str, id);
2280 err = got_object_open_as_commit(&commit, a->repo, id);
2284 bline->committer = strdup(got_object_commit_get_committer(commit));
2285 if (bline->committer == NULL) {
2286 err = got_error_from_errno("strdup");
2290 committer_time = got_object_commit_get_committer_time(commit);
2291 if (localtime_r(&committer_time, &tm) == NULL)
2292 return got_error_from_errno("localtime_r");
2293 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2294 &tm) >= sizeof(bline->datebuf)) {
2295 err = got_error(GOT_ERR_NO_SPACE);
2298 bline->annotated = 1;
2300 /* Print lines annotated so far. */
2301 bline = &a->lines[a->lineno_cur - 1];
2302 if (!bline->annotated)
2305 offset = a->line_offsets[a->lineno_cur - 1];
2306 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2307 err = got_error_from_errno("fseeko");
2311 while (bline->annotated) {
2312 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2313 *line_escape = NULL;
2316 if (getline(&line, &linesize, a->f) == -1) {
2318 err = got_error_from_errno("getline");
2322 committer = bline->committer;
2323 smallerthan = strchr(committer, '<');
2324 if (smallerthan && smallerthan[1] != '\0')
2325 committer = smallerthan + 1;
2326 at = strchr(committer, '@');
2329 len = strlen(committer);
2331 committer[8] = '\0';
2333 nl = strchr(line, '\n');
2337 if (strcmp(line, "") != 0)
2338 line_escape = strdup(gw_html_escape(line));
2340 line_escape = strdup("");
2342 asprintf(&blame_row, blame_line, a->nlines_prec,
2343 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2346 err = buf_puts(&newsize, a->blamebuf, blame_row);
2350 bline = &a->lines[a->lineno_cur - 1];
2356 got_object_commit_close(commit);
2362 gw_get_file_blame_blob(struct gw_trans *gw_trans)
2364 const struct got_error *error = NULL;
2365 struct got_repository *repo = NULL;
2366 struct got_object_id *obj_id = NULL;
2367 struct got_object_id *commit_id = NULL;
2368 struct got_blob_object *blob = NULL;
2369 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL;
2370 char *folder = NULL;
2371 struct gw_blame_cb_args bca;
2375 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2379 if (gw_trans->repo_folder != NULL) {
2380 if (asprintf(&folder, "%s/", gw_trans->repo_folder) == -1) {
2381 error = got_error_from_errno("asprintf");
2385 folder = strdup("");
2387 if (asprintf(&path, "%s%s", folder, gw_trans->repo_file) == -1) {
2388 error = got_error_from_errno("asprintf");
2393 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2397 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2398 GOT_OBJ_TYPE_COMMIT, 1, repo);
2402 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2406 if (obj_id == NULL) {
2407 error = got_error(GOT_ERR_NO_OBJ);
2411 error = got_object_get_type(&obj_type, repo, obj_id);
2415 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2416 error = got_error(GOT_ERR_OBJ_TYPE);
2420 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2424 error = buf_alloc(&bca.blamebuf, 0);
2428 bca.f = got_opentemp();
2429 if (bca.f == NULL) {
2430 error = got_error_from_errno("got_opentemp");
2433 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2434 &bca.line_offsets, bca.f, blob);
2435 if (error || bca.nlines == 0)
2438 if (gw_trans->action == GW_BLOB) {
2440 fseek(bca.f, 0, SEEK_END);
2441 len = ftell(bca.f) + 1;
2442 fseek(bca.f, 0, SEEK_SET);
2443 if ((blame_html = calloc(len, sizeof(char *))) == NULL)
2445 fread(blame_html, 1, len, bca.f);
2449 /* Don't include \n at EOF in the blame line count. */
2450 if (bca.line_offsets[bca.nlines - 1] == filesize)
2453 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2454 if (bca.lines == NULL) {
2455 error = got_error_from_errno("calloc");
2459 bca.nlines_prec = 0;
2466 bca.gw_trans = gw_trans;
2468 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2472 if (buf_len(bca.blamebuf) > 0) {
2473 error = buf_putc(bca.blamebuf, '\0');
2474 blame_html = strdup(buf_get(bca.blamebuf));
2477 free(bca.line_offsets);
2484 if (gw_trans->action != GW_BLOB && bca.lines) {
2485 for (i = 0; i < bca.nlines; i++) {
2486 struct blame_line *bline = &bca.lines[i];
2487 free(bline->id_str);
2488 free(bline->committer);
2494 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2497 error = got_object_blob_close(blob);
2501 error = got_repo_close(repo);
2509 gw_get_repo_tree(struct gw_trans *gw_trans)
2511 const struct got_error *error = NULL;
2512 struct got_repository *repo = NULL;
2513 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2514 struct got_tree_object *tree = NULL;
2515 struct buf *diffbuf = NULL;
2517 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2518 *tree_row = NULL, *id_str, *class = NULL;
2519 int nentries, i, class_flip = 0;
2521 error = buf_alloc(&diffbuf, 0);
2525 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2529 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2533 if (gw_trans->repo_folder != NULL)
2534 path = strdup(gw_trans->repo_folder);
2535 else if (in_repo_path) {
2537 path = in_repo_path;
2540 if (gw_trans->commit == NULL) {
2541 struct got_reference *head_ref;
2542 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2546 error = got_ref_resolve(&commit_id, repo, head_ref);
2547 got_ref_close(head_ref);
2550 error = got_repo_match_object_id(&commit_id, NULL,
2551 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2555 error = got_object_id_str(&gw_trans->commit, commit_id);
2559 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2563 error = got_object_open_as_tree(&tree, repo, tree_id);
2567 nentries = got_object_tree_get_nentries(tree);
2569 for (i = 0; i < nentries; i++) {
2570 struct got_tree_entry *te;
2571 const char *modestr = "";
2572 char *id = NULL, *url_html = NULL;
2574 te = got_object_tree_get_entry(tree, i);
2576 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2580 if (asprintf(&id, "%s", id_str) == -1) {
2581 error = got_error_from_errno("asprintf");
2586 mode_t mode = got_tree_entry_get_mode(te);
2588 if (got_object_tree_entry_is_submodule(te))
2590 else if (S_ISLNK(mode))
2592 else if (S_ISDIR(mode))
2594 else if (mode & S_IXUSR)
2597 if (class_flip == 0) {
2598 class = strdup("back_lightgray");
2601 class = strdup("back_white");
2605 char *build_folder = NULL;
2606 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2607 if (gw_trans->repo_folder != NULL) {
2608 if (asprintf(&build_folder, "%s/%s",
2609 gw_trans->repo_folder,
2610 got_tree_entry_get_name(te)) == -1) {
2612 got_error_from_errno("asprintf");
2616 if (asprintf(&build_folder, "%s",
2617 got_tree_entry_get_name(te)) == -1)
2621 if (asprintf(&url_html, folder_html,
2622 gw_trans->repo_name, gw_trans->action_name,
2623 gw_trans->commit, build_folder,
2624 got_tree_entry_get_name(te), modestr) == -1) {
2625 error = got_error_from_errno("asprintf");
2628 if (asprintf(&tree_row, tree_line, class, url_html,
2630 error = got_error_from_errno("asprintf");
2635 if (gw_trans->repo_folder != NULL) {
2636 if (asprintf(&build_folder, "%s",
2637 gw_trans->repo_folder) == -1) {
2639 got_error_from_errno("asprintf");
2643 build_folder = strdup("");
2645 if (asprintf(&url_html, file_html, gw_trans->repo_name,
2646 "blob", gw_trans->commit,
2647 got_tree_entry_get_name(te), build_folder,
2648 got_tree_entry_get_name(te), modestr) == -1) {
2649 error = got_error_from_errno("asprintf");
2653 if (asprintf(&tree_row, tree_line_with_navs, class,
2654 url_html, class, gw_trans->repo_name, "blob",
2655 gw_trans->commit, got_tree_entry_get_name(te),
2656 build_folder, "blob", gw_trans->repo_name,
2657 "blame", gw_trans->commit,
2658 got_tree_entry_get_name(te), build_folder,
2660 error = got_error_from_errno("asprintf");
2669 error = buf_puts(&newsize, diffbuf, tree_row);
2679 if (buf_len(diffbuf) > 0) {
2680 error = buf_putc(diffbuf, '\0');
2681 tree_html = strdup(buf_get(diffbuf));
2685 got_object_tree_close(tree);
2687 got_repo_close(repo);
2699 gw_get_repo_heads(struct gw_trans *gw_trans)
2701 const struct got_error *error = NULL;
2702 struct got_repository *repo = NULL;
2703 struct got_reflist_head refs;
2704 struct got_reflist_entry *re;
2705 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2706 struct buf *diffbuf = NULL;
2709 SIMPLEQ_INIT(&refs);
2711 error = buf_alloc(&diffbuf, 0);
2715 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2719 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2724 SIMPLEQ_FOREACH(re, &refs, entry) {
2727 refname = strdup(got_ref_get_name(re->ref));
2728 if (refname == NULL) {
2729 error = got_error_from_errno("got_ref_to_str");
2733 if (strncmp(refname, "refs/heads/", 11) != 0) {
2738 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path, refname,
2743 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2744 refname, gw_trans->repo_name, refname,
2745 gw_trans->repo_name, refname, gw_trans->repo_name,
2747 error = got_error_from_errno("asprintf");
2751 if (strncmp(refname, "refs/heads/", 11) == 0)
2754 if (asprintf(&head_row, heads_row, age, refname,
2755 head_navs_disp) == -1) {
2756 error = got_error_from_errno("asprintf");
2760 error = buf_puts(&newsize, diffbuf, head_row);
2762 free(head_navs_disp);
2766 if (buf_len(diffbuf) > 0) {
2767 error = buf_putc(diffbuf, '\0');
2768 heads = strdup(buf_get(diffbuf));
2772 got_ref_list_free(&refs);
2774 got_repo_close(repo);
2782 gw_get_got_link(struct gw_trans *gw_trans)
2786 if (asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2787 gw_trans->gw_conf->got_logo) == -1)
2794 gw_get_site_link(struct gw_trans *gw_trans)
2796 char *link, *repo = "", *action = "";
2798 if (gw_trans->repo_name != NULL)
2799 if (asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2800 "</a>", gw_trans->repo_name, gw_trans->repo_name) == -1)
2803 if (gw_trans->action_name != NULL)
2804 if (asprintf(&action, " / %s", gw_trans->action_name) == -1)
2807 if (asprintf(&link, site_link, GOTWEB,
2808 gw_trans->gw_conf->got_site_link, repo, action) == -1)
2815 gw_colordiff_line(char *buf)
2817 const struct got_error *error = NULL;
2818 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2819 struct buf *diffbuf = NULL;
2822 error = buf_alloc(&diffbuf, 0);
2828 if (strncmp(buf, "-", 1) == 0)
2829 color = "diff_minus";
2830 if (strncmp(buf, "+", 1) == 0)
2831 color = "diff_plus";
2832 if (strncmp(buf, "@@", 2) == 0)
2833 color = "diff_chunk_header";
2834 if (strncmp(buf, "@@", 2) == 0)
2835 color = "diff_chunk_header";
2836 if (strncmp(buf, "commit +", 8) == 0)
2837 color = "diff_meta";
2838 if (strncmp(buf, "commit -", 8) == 0)
2839 color = "diff_meta";
2840 if (strncmp(buf, "blob +", 6) == 0)
2841 color = "diff_meta";
2842 if (strncmp(buf, "blob -", 6) == 0)
2843 color = "diff_meta";
2844 if (strncmp(buf, "file +", 6) == 0)
2845 color = "diff_meta";
2846 if (strncmp(buf, "file -", 6) == 0)
2847 color = "diff_meta";
2848 if (strncmp(buf, "from:", 5) == 0)
2849 color = "diff_author";
2850 if (strncmp(buf, "via:", 4) == 0)
2851 color = "diff_author";
2852 if (strncmp(buf, "date:", 5) == 0)
2853 color = "diff_date";
2855 if (asprintf(&div_diff_line_div, div_diff_line, color) == -1)
2858 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2862 error = buf_puts(&newsize, diffbuf, buf);
2866 if (buf_len(diffbuf) > 0) {
2867 error = buf_putc(diffbuf, '\0');
2868 colorized_line = strdup(buf_get(diffbuf));
2872 free(div_diff_line_div);
2873 return colorized_line;
2877 gw_html_escape(const char *html)
2879 char *escaped_str = NULL, *buf;
2881 size_t sz, i, buff_sz = 2048;
2883 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2889 if ((sz = strlen(html)) == 0)
2892 /* only work with buff_sz */
2896 for (i = 0; i < sz; i++) {
2900 strcat(buf, ">");
2903 strcat(buf, "&");
2906 strcat(buf, "<");
2909 strcat(buf, """);
2912 strcat(buf, "'");
2915 strcat(buf, "<br />");
2921 asprintf(&escaped_str, "%s", buf);
2927 main(int argc, char *argv[])
2929 const struct got_error *error = NULL;
2930 struct gw_trans *gw_trans;
2931 struct gw_dir *dir = NULL, *tdir;
2932 const char *page = "index";
2936 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2939 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2942 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2945 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2948 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2949 if (kerr != KCGI_OK) {
2950 error = gw_kcgi_error(kerr);
2954 if ((gw_trans->gw_conf =
2955 malloc(sizeof(struct gotweb_conf))) == NULL) {
2957 error = got_error_from_errno("malloc");
2961 TAILQ_INIT(&gw_trans->gw_dirs);
2962 TAILQ_INIT(&gw_trans->gw_headers);
2965 gw_trans->repos_total = 0;
2966 gw_trans->repo_path = NULL;
2967 gw_trans->commit = NULL;
2968 gw_trans->headref = strdup(GOT_REF_HEAD);
2969 gw_trans->mime = KMIME_TEXT_HTML;
2970 gw_trans->gw_tmpl->key = gw_templs;
2971 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2972 gw_trans->gw_tmpl->arg = gw_trans;
2973 gw_trans->gw_tmpl->cb = gw_template;
2974 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2978 error = gw_parse_querystring(gw_trans);
2982 error = gw_display_index(gw_trans);
2985 gw_trans->mime = KMIME_TEXT_PLAIN;
2986 gw_trans->action = GW_ERR;
2987 gw_display_error(gw_trans, error);
2990 free(gw_trans->gw_conf->got_repos_path);
2991 free(gw_trans->gw_conf->got_site_name);
2992 free(gw_trans->gw_conf->got_site_owner);
2993 free(gw_trans->gw_conf->got_site_link);
2994 free(gw_trans->gw_conf->got_logo);
2995 free(gw_trans->gw_conf->got_logo_url);
2996 free(gw_trans->gw_conf);
2997 free(gw_trans->commit);
2998 free(gw_trans->repo_path);
2999 free(gw_trans->repo_name);
3000 free(gw_trans->repo_file);
3001 free(gw_trans->action_name);
3002 free(gw_trans->headref);
3004 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3006 free(dir->description);
3015 khttp_free(gw_trans->gw_req);