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_colordiff_line(struct gw_trans *, char *);
181 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
183 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
185 static const struct got_error *gw_gen_author_header(struct gw_trans *,
187 static const struct got_error *gw_gen_age_header(struct gw_trans *,
189 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
191 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
193 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
195 static void gw_free_headers(struct gw_header *);
196 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
198 static const struct got_error* gw_display_index(struct gw_trans *);
199 static void gw_display_error(struct gw_trans *,
200 const struct got_error *);
202 static int gw_template(size_t, void *);
204 static const struct got_error* gw_get_header(struct gw_trans *,
205 struct gw_header *, int);
206 static const struct got_error* gw_get_commits(struct gw_trans *,
207 struct gw_header *, int);
208 static const struct got_error* gw_get_commit(struct gw_trans *,
210 static const struct got_error* gw_apply_unveil(const char *, const char *);
211 static const struct got_error* gw_blame_cb(void *, int, int,
212 struct got_object_id *);
213 static const struct got_error* gw_load_got_paths(struct gw_trans *);
214 static const struct got_error* gw_load_got_path(struct gw_trans *,
216 static const struct got_error* gw_parse_querystring(struct gw_trans *);
218 static const struct got_error* gw_blame(struct gw_trans *);
219 static const struct got_error* gw_blob(struct gw_trans *);
220 static const struct got_error* gw_diff(struct gw_trans *);
221 static const struct got_error* gw_index(struct gw_trans *);
222 static const struct got_error* gw_commits(struct gw_trans *);
223 static const struct got_error* gw_briefs(struct gw_trans *);
224 static const struct got_error* gw_summary(struct gw_trans *);
225 static const struct got_error* gw_tree(struct gw_trans *);
226 static const struct got_error* gw_tag(struct gw_trans *);
228 struct gw_query_action {
229 unsigned int func_id;
230 const char *func_name;
231 const struct got_error *(*func_main)(struct gw_trans *);
235 enum gw_query_actions {
248 static struct gw_query_action gw_query_funcs[] = {
249 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
250 { GW_BLOB, "blob", NULL, NULL },
251 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
252 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
253 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
254 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
255 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
256 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
257 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
258 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
261 static const struct got_error *
262 gw_kcgi_error(enum kcgi_err kerr)
267 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
268 return got_error(GOT_ERR_CANCELLED);
270 if (kerr == KCGI_ENOMEM)
271 return got_error_set_errno(ENOMEM,
272 kcgi_strerror(kerr));
274 if (kerr == KCGI_ENFILE)
275 return got_error_set_errno(ENFILE,
276 kcgi_strerror(kerr));
278 if (kerr == KCGI_EAGAIN)
279 return got_error_set_errno(EAGAIN,
280 kcgi_strerror(kerr));
282 if (kerr == KCGI_FORM)
283 return got_error_msg(GOT_ERR_IO,
284 kcgi_strerror(kerr));
286 return got_error_from_errno(kcgi_strerror(kerr));
289 static const struct got_error *
290 gw_apply_unveil(const char *repo_path, const char *repo_file)
292 const struct got_error *err;
294 if (repo_path && repo_file) {
296 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
297 return got_error_from_errno("asprintf unveil");
298 if (unveil(full_path, "r") != 0)
299 return got_error_from_errno2("unveil", full_path);
302 if (repo_path && unveil(repo_path, "r") != 0)
303 return got_error_from_errno2("unveil", repo_path);
305 if (unveil("/tmp", "rwc") != 0)
306 return got_error_from_errno2("unveil", "/tmp");
308 err = got_privsep_unveil_exec_helpers();
312 if (unveil(NULL, NULL) != 0)
313 return got_error_from_errno("unveil");
318 static const struct got_error *
319 gw_empty_string(char **s)
323 return got_error_from_errno("strdup");
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;
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, NULL);
357 error = gw_get_header(gw_trans, header, 1);
362 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
363 "blame_header_wrapper", KATTR__MAX);
366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
367 "blame_header", KATTR__MAX);
370 error = gw_get_time_str(&age, header->committer_time,
374 error = gw_gen_age_header(gw_trans, age ?age : "");
377 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
380 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
383 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
384 "dotted_line", KATTR__MAX);
387 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
392 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
393 "blame", KATTR__MAX);
396 error = gw_output_file_blame(gw_trans);
399 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
401 got_ref_list_free(&header->refs);
402 gw_free_headers(header);
403 if (error == NULL && kerr != KCGI_OK)
404 error = gw_kcgi_error(kerr);
408 static const struct got_error *
409 gw_blob(struct gw_trans *gw_trans)
411 const struct got_error *error = NULL;
412 struct gw_header *header = NULL;
414 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
416 return got_error_from_errno("pledge");
418 if ((header = gw_init_header()) == NULL)
419 return got_error_from_errno("malloc");
421 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
425 error = gw_get_header(gw_trans, header, 1);
429 error = gw_output_blob_buf(gw_trans);
431 got_ref_list_free(&header->refs);
432 gw_free_headers(header);
436 static const struct got_error *
437 gw_diff(struct gw_trans *gw_trans)
439 const struct got_error *error = NULL;
440 struct gw_header *header = NULL;
442 enum kcgi_err kerr = KCGI_OK;
444 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
446 return got_error_from_errno("pledge");
448 if ((header = gw_init_header()) == NULL)
449 return got_error_from_errno("malloc");
451 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
455 error = gw_get_header(gw_trans, header, 1);
460 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
461 "diff_header_wrapper", KATTR__MAX);
464 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
465 "diff_header", KATTR__MAX);
468 error = gw_gen_diff_header(gw_trans, header->parent_id,
472 error = gw_gen_commit_header(gw_trans, header->commit_id,
476 error = gw_gen_tree_header(gw_trans, header->tree_id);
479 error = gw_gen_author_header(gw_trans, header->author);
482 error = gw_gen_committer_header(gw_trans, header->author);
485 error = gw_get_time_str(&age, header->committer_time,
489 error = gw_gen_age_header(gw_trans, age ?age : "");
492 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
495 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
499 "dotted_line", KATTR__MAX);
502 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
507 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
511 error = gw_output_diff(gw_trans, header);
515 /* diff content close */
516 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
520 got_ref_list_free(&header->refs);
521 gw_free_headers(header);
523 if (error == NULL && kerr != KCGI_OK)
524 error = gw_kcgi_error(kerr);
528 static const struct got_error *
529 gw_index(struct gw_trans *gw_trans)
531 const struct got_error *error = NULL;
532 struct gw_dir *gw_dir = NULL;
533 char *html, *navs, *next, *prev;
534 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
537 if (pledge("stdio rpath proc exec sendfd unveil",
539 error = got_error_from_errno("pledge");
543 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
547 error = gw_load_got_paths(gw_trans);
551 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
553 return gw_kcgi_error(kerr);
555 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
556 if (asprintf(&html, index_projects_empty,
557 gw_trans->gw_conf->got_repos_path) == -1)
558 return got_error_from_errno("asprintf");
559 kerr = khttp_puts(gw_trans->gw_req, html);
561 error = gw_kcgi_error(kerr);
566 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
569 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
570 if (gw_trans->page > 0 && (gw_trans->page *
571 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
580 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
581 gw_dir->name, gw_dir->name) == -1)
582 return got_error_from_errno("asprintf");
584 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
585 gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
588 return got_error_from_errno("asprintf");
590 kerr = khttp_puts(gw_trans->gw_req, html);
594 return gw_kcgi_error(kerr);
596 if (gw_trans->gw_conf->got_max_repos_display == 0)
599 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
600 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
602 return gw_kcgi_error(kerr);
603 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
604 (gw_trans->page > 0) &&
605 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
606 prev_disp == gw_trans->repos_total)) {
607 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
609 return gw_kcgi_error(kerr);
612 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
613 (gw_trans->page > 0) &&
614 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
615 prev_disp == gw_trans->repos_total)) {
616 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
617 return got_error_from_errno("asprintf");
618 kerr = khttp_puts(gw_trans->gw_req, prev);
621 return gw_kcgi_error(kerr);
624 kerr = khttp_puts(gw_trans->gw_req, div_end);
626 return gw_kcgi_error(kerr);
628 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
629 next_disp == gw_trans->gw_conf->got_max_repos_display &&
630 dir_c != (gw_trans->page + 1) *
631 gw_trans->gw_conf->got_max_repos_display) {
632 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
633 return got_error_from_errno("calloc");
634 kerr = khttp_puts(gw_trans->gw_req, next);
637 return gw_kcgi_error(kerr);
638 kerr = khttp_puts(gw_trans->gw_req, div_end);
640 error = gw_kcgi_error(kerr);
645 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
646 (gw_trans->page > 0) &&
647 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
648 prev_disp == gw_trans->repos_total)) {
649 kerr = khttp_puts(gw_trans->gw_req, div_end);
651 return gw_kcgi_error(kerr);
659 static const struct got_error *
660 gw_commits(struct gw_trans *gw_trans)
662 const struct got_error *error = NULL;
663 struct gw_header *header = NULL, *n_header = NULL;
665 char *href_diff = NULL, *href_blob = NULL;
666 enum kcgi_err kerr = KCGI_OK;
668 if ((header = gw_init_header()) == NULL)
669 return got_error_from_errno("malloc");
671 if (pledge("stdio rpath proc exec sendfd unveil",
673 error = got_error_from_errno("pledge");
677 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
681 error = gw_get_header(gw_trans, header,
682 gw_trans->gw_conf->got_max_commits_display);
687 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
689 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
690 "commits_line_wrapper", KATTR__MAX);
693 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
697 error = gw_gen_author_header(gw_trans, n_header->author);
700 error = gw_gen_committer_header(gw_trans, n_header->author);
703 error = gw_get_time_str(&age, n_header->committer_time,
707 error = gw_gen_age_header(gw_trans, age ?age : "");
710 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
715 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
716 "dotted_line", KATTR__MAX);
719 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
724 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
725 "commit", KATTR__MAX);
728 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
731 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
737 /* XXX: create gen code for this */
739 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
740 gw_trans->repo_name, n_header->commit_id) == -1) {
741 error = got_error_from_errno("asprintf");
744 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
745 KATTR_ID, "navs_wrapper", KATTR__MAX);
748 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
749 KATTR_ID, "navs", KATTR__MAX);
752 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
753 KATTR_HREF, href_diff, KATTR__MAX);
756 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
759 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
763 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
767 /* XXX: create gen code for this */
769 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
770 gw_trans->repo_name, n_header->commit_id) == -1) {
771 error = got_error_from_errno("asprintf");
774 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
775 KATTR_HREF, href_blob, KATTR__MAX);
778 khtml_puts(gw_trans->gw_html_req, "tree");
779 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
782 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
786 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
787 "solid_line", KATTR__MAX);
790 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
798 got_ref_list_free(&header->refs);
799 gw_free_headers(header);
800 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
801 gw_free_headers(n_header);
805 if (error == NULL && kerr != KCGI_OK)
806 error = gw_kcgi_error(kerr);
810 static const struct got_error *
811 gw_briefs(struct gw_trans *gw_trans)
813 const struct got_error *error = NULL;
814 struct gw_header *header = NULL, *n_header = NULL;
815 char *age = NULL, *age_html = NULL;
816 char *href_diff = NULL, *href_blob = NULL;
817 char *newline, *smallerthan;
818 enum kcgi_err kerr = KCGI_OK;
820 if ((header = gw_init_header()) == NULL)
821 return got_error_from_errno("malloc");
823 if (pledge("stdio rpath proc exec sendfd unveil",
825 error = got_error_from_errno("pledge");
829 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
833 if (gw_trans->action == GW_SUMMARY)
834 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
836 error = gw_get_header(gw_trans, header,
837 gw_trans->gw_conf->got_max_commits_display);
841 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
842 error = gw_get_time_str(&age, n_header->committer_time,
848 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
849 KATTR_ID, "briefs_wrapper", KATTR__MAX);
854 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
855 KATTR_ID, "briefs_age", KATTR__MAX);
858 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
859 error = got_error_from_errno("asprintf");
862 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
865 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
870 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
871 KATTR_ID, "briefs_author", KATTR__MAX);
874 smallerthan = strchr(n_header->author, '<');
877 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
880 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
885 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
886 gw_trans->repo_name, n_header->commit_id) == -1) {
887 error = got_error_from_errno("asprintf");
890 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
891 KATTR_ID, "briefs_log", KATTR__MAX);
894 newline = strchr(n_header->commit_msg, '\n');
897 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
898 KATTR_HREF, href_diff, KATTR__MAX);
901 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
904 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
909 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
910 KATTR_ID, "navs_wrapper", KATTR__MAX);
913 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
914 KATTR_ID, "navs", KATTR__MAX);
917 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
918 KATTR_HREF, href_diff, KATTR__MAX);
921 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
924 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
928 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
933 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
934 gw_trans->repo_name, n_header->commit_id) == -1) {
935 error = got_error_from_errno("asprintf");
938 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
939 KATTR_HREF, href_blob, KATTR__MAX);
942 khtml_puts(gw_trans->gw_html_req, "tree");
943 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
946 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
951 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
952 KATTR_ID, "dotted_line", KATTR__MAX);
955 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
969 got_ref_list_free(&header->refs);
970 gw_free_headers(header);
971 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
972 gw_free_headers(n_header);
977 if (error == NULL && kerr != KCGI_OK)
978 error = gw_kcgi_error(kerr);
982 static const struct got_error *
983 gw_summary(struct gw_trans *gw_trans)
985 const struct got_error *error = NULL;
986 char *age = NULL, *tags = NULL, *heads = NULL;
987 enum kcgi_err kerr = KCGI_OK;
989 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
990 return got_error_from_errno("pledge");
992 /* unveil is applied with gw_briefs below */
994 /* summary wrapper */
995 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
996 "summary_wrapper", KATTR__MAX);
998 return gw_kcgi_error(kerr);
1001 if (gw_trans->gw_conf->got_show_repo_description &&
1002 gw_trans->gw_dir->description != NULL &&
1003 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1004 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1005 KATTR_ID, "description_title", KATTR__MAX);
1006 if (kerr != KCGI_OK)
1008 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1009 if (kerr != KCGI_OK)
1011 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1012 if (kerr != KCGI_OK)
1014 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1015 KATTR_ID, "description", KATTR__MAX);
1016 if (kerr != KCGI_OK)
1018 kerr = khtml_puts(gw_trans->gw_html_req,
1019 gw_trans->gw_dir->description);
1020 if (kerr != KCGI_OK)
1022 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1023 if (kerr != KCGI_OK)
1028 if (gw_trans->gw_conf->got_show_repo_owner &&
1029 gw_trans->gw_dir->owner != NULL) {
1030 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1031 KATTR_ID, "repo_owner_title", KATTR__MAX);
1032 if (kerr != KCGI_OK)
1034 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1035 if (kerr != KCGI_OK)
1037 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1038 if (kerr != KCGI_OK)
1040 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1041 KATTR_ID, "repo_owner", KATTR__MAX);
1042 if (kerr != KCGI_OK)
1044 kerr = khtml_puts(gw_trans->gw_html_req,
1045 gw_trans->gw_dir->owner);
1046 if (kerr != KCGI_OK)
1048 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1049 if (kerr != KCGI_OK)
1054 if (gw_trans->gw_conf->got_show_repo_age) {
1055 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1056 "refs/heads", TM_LONG);
1060 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1061 KATTR_ID, "last_change_title", KATTR__MAX);
1062 if (kerr != KCGI_OK)
1064 kerr = khtml_puts(gw_trans->gw_html_req,
1066 if (kerr != KCGI_OK)
1068 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1069 if (kerr != KCGI_OK)
1071 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1072 KATTR_ID, "last_change", KATTR__MAX);
1073 if (kerr != KCGI_OK)
1075 kerr = khtml_puts(gw_trans->gw_html_req, age);
1076 if (kerr != KCGI_OK)
1078 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1079 if (kerr != KCGI_OK)
1085 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1086 gw_trans->gw_dir->url != NULL &&
1087 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1088 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1089 KATTR_ID, "cloneurl_title", KATTR__MAX);
1090 if (kerr != KCGI_OK)
1092 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1093 if (kerr != KCGI_OK)
1095 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1096 if (kerr != KCGI_OK)
1098 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1099 KATTR_ID, "cloneurl", KATTR__MAX);
1100 if (kerr != KCGI_OK)
1102 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1103 if (kerr != KCGI_OK)
1105 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1106 if (kerr != KCGI_OK)
1110 /* close summary wrapper */
1111 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1112 if (kerr != KCGI_OK)
1115 /* commit briefs header */
1116 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1117 "briefs_title_wrapper", KATTR__MAX);
1118 if (kerr != KCGI_OK)
1120 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1121 "briefs_title", KATTR__MAX);
1122 if (kerr != KCGI_OK)
1124 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1125 if (kerr != KCGI_OK)
1127 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1128 if (kerr != KCGI_OK)
1130 error = gw_briefs(gw_trans);
1135 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1141 error = gw_get_repo_heads(&heads, gw_trans);
1144 if (heads != NULL && strcmp(heads, "") != 0) {
1145 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1146 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
1147 if (kerr != KCGI_OK)
1149 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1150 KATTR_ID, "summary_heads_title", KATTR__MAX);
1151 if (kerr != KCGI_OK)
1153 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
1154 if (kerr != KCGI_OK)
1156 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1157 if (kerr != KCGI_OK)
1159 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1160 KATTR_ID, "summary_heads_content", KATTR__MAX);
1161 if (kerr != KCGI_OK)
1163 kerr = khttp_puts(gw_trans->gw_req, heads);
1164 if (kerr != KCGI_OK)
1166 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1167 if (kerr != KCGI_OK)
1174 if (error == NULL && kerr != KCGI_OK)
1175 error = gw_kcgi_error(kerr);
1179 static const struct got_error *
1180 gw_tree(struct gw_trans *gw_trans)
1182 const struct got_error *error = NULL;
1183 struct gw_header *header = NULL;
1184 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1185 char *age = NULL, *age_html = NULL;
1188 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1189 return got_error_from_errno("pledge");
1191 if ((header = gw_init_header()) == NULL)
1192 return got_error_from_errno("malloc");
1194 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1198 error = gw_get_header(gw_trans, header, 1);
1203 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1204 "tree_header_wrapper", KATTR__MAX);
1205 if (kerr != KCGI_OK)
1207 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1208 "tree_header", KATTR__MAX);
1209 if (kerr != KCGI_OK)
1211 error = gw_gen_tree_header(gw_trans, header->tree_id);
1214 error = gw_get_time_str(&age, header->committer_time,
1218 error = gw_gen_age_header(gw_trans, age ?age : "");
1221 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1224 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1225 if (kerr != KCGI_OK)
1227 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1228 "dotted_line", KATTR__MAX);
1229 if (kerr != KCGI_OK)
1231 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1232 if (kerr != KCGI_OK)
1236 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1237 "tree", KATTR__MAX);
1238 if (kerr != KCGI_OK)
1240 error = gw_output_repo_tree(gw_trans);
1244 /* tree content close */
1245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1247 got_ref_list_free(&header->refs);
1248 gw_free_headers(header);
1249 free(tree_html_disp);
1254 if (error == NULL && kerr != KCGI_OK)
1255 error = gw_kcgi_error(kerr);
1259 static const struct got_error *
1260 gw_tag(struct gw_trans *gw_trans)
1262 const struct got_error *error = NULL;
1263 struct gw_header *header = NULL;
1266 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1267 return got_error_from_errno("pledge");
1269 if ((header = gw_init_header()) == NULL)
1270 return got_error_from_errno("malloc");
1272 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1276 error = gw_get_header(gw_trans, header, 1);
1281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1282 "tag_header_wrapper", KATTR__MAX);
1283 if (kerr != KCGI_OK)
1285 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1286 "tag_header", KATTR__MAX);
1287 if (kerr != KCGI_OK)
1289 error = gw_gen_commit_header(gw_trans, header->commit_id,
1293 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1296 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1297 if (kerr != KCGI_OK)
1299 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1300 "dotted_line", KATTR__MAX);
1301 if (kerr != KCGI_OK)
1303 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1304 if (kerr != KCGI_OK)
1308 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1309 "tree", KATTR__MAX);
1310 if (kerr != KCGI_OK)
1313 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1317 /* tag content close */
1318 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1320 got_ref_list_free(&header->refs);
1321 gw_free_headers(header);
1322 if (error == NULL && kerr != KCGI_OK)
1323 error = gw_kcgi_error(kerr);
1327 static const struct got_error *
1328 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1330 const struct got_error *error = NULL;
1335 if (asprintf(&dir_test, "%s/%s/%s",
1336 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1337 GOTWEB_GIT_DIR) == -1)
1338 return got_error_from_errno("asprintf");
1340 dt = opendir(dir_test);
1344 gw_dir->path = strdup(dir_test);
1349 if (asprintf(&dir_test, "%s/%s/%s",
1350 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1351 GOTWEB_GOT_DIR) == -1)
1352 return got_error_from_errno("asprintf");
1354 dt = opendir(dir_test);
1359 error = got_error(GOT_ERR_NOT_GIT_REPO);
1363 if (asprintf(&dir_test, "%s/%s",
1364 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1365 return got_error_from_errno("asprintf");
1367 gw_dir->path = strdup(dir_test);
1369 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1373 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1376 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1377 "refs/heads", TM_DIFF);
1380 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1388 static const struct got_error *
1389 gw_load_got_paths(struct gw_trans *gw_trans)
1391 const struct got_error *error = NULL;
1393 struct dirent **sd_dent;
1394 struct gw_dir *gw_dir;
1396 unsigned int d_cnt, d_i;
1398 d = opendir(gw_trans->gw_conf->got_repos_path);
1400 error = got_error_from_errno2("opendir",
1401 gw_trans->gw_conf->got_repos_path);
1405 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1408 error = got_error_from_errno2("scandir",
1409 gw_trans->gw_conf->got_repos_path);
1413 for (d_i = 0; d_i < d_cnt; d_i++) {
1414 if (gw_trans->gw_conf->got_max_repos > 0 &&
1415 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1416 break; /* account for parent and self */
1418 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1419 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1422 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1423 return got_error_from_errno("gw_dir malloc");
1425 error = gw_load_got_path(gw_trans, gw_dir);
1426 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1433 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1434 !got_path_dir_is_empty(gw_dir->path)) {
1435 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1437 gw_trans->repos_total++;
1445 static const struct got_error *
1446 gw_parse_querystring(struct gw_trans *gw_trans)
1448 const struct got_error *error = NULL;
1450 struct gw_query_action *action = NULL;
1453 if (gw_trans->gw_req->fieldnmap[0]) {
1454 error = got_error_from_errno("bad parse");
1456 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1457 /* define gw_trans->repo_path */
1458 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1459 return got_error_from_errno("asprintf");
1461 if (asprintf(&gw_trans->repo_path, "%s/%s",
1462 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1463 return got_error_from_errno("asprintf");
1465 /* get action and set function */
1466 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1467 for (i = 0; i < nitems(gw_query_funcs); i++) {
1468 action = &gw_query_funcs[i];
1469 if (action->func_name == NULL)
1472 if (strcmp(action->func_name,
1473 p->parsed.s) == 0) {
1474 gw_trans->action = i;
1475 if (asprintf(&gw_trans->action_name,
1476 "%s", action->func_name) == -1)
1478 got_error_from_errno(
1487 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1488 if (asprintf(&gw_trans->commit, "%s",
1490 return got_error_from_errno("asprintf");
1492 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1493 if (asprintf(&gw_trans->repo_file, "%s",
1495 return got_error_from_errno("asprintf");
1497 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1498 if (asprintf(&gw_trans->repo_folder, "%s",
1500 return got_error_from_errno("asprintf");
1502 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1503 if (asprintf(&gw_trans->headref, "%s",
1505 return got_error_from_errno("asprintf");
1507 if (action == NULL) {
1508 error = got_error_from_errno("invalid action");
1511 if ((gw_trans->gw_dir =
1512 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1513 return got_error_from_errno("gw_dir malloc");
1515 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1519 gw_trans->action = GW_INDEX;
1521 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1522 gw_trans->page = p->parsed.i;
1527 static struct gw_dir *
1528 gw_init_gw_dir(char *dir)
1530 struct gw_dir *gw_dir;
1532 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1535 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1541 static const struct got_error *
1542 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1546 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1547 if (kerr != KCGI_OK)
1548 return gw_kcgi_error(kerr);
1549 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1551 if (kerr != KCGI_OK)
1552 return gw_kcgi_error(kerr);
1553 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1555 if (kerr != KCGI_OK)
1556 return gw_kcgi_error(kerr);
1557 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1559 if (kerr != KCGI_OK)
1560 return gw_kcgi_error(kerr);
1561 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1562 if (kerr != KCGI_OK)
1563 return gw_kcgi_error(kerr);
1564 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1566 if (kerr != KCGI_OK)
1567 return gw_kcgi_error(kerr);
1569 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1570 kerr = khttp_head(gw_trans->gw_req,
1571 kresps[KRESP_CONTENT_DISPOSITION],
1572 "attachment; filename=%s", gw_trans->repo_file);
1573 if (kerr != KCGI_OK)
1574 return gw_kcgi_error(kerr);
1577 kerr = khttp_body(gw_trans->gw_req);
1578 return gw_kcgi_error(kerr);
1581 static const struct got_error *
1582 gw_display_index(struct gw_trans *gw_trans)
1584 const struct got_error *error;
1587 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1591 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1592 if (kerr != KCGI_OK)
1593 return gw_kcgi_error(kerr);
1595 if (gw_trans->action != GW_BLOB) {
1596 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1597 gw_query_funcs[gw_trans->action].template);
1598 if (kerr != KCGI_OK) {
1599 khtml_close(gw_trans->gw_html_req);
1600 return gw_kcgi_error(kerr);
1604 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1608 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1610 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1613 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1615 khtml_puts(gw_trans->gw_html_req, err->msg);
1616 khtml_close(gw_trans->gw_html_req);
1620 gw_template(size_t key, void *arg)
1622 const struct got_error *error = NULL;
1624 struct gw_trans *gw_trans = arg;
1625 char *gw_site_link, *img_src = NULL;
1629 kerr = khttp_puts(gw_trans->gw_req, head);
1630 if (kerr != KCGI_OK)
1634 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1635 KATTR_ID, "got_link", KATTR__MAX);
1636 if (kerr != KCGI_OK)
1638 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1639 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1640 KATTR_TARGET, "_sotd", KATTR__MAX);
1641 if (kerr != KCGI_OK)
1643 if (asprintf(&img_src, "/%s",
1644 gw_trans->gw_conf->got_logo) == -1)
1646 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1647 KATTR_SRC, img_src, KATTR__MAX);
1648 if (kerr != KCGI_OK) {
1652 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1653 if (kerr != KCGI_OK) {
1658 case (TEMPL_SITEPATH):
1659 gw_site_link = gw_get_site_link(gw_trans);
1660 if (gw_site_link != NULL) {
1661 kerr = khttp_puts(gw_trans->gw_req, gw_site_link);
1662 if (kerr != KCGI_OK) {
1670 if (gw_trans->gw_conf->got_site_name != NULL) {
1671 kerr = khtml_puts(gw_trans->gw_html_req,
1672 gw_trans->gw_conf->got_site_name);
1673 if (kerr != KCGI_OK)
1677 case (TEMPL_SEARCH):
1678 kerr = khttp_puts(gw_trans->gw_req, search);
1679 if (kerr != KCGI_OK)
1682 case(TEMPL_SITEOWNER):
1683 if (gw_trans->gw_conf->got_site_owner != NULL &&
1684 gw_trans->gw_conf->got_show_site_owner) {
1685 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1686 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1687 if (kerr != KCGI_OK)
1689 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1690 KATTR_ID, "site_owner", KATTR__MAX);
1691 if (kerr != KCGI_OK)
1693 kerr = khtml_puts(gw_trans->gw_html_req,
1694 gw_trans->gw_conf->got_site_owner);
1695 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1696 if (kerr != KCGI_OK)
1700 case(TEMPL_CONTENT):
1701 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1703 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1704 KATTR_ID, "tmpl_err", KATTR__MAX);
1705 if (kerr != KCGI_OK)
1707 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1708 if (kerr != KCGI_OK)
1710 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1711 if (kerr != KCGI_OK)
1713 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1714 if (kerr != KCGI_OK)
1724 static const struct got_error *
1725 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1727 const struct got_error *error = NULL;
1728 char *ref_str = NULL;
1729 enum kcgi_err kerr = KCGI_OK;
1731 if (strcmp(str2, "") != 0) {
1732 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1733 error = got_error_from_errno("asprintf");
1737 ref_str = strdup("");
1739 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1740 KATTR_ID, "header_commit_title", KATTR__MAX);
1741 if (kerr != KCGI_OK)
1743 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
1744 if (kerr != KCGI_OK)
1746 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1747 if (kerr != KCGI_OK)
1749 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1750 KATTR_ID, "header_commit", KATTR__MAX);
1751 if (kerr != KCGI_OK)
1753 kerr = khtml_puts(gw_trans->gw_html_req, str1);
1754 if (kerr != KCGI_OK)
1756 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1757 if (kerr != KCGI_OK)
1759 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
1760 if (kerr != KCGI_OK)
1762 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1764 if (error == NULL && kerr != KCGI_OK)
1765 error = gw_kcgi_error(kerr);
1769 static const struct got_error *
1770 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
1772 const struct got_error *error = NULL;
1773 enum kcgi_err kerr = KCGI_OK;
1775 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1776 KATTR_ID, "header_diff_title", KATTR__MAX);
1777 if (kerr != KCGI_OK)
1779 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
1780 if (kerr != KCGI_OK)
1782 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1783 if (kerr != KCGI_OK)
1785 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1786 KATTR_ID, "header_diff", KATTR__MAX);
1787 if (kerr != KCGI_OK)
1789 kerr = khtml_puts(gw_trans->gw_html_req, str1);
1790 if (kerr != KCGI_OK)
1792 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
1793 if (kerr != KCGI_OK)
1795 kerr = khtml_puts(gw_trans->gw_html_req, str2);
1796 if (kerr != KCGI_OK)
1798 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1800 if (error == NULL && kerr != KCGI_OK)
1801 error = gw_kcgi_error(kerr);
1805 static const struct got_error *
1806 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
1808 const struct got_error *error = NULL;
1809 enum kcgi_err kerr = KCGI_OK;
1811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1812 KATTR_ID, "header_age_title", KATTR__MAX);
1813 if (kerr != KCGI_OK)
1815 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
1816 if (kerr != KCGI_OK)
1818 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1819 if (kerr != KCGI_OK)
1821 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1822 KATTR_ID, "header_age", KATTR__MAX);
1823 if (kerr != KCGI_OK)
1825 kerr = khtml_puts(gw_trans->gw_html_req, str);
1826 if (kerr != KCGI_OK)
1828 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1830 if (error == NULL && kerr != KCGI_OK)
1831 error = gw_kcgi_error(kerr);
1835 static const struct got_error *
1836 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
1838 const struct got_error *error = NULL;
1841 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1842 KATTR_ID, "header_author_title", KATTR__MAX);
1843 if (kerr != KCGI_OK)
1845 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
1846 if (kerr != KCGI_OK)
1848 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1849 if (kerr != KCGI_OK)
1851 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1852 KATTR_ID, "header_author", KATTR__MAX);
1853 if (kerr != KCGI_OK)
1855 kerr = khtml_puts(gw_trans->gw_html_req, str);
1856 if (kerr != KCGI_OK)
1858 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1860 if (error == NULL && kerr != KCGI_OK)
1861 error = gw_kcgi_error(kerr);
1865 static const struct got_error *
1866 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
1868 const struct got_error *error = NULL;
1871 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1872 KATTR_ID, "header_committer_title", KATTR__MAX);
1873 if (kerr != KCGI_OK)
1875 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
1876 if (kerr != KCGI_OK)
1878 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1879 if (kerr != KCGI_OK)
1881 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1882 KATTR_ID, "header_committer", KATTR__MAX);
1883 if (kerr != KCGI_OK)
1885 kerr = khtml_puts(gw_trans->gw_html_req, str);
1886 if (kerr != KCGI_OK)
1888 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1890 if (error == NULL && kerr != KCGI_OK)
1891 error = gw_kcgi_error(kerr);
1895 static const struct got_error *
1896 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
1898 const struct got_error *error = NULL;
1901 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1902 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
1903 if (kerr != KCGI_OK)
1905 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
1906 if (kerr != KCGI_OK)
1908 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1909 if (kerr != KCGI_OK)
1911 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1912 KATTR_ID, "header_commit_msg", KATTR__MAX);
1913 if (kerr != KCGI_OK)
1915 kerr = khttp_puts(gw_trans->gw_req, str);
1916 if (kerr != KCGI_OK)
1918 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1920 if (error == NULL && kerr != KCGI_OK)
1921 error = gw_kcgi_error(kerr);
1925 static const struct got_error *
1926 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
1928 const struct got_error *error = NULL;
1931 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1932 KATTR_ID, "header_tree_title", KATTR__MAX);
1933 if (kerr != KCGI_OK)
1935 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
1936 if (kerr != KCGI_OK)
1938 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1939 if (kerr != KCGI_OK)
1941 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1942 KATTR_ID, "header_tree", KATTR__MAX);
1943 if (kerr != KCGI_OK)
1945 kerr = khtml_puts(gw_trans->gw_html_req, str);
1946 if (kerr != KCGI_OK)
1948 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1950 if (error == NULL && kerr != KCGI_OK)
1951 error = gw_kcgi_error(kerr);
1955 static const struct got_error *
1956 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
1959 const struct got_error *error = NULL;
1961 char *d_file = NULL;
1965 *description = NULL;
1966 if (gw_trans->gw_conf->got_show_repo_description == 0)
1967 return gw_empty_string(description);
1969 if (asprintf(&d_file, "%s/description", dir) == -1)
1970 return got_error_from_errno("asprintf");
1972 f = fopen(d_file, "r");
1974 if (errno == ENOENT || errno == EACCES)
1975 return gw_empty_string(description);
1976 error = got_error_from_errno2("fopen", d_file);
1980 if (fseek(f, 0, SEEK_END) == -1) {
1981 error = got_ferror(f, GOT_ERR_IO);
1986 error = got_ferror(f, GOT_ERR_IO);
1989 if (fseek(f, 0, SEEK_SET) == -1) {
1990 error = got_ferror(f, GOT_ERR_IO);
1993 *description = calloc(len + 1, sizeof(**description));
1994 if (*description == NULL) {
1995 error = got_error_from_errno("calloc");
1999 n = fread(*description, 1, len, f);
2000 if (n == 0 && ferror(f))
2001 error = got_ferror(f, GOT_ERR_IO);
2003 if (f != NULL && fclose(f) == -1 && error == NULL)
2004 error = got_error_from_errno("fclose");
2009 static const struct got_error *
2010 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2014 char *years = "years ago", *months = "months ago";
2015 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2016 char *minutes = "minutes ago", *seconds = "seconds ago";
2017 char *now = "right now";
2025 diff_time = time(NULL) - committer_time;
2026 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2027 if (asprintf(repo_age, "%lld %s",
2028 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2029 return got_error_from_errno("asprintf");
2030 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2031 if (asprintf(repo_age, "%lld %s",
2032 (diff_time / 60 / 60 / 24 / (365 / 12)),
2034 return got_error_from_errno("asprintf");
2035 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2036 if (asprintf(repo_age, "%lld %s",
2037 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2038 return got_error_from_errno("asprintf");
2039 } else if (diff_time > 60 * 60 * 24 * 2) {
2040 if (asprintf(repo_age, "%lld %s",
2041 (diff_time / 60 / 60 / 24), days) == -1)
2042 return got_error_from_errno("asprintf");
2043 } else if (diff_time > 60 * 60 * 2) {
2044 if (asprintf(repo_age, "%lld %s",
2045 (diff_time / 60 / 60), hours) == -1)
2046 return got_error_from_errno("asprintf");
2047 } else if (diff_time > 60 * 2) {
2048 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2050 return got_error_from_errno("asprintf");
2051 } else if (diff_time > 2) {
2052 if (asprintf(repo_age, "%lld %s", diff_time,
2054 return got_error_from_errno("asprintf");
2056 if (asprintf(repo_age, "%s", now) == -1)
2057 return got_error_from_errno("asprintf");
2061 if (gmtime_r(&committer_time, &tm) == NULL)
2062 return got_error_from_errno("gmtime_r");
2064 s = asctime_r(&tm, datebuf);
2066 return got_error_from_errno("asctime_r");
2068 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2069 return got_error_from_errno("asprintf");
2075 static const struct got_error *
2076 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2077 char *repo_ref, int ref_tm)
2079 const struct got_error *error = NULL;
2080 struct got_object_id *id = NULL;
2081 struct got_repository *repo = NULL;
2082 struct got_commit_object *commit = NULL;
2083 struct got_reflist_head refs;
2084 struct got_reflist_entry *re;
2085 struct got_reference *head_ref;
2087 time_t committer_time = 0, cmp_time = 0;
2088 const char *refname;
2091 SIMPLEQ_INIT(&refs);
2093 if (repo_ref == NULL)
2096 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2099 if (gw_trans->gw_conf->got_show_repo_age == 0)
2102 error = got_repo_open(&repo, dir, NULL);
2107 error = got_ref_list(&refs, repo, "refs/heads",
2108 got_ref_cmp_by_name, NULL);
2110 error = got_ref_list(&refs, repo, repo_ref,
2111 got_ref_cmp_by_name, NULL);
2115 SIMPLEQ_FOREACH(re, &refs, entry) {
2117 refname = strdup(repo_ref);
2119 refname = got_ref_get_name(re->ref);
2120 error = got_ref_open(&head_ref, repo, refname, 0);
2124 error = got_ref_resolve(&id, repo, head_ref);
2125 got_ref_close(head_ref);
2129 error = got_object_open_as_commit(&commit, repo, id);
2134 got_object_commit_get_committer_time(commit);
2136 if (cmp_time < committer_time)
2137 cmp_time = committer_time;
2140 if (cmp_time != 0) {
2141 committer_time = cmp_time;
2142 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2145 got_ref_list_free(&refs);
2150 static const struct got_error *
2151 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2153 const struct got_error *error;
2155 struct got_object_id *id1 = NULL, *id2 = NULL;
2156 char *label1 = NULL, *label2 = NULL, *line = NULL;
2158 size_t linesize = 0;
2160 enum kcgi_err kerr = KCGI_OK;
2166 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2170 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2171 error = got_repo_match_object_id(&id1, &label1,
2172 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2177 error = got_repo_match_object_id(&id2, &label2,
2178 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2182 error = got_object_get_type(&obj_type, header->repo, id2);
2186 case GOT_OBJ_TYPE_BLOB:
2187 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2190 case GOT_OBJ_TYPE_TREE:
2191 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2194 case GOT_OBJ_TYPE_COMMIT:
2195 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2199 error = got_error(GOT_ERR_OBJ_TYPE);
2204 if (fseek(f, 0, SEEK_SET) == -1) {
2205 error = got_ferror(f, GOT_ERR_IO);
2209 while ((linelen = getline(&line, &linesize, f)) != -1) {
2210 error = gw_colordiff_line(gw_trans, line);
2213 /* XXX: KHTML_PRETTY breaks this */
2214 kerr = khtml_puts(gw_trans->gw_html_req, line);
2215 if (kerr != KCGI_OK)
2217 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2218 if (kerr != KCGI_OK)
2221 if (linelen == -1 && ferror(f))
2222 error = got_error_from_errno("getline");
2224 if (f && fclose(f) == -1 && error == NULL)
2225 error = got_error_from_errno("fclose");
2232 if (error == NULL && kerr != KCGI_OK)
2233 error = gw_kcgi_error(kerr);
2237 static const struct got_error *
2238 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2240 const struct got_error *error = NULL;
2241 struct got_repository *repo;
2242 const char *gitconfig_owner;
2246 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2249 error = got_repo_open(&repo, dir, NULL);
2252 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2253 if (gitconfig_owner) {
2254 *owner = strdup(gitconfig_owner);
2256 error = got_error_from_errno("strdup");
2258 got_repo_close(repo);
2262 static const struct got_error *
2263 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2265 const struct got_error *error = NULL;
2267 char *d_file = NULL;
2273 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2274 return got_error_from_errno("asprintf");
2276 f = fopen(d_file, "r");
2278 if (errno != ENOENT && errno != EACCES)
2279 error = got_error_from_errno2("fopen", d_file);
2283 if (fseek(f, 0, SEEK_END) == -1) {
2284 error = got_ferror(f, GOT_ERR_IO);
2289 error = got_ferror(f, GOT_ERR_IO);
2292 if (fseek(f, 0, SEEK_SET) == -1) {
2293 error = got_ferror(f, GOT_ERR_IO);
2297 *url = calloc(len + 1, sizeof(**url));
2299 error = got_error_from_errno("calloc");
2303 n = fread(*url, 1, len, f);
2304 if (n == 0 && ferror(f))
2305 error = got_ferror(f, GOT_ERR_IO);
2307 if (f && fclose(f) == -1 && error == NULL)
2308 error = got_error_from_errno("fclose");
2313 static const struct got_error *
2314 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2315 int limit, int tag_type)
2317 const struct got_error *error = NULL;
2318 struct got_repository *repo = NULL;
2319 struct got_reflist_head refs;
2320 struct got_reflist_entry *re;
2322 char *id_str = NULL, *refstr = NULL, *newline, *href_commits = NULL;
2323 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2324 struct got_tag_object *tag = NULL;
2325 enum kcgi_err kerr = KCGI_OK;
2326 int summary_header_displayed = 0;
2328 SIMPLEQ_INIT(&refs);
2330 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2334 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2338 SIMPLEQ_FOREACH(re, &refs, entry) {
2339 const char *refname;
2341 const char *tag_commit;
2343 struct got_object_id *id;
2345 refname = got_ref_get_name(re->ref);
2346 if (strncmp(refname, "refs/tags/", 10) != 0)
2349 refstr = got_ref_to_str(re->ref);
2350 if (refstr == NULL) {
2351 error = got_error_from_errno("got_ref_to_str");
2355 error = got_ref_resolve(&id, repo, re->ref);
2360 * XXX: some of my repos are failing here. need to investigate.
2361 * currently setting error to NULL so no error is returned,
2362 * which stops Heads from being displayed on gw_summary.
2364 * got ref -l lists refs and first tag ref above can be
2367 * got tag -l will list tags just fine, so I don't know what
2370 error = got_object_open_as_tag(&tag, repo, id);
2377 tagger = got_object_tag_get_tagger(tag);
2378 tagger_time = got_object_tag_get_tagger_time(tag);
2380 error = got_object_id_str(&id_str,
2381 got_object_tag_get_object_id(tag));
2385 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2386 strlen(id_str)) != 0)
2389 tag_commit0 = strdup(got_object_tag_get_message(tag));
2390 if (tag_commit0 == NULL) {
2391 error = got_error_from_errno("strdup");
2395 tag_commit = tag_commit0;
2396 while (*tag_commit == '\n')
2401 newline = strchr(tag_commit, '\n');
2405 if (summary_header_displayed == 0) {
2406 kerr = khtml_attr(gw_trans->gw_html_req,
2407 KELEM_DIV, KATTR_ID,
2408 "summary_tags_title_wrapper", KATTR__MAX);
2409 if (kerr != KCGI_OK)
2411 kerr = khtml_attr(gw_trans->gw_html_req,
2412 KELEM_DIV, KATTR_ID,
2413 "summary_tags_title", KATTR__MAX);
2414 if (kerr != KCGI_OK)
2416 kerr = khtml_puts(gw_trans->gw_html_req,
2418 if (kerr != KCGI_OK)
2420 kerr = khtml_closeelem(gw_trans->gw_html_req,
2422 if (kerr != KCGI_OK)
2424 kerr = khtml_attr(gw_trans->gw_html_req,
2425 KELEM_DIV, KATTR_ID,
2426 "summary_tags_content", KATTR__MAX);
2427 if (kerr != KCGI_OK)
2429 summary_header_displayed = 1;
2432 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2433 KATTR_ID, "tags_wrapper", KATTR__MAX);
2434 if (kerr != KCGI_OK)
2436 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2437 KATTR_ID, "tags_age", KATTR__MAX);
2438 if (kerr != KCGI_OK)
2440 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2443 kerr = khtml_puts(gw_trans->gw_html_req,
2445 if (kerr != KCGI_OK)
2447 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2448 if (kerr != KCGI_OK)
2450 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2451 KATTR_ID, "tags", KATTR__MAX);
2452 if (kerr != KCGI_OK)
2454 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2455 if (kerr != KCGI_OK)
2457 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2458 if (kerr != KCGI_OK)
2460 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2461 KATTR_ID, "tags_name", KATTR__MAX);
2462 if (kerr != KCGI_OK)
2464 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2465 gw_trans->repo_name, id_str) == -1) {
2466 error = got_error_from_errno("asprintf");
2469 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2470 KATTR_HREF, href_tag, KATTR__MAX);
2471 if (kerr != KCGI_OK)
2473 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2474 if (kerr != KCGI_OK)
2476 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2477 if (kerr != KCGI_OK)
2480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2481 KATTR_ID, "navs_wrapper", KATTR__MAX);
2482 if (kerr != KCGI_OK)
2484 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2485 KATTR_ID, "navs", KATTR__MAX);
2486 if (kerr != KCGI_OK)
2489 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2490 KATTR_HREF, href_tag, KATTR__MAX);
2491 if (kerr != KCGI_OK)
2493 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2494 if (kerr != KCGI_OK)
2496 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2497 if (kerr != KCGI_OK)
2500 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2501 if (kerr != KCGI_OK)
2504 if (asprintf(&href_briefs,
2505 "?path=%s&action=briefs&commit=%s",
2506 gw_trans->repo_name, id_str) == -1) {
2507 error = got_error_from_errno("asprintf");
2510 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2511 KATTR_HREF, href_briefs, KATTR__MAX);
2512 if (kerr != KCGI_OK)
2514 kerr = khtml_puts(gw_trans->gw_html_req,
2516 if (kerr != KCGI_OK)
2518 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2519 if (kerr != KCGI_OK)
2522 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2523 if (kerr != KCGI_OK)
2526 if (asprintf(&href_commits,
2527 "?path=%s&action=commits&commit=%s",
2528 gw_trans->repo_name, id_str) == -1) {
2529 error = got_error_from_errno("asprintf");
2532 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2533 KATTR_HREF, href_commits, KATTR__MAX);
2534 if (kerr != KCGI_OK)
2536 kerr = khtml_puts(gw_trans->gw_html_req,
2538 if (kerr != KCGI_OK)
2540 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2541 if (kerr != KCGI_OK)
2544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2545 KATTR_ID, "dotted_line", KATTR__MAX);
2546 if (kerr != KCGI_OK)
2548 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2549 if (kerr != KCGI_OK)
2553 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2554 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2555 if (kerr != KCGI_OK)
2557 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2558 if (kerr != KCGI_OK)
2560 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2561 if (kerr != KCGI_OK)
2563 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2564 KATTR_ID, "tag_info_date", KATTR__MAX);
2565 if (kerr != KCGI_OK)
2567 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2570 kerr = khtml_puts(gw_trans->gw_html_req,
2572 if (kerr != KCGI_OK)
2574 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2575 if (kerr != KCGI_OK)
2578 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2579 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2580 if (kerr != KCGI_OK)
2582 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2583 if (kerr != KCGI_OK)
2585 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2586 if (kerr != KCGI_OK)
2588 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2589 KATTR_ID, "tag_info_date", KATTR__MAX);
2590 if (kerr != KCGI_OK)
2592 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2593 if (kerr != KCGI_OK)
2595 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2596 if (kerr != KCGI_OK)
2599 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2600 KATTR_ID, "tag_info", KATTR__MAX);
2601 if (kerr != KCGI_OK)
2603 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2604 if (kerr != KCGI_OK)
2610 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2611 if (kerr != KCGI_OK)
2614 if (limit && --limit == 0)
2617 got_object_tag_close(tag);
2632 href_commits = NULL;
2636 got_object_tag_close(tag);
2644 got_ref_list_free(&refs);
2646 got_repo_close(repo);
2647 if (error == NULL && kerr != KCGI_OK)
2648 error = gw_kcgi_error(kerr);
2653 gw_free_headers(struct gw_header *header)
2657 if (header->commit != NULL)
2658 got_object_commit_close(header->commit);
2660 got_repo_close(header->repo);
2661 free(header->refs_str);
2662 free(header->commit_id);
2663 free(header->parent_id);
2664 free(header->tree_id);
2665 free(header->commit_msg);
2668 static struct gw_header *
2671 struct gw_header *header;
2673 header = malloc(sizeof(*header));
2677 header->repo = NULL;
2678 header->commit = NULL;
2680 header->path = NULL;
2681 SIMPLEQ_INIT(&header->refs);
2686 static const struct got_error *
2687 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2690 const struct got_error *error = NULL;
2691 struct got_commit_graph *graph = NULL;
2693 error = got_commit_graph_open(&graph, header->path, 0);
2697 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2703 error = got_commit_graph_iter_next(&header->id, graph,
2704 header->repo, NULL, NULL);
2706 if (error->code == GOT_ERR_ITER_COMPLETED)
2710 if (header->id == NULL)
2713 error = got_object_open_as_commit(&header->commit, header->repo,
2718 error = gw_get_commit(gw_trans, header);
2720 struct gw_header *n_header = NULL;
2721 if ((n_header = gw_init_header()) == NULL) {
2722 error = got_error_from_errno("malloc");
2726 n_header->refs_str = strdup(header->refs_str);
2727 n_header->commit_id = strdup(header->commit_id);
2728 n_header->parent_id = strdup(header->parent_id);
2729 n_header->tree_id = strdup(header->tree_id);
2730 n_header->author = strdup(header->author);
2731 n_header->committer = strdup(header->committer);
2732 n_header->commit_msg = strdup(header->commit_msg);
2733 n_header->committer_time = header->committer_time;
2734 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2737 if (error || (limit && --limit == 0))
2742 got_commit_graph_close(graph);
2746 static const struct got_error *
2747 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2749 const struct got_error *error = NULL;
2750 struct got_reflist_entry *re;
2751 struct got_object_id *id2 = NULL;
2752 struct got_object_qid *parent_id;
2753 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2756 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2759 struct got_tag_object *tag = NULL;
2762 name = got_ref_get_name(re->ref);
2763 if (strcmp(name, GOT_REF_HEAD) == 0)
2765 if (strncmp(name, "refs/", 5) == 0)
2767 if (strncmp(name, "got/", 4) == 0)
2769 if (strncmp(name, "heads/", 6) == 0)
2771 if (strncmp(name, "remotes/", 8) == 0)
2773 if (strncmp(name, "tags/", 5) == 0) {
2774 error = got_object_open_as_tag(&tag, header->repo,
2777 if (error->code != GOT_ERR_OBJ_TYPE)
2780 * Ref points at something other
2787 cmp = got_object_id_cmp(tag ?
2788 got_object_tag_get_object_id(tag) : re->id, header->id);
2790 got_object_tag_close(tag);
2794 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2795 s ? ", " : "", name) == -1) {
2796 error = got_error_from_errno("asprintf");
2800 header->refs_str = strdup(refs_str);
2804 if (refs_str == NULL)
2805 header->refs_str = strdup("");
2808 error = got_object_id_str(&header->commit_id, header->id);
2812 error = got_object_id_str(&header->tree_id,
2813 got_object_commit_get_tree_id(header->commit));
2817 if (gw_trans->action == GW_DIFF) {
2818 parent_id = SIMPLEQ_FIRST(
2819 got_object_commit_get_parent_ids(header->commit));
2820 if (parent_id != NULL) {
2821 id2 = got_object_id_dup(parent_id->id);
2823 error = got_object_id_str(&header->parent_id, id2);
2828 header->parent_id = strdup("/dev/null");
2830 header->parent_id = strdup("");
2832 header->committer_time =
2833 got_object_commit_get_committer_time(header->commit);
2836 got_object_commit_get_author(header->commit);
2838 got_object_commit_get_committer(header->commit);
2841 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2845 commit_msg = commit_msg0;
2846 while (*commit_msg == '\n')
2849 header->commit_msg = strdup(commit_msg);
2854 static const struct got_error *
2855 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2857 const struct got_error *error = NULL;
2858 char *in_repo_path = NULL;
2860 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2864 if (gw_trans->commit == NULL) {
2865 struct got_reference *head_ref;
2866 error = got_ref_open(&head_ref, header->repo,
2867 gw_trans->headref, 0);
2871 error = got_ref_resolve(&header->id, header->repo, head_ref);
2872 got_ref_close(head_ref);
2876 error = got_object_open_as_commit(&header->commit,
2877 header->repo, header->id);
2879 struct got_reference *ref;
2880 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2881 if (error == NULL) {
2883 error = got_ref_resolve(&header->id, header->repo, ref);
2887 error = got_object_get_type(&obj_type, header->repo,
2891 if (obj_type == GOT_OBJ_TYPE_TAG) {
2892 struct got_tag_object *tag;
2893 error = got_object_open_as_tag(&tag,
2894 header->repo, header->id);
2897 if (got_object_tag_get_object_type(tag) !=
2898 GOT_OBJ_TYPE_COMMIT) {
2899 got_object_tag_close(tag);
2900 error = got_error(GOT_ERR_OBJ_TYPE);
2904 header->id = got_object_id_dup(
2905 got_object_tag_get_object_id(tag));
2906 if (header->id == NULL)
2907 error = got_error_from_errno(
2908 "got_object_id_dup");
2909 got_object_tag_close(tag);
2912 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2913 error = got_error(GOT_ERR_OBJ_TYPE);
2916 error = got_object_open_as_commit(&header->commit,
2917 header->repo, header->id);
2921 if (header->commit == NULL) {
2922 error = got_repo_match_object_id_prefix(&header->id,
2923 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2928 error = got_repo_match_object_id_prefix(&header->id,
2929 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2933 error = got_repo_map_path(&in_repo_path, header->repo,
2934 gw_trans->repo_path, 1);
2939 header->path = strdup(in_repo_path);
2943 error = got_ref_list(&header->refs, header->repo, NULL,
2944 got_ref_cmp_by_name, NULL);
2948 error = gw_get_commits(gw_trans, header, limit);
2956 char datebuf[11]; /* YYYY-MM-DD + NUL */
2959 struct gw_blame_cb_args {
2960 struct blame_line *lines;
2964 off_t *line_offsets;
2966 struct got_repository *repo;
2967 struct gw_trans *gw_trans;
2970 static const struct got_error *
2971 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2973 const struct got_error *err = NULL;
2974 struct gw_blame_cb_args *a = arg;
2975 struct blame_line *bline;
2977 size_t linesize = 0;
2978 struct got_commit_object *commit = NULL;
2981 time_t committer_time;
2982 enum kcgi_err kerr = KCGI_OK;
2984 if (nlines != a->nlines ||
2985 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2986 return got_error(GOT_ERR_RANGE);
2989 return NULL; /* no change in this commit */
2991 /* Annotate this line. */
2992 bline = &a->lines[lineno - 1];
2993 if (bline->annotated)
2995 err = got_object_id_str(&bline->id_str, id);
2999 err = got_object_open_as_commit(&commit, a->repo, id);
3003 bline->committer = strdup(got_object_commit_get_committer(commit));
3004 if (bline->committer == NULL) {
3005 err = got_error_from_errno("strdup");
3009 committer_time = got_object_commit_get_committer_time(commit);
3010 if (localtime_r(&committer_time, &tm) == NULL)
3011 return got_error_from_errno("localtime_r");
3012 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3013 &tm) >= sizeof(bline->datebuf)) {
3014 err = got_error(GOT_ERR_NO_SPACE);
3017 bline->annotated = 1;
3019 /* Print lines annotated so far. */
3020 bline = &a->lines[a->lineno_cur - 1];
3021 if (!bline->annotated)
3024 offset = a->line_offsets[a->lineno_cur - 1];
3025 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3026 err = got_error_from_errno("fseeko");
3030 while (bline->annotated) {
3031 char *smallerthan, *at, *nl, *committer;
3032 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3035 if (getline(&line, &linesize, a->f) == -1) {
3037 err = got_error_from_errno("getline");
3041 committer = bline->committer;
3042 smallerthan = strchr(committer, '<');
3043 if (smallerthan && smallerthan[1] != '\0')
3044 committer = smallerthan + 1;
3045 at = strchr(committer, '@');
3048 len = strlen(committer);
3050 committer[8] = '\0';
3052 nl = strchr(line, '\n');
3056 if (a->gw_trans->repo_folder == NULL)
3057 a->gw_trans->repo_folder = strdup("");
3058 if (a->gw_trans->repo_folder == NULL)
3062 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3063 "blame_wrapper", KATTR__MAX);
3064 if (kerr != KCGI_OK)
3066 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3067 "blame_number", KATTR__MAX);
3068 if (kerr != KCGI_OK)
3070 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3071 a->lineno_cur) == -1)
3073 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3074 if (kerr != KCGI_OK)
3076 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3077 if (kerr != KCGI_OK)
3080 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3081 "blame_hash", KATTR__MAX);
3082 if (kerr != KCGI_OK)
3084 if (asprintf(&href_diff,
3085 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3086 a->gw_trans->repo_name, bline->id_str,
3087 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3088 err = got_error_from_errno("asprintf");
3091 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3092 err = got_error_from_errno("asprintf");
3095 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3096 KATTR_HREF, href_diff, KATTR__MAX);
3097 if (kerr != KCGI_OK)
3099 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3100 if (kerr != KCGI_OK)
3102 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3103 if (kerr != KCGI_OK)
3106 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3107 "blame_date", KATTR__MAX);
3108 if (kerr != KCGI_OK)
3110 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3111 if (kerr != KCGI_OK)
3113 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3114 if (kerr != KCGI_OK)
3117 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3118 "blame_author", KATTR__MAX);
3119 if (kerr != KCGI_OK)
3121 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
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_code", KATTR__MAX);
3130 if (kerr != KCGI_OK)
3132 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3133 if (kerr != KCGI_OK)
3135 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3136 if (kerr != KCGI_OK)
3139 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3140 if (kerr != KCGI_OK)
3144 bline = &a->lines[a->lineno_cur - 1];
3152 got_object_commit_close(commit);
3154 if (err == NULL && kerr != KCGI_OK)
3155 err = gw_kcgi_error(kerr);
3159 static const struct got_error *
3160 gw_output_file_blame(struct gw_trans *gw_trans)
3162 const struct got_error *error = NULL;
3163 struct got_repository *repo = NULL;
3164 struct got_object_id *obj_id = NULL;
3165 struct got_object_id *commit_id = NULL;
3166 struct got_blob_object *blob = NULL;
3167 char *path = NULL, *in_repo_path = NULL;
3168 struct gw_blame_cb_args bca;
3172 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3176 if (asprintf(&path, "%s%s%s",
3177 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3178 gw_trans->repo_folder ? "/" : "",
3179 gw_trans->repo_file) == -1) {
3180 error = got_error_from_errno("asprintf");
3184 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3188 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3189 GOT_OBJ_TYPE_COMMIT, 1, repo);
3193 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3197 if (obj_id == NULL) {
3198 error = got_error(GOT_ERR_NO_OBJ);
3202 error = got_object_get_type(&obj_type, repo, obj_id);
3206 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3207 error = got_error(GOT_ERR_OBJ_TYPE);
3211 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3215 bca.f = got_opentemp();
3216 if (bca.f == NULL) {
3217 error = got_error_from_errno("got_opentemp");
3220 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3221 &bca.line_offsets, bca.f, blob);
3222 if (error || bca.nlines == 0)
3225 /* Don't include \n at EOF in the blame line count. */
3226 if (bca.line_offsets[bca.nlines - 1] == filesize)
3229 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3230 if (bca.lines == NULL) {
3231 error = got_error_from_errno("calloc");
3235 bca.nlines_prec = 0;
3242 bca.gw_trans = gw_trans;
3244 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3249 free(bca.line_offsets);
3255 for (i = 0; i < bca.nlines; i++) {
3256 struct blame_line *bline = &bca.lines[i];
3257 free(bline->id_str);
3258 free(bline->committer);
3261 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3262 error = got_error_from_errno("fclose");
3264 got_object_blob_close(blob);
3266 got_repo_close(repo);
3270 static const struct got_error *
3271 gw_output_blob_buf(struct gw_trans *gw_trans)
3273 const struct got_error *error = NULL;
3274 struct got_repository *repo = NULL;
3275 struct got_object_id *obj_id = NULL;
3276 struct got_object_id *commit_id = NULL;
3277 struct got_blob_object *blob = NULL;
3278 char *path = NULL, *in_repo_path = NULL;
3279 int obj_type, set_mime = 0;
3282 enum kcgi_err kerr = KCGI_OK;
3284 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3288 if (asprintf(&path, "%s%s%s",
3289 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3290 gw_trans->repo_folder ? "/" : "",
3291 gw_trans->repo_file) == -1) {
3292 error = got_error_from_errno("asprintf");
3296 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3300 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3301 GOT_OBJ_TYPE_COMMIT, 1, repo);
3305 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3309 if (obj_id == NULL) {
3310 error = got_error(GOT_ERR_NO_OBJ);
3314 error = got_object_get_type(&obj_type, repo, obj_id);
3318 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3319 error = got_error(GOT_ERR_OBJ_TYPE);
3323 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3327 hdrlen = got_object_blob_get_hdrlen(blob);
3329 error = got_object_blob_read_block(&len, blob);
3332 buf = got_object_blob_get_read_buf(blob);
3335 * Skip blob object header first time around,
3336 * which also contains a zero byte.
3339 if (set_mime == 0) {
3340 if (isbinary(buf, len - hdrlen))
3341 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3343 gw_trans->mime = KMIME_TEXT_PLAIN;
3345 error = gw_display_index(gw_trans);
3349 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3358 got_object_blob_close(blob);
3360 got_repo_close(repo);
3361 if (error == NULL && kerr != KCGI_OK)
3362 error = gw_kcgi_error(kerr);
3366 static const struct got_error *
3367 gw_output_repo_tree(struct gw_trans *gw_trans)
3369 const struct got_error *error = NULL;
3370 struct got_repository *repo = NULL;
3371 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3372 struct got_tree_object *tree = NULL;
3373 char *path = NULL, *in_repo_path = NULL;
3374 char *id_str = NULL;
3375 char *build_folder = NULL;
3376 char *href_blob = NULL, *href_blame = NULL;
3377 const char *class = NULL;
3378 int nentries, i, class_flip = 0;
3379 enum kcgi_err kerr = KCGI_OK;
3381 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3385 if (gw_trans->repo_folder != NULL)
3386 path = strdup(gw_trans->repo_folder);
3388 error = got_repo_map_path(&in_repo_path, repo,
3389 gw_trans->repo_path, 1);
3393 path = in_repo_path;
3396 if (gw_trans->commit == NULL) {
3397 struct got_reference *head_ref;
3398 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3401 error = got_ref_resolve(&commit_id, repo, head_ref);
3404 got_ref_close(head_ref);
3407 error = got_repo_match_object_id(&commit_id, NULL,
3408 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3413 error = got_object_id_str(&gw_trans->commit, commit_id);
3417 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3421 error = got_object_open_as_tree(&tree, repo, tree_id);
3425 nentries = got_object_tree_get_nentries(tree);
3426 for (i = 0; i < nentries; i++) {
3427 struct got_tree_entry *te;
3428 const char *modestr = "";
3431 te = got_object_tree_get_entry(tree, i);
3433 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3437 mode = got_tree_entry_get_mode(te);
3438 if (got_object_tree_entry_is_submodule(te))
3440 else if (S_ISLNK(mode))
3442 else if (S_ISDIR(mode))
3444 else if (mode & S_IXUSR)
3447 if (class_flip == 0) {
3448 class = "back_lightgray";
3451 class = "back_white";
3455 if (S_ISDIR(mode)) {
3456 if (asprintf(&build_folder, "%s/%s",
3457 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3458 got_tree_entry_get_name(te)) == -1) {
3459 error = got_error_from_errno(
3463 if (asprintf(&href_blob,
3464 "?path=%s&action=%s&commit=%s&folder=%s",
3465 gw_trans->repo_name, gw_trans->action_name,
3466 gw_trans->commit, build_folder) == -1) {
3467 error = got_error_from_errno("asprintf");
3471 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3472 KATTR_ID, "tree_wrapper", KATTR__MAX);
3473 if (kerr != KCGI_OK)
3475 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3476 KATTR_ID, "tree_line", KATTR_CLASS, class,
3478 if (kerr != KCGI_OK)
3480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3481 KATTR_HREF, href_blob, KATTR_CLASS,
3482 "diff_directory", KATTR__MAX);
3483 if (kerr != KCGI_OK)
3485 kerr = khtml_puts(gw_trans->gw_html_req,
3486 got_tree_entry_get_name(te));
3487 if (kerr != KCGI_OK)
3489 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3490 if (kerr != KCGI_OK)
3492 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3493 if (kerr != KCGI_OK)
3495 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3496 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3498 if (kerr != KCGI_OK)
3500 kerr = khtml_entity(gw_trans->gw_html_req,
3502 if (kerr != KCGI_OK)
3504 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3505 if (kerr != KCGI_OK)
3508 if (asprintf(&href_blob,
3509 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3510 gw_trans->repo_name, "blob", gw_trans->commit,
3511 got_tree_entry_get_name(te),
3512 gw_trans->repo_folder ?
3513 gw_trans->repo_folder : "") == -1) {
3514 error = got_error_from_errno("asprintf");
3517 if (asprintf(&href_blame,
3518 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3519 gw_trans->repo_name, "blame", gw_trans->commit,
3520 got_tree_entry_get_name(te),
3521 gw_trans->repo_folder ?
3522 gw_trans->repo_folder : "") == -1) {
3523 error = got_error_from_errno("asprintf");
3527 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3528 KATTR_ID, "tree_wrapper", KATTR__MAX);
3529 if (kerr != KCGI_OK)
3531 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3532 KATTR_ID, "tree_line", KATTR_CLASS, class,
3534 if (kerr != KCGI_OK)
3536 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3537 KATTR_HREF, href_blob, KATTR__MAX);
3538 if (kerr != KCGI_OK)
3540 kerr = khtml_puts(gw_trans->gw_html_req,
3541 got_tree_entry_get_name(te));
3542 if (kerr != KCGI_OK)
3544 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3545 if (kerr != KCGI_OK)
3547 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3548 if (kerr != KCGI_OK)
3550 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3551 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3553 if (kerr != KCGI_OK)
3556 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3557 KATTR_HREF, href_blob, KATTR__MAX);
3558 if (kerr != KCGI_OK)
3560 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3561 if (kerr != KCGI_OK)
3563 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3564 if (kerr != KCGI_OK)
3567 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3568 if (kerr != KCGI_OK)
3571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3572 KATTR_HREF, href_blame, KATTR__MAX);
3573 if (kerr != KCGI_OK)
3575 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3576 if (kerr != KCGI_OK)
3579 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3580 if (kerr != KCGI_OK)
3588 build_folder = NULL;
3592 got_object_tree_close(tree);
3594 got_repo_close(repo);
3601 if (error == NULL && kerr != KCGI_OK)
3602 error = gw_kcgi_error(kerr);
3606 static const struct got_error *
3607 gw_get_repo_heads(char **head_html, struct gw_trans *gw_trans)
3609 const struct got_error *error = NULL;
3610 struct got_repository *repo = NULL;
3611 struct got_reflist_head refs;
3612 struct got_reflist_entry *re;
3613 char *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
3614 struct buf *diffbuf = NULL;
3619 SIMPLEQ_INIT(&refs);
3621 error = buf_alloc(&diffbuf, 0);
3625 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3629 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3634 SIMPLEQ_FOREACH(re, &refs, entry) {
3637 refname = strdup(got_ref_get_name(re->ref));
3638 if (refname == NULL) {
3639 error = got_error_from_errno("got_ref_to_str");
3643 if (strncmp(refname, "refs/heads/", 11) != 0) {
3648 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3653 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
3654 refname, gw_trans->repo_name, refname,
3655 gw_trans->repo_name, refname, gw_trans->repo_name,
3657 error = got_error_from_errno("asprintf");
3661 if (strncmp(refname, "refs/heads/", 11) == 0)
3664 if (asprintf(&head_row, heads_row, age, refname,
3665 head_navs_disp) == -1) {
3666 error = got_error_from_errno("asprintf");
3670 error = buf_puts(&newsize, diffbuf, head_row);
3672 free(head_navs_disp);
3676 if (buf_len(diffbuf) > 0) {
3677 error = buf_putc(diffbuf, '\0');
3678 *head_html = strdup(buf_get(diffbuf));
3679 if (*head_html == NULL)
3680 error = got_error_from_errno("strdup");
3684 got_ref_list_free(&refs);
3686 got_repo_close(repo);
3691 gw_get_site_link(struct gw_trans *gw_trans)
3693 char *link = NULL, *repo = NULL, *action = NULL;
3695 if (gw_trans->repo_name != NULL &&
3696 asprintf(&repo, " / <a href='?path=%s&action=summary'>%s</a>",
3697 gw_trans->repo_name, gw_trans->repo_name) == -1)
3700 if (gw_trans->action_name != NULL &&
3701 asprintf(&action, " / %s", gw_trans->action_name) == -1) {
3706 if (asprintf(&link, site_link, GOTWEB,
3707 gw_trans->gw_conf->got_site_link,
3708 repo ? repo : "", action ? action : "") == -1) {
3719 static const struct got_error *
3720 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
3722 const struct got_error *error = NULL;
3724 enum kcgi_err kerr = KCGI_OK;
3726 if (strncmp(buf, "-", 1) == 0)
3727 color = "diff_minus";
3728 else if (strncmp(buf, "+", 1) == 0)
3729 color = "diff_plus";
3730 else if (strncmp(buf, "@@", 2) == 0)
3731 color = "diff_chunk_header";
3732 else if (strncmp(buf, "@@", 2) == 0)
3733 color = "diff_chunk_header";
3734 else if (strncmp(buf, "commit +", 8) == 0)
3735 color = "diff_meta";
3736 else if (strncmp(buf, "commit -", 8) == 0)
3737 color = "diff_meta";
3738 else if (strncmp(buf, "blob +", 6) == 0)
3739 color = "diff_meta";
3740 else if (strncmp(buf, "blob -", 6) == 0)
3741 color = "diff_meta";
3742 else if (strncmp(buf, "file +", 6) == 0)
3743 color = "diff_meta";
3744 else if (strncmp(buf, "file -", 6) == 0)
3745 color = "diff_meta";
3746 else if (strncmp(buf, "from:", 5) == 0)
3747 color = "diff_author";
3748 else if (strncmp(buf, "via:", 4) == 0)
3749 color = "diff_author";
3750 else if (strncmp(buf, "date:", 5) == 0)
3751 color = "diff_date";
3752 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3753 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
3754 if (error == NULL && kerr != KCGI_OK)
3755 error = gw_kcgi_error(kerr);
3760 main(int argc, char *argv[])
3762 const struct got_error *error = NULL;
3763 struct gw_trans *gw_trans;
3764 struct gw_dir *dir = NULL, *tdir;
3765 const char *page = "index";
3769 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3772 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3775 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3778 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3781 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3782 if (kerr != KCGI_OK) {
3783 error = gw_kcgi_error(kerr);
3787 if ((gw_trans->gw_conf =
3788 malloc(sizeof(struct gotweb_conf))) == NULL) {
3790 error = got_error_from_errno("malloc");
3794 TAILQ_INIT(&gw_trans->gw_dirs);
3795 TAILQ_INIT(&gw_trans->gw_headers);
3798 gw_trans->repos_total = 0;
3799 gw_trans->repo_path = NULL;
3800 gw_trans->commit = NULL;
3801 gw_trans->headref = strdup(GOT_REF_HEAD);
3802 gw_trans->mime = KMIME_TEXT_HTML;
3803 gw_trans->gw_tmpl->key = gw_templs;
3804 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3805 gw_trans->gw_tmpl->arg = gw_trans;
3806 gw_trans->gw_tmpl->cb = gw_template;
3807 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3811 error = gw_parse_querystring(gw_trans);
3815 if (gw_trans->action == GW_BLOB)
3816 error = gw_blob(gw_trans);
3818 error = gw_display_index(gw_trans);
3821 gw_trans->mime = KMIME_TEXT_PLAIN;
3822 gw_trans->action = GW_ERR;
3823 gw_display_error(gw_trans, error);
3826 free(gw_trans->gw_conf->got_repos_path);
3827 free(gw_trans->gw_conf->got_site_name);
3828 free(gw_trans->gw_conf->got_site_owner);
3829 free(gw_trans->gw_conf->got_site_link);
3830 free(gw_trans->gw_conf->got_logo);
3831 free(gw_trans->gw_conf->got_logo_url);
3832 free(gw_trans->gw_conf);
3833 free(gw_trans->commit);
3834 free(gw_trans->repo_path);
3835 free(gw_trans->repo_name);
3836 free(gw_trans->repo_file);
3837 free(gw_trans->action_name);
3838 free(gw_trans->headref);
3840 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3842 free(dir->description);
3851 khttp_free(gw_trans->gw_req);