2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
20 #include <sys/types.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct gw_dir *gw_dir;
59 struct gotweb_conf *gw_conf;
60 struct ktemplate *gw_tmpl;
61 struct khtmlreq *gw_html_req;
72 unsigned int repos_total;
77 TAILQ_ENTRY(gw_header) entry;
78 struct got_repository *repo;
79 struct got_reflist_head refs;
80 struct got_commit_object *commit;
81 struct got_object_id *id;
85 char *commit_id; /* id_str1 */
86 char *parent_id; /* id_str2 */
91 time_t committer_time;
95 TAILQ_ENTRY(gw_dir) entry;
136 static const char *const gw_templs[TEMPL__MAX] = {
146 static const struct kvalid gw_keys[KEY__ZMAX] = {
147 { kvalid_stringne, "action" },
148 { kvalid_stringne, "commit" },
149 { kvalid_stringne, "file" },
150 { kvalid_stringne, "folder" },
151 { kvalid_stringne, "headref" },
152 { kvalid_int, "page" },
153 { kvalid_stringne, "path" },
156 static const struct got_error *gw_init_gw_dir(struct gw_dir **, char *);
157 static struct gw_header *gw_init_header(void);
159 static void gw_free_headers(struct gw_header *);
160 static void gw_display_error(struct gw_trans *,
161 const struct got_error *);
163 static int gw_template(size_t, void *);
165 static const struct got_error *gw_strdup_string(char **, char *,
167 static const struct got_error *gw_get_repo_description(char **,
168 struct gw_trans *, char *);
169 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
171 static const struct got_error *gw_get_time_str(char **, time_t, int);
172 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
173 char *, char *, int);
174 static const struct got_error *gw_output_file_blame(struct gw_trans *);
175 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
176 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
177 static const struct got_error *gw_output_diff(struct gw_trans *,
179 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
180 struct gw_header *, int, int);
181 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
182 static const struct got_error *gw_output_site_link(struct gw_trans *);
183 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
185 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
187 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
189 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
191 static const struct got_error *gw_gen_author_header(struct gw_trans *,
193 static const struct got_error *gw_gen_age_header(struct gw_trans *,
195 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
197 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
199 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
200 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
202 static const struct got_error *gw_display_index(struct gw_trans *);
203 static const struct got_error *gw_get_header(struct gw_trans *,
204 struct gw_header *, int);
205 static const struct got_error *gw_get_commits(struct gw_trans *,
206 struct gw_header *, int);
207 static const struct got_error *gw_get_commit(struct gw_trans *,
209 static const struct got_error *gw_apply_unveil(const char *);
210 static const struct got_error *gw_blame_cb(void *, int, int,
211 struct got_object_id *);
212 static const struct got_error *gw_load_got_paths(struct gw_trans *);
213 static const struct got_error *gw_load_got_path(struct gw_trans *,
215 static const struct got_error *gw_parse_querystring(struct gw_trans *);
216 static const struct got_error *gw_blame(struct gw_trans *);
217 static const struct got_error *gw_blob(struct gw_trans *);
218 static const struct got_error *gw_diff(struct gw_trans *);
219 static const struct got_error *gw_index(struct gw_trans *);
220 static const struct got_error *gw_commits(struct gw_trans *);
221 static const struct got_error *gw_briefs(struct gw_trans *);
222 static const struct got_error *gw_summary(struct gw_trans *);
223 static const struct got_error *gw_tree(struct gw_trans *);
224 static const struct got_error *gw_tag(struct gw_trans *);
226 struct gw_query_action {
227 unsigned int func_id;
228 const char *func_name;
229 const struct got_error *(*func_main)(struct gw_trans *);
233 enum gw_query_actions {
246 static struct gw_query_action gw_query_funcs[] = {
247 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
248 { GW_BLOB, "blob", NULL, NULL },
249 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
250 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
251 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
252 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
253 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
254 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
255 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
256 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
259 static const struct got_error *
260 gw_kcgi_error(enum kcgi_err kerr)
265 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
266 return got_error(GOT_ERR_CANCELLED);
268 if (kerr == KCGI_ENOMEM)
269 return got_error_set_errno(ENOMEM,
270 kcgi_strerror(kerr));
272 if (kerr == KCGI_ENFILE)
273 return got_error_set_errno(ENFILE,
274 kcgi_strerror(kerr));
276 if (kerr == KCGI_EAGAIN)
277 return got_error_set_errno(EAGAIN,
278 kcgi_strerror(kerr));
280 if (kerr == KCGI_FORM)
281 return got_error_msg(GOT_ERR_IO,
282 kcgi_strerror(kerr));
284 return got_error_from_errno(kcgi_strerror(kerr));
287 static const struct got_error *
288 gw_apply_unveil(const char *repo_path)
290 const struct got_error *err;
292 if (repo_path && unveil(repo_path, "r") != 0)
293 return got_error_from_errno2("unveil", repo_path);
295 if (unveil("/tmp", "rwc") != 0)
296 return got_error_from_errno2("unveil", "/tmp");
298 err = got_privsep_unveil_exec_helpers();
302 if (unveil(NULL, NULL) != 0)
303 return got_error_from_errno("unveil");
308 static const struct got_error *
309 gw_strdup_string(char **s, char *str1, const char *str2)
312 /* XXX and what is the value of errno ?!? */
313 return got_error_from_errno("strdup");
319 return got_error_from_errno("strdup");
324 isbinary(const uint8_t *buf, size_t n)
328 for (i = 0; i < n; i++)
334 static const struct got_error *
335 gw_blame(struct gw_trans *gw_trans)
337 const struct got_error *error = NULL;
338 struct gw_header *header = NULL;
342 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
344 return got_error_from_errno("pledge");
346 if ((header = gw_init_header()) == NULL)
347 return got_error_from_errno("malloc");
349 error = gw_apply_unveil(gw_trans->gw_dir->path);
353 error = gw_get_header(gw_trans, header, 1);
357 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
358 "blame_header_wrapper", KATTR__MAX);
361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
362 "blame_header", KATTR__MAX);
365 error = gw_get_time_str(&age, header->committer_time,
369 error = gw_gen_age_header(gw_trans, age ?age : "");
372 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
375 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
379 "dotted_line", KATTR__MAX);
382 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
387 "blame", KATTR__MAX);
390 error = gw_output_file_blame(gw_trans);
393 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
395 got_ref_list_free(&header->refs);
396 gw_free_headers(header);
397 if (error == NULL && kerr != KCGI_OK)
398 error = gw_kcgi_error(kerr);
402 static const struct got_error *
403 gw_blob(struct gw_trans *gw_trans)
405 const struct got_error *error = NULL;
406 struct gw_header *header = NULL;
408 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
410 return got_error_from_errno("pledge");
412 if ((header = gw_init_header()) == NULL)
413 return got_error_from_errno("malloc");
415 error = gw_apply_unveil(gw_trans->gw_dir->path);
419 error = gw_get_header(gw_trans, header, 1);
423 error = gw_output_blob_buf(gw_trans);
425 got_ref_list_free(&header->refs);
426 gw_free_headers(header);
430 static const struct got_error *
431 gw_diff(struct gw_trans *gw_trans)
433 const struct got_error *error = NULL;
434 struct gw_header *header = NULL;
436 enum kcgi_err kerr = KCGI_OK;
438 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
440 return got_error_from_errno("pledge");
442 if ((header = gw_init_header()) == NULL)
443 return got_error_from_errno("malloc");
445 error = gw_apply_unveil(gw_trans->gw_dir->path);
449 error = gw_get_header(gw_trans, header, 1);
453 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
454 "diff_header_wrapper", KATTR__MAX);
457 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
458 "diff_header", KATTR__MAX);
461 error = gw_gen_diff_header(gw_trans, header->parent_id,
465 error = gw_gen_commit_header(gw_trans, header->commit_id,
469 error = gw_gen_tree_header(gw_trans, header->tree_id);
472 error = gw_gen_author_header(gw_trans, header->author);
475 error = gw_gen_committer_header(gw_trans, header->author);
478 error = gw_get_time_str(&age, header->committer_time,
482 error = gw_gen_age_header(gw_trans, age ?age : "");
485 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
488 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
491 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
492 "dotted_line", KATTR__MAX);
495 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
499 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
503 error = gw_output_diff(gw_trans, header);
507 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
509 got_ref_list_free(&header->refs);
510 gw_free_headers(header);
512 if (error == NULL && kerr != KCGI_OK)
513 error = gw_kcgi_error(kerr);
517 static const struct got_error *
518 gw_index(struct gw_trans *gw_trans)
520 const struct got_error *error = NULL;
521 struct gw_dir *gw_dir = NULL;
522 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
523 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
524 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
527 if (pledge("stdio rpath proc exec sendfd unveil",
529 error = got_error_from_errno("pledge");
533 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
537 error = gw_load_got_paths(gw_trans);
541 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
542 "index_header", KATTR__MAX);
544 return gw_kcgi_error(kerr);
545 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
546 "index_header_project", KATTR__MAX);
548 return gw_kcgi_error(kerr);
549 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
551 return gw_kcgi_error(kerr);
552 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
554 return gw_kcgi_error(kerr);
556 if (gw_trans->gw_conf->got_show_repo_description) {
557 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
558 "index_header_description", KATTR__MAX);
560 return gw_kcgi_error(kerr);
561 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
563 return gw_kcgi_error(kerr);
564 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
566 return gw_kcgi_error(kerr);
569 if (gw_trans->gw_conf->got_show_repo_owner) {
570 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
571 "index_header_owner", KATTR__MAX);
573 return gw_kcgi_error(kerr);
574 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
576 return gw_kcgi_error(kerr);
577 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
579 return gw_kcgi_error(kerr);
582 if (gw_trans->gw_conf->got_show_repo_age) {
583 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
584 "index_header_age", KATTR__MAX);
586 return gw_kcgi_error(kerr);
587 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
589 return gw_kcgi_error(kerr);
590 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
592 return gw_kcgi_error(kerr);
595 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
597 return gw_kcgi_error(kerr);
599 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
600 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
601 "index_wrapper", KATTR__MAX);
603 return gw_kcgi_error(kerr);
604 kerr = khtml_puts(gw_trans->gw_html_req,
605 "No repositories found in ");
607 return gw_kcgi_error(kerr);
608 kerr = khtml_puts(gw_trans->gw_html_req,
609 gw_trans->gw_conf->got_repos_path);
611 return gw_kcgi_error(kerr);
612 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
614 return gw_kcgi_error(kerr);
615 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
616 "dotted_line", KATTR__MAX);
618 return gw_kcgi_error(kerr);
619 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
621 return gw_kcgi_error(kerr);
625 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
628 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
629 if (gw_trans->page > 0 && (gw_trans->page *
630 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
637 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
638 "index_wrapper", KATTR__MAX);
642 if (asprintf(&href_summary, "?path=%s&action=summary",
643 gw_dir->name) == -1) {
644 error = got_error_from_errno("asprintf");
647 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
648 "index_project", KATTR__MAX);
651 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
652 href_summary, KATTR__MAX);
655 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
658 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
661 if (gw_trans->gw_conf->got_show_repo_description) {
662 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
663 KATTR_ID, "index_project_description", KATTR__MAX);
666 kerr = khtml_puts(gw_trans->gw_html_req,
667 gw_dir->description ? gw_dir->description : "");
670 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
674 if (gw_trans->gw_conf->got_show_repo_owner) {
675 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
676 KATTR_ID, "index_project_owner", KATTR__MAX);
679 kerr = khtml_puts(gw_trans->gw_html_req,
680 gw_dir->owner ? gw_dir->owner : "");
683 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
687 if (gw_trans->gw_conf->got_show_repo_age) {
688 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
689 KATTR_ID, "index_project_age", KATTR__MAX);
692 kerr = khtml_puts(gw_trans->gw_html_req,
693 gw_dir->age ? gw_dir->age : "");
696 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
701 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
702 "navs_wrapper", KATTR__MAX);
705 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
710 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
711 href_summary, KATTR__MAX);
714 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
717 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
721 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
725 if (asprintf(&href_briefs, "?path=%s&action=briefs",
726 gw_dir->name) == -1) {
727 error = got_error_from_errno("asprintf");
730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
731 href_briefs, KATTR__MAX);
734 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
737 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
739 error = gw_kcgi_error(kerr);
741 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
745 if (asprintf(&href_commits, "?path=%s&action=commits",
746 gw_dir->name) == -1) {
747 error = got_error_from_errno("asprintf");
750 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
751 href_commits, KATTR__MAX);
754 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
757 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
761 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
765 if (asprintf(&href_tree, "?path=%s&action=tree",
766 gw_dir->name) == -1) {
767 error = got_error_from_errno("asprintf");
770 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
771 href_tree, KATTR__MAX);
774 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
778 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
781 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
782 "dotted_line", KATTR__MAX);
785 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
798 if (gw_trans->gw_conf->got_max_repos_display == 0)
801 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
803 KATTR_ID, "np_wrapper", KATTR__MAX);
806 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
807 KATTR_ID, "nav_prev", KATTR__MAX);
810 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
811 (gw_trans->page > 0) &&
812 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
813 prev_disp == gw_trans->repos_total)) {
814 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
815 KATTR_ID, "np_wrapper", KATTR__MAX);
818 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
819 KATTR_ID, "nav_prev", KATTR__MAX);
824 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
825 (gw_trans->page > 0) &&
826 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
827 prev_disp == gw_trans->repos_total)) {
828 if (asprintf(&href_prev, "?page=%d",
829 gw_trans->page - 1) == -1) {
830 error = got_error_from_errno("asprintf");
833 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
834 KATTR_HREF, href_prev, KATTR__MAX);
837 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
840 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
845 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
847 return gw_kcgi_error(kerr);
849 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
850 next_disp == gw_trans->gw_conf->got_max_repos_display &&
851 dir_c != (gw_trans->page + 1) *
852 gw_trans->gw_conf->got_max_repos_display) {
853 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
854 KATTR_ID, "nav_next", KATTR__MAX);
857 if (asprintf(&href_next, "?page=%d",
858 gw_trans->page + 1) == -1) {
859 error = got_error_from_errno("calloc");
862 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
863 KATTR_HREF, href_next, KATTR__MAX);
866 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
869 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
876 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
877 (gw_trans->page > 0) &&
878 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
879 prev_disp == gw_trans->repos_total)) {
880 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
893 if (error == NULL && kerr != KCGI_OK)
894 error = gw_kcgi_error(kerr);
898 static const struct got_error *
899 gw_commits(struct gw_trans *gw_trans)
901 const struct got_error *error = NULL;
902 struct gw_header *header = NULL, *n_header = NULL;
904 char *href_diff = NULL, *href_blob = NULL;
905 enum kcgi_err kerr = KCGI_OK;
907 if ((header = gw_init_header()) == NULL)
908 return got_error_from_errno("malloc");
910 if (pledge("stdio rpath proc exec sendfd unveil",
912 error = got_error_from_errno("pledge");
916 error = gw_apply_unveil(gw_trans->gw_dir->path);
920 error = gw_get_header(gw_trans, header,
921 gw_trans->gw_conf->got_max_commits_display);
925 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
926 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
927 "commits_line_wrapper", KATTR__MAX);
930 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
934 error = gw_gen_author_header(gw_trans, n_header->author);
937 error = gw_gen_committer_header(gw_trans, n_header->author);
940 error = gw_get_time_str(&age, n_header->committer_time,
944 error = gw_gen_age_header(gw_trans, age ?age : "");
947 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
951 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
952 "dotted_line", KATTR__MAX);
955 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
959 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
960 "commit", KATTR__MAX);
963 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
966 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
970 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
971 gw_trans->repo_name, n_header->commit_id) == -1) {
972 error = got_error_from_errno("asprintf");
975 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
976 KATTR_ID, "navs_wrapper", KATTR__MAX);
979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
980 KATTR_ID, "navs", KATTR__MAX);
983 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
984 KATTR_HREF, href_diff, KATTR__MAX);
987 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
990 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
994 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
998 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
999 gw_trans->repo_name, n_header->commit_id) == -1) {
1000 error = got_error_from_errno("asprintf");
1003 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1004 KATTR_HREF, href_blob, KATTR__MAX);
1005 if (kerr != KCGI_OK)
1007 khtml_puts(gw_trans->gw_html_req, "tree");
1008 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1009 if (kerr != KCGI_OK)
1011 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1012 if (kerr != KCGI_OK)
1015 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1016 "solid_line", KATTR__MAX);
1017 if (kerr != KCGI_OK)
1019 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1020 if (kerr != KCGI_OK)
1027 got_ref_list_free(&header->refs);
1028 gw_free_headers(header);
1029 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1030 gw_free_headers(n_header);
1034 if (error == NULL && kerr != KCGI_OK)
1035 error = gw_kcgi_error(kerr);
1039 static const struct got_error *
1040 gw_briefs(struct gw_trans *gw_trans)
1042 const struct got_error *error = NULL;
1043 struct gw_header *header = NULL, *n_header = NULL;
1044 char *age = NULL, *age_html = NULL;
1045 char *href_diff = NULL, *href_blob = NULL;
1046 char *newline, *smallerthan;
1047 enum kcgi_err kerr = KCGI_OK;
1049 if ((header = gw_init_header()) == NULL)
1050 return got_error_from_errno("malloc");
1052 if (pledge("stdio rpath proc exec sendfd unveil",
1054 error = got_error_from_errno("pledge");
1058 error = gw_apply_unveil(gw_trans->gw_dir->path);
1062 if (gw_trans->action == GW_SUMMARY)
1063 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1065 error = gw_get_header(gw_trans, header,
1066 gw_trans->gw_conf->got_max_commits_display);
1070 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1071 error = gw_get_time_str(&age, n_header->committer_time,
1076 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1077 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1078 if (kerr != KCGI_OK)
1081 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1082 KATTR_ID, "briefs_age", KATTR__MAX);
1083 if (kerr != KCGI_OK)
1085 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
1086 error = got_error_from_errno("asprintf");
1089 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
1090 if (kerr != KCGI_OK)
1092 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1093 if (kerr != KCGI_OK)
1096 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1097 KATTR_ID, "briefs_author", KATTR__MAX);
1098 if (kerr != KCGI_OK)
1100 smallerthan = strchr(n_header->author, '<');
1102 *smallerthan = '\0';
1103 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1104 if (kerr != KCGI_OK)
1106 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1107 if (kerr != KCGI_OK)
1110 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1111 gw_trans->repo_name, n_header->commit_id) == -1) {
1112 error = got_error_from_errno("asprintf");
1115 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1116 KATTR_ID, "briefs_log", KATTR__MAX);
1117 if (kerr != KCGI_OK)
1119 newline = strchr(n_header->commit_msg, '\n');
1122 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1123 KATTR_HREF, href_diff, KATTR__MAX);
1124 if (kerr != KCGI_OK)
1126 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1127 if (kerr != KCGI_OK)
1129 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1130 if (kerr != KCGI_OK)
1133 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1134 KATTR_ID, "navs_wrapper", KATTR__MAX);
1135 if (kerr != KCGI_OK)
1137 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1138 KATTR_ID, "navs", KATTR__MAX);
1139 if (kerr != KCGI_OK)
1141 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1142 KATTR_HREF, href_diff, KATTR__MAX);
1143 if (kerr != KCGI_OK)
1145 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1146 if (kerr != KCGI_OK)
1148 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1149 if (kerr != KCGI_OK)
1152 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1153 if (kerr != KCGI_OK)
1156 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1157 gw_trans->repo_name, n_header->commit_id) == -1) {
1158 error = got_error_from_errno("asprintf");
1161 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1162 KATTR_HREF, href_blob, KATTR__MAX);
1163 if (kerr != KCGI_OK)
1165 khtml_puts(gw_trans->gw_html_req, "tree");
1166 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1167 if (kerr != KCGI_OK)
1169 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1170 if (kerr != KCGI_OK)
1173 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1174 KATTR_ID, "dotted_line", KATTR__MAX);
1175 if (kerr != KCGI_OK)
1177 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1178 if (kerr != KCGI_OK)
1191 got_ref_list_free(&header->refs);
1192 gw_free_headers(header);
1193 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1194 gw_free_headers(n_header);
1199 if (error == NULL && kerr != KCGI_OK)
1200 error = gw_kcgi_error(kerr);
1204 static const struct got_error *
1205 gw_summary(struct gw_trans *gw_trans)
1207 const struct got_error *error = NULL;
1209 enum kcgi_err kerr = KCGI_OK;
1211 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1212 return got_error_from_errno("pledge");
1214 /* unveil is applied with gw_briefs below */
1216 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1217 "summary_wrapper", KATTR__MAX);
1218 if (kerr != KCGI_OK)
1219 return gw_kcgi_error(kerr);
1221 if (gw_trans->gw_conf->got_show_repo_description &&
1222 gw_trans->gw_dir->description != NULL &&
1223 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1224 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1225 KATTR_ID, "description_title", KATTR__MAX);
1226 if (kerr != KCGI_OK)
1228 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1229 if (kerr != KCGI_OK)
1231 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1232 if (kerr != KCGI_OK)
1234 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1235 KATTR_ID, "description", KATTR__MAX);
1236 if (kerr != KCGI_OK)
1238 kerr = khtml_puts(gw_trans->gw_html_req,
1239 gw_trans->gw_dir->description);
1240 if (kerr != KCGI_OK)
1242 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1243 if (kerr != KCGI_OK)
1247 if (gw_trans->gw_conf->got_show_repo_owner &&
1248 gw_trans->gw_dir->owner != NULL &&
1249 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1250 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1251 KATTR_ID, "repo_owner_title", KATTR__MAX);
1252 if (kerr != KCGI_OK)
1254 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1255 if (kerr != KCGI_OK)
1257 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1258 if (kerr != KCGI_OK)
1260 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1261 KATTR_ID, "repo_owner", KATTR__MAX);
1262 if (kerr != KCGI_OK)
1264 kerr = khtml_puts(gw_trans->gw_html_req,
1265 gw_trans->gw_dir->owner);
1266 if (kerr != KCGI_OK)
1268 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1269 if (kerr != KCGI_OK)
1273 if (gw_trans->gw_conf->got_show_repo_age) {
1274 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1275 "refs/heads", TM_LONG);
1279 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1280 KATTR_ID, "last_change_title", KATTR__MAX);
1281 if (kerr != KCGI_OK)
1283 kerr = khtml_puts(gw_trans->gw_html_req,
1285 if (kerr != KCGI_OK)
1287 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1288 if (kerr != KCGI_OK)
1290 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1291 KATTR_ID, "last_change", KATTR__MAX);
1292 if (kerr != KCGI_OK)
1294 kerr = khtml_puts(gw_trans->gw_html_req, age);
1295 if (kerr != KCGI_OK)
1297 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1298 if (kerr != KCGI_OK)
1303 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1304 gw_trans->gw_dir->url != NULL &&
1305 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1306 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1307 KATTR_ID, "cloneurl_title", KATTR__MAX);
1308 if (kerr != KCGI_OK)
1310 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1311 if (kerr != KCGI_OK)
1313 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1314 if (kerr != KCGI_OK)
1316 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1317 KATTR_ID, "cloneurl", KATTR__MAX);
1318 if (kerr != KCGI_OK)
1320 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1321 if (kerr != KCGI_OK)
1323 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1324 if (kerr != KCGI_OK)
1328 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1329 if (kerr != KCGI_OK)
1332 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1333 "briefs_title_wrapper", KATTR__MAX);
1334 if (kerr != KCGI_OK)
1336 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1337 "briefs_title", KATTR__MAX);
1338 if (kerr != KCGI_OK)
1340 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1341 if (kerr != KCGI_OK)
1343 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1344 if (kerr != KCGI_OK)
1346 error = gw_briefs(gw_trans);
1350 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1355 error = gw_output_repo_heads(gw_trans);
1358 if (error == NULL && kerr != KCGI_OK)
1359 error = gw_kcgi_error(kerr);
1363 static const struct got_error *
1364 gw_tree(struct gw_trans *gw_trans)
1366 const struct got_error *error = NULL;
1367 struct gw_header *header = NULL;
1368 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1369 char *age = NULL, *age_html = NULL;
1372 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1373 return got_error_from_errno("pledge");
1375 if ((header = gw_init_header()) == NULL)
1376 return got_error_from_errno("malloc");
1378 error = gw_apply_unveil(gw_trans->gw_dir->path);
1382 error = gw_get_header(gw_trans, header, 1);
1386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1387 "tree_header_wrapper", KATTR__MAX);
1388 if (kerr != KCGI_OK)
1390 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1391 "tree_header", KATTR__MAX);
1392 if (kerr != KCGI_OK)
1394 error = gw_gen_tree_header(gw_trans, header->tree_id);
1397 error = gw_get_time_str(&age, header->committer_time,
1401 error = gw_gen_age_header(gw_trans, age ?age : "");
1404 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1407 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1408 if (kerr != KCGI_OK)
1410 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1411 "dotted_line", KATTR__MAX);
1412 if (kerr != KCGI_OK)
1414 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1415 if (kerr != KCGI_OK)
1418 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1419 "tree", KATTR__MAX);
1420 if (kerr != KCGI_OK)
1422 error = gw_output_repo_tree(gw_trans);
1426 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1428 got_ref_list_free(&header->refs);
1429 gw_free_headers(header);
1430 free(tree_html_disp);
1435 if (error == NULL && kerr != KCGI_OK)
1436 error = gw_kcgi_error(kerr);
1440 static const struct got_error *
1441 gw_tag(struct gw_trans *gw_trans)
1443 const struct got_error *error = NULL;
1444 struct gw_header *header = NULL;
1447 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1448 return got_error_from_errno("pledge");
1450 if ((header = gw_init_header()) == NULL)
1451 return got_error_from_errno("malloc");
1453 error = gw_apply_unveil(gw_trans->gw_dir->path);
1457 error = gw_get_header(gw_trans, header, 1);
1461 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1462 "tag_header_wrapper", KATTR__MAX);
1463 if (kerr != KCGI_OK)
1465 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1466 "tag_header", KATTR__MAX);
1467 if (kerr != KCGI_OK)
1469 error = gw_gen_commit_header(gw_trans, header->commit_id,
1473 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1476 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1477 if (kerr != KCGI_OK)
1479 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1480 "dotted_line", KATTR__MAX);
1481 if (kerr != KCGI_OK)
1483 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1484 if (kerr != KCGI_OK)
1487 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1488 "tree", KATTR__MAX);
1489 if (kerr != KCGI_OK)
1492 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1496 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1498 got_ref_list_free(&header->refs);
1499 gw_free_headers(header);
1500 if (error == NULL && kerr != KCGI_OK)
1501 error = gw_kcgi_error(kerr);
1505 static const struct got_error *
1506 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1508 const struct got_error *error = NULL;
1513 if (asprintf(&dir_test, "%s/%s/%s",
1514 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1515 GOTWEB_GIT_DIR) == -1)
1516 return got_error_from_errno("asprintf");
1518 dt = opendir(dir_test);
1522 error = gw_strdup_string(&gw_dir->path, dir_test, NULL);
1529 if (asprintf(&dir_test, "%s/%s/%s",
1530 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1531 GOTWEB_GOT_DIR) == -1)
1532 return got_error_from_errno("asprintf");
1534 dt = opendir(dir_test);
1539 error = got_error(GOT_ERR_NOT_GIT_REPO);
1543 if (asprintf(&dir_test, "%s/%s",
1544 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1545 return got_error_from_errno("asprintf");
1547 error = gw_strdup_string(&gw_dir->path, dir_test, NULL);
1553 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1557 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1560 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1561 "refs/heads", TM_DIFF);
1564 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1572 static const struct got_error *
1573 gw_load_got_paths(struct gw_trans *gw_trans)
1575 const struct got_error *error = NULL;
1577 struct dirent **sd_dent;
1578 struct gw_dir *gw_dir;
1580 unsigned int d_cnt, d_i;
1582 d = opendir(gw_trans->gw_conf->got_repos_path);
1584 error = got_error_from_errno2("opendir",
1585 gw_trans->gw_conf->got_repos_path);
1589 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1592 error = got_error_from_errno2("scandir",
1593 gw_trans->gw_conf->got_repos_path);
1597 for (d_i = 0; d_i < d_cnt; d_i++) {
1598 if (gw_trans->gw_conf->got_max_repos > 0 &&
1599 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1600 break; /* account for parent and self */
1602 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1603 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1606 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1610 error = gw_load_got_path(gw_trans, gw_dir);
1611 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1618 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1619 !got_path_dir_is_empty(gw_dir->path)) {
1620 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1622 gw_trans->repos_total++;
1630 static const struct got_error *
1631 gw_parse_querystring(struct gw_trans *gw_trans)
1633 const struct got_error *error = NULL;
1635 struct gw_query_action *action = NULL;
1638 if (gw_trans->gw_req->fieldnmap[0]) {
1639 error = got_error_from_errno("bad parse");
1641 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1642 /* define gw_trans->repo_path */
1643 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1644 return got_error_from_errno("asprintf");
1646 if (asprintf(&gw_trans->repo_path, "%s/%s",
1647 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1648 return got_error_from_errno("asprintf");
1650 /* get action and set function */
1651 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1652 for (i = 0; i < nitems(gw_query_funcs); i++) {
1653 action = &gw_query_funcs[i];
1654 if (action->func_name == NULL)
1657 if (strcmp(action->func_name,
1658 p->parsed.s) == 0) {
1659 gw_trans->action = i;
1660 if (asprintf(&gw_trans->action_name,
1661 "%s", action->func_name) == -1)
1663 got_error_from_errno(
1672 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1673 if (asprintf(&gw_trans->commit, "%s",
1675 return got_error_from_errno("asprintf");
1677 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1678 if (asprintf(&gw_trans->repo_file, "%s",
1680 return got_error_from_errno("asprintf");
1682 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1683 if (asprintf(&gw_trans->repo_folder, "%s",
1685 return got_error_from_errno("asprintf");
1687 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1688 if (asprintf(&gw_trans->headref, "%s",
1690 return got_error_from_errno("asprintf");
1692 if (action == NULL) {
1693 error = got_error_from_errno("invalid action");
1696 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1700 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1704 gw_trans->action = GW_INDEX;
1706 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1707 gw_trans->page = p->parsed.i;
1712 static const struct got_error *
1713 gw_init_gw_dir(struct gw_dir **gw_dir, char *dir)
1715 const struct got_error *error;
1717 *gw_dir = malloc(sizeof(**gw_dir));
1718 if (*gw_dir == NULL)
1719 return got_error_from_errno("malloc");
1721 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1722 error = got_error_from_errno("asprintf");
1731 static const struct got_error *
1732 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1736 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1737 if (kerr != KCGI_OK)
1738 return gw_kcgi_error(kerr);
1739 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1741 if (kerr != KCGI_OK)
1742 return gw_kcgi_error(kerr);
1743 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1745 if (kerr != KCGI_OK)
1746 return gw_kcgi_error(kerr);
1747 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1749 if (kerr != KCGI_OK)
1750 return gw_kcgi_error(kerr);
1751 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1752 if (kerr != KCGI_OK)
1753 return gw_kcgi_error(kerr);
1754 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1756 if (kerr != KCGI_OK)
1757 return gw_kcgi_error(kerr);
1759 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1760 kerr = khttp_head(gw_trans->gw_req,
1761 kresps[KRESP_CONTENT_DISPOSITION],
1762 "attachment; filename=%s", gw_trans->repo_file);
1763 if (kerr != KCGI_OK)
1764 return gw_kcgi_error(kerr);
1767 kerr = khttp_body(gw_trans->gw_req);
1768 return gw_kcgi_error(kerr);
1771 static const struct got_error *
1772 gw_display_index(struct gw_trans *gw_trans)
1774 const struct got_error *error;
1777 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1781 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1782 if (kerr != KCGI_OK)
1783 return gw_kcgi_error(kerr);
1785 if (gw_trans->action != GW_BLOB) {
1786 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1787 gw_query_funcs[gw_trans->action].template);
1788 if (kerr != KCGI_OK) {
1789 khtml_close(gw_trans->gw_html_req);
1790 return gw_kcgi_error(kerr);
1794 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1798 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1800 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1803 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1805 khtml_puts(gw_trans->gw_html_req, err->msg);
1806 khtml_close(gw_trans->gw_html_req);
1810 gw_template(size_t key, void *arg)
1812 const struct got_error *error = NULL;
1814 struct gw_trans *gw_trans = arg;
1815 char *img_src = NULL;
1819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1820 KATTR_NAME, "viewport",
1821 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1823 if (kerr != KCGI_OK)
1825 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1826 if (kerr != KCGI_OK)
1828 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1829 KATTR_CHARSET, "utf-8",
1831 if (kerr != KCGI_OK)
1833 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1834 if (kerr != KCGI_OK)
1836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1837 KATTR_NAME, "msapplication-TileColor",
1838 KATTR_CONTENT, "#da532c", KATTR__MAX);
1839 if (kerr != KCGI_OK)
1841 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1842 if (kerr != KCGI_OK)
1844 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1845 KATTR_NAME, "theme-color",
1846 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1847 if (kerr != KCGI_OK)
1849 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1850 if (kerr != KCGI_OK)
1852 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1853 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1854 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1855 if (kerr != KCGI_OK)
1857 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1858 if (kerr != KCGI_OK)
1860 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1861 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1862 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1863 if (kerr != KCGI_OK)
1865 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1866 if (kerr != KCGI_OK)
1868 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1869 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1870 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1871 if (kerr != KCGI_OK)
1873 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1874 if (kerr != KCGI_OK)
1876 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1877 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1879 if (kerr != KCGI_OK)
1881 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1882 if (kerr != KCGI_OK)
1884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1885 KATTR_REL, "mask-icon", KATTR_HREF,
1886 "/safari-pinned-tab.svg", KATTR__MAX);
1887 if (kerr != KCGI_OK)
1889 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1890 if (kerr != KCGI_OK)
1892 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1893 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1894 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1895 if (kerr != KCGI_OK)
1897 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1898 if (kerr != KCGI_OK)
1902 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1903 KATTR_ID, "got_link", KATTR__MAX);
1904 if (kerr != KCGI_OK)
1906 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1907 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1908 KATTR_TARGET, "_sotd", KATTR__MAX);
1909 if (kerr != KCGI_OK)
1911 if (asprintf(&img_src, "/%s",
1912 gw_trans->gw_conf->got_logo) == -1)
1914 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1915 KATTR_SRC, img_src, KATTR__MAX);
1916 if (kerr != KCGI_OK) {
1920 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1921 if (kerr != KCGI_OK) {
1926 case (TEMPL_SITEPATH):
1927 error = gw_output_site_link(gw_trans);
1932 if (gw_trans->gw_conf->got_site_name != NULL) {
1933 kerr = khtml_puts(gw_trans->gw_html_req,
1934 gw_trans->gw_conf->got_site_name);
1935 if (kerr != KCGI_OK)
1939 case (TEMPL_SEARCH):
1941 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1942 "search", KATTR__MAX);
1943 if (kerr != KCGI_OK)
1945 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1946 KATTR_METHOD, "POST", KATTR__MAX);
1947 if (kerr != KCGI_OK)
1949 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1950 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1951 KATTR_MAXLENGTH, "50", KATTR__MAX);
1952 if (kerr != KCGI_OK)
1954 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1956 if (kerr != KCGI_OK)
1958 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1959 if (kerr != KCGI_OK)
1961 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1962 if (kerr != KCGI_OK)
1965 case(TEMPL_SITEOWNER):
1966 if (gw_trans->gw_conf->got_site_owner != NULL &&
1967 gw_trans->gw_conf->got_show_site_owner) {
1968 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1969 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1970 if (kerr != KCGI_OK)
1972 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1973 KATTR_ID, "site_owner", KATTR__MAX);
1974 if (kerr != KCGI_OK)
1976 kerr = khtml_puts(gw_trans->gw_html_req,
1977 gw_trans->gw_conf->got_site_owner);
1978 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1979 if (kerr != KCGI_OK)
1983 case(TEMPL_CONTENT):
1984 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1986 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1987 KATTR_ID, "tmpl_err", KATTR__MAX);
1988 if (kerr != KCGI_OK)
1990 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1991 if (kerr != KCGI_OK)
1993 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1994 if (kerr != KCGI_OK)
1996 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1997 if (kerr != KCGI_OK)
2007 static const struct got_error *
2008 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2010 const struct got_error *error = NULL;
2011 char *ref_str = NULL;
2012 enum kcgi_err kerr = KCGI_OK;
2014 if (strcmp(str2, "") != 0) {
2015 if (asprintf(&ref_str, "(%s)", str2) == -1) {
2016 error = got_error_from_errno("asprintf");
2020 error = gw_strdup_string(&ref_str, "", NULL);
2025 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2026 KATTR_ID, "header_commit_title", KATTR__MAX);
2027 if (kerr != KCGI_OK)
2029 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2030 if (kerr != KCGI_OK)
2032 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2033 if (kerr != KCGI_OK)
2035 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2036 KATTR_ID, "header_commit", KATTR__MAX);
2037 if (kerr != KCGI_OK)
2039 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2040 if (kerr != KCGI_OK)
2042 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2043 if (kerr != KCGI_OK)
2045 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
2046 if (kerr != KCGI_OK)
2048 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2050 if (error == NULL && kerr != KCGI_OK)
2051 error = gw_kcgi_error(kerr);
2055 static const struct got_error *
2056 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2058 const struct got_error *error = NULL;
2059 enum kcgi_err kerr = KCGI_OK;
2061 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2062 KATTR_ID, "header_diff_title", KATTR__MAX);
2063 if (kerr != KCGI_OK)
2065 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2066 if (kerr != KCGI_OK)
2068 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2069 if (kerr != KCGI_OK)
2071 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2072 KATTR_ID, "header_diff", KATTR__MAX);
2073 if (kerr != KCGI_OK)
2075 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2076 if (kerr != KCGI_OK)
2078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2079 if (kerr != KCGI_OK)
2081 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2082 if (kerr != KCGI_OK)
2084 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2086 if (error == NULL && kerr != KCGI_OK)
2087 error = gw_kcgi_error(kerr);
2091 static const struct got_error *
2092 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2094 const struct got_error *error = NULL;
2095 enum kcgi_err kerr = KCGI_OK;
2097 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2098 KATTR_ID, "header_age_title", KATTR__MAX);
2099 if (kerr != KCGI_OK)
2101 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2102 if (kerr != KCGI_OK)
2104 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2105 if (kerr != KCGI_OK)
2107 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2108 KATTR_ID, "header_age", KATTR__MAX);
2109 if (kerr != KCGI_OK)
2111 kerr = khtml_puts(gw_trans->gw_html_req, str);
2112 if (kerr != KCGI_OK)
2114 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2116 if (error == NULL && kerr != KCGI_OK)
2117 error = gw_kcgi_error(kerr);
2121 static const struct got_error *
2122 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2124 const struct got_error *error = NULL;
2127 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2128 KATTR_ID, "header_author_title", KATTR__MAX);
2129 if (kerr != KCGI_OK)
2131 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2132 if (kerr != KCGI_OK)
2134 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2135 if (kerr != KCGI_OK)
2137 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2138 KATTR_ID, "header_author", KATTR__MAX);
2139 if (kerr != KCGI_OK)
2141 kerr = khtml_puts(gw_trans->gw_html_req, str);
2142 if (kerr != KCGI_OK)
2144 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2146 if (error == NULL && kerr != KCGI_OK)
2147 error = gw_kcgi_error(kerr);
2151 static const struct got_error *
2152 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2154 const struct got_error *error = NULL;
2157 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2158 KATTR_ID, "header_committer_title", KATTR__MAX);
2159 if (kerr != KCGI_OK)
2161 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2162 if (kerr != KCGI_OK)
2164 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2165 if (kerr != KCGI_OK)
2167 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2168 KATTR_ID, "header_committer", KATTR__MAX);
2169 if (kerr != KCGI_OK)
2171 kerr = khtml_puts(gw_trans->gw_html_req, str);
2172 if (kerr != KCGI_OK)
2174 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2176 if (error == NULL && kerr != KCGI_OK)
2177 error = gw_kcgi_error(kerr);
2181 static const struct got_error *
2182 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2184 const struct got_error *error = NULL;
2187 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2188 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2189 if (kerr != KCGI_OK)
2191 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2192 if (kerr != KCGI_OK)
2194 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2195 if (kerr != KCGI_OK)
2197 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2198 KATTR_ID, "header_commit_msg", KATTR__MAX);
2199 if (kerr != KCGI_OK)
2201 kerr = khttp_puts(gw_trans->gw_req, str);
2202 if (kerr != KCGI_OK)
2204 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2206 if (error == NULL && kerr != KCGI_OK)
2207 error = gw_kcgi_error(kerr);
2211 static const struct got_error *
2212 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2214 const struct got_error *error = NULL;
2217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2218 KATTR_ID, "header_tree_title", KATTR__MAX);
2219 if (kerr != KCGI_OK)
2221 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2222 if (kerr != KCGI_OK)
2224 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2225 if (kerr != KCGI_OK)
2227 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2228 KATTR_ID, "header_tree", KATTR__MAX);
2229 if (kerr != KCGI_OK)
2231 kerr = khtml_puts(gw_trans->gw_html_req, str);
2232 if (kerr != KCGI_OK)
2234 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2236 if (error == NULL && kerr != KCGI_OK)
2237 error = gw_kcgi_error(kerr);
2241 static const struct got_error *
2242 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2245 const struct got_error *error = NULL;
2247 char *d_file = NULL;
2251 *description = NULL;
2252 if (gw_trans->gw_conf->got_show_repo_description == 0)
2255 if (asprintf(&d_file, "%s/description", dir) == -1)
2256 return got_error_from_errno("asprintf");
2258 f = fopen(d_file, "r");
2260 if (errno == ENOENT || errno == EACCES)
2262 error = got_error_from_errno2("fopen", d_file);
2266 if (fseek(f, 0, SEEK_END) == -1) {
2267 error = got_ferror(f, GOT_ERR_IO);
2272 error = got_ferror(f, GOT_ERR_IO);
2275 if (fseek(f, 0, SEEK_SET) == -1) {
2276 error = got_ferror(f, GOT_ERR_IO);
2279 *description = calloc(len + 1, sizeof(**description));
2280 if (*description == NULL) {
2281 error = got_error_from_errno("calloc");
2285 n = fread(*description, 1, len, f);
2286 if (n == 0 && ferror(f))
2287 error = got_ferror(f, GOT_ERR_IO);
2289 if (f != NULL && fclose(f) == -1 && error == NULL)
2290 error = got_error_from_errno("fclose");
2295 static const struct got_error *
2296 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2300 char *years = "years ago", *months = "months ago";
2301 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2302 char *minutes = "minutes ago", *seconds = "seconds ago";
2303 char *now = "right now";
2311 diff_time = time(NULL) - committer_time;
2312 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2313 if (asprintf(repo_age, "%lld %s",
2314 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2315 return got_error_from_errno("asprintf");
2316 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2317 if (asprintf(repo_age, "%lld %s",
2318 (diff_time / 60 / 60 / 24 / (365 / 12)),
2320 return got_error_from_errno("asprintf");
2321 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2322 if (asprintf(repo_age, "%lld %s",
2323 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2324 return got_error_from_errno("asprintf");
2325 } else if (diff_time > 60 * 60 * 24 * 2) {
2326 if (asprintf(repo_age, "%lld %s",
2327 (diff_time / 60 / 60 / 24), days) == -1)
2328 return got_error_from_errno("asprintf");
2329 } else if (diff_time > 60 * 60 * 2) {
2330 if (asprintf(repo_age, "%lld %s",
2331 (diff_time / 60 / 60), hours) == -1)
2332 return got_error_from_errno("asprintf");
2333 } else if (diff_time > 60 * 2) {
2334 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2336 return got_error_from_errno("asprintf");
2337 } else if (diff_time > 2) {
2338 if (asprintf(repo_age, "%lld %s", diff_time,
2340 return got_error_from_errno("asprintf");
2342 if (asprintf(repo_age, "%s", now) == -1)
2343 return got_error_from_errno("asprintf");
2347 if (gmtime_r(&committer_time, &tm) == NULL)
2348 return got_error_from_errno("gmtime_r");
2350 s = asctime_r(&tm, datebuf);
2352 return got_error_from_errno("asctime_r");
2354 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2355 return got_error_from_errno("asprintf");
2361 static const struct got_error *
2362 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2363 char *repo_ref, int ref_tm)
2365 const struct got_error *error = NULL;
2366 struct got_object_id *id = NULL;
2367 struct got_repository *repo = NULL;
2368 struct got_commit_object *commit = NULL;
2369 struct got_reflist_head refs;
2370 struct got_reflist_entry *re;
2371 struct got_reference *head_ref;
2373 time_t committer_time = 0, cmp_time = 0;
2377 SIMPLEQ_INIT(&refs);
2379 if (repo_ref == NULL)
2382 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2385 if (gw_trans->gw_conf->got_show_repo_age == 0)
2388 error = got_repo_open(&repo, dir, NULL);
2393 error = got_ref_list(&refs, repo, "refs/heads",
2394 got_ref_cmp_by_name, NULL);
2396 error = got_ref_list(&refs, repo, repo_ref,
2397 got_ref_cmp_by_name, NULL);
2401 SIMPLEQ_FOREACH(re, &refs, entry) {
2403 error = gw_strdup_string(&refname, repo_ref, NULL);
2407 error = gw_strdup_string(&refname, NULL,
2408 got_ref_get_name(re->ref));
2412 error = got_ref_open(&head_ref, repo, refname, 0);
2416 error = got_ref_resolve(&id, repo, head_ref);
2417 got_ref_close(head_ref);
2421 error = got_object_open_as_commit(&commit, repo, id);
2426 got_object_commit_get_committer_time(commit);
2428 if (cmp_time < committer_time)
2429 cmp_time = committer_time;
2432 if (cmp_time != 0) {
2433 committer_time = cmp_time;
2434 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2437 got_ref_list_free(&refs);
2442 static const struct got_error *
2443 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2445 const struct got_error *error;
2447 struct got_object_id *id1 = NULL, *id2 = NULL;
2448 char *label1 = NULL, *label2 = NULL, *line = NULL;
2450 size_t linesize = 0;
2452 enum kcgi_err kerr = KCGI_OK;
2458 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2462 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2463 error = got_repo_match_object_id(&id1, &label1,
2464 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2469 error = got_repo_match_object_id(&id2, &label2,
2470 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2474 error = got_object_get_type(&obj_type, header->repo, id2);
2478 case GOT_OBJ_TYPE_BLOB:
2479 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2482 case GOT_OBJ_TYPE_TREE:
2483 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2486 case GOT_OBJ_TYPE_COMMIT:
2487 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2491 error = got_error(GOT_ERR_OBJ_TYPE);
2496 if (fseek(f, 0, SEEK_SET) == -1) {
2497 error = got_ferror(f, GOT_ERR_IO);
2501 while ((linelen = getline(&line, &linesize, f)) != -1) {
2502 error = gw_colordiff_line(gw_trans, line);
2505 /* XXX: KHTML_PRETTY breaks this */
2506 kerr = khtml_puts(gw_trans->gw_html_req, line);
2507 if (kerr != KCGI_OK)
2509 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2510 if (kerr != KCGI_OK)
2513 if (linelen == -1 && ferror(f))
2514 error = got_error_from_errno("getline");
2516 if (f && fclose(f) == -1 && error == NULL)
2517 error = got_error_from_errno("fclose");
2524 if (error == NULL && kerr != KCGI_OK)
2525 error = gw_kcgi_error(kerr);
2529 static const struct got_error *
2530 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2532 const struct got_error *error = NULL;
2533 struct got_repository *repo;
2534 const char *gitconfig_owner;
2538 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2541 error = got_repo_open(&repo, dir, NULL);
2544 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2545 if (gitconfig_owner)
2546 error = gw_strdup_string(owner, NULL, gitconfig_owner);
2547 got_repo_close(repo);
2551 static const struct got_error *
2552 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2554 const struct got_error *error = NULL;
2556 char *d_file = NULL;
2562 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2563 return got_error_from_errno("asprintf");
2565 f = fopen(d_file, "r");
2567 if (errno != ENOENT && errno != EACCES)
2568 error = got_error_from_errno2("fopen", d_file);
2572 if (fseek(f, 0, SEEK_END) == -1) {
2573 error = got_ferror(f, GOT_ERR_IO);
2578 error = got_ferror(f, GOT_ERR_IO);
2581 if (fseek(f, 0, SEEK_SET) == -1) {
2582 error = got_ferror(f, GOT_ERR_IO);
2586 *url = calloc(len + 1, sizeof(**url));
2588 error = got_error_from_errno("calloc");
2592 n = fread(*url, 1, len, f);
2593 if (n == 0 && ferror(f))
2594 error = got_ferror(f, GOT_ERR_IO);
2596 if (f && fclose(f) == -1 && error == NULL)
2597 error = got_error_from_errno("fclose");
2602 static const struct got_error *
2603 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2604 int limit, int tag_type)
2606 const struct got_error *error = NULL;
2607 struct got_repository *repo = NULL;
2608 struct got_reflist_head refs;
2609 struct got_reflist_entry *re;
2611 char *id_str = NULL, *newline, *href_commits = NULL;
2612 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2613 struct got_tag_object *tag = NULL;
2614 enum kcgi_err kerr = KCGI_OK;
2615 int summary_header_displayed = 0;
2617 SIMPLEQ_INIT(&refs);
2619 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2623 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2627 SIMPLEQ_FOREACH(re, &refs, entry) {
2628 const char *refname;
2630 const char *tag_commit;
2632 struct got_object_id *id;
2633 struct got_commit_object *commit = NULL;
2635 refname = got_ref_get_name(re->ref);
2636 if (strncmp(refname, "refs/tags/", 10) != 0)
2640 error = got_ref_resolve(&id, repo, re->ref);
2644 error = got_object_open_as_tag(&tag, repo, id);
2646 if (error->code != GOT_ERR_OBJ_TYPE) {
2650 /* "lightweight" tag */
2651 error = got_object_open_as_commit(&commit, repo, id);
2656 tagger = got_object_commit_get_committer(commit);
2658 got_object_commit_get_committer_time(commit);
2659 error = got_object_id_str(&id_str, id);
2663 tagger = got_object_tag_get_tagger(tag);
2664 tagger_time = got_object_tag_get_tagger_time(tag);
2665 error = got_object_id_str(&id_str,
2666 got_object_tag_get_object_id(tag));
2671 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2672 strlen(id_str)) != 0)
2676 error = got_object_commit_get_logmsg(&tag_commit0,
2680 got_object_commit_close(commit);
2682 tag_commit0 = strdup(got_object_tag_get_message(tag));
2683 if (tag_commit0 == NULL) {
2684 error = got_error_from_errno("strdup");
2689 tag_commit = tag_commit0;
2690 while (*tag_commit == '\n')
2695 newline = strchr(tag_commit, '\n');
2699 if (summary_header_displayed == 0) {
2700 kerr = khtml_attr(gw_trans->gw_html_req,
2701 KELEM_DIV, KATTR_ID,
2702 "summary_tags_title_wrapper", KATTR__MAX);
2703 if (kerr != KCGI_OK)
2705 kerr = khtml_attr(gw_trans->gw_html_req,
2706 KELEM_DIV, KATTR_ID,
2707 "summary_tags_title", KATTR__MAX);
2708 if (kerr != KCGI_OK)
2710 kerr = khtml_puts(gw_trans->gw_html_req,
2712 if (kerr != KCGI_OK)
2714 kerr = khtml_closeelem(gw_trans->gw_html_req,
2716 if (kerr != KCGI_OK)
2718 kerr = khtml_attr(gw_trans->gw_html_req,
2719 KELEM_DIV, KATTR_ID,
2720 "summary_tags_content", KATTR__MAX);
2721 if (kerr != KCGI_OK)
2723 summary_header_displayed = 1;
2726 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2727 KATTR_ID, "tags_wrapper", KATTR__MAX);
2728 if (kerr != KCGI_OK)
2730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2731 KATTR_ID, "tags_age", KATTR__MAX);
2732 if (kerr != KCGI_OK)
2734 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2737 kerr = khtml_puts(gw_trans->gw_html_req,
2739 if (kerr != KCGI_OK)
2741 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2742 if (kerr != KCGI_OK)
2744 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2745 KATTR_ID, "tags", KATTR__MAX);
2746 if (kerr != KCGI_OK)
2748 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2749 if (kerr != KCGI_OK)
2751 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2752 if (kerr != KCGI_OK)
2754 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2755 KATTR_ID, "tags_name", KATTR__MAX);
2756 if (kerr != KCGI_OK)
2758 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2759 gw_trans->repo_name, id_str) == -1) {
2760 error = got_error_from_errno("asprintf");
2763 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2764 KATTR_HREF, href_tag, KATTR__MAX);
2765 if (kerr != KCGI_OK)
2767 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2768 if (kerr != KCGI_OK)
2770 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2771 if (kerr != KCGI_OK)
2774 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2775 KATTR_ID, "navs_wrapper", KATTR__MAX);
2776 if (kerr != KCGI_OK)
2778 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2779 KATTR_ID, "navs", KATTR__MAX);
2780 if (kerr != KCGI_OK)
2783 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2784 KATTR_HREF, href_tag, KATTR__MAX);
2785 if (kerr != KCGI_OK)
2787 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2788 if (kerr != KCGI_OK)
2790 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2791 if (kerr != KCGI_OK)
2794 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2795 if (kerr != KCGI_OK)
2797 if (asprintf(&href_briefs,
2798 "?path=%s&action=briefs&commit=%s",
2799 gw_trans->repo_name, id_str) == -1) {
2800 error = got_error_from_errno("asprintf");
2803 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2804 KATTR_HREF, href_briefs, KATTR__MAX);
2805 if (kerr != KCGI_OK)
2807 kerr = khtml_puts(gw_trans->gw_html_req,
2809 if (kerr != KCGI_OK)
2811 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2812 if (kerr != KCGI_OK)
2815 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2816 if (kerr != KCGI_OK)
2819 if (asprintf(&href_commits,
2820 "?path=%s&action=commits&commit=%s",
2821 gw_trans->repo_name, id_str) == -1) {
2822 error = got_error_from_errno("asprintf");
2825 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2826 KATTR_HREF, href_commits, KATTR__MAX);
2827 if (kerr != KCGI_OK)
2829 kerr = khtml_puts(gw_trans->gw_html_req,
2831 if (kerr != KCGI_OK)
2833 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2834 if (kerr != KCGI_OK)
2837 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2838 KATTR_ID, "dotted_line", KATTR__MAX);
2839 if (kerr != KCGI_OK)
2841 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2842 if (kerr != KCGI_OK)
2846 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2847 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2848 if (kerr != KCGI_OK)
2850 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2851 if (kerr != KCGI_OK)
2853 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2854 if (kerr != KCGI_OK)
2856 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2857 KATTR_ID, "tag_info_date", KATTR__MAX);
2858 if (kerr != KCGI_OK)
2860 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2863 kerr = khtml_puts(gw_trans->gw_html_req,
2865 if (kerr != KCGI_OK)
2867 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2868 if (kerr != KCGI_OK)
2871 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2872 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2873 if (kerr != KCGI_OK)
2875 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2876 if (kerr != KCGI_OK)
2878 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2879 if (kerr != KCGI_OK)
2881 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2882 KATTR_ID, "tag_info_date", KATTR__MAX);
2883 if (kerr != KCGI_OK)
2885 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2886 if (kerr != KCGI_OK)
2888 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2889 if (kerr != KCGI_OK)
2892 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2893 KATTR_ID, "tag_info", KATTR__MAX);
2894 if (kerr != KCGI_OK)
2896 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2897 if (kerr != KCGI_OK)
2903 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2904 if (kerr != KCGI_OK)
2907 if (limit && --limit == 0)
2911 got_object_tag_close(tag);
2924 href_commits = NULL;
2928 got_object_tag_close(tag);
2935 got_ref_list_free(&refs);
2937 got_repo_close(repo);
2938 if (error == NULL && kerr != KCGI_OK)
2939 error = gw_kcgi_error(kerr);
2944 gw_free_headers(struct gw_header *header)
2948 if (header->commit != NULL)
2949 got_object_commit_close(header->commit);
2951 got_repo_close(header->repo);
2952 free(header->refs_str);
2953 free(header->commit_id);
2954 free(header->author);
2955 free(header->committer);
2956 free(header->parent_id);
2957 free(header->tree_id);
2958 free(header->commit_msg);
2961 static struct gw_header *
2964 struct gw_header *header;
2966 header = malloc(sizeof(*header));
2970 header->repo = NULL;
2971 header->commit = NULL;
2973 header->path = NULL;
2974 SIMPLEQ_INIT(&header->refs);
2979 static const struct got_error *
2980 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2983 const struct got_error *error = NULL;
2984 struct got_commit_graph *graph = NULL;
2986 error = got_commit_graph_open(&graph, header->path, 0);
2990 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2996 error = got_commit_graph_iter_next(&header->id, graph,
2997 header->repo, NULL, NULL);
2999 if (error->code == GOT_ERR_ITER_COMPLETED)
3003 if (header->id == NULL)
3006 error = got_object_open_as_commit(&header->commit, header->repo,
3011 error = gw_get_commit(gw_trans, header);
3013 struct gw_header *n_header = NULL;
3014 if ((n_header = gw_init_header()) == NULL) {
3015 error = got_error_from_errno("malloc");
3019 error = gw_strdup_string(&n_header->refs_str,
3020 header->refs_str, NULL);
3023 error = gw_strdup_string(&n_header->commit_id,
3024 header->commit_id, NULL);
3027 error = gw_strdup_string(&n_header->parent_id,
3028 header->parent_id, NULL);
3031 error = gw_strdup_string(&n_header->tree_id,
3032 header->tree_id, NULL);
3035 error = gw_strdup_string(&n_header->author,
3036 header->author, NULL);
3039 error = gw_strdup_string(&n_header->committer,
3040 header->committer, NULL);
3043 error = gw_strdup_string(&n_header->commit_msg,
3044 header->commit_msg, NULL);
3047 n_header->committer_time = header->committer_time;
3048 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3051 if (error || (limit && --limit == 0))
3056 got_commit_graph_close(graph);
3060 static const struct got_error *
3061 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3063 const struct got_error *error = NULL;
3064 struct got_reflist_entry *re;
3065 struct got_object_id *id2 = NULL;
3066 struct got_object_qid *parent_id;
3067 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
3070 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3073 struct got_tag_object *tag = NULL;
3076 name = got_ref_get_name(re->ref);
3077 if (strcmp(name, GOT_REF_HEAD) == 0)
3079 if (strncmp(name, "refs/", 5) == 0)
3081 if (strncmp(name, "got/", 4) == 0)
3083 if (strncmp(name, "heads/", 6) == 0)
3085 if (strncmp(name, "remotes/", 8) == 0)
3087 if (strncmp(name, "tags/", 5) == 0) {
3088 error = got_object_open_as_tag(&tag, header->repo,
3091 if (error->code != GOT_ERR_OBJ_TYPE)
3094 * Ref points at something other
3101 cmp = got_object_id_cmp(tag ?
3102 got_object_tag_get_object_id(tag) : re->id, header->id);
3104 got_object_tag_close(tag);
3108 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
3109 s ? ", " : "", name) == -1) {
3110 error = got_error_from_errno("asprintf");
3114 error = gw_strdup_string(&header->refs_str, refs_str, NULL);
3120 if (refs_str == NULL) {
3121 error = gw_strdup_string(&header->refs_str, "", NULL);
3127 error = got_object_id_str(&header->commit_id, header->id);
3131 error = got_object_id_str(&header->tree_id,
3132 got_object_commit_get_tree_id(header->commit));
3136 if (gw_trans->action == GW_DIFF) {
3137 parent_id = SIMPLEQ_FIRST(
3138 got_object_commit_get_parent_ids(header->commit));
3139 if (parent_id != NULL) {
3140 id2 = got_object_id_dup(parent_id->id);
3142 error = got_object_id_str(&header->parent_id, id2);
3147 error = gw_strdup_string(&header->parent_id,
3153 error = gw_strdup_string(&header->parent_id, "", NULL);
3158 header->committer_time =
3159 got_object_commit_get_committer_time(header->commit);
3161 error = gw_strdup_string(&header->author, NULL,
3162 got_object_commit_get_author(header->commit));
3165 error = gw_strdup_string(&header->committer, NULL,
3166 got_object_commit_get_committer(header->commit));
3169 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3173 commit_msg = commit_msg0;
3174 while (*commit_msg == '\n')
3177 error = gw_strdup_string(&header->commit_msg, commit_msg, NULL);
3182 static const struct got_error *
3183 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3185 const struct got_error *error = NULL;
3186 char *in_repo_path = NULL;
3188 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3192 if (gw_trans->commit == NULL) {
3193 struct got_reference *head_ref;
3194 error = got_ref_open(&head_ref, header->repo,
3195 gw_trans->headref, 0);
3199 error = got_ref_resolve(&header->id, header->repo, head_ref);
3200 got_ref_close(head_ref);
3204 error = got_object_open_as_commit(&header->commit,
3205 header->repo, header->id);
3207 struct got_reference *ref;
3208 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3209 if (error == NULL) {
3211 error = got_ref_resolve(&header->id, header->repo, ref);
3215 error = got_object_get_type(&obj_type, header->repo,
3219 if (obj_type == GOT_OBJ_TYPE_TAG) {
3220 struct got_tag_object *tag;
3221 error = got_object_open_as_tag(&tag,
3222 header->repo, header->id);
3225 if (got_object_tag_get_object_type(tag) !=
3226 GOT_OBJ_TYPE_COMMIT) {
3227 got_object_tag_close(tag);
3228 error = got_error(GOT_ERR_OBJ_TYPE);
3232 header->id = got_object_id_dup(
3233 got_object_tag_get_object_id(tag));
3234 if (header->id == NULL)
3235 error = got_error_from_errno(
3236 "got_object_id_dup");
3237 got_object_tag_close(tag);
3240 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3241 error = got_error(GOT_ERR_OBJ_TYPE);
3244 error = got_object_open_as_commit(&header->commit,
3245 header->repo, header->id);
3249 if (header->commit == NULL) {
3250 error = got_repo_match_object_id_prefix(&header->id,
3251 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3256 error = got_repo_match_object_id_prefix(&header->id,
3257 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3261 error = got_repo_map_path(&in_repo_path, header->repo,
3262 gw_trans->repo_path, 1);
3267 error = gw_strdup_string(&header->path, in_repo_path, NULL);
3275 error = got_ref_list(&header->refs, header->repo, NULL,
3276 got_ref_cmp_by_name, NULL);
3280 error = gw_get_commits(gw_trans, header, limit);
3288 char datebuf[11]; /* YYYY-MM-DD + NUL */
3291 struct gw_blame_cb_args {
3292 struct blame_line *lines;
3296 off_t *line_offsets;
3298 struct got_repository *repo;
3299 struct gw_trans *gw_trans;
3302 static const struct got_error *
3303 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3305 const struct got_error *err = NULL;
3306 struct gw_blame_cb_args *a = arg;
3307 struct blame_line *bline;
3309 size_t linesize = 0;
3310 struct got_commit_object *commit = NULL;
3313 time_t committer_time;
3314 enum kcgi_err kerr = KCGI_OK;
3316 if (nlines != a->nlines ||
3317 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3318 return got_error(GOT_ERR_RANGE);
3321 return NULL; /* no change in this commit */
3323 /* Annotate this line. */
3324 bline = &a->lines[lineno - 1];
3325 if (bline->annotated)
3327 err = got_object_id_str(&bline->id_str, id);
3331 err = got_object_open_as_commit(&commit, a->repo, id);
3335 err = gw_strdup_string(&bline->committer, NULL,
3336 got_object_commit_get_committer(commit));
3340 committer_time = got_object_commit_get_committer_time(commit);
3341 if (localtime_r(&committer_time, &tm) == NULL)
3342 return got_error_from_errno("localtime_r");
3343 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3344 &tm) >= sizeof(bline->datebuf)) {
3345 err = got_error(GOT_ERR_NO_SPACE);
3348 bline->annotated = 1;
3350 /* Print lines annotated so far. */
3351 bline = &a->lines[a->lineno_cur - 1];
3352 if (!bline->annotated)
3355 offset = a->line_offsets[a->lineno_cur - 1];
3356 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3357 err = got_error_from_errno("fseeko");
3361 while (bline->annotated) {
3362 char *smallerthan, *at, *nl, *committer;
3363 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3366 if (getline(&line, &linesize, a->f) == -1) {
3368 err = got_error_from_errno("getline");
3372 committer = bline->committer;
3373 smallerthan = strchr(committer, '<');
3374 if (smallerthan && smallerthan[1] != '\0')
3375 committer = smallerthan + 1;
3376 at = strchr(committer, '@');
3379 len = strlen(committer);
3381 committer[8] = '\0';
3383 nl = strchr(line, '\n');
3387 if (a->gw_trans->repo_folder == NULL) {
3388 err = gw_strdup_string(&a->gw_trans->repo_folder, "",
3393 if (a->gw_trans->repo_folder == NULL)
3396 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3397 "blame_wrapper", KATTR__MAX);
3398 if (kerr != KCGI_OK)
3400 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3401 "blame_number", KATTR__MAX);
3402 if (kerr != KCGI_OK)
3404 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3405 a->lineno_cur) == -1)
3407 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3408 if (kerr != KCGI_OK)
3410 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3411 if (kerr != KCGI_OK)
3414 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3415 "blame_hash", KATTR__MAX);
3416 if (kerr != KCGI_OK)
3418 if (asprintf(&href_diff,
3419 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3420 a->gw_trans->repo_name, bline->id_str,
3421 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3422 err = got_error_from_errno("asprintf");
3425 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3426 err = got_error_from_errno("asprintf");
3429 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3430 KATTR_HREF, href_diff, KATTR__MAX);
3431 if (kerr != KCGI_OK)
3433 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3434 if (kerr != KCGI_OK)
3436 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3437 if (kerr != KCGI_OK)
3440 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3441 "blame_date", KATTR__MAX);
3442 if (kerr != KCGI_OK)
3444 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3445 if (kerr != KCGI_OK)
3447 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3448 if (kerr != KCGI_OK)
3451 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3452 "blame_author", KATTR__MAX);
3453 if (kerr != KCGI_OK)
3455 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3456 if (kerr != KCGI_OK)
3458 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3459 if (kerr != KCGI_OK)
3462 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3463 "blame_code", KATTR__MAX);
3464 if (kerr != KCGI_OK)
3466 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3467 if (kerr != KCGI_OK)
3469 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3470 if (kerr != KCGI_OK)
3473 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3474 if (kerr != KCGI_OK)
3478 bline = &a->lines[a->lineno_cur - 1];
3486 got_object_commit_close(commit);
3488 if (err == NULL && kerr != KCGI_OK)
3489 err = gw_kcgi_error(kerr);
3493 static const struct got_error *
3494 gw_output_file_blame(struct gw_trans *gw_trans)
3496 const struct got_error *error = NULL;
3497 struct got_repository *repo = NULL;
3498 struct got_object_id *obj_id = NULL;
3499 struct got_object_id *commit_id = NULL;
3500 struct got_blob_object *blob = NULL;
3501 char *path = NULL, *in_repo_path = NULL;
3502 struct gw_blame_cb_args bca;
3506 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3510 if (asprintf(&path, "%s%s%s",
3511 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3512 gw_trans->repo_folder ? "/" : "",
3513 gw_trans->repo_file) == -1) {
3514 error = got_error_from_errno("asprintf");
3518 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3522 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3523 GOT_OBJ_TYPE_COMMIT, 1, repo);
3527 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3531 if (obj_id == NULL) {
3532 error = got_error(GOT_ERR_NO_OBJ);
3536 error = got_object_get_type(&obj_type, repo, obj_id);
3540 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3541 error = got_error(GOT_ERR_OBJ_TYPE);
3545 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3549 bca.f = got_opentemp();
3550 if (bca.f == NULL) {
3551 error = got_error_from_errno("got_opentemp");
3554 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3555 &bca.line_offsets, bca.f, blob);
3556 if (error || bca.nlines == 0)
3559 /* Don't include \n at EOF in the blame line count. */
3560 if (bca.line_offsets[bca.nlines - 1] == filesize)
3563 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3564 if (bca.lines == NULL) {
3565 error = got_error_from_errno("calloc");
3569 bca.nlines_prec = 0;
3576 bca.gw_trans = gw_trans;
3578 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3581 free(bca.line_offsets);
3587 for (i = 0; i < bca.nlines; i++) {
3588 struct blame_line *bline = &bca.lines[i];
3589 free(bline->id_str);
3590 free(bline->committer);
3593 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3594 error = got_error_from_errno("fclose");
3596 got_object_blob_close(blob);
3598 got_repo_close(repo);
3602 static const struct got_error *
3603 gw_output_blob_buf(struct gw_trans *gw_trans)
3605 const struct got_error *error = NULL;
3606 struct got_repository *repo = NULL;
3607 struct got_object_id *obj_id = NULL;
3608 struct got_object_id *commit_id = NULL;
3609 struct got_blob_object *blob = NULL;
3610 char *path = NULL, *in_repo_path = NULL;
3611 int obj_type, set_mime = 0;
3614 enum kcgi_err kerr = KCGI_OK;
3616 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3620 if (asprintf(&path, "%s%s%s",
3621 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3622 gw_trans->repo_folder ? "/" : "",
3623 gw_trans->repo_file) == -1) {
3624 error = got_error_from_errno("asprintf");
3628 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3632 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3633 GOT_OBJ_TYPE_COMMIT, 1, repo);
3637 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3641 if (obj_id == NULL) {
3642 error = got_error(GOT_ERR_NO_OBJ);
3646 error = got_object_get_type(&obj_type, repo, obj_id);
3650 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3651 error = got_error(GOT_ERR_OBJ_TYPE);
3655 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3659 hdrlen = got_object_blob_get_hdrlen(blob);
3661 error = got_object_blob_read_block(&len, blob);
3664 buf = got_object_blob_get_read_buf(blob);
3667 * Skip blob object header first time around,
3668 * which also contains a zero byte.
3671 if (set_mime == 0) {
3672 if (isbinary(buf, len - hdrlen))
3673 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3675 gw_trans->mime = KMIME_TEXT_PLAIN;
3677 error = gw_display_index(gw_trans);
3681 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3690 got_object_blob_close(blob);
3692 got_repo_close(repo);
3693 if (error == NULL && kerr != KCGI_OK)
3694 error = gw_kcgi_error(kerr);
3698 static const struct got_error *
3699 gw_output_repo_tree(struct gw_trans *gw_trans)
3701 const struct got_error *error = NULL;
3702 struct got_repository *repo = NULL;
3703 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3704 struct got_tree_object *tree = NULL;
3705 char *path = NULL, *in_repo_path = NULL;
3706 char *id_str = NULL;
3707 char *build_folder = NULL;
3708 char *href_blob = NULL, *href_blame = NULL;
3709 const char *class = NULL;
3710 int nentries, i, class_flip = 0;
3711 enum kcgi_err kerr = KCGI_OK;
3713 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3717 if (gw_trans->repo_folder != NULL) {
3718 error = gw_strdup_string(&path, gw_trans->repo_folder, NULL);
3722 error = got_repo_map_path(&in_repo_path, repo,
3723 gw_trans->repo_path, 1);
3727 path = in_repo_path;
3730 if (gw_trans->commit == NULL) {
3731 struct got_reference *head_ref;
3732 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3735 error = got_ref_resolve(&commit_id, repo, head_ref);
3738 got_ref_close(head_ref);
3741 error = got_repo_match_object_id(&commit_id, NULL,
3742 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3747 error = got_object_id_str(&gw_trans->commit, commit_id);
3751 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3755 error = got_object_open_as_tree(&tree, repo, tree_id);
3759 nentries = got_object_tree_get_nentries(tree);
3760 for (i = 0; i < nentries; i++) {
3761 struct got_tree_entry *te;
3762 const char *modestr = "";
3765 te = got_object_tree_get_entry(tree, i);
3767 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3771 mode = got_tree_entry_get_mode(te);
3772 if (got_object_tree_entry_is_submodule(te))
3774 else if (S_ISLNK(mode))
3776 else if (S_ISDIR(mode))
3778 else if (mode & S_IXUSR)
3781 if (class_flip == 0) {
3782 class = "back_lightgray";
3785 class = "back_white";
3789 if (S_ISDIR(mode)) {
3790 if (asprintf(&build_folder, "%s/%s",
3791 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3792 got_tree_entry_get_name(te)) == -1) {
3793 error = got_error_from_errno(
3797 if (asprintf(&href_blob,
3798 "?path=%s&action=%s&commit=%s&folder=%s",
3799 gw_trans->repo_name, gw_trans->action_name,
3800 gw_trans->commit, build_folder) == -1) {
3801 error = got_error_from_errno("asprintf");
3805 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3806 KATTR_ID, "tree_wrapper", KATTR__MAX);
3807 if (kerr != KCGI_OK)
3809 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3810 KATTR_ID, "tree_line", KATTR_CLASS, class,
3812 if (kerr != KCGI_OK)
3814 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3815 KATTR_HREF, href_blob, KATTR_CLASS,
3816 "diff_directory", KATTR__MAX);
3817 if (kerr != KCGI_OK)
3819 kerr = khtml_puts(gw_trans->gw_html_req,
3820 got_tree_entry_get_name(te));
3821 if (kerr != KCGI_OK)
3823 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3824 if (kerr != KCGI_OK)
3826 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3827 if (kerr != KCGI_OK)
3829 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3830 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3832 if (kerr != KCGI_OK)
3834 kerr = khtml_entity(gw_trans->gw_html_req,
3836 if (kerr != KCGI_OK)
3838 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3839 if (kerr != KCGI_OK)
3842 if (asprintf(&href_blob,
3843 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3844 gw_trans->repo_name, "blob", gw_trans->commit,
3845 got_tree_entry_get_name(te),
3846 gw_trans->repo_folder ?
3847 gw_trans->repo_folder : "") == -1) {
3848 error = got_error_from_errno("asprintf");
3851 if (asprintf(&href_blame,
3852 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3853 gw_trans->repo_name, "blame", gw_trans->commit,
3854 got_tree_entry_get_name(te),
3855 gw_trans->repo_folder ?
3856 gw_trans->repo_folder : "") == -1) {
3857 error = got_error_from_errno("asprintf");
3861 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3862 KATTR_ID, "tree_wrapper", KATTR__MAX);
3863 if (kerr != KCGI_OK)
3865 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3866 KATTR_ID, "tree_line", KATTR_CLASS, class,
3868 if (kerr != KCGI_OK)
3870 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3871 KATTR_HREF, href_blob, KATTR__MAX);
3872 if (kerr != KCGI_OK)
3874 kerr = khtml_puts(gw_trans->gw_html_req,
3875 got_tree_entry_get_name(te));
3876 if (kerr != KCGI_OK)
3878 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3879 if (kerr != KCGI_OK)
3881 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3882 if (kerr != KCGI_OK)
3884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3885 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3887 if (kerr != KCGI_OK)
3890 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3891 KATTR_HREF, href_blob, KATTR__MAX);
3892 if (kerr != KCGI_OK)
3894 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3895 if (kerr != KCGI_OK)
3897 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3898 if (kerr != KCGI_OK)
3901 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3902 if (kerr != KCGI_OK)
3905 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3906 KATTR_HREF, href_blame, KATTR__MAX);
3907 if (kerr != KCGI_OK)
3909 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3910 if (kerr != KCGI_OK)
3913 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3914 if (kerr != KCGI_OK)
3922 build_folder = NULL;
3926 got_object_tree_close(tree);
3928 got_repo_close(repo);
3935 if (error == NULL && kerr != KCGI_OK)
3936 error = gw_kcgi_error(kerr);
3940 static const struct got_error *
3941 gw_output_repo_heads(struct gw_trans *gw_trans)
3943 const struct got_error *error = NULL;
3944 struct got_repository *repo = NULL;
3945 struct got_reflist_head refs;
3946 struct got_reflist_entry *re;
3947 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3948 char *href_commits = NULL;
3949 enum kcgi_err kerr = KCGI_OK;
3951 SIMPLEQ_INIT(&refs);
3953 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3957 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3962 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3963 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3964 if (kerr != KCGI_OK)
3966 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3967 KATTR_ID, "summary_heads_title", KATTR__MAX);
3968 if (kerr != KCGI_OK)
3970 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3971 if (kerr != KCGI_OK)
3973 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3974 if (kerr != KCGI_OK)
3976 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3977 KATTR_ID, "summary_heads_content", KATTR__MAX);
3978 if (kerr != KCGI_OK)
3981 SIMPLEQ_FOREACH(re, &refs, entry) {
3984 error = gw_strdup_string(&refname, NULL,
3985 got_ref_get_name(re->ref));
3989 if (strncmp(refname, "refs/heads/", 11) != 0) {
3994 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3999 if (strncmp(refname, "refs/heads/", 11) == 0)
4002 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4003 KATTR_ID, "heads_wrapper", KATTR__MAX);
4004 if (kerr != KCGI_OK)
4006 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4007 KATTR_ID, "heads_age", KATTR__MAX);
4008 if (kerr != KCGI_OK)
4010 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4011 if (kerr != KCGI_OK)
4013 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4014 if (kerr != KCGI_OK)
4016 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4017 KATTR_ID, "heads_space", KATTR__MAX);
4018 if (kerr != KCGI_OK)
4020 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4021 if (kerr != KCGI_OK)
4023 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4024 if (kerr != KCGI_OK)
4026 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4027 KATTR_ID, "head", KATTR__MAX);
4028 if (kerr != KCGI_OK)
4030 if (asprintf(&href_summary,
4031 "?path=%s&action=summary&headref=%s",
4032 gw_trans->repo_name, refname) == -1) {
4033 error = got_error_from_errno("asprintf");
4036 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4037 href_summary, KATTR__MAX);
4038 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4039 if (kerr != KCGI_OK)
4041 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4042 if (kerr != KCGI_OK)
4045 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4046 "navs_wrapper", KATTR__MAX);
4047 if (kerr != KCGI_OK)
4049 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4050 "navs", KATTR__MAX);
4051 if (kerr != KCGI_OK)
4054 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4055 href_summary, KATTR__MAX);
4056 if (kerr != KCGI_OK)
4058 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4059 if (kerr != KCGI_OK)
4061 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4062 if (kerr != KCGI_OK)
4065 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4066 if (kerr != KCGI_OK)
4068 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4069 gw_trans->repo_name, refname) == -1) {
4070 error = got_error_from_errno("asprintf");
4073 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4074 href_briefs, KATTR__MAX);
4075 if (kerr != KCGI_OK)
4077 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4078 if (kerr != KCGI_OK)
4080 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4081 if (kerr != KCGI_OK)
4084 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4085 if (kerr != KCGI_OK)
4088 if (asprintf(&href_commits,
4089 "?path=%s&action=commits&headref=%s",
4090 gw_trans->repo_name, refname) == -1) {
4091 error = got_error_from_errno("asprintf");
4094 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4095 href_commits, KATTR__MAX);
4096 if (kerr != KCGI_OK)
4098 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4099 if (kerr != KCGI_OK)
4101 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4102 if (kerr != KCGI_OK)
4105 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4106 "dotted_line", KATTR__MAX);
4107 if (kerr != KCGI_OK)
4109 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4110 if (kerr != KCGI_OK)
4113 href_summary = NULL;
4117 href_commits = NULL;
4120 got_ref_list_free(&refs);
4125 got_repo_close(repo);
4129 static const struct got_error *
4130 gw_output_site_link(struct gw_trans *gw_trans)
4132 const struct got_error *error = NULL;
4133 char *href_summary = NULL;
4134 enum kcgi_err kerr = KCGI_OK;
4136 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4137 "site_link", KATTR__MAX);
4138 if (kerr != KCGI_OK)
4140 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4142 if (kerr != KCGI_OK)
4144 kerr = khtml_puts(gw_trans->gw_html_req,
4145 gw_trans->gw_conf->got_site_link);
4146 if (kerr != KCGI_OK)
4148 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4149 if (kerr != KCGI_OK)
4152 if (gw_trans->repo_name != NULL) {
4153 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4154 if (kerr != KCGI_OK)
4156 if (asprintf(&href_summary, "?path=%s&action=summary",
4157 gw_trans->repo_name) == -1)
4159 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4160 href_summary, KATTR__MAX);
4161 if (kerr != KCGI_OK)
4163 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4164 if (kerr != KCGI_OK)
4166 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4167 if (kerr != KCGI_OK)
4169 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4170 if (kerr != KCGI_OK)
4172 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->action_name);
4173 if (kerr != KCGI_OK)
4177 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4178 if (kerr != KCGI_OK)
4185 static const struct got_error *
4186 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4188 const struct got_error *error = NULL;
4190 enum kcgi_err kerr = KCGI_OK;
4192 if (strncmp(buf, "-", 1) == 0)
4193 color = "diff_minus";
4194 else if (strncmp(buf, "+", 1) == 0)
4195 color = "diff_plus";
4196 else if (strncmp(buf, "@@", 2) == 0)
4197 color = "diff_chunk_header";
4198 else if (strncmp(buf, "@@", 2) == 0)
4199 color = "diff_chunk_header";
4200 else if (strncmp(buf, "commit +", 8) == 0)
4201 color = "diff_meta";
4202 else if (strncmp(buf, "commit -", 8) == 0)
4203 color = "diff_meta";
4204 else if (strncmp(buf, "blob +", 6) == 0)
4205 color = "diff_meta";
4206 else if (strncmp(buf, "blob -", 6) == 0)
4207 color = "diff_meta";
4208 else if (strncmp(buf, "file +", 6) == 0)
4209 color = "diff_meta";
4210 else if (strncmp(buf, "file -", 6) == 0)
4211 color = "diff_meta";
4212 else if (strncmp(buf, "from:", 5) == 0)
4213 color = "diff_author";
4214 else if (strncmp(buf, "via:", 4) == 0)
4215 color = "diff_author";
4216 else if (strncmp(buf, "date:", 5) == 0)
4217 color = "diff_date";
4218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4219 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4220 if (error == NULL && kerr != KCGI_OK)
4221 error = gw_kcgi_error(kerr);
4226 main(int argc, char *argv[])
4228 const struct got_error *error = NULL;
4229 struct gw_trans *gw_trans;
4230 struct gw_dir *dir = NULL, *tdir;
4231 const char *page = "index";
4235 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4238 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4241 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4244 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4247 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4248 if (kerr != KCGI_OK) {
4249 error = gw_kcgi_error(kerr);
4253 if ((gw_trans->gw_conf =
4254 malloc(sizeof(struct gotweb_conf))) == NULL) {
4256 error = got_error_from_errno("malloc");
4260 TAILQ_INIT(&gw_trans->gw_dirs);
4261 TAILQ_INIT(&gw_trans->gw_headers);
4264 gw_trans->repos_total = 0;
4265 gw_trans->repo_path = NULL;
4266 gw_trans->commit = NULL;
4267 error = gw_strdup_string(&gw_trans->headref, GOT_REF_HEAD, NULL);
4270 gw_trans->mime = KMIME_TEXT_HTML;
4271 gw_trans->gw_tmpl->key = gw_templs;
4272 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4273 gw_trans->gw_tmpl->arg = gw_trans;
4274 gw_trans->gw_tmpl->cb = gw_template;
4275 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4279 error = gw_parse_querystring(gw_trans);
4283 if (gw_trans->action == GW_BLOB)
4284 error = gw_blob(gw_trans);
4286 error = gw_display_index(gw_trans);
4289 gw_trans->mime = KMIME_TEXT_PLAIN;
4290 gw_trans->action = GW_ERR;
4291 gw_display_error(gw_trans, error);
4294 free(gw_trans->gw_conf->got_repos_path);
4295 free(gw_trans->gw_conf->got_site_name);
4296 free(gw_trans->gw_conf->got_site_owner);
4297 free(gw_trans->gw_conf->got_site_link);
4298 free(gw_trans->gw_conf->got_logo);
4299 free(gw_trans->gw_conf->got_logo_url);
4300 free(gw_trans->gw_conf);
4301 free(gw_trans->commit);
4302 free(gw_trans->repo_path);
4303 free(gw_trans->repo_name);
4304 free(gw_trans->repo_file);
4305 free(gw_trans->action_name);
4306 free(gw_trans->headref);
4308 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4310 free(dir->description);
4319 khttp_free(gw_trans->gw_req);