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_error.h>
35 #include <got_object.h>
36 #include <got_reference.h>
37 #include <got_repository.h>
39 #include <got_cancel.h>
40 #include <got_worktree.h>
42 #include <got_commit_graph.h>
43 #include <got_blame.h>
44 #include <got_privsep.h>
45 #include <got_opentemp.h>
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 TAILQ_HEAD(headers, gw_header) gw_headers;
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct got_repository *repo;
60 struct gw_dir *gw_dir;
61 struct gotweb_config *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
65 const struct got_error *error;
66 const char *repo_name;
71 const char *repo_file;
76 unsigned int repos_total;
81 TAILQ_ENTRY(gw_header) entry;
82 struct got_reflist_head refs;
86 char *commit_id; /* id_str1 */
87 char *parent_id; /* id_str2 */
92 time_t committer_time;
96 TAILQ_ENTRY(gw_dir) entry;
138 static const char *const gw_templs[TEMPL__MAX] = {
148 static const struct kvalid gw_keys[KEY__ZMAX] = {
149 { kvalid_stringne, "action" },
150 { kvalid_stringne, "commit" },
151 { kvalid_stringne, "file" },
152 { kvalid_stringne, "folder" },
153 { kvalid_stringne, "headref" },
154 { kvalid_int, "page" },
155 { kvalid_stringne, "path" },
156 { kvalid_stringne, "prev" },
159 static struct gw_header *gw_init_header(void);
161 static void gw_free_header(struct gw_header *);
163 static int gw_template(size_t, void *);
165 static const struct got_error *gw_error(struct gw_trans *);
166 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
167 static const struct got_error *gw_get_repo_description(char **,
168 struct gw_trans *, char *);
169 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
171 static const struct got_error *gw_get_time_str(char **, time_t, int);
172 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
173 char *, const char *, int);
174 static const struct got_error *gw_output_file_blame(struct gw_trans *,
176 static const struct got_error *gw_output_blob_buf(struct gw_trans *,
178 static const struct got_error *gw_output_repo_tree(struct gw_trans *,
180 static const struct got_error *gw_output_diff(struct gw_trans *,
182 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
183 struct gw_header *, int, int);
184 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
185 static const struct got_error *gw_output_site_link(struct gw_trans *);
186 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
188 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
190 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
192 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
194 static const struct got_error *gw_gen_author_header(struct gw_trans *,
196 static const struct got_error *gw_gen_age_header(struct gw_trans *,
198 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
200 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
202 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
203 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
205 static const struct got_error *gw_display_index(struct gw_trans *);
206 static const struct got_error *gw_get_header(struct gw_trans *,
207 struct gw_header *, int);
208 static const struct got_error *gw_get_commits(struct gw_trans *,
209 struct gw_header *, int,
210 struct got_object_id *);
211 static const struct got_error *gw_get_commit(struct gw_trans *,
213 struct got_commit_object *,
214 struct got_object_id *);
215 static const struct got_error *gw_apply_unveil(const char *);
216 static const struct got_error *gw_blame_cb(void *, int, int,
217 struct got_object_id *);
218 static const struct got_error *gw_load_got_paths(struct gw_trans *);
219 static const struct got_error *gw_load_got_path(struct gw_trans *,
221 static const struct got_error *gw_parse_querystring(struct gw_trans *);
222 static const struct got_error *gw_blame(struct gw_trans *);
223 static const struct got_error *gw_blob(struct gw_trans *);
224 static const struct got_error *gw_diff(struct gw_trans *);
225 static const struct got_error *gw_index(struct gw_trans *);
226 static const struct got_error *gw_commits(struct gw_trans *);
227 static const struct got_error *gw_briefs(struct gw_trans *);
228 static const struct got_error *gw_summary(struct gw_trans *);
229 static const struct got_error *gw_tree(struct gw_trans *);
230 static const struct got_error *gw_tag(struct gw_trans *);
231 static const struct got_error *gw_tags(struct gw_trans *);
233 struct gw_query_action {
234 unsigned int func_id;
235 const char *func_name;
236 const struct got_error *(*func_main)(struct gw_trans *);
240 enum gw_query_actions {
254 static const struct gw_query_action gw_query_funcs[] = {
255 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
256 { GW_BLOB, "blob", NULL, NULL },
257 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
258 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
259 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
260 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
261 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
262 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
263 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
264 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
265 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
269 gw_get_action_name(struct gw_trans *gw_trans)
271 return gw_query_funcs[gw_trans->action].func_name;
274 static const struct got_error *
275 gw_kcgi_error(enum kcgi_err kerr)
280 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
281 return got_error(GOT_ERR_CANCELLED);
283 if (kerr == KCGI_ENOMEM)
284 return got_error_set_errno(ENOMEM,
285 kcgi_strerror(kerr));
287 if (kerr == KCGI_ENFILE)
288 return got_error_set_errno(ENFILE,
289 kcgi_strerror(kerr));
291 if (kerr == KCGI_EAGAIN)
292 return got_error_set_errno(EAGAIN,
293 kcgi_strerror(kerr));
295 if (kerr == KCGI_FORM)
296 return got_error_msg(GOT_ERR_IO,
297 kcgi_strerror(kerr));
299 return got_error_from_errno(kcgi_strerror(kerr));
302 static const struct got_error *
303 gw_apply_unveil(const char *repo_path)
305 const struct got_error *err;
308 if (unveil("gmon.out", "rwc") != 0)
309 return got_error_from_errno2("unveil", "gmon.out");
311 if (repo_path && unveil(repo_path, "r") != 0)
312 return got_error_from_errno2("unveil", repo_path);
314 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
315 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
317 err = got_privsep_unveil_exec_helpers();
321 if (unveil(NULL, NULL) != 0)
322 return got_error_from_errno("unveil");
328 isbinary(const uint8_t *buf, size_t n)
332 for (i = 0; i < n; i++)
338 static const struct got_error *
339 gw_blame(struct gw_trans *gw_trans)
341 const struct got_error *error = NULL;
342 struct gw_header *header = NULL;
344 enum kcgi_err kerr = KCGI_OK;
347 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
349 return got_error_from_errno("pledge");
351 if ((header = gw_init_header()) == NULL)
352 return got_error_from_errno("malloc");
354 error = gw_apply_unveil(gw_trans->gw_dir->path);
358 /* check querystring */
359 if (gw_trans->repo_file == NULL) {
360 error = got_error_msg(GOT_ERR_QUERYSTRING,
361 "file required in querystring");
364 if (gw_trans->commit_id == NULL) {
365 error = got_error_msg(GOT_ERR_QUERYSTRING,
366 "commit required in querystring");
370 error = gw_get_header(gw_trans, header, 1);
373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
374 "blame_header_wrapper", KATTR__MAX);
377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
378 "blame_header", KATTR__MAX);
381 error = gw_get_time_str(&age, header->committer_time,
385 error = gw_gen_age_header(gw_trans, age ?age : "");
388 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
391 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
394 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
395 "dotted_line", KATTR__MAX);
398 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
402 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
403 "blame", KATTR__MAX);
406 error = gw_output_file_blame(gw_trans, header);
409 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
411 gw_free_header(header);
412 if (error == NULL && kerr != KCGI_OK)
413 error = gw_kcgi_error(kerr);
417 static const struct got_error *
418 gw_blob(struct gw_trans *gw_trans)
420 const struct got_error *error = NULL, *err = NULL;
421 struct gw_header *header = NULL;
422 enum kcgi_err kerr = KCGI_OK;
425 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
427 return got_error_from_errno("pledge");
429 if ((header = gw_init_header()) == NULL)
430 return got_error_from_errno("malloc");
432 error = gw_apply_unveil(gw_trans->gw_dir->path);
436 /* check querystring */
437 if (gw_trans->repo_file == NULL) {
438 error = got_error_msg(GOT_ERR_QUERYSTRING,
439 "file required in querystring");
442 if (gw_trans->commit_id == NULL) {
443 error = got_error_msg(GOT_ERR_QUERYSTRING,
444 "commit required in querystring");
447 error = gw_get_header(gw_trans, header, 1);
451 error = gw_output_blob_buf(gw_trans, header);
455 gw_trans->mime = KMIME_TEXT_PLAIN;
456 err = gw_display_index(gw_trans);
461 kerr = khttp_puts(gw_trans->gw_req, error->msg);
464 gw_free_header(header);
465 if (error == NULL && kerr != KCGI_OK)
466 error = gw_kcgi_error(kerr);
470 static const struct got_error *
471 gw_diff(struct gw_trans *gw_trans)
473 const struct got_error *error = NULL;
474 struct gw_header *header = NULL;
476 enum kcgi_err kerr = KCGI_OK;
479 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
481 return got_error_from_errno("pledge");
483 if ((header = gw_init_header()) == NULL)
484 return got_error_from_errno("malloc");
486 error = gw_apply_unveil(gw_trans->gw_dir->path);
490 error = gw_get_header(gw_trans, header, 1);
494 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
495 "diff_header_wrapper", KATTR__MAX);
498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
499 "diff_header", KATTR__MAX);
502 error = gw_gen_diff_header(gw_trans, header->parent_id,
506 error = gw_gen_commit_header(gw_trans, header->commit_id,
510 error = gw_gen_tree_header(gw_trans, header->tree_id);
513 error = gw_gen_author_header(gw_trans, header->author);
516 error = gw_gen_committer_header(gw_trans, header->author);
519 error = gw_get_time_str(&age, header->committer_time,
523 error = gw_gen_age_header(gw_trans, age ?age : "");
526 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
529 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
532 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
533 "dotted_line", KATTR__MAX);
536 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
540 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
544 error = gw_output_diff(gw_trans, header);
548 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
550 gw_free_header(header);
552 if (error == NULL && kerr != KCGI_OK)
553 error = gw_kcgi_error(kerr);
557 static const struct got_error *
558 gw_index(struct gw_trans *gw_trans)
560 const struct got_error *error = NULL;
561 struct gw_dir *gw_dir = NULL;
562 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
563 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
564 char *href_tags = NULL;
565 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
566 enum kcgi_err kerr = KCGI_OK;
569 if (pledge("stdio rpath proc exec sendfd unveil",
571 error = got_error_from_errno("pledge");
575 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
579 error = gw_load_got_paths(gw_trans);
583 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
584 "index_header", KATTR__MAX);
586 return gw_kcgi_error(kerr);
587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
588 "index_header_project", KATTR__MAX);
590 return gw_kcgi_error(kerr);
591 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
593 return gw_kcgi_error(kerr);
594 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
596 return gw_kcgi_error(kerr);
598 if (gw_trans->gw_conf->got_show_repo_description) {
599 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
600 "index_header_description", KATTR__MAX);
602 return gw_kcgi_error(kerr);
603 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
605 return gw_kcgi_error(kerr);
606 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
608 return gw_kcgi_error(kerr);
611 if (gw_trans->gw_conf->got_show_repo_owner) {
612 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
613 "index_header_owner", KATTR__MAX);
615 return gw_kcgi_error(kerr);
616 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
618 return gw_kcgi_error(kerr);
619 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
621 return gw_kcgi_error(kerr);
624 if (gw_trans->gw_conf->got_show_repo_age) {
625 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
626 "index_header_age", KATTR__MAX);
628 return gw_kcgi_error(kerr);
629 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
631 return gw_kcgi_error(kerr);
632 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
634 return gw_kcgi_error(kerr);
637 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
639 return gw_kcgi_error(kerr);
641 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
642 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
643 "index_wrapper", KATTR__MAX);
645 return gw_kcgi_error(kerr);
646 kerr = khtml_printf(gw_trans->gw_html_req,
647 "No repositories found in %s",
648 gw_trans->gw_conf->got_repos_path);
650 return gw_kcgi_error(kerr);
651 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
653 return gw_kcgi_error(kerr);
654 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
655 "dotted_line", KATTR__MAX);
657 return gw_kcgi_error(kerr);
658 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
660 return gw_kcgi_error(kerr);
664 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
667 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
668 if (gw_trans->page > 0 && (gw_trans->page *
669 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
676 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
677 "index_wrapper", KATTR__MAX);
681 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
682 gw_dir->name, "action", "summary", NULL);
683 if (href_summary == NULL) {
684 error = got_error_from_errno("khttp_urlpart");
687 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
688 "index_project", KATTR__MAX);
691 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
692 href_summary, KATTR__MAX);
695 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
698 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
701 if (gw_trans->gw_conf->got_show_repo_description) {
702 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
703 KATTR_ID, "index_project_description", KATTR__MAX);
706 kerr = khtml_puts(gw_trans->gw_html_req,
707 gw_dir->description ? gw_dir->description : "");
710 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
714 if (gw_trans->gw_conf->got_show_repo_owner) {
715 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
716 KATTR_ID, "index_project_owner", KATTR__MAX);
719 kerr = khtml_puts(gw_trans->gw_html_req,
720 gw_dir->owner ? gw_dir->owner : "");
723 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
727 if (gw_trans->gw_conf->got_show_repo_age) {
728 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
729 KATTR_ID, "index_project_age", KATTR__MAX);
732 kerr = khtml_puts(gw_trans->gw_html_req,
733 gw_dir->age ? gw_dir->age : "");
736 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
741 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
742 "navs_wrapper", KATTR__MAX);
745 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
750 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
751 href_summary, KATTR__MAX);
754 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
757 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
761 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
765 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
766 gw_dir->name, "action", "briefs", NULL);
767 if (href_briefs == NULL) {
768 error = got_error_from_errno("khttp_urlpart");
771 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
772 href_briefs, KATTR__MAX);
775 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
778 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
780 error = gw_kcgi_error(kerr);
782 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
786 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
787 gw_dir->name, "action", "commits", NULL);
788 if (href_commits == NULL) {
789 error = got_error_from_errno("khttp_urlpart");
792 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
793 href_commits, KATTR__MAX);
796 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
799 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
803 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
807 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
808 gw_dir->name, "action", "tags", NULL);
809 if (href_tags == NULL) {
810 error = got_error_from_errno("khttp_urlpart");
813 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
814 href_tags, KATTR__MAX);
817 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
820 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
824 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
828 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
829 gw_dir->name, "action", "tree", NULL);
830 if (href_tree == NULL) {
831 error = got_error_from_errno("khttp_urlpart");
834 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
835 href_tree, KATTR__MAX);
838 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
842 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
845 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
846 "dotted_line", KATTR__MAX);
849 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
864 if (gw_trans->gw_conf->got_max_repos_display == 0)
867 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
868 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
869 (gw_trans->page > 0) &&
870 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
871 prev_disp == gw_trans->repos_total))) {
872 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
873 KATTR_ID, "np_wrapper", KATTR__MAX);
876 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
877 KATTR_ID, "nav_prev", KATTR__MAX);
882 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
883 (gw_trans->page > 0) &&
884 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
885 prev_disp == gw_trans->repos_total)) {
886 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
887 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
888 if (href_prev == NULL) {
889 error = got_error_from_errno("khttp_urlpartx");
892 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
893 KATTR_HREF, href_prev, KATTR__MAX);
896 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
899 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
904 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
906 return gw_kcgi_error(kerr);
908 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
909 next_disp == gw_trans->gw_conf->got_max_repos_display &&
910 dir_c != (gw_trans->page + 1) *
911 gw_trans->gw_conf->got_max_repos_display) {
912 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
913 KATTR_ID, "nav_next", KATTR__MAX);
916 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
917 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
918 if (href_next == NULL) {
919 error = got_error_from_errno("khttp_urlpartx");
922 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
923 KATTR_HREF, href_next, KATTR__MAX);
926 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
929 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
936 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
937 (gw_trans->page > 0) &&
938 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
939 prev_disp == gw_trans->repos_total)) {
940 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
954 if (error == NULL && kerr != KCGI_OK)
955 error = gw_kcgi_error(kerr);
959 static const struct got_error *
960 gw_commits(struct gw_trans *gw_trans)
962 const struct got_error *error = NULL;
963 struct gw_header *header = NULL, *n_header = NULL;
964 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
965 char *href_prev = NULL, *href_next = NULL;
966 enum kcgi_err kerr = KCGI_OK;
967 int commit_found = 0;
969 if ((header = gw_init_header()) == NULL)
970 return got_error_from_errno("malloc");
973 if (pledge("stdio rpath proc exec sendfd unveil",
975 error = got_error_from_errno("pledge");
979 error = gw_apply_unveil(gw_trans->gw_dir->path);
983 error = gw_get_header(gw_trans, header,
984 gw_trans->gw_conf->got_max_commits_display);
988 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
989 if (commit_found == 0 && gw_trans->commit_id != NULL) {
990 if (strcmp(gw_trans->commit_id,
991 n_header->commit_id) != 0)
996 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
997 "commits_line_wrapper", KATTR__MAX);
1000 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
1001 n_header->refs_str);
1004 error = gw_gen_author_header(gw_trans, n_header->author);
1007 error = gw_gen_committer_header(gw_trans, n_header->author);
1010 error = gw_get_time_str(&age, n_header->committer_time,
1014 error = gw_gen_age_header(gw_trans, age ?age : "");
1017 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1018 if (kerr != KCGI_OK)
1021 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1022 "dotted_line", KATTR__MAX);
1023 if (kerr != KCGI_OK)
1025 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1026 if (kerr != KCGI_OK)
1029 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1030 "commit", KATTR__MAX);
1031 if (kerr != KCGI_OK)
1033 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1034 if (kerr != KCGI_OK)
1036 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1037 if (kerr != KCGI_OK)
1040 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1041 gw_trans->repo_name, "action", "diff", "commit",
1042 n_header->commit_id, NULL);
1043 if (href_diff == NULL) {
1044 error = got_error_from_errno("khttp_urlpart");
1047 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1048 KATTR_ID, "navs_wrapper", KATTR__MAX);
1049 if (kerr != KCGI_OK)
1051 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1052 KATTR_ID, "navs", KATTR__MAX);
1053 if (kerr != KCGI_OK)
1055 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1056 KATTR_HREF, href_diff, KATTR__MAX);
1057 if (kerr != KCGI_OK)
1059 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1060 if (kerr != KCGI_OK)
1062 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1063 if (kerr != KCGI_OK)
1066 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1067 if (kerr != KCGI_OK)
1070 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1071 gw_trans->repo_name, "action", "tree", "commit",
1072 n_header->commit_id, NULL);
1073 if (href_tree == NULL) {
1074 error = got_error_from_errno("khttp_urlpart");
1077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1078 KATTR_HREF, href_tree, KATTR__MAX);
1079 if (kerr != KCGI_OK)
1081 khtml_puts(gw_trans->gw_html_req, "tree");
1082 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1083 if (kerr != KCGI_OK)
1085 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1086 if (kerr != KCGI_OK)
1089 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1090 "solid_line", KATTR__MAX);
1091 if (kerr != KCGI_OK)
1093 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1094 if (kerr != KCGI_OK)
1101 if (gw_trans->next_id || gw_trans->prev_id) {
1102 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1103 KATTR_ID, "np_wrapper", KATTR__MAX);
1104 if (kerr != KCGI_OK)
1106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1107 KATTR_ID, "nav_prev", KATTR__MAX);
1108 if (kerr != KCGI_OK)
1112 if (gw_trans->prev_id) {
1113 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1114 KATTRX_STRING, gw_trans->repo_name, "page",
1115 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1116 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1117 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1118 if (href_prev == NULL) {
1119 error = got_error_from_errno("khttp_urlpartx");
1122 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1123 KATTR_HREF, href_prev, KATTR__MAX);
1124 if (kerr != KCGI_OK)
1126 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1127 if (kerr != KCGI_OK)
1129 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1130 if (kerr != KCGI_OK)
1134 if (gw_trans->next_id || gw_trans->page > 0) {
1135 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1136 if (kerr != KCGI_OK)
1137 return gw_kcgi_error(kerr);
1140 if (gw_trans->next_id) {
1141 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1142 KATTR_ID, "nav_next", KATTR__MAX);
1143 if (kerr != KCGI_OK)
1145 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1146 KATTRX_STRING, gw_trans->repo_name, "page",
1147 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1148 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1149 gw_trans->next_id, NULL);
1150 if (href_next == NULL) {
1151 error = got_error_from_errno("khttp_urlpartx");
1154 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1155 KATTR_HREF, href_next, KATTR__MAX);
1156 if (kerr != KCGI_OK)
1158 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1159 if (kerr != KCGI_OK)
1161 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1162 if (kerr != KCGI_OK)
1166 if (gw_trans->next_id || gw_trans->page > 0) {
1167 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1168 if (kerr != KCGI_OK)
1172 gw_free_header(header);
1173 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1174 gw_free_header(n_header);
1180 if (error == NULL && kerr != KCGI_OK)
1181 error = gw_kcgi_error(kerr);
1185 static const struct got_error *
1186 gw_briefs(struct gw_trans *gw_trans)
1188 const struct got_error *error = NULL;
1189 struct gw_header *header = NULL, *n_header = NULL;
1190 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1191 char *href_prev = NULL, *href_next = NULL;
1192 char *newline, *smallerthan;
1193 enum kcgi_err kerr = KCGI_OK;
1194 int commit_found = 0;
1196 if ((header = gw_init_header()) == NULL)
1197 return got_error_from_errno("malloc");
1200 if (pledge("stdio rpath proc exec sendfd unveil",
1202 error = got_error_from_errno("pledge");
1206 if (gw_trans->action != GW_SUMMARY) {
1207 error = gw_apply_unveil(gw_trans->gw_dir->path);
1212 if (gw_trans->action == GW_SUMMARY)
1213 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1215 error = gw_get_header(gw_trans, header,
1216 gw_trans->gw_conf->got_max_commits_display);
1220 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1221 if (commit_found == 0 && gw_trans->commit_id != NULL) {
1222 if (strcmp(gw_trans->commit_id,
1223 n_header->commit_id) != 0)
1228 error = gw_get_time_str(&age, n_header->committer_time,
1233 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1234 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1235 if (kerr != KCGI_OK)
1238 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1239 KATTR_ID, "briefs_age", KATTR__MAX);
1240 if (kerr != KCGI_OK)
1242 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1243 if (kerr != KCGI_OK)
1245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1246 if (kerr != KCGI_OK)
1249 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1250 KATTR_ID, "briefs_author", KATTR__MAX);
1251 if (kerr != KCGI_OK)
1253 smallerthan = strchr(n_header->author, '<');
1255 *smallerthan = '\0';
1256 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1257 if (kerr != KCGI_OK)
1259 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1260 if (kerr != KCGI_OK)
1263 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1264 gw_trans->repo_name, "action", "diff", "commit",
1265 n_header->commit_id, NULL);
1266 if (href_diff == NULL) {
1267 error = got_error_from_errno("khttp_urlpart");
1270 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1271 KATTR_ID, "briefs_log", KATTR__MAX);
1272 if (kerr != KCGI_OK)
1274 newline = strchr(n_header->commit_msg, '\n');
1277 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1278 KATTR_HREF, href_diff, KATTR__MAX);
1279 if (kerr != KCGI_OK)
1281 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1282 if (kerr != KCGI_OK)
1284 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1285 if (kerr != KCGI_OK)
1288 if (n_header->refs_str) {
1289 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1290 if (kerr != KCGI_OK)
1292 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1293 KATTR_ID, "refs_str", KATTR__MAX);
1294 if (kerr != KCGI_OK)
1296 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1297 n_header->refs_str);
1298 if (kerr != KCGI_OK)
1300 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1301 if (kerr != KCGI_OK)
1305 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1306 if (kerr != KCGI_OK)
1309 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1310 KATTR_ID, "navs_wrapper", KATTR__MAX);
1311 if (kerr != KCGI_OK)
1313 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1314 KATTR_ID, "navs", KATTR__MAX);
1315 if (kerr != KCGI_OK)
1317 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1318 KATTR_HREF, href_diff, KATTR__MAX);
1319 if (kerr != KCGI_OK)
1321 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1322 if (kerr != KCGI_OK)
1324 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1325 if (kerr != KCGI_OK)
1328 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1329 if (kerr != KCGI_OK)
1332 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1333 gw_trans->repo_name, "action", "tree", "commit",
1334 n_header->commit_id, NULL);
1335 if (href_tree == NULL) {
1336 error = got_error_from_errno("khttp_urlpart");
1339 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1340 KATTR_HREF, href_tree, KATTR__MAX);
1341 if (kerr != KCGI_OK)
1343 khtml_puts(gw_trans->gw_html_req, "tree");
1344 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1345 if (kerr != KCGI_OK)
1347 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1348 if (kerr != KCGI_OK)
1351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1352 KATTR_ID, "dotted_line", KATTR__MAX);
1353 if (kerr != KCGI_OK)
1355 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1356 if (kerr != KCGI_OK)
1367 if (gw_trans->next_id || gw_trans->prev_id) {
1368 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1369 KATTR_ID, "np_wrapper", KATTR__MAX);
1370 if (kerr != KCGI_OK)
1372 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1373 KATTR_ID, "nav_prev", KATTR__MAX);
1374 if (kerr != KCGI_OK)
1378 if (gw_trans->prev_id) {
1379 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1380 KATTRX_STRING, gw_trans->repo_name, "page",
1381 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1382 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1383 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1384 if (href_prev == NULL) {
1385 error = got_error_from_errno("khttp_urlpartx");
1388 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1389 KATTR_HREF, href_prev, KATTR__MAX);
1390 if (kerr != KCGI_OK)
1392 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1393 if (kerr != KCGI_OK)
1395 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1396 if (kerr != KCGI_OK)
1400 if (gw_trans->next_id || gw_trans->page > 0) {
1401 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1402 if (kerr != KCGI_OK)
1403 return gw_kcgi_error(kerr);
1406 if (gw_trans->next_id) {
1407 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1408 KATTR_ID, "nav_next", KATTR__MAX);
1409 if (kerr != KCGI_OK)
1412 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1413 KATTRX_STRING, gw_trans->repo_name, "page",
1414 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1415 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1416 gw_trans->next_id, NULL);
1417 if (href_next == NULL) {
1418 error = got_error_from_errno("khttp_urlpartx");
1421 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1422 KATTR_HREF, href_next, KATTR__MAX);
1423 if (kerr != KCGI_OK)
1425 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1426 if (kerr != KCGI_OK)
1428 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1429 if (kerr != KCGI_OK)
1433 if (gw_trans->next_id || gw_trans->page > 0) {
1434 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1435 if (kerr != KCGI_OK)
1439 gw_free_header(header);
1440 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1441 gw_free_header(n_header);
1447 if (error == NULL && kerr != KCGI_OK)
1448 error = gw_kcgi_error(kerr);
1452 static const struct got_error *
1453 gw_summary(struct gw_trans *gw_trans)
1455 const struct got_error *error = NULL;
1457 enum kcgi_err kerr = KCGI_OK;
1460 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1461 return got_error_from_errno("pledge");
1463 error = gw_apply_unveil(gw_trans->gw_dir->path);
1467 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1468 "summary_wrapper", KATTR__MAX);
1469 if (kerr != KCGI_OK)
1470 return gw_kcgi_error(kerr);
1472 if (gw_trans->gw_conf->got_show_repo_description &&
1473 gw_trans->gw_dir->description != NULL &&
1474 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1475 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1476 KATTR_ID, "description_title", KATTR__MAX);
1477 if (kerr != KCGI_OK)
1479 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1480 if (kerr != KCGI_OK)
1482 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1483 if (kerr != KCGI_OK)
1485 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1486 KATTR_ID, "description", KATTR__MAX);
1487 if (kerr != KCGI_OK)
1489 kerr = khtml_puts(gw_trans->gw_html_req,
1490 gw_trans->gw_dir->description);
1491 if (kerr != KCGI_OK)
1493 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1494 if (kerr != KCGI_OK)
1498 if (gw_trans->gw_conf->got_show_repo_owner &&
1499 gw_trans->gw_dir->owner != NULL &&
1500 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1501 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1502 KATTR_ID, "repo_owner_title", KATTR__MAX);
1503 if (kerr != KCGI_OK)
1505 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1506 if (kerr != KCGI_OK)
1508 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1509 if (kerr != KCGI_OK)
1511 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1512 KATTR_ID, "repo_owner", KATTR__MAX);
1513 if (kerr != KCGI_OK)
1515 kerr = khtml_puts(gw_trans->gw_html_req,
1516 gw_trans->gw_dir->owner);
1517 if (kerr != KCGI_OK)
1519 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1520 if (kerr != KCGI_OK)
1524 if (gw_trans->gw_conf->got_show_repo_age) {
1525 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1530 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1531 KATTR_ID, "last_change_title", KATTR__MAX);
1532 if (kerr != KCGI_OK)
1534 kerr = khtml_puts(gw_trans->gw_html_req,
1536 if (kerr != KCGI_OK)
1538 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1539 if (kerr != KCGI_OK)
1541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1542 KATTR_ID, "last_change", KATTR__MAX);
1543 if (kerr != KCGI_OK)
1545 kerr = khtml_puts(gw_trans->gw_html_req, age);
1546 if (kerr != KCGI_OK)
1548 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1549 if (kerr != KCGI_OK)
1554 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1555 gw_trans->gw_dir->url != NULL &&
1556 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1557 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1558 KATTR_ID, "cloneurl_title", KATTR__MAX);
1559 if (kerr != KCGI_OK)
1561 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1562 if (kerr != KCGI_OK)
1564 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1565 if (kerr != KCGI_OK)
1567 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1568 KATTR_ID, "cloneurl", KATTR__MAX);
1569 if (kerr != KCGI_OK)
1571 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1572 if (kerr != KCGI_OK)
1574 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1575 if (kerr != KCGI_OK)
1579 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1580 if (kerr != KCGI_OK)
1583 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1584 "briefs_title_wrapper", KATTR__MAX);
1585 if (kerr != KCGI_OK)
1587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1588 "briefs_title", KATTR__MAX);
1589 if (kerr != KCGI_OK)
1591 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1592 if (kerr != KCGI_OK)
1594 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1595 if (kerr != KCGI_OK)
1597 error = gw_briefs(gw_trans);
1601 error = gw_tags(gw_trans);
1605 error = gw_output_repo_heads(gw_trans);
1608 if (error == NULL && kerr != KCGI_OK)
1609 error = gw_kcgi_error(kerr);
1613 static const struct got_error *
1614 gw_tree(struct gw_trans *gw_trans)
1616 const struct got_error *error = NULL;
1617 struct gw_header *header = NULL;
1618 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1620 enum kcgi_err kerr = KCGI_OK;
1623 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1624 return got_error_from_errno("pledge");
1626 if ((header = gw_init_header()) == NULL)
1627 return got_error_from_errno("malloc");
1629 error = gw_apply_unveil(gw_trans->gw_dir->path);
1633 error = gw_get_header(gw_trans, header, 1);
1637 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1638 "tree_header_wrapper", KATTR__MAX);
1639 if (kerr != KCGI_OK)
1641 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1642 "tree_header", KATTR__MAX);
1643 if (kerr != KCGI_OK)
1645 error = gw_gen_tree_header(gw_trans, header->tree_id);
1648 error = gw_get_time_str(&age, header->committer_time,
1652 error = gw_gen_age_header(gw_trans, age ?age : "");
1655 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1658 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1659 if (kerr != KCGI_OK)
1661 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1662 "dotted_line", KATTR__MAX);
1663 if (kerr != KCGI_OK)
1665 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1666 if (kerr != KCGI_OK)
1669 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1670 "tree", KATTR__MAX);
1671 if (kerr != KCGI_OK)
1673 error = gw_output_repo_tree(gw_trans, header);
1677 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1679 gw_free_header(header);
1680 free(tree_html_disp);
1684 if (error == NULL && kerr != KCGI_OK)
1685 error = gw_kcgi_error(kerr);
1689 static const struct got_error *
1690 gw_tags(struct gw_trans *gw_trans)
1692 const struct got_error *error = NULL;
1693 struct gw_header *header = NULL;
1694 char *href_next = NULL, *href_prev = NULL;
1695 enum kcgi_err kerr = KCGI_OK;
1698 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1699 return got_error_from_errno("pledge");
1701 if ((header = gw_init_header()) == NULL)
1702 return got_error_from_errno("malloc");
1704 if (gw_trans->action != GW_SUMMARY) {
1705 error = gw_apply_unveil(gw_trans->gw_dir->path);
1710 error = gw_get_header(gw_trans, header, 1);
1714 if (gw_trans->action == GW_SUMMARY) {
1715 gw_trans->next_id = NULL;
1716 error = gw_output_repo_tags(gw_trans, header,
1717 D_MAXSLCOMMDISP, TAGBRIEF);
1721 error = gw_output_repo_tags(gw_trans, header,
1722 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1727 if (gw_trans->next_id || gw_trans->page > 0) {
1728 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1729 KATTR_ID, "np_wrapper", KATTR__MAX);
1730 if (kerr != KCGI_OK)
1732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1733 KATTR_ID, "nav_prev", KATTR__MAX);
1734 if (kerr != KCGI_OK)
1738 if (gw_trans->prev_id) {
1739 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1740 KATTRX_STRING, gw_trans->repo_name, "page",
1741 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1742 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1743 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1744 if (href_prev == NULL) {
1745 error = got_error_from_errno("khttp_urlpartx");
1748 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1749 KATTR_HREF, href_prev, KATTR__MAX);
1750 if (kerr != KCGI_OK)
1752 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1753 if (kerr != KCGI_OK)
1755 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1756 if (kerr != KCGI_OK)
1760 if (gw_trans->next_id || gw_trans->page > 0) {
1761 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1762 if (kerr != KCGI_OK)
1763 return gw_kcgi_error(kerr);
1766 if (gw_trans->next_id) {
1767 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1768 KATTR_ID, "nav_next", KATTR__MAX);
1769 if (kerr != KCGI_OK)
1771 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1772 KATTRX_STRING, gw_trans->repo_name, "page",
1773 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1774 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1775 gw_trans->next_id, NULL);
1776 if (href_next == NULL) {
1777 error = got_error_from_errno("khttp_urlpartx");
1780 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1781 KATTR_HREF, href_next, KATTR__MAX);
1782 if (kerr != KCGI_OK)
1784 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1785 if (kerr != KCGI_OK)
1787 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1788 if (kerr != KCGI_OK)
1792 if (gw_trans->next_id || gw_trans->page > 0) {
1793 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1794 if (kerr != KCGI_OK)
1798 gw_free_header(header);
1801 if (error == NULL && kerr != KCGI_OK)
1802 error = gw_kcgi_error(kerr);
1806 static const struct got_error *
1807 gw_tag(struct gw_trans *gw_trans)
1809 const struct got_error *error = NULL;
1810 struct gw_header *header = NULL;
1811 enum kcgi_err kerr = KCGI_OK;
1814 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1815 return got_error_from_errno("pledge");
1817 if ((header = gw_init_header()) == NULL)
1818 return got_error_from_errno("malloc");
1820 error = gw_apply_unveil(gw_trans->gw_dir->path);
1824 if (gw_trans->commit_id == NULL) {
1825 error = got_error_msg(GOT_ERR_QUERYSTRING,
1826 "commit required in querystring");
1830 error = gw_get_header(gw_trans, header, 1);
1834 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1835 "tag_header_wrapper", KATTR__MAX);
1836 if (kerr != KCGI_OK)
1838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1839 "tag_header", KATTR__MAX);
1840 if (kerr != KCGI_OK)
1842 error = gw_gen_commit_header(gw_trans, header->commit_id,
1846 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1849 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1850 if (kerr != KCGI_OK)
1852 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1853 "dotted_line", KATTR__MAX);
1854 if (kerr != KCGI_OK)
1856 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1857 if (kerr != KCGI_OK)
1860 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1861 "tree", KATTR__MAX);
1862 if (kerr != KCGI_OK)
1865 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1869 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1871 gw_free_header(header);
1872 if (error == NULL && kerr != KCGI_OK)
1873 error = gw_kcgi_error(kerr);
1877 static const struct got_error *
1878 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1880 const struct got_error *error = NULL;
1885 if (asprintf(&dir_test, "%s/%s/%s",
1886 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1887 GOTWEB_GIT_DIR) == -1)
1888 return got_error_from_errno("asprintf");
1890 dt = opendir(dir_test);
1894 gw_dir->path = strdup(dir_test);
1895 if (gw_dir->path == NULL) {
1897 error = got_error_from_errno("strdup");
1904 if (asprintf(&dir_test, "%s/%s/%s",
1905 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1906 GOTWEB_GOT_DIR) == -1) {
1908 error = got_error_from_errno("asprintf");
1912 dt = opendir(dir_test);
1917 error = got_error(GOT_ERR_NOT_GIT_REPO);
1921 if (asprintf(&dir_test, "%s/%s",
1922 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1923 error = got_error_from_errno("asprintf");
1928 gw_dir->path = strdup(dir_test);
1929 if (gw_dir->path == NULL) {
1931 error = got_error_from_errno("strdup");
1935 dt = opendir(dir_test);
1937 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1942 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1946 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1949 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1953 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1957 if (dt && closedir(dt) == -1 && error == NULL)
1958 error = got_error_from_errno("closedir");
1962 static const struct got_error *
1963 gw_load_got_paths(struct gw_trans *gw_trans)
1965 const struct got_error *error = NULL;
1967 struct dirent **sd_dent;
1968 struct gw_dir *gw_dir;
1970 unsigned int d_cnt, d_i;
1972 d = opendir(gw_trans->gw_conf->got_repos_path);
1974 error = got_error_from_errno2("opendir",
1975 gw_trans->gw_conf->got_repos_path);
1979 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1982 error = got_error_from_errno2("scandir",
1983 gw_trans->gw_conf->got_repos_path);
1987 for (d_i = 0; d_i < d_cnt; d_i++) {
1988 if (gw_trans->gw_conf->got_max_repos > 0 &&
1989 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1990 break; /* account for parent and self */
1992 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1993 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1996 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
2000 error = gw_load_got_path(gw_trans, gw_dir);
2001 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
2004 } else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
2007 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
2008 !got_path_dir_is_empty(gw_dir->path)) {
2009 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
2011 gw_trans->repos_total++;
2015 if (d && closedir(d) == -1 && error == NULL)
2016 error = got_error_from_errno("closedir");
2020 static const struct got_error *
2021 gw_parse_querystring(struct gw_trans *gw_trans)
2023 const struct got_error *error = NULL;
2025 const struct gw_query_action *action = NULL;
2028 if (gw_trans->gw_req->fieldnmap[0]) {
2029 return got_error(GOT_ERR_QUERYSTRING);
2030 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2031 /* define gw_trans->repo_path */
2032 gw_trans->repo_name = p->parsed.s;
2034 if (asprintf(&gw_trans->repo_path, "%s/%s",
2035 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2036 return got_error_from_errno("asprintf");
2038 /* get action and set function */
2039 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2040 for (i = 0; i < nitems(gw_query_funcs); i++) {
2041 action = &gw_query_funcs[i];
2042 if (action->func_name == NULL)
2044 if (strcmp(action->func_name,
2045 p->parsed.s) == 0) {
2046 gw_trans->action = i;
2051 if (gw_trans->action == -1) {
2052 gw_trans->action = GW_ERR;
2053 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2054 p != NULL ? "bad action in querystring" :
2055 "no action in querystring");
2059 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2060 if (asprintf(&gw_trans->commit_id, "%s",
2062 return got_error_from_errno("asprintf");
2065 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2066 gw_trans->repo_file = p->parsed.s;
2068 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2069 if (asprintf(&gw_trans->repo_folder, "%s",
2071 return got_error_from_errno("asprintf");
2074 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2075 if (asprintf(&gw_trans->prev_id, "%s",
2077 return got_error_from_errno("asprintf");
2080 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2081 gw_trans->headref = p->parsed.s;
2083 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2087 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2089 gw_trans->action = GW_INDEX;
2091 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2092 gw_trans->page = p->parsed.i;
2097 static const struct got_error *
2098 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2100 const struct got_error *error;
2102 *gw_dir = malloc(sizeof(**gw_dir));
2103 if (*gw_dir == NULL)
2104 return got_error_from_errno("malloc");
2106 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2107 error = got_error_from_errno("asprintf");
2116 static const struct got_error *
2117 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2119 enum kcgi_err kerr = KCGI_OK;
2121 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2122 if (kerr != KCGI_OK)
2123 return gw_kcgi_error(kerr);
2124 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2126 if (kerr != KCGI_OK)
2127 return gw_kcgi_error(kerr);
2128 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2130 if (kerr != KCGI_OK)
2131 return gw_kcgi_error(kerr);
2132 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2134 if (kerr != KCGI_OK)
2135 return gw_kcgi_error(kerr);
2136 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2137 if (kerr != KCGI_OK)
2138 return gw_kcgi_error(kerr);
2139 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2141 if (kerr != KCGI_OK)
2142 return gw_kcgi_error(kerr);
2144 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2145 kerr = khttp_head(gw_trans->gw_req,
2146 kresps[KRESP_CONTENT_DISPOSITION],
2147 "attachment; filename=%s", gw_trans->repo_file);
2148 if (kerr != KCGI_OK)
2149 return gw_kcgi_error(kerr);
2152 kerr = khttp_body(gw_trans->gw_req);
2153 return gw_kcgi_error(kerr);
2156 static const struct got_error *
2157 gw_display_index(struct gw_trans *gw_trans)
2159 const struct got_error *error;
2160 enum kcgi_err kerr = KCGI_OK;
2162 /* catch early querystring errors */
2163 if (gw_trans->error)
2164 gw_trans->action = GW_ERR;
2166 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2170 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2171 if (kerr != KCGI_OK)
2172 return gw_kcgi_error(kerr);
2174 if (gw_trans->action != GW_BLOB) {
2175 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2176 gw_query_funcs[gw_trans->action].template);
2177 if (kerr != KCGI_OK) {
2178 khtml_close(gw_trans->gw_html_req);
2179 return gw_kcgi_error(kerr);
2183 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2186 static const struct got_error *
2187 gw_error(struct gw_trans *gw_trans)
2189 enum kcgi_err kerr = KCGI_OK;
2191 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2193 return gw_kcgi_error(kerr);
2197 gw_template(size_t key, void *arg)
2199 const struct got_error *error = NULL;
2200 enum kcgi_err kerr = KCGI_OK;
2201 struct gw_trans *gw_trans = arg;
2202 char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
2203 char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
2205 if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
2206 "/apple-touch-icon.png") == -1)
2208 if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
2209 "/favicon-32x32.png") == -1)
2211 if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
2212 "/favicon-16x16.png") == -1)
2214 if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
2215 "/site.webmanifest") == -1)
2217 if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
2218 "/safari-pinned-tab.svg") == -1)
2220 if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
2221 "/gotweb.css") == -1)
2223 if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
2224 gw_trans->gw_conf->got_www_path ? "/" : "",
2225 gw_trans->gw_conf->got_logo) == -1)
2230 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2231 KATTR_NAME, "viewport",
2232 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2234 if (kerr != KCGI_OK)
2236 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2237 if (kerr != KCGI_OK)
2239 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2240 KATTR_CHARSET, "utf-8",
2242 if (kerr != KCGI_OK)
2244 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2245 if (kerr != KCGI_OK)
2247 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2248 KATTR_NAME, "msapplication-TileColor",
2249 KATTR_CONTENT, "#da532c", KATTR__MAX);
2250 if (kerr != KCGI_OK)
2252 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2253 if (kerr != KCGI_OK)
2255 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2256 KATTR_NAME, "theme-color",
2257 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2258 if (kerr != KCGI_OK)
2260 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2261 if (kerr != KCGI_OK)
2263 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2264 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2265 KATTR_HREF, ati, KATTR__MAX);
2266 if (kerr != KCGI_OK)
2268 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2269 if (kerr != KCGI_OK)
2271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2272 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2273 "32x32", KATTR_HREF, fic32, KATTR__MAX);
2274 if (kerr != KCGI_OK)
2276 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2277 if (kerr != KCGI_OK)
2279 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2280 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2281 "16x16", KATTR_HREF, fic16, KATTR__MAX);
2282 if (kerr != KCGI_OK)
2284 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2285 if (kerr != KCGI_OK)
2287 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2288 KATTR_REL, "manifest", KATTR_HREF, swm,
2290 if (kerr != KCGI_OK)
2292 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2293 if (kerr != KCGI_OK)
2295 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2296 KATTR_REL, "mask-icon", KATTR_HREF,
2298 if (kerr != KCGI_OK)
2300 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2301 if (kerr != KCGI_OK)
2303 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2304 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2305 KATTR_HREF, css, KATTR__MAX);
2306 if (kerr != KCGI_OK)
2308 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2309 if (kerr != KCGI_OK)
2313 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2314 KATTR_ID, "got_link", KATTR__MAX);
2315 if (kerr != KCGI_OK)
2317 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2318 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2319 KATTR_TARGET, "_sotd", KATTR__MAX);
2320 if (kerr != KCGI_OK)
2322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2323 KATTR_SRC, logo, KATTR__MAX);
2324 if (kerr != KCGI_OK)
2326 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2327 if (kerr != KCGI_OK)
2330 case (TEMPL_SITEPATH):
2331 error = gw_output_site_link(gw_trans);
2336 if (gw_trans->gw_conf->got_site_name != NULL) {
2337 kerr = khtml_puts(gw_trans->gw_html_req,
2338 gw_trans->gw_conf->got_site_name);
2339 if (kerr != KCGI_OK)
2343 case (TEMPL_SEARCH):
2345 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2346 "search", KATTR__MAX);
2347 if (kerr != KCGI_OK)
2349 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2350 KATTR_METHOD, "POST", KATTR__MAX);
2351 if (kerr != KCGI_OK)
2353 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2354 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2355 KATTR_MAXLENGTH, "50", KATTR__MAX);
2356 if (kerr != KCGI_OK)
2358 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2360 if (kerr != KCGI_OK)
2362 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2363 if (kerr != KCGI_OK)
2365 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2366 if (kerr != KCGI_OK)
2369 case(TEMPL_SITEOWNER):
2370 if (gw_trans->gw_conf->got_site_owner != NULL &&
2371 gw_trans->gw_conf->got_show_site_owner) {
2372 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2373 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2374 if (kerr != KCGI_OK)
2376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2377 KATTR_ID, "site_owner", KATTR__MAX);
2378 if (kerr != KCGI_OK)
2380 kerr = khtml_puts(gw_trans->gw_html_req,
2381 gw_trans->gw_conf->got_site_owner);
2382 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2383 if (kerr != KCGI_OK)
2387 case(TEMPL_CONTENT):
2388 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2390 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2391 KATTR_ID, "tmpl_err", KATTR__MAX);
2392 if (kerr != KCGI_OK)
2394 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2396 if (kerr != KCGI_OK)
2398 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2399 if (kerr != KCGI_OK)
2425 static const struct got_error *
2426 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2428 const struct got_error *error = NULL;
2429 enum kcgi_err kerr = KCGI_OK;
2431 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2432 KATTR_ID, "header_commit_title", KATTR__MAX);
2433 if (kerr != KCGI_OK)
2435 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2436 if (kerr != KCGI_OK)
2438 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2439 if (kerr != KCGI_OK)
2441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2442 KATTR_ID, "header_commit", KATTR__MAX);
2443 if (kerr != KCGI_OK)
2445 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2446 if (kerr != KCGI_OK)
2449 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2450 KATTR_ID, "refs_str", KATTR__MAX);
2451 if (kerr != KCGI_OK)
2453 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2454 if (kerr != KCGI_OK)
2456 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2457 if (kerr != KCGI_OK)
2460 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2462 if (error == NULL && kerr != KCGI_OK)
2463 error = gw_kcgi_error(kerr);
2467 static const struct got_error *
2468 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2470 const struct got_error *error = NULL;
2471 enum kcgi_err kerr = KCGI_OK;
2473 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2474 KATTR_ID, "header_diff_title", KATTR__MAX);
2475 if (kerr != KCGI_OK)
2477 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2478 if (kerr != KCGI_OK)
2480 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2481 if (kerr != KCGI_OK)
2483 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2484 KATTR_ID, "header_diff", KATTR__MAX);
2485 if (kerr != KCGI_OK)
2488 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2489 if (kerr != KCGI_OK)
2492 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2493 if (kerr != KCGI_OK)
2495 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2496 if (kerr != KCGI_OK)
2498 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2500 if (error == NULL && kerr != KCGI_OK)
2501 error = gw_kcgi_error(kerr);
2505 static const struct got_error *
2506 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2508 const struct got_error *error = NULL;
2509 enum kcgi_err kerr = KCGI_OK;
2511 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2512 KATTR_ID, "header_age_title", KATTR__MAX);
2513 if (kerr != KCGI_OK)
2515 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2516 if (kerr != KCGI_OK)
2518 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2519 if (kerr != KCGI_OK)
2521 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2522 KATTR_ID, "header_age", KATTR__MAX);
2523 if (kerr != KCGI_OK)
2525 kerr = khtml_puts(gw_trans->gw_html_req, str);
2526 if (kerr != KCGI_OK)
2528 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2530 if (error == NULL && kerr != KCGI_OK)
2531 error = gw_kcgi_error(kerr);
2535 static const struct got_error *
2536 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2538 const struct got_error *error = NULL;
2539 enum kcgi_err kerr = KCGI_OK;
2541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2542 KATTR_ID, "header_author_title", KATTR__MAX);
2543 if (kerr != KCGI_OK)
2545 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2546 if (kerr != KCGI_OK)
2548 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2549 if (kerr != KCGI_OK)
2551 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2552 KATTR_ID, "header_author", KATTR__MAX);
2553 if (kerr != KCGI_OK)
2555 kerr = khtml_puts(gw_trans->gw_html_req, str);
2556 if (kerr != KCGI_OK)
2558 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2560 if (error == NULL && kerr != KCGI_OK)
2561 error = gw_kcgi_error(kerr);
2565 static const struct got_error *
2566 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2568 const struct got_error *error = NULL;
2569 enum kcgi_err kerr = KCGI_OK;
2571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2572 KATTR_ID, "header_committer_title", KATTR__MAX);
2573 if (kerr != KCGI_OK)
2575 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2576 if (kerr != KCGI_OK)
2578 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2579 if (kerr != KCGI_OK)
2581 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2582 KATTR_ID, "header_committer", KATTR__MAX);
2583 if (kerr != KCGI_OK)
2585 kerr = khtml_puts(gw_trans->gw_html_req, str);
2586 if (kerr != KCGI_OK)
2588 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2590 if (error == NULL && kerr != KCGI_OK)
2591 error = gw_kcgi_error(kerr);
2595 static const struct got_error *
2596 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2598 const struct got_error *error = NULL;
2599 enum kcgi_err kerr = KCGI_OK;
2601 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2602 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2603 if (kerr != KCGI_OK)
2605 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2606 if (kerr != KCGI_OK)
2608 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2609 if (kerr != KCGI_OK)
2611 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2612 KATTR_ID, "header_commit_msg", KATTR__MAX);
2613 if (kerr != KCGI_OK)
2615 kerr = khttp_puts(gw_trans->gw_req, str);
2616 if (kerr != KCGI_OK)
2618 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2620 if (error == NULL && kerr != KCGI_OK)
2621 error = gw_kcgi_error(kerr);
2625 static const struct got_error *
2626 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2628 const struct got_error *error = NULL;
2629 enum kcgi_err kerr = KCGI_OK;
2631 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2632 KATTR_ID, "header_tree_title", KATTR__MAX);
2633 if (kerr != KCGI_OK)
2635 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2636 if (kerr != KCGI_OK)
2638 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2639 if (kerr != KCGI_OK)
2641 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2642 KATTR_ID, "header_tree", KATTR__MAX);
2643 if (kerr != KCGI_OK)
2645 kerr = khtml_puts(gw_trans->gw_html_req, str);
2646 if (kerr != KCGI_OK)
2648 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2650 if (error == NULL && kerr != KCGI_OK)
2651 error = gw_kcgi_error(kerr);
2655 static const struct got_error *
2656 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2659 const struct got_error *error = NULL;
2661 char *d_file = NULL;
2665 *description = NULL;
2666 if (gw_trans->gw_conf->got_show_repo_description == 0)
2669 if (asprintf(&d_file, "%s/description", dir) == -1)
2670 return got_error_from_errno("asprintf");
2672 f = fopen(d_file, "re");
2674 if (errno == ENOENT || errno == EACCES)
2676 error = got_error_from_errno2("fopen", d_file);
2680 if (fseek(f, 0, SEEK_END) == -1) {
2681 error = got_ferror(f, GOT_ERR_IO);
2686 error = got_ferror(f, GOT_ERR_IO);
2689 if (fseek(f, 0, SEEK_SET) == -1) {
2690 error = got_ferror(f, GOT_ERR_IO);
2693 *description = calloc(len + 1, sizeof(**description));
2694 if (*description == NULL) {
2695 error = got_error_from_errno("calloc");
2699 n = fread(*description, 1, len, f);
2700 if (n == 0 && ferror(f))
2701 error = got_ferror(f, GOT_ERR_IO);
2703 if (f != NULL && fclose(f) == EOF && error == NULL)
2704 error = got_error_from_errno("fclose");
2709 static const struct got_error *
2710 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2714 char *years = "years ago", *months = "months ago";
2715 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2716 char *minutes = "minutes ago", *seconds = "seconds ago";
2717 char *now = "right now";
2725 diff_time = time(NULL) - committer_time;
2726 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2727 if (asprintf(repo_age, "%lld %s",
2728 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2729 return got_error_from_errno("asprintf");
2730 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2731 if (asprintf(repo_age, "%lld %s",
2732 (diff_time / 60 / 60 / 24 / (365 / 12)),
2734 return got_error_from_errno("asprintf");
2735 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2736 if (asprintf(repo_age, "%lld %s",
2737 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2738 return got_error_from_errno("asprintf");
2739 } else if (diff_time > 60 * 60 * 24 * 2) {
2740 if (asprintf(repo_age, "%lld %s",
2741 (diff_time / 60 / 60 / 24), days) == -1)
2742 return got_error_from_errno("asprintf");
2743 } else if (diff_time > 60 * 60 * 2) {
2744 if (asprintf(repo_age, "%lld %s",
2745 (diff_time / 60 / 60), hours) == -1)
2746 return got_error_from_errno("asprintf");
2747 } else if (diff_time > 60 * 2) {
2748 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2750 return got_error_from_errno("asprintf");
2751 } else if (diff_time > 2) {
2752 if (asprintf(repo_age, "%lld %s", diff_time,
2754 return got_error_from_errno("asprintf");
2756 if (asprintf(repo_age, "%s", now) == -1)
2757 return got_error_from_errno("asprintf");
2761 if (gmtime_r(&committer_time, &tm) == NULL)
2762 return got_error_from_errno("gmtime_r");
2764 s = asctime_r(&tm, datebuf);
2766 return got_error_from_errno("asctime_r");
2768 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2769 return got_error_from_errno("asprintf");
2775 static const struct got_error *
2776 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2777 const char *refname, int ref_tm)
2779 const struct got_error *error = NULL;
2780 struct got_repository *repo = NULL;
2781 struct got_commit_object *commit = NULL;
2782 struct got_reflist_head refs;
2783 struct got_reflist_entry *re;
2784 time_t committer_time = 0, cmp_time = 0;
2789 if (gw_trans->gw_conf->got_show_repo_age == 0)
2793 repo = gw_trans->repo;
2795 error = got_repo_open(&repo, dir, NULL);
2800 error = got_ref_list(&refs, repo, "refs/heads",
2801 got_ref_cmp_by_name, NULL);
2806 * Find the youngest branch tip in the repository, or the age of
2807 * the a specific branch tip if a name was provided by the caller.
2809 TAILQ_FOREACH(re, &refs, entry) {
2810 struct got_object_id *id = NULL;
2812 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2815 error = got_ref_resolve(&id, repo, re->ref);
2819 error = got_object_open_as_commit(&commit, repo, id);
2825 got_object_commit_get_committer_time(commit);
2826 got_object_commit_close(commit);
2827 if (cmp_time < committer_time)
2828 cmp_time = committer_time;
2834 if (cmp_time != 0) {
2835 committer_time = cmp_time;
2836 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2839 got_ref_list_free(&refs);
2840 if (gw_trans->repo == NULL) {
2841 const struct got_error *close_err = got_repo_close(repo);
2848 static const struct got_error *
2849 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2851 const struct got_error *error;
2853 struct got_object_id *id1 = NULL, *id2 = NULL;
2854 char *label1 = NULL, *label2 = NULL, *line = NULL;
2856 size_t linesize = 0;
2858 enum kcgi_err kerr = KCGI_OK;
2864 if (header->parent_id != NULL &&
2865 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2866 error = got_repo_match_object_id(&id1, &label1,
2867 header->parent_id, GOT_OBJ_TYPE_ANY,
2868 &header->refs, gw_trans->repo);
2873 error = got_repo_match_object_id(&id2, &label2,
2874 header->commit_id, GOT_OBJ_TYPE_ANY, &header->refs,
2879 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2883 case GOT_OBJ_TYPE_BLOB:
2884 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
2885 NULL, NULL, 3, 0, 0, gw_trans->repo, f);
2887 case GOT_OBJ_TYPE_TREE:
2888 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
2889 NULL, "", "", 3, 0, 0, gw_trans->repo, f);
2891 case GOT_OBJ_TYPE_COMMIT:
2892 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
2893 NULL, 3, 0, 0, gw_trans->repo, f);
2896 error = got_error(GOT_ERR_OBJ_TYPE);
2901 if (fseek(f, 0, SEEK_SET) == -1) {
2902 error = got_ferror(f, GOT_ERR_IO);
2906 while ((linelen = getline(&line, &linesize, f)) != -1) {
2907 error = gw_colordiff_line(gw_trans, line);
2910 /* XXX: KHTML_PRETTY breaks this */
2911 kerr = khtml_puts(gw_trans->gw_html_req, line);
2912 if (kerr != KCGI_OK)
2914 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2915 if (kerr != KCGI_OK)
2918 if (linelen == -1 && ferror(f))
2919 error = got_error_from_errno("getline");
2921 if (f && fclose(f) == EOF && error == NULL)
2922 error = got_error_from_errno("fclose");
2929 if (error == NULL && kerr != KCGI_OK)
2930 error = gw_kcgi_error(kerr);
2934 static const struct got_error *
2935 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2937 const struct got_error *error = NULL, *close_err;
2938 struct got_repository *repo;
2939 const char *gitconfig_owner;
2943 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2946 error = got_repo_open(&repo, dir, NULL);
2949 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2950 if (gitconfig_owner) {
2951 *owner = strdup(gitconfig_owner);
2953 error = got_error_from_errno("strdup");
2955 close_err = got_repo_close(repo);
2961 static const struct got_error *
2962 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2964 const struct got_error *error = NULL;
2966 char *d_file = NULL;
2972 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2973 return got_error_from_errno("asprintf");
2975 f = fopen(d_file, "re");
2977 if (errno != ENOENT && errno != EACCES)
2978 error = got_error_from_errno2("fopen", d_file);
2982 if (fseek(f, 0, SEEK_END) == -1) {
2983 error = got_ferror(f, GOT_ERR_IO);
2988 error = got_ferror(f, GOT_ERR_IO);
2991 if (fseek(f, 0, SEEK_SET) == -1) {
2992 error = got_ferror(f, GOT_ERR_IO);
2996 *url = calloc(len + 1, sizeof(**url));
2998 error = got_error_from_errno("calloc");
3002 n = fread(*url, 1, len, f);
3003 if (n == 0 && ferror(f))
3004 error = got_ferror(f, GOT_ERR_IO);
3006 if (f && fclose(f) == EOF && error == NULL)
3007 error = got_error_from_errno("fclose");
3012 static const struct got_error *
3013 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3014 int limit, int tag_type)
3016 const struct got_error *error = NULL;
3017 struct got_reflist_head refs;
3018 struct got_reflist_entry *re;
3020 char *id_str = NULL, *newline, *href_commits = NULL;
3021 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3022 struct got_tag_object *tag = NULL;
3023 enum kcgi_err kerr = KCGI_OK;
3024 int summary_header_displayed = 0, chk_next = 0;
3025 int tag_count = 0, commit_found = 0, c_cnt = 0;
3029 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3030 got_ref_cmp_tags, gw_trans->repo);
3034 TAILQ_FOREACH(re, &refs, entry) {
3035 const char *refname;
3037 const char *tag_commit;
3039 struct got_object_id *id;
3040 struct got_commit_object *commit = NULL;
3042 refname = got_ref_get_name(re->ref);
3043 if (strncmp(refname, "refs/tags/", 10) != 0)
3047 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3051 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3053 if (error->code != GOT_ERR_OBJ_TYPE) {
3057 /* "lightweight" tag */
3058 error = got_object_open_as_commit(&commit,
3059 gw_trans->repo, id);
3064 tagger = got_object_commit_get_committer(commit);
3066 got_object_commit_get_committer_time(commit);
3067 error = got_object_id_str(&id_str, id);
3071 tagger = got_object_tag_get_tagger(tag);
3072 tagger_time = got_object_tag_get_tagger_time(tag);
3073 error = got_object_id_str(&id_str,
3074 got_object_tag_get_object_id(tag));
3079 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3080 strlen(id_str)) != 0)
3083 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3084 commit_found == 0 && strncmp(id_str, gw_trans->commit_id,
3085 strlen(id_str)) != 0)
3093 gw_trans->next_id = strdup(id_str);
3094 if (gw_trans->next_id == NULL)
3095 error = got_error_from_errno("strdup");
3100 error = got_object_commit_get_logmsg(&tag_commit0,
3104 got_object_commit_close(commit);
3106 tag_commit0 = strdup(got_object_tag_get_message(tag));
3107 if (tag_commit0 == NULL) {
3108 error = got_error_from_errno("strdup");
3113 tag_commit = tag_commit0;
3114 while (*tag_commit == '\n')
3119 newline = strchr(tag_commit, '\n');
3123 if (summary_header_displayed == 0) {
3124 kerr = khtml_attr(gw_trans->gw_html_req,
3125 KELEM_DIV, KATTR_ID,
3126 "summary_tags_title_wrapper", KATTR__MAX);
3127 if (kerr != KCGI_OK)
3129 kerr = khtml_attr(gw_trans->gw_html_req,
3130 KELEM_DIV, KATTR_ID,
3131 "summary_tags_title", KATTR__MAX);
3132 if (kerr != KCGI_OK)
3134 kerr = khtml_puts(gw_trans->gw_html_req,
3136 if (kerr != KCGI_OK)
3138 kerr = khtml_closeelem(gw_trans->gw_html_req,
3140 if (kerr != KCGI_OK)
3142 kerr = khtml_attr(gw_trans->gw_html_req,
3143 KELEM_DIV, KATTR_ID,
3144 "summary_tags_content", KATTR__MAX);
3145 if (kerr != KCGI_OK)
3147 summary_header_displayed = 1;
3150 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3151 KATTR_ID, "tag_wrapper", KATTR__MAX);
3152 if (kerr != KCGI_OK)
3154 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3155 KATTR_ID, "tag_age", KATTR__MAX);
3156 if (kerr != KCGI_OK)
3158 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3161 kerr = khtml_puts(gw_trans->gw_html_req,
3163 if (kerr != KCGI_OK)
3165 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3166 if (kerr != KCGI_OK)
3168 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3169 KATTR_ID, "tag", KATTR__MAX);
3170 if (kerr != KCGI_OK)
3172 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3173 if (kerr != KCGI_OK)
3175 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3176 if (kerr != KCGI_OK)
3178 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3179 KATTR_ID, "tag_name", KATTR__MAX);
3180 if (kerr != KCGI_OK)
3183 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3184 gw_trans->repo_name, "action", "tag", "commit",
3186 if (href_tag == NULL) {
3187 error = got_error_from_errno("khttp_urlpart");
3190 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3191 KATTR_HREF, href_tag, KATTR__MAX);
3192 if (kerr != KCGI_OK)
3194 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3195 if (kerr != KCGI_OK)
3197 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3198 if (kerr != KCGI_OK)
3201 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3202 KATTR_ID, "navs_wrapper", KATTR__MAX);
3203 if (kerr != KCGI_OK)
3205 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3206 KATTR_ID, "navs", KATTR__MAX);
3207 if (kerr != KCGI_OK)
3210 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3211 KATTR_HREF, href_tag, KATTR__MAX);
3212 if (kerr != KCGI_OK)
3214 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3215 if (kerr != KCGI_OK)
3217 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3218 if (kerr != KCGI_OK)
3221 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3222 if (kerr != KCGI_OK)
3225 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3226 "path", gw_trans->repo_name, "action", "briefs",
3227 "commit", id_str, NULL);
3228 if (href_briefs == NULL) {
3229 error = got_error_from_errno("khttp_urlpart");
3232 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3233 KATTR_HREF, href_briefs, KATTR__MAX);
3234 if (kerr != KCGI_OK)
3236 kerr = khtml_puts(gw_trans->gw_html_req,
3238 if (kerr != KCGI_OK)
3240 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3241 if (kerr != KCGI_OK)
3244 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3245 if (kerr != KCGI_OK)
3248 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3249 "path", gw_trans->repo_name, "action", "commits",
3250 "commit", id_str, NULL);
3251 if (href_commits == NULL) {
3252 error = got_error_from_errno("khttp_urlpart");
3255 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3256 KATTR_HREF, href_commits, KATTR__MAX);
3257 if (kerr != KCGI_OK)
3259 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3260 if (kerr != KCGI_OK)
3262 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3263 if (kerr != KCGI_OK)
3266 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3267 KATTR_ID, "dotted_line", KATTR__MAX);
3268 if (kerr != KCGI_OK)
3270 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3271 if (kerr != KCGI_OK)
3275 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3276 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3277 if (kerr != KCGI_OK)
3279 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3280 if (kerr != KCGI_OK)
3282 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3283 if (kerr != KCGI_OK)
3285 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3286 KATTR_ID, "tag_info_date", KATTR__MAX);
3287 if (kerr != KCGI_OK)
3289 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3292 kerr = khtml_puts(gw_trans->gw_html_req,
3294 if (kerr != KCGI_OK)
3296 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3297 if (kerr != KCGI_OK)
3300 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3301 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3302 if (kerr != KCGI_OK)
3304 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3305 if (kerr != KCGI_OK)
3307 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3308 if (kerr != KCGI_OK)
3310 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3311 KATTR_ID, "tag_info_date", KATTR__MAX);
3312 if (kerr != KCGI_OK)
3314 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3315 if (kerr != KCGI_OK)
3317 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3318 if (kerr != KCGI_OK)
3321 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3322 KATTR_ID, "tag_info", KATTR__MAX);
3323 if (kerr != KCGI_OK)
3325 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3326 if (kerr != KCGI_OK)
3332 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3333 if (kerr != KCGI_OK)
3336 if (limit && --limit == 0)
3340 got_object_tag_close(tag);
3353 href_commits = NULL;
3355 if (tag_count == 0) {
3356 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3357 "summary_tags_title_wrapper", KATTR__MAX);
3358 if (kerr != KCGI_OK)
3360 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3361 "summary_tags_title", KATTR__MAX);
3362 if (kerr != KCGI_OK)
3364 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3365 if (kerr != KCGI_OK)
3367 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3368 if (kerr != KCGI_OK)
3370 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3371 "summary_tags_content", KATTR__MAX);
3372 if (kerr != KCGI_OK)
3374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3375 "tags_info", KATTR__MAX);
3376 if (kerr != KCGI_OK)
3378 kerr = khttp_puts(gw_trans->gw_req,
3379 "There are no tags for this repo.");
3380 if (kerr != KCGI_OK)
3382 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3387 TAILQ_FOREACH_REVERSE(re, &refs, got_reflist_head, entry) {
3388 const char *refname;
3389 struct got_object_id *id;
3390 struct got_commit_object *commit = NULL;
3392 refname = got_ref_get_name(re->ref);
3393 if (strncmp(refname, "refs/tags/", 10) != 0)
3397 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3401 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3403 if (error->code != GOT_ERR_OBJ_TYPE) {
3407 /* "lightweight" tag */
3408 error = got_object_open_as_commit(&commit,
3409 gw_trans->repo, id);
3414 error = got_object_id_str(&id_str, id);
3418 error = got_object_id_str(&id_str,
3419 got_object_tag_get_object_id(tag));
3424 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3425 strlen(id_str)) != 0)
3428 if (commit_found == 0 && tag_type == TAGBRIEF &&
3429 gw_trans->commit_id != NULL &&
3430 strncmp(id_str, gw_trans->commit_id, strlen(id_str)) != 0)
3435 if (gw_trans->commit_id != NULL &&
3436 strcmp(id_str, gw_trans->commit_id) != 0 &&
3437 (re == TAILQ_FIRST(&refs) ||
3438 c_cnt == gw_trans->gw_conf->got_max_commits_display)) {
3439 gw_trans->prev_id = strdup(id_str);
3440 if (gw_trans->prev_id == NULL) {
3441 error = got_error_from_errno("strdup");
3450 got_object_tag_close(tag);
3457 got_ref_list_free(&refs);
3458 if (error == NULL && kerr != KCGI_OK)
3459 error = gw_kcgi_error(kerr);
3464 gw_free_header(struct gw_header *header)
3467 free(header->author);
3468 free(header->committer);
3469 free(header->refs_str);
3470 free(header->commit_id);
3471 free(header->parent_id);
3472 free(header->tree_id);
3473 free(header->commit_msg);
3476 static struct gw_header *
3479 struct gw_header *header;
3481 header = malloc(sizeof(*header));
3485 header->path = NULL;
3486 TAILQ_INIT(&header->refs);
3488 header->refs_str = NULL;
3489 header->commit_id = NULL;
3490 header->committer = NULL;
3491 header->author = NULL;
3492 header->parent_id = NULL;
3493 header->tree_id = NULL;
3494 header->commit_msg = NULL;
3499 static const struct got_error *
3500 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3501 int limit, struct got_object_id *id)
3503 const struct got_error *error = NULL;
3504 struct got_commit_graph *graph = NULL;
3505 struct got_commit_object *commit = NULL;
3506 int chk_next = 0, chk_multi = 0, c_cnt = 0, commit_found = 0;
3507 struct gw_header *t_header = NULL;
3509 error = got_commit_graph_open(&graph, header->path, 0);
3513 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3519 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3522 if (error->code == GOT_ERR_ITER_COMPLETED)
3529 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3532 if (limit == 1 && chk_multi == 0 &&
3533 gw_trans->gw_conf->got_max_commits_display != 1) {
3534 error = gw_get_commit(gw_trans, header, commit, id);
3540 struct gw_header *n_header = NULL;
3541 if ((n_header = gw_init_header()) == NULL) {
3542 error = got_error_from_errno("malloc");
3545 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3547 error = got_ref_list(&n_header->refs, gw_trans->repo,
3548 NULL, got_ref_cmp_by_name, NULL);
3552 error = gw_get_commit(gw_trans, n_header, commit, id);
3555 got_ref_list_free(&n_header->refs);
3557 if (gw_trans->commit_id != NULL) {
3558 if (strcmp(gw_trans->commit_id,
3559 n_header->commit_id) == 0)
3565 * check for one more commit before breaking,
3566 * so we know whether to navicate through gw_briefs
3567 * gw_commits and gw_summary
3569 if (chk_next && (gw_trans->action == GW_BRIEFS ||
3570 gw_trans->action == GW_COMMITS ||
3571 gw_trans->action == GW_SUMMARY)) {
3572 gw_trans->next_id = strdup(n_header->commit_id);
3573 if (gw_trans->next_id == NULL)
3574 error = got_error_from_errno("strdup");
3575 TAILQ_REMOVE(&gw_trans->gw_headers, n_header,
3581 if (commit_found == 1 && (error || (limit && --limit == 0))) {
3588 if (gw_trans->prev_id == NULL && gw_trans->commit_id != NULL &&
3589 (gw_trans->action == GW_BRIEFS || gw_trans->action == GW_COMMITS)) {
3591 TAILQ_FOREACH_REVERSE(t_header, &gw_trans->gw_headers,
3593 if (commit_found == 0 &&
3594 strcmp(gw_trans->commit_id,
3595 t_header->commit_id) != 0)
3599 if (gw_trans->commit_id != NULL &&
3600 strcmp(gw_trans->commit_id,
3601 t_header->commit_id) != 0 &&
3602 (c_cnt == gw_trans->gw_conf->got_max_commits_display
3604 TAILQ_FIRST(&gw_trans->gw_headers))) {
3605 gw_trans->prev_id = strdup(t_header->commit_id);
3606 if (gw_trans->prev_id == NULL)
3607 error = got_error_from_errno("strdup");
3615 got_object_commit_close(commit);
3617 got_commit_graph_close(graph);
3621 static const struct got_error *
3622 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3623 struct got_commit_object *commit, struct got_object_id *id)
3625 const struct got_error *error = NULL;
3626 struct got_reflist_entry *re;
3627 struct got_object_id *id2 = NULL;
3628 struct got_object_qid *parent_id;
3629 char *commit_msg = NULL, *commit_msg0;
3632 TAILQ_FOREACH(re, &header->refs, entry) {
3635 struct got_tag_object *tag = NULL;
3636 struct got_object_id *ref_id;
3639 if (got_ref_is_symbolic(re->ref))
3642 name = got_ref_get_name(re->ref);
3643 if (strncmp(name, "refs/", 5) == 0)
3645 if (strncmp(name, "got/", 4) == 0)
3647 if (strncmp(name, "heads/", 6) == 0)
3649 if (strncmp(name, "remotes/", 8) == 0) {
3651 s = strstr(name, "/" GOT_REF_HEAD);
3652 if (s != NULL && s[strlen(s)] == '\0')
3655 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3658 if (strncmp(name, "tags/", 5) == 0) {
3659 error = got_object_open_as_tag(&tag, gw_trans->repo,
3662 if (error->code != GOT_ERR_OBJ_TYPE) {
3667 * Ref points at something other
3674 cmp = got_object_id_cmp(tag ?
3675 got_object_tag_get_object_id(tag) : ref_id, id);
3678 got_object_tag_close(tag);
3681 s = header->refs_str;
3682 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3683 s ? ", " : "", name) == -1) {
3684 error = got_error_from_errno("asprintf");
3686 header->refs_str = NULL;
3692 error = got_object_id_str(&header->commit_id, id);
3696 error = got_object_id_str(&header->tree_id,
3697 got_object_commit_get_tree_id(commit));
3701 if (gw_trans->action == GW_DIFF) {
3702 parent_id = STAILQ_FIRST(
3703 got_object_commit_get_parent_ids(commit));
3704 if (parent_id != NULL) {
3705 id2 = got_object_id_dup(parent_id->id);
3707 error = got_object_id_str(&header->parent_id, id2);
3712 header->parent_id = strdup("/dev/null");
3713 if (header->parent_id == NULL) {
3714 error = got_error_from_errno("strdup");
3720 header->committer_time =
3721 got_object_commit_get_committer_time(commit);
3724 strdup(got_object_commit_get_author(commit));
3725 if (header->author == NULL) {
3726 error = got_error_from_errno("strdup");
3730 strdup(got_object_commit_get_committer(commit));
3731 if (header->committer == NULL) {
3732 error = got_error_from_errno("strdup");
3735 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3739 commit_msg = commit_msg0;
3740 while (*commit_msg == '\n')
3743 header->commit_msg = strdup(commit_msg);
3744 if (header->commit_msg == NULL)
3745 error = got_error_from_errno("strdup");
3750 static const struct got_error *
3751 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3753 const struct got_error *error = NULL;
3754 char *in_repo_path = NULL;
3755 struct got_object_id *id = NULL;
3756 struct got_reference *ref;
3758 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3762 if (gw_trans->commit_id == NULL || gw_trans->action == GW_COMMITS ||
3763 gw_trans->action == GW_BRIEFS || gw_trans->action == GW_SUMMARY ||
3764 gw_trans->action == GW_TAGS) {
3765 error = got_ref_open(&ref, gw_trans->repo,
3766 gw_trans->headref, 0);
3770 error = got_ref_resolve(&id, gw_trans->repo, ref);
3775 error = got_ref_open(&ref, gw_trans->repo,
3776 gw_trans->commit_id, 0);
3777 if (error == NULL) {
3779 error = got_ref_resolve(&id, gw_trans->repo, ref);
3783 error = got_object_get_type(&obj_type, gw_trans->repo,
3787 if (obj_type == GOT_OBJ_TYPE_TAG) {
3788 struct got_tag_object *tag;
3789 error = got_object_open_as_tag(&tag,
3790 gw_trans->repo, id);
3793 if (got_object_tag_get_object_type(tag) !=
3794 GOT_OBJ_TYPE_COMMIT) {
3795 got_object_tag_close(tag);
3796 error = got_error(GOT_ERR_OBJ_TYPE);
3800 id = got_object_id_dup(
3801 got_object_tag_get_object_id(tag));
3803 error = got_error_from_errno(
3804 "got_object_id_dup");
3805 got_object_tag_close(tag);
3808 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3809 error = got_error(GOT_ERR_OBJ_TYPE);
3813 error = got_repo_match_object_id_prefix(&id,
3814 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3820 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3821 gw_trans->repo_path);
3826 header->path = strdup(in_repo_path);
3827 if (header->path == NULL) {
3828 error = got_error_from_errno("strdup");
3833 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3834 got_ref_cmp_by_name, NULL);
3838 error = gw_get_commits(gw_trans, header, limit, id);
3840 got_ref_list_free(&header->refs);
3850 char datebuf[11]; /* YYYY-MM-DD + NUL */
3853 struct gw_blame_cb_args {
3854 struct blame_line *lines;
3858 off_t *line_offsets;
3860 struct got_repository *repo;
3861 struct gw_trans *gw_trans;
3864 static const struct got_error *
3865 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3867 const struct got_error *err = NULL;
3868 struct gw_blame_cb_args *a = arg;
3869 struct blame_line *bline;
3871 size_t linesize = 0;
3872 struct got_commit_object *commit = NULL;
3875 time_t committer_time;
3876 enum kcgi_err kerr = KCGI_OK;
3878 if (nlines != a->nlines ||
3879 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3880 return got_error(GOT_ERR_RANGE);
3883 return NULL; /* no change in this commit */
3885 /* Annotate this line. */
3886 bline = &a->lines[lineno - 1];
3887 if (bline->annotated)
3889 err = got_object_id_str(&bline->id_str, id);
3893 err = got_object_open_as_commit(&commit, a->repo, id);
3897 bline->committer = strdup(got_object_commit_get_committer(commit));
3898 if (bline->committer == NULL) {
3899 err = got_error_from_errno("strdup");
3903 committer_time = got_object_commit_get_committer_time(commit);
3904 if (gmtime_r(&committer_time, &tm) == NULL)
3905 return got_error_from_errno("gmtime_r");
3906 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3908 err = got_error(GOT_ERR_NO_SPACE);
3911 bline->annotated = 1;
3913 /* Print lines annotated so far. */
3914 bline = &a->lines[a->lineno_cur - 1];
3915 if (!bline->annotated)
3918 offset = a->line_offsets[a->lineno_cur - 1];
3919 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3920 err = got_error_from_errno("fseeko");
3924 while (bline->annotated) {
3925 char *smallerthan, *at, *nl, *committer;
3926 char *href_diff = NULL;
3929 if (getline(&line, &linesize, a->f) == -1) {
3931 err = got_error_from_errno("getline");
3935 committer = bline->committer;
3936 smallerthan = strchr(committer, '<');
3937 if (smallerthan && smallerthan[1] != '\0')
3938 committer = smallerthan + 1;
3939 at = strchr(committer, '@');
3942 len = strlen(committer);
3944 committer[8] = '\0';
3946 nl = strchr(line, '\n');
3950 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3951 "blame_wrapper", KATTR__MAX);
3952 if (kerr != KCGI_OK)
3954 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3955 "blame_number", KATTR__MAX);
3956 if (kerr != KCGI_OK)
3958 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3959 a->nlines_prec, a->lineno_cur);
3960 if (kerr != KCGI_OK)
3962 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3963 if (kerr != KCGI_OK)
3966 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3967 "blame_hash", KATTR__MAX);
3968 if (kerr != KCGI_OK)
3971 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3972 a->gw_trans->repo_name, "action", "diff", "commit",
3973 bline->id_str, NULL);
3974 if (href_diff == NULL) {
3975 err = got_error_from_errno("khttp_urlpart");
3978 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3979 KATTR_HREF, href_diff, KATTR__MAX);
3980 if (kerr != KCGI_OK)
3982 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3984 if (kerr != KCGI_OK)
3986 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3987 if (kerr != KCGI_OK)
3990 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3991 "blame_date", KATTR__MAX);
3992 if (kerr != KCGI_OK)
3994 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3995 if (kerr != KCGI_OK)
3997 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3998 if (kerr != KCGI_OK)
4001 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4002 "blame_author", KATTR__MAX);
4003 if (kerr != KCGI_OK)
4005 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
4006 if (kerr != KCGI_OK)
4008 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4009 if (kerr != KCGI_OK)
4012 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4013 "blame_code", KATTR__MAX);
4014 if (kerr != KCGI_OK)
4016 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
4017 if (kerr != KCGI_OK)
4019 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4020 if (kerr != KCGI_OK)
4023 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4024 if (kerr != KCGI_OK)
4028 bline = &a->lines[a->lineno_cur - 1];
4034 got_object_commit_close(commit);
4036 if (err == NULL && kerr != KCGI_OK)
4037 err = gw_kcgi_error(kerr);
4041 static const struct got_error *
4042 gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
4044 const struct got_error *error = NULL;
4045 struct got_object_id *obj_id = NULL;
4046 struct got_object_id *commit_id = NULL;
4047 struct got_blob_object *blob = NULL;
4048 char *path = NULL, *in_repo_path = NULL;
4049 struct gw_blame_cb_args bca;
4053 if (asprintf(&path, "%s%s%s",
4054 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4055 gw_trans->repo_folder ? "/" : "",
4056 gw_trans->repo_file) == -1) {
4057 error = got_error_from_errno("asprintf");
4061 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4065 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4066 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4070 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4075 if (obj_id == NULL) {
4076 error = got_error(GOT_ERR_NO_OBJ);
4080 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4084 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4085 error = got_error(GOT_ERR_OBJ_TYPE);
4089 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4093 bca.f = got_opentemp();
4094 if (bca.f == NULL) {
4095 error = got_error_from_errno("got_opentemp");
4098 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4099 &bca.line_offsets, bca.f, blob);
4100 if (error || bca.nlines == 0)
4103 /* Don't include \n at EOF in the blame line count. */
4104 if (bca.line_offsets[bca.nlines - 1] == filesize)
4107 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4108 if (bca.lines == NULL) {
4109 error = got_error_from_errno("calloc");
4113 bca.nlines_prec = 0;
4119 bca.repo = gw_trans->repo;
4120 bca.gw_trans = gw_trans;
4122 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4131 free(bca.line_offsets);
4132 for (i = 0; i < bca.nlines; i++) {
4133 struct blame_line *bline = &bca.lines[i];
4134 free(bline->id_str);
4135 free(bline->committer);
4138 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4139 error = got_error_from_errno("fclose");
4142 got_object_blob_close(blob);
4146 static const struct got_error *
4147 gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
4149 const struct got_error *error = NULL;
4150 struct got_object_id *obj_id = NULL;
4151 struct got_object_id *commit_id = NULL;
4152 struct got_blob_object *blob = NULL;
4153 char *path = NULL, *in_repo_path = NULL;
4154 int obj_type, set_mime = 0;
4157 enum kcgi_err kerr = KCGI_OK;
4159 if (asprintf(&path, "%s%s%s",
4160 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4161 gw_trans->repo_folder ? "/" : "",
4162 gw_trans->repo_file) == -1) {
4163 error = got_error_from_errno("asprintf");
4167 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4171 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4172 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4176 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4181 if (obj_id == NULL) {
4182 error = got_error(GOT_ERR_NO_OBJ);
4186 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4190 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4191 error = got_error(GOT_ERR_OBJ_TYPE);
4195 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4199 hdrlen = got_object_blob_get_hdrlen(blob);
4201 error = got_object_blob_read_block(&len, blob);
4204 buf = got_object_blob_get_read_buf(blob);
4207 * Skip blob object header first time around,
4208 * which also contains a zero byte.
4211 if (set_mime == 0) {
4212 if (isbinary(buf, len - hdrlen))
4213 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4215 gw_trans->mime = KMIME_TEXT_PLAIN;
4217 error = gw_display_index(gw_trans);
4221 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4222 if (kerr != KCGI_OK)
4232 got_object_blob_close(blob);
4233 if (error == NULL && kerr != KCGI_OK)
4234 error = gw_kcgi_error(kerr);
4238 static const struct got_error *
4239 gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
4241 const struct got_error *error = NULL;
4242 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4243 struct got_tree_object *tree = NULL;
4244 char *path = NULL, *in_repo_path = NULL;
4245 char *id_str = NULL;
4246 char *build_folder = NULL;
4247 char *href_blob = NULL, *href_blame = NULL;
4248 const char *class = NULL;
4249 int nentries, i, class_flip = 0;
4250 enum kcgi_err kerr = KCGI_OK;
4252 if (gw_trans->repo_folder != NULL) {
4253 path = strdup(gw_trans->repo_folder);
4255 error = got_error_from_errno("strdup");
4259 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4260 gw_trans->repo_path);
4264 path = in_repo_path;
4267 if (gw_trans->commit_id == NULL) {
4268 struct got_reference *head_ref;
4269 error = got_ref_open(&head_ref, gw_trans->repo,
4270 gw_trans->headref, 0);
4273 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4276 got_ref_close(head_ref);
4278 * gw_trans->commit_id was not parsed from the querystring
4279 * we hit this code path from gw_index, where we don't know the
4280 * commit values for the tree link yet, so set
4281 * gw_trans->commit_id here to continue further into the tree
4283 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4288 error = got_repo_match_object_id(&commit_id, NULL,
4289 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
4295 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4300 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4304 nentries = got_object_tree_get_nentries(tree);
4305 for (i = 0; i < nentries; i++) {
4306 struct got_tree_entry *te;
4307 const char *modestr = "";
4310 te = got_object_tree_get_entry(tree, i);
4312 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4316 mode = got_tree_entry_get_mode(te);
4317 if (got_object_tree_entry_is_submodule(te))
4319 else if (S_ISLNK(mode))
4321 else if (S_ISDIR(mode))
4323 else if (mode & S_IXUSR)
4326 if (class_flip == 0) {
4327 class = "back_lightgray";
4330 class = "back_white";
4334 if (S_ISDIR(mode)) {
4335 if (asprintf(&build_folder, "%s/%s",
4336 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4337 got_tree_entry_get_name(te)) == -1) {
4338 error = got_error_from_errno("asprintf");
4342 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4343 gw_trans->repo_name, "action",
4344 gw_get_action_name(gw_trans), "commit",
4345 gw_trans->commit_id, "folder", build_folder, NULL);
4346 if (href_blob == NULL) {
4347 error = got_error_from_errno("khttp_urlpart");
4350 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4351 KATTR_ID, "tree_wrapper", KATTR__MAX);
4352 if (kerr != KCGI_OK)
4354 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4355 KATTR_ID, "tree_line", KATTR_CLASS, class,
4357 if (kerr != KCGI_OK)
4359 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4360 KATTR_HREF, href_blob, KATTR_CLASS,
4361 "diff_directory", KATTR__MAX);
4362 if (kerr != KCGI_OK)
4364 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4365 got_tree_entry_get_name(te), modestr);
4366 if (kerr != KCGI_OK)
4368 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4369 if (kerr != KCGI_OK)
4371 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4372 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4374 if (kerr != KCGI_OK)
4376 kerr = khtml_entity(gw_trans->gw_html_req,
4378 if (kerr != KCGI_OK)
4380 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4381 if (kerr != KCGI_OK)
4384 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4385 gw_trans->repo_name, "action", "blob", "commit",
4386 gw_trans->commit_id, "file",
4387 got_tree_entry_get_name(te), "folder",
4388 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4390 if (href_blob == NULL) {
4391 error = got_error_from_errno("khttp_urlpart");
4394 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4395 gw_trans->repo_name, "action", "blame", "commit",
4396 gw_trans->commit_id, "file",
4397 got_tree_entry_get_name(te), "folder",
4398 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4400 if (href_blame == NULL) {
4401 error = got_error_from_errno("khttp_urlpart");
4404 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4405 KATTR_ID, "tree_wrapper", KATTR__MAX);
4406 if (kerr != KCGI_OK)
4408 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4409 KATTR_ID, "tree_line", KATTR_CLASS, class,
4411 if (kerr != KCGI_OK)
4413 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4414 KATTR_HREF, href_blob, KATTR__MAX);
4415 if (kerr != KCGI_OK)
4417 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4418 got_tree_entry_get_name(te), modestr);
4419 if (kerr != KCGI_OK)
4421 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4422 if (kerr != KCGI_OK)
4424 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4425 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4427 if (kerr != KCGI_OK)
4430 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4431 KATTR_HREF, href_blob, KATTR__MAX);
4432 if (kerr != KCGI_OK)
4434 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4435 if (kerr != KCGI_OK)
4437 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4438 if (kerr != KCGI_OK)
4441 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4442 if (kerr != KCGI_OK)
4445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4446 KATTR_HREF, href_blame, KATTR__MAX);
4447 if (kerr != KCGI_OK)
4449 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4450 if (kerr != KCGI_OK)
4453 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4454 if (kerr != KCGI_OK)
4462 build_folder = NULL;
4466 got_object_tree_close(tree);
4473 if (error == NULL && kerr != KCGI_OK)
4474 error = gw_kcgi_error(kerr);
4478 static const struct got_error *
4479 gw_output_repo_heads(struct gw_trans *gw_trans)
4481 const struct got_error *error = NULL;
4482 struct got_reflist_head refs;
4483 struct got_reflist_entry *re;
4484 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4485 char *href_commits = NULL;
4486 enum kcgi_err kerr = KCGI_OK;
4490 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4491 got_ref_cmp_by_name, NULL);
4495 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4496 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4497 if (kerr != KCGI_OK)
4499 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4500 KATTR_ID, "summary_heads_title", KATTR__MAX);
4501 if (kerr != KCGI_OK)
4503 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4504 if (kerr != KCGI_OK)
4506 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4507 if (kerr != KCGI_OK)
4509 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4510 KATTR_ID, "summary_heads_content", KATTR__MAX);
4511 if (kerr != KCGI_OK)
4514 TAILQ_FOREACH(re, &refs, entry) {
4515 const char *refname;
4517 if (got_ref_is_symbolic(re->ref))
4520 refname = got_ref_get_name(re->ref);
4521 if (strncmp(refname, "refs/heads/", 11) != 0)
4524 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4529 if (strncmp(refname, "refs/heads/", 11) == 0)
4532 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4533 KATTR_ID, "heads_wrapper", KATTR__MAX);
4534 if (kerr != KCGI_OK)
4536 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4537 KATTR_ID, "heads_age", KATTR__MAX);
4538 if (kerr != KCGI_OK)
4540 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4541 if (kerr != KCGI_OK)
4543 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4544 if (kerr != KCGI_OK)
4546 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4547 KATTR_ID, "heads_space", KATTR__MAX);
4548 if (kerr != KCGI_OK)
4550 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4551 if (kerr != KCGI_OK)
4553 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4554 if (kerr != KCGI_OK)
4556 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4557 KATTR_ID, "head", KATTR__MAX);
4558 if (kerr != KCGI_OK)
4561 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4562 gw_trans->repo_name, "action", "summary", "headref",
4564 if (href_summary == NULL) {
4565 error = got_error_from_errno("khttp_urlpart");
4568 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4569 href_summary, KATTR__MAX);
4570 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4571 if (kerr != KCGI_OK)
4573 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4574 if (kerr != KCGI_OK)
4577 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4578 "navs_wrapper", KATTR__MAX);
4579 if (kerr != KCGI_OK)
4581 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4582 "navs", KATTR__MAX);
4583 if (kerr != KCGI_OK)
4586 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4587 href_summary, KATTR__MAX);
4588 if (kerr != KCGI_OK)
4590 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4591 if (kerr != KCGI_OK)
4593 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4594 if (kerr != KCGI_OK)
4597 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4598 if (kerr != KCGI_OK)
4601 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4602 gw_trans->repo_name, "action", "briefs", "headref",
4604 if (href_briefs == NULL) {
4605 error = got_error_from_errno("khttp_urlpart");
4608 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4609 href_briefs, KATTR__MAX);
4610 if (kerr != KCGI_OK)
4612 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4613 if (kerr != KCGI_OK)
4615 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4616 if (kerr != KCGI_OK)
4619 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4620 if (kerr != KCGI_OK)
4623 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4624 gw_trans->repo_name, "action", "commits", "headref",
4626 if (href_commits == NULL) {
4627 error = got_error_from_errno("khttp_urlpart");
4630 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4631 href_commits, KATTR__MAX);
4632 if (kerr != KCGI_OK)
4634 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4635 if (kerr != KCGI_OK)
4637 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4638 if (kerr != KCGI_OK)
4641 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4642 "dotted_line", KATTR__MAX);
4643 if (kerr != KCGI_OK)
4645 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4646 if (kerr != KCGI_OK)
4649 href_summary = NULL;
4653 href_commits = NULL;
4656 got_ref_list_free(&refs);
4663 static const struct got_error *
4664 gw_output_site_link(struct gw_trans *gw_trans)
4666 const struct got_error *error = NULL;
4667 char *href_summary = NULL;
4668 enum kcgi_err kerr = KCGI_OK;
4670 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4671 "site_link", KATTR__MAX);
4672 if (kerr != KCGI_OK)
4674 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4676 if (kerr != KCGI_OK)
4678 kerr = khtml_puts(gw_trans->gw_html_req,
4679 gw_trans->gw_conf->got_site_link);
4680 if (kerr != KCGI_OK)
4682 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4683 if (kerr != KCGI_OK)
4686 if (gw_trans->repo_name != NULL) {
4687 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4688 if (kerr != KCGI_OK)
4691 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4692 gw_trans->repo_name, "action", "summary", NULL);
4693 if (href_summary == NULL) {
4694 error = got_error_from_errno("khttp_urlpart");
4697 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4698 href_summary, KATTR__MAX);
4699 if (kerr != KCGI_OK)
4701 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4702 if (kerr != KCGI_OK)
4704 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4705 if (kerr != KCGI_OK)
4707 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4708 gw_get_action_name(gw_trans));
4709 if (kerr != KCGI_OK)
4713 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4714 if (kerr != KCGI_OK)
4718 if (error == NULL && kerr != KCGI_OK)
4719 error = gw_kcgi_error(kerr);
4723 static const struct got_error *
4724 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4726 const struct got_error *error = NULL;
4728 enum kcgi_err kerr = KCGI_OK;
4730 if (strncmp(buf, "-", 1) == 0)
4731 color = "diff_minus";
4732 else if (strncmp(buf, "+", 1) == 0)
4733 color = "diff_plus";
4734 else if (strncmp(buf, "@@", 2) == 0)
4735 color = "diff_chunk_header";
4736 else if (strncmp(buf, "@@", 2) == 0)
4737 color = "diff_chunk_header";
4738 else if (strncmp(buf, "commit +", 8) == 0)
4739 color = "diff_meta";
4740 else if (strncmp(buf, "commit -", 8) == 0)
4741 color = "diff_meta";
4742 else if (strncmp(buf, "blob +", 6) == 0)
4743 color = "diff_meta";
4744 else if (strncmp(buf, "blob -", 6) == 0)
4745 color = "diff_meta";
4746 else if (strncmp(buf, "file +", 6) == 0)
4747 color = "diff_meta";
4748 else if (strncmp(buf, "file -", 6) == 0)
4749 color = "diff_meta";
4750 else if (strncmp(buf, "from:", 5) == 0)
4751 color = "diff_author";
4752 else if (strncmp(buf, "via:", 4) == 0)
4753 color = "diff_author";
4754 else if (strncmp(buf, "date:", 5) == 0)
4755 color = "diff_date";
4756 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4757 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4758 if (error == NULL && kerr != KCGI_OK)
4759 error = gw_kcgi_error(kerr);
4764 main(int argc, char *argv[])
4766 const struct got_error *error = NULL, *error2 = NULL;
4767 struct gw_trans *gw_trans;
4768 struct gw_dir *dir = NULL, *tdir;
4769 const char *page = "index";
4770 enum kcgi_err kerr = KCGI_OK;
4772 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4775 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4778 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4781 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4784 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4785 if (kerr != KCGI_OK) {
4786 error = gw_kcgi_error(kerr);
4790 TAILQ_INIT(&gw_trans->gw_dirs);
4791 TAILQ_INIT(&gw_trans->gw_headers);
4793 gw_trans->action = -1;
4795 gw_trans->repos_total = 0;
4796 gw_trans->repo_path = NULL;
4797 gw_trans->commit_id = NULL;
4798 gw_trans->next_id = NULL;
4799 gw_trans->prev_id = NULL;
4800 gw_trans->headref = GOT_REF_HEAD;
4801 gw_trans->mime = KMIME_TEXT_HTML;
4802 gw_trans->gw_tmpl->key = gw_templs;
4803 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4804 gw_trans->gw_tmpl->arg = gw_trans;
4805 gw_trans->gw_tmpl->cb = gw_template;
4807 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4811 error = gw_parse_querystring(gw_trans);
4815 if (gw_trans->action == GW_BLOB)
4816 error = gw_blob(gw_trans);
4818 error = gw_display_index(gw_trans);
4820 if (gw_trans->repo) {
4821 const struct got_error *close_err;
4822 close_err = got_repo_close(gw_trans->repo);
4827 gw_trans->error = error;
4828 gw_trans->action = GW_ERR;
4829 error2 = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
4831 goto cleanup; /* we can't display an error page */
4832 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
4833 if (kerr != KCGI_OK)
4834 goto cleanup; /* we can't display an error page */
4835 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
4836 gw_query_funcs[gw_trans->action].template);
4837 if (kerr != KCGI_OK) {
4838 khtml_close(gw_trans->gw_html_req);
4839 goto cleanup; /* we can't display an error page */
4844 free(gw_trans->gw_conf->got_repos_path);
4845 free(gw_trans->gw_conf->got_www_path);
4846 free(gw_trans->gw_conf->got_site_name);
4847 free(gw_trans->gw_conf->got_site_owner);
4848 free(gw_trans->gw_conf->got_site_link);
4849 free(gw_trans->gw_conf->got_logo);
4850 free(gw_trans->gw_conf->got_logo_url);
4851 free(gw_trans->gw_conf);
4852 free(gw_trans->commit_id);
4853 free(gw_trans->next_id);
4854 free(gw_trans->prev_id);
4855 free(gw_trans->repo_path);
4856 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4858 free(dir->description);
4865 khttp_free(gw_trans->gw_req);