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>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
51 #include "gotweb_ui.h"
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 TAILQ_HEAD(headers, gw_header) gw_headers;
59 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
60 struct gw_dir *gw_dir;
61 struct gotweb_conf *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
74 unsigned int repos_total;
79 TAILQ_ENTRY(gw_header) entry;
80 struct got_repository *repo;
81 struct got_reflist_head refs;
82 struct got_commit_object *commit;
83 struct got_object_id *id;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
91 const char *committer;
93 time_t committer_time;
97 TAILQ_ENTRY(gw_dir) entry;
138 static const char *const gw_templs[TEMPL__MAX] = {
148 static const struct kvalid gw_keys[KEY__ZMAX] = {
149 { kvalid_stringne, "action" },
150 { kvalid_stringne, "commit" },
151 { kvalid_stringne, "file" },
152 { kvalid_stringne, "folder" },
153 { kvalid_stringne, "headref" },
154 { kvalid_int, "page" },
155 { kvalid_stringne, "path" },
158 static struct gw_dir *gw_init_gw_dir(char *);
159 static struct gw_header *gw_init_header(void);
161 static const struct got_error *gw_get_repo_description(char **,
162 struct gw_trans *, char *);
163 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
165 static const struct got_error *gw_get_time_str(char **, time_t, int);
166 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
167 char *, char *, int);
168 static const struct got_error *gw_get_file_blame_blob(char **,
170 static const struct got_error *gw_get_file_read_blob(char **, size_t *,
172 static const struct got_error *gw_get_repo_tree(char **, struct gw_trans *);
173 static const struct got_error *gw_get_diff(char **, struct gw_trans *,
175 static const struct got_error *gw_get_repo_tags(char **, struct gw_trans *,
176 struct gw_header *, int, int);
177 static const struct got_error *gw_get_repo_heads(char **, struct gw_trans *);
178 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
180 static char *gw_get_site_link(struct gw_trans *);
181 static const struct got_error *gw_html_escape(char **, const char *);
182 static const struct got_error *gw_colordiff_line(char **, char *);
184 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
186 static char *gw_gen_commit_header_old(char *, char*);
187 static char *gw_gen_diff_header(char *, char*);
188 static const struct got_error *gw_gen_author_header(struct gw_trans *,
190 static char *gw_gen_author_header_old(const char *);
191 static const struct got_error *gw_gen_age_header(struct gw_trans *,
193 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
195 static char *gw_gen_committer_header_old(const char *);
196 static char *gw_gen_commit_msg_header(char *);
197 static char *gw_gen_tree_header(char *);
199 static void gw_free_headers(struct gw_header *);
200 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
202 static const struct got_error* gw_display_index(struct gw_trans *);
203 static void gw_display_error(struct gw_trans *,
204 const struct got_error *);
206 static int gw_template(size_t, void *);
208 static const struct got_error* gw_get_header(struct gw_trans *,
209 struct gw_header *, int);
210 static const struct got_error* gw_get_commits(struct gw_trans *,
211 struct gw_header *, int);
212 static const struct got_error* gw_get_commit(struct gw_trans *,
214 static const struct got_error* gw_apply_unveil(const char *, const char *);
215 static const struct got_error* gw_blame_cb(void *, int, int,
216 struct got_object_id *);
217 static const struct got_error* gw_load_got_paths(struct gw_trans *);
218 static const struct got_error* gw_load_got_path(struct gw_trans *,
220 static const struct got_error* gw_parse_querystring(struct gw_trans *);
222 static const struct got_error* gw_blame(struct gw_trans *);
223 static const struct got_error* gw_blob(struct gw_trans *);
224 static const struct got_error* gw_diff(struct gw_trans *);
225 static const struct got_error* gw_index(struct gw_trans *);
226 static const struct got_error* gw_commits(struct gw_trans *);
227 static const struct got_error* gw_briefs(struct gw_trans *);
228 static const struct got_error* gw_summary(struct gw_trans *);
229 static const struct got_error* gw_tree(struct gw_trans *);
230 static const struct got_error* gw_tag(struct gw_trans *);
232 struct gw_query_action {
233 unsigned int func_id;
234 const char *func_name;
235 const struct got_error *(*func_main)(struct gw_trans *);
239 enum gw_query_actions {
252 static struct gw_query_action gw_query_funcs[] = {
253 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
254 { GW_BLOB, "blob", NULL, NULL },
255 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
256 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
257 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
258 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
259 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
260 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
261 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
262 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
265 static const struct got_error *
266 gw_kcgi_error(enum kcgi_err kerr)
271 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
272 return got_error(GOT_ERR_CANCELLED);
274 if (kerr == KCGI_ENOMEM)
275 return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
277 if (kerr == KCGI_ENFILE)
278 return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
280 if (kerr == KCGI_EAGAIN)
281 return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
283 if (kerr == KCGI_FORM)
284 return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
286 return got_error_from_errno(kcgi_strerror(kerr));
289 static const struct got_error *
290 gw_apply_unveil(const char *repo_path, const char *repo_file)
292 const struct got_error *err;
294 if (repo_path && repo_file) {
296 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
297 return got_error_from_errno("asprintf unveil");
298 if (unveil(full_path, "r") != 0)
299 return got_error_from_errno2("unveil", full_path);
302 if (repo_path && unveil(repo_path, "r") != 0)
303 return got_error_from_errno2("unveil", repo_path);
305 if (unveil("/tmp", "rwc") != 0)
306 return got_error_from_errno2("unveil", "/tmp");
308 err = got_privsep_unveil_exec_helpers();
312 if (unveil(NULL, NULL) != 0)
313 return got_error_from_errno("unveil");
318 static const struct got_error *
319 gw_empty_string(char **s)
323 return got_error_from_errno("strdup");
328 isbinary(const char *buf, size_t n)
330 return (memchr(buf, '\0', n) != NULL);
333 static const struct got_error *
334 gw_blame(struct gw_trans *gw_trans)
336 const struct got_error *error = NULL;
337 struct gw_header *header = NULL;
338 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
339 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
342 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
344 return got_error_from_errno("pledge");
346 if ((header = gw_init_header()) == NULL)
347 return got_error_from_errno("malloc");
349 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
353 error = gw_get_header(gw_trans, header, 1);
357 error = gw_get_file_blame_blob(&blame_html, gw_trans);
361 error = gw_get_time_str(&age, header->committer_time, TM_LONG);
364 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
365 error = got_error_from_errno("asprintf");
369 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
372 if (asprintf(&blame_html_disp, blame_header, age_html,
373 gw_gen_commit_msg_header(escaped_commit_msg), blame_html) == -1) {
374 error = got_error_from_errno("asprintf");
378 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
379 error = got_error_from_errno("asprintf");
383 kerr = khttp_puts(gw_trans->gw_req, blame);
385 error = gw_kcgi_error(kerr);
387 got_ref_list_free(&header->refs);
388 gw_free_headers(header);
389 free(blame_html_disp);
392 free(escaped_commit_msg);
396 static const struct got_error *
397 gw_blob(struct gw_trans *gw_trans)
399 const struct got_error *error = NULL;
400 struct gw_header *header = NULL;
401 char *content = NULL;
405 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
407 return got_error_from_errno("pledge");
409 if ((header = gw_init_header()) == NULL)
410 return got_error_from_errno("malloc");
412 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
416 error = gw_get_header(gw_trans, header, 1);
420 error = gw_get_file_read_blob(&content, &filesize, gw_trans);
424 if (isbinary(content, filesize))
425 gw_trans->mime = KMIME_APP_OCTET_STREAM;
427 gw_trans->mime = KMIME_TEXT_PLAIN;
429 error = gw_display_index(gw_trans);
433 kerr = khttp_write(gw_trans->gw_req, content, filesize);
435 error = gw_kcgi_error(kerr);
437 got_ref_list_free(&header->refs);
438 gw_free_headers(header);
443 static const struct got_error *
444 gw_diff(struct gw_trans *gw_trans)
446 const struct got_error *error = NULL;
447 struct gw_header *header = NULL;
448 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
449 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
452 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
454 return got_error_from_errno("pledge");
456 if ((header = gw_init_header()) == NULL)
457 return got_error_from_errno("malloc");
459 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
463 error = gw_get_header(gw_trans, header, 1);
467 error = gw_get_diff(&diff_html, gw_trans, header);
471 error = gw_get_time_str(&age, header->committer_time, TM_LONG);
474 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
475 error = got_error_from_errno("asprintf");
478 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
481 if (asprintf(&diff_html_disp, diff_header,
482 gw_gen_diff_header(header->parent_id, header->commit_id),
483 gw_gen_commit_header_old(header->commit_id, header->refs_str),
484 gw_gen_tree_header(header->tree_id),
485 gw_gen_author_header_old(header->author),
486 gw_gen_committer_header_old(header->committer), age_html,
487 gw_gen_commit_msg_header(escaped_commit_msg),
488 diff_html ? diff_html : "") == -1) {
489 error = got_error_from_errno("asprintf");
493 if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
494 error = got_error_from_errno("asprintf");
498 kerr = khttp_puts(gw_trans->gw_req, diff);
500 error = gw_kcgi_error(kerr);
502 got_ref_list_free(&header->refs);
503 gw_free_headers(header);
504 free(diff_html_disp);
509 free(escaped_commit_msg);
513 static const struct got_error *
514 gw_index(struct gw_trans *gw_trans)
516 const struct got_error *error = NULL;
517 struct gw_dir *gw_dir = NULL;
518 char *html, *navs, *next, *prev;
519 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
522 if (pledge("stdio rpath proc exec sendfd unveil",
524 error = got_error_from_errno("pledge");
528 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
532 error = gw_load_got_paths(gw_trans);
536 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
538 return gw_kcgi_error(kerr);
540 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
541 if (asprintf(&html, index_projects_empty,
542 gw_trans->gw_conf->got_repos_path) == -1)
543 return got_error_from_errno("asprintf");
544 kerr = khttp_puts(gw_trans->gw_req, html);
546 error = gw_kcgi_error(kerr);
551 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
554 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
555 if (gw_trans->page > 0 && (gw_trans->page *
556 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
565 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
566 gw_dir->name, gw_dir->name) == -1)
567 return got_error_from_errno("asprintf");
569 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
570 gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
573 return got_error_from_errno("asprintf");
575 kerr = khttp_puts(gw_trans->gw_req, html);
579 return gw_kcgi_error(kerr);
581 if (gw_trans->gw_conf->got_max_repos_display == 0)
584 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
585 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
587 return gw_kcgi_error(kerr);
588 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
589 (gw_trans->page > 0) &&
590 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
591 prev_disp == gw_trans->repos_total)) {
592 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
594 return gw_kcgi_error(kerr);
597 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
598 (gw_trans->page > 0) &&
599 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
600 prev_disp == gw_trans->repos_total)) {
601 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
602 return got_error_from_errno("asprintf");
603 kerr = khttp_puts(gw_trans->gw_req, prev);
606 return gw_kcgi_error(kerr);
609 kerr = khttp_puts(gw_trans->gw_req, div_end);
611 return gw_kcgi_error(kerr);
613 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
614 next_disp == gw_trans->gw_conf->got_max_repos_display &&
615 dir_c != (gw_trans->page + 1) *
616 gw_trans->gw_conf->got_max_repos_display) {
617 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
618 return got_error_from_errno("calloc");
619 kerr = khttp_puts(gw_trans->gw_req, next);
622 return gw_kcgi_error(kerr);
623 kerr = khttp_puts(gw_trans->gw_req, div_end);
625 error = gw_kcgi_error(kerr);
630 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
631 (gw_trans->page > 0) &&
632 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
633 prev_disp == gw_trans->repos_total)) {
634 kerr = khttp_puts(gw_trans->gw_req, div_end);
636 return gw_kcgi_error(kerr);
644 static const struct got_error *
645 gw_commits(struct gw_trans *gw_trans)
647 const struct got_error *error = NULL;
648 struct gw_header *header = NULL, *n_header = NULL;
649 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
650 char *href_diff = NULL, *href_tree = NULL;
651 enum kcgi_err kerr = KCGI_OK;
653 if ((header = gw_init_header()) == NULL)
654 return got_error_from_errno("malloc");
656 if (pledge("stdio rpath proc exec sendfd unveil",
658 error = got_error_from_errno("pledge");
662 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
666 error = gw_get_header(gw_trans, header,
667 gw_trans->gw_conf->got_max_commits_display);
671 /* commit briefs header */
672 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
673 "commits_title_wrapper", KATTR__MAX);
676 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
677 "commits_title", KATTR__MAX);
680 kerr = khtml_puts(gw_trans->gw_html_req, "Commits");
683 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
688 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
689 "commits_content", KATTR__MAX);
692 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
694 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
695 "commits_line_wrapper", KATTR__MAX);
698 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
700 error = gw_gen_author_header(gw_trans, n_header->author);
703 error = gw_gen_committer_header(gw_trans, n_header->author);
706 error = gw_get_time_str(&age, n_header->committer_time,
710 error = gw_gen_age_header(gw_trans, age ?age : "");
713 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
718 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
719 "dotted_line", KATTR__MAX);
722 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
727 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
728 "commit", KATTR__MAX);
732 * XXX: keeping this for now, since kcgihtml does not convert
733 * \n into <br /> yet.
735 error = gw_html_escape(&escaped_commit_msg,
736 n_header->commit_msg);
739 kerr = khttp_puts(gw_trans->gw_req, escaped_commit_msg);
742 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
748 /* XXX: create gen code for this */
750 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
751 gw_trans->repo_name, n_header->commit_id) == -1) {
752 error = got_error_from_errno("asprintf");
755 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
756 KATTR_ID, "navs_wrapper", KATTR__MAX);
759 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
760 KATTR_ID, "navs", KATTR__MAX);
763 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
764 KATTR_HREF, href_diff, KATTR__MAX);
767 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
770 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
774 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
778 /* XXX: create gen code for this */
780 if (asprintf(&href_tree, "?path=%s&action=tree&commit=%s",
781 gw_trans->repo_name, n_header->commit_id) == -1) {
782 error = got_error_from_errno("asprintf");
785 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
786 KATTR_HREF, href_tree, KATTR__MAX);
789 khtml_puts(gw_trans->gw_html_req, "tree");
790 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
793 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
797 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
798 "solid_line", KATTR__MAX);
801 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
809 free(escaped_commit_msg);
810 escaped_commit_msg = NULL;
812 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
814 got_ref_list_free(&header->refs);
815 gw_free_headers(header);
816 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
817 gw_free_headers(n_header);
822 free(escaped_commit_msg);
823 if (error == NULL && kerr != KCGI_OK)
824 error = gw_kcgi_error(kerr);
828 static const struct got_error *
829 gw_briefs(struct gw_trans *gw_trans)
831 const struct got_error *error = NULL;
832 struct gw_header *header = NULL, *n_header = NULL;
833 char *age = NULL, *age_html = NULL;
834 char *href_diff = NULL, *href_tree = NULL;
835 char *newline, *smallerthan;
836 enum kcgi_err kerr = KCGI_OK;
838 if ((header = gw_init_header()) == NULL)
839 return got_error_from_errno("malloc");
841 if (pledge("stdio rpath proc exec sendfd unveil",
843 error = got_error_from_errno("pledge");
847 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
851 if (gw_trans->action == GW_SUMMARY)
852 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
854 error = gw_get_header(gw_trans, header,
855 gw_trans->gw_conf->got_max_commits_display);
859 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
860 error = gw_get_time_str(&age, n_header->committer_time,
866 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
867 KATTR_ID, "briefs_wrapper", KATTR__MAX);
872 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
873 KATTR_ID, "briefs_age", KATTR__MAX);
876 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
877 error = got_error_from_errno("asprintf");
880 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
883 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
888 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
889 KATTR_ID, "briefs_author", KATTR__MAX);
892 smallerthan = strchr(n_header->author, '<');
895 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
898 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
903 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
904 gw_trans->repo_name, n_header->commit_id) == -1) {
905 error = got_error_from_errno("asprintf");
908 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
909 KATTR_ID, "briefs_log", KATTR__MAX);
912 newline = strchr(n_header->commit_msg, '\n');
915 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
916 KATTR_HREF, href_diff, KATTR__MAX);
919 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
922 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
927 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
928 KATTR_ID, "navs_wrapper", KATTR__MAX);
931 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
932 KATTR_ID, "navs", KATTR__MAX);
935 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
936 KATTR_HREF, href_diff, KATTR__MAX);
939 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
942 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
946 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
951 if (asprintf(&href_tree, "?path=%s&action=tree&commit=%s",
952 gw_trans->repo_name, n_header->commit_id) == -1) {
953 error = got_error_from_errno("asprintf");
956 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
957 KATTR_HREF, href_tree, KATTR__MAX);
960 khtml_puts(gw_trans->gw_html_req, "tree");
961 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
964 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
969 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
970 KATTR_ID, "dotted_line", KATTR__MAX);
973 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
987 got_ref_list_free(&header->refs);
988 gw_free_headers(header);
989 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
990 gw_free_headers(n_header);
995 if (error == NULL && kerr != KCGI_OK)
996 error = gw_kcgi_error(kerr);
1000 static const struct got_error *
1001 gw_summary(struct gw_trans *gw_trans)
1003 const struct got_error *error = NULL;
1004 char *age = NULL, *tags = NULL, *heads = NULL;
1005 enum kcgi_err kerr = KCGI_OK;
1007 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1008 return got_error_from_errno("pledge");
1010 /* unveil is applied with gw_briefs below */
1012 /* summary wrapper */
1013 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1014 "summary_wrapper", KATTR__MAX);
1015 if (kerr != KCGI_OK)
1016 return gw_kcgi_error(kerr);
1019 if (gw_trans->gw_conf->got_show_repo_description &&
1020 gw_trans->gw_dir->description != NULL &&
1021 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1022 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1023 KATTR_ID, "description_title", KATTR__MAX);
1024 if (kerr != KCGI_OK)
1026 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1027 if (kerr != KCGI_OK)
1029 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1030 if (kerr != KCGI_OK)
1032 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1033 KATTR_ID, "description", KATTR__MAX);
1034 if (kerr != KCGI_OK)
1036 kerr = khtml_puts(gw_trans->gw_html_req,
1037 gw_trans->gw_dir->description);
1038 if (kerr != KCGI_OK)
1040 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1041 if (kerr != KCGI_OK)
1046 if (gw_trans->gw_conf->got_show_repo_owner &&
1047 gw_trans->gw_dir->owner != NULL) {
1048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1049 KATTR_ID, "repo_owner_title", KATTR__MAX);
1050 if (kerr != KCGI_OK)
1052 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1053 if (kerr != KCGI_OK)
1055 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1056 if (kerr != KCGI_OK)
1058 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1059 KATTR_ID, "repo_owner", KATTR__MAX);
1060 if (kerr != KCGI_OK)
1062 kerr = khtml_puts(gw_trans->gw_html_req,
1063 gw_trans->gw_dir->owner);
1064 if (kerr != KCGI_OK)
1066 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1067 if (kerr != KCGI_OK)
1072 if (gw_trans->gw_conf->got_show_repo_age) {
1073 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1074 "refs/heads", TM_LONG);
1078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1079 KATTR_ID, "last_change_title", KATTR__MAX);
1080 if (kerr != KCGI_OK)
1082 kerr = khtml_puts(gw_trans->gw_html_req,
1084 if (kerr != KCGI_OK)
1086 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1087 if (kerr != KCGI_OK)
1089 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1090 KATTR_ID, "last_change", KATTR__MAX);
1091 if (kerr != KCGI_OK)
1093 kerr = khtml_puts(gw_trans->gw_html_req, age);
1094 if (kerr != KCGI_OK)
1096 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1097 if (kerr != KCGI_OK)
1103 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1104 gw_trans->gw_dir->url != NULL &&
1105 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1107 KATTR_ID, "cloneurl_title", KATTR__MAX);
1108 if (kerr != KCGI_OK)
1110 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1111 if (kerr != KCGI_OK)
1113 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1114 if (kerr != KCGI_OK)
1116 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1117 KATTR_ID, "cloneurl", KATTR__MAX);
1118 if (kerr != KCGI_OK)
1120 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1121 if (kerr != KCGI_OK)
1123 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1124 if (kerr != KCGI_OK)
1128 /* close summary wrapper */
1129 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1130 if (kerr != KCGI_OK)
1133 /* commit briefs header */
1134 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1135 "briefs_title_wrapper", KATTR__MAX);
1136 if (kerr != KCGI_OK)
1138 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1139 "briefs_title", KATTR__MAX);
1140 if (kerr != KCGI_OK)
1142 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1143 if (kerr != KCGI_OK)
1145 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1146 if (kerr != KCGI_OK)
1148 error = gw_briefs(gw_trans);
1153 error = gw_get_repo_tags(&tags, gw_trans, NULL, D_MAXSLCOMMDISP,
1158 if (tags != NULL && strcmp(tags, "") != 0) {
1159 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1160 KATTR_ID, "summary_tags_title_wrapper", KATTR__MAX);
1161 if (kerr != KCGI_OK)
1163 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1164 KATTR_ID, "summary_tags_title", KATTR__MAX);
1165 if (kerr != KCGI_OK)
1167 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
1168 if (kerr != KCGI_OK)
1170 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1171 if (kerr != KCGI_OK)
1173 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1174 KATTR_ID, "summary_tags_content", KATTR__MAX);
1175 if (kerr != KCGI_OK)
1177 kerr = khttp_puts(gw_trans->gw_req, tags);
1178 if (kerr != KCGI_OK)
1180 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1181 if (kerr != KCGI_OK)
1186 error = gw_get_repo_heads(&heads, gw_trans);
1189 if (heads != NULL && strcmp(heads, "") != 0) {
1190 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1191 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
1192 if (kerr != KCGI_OK)
1194 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1195 KATTR_ID, "summary_heads_title", KATTR__MAX);
1196 if (kerr != KCGI_OK)
1198 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
1199 if (kerr != KCGI_OK)
1201 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1202 if (kerr != KCGI_OK)
1204 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1205 KATTR_ID, "summary_heads_content", KATTR__MAX);
1206 if (kerr != KCGI_OK)
1208 kerr = khttp_puts(gw_trans->gw_req, heads);
1209 if (kerr != KCGI_OK)
1211 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1212 if (kerr != KCGI_OK)
1216 if (kerr != KCGI_OK)
1217 error = gw_kcgi_error(kerr);
1224 static const struct got_error *
1225 gw_tree(struct gw_trans *gw_trans)
1227 const struct got_error *error = NULL;
1228 struct gw_header *header = NULL;
1229 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1230 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
1233 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1234 return got_error_from_errno("pledge");
1236 if ((header = gw_init_header()) == NULL)
1237 return got_error_from_errno("malloc");
1239 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1243 error = gw_get_header(gw_trans, header, 1);
1247 error = gw_get_repo_tree(&tree_html, gw_trans);
1251 error = gw_get_time_str(&age, header->committer_time, TM_LONG);
1254 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
1255 error = got_error_from_errno("asprintf");
1258 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1261 if (asprintf(&tree_html_disp, tree_header, age_html,
1262 gw_gen_commit_msg_header(escaped_commit_msg),
1263 tree_html ? tree_html : "") == -1) {
1264 error = got_error_from_errno("asprintf");
1268 if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
1269 error = got_error_from_errno("asprintf");
1273 kerr = khttp_puts(gw_trans->gw_req, tree);
1274 if (kerr != KCGI_OK)
1275 error = gw_kcgi_error(kerr);
1277 got_ref_list_free(&header->refs);
1278 gw_free_headers(header);
1279 free(tree_html_disp);
1284 free(escaped_commit_msg);
1288 static const struct got_error *
1289 gw_tag(struct gw_trans *gw_trans)
1291 const struct got_error *error = NULL;
1292 struct gw_header *header = NULL;
1293 char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
1294 char *escaped_commit_msg = NULL;
1297 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1298 return got_error_from_errno("pledge");
1300 if ((header = gw_init_header()) == NULL)
1301 return got_error_from_errno("malloc");
1303 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1307 error = gw_get_header(gw_trans, header, 1);
1311 error = gw_get_repo_tags(&tag_html, gw_trans, header, 1, TAGFULL);
1315 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1318 if (asprintf(&tag_html_disp, tag_header,
1319 gw_gen_commit_header_old(header->commit_id, header->refs_str),
1320 gw_gen_commit_msg_header(escaped_commit_msg),
1321 tag_html ? tag_html : "") == -1) {
1322 error = got_error_from_errno("asprintf");
1326 if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
1327 error = got_error_from_errno("asprintf");
1331 kerr = khttp_puts(gw_trans->gw_req, tag);
1332 if (kerr != KCGI_OK)
1333 error = gw_kcgi_error(kerr);
1335 got_ref_list_free(&header->refs);
1336 gw_free_headers(header);
1337 free(tag_html_disp);
1340 free(escaped_commit_msg);
1344 static const struct got_error *
1345 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1347 const struct got_error *error = NULL;
1352 if (asprintf(&dir_test, "%s/%s/%s",
1353 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1354 GOTWEB_GIT_DIR) == -1)
1355 return got_error_from_errno("asprintf");
1357 dt = opendir(dir_test);
1361 gw_dir->path = strdup(dir_test);
1366 if (asprintf(&dir_test, "%s/%s/%s",
1367 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1368 GOTWEB_GOT_DIR) == -1)
1369 return got_error_from_errno("asprintf");
1371 dt = opendir(dir_test);
1376 error = got_error(GOT_ERR_NOT_GIT_REPO);
1380 if (asprintf(&dir_test, "%s/%s",
1381 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1382 return got_error_from_errno("asprintf");
1384 gw_dir->path = strdup(dir_test);
1387 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1391 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1394 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1395 "refs/heads", TM_DIFF);
1398 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1406 static const struct got_error *
1407 gw_load_got_paths(struct gw_trans *gw_trans)
1409 const struct got_error *error = NULL;
1411 struct dirent **sd_dent;
1412 struct gw_dir *gw_dir;
1414 unsigned int d_cnt, d_i;
1416 d = opendir(gw_trans->gw_conf->got_repos_path);
1418 error = got_error_from_errno2("opendir",
1419 gw_trans->gw_conf->got_repos_path);
1423 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1426 error = got_error_from_errno2("scandir",
1427 gw_trans->gw_conf->got_repos_path);
1431 for (d_i = 0; d_i < d_cnt; d_i++) {
1432 if (gw_trans->gw_conf->got_max_repos > 0 &&
1433 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1434 break; /* account for parent and self */
1436 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1437 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1440 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1441 return got_error_from_errno("gw_dir malloc");
1443 error = gw_load_got_path(gw_trans, gw_dir);
1444 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1449 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1450 !got_path_dir_is_empty(gw_dir->path)) {
1451 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1453 gw_trans->repos_total++;
1461 static const struct got_error *
1462 gw_parse_querystring(struct gw_trans *gw_trans)
1464 const struct got_error *error = NULL;
1466 struct gw_query_action *action = NULL;
1469 if (gw_trans->gw_req->fieldnmap[0]) {
1470 error = got_error_from_errno("bad parse");
1472 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1473 /* define gw_trans->repo_path */
1474 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1475 return got_error_from_errno("asprintf");
1477 if (asprintf(&gw_trans->repo_path, "%s/%s",
1478 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1479 return got_error_from_errno("asprintf");
1481 /* get action and set function */
1482 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1483 for (i = 0; i < nitems(gw_query_funcs); i++) {
1484 action = &gw_query_funcs[i];
1485 if (action->func_name == NULL)
1488 if (strcmp(action->func_name,
1489 p->parsed.s) == 0) {
1490 gw_trans->action = i;
1491 if (asprintf(&gw_trans->action_name,
1492 "%s", action->func_name) == -1)
1494 got_error_from_errno(
1503 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1504 if (asprintf(&gw_trans->commit, "%s",
1506 return got_error_from_errno("asprintf");
1508 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1509 if (asprintf(&gw_trans->repo_file, "%s",
1511 return got_error_from_errno("asprintf");
1513 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1514 if (asprintf(&gw_trans->repo_folder, "%s",
1516 return got_error_from_errno("asprintf");
1518 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1519 if (asprintf(&gw_trans->headref, "%s",
1521 return got_error_from_errno("asprintf");
1523 if (action == NULL) {
1524 error = got_error_from_errno("invalid action");
1527 if ((gw_trans->gw_dir =
1528 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1529 return got_error_from_errno("gw_dir malloc");
1531 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1535 gw_trans->action = GW_INDEX;
1537 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1538 gw_trans->page = p->parsed.i;
1543 static struct gw_dir *
1544 gw_init_gw_dir(char *dir)
1546 struct gw_dir *gw_dir;
1548 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1551 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1557 static const struct got_error *
1558 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1562 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1563 if (kerr != KCGI_OK)
1564 return gw_kcgi_error(kerr);
1565 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1567 if (kerr != KCGI_OK)
1568 return gw_kcgi_error(kerr);
1569 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1571 if (kerr != KCGI_OK)
1572 return gw_kcgi_error(kerr);
1573 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1575 if (kerr != KCGI_OK)
1576 return gw_kcgi_error(kerr);
1577 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1578 if (kerr != KCGI_OK)
1579 return gw_kcgi_error(kerr);
1580 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1582 if (kerr != KCGI_OK)
1583 return gw_kcgi_error(kerr);
1585 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1586 kerr = khttp_head(gw_trans->gw_req,
1587 kresps[KRESP_CONTENT_DISPOSITION],
1588 "attachment; filename=%s", gw_trans->repo_file);
1589 if (kerr != KCGI_OK)
1590 return gw_kcgi_error(kerr);
1593 kerr = khttp_body(gw_trans->gw_req);
1594 return gw_kcgi_error(kerr);
1597 static const struct got_error *
1598 gw_display_index(struct gw_trans *gw_trans)
1600 const struct got_error *error;
1603 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1607 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, KHTML_PRETTY);
1609 return gw_kcgi_error(kerr);
1611 if (gw_trans->action != GW_BLOB) {
1612 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1613 gw_query_funcs[gw_trans->action].template);
1614 if (kerr != KCGI_OK) {
1615 khtml_close(gw_trans->gw_html_req);
1616 return gw_kcgi_error(kerr);
1620 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1624 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1626 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1629 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1631 khtml_puts(gw_trans->gw_html_req, err->msg);
1632 khtml_close(gw_trans->gw_html_req);
1636 gw_template(size_t key, void *arg)
1638 const struct got_error *error = NULL;
1640 struct gw_trans *gw_trans = arg;
1641 char *gw_site_link, *img_src = NULL;
1645 kerr = khttp_puts(gw_trans->gw_req, head);
1646 if (kerr != KCGI_OK)
1650 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1651 KATTR_ID, "got_link", KATTR__MAX);
1652 if (kerr != KCGI_OK)
1654 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1655 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1656 KATTR_TARGET, "_sotd", KATTR__MAX);
1657 if (kerr != KCGI_OK)
1659 if (asprintf(&img_src, "/%s",
1660 gw_trans->gw_conf->got_logo) == -1)
1662 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1663 KATTR_SRC, img_src, KATTR__MAX);
1664 if (kerr != KCGI_OK) {
1668 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1669 if (kerr != KCGI_OK) {
1674 case (TEMPL_SITEPATH):
1675 gw_site_link = gw_get_site_link(gw_trans);
1676 if (gw_site_link != NULL) {
1677 kerr = khttp_puts(gw_trans->gw_req, gw_site_link);
1678 if (kerr != KCGI_OK) {
1686 if (gw_trans->gw_conf->got_site_name != NULL) {
1687 kerr = khtml_puts(gw_trans->gw_html_req,
1688 gw_trans->gw_conf->got_site_name);
1689 if (kerr != KCGI_OK)
1693 case (TEMPL_SEARCH):
1694 kerr = khttp_puts(gw_trans->gw_req, search);
1695 if (kerr != KCGI_OK)
1698 case(TEMPL_SITEOWNER):
1699 if (gw_trans->gw_conf->got_site_owner != NULL &&
1700 gw_trans->gw_conf->got_show_site_owner) {
1701 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1702 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1703 if (kerr != KCGI_OK)
1705 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1706 KATTR_ID, "site_owner", KATTR__MAX);
1707 if (kerr != KCGI_OK)
1709 kerr = khtml_puts(gw_trans->gw_html_req,
1710 gw_trans->gw_conf->got_site_owner);
1711 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1712 if (kerr != KCGI_OK)
1716 case(TEMPL_CONTENT):
1717 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1719 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1720 if (kerr != KCGI_OK)
1731 gw_gen_commit_header_old(char *str1, char *str2)
1733 char *return_html = NULL, *ref_str = NULL;
1735 if (strcmp(str2, "") != 0) {
1736 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1737 return_html = strdup("");
1741 ref_str = strdup("");
1743 if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1744 return_html = strdup("");
1750 static const struct got_error *
1751 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1753 const struct got_error *error = NULL;
1754 char *ref_str = NULL;
1755 enum kcgi_err kerr = KCGI_OK;
1757 if (strcmp(str2, "") != 0) {
1758 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1759 error = got_error_from_errno("asprintf");
1763 ref_str = strdup("");
1765 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1766 KATTR_ID, "header_commit_title", KATTR__MAX);
1767 if (kerr != KCGI_OK)
1769 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
1770 if (kerr != KCGI_OK)
1772 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1773 if (kerr != KCGI_OK)
1775 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1776 KATTR_ID, "header_commit", KATTR__MAX);
1777 if (kerr != KCGI_OK)
1779 kerr = khtml_puts(gw_trans->gw_html_req, str1);
1780 if (kerr != KCGI_OK)
1782 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1783 if (kerr != KCGI_OK)
1785 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
1786 if (kerr != KCGI_OK)
1788 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1789 if (kerr != KCGI_OK)
1792 if (error == NULL && kerr != KCGI_OK)
1793 error = gw_kcgi_error(kerr);
1798 gw_gen_diff_header(char *str1, char *str2)
1800 char *return_html = NULL;
1802 if (asprintf(&return_html, header_diff_html, str1, str2) == -1)
1803 return_html = strdup("");
1808 /* XXX: slated for deletion */
1810 gw_gen_author_header_old(const char *str)
1812 const struct got_error *error = NULL;
1813 char *return_html = NULL;
1814 char *escaped_html = NULL;
1816 error = gw_html_escape(&escaped_html, str);
1819 if (asprintf(&return_html, header_author_html, escaped_html) == -1)
1820 return_html = strdup("");
1826 static const struct got_error *
1827 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
1829 const struct got_error *error = NULL;
1830 enum kcgi_err kerr = KCGI_OK;
1832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1833 KATTR_ID, "header_age_title", KATTR__MAX);
1834 if (kerr != KCGI_OK)
1836 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
1837 if (kerr != KCGI_OK)
1839 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1840 if (kerr != KCGI_OK)
1842 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1843 KATTR_ID, "header_age", KATTR__MAX);
1844 if (kerr != KCGI_OK)
1846 kerr = khtml_puts(gw_trans->gw_html_req, str);
1847 if (kerr != KCGI_OK)
1849 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1850 if (kerr != KCGI_OK)
1853 if (error == NULL && kerr != KCGI_OK)
1854 error = gw_kcgi_error(kerr);
1858 static const struct got_error *
1859 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
1861 const struct got_error *error = NULL;
1864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1865 KATTR_ID, "header_author_title", KATTR__MAX);
1866 if (kerr != KCGI_OK)
1868 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
1869 if (kerr != KCGI_OK)
1871 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1872 if (kerr != KCGI_OK)
1874 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1875 KATTR_ID, "header_author", KATTR__MAX);
1876 if (kerr != KCGI_OK)
1878 kerr = khtml_puts(gw_trans->gw_html_req, str);
1879 if (kerr != KCGI_OK)
1881 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1882 if (kerr != KCGI_OK)
1885 if (error == NULL && kerr != KCGI_OK)
1886 error = gw_kcgi_error(kerr);
1890 static const struct got_error *
1891 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
1893 const struct got_error *error = NULL;
1896 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1897 KATTR_ID, "header_committer_title", KATTR__MAX);
1898 if (kerr != KCGI_OK)
1900 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
1901 if (kerr != KCGI_OK)
1903 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1904 if (kerr != KCGI_OK)
1906 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1907 KATTR_ID, "header_committer", KATTR__MAX);
1908 if (kerr != KCGI_OK)
1910 kerr = khtml_puts(gw_trans->gw_html_req, str);
1911 if (kerr != KCGI_OK)
1913 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1914 if (kerr != KCGI_OK)
1917 if (error == NULL && kerr != KCGI_OK)
1918 error = gw_kcgi_error(kerr);
1922 /* XXX: slated for deletion */
1924 gw_gen_committer_header_old(const char *str)
1926 const struct got_error *error = NULL;
1927 char *return_html = NULL;
1928 char *escaped_html = NULL;
1930 error = gw_html_escape(&escaped_html, str);
1933 if (asprintf(&return_html, header_committer_html, escaped_html) == -1)
1934 return_html = strdup("");
1941 gw_gen_commit_msg_header(char *str)
1943 char *return_html = NULL;
1945 if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1946 return_html = strdup("");
1952 gw_gen_tree_header(char *str)
1954 char *return_html = NULL;
1956 if (asprintf(&return_html, header_tree_html, str) == -1)
1957 return_html = strdup("");
1962 static const struct got_error *
1963 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
1966 const struct got_error *error = NULL;
1968 char *d_file = NULL;
1972 *description = NULL;
1973 if (gw_trans->gw_conf->got_show_repo_description == 0)
1974 return gw_empty_string(description);
1976 if (asprintf(&d_file, "%s/description", dir) == -1)
1977 return got_error_from_errno("asprintf");
1979 f = fopen(d_file, "r");
1981 if (errno == ENOENT || errno == EACCES)
1982 return gw_empty_string(description);
1983 error = got_error_from_errno2("fopen", d_file);
1987 if (fseek(f, 0, SEEK_END) == -1) {
1988 error = got_ferror(f, GOT_ERR_IO);
1993 error = got_ferror(f, GOT_ERR_IO);
1996 if (fseek(f, 0, SEEK_SET) == -1) {
1997 error = got_ferror(f, GOT_ERR_IO);
2000 *description = calloc(len + 1, sizeof(**description));
2001 if (*description == NULL) {
2002 error = got_error_from_errno("calloc");
2006 n = fread(*description, 1, len, f);
2007 if (n == 0 && ferror(f))
2008 error = got_ferror(f, GOT_ERR_IO);
2010 if (f != NULL && fclose(f) == -1 && error == NULL)
2011 error = got_error_from_errno("fclose");
2016 static const struct got_error *
2017 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2021 char *years = "years ago", *months = "months ago";
2022 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2023 char *minutes = "minutes ago", *seconds = "seconds ago";
2024 char *now = "right now";
2032 diff_time = time(NULL) - committer_time;
2033 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2034 if (asprintf(repo_age, "%lld %s",
2035 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2036 return got_error_from_errno("asprintf");
2037 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2038 if (asprintf(repo_age, "%lld %s",
2039 (diff_time / 60 / 60 / 24 / (365 / 12)),
2041 return got_error_from_errno("asprintf");
2042 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2043 if (asprintf(repo_age, "%lld %s",
2044 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2045 return got_error_from_errno("asprintf");
2046 } else if (diff_time > 60 * 60 * 24 * 2) {
2047 if (asprintf(repo_age, "%lld %s",
2048 (diff_time / 60 / 60 / 24), days) == -1)
2049 return got_error_from_errno("asprintf");
2050 } else if (diff_time > 60 * 60 * 2) {
2051 if (asprintf(repo_age, "%lld %s",
2052 (diff_time / 60 / 60), hours) == -1)
2053 return got_error_from_errno("asprintf");
2054 } else if (diff_time > 60 * 2) {
2055 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2057 return got_error_from_errno("asprintf");
2058 } else if (diff_time > 2) {
2059 if (asprintf(repo_age, "%lld %s", diff_time,
2061 return got_error_from_errno("asprintf");
2063 if (asprintf(repo_age, "%s", now) == -1)
2064 return got_error_from_errno("asprintf");
2068 if (gmtime_r(&committer_time, &tm) == NULL)
2069 return got_error_from_errno("gmtime_r");
2071 s = asctime_r(&tm, datebuf);
2073 return got_error_from_errno("asctime_r");
2075 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2076 return got_error_from_errno("asprintf");
2082 static const struct got_error *
2083 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2084 char *repo_ref, int ref_tm)
2086 const struct got_error *error = NULL;
2087 struct got_object_id *id = NULL;
2088 struct got_repository *repo = NULL;
2089 struct got_commit_object *commit = NULL;
2090 struct got_reflist_head refs;
2091 struct got_reflist_entry *re;
2092 struct got_reference *head_ref;
2094 time_t committer_time = 0, cmp_time = 0;
2095 const char *refname;
2098 SIMPLEQ_INIT(&refs);
2100 if (repo_ref == NULL)
2103 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2106 if (gw_trans->gw_conf->got_show_repo_age == 0)
2109 error = got_repo_open(&repo, dir, NULL);
2114 error = got_ref_list(&refs, repo, "refs/heads",
2115 got_ref_cmp_by_name, NULL);
2117 error = got_ref_list(&refs, repo, repo_ref,
2118 got_ref_cmp_by_name, NULL);
2122 SIMPLEQ_FOREACH(re, &refs, entry) {
2124 refname = strdup(repo_ref);
2126 refname = got_ref_get_name(re->ref);
2127 error = got_ref_open(&head_ref, repo, refname, 0);
2131 error = got_ref_resolve(&id, repo, head_ref);
2132 got_ref_close(head_ref);
2136 error = got_object_open_as_commit(&commit, repo, id);
2141 got_object_commit_get_committer_time(commit);
2143 if (cmp_time < committer_time)
2144 cmp_time = committer_time;
2147 if (cmp_time != 0) {
2148 committer_time = cmp_time;
2149 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2152 got_ref_list_free(&refs);
2157 static const struct got_error *
2158 gw_get_diff(char **diff_html, struct gw_trans *gw_trans,
2159 struct gw_header *header)
2161 const struct got_error *error;
2163 struct got_object_id *id1 = NULL, *id2 = NULL;
2164 struct buf *diffbuf = NULL;
2165 char *label1 = NULL, *label2 = NULL, *line = NULL;
2166 char *diff_line_html = NULL;
2168 size_t newsize, linesize = 0;
2175 error = buf_alloc(&diffbuf, 0);
2179 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2183 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2184 error = got_repo_match_object_id(&id1, &label1,
2185 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2190 error = got_repo_match_object_id(&id2, &label2,
2191 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2195 error = got_object_get_type(&obj_type, header->repo, id2);
2199 case GOT_OBJ_TYPE_BLOB:
2200 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2203 case GOT_OBJ_TYPE_TREE:
2204 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2207 case GOT_OBJ_TYPE_COMMIT:
2208 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2212 error = got_error(GOT_ERR_OBJ_TYPE);
2217 if (fseek(f, 0, SEEK_SET) == -1) {
2218 error = got_ferror(f, GOT_ERR_IO);
2222 while ((linelen = getline(&line, &linesize, f)) != -1) {
2224 error = gw_html_escape(&escaped_line, line);
2228 error = gw_colordiff_line(&diff_line_html, escaped_line);
2232 error = buf_puts(&newsize, diffbuf, diff_line_html);
2236 error = buf_puts(&newsize, diffbuf, div_end);
2240 if (linelen == -1 && ferror(f)) {
2241 error = got_error_from_errno("getline");
2245 if (buf_len(diffbuf) > 0) {
2246 error = buf_putc(diffbuf, '\0');
2249 *diff_html = strdup(buf_get(diffbuf));
2250 if (*diff_html == NULL)
2251 error = got_error_from_errno("strdup");
2254 if (f && fclose(f) == -1 && error == NULL)
2255 error = got_error_from_errno("fclose");
2256 free(diff_line_html);
2267 static const struct got_error *
2268 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2270 const struct got_error *error = NULL;
2271 struct got_repository *repo;
2272 const char *gitconfig_owner;
2276 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2279 error = got_repo_open(&repo, dir, NULL);
2282 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2283 if (gitconfig_owner) {
2284 *owner = strdup(gitconfig_owner);
2286 error = got_error_from_errno("strdup");
2288 got_repo_close(repo);
2292 static const struct got_error *
2293 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2295 const struct got_error *error = NULL;
2297 char *d_file = NULL;
2303 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2304 return got_error_from_errno("asprintf");
2306 f = fopen(d_file, "r");
2308 if (errno != ENOENT && errno != EACCES)
2309 error = got_error_from_errno2("fopen", d_file);
2313 if (fseek(f, 0, SEEK_END) == -1) {
2314 error = got_ferror(f, GOT_ERR_IO);
2319 error = got_ferror(f, GOT_ERR_IO);
2322 if (fseek(f, 0, SEEK_SET) == -1) {
2323 error = got_ferror(f, GOT_ERR_IO);
2327 *url = calloc(len + 1, sizeof(**url));
2329 error = got_error_from_errno("calloc");
2333 n = fread(*url, 1, len, f);
2334 if (n == 0 && ferror(f))
2335 error = got_ferror(f, GOT_ERR_IO);
2337 if (f && fclose(f) == -1 && error == NULL)
2338 error = got_error_from_errno("fclose");
2343 static const struct got_error *
2344 gw_get_repo_tags(char **tag_html, struct gw_trans *gw_trans,
2345 struct gw_header *header, int limit, int tag_type)
2347 const struct got_error *error = NULL;
2348 struct got_repository *repo = NULL;
2349 struct got_reflist_head refs;
2350 struct got_reflist_entry *re;
2351 char *tag_row = NULL, *tags_navs_disp = NULL;
2352 char *age = NULL, *age_html = NULL, *newline;
2353 char *escaped_tagger = NULL, *escaped_tag_commit = NULL;
2354 char *id_str = NULL, *refstr = NULL;
2355 char *tag_commit0 = NULL;
2356 struct buf *diffbuf = NULL;
2358 struct got_tag_object *tag = NULL;
2362 SIMPLEQ_INIT(&refs);
2364 error = buf_alloc(&diffbuf, 0);
2368 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2372 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2376 SIMPLEQ_FOREACH(re, &refs, entry) {
2377 const char *refname;
2379 const char *tag_commit;
2381 struct got_object_id *id;
2383 refname = got_ref_get_name(re->ref);
2384 if (strncmp(refname, "refs/tags/", 10) != 0)
2387 refstr = got_ref_to_str(re->ref);
2388 if (refstr == NULL) {
2389 error = got_error_from_errno("got_ref_to_str");
2393 error = got_ref_resolve(&id, repo, re->ref);
2396 error = got_object_open_as_tag(&tag, repo, id);
2401 tagger = got_object_tag_get_tagger(tag);
2402 tagger_time = got_object_tag_get_tagger_time(tag);
2404 error = got_object_id_str(&id_str,
2405 got_object_tag_get_object_id(tag));
2409 tag_commit0 = strdup(got_object_tag_get_message(tag));
2410 if (tag_commit0 == NULL) {
2411 error = got_error_from_errno("strdup");
2415 tag_commit = tag_commit0;
2416 while (*tag_commit == '\n')
2421 newline = strchr(tag_commit, '\n');
2425 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2429 if (asprintf(&tags_navs_disp, tags_navs,
2430 gw_trans->repo_name, id_str, gw_trans->repo_name,
2431 id_str, gw_trans->repo_name, id_str,
2432 gw_trans->repo_name, id_str) == -1) {
2433 error = got_error_from_errno("asprintf");
2437 if (asprintf(&tag_row, tags_row, age ? age : "",
2438 refname, tag_commit, tags_navs_disp) == -1) {
2439 error = got_error_from_errno("asprintf");
2443 free(tags_navs_disp);
2446 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2449 error = gw_html_escape(&escaped_tagger, tagger);
2452 error = gw_html_escape(&escaped_tag_commit, tag_commit);
2455 if (asprintf(&tag_row, tag_info, age ? age : "",
2456 escaped_tagger, escaped_tag_commit) == -1) {
2457 error = got_error_from_errno("asprintf");
2465 error = buf_puts(&newsize, diffbuf, tag_row);
2469 if (limit && --limit == 0)
2472 got_object_tag_close(tag);
2482 free(escaped_tagger);
2483 escaped_tagger = NULL;
2484 free(escaped_tag_commit);
2485 escaped_tag_commit = NULL;
2492 if (buf_len(diffbuf) > 0) {
2493 error = buf_putc(diffbuf, '\0');
2494 *tag_html = strdup(buf_get(diffbuf));
2495 if (*tag_html == NULL)
2496 error = got_error_from_errno("strdup");
2500 got_object_tag_close(tag);
2505 free(escaped_tagger);
2506 free(escaped_tag_commit);
2510 got_ref_list_free(&refs);
2512 got_repo_close(repo);
2517 gw_free_headers(struct gw_header *header)
2521 if (header->commit != NULL)
2522 got_object_commit_close(header->commit);
2524 got_repo_close(header->repo);
2525 free(header->refs_str);
2526 free(header->commit_id);
2527 free(header->parent_id);
2528 free(header->tree_id);
2529 free(header->commit_msg);
2532 static struct gw_header *
2535 struct gw_header *header;
2537 header = malloc(sizeof(*header));
2541 header->repo = NULL;
2542 header->commit = NULL;
2544 header->path = NULL;
2545 SIMPLEQ_INIT(&header->refs);
2550 static const struct got_error *
2551 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2554 const struct got_error *error = NULL;
2555 struct got_commit_graph *graph = NULL;
2557 error = got_commit_graph_open(&graph, header->path, 0);
2561 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2567 error = got_commit_graph_iter_next(&header->id, graph,
2568 header->repo, NULL, NULL);
2570 if (error->code == GOT_ERR_ITER_COMPLETED)
2574 if (header->id == NULL)
2577 error = got_object_open_as_commit(&header->commit, header->repo,
2582 error = gw_get_commit(gw_trans, header);
2584 struct gw_header *n_header = NULL;
2585 if ((n_header = gw_init_header()) == NULL) {
2586 error = got_error_from_errno("malloc");
2590 n_header->refs_str = strdup(header->refs_str);
2591 n_header->commit_id = strdup(header->commit_id);
2592 n_header->parent_id = strdup(header->parent_id);
2593 n_header->tree_id = strdup(header->tree_id);
2594 n_header->author = strdup(header->author);
2595 n_header->committer = strdup(header->committer);
2596 n_header->commit_msg = strdup(header->commit_msg);
2597 n_header->committer_time = header->committer_time;
2598 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2601 if (error || (limit && --limit == 0))
2606 got_commit_graph_close(graph);
2610 static const struct got_error *
2611 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2613 const struct got_error *error = NULL;
2614 struct got_reflist_entry *re;
2615 struct got_object_id *id2 = NULL;
2616 struct got_object_qid *parent_id;
2617 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2620 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2623 struct got_tag_object *tag = NULL;
2626 name = got_ref_get_name(re->ref);
2627 if (strcmp(name, GOT_REF_HEAD) == 0)
2629 if (strncmp(name, "refs/", 5) == 0)
2631 if (strncmp(name, "got/", 4) == 0)
2633 if (strncmp(name, "heads/", 6) == 0)
2635 if (strncmp(name, "remotes/", 8) == 0)
2637 if (strncmp(name, "tags/", 5) == 0) {
2638 error = got_object_open_as_tag(&tag, header->repo,
2641 if (error->code != GOT_ERR_OBJ_TYPE)
2644 * Ref points at something other
2651 cmp = got_object_id_cmp(tag ?
2652 got_object_tag_get_object_id(tag) : re->id, header->id);
2654 got_object_tag_close(tag);
2658 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2659 s ? ", " : "", name) == -1) {
2660 error = got_error_from_errno("asprintf");
2664 header->refs_str = strdup(refs_str);
2668 if (refs_str == NULL)
2669 header->refs_str = strdup("");
2672 error = got_object_id_str(&header->commit_id, header->id);
2676 error = got_object_id_str(&header->tree_id,
2677 got_object_commit_get_tree_id(header->commit));
2681 if (gw_trans->action == GW_DIFF) {
2682 parent_id = SIMPLEQ_FIRST(
2683 got_object_commit_get_parent_ids(header->commit));
2684 if (parent_id != NULL) {
2685 id2 = got_object_id_dup(parent_id->id);
2687 error = got_object_id_str(&header->parent_id, id2);
2692 header->parent_id = strdup("/dev/null");
2694 header->parent_id = strdup("");
2696 header->committer_time =
2697 got_object_commit_get_committer_time(header->commit);
2700 got_object_commit_get_author(header->commit);
2702 got_object_commit_get_committer(header->commit);
2706 /* XXX Doesn't the log message require escaping? */
2707 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2711 commit_msg = commit_msg0;
2712 while (*commit_msg == '\n')
2715 header->commit_msg = strdup(commit_msg);
2720 static const struct got_error *
2721 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2723 const struct got_error *error = NULL;
2724 char *in_repo_path = NULL;
2726 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2730 if (gw_trans->commit == NULL) {
2731 struct got_reference *head_ref;
2732 error = got_ref_open(&head_ref, header->repo,
2733 gw_trans->headref, 0);
2737 error = got_ref_resolve(&header->id, header->repo, head_ref);
2738 got_ref_close(head_ref);
2742 error = got_object_open_as_commit(&header->commit,
2743 header->repo, header->id);
2745 struct got_reference *ref;
2746 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2747 if (error == NULL) {
2749 error = got_ref_resolve(&header->id, header->repo, ref);
2753 error = got_object_get_type(&obj_type, header->repo,
2757 if (obj_type == GOT_OBJ_TYPE_TAG) {
2758 struct got_tag_object *tag;
2759 error = got_object_open_as_tag(&tag,
2760 header->repo, header->id);
2763 if (got_object_tag_get_object_type(tag) !=
2764 GOT_OBJ_TYPE_COMMIT) {
2765 got_object_tag_close(tag);
2766 error = got_error(GOT_ERR_OBJ_TYPE);
2770 header->id = got_object_id_dup(
2771 got_object_tag_get_object_id(tag));
2772 if (header->id == NULL)
2773 error = got_error_from_errno(
2774 "got_object_id_dup");
2775 got_object_tag_close(tag);
2778 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2779 error = got_error(GOT_ERR_OBJ_TYPE);
2782 error = got_object_open_as_commit(&header->commit,
2783 header->repo, header->id);
2787 if (header->commit == NULL) {
2788 error = got_repo_match_object_id_prefix(&header->id,
2789 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2794 error = got_repo_match_object_id_prefix(&header->id,
2795 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2799 error = got_repo_map_path(&in_repo_path, header->repo,
2800 gw_trans->repo_path, 1);
2805 header->path = strdup(in_repo_path);
2809 error = got_ref_list(&header->refs, header->repo, NULL,
2810 got_ref_cmp_by_name, NULL);
2814 error = gw_get_commits(gw_trans, header, limit);
2822 char datebuf[11]; /* YYYY-MM-DD + NUL */
2825 struct gw_blame_cb_args {
2826 struct blame_line *lines;
2830 off_t *line_offsets;
2832 struct got_repository *repo;
2833 struct gw_trans *gw_trans;
2834 struct buf *blamebuf;
2837 static const struct got_error *
2838 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2840 const struct got_error *err = NULL;
2841 struct gw_blame_cb_args *a = arg;
2842 struct blame_line *bline;
2844 size_t linesize = 0, newsize;
2845 struct got_commit_object *commit = NULL;
2848 time_t committer_time;
2850 if (nlines != a->nlines ||
2851 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2852 return got_error(GOT_ERR_RANGE);
2855 return NULL; /* no change in this commit */
2857 /* Annotate this line. */
2858 bline = &a->lines[lineno - 1];
2859 if (bline->annotated)
2861 err = got_object_id_str(&bline->id_str, id);
2865 err = got_object_open_as_commit(&commit, a->repo, id);
2869 bline->committer = strdup(got_object_commit_get_committer(commit));
2870 if (bline->committer == NULL) {
2871 err = got_error_from_errno("strdup");
2875 committer_time = got_object_commit_get_committer_time(commit);
2876 if (localtime_r(&committer_time, &tm) == NULL)
2877 return got_error_from_errno("localtime_r");
2878 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2879 &tm) >= sizeof(bline->datebuf)) {
2880 err = got_error(GOT_ERR_NO_SPACE);
2883 bline->annotated = 1;
2885 /* Print lines annotated so far. */
2886 bline = &a->lines[a->lineno_cur - 1];
2887 if (!bline->annotated)
2890 offset = a->line_offsets[a->lineno_cur - 1];
2891 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2892 err = got_error_from_errno("fseeko");
2896 while (bline->annotated) {
2897 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2898 *line_escape = NULL;
2901 if (getline(&line, &linesize, a->f) == -1) {
2903 err = got_error_from_errno("getline");
2907 committer = bline->committer;
2908 smallerthan = strchr(committer, '<');
2909 if (smallerthan && smallerthan[1] != '\0')
2910 committer = smallerthan + 1;
2911 at = strchr(committer, '@');
2914 len = strlen(committer);
2916 committer[8] = '\0';
2918 nl = strchr(line, '\n');
2922 err = gw_html_escape(&line_escape, line);
2926 if (a->gw_trans->repo_folder == NULL)
2927 a->gw_trans->repo_folder = strdup("");
2928 if (a->gw_trans->repo_folder == NULL)
2930 asprintf(&blame_row, blame_line, a->nlines_prec, a->lineno_cur,
2931 a->gw_trans->repo_name, bline->id_str,
2932 a->gw_trans->repo_file, a->gw_trans->repo_folder,
2933 bline->id_str, bline->datebuf, committer, line_escape);
2935 err = buf_puts(&newsize, a->blamebuf, blame_row);
2939 bline = &a->lines[a->lineno_cur - 1];
2946 got_object_commit_close(commit);
2951 static const struct got_error *
2952 gw_get_file_blame_blob(char **blame_html, struct gw_trans *gw_trans)
2954 const struct got_error *error = NULL;
2955 struct got_repository *repo = NULL;
2956 struct got_object_id *obj_id = NULL;
2957 struct got_object_id *commit_id = NULL;
2958 struct got_blob_object *blob = NULL;
2959 char *path = NULL, *in_repo_path = NULL;
2960 struct gw_blame_cb_args bca;
2966 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2970 if (asprintf(&path, "%s%s%s",
2971 gw_trans->repo_folder ? gw_trans->repo_folder : "",
2972 gw_trans->repo_folder ? "/" : "",
2973 gw_trans->repo_file) == -1) {
2974 error = got_error_from_errno("asprintf");
2978 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2982 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2983 GOT_OBJ_TYPE_COMMIT, 1, repo);
2987 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2991 if (obj_id == NULL) {
2992 error = got_error(GOT_ERR_NO_OBJ);
2996 error = got_object_get_type(&obj_type, repo, obj_id);
3000 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3001 error = got_error(GOT_ERR_OBJ_TYPE);
3005 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3009 error = buf_alloc(&bca.blamebuf, 0);
3013 bca.f = got_opentemp();
3014 if (bca.f == NULL) {
3015 error = got_error_from_errno("got_opentemp");
3018 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3019 &bca.line_offsets, bca.f, blob);
3020 if (error || bca.nlines == 0)
3023 /* Don't include \n at EOF in the blame line count. */
3024 if (bca.line_offsets[bca.nlines - 1] == filesize)
3027 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3028 if (bca.lines == NULL) {
3029 error = got_error_from_errno("calloc");
3033 bca.nlines_prec = 0;
3040 bca.gw_trans = gw_trans;
3042 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3046 if (buf_len(bca.blamebuf) > 0) {
3047 error = buf_putc(bca.blamebuf, '\0');
3050 *blame_html = strdup(buf_get(bca.blamebuf));
3051 if (*blame_html == NULL) {
3052 error = got_error_from_errno("strdup");
3057 free(bca.line_offsets);
3064 for (i = 0; i < bca.nlines; i++) {
3065 struct blame_line *bline = &bca.lines[i];
3066 free(bline->id_str);
3067 free(bline->committer);
3070 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3071 error = got_error_from_errno("fclose");
3073 got_object_blob_close(blob);
3075 got_repo_close(repo);
3079 static const struct got_error *
3080 gw_get_file_read_blob(char **blobstr, size_t *filesize, struct gw_trans *gw_trans)
3082 const struct got_error *error = NULL;
3083 struct got_repository *repo = NULL;
3084 struct got_object_id *obj_id = NULL;
3085 struct got_object_id *commit_id = NULL;
3086 struct got_blob_object *blob = NULL;
3087 char *path = NULL, *in_repo_path = NULL;
3095 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3099 if (asprintf(&path, "%s%s%s",
3100 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3101 gw_trans->repo_folder ? "/" : "",
3102 gw_trans->repo_file) == -1) {
3103 error = got_error_from_errno("asprintf");
3107 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3111 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3112 GOT_OBJ_TYPE_COMMIT, 1, repo);
3116 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3120 if (obj_id == NULL) {
3121 error = got_error(GOT_ERR_NO_OBJ);
3125 error = got_object_get_type(&obj_type, repo, obj_id);
3129 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3130 error = got_error(GOT_ERR_OBJ_TYPE);
3134 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3140 error = got_error_from_errno("got_opentemp");
3143 error = got_object_blob_dump_to_file(filesize, NULL, NULL, f, blob);
3147 /* XXX This will fail on large files... */
3148 *blobstr = calloc(*filesize + 1, sizeof(**blobstr));
3149 if (*blobstr == NULL) {
3150 error = got_error_from_errno("calloc");
3154 n = fread(*blobstr, 1, *filesize, f);
3157 error = got_ferror(f, GOT_ERR_IO);
3166 got_object_blob_close(blob);
3168 got_repo_close(repo);
3169 if (f != NULL && fclose(f) == -1 && error == NULL)
3170 error = got_error_from_errno("fclose");
3179 static const struct got_error *
3180 gw_get_repo_tree(char **tree_html, struct gw_trans *gw_trans)
3182 const struct got_error *error = NULL;
3183 struct got_repository *repo = NULL;
3184 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3185 struct got_tree_object *tree = NULL;
3186 struct buf *diffbuf = NULL;
3188 char *path = NULL, *in_repo_path = NULL, *tree_row = NULL;
3189 char *id_str = NULL;
3190 char *build_folder = NULL;
3191 char *url_html = NULL;
3192 const char *class = NULL;
3193 int nentries, i, class_flip = 0;
3197 error = buf_alloc(&diffbuf, 0);
3201 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3205 if (gw_trans->repo_folder != NULL)
3206 path = strdup(gw_trans->repo_folder);
3208 error = got_repo_map_path(&in_repo_path, repo,
3209 gw_trans->repo_path, 1);
3213 path = in_repo_path;
3216 if (gw_trans->commit == NULL) {
3217 struct got_reference *head_ref;
3218 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3221 error = got_ref_resolve(&commit_id, repo, head_ref);
3224 got_ref_close(head_ref);
3227 error = got_repo_match_object_id(&commit_id, NULL,
3228 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3233 error = got_object_id_str(&gw_trans->commit, commit_id);
3237 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3241 error = got_object_open_as_tree(&tree, repo, tree_id);
3245 nentries = got_object_tree_get_nentries(tree);
3246 for (i = 0; i < nentries; i++) {
3247 struct got_tree_entry *te;
3248 const char *modestr = "";
3251 te = got_object_tree_get_entry(tree, i);
3253 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3257 mode = got_tree_entry_get_mode(te);
3258 if (got_object_tree_entry_is_submodule(te))
3260 else if (S_ISLNK(mode))
3262 else if (S_ISDIR(mode))
3264 else if (mode & S_IXUSR)
3267 if (class_flip == 0) {
3268 class = "back_lightgray";
3271 class = "back_white";
3275 if (S_ISDIR(mode)) {
3276 if (asprintf(&build_folder, "%s%s%s",
3277 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3278 gw_trans->repo_folder ? "/" : "",
3279 got_tree_entry_get_name(te)) == -1) {
3280 error = got_error_from_errno(
3285 if (asprintf(&url_html, folder_html,
3286 gw_trans->repo_name, gw_trans->action_name,
3287 gw_trans->commit, build_folder,
3288 got_tree_entry_get_name(te), modestr) == -1) {
3289 error = got_error_from_errno("asprintf");
3292 if (asprintf(&tree_row, tree_line, class, url_html,
3294 error = got_error_from_errno("asprintf");
3298 if (asprintf(&url_html, file_html, gw_trans->repo_name,
3299 "blob", gw_trans->commit,
3300 got_tree_entry_get_name(te),
3301 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3302 got_tree_entry_get_name(te), modestr) == -1) {
3303 error = got_error_from_errno("asprintf");
3307 if (asprintf(&tree_row, tree_line_with_navs, class,
3308 url_html, class, gw_trans->repo_name, "blob",
3309 gw_trans->commit, got_tree_entry_get_name(te),
3310 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3311 "blob", gw_trans->repo_name,
3312 "blame", gw_trans->commit,
3313 got_tree_entry_get_name(te),
3314 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3316 error = got_error_from_errno("asprintf");
3321 error = buf_puts(&newsize, diffbuf, tree_row);
3332 build_folder = NULL;
3335 if (buf_len(diffbuf) > 0) {
3336 error = buf_putc(diffbuf, '\0');
3339 *tree_html = strdup(buf_get(diffbuf));
3340 if (*tree_html == NULL) {
3341 error = got_error_from_errno("strdup");
3347 got_object_tree_close(tree);
3349 got_repo_close(repo);
3361 static const struct got_error *
3362 gw_get_repo_heads(char **head_html, struct gw_trans *gw_trans)
3364 const struct got_error *error = NULL;
3365 struct got_repository *repo = NULL;
3366 struct got_reflist_head refs;
3367 struct got_reflist_entry *re;
3368 char *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
3369 struct buf *diffbuf = NULL;
3374 SIMPLEQ_INIT(&refs);
3376 error = buf_alloc(&diffbuf, 0);
3380 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3384 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3389 SIMPLEQ_FOREACH(re, &refs, entry) {
3392 refname = strdup(got_ref_get_name(re->ref));
3393 if (refname == NULL) {
3394 error = got_error_from_errno("got_ref_to_str");
3398 if (strncmp(refname, "refs/heads/", 11) != 0) {
3403 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3408 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
3409 refname, gw_trans->repo_name, refname,
3410 gw_trans->repo_name, refname, gw_trans->repo_name,
3412 error = got_error_from_errno("asprintf");
3416 if (strncmp(refname, "refs/heads/", 11) == 0)
3419 if (asprintf(&head_row, heads_row, age, refname,
3420 head_navs_disp) == -1) {
3421 error = got_error_from_errno("asprintf");
3425 error = buf_puts(&newsize, diffbuf, head_row);
3427 free(head_navs_disp);
3431 if (buf_len(diffbuf) > 0) {
3432 error = buf_putc(diffbuf, '\0');
3433 *head_html = strdup(buf_get(diffbuf));
3434 if (*head_html == NULL)
3435 error = got_error_from_errno("strdup");
3439 got_ref_list_free(&refs);
3441 got_repo_close(repo);
3446 gw_get_site_link(struct gw_trans *gw_trans)
3448 char *link = NULL, *repo = NULL, *action = NULL;
3450 if (gw_trans->repo_name != NULL &&
3451 asprintf(&repo, " / <a href='?path=%s&action=summary'>%s</a>",
3452 gw_trans->repo_name, gw_trans->repo_name) == -1)
3455 if (gw_trans->action_name != NULL &&
3456 asprintf(&action, " / %s", gw_trans->action_name) == -1) {
3461 if (asprintf(&link, site_link, GOTWEB,
3462 gw_trans->gw_conf->got_site_link,
3463 repo ? repo : "", action ? action : "") == -1) {
3474 static const struct got_error *
3475 gw_colordiff_line(char **colorized_line, char *buf)
3477 const struct got_error *error = NULL;
3478 char *div_diff_line_div = NULL, *color = NULL;
3479 struct buf *diffbuf = NULL;
3482 *colorized_line = NULL;
3484 error = buf_alloc(&diffbuf, 0);
3488 if (strncmp(buf, "-", 1) == 0)
3489 color = "diff_minus";
3490 else if (strncmp(buf, "+", 1) == 0)
3491 color = "diff_plus";
3492 else if (strncmp(buf, "@@", 2) == 0)
3493 color = "diff_chunk_header";
3494 else if (strncmp(buf, "@@", 2) == 0)
3495 color = "diff_chunk_header";
3496 else if (strncmp(buf, "commit +", 8) == 0)
3497 color = "diff_meta";
3498 else if (strncmp(buf, "commit -", 8) == 0)
3499 color = "diff_meta";
3500 else if (strncmp(buf, "blob +", 6) == 0)
3501 color = "diff_meta";
3502 else if (strncmp(buf, "blob -", 6) == 0)
3503 color = "diff_meta";
3504 else if (strncmp(buf, "file +", 6) == 0)
3505 color = "diff_meta";
3506 else if (strncmp(buf, "file -", 6) == 0)
3507 color = "diff_meta";
3508 else if (strncmp(buf, "from:", 5) == 0)
3509 color = "diff_author";
3510 else if (strncmp(buf, "via:", 4) == 0)
3511 color = "diff_author";
3512 else if (strncmp(buf, "date:", 5) == 0)
3513 color = "diff_date";
3515 if (asprintf(&div_diff_line_div, div_diff_line, color ? color : "")
3517 error = got_error_from_errno("asprintf");
3521 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
3525 error = buf_puts(&newsize, diffbuf, buf);
3529 if (buf_len(diffbuf) > 0) {
3530 error = buf_putc(diffbuf, '\0');
3531 *colorized_line = strdup(buf_get(diffbuf));
3532 if (*colorized_line == NULL)
3533 error = got_error_from_errno("strdup");
3537 free(div_diff_line_div);
3542 * XXX This function should not exist.
3543 * We should let khtml_puts(3) handle HTML escaping.
3545 static const struct got_error *
3546 gw_html_escape(char **escaped_html, const char *orig_html)
3548 const struct got_error *error = NULL;
3549 struct escape_pair {
3560 size_t orig_len, len;
3563 orig_len = strlen(orig_html);
3565 for (i = 0; i < orig_len; i++) {
3566 for (j = 0; j < nitems(esc); j++) {
3567 if (orig_html[i] != esc[j].c)
3569 len += strlen(esc[j].s) - 1 /* escaped char */;
3573 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
3574 if (*escaped_html == NULL)
3575 return got_error_from_errno("calloc");
3578 for (i = 0; i < orig_len; i++) {
3580 for (j = 0; j < nitems(esc); j++) {
3581 if (orig_html[i] != esc[j].c)
3584 if (strlcat(*escaped_html, esc[j].s, len + 1)
3586 error = got_error(GOT_ERR_NO_SPACE);
3589 x += strlen(esc[j].s);
3594 (*escaped_html)[x] = orig_html[i];
3600 free(*escaped_html);
3601 *escaped_html = NULL;
3603 (*escaped_html)[x] = '\0';
3609 main(int argc, char *argv[])
3611 const struct got_error *error = NULL;
3612 struct gw_trans *gw_trans;
3613 struct gw_dir *dir = NULL, *tdir;
3614 const char *page = "index";
3618 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3621 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3624 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3627 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3630 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3631 if (kerr != KCGI_OK) {
3632 error = gw_kcgi_error(kerr);
3636 if ((gw_trans->gw_conf =
3637 malloc(sizeof(struct gotweb_conf))) == NULL) {
3639 error = got_error_from_errno("malloc");
3643 TAILQ_INIT(&gw_trans->gw_dirs);
3644 TAILQ_INIT(&gw_trans->gw_headers);
3647 gw_trans->repos_total = 0;
3648 gw_trans->repo_path = NULL;
3649 gw_trans->commit = NULL;
3650 gw_trans->headref = strdup(GOT_REF_HEAD);
3651 gw_trans->mime = KMIME_TEXT_HTML;
3652 gw_trans->gw_tmpl->key = gw_templs;
3653 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3654 gw_trans->gw_tmpl->arg = gw_trans;
3655 gw_trans->gw_tmpl->cb = gw_template;
3656 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3660 error = gw_parse_querystring(gw_trans);
3664 if (gw_trans->action == GW_BLOB)
3665 error = gw_blob(gw_trans);
3667 error = gw_display_index(gw_trans);
3670 gw_trans->mime = KMIME_TEXT_PLAIN;
3671 gw_trans->action = GW_ERR;
3672 gw_display_error(gw_trans, error);
3675 free(gw_trans->gw_conf->got_repos_path);
3676 free(gw_trans->gw_conf->got_site_name);
3677 free(gw_trans->gw_conf->got_site_owner);
3678 free(gw_trans->gw_conf->got_site_link);
3679 free(gw_trans->gw_conf->got_logo);
3680 free(gw_trans->gw_conf->got_logo_url);
3681 free(gw_trans->gw_conf);
3682 free(gw_trans->commit);
3683 free(gw_trans->repo_path);
3684 free(gw_trans->repo_name);
3685 free(gw_trans->repo_file);
3686 free(gw_trans->action_name);
3687 free(gw_trans->headref);
3689 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3691 free(dir->description);
3700 khttp_free(gw_trans->gw_req);