2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
20 #include <sys/types.h>
34 #include <got_error.h>
35 #include <got_object.h>
36 #include <got_reference.h>
37 #include <got_repository.h>
39 #include <got_cancel.h>
40 #include <got_worktree.h>
42 #include <got_commit_graph.h>
43 #include <got_blame.h>
44 #include <got_privsep.h>
45 #include <got_opentemp.h>
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 TAILQ_HEAD(headers, gw_header) gw_headers;
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct got_repository *repo;
60 struct gw_dir *gw_dir;
61 struct gotweb_config *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
65 const struct got_error *error;
66 const char *repo_name;
73 const char *repo_file;
78 unsigned int repos_total;
83 TAILQ_ENTRY(gw_header) entry;
84 struct got_reflist_head refs;
88 char *commit_id; /* id_str1 */
89 char *parent_id; /* id_str2 */
94 time_t committer_time;
98 TAILQ_ENTRY(gw_dir) entry;
141 static const char *const gw_templs[TEMPL__MAX] = {
151 static const struct kvalid gw_keys[KEY__ZMAX] = {
152 { kvalid_stringne, "action" },
153 { kvalid_stringne, "commit" },
154 { kvalid_stringne, "file" },
155 { kvalid_stringne, "folder" },
156 { kvalid_stringne, "headref" },
157 { kvalid_int, "page" },
158 { kvalid_stringne, "path" },
159 { kvalid_stringne, "prev" },
160 { kvalid_stringne, "prev_prev" },
163 static struct gw_header *gw_init_header(void);
165 static void gw_free_header(struct gw_header *);
167 static int gw_template(size_t, void *);
169 static const struct got_error *gw_error(struct gw_trans *);
170 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
171 static const struct got_error *gw_get_repo_description(char **,
172 struct gw_trans *, char *);
173 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
175 static const struct got_error *gw_get_time_str(char **, time_t, int);
176 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
177 char *, const char *, int);
178 static const struct got_error *gw_output_file_blame(struct gw_trans *,
180 static const struct got_error *gw_output_blob_buf(struct gw_trans *,
182 static const struct got_error *gw_output_repo_tree(struct gw_trans *,
184 static const struct got_error *gw_output_diff(struct gw_trans *,
186 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
187 struct gw_header *, int, int);
188 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
189 static const struct got_error *gw_output_site_link(struct gw_trans *);
190 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
192 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
194 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
196 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
198 static const struct got_error *gw_gen_author_header(struct gw_trans *,
200 static const struct got_error *gw_gen_age_header(struct gw_trans *,
202 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
204 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
206 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
207 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
209 static const struct got_error *gw_display_index(struct gw_trans *);
210 static const struct got_error *gw_get_header(struct gw_trans *,
211 struct gw_header *, int);
212 static const struct got_error *gw_get_commits(struct gw_trans *,
213 struct gw_header *, int,
214 struct got_object_id *);
215 static const struct got_error *gw_get_commit(struct gw_trans *,
217 struct got_commit_object *,
218 struct got_object_id *);
219 static const struct got_error *gw_apply_unveil(const char *);
220 static const struct got_error *gw_blame_cb(void *, int, int,
221 struct got_object_id *);
222 static const struct got_error *gw_load_got_paths(struct gw_trans *);
223 static const struct got_error *gw_load_got_path(struct gw_trans *,
225 static const struct got_error *gw_parse_querystring(struct gw_trans *);
226 static const struct got_error *gw_blame(struct gw_trans *);
227 static const struct got_error *gw_blob(struct gw_trans *);
228 static const struct got_error *gw_diff(struct gw_trans *);
229 static const struct got_error *gw_index(struct gw_trans *);
230 static const struct got_error *gw_commits(struct gw_trans *);
231 static const struct got_error *gw_briefs(struct gw_trans *);
232 static const struct got_error *gw_summary(struct gw_trans *);
233 static const struct got_error *gw_tree(struct gw_trans *);
234 static const struct got_error *gw_tag(struct gw_trans *);
235 static const struct got_error *gw_tags(struct gw_trans *);
237 struct gw_query_action {
238 unsigned int func_id;
239 const char *func_name;
240 const struct got_error *(*func_main)(struct gw_trans *);
244 enum gw_query_actions {
258 static struct gw_query_action gw_query_funcs[] = {
259 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
260 { GW_BLOB, "blob", NULL, NULL },
261 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
262 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
263 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
264 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
265 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
266 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
267 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
268 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
269 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
273 gw_get_action_name(struct gw_trans *gw_trans)
275 return gw_query_funcs[gw_trans->action].func_name;
278 static const struct got_error *
279 gw_kcgi_error(enum kcgi_err kerr)
284 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
285 return got_error(GOT_ERR_CANCELLED);
287 if (kerr == KCGI_ENOMEM)
288 return got_error_set_errno(ENOMEM,
289 kcgi_strerror(kerr));
291 if (kerr == KCGI_ENFILE)
292 return got_error_set_errno(ENFILE,
293 kcgi_strerror(kerr));
295 if (kerr == KCGI_EAGAIN)
296 return got_error_set_errno(EAGAIN,
297 kcgi_strerror(kerr));
299 if (kerr == KCGI_FORM)
300 return got_error_msg(GOT_ERR_IO,
301 kcgi_strerror(kerr));
303 return got_error_from_errno(kcgi_strerror(kerr));
306 static const struct got_error *
307 gw_apply_unveil(const char *repo_path)
309 const struct got_error *err;
311 if (repo_path && unveil(repo_path, "r") != 0)
312 return got_error_from_errno2("unveil", repo_path);
314 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
315 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
317 err = got_privsep_unveil_exec_helpers();
321 if (unveil(NULL, NULL) != 0)
322 return got_error_from_errno("unveil");
328 isbinary(const uint8_t *buf, size_t n)
332 for (i = 0; i < n; i++)
338 static const struct got_error *
339 gw_blame(struct gw_trans *gw_trans)
341 const struct got_error *error = NULL;
342 struct gw_header *header = NULL;
344 enum kcgi_err kerr = KCGI_OK;
346 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
348 return got_error_from_errno("pledge");
350 if ((header = gw_init_header()) == NULL)
351 return got_error_from_errno("malloc");
353 error = gw_apply_unveil(gw_trans->gw_dir->path);
357 /* check querystring */
358 if (gw_trans->repo_file == NULL) {
359 error = got_error_msg(GOT_ERR_QUERYSTRING,
360 "file required in querystring");
363 if (gw_trans->commit_id == NULL) {
364 error = got_error_msg(GOT_ERR_QUERYSTRING,
365 "commit required in querystring");
369 error = gw_get_header(gw_trans, header, 1);
372 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
373 "blame_header_wrapper", KATTR__MAX);
376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
377 "blame_header", KATTR__MAX);
380 error = gw_get_time_str(&age, header->committer_time,
384 error = gw_gen_age_header(gw_trans, age ?age : "");
387 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
390 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
393 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
394 "dotted_line", KATTR__MAX);
397 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
401 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
402 "blame", KATTR__MAX);
405 error = gw_output_file_blame(gw_trans, header);
408 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
410 gw_free_header(header);
411 if (error == NULL && kerr != KCGI_OK)
412 error = gw_kcgi_error(kerr);
416 static const struct got_error *
417 gw_blob(struct gw_trans *gw_trans)
419 const struct got_error *error = NULL, *err = NULL;
420 struct gw_header *header = NULL;
421 enum kcgi_err kerr = KCGI_OK;
423 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
425 return got_error_from_errno("pledge");
427 if ((header = gw_init_header()) == NULL)
428 return got_error_from_errno("malloc");
430 error = gw_apply_unveil(gw_trans->gw_dir->path);
434 /* check querystring */
435 if (gw_trans->repo_file == NULL) {
436 error = got_error_msg(GOT_ERR_QUERYSTRING,
437 "file required in querystring");
440 if (gw_trans->commit_id == NULL) {
441 error = got_error_msg(GOT_ERR_QUERYSTRING,
442 "commit required in querystring");
445 error = gw_get_header(gw_trans, header, 1);
449 error = gw_output_blob_buf(gw_trans, header);
453 gw_trans->mime = KMIME_TEXT_PLAIN;
454 err = gw_display_index(gw_trans);
459 kerr = khttp_puts(gw_trans->gw_req, error->msg);
462 gw_free_header(header);
463 if (error == NULL && kerr != KCGI_OK)
464 error = gw_kcgi_error(kerr);
468 static const struct got_error *
469 gw_diff(struct gw_trans *gw_trans)
471 const struct got_error *error = NULL;
472 struct gw_header *header = NULL;
474 enum kcgi_err kerr = KCGI_OK;
476 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
478 return got_error_from_errno("pledge");
480 if ((header = gw_init_header()) == NULL)
481 return got_error_from_errno("malloc");
483 error = gw_apply_unveil(gw_trans->gw_dir->path);
487 error = gw_get_header(gw_trans, header, 1);
491 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
492 "diff_header_wrapper", KATTR__MAX);
495 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
496 "diff_header", KATTR__MAX);
499 error = gw_gen_diff_header(gw_trans, header->parent_id,
503 error = gw_gen_commit_header(gw_trans, header->commit_id,
507 error = gw_gen_tree_header(gw_trans, header->tree_id);
510 error = gw_gen_author_header(gw_trans, header->author);
513 error = gw_gen_committer_header(gw_trans, header->author);
516 error = gw_get_time_str(&age, header->committer_time,
520 error = gw_gen_age_header(gw_trans, age ?age : "");
523 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
526 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
529 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
530 "dotted_line", KATTR__MAX);
533 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
537 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
541 error = gw_output_diff(gw_trans, header);
545 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
547 gw_free_header(header);
549 if (error == NULL && kerr != KCGI_OK)
550 error = gw_kcgi_error(kerr);
554 static const struct got_error *
555 gw_index(struct gw_trans *gw_trans)
557 const struct got_error *error = NULL;
558 struct gw_dir *gw_dir = NULL;
559 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
560 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
561 char *href_tags = NULL;
562 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
563 enum kcgi_err kerr = KCGI_OK;
565 if (pledge("stdio rpath proc exec sendfd unveil",
567 error = got_error_from_errno("pledge");
571 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
575 error = gw_load_got_paths(gw_trans);
579 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
580 "index_header", KATTR__MAX);
582 return gw_kcgi_error(kerr);
583 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
584 "index_header_project", KATTR__MAX);
586 return gw_kcgi_error(kerr);
587 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
589 return gw_kcgi_error(kerr);
590 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
592 return gw_kcgi_error(kerr);
594 if (gw_trans->gw_conf->got_show_repo_description) {
595 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
596 "index_header_description", KATTR__MAX);
598 return gw_kcgi_error(kerr);
599 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
601 return gw_kcgi_error(kerr);
602 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
604 return gw_kcgi_error(kerr);
607 if (gw_trans->gw_conf->got_show_repo_owner) {
608 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
609 "index_header_owner", KATTR__MAX);
611 return gw_kcgi_error(kerr);
612 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
614 return gw_kcgi_error(kerr);
615 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
617 return gw_kcgi_error(kerr);
620 if (gw_trans->gw_conf->got_show_repo_age) {
621 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
622 "index_header_age", KATTR__MAX);
624 return gw_kcgi_error(kerr);
625 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
627 return gw_kcgi_error(kerr);
628 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
630 return gw_kcgi_error(kerr);
633 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
635 return gw_kcgi_error(kerr);
637 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
638 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
639 "index_wrapper", KATTR__MAX);
641 return gw_kcgi_error(kerr);
642 kerr = khtml_printf(gw_trans->gw_html_req,
643 "No repositories found in %s",
644 gw_trans->gw_conf->got_repos_path);
646 return gw_kcgi_error(kerr);
647 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
649 return gw_kcgi_error(kerr);
650 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
651 "dotted_line", KATTR__MAX);
653 return gw_kcgi_error(kerr);
654 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
656 return gw_kcgi_error(kerr);
660 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
663 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
664 if (gw_trans->page > 0 && (gw_trans->page *
665 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
672 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
673 "index_wrapper", KATTR__MAX);
677 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
678 gw_dir->name, "action", "summary", NULL);
679 if (href_summary == NULL) {
680 error = got_error_from_errno("khttp_urlpart");
683 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
684 "index_project", KATTR__MAX);
687 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
688 href_summary, KATTR__MAX);
691 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
694 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
697 if (gw_trans->gw_conf->got_show_repo_description) {
698 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
699 KATTR_ID, "index_project_description", KATTR__MAX);
702 kerr = khtml_puts(gw_trans->gw_html_req,
703 gw_dir->description ? gw_dir->description : "");
706 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
710 if (gw_trans->gw_conf->got_show_repo_owner) {
711 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
712 KATTR_ID, "index_project_owner", KATTR__MAX);
715 kerr = khtml_puts(gw_trans->gw_html_req,
716 gw_dir->owner ? gw_dir->owner : "");
719 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
723 if (gw_trans->gw_conf->got_show_repo_age) {
724 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
725 KATTR_ID, "index_project_age", KATTR__MAX);
728 kerr = khtml_puts(gw_trans->gw_html_req,
729 gw_dir->age ? gw_dir->age : "");
732 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
737 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
738 "navs_wrapper", KATTR__MAX);
741 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
746 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
747 href_summary, KATTR__MAX);
750 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
753 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
757 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
761 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
762 gw_dir->name, "action", "briefs", NULL);
763 if (href_briefs == NULL) {
764 error = got_error_from_errno("khttp_urlpart");
767 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
768 href_briefs, KATTR__MAX);
771 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
774 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
776 error = gw_kcgi_error(kerr);
778 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
782 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
783 gw_dir->name, "action", "commits", NULL);
784 if (href_commits == NULL) {
785 error = got_error_from_errno("khttp_urlpart");
788 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
789 href_commits, KATTR__MAX);
792 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
795 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
799 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
803 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
804 gw_dir->name, "action", "tags", NULL);
805 if (href_tags == NULL) {
806 error = got_error_from_errno("khttp_urlpart");
809 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
810 href_tags, KATTR__MAX);
813 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
816 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
820 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
824 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
825 gw_dir->name, "action", "tree", NULL);
826 if (href_tree == NULL) {
827 error = got_error_from_errno("khttp_urlpart");
830 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
831 href_tree, KATTR__MAX);
834 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
838 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
842 "dotted_line", KATTR__MAX);
845 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
860 if (gw_trans->gw_conf->got_max_repos_display == 0)
863 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
864 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
865 (gw_trans->page > 0) &&
866 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
867 prev_disp == gw_trans->repos_total))) {
868 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
869 KATTR_ID, "np_wrapper", KATTR__MAX);
872 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
873 KATTR_ID, "nav_prev", KATTR__MAX);
878 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
879 (gw_trans->page > 0) &&
880 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
881 prev_disp == gw_trans->repos_total)) {
882 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
883 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
884 if (href_prev == NULL) {
885 error = got_error_from_errno("khttp_urlpartx");
888 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
889 KATTR_HREF, href_prev, KATTR__MAX);
892 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
895 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
900 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
902 return gw_kcgi_error(kerr);
904 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
905 next_disp == gw_trans->gw_conf->got_max_repos_display &&
906 dir_c != (gw_trans->page + 1) *
907 gw_trans->gw_conf->got_max_repos_display) {
908 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
909 KATTR_ID, "nav_next", KATTR__MAX);
912 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
913 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
914 if (href_next == NULL) {
915 error = got_error_from_errno("khttp_urlpartx");
918 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
919 KATTR_HREF, href_next, KATTR__MAX);
922 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
925 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
932 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
933 (gw_trans->page > 0) &&
934 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
935 prev_disp == gw_trans->repos_total)) {
936 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
950 if (error == NULL && kerr != KCGI_OK)
951 error = gw_kcgi_error(kerr);
955 static const struct got_error *
956 gw_commits(struct gw_trans *gw_trans)
958 const struct got_error *error = NULL;
959 struct gw_header *header = NULL, *n_header = NULL;
960 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
961 char *href_prev = NULL, *href_next = NULL;
962 enum kcgi_err kerr = KCGI_OK;
964 if ((header = gw_init_header()) == NULL)
965 return got_error_from_errno("malloc");
967 if (pledge("stdio rpath proc exec sendfd unveil",
969 error = got_error_from_errno("pledge");
973 error = gw_apply_unveil(gw_trans->gw_dir->path);
977 error = gw_get_header(gw_trans, header,
978 gw_trans->gw_conf->got_max_commits_display);
982 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
983 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
984 "commits_line_wrapper", KATTR__MAX);
987 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
991 error = gw_gen_author_header(gw_trans, n_header->author);
994 error = gw_gen_committer_header(gw_trans, n_header->author);
997 error = gw_get_time_str(&age, n_header->committer_time,
1001 error = gw_gen_age_header(gw_trans, age ?age : "");
1004 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1005 if (kerr != KCGI_OK)
1008 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1009 "dotted_line", KATTR__MAX);
1010 if (kerr != KCGI_OK)
1012 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1013 if (kerr != KCGI_OK)
1016 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1017 "commit", KATTR__MAX);
1018 if (kerr != KCGI_OK)
1020 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1021 if (kerr != KCGI_OK)
1023 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1024 if (kerr != KCGI_OK)
1027 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1028 gw_trans->repo_name, "action", "diff", "commit",
1029 n_header->commit_id, NULL);
1030 if (href_diff == NULL) {
1031 error = got_error_from_errno("khttp_urlpart");
1034 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1035 KATTR_ID, "navs_wrapper", KATTR__MAX);
1036 if (kerr != KCGI_OK)
1038 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1039 KATTR_ID, "navs", KATTR__MAX);
1040 if (kerr != KCGI_OK)
1042 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1043 KATTR_HREF, href_diff, KATTR__MAX);
1044 if (kerr != KCGI_OK)
1046 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1047 if (kerr != KCGI_OK)
1049 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1050 if (kerr != KCGI_OK)
1053 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1054 if (kerr != KCGI_OK)
1057 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1058 gw_trans->repo_name, "action", "tree", "commit",
1059 n_header->commit_id, NULL);
1060 if (href_tree == NULL) {
1061 error = got_error_from_errno("khttp_urlpart");
1064 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1065 KATTR_HREF, href_tree, KATTR__MAX);
1066 if (kerr != KCGI_OK)
1068 khtml_puts(gw_trans->gw_html_req, "tree");
1069 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1070 if (kerr != KCGI_OK)
1072 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1073 if (kerr != KCGI_OK)
1076 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1077 "solid_line", KATTR__MAX);
1078 if (kerr != KCGI_OK)
1080 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1081 if (kerr != KCGI_OK)
1088 if (gw_trans->next_id || gw_trans->page > 0) {
1089 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1090 KATTR_ID, "np_wrapper", KATTR__MAX);
1091 if (kerr != KCGI_OK)
1093 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1094 KATTR_ID, "nav_prev", KATTR__MAX);
1095 if (kerr != KCGI_OK)
1099 if (gw_trans->page > 0 && gw_trans->prev_id) {
1100 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1101 KATTRX_STRING, gw_trans->repo_name, "page",
1102 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1103 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1104 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1105 KATTRX_STRING, gw_trans->prev_prev_id ?
1106 gw_trans->prev_prev_id : "", NULL);
1107 if (href_prev == NULL) {
1108 error = got_error_from_errno("khttp_urlpartx");
1111 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1112 KATTR_HREF, href_prev, KATTR__MAX);
1113 if (kerr != KCGI_OK)
1115 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1116 if (kerr != KCGI_OK)
1118 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1119 if (kerr != KCGI_OK)
1123 if (gw_trans->next_id || gw_trans->page > 0) {
1124 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1125 if (kerr != KCGI_OK)
1126 return gw_kcgi_error(kerr);
1129 if (gw_trans->next_id) {
1130 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1131 KATTR_ID, "nav_next", KATTR__MAX);
1132 if (kerr != KCGI_OK)
1134 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1135 KATTRX_STRING, gw_trans->repo_name, "page",
1136 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1137 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1138 gw_trans->next_id, "prev", KATTRX_STRING,
1139 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1140 "prev_prev", KATTRX_STRING, gw_trans->prev_prev_id ?
1141 gw_trans->prev_prev_id : "", NULL);
1142 if (href_next == NULL) {
1143 error = got_error_from_errno("khttp_urlpartx");
1146 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1147 KATTR_HREF, href_next, KATTR__MAX);
1148 if (kerr != KCGI_OK)
1150 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1151 if (kerr != KCGI_OK)
1153 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1154 if (kerr != KCGI_OK)
1158 if (gw_trans->next_id || gw_trans->page > 0) {
1159 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1160 if (kerr != KCGI_OK)
1164 gw_free_header(header);
1165 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1166 gw_free_header(n_header);
1172 if (error == NULL && kerr != KCGI_OK)
1173 error = gw_kcgi_error(kerr);
1177 static const struct got_error *
1178 gw_briefs(struct gw_trans *gw_trans)
1180 const struct got_error *error = NULL;
1181 struct gw_header *header = NULL, *n_header = NULL;
1182 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1183 char *href_prev = NULL, *href_next = NULL;
1184 char *newline, *smallerthan;
1185 enum kcgi_err kerr = KCGI_OK;
1187 if ((header = gw_init_header()) == NULL)
1188 return got_error_from_errno("malloc");
1190 if (pledge("stdio rpath proc exec sendfd unveil",
1192 error = got_error_from_errno("pledge");
1196 if (gw_trans->action != GW_SUMMARY) {
1197 error = gw_apply_unveil(gw_trans->gw_dir->path);
1202 if (gw_trans->action == GW_SUMMARY)
1203 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1205 error = gw_get_header(gw_trans, header,
1206 gw_trans->gw_conf->got_max_commits_display);
1210 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1211 error = gw_get_time_str(&age, n_header->committer_time,
1216 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1217 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1218 if (kerr != KCGI_OK)
1221 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1222 KATTR_ID, "briefs_age", KATTR__MAX);
1223 if (kerr != KCGI_OK)
1225 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1226 if (kerr != KCGI_OK)
1228 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1229 if (kerr != KCGI_OK)
1232 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1233 KATTR_ID, "briefs_author", KATTR__MAX);
1234 if (kerr != KCGI_OK)
1236 smallerthan = strchr(n_header->author, '<');
1238 *smallerthan = '\0';
1239 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1240 if (kerr != KCGI_OK)
1242 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1243 if (kerr != KCGI_OK)
1246 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1247 gw_trans->repo_name, "action", "diff", "commit",
1248 n_header->commit_id, NULL);
1249 if (href_diff == NULL) {
1250 error = got_error_from_errno("khttp_urlpart");
1253 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1254 KATTR_ID, "briefs_log", KATTR__MAX);
1255 if (kerr != KCGI_OK)
1257 newline = strchr(n_header->commit_msg, '\n');
1260 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1261 KATTR_HREF, href_diff, KATTR__MAX);
1262 if (kerr != KCGI_OK)
1264 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1265 if (kerr != KCGI_OK)
1267 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1268 if (kerr != KCGI_OK)
1271 if (n_header->refs_str) {
1272 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1273 if (kerr != KCGI_OK)
1275 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1276 KATTR_ID, "refs_str", KATTR__MAX);
1277 if (kerr != KCGI_OK)
1279 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1280 n_header->refs_str);
1281 if (kerr != KCGI_OK)
1283 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1284 if (kerr != KCGI_OK)
1288 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1289 if (kerr != KCGI_OK)
1292 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1293 KATTR_ID, "navs_wrapper", KATTR__MAX);
1294 if (kerr != KCGI_OK)
1296 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1297 KATTR_ID, "navs", KATTR__MAX);
1298 if (kerr != KCGI_OK)
1300 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1301 KATTR_HREF, href_diff, KATTR__MAX);
1302 if (kerr != KCGI_OK)
1304 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1305 if (kerr != KCGI_OK)
1307 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1308 if (kerr != KCGI_OK)
1311 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1312 if (kerr != KCGI_OK)
1315 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1316 gw_trans->repo_name, "action", "tree", "commit",
1317 n_header->commit_id, NULL);
1318 if (href_tree == NULL) {
1319 error = got_error_from_errno("khttp_urlpart");
1322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1323 KATTR_HREF, href_tree, KATTR__MAX);
1324 if (kerr != KCGI_OK)
1326 khtml_puts(gw_trans->gw_html_req, "tree");
1327 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1328 if (kerr != KCGI_OK)
1330 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1331 if (kerr != KCGI_OK)
1334 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1335 KATTR_ID, "dotted_line", KATTR__MAX);
1336 if (kerr != KCGI_OK)
1338 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1339 if (kerr != KCGI_OK)
1350 if (gw_trans->next_id || gw_trans->page > 0) {
1351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1352 KATTR_ID, "np_wrapper", KATTR__MAX);
1353 if (kerr != KCGI_OK)
1355 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1356 KATTR_ID, "nav_prev", KATTR__MAX);
1357 if (kerr != KCGI_OK)
1361 if (gw_trans->page > 0 && gw_trans->prev_id) {
1362 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1363 KATTRX_STRING, gw_trans->repo_name, "page",
1364 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1365 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1366 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1367 KATTRX_STRING, gw_trans->prev_prev_id ?
1368 gw_trans->prev_prev_id : "", NULL);
1369 if (href_prev == NULL) {
1370 error = got_error_from_errno("khttp_urlpartx");
1373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1374 KATTR_HREF, href_prev, KATTR__MAX);
1375 if (kerr != KCGI_OK)
1377 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1378 if (kerr != KCGI_OK)
1380 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1381 if (kerr != KCGI_OK)
1385 if (gw_trans->next_id || gw_trans->page > 0) {
1386 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1387 if (kerr != KCGI_OK)
1388 return gw_kcgi_error(kerr);
1391 if (gw_trans->next_id) {
1392 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1393 KATTR_ID, "nav_next", KATTR__MAX);
1394 if (kerr != KCGI_OK)
1397 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1398 KATTRX_STRING, gw_trans->repo_name, "page",
1399 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1400 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1401 gw_trans->next_id, "prev", KATTRX_STRING,
1402 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1403 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1404 gw_trans->prev_id : "", NULL);
1405 if (href_next == NULL) {
1406 error = got_error_from_errno("khttp_urlpartx");
1409 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1410 KATTR_HREF, href_next, KATTR__MAX);
1411 if (kerr != KCGI_OK)
1413 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1414 if (kerr != KCGI_OK)
1416 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1417 if (kerr != KCGI_OK)
1421 if (gw_trans->next_id || gw_trans->page > 0) {
1422 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1423 if (kerr != KCGI_OK)
1427 gw_free_header(header);
1428 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1429 gw_free_header(n_header);
1435 if (error == NULL && kerr != KCGI_OK)
1436 error = gw_kcgi_error(kerr);
1440 static const struct got_error *
1441 gw_summary(struct gw_trans *gw_trans)
1443 const struct got_error *error = NULL;
1445 enum kcgi_err kerr = KCGI_OK;
1447 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1448 return got_error_from_errno("pledge");
1450 error = gw_apply_unveil(gw_trans->gw_dir->path);
1454 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1455 "summary_wrapper", KATTR__MAX);
1456 if (kerr != KCGI_OK)
1457 return gw_kcgi_error(kerr);
1459 if (gw_trans->gw_conf->got_show_repo_description &&
1460 gw_trans->gw_dir->description != NULL &&
1461 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1462 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1463 KATTR_ID, "description_title", KATTR__MAX);
1464 if (kerr != KCGI_OK)
1466 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1467 if (kerr != KCGI_OK)
1469 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1470 if (kerr != KCGI_OK)
1472 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1473 KATTR_ID, "description", KATTR__MAX);
1474 if (kerr != KCGI_OK)
1476 kerr = khtml_puts(gw_trans->gw_html_req,
1477 gw_trans->gw_dir->description);
1478 if (kerr != KCGI_OK)
1480 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1481 if (kerr != KCGI_OK)
1485 if (gw_trans->gw_conf->got_show_repo_owner &&
1486 gw_trans->gw_dir->owner != NULL &&
1487 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1488 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1489 KATTR_ID, "repo_owner_title", KATTR__MAX);
1490 if (kerr != KCGI_OK)
1492 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1493 if (kerr != KCGI_OK)
1495 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1496 if (kerr != KCGI_OK)
1498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1499 KATTR_ID, "repo_owner", KATTR__MAX);
1500 if (kerr != KCGI_OK)
1502 kerr = khtml_puts(gw_trans->gw_html_req,
1503 gw_trans->gw_dir->owner);
1504 if (kerr != KCGI_OK)
1506 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1507 if (kerr != KCGI_OK)
1511 if (gw_trans->gw_conf->got_show_repo_age) {
1512 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1517 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1518 KATTR_ID, "last_change_title", KATTR__MAX);
1519 if (kerr != KCGI_OK)
1521 kerr = khtml_puts(gw_trans->gw_html_req,
1523 if (kerr != KCGI_OK)
1525 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1526 if (kerr != KCGI_OK)
1528 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1529 KATTR_ID, "last_change", KATTR__MAX);
1530 if (kerr != KCGI_OK)
1532 kerr = khtml_puts(gw_trans->gw_html_req, age);
1533 if (kerr != KCGI_OK)
1535 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1536 if (kerr != KCGI_OK)
1541 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1542 gw_trans->gw_dir->url != NULL &&
1543 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1545 KATTR_ID, "cloneurl_title", KATTR__MAX);
1546 if (kerr != KCGI_OK)
1548 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1549 if (kerr != KCGI_OK)
1551 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1552 if (kerr != KCGI_OK)
1554 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1555 KATTR_ID, "cloneurl", KATTR__MAX);
1556 if (kerr != KCGI_OK)
1558 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1559 if (kerr != KCGI_OK)
1561 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1562 if (kerr != KCGI_OK)
1566 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1567 if (kerr != KCGI_OK)
1570 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1571 "briefs_title_wrapper", KATTR__MAX);
1572 if (kerr != KCGI_OK)
1574 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1575 "briefs_title", KATTR__MAX);
1576 if (kerr != KCGI_OK)
1578 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1579 if (kerr != KCGI_OK)
1581 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1582 if (kerr != KCGI_OK)
1584 error = gw_briefs(gw_trans);
1588 error = gw_tags(gw_trans);
1592 error = gw_output_repo_heads(gw_trans);
1595 if (error == NULL && kerr != KCGI_OK)
1596 error = gw_kcgi_error(kerr);
1600 static const struct got_error *
1601 gw_tree(struct gw_trans *gw_trans)
1603 const struct got_error *error = NULL;
1604 struct gw_header *header = NULL;
1605 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1607 enum kcgi_err kerr = KCGI_OK;
1609 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1610 return got_error_from_errno("pledge");
1612 if ((header = gw_init_header()) == NULL)
1613 return got_error_from_errno("malloc");
1615 error = gw_apply_unveil(gw_trans->gw_dir->path);
1619 error = gw_get_header(gw_trans, header, 1);
1623 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1624 "tree_header_wrapper", KATTR__MAX);
1625 if (kerr != KCGI_OK)
1627 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1628 "tree_header", KATTR__MAX);
1629 if (kerr != KCGI_OK)
1631 error = gw_gen_tree_header(gw_trans, header->tree_id);
1634 error = gw_get_time_str(&age, header->committer_time,
1638 error = gw_gen_age_header(gw_trans, age ?age : "");
1641 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1644 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1645 if (kerr != KCGI_OK)
1647 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1648 "dotted_line", KATTR__MAX);
1649 if (kerr != KCGI_OK)
1651 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1652 if (kerr != KCGI_OK)
1655 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1656 "tree", KATTR__MAX);
1657 if (kerr != KCGI_OK)
1659 error = gw_output_repo_tree(gw_trans, header);
1663 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1665 gw_free_header(header);
1666 free(tree_html_disp);
1670 if (error == NULL && kerr != KCGI_OK)
1671 error = gw_kcgi_error(kerr);
1675 static const struct got_error *
1676 gw_tags(struct gw_trans *gw_trans)
1678 const struct got_error *error = NULL;
1679 struct gw_header *header = NULL;
1680 char *href_next = NULL, *href_prev = NULL;
1681 enum kcgi_err kerr = KCGI_OK;
1683 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1684 return got_error_from_errno("pledge");
1686 if ((header = gw_init_header()) == NULL)
1687 return got_error_from_errno("malloc");
1689 if (gw_trans->action != GW_SUMMARY) {
1690 error = gw_apply_unveil(gw_trans->gw_dir->path);
1695 error = gw_get_header(gw_trans, header, 1);
1699 if (gw_trans->action == GW_SUMMARY) {
1700 gw_trans->next_id = NULL;
1701 error = gw_output_repo_tags(gw_trans, header,
1702 D_MAXSLCOMMDISP, TAGBRIEF);
1706 error = gw_output_repo_tags(gw_trans, header,
1707 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1712 if (gw_trans->next_id || gw_trans->page > 0) {
1713 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1714 KATTR_ID, "np_wrapper", KATTR__MAX);
1715 if (kerr != KCGI_OK)
1717 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1718 KATTR_ID, "nav_prev", KATTR__MAX);
1719 if (kerr != KCGI_OK)
1723 if (gw_trans->page > 0 && gw_trans->prev_id) {
1724 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1725 KATTRX_STRING, gw_trans->repo_name, "page",
1726 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1727 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1728 gw_trans->prev_id ? gw_trans->prev_id : "", "prev",
1729 KATTRX_STRING, gw_trans->prev_prev_id ?
1730 gw_trans->prev_prev_id : "", NULL);
1731 if (href_prev == NULL) {
1732 error = got_error_from_errno("khttp_urlpartx");
1735 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1736 KATTR_HREF, href_prev, KATTR__MAX);
1737 if (kerr != KCGI_OK)
1739 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1740 if (kerr != KCGI_OK)
1742 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1743 if (kerr != KCGI_OK)
1747 if (gw_trans->next_id || gw_trans->page > 0) {
1748 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1749 if (kerr != KCGI_OK)
1750 return gw_kcgi_error(kerr);
1753 if (gw_trans->next_id) {
1754 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1755 KATTR_ID, "nav_next", KATTR__MAX);
1756 if (kerr != KCGI_OK)
1758 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1759 KATTRX_STRING, gw_trans->repo_name, "page",
1760 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1761 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1762 gw_trans->next_id, "prev", KATTRX_STRING,
1763 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1764 "prev_prev", KATTRX_STRING, gw_trans->prev_id ?
1765 gw_trans->prev_id : "", NULL);
1766 if (href_next == NULL) {
1767 error = got_error_from_errno("khttp_urlpartx");
1770 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1771 KATTR_HREF, href_next, KATTR__MAX);
1772 if (kerr != KCGI_OK)
1774 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1775 if (kerr != KCGI_OK)
1777 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1778 if (kerr != KCGI_OK)
1782 if (gw_trans->next_id || gw_trans->page > 0) {
1783 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1784 if (kerr != KCGI_OK)
1788 gw_free_header(header);
1791 if (error == NULL && kerr != KCGI_OK)
1792 error = gw_kcgi_error(kerr);
1796 static const struct got_error *
1797 gw_tag(struct gw_trans *gw_trans)
1799 const struct got_error *error = NULL;
1800 struct gw_header *header = NULL;
1801 enum kcgi_err kerr = KCGI_OK;
1803 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1804 return got_error_from_errno("pledge");
1806 if ((header = gw_init_header()) == NULL)
1807 return got_error_from_errno("malloc");
1809 error = gw_apply_unveil(gw_trans->gw_dir->path);
1813 if (gw_trans->commit_id == NULL) {
1814 error = got_error_msg(GOT_ERR_QUERYSTRING,
1815 "commit required in querystring");
1819 error = gw_get_header(gw_trans, header, 1);
1823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1824 "tag_header_wrapper", KATTR__MAX);
1825 if (kerr != KCGI_OK)
1827 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1828 "tag_header", KATTR__MAX);
1829 if (kerr != KCGI_OK)
1831 error = gw_gen_commit_header(gw_trans, header->commit_id,
1835 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1838 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1839 if (kerr != KCGI_OK)
1841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1842 "dotted_line", KATTR__MAX);
1843 if (kerr != KCGI_OK)
1845 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1846 if (kerr != KCGI_OK)
1849 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1850 "tree", KATTR__MAX);
1851 if (kerr != KCGI_OK)
1854 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1858 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1860 gw_free_header(header);
1861 if (error == NULL && kerr != KCGI_OK)
1862 error = gw_kcgi_error(kerr);
1866 static const struct got_error *
1867 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1869 const struct got_error *error = NULL;
1874 if (asprintf(&dir_test, "%s/%s/%s",
1875 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1876 GOTWEB_GIT_DIR) == -1)
1877 return got_error_from_errno("asprintf");
1879 dt = opendir(dir_test);
1883 gw_dir->path = strdup(dir_test);
1884 if (gw_dir->path == NULL) {
1886 error = got_error_from_errno("strdup");
1893 if (asprintf(&dir_test, "%s/%s/%s",
1894 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1895 GOTWEB_GOT_DIR) == -1) {
1897 error = got_error_from_errno("asprintf");
1901 dt = opendir(dir_test);
1906 error = got_error(GOT_ERR_NOT_GIT_REPO);
1910 if (asprintf(&dir_test, "%s/%s",
1911 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1912 error = got_error_from_errno("asprintf");
1917 gw_dir->path = strdup(dir_test);
1918 if (gw_dir->path == NULL) {
1920 error = got_error_from_errno("strdup");
1924 dt = opendir(dir_test);
1926 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1931 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1935 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1938 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1942 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1946 if (dt && closedir(dt) == -1 && error == NULL)
1947 error = got_error_from_errno("closedir");
1951 static const struct got_error *
1952 gw_load_got_paths(struct gw_trans *gw_trans)
1954 const struct got_error *error = NULL;
1956 struct dirent **sd_dent;
1957 struct gw_dir *gw_dir;
1959 unsigned int d_cnt, d_i;
1961 d = opendir(gw_trans->gw_conf->got_repos_path);
1963 error = got_error_from_errno2("opendir",
1964 gw_trans->gw_conf->got_repos_path);
1968 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1971 error = got_error_from_errno2("scandir",
1972 gw_trans->gw_conf->got_repos_path);
1976 for (d_i = 0; d_i < d_cnt; d_i++) {
1977 if (gw_trans->gw_conf->got_max_repos > 0 &&
1978 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1979 break; /* account for parent and self */
1981 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1982 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1985 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1989 error = gw_load_got_path(gw_trans, gw_dir);
1990 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1997 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1998 !got_path_dir_is_empty(gw_dir->path)) {
1999 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
2001 gw_trans->repos_total++;
2005 if (d && closedir(d) == -1 && error == NULL)
2006 error = got_error_from_errno("closedir");
2010 static const struct got_error *
2011 gw_parse_querystring(struct gw_trans *gw_trans)
2013 const struct got_error *error = NULL;
2015 struct gw_query_action *action = NULL;
2018 if (gw_trans->gw_req->fieldnmap[0]) {
2019 return got_error(GOT_ERR_QUERYSTRING);
2020 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2021 /* define gw_trans->repo_path */
2022 gw_trans->repo_name = p->parsed.s;
2024 if (asprintf(&gw_trans->repo_path, "%s/%s",
2025 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2026 return got_error_from_errno("asprintf");
2028 /* get action and set function */
2029 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2030 for (i = 0; i < nitems(gw_query_funcs); i++) {
2031 action = &gw_query_funcs[i];
2032 if (action->func_name == NULL)
2034 if (strcmp(action->func_name,
2035 p->parsed.s) == 0) {
2036 gw_trans->action = i;
2041 if (gw_trans->action == -1) {
2042 gw_trans->action = GW_ERR;
2043 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2044 p != NULL ? "bad action in querystring" :
2045 "no action in querystring");
2049 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2050 if (asprintf(&gw_trans->commit_id, "%s",
2052 return got_error_from_errno("asprintf");
2055 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2056 gw_trans->repo_file = p->parsed.s;
2058 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2059 if (asprintf(&gw_trans->repo_folder, "%s",
2061 return got_error_from_errno("asprintf");
2064 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2065 if (asprintf(&gw_trans->prev_id, "%s",
2067 return got_error_from_errno("asprintf");
2070 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
2071 if (asprintf(&gw_trans->prev_prev_id, "%s",
2073 return got_error_from_errno("asprintf");
2076 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2077 gw_trans->headref = p->parsed.s;
2079 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2083 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2085 gw_trans->action = GW_INDEX;
2087 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2088 gw_trans->page = p->parsed.i;
2093 static const struct got_error *
2094 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2096 const struct got_error *error;
2098 *gw_dir = malloc(sizeof(**gw_dir));
2099 if (*gw_dir == NULL)
2100 return got_error_from_errno("malloc");
2102 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2103 error = got_error_from_errno("asprintf");
2112 static const struct got_error *
2113 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2115 enum kcgi_err kerr = KCGI_OK;
2117 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2118 if (kerr != KCGI_OK)
2119 return gw_kcgi_error(kerr);
2120 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2122 if (kerr != KCGI_OK)
2123 return gw_kcgi_error(kerr);
2124 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2126 if (kerr != KCGI_OK)
2127 return gw_kcgi_error(kerr);
2128 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2130 if (kerr != KCGI_OK)
2131 return gw_kcgi_error(kerr);
2132 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2133 if (kerr != KCGI_OK)
2134 return gw_kcgi_error(kerr);
2135 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2137 if (kerr != KCGI_OK)
2138 return gw_kcgi_error(kerr);
2140 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2141 kerr = khttp_head(gw_trans->gw_req,
2142 kresps[KRESP_CONTENT_DISPOSITION],
2143 "attachment; filename=%s", gw_trans->repo_file);
2144 if (kerr != KCGI_OK)
2145 return gw_kcgi_error(kerr);
2148 kerr = khttp_body(gw_trans->gw_req);
2149 return gw_kcgi_error(kerr);
2152 static const struct got_error *
2153 gw_display_index(struct gw_trans *gw_trans)
2155 const struct got_error *error;
2156 enum kcgi_err kerr = KCGI_OK;
2158 /* catch early querystring errors */
2159 if (gw_trans->error)
2160 gw_trans->action = GW_ERR;
2162 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2166 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2167 if (kerr != KCGI_OK)
2168 return gw_kcgi_error(kerr);
2170 if (gw_trans->action != GW_BLOB) {
2171 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2172 gw_query_funcs[gw_trans->action].template);
2173 if (kerr != KCGI_OK) {
2174 khtml_close(gw_trans->gw_html_req);
2175 return gw_kcgi_error(kerr);
2179 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2182 static const struct got_error *
2183 gw_error(struct gw_trans *gw_trans)
2185 enum kcgi_err kerr = KCGI_OK;
2187 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2189 return gw_kcgi_error(kerr);
2193 gw_template(size_t key, void *arg)
2195 const struct got_error *error = NULL;
2196 enum kcgi_err kerr = KCGI_OK;
2197 struct gw_trans *gw_trans = arg;
2198 char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
2199 char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
2201 if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
2202 "/apple-touch-icon.png") == -1)
2204 if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
2205 "/favicon-32x32.png") == -1)
2207 if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
2208 "/favicon-16x16.png") == -1)
2210 if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
2211 "/site.webmanifest") == -1)
2213 if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
2214 "/safari-pinned-tab.svg") == -1)
2216 if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
2217 "/gotweb.css") == -1)
2219 if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
2220 gw_trans->gw_conf->got_www_path ? "/" : "",
2221 gw_trans->gw_conf->got_logo) == -1)
2226 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2227 KATTR_NAME, "viewport",
2228 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2230 if (kerr != KCGI_OK)
2232 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2233 if (kerr != KCGI_OK)
2235 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2236 KATTR_CHARSET, "utf-8",
2238 if (kerr != KCGI_OK)
2240 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2241 if (kerr != KCGI_OK)
2243 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2244 KATTR_NAME, "msapplication-TileColor",
2245 KATTR_CONTENT, "#da532c", KATTR__MAX);
2246 if (kerr != KCGI_OK)
2248 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2249 if (kerr != KCGI_OK)
2251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2252 KATTR_NAME, "theme-color",
2253 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2254 if (kerr != KCGI_OK)
2256 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2257 if (kerr != KCGI_OK)
2259 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2260 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2261 KATTR_HREF, ati, KATTR__MAX);
2262 if (kerr != KCGI_OK)
2264 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2265 if (kerr != KCGI_OK)
2267 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2268 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2269 "32x32", KATTR_HREF, fic32, KATTR__MAX);
2270 if (kerr != KCGI_OK)
2272 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2273 if (kerr != KCGI_OK)
2275 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2276 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2277 "16x16", KATTR_HREF, fic16, KATTR__MAX);
2278 if (kerr != KCGI_OK)
2280 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2281 if (kerr != KCGI_OK)
2283 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2284 KATTR_REL, "manifest", KATTR_HREF, swm,
2286 if (kerr != KCGI_OK)
2288 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2289 if (kerr != KCGI_OK)
2291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2292 KATTR_REL, "mask-icon", KATTR_HREF,
2294 if (kerr != KCGI_OK)
2296 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2297 if (kerr != KCGI_OK)
2299 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2300 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2301 KATTR_HREF, css, KATTR__MAX);
2302 if (kerr != KCGI_OK)
2304 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2305 if (kerr != KCGI_OK)
2309 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2310 KATTR_ID, "got_link", KATTR__MAX);
2311 if (kerr != KCGI_OK)
2313 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2314 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2315 KATTR_TARGET, "_sotd", KATTR__MAX);
2316 if (kerr != KCGI_OK)
2318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2319 KATTR_SRC, logo, KATTR__MAX);
2320 if (kerr != KCGI_OK)
2322 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2323 if (kerr != KCGI_OK)
2326 case (TEMPL_SITEPATH):
2327 error = gw_output_site_link(gw_trans);
2332 if (gw_trans->gw_conf->got_site_name != NULL) {
2333 kerr = khtml_puts(gw_trans->gw_html_req,
2334 gw_trans->gw_conf->got_site_name);
2335 if (kerr != KCGI_OK)
2339 case (TEMPL_SEARCH):
2341 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2342 "search", KATTR__MAX);
2343 if (kerr != KCGI_OK)
2345 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2346 KATTR_METHOD, "POST", KATTR__MAX);
2347 if (kerr != KCGI_OK)
2349 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2350 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2351 KATTR_MAXLENGTH, "50", KATTR__MAX);
2352 if (kerr != KCGI_OK)
2354 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2356 if (kerr != KCGI_OK)
2358 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2359 if (kerr != KCGI_OK)
2361 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2362 if (kerr != KCGI_OK)
2365 case(TEMPL_SITEOWNER):
2366 if (gw_trans->gw_conf->got_site_owner != NULL &&
2367 gw_trans->gw_conf->got_show_site_owner) {
2368 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2369 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2370 if (kerr != KCGI_OK)
2372 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2373 KATTR_ID, "site_owner", KATTR__MAX);
2374 if (kerr != KCGI_OK)
2376 kerr = khtml_puts(gw_trans->gw_html_req,
2377 gw_trans->gw_conf->got_site_owner);
2378 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2379 if (kerr != KCGI_OK)
2383 case(TEMPL_CONTENT):
2384 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2387 KATTR_ID, "tmpl_err", KATTR__MAX);
2388 if (kerr != KCGI_OK)
2390 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2392 if (kerr != KCGI_OK)
2394 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2395 if (kerr != KCGI_OK)
2421 static const struct got_error *
2422 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2424 const struct got_error *error = NULL;
2425 enum kcgi_err kerr = KCGI_OK;
2427 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2428 KATTR_ID, "header_commit_title", KATTR__MAX);
2429 if (kerr != KCGI_OK)
2431 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2432 if (kerr != KCGI_OK)
2434 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2435 if (kerr != KCGI_OK)
2437 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2438 KATTR_ID, "header_commit", KATTR__MAX);
2439 if (kerr != KCGI_OK)
2441 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2442 if (kerr != KCGI_OK)
2445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2446 KATTR_ID, "refs_str", KATTR__MAX);
2447 if (kerr != KCGI_OK)
2449 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2450 if (kerr != KCGI_OK)
2452 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2453 if (kerr != KCGI_OK)
2456 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2458 if (error == NULL && kerr != KCGI_OK)
2459 error = gw_kcgi_error(kerr);
2463 static const struct got_error *
2464 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2466 const struct got_error *error = NULL;
2467 enum kcgi_err kerr = KCGI_OK;
2469 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2470 KATTR_ID, "header_diff_title", KATTR__MAX);
2471 if (kerr != KCGI_OK)
2473 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2474 if (kerr != KCGI_OK)
2476 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2477 if (kerr != KCGI_OK)
2479 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2480 KATTR_ID, "header_diff", KATTR__MAX);
2481 if (kerr != KCGI_OK)
2484 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2485 if (kerr != KCGI_OK)
2488 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2489 if (kerr != KCGI_OK)
2491 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2492 if (kerr != KCGI_OK)
2494 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2496 if (error == NULL && kerr != KCGI_OK)
2497 error = gw_kcgi_error(kerr);
2501 static const struct got_error *
2502 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2504 const struct got_error *error = NULL;
2505 enum kcgi_err kerr = KCGI_OK;
2507 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2508 KATTR_ID, "header_age_title", KATTR__MAX);
2509 if (kerr != KCGI_OK)
2511 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2512 if (kerr != KCGI_OK)
2514 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2515 if (kerr != KCGI_OK)
2517 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2518 KATTR_ID, "header_age", KATTR__MAX);
2519 if (kerr != KCGI_OK)
2521 kerr = khtml_puts(gw_trans->gw_html_req, str);
2522 if (kerr != KCGI_OK)
2524 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2526 if (error == NULL && kerr != KCGI_OK)
2527 error = gw_kcgi_error(kerr);
2531 static const struct got_error *
2532 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2534 const struct got_error *error = NULL;
2535 enum kcgi_err kerr = KCGI_OK;
2537 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2538 KATTR_ID, "header_author_title", KATTR__MAX);
2539 if (kerr != KCGI_OK)
2541 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2542 if (kerr != KCGI_OK)
2544 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2545 if (kerr != KCGI_OK)
2547 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2548 KATTR_ID, "header_author", KATTR__MAX);
2549 if (kerr != KCGI_OK)
2551 kerr = khtml_puts(gw_trans->gw_html_req, str);
2552 if (kerr != KCGI_OK)
2554 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2556 if (error == NULL && kerr != KCGI_OK)
2557 error = gw_kcgi_error(kerr);
2561 static const struct got_error *
2562 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2564 const struct got_error *error = NULL;
2565 enum kcgi_err kerr = KCGI_OK;
2567 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2568 KATTR_ID, "header_committer_title", KATTR__MAX);
2569 if (kerr != KCGI_OK)
2571 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2572 if (kerr != KCGI_OK)
2574 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2575 if (kerr != KCGI_OK)
2577 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2578 KATTR_ID, "header_committer", KATTR__MAX);
2579 if (kerr != KCGI_OK)
2581 kerr = khtml_puts(gw_trans->gw_html_req, str);
2582 if (kerr != KCGI_OK)
2584 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2586 if (error == NULL && kerr != KCGI_OK)
2587 error = gw_kcgi_error(kerr);
2591 static const struct got_error *
2592 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2594 const struct got_error *error = NULL;
2595 enum kcgi_err kerr = KCGI_OK;
2597 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2598 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2599 if (kerr != KCGI_OK)
2601 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2602 if (kerr != KCGI_OK)
2604 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2605 if (kerr != KCGI_OK)
2607 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2608 KATTR_ID, "header_commit_msg", KATTR__MAX);
2609 if (kerr != KCGI_OK)
2611 kerr = khttp_puts(gw_trans->gw_req, str);
2612 if (kerr != KCGI_OK)
2614 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2616 if (error == NULL && kerr != KCGI_OK)
2617 error = gw_kcgi_error(kerr);
2621 static const struct got_error *
2622 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2624 const struct got_error *error = NULL;
2625 enum kcgi_err kerr = KCGI_OK;
2627 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2628 KATTR_ID, "header_tree_title", KATTR__MAX);
2629 if (kerr != KCGI_OK)
2631 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2632 if (kerr != KCGI_OK)
2634 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2635 if (kerr != KCGI_OK)
2637 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2638 KATTR_ID, "header_tree", KATTR__MAX);
2639 if (kerr != KCGI_OK)
2641 kerr = khtml_puts(gw_trans->gw_html_req, str);
2642 if (kerr != KCGI_OK)
2644 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2646 if (error == NULL && kerr != KCGI_OK)
2647 error = gw_kcgi_error(kerr);
2651 static const struct got_error *
2652 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2655 const struct got_error *error = NULL;
2657 char *d_file = NULL;
2661 *description = NULL;
2662 if (gw_trans->gw_conf->got_show_repo_description == 0)
2665 if (asprintf(&d_file, "%s/description", dir) == -1)
2666 return got_error_from_errno("asprintf");
2668 f = fopen(d_file, "r");
2670 if (errno == ENOENT || errno == EACCES)
2672 error = got_error_from_errno2("fopen", d_file);
2676 if (fseek(f, 0, SEEK_END) == -1) {
2677 error = got_ferror(f, GOT_ERR_IO);
2682 error = got_ferror(f, GOT_ERR_IO);
2685 if (fseek(f, 0, SEEK_SET) == -1) {
2686 error = got_ferror(f, GOT_ERR_IO);
2689 *description = calloc(len + 1, sizeof(**description));
2690 if (*description == NULL) {
2691 error = got_error_from_errno("calloc");
2695 n = fread(*description, 1, len, f);
2696 if (n == 0 && ferror(f))
2697 error = got_ferror(f, GOT_ERR_IO);
2699 if (f != NULL && fclose(f) == EOF && error == NULL)
2700 error = got_error_from_errno("fclose");
2705 static const struct got_error *
2706 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2710 char *years = "years ago", *months = "months ago";
2711 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2712 char *minutes = "minutes ago", *seconds = "seconds ago";
2713 char *now = "right now";
2721 diff_time = time(NULL) - committer_time;
2722 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2723 if (asprintf(repo_age, "%lld %s",
2724 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2725 return got_error_from_errno("asprintf");
2726 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2727 if (asprintf(repo_age, "%lld %s",
2728 (diff_time / 60 / 60 / 24 / (365 / 12)),
2730 return got_error_from_errno("asprintf");
2731 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2732 if (asprintf(repo_age, "%lld %s",
2733 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2734 return got_error_from_errno("asprintf");
2735 } else if (diff_time > 60 * 60 * 24 * 2) {
2736 if (asprintf(repo_age, "%lld %s",
2737 (diff_time / 60 / 60 / 24), days) == -1)
2738 return got_error_from_errno("asprintf");
2739 } else if (diff_time > 60 * 60 * 2) {
2740 if (asprintf(repo_age, "%lld %s",
2741 (diff_time / 60 / 60), hours) == -1)
2742 return got_error_from_errno("asprintf");
2743 } else if (diff_time > 60 * 2) {
2744 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2746 return got_error_from_errno("asprintf");
2747 } else if (diff_time > 2) {
2748 if (asprintf(repo_age, "%lld %s", diff_time,
2750 return got_error_from_errno("asprintf");
2752 if (asprintf(repo_age, "%s", now) == -1)
2753 return got_error_from_errno("asprintf");
2757 if (gmtime_r(&committer_time, &tm) == NULL)
2758 return got_error_from_errno("gmtime_r");
2760 s = asctime_r(&tm, datebuf);
2762 return got_error_from_errno("asctime_r");
2764 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2765 return got_error_from_errno("asprintf");
2771 static const struct got_error *
2772 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2773 const char *refname, int ref_tm)
2775 const struct got_error *error = NULL;
2776 struct got_repository *repo = NULL;
2777 struct got_commit_object *commit = NULL;
2778 struct got_reflist_head refs;
2779 struct got_reflist_entry *re;
2780 time_t committer_time = 0, cmp_time = 0;
2785 if (gw_trans->gw_conf->got_show_repo_age == 0)
2789 repo = gw_trans->repo;
2791 error = got_repo_open(&repo, dir, NULL);
2796 error = got_ref_list(&refs, repo, "refs/heads",
2797 got_ref_cmp_by_name, NULL);
2802 * Find the youngest branch tip in the repository, or the age of
2803 * the a specific branch tip if a name was provided by the caller.
2805 TAILQ_FOREACH(re, &refs, entry) {
2806 struct got_object_id *id = NULL;
2808 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2811 error = got_ref_resolve(&id, repo, re->ref);
2815 error = got_object_open_as_commit(&commit, repo, id);
2821 got_object_commit_get_committer_time(commit);
2822 got_object_commit_close(commit);
2823 if (cmp_time < committer_time)
2824 cmp_time = committer_time;
2830 if (cmp_time != 0) {
2831 committer_time = cmp_time;
2832 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2835 got_ref_list_free(&refs);
2836 if (gw_trans->repo == NULL)
2837 got_repo_close(repo);
2841 static const struct got_error *
2842 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2844 const struct got_error *error;
2846 struct got_object_id *id1 = NULL, *id2 = NULL;
2847 char *label1 = NULL, *label2 = NULL, *line = NULL;
2849 size_t linesize = 0;
2851 enum kcgi_err kerr = KCGI_OK;
2857 if (header->parent_id != NULL &&
2858 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2859 error = got_repo_match_object_id(&id1, &label1,
2860 header->parent_id, GOT_OBJ_TYPE_ANY,
2861 &header->refs, gw_trans->repo);
2866 error = got_repo_match_object_id(&id2, &label2,
2867 header->commit_id, GOT_OBJ_TYPE_ANY, &header->refs,
2872 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2876 case GOT_OBJ_TYPE_BLOB:
2877 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
2878 NULL, NULL, 3, 0, 0, gw_trans->repo, f);
2880 case GOT_OBJ_TYPE_TREE:
2881 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
2882 "", "", 3, 0, 0, gw_trans->repo, f);
2884 case GOT_OBJ_TYPE_COMMIT:
2885 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
2886 3, 0, 0, gw_trans->repo, f);
2889 error = got_error(GOT_ERR_OBJ_TYPE);
2894 if (fseek(f, 0, SEEK_SET) == -1) {
2895 error = got_ferror(f, GOT_ERR_IO);
2899 while ((linelen = getline(&line, &linesize, f)) != -1) {
2900 error = gw_colordiff_line(gw_trans, line);
2903 /* XXX: KHTML_PRETTY breaks this */
2904 kerr = khtml_puts(gw_trans->gw_html_req, line);
2905 if (kerr != KCGI_OK)
2907 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2908 if (kerr != KCGI_OK)
2911 if (linelen == -1 && ferror(f))
2912 error = got_error_from_errno("getline");
2914 if (f && fclose(f) == EOF && error == NULL)
2915 error = got_error_from_errno("fclose");
2922 if (error == NULL && kerr != KCGI_OK)
2923 error = gw_kcgi_error(kerr);
2927 static const struct got_error *
2928 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2930 const struct got_error *error = NULL;
2931 struct got_repository *repo;
2932 const char *gitconfig_owner;
2936 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2939 error = got_repo_open(&repo, dir, NULL);
2942 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2943 if (gitconfig_owner) {
2944 *owner = strdup(gitconfig_owner);
2946 error = got_error_from_errno("strdup");
2948 got_repo_close(repo);
2952 static const struct got_error *
2953 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2955 const struct got_error *error = NULL;
2957 char *d_file = NULL;
2963 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2964 return got_error_from_errno("asprintf");
2966 f = fopen(d_file, "r");
2968 if (errno != ENOENT && errno != EACCES)
2969 error = got_error_from_errno2("fopen", d_file);
2973 if (fseek(f, 0, SEEK_END) == -1) {
2974 error = got_ferror(f, GOT_ERR_IO);
2979 error = got_ferror(f, GOT_ERR_IO);
2982 if (fseek(f, 0, SEEK_SET) == -1) {
2983 error = got_ferror(f, GOT_ERR_IO);
2987 *url = calloc(len + 1, sizeof(**url));
2989 error = got_error_from_errno("calloc");
2993 n = fread(*url, 1, len, f);
2994 if (n == 0 && ferror(f))
2995 error = got_ferror(f, GOT_ERR_IO);
2997 if (f && fclose(f) == EOF && error == NULL)
2998 error = got_error_from_errno("fclose");
3003 static const struct got_error *
3004 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3005 int limit, int tag_type)
3007 const struct got_error *error = NULL;
3008 struct got_reflist_head refs;
3009 struct got_reflist_entry *re;
3011 char *id_str = NULL, *newline, *href_commits = NULL;
3012 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3013 struct got_tag_object *tag = NULL;
3014 enum kcgi_err kerr = KCGI_OK;
3015 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
3016 int prev_set = 0, tag_count = 0;
3020 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3021 got_ref_cmp_tags, gw_trans->repo);
3025 TAILQ_FOREACH(re, &refs, entry) {
3026 const char *refname;
3028 const char *tag_commit;
3030 struct got_object_id *id;
3031 struct got_commit_object *commit = NULL;
3033 refname = got_ref_get_name(re->ref);
3034 if (strncmp(refname, "refs/tags/", 10) != 0)
3038 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3042 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3044 if (error->code != GOT_ERR_OBJ_TYPE) {
3048 /* "lightweight" tag */
3049 error = got_object_open_as_commit(&commit,
3050 gw_trans->repo, id);
3055 tagger = got_object_commit_get_committer(commit);
3057 got_object_commit_get_committer_time(commit);
3058 error = got_object_id_str(&id_str, id);
3062 tagger = got_object_tag_get_tagger(tag);
3063 tagger_time = got_object_tag_get_tagger_time(tag);
3064 error = got_object_id_str(&id_str,
3065 got_object_tag_get_object_id(tag));
3070 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3071 strlen(id_str)) != 0)
3074 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3075 start_tag == 0 && strncmp(id_str, header->commit_id,
3076 strlen(id_str)) != 0) {
3084 if (prev_set == 0 && start_tag == 1) {
3085 gw_trans->next_prev_id = strdup(id_str);
3086 if (gw_trans->next_prev_id == NULL) {
3087 error = got_error_from_errno("strdup");
3094 gw_trans->next_id = strdup(id_str);
3095 if (gw_trans->next_id == NULL)
3096 error = got_error_from_errno("strdup");
3101 error = got_object_commit_get_logmsg(&tag_commit0,
3105 got_object_commit_close(commit);
3107 tag_commit0 = strdup(got_object_tag_get_message(tag));
3108 if (tag_commit0 == NULL) {
3109 error = got_error_from_errno("strdup");
3114 tag_commit = tag_commit0;
3115 while (*tag_commit == '\n')
3120 newline = strchr(tag_commit, '\n');
3124 if (summary_header_displayed == 0) {
3125 kerr = khtml_attr(gw_trans->gw_html_req,
3126 KELEM_DIV, KATTR_ID,
3127 "summary_tags_title_wrapper", KATTR__MAX);
3128 if (kerr != KCGI_OK)
3130 kerr = khtml_attr(gw_trans->gw_html_req,
3131 KELEM_DIV, KATTR_ID,
3132 "summary_tags_title", KATTR__MAX);
3133 if (kerr != KCGI_OK)
3135 kerr = khtml_puts(gw_trans->gw_html_req,
3137 if (kerr != KCGI_OK)
3139 kerr = khtml_closeelem(gw_trans->gw_html_req,
3141 if (kerr != KCGI_OK)
3143 kerr = khtml_attr(gw_trans->gw_html_req,
3144 KELEM_DIV, KATTR_ID,
3145 "summary_tags_content", KATTR__MAX);
3146 if (kerr != KCGI_OK)
3148 summary_header_displayed = 1;
3151 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3152 KATTR_ID, "tag_wrapper", KATTR__MAX);
3153 if (kerr != KCGI_OK)
3155 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3156 KATTR_ID, "tag_age", KATTR__MAX);
3157 if (kerr != KCGI_OK)
3159 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3162 kerr = khtml_puts(gw_trans->gw_html_req,
3164 if (kerr != KCGI_OK)
3166 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3167 if (kerr != KCGI_OK)
3169 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3170 KATTR_ID, "tag", KATTR__MAX);
3171 if (kerr != KCGI_OK)
3173 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3174 if (kerr != KCGI_OK)
3176 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3177 if (kerr != KCGI_OK)
3179 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3180 KATTR_ID, "tag_name", KATTR__MAX);
3181 if (kerr != KCGI_OK)
3184 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3185 gw_trans->repo_name, "action", "tag", "commit",
3187 if (href_tag == NULL) {
3188 error = got_error_from_errno("khttp_urlpart");
3191 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3192 KATTR_HREF, href_tag, KATTR__MAX);
3193 if (kerr != KCGI_OK)
3195 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3196 if (kerr != KCGI_OK)
3198 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3199 if (kerr != KCGI_OK)
3202 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3203 KATTR_ID, "navs_wrapper", KATTR__MAX);
3204 if (kerr != KCGI_OK)
3206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3207 KATTR_ID, "navs", KATTR__MAX);
3208 if (kerr != KCGI_OK)
3211 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3212 KATTR_HREF, href_tag, KATTR__MAX);
3213 if (kerr != KCGI_OK)
3215 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3216 if (kerr != KCGI_OK)
3218 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3219 if (kerr != KCGI_OK)
3222 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3223 if (kerr != KCGI_OK)
3226 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3227 "path", gw_trans->repo_name, "action", "briefs",
3228 "commit", id_str, NULL);
3229 if (href_briefs == NULL) {
3230 error = got_error_from_errno("khttp_urlpart");
3233 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3234 KATTR_HREF, href_briefs, KATTR__MAX);
3235 if (kerr != KCGI_OK)
3237 kerr = khtml_puts(gw_trans->gw_html_req,
3239 if (kerr != KCGI_OK)
3241 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3242 if (kerr != KCGI_OK)
3245 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3246 if (kerr != KCGI_OK)
3249 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3250 "path", gw_trans->repo_name, "action", "commits",
3251 "commit", id_str, NULL);
3252 if (href_commits == NULL) {
3253 error = got_error_from_errno("khttp_urlpart");
3256 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3257 KATTR_HREF, href_commits, KATTR__MAX);
3258 if (kerr != KCGI_OK)
3260 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3261 if (kerr != KCGI_OK)
3263 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3264 if (kerr != KCGI_OK)
3267 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3268 KATTR_ID, "dotted_line", KATTR__MAX);
3269 if (kerr != KCGI_OK)
3271 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3272 if (kerr != KCGI_OK)
3276 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3277 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3278 if (kerr != KCGI_OK)
3280 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3281 if (kerr != KCGI_OK)
3283 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3284 if (kerr != KCGI_OK)
3286 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3287 KATTR_ID, "tag_info_date", KATTR__MAX);
3288 if (kerr != KCGI_OK)
3290 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3293 kerr = khtml_puts(gw_trans->gw_html_req,
3295 if (kerr != KCGI_OK)
3297 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3298 if (kerr != KCGI_OK)
3301 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3302 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3303 if (kerr != KCGI_OK)
3305 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3306 if (kerr != KCGI_OK)
3308 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3309 if (kerr != KCGI_OK)
3311 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3312 KATTR_ID, "tag_info_date", KATTR__MAX);
3313 if (kerr != KCGI_OK)
3315 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3316 if (kerr != KCGI_OK)
3318 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3319 if (kerr != KCGI_OK)
3322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3323 KATTR_ID, "tag_info", KATTR__MAX);
3324 if (kerr != KCGI_OK)
3326 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3327 if (kerr != KCGI_OK)
3333 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3334 if (kerr != KCGI_OK)
3337 if (limit && --limit == 0)
3341 got_object_tag_close(tag);
3354 href_commits = NULL;
3356 if (tag_count == 0) {
3357 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3358 "summary_tags_title_wrapper", KATTR__MAX);
3359 if (kerr != KCGI_OK)
3361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3362 "summary_tags_title", KATTR__MAX);
3363 if (kerr != KCGI_OK)
3365 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3366 if (kerr != KCGI_OK)
3368 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3369 if (kerr != KCGI_OK)
3371 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3372 "summary_tags_content", KATTR__MAX);
3373 if (kerr != KCGI_OK)
3375 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3376 "tags_info", KATTR__MAX);
3377 if (kerr != KCGI_OK)
3379 kerr = khttp_puts(gw_trans->gw_req,
3380 "There are no tags for this repo.");
3381 if (kerr != KCGI_OK)
3383 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3387 got_object_tag_close(tag);
3394 got_ref_list_free(&refs);
3395 if (error == NULL && kerr != KCGI_OK)
3396 error = gw_kcgi_error(kerr);
3401 gw_free_header(struct gw_header *header)
3404 free(header->author);
3405 free(header->committer);
3406 free(header->refs_str);
3407 free(header->commit_id);
3408 free(header->parent_id);
3409 free(header->tree_id);
3410 free(header->commit_msg);
3413 static struct gw_header *
3416 struct gw_header *header;
3418 header = malloc(sizeof(*header));
3422 header->path = NULL;
3423 TAILQ_INIT(&header->refs);
3425 header->refs_str = NULL;
3426 header->commit_id = NULL;
3427 header->committer = NULL;
3428 header->author = NULL;
3429 header->parent_id = NULL;
3430 header->tree_id = NULL;
3431 header->commit_msg = NULL;
3436 static const struct got_error *
3437 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3438 int limit, struct got_object_id *id)
3440 const struct got_error *error = NULL;
3441 struct got_commit_graph *graph = NULL;
3442 struct got_commit_object *commit = NULL;
3443 int chk_next = 0, chk_multi = 0, prev_set = 0;
3445 error = got_commit_graph_open(&graph, header->path, 0);
3449 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3455 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3458 if (error->code == GOT_ERR_ITER_COMPLETED)
3465 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3468 if (limit == 1 && chk_multi == 0 &&
3469 gw_trans->gw_conf->got_max_commits_display != 1) {
3470 error = gw_get_commit(gw_trans, header, commit, id);
3475 struct gw_header *n_header = NULL;
3476 if ((n_header = gw_init_header()) == NULL) {
3477 error = got_error_from_errno("malloc");
3480 error = got_ref_list(&n_header->refs, gw_trans->repo,
3481 NULL, got_ref_cmp_by_name, NULL);
3485 error = gw_get_commit(gw_trans, n_header, commit, id);
3488 got_ref_list_free(&n_header->refs);
3491 * we have a commit_id now, so copy it to next_prev_id
3492 * for navigation through gw_briefs and gw_commits
3494 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3495 (gw_trans->action == GW_BRIEFS ||
3496 gw_trans->action == GW_COMMITS ||
3497 gw_trans->action == GW_SUMMARY)) {
3499 gw_trans->next_prev_id =
3500 strdup(n_header->commit_id);
3501 if (gw_trans->next_prev_id == NULL) {
3502 error = got_error_from_errno("strdup");
3508 * check for one more commit before breaking,
3509 * so we know whether to navicate through gw_briefs
3510 * gw_commits and gw_summary
3512 if (chk_next && (gw_trans->action == GW_BRIEFS||
3513 gw_trans->action == GW_COMMITS ||
3514 gw_trans->action == GW_SUMMARY)) {
3515 gw_trans->next_id = strdup(n_header->commit_id);
3516 if (gw_trans->next_id == NULL)
3517 error = got_error_from_errno("strdup");
3521 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3524 if (error || (limit && --limit == 0)) {
3532 got_object_commit_close(commit);
3534 got_commit_graph_close(graph);
3538 static const struct got_error *
3539 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3540 struct got_commit_object *commit, struct got_object_id *id)
3542 const struct got_error *error = NULL;
3543 struct got_reflist_entry *re;
3544 struct got_object_id *id2 = NULL;
3545 struct got_object_qid *parent_id;
3546 char *commit_msg = NULL, *commit_msg0;
3549 TAILQ_FOREACH(re, &header->refs, entry) {
3552 struct got_tag_object *tag = NULL;
3553 struct got_object_id *ref_id;
3556 if (got_ref_is_symbolic(re->ref))
3559 name = got_ref_get_name(re->ref);
3560 if (strncmp(name, "refs/", 5) == 0)
3562 if (strncmp(name, "got/", 4) == 0)
3564 if (strncmp(name, "heads/", 6) == 0)
3566 if (strncmp(name, "remotes/", 8) == 0) {
3568 s = strstr(name, "/" GOT_REF_HEAD);
3569 if (s != NULL && s[strlen(s)] == '\0')
3572 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3575 if (strncmp(name, "tags/", 5) == 0) {
3576 error = got_object_open_as_tag(&tag, gw_trans->repo,
3579 if (error->code != GOT_ERR_OBJ_TYPE) {
3584 * Ref points at something other
3591 cmp = got_object_id_cmp(tag ?
3592 got_object_tag_get_object_id(tag) : ref_id, id);
3595 got_object_tag_close(tag);
3598 s = header->refs_str;
3599 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3600 s ? ", " : "", name) == -1) {
3601 error = got_error_from_errno("asprintf");
3603 header->refs_str = NULL;
3609 error = got_object_id_str(&header->commit_id, id);
3613 error = got_object_id_str(&header->tree_id,
3614 got_object_commit_get_tree_id(commit));
3618 if (gw_trans->action == GW_DIFF) {
3619 parent_id = SIMPLEQ_FIRST(
3620 got_object_commit_get_parent_ids(commit));
3621 if (parent_id != NULL) {
3622 id2 = got_object_id_dup(parent_id->id);
3624 error = got_object_id_str(&header->parent_id, id2);
3629 header->parent_id = strdup("/dev/null");
3630 if (header->parent_id == NULL) {
3631 error = got_error_from_errno("strdup");
3637 header->committer_time =
3638 got_object_commit_get_committer_time(commit);
3641 strdup(got_object_commit_get_author(commit));
3642 if (header->author == NULL) {
3643 error = got_error_from_errno("strdup");
3647 strdup(got_object_commit_get_committer(commit));
3648 if (header->committer == NULL) {
3649 error = got_error_from_errno("strdup");
3652 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3656 commit_msg = commit_msg0;
3657 while (*commit_msg == '\n')
3660 header->commit_msg = strdup(commit_msg);
3661 if (header->commit_msg == NULL)
3662 error = got_error_from_errno("strdup");
3667 static const struct got_error *
3668 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3670 const struct got_error *error = NULL;
3671 char *in_repo_path = NULL;
3672 struct got_object_id *id = NULL;
3674 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3678 if (gw_trans->commit_id == NULL) {
3679 struct got_reference *head_ref;
3680 error = got_ref_open(&head_ref, gw_trans->repo,
3681 gw_trans->headref, 0);
3685 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3686 got_ref_close(head_ref);
3690 struct got_reference *ref;
3692 error = got_ref_open(&ref, gw_trans->repo,
3693 gw_trans->commit_id, 0);
3694 if (error == NULL) {
3696 error = got_ref_resolve(&id, gw_trans->repo, ref);
3700 error = got_object_get_type(&obj_type, gw_trans->repo,
3704 if (obj_type == GOT_OBJ_TYPE_TAG) {
3705 struct got_tag_object *tag;
3706 error = got_object_open_as_tag(&tag,
3707 gw_trans->repo, id);
3710 if (got_object_tag_get_object_type(tag) !=
3711 GOT_OBJ_TYPE_COMMIT) {
3712 got_object_tag_close(tag);
3713 error = got_error(GOT_ERR_OBJ_TYPE);
3717 id = got_object_id_dup(
3718 got_object_tag_get_object_id(tag));
3720 error = got_error_from_errno(
3721 "got_object_id_dup");
3722 got_object_tag_close(tag);
3725 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3726 error = got_error(GOT_ERR_OBJ_TYPE);
3730 error = got_repo_match_object_id_prefix(&id,
3731 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3737 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3738 gw_trans->repo_path);
3743 header->path = strdup(in_repo_path);
3744 if (header->path == NULL) {
3745 error = got_error_from_errno("strdup");
3750 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3751 got_ref_cmp_by_name, NULL);
3755 error = gw_get_commits(gw_trans, header, limit, id);
3757 got_ref_list_free(&header->refs);
3767 char datebuf[11]; /* YYYY-MM-DD + NUL */
3770 struct gw_blame_cb_args {
3771 struct blame_line *lines;
3775 off_t *line_offsets;
3777 struct got_repository *repo;
3778 struct gw_trans *gw_trans;
3781 static const struct got_error *
3782 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3784 const struct got_error *err = NULL;
3785 struct gw_blame_cb_args *a = arg;
3786 struct blame_line *bline;
3788 size_t linesize = 0;
3789 struct got_commit_object *commit = NULL;
3792 time_t committer_time;
3793 enum kcgi_err kerr = KCGI_OK;
3795 if (nlines != a->nlines ||
3796 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3797 return got_error(GOT_ERR_RANGE);
3800 return NULL; /* no change in this commit */
3802 /* Annotate this line. */
3803 bline = &a->lines[lineno - 1];
3804 if (bline->annotated)
3806 err = got_object_id_str(&bline->id_str, id);
3810 err = got_object_open_as_commit(&commit, a->repo, id);
3814 bline->committer = strdup(got_object_commit_get_committer(commit));
3815 if (bline->committer == NULL) {
3816 err = got_error_from_errno("strdup");
3820 committer_time = got_object_commit_get_committer_time(commit);
3821 if (localtime_r(&committer_time, &tm) == NULL)
3822 return got_error_from_errno("localtime_r");
3823 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3825 err = got_error(GOT_ERR_NO_SPACE);
3828 bline->annotated = 1;
3830 /* Print lines annotated so far. */
3831 bline = &a->lines[a->lineno_cur - 1];
3832 if (!bline->annotated)
3835 offset = a->line_offsets[a->lineno_cur - 1];
3836 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3837 err = got_error_from_errno("fseeko");
3841 while (bline->annotated) {
3842 char *smallerthan, *at, *nl, *committer;
3843 char *href_diff = NULL;
3846 if (getline(&line, &linesize, a->f) == -1) {
3848 err = got_error_from_errno("getline");
3852 committer = bline->committer;
3853 smallerthan = strchr(committer, '<');
3854 if (smallerthan && smallerthan[1] != '\0')
3855 committer = smallerthan + 1;
3856 at = strchr(committer, '@');
3859 len = strlen(committer);
3861 committer[8] = '\0';
3863 nl = strchr(line, '\n');
3867 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3868 "blame_wrapper", KATTR__MAX);
3869 if (kerr != KCGI_OK)
3871 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3872 "blame_number", KATTR__MAX);
3873 if (kerr != KCGI_OK)
3875 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3876 a->nlines_prec, a->lineno_cur);
3877 if (kerr != KCGI_OK)
3879 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3880 if (kerr != KCGI_OK)
3883 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3884 "blame_hash", KATTR__MAX);
3885 if (kerr != KCGI_OK)
3888 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3889 a->gw_trans->repo_name, "action", "diff", "commit",
3890 bline->id_str, NULL);
3891 if (href_diff == NULL) {
3892 err = got_error_from_errno("khttp_urlpart");
3895 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3896 KATTR_HREF, href_diff, KATTR__MAX);
3897 if (kerr != KCGI_OK)
3899 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3901 if (kerr != KCGI_OK)
3903 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3904 if (kerr != KCGI_OK)
3907 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3908 "blame_date", KATTR__MAX);
3909 if (kerr != KCGI_OK)
3911 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3912 if (kerr != KCGI_OK)
3914 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3915 if (kerr != KCGI_OK)
3918 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3919 "blame_author", KATTR__MAX);
3920 if (kerr != KCGI_OK)
3922 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3923 if (kerr != KCGI_OK)
3925 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3926 if (kerr != KCGI_OK)
3929 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3930 "blame_code", KATTR__MAX);
3931 if (kerr != KCGI_OK)
3933 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3934 if (kerr != KCGI_OK)
3936 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3937 if (kerr != KCGI_OK)
3940 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3941 if (kerr != KCGI_OK)
3945 bline = &a->lines[a->lineno_cur - 1];
3951 got_object_commit_close(commit);
3953 if (err == NULL && kerr != KCGI_OK)
3954 err = gw_kcgi_error(kerr);
3958 static const struct got_error *
3959 gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
3961 const struct got_error *error = NULL;
3962 struct got_object_id *obj_id = NULL;
3963 struct got_object_id *commit_id = NULL;
3964 struct got_blob_object *blob = NULL;
3965 char *path = NULL, *in_repo_path = NULL;
3966 struct gw_blame_cb_args bca;
3970 if (asprintf(&path, "%s%s%s",
3971 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3972 gw_trans->repo_folder ? "/" : "",
3973 gw_trans->repo_file) == -1) {
3974 error = got_error_from_errno("asprintf");
3978 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
3982 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3983 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
3987 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3992 if (obj_id == NULL) {
3993 error = got_error(GOT_ERR_NO_OBJ);
3997 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4001 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4002 error = got_error(GOT_ERR_OBJ_TYPE);
4006 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4010 bca.f = got_opentemp();
4011 if (bca.f == NULL) {
4012 error = got_error_from_errno("got_opentemp");
4015 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4016 &bca.line_offsets, bca.f, blob);
4017 if (error || bca.nlines == 0)
4020 /* Don't include \n at EOF in the blame line count. */
4021 if (bca.line_offsets[bca.nlines - 1] == filesize)
4024 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4025 if (bca.lines == NULL) {
4026 error = got_error_from_errno("calloc");
4030 bca.nlines_prec = 0;
4036 bca.repo = gw_trans->repo;
4037 bca.gw_trans = gw_trans;
4039 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4048 free(bca.line_offsets);
4049 for (i = 0; i < bca.nlines; i++) {
4050 struct blame_line *bline = &bca.lines[i];
4051 free(bline->id_str);
4052 free(bline->committer);
4055 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4056 error = got_error_from_errno("fclose");
4059 got_object_blob_close(blob);
4063 static const struct got_error *
4064 gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
4066 const struct got_error *error = NULL;
4067 struct got_object_id *obj_id = NULL;
4068 struct got_object_id *commit_id = NULL;
4069 struct got_blob_object *blob = NULL;
4070 char *path = NULL, *in_repo_path = NULL;
4071 int obj_type, set_mime = 0;
4074 enum kcgi_err kerr = KCGI_OK;
4076 if (asprintf(&path, "%s%s%s",
4077 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4078 gw_trans->repo_folder ? "/" : "",
4079 gw_trans->repo_file) == -1) {
4080 error = got_error_from_errno("asprintf");
4084 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4088 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4089 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4093 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4098 if (obj_id == NULL) {
4099 error = got_error(GOT_ERR_NO_OBJ);
4103 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4107 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4108 error = got_error(GOT_ERR_OBJ_TYPE);
4112 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4116 hdrlen = got_object_blob_get_hdrlen(blob);
4118 error = got_object_blob_read_block(&len, blob);
4121 buf = got_object_blob_get_read_buf(blob);
4124 * Skip blob object header first time around,
4125 * which also contains a zero byte.
4128 if (set_mime == 0) {
4129 if (isbinary(buf, len - hdrlen))
4130 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4132 gw_trans->mime = KMIME_TEXT_PLAIN;
4134 error = gw_display_index(gw_trans);
4138 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4139 if (kerr != KCGI_OK)
4149 got_object_blob_close(blob);
4150 if (error == NULL && kerr != KCGI_OK)
4151 error = gw_kcgi_error(kerr);
4155 static const struct got_error *
4156 gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
4158 const struct got_error *error = NULL;
4159 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4160 struct got_tree_object *tree = NULL;
4161 char *path = NULL, *in_repo_path = NULL;
4162 char *id_str = NULL;
4163 char *build_folder = NULL;
4164 char *href_blob = NULL, *href_blame = NULL;
4165 const char *class = NULL;
4166 int nentries, i, class_flip = 0;
4167 enum kcgi_err kerr = KCGI_OK;
4169 if (gw_trans->repo_folder != NULL) {
4170 path = strdup(gw_trans->repo_folder);
4172 error = got_error_from_errno("strdup");
4176 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4177 gw_trans->repo_path);
4181 path = in_repo_path;
4184 if (gw_trans->commit_id == NULL) {
4185 struct got_reference *head_ref;
4186 error = got_ref_open(&head_ref, gw_trans->repo,
4187 gw_trans->headref, 0);
4190 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4193 got_ref_close(head_ref);
4195 * gw_trans->commit_id was not parsed from the querystring
4196 * we hit this code path from gw_index, where we don't know the
4197 * commit values for the tree link yet, so set
4198 * gw_trans->commit_id here to continue further into the tree
4200 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4205 error = got_repo_match_object_id(&commit_id, NULL,
4206 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
4212 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4217 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4221 nentries = got_object_tree_get_nentries(tree);
4222 for (i = 0; i < nentries; i++) {
4223 struct got_tree_entry *te;
4224 const char *modestr = "";
4227 te = got_object_tree_get_entry(tree, i);
4229 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4233 mode = got_tree_entry_get_mode(te);
4234 if (got_object_tree_entry_is_submodule(te))
4236 else if (S_ISLNK(mode))
4238 else if (S_ISDIR(mode))
4240 else if (mode & S_IXUSR)
4243 if (class_flip == 0) {
4244 class = "back_lightgray";
4247 class = "back_white";
4251 if (S_ISDIR(mode)) {
4252 if (asprintf(&build_folder, "%s/%s",
4253 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4254 got_tree_entry_get_name(te)) == -1) {
4255 error = got_error_from_errno("asprintf");
4259 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4260 gw_trans->repo_name, "action",
4261 gw_get_action_name(gw_trans), "commit",
4262 gw_trans->commit_id, "folder", build_folder, NULL);
4263 if (href_blob == NULL) {
4264 error = got_error_from_errno("khttp_urlpart");
4267 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4268 KATTR_ID, "tree_wrapper", KATTR__MAX);
4269 if (kerr != KCGI_OK)
4271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4272 KATTR_ID, "tree_line", KATTR_CLASS, class,
4274 if (kerr != KCGI_OK)
4276 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4277 KATTR_HREF, href_blob, KATTR_CLASS,
4278 "diff_directory", KATTR__MAX);
4279 if (kerr != KCGI_OK)
4281 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4282 got_tree_entry_get_name(te), modestr);
4283 if (kerr != KCGI_OK)
4285 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4286 if (kerr != KCGI_OK)
4288 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4289 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4291 if (kerr != KCGI_OK)
4293 kerr = khtml_entity(gw_trans->gw_html_req,
4295 if (kerr != KCGI_OK)
4297 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4298 if (kerr != KCGI_OK)
4301 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4302 gw_trans->repo_name, "action", "blob", "commit",
4303 gw_trans->commit_id, "file",
4304 got_tree_entry_get_name(te), "folder",
4305 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4307 if (href_blob == NULL) {
4308 error = got_error_from_errno("khttp_urlpart");
4311 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4312 gw_trans->repo_name, "action", "blame", "commit",
4313 gw_trans->commit_id, "file",
4314 got_tree_entry_get_name(te), "folder",
4315 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4317 if (href_blame == NULL) {
4318 error = got_error_from_errno("khttp_urlpart");
4321 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4322 KATTR_ID, "tree_wrapper", KATTR__MAX);
4323 if (kerr != KCGI_OK)
4325 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4326 KATTR_ID, "tree_line", KATTR_CLASS, class,
4328 if (kerr != KCGI_OK)
4330 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4331 KATTR_HREF, href_blob, KATTR__MAX);
4332 if (kerr != KCGI_OK)
4334 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4335 got_tree_entry_get_name(te), modestr);
4336 if (kerr != KCGI_OK)
4338 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4339 if (kerr != KCGI_OK)
4341 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4342 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4344 if (kerr != KCGI_OK)
4347 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4348 KATTR_HREF, href_blob, KATTR__MAX);
4349 if (kerr != KCGI_OK)
4351 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4352 if (kerr != KCGI_OK)
4354 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4355 if (kerr != KCGI_OK)
4358 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4359 if (kerr != KCGI_OK)
4362 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4363 KATTR_HREF, href_blame, KATTR__MAX);
4364 if (kerr != KCGI_OK)
4366 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4367 if (kerr != KCGI_OK)
4370 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4371 if (kerr != KCGI_OK)
4379 build_folder = NULL;
4383 got_object_tree_close(tree);
4390 if (error == NULL && kerr != KCGI_OK)
4391 error = gw_kcgi_error(kerr);
4395 static const struct got_error *
4396 gw_output_repo_heads(struct gw_trans *gw_trans)
4398 const struct got_error *error = NULL;
4399 struct got_reflist_head refs;
4400 struct got_reflist_entry *re;
4401 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4402 char *href_commits = NULL;
4403 enum kcgi_err kerr = KCGI_OK;
4407 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4408 got_ref_cmp_by_name, NULL);
4412 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4413 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4414 if (kerr != KCGI_OK)
4416 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4417 KATTR_ID, "summary_heads_title", KATTR__MAX);
4418 if (kerr != KCGI_OK)
4420 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4421 if (kerr != KCGI_OK)
4423 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4424 if (kerr != KCGI_OK)
4426 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4427 KATTR_ID, "summary_heads_content", KATTR__MAX);
4428 if (kerr != KCGI_OK)
4431 TAILQ_FOREACH(re, &refs, entry) {
4432 const char *refname;
4434 if (got_ref_is_symbolic(re->ref))
4437 refname = got_ref_get_name(re->ref);
4438 if (strncmp(refname, "refs/heads/", 11) != 0)
4441 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4446 if (strncmp(refname, "refs/heads/", 11) == 0)
4449 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4450 KATTR_ID, "heads_wrapper", KATTR__MAX);
4451 if (kerr != KCGI_OK)
4453 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4454 KATTR_ID, "heads_age", KATTR__MAX);
4455 if (kerr != KCGI_OK)
4457 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4458 if (kerr != KCGI_OK)
4460 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4461 if (kerr != KCGI_OK)
4463 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4464 KATTR_ID, "heads_space", KATTR__MAX);
4465 if (kerr != KCGI_OK)
4467 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4468 if (kerr != KCGI_OK)
4470 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4471 if (kerr != KCGI_OK)
4473 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4474 KATTR_ID, "head", KATTR__MAX);
4475 if (kerr != KCGI_OK)
4478 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4479 gw_trans->repo_name, "action", "summary", "headref",
4481 if (href_summary == NULL) {
4482 error = got_error_from_errno("khttp_urlpart");
4485 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4486 href_summary, KATTR__MAX);
4487 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4488 if (kerr != KCGI_OK)
4490 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4491 if (kerr != KCGI_OK)
4494 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4495 "navs_wrapper", KATTR__MAX);
4496 if (kerr != KCGI_OK)
4498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4499 "navs", KATTR__MAX);
4500 if (kerr != KCGI_OK)
4503 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4504 href_summary, KATTR__MAX);
4505 if (kerr != KCGI_OK)
4507 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4508 if (kerr != KCGI_OK)
4510 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4511 if (kerr != KCGI_OK)
4514 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4515 if (kerr != KCGI_OK)
4518 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4519 gw_trans->repo_name, "action", "briefs", "headref",
4521 if (href_briefs == NULL) {
4522 error = got_error_from_errno("khttp_urlpart");
4525 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4526 href_briefs, KATTR__MAX);
4527 if (kerr != KCGI_OK)
4529 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4530 if (kerr != KCGI_OK)
4532 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4533 if (kerr != KCGI_OK)
4536 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4537 if (kerr != KCGI_OK)
4540 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4541 gw_trans->repo_name, "action", "commits", "headref",
4543 if (href_commits == NULL) {
4544 error = got_error_from_errno("khttp_urlpart");
4547 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4548 href_commits, KATTR__MAX);
4549 if (kerr != KCGI_OK)
4551 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4552 if (kerr != KCGI_OK)
4554 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4555 if (kerr != KCGI_OK)
4558 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4559 "dotted_line", KATTR__MAX);
4560 if (kerr != KCGI_OK)
4562 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4563 if (kerr != KCGI_OK)
4566 href_summary = NULL;
4570 href_commits = NULL;
4573 got_ref_list_free(&refs);
4580 static const struct got_error *
4581 gw_output_site_link(struct gw_trans *gw_trans)
4583 const struct got_error *error = NULL;
4584 char *href_summary = NULL;
4585 enum kcgi_err kerr = KCGI_OK;
4587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4588 "site_link", KATTR__MAX);
4589 if (kerr != KCGI_OK)
4591 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4593 if (kerr != KCGI_OK)
4595 kerr = khtml_puts(gw_trans->gw_html_req,
4596 gw_trans->gw_conf->got_site_link);
4597 if (kerr != KCGI_OK)
4599 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4600 if (kerr != KCGI_OK)
4603 if (gw_trans->repo_name != NULL) {
4604 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4605 if (kerr != KCGI_OK)
4608 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4609 gw_trans->repo_name, "action", "summary", NULL);
4610 if (href_summary == NULL) {
4611 error = got_error_from_errno("khttp_urlpart");
4614 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4615 href_summary, KATTR__MAX);
4616 if (kerr != KCGI_OK)
4618 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4619 if (kerr != KCGI_OK)
4621 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4622 if (kerr != KCGI_OK)
4624 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4625 gw_get_action_name(gw_trans));
4626 if (kerr != KCGI_OK)
4630 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4631 if (kerr != KCGI_OK)
4635 if (error == NULL && kerr != KCGI_OK)
4636 error = gw_kcgi_error(kerr);
4640 static const struct got_error *
4641 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4643 const struct got_error *error = NULL;
4645 enum kcgi_err kerr = KCGI_OK;
4647 if (strncmp(buf, "-", 1) == 0)
4648 color = "diff_minus";
4649 else if (strncmp(buf, "+", 1) == 0)
4650 color = "diff_plus";
4651 else if (strncmp(buf, "@@", 2) == 0)
4652 color = "diff_chunk_header";
4653 else if (strncmp(buf, "@@", 2) == 0)
4654 color = "diff_chunk_header";
4655 else if (strncmp(buf, "commit +", 8) == 0)
4656 color = "diff_meta";
4657 else if (strncmp(buf, "commit -", 8) == 0)
4658 color = "diff_meta";
4659 else if (strncmp(buf, "blob +", 6) == 0)
4660 color = "diff_meta";
4661 else if (strncmp(buf, "blob -", 6) == 0)
4662 color = "diff_meta";
4663 else if (strncmp(buf, "file +", 6) == 0)
4664 color = "diff_meta";
4665 else if (strncmp(buf, "file -", 6) == 0)
4666 color = "diff_meta";
4667 else if (strncmp(buf, "from:", 5) == 0)
4668 color = "diff_author";
4669 else if (strncmp(buf, "via:", 4) == 0)
4670 color = "diff_author";
4671 else if (strncmp(buf, "date:", 5) == 0)
4672 color = "diff_date";
4673 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4674 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4675 if (error == NULL && kerr != KCGI_OK)
4676 error = gw_kcgi_error(kerr);
4681 main(int argc, char *argv[])
4683 const struct got_error *error = NULL;
4684 struct gw_trans *gw_trans;
4685 struct gw_dir *dir = NULL, *tdir;
4686 const char *page = "index";
4687 enum kcgi_err kerr = KCGI_OK;
4689 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4692 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4695 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4698 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4701 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4702 if (kerr != KCGI_OK) {
4703 error = gw_kcgi_error(kerr);
4707 TAILQ_INIT(&gw_trans->gw_dirs);
4708 TAILQ_INIT(&gw_trans->gw_headers);
4710 gw_trans->action = -1;
4712 gw_trans->repos_total = 0;
4713 gw_trans->repo_path = NULL;
4714 gw_trans->commit_id = NULL;
4715 gw_trans->next_id = NULL;
4716 gw_trans->next_prev_id = NULL;
4717 gw_trans->prev_id = NULL;
4718 gw_trans->prev_prev_id = NULL;
4719 gw_trans->headref = GOT_REF_HEAD;
4720 gw_trans->mime = KMIME_TEXT_HTML;
4721 gw_trans->gw_tmpl->key = gw_templs;
4722 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4723 gw_trans->gw_tmpl->arg = gw_trans;
4724 gw_trans->gw_tmpl->cb = gw_template;
4726 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4730 error = gw_parse_querystring(gw_trans);
4734 if (gw_trans->action == GW_BLOB)
4735 error = gw_blob(gw_trans);
4737 error = gw_display_index(gw_trans);
4739 free(gw_trans->gw_conf->got_repos_path);
4740 free(gw_trans->gw_conf->got_www_path);
4741 free(gw_trans->gw_conf->got_site_name);
4742 free(gw_trans->gw_conf->got_site_owner);
4743 free(gw_trans->gw_conf->got_site_link);
4744 free(gw_trans->gw_conf->got_logo);
4745 free(gw_trans->gw_conf->got_logo_url);
4746 free(gw_trans->gw_conf);
4747 free(gw_trans->commit_id);
4748 free(gw_trans->next_id);
4749 free(gw_trans->next_prev_id);
4750 free(gw_trans->prev_id);
4751 free(gw_trans->prev_prev_id);
4752 free(gw_trans->repo_path);
4754 got_repo_close(gw_trans->repo);
4756 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4758 free(dir->description);
4765 khttp_free(gw_trans->gw_req);