2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
20 #include <sys/types.h>
34 #include <got_error.h>
35 #include <got_object.h>
36 #include <got_reference.h>
37 #include <got_repository.h>
39 #include <got_cancel.h>
40 #include <got_worktree.h>
42 #include <got_commit_graph.h>
43 #include <got_blame.h>
44 #include <got_privsep.h>
45 #include <got_opentemp.h>
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 TAILQ_HEAD(headers, gw_header) gw_headers;
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct got_repository *repo;
60 struct gw_dir *gw_dir;
61 struct gotweb_config *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
65 const struct got_error *error;
66 const char *repo_name;
73 const char *repo_file;
78 unsigned int repos_total;
83 TAILQ_ENTRY(gw_header) entry;
84 struct got_reflist_head refs;
88 char *commit_id; /* id_str1 */
89 char *parent_id; /* id_str2 */
94 time_t committer_time;
98 TAILQ_ENTRY(gw_dir) entry;
141 static const char *const gw_templs[TEMPL__MAX] = {
151 static const struct kvalid gw_keys[KEY__ZMAX] = {
152 { kvalid_stringne, "action" },
153 { kvalid_stringne, "commit" },
154 { kvalid_stringne, "file" },
155 { kvalid_stringne, "folder" },
156 { kvalid_stringne, "headref" },
157 { kvalid_int, "page" },
158 { kvalid_stringne, "path" },
159 { kvalid_stringne, "prev" },
160 { kvalid_stringne, "prev_prev" },
163 static struct gw_header *gw_init_header(void);
165 static void gw_free_header(struct gw_header *);
167 static int gw_template(size_t, void *);
169 static const struct got_error *gw_error(struct gw_trans *);
170 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
171 static const struct got_error *gw_get_repo_description(char **,
172 struct gw_trans *, char *);
173 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
175 static const struct got_error *gw_get_time_str(char **, time_t, int);
176 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
177 char *, const char *, int);
178 static const struct got_error *gw_output_file_blame(struct gw_trans *);
179 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
180 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
181 static const struct got_error *gw_output_diff(struct gw_trans *,
183 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
184 struct gw_header *, int, int);
185 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
186 static const struct got_error *gw_output_site_link(struct gw_trans *);
187 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
189 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
191 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
193 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
195 static const struct got_error *gw_gen_author_header(struct gw_trans *,
197 static const struct got_error *gw_gen_age_header(struct gw_trans *,
199 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
201 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
203 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
204 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
206 static const struct got_error *gw_display_index(struct gw_trans *);
207 static const struct got_error *gw_get_header(struct gw_trans *,
208 struct gw_header *, int);
209 static const struct got_error *gw_get_commits(struct gw_trans *,
210 struct gw_header *, int,
211 struct got_object_id *);
212 static const struct got_error *gw_get_commit(struct gw_trans *,
214 struct got_commit_object *,
215 struct got_object_id *);
216 static const struct got_error *gw_apply_unveil(const char *);
217 static const struct got_error *gw_blame_cb(void *, int, int,
218 struct got_object_id *);
219 static const struct got_error *gw_load_got_paths(struct gw_trans *);
220 static const struct got_error *gw_load_got_path(struct gw_trans *,
222 static const struct got_error *gw_parse_querystring(struct gw_trans *);
223 static const struct got_error *gw_blame(struct gw_trans *);
224 static const struct got_error *gw_blob(struct gw_trans *);
225 static const struct got_error *gw_diff(struct gw_trans *);
226 static const struct got_error *gw_index(struct gw_trans *);
227 static const struct got_error *gw_commits(struct gw_trans *);
228 static const struct got_error *gw_briefs(struct gw_trans *);
229 static const struct got_error *gw_summary(struct gw_trans *);
230 static const struct got_error *gw_tree(struct gw_trans *);
231 static const struct got_error *gw_tag(struct gw_trans *);
232 static const struct got_error *gw_tags(struct gw_trans *);
234 struct gw_query_action {
235 unsigned int func_id;
236 const char *func_name;
237 const struct got_error *(*func_main)(struct gw_trans *);
241 enum gw_query_actions {
255 static struct gw_query_action gw_query_funcs[] = {
256 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
257 { GW_BLOB, "blob", NULL, NULL },
258 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
259 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
260 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
261 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
262 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
263 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
264 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
265 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
266 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
270 gw_get_action_name(struct gw_trans *gw_trans)
272 return gw_query_funcs[gw_trans->action].func_name;
275 static const struct got_error *
276 gw_kcgi_error(enum kcgi_err kerr)
281 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
282 return got_error(GOT_ERR_CANCELLED);
284 if (kerr == KCGI_ENOMEM)
285 return got_error_set_errno(ENOMEM,
286 kcgi_strerror(kerr));
288 if (kerr == KCGI_ENFILE)
289 return got_error_set_errno(ENFILE,
290 kcgi_strerror(kerr));
292 if (kerr == KCGI_EAGAIN)
293 return got_error_set_errno(EAGAIN,
294 kcgi_strerror(kerr));
296 if (kerr == KCGI_FORM)
297 return got_error_msg(GOT_ERR_IO,
298 kcgi_strerror(kerr));
300 return got_error_from_errno(kcgi_strerror(kerr));
303 static const struct got_error *
304 gw_apply_unveil(const char *repo_path)
306 const struct got_error *err;
308 if (repo_path && unveil(repo_path, "r") != 0)
309 return got_error_from_errno2("unveil", repo_path);
311 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
312 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
314 err = got_privsep_unveil_exec_helpers();
318 if (unveil(NULL, NULL) != 0)
319 return got_error_from_errno("unveil");
325 isbinary(const uint8_t *buf, size_t n)
329 for (i = 0; i < n; i++)
335 static const struct got_error *
336 gw_blame(struct gw_trans *gw_trans)
338 const struct got_error *error = NULL;
339 struct gw_header *header = NULL;
341 enum kcgi_err kerr = KCGI_OK;
343 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
345 return got_error_from_errno("pledge");
347 if ((header = gw_init_header()) == NULL)
348 return got_error_from_errno("malloc");
350 error = gw_apply_unveil(gw_trans->gw_dir->path);
354 /* check querystring */
355 if (gw_trans->repo_file == NULL) {
356 error = got_error_msg(GOT_ERR_QUERYSTRING,
357 "file required in querystring");
360 if (gw_trans->commit_id == NULL) {
361 error = got_error_msg(GOT_ERR_QUERYSTRING,
362 "commit required in querystring");
366 error = gw_get_header(gw_trans, header, 1);
369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
370 "blame_header_wrapper", KATTR__MAX);
373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
374 "blame_header", KATTR__MAX);
377 error = gw_get_time_str(&age, header->committer_time,
381 error = gw_gen_age_header(gw_trans, age ?age : "");
384 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
387 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
390 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
391 "dotted_line", KATTR__MAX);
394 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
398 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
399 "blame", KATTR__MAX);
402 error = gw_output_file_blame(gw_trans);
405 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
407 gw_free_header(header);
408 if (error == NULL && kerr != KCGI_OK)
409 error = gw_kcgi_error(kerr);
413 static const struct got_error *
414 gw_blob(struct gw_trans *gw_trans)
416 const struct got_error *error = NULL, *err = NULL;
417 struct gw_header *header = NULL;
418 enum kcgi_err kerr = KCGI_OK;
420 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
422 return got_error_from_errno("pledge");
424 if ((header = gw_init_header()) == NULL)
425 return got_error_from_errno("malloc");
427 error = gw_apply_unveil(gw_trans->gw_dir->path);
431 /* check querystring */
432 if (gw_trans->repo_file == NULL) {
433 error = got_error_msg(GOT_ERR_QUERYSTRING,
434 "file required in querystring");
437 if (gw_trans->commit_id == NULL) {
438 error = got_error_msg(GOT_ERR_QUERYSTRING,
439 "commit required in querystring");
442 error = gw_get_header(gw_trans, header, 1);
446 error = gw_output_blob_buf(gw_trans);
450 gw_trans->mime = KMIME_TEXT_PLAIN;
451 err = gw_display_index(gw_trans);
456 kerr = khttp_puts(gw_trans->gw_req, error->msg);
459 gw_free_header(header);
460 if (error == NULL && kerr != KCGI_OK)
461 error = gw_kcgi_error(kerr);
465 static const struct got_error *
466 gw_diff(struct gw_trans *gw_trans)
468 const struct got_error *error = NULL;
469 struct gw_header *header = NULL;
471 enum kcgi_err kerr = KCGI_OK;
473 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
475 return got_error_from_errno("pledge");
477 if ((header = gw_init_header()) == NULL)
478 return got_error_from_errno("malloc");
480 error = gw_apply_unveil(gw_trans->gw_dir->path);
484 error = gw_get_header(gw_trans, header, 1);
488 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
489 "diff_header_wrapper", KATTR__MAX);
492 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
493 "diff_header", KATTR__MAX);
496 error = gw_gen_diff_header(gw_trans, header->parent_id,
500 error = gw_gen_commit_header(gw_trans, header->commit_id,
504 error = gw_gen_tree_header(gw_trans, header->tree_id);
507 error = gw_gen_author_header(gw_trans, header->author);
510 error = gw_gen_committer_header(gw_trans, header->author);
513 error = gw_get_time_str(&age, header->committer_time,
517 error = gw_gen_age_header(gw_trans, age ?age : "");
520 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
523 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
526 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
527 "dotted_line", KATTR__MAX);
530 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
534 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
538 error = gw_output_diff(gw_trans, header);
542 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
544 gw_free_header(header);
546 if (error == NULL && kerr != KCGI_OK)
547 error = gw_kcgi_error(kerr);
551 static const struct got_error *
552 gw_index(struct gw_trans *gw_trans)
554 const struct got_error *error = NULL;
555 struct gw_dir *gw_dir = NULL;
556 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
557 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
558 char *href_tags = NULL;
559 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
560 enum kcgi_err kerr = KCGI_OK;
562 if (pledge("stdio rpath proc exec sendfd unveil",
564 error = got_error_from_errno("pledge");
568 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
572 error = gw_load_got_paths(gw_trans);
576 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
577 "index_header", KATTR__MAX);
579 return gw_kcgi_error(kerr);
580 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
581 "index_header_project", KATTR__MAX);
583 return gw_kcgi_error(kerr);
584 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
586 return gw_kcgi_error(kerr);
587 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
589 return gw_kcgi_error(kerr);
591 if (gw_trans->gw_conf->got_show_repo_description) {
592 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
593 "index_header_description", KATTR__MAX);
595 return gw_kcgi_error(kerr);
596 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
598 return gw_kcgi_error(kerr);
599 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
601 return gw_kcgi_error(kerr);
604 if (gw_trans->gw_conf->got_show_repo_owner) {
605 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
606 "index_header_owner", KATTR__MAX);
608 return gw_kcgi_error(kerr);
609 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
611 return gw_kcgi_error(kerr);
612 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
614 return gw_kcgi_error(kerr);
617 if (gw_trans->gw_conf->got_show_repo_age) {
618 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
619 "index_header_age", KATTR__MAX);
621 return gw_kcgi_error(kerr);
622 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
624 return gw_kcgi_error(kerr);
625 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
627 return gw_kcgi_error(kerr);
630 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
632 return gw_kcgi_error(kerr);
634 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
635 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
636 "index_wrapper", KATTR__MAX);
638 return gw_kcgi_error(kerr);
639 kerr = khtml_printf(gw_trans->gw_html_req,
640 "No repositories found in %s",
641 gw_trans->gw_conf->got_repos_path);
643 return gw_kcgi_error(kerr);
644 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
646 return gw_kcgi_error(kerr);
647 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
648 "dotted_line", KATTR__MAX);
650 return gw_kcgi_error(kerr);
651 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
653 return gw_kcgi_error(kerr);
657 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
660 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
661 if (gw_trans->page > 0 && (gw_trans->page *
662 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
669 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
670 "index_wrapper", KATTR__MAX);
674 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
675 gw_dir->name, "action", "summary", NULL);
676 if (href_summary == NULL) {
677 error = got_error_from_errno("khttp_urlpart");
680 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
681 "index_project", KATTR__MAX);
684 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
685 href_summary, KATTR__MAX);
688 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
691 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
694 if (gw_trans->gw_conf->got_show_repo_description) {
695 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
696 KATTR_ID, "index_project_description", KATTR__MAX);
699 kerr = khtml_puts(gw_trans->gw_html_req,
700 gw_dir->description ? gw_dir->description : "");
703 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
707 if (gw_trans->gw_conf->got_show_repo_owner) {
708 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
709 KATTR_ID, "index_project_owner", KATTR__MAX);
712 kerr = khtml_puts(gw_trans->gw_html_req,
713 gw_dir->owner ? gw_dir->owner : "");
716 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
720 if (gw_trans->gw_conf->got_show_repo_age) {
721 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
722 KATTR_ID, "index_project_age", KATTR__MAX);
725 kerr = khtml_puts(gw_trans->gw_html_req,
726 gw_dir->age ? gw_dir->age : "");
729 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
734 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
735 "navs_wrapper", KATTR__MAX);
738 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
743 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
744 href_summary, KATTR__MAX);
747 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
750 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
754 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
758 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
759 gw_dir->name, "action", "briefs", NULL);
760 if (href_briefs == NULL) {
761 error = got_error_from_errno("khttp_urlpart");
764 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
765 href_briefs, KATTR__MAX);
768 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
771 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
773 error = gw_kcgi_error(kerr);
775 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
779 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
780 gw_dir->name, "action", "commits", NULL);
781 if (href_commits == NULL) {
782 error = got_error_from_errno("khttp_urlpart");
785 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
786 href_commits, KATTR__MAX);
789 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
792 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
796 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
800 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
801 gw_dir->name, "action", "tags", NULL);
802 if (href_tags == NULL) {
803 error = got_error_from_errno("khttp_urlpart");
806 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
807 href_tags, KATTR__MAX);
810 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
813 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
817 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
821 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
822 gw_dir->name, "action", "tree", NULL);
823 if (href_tree == NULL) {
824 error = got_error_from_errno("khttp_urlpart");
827 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
828 href_tree, KATTR__MAX);
831 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
835 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
839 "dotted_line", KATTR__MAX);
842 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
857 if (gw_trans->gw_conf->got_max_repos_display == 0)
860 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
861 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
862 (gw_trans->page > 0) &&
863 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
864 prev_disp == gw_trans->repos_total))) {
865 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
866 KATTR_ID, "np_wrapper", KATTR__MAX);
869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
870 KATTR_ID, "nav_prev", KATTR__MAX);
875 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
876 (gw_trans->page > 0) &&
877 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
878 prev_disp == gw_trans->repos_total)) {
879 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
880 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
881 if (href_prev == NULL) {
882 error = got_error_from_errno("khttp_urlpartx");
885 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
886 KATTR_HREF, href_prev, KATTR__MAX);
889 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
892 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
897 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
899 return gw_kcgi_error(kerr);
901 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
902 next_disp == gw_trans->gw_conf->got_max_repos_display &&
903 dir_c != (gw_trans->page + 1) *
904 gw_trans->gw_conf->got_max_repos_display) {
905 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
906 KATTR_ID, "nav_next", KATTR__MAX);
909 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
910 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
911 if (href_next == NULL) {
912 error = got_error_from_errno("khttp_urlpartx");
915 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
916 KATTR_HREF, href_next, KATTR__MAX);
919 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
922 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
929 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
930 (gw_trans->page > 0) &&
931 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
932 prev_disp == gw_trans->repos_total)) {
933 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
947 if (error == NULL && kerr != KCGI_OK)
948 error = gw_kcgi_error(kerr);
952 static const struct got_error *
953 gw_commits(struct gw_trans *gw_trans)
955 const struct got_error *error = NULL;
956 struct gw_header *header = NULL, *n_header = NULL;
957 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
958 char *href_prev = NULL, *href_next = NULL;
959 enum kcgi_err kerr = KCGI_OK;
961 if ((header = gw_init_header()) == NULL)
962 return got_error_from_errno("malloc");
964 if (pledge("stdio rpath proc exec sendfd unveil",
966 error = got_error_from_errno("pledge");
970 error = gw_apply_unveil(gw_trans->gw_dir->path);
974 error = gw_get_header(gw_trans, header,
975 gw_trans->gw_conf->got_max_commits_display);
979 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
980 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
981 "commits_line_wrapper", KATTR__MAX);
984 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
988 error = gw_gen_author_header(gw_trans, n_header->author);
991 error = gw_gen_committer_header(gw_trans, n_header->author);
994 error = gw_get_time_str(&age, n_header->committer_time,
998 error = gw_gen_age_header(gw_trans, age ?age : "");
1001 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1002 if (kerr != KCGI_OK)
1005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1006 "dotted_line", KATTR__MAX);
1007 if (kerr != KCGI_OK)
1009 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1010 if (kerr != KCGI_OK)
1013 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1014 "commit", KATTR__MAX);
1015 if (kerr != KCGI_OK)
1017 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1018 if (kerr != KCGI_OK)
1020 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1021 if (kerr != KCGI_OK)
1024 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1025 gw_trans->repo_name, "action", "diff", "commit",
1026 n_header->commit_id, NULL);
1027 if (href_diff == NULL) {
1028 error = got_error_from_errno("khttp_urlpart");
1031 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1032 KATTR_ID, "navs_wrapper", KATTR__MAX);
1033 if (kerr != KCGI_OK)
1035 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1036 KATTR_ID, "navs", KATTR__MAX);
1037 if (kerr != KCGI_OK)
1039 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1040 KATTR_HREF, href_diff, KATTR__MAX);
1041 if (kerr != KCGI_OK)
1043 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1044 if (kerr != KCGI_OK)
1046 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1047 if (kerr != KCGI_OK)
1050 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1051 if (kerr != KCGI_OK)
1054 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1055 gw_trans->repo_name, "action", "tree", "commit",
1056 n_header->commit_id, NULL);
1057 if (href_tree == NULL) {
1058 error = got_error_from_errno("khttp_urlpart");
1061 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1062 KATTR_HREF, href_tree, KATTR__MAX);
1063 if (kerr != KCGI_OK)
1065 khtml_puts(gw_trans->gw_html_req, "tree");
1066 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1067 if (kerr != KCGI_OK)
1069 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1070 if (kerr != KCGI_OK)
1073 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1074 "solid_line", KATTR__MAX);
1075 if (kerr != KCGI_OK)
1077 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1078 if (kerr != KCGI_OK)
1085 if (gw_trans->next_id || gw_trans->page > 0) {
1086 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1087 KATTR_ID, "np_wrapper", KATTR__MAX);
1088 if (kerr != KCGI_OK)
1090 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1091 KATTR_ID, "nav_prev", KATTR__MAX);
1092 if (kerr != KCGI_OK)
1096 if (gw_trans->page > 0 && gw_trans->prev_id) {
1097 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1098 KATTRX_STRING, gw_trans->repo_name, "page",
1099 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1100 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1101 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1102 KATTRX_STRING, gw_trans->prev_prev_id ?
1103 gw_trans->prev_prev_id : "", NULL);
1104 if (href_prev == NULL) {
1105 error = got_error_from_errno("khttp_urlpartx");
1108 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1109 KATTR_HREF, href_prev, KATTR__MAX);
1110 if (kerr != KCGI_OK)
1112 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1113 if (kerr != KCGI_OK)
1115 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1116 if (kerr != KCGI_OK)
1120 if (gw_trans->next_id || gw_trans->page > 0) {
1121 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1122 if (kerr != KCGI_OK)
1123 return gw_kcgi_error(kerr);
1126 if (gw_trans->next_id) {
1127 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1128 KATTR_ID, "nav_next", KATTR__MAX);
1129 if (kerr != KCGI_OK)
1131 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1132 KATTRX_STRING, gw_trans->repo_name, "page",
1133 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1134 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1135 gw_trans->next_id, "prev", KATTRX_STRING,
1136 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1137 "prev_prev", KATTRX_STRING, gw_trans->prev_prev_id ?
1138 gw_trans->prev_prev_id : "", NULL);
1139 if (href_next == NULL) {
1140 error = got_error_from_errno("khttp_urlpartx");
1143 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1144 KATTR_HREF, href_next, KATTR__MAX);
1145 if (kerr != KCGI_OK)
1147 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1148 if (kerr != KCGI_OK)
1150 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1151 if (kerr != KCGI_OK)
1155 if (gw_trans->next_id || gw_trans->page > 0) {
1156 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1157 if (kerr != KCGI_OK)
1161 gw_free_header(header);
1162 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1163 gw_free_header(n_header);
1169 if (error == NULL && kerr != KCGI_OK)
1170 error = gw_kcgi_error(kerr);
1174 static const struct got_error *
1175 gw_briefs(struct gw_trans *gw_trans)
1177 const struct got_error *error = NULL;
1178 struct gw_header *header = NULL, *n_header = NULL;
1179 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1180 char *href_prev = NULL, *href_next = NULL;
1181 char *newline, *smallerthan;
1182 enum kcgi_err kerr = KCGI_OK;
1184 if ((header = gw_init_header()) == NULL)
1185 return got_error_from_errno("malloc");
1187 if (pledge("stdio rpath proc exec sendfd unveil",
1189 error = got_error_from_errno("pledge");
1193 if (gw_trans->action != GW_SUMMARY) {
1194 error = gw_apply_unveil(gw_trans->gw_dir->path);
1199 if (gw_trans->action == GW_SUMMARY)
1200 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1202 error = gw_get_header(gw_trans, header,
1203 gw_trans->gw_conf->got_max_commits_display);
1207 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1208 error = gw_get_time_str(&age, n_header->committer_time,
1213 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1214 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1215 if (kerr != KCGI_OK)
1218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1219 KATTR_ID, "briefs_age", KATTR__MAX);
1220 if (kerr != KCGI_OK)
1222 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1223 if (kerr != KCGI_OK)
1225 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1226 if (kerr != KCGI_OK)
1229 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1230 KATTR_ID, "briefs_author", KATTR__MAX);
1231 if (kerr != KCGI_OK)
1233 smallerthan = strchr(n_header->author, '<');
1235 *smallerthan = '\0';
1236 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1237 if (kerr != KCGI_OK)
1239 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1240 if (kerr != KCGI_OK)
1243 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1244 gw_trans->repo_name, "action", "diff", "commit",
1245 n_header->commit_id, NULL);
1246 if (href_diff == NULL) {
1247 error = got_error_from_errno("khttp_urlpart");
1250 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1251 KATTR_ID, "briefs_log", KATTR__MAX);
1252 if (kerr != KCGI_OK)
1254 newline = strchr(n_header->commit_msg, '\n');
1257 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1258 KATTR_HREF, href_diff, KATTR__MAX);
1259 if (kerr != KCGI_OK)
1261 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1262 if (kerr != KCGI_OK)
1264 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1265 if (kerr != KCGI_OK)
1268 if (n_header->refs_str) {
1269 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1270 if (kerr != KCGI_OK)
1272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1273 KATTR_ID, "refs_str", KATTR__MAX);
1274 if (kerr != KCGI_OK)
1276 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1277 n_header->refs_str);
1278 if (kerr != KCGI_OK)
1280 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1281 if (kerr != KCGI_OK)
1285 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1286 if (kerr != KCGI_OK)
1289 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1290 KATTR_ID, "navs_wrapper", KATTR__MAX);
1291 if (kerr != KCGI_OK)
1293 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1294 KATTR_ID, "navs", KATTR__MAX);
1295 if (kerr != KCGI_OK)
1297 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1298 KATTR_HREF, href_diff, KATTR__MAX);
1299 if (kerr != KCGI_OK)
1301 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1302 if (kerr != KCGI_OK)
1304 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1305 if (kerr != KCGI_OK)
1308 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1309 if (kerr != KCGI_OK)
1312 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1313 gw_trans->repo_name, "action", "tree", "commit",
1314 n_header->commit_id, NULL);
1315 if (href_tree == NULL) {
1316 error = got_error_from_errno("khttp_urlpart");
1319 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1320 KATTR_HREF, href_tree, KATTR__MAX);
1321 if (kerr != KCGI_OK)
1323 khtml_puts(gw_trans->gw_html_req, "tree");
1324 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1325 if (kerr != KCGI_OK)
1327 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1328 if (kerr != KCGI_OK)
1331 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1332 KATTR_ID, "dotted_line", KATTR__MAX);
1333 if (kerr != KCGI_OK)
1335 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1336 if (kerr != KCGI_OK)
1347 if (gw_trans->next_id || gw_trans->page > 0) {
1348 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1349 KATTR_ID, "np_wrapper", KATTR__MAX);
1350 if (kerr != KCGI_OK)
1352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1353 KATTR_ID, "nav_prev", KATTR__MAX);
1354 if (kerr != KCGI_OK)
1358 if (gw_trans->page > 0 && gw_trans->prev_id) {
1359 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1360 KATTRX_STRING, gw_trans->repo_name, "page",
1361 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1362 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1363 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1364 KATTRX_STRING, gw_trans->prev_prev_id ?
1365 gw_trans->prev_prev_id : "", NULL);
1366 if (href_prev == NULL) {
1367 error = got_error_from_errno("khttp_urlpartx");
1370 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1371 KATTR_HREF, href_prev, KATTR__MAX);
1372 if (kerr != KCGI_OK)
1374 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1375 if (kerr != KCGI_OK)
1377 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1378 if (kerr != KCGI_OK)
1382 if (gw_trans->next_id || gw_trans->page > 0) {
1383 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1384 if (kerr != KCGI_OK)
1385 return gw_kcgi_error(kerr);
1388 if (gw_trans->next_id) {
1389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1390 KATTR_ID, "nav_next", KATTR__MAX);
1391 if (kerr != KCGI_OK)
1394 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1395 KATTRX_STRING, gw_trans->repo_name, "page",
1396 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1397 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1398 gw_trans->next_id, "prev", KATTRX_STRING,
1399 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1400 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1401 gw_trans->prev_id : "", NULL);
1402 if (href_next == NULL) {
1403 error = got_error_from_errno("khttp_urlpartx");
1406 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1407 KATTR_HREF, href_next, KATTR__MAX);
1408 if (kerr != KCGI_OK)
1410 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1411 if (kerr != KCGI_OK)
1413 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1414 if (kerr != KCGI_OK)
1418 if (gw_trans->next_id || gw_trans->page > 0) {
1419 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1420 if (kerr != KCGI_OK)
1424 gw_free_header(header);
1425 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1426 gw_free_header(n_header);
1432 if (error == NULL && kerr != KCGI_OK)
1433 error = gw_kcgi_error(kerr);
1437 static const struct got_error *
1438 gw_summary(struct gw_trans *gw_trans)
1440 const struct got_error *error = NULL;
1442 enum kcgi_err kerr = KCGI_OK;
1444 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1445 return got_error_from_errno("pledge");
1447 error = gw_apply_unveil(gw_trans->gw_dir->path);
1451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1452 "summary_wrapper", KATTR__MAX);
1453 if (kerr != KCGI_OK)
1454 return gw_kcgi_error(kerr);
1456 if (gw_trans->gw_conf->got_show_repo_description &&
1457 gw_trans->gw_dir->description != NULL &&
1458 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1459 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1460 KATTR_ID, "description_title", KATTR__MAX);
1461 if (kerr != KCGI_OK)
1463 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1464 if (kerr != KCGI_OK)
1466 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1467 if (kerr != KCGI_OK)
1469 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1470 KATTR_ID, "description", KATTR__MAX);
1471 if (kerr != KCGI_OK)
1473 kerr = khtml_puts(gw_trans->gw_html_req,
1474 gw_trans->gw_dir->description);
1475 if (kerr != KCGI_OK)
1477 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1478 if (kerr != KCGI_OK)
1482 if (gw_trans->gw_conf->got_show_repo_owner &&
1483 gw_trans->gw_dir->owner != NULL &&
1484 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1485 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1486 KATTR_ID, "repo_owner_title", KATTR__MAX);
1487 if (kerr != KCGI_OK)
1489 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1490 if (kerr != KCGI_OK)
1492 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1493 if (kerr != KCGI_OK)
1495 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1496 KATTR_ID, "repo_owner", KATTR__MAX);
1497 if (kerr != KCGI_OK)
1499 kerr = khtml_puts(gw_trans->gw_html_req,
1500 gw_trans->gw_dir->owner);
1501 if (kerr != KCGI_OK)
1503 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1504 if (kerr != KCGI_OK)
1508 if (gw_trans->gw_conf->got_show_repo_age) {
1509 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1515 KATTR_ID, "last_change_title", KATTR__MAX);
1516 if (kerr != KCGI_OK)
1518 kerr = khtml_puts(gw_trans->gw_html_req,
1520 if (kerr != KCGI_OK)
1522 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1523 if (kerr != KCGI_OK)
1525 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1526 KATTR_ID, "last_change", KATTR__MAX);
1527 if (kerr != KCGI_OK)
1529 kerr = khtml_puts(gw_trans->gw_html_req, age);
1530 if (kerr != KCGI_OK)
1532 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1533 if (kerr != KCGI_OK)
1538 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1539 gw_trans->gw_dir->url != NULL &&
1540 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1542 KATTR_ID, "cloneurl_title", KATTR__MAX);
1543 if (kerr != KCGI_OK)
1545 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1546 if (kerr != KCGI_OK)
1548 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1549 if (kerr != KCGI_OK)
1551 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1552 KATTR_ID, "cloneurl", KATTR__MAX);
1553 if (kerr != KCGI_OK)
1555 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1556 if (kerr != KCGI_OK)
1558 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1559 if (kerr != KCGI_OK)
1563 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1564 if (kerr != KCGI_OK)
1567 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1568 "briefs_title_wrapper", KATTR__MAX);
1569 if (kerr != KCGI_OK)
1571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1572 "briefs_title", KATTR__MAX);
1573 if (kerr != KCGI_OK)
1575 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1576 if (kerr != KCGI_OK)
1578 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1579 if (kerr != KCGI_OK)
1581 error = gw_briefs(gw_trans);
1585 error = gw_tags(gw_trans);
1589 error = gw_output_repo_heads(gw_trans);
1592 if (error == NULL && kerr != KCGI_OK)
1593 error = gw_kcgi_error(kerr);
1597 static const struct got_error *
1598 gw_tree(struct gw_trans *gw_trans)
1600 const struct got_error *error = NULL;
1601 struct gw_header *header = NULL;
1602 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1604 enum kcgi_err kerr = KCGI_OK;
1606 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1607 return got_error_from_errno("pledge");
1609 if ((header = gw_init_header()) == NULL)
1610 return got_error_from_errno("malloc");
1612 error = gw_apply_unveil(gw_trans->gw_dir->path);
1616 error = gw_get_header(gw_trans, header, 1);
1620 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1621 "tree_header_wrapper", KATTR__MAX);
1622 if (kerr != KCGI_OK)
1624 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1625 "tree_header", KATTR__MAX);
1626 if (kerr != KCGI_OK)
1628 error = gw_gen_tree_header(gw_trans, header->tree_id);
1631 error = gw_get_time_str(&age, header->committer_time,
1635 error = gw_gen_age_header(gw_trans, age ?age : "");
1638 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1641 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1642 if (kerr != KCGI_OK)
1644 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1645 "dotted_line", KATTR__MAX);
1646 if (kerr != KCGI_OK)
1648 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1649 if (kerr != KCGI_OK)
1652 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1653 "tree", KATTR__MAX);
1654 if (kerr != KCGI_OK)
1656 error = gw_output_repo_tree(gw_trans);
1660 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1662 gw_free_header(header);
1663 free(tree_html_disp);
1667 if (error == NULL && kerr != KCGI_OK)
1668 error = gw_kcgi_error(kerr);
1672 static const struct got_error *
1673 gw_tags(struct gw_trans *gw_trans)
1675 const struct got_error *error = NULL;
1676 struct gw_header *header = NULL;
1677 char *href_next = NULL, *href_prev = NULL;
1678 enum kcgi_err kerr = KCGI_OK;
1680 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1681 return got_error_from_errno("pledge");
1683 if ((header = gw_init_header()) == NULL)
1684 return got_error_from_errno("malloc");
1686 if (gw_trans->action != GW_SUMMARY) {
1687 error = gw_apply_unveil(gw_trans->gw_dir->path);
1692 error = gw_get_header(gw_trans, header, 1);
1696 if (gw_trans->action == GW_SUMMARY) {
1697 gw_trans->next_id = NULL;
1698 error = gw_output_repo_tags(gw_trans, header,
1699 D_MAXSLCOMMDISP, TAGBRIEF);
1703 error = gw_output_repo_tags(gw_trans, header,
1704 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1709 if (gw_trans->next_id || gw_trans->page > 0) {
1710 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1711 KATTR_ID, "np_wrapper", KATTR__MAX);
1712 if (kerr != KCGI_OK)
1714 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1715 KATTR_ID, "nav_prev", KATTR__MAX);
1716 if (kerr != KCGI_OK)
1720 if (gw_trans->page > 0 && gw_trans->prev_id) {
1721 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1722 KATTRX_STRING, gw_trans->repo_name, "page",
1723 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1724 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1725 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1726 KATTRX_STRING, gw_trans->prev_prev_id ?
1727 gw_trans->prev_prev_id : "", NULL);
1728 if (href_prev == NULL) {
1729 error = got_error_from_errno("khttp_urlpartx");
1732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1733 KATTR_HREF, href_prev, KATTR__MAX);
1734 if (kerr != KCGI_OK)
1736 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1737 if (kerr != KCGI_OK)
1739 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1740 if (kerr != KCGI_OK)
1744 if (gw_trans->next_id || gw_trans->page > 0) {
1745 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1746 if (kerr != KCGI_OK)
1747 return gw_kcgi_error(kerr);
1750 if (gw_trans->next_id) {
1751 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1752 KATTR_ID, "nav_next", KATTR__MAX);
1753 if (kerr != KCGI_OK)
1755 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1756 KATTRX_STRING, gw_trans->repo_name, "page",
1757 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1758 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1759 gw_trans->next_id, "prev", KATTRX_STRING,
1760 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1761 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1762 gw_trans->prev_id : "", NULL);
1763 if (href_next == NULL) {
1764 error = got_error_from_errno("khttp_urlpartx");
1767 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1768 KATTR_HREF, href_next, KATTR__MAX);
1769 if (kerr != KCGI_OK)
1771 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1772 if (kerr != KCGI_OK)
1774 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1775 if (kerr != KCGI_OK)
1779 if (gw_trans->next_id || gw_trans->page > 0) {
1780 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1781 if (kerr != KCGI_OK)
1785 gw_free_header(header);
1788 if (error == NULL && kerr != KCGI_OK)
1789 error = gw_kcgi_error(kerr);
1793 static const struct got_error *
1794 gw_tag(struct gw_trans *gw_trans)
1796 const struct got_error *error = NULL;
1797 struct gw_header *header = NULL;
1798 enum kcgi_err kerr = KCGI_OK;
1800 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1801 return got_error_from_errno("pledge");
1803 if ((header = gw_init_header()) == NULL)
1804 return got_error_from_errno("malloc");
1806 error = gw_apply_unveil(gw_trans->gw_dir->path);
1810 if (gw_trans->commit_id == NULL) {
1811 error = got_error_msg(GOT_ERR_QUERYSTRING,
1812 "commit required in querystring");
1816 error = gw_get_header(gw_trans, header, 1);
1820 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1821 "tag_header_wrapper", KATTR__MAX);
1822 if (kerr != KCGI_OK)
1824 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1825 "tag_header", KATTR__MAX);
1826 if (kerr != KCGI_OK)
1828 error = gw_gen_commit_header(gw_trans, header->commit_id,
1832 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1835 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1836 if (kerr != KCGI_OK)
1838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1839 "dotted_line", KATTR__MAX);
1840 if (kerr != KCGI_OK)
1842 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1843 if (kerr != KCGI_OK)
1846 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1847 "tree", KATTR__MAX);
1848 if (kerr != KCGI_OK)
1851 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1855 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1857 gw_free_header(header);
1858 if (error == NULL && kerr != KCGI_OK)
1859 error = gw_kcgi_error(kerr);
1863 static const struct got_error *
1864 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1866 const struct got_error *error = NULL;
1871 if (asprintf(&dir_test, "%s/%s/%s",
1872 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1873 GOTWEB_GIT_DIR) == -1)
1874 return got_error_from_errno("asprintf");
1876 dt = opendir(dir_test);
1880 gw_dir->path = strdup(dir_test);
1881 if (gw_dir->path == NULL) {
1883 error = got_error_from_errno("strdup");
1890 if (asprintf(&dir_test, "%s/%s/%s",
1891 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1892 GOTWEB_GOT_DIR) == -1) {
1894 error = got_error_from_errno("asprintf");
1898 dt = opendir(dir_test);
1903 error = got_error(GOT_ERR_NOT_GIT_REPO);
1907 if (asprintf(&dir_test, "%s/%s",
1908 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1909 error = got_error_from_errno("asprintf");
1914 gw_dir->path = strdup(dir_test);
1915 if (gw_dir->path == NULL) {
1917 error = got_error_from_errno("strdup");
1921 dt = opendir(dir_test);
1923 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1928 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1932 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1935 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1939 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1943 if (dt && closedir(dt) == -1 && error == NULL)
1944 error = got_error_from_errno("closedir");
1948 static const struct got_error *
1949 gw_load_got_paths(struct gw_trans *gw_trans)
1951 const struct got_error *error = NULL;
1953 struct dirent **sd_dent;
1954 struct gw_dir *gw_dir;
1956 unsigned int d_cnt, d_i;
1958 d = opendir(gw_trans->gw_conf->got_repos_path);
1960 error = got_error_from_errno2("opendir",
1961 gw_trans->gw_conf->got_repos_path);
1965 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1968 error = got_error_from_errno2("scandir",
1969 gw_trans->gw_conf->got_repos_path);
1973 for (d_i = 0; d_i < d_cnt; d_i++) {
1974 if (gw_trans->gw_conf->got_max_repos > 0 &&
1975 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1976 break; /* account for parent and self */
1978 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1979 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1982 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1986 error = gw_load_got_path(gw_trans, gw_dir);
1987 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1994 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1995 !got_path_dir_is_empty(gw_dir->path)) {
1996 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1998 gw_trans->repos_total++;
2002 if (d && closedir(d) == -1 && error == NULL)
2003 error = got_error_from_errno("closedir");
2007 static const struct got_error *
2008 gw_parse_querystring(struct gw_trans *gw_trans)
2010 const struct got_error *error = NULL;
2012 struct gw_query_action *action = NULL;
2015 if (gw_trans->gw_req->fieldnmap[0]) {
2016 return got_error(GOT_ERR_QUERYSTRING);
2017 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2018 /* define gw_trans->repo_path */
2019 gw_trans->repo_name = p->parsed.s;
2021 if (asprintf(&gw_trans->repo_path, "%s/%s",
2022 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2023 return got_error_from_errno("asprintf");
2025 /* get action and set function */
2026 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2027 for (i = 0; i < nitems(gw_query_funcs); i++) {
2028 action = &gw_query_funcs[i];
2029 if (action->func_name == NULL)
2031 if (strcmp(action->func_name,
2032 p->parsed.s) == 0) {
2033 gw_trans->action = i;
2038 if (gw_trans->action == -1) {
2039 gw_trans->action = GW_ERR;
2040 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2041 p != NULL ? "bad action in querystring" :
2042 "no action in querystring");
2046 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2047 if (asprintf(&gw_trans->commit_id, "%s",
2049 return got_error_from_errno("asprintf");
2052 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2053 gw_trans->repo_file = p->parsed.s;
2055 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2056 if (asprintf(&gw_trans->repo_folder, "%s",
2058 return got_error_from_errno("asprintf");
2061 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2062 if (asprintf(&gw_trans->prev_id, "%s",
2064 return got_error_from_errno("asprintf");
2067 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
2068 if (asprintf(&gw_trans->prev_prev_id, "%s",
2070 return got_error_from_errno("asprintf");
2073 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2074 gw_trans->headref = p->parsed.s;
2076 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2080 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2082 gw_trans->action = GW_INDEX;
2084 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2085 gw_trans->page = p->parsed.i;
2090 static const struct got_error *
2091 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2093 const struct got_error *error;
2095 *gw_dir = malloc(sizeof(**gw_dir));
2096 if (*gw_dir == NULL)
2097 return got_error_from_errno("malloc");
2099 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2100 error = got_error_from_errno("asprintf");
2109 static const struct got_error *
2110 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2112 enum kcgi_err kerr = KCGI_OK;
2114 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2115 if (kerr != KCGI_OK)
2116 return gw_kcgi_error(kerr);
2117 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2119 if (kerr != KCGI_OK)
2120 return gw_kcgi_error(kerr);
2121 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2123 if (kerr != KCGI_OK)
2124 return gw_kcgi_error(kerr);
2125 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2127 if (kerr != KCGI_OK)
2128 return gw_kcgi_error(kerr);
2129 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2130 if (kerr != KCGI_OK)
2131 return gw_kcgi_error(kerr);
2132 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2134 if (kerr != KCGI_OK)
2135 return gw_kcgi_error(kerr);
2137 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2138 kerr = khttp_head(gw_trans->gw_req,
2139 kresps[KRESP_CONTENT_DISPOSITION],
2140 "attachment; filename=%s", gw_trans->repo_file);
2141 if (kerr != KCGI_OK)
2142 return gw_kcgi_error(kerr);
2145 kerr = khttp_body(gw_trans->gw_req);
2146 return gw_kcgi_error(kerr);
2149 static const struct got_error *
2150 gw_display_index(struct gw_trans *gw_trans)
2152 const struct got_error *error;
2153 enum kcgi_err kerr = KCGI_OK;
2155 /* catch early querystring errors */
2156 if (gw_trans->error)
2157 gw_trans->action = GW_ERR;
2159 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2163 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2164 if (kerr != KCGI_OK)
2165 return gw_kcgi_error(kerr);
2167 if (gw_trans->action != GW_BLOB) {
2168 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2169 gw_query_funcs[gw_trans->action].template);
2170 if (kerr != KCGI_OK) {
2171 khtml_close(gw_trans->gw_html_req);
2172 return gw_kcgi_error(kerr);
2176 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2179 static const struct got_error *
2180 gw_error(struct gw_trans *gw_trans)
2182 enum kcgi_err kerr = KCGI_OK;
2184 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2186 return gw_kcgi_error(kerr);
2190 gw_template(size_t key, void *arg)
2192 const struct got_error *error = NULL;
2193 enum kcgi_err kerr = KCGI_OK;
2194 struct gw_trans *gw_trans = arg;
2195 char *img_src = NULL;
2199 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2200 KATTR_NAME, "viewport",
2201 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2203 if (kerr != KCGI_OK)
2205 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2206 if (kerr != KCGI_OK)
2208 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2209 KATTR_CHARSET, "utf-8",
2211 if (kerr != KCGI_OK)
2213 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2214 if (kerr != KCGI_OK)
2216 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2217 KATTR_NAME, "msapplication-TileColor",
2218 KATTR_CONTENT, "#da532c", KATTR__MAX);
2219 if (kerr != KCGI_OK)
2221 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2222 if (kerr != KCGI_OK)
2224 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2225 KATTR_NAME, "theme-color",
2226 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2227 if (kerr != KCGI_OK)
2229 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2230 if (kerr != KCGI_OK)
2232 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2233 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2234 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
2235 if (kerr != KCGI_OK)
2237 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2238 if (kerr != KCGI_OK)
2240 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2241 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2242 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
2243 if (kerr != KCGI_OK)
2245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2246 if (kerr != KCGI_OK)
2248 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2249 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2250 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
2251 if (kerr != KCGI_OK)
2253 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2254 if (kerr != KCGI_OK)
2256 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2257 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
2259 if (kerr != KCGI_OK)
2261 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2262 if (kerr != KCGI_OK)
2264 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2265 KATTR_REL, "mask-icon", KATTR_HREF,
2266 "/safari-pinned-tab.svg", KATTR__MAX);
2267 if (kerr != KCGI_OK)
2269 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2270 if (kerr != KCGI_OK)
2272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2273 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2274 KATTR_HREF, "/gotweb.css", KATTR__MAX);
2275 if (kerr != KCGI_OK)
2277 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2278 if (kerr != KCGI_OK)
2282 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2283 KATTR_ID, "got_link", KATTR__MAX);
2284 if (kerr != KCGI_OK)
2286 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2287 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2288 KATTR_TARGET, "_sotd", KATTR__MAX);
2289 if (kerr != KCGI_OK)
2291 if (asprintf(&img_src, "/%s",
2292 gw_trans->gw_conf->got_logo) == -1)
2294 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2295 KATTR_SRC, img_src, KATTR__MAX);
2296 if (kerr != KCGI_OK) {
2300 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2301 if (kerr != KCGI_OK) {
2306 case (TEMPL_SITEPATH):
2307 error = gw_output_site_link(gw_trans);
2312 if (gw_trans->gw_conf->got_site_name != NULL) {
2313 kerr = khtml_puts(gw_trans->gw_html_req,
2314 gw_trans->gw_conf->got_site_name);
2315 if (kerr != KCGI_OK)
2319 case (TEMPL_SEARCH):
2321 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2322 "search", KATTR__MAX);
2323 if (kerr != KCGI_OK)
2325 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2326 KATTR_METHOD, "POST", KATTR__MAX);
2327 if (kerr != KCGI_OK)
2329 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2330 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2331 KATTR_MAXLENGTH, "50", KATTR__MAX);
2332 if (kerr != KCGI_OK)
2334 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2336 if (kerr != KCGI_OK)
2338 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2339 if (kerr != KCGI_OK)
2341 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2342 if (kerr != KCGI_OK)
2345 case(TEMPL_SITEOWNER):
2346 if (gw_trans->gw_conf->got_site_owner != NULL &&
2347 gw_trans->gw_conf->got_show_site_owner) {
2348 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2349 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2350 if (kerr != KCGI_OK)
2352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2353 KATTR_ID, "site_owner", KATTR__MAX);
2354 if (kerr != KCGI_OK)
2356 kerr = khtml_puts(gw_trans->gw_html_req,
2357 gw_trans->gw_conf->got_site_owner);
2358 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2359 if (kerr != KCGI_OK)
2363 case(TEMPL_CONTENT):
2364 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2367 KATTR_ID, "tmpl_err", KATTR__MAX);
2368 if (kerr != KCGI_OK)
2370 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2372 if (kerr != KCGI_OK)
2374 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2375 if (kerr != KCGI_OK)
2385 static const struct got_error *
2386 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2388 const struct got_error *error = NULL;
2389 enum kcgi_err kerr = KCGI_OK;
2391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2392 KATTR_ID, "header_commit_title", KATTR__MAX);
2393 if (kerr != KCGI_OK)
2395 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2396 if (kerr != KCGI_OK)
2398 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2399 if (kerr != KCGI_OK)
2401 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2402 KATTR_ID, "header_commit", KATTR__MAX);
2403 if (kerr != KCGI_OK)
2405 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2406 if (kerr != KCGI_OK)
2409 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2410 KATTR_ID, "refs_str", KATTR__MAX);
2411 if (kerr != KCGI_OK)
2413 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2414 if (kerr != KCGI_OK)
2416 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2417 if (kerr != KCGI_OK)
2420 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2422 if (error == NULL && kerr != KCGI_OK)
2423 error = gw_kcgi_error(kerr);
2427 static const struct got_error *
2428 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2430 const struct got_error *error = NULL;
2431 enum kcgi_err kerr = KCGI_OK;
2433 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2434 KATTR_ID, "header_diff_title", KATTR__MAX);
2435 if (kerr != KCGI_OK)
2437 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2438 if (kerr != KCGI_OK)
2440 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2441 if (kerr != KCGI_OK)
2443 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2444 KATTR_ID, "header_diff", KATTR__MAX);
2445 if (kerr != KCGI_OK)
2448 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2449 if (kerr != KCGI_OK)
2452 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2453 if (kerr != KCGI_OK)
2455 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2456 if (kerr != KCGI_OK)
2458 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2460 if (error == NULL && kerr != KCGI_OK)
2461 error = gw_kcgi_error(kerr);
2465 static const struct got_error *
2466 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2468 const struct got_error *error = NULL;
2469 enum kcgi_err kerr = KCGI_OK;
2471 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2472 KATTR_ID, "header_age_title", KATTR__MAX);
2473 if (kerr != KCGI_OK)
2475 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2476 if (kerr != KCGI_OK)
2478 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2479 if (kerr != KCGI_OK)
2481 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2482 KATTR_ID, "header_age", KATTR__MAX);
2483 if (kerr != KCGI_OK)
2485 kerr = khtml_puts(gw_trans->gw_html_req, str);
2486 if (kerr != KCGI_OK)
2488 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2490 if (error == NULL && kerr != KCGI_OK)
2491 error = gw_kcgi_error(kerr);
2495 static const struct got_error *
2496 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2498 const struct got_error *error = NULL;
2499 enum kcgi_err kerr = KCGI_OK;
2501 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2502 KATTR_ID, "header_author_title", KATTR__MAX);
2503 if (kerr != KCGI_OK)
2505 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2506 if (kerr != KCGI_OK)
2508 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2509 if (kerr != KCGI_OK)
2511 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2512 KATTR_ID, "header_author", KATTR__MAX);
2513 if (kerr != KCGI_OK)
2515 kerr = khtml_puts(gw_trans->gw_html_req, str);
2516 if (kerr != KCGI_OK)
2518 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2520 if (error == NULL && kerr != KCGI_OK)
2521 error = gw_kcgi_error(kerr);
2525 static const struct got_error *
2526 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2528 const struct got_error *error = NULL;
2529 enum kcgi_err kerr = KCGI_OK;
2531 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2532 KATTR_ID, "header_committer_title", KATTR__MAX);
2533 if (kerr != KCGI_OK)
2535 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2536 if (kerr != KCGI_OK)
2538 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2539 if (kerr != KCGI_OK)
2541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2542 KATTR_ID, "header_committer", KATTR__MAX);
2543 if (kerr != KCGI_OK)
2545 kerr = khtml_puts(gw_trans->gw_html_req, str);
2546 if (kerr != KCGI_OK)
2548 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2550 if (error == NULL && kerr != KCGI_OK)
2551 error = gw_kcgi_error(kerr);
2555 static const struct got_error *
2556 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2558 const struct got_error *error = NULL;
2559 enum kcgi_err kerr = KCGI_OK;
2561 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2562 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2563 if (kerr != KCGI_OK)
2565 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2566 if (kerr != KCGI_OK)
2568 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2569 if (kerr != KCGI_OK)
2571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2572 KATTR_ID, "header_commit_msg", KATTR__MAX);
2573 if (kerr != KCGI_OK)
2575 kerr = khttp_puts(gw_trans->gw_req, str);
2576 if (kerr != KCGI_OK)
2578 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2580 if (error == NULL && kerr != KCGI_OK)
2581 error = gw_kcgi_error(kerr);
2585 static const struct got_error *
2586 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2588 const struct got_error *error = NULL;
2589 enum kcgi_err kerr = KCGI_OK;
2591 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2592 KATTR_ID, "header_tree_title", KATTR__MAX);
2593 if (kerr != KCGI_OK)
2595 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2596 if (kerr != KCGI_OK)
2598 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2599 if (kerr != KCGI_OK)
2601 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2602 KATTR_ID, "header_tree", KATTR__MAX);
2603 if (kerr != KCGI_OK)
2605 kerr = khtml_puts(gw_trans->gw_html_req, str);
2606 if (kerr != KCGI_OK)
2608 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2610 if (error == NULL && kerr != KCGI_OK)
2611 error = gw_kcgi_error(kerr);
2615 static const struct got_error *
2616 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2619 const struct got_error *error = NULL;
2621 char *d_file = NULL;
2625 *description = NULL;
2626 if (gw_trans->gw_conf->got_show_repo_description == 0)
2629 if (asprintf(&d_file, "%s/description", dir) == -1)
2630 return got_error_from_errno("asprintf");
2632 f = fopen(d_file, "r");
2634 if (errno == ENOENT || errno == EACCES)
2636 error = got_error_from_errno2("fopen", d_file);
2640 if (fseek(f, 0, SEEK_END) == -1) {
2641 error = got_ferror(f, GOT_ERR_IO);
2646 error = got_ferror(f, GOT_ERR_IO);
2649 if (fseek(f, 0, SEEK_SET) == -1) {
2650 error = got_ferror(f, GOT_ERR_IO);
2653 *description = calloc(len + 1, sizeof(**description));
2654 if (*description == NULL) {
2655 error = got_error_from_errno("calloc");
2659 n = fread(*description, 1, len, f);
2660 if (n == 0 && ferror(f))
2661 error = got_ferror(f, GOT_ERR_IO);
2663 if (f != NULL && fclose(f) == -1 && error == NULL)
2664 error = got_error_from_errno("fclose");
2669 static const struct got_error *
2670 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2674 char *years = "years ago", *months = "months ago";
2675 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2676 char *minutes = "minutes ago", *seconds = "seconds ago";
2677 char *now = "right now";
2685 diff_time = time(NULL) - committer_time;
2686 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2687 if (asprintf(repo_age, "%lld %s",
2688 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2689 return got_error_from_errno("asprintf");
2690 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2691 if (asprintf(repo_age, "%lld %s",
2692 (diff_time / 60 / 60 / 24 / (365 / 12)),
2694 return got_error_from_errno("asprintf");
2695 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2696 if (asprintf(repo_age, "%lld %s",
2697 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2698 return got_error_from_errno("asprintf");
2699 } else if (diff_time > 60 * 60 * 24 * 2) {
2700 if (asprintf(repo_age, "%lld %s",
2701 (diff_time / 60 / 60 / 24), days) == -1)
2702 return got_error_from_errno("asprintf");
2703 } else if (diff_time > 60 * 60 * 2) {
2704 if (asprintf(repo_age, "%lld %s",
2705 (diff_time / 60 / 60), hours) == -1)
2706 return got_error_from_errno("asprintf");
2707 } else if (diff_time > 60 * 2) {
2708 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2710 return got_error_from_errno("asprintf");
2711 } else if (diff_time > 2) {
2712 if (asprintf(repo_age, "%lld %s", diff_time,
2714 return got_error_from_errno("asprintf");
2716 if (asprintf(repo_age, "%s", now) == -1)
2717 return got_error_from_errno("asprintf");
2721 if (gmtime_r(&committer_time, &tm) == NULL)
2722 return got_error_from_errno("gmtime_r");
2724 s = asctime_r(&tm, datebuf);
2726 return got_error_from_errno("asctime_r");
2728 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2729 return got_error_from_errno("asprintf");
2735 static const struct got_error *
2736 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2737 const char *refname, int ref_tm)
2739 const struct got_error *error = NULL;
2740 struct got_repository *repo = NULL;
2741 struct got_commit_object *commit = NULL;
2742 struct got_reflist_head refs;
2743 struct got_reflist_entry *re;
2744 time_t committer_time = 0, cmp_time = 0;
2747 SIMPLEQ_INIT(&refs);
2749 if (gw_trans->gw_conf->got_show_repo_age == 0)
2753 repo = gw_trans->repo;
2755 error = got_repo_open(&repo, dir, NULL);
2760 error = got_ref_list(&refs, repo, "refs/heads",
2761 got_ref_cmp_by_name, NULL);
2766 * Find the youngest branch tip in the repository, or the age of
2767 * the a specific branch tip if a name was provided by the caller.
2769 SIMPLEQ_FOREACH(re, &refs, entry) {
2770 struct got_object_id *id = NULL;
2772 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2775 error = got_ref_resolve(&id, repo, re->ref);
2779 error = got_object_open_as_commit(&commit, repo, id);
2785 got_object_commit_get_committer_time(commit);
2786 got_object_commit_close(commit);
2787 if (cmp_time < committer_time)
2788 cmp_time = committer_time;
2794 if (cmp_time != 0) {
2795 committer_time = cmp_time;
2796 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2799 got_ref_list_free(&refs);
2800 if (gw_trans->repo == NULL)
2801 got_repo_close(repo);
2805 static const struct got_error *
2806 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2808 const struct got_error *error;
2810 struct got_object_id *id1 = NULL, *id2 = NULL;
2811 char *label1 = NULL, *label2 = NULL, *line = NULL;
2813 size_t linesize = 0;
2815 enum kcgi_err kerr = KCGI_OK;
2821 if (header->parent_id != NULL &&
2822 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2823 error = got_repo_match_object_id(&id1, &label1,
2824 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2829 error = got_repo_match_object_id(&id2, &label2,
2830 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2834 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2838 case GOT_OBJ_TYPE_BLOB:
2839 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2842 case GOT_OBJ_TYPE_TREE:
2843 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2846 case GOT_OBJ_TYPE_COMMIT:
2847 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2851 error = got_error(GOT_ERR_OBJ_TYPE);
2856 if (fseek(f, 0, SEEK_SET) == -1) {
2857 error = got_ferror(f, GOT_ERR_IO);
2861 while ((linelen = getline(&line, &linesize, f)) != -1) {
2862 error = gw_colordiff_line(gw_trans, line);
2865 /* XXX: KHTML_PRETTY breaks this */
2866 kerr = khtml_puts(gw_trans->gw_html_req, line);
2867 if (kerr != KCGI_OK)
2869 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2870 if (kerr != KCGI_OK)
2873 if (linelen == -1 && ferror(f))
2874 error = got_error_from_errno("getline");
2876 if (f && fclose(f) == -1 && error == NULL)
2877 error = got_error_from_errno("fclose");
2884 if (error == NULL && kerr != KCGI_OK)
2885 error = gw_kcgi_error(kerr);
2889 static const struct got_error *
2890 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2892 const struct got_error *error = NULL;
2893 struct got_repository *repo;
2894 const char *gitconfig_owner;
2898 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2901 error = got_repo_open(&repo, dir, NULL);
2904 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2905 if (gitconfig_owner) {
2906 *owner = strdup(gitconfig_owner);
2908 error = got_error_from_errno("strdup");
2910 got_repo_close(repo);
2914 static const struct got_error *
2915 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2917 const struct got_error *error = NULL;
2919 char *d_file = NULL;
2925 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2926 return got_error_from_errno("asprintf");
2928 f = fopen(d_file, "r");
2930 if (errno != ENOENT && errno != EACCES)
2931 error = got_error_from_errno2("fopen", d_file);
2935 if (fseek(f, 0, SEEK_END) == -1) {
2936 error = got_ferror(f, GOT_ERR_IO);
2941 error = got_ferror(f, GOT_ERR_IO);
2944 if (fseek(f, 0, SEEK_SET) == -1) {
2945 error = got_ferror(f, GOT_ERR_IO);
2949 *url = calloc(len + 1, sizeof(**url));
2951 error = got_error_from_errno("calloc");
2955 n = fread(*url, 1, len, f);
2956 if (n == 0 && ferror(f))
2957 error = got_ferror(f, GOT_ERR_IO);
2959 if (f && fclose(f) == -1 && error == NULL)
2960 error = got_error_from_errno("fclose");
2965 static const struct got_error *
2966 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2967 int limit, int tag_type)
2969 const struct got_error *error = NULL;
2970 struct got_reflist_head refs;
2971 struct got_reflist_entry *re;
2973 char *id_str = NULL, *newline, *href_commits = NULL;
2974 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2975 struct got_tag_object *tag = NULL;
2976 enum kcgi_err kerr = KCGI_OK;
2977 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
2978 int prev_set = 0, tag_count = 0;
2980 SIMPLEQ_INIT(&refs);
2982 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2983 got_ref_cmp_tags, gw_trans->repo);
2987 SIMPLEQ_FOREACH(re, &refs, entry) {
2988 const char *refname;
2990 const char *tag_commit;
2992 struct got_object_id *id;
2993 struct got_commit_object *commit = NULL;
2995 refname = got_ref_get_name(re->ref);
2996 if (strncmp(refname, "refs/tags/", 10) != 0)
3000 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3004 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3006 if (error->code != GOT_ERR_OBJ_TYPE) {
3010 /* "lightweight" tag */
3011 error = got_object_open_as_commit(&commit,
3012 gw_trans->repo, id);
3017 tagger = got_object_commit_get_committer(commit);
3019 got_object_commit_get_committer_time(commit);
3020 error = got_object_id_str(&id_str, id);
3024 tagger = got_object_tag_get_tagger(tag);
3025 tagger_time = got_object_tag_get_tagger_time(tag);
3026 error = got_object_id_str(&id_str,
3027 got_object_tag_get_object_id(tag));
3032 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3033 strlen(id_str)) != 0)
3036 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3037 start_tag == 0 && strncmp(id_str, header->commit_id,
3038 strlen(id_str)) != 0) {
3046 if (prev_set == 0 && start_tag == 1) {
3047 gw_trans->next_prev_id = strdup(id_str);
3048 if (gw_trans->next_prev_id == NULL) {
3049 error = got_error_from_errno("strdup");
3056 gw_trans->next_id = strdup(id_str);
3057 if (gw_trans->next_id == NULL)
3058 error = got_error_from_errno("strdup");
3063 error = got_object_commit_get_logmsg(&tag_commit0,
3067 got_object_commit_close(commit);
3069 tag_commit0 = strdup(got_object_tag_get_message(tag));
3070 if (tag_commit0 == NULL) {
3071 error = got_error_from_errno("strdup");
3076 tag_commit = tag_commit0;
3077 while (*tag_commit == '\n')
3082 newline = strchr(tag_commit, '\n');
3086 if (summary_header_displayed == 0) {
3087 kerr = khtml_attr(gw_trans->gw_html_req,
3088 KELEM_DIV, KATTR_ID,
3089 "summary_tags_title_wrapper", KATTR__MAX);
3090 if (kerr != KCGI_OK)
3092 kerr = khtml_attr(gw_trans->gw_html_req,
3093 KELEM_DIV, KATTR_ID,
3094 "summary_tags_title", KATTR__MAX);
3095 if (kerr != KCGI_OK)
3097 kerr = khtml_puts(gw_trans->gw_html_req,
3099 if (kerr != KCGI_OK)
3101 kerr = khtml_closeelem(gw_trans->gw_html_req,
3103 if (kerr != KCGI_OK)
3105 kerr = khtml_attr(gw_trans->gw_html_req,
3106 KELEM_DIV, KATTR_ID,
3107 "summary_tags_content", KATTR__MAX);
3108 if (kerr != KCGI_OK)
3110 summary_header_displayed = 1;
3113 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3114 KATTR_ID, "tag_wrapper", KATTR__MAX);
3115 if (kerr != KCGI_OK)
3117 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3118 KATTR_ID, "tag_age", KATTR__MAX);
3119 if (kerr != KCGI_OK)
3121 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3124 kerr = khtml_puts(gw_trans->gw_html_req,
3126 if (kerr != KCGI_OK)
3128 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3129 if (kerr != KCGI_OK)
3131 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3132 KATTR_ID, "tag", KATTR__MAX);
3133 if (kerr != KCGI_OK)
3135 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3136 if (kerr != KCGI_OK)
3138 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3139 if (kerr != KCGI_OK)
3141 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3142 KATTR_ID, "tag_name", KATTR__MAX);
3143 if (kerr != KCGI_OK)
3146 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3147 gw_trans->repo_name, "action", "tag", "commit",
3149 if (href_tag == NULL) {
3150 error = got_error_from_errno("khttp_urlpart");
3153 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3154 KATTR_HREF, href_tag, KATTR__MAX);
3155 if (kerr != KCGI_OK)
3157 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3158 if (kerr != KCGI_OK)
3160 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3161 if (kerr != KCGI_OK)
3164 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3165 KATTR_ID, "navs_wrapper", KATTR__MAX);
3166 if (kerr != KCGI_OK)
3168 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3169 KATTR_ID, "navs", KATTR__MAX);
3170 if (kerr != KCGI_OK)
3173 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3174 KATTR_HREF, href_tag, KATTR__MAX);
3175 if (kerr != KCGI_OK)
3177 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3178 if (kerr != KCGI_OK)
3180 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3181 if (kerr != KCGI_OK)
3184 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3185 if (kerr != KCGI_OK)
3188 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3189 "path", gw_trans->repo_name, "action", "briefs",
3190 "commit", id_str, NULL);
3191 if (href_briefs == NULL) {
3192 error = got_error_from_errno("khttp_urlpart");
3195 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3196 KATTR_HREF, href_briefs, KATTR__MAX);
3197 if (kerr != KCGI_OK)
3199 kerr = khtml_puts(gw_trans->gw_html_req,
3201 if (kerr != KCGI_OK)
3203 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3204 if (kerr != KCGI_OK)
3207 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3208 if (kerr != KCGI_OK)
3211 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3212 "path", gw_trans->repo_name, "action", "commits",
3213 "commit", id_str, NULL);
3214 if (href_commits == NULL) {
3215 error = got_error_from_errno("khttp_urlpart");
3218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3219 KATTR_HREF, href_commits, KATTR__MAX);
3220 if (kerr != KCGI_OK)
3222 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3223 if (kerr != KCGI_OK)
3225 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3226 if (kerr != KCGI_OK)
3229 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3230 KATTR_ID, "dotted_line", KATTR__MAX);
3231 if (kerr != KCGI_OK)
3233 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3234 if (kerr != KCGI_OK)
3238 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3239 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3240 if (kerr != KCGI_OK)
3242 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3243 if (kerr != KCGI_OK)
3245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3246 if (kerr != KCGI_OK)
3248 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3249 KATTR_ID, "tag_info_date", KATTR__MAX);
3250 if (kerr != KCGI_OK)
3252 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3255 kerr = khtml_puts(gw_trans->gw_html_req,
3257 if (kerr != KCGI_OK)
3259 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3260 if (kerr != KCGI_OK)
3263 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3264 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3265 if (kerr != KCGI_OK)
3267 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3268 if (kerr != KCGI_OK)
3270 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3271 if (kerr != KCGI_OK)
3273 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3274 KATTR_ID, "tag_info_date", KATTR__MAX);
3275 if (kerr != KCGI_OK)
3277 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3278 if (kerr != KCGI_OK)
3280 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3281 if (kerr != KCGI_OK)
3284 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3285 KATTR_ID, "tag_info", KATTR__MAX);
3286 if (kerr != KCGI_OK)
3288 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3289 if (kerr != KCGI_OK)
3295 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3296 if (kerr != KCGI_OK)
3299 if (limit && --limit == 0)
3303 got_object_tag_close(tag);
3316 href_commits = NULL;
3318 if (tag_count == 0) {
3319 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3320 "summary_tags_title_wrapper", KATTR__MAX);
3321 if (kerr != KCGI_OK)
3323 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3324 "summary_tags_title", KATTR__MAX);
3325 if (kerr != KCGI_OK)
3327 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3328 if (kerr != KCGI_OK)
3330 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3331 if (kerr != KCGI_OK)
3333 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3334 "summary_tags_content", KATTR__MAX);
3335 if (kerr != KCGI_OK)
3337 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3338 "tags_info", KATTR__MAX);
3339 if (kerr != KCGI_OK)
3341 kerr = khttp_puts(gw_trans->gw_req,
3342 "There are no tags for this repo.");
3343 if (kerr != KCGI_OK)
3345 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3349 got_object_tag_close(tag);
3356 got_ref_list_free(&refs);
3357 if (error == NULL && kerr != KCGI_OK)
3358 error = gw_kcgi_error(kerr);
3363 gw_free_header(struct gw_header *header)
3366 free(header->author);
3367 free(header->committer);
3368 free(header->refs_str);
3369 free(header->commit_id);
3370 free(header->parent_id);
3371 free(header->tree_id);
3372 free(header->commit_msg);
3375 static struct gw_header *
3378 struct gw_header *header;
3380 header = malloc(sizeof(*header));
3384 header->path = NULL;
3385 SIMPLEQ_INIT(&header->refs);
3387 header->refs_str = NULL;
3388 header->commit_id = NULL;
3389 header->committer = NULL;
3390 header->author = NULL;
3391 header->parent_id = NULL;
3392 header->tree_id = NULL;
3393 header->commit_msg = NULL;
3398 static const struct got_error *
3399 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3400 int limit, struct got_object_id *id)
3402 const struct got_error *error = NULL;
3403 struct got_commit_graph *graph = NULL;
3404 struct got_commit_object *commit = NULL;
3405 int chk_next = 0, chk_multi = 0, prev_set = 0;
3407 error = got_commit_graph_open(&graph, header->path, 0);
3411 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3417 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3420 if (error->code == GOT_ERR_ITER_COMPLETED)
3427 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3430 if (limit == 1 && chk_multi == 0 &&
3431 gw_trans->gw_conf->got_max_commits_display != 1) {
3432 error = gw_get_commit(gw_trans, header, commit, id);
3437 struct gw_header *n_header = NULL;
3438 if ((n_header = gw_init_header()) == NULL) {
3439 error = got_error_from_errno("malloc");
3442 error = got_ref_list(&n_header->refs, gw_trans->repo,
3443 NULL, got_ref_cmp_by_name, NULL);
3447 error = gw_get_commit(gw_trans, n_header, commit, id);
3450 got_ref_list_free(&n_header->refs);
3453 * we have a commit_id now, so copy it to next_prev_id
3454 * for navigation through gw_briefs and gw_commits
3456 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3457 (gw_trans->action == GW_BRIEFS ||
3458 gw_trans->action == GW_COMMITS ||
3459 gw_trans->action == GW_SUMMARY)) {
3461 gw_trans->next_prev_id =
3462 strdup(n_header->commit_id);
3463 if (gw_trans->next_prev_id == NULL) {
3464 error = got_error_from_errno("strdup");
3470 * check for one more commit before breaking,
3471 * so we know whether to navicate through gw_briefs
3472 * gw_commits and gw_summary
3474 if (chk_next && (gw_trans->action == GW_BRIEFS||
3475 gw_trans->action == GW_COMMITS ||
3476 gw_trans->action == GW_SUMMARY)) {
3477 gw_trans->next_id = strdup(n_header->commit_id);
3478 if (gw_trans->next_id == NULL)
3479 error = got_error_from_errno("strdup");
3483 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3486 if (error || (limit && --limit == 0)) {
3494 got_object_commit_close(commit);
3496 got_commit_graph_close(graph);
3500 static const struct got_error *
3501 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3502 struct got_commit_object *commit, struct got_object_id *id)
3504 const struct got_error *error = NULL;
3505 struct got_reflist_entry *re;
3506 struct got_object_id *id2 = NULL;
3507 struct got_object_qid *parent_id;
3508 char *commit_msg = NULL, *commit_msg0;
3511 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3514 struct got_tag_object *tag = NULL;
3517 if (got_ref_is_symbolic(re->ref))
3520 name = got_ref_get_name(re->ref);
3521 if (strncmp(name, "refs/", 5) == 0)
3523 if (strncmp(name, "got/", 4) == 0)
3525 if (strncmp(name, "heads/", 6) == 0)
3527 if (strncmp(name, "remotes/", 8) == 0)
3529 if (strncmp(name, "tags/", 5) == 0) {
3530 error = got_object_open_as_tag(&tag, gw_trans->repo,
3533 if (error->code != GOT_ERR_OBJ_TYPE)
3536 * Ref points at something other
3543 cmp = got_object_id_cmp(tag ?
3544 got_object_tag_get_object_id(tag) : re->id, id);
3546 got_object_tag_close(tag);
3549 s = header->refs_str;
3550 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3551 s ? ", " : "", name) == -1) {
3552 error = got_error_from_errno("asprintf");
3554 header->refs_str = NULL;
3560 error = got_object_id_str(&header->commit_id, id);
3564 error = got_object_id_str(&header->tree_id,
3565 got_object_commit_get_tree_id(commit));
3569 if (gw_trans->action == GW_DIFF) {
3570 parent_id = SIMPLEQ_FIRST(
3571 got_object_commit_get_parent_ids(commit));
3572 if (parent_id != NULL) {
3573 id2 = got_object_id_dup(parent_id->id);
3575 error = got_object_id_str(&header->parent_id, id2);
3580 header->parent_id = strdup("/dev/null");
3581 if (header->parent_id == NULL) {
3582 error = got_error_from_errno("strdup");
3588 header->committer_time =
3589 got_object_commit_get_committer_time(commit);
3592 strdup(got_object_commit_get_author(commit));
3593 if (header->author == NULL) {
3594 error = got_error_from_errno("strdup");
3598 strdup(got_object_commit_get_committer(commit));
3599 if (header->committer == NULL) {
3600 error = got_error_from_errno("strdup");
3603 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3607 commit_msg = commit_msg0;
3608 while (*commit_msg == '\n')
3611 header->commit_msg = strdup(commit_msg);
3612 if (header->commit_msg == NULL)
3613 error = got_error_from_errno("strdup");
3618 static const struct got_error *
3619 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3621 const struct got_error *error = NULL;
3622 char *in_repo_path = NULL;
3623 struct got_object_id *id = NULL;
3625 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3629 if (gw_trans->commit_id == NULL) {
3630 struct got_reference *head_ref;
3631 error = got_ref_open(&head_ref, gw_trans->repo,
3632 gw_trans->headref, 0);
3636 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3637 got_ref_close(head_ref);
3641 struct got_reference *ref;
3643 error = got_ref_open(&ref, gw_trans->repo,
3644 gw_trans->commit_id, 0);
3645 if (error == NULL) {
3647 error = got_ref_resolve(&id, gw_trans->repo, ref);
3651 error = got_object_get_type(&obj_type, gw_trans->repo,
3655 if (obj_type == GOT_OBJ_TYPE_TAG) {
3656 struct got_tag_object *tag;
3657 error = got_object_open_as_tag(&tag,
3658 gw_trans->repo, id);
3661 if (got_object_tag_get_object_type(tag) !=
3662 GOT_OBJ_TYPE_COMMIT) {
3663 got_object_tag_close(tag);
3664 error = got_error(GOT_ERR_OBJ_TYPE);
3668 id = got_object_id_dup(
3669 got_object_tag_get_object_id(tag));
3671 error = got_error_from_errno(
3672 "got_object_id_dup");
3673 got_object_tag_close(tag);
3676 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3677 error = got_error(GOT_ERR_OBJ_TYPE);
3681 error = got_repo_match_object_id_prefix(&id,
3682 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3688 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3689 gw_trans->repo_path, 1);
3694 header->path = strdup(in_repo_path);
3695 if (header->path == NULL) {
3696 error = got_error_from_errno("strdup");
3701 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3702 got_ref_cmp_by_name, NULL);
3706 error = gw_get_commits(gw_trans, header, limit, id);
3708 got_ref_list_free(&header->refs);
3718 char datebuf[11]; /* YYYY-MM-DD + NUL */
3721 struct gw_blame_cb_args {
3722 struct blame_line *lines;
3726 off_t *line_offsets;
3728 struct got_repository *repo;
3729 struct gw_trans *gw_trans;
3732 static const struct got_error *
3733 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3735 const struct got_error *err = NULL;
3736 struct gw_blame_cb_args *a = arg;
3737 struct blame_line *bline;
3739 size_t linesize = 0;
3740 struct got_commit_object *commit = NULL;
3743 time_t committer_time;
3744 enum kcgi_err kerr = KCGI_OK;
3746 if (nlines != a->nlines ||
3747 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3748 return got_error(GOT_ERR_RANGE);
3751 return NULL; /* no change in this commit */
3753 /* Annotate this line. */
3754 bline = &a->lines[lineno - 1];
3755 if (bline->annotated)
3757 err = got_object_id_str(&bline->id_str, id);
3761 err = got_object_open_as_commit(&commit, a->repo, id);
3765 bline->committer = strdup(got_object_commit_get_committer(commit));
3766 if (bline->committer == NULL) {
3767 err = got_error_from_errno("strdup");
3771 committer_time = got_object_commit_get_committer_time(commit);
3772 if (localtime_r(&committer_time, &tm) == NULL)
3773 return got_error_from_errno("localtime_r");
3774 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3775 &tm) >= sizeof(bline->datebuf)) {
3776 err = got_error(GOT_ERR_NO_SPACE);
3779 bline->annotated = 1;
3781 /* Print lines annotated so far. */
3782 bline = &a->lines[a->lineno_cur - 1];
3783 if (!bline->annotated)
3786 offset = a->line_offsets[a->lineno_cur - 1];
3787 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3788 err = got_error_from_errno("fseeko");
3792 while (bline->annotated) {
3793 char *smallerthan, *at, *nl, *committer;
3794 char *href_diff = NULL;
3797 if (getline(&line, &linesize, a->f) == -1) {
3799 err = got_error_from_errno("getline");
3803 committer = bline->committer;
3804 smallerthan = strchr(committer, '<');
3805 if (smallerthan && smallerthan[1] != '\0')
3806 committer = smallerthan + 1;
3807 at = strchr(committer, '@');
3810 len = strlen(committer);
3812 committer[8] = '\0';
3814 nl = strchr(line, '\n');
3818 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3819 "blame_wrapper", KATTR__MAX);
3820 if (kerr != KCGI_OK)
3822 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3823 "blame_number", KATTR__MAX);
3824 if (kerr != KCGI_OK)
3826 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3827 a->nlines_prec, a->lineno_cur);
3828 if (kerr != KCGI_OK)
3830 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3831 if (kerr != KCGI_OK)
3834 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3835 "blame_hash", KATTR__MAX);
3836 if (kerr != KCGI_OK)
3839 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3840 a->gw_trans->repo_name, "action", "diff", "commit",
3841 bline->id_str, NULL);
3842 if (href_diff == NULL) {
3843 err = got_error_from_errno("khttp_urlpart");
3846 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3847 KATTR_HREF, href_diff, KATTR__MAX);
3848 if (kerr != KCGI_OK)
3850 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3852 if (kerr != KCGI_OK)
3854 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3855 if (kerr != KCGI_OK)
3858 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3859 "blame_date", KATTR__MAX);
3860 if (kerr != KCGI_OK)
3862 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3863 if (kerr != KCGI_OK)
3865 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3866 if (kerr != KCGI_OK)
3869 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3870 "blame_author", KATTR__MAX);
3871 if (kerr != KCGI_OK)
3873 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3874 if (kerr != KCGI_OK)
3876 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3877 if (kerr != KCGI_OK)
3880 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3881 "blame_code", KATTR__MAX);
3882 if (kerr != KCGI_OK)
3884 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3885 if (kerr != KCGI_OK)
3887 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3888 if (kerr != KCGI_OK)
3891 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3892 if (kerr != KCGI_OK)
3896 bline = &a->lines[a->lineno_cur - 1];
3902 got_object_commit_close(commit);
3904 if (err == NULL && kerr != KCGI_OK)
3905 err = gw_kcgi_error(kerr);
3909 static const struct got_error *
3910 gw_output_file_blame(struct gw_trans *gw_trans)
3912 const struct got_error *error = NULL;
3913 struct got_object_id *obj_id = NULL;
3914 struct got_object_id *commit_id = NULL;
3915 struct got_blob_object *blob = NULL;
3916 char *path = NULL, *in_repo_path = NULL;
3917 struct gw_blame_cb_args bca;
3921 if (asprintf(&path, "%s%s%s",
3922 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3923 gw_trans->repo_folder ? "/" : "",
3924 gw_trans->repo_file) == -1) {
3925 error = got_error_from_errno("asprintf");
3929 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3933 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3934 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3938 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3943 if (obj_id == NULL) {
3944 error = got_error(GOT_ERR_NO_OBJ);
3948 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3952 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3953 error = got_error(GOT_ERR_OBJ_TYPE);
3957 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3961 bca.f = got_opentemp();
3962 if (bca.f == NULL) {
3963 error = got_error_from_errno("got_opentemp");
3966 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3967 &bca.line_offsets, bca.f, blob);
3968 if (error || bca.nlines == 0)
3971 /* Don't include \n at EOF in the blame line count. */
3972 if (bca.line_offsets[bca.nlines - 1] == filesize)
3975 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3976 if (bca.lines == NULL) {
3977 error = got_error_from_errno("calloc");
3981 bca.nlines_prec = 0;
3987 bca.repo = gw_trans->repo;
3988 bca.gw_trans = gw_trans;
3990 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3999 free(bca.line_offsets);
4000 for (i = 0; i < bca.nlines; i++) {
4001 struct blame_line *bline = &bca.lines[i];
4002 free(bline->id_str);
4003 free(bline->committer);
4006 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4007 error = got_error_from_errno("fclose");
4010 got_object_blob_close(blob);
4014 static const struct got_error *
4015 gw_output_blob_buf(struct gw_trans *gw_trans)
4017 const struct got_error *error = NULL;
4018 struct got_object_id *obj_id = NULL;
4019 struct got_object_id *commit_id = NULL;
4020 struct got_blob_object *blob = NULL;
4021 char *path = NULL, *in_repo_path = NULL;
4022 int obj_type, set_mime = 0;
4025 enum kcgi_err kerr = KCGI_OK;
4027 if (asprintf(&path, "%s%s%s",
4028 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4029 gw_trans->repo_folder ? "/" : "",
4030 gw_trans->repo_file) == -1) {
4031 error = got_error_from_errno("asprintf");
4035 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
4039 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4040 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
4044 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4049 if (obj_id == NULL) {
4050 error = got_error(GOT_ERR_NO_OBJ);
4054 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4058 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4059 error = got_error(GOT_ERR_OBJ_TYPE);
4063 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4067 hdrlen = got_object_blob_get_hdrlen(blob);
4069 error = got_object_blob_read_block(&len, blob);
4072 buf = got_object_blob_get_read_buf(blob);
4075 * Skip blob object header first time around,
4076 * which also contains a zero byte.
4079 if (set_mime == 0) {
4080 if (isbinary(buf, len - hdrlen))
4081 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4083 gw_trans->mime = KMIME_TEXT_PLAIN;
4085 error = gw_display_index(gw_trans);
4089 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4090 if (kerr != KCGI_OK)
4100 got_object_blob_close(blob);
4101 if (error == NULL && kerr != KCGI_OK)
4102 error = gw_kcgi_error(kerr);
4106 static const struct got_error *
4107 gw_output_repo_tree(struct gw_trans *gw_trans)
4109 const struct got_error *error = NULL;
4110 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4111 struct got_tree_object *tree = NULL;
4112 char *path = NULL, *in_repo_path = NULL;
4113 char *id_str = NULL;
4114 char *build_folder = NULL;
4115 char *href_blob = NULL, *href_blame = NULL;
4116 const char *class = NULL;
4117 int nentries, i, class_flip = 0;
4118 enum kcgi_err kerr = KCGI_OK;
4120 if (gw_trans->repo_folder != NULL) {
4121 path = strdup(gw_trans->repo_folder);
4123 error = got_error_from_errno("strdup");
4127 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4128 gw_trans->repo_path, 1);
4132 path = in_repo_path;
4135 if (gw_trans->commit_id == NULL) {
4136 struct got_reference *head_ref;
4137 error = got_ref_open(&head_ref, gw_trans->repo,
4138 gw_trans->headref, 0);
4141 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4144 got_ref_close(head_ref);
4146 * gw_trans->commit_id was not parsed from the querystring
4147 * we hit this code path from gw_index, where we don't know the
4148 * commit values for the tree link yet, so set
4149 * gw_trans->commit_id here to continue further into the tree
4151 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4156 error = got_repo_match_object_id(&commit_id, NULL,
4157 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
4163 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4168 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4172 nentries = got_object_tree_get_nentries(tree);
4173 for (i = 0; i < nentries; i++) {
4174 struct got_tree_entry *te;
4175 const char *modestr = "";
4178 te = got_object_tree_get_entry(tree, i);
4180 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4184 mode = got_tree_entry_get_mode(te);
4185 if (got_object_tree_entry_is_submodule(te))
4187 else if (S_ISLNK(mode))
4189 else if (S_ISDIR(mode))
4191 else if (mode & S_IXUSR)
4194 if (class_flip == 0) {
4195 class = "back_lightgray";
4198 class = "back_white";
4202 if (S_ISDIR(mode)) {
4203 if (asprintf(&build_folder, "%s/%s",
4204 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4205 got_tree_entry_get_name(te)) == -1) {
4206 error = got_error_from_errno("asprintf");
4210 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4211 gw_trans->repo_name, "action",
4212 gw_get_action_name(gw_trans), "commit",
4213 gw_trans->commit_id, "folder", build_folder, NULL);
4214 if (href_blob == NULL) {
4215 error = got_error_from_errno("khttp_urlpart");
4218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4219 KATTR_ID, "tree_wrapper", KATTR__MAX);
4220 if (kerr != KCGI_OK)
4222 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4223 KATTR_ID, "tree_line", KATTR_CLASS, class,
4225 if (kerr != KCGI_OK)
4227 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4228 KATTR_HREF, href_blob, KATTR_CLASS,
4229 "diff_directory", KATTR__MAX);
4230 if (kerr != KCGI_OK)
4232 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4233 got_tree_entry_get_name(te), modestr);
4234 if (kerr != KCGI_OK)
4236 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4237 if (kerr != KCGI_OK)
4239 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4240 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4242 if (kerr != KCGI_OK)
4244 kerr = khtml_entity(gw_trans->gw_html_req,
4246 if (kerr != KCGI_OK)
4248 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4249 if (kerr != KCGI_OK)
4252 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4253 gw_trans->repo_name, "action", "blob", "commit",
4254 gw_trans->commit_id, "file",
4255 got_tree_entry_get_name(te), "folder",
4256 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4258 if (href_blob == NULL) {
4259 error = got_error_from_errno("khttp_urlpart");
4262 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4263 gw_trans->repo_name, "action", "blame", "commit",
4264 gw_trans->commit_id, "file",
4265 got_tree_entry_get_name(te), "folder",
4266 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4268 if (href_blame == NULL) {
4269 error = got_error_from_errno("khttp_urlpart");
4272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4273 KATTR_ID, "tree_wrapper", KATTR__MAX);
4274 if (kerr != KCGI_OK)
4276 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4277 KATTR_ID, "tree_line", KATTR_CLASS, class,
4279 if (kerr != KCGI_OK)
4281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4282 KATTR_HREF, href_blob, KATTR__MAX);
4283 if (kerr != KCGI_OK)
4285 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4286 got_tree_entry_get_name(te), modestr);
4287 if (kerr != KCGI_OK)
4289 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4290 if (kerr != KCGI_OK)
4292 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4293 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4295 if (kerr != KCGI_OK)
4298 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4299 KATTR_HREF, href_blob, KATTR__MAX);
4300 if (kerr != KCGI_OK)
4302 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4303 if (kerr != KCGI_OK)
4305 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4306 if (kerr != KCGI_OK)
4309 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4310 if (kerr != KCGI_OK)
4313 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4314 KATTR_HREF, href_blame, KATTR__MAX);
4315 if (kerr != KCGI_OK)
4317 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4318 if (kerr != KCGI_OK)
4321 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4322 if (kerr != KCGI_OK)
4330 build_folder = NULL;
4334 got_object_tree_close(tree);
4341 if (error == NULL && kerr != KCGI_OK)
4342 error = gw_kcgi_error(kerr);
4346 static const struct got_error *
4347 gw_output_repo_heads(struct gw_trans *gw_trans)
4349 const struct got_error *error = NULL;
4350 struct got_reflist_head refs;
4351 struct got_reflist_entry *re;
4352 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4353 char *href_commits = NULL;
4354 enum kcgi_err kerr = KCGI_OK;
4356 SIMPLEQ_INIT(&refs);
4358 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4359 got_ref_cmp_by_name, NULL);
4363 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4364 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4365 if (kerr != KCGI_OK)
4367 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4368 KATTR_ID, "summary_heads_title", KATTR__MAX);
4369 if (kerr != KCGI_OK)
4371 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4372 if (kerr != KCGI_OK)
4374 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4375 if (kerr != KCGI_OK)
4377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4378 KATTR_ID, "summary_heads_content", KATTR__MAX);
4379 if (kerr != KCGI_OK)
4382 SIMPLEQ_FOREACH(re, &refs, entry) {
4383 const char *refname;
4385 if (got_ref_is_symbolic(re->ref))
4388 refname = got_ref_get_name(re->ref);
4389 if (strncmp(refname, "refs/heads/", 11) != 0)
4392 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4397 if (strncmp(refname, "refs/heads/", 11) == 0)
4400 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4401 KATTR_ID, "heads_wrapper", KATTR__MAX);
4402 if (kerr != KCGI_OK)
4404 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4405 KATTR_ID, "heads_age", KATTR__MAX);
4406 if (kerr != KCGI_OK)
4408 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4409 if (kerr != KCGI_OK)
4411 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4412 if (kerr != KCGI_OK)
4414 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4415 KATTR_ID, "heads_space", KATTR__MAX);
4416 if (kerr != KCGI_OK)
4418 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4419 if (kerr != KCGI_OK)
4421 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4422 if (kerr != KCGI_OK)
4424 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4425 KATTR_ID, "head", KATTR__MAX);
4426 if (kerr != KCGI_OK)
4429 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4430 gw_trans->repo_name, "action", "summary", "headref",
4432 if (href_summary == NULL) {
4433 error = got_error_from_errno("khttp_urlpart");
4436 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4437 href_summary, KATTR__MAX);
4438 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4439 if (kerr != KCGI_OK)
4441 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4442 if (kerr != KCGI_OK)
4445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4446 "navs_wrapper", KATTR__MAX);
4447 if (kerr != KCGI_OK)
4449 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4450 "navs", KATTR__MAX);
4451 if (kerr != KCGI_OK)
4454 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4455 href_summary, KATTR__MAX);
4456 if (kerr != KCGI_OK)
4458 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4459 if (kerr != KCGI_OK)
4461 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4462 if (kerr != KCGI_OK)
4465 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4466 if (kerr != KCGI_OK)
4469 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4470 gw_trans->repo_name, "action", "briefs", "headref",
4472 if (href_briefs == NULL) {
4473 error = got_error_from_errno("khttp_urlpart");
4476 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4477 href_briefs, KATTR__MAX);
4478 if (kerr != KCGI_OK)
4480 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4481 if (kerr != KCGI_OK)
4483 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4484 if (kerr != KCGI_OK)
4487 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4488 if (kerr != KCGI_OK)
4491 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4492 gw_trans->repo_name, "action", "commits", "headref",
4494 if (href_commits == NULL) {
4495 error = got_error_from_errno("khttp_urlpart");
4498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4499 href_commits, KATTR__MAX);
4500 if (kerr != KCGI_OK)
4502 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4503 if (kerr != KCGI_OK)
4505 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4506 if (kerr != KCGI_OK)
4509 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4510 "dotted_line", KATTR__MAX);
4511 if (kerr != KCGI_OK)
4513 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4514 if (kerr != KCGI_OK)
4517 href_summary = NULL;
4521 href_commits = NULL;
4524 got_ref_list_free(&refs);
4531 static const struct got_error *
4532 gw_output_site_link(struct gw_trans *gw_trans)
4534 const struct got_error *error = NULL;
4535 char *href_summary = NULL;
4536 enum kcgi_err kerr = KCGI_OK;
4538 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4539 "site_link", KATTR__MAX);
4540 if (kerr != KCGI_OK)
4542 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4544 if (kerr != KCGI_OK)
4546 kerr = khtml_puts(gw_trans->gw_html_req,
4547 gw_trans->gw_conf->got_site_link);
4548 if (kerr != KCGI_OK)
4550 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4551 if (kerr != KCGI_OK)
4554 if (gw_trans->repo_name != NULL) {
4555 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4556 if (kerr != KCGI_OK)
4559 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4560 gw_trans->repo_name, "action", "summary", NULL);
4561 if (href_summary == NULL) {
4562 error = got_error_from_errno("khttp_urlpart");
4565 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4566 href_summary, KATTR__MAX);
4567 if (kerr != KCGI_OK)
4569 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4570 if (kerr != KCGI_OK)
4572 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4573 if (kerr != KCGI_OK)
4575 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4576 gw_get_action_name(gw_trans));
4577 if (kerr != KCGI_OK)
4581 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4582 if (kerr != KCGI_OK)
4586 if (error == NULL && kerr != KCGI_OK)
4587 error = gw_kcgi_error(kerr);
4591 static const struct got_error *
4592 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4594 const struct got_error *error = NULL;
4596 enum kcgi_err kerr = KCGI_OK;
4598 if (strncmp(buf, "-", 1) == 0)
4599 color = "diff_minus";
4600 else if (strncmp(buf, "+", 1) == 0)
4601 color = "diff_plus";
4602 else if (strncmp(buf, "@@", 2) == 0)
4603 color = "diff_chunk_header";
4604 else if (strncmp(buf, "@@", 2) == 0)
4605 color = "diff_chunk_header";
4606 else if (strncmp(buf, "commit +", 8) == 0)
4607 color = "diff_meta";
4608 else if (strncmp(buf, "commit -", 8) == 0)
4609 color = "diff_meta";
4610 else if (strncmp(buf, "blob +", 6) == 0)
4611 color = "diff_meta";
4612 else if (strncmp(buf, "blob -", 6) == 0)
4613 color = "diff_meta";
4614 else if (strncmp(buf, "file +", 6) == 0)
4615 color = "diff_meta";
4616 else if (strncmp(buf, "file -", 6) == 0)
4617 color = "diff_meta";
4618 else if (strncmp(buf, "from:", 5) == 0)
4619 color = "diff_author";
4620 else if (strncmp(buf, "via:", 4) == 0)
4621 color = "diff_author";
4622 else if (strncmp(buf, "date:", 5) == 0)
4623 color = "diff_date";
4624 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4625 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4626 if (error == NULL && kerr != KCGI_OK)
4627 error = gw_kcgi_error(kerr);
4632 main(int argc, char *argv[])
4634 const struct got_error *error = NULL;
4635 struct gw_trans *gw_trans;
4636 struct gw_dir *dir = NULL, *tdir;
4637 const char *page = "index";
4639 enum kcgi_err kerr = KCGI_OK;
4641 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4644 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4647 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4650 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4653 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4654 if (kerr != KCGI_OK) {
4655 error = gw_kcgi_error(kerr);
4659 TAILQ_INIT(&gw_trans->gw_dirs);
4660 TAILQ_INIT(&gw_trans->gw_headers);
4662 gw_trans->action = -1;
4664 gw_trans->repos_total = 0;
4665 gw_trans->repo_path = NULL;
4666 gw_trans->commit_id = NULL;
4667 gw_trans->next_id = NULL;
4668 gw_trans->next_prev_id = NULL;
4669 gw_trans->prev_id = NULL;
4670 gw_trans->prev_prev_id = NULL;
4671 gw_trans->headref = GOT_REF_HEAD;
4672 gw_trans->mime = KMIME_TEXT_HTML;
4673 gw_trans->gw_tmpl->key = gw_templs;
4674 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4675 gw_trans->gw_tmpl->arg = gw_trans;
4676 gw_trans->gw_tmpl->cb = gw_template;
4678 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4682 error = gw_parse_querystring(gw_trans);
4686 if (gw_trans->action == GW_BLOB)
4687 error = gw_blob(gw_trans);
4689 error = gw_display_index(gw_trans);
4692 free(gw_trans->gw_conf->got_repos_path);
4693 free(gw_trans->gw_conf->got_site_name);
4694 free(gw_trans->gw_conf->got_site_owner);
4695 free(gw_trans->gw_conf->got_site_link);
4696 free(gw_trans->gw_conf->got_logo);
4697 free(gw_trans->gw_conf->got_logo_url);
4698 free(gw_trans->gw_conf);
4699 free(gw_trans->commit_id);
4700 free(gw_trans->next_id);
4701 free(gw_trans->next_prev_id);
4702 free(gw_trans->prev_id);
4703 free(gw_trans->prev_prev_id);
4704 free(gw_trans->repo_path);
4706 got_repo_close(gw_trans->repo);
4708 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4710 free(dir->description);
4719 khttp_free(gw_trans->gw_req);