2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
20 #include <sys/types.h>
34 #include <got_error.h>
35 #include <got_object.h>
36 #include <got_reference.h>
37 #include <got_repository.h>
39 #include <got_cancel.h>
40 #include <got_worktree.h>
42 #include <got_commit_graph.h>
43 #include <got_blame.h>
44 #include <got_privsep.h>
45 #include <got_opentemp.h>
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 TAILQ_HEAD(headers, gw_header) gw_headers;
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct got_repository *repo;
60 struct gw_dir *gw_dir;
61 struct gotweb_config *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
65 const struct got_error *error;
66 const char *repo_name;
73 const char *repo_file;
78 unsigned int repos_total;
83 TAILQ_ENTRY(gw_header) entry;
84 struct got_reflist_head refs;
88 char *commit_id; /* id_str1 */
89 char *parent_id; /* id_str2 */
94 time_t committer_time;
98 TAILQ_ENTRY(gw_dir) entry;
141 static const char *const gw_templs[TEMPL__MAX] = {
151 static const struct kvalid gw_keys[KEY__ZMAX] = {
152 { kvalid_stringne, "action" },
153 { kvalid_stringne, "commit" },
154 { kvalid_stringne, "file" },
155 { kvalid_stringne, "folder" },
156 { kvalid_stringne, "headref" },
157 { kvalid_int, "page" },
158 { kvalid_stringne, "path" },
159 { kvalid_stringne, "prev" },
160 { kvalid_stringne, "prev_prev" },
163 static struct gw_header *gw_init_header(void);
165 static void gw_free_header(struct gw_header *);
167 static int gw_template(size_t, void *);
169 static const struct got_error *gw_error(struct gw_trans *);
170 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
171 static const struct got_error *gw_get_repo_description(char **,
172 struct gw_trans *, char *);
173 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
175 static const struct got_error *gw_get_time_str(char **, time_t, int);
176 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
177 char *, const char *, int);
178 static const struct got_error *gw_output_file_blame(struct gw_trans *,
180 static const struct got_error *gw_output_blob_buf(struct gw_trans *,
182 static const struct got_error *gw_output_repo_tree(struct gw_trans *,
184 static const struct got_error *gw_output_diff(struct gw_trans *,
186 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
187 struct gw_header *, int, int);
188 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
189 static const struct got_error *gw_output_site_link(struct gw_trans *);
190 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
192 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
194 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
196 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
198 static const struct got_error *gw_gen_author_header(struct gw_trans *,
200 static const struct got_error *gw_gen_age_header(struct gw_trans *,
202 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
204 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
206 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
207 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
209 static const struct got_error *gw_display_index(struct gw_trans *);
210 static const struct got_error *gw_get_header(struct gw_trans *,
211 struct gw_header *, int);
212 static const struct got_error *gw_get_commits(struct gw_trans *,
213 struct gw_header *, int,
214 struct got_object_id *);
215 static const struct got_error *gw_get_commit(struct gw_trans *,
217 struct got_commit_object *,
218 struct got_object_id *);
219 static const struct got_error *gw_apply_unveil(const char *);
220 static const struct got_error *gw_blame_cb(void *, int, int,
221 struct got_object_id *);
222 static const struct got_error *gw_load_got_paths(struct gw_trans *);
223 static const struct got_error *gw_load_got_path(struct gw_trans *,
225 static const struct got_error *gw_parse_querystring(struct gw_trans *);
226 static const struct got_error *gw_blame(struct gw_trans *);
227 static const struct got_error *gw_blob(struct gw_trans *);
228 static const struct got_error *gw_diff(struct gw_trans *);
229 static const struct got_error *gw_index(struct gw_trans *);
230 static const struct got_error *gw_commits(struct gw_trans *);
231 static const struct got_error *gw_briefs(struct gw_trans *);
232 static const struct got_error *gw_summary(struct gw_trans *);
233 static const struct got_error *gw_tree(struct gw_trans *);
234 static const struct got_error *gw_tag(struct gw_trans *);
235 static const struct got_error *gw_tags(struct gw_trans *);
237 struct gw_query_action {
238 unsigned int func_id;
239 const char *func_name;
240 const struct got_error *(*func_main)(struct gw_trans *);
244 enum gw_query_actions {
258 static struct gw_query_action gw_query_funcs[] = {
259 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
260 { GW_BLOB, "blob", NULL, NULL },
261 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
262 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
263 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
264 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
265 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
266 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
267 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
268 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
269 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
273 gw_get_action_name(struct gw_trans *gw_trans)
275 return gw_query_funcs[gw_trans->action].func_name;
278 static const struct got_error *
279 gw_kcgi_error(enum kcgi_err kerr)
284 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
285 return got_error(GOT_ERR_CANCELLED);
287 if (kerr == KCGI_ENOMEM)
288 return got_error_set_errno(ENOMEM,
289 kcgi_strerror(kerr));
291 if (kerr == KCGI_ENFILE)
292 return got_error_set_errno(ENFILE,
293 kcgi_strerror(kerr));
295 if (kerr == KCGI_EAGAIN)
296 return got_error_set_errno(EAGAIN,
297 kcgi_strerror(kerr));
299 if (kerr == KCGI_FORM)
300 return got_error_msg(GOT_ERR_IO,
301 kcgi_strerror(kerr));
303 return got_error_from_errno(kcgi_strerror(kerr));
306 static const struct got_error *
307 gw_apply_unveil(const char *repo_path)
309 const struct got_error *err;
312 if (unveil("gmon.out", "rwc") != 0)
313 return got_error_from_errno2("unveil", "gmon.out");
315 if (repo_path && unveil(repo_path, "r") != 0)
316 return got_error_from_errno2("unveil", repo_path);
318 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
319 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
321 err = got_privsep_unveil_exec_helpers();
325 if (unveil(NULL, NULL) != 0)
326 return got_error_from_errno("unveil");
332 isbinary(const uint8_t *buf, size_t n)
336 for (i = 0; i < n; i++)
342 static const struct got_error *
343 gw_blame(struct gw_trans *gw_trans)
345 const struct got_error *error = NULL;
346 struct gw_header *header = NULL;
348 enum kcgi_err kerr = KCGI_OK;
351 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
353 return got_error_from_errno("pledge");
355 if ((header = gw_init_header()) == NULL)
356 return got_error_from_errno("malloc");
358 error = gw_apply_unveil(gw_trans->gw_dir->path);
362 /* check querystring */
363 if (gw_trans->repo_file == NULL) {
364 error = got_error_msg(GOT_ERR_QUERYSTRING,
365 "file required in querystring");
368 if (gw_trans->commit_id == NULL) {
369 error = got_error_msg(GOT_ERR_QUERYSTRING,
370 "commit required in querystring");
374 error = gw_get_header(gw_trans, header, 1);
377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
378 "blame_header_wrapper", KATTR__MAX);
381 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
382 "blame_header", KATTR__MAX);
385 error = gw_get_time_str(&age, header->committer_time,
389 error = gw_gen_age_header(gw_trans, age ?age : "");
392 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
395 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
398 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
399 "dotted_line", KATTR__MAX);
402 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
406 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
407 "blame", KATTR__MAX);
410 error = gw_output_file_blame(gw_trans, header);
413 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
415 gw_free_header(header);
416 if (error == NULL && kerr != KCGI_OK)
417 error = gw_kcgi_error(kerr);
421 static const struct got_error *
422 gw_blob(struct gw_trans *gw_trans)
424 const struct got_error *error = NULL, *err = NULL;
425 struct gw_header *header = NULL;
426 enum kcgi_err kerr = KCGI_OK;
429 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
431 return got_error_from_errno("pledge");
433 if ((header = gw_init_header()) == NULL)
434 return got_error_from_errno("malloc");
436 error = gw_apply_unveil(gw_trans->gw_dir->path);
440 /* check querystring */
441 if (gw_trans->repo_file == NULL) {
442 error = got_error_msg(GOT_ERR_QUERYSTRING,
443 "file required in querystring");
446 if (gw_trans->commit_id == NULL) {
447 error = got_error_msg(GOT_ERR_QUERYSTRING,
448 "commit required in querystring");
451 error = gw_get_header(gw_trans, header, 1);
455 error = gw_output_blob_buf(gw_trans, header);
459 gw_trans->mime = KMIME_TEXT_PLAIN;
460 err = gw_display_index(gw_trans);
465 kerr = khttp_puts(gw_trans->gw_req, error->msg);
468 gw_free_header(header);
469 if (error == NULL && kerr != KCGI_OK)
470 error = gw_kcgi_error(kerr);
474 static const struct got_error *
475 gw_diff(struct gw_trans *gw_trans)
477 const struct got_error *error = NULL;
478 struct gw_header *header = NULL;
480 enum kcgi_err kerr = KCGI_OK;
483 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
485 return got_error_from_errno("pledge");
487 if ((header = gw_init_header()) == NULL)
488 return got_error_from_errno("malloc");
490 error = gw_apply_unveil(gw_trans->gw_dir->path);
494 error = gw_get_header(gw_trans, header, 1);
498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
499 "diff_header_wrapper", KATTR__MAX);
502 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
503 "diff_header", KATTR__MAX);
506 error = gw_gen_diff_header(gw_trans, header->parent_id,
510 error = gw_gen_commit_header(gw_trans, header->commit_id,
514 error = gw_gen_tree_header(gw_trans, header->tree_id);
517 error = gw_gen_author_header(gw_trans, header->author);
520 error = gw_gen_committer_header(gw_trans, header->author);
523 error = gw_get_time_str(&age, header->committer_time,
527 error = gw_gen_age_header(gw_trans, age ?age : "");
530 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
533 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
536 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
537 "dotted_line", KATTR__MAX);
540 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
548 error = gw_output_diff(gw_trans, header);
552 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
554 gw_free_header(header);
556 if (error == NULL && kerr != KCGI_OK)
557 error = gw_kcgi_error(kerr);
561 static const struct got_error *
562 gw_index(struct gw_trans *gw_trans)
564 const struct got_error *error = NULL;
565 struct gw_dir *gw_dir = NULL;
566 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
567 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
568 char *href_tags = NULL;
569 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
570 enum kcgi_err kerr = KCGI_OK;
573 if (pledge("stdio rpath proc exec sendfd unveil",
575 error = got_error_from_errno("pledge");
579 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
583 error = gw_load_got_paths(gw_trans);
587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
588 "index_header", KATTR__MAX);
590 return gw_kcgi_error(kerr);
591 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
592 "index_header_project", KATTR__MAX);
594 return gw_kcgi_error(kerr);
595 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
597 return gw_kcgi_error(kerr);
598 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
600 return gw_kcgi_error(kerr);
602 if (gw_trans->gw_conf->got_show_repo_description) {
603 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
604 "index_header_description", KATTR__MAX);
606 return gw_kcgi_error(kerr);
607 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
609 return gw_kcgi_error(kerr);
610 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
612 return gw_kcgi_error(kerr);
615 if (gw_trans->gw_conf->got_show_repo_owner) {
616 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
617 "index_header_owner", KATTR__MAX);
619 return gw_kcgi_error(kerr);
620 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
622 return gw_kcgi_error(kerr);
623 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
625 return gw_kcgi_error(kerr);
628 if (gw_trans->gw_conf->got_show_repo_age) {
629 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
630 "index_header_age", KATTR__MAX);
632 return gw_kcgi_error(kerr);
633 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
635 return gw_kcgi_error(kerr);
636 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
638 return gw_kcgi_error(kerr);
641 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
643 return gw_kcgi_error(kerr);
645 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
646 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
647 "index_wrapper", KATTR__MAX);
649 return gw_kcgi_error(kerr);
650 kerr = khtml_printf(gw_trans->gw_html_req,
651 "No repositories found in %s",
652 gw_trans->gw_conf->got_repos_path);
654 return gw_kcgi_error(kerr);
655 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
657 return gw_kcgi_error(kerr);
658 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
659 "dotted_line", KATTR__MAX);
661 return gw_kcgi_error(kerr);
662 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
664 return gw_kcgi_error(kerr);
668 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
671 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
672 if (gw_trans->page > 0 && (gw_trans->page *
673 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
680 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
681 "index_wrapper", KATTR__MAX);
685 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
686 gw_dir->name, "action", "summary", NULL);
687 if (href_summary == NULL) {
688 error = got_error_from_errno("khttp_urlpart");
691 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
692 "index_project", KATTR__MAX);
695 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
696 href_summary, KATTR__MAX);
699 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
702 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
705 if (gw_trans->gw_conf->got_show_repo_description) {
706 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
707 KATTR_ID, "index_project_description", KATTR__MAX);
710 kerr = khtml_puts(gw_trans->gw_html_req,
711 gw_dir->description ? gw_dir->description : "");
714 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
718 if (gw_trans->gw_conf->got_show_repo_owner) {
719 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
720 KATTR_ID, "index_project_owner", KATTR__MAX);
723 kerr = khtml_puts(gw_trans->gw_html_req,
724 gw_dir->owner ? gw_dir->owner : "");
727 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
731 if (gw_trans->gw_conf->got_show_repo_age) {
732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
733 KATTR_ID, "index_project_age", KATTR__MAX);
736 kerr = khtml_puts(gw_trans->gw_html_req,
737 gw_dir->age ? gw_dir->age : "");
740 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
745 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
746 "navs_wrapper", KATTR__MAX);
749 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
754 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
755 href_summary, KATTR__MAX);
758 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
761 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
765 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
769 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
770 gw_dir->name, "action", "briefs", NULL);
771 if (href_briefs == NULL) {
772 error = got_error_from_errno("khttp_urlpart");
775 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
776 href_briefs, KATTR__MAX);
779 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
782 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
784 error = gw_kcgi_error(kerr);
786 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
790 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
791 gw_dir->name, "action", "commits", NULL);
792 if (href_commits == NULL) {
793 error = got_error_from_errno("khttp_urlpart");
796 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
797 href_commits, KATTR__MAX);
800 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
803 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
807 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
811 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
812 gw_dir->name, "action", "tags", NULL);
813 if (href_tags == NULL) {
814 error = got_error_from_errno("khttp_urlpart");
817 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
818 href_tags, KATTR__MAX);
821 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
824 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
828 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
832 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
833 gw_dir->name, "action", "tree", NULL);
834 if (href_tree == NULL) {
835 error = got_error_from_errno("khttp_urlpart");
838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
839 href_tree, KATTR__MAX);
842 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
846 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
849 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
850 "dotted_line", KATTR__MAX);
853 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
868 if (gw_trans->gw_conf->got_max_repos_display == 0)
871 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
872 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
873 (gw_trans->page > 0) &&
874 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
875 prev_disp == gw_trans->repos_total))) {
876 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
877 KATTR_ID, "np_wrapper", KATTR__MAX);
880 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
881 KATTR_ID, "nav_prev", KATTR__MAX);
886 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
887 (gw_trans->page > 0) &&
888 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
889 prev_disp == gw_trans->repos_total)) {
890 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
891 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
892 if (href_prev == NULL) {
893 error = got_error_from_errno("khttp_urlpartx");
896 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
897 KATTR_HREF, href_prev, KATTR__MAX);
900 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
903 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
908 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
910 return gw_kcgi_error(kerr);
912 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
913 next_disp == gw_trans->gw_conf->got_max_repos_display &&
914 dir_c != (gw_trans->page + 1) *
915 gw_trans->gw_conf->got_max_repos_display) {
916 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
917 KATTR_ID, "nav_next", KATTR__MAX);
920 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
921 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
922 if (href_next == NULL) {
923 error = got_error_from_errno("khttp_urlpartx");
926 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
927 KATTR_HREF, href_next, KATTR__MAX);
930 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
933 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
940 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
941 (gw_trans->page > 0) &&
942 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
943 prev_disp == gw_trans->repos_total)) {
944 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
958 if (error == NULL && kerr != KCGI_OK)
959 error = gw_kcgi_error(kerr);
963 static const struct got_error *
964 gw_commits(struct gw_trans *gw_trans)
966 const struct got_error *error = NULL;
967 struct gw_header *header = NULL, *n_header = NULL;
968 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
969 char *href_prev = NULL, *href_next = NULL;
970 enum kcgi_err kerr = KCGI_OK;
972 if ((header = gw_init_header()) == NULL)
973 return got_error_from_errno("malloc");
976 if (pledge("stdio rpath proc exec sendfd unveil",
978 error = got_error_from_errno("pledge");
982 error = gw_apply_unveil(gw_trans->gw_dir->path);
986 error = gw_get_header(gw_trans, header,
987 gw_trans->gw_conf->got_max_commits_display);
991 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
992 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
993 "commits_line_wrapper", KATTR__MAX);
996 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
1000 error = gw_gen_author_header(gw_trans, n_header->author);
1003 error = gw_gen_committer_header(gw_trans, n_header->author);
1006 error = gw_get_time_str(&age, n_header->committer_time,
1010 error = gw_gen_age_header(gw_trans, age ?age : "");
1013 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1014 if (kerr != KCGI_OK)
1017 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1018 "dotted_line", KATTR__MAX);
1019 if (kerr != KCGI_OK)
1021 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1022 if (kerr != KCGI_OK)
1025 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1026 "commit", KATTR__MAX);
1027 if (kerr != KCGI_OK)
1029 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1030 if (kerr != KCGI_OK)
1032 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1033 if (kerr != KCGI_OK)
1036 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1037 gw_trans->repo_name, "action", "diff", "commit",
1038 n_header->commit_id, NULL);
1039 if (href_diff == NULL) {
1040 error = got_error_from_errno("khttp_urlpart");
1043 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1044 KATTR_ID, "navs_wrapper", KATTR__MAX);
1045 if (kerr != KCGI_OK)
1047 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1048 KATTR_ID, "navs", KATTR__MAX);
1049 if (kerr != KCGI_OK)
1051 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1052 KATTR_HREF, href_diff, KATTR__MAX);
1053 if (kerr != KCGI_OK)
1055 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1056 if (kerr != KCGI_OK)
1058 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1059 if (kerr != KCGI_OK)
1062 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1063 if (kerr != KCGI_OK)
1066 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1067 gw_trans->repo_name, "action", "tree", "commit",
1068 n_header->commit_id, NULL);
1069 if (href_tree == NULL) {
1070 error = got_error_from_errno("khttp_urlpart");
1073 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1074 KATTR_HREF, href_tree, KATTR__MAX);
1075 if (kerr != KCGI_OK)
1077 khtml_puts(gw_trans->gw_html_req, "tree");
1078 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1079 if (kerr != KCGI_OK)
1081 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1082 if (kerr != KCGI_OK)
1085 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1086 "solid_line", KATTR__MAX);
1087 if (kerr != KCGI_OK)
1089 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1090 if (kerr != KCGI_OK)
1097 if (gw_trans->next_id || gw_trans->page > 0) {
1098 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1099 KATTR_ID, "np_wrapper", KATTR__MAX);
1100 if (kerr != KCGI_OK)
1102 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1103 KATTR_ID, "nav_prev", KATTR__MAX);
1104 if (kerr != KCGI_OK)
1108 if (gw_trans->page > 0 && gw_trans->prev_id) {
1109 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1110 KATTRX_STRING, gw_trans->repo_name, "page",
1111 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1112 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1113 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1114 KATTRX_STRING, gw_trans->prev_prev_id ?
1115 gw_trans->prev_prev_id : "", NULL);
1116 if (href_prev == NULL) {
1117 error = got_error_from_errno("khttp_urlpartx");
1120 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1121 KATTR_HREF, href_prev, KATTR__MAX);
1122 if (kerr != KCGI_OK)
1124 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1125 if (kerr != KCGI_OK)
1127 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1128 if (kerr != KCGI_OK)
1132 if (gw_trans->next_id || gw_trans->page > 0) {
1133 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1134 if (kerr != KCGI_OK)
1135 return gw_kcgi_error(kerr);
1138 if (gw_trans->next_id) {
1139 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1140 KATTR_ID, "nav_next", KATTR__MAX);
1141 if (kerr != KCGI_OK)
1143 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1144 KATTRX_STRING, gw_trans->repo_name, "page",
1145 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1146 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1147 gw_trans->next_id, "prev", KATTRX_STRING,
1148 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1149 "prev_prev", KATTRX_STRING, gw_trans->prev_prev_id ?
1150 gw_trans->prev_prev_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;
1196 if ((header = gw_init_header()) == NULL)
1197 return got_error_from_errno("malloc");
1200 if (pledge("stdio rpath proc exec sendfd unveil",
1202 error = got_error_from_errno("pledge");
1206 if (gw_trans->action != GW_SUMMARY) {
1207 error = gw_apply_unveil(gw_trans->gw_dir->path);
1212 if (gw_trans->action == GW_SUMMARY)
1213 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1215 error = gw_get_header(gw_trans, header,
1216 gw_trans->gw_conf->got_max_commits_display);
1220 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1221 error = gw_get_time_str(&age, n_header->committer_time,
1226 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1227 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1228 if (kerr != KCGI_OK)
1231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1232 KATTR_ID, "briefs_age", KATTR__MAX);
1233 if (kerr != KCGI_OK)
1235 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1236 if (kerr != KCGI_OK)
1238 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1239 if (kerr != KCGI_OK)
1242 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1243 KATTR_ID, "briefs_author", KATTR__MAX);
1244 if (kerr != KCGI_OK)
1246 smallerthan = strchr(n_header->author, '<');
1248 *smallerthan = '\0';
1249 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1250 if (kerr != KCGI_OK)
1252 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1253 if (kerr != KCGI_OK)
1256 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1257 gw_trans->repo_name, "action", "diff", "commit",
1258 n_header->commit_id, NULL);
1259 if (href_diff == NULL) {
1260 error = got_error_from_errno("khttp_urlpart");
1263 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1264 KATTR_ID, "briefs_log", KATTR__MAX);
1265 if (kerr != KCGI_OK)
1267 newline = strchr(n_header->commit_msg, '\n');
1270 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1271 KATTR_HREF, href_diff, KATTR__MAX);
1272 if (kerr != KCGI_OK)
1274 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1275 if (kerr != KCGI_OK)
1277 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1278 if (kerr != KCGI_OK)
1281 if (n_header->refs_str) {
1282 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1283 if (kerr != KCGI_OK)
1285 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1286 KATTR_ID, "refs_str", KATTR__MAX);
1287 if (kerr != KCGI_OK)
1289 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1290 n_header->refs_str);
1291 if (kerr != KCGI_OK)
1293 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1294 if (kerr != KCGI_OK)
1298 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1299 if (kerr != KCGI_OK)
1302 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1303 KATTR_ID, "navs_wrapper", KATTR__MAX);
1304 if (kerr != KCGI_OK)
1306 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1307 KATTR_ID, "navs", KATTR__MAX);
1308 if (kerr != KCGI_OK)
1310 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1311 KATTR_HREF, href_diff, KATTR__MAX);
1312 if (kerr != KCGI_OK)
1314 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1315 if (kerr != KCGI_OK)
1317 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1318 if (kerr != KCGI_OK)
1321 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1322 if (kerr != KCGI_OK)
1325 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1326 gw_trans->repo_name, "action", "tree", "commit",
1327 n_header->commit_id, NULL);
1328 if (href_tree == NULL) {
1329 error = got_error_from_errno("khttp_urlpart");
1332 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1333 KATTR_HREF, href_tree, KATTR__MAX);
1334 if (kerr != KCGI_OK)
1336 khtml_puts(gw_trans->gw_html_req, "tree");
1337 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1338 if (kerr != KCGI_OK)
1340 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1341 if (kerr != KCGI_OK)
1344 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1345 KATTR_ID, "dotted_line", KATTR__MAX);
1346 if (kerr != KCGI_OK)
1348 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1349 if (kerr != KCGI_OK)
1360 if (gw_trans->next_id || gw_trans->page > 0) {
1361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1362 KATTR_ID, "np_wrapper", KATTR__MAX);
1363 if (kerr != KCGI_OK)
1365 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1366 KATTR_ID, "nav_prev", KATTR__MAX);
1367 if (kerr != KCGI_OK)
1371 if (gw_trans->page > 0 && gw_trans->prev_id) {
1372 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1373 KATTRX_STRING, gw_trans->repo_name, "page",
1374 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1375 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1376 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1377 KATTRX_STRING, gw_trans->prev_prev_id ?
1378 gw_trans->prev_prev_id : "", NULL);
1379 if (href_prev == NULL) {
1380 error = got_error_from_errno("khttp_urlpartx");
1383 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1384 KATTR_HREF, href_prev, KATTR__MAX);
1385 if (kerr != KCGI_OK)
1387 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1388 if (kerr != KCGI_OK)
1390 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1391 if (kerr != KCGI_OK)
1395 if (gw_trans->next_id || gw_trans->page > 0) {
1396 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1397 if (kerr != KCGI_OK)
1398 return gw_kcgi_error(kerr);
1401 if (gw_trans->next_id) {
1402 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1403 KATTR_ID, "nav_next", KATTR__MAX);
1404 if (kerr != KCGI_OK)
1407 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1408 KATTRX_STRING, gw_trans->repo_name, "page",
1409 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1410 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1411 gw_trans->next_id, "prev", KATTRX_STRING,
1412 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1413 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1414 gw_trans->prev_id : "", NULL);
1415 if (href_next == NULL) {
1416 error = got_error_from_errno("khttp_urlpartx");
1419 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1420 KATTR_HREF, href_next, KATTR__MAX);
1421 if (kerr != KCGI_OK)
1423 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1424 if (kerr != KCGI_OK)
1426 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1427 if (kerr != KCGI_OK)
1431 if (gw_trans->next_id || gw_trans->page > 0) {
1432 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1433 if (kerr != KCGI_OK)
1437 gw_free_header(header);
1438 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1439 gw_free_header(n_header);
1445 if (error == NULL && kerr != KCGI_OK)
1446 error = gw_kcgi_error(kerr);
1450 static const struct got_error *
1451 gw_summary(struct gw_trans *gw_trans)
1453 const struct got_error *error = NULL;
1455 enum kcgi_err kerr = KCGI_OK;
1458 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1459 return got_error_from_errno("pledge");
1461 error = gw_apply_unveil(gw_trans->gw_dir->path);
1465 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1466 "summary_wrapper", KATTR__MAX);
1467 if (kerr != KCGI_OK)
1468 return gw_kcgi_error(kerr);
1470 if (gw_trans->gw_conf->got_show_repo_description &&
1471 gw_trans->gw_dir->description != NULL &&
1472 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1473 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1474 KATTR_ID, "description_title", KATTR__MAX);
1475 if (kerr != KCGI_OK)
1477 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1478 if (kerr != KCGI_OK)
1480 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1481 if (kerr != KCGI_OK)
1483 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1484 KATTR_ID, "description", KATTR__MAX);
1485 if (kerr != KCGI_OK)
1487 kerr = khtml_puts(gw_trans->gw_html_req,
1488 gw_trans->gw_dir->description);
1489 if (kerr != KCGI_OK)
1491 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1492 if (kerr != KCGI_OK)
1496 if (gw_trans->gw_conf->got_show_repo_owner &&
1497 gw_trans->gw_dir->owner != NULL &&
1498 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1499 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1500 KATTR_ID, "repo_owner_title", KATTR__MAX);
1501 if (kerr != KCGI_OK)
1503 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1504 if (kerr != KCGI_OK)
1506 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1507 if (kerr != KCGI_OK)
1509 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1510 KATTR_ID, "repo_owner", KATTR__MAX);
1511 if (kerr != KCGI_OK)
1513 kerr = khtml_puts(gw_trans->gw_html_req,
1514 gw_trans->gw_dir->owner);
1515 if (kerr != KCGI_OK)
1517 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1518 if (kerr != KCGI_OK)
1522 if (gw_trans->gw_conf->got_show_repo_age) {
1523 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1528 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1529 KATTR_ID, "last_change_title", KATTR__MAX);
1530 if (kerr != KCGI_OK)
1532 kerr = khtml_puts(gw_trans->gw_html_req,
1534 if (kerr != KCGI_OK)
1536 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1537 if (kerr != KCGI_OK)
1539 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1540 KATTR_ID, "last_change", KATTR__MAX);
1541 if (kerr != KCGI_OK)
1543 kerr = khtml_puts(gw_trans->gw_html_req, age);
1544 if (kerr != KCGI_OK)
1546 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1547 if (kerr != KCGI_OK)
1552 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1553 gw_trans->gw_dir->url != NULL &&
1554 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1555 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1556 KATTR_ID, "cloneurl_title", KATTR__MAX);
1557 if (kerr != KCGI_OK)
1559 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1560 if (kerr != KCGI_OK)
1562 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1563 if (kerr != KCGI_OK)
1565 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1566 KATTR_ID, "cloneurl", KATTR__MAX);
1567 if (kerr != KCGI_OK)
1569 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1570 if (kerr != KCGI_OK)
1572 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1573 if (kerr != KCGI_OK)
1577 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1578 if (kerr != KCGI_OK)
1581 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1582 "briefs_title_wrapper", KATTR__MAX);
1583 if (kerr != KCGI_OK)
1585 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1586 "briefs_title", KATTR__MAX);
1587 if (kerr != KCGI_OK)
1589 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1590 if (kerr != KCGI_OK)
1592 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1593 if (kerr != KCGI_OK)
1595 error = gw_briefs(gw_trans);
1599 error = gw_tags(gw_trans);
1603 error = gw_output_repo_heads(gw_trans);
1606 if (error == NULL && kerr != KCGI_OK)
1607 error = gw_kcgi_error(kerr);
1611 static const struct got_error *
1612 gw_tree(struct gw_trans *gw_trans)
1614 const struct got_error *error = NULL;
1615 struct gw_header *header = NULL;
1616 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1618 enum kcgi_err kerr = KCGI_OK;
1621 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1622 return got_error_from_errno("pledge");
1624 if ((header = gw_init_header()) == NULL)
1625 return got_error_from_errno("malloc");
1627 error = gw_apply_unveil(gw_trans->gw_dir->path);
1631 error = gw_get_header(gw_trans, header, 1);
1635 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1636 "tree_header_wrapper", KATTR__MAX);
1637 if (kerr != KCGI_OK)
1639 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1640 "tree_header", KATTR__MAX);
1641 if (kerr != KCGI_OK)
1643 error = gw_gen_tree_header(gw_trans, header->tree_id);
1646 error = gw_get_time_str(&age, header->committer_time,
1650 error = gw_gen_age_header(gw_trans, age ?age : "");
1653 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1656 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1657 if (kerr != KCGI_OK)
1659 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1660 "dotted_line", KATTR__MAX);
1661 if (kerr != KCGI_OK)
1663 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1664 if (kerr != KCGI_OK)
1667 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1668 "tree", KATTR__MAX);
1669 if (kerr != KCGI_OK)
1671 error = gw_output_repo_tree(gw_trans, header);
1675 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1677 gw_free_header(header);
1678 free(tree_html_disp);
1682 if (error == NULL && kerr != KCGI_OK)
1683 error = gw_kcgi_error(kerr);
1687 static const struct got_error *
1688 gw_tags(struct gw_trans *gw_trans)
1690 const struct got_error *error = NULL;
1691 struct gw_header *header = NULL;
1692 char *href_next = NULL, *href_prev = NULL;
1693 enum kcgi_err kerr = KCGI_OK;
1696 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1697 return got_error_from_errno("pledge");
1699 if ((header = gw_init_header()) == NULL)
1700 return got_error_from_errno("malloc");
1702 if (gw_trans->action != GW_SUMMARY) {
1703 error = gw_apply_unveil(gw_trans->gw_dir->path);
1708 error = gw_get_header(gw_trans, header, 1);
1712 if (gw_trans->action == GW_SUMMARY) {
1713 gw_trans->next_id = NULL;
1714 error = gw_output_repo_tags(gw_trans, header,
1715 D_MAXSLCOMMDISP, TAGBRIEF);
1719 error = gw_output_repo_tags(gw_trans, header,
1720 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1725 if (gw_trans->next_id || gw_trans->page > 0) {
1726 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1727 KATTR_ID, "np_wrapper", KATTR__MAX);
1728 if (kerr != KCGI_OK)
1730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1731 KATTR_ID, "nav_prev", KATTR__MAX);
1732 if (kerr != KCGI_OK)
1736 if (gw_trans->page > 0 && gw_trans->prev_id) {
1737 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1738 KATTRX_STRING, gw_trans->repo_name, "page",
1739 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1740 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1741 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1742 KATTRX_STRING, gw_trans->prev_prev_id ?
1743 gw_trans->prev_prev_id : "", NULL);
1744 if (href_prev == NULL) {
1745 error = got_error_from_errno("khttp_urlpartx");
1748 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1749 KATTR_HREF, href_prev, KATTR__MAX);
1750 if (kerr != KCGI_OK)
1752 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1753 if (kerr != KCGI_OK)
1755 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1756 if (kerr != KCGI_OK)
1760 if (gw_trans->next_id || gw_trans->page > 0) {
1761 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1762 if (kerr != KCGI_OK)
1763 return gw_kcgi_error(kerr);
1766 if (gw_trans->next_id) {
1767 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1768 KATTR_ID, "nav_next", KATTR__MAX);
1769 if (kerr != KCGI_OK)
1771 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1772 KATTRX_STRING, gw_trans->repo_name, "page",
1773 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1774 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1775 gw_trans->next_id, "prev", KATTRX_STRING,
1776 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1777 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1778 gw_trans->prev_id : "", NULL);
1779 if (href_next == NULL) {
1780 error = got_error_from_errno("khttp_urlpartx");
1783 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1784 KATTR_HREF, href_next, KATTR__MAX);
1785 if (kerr != KCGI_OK)
1787 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1788 if (kerr != KCGI_OK)
1790 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1791 if (kerr != KCGI_OK)
1795 if (gw_trans->next_id || gw_trans->page > 0) {
1796 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1797 if (kerr != KCGI_OK)
1801 gw_free_header(header);
1804 if (error == NULL && kerr != KCGI_OK)
1805 error = gw_kcgi_error(kerr);
1809 static const struct got_error *
1810 gw_tag(struct gw_trans *gw_trans)
1812 const struct got_error *error = NULL;
1813 struct gw_header *header = NULL;
1814 enum kcgi_err kerr = KCGI_OK;
1817 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1818 return got_error_from_errno("pledge");
1820 if ((header = gw_init_header()) == NULL)
1821 return got_error_from_errno("malloc");
1823 error = gw_apply_unveil(gw_trans->gw_dir->path);
1827 if (gw_trans->commit_id == NULL) {
1828 error = got_error_msg(GOT_ERR_QUERYSTRING,
1829 "commit required in querystring");
1833 error = gw_get_header(gw_trans, header, 1);
1837 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1838 "tag_header_wrapper", KATTR__MAX);
1839 if (kerr != KCGI_OK)
1841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1842 "tag_header", KATTR__MAX);
1843 if (kerr != KCGI_OK)
1845 error = gw_gen_commit_header(gw_trans, header->commit_id,
1849 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1852 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1853 if (kerr != KCGI_OK)
1855 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1856 "dotted_line", KATTR__MAX);
1857 if (kerr != KCGI_OK)
1859 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1860 if (kerr != KCGI_OK)
1863 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1864 "tree", KATTR__MAX);
1865 if (kerr != KCGI_OK)
1868 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1872 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1874 gw_free_header(header);
1875 if (error == NULL && kerr != KCGI_OK)
1876 error = gw_kcgi_error(kerr);
1880 static const struct got_error *
1881 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1883 const struct got_error *error = NULL;
1888 if (asprintf(&dir_test, "%s/%s/%s",
1889 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1890 GOTWEB_GIT_DIR) == -1)
1891 return got_error_from_errno("asprintf");
1893 dt = opendir(dir_test);
1897 gw_dir->path = strdup(dir_test);
1898 if (gw_dir->path == NULL) {
1900 error = got_error_from_errno("strdup");
1907 if (asprintf(&dir_test, "%s/%s/%s",
1908 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1909 GOTWEB_GOT_DIR) == -1) {
1911 error = got_error_from_errno("asprintf");
1915 dt = opendir(dir_test);
1920 error = got_error(GOT_ERR_NOT_GIT_REPO);
1924 if (asprintf(&dir_test, "%s/%s",
1925 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1926 error = got_error_from_errno("asprintf");
1931 gw_dir->path = strdup(dir_test);
1932 if (gw_dir->path == NULL) {
1934 error = got_error_from_errno("strdup");
1938 dt = opendir(dir_test);
1940 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1945 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1949 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1952 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1956 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1960 if (dt && closedir(dt) == -1 && error == NULL)
1961 error = got_error_from_errno("closedir");
1965 static const struct got_error *
1966 gw_load_got_paths(struct gw_trans *gw_trans)
1968 const struct got_error *error = NULL;
1970 struct dirent **sd_dent;
1971 struct gw_dir *gw_dir;
1973 unsigned int d_cnt, d_i;
1975 d = opendir(gw_trans->gw_conf->got_repos_path);
1977 error = got_error_from_errno2("opendir",
1978 gw_trans->gw_conf->got_repos_path);
1982 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1985 error = got_error_from_errno2("scandir",
1986 gw_trans->gw_conf->got_repos_path);
1990 for (d_i = 0; d_i < d_cnt; d_i++) {
1991 if (gw_trans->gw_conf->got_max_repos > 0 &&
1992 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1993 break; /* account for parent and self */
1995 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1996 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1999 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
2003 error = gw_load_got_path(gw_trans, gw_dir);
2004 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
2011 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
2012 !got_path_dir_is_empty(gw_dir->path)) {
2013 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
2015 gw_trans->repos_total++;
2019 if (d && closedir(d) == -1 && error == NULL)
2020 error = got_error_from_errno("closedir");
2024 static const struct got_error *
2025 gw_parse_querystring(struct gw_trans *gw_trans)
2027 const struct got_error *error = NULL;
2029 struct gw_query_action *action = NULL;
2032 if (gw_trans->gw_req->fieldnmap[0]) {
2033 return got_error(GOT_ERR_QUERYSTRING);
2034 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2035 /* define gw_trans->repo_path */
2036 gw_trans->repo_name = p->parsed.s;
2038 if (asprintf(&gw_trans->repo_path, "%s/%s",
2039 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2040 return got_error_from_errno("asprintf");
2042 /* get action and set function */
2043 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2044 for (i = 0; i < nitems(gw_query_funcs); i++) {
2045 action = &gw_query_funcs[i];
2046 if (action->func_name == NULL)
2048 if (strcmp(action->func_name,
2049 p->parsed.s) == 0) {
2050 gw_trans->action = i;
2055 if (gw_trans->action == -1) {
2056 gw_trans->action = GW_ERR;
2057 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2058 p != NULL ? "bad action in querystring" :
2059 "no action in querystring");
2063 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2064 if (asprintf(&gw_trans->commit_id, "%s",
2066 return got_error_from_errno("asprintf");
2069 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2070 gw_trans->repo_file = p->parsed.s;
2072 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2073 if (asprintf(&gw_trans->repo_folder, "%s",
2075 return got_error_from_errno("asprintf");
2078 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2079 if (asprintf(&gw_trans->prev_id, "%s",
2081 return got_error_from_errno("asprintf");
2084 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
2085 if (asprintf(&gw_trans->prev_prev_id, "%s",
2087 return got_error_from_errno("asprintf");
2090 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2091 gw_trans->headref = p->parsed.s;
2093 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2097 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2099 gw_trans->action = GW_INDEX;
2101 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2102 gw_trans->page = p->parsed.i;
2107 static const struct got_error *
2108 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2110 const struct got_error *error;
2112 *gw_dir = malloc(sizeof(**gw_dir));
2113 if (*gw_dir == NULL)
2114 return got_error_from_errno("malloc");
2116 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2117 error = got_error_from_errno("asprintf");
2126 static const struct got_error *
2127 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2129 enum kcgi_err kerr = KCGI_OK;
2131 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2132 if (kerr != KCGI_OK)
2133 return gw_kcgi_error(kerr);
2134 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2136 if (kerr != KCGI_OK)
2137 return gw_kcgi_error(kerr);
2138 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2140 if (kerr != KCGI_OK)
2141 return gw_kcgi_error(kerr);
2142 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2144 if (kerr != KCGI_OK)
2145 return gw_kcgi_error(kerr);
2146 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2147 if (kerr != KCGI_OK)
2148 return gw_kcgi_error(kerr);
2149 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2151 if (kerr != KCGI_OK)
2152 return gw_kcgi_error(kerr);
2154 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2155 kerr = khttp_head(gw_trans->gw_req,
2156 kresps[KRESP_CONTENT_DISPOSITION],
2157 "attachment; filename=%s", gw_trans->repo_file);
2158 if (kerr != KCGI_OK)
2159 return gw_kcgi_error(kerr);
2162 kerr = khttp_body(gw_trans->gw_req);
2163 return gw_kcgi_error(kerr);
2166 static const struct got_error *
2167 gw_display_index(struct gw_trans *gw_trans)
2169 const struct got_error *error;
2170 enum kcgi_err kerr = KCGI_OK;
2172 /* catch early querystring errors */
2173 if (gw_trans->error)
2174 gw_trans->action = GW_ERR;
2176 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2180 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2181 if (kerr != KCGI_OK)
2182 return gw_kcgi_error(kerr);
2184 if (gw_trans->action != GW_BLOB) {
2185 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2186 gw_query_funcs[gw_trans->action].template);
2187 if (kerr != KCGI_OK) {
2188 khtml_close(gw_trans->gw_html_req);
2189 return gw_kcgi_error(kerr);
2193 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2196 static const struct got_error *
2197 gw_error(struct gw_trans *gw_trans)
2199 enum kcgi_err kerr = KCGI_OK;
2201 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2203 return gw_kcgi_error(kerr);
2207 gw_template(size_t key, void *arg)
2209 const struct got_error *error = NULL;
2210 enum kcgi_err kerr = KCGI_OK;
2211 struct gw_trans *gw_trans = arg;
2212 char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
2213 char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
2215 if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
2216 "/apple-touch-icon.png") == -1)
2218 if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
2219 "/favicon-32x32.png") == -1)
2221 if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
2222 "/favicon-16x16.png") == -1)
2224 if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
2225 "/site.webmanifest") == -1)
2227 if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
2228 "/safari-pinned-tab.svg") == -1)
2230 if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
2231 "/gotweb.css") == -1)
2233 if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
2234 gw_trans->gw_conf->got_www_path ? "/" : "",
2235 gw_trans->gw_conf->got_logo) == -1)
2240 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2241 KATTR_NAME, "viewport",
2242 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2244 if (kerr != KCGI_OK)
2246 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2247 if (kerr != KCGI_OK)
2249 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2250 KATTR_CHARSET, "utf-8",
2252 if (kerr != KCGI_OK)
2254 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2255 if (kerr != KCGI_OK)
2257 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2258 KATTR_NAME, "msapplication-TileColor",
2259 KATTR_CONTENT, "#da532c", KATTR__MAX);
2260 if (kerr != KCGI_OK)
2262 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2263 if (kerr != KCGI_OK)
2265 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2266 KATTR_NAME, "theme-color",
2267 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2268 if (kerr != KCGI_OK)
2270 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2271 if (kerr != KCGI_OK)
2273 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2274 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2275 KATTR_HREF, ati, KATTR__MAX);
2276 if (kerr != KCGI_OK)
2278 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2279 if (kerr != KCGI_OK)
2281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2282 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2283 "32x32", KATTR_HREF, fic32, KATTR__MAX);
2284 if (kerr != KCGI_OK)
2286 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2287 if (kerr != KCGI_OK)
2289 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2290 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2291 "16x16", KATTR_HREF, fic16, KATTR__MAX);
2292 if (kerr != KCGI_OK)
2294 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2295 if (kerr != KCGI_OK)
2297 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2298 KATTR_REL, "manifest", KATTR_HREF, swm,
2300 if (kerr != KCGI_OK)
2302 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2303 if (kerr != KCGI_OK)
2305 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2306 KATTR_REL, "mask-icon", KATTR_HREF,
2308 if (kerr != KCGI_OK)
2310 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2311 if (kerr != KCGI_OK)
2313 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2314 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2315 KATTR_HREF, css, KATTR__MAX);
2316 if (kerr != KCGI_OK)
2318 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2319 if (kerr != KCGI_OK)
2323 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2324 KATTR_ID, "got_link", KATTR__MAX);
2325 if (kerr != KCGI_OK)
2327 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2328 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2329 KATTR_TARGET, "_sotd", KATTR__MAX);
2330 if (kerr != KCGI_OK)
2332 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2333 KATTR_SRC, logo, KATTR__MAX);
2334 if (kerr != KCGI_OK)
2336 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2337 if (kerr != KCGI_OK)
2340 case (TEMPL_SITEPATH):
2341 error = gw_output_site_link(gw_trans);
2346 if (gw_trans->gw_conf->got_site_name != NULL) {
2347 kerr = khtml_puts(gw_trans->gw_html_req,
2348 gw_trans->gw_conf->got_site_name);
2349 if (kerr != KCGI_OK)
2353 case (TEMPL_SEARCH):
2355 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2356 "search", KATTR__MAX);
2357 if (kerr != KCGI_OK)
2359 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2360 KATTR_METHOD, "POST", KATTR__MAX);
2361 if (kerr != KCGI_OK)
2363 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2364 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2365 KATTR_MAXLENGTH, "50", KATTR__MAX);
2366 if (kerr != KCGI_OK)
2368 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2370 if (kerr != KCGI_OK)
2372 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2373 if (kerr != KCGI_OK)
2375 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2376 if (kerr != KCGI_OK)
2379 case(TEMPL_SITEOWNER):
2380 if (gw_trans->gw_conf->got_site_owner != NULL &&
2381 gw_trans->gw_conf->got_show_site_owner) {
2382 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2383 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2384 if (kerr != KCGI_OK)
2386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2387 KATTR_ID, "site_owner", KATTR__MAX);
2388 if (kerr != KCGI_OK)
2390 kerr = khtml_puts(gw_trans->gw_html_req,
2391 gw_trans->gw_conf->got_site_owner);
2392 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2393 if (kerr != KCGI_OK)
2397 case(TEMPL_CONTENT):
2398 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2400 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2401 KATTR_ID, "tmpl_err", KATTR__MAX);
2402 if (kerr != KCGI_OK)
2404 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2406 if (kerr != KCGI_OK)
2408 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2409 if (kerr != KCGI_OK)
2435 static const struct got_error *
2436 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2438 const struct got_error *error = NULL;
2439 enum kcgi_err kerr = KCGI_OK;
2441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2442 KATTR_ID, "header_commit_title", KATTR__MAX);
2443 if (kerr != KCGI_OK)
2445 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2446 if (kerr != KCGI_OK)
2448 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2449 if (kerr != KCGI_OK)
2451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2452 KATTR_ID, "header_commit", KATTR__MAX);
2453 if (kerr != KCGI_OK)
2455 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2456 if (kerr != KCGI_OK)
2459 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2460 KATTR_ID, "refs_str", KATTR__MAX);
2461 if (kerr != KCGI_OK)
2463 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2464 if (kerr != KCGI_OK)
2466 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2467 if (kerr != KCGI_OK)
2470 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2472 if (error == NULL && kerr != KCGI_OK)
2473 error = gw_kcgi_error(kerr);
2477 static const struct got_error *
2478 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2480 const struct got_error *error = NULL;
2481 enum kcgi_err kerr = KCGI_OK;
2483 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2484 KATTR_ID, "header_diff_title", KATTR__MAX);
2485 if (kerr != KCGI_OK)
2487 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2488 if (kerr != KCGI_OK)
2490 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2491 if (kerr != KCGI_OK)
2493 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2494 KATTR_ID, "header_diff", KATTR__MAX);
2495 if (kerr != KCGI_OK)
2498 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2499 if (kerr != KCGI_OK)
2502 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2503 if (kerr != KCGI_OK)
2505 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2506 if (kerr != KCGI_OK)
2508 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2510 if (error == NULL && kerr != KCGI_OK)
2511 error = gw_kcgi_error(kerr);
2515 static const struct got_error *
2516 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2518 const struct got_error *error = NULL;
2519 enum kcgi_err kerr = KCGI_OK;
2521 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2522 KATTR_ID, "header_age_title", KATTR__MAX);
2523 if (kerr != KCGI_OK)
2525 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2526 if (kerr != KCGI_OK)
2528 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2529 if (kerr != KCGI_OK)
2531 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2532 KATTR_ID, "header_age", KATTR__MAX);
2533 if (kerr != KCGI_OK)
2535 kerr = khtml_puts(gw_trans->gw_html_req, str);
2536 if (kerr != KCGI_OK)
2538 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2540 if (error == NULL && kerr != KCGI_OK)
2541 error = gw_kcgi_error(kerr);
2545 static const struct got_error *
2546 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2548 const struct got_error *error = NULL;
2549 enum kcgi_err kerr = KCGI_OK;
2551 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2552 KATTR_ID, "header_author_title", KATTR__MAX);
2553 if (kerr != KCGI_OK)
2555 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2556 if (kerr != KCGI_OK)
2558 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2559 if (kerr != KCGI_OK)
2561 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2562 KATTR_ID, "header_author", KATTR__MAX);
2563 if (kerr != KCGI_OK)
2565 kerr = khtml_puts(gw_trans->gw_html_req, str);
2566 if (kerr != KCGI_OK)
2568 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2570 if (error == NULL && kerr != KCGI_OK)
2571 error = gw_kcgi_error(kerr);
2575 static const struct got_error *
2576 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2578 const struct got_error *error = NULL;
2579 enum kcgi_err kerr = KCGI_OK;
2581 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2582 KATTR_ID, "header_committer_title", KATTR__MAX);
2583 if (kerr != KCGI_OK)
2585 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2586 if (kerr != KCGI_OK)
2588 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2589 if (kerr != KCGI_OK)
2591 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2592 KATTR_ID, "header_committer", KATTR__MAX);
2593 if (kerr != KCGI_OK)
2595 kerr = khtml_puts(gw_trans->gw_html_req, str);
2596 if (kerr != KCGI_OK)
2598 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2600 if (error == NULL && kerr != KCGI_OK)
2601 error = gw_kcgi_error(kerr);
2605 static const struct got_error *
2606 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2608 const struct got_error *error = NULL;
2609 enum kcgi_err kerr = KCGI_OK;
2611 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2612 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2613 if (kerr != KCGI_OK)
2615 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2616 if (kerr != KCGI_OK)
2618 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2619 if (kerr != KCGI_OK)
2621 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2622 KATTR_ID, "header_commit_msg", KATTR__MAX);
2623 if (kerr != KCGI_OK)
2625 kerr = khttp_puts(gw_trans->gw_req, str);
2626 if (kerr != KCGI_OK)
2628 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2630 if (error == NULL && kerr != KCGI_OK)
2631 error = gw_kcgi_error(kerr);
2635 static const struct got_error *
2636 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2638 const struct got_error *error = NULL;
2639 enum kcgi_err kerr = KCGI_OK;
2641 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2642 KATTR_ID, "header_tree_title", KATTR__MAX);
2643 if (kerr != KCGI_OK)
2645 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2646 if (kerr != KCGI_OK)
2648 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2649 if (kerr != KCGI_OK)
2651 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2652 KATTR_ID, "header_tree", KATTR__MAX);
2653 if (kerr != KCGI_OK)
2655 kerr = khtml_puts(gw_trans->gw_html_req, str);
2656 if (kerr != KCGI_OK)
2658 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2660 if (error == NULL && kerr != KCGI_OK)
2661 error = gw_kcgi_error(kerr);
2665 static const struct got_error *
2666 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2669 const struct got_error *error = NULL;
2671 char *d_file = NULL;
2675 *description = NULL;
2676 if (gw_trans->gw_conf->got_show_repo_description == 0)
2679 if (asprintf(&d_file, "%s/description", dir) == -1)
2680 return got_error_from_errno("asprintf");
2682 f = fopen(d_file, "r");
2684 if (errno == ENOENT || errno == EACCES)
2686 error = got_error_from_errno2("fopen", d_file);
2690 if (fseek(f, 0, SEEK_END) == -1) {
2691 error = got_ferror(f, GOT_ERR_IO);
2696 error = got_ferror(f, GOT_ERR_IO);
2699 if (fseek(f, 0, SEEK_SET) == -1) {
2700 error = got_ferror(f, GOT_ERR_IO);
2703 *description = calloc(len + 1, sizeof(**description));
2704 if (*description == NULL) {
2705 error = got_error_from_errno("calloc");
2709 n = fread(*description, 1, len, f);
2710 if (n == 0 && ferror(f))
2711 error = got_ferror(f, GOT_ERR_IO);
2713 if (f != NULL && fclose(f) == EOF && error == NULL)
2714 error = got_error_from_errno("fclose");
2719 static const struct got_error *
2720 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2724 char *years = "years ago", *months = "months ago";
2725 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2726 char *minutes = "minutes ago", *seconds = "seconds ago";
2727 char *now = "right now";
2735 diff_time = time(NULL) - committer_time;
2736 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2737 if (asprintf(repo_age, "%lld %s",
2738 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2739 return got_error_from_errno("asprintf");
2740 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2741 if (asprintf(repo_age, "%lld %s",
2742 (diff_time / 60 / 60 / 24 / (365 / 12)),
2744 return got_error_from_errno("asprintf");
2745 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2746 if (asprintf(repo_age, "%lld %s",
2747 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2748 return got_error_from_errno("asprintf");
2749 } else if (diff_time > 60 * 60 * 24 * 2) {
2750 if (asprintf(repo_age, "%lld %s",
2751 (diff_time / 60 / 60 / 24), days) == -1)
2752 return got_error_from_errno("asprintf");
2753 } else if (diff_time > 60 * 60 * 2) {
2754 if (asprintf(repo_age, "%lld %s",
2755 (diff_time / 60 / 60), hours) == -1)
2756 return got_error_from_errno("asprintf");
2757 } else if (diff_time > 60 * 2) {
2758 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2760 return got_error_from_errno("asprintf");
2761 } else if (diff_time > 2) {
2762 if (asprintf(repo_age, "%lld %s", diff_time,
2764 return got_error_from_errno("asprintf");
2766 if (asprintf(repo_age, "%s", now) == -1)
2767 return got_error_from_errno("asprintf");
2771 if (gmtime_r(&committer_time, &tm) == NULL)
2772 return got_error_from_errno("gmtime_r");
2774 s = asctime_r(&tm, datebuf);
2776 return got_error_from_errno("asctime_r");
2778 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2779 return got_error_from_errno("asprintf");
2785 static const struct got_error *
2786 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2787 const char *refname, int ref_tm)
2789 const struct got_error *error = NULL;
2790 struct got_repository *repo = NULL;
2791 struct got_commit_object *commit = NULL;
2792 struct got_reflist_head refs;
2793 struct got_reflist_entry *re;
2794 time_t committer_time = 0, cmp_time = 0;
2799 if (gw_trans->gw_conf->got_show_repo_age == 0)
2803 repo = gw_trans->repo;
2805 error = got_repo_open(&repo, dir, NULL);
2810 error = got_ref_list(&refs, repo, "refs/heads",
2811 got_ref_cmp_by_name, NULL);
2816 * Find the youngest branch tip in the repository, or the age of
2817 * the a specific branch tip if a name was provided by the caller.
2819 TAILQ_FOREACH(re, &refs, entry) {
2820 struct got_object_id *id = NULL;
2822 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2825 error = got_ref_resolve(&id, repo, re->ref);
2829 error = got_object_open_as_commit(&commit, repo, id);
2835 got_object_commit_get_committer_time(commit);
2836 got_object_commit_close(commit);
2837 if (cmp_time < committer_time)
2838 cmp_time = committer_time;
2844 if (cmp_time != 0) {
2845 committer_time = cmp_time;
2846 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2849 got_ref_list_free(&refs);
2850 if (gw_trans->repo == NULL) {
2851 const struct got_error *close_err = got_repo_close(repo);
2858 static const struct got_error *
2859 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2861 const struct got_error *error;
2863 struct got_object_id *id1 = NULL, *id2 = NULL;
2864 char *label1 = NULL, *label2 = NULL, *line = NULL;
2866 size_t linesize = 0;
2868 enum kcgi_err kerr = KCGI_OK;
2874 if (header->parent_id != NULL &&
2875 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2876 error = got_repo_match_object_id(&id1, &label1,
2877 header->parent_id, GOT_OBJ_TYPE_ANY,
2878 &header->refs, gw_trans->repo);
2883 error = got_repo_match_object_id(&id2, &label2,
2884 header->commit_id, GOT_OBJ_TYPE_ANY, &header->refs,
2889 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2893 case GOT_OBJ_TYPE_BLOB:
2894 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
2895 NULL, NULL, 3, 0, 0, gw_trans->repo, f);
2897 case GOT_OBJ_TYPE_TREE:
2898 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
2899 "", "", 3, 0, 0, gw_trans->repo, f);
2901 case GOT_OBJ_TYPE_COMMIT:
2902 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
2903 3, 0, 0, gw_trans->repo, f);
2906 error = got_error(GOT_ERR_OBJ_TYPE);
2911 if (fseek(f, 0, SEEK_SET) == -1) {
2912 error = got_ferror(f, GOT_ERR_IO);
2916 while ((linelen = getline(&line, &linesize, f)) != -1) {
2917 error = gw_colordiff_line(gw_trans, line);
2920 /* XXX: KHTML_PRETTY breaks this */
2921 kerr = khtml_puts(gw_trans->gw_html_req, line);
2922 if (kerr != KCGI_OK)
2924 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2925 if (kerr != KCGI_OK)
2928 if (linelen == -1 && ferror(f))
2929 error = got_error_from_errno("getline");
2931 if (f && fclose(f) == EOF && error == NULL)
2932 error = got_error_from_errno("fclose");
2939 if (error == NULL && kerr != KCGI_OK)
2940 error = gw_kcgi_error(kerr);
2944 static const struct got_error *
2945 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2947 const struct got_error *error = NULL, *close_err;
2948 struct got_repository *repo;
2949 const char *gitconfig_owner;
2953 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2956 error = got_repo_open(&repo, dir, NULL);
2959 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2960 if (gitconfig_owner) {
2961 *owner = strdup(gitconfig_owner);
2963 error = got_error_from_errno("strdup");
2965 close_err = got_repo_close(repo);
2971 static const struct got_error *
2972 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2974 const struct got_error *error = NULL;
2976 char *d_file = NULL;
2982 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2983 return got_error_from_errno("asprintf");
2985 f = fopen(d_file, "r");
2987 if (errno != ENOENT && errno != EACCES)
2988 error = got_error_from_errno2("fopen", d_file);
2992 if (fseek(f, 0, SEEK_END) == -1) {
2993 error = got_ferror(f, GOT_ERR_IO);
2998 error = got_ferror(f, GOT_ERR_IO);
3001 if (fseek(f, 0, SEEK_SET) == -1) {
3002 error = got_ferror(f, GOT_ERR_IO);
3006 *url = calloc(len + 1, sizeof(**url));
3008 error = got_error_from_errno("calloc");
3012 n = fread(*url, 1, len, f);
3013 if (n == 0 && ferror(f))
3014 error = got_ferror(f, GOT_ERR_IO);
3016 if (f && fclose(f) == EOF && error == NULL)
3017 error = got_error_from_errno("fclose");
3022 static const struct got_error *
3023 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3024 int limit, int tag_type)
3026 const struct got_error *error = NULL;
3027 struct got_reflist_head refs;
3028 struct got_reflist_entry *re;
3030 char *id_str = NULL, *newline, *href_commits = NULL;
3031 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3032 struct got_tag_object *tag = NULL;
3033 enum kcgi_err kerr = KCGI_OK;
3034 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
3035 int prev_set = 0, tag_count = 0;
3039 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3040 got_ref_cmp_tags, gw_trans->repo);
3044 TAILQ_FOREACH(re, &refs, entry) {
3045 const char *refname;
3047 const char *tag_commit;
3049 struct got_object_id *id;
3050 struct got_commit_object *commit = NULL;
3052 refname = got_ref_get_name(re->ref);
3053 if (strncmp(refname, "refs/tags/", 10) != 0)
3057 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3061 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3063 if (error->code != GOT_ERR_OBJ_TYPE) {
3067 /* "lightweight" tag */
3068 error = got_object_open_as_commit(&commit,
3069 gw_trans->repo, id);
3074 tagger = got_object_commit_get_committer(commit);
3076 got_object_commit_get_committer_time(commit);
3077 error = got_object_id_str(&id_str, id);
3081 tagger = got_object_tag_get_tagger(tag);
3082 tagger_time = got_object_tag_get_tagger_time(tag);
3083 error = got_object_id_str(&id_str,
3084 got_object_tag_get_object_id(tag));
3089 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3090 strlen(id_str)) != 0)
3093 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3094 start_tag == 0 && strncmp(id_str, header->commit_id,
3095 strlen(id_str)) != 0) {
3103 if (prev_set == 0 && start_tag == 1) {
3104 gw_trans->next_prev_id = strdup(id_str);
3105 if (gw_trans->next_prev_id == NULL) {
3106 error = got_error_from_errno("strdup");
3113 gw_trans->next_id = strdup(id_str);
3114 if (gw_trans->next_id == NULL)
3115 error = got_error_from_errno("strdup");
3120 error = got_object_commit_get_logmsg(&tag_commit0,
3124 got_object_commit_close(commit);
3126 tag_commit0 = strdup(got_object_tag_get_message(tag));
3127 if (tag_commit0 == NULL) {
3128 error = got_error_from_errno("strdup");
3133 tag_commit = tag_commit0;
3134 while (*tag_commit == '\n')
3139 newline = strchr(tag_commit, '\n');
3143 if (summary_header_displayed == 0) {
3144 kerr = khtml_attr(gw_trans->gw_html_req,
3145 KELEM_DIV, KATTR_ID,
3146 "summary_tags_title_wrapper", KATTR__MAX);
3147 if (kerr != KCGI_OK)
3149 kerr = khtml_attr(gw_trans->gw_html_req,
3150 KELEM_DIV, KATTR_ID,
3151 "summary_tags_title", KATTR__MAX);
3152 if (kerr != KCGI_OK)
3154 kerr = khtml_puts(gw_trans->gw_html_req,
3156 if (kerr != KCGI_OK)
3158 kerr = khtml_closeelem(gw_trans->gw_html_req,
3160 if (kerr != KCGI_OK)
3162 kerr = khtml_attr(gw_trans->gw_html_req,
3163 KELEM_DIV, KATTR_ID,
3164 "summary_tags_content", KATTR__MAX);
3165 if (kerr != KCGI_OK)
3167 summary_header_displayed = 1;
3170 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3171 KATTR_ID, "tag_wrapper", KATTR__MAX);
3172 if (kerr != KCGI_OK)
3174 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3175 KATTR_ID, "tag_age", KATTR__MAX);
3176 if (kerr != KCGI_OK)
3178 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3181 kerr = khtml_puts(gw_trans->gw_html_req,
3183 if (kerr != KCGI_OK)
3185 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3186 if (kerr != KCGI_OK)
3188 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3189 KATTR_ID, "tag", KATTR__MAX);
3190 if (kerr != KCGI_OK)
3192 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3193 if (kerr != KCGI_OK)
3195 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3196 if (kerr != KCGI_OK)
3198 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3199 KATTR_ID, "tag_name", KATTR__MAX);
3200 if (kerr != KCGI_OK)
3203 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3204 gw_trans->repo_name, "action", "tag", "commit",
3206 if (href_tag == NULL) {
3207 error = got_error_from_errno("khttp_urlpart");
3210 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3211 KATTR_HREF, href_tag, KATTR__MAX);
3212 if (kerr != KCGI_OK)
3214 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3215 if (kerr != KCGI_OK)
3217 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3218 if (kerr != KCGI_OK)
3221 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3222 KATTR_ID, "navs_wrapper", KATTR__MAX);
3223 if (kerr != KCGI_OK)
3225 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3226 KATTR_ID, "navs", KATTR__MAX);
3227 if (kerr != KCGI_OK)
3230 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3231 KATTR_HREF, href_tag, KATTR__MAX);
3232 if (kerr != KCGI_OK)
3234 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3235 if (kerr != KCGI_OK)
3237 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3238 if (kerr != KCGI_OK)
3241 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3242 if (kerr != KCGI_OK)
3245 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3246 "path", gw_trans->repo_name, "action", "briefs",
3247 "commit", id_str, NULL);
3248 if (href_briefs == NULL) {
3249 error = got_error_from_errno("khttp_urlpart");
3252 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3253 KATTR_HREF, href_briefs, KATTR__MAX);
3254 if (kerr != KCGI_OK)
3256 kerr = khtml_puts(gw_trans->gw_html_req,
3258 if (kerr != KCGI_OK)
3260 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3261 if (kerr != KCGI_OK)
3264 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3265 if (kerr != KCGI_OK)
3268 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3269 "path", gw_trans->repo_name, "action", "commits",
3270 "commit", id_str, NULL);
3271 if (href_commits == NULL) {
3272 error = got_error_from_errno("khttp_urlpart");
3275 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3276 KATTR_HREF, href_commits, KATTR__MAX);
3277 if (kerr != KCGI_OK)
3279 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3280 if (kerr != KCGI_OK)
3282 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3283 if (kerr != KCGI_OK)
3286 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3287 KATTR_ID, "dotted_line", KATTR__MAX);
3288 if (kerr != KCGI_OK)
3290 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3291 if (kerr != KCGI_OK)
3295 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3296 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3297 if (kerr != KCGI_OK)
3299 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3300 if (kerr != KCGI_OK)
3302 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3303 if (kerr != KCGI_OK)
3305 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3306 KATTR_ID, "tag_info_date", KATTR__MAX);
3307 if (kerr != KCGI_OK)
3309 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3312 kerr = khtml_puts(gw_trans->gw_html_req,
3314 if (kerr != KCGI_OK)
3316 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3317 if (kerr != KCGI_OK)
3320 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3321 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3322 if (kerr != KCGI_OK)
3324 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3325 if (kerr != KCGI_OK)
3327 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3328 if (kerr != KCGI_OK)
3330 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3331 KATTR_ID, "tag_info_date", KATTR__MAX);
3332 if (kerr != KCGI_OK)
3334 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3335 if (kerr != KCGI_OK)
3337 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3338 if (kerr != KCGI_OK)
3341 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3342 KATTR_ID, "tag_info", KATTR__MAX);
3343 if (kerr != KCGI_OK)
3345 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3346 if (kerr != KCGI_OK)
3352 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3353 if (kerr != KCGI_OK)
3356 if (limit && --limit == 0)
3360 got_object_tag_close(tag);
3373 href_commits = NULL;
3375 if (tag_count == 0) {
3376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3377 "summary_tags_title_wrapper", KATTR__MAX);
3378 if (kerr != KCGI_OK)
3380 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3381 "summary_tags_title", KATTR__MAX);
3382 if (kerr != KCGI_OK)
3384 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3385 if (kerr != KCGI_OK)
3387 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3388 if (kerr != KCGI_OK)
3390 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3391 "summary_tags_content", KATTR__MAX);
3392 if (kerr != KCGI_OK)
3394 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3395 "tags_info", KATTR__MAX);
3396 if (kerr != KCGI_OK)
3398 kerr = khttp_puts(gw_trans->gw_req,
3399 "There are no tags for this repo.");
3400 if (kerr != KCGI_OK)
3402 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3406 got_object_tag_close(tag);
3413 got_ref_list_free(&refs);
3414 if (error == NULL && kerr != KCGI_OK)
3415 error = gw_kcgi_error(kerr);
3420 gw_free_header(struct gw_header *header)
3423 free(header->author);
3424 free(header->committer);
3425 free(header->refs_str);
3426 free(header->commit_id);
3427 free(header->parent_id);
3428 free(header->tree_id);
3429 free(header->commit_msg);
3432 static struct gw_header *
3435 struct gw_header *header;
3437 header = malloc(sizeof(*header));
3441 header->path = NULL;
3442 TAILQ_INIT(&header->refs);
3444 header->refs_str = NULL;
3445 header->commit_id = NULL;
3446 header->committer = NULL;
3447 header->author = NULL;
3448 header->parent_id = NULL;
3449 header->tree_id = NULL;
3450 header->commit_msg = NULL;
3455 static const struct got_error *
3456 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3457 int limit, struct got_object_id *id)
3459 const struct got_error *error = NULL;
3460 struct got_commit_graph *graph = NULL;
3461 struct got_commit_object *commit = NULL;
3462 int chk_next = 0, chk_multi = 0, prev_set = 0;
3464 error = got_commit_graph_open(&graph, header->path, 0);
3468 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3474 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3477 if (error->code == GOT_ERR_ITER_COMPLETED)
3484 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3487 if (limit == 1 && chk_multi == 0 &&
3488 gw_trans->gw_conf->got_max_commits_display != 1) {
3489 error = gw_get_commit(gw_trans, header, commit, id);
3494 struct gw_header *n_header = NULL;
3495 if ((n_header = gw_init_header()) == NULL) {
3496 error = got_error_from_errno("malloc");
3499 error = got_ref_list(&n_header->refs, gw_trans->repo,
3500 NULL, got_ref_cmp_by_name, NULL);
3504 error = gw_get_commit(gw_trans, n_header, commit, id);
3507 got_ref_list_free(&n_header->refs);
3510 * we have a commit_id now, so copy it to next_prev_id
3511 * for navigation through gw_briefs and gw_commits
3513 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3514 (gw_trans->action == GW_BRIEFS ||
3515 gw_trans->action == GW_COMMITS ||
3516 gw_trans->action == GW_SUMMARY)) {
3518 gw_trans->next_prev_id =
3519 strdup(n_header->commit_id);
3520 if (gw_trans->next_prev_id == NULL) {
3521 error = got_error_from_errno("strdup");
3527 * check for one more commit before breaking,
3528 * so we know whether to navicate through gw_briefs
3529 * gw_commits and gw_summary
3531 if (chk_next && (gw_trans->action == GW_BRIEFS||
3532 gw_trans->action == GW_COMMITS ||
3533 gw_trans->action == GW_SUMMARY)) {
3534 gw_trans->next_id = strdup(n_header->commit_id);
3535 if (gw_trans->next_id == NULL)
3536 error = got_error_from_errno("strdup");
3540 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3543 if (error || (limit && --limit == 0)) {
3551 got_object_commit_close(commit);
3553 got_commit_graph_close(graph);
3557 static const struct got_error *
3558 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3559 struct got_commit_object *commit, struct got_object_id *id)
3561 const struct got_error *error = NULL;
3562 struct got_reflist_entry *re;
3563 struct got_object_id *id2 = NULL;
3564 struct got_object_qid *parent_id;
3565 char *commit_msg = NULL, *commit_msg0;
3568 TAILQ_FOREACH(re, &header->refs, entry) {
3571 struct got_tag_object *tag = NULL;
3572 struct got_object_id *ref_id;
3575 if (got_ref_is_symbolic(re->ref))
3578 name = got_ref_get_name(re->ref);
3579 if (strncmp(name, "refs/", 5) == 0)
3581 if (strncmp(name, "got/", 4) == 0)
3583 if (strncmp(name, "heads/", 6) == 0)
3585 if (strncmp(name, "remotes/", 8) == 0) {
3587 s = strstr(name, "/" GOT_REF_HEAD);
3588 if (s != NULL && s[strlen(s)] == '\0')
3591 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3594 if (strncmp(name, "tags/", 5) == 0) {
3595 error = got_object_open_as_tag(&tag, gw_trans->repo,
3598 if (error->code != GOT_ERR_OBJ_TYPE) {
3603 * Ref points at something other
3610 cmp = got_object_id_cmp(tag ?
3611 got_object_tag_get_object_id(tag) : ref_id, id);
3614 got_object_tag_close(tag);
3617 s = header->refs_str;
3618 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3619 s ? ", " : "", name) == -1) {
3620 error = got_error_from_errno("asprintf");
3622 header->refs_str = NULL;
3628 error = got_object_id_str(&header->commit_id, id);
3632 error = got_object_id_str(&header->tree_id,
3633 got_object_commit_get_tree_id(commit));
3637 if (gw_trans->action == GW_DIFF) {
3638 parent_id = SIMPLEQ_FIRST(
3639 got_object_commit_get_parent_ids(commit));
3640 if (parent_id != NULL) {
3641 id2 = got_object_id_dup(parent_id->id);
3643 error = got_object_id_str(&header->parent_id, id2);
3648 header->parent_id = strdup("/dev/null");
3649 if (header->parent_id == NULL) {
3650 error = got_error_from_errno("strdup");
3656 header->committer_time =
3657 got_object_commit_get_committer_time(commit);
3660 strdup(got_object_commit_get_author(commit));
3661 if (header->author == NULL) {
3662 error = got_error_from_errno("strdup");
3666 strdup(got_object_commit_get_committer(commit));
3667 if (header->committer == NULL) {
3668 error = got_error_from_errno("strdup");
3671 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3675 commit_msg = commit_msg0;
3676 while (*commit_msg == '\n')
3679 header->commit_msg = strdup(commit_msg);
3680 if (header->commit_msg == NULL)
3681 error = got_error_from_errno("strdup");
3686 static const struct got_error *
3687 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3689 const struct got_error *error = NULL;
3690 char *in_repo_path = NULL;
3691 struct got_object_id *id = NULL;
3693 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3697 if (gw_trans->commit_id == NULL) {
3698 struct got_reference *head_ref;
3699 error = got_ref_open(&head_ref, gw_trans->repo,
3700 gw_trans->headref, 0);
3704 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3705 got_ref_close(head_ref);
3709 struct got_reference *ref;
3711 error = got_ref_open(&ref, gw_trans->repo,
3712 gw_trans->commit_id, 0);
3713 if (error == NULL) {
3715 error = got_ref_resolve(&id, gw_trans->repo, ref);
3719 error = got_object_get_type(&obj_type, gw_trans->repo,
3723 if (obj_type == GOT_OBJ_TYPE_TAG) {
3724 struct got_tag_object *tag;
3725 error = got_object_open_as_tag(&tag,
3726 gw_trans->repo, id);
3729 if (got_object_tag_get_object_type(tag) !=
3730 GOT_OBJ_TYPE_COMMIT) {
3731 got_object_tag_close(tag);
3732 error = got_error(GOT_ERR_OBJ_TYPE);
3736 id = got_object_id_dup(
3737 got_object_tag_get_object_id(tag));
3739 error = got_error_from_errno(
3740 "got_object_id_dup");
3741 got_object_tag_close(tag);
3744 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3745 error = got_error(GOT_ERR_OBJ_TYPE);
3749 error = got_repo_match_object_id_prefix(&id,
3750 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3756 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3757 gw_trans->repo_path);
3762 header->path = strdup(in_repo_path);
3763 if (header->path == NULL) {
3764 error = got_error_from_errno("strdup");
3769 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3770 got_ref_cmp_by_name, NULL);
3774 error = gw_get_commits(gw_trans, header, limit, id);
3776 got_ref_list_free(&header->refs);
3786 char datebuf[11]; /* YYYY-MM-DD + NUL */
3789 struct gw_blame_cb_args {
3790 struct blame_line *lines;
3794 off_t *line_offsets;
3796 struct got_repository *repo;
3797 struct gw_trans *gw_trans;
3800 static const struct got_error *
3801 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3803 const struct got_error *err = NULL;
3804 struct gw_blame_cb_args *a = arg;
3805 struct blame_line *bline;
3807 size_t linesize = 0;
3808 struct got_commit_object *commit = NULL;
3811 time_t committer_time;
3812 enum kcgi_err kerr = KCGI_OK;
3814 if (nlines != a->nlines ||
3815 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3816 return got_error(GOT_ERR_RANGE);
3819 return NULL; /* no change in this commit */
3821 /* Annotate this line. */
3822 bline = &a->lines[lineno - 1];
3823 if (bline->annotated)
3825 err = got_object_id_str(&bline->id_str, id);
3829 err = got_object_open_as_commit(&commit, a->repo, id);
3833 bline->committer = strdup(got_object_commit_get_committer(commit));
3834 if (bline->committer == NULL) {
3835 err = got_error_from_errno("strdup");
3839 committer_time = got_object_commit_get_committer_time(commit);
3840 if (localtime_r(&committer_time, &tm) == NULL)
3841 return got_error_from_errno("localtime_r");
3842 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3844 err = got_error(GOT_ERR_NO_SPACE);
3847 bline->annotated = 1;
3849 /* Print lines annotated so far. */
3850 bline = &a->lines[a->lineno_cur - 1];
3851 if (!bline->annotated)
3854 offset = a->line_offsets[a->lineno_cur - 1];
3855 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3856 err = got_error_from_errno("fseeko");
3860 while (bline->annotated) {
3861 char *smallerthan, *at, *nl, *committer;
3862 char *href_diff = NULL;
3865 if (getline(&line, &linesize, a->f) == -1) {
3867 err = got_error_from_errno("getline");
3871 committer = bline->committer;
3872 smallerthan = strchr(committer, '<');
3873 if (smallerthan && smallerthan[1] != '\0')
3874 committer = smallerthan + 1;
3875 at = strchr(committer, '@');
3878 len = strlen(committer);
3880 committer[8] = '\0';
3882 nl = strchr(line, '\n');
3886 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3887 "blame_wrapper", KATTR__MAX);
3888 if (kerr != KCGI_OK)
3890 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3891 "blame_number", KATTR__MAX);
3892 if (kerr != KCGI_OK)
3894 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3895 a->nlines_prec, a->lineno_cur);
3896 if (kerr != KCGI_OK)
3898 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3899 if (kerr != KCGI_OK)
3902 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3903 "blame_hash", KATTR__MAX);
3904 if (kerr != KCGI_OK)
3907 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3908 a->gw_trans->repo_name, "action", "diff", "commit",
3909 bline->id_str, NULL);
3910 if (href_diff == NULL) {
3911 err = got_error_from_errno("khttp_urlpart");
3914 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3915 KATTR_HREF, href_diff, KATTR__MAX);
3916 if (kerr != KCGI_OK)
3918 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3920 if (kerr != KCGI_OK)
3922 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3923 if (kerr != KCGI_OK)
3926 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3927 "blame_date", KATTR__MAX);
3928 if (kerr != KCGI_OK)
3930 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3931 if (kerr != KCGI_OK)
3933 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3934 if (kerr != KCGI_OK)
3937 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3938 "blame_author", KATTR__MAX);
3939 if (kerr != KCGI_OK)
3941 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3942 if (kerr != KCGI_OK)
3944 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3945 if (kerr != KCGI_OK)
3948 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3949 "blame_code", KATTR__MAX);
3950 if (kerr != KCGI_OK)
3952 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3953 if (kerr != KCGI_OK)
3955 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3956 if (kerr != KCGI_OK)
3959 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3960 if (kerr != KCGI_OK)
3964 bline = &a->lines[a->lineno_cur - 1];
3970 got_object_commit_close(commit);
3972 if (err == NULL && kerr != KCGI_OK)
3973 err = gw_kcgi_error(kerr);
3977 static const struct got_error *
3978 gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
3980 const struct got_error *error = NULL;
3981 struct got_object_id *obj_id = NULL;
3982 struct got_object_id *commit_id = NULL;
3983 struct got_blob_object *blob = NULL;
3984 char *path = NULL, *in_repo_path = NULL;
3985 struct gw_blame_cb_args bca;
3989 if (asprintf(&path, "%s%s%s",
3990 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3991 gw_trans->repo_folder ? "/" : "",
3992 gw_trans->repo_file) == -1) {
3993 error = got_error_from_errno("asprintf");
3997 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4001 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4002 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4006 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4011 if (obj_id == NULL) {
4012 error = got_error(GOT_ERR_NO_OBJ);
4016 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4020 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4021 error = got_error(GOT_ERR_OBJ_TYPE);
4025 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4029 bca.f = got_opentemp();
4030 if (bca.f == NULL) {
4031 error = got_error_from_errno("got_opentemp");
4034 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4035 &bca.line_offsets, bca.f, blob);
4036 if (error || bca.nlines == 0)
4039 /* Don't include \n at EOF in the blame line count. */
4040 if (bca.line_offsets[bca.nlines - 1] == filesize)
4043 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4044 if (bca.lines == NULL) {
4045 error = got_error_from_errno("calloc");
4049 bca.nlines_prec = 0;
4055 bca.repo = gw_trans->repo;
4056 bca.gw_trans = gw_trans;
4058 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4067 free(bca.line_offsets);
4068 for (i = 0; i < bca.nlines; i++) {
4069 struct blame_line *bline = &bca.lines[i];
4070 free(bline->id_str);
4071 free(bline->committer);
4074 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4075 error = got_error_from_errno("fclose");
4078 got_object_blob_close(blob);
4082 static const struct got_error *
4083 gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
4085 const struct got_error *error = NULL;
4086 struct got_object_id *obj_id = NULL;
4087 struct got_object_id *commit_id = NULL;
4088 struct got_blob_object *blob = NULL;
4089 char *path = NULL, *in_repo_path = NULL;
4090 int obj_type, set_mime = 0;
4093 enum kcgi_err kerr = KCGI_OK;
4095 if (asprintf(&path, "%s%s%s",
4096 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4097 gw_trans->repo_folder ? "/" : "",
4098 gw_trans->repo_file) == -1) {
4099 error = got_error_from_errno("asprintf");
4103 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4107 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4108 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4112 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4117 if (obj_id == NULL) {
4118 error = got_error(GOT_ERR_NO_OBJ);
4122 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4126 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4127 error = got_error(GOT_ERR_OBJ_TYPE);
4131 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4135 hdrlen = got_object_blob_get_hdrlen(blob);
4137 error = got_object_blob_read_block(&len, blob);
4140 buf = got_object_blob_get_read_buf(blob);
4143 * Skip blob object header first time around,
4144 * which also contains a zero byte.
4147 if (set_mime == 0) {
4148 if (isbinary(buf, len - hdrlen))
4149 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4151 gw_trans->mime = KMIME_TEXT_PLAIN;
4153 error = gw_display_index(gw_trans);
4157 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4158 if (kerr != KCGI_OK)
4168 got_object_blob_close(blob);
4169 if (error == NULL && kerr != KCGI_OK)
4170 error = gw_kcgi_error(kerr);
4174 static const struct got_error *
4175 gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
4177 const struct got_error *error = NULL;
4178 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4179 struct got_tree_object *tree = NULL;
4180 char *path = NULL, *in_repo_path = NULL;
4181 char *id_str = NULL;
4182 char *build_folder = NULL;
4183 char *href_blob = NULL, *href_blame = NULL;
4184 const char *class = NULL;
4185 int nentries, i, class_flip = 0;
4186 enum kcgi_err kerr = KCGI_OK;
4188 if (gw_trans->repo_folder != NULL) {
4189 path = strdup(gw_trans->repo_folder);
4191 error = got_error_from_errno("strdup");
4195 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4196 gw_trans->repo_path);
4200 path = in_repo_path;
4203 if (gw_trans->commit_id == NULL) {
4204 struct got_reference *head_ref;
4205 error = got_ref_open(&head_ref, gw_trans->repo,
4206 gw_trans->headref, 0);
4209 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4212 got_ref_close(head_ref);
4214 * gw_trans->commit_id was not parsed from the querystring
4215 * we hit this code path from gw_index, where we don't know the
4216 * commit values for the tree link yet, so set
4217 * gw_trans->commit_id here to continue further into the tree
4219 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4224 error = got_repo_match_object_id(&commit_id, NULL,
4225 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
4231 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4236 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4240 nentries = got_object_tree_get_nentries(tree);
4241 for (i = 0; i < nentries; i++) {
4242 struct got_tree_entry *te;
4243 const char *modestr = "";
4246 te = got_object_tree_get_entry(tree, i);
4248 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4252 mode = got_tree_entry_get_mode(te);
4253 if (got_object_tree_entry_is_submodule(te))
4255 else if (S_ISLNK(mode))
4257 else if (S_ISDIR(mode))
4259 else if (mode & S_IXUSR)
4262 if (class_flip == 0) {
4263 class = "back_lightgray";
4266 class = "back_white";
4270 if (S_ISDIR(mode)) {
4271 if (asprintf(&build_folder, "%s/%s",
4272 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4273 got_tree_entry_get_name(te)) == -1) {
4274 error = got_error_from_errno("asprintf");
4278 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4279 gw_trans->repo_name, "action",
4280 gw_get_action_name(gw_trans), "commit",
4281 gw_trans->commit_id, "folder", build_folder, NULL);
4282 if (href_blob == NULL) {
4283 error = got_error_from_errno("khttp_urlpart");
4286 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4287 KATTR_ID, "tree_wrapper", KATTR__MAX);
4288 if (kerr != KCGI_OK)
4290 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4291 KATTR_ID, "tree_line", KATTR_CLASS, class,
4293 if (kerr != KCGI_OK)
4295 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4296 KATTR_HREF, href_blob, KATTR_CLASS,
4297 "diff_directory", KATTR__MAX);
4298 if (kerr != KCGI_OK)
4300 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4301 got_tree_entry_get_name(te), modestr);
4302 if (kerr != KCGI_OK)
4304 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4305 if (kerr != KCGI_OK)
4307 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4308 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4310 if (kerr != KCGI_OK)
4312 kerr = khtml_entity(gw_trans->gw_html_req,
4314 if (kerr != KCGI_OK)
4316 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4317 if (kerr != KCGI_OK)
4320 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4321 gw_trans->repo_name, "action", "blob", "commit",
4322 gw_trans->commit_id, "file",
4323 got_tree_entry_get_name(te), "folder",
4324 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4326 if (href_blob == NULL) {
4327 error = got_error_from_errno("khttp_urlpart");
4330 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4331 gw_trans->repo_name, "action", "blame", "commit",
4332 gw_trans->commit_id, "file",
4333 got_tree_entry_get_name(te), "folder",
4334 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4336 if (href_blame == NULL) {
4337 error = got_error_from_errno("khttp_urlpart");
4340 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4341 KATTR_ID, "tree_wrapper", KATTR__MAX);
4342 if (kerr != KCGI_OK)
4344 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4345 KATTR_ID, "tree_line", KATTR_CLASS, class,
4347 if (kerr != KCGI_OK)
4349 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4350 KATTR_HREF, href_blob, KATTR__MAX);
4351 if (kerr != KCGI_OK)
4353 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4354 got_tree_entry_get_name(te), modestr);
4355 if (kerr != KCGI_OK)
4357 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4358 if (kerr != KCGI_OK)
4360 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4361 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4363 if (kerr != KCGI_OK)
4366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4367 KATTR_HREF, href_blob, KATTR__MAX);
4368 if (kerr != KCGI_OK)
4370 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4371 if (kerr != KCGI_OK)
4373 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4374 if (kerr != KCGI_OK)
4377 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4378 if (kerr != KCGI_OK)
4381 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4382 KATTR_HREF, href_blame, KATTR__MAX);
4383 if (kerr != KCGI_OK)
4385 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4386 if (kerr != KCGI_OK)
4389 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4390 if (kerr != KCGI_OK)
4398 build_folder = NULL;
4402 got_object_tree_close(tree);
4409 if (error == NULL && kerr != KCGI_OK)
4410 error = gw_kcgi_error(kerr);
4414 static const struct got_error *
4415 gw_output_repo_heads(struct gw_trans *gw_trans)
4417 const struct got_error *error = NULL;
4418 struct got_reflist_head refs;
4419 struct got_reflist_entry *re;
4420 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4421 char *href_commits = NULL;
4422 enum kcgi_err kerr = KCGI_OK;
4426 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4427 got_ref_cmp_by_name, NULL);
4431 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4432 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4433 if (kerr != KCGI_OK)
4435 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4436 KATTR_ID, "summary_heads_title", KATTR__MAX);
4437 if (kerr != KCGI_OK)
4439 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4440 if (kerr != KCGI_OK)
4442 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4443 if (kerr != KCGI_OK)
4445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4446 KATTR_ID, "summary_heads_content", KATTR__MAX);
4447 if (kerr != KCGI_OK)
4450 TAILQ_FOREACH(re, &refs, entry) {
4451 const char *refname;
4453 if (got_ref_is_symbolic(re->ref))
4456 refname = got_ref_get_name(re->ref);
4457 if (strncmp(refname, "refs/heads/", 11) != 0)
4460 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4465 if (strncmp(refname, "refs/heads/", 11) == 0)
4468 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4469 KATTR_ID, "heads_wrapper", KATTR__MAX);
4470 if (kerr != KCGI_OK)
4472 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4473 KATTR_ID, "heads_age", KATTR__MAX);
4474 if (kerr != KCGI_OK)
4476 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4477 if (kerr != KCGI_OK)
4479 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4480 if (kerr != KCGI_OK)
4482 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4483 KATTR_ID, "heads_space", KATTR__MAX);
4484 if (kerr != KCGI_OK)
4486 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4487 if (kerr != KCGI_OK)
4489 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4490 if (kerr != KCGI_OK)
4492 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4493 KATTR_ID, "head", KATTR__MAX);
4494 if (kerr != KCGI_OK)
4497 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4498 gw_trans->repo_name, "action", "summary", "headref",
4500 if (href_summary == NULL) {
4501 error = got_error_from_errno("khttp_urlpart");
4504 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4505 href_summary, KATTR__MAX);
4506 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4507 if (kerr != KCGI_OK)
4509 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4510 if (kerr != KCGI_OK)
4513 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4514 "navs_wrapper", KATTR__MAX);
4515 if (kerr != KCGI_OK)
4517 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4518 "navs", KATTR__MAX);
4519 if (kerr != KCGI_OK)
4522 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4523 href_summary, KATTR__MAX);
4524 if (kerr != KCGI_OK)
4526 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4527 if (kerr != KCGI_OK)
4529 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4530 if (kerr != KCGI_OK)
4533 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4534 if (kerr != KCGI_OK)
4537 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4538 gw_trans->repo_name, "action", "briefs", "headref",
4540 if (href_briefs == NULL) {
4541 error = got_error_from_errno("khttp_urlpart");
4544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4545 href_briefs, KATTR__MAX);
4546 if (kerr != KCGI_OK)
4548 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4549 if (kerr != KCGI_OK)
4551 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4552 if (kerr != KCGI_OK)
4555 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4556 if (kerr != KCGI_OK)
4559 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4560 gw_trans->repo_name, "action", "commits", "headref",
4562 if (href_commits == NULL) {
4563 error = got_error_from_errno("khttp_urlpart");
4566 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4567 href_commits, KATTR__MAX);
4568 if (kerr != KCGI_OK)
4570 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4571 if (kerr != KCGI_OK)
4573 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4574 if (kerr != KCGI_OK)
4577 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4578 "dotted_line", KATTR__MAX);
4579 if (kerr != KCGI_OK)
4581 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4582 if (kerr != KCGI_OK)
4585 href_summary = NULL;
4589 href_commits = NULL;
4592 got_ref_list_free(&refs);
4599 static const struct got_error *
4600 gw_output_site_link(struct gw_trans *gw_trans)
4602 const struct got_error *error = NULL;
4603 char *href_summary = NULL;
4604 enum kcgi_err kerr = KCGI_OK;
4606 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4607 "site_link", KATTR__MAX);
4608 if (kerr != KCGI_OK)
4610 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4612 if (kerr != KCGI_OK)
4614 kerr = khtml_puts(gw_trans->gw_html_req,
4615 gw_trans->gw_conf->got_site_link);
4616 if (kerr != KCGI_OK)
4618 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4619 if (kerr != KCGI_OK)
4622 if (gw_trans->repo_name != NULL) {
4623 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4624 if (kerr != KCGI_OK)
4627 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4628 gw_trans->repo_name, "action", "summary", NULL);
4629 if (href_summary == NULL) {
4630 error = got_error_from_errno("khttp_urlpart");
4633 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4634 href_summary, KATTR__MAX);
4635 if (kerr != KCGI_OK)
4637 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4638 if (kerr != KCGI_OK)
4640 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4641 if (kerr != KCGI_OK)
4643 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4644 gw_get_action_name(gw_trans));
4645 if (kerr != KCGI_OK)
4649 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4650 if (kerr != KCGI_OK)
4654 if (error == NULL && kerr != KCGI_OK)
4655 error = gw_kcgi_error(kerr);
4659 static const struct got_error *
4660 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4662 const struct got_error *error = NULL;
4664 enum kcgi_err kerr = KCGI_OK;
4666 if (strncmp(buf, "-", 1) == 0)
4667 color = "diff_minus";
4668 else if (strncmp(buf, "+", 1) == 0)
4669 color = "diff_plus";
4670 else if (strncmp(buf, "@@", 2) == 0)
4671 color = "diff_chunk_header";
4672 else if (strncmp(buf, "@@", 2) == 0)
4673 color = "diff_chunk_header";
4674 else if (strncmp(buf, "commit +", 8) == 0)
4675 color = "diff_meta";
4676 else if (strncmp(buf, "commit -", 8) == 0)
4677 color = "diff_meta";
4678 else if (strncmp(buf, "blob +", 6) == 0)
4679 color = "diff_meta";
4680 else if (strncmp(buf, "blob -", 6) == 0)
4681 color = "diff_meta";
4682 else if (strncmp(buf, "file +", 6) == 0)
4683 color = "diff_meta";
4684 else if (strncmp(buf, "file -", 6) == 0)
4685 color = "diff_meta";
4686 else if (strncmp(buf, "from:", 5) == 0)
4687 color = "diff_author";
4688 else if (strncmp(buf, "via:", 4) == 0)
4689 color = "diff_author";
4690 else if (strncmp(buf, "date:", 5) == 0)
4691 color = "diff_date";
4692 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4693 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4694 if (error == NULL && kerr != KCGI_OK)
4695 error = gw_kcgi_error(kerr);
4700 main(int argc, char *argv[])
4702 const struct got_error *error = NULL, *error2 = NULL;
4703 struct gw_trans *gw_trans;
4704 struct gw_dir *dir = NULL, *tdir;
4705 const char *page = "index";
4706 enum kcgi_err kerr = KCGI_OK;
4708 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4711 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4714 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4717 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4720 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4721 if (kerr != KCGI_OK) {
4722 error = gw_kcgi_error(kerr);
4726 TAILQ_INIT(&gw_trans->gw_dirs);
4727 TAILQ_INIT(&gw_trans->gw_headers);
4729 gw_trans->action = -1;
4731 gw_trans->repos_total = 0;
4732 gw_trans->repo_path = NULL;
4733 gw_trans->commit_id = NULL;
4734 gw_trans->next_id = NULL;
4735 gw_trans->next_prev_id = NULL;
4736 gw_trans->prev_id = NULL;
4737 gw_trans->prev_prev_id = NULL;
4738 gw_trans->headref = GOT_REF_HEAD;
4739 gw_trans->mime = KMIME_TEXT_HTML;
4740 gw_trans->gw_tmpl->key = gw_templs;
4741 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4742 gw_trans->gw_tmpl->arg = gw_trans;
4743 gw_trans->gw_tmpl->cb = gw_template;
4745 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4749 error = gw_parse_querystring(gw_trans);
4753 if (gw_trans->action == GW_BLOB)
4754 error = gw_blob(gw_trans);
4756 error = gw_display_index(gw_trans);
4758 if (gw_trans->repo) {
4759 const struct got_error *close_err;
4760 close_err = got_repo_close(gw_trans->repo);
4765 gw_trans->error = error;
4766 gw_trans->action = GW_ERR;
4767 error2 = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
4769 goto cleanup; /* we can't display an error page */
4770 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
4771 if (kerr != KCGI_OK)
4772 goto cleanup; /* we can't display an error page */
4773 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
4774 gw_query_funcs[gw_trans->action].template);
4775 if (kerr != KCGI_OK) {
4776 khtml_close(gw_trans->gw_html_req);
4777 goto cleanup; /* we can't display an error page */
4782 free(gw_trans->gw_conf->got_repos_path);
4783 free(gw_trans->gw_conf->got_www_path);
4784 free(gw_trans->gw_conf->got_site_name);
4785 free(gw_trans->gw_conf->got_site_owner);
4786 free(gw_trans->gw_conf->got_site_link);
4787 free(gw_trans->gw_conf->got_logo);
4788 free(gw_trans->gw_conf->got_logo_url);
4789 free(gw_trans->gw_conf);
4790 free(gw_trans->commit_id);
4791 free(gw_trans->next_id);
4792 free(gw_trans->next_prev_id);
4793 free(gw_trans->prev_id);
4794 free(gw_trans->prev_prev_id);
4795 free(gw_trans->repo_path);
4796 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4798 free(dir->description);
4805 khttp_free(gw_trans->gw_req);