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 char *repo_name;
66 const char *repo_file;
71 unsigned int repos_total;
76 TAILQ_ENTRY(gw_header) entry;
77 struct got_repository *repo;
78 struct got_reflist_head refs;
79 struct got_commit_object *commit;
80 struct got_object_id *id;
84 char *commit_id; /* id_str1 */
85 char *parent_id; /* id_str2 */
88 const char *committer;
90 time_t committer_time;
94 TAILQ_ENTRY(gw_dir) entry;
135 static const char *const gw_templs[TEMPL__MAX] = {
145 static const struct kvalid gw_keys[KEY__ZMAX] = {
146 { kvalid_stringne, "action" },
147 { kvalid_stringne, "commit" },
148 { kvalid_stringne, "file" },
149 { kvalid_stringne, "folder" },
150 { kvalid_stringne, "headref" },
151 { kvalid_int, "page" },
152 { kvalid_stringne, "path" },
155 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
156 static struct gw_header *gw_init_header(void);
158 static void gw_free_headers(struct gw_header *);
159 static void gw_display_error(struct gw_trans *,
160 const struct got_error *);
162 static int gw_template(size_t, void *);
164 static const struct got_error *gw_get_repo_description(char **,
165 struct gw_trans *, char *);
166 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
168 static const struct got_error *gw_get_time_str(char **, time_t, int);
169 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
170 char *, char *, int);
171 static const struct got_error *gw_output_file_blame(struct gw_trans *);
172 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
173 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
174 static const struct got_error *gw_output_diff(struct gw_trans *,
176 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
177 struct gw_header *, int, int);
178 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
179 static const struct got_error *gw_output_site_link(struct gw_trans *);
180 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
182 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
184 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
186 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
188 static const struct got_error *gw_gen_author_header(struct gw_trans *,
190 static const struct got_error *gw_gen_age_header(struct gw_trans *,
192 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
194 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
196 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
197 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
199 static const struct got_error *gw_display_index(struct gw_trans *);
200 static const struct got_error *gw_get_header(struct gw_trans *,
201 struct gw_header *, int);
202 static const struct got_error *gw_get_commits(struct gw_trans *,
203 struct gw_header *, int);
204 static const struct got_error *gw_get_commit(struct gw_trans *,
206 static const struct got_error *gw_apply_unveil(const char *);
207 static const struct got_error *gw_blame_cb(void *, int, int,
208 struct got_object_id *);
209 static const struct got_error *gw_load_got_paths(struct gw_trans *);
210 static const struct got_error *gw_load_got_path(struct gw_trans *,
212 static const struct got_error *gw_parse_querystring(struct gw_trans *);
213 static const struct got_error *gw_blame(struct gw_trans *);
214 static const struct got_error *gw_blob(struct gw_trans *);
215 static const struct got_error *gw_diff(struct gw_trans *);
216 static const struct got_error *gw_index(struct gw_trans *);
217 static const struct got_error *gw_commits(struct gw_trans *);
218 static const struct got_error *gw_briefs(struct gw_trans *);
219 static const struct got_error *gw_summary(struct gw_trans *);
220 static const struct got_error *gw_tree(struct gw_trans *);
221 static const struct got_error *gw_tag(struct gw_trans *);
223 struct gw_query_action {
224 unsigned int func_id;
225 const char *func_name;
226 const struct got_error *(*func_main)(struct gw_trans *);
230 enum gw_query_actions {
243 static struct gw_query_action gw_query_funcs[] = {
244 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
245 { GW_BLOB, "blob", NULL, NULL },
246 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
247 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
248 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
249 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
250 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
251 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
252 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
253 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
257 gw_get_action_name(struct gw_trans *gw_trans)
259 return gw_query_funcs[gw_trans->action].func_name;
262 static const struct got_error *
263 gw_kcgi_error(enum kcgi_err kerr)
268 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
269 return got_error(GOT_ERR_CANCELLED);
271 if (kerr == KCGI_ENOMEM)
272 return got_error_set_errno(ENOMEM,
273 kcgi_strerror(kerr));
275 if (kerr == KCGI_ENFILE)
276 return got_error_set_errno(ENFILE,
277 kcgi_strerror(kerr));
279 if (kerr == KCGI_EAGAIN)
280 return got_error_set_errno(EAGAIN,
281 kcgi_strerror(kerr));
283 if (kerr == KCGI_FORM)
284 return got_error_msg(GOT_ERR_IO,
285 kcgi_strerror(kerr));
287 return got_error_from_errno(kcgi_strerror(kerr));
290 static const struct got_error *
291 gw_apply_unveil(const char *repo_path)
293 const struct got_error *err;
295 if (repo_path && unveil(repo_path, "r") != 0)
296 return got_error_from_errno2("unveil", repo_path);
298 if (unveil("/tmp", "rwc") != 0)
299 return got_error_from_errno2("unveil", "/tmp");
301 err = got_privsep_unveil_exec_helpers();
305 if (unveil(NULL, NULL) != 0)
306 return got_error_from_errno("unveil");
312 isbinary(const uint8_t *buf, size_t n)
316 for (i = 0; i < n; i++)
322 static const struct got_error *
323 gw_blame(struct gw_trans *gw_trans)
325 const struct got_error *error = NULL;
326 struct gw_header *header = NULL;
330 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
332 return got_error_from_errno("pledge");
334 if ((header = gw_init_header()) == NULL)
335 return got_error_from_errno("malloc");
337 error = gw_apply_unveil(gw_trans->gw_dir->path);
341 error = gw_get_header(gw_trans, header, 1);
345 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
346 "blame_header_wrapper", KATTR__MAX);
349 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
350 "blame_header", KATTR__MAX);
353 error = gw_get_time_str(&age, header->committer_time,
357 error = gw_gen_age_header(gw_trans, age ?age : "");
360 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
363 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
367 "dotted_line", KATTR__MAX);
370 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
375 "blame", KATTR__MAX);
378 error = gw_output_file_blame(gw_trans);
381 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
383 got_ref_list_free(&header->refs);
384 gw_free_headers(header);
385 if (error == NULL && kerr != KCGI_OK)
386 error = gw_kcgi_error(kerr);
390 static const struct got_error *
391 gw_blob(struct gw_trans *gw_trans)
393 const struct got_error *error = NULL;
394 struct gw_header *header = NULL;
396 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
398 return got_error_from_errno("pledge");
400 if ((header = gw_init_header()) == NULL)
401 return got_error_from_errno("malloc");
403 error = gw_apply_unveil(gw_trans->gw_dir->path);
407 error = gw_get_header(gw_trans, header, 1);
411 error = gw_output_blob_buf(gw_trans);
413 got_ref_list_free(&header->refs);
414 gw_free_headers(header);
418 static const struct got_error *
419 gw_diff(struct gw_trans *gw_trans)
421 const struct got_error *error = NULL;
422 struct gw_header *header = NULL;
424 enum kcgi_err kerr = KCGI_OK;
426 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
428 return got_error_from_errno("pledge");
430 if ((header = gw_init_header()) == NULL)
431 return got_error_from_errno("malloc");
433 error = gw_apply_unveil(gw_trans->gw_dir->path);
437 error = gw_get_header(gw_trans, header, 1);
441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
442 "diff_header_wrapper", KATTR__MAX);
445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
446 "diff_header", KATTR__MAX);
449 error = gw_gen_diff_header(gw_trans, header->parent_id,
453 error = gw_gen_commit_header(gw_trans, header->commit_id,
457 error = gw_gen_tree_header(gw_trans, header->tree_id);
460 error = gw_gen_author_header(gw_trans, header->author);
463 error = gw_gen_committer_header(gw_trans, header->author);
466 error = gw_get_time_str(&age, header->committer_time,
470 error = gw_gen_age_header(gw_trans, age ?age : "");
473 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
476 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
479 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
480 "dotted_line", KATTR__MAX);
483 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
487 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
491 error = gw_output_diff(gw_trans, header);
495 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
497 got_ref_list_free(&header->refs);
498 gw_free_headers(header);
500 if (error == NULL && kerr != KCGI_OK)
501 error = gw_kcgi_error(kerr);
505 static const struct got_error *
506 gw_index(struct gw_trans *gw_trans)
508 const struct got_error *error = NULL;
509 struct gw_dir *gw_dir = NULL;
510 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
511 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
512 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
515 if (pledge("stdio rpath proc exec sendfd unveil",
517 error = got_error_from_errno("pledge");
521 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
525 error = gw_load_got_paths(gw_trans);
529 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
530 "index_header", KATTR__MAX);
532 return gw_kcgi_error(kerr);
533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
534 "index_header_project", KATTR__MAX);
536 return gw_kcgi_error(kerr);
537 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
539 return gw_kcgi_error(kerr);
540 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
542 return gw_kcgi_error(kerr);
544 if (gw_trans->gw_conf->got_show_repo_description) {
545 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
546 "index_header_description", KATTR__MAX);
548 return gw_kcgi_error(kerr);
549 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
551 return gw_kcgi_error(kerr);
552 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
554 return gw_kcgi_error(kerr);
557 if (gw_trans->gw_conf->got_show_repo_owner) {
558 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
559 "index_header_owner", KATTR__MAX);
561 return gw_kcgi_error(kerr);
562 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
564 return gw_kcgi_error(kerr);
565 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
567 return gw_kcgi_error(kerr);
570 if (gw_trans->gw_conf->got_show_repo_age) {
571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
572 "index_header_age", KATTR__MAX);
574 return gw_kcgi_error(kerr);
575 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
577 return gw_kcgi_error(kerr);
578 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
580 return gw_kcgi_error(kerr);
583 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
585 return gw_kcgi_error(kerr);
587 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
588 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
589 "index_wrapper", KATTR__MAX);
591 return gw_kcgi_error(kerr);
592 kerr = khtml_puts(gw_trans->gw_html_req,
593 "No repositories found in ");
595 return gw_kcgi_error(kerr);
596 kerr = khtml_puts(gw_trans->gw_html_req,
597 gw_trans->gw_conf->got_repos_path);
599 return gw_kcgi_error(kerr);
600 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
602 return gw_kcgi_error(kerr);
603 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
604 "dotted_line", KATTR__MAX);
606 return gw_kcgi_error(kerr);
607 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
609 return gw_kcgi_error(kerr);
613 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
616 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
617 if (gw_trans->page > 0 && (gw_trans->page *
618 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
625 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
626 "index_wrapper", KATTR__MAX);
630 if (asprintf(&href_summary, "?path=%s&action=summary",
631 gw_dir->name) == -1) {
632 error = got_error_from_errno("asprintf");
635 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
636 "index_project", KATTR__MAX);
639 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
640 href_summary, KATTR__MAX);
643 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
646 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
649 if (gw_trans->gw_conf->got_show_repo_description) {
650 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
651 KATTR_ID, "index_project_description", KATTR__MAX);
654 kerr = khtml_puts(gw_trans->gw_html_req,
655 gw_dir->description ? gw_dir->description : "");
658 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
662 if (gw_trans->gw_conf->got_show_repo_owner) {
663 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
664 KATTR_ID, "index_project_owner", KATTR__MAX);
667 kerr = khtml_puts(gw_trans->gw_html_req,
668 gw_dir->owner ? gw_dir->owner : "");
671 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
675 if (gw_trans->gw_conf->got_show_repo_age) {
676 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
677 KATTR_ID, "index_project_age", KATTR__MAX);
680 kerr = khtml_puts(gw_trans->gw_html_req,
681 gw_dir->age ? gw_dir->age : "");
684 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
689 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
690 "navs_wrapper", KATTR__MAX);
693 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
698 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
699 href_summary, KATTR__MAX);
702 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
705 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
709 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
713 if (asprintf(&href_briefs, "?path=%s&action=briefs",
714 gw_dir->name) == -1) {
715 error = got_error_from_errno("asprintf");
718 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
719 href_briefs, KATTR__MAX);
722 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
725 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
727 error = gw_kcgi_error(kerr);
729 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
733 if (asprintf(&href_commits, "?path=%s&action=commits",
734 gw_dir->name) == -1) {
735 error = got_error_from_errno("asprintf");
738 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
739 href_commits, KATTR__MAX);
742 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
745 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
749 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
753 if (asprintf(&href_tree, "?path=%s&action=tree",
754 gw_dir->name) == -1) {
755 error = got_error_from_errno("asprintf");
758 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
759 href_tree, KATTR__MAX);
762 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
766 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
769 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
770 "dotted_line", KATTR__MAX);
773 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
786 if (gw_trans->gw_conf->got_max_repos_display == 0)
789 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
790 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
791 KATTR_ID, "np_wrapper", KATTR__MAX);
794 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
795 KATTR_ID, "nav_prev", KATTR__MAX);
798 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
799 (gw_trans->page > 0) &&
800 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
801 prev_disp == gw_trans->repos_total)) {
802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
803 KATTR_ID, "np_wrapper", KATTR__MAX);
806 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
807 KATTR_ID, "nav_prev", KATTR__MAX);
812 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
813 (gw_trans->page > 0) &&
814 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
815 prev_disp == gw_trans->repos_total)) {
816 if (asprintf(&href_prev, "?page=%d",
817 gw_trans->page - 1) == -1) {
818 error = got_error_from_errno("asprintf");
821 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
822 KATTR_HREF, href_prev, KATTR__MAX);
825 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
828 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
833 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
835 return gw_kcgi_error(kerr);
837 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
838 next_disp == gw_trans->gw_conf->got_max_repos_display &&
839 dir_c != (gw_trans->page + 1) *
840 gw_trans->gw_conf->got_max_repos_display) {
841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
842 KATTR_ID, "nav_next", KATTR__MAX);
845 if (asprintf(&href_next, "?page=%d",
846 gw_trans->page + 1) == -1) {
847 error = got_error_from_errno("calloc");
850 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
851 KATTR_HREF, href_next, KATTR__MAX);
854 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
857 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
864 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
865 (gw_trans->page > 0) &&
866 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
867 prev_disp == gw_trans->repos_total)) {
868 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
881 if (error == NULL && kerr != KCGI_OK)
882 error = gw_kcgi_error(kerr);
886 static const struct got_error *
887 gw_commits(struct gw_trans *gw_trans)
889 const struct got_error *error = NULL;
890 struct gw_header *header = NULL, *n_header = NULL;
892 char *href_diff = NULL, *href_blob = NULL;
893 enum kcgi_err kerr = KCGI_OK;
895 if ((header = gw_init_header()) == NULL)
896 return got_error_from_errno("malloc");
898 if (pledge("stdio rpath proc exec sendfd unveil",
900 error = got_error_from_errno("pledge");
904 error = gw_apply_unveil(gw_trans->gw_dir->path);
908 error = gw_get_header(gw_trans, header,
909 gw_trans->gw_conf->got_max_commits_display);
913 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
914 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
915 "commits_line_wrapper", KATTR__MAX);
918 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
922 error = gw_gen_author_header(gw_trans, n_header->author);
925 error = gw_gen_committer_header(gw_trans, n_header->author);
928 error = gw_get_time_str(&age, n_header->committer_time,
932 error = gw_gen_age_header(gw_trans, age ?age : "");
935 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
939 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
940 "dotted_line", KATTR__MAX);
943 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
947 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
948 "commit", KATTR__MAX);
951 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
954 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
958 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
959 gw_trans->repo_name, n_header->commit_id) == -1) {
960 error = got_error_from_errno("asprintf");
963 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
964 KATTR_ID, "navs_wrapper", KATTR__MAX);
967 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
968 KATTR_ID, "navs", KATTR__MAX);
971 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
972 KATTR_HREF, href_diff, KATTR__MAX);
975 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
978 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
982 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
986 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
987 gw_trans->repo_name, n_header->commit_id) == -1) {
988 error = got_error_from_errno("asprintf");
991 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
992 KATTR_HREF, href_blob, KATTR__MAX);
995 khtml_puts(gw_trans->gw_html_req, "tree");
996 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
999 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1000 if (kerr != KCGI_OK)
1003 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1004 "solid_line", KATTR__MAX);
1005 if (kerr != KCGI_OK)
1007 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1008 if (kerr != KCGI_OK)
1015 got_ref_list_free(&header->refs);
1016 gw_free_headers(header);
1017 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1018 gw_free_headers(n_header);
1022 if (error == NULL && kerr != KCGI_OK)
1023 error = gw_kcgi_error(kerr);
1027 static const struct got_error *
1028 gw_briefs(struct gw_trans *gw_trans)
1030 const struct got_error *error = NULL;
1031 struct gw_header *header = NULL, *n_header = NULL;
1032 char *age = NULL, *age_html = 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 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
1074 error = got_error_from_errno("asprintf");
1077 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
1078 if (kerr != KCGI_OK)
1080 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1081 if (kerr != KCGI_OK)
1084 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1085 KATTR_ID, "briefs_author", KATTR__MAX);
1086 if (kerr != KCGI_OK)
1088 smallerthan = strchr(n_header->author, '<');
1090 *smallerthan = '\0';
1091 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1092 if (kerr != KCGI_OK)
1094 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1095 if (kerr != KCGI_OK)
1098 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1099 gw_trans->repo_name, n_header->commit_id) == -1) {
1100 error = got_error_from_errno("asprintf");
1103 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1104 KATTR_ID, "briefs_log", KATTR__MAX);
1105 if (kerr != KCGI_OK)
1107 newline = strchr(n_header->commit_msg, '\n');
1110 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1111 KATTR_HREF, href_diff, KATTR__MAX);
1112 if (kerr != KCGI_OK)
1114 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1115 if (kerr != KCGI_OK)
1117 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1118 if (kerr != KCGI_OK)
1121 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1122 KATTR_ID, "navs_wrapper", KATTR__MAX);
1123 if (kerr != KCGI_OK)
1125 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1126 KATTR_ID, "navs", KATTR__MAX);
1127 if (kerr != KCGI_OK)
1129 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1130 KATTR_HREF, href_diff, KATTR__MAX);
1131 if (kerr != KCGI_OK)
1133 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1134 if (kerr != KCGI_OK)
1136 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1137 if (kerr != KCGI_OK)
1140 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1141 if (kerr != KCGI_OK)
1144 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1145 gw_trans->repo_name, n_header->commit_id) == -1) {
1146 error = got_error_from_errno("asprintf");
1149 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1150 KATTR_HREF, href_blob, KATTR__MAX);
1151 if (kerr != KCGI_OK)
1153 khtml_puts(gw_trans->gw_html_req, "tree");
1154 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1155 if (kerr != KCGI_OK)
1157 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1158 if (kerr != KCGI_OK)
1161 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1162 KATTR_ID, "dotted_line", KATTR__MAX);
1163 if (kerr != KCGI_OK)
1165 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1166 if (kerr != KCGI_OK)
1179 got_ref_list_free(&header->refs);
1180 gw_free_headers(header);
1181 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1182 gw_free_headers(n_header);
1187 if (error == NULL && kerr != KCGI_OK)
1188 error = gw_kcgi_error(kerr);
1192 static const struct got_error *
1193 gw_summary(struct gw_trans *gw_trans)
1195 const struct got_error *error = NULL;
1197 enum kcgi_err kerr = KCGI_OK;
1199 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1200 return got_error_from_errno("pledge");
1202 /* unveil is applied with gw_briefs below */
1204 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1205 "summary_wrapper", KATTR__MAX);
1206 if (kerr != KCGI_OK)
1207 return gw_kcgi_error(kerr);
1209 if (gw_trans->gw_conf->got_show_repo_description &&
1210 gw_trans->gw_dir->description != NULL &&
1211 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1212 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1213 KATTR_ID, "description_title", KATTR__MAX);
1214 if (kerr != KCGI_OK)
1216 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1217 if (kerr != KCGI_OK)
1219 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1220 if (kerr != KCGI_OK)
1222 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1223 KATTR_ID, "description", KATTR__MAX);
1224 if (kerr != KCGI_OK)
1226 kerr = khtml_puts(gw_trans->gw_html_req,
1227 gw_trans->gw_dir->description);
1228 if (kerr != KCGI_OK)
1230 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1231 if (kerr != KCGI_OK)
1235 if (gw_trans->gw_conf->got_show_repo_owner &&
1236 gw_trans->gw_dir->owner != NULL &&
1237 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1238 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1239 KATTR_ID, "repo_owner_title", KATTR__MAX);
1240 if (kerr != KCGI_OK)
1242 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1243 if (kerr != KCGI_OK)
1245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1246 if (kerr != KCGI_OK)
1248 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1249 KATTR_ID, "repo_owner", KATTR__MAX);
1250 if (kerr != KCGI_OK)
1252 kerr = khtml_puts(gw_trans->gw_html_req,
1253 gw_trans->gw_dir->owner);
1254 if (kerr != KCGI_OK)
1256 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1257 if (kerr != KCGI_OK)
1261 if (gw_trans->gw_conf->got_show_repo_age) {
1262 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1263 "refs/heads", TM_LONG);
1267 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1268 KATTR_ID, "last_change_title", KATTR__MAX);
1269 if (kerr != KCGI_OK)
1271 kerr = khtml_puts(gw_trans->gw_html_req,
1273 if (kerr != KCGI_OK)
1275 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1276 if (kerr != KCGI_OK)
1278 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1279 KATTR_ID, "last_change", KATTR__MAX);
1280 if (kerr != KCGI_OK)
1282 kerr = khtml_puts(gw_trans->gw_html_req, age);
1283 if (kerr != KCGI_OK)
1285 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1286 if (kerr != KCGI_OK)
1291 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1292 gw_trans->gw_dir->url != NULL &&
1293 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1294 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1295 KATTR_ID, "cloneurl_title", KATTR__MAX);
1296 if (kerr != KCGI_OK)
1298 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1299 if (kerr != KCGI_OK)
1301 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1302 if (kerr != KCGI_OK)
1304 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1305 KATTR_ID, "cloneurl", KATTR__MAX);
1306 if (kerr != KCGI_OK)
1308 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1309 if (kerr != KCGI_OK)
1311 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1312 if (kerr != KCGI_OK)
1316 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1317 if (kerr != KCGI_OK)
1320 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1321 "briefs_title_wrapper", KATTR__MAX);
1322 if (kerr != KCGI_OK)
1324 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1325 "briefs_title", KATTR__MAX);
1326 if (kerr != KCGI_OK)
1328 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1329 if (kerr != KCGI_OK)
1331 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1332 if (kerr != KCGI_OK)
1334 error = gw_briefs(gw_trans);
1338 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1343 error = gw_output_repo_heads(gw_trans);
1346 if (error == NULL && kerr != KCGI_OK)
1347 error = gw_kcgi_error(kerr);
1351 static const struct got_error *
1352 gw_tree(struct gw_trans *gw_trans)
1354 const struct got_error *error = NULL;
1355 struct gw_header *header = NULL;
1356 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1357 char *age = NULL, *age_html = NULL;
1360 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1361 return got_error_from_errno("pledge");
1363 if ((header = gw_init_header()) == NULL)
1364 return got_error_from_errno("malloc");
1366 error = gw_apply_unveil(gw_trans->gw_dir->path);
1370 error = gw_get_header(gw_trans, header, 1);
1374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1375 "tree_header_wrapper", KATTR__MAX);
1376 if (kerr != KCGI_OK)
1378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1379 "tree_header", KATTR__MAX);
1380 if (kerr != KCGI_OK)
1382 error = gw_gen_tree_header(gw_trans, header->tree_id);
1385 error = gw_get_time_str(&age, header->committer_time,
1389 error = gw_gen_age_header(gw_trans, age ?age : "");
1392 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1395 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1396 if (kerr != KCGI_OK)
1398 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1399 "dotted_line", KATTR__MAX);
1400 if (kerr != KCGI_OK)
1402 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1403 if (kerr != KCGI_OK)
1406 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1407 "tree", KATTR__MAX);
1408 if (kerr != KCGI_OK)
1410 error = gw_output_repo_tree(gw_trans);
1414 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1416 got_ref_list_free(&header->refs);
1417 gw_free_headers(header);
1418 free(tree_html_disp);
1423 if (error == NULL && kerr != KCGI_OK)
1424 error = gw_kcgi_error(kerr);
1428 static const struct got_error *
1429 gw_tag(struct gw_trans *gw_trans)
1431 const struct got_error *error = NULL;
1432 struct gw_header *header = NULL;
1435 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1436 return got_error_from_errno("pledge");
1438 if ((header = gw_init_header()) == NULL)
1439 return got_error_from_errno("malloc");
1441 error = gw_apply_unveil(gw_trans->gw_dir->path);
1445 error = gw_get_header(gw_trans, header, 1);
1449 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1450 "tag_header_wrapper", KATTR__MAX);
1451 if (kerr != KCGI_OK)
1453 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1454 "tag_header", KATTR__MAX);
1455 if (kerr != KCGI_OK)
1457 error = gw_gen_commit_header(gw_trans, header->commit_id,
1461 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1464 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1465 if (kerr != KCGI_OK)
1467 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1468 "dotted_line", KATTR__MAX);
1469 if (kerr != KCGI_OK)
1471 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1472 if (kerr != KCGI_OK)
1475 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1476 "tree", KATTR__MAX);
1477 if (kerr != KCGI_OK)
1480 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1484 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1486 got_ref_list_free(&header->refs);
1487 gw_free_headers(header);
1488 if (error == NULL && kerr != KCGI_OK)
1489 error = gw_kcgi_error(kerr);
1493 static const struct got_error *
1494 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1496 const struct got_error *error = NULL;
1501 if (asprintf(&dir_test, "%s/%s/%s",
1502 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1503 GOTWEB_GIT_DIR) == -1)
1504 return got_error_from_errno("asprintf");
1506 dt = opendir(dir_test);
1510 gw_dir->path = strdup(dir_test);
1511 if (gw_dir->path == NULL) {
1513 error = got_error_from_errno("strdup");
1520 if (asprintf(&dir_test, "%s/%s/%s",
1521 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1522 GOTWEB_GOT_DIR) == -1)
1523 return got_error_from_errno("asprintf");
1525 dt = opendir(dir_test);
1530 error = got_error(GOT_ERR_NOT_GIT_REPO);
1534 if (asprintf(&dir_test, "%s/%s",
1535 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1536 return got_error_from_errno("asprintf");
1538 gw_dir->path = strdup(dir_test);
1539 if (gw_dir->path == NULL) {
1541 error = got_error_from_errno("strdup");
1545 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1549 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1552 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1553 "refs/heads", TM_DIFF);
1556 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1564 static const struct got_error *
1565 gw_load_got_paths(struct gw_trans *gw_trans)
1567 const struct got_error *error = NULL;
1569 struct dirent **sd_dent;
1570 struct gw_dir *gw_dir;
1572 unsigned int d_cnt, d_i;
1574 d = opendir(gw_trans->gw_conf->got_repos_path);
1576 error = got_error_from_errno2("opendir",
1577 gw_trans->gw_conf->got_repos_path);
1581 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1584 error = got_error_from_errno2("scandir",
1585 gw_trans->gw_conf->got_repos_path);
1589 for (d_i = 0; d_i < d_cnt; d_i++) {
1590 if (gw_trans->gw_conf->got_max_repos > 0 &&
1591 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1592 break; /* account for parent and self */
1594 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1595 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1598 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1602 error = gw_load_got_path(gw_trans, gw_dir);
1603 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1610 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1611 !got_path_dir_is_empty(gw_dir->path)) {
1612 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1614 gw_trans->repos_total++;
1622 static const struct got_error *
1623 gw_parse_querystring(struct gw_trans *gw_trans)
1625 const struct got_error *error = NULL;
1627 struct gw_query_action *action = NULL;
1630 if (gw_trans->gw_req->fieldnmap[0]) {
1631 error = got_error_from_errno("bad parse");
1633 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1634 /* define gw_trans->repo_path */
1635 gw_trans->repo_name = p->parsed.s;
1637 if (asprintf(&gw_trans->repo_path, "%s/%s",
1638 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1639 return got_error_from_errno("asprintf");
1641 /* get action and set function */
1642 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1643 for (i = 0; i < nitems(gw_query_funcs); i++) {
1644 action = &gw_query_funcs[i];
1645 if (action->func_name == NULL ||
1646 strcmp(action->func_name, p->parsed.s))
1649 gw_trans->action = i;
1653 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1654 if (asprintf(&gw_trans->commit, "%s",
1656 return got_error_from_errno("asprintf");
1659 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1660 gw_trans->repo_file = p->parsed.s;
1662 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1663 if (asprintf(&gw_trans->repo_folder, "%s",
1665 return got_error_from_errno("asprintf");
1668 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF])) {
1669 if (asprintf(&gw_trans->headref, "%s",
1671 return got_error_from_errno("asprintf");
1674 if (action == NULL) {
1675 error = got_error_from_errno("invalid action");
1678 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1682 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1686 gw_trans->action = GW_INDEX;
1688 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1689 gw_trans->page = p->parsed.i;
1694 static const struct got_error *
1695 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1697 const struct got_error *error;
1699 *gw_dir = malloc(sizeof(**gw_dir));
1700 if (*gw_dir == NULL)
1701 return got_error_from_errno("malloc");
1703 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1704 error = got_error_from_errno("asprintf");
1713 static const struct got_error *
1714 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1718 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1719 if (kerr != KCGI_OK)
1720 return gw_kcgi_error(kerr);
1721 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1723 if (kerr != KCGI_OK)
1724 return gw_kcgi_error(kerr);
1725 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1727 if (kerr != KCGI_OK)
1728 return gw_kcgi_error(kerr);
1729 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1731 if (kerr != KCGI_OK)
1732 return gw_kcgi_error(kerr);
1733 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1734 if (kerr != KCGI_OK)
1735 return gw_kcgi_error(kerr);
1736 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1738 if (kerr != KCGI_OK)
1739 return gw_kcgi_error(kerr);
1741 /* XXX repo_file could be NULL if not present in querystring */
1742 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1743 kerr = khttp_head(gw_trans->gw_req,
1744 kresps[KRESP_CONTENT_DISPOSITION],
1745 "attachment; filename=%s", gw_trans->repo_file);
1746 if (kerr != KCGI_OK)
1747 return gw_kcgi_error(kerr);
1750 kerr = khttp_body(gw_trans->gw_req);
1751 return gw_kcgi_error(kerr);
1754 static const struct got_error *
1755 gw_display_index(struct gw_trans *gw_trans)
1757 const struct got_error *error;
1760 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1764 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1765 if (kerr != KCGI_OK)
1766 return gw_kcgi_error(kerr);
1768 if (gw_trans->action != GW_BLOB) {
1769 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1770 gw_query_funcs[gw_trans->action].template);
1771 if (kerr != KCGI_OK) {
1772 khtml_close(gw_trans->gw_html_req);
1773 return gw_kcgi_error(kerr);
1777 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1781 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1783 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1786 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1788 khtml_puts(gw_trans->gw_html_req, err->msg);
1789 khtml_close(gw_trans->gw_html_req);
1793 gw_template(size_t key, void *arg)
1795 const struct got_error *error = NULL;
1797 struct gw_trans *gw_trans = arg;
1798 char *img_src = NULL;
1802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1803 KATTR_NAME, "viewport",
1804 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1806 if (kerr != KCGI_OK)
1808 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1809 if (kerr != KCGI_OK)
1811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1812 KATTR_CHARSET, "utf-8",
1814 if (kerr != KCGI_OK)
1816 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1817 if (kerr != KCGI_OK)
1819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1820 KATTR_NAME, "msapplication-TileColor",
1821 KATTR_CONTENT, "#da532c", KATTR__MAX);
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_NAME, "theme-color",
1829 KATTR_CONTENT, "#ffffff", KATTR__MAX);
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_LINK,
1836 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1837 KATTR_HREF, "/apple-touch-icon.png", 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_LINK,
1844 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1845 "32x32", KATTR_HREF, "/favicon-32x32.png", 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, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1853 "16x16", KATTR_HREF, "/favicon-16x16.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, "manifest", KATTR_HREF, "/site.webmanifest",
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, "mask-icon", KATTR_HREF,
1869 "/safari-pinned-tab.svg", 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, "stylesheet", KATTR_TYPE, "text/css",
1877 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1878 if (kerr != KCGI_OK)
1880 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1881 if (kerr != KCGI_OK)
1885 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1886 KATTR_ID, "got_link", KATTR__MAX);
1887 if (kerr != KCGI_OK)
1889 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1890 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1891 KATTR_TARGET, "_sotd", KATTR__MAX);
1892 if (kerr != KCGI_OK)
1894 if (asprintf(&img_src, "/%s",
1895 gw_trans->gw_conf->got_logo) == -1)
1897 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1898 KATTR_SRC, img_src, KATTR__MAX);
1899 if (kerr != KCGI_OK) {
1903 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1904 if (kerr != KCGI_OK) {
1909 case (TEMPL_SITEPATH):
1910 error = gw_output_site_link(gw_trans);
1915 if (gw_trans->gw_conf->got_site_name != NULL) {
1916 kerr = khtml_puts(gw_trans->gw_html_req,
1917 gw_trans->gw_conf->got_site_name);
1918 if (kerr != KCGI_OK)
1922 case (TEMPL_SEARCH):
1924 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1925 "search", KATTR__MAX);
1926 if (kerr != KCGI_OK)
1928 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1929 KATTR_METHOD, "POST", KATTR__MAX);
1930 if (kerr != KCGI_OK)
1932 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1933 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1934 KATTR_MAXLENGTH, "50", KATTR__MAX);
1935 if (kerr != KCGI_OK)
1937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1939 if (kerr != KCGI_OK)
1941 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1942 if (kerr != KCGI_OK)
1944 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1945 if (kerr != KCGI_OK)
1948 case(TEMPL_SITEOWNER):
1949 if (gw_trans->gw_conf->got_site_owner != NULL &&
1950 gw_trans->gw_conf->got_show_site_owner) {
1951 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1952 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1953 if (kerr != KCGI_OK)
1955 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1956 KATTR_ID, "site_owner", KATTR__MAX);
1957 if (kerr != KCGI_OK)
1959 kerr = khtml_puts(gw_trans->gw_html_req,
1960 gw_trans->gw_conf->got_site_owner);
1961 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1962 if (kerr != KCGI_OK)
1966 case(TEMPL_CONTENT):
1967 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1969 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1970 KATTR_ID, "tmpl_err", KATTR__MAX);
1971 if (kerr != KCGI_OK)
1973 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1974 if (kerr != KCGI_OK)
1976 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1977 if (kerr != KCGI_OK)
1979 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1980 if (kerr != KCGI_OK)
1990 static const struct got_error *
1991 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1993 const struct got_error *error = NULL;
1994 enum kcgi_err kerr = KCGI_OK;
1996 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1997 KATTR_ID, "header_commit_title", KATTR__MAX);
1998 if (kerr != KCGI_OK)
2000 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2001 if (kerr != KCGI_OK)
2003 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2004 if (kerr != KCGI_OK)
2006 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2007 KATTR_ID, "header_commit", KATTR__MAX);
2008 if (kerr != KCGI_OK)
2010 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2011 if (kerr != KCGI_OK)
2013 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2014 if (kerr != KCGI_OK)
2017 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2018 if (kerr != KCGI_OK)
2020 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2021 if (kerr != KCGI_OK)
2023 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2024 if (kerr != KCGI_OK)
2027 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2029 if (error == NULL && kerr != KCGI_OK)
2030 error = gw_kcgi_error(kerr);
2034 static const struct got_error *
2035 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2037 const struct got_error *error = NULL;
2038 enum kcgi_err kerr = KCGI_OK;
2040 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2041 KATTR_ID, "header_diff_title", KATTR__MAX);
2042 if (kerr != KCGI_OK)
2044 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2045 if (kerr != KCGI_OK)
2047 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2048 if (kerr != KCGI_OK)
2050 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2051 KATTR_ID, "header_diff", KATTR__MAX);
2052 if (kerr != KCGI_OK)
2055 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2056 if (kerr != KCGI_OK)
2059 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2060 if (kerr != KCGI_OK)
2062 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2063 if (kerr != KCGI_OK)
2065 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2067 if (error == NULL && kerr != KCGI_OK)
2068 error = gw_kcgi_error(kerr);
2072 static const struct got_error *
2073 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2075 const struct got_error *error = NULL;
2076 enum kcgi_err kerr = KCGI_OK;
2078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2079 KATTR_ID, "header_age_title", KATTR__MAX);
2080 if (kerr != KCGI_OK)
2082 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2083 if (kerr != KCGI_OK)
2085 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2086 if (kerr != KCGI_OK)
2088 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2089 KATTR_ID, "header_age", KATTR__MAX);
2090 if (kerr != KCGI_OK)
2092 kerr = khtml_puts(gw_trans->gw_html_req, str);
2093 if (kerr != KCGI_OK)
2095 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2097 if (error == NULL && kerr != KCGI_OK)
2098 error = gw_kcgi_error(kerr);
2102 static const struct got_error *
2103 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2105 const struct got_error *error = NULL;
2108 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2109 KATTR_ID, "header_author_title", KATTR__MAX);
2110 if (kerr != KCGI_OK)
2112 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2113 if (kerr != KCGI_OK)
2115 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2116 if (kerr != KCGI_OK)
2118 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2119 KATTR_ID, "header_author", KATTR__MAX);
2120 if (kerr != KCGI_OK)
2122 kerr = khtml_puts(gw_trans->gw_html_req, str);
2123 if (kerr != KCGI_OK)
2125 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2127 if (error == NULL && kerr != KCGI_OK)
2128 error = gw_kcgi_error(kerr);
2132 static const struct got_error *
2133 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2135 const struct got_error *error = NULL;
2138 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2139 KATTR_ID, "header_committer_title", KATTR__MAX);
2140 if (kerr != KCGI_OK)
2142 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2143 if (kerr != KCGI_OK)
2145 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2146 if (kerr != KCGI_OK)
2148 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2149 KATTR_ID, "header_committer", KATTR__MAX);
2150 if (kerr != KCGI_OK)
2152 kerr = khtml_puts(gw_trans->gw_html_req, str);
2153 if (kerr != KCGI_OK)
2155 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2157 if (error == NULL && kerr != KCGI_OK)
2158 error = gw_kcgi_error(kerr);
2162 static const struct got_error *
2163 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2165 const struct got_error *error = NULL;
2168 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2169 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2170 if (kerr != KCGI_OK)
2172 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2173 if (kerr != KCGI_OK)
2175 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2176 if (kerr != KCGI_OK)
2178 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2179 KATTR_ID, "header_commit_msg", KATTR__MAX);
2180 if (kerr != KCGI_OK)
2182 kerr = khttp_puts(gw_trans->gw_req, str);
2183 if (kerr != KCGI_OK)
2185 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2187 if (error == NULL && kerr != KCGI_OK)
2188 error = gw_kcgi_error(kerr);
2192 static const struct got_error *
2193 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2195 const struct got_error *error = NULL;
2198 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2199 KATTR_ID, "header_tree_title", KATTR__MAX);
2200 if (kerr != KCGI_OK)
2202 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2203 if (kerr != KCGI_OK)
2205 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2206 if (kerr != KCGI_OK)
2208 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2209 KATTR_ID, "header_tree", KATTR__MAX);
2210 if (kerr != KCGI_OK)
2212 kerr = khtml_puts(gw_trans->gw_html_req, str);
2213 if (kerr != KCGI_OK)
2215 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2217 if (error == NULL && kerr != KCGI_OK)
2218 error = gw_kcgi_error(kerr);
2222 static const struct got_error *
2223 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2226 const struct got_error *error = NULL;
2228 char *d_file = NULL;
2232 *description = NULL;
2233 if (gw_trans->gw_conf->got_show_repo_description == 0)
2236 if (asprintf(&d_file, "%s/description", dir) == -1)
2237 return got_error_from_errno("asprintf");
2239 f = fopen(d_file, "r");
2241 if (errno == ENOENT || errno == EACCES)
2243 error = got_error_from_errno2("fopen", d_file);
2247 if (fseek(f, 0, SEEK_END) == -1) {
2248 error = got_ferror(f, GOT_ERR_IO);
2253 error = got_ferror(f, GOT_ERR_IO);
2256 if (fseek(f, 0, SEEK_SET) == -1) {
2257 error = got_ferror(f, GOT_ERR_IO);
2260 *description = calloc(len + 1, sizeof(**description));
2261 if (*description == NULL) {
2262 error = got_error_from_errno("calloc");
2266 n = fread(*description, 1, len, f);
2267 if (n == 0 && ferror(f))
2268 error = got_ferror(f, GOT_ERR_IO);
2270 if (f != NULL && fclose(f) == -1 && error == NULL)
2271 error = got_error_from_errno("fclose");
2276 static const struct got_error *
2277 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2281 char *years = "years ago", *months = "months ago";
2282 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2283 char *minutes = "minutes ago", *seconds = "seconds ago";
2284 char *now = "right now";
2292 diff_time = time(NULL) - committer_time;
2293 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2294 if (asprintf(repo_age, "%lld %s",
2295 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2296 return got_error_from_errno("asprintf");
2297 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2298 if (asprintf(repo_age, "%lld %s",
2299 (diff_time / 60 / 60 / 24 / (365 / 12)),
2301 return got_error_from_errno("asprintf");
2302 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2303 if (asprintf(repo_age, "%lld %s",
2304 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2305 return got_error_from_errno("asprintf");
2306 } else if (diff_time > 60 * 60 * 24 * 2) {
2307 if (asprintf(repo_age, "%lld %s",
2308 (diff_time / 60 / 60 / 24), days) == -1)
2309 return got_error_from_errno("asprintf");
2310 } else if (diff_time > 60 * 60 * 2) {
2311 if (asprintf(repo_age, "%lld %s",
2312 (diff_time / 60 / 60), hours) == -1)
2313 return got_error_from_errno("asprintf");
2314 } else if (diff_time > 60 * 2) {
2315 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2317 return got_error_from_errno("asprintf");
2318 } else if (diff_time > 2) {
2319 if (asprintf(repo_age, "%lld %s", diff_time,
2321 return got_error_from_errno("asprintf");
2323 if (asprintf(repo_age, "%s", now) == -1)
2324 return got_error_from_errno("asprintf");
2328 if (gmtime_r(&committer_time, &tm) == NULL)
2329 return got_error_from_errno("gmtime_r");
2331 s = asctime_r(&tm, datebuf);
2333 return got_error_from_errno("asctime_r");
2335 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2336 return got_error_from_errno("asprintf");
2342 static const struct got_error *
2343 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2344 char *repo_ref, int ref_tm)
2346 const struct got_error *error = NULL;
2347 struct got_object_id *id = NULL;
2348 struct got_repository *repo = NULL;
2349 struct got_commit_object *commit = NULL;
2350 struct got_reflist_head refs;
2351 struct got_reflist_entry *re;
2352 struct got_reference *head_ref;
2354 time_t committer_time = 0, cmp_time = 0;
2355 const char *refname;
2358 SIMPLEQ_INIT(&refs);
2360 if (repo_ref == NULL)
2363 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2366 if (gw_trans->gw_conf->got_show_repo_age == 0)
2369 error = got_repo_open(&repo, dir, NULL);
2374 error = got_ref_list(&refs, repo, "refs/heads",
2375 got_ref_cmp_by_name, NULL);
2377 error = got_ref_list(&refs, repo, repo_ref,
2378 got_ref_cmp_by_name, NULL);
2382 SIMPLEQ_FOREACH(re, &refs, entry) {
2384 refname = strdup(repo_ref);
2385 if (refname == NULL) {
2386 error = got_error_from_errno("strdup");
2390 refname = got_ref_get_name(re->ref);
2391 if (refname == NULL) {
2392 error = got_error_from_errno("strdup");
2396 error = got_ref_open(&head_ref, repo, refname, 0);
2400 error = got_ref_resolve(&id, repo, head_ref);
2401 got_ref_close(head_ref);
2405 error = got_object_open_as_commit(&commit, repo, id);
2410 got_object_commit_get_committer_time(commit);
2412 if (cmp_time < committer_time)
2413 cmp_time = committer_time;
2416 if (cmp_time != 0) {
2417 committer_time = cmp_time;
2418 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2421 got_ref_list_free(&refs);
2426 static const struct got_error *
2427 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2429 const struct got_error *error;
2431 struct got_object_id *id1 = NULL, *id2 = NULL;
2432 char *label1 = NULL, *label2 = NULL, *line = NULL;
2434 size_t linesize = 0;
2436 enum kcgi_err kerr = KCGI_OK;
2442 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2446 if (header->parent_id != NULL &&
2447 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2448 error = got_repo_match_object_id(&id1, &label1,
2449 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2454 error = got_repo_match_object_id(&id2, &label2,
2455 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2459 error = got_object_get_type(&obj_type, header->repo, id2);
2463 case GOT_OBJ_TYPE_BLOB:
2464 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2467 case GOT_OBJ_TYPE_TREE:
2468 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2471 case GOT_OBJ_TYPE_COMMIT:
2472 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2476 error = got_error(GOT_ERR_OBJ_TYPE);
2481 if (fseek(f, 0, SEEK_SET) == -1) {
2482 error = got_ferror(f, GOT_ERR_IO);
2486 while ((linelen = getline(&line, &linesize, f)) != -1) {
2487 error = gw_colordiff_line(gw_trans, line);
2490 /* XXX: KHTML_PRETTY breaks this */
2491 kerr = khtml_puts(gw_trans->gw_html_req, line);
2492 if (kerr != KCGI_OK)
2494 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2495 if (kerr != KCGI_OK)
2498 if (linelen == -1 && ferror(f))
2499 error = got_error_from_errno("getline");
2501 if (f && fclose(f) == -1 && error == NULL)
2502 error = got_error_from_errno("fclose");
2509 if (error == NULL && kerr != KCGI_OK)
2510 error = gw_kcgi_error(kerr);
2514 static const struct got_error *
2515 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2517 const struct got_error *error = NULL;
2518 struct got_repository *repo;
2519 const char *gitconfig_owner;
2523 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2526 error = got_repo_open(&repo, dir, NULL);
2529 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2530 if (gitconfig_owner) {
2531 *owner = strdup(gitconfig_owner);
2533 error = got_error_from_errno("strdup");
2535 got_repo_close(repo);
2539 static const struct got_error *
2540 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2542 const struct got_error *error = NULL;
2544 char *d_file = NULL;
2550 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2551 return got_error_from_errno("asprintf");
2553 f = fopen(d_file, "r");
2555 if (errno != ENOENT && errno != EACCES)
2556 error = got_error_from_errno2("fopen", d_file);
2560 if (fseek(f, 0, SEEK_END) == -1) {
2561 error = got_ferror(f, GOT_ERR_IO);
2566 error = got_ferror(f, GOT_ERR_IO);
2569 if (fseek(f, 0, SEEK_SET) == -1) {
2570 error = got_ferror(f, GOT_ERR_IO);
2574 *url = calloc(len + 1, sizeof(**url));
2576 error = got_error_from_errno("calloc");
2580 n = fread(*url, 1, len, f);
2581 if (n == 0 && ferror(f))
2582 error = got_ferror(f, GOT_ERR_IO);
2584 if (f && fclose(f) == -1 && error == NULL)
2585 error = got_error_from_errno("fclose");
2590 static const struct got_error *
2591 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2592 int limit, int tag_type)
2594 const struct got_error *error = NULL;
2595 struct got_repository *repo = NULL;
2596 struct got_reflist_head refs;
2597 struct got_reflist_entry *re;
2599 char *id_str = NULL, *newline, *href_commits = NULL;
2600 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2601 struct got_tag_object *tag = NULL;
2602 enum kcgi_err kerr = KCGI_OK;
2603 int summary_header_displayed = 0;
2605 SIMPLEQ_INIT(&refs);
2607 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2611 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2615 SIMPLEQ_FOREACH(re, &refs, entry) {
2616 const char *refname;
2618 const char *tag_commit;
2620 struct got_object_id *id;
2621 struct got_commit_object *commit = NULL;
2623 refname = got_ref_get_name(re->ref);
2624 if (strncmp(refname, "refs/tags/", 10) != 0)
2628 error = got_ref_resolve(&id, repo, re->ref);
2632 error = got_object_open_as_tag(&tag, repo, id);
2634 if (error->code != GOT_ERR_OBJ_TYPE) {
2638 /* "lightweight" tag */
2639 error = got_object_open_as_commit(&commit, repo, id);
2644 tagger = got_object_commit_get_committer(commit);
2646 got_object_commit_get_committer_time(commit);
2647 error = got_object_id_str(&id_str, id);
2651 tagger = got_object_tag_get_tagger(tag);
2652 tagger_time = got_object_tag_get_tagger_time(tag);
2653 error = got_object_id_str(&id_str,
2654 got_object_tag_get_object_id(tag));
2659 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2660 strlen(id_str)) != 0)
2664 error = got_object_commit_get_logmsg(&tag_commit0,
2668 got_object_commit_close(commit);
2670 tag_commit0 = strdup(got_object_tag_get_message(tag));
2671 if (tag_commit0 == NULL) {
2672 error = got_error_from_errno("strdup");
2677 tag_commit = tag_commit0;
2678 while (*tag_commit == '\n')
2683 newline = strchr(tag_commit, '\n');
2687 if (summary_header_displayed == 0) {
2688 kerr = khtml_attr(gw_trans->gw_html_req,
2689 KELEM_DIV, KATTR_ID,
2690 "summary_tags_title_wrapper", KATTR__MAX);
2691 if (kerr != KCGI_OK)
2693 kerr = khtml_attr(gw_trans->gw_html_req,
2694 KELEM_DIV, KATTR_ID,
2695 "summary_tags_title", KATTR__MAX);
2696 if (kerr != KCGI_OK)
2698 kerr = khtml_puts(gw_trans->gw_html_req,
2700 if (kerr != KCGI_OK)
2702 kerr = khtml_closeelem(gw_trans->gw_html_req,
2704 if (kerr != KCGI_OK)
2706 kerr = khtml_attr(gw_trans->gw_html_req,
2707 KELEM_DIV, KATTR_ID,
2708 "summary_tags_content", KATTR__MAX);
2709 if (kerr != KCGI_OK)
2711 summary_header_displayed = 1;
2714 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2715 KATTR_ID, "tags_wrapper", KATTR__MAX);
2716 if (kerr != KCGI_OK)
2718 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2719 KATTR_ID, "tags_age", KATTR__MAX);
2720 if (kerr != KCGI_OK)
2722 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2725 kerr = khtml_puts(gw_trans->gw_html_req,
2727 if (kerr != KCGI_OK)
2729 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2730 if (kerr != KCGI_OK)
2732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2733 KATTR_ID, "tags", KATTR__MAX);
2734 if (kerr != KCGI_OK)
2736 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2737 if (kerr != KCGI_OK)
2739 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2740 if (kerr != KCGI_OK)
2742 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2743 KATTR_ID, "tags_name", KATTR__MAX);
2744 if (kerr != KCGI_OK)
2746 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2747 gw_trans->repo_name, id_str) == -1) {
2748 error = got_error_from_errno("asprintf");
2751 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2752 KATTR_HREF, href_tag, KATTR__MAX);
2753 if (kerr != KCGI_OK)
2755 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2756 if (kerr != KCGI_OK)
2758 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2759 if (kerr != KCGI_OK)
2762 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2763 KATTR_ID, "navs_wrapper", KATTR__MAX);
2764 if (kerr != KCGI_OK)
2766 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2767 KATTR_ID, "navs", KATTR__MAX);
2768 if (kerr != KCGI_OK)
2771 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2772 KATTR_HREF, href_tag, KATTR__MAX);
2773 if (kerr != KCGI_OK)
2775 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2776 if (kerr != KCGI_OK)
2778 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2779 if (kerr != KCGI_OK)
2782 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2783 if (kerr != KCGI_OK)
2785 if (asprintf(&href_briefs,
2786 "?path=%s&action=briefs&commit=%s",
2787 gw_trans->repo_name, id_str) == -1) {
2788 error = got_error_from_errno("asprintf");
2791 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2792 KATTR_HREF, href_briefs, KATTR__MAX);
2793 if (kerr != KCGI_OK)
2795 kerr = khtml_puts(gw_trans->gw_html_req,
2797 if (kerr != KCGI_OK)
2799 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2800 if (kerr != KCGI_OK)
2803 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2804 if (kerr != KCGI_OK)
2807 if (asprintf(&href_commits,
2808 "?path=%s&action=commits&commit=%s",
2809 gw_trans->repo_name, id_str) == -1) {
2810 error = got_error_from_errno("asprintf");
2813 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2814 KATTR_HREF, href_commits, KATTR__MAX);
2815 if (kerr != KCGI_OK)
2817 kerr = khtml_puts(gw_trans->gw_html_req,
2819 if (kerr != KCGI_OK)
2821 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2822 if (kerr != KCGI_OK)
2825 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2826 KATTR_ID, "dotted_line", KATTR__MAX);
2827 if (kerr != KCGI_OK)
2829 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2830 if (kerr != KCGI_OK)
2834 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2835 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2836 if (kerr != KCGI_OK)
2838 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2839 if (kerr != KCGI_OK)
2841 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2842 if (kerr != KCGI_OK)
2844 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2845 KATTR_ID, "tag_info_date", KATTR__MAX);
2846 if (kerr != KCGI_OK)
2848 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2851 kerr = khtml_puts(gw_trans->gw_html_req,
2853 if (kerr != KCGI_OK)
2855 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2856 if (kerr != KCGI_OK)
2859 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2860 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2861 if (kerr != KCGI_OK)
2863 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2864 if (kerr != KCGI_OK)
2866 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2867 if (kerr != KCGI_OK)
2869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2870 KATTR_ID, "tag_info_date", KATTR__MAX);
2871 if (kerr != KCGI_OK)
2873 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2874 if (kerr != KCGI_OK)
2876 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2877 if (kerr != KCGI_OK)
2880 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2881 KATTR_ID, "tag_info", KATTR__MAX);
2882 if (kerr != KCGI_OK)
2884 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2885 if (kerr != KCGI_OK)
2891 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2892 if (kerr != KCGI_OK)
2895 if (limit && --limit == 0)
2899 got_object_tag_close(tag);
2912 href_commits = NULL;
2916 got_object_tag_close(tag);
2923 got_ref_list_free(&refs);
2925 got_repo_close(repo);
2926 if (error == NULL && kerr != KCGI_OK)
2927 error = gw_kcgi_error(kerr);
2932 gw_free_headers(struct gw_header *header)
2936 if (header->commit != NULL)
2937 got_object_commit_close(header->commit);
2939 got_repo_close(header->repo);
2940 free(header->refs_str);
2941 free(header->commit_id);
2942 free(header->parent_id);
2943 free(header->tree_id);
2944 free(header->commit_msg);
2947 static struct gw_header *
2950 struct gw_header *header;
2952 header = malloc(sizeof(*header));
2956 header->repo = NULL;
2957 header->commit = NULL;
2959 header->path = NULL;
2960 SIMPLEQ_INIT(&header->refs);
2962 header->refs_str = NULL;
2963 header->commit_id = NULL;
2964 header->parent_id = NULL;
2965 header->tree_id = NULL;
2966 header->commit_msg = NULL;
2971 static const struct got_error *
2972 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2975 const struct got_error *error = NULL;
2976 struct got_commit_graph *graph = NULL;
2978 error = got_commit_graph_open(&graph, header->path, 0);
2982 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2988 error = got_commit_graph_iter_next(&header->id, graph,
2989 header->repo, NULL, NULL);
2991 if (error->code == GOT_ERR_ITER_COMPLETED)
2995 if (header->id == NULL)
2998 error = got_object_open_as_commit(&header->commit, header->repo,
3003 error = gw_get_commit(gw_trans, header);
3005 struct gw_header *n_header = NULL;
3006 if ((n_header = gw_init_header()) == NULL) {
3007 error = got_error_from_errno("malloc");
3011 if (header->refs_str != NULL) {
3012 n_header->refs_str = strdup(header->refs_str);
3013 if (n_header->refs_str == NULL) {
3014 error = got_error_from_errno("strdup");
3018 n_header->commit_id = strdup(header->commit_id);
3019 if (n_header->commit_id == NULL) {
3020 error = got_error_from_errno("strdup");
3023 if (header->parent_id != NULL) {
3024 n_header->parent_id = strdup(header->parent_id);
3025 if (n_header->parent_id == NULL) {
3026 error = got_error_from_errno("strdup");
3030 n_header->tree_id = strdup(header->tree_id);
3031 if (n_header->tree_id == NULL) {
3032 error = got_error_from_errno("strdup");
3035 n_header->author = header->author;
3036 n_header->committer = header->committer;
3037 n_header->commit_msg = strdup(header->commit_msg);
3038 if (n_header->commit_msg == NULL) {
3039 error = got_error_from_errno("strdup");
3042 n_header->committer_time = header->committer_time;
3043 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3046 if (error || (limit && --limit == 0))
3051 got_commit_graph_close(graph);
3055 static const struct got_error *
3056 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3058 const struct got_error *error = NULL;
3059 struct got_reflist_entry *re;
3060 struct got_object_id *id2 = NULL;
3061 struct got_object_qid *parent_id;
3062 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
3065 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3068 struct got_tag_object *tag = NULL;
3071 name = got_ref_get_name(re->ref);
3072 if (strcmp(name, GOT_REF_HEAD) == 0)
3074 if (strncmp(name, "refs/", 5) == 0)
3076 if (strncmp(name, "got/", 4) == 0)
3078 if (strncmp(name, "heads/", 6) == 0)
3080 if (strncmp(name, "remotes/", 8) == 0)
3082 if (strncmp(name, "tags/", 5) == 0) {
3083 error = got_object_open_as_tag(&tag, header->repo,
3086 if (error->code != GOT_ERR_OBJ_TYPE)
3089 * Ref points at something other
3096 cmp = got_object_id_cmp(tag ?
3097 got_object_tag_get_object_id(tag) : re->id, header->id);
3099 got_object_tag_close(tag);
3103 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
3104 s ? ", " : "", name) == -1) {
3105 error = got_error_from_errno("asprintf");
3110 header->refs_str = strdup(refs_str);
3111 if (header->refs_str == NULL) {
3112 error = got_error_from_errno("strdup");
3119 error = got_object_id_str(&header->commit_id, header->id);
3123 error = got_object_id_str(&header->tree_id,
3124 got_object_commit_get_tree_id(header->commit));
3128 if (gw_trans->action == GW_DIFF) {
3129 parent_id = SIMPLEQ_FIRST(
3130 got_object_commit_get_parent_ids(header->commit));
3131 if (parent_id != NULL) {
3132 id2 = got_object_id_dup(parent_id->id);
3134 error = got_object_id_str(&header->parent_id, id2);
3139 header->parent_id = strdup("/dev/null");
3140 if (header->parent_id == NULL) {
3141 error = got_error_from_errno("strdup");
3147 header->committer_time =
3148 got_object_commit_get_committer_time(header->commit);
3151 got_object_commit_get_author(header->commit);
3153 got_object_commit_get_committer(header->commit);
3156 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3160 commit_msg = commit_msg0;
3161 while (*commit_msg == '\n')
3164 header->commit_msg = strdup(commit_msg);
3165 if (header->commit_msg == NULL)
3166 error = got_error_from_errno("strdup");
3171 static const struct got_error *
3172 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3174 const struct got_error *error = NULL;
3175 char *in_repo_path = NULL;
3177 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3181 if (gw_trans->commit == NULL) {
3182 struct got_reference *head_ref;
3183 error = got_ref_open(&head_ref, header->repo,
3184 gw_trans->headref, 0);
3188 error = got_ref_resolve(&header->id, header->repo, head_ref);
3189 got_ref_close(head_ref);
3193 error = got_object_open_as_commit(&header->commit,
3194 header->repo, header->id);
3196 struct got_reference *ref;
3197 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3198 if (error == NULL) {
3200 error = got_ref_resolve(&header->id, header->repo, ref);
3204 error = got_object_get_type(&obj_type, header->repo,
3208 if (obj_type == GOT_OBJ_TYPE_TAG) {
3209 struct got_tag_object *tag;
3210 error = got_object_open_as_tag(&tag,
3211 header->repo, header->id);
3214 if (got_object_tag_get_object_type(tag) !=
3215 GOT_OBJ_TYPE_COMMIT) {
3216 got_object_tag_close(tag);
3217 error = got_error(GOT_ERR_OBJ_TYPE);
3221 header->id = got_object_id_dup(
3222 got_object_tag_get_object_id(tag));
3223 if (header->id == NULL)
3224 error = got_error_from_errno(
3225 "got_object_id_dup");
3226 got_object_tag_close(tag);
3229 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3230 error = got_error(GOT_ERR_OBJ_TYPE);
3233 error = got_object_open_as_commit(&header->commit,
3234 header->repo, header->id);
3238 if (header->commit == NULL) {
3239 error = got_repo_match_object_id_prefix(&header->id,
3240 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3245 error = got_repo_match_object_id_prefix(&header->id,
3246 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3250 error = got_repo_map_path(&in_repo_path, header->repo,
3251 gw_trans->repo_path, 1);
3256 header->path = strdup(in_repo_path);
3257 if (header->path == NULL) {
3258 error = got_error_from_errno("strdup");
3265 error = got_ref_list(&header->refs, header->repo, NULL,
3266 got_ref_cmp_by_name, NULL);
3270 error = gw_get_commits(gw_trans, header, limit);
3278 char datebuf[11]; /* YYYY-MM-DD + NUL */
3281 struct gw_blame_cb_args {
3282 struct blame_line *lines;
3286 off_t *line_offsets;
3288 struct got_repository *repo;
3289 struct gw_trans *gw_trans;
3292 static const struct got_error *
3293 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3295 const struct got_error *err = NULL;
3296 struct gw_blame_cb_args *a = arg;
3297 struct blame_line *bline;
3299 size_t linesize = 0;
3300 struct got_commit_object *commit = NULL;
3303 time_t committer_time;
3304 enum kcgi_err kerr = KCGI_OK;
3306 if (nlines != a->nlines ||
3307 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3308 return got_error(GOT_ERR_RANGE);
3311 return NULL; /* no change in this commit */
3313 /* Annotate this line. */
3314 bline = &a->lines[lineno - 1];
3315 if (bline->annotated)
3317 err = got_object_id_str(&bline->id_str, id);
3321 err = got_object_open_as_commit(&commit, a->repo, id);
3325 bline->committer = strdup(got_object_commit_get_committer(commit));
3326 if (bline->committer == NULL) {
3327 err = got_error_from_errno("strdup");
3331 committer_time = got_object_commit_get_committer_time(commit);
3332 if (localtime_r(&committer_time, &tm) == NULL)
3333 return got_error_from_errno("localtime_r");
3334 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3335 &tm) >= sizeof(bline->datebuf)) {
3336 err = got_error(GOT_ERR_NO_SPACE);
3339 bline->annotated = 1;
3341 /* Print lines annotated so far. */
3342 bline = &a->lines[a->lineno_cur - 1];
3343 if (!bline->annotated)
3346 offset = a->line_offsets[a->lineno_cur - 1];
3347 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3348 err = got_error_from_errno("fseeko");
3352 while (bline->annotated) {
3353 char *smallerthan, *at, *nl, *committer;
3354 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3357 if (getline(&line, &linesize, a->f) == -1) {
3359 err = got_error_from_errno("getline");
3363 committer = bline->committer;
3364 smallerthan = strchr(committer, '<');
3365 if (smallerthan && smallerthan[1] != '\0')
3366 committer = smallerthan + 1;
3367 at = strchr(committer, '@');
3370 len = strlen(committer);
3372 committer[8] = '\0';
3374 nl = strchr(line, '\n');
3378 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3379 "blame_wrapper", KATTR__MAX);
3380 if (kerr != KCGI_OK)
3382 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3383 "blame_number", KATTR__MAX);
3384 if (kerr != KCGI_OK)
3386 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3387 a->lineno_cur) == -1)
3389 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3390 if (kerr != KCGI_OK)
3392 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3393 if (kerr != KCGI_OK)
3396 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3397 "blame_hash", KATTR__MAX);
3398 if (kerr != KCGI_OK)
3400 /* XXX repo_file could be NULL if not present in querystring */
3401 if (asprintf(&href_diff,
3402 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3403 a->gw_trans->repo_name, bline->id_str,
3404 a->gw_trans->repo_file, a->gw_trans->repo_folder ?
3405 a->gw_trans->repo_folder : "") == -1) {
3406 err = got_error_from_errno("asprintf");
3409 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3410 err = got_error_from_errno("asprintf");
3413 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3414 KATTR_HREF, href_diff, KATTR__MAX);
3415 if (kerr != KCGI_OK)
3417 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3418 if (kerr != KCGI_OK)
3420 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3421 if (kerr != KCGI_OK)
3424 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3425 "blame_date", KATTR__MAX);
3426 if (kerr != KCGI_OK)
3428 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3429 if (kerr != KCGI_OK)
3431 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3432 if (kerr != KCGI_OK)
3435 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3436 "blame_author", KATTR__MAX);
3437 if (kerr != KCGI_OK)
3439 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3440 if (kerr != KCGI_OK)
3442 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3443 if (kerr != KCGI_OK)
3446 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3447 "blame_code", KATTR__MAX);
3448 if (kerr != KCGI_OK)
3450 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3451 if (kerr != KCGI_OK)
3453 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3454 if (kerr != KCGI_OK)
3457 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3458 if (kerr != KCGI_OK)
3462 bline = &a->lines[a->lineno_cur - 1];
3470 got_object_commit_close(commit);
3472 if (err == NULL && kerr != KCGI_OK)
3473 err = gw_kcgi_error(kerr);
3477 static const struct got_error *
3478 gw_output_file_blame(struct gw_trans *gw_trans)
3480 const struct got_error *error = NULL;
3481 struct got_repository *repo = NULL;
3482 struct got_object_id *obj_id = NULL;
3483 struct got_object_id *commit_id = NULL;
3484 struct got_blob_object *blob = NULL;
3485 char *path = NULL, *in_repo_path = NULL;
3486 struct gw_blame_cb_args bca;
3490 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3494 /* XXX repo_file could be NULL if not present in querystring */
3495 if (asprintf(&path, "%s%s%s",
3496 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3497 gw_trans->repo_folder ? "/" : "",
3498 gw_trans->repo_file) == -1) {
3499 error = got_error_from_errno("asprintf");
3503 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3507 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3508 GOT_OBJ_TYPE_COMMIT, 1, repo);
3512 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3516 if (obj_id == NULL) {
3517 error = got_error(GOT_ERR_NO_OBJ);
3521 error = got_object_get_type(&obj_type, repo, obj_id);
3525 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3526 error = got_error(GOT_ERR_OBJ_TYPE);
3530 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3534 bca.f = got_opentemp();
3535 if (bca.f == NULL) {
3536 error = got_error_from_errno("got_opentemp");
3539 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3540 &bca.line_offsets, bca.f, blob);
3541 if (error || bca.nlines == 0)
3544 /* Don't include \n at EOF in the blame line count. */
3545 if (bca.line_offsets[bca.nlines - 1] == filesize)
3548 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3549 if (bca.lines == NULL) {
3550 error = got_error_from_errno("calloc");
3554 bca.nlines_prec = 0;
3561 bca.gw_trans = gw_trans;
3563 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3566 free(bca.line_offsets);
3572 for (i = 0; i < bca.nlines; i++) {
3573 struct blame_line *bline = &bca.lines[i];
3574 free(bline->id_str);
3575 free(bline->committer);
3578 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3579 error = got_error_from_errno("fclose");
3581 got_object_blob_close(blob);
3583 got_repo_close(repo);
3587 static const struct got_error *
3588 gw_output_blob_buf(struct gw_trans *gw_trans)
3590 const struct got_error *error = NULL;
3591 struct got_repository *repo = NULL;
3592 struct got_object_id *obj_id = NULL;
3593 struct got_object_id *commit_id = NULL;
3594 struct got_blob_object *blob = NULL;
3595 char *path = NULL, *in_repo_path = NULL;
3596 int obj_type, set_mime = 0;
3599 enum kcgi_err kerr = KCGI_OK;
3601 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3605 /* XXX repo_file could be NULL if not present in querystring */
3606 if (asprintf(&path, "%s%s%s",
3607 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3608 gw_trans->repo_folder ? "/" : "",
3609 gw_trans->repo_file) == -1) {
3610 error = got_error_from_errno("asprintf");
3614 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3618 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3619 GOT_OBJ_TYPE_COMMIT, 1, repo);
3623 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3627 if (obj_id == NULL) {
3628 error = got_error(GOT_ERR_NO_OBJ);
3632 error = got_object_get_type(&obj_type, repo, obj_id);
3636 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3637 error = got_error(GOT_ERR_OBJ_TYPE);
3641 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3645 hdrlen = got_object_blob_get_hdrlen(blob);
3647 error = got_object_blob_read_block(&len, blob);
3650 buf = got_object_blob_get_read_buf(blob);
3653 * Skip blob object header first time around,
3654 * which also contains a zero byte.
3657 if (set_mime == 0) {
3658 if (isbinary(buf, len - hdrlen))
3659 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3661 gw_trans->mime = KMIME_TEXT_PLAIN;
3663 error = gw_display_index(gw_trans);
3667 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3676 got_object_blob_close(blob);
3678 got_repo_close(repo);
3679 if (error == NULL && kerr != KCGI_OK)
3680 error = gw_kcgi_error(kerr);
3684 static const struct got_error *
3685 gw_output_repo_tree(struct gw_trans *gw_trans)
3687 const struct got_error *error = NULL;
3688 struct got_repository *repo = NULL;
3689 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3690 struct got_tree_object *tree = NULL;
3691 char *path = NULL, *in_repo_path = NULL;
3692 char *id_str = NULL;
3693 char *build_folder = NULL;
3694 char *href_blob = NULL, *href_blame = NULL;
3695 const char *class = NULL;
3696 int nentries, i, class_flip = 0;
3697 enum kcgi_err kerr = KCGI_OK;
3699 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3703 if (gw_trans->repo_folder != NULL) {
3704 path = strdup(gw_trans->repo_folder);
3706 error = got_error_from_errno("strdup");
3710 error = got_repo_map_path(&in_repo_path, repo,
3711 gw_trans->repo_path, 1);
3715 path = in_repo_path;
3718 if (gw_trans->commit == NULL) {
3719 struct got_reference *head_ref;
3720 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3723 error = got_ref_resolve(&commit_id, repo, head_ref);
3726 got_ref_close(head_ref);
3729 error = got_repo_match_object_id(&commit_id, NULL,
3730 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3736 * XXX gw_trans->commit might have already been allocated in
3737 * gw_parse_querystring(); could we more cleanly seperate values
3738 * we received as arguments from values we compute ourselves?
3740 error = got_object_id_str(&gw_trans->commit, commit_id);
3744 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3748 error = got_object_open_as_tree(&tree, repo, tree_id);
3752 nentries = got_object_tree_get_nentries(tree);
3753 for (i = 0; i < nentries; i++) {
3754 struct got_tree_entry *te;
3755 const char *modestr = "";
3758 te = got_object_tree_get_entry(tree, i);
3760 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3764 mode = got_tree_entry_get_mode(te);
3765 if (got_object_tree_entry_is_submodule(te))
3767 else if (S_ISLNK(mode))
3769 else if (S_ISDIR(mode))
3771 else if (mode & S_IXUSR)
3774 if (class_flip == 0) {
3775 class = "back_lightgray";
3778 class = "back_white";
3782 if (S_ISDIR(mode)) {
3783 if (asprintf(&build_folder, "%s/%s",
3784 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3785 got_tree_entry_get_name(te)) == -1) {
3786 error = got_error_from_errno(
3790 if (asprintf(&href_blob,
3791 "?path=%s&action=%s&commit=%s&folder=%s",
3792 gw_trans->repo_name, gw_get_action_name(gw_trans),
3793 gw_trans->commit, build_folder) == -1) {
3794 error = got_error_from_errno("asprintf");
3798 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3799 KATTR_ID, "tree_wrapper", KATTR__MAX);
3800 if (kerr != KCGI_OK)
3802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3803 KATTR_ID, "tree_line", KATTR_CLASS, class,
3805 if (kerr != KCGI_OK)
3807 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3808 KATTR_HREF, href_blob, KATTR_CLASS,
3809 "diff_directory", KATTR__MAX);
3810 if (kerr != KCGI_OK)
3812 kerr = khtml_puts(gw_trans->gw_html_req,
3813 got_tree_entry_get_name(te));
3814 if (kerr != KCGI_OK)
3816 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3817 if (kerr != KCGI_OK)
3819 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3820 if (kerr != KCGI_OK)
3822 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3823 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3825 if (kerr != KCGI_OK)
3827 kerr = khtml_entity(gw_trans->gw_html_req,
3829 if (kerr != KCGI_OK)
3831 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3832 if (kerr != KCGI_OK)
3835 if (asprintf(&href_blob,
3836 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3837 gw_trans->repo_name, "blob", gw_trans->commit,
3838 got_tree_entry_get_name(te),
3839 gw_trans->repo_folder ?
3840 gw_trans->repo_folder : "") == -1) {
3841 error = got_error_from_errno("asprintf");
3844 if (asprintf(&href_blame,
3845 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3846 gw_trans->repo_name, "blame", gw_trans->commit,
3847 got_tree_entry_get_name(te),
3848 gw_trans->repo_folder ?
3849 gw_trans->repo_folder : "") == -1) {
3850 error = got_error_from_errno("asprintf");
3854 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3855 KATTR_ID, "tree_wrapper", KATTR__MAX);
3856 if (kerr != KCGI_OK)
3858 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3859 KATTR_ID, "tree_line", KATTR_CLASS, class,
3861 if (kerr != KCGI_OK)
3863 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3864 KATTR_HREF, href_blob, KATTR__MAX);
3865 if (kerr != KCGI_OK)
3867 kerr = khtml_puts(gw_trans->gw_html_req,
3868 got_tree_entry_get_name(te));
3869 if (kerr != KCGI_OK)
3871 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3872 if (kerr != KCGI_OK)
3874 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3875 if (kerr != KCGI_OK)
3877 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3878 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3880 if (kerr != KCGI_OK)
3883 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3884 KATTR_HREF, href_blob, KATTR__MAX);
3885 if (kerr != KCGI_OK)
3887 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3888 if (kerr != KCGI_OK)
3890 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3891 if (kerr != KCGI_OK)
3894 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3895 if (kerr != KCGI_OK)
3898 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3899 KATTR_HREF, href_blame, KATTR__MAX);
3900 if (kerr != KCGI_OK)
3902 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3903 if (kerr != KCGI_OK)
3906 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3907 if (kerr != KCGI_OK)
3915 build_folder = NULL;
3919 got_object_tree_close(tree);
3921 got_repo_close(repo);
3928 if (error == NULL && kerr != KCGI_OK)
3929 error = gw_kcgi_error(kerr);
3933 static const struct got_error *
3934 gw_output_repo_heads(struct gw_trans *gw_trans)
3936 const struct got_error *error = NULL;
3937 struct got_repository *repo = NULL;
3938 struct got_reflist_head refs;
3939 struct got_reflist_entry *re;
3940 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3941 char *href_commits = NULL;
3942 enum kcgi_err kerr = KCGI_OK;
3944 SIMPLEQ_INIT(&refs);
3946 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3950 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3955 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3956 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3957 if (kerr != KCGI_OK)
3959 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3960 KATTR_ID, "summary_heads_title", KATTR__MAX);
3961 if (kerr != KCGI_OK)
3963 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3964 if (kerr != KCGI_OK)
3966 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3967 if (kerr != KCGI_OK)
3969 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3970 KATTR_ID, "summary_heads_content", KATTR__MAX);
3971 if (kerr != KCGI_OK)
3974 SIMPLEQ_FOREACH(re, &refs, entry) {
3977 refname = strdup(got_ref_get_name(re->ref));
3978 if (refname == NULL) {
3979 error = got_error_from_errno("got_ref_to_str");
3983 if (strncmp(refname, "refs/heads/", 11) != 0) {
3988 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3993 if (strncmp(refname, "refs/heads/", 11) == 0)
3996 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3997 KATTR_ID, "heads_wrapper", KATTR__MAX);
3998 if (kerr != KCGI_OK)
4000 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4001 KATTR_ID, "heads_age", KATTR__MAX);
4002 if (kerr != KCGI_OK)
4004 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4005 if (kerr != KCGI_OK)
4007 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4008 if (kerr != KCGI_OK)
4010 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4011 KATTR_ID, "heads_space", KATTR__MAX);
4012 if (kerr != KCGI_OK)
4014 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4015 if (kerr != KCGI_OK)
4017 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4018 if (kerr != KCGI_OK)
4020 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4021 KATTR_ID, "head", KATTR__MAX);
4022 if (kerr != KCGI_OK)
4024 if (asprintf(&href_summary,
4025 "?path=%s&action=summary&headref=%s",
4026 gw_trans->repo_name, refname) == -1) {
4027 error = got_error_from_errno("asprintf");
4030 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4031 href_summary, KATTR__MAX);
4032 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4033 if (kerr != KCGI_OK)
4035 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4036 if (kerr != KCGI_OK)
4039 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4040 "navs_wrapper", KATTR__MAX);
4041 if (kerr != KCGI_OK)
4043 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4044 "navs", KATTR__MAX);
4045 if (kerr != KCGI_OK)
4048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4049 href_summary, KATTR__MAX);
4050 if (kerr != KCGI_OK)
4052 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4053 if (kerr != KCGI_OK)
4055 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4056 if (kerr != KCGI_OK)
4059 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4060 if (kerr != KCGI_OK)
4062 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4063 gw_trans->repo_name, refname) == -1) {
4064 error = got_error_from_errno("asprintf");
4067 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4068 href_briefs, KATTR__MAX);
4069 if (kerr != KCGI_OK)
4071 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4072 if (kerr != KCGI_OK)
4074 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4075 if (kerr != KCGI_OK)
4078 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4079 if (kerr != KCGI_OK)
4082 if (asprintf(&href_commits,
4083 "?path=%s&action=commits&headref=%s",
4084 gw_trans->repo_name, refname) == -1) {
4085 error = got_error_from_errno("asprintf");
4088 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4089 href_commits, KATTR__MAX);
4090 if (kerr != KCGI_OK)
4092 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4093 if (kerr != KCGI_OK)
4095 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4096 if (kerr != KCGI_OK)
4099 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4100 "dotted_line", KATTR__MAX);
4101 if (kerr != KCGI_OK)
4103 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4104 if (kerr != KCGI_OK)
4107 href_summary = NULL;
4111 href_commits = NULL;
4114 got_ref_list_free(&refs);
4119 got_repo_close(repo);
4123 static const struct got_error *
4124 gw_output_site_link(struct gw_trans *gw_trans)
4126 const struct got_error *error = NULL;
4127 char *href_summary = NULL;
4128 enum kcgi_err kerr = KCGI_OK;
4130 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4131 "site_link", KATTR__MAX);
4132 if (kerr != KCGI_OK)
4134 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4136 if (kerr != KCGI_OK)
4138 kerr = khtml_puts(gw_trans->gw_html_req,
4139 gw_trans->gw_conf->got_site_link);
4140 if (kerr != KCGI_OK)
4142 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4143 if (kerr != KCGI_OK)
4146 if (gw_trans->repo_name != NULL) {
4147 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4148 if (kerr != KCGI_OK)
4150 if (asprintf(&href_summary, "?path=%s&action=summary",
4151 gw_trans->repo_name) == -1)
4153 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4154 href_summary, KATTR__MAX);
4155 if (kerr != KCGI_OK)
4157 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4158 if (kerr != KCGI_OK)
4160 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4161 if (kerr != KCGI_OK)
4163 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4164 if (kerr != KCGI_OK)
4166 kerr = khtml_puts(gw_trans->gw_html_req,
4167 gw_get_action_name(gw_trans));
4168 if (kerr != KCGI_OK)
4172 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4173 if (kerr != KCGI_OK)
4180 static const struct got_error *
4181 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4183 const struct got_error *error = NULL;
4185 enum kcgi_err kerr = KCGI_OK;
4187 if (strncmp(buf, "-", 1) == 0)
4188 color = "diff_minus";
4189 else if (strncmp(buf, "+", 1) == 0)
4190 color = "diff_plus";
4191 else if (strncmp(buf, "@@", 2) == 0)
4192 color = "diff_chunk_header";
4193 else if (strncmp(buf, "@@", 2) == 0)
4194 color = "diff_chunk_header";
4195 else if (strncmp(buf, "commit +", 8) == 0)
4196 color = "diff_meta";
4197 else if (strncmp(buf, "commit -", 8) == 0)
4198 color = "diff_meta";
4199 else if (strncmp(buf, "blob +", 6) == 0)
4200 color = "diff_meta";
4201 else if (strncmp(buf, "blob -", 6) == 0)
4202 color = "diff_meta";
4203 else if (strncmp(buf, "file +", 6) == 0)
4204 color = "diff_meta";
4205 else if (strncmp(buf, "file -", 6) == 0)
4206 color = "diff_meta";
4207 else if (strncmp(buf, "from:", 5) == 0)
4208 color = "diff_author";
4209 else if (strncmp(buf, "via:", 4) == 0)
4210 color = "diff_author";
4211 else if (strncmp(buf, "date:", 5) == 0)
4212 color = "diff_date";
4213 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4214 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4215 if (error == NULL && kerr != KCGI_OK)
4216 error = gw_kcgi_error(kerr);
4221 main(int argc, char *argv[])
4223 const struct got_error *error = NULL;
4224 struct gw_trans *gw_trans;
4225 struct gw_dir *dir = NULL, *tdir;
4226 const char *page = "index";
4230 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4233 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4236 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4239 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4242 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4243 if (kerr != KCGI_OK) {
4244 error = gw_kcgi_error(kerr);
4248 if ((gw_trans->gw_conf =
4249 malloc(sizeof(struct gotweb_conf))) == NULL) {
4251 error = got_error_from_errno("malloc");
4255 TAILQ_INIT(&gw_trans->gw_dirs);
4256 TAILQ_INIT(&gw_trans->gw_headers);
4259 gw_trans->repos_total = 0;
4260 gw_trans->repo_path = NULL;
4261 gw_trans->commit = NULL;
4262 gw_trans->headref = strdup(GOT_REF_HEAD);
4263 if (gw_trans->headref == NULL) {
4264 error = got_error_from_errno("strdup");
4267 gw_trans->mime = KMIME_TEXT_HTML;
4268 gw_trans->gw_tmpl->key = gw_templs;
4269 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4270 gw_trans->gw_tmpl->arg = gw_trans;
4271 gw_trans->gw_tmpl->cb = gw_template;
4272 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4276 error = gw_parse_querystring(gw_trans);
4280 if (gw_trans->action == GW_BLOB)
4281 error = gw_blob(gw_trans);
4283 error = gw_display_index(gw_trans);
4286 gw_trans->mime = KMIME_TEXT_PLAIN;
4287 gw_trans->action = GW_ERR;
4288 gw_display_error(gw_trans, error);
4291 free(gw_trans->gw_conf->got_repos_path);
4292 free(gw_trans->gw_conf->got_site_name);
4293 free(gw_trans->gw_conf->got_site_owner);
4294 free(gw_trans->gw_conf->got_site_link);
4295 free(gw_trans->gw_conf->got_logo);
4296 free(gw_trans->gw_conf->got_logo_url);
4297 free(gw_trans->gw_conf);
4298 free(gw_trans->commit);
4299 free(gw_trans->repo_path);
4300 free(gw_trans->headref);
4302 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4304 free(dir->description);
4313 khttp_free(gw_trans->gw_req);