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.
19 #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_commit_object *,
218 struct got_object_id *);
219 static const struct got_error *gw_load_got_paths(struct gw_trans *);
220 static const struct got_error *gw_load_got_path(struct gw_trans *,
222 static const struct got_error *gw_parse_querystring(struct gw_trans *);
223 static const struct got_error *gw_blame(struct gw_trans *);
224 static const struct got_error *gw_blob(struct gw_trans *);
225 static const struct got_error *gw_diff(struct gw_trans *);
226 static const struct got_error *gw_index(struct gw_trans *);
227 static const struct got_error *gw_commits(struct gw_trans *);
228 static const struct got_error *gw_briefs(struct gw_trans *);
229 static const struct got_error *gw_summary(struct gw_trans *);
230 static const struct got_error *gw_tree(struct gw_trans *);
231 static const struct got_error *gw_tag(struct gw_trans *);
232 static const struct got_error *gw_tags(struct gw_trans *);
234 struct gw_query_action {
235 unsigned int func_id;
236 const char *func_name;
237 const struct got_error *(*func_main)(struct gw_trans *);
241 enum gw_query_actions {
255 static const struct gw_query_action gw_query_funcs[] = {
256 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
257 { GW_BLOB, "blob", NULL, NULL },
258 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
259 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
260 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
261 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
262 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
263 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
264 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
265 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
266 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
270 gw_get_action_name(struct gw_trans *gw_trans)
272 return gw_query_funcs[gw_trans->action].func_name;
275 static const struct got_error *
276 gw_kcgi_error(enum kcgi_err kerr)
281 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
282 return got_error(GOT_ERR_CANCELLED);
284 if (kerr == KCGI_ENOMEM)
285 return got_error_set_errno(ENOMEM,
286 kcgi_strerror(kerr));
288 if (kerr == KCGI_ENFILE)
289 return got_error_set_errno(ENFILE,
290 kcgi_strerror(kerr));
292 if (kerr == KCGI_EAGAIN)
293 return got_error_set_errno(EAGAIN,
294 kcgi_strerror(kerr));
296 if (kerr == KCGI_FORM)
297 return got_error_msg(GOT_ERR_IO,
298 kcgi_strerror(kerr));
300 return got_error_from_errno(kcgi_strerror(kerr));
303 static const struct got_error *
304 gw_apply_unveil(const char *repo_path)
306 const struct got_error *err;
309 if (unveil("gmon.out", "rwc") != 0)
310 return got_error_from_errno2("unveil", "gmon.out");
312 if (repo_path && unveil(repo_path, "r") != 0)
313 return got_error_from_errno2("unveil", repo_path);
315 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
316 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
318 err = got_privsep_unveil_exec_helpers();
322 if (unveil(NULL, NULL) != 0)
323 return got_error_from_errno("unveil");
329 isbinary(const uint8_t *buf, size_t n)
333 for (i = 0; i < n; i++)
339 static const struct got_error *
340 gw_blame(struct gw_trans *gw_trans)
342 const struct got_error *error = NULL;
343 struct gw_header *header = NULL;
345 enum kcgi_err kerr = KCGI_OK;
348 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
350 return got_error_from_errno("pledge");
352 if ((header = gw_init_header()) == NULL)
353 return got_error_from_errno("malloc");
355 error = gw_apply_unveil(gw_trans->gw_dir->path);
359 /* check querystring */
360 if (gw_trans->repo_file == NULL) {
361 error = got_error_msg(GOT_ERR_QUERYSTRING,
362 "file required in querystring");
365 if (gw_trans->commit_id == NULL) {
366 error = got_error_msg(GOT_ERR_QUERYSTRING,
367 "commit required in querystring");
371 error = gw_get_header(gw_trans, header, 1);
374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
375 "blame_header_wrapper", KATTR__MAX);
378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
379 "blame_header", KATTR__MAX);
382 error = gw_get_time_str(&age, header->committer_time,
386 error = gw_gen_age_header(gw_trans, age ?age : "");
389 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
392 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
395 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
396 "dotted_line", KATTR__MAX);
399 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
403 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
404 "blame", KATTR__MAX);
407 error = gw_output_file_blame(gw_trans, header);
410 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
412 gw_free_header(header);
413 if (error == NULL && kerr != KCGI_OK)
414 error = gw_kcgi_error(kerr);
418 static const struct got_error *
419 gw_blob(struct gw_trans *gw_trans)
421 const struct got_error *error = NULL, *err = NULL;
422 struct gw_header *header = NULL;
423 enum kcgi_err kerr = KCGI_OK;
426 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
428 return got_error_from_errno("pledge");
430 if ((header = gw_init_header()) == NULL)
431 return got_error_from_errno("malloc");
433 error = gw_apply_unveil(gw_trans->gw_dir->path);
437 /* check querystring */
438 if (gw_trans->repo_file == NULL) {
439 error = got_error_msg(GOT_ERR_QUERYSTRING,
440 "file required in querystring");
443 if (gw_trans->commit_id == NULL) {
444 error = got_error_msg(GOT_ERR_QUERYSTRING,
445 "commit required in querystring");
448 error = gw_get_header(gw_trans, header, 1);
452 error = gw_output_blob_buf(gw_trans, header);
456 gw_trans->mime = KMIME_TEXT_PLAIN;
457 err = gw_display_index(gw_trans);
462 kerr = khttp_puts(gw_trans->gw_req, error->msg);
465 gw_free_header(header);
466 if (error == NULL && kerr != KCGI_OK)
467 error = gw_kcgi_error(kerr);
471 static const struct got_error *
472 gw_diff(struct gw_trans *gw_trans)
474 const struct got_error *error = NULL;
475 struct gw_header *header = NULL;
477 enum kcgi_err kerr = KCGI_OK;
480 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
482 return got_error_from_errno("pledge");
484 if ((header = gw_init_header()) == NULL)
485 return got_error_from_errno("malloc");
487 error = gw_apply_unveil(gw_trans->gw_dir->path);
491 error = gw_get_header(gw_trans, header, 1);
495 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
496 "diff_header_wrapper", KATTR__MAX);
499 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
500 "diff_header", KATTR__MAX);
503 error = gw_gen_diff_header(gw_trans, header->parent_id,
507 error = gw_gen_commit_header(gw_trans, header->commit_id,
511 error = gw_gen_tree_header(gw_trans, header->tree_id);
514 error = gw_gen_author_header(gw_trans, header->author);
517 error = gw_gen_committer_header(gw_trans, header->author);
520 error = gw_get_time_str(&age, header->committer_time,
524 error = gw_gen_age_header(gw_trans, age ?age : "");
527 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
530 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
534 "dotted_line", KATTR__MAX);
537 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
545 error = gw_output_diff(gw_trans, header);
549 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
551 gw_free_header(header);
553 if (error == NULL && kerr != KCGI_OK)
554 error = gw_kcgi_error(kerr);
558 static const struct got_error *
559 gw_index(struct gw_trans *gw_trans)
561 const struct got_error *error = NULL;
562 struct gw_dir *gw_dir = NULL;
563 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
564 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
565 char *href_tags = NULL;
566 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
567 enum kcgi_err kerr = KCGI_OK;
570 if (pledge("stdio rpath proc exec sendfd unveil",
572 error = got_error_from_errno("pledge");
576 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
580 error = gw_load_got_paths(gw_trans);
584 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
585 "index_header", KATTR__MAX);
587 return gw_kcgi_error(kerr);
588 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
589 "index_header_project", KATTR__MAX);
591 return gw_kcgi_error(kerr);
592 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
594 return gw_kcgi_error(kerr);
595 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
597 return gw_kcgi_error(kerr);
599 if (gw_trans->gw_conf->got_show_repo_description) {
600 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
601 "index_header_description", KATTR__MAX);
603 return gw_kcgi_error(kerr);
604 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
606 return gw_kcgi_error(kerr);
607 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
609 return gw_kcgi_error(kerr);
612 if (gw_trans->gw_conf->got_show_repo_owner) {
613 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
614 "index_header_owner", KATTR__MAX);
616 return gw_kcgi_error(kerr);
617 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
619 return gw_kcgi_error(kerr);
620 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
622 return gw_kcgi_error(kerr);
625 if (gw_trans->gw_conf->got_show_repo_age) {
626 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
627 "index_header_age", KATTR__MAX);
629 return gw_kcgi_error(kerr);
630 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
632 return gw_kcgi_error(kerr);
633 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
635 return gw_kcgi_error(kerr);
638 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
640 return gw_kcgi_error(kerr);
642 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
643 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
644 "index_wrapper", KATTR__MAX);
646 return gw_kcgi_error(kerr);
647 kerr = khtml_printf(gw_trans->gw_html_req,
648 "No repositories found in %s",
649 gw_trans->gw_conf->got_repos_path);
651 return gw_kcgi_error(kerr);
652 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
654 return gw_kcgi_error(kerr);
655 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
656 "dotted_line", KATTR__MAX);
658 return gw_kcgi_error(kerr);
659 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
661 return gw_kcgi_error(kerr);
665 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
668 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
669 if (gw_trans->page > 0 && (gw_trans->page *
670 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
677 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
678 "index_wrapper", KATTR__MAX);
682 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
683 gw_dir->name, "action", "summary", NULL);
684 if (href_summary == NULL) {
685 error = got_error_from_errno("khttp_urlpart");
688 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
689 "index_project", KATTR__MAX);
692 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
693 href_summary, KATTR__MAX);
696 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
699 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
702 if (gw_trans->gw_conf->got_show_repo_description) {
703 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
704 KATTR_ID, "index_project_description", KATTR__MAX);
707 kerr = khtml_puts(gw_trans->gw_html_req,
708 gw_dir->description ? gw_dir->description : "");
711 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
715 if (gw_trans->gw_conf->got_show_repo_owner) {
716 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
717 KATTR_ID, "index_project_owner", KATTR__MAX);
720 kerr = khtml_puts(gw_trans->gw_html_req,
721 gw_dir->owner ? gw_dir->owner : "");
724 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
728 if (gw_trans->gw_conf->got_show_repo_age) {
729 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
730 KATTR_ID, "index_project_age", KATTR__MAX);
733 kerr = khtml_puts(gw_trans->gw_html_req,
734 gw_dir->age ? gw_dir->age : "");
737 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
742 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
743 "navs_wrapper", KATTR__MAX);
746 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
751 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
752 href_summary, KATTR__MAX);
755 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
758 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
762 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
766 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
767 gw_dir->name, "action", "briefs", NULL);
768 if (href_briefs == NULL) {
769 error = got_error_from_errno("khttp_urlpart");
772 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
773 href_briefs, KATTR__MAX);
776 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
779 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
781 error = gw_kcgi_error(kerr);
783 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
787 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
788 gw_dir->name, "action", "commits", NULL);
789 if (href_commits == NULL) {
790 error = got_error_from_errno("khttp_urlpart");
793 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
794 href_commits, KATTR__MAX);
797 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
800 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
804 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
808 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
809 gw_dir->name, "action", "tags", NULL);
810 if (href_tags == NULL) {
811 error = got_error_from_errno("khttp_urlpart");
814 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
815 href_tags, KATTR__MAX);
818 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
821 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
825 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
829 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
830 gw_dir->name, "action", "tree", NULL);
831 if (href_tree == NULL) {
832 error = got_error_from_errno("khttp_urlpart");
835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
836 href_tree, KATTR__MAX);
839 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
843 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
846 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
847 "dotted_line", KATTR__MAX);
850 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
865 if (gw_trans->gw_conf->got_max_repos_display == 0)
868 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
869 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
870 (gw_trans->page > 0) &&
871 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
872 prev_disp == gw_trans->repos_total))) {
873 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
874 KATTR_ID, "np_wrapper", KATTR__MAX);
877 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
878 KATTR_ID, "nav_prev", KATTR__MAX);
883 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
884 (gw_trans->page > 0) &&
885 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
886 prev_disp == gw_trans->repos_total)) {
887 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
888 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
889 if (href_prev == NULL) {
890 error = got_error_from_errno("khttp_urlpartx");
893 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
894 KATTR_HREF, href_prev, KATTR__MAX);
897 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
900 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
905 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
907 return gw_kcgi_error(kerr);
909 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
910 next_disp == gw_trans->gw_conf->got_max_repos_display &&
911 dir_c != (gw_trans->page + 1) *
912 gw_trans->gw_conf->got_max_repos_display) {
913 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
914 KATTR_ID, "nav_next", KATTR__MAX);
917 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
918 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
919 if (href_next == NULL) {
920 error = got_error_from_errno("khttp_urlpartx");
923 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
924 KATTR_HREF, href_next, KATTR__MAX);
927 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
930 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
937 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
938 (gw_trans->page > 0) &&
939 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
940 prev_disp == gw_trans->repos_total)) {
941 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
955 if (error == NULL && kerr != KCGI_OK)
956 error = gw_kcgi_error(kerr);
960 static const struct got_error *
961 gw_commits(struct gw_trans *gw_trans)
963 const struct got_error *error = NULL;
964 struct gw_header *header = NULL, *n_header = NULL;
965 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
966 char *href_prev = NULL, *href_next = NULL;
967 enum kcgi_err kerr = KCGI_OK;
968 int commit_found = 0;
970 if ((header = gw_init_header()) == NULL)
971 return got_error_from_errno("malloc");
974 if (pledge("stdio rpath proc exec sendfd unveil",
976 error = got_error_from_errno("pledge");
980 error = gw_apply_unveil(gw_trans->gw_dir->path);
984 error = gw_get_header(gw_trans, header,
985 gw_trans->gw_conf->got_max_commits_display);
989 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
990 if (commit_found == 0 && gw_trans->commit_id != NULL) {
991 if (strcmp(gw_trans->commit_id,
992 n_header->commit_id) != 0)
997 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
998 "commits_line_wrapper", KATTR__MAX);
1001 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
1002 n_header->refs_str);
1005 error = gw_gen_author_header(gw_trans, n_header->author);
1008 error = gw_gen_committer_header(gw_trans, n_header->author);
1011 error = gw_get_time_str(&age, n_header->committer_time,
1015 error = gw_gen_age_header(gw_trans, age ?age : "");
1018 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1019 if (kerr != KCGI_OK)
1022 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1023 "dotted_line", KATTR__MAX);
1024 if (kerr != KCGI_OK)
1026 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1027 if (kerr != KCGI_OK)
1030 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1031 "commit", KATTR__MAX);
1032 if (kerr != KCGI_OK)
1034 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1035 if (kerr != KCGI_OK)
1037 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1038 if (kerr != KCGI_OK)
1041 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1042 gw_trans->repo_name, "action", "diff", "commit",
1043 n_header->commit_id, NULL);
1044 if (href_diff == NULL) {
1045 error = got_error_from_errno("khttp_urlpart");
1048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1049 KATTR_ID, "navs_wrapper", KATTR__MAX);
1050 if (kerr != KCGI_OK)
1052 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1053 KATTR_ID, "navs", KATTR__MAX);
1054 if (kerr != KCGI_OK)
1056 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1057 KATTR_HREF, href_diff, KATTR__MAX);
1058 if (kerr != KCGI_OK)
1060 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1061 if (kerr != KCGI_OK)
1063 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1064 if (kerr != KCGI_OK)
1067 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1068 if (kerr != KCGI_OK)
1071 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1072 gw_trans->repo_name, "action", "tree", "commit",
1073 n_header->commit_id, NULL);
1074 if (href_tree == NULL) {
1075 error = got_error_from_errno("khttp_urlpart");
1078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1079 KATTR_HREF, href_tree, KATTR__MAX);
1080 if (kerr != KCGI_OK)
1082 khtml_puts(gw_trans->gw_html_req, "tree");
1083 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1084 if (kerr != KCGI_OK)
1086 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1087 if (kerr != KCGI_OK)
1090 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1091 "solid_line", KATTR__MAX);
1092 if (kerr != KCGI_OK)
1094 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1095 if (kerr != KCGI_OK)
1102 if (gw_trans->next_id || gw_trans->prev_id) {
1103 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1104 KATTR_ID, "np_wrapper", KATTR__MAX);
1105 if (kerr != KCGI_OK)
1107 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1108 KATTR_ID, "nav_prev", KATTR__MAX);
1109 if (kerr != KCGI_OK)
1113 if (gw_trans->prev_id) {
1114 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1115 KATTRX_STRING, gw_trans->repo_name, "page",
1116 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1117 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1118 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1119 if (href_prev == NULL) {
1120 error = got_error_from_errno("khttp_urlpartx");
1123 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1124 KATTR_HREF, href_prev, KATTR__MAX);
1125 if (kerr != KCGI_OK)
1127 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1128 if (kerr != KCGI_OK)
1130 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1131 if (kerr != KCGI_OK)
1135 if (gw_trans->next_id || gw_trans->page > 0) {
1136 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1137 if (kerr != KCGI_OK)
1138 return gw_kcgi_error(kerr);
1141 if (gw_trans->next_id) {
1142 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1143 KATTR_ID, "nav_next", KATTR__MAX);
1144 if (kerr != KCGI_OK)
1146 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1147 KATTRX_STRING, gw_trans->repo_name, "page",
1148 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1149 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1150 gw_trans->next_id, NULL);
1151 if (href_next == NULL) {
1152 error = got_error_from_errno("khttp_urlpartx");
1155 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1156 KATTR_HREF, href_next, KATTR__MAX);
1157 if (kerr != KCGI_OK)
1159 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1160 if (kerr != KCGI_OK)
1162 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1163 if (kerr != KCGI_OK)
1167 if (gw_trans->next_id || gw_trans->page > 0) {
1168 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1169 if (kerr != KCGI_OK)
1173 gw_free_header(header);
1174 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1175 gw_free_header(n_header);
1181 if (error == NULL && kerr != KCGI_OK)
1182 error = gw_kcgi_error(kerr);
1186 static const struct got_error *
1187 gw_briefs(struct gw_trans *gw_trans)
1189 const struct got_error *error = NULL;
1190 struct gw_header *header = NULL, *n_header = NULL;
1191 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1192 char *href_prev = NULL, *href_next = NULL;
1193 char *newline, *smallerthan;
1194 enum kcgi_err kerr = KCGI_OK;
1195 int commit_found = 0;
1197 if ((header = gw_init_header()) == NULL)
1198 return got_error_from_errno("malloc");
1201 if (pledge("stdio rpath proc exec sendfd unveil",
1203 error = got_error_from_errno("pledge");
1207 if (gw_trans->action != GW_SUMMARY) {
1208 error = gw_apply_unveil(gw_trans->gw_dir->path);
1213 if (gw_trans->action == GW_SUMMARY)
1214 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1216 error = gw_get_header(gw_trans, header,
1217 gw_trans->gw_conf->got_max_commits_display);
1221 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1222 if (commit_found == 0 && gw_trans->commit_id != NULL) {
1223 if (strcmp(gw_trans->commit_id,
1224 n_header->commit_id) != 0)
1229 error = gw_get_time_str(&age, n_header->committer_time,
1234 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1235 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1236 if (kerr != KCGI_OK)
1239 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1240 KATTR_ID, "briefs_age", KATTR__MAX);
1241 if (kerr != KCGI_OK)
1243 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1244 if (kerr != KCGI_OK)
1246 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1247 if (kerr != KCGI_OK)
1250 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1251 KATTR_ID, "briefs_author", KATTR__MAX);
1252 if (kerr != KCGI_OK)
1254 smallerthan = strchr(n_header->author, '<');
1256 *smallerthan = '\0';
1257 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1258 if (kerr != KCGI_OK)
1260 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1261 if (kerr != KCGI_OK)
1264 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1265 gw_trans->repo_name, "action", "diff", "commit",
1266 n_header->commit_id, NULL);
1267 if (href_diff == NULL) {
1268 error = got_error_from_errno("khttp_urlpart");
1271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1272 KATTR_ID, "briefs_log", KATTR__MAX);
1273 if (kerr != KCGI_OK)
1275 newline = strchr(n_header->commit_msg, '\n');
1278 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1279 KATTR_HREF, href_diff, KATTR__MAX);
1280 if (kerr != KCGI_OK)
1282 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1283 if (kerr != KCGI_OK)
1285 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1286 if (kerr != KCGI_OK)
1289 if (n_header->refs_str) {
1290 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1291 if (kerr != KCGI_OK)
1293 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1294 KATTR_ID, "refs_str", KATTR__MAX);
1295 if (kerr != KCGI_OK)
1297 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1298 n_header->refs_str);
1299 if (kerr != KCGI_OK)
1301 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1302 if (kerr != KCGI_OK)
1306 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1307 if (kerr != KCGI_OK)
1310 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1311 KATTR_ID, "navs_wrapper", KATTR__MAX);
1312 if (kerr != KCGI_OK)
1314 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1315 KATTR_ID, "navs", KATTR__MAX);
1316 if (kerr != KCGI_OK)
1318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1319 KATTR_HREF, href_diff, KATTR__MAX);
1320 if (kerr != KCGI_OK)
1322 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1323 if (kerr != KCGI_OK)
1325 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1326 if (kerr != KCGI_OK)
1329 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1330 if (kerr != KCGI_OK)
1333 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1334 gw_trans->repo_name, "action", "tree", "commit",
1335 n_header->commit_id, NULL);
1336 if (href_tree == NULL) {
1337 error = got_error_from_errno("khttp_urlpart");
1340 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1341 KATTR_HREF, href_tree, KATTR__MAX);
1342 if (kerr != KCGI_OK)
1344 khtml_puts(gw_trans->gw_html_req, "tree");
1345 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1346 if (kerr != KCGI_OK)
1348 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1349 if (kerr != KCGI_OK)
1352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1353 KATTR_ID, "dotted_line", KATTR__MAX);
1354 if (kerr != KCGI_OK)
1356 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1357 if (kerr != KCGI_OK)
1368 if (gw_trans->next_id || gw_trans->prev_id) {
1369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1370 KATTR_ID, "np_wrapper", KATTR__MAX);
1371 if (kerr != KCGI_OK)
1373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1374 KATTR_ID, "nav_prev", KATTR__MAX);
1375 if (kerr != KCGI_OK)
1379 if (gw_trans->prev_id) {
1380 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1381 KATTRX_STRING, gw_trans->repo_name, "page",
1382 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1383 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1384 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1385 if (href_prev == NULL) {
1386 error = got_error_from_errno("khttp_urlpartx");
1389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1390 KATTR_HREF, href_prev, KATTR__MAX);
1391 if (kerr != KCGI_OK)
1393 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1394 if (kerr != KCGI_OK)
1396 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1397 if (kerr != KCGI_OK)
1401 if (gw_trans->next_id || gw_trans->page > 0) {
1402 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1403 if (kerr != KCGI_OK)
1404 return gw_kcgi_error(kerr);
1407 if (gw_trans->next_id) {
1408 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1409 KATTR_ID, "nav_next", KATTR__MAX);
1410 if (kerr != KCGI_OK)
1413 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1414 KATTRX_STRING, gw_trans->repo_name, "page",
1415 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1416 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1417 gw_trans->next_id, NULL);
1418 if (href_next == NULL) {
1419 error = got_error_from_errno("khttp_urlpartx");
1422 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1423 KATTR_HREF, href_next, KATTR__MAX);
1424 if (kerr != KCGI_OK)
1426 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1427 if (kerr != KCGI_OK)
1429 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1430 if (kerr != KCGI_OK)
1434 if (gw_trans->next_id || gw_trans->page > 0) {
1435 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1436 if (kerr != KCGI_OK)
1440 gw_free_header(header);
1441 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1442 gw_free_header(n_header);
1448 if (error == NULL && kerr != KCGI_OK)
1449 error = gw_kcgi_error(kerr);
1453 static const struct got_error *
1454 gw_summary(struct gw_trans *gw_trans)
1456 const struct got_error *error = NULL;
1458 enum kcgi_err kerr = KCGI_OK;
1461 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1462 return got_error_from_errno("pledge");
1464 error = gw_apply_unveil(gw_trans->gw_dir->path);
1468 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1469 "summary_wrapper", KATTR__MAX);
1470 if (kerr != KCGI_OK)
1471 return gw_kcgi_error(kerr);
1473 if (gw_trans->gw_conf->got_show_repo_description &&
1474 gw_trans->gw_dir->description != NULL &&
1475 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1476 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1477 KATTR_ID, "description_title", KATTR__MAX);
1478 if (kerr != KCGI_OK)
1480 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1481 if (kerr != KCGI_OK)
1483 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1484 if (kerr != KCGI_OK)
1486 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1487 KATTR_ID, "description", KATTR__MAX);
1488 if (kerr != KCGI_OK)
1490 kerr = khtml_puts(gw_trans->gw_html_req,
1491 gw_trans->gw_dir->description);
1492 if (kerr != KCGI_OK)
1494 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1495 if (kerr != KCGI_OK)
1499 if (gw_trans->gw_conf->got_show_repo_owner &&
1500 gw_trans->gw_dir->owner != NULL &&
1501 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1502 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1503 KATTR_ID, "repo_owner_title", KATTR__MAX);
1504 if (kerr != KCGI_OK)
1506 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1507 if (kerr != KCGI_OK)
1509 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1510 if (kerr != KCGI_OK)
1512 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1513 KATTR_ID, "repo_owner", KATTR__MAX);
1514 if (kerr != KCGI_OK)
1516 kerr = khtml_puts(gw_trans->gw_html_req,
1517 gw_trans->gw_dir->owner);
1518 if (kerr != KCGI_OK)
1520 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1521 if (kerr != KCGI_OK)
1525 if (gw_trans->gw_conf->got_show_repo_age) {
1526 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1531 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1532 KATTR_ID, "last_change_title", KATTR__MAX);
1533 if (kerr != KCGI_OK)
1535 kerr = khtml_puts(gw_trans->gw_html_req,
1537 if (kerr != KCGI_OK)
1539 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1540 if (kerr != KCGI_OK)
1542 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1543 KATTR_ID, "last_change", KATTR__MAX);
1544 if (kerr != KCGI_OK)
1546 kerr = khtml_puts(gw_trans->gw_html_req, age);
1547 if (kerr != KCGI_OK)
1549 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1550 if (kerr != KCGI_OK)
1555 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1556 gw_trans->gw_dir->url != NULL &&
1557 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1558 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1559 KATTR_ID, "cloneurl_title", KATTR__MAX);
1560 if (kerr != KCGI_OK)
1562 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1563 if (kerr != KCGI_OK)
1565 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1566 if (kerr != KCGI_OK)
1568 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1569 KATTR_ID, "cloneurl", KATTR__MAX);
1570 if (kerr != KCGI_OK)
1572 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1573 if (kerr != KCGI_OK)
1575 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1576 if (kerr != KCGI_OK)
1580 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1581 if (kerr != KCGI_OK)
1584 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1585 "briefs_title_wrapper", KATTR__MAX);
1586 if (kerr != KCGI_OK)
1588 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1589 "briefs_title", KATTR__MAX);
1590 if (kerr != KCGI_OK)
1592 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1593 if (kerr != KCGI_OK)
1595 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1596 if (kerr != KCGI_OK)
1598 error = gw_briefs(gw_trans);
1602 error = gw_tags(gw_trans);
1606 error = gw_output_repo_heads(gw_trans);
1609 if (error == NULL && kerr != KCGI_OK)
1610 error = gw_kcgi_error(kerr);
1614 static const struct got_error *
1615 gw_tree(struct gw_trans *gw_trans)
1617 const struct got_error *error = NULL;
1618 struct gw_header *header = NULL;
1619 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1621 enum kcgi_err kerr = KCGI_OK;
1624 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1625 return got_error_from_errno("pledge");
1627 if ((header = gw_init_header()) == NULL)
1628 return got_error_from_errno("malloc");
1630 error = gw_apply_unveil(gw_trans->gw_dir->path);
1634 error = gw_get_header(gw_trans, header, 1);
1638 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1639 "tree_header_wrapper", KATTR__MAX);
1640 if (kerr != KCGI_OK)
1642 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1643 "tree_header", KATTR__MAX);
1644 if (kerr != KCGI_OK)
1646 error = gw_gen_tree_header(gw_trans, header->tree_id);
1649 error = gw_get_time_str(&age, header->committer_time,
1653 error = gw_gen_age_header(gw_trans, age ?age : "");
1656 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1659 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1660 if (kerr != KCGI_OK)
1662 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1663 "dotted_line", KATTR__MAX);
1664 if (kerr != KCGI_OK)
1666 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1667 if (kerr != KCGI_OK)
1670 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1671 "tree", KATTR__MAX);
1672 if (kerr != KCGI_OK)
1674 error = gw_output_repo_tree(gw_trans, header);
1678 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1680 gw_free_header(header);
1681 free(tree_html_disp);
1685 if (error == NULL && kerr != KCGI_OK)
1686 error = gw_kcgi_error(kerr);
1690 static const struct got_error *
1691 gw_tags(struct gw_trans *gw_trans)
1693 const struct got_error *error = NULL;
1694 struct gw_header *header = NULL;
1695 char *href_next = NULL, *href_prev = NULL;
1696 enum kcgi_err kerr = KCGI_OK;
1699 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1700 return got_error_from_errno("pledge");
1702 if ((header = gw_init_header()) == NULL)
1703 return got_error_from_errno("malloc");
1705 if (gw_trans->action != GW_SUMMARY) {
1706 error = gw_apply_unveil(gw_trans->gw_dir->path);
1711 error = gw_get_header(gw_trans, header, 1);
1715 if (gw_trans->action == GW_SUMMARY) {
1716 gw_trans->next_id = NULL;
1717 error = gw_output_repo_tags(gw_trans, header,
1718 D_MAXSLCOMMDISP, TAGBRIEF);
1722 error = gw_output_repo_tags(gw_trans, header,
1723 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1728 if (gw_trans->next_id || gw_trans->page > 0) {
1729 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1730 KATTR_ID, "np_wrapper", KATTR__MAX);
1731 if (kerr != KCGI_OK)
1733 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1734 KATTR_ID, "nav_prev", KATTR__MAX);
1735 if (kerr != KCGI_OK)
1739 if (gw_trans->prev_id) {
1740 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1741 KATTRX_STRING, gw_trans->repo_name, "page",
1742 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1743 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1744 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1745 if (href_prev == NULL) {
1746 error = got_error_from_errno("khttp_urlpartx");
1749 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1750 KATTR_HREF, href_prev, KATTR__MAX);
1751 if (kerr != KCGI_OK)
1753 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1754 if (kerr != KCGI_OK)
1756 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1757 if (kerr != KCGI_OK)
1761 if (gw_trans->next_id || gw_trans->page > 0) {
1762 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1763 if (kerr != KCGI_OK)
1764 return gw_kcgi_error(kerr);
1767 if (gw_trans->next_id) {
1768 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1769 KATTR_ID, "nav_next", KATTR__MAX);
1770 if (kerr != KCGI_OK)
1772 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1773 KATTRX_STRING, gw_trans->repo_name, "page",
1774 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1775 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1776 gw_trans->next_id, NULL);
1777 if (href_next == NULL) {
1778 error = got_error_from_errno("khttp_urlpartx");
1781 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1782 KATTR_HREF, href_next, KATTR__MAX);
1783 if (kerr != KCGI_OK)
1785 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1786 if (kerr != KCGI_OK)
1788 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1789 if (kerr != KCGI_OK)
1793 if (gw_trans->next_id || gw_trans->page > 0) {
1794 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1795 if (kerr != KCGI_OK)
1799 gw_free_header(header);
1802 if (error == NULL && kerr != KCGI_OK)
1803 error = gw_kcgi_error(kerr);
1807 static const struct got_error *
1808 gw_tag(struct gw_trans *gw_trans)
1810 const struct got_error *error = NULL;
1811 struct gw_header *header = NULL;
1812 enum kcgi_err kerr = KCGI_OK;
1815 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1816 return got_error_from_errno("pledge");
1818 if ((header = gw_init_header()) == NULL)
1819 return got_error_from_errno("malloc");
1821 error = gw_apply_unveil(gw_trans->gw_dir->path);
1825 if (gw_trans->commit_id == NULL) {
1826 error = got_error_msg(GOT_ERR_QUERYSTRING,
1827 "commit required in querystring");
1831 error = gw_get_header(gw_trans, header, 1);
1835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1836 "tag_header_wrapper", KATTR__MAX);
1837 if (kerr != KCGI_OK)
1839 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1840 "tag_header", KATTR__MAX);
1841 if (kerr != KCGI_OK)
1843 error = gw_gen_commit_header(gw_trans, header->commit_id,
1847 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1850 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1851 if (kerr != KCGI_OK)
1853 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1854 "dotted_line", KATTR__MAX);
1855 if (kerr != KCGI_OK)
1857 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1858 if (kerr != KCGI_OK)
1861 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1862 "tree", KATTR__MAX);
1863 if (kerr != KCGI_OK)
1866 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1870 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1872 gw_free_header(header);
1873 if (error == NULL && kerr != KCGI_OK)
1874 error = gw_kcgi_error(kerr);
1878 static const struct got_error *
1879 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1881 const struct got_error *error = NULL;
1886 if (asprintf(&dir_test, "%s/%s/%s",
1887 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1888 GOTWEB_GIT_DIR) == -1)
1889 return got_error_from_errno("asprintf");
1891 dt = opendir(dir_test);
1895 gw_dir->path = strdup(dir_test);
1896 if (gw_dir->path == NULL) {
1898 error = got_error_from_errno("strdup");
1905 if (asprintf(&dir_test, "%s/%s/%s",
1906 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1907 GOTWEB_GOT_DIR) == -1) {
1909 error = got_error_from_errno("asprintf");
1913 dt = opendir(dir_test);
1918 error = got_error(GOT_ERR_NOT_GIT_REPO);
1922 if (asprintf(&dir_test, "%s/%s",
1923 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1924 error = got_error_from_errno("asprintf");
1929 gw_dir->path = strdup(dir_test);
1930 if (gw_dir->path == NULL) {
1932 error = got_error_from_errno("strdup");
1936 dt = opendir(dir_test);
1938 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1943 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1947 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1950 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1954 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1958 if (dt && closedir(dt) == -1 && error == NULL)
1959 error = got_error_from_errno("closedir");
1963 static const struct got_error *
1964 gw_load_got_paths(struct gw_trans *gw_trans)
1966 const struct got_error *error = NULL;
1968 struct dirent **sd_dent;
1969 struct gw_dir *gw_dir;
1971 unsigned int d_cnt, d_i;
1973 d = opendir(gw_trans->gw_conf->got_repos_path);
1975 error = got_error_from_errno2("opendir",
1976 gw_trans->gw_conf->got_repos_path);
1980 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1983 error = got_error_from_errno2("scandir",
1984 gw_trans->gw_conf->got_repos_path);
1988 for (d_i = 0; d_i < d_cnt; d_i++) {
1989 if (gw_trans->gw_conf->got_max_repos > 0 &&
1990 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1991 break; /* account for parent and self */
1993 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1994 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1997 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
2001 error = gw_load_got_path(gw_trans, gw_dir);
2002 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
2005 } else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
2008 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
2009 !got_path_dir_is_empty(gw_dir->path)) {
2010 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
2012 gw_trans->repos_total++;
2016 if (d && closedir(d) == -1 && error == NULL)
2017 error = got_error_from_errno("closedir");
2021 static const struct got_error *
2022 gw_parse_querystring(struct gw_trans *gw_trans)
2024 const struct got_error *error = NULL;
2026 const struct gw_query_action *action = NULL;
2029 if (gw_trans->gw_req->fieldnmap[0]) {
2030 return got_error(GOT_ERR_QUERYSTRING);
2031 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2032 /* define gw_trans->repo_path */
2033 gw_trans->repo_name = p->parsed.s;
2035 if (asprintf(&gw_trans->repo_path, "%s/%s",
2036 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2037 return got_error_from_errno("asprintf");
2039 /* get action and set function */
2040 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2041 for (i = 0; i < nitems(gw_query_funcs); i++) {
2042 action = &gw_query_funcs[i];
2043 if (action->func_name == NULL)
2045 if (strcmp(action->func_name,
2046 p->parsed.s) == 0) {
2047 gw_trans->action = i;
2052 if (gw_trans->action == -1) {
2053 gw_trans->action = GW_ERR;
2054 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2055 p != NULL ? "bad action in querystring" :
2056 "no action in querystring");
2060 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2061 if (asprintf(&gw_trans->commit_id, "%s",
2063 return got_error_from_errno("asprintf");
2066 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2067 gw_trans->repo_file = p->parsed.s;
2069 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2070 if (asprintf(&gw_trans->repo_folder, "%s",
2072 return got_error_from_errno("asprintf");
2075 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2076 if (asprintf(&gw_trans->prev_id, "%s",
2078 return got_error_from_errno("asprintf");
2081 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2082 gw_trans->headref = p->parsed.s;
2084 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2088 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2090 gw_trans->action = GW_INDEX;
2092 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2093 gw_trans->page = p->parsed.i;
2098 static const struct got_error *
2099 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2101 const struct got_error *error;
2103 *gw_dir = malloc(sizeof(**gw_dir));
2104 if (*gw_dir == NULL)
2105 return got_error_from_errno("malloc");
2107 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2108 error = got_error_from_errno("asprintf");
2117 static const struct got_error *
2118 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2120 enum kcgi_err kerr = KCGI_OK;
2122 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2123 if (kerr != KCGI_OK)
2124 return gw_kcgi_error(kerr);
2125 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2127 if (kerr != KCGI_OK)
2128 return gw_kcgi_error(kerr);
2129 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2131 if (kerr != KCGI_OK)
2132 return gw_kcgi_error(kerr);
2133 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2135 if (kerr != KCGI_OK)
2136 return gw_kcgi_error(kerr);
2137 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2138 if (kerr != KCGI_OK)
2139 return gw_kcgi_error(kerr);
2140 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2142 if (kerr != KCGI_OK)
2143 return gw_kcgi_error(kerr);
2145 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2146 kerr = khttp_head(gw_trans->gw_req,
2147 kresps[KRESP_CONTENT_DISPOSITION],
2148 "attachment; filename=%s", gw_trans->repo_file);
2149 if (kerr != KCGI_OK)
2150 return gw_kcgi_error(kerr);
2153 kerr = khttp_body(gw_trans->gw_req);
2154 return gw_kcgi_error(kerr);
2157 static const struct got_error *
2158 gw_display_index(struct gw_trans *gw_trans)
2160 const struct got_error *error;
2161 enum kcgi_err kerr = KCGI_OK;
2163 /* catch early querystring errors */
2164 if (gw_trans->error)
2165 gw_trans->action = GW_ERR;
2167 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2171 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2172 if (kerr != KCGI_OK)
2173 return gw_kcgi_error(kerr);
2175 if (gw_trans->action != GW_BLOB) {
2176 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2177 gw_query_funcs[gw_trans->action].template);
2178 if (kerr != KCGI_OK) {
2179 khtml_close(gw_trans->gw_html_req);
2180 return gw_kcgi_error(kerr);
2184 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2187 static const struct got_error *
2188 gw_error(struct gw_trans *gw_trans)
2190 enum kcgi_err kerr = KCGI_OK;
2192 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2194 return gw_kcgi_error(kerr);
2198 gw_template(size_t key, void *arg)
2200 const struct got_error *error = NULL;
2201 enum kcgi_err kerr = KCGI_OK;
2202 struct gw_trans *gw_trans = arg;
2203 char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
2204 char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
2206 if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
2207 "/apple-touch-icon.png") == -1)
2209 if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
2210 "/favicon-32x32.png") == -1)
2212 if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
2213 "/favicon-16x16.png") == -1)
2215 if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
2216 "/site.webmanifest") == -1)
2218 if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
2219 "/safari-pinned-tab.svg") == -1)
2221 if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
2222 "/gotweb.css") == -1)
2224 if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
2225 gw_trans->gw_conf->got_www_path ? "/" : "",
2226 gw_trans->gw_conf->got_logo) == -1)
2231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2232 KATTR_NAME, "viewport",
2233 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2235 if (kerr != KCGI_OK)
2237 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2238 if (kerr != KCGI_OK)
2240 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2241 KATTR_CHARSET, "utf-8",
2243 if (kerr != KCGI_OK)
2245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2246 if (kerr != KCGI_OK)
2248 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2249 KATTR_NAME, "msapplication-TileColor",
2250 KATTR_CONTENT, "#da532c", KATTR__MAX);
2251 if (kerr != KCGI_OK)
2253 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2254 if (kerr != KCGI_OK)
2256 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2257 KATTR_NAME, "theme-color",
2258 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2259 if (kerr != KCGI_OK)
2261 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2262 if (kerr != KCGI_OK)
2264 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2265 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2266 KATTR_HREF, ati, KATTR__MAX);
2267 if (kerr != KCGI_OK)
2269 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2270 if (kerr != KCGI_OK)
2272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2273 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2274 "32x32", KATTR_HREF, fic32, KATTR__MAX);
2275 if (kerr != KCGI_OK)
2277 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2278 if (kerr != KCGI_OK)
2280 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2281 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2282 "16x16", KATTR_HREF, fic16, KATTR__MAX);
2283 if (kerr != KCGI_OK)
2285 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2286 if (kerr != KCGI_OK)
2288 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2289 KATTR_REL, "manifest", KATTR_HREF, swm,
2291 if (kerr != KCGI_OK)
2293 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2294 if (kerr != KCGI_OK)
2296 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2297 KATTR_REL, "mask-icon", KATTR_HREF,
2299 if (kerr != KCGI_OK)
2301 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2302 if (kerr != KCGI_OK)
2304 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2305 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2306 KATTR_HREF, css, KATTR__MAX);
2307 if (kerr != KCGI_OK)
2309 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2310 if (kerr != KCGI_OK)
2314 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2315 KATTR_ID, "got_link", KATTR__MAX);
2316 if (kerr != KCGI_OK)
2318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2319 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2320 KATTR_TARGET, "_sotd", KATTR__MAX);
2321 if (kerr != KCGI_OK)
2323 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2324 KATTR_SRC, logo, KATTR__MAX);
2325 if (kerr != KCGI_OK)
2327 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2328 if (kerr != KCGI_OK)
2331 case (TEMPL_SITEPATH):
2332 error = gw_output_site_link(gw_trans);
2337 if (gw_trans->gw_conf->got_site_name != NULL) {
2338 kerr = khtml_puts(gw_trans->gw_html_req,
2339 gw_trans->gw_conf->got_site_name);
2340 if (kerr != KCGI_OK)
2344 case (TEMPL_SEARCH):
2346 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2347 "search", KATTR__MAX);
2348 if (kerr != KCGI_OK)
2350 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2351 KATTR_METHOD, "POST", KATTR__MAX);
2352 if (kerr != KCGI_OK)
2354 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2355 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2356 KATTR_MAXLENGTH, "50", KATTR__MAX);
2357 if (kerr != KCGI_OK)
2359 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2361 if (kerr != KCGI_OK)
2363 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2364 if (kerr != KCGI_OK)
2366 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2367 if (kerr != KCGI_OK)
2370 case(TEMPL_SITEOWNER):
2371 if (gw_trans->gw_conf->got_site_owner != NULL &&
2372 gw_trans->gw_conf->got_show_site_owner) {
2373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2374 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2375 if (kerr != KCGI_OK)
2377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2378 KATTR_ID, "site_owner", KATTR__MAX);
2379 if (kerr != KCGI_OK)
2381 kerr = khtml_puts(gw_trans->gw_html_req,
2382 gw_trans->gw_conf->got_site_owner);
2383 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2384 if (kerr != KCGI_OK)
2388 case(TEMPL_CONTENT):
2389 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2392 KATTR_ID, "tmpl_err", KATTR__MAX);
2393 if (kerr != KCGI_OK)
2395 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2397 if (kerr != KCGI_OK)
2399 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2400 if (kerr != KCGI_OK)
2426 static const struct got_error *
2427 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2429 const struct got_error *error = NULL;
2430 enum kcgi_err kerr = KCGI_OK;
2432 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2433 KATTR_ID, "header_commit_title", KATTR__MAX);
2434 if (kerr != KCGI_OK)
2436 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2437 if (kerr != KCGI_OK)
2439 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2440 if (kerr != KCGI_OK)
2442 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2443 KATTR_ID, "header_commit", KATTR__MAX);
2444 if (kerr != KCGI_OK)
2446 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2447 if (kerr != KCGI_OK)
2450 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2451 KATTR_ID, "refs_str", KATTR__MAX);
2452 if (kerr != KCGI_OK)
2454 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2455 if (kerr != KCGI_OK)
2457 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2458 if (kerr != KCGI_OK)
2461 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2463 if (error == NULL && kerr != KCGI_OK)
2464 error = gw_kcgi_error(kerr);
2468 static const struct got_error *
2469 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2471 const struct got_error *error = NULL;
2472 enum kcgi_err kerr = KCGI_OK;
2474 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2475 KATTR_ID, "header_diff_title", KATTR__MAX);
2476 if (kerr != KCGI_OK)
2478 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2479 if (kerr != KCGI_OK)
2481 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2482 if (kerr != KCGI_OK)
2484 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2485 KATTR_ID, "header_diff", KATTR__MAX);
2486 if (kerr != KCGI_OK)
2489 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2490 if (kerr != KCGI_OK)
2493 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2494 if (kerr != KCGI_OK)
2496 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2497 if (kerr != KCGI_OK)
2499 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2501 if (error == NULL && kerr != KCGI_OK)
2502 error = gw_kcgi_error(kerr);
2506 static const struct got_error *
2507 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2509 const struct got_error *error = NULL;
2510 enum kcgi_err kerr = KCGI_OK;
2512 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2513 KATTR_ID, "header_age_title", KATTR__MAX);
2514 if (kerr != KCGI_OK)
2516 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2517 if (kerr != KCGI_OK)
2519 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2520 if (kerr != KCGI_OK)
2522 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2523 KATTR_ID, "header_age", KATTR__MAX);
2524 if (kerr != KCGI_OK)
2526 kerr = khtml_puts(gw_trans->gw_html_req, str);
2527 if (kerr != KCGI_OK)
2529 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2531 if (error == NULL && kerr != KCGI_OK)
2532 error = gw_kcgi_error(kerr);
2536 static const struct got_error *
2537 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2539 const struct got_error *error = NULL;
2540 enum kcgi_err kerr = KCGI_OK;
2542 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2543 KATTR_ID, "header_author_title", KATTR__MAX);
2544 if (kerr != KCGI_OK)
2546 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2547 if (kerr != KCGI_OK)
2549 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2550 if (kerr != KCGI_OK)
2552 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2553 KATTR_ID, "header_author", KATTR__MAX);
2554 if (kerr != KCGI_OK)
2556 kerr = khtml_puts(gw_trans->gw_html_req, str);
2557 if (kerr != KCGI_OK)
2559 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2561 if (error == NULL && kerr != KCGI_OK)
2562 error = gw_kcgi_error(kerr);
2566 static const struct got_error *
2567 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2569 const struct got_error *error = NULL;
2570 enum kcgi_err kerr = KCGI_OK;
2572 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2573 KATTR_ID, "header_committer_title", KATTR__MAX);
2574 if (kerr != KCGI_OK)
2576 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2577 if (kerr != KCGI_OK)
2579 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2580 if (kerr != KCGI_OK)
2582 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2583 KATTR_ID, "header_committer", KATTR__MAX);
2584 if (kerr != KCGI_OK)
2586 kerr = khtml_puts(gw_trans->gw_html_req, str);
2587 if (kerr != KCGI_OK)
2589 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2591 if (error == NULL && kerr != KCGI_OK)
2592 error = gw_kcgi_error(kerr);
2596 static const struct got_error *
2597 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2599 const struct got_error *error = NULL;
2600 enum kcgi_err kerr = KCGI_OK;
2602 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2603 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2604 if (kerr != KCGI_OK)
2606 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2607 if (kerr != KCGI_OK)
2609 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2610 if (kerr != KCGI_OK)
2612 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2613 KATTR_ID, "header_commit_msg", KATTR__MAX);
2614 if (kerr != KCGI_OK)
2616 kerr = khttp_puts(gw_trans->gw_req, str);
2617 if (kerr != KCGI_OK)
2619 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2621 if (error == NULL && kerr != KCGI_OK)
2622 error = gw_kcgi_error(kerr);
2626 static const struct got_error *
2627 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2629 const struct got_error *error = NULL;
2630 enum kcgi_err kerr = KCGI_OK;
2632 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2633 KATTR_ID, "header_tree_title", KATTR__MAX);
2634 if (kerr != KCGI_OK)
2636 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2637 if (kerr != KCGI_OK)
2639 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2640 if (kerr != KCGI_OK)
2642 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2643 KATTR_ID, "header_tree", KATTR__MAX);
2644 if (kerr != KCGI_OK)
2646 kerr = khtml_puts(gw_trans->gw_html_req, str);
2647 if (kerr != KCGI_OK)
2649 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2651 if (error == NULL && kerr != KCGI_OK)
2652 error = gw_kcgi_error(kerr);
2656 static const struct got_error *
2657 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2660 const struct got_error *error = NULL;
2662 char *d_file = NULL;
2666 *description = NULL;
2667 if (gw_trans->gw_conf->got_show_repo_description == 0)
2670 if (asprintf(&d_file, "%s/description", dir) == -1)
2671 return got_error_from_errno("asprintf");
2673 f = fopen(d_file, "re");
2675 if (errno == ENOENT || errno == EACCES)
2677 error = got_error_from_errno2("fopen", d_file);
2681 if (fseek(f, 0, SEEK_END) == -1) {
2682 error = got_ferror(f, GOT_ERR_IO);
2687 error = got_ferror(f, GOT_ERR_IO);
2690 if (fseek(f, 0, SEEK_SET) == -1) {
2691 error = got_ferror(f, GOT_ERR_IO);
2694 *description = calloc(len + 1, sizeof(**description));
2695 if (*description == NULL) {
2696 error = got_error_from_errno("calloc");
2700 n = fread(*description, 1, len, f);
2701 if (n == 0 && ferror(f))
2702 error = got_ferror(f, GOT_ERR_IO);
2704 if (f != NULL && fclose(f) == EOF && error == NULL)
2705 error = got_error_from_errno("fclose");
2710 static const struct got_error *
2711 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2715 char *years = "years ago", *months = "months ago";
2716 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2717 char *minutes = "minutes ago", *seconds = "seconds ago";
2718 char *now = "right now";
2726 diff_time = time(NULL) - committer_time;
2727 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2728 if (asprintf(repo_age, "%lld %s",
2729 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2730 return got_error_from_errno("asprintf");
2731 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2732 if (asprintf(repo_age, "%lld %s",
2733 (diff_time / 60 / 60 / 24 / (365 / 12)),
2735 return got_error_from_errno("asprintf");
2736 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2737 if (asprintf(repo_age, "%lld %s",
2738 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2739 return got_error_from_errno("asprintf");
2740 } else if (diff_time > 60 * 60 * 24 * 2) {
2741 if (asprintf(repo_age, "%lld %s",
2742 (diff_time / 60 / 60 / 24), days) == -1)
2743 return got_error_from_errno("asprintf");
2744 } else if (diff_time > 60 * 60 * 2) {
2745 if (asprintf(repo_age, "%lld %s",
2746 (diff_time / 60 / 60), hours) == -1)
2747 return got_error_from_errno("asprintf");
2748 } else if (diff_time > 60 * 2) {
2749 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2751 return got_error_from_errno("asprintf");
2752 } else if (diff_time > 2) {
2753 if (asprintf(repo_age, "%lld %s", diff_time,
2755 return got_error_from_errno("asprintf");
2757 if (asprintf(repo_age, "%s", now) == -1)
2758 return got_error_from_errno("asprintf");
2762 if (gmtime_r(&committer_time, &tm) == NULL)
2763 return got_error_from_errno("gmtime_r");
2765 s = asctime_r(&tm, datebuf);
2767 return got_error_from_errno("asctime_r");
2769 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2770 return got_error_from_errno("asprintf");
2776 static const struct got_error *
2777 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2778 const char *refname, int ref_tm)
2780 const struct got_error *error = NULL;
2781 struct got_repository *repo = NULL;
2782 struct got_commit_object *commit = NULL;
2783 struct got_reflist_head refs;
2784 struct got_reflist_entry *re;
2785 time_t committer_time = 0, cmp_time = 0;
2790 if (gw_trans->gw_conf->got_show_repo_age == 0)
2794 repo = gw_trans->repo;
2796 error = got_repo_open(&repo, dir, NULL);
2801 error = got_ref_list(&refs, repo, "refs/heads",
2802 got_ref_cmp_by_name, NULL);
2807 * Find the youngest branch tip in the repository, or the age of
2808 * the a specific branch tip if a name was provided by the caller.
2810 TAILQ_FOREACH(re, &refs, entry) {
2811 struct got_object_id *id = NULL;
2813 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2816 error = got_ref_resolve(&id, repo, re->ref);
2820 error = got_object_open_as_commit(&commit, repo, id);
2826 got_object_commit_get_committer_time(commit);
2827 got_object_commit_close(commit);
2828 if (cmp_time < committer_time)
2829 cmp_time = committer_time;
2835 if (cmp_time != 0) {
2836 committer_time = cmp_time;
2837 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2840 got_ref_list_free(&refs);
2841 if (gw_trans->repo == NULL) {
2842 const struct got_error *close_err = got_repo_close(repo);
2849 static const struct got_error *
2850 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2852 const struct got_error *error;
2853 FILE *f = NULL, *f1 = NULL, *f2 = NULL;
2854 struct got_object_id *id1 = NULL, *id2 = NULL;
2855 char *label1 = NULL, *label2 = NULL, *line = NULL;
2857 size_t linesize = 0;
2859 enum kcgi_err kerr = KCGI_OK;
2865 f1 = got_opentemp();
2867 error = got_error_from_errno("got_opentemp");
2871 f2 = got_opentemp();
2873 error = got_error_from_errno("got_opentemp");
2877 if (header->parent_id != NULL &&
2878 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2879 error = got_repo_match_object_id(&id1, &label1,
2880 header->parent_id, GOT_OBJ_TYPE_ANY,
2881 &header->refs, gw_trans->repo);
2886 error = got_repo_match_object_id(&id2, &label2,
2887 header->commit_id, GOT_OBJ_TYPE_ANY, &header->refs,
2892 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2896 case GOT_OBJ_TYPE_BLOB:
2897 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
2898 id1, id2, NULL, NULL, 3, 0, 0, gw_trans->repo, f);
2900 case GOT_OBJ_TYPE_TREE:
2901 error = got_diff_objects_as_trees(NULL, NULL, f1, f2,
2902 id1, id2, NULL, "", "", 3, 0, 0, gw_trans->repo, f);
2904 case GOT_OBJ_TYPE_COMMIT:
2905 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
2906 id1, id2, NULL, 3, 0, 0, gw_trans->repo, f);
2909 error = got_error(GOT_ERR_OBJ_TYPE);
2914 if (fseek(f, 0, SEEK_SET) == -1) {
2915 error = got_ferror(f, GOT_ERR_IO);
2919 while ((linelen = getline(&line, &linesize, f)) != -1) {
2920 error = gw_colordiff_line(gw_trans, line);
2923 /* XXX: KHTML_PRETTY breaks this */
2924 kerr = khtml_puts(gw_trans->gw_html_req, line);
2925 if (kerr != KCGI_OK)
2927 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2928 if (kerr != KCGI_OK)
2931 if (linelen == -1 && ferror(f))
2932 error = got_error_from_errno("getline");
2934 if (f && fclose(f) == EOF && error == NULL)
2935 error = got_error_from_errno("fclose");
2936 if (f1 && fclose(f1) == EOF && error == NULL)
2937 error = got_error_from_errno("fclose");
2938 if (f2 && fclose(f2) == EOF && error == NULL)
2939 error = got_error_from_errno("fclose");
2946 if (error == NULL && kerr != KCGI_OK)
2947 error = gw_kcgi_error(kerr);
2951 static const struct got_error *
2952 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2954 const struct got_error *error = NULL, *close_err;
2955 struct got_repository *repo;
2956 const char *gitconfig_owner;
2960 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2963 error = got_repo_open(&repo, dir, NULL);
2966 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2967 if (gitconfig_owner) {
2968 *owner = strdup(gitconfig_owner);
2970 error = got_error_from_errno("strdup");
2972 close_err = got_repo_close(repo);
2978 static const struct got_error *
2979 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2981 const struct got_error *error = NULL;
2983 char *d_file = NULL;
2989 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2990 return got_error_from_errno("asprintf");
2992 f = fopen(d_file, "re");
2994 if (errno != ENOENT && errno != EACCES)
2995 error = got_error_from_errno2("fopen", d_file);
2999 if (fseek(f, 0, SEEK_END) == -1) {
3000 error = got_ferror(f, GOT_ERR_IO);
3005 error = got_ferror(f, GOT_ERR_IO);
3008 if (fseek(f, 0, SEEK_SET) == -1) {
3009 error = got_ferror(f, GOT_ERR_IO);
3013 *url = calloc(len + 1, sizeof(**url));
3015 error = got_error_from_errno("calloc");
3019 n = fread(*url, 1, len, f);
3020 if (n == 0 && ferror(f))
3021 error = got_ferror(f, GOT_ERR_IO);
3023 if (f && fclose(f) == EOF && error == NULL)
3024 error = got_error_from_errno("fclose");
3029 static const struct got_error *
3030 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3031 int limit, int tag_type)
3033 const struct got_error *error = NULL;
3034 struct got_reflist_head refs;
3035 struct got_reflist_entry *re;
3037 char *id_str = NULL, *newline, *href_commits = NULL;
3038 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3039 struct got_tag_object *tag = NULL;
3040 enum kcgi_err kerr = KCGI_OK;
3041 int summary_header_displayed = 0, chk_next = 0;
3042 int tag_count = 0, commit_found = 0, c_cnt = 0;
3046 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3047 got_ref_cmp_tags, gw_trans->repo);
3051 TAILQ_FOREACH(re, &refs, entry) {
3052 const char *refname;
3054 const char *tag_commit;
3056 struct got_object_id *id;
3057 struct got_commit_object *commit = NULL;
3059 refname = got_ref_get_name(re->ref);
3060 if (strncmp(refname, "refs/tags/", 10) != 0)
3064 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3068 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3070 if (error->code != GOT_ERR_OBJ_TYPE) {
3074 /* "lightweight" tag */
3075 error = got_object_open_as_commit(&commit,
3076 gw_trans->repo, id);
3081 tagger = got_object_commit_get_committer(commit);
3083 got_object_commit_get_committer_time(commit);
3084 error = got_object_id_str(&id_str, id);
3088 tagger = got_object_tag_get_tagger(tag);
3089 tagger_time = got_object_tag_get_tagger_time(tag);
3090 error = got_object_id_str(&id_str,
3091 got_object_tag_get_object_id(tag));
3096 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3097 strlen(id_str)) != 0)
3100 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3101 commit_found == 0 && strncmp(id_str, gw_trans->commit_id,
3102 strlen(id_str)) != 0)
3110 gw_trans->next_id = strdup(id_str);
3111 if (gw_trans->next_id == NULL)
3112 error = got_error_from_errno("strdup");
3117 error = got_object_commit_get_logmsg(&tag_commit0,
3121 got_object_commit_close(commit);
3123 tag_commit0 = strdup(got_object_tag_get_message(tag));
3124 if (tag_commit0 == NULL) {
3125 error = got_error_from_errno("strdup");
3130 tag_commit = tag_commit0;
3131 while (*tag_commit == '\n')
3136 newline = strchr(tag_commit, '\n');
3140 if (summary_header_displayed == 0) {
3141 kerr = khtml_attr(gw_trans->gw_html_req,
3142 KELEM_DIV, KATTR_ID,
3143 "summary_tags_title_wrapper", KATTR__MAX);
3144 if (kerr != KCGI_OK)
3146 kerr = khtml_attr(gw_trans->gw_html_req,
3147 KELEM_DIV, KATTR_ID,
3148 "summary_tags_title", KATTR__MAX);
3149 if (kerr != KCGI_OK)
3151 kerr = khtml_puts(gw_trans->gw_html_req,
3153 if (kerr != KCGI_OK)
3155 kerr = khtml_closeelem(gw_trans->gw_html_req,
3157 if (kerr != KCGI_OK)
3159 kerr = khtml_attr(gw_trans->gw_html_req,
3160 KELEM_DIV, KATTR_ID,
3161 "summary_tags_content", KATTR__MAX);
3162 if (kerr != KCGI_OK)
3164 summary_header_displayed = 1;
3167 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3168 KATTR_ID, "tag_wrapper", KATTR__MAX);
3169 if (kerr != KCGI_OK)
3171 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3172 KATTR_ID, "tag_age", KATTR__MAX);
3173 if (kerr != KCGI_OK)
3175 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3178 kerr = khtml_puts(gw_trans->gw_html_req,
3180 if (kerr != KCGI_OK)
3182 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3183 if (kerr != KCGI_OK)
3185 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3186 KATTR_ID, "tag", KATTR__MAX);
3187 if (kerr != KCGI_OK)
3189 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3190 if (kerr != KCGI_OK)
3192 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3193 if (kerr != KCGI_OK)
3195 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3196 KATTR_ID, "tag_name", KATTR__MAX);
3197 if (kerr != KCGI_OK)
3200 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3201 gw_trans->repo_name, "action", "tag", "commit",
3203 if (href_tag == NULL) {
3204 error = got_error_from_errno("khttp_urlpart");
3207 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3208 KATTR_HREF, href_tag, KATTR__MAX);
3209 if (kerr != KCGI_OK)
3211 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3212 if (kerr != KCGI_OK)
3214 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3215 if (kerr != KCGI_OK)
3218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3219 KATTR_ID, "navs_wrapper", KATTR__MAX);
3220 if (kerr != KCGI_OK)
3222 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3223 KATTR_ID, "navs", KATTR__MAX);
3224 if (kerr != KCGI_OK)
3227 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3228 KATTR_HREF, href_tag, KATTR__MAX);
3229 if (kerr != KCGI_OK)
3231 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3232 if (kerr != KCGI_OK)
3234 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3235 if (kerr != KCGI_OK)
3238 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3239 if (kerr != KCGI_OK)
3242 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3243 "path", gw_trans->repo_name, "action", "briefs",
3244 "commit", id_str, NULL);
3245 if (href_briefs == NULL) {
3246 error = got_error_from_errno("khttp_urlpart");
3249 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3250 KATTR_HREF, href_briefs, KATTR__MAX);
3251 if (kerr != KCGI_OK)
3253 kerr = khtml_puts(gw_trans->gw_html_req,
3255 if (kerr != KCGI_OK)
3257 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3258 if (kerr != KCGI_OK)
3261 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3262 if (kerr != KCGI_OK)
3265 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3266 "path", gw_trans->repo_name, "action", "commits",
3267 "commit", id_str, NULL);
3268 if (href_commits == NULL) {
3269 error = got_error_from_errno("khttp_urlpart");
3272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3273 KATTR_HREF, href_commits, KATTR__MAX);
3274 if (kerr != KCGI_OK)
3276 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3277 if (kerr != KCGI_OK)
3279 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3280 if (kerr != KCGI_OK)
3283 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3284 KATTR_ID, "dotted_line", KATTR__MAX);
3285 if (kerr != KCGI_OK)
3287 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3288 if (kerr != KCGI_OK)
3292 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3293 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3294 if (kerr != KCGI_OK)
3296 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3297 if (kerr != KCGI_OK)
3299 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3300 if (kerr != KCGI_OK)
3302 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3303 KATTR_ID, "tag_info_date", KATTR__MAX);
3304 if (kerr != KCGI_OK)
3306 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3309 kerr = khtml_puts(gw_trans->gw_html_req,
3311 if (kerr != KCGI_OK)
3313 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3314 if (kerr != KCGI_OK)
3317 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3318 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3319 if (kerr != KCGI_OK)
3321 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3322 if (kerr != KCGI_OK)
3324 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3325 if (kerr != KCGI_OK)
3327 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3328 KATTR_ID, "tag_info_date", KATTR__MAX);
3329 if (kerr != KCGI_OK)
3331 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3332 if (kerr != KCGI_OK)
3334 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3335 if (kerr != KCGI_OK)
3338 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3339 KATTR_ID, "tag_info", KATTR__MAX);
3340 if (kerr != KCGI_OK)
3342 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3343 if (kerr != KCGI_OK)
3349 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3350 if (kerr != KCGI_OK)
3353 if (limit && --limit == 0)
3357 got_object_tag_close(tag);
3370 href_commits = NULL;
3372 if (tag_count == 0) {
3373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3374 "summary_tags_title_wrapper", KATTR__MAX);
3375 if (kerr != KCGI_OK)
3377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3378 "summary_tags_title", KATTR__MAX);
3379 if (kerr != KCGI_OK)
3381 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3382 if (kerr != KCGI_OK)
3384 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3385 if (kerr != KCGI_OK)
3387 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3388 "summary_tags_content", KATTR__MAX);
3389 if (kerr != KCGI_OK)
3391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3392 "tags_info", KATTR__MAX);
3393 if (kerr != KCGI_OK)
3395 kerr = khttp_puts(gw_trans->gw_req,
3396 "There are no tags for this repo.");
3397 if (kerr != KCGI_OK)
3399 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3404 TAILQ_FOREACH_REVERSE(re, &refs, got_reflist_head, entry) {
3405 const char *refname;
3406 struct got_object_id *id;
3407 struct got_commit_object *commit = NULL;
3409 refname = got_ref_get_name(re->ref);
3410 if (strncmp(refname, "refs/tags/", 10) != 0)
3414 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3418 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3420 if (error->code != GOT_ERR_OBJ_TYPE) {
3424 /* "lightweight" tag */
3425 error = got_object_open_as_commit(&commit,
3426 gw_trans->repo, id);
3431 error = got_object_id_str(&id_str, id);
3435 error = got_object_id_str(&id_str,
3436 got_object_tag_get_object_id(tag));
3441 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3442 strlen(id_str)) != 0)
3445 if (commit_found == 0 && tag_type == TAGBRIEF &&
3446 gw_trans->commit_id != NULL &&
3447 strncmp(id_str, gw_trans->commit_id, strlen(id_str)) != 0)
3452 if (gw_trans->commit_id != NULL &&
3453 strcmp(id_str, gw_trans->commit_id) != 0 &&
3454 (re == TAILQ_FIRST(&refs) ||
3455 c_cnt == gw_trans->gw_conf->got_max_commits_display)) {
3456 gw_trans->prev_id = strdup(id_str);
3457 if (gw_trans->prev_id == NULL) {
3458 error = got_error_from_errno("strdup");
3467 got_object_tag_close(tag);
3474 got_ref_list_free(&refs);
3475 if (error == NULL && kerr != KCGI_OK)
3476 error = gw_kcgi_error(kerr);
3481 gw_free_header(struct gw_header *header)
3484 free(header->author);
3485 free(header->committer);
3486 free(header->refs_str);
3487 free(header->commit_id);
3488 free(header->parent_id);
3489 free(header->tree_id);
3490 free(header->commit_msg);
3493 static struct gw_header *
3496 struct gw_header *header;
3498 header = malloc(sizeof(*header));
3502 header->path = NULL;
3503 TAILQ_INIT(&header->refs);
3505 header->refs_str = NULL;
3506 header->commit_id = NULL;
3507 header->committer = NULL;
3508 header->author = NULL;
3509 header->parent_id = NULL;
3510 header->tree_id = NULL;
3511 header->commit_msg = NULL;
3516 static const struct got_error *
3517 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3518 int limit, struct got_object_id *id)
3520 const struct got_error *error = NULL;
3521 struct got_commit_graph *graph = NULL;
3522 struct got_commit_object *commit = NULL;
3523 int chk_next = 0, chk_multi = 0, c_cnt = 0, commit_found = 0;
3524 struct gw_header *t_header = NULL;
3526 error = got_commit_graph_open(&graph, header->path, 0);
3530 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3536 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3539 if (error->code == GOT_ERR_ITER_COMPLETED)
3546 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3549 if (limit == 1 && chk_multi == 0 &&
3550 gw_trans->gw_conf->got_max_commits_display != 1) {
3551 error = gw_get_commit(gw_trans, header, commit, id);
3557 struct gw_header *n_header = NULL;
3558 if ((n_header = gw_init_header()) == NULL) {
3559 error = got_error_from_errno("malloc");
3562 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3564 error = got_ref_list(&n_header->refs, gw_trans->repo,
3565 NULL, got_ref_cmp_by_name, NULL);
3569 error = gw_get_commit(gw_trans, n_header, commit, id);
3572 got_ref_list_free(&n_header->refs);
3574 if (gw_trans->commit_id != NULL) {
3575 if (strcmp(gw_trans->commit_id,
3576 n_header->commit_id) == 0)
3582 * check for one more commit before breaking,
3583 * so we know whether to navigate through gw_briefs
3584 * gw_commits and gw_summary
3586 if (chk_next && (gw_trans->action == GW_BRIEFS ||
3587 gw_trans->action == GW_COMMITS ||
3588 gw_trans->action == GW_SUMMARY)) {
3589 gw_trans->next_id = strdup(n_header->commit_id);
3590 if (gw_trans->next_id == NULL)
3591 error = got_error_from_errno("strdup");
3592 TAILQ_REMOVE(&gw_trans->gw_headers, n_header,
3598 if (commit_found == 1 && (error || (limit && --limit == 0))) {
3605 if (gw_trans->prev_id == NULL && gw_trans->commit_id != NULL &&
3606 (gw_trans->action == GW_BRIEFS || gw_trans->action == GW_COMMITS)) {
3608 TAILQ_FOREACH_REVERSE(t_header, &gw_trans->gw_headers,
3610 if (commit_found == 0 &&
3611 strcmp(gw_trans->commit_id,
3612 t_header->commit_id) != 0)
3616 if (gw_trans->commit_id != NULL &&
3617 strcmp(gw_trans->commit_id,
3618 t_header->commit_id) != 0 &&
3619 (c_cnt == gw_trans->gw_conf->got_max_commits_display
3621 TAILQ_FIRST(&gw_trans->gw_headers))) {
3622 gw_trans->prev_id = strdup(t_header->commit_id);
3623 if (gw_trans->prev_id == NULL)
3624 error = got_error_from_errno("strdup");
3632 got_object_commit_close(commit);
3634 got_commit_graph_close(graph);
3638 static const struct got_error *
3639 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3640 struct got_commit_object *commit, struct got_object_id *id)
3642 const struct got_error *error = NULL;
3643 struct got_reflist_entry *re;
3644 struct got_object_id *id2 = NULL;
3645 struct got_object_qid *parent_id;
3646 char *commit_msg = NULL, *commit_msg0;
3649 TAILQ_FOREACH(re, &header->refs, entry) {
3652 struct got_tag_object *tag = NULL;
3653 struct got_object_id *ref_id;
3656 if (got_ref_is_symbolic(re->ref))
3659 name = got_ref_get_name(re->ref);
3660 if (strncmp(name, "refs/", 5) == 0)
3662 if (strncmp(name, "got/", 4) == 0)
3664 if (strncmp(name, "heads/", 6) == 0)
3666 if (strncmp(name, "remotes/", 8) == 0) {
3668 s = strstr(name, "/" GOT_REF_HEAD);
3669 if (s != NULL && s[strlen(s)] == '\0')
3672 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3675 if (strncmp(name, "tags/", 5) == 0) {
3676 error = got_object_open_as_tag(&tag, gw_trans->repo,
3679 if (error->code != GOT_ERR_OBJ_TYPE) {
3684 * Ref points at something other
3691 cmp = got_object_id_cmp(tag ?
3692 got_object_tag_get_object_id(tag) : ref_id, id);
3695 got_object_tag_close(tag);
3698 s = header->refs_str;
3699 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3700 s ? ", " : "", name) == -1) {
3701 error = got_error_from_errno("asprintf");
3703 header->refs_str = NULL;
3709 error = got_object_id_str(&header->commit_id, id);
3713 error = got_object_id_str(&header->tree_id,
3714 got_object_commit_get_tree_id(commit));
3718 if (gw_trans->action == GW_DIFF) {
3719 parent_id = STAILQ_FIRST(
3720 got_object_commit_get_parent_ids(commit));
3721 if (parent_id != NULL) {
3722 id2 = got_object_id_dup(&parent_id->id);
3724 error = got_object_id_str(&header->parent_id, id2);
3729 header->parent_id = strdup("/dev/null");
3730 if (header->parent_id == NULL) {
3731 error = got_error_from_errno("strdup");
3737 header->committer_time =
3738 got_object_commit_get_committer_time(commit);
3741 strdup(got_object_commit_get_author(commit));
3742 if (header->author == NULL) {
3743 error = got_error_from_errno("strdup");
3747 strdup(got_object_commit_get_committer(commit));
3748 if (header->committer == NULL) {
3749 error = got_error_from_errno("strdup");
3752 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3756 commit_msg = commit_msg0;
3757 while (*commit_msg == '\n')
3760 header->commit_msg = strdup(commit_msg);
3761 if (header->commit_msg == NULL)
3762 error = got_error_from_errno("strdup");
3767 static const struct got_error *
3768 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3770 const struct got_error *error = NULL;
3771 char *in_repo_path = NULL;
3772 struct got_object_id *id = NULL;
3773 struct got_reference *ref;
3775 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3779 if (gw_trans->commit_id == NULL || gw_trans->action == GW_COMMITS ||
3780 gw_trans->action == GW_BRIEFS || gw_trans->action == GW_SUMMARY ||
3781 gw_trans->action == GW_TAGS) {
3782 error = got_ref_open(&ref, gw_trans->repo,
3783 gw_trans->headref, 0);
3787 error = got_ref_resolve(&id, gw_trans->repo, ref);
3792 error = got_ref_open(&ref, gw_trans->repo,
3793 gw_trans->commit_id, 0);
3794 if (error == NULL) {
3796 error = got_ref_resolve(&id, gw_trans->repo, ref);
3800 error = got_object_get_type(&obj_type, gw_trans->repo,
3804 if (obj_type == GOT_OBJ_TYPE_TAG) {
3805 struct got_tag_object *tag;
3806 error = got_object_open_as_tag(&tag,
3807 gw_trans->repo, id);
3810 if (got_object_tag_get_object_type(tag) !=
3811 GOT_OBJ_TYPE_COMMIT) {
3812 got_object_tag_close(tag);
3813 error = got_error(GOT_ERR_OBJ_TYPE);
3817 id = got_object_id_dup(
3818 got_object_tag_get_object_id(tag));
3820 error = got_error_from_errno(
3821 "got_object_id_dup");
3822 got_object_tag_close(tag);
3825 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3826 error = got_error(GOT_ERR_OBJ_TYPE);
3830 error = got_repo_match_object_id_prefix(&id,
3831 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3837 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3838 gw_trans->repo_path);
3843 header->path = strdup(in_repo_path);
3844 if (header->path == NULL) {
3845 error = got_error_from_errno("strdup");
3850 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3851 got_ref_cmp_by_name, NULL);
3855 error = gw_get_commits(gw_trans, header, limit, id);
3857 got_ref_list_free(&header->refs);
3867 char datebuf[11]; /* YYYY-MM-DD + NUL */
3870 struct gw_blame_cb_args {
3871 struct blame_line *lines;
3875 off_t *line_offsets;
3877 struct got_repository *repo;
3878 struct gw_trans *gw_trans;
3881 static const struct got_error *
3882 gw_blame_cb(void *arg, int nlines, int lineno,
3883 struct got_commit_object *commit, struct got_object_id *id)
3885 const struct got_error *err = NULL;
3886 struct gw_blame_cb_args *a = arg;
3887 struct blame_line *bline;
3889 size_t linesize = 0;
3892 time_t committer_time;
3893 enum kcgi_err kerr = KCGI_OK;
3895 if (nlines != a->nlines ||
3896 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3897 return got_error(GOT_ERR_RANGE);
3900 return NULL; /* no change in this commit */
3902 /* Annotate this line. */
3903 bline = &a->lines[lineno - 1];
3904 if (bline->annotated)
3906 err = got_object_id_str(&bline->id_str, id);
3910 bline->committer = strdup(got_object_commit_get_committer(commit));
3911 if (bline->committer == NULL) {
3912 err = got_error_from_errno("strdup");
3916 committer_time = got_object_commit_get_committer_time(commit);
3917 if (gmtime_r(&committer_time, &tm) == NULL)
3918 return got_error_from_errno("gmtime_r");
3919 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3921 err = got_error(GOT_ERR_NO_SPACE);
3924 bline->annotated = 1;
3926 /* Print lines annotated so far. */
3927 bline = &a->lines[a->lineno_cur - 1];
3928 if (!bline->annotated)
3931 offset = a->line_offsets[a->lineno_cur - 1];
3932 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3933 err = got_error_from_errno("fseeko");
3937 while (bline->annotated) {
3938 char *smallerthan, *at, *nl, *committer;
3939 char *href_diff = NULL;
3942 if (getline(&line, &linesize, a->f) == -1) {
3944 err = got_error_from_errno("getline");
3948 committer = bline->committer;
3949 smallerthan = strchr(committer, '<');
3950 if (smallerthan && smallerthan[1] != '\0')
3951 committer = smallerthan + 1;
3952 at = strchr(committer, '@');
3955 len = strlen(committer);
3957 committer[8] = '\0';
3959 nl = strchr(line, '\n');
3963 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3964 "blame_wrapper", KATTR__MAX);
3965 if (kerr != KCGI_OK)
3967 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3968 "blame_number", KATTR__MAX);
3969 if (kerr != KCGI_OK)
3971 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3972 a->nlines_prec, a->lineno_cur);
3973 if (kerr != KCGI_OK)
3975 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3976 if (kerr != KCGI_OK)
3979 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3980 "blame_hash", KATTR__MAX);
3981 if (kerr != KCGI_OK)
3984 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3985 a->gw_trans->repo_name, "action", "diff", "commit",
3986 bline->id_str, NULL);
3987 if (href_diff == NULL) {
3988 err = got_error_from_errno("khttp_urlpart");
3991 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3992 KATTR_HREF, href_diff, KATTR__MAX);
3993 if (kerr != KCGI_OK)
3995 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3997 if (kerr != KCGI_OK)
3999 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
4000 if (kerr != KCGI_OK)
4003 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4004 "blame_date", KATTR__MAX);
4005 if (kerr != KCGI_OK)
4007 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
4008 if (kerr != KCGI_OK)
4010 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4011 if (kerr != KCGI_OK)
4014 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4015 "blame_author", KATTR__MAX);
4016 if (kerr != KCGI_OK)
4018 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
4019 if (kerr != KCGI_OK)
4021 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4022 if (kerr != KCGI_OK)
4025 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4026 "blame_code", KATTR__MAX);
4027 if (kerr != KCGI_OK)
4029 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
4030 if (kerr != KCGI_OK)
4032 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4033 if (kerr != KCGI_OK)
4036 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4037 if (kerr != KCGI_OK)
4041 bline = &a->lines[a->lineno_cur - 1];
4047 if (err == NULL && kerr != KCGI_OK)
4048 err = gw_kcgi_error(kerr);
4052 static const struct got_error *
4053 gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
4055 const struct got_error *error = NULL;
4056 struct got_object_id *obj_id = NULL;
4057 struct got_object_id *commit_id = NULL;
4058 struct got_commit_object *commit = NULL;
4059 struct got_blob_object *blob = NULL;
4060 char *path = NULL, *in_repo_path = NULL;
4061 struct gw_blame_cb_args bca;
4065 memset(&bca, 0, sizeof(bca));
4067 if (asprintf(&path, "%s%s%s",
4068 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4069 gw_trans->repo_folder ? "/" : "",
4070 gw_trans->repo_file) == -1) {
4071 error = got_error_from_errno("asprintf");
4075 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4079 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4080 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4084 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4088 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit,
4093 if (obj_id == NULL) {
4094 error = got_error(GOT_ERR_NO_OBJ);
4098 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4102 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4103 error = got_error(GOT_ERR_OBJ_TYPE);
4107 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4111 bca.f = got_opentemp();
4112 if (bca.f == NULL) {
4113 error = got_error_from_errno("got_opentemp");
4116 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4117 &bca.line_offsets, bca.f, blob);
4118 if (error || bca.nlines == 0)
4121 /* Don't include \n at EOF in the blame line count. */
4122 if (bca.line_offsets[bca.nlines - 1] == filesize)
4125 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4126 if (bca.lines == NULL) {
4127 error = got_error_from_errno("calloc");
4131 bca.nlines_prec = 0;
4137 bca.repo = gw_trans->repo;
4138 bca.gw_trans = gw_trans;
4140 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4149 free(bca.line_offsets);
4150 for (i = 0; i < bca.nlines; i++) {
4151 struct blame_line *bline = &bca.lines[i];
4152 free(bline->id_str);
4153 free(bline->committer);
4156 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4157 error = got_error_from_errno("fclose");
4160 got_object_blob_close(blob);
4162 got_object_commit_close(commit);
4166 static const struct got_error *
4167 gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
4169 const struct got_error *error = NULL;
4170 struct got_object_id *obj_id = NULL;
4171 struct got_object_id *commit_id = NULL;
4172 struct got_commit_object *commit = NULL;
4173 struct got_blob_object *blob = NULL;
4174 char *path = NULL, *in_repo_path = NULL;
4175 int obj_type, set_mime = 0;
4178 enum kcgi_err kerr = KCGI_OK;
4180 if (asprintf(&path, "%s%s%s",
4181 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4182 gw_trans->repo_folder ? "/" : "",
4183 gw_trans->repo_file) == -1) {
4184 error = got_error_from_errno("asprintf");
4188 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4192 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4193 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4197 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4201 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit,
4206 if (obj_id == NULL) {
4207 error = got_error(GOT_ERR_NO_OBJ);
4211 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4215 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4216 error = got_error(GOT_ERR_OBJ_TYPE);
4220 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4224 hdrlen = got_object_blob_get_hdrlen(blob);
4226 error = got_object_blob_read_block(&len, blob);
4229 buf = got_object_blob_get_read_buf(blob);
4232 * Skip blob object header first time around,
4233 * which also contains a zero byte.
4236 if (set_mime == 0) {
4237 if (isbinary(buf, len - hdrlen))
4238 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4240 gw_trans->mime = KMIME_TEXT_PLAIN;
4242 error = gw_display_index(gw_trans);
4246 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4247 if (kerr != KCGI_OK)
4257 got_object_blob_close(blob);
4259 got_object_commit_close(commit);
4260 if (error == NULL && kerr != KCGI_OK)
4261 error = gw_kcgi_error(kerr);
4265 static const struct got_error *
4266 gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
4268 const struct got_error *error = NULL;
4269 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4270 struct got_tree_object *tree = NULL;
4271 struct got_commit_object *commit = NULL;
4272 char *path = NULL, *in_repo_path = NULL;
4273 char *id_str = NULL;
4274 char *build_folder = NULL;
4275 char *href_blob = NULL, *href_blame = NULL;
4276 const char *class = NULL;
4277 int nentries, i, class_flip = 0;
4278 enum kcgi_err kerr = KCGI_OK;
4280 if (gw_trans->repo_folder != NULL) {
4281 path = strdup(gw_trans->repo_folder);
4283 error = got_error_from_errno("strdup");
4287 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4288 gw_trans->repo_path);
4292 path = in_repo_path;
4295 if (gw_trans->commit_id == NULL) {
4296 struct got_reference *head_ref;
4297 error = got_ref_open(&head_ref, gw_trans->repo,
4298 gw_trans->headref, 0);
4301 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4304 got_ref_close(head_ref);
4306 * gw_trans->commit_id was not parsed from the querystring
4307 * we hit this code path from gw_index, where we don't know the
4308 * commit values for the tree link yet, so set
4309 * gw_trans->commit_id here to continue further into the tree
4311 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4316 error = got_repo_match_object_id(&commit_id, NULL,
4317 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
4323 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4327 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit,
4332 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4336 nentries = got_object_tree_get_nentries(tree);
4337 for (i = 0; i < nentries; i++) {
4338 struct got_tree_entry *te;
4339 const char *modestr = "";
4342 te = got_object_tree_get_entry(tree, i);
4344 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4348 mode = got_tree_entry_get_mode(te);
4349 if (got_object_tree_entry_is_submodule(te))
4351 else if (S_ISLNK(mode))
4353 else if (S_ISDIR(mode))
4355 else if (mode & S_IXUSR)
4358 if (class_flip == 0) {
4359 class = "back_lightgray";
4362 class = "back_white";
4366 if (S_ISDIR(mode)) {
4367 if (asprintf(&build_folder, "%s/%s",
4368 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4369 got_tree_entry_get_name(te)) == -1) {
4370 error = got_error_from_errno("asprintf");
4374 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4375 gw_trans->repo_name, "action",
4376 gw_get_action_name(gw_trans), "commit",
4377 gw_trans->commit_id, "folder", build_folder, NULL);
4378 if (href_blob == NULL) {
4379 error = got_error_from_errno("khttp_urlpart");
4382 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4383 KATTR_ID, "tree_wrapper", KATTR__MAX);
4384 if (kerr != KCGI_OK)
4386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4387 KATTR_ID, "tree_line", KATTR_CLASS, class,
4389 if (kerr != KCGI_OK)
4391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4392 KATTR_HREF, href_blob, KATTR_CLASS,
4393 "diff_directory", KATTR__MAX);
4394 if (kerr != KCGI_OK)
4396 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4397 got_tree_entry_get_name(te), modestr);
4398 if (kerr != KCGI_OK)
4400 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4401 if (kerr != KCGI_OK)
4403 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4404 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4406 if (kerr != KCGI_OK)
4408 kerr = khtml_entity(gw_trans->gw_html_req,
4410 if (kerr != KCGI_OK)
4412 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4413 if (kerr != KCGI_OK)
4416 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4417 gw_trans->repo_name, "action", "blob", "commit",
4418 gw_trans->commit_id, "file",
4419 got_tree_entry_get_name(te), "folder",
4420 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4422 if (href_blob == NULL) {
4423 error = got_error_from_errno("khttp_urlpart");
4426 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4427 gw_trans->repo_name, "action", "blame", "commit",
4428 gw_trans->commit_id, "file",
4429 got_tree_entry_get_name(te), "folder",
4430 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4432 if (href_blame == NULL) {
4433 error = got_error_from_errno("khttp_urlpart");
4436 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4437 KATTR_ID, "tree_wrapper", KATTR__MAX);
4438 if (kerr != KCGI_OK)
4440 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4441 KATTR_ID, "tree_line", KATTR_CLASS, class,
4443 if (kerr != KCGI_OK)
4445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4446 KATTR_HREF, href_blob, KATTR__MAX);
4447 if (kerr != KCGI_OK)
4449 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4450 got_tree_entry_get_name(te), modestr);
4451 if (kerr != KCGI_OK)
4453 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4454 if (kerr != KCGI_OK)
4456 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4457 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4459 if (kerr != KCGI_OK)
4462 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4463 KATTR_HREF, href_blob, KATTR__MAX);
4464 if (kerr != KCGI_OK)
4466 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4467 if (kerr != KCGI_OK)
4469 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4470 if (kerr != KCGI_OK)
4473 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4474 if (kerr != KCGI_OK)
4477 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4478 KATTR_HREF, href_blame, KATTR__MAX);
4479 if (kerr != KCGI_OK)
4481 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4482 if (kerr != KCGI_OK)
4485 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4486 if (kerr != KCGI_OK)
4494 build_folder = NULL;
4498 got_object_tree_close(tree);
4500 got_object_commit_close(commit);
4507 if (error == NULL && kerr != KCGI_OK)
4508 error = gw_kcgi_error(kerr);
4512 static const struct got_error *
4513 gw_output_repo_heads(struct gw_trans *gw_trans)
4515 const struct got_error *error = NULL;
4516 struct got_reflist_head refs;
4517 struct got_reflist_entry *re;
4518 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4519 char *href_commits = NULL;
4520 enum kcgi_err kerr = KCGI_OK;
4524 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4525 got_ref_cmp_by_name, NULL);
4529 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4530 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4531 if (kerr != KCGI_OK)
4533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4534 KATTR_ID, "summary_heads_title", KATTR__MAX);
4535 if (kerr != KCGI_OK)
4537 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4538 if (kerr != KCGI_OK)
4540 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4541 if (kerr != KCGI_OK)
4543 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4544 KATTR_ID, "summary_heads_content", KATTR__MAX);
4545 if (kerr != KCGI_OK)
4548 TAILQ_FOREACH(re, &refs, entry) {
4549 const char *refname;
4551 if (got_ref_is_symbolic(re->ref))
4554 refname = got_ref_get_name(re->ref);
4555 if (strncmp(refname, "refs/heads/", 11) != 0)
4558 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4563 if (strncmp(refname, "refs/heads/", 11) == 0)
4566 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4567 KATTR_ID, "heads_wrapper", KATTR__MAX);
4568 if (kerr != KCGI_OK)
4570 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4571 KATTR_ID, "heads_age", KATTR__MAX);
4572 if (kerr != KCGI_OK)
4574 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4575 if (kerr != KCGI_OK)
4577 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4578 if (kerr != KCGI_OK)
4580 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4581 KATTR_ID, "heads_space", KATTR__MAX);
4582 if (kerr != KCGI_OK)
4584 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4585 if (kerr != KCGI_OK)
4587 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4588 if (kerr != KCGI_OK)
4590 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4591 KATTR_ID, "head", KATTR__MAX);
4592 if (kerr != KCGI_OK)
4595 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4596 gw_trans->repo_name, "action", "summary", "headref",
4598 if (href_summary == NULL) {
4599 error = got_error_from_errno("khttp_urlpart");
4602 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4603 href_summary, KATTR__MAX);
4604 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4605 if (kerr != KCGI_OK)
4607 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4608 if (kerr != KCGI_OK)
4611 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4612 "navs_wrapper", KATTR__MAX);
4613 if (kerr != KCGI_OK)
4615 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4616 "navs", KATTR__MAX);
4617 if (kerr != KCGI_OK)
4620 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4621 href_summary, KATTR__MAX);
4622 if (kerr != KCGI_OK)
4624 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4625 if (kerr != KCGI_OK)
4627 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4628 if (kerr != KCGI_OK)
4631 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4632 if (kerr != KCGI_OK)
4635 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4636 gw_trans->repo_name, "action", "briefs", "headref",
4638 if (href_briefs == NULL) {
4639 error = got_error_from_errno("khttp_urlpart");
4642 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4643 href_briefs, KATTR__MAX);
4644 if (kerr != KCGI_OK)
4646 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4647 if (kerr != KCGI_OK)
4649 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4650 if (kerr != KCGI_OK)
4653 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4654 if (kerr != KCGI_OK)
4657 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4658 gw_trans->repo_name, "action", "commits", "headref",
4660 if (href_commits == NULL) {
4661 error = got_error_from_errno("khttp_urlpart");
4664 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4665 href_commits, KATTR__MAX);
4666 if (kerr != KCGI_OK)
4668 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4669 if (kerr != KCGI_OK)
4671 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4672 if (kerr != KCGI_OK)
4675 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4676 "dotted_line", KATTR__MAX);
4677 if (kerr != KCGI_OK)
4679 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4680 if (kerr != KCGI_OK)
4683 href_summary = NULL;
4687 href_commits = NULL;
4690 got_ref_list_free(&refs);
4697 static const struct got_error *
4698 gw_output_site_link(struct gw_trans *gw_trans)
4700 const struct got_error *error = NULL;
4701 char *href_summary = NULL;
4702 enum kcgi_err kerr = KCGI_OK;
4704 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4705 "site_link", KATTR__MAX);
4706 if (kerr != KCGI_OK)
4708 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4710 if (kerr != KCGI_OK)
4712 kerr = khtml_puts(gw_trans->gw_html_req,
4713 gw_trans->gw_conf->got_site_link);
4714 if (kerr != KCGI_OK)
4716 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4717 if (kerr != KCGI_OK)
4720 if (gw_trans->repo_name != NULL) {
4721 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4722 if (kerr != KCGI_OK)
4725 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4726 gw_trans->repo_name, "action", "summary", NULL);
4727 if (href_summary == NULL) {
4728 error = got_error_from_errno("khttp_urlpart");
4731 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4732 href_summary, KATTR__MAX);
4733 if (kerr != KCGI_OK)
4735 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4736 if (kerr != KCGI_OK)
4738 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4739 if (kerr != KCGI_OK)
4741 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4742 gw_get_action_name(gw_trans));
4743 if (kerr != KCGI_OK)
4747 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4748 if (kerr != KCGI_OK)
4752 if (error == NULL && kerr != KCGI_OK)
4753 error = gw_kcgi_error(kerr);
4757 static const struct got_error *
4758 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4760 const struct got_error *error = NULL;
4762 enum kcgi_err kerr = KCGI_OK;
4764 if (strncmp(buf, "-", 1) == 0)
4765 color = "diff_minus";
4766 else if (strncmp(buf, "+", 1) == 0)
4767 color = "diff_plus";
4768 else if (strncmp(buf, "@@", 2) == 0)
4769 color = "diff_chunk_header";
4770 else if (strncmp(buf, "@@", 2) == 0)
4771 color = "diff_chunk_header";
4772 else if (strncmp(buf, "commit +", 8) == 0)
4773 color = "diff_meta";
4774 else if (strncmp(buf, "commit -", 8) == 0)
4775 color = "diff_meta";
4776 else if (strncmp(buf, "blob +", 6) == 0)
4777 color = "diff_meta";
4778 else if (strncmp(buf, "blob -", 6) == 0)
4779 color = "diff_meta";
4780 else if (strncmp(buf, "file +", 6) == 0)
4781 color = "diff_meta";
4782 else if (strncmp(buf, "file -", 6) == 0)
4783 color = "diff_meta";
4784 else if (strncmp(buf, "from:", 5) == 0)
4785 color = "diff_author";
4786 else if (strncmp(buf, "via:", 4) == 0)
4787 color = "diff_author";
4788 else if (strncmp(buf, "date:", 5) == 0)
4789 color = "diff_date";
4790 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4791 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4792 if (error == NULL && kerr != KCGI_OK)
4793 error = gw_kcgi_error(kerr);
4798 main(int argc, char *argv[])
4800 const struct got_error *error = NULL, *error2 = NULL;
4801 struct gw_trans *gw_trans;
4802 struct gw_dir *dir = NULL, *tdir;
4803 const char *page = "index";
4804 enum kcgi_err kerr = KCGI_OK;
4806 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4809 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4812 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4815 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4818 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4819 if (kerr != KCGI_OK) {
4820 error = gw_kcgi_error(kerr);
4824 TAILQ_INIT(&gw_trans->gw_dirs);
4825 TAILQ_INIT(&gw_trans->gw_headers);
4827 gw_trans->action = -1;
4829 gw_trans->repos_total = 0;
4830 gw_trans->repo_path = NULL;
4831 gw_trans->commit_id = NULL;
4832 gw_trans->next_id = NULL;
4833 gw_trans->prev_id = NULL;
4834 gw_trans->headref = GOT_REF_HEAD;
4835 gw_trans->mime = KMIME_TEXT_HTML;
4836 gw_trans->gw_tmpl->key = gw_templs;
4837 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4838 gw_trans->gw_tmpl->arg = gw_trans;
4839 gw_trans->gw_tmpl->cb = gw_template;
4841 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4845 error = gw_parse_querystring(gw_trans);
4849 if (gw_trans->action == GW_BLOB)
4850 error = gw_blob(gw_trans);
4852 error = gw_display_index(gw_trans);
4854 if (gw_trans->repo) {
4855 const struct got_error *close_err;
4856 close_err = got_repo_close(gw_trans->repo);
4861 gw_trans->error = error;
4862 gw_trans->action = GW_ERR;
4863 error2 = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
4865 goto cleanup; /* we can't display an error page */
4866 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
4867 if (kerr != KCGI_OK)
4868 goto cleanup; /* we can't display an error page */
4869 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
4870 gw_query_funcs[gw_trans->action].template);
4871 if (kerr != KCGI_OK) {
4872 khtml_close(gw_trans->gw_html_req);
4873 goto cleanup; /* we can't display an error page */
4878 free(gw_trans->gw_conf->got_repos_path);
4879 free(gw_trans->gw_conf->got_www_path);
4880 free(gw_trans->gw_conf->got_site_name);
4881 free(gw_trans->gw_conf->got_site_owner);
4882 free(gw_trans->gw_conf->got_site_link);
4883 free(gw_trans->gw_conf->got_logo);
4884 free(gw_trans->gw_conf->got_logo_url);
4885 free(gw_trans->gw_conf);
4886 free(gw_trans->commit_id);
4887 free(gw_trans->next_id);
4888 free(gw_trans->prev_id);
4889 free(gw_trans->repo_path);
4890 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4892 free(dir->description);
4899 khttp_free(gw_trans->gw_req);