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>
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct gw_dir *gw_dir;
59 struct gotweb_conf *gw_conf;
60 struct ktemplate *gw_tmpl;
61 struct khtmlreq *gw_html_req;
63 const struct got_error *error;
64 const char *repo_name;
67 const char *repo_file;
72 unsigned int repos_total;
77 TAILQ_ENTRY(gw_header) entry;
78 struct got_repository *repo;
79 struct got_reflist_head refs;
80 struct got_commit_object *commit;
81 struct got_object_id *id;
85 char *commit_id; /* id_str1 */
86 char *parent_id; /* id_str2 */
89 const char *committer;
91 time_t committer_time;
95 TAILQ_ENTRY(gw_dir) entry;
136 static const char *const gw_templs[TEMPL__MAX] = {
146 static const struct kvalid gw_keys[KEY__ZMAX] = {
147 { kvalid_stringne, "action" },
148 { kvalid_stringne, "commit" },
149 { kvalid_stringne, "file" },
150 { kvalid_stringne, "folder" },
151 { kvalid_stringne, "headref" },
152 { kvalid_int, "page" },
153 { kvalid_stringne, "path" },
156 static struct gw_header *gw_init_header(void);
158 static void gw_free_headers(struct gw_header *);
160 static int gw_template(size_t, void *);
162 static const struct got_error *gw_error(struct gw_trans *);
163 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
164 static const struct got_error *gw_get_repo_description(char **,
165 struct gw_trans *, char *);
166 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
168 static const struct got_error *gw_get_time_str(char **, time_t, int);
169 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
170 char *, char *, int);
171 static const struct got_error *gw_output_file_blame(struct gw_trans *);
172 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
173 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
174 static const struct got_error *gw_output_diff(struct gw_trans *,
176 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
177 struct gw_header *, int, int);
178 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
179 static const struct got_error *gw_output_site_link(struct gw_trans *);
180 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
182 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
184 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
186 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
188 static const struct got_error *gw_gen_author_header(struct gw_trans *,
190 static const struct got_error *gw_gen_age_header(struct gw_trans *,
192 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
194 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
196 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
197 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
199 static const struct got_error *gw_display_index(struct gw_trans *);
200 static const struct got_error *gw_get_header(struct gw_trans *,
201 struct gw_header *, int);
202 static const struct got_error *gw_get_commits(struct gw_trans *,
203 struct gw_header *, int);
204 static const struct got_error *gw_get_commit(struct gw_trans *,
206 static const struct got_error *gw_apply_unveil(const char *);
207 static const struct got_error *gw_blame_cb(void *, int, int,
208 struct got_object_id *);
209 static const struct got_error *gw_load_got_paths(struct gw_trans *);
210 static const struct got_error *gw_load_got_path(struct gw_trans *,
212 static const struct got_error *gw_parse_querystring(struct gw_trans *);
213 static const struct got_error *gw_blame(struct gw_trans *);
214 static const struct got_error *gw_blob(struct gw_trans *);
215 static const struct got_error *gw_diff(struct gw_trans *);
216 static const struct got_error *gw_index(struct gw_trans *);
217 static const struct got_error *gw_commits(struct gw_trans *);
218 static const struct got_error *gw_briefs(struct gw_trans *);
219 static const struct got_error *gw_summary(struct gw_trans *);
220 static const struct got_error *gw_tree(struct gw_trans *);
221 static const struct got_error *gw_tag(struct gw_trans *);
223 struct gw_query_action {
224 unsigned int func_id;
225 const char *func_name;
226 const struct got_error *(*func_main)(struct gw_trans *);
230 enum gw_query_actions {
243 static struct gw_query_action gw_query_funcs[] = {
244 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
245 { GW_BLOB, "blob", NULL, NULL },
246 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
247 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
248 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
249 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
250 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
251 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
252 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
253 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
257 gw_get_action_name(struct gw_trans *gw_trans)
259 return gw_query_funcs[gw_trans->action].func_name;
262 static const struct got_error *
263 gw_kcgi_error(enum kcgi_err kerr)
268 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
269 return got_error(GOT_ERR_CANCELLED);
271 if (kerr == KCGI_ENOMEM)
272 return got_error_set_errno(ENOMEM,
273 kcgi_strerror(kerr));
275 if (kerr == KCGI_ENFILE)
276 return got_error_set_errno(ENFILE,
277 kcgi_strerror(kerr));
279 if (kerr == KCGI_EAGAIN)
280 return got_error_set_errno(EAGAIN,
281 kcgi_strerror(kerr));
283 if (kerr == KCGI_FORM)
284 return got_error_msg(GOT_ERR_IO,
285 kcgi_strerror(kerr));
287 return got_error_from_errno(kcgi_strerror(kerr));
290 static const struct got_error *
291 gw_apply_unveil(const char *repo_path)
293 const struct got_error *err;
295 if (repo_path && unveil(repo_path, "r") != 0)
296 return got_error_from_errno2("unveil", repo_path);
298 if (unveil("/tmp", "rwc") != 0)
299 return got_error_from_errno2("unveil", "/tmp");
301 err = got_privsep_unveil_exec_helpers();
305 if (unveil(NULL, NULL) != 0)
306 return got_error_from_errno("unveil");
312 isbinary(const uint8_t *buf, size_t n)
316 for (i = 0; i < n; i++)
322 static const struct got_error *
323 gw_blame(struct gw_trans *gw_trans)
325 const struct got_error *error = NULL;
326 struct gw_header *header = NULL;
330 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
332 return got_error_from_errno("pledge");
334 if ((header = gw_init_header()) == NULL)
335 return got_error_from_errno("malloc");
337 error = gw_apply_unveil(gw_trans->gw_dir->path);
341 error = gw_get_header(gw_trans, header, 1);
345 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
346 "blame_header_wrapper", KATTR__MAX);
349 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
350 "blame_header", KATTR__MAX);
353 error = gw_get_time_str(&age, header->committer_time,
357 error = gw_gen_age_header(gw_trans, age ?age : "");
360 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
363 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
367 "dotted_line", KATTR__MAX);
370 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
375 "blame", KATTR__MAX);
378 error = gw_output_file_blame(gw_trans);
381 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
383 got_ref_list_free(&header->refs);
384 gw_free_headers(header);
385 if (error == NULL && kerr != KCGI_OK)
386 error = gw_kcgi_error(kerr);
390 static const struct got_error *
391 gw_blob(struct gw_trans *gw_trans)
393 const struct got_error *error = NULL;
394 struct gw_header *header = NULL;
396 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
398 return got_error_from_errno("pledge");
400 if ((header = gw_init_header()) == NULL)
401 return got_error_from_errno("malloc");
403 error = gw_apply_unveil(gw_trans->gw_dir->path);
407 error = gw_get_header(gw_trans, header, 1);
411 error = gw_output_blob_buf(gw_trans);
413 got_ref_list_free(&header->refs);
414 gw_free_headers(header);
418 static const struct got_error *
419 gw_diff(struct gw_trans *gw_trans)
421 const struct got_error *error = NULL;
422 struct gw_header *header = NULL;
424 enum kcgi_err kerr = KCGI_OK;
426 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
428 return got_error_from_errno("pledge");
430 if ((header = gw_init_header()) == NULL)
431 return got_error_from_errno("malloc");
433 error = gw_apply_unveil(gw_trans->gw_dir->path);
437 error = gw_get_header(gw_trans, header, 1);
441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
442 "diff_header_wrapper", KATTR__MAX);
445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
446 "diff_header", KATTR__MAX);
449 error = gw_gen_diff_header(gw_trans, header->parent_id,
453 error = gw_gen_commit_header(gw_trans, header->commit_id,
457 error = gw_gen_tree_header(gw_trans, header->tree_id);
460 error = gw_gen_author_header(gw_trans, header->author);
463 error = gw_gen_committer_header(gw_trans, header->author);
466 error = gw_get_time_str(&age, header->committer_time,
470 error = gw_gen_age_header(gw_trans, age ?age : "");
473 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
476 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
479 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
480 "dotted_line", KATTR__MAX);
483 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
487 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
491 error = gw_output_diff(gw_trans, header);
495 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
497 got_ref_list_free(&header->refs);
498 gw_free_headers(header);
500 if (error == NULL && kerr != KCGI_OK)
501 error = gw_kcgi_error(kerr);
505 static const struct got_error *
506 gw_index(struct gw_trans *gw_trans)
508 const struct got_error *error = NULL;
509 struct gw_dir *gw_dir = NULL;
510 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
511 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
512 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
515 if (pledge("stdio rpath proc exec sendfd unveil",
517 error = got_error_from_errno("pledge");
521 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
525 error = gw_load_got_paths(gw_trans);
529 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
530 "index_header", KATTR__MAX);
532 return gw_kcgi_error(kerr);
533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
534 "index_header_project", KATTR__MAX);
536 return gw_kcgi_error(kerr);
537 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
539 return gw_kcgi_error(kerr);
540 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
542 return gw_kcgi_error(kerr);
544 if (gw_trans->gw_conf->got_show_repo_description) {
545 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
546 "index_header_description", KATTR__MAX);
548 return gw_kcgi_error(kerr);
549 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
551 return gw_kcgi_error(kerr);
552 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
554 return gw_kcgi_error(kerr);
557 if (gw_trans->gw_conf->got_show_repo_owner) {
558 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
559 "index_header_owner", KATTR__MAX);
561 return gw_kcgi_error(kerr);
562 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
564 return gw_kcgi_error(kerr);
565 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
567 return gw_kcgi_error(kerr);
570 if (gw_trans->gw_conf->got_show_repo_age) {
571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
572 "index_header_age", KATTR__MAX);
574 return gw_kcgi_error(kerr);
575 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
577 return gw_kcgi_error(kerr);
578 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
580 return gw_kcgi_error(kerr);
583 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
585 return gw_kcgi_error(kerr);
587 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
588 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
589 "index_wrapper", KATTR__MAX);
591 return gw_kcgi_error(kerr);
592 kerr = khtml_puts(gw_trans->gw_html_req,
593 "No repositories found in ");
595 return gw_kcgi_error(kerr);
596 kerr = khtml_puts(gw_trans->gw_html_req,
597 gw_trans->gw_conf->got_repos_path);
599 return gw_kcgi_error(kerr);
600 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
602 return gw_kcgi_error(kerr);
603 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
604 "dotted_line", KATTR__MAX);
606 return gw_kcgi_error(kerr);
607 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
609 return gw_kcgi_error(kerr);
613 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
616 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
617 if (gw_trans->page > 0 && (gw_trans->page *
618 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
625 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
626 "index_wrapper", KATTR__MAX);
630 if (asprintf(&href_summary, "?path=%s&action=summary",
631 gw_dir->name) == -1) {
632 error = got_error_from_errno("asprintf");
635 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
636 "index_project", KATTR__MAX);
639 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
640 href_summary, KATTR__MAX);
643 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
646 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
649 if (gw_trans->gw_conf->got_show_repo_description) {
650 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
651 KATTR_ID, "index_project_description", KATTR__MAX);
654 kerr = khtml_puts(gw_trans->gw_html_req,
655 gw_dir->description ? gw_dir->description : "");
658 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
662 if (gw_trans->gw_conf->got_show_repo_owner) {
663 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
664 KATTR_ID, "index_project_owner", KATTR__MAX);
667 kerr = khtml_puts(gw_trans->gw_html_req,
668 gw_dir->owner ? gw_dir->owner : "");
671 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
675 if (gw_trans->gw_conf->got_show_repo_age) {
676 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
677 KATTR_ID, "index_project_age", KATTR__MAX);
680 kerr = khtml_puts(gw_trans->gw_html_req,
681 gw_dir->age ? gw_dir->age : "");
684 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
689 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
690 "navs_wrapper", KATTR__MAX);
693 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
698 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
699 href_summary, KATTR__MAX);
702 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
705 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
709 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
713 if (asprintf(&href_briefs, "?path=%s&action=briefs",
714 gw_dir->name) == -1) {
715 error = got_error_from_errno("asprintf");
718 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
719 href_briefs, KATTR__MAX);
722 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
725 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
727 error = gw_kcgi_error(kerr);
729 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
733 if (asprintf(&href_commits, "?path=%s&action=commits",
734 gw_dir->name) == -1) {
735 error = got_error_from_errno("asprintf");
738 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
739 href_commits, KATTR__MAX);
742 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
745 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
749 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
753 if (asprintf(&href_tree, "?path=%s&action=tree",
754 gw_dir->name) == -1) {
755 error = got_error_from_errno("asprintf");
758 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
759 href_tree, KATTR__MAX);
762 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
766 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
769 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
770 "dotted_line", KATTR__MAX);
773 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
786 if (gw_trans->gw_conf->got_max_repos_display == 0)
789 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
790 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
791 KATTR_ID, "np_wrapper", KATTR__MAX);
794 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
795 KATTR_ID, "nav_prev", KATTR__MAX);
798 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
799 (gw_trans->page > 0) &&
800 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
801 prev_disp == gw_trans->repos_total)) {
802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
803 KATTR_ID, "np_wrapper", KATTR__MAX);
806 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
807 KATTR_ID, "nav_prev", KATTR__MAX);
812 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
813 (gw_trans->page > 0) &&
814 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
815 prev_disp == gw_trans->repos_total)) {
816 if (asprintf(&href_prev, "?page=%d",
817 gw_trans->page - 1) == -1) {
818 error = got_error_from_errno("asprintf");
821 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
822 KATTR_HREF, href_prev, KATTR__MAX);
825 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
828 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
833 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
835 return gw_kcgi_error(kerr);
837 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
838 next_disp == gw_trans->gw_conf->got_max_repos_display &&
839 dir_c != (gw_trans->page + 1) *
840 gw_trans->gw_conf->got_max_repos_display) {
841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
842 KATTR_ID, "nav_next", KATTR__MAX);
845 if (asprintf(&href_next, "?page=%d",
846 gw_trans->page + 1) == -1) {
847 error = got_error_from_errno("calloc");
850 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
851 KATTR_HREF, href_next, KATTR__MAX);
854 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
857 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
864 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
865 (gw_trans->page > 0) &&
866 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
867 prev_disp == gw_trans->repos_total)) {
868 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
881 if (error == NULL && kerr != KCGI_OK)
882 error = gw_kcgi_error(kerr);
886 static const struct got_error *
887 gw_commits(struct gw_trans *gw_trans)
889 const struct got_error *error = NULL;
890 struct gw_header *header = NULL, *n_header = NULL;
892 char *href_diff = NULL, *href_blob = NULL;
893 enum kcgi_err kerr = KCGI_OK;
895 if ((header = gw_init_header()) == NULL)
896 return got_error_from_errno("malloc");
898 if (pledge("stdio rpath proc exec sendfd unveil",
900 error = got_error_from_errno("pledge");
904 error = gw_apply_unveil(gw_trans->gw_dir->path);
908 error = gw_get_header(gw_trans, header,
909 gw_trans->gw_conf->got_max_commits_display);
913 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
914 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
915 "commits_line_wrapper", KATTR__MAX);
918 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
922 error = gw_gen_author_header(gw_trans, n_header->author);
925 error = gw_gen_committer_header(gw_trans, n_header->author);
928 error = gw_get_time_str(&age, n_header->committer_time,
932 error = gw_gen_age_header(gw_trans, age ?age : "");
935 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
939 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
940 "dotted_line", KATTR__MAX);
943 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
947 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
948 "commit", KATTR__MAX);
951 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
954 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
958 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
959 gw_trans->repo_name, n_header->commit_id) == -1) {
960 error = got_error_from_errno("asprintf");
963 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
964 KATTR_ID, "navs_wrapper", KATTR__MAX);
967 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
968 KATTR_ID, "navs", KATTR__MAX);
971 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
972 KATTR_HREF, href_diff, KATTR__MAX);
975 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
978 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
982 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
986 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
987 gw_trans->repo_name, n_header->commit_id) == -1) {
988 error = got_error_from_errno("asprintf");
991 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
992 KATTR_HREF, href_blob, KATTR__MAX);
995 khtml_puts(gw_trans->gw_html_req, "tree");
996 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
999 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1000 if (kerr != KCGI_OK)
1003 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1004 "solid_line", KATTR__MAX);
1005 if (kerr != KCGI_OK)
1007 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1008 if (kerr != KCGI_OK)
1015 got_ref_list_free(&header->refs);
1016 gw_free_headers(header);
1017 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1018 gw_free_headers(n_header);
1022 if (error == NULL && kerr != KCGI_OK)
1023 error = gw_kcgi_error(kerr);
1027 static const struct got_error *
1028 gw_briefs(struct gw_trans *gw_trans)
1030 const struct got_error *error = NULL;
1031 struct gw_header *header = NULL, *n_header = NULL;
1033 char *href_diff = NULL, *href_blob = NULL;
1034 char *newline, *smallerthan;
1035 enum kcgi_err kerr = KCGI_OK;
1037 if ((header = gw_init_header()) == NULL)
1038 return got_error_from_errno("malloc");
1040 if (pledge("stdio rpath proc exec sendfd unveil",
1042 error = got_error_from_errno("pledge");
1046 error = gw_apply_unveil(gw_trans->gw_dir->path);
1050 if (gw_trans->action == GW_SUMMARY)
1051 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1053 error = gw_get_header(gw_trans, header,
1054 gw_trans->gw_conf->got_max_commits_display);
1058 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1059 error = gw_get_time_str(&age, n_header->committer_time,
1064 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1065 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1066 if (kerr != KCGI_OK)
1069 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1070 KATTR_ID, "briefs_age", KATTR__MAX);
1071 if (kerr != KCGI_OK)
1073 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1074 if (kerr != KCGI_OK)
1076 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1077 if (kerr != KCGI_OK)
1080 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1081 KATTR_ID, "briefs_author", KATTR__MAX);
1082 if (kerr != KCGI_OK)
1084 smallerthan = strchr(n_header->author, '<');
1086 *smallerthan = '\0';
1087 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1088 if (kerr != KCGI_OK)
1090 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1091 if (kerr != KCGI_OK)
1094 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1095 gw_trans->repo_name, n_header->commit_id) == -1) {
1096 error = got_error_from_errno("asprintf");
1099 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1100 KATTR_ID, "briefs_log", KATTR__MAX);
1101 if (kerr != KCGI_OK)
1103 newline = strchr(n_header->commit_msg, '\n');
1106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1107 KATTR_HREF, href_diff, KATTR__MAX);
1108 if (kerr != KCGI_OK)
1110 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1111 if (kerr != KCGI_OK)
1113 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1114 if (kerr != KCGI_OK)
1117 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1118 KATTR_ID, "navs_wrapper", KATTR__MAX);
1119 if (kerr != KCGI_OK)
1121 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1122 KATTR_ID, "navs", KATTR__MAX);
1123 if (kerr != KCGI_OK)
1125 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1126 KATTR_HREF, href_diff, KATTR__MAX);
1127 if (kerr != KCGI_OK)
1129 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1130 if (kerr != KCGI_OK)
1132 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1133 if (kerr != KCGI_OK)
1136 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1137 if (kerr != KCGI_OK)
1140 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1141 gw_trans->repo_name, n_header->commit_id) == -1) {
1142 error = got_error_from_errno("asprintf");
1145 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1146 KATTR_HREF, href_blob, KATTR__MAX);
1147 if (kerr != KCGI_OK)
1149 khtml_puts(gw_trans->gw_html_req, "tree");
1150 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1151 if (kerr != KCGI_OK)
1153 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1154 if (kerr != KCGI_OK)
1157 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1158 KATTR_ID, "dotted_line", KATTR__MAX);
1159 if (kerr != KCGI_OK)
1161 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1162 if (kerr != KCGI_OK)
1173 got_ref_list_free(&header->refs);
1174 gw_free_headers(header);
1175 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1176 gw_free_headers(n_header);
1180 if (error == NULL && kerr != KCGI_OK)
1181 error = gw_kcgi_error(kerr);
1185 static const struct got_error *
1186 gw_summary(struct gw_trans *gw_trans)
1188 const struct got_error *error = NULL;
1190 enum kcgi_err kerr = KCGI_OK;
1192 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1193 return got_error_from_errno("pledge");
1195 /* unveil is applied with gw_briefs below */
1197 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1198 "summary_wrapper", KATTR__MAX);
1199 if (kerr != KCGI_OK)
1200 return gw_kcgi_error(kerr);
1202 if (gw_trans->gw_conf->got_show_repo_description &&
1203 gw_trans->gw_dir->description != NULL &&
1204 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1205 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1206 KATTR_ID, "description_title", KATTR__MAX);
1207 if (kerr != KCGI_OK)
1209 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1210 if (kerr != KCGI_OK)
1212 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1213 if (kerr != KCGI_OK)
1215 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1216 KATTR_ID, "description", KATTR__MAX);
1217 if (kerr != KCGI_OK)
1219 kerr = khtml_puts(gw_trans->gw_html_req,
1220 gw_trans->gw_dir->description);
1221 if (kerr != KCGI_OK)
1223 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1224 if (kerr != KCGI_OK)
1228 if (gw_trans->gw_conf->got_show_repo_owner &&
1229 gw_trans->gw_dir->owner != NULL &&
1230 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1232 KATTR_ID, "repo_owner_title", KATTR__MAX);
1233 if (kerr != KCGI_OK)
1235 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1236 if (kerr != KCGI_OK)
1238 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1239 if (kerr != KCGI_OK)
1241 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1242 KATTR_ID, "repo_owner", KATTR__MAX);
1243 if (kerr != KCGI_OK)
1245 kerr = khtml_puts(gw_trans->gw_html_req,
1246 gw_trans->gw_dir->owner);
1247 if (kerr != KCGI_OK)
1249 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1250 if (kerr != KCGI_OK)
1254 if (gw_trans->gw_conf->got_show_repo_age) {
1255 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1256 "refs/heads", TM_LONG);
1260 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1261 KATTR_ID, "last_change_title", KATTR__MAX);
1262 if (kerr != KCGI_OK)
1264 kerr = khtml_puts(gw_trans->gw_html_req,
1266 if (kerr != KCGI_OK)
1268 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1269 if (kerr != KCGI_OK)
1271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1272 KATTR_ID, "last_change", KATTR__MAX);
1273 if (kerr != KCGI_OK)
1275 kerr = khtml_puts(gw_trans->gw_html_req, age);
1276 if (kerr != KCGI_OK)
1278 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1279 if (kerr != KCGI_OK)
1284 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1285 gw_trans->gw_dir->url != NULL &&
1286 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1287 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1288 KATTR_ID, "cloneurl_title", KATTR__MAX);
1289 if (kerr != KCGI_OK)
1291 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1292 if (kerr != KCGI_OK)
1294 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1295 if (kerr != KCGI_OK)
1297 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1298 KATTR_ID, "cloneurl", KATTR__MAX);
1299 if (kerr != KCGI_OK)
1301 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1302 if (kerr != KCGI_OK)
1304 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1305 if (kerr != KCGI_OK)
1309 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1310 if (kerr != KCGI_OK)
1313 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1314 "briefs_title_wrapper", KATTR__MAX);
1315 if (kerr != KCGI_OK)
1317 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1318 "briefs_title", KATTR__MAX);
1319 if (kerr != KCGI_OK)
1321 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1322 if (kerr != KCGI_OK)
1324 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1325 if (kerr != KCGI_OK)
1327 error = gw_briefs(gw_trans);
1331 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1336 error = gw_output_repo_heads(gw_trans);
1339 if (error == NULL && kerr != KCGI_OK)
1340 error = gw_kcgi_error(kerr);
1344 static const struct got_error *
1345 gw_tree(struct gw_trans *gw_trans)
1347 const struct got_error *error = NULL;
1348 struct gw_header *header = NULL;
1349 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1353 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1354 return got_error_from_errno("pledge");
1356 if ((header = gw_init_header()) == NULL)
1357 return got_error_from_errno("malloc");
1359 error = gw_apply_unveil(gw_trans->gw_dir->path);
1363 error = gw_get_header(gw_trans, header, 1);
1367 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1368 "tree_header_wrapper", KATTR__MAX);
1369 if (kerr != KCGI_OK)
1371 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1372 "tree_header", KATTR__MAX);
1373 if (kerr != KCGI_OK)
1375 error = gw_gen_tree_header(gw_trans, header->tree_id);
1378 error = gw_get_time_str(&age, header->committer_time,
1382 error = gw_gen_age_header(gw_trans, age ?age : "");
1385 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1388 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1389 if (kerr != KCGI_OK)
1391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1392 "dotted_line", KATTR__MAX);
1393 if (kerr != KCGI_OK)
1395 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1396 if (kerr != KCGI_OK)
1399 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1400 "tree", KATTR__MAX);
1401 if (kerr != KCGI_OK)
1403 error = gw_output_repo_tree(gw_trans);
1407 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1409 got_ref_list_free(&header->refs);
1410 gw_free_headers(header);
1411 free(tree_html_disp);
1415 if (error == NULL && kerr != KCGI_OK)
1416 error = gw_kcgi_error(kerr);
1420 static const struct got_error *
1421 gw_tag(struct gw_trans *gw_trans)
1423 const struct got_error *error = NULL;
1424 struct gw_header *header = NULL;
1427 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1428 return got_error_from_errno("pledge");
1430 if ((header = gw_init_header()) == NULL)
1431 return got_error_from_errno("malloc");
1433 error = gw_apply_unveil(gw_trans->gw_dir->path);
1437 error = gw_get_header(gw_trans, header, 1);
1441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1442 "tag_header_wrapper", KATTR__MAX);
1443 if (kerr != KCGI_OK)
1445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1446 "tag_header", KATTR__MAX);
1447 if (kerr != KCGI_OK)
1449 error = gw_gen_commit_header(gw_trans, header->commit_id,
1453 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1456 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1457 if (kerr != KCGI_OK)
1459 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1460 "dotted_line", KATTR__MAX);
1461 if (kerr != KCGI_OK)
1463 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1464 if (kerr != KCGI_OK)
1467 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1468 "tree", KATTR__MAX);
1469 if (kerr != KCGI_OK)
1472 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1476 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1478 got_ref_list_free(&header->refs);
1479 gw_free_headers(header);
1480 if (error == NULL && kerr != KCGI_OK)
1481 error = gw_kcgi_error(kerr);
1485 static const struct got_error *
1486 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1488 const struct got_error *error = NULL;
1493 if (asprintf(&dir_test, "%s/%s/%s",
1494 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1495 GOTWEB_GIT_DIR) == -1)
1496 return got_error_from_errno("asprintf");
1498 dt = opendir(dir_test);
1502 gw_dir->path = strdup(dir_test);
1503 if (gw_dir->path == NULL) {
1505 error = got_error_from_errno("strdup");
1512 if (asprintf(&dir_test, "%s/%s/%s",
1513 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1514 GOTWEB_GOT_DIR) == -1) {
1516 error = got_error_from_errno("asprintf");
1520 dt = opendir(dir_test);
1525 error = got_error(GOT_ERR_NOT_GIT_REPO);
1529 if (asprintf(&dir_test, "%s/%s",
1530 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1531 error = got_error_from_errno("asprintf");
1536 gw_dir->path = strdup(dir_test);
1537 if (gw_dir->path == NULL) {
1539 error = got_error_from_errno("strdup");
1543 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1547 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1550 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1551 "refs/heads", TM_DIFF);
1554 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1562 static const struct got_error *
1563 gw_load_got_paths(struct gw_trans *gw_trans)
1565 const struct got_error *error = NULL;
1567 struct dirent **sd_dent;
1568 struct gw_dir *gw_dir;
1570 unsigned int d_cnt, d_i;
1572 d = opendir(gw_trans->gw_conf->got_repos_path);
1574 error = got_error_from_errno2("opendir",
1575 gw_trans->gw_conf->got_repos_path);
1579 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1582 error = got_error_from_errno2("scandir",
1583 gw_trans->gw_conf->got_repos_path);
1587 for (d_i = 0; d_i < d_cnt; d_i++) {
1588 if (gw_trans->gw_conf->got_max_repos > 0 &&
1589 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1590 break; /* account for parent and self */
1592 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1593 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1596 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1600 error = gw_load_got_path(gw_trans, gw_dir);
1601 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1608 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1609 !got_path_dir_is_empty(gw_dir->path)) {
1610 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1612 gw_trans->repos_total++;
1620 static const struct got_error *
1621 gw_parse_querystring(struct gw_trans *gw_trans)
1623 const struct got_error *error = NULL;
1625 struct gw_query_action *action = NULL;
1628 if (gw_trans->gw_req->fieldnmap[0]) {
1629 error = got_error_from_errno("bad parse");
1631 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1632 /* define gw_trans->repo_path */
1633 gw_trans->repo_name = p->parsed.s;
1635 if (asprintf(&gw_trans->repo_path, "%s/%s",
1636 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1637 return got_error_from_errno("asprintf");
1639 /* get action and set function */
1640 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1641 for (i = 0; i < nitems(gw_query_funcs); i++) {
1642 action = &gw_query_funcs[i];
1643 if (action->func_name == NULL)
1645 if (strcmp(action->func_name,
1646 p->parsed.s) == 0) {
1647 gw_trans->action = i;
1651 if (gw_trans->action == -1) {
1652 gw_trans->action = GW_ERR;
1653 gw_trans->error = got_error_from_errno("bad action");
1657 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1658 if (asprintf(&gw_trans->commit, "%s",
1660 return got_error_from_errno("asprintf");
1663 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1664 gw_trans->repo_file = p->parsed.s;
1666 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1667 if (asprintf(&gw_trans->repo_folder, "%s",
1669 return got_error_from_errno("asprintf");
1672 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF])) {
1673 if (asprintf(&gw_trans->headref, "%s",
1675 return got_error_from_errno("asprintf");
1678 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1682 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1686 gw_trans->action = GW_INDEX;
1688 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1689 gw_trans->page = p->parsed.i;
1694 static const struct got_error *
1695 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1697 const struct got_error *error;
1699 *gw_dir = malloc(sizeof(**gw_dir));
1700 if (*gw_dir == NULL)
1701 return got_error_from_errno("malloc");
1703 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1704 error = got_error_from_errno("asprintf");
1713 static const struct got_error *
1714 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1718 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1719 if (kerr != KCGI_OK)
1720 return gw_kcgi_error(kerr);
1721 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1723 if (kerr != KCGI_OK)
1724 return gw_kcgi_error(kerr);
1725 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1727 if (kerr != KCGI_OK)
1728 return gw_kcgi_error(kerr);
1729 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1731 if (kerr != KCGI_OK)
1732 return gw_kcgi_error(kerr);
1733 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1734 if (kerr != KCGI_OK)
1735 return gw_kcgi_error(kerr);
1736 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1738 if (kerr != KCGI_OK)
1739 return gw_kcgi_error(kerr);
1741 /* XXX repo_file could be NULL if not present in querystring */
1742 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1743 kerr = khttp_head(gw_trans->gw_req,
1744 kresps[KRESP_CONTENT_DISPOSITION],
1745 "attachment; filename=%s", gw_trans->repo_file);
1746 if (kerr != KCGI_OK)
1747 return gw_kcgi_error(kerr);
1750 kerr = khttp_body(gw_trans->gw_req);
1751 return gw_kcgi_error(kerr);
1754 static const struct got_error *
1755 gw_display_index(struct gw_trans *gw_trans)
1757 const struct got_error *error;
1760 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1764 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1765 if (kerr != KCGI_OK)
1766 return gw_kcgi_error(kerr);
1768 if (gw_trans->action != GW_BLOB) {
1769 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1770 gw_query_funcs[gw_trans->action].template);
1771 if (kerr != KCGI_OK) {
1772 khtml_close(gw_trans->gw_html_req);
1773 return gw_kcgi_error(kerr);
1777 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1780 static const struct got_error *
1781 gw_error(struct gw_trans *gw_trans)
1785 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
1787 return gw_kcgi_error(kerr);
1791 gw_template(size_t key, void *arg)
1793 const struct got_error *error = NULL;
1795 struct gw_trans *gw_trans = arg;
1796 char *img_src = NULL;
1800 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1801 KATTR_NAME, "viewport",
1802 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1804 if (kerr != KCGI_OK)
1806 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1807 if (kerr != KCGI_OK)
1809 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1810 KATTR_CHARSET, "utf-8",
1812 if (kerr != KCGI_OK)
1814 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1815 if (kerr != KCGI_OK)
1817 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1818 KATTR_NAME, "msapplication-TileColor",
1819 KATTR_CONTENT, "#da532c", KATTR__MAX);
1820 if (kerr != KCGI_OK)
1822 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1823 if (kerr != KCGI_OK)
1825 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1826 KATTR_NAME, "theme-color",
1827 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1828 if (kerr != KCGI_OK)
1830 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1831 if (kerr != KCGI_OK)
1833 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1834 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1835 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1836 if (kerr != KCGI_OK)
1838 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1839 if (kerr != KCGI_OK)
1841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1842 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1843 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1844 if (kerr != KCGI_OK)
1846 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1847 if (kerr != KCGI_OK)
1849 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1850 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1851 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1852 if (kerr != KCGI_OK)
1854 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1855 if (kerr != KCGI_OK)
1857 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1858 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1860 if (kerr != KCGI_OK)
1862 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1863 if (kerr != KCGI_OK)
1865 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1866 KATTR_REL, "mask-icon", KATTR_HREF,
1867 "/safari-pinned-tab.svg", KATTR__MAX);
1868 if (kerr != KCGI_OK)
1870 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1871 if (kerr != KCGI_OK)
1873 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1874 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1875 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1876 if (kerr != KCGI_OK)
1878 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1879 if (kerr != KCGI_OK)
1883 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1884 KATTR_ID, "got_link", KATTR__MAX);
1885 if (kerr != KCGI_OK)
1887 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1888 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1889 KATTR_TARGET, "_sotd", KATTR__MAX);
1890 if (kerr != KCGI_OK)
1892 if (asprintf(&img_src, "/%s",
1893 gw_trans->gw_conf->got_logo) == -1)
1895 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1896 KATTR_SRC, img_src, KATTR__MAX);
1897 if (kerr != KCGI_OK) {
1901 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1902 if (kerr != KCGI_OK) {
1907 case (TEMPL_SITEPATH):
1908 error = gw_output_site_link(gw_trans);
1913 if (gw_trans->gw_conf->got_site_name != NULL) {
1914 kerr = khtml_puts(gw_trans->gw_html_req,
1915 gw_trans->gw_conf->got_site_name);
1916 if (kerr != KCGI_OK)
1920 case (TEMPL_SEARCH):
1922 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1923 "search", KATTR__MAX);
1924 if (kerr != KCGI_OK)
1926 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1927 KATTR_METHOD, "POST", KATTR__MAX);
1928 if (kerr != KCGI_OK)
1930 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1931 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1932 KATTR_MAXLENGTH, "50", KATTR__MAX);
1933 if (kerr != KCGI_OK)
1935 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1937 if (kerr != KCGI_OK)
1939 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1940 if (kerr != KCGI_OK)
1942 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1943 if (kerr != KCGI_OK)
1946 case(TEMPL_SITEOWNER):
1947 if (gw_trans->gw_conf->got_site_owner != NULL &&
1948 gw_trans->gw_conf->got_show_site_owner) {
1949 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1950 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1951 if (kerr != KCGI_OK)
1953 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1954 KATTR_ID, "site_owner", KATTR__MAX);
1955 if (kerr != KCGI_OK)
1957 kerr = khtml_puts(gw_trans->gw_html_req,
1958 gw_trans->gw_conf->got_site_owner);
1959 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1960 if (kerr != KCGI_OK)
1964 case(TEMPL_CONTENT):
1965 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1967 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1968 KATTR_ID, "tmpl_err", KATTR__MAX);
1969 if (kerr != KCGI_OK)
1971 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1972 if (kerr != KCGI_OK)
1974 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1975 if (kerr != KCGI_OK)
1977 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1978 if (kerr != KCGI_OK)
1988 static const struct got_error *
1989 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1991 const struct got_error *error = NULL;
1992 enum kcgi_err kerr = KCGI_OK;
1994 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1995 KATTR_ID, "header_commit_title", KATTR__MAX);
1996 if (kerr != KCGI_OK)
1998 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
1999 if (kerr != KCGI_OK)
2001 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2002 if (kerr != KCGI_OK)
2004 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2005 KATTR_ID, "header_commit", KATTR__MAX);
2006 if (kerr != KCGI_OK)
2008 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2009 if (kerr != KCGI_OK)
2011 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2012 if (kerr != KCGI_OK)
2015 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2016 if (kerr != KCGI_OK)
2018 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2019 if (kerr != KCGI_OK)
2021 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2022 if (kerr != KCGI_OK)
2025 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2027 if (error == NULL && kerr != KCGI_OK)
2028 error = gw_kcgi_error(kerr);
2032 static const struct got_error *
2033 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2035 const struct got_error *error = NULL;
2036 enum kcgi_err kerr = KCGI_OK;
2038 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2039 KATTR_ID, "header_diff_title", KATTR__MAX);
2040 if (kerr != KCGI_OK)
2042 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2043 if (kerr != KCGI_OK)
2045 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2046 if (kerr != KCGI_OK)
2048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2049 KATTR_ID, "header_diff", KATTR__MAX);
2050 if (kerr != KCGI_OK)
2053 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2054 if (kerr != KCGI_OK)
2057 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2058 if (kerr != KCGI_OK)
2060 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2061 if (kerr != KCGI_OK)
2063 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2065 if (error == NULL && kerr != KCGI_OK)
2066 error = gw_kcgi_error(kerr);
2070 static const struct got_error *
2071 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2073 const struct got_error *error = NULL;
2074 enum kcgi_err kerr = KCGI_OK;
2076 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2077 KATTR_ID, "header_age_title", KATTR__MAX);
2078 if (kerr != KCGI_OK)
2080 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2081 if (kerr != KCGI_OK)
2083 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2084 if (kerr != KCGI_OK)
2086 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2087 KATTR_ID, "header_age", KATTR__MAX);
2088 if (kerr != KCGI_OK)
2090 kerr = khtml_puts(gw_trans->gw_html_req, str);
2091 if (kerr != KCGI_OK)
2093 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2095 if (error == NULL && kerr != KCGI_OK)
2096 error = gw_kcgi_error(kerr);
2100 static const struct got_error *
2101 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2103 const struct got_error *error = NULL;
2106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2107 KATTR_ID, "header_author_title", KATTR__MAX);
2108 if (kerr != KCGI_OK)
2110 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2111 if (kerr != KCGI_OK)
2113 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2114 if (kerr != KCGI_OK)
2116 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2117 KATTR_ID, "header_author", KATTR__MAX);
2118 if (kerr != KCGI_OK)
2120 kerr = khtml_puts(gw_trans->gw_html_req, str);
2121 if (kerr != KCGI_OK)
2123 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2125 if (error == NULL && kerr != KCGI_OK)
2126 error = gw_kcgi_error(kerr);
2130 static const struct got_error *
2131 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2133 const struct got_error *error = NULL;
2136 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2137 KATTR_ID, "header_committer_title", KATTR__MAX);
2138 if (kerr != KCGI_OK)
2140 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2141 if (kerr != KCGI_OK)
2143 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2144 if (kerr != KCGI_OK)
2146 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2147 KATTR_ID, "header_committer", KATTR__MAX);
2148 if (kerr != KCGI_OK)
2150 kerr = khtml_puts(gw_trans->gw_html_req, str);
2151 if (kerr != KCGI_OK)
2153 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2155 if (error == NULL && kerr != KCGI_OK)
2156 error = gw_kcgi_error(kerr);
2160 static const struct got_error *
2161 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2163 const struct got_error *error = NULL;
2166 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2167 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2168 if (kerr != KCGI_OK)
2170 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2171 if (kerr != KCGI_OK)
2173 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2174 if (kerr != KCGI_OK)
2176 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2177 KATTR_ID, "header_commit_msg", KATTR__MAX);
2178 if (kerr != KCGI_OK)
2180 kerr = khttp_puts(gw_trans->gw_req, str);
2181 if (kerr != KCGI_OK)
2183 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2185 if (error == NULL && kerr != KCGI_OK)
2186 error = gw_kcgi_error(kerr);
2190 static const struct got_error *
2191 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2193 const struct got_error *error = NULL;
2196 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2197 KATTR_ID, "header_tree_title", KATTR__MAX);
2198 if (kerr != KCGI_OK)
2200 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2201 if (kerr != KCGI_OK)
2203 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2204 if (kerr != KCGI_OK)
2206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2207 KATTR_ID, "header_tree", KATTR__MAX);
2208 if (kerr != KCGI_OK)
2210 kerr = khtml_puts(gw_trans->gw_html_req, str);
2211 if (kerr != KCGI_OK)
2213 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2215 if (error == NULL && kerr != KCGI_OK)
2216 error = gw_kcgi_error(kerr);
2220 static const struct got_error *
2221 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2224 const struct got_error *error = NULL;
2226 char *d_file = NULL;
2230 *description = NULL;
2231 if (gw_trans->gw_conf->got_show_repo_description == 0)
2234 if (asprintf(&d_file, "%s/description", dir) == -1)
2235 return got_error_from_errno("asprintf");
2237 f = fopen(d_file, "r");
2239 if (errno == ENOENT || errno == EACCES)
2241 error = got_error_from_errno2("fopen", d_file);
2245 if (fseek(f, 0, SEEK_END) == -1) {
2246 error = got_ferror(f, GOT_ERR_IO);
2251 error = got_ferror(f, GOT_ERR_IO);
2254 if (fseek(f, 0, SEEK_SET) == -1) {
2255 error = got_ferror(f, GOT_ERR_IO);
2258 *description = calloc(len + 1, sizeof(**description));
2259 if (*description == NULL) {
2260 error = got_error_from_errno("calloc");
2264 n = fread(*description, 1, len, f);
2265 if (n == 0 && ferror(f))
2266 error = got_ferror(f, GOT_ERR_IO);
2268 if (f != NULL && fclose(f) == -1 && error == NULL)
2269 error = got_error_from_errno("fclose");
2274 static const struct got_error *
2275 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2279 char *years = "years ago", *months = "months ago";
2280 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2281 char *minutes = "minutes ago", *seconds = "seconds ago";
2282 char *now = "right now";
2290 diff_time = time(NULL) - committer_time;
2291 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2292 if (asprintf(repo_age, "%lld %s",
2293 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2294 return got_error_from_errno("asprintf");
2295 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2296 if (asprintf(repo_age, "%lld %s",
2297 (diff_time / 60 / 60 / 24 / (365 / 12)),
2299 return got_error_from_errno("asprintf");
2300 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2301 if (asprintf(repo_age, "%lld %s",
2302 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2303 return got_error_from_errno("asprintf");
2304 } else if (diff_time > 60 * 60 * 24 * 2) {
2305 if (asprintf(repo_age, "%lld %s",
2306 (diff_time / 60 / 60 / 24), days) == -1)
2307 return got_error_from_errno("asprintf");
2308 } else if (diff_time > 60 * 60 * 2) {
2309 if (asprintf(repo_age, "%lld %s",
2310 (diff_time / 60 / 60), hours) == -1)
2311 return got_error_from_errno("asprintf");
2312 } else if (diff_time > 60 * 2) {
2313 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2315 return got_error_from_errno("asprintf");
2316 } else if (diff_time > 2) {
2317 if (asprintf(repo_age, "%lld %s", diff_time,
2319 return got_error_from_errno("asprintf");
2321 if (asprintf(repo_age, "%s", now) == -1)
2322 return got_error_from_errno("asprintf");
2326 if (gmtime_r(&committer_time, &tm) == NULL)
2327 return got_error_from_errno("gmtime_r");
2329 s = asctime_r(&tm, datebuf);
2331 return got_error_from_errno("asctime_r");
2333 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2334 return got_error_from_errno("asprintf");
2340 static const struct got_error *
2341 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2342 char *repo_ref, int ref_tm)
2344 const struct got_error *error = NULL;
2345 struct got_object_id *id = NULL;
2346 struct got_repository *repo = NULL;
2347 struct got_commit_object *commit = NULL;
2348 struct got_reflist_head refs;
2349 struct got_reflist_entry *re;
2350 struct got_reference *head_ref;
2352 time_t committer_time = 0, cmp_time = 0;
2353 const char *refname;
2356 SIMPLEQ_INIT(&refs);
2358 if (repo_ref == NULL)
2361 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2364 if (gw_trans->gw_conf->got_show_repo_age == 0)
2367 error = got_repo_open(&repo, dir, NULL);
2372 error = got_ref_list(&refs, repo, "refs/heads",
2373 got_ref_cmp_by_name, NULL);
2375 error = got_ref_list(&refs, repo, repo_ref,
2376 got_ref_cmp_by_name, NULL);
2380 SIMPLEQ_FOREACH(re, &refs, entry) {
2382 refname = strdup(repo_ref);
2383 if (refname == NULL) {
2384 error = got_error_from_errno("strdup");
2388 refname = got_ref_get_name(re->ref);
2389 if (refname == NULL) {
2390 error = got_error_from_errno("strdup");
2394 error = got_ref_open(&head_ref, repo, refname, 0);
2398 error = got_ref_resolve(&id, repo, head_ref);
2399 got_ref_close(head_ref);
2403 error = got_object_open_as_commit(&commit, repo, id);
2408 got_object_commit_get_committer_time(commit);
2410 if (cmp_time < committer_time)
2411 cmp_time = committer_time;
2414 if (cmp_time != 0) {
2415 committer_time = cmp_time;
2416 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2419 got_ref_list_free(&refs);
2424 static const struct got_error *
2425 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2427 const struct got_error *error;
2429 struct got_object_id *id1 = NULL, *id2 = NULL;
2430 char *label1 = NULL, *label2 = NULL, *line = NULL;
2432 size_t linesize = 0;
2434 enum kcgi_err kerr = KCGI_OK;
2440 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2444 if (header->parent_id != NULL &&
2445 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2446 error = got_repo_match_object_id(&id1, &label1,
2447 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2452 error = got_repo_match_object_id(&id2, &label2,
2453 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2457 error = got_object_get_type(&obj_type, header->repo, id2);
2461 case GOT_OBJ_TYPE_BLOB:
2462 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2465 case GOT_OBJ_TYPE_TREE:
2466 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2469 case GOT_OBJ_TYPE_COMMIT:
2470 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2474 error = got_error(GOT_ERR_OBJ_TYPE);
2479 if (fseek(f, 0, SEEK_SET) == -1) {
2480 error = got_ferror(f, GOT_ERR_IO);
2484 while ((linelen = getline(&line, &linesize, f)) != -1) {
2485 error = gw_colordiff_line(gw_trans, line);
2488 /* XXX: KHTML_PRETTY breaks this */
2489 kerr = khtml_puts(gw_trans->gw_html_req, line);
2490 if (kerr != KCGI_OK)
2492 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2493 if (kerr != KCGI_OK)
2496 if (linelen == -1 && ferror(f))
2497 error = got_error_from_errno("getline");
2499 if (f && fclose(f) == -1 && error == NULL)
2500 error = got_error_from_errno("fclose");
2507 if (error == NULL && kerr != KCGI_OK)
2508 error = gw_kcgi_error(kerr);
2512 static const struct got_error *
2513 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2515 const struct got_error *error = NULL;
2516 struct got_repository *repo;
2517 const char *gitconfig_owner;
2521 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2524 error = got_repo_open(&repo, dir, NULL);
2527 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2528 if (gitconfig_owner) {
2529 *owner = strdup(gitconfig_owner);
2531 error = got_error_from_errno("strdup");
2533 got_repo_close(repo);
2537 static const struct got_error *
2538 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2540 const struct got_error *error = NULL;
2542 char *d_file = NULL;
2548 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2549 return got_error_from_errno("asprintf");
2551 f = fopen(d_file, "r");
2553 if (errno != ENOENT && errno != EACCES)
2554 error = got_error_from_errno2("fopen", d_file);
2558 if (fseek(f, 0, SEEK_END) == -1) {
2559 error = got_ferror(f, GOT_ERR_IO);
2564 error = got_ferror(f, GOT_ERR_IO);
2567 if (fseek(f, 0, SEEK_SET) == -1) {
2568 error = got_ferror(f, GOT_ERR_IO);
2572 *url = calloc(len + 1, sizeof(**url));
2574 error = got_error_from_errno("calloc");
2578 n = fread(*url, 1, len, f);
2579 if (n == 0 && ferror(f))
2580 error = got_ferror(f, GOT_ERR_IO);
2582 if (f && fclose(f) == -1 && error == NULL)
2583 error = got_error_from_errno("fclose");
2588 static const struct got_error *
2589 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2590 int limit, int tag_type)
2592 const struct got_error *error = NULL;
2593 struct got_repository *repo = NULL;
2594 struct got_reflist_head refs;
2595 struct got_reflist_entry *re;
2597 char *id_str = NULL, *newline, *href_commits = NULL;
2598 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2599 struct got_tag_object *tag = NULL;
2600 enum kcgi_err kerr = KCGI_OK;
2601 int summary_header_displayed = 0;
2603 SIMPLEQ_INIT(&refs);
2605 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2609 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2613 SIMPLEQ_FOREACH(re, &refs, entry) {
2614 const char *refname;
2616 const char *tag_commit;
2618 struct got_object_id *id;
2619 struct got_commit_object *commit = NULL;
2621 refname = got_ref_get_name(re->ref);
2622 if (strncmp(refname, "refs/tags/", 10) != 0)
2626 error = got_ref_resolve(&id, repo, re->ref);
2630 error = got_object_open_as_tag(&tag, repo, id);
2632 if (error->code != GOT_ERR_OBJ_TYPE) {
2636 /* "lightweight" tag */
2637 error = got_object_open_as_commit(&commit, repo, id);
2642 tagger = got_object_commit_get_committer(commit);
2644 got_object_commit_get_committer_time(commit);
2645 error = got_object_id_str(&id_str, id);
2649 tagger = got_object_tag_get_tagger(tag);
2650 tagger_time = got_object_tag_get_tagger_time(tag);
2651 error = got_object_id_str(&id_str,
2652 got_object_tag_get_object_id(tag));
2657 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2658 strlen(id_str)) != 0)
2662 error = got_object_commit_get_logmsg(&tag_commit0,
2666 got_object_commit_close(commit);
2668 tag_commit0 = strdup(got_object_tag_get_message(tag));
2669 if (tag_commit0 == NULL) {
2670 error = got_error_from_errno("strdup");
2675 tag_commit = tag_commit0;
2676 while (*tag_commit == '\n')
2681 newline = strchr(tag_commit, '\n');
2685 if (summary_header_displayed == 0) {
2686 kerr = khtml_attr(gw_trans->gw_html_req,
2687 KELEM_DIV, KATTR_ID,
2688 "summary_tags_title_wrapper", KATTR__MAX);
2689 if (kerr != KCGI_OK)
2691 kerr = khtml_attr(gw_trans->gw_html_req,
2692 KELEM_DIV, KATTR_ID,
2693 "summary_tags_title", KATTR__MAX);
2694 if (kerr != KCGI_OK)
2696 kerr = khtml_puts(gw_trans->gw_html_req,
2698 if (kerr != KCGI_OK)
2700 kerr = khtml_closeelem(gw_trans->gw_html_req,
2702 if (kerr != KCGI_OK)
2704 kerr = khtml_attr(gw_trans->gw_html_req,
2705 KELEM_DIV, KATTR_ID,
2706 "summary_tags_content", KATTR__MAX);
2707 if (kerr != KCGI_OK)
2709 summary_header_displayed = 1;
2712 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2713 KATTR_ID, "tags_wrapper", KATTR__MAX);
2714 if (kerr != KCGI_OK)
2716 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2717 KATTR_ID, "tags_age", KATTR__MAX);
2718 if (kerr != KCGI_OK)
2720 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2723 kerr = khtml_puts(gw_trans->gw_html_req,
2725 if (kerr != KCGI_OK)
2727 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2728 if (kerr != KCGI_OK)
2730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2731 KATTR_ID, "tags", KATTR__MAX);
2732 if (kerr != KCGI_OK)
2734 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2735 if (kerr != KCGI_OK)
2737 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2738 if (kerr != KCGI_OK)
2740 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2741 KATTR_ID, "tags_name", KATTR__MAX);
2742 if (kerr != KCGI_OK)
2744 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2745 gw_trans->repo_name, id_str) == -1) {
2746 error = got_error_from_errno("asprintf");
2749 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2750 KATTR_HREF, href_tag, KATTR__MAX);
2751 if (kerr != KCGI_OK)
2753 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2754 if (kerr != KCGI_OK)
2756 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2757 if (kerr != KCGI_OK)
2760 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2761 KATTR_ID, "navs_wrapper", KATTR__MAX);
2762 if (kerr != KCGI_OK)
2764 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2765 KATTR_ID, "navs", KATTR__MAX);
2766 if (kerr != KCGI_OK)
2769 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2770 KATTR_HREF, href_tag, KATTR__MAX);
2771 if (kerr != KCGI_OK)
2773 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2774 if (kerr != KCGI_OK)
2776 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2777 if (kerr != KCGI_OK)
2780 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2781 if (kerr != KCGI_OK)
2783 if (asprintf(&href_briefs,
2784 "?path=%s&action=briefs&commit=%s",
2785 gw_trans->repo_name, id_str) == -1) {
2786 error = got_error_from_errno("asprintf");
2789 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2790 KATTR_HREF, href_briefs, KATTR__MAX);
2791 if (kerr != KCGI_OK)
2793 kerr = khtml_puts(gw_trans->gw_html_req,
2795 if (kerr != KCGI_OK)
2797 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2798 if (kerr != KCGI_OK)
2801 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2802 if (kerr != KCGI_OK)
2805 if (asprintf(&href_commits,
2806 "?path=%s&action=commits&commit=%s",
2807 gw_trans->repo_name, id_str) == -1) {
2808 error = got_error_from_errno("asprintf");
2811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2812 KATTR_HREF, href_commits, KATTR__MAX);
2813 if (kerr != KCGI_OK)
2815 kerr = khtml_puts(gw_trans->gw_html_req,
2817 if (kerr != KCGI_OK)
2819 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2820 if (kerr != KCGI_OK)
2823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2824 KATTR_ID, "dotted_line", KATTR__MAX);
2825 if (kerr != KCGI_OK)
2827 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2828 if (kerr != KCGI_OK)
2832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2833 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2834 if (kerr != KCGI_OK)
2836 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2837 if (kerr != KCGI_OK)
2839 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2840 if (kerr != KCGI_OK)
2842 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2843 KATTR_ID, "tag_info_date", KATTR__MAX);
2844 if (kerr != KCGI_OK)
2846 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2849 kerr = khtml_puts(gw_trans->gw_html_req,
2851 if (kerr != KCGI_OK)
2853 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2854 if (kerr != KCGI_OK)
2857 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2858 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2859 if (kerr != KCGI_OK)
2861 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2862 if (kerr != KCGI_OK)
2864 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2865 if (kerr != KCGI_OK)
2867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2868 KATTR_ID, "tag_info_date", KATTR__MAX);
2869 if (kerr != KCGI_OK)
2871 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2872 if (kerr != KCGI_OK)
2874 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2875 if (kerr != KCGI_OK)
2878 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2879 KATTR_ID, "tag_info", KATTR__MAX);
2880 if (kerr != KCGI_OK)
2882 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2883 if (kerr != KCGI_OK)
2889 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2890 if (kerr != KCGI_OK)
2893 if (limit && --limit == 0)
2897 got_object_tag_close(tag);
2910 href_commits = NULL;
2914 got_object_tag_close(tag);
2921 got_ref_list_free(&refs);
2923 got_repo_close(repo);
2924 if (error == NULL && kerr != KCGI_OK)
2925 error = gw_kcgi_error(kerr);
2930 gw_free_headers(struct gw_header *header)
2934 if (header->commit != NULL)
2935 got_object_commit_close(header->commit);
2937 got_repo_close(header->repo);
2938 free(header->refs_str);
2939 free(header->commit_id);
2940 free(header->parent_id);
2941 free(header->tree_id);
2942 free(header->commit_msg);
2945 static struct gw_header *
2948 struct gw_header *header;
2950 header = malloc(sizeof(*header));
2954 header->repo = NULL;
2955 header->commit = NULL;
2957 header->path = NULL;
2958 SIMPLEQ_INIT(&header->refs);
2960 header->refs_str = NULL;
2961 header->commit_id = NULL;
2962 header->parent_id = NULL;
2963 header->tree_id = NULL;
2964 header->commit_msg = NULL;
2969 static const struct got_error *
2970 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2973 const struct got_error *error = NULL;
2974 struct got_commit_graph *graph = NULL;
2976 error = got_commit_graph_open(&graph, header->path, 0);
2980 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2986 error = got_commit_graph_iter_next(&header->id, graph,
2987 header->repo, NULL, NULL);
2989 if (error->code == GOT_ERR_ITER_COMPLETED)
2993 if (header->id == NULL)
2996 error = got_object_open_as_commit(&header->commit, header->repo,
3001 error = gw_get_commit(gw_trans, header);
3003 struct gw_header *n_header = NULL;
3004 if ((n_header = gw_init_header()) == NULL) {
3005 error = got_error_from_errno("malloc");
3009 if (header->refs_str != NULL) {
3010 n_header->refs_str = strdup(header->refs_str);
3011 if (n_header->refs_str == NULL) {
3012 error = got_error_from_errno("strdup");
3016 n_header->commit_id = strdup(header->commit_id);
3017 if (n_header->commit_id == NULL) {
3018 error = got_error_from_errno("strdup");
3021 if (header->parent_id != NULL) {
3022 n_header->parent_id = strdup(header->parent_id);
3023 if (n_header->parent_id == NULL) {
3024 error = got_error_from_errno("strdup");
3028 n_header->tree_id = strdup(header->tree_id);
3029 if (n_header->tree_id == NULL) {
3030 error = got_error_from_errno("strdup");
3033 n_header->author = header->author;
3034 n_header->committer = header->committer;
3035 n_header->commit_msg = strdup(header->commit_msg);
3036 if (n_header->commit_msg == NULL) {
3037 error = got_error_from_errno("strdup");
3040 n_header->committer_time = header->committer_time;
3041 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3044 if (error || (limit && --limit == 0))
3049 got_commit_graph_close(graph);
3053 static const struct got_error *
3054 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3056 const struct got_error *error = NULL;
3057 struct got_reflist_entry *re;
3058 struct got_object_id *id2 = NULL;
3059 struct got_object_qid *parent_id;
3060 char *commit_msg = NULL, *commit_msg0;
3063 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3066 struct got_tag_object *tag = NULL;
3069 name = got_ref_get_name(re->ref);
3070 if (strcmp(name, GOT_REF_HEAD) == 0)
3072 if (strncmp(name, "refs/", 5) == 0)
3074 if (strncmp(name, "got/", 4) == 0)
3076 if (strncmp(name, "heads/", 6) == 0)
3078 if (strncmp(name, "remotes/", 8) == 0)
3080 if (strncmp(name, "tags/", 5) == 0) {
3081 error = got_object_open_as_tag(&tag, header->repo,
3084 if (error->code != GOT_ERR_OBJ_TYPE)
3087 * Ref points at something other
3094 cmp = got_object_id_cmp(tag ?
3095 got_object_tag_get_object_id(tag) : re->id, header->id);
3097 got_object_tag_close(tag);
3100 s = header->refs_str;
3101 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3102 s ? ", " : "", name) == -1) {
3103 error = got_error_from_errno("asprintf");
3105 header->refs_str = NULL;
3111 error = got_object_id_str(&header->commit_id, header->id);
3115 error = got_object_id_str(&header->tree_id,
3116 got_object_commit_get_tree_id(header->commit));
3120 if (gw_trans->action == GW_DIFF) {
3121 parent_id = SIMPLEQ_FIRST(
3122 got_object_commit_get_parent_ids(header->commit));
3123 if (parent_id != NULL) {
3124 id2 = got_object_id_dup(parent_id->id);
3126 error = got_object_id_str(&header->parent_id, id2);
3131 header->parent_id = strdup("/dev/null");
3132 if (header->parent_id == NULL) {
3133 error = got_error_from_errno("strdup");
3139 header->committer_time =
3140 got_object_commit_get_committer_time(header->commit);
3143 got_object_commit_get_author(header->commit);
3145 got_object_commit_get_committer(header->commit);
3148 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3152 commit_msg = commit_msg0;
3153 while (*commit_msg == '\n')
3156 header->commit_msg = strdup(commit_msg);
3157 if (header->commit_msg == NULL)
3158 error = got_error_from_errno("strdup");
3163 static const struct got_error *
3164 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3166 const struct got_error *error = NULL;
3167 char *in_repo_path = NULL;
3169 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3173 if (gw_trans->commit == NULL) {
3174 struct got_reference *head_ref;
3175 error = got_ref_open(&head_ref, header->repo,
3176 gw_trans->headref, 0);
3180 error = got_ref_resolve(&header->id, header->repo, head_ref);
3181 got_ref_close(head_ref);
3185 error = got_object_open_as_commit(&header->commit,
3186 header->repo, header->id);
3188 struct got_reference *ref;
3189 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3190 if (error == NULL) {
3192 error = got_ref_resolve(&header->id, header->repo, ref);
3196 error = got_object_get_type(&obj_type, header->repo,
3200 if (obj_type == GOT_OBJ_TYPE_TAG) {
3201 struct got_tag_object *tag;
3202 error = got_object_open_as_tag(&tag,
3203 header->repo, header->id);
3206 if (got_object_tag_get_object_type(tag) !=
3207 GOT_OBJ_TYPE_COMMIT) {
3208 got_object_tag_close(tag);
3209 error = got_error(GOT_ERR_OBJ_TYPE);
3213 header->id = got_object_id_dup(
3214 got_object_tag_get_object_id(tag));
3215 if (header->id == NULL)
3216 error = got_error_from_errno(
3217 "got_object_id_dup");
3218 got_object_tag_close(tag);
3221 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3222 error = got_error(GOT_ERR_OBJ_TYPE);
3225 error = got_object_open_as_commit(&header->commit,
3226 header->repo, header->id);
3230 if (header->commit == NULL) {
3231 error = got_repo_match_object_id_prefix(&header->id,
3232 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3237 error = got_repo_match_object_id_prefix(&header->id,
3238 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3242 error = got_repo_map_path(&in_repo_path, header->repo,
3243 gw_trans->repo_path, 1);
3248 header->path = strdup(in_repo_path);
3249 if (header->path == NULL) {
3250 error = got_error_from_errno("strdup");
3257 error = got_ref_list(&header->refs, header->repo, NULL,
3258 got_ref_cmp_by_name, NULL);
3262 error = gw_get_commits(gw_trans, header, limit);
3270 char datebuf[11]; /* YYYY-MM-DD + NUL */
3273 struct gw_blame_cb_args {
3274 struct blame_line *lines;
3278 off_t *line_offsets;
3280 struct got_repository *repo;
3281 struct gw_trans *gw_trans;
3284 static const struct got_error *
3285 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3287 const struct got_error *err = NULL;
3288 struct gw_blame_cb_args *a = arg;
3289 struct blame_line *bline;
3291 size_t linesize = 0;
3292 struct got_commit_object *commit = NULL;
3295 time_t committer_time;
3296 enum kcgi_err kerr = KCGI_OK;
3298 if (nlines != a->nlines ||
3299 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3300 return got_error(GOT_ERR_RANGE);
3303 return NULL; /* no change in this commit */
3305 /* Annotate this line. */
3306 bline = &a->lines[lineno - 1];
3307 if (bline->annotated)
3309 err = got_object_id_str(&bline->id_str, id);
3313 err = got_object_open_as_commit(&commit, a->repo, id);
3317 bline->committer = strdup(got_object_commit_get_committer(commit));
3318 if (bline->committer == NULL) {
3319 err = got_error_from_errno("strdup");
3323 committer_time = got_object_commit_get_committer_time(commit);
3324 if (localtime_r(&committer_time, &tm) == NULL)
3325 return got_error_from_errno("localtime_r");
3326 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3327 &tm) >= sizeof(bline->datebuf)) {
3328 err = got_error(GOT_ERR_NO_SPACE);
3331 bline->annotated = 1;
3333 /* Print lines annotated so far. */
3334 bline = &a->lines[a->lineno_cur - 1];
3335 if (!bline->annotated)
3338 offset = a->line_offsets[a->lineno_cur - 1];
3339 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3340 err = got_error_from_errno("fseeko");
3344 while (bline->annotated) {
3345 char *smallerthan, *at, *nl, *committer;
3346 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3349 if (getline(&line, &linesize, a->f) == -1) {
3351 err = got_error_from_errno("getline");
3355 committer = bline->committer;
3356 smallerthan = strchr(committer, '<');
3357 if (smallerthan && smallerthan[1] != '\0')
3358 committer = smallerthan + 1;
3359 at = strchr(committer, '@');
3362 len = strlen(committer);
3364 committer[8] = '\0';
3366 nl = strchr(line, '\n');
3370 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3371 "blame_wrapper", KATTR__MAX);
3372 if (kerr != KCGI_OK)
3374 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3375 "blame_number", KATTR__MAX);
3376 if (kerr != KCGI_OK)
3378 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3379 a->lineno_cur) == -1)
3381 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3382 if (kerr != KCGI_OK)
3384 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3385 if (kerr != KCGI_OK)
3388 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3389 "blame_hash", KATTR__MAX);
3390 if (kerr != KCGI_OK)
3392 /* XXX repo_file could be NULL if not present in querystring */
3393 if (asprintf(&href_diff,
3394 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3395 a->gw_trans->repo_name, bline->id_str,
3396 a->gw_trans->repo_file, a->gw_trans->repo_folder ?
3397 a->gw_trans->repo_folder : "") == -1) {
3398 err = got_error_from_errno("asprintf");
3401 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3402 err = got_error_from_errno("asprintf");
3405 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3406 KATTR_HREF, href_diff, KATTR__MAX);
3407 if (kerr != KCGI_OK)
3409 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3410 if (kerr != KCGI_OK)
3412 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3413 if (kerr != KCGI_OK)
3416 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3417 "blame_date", KATTR__MAX);
3418 if (kerr != KCGI_OK)
3420 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3421 if (kerr != KCGI_OK)
3423 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3424 if (kerr != KCGI_OK)
3427 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3428 "blame_author", KATTR__MAX);
3429 if (kerr != KCGI_OK)
3431 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3432 if (kerr != KCGI_OK)
3434 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3435 if (kerr != KCGI_OK)
3438 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3439 "blame_code", KATTR__MAX);
3440 if (kerr != KCGI_OK)
3442 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3443 if (kerr != KCGI_OK)
3445 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3446 if (kerr != KCGI_OK)
3449 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3450 if (kerr != KCGI_OK)
3454 bline = &a->lines[a->lineno_cur - 1];
3462 got_object_commit_close(commit);
3464 if (err == NULL && kerr != KCGI_OK)
3465 err = gw_kcgi_error(kerr);
3469 static const struct got_error *
3470 gw_output_file_blame(struct gw_trans *gw_trans)
3472 const struct got_error *error = NULL;
3473 struct got_repository *repo = NULL;
3474 struct got_object_id *obj_id = NULL;
3475 struct got_object_id *commit_id = NULL;
3476 struct got_blob_object *blob = NULL;
3477 char *path = NULL, *in_repo_path = NULL;
3478 struct gw_blame_cb_args bca;
3482 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3486 /* XXX repo_file could be NULL if not present in querystring */
3487 if (asprintf(&path, "%s%s%s",
3488 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3489 gw_trans->repo_folder ? "/" : "",
3490 gw_trans->repo_file) == -1) {
3491 error = got_error_from_errno("asprintf");
3495 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3499 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3500 GOT_OBJ_TYPE_COMMIT, 1, repo);
3504 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3508 if (obj_id == NULL) {
3509 error = got_error(GOT_ERR_NO_OBJ);
3513 error = got_object_get_type(&obj_type, repo, obj_id);
3517 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3518 error = got_error(GOT_ERR_OBJ_TYPE);
3522 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3526 bca.f = got_opentemp();
3527 if (bca.f == NULL) {
3528 error = got_error_from_errno("got_opentemp");
3531 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3532 &bca.line_offsets, bca.f, blob);
3533 if (error || bca.nlines == 0)
3536 /* Don't include \n at EOF in the blame line count. */
3537 if (bca.line_offsets[bca.nlines - 1] == filesize)
3540 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3541 if (bca.lines == NULL) {
3542 error = got_error_from_errno("calloc");
3546 bca.nlines_prec = 0;
3553 bca.gw_trans = gw_trans;
3555 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3558 free(bca.line_offsets);
3564 for (i = 0; i < bca.nlines; i++) {
3565 struct blame_line *bline = &bca.lines[i];
3566 free(bline->id_str);
3567 free(bline->committer);
3570 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3571 error = got_error_from_errno("fclose");
3573 got_object_blob_close(blob);
3575 got_repo_close(repo);
3579 static const struct got_error *
3580 gw_output_blob_buf(struct gw_trans *gw_trans)
3582 const struct got_error *error = NULL;
3583 struct got_repository *repo = NULL;
3584 struct got_object_id *obj_id = NULL;
3585 struct got_object_id *commit_id = NULL;
3586 struct got_blob_object *blob = NULL;
3587 char *path = NULL, *in_repo_path = NULL;
3588 int obj_type, set_mime = 0;
3591 enum kcgi_err kerr = KCGI_OK;
3593 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3597 /* XXX repo_file could be NULL if not present in querystring */
3598 if (asprintf(&path, "%s%s%s",
3599 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3600 gw_trans->repo_folder ? "/" : "",
3601 gw_trans->repo_file) == -1) {
3602 error = got_error_from_errno("asprintf");
3606 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3610 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3611 GOT_OBJ_TYPE_COMMIT, 1, repo);
3615 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3619 if (obj_id == NULL) {
3620 error = got_error(GOT_ERR_NO_OBJ);
3624 error = got_object_get_type(&obj_type, repo, obj_id);
3628 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3629 error = got_error(GOT_ERR_OBJ_TYPE);
3633 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3637 hdrlen = got_object_blob_get_hdrlen(blob);
3639 error = got_object_blob_read_block(&len, blob);
3642 buf = got_object_blob_get_read_buf(blob);
3645 * Skip blob object header first time around,
3646 * which also contains a zero byte.
3649 if (set_mime == 0) {
3650 if (isbinary(buf, len - hdrlen))
3651 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3653 gw_trans->mime = KMIME_TEXT_PLAIN;
3655 error = gw_display_index(gw_trans);
3659 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3668 got_object_blob_close(blob);
3670 got_repo_close(repo);
3671 if (error == NULL && kerr != KCGI_OK)
3672 error = gw_kcgi_error(kerr);
3676 static const struct got_error *
3677 gw_output_repo_tree(struct gw_trans *gw_trans)
3679 const struct got_error *error = NULL;
3680 struct got_repository *repo = NULL;
3681 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3682 struct got_tree_object *tree = NULL;
3683 char *path = NULL, *in_repo_path = NULL;
3684 char *id_str = NULL;
3685 char *build_folder = NULL;
3686 char *href_blob = NULL, *href_blame = NULL;
3687 const char *class = NULL;
3688 int nentries, i, class_flip = 0;
3689 enum kcgi_err kerr = KCGI_OK;
3691 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3695 if (gw_trans->repo_folder != NULL) {
3696 path = strdup(gw_trans->repo_folder);
3698 error = got_error_from_errno("strdup");
3702 error = got_repo_map_path(&in_repo_path, repo,
3703 gw_trans->repo_path, 1);
3707 path = in_repo_path;
3710 if (gw_trans->commit == NULL) {
3711 struct got_reference *head_ref;
3712 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3715 error = got_ref_resolve(&commit_id, repo, head_ref);
3718 got_ref_close(head_ref);
3721 error = got_repo_match_object_id(&commit_id, NULL,
3722 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3728 * XXX gw_trans->commit might have already been allocated in
3729 * gw_parse_querystring(); could we more cleanly seperate values
3730 * we received as arguments from values we compute ourselves?
3732 error = got_object_id_str(&gw_trans->commit, commit_id);
3736 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3740 error = got_object_open_as_tree(&tree, repo, tree_id);
3744 nentries = got_object_tree_get_nentries(tree);
3745 for (i = 0; i < nentries; i++) {
3746 struct got_tree_entry *te;
3747 const char *modestr = "";
3750 te = got_object_tree_get_entry(tree, i);
3752 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3756 mode = got_tree_entry_get_mode(te);
3757 if (got_object_tree_entry_is_submodule(te))
3759 else if (S_ISLNK(mode))
3761 else if (S_ISDIR(mode))
3763 else if (mode & S_IXUSR)
3766 if (class_flip == 0) {
3767 class = "back_lightgray";
3770 class = "back_white";
3774 if (S_ISDIR(mode)) {
3775 if (asprintf(&build_folder, "%s/%s",
3776 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3777 got_tree_entry_get_name(te)) == -1) {
3778 error = got_error_from_errno("asprintf");
3781 if (asprintf(&href_blob,
3782 "?path=%s&action=%s&commit=%s&folder=%s",
3783 gw_trans->repo_name, gw_get_action_name(gw_trans),
3784 gw_trans->commit, build_folder) == -1) {
3785 error = got_error_from_errno("asprintf");
3789 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3790 KATTR_ID, "tree_wrapper", KATTR__MAX);
3791 if (kerr != KCGI_OK)
3793 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3794 KATTR_ID, "tree_line", KATTR_CLASS, class,
3796 if (kerr != KCGI_OK)
3798 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3799 KATTR_HREF, href_blob, KATTR_CLASS,
3800 "diff_directory", KATTR__MAX);
3801 if (kerr != KCGI_OK)
3803 kerr = khtml_puts(gw_trans->gw_html_req,
3804 got_tree_entry_get_name(te));
3805 if (kerr != KCGI_OK)
3807 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3808 if (kerr != KCGI_OK)
3810 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3811 if (kerr != KCGI_OK)
3813 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3814 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3816 if (kerr != KCGI_OK)
3818 kerr = khtml_entity(gw_trans->gw_html_req,
3820 if (kerr != KCGI_OK)
3822 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3823 if (kerr != KCGI_OK)
3826 if (asprintf(&href_blob,
3827 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3828 gw_trans->repo_name, "blob", gw_trans->commit,
3829 got_tree_entry_get_name(te),
3830 gw_trans->repo_folder ?
3831 gw_trans->repo_folder : "") == -1) {
3832 error = got_error_from_errno("asprintf");
3835 if (asprintf(&href_blame,
3836 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3837 gw_trans->repo_name, "blame", gw_trans->commit,
3838 got_tree_entry_get_name(te),
3839 gw_trans->repo_folder ?
3840 gw_trans->repo_folder : "") == -1) {
3841 error = got_error_from_errno("asprintf");
3845 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3846 KATTR_ID, "tree_wrapper", KATTR__MAX);
3847 if (kerr != KCGI_OK)
3849 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3850 KATTR_ID, "tree_line", KATTR_CLASS, class,
3852 if (kerr != KCGI_OK)
3854 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3855 KATTR_HREF, href_blob, KATTR__MAX);
3856 if (kerr != KCGI_OK)
3858 kerr = khtml_puts(gw_trans->gw_html_req,
3859 got_tree_entry_get_name(te));
3860 if (kerr != KCGI_OK)
3862 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3863 if (kerr != KCGI_OK)
3865 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3866 if (kerr != KCGI_OK)
3868 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3869 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3871 if (kerr != KCGI_OK)
3874 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3875 KATTR_HREF, href_blob, KATTR__MAX);
3876 if (kerr != KCGI_OK)
3878 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3879 if (kerr != KCGI_OK)
3881 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3882 if (kerr != KCGI_OK)
3885 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3886 if (kerr != KCGI_OK)
3889 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3890 KATTR_HREF, href_blame, KATTR__MAX);
3891 if (kerr != KCGI_OK)
3893 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3894 if (kerr != KCGI_OK)
3897 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3898 if (kerr != KCGI_OK)
3906 build_folder = NULL;
3910 got_object_tree_close(tree);
3912 got_repo_close(repo);
3919 if (error == NULL && kerr != KCGI_OK)
3920 error = gw_kcgi_error(kerr);
3924 static const struct got_error *
3925 gw_output_repo_heads(struct gw_trans *gw_trans)
3927 const struct got_error *error = NULL;
3928 struct got_repository *repo = NULL;
3929 struct got_reflist_head refs;
3930 struct got_reflist_entry *re;
3931 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3932 char *href_commits = NULL;
3933 enum kcgi_err kerr = KCGI_OK;
3935 SIMPLEQ_INIT(&refs);
3937 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3941 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3946 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3947 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3948 if (kerr != KCGI_OK)
3950 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3951 KATTR_ID, "summary_heads_title", KATTR__MAX);
3952 if (kerr != KCGI_OK)
3954 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3955 if (kerr != KCGI_OK)
3957 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3958 if (kerr != KCGI_OK)
3960 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3961 KATTR_ID, "summary_heads_content", KATTR__MAX);
3962 if (kerr != KCGI_OK)
3965 SIMPLEQ_FOREACH(re, &refs, entry) {
3968 refname = strdup(got_ref_get_name(re->ref));
3969 if (refname == NULL) {
3970 error = got_error_from_errno("got_ref_to_str");
3974 if (strncmp(refname, "refs/heads/", 11) != 0) {
3979 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3984 if (strncmp(refname, "refs/heads/", 11) == 0)
3987 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3988 KATTR_ID, "heads_wrapper", KATTR__MAX);
3989 if (kerr != KCGI_OK)
3991 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3992 KATTR_ID, "heads_age", KATTR__MAX);
3993 if (kerr != KCGI_OK)
3995 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
3996 if (kerr != KCGI_OK)
3998 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3999 if (kerr != KCGI_OK)
4001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4002 KATTR_ID, "heads_space", KATTR__MAX);
4003 if (kerr != KCGI_OK)
4005 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4006 if (kerr != KCGI_OK)
4008 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4009 if (kerr != KCGI_OK)
4011 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4012 KATTR_ID, "head", KATTR__MAX);
4013 if (kerr != KCGI_OK)
4015 if (asprintf(&href_summary,
4016 "?path=%s&action=summary&headref=%s",
4017 gw_trans->repo_name, refname) == -1) {
4018 error = got_error_from_errno("asprintf");
4021 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4022 href_summary, KATTR__MAX);
4023 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4024 if (kerr != KCGI_OK)
4026 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4027 if (kerr != KCGI_OK)
4030 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4031 "navs_wrapper", KATTR__MAX);
4032 if (kerr != KCGI_OK)
4034 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4035 "navs", KATTR__MAX);
4036 if (kerr != KCGI_OK)
4039 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4040 href_summary, KATTR__MAX);
4041 if (kerr != KCGI_OK)
4043 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4044 if (kerr != KCGI_OK)
4046 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4047 if (kerr != KCGI_OK)
4050 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4051 if (kerr != KCGI_OK)
4053 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4054 gw_trans->repo_name, refname) == -1) {
4055 error = got_error_from_errno("asprintf");
4058 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4059 href_briefs, KATTR__MAX);
4060 if (kerr != KCGI_OK)
4062 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4063 if (kerr != KCGI_OK)
4065 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4066 if (kerr != KCGI_OK)
4069 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4070 if (kerr != KCGI_OK)
4073 if (asprintf(&href_commits,
4074 "?path=%s&action=commits&headref=%s",
4075 gw_trans->repo_name, refname) == -1) {
4076 error = got_error_from_errno("asprintf");
4079 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4080 href_commits, KATTR__MAX);
4081 if (kerr != KCGI_OK)
4083 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4084 if (kerr != KCGI_OK)
4086 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4087 if (kerr != KCGI_OK)
4090 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4091 "dotted_line", KATTR__MAX);
4092 if (kerr != KCGI_OK)
4094 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4095 if (kerr != KCGI_OK)
4098 href_summary = NULL;
4102 href_commits = NULL;
4105 got_ref_list_free(&refs);
4110 got_repo_close(repo);
4114 static const struct got_error *
4115 gw_output_site_link(struct gw_trans *gw_trans)
4117 const struct got_error *error = NULL;
4118 char *href_summary = NULL;
4119 enum kcgi_err kerr = KCGI_OK;
4121 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4122 "site_link", KATTR__MAX);
4123 if (kerr != KCGI_OK)
4125 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4127 if (kerr != KCGI_OK)
4129 kerr = khtml_puts(gw_trans->gw_html_req,
4130 gw_trans->gw_conf->got_site_link);
4131 if (kerr != KCGI_OK)
4133 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4134 if (kerr != KCGI_OK)
4137 if (gw_trans->repo_name != NULL) {
4138 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4139 if (kerr != KCGI_OK)
4141 if (asprintf(&href_summary, "?path=%s&action=summary",
4142 gw_trans->repo_name) == -1)
4144 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4145 href_summary, KATTR__MAX);
4146 if (kerr != KCGI_OK)
4148 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4149 if (kerr != KCGI_OK)
4151 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4152 if (kerr != KCGI_OK)
4154 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4155 if (kerr != KCGI_OK)
4157 kerr = khtml_puts(gw_trans->gw_html_req,
4158 gw_get_action_name(gw_trans));
4159 if (kerr != KCGI_OK)
4163 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4164 if (kerr != KCGI_OK)
4171 static const struct got_error *
4172 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4174 const struct got_error *error = NULL;
4176 enum kcgi_err kerr = KCGI_OK;
4178 if (strncmp(buf, "-", 1) == 0)
4179 color = "diff_minus";
4180 else if (strncmp(buf, "+", 1) == 0)
4181 color = "diff_plus";
4182 else if (strncmp(buf, "@@", 2) == 0)
4183 color = "diff_chunk_header";
4184 else if (strncmp(buf, "@@", 2) == 0)
4185 color = "diff_chunk_header";
4186 else if (strncmp(buf, "commit +", 8) == 0)
4187 color = "diff_meta";
4188 else if (strncmp(buf, "commit -", 8) == 0)
4189 color = "diff_meta";
4190 else if (strncmp(buf, "blob +", 6) == 0)
4191 color = "diff_meta";
4192 else if (strncmp(buf, "blob -", 6) == 0)
4193 color = "diff_meta";
4194 else if (strncmp(buf, "file +", 6) == 0)
4195 color = "diff_meta";
4196 else if (strncmp(buf, "file -", 6) == 0)
4197 color = "diff_meta";
4198 else if (strncmp(buf, "from:", 5) == 0)
4199 color = "diff_author";
4200 else if (strncmp(buf, "via:", 4) == 0)
4201 color = "diff_author";
4202 else if (strncmp(buf, "date:", 5) == 0)
4203 color = "diff_date";
4204 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4205 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4206 if (error == NULL && kerr != KCGI_OK)
4207 error = gw_kcgi_error(kerr);
4212 main(int argc, char *argv[])
4214 const struct got_error *error = NULL;
4215 struct gw_trans *gw_trans;
4216 struct gw_dir *dir = NULL, *tdir;
4217 const char *page = "index";
4221 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4224 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4227 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4230 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4233 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4234 if (kerr != KCGI_OK) {
4235 error = gw_kcgi_error(kerr);
4239 if ((gw_trans->gw_conf =
4240 malloc(sizeof(struct gotweb_conf))) == NULL) {
4242 error = got_error_from_errno("malloc");
4246 TAILQ_INIT(&gw_trans->gw_dirs);
4247 TAILQ_INIT(&gw_trans->gw_headers);
4249 gw_trans->action = -1;
4251 gw_trans->repos_total = 0;
4252 gw_trans->repo_path = NULL;
4253 gw_trans->commit = NULL;
4254 gw_trans->headref = strdup(GOT_REF_HEAD);
4255 if (gw_trans->headref == NULL) {
4256 error = got_error_from_errno("strdup");
4259 gw_trans->mime = KMIME_TEXT_HTML;
4260 gw_trans->gw_tmpl->key = gw_templs;
4261 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4262 gw_trans->gw_tmpl->arg = gw_trans;
4263 gw_trans->gw_tmpl->cb = gw_template;
4264 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4268 error = gw_parse_querystring(gw_trans);
4272 if (gw_trans->action == GW_BLOB)
4273 error = gw_blob(gw_trans);
4275 error = gw_display_index(gw_trans);
4278 free(gw_trans->gw_conf->got_repos_path);
4279 free(gw_trans->gw_conf->got_site_name);
4280 free(gw_trans->gw_conf->got_site_owner);
4281 free(gw_trans->gw_conf->got_site_link);
4282 free(gw_trans->gw_conf->got_logo);
4283 free(gw_trans->gw_conf->got_logo_url);
4284 free(gw_trans->gw_conf);
4285 free(gw_trans->commit);
4286 free(gw_trans->repo_path);
4287 free(gw_trans->headref);
4289 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4291 free(dir->description);
4300 khttp_free(gw_trans->gw_req);