2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
20 #include <sys/types.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct got_repository *repo;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
64 const struct got_error *error;
65 const char *repo_name;
72 const char *repo_file;
77 unsigned int repos_total;
82 TAILQ_ENTRY(gw_header) entry;
83 struct got_reflist_head refs;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
93 time_t committer_time;
97 TAILQ_ENTRY(gw_dir) entry;
140 static const char *const gw_templs[TEMPL__MAX] = {
150 static const struct kvalid gw_keys[KEY__ZMAX] = {
151 { kvalid_stringne, "action" },
152 { kvalid_stringne, "commit" },
153 { kvalid_stringne, "file" },
154 { kvalid_stringne, "folder" },
155 { kvalid_stringne, "headref" },
156 { kvalid_int, "page" },
157 { kvalid_stringne, "path" },
158 { kvalid_stringne, "prev" },
159 { kvalid_stringne, "prev_prev" },
162 static struct gw_header *gw_init_header(void);
164 static void gw_free_header(struct gw_header *);
166 static int gw_template(size_t, void *);
168 static const struct got_error *gw_error(struct gw_trans *);
169 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
170 static const struct got_error *gw_get_repo_description(char **,
171 struct gw_trans *, char *);
172 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
174 static const struct got_error *gw_get_time_str(char **, time_t, int);
175 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
176 char *, const char *, int);
177 static const struct got_error *gw_output_file_blame(struct gw_trans *);
178 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
179 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
180 static const struct got_error *gw_output_diff(struct gw_trans *,
182 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
183 struct gw_header *, int, int);
184 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
185 static const struct got_error *gw_output_site_link(struct gw_trans *);
186 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
188 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
190 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
192 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
194 static const struct got_error *gw_gen_author_header(struct gw_trans *,
196 static const struct got_error *gw_gen_age_header(struct gw_trans *,
198 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
200 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
202 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
203 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
205 static const struct got_error *gw_display_index(struct gw_trans *);
206 static const struct got_error *gw_get_header(struct gw_trans *,
207 struct gw_header *, int);
208 static const struct got_error *gw_get_commits(struct gw_trans *,
209 struct gw_header *, int,
210 struct got_object_id *);
211 static const struct got_error *gw_get_commit(struct gw_trans *,
213 struct got_commit_object *,
214 struct got_object_id *);
215 static const struct got_error *gw_apply_unveil(const char *);
216 static const struct got_error *gw_blame_cb(void *, int, int,
217 struct got_object_id *);
218 static const struct got_error *gw_load_got_paths(struct gw_trans *);
219 static const struct got_error *gw_load_got_path(struct gw_trans *,
221 static const struct got_error *gw_parse_querystring(struct gw_trans *);
222 static const struct got_error *gw_blame(struct gw_trans *);
223 static const struct got_error *gw_blob(struct gw_trans *);
224 static const struct got_error *gw_diff(struct gw_trans *);
225 static const struct got_error *gw_index(struct gw_trans *);
226 static const struct got_error *gw_commits(struct gw_trans *);
227 static const struct got_error *gw_briefs(struct gw_trans *);
228 static const struct got_error *gw_summary(struct gw_trans *);
229 static const struct got_error *gw_tree(struct gw_trans *);
230 static const struct got_error *gw_tag(struct gw_trans *);
231 static const struct got_error *gw_tags(struct gw_trans *);
233 struct gw_query_action {
234 unsigned int func_id;
235 const char *func_name;
236 const struct got_error *(*func_main)(struct gw_trans *);
240 enum gw_query_actions {
254 static struct gw_query_action gw_query_funcs[] = {
255 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
256 { GW_BLOB, "blob", NULL, NULL },
257 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
258 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
259 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
260 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
261 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
262 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
263 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
264 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
265 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
269 gw_get_action_name(struct gw_trans *gw_trans)
271 return gw_query_funcs[gw_trans->action].func_name;
274 static const struct got_error *
275 gw_kcgi_error(enum kcgi_err kerr)
280 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
281 return got_error(GOT_ERR_CANCELLED);
283 if (kerr == KCGI_ENOMEM)
284 return got_error_set_errno(ENOMEM,
285 kcgi_strerror(kerr));
287 if (kerr == KCGI_ENFILE)
288 return got_error_set_errno(ENFILE,
289 kcgi_strerror(kerr));
291 if (kerr == KCGI_EAGAIN)
292 return got_error_set_errno(EAGAIN,
293 kcgi_strerror(kerr));
295 if (kerr == KCGI_FORM)
296 return got_error_msg(GOT_ERR_IO,
297 kcgi_strerror(kerr));
299 return got_error_from_errno(kcgi_strerror(kerr));
302 static const struct got_error *
303 gw_apply_unveil(const char *repo_path)
305 const struct got_error *err;
307 if (repo_path && unveil(repo_path, "r") != 0)
308 return got_error_from_errno2("unveil", repo_path);
310 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
311 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
313 err = got_privsep_unveil_exec_helpers();
317 if (unveil(NULL, NULL) != 0)
318 return got_error_from_errno("unveil");
324 isbinary(const uint8_t *buf, size_t n)
328 for (i = 0; i < n; i++)
334 static const struct got_error *
335 gw_blame(struct gw_trans *gw_trans)
337 const struct got_error *error = NULL;
338 struct gw_header *header = NULL;
340 enum kcgi_err kerr = KCGI_OK;
342 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
344 return got_error_from_errno("pledge");
346 if ((header = gw_init_header()) == NULL)
347 return got_error_from_errno("malloc");
349 error = gw_apply_unveil(gw_trans->gw_dir->path);
353 /* check querystring */
354 if (gw_trans->repo_file == NULL) {
355 error = got_error_msg(GOT_ERR_QUERYSTRING,
356 "file required in querystring");
359 if (gw_trans->commit_id == NULL) {
360 error = got_error_msg(GOT_ERR_QUERYSTRING,
361 "commit required in querystring");
365 error = gw_get_header(gw_trans, header, 1);
368 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
369 "blame_header_wrapper", KATTR__MAX);
372 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
373 "blame_header", KATTR__MAX);
376 error = gw_get_time_str(&age, header->committer_time,
380 error = gw_gen_age_header(gw_trans, age ?age : "");
383 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
386 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
390 "dotted_line", KATTR__MAX);
393 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
397 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
398 "blame", KATTR__MAX);
401 error = gw_output_file_blame(gw_trans);
404 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
406 gw_free_header(header);
407 if (error == NULL && kerr != KCGI_OK)
408 error = gw_kcgi_error(kerr);
412 static const struct got_error *
413 gw_blob(struct gw_trans *gw_trans)
415 const struct got_error *error = NULL, *err = NULL;
416 struct gw_header *header = NULL;
417 enum kcgi_err kerr = KCGI_OK;
419 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
421 return got_error_from_errno("pledge");
423 if ((header = gw_init_header()) == NULL)
424 return got_error_from_errno("malloc");
426 error = gw_apply_unveil(gw_trans->gw_dir->path);
430 /* check querystring */
431 if (gw_trans->repo_file == NULL) {
432 error = got_error_msg(GOT_ERR_QUERYSTRING,
433 "file required in querystring");
436 if (gw_trans->commit_id == NULL) {
437 error = got_error_msg(GOT_ERR_QUERYSTRING,
438 "commit required in querystring");
441 error = gw_get_header(gw_trans, header, 1);
445 error = gw_output_blob_buf(gw_trans);
449 gw_trans->mime = KMIME_TEXT_PLAIN;
450 err = gw_display_index(gw_trans);
455 kerr = khttp_puts(gw_trans->gw_req, error->msg);
458 gw_free_header(header);
459 if (error == NULL && kerr != KCGI_OK)
460 error = gw_kcgi_error(kerr);
464 static const struct got_error *
465 gw_diff(struct gw_trans *gw_trans)
467 const struct got_error *error = NULL;
468 struct gw_header *header = NULL;
470 enum kcgi_err kerr = KCGI_OK;
472 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
474 return got_error_from_errno("pledge");
476 if ((header = gw_init_header()) == NULL)
477 return got_error_from_errno("malloc");
479 error = gw_apply_unveil(gw_trans->gw_dir->path);
483 error = gw_get_header(gw_trans, header, 1);
487 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
488 "diff_header_wrapper", KATTR__MAX);
491 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
492 "diff_header", KATTR__MAX);
495 error = gw_gen_diff_header(gw_trans, header->parent_id,
499 error = gw_gen_commit_header(gw_trans, header->commit_id,
503 error = gw_gen_tree_header(gw_trans, header->tree_id);
506 error = gw_gen_author_header(gw_trans, header->author);
509 error = gw_gen_committer_header(gw_trans, header->author);
512 error = gw_get_time_str(&age, header->committer_time,
516 error = gw_gen_age_header(gw_trans, age ?age : "");
519 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
522 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
525 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
526 "dotted_line", KATTR__MAX);
529 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
537 error = gw_output_diff(gw_trans, header);
541 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
543 gw_free_header(header);
545 if (error == NULL && kerr != KCGI_OK)
546 error = gw_kcgi_error(kerr);
550 static const struct got_error *
551 gw_index(struct gw_trans *gw_trans)
553 const struct got_error *error = NULL;
554 struct gw_dir *gw_dir = NULL;
555 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
556 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
557 char *href_tags = NULL;
558 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
559 enum kcgi_err kerr = KCGI_OK;
561 if (pledge("stdio rpath proc exec sendfd unveil",
563 error = got_error_from_errno("pledge");
567 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
571 error = gw_load_got_paths(gw_trans);
575 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
576 "index_header", KATTR__MAX);
578 return gw_kcgi_error(kerr);
579 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
580 "index_header_project", KATTR__MAX);
582 return gw_kcgi_error(kerr);
583 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
585 return gw_kcgi_error(kerr);
586 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
588 return gw_kcgi_error(kerr);
590 if (gw_trans->gw_conf->got_show_repo_description) {
591 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
592 "index_header_description", KATTR__MAX);
594 return gw_kcgi_error(kerr);
595 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
597 return gw_kcgi_error(kerr);
598 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
600 return gw_kcgi_error(kerr);
603 if (gw_trans->gw_conf->got_show_repo_owner) {
604 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
605 "index_header_owner", KATTR__MAX);
607 return gw_kcgi_error(kerr);
608 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
610 return gw_kcgi_error(kerr);
611 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
613 return gw_kcgi_error(kerr);
616 if (gw_trans->gw_conf->got_show_repo_age) {
617 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
618 "index_header_age", KATTR__MAX);
620 return gw_kcgi_error(kerr);
621 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
623 return gw_kcgi_error(kerr);
624 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
626 return gw_kcgi_error(kerr);
629 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
631 return gw_kcgi_error(kerr);
633 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
634 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
635 "index_wrapper", KATTR__MAX);
637 return gw_kcgi_error(kerr);
638 kerr = khtml_printf(gw_trans->gw_html_req,
639 "No repositories found in %s",
640 gw_trans->gw_conf->got_repos_path);
642 return gw_kcgi_error(kerr);
643 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
645 return gw_kcgi_error(kerr);
646 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
647 "dotted_line", KATTR__MAX);
649 return gw_kcgi_error(kerr);
650 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
652 return gw_kcgi_error(kerr);
656 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
659 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
660 if (gw_trans->page > 0 && (gw_trans->page *
661 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
668 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
669 "index_wrapper", KATTR__MAX);
673 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
674 gw_dir->name, "action", "summary", NULL);
675 if (href_summary == NULL) {
676 error = got_error_from_errno("khttp_urlpart");
679 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
680 "index_project", KATTR__MAX);
683 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
684 href_summary, KATTR__MAX);
687 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
690 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
693 if (gw_trans->gw_conf->got_show_repo_description) {
694 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
695 KATTR_ID, "index_project_description", KATTR__MAX);
698 kerr = khtml_puts(gw_trans->gw_html_req,
699 gw_dir->description ? gw_dir->description : "");
702 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
706 if (gw_trans->gw_conf->got_show_repo_owner) {
707 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
708 KATTR_ID, "index_project_owner", KATTR__MAX);
711 kerr = khtml_puts(gw_trans->gw_html_req,
712 gw_dir->owner ? gw_dir->owner : "");
715 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
719 if (gw_trans->gw_conf->got_show_repo_age) {
720 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
721 KATTR_ID, "index_project_age", KATTR__MAX);
724 kerr = khtml_puts(gw_trans->gw_html_req,
725 gw_dir->age ? gw_dir->age : "");
728 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
733 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
734 "navs_wrapper", KATTR__MAX);
737 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
742 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
743 href_summary, KATTR__MAX);
746 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
749 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
753 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
757 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
758 gw_dir->name, "action", "briefs", NULL);
759 if (href_briefs == NULL) {
760 error = got_error_from_errno("khttp_urlpart");
763 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
764 href_briefs, KATTR__MAX);
767 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
770 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
772 error = gw_kcgi_error(kerr);
774 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
778 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
779 gw_dir->name, "action", "commits", NULL);
780 if (href_commits == NULL) {
781 error = got_error_from_errno("khttp_urlpart");
784 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
785 href_commits, KATTR__MAX);
788 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
791 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
795 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
799 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
800 gw_dir->name, "action", "tags", NULL);
801 if (href_tags == NULL) {
802 error = got_error_from_errno("khttp_urlpart");
805 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
806 href_tags, KATTR__MAX);
809 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
812 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
816 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
820 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
821 gw_dir->name, "action", "tree", NULL);
822 if (href_tree == NULL) {
823 error = got_error_from_errno("khttp_urlpart");
826 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
827 href_tree, KATTR__MAX);
830 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
834 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
837 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
838 "dotted_line", KATTR__MAX);
841 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
856 if (gw_trans->gw_conf->got_max_repos_display == 0)
859 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
860 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
861 (gw_trans->page > 0) &&
862 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
863 prev_disp == gw_trans->repos_total))) {
864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
865 KATTR_ID, "np_wrapper", KATTR__MAX);
868 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
869 KATTR_ID, "nav_prev", KATTR__MAX);
874 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
875 (gw_trans->page > 0) &&
876 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
877 prev_disp == gw_trans->repos_total)) {
878 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
879 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
880 if (href_prev == NULL) {
881 error = got_error_from_errno("khttp_urlpartx");
884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
885 KATTR_HREF, href_prev, KATTR__MAX);
888 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
891 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
896 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
898 return gw_kcgi_error(kerr);
900 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
901 next_disp == gw_trans->gw_conf->got_max_repos_display &&
902 dir_c != (gw_trans->page + 1) *
903 gw_trans->gw_conf->got_max_repos_display) {
904 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
905 KATTR_ID, "nav_next", KATTR__MAX);
908 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
909 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
910 if (href_next == NULL) {
911 error = got_error_from_errno("khttp_urlpartx");
914 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
915 KATTR_HREF, href_next, KATTR__MAX);
918 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
921 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
928 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
929 (gw_trans->page > 0) &&
930 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
931 prev_disp == gw_trans->repos_total)) {
932 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
946 if (error == NULL && kerr != KCGI_OK)
947 error = gw_kcgi_error(kerr);
951 static const struct got_error *
952 gw_commits(struct gw_trans *gw_trans)
954 const struct got_error *error = NULL;
955 struct gw_header *header = NULL, *n_header = NULL;
956 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
957 char *href_prev = NULL, *href_next = NULL;
958 enum kcgi_err kerr = KCGI_OK;
960 if ((header = gw_init_header()) == NULL)
961 return got_error_from_errno("malloc");
963 if (pledge("stdio rpath proc exec sendfd unveil",
965 error = got_error_from_errno("pledge");
969 error = gw_apply_unveil(gw_trans->gw_dir->path);
973 error = gw_get_header(gw_trans, header,
974 gw_trans->gw_conf->got_max_commits_display);
978 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
980 "commits_line_wrapper", KATTR__MAX);
983 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
987 error = gw_gen_author_header(gw_trans, n_header->author);
990 error = gw_gen_committer_header(gw_trans, n_header->author);
993 error = gw_get_time_str(&age, n_header->committer_time,
997 error = gw_gen_age_header(gw_trans, age ?age : "");
1000 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1001 if (kerr != KCGI_OK)
1004 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1005 "dotted_line", KATTR__MAX);
1006 if (kerr != KCGI_OK)
1008 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1009 if (kerr != KCGI_OK)
1012 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1013 "commit", KATTR__MAX);
1014 if (kerr != KCGI_OK)
1016 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1017 if (kerr != KCGI_OK)
1019 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1020 if (kerr != KCGI_OK)
1023 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1024 gw_trans->repo_name, "action", "diff", "commit",
1025 n_header->commit_id, NULL);
1026 if (href_diff == NULL) {
1027 error = got_error_from_errno("khttp_urlpart");
1030 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1031 KATTR_ID, "navs_wrapper", KATTR__MAX);
1032 if (kerr != KCGI_OK)
1034 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1035 KATTR_ID, "navs", KATTR__MAX);
1036 if (kerr != KCGI_OK)
1038 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1039 KATTR_HREF, href_diff, KATTR__MAX);
1040 if (kerr != KCGI_OK)
1042 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1043 if (kerr != KCGI_OK)
1045 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1046 if (kerr != KCGI_OK)
1049 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1050 if (kerr != KCGI_OK)
1053 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1054 gw_trans->repo_name, "action", "tree", "commit",
1055 n_header->commit_id, NULL);
1056 if (href_tree == NULL) {
1057 error = got_error_from_errno("khttp_urlpart");
1060 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1061 KATTR_HREF, href_tree, KATTR__MAX);
1062 if (kerr != KCGI_OK)
1064 khtml_puts(gw_trans->gw_html_req, "tree");
1065 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1066 if (kerr != KCGI_OK)
1068 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1069 if (kerr != KCGI_OK)
1072 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1073 "solid_line", KATTR__MAX);
1074 if (kerr != KCGI_OK)
1076 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1077 if (kerr != KCGI_OK)
1084 if (gw_trans->next_id || gw_trans->page > 0) {
1085 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1086 KATTR_ID, "np_wrapper", KATTR__MAX);
1087 if (kerr != KCGI_OK)
1089 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1090 KATTR_ID, "nav_prev", KATTR__MAX);
1091 if (kerr != KCGI_OK)
1095 if (gw_trans->page > 0 && gw_trans->prev_id) {
1096 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1097 KATTRX_STRING, gw_trans->repo_name, "page",
1098 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1099 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1100 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1101 KATTRX_STRING, gw_trans->prev_prev_id ?
1102 gw_trans->prev_prev_id : "", NULL);
1103 if (href_prev == NULL) {
1104 error = got_error_from_errno("khttp_urlpartx");
1107 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1108 KATTR_HREF, href_prev, KATTR__MAX);
1109 if (kerr != KCGI_OK)
1111 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1112 if (kerr != KCGI_OK)
1114 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1115 if (kerr != KCGI_OK)
1119 if (gw_trans->next_id || gw_trans->page > 0) {
1120 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1121 if (kerr != KCGI_OK)
1122 return gw_kcgi_error(kerr);
1125 if (gw_trans->next_id) {
1126 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1127 KATTR_ID, "nav_next", KATTR__MAX);
1128 if (kerr != KCGI_OK)
1130 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1131 KATTRX_STRING, gw_trans->repo_name, "page",
1132 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1133 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1134 gw_trans->next_id, "prev", KATTRX_STRING,
1135 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1136 "prev_prev", KATTRX_STRING, gw_trans->prev_prev_id ?
1137 gw_trans->prev_prev_id : "", NULL);
1138 if (href_next == NULL) {
1139 error = got_error_from_errno("khttp_urlpartx");
1142 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1143 KATTR_HREF, href_next, KATTR__MAX);
1144 if (kerr != KCGI_OK)
1146 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1147 if (kerr != KCGI_OK)
1149 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1150 if (kerr != KCGI_OK)
1154 if (gw_trans->next_id || gw_trans->page > 0) {
1155 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1156 if (kerr != KCGI_OK)
1160 gw_free_header(header);
1161 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1162 gw_free_header(n_header);
1168 if (error == NULL && kerr != KCGI_OK)
1169 error = gw_kcgi_error(kerr);
1173 static const struct got_error *
1174 gw_briefs(struct gw_trans *gw_trans)
1176 const struct got_error *error = NULL;
1177 struct gw_header *header = NULL, *n_header = NULL;
1178 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1179 char *href_prev = NULL, *href_next = NULL;
1180 char *newline, *smallerthan;
1181 enum kcgi_err kerr = KCGI_OK;
1183 if ((header = gw_init_header()) == NULL)
1184 return got_error_from_errno("malloc");
1186 if (pledge("stdio rpath proc exec sendfd unveil",
1188 error = got_error_from_errno("pledge");
1192 if (gw_trans->action != GW_SUMMARY) {
1193 error = gw_apply_unveil(gw_trans->gw_dir->path);
1198 if (gw_trans->action == GW_SUMMARY)
1199 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1201 error = gw_get_header(gw_trans, header,
1202 gw_trans->gw_conf->got_max_commits_display);
1206 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1207 error = gw_get_time_str(&age, n_header->committer_time,
1212 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1213 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1214 if (kerr != KCGI_OK)
1217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1218 KATTR_ID, "briefs_age", KATTR__MAX);
1219 if (kerr != KCGI_OK)
1221 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1222 if (kerr != KCGI_OK)
1224 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1225 if (kerr != KCGI_OK)
1228 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1229 KATTR_ID, "briefs_author", KATTR__MAX);
1230 if (kerr != KCGI_OK)
1232 smallerthan = strchr(n_header->author, '<');
1234 *smallerthan = '\0';
1235 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1236 if (kerr != KCGI_OK)
1238 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1239 if (kerr != KCGI_OK)
1242 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1243 gw_trans->repo_name, "action", "diff", "commit",
1244 n_header->commit_id, NULL);
1245 if (href_diff == NULL) {
1246 error = got_error_from_errno("khttp_urlpart");
1249 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1250 KATTR_ID, "briefs_log", KATTR__MAX);
1251 if (kerr != KCGI_OK)
1253 newline = strchr(n_header->commit_msg, '\n');
1256 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1257 KATTR_HREF, href_diff, KATTR__MAX);
1258 if (kerr != KCGI_OK)
1260 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1261 if (kerr != KCGI_OK)
1263 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1264 if (kerr != KCGI_OK)
1267 if (n_header->refs_str) {
1268 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1269 if (kerr != KCGI_OK)
1271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1272 KATTR_ID, "refs_str", KATTR__MAX);
1273 if (kerr != KCGI_OK)
1275 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1276 n_header->refs_str);
1277 if (kerr != KCGI_OK)
1279 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1280 if (kerr != KCGI_OK)
1284 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1285 if (kerr != KCGI_OK)
1288 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1289 KATTR_ID, "navs_wrapper", KATTR__MAX);
1290 if (kerr != KCGI_OK)
1292 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1293 KATTR_ID, "navs", KATTR__MAX);
1294 if (kerr != KCGI_OK)
1296 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1297 KATTR_HREF, href_diff, KATTR__MAX);
1298 if (kerr != KCGI_OK)
1300 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1301 if (kerr != KCGI_OK)
1303 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1304 if (kerr != KCGI_OK)
1307 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1308 if (kerr != KCGI_OK)
1311 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1312 gw_trans->repo_name, "action", "tree", "commit",
1313 n_header->commit_id, NULL);
1314 if (href_tree == NULL) {
1315 error = got_error_from_errno("khttp_urlpart");
1318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1319 KATTR_HREF, href_tree, KATTR__MAX);
1320 if (kerr != KCGI_OK)
1322 khtml_puts(gw_trans->gw_html_req, "tree");
1323 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1324 if (kerr != KCGI_OK)
1326 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1327 if (kerr != KCGI_OK)
1330 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1331 KATTR_ID, "dotted_line", KATTR__MAX);
1332 if (kerr != KCGI_OK)
1334 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1335 if (kerr != KCGI_OK)
1346 if (gw_trans->next_id || gw_trans->page > 0) {
1347 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1348 KATTR_ID, "np_wrapper", KATTR__MAX);
1349 if (kerr != KCGI_OK)
1351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1352 KATTR_ID, "nav_prev", KATTR__MAX);
1353 if (kerr != KCGI_OK)
1357 if (gw_trans->page > 0 && gw_trans->prev_id) {
1358 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1359 KATTRX_STRING, gw_trans->repo_name, "page",
1360 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1361 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1362 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1363 KATTRX_STRING, gw_trans->prev_prev_id ?
1364 gw_trans->prev_prev_id : "", NULL);
1365 if (href_prev == NULL) {
1366 error = got_error_from_errno("khttp_urlpartx");
1369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1370 KATTR_HREF, href_prev, KATTR__MAX);
1371 if (kerr != KCGI_OK)
1373 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1374 if (kerr != KCGI_OK)
1376 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1377 if (kerr != KCGI_OK)
1381 if (gw_trans->next_id || gw_trans->page > 0) {
1382 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1383 if (kerr != KCGI_OK)
1384 return gw_kcgi_error(kerr);
1387 if (gw_trans->next_id) {
1388 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1389 KATTR_ID, "nav_next", KATTR__MAX);
1390 if (kerr != KCGI_OK)
1393 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1394 KATTRX_STRING, gw_trans->repo_name, "page",
1395 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1396 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1397 gw_trans->next_id, "prev", KATTRX_STRING,
1398 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1399 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1400 gw_trans->prev_id : "", NULL);
1401 if (href_next == NULL) {
1402 error = got_error_from_errno("khttp_urlpartx");
1405 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1406 KATTR_HREF, href_next, KATTR__MAX);
1407 if (kerr != KCGI_OK)
1409 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1410 if (kerr != KCGI_OK)
1412 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1413 if (kerr != KCGI_OK)
1417 if (gw_trans->next_id || gw_trans->page > 0) {
1418 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1419 if (kerr != KCGI_OK)
1423 gw_free_header(header);
1424 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1425 gw_free_header(n_header);
1431 if (error == NULL && kerr != KCGI_OK)
1432 error = gw_kcgi_error(kerr);
1436 static const struct got_error *
1437 gw_summary(struct gw_trans *gw_trans)
1439 const struct got_error *error = NULL;
1441 enum kcgi_err kerr = KCGI_OK;
1443 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1444 return got_error_from_errno("pledge");
1446 error = gw_apply_unveil(gw_trans->gw_dir->path);
1450 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1451 "summary_wrapper", KATTR__MAX);
1452 if (kerr != KCGI_OK)
1453 return gw_kcgi_error(kerr);
1455 if (gw_trans->gw_conf->got_show_repo_description &&
1456 gw_trans->gw_dir->description != NULL &&
1457 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1458 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1459 KATTR_ID, "description_title", KATTR__MAX);
1460 if (kerr != KCGI_OK)
1462 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1463 if (kerr != KCGI_OK)
1465 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1466 if (kerr != KCGI_OK)
1468 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1469 KATTR_ID, "description", KATTR__MAX);
1470 if (kerr != KCGI_OK)
1472 kerr = khtml_puts(gw_trans->gw_html_req,
1473 gw_trans->gw_dir->description);
1474 if (kerr != KCGI_OK)
1476 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1477 if (kerr != KCGI_OK)
1481 if (gw_trans->gw_conf->got_show_repo_owner &&
1482 gw_trans->gw_dir->owner != NULL &&
1483 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1484 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1485 KATTR_ID, "repo_owner_title", KATTR__MAX);
1486 if (kerr != KCGI_OK)
1488 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1489 if (kerr != KCGI_OK)
1491 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1492 if (kerr != KCGI_OK)
1494 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1495 KATTR_ID, "repo_owner", KATTR__MAX);
1496 if (kerr != KCGI_OK)
1498 kerr = khtml_puts(gw_trans->gw_html_req,
1499 gw_trans->gw_dir->owner);
1500 if (kerr != KCGI_OK)
1502 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1503 if (kerr != KCGI_OK)
1507 if (gw_trans->gw_conf->got_show_repo_age) {
1508 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1513 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1514 KATTR_ID, "last_change_title", KATTR__MAX);
1515 if (kerr != KCGI_OK)
1517 kerr = khtml_puts(gw_trans->gw_html_req,
1519 if (kerr != KCGI_OK)
1521 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1522 if (kerr != KCGI_OK)
1524 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1525 KATTR_ID, "last_change", KATTR__MAX);
1526 if (kerr != KCGI_OK)
1528 kerr = khtml_puts(gw_trans->gw_html_req, age);
1529 if (kerr != KCGI_OK)
1531 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1532 if (kerr != KCGI_OK)
1537 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1538 gw_trans->gw_dir->url != NULL &&
1539 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1540 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1541 KATTR_ID, "cloneurl_title", KATTR__MAX);
1542 if (kerr != KCGI_OK)
1544 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1545 if (kerr != KCGI_OK)
1547 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1548 if (kerr != KCGI_OK)
1550 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1551 KATTR_ID, "cloneurl", KATTR__MAX);
1552 if (kerr != KCGI_OK)
1554 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1555 if (kerr != KCGI_OK)
1557 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1558 if (kerr != KCGI_OK)
1562 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1563 if (kerr != KCGI_OK)
1566 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1567 "briefs_title_wrapper", KATTR__MAX);
1568 if (kerr != KCGI_OK)
1570 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1571 "briefs_title", KATTR__MAX);
1572 if (kerr != KCGI_OK)
1574 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1575 if (kerr != KCGI_OK)
1577 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1578 if (kerr != KCGI_OK)
1580 error = gw_briefs(gw_trans);
1584 error = gw_tags(gw_trans);
1588 error = gw_output_repo_heads(gw_trans);
1591 if (error == NULL && kerr != KCGI_OK)
1592 error = gw_kcgi_error(kerr);
1596 static const struct got_error *
1597 gw_tree(struct gw_trans *gw_trans)
1599 const struct got_error *error = NULL;
1600 struct gw_header *header = NULL;
1601 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1603 enum kcgi_err kerr = KCGI_OK;
1605 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1606 return got_error_from_errno("pledge");
1608 if ((header = gw_init_header()) == NULL)
1609 return got_error_from_errno("malloc");
1611 error = gw_apply_unveil(gw_trans->gw_dir->path);
1615 error = gw_get_header(gw_trans, header, 1);
1619 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1620 "tree_header_wrapper", KATTR__MAX);
1621 if (kerr != KCGI_OK)
1623 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1624 "tree_header", KATTR__MAX);
1625 if (kerr != KCGI_OK)
1627 error = gw_gen_tree_header(gw_trans, header->tree_id);
1630 error = gw_get_time_str(&age, header->committer_time,
1634 error = gw_gen_age_header(gw_trans, age ?age : "");
1637 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1640 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1641 if (kerr != KCGI_OK)
1643 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1644 "dotted_line", KATTR__MAX);
1645 if (kerr != KCGI_OK)
1647 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1648 if (kerr != KCGI_OK)
1651 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1652 "tree", KATTR__MAX);
1653 if (kerr != KCGI_OK)
1655 error = gw_output_repo_tree(gw_trans);
1659 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1661 gw_free_header(header);
1662 free(tree_html_disp);
1666 if (error == NULL && kerr != KCGI_OK)
1667 error = gw_kcgi_error(kerr);
1671 static const struct got_error *
1672 gw_tags(struct gw_trans *gw_trans)
1674 const struct got_error *error = NULL;
1675 struct gw_header *header = NULL;
1676 char *href_next = NULL, *href_prev = NULL;
1677 enum kcgi_err kerr = KCGI_OK;
1679 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1680 return got_error_from_errno("pledge");
1682 if ((header = gw_init_header()) == NULL)
1683 return got_error_from_errno("malloc");
1685 if (gw_trans->action != GW_SUMMARY) {
1686 error = gw_apply_unveil(gw_trans->gw_dir->path);
1691 error = gw_get_header(gw_trans, header, 1);
1695 if (gw_trans->action == GW_SUMMARY) {
1696 gw_trans->next_id = NULL;
1697 error = gw_output_repo_tags(gw_trans, header,
1698 D_MAXSLCOMMDISP, TAGBRIEF);
1702 error = gw_output_repo_tags(gw_trans, header,
1703 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1708 if (gw_trans->next_id || gw_trans->page > 0) {
1709 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1710 KATTR_ID, "np_wrapper", KATTR__MAX);
1711 if (kerr != KCGI_OK)
1713 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1714 KATTR_ID, "nav_prev", KATTR__MAX);
1715 if (kerr != KCGI_OK)
1719 if (gw_trans->page > 0 && gw_trans->prev_id) {
1720 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1721 KATTRX_STRING, gw_trans->repo_name, "page",
1722 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1723 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1724 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1725 KATTRX_STRING, gw_trans->prev_prev_id ?
1726 gw_trans->prev_prev_id : "", NULL);
1727 if (href_prev == NULL) {
1728 error = got_error_from_errno("khttp_urlpartx");
1731 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1732 KATTR_HREF, href_prev, KATTR__MAX);
1733 if (kerr != KCGI_OK)
1735 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1736 if (kerr != KCGI_OK)
1738 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1739 if (kerr != KCGI_OK)
1743 if (gw_trans->next_id || gw_trans->page > 0) {
1744 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1745 if (kerr != KCGI_OK)
1746 return gw_kcgi_error(kerr);
1749 if (gw_trans->next_id) {
1750 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1751 KATTR_ID, "nav_next", KATTR__MAX);
1752 if (kerr != KCGI_OK)
1754 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1755 KATTRX_STRING, gw_trans->repo_name, "page",
1756 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1757 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1758 gw_trans->next_id, "prev", KATTRX_STRING,
1759 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1760 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1761 gw_trans->prev_id : "", NULL);
1762 if (href_next == NULL) {
1763 error = got_error_from_errno("khttp_urlpartx");
1766 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1767 KATTR_HREF, href_next, KATTR__MAX);
1768 if (kerr != KCGI_OK)
1770 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1771 if (kerr != KCGI_OK)
1773 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1774 if (kerr != KCGI_OK)
1778 if (gw_trans->next_id || gw_trans->page > 0) {
1779 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1780 if (kerr != KCGI_OK)
1784 gw_free_header(header);
1787 if (error == NULL && kerr != KCGI_OK)
1788 error = gw_kcgi_error(kerr);
1792 static const struct got_error *
1793 gw_tag(struct gw_trans *gw_trans)
1795 const struct got_error *error = NULL;
1796 struct gw_header *header = NULL;
1797 enum kcgi_err kerr = KCGI_OK;
1799 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1800 return got_error_from_errno("pledge");
1802 if ((header = gw_init_header()) == NULL)
1803 return got_error_from_errno("malloc");
1805 error = gw_apply_unveil(gw_trans->gw_dir->path);
1809 if (gw_trans->commit_id == NULL) {
1810 error = got_error_msg(GOT_ERR_QUERYSTRING,
1811 "commit required in querystring");
1815 error = gw_get_header(gw_trans, header, 1);
1819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1820 "tag_header_wrapper", KATTR__MAX);
1821 if (kerr != KCGI_OK)
1823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1824 "tag_header", KATTR__MAX);
1825 if (kerr != KCGI_OK)
1827 error = gw_gen_commit_header(gw_trans, header->commit_id,
1831 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1834 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1835 if (kerr != KCGI_OK)
1837 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1838 "dotted_line", KATTR__MAX);
1839 if (kerr != KCGI_OK)
1841 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1842 if (kerr != KCGI_OK)
1845 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1846 "tree", KATTR__MAX);
1847 if (kerr != KCGI_OK)
1850 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1854 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1856 gw_free_header(header);
1857 if (error == NULL && kerr != KCGI_OK)
1858 error = gw_kcgi_error(kerr);
1862 static const struct got_error *
1863 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1865 const struct got_error *error = NULL;
1870 if (asprintf(&dir_test, "%s/%s/%s",
1871 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1872 GOTWEB_GIT_DIR) == -1)
1873 return got_error_from_errno("asprintf");
1875 dt = opendir(dir_test);
1879 gw_dir->path = strdup(dir_test);
1880 if (gw_dir->path == NULL) {
1882 error = got_error_from_errno("strdup");
1889 if (asprintf(&dir_test, "%s/%s/%s",
1890 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1891 GOTWEB_GOT_DIR) == -1) {
1893 error = got_error_from_errno("asprintf");
1897 dt = opendir(dir_test);
1902 error = got_error(GOT_ERR_NOT_GIT_REPO);
1906 if (asprintf(&dir_test, "%s/%s",
1907 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1908 error = got_error_from_errno("asprintf");
1913 gw_dir->path = strdup(dir_test);
1914 if (gw_dir->path == NULL) {
1916 error = got_error_from_errno("strdup");
1920 dt = opendir(dir_test);
1922 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1927 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1931 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1934 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1938 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1942 if (dt && closedir(dt) == -1 && error == NULL)
1943 error = got_error_from_errno("closedir");
1947 static const struct got_error *
1948 gw_load_got_paths(struct gw_trans *gw_trans)
1950 const struct got_error *error = NULL;
1952 struct dirent **sd_dent;
1953 struct gw_dir *gw_dir;
1955 unsigned int d_cnt, d_i;
1957 d = opendir(gw_trans->gw_conf->got_repos_path);
1959 error = got_error_from_errno2("opendir",
1960 gw_trans->gw_conf->got_repos_path);
1964 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1967 error = got_error_from_errno2("scandir",
1968 gw_trans->gw_conf->got_repos_path);
1972 for (d_i = 0; d_i < d_cnt; d_i++) {
1973 if (gw_trans->gw_conf->got_max_repos > 0 &&
1974 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1975 break; /* account for parent and self */
1977 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1978 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1981 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1985 error = gw_load_got_path(gw_trans, gw_dir);
1986 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1993 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1994 !got_path_dir_is_empty(gw_dir->path)) {
1995 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1997 gw_trans->repos_total++;
2001 if (d && closedir(d) == -1 && error == NULL)
2002 error = got_error_from_errno("closedir");
2006 static const struct got_error *
2007 gw_parse_querystring(struct gw_trans *gw_trans)
2009 const struct got_error *error = NULL;
2011 struct gw_query_action *action = NULL;
2014 if (gw_trans->gw_req->fieldnmap[0]) {
2015 return got_error(GOT_ERR_QUERYSTRING);
2016 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2017 /* define gw_trans->repo_path */
2018 gw_trans->repo_name = p->parsed.s;
2020 if (asprintf(&gw_trans->repo_path, "%s/%s",
2021 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2022 return got_error_from_errno("asprintf");
2024 /* get action and set function */
2025 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2026 for (i = 0; i < nitems(gw_query_funcs); i++) {
2027 action = &gw_query_funcs[i];
2028 if (action->func_name == NULL)
2030 if (strcmp(action->func_name,
2031 p->parsed.s) == 0) {
2032 gw_trans->action = i;
2037 if (gw_trans->action == -1) {
2038 gw_trans->action = GW_ERR;
2039 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2040 p != NULL ? "bad action in querystring" :
2041 "no action in querystring");
2045 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2046 if (asprintf(&gw_trans->commit_id, "%s",
2048 return got_error_from_errno("asprintf");
2051 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2052 gw_trans->repo_file = p->parsed.s;
2054 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2055 if (asprintf(&gw_trans->repo_folder, "%s",
2057 return got_error_from_errno("asprintf");
2060 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2061 if (asprintf(&gw_trans->prev_id, "%s",
2063 return got_error_from_errno("asprintf");
2066 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
2067 if (asprintf(&gw_trans->prev_prev_id, "%s",
2069 return got_error_from_errno("asprintf");
2072 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2073 gw_trans->headref = p->parsed.s;
2075 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2079 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2081 gw_trans->action = GW_INDEX;
2083 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2084 gw_trans->page = p->parsed.i;
2089 static const struct got_error *
2090 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2092 const struct got_error *error;
2094 *gw_dir = malloc(sizeof(**gw_dir));
2095 if (*gw_dir == NULL)
2096 return got_error_from_errno("malloc");
2098 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2099 error = got_error_from_errno("asprintf");
2108 static const struct got_error *
2109 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2111 enum kcgi_err kerr = KCGI_OK;
2113 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2114 if (kerr != KCGI_OK)
2115 return gw_kcgi_error(kerr);
2116 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2118 if (kerr != KCGI_OK)
2119 return gw_kcgi_error(kerr);
2120 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2122 if (kerr != KCGI_OK)
2123 return gw_kcgi_error(kerr);
2124 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2126 if (kerr != KCGI_OK)
2127 return gw_kcgi_error(kerr);
2128 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2129 if (kerr != KCGI_OK)
2130 return gw_kcgi_error(kerr);
2131 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2133 if (kerr != KCGI_OK)
2134 return gw_kcgi_error(kerr);
2136 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2137 kerr = khttp_head(gw_trans->gw_req,
2138 kresps[KRESP_CONTENT_DISPOSITION],
2139 "attachment; filename=%s", gw_trans->repo_file);
2140 if (kerr != KCGI_OK)
2141 return gw_kcgi_error(kerr);
2144 kerr = khttp_body(gw_trans->gw_req);
2145 return gw_kcgi_error(kerr);
2148 static const struct got_error *
2149 gw_display_index(struct gw_trans *gw_trans)
2151 const struct got_error *error;
2152 enum kcgi_err kerr = KCGI_OK;
2154 /* catch early querystring errors */
2155 if (gw_trans->error)
2156 gw_trans->action = GW_ERR;
2158 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2162 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2163 if (kerr != KCGI_OK)
2164 return gw_kcgi_error(kerr);
2166 if (gw_trans->action != GW_BLOB) {
2167 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2168 gw_query_funcs[gw_trans->action].template);
2169 if (kerr != KCGI_OK) {
2170 khtml_close(gw_trans->gw_html_req);
2171 return gw_kcgi_error(kerr);
2175 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2178 static const struct got_error *
2179 gw_error(struct gw_trans *gw_trans)
2181 enum kcgi_err kerr = KCGI_OK;
2183 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2185 return gw_kcgi_error(kerr);
2189 gw_template(size_t key, void *arg)
2191 const struct got_error *error = NULL;
2192 enum kcgi_err kerr = KCGI_OK;
2193 struct gw_trans *gw_trans = arg;
2194 char *img_src = NULL;
2198 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2199 KATTR_NAME, "viewport",
2200 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2202 if (kerr != KCGI_OK)
2204 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2205 if (kerr != KCGI_OK)
2207 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2208 KATTR_CHARSET, "utf-8",
2210 if (kerr != KCGI_OK)
2212 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2213 if (kerr != KCGI_OK)
2215 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2216 KATTR_NAME, "msapplication-TileColor",
2217 KATTR_CONTENT, "#da532c", KATTR__MAX);
2218 if (kerr != KCGI_OK)
2220 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2221 if (kerr != KCGI_OK)
2223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2224 KATTR_NAME, "theme-color",
2225 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2226 if (kerr != KCGI_OK)
2228 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2229 if (kerr != KCGI_OK)
2231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2232 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2233 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
2234 if (kerr != KCGI_OK)
2236 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2237 if (kerr != KCGI_OK)
2239 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2240 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2241 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
2242 if (kerr != KCGI_OK)
2244 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2245 if (kerr != KCGI_OK)
2247 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2248 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2249 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
2250 if (kerr != KCGI_OK)
2252 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2253 if (kerr != KCGI_OK)
2255 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2256 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
2258 if (kerr != KCGI_OK)
2260 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2261 if (kerr != KCGI_OK)
2263 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2264 KATTR_REL, "mask-icon", KATTR_HREF,
2265 "/safari-pinned-tab.svg", KATTR__MAX);
2266 if (kerr != KCGI_OK)
2268 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2269 if (kerr != KCGI_OK)
2271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2272 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2273 KATTR_HREF, "/gotweb.css", KATTR__MAX);
2274 if (kerr != KCGI_OK)
2276 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2277 if (kerr != KCGI_OK)
2281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2282 KATTR_ID, "got_link", KATTR__MAX);
2283 if (kerr != KCGI_OK)
2285 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2286 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2287 KATTR_TARGET, "_sotd", KATTR__MAX);
2288 if (kerr != KCGI_OK)
2290 if (asprintf(&img_src, "/%s",
2291 gw_trans->gw_conf->got_logo) == -1)
2293 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2294 KATTR_SRC, img_src, KATTR__MAX);
2295 if (kerr != KCGI_OK) {
2299 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2300 if (kerr != KCGI_OK) {
2305 case (TEMPL_SITEPATH):
2306 error = gw_output_site_link(gw_trans);
2311 if (gw_trans->gw_conf->got_site_name != NULL) {
2312 kerr = khtml_puts(gw_trans->gw_html_req,
2313 gw_trans->gw_conf->got_site_name);
2314 if (kerr != KCGI_OK)
2318 case (TEMPL_SEARCH):
2320 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2321 "search", KATTR__MAX);
2322 if (kerr != KCGI_OK)
2324 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2325 KATTR_METHOD, "POST", KATTR__MAX);
2326 if (kerr != KCGI_OK)
2328 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2329 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2330 KATTR_MAXLENGTH, "50", KATTR__MAX);
2331 if (kerr != KCGI_OK)
2333 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2335 if (kerr != KCGI_OK)
2337 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2338 if (kerr != KCGI_OK)
2340 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2341 if (kerr != KCGI_OK)
2344 case(TEMPL_SITEOWNER):
2345 if (gw_trans->gw_conf->got_site_owner != NULL &&
2346 gw_trans->gw_conf->got_show_site_owner) {
2347 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2348 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2349 if (kerr != KCGI_OK)
2351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2352 KATTR_ID, "site_owner", KATTR__MAX);
2353 if (kerr != KCGI_OK)
2355 kerr = khtml_puts(gw_trans->gw_html_req,
2356 gw_trans->gw_conf->got_site_owner);
2357 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2358 if (kerr != KCGI_OK)
2362 case(TEMPL_CONTENT):
2363 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2365 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2366 KATTR_ID, "tmpl_err", KATTR__MAX);
2367 if (kerr != KCGI_OK)
2369 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2371 if (kerr != KCGI_OK)
2373 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2374 if (kerr != KCGI_OK)
2384 static const struct got_error *
2385 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2387 const struct got_error *error = NULL;
2388 enum kcgi_err kerr = KCGI_OK;
2390 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2391 KATTR_ID, "header_commit_title", KATTR__MAX);
2392 if (kerr != KCGI_OK)
2394 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2395 if (kerr != KCGI_OK)
2397 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2398 if (kerr != KCGI_OK)
2400 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2401 KATTR_ID, "header_commit", KATTR__MAX);
2402 if (kerr != KCGI_OK)
2404 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2405 if (kerr != KCGI_OK)
2408 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2409 KATTR_ID, "refs_str", KATTR__MAX);
2410 if (kerr != KCGI_OK)
2412 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2413 if (kerr != KCGI_OK)
2415 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2416 if (kerr != KCGI_OK)
2419 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2421 if (error == NULL && kerr != KCGI_OK)
2422 error = gw_kcgi_error(kerr);
2426 static const struct got_error *
2427 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2429 const struct got_error *error = NULL;
2430 enum kcgi_err kerr = KCGI_OK;
2432 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2433 KATTR_ID, "header_diff_title", KATTR__MAX);
2434 if (kerr != KCGI_OK)
2436 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2437 if (kerr != KCGI_OK)
2439 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2440 if (kerr != KCGI_OK)
2442 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2443 KATTR_ID, "header_diff", KATTR__MAX);
2444 if (kerr != KCGI_OK)
2447 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2448 if (kerr != KCGI_OK)
2451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2452 if (kerr != KCGI_OK)
2454 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2455 if (kerr != KCGI_OK)
2457 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2459 if (error == NULL && kerr != KCGI_OK)
2460 error = gw_kcgi_error(kerr);
2464 static const struct got_error *
2465 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2467 const struct got_error *error = NULL;
2468 enum kcgi_err kerr = KCGI_OK;
2470 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2471 KATTR_ID, "header_age_title", KATTR__MAX);
2472 if (kerr != KCGI_OK)
2474 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2475 if (kerr != KCGI_OK)
2477 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2478 if (kerr != KCGI_OK)
2480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2481 KATTR_ID, "header_age", KATTR__MAX);
2482 if (kerr != KCGI_OK)
2484 kerr = khtml_puts(gw_trans->gw_html_req, str);
2485 if (kerr != KCGI_OK)
2487 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2489 if (error == NULL && kerr != KCGI_OK)
2490 error = gw_kcgi_error(kerr);
2494 static const struct got_error *
2495 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2497 const struct got_error *error = NULL;
2498 enum kcgi_err kerr = KCGI_OK;
2500 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2501 KATTR_ID, "header_author_title", KATTR__MAX);
2502 if (kerr != KCGI_OK)
2504 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2505 if (kerr != KCGI_OK)
2507 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2508 if (kerr != KCGI_OK)
2510 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2511 KATTR_ID, "header_author", KATTR__MAX);
2512 if (kerr != KCGI_OK)
2514 kerr = khtml_puts(gw_trans->gw_html_req, str);
2515 if (kerr != KCGI_OK)
2517 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2519 if (error == NULL && kerr != KCGI_OK)
2520 error = gw_kcgi_error(kerr);
2524 static const struct got_error *
2525 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2527 const struct got_error *error = NULL;
2528 enum kcgi_err kerr = KCGI_OK;
2530 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2531 KATTR_ID, "header_committer_title", KATTR__MAX);
2532 if (kerr != KCGI_OK)
2534 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2535 if (kerr != KCGI_OK)
2537 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2538 if (kerr != KCGI_OK)
2540 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2541 KATTR_ID, "header_committer", KATTR__MAX);
2542 if (kerr != KCGI_OK)
2544 kerr = khtml_puts(gw_trans->gw_html_req, str);
2545 if (kerr != KCGI_OK)
2547 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2549 if (error == NULL && kerr != KCGI_OK)
2550 error = gw_kcgi_error(kerr);
2554 static const struct got_error *
2555 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2557 const struct got_error *error = NULL;
2558 enum kcgi_err kerr = KCGI_OK;
2560 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2561 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2562 if (kerr != KCGI_OK)
2564 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2565 if (kerr != KCGI_OK)
2567 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2568 if (kerr != KCGI_OK)
2570 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2571 KATTR_ID, "header_commit_msg", KATTR__MAX);
2572 if (kerr != KCGI_OK)
2574 kerr = khttp_puts(gw_trans->gw_req, str);
2575 if (kerr != KCGI_OK)
2577 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2579 if (error == NULL && kerr != KCGI_OK)
2580 error = gw_kcgi_error(kerr);
2584 static const struct got_error *
2585 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2587 const struct got_error *error = NULL;
2588 enum kcgi_err kerr = KCGI_OK;
2590 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2591 KATTR_ID, "header_tree_title", KATTR__MAX);
2592 if (kerr != KCGI_OK)
2594 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2595 if (kerr != KCGI_OK)
2597 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2598 if (kerr != KCGI_OK)
2600 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2601 KATTR_ID, "header_tree", KATTR__MAX);
2602 if (kerr != KCGI_OK)
2604 kerr = khtml_puts(gw_trans->gw_html_req, str);
2605 if (kerr != KCGI_OK)
2607 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2609 if (error == NULL && kerr != KCGI_OK)
2610 error = gw_kcgi_error(kerr);
2614 static const struct got_error *
2615 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2618 const struct got_error *error = NULL;
2620 char *d_file = NULL;
2624 *description = NULL;
2625 if (gw_trans->gw_conf->got_show_repo_description == 0)
2628 if (asprintf(&d_file, "%s/description", dir) == -1)
2629 return got_error_from_errno("asprintf");
2631 f = fopen(d_file, "r");
2633 if (errno == ENOENT || errno == EACCES)
2635 error = got_error_from_errno2("fopen", d_file);
2639 if (fseek(f, 0, SEEK_END) == -1) {
2640 error = got_ferror(f, GOT_ERR_IO);
2645 error = got_ferror(f, GOT_ERR_IO);
2648 if (fseek(f, 0, SEEK_SET) == -1) {
2649 error = got_ferror(f, GOT_ERR_IO);
2652 *description = calloc(len + 1, sizeof(**description));
2653 if (*description == NULL) {
2654 error = got_error_from_errno("calloc");
2658 n = fread(*description, 1, len, f);
2659 if (n == 0 && ferror(f))
2660 error = got_ferror(f, GOT_ERR_IO);
2662 if (f != NULL && fclose(f) == -1 && error == NULL)
2663 error = got_error_from_errno("fclose");
2668 static const struct got_error *
2669 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2673 char *years = "years ago", *months = "months ago";
2674 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2675 char *minutes = "minutes ago", *seconds = "seconds ago";
2676 char *now = "right now";
2684 diff_time = time(NULL) - committer_time;
2685 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2686 if (asprintf(repo_age, "%lld %s",
2687 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2688 return got_error_from_errno("asprintf");
2689 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2690 if (asprintf(repo_age, "%lld %s",
2691 (diff_time / 60 / 60 / 24 / (365 / 12)),
2693 return got_error_from_errno("asprintf");
2694 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2695 if (asprintf(repo_age, "%lld %s",
2696 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2697 return got_error_from_errno("asprintf");
2698 } else if (diff_time > 60 * 60 * 24 * 2) {
2699 if (asprintf(repo_age, "%lld %s",
2700 (diff_time / 60 / 60 / 24), days) == -1)
2701 return got_error_from_errno("asprintf");
2702 } else if (diff_time > 60 * 60 * 2) {
2703 if (asprintf(repo_age, "%lld %s",
2704 (diff_time / 60 / 60), hours) == -1)
2705 return got_error_from_errno("asprintf");
2706 } else if (diff_time > 60 * 2) {
2707 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2709 return got_error_from_errno("asprintf");
2710 } else if (diff_time > 2) {
2711 if (asprintf(repo_age, "%lld %s", diff_time,
2713 return got_error_from_errno("asprintf");
2715 if (asprintf(repo_age, "%s", now) == -1)
2716 return got_error_from_errno("asprintf");
2720 if (gmtime_r(&committer_time, &tm) == NULL)
2721 return got_error_from_errno("gmtime_r");
2723 s = asctime_r(&tm, datebuf);
2725 return got_error_from_errno("asctime_r");
2727 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2728 return got_error_from_errno("asprintf");
2734 static const struct got_error *
2735 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2736 const char *refname, int ref_tm)
2738 const struct got_error *error = NULL;
2739 struct got_repository *repo = NULL;
2740 struct got_commit_object *commit = NULL;
2741 struct got_reflist_head refs;
2742 struct got_reflist_entry *re;
2743 time_t committer_time = 0, cmp_time = 0;
2746 SIMPLEQ_INIT(&refs);
2748 if (gw_trans->gw_conf->got_show_repo_age == 0)
2752 repo = gw_trans->repo;
2754 error = got_repo_open(&repo, dir, NULL);
2759 error = got_ref_list(&refs, repo, "refs/heads",
2760 got_ref_cmp_by_name, NULL);
2765 * Find the youngest branch tip in the repository, or the age of
2766 * the a specific branch tip if a name was provided by the caller.
2768 SIMPLEQ_FOREACH(re, &refs, entry) {
2769 struct got_object_id *id = NULL;
2771 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2774 error = got_ref_resolve(&id, repo, re->ref);
2778 error = got_object_open_as_commit(&commit, repo, id);
2784 got_object_commit_get_committer_time(commit);
2785 got_object_commit_close(commit);
2786 if (cmp_time < committer_time)
2787 cmp_time = committer_time;
2793 if (cmp_time != 0) {
2794 committer_time = cmp_time;
2795 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2798 got_ref_list_free(&refs);
2799 if (gw_trans->repo == NULL)
2800 got_repo_close(repo);
2804 static const struct got_error *
2805 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2807 const struct got_error *error;
2809 struct got_object_id *id1 = NULL, *id2 = NULL;
2810 char *label1 = NULL, *label2 = NULL, *line = NULL;
2812 size_t linesize = 0;
2814 enum kcgi_err kerr = KCGI_OK;
2820 if (header->parent_id != NULL &&
2821 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2822 error = got_repo_match_object_id(&id1, &label1,
2823 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2828 error = got_repo_match_object_id(&id2, &label2,
2829 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2833 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2837 case GOT_OBJ_TYPE_BLOB:
2838 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2841 case GOT_OBJ_TYPE_TREE:
2842 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2845 case GOT_OBJ_TYPE_COMMIT:
2846 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2850 error = got_error(GOT_ERR_OBJ_TYPE);
2855 if (fseek(f, 0, SEEK_SET) == -1) {
2856 error = got_ferror(f, GOT_ERR_IO);
2860 while ((linelen = getline(&line, &linesize, f)) != -1) {
2861 error = gw_colordiff_line(gw_trans, line);
2864 /* XXX: KHTML_PRETTY breaks this */
2865 kerr = khtml_puts(gw_trans->gw_html_req, line);
2866 if (kerr != KCGI_OK)
2868 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2869 if (kerr != KCGI_OK)
2872 if (linelen == -1 && ferror(f))
2873 error = got_error_from_errno("getline");
2875 if (f && fclose(f) == -1 && error == NULL)
2876 error = got_error_from_errno("fclose");
2883 if (error == NULL && kerr != KCGI_OK)
2884 error = gw_kcgi_error(kerr);
2888 static const struct got_error *
2889 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2891 const struct got_error *error = NULL;
2892 struct got_repository *repo;
2893 const char *gitconfig_owner;
2897 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2900 error = got_repo_open(&repo, dir, NULL);
2903 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2904 if (gitconfig_owner) {
2905 *owner = strdup(gitconfig_owner);
2907 error = got_error_from_errno("strdup");
2909 got_repo_close(repo);
2913 static const struct got_error *
2914 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2916 const struct got_error *error = NULL;
2918 char *d_file = NULL;
2924 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2925 return got_error_from_errno("asprintf");
2927 f = fopen(d_file, "r");
2929 if (errno != ENOENT && errno != EACCES)
2930 error = got_error_from_errno2("fopen", d_file);
2934 if (fseek(f, 0, SEEK_END) == -1) {
2935 error = got_ferror(f, GOT_ERR_IO);
2940 error = got_ferror(f, GOT_ERR_IO);
2943 if (fseek(f, 0, SEEK_SET) == -1) {
2944 error = got_ferror(f, GOT_ERR_IO);
2948 *url = calloc(len + 1, sizeof(**url));
2950 error = got_error_from_errno("calloc");
2954 n = fread(*url, 1, len, f);
2955 if (n == 0 && ferror(f))
2956 error = got_ferror(f, GOT_ERR_IO);
2958 if (f && fclose(f) == -1 && error == NULL)
2959 error = got_error_from_errno("fclose");
2964 static const struct got_error *
2965 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2966 int limit, int tag_type)
2968 const struct got_error *error = NULL;
2969 struct got_reflist_head refs;
2970 struct got_reflist_entry *re;
2972 char *id_str = NULL, *newline, *href_commits = NULL;
2973 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2974 struct got_tag_object *tag = NULL;
2975 enum kcgi_err kerr = KCGI_OK;
2976 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
2977 int prev_set = 0, tag_count = 0;
2979 SIMPLEQ_INIT(&refs);
2981 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2982 got_ref_cmp_tags, gw_trans->repo);
2986 SIMPLEQ_FOREACH(re, &refs, entry) {
2987 const char *refname;
2989 const char *tag_commit;
2991 struct got_object_id *id;
2992 struct got_commit_object *commit = NULL;
2994 refname = got_ref_get_name(re->ref);
2995 if (strncmp(refname, "refs/tags/", 10) != 0)
2999 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3003 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3005 if (error->code != GOT_ERR_OBJ_TYPE) {
3009 /* "lightweight" tag */
3010 error = got_object_open_as_commit(&commit,
3011 gw_trans->repo, id);
3016 tagger = got_object_commit_get_committer(commit);
3018 got_object_commit_get_committer_time(commit);
3019 error = got_object_id_str(&id_str, id);
3023 tagger = got_object_tag_get_tagger(tag);
3024 tagger_time = got_object_tag_get_tagger_time(tag);
3025 error = got_object_id_str(&id_str,
3026 got_object_tag_get_object_id(tag));
3031 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3032 strlen(id_str)) != 0)
3035 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3036 start_tag == 0 && strncmp(id_str, header->commit_id,
3037 strlen(id_str)) != 0) {
3045 if (prev_set == 0 && start_tag == 1) {
3046 gw_trans->next_prev_id = strdup(id_str);
3047 if (gw_trans->next_prev_id == NULL) {
3048 error = got_error_from_errno("strdup");
3055 gw_trans->next_id = strdup(id_str);
3056 if (gw_trans->next_id == NULL)
3057 error = got_error_from_errno("strdup");
3062 error = got_object_commit_get_logmsg(&tag_commit0,
3066 got_object_commit_close(commit);
3068 tag_commit0 = strdup(got_object_tag_get_message(tag));
3069 if (tag_commit0 == NULL) {
3070 error = got_error_from_errno("strdup");
3075 tag_commit = tag_commit0;
3076 while (*tag_commit == '\n')
3081 newline = strchr(tag_commit, '\n');
3085 if (summary_header_displayed == 0) {
3086 kerr = khtml_attr(gw_trans->gw_html_req,
3087 KELEM_DIV, KATTR_ID,
3088 "summary_tags_title_wrapper", KATTR__MAX);
3089 if (kerr != KCGI_OK)
3091 kerr = khtml_attr(gw_trans->gw_html_req,
3092 KELEM_DIV, KATTR_ID,
3093 "summary_tags_title", KATTR__MAX);
3094 if (kerr != KCGI_OK)
3096 kerr = khtml_puts(gw_trans->gw_html_req,
3098 if (kerr != KCGI_OK)
3100 kerr = khtml_closeelem(gw_trans->gw_html_req,
3102 if (kerr != KCGI_OK)
3104 kerr = khtml_attr(gw_trans->gw_html_req,
3105 KELEM_DIV, KATTR_ID,
3106 "summary_tags_content", KATTR__MAX);
3107 if (kerr != KCGI_OK)
3109 summary_header_displayed = 1;
3112 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3113 KATTR_ID, "tag_wrapper", KATTR__MAX);
3114 if (kerr != KCGI_OK)
3116 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3117 KATTR_ID, "tag_age", KATTR__MAX);
3118 if (kerr != KCGI_OK)
3120 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3123 kerr = khtml_puts(gw_trans->gw_html_req,
3125 if (kerr != KCGI_OK)
3127 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3128 if (kerr != KCGI_OK)
3130 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3131 KATTR_ID, "tag", KATTR__MAX);
3132 if (kerr != KCGI_OK)
3134 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3135 if (kerr != KCGI_OK)
3137 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3138 if (kerr != KCGI_OK)
3140 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3141 KATTR_ID, "tag_name", KATTR__MAX);
3142 if (kerr != KCGI_OK)
3145 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3146 gw_trans->repo_name, "action", "tag", "commit",
3148 if (href_tag == NULL) {
3149 error = got_error_from_errno("khttp_urlpart");
3152 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3153 KATTR_HREF, href_tag, KATTR__MAX);
3154 if (kerr != KCGI_OK)
3156 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3157 if (kerr != KCGI_OK)
3159 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3160 if (kerr != KCGI_OK)
3163 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3164 KATTR_ID, "navs_wrapper", KATTR__MAX);
3165 if (kerr != KCGI_OK)
3167 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3168 KATTR_ID, "navs", KATTR__MAX);
3169 if (kerr != KCGI_OK)
3172 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3173 KATTR_HREF, href_tag, KATTR__MAX);
3174 if (kerr != KCGI_OK)
3176 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3177 if (kerr != KCGI_OK)
3179 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3180 if (kerr != KCGI_OK)
3183 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3184 if (kerr != KCGI_OK)
3187 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3188 "path", gw_trans->repo_name, "action", "briefs",
3189 "commit", id_str, NULL);
3190 if (href_briefs == NULL) {
3191 error = got_error_from_errno("khttp_urlpart");
3194 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3195 KATTR_HREF, href_briefs, KATTR__MAX);
3196 if (kerr != KCGI_OK)
3198 kerr = khtml_puts(gw_trans->gw_html_req,
3200 if (kerr != KCGI_OK)
3202 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3203 if (kerr != KCGI_OK)
3206 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3207 if (kerr != KCGI_OK)
3210 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3211 "path", gw_trans->repo_name, "action", "commits",
3212 "commit", id_str, NULL);
3213 if (href_commits == NULL) {
3214 error = got_error_from_errno("khttp_urlpart");
3217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3218 KATTR_HREF, href_commits, KATTR__MAX);
3219 if (kerr != KCGI_OK)
3221 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3222 if (kerr != KCGI_OK)
3224 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3225 if (kerr != KCGI_OK)
3228 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3229 KATTR_ID, "dotted_line", KATTR__MAX);
3230 if (kerr != KCGI_OK)
3232 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3233 if (kerr != KCGI_OK)
3237 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3238 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3239 if (kerr != KCGI_OK)
3241 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3242 if (kerr != KCGI_OK)
3244 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3245 if (kerr != KCGI_OK)
3247 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3248 KATTR_ID, "tag_info_date", KATTR__MAX);
3249 if (kerr != KCGI_OK)
3251 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3254 kerr = khtml_puts(gw_trans->gw_html_req,
3256 if (kerr != KCGI_OK)
3258 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3259 if (kerr != KCGI_OK)
3262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3263 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3264 if (kerr != KCGI_OK)
3266 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3267 if (kerr != KCGI_OK)
3269 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3270 if (kerr != KCGI_OK)
3272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3273 KATTR_ID, "tag_info_date", KATTR__MAX);
3274 if (kerr != KCGI_OK)
3276 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3277 if (kerr != KCGI_OK)
3279 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3280 if (kerr != KCGI_OK)
3283 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3284 KATTR_ID, "tag_info", KATTR__MAX);
3285 if (kerr != KCGI_OK)
3287 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3288 if (kerr != KCGI_OK)
3294 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3295 if (kerr != KCGI_OK)
3298 if (limit && --limit == 0)
3302 got_object_tag_close(tag);
3315 href_commits = NULL;
3317 if (tag_count == 0) {
3318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3319 "summary_tags_title_wrapper", KATTR__MAX);
3320 if (kerr != KCGI_OK)
3322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3323 "summary_tags_title", KATTR__MAX);
3324 if (kerr != KCGI_OK)
3326 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3327 if (kerr != KCGI_OK)
3329 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3330 if (kerr != KCGI_OK)
3332 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3333 "summary_tags_content", KATTR__MAX);
3334 if (kerr != KCGI_OK)
3336 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3337 "tags_info", KATTR__MAX);
3338 if (kerr != KCGI_OK)
3340 kerr = khttp_puts(gw_trans->gw_req,
3341 "There are no tags for this repo.");
3342 if (kerr != KCGI_OK)
3344 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3348 got_object_tag_close(tag);
3355 got_ref_list_free(&refs);
3356 if (error == NULL && kerr != KCGI_OK)
3357 error = gw_kcgi_error(kerr);
3362 gw_free_header(struct gw_header *header)
3365 free(header->author);
3366 free(header->committer);
3367 free(header->refs_str);
3368 free(header->commit_id);
3369 free(header->parent_id);
3370 free(header->tree_id);
3371 free(header->commit_msg);
3374 static struct gw_header *
3377 struct gw_header *header;
3379 header = malloc(sizeof(*header));
3383 header->path = NULL;
3384 SIMPLEQ_INIT(&header->refs);
3386 header->refs_str = NULL;
3387 header->commit_id = NULL;
3388 header->committer = NULL;
3389 header->author = NULL;
3390 header->parent_id = NULL;
3391 header->tree_id = NULL;
3392 header->commit_msg = NULL;
3397 static const struct got_error *
3398 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3399 int limit, struct got_object_id *id)
3401 const struct got_error *error = NULL;
3402 struct got_commit_graph *graph = NULL;
3403 struct got_commit_object *commit = NULL;
3404 int chk_next = 0, chk_multi = 0, prev_set = 0;
3406 error = got_commit_graph_open(&graph, header->path, 0);
3410 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3416 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3419 if (error->code == GOT_ERR_ITER_COMPLETED)
3426 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3429 if (limit == 1 && chk_multi == 0 &&
3430 gw_trans->gw_conf->got_max_commits_display != 1) {
3431 error = gw_get_commit(gw_trans, header, commit, id);
3436 struct gw_header *n_header = NULL;
3437 if ((n_header = gw_init_header()) == NULL) {
3438 error = got_error_from_errno("malloc");
3441 error = got_ref_list(&n_header->refs, gw_trans->repo,
3442 NULL, got_ref_cmp_by_name, NULL);
3446 error = gw_get_commit(gw_trans, n_header, commit, id);
3449 got_ref_list_free(&n_header->refs);
3452 * we have a commit_id now, so copy it to next_prev_id
3453 * for navigation through gw_briefs and gw_commits
3455 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3456 (gw_trans->action == GW_BRIEFS ||
3457 gw_trans->action == GW_COMMITS ||
3458 gw_trans->action == GW_SUMMARY)) {
3460 gw_trans->next_prev_id =
3461 strdup(n_header->commit_id);
3462 if (gw_trans->next_prev_id == NULL) {
3463 error = got_error_from_errno("strdup");
3469 * check for one more commit before breaking,
3470 * so we know whether to navicate through gw_briefs
3471 * gw_commits and gw_summary
3473 if (chk_next && (gw_trans->action == GW_BRIEFS||
3474 gw_trans->action == GW_COMMITS ||
3475 gw_trans->action == GW_SUMMARY)) {
3476 gw_trans->next_id = strdup(n_header->commit_id);
3477 if (gw_trans->next_id == NULL)
3478 error = got_error_from_errno("strdup");
3482 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3485 if (error || (limit && --limit == 0)) {
3493 got_object_commit_close(commit);
3495 got_commit_graph_close(graph);
3499 static const struct got_error *
3500 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3501 struct got_commit_object *commit, struct got_object_id *id)
3503 const struct got_error *error = NULL;
3504 struct got_reflist_entry *re;
3505 struct got_object_id *id2 = NULL;
3506 struct got_object_qid *parent_id;
3507 char *commit_msg = NULL, *commit_msg0;
3510 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3513 struct got_tag_object *tag = NULL;
3516 if (got_ref_is_symbolic(re->ref))
3519 name = got_ref_get_name(re->ref);
3520 if (strncmp(name, "refs/", 5) == 0)
3522 if (strncmp(name, "got/", 4) == 0)
3524 if (strncmp(name, "heads/", 6) == 0)
3526 if (strncmp(name, "remotes/", 8) == 0)
3528 if (strncmp(name, "tags/", 5) == 0) {
3529 error = got_object_open_as_tag(&tag, gw_trans->repo,
3532 if (error->code != GOT_ERR_OBJ_TYPE)
3535 * Ref points at something other
3542 cmp = got_object_id_cmp(tag ?
3543 got_object_tag_get_object_id(tag) : re->id, id);
3545 got_object_tag_close(tag);
3548 s = header->refs_str;
3549 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3550 s ? ", " : "", name) == -1) {
3551 error = got_error_from_errno("asprintf");
3553 header->refs_str = NULL;
3559 error = got_object_id_str(&header->commit_id, id);
3563 error = got_object_id_str(&header->tree_id,
3564 got_object_commit_get_tree_id(commit));
3568 if (gw_trans->action == GW_DIFF) {
3569 parent_id = SIMPLEQ_FIRST(
3570 got_object_commit_get_parent_ids(commit));
3571 if (parent_id != NULL) {
3572 id2 = got_object_id_dup(parent_id->id);
3574 error = got_object_id_str(&header->parent_id, id2);
3579 header->parent_id = strdup("/dev/null");
3580 if (header->parent_id == NULL) {
3581 error = got_error_from_errno("strdup");
3587 header->committer_time =
3588 got_object_commit_get_committer_time(commit);
3591 strdup(got_object_commit_get_author(commit));
3592 if (header->author == NULL) {
3593 error = got_error_from_errno("strdup");
3597 strdup(got_object_commit_get_committer(commit));
3598 if (header->committer == NULL) {
3599 error = got_error_from_errno("strdup");
3602 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3606 commit_msg = commit_msg0;
3607 while (*commit_msg == '\n')
3610 header->commit_msg = strdup(commit_msg);
3611 if (header->commit_msg == NULL)
3612 error = got_error_from_errno("strdup");
3617 static const struct got_error *
3618 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3620 const struct got_error *error = NULL;
3621 char *in_repo_path = NULL;
3622 struct got_object_id *id = NULL;
3624 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3628 if (gw_trans->commit_id == NULL) {
3629 struct got_reference *head_ref;
3630 error = got_ref_open(&head_ref, gw_trans->repo,
3631 gw_trans->headref, 0);
3635 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3636 got_ref_close(head_ref);
3640 struct got_reference *ref;
3642 error = got_ref_open(&ref, gw_trans->repo,
3643 gw_trans->commit_id, 0);
3644 if (error == NULL) {
3646 error = got_ref_resolve(&id, gw_trans->repo, ref);
3650 error = got_object_get_type(&obj_type, gw_trans->repo,
3654 if (obj_type == GOT_OBJ_TYPE_TAG) {
3655 struct got_tag_object *tag;
3656 error = got_object_open_as_tag(&tag,
3657 gw_trans->repo, id);
3660 if (got_object_tag_get_object_type(tag) !=
3661 GOT_OBJ_TYPE_COMMIT) {
3662 got_object_tag_close(tag);
3663 error = got_error(GOT_ERR_OBJ_TYPE);
3667 id = got_object_id_dup(
3668 got_object_tag_get_object_id(tag));
3670 error = got_error_from_errno(
3671 "got_object_id_dup");
3672 got_object_tag_close(tag);
3675 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3676 error = got_error(GOT_ERR_OBJ_TYPE);
3680 error = got_repo_match_object_id_prefix(&id,
3681 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3687 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3688 gw_trans->repo_path, 1);
3693 header->path = strdup(in_repo_path);
3694 if (header->path == NULL) {
3695 error = got_error_from_errno("strdup");
3700 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3701 got_ref_cmp_by_name, NULL);
3705 error = gw_get_commits(gw_trans, header, limit, id);
3707 got_ref_list_free(&header->refs);
3717 char datebuf[11]; /* YYYY-MM-DD + NUL */
3720 struct gw_blame_cb_args {
3721 struct blame_line *lines;
3725 off_t *line_offsets;
3727 struct got_repository *repo;
3728 struct gw_trans *gw_trans;
3731 static const struct got_error *
3732 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3734 const struct got_error *err = NULL;
3735 struct gw_blame_cb_args *a = arg;
3736 struct blame_line *bline;
3738 size_t linesize = 0;
3739 struct got_commit_object *commit = NULL;
3742 time_t committer_time;
3743 enum kcgi_err kerr = KCGI_OK;
3745 if (nlines != a->nlines ||
3746 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3747 return got_error(GOT_ERR_RANGE);
3750 return NULL; /* no change in this commit */
3752 /* Annotate this line. */
3753 bline = &a->lines[lineno - 1];
3754 if (bline->annotated)
3756 err = got_object_id_str(&bline->id_str, id);
3760 err = got_object_open_as_commit(&commit, a->repo, id);
3764 bline->committer = strdup(got_object_commit_get_committer(commit));
3765 if (bline->committer == NULL) {
3766 err = got_error_from_errno("strdup");
3770 committer_time = got_object_commit_get_committer_time(commit);
3771 if (localtime_r(&committer_time, &tm) == NULL)
3772 return got_error_from_errno("localtime_r");
3773 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3774 &tm) >= sizeof(bline->datebuf)) {
3775 err = got_error(GOT_ERR_NO_SPACE);
3778 bline->annotated = 1;
3780 /* Print lines annotated so far. */
3781 bline = &a->lines[a->lineno_cur - 1];
3782 if (!bline->annotated)
3785 offset = a->line_offsets[a->lineno_cur - 1];
3786 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3787 err = got_error_from_errno("fseeko");
3791 while (bline->annotated) {
3792 char *smallerthan, *at, *nl, *committer;
3793 char *href_diff = NULL;
3796 if (getline(&line, &linesize, a->f) == -1) {
3798 err = got_error_from_errno("getline");
3802 committer = bline->committer;
3803 smallerthan = strchr(committer, '<');
3804 if (smallerthan && smallerthan[1] != '\0')
3805 committer = smallerthan + 1;
3806 at = strchr(committer, '@');
3809 len = strlen(committer);
3811 committer[8] = '\0';
3813 nl = strchr(line, '\n');
3817 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3818 "blame_wrapper", KATTR__MAX);
3819 if (kerr != KCGI_OK)
3821 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3822 "blame_number", KATTR__MAX);
3823 if (kerr != KCGI_OK)
3825 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3826 a->nlines_prec, a->lineno_cur);
3827 if (kerr != KCGI_OK)
3829 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3830 if (kerr != KCGI_OK)
3833 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3834 "blame_hash", KATTR__MAX);
3835 if (kerr != KCGI_OK)
3838 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3839 a->gw_trans->repo_name, "action", "diff", "commit",
3840 bline->id_str, NULL);
3841 if (href_diff == NULL) {
3842 err = got_error_from_errno("khttp_urlpart");
3845 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3846 KATTR_HREF, href_diff, KATTR__MAX);
3847 if (kerr != KCGI_OK)
3849 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3851 if (kerr != KCGI_OK)
3853 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3854 if (kerr != KCGI_OK)
3857 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3858 "blame_date", KATTR__MAX);
3859 if (kerr != KCGI_OK)
3861 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3862 if (kerr != KCGI_OK)
3864 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3865 if (kerr != KCGI_OK)
3868 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3869 "blame_author", KATTR__MAX);
3870 if (kerr != KCGI_OK)
3872 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3873 if (kerr != KCGI_OK)
3875 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3876 if (kerr != KCGI_OK)
3879 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3880 "blame_code", KATTR__MAX);
3881 if (kerr != KCGI_OK)
3883 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3884 if (kerr != KCGI_OK)
3886 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3887 if (kerr != KCGI_OK)
3890 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3891 if (kerr != KCGI_OK)
3895 bline = &a->lines[a->lineno_cur - 1];
3901 got_object_commit_close(commit);
3903 if (err == NULL && kerr != KCGI_OK)
3904 err = gw_kcgi_error(kerr);
3908 static const struct got_error *
3909 gw_output_file_blame(struct gw_trans *gw_trans)
3911 const struct got_error *error = NULL;
3912 struct got_object_id *obj_id = NULL;
3913 struct got_object_id *commit_id = NULL;
3914 struct got_blob_object *blob = NULL;
3915 char *path = NULL, *in_repo_path = NULL;
3916 struct gw_blame_cb_args bca;
3920 if (asprintf(&path, "%s%s%s",
3921 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3922 gw_trans->repo_folder ? "/" : "",
3923 gw_trans->repo_file) == -1) {
3924 error = got_error_from_errno("asprintf");
3928 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3932 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3933 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3937 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3942 if (obj_id == NULL) {
3943 error = got_error(GOT_ERR_NO_OBJ);
3947 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3951 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3952 error = got_error(GOT_ERR_OBJ_TYPE);
3956 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3960 bca.f = got_opentemp();
3961 if (bca.f == NULL) {
3962 error = got_error_from_errno("got_opentemp");
3965 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3966 &bca.line_offsets, bca.f, blob);
3967 if (error || bca.nlines == 0)
3970 /* Don't include \n at EOF in the blame line count. */
3971 if (bca.line_offsets[bca.nlines - 1] == filesize)
3974 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3975 if (bca.lines == NULL) {
3976 error = got_error_from_errno("calloc");
3980 bca.nlines_prec = 0;
3986 bca.repo = gw_trans->repo;
3987 bca.gw_trans = gw_trans;
3989 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3998 free(bca.line_offsets);
3999 for (i = 0; i < bca.nlines; i++) {
4000 struct blame_line *bline = &bca.lines[i];
4001 free(bline->id_str);
4002 free(bline->committer);
4005 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4006 error = got_error_from_errno("fclose");
4009 got_object_blob_close(blob);
4013 static const struct got_error *
4014 gw_output_blob_buf(struct gw_trans *gw_trans)
4016 const struct got_error *error = NULL;
4017 struct got_object_id *obj_id = NULL;
4018 struct got_object_id *commit_id = NULL;
4019 struct got_blob_object *blob = NULL;
4020 char *path = NULL, *in_repo_path = NULL;
4021 int obj_type, set_mime = 0;
4024 enum kcgi_err kerr = KCGI_OK;
4026 if (asprintf(&path, "%s%s%s",
4027 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4028 gw_trans->repo_folder ? "/" : "",
4029 gw_trans->repo_file) == -1) {
4030 error = got_error_from_errno("asprintf");
4034 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
4038 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4039 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
4043 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4048 if (obj_id == NULL) {
4049 error = got_error(GOT_ERR_NO_OBJ);
4053 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4057 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4058 error = got_error(GOT_ERR_OBJ_TYPE);
4062 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4066 hdrlen = got_object_blob_get_hdrlen(blob);
4068 error = got_object_blob_read_block(&len, blob);
4071 buf = got_object_blob_get_read_buf(blob);
4074 * Skip blob object header first time around,
4075 * which also contains a zero byte.
4078 if (set_mime == 0) {
4079 if (isbinary(buf, len - hdrlen))
4080 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4082 gw_trans->mime = KMIME_TEXT_PLAIN;
4084 error = gw_display_index(gw_trans);
4088 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4089 if (kerr != KCGI_OK)
4099 got_object_blob_close(blob);
4100 if (error == NULL && kerr != KCGI_OK)
4101 error = gw_kcgi_error(kerr);
4105 static const struct got_error *
4106 gw_output_repo_tree(struct gw_trans *gw_trans)
4108 const struct got_error *error = NULL;
4109 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4110 struct got_tree_object *tree = NULL;
4111 char *path = NULL, *in_repo_path = NULL;
4112 char *id_str = NULL;
4113 char *build_folder = NULL;
4114 char *href_blob = NULL, *href_blame = NULL;
4115 const char *class = NULL;
4116 int nentries, i, class_flip = 0;
4117 enum kcgi_err kerr = KCGI_OK;
4119 if (gw_trans->repo_folder != NULL) {
4120 path = strdup(gw_trans->repo_folder);
4122 error = got_error_from_errno("strdup");
4126 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4127 gw_trans->repo_path, 1);
4131 path = in_repo_path;
4134 if (gw_trans->commit_id == NULL) {
4135 struct got_reference *head_ref;
4136 error = got_ref_open(&head_ref, gw_trans->repo,
4137 gw_trans->headref, 0);
4140 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4143 got_ref_close(head_ref);
4145 * gw_trans->commit_id was not parsed from the querystring
4146 * we hit this code path from gw_index, where we don't know the
4147 * commit values for the tree link yet, so set
4148 * gw_trans->commit_id here to continue further into the tree
4150 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4155 error = got_repo_match_object_id(&commit_id, NULL,
4156 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
4162 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4167 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4171 nentries = got_object_tree_get_nentries(tree);
4172 for (i = 0; i < nentries; i++) {
4173 struct got_tree_entry *te;
4174 const char *modestr = "";
4177 te = got_object_tree_get_entry(tree, i);
4179 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4183 mode = got_tree_entry_get_mode(te);
4184 if (got_object_tree_entry_is_submodule(te))
4186 else if (S_ISLNK(mode))
4188 else if (S_ISDIR(mode))
4190 else if (mode & S_IXUSR)
4193 if (class_flip == 0) {
4194 class = "back_lightgray";
4197 class = "back_white";
4201 if (S_ISDIR(mode)) {
4202 if (asprintf(&build_folder, "%s/%s",
4203 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4204 got_tree_entry_get_name(te)) == -1) {
4205 error = got_error_from_errno("asprintf");
4209 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4210 gw_trans->repo_name, "action",
4211 gw_get_action_name(gw_trans), "commit",
4212 gw_trans->commit_id, "folder", build_folder, NULL);
4213 if (href_blob == NULL) {
4214 error = got_error_from_errno("khttp_urlpart");
4217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4218 KATTR_ID, "tree_wrapper", KATTR__MAX);
4219 if (kerr != KCGI_OK)
4221 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4222 KATTR_ID, "tree_line", KATTR_CLASS, class,
4224 if (kerr != KCGI_OK)
4226 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4227 KATTR_HREF, href_blob, KATTR_CLASS,
4228 "diff_directory", KATTR__MAX);
4229 if (kerr != KCGI_OK)
4231 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4232 got_tree_entry_get_name(te), modestr);
4233 if (kerr != KCGI_OK)
4235 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4236 if (kerr != KCGI_OK)
4238 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4239 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4241 if (kerr != KCGI_OK)
4243 kerr = khtml_entity(gw_trans->gw_html_req,
4245 if (kerr != KCGI_OK)
4247 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4248 if (kerr != KCGI_OK)
4251 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4252 gw_trans->repo_name, "action", "blob", "commit",
4253 gw_trans->commit_id, "file",
4254 got_tree_entry_get_name(te), "folder",
4255 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4257 if (href_blob == NULL) {
4258 error = got_error_from_errno("khttp_urlpart");
4261 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4262 gw_trans->repo_name, "action", "blame", "commit",
4263 gw_trans->commit_id, "file",
4264 got_tree_entry_get_name(te), "folder",
4265 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4267 if (href_blame == NULL) {
4268 error = got_error_from_errno("khttp_urlpart");
4271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4272 KATTR_ID, "tree_wrapper", KATTR__MAX);
4273 if (kerr != KCGI_OK)
4275 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4276 KATTR_ID, "tree_line", KATTR_CLASS, class,
4278 if (kerr != KCGI_OK)
4280 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4281 KATTR_HREF, href_blob, KATTR__MAX);
4282 if (kerr != KCGI_OK)
4284 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4285 got_tree_entry_get_name(te), modestr);
4286 if (kerr != KCGI_OK)
4288 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4289 if (kerr != KCGI_OK)
4291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4292 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4294 if (kerr != KCGI_OK)
4297 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4298 KATTR_HREF, href_blob, KATTR__MAX);
4299 if (kerr != KCGI_OK)
4301 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4302 if (kerr != KCGI_OK)
4304 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4305 if (kerr != KCGI_OK)
4308 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4309 if (kerr != KCGI_OK)
4312 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4313 KATTR_HREF, href_blame, KATTR__MAX);
4314 if (kerr != KCGI_OK)
4316 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4317 if (kerr != KCGI_OK)
4320 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4321 if (kerr != KCGI_OK)
4329 build_folder = NULL;
4333 got_object_tree_close(tree);
4340 if (error == NULL && kerr != KCGI_OK)
4341 error = gw_kcgi_error(kerr);
4345 static const struct got_error *
4346 gw_output_repo_heads(struct gw_trans *gw_trans)
4348 const struct got_error *error = NULL;
4349 struct got_reflist_head refs;
4350 struct got_reflist_entry *re;
4351 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4352 char *href_commits = NULL;
4353 enum kcgi_err kerr = KCGI_OK;
4355 SIMPLEQ_INIT(&refs);
4357 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4358 got_ref_cmp_by_name, NULL);
4362 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4363 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4364 if (kerr != KCGI_OK)
4366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4367 KATTR_ID, "summary_heads_title", KATTR__MAX);
4368 if (kerr != KCGI_OK)
4370 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4371 if (kerr != KCGI_OK)
4373 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4374 if (kerr != KCGI_OK)
4376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4377 KATTR_ID, "summary_heads_content", KATTR__MAX);
4378 if (kerr != KCGI_OK)
4381 SIMPLEQ_FOREACH(re, &refs, entry) {
4382 const char *refname;
4384 if (got_ref_is_symbolic(re->ref))
4387 refname = got_ref_get_name(re->ref);
4388 if (strncmp(refname, "refs/heads/", 11) != 0)
4391 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4396 if (strncmp(refname, "refs/heads/", 11) == 0)
4399 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4400 KATTR_ID, "heads_wrapper", KATTR__MAX);
4401 if (kerr != KCGI_OK)
4403 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4404 KATTR_ID, "heads_age", KATTR__MAX);
4405 if (kerr != KCGI_OK)
4407 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4408 if (kerr != KCGI_OK)
4410 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4411 if (kerr != KCGI_OK)
4413 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4414 KATTR_ID, "heads_space", KATTR__MAX);
4415 if (kerr != KCGI_OK)
4417 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4418 if (kerr != KCGI_OK)
4420 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4421 if (kerr != KCGI_OK)
4423 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4424 KATTR_ID, "head", KATTR__MAX);
4425 if (kerr != KCGI_OK)
4428 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4429 gw_trans->repo_name, "action", "summary", "headref",
4431 if (href_summary == NULL) {
4432 error = got_error_from_errno("khttp_urlpart");
4435 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4436 href_summary, KATTR__MAX);
4437 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4438 if (kerr != KCGI_OK)
4440 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4441 if (kerr != KCGI_OK)
4444 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4445 "navs_wrapper", KATTR__MAX);
4446 if (kerr != KCGI_OK)
4448 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4449 "navs", KATTR__MAX);
4450 if (kerr != KCGI_OK)
4453 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4454 href_summary, KATTR__MAX);
4455 if (kerr != KCGI_OK)
4457 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4458 if (kerr != KCGI_OK)
4460 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4461 if (kerr != KCGI_OK)
4464 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4465 if (kerr != KCGI_OK)
4468 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4469 gw_trans->repo_name, "action", "briefs", "headref",
4471 if (href_briefs == NULL) {
4472 error = got_error_from_errno("khttp_urlpart");
4475 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4476 href_briefs, KATTR__MAX);
4477 if (kerr != KCGI_OK)
4479 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4480 if (kerr != KCGI_OK)
4482 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4483 if (kerr != KCGI_OK)
4486 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4487 if (kerr != KCGI_OK)
4490 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4491 gw_trans->repo_name, "action", "commits", "headref",
4493 if (href_commits == NULL) {
4494 error = got_error_from_errno("khttp_urlpart");
4497 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4498 href_commits, KATTR__MAX);
4499 if (kerr != KCGI_OK)
4501 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4502 if (kerr != KCGI_OK)
4504 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4505 if (kerr != KCGI_OK)
4508 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4509 "dotted_line", KATTR__MAX);
4510 if (kerr != KCGI_OK)
4512 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4513 if (kerr != KCGI_OK)
4516 href_summary = NULL;
4520 href_commits = NULL;
4523 got_ref_list_free(&refs);
4530 static const struct got_error *
4531 gw_output_site_link(struct gw_trans *gw_trans)
4533 const struct got_error *error = NULL;
4534 char *href_summary = NULL;
4535 enum kcgi_err kerr = KCGI_OK;
4537 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4538 "site_link", KATTR__MAX);
4539 if (kerr != KCGI_OK)
4541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4543 if (kerr != KCGI_OK)
4545 kerr = khtml_puts(gw_trans->gw_html_req,
4546 gw_trans->gw_conf->got_site_link);
4547 if (kerr != KCGI_OK)
4549 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4550 if (kerr != KCGI_OK)
4553 if (gw_trans->repo_name != NULL) {
4554 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4555 if (kerr != KCGI_OK)
4558 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4559 gw_trans->repo_name, "action", "summary", NULL);
4560 if (href_summary == NULL) {
4561 error = got_error_from_errno("khttp_urlpart");
4564 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4565 href_summary, KATTR__MAX);
4566 if (kerr != KCGI_OK)
4568 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4569 if (kerr != KCGI_OK)
4571 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4572 if (kerr != KCGI_OK)
4574 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4575 gw_get_action_name(gw_trans));
4576 if (kerr != KCGI_OK)
4580 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4581 if (kerr != KCGI_OK)
4585 if (error == NULL && kerr != KCGI_OK)
4586 error = gw_kcgi_error(kerr);
4590 static const struct got_error *
4591 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4593 const struct got_error *error = NULL;
4595 enum kcgi_err kerr = KCGI_OK;
4597 if (strncmp(buf, "-", 1) == 0)
4598 color = "diff_minus";
4599 else if (strncmp(buf, "+", 1) == 0)
4600 color = "diff_plus";
4601 else if (strncmp(buf, "@@", 2) == 0)
4602 color = "diff_chunk_header";
4603 else if (strncmp(buf, "@@", 2) == 0)
4604 color = "diff_chunk_header";
4605 else if (strncmp(buf, "commit +", 8) == 0)
4606 color = "diff_meta";
4607 else if (strncmp(buf, "commit -", 8) == 0)
4608 color = "diff_meta";
4609 else if (strncmp(buf, "blob +", 6) == 0)
4610 color = "diff_meta";
4611 else if (strncmp(buf, "blob -", 6) == 0)
4612 color = "diff_meta";
4613 else if (strncmp(buf, "file +", 6) == 0)
4614 color = "diff_meta";
4615 else if (strncmp(buf, "file -", 6) == 0)
4616 color = "diff_meta";
4617 else if (strncmp(buf, "from:", 5) == 0)
4618 color = "diff_author";
4619 else if (strncmp(buf, "via:", 4) == 0)
4620 color = "diff_author";
4621 else if (strncmp(buf, "date:", 5) == 0)
4622 color = "diff_date";
4623 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4624 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4625 if (error == NULL && kerr != KCGI_OK)
4626 error = gw_kcgi_error(kerr);
4631 main(int argc, char *argv[])
4633 const struct got_error *error = NULL;
4634 struct gw_trans *gw_trans;
4635 struct gw_dir *dir = NULL, *tdir;
4636 const char *page = "index";
4638 enum kcgi_err kerr = KCGI_OK;
4640 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4643 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4646 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4649 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4652 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4653 if (kerr != KCGI_OK) {
4654 error = gw_kcgi_error(kerr);
4658 if ((gw_trans->gw_conf =
4659 malloc(sizeof(struct gotweb_conf))) == NULL) {
4661 error = got_error_from_errno("malloc");
4665 TAILQ_INIT(&gw_trans->gw_dirs);
4666 TAILQ_INIT(&gw_trans->gw_headers);
4668 gw_trans->action = -1;
4670 gw_trans->repos_total = 0;
4671 gw_trans->repo_path = NULL;
4672 gw_trans->commit_id = NULL;
4673 gw_trans->next_id = NULL;
4674 gw_trans->next_prev_id = NULL;
4675 gw_trans->prev_id = NULL;
4676 gw_trans->prev_prev_id = NULL;
4677 gw_trans->headref = GOT_REF_HEAD;
4678 gw_trans->mime = KMIME_TEXT_HTML;
4679 gw_trans->gw_tmpl->key = gw_templs;
4680 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4681 gw_trans->gw_tmpl->arg = gw_trans;
4682 gw_trans->gw_tmpl->cb = gw_template;
4683 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4687 error = gw_parse_querystring(gw_trans);
4691 if (gw_trans->action == GW_BLOB)
4692 error = gw_blob(gw_trans);
4694 error = gw_display_index(gw_trans);
4697 free(gw_trans->gw_conf->got_repos_path);
4698 free(gw_trans->gw_conf->got_site_name);
4699 free(gw_trans->gw_conf->got_site_owner);
4700 free(gw_trans->gw_conf->got_site_link);
4701 free(gw_trans->gw_conf->got_logo);
4702 free(gw_trans->gw_conf->got_logo_url);
4703 free(gw_trans->gw_conf);
4704 free(gw_trans->commit_id);
4705 free(gw_trans->next_id);
4706 free(gw_trans->next_prev_id);
4707 free(gw_trans->prev_id);
4708 free(gw_trans->prev_prev_id);
4709 free(gw_trans->repo_path);
4711 got_repo_close(gw_trans->repo);
4713 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4715 free(dir->description);
4724 khttp_free(gw_trans->gw_req);