2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
20 #include <sys/types.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
51 #include "gotweb_ui.h"
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 TAILQ_HEAD(headers, gw_header) gw_headers;
59 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
60 struct gw_dir *gw_dir;
61 struct gotweb_conf *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
74 unsigned int repos_total;
79 TAILQ_ENTRY(gw_header) entry;
80 struct got_repository *repo;
81 struct got_reflist_head refs;
82 struct got_commit_object *commit;
83 struct got_object_id *id;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
91 const char *committer;
93 time_t committer_time;
97 TAILQ_ENTRY(gw_dir) entry;
138 static const char *const gw_templs[TEMPL__MAX] = {
148 static const struct kvalid gw_keys[KEY__ZMAX] = {
149 { kvalid_stringne, "action" },
150 { kvalid_stringne, "commit" },
151 { kvalid_stringne, "file" },
152 { kvalid_stringne, "folder" },
153 { kvalid_stringne, "headref" },
154 { kvalid_int, "page" },
155 { kvalid_stringne, "path" },
158 static struct gw_dir *gw_init_gw_dir(char *);
159 static struct gw_header *gw_init_header(void);
161 static const struct got_error *gw_get_repo_description(char **,
162 struct gw_trans *, char *);
163 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
165 static const struct got_error *gw_get_time_str(char **, time_t, int);
166 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
167 char *, char *, int);
168 static const struct got_error *gw_output_file_blame(struct gw_trans *);
169 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
170 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
171 static const struct got_error *gw_output_diff(struct gw_trans *,
173 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
174 struct gw_header *, int, int);
175 static const struct got_error *gw_get_repo_heads(char **, struct gw_trans *);
176 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
178 static char *gw_get_site_link(struct gw_trans *);
179 static const struct got_error *gw_html_escape(char **, const char *);
180 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
182 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
184 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
186 static const struct got_error *gw_gen_author_header(struct gw_trans *,
188 static const struct got_error *gw_gen_age_header(struct gw_trans *,
190 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
192 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
194 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
196 static void gw_free_headers(struct gw_header *);
197 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
199 static const struct got_error* gw_display_index(struct gw_trans *);
200 static void gw_display_error(struct gw_trans *,
201 const struct got_error *);
203 static int gw_template(size_t, void *);
205 static const struct got_error* gw_get_header(struct gw_trans *,
206 struct gw_header *, int);
207 static const struct got_error* gw_get_commits(struct gw_trans *,
208 struct gw_header *, int);
209 static const struct got_error* gw_get_commit(struct gw_trans *,
211 static const struct got_error* gw_apply_unveil(const char *, const char *);
212 static const struct got_error* gw_blame_cb(void *, int, int,
213 struct got_object_id *);
214 static const struct got_error* gw_load_got_paths(struct gw_trans *);
215 static const struct got_error* gw_load_got_path(struct gw_trans *,
217 static const struct got_error* gw_parse_querystring(struct gw_trans *);
219 static const struct got_error* gw_blame(struct gw_trans *);
220 static const struct got_error* gw_blob(struct gw_trans *);
221 static const struct got_error* gw_diff(struct gw_trans *);
222 static const struct got_error* gw_index(struct gw_trans *);
223 static const struct got_error* gw_commits(struct gw_trans *);
224 static const struct got_error* gw_briefs(struct gw_trans *);
225 static const struct got_error* gw_summary(struct gw_trans *);
226 static const struct got_error* gw_tree(struct gw_trans *);
227 static const struct got_error* gw_tag(struct gw_trans *);
229 struct gw_query_action {
230 unsigned int func_id;
231 const char *func_name;
232 const struct got_error *(*func_main)(struct gw_trans *);
236 enum gw_query_actions {
249 static struct gw_query_action gw_query_funcs[] = {
250 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
251 { GW_BLOB, "blob", NULL, NULL },
252 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
253 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
254 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
255 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
256 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
257 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
258 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
259 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
262 static const struct got_error *
263 gw_kcgi_error(enum kcgi_err kerr)
268 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
269 return got_error(GOT_ERR_CANCELLED);
271 if (kerr == KCGI_ENOMEM)
272 return got_error_set_errno(ENOMEM,
273 kcgi_strerror(kerr));
275 if (kerr == KCGI_ENFILE)
276 return got_error_set_errno(ENFILE,
277 kcgi_strerror(kerr));
279 if (kerr == KCGI_EAGAIN)
280 return got_error_set_errno(EAGAIN,
281 kcgi_strerror(kerr));
283 if (kerr == KCGI_FORM)
284 return got_error_msg(GOT_ERR_IO,
285 kcgi_strerror(kerr));
287 return got_error_from_errno(kcgi_strerror(kerr));
290 static const struct got_error *
291 gw_apply_unveil(const char *repo_path, const char *repo_file)
293 const struct got_error *err;
295 if (repo_path && repo_file) {
297 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
298 return got_error_from_errno("asprintf unveil");
299 if (unveil(full_path, "r") != 0)
300 return got_error_from_errno2("unveil", full_path);
303 if (repo_path && unveil(repo_path, "r") != 0)
304 return got_error_from_errno2("unveil", repo_path);
306 if (unveil("/tmp", "rwc") != 0)
307 return got_error_from_errno2("unveil", "/tmp");
309 err = got_privsep_unveil_exec_helpers();
313 if (unveil(NULL, NULL) != 0)
314 return got_error_from_errno("unveil");
319 static const struct got_error *
320 gw_empty_string(char **s)
324 return got_error_from_errno("strdup");
329 isbinary(const uint8_t *buf, size_t n)
333 for (i = 0; i < n; i++)
339 static const struct got_error *
340 gw_blame(struct gw_trans *gw_trans)
342 const struct got_error *error = NULL;
343 struct gw_header *header = NULL;
344 char *age = NULL, *escaped_commit_msg = NULL;
347 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
349 return got_error_from_errno("pledge");
351 if ((header = gw_init_header()) == NULL)
352 return got_error_from_errno("malloc");
354 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
358 error = gw_get_header(gw_trans, header, 1);
363 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
364 "blame_header_wrapper", KATTR__MAX);
367 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
368 "blame_header", KATTR__MAX);
371 error = gw_get_time_str(&age, header->committer_time,
375 error = gw_gen_age_header(gw_trans, age ?age : "");
379 * XXX: keeping this for now, since kcgihtml does not convert
380 * \n into <br /> yet.
382 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
385 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
388 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
392 "dotted_line", KATTR__MAX);
395 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
400 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
401 "blame", KATTR__MAX);
404 error = gw_output_file_blame(gw_trans);
407 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
409 got_ref_list_free(&header->refs);
410 gw_free_headers(header);
411 free(escaped_commit_msg);
412 if (error == NULL && kerr != KCGI_OK)
413 error = gw_kcgi_error(kerr);
417 static const struct got_error *
418 gw_blob(struct gw_trans *gw_trans)
420 const struct got_error *error = NULL;
421 struct gw_header *header = NULL;
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, NULL);
434 error = gw_get_header(gw_trans, header, 1);
438 error = gw_output_blob_buf(gw_trans);
440 got_ref_list_free(&header->refs);
441 gw_free_headers(header);
445 static const struct got_error *
446 gw_diff(struct gw_trans *gw_trans)
448 const struct got_error *error = NULL;
449 struct gw_header *header = NULL;
450 char *age = NULL, *escaped_commit_msg = NULL;
451 enum kcgi_err kerr = KCGI_OK;
453 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
455 return got_error_from_errno("pledge");
457 if ((header = gw_init_header()) == NULL)
458 return got_error_from_errno("malloc");
460 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
464 error = gw_get_header(gw_trans, header, 1);
469 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
470 "diff_header_wrapper", KATTR__MAX);
473 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
474 "diff_header", KATTR__MAX);
477 error = gw_gen_diff_header(gw_trans, header->parent_id,
481 error = gw_gen_commit_header(gw_trans, header->commit_id,
485 error = gw_gen_tree_header(gw_trans, header->tree_id);
488 error = gw_gen_author_header(gw_trans, header->author);
491 error = gw_gen_committer_header(gw_trans, header->author);
494 error = gw_get_time_str(&age, header->committer_time,
498 error = gw_gen_age_header(gw_trans, age ?age : "");
502 * XXX: keeping this for now, since kcgihtml does not convert
503 * \n into <br /> yet.
505 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
508 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
511 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
515 "dotted_line", KATTR__MAX);
518 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
523 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
527 error = gw_output_diff(gw_trans, header);
531 /* diff content close */
532 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
536 got_ref_list_free(&header->refs);
537 gw_free_headers(header);
539 free(escaped_commit_msg);
540 if (error == NULL && kerr != KCGI_OK)
541 error = gw_kcgi_error(kerr);
545 static const struct got_error *
546 gw_index(struct gw_trans *gw_trans)
548 const struct got_error *error = NULL;
549 struct gw_dir *gw_dir = NULL;
550 char *html, *navs, *next, *prev;
551 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
554 if (pledge("stdio rpath proc exec sendfd unveil",
556 error = got_error_from_errno("pledge");
560 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
564 error = gw_load_got_paths(gw_trans);
568 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
570 return gw_kcgi_error(kerr);
572 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
573 if (asprintf(&html, index_projects_empty,
574 gw_trans->gw_conf->got_repos_path) == -1)
575 return got_error_from_errno("asprintf");
576 kerr = khttp_puts(gw_trans->gw_req, html);
578 error = gw_kcgi_error(kerr);
583 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
586 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
587 if (gw_trans->page > 0 && (gw_trans->page *
588 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
597 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
598 gw_dir->name, gw_dir->name) == -1)
599 return got_error_from_errno("asprintf");
601 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
602 gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
605 return got_error_from_errno("asprintf");
607 kerr = khttp_puts(gw_trans->gw_req, html);
611 return gw_kcgi_error(kerr);
613 if (gw_trans->gw_conf->got_max_repos_display == 0)
616 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
617 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
619 return gw_kcgi_error(kerr);
620 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
621 (gw_trans->page > 0) &&
622 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
623 prev_disp == gw_trans->repos_total)) {
624 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
626 return gw_kcgi_error(kerr);
629 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
630 (gw_trans->page > 0) &&
631 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
632 prev_disp == gw_trans->repos_total)) {
633 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
634 return got_error_from_errno("asprintf");
635 kerr = khttp_puts(gw_trans->gw_req, prev);
638 return gw_kcgi_error(kerr);
641 kerr = khttp_puts(gw_trans->gw_req, div_end);
643 return gw_kcgi_error(kerr);
645 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
646 next_disp == gw_trans->gw_conf->got_max_repos_display &&
647 dir_c != (gw_trans->page + 1) *
648 gw_trans->gw_conf->got_max_repos_display) {
649 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
650 return got_error_from_errno("calloc");
651 kerr = khttp_puts(gw_trans->gw_req, next);
654 return gw_kcgi_error(kerr);
655 kerr = khttp_puts(gw_trans->gw_req, div_end);
657 error = gw_kcgi_error(kerr);
662 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
663 (gw_trans->page > 0) &&
664 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
665 prev_disp == gw_trans->repos_total)) {
666 kerr = khttp_puts(gw_trans->gw_req, div_end);
668 return gw_kcgi_error(kerr);
676 static const struct got_error *
677 gw_commits(struct gw_trans *gw_trans)
679 const struct got_error *error = NULL;
680 struct gw_header *header = NULL, *n_header = NULL;
681 char *age = NULL, *escaped_commit_msg = NULL;
682 char *href_diff = NULL, *href_blob = NULL;
683 enum kcgi_err kerr = KCGI_OK;
685 if ((header = gw_init_header()) == NULL)
686 return got_error_from_errno("malloc");
688 if (pledge("stdio rpath proc exec sendfd unveil",
690 error = got_error_from_errno("pledge");
694 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
698 error = gw_get_header(gw_trans, header,
699 gw_trans->gw_conf->got_max_commits_display);
704 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
706 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
707 "commits_line_wrapper", KATTR__MAX);
710 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
714 error = gw_gen_author_header(gw_trans, n_header->author);
717 error = gw_gen_committer_header(gw_trans, n_header->author);
720 error = gw_get_time_str(&age, n_header->committer_time,
724 error = gw_gen_age_header(gw_trans, age ?age : "");
727 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
733 "dotted_line", KATTR__MAX);
736 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
741 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
742 "commit", KATTR__MAX);
746 * XXX: keeping this for now, since kcgihtml does not convert
747 * \n into <br /> yet.
749 error = gw_html_escape(&escaped_commit_msg,
750 n_header->commit_msg);
753 kerr = khttp_puts(gw_trans->gw_req, escaped_commit_msg);
756 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
762 /* XXX: create gen code for this */
764 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
765 gw_trans->repo_name, n_header->commit_id) == -1) {
766 error = got_error_from_errno("asprintf");
769 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
770 KATTR_ID, "navs_wrapper", KATTR__MAX);
773 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
774 KATTR_ID, "navs", KATTR__MAX);
777 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
778 KATTR_HREF, href_diff, KATTR__MAX);
781 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
784 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
788 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
792 /* XXX: create gen code for this */
794 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
795 gw_trans->repo_name, n_header->commit_id) == -1) {
796 error = got_error_from_errno("asprintf");
799 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
800 KATTR_HREF, href_blob, KATTR__MAX);
803 khtml_puts(gw_trans->gw_html_req, "tree");
804 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
807 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
812 "solid_line", KATTR__MAX);
815 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
821 free(escaped_commit_msg);
822 escaped_commit_msg = NULL;
825 got_ref_list_free(&header->refs);
826 gw_free_headers(header);
827 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
828 gw_free_headers(n_header);
832 free(escaped_commit_msg);
833 if (error == NULL && kerr != KCGI_OK)
834 error = gw_kcgi_error(kerr);
838 static const struct got_error *
839 gw_briefs(struct gw_trans *gw_trans)
841 const struct got_error *error = NULL;
842 struct gw_header *header = NULL, *n_header = NULL;
843 char *age = NULL, *age_html = NULL;
844 char *href_diff = NULL, *href_blob = NULL;
845 char *newline, *smallerthan;
846 enum kcgi_err kerr = KCGI_OK;
848 if ((header = gw_init_header()) == NULL)
849 return got_error_from_errno("malloc");
851 if (pledge("stdio rpath proc exec sendfd unveil",
853 error = got_error_from_errno("pledge");
857 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
861 if (gw_trans->action == GW_SUMMARY)
862 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
864 error = gw_get_header(gw_trans, header,
865 gw_trans->gw_conf->got_max_commits_display);
869 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
870 error = gw_get_time_str(&age, n_header->committer_time,
876 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
877 KATTR_ID, "briefs_wrapper", KATTR__MAX);
882 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
883 KATTR_ID, "briefs_age", KATTR__MAX);
886 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
887 error = got_error_from_errno("asprintf");
890 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
893 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
898 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
899 KATTR_ID, "briefs_author", KATTR__MAX);
902 smallerthan = strchr(n_header->author, '<');
905 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
908 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
913 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
914 gw_trans->repo_name, n_header->commit_id) == -1) {
915 error = got_error_from_errno("asprintf");
918 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
919 KATTR_ID, "briefs_log", KATTR__MAX);
922 newline = strchr(n_header->commit_msg, '\n');
925 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
926 KATTR_HREF, href_diff, KATTR__MAX);
929 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
932 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
938 KATTR_ID, "navs_wrapper", KATTR__MAX);
941 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
942 KATTR_ID, "navs", KATTR__MAX);
945 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
946 KATTR_HREF, href_diff, KATTR__MAX);
949 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
952 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
956 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
961 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
962 gw_trans->repo_name, n_header->commit_id) == -1) {
963 error = got_error_from_errno("asprintf");
966 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
967 KATTR_HREF, href_blob, KATTR__MAX);
970 khtml_puts(gw_trans->gw_html_req, "tree");
971 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
974 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
980 KATTR_ID, "dotted_line", KATTR__MAX);
983 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
997 got_ref_list_free(&header->refs);
998 gw_free_headers(header);
999 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1000 gw_free_headers(n_header);
1005 if (error == NULL && kerr != KCGI_OK)
1006 error = gw_kcgi_error(kerr);
1010 static const struct got_error *
1011 gw_summary(struct gw_trans *gw_trans)
1013 const struct got_error *error = NULL;
1014 char *age = NULL, *tags = NULL, *heads = NULL;
1015 enum kcgi_err kerr = KCGI_OK;
1017 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1018 return got_error_from_errno("pledge");
1020 /* unveil is applied with gw_briefs below */
1022 /* summary wrapper */
1023 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1024 "summary_wrapper", KATTR__MAX);
1025 if (kerr != KCGI_OK)
1026 return gw_kcgi_error(kerr);
1029 if (gw_trans->gw_conf->got_show_repo_description &&
1030 gw_trans->gw_dir->description != NULL &&
1031 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1032 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1033 KATTR_ID, "description_title", KATTR__MAX);
1034 if (kerr != KCGI_OK)
1036 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1037 if (kerr != KCGI_OK)
1039 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1040 if (kerr != KCGI_OK)
1042 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1043 KATTR_ID, "description", KATTR__MAX);
1044 if (kerr != KCGI_OK)
1046 kerr = khtml_puts(gw_trans->gw_html_req,
1047 gw_trans->gw_dir->description);
1048 if (kerr != KCGI_OK)
1050 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1051 if (kerr != KCGI_OK)
1056 if (gw_trans->gw_conf->got_show_repo_owner &&
1057 gw_trans->gw_dir->owner != NULL) {
1058 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1059 KATTR_ID, "repo_owner_title", KATTR__MAX);
1060 if (kerr != KCGI_OK)
1062 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1063 if (kerr != KCGI_OK)
1065 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1066 if (kerr != KCGI_OK)
1068 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1069 KATTR_ID, "repo_owner", KATTR__MAX);
1070 if (kerr != KCGI_OK)
1072 kerr = khtml_puts(gw_trans->gw_html_req,
1073 gw_trans->gw_dir->owner);
1074 if (kerr != KCGI_OK)
1076 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1077 if (kerr != KCGI_OK)
1082 if (gw_trans->gw_conf->got_show_repo_age) {
1083 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1084 "refs/heads", TM_LONG);
1088 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1089 KATTR_ID, "last_change_title", KATTR__MAX);
1090 if (kerr != KCGI_OK)
1092 kerr = khtml_puts(gw_trans->gw_html_req,
1094 if (kerr != KCGI_OK)
1096 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1097 if (kerr != KCGI_OK)
1099 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1100 KATTR_ID, "last_change", KATTR__MAX);
1101 if (kerr != KCGI_OK)
1103 kerr = khtml_puts(gw_trans->gw_html_req, age);
1104 if (kerr != KCGI_OK)
1106 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1107 if (kerr != KCGI_OK)
1113 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1114 gw_trans->gw_dir->url != NULL &&
1115 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1116 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1117 KATTR_ID, "cloneurl_title", KATTR__MAX);
1118 if (kerr != KCGI_OK)
1120 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1121 if (kerr != KCGI_OK)
1123 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1124 if (kerr != KCGI_OK)
1126 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1127 KATTR_ID, "cloneurl", KATTR__MAX);
1128 if (kerr != KCGI_OK)
1130 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1131 if (kerr != KCGI_OK)
1133 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1134 if (kerr != KCGI_OK)
1138 /* close summary wrapper */
1139 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1140 if (kerr != KCGI_OK)
1143 /* commit briefs header */
1144 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1145 "briefs_title_wrapper", KATTR__MAX);
1146 if (kerr != KCGI_OK)
1148 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1149 "briefs_title", KATTR__MAX);
1150 if (kerr != KCGI_OK)
1152 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1153 if (kerr != KCGI_OK)
1155 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1156 if (kerr != KCGI_OK)
1158 error = gw_briefs(gw_trans);
1163 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1169 error = gw_get_repo_heads(&heads, gw_trans);
1172 if (heads != NULL && strcmp(heads, "") != 0) {
1173 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1174 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
1175 if (kerr != KCGI_OK)
1177 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1178 KATTR_ID, "summary_heads_title", KATTR__MAX);
1179 if (kerr != KCGI_OK)
1181 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
1182 if (kerr != KCGI_OK)
1184 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1185 if (kerr != KCGI_OK)
1187 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1188 KATTR_ID, "summary_heads_content", KATTR__MAX);
1189 if (kerr != KCGI_OK)
1191 kerr = khttp_puts(gw_trans->gw_req, heads);
1192 if (kerr != KCGI_OK)
1194 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1195 if (kerr != KCGI_OK)
1202 if (error == NULL && kerr != KCGI_OK)
1203 error = gw_kcgi_error(kerr);
1207 static const struct got_error *
1208 gw_tree(struct gw_trans *gw_trans)
1210 const struct got_error *error = NULL;
1211 struct gw_header *header = NULL;
1212 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1213 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
1216 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1217 return got_error_from_errno("pledge");
1219 if ((header = gw_init_header()) == NULL)
1220 return got_error_from_errno("malloc");
1222 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1226 error = gw_get_header(gw_trans, header, 1);
1231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1232 "tree_header_wrapper", KATTR__MAX);
1233 if (kerr != KCGI_OK)
1235 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1236 "tree_header", KATTR__MAX);
1237 if (kerr != KCGI_OK)
1239 error = gw_gen_tree_header(gw_trans, header->tree_id);
1242 error = gw_get_time_str(&age, header->committer_time,
1246 error = gw_gen_age_header(gw_trans, age ?age : "");
1250 * XXX: keeping this for now, since kcgihtml does not convert
1251 * \n into <br /> yet.
1253 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1256 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1259 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1260 if (kerr != KCGI_OK)
1262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1263 "dotted_line", KATTR__MAX);
1264 if (kerr != KCGI_OK)
1266 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1267 if (kerr != KCGI_OK)
1271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1272 "tree", KATTR__MAX);
1273 if (kerr != KCGI_OK)
1275 error = gw_output_repo_tree(gw_trans);
1279 /* tree content close */
1280 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1282 got_ref_list_free(&header->refs);
1283 gw_free_headers(header);
1284 free(tree_html_disp);
1289 free(escaped_commit_msg);
1290 if (error == NULL && kerr != KCGI_OK)
1291 error = gw_kcgi_error(kerr);
1295 static const struct got_error *
1296 gw_tag(struct gw_trans *gw_trans)
1298 const struct got_error *error = NULL;
1299 struct gw_header *header = NULL;
1300 char *escaped_commit_msg = NULL;
1303 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1304 return got_error_from_errno("pledge");
1306 if ((header = gw_init_header()) == NULL)
1307 return got_error_from_errno("malloc");
1309 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1313 error = gw_get_header(gw_trans, header, 1);
1318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1319 "tag_header_wrapper", KATTR__MAX);
1320 if (kerr != KCGI_OK)
1322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1323 "tag_header", KATTR__MAX);
1324 if (kerr != KCGI_OK)
1326 error = gw_gen_commit_header(gw_trans, header->commit_id,
1331 * XXX: keeping this for now, since kcgihtml does not convert
1332 * \n into <br /> yet.
1334 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1337 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1340 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1341 if (kerr != KCGI_OK)
1343 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1344 "dotted_line", KATTR__MAX);
1345 if (kerr != KCGI_OK)
1347 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1348 if (kerr != KCGI_OK)
1352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1353 "tree", KATTR__MAX);
1354 if (kerr != KCGI_OK)
1357 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1361 /* tag content close */
1362 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1364 got_ref_list_free(&header->refs);
1365 gw_free_headers(header);
1366 free(escaped_commit_msg);
1367 if (error == NULL && kerr != KCGI_OK)
1368 error = gw_kcgi_error(kerr);
1372 static const struct got_error *
1373 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1375 const struct got_error *error = NULL;
1380 if (asprintf(&dir_test, "%s/%s/%s",
1381 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1382 GOTWEB_GIT_DIR) == -1)
1383 return got_error_from_errno("asprintf");
1385 dt = opendir(dir_test);
1389 gw_dir->path = strdup(dir_test);
1394 if (asprintf(&dir_test, "%s/%s/%s",
1395 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1396 GOTWEB_GOT_DIR) == -1)
1397 return got_error_from_errno("asprintf");
1399 dt = opendir(dir_test);
1404 error = got_error(GOT_ERR_NOT_GIT_REPO);
1408 if (asprintf(&dir_test, "%s/%s",
1409 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1410 return got_error_from_errno("asprintf");
1412 gw_dir->path = strdup(dir_test);
1414 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1418 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1421 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1422 "refs/heads", TM_DIFF);
1425 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1433 static const struct got_error *
1434 gw_load_got_paths(struct gw_trans *gw_trans)
1436 const struct got_error *error = NULL;
1438 struct dirent **sd_dent;
1439 struct gw_dir *gw_dir;
1441 unsigned int d_cnt, d_i;
1443 d = opendir(gw_trans->gw_conf->got_repos_path);
1445 error = got_error_from_errno2("opendir",
1446 gw_trans->gw_conf->got_repos_path);
1450 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1453 error = got_error_from_errno2("scandir",
1454 gw_trans->gw_conf->got_repos_path);
1458 for (d_i = 0; d_i < d_cnt; d_i++) {
1459 if (gw_trans->gw_conf->got_max_repos > 0 &&
1460 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1461 break; /* account for parent and self */
1463 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1464 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1467 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1468 return got_error_from_errno("gw_dir malloc");
1470 error = gw_load_got_path(gw_trans, gw_dir);
1471 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1478 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1479 !got_path_dir_is_empty(gw_dir->path)) {
1480 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1482 gw_trans->repos_total++;
1490 static const struct got_error *
1491 gw_parse_querystring(struct gw_trans *gw_trans)
1493 const struct got_error *error = NULL;
1495 struct gw_query_action *action = NULL;
1498 if (gw_trans->gw_req->fieldnmap[0]) {
1499 error = got_error_from_errno("bad parse");
1501 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1502 /* define gw_trans->repo_path */
1503 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1504 return got_error_from_errno("asprintf");
1506 if (asprintf(&gw_trans->repo_path, "%s/%s",
1507 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1508 return got_error_from_errno("asprintf");
1510 /* get action and set function */
1511 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1512 for (i = 0; i < nitems(gw_query_funcs); i++) {
1513 action = &gw_query_funcs[i];
1514 if (action->func_name == NULL)
1517 if (strcmp(action->func_name,
1518 p->parsed.s) == 0) {
1519 gw_trans->action = i;
1520 if (asprintf(&gw_trans->action_name,
1521 "%s", action->func_name) == -1)
1523 got_error_from_errno(
1532 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1533 if (asprintf(&gw_trans->commit, "%s",
1535 return got_error_from_errno("asprintf");
1537 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1538 if (asprintf(&gw_trans->repo_file, "%s",
1540 return got_error_from_errno("asprintf");
1542 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1543 if (asprintf(&gw_trans->repo_folder, "%s",
1545 return got_error_from_errno("asprintf");
1547 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1548 if (asprintf(&gw_trans->headref, "%s",
1550 return got_error_from_errno("asprintf");
1552 if (action == NULL) {
1553 error = got_error_from_errno("invalid action");
1556 if ((gw_trans->gw_dir =
1557 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1558 return got_error_from_errno("gw_dir malloc");
1560 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1564 gw_trans->action = GW_INDEX;
1566 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1567 gw_trans->page = p->parsed.i;
1572 static struct gw_dir *
1573 gw_init_gw_dir(char *dir)
1575 struct gw_dir *gw_dir;
1577 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1580 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1586 static const struct got_error *
1587 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1591 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1592 if (kerr != KCGI_OK)
1593 return gw_kcgi_error(kerr);
1594 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1596 if (kerr != KCGI_OK)
1597 return gw_kcgi_error(kerr);
1598 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1600 if (kerr != KCGI_OK)
1601 return gw_kcgi_error(kerr);
1602 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1604 if (kerr != KCGI_OK)
1605 return gw_kcgi_error(kerr);
1606 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1607 if (kerr != KCGI_OK)
1608 return gw_kcgi_error(kerr);
1609 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1611 if (kerr != KCGI_OK)
1612 return gw_kcgi_error(kerr);
1614 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1615 kerr = khttp_head(gw_trans->gw_req,
1616 kresps[KRESP_CONTENT_DISPOSITION],
1617 "attachment; filename=%s", gw_trans->repo_file);
1618 if (kerr != KCGI_OK)
1619 return gw_kcgi_error(kerr);
1622 kerr = khttp_body(gw_trans->gw_req);
1623 return gw_kcgi_error(kerr);
1626 static const struct got_error *
1627 gw_display_index(struct gw_trans *gw_trans)
1629 const struct got_error *error;
1632 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1636 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1637 if (kerr != KCGI_OK)
1638 return gw_kcgi_error(kerr);
1640 if (gw_trans->action != GW_BLOB) {
1641 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1642 gw_query_funcs[gw_trans->action].template);
1643 if (kerr != KCGI_OK) {
1644 khtml_close(gw_trans->gw_html_req);
1645 return gw_kcgi_error(kerr);
1649 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1653 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1655 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1658 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1660 khtml_puts(gw_trans->gw_html_req, err->msg);
1661 khtml_close(gw_trans->gw_html_req);
1665 gw_template(size_t key, void *arg)
1667 const struct got_error *error = NULL;
1669 struct gw_trans *gw_trans = arg;
1670 char *gw_site_link, *img_src = NULL;
1674 kerr = khttp_puts(gw_trans->gw_req, head);
1675 if (kerr != KCGI_OK)
1679 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1680 KATTR_ID, "got_link", KATTR__MAX);
1681 if (kerr != KCGI_OK)
1683 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1684 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1685 KATTR_TARGET, "_sotd", KATTR__MAX);
1686 if (kerr != KCGI_OK)
1688 if (asprintf(&img_src, "/%s",
1689 gw_trans->gw_conf->got_logo) == -1)
1691 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1692 KATTR_SRC, img_src, KATTR__MAX);
1693 if (kerr != KCGI_OK) {
1697 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1698 if (kerr != KCGI_OK) {
1703 case (TEMPL_SITEPATH):
1704 gw_site_link = gw_get_site_link(gw_trans);
1705 if (gw_site_link != NULL) {
1706 kerr = khttp_puts(gw_trans->gw_req, gw_site_link);
1707 if (kerr != KCGI_OK) {
1715 if (gw_trans->gw_conf->got_site_name != NULL) {
1716 kerr = khtml_puts(gw_trans->gw_html_req,
1717 gw_trans->gw_conf->got_site_name);
1718 if (kerr != KCGI_OK)
1722 case (TEMPL_SEARCH):
1723 kerr = khttp_puts(gw_trans->gw_req, search);
1724 if (kerr != KCGI_OK)
1727 case(TEMPL_SITEOWNER):
1728 if (gw_trans->gw_conf->got_site_owner != NULL &&
1729 gw_trans->gw_conf->got_show_site_owner) {
1730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1731 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1732 if (kerr != KCGI_OK)
1734 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1735 KATTR_ID, "site_owner", KATTR__MAX);
1736 if (kerr != KCGI_OK)
1738 kerr = khtml_puts(gw_trans->gw_html_req,
1739 gw_trans->gw_conf->got_site_owner);
1740 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1741 if (kerr != KCGI_OK)
1745 case(TEMPL_CONTENT):
1746 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1748 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1749 if (kerr != KCGI_OK)
1759 static const struct got_error *
1760 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1762 const struct got_error *error = NULL;
1763 char *ref_str = NULL;
1764 enum kcgi_err kerr = KCGI_OK;
1766 if (strcmp(str2, "") != 0) {
1767 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1768 error = got_error_from_errno("asprintf");
1772 ref_str = strdup("");
1774 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1775 KATTR_ID, "header_commit_title", KATTR__MAX);
1776 if (kerr != KCGI_OK)
1778 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
1779 if (kerr != KCGI_OK)
1781 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1782 if (kerr != KCGI_OK)
1784 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1785 KATTR_ID, "header_commit", KATTR__MAX);
1786 if (kerr != KCGI_OK)
1788 kerr = khtml_puts(gw_trans->gw_html_req, str1);
1789 if (kerr != KCGI_OK)
1791 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1792 if (kerr != KCGI_OK)
1794 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
1795 if (kerr != KCGI_OK)
1797 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1799 if (error == NULL && kerr != KCGI_OK)
1800 error = gw_kcgi_error(kerr);
1804 static const struct got_error *
1805 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
1807 const struct got_error *error = NULL;
1808 enum kcgi_err kerr = KCGI_OK;
1810 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1811 KATTR_ID, "header_diff_title", KATTR__MAX);
1812 if (kerr != KCGI_OK)
1814 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
1815 if (kerr != KCGI_OK)
1817 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1818 if (kerr != KCGI_OK)
1820 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1821 KATTR_ID, "header_diff", KATTR__MAX);
1822 if (kerr != KCGI_OK)
1824 kerr = khtml_puts(gw_trans->gw_html_req, str1);
1825 if (kerr != KCGI_OK)
1827 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
1828 if (kerr != KCGI_OK)
1830 kerr = khtml_puts(gw_trans->gw_html_req, str2);
1831 if (kerr != KCGI_OK)
1833 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1835 if (error == NULL && kerr != KCGI_OK)
1836 error = gw_kcgi_error(kerr);
1840 static const struct got_error *
1841 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
1843 const struct got_error *error = NULL;
1844 enum kcgi_err kerr = KCGI_OK;
1846 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1847 KATTR_ID, "header_age_title", KATTR__MAX);
1848 if (kerr != KCGI_OK)
1850 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
1851 if (kerr != KCGI_OK)
1853 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1854 if (kerr != KCGI_OK)
1856 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1857 KATTR_ID, "header_age", KATTR__MAX);
1858 if (kerr != KCGI_OK)
1860 kerr = khtml_puts(gw_trans->gw_html_req, str);
1861 if (kerr != KCGI_OK)
1863 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1865 if (error == NULL && kerr != KCGI_OK)
1866 error = gw_kcgi_error(kerr);
1870 static const struct got_error *
1871 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
1873 const struct got_error *error = NULL;
1876 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1877 KATTR_ID, "header_author_title", KATTR__MAX);
1878 if (kerr != KCGI_OK)
1880 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
1881 if (kerr != KCGI_OK)
1883 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1884 if (kerr != KCGI_OK)
1886 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1887 KATTR_ID, "header_author", KATTR__MAX);
1888 if (kerr != KCGI_OK)
1890 kerr = khtml_puts(gw_trans->gw_html_req, str);
1891 if (kerr != KCGI_OK)
1893 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1895 if (error == NULL && kerr != KCGI_OK)
1896 error = gw_kcgi_error(kerr);
1900 static const struct got_error *
1901 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
1903 const struct got_error *error = NULL;
1906 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1907 KATTR_ID, "header_committer_title", KATTR__MAX);
1908 if (kerr != KCGI_OK)
1910 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
1911 if (kerr != KCGI_OK)
1913 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1914 if (kerr != KCGI_OK)
1916 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1917 KATTR_ID, "header_committer", KATTR__MAX);
1918 if (kerr != KCGI_OK)
1920 kerr = khtml_puts(gw_trans->gw_html_req, str);
1921 if (kerr != KCGI_OK)
1923 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1925 if (error == NULL && kerr != KCGI_OK)
1926 error = gw_kcgi_error(kerr);
1930 static const struct got_error *
1931 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
1933 const struct got_error *error = NULL;
1936 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1937 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
1938 if (kerr != KCGI_OK)
1940 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
1941 if (kerr != KCGI_OK)
1943 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1944 if (kerr != KCGI_OK)
1946 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1947 KATTR_ID, "header_commit_msg", KATTR__MAX);
1948 if (kerr != KCGI_OK)
1950 kerr = khttp_puts(gw_trans->gw_req, str);
1951 if (kerr != KCGI_OK)
1953 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1955 if (error == NULL && kerr != KCGI_OK)
1956 error = gw_kcgi_error(kerr);
1960 static const struct got_error *
1961 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
1963 const struct got_error *error = NULL;
1966 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1967 KATTR_ID, "header_tree_title", KATTR__MAX);
1968 if (kerr != KCGI_OK)
1970 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
1971 if (kerr != KCGI_OK)
1973 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1974 if (kerr != KCGI_OK)
1976 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1977 KATTR_ID, "header_tree", KATTR__MAX);
1978 if (kerr != KCGI_OK)
1980 kerr = khtml_puts(gw_trans->gw_html_req, str);
1981 if (kerr != KCGI_OK)
1983 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1985 if (error == NULL && kerr != KCGI_OK)
1986 error = gw_kcgi_error(kerr);
1990 static const struct got_error *
1991 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
1994 const struct got_error *error = NULL;
1996 char *d_file = NULL;
2000 *description = NULL;
2001 if (gw_trans->gw_conf->got_show_repo_description == 0)
2002 return gw_empty_string(description);
2004 if (asprintf(&d_file, "%s/description", dir) == -1)
2005 return got_error_from_errno("asprintf");
2007 f = fopen(d_file, "r");
2009 if (errno == ENOENT || errno == EACCES)
2010 return gw_empty_string(description);
2011 error = got_error_from_errno2("fopen", d_file);
2015 if (fseek(f, 0, SEEK_END) == -1) {
2016 error = got_ferror(f, GOT_ERR_IO);
2021 error = got_ferror(f, GOT_ERR_IO);
2024 if (fseek(f, 0, SEEK_SET) == -1) {
2025 error = got_ferror(f, GOT_ERR_IO);
2028 *description = calloc(len + 1, sizeof(**description));
2029 if (*description == NULL) {
2030 error = got_error_from_errno("calloc");
2034 n = fread(*description, 1, len, f);
2035 if (n == 0 && ferror(f))
2036 error = got_ferror(f, GOT_ERR_IO);
2038 if (f != NULL && fclose(f) == -1 && error == NULL)
2039 error = got_error_from_errno("fclose");
2044 static const struct got_error *
2045 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2049 char *years = "years ago", *months = "months ago";
2050 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2051 char *minutes = "minutes ago", *seconds = "seconds ago";
2052 char *now = "right now";
2060 diff_time = time(NULL) - committer_time;
2061 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2062 if (asprintf(repo_age, "%lld %s",
2063 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2064 return got_error_from_errno("asprintf");
2065 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2066 if (asprintf(repo_age, "%lld %s",
2067 (diff_time / 60 / 60 / 24 / (365 / 12)),
2069 return got_error_from_errno("asprintf");
2070 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2071 if (asprintf(repo_age, "%lld %s",
2072 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2073 return got_error_from_errno("asprintf");
2074 } else if (diff_time > 60 * 60 * 24 * 2) {
2075 if (asprintf(repo_age, "%lld %s",
2076 (diff_time / 60 / 60 / 24), days) == -1)
2077 return got_error_from_errno("asprintf");
2078 } else if (diff_time > 60 * 60 * 2) {
2079 if (asprintf(repo_age, "%lld %s",
2080 (diff_time / 60 / 60), hours) == -1)
2081 return got_error_from_errno("asprintf");
2082 } else if (diff_time > 60 * 2) {
2083 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2085 return got_error_from_errno("asprintf");
2086 } else if (diff_time > 2) {
2087 if (asprintf(repo_age, "%lld %s", diff_time,
2089 return got_error_from_errno("asprintf");
2091 if (asprintf(repo_age, "%s", now) == -1)
2092 return got_error_from_errno("asprintf");
2096 if (gmtime_r(&committer_time, &tm) == NULL)
2097 return got_error_from_errno("gmtime_r");
2099 s = asctime_r(&tm, datebuf);
2101 return got_error_from_errno("asctime_r");
2103 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2104 return got_error_from_errno("asprintf");
2110 static const struct got_error *
2111 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2112 char *repo_ref, int ref_tm)
2114 const struct got_error *error = NULL;
2115 struct got_object_id *id = NULL;
2116 struct got_repository *repo = NULL;
2117 struct got_commit_object *commit = NULL;
2118 struct got_reflist_head refs;
2119 struct got_reflist_entry *re;
2120 struct got_reference *head_ref;
2122 time_t committer_time = 0, cmp_time = 0;
2123 const char *refname;
2126 SIMPLEQ_INIT(&refs);
2128 if (repo_ref == NULL)
2131 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2134 if (gw_trans->gw_conf->got_show_repo_age == 0)
2137 error = got_repo_open(&repo, dir, NULL);
2142 error = got_ref_list(&refs, repo, "refs/heads",
2143 got_ref_cmp_by_name, NULL);
2145 error = got_ref_list(&refs, repo, repo_ref,
2146 got_ref_cmp_by_name, NULL);
2150 SIMPLEQ_FOREACH(re, &refs, entry) {
2152 refname = strdup(repo_ref);
2154 refname = got_ref_get_name(re->ref);
2155 error = got_ref_open(&head_ref, repo, refname, 0);
2159 error = got_ref_resolve(&id, repo, head_ref);
2160 got_ref_close(head_ref);
2164 error = got_object_open_as_commit(&commit, repo, id);
2169 got_object_commit_get_committer_time(commit);
2171 if (cmp_time < committer_time)
2172 cmp_time = committer_time;
2175 if (cmp_time != 0) {
2176 committer_time = cmp_time;
2177 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2180 got_ref_list_free(&refs);
2185 static const struct got_error *
2186 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2188 const struct got_error *error;
2190 struct got_object_id *id1 = NULL, *id2 = NULL;
2191 char *label1 = NULL, *label2 = NULL, *line = NULL;
2193 size_t linesize = 0;
2195 enum kcgi_err kerr = KCGI_OK;
2201 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2205 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2206 error = got_repo_match_object_id(&id1, &label1,
2207 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2212 error = got_repo_match_object_id(&id2, &label2,
2213 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2217 error = got_object_get_type(&obj_type, header->repo, id2);
2221 case GOT_OBJ_TYPE_BLOB:
2222 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2225 case GOT_OBJ_TYPE_TREE:
2226 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2229 case GOT_OBJ_TYPE_COMMIT:
2230 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2234 error = got_error(GOT_ERR_OBJ_TYPE);
2239 if (fseek(f, 0, SEEK_SET) == -1) {
2240 error = got_ferror(f, GOT_ERR_IO);
2244 while ((linelen = getline(&line, &linesize, f)) != -1) {
2245 error = gw_colordiff_line(gw_trans, line);
2248 /* XXX: KHTML_PRETTY breaks this */
2249 kerr = khtml_puts(gw_trans->gw_html_req, line);
2250 if (kerr != KCGI_OK)
2252 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2253 if (kerr != KCGI_OK)
2256 if (linelen == -1 && ferror(f))
2257 error = got_error_from_errno("getline");
2259 if (f && fclose(f) == -1 && error == NULL)
2260 error = got_error_from_errno("fclose");
2267 if (error == NULL && kerr != KCGI_OK)
2268 error = gw_kcgi_error(kerr);
2272 static const struct got_error *
2273 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2275 const struct got_error *error = NULL;
2276 struct got_repository *repo;
2277 const char *gitconfig_owner;
2281 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2284 error = got_repo_open(&repo, dir, NULL);
2287 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2288 if (gitconfig_owner) {
2289 *owner = strdup(gitconfig_owner);
2291 error = got_error_from_errno("strdup");
2293 got_repo_close(repo);
2297 static const struct got_error *
2298 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2300 const struct got_error *error = NULL;
2302 char *d_file = NULL;
2308 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2309 return got_error_from_errno("asprintf");
2311 f = fopen(d_file, "r");
2313 if (errno != ENOENT && errno != EACCES)
2314 error = got_error_from_errno2("fopen", d_file);
2318 if (fseek(f, 0, SEEK_END) == -1) {
2319 error = got_ferror(f, GOT_ERR_IO);
2324 error = got_ferror(f, GOT_ERR_IO);
2327 if (fseek(f, 0, SEEK_SET) == -1) {
2328 error = got_ferror(f, GOT_ERR_IO);
2332 *url = calloc(len + 1, sizeof(**url));
2334 error = got_error_from_errno("calloc");
2338 n = fread(*url, 1, len, f);
2339 if (n == 0 && ferror(f))
2340 error = got_ferror(f, GOT_ERR_IO);
2342 if (f && fclose(f) == -1 && error == NULL)
2343 error = got_error_from_errno("fclose");
2348 static const struct got_error *
2349 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2350 int limit, int tag_type)
2352 const struct got_error *error = NULL;
2353 struct got_repository *repo = NULL;
2354 struct got_reflist_head refs;
2355 struct got_reflist_entry *re;
2357 char *escaped_tag_commit = NULL;
2358 char *id_str = NULL, *refstr = NULL, *newline, *href_commits = NULL;
2359 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2360 struct got_tag_object *tag = NULL;
2361 enum kcgi_err kerr = KCGI_OK;
2362 int summary_header_displayed = 0;
2364 SIMPLEQ_INIT(&refs);
2366 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2370 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2374 SIMPLEQ_FOREACH(re, &refs, entry) {
2375 const char *refname;
2377 const char *tag_commit;
2379 struct got_object_id *id;
2381 refname = got_ref_get_name(re->ref);
2382 if (strncmp(refname, "refs/tags/", 10) != 0)
2385 refstr = got_ref_to_str(re->ref);
2386 if (refstr == NULL) {
2387 error = got_error_from_errno("got_ref_to_str");
2391 error = got_ref_resolve(&id, repo, re->ref);
2396 * XXX: some of my repos are failing here. need to investigate.
2397 * currently setting error to NULL so no error is returned,
2398 * which stops Heads from being displayed on gw_summary.
2400 * got ref -l lists refs and first tag ref above can be
2403 * got tag -l will list tags just fine, so I don't know what
2406 error = got_object_open_as_tag(&tag, repo, id);
2413 tagger = got_object_tag_get_tagger(tag);
2414 tagger_time = got_object_tag_get_tagger_time(tag);
2416 error = got_object_id_str(&id_str,
2417 got_object_tag_get_object_id(tag));
2421 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2422 strlen(id_str)) != 0)
2425 tag_commit0 = strdup(got_object_tag_get_message(tag));
2426 if (tag_commit0 == NULL) {
2427 error = got_error_from_errno("strdup");
2431 tag_commit = tag_commit0;
2432 while (*tag_commit == '\n')
2437 newline = strchr(tag_commit, '\n');
2441 if (summary_header_displayed == 0) {
2442 kerr = khtml_attr(gw_trans->gw_html_req,
2443 KELEM_DIV, KATTR_ID,
2444 "summary_tags_title_wrapper", KATTR__MAX);
2445 if (kerr != KCGI_OK)
2447 kerr = khtml_attr(gw_trans->gw_html_req,
2448 KELEM_DIV, KATTR_ID,
2449 "summary_tags_title", KATTR__MAX);
2450 if (kerr != KCGI_OK)
2452 kerr = khtml_puts(gw_trans->gw_html_req,
2454 if (kerr != KCGI_OK)
2456 kerr = khtml_closeelem(gw_trans->gw_html_req,
2458 if (kerr != KCGI_OK)
2460 kerr = khtml_attr(gw_trans->gw_html_req,
2461 KELEM_DIV, KATTR_ID,
2462 "summary_tags_content", KATTR__MAX);
2463 if (kerr != KCGI_OK)
2465 summary_header_displayed = 1;
2468 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2469 KATTR_ID, "tags_wrapper", KATTR__MAX);
2470 if (kerr != KCGI_OK)
2472 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2473 KATTR_ID, "tags_age", KATTR__MAX);
2474 if (kerr != KCGI_OK)
2476 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2479 kerr = khtml_puts(gw_trans->gw_html_req,
2481 if (kerr != KCGI_OK)
2483 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2484 if (kerr != KCGI_OK)
2486 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2487 KATTR_ID, "tags", KATTR__MAX);
2488 if (kerr != KCGI_OK)
2490 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2491 if (kerr != KCGI_OK)
2493 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2494 if (kerr != KCGI_OK)
2496 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2497 KATTR_ID, "tags_name", KATTR__MAX);
2498 if (kerr != KCGI_OK)
2500 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2501 gw_trans->repo_name, id_str) == -1) {
2502 error = got_error_from_errno("asprintf");
2505 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2506 KATTR_HREF, href_tag, KATTR__MAX);
2507 if (kerr != KCGI_OK)
2509 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2510 if (kerr != KCGI_OK)
2512 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2513 if (kerr != KCGI_OK)
2516 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2517 KATTR_ID, "navs_wrapper", KATTR__MAX);
2518 if (kerr != KCGI_OK)
2520 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2521 KATTR_ID, "navs", KATTR__MAX);
2522 if (kerr != KCGI_OK)
2525 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2526 KATTR_HREF, href_tag, KATTR__MAX);
2527 if (kerr != KCGI_OK)
2529 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2530 if (kerr != KCGI_OK)
2532 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2533 if (kerr != KCGI_OK)
2536 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2537 if (kerr != KCGI_OK)
2540 if (asprintf(&href_briefs,
2541 "?path=%s&action=briefs&commit=%s",
2542 gw_trans->repo_name, id_str) == -1) {
2543 error = got_error_from_errno("asprintf");
2546 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2547 KATTR_HREF, href_briefs, KATTR__MAX);
2548 if (kerr != KCGI_OK)
2550 kerr = khtml_puts(gw_trans->gw_html_req,
2552 if (kerr != KCGI_OK)
2554 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2555 if (kerr != KCGI_OK)
2558 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2559 if (kerr != KCGI_OK)
2562 if (asprintf(&href_commits,
2563 "?path=%s&action=commits&commit=%s",
2564 gw_trans->repo_name, id_str) == -1) {
2565 error = got_error_from_errno("asprintf");
2568 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2569 KATTR_HREF, href_commits, KATTR__MAX);
2570 if (kerr != KCGI_OK)
2572 kerr = khtml_puts(gw_trans->gw_html_req,
2574 if (kerr != KCGI_OK)
2576 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2577 if (kerr != KCGI_OK)
2580 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2581 KATTR_ID, "dotted_line", KATTR__MAX);
2582 if (kerr != KCGI_OK)
2584 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2585 if (kerr != KCGI_OK)
2589 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2590 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2591 if (kerr != KCGI_OK)
2593 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2594 if (kerr != KCGI_OK)
2596 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2597 if (kerr != KCGI_OK)
2599 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2600 KATTR_ID, "tag_info_date", KATTR__MAX);
2601 if (kerr != KCGI_OK)
2603 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2606 kerr = khtml_puts(gw_trans->gw_html_req,
2608 if (kerr != KCGI_OK)
2610 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2611 if (kerr != KCGI_OK)
2614 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2615 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2616 if (kerr != KCGI_OK)
2618 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2619 if (kerr != KCGI_OK)
2621 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2622 if (kerr != KCGI_OK)
2624 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2625 KATTR_ID, "tag_info_date", KATTR__MAX);
2626 if (kerr != KCGI_OK)
2628 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2629 if (kerr != KCGI_OK)
2631 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2632 if (kerr != KCGI_OK)
2635 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2636 KATTR_ID, "tag_info", KATTR__MAX);
2637 if (kerr != KCGI_OK)
2640 * XXX: keeping this for now, since kcgihtml does not
2641 * convert \n into <br /> yet.
2643 error = gw_html_escape(&escaped_tag_commit, tag_commit);
2646 kerr = khttp_puts(gw_trans->gw_req, escaped_tag_commit);
2647 if (kerr != KCGI_OK)
2653 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2654 if (kerr != KCGI_OK)
2657 if (limit && --limit == 0)
2660 got_object_tag_close(tag);
2668 free(escaped_tag_commit);
2669 escaped_tag_commit = NULL;
2677 href_commits = NULL;
2681 got_object_tag_close(tag);
2685 free(escaped_tag_commit);
2690 got_ref_list_free(&refs);
2692 got_repo_close(repo);
2693 if (error == NULL && kerr != KCGI_OK)
2694 error = gw_kcgi_error(kerr);
2699 gw_free_headers(struct gw_header *header)
2703 if (header->commit != NULL)
2704 got_object_commit_close(header->commit);
2706 got_repo_close(header->repo);
2707 free(header->refs_str);
2708 free(header->commit_id);
2709 free(header->parent_id);
2710 free(header->tree_id);
2711 free(header->commit_msg);
2714 static struct gw_header *
2717 struct gw_header *header;
2719 header = malloc(sizeof(*header));
2723 header->repo = NULL;
2724 header->commit = NULL;
2726 header->path = NULL;
2727 SIMPLEQ_INIT(&header->refs);
2732 static const struct got_error *
2733 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2736 const struct got_error *error = NULL;
2737 struct got_commit_graph *graph = NULL;
2739 error = got_commit_graph_open(&graph, header->path, 0);
2743 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2749 error = got_commit_graph_iter_next(&header->id, graph,
2750 header->repo, NULL, NULL);
2752 if (error->code == GOT_ERR_ITER_COMPLETED)
2756 if (header->id == NULL)
2759 error = got_object_open_as_commit(&header->commit, header->repo,
2764 error = gw_get_commit(gw_trans, header);
2766 struct gw_header *n_header = NULL;
2767 if ((n_header = gw_init_header()) == NULL) {
2768 error = got_error_from_errno("malloc");
2772 n_header->refs_str = strdup(header->refs_str);
2773 n_header->commit_id = strdup(header->commit_id);
2774 n_header->parent_id = strdup(header->parent_id);
2775 n_header->tree_id = strdup(header->tree_id);
2776 n_header->author = strdup(header->author);
2777 n_header->committer = strdup(header->committer);
2778 n_header->commit_msg = strdup(header->commit_msg);
2779 n_header->committer_time = header->committer_time;
2780 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2783 if (error || (limit && --limit == 0))
2788 got_commit_graph_close(graph);
2792 static const struct got_error *
2793 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2795 const struct got_error *error = NULL;
2796 struct got_reflist_entry *re;
2797 struct got_object_id *id2 = NULL;
2798 struct got_object_qid *parent_id;
2799 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2802 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2805 struct got_tag_object *tag = NULL;
2808 name = got_ref_get_name(re->ref);
2809 if (strcmp(name, GOT_REF_HEAD) == 0)
2811 if (strncmp(name, "refs/", 5) == 0)
2813 if (strncmp(name, "got/", 4) == 0)
2815 if (strncmp(name, "heads/", 6) == 0)
2817 if (strncmp(name, "remotes/", 8) == 0)
2819 if (strncmp(name, "tags/", 5) == 0) {
2820 error = got_object_open_as_tag(&tag, header->repo,
2823 if (error->code != GOT_ERR_OBJ_TYPE)
2826 * Ref points at something other
2833 cmp = got_object_id_cmp(tag ?
2834 got_object_tag_get_object_id(tag) : re->id, header->id);
2836 got_object_tag_close(tag);
2840 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2841 s ? ", " : "", name) == -1) {
2842 error = got_error_from_errno("asprintf");
2846 header->refs_str = strdup(refs_str);
2850 if (refs_str == NULL)
2851 header->refs_str = strdup("");
2854 error = got_object_id_str(&header->commit_id, header->id);
2858 error = got_object_id_str(&header->tree_id,
2859 got_object_commit_get_tree_id(header->commit));
2863 if (gw_trans->action == GW_DIFF) {
2864 parent_id = SIMPLEQ_FIRST(
2865 got_object_commit_get_parent_ids(header->commit));
2866 if (parent_id != NULL) {
2867 id2 = got_object_id_dup(parent_id->id);
2869 error = got_object_id_str(&header->parent_id, id2);
2874 header->parent_id = strdup("/dev/null");
2876 header->parent_id = strdup("");
2878 header->committer_time =
2879 got_object_commit_get_committer_time(header->commit);
2882 got_object_commit_get_author(header->commit);
2884 got_object_commit_get_committer(header->commit);
2888 /* XXX Doesn't the log message require escaping? */
2889 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2893 commit_msg = commit_msg0;
2894 while (*commit_msg == '\n')
2897 header->commit_msg = strdup(commit_msg);
2902 static const struct got_error *
2903 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2905 const struct got_error *error = NULL;
2906 char *in_repo_path = NULL;
2908 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2912 if (gw_trans->commit == NULL) {
2913 struct got_reference *head_ref;
2914 error = got_ref_open(&head_ref, header->repo,
2915 gw_trans->headref, 0);
2919 error = got_ref_resolve(&header->id, header->repo, head_ref);
2920 got_ref_close(head_ref);
2924 error = got_object_open_as_commit(&header->commit,
2925 header->repo, header->id);
2927 struct got_reference *ref;
2928 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2929 if (error == NULL) {
2931 error = got_ref_resolve(&header->id, header->repo, ref);
2935 error = got_object_get_type(&obj_type, header->repo,
2939 if (obj_type == GOT_OBJ_TYPE_TAG) {
2940 struct got_tag_object *tag;
2941 error = got_object_open_as_tag(&tag,
2942 header->repo, header->id);
2945 if (got_object_tag_get_object_type(tag) !=
2946 GOT_OBJ_TYPE_COMMIT) {
2947 got_object_tag_close(tag);
2948 error = got_error(GOT_ERR_OBJ_TYPE);
2952 header->id = got_object_id_dup(
2953 got_object_tag_get_object_id(tag));
2954 if (header->id == NULL)
2955 error = got_error_from_errno(
2956 "got_object_id_dup");
2957 got_object_tag_close(tag);
2960 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2961 error = got_error(GOT_ERR_OBJ_TYPE);
2964 error = got_object_open_as_commit(&header->commit,
2965 header->repo, header->id);
2969 if (header->commit == NULL) {
2970 error = got_repo_match_object_id_prefix(&header->id,
2971 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2976 error = got_repo_match_object_id_prefix(&header->id,
2977 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2981 error = got_repo_map_path(&in_repo_path, header->repo,
2982 gw_trans->repo_path, 1);
2987 header->path = strdup(in_repo_path);
2991 error = got_ref_list(&header->refs, header->repo, NULL,
2992 got_ref_cmp_by_name, NULL);
2996 error = gw_get_commits(gw_trans, header, limit);
3004 char datebuf[11]; /* YYYY-MM-DD + NUL */
3007 struct gw_blame_cb_args {
3008 struct blame_line *lines;
3012 off_t *line_offsets;
3014 struct got_repository *repo;
3015 struct gw_trans *gw_trans;
3018 static const struct got_error *
3019 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3021 const struct got_error *err = NULL;
3022 struct gw_blame_cb_args *a = arg;
3023 struct blame_line *bline;
3025 size_t linesize = 0;
3026 struct got_commit_object *commit = NULL;
3029 time_t committer_time;
3030 enum kcgi_err kerr = KCGI_OK;
3032 if (nlines != a->nlines ||
3033 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3034 return got_error(GOT_ERR_RANGE);
3037 return NULL; /* no change in this commit */
3039 /* Annotate this line. */
3040 bline = &a->lines[lineno - 1];
3041 if (bline->annotated)
3043 err = got_object_id_str(&bline->id_str, id);
3047 err = got_object_open_as_commit(&commit, a->repo, id);
3051 bline->committer = strdup(got_object_commit_get_committer(commit));
3052 if (bline->committer == NULL) {
3053 err = got_error_from_errno("strdup");
3057 committer_time = got_object_commit_get_committer_time(commit);
3058 if (localtime_r(&committer_time, &tm) == NULL)
3059 return got_error_from_errno("localtime_r");
3060 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3061 &tm) >= sizeof(bline->datebuf)) {
3062 err = got_error(GOT_ERR_NO_SPACE);
3065 bline->annotated = 1;
3067 /* Print lines annotated so far. */
3068 bline = &a->lines[a->lineno_cur - 1];
3069 if (!bline->annotated)
3072 offset = a->line_offsets[a->lineno_cur - 1];
3073 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3074 err = got_error_from_errno("fseeko");
3078 while (bline->annotated) {
3079 char *smallerthan, *at, *nl, *committer;
3080 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3083 if (getline(&line, &linesize, a->f) == -1) {
3085 err = got_error_from_errno("getline");
3089 committer = bline->committer;
3090 smallerthan = strchr(committer, '<');
3091 if (smallerthan && smallerthan[1] != '\0')
3092 committer = smallerthan + 1;
3093 at = strchr(committer, '@');
3096 len = strlen(committer);
3098 committer[8] = '\0';
3100 nl = strchr(line, '\n');
3104 if (a->gw_trans->repo_folder == NULL)
3105 a->gw_trans->repo_folder = strdup("");
3106 if (a->gw_trans->repo_folder == NULL)
3110 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3111 "blame_wrapper", KATTR__MAX);
3112 if (kerr != KCGI_OK)
3114 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3115 "blame_number", KATTR__MAX);
3116 if (kerr != KCGI_OK)
3118 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3119 a->lineno_cur) == -1)
3121 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3122 if (kerr != KCGI_OK)
3124 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3125 if (kerr != KCGI_OK)
3128 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3129 "blame_hash", KATTR__MAX);
3130 if (kerr != KCGI_OK)
3132 if (asprintf(&href_diff,
3133 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3134 a->gw_trans->repo_name, bline->id_str,
3135 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3136 err = got_error_from_errno("asprintf");
3139 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3140 err = got_error_from_errno("asprintf");
3143 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3144 KATTR_HREF, href_diff, KATTR__MAX);
3145 if (kerr != KCGI_OK)
3147 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3148 if (kerr != KCGI_OK)
3150 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3151 if (kerr != KCGI_OK)
3154 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3155 "blame_date", KATTR__MAX);
3156 if (kerr != KCGI_OK)
3158 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3159 if (kerr != KCGI_OK)
3161 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3162 if (kerr != KCGI_OK)
3165 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3166 "blame_author", KATTR__MAX);
3167 if (kerr != KCGI_OK)
3169 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3170 if (kerr != KCGI_OK)
3172 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3173 if (kerr != KCGI_OK)
3176 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3177 "blame_code", KATTR__MAX);
3178 if (kerr != KCGI_OK)
3180 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3181 if (kerr != KCGI_OK)
3183 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3184 if (kerr != KCGI_OK)
3187 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3188 if (kerr != KCGI_OK)
3192 bline = &a->lines[a->lineno_cur - 1];
3200 got_object_commit_close(commit);
3202 if (err == NULL && kerr != KCGI_OK)
3203 err = gw_kcgi_error(kerr);
3207 static const struct got_error *
3208 gw_output_file_blame(struct gw_trans *gw_trans)
3210 const struct got_error *error = NULL;
3211 struct got_repository *repo = NULL;
3212 struct got_object_id *obj_id = NULL;
3213 struct got_object_id *commit_id = NULL;
3214 struct got_blob_object *blob = NULL;
3215 char *path = NULL, *in_repo_path = NULL;
3216 struct gw_blame_cb_args bca;
3220 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3224 if (asprintf(&path, "%s%s%s",
3225 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3226 gw_trans->repo_folder ? "/" : "",
3227 gw_trans->repo_file) == -1) {
3228 error = got_error_from_errno("asprintf");
3232 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3236 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3237 GOT_OBJ_TYPE_COMMIT, 1, repo);
3241 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3245 if (obj_id == NULL) {
3246 error = got_error(GOT_ERR_NO_OBJ);
3250 error = got_object_get_type(&obj_type, repo, obj_id);
3254 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3255 error = got_error(GOT_ERR_OBJ_TYPE);
3259 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3263 bca.f = got_opentemp();
3264 if (bca.f == NULL) {
3265 error = got_error_from_errno("got_opentemp");
3268 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3269 &bca.line_offsets, bca.f, blob);
3270 if (error || bca.nlines == 0)
3273 /* Don't include \n at EOF in the blame line count. */
3274 if (bca.line_offsets[bca.nlines - 1] == filesize)
3277 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3278 if (bca.lines == NULL) {
3279 error = got_error_from_errno("calloc");
3283 bca.nlines_prec = 0;
3290 bca.gw_trans = gw_trans;
3292 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3297 free(bca.line_offsets);
3303 for (i = 0; i < bca.nlines; i++) {
3304 struct blame_line *bline = &bca.lines[i];
3305 free(bline->id_str);
3306 free(bline->committer);
3309 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3310 error = got_error_from_errno("fclose");
3312 got_object_blob_close(blob);
3314 got_repo_close(repo);
3318 static const struct got_error *
3319 gw_output_blob_buf(struct gw_trans *gw_trans)
3321 const struct got_error *error = NULL;
3322 struct got_repository *repo = NULL;
3323 struct got_object_id *obj_id = NULL;
3324 struct got_object_id *commit_id = NULL;
3325 struct got_blob_object *blob = NULL;
3326 char *path = NULL, *in_repo_path = NULL;
3327 int obj_type, set_mime = 0;
3330 enum kcgi_err kerr = KCGI_OK;
3332 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3336 if (asprintf(&path, "%s%s%s",
3337 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3338 gw_trans->repo_folder ? "/" : "",
3339 gw_trans->repo_file) == -1) {
3340 error = got_error_from_errno("asprintf");
3344 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3348 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3349 GOT_OBJ_TYPE_COMMIT, 1, repo);
3353 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3357 if (obj_id == NULL) {
3358 error = got_error(GOT_ERR_NO_OBJ);
3362 error = got_object_get_type(&obj_type, repo, obj_id);
3366 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3367 error = got_error(GOT_ERR_OBJ_TYPE);
3371 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3375 hdrlen = got_object_blob_get_hdrlen(blob);
3377 error = got_object_blob_read_block(&len, blob);
3380 buf = got_object_blob_get_read_buf(blob);
3383 * Skip blob object header first time around,
3384 * which also contains a zero byte.
3387 if (set_mime == 0) {
3388 if (isbinary(buf, len - hdrlen))
3389 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3391 gw_trans->mime = KMIME_TEXT_PLAIN;
3393 error = gw_display_index(gw_trans);
3397 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3406 got_object_blob_close(blob);
3408 got_repo_close(repo);
3409 if (error == NULL && kerr != KCGI_OK)
3410 error = gw_kcgi_error(kerr);
3414 static const struct got_error *
3415 gw_output_repo_tree(struct gw_trans *gw_trans)
3417 const struct got_error *error = NULL;
3418 struct got_repository *repo = NULL;
3419 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3420 struct got_tree_object *tree = NULL;
3421 char *path = NULL, *in_repo_path = NULL;
3422 char *id_str = NULL;
3423 char *build_folder = NULL;
3424 char *href_blob = NULL, *href_blame = NULL;
3425 const char *class = NULL;
3426 int nentries, i, class_flip = 0;
3427 enum kcgi_err kerr = KCGI_OK;
3429 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3433 if (gw_trans->repo_folder != NULL)
3434 path = strdup(gw_trans->repo_folder);
3436 error = got_repo_map_path(&in_repo_path, repo,
3437 gw_trans->repo_path, 1);
3441 path = in_repo_path;
3444 if (gw_trans->commit == NULL) {
3445 struct got_reference *head_ref;
3446 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3449 error = got_ref_resolve(&commit_id, repo, head_ref);
3452 got_ref_close(head_ref);
3455 error = got_repo_match_object_id(&commit_id, NULL,
3456 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3461 error = got_object_id_str(&gw_trans->commit, commit_id);
3465 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3469 error = got_object_open_as_tree(&tree, repo, tree_id);
3473 nentries = got_object_tree_get_nentries(tree);
3474 for (i = 0; i < nentries; i++) {
3475 struct got_tree_entry *te;
3476 const char *modestr = "";
3479 te = got_object_tree_get_entry(tree, i);
3481 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3485 mode = got_tree_entry_get_mode(te);
3486 if (got_object_tree_entry_is_submodule(te))
3488 else if (S_ISLNK(mode))
3490 else if (S_ISDIR(mode))
3492 else if (mode & S_IXUSR)
3495 if (class_flip == 0) {
3496 class = "back_lightgray";
3499 class = "back_white";
3503 if (S_ISDIR(mode)) {
3504 if (asprintf(&build_folder, "%s/%s",
3505 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3506 got_tree_entry_get_name(te)) == -1) {
3507 error = got_error_from_errno(
3511 if (asprintf(&href_blob,
3512 "?path=%s&action=%s&commit=%s&folder=%s",
3513 gw_trans->repo_name, gw_trans->action_name,
3514 gw_trans->commit, build_folder) == -1) {
3515 error = got_error_from_errno("asprintf");
3519 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3520 KATTR_ID, "tree_wrapper", KATTR__MAX);
3521 if (kerr != KCGI_OK)
3523 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3524 KATTR_ID, "tree_line", KATTR_CLASS, class,
3526 if (kerr != KCGI_OK)
3528 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3529 KATTR_HREF, href_blob, KATTR_CLASS,
3530 "diff_directory", KATTR__MAX);
3531 if (kerr != KCGI_OK)
3533 kerr = khtml_puts(gw_trans->gw_html_req,
3534 got_tree_entry_get_name(te));
3535 if (kerr != KCGI_OK)
3537 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3538 if (kerr != KCGI_OK)
3540 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3541 if (kerr != KCGI_OK)
3543 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3544 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3546 if (kerr != KCGI_OK)
3548 kerr = khtml_entity(gw_trans->gw_html_req,
3550 if (kerr != KCGI_OK)
3552 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3553 if (kerr != KCGI_OK)
3556 if (asprintf(&href_blob,
3557 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3558 gw_trans->repo_name, "blob", gw_trans->commit,
3559 got_tree_entry_get_name(te),
3560 gw_trans->repo_folder ?
3561 gw_trans->repo_folder : "") == -1) {
3562 error = got_error_from_errno("asprintf");
3565 if (asprintf(&href_blame,
3566 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3567 gw_trans->repo_name, "blame", gw_trans->commit,
3568 got_tree_entry_get_name(te),
3569 gw_trans->repo_folder ?
3570 gw_trans->repo_folder : "") == -1) {
3571 error = got_error_from_errno("asprintf");
3575 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3576 KATTR_ID, "tree_wrapper", KATTR__MAX);
3577 if (kerr != KCGI_OK)
3579 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3580 KATTR_ID, "tree_line", KATTR_CLASS, class,
3582 if (kerr != KCGI_OK)
3584 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3585 KATTR_HREF, href_blob, KATTR__MAX);
3586 if (kerr != KCGI_OK)
3588 kerr = khtml_puts(gw_trans->gw_html_req,
3589 got_tree_entry_get_name(te));
3590 if (kerr != KCGI_OK)
3592 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3593 if (kerr != KCGI_OK)
3595 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3596 if (kerr != KCGI_OK)
3598 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3599 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3601 if (kerr != KCGI_OK)
3604 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3605 KATTR_HREF, href_blob, KATTR__MAX);
3606 if (kerr != KCGI_OK)
3608 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3609 if (kerr != KCGI_OK)
3611 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3612 if (kerr != KCGI_OK)
3615 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3616 if (kerr != KCGI_OK)
3619 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3620 KATTR_HREF, href_blame, KATTR__MAX);
3621 if (kerr != KCGI_OK)
3623 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3624 if (kerr != KCGI_OK)
3627 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3628 if (kerr != KCGI_OK)
3636 build_folder = NULL;
3640 got_object_tree_close(tree);
3642 got_repo_close(repo);
3649 if (error == NULL && kerr != KCGI_OK)
3650 error = gw_kcgi_error(kerr);
3654 static const struct got_error *
3655 gw_get_repo_heads(char **head_html, struct gw_trans *gw_trans)
3657 const struct got_error *error = NULL;
3658 struct got_repository *repo = NULL;
3659 struct got_reflist_head refs;
3660 struct got_reflist_entry *re;
3661 char *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
3662 struct buf *diffbuf = NULL;
3667 SIMPLEQ_INIT(&refs);
3669 error = buf_alloc(&diffbuf, 0);
3673 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3677 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3682 SIMPLEQ_FOREACH(re, &refs, entry) {
3685 refname = strdup(got_ref_get_name(re->ref));
3686 if (refname == NULL) {
3687 error = got_error_from_errno("got_ref_to_str");
3691 if (strncmp(refname, "refs/heads/", 11) != 0) {
3696 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3701 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
3702 refname, gw_trans->repo_name, refname,
3703 gw_trans->repo_name, refname, gw_trans->repo_name,
3705 error = got_error_from_errno("asprintf");
3709 if (strncmp(refname, "refs/heads/", 11) == 0)
3712 if (asprintf(&head_row, heads_row, age, refname,
3713 head_navs_disp) == -1) {
3714 error = got_error_from_errno("asprintf");
3718 error = buf_puts(&newsize, diffbuf, head_row);
3720 free(head_navs_disp);
3724 if (buf_len(diffbuf) > 0) {
3725 error = buf_putc(diffbuf, '\0');
3726 *head_html = strdup(buf_get(diffbuf));
3727 if (*head_html == NULL)
3728 error = got_error_from_errno("strdup");
3732 got_ref_list_free(&refs);
3734 got_repo_close(repo);
3739 gw_get_site_link(struct gw_trans *gw_trans)
3741 char *link = NULL, *repo = NULL, *action = NULL;
3743 if (gw_trans->repo_name != NULL &&
3744 asprintf(&repo, " / <a href='?path=%s&action=summary'>%s</a>",
3745 gw_trans->repo_name, gw_trans->repo_name) == -1)
3748 if (gw_trans->action_name != NULL &&
3749 asprintf(&action, " / %s", gw_trans->action_name) == -1) {
3754 if (asprintf(&link, site_link, GOTWEB,
3755 gw_trans->gw_conf->got_site_link,
3756 repo ? repo : "", action ? action : "") == -1) {
3767 static const struct got_error *
3768 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
3770 const struct got_error *error = NULL;
3772 enum kcgi_err kerr = KCGI_OK;
3774 if (strncmp(buf, "-", 1) == 0)
3775 color = "diff_minus";
3776 else if (strncmp(buf, "+", 1) == 0)
3777 color = "diff_plus";
3778 else if (strncmp(buf, "@@", 2) == 0)
3779 color = "diff_chunk_header";
3780 else if (strncmp(buf, "@@", 2) == 0)
3781 color = "diff_chunk_header";
3782 else if (strncmp(buf, "commit +", 8) == 0)
3783 color = "diff_meta";
3784 else if (strncmp(buf, "commit -", 8) == 0)
3785 color = "diff_meta";
3786 else if (strncmp(buf, "blob +", 6) == 0)
3787 color = "diff_meta";
3788 else if (strncmp(buf, "blob -", 6) == 0)
3789 color = "diff_meta";
3790 else if (strncmp(buf, "file +", 6) == 0)
3791 color = "diff_meta";
3792 else if (strncmp(buf, "file -", 6) == 0)
3793 color = "diff_meta";
3794 else if (strncmp(buf, "from:", 5) == 0)
3795 color = "diff_author";
3796 else if (strncmp(buf, "via:", 4) == 0)
3797 color = "diff_author";
3798 else if (strncmp(buf, "date:", 5) == 0)
3799 color = "diff_date";
3800 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3801 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
3802 if (error == NULL && kerr != KCGI_OK)
3803 error = gw_kcgi_error(kerr);
3808 * XXX This function should not exist.
3809 * We should let khtml_puts(3) handle HTML escaping.
3811 static const struct got_error *
3812 gw_html_escape(char **escaped_html, const char *orig_html)
3814 const struct got_error *error = NULL;
3815 struct escape_pair {
3826 size_t orig_len, len;
3829 orig_len = strlen(orig_html);
3831 for (i = 0; i < orig_len; i++) {
3832 for (j = 0; j < nitems(esc); j++) {
3833 if (orig_html[i] != esc[j].c)
3835 len += strlen(esc[j].s) - 1 /* escaped char */;
3839 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
3840 if (*escaped_html == NULL)
3841 return got_error_from_errno("calloc");
3844 for (i = 0; i < orig_len; i++) {
3846 for (j = 0; j < nitems(esc); j++) {
3847 if (orig_html[i] != esc[j].c)
3850 if (strlcat(*escaped_html, esc[j].s, len + 1)
3852 error = got_error(GOT_ERR_NO_SPACE);
3855 x += strlen(esc[j].s);
3860 (*escaped_html)[x] = orig_html[i];
3866 free(*escaped_html);
3867 *escaped_html = NULL;
3869 (*escaped_html)[x] = '\0';
3875 main(int argc, char *argv[])
3877 const struct got_error *error = NULL;
3878 struct gw_trans *gw_trans;
3879 struct gw_dir *dir = NULL, *tdir;
3880 const char *page = "index";
3884 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3887 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3890 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3893 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3896 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3897 if (kerr != KCGI_OK) {
3898 error = gw_kcgi_error(kerr);
3902 if ((gw_trans->gw_conf =
3903 malloc(sizeof(struct gotweb_conf))) == NULL) {
3905 error = got_error_from_errno("malloc");
3909 TAILQ_INIT(&gw_trans->gw_dirs);
3910 TAILQ_INIT(&gw_trans->gw_headers);
3913 gw_trans->repos_total = 0;
3914 gw_trans->repo_path = NULL;
3915 gw_trans->commit = NULL;
3916 gw_trans->headref = strdup(GOT_REF_HEAD);
3917 gw_trans->mime = KMIME_TEXT_HTML;
3918 gw_trans->gw_tmpl->key = gw_templs;
3919 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3920 gw_trans->gw_tmpl->arg = gw_trans;
3921 gw_trans->gw_tmpl->cb = gw_template;
3922 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3926 error = gw_parse_querystring(gw_trans);
3930 if (gw_trans->action == GW_BLOB)
3931 error = gw_blob(gw_trans);
3933 error = gw_display_index(gw_trans);
3936 gw_trans->mime = KMIME_TEXT_PLAIN;
3937 gw_trans->action = GW_ERR;
3938 gw_display_error(gw_trans, error);
3941 free(gw_trans->gw_conf->got_repos_path);
3942 free(gw_trans->gw_conf->got_site_name);
3943 free(gw_trans->gw_conf->got_site_owner);
3944 free(gw_trans->gw_conf->got_site_link);
3945 free(gw_trans->gw_conf->got_logo);
3946 free(gw_trans->gw_conf->got_logo_url);
3947 free(gw_trans->gw_conf);
3948 free(gw_trans->commit);
3949 free(gw_trans->repo_path);
3950 free(gw_trans->repo_name);
3951 free(gw_trans->repo_file);
3952 free(gw_trans->action_name);
3953 free(gw_trans->headref);
3955 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3957 free(dir->description);
3966 khttp_free(gw_trans->gw_req);