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 *);
179 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
180 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
181 static const struct got_error *gw_output_diff(struct gw_trans *,
183 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
184 struct gw_header *, int, int);
185 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
186 static const struct got_error *gw_output_site_link(struct gw_trans *);
187 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
189 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
191 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
193 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
195 static const struct got_error *gw_gen_author_header(struct gw_trans *,
197 static const struct got_error *gw_gen_age_header(struct gw_trans *,
199 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
201 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
203 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
204 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
206 static const struct got_error *gw_display_index(struct gw_trans *);
207 static const struct got_error *gw_get_header(struct gw_trans *,
208 struct gw_header *, int);
209 static const struct got_error *gw_get_commits(struct gw_trans *,
210 struct gw_header *, int,
211 struct got_object_id *);
212 static const struct got_error *gw_get_commit(struct gw_trans *,
214 struct got_commit_object *,
215 struct got_object_id *);
216 static const struct got_error *gw_apply_unveil(const char *);
217 static const struct got_error *gw_blame_cb(void *, int, int,
218 struct got_object_id *);
219 static const struct got_error *gw_load_got_paths(struct gw_trans *);
220 static const struct got_error *gw_load_got_path(struct gw_trans *,
222 static const struct got_error *gw_parse_querystring(struct gw_trans *);
223 static const struct got_error *gw_blame(struct gw_trans *);
224 static const struct got_error *gw_blob(struct gw_trans *);
225 static const struct got_error *gw_diff(struct gw_trans *);
226 static const struct got_error *gw_index(struct gw_trans *);
227 static const struct got_error *gw_commits(struct gw_trans *);
228 static const struct got_error *gw_briefs(struct gw_trans *);
229 static const struct got_error *gw_summary(struct gw_trans *);
230 static const struct got_error *gw_tree(struct gw_trans *);
231 static const struct got_error *gw_tag(struct gw_trans *);
232 static const struct got_error *gw_tags(struct gw_trans *);
234 struct gw_query_action {
235 unsigned int func_id;
236 const char *func_name;
237 const struct got_error *(*func_main)(struct gw_trans *);
241 enum gw_query_actions {
255 static struct gw_query_action gw_query_funcs[] = {
256 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
257 { GW_BLOB, "blob", NULL, NULL },
258 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
259 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
260 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
261 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
262 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
263 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
264 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
265 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
266 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
270 gw_get_action_name(struct gw_trans *gw_trans)
272 return gw_query_funcs[gw_trans->action].func_name;
275 static const struct got_error *
276 gw_kcgi_error(enum kcgi_err kerr)
281 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
282 return got_error(GOT_ERR_CANCELLED);
284 if (kerr == KCGI_ENOMEM)
285 return got_error_set_errno(ENOMEM,
286 kcgi_strerror(kerr));
288 if (kerr == KCGI_ENFILE)
289 return got_error_set_errno(ENFILE,
290 kcgi_strerror(kerr));
292 if (kerr == KCGI_EAGAIN)
293 return got_error_set_errno(EAGAIN,
294 kcgi_strerror(kerr));
296 if (kerr == KCGI_FORM)
297 return got_error_msg(GOT_ERR_IO,
298 kcgi_strerror(kerr));
300 return got_error_from_errno(kcgi_strerror(kerr));
303 static const struct got_error *
304 gw_apply_unveil(const char *repo_path)
306 const struct got_error *err;
308 if (repo_path && unveil(repo_path, "r") != 0)
309 return got_error_from_errno2("unveil", repo_path);
311 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
312 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
314 err = got_privsep_unveil_exec_helpers();
318 if (unveil(NULL, NULL) != 0)
319 return got_error_from_errno("unveil");
325 isbinary(const uint8_t *buf, size_t n)
329 for (i = 0; i < n; i++)
335 static const struct got_error *
336 gw_blame(struct gw_trans *gw_trans)
338 const struct got_error *error = NULL;
339 struct gw_header *header = NULL;
341 enum kcgi_err kerr = KCGI_OK;
343 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
345 return got_error_from_errno("pledge");
347 if ((header = gw_init_header()) == NULL)
348 return got_error_from_errno("malloc");
350 error = gw_apply_unveil(gw_trans->gw_dir->path);
354 /* check querystring */
355 if (gw_trans->repo_file == NULL) {
356 error = got_error_msg(GOT_ERR_QUERYSTRING,
357 "file required in querystring");
360 if (gw_trans->commit_id == NULL) {
361 error = got_error_msg(GOT_ERR_QUERYSTRING,
362 "commit required in querystring");
366 error = gw_get_header(gw_trans, header, 1);
369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
370 "blame_header_wrapper", KATTR__MAX);
373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
374 "blame_header", KATTR__MAX);
377 error = gw_get_time_str(&age, header->committer_time,
381 error = gw_gen_age_header(gw_trans, age ?age : "");
384 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
387 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
390 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
391 "dotted_line", KATTR__MAX);
394 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
398 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
399 "blame", KATTR__MAX);
402 error = gw_output_file_blame(gw_trans);
405 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
407 gw_free_header(header);
408 if (error == NULL && kerr != KCGI_OK)
409 error = gw_kcgi_error(kerr);
413 static const struct got_error *
414 gw_blob(struct gw_trans *gw_trans)
416 const struct got_error *error = NULL, *err = NULL;
417 struct gw_header *header = NULL;
418 enum kcgi_err kerr = KCGI_OK;
420 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
422 return got_error_from_errno("pledge");
424 if ((header = gw_init_header()) == NULL)
425 return got_error_from_errno("malloc");
427 error = gw_apply_unveil(gw_trans->gw_dir->path);
431 /* check querystring */
432 if (gw_trans->repo_file == NULL) {
433 error = got_error_msg(GOT_ERR_QUERYSTRING,
434 "file required in querystring");
437 if (gw_trans->commit_id == NULL) {
438 error = got_error_msg(GOT_ERR_QUERYSTRING,
439 "commit required in querystring");
442 error = gw_get_header(gw_trans, header, 1);
446 error = gw_output_blob_buf(gw_trans);
450 gw_trans->mime = KMIME_TEXT_PLAIN;
451 err = gw_display_index(gw_trans);
456 kerr = khttp_puts(gw_trans->gw_req, error->msg);
459 gw_free_header(header);
460 if (error == NULL && kerr != KCGI_OK)
461 error = gw_kcgi_error(kerr);
465 static const struct got_error *
466 gw_diff(struct gw_trans *gw_trans)
468 const struct got_error *error = NULL;
469 struct gw_header *header = NULL;
471 enum kcgi_err kerr = KCGI_OK;
473 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
475 return got_error_from_errno("pledge");
477 if ((header = gw_init_header()) == NULL)
478 return got_error_from_errno("malloc");
480 error = gw_apply_unveil(gw_trans->gw_dir->path);
484 error = gw_get_header(gw_trans, header, 1);
488 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
489 "diff_header_wrapper", KATTR__MAX);
492 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
493 "diff_header", KATTR__MAX);
496 error = gw_gen_diff_header(gw_trans, header->parent_id,
500 error = gw_gen_commit_header(gw_trans, header->commit_id,
504 error = gw_gen_tree_header(gw_trans, header->tree_id);
507 error = gw_gen_author_header(gw_trans, header->author);
510 error = gw_gen_committer_header(gw_trans, header->author);
513 error = gw_get_time_str(&age, header->committer_time,
517 error = gw_gen_age_header(gw_trans, age ?age : "");
520 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
523 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
526 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
527 "dotted_line", KATTR__MAX);
530 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
534 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
538 error = gw_output_diff(gw_trans, header);
542 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
544 gw_free_header(header);
546 if (error == NULL && kerr != KCGI_OK)
547 error = gw_kcgi_error(kerr);
551 static const struct got_error *
552 gw_index(struct gw_trans *gw_trans)
554 const struct got_error *error = NULL;
555 struct gw_dir *gw_dir = NULL;
556 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
557 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
558 char *href_tags = NULL;
559 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
560 enum kcgi_err kerr = KCGI_OK;
562 if (pledge("stdio rpath proc exec sendfd unveil",
564 error = got_error_from_errno("pledge");
568 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
572 error = gw_load_got_paths(gw_trans);
576 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
577 "index_header", KATTR__MAX);
579 return gw_kcgi_error(kerr);
580 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
581 "index_header_project", KATTR__MAX);
583 return gw_kcgi_error(kerr);
584 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
586 return gw_kcgi_error(kerr);
587 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
589 return gw_kcgi_error(kerr);
591 if (gw_trans->gw_conf->got_show_repo_description) {
592 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
593 "index_header_description", KATTR__MAX);
595 return gw_kcgi_error(kerr);
596 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
598 return gw_kcgi_error(kerr);
599 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
601 return gw_kcgi_error(kerr);
604 if (gw_trans->gw_conf->got_show_repo_owner) {
605 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
606 "index_header_owner", KATTR__MAX);
608 return gw_kcgi_error(kerr);
609 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
611 return gw_kcgi_error(kerr);
612 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
614 return gw_kcgi_error(kerr);
617 if (gw_trans->gw_conf->got_show_repo_age) {
618 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
619 "index_header_age", KATTR__MAX);
621 return gw_kcgi_error(kerr);
622 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
624 return gw_kcgi_error(kerr);
625 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
627 return gw_kcgi_error(kerr);
630 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
632 return gw_kcgi_error(kerr);
634 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
635 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
636 "index_wrapper", KATTR__MAX);
638 return gw_kcgi_error(kerr);
639 kerr = khtml_printf(gw_trans->gw_html_req,
640 "No repositories found in %s",
641 gw_trans->gw_conf->got_repos_path);
643 return gw_kcgi_error(kerr);
644 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
646 return gw_kcgi_error(kerr);
647 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
648 "dotted_line", KATTR__MAX);
650 return gw_kcgi_error(kerr);
651 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
653 return gw_kcgi_error(kerr);
657 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
660 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
661 if (gw_trans->page > 0 && (gw_trans->page *
662 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
669 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
670 "index_wrapper", KATTR__MAX);
674 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
675 gw_dir->name, "action", "summary", NULL);
676 if (href_summary == NULL) {
677 error = got_error_from_errno("khttp_urlpart");
680 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
681 "index_project", KATTR__MAX);
684 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
685 href_summary, KATTR__MAX);
688 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
691 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
694 if (gw_trans->gw_conf->got_show_repo_description) {
695 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
696 KATTR_ID, "index_project_description", KATTR__MAX);
699 kerr = khtml_puts(gw_trans->gw_html_req,
700 gw_dir->description ? gw_dir->description : "");
703 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
707 if (gw_trans->gw_conf->got_show_repo_owner) {
708 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
709 KATTR_ID, "index_project_owner", KATTR__MAX);
712 kerr = khtml_puts(gw_trans->gw_html_req,
713 gw_dir->owner ? gw_dir->owner : "");
716 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
720 if (gw_trans->gw_conf->got_show_repo_age) {
721 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
722 KATTR_ID, "index_project_age", KATTR__MAX);
725 kerr = khtml_puts(gw_trans->gw_html_req,
726 gw_dir->age ? gw_dir->age : "");
729 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
734 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
735 "navs_wrapper", KATTR__MAX);
738 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
743 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
744 href_summary, KATTR__MAX);
747 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
750 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
754 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
758 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
759 gw_dir->name, "action", "briefs", NULL);
760 if (href_briefs == NULL) {
761 error = got_error_from_errno("khttp_urlpart");
764 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
765 href_briefs, KATTR__MAX);
768 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
771 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
773 error = gw_kcgi_error(kerr);
775 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
779 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
780 gw_dir->name, "action", "commits", NULL);
781 if (href_commits == NULL) {
782 error = got_error_from_errno("khttp_urlpart");
785 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
786 href_commits, KATTR__MAX);
789 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
792 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
796 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
800 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
801 gw_dir->name, "action", "tags", NULL);
802 if (href_tags == NULL) {
803 error = got_error_from_errno("khttp_urlpart");
806 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
807 href_tags, KATTR__MAX);
810 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
813 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
817 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
821 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
822 gw_dir->name, "action", "tree", NULL);
823 if (href_tree == NULL) {
824 error = got_error_from_errno("khttp_urlpart");
827 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
828 href_tree, KATTR__MAX);
831 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
835 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
839 "dotted_line", KATTR__MAX);
842 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
857 if (gw_trans->gw_conf->got_max_repos_display == 0)
860 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
861 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
862 (gw_trans->page > 0) &&
863 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
864 prev_disp == gw_trans->repos_total))) {
865 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
866 KATTR_ID, "np_wrapper", KATTR__MAX);
869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
870 KATTR_ID, "nav_prev", KATTR__MAX);
875 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
876 (gw_trans->page > 0) &&
877 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
878 prev_disp == gw_trans->repos_total)) {
879 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
880 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
881 if (href_prev == NULL) {
882 error = got_error_from_errno("khttp_urlpartx");
885 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
886 KATTR_HREF, href_prev, KATTR__MAX);
889 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
892 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
897 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
899 return gw_kcgi_error(kerr);
901 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
902 next_disp == gw_trans->gw_conf->got_max_repos_display &&
903 dir_c != (gw_trans->page + 1) *
904 gw_trans->gw_conf->got_max_repos_display) {
905 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
906 KATTR_ID, "nav_next", KATTR__MAX);
909 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
910 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
911 if (href_next == NULL) {
912 error = got_error_from_errno("khttp_urlpartx");
915 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
916 KATTR_HREF, href_next, KATTR__MAX);
919 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
922 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
929 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
930 (gw_trans->page > 0) &&
931 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
932 prev_disp == gw_trans->repos_total)) {
933 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
947 if (error == NULL && kerr != KCGI_OK)
948 error = gw_kcgi_error(kerr);
952 static const struct got_error *
953 gw_commits(struct gw_trans *gw_trans)
955 const struct got_error *error = NULL;
956 struct gw_header *header = NULL, *n_header = NULL;
957 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
958 char *href_prev = NULL, *href_next = NULL;
959 enum kcgi_err kerr = KCGI_OK;
961 if ((header = gw_init_header()) == NULL)
962 return got_error_from_errno("malloc");
964 if (pledge("stdio rpath proc exec sendfd unveil",
966 error = got_error_from_errno("pledge");
970 error = gw_apply_unveil(gw_trans->gw_dir->path);
974 error = gw_get_header(gw_trans, header,
975 gw_trans->gw_conf->got_max_commits_display);
979 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
980 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
981 "commits_line_wrapper", KATTR__MAX);
984 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
988 error = gw_gen_author_header(gw_trans, n_header->author);
991 error = gw_gen_committer_header(gw_trans, n_header->author);
994 error = gw_get_time_str(&age, n_header->committer_time,
998 error = gw_gen_age_header(gw_trans, age ?age : "");
1001 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1002 if (kerr != KCGI_OK)
1005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1006 "dotted_line", KATTR__MAX);
1007 if (kerr != KCGI_OK)
1009 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1010 if (kerr != KCGI_OK)
1013 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1014 "commit", KATTR__MAX);
1015 if (kerr != KCGI_OK)
1017 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1018 if (kerr != KCGI_OK)
1020 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1021 if (kerr != KCGI_OK)
1024 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1025 gw_trans->repo_name, "action", "diff", "commit",
1026 n_header->commit_id, NULL);
1027 if (href_diff == NULL) {
1028 error = got_error_from_errno("khttp_urlpart");
1031 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1032 KATTR_ID, "navs_wrapper", KATTR__MAX);
1033 if (kerr != KCGI_OK)
1035 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1036 KATTR_ID, "navs", KATTR__MAX);
1037 if (kerr != KCGI_OK)
1039 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1040 KATTR_HREF, href_diff, KATTR__MAX);
1041 if (kerr != KCGI_OK)
1043 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1044 if (kerr != KCGI_OK)
1046 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1047 if (kerr != KCGI_OK)
1050 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1051 if (kerr != KCGI_OK)
1054 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1055 gw_trans->repo_name, "action", "tree", "commit",
1056 n_header->commit_id, NULL);
1057 if (href_tree == NULL) {
1058 error = got_error_from_errno("khttp_urlpart");
1061 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1062 KATTR_HREF, href_tree, KATTR__MAX);
1063 if (kerr != KCGI_OK)
1065 khtml_puts(gw_trans->gw_html_req, "tree");
1066 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1067 if (kerr != KCGI_OK)
1069 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1070 if (kerr != KCGI_OK)
1073 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1074 "solid_line", KATTR__MAX);
1075 if (kerr != KCGI_OK)
1077 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1078 if (kerr != KCGI_OK)
1085 if (gw_trans->next_id || gw_trans->page > 0) {
1086 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1087 KATTR_ID, "np_wrapper", KATTR__MAX);
1088 if (kerr != KCGI_OK)
1090 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1091 KATTR_ID, "nav_prev", KATTR__MAX);
1092 if (kerr != KCGI_OK)
1096 if (gw_trans->page > 0 && gw_trans->prev_id) {
1097 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1098 KATTRX_STRING, gw_trans->repo_name, "page",
1099 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1100 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1101 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1102 KATTRX_STRING, gw_trans->prev_prev_id ?
1103 gw_trans->prev_prev_id : "", NULL);
1104 if (href_prev == NULL) {
1105 error = got_error_from_errno("khttp_urlpartx");
1108 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1109 KATTR_HREF, href_prev, KATTR__MAX);
1110 if (kerr != KCGI_OK)
1112 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1113 if (kerr != KCGI_OK)
1115 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1116 if (kerr != KCGI_OK)
1120 if (gw_trans->next_id || gw_trans->page > 0) {
1121 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1122 if (kerr != KCGI_OK)
1123 return gw_kcgi_error(kerr);
1126 if (gw_trans->next_id) {
1127 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1128 KATTR_ID, "nav_next", KATTR__MAX);
1129 if (kerr != KCGI_OK)
1131 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1132 KATTRX_STRING, gw_trans->repo_name, "page",
1133 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1134 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1135 gw_trans->next_id, "prev", KATTRX_STRING,
1136 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1137 "prev_prev", KATTRX_STRING, gw_trans->prev_prev_id ?
1138 gw_trans->prev_prev_id : "", NULL);
1139 if (href_next == NULL) {
1140 error = got_error_from_errno("khttp_urlpartx");
1143 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1144 KATTR_HREF, href_next, KATTR__MAX);
1145 if (kerr != KCGI_OK)
1147 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1148 if (kerr != KCGI_OK)
1150 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1151 if (kerr != KCGI_OK)
1155 if (gw_trans->next_id || gw_trans->page > 0) {
1156 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1157 if (kerr != KCGI_OK)
1161 gw_free_header(header);
1162 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1163 gw_free_header(n_header);
1169 if (error == NULL && kerr != KCGI_OK)
1170 error = gw_kcgi_error(kerr);
1174 static const struct got_error *
1175 gw_briefs(struct gw_trans *gw_trans)
1177 const struct got_error *error = NULL;
1178 struct gw_header *header = NULL, *n_header = NULL;
1179 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1180 char *href_prev = NULL, *href_next = NULL;
1181 char *newline, *smallerthan;
1182 enum kcgi_err kerr = KCGI_OK;
1184 if ((header = gw_init_header()) == NULL)
1185 return got_error_from_errno("malloc");
1187 if (pledge("stdio rpath proc exec sendfd unveil",
1189 error = got_error_from_errno("pledge");
1193 if (gw_trans->action != GW_SUMMARY) {
1194 error = gw_apply_unveil(gw_trans->gw_dir->path);
1199 if (gw_trans->action == GW_SUMMARY)
1200 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1202 error = gw_get_header(gw_trans, header,
1203 gw_trans->gw_conf->got_max_commits_display);
1207 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1208 error = gw_get_time_str(&age, n_header->committer_time,
1213 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1214 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1215 if (kerr != KCGI_OK)
1218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1219 KATTR_ID, "briefs_age", KATTR__MAX);
1220 if (kerr != KCGI_OK)
1222 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1223 if (kerr != KCGI_OK)
1225 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1226 if (kerr != KCGI_OK)
1229 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1230 KATTR_ID, "briefs_author", KATTR__MAX);
1231 if (kerr != KCGI_OK)
1233 smallerthan = strchr(n_header->author, '<');
1235 *smallerthan = '\0';
1236 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1237 if (kerr != KCGI_OK)
1239 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1240 if (kerr != KCGI_OK)
1243 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1244 gw_trans->repo_name, "action", "diff", "commit",
1245 n_header->commit_id, NULL);
1246 if (href_diff == NULL) {
1247 error = got_error_from_errno("khttp_urlpart");
1250 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1251 KATTR_ID, "briefs_log", KATTR__MAX);
1252 if (kerr != KCGI_OK)
1254 newline = strchr(n_header->commit_msg, '\n');
1257 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1258 KATTR_HREF, href_diff, KATTR__MAX);
1259 if (kerr != KCGI_OK)
1261 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1262 if (kerr != KCGI_OK)
1264 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1265 if (kerr != KCGI_OK)
1268 if (n_header->refs_str) {
1269 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1270 if (kerr != KCGI_OK)
1272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1273 KATTR_ID, "refs_str", KATTR__MAX);
1274 if (kerr != KCGI_OK)
1276 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1277 n_header->refs_str);
1278 if (kerr != KCGI_OK)
1280 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1281 if (kerr != KCGI_OK)
1285 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1286 if (kerr != KCGI_OK)
1289 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1290 KATTR_ID, "navs_wrapper", KATTR__MAX);
1291 if (kerr != KCGI_OK)
1293 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1294 KATTR_ID, "navs", KATTR__MAX);
1295 if (kerr != KCGI_OK)
1297 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1298 KATTR_HREF, href_diff, KATTR__MAX);
1299 if (kerr != KCGI_OK)
1301 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1302 if (kerr != KCGI_OK)
1304 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1305 if (kerr != KCGI_OK)
1308 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1309 if (kerr != KCGI_OK)
1312 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1313 gw_trans->repo_name, "action", "tree", "commit",
1314 n_header->commit_id, NULL);
1315 if (href_tree == NULL) {
1316 error = got_error_from_errno("khttp_urlpart");
1319 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1320 KATTR_HREF, href_tree, KATTR__MAX);
1321 if (kerr != KCGI_OK)
1323 khtml_puts(gw_trans->gw_html_req, "tree");
1324 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1325 if (kerr != KCGI_OK)
1327 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1328 if (kerr != KCGI_OK)
1331 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1332 KATTR_ID, "dotted_line", KATTR__MAX);
1333 if (kerr != KCGI_OK)
1335 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1336 if (kerr != KCGI_OK)
1347 if (gw_trans->next_id || gw_trans->page > 0) {
1348 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1349 KATTR_ID, "np_wrapper", KATTR__MAX);
1350 if (kerr != KCGI_OK)
1352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1353 KATTR_ID, "nav_prev", KATTR__MAX);
1354 if (kerr != KCGI_OK)
1358 if (gw_trans->page > 0 && gw_trans->prev_id) {
1359 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1360 KATTRX_STRING, gw_trans->repo_name, "page",
1361 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1362 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1363 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1364 KATTRX_STRING, gw_trans->prev_prev_id ?
1365 gw_trans->prev_prev_id : "", NULL);
1366 if (href_prev == NULL) {
1367 error = got_error_from_errno("khttp_urlpartx");
1370 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1371 KATTR_HREF, href_prev, KATTR__MAX);
1372 if (kerr != KCGI_OK)
1374 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1375 if (kerr != KCGI_OK)
1377 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1378 if (kerr != KCGI_OK)
1382 if (gw_trans->next_id || gw_trans->page > 0) {
1383 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1384 if (kerr != KCGI_OK)
1385 return gw_kcgi_error(kerr);
1388 if (gw_trans->next_id) {
1389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1390 KATTR_ID, "nav_next", KATTR__MAX);
1391 if (kerr != KCGI_OK)
1394 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1395 KATTRX_STRING, gw_trans->repo_name, "page",
1396 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1397 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1398 gw_trans->next_id, "prev", KATTRX_STRING,
1399 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1400 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1401 gw_trans->prev_id : "", NULL);
1402 if (href_next == NULL) {
1403 error = got_error_from_errno("khttp_urlpartx");
1406 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1407 KATTR_HREF, href_next, KATTR__MAX);
1408 if (kerr != KCGI_OK)
1410 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1411 if (kerr != KCGI_OK)
1413 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1414 if (kerr != KCGI_OK)
1418 if (gw_trans->next_id || gw_trans->page > 0) {
1419 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1420 if (kerr != KCGI_OK)
1424 gw_free_header(header);
1425 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1426 gw_free_header(n_header);
1432 if (error == NULL && kerr != KCGI_OK)
1433 error = gw_kcgi_error(kerr);
1437 static const struct got_error *
1438 gw_summary(struct gw_trans *gw_trans)
1440 const struct got_error *error = NULL;
1442 enum kcgi_err kerr = KCGI_OK;
1444 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1445 return got_error_from_errno("pledge");
1447 error = gw_apply_unveil(gw_trans->gw_dir->path);
1451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1452 "summary_wrapper", KATTR__MAX);
1453 if (kerr != KCGI_OK)
1454 return gw_kcgi_error(kerr);
1456 if (gw_trans->gw_conf->got_show_repo_description &&
1457 gw_trans->gw_dir->description != NULL &&
1458 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1459 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1460 KATTR_ID, "description_title", KATTR__MAX);
1461 if (kerr != KCGI_OK)
1463 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1464 if (kerr != KCGI_OK)
1466 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1467 if (kerr != KCGI_OK)
1469 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1470 KATTR_ID, "description", KATTR__MAX);
1471 if (kerr != KCGI_OK)
1473 kerr = khtml_puts(gw_trans->gw_html_req,
1474 gw_trans->gw_dir->description);
1475 if (kerr != KCGI_OK)
1477 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1478 if (kerr != KCGI_OK)
1482 if (gw_trans->gw_conf->got_show_repo_owner &&
1483 gw_trans->gw_dir->owner != NULL &&
1484 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1485 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1486 KATTR_ID, "repo_owner_title", KATTR__MAX);
1487 if (kerr != KCGI_OK)
1489 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1490 if (kerr != KCGI_OK)
1492 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1493 if (kerr != KCGI_OK)
1495 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1496 KATTR_ID, "repo_owner", KATTR__MAX);
1497 if (kerr != KCGI_OK)
1499 kerr = khtml_puts(gw_trans->gw_html_req,
1500 gw_trans->gw_dir->owner);
1501 if (kerr != KCGI_OK)
1503 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1504 if (kerr != KCGI_OK)
1508 if (gw_trans->gw_conf->got_show_repo_age) {
1509 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1515 KATTR_ID, "last_change_title", KATTR__MAX);
1516 if (kerr != KCGI_OK)
1518 kerr = khtml_puts(gw_trans->gw_html_req,
1520 if (kerr != KCGI_OK)
1522 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1523 if (kerr != KCGI_OK)
1525 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1526 KATTR_ID, "last_change", KATTR__MAX);
1527 if (kerr != KCGI_OK)
1529 kerr = khtml_puts(gw_trans->gw_html_req, age);
1530 if (kerr != KCGI_OK)
1532 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1533 if (kerr != KCGI_OK)
1538 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1539 gw_trans->gw_dir->url != NULL &&
1540 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1542 KATTR_ID, "cloneurl_title", KATTR__MAX);
1543 if (kerr != KCGI_OK)
1545 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1546 if (kerr != KCGI_OK)
1548 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1549 if (kerr != KCGI_OK)
1551 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1552 KATTR_ID, "cloneurl", KATTR__MAX);
1553 if (kerr != KCGI_OK)
1555 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1556 if (kerr != KCGI_OK)
1558 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1559 if (kerr != KCGI_OK)
1563 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1564 if (kerr != KCGI_OK)
1567 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1568 "briefs_title_wrapper", KATTR__MAX);
1569 if (kerr != KCGI_OK)
1571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1572 "briefs_title", KATTR__MAX);
1573 if (kerr != KCGI_OK)
1575 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1576 if (kerr != KCGI_OK)
1578 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1579 if (kerr != KCGI_OK)
1581 error = gw_briefs(gw_trans);
1585 error = gw_tags(gw_trans);
1589 error = gw_output_repo_heads(gw_trans);
1592 if (error == NULL && kerr != KCGI_OK)
1593 error = gw_kcgi_error(kerr);
1597 static const struct got_error *
1598 gw_tree(struct gw_trans *gw_trans)
1600 const struct got_error *error = NULL;
1601 struct gw_header *header = NULL;
1602 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1604 enum kcgi_err kerr = KCGI_OK;
1606 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1607 return got_error_from_errno("pledge");
1609 if ((header = gw_init_header()) == NULL)
1610 return got_error_from_errno("malloc");
1612 error = gw_apply_unveil(gw_trans->gw_dir->path);
1616 error = gw_get_header(gw_trans, header, 1);
1620 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1621 "tree_header_wrapper", KATTR__MAX);
1622 if (kerr != KCGI_OK)
1624 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1625 "tree_header", KATTR__MAX);
1626 if (kerr != KCGI_OK)
1628 error = gw_gen_tree_header(gw_trans, header->tree_id);
1631 error = gw_get_time_str(&age, header->committer_time,
1635 error = gw_gen_age_header(gw_trans, age ?age : "");
1638 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1641 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1642 if (kerr != KCGI_OK)
1644 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1645 "dotted_line", KATTR__MAX);
1646 if (kerr != KCGI_OK)
1648 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1649 if (kerr != KCGI_OK)
1652 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1653 "tree", KATTR__MAX);
1654 if (kerr != KCGI_OK)
1656 error = gw_output_repo_tree(gw_trans);
1660 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1662 gw_free_header(header);
1663 free(tree_html_disp);
1667 if (error == NULL && kerr != KCGI_OK)
1668 error = gw_kcgi_error(kerr);
1672 static const struct got_error *
1673 gw_tags(struct gw_trans *gw_trans)
1675 const struct got_error *error = NULL;
1676 struct gw_header *header = NULL;
1677 char *href_next = NULL, *href_prev = NULL;
1678 enum kcgi_err kerr = KCGI_OK;
1680 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1681 return got_error_from_errno("pledge");
1683 if ((header = gw_init_header()) == NULL)
1684 return got_error_from_errno("malloc");
1686 if (gw_trans->action != GW_SUMMARY) {
1687 error = gw_apply_unveil(gw_trans->gw_dir->path);
1692 error = gw_get_header(gw_trans, header, 1);
1696 if (gw_trans->action == GW_SUMMARY) {
1697 gw_trans->next_id = NULL;
1698 error = gw_output_repo_tags(gw_trans, header,
1699 D_MAXSLCOMMDISP, TAGBRIEF);
1703 error = gw_output_repo_tags(gw_trans, header,
1704 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1709 if (gw_trans->next_id || gw_trans->page > 0) {
1710 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1711 KATTR_ID, "np_wrapper", KATTR__MAX);
1712 if (kerr != KCGI_OK)
1714 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1715 KATTR_ID, "nav_prev", KATTR__MAX);
1716 if (kerr != KCGI_OK)
1720 if (gw_trans->page > 0 && gw_trans->prev_id) {
1721 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1722 KATTRX_STRING, gw_trans->repo_name, "page",
1723 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1724 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1725 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1726 KATTRX_STRING, gw_trans->prev_prev_id ?
1727 gw_trans->prev_prev_id : "", NULL);
1728 if (href_prev == NULL) {
1729 error = got_error_from_errno("khttp_urlpartx");
1732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1733 KATTR_HREF, href_prev, KATTR__MAX);
1734 if (kerr != KCGI_OK)
1736 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1737 if (kerr != KCGI_OK)
1739 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1740 if (kerr != KCGI_OK)
1744 if (gw_trans->next_id || gw_trans->page > 0) {
1745 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1746 if (kerr != KCGI_OK)
1747 return gw_kcgi_error(kerr);
1750 if (gw_trans->next_id) {
1751 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1752 KATTR_ID, "nav_next", KATTR__MAX);
1753 if (kerr != KCGI_OK)
1755 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1756 KATTRX_STRING, gw_trans->repo_name, "page",
1757 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1758 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1759 gw_trans->next_id, "prev", KATTRX_STRING,
1760 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1761 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1762 gw_trans->prev_id : "", NULL);
1763 if (href_next == NULL) {
1764 error = got_error_from_errno("khttp_urlpartx");
1767 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1768 KATTR_HREF, href_next, KATTR__MAX);
1769 if (kerr != KCGI_OK)
1771 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1772 if (kerr != KCGI_OK)
1774 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1775 if (kerr != KCGI_OK)
1779 if (gw_trans->next_id || gw_trans->page > 0) {
1780 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1781 if (kerr != KCGI_OK)
1785 gw_free_header(header);
1788 if (error == NULL && kerr != KCGI_OK)
1789 error = gw_kcgi_error(kerr);
1793 static const struct got_error *
1794 gw_tag(struct gw_trans *gw_trans)
1796 const struct got_error *error = NULL;
1797 struct gw_header *header = NULL;
1798 enum kcgi_err kerr = KCGI_OK;
1800 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1801 return got_error_from_errno("pledge");
1803 if ((header = gw_init_header()) == NULL)
1804 return got_error_from_errno("malloc");
1806 error = gw_apply_unveil(gw_trans->gw_dir->path);
1810 if (gw_trans->commit_id == NULL) {
1811 error = got_error_msg(GOT_ERR_QUERYSTRING,
1812 "commit required in querystring");
1816 error = gw_get_header(gw_trans, header, 1);
1820 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1821 "tag_header_wrapper", KATTR__MAX);
1822 if (kerr != KCGI_OK)
1824 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1825 "tag_header", KATTR__MAX);
1826 if (kerr != KCGI_OK)
1828 error = gw_gen_commit_header(gw_trans, header->commit_id,
1832 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1835 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1836 if (kerr != KCGI_OK)
1838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1839 "dotted_line", KATTR__MAX);
1840 if (kerr != KCGI_OK)
1842 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1843 if (kerr != KCGI_OK)
1846 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1847 "tree", KATTR__MAX);
1848 if (kerr != KCGI_OK)
1851 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1855 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1857 gw_free_header(header);
1858 if (error == NULL && kerr != KCGI_OK)
1859 error = gw_kcgi_error(kerr);
1863 static const struct got_error *
1864 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1866 const struct got_error *error = NULL;
1871 if (asprintf(&dir_test, "%s/%s/%s",
1872 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1873 GOTWEB_GIT_DIR) == -1)
1874 return got_error_from_errno("asprintf");
1876 dt = opendir(dir_test);
1880 gw_dir->path = strdup(dir_test);
1881 if (gw_dir->path == NULL) {
1883 error = got_error_from_errno("strdup");
1890 if (asprintf(&dir_test, "%s/%s/%s",
1891 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1892 GOTWEB_GOT_DIR) == -1) {
1894 error = got_error_from_errno("asprintf");
1898 dt = opendir(dir_test);
1903 error = got_error(GOT_ERR_NOT_GIT_REPO);
1907 if (asprintf(&dir_test, "%s/%s",
1908 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1909 error = got_error_from_errno("asprintf");
1914 gw_dir->path = strdup(dir_test);
1915 if (gw_dir->path == NULL) {
1917 error = got_error_from_errno("strdup");
1921 dt = opendir(dir_test);
1923 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1928 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1932 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1935 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1939 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1943 if (dt && closedir(dt) == -1 && error == NULL)
1944 error = got_error_from_errno("closedir");
1948 static const struct got_error *
1949 gw_load_got_paths(struct gw_trans *gw_trans)
1951 const struct got_error *error = NULL;
1953 struct dirent **sd_dent;
1954 struct gw_dir *gw_dir;
1956 unsigned int d_cnt, d_i;
1958 d = opendir(gw_trans->gw_conf->got_repos_path);
1960 error = got_error_from_errno2("opendir",
1961 gw_trans->gw_conf->got_repos_path);
1965 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1968 error = got_error_from_errno2("scandir",
1969 gw_trans->gw_conf->got_repos_path);
1973 for (d_i = 0; d_i < d_cnt; d_i++) {
1974 if (gw_trans->gw_conf->got_max_repos > 0 &&
1975 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1976 break; /* account for parent and self */
1978 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1979 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1982 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1986 error = gw_load_got_path(gw_trans, gw_dir);
1987 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1994 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1995 !got_path_dir_is_empty(gw_dir->path)) {
1996 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1998 gw_trans->repos_total++;
2002 if (d && closedir(d) == -1 && error == NULL)
2003 error = got_error_from_errno("closedir");
2007 static const struct got_error *
2008 gw_parse_querystring(struct gw_trans *gw_trans)
2010 const struct got_error *error = NULL;
2012 struct gw_query_action *action = NULL;
2015 if (gw_trans->gw_req->fieldnmap[0]) {
2016 return got_error(GOT_ERR_QUERYSTRING);
2017 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2018 /* define gw_trans->repo_path */
2019 gw_trans->repo_name = p->parsed.s;
2021 if (asprintf(&gw_trans->repo_path, "%s/%s",
2022 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2023 return got_error_from_errno("asprintf");
2025 /* get action and set function */
2026 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2027 for (i = 0; i < nitems(gw_query_funcs); i++) {
2028 action = &gw_query_funcs[i];
2029 if (action->func_name == NULL)
2031 if (strcmp(action->func_name,
2032 p->parsed.s) == 0) {
2033 gw_trans->action = i;
2038 if (gw_trans->action == -1) {
2039 gw_trans->action = GW_ERR;
2040 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2041 p != NULL ? "bad action in querystring" :
2042 "no action in querystring");
2046 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2047 if (asprintf(&gw_trans->commit_id, "%s",
2049 return got_error_from_errno("asprintf");
2052 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2053 gw_trans->repo_file = p->parsed.s;
2055 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2056 if (asprintf(&gw_trans->repo_folder, "%s",
2058 return got_error_from_errno("asprintf");
2061 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2062 if (asprintf(&gw_trans->prev_id, "%s",
2064 return got_error_from_errno("asprintf");
2067 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
2068 if (asprintf(&gw_trans->prev_prev_id, "%s",
2070 return got_error_from_errno("asprintf");
2073 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2074 gw_trans->headref = p->parsed.s;
2076 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2080 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2082 gw_trans->action = GW_INDEX;
2084 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2085 gw_trans->page = p->parsed.i;
2090 static const struct got_error *
2091 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2093 const struct got_error *error;
2095 *gw_dir = malloc(sizeof(**gw_dir));
2096 if (*gw_dir == NULL)
2097 return got_error_from_errno("malloc");
2099 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2100 error = got_error_from_errno("asprintf");
2109 static const struct got_error *
2110 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2112 enum kcgi_err kerr = KCGI_OK;
2114 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2115 if (kerr != KCGI_OK)
2116 return gw_kcgi_error(kerr);
2117 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2119 if (kerr != KCGI_OK)
2120 return gw_kcgi_error(kerr);
2121 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2123 if (kerr != KCGI_OK)
2124 return gw_kcgi_error(kerr);
2125 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2127 if (kerr != KCGI_OK)
2128 return gw_kcgi_error(kerr);
2129 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2130 if (kerr != KCGI_OK)
2131 return gw_kcgi_error(kerr);
2132 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2134 if (kerr != KCGI_OK)
2135 return gw_kcgi_error(kerr);
2137 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2138 kerr = khttp_head(gw_trans->gw_req,
2139 kresps[KRESP_CONTENT_DISPOSITION],
2140 "attachment; filename=%s", gw_trans->repo_file);
2141 if (kerr != KCGI_OK)
2142 return gw_kcgi_error(kerr);
2145 kerr = khttp_body(gw_trans->gw_req);
2146 return gw_kcgi_error(kerr);
2149 static const struct got_error *
2150 gw_display_index(struct gw_trans *gw_trans)
2152 const struct got_error *error;
2153 enum kcgi_err kerr = KCGI_OK;
2155 /* catch early querystring errors */
2156 if (gw_trans->error)
2157 gw_trans->action = GW_ERR;
2159 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2163 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2164 if (kerr != KCGI_OK)
2165 return gw_kcgi_error(kerr);
2167 if (gw_trans->action != GW_BLOB) {
2168 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2169 gw_query_funcs[gw_trans->action].template);
2170 if (kerr != KCGI_OK) {
2171 khtml_close(gw_trans->gw_html_req);
2172 return gw_kcgi_error(kerr);
2176 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2179 static const struct got_error *
2180 gw_error(struct gw_trans *gw_trans)
2182 enum kcgi_err kerr = KCGI_OK;
2184 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2186 return gw_kcgi_error(kerr);
2190 gw_template(size_t key, void *arg)
2192 const struct got_error *error = NULL;
2193 enum kcgi_err kerr = KCGI_OK;
2194 struct gw_trans *gw_trans = arg;
2195 char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
2196 char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
2198 if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
2199 "/apple-touch-icon.png") == -1)
2201 if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
2202 "/favicon-32x32.png") == -1)
2204 if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
2205 "/favicon-16x16.png") == -1)
2207 if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
2208 "/site.webmanifest") == -1)
2210 if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
2211 "/safari-pinned-tab.svg") == -1)
2213 if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
2214 "/gotweb.css") == -1)
2216 if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
2217 gw_trans->gw_conf->got_www_path ? "/" : "",
2218 gw_trans->gw_conf->got_logo) == -1)
2223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2224 KATTR_NAME, "viewport",
2225 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2227 if (kerr != KCGI_OK)
2229 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2230 if (kerr != KCGI_OK)
2232 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2233 KATTR_CHARSET, "utf-8",
2235 if (kerr != KCGI_OK)
2237 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2238 if (kerr != KCGI_OK)
2240 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2241 KATTR_NAME, "msapplication-TileColor",
2242 KATTR_CONTENT, "#da532c", KATTR__MAX);
2243 if (kerr != KCGI_OK)
2245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2246 if (kerr != KCGI_OK)
2248 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2249 KATTR_NAME, "theme-color",
2250 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2251 if (kerr != KCGI_OK)
2253 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2254 if (kerr != KCGI_OK)
2256 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2257 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2258 KATTR_HREF, ati, KATTR__MAX);
2259 if (kerr != KCGI_OK)
2261 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2262 if (kerr != KCGI_OK)
2264 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2265 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2266 "32x32", KATTR_HREF, fic32, KATTR__MAX);
2267 if (kerr != KCGI_OK)
2269 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2270 if (kerr != KCGI_OK)
2272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2273 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2274 "16x16", KATTR_HREF, fic16, KATTR__MAX);
2275 if (kerr != KCGI_OK)
2277 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2278 if (kerr != KCGI_OK)
2280 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2281 KATTR_REL, "manifest", KATTR_HREF, swm,
2283 if (kerr != KCGI_OK)
2285 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2286 if (kerr != KCGI_OK)
2288 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2289 KATTR_REL, "mask-icon", KATTR_HREF,
2291 if (kerr != KCGI_OK)
2293 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2294 if (kerr != KCGI_OK)
2296 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2297 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2298 KATTR_HREF, css, KATTR__MAX);
2299 if (kerr != KCGI_OK)
2301 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2302 if (kerr != KCGI_OK)
2306 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2307 KATTR_ID, "got_link", KATTR__MAX);
2308 if (kerr != KCGI_OK)
2310 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2311 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2312 KATTR_TARGET, "_sotd", KATTR__MAX);
2313 if (kerr != KCGI_OK)
2315 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2316 KATTR_SRC, logo, KATTR__MAX);
2317 if (kerr != KCGI_OK)
2319 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2320 if (kerr != KCGI_OK)
2323 case (TEMPL_SITEPATH):
2324 error = gw_output_site_link(gw_trans);
2329 if (gw_trans->gw_conf->got_site_name != NULL) {
2330 kerr = khtml_puts(gw_trans->gw_html_req,
2331 gw_trans->gw_conf->got_site_name);
2332 if (kerr != KCGI_OK)
2336 case (TEMPL_SEARCH):
2338 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2339 "search", KATTR__MAX);
2340 if (kerr != KCGI_OK)
2342 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2343 KATTR_METHOD, "POST", KATTR__MAX);
2344 if (kerr != KCGI_OK)
2346 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2347 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2348 KATTR_MAXLENGTH, "50", KATTR__MAX);
2349 if (kerr != KCGI_OK)
2351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2353 if (kerr != KCGI_OK)
2355 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2356 if (kerr != KCGI_OK)
2358 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2359 if (kerr != KCGI_OK)
2362 case(TEMPL_SITEOWNER):
2363 if (gw_trans->gw_conf->got_site_owner != NULL &&
2364 gw_trans->gw_conf->got_show_site_owner) {
2365 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2366 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2367 if (kerr != KCGI_OK)
2369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2370 KATTR_ID, "site_owner", KATTR__MAX);
2371 if (kerr != KCGI_OK)
2373 kerr = khtml_puts(gw_trans->gw_html_req,
2374 gw_trans->gw_conf->got_site_owner);
2375 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2376 if (kerr != KCGI_OK)
2380 case(TEMPL_CONTENT):
2381 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2383 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2384 KATTR_ID, "tmpl_err", KATTR__MAX);
2385 if (kerr != KCGI_OK)
2387 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2389 if (kerr != KCGI_OK)
2391 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2392 if (kerr != KCGI_OK)
2418 static const struct got_error *
2419 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2421 const struct got_error *error = NULL;
2422 enum kcgi_err kerr = KCGI_OK;
2424 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2425 KATTR_ID, "header_commit_title", KATTR__MAX);
2426 if (kerr != KCGI_OK)
2428 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2429 if (kerr != KCGI_OK)
2431 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2432 if (kerr != KCGI_OK)
2434 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2435 KATTR_ID, "header_commit", KATTR__MAX);
2436 if (kerr != KCGI_OK)
2438 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2439 if (kerr != KCGI_OK)
2442 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2443 KATTR_ID, "refs_str", KATTR__MAX);
2444 if (kerr != KCGI_OK)
2446 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2447 if (kerr != KCGI_OK)
2449 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2450 if (kerr != KCGI_OK)
2453 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2455 if (error == NULL && kerr != KCGI_OK)
2456 error = gw_kcgi_error(kerr);
2460 static const struct got_error *
2461 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2463 const struct got_error *error = NULL;
2464 enum kcgi_err kerr = KCGI_OK;
2466 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2467 KATTR_ID, "header_diff_title", KATTR__MAX);
2468 if (kerr != KCGI_OK)
2470 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2471 if (kerr != KCGI_OK)
2473 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2474 if (kerr != KCGI_OK)
2476 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2477 KATTR_ID, "header_diff", KATTR__MAX);
2478 if (kerr != KCGI_OK)
2481 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2482 if (kerr != KCGI_OK)
2485 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2486 if (kerr != KCGI_OK)
2488 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2489 if (kerr != KCGI_OK)
2491 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2493 if (error == NULL && kerr != KCGI_OK)
2494 error = gw_kcgi_error(kerr);
2498 static const struct got_error *
2499 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2501 const struct got_error *error = NULL;
2502 enum kcgi_err kerr = KCGI_OK;
2504 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2505 KATTR_ID, "header_age_title", KATTR__MAX);
2506 if (kerr != KCGI_OK)
2508 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2509 if (kerr != KCGI_OK)
2511 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2512 if (kerr != KCGI_OK)
2514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2515 KATTR_ID, "header_age", KATTR__MAX);
2516 if (kerr != KCGI_OK)
2518 kerr = khtml_puts(gw_trans->gw_html_req, str);
2519 if (kerr != KCGI_OK)
2521 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2523 if (error == NULL && kerr != KCGI_OK)
2524 error = gw_kcgi_error(kerr);
2528 static const struct got_error *
2529 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2531 const struct got_error *error = NULL;
2532 enum kcgi_err kerr = KCGI_OK;
2534 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2535 KATTR_ID, "header_author_title", KATTR__MAX);
2536 if (kerr != KCGI_OK)
2538 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2539 if (kerr != KCGI_OK)
2541 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2542 if (kerr != KCGI_OK)
2544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2545 KATTR_ID, "header_author", KATTR__MAX);
2546 if (kerr != KCGI_OK)
2548 kerr = khtml_puts(gw_trans->gw_html_req, str);
2549 if (kerr != KCGI_OK)
2551 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2553 if (error == NULL && kerr != KCGI_OK)
2554 error = gw_kcgi_error(kerr);
2558 static const struct got_error *
2559 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2561 const struct got_error *error = NULL;
2562 enum kcgi_err kerr = KCGI_OK;
2564 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2565 KATTR_ID, "header_committer_title", KATTR__MAX);
2566 if (kerr != KCGI_OK)
2568 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2569 if (kerr != KCGI_OK)
2571 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2572 if (kerr != KCGI_OK)
2574 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2575 KATTR_ID, "header_committer", KATTR__MAX);
2576 if (kerr != KCGI_OK)
2578 kerr = khtml_puts(gw_trans->gw_html_req, str);
2579 if (kerr != KCGI_OK)
2581 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2583 if (error == NULL && kerr != KCGI_OK)
2584 error = gw_kcgi_error(kerr);
2588 static const struct got_error *
2589 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2591 const struct got_error *error = NULL;
2592 enum kcgi_err kerr = KCGI_OK;
2594 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2595 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2596 if (kerr != KCGI_OK)
2598 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2599 if (kerr != KCGI_OK)
2601 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2602 if (kerr != KCGI_OK)
2604 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2605 KATTR_ID, "header_commit_msg", KATTR__MAX);
2606 if (kerr != KCGI_OK)
2608 kerr = khttp_puts(gw_trans->gw_req, str);
2609 if (kerr != KCGI_OK)
2611 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2613 if (error == NULL && kerr != KCGI_OK)
2614 error = gw_kcgi_error(kerr);
2618 static const struct got_error *
2619 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2621 const struct got_error *error = NULL;
2622 enum kcgi_err kerr = KCGI_OK;
2624 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2625 KATTR_ID, "header_tree_title", KATTR__MAX);
2626 if (kerr != KCGI_OK)
2628 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2629 if (kerr != KCGI_OK)
2631 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2632 if (kerr != KCGI_OK)
2634 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2635 KATTR_ID, "header_tree", KATTR__MAX);
2636 if (kerr != KCGI_OK)
2638 kerr = khtml_puts(gw_trans->gw_html_req, str);
2639 if (kerr != KCGI_OK)
2641 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2643 if (error == NULL && kerr != KCGI_OK)
2644 error = gw_kcgi_error(kerr);
2648 static const struct got_error *
2649 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2652 const struct got_error *error = NULL;
2654 char *d_file = NULL;
2658 *description = NULL;
2659 if (gw_trans->gw_conf->got_show_repo_description == 0)
2662 if (asprintf(&d_file, "%s/description", dir) == -1)
2663 return got_error_from_errno("asprintf");
2665 f = fopen(d_file, "r");
2667 if (errno == ENOENT || errno == EACCES)
2669 error = got_error_from_errno2("fopen", d_file);
2673 if (fseek(f, 0, SEEK_END) == -1) {
2674 error = got_ferror(f, GOT_ERR_IO);
2679 error = got_ferror(f, GOT_ERR_IO);
2682 if (fseek(f, 0, SEEK_SET) == -1) {
2683 error = got_ferror(f, GOT_ERR_IO);
2686 *description = calloc(len + 1, sizeof(**description));
2687 if (*description == NULL) {
2688 error = got_error_from_errno("calloc");
2692 n = fread(*description, 1, len, f);
2693 if (n == 0 && ferror(f))
2694 error = got_ferror(f, GOT_ERR_IO);
2696 if (f != NULL && fclose(f) == -1 && error == NULL)
2697 error = got_error_from_errno("fclose");
2702 static const struct got_error *
2703 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2707 char *years = "years ago", *months = "months ago";
2708 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2709 char *minutes = "minutes ago", *seconds = "seconds ago";
2710 char *now = "right now";
2718 diff_time = time(NULL) - committer_time;
2719 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2720 if (asprintf(repo_age, "%lld %s",
2721 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2722 return got_error_from_errno("asprintf");
2723 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2724 if (asprintf(repo_age, "%lld %s",
2725 (diff_time / 60 / 60 / 24 / (365 / 12)),
2727 return got_error_from_errno("asprintf");
2728 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2729 if (asprintf(repo_age, "%lld %s",
2730 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2731 return got_error_from_errno("asprintf");
2732 } else if (diff_time > 60 * 60 * 24 * 2) {
2733 if (asprintf(repo_age, "%lld %s",
2734 (diff_time / 60 / 60 / 24), days) == -1)
2735 return got_error_from_errno("asprintf");
2736 } else if (diff_time > 60 * 60 * 2) {
2737 if (asprintf(repo_age, "%lld %s",
2738 (diff_time / 60 / 60), hours) == -1)
2739 return got_error_from_errno("asprintf");
2740 } else if (diff_time > 60 * 2) {
2741 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2743 return got_error_from_errno("asprintf");
2744 } else if (diff_time > 2) {
2745 if (asprintf(repo_age, "%lld %s", diff_time,
2747 return got_error_from_errno("asprintf");
2749 if (asprintf(repo_age, "%s", now) == -1)
2750 return got_error_from_errno("asprintf");
2754 if (gmtime_r(&committer_time, &tm) == NULL)
2755 return got_error_from_errno("gmtime_r");
2757 s = asctime_r(&tm, datebuf);
2759 return got_error_from_errno("asctime_r");
2761 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2762 return got_error_from_errno("asprintf");
2768 static const struct got_error *
2769 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2770 const char *refname, int ref_tm)
2772 const struct got_error *error = NULL;
2773 struct got_repository *repo = NULL;
2774 struct got_commit_object *commit = NULL;
2775 struct got_reflist_head refs;
2776 struct got_reflist_entry *re;
2777 time_t committer_time = 0, cmp_time = 0;
2780 SIMPLEQ_INIT(&refs);
2782 if (gw_trans->gw_conf->got_show_repo_age == 0)
2786 repo = gw_trans->repo;
2788 error = got_repo_open(&repo, dir, NULL);
2793 error = got_ref_list(&refs, repo, "refs/heads",
2794 got_ref_cmp_by_name, NULL);
2799 * Find the youngest branch tip in the repository, or the age of
2800 * the a specific branch tip if a name was provided by the caller.
2802 SIMPLEQ_FOREACH(re, &refs, entry) {
2803 struct got_object_id *id = NULL;
2805 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2808 error = got_ref_resolve(&id, repo, re->ref);
2812 error = got_object_open_as_commit(&commit, repo, id);
2818 got_object_commit_get_committer_time(commit);
2819 got_object_commit_close(commit);
2820 if (cmp_time < committer_time)
2821 cmp_time = committer_time;
2827 if (cmp_time != 0) {
2828 committer_time = cmp_time;
2829 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2832 got_ref_list_free(&refs);
2833 if (gw_trans->repo == NULL)
2834 got_repo_close(repo);
2838 static const struct got_error *
2839 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2841 const struct got_error *error;
2843 struct got_object_id *id1 = NULL, *id2 = NULL;
2844 char *label1 = NULL, *label2 = NULL, *line = NULL;
2846 size_t linesize = 0;
2848 enum kcgi_err kerr = KCGI_OK;
2854 if (header->parent_id != NULL &&
2855 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2856 error = got_repo_match_object_id(&id1, &label1,
2857 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2862 error = got_repo_match_object_id(&id2, &label2,
2863 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2867 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2871 case GOT_OBJ_TYPE_BLOB:
2872 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
2873 NULL, NULL, 3, 0, gw_trans->repo, f);
2875 case GOT_OBJ_TYPE_TREE:
2876 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
2877 "", "", 3, 0, gw_trans->repo, f);
2879 case GOT_OBJ_TYPE_COMMIT:
2880 error = got_diff_objects_as_commits(NULL, NULL, id1, id2, 3, 0,
2884 error = got_error(GOT_ERR_OBJ_TYPE);
2889 if (fseek(f, 0, SEEK_SET) == -1) {
2890 error = got_ferror(f, GOT_ERR_IO);
2894 while ((linelen = getline(&line, &linesize, f)) != -1) {
2895 error = gw_colordiff_line(gw_trans, line);
2898 /* XXX: KHTML_PRETTY breaks this */
2899 kerr = khtml_puts(gw_trans->gw_html_req, line);
2900 if (kerr != KCGI_OK)
2902 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2903 if (kerr != KCGI_OK)
2906 if (linelen == -1 && ferror(f))
2907 error = got_error_from_errno("getline");
2909 if (f && fclose(f) == -1 && error == NULL)
2910 error = got_error_from_errno("fclose");
2917 if (error == NULL && kerr != KCGI_OK)
2918 error = gw_kcgi_error(kerr);
2922 static const struct got_error *
2923 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2925 const struct got_error *error = NULL;
2926 struct got_repository *repo;
2927 const char *gitconfig_owner;
2931 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2934 error = got_repo_open(&repo, dir, NULL);
2937 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2938 if (gitconfig_owner) {
2939 *owner = strdup(gitconfig_owner);
2941 error = got_error_from_errno("strdup");
2943 got_repo_close(repo);
2947 static const struct got_error *
2948 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2950 const struct got_error *error = NULL;
2952 char *d_file = NULL;
2958 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2959 return got_error_from_errno("asprintf");
2961 f = fopen(d_file, "r");
2963 if (errno != ENOENT && errno != EACCES)
2964 error = got_error_from_errno2("fopen", d_file);
2968 if (fseek(f, 0, SEEK_END) == -1) {
2969 error = got_ferror(f, GOT_ERR_IO);
2974 error = got_ferror(f, GOT_ERR_IO);
2977 if (fseek(f, 0, SEEK_SET) == -1) {
2978 error = got_ferror(f, GOT_ERR_IO);
2982 *url = calloc(len + 1, sizeof(**url));
2984 error = got_error_from_errno("calloc");
2988 n = fread(*url, 1, len, f);
2989 if (n == 0 && ferror(f))
2990 error = got_ferror(f, GOT_ERR_IO);
2992 if (f && fclose(f) == -1 && error == NULL)
2993 error = got_error_from_errno("fclose");
2998 static const struct got_error *
2999 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3000 int limit, int tag_type)
3002 const struct got_error *error = NULL;
3003 struct got_reflist_head refs;
3004 struct got_reflist_entry *re;
3006 char *id_str = NULL, *newline, *href_commits = NULL;
3007 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3008 struct got_tag_object *tag = NULL;
3009 enum kcgi_err kerr = KCGI_OK;
3010 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
3011 int prev_set = 0, tag_count = 0;
3013 SIMPLEQ_INIT(&refs);
3015 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3016 got_ref_cmp_tags, gw_trans->repo);
3020 SIMPLEQ_FOREACH(re, &refs, entry) {
3021 const char *refname;
3023 const char *tag_commit;
3025 struct got_object_id *id;
3026 struct got_commit_object *commit = NULL;
3028 refname = got_ref_get_name(re->ref);
3029 if (strncmp(refname, "refs/tags/", 10) != 0)
3033 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3037 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3039 if (error->code != GOT_ERR_OBJ_TYPE) {
3043 /* "lightweight" tag */
3044 error = got_object_open_as_commit(&commit,
3045 gw_trans->repo, id);
3050 tagger = got_object_commit_get_committer(commit);
3052 got_object_commit_get_committer_time(commit);
3053 error = got_object_id_str(&id_str, id);
3057 tagger = got_object_tag_get_tagger(tag);
3058 tagger_time = got_object_tag_get_tagger_time(tag);
3059 error = got_object_id_str(&id_str,
3060 got_object_tag_get_object_id(tag));
3065 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3066 strlen(id_str)) != 0)
3069 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3070 start_tag == 0 && strncmp(id_str, header->commit_id,
3071 strlen(id_str)) != 0) {
3079 if (prev_set == 0 && start_tag == 1) {
3080 gw_trans->next_prev_id = strdup(id_str);
3081 if (gw_trans->next_prev_id == NULL) {
3082 error = got_error_from_errno("strdup");
3089 gw_trans->next_id = strdup(id_str);
3090 if (gw_trans->next_id == NULL)
3091 error = got_error_from_errno("strdup");
3096 error = got_object_commit_get_logmsg(&tag_commit0,
3100 got_object_commit_close(commit);
3102 tag_commit0 = strdup(got_object_tag_get_message(tag));
3103 if (tag_commit0 == NULL) {
3104 error = got_error_from_errno("strdup");
3109 tag_commit = tag_commit0;
3110 while (*tag_commit == '\n')
3115 newline = strchr(tag_commit, '\n');
3119 if (summary_header_displayed == 0) {
3120 kerr = khtml_attr(gw_trans->gw_html_req,
3121 KELEM_DIV, KATTR_ID,
3122 "summary_tags_title_wrapper", KATTR__MAX);
3123 if (kerr != KCGI_OK)
3125 kerr = khtml_attr(gw_trans->gw_html_req,
3126 KELEM_DIV, KATTR_ID,
3127 "summary_tags_title", KATTR__MAX);
3128 if (kerr != KCGI_OK)
3130 kerr = khtml_puts(gw_trans->gw_html_req,
3132 if (kerr != KCGI_OK)
3134 kerr = khtml_closeelem(gw_trans->gw_html_req,
3136 if (kerr != KCGI_OK)
3138 kerr = khtml_attr(gw_trans->gw_html_req,
3139 KELEM_DIV, KATTR_ID,
3140 "summary_tags_content", KATTR__MAX);
3141 if (kerr != KCGI_OK)
3143 summary_header_displayed = 1;
3146 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3147 KATTR_ID, "tag_wrapper", KATTR__MAX);
3148 if (kerr != KCGI_OK)
3150 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3151 KATTR_ID, "tag_age", KATTR__MAX);
3152 if (kerr != KCGI_OK)
3154 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3157 kerr = khtml_puts(gw_trans->gw_html_req,
3159 if (kerr != KCGI_OK)
3161 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3162 if (kerr != KCGI_OK)
3164 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3165 KATTR_ID, "tag", KATTR__MAX);
3166 if (kerr != KCGI_OK)
3168 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3169 if (kerr != KCGI_OK)
3171 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3172 if (kerr != KCGI_OK)
3174 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3175 KATTR_ID, "tag_name", KATTR__MAX);
3176 if (kerr != KCGI_OK)
3179 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3180 gw_trans->repo_name, "action", "tag", "commit",
3182 if (href_tag == NULL) {
3183 error = got_error_from_errno("khttp_urlpart");
3186 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3187 KATTR_HREF, href_tag, KATTR__MAX);
3188 if (kerr != KCGI_OK)
3190 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3191 if (kerr != KCGI_OK)
3193 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3194 if (kerr != KCGI_OK)
3197 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3198 KATTR_ID, "navs_wrapper", KATTR__MAX);
3199 if (kerr != KCGI_OK)
3201 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3202 KATTR_ID, "navs", KATTR__MAX);
3203 if (kerr != KCGI_OK)
3206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3207 KATTR_HREF, href_tag, KATTR__MAX);
3208 if (kerr != KCGI_OK)
3210 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3211 if (kerr != KCGI_OK)
3213 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3214 if (kerr != KCGI_OK)
3217 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3218 if (kerr != KCGI_OK)
3221 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3222 "path", gw_trans->repo_name, "action", "briefs",
3223 "commit", id_str, NULL);
3224 if (href_briefs == NULL) {
3225 error = got_error_from_errno("khttp_urlpart");
3228 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3229 KATTR_HREF, href_briefs, KATTR__MAX);
3230 if (kerr != KCGI_OK)
3232 kerr = khtml_puts(gw_trans->gw_html_req,
3234 if (kerr != KCGI_OK)
3236 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3237 if (kerr != KCGI_OK)
3240 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3241 if (kerr != KCGI_OK)
3244 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3245 "path", gw_trans->repo_name, "action", "commits",
3246 "commit", id_str, NULL);
3247 if (href_commits == NULL) {
3248 error = got_error_from_errno("khttp_urlpart");
3251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3252 KATTR_HREF, href_commits, KATTR__MAX);
3253 if (kerr != KCGI_OK)
3255 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3256 if (kerr != KCGI_OK)
3258 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3259 if (kerr != KCGI_OK)
3262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3263 KATTR_ID, "dotted_line", KATTR__MAX);
3264 if (kerr != KCGI_OK)
3266 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3267 if (kerr != KCGI_OK)
3271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3272 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3273 if (kerr != KCGI_OK)
3275 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3276 if (kerr != KCGI_OK)
3278 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3279 if (kerr != KCGI_OK)
3281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3282 KATTR_ID, "tag_info_date", KATTR__MAX);
3283 if (kerr != KCGI_OK)
3285 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3288 kerr = khtml_puts(gw_trans->gw_html_req,
3290 if (kerr != KCGI_OK)
3292 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3293 if (kerr != KCGI_OK)
3296 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3297 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3298 if (kerr != KCGI_OK)
3300 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3301 if (kerr != KCGI_OK)
3303 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3304 if (kerr != KCGI_OK)
3306 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3307 KATTR_ID, "tag_info_date", KATTR__MAX);
3308 if (kerr != KCGI_OK)
3310 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3311 if (kerr != KCGI_OK)
3313 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3314 if (kerr != KCGI_OK)
3317 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3318 KATTR_ID, "tag_info", KATTR__MAX);
3319 if (kerr != KCGI_OK)
3321 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3322 if (kerr != KCGI_OK)
3328 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3329 if (kerr != KCGI_OK)
3332 if (limit && --limit == 0)
3336 got_object_tag_close(tag);
3349 href_commits = NULL;
3351 if (tag_count == 0) {
3352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3353 "summary_tags_title_wrapper", KATTR__MAX);
3354 if (kerr != KCGI_OK)
3356 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3357 "summary_tags_title", KATTR__MAX);
3358 if (kerr != KCGI_OK)
3360 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3361 if (kerr != KCGI_OK)
3363 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3364 if (kerr != KCGI_OK)
3366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3367 "summary_tags_content", KATTR__MAX);
3368 if (kerr != KCGI_OK)
3370 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3371 "tags_info", KATTR__MAX);
3372 if (kerr != KCGI_OK)
3374 kerr = khttp_puts(gw_trans->gw_req,
3375 "There are no tags for this repo.");
3376 if (kerr != KCGI_OK)
3378 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3382 got_object_tag_close(tag);
3389 got_ref_list_free(&refs);
3390 if (error == NULL && kerr != KCGI_OK)
3391 error = gw_kcgi_error(kerr);
3396 gw_free_header(struct gw_header *header)
3399 free(header->author);
3400 free(header->committer);
3401 free(header->refs_str);
3402 free(header->commit_id);
3403 free(header->parent_id);
3404 free(header->tree_id);
3405 free(header->commit_msg);
3408 static struct gw_header *
3411 struct gw_header *header;
3413 header = malloc(sizeof(*header));
3417 header->path = NULL;
3418 SIMPLEQ_INIT(&header->refs);
3420 header->refs_str = NULL;
3421 header->commit_id = NULL;
3422 header->committer = NULL;
3423 header->author = NULL;
3424 header->parent_id = NULL;
3425 header->tree_id = NULL;
3426 header->commit_msg = NULL;
3431 static const struct got_error *
3432 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3433 int limit, struct got_object_id *id)
3435 const struct got_error *error = NULL;
3436 struct got_commit_graph *graph = NULL;
3437 struct got_commit_object *commit = NULL;
3438 int chk_next = 0, chk_multi = 0, prev_set = 0;
3440 error = got_commit_graph_open(&graph, header->path, 0);
3444 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3450 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3453 if (error->code == GOT_ERR_ITER_COMPLETED)
3460 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3463 if (limit == 1 && chk_multi == 0 &&
3464 gw_trans->gw_conf->got_max_commits_display != 1) {
3465 error = gw_get_commit(gw_trans, header, commit, id);
3470 struct gw_header *n_header = NULL;
3471 if ((n_header = gw_init_header()) == NULL) {
3472 error = got_error_from_errno("malloc");
3475 error = got_ref_list(&n_header->refs, gw_trans->repo,
3476 NULL, got_ref_cmp_by_name, NULL);
3480 error = gw_get_commit(gw_trans, n_header, commit, id);
3483 got_ref_list_free(&n_header->refs);
3486 * we have a commit_id now, so copy it to next_prev_id
3487 * for navigation through gw_briefs and gw_commits
3489 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3490 (gw_trans->action == GW_BRIEFS ||
3491 gw_trans->action == GW_COMMITS ||
3492 gw_trans->action == GW_SUMMARY)) {
3494 gw_trans->next_prev_id =
3495 strdup(n_header->commit_id);
3496 if (gw_trans->next_prev_id == NULL) {
3497 error = got_error_from_errno("strdup");
3503 * check for one more commit before breaking,
3504 * so we know whether to navicate through gw_briefs
3505 * gw_commits and gw_summary
3507 if (chk_next && (gw_trans->action == GW_BRIEFS||
3508 gw_trans->action == GW_COMMITS ||
3509 gw_trans->action == GW_SUMMARY)) {
3510 gw_trans->next_id = strdup(n_header->commit_id);
3511 if (gw_trans->next_id == NULL)
3512 error = got_error_from_errno("strdup");
3516 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3519 if (error || (limit && --limit == 0)) {
3527 got_object_commit_close(commit);
3529 got_commit_graph_close(graph);
3533 static const struct got_error *
3534 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3535 struct got_commit_object *commit, struct got_object_id *id)
3537 const struct got_error *error = NULL;
3538 struct got_reflist_entry *re;
3539 struct got_object_id *id2 = NULL;
3540 struct got_object_qid *parent_id;
3541 char *commit_msg = NULL, *commit_msg0;
3544 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3547 struct got_tag_object *tag = NULL;
3548 struct got_object_id *ref_id;
3551 if (got_ref_is_symbolic(re->ref))
3554 name = got_ref_get_name(re->ref);
3555 if (strncmp(name, "refs/", 5) == 0)
3557 if (strncmp(name, "got/", 4) == 0)
3559 if (strncmp(name, "heads/", 6) == 0)
3561 if (strncmp(name, "remotes/", 8) == 0) {
3563 s = strstr(name, "/" GOT_REF_HEAD);
3564 if (s != NULL && s[strlen(s)] == '\0')
3567 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3570 if (strncmp(name, "tags/", 5) == 0) {
3571 error = got_object_open_as_tag(&tag, gw_trans->repo,
3574 if (error->code != GOT_ERR_OBJ_TYPE) {
3579 * Ref points at something other
3586 cmp = got_object_id_cmp(tag ?
3587 got_object_tag_get_object_id(tag) : ref_id, id);
3590 got_object_tag_close(tag);
3593 s = header->refs_str;
3594 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3595 s ? ", " : "", name) == -1) {
3596 error = got_error_from_errno("asprintf");
3598 header->refs_str = NULL;
3604 error = got_object_id_str(&header->commit_id, id);
3608 error = got_object_id_str(&header->tree_id,
3609 got_object_commit_get_tree_id(commit));
3613 if (gw_trans->action == GW_DIFF) {
3614 parent_id = SIMPLEQ_FIRST(
3615 got_object_commit_get_parent_ids(commit));
3616 if (parent_id != NULL) {
3617 id2 = got_object_id_dup(parent_id->id);
3619 error = got_object_id_str(&header->parent_id, id2);
3624 header->parent_id = strdup("/dev/null");
3625 if (header->parent_id == NULL) {
3626 error = got_error_from_errno("strdup");
3632 header->committer_time =
3633 got_object_commit_get_committer_time(commit);
3636 strdup(got_object_commit_get_author(commit));
3637 if (header->author == NULL) {
3638 error = got_error_from_errno("strdup");
3642 strdup(got_object_commit_get_committer(commit));
3643 if (header->committer == NULL) {
3644 error = got_error_from_errno("strdup");
3647 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3651 commit_msg = commit_msg0;
3652 while (*commit_msg == '\n')
3655 header->commit_msg = strdup(commit_msg);
3656 if (header->commit_msg == NULL)
3657 error = got_error_from_errno("strdup");
3662 static const struct got_error *
3663 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3665 const struct got_error *error = NULL;
3666 char *in_repo_path = NULL;
3667 struct got_object_id *id = NULL;
3669 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3673 if (gw_trans->commit_id == NULL) {
3674 struct got_reference *head_ref;
3675 error = got_ref_open(&head_ref, gw_trans->repo,
3676 gw_trans->headref, 0);
3680 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3681 got_ref_close(head_ref);
3685 struct got_reference *ref;
3687 error = got_ref_open(&ref, gw_trans->repo,
3688 gw_trans->commit_id, 0);
3689 if (error == NULL) {
3691 error = got_ref_resolve(&id, gw_trans->repo, ref);
3695 error = got_object_get_type(&obj_type, gw_trans->repo,
3699 if (obj_type == GOT_OBJ_TYPE_TAG) {
3700 struct got_tag_object *tag;
3701 error = got_object_open_as_tag(&tag,
3702 gw_trans->repo, id);
3705 if (got_object_tag_get_object_type(tag) !=
3706 GOT_OBJ_TYPE_COMMIT) {
3707 got_object_tag_close(tag);
3708 error = got_error(GOT_ERR_OBJ_TYPE);
3712 id = got_object_id_dup(
3713 got_object_tag_get_object_id(tag));
3715 error = got_error_from_errno(
3716 "got_object_id_dup");
3717 got_object_tag_close(tag);
3720 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3721 error = got_error(GOT_ERR_OBJ_TYPE);
3725 error = got_repo_match_object_id_prefix(&id,
3726 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3732 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3733 gw_trans->repo_path);
3738 header->path = strdup(in_repo_path);
3739 if (header->path == NULL) {
3740 error = got_error_from_errno("strdup");
3745 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3746 got_ref_cmp_by_name, NULL);
3750 error = gw_get_commits(gw_trans, header, limit, id);
3752 got_ref_list_free(&header->refs);
3762 char datebuf[11]; /* YYYY-MM-DD + NUL */
3765 struct gw_blame_cb_args {
3766 struct blame_line *lines;
3770 off_t *line_offsets;
3772 struct got_repository *repo;
3773 struct gw_trans *gw_trans;
3776 static const struct got_error *
3777 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3779 const struct got_error *err = NULL;
3780 struct gw_blame_cb_args *a = arg;
3781 struct blame_line *bline;
3783 size_t linesize = 0;
3784 struct got_commit_object *commit = NULL;
3787 time_t committer_time;
3788 enum kcgi_err kerr = KCGI_OK;
3790 if (nlines != a->nlines ||
3791 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3792 return got_error(GOT_ERR_RANGE);
3795 return NULL; /* no change in this commit */
3797 /* Annotate this line. */
3798 bline = &a->lines[lineno - 1];
3799 if (bline->annotated)
3801 err = got_object_id_str(&bline->id_str, id);
3805 err = got_object_open_as_commit(&commit, a->repo, id);
3809 bline->committer = strdup(got_object_commit_get_committer(commit));
3810 if (bline->committer == NULL) {
3811 err = got_error_from_errno("strdup");
3815 committer_time = got_object_commit_get_committer_time(commit);
3816 if (localtime_r(&committer_time, &tm) == NULL)
3817 return got_error_from_errno("localtime_r");
3818 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3819 &tm) >= sizeof(bline->datebuf)) {
3820 err = got_error(GOT_ERR_NO_SPACE);
3823 bline->annotated = 1;
3825 /* Print lines annotated so far. */
3826 bline = &a->lines[a->lineno_cur - 1];
3827 if (!bline->annotated)
3830 offset = a->line_offsets[a->lineno_cur - 1];
3831 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3832 err = got_error_from_errno("fseeko");
3836 while (bline->annotated) {
3837 char *smallerthan, *at, *nl, *committer;
3838 char *href_diff = NULL;
3841 if (getline(&line, &linesize, a->f) == -1) {
3843 err = got_error_from_errno("getline");
3847 committer = bline->committer;
3848 smallerthan = strchr(committer, '<');
3849 if (smallerthan && smallerthan[1] != '\0')
3850 committer = smallerthan + 1;
3851 at = strchr(committer, '@');
3854 len = strlen(committer);
3856 committer[8] = '\0';
3858 nl = strchr(line, '\n');
3862 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3863 "blame_wrapper", KATTR__MAX);
3864 if (kerr != KCGI_OK)
3866 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3867 "blame_number", KATTR__MAX);
3868 if (kerr != KCGI_OK)
3870 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3871 a->nlines_prec, a->lineno_cur);
3872 if (kerr != KCGI_OK)
3874 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3875 if (kerr != KCGI_OK)
3878 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3879 "blame_hash", KATTR__MAX);
3880 if (kerr != KCGI_OK)
3883 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3884 a->gw_trans->repo_name, "action", "diff", "commit",
3885 bline->id_str, NULL);
3886 if (href_diff == NULL) {
3887 err = got_error_from_errno("khttp_urlpart");
3890 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3891 KATTR_HREF, href_diff, KATTR__MAX);
3892 if (kerr != KCGI_OK)
3894 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3896 if (kerr != KCGI_OK)
3898 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3899 if (kerr != KCGI_OK)
3902 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3903 "blame_date", KATTR__MAX);
3904 if (kerr != KCGI_OK)
3906 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3907 if (kerr != KCGI_OK)
3909 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3910 if (kerr != KCGI_OK)
3913 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3914 "blame_author", KATTR__MAX);
3915 if (kerr != KCGI_OK)
3917 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3918 if (kerr != KCGI_OK)
3920 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3921 if (kerr != KCGI_OK)
3924 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3925 "blame_code", KATTR__MAX);
3926 if (kerr != KCGI_OK)
3928 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3929 if (kerr != KCGI_OK)
3931 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3932 if (kerr != KCGI_OK)
3935 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3936 if (kerr != KCGI_OK)
3940 bline = &a->lines[a->lineno_cur - 1];
3946 got_object_commit_close(commit);
3948 if (err == NULL && kerr != KCGI_OK)
3949 err = gw_kcgi_error(kerr);
3953 static const struct got_error *
3954 gw_output_file_blame(struct gw_trans *gw_trans)
3956 const struct got_error *error = NULL;
3957 struct got_object_id *obj_id = NULL;
3958 struct got_object_id *commit_id = NULL;
3959 struct got_blob_object *blob = NULL;
3960 char *path = NULL, *in_repo_path = NULL;
3961 struct gw_blame_cb_args bca;
3965 if (asprintf(&path, "%s%s%s",
3966 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3967 gw_trans->repo_folder ? "/" : "",
3968 gw_trans->repo_file) == -1) {
3969 error = got_error_from_errno("asprintf");
3973 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
3977 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3978 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3982 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3987 if (obj_id == NULL) {
3988 error = got_error(GOT_ERR_NO_OBJ);
3992 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3996 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3997 error = got_error(GOT_ERR_OBJ_TYPE);
4001 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4005 bca.f = got_opentemp();
4006 if (bca.f == NULL) {
4007 error = got_error_from_errno("got_opentemp");
4010 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4011 &bca.line_offsets, bca.f, blob);
4012 if (error || bca.nlines == 0)
4015 /* Don't include \n at EOF in the blame line count. */
4016 if (bca.line_offsets[bca.nlines - 1] == filesize)
4019 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4020 if (bca.lines == NULL) {
4021 error = got_error_from_errno("calloc");
4025 bca.nlines_prec = 0;
4031 bca.repo = gw_trans->repo;
4032 bca.gw_trans = gw_trans;
4034 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4043 free(bca.line_offsets);
4044 for (i = 0; i < bca.nlines; i++) {
4045 struct blame_line *bline = &bca.lines[i];
4046 free(bline->id_str);
4047 free(bline->committer);
4050 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4051 error = got_error_from_errno("fclose");
4054 got_object_blob_close(blob);
4058 static const struct got_error *
4059 gw_output_blob_buf(struct gw_trans *gw_trans)
4061 const struct got_error *error = NULL;
4062 struct got_object_id *obj_id = NULL;
4063 struct got_object_id *commit_id = NULL;
4064 struct got_blob_object *blob = NULL;
4065 char *path = NULL, *in_repo_path = NULL;
4066 int obj_type, set_mime = 0;
4069 enum kcgi_err kerr = KCGI_OK;
4071 if (asprintf(&path, "%s%s%s",
4072 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4073 gw_trans->repo_folder ? "/" : "",
4074 gw_trans->repo_file) == -1) {
4075 error = got_error_from_errno("asprintf");
4079 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4083 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4084 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
4088 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4093 if (obj_id == NULL) {
4094 error = got_error(GOT_ERR_NO_OBJ);
4098 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4102 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4103 error = got_error(GOT_ERR_OBJ_TYPE);
4107 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4111 hdrlen = got_object_blob_get_hdrlen(blob);
4113 error = got_object_blob_read_block(&len, blob);
4116 buf = got_object_blob_get_read_buf(blob);
4119 * Skip blob object header first time around,
4120 * which also contains a zero byte.
4123 if (set_mime == 0) {
4124 if (isbinary(buf, len - hdrlen))
4125 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4127 gw_trans->mime = KMIME_TEXT_PLAIN;
4129 error = gw_display_index(gw_trans);
4133 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4134 if (kerr != KCGI_OK)
4144 got_object_blob_close(blob);
4145 if (error == NULL && kerr != KCGI_OK)
4146 error = gw_kcgi_error(kerr);
4150 static const struct got_error *
4151 gw_output_repo_tree(struct gw_trans *gw_trans)
4153 const struct got_error *error = NULL;
4154 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4155 struct got_tree_object *tree = NULL;
4156 char *path = NULL, *in_repo_path = NULL;
4157 char *id_str = NULL;
4158 char *build_folder = NULL;
4159 char *href_blob = NULL, *href_blame = NULL;
4160 const char *class = NULL;
4161 int nentries, i, class_flip = 0;
4162 enum kcgi_err kerr = KCGI_OK;
4164 if (gw_trans->repo_folder != NULL) {
4165 path = strdup(gw_trans->repo_folder);
4167 error = got_error_from_errno("strdup");
4171 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4172 gw_trans->repo_path);
4176 path = in_repo_path;
4179 if (gw_trans->commit_id == NULL) {
4180 struct got_reference *head_ref;
4181 error = got_ref_open(&head_ref, gw_trans->repo,
4182 gw_trans->headref, 0);
4185 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4188 got_ref_close(head_ref);
4190 * gw_trans->commit_id was not parsed from the querystring
4191 * we hit this code path from gw_index, where we don't know the
4192 * commit values for the tree link yet, so set
4193 * gw_trans->commit_id here to continue further into the tree
4195 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4200 error = got_repo_match_object_id(&commit_id, NULL,
4201 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
4207 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4212 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4216 nentries = got_object_tree_get_nentries(tree);
4217 for (i = 0; i < nentries; i++) {
4218 struct got_tree_entry *te;
4219 const char *modestr = "";
4222 te = got_object_tree_get_entry(tree, i);
4224 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4228 mode = got_tree_entry_get_mode(te);
4229 if (got_object_tree_entry_is_submodule(te))
4231 else if (S_ISLNK(mode))
4233 else if (S_ISDIR(mode))
4235 else if (mode & S_IXUSR)
4238 if (class_flip == 0) {
4239 class = "back_lightgray";
4242 class = "back_white";
4246 if (S_ISDIR(mode)) {
4247 if (asprintf(&build_folder, "%s/%s",
4248 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4249 got_tree_entry_get_name(te)) == -1) {
4250 error = got_error_from_errno("asprintf");
4254 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4255 gw_trans->repo_name, "action",
4256 gw_get_action_name(gw_trans), "commit",
4257 gw_trans->commit_id, "folder", build_folder, NULL);
4258 if (href_blob == NULL) {
4259 error = got_error_from_errno("khttp_urlpart");
4262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4263 KATTR_ID, "tree_wrapper", KATTR__MAX);
4264 if (kerr != KCGI_OK)
4266 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4267 KATTR_ID, "tree_line", KATTR_CLASS, class,
4269 if (kerr != KCGI_OK)
4271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4272 KATTR_HREF, href_blob, KATTR_CLASS,
4273 "diff_directory", KATTR__MAX);
4274 if (kerr != KCGI_OK)
4276 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4277 got_tree_entry_get_name(te), modestr);
4278 if (kerr != KCGI_OK)
4280 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4281 if (kerr != KCGI_OK)
4283 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4284 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4286 if (kerr != KCGI_OK)
4288 kerr = khtml_entity(gw_trans->gw_html_req,
4290 if (kerr != KCGI_OK)
4292 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4293 if (kerr != KCGI_OK)
4296 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4297 gw_trans->repo_name, "action", "blob", "commit",
4298 gw_trans->commit_id, "file",
4299 got_tree_entry_get_name(te), "folder",
4300 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4302 if (href_blob == NULL) {
4303 error = got_error_from_errno("khttp_urlpart");
4306 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4307 gw_trans->repo_name, "action", "blame", "commit",
4308 gw_trans->commit_id, "file",
4309 got_tree_entry_get_name(te), "folder",
4310 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4312 if (href_blame == NULL) {
4313 error = got_error_from_errno("khttp_urlpart");
4316 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4317 KATTR_ID, "tree_wrapper", KATTR__MAX);
4318 if (kerr != KCGI_OK)
4320 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4321 KATTR_ID, "tree_line", KATTR_CLASS, class,
4323 if (kerr != KCGI_OK)
4325 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4326 KATTR_HREF, href_blob, KATTR__MAX);
4327 if (kerr != KCGI_OK)
4329 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4330 got_tree_entry_get_name(te), modestr);
4331 if (kerr != KCGI_OK)
4333 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4334 if (kerr != KCGI_OK)
4336 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4337 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4339 if (kerr != KCGI_OK)
4342 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4343 KATTR_HREF, href_blob, KATTR__MAX);
4344 if (kerr != KCGI_OK)
4346 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4347 if (kerr != KCGI_OK)
4349 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4350 if (kerr != KCGI_OK)
4353 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4354 if (kerr != KCGI_OK)
4357 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4358 KATTR_HREF, href_blame, KATTR__MAX);
4359 if (kerr != KCGI_OK)
4361 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4362 if (kerr != KCGI_OK)
4365 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4366 if (kerr != KCGI_OK)
4374 build_folder = NULL;
4378 got_object_tree_close(tree);
4385 if (error == NULL && kerr != KCGI_OK)
4386 error = gw_kcgi_error(kerr);
4390 static const struct got_error *
4391 gw_output_repo_heads(struct gw_trans *gw_trans)
4393 const struct got_error *error = NULL;
4394 struct got_reflist_head refs;
4395 struct got_reflist_entry *re;
4396 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4397 char *href_commits = NULL;
4398 enum kcgi_err kerr = KCGI_OK;
4400 SIMPLEQ_INIT(&refs);
4402 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4403 got_ref_cmp_by_name, NULL);
4407 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4408 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4409 if (kerr != KCGI_OK)
4411 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4412 KATTR_ID, "summary_heads_title", KATTR__MAX);
4413 if (kerr != KCGI_OK)
4415 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4416 if (kerr != KCGI_OK)
4418 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4419 if (kerr != KCGI_OK)
4421 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4422 KATTR_ID, "summary_heads_content", KATTR__MAX);
4423 if (kerr != KCGI_OK)
4426 SIMPLEQ_FOREACH(re, &refs, entry) {
4427 const char *refname;
4429 if (got_ref_is_symbolic(re->ref))
4432 refname = got_ref_get_name(re->ref);
4433 if (strncmp(refname, "refs/heads/", 11) != 0)
4436 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4441 if (strncmp(refname, "refs/heads/", 11) == 0)
4444 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4445 KATTR_ID, "heads_wrapper", KATTR__MAX);
4446 if (kerr != KCGI_OK)
4448 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4449 KATTR_ID, "heads_age", KATTR__MAX);
4450 if (kerr != KCGI_OK)
4452 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4453 if (kerr != KCGI_OK)
4455 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4456 if (kerr != KCGI_OK)
4458 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4459 KATTR_ID, "heads_space", KATTR__MAX);
4460 if (kerr != KCGI_OK)
4462 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4463 if (kerr != KCGI_OK)
4465 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4466 if (kerr != KCGI_OK)
4468 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4469 KATTR_ID, "head", KATTR__MAX);
4470 if (kerr != KCGI_OK)
4473 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4474 gw_trans->repo_name, "action", "summary", "headref",
4476 if (href_summary == NULL) {
4477 error = got_error_from_errno("khttp_urlpart");
4480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4481 href_summary, KATTR__MAX);
4482 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4483 if (kerr != KCGI_OK)
4485 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4486 if (kerr != KCGI_OK)
4489 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4490 "navs_wrapper", KATTR__MAX);
4491 if (kerr != KCGI_OK)
4493 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4494 "navs", KATTR__MAX);
4495 if (kerr != KCGI_OK)
4498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4499 href_summary, KATTR__MAX);
4500 if (kerr != KCGI_OK)
4502 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4503 if (kerr != KCGI_OK)
4505 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4506 if (kerr != KCGI_OK)
4509 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4510 if (kerr != KCGI_OK)
4513 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4514 gw_trans->repo_name, "action", "briefs", "headref",
4516 if (href_briefs == NULL) {
4517 error = got_error_from_errno("khttp_urlpart");
4520 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4521 href_briefs, KATTR__MAX);
4522 if (kerr != KCGI_OK)
4524 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4525 if (kerr != KCGI_OK)
4527 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4528 if (kerr != KCGI_OK)
4531 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4532 if (kerr != KCGI_OK)
4535 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4536 gw_trans->repo_name, "action", "commits", "headref",
4538 if (href_commits == NULL) {
4539 error = got_error_from_errno("khttp_urlpart");
4542 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4543 href_commits, KATTR__MAX);
4544 if (kerr != KCGI_OK)
4546 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4547 if (kerr != KCGI_OK)
4549 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4550 if (kerr != KCGI_OK)
4553 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4554 "dotted_line", KATTR__MAX);
4555 if (kerr != KCGI_OK)
4557 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4558 if (kerr != KCGI_OK)
4561 href_summary = NULL;
4565 href_commits = NULL;
4568 got_ref_list_free(&refs);
4575 static const struct got_error *
4576 gw_output_site_link(struct gw_trans *gw_trans)
4578 const struct got_error *error = NULL;
4579 char *href_summary = NULL;
4580 enum kcgi_err kerr = KCGI_OK;
4582 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4583 "site_link", KATTR__MAX);
4584 if (kerr != KCGI_OK)
4586 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4588 if (kerr != KCGI_OK)
4590 kerr = khtml_puts(gw_trans->gw_html_req,
4591 gw_trans->gw_conf->got_site_link);
4592 if (kerr != KCGI_OK)
4594 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4595 if (kerr != KCGI_OK)
4598 if (gw_trans->repo_name != NULL) {
4599 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4600 if (kerr != KCGI_OK)
4603 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4604 gw_trans->repo_name, "action", "summary", NULL);
4605 if (href_summary == NULL) {
4606 error = got_error_from_errno("khttp_urlpart");
4609 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4610 href_summary, KATTR__MAX);
4611 if (kerr != KCGI_OK)
4613 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4614 if (kerr != KCGI_OK)
4616 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4617 if (kerr != KCGI_OK)
4619 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4620 gw_get_action_name(gw_trans));
4621 if (kerr != KCGI_OK)
4625 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4626 if (kerr != KCGI_OK)
4630 if (error == NULL && kerr != KCGI_OK)
4631 error = gw_kcgi_error(kerr);
4635 static const struct got_error *
4636 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4638 const struct got_error *error = NULL;
4640 enum kcgi_err kerr = KCGI_OK;
4642 if (strncmp(buf, "-", 1) == 0)
4643 color = "diff_minus";
4644 else if (strncmp(buf, "+", 1) == 0)
4645 color = "diff_plus";
4646 else if (strncmp(buf, "@@", 2) == 0)
4647 color = "diff_chunk_header";
4648 else if (strncmp(buf, "@@", 2) == 0)
4649 color = "diff_chunk_header";
4650 else if (strncmp(buf, "commit +", 8) == 0)
4651 color = "diff_meta";
4652 else if (strncmp(buf, "commit -", 8) == 0)
4653 color = "diff_meta";
4654 else if (strncmp(buf, "blob +", 6) == 0)
4655 color = "diff_meta";
4656 else if (strncmp(buf, "blob -", 6) == 0)
4657 color = "diff_meta";
4658 else if (strncmp(buf, "file +", 6) == 0)
4659 color = "diff_meta";
4660 else if (strncmp(buf, "file -", 6) == 0)
4661 color = "diff_meta";
4662 else if (strncmp(buf, "from:", 5) == 0)
4663 color = "diff_author";
4664 else if (strncmp(buf, "via:", 4) == 0)
4665 color = "diff_author";
4666 else if (strncmp(buf, "date:", 5) == 0)
4667 color = "diff_date";
4668 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4669 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4670 if (error == NULL && kerr != KCGI_OK)
4671 error = gw_kcgi_error(kerr);
4676 main(int argc, char *argv[])
4678 const struct got_error *error = NULL;
4679 struct gw_trans *gw_trans;
4680 struct gw_dir *dir = NULL, *tdir;
4681 const char *page = "index";
4682 enum kcgi_err kerr = KCGI_OK;
4684 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4687 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4690 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4693 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4696 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4697 if (kerr != KCGI_OK) {
4698 error = gw_kcgi_error(kerr);
4702 TAILQ_INIT(&gw_trans->gw_dirs);
4703 TAILQ_INIT(&gw_trans->gw_headers);
4705 gw_trans->action = -1;
4707 gw_trans->repos_total = 0;
4708 gw_trans->repo_path = NULL;
4709 gw_trans->commit_id = NULL;
4710 gw_trans->next_id = NULL;
4711 gw_trans->next_prev_id = NULL;
4712 gw_trans->prev_id = NULL;
4713 gw_trans->prev_prev_id = NULL;
4714 gw_trans->headref = GOT_REF_HEAD;
4715 gw_trans->mime = KMIME_TEXT_HTML;
4716 gw_trans->gw_tmpl->key = gw_templs;
4717 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4718 gw_trans->gw_tmpl->arg = gw_trans;
4719 gw_trans->gw_tmpl->cb = gw_template;
4721 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4725 error = gw_parse_querystring(gw_trans);
4729 if (gw_trans->action == GW_BLOB)
4730 error = gw_blob(gw_trans);
4732 error = gw_display_index(gw_trans);
4734 free(gw_trans->gw_conf->got_repos_path);
4735 free(gw_trans->gw_conf->got_www_path);
4736 free(gw_trans->gw_conf->got_site_name);
4737 free(gw_trans->gw_conf->got_site_owner);
4738 free(gw_trans->gw_conf->got_site_link);
4739 free(gw_trans->gw_conf->got_logo);
4740 free(gw_trans->gw_conf->got_logo_url);
4741 free(gw_trans->gw_conf);
4742 free(gw_trans->commit_id);
4743 free(gw_trans->next_id);
4744 free(gw_trans->next_prev_id);
4745 free(gw_trans->prev_id);
4746 free(gw_trans->prev_prev_id);
4747 free(gw_trans->repo_path);
4749 got_repo_close(gw_trans->repo);
4751 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4753 free(dir->description);
4760 khttp_free(gw_trans->gw_req);