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_header(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 *, const 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_header(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_header(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_header(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_header(header);
1017 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1018 gw_free_header(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_header(header);
1175 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1176 gw_free_header(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,
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 if (gw_trans->headref) {
1325 kerr = khtml_puts(gw_trans->gw_html_req, " (");
1326 if (kerr != KCGI_OK)
1328 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->headref);
1329 if (kerr != KCGI_OK)
1331 kerr = khtml_puts(gw_trans->gw_html_req, ")");
1332 if (kerr != KCGI_OK)
1335 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1336 if (kerr != KCGI_OK)
1338 error = gw_briefs(gw_trans);
1342 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1347 error = gw_output_repo_heads(gw_trans);
1350 if (error == NULL && kerr != KCGI_OK)
1351 error = gw_kcgi_error(kerr);
1355 static const struct got_error *
1356 gw_tree(struct gw_trans *gw_trans)
1358 const struct got_error *error = NULL;
1359 struct gw_header *header = NULL;
1360 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1364 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1365 return got_error_from_errno("pledge");
1367 if ((header = gw_init_header()) == NULL)
1368 return got_error_from_errno("malloc");
1370 error = gw_apply_unveil(gw_trans->gw_dir->path);
1374 error = gw_get_header(gw_trans, header, 1);
1378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1379 "tree_header_wrapper", KATTR__MAX);
1380 if (kerr != KCGI_OK)
1382 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1383 "tree_header", KATTR__MAX);
1384 if (kerr != KCGI_OK)
1386 error = gw_gen_tree_header(gw_trans, header->tree_id);
1389 error = gw_get_time_str(&age, header->committer_time,
1393 error = gw_gen_age_header(gw_trans, age ?age : "");
1396 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1399 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1400 if (kerr != KCGI_OK)
1402 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1403 "dotted_line", KATTR__MAX);
1404 if (kerr != KCGI_OK)
1406 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1407 if (kerr != KCGI_OK)
1410 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1411 "tree", KATTR__MAX);
1412 if (kerr != KCGI_OK)
1414 error = gw_output_repo_tree(gw_trans);
1418 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1420 got_ref_list_free(&header->refs);
1421 gw_free_header(header);
1422 free(tree_html_disp);
1426 if (error == NULL && kerr != KCGI_OK)
1427 error = gw_kcgi_error(kerr);
1431 static const struct got_error *
1432 gw_tag(struct gw_trans *gw_trans)
1434 const struct got_error *error = NULL;
1435 struct gw_header *header = NULL;
1438 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1439 return got_error_from_errno("pledge");
1441 if ((header = gw_init_header()) == NULL)
1442 return got_error_from_errno("malloc");
1444 error = gw_apply_unveil(gw_trans->gw_dir->path);
1448 error = gw_get_header(gw_trans, header, 1);
1452 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1453 "tag_header_wrapper", KATTR__MAX);
1454 if (kerr != KCGI_OK)
1456 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1457 "tag_header", KATTR__MAX);
1458 if (kerr != KCGI_OK)
1460 error = gw_gen_commit_header(gw_trans, header->commit_id,
1464 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1467 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1468 if (kerr != KCGI_OK)
1470 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1471 "dotted_line", KATTR__MAX);
1472 if (kerr != KCGI_OK)
1474 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1475 if (kerr != KCGI_OK)
1478 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1479 "tree", KATTR__MAX);
1480 if (kerr != KCGI_OK)
1483 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1487 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1489 got_ref_list_free(&header->refs);
1490 gw_free_header(header);
1491 if (error == NULL && kerr != KCGI_OK)
1492 error = gw_kcgi_error(kerr);
1496 static const struct got_error *
1497 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1499 const struct got_error *error = NULL;
1504 if (asprintf(&dir_test, "%s/%s/%s",
1505 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1506 GOTWEB_GIT_DIR) == -1)
1507 return got_error_from_errno("asprintf");
1509 dt = opendir(dir_test);
1513 gw_dir->path = strdup(dir_test);
1514 if (gw_dir->path == NULL) {
1516 error = got_error_from_errno("strdup");
1523 if (asprintf(&dir_test, "%s/%s/%s",
1524 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1525 GOTWEB_GOT_DIR) == -1) {
1527 error = got_error_from_errno("asprintf");
1531 dt = opendir(dir_test);
1536 error = got_error(GOT_ERR_NOT_GIT_REPO);
1540 if (asprintf(&dir_test, "%s/%s",
1541 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1542 error = got_error_from_errno("asprintf");
1547 gw_dir->path = strdup(dir_test);
1548 if (gw_dir->path == NULL) {
1550 error = got_error_from_errno("strdup");
1554 dt = opendir(dir_test);
1556 error = got_error_from_errno2("bad path", dir_test);
1561 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1565 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1568 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1572 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1580 static const struct got_error *
1581 gw_load_got_paths(struct gw_trans *gw_trans)
1583 const struct got_error *error = NULL;
1585 struct dirent **sd_dent;
1586 struct gw_dir *gw_dir;
1588 unsigned int d_cnt, d_i;
1590 d = opendir(gw_trans->gw_conf->got_repos_path);
1592 error = got_error_from_errno2("opendir",
1593 gw_trans->gw_conf->got_repos_path);
1597 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1600 error = got_error_from_errno2("scandir",
1601 gw_trans->gw_conf->got_repos_path);
1605 for (d_i = 0; d_i < d_cnt; d_i++) {
1606 if (gw_trans->gw_conf->got_max_repos > 0 &&
1607 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1608 break; /* account for parent and self */
1610 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1611 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1614 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1618 error = gw_load_got_path(gw_trans, gw_dir);
1619 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1626 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1627 !got_path_dir_is_empty(gw_dir->path)) {
1628 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1630 gw_trans->repos_total++;
1638 static const struct got_error *
1639 gw_parse_querystring(struct gw_trans *gw_trans)
1641 const struct got_error *error = NULL;
1643 struct gw_query_action *action = NULL;
1646 if (gw_trans->gw_req->fieldnmap[0]) {
1647 error = got_error_from_errno("bad parse");
1649 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1650 /* define gw_trans->repo_path */
1651 gw_trans->repo_name = p->parsed.s;
1653 if (asprintf(&gw_trans->repo_path, "%s/%s",
1654 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1655 return got_error_from_errno("asprintf");
1657 /* get action and set function */
1658 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1659 for (i = 0; i < nitems(gw_query_funcs); i++) {
1660 action = &gw_query_funcs[i];
1661 if (action->func_name == NULL)
1663 if (strcmp(action->func_name,
1664 p->parsed.s) == 0) {
1665 gw_trans->action = i;
1670 if (gw_trans->action == -1) {
1671 gw_trans->action = GW_ERR;
1672 gw_trans->error = got_error_from_errno("bad action");
1676 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1677 if (asprintf(&gw_trans->commit, "%s",
1679 return got_error_from_errno("asprintf");
1682 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1683 gw_trans->repo_file = p->parsed.s;
1685 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1686 if (asprintf(&gw_trans->repo_folder, "%s",
1688 return got_error_from_errno("asprintf");
1691 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1692 gw_trans->headref = p->parsed.s;
1694 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1698 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1700 gw_trans->action = GW_INDEX;
1702 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1703 gw_trans->page = p->parsed.i;
1708 static const struct got_error *
1709 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1711 const struct got_error *error;
1713 *gw_dir = malloc(sizeof(**gw_dir));
1714 if (*gw_dir == NULL)
1715 return got_error_from_errno("malloc");
1717 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1718 error = got_error_from_errno("asprintf");
1727 static const struct got_error *
1728 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1732 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1733 if (kerr != KCGI_OK)
1734 return gw_kcgi_error(kerr);
1735 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1737 if (kerr != KCGI_OK)
1738 return gw_kcgi_error(kerr);
1739 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1741 if (kerr != KCGI_OK)
1742 return gw_kcgi_error(kerr);
1743 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1745 if (kerr != KCGI_OK)
1746 return gw_kcgi_error(kerr);
1747 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1748 if (kerr != KCGI_OK)
1749 return gw_kcgi_error(kerr);
1750 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1752 if (kerr != KCGI_OK)
1753 return gw_kcgi_error(kerr);
1755 /* XXX repo_file could be NULL if not present in querystring */
1756 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1757 kerr = khttp_head(gw_trans->gw_req,
1758 kresps[KRESP_CONTENT_DISPOSITION],
1759 "attachment; filename=%s", gw_trans->repo_file);
1760 if (kerr != KCGI_OK)
1761 return gw_kcgi_error(kerr);
1764 kerr = khttp_body(gw_trans->gw_req);
1765 return gw_kcgi_error(kerr);
1768 static const struct got_error *
1769 gw_display_index(struct gw_trans *gw_trans)
1771 const struct got_error *error;
1774 /* catch early querystring errors */
1775 if (gw_trans->error)
1776 gw_trans->action = GW_ERR;
1778 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1782 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1783 if (kerr != KCGI_OK)
1784 return gw_kcgi_error(kerr);
1786 if (gw_trans->action != GW_BLOB) {
1787 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1788 gw_query_funcs[gw_trans->action].template);
1789 if (kerr != KCGI_OK) {
1790 khtml_close(gw_trans->gw_html_req);
1791 return gw_kcgi_error(kerr);
1795 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1798 static const struct got_error *
1799 gw_error(struct gw_trans *gw_trans)
1803 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
1805 return gw_kcgi_error(kerr);
1809 gw_template(size_t key, void *arg)
1811 const struct got_error *error = NULL;
1813 struct gw_trans *gw_trans = arg;
1814 char *img_src = NULL;
1818 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1819 KATTR_NAME, "viewport",
1820 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1822 if (kerr != KCGI_OK)
1824 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1825 if (kerr != KCGI_OK)
1827 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1828 KATTR_CHARSET, "utf-8",
1830 if (kerr != KCGI_OK)
1832 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1833 if (kerr != KCGI_OK)
1835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1836 KATTR_NAME, "msapplication-TileColor",
1837 KATTR_CONTENT, "#da532c", KATTR__MAX);
1838 if (kerr != KCGI_OK)
1840 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1841 if (kerr != KCGI_OK)
1843 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1844 KATTR_NAME, "theme-color",
1845 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1846 if (kerr != KCGI_OK)
1848 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1849 if (kerr != KCGI_OK)
1851 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1852 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1853 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1854 if (kerr != KCGI_OK)
1856 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1857 if (kerr != KCGI_OK)
1859 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1860 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1861 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1862 if (kerr != KCGI_OK)
1864 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1865 if (kerr != KCGI_OK)
1867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1868 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1869 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1870 if (kerr != KCGI_OK)
1872 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1873 if (kerr != KCGI_OK)
1875 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1876 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1878 if (kerr != KCGI_OK)
1880 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1881 if (kerr != KCGI_OK)
1883 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1884 KATTR_REL, "mask-icon", KATTR_HREF,
1885 "/safari-pinned-tab.svg", KATTR__MAX);
1886 if (kerr != KCGI_OK)
1888 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1889 if (kerr != KCGI_OK)
1891 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1892 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1893 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1894 if (kerr != KCGI_OK)
1896 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1897 if (kerr != KCGI_OK)
1901 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1902 KATTR_ID, "got_link", KATTR__MAX);
1903 if (kerr != KCGI_OK)
1905 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1906 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1907 KATTR_TARGET, "_sotd", KATTR__MAX);
1908 if (kerr != KCGI_OK)
1910 if (asprintf(&img_src, "/%s",
1911 gw_trans->gw_conf->got_logo) == -1)
1913 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1914 KATTR_SRC, img_src, KATTR__MAX);
1915 if (kerr != KCGI_OK) {
1919 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1920 if (kerr != KCGI_OK) {
1925 case (TEMPL_SITEPATH):
1926 error = gw_output_site_link(gw_trans);
1931 if (gw_trans->gw_conf->got_site_name != NULL) {
1932 kerr = khtml_puts(gw_trans->gw_html_req,
1933 gw_trans->gw_conf->got_site_name);
1934 if (kerr != KCGI_OK)
1938 case (TEMPL_SEARCH):
1940 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1941 "search", KATTR__MAX);
1942 if (kerr != KCGI_OK)
1944 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1945 KATTR_METHOD, "POST", KATTR__MAX);
1946 if (kerr != KCGI_OK)
1948 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1949 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1950 KATTR_MAXLENGTH, "50", KATTR__MAX);
1951 if (kerr != KCGI_OK)
1953 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1955 if (kerr != KCGI_OK)
1957 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1958 if (kerr != KCGI_OK)
1960 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1961 if (kerr != KCGI_OK)
1964 case(TEMPL_SITEOWNER):
1965 if (gw_trans->gw_conf->got_site_owner != NULL &&
1966 gw_trans->gw_conf->got_show_site_owner) {
1967 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1968 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1969 if (kerr != KCGI_OK)
1971 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1972 KATTR_ID, "site_owner", KATTR__MAX);
1973 if (kerr != KCGI_OK)
1975 kerr = khtml_puts(gw_trans->gw_html_req,
1976 gw_trans->gw_conf->got_site_owner);
1977 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1978 if (kerr != KCGI_OK)
1982 case(TEMPL_CONTENT):
1983 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1985 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1986 KATTR_ID, "tmpl_err", KATTR__MAX);
1987 if (kerr != KCGI_OK)
1989 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1990 if (kerr != KCGI_OK)
1992 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1993 if (kerr != KCGI_OK)
1995 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1996 if (kerr != KCGI_OK)
2006 static const struct got_error *
2007 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2009 const struct got_error *error = NULL;
2010 enum kcgi_err kerr = KCGI_OK;
2012 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2013 KATTR_ID, "header_commit_title", KATTR__MAX);
2014 if (kerr != KCGI_OK)
2016 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2017 if (kerr != KCGI_OK)
2019 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2020 if (kerr != KCGI_OK)
2022 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2023 KATTR_ID, "header_commit", KATTR__MAX);
2024 if (kerr != KCGI_OK)
2026 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2027 if (kerr != KCGI_OK)
2029 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2030 if (kerr != KCGI_OK)
2033 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2034 if (kerr != KCGI_OK)
2036 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2037 if (kerr != KCGI_OK)
2039 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2040 if (kerr != KCGI_OK)
2043 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2045 if (error == NULL && kerr != KCGI_OK)
2046 error = gw_kcgi_error(kerr);
2050 static const struct got_error *
2051 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2053 const struct got_error *error = NULL;
2054 enum kcgi_err kerr = KCGI_OK;
2056 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2057 KATTR_ID, "header_diff_title", KATTR__MAX);
2058 if (kerr != KCGI_OK)
2060 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2061 if (kerr != KCGI_OK)
2063 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2064 if (kerr != KCGI_OK)
2066 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2067 KATTR_ID, "header_diff", KATTR__MAX);
2068 if (kerr != KCGI_OK)
2071 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2072 if (kerr != KCGI_OK)
2075 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2076 if (kerr != KCGI_OK)
2078 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2079 if (kerr != KCGI_OK)
2081 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2083 if (error == NULL && kerr != KCGI_OK)
2084 error = gw_kcgi_error(kerr);
2088 static const struct got_error *
2089 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2091 const struct got_error *error = NULL;
2092 enum kcgi_err kerr = KCGI_OK;
2094 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2095 KATTR_ID, "header_age_title", KATTR__MAX);
2096 if (kerr != KCGI_OK)
2098 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2099 if (kerr != KCGI_OK)
2101 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2102 if (kerr != KCGI_OK)
2104 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2105 KATTR_ID, "header_age", KATTR__MAX);
2106 if (kerr != KCGI_OK)
2108 kerr = khtml_puts(gw_trans->gw_html_req, str);
2109 if (kerr != KCGI_OK)
2111 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2113 if (error == NULL && kerr != KCGI_OK)
2114 error = gw_kcgi_error(kerr);
2118 static const struct got_error *
2119 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2121 const struct got_error *error = NULL;
2124 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2125 KATTR_ID, "header_author_title", KATTR__MAX);
2126 if (kerr != KCGI_OK)
2128 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2129 if (kerr != KCGI_OK)
2131 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2132 if (kerr != KCGI_OK)
2134 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2135 KATTR_ID, "header_author", KATTR__MAX);
2136 if (kerr != KCGI_OK)
2138 kerr = khtml_puts(gw_trans->gw_html_req, str);
2139 if (kerr != KCGI_OK)
2141 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2143 if (error == NULL && kerr != KCGI_OK)
2144 error = gw_kcgi_error(kerr);
2148 static const struct got_error *
2149 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2151 const struct got_error *error = NULL;
2154 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2155 KATTR_ID, "header_committer_title", KATTR__MAX);
2156 if (kerr != KCGI_OK)
2158 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2159 if (kerr != KCGI_OK)
2161 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2162 if (kerr != KCGI_OK)
2164 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2165 KATTR_ID, "header_committer", KATTR__MAX);
2166 if (kerr != KCGI_OK)
2168 kerr = khtml_puts(gw_trans->gw_html_req, str);
2169 if (kerr != KCGI_OK)
2171 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2173 if (error == NULL && kerr != KCGI_OK)
2174 error = gw_kcgi_error(kerr);
2178 static const struct got_error *
2179 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2181 const struct got_error *error = NULL;
2184 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2185 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2186 if (kerr != KCGI_OK)
2188 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2189 if (kerr != KCGI_OK)
2191 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2192 if (kerr != KCGI_OK)
2194 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2195 KATTR_ID, "header_commit_msg", KATTR__MAX);
2196 if (kerr != KCGI_OK)
2198 kerr = khttp_puts(gw_trans->gw_req, str);
2199 if (kerr != KCGI_OK)
2201 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2203 if (error == NULL && kerr != KCGI_OK)
2204 error = gw_kcgi_error(kerr);
2208 static const struct got_error *
2209 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2211 const struct got_error *error = NULL;
2214 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2215 KATTR_ID, "header_tree_title", KATTR__MAX);
2216 if (kerr != KCGI_OK)
2218 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2219 if (kerr != KCGI_OK)
2221 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2222 if (kerr != KCGI_OK)
2224 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2225 KATTR_ID, "header_tree", KATTR__MAX);
2226 if (kerr != KCGI_OK)
2228 kerr = khtml_puts(gw_trans->gw_html_req, str);
2229 if (kerr != KCGI_OK)
2231 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2233 if (error == NULL && kerr != KCGI_OK)
2234 error = gw_kcgi_error(kerr);
2238 static const struct got_error *
2239 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2242 const struct got_error *error = NULL;
2244 char *d_file = NULL;
2248 *description = NULL;
2249 if (gw_trans->gw_conf->got_show_repo_description == 0)
2252 if (asprintf(&d_file, "%s/description", dir) == -1)
2253 return got_error_from_errno("asprintf");
2255 f = fopen(d_file, "r");
2257 if (errno == ENOENT || errno == EACCES)
2259 error = got_error_from_errno2("fopen", d_file);
2263 if (fseek(f, 0, SEEK_END) == -1) {
2264 error = got_ferror(f, GOT_ERR_IO);
2269 error = got_ferror(f, GOT_ERR_IO);
2272 if (fseek(f, 0, SEEK_SET) == -1) {
2273 error = got_ferror(f, GOT_ERR_IO);
2276 *description = calloc(len + 1, sizeof(**description));
2277 if (*description == NULL) {
2278 error = got_error_from_errno("calloc");
2282 n = fread(*description, 1, len, f);
2283 if (n == 0 && ferror(f))
2284 error = got_ferror(f, GOT_ERR_IO);
2286 if (f != NULL && fclose(f) == -1 && error == NULL)
2287 error = got_error_from_errno("fclose");
2292 static const struct got_error *
2293 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2297 char *years = "years ago", *months = "months ago";
2298 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2299 char *minutes = "minutes ago", *seconds = "seconds ago";
2300 char *now = "right now";
2308 diff_time = time(NULL) - committer_time;
2309 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2310 if (asprintf(repo_age, "%lld %s",
2311 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2312 return got_error_from_errno("asprintf");
2313 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2314 if (asprintf(repo_age, "%lld %s",
2315 (diff_time / 60 / 60 / 24 / (365 / 12)),
2317 return got_error_from_errno("asprintf");
2318 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2319 if (asprintf(repo_age, "%lld %s",
2320 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2321 return got_error_from_errno("asprintf");
2322 } else if (diff_time > 60 * 60 * 24 * 2) {
2323 if (asprintf(repo_age, "%lld %s",
2324 (diff_time / 60 / 60 / 24), days) == -1)
2325 return got_error_from_errno("asprintf");
2326 } else if (diff_time > 60 * 60 * 2) {
2327 if (asprintf(repo_age, "%lld %s",
2328 (diff_time / 60 / 60), hours) == -1)
2329 return got_error_from_errno("asprintf");
2330 } else if (diff_time > 60 * 2) {
2331 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2333 return got_error_from_errno("asprintf");
2334 } else if (diff_time > 2) {
2335 if (asprintf(repo_age, "%lld %s", diff_time,
2337 return got_error_from_errno("asprintf");
2339 if (asprintf(repo_age, "%s", now) == -1)
2340 return got_error_from_errno("asprintf");
2344 if (gmtime_r(&committer_time, &tm) == NULL)
2345 return got_error_from_errno("gmtime_r");
2347 s = asctime_r(&tm, datebuf);
2349 return got_error_from_errno("asctime_r");
2351 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2352 return got_error_from_errno("asprintf");
2358 static const struct got_error *
2359 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2360 const char *refname, int ref_tm)
2362 const struct got_error *error = NULL;
2363 struct got_repository *repo = NULL;
2364 struct got_commit_object *commit = NULL;
2365 struct got_reflist_head refs;
2366 struct got_reflist_entry *re;
2367 time_t committer_time = 0, cmp_time = 0;
2370 SIMPLEQ_INIT(&refs);
2372 if (gw_trans->gw_conf->got_show_repo_age == 0)
2375 error = got_repo_open(&repo, dir, NULL);
2379 error = got_ref_list(&refs, repo, "refs/heads",
2380 got_ref_cmp_by_name, NULL);
2385 * Find the youngest branch tip in the repository, or the age of
2386 * the a specific branch tip if a name was provided by the caller.
2388 SIMPLEQ_FOREACH(re, &refs, entry) {
2389 struct got_object_id *id = NULL;
2391 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2394 error = got_ref_resolve(&id, repo, re->ref);
2398 error = got_object_open_as_commit(&commit, repo, id);
2404 got_object_commit_get_committer_time(commit);
2405 got_object_commit_close(commit);
2406 if (cmp_time < committer_time)
2407 cmp_time = committer_time;
2413 if (cmp_time != 0) {
2414 committer_time = cmp_time;
2415 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2418 got_ref_list_free(&refs);
2419 got_repo_close(repo);
2423 static const struct got_error *
2424 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2426 const struct got_error *error;
2428 struct got_object_id *id1 = NULL, *id2 = NULL;
2429 char *label1 = NULL, *label2 = NULL, *line = NULL;
2431 size_t linesize = 0;
2433 enum kcgi_err kerr = KCGI_OK;
2439 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2443 if (header->parent_id != NULL &&
2444 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2445 error = got_repo_match_object_id(&id1, &label1,
2446 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2451 error = got_repo_match_object_id(&id2, &label2,
2452 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2456 error = got_object_get_type(&obj_type, header->repo, id2);
2460 case GOT_OBJ_TYPE_BLOB:
2461 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2464 case GOT_OBJ_TYPE_TREE:
2465 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2468 case GOT_OBJ_TYPE_COMMIT:
2469 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2473 error = got_error(GOT_ERR_OBJ_TYPE);
2478 if (fseek(f, 0, SEEK_SET) == -1) {
2479 error = got_ferror(f, GOT_ERR_IO);
2483 while ((linelen = getline(&line, &linesize, f)) != -1) {
2484 error = gw_colordiff_line(gw_trans, line);
2487 /* XXX: KHTML_PRETTY breaks this */
2488 kerr = khtml_puts(gw_trans->gw_html_req, line);
2489 if (kerr != KCGI_OK)
2491 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2492 if (kerr != KCGI_OK)
2495 if (linelen == -1 && ferror(f))
2496 error = got_error_from_errno("getline");
2498 if (f && fclose(f) == -1 && error == NULL)
2499 error = got_error_from_errno("fclose");
2506 if (error == NULL && kerr != KCGI_OK)
2507 error = gw_kcgi_error(kerr);
2511 static const struct got_error *
2512 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2514 const struct got_error *error = NULL;
2515 struct got_repository *repo;
2516 const char *gitconfig_owner;
2520 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2523 error = got_repo_open(&repo, dir, NULL);
2526 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2527 if (gitconfig_owner) {
2528 *owner = strdup(gitconfig_owner);
2530 error = got_error_from_errno("strdup");
2532 got_repo_close(repo);
2536 static const struct got_error *
2537 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2539 const struct got_error *error = NULL;
2541 char *d_file = NULL;
2547 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2548 return got_error_from_errno("asprintf");
2550 f = fopen(d_file, "r");
2552 if (errno != ENOENT && errno != EACCES)
2553 error = got_error_from_errno2("fopen", d_file);
2557 if (fseek(f, 0, SEEK_END) == -1) {
2558 error = got_ferror(f, GOT_ERR_IO);
2563 error = got_ferror(f, GOT_ERR_IO);
2566 if (fseek(f, 0, SEEK_SET) == -1) {
2567 error = got_ferror(f, GOT_ERR_IO);
2571 *url = calloc(len + 1, sizeof(**url));
2573 error = got_error_from_errno("calloc");
2577 n = fread(*url, 1, len, f);
2578 if (n == 0 && ferror(f))
2579 error = got_ferror(f, GOT_ERR_IO);
2581 if (f && fclose(f) == -1 && error == NULL)
2582 error = got_error_from_errno("fclose");
2587 static const struct got_error *
2588 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2589 int limit, int tag_type)
2591 const struct got_error *error = NULL;
2592 struct got_repository *repo = NULL;
2593 struct got_reflist_head refs;
2594 struct got_reflist_entry *re;
2596 char *id_str = NULL, *newline, *href_commits = NULL;
2597 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2598 struct got_tag_object *tag = NULL;
2599 enum kcgi_err kerr = KCGI_OK;
2600 int summary_header_displayed = 0;
2602 SIMPLEQ_INIT(&refs);
2604 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2608 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2612 SIMPLEQ_FOREACH(re, &refs, entry) {
2613 const char *refname;
2615 const char *tag_commit;
2617 struct got_object_id *id;
2618 struct got_commit_object *commit = NULL;
2620 refname = got_ref_get_name(re->ref);
2621 if (strncmp(refname, "refs/tags/", 10) != 0)
2625 error = got_ref_resolve(&id, repo, re->ref);
2629 error = got_object_open_as_tag(&tag, repo, id);
2631 if (error->code != GOT_ERR_OBJ_TYPE) {
2635 /* "lightweight" tag */
2636 error = got_object_open_as_commit(&commit, repo, id);
2641 tagger = got_object_commit_get_committer(commit);
2643 got_object_commit_get_committer_time(commit);
2644 error = got_object_id_str(&id_str, id);
2648 tagger = got_object_tag_get_tagger(tag);
2649 tagger_time = got_object_tag_get_tagger_time(tag);
2650 error = got_object_id_str(&id_str,
2651 got_object_tag_get_object_id(tag));
2656 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2657 strlen(id_str)) != 0)
2661 error = got_object_commit_get_logmsg(&tag_commit0,
2665 got_object_commit_close(commit);
2667 tag_commit0 = strdup(got_object_tag_get_message(tag));
2668 if (tag_commit0 == NULL) {
2669 error = got_error_from_errno("strdup");
2674 tag_commit = tag_commit0;
2675 while (*tag_commit == '\n')
2680 newline = strchr(tag_commit, '\n');
2684 if (summary_header_displayed == 0) {
2685 kerr = khtml_attr(gw_trans->gw_html_req,
2686 KELEM_DIV, KATTR_ID,
2687 "summary_tags_title_wrapper", KATTR__MAX);
2688 if (kerr != KCGI_OK)
2690 kerr = khtml_attr(gw_trans->gw_html_req,
2691 KELEM_DIV, KATTR_ID,
2692 "summary_tags_title", KATTR__MAX);
2693 if (kerr != KCGI_OK)
2695 kerr = khtml_puts(gw_trans->gw_html_req,
2697 if (kerr != KCGI_OK)
2699 kerr = khtml_closeelem(gw_trans->gw_html_req,
2701 if (kerr != KCGI_OK)
2703 kerr = khtml_attr(gw_trans->gw_html_req,
2704 KELEM_DIV, KATTR_ID,
2705 "summary_tags_content", KATTR__MAX);
2706 if (kerr != KCGI_OK)
2708 summary_header_displayed = 1;
2711 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2712 KATTR_ID, "tags_wrapper", KATTR__MAX);
2713 if (kerr != KCGI_OK)
2715 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2716 KATTR_ID, "tags_age", KATTR__MAX);
2717 if (kerr != KCGI_OK)
2719 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2722 kerr = khtml_puts(gw_trans->gw_html_req,
2724 if (kerr != KCGI_OK)
2726 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2727 if (kerr != KCGI_OK)
2729 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2730 KATTR_ID, "tags", KATTR__MAX);
2731 if (kerr != KCGI_OK)
2733 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2734 if (kerr != KCGI_OK)
2736 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2737 if (kerr != KCGI_OK)
2739 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2740 KATTR_ID, "tags_name", KATTR__MAX);
2741 if (kerr != KCGI_OK)
2743 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2744 gw_trans->repo_name, id_str) == -1) {
2745 error = got_error_from_errno("asprintf");
2748 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2749 KATTR_HREF, href_tag, KATTR__MAX);
2750 if (kerr != KCGI_OK)
2752 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2753 if (kerr != KCGI_OK)
2755 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2756 if (kerr != KCGI_OK)
2759 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2760 KATTR_ID, "navs_wrapper", KATTR__MAX);
2761 if (kerr != KCGI_OK)
2763 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2764 KATTR_ID, "navs", KATTR__MAX);
2765 if (kerr != KCGI_OK)
2768 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2769 KATTR_HREF, href_tag, KATTR__MAX);
2770 if (kerr != KCGI_OK)
2772 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2773 if (kerr != KCGI_OK)
2775 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2776 if (kerr != KCGI_OK)
2779 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2780 if (kerr != KCGI_OK)
2782 if (asprintf(&href_briefs,
2783 "?path=%s&action=briefs&commit=%s",
2784 gw_trans->repo_name, id_str) == -1) {
2785 error = got_error_from_errno("asprintf");
2788 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2789 KATTR_HREF, href_briefs, KATTR__MAX);
2790 if (kerr != KCGI_OK)
2792 kerr = khtml_puts(gw_trans->gw_html_req,
2794 if (kerr != KCGI_OK)
2796 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2797 if (kerr != KCGI_OK)
2800 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2801 if (kerr != KCGI_OK)
2804 if (asprintf(&href_commits,
2805 "?path=%s&action=commits&commit=%s",
2806 gw_trans->repo_name, id_str) == -1) {
2807 error = got_error_from_errno("asprintf");
2810 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2811 KATTR_HREF, href_commits, KATTR__MAX);
2812 if (kerr != KCGI_OK)
2814 kerr = khtml_puts(gw_trans->gw_html_req,
2816 if (kerr != KCGI_OK)
2818 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2819 if (kerr != KCGI_OK)
2822 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2823 KATTR_ID, "dotted_line", KATTR__MAX);
2824 if (kerr != KCGI_OK)
2826 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2827 if (kerr != KCGI_OK)
2831 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2832 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2833 if (kerr != KCGI_OK)
2835 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2836 if (kerr != KCGI_OK)
2838 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2839 if (kerr != KCGI_OK)
2841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2842 KATTR_ID, "tag_info_date", KATTR__MAX);
2843 if (kerr != KCGI_OK)
2845 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2848 kerr = khtml_puts(gw_trans->gw_html_req,
2850 if (kerr != KCGI_OK)
2852 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2853 if (kerr != KCGI_OK)
2856 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2857 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2858 if (kerr != KCGI_OK)
2860 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2861 if (kerr != KCGI_OK)
2863 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2864 if (kerr != KCGI_OK)
2866 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2867 KATTR_ID, "tag_info_date", KATTR__MAX);
2868 if (kerr != KCGI_OK)
2870 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2871 if (kerr != KCGI_OK)
2873 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2874 if (kerr != KCGI_OK)
2877 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2878 KATTR_ID, "tag_info", KATTR__MAX);
2879 if (kerr != KCGI_OK)
2881 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2882 if (kerr != KCGI_OK)
2888 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2889 if (kerr != KCGI_OK)
2892 if (limit && --limit == 0)
2896 got_object_tag_close(tag);
2909 href_commits = NULL;
2913 got_object_tag_close(tag);
2920 got_ref_list_free(&refs);
2922 got_repo_close(repo);
2923 if (error == NULL && kerr != KCGI_OK)
2924 error = gw_kcgi_error(kerr);
2929 gw_free_header(struct gw_header *header)
2933 if (header->commit != NULL)
2934 got_object_commit_close(header->commit);
2936 got_repo_close(header->repo);
2937 free(header->refs_str);
2938 free(header->commit_id);
2939 free(header->parent_id);
2940 free(header->tree_id);
2941 free(header->commit_msg);
2944 static struct gw_header *
2947 struct gw_header *header;
2949 header = malloc(sizeof(*header));
2953 header->repo = NULL;
2954 header->commit = NULL;
2956 header->path = NULL;
2957 SIMPLEQ_INIT(&header->refs);
2959 header->refs_str = NULL;
2960 header->commit_id = NULL;
2961 header->parent_id = NULL;
2962 header->tree_id = NULL;
2963 header->commit_msg = NULL;
2968 static const struct got_error *
2969 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2972 const struct got_error *error = NULL;
2973 struct got_commit_graph *graph = NULL;
2975 error = got_commit_graph_open(&graph, header->path, 0);
2979 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2986 * XXX This is gross; Some fields of 'header' change during every
2987 * loop iteration, some remain constant (e.g. header->repo).
2988 * We should refactor this to be able to call gw_free_header()
2989 * during every loop iteration. Or perhaps do away with the
2990 * appraoch of passing a struct around which contains data
2991 * of various lifetimes, and instead pass globals like 'repo'
2992 * around separately as done in e.g. tog(1). Any state which
2993 * keeps changing with every iteration (e.g. header->id) would
2994 * better stored in local variables of this function instead.
2996 /* Clear fields that will be filled again by gw_get_commit. */
2997 free(header->refs_str);
2998 header->refs_str = NULL;
2999 free(header->commit_id);
3000 header->commit_id = NULL;
3001 free(header->parent_id);
3002 header->parent_id = NULL;
3003 free(header->tree_id);
3004 header->tree_id = NULL;
3005 free(header->commit_msg);
3006 header->commit_msg = NULL;
3008 free(header->id); /* XXX see above comment */
3009 error = got_commit_graph_iter_next(&header->id, graph,
3010 header->repo, NULL, NULL);
3012 if (error->code == GOT_ERR_ITER_COMPLETED)
3016 if (header->id == NULL)
3019 if (header->commit != NULL) /* XXX see above comment */
3020 got_object_commit_close(header->commit);
3021 error = got_object_open_as_commit(&header->commit, header->repo,
3026 error = gw_get_commit(gw_trans, header);
3028 struct gw_header *n_header = NULL;
3029 if ((n_header = gw_init_header()) == NULL) {
3030 error = got_error_from_errno("malloc");
3034 if (header->refs_str != NULL) {
3035 n_header->refs_str = strdup(header->refs_str);
3036 if (n_header->refs_str == NULL) {
3037 error = got_error_from_errno("strdup");
3041 n_header->commit_id = strdup(header->commit_id);
3042 if (n_header->commit_id == NULL) {
3043 error = got_error_from_errno("strdup");
3046 if (header->parent_id != NULL) {
3047 n_header->parent_id = strdup(header->parent_id);
3048 if (n_header->parent_id == NULL) {
3049 error = got_error_from_errno("strdup");
3053 n_header->tree_id = strdup(header->tree_id);
3054 if (n_header->tree_id == NULL) {
3055 error = got_error_from_errno("strdup");
3058 n_header->author = header->author;
3059 n_header->committer = header->committer;
3060 n_header->commit_msg = strdup(header->commit_msg);
3061 if (n_header->commit_msg == NULL) {
3062 error = got_error_from_errno("strdup");
3065 n_header->committer_time = header->committer_time;
3066 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3069 if (error || (limit && --limit == 0))
3074 got_commit_graph_close(graph);
3078 static const struct got_error *
3079 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3081 const struct got_error *error = NULL;
3082 struct got_reflist_entry *re;
3083 struct got_object_id *id2 = NULL;
3084 struct got_object_qid *parent_id;
3085 char *commit_msg = NULL, *commit_msg0;
3088 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3091 struct got_tag_object *tag = NULL;
3094 if (got_ref_is_symbolic(re->ref))
3097 name = got_ref_get_name(re->ref);
3098 if (strncmp(name, "refs/", 5) == 0)
3100 if (strncmp(name, "got/", 4) == 0)
3102 if (strncmp(name, "heads/", 6) == 0)
3104 if (strncmp(name, "remotes/", 8) == 0)
3106 if (strncmp(name, "tags/", 5) == 0) {
3107 error = got_object_open_as_tag(&tag, header->repo,
3110 if (error->code != GOT_ERR_OBJ_TYPE)
3113 * Ref points at something other
3120 cmp = got_object_id_cmp(tag ?
3121 got_object_tag_get_object_id(tag) : re->id, header->id);
3123 got_object_tag_close(tag);
3126 s = header->refs_str;
3127 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3128 s ? ", " : "", name) == -1) {
3129 error = got_error_from_errno("asprintf");
3131 header->refs_str = NULL;
3137 error = got_object_id_str(&header->commit_id, header->id);
3141 error = got_object_id_str(&header->tree_id,
3142 got_object_commit_get_tree_id(header->commit));
3146 if (gw_trans->action == GW_DIFF) {
3147 parent_id = SIMPLEQ_FIRST(
3148 got_object_commit_get_parent_ids(header->commit));
3149 if (parent_id != NULL) {
3150 id2 = got_object_id_dup(parent_id->id);
3152 error = got_object_id_str(&header->parent_id, id2);
3157 header->parent_id = strdup("/dev/null");
3158 if (header->parent_id == NULL) {
3159 error = got_error_from_errno("strdup");
3165 header->committer_time =
3166 got_object_commit_get_committer_time(header->commit);
3169 got_object_commit_get_author(header->commit);
3171 got_object_commit_get_committer(header->commit);
3174 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3178 commit_msg = commit_msg0;
3179 while (*commit_msg == '\n')
3182 header->commit_msg = strdup(commit_msg);
3183 if (header->commit_msg == NULL)
3184 error = got_error_from_errno("strdup");
3189 static const struct got_error *
3190 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3192 const struct got_error *error = NULL;
3193 char *in_repo_path = NULL;
3195 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3199 if (gw_trans->commit == NULL) {
3200 struct got_reference *head_ref;
3201 error = got_ref_open(&head_ref, header->repo,
3202 gw_trans->headref, 0);
3206 error = got_ref_resolve(&header->id, header->repo, head_ref);
3207 got_ref_close(head_ref);
3211 error = got_object_open_as_commit(&header->commit,
3212 header->repo, header->id);
3216 struct got_reference *ref;
3217 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3218 if (error == NULL) {
3220 error = got_ref_resolve(&header->id, header->repo, ref);
3224 error = got_object_get_type(&obj_type, header->repo,
3228 if (obj_type == GOT_OBJ_TYPE_TAG) {
3229 struct got_tag_object *tag;
3230 error = got_object_open_as_tag(&tag,
3231 header->repo, header->id);
3234 if (got_object_tag_get_object_type(tag) !=
3235 GOT_OBJ_TYPE_COMMIT) {
3236 got_object_tag_close(tag);
3237 error = got_error(GOT_ERR_OBJ_TYPE);
3241 header->id = got_object_id_dup(
3242 got_object_tag_get_object_id(tag));
3243 if (header->id == NULL)
3244 error = got_error_from_errno(
3245 "got_object_id_dup");
3246 got_object_tag_close(tag);
3249 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3250 error = got_error(GOT_ERR_OBJ_TYPE);
3253 error = got_object_open_as_commit(&header->commit,
3254 header->repo, header->id);
3258 if (header->commit == NULL) {
3259 error = got_repo_match_object_id_prefix(&header->id,
3260 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3265 error = got_repo_match_object_id_prefix(&header->id,
3266 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3272 error = got_repo_map_path(&in_repo_path, header->repo,
3273 gw_trans->repo_path, 1);
3278 header->path = strdup(in_repo_path);
3279 if (header->path == NULL) {
3280 error = got_error_from_errno("strdup");
3287 error = got_ref_list(&header->refs, header->repo, NULL,
3288 got_ref_cmp_by_name, NULL);
3292 error = gw_get_commits(gw_trans, header, limit);
3300 char datebuf[11]; /* YYYY-MM-DD + NUL */
3303 struct gw_blame_cb_args {
3304 struct blame_line *lines;
3308 off_t *line_offsets;
3310 struct got_repository *repo;
3311 struct gw_trans *gw_trans;
3314 static const struct got_error *
3315 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3317 const struct got_error *err = NULL;
3318 struct gw_blame_cb_args *a = arg;
3319 struct blame_line *bline;
3321 size_t linesize = 0;
3322 struct got_commit_object *commit = NULL;
3325 time_t committer_time;
3326 enum kcgi_err kerr = KCGI_OK;
3328 if (nlines != a->nlines ||
3329 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3330 return got_error(GOT_ERR_RANGE);
3333 return NULL; /* no change in this commit */
3335 /* Annotate this line. */
3336 bline = &a->lines[lineno - 1];
3337 if (bline->annotated)
3339 err = got_object_id_str(&bline->id_str, id);
3343 err = got_object_open_as_commit(&commit, a->repo, id);
3347 bline->committer = strdup(got_object_commit_get_committer(commit));
3348 if (bline->committer == NULL) {
3349 err = got_error_from_errno("strdup");
3353 committer_time = got_object_commit_get_committer_time(commit);
3354 if (localtime_r(&committer_time, &tm) == NULL)
3355 return got_error_from_errno("localtime_r");
3356 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3357 &tm) >= sizeof(bline->datebuf)) {
3358 err = got_error(GOT_ERR_NO_SPACE);
3361 bline->annotated = 1;
3363 /* Print lines annotated so far. */
3364 bline = &a->lines[a->lineno_cur - 1];
3365 if (!bline->annotated)
3368 offset = a->line_offsets[a->lineno_cur - 1];
3369 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3370 err = got_error_from_errno("fseeko");
3374 while (bline->annotated) {
3375 char *smallerthan, *at, *nl, *committer;
3376 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3379 if (getline(&line, &linesize, a->f) == -1) {
3381 err = got_error_from_errno("getline");
3385 committer = bline->committer;
3386 smallerthan = strchr(committer, '<');
3387 if (smallerthan && smallerthan[1] != '\0')
3388 committer = smallerthan + 1;
3389 at = strchr(committer, '@');
3392 len = strlen(committer);
3394 committer[8] = '\0';
3396 nl = strchr(line, '\n');
3400 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3401 "blame_wrapper", KATTR__MAX);
3402 if (kerr != KCGI_OK)
3404 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3405 "blame_number", KATTR__MAX);
3406 if (kerr != KCGI_OK)
3408 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3409 a->lineno_cur) == -1)
3411 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3412 if (kerr != KCGI_OK)
3414 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3415 if (kerr != KCGI_OK)
3418 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3419 "blame_hash", KATTR__MAX);
3420 if (kerr != KCGI_OK)
3422 /* XXX repo_file could be NULL if not present in querystring */
3423 if (asprintf(&href_diff,
3424 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3425 a->gw_trans->repo_name, bline->id_str,
3426 a->gw_trans->repo_file, a->gw_trans->repo_folder ?
3427 a->gw_trans->repo_folder : "") == -1) {
3428 err = got_error_from_errno("asprintf");
3431 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3432 err = got_error_from_errno("asprintf");
3435 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3436 KATTR_HREF, href_diff, KATTR__MAX);
3437 if (kerr != KCGI_OK)
3439 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3440 if (kerr != KCGI_OK)
3442 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3443 if (kerr != KCGI_OK)
3446 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3447 "blame_date", KATTR__MAX);
3448 if (kerr != KCGI_OK)
3450 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3451 if (kerr != KCGI_OK)
3453 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3454 if (kerr != KCGI_OK)
3457 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3458 "blame_author", KATTR__MAX);
3459 if (kerr != KCGI_OK)
3461 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3462 if (kerr != KCGI_OK)
3464 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3465 if (kerr != KCGI_OK)
3468 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3469 "blame_code", KATTR__MAX);
3470 if (kerr != KCGI_OK)
3472 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3473 if (kerr != KCGI_OK)
3475 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3476 if (kerr != KCGI_OK)
3479 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3480 if (kerr != KCGI_OK)
3484 bline = &a->lines[a->lineno_cur - 1];
3492 got_object_commit_close(commit);
3494 if (err == NULL && kerr != KCGI_OK)
3495 err = gw_kcgi_error(kerr);
3499 static const struct got_error *
3500 gw_output_file_blame(struct gw_trans *gw_trans)
3502 const struct got_error *error = NULL;
3503 struct got_repository *repo = NULL;
3504 struct got_object_id *obj_id = NULL;
3505 struct got_object_id *commit_id = NULL;
3506 struct got_blob_object *blob = NULL;
3507 char *path = NULL, *in_repo_path = NULL;
3508 struct gw_blame_cb_args bca;
3512 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3516 /* XXX repo_file could be NULL if not present in querystring */
3517 if (asprintf(&path, "%s%s%s",
3518 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3519 gw_trans->repo_folder ? "/" : "",
3520 gw_trans->repo_file) == -1) {
3521 error = got_error_from_errno("asprintf");
3525 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3529 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3530 GOT_OBJ_TYPE_COMMIT, 1, repo);
3534 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3538 if (obj_id == NULL) {
3539 error = got_error(GOT_ERR_NO_OBJ);
3543 error = got_object_get_type(&obj_type, repo, obj_id);
3547 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3548 error = got_error(GOT_ERR_OBJ_TYPE);
3552 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3556 bca.f = got_opentemp();
3557 if (bca.f == NULL) {
3558 error = got_error_from_errno("got_opentemp");
3561 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3562 &bca.line_offsets, bca.f, blob);
3563 if (error || bca.nlines == 0)
3566 /* Don't include \n at EOF in the blame line count. */
3567 if (bca.line_offsets[bca.nlines - 1] == filesize)
3570 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3571 if (bca.lines == NULL) {
3572 error = got_error_from_errno("calloc");
3576 bca.nlines_prec = 0;
3583 bca.gw_trans = gw_trans;
3585 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3588 free(bca.line_offsets);
3594 for (i = 0; i < bca.nlines; i++) {
3595 struct blame_line *bline = &bca.lines[i];
3596 free(bline->id_str);
3597 free(bline->committer);
3600 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3601 error = got_error_from_errno("fclose");
3603 got_object_blob_close(blob);
3605 got_repo_close(repo);
3609 static const struct got_error *
3610 gw_output_blob_buf(struct gw_trans *gw_trans)
3612 const struct got_error *error = NULL;
3613 struct got_repository *repo = NULL;
3614 struct got_object_id *obj_id = NULL;
3615 struct got_object_id *commit_id = NULL;
3616 struct got_blob_object *blob = NULL;
3617 char *path = NULL, *in_repo_path = NULL;
3618 int obj_type, set_mime = 0;
3621 enum kcgi_err kerr = KCGI_OK;
3623 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3627 /* XXX repo_file could be NULL if not present in querystring */
3628 if (asprintf(&path, "%s%s%s",
3629 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3630 gw_trans->repo_folder ? "/" : "",
3631 gw_trans->repo_file) == -1) {
3632 error = got_error_from_errno("asprintf");
3636 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3640 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3641 GOT_OBJ_TYPE_COMMIT, 1, repo);
3645 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3649 if (obj_id == NULL) {
3650 error = got_error(GOT_ERR_NO_OBJ);
3654 error = got_object_get_type(&obj_type, repo, obj_id);
3658 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3659 error = got_error(GOT_ERR_OBJ_TYPE);
3663 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3667 hdrlen = got_object_blob_get_hdrlen(blob);
3669 error = got_object_blob_read_block(&len, blob);
3672 buf = got_object_blob_get_read_buf(blob);
3675 * Skip blob object header first time around,
3676 * which also contains a zero byte.
3679 if (set_mime == 0) {
3680 if (isbinary(buf, len - hdrlen))
3681 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3683 gw_trans->mime = KMIME_TEXT_PLAIN;
3685 error = gw_display_index(gw_trans);
3689 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3698 got_object_blob_close(blob);
3700 got_repo_close(repo);
3701 if (error == NULL && kerr != KCGI_OK)
3702 error = gw_kcgi_error(kerr);
3706 static const struct got_error *
3707 gw_output_repo_tree(struct gw_trans *gw_trans)
3709 const struct got_error *error = NULL;
3710 struct got_repository *repo = NULL;
3711 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3712 struct got_tree_object *tree = NULL;
3713 char *path = NULL, *in_repo_path = NULL;
3714 char *id_str = NULL;
3715 char *build_folder = NULL;
3716 char *href_blob = NULL, *href_blame = NULL;
3717 const char *class = NULL;
3718 int nentries, i, class_flip = 0;
3719 enum kcgi_err kerr = KCGI_OK;
3721 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3725 if (gw_trans->repo_folder != NULL) {
3726 path = strdup(gw_trans->repo_folder);
3728 error = got_error_from_errno("strdup");
3732 error = got_repo_map_path(&in_repo_path, repo,
3733 gw_trans->repo_path, 1);
3737 path = in_repo_path;
3740 if (gw_trans->commit == NULL) {
3741 struct got_reference *head_ref;
3742 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3745 error = got_ref_resolve(&commit_id, repo, head_ref);
3748 got_ref_close(head_ref);
3751 error = got_repo_match_object_id(&commit_id, NULL,
3752 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3758 * XXX gw_trans->commit might have already been allocated in
3759 * gw_parse_querystring(); could we more cleanly seperate values
3760 * we received as arguments from values we compute ourselves?
3762 error = got_object_id_str(&gw_trans->commit, commit_id);
3766 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3770 error = got_object_open_as_tree(&tree, repo, tree_id);
3774 nentries = got_object_tree_get_nentries(tree);
3775 for (i = 0; i < nentries; i++) {
3776 struct got_tree_entry *te;
3777 const char *modestr = "";
3780 te = got_object_tree_get_entry(tree, i);
3782 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3786 mode = got_tree_entry_get_mode(te);
3787 if (got_object_tree_entry_is_submodule(te))
3789 else if (S_ISLNK(mode))
3791 else if (S_ISDIR(mode))
3793 else if (mode & S_IXUSR)
3796 if (class_flip == 0) {
3797 class = "back_lightgray";
3800 class = "back_white";
3804 if (S_ISDIR(mode)) {
3805 if (asprintf(&build_folder, "%s/%s",
3806 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3807 got_tree_entry_get_name(te)) == -1) {
3808 error = got_error_from_errno("asprintf");
3811 if (asprintf(&href_blob,
3812 "?path=%s&action=%s&commit=%s&folder=%s",
3813 gw_trans->repo_name, gw_get_action_name(gw_trans),
3814 gw_trans->commit, build_folder) == -1) {
3815 error = got_error_from_errno("asprintf");
3819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3820 KATTR_ID, "tree_wrapper", KATTR__MAX);
3821 if (kerr != KCGI_OK)
3823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3824 KATTR_ID, "tree_line", KATTR_CLASS, class,
3826 if (kerr != KCGI_OK)
3828 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3829 KATTR_HREF, href_blob, KATTR_CLASS,
3830 "diff_directory", KATTR__MAX);
3831 if (kerr != KCGI_OK)
3833 kerr = khtml_puts(gw_trans->gw_html_req,
3834 got_tree_entry_get_name(te));
3835 if (kerr != KCGI_OK)
3837 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3838 if (kerr != KCGI_OK)
3840 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3841 if (kerr != KCGI_OK)
3843 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3844 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3846 if (kerr != KCGI_OK)
3848 kerr = khtml_entity(gw_trans->gw_html_req,
3850 if (kerr != KCGI_OK)
3852 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3853 if (kerr != KCGI_OK)
3856 if (asprintf(&href_blob,
3857 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3858 gw_trans->repo_name, "blob", gw_trans->commit,
3859 got_tree_entry_get_name(te),
3860 gw_trans->repo_folder ?
3861 gw_trans->repo_folder : "") == -1) {
3862 error = got_error_from_errno("asprintf");
3865 if (asprintf(&href_blame,
3866 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3867 gw_trans->repo_name, "blame", gw_trans->commit,
3868 got_tree_entry_get_name(te),
3869 gw_trans->repo_folder ?
3870 gw_trans->repo_folder : "") == -1) {
3871 error = got_error_from_errno("asprintf");
3875 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3876 KATTR_ID, "tree_wrapper", KATTR__MAX);
3877 if (kerr != KCGI_OK)
3879 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3880 KATTR_ID, "tree_line", KATTR_CLASS, class,
3882 if (kerr != KCGI_OK)
3884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3885 KATTR_HREF, href_blob, KATTR__MAX);
3886 if (kerr != KCGI_OK)
3888 kerr = khtml_puts(gw_trans->gw_html_req,
3889 got_tree_entry_get_name(te));
3890 if (kerr != KCGI_OK)
3892 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3893 if (kerr != KCGI_OK)
3895 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3896 if (kerr != KCGI_OK)
3898 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3899 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3901 if (kerr != KCGI_OK)
3904 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3905 KATTR_HREF, href_blob, KATTR__MAX);
3906 if (kerr != KCGI_OK)
3908 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3909 if (kerr != KCGI_OK)
3911 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3912 if (kerr != KCGI_OK)
3915 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3916 if (kerr != KCGI_OK)
3919 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3920 KATTR_HREF, href_blame, KATTR__MAX);
3921 if (kerr != KCGI_OK)
3923 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3924 if (kerr != KCGI_OK)
3927 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3928 if (kerr != KCGI_OK)
3936 build_folder = NULL;
3940 got_object_tree_close(tree);
3942 got_repo_close(repo);
3949 if (error == NULL && kerr != KCGI_OK)
3950 error = gw_kcgi_error(kerr);
3954 static const struct got_error *
3955 gw_output_repo_heads(struct gw_trans *gw_trans)
3957 const struct got_error *error = NULL;
3958 struct got_repository *repo = NULL;
3959 struct got_reflist_head refs;
3960 struct got_reflist_entry *re;
3961 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3962 char *href_commits = NULL;
3963 enum kcgi_err kerr = KCGI_OK;
3965 SIMPLEQ_INIT(&refs);
3967 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3971 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3976 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3977 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3978 if (kerr != KCGI_OK)
3980 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3981 KATTR_ID, "summary_heads_title", KATTR__MAX);
3982 if (kerr != KCGI_OK)
3984 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3985 if (kerr != KCGI_OK)
3987 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3988 if (kerr != KCGI_OK)
3990 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3991 KATTR_ID, "summary_heads_content", KATTR__MAX);
3992 if (kerr != KCGI_OK)
3995 SIMPLEQ_FOREACH(re, &refs, entry) {
3996 const char *refname;
3998 if (got_ref_is_symbolic(re->ref))
4001 refname = got_ref_get_name(re->ref);
4002 if (refname == NULL) {
4003 error = got_error_from_errno("got_ref_to_str");
4007 if (strncmp(refname, "refs/heads/", 11) != 0)
4010 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4015 if (strncmp(refname, "refs/heads/", 11) == 0)
4018 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4019 KATTR_ID, "heads_wrapper", KATTR__MAX);
4020 if (kerr != KCGI_OK)
4022 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4023 KATTR_ID, "heads_age", KATTR__MAX);
4024 if (kerr != KCGI_OK)
4026 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4027 if (kerr != KCGI_OK)
4029 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4030 if (kerr != KCGI_OK)
4032 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4033 KATTR_ID, "heads_space", KATTR__MAX);
4034 if (kerr != KCGI_OK)
4036 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4037 if (kerr != KCGI_OK)
4039 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4040 if (kerr != KCGI_OK)
4042 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4043 KATTR_ID, "head", KATTR__MAX);
4044 if (kerr != KCGI_OK)
4046 if (asprintf(&href_summary,
4047 "?path=%s&action=summary&headref=%s",
4048 gw_trans->repo_name, refname) == -1) {
4049 error = got_error_from_errno("asprintf");
4052 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4053 href_summary, KATTR__MAX);
4054 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4055 if (kerr != KCGI_OK)
4057 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4058 if (kerr != KCGI_OK)
4061 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4062 "navs_wrapper", KATTR__MAX);
4063 if (kerr != KCGI_OK)
4065 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4066 "navs", KATTR__MAX);
4067 if (kerr != KCGI_OK)
4070 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4071 href_summary, KATTR__MAX);
4072 if (kerr != KCGI_OK)
4074 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4075 if (kerr != KCGI_OK)
4077 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4078 if (kerr != KCGI_OK)
4081 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4082 if (kerr != KCGI_OK)
4084 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4085 gw_trans->repo_name, refname) == -1) {
4086 error = got_error_from_errno("asprintf");
4089 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4090 href_briefs, KATTR__MAX);
4091 if (kerr != KCGI_OK)
4093 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4094 if (kerr != KCGI_OK)
4096 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4097 if (kerr != KCGI_OK)
4100 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4101 if (kerr != KCGI_OK)
4104 if (asprintf(&href_commits,
4105 "?path=%s&action=commits&headref=%s",
4106 gw_trans->repo_name, refname) == -1) {
4107 error = got_error_from_errno("asprintf");
4110 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4111 href_commits, KATTR__MAX);
4112 if (kerr != KCGI_OK)
4114 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4115 if (kerr != KCGI_OK)
4117 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4118 if (kerr != KCGI_OK)
4121 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4122 "dotted_line", KATTR__MAX);
4123 if (kerr != KCGI_OK)
4125 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4126 if (kerr != KCGI_OK)
4129 href_summary = NULL;
4133 href_commits = NULL;
4136 got_ref_list_free(&refs);
4141 got_repo_close(repo);
4145 static const struct got_error *
4146 gw_output_site_link(struct gw_trans *gw_trans)
4148 const struct got_error *error = NULL;
4149 char *href_summary = NULL;
4150 enum kcgi_err kerr = KCGI_OK;
4152 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4153 "site_link", KATTR__MAX);
4154 if (kerr != KCGI_OK)
4156 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4158 if (kerr != KCGI_OK)
4160 kerr = khtml_puts(gw_trans->gw_html_req,
4161 gw_trans->gw_conf->got_site_link);
4162 if (kerr != KCGI_OK)
4164 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4165 if (kerr != KCGI_OK)
4168 if (gw_trans->repo_name != NULL) {
4169 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4170 if (kerr != KCGI_OK)
4172 if (asprintf(&href_summary, "?path=%s&action=summary",
4173 gw_trans->repo_name) == -1)
4175 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4176 href_summary, KATTR__MAX);
4177 if (kerr != KCGI_OK)
4179 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4180 if (kerr != KCGI_OK)
4182 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4183 if (kerr != KCGI_OK)
4185 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4186 if (kerr != KCGI_OK)
4188 kerr = khtml_puts(gw_trans->gw_html_req,
4189 gw_get_action_name(gw_trans));
4190 if (kerr != KCGI_OK)
4194 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4195 if (kerr != KCGI_OK)
4202 static const struct got_error *
4203 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4205 const struct got_error *error = NULL;
4207 enum kcgi_err kerr = KCGI_OK;
4209 if (strncmp(buf, "-", 1) == 0)
4210 color = "diff_minus";
4211 else if (strncmp(buf, "+", 1) == 0)
4212 color = "diff_plus";
4213 else if (strncmp(buf, "@@", 2) == 0)
4214 color = "diff_chunk_header";
4215 else if (strncmp(buf, "@@", 2) == 0)
4216 color = "diff_chunk_header";
4217 else if (strncmp(buf, "commit +", 8) == 0)
4218 color = "diff_meta";
4219 else if (strncmp(buf, "commit -", 8) == 0)
4220 color = "diff_meta";
4221 else if (strncmp(buf, "blob +", 6) == 0)
4222 color = "diff_meta";
4223 else if (strncmp(buf, "blob -", 6) == 0)
4224 color = "diff_meta";
4225 else if (strncmp(buf, "file +", 6) == 0)
4226 color = "diff_meta";
4227 else if (strncmp(buf, "file -", 6) == 0)
4228 color = "diff_meta";
4229 else if (strncmp(buf, "from:", 5) == 0)
4230 color = "diff_author";
4231 else if (strncmp(buf, "via:", 4) == 0)
4232 color = "diff_author";
4233 else if (strncmp(buf, "date:", 5) == 0)
4234 color = "diff_date";
4235 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4236 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4237 if (error == NULL && kerr != KCGI_OK)
4238 error = gw_kcgi_error(kerr);
4243 main(int argc, char *argv[])
4245 const struct got_error *error = NULL;
4246 struct gw_trans *gw_trans;
4247 struct gw_dir *dir = NULL, *tdir;
4248 const char *page = "index";
4252 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4255 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4258 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4261 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4264 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4265 if (kerr != KCGI_OK) {
4266 error = gw_kcgi_error(kerr);
4270 if ((gw_trans->gw_conf =
4271 malloc(sizeof(struct gotweb_conf))) == NULL) {
4273 error = got_error_from_errno("malloc");
4277 TAILQ_INIT(&gw_trans->gw_dirs);
4278 TAILQ_INIT(&gw_trans->gw_headers);
4280 gw_trans->action = -1;
4282 gw_trans->repos_total = 0;
4283 gw_trans->repo_path = NULL;
4284 gw_trans->commit = NULL;
4285 gw_trans->headref = GOT_REF_HEAD;
4286 gw_trans->mime = KMIME_TEXT_HTML;
4287 gw_trans->gw_tmpl->key = gw_templs;
4288 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4289 gw_trans->gw_tmpl->arg = gw_trans;
4290 gw_trans->gw_tmpl->cb = gw_template;
4291 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4295 error = gw_parse_querystring(gw_trans);
4299 if (gw_trans->action == GW_BLOB)
4300 error = gw_blob(gw_trans);
4302 error = gw_display_index(gw_trans);
4305 free(gw_trans->gw_conf->got_repos_path);
4306 free(gw_trans->gw_conf->got_site_name);
4307 free(gw_trans->gw_conf->got_site_owner);
4308 free(gw_trans->gw_conf->got_site_link);
4309 free(gw_trans->gw_conf->got_logo);
4310 free(gw_trans->gw_conf->got_logo_url);
4311 free(gw_trans->gw_conf);
4312 free(gw_trans->commit);
4313 free(gw_trans->repo_path);
4315 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4317 free(dir->description);
4326 khttp_free(gw_trans->gw_req);