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 got_repository *repo;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
64 const struct got_error *error;
65 const char *repo_name;
68 const char *repo_file;
73 unsigned int repos_total;
78 TAILQ_ENTRY(gw_header) entry;
79 struct got_reflist_head refs;
83 char *commit_id; /* id_str1 */
84 char *parent_id; /* id_str2 */
89 time_t committer_time;
93 TAILQ_ENTRY(gw_dir) entry;
134 static const char *const gw_templs[TEMPL__MAX] = {
144 static const struct kvalid gw_keys[KEY__ZMAX] = {
145 { kvalid_stringne, "action" },
146 { kvalid_stringne, "commit" },
147 { kvalid_stringne, "file" },
148 { kvalid_stringne, "folder" },
149 { kvalid_stringne, "headref" },
150 { kvalid_int, "page" },
151 { kvalid_stringne, "path" },
154 static struct gw_header *gw_init_header(void);
156 static void gw_free_header(struct gw_header *);
158 static int gw_template(size_t, void *);
160 static const struct got_error *gw_error(struct gw_trans *);
161 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
162 static const struct got_error *gw_get_repo_description(char **,
163 struct gw_trans *, char *);
164 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
166 static const struct got_error *gw_get_time_str(char **, time_t, int);
167 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
168 char *, const char *, int);
169 static const struct got_error *gw_output_file_blame(struct gw_trans *);
170 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
171 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
172 static const struct got_error *gw_output_diff(struct gw_trans *,
174 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
175 struct gw_header *, int, int);
176 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
177 static const struct got_error *gw_output_site_link(struct gw_trans *);
178 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
180 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
182 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
184 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
186 static const struct got_error *gw_gen_author_header(struct gw_trans *,
188 static const struct got_error *gw_gen_age_header(struct gw_trans *,
190 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
192 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
194 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
195 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
197 static const struct got_error *gw_display_index(struct gw_trans *);
198 static const struct got_error *gw_get_header(struct gw_trans *,
199 struct gw_header *, int);
200 static const struct got_error *gw_get_commits(struct gw_trans *,
201 struct gw_header *, int,
202 struct got_object_id *);
203 static const struct got_error *gw_get_commit(struct gw_trans *,
205 struct got_commit_object *,
206 struct got_object_id *);
207 static const struct got_error *gw_apply_unveil(const char *);
208 static const struct got_error *gw_blame_cb(void *, int, int,
209 struct got_object_id *);
210 static const struct got_error *gw_load_got_paths(struct gw_trans *);
211 static const struct got_error *gw_load_got_path(struct gw_trans *,
213 static const struct got_error *gw_parse_querystring(struct gw_trans *);
214 static const struct got_error *gw_blame(struct gw_trans *);
215 static const struct got_error *gw_blob(struct gw_trans *);
216 static const struct got_error *gw_diff(struct gw_trans *);
217 static const struct got_error *gw_index(struct gw_trans *);
218 static const struct got_error *gw_commits(struct gw_trans *);
219 static const struct got_error *gw_briefs(struct gw_trans *);
220 static const struct got_error *gw_summary(struct gw_trans *);
221 static const struct got_error *gw_tree(struct gw_trans *);
222 static const struct got_error *gw_tag(struct gw_trans *);
224 struct gw_query_action {
225 unsigned int func_id;
226 const char *func_name;
227 const struct got_error *(*func_main)(struct gw_trans *);
231 enum gw_query_actions {
244 static struct gw_query_action gw_query_funcs[] = {
245 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
246 { GW_BLOB, "blob", NULL, NULL },
247 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
248 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
249 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
250 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
251 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
252 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
253 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
254 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
258 gw_get_action_name(struct gw_trans *gw_trans)
260 return gw_query_funcs[gw_trans->action].func_name;
263 static const struct got_error *
264 gw_kcgi_error(enum kcgi_err kerr)
269 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
270 return got_error(GOT_ERR_CANCELLED);
272 if (kerr == KCGI_ENOMEM)
273 return got_error_set_errno(ENOMEM,
274 kcgi_strerror(kerr));
276 if (kerr == KCGI_ENFILE)
277 return got_error_set_errno(ENFILE,
278 kcgi_strerror(kerr));
280 if (kerr == KCGI_EAGAIN)
281 return got_error_set_errno(EAGAIN,
282 kcgi_strerror(kerr));
284 if (kerr == KCGI_FORM)
285 return got_error_msg(GOT_ERR_IO,
286 kcgi_strerror(kerr));
288 return got_error_from_errno(kcgi_strerror(kerr));
291 static const struct got_error *
292 gw_apply_unveil(const char *repo_path)
294 const struct got_error *err;
296 if (repo_path && unveil(repo_path, "r") != 0)
297 return got_error_from_errno2("unveil", repo_path);
299 if (unveil("/tmp", "rwc") != 0)
300 return got_error_from_errno2("unveil", "/tmp");
302 err = got_privsep_unveil_exec_helpers();
306 if (unveil(NULL, NULL) != 0)
307 return got_error_from_errno("unveil");
313 isbinary(const uint8_t *buf, size_t n)
317 for (i = 0; i < n; i++)
323 static const struct got_error *
324 gw_blame(struct gw_trans *gw_trans)
326 const struct got_error *error = NULL;
327 struct gw_header *header = NULL;
331 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
333 return got_error_from_errno("pledge");
335 if ((header = gw_init_header()) == NULL)
336 return got_error_from_errno("malloc");
338 error = gw_apply_unveil(gw_trans->gw_dir->path);
342 error = gw_get_header(gw_trans, header, 1);
346 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
347 "blame_header_wrapper", KATTR__MAX);
350 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
351 "blame_header", KATTR__MAX);
354 error = gw_get_time_str(&age, header->committer_time,
358 error = gw_gen_age_header(gw_trans, age ?age : "");
361 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
364 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
367 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
368 "dotted_line", KATTR__MAX);
371 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
375 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
376 "blame", KATTR__MAX);
379 error = gw_output_file_blame(gw_trans);
382 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
384 gw_free_header(header);
385 if (error == NULL && kerr != KCGI_OK)
386 error = gw_kcgi_error(kerr);
390 static const struct got_error *
391 gw_blob(struct gw_trans *gw_trans)
393 const struct got_error *error = NULL;
394 struct gw_header *header = NULL;
396 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
398 return got_error_from_errno("pledge");
400 if ((header = gw_init_header()) == NULL)
401 return got_error_from_errno("malloc");
403 error = gw_apply_unveil(gw_trans->gw_dir->path);
407 error = gw_get_header(gw_trans, header, 1);
411 error = gw_output_blob_buf(gw_trans);
413 gw_free_header(header);
417 static const struct got_error *
418 gw_diff(struct gw_trans *gw_trans)
420 const struct got_error *error = NULL;
421 struct gw_header *header = NULL;
423 enum kcgi_err kerr = KCGI_OK;
425 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
427 return got_error_from_errno("pledge");
429 if ((header = gw_init_header()) == NULL)
430 return got_error_from_errno("malloc");
432 error = gw_apply_unveil(gw_trans->gw_dir->path);
436 error = gw_get_header(gw_trans, header, 1);
440 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
441 "diff_header_wrapper", KATTR__MAX);
444 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
445 "diff_header", KATTR__MAX);
448 error = gw_gen_diff_header(gw_trans, header->parent_id,
452 error = gw_gen_commit_header(gw_trans, header->commit_id,
456 error = gw_gen_tree_header(gw_trans, header->tree_id);
459 error = gw_gen_author_header(gw_trans, header->author);
462 error = gw_gen_committer_header(gw_trans, header->author);
465 error = gw_get_time_str(&age, header->committer_time,
469 error = gw_gen_age_header(gw_trans, age ?age : "");
472 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
475 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
478 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
479 "dotted_line", KATTR__MAX);
482 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
486 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
490 error = gw_output_diff(gw_trans, header);
494 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
496 gw_free_header(header);
498 if (error == NULL && kerr != KCGI_OK)
499 error = gw_kcgi_error(kerr);
503 static const struct got_error *
504 gw_index(struct gw_trans *gw_trans)
506 const struct got_error *error = NULL;
507 struct gw_dir *gw_dir = NULL;
508 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
509 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
510 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
513 if (pledge("stdio rpath proc exec sendfd unveil",
515 error = got_error_from_errno("pledge");
519 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
523 error = gw_load_got_paths(gw_trans);
527 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
528 "index_header", KATTR__MAX);
530 return gw_kcgi_error(kerr);
531 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
532 "index_header_project", KATTR__MAX);
534 return gw_kcgi_error(kerr);
535 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
537 return gw_kcgi_error(kerr);
538 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
540 return gw_kcgi_error(kerr);
542 if (gw_trans->gw_conf->got_show_repo_description) {
543 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
544 "index_header_description", KATTR__MAX);
546 return gw_kcgi_error(kerr);
547 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
549 return gw_kcgi_error(kerr);
550 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
552 return gw_kcgi_error(kerr);
555 if (gw_trans->gw_conf->got_show_repo_owner) {
556 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
557 "index_header_owner", KATTR__MAX);
559 return gw_kcgi_error(kerr);
560 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
562 return gw_kcgi_error(kerr);
563 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
565 return gw_kcgi_error(kerr);
568 if (gw_trans->gw_conf->got_show_repo_age) {
569 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
570 "index_header_age", KATTR__MAX);
572 return gw_kcgi_error(kerr);
573 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
575 return gw_kcgi_error(kerr);
576 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
578 return gw_kcgi_error(kerr);
581 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
583 return gw_kcgi_error(kerr);
585 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
586 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
587 "index_wrapper", KATTR__MAX);
589 return gw_kcgi_error(kerr);
590 kerr = khtml_puts(gw_trans->gw_html_req,
591 "No repositories found in ");
593 return gw_kcgi_error(kerr);
594 kerr = khtml_puts(gw_trans->gw_html_req,
595 gw_trans->gw_conf->got_repos_path);
597 return gw_kcgi_error(kerr);
598 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
600 return gw_kcgi_error(kerr);
601 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
602 "dotted_line", KATTR__MAX);
604 return gw_kcgi_error(kerr);
605 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
607 return gw_kcgi_error(kerr);
611 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
614 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
615 if (gw_trans->page > 0 && (gw_trans->page *
616 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
623 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
624 "index_wrapper", KATTR__MAX);
628 if (asprintf(&href_summary, "?path=%s&action=summary",
629 gw_dir->name) == -1) {
630 error = got_error_from_errno("asprintf");
633 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
634 "index_project", KATTR__MAX);
637 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
638 href_summary, KATTR__MAX);
641 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
644 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
647 if (gw_trans->gw_conf->got_show_repo_description) {
648 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
649 KATTR_ID, "index_project_description", KATTR__MAX);
652 kerr = khtml_puts(gw_trans->gw_html_req,
653 gw_dir->description ? gw_dir->description : "");
656 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
660 if (gw_trans->gw_conf->got_show_repo_owner) {
661 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
662 KATTR_ID, "index_project_owner", KATTR__MAX);
665 kerr = khtml_puts(gw_trans->gw_html_req,
666 gw_dir->owner ? gw_dir->owner : "");
669 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
673 if (gw_trans->gw_conf->got_show_repo_age) {
674 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
675 KATTR_ID, "index_project_age", KATTR__MAX);
678 kerr = khtml_puts(gw_trans->gw_html_req,
679 gw_dir->age ? gw_dir->age : "");
682 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
687 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
688 "navs_wrapper", KATTR__MAX);
691 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
696 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
697 href_summary, KATTR__MAX);
700 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
703 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
707 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
711 if (asprintf(&href_briefs, "?path=%s&action=briefs",
712 gw_dir->name) == -1) {
713 error = got_error_from_errno("asprintf");
716 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
717 href_briefs, KATTR__MAX);
720 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
723 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
725 error = gw_kcgi_error(kerr);
727 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
731 if (asprintf(&href_commits, "?path=%s&action=commits",
732 gw_dir->name) == -1) {
733 error = got_error_from_errno("asprintf");
736 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
737 href_commits, KATTR__MAX);
740 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
743 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
747 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
751 if (asprintf(&href_tree, "?path=%s&action=tree",
752 gw_dir->name) == -1) {
753 error = got_error_from_errno("asprintf");
756 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
757 href_tree, KATTR__MAX);
760 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
764 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
767 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
768 "dotted_line", KATTR__MAX);
771 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
784 if (gw_trans->gw_conf->got_max_repos_display == 0)
787 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
788 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
789 KATTR_ID, "np_wrapper", KATTR__MAX);
792 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
793 KATTR_ID, "nav_prev", KATTR__MAX);
796 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
797 (gw_trans->page > 0) &&
798 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
799 prev_disp == gw_trans->repos_total)) {
800 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
801 KATTR_ID, "np_wrapper", KATTR__MAX);
804 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
805 KATTR_ID, "nav_prev", KATTR__MAX);
810 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 if (asprintf(&href_prev, "?page=%d",
815 gw_trans->page - 1) == -1) {
816 error = got_error_from_errno("asprintf");
819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
820 KATTR_HREF, href_prev, KATTR__MAX);
823 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
826 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
831 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
833 return gw_kcgi_error(kerr);
835 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
836 next_disp == gw_trans->gw_conf->got_max_repos_display &&
837 dir_c != (gw_trans->page + 1) *
838 gw_trans->gw_conf->got_max_repos_display) {
839 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
840 KATTR_ID, "nav_next", KATTR__MAX);
843 if (asprintf(&href_next, "?page=%d",
844 gw_trans->page + 1) == -1) {
845 error = got_error_from_errno("calloc");
848 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
849 KATTR_HREF, href_next, KATTR__MAX);
852 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
855 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
862 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
863 (gw_trans->page > 0) &&
864 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
865 prev_disp == gw_trans->repos_total)) {
866 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
879 if (error == NULL && kerr != KCGI_OK)
880 error = gw_kcgi_error(kerr);
884 static const struct got_error *
885 gw_commits(struct gw_trans *gw_trans)
887 const struct got_error *error = NULL;
888 struct gw_header *header = NULL, *n_header = NULL;
890 char *href_diff = NULL, *href_blob = NULL;
891 enum kcgi_err kerr = KCGI_OK;
893 if ((header = gw_init_header()) == NULL)
894 return got_error_from_errno("malloc");
896 if (pledge("stdio rpath proc exec sendfd unveil",
898 error = got_error_from_errno("pledge");
902 error = gw_apply_unveil(gw_trans->gw_dir->path);
906 error = gw_get_header(gw_trans, header,
907 gw_trans->gw_conf->got_max_commits_display);
911 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
912 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
913 "commits_line_wrapper", KATTR__MAX);
916 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
920 error = gw_gen_author_header(gw_trans, n_header->author);
923 error = gw_gen_committer_header(gw_trans, n_header->author);
926 error = gw_get_time_str(&age, n_header->committer_time,
930 error = gw_gen_age_header(gw_trans, age ?age : "");
933 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
938 "dotted_line", KATTR__MAX);
941 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
945 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
946 "commit", KATTR__MAX);
949 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
952 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
956 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
957 gw_trans->repo_name, n_header->commit_id) == -1) {
958 error = got_error_from_errno("asprintf");
961 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
962 KATTR_ID, "navs_wrapper", KATTR__MAX);
965 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
966 KATTR_ID, "navs", KATTR__MAX);
969 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
970 KATTR_HREF, href_diff, KATTR__MAX);
973 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
976 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
980 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
984 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
985 gw_trans->repo_name, n_header->commit_id) == -1) {
986 error = got_error_from_errno("asprintf");
989 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
990 KATTR_HREF, href_blob, KATTR__MAX);
993 khtml_puts(gw_trans->gw_html_req, "tree");
994 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
997 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1002 "solid_line", KATTR__MAX);
1003 if (kerr != KCGI_OK)
1005 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1006 if (kerr != KCGI_OK)
1013 gw_free_header(header);
1014 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1015 gw_free_header(n_header);
1019 if (error == NULL && kerr != KCGI_OK)
1020 error = gw_kcgi_error(kerr);
1024 static const struct got_error *
1025 gw_briefs(struct gw_trans *gw_trans)
1027 const struct got_error *error = NULL;
1028 struct gw_header *header = NULL, *n_header = NULL;
1030 char *href_diff = NULL, *href_blob = NULL;
1031 char *newline, *smallerthan;
1032 enum kcgi_err kerr = KCGI_OK;
1034 if ((header = gw_init_header()) == NULL)
1035 return got_error_from_errno("malloc");
1037 if (pledge("stdio rpath proc exec sendfd unveil",
1039 error = got_error_from_errno("pledge");
1043 error = gw_apply_unveil(gw_trans->gw_dir->path);
1047 if (gw_trans->action == GW_SUMMARY)
1048 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1050 error = gw_get_header(gw_trans, header,
1051 gw_trans->gw_conf->got_max_commits_display);
1055 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1056 error = gw_get_time_str(&age, n_header->committer_time,
1061 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1062 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1063 if (kerr != KCGI_OK)
1066 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1067 KATTR_ID, "briefs_age", KATTR__MAX);
1068 if (kerr != KCGI_OK)
1070 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1071 if (kerr != KCGI_OK)
1073 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1074 if (kerr != KCGI_OK)
1077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1078 KATTR_ID, "briefs_author", KATTR__MAX);
1079 if (kerr != KCGI_OK)
1081 smallerthan = strchr(n_header->author, '<');
1083 *smallerthan = '\0';
1084 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1085 if (kerr != KCGI_OK)
1087 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1088 if (kerr != KCGI_OK)
1091 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1092 gw_trans->repo_name, n_header->commit_id) == -1) {
1093 error = got_error_from_errno("asprintf");
1096 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1097 KATTR_ID, "briefs_log", KATTR__MAX);
1098 if (kerr != KCGI_OK)
1100 newline = strchr(n_header->commit_msg, '\n');
1103 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1104 KATTR_HREF, href_diff, KATTR__MAX);
1105 if (kerr != KCGI_OK)
1107 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1108 if (kerr != KCGI_OK)
1110 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1111 if (kerr != KCGI_OK)
1114 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1115 KATTR_ID, "navs_wrapper", KATTR__MAX);
1116 if (kerr != KCGI_OK)
1118 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1119 KATTR_ID, "navs", KATTR__MAX);
1120 if (kerr != KCGI_OK)
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, "diff");
1127 if (kerr != KCGI_OK)
1129 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1130 if (kerr != KCGI_OK)
1133 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1134 if (kerr != KCGI_OK)
1137 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1138 gw_trans->repo_name, n_header->commit_id) == -1) {
1139 error = got_error_from_errno("asprintf");
1142 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1143 KATTR_HREF, href_blob, KATTR__MAX);
1144 if (kerr != KCGI_OK)
1146 khtml_puts(gw_trans->gw_html_req, "tree");
1147 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1148 if (kerr != KCGI_OK)
1150 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1151 if (kerr != KCGI_OK)
1154 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1155 KATTR_ID, "dotted_line", KATTR__MAX);
1156 if (kerr != KCGI_OK)
1158 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1159 if (kerr != KCGI_OK)
1170 gw_free_header(header);
1171 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1172 gw_free_header(n_header);
1176 if (error == NULL && kerr != KCGI_OK)
1177 error = gw_kcgi_error(kerr);
1181 static const struct got_error *
1182 gw_summary(struct gw_trans *gw_trans)
1184 const struct got_error *error = NULL;
1186 enum kcgi_err kerr = KCGI_OK;
1188 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1189 return got_error_from_errno("pledge");
1191 /* unveil is applied with gw_briefs below */
1193 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1194 "summary_wrapper", KATTR__MAX);
1195 if (kerr != KCGI_OK)
1196 return gw_kcgi_error(kerr);
1198 if (gw_trans->gw_conf->got_show_repo_description &&
1199 gw_trans->gw_dir->description != NULL &&
1200 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1201 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1202 KATTR_ID, "description_title", KATTR__MAX);
1203 if (kerr != KCGI_OK)
1205 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1206 if (kerr != KCGI_OK)
1208 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1209 if (kerr != KCGI_OK)
1211 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1212 KATTR_ID, "description", KATTR__MAX);
1213 if (kerr != KCGI_OK)
1215 kerr = khtml_puts(gw_trans->gw_html_req,
1216 gw_trans->gw_dir->description);
1217 if (kerr != KCGI_OK)
1219 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1220 if (kerr != KCGI_OK)
1224 if (gw_trans->gw_conf->got_show_repo_owner &&
1225 gw_trans->gw_dir->owner != NULL &&
1226 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1227 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1228 KATTR_ID, "repo_owner_title", KATTR__MAX);
1229 if (kerr != KCGI_OK)
1231 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1232 if (kerr != KCGI_OK)
1234 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1235 if (kerr != KCGI_OK)
1237 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1238 KATTR_ID, "repo_owner", KATTR__MAX);
1239 if (kerr != KCGI_OK)
1241 kerr = khtml_puts(gw_trans->gw_html_req,
1242 gw_trans->gw_dir->owner);
1243 if (kerr != KCGI_OK)
1245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1246 if (kerr != KCGI_OK)
1250 if (gw_trans->gw_conf->got_show_repo_age) {
1251 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1256 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1257 KATTR_ID, "last_change_title", KATTR__MAX);
1258 if (kerr != KCGI_OK)
1260 kerr = khtml_puts(gw_trans->gw_html_req,
1262 if (kerr != KCGI_OK)
1264 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1265 if (kerr != KCGI_OK)
1267 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1268 KATTR_ID, "last_change", KATTR__MAX);
1269 if (kerr != KCGI_OK)
1271 kerr = khtml_puts(gw_trans->gw_html_req, age);
1272 if (kerr != KCGI_OK)
1274 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1275 if (kerr != KCGI_OK)
1280 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1281 gw_trans->gw_dir->url != NULL &&
1282 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1283 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1284 KATTR_ID, "cloneurl_title", KATTR__MAX);
1285 if (kerr != KCGI_OK)
1287 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1288 if (kerr != KCGI_OK)
1290 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1291 if (kerr != KCGI_OK)
1293 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1294 KATTR_ID, "cloneurl", KATTR__MAX);
1295 if (kerr != KCGI_OK)
1297 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1298 if (kerr != KCGI_OK)
1300 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1301 if (kerr != KCGI_OK)
1305 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1306 if (kerr != KCGI_OK)
1309 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1310 "briefs_title_wrapper", KATTR__MAX);
1311 if (kerr != KCGI_OK)
1313 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1314 "briefs_title", KATTR__MAX);
1315 if (kerr != KCGI_OK)
1317 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1318 if (kerr != KCGI_OK)
1320 if (gw_trans->headref) {
1321 kerr = khtml_puts(gw_trans->gw_html_req, " (");
1322 if (kerr != KCGI_OK)
1324 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->headref);
1325 if (kerr != KCGI_OK)
1327 kerr = khtml_puts(gw_trans->gw_html_req, ")");
1328 if (kerr != KCGI_OK)
1331 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1332 if (kerr != KCGI_OK)
1334 error = gw_briefs(gw_trans);
1338 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1343 error = gw_output_repo_heads(gw_trans);
1346 if (error == NULL && kerr != KCGI_OK)
1347 error = gw_kcgi_error(kerr);
1351 static const struct got_error *
1352 gw_tree(struct gw_trans *gw_trans)
1354 const struct got_error *error = NULL;
1355 struct gw_header *header = NULL;
1356 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1360 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1361 return got_error_from_errno("pledge");
1363 if ((header = gw_init_header()) == NULL)
1364 return got_error_from_errno("malloc");
1366 error = gw_apply_unveil(gw_trans->gw_dir->path);
1370 error = gw_get_header(gw_trans, header, 1);
1374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1375 "tree_header_wrapper", KATTR__MAX);
1376 if (kerr != KCGI_OK)
1378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1379 "tree_header", KATTR__MAX);
1380 if (kerr != KCGI_OK)
1382 error = gw_gen_tree_header(gw_trans, header->tree_id);
1385 error = gw_get_time_str(&age, header->committer_time,
1389 error = gw_gen_age_header(gw_trans, age ?age : "");
1392 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1395 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1396 if (kerr != KCGI_OK)
1398 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1399 "dotted_line", KATTR__MAX);
1400 if (kerr != KCGI_OK)
1402 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1403 if (kerr != KCGI_OK)
1406 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1407 "tree", KATTR__MAX);
1408 if (kerr != KCGI_OK)
1410 error = gw_output_repo_tree(gw_trans);
1414 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1416 gw_free_header(header);
1417 free(tree_html_disp);
1421 if (error == NULL && kerr != KCGI_OK)
1422 error = gw_kcgi_error(kerr);
1426 static const struct got_error *
1427 gw_tag(struct gw_trans *gw_trans)
1429 const struct got_error *error = NULL;
1430 struct gw_header *header = NULL;
1433 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1434 return got_error_from_errno("pledge");
1436 if ((header = gw_init_header()) == NULL)
1437 return got_error_from_errno("malloc");
1439 error = gw_apply_unveil(gw_trans->gw_dir->path);
1443 error = gw_get_header(gw_trans, header, 1);
1447 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1448 "tag_header_wrapper", KATTR__MAX);
1449 if (kerr != KCGI_OK)
1451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1452 "tag_header", KATTR__MAX);
1453 if (kerr != KCGI_OK)
1455 error = gw_gen_commit_header(gw_trans, header->commit_id,
1459 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1462 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1463 if (kerr != KCGI_OK)
1465 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1466 "dotted_line", KATTR__MAX);
1467 if (kerr != KCGI_OK)
1469 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1470 if (kerr != KCGI_OK)
1473 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1474 "tree", KATTR__MAX);
1475 if (kerr != KCGI_OK)
1478 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1482 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1484 gw_free_header(header);
1485 if (error == NULL && kerr != KCGI_OK)
1486 error = gw_kcgi_error(kerr);
1490 static const struct got_error *
1491 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1493 const struct got_error *error = NULL;
1498 if (asprintf(&dir_test, "%s/%s/%s",
1499 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1500 GOTWEB_GIT_DIR) == -1)
1501 return got_error_from_errno("asprintf");
1503 dt = opendir(dir_test);
1507 gw_dir->path = strdup(dir_test);
1508 if (gw_dir->path == NULL) {
1510 error = got_error_from_errno("strdup");
1517 if (asprintf(&dir_test, "%s/%s/%s",
1518 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1519 GOTWEB_GOT_DIR) == -1) {
1521 error = got_error_from_errno("asprintf");
1525 dt = opendir(dir_test);
1530 error = got_error(GOT_ERR_NOT_GIT_REPO);
1534 if (asprintf(&dir_test, "%s/%s",
1535 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1536 error = got_error_from_errno("asprintf");
1541 gw_dir->path = strdup(dir_test);
1542 if (gw_dir->path == NULL) {
1544 error = got_error_from_errno("strdup");
1548 dt = opendir(dir_test);
1550 error = got_error_from_errno2("bad path", dir_test);
1555 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1559 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1562 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1566 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1574 static const struct got_error *
1575 gw_load_got_paths(struct gw_trans *gw_trans)
1577 const struct got_error *error = NULL;
1579 struct dirent **sd_dent;
1580 struct gw_dir *gw_dir;
1582 unsigned int d_cnt, d_i;
1584 d = opendir(gw_trans->gw_conf->got_repos_path);
1586 error = got_error_from_errno2("opendir",
1587 gw_trans->gw_conf->got_repos_path);
1591 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1594 error = got_error_from_errno2("scandir",
1595 gw_trans->gw_conf->got_repos_path);
1599 for (d_i = 0; d_i < d_cnt; d_i++) {
1600 if (gw_trans->gw_conf->got_max_repos > 0 &&
1601 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1602 break; /* account for parent and self */
1604 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1605 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1608 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1612 error = gw_load_got_path(gw_trans, gw_dir);
1613 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1620 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1621 !got_path_dir_is_empty(gw_dir->path)) {
1622 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1624 gw_trans->repos_total++;
1632 static const struct got_error *
1633 gw_parse_querystring(struct gw_trans *gw_trans)
1635 const struct got_error *error = NULL;
1637 struct gw_query_action *action = NULL;
1640 if (gw_trans->gw_req->fieldnmap[0]) {
1641 error = got_error_from_errno("bad parse");
1643 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1644 /* define gw_trans->repo_path */
1645 gw_trans->repo_name = p->parsed.s;
1647 if (asprintf(&gw_trans->repo_path, "%s/%s",
1648 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1649 return got_error_from_errno("asprintf");
1651 /* get action and set function */
1652 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1653 for (i = 0; i < nitems(gw_query_funcs); i++) {
1654 action = &gw_query_funcs[i];
1655 if (action->func_name == NULL)
1657 if (strcmp(action->func_name,
1658 p->parsed.s) == 0) {
1659 gw_trans->action = i;
1664 if (gw_trans->action == -1) {
1665 gw_trans->action = GW_ERR;
1666 gw_trans->error = got_error_from_errno("bad action");
1670 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1671 if (asprintf(&gw_trans->commit, "%s",
1673 return got_error_from_errno("asprintf");
1676 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1677 gw_trans->repo_file = p->parsed.s;
1679 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1680 if (asprintf(&gw_trans->repo_folder, "%s",
1682 return got_error_from_errno("asprintf");
1685 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1686 gw_trans->headref = p->parsed.s;
1688 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1692 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1694 gw_trans->action = GW_INDEX;
1696 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1697 gw_trans->page = p->parsed.i;
1702 static const struct got_error *
1703 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1705 const struct got_error *error;
1707 *gw_dir = malloc(sizeof(**gw_dir));
1708 if (*gw_dir == NULL)
1709 return got_error_from_errno("malloc");
1711 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1712 error = got_error_from_errno("asprintf");
1721 static const struct got_error *
1722 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1726 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1727 if (kerr != KCGI_OK)
1728 return gw_kcgi_error(kerr);
1729 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1731 if (kerr != KCGI_OK)
1732 return gw_kcgi_error(kerr);
1733 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1735 if (kerr != KCGI_OK)
1736 return gw_kcgi_error(kerr);
1737 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1739 if (kerr != KCGI_OK)
1740 return gw_kcgi_error(kerr);
1741 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1742 if (kerr != KCGI_OK)
1743 return gw_kcgi_error(kerr);
1744 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1746 if (kerr != KCGI_OK)
1747 return gw_kcgi_error(kerr);
1749 /* XXX repo_file could be NULL if not present in querystring */
1750 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1751 kerr = khttp_head(gw_trans->gw_req,
1752 kresps[KRESP_CONTENT_DISPOSITION],
1753 "attachment; filename=%s", gw_trans->repo_file);
1754 if (kerr != KCGI_OK)
1755 return gw_kcgi_error(kerr);
1758 kerr = khttp_body(gw_trans->gw_req);
1759 return gw_kcgi_error(kerr);
1762 static const struct got_error *
1763 gw_display_index(struct gw_trans *gw_trans)
1765 const struct got_error *error;
1768 /* catch early querystring errors */
1769 if (gw_trans->error)
1770 gw_trans->action = GW_ERR;
1772 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1776 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1777 if (kerr != KCGI_OK)
1778 return gw_kcgi_error(kerr);
1780 if (gw_trans->action != GW_BLOB) {
1781 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1782 gw_query_funcs[gw_trans->action].template);
1783 if (kerr != KCGI_OK) {
1784 khtml_close(gw_trans->gw_html_req);
1785 return gw_kcgi_error(kerr);
1789 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1792 static const struct got_error *
1793 gw_error(struct gw_trans *gw_trans)
1797 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
1799 return gw_kcgi_error(kerr);
1803 gw_template(size_t key, void *arg)
1805 const struct got_error *error = NULL;
1807 struct gw_trans *gw_trans = arg;
1808 char *img_src = NULL;
1812 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1813 KATTR_NAME, "viewport",
1814 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1816 if (kerr != KCGI_OK)
1818 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1819 if (kerr != KCGI_OK)
1821 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1822 KATTR_CHARSET, "utf-8",
1824 if (kerr != KCGI_OK)
1826 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1827 if (kerr != KCGI_OK)
1829 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1830 KATTR_NAME, "msapplication-TileColor",
1831 KATTR_CONTENT, "#da532c", KATTR__MAX);
1832 if (kerr != KCGI_OK)
1834 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1835 if (kerr != KCGI_OK)
1837 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1838 KATTR_NAME, "theme-color",
1839 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1840 if (kerr != KCGI_OK)
1842 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1843 if (kerr != KCGI_OK)
1845 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1846 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1847 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1848 if (kerr != KCGI_OK)
1850 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1851 if (kerr != KCGI_OK)
1853 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1854 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1855 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1856 if (kerr != KCGI_OK)
1858 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1859 if (kerr != KCGI_OK)
1861 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1862 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1863 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1864 if (kerr != KCGI_OK)
1866 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1867 if (kerr != KCGI_OK)
1869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1870 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1872 if (kerr != KCGI_OK)
1874 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1875 if (kerr != KCGI_OK)
1877 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1878 KATTR_REL, "mask-icon", KATTR_HREF,
1879 "/safari-pinned-tab.svg", KATTR__MAX);
1880 if (kerr != KCGI_OK)
1882 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1883 if (kerr != KCGI_OK)
1885 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1886 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1887 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1888 if (kerr != KCGI_OK)
1890 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1891 if (kerr != KCGI_OK)
1895 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1896 KATTR_ID, "got_link", KATTR__MAX);
1897 if (kerr != KCGI_OK)
1899 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1900 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1901 KATTR_TARGET, "_sotd", KATTR__MAX);
1902 if (kerr != KCGI_OK)
1904 if (asprintf(&img_src, "/%s",
1905 gw_trans->gw_conf->got_logo) == -1)
1907 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1908 KATTR_SRC, img_src, KATTR__MAX);
1909 if (kerr != KCGI_OK) {
1913 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1914 if (kerr != KCGI_OK) {
1919 case (TEMPL_SITEPATH):
1920 error = gw_output_site_link(gw_trans);
1925 if (gw_trans->gw_conf->got_site_name != NULL) {
1926 kerr = khtml_puts(gw_trans->gw_html_req,
1927 gw_trans->gw_conf->got_site_name);
1928 if (kerr != KCGI_OK)
1932 case (TEMPL_SEARCH):
1934 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1935 "search", KATTR__MAX);
1936 if (kerr != KCGI_OK)
1938 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1939 KATTR_METHOD, "POST", KATTR__MAX);
1940 if (kerr != KCGI_OK)
1942 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1943 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1944 KATTR_MAXLENGTH, "50", KATTR__MAX);
1945 if (kerr != KCGI_OK)
1947 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1949 if (kerr != KCGI_OK)
1951 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1952 if (kerr != KCGI_OK)
1954 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1955 if (kerr != KCGI_OK)
1958 case(TEMPL_SITEOWNER):
1959 if (gw_trans->gw_conf->got_site_owner != NULL &&
1960 gw_trans->gw_conf->got_show_site_owner) {
1961 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1962 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1963 if (kerr != KCGI_OK)
1965 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1966 KATTR_ID, "site_owner", KATTR__MAX);
1967 if (kerr != KCGI_OK)
1969 kerr = khtml_puts(gw_trans->gw_html_req,
1970 gw_trans->gw_conf->got_site_owner);
1971 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1972 if (kerr != KCGI_OK)
1976 case(TEMPL_CONTENT):
1977 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1980 KATTR_ID, "tmpl_err", KATTR__MAX);
1981 if (kerr != KCGI_OK)
1983 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1984 if (kerr != KCGI_OK)
1986 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1987 if (kerr != KCGI_OK)
1989 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1990 if (kerr != KCGI_OK)
2000 static const struct got_error *
2001 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2003 const struct got_error *error = NULL;
2004 enum kcgi_err kerr = KCGI_OK;
2006 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2007 KATTR_ID, "header_commit_title", KATTR__MAX);
2008 if (kerr != KCGI_OK)
2010 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2011 if (kerr != KCGI_OK)
2013 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2014 if (kerr != KCGI_OK)
2016 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2017 KATTR_ID, "header_commit", KATTR__MAX);
2018 if (kerr != KCGI_OK)
2020 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2021 if (kerr != KCGI_OK)
2023 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2024 if (kerr != KCGI_OK)
2027 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2028 if (kerr != KCGI_OK)
2030 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2031 if (kerr != KCGI_OK)
2033 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2034 if (kerr != KCGI_OK)
2037 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2039 if (error == NULL && kerr != KCGI_OK)
2040 error = gw_kcgi_error(kerr);
2044 static const struct got_error *
2045 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2047 const struct got_error *error = NULL;
2048 enum kcgi_err kerr = KCGI_OK;
2050 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2051 KATTR_ID, "header_diff_title", KATTR__MAX);
2052 if (kerr != KCGI_OK)
2054 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2055 if (kerr != KCGI_OK)
2057 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2058 if (kerr != KCGI_OK)
2060 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2061 KATTR_ID, "header_diff", KATTR__MAX);
2062 if (kerr != KCGI_OK)
2065 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2066 if (kerr != KCGI_OK)
2069 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2070 if (kerr != KCGI_OK)
2072 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2073 if (kerr != KCGI_OK)
2075 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2077 if (error == NULL && kerr != KCGI_OK)
2078 error = gw_kcgi_error(kerr);
2082 static const struct got_error *
2083 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2085 const struct got_error *error = NULL;
2086 enum kcgi_err kerr = KCGI_OK;
2088 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2089 KATTR_ID, "header_age_title", KATTR__MAX);
2090 if (kerr != KCGI_OK)
2092 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2093 if (kerr != KCGI_OK)
2095 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2096 if (kerr != KCGI_OK)
2098 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2099 KATTR_ID, "header_age", KATTR__MAX);
2100 if (kerr != KCGI_OK)
2102 kerr = khtml_puts(gw_trans->gw_html_req, str);
2103 if (kerr != KCGI_OK)
2105 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2107 if (error == NULL && kerr != KCGI_OK)
2108 error = gw_kcgi_error(kerr);
2112 static const struct got_error *
2113 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2115 const struct got_error *error = NULL;
2118 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2119 KATTR_ID, "header_author_title", KATTR__MAX);
2120 if (kerr != KCGI_OK)
2122 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2123 if (kerr != KCGI_OK)
2125 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2126 if (kerr != KCGI_OK)
2128 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2129 KATTR_ID, "header_author", KATTR__MAX);
2130 if (kerr != KCGI_OK)
2132 kerr = khtml_puts(gw_trans->gw_html_req, str);
2133 if (kerr != KCGI_OK)
2135 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2137 if (error == NULL && kerr != KCGI_OK)
2138 error = gw_kcgi_error(kerr);
2142 static const struct got_error *
2143 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2145 const struct got_error *error = NULL;
2148 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2149 KATTR_ID, "header_committer_title", KATTR__MAX);
2150 if (kerr != KCGI_OK)
2152 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2153 if (kerr != KCGI_OK)
2155 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2156 if (kerr != KCGI_OK)
2158 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2159 KATTR_ID, "header_committer", KATTR__MAX);
2160 if (kerr != KCGI_OK)
2162 kerr = khtml_puts(gw_trans->gw_html_req, str);
2163 if (kerr != KCGI_OK)
2165 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2167 if (error == NULL && kerr != KCGI_OK)
2168 error = gw_kcgi_error(kerr);
2172 static const struct got_error *
2173 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2175 const struct got_error *error = NULL;
2178 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2179 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2180 if (kerr != KCGI_OK)
2182 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2183 if (kerr != KCGI_OK)
2185 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2186 if (kerr != KCGI_OK)
2188 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2189 KATTR_ID, "header_commit_msg", KATTR__MAX);
2190 if (kerr != KCGI_OK)
2192 kerr = khttp_puts(gw_trans->gw_req, str);
2193 if (kerr != KCGI_OK)
2195 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2197 if (error == NULL && kerr != KCGI_OK)
2198 error = gw_kcgi_error(kerr);
2202 static const struct got_error *
2203 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2205 const struct got_error *error = NULL;
2208 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2209 KATTR_ID, "header_tree_title", KATTR__MAX);
2210 if (kerr != KCGI_OK)
2212 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2213 if (kerr != KCGI_OK)
2215 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2216 if (kerr != KCGI_OK)
2218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2219 KATTR_ID, "header_tree", KATTR__MAX);
2220 if (kerr != KCGI_OK)
2222 kerr = khtml_puts(gw_trans->gw_html_req, str);
2223 if (kerr != KCGI_OK)
2225 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2227 if (error == NULL && kerr != KCGI_OK)
2228 error = gw_kcgi_error(kerr);
2232 static const struct got_error *
2233 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2236 const struct got_error *error = NULL;
2238 char *d_file = NULL;
2242 *description = NULL;
2243 if (gw_trans->gw_conf->got_show_repo_description == 0)
2246 if (asprintf(&d_file, "%s/description", dir) == -1)
2247 return got_error_from_errno("asprintf");
2249 f = fopen(d_file, "r");
2251 if (errno == ENOENT || errno == EACCES)
2253 error = got_error_from_errno2("fopen", d_file);
2257 if (fseek(f, 0, SEEK_END) == -1) {
2258 error = got_ferror(f, GOT_ERR_IO);
2263 error = got_ferror(f, GOT_ERR_IO);
2266 if (fseek(f, 0, SEEK_SET) == -1) {
2267 error = got_ferror(f, GOT_ERR_IO);
2270 *description = calloc(len + 1, sizeof(**description));
2271 if (*description == NULL) {
2272 error = got_error_from_errno("calloc");
2276 n = fread(*description, 1, len, f);
2277 if (n == 0 && ferror(f))
2278 error = got_ferror(f, GOT_ERR_IO);
2280 if (f != NULL && fclose(f) == -1 && error == NULL)
2281 error = got_error_from_errno("fclose");
2286 static const struct got_error *
2287 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2291 char *years = "years ago", *months = "months ago";
2292 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2293 char *minutes = "minutes ago", *seconds = "seconds ago";
2294 char *now = "right now";
2302 diff_time = time(NULL) - committer_time;
2303 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2304 if (asprintf(repo_age, "%lld %s",
2305 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2306 return got_error_from_errno("asprintf");
2307 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2308 if (asprintf(repo_age, "%lld %s",
2309 (diff_time / 60 / 60 / 24 / (365 / 12)),
2311 return got_error_from_errno("asprintf");
2312 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2313 if (asprintf(repo_age, "%lld %s",
2314 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2315 return got_error_from_errno("asprintf");
2316 } else if (diff_time > 60 * 60 * 24 * 2) {
2317 if (asprintf(repo_age, "%lld %s",
2318 (diff_time / 60 / 60 / 24), days) == -1)
2319 return got_error_from_errno("asprintf");
2320 } else if (diff_time > 60 * 60 * 2) {
2321 if (asprintf(repo_age, "%lld %s",
2322 (diff_time / 60 / 60), hours) == -1)
2323 return got_error_from_errno("asprintf");
2324 } else if (diff_time > 60 * 2) {
2325 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2327 return got_error_from_errno("asprintf");
2328 } else if (diff_time > 2) {
2329 if (asprintf(repo_age, "%lld %s", diff_time,
2331 return got_error_from_errno("asprintf");
2333 if (asprintf(repo_age, "%s", now) == -1)
2334 return got_error_from_errno("asprintf");
2338 if (gmtime_r(&committer_time, &tm) == NULL)
2339 return got_error_from_errno("gmtime_r");
2341 s = asctime_r(&tm, datebuf);
2343 return got_error_from_errno("asctime_r");
2345 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2346 return got_error_from_errno("asprintf");
2352 static const struct got_error *
2353 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2354 const char *refname, int ref_tm)
2356 const struct got_error *error = NULL;
2357 struct got_repository *repo = NULL;
2358 struct got_commit_object *commit = NULL;
2359 struct got_reflist_head refs;
2360 struct got_reflist_entry *re;
2361 time_t committer_time = 0, cmp_time = 0;
2364 SIMPLEQ_INIT(&refs);
2366 if (gw_trans->gw_conf->got_show_repo_age == 0)
2370 repo = gw_trans->repo;
2372 error = got_repo_open(&repo, dir, NULL);
2377 error = got_ref_list(&refs, repo, "refs/heads",
2378 got_ref_cmp_by_name, NULL);
2383 * Find the youngest branch tip in the repository, or the age of
2384 * the a specific branch tip if a name was provided by the caller.
2386 SIMPLEQ_FOREACH(re, &refs, entry) {
2387 struct got_object_id *id = NULL;
2389 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2392 error = got_ref_resolve(&id, repo, re->ref);
2396 error = got_object_open_as_commit(&commit, repo, id);
2402 got_object_commit_get_committer_time(commit);
2403 got_object_commit_close(commit);
2404 if (cmp_time < committer_time)
2405 cmp_time = committer_time;
2411 if (cmp_time != 0) {
2412 committer_time = cmp_time;
2413 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2416 got_ref_list_free(&refs);
2417 if (gw_trans->repo == NULL)
2418 got_repo_close(repo);
2422 static const struct got_error *
2423 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2425 const struct got_error *error;
2427 struct got_object_id *id1 = NULL, *id2 = NULL;
2428 char *label1 = NULL, *label2 = NULL, *line = NULL;
2430 size_t linesize = 0;
2432 enum kcgi_err kerr = KCGI_OK;
2438 if (header->parent_id != NULL &&
2439 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2440 error = got_repo_match_object_id(&id1, &label1,
2441 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2446 error = got_repo_match_object_id(&id2, &label2,
2447 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2451 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2455 case GOT_OBJ_TYPE_BLOB:
2456 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2459 case GOT_OBJ_TYPE_TREE:
2460 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2463 case GOT_OBJ_TYPE_COMMIT:
2464 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2468 error = got_error(GOT_ERR_OBJ_TYPE);
2473 if (fseek(f, 0, SEEK_SET) == -1) {
2474 error = got_ferror(f, GOT_ERR_IO);
2478 while ((linelen = getline(&line, &linesize, f)) != -1) {
2479 error = gw_colordiff_line(gw_trans, line);
2482 /* XXX: KHTML_PRETTY breaks this */
2483 kerr = khtml_puts(gw_trans->gw_html_req, line);
2484 if (kerr != KCGI_OK)
2486 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2487 if (kerr != KCGI_OK)
2490 if (linelen == -1 && ferror(f))
2491 error = got_error_from_errno("getline");
2493 if (f && fclose(f) == -1 && error == NULL)
2494 error = got_error_from_errno("fclose");
2501 if (error == NULL && kerr != KCGI_OK)
2502 error = gw_kcgi_error(kerr);
2506 static const struct got_error *
2507 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2509 const struct got_error *error = NULL;
2510 struct got_repository *repo;
2511 const char *gitconfig_owner;
2515 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2518 error = got_repo_open(&repo, dir, NULL);
2521 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2522 if (gitconfig_owner) {
2523 *owner = strdup(gitconfig_owner);
2525 error = got_error_from_errno("strdup");
2527 got_repo_close(repo);
2531 static const struct got_error *
2532 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2534 const struct got_error *error = NULL;
2536 char *d_file = NULL;
2542 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2543 return got_error_from_errno("asprintf");
2545 f = fopen(d_file, "r");
2547 if (errno != ENOENT && errno != EACCES)
2548 error = got_error_from_errno2("fopen", d_file);
2552 if (fseek(f, 0, SEEK_END) == -1) {
2553 error = got_ferror(f, GOT_ERR_IO);
2558 error = got_ferror(f, GOT_ERR_IO);
2561 if (fseek(f, 0, SEEK_SET) == -1) {
2562 error = got_ferror(f, GOT_ERR_IO);
2566 *url = calloc(len + 1, sizeof(**url));
2568 error = got_error_from_errno("calloc");
2572 n = fread(*url, 1, len, f);
2573 if (n == 0 && ferror(f))
2574 error = got_ferror(f, GOT_ERR_IO);
2576 if (f && fclose(f) == -1 && error == NULL)
2577 error = got_error_from_errno("fclose");
2582 static const struct got_error *
2583 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2584 int limit, int tag_type)
2586 const struct got_error *error = NULL;
2587 struct got_reflist_head refs;
2588 struct got_reflist_entry *re;
2590 char *id_str = NULL, *newline, *href_commits = NULL;
2591 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2592 struct got_tag_object *tag = NULL;
2593 enum kcgi_err kerr = KCGI_OK;
2594 int summary_header_displayed = 0;
2596 SIMPLEQ_INIT(&refs);
2598 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2599 got_ref_cmp_tags, gw_trans->repo);
2603 SIMPLEQ_FOREACH(re, &refs, entry) {
2604 const char *refname;
2606 const char *tag_commit;
2608 struct got_object_id *id;
2609 struct got_commit_object *commit = NULL;
2611 refname = got_ref_get_name(re->ref);
2612 if (strncmp(refname, "refs/tags/", 10) != 0)
2616 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2620 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2622 if (error->code != GOT_ERR_OBJ_TYPE) {
2626 /* "lightweight" tag */
2627 error = got_object_open_as_commit(&commit,
2628 gw_trans->repo, id);
2633 tagger = got_object_commit_get_committer(commit);
2635 got_object_commit_get_committer_time(commit);
2636 error = got_object_id_str(&id_str, id);
2640 tagger = got_object_tag_get_tagger(tag);
2641 tagger_time = got_object_tag_get_tagger_time(tag);
2642 error = got_object_id_str(&id_str,
2643 got_object_tag_get_object_id(tag));
2648 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2649 strlen(id_str)) != 0)
2653 error = got_object_commit_get_logmsg(&tag_commit0,
2657 got_object_commit_close(commit);
2659 tag_commit0 = strdup(got_object_tag_get_message(tag));
2660 if (tag_commit0 == NULL) {
2661 error = got_error_from_errno("strdup");
2666 tag_commit = tag_commit0;
2667 while (*tag_commit == '\n')
2672 newline = strchr(tag_commit, '\n');
2676 if (summary_header_displayed == 0) {
2677 kerr = khtml_attr(gw_trans->gw_html_req,
2678 KELEM_DIV, KATTR_ID,
2679 "summary_tags_title_wrapper", KATTR__MAX);
2680 if (kerr != KCGI_OK)
2682 kerr = khtml_attr(gw_trans->gw_html_req,
2683 KELEM_DIV, KATTR_ID,
2684 "summary_tags_title", KATTR__MAX);
2685 if (kerr != KCGI_OK)
2687 kerr = khtml_puts(gw_trans->gw_html_req,
2689 if (kerr != KCGI_OK)
2691 kerr = khtml_closeelem(gw_trans->gw_html_req,
2693 if (kerr != KCGI_OK)
2695 kerr = khtml_attr(gw_trans->gw_html_req,
2696 KELEM_DIV, KATTR_ID,
2697 "summary_tags_content", KATTR__MAX);
2698 if (kerr != KCGI_OK)
2700 summary_header_displayed = 1;
2703 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2704 KATTR_ID, "tags_wrapper", KATTR__MAX);
2705 if (kerr != KCGI_OK)
2707 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2708 KATTR_ID, "tags_age", KATTR__MAX);
2709 if (kerr != KCGI_OK)
2711 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2714 kerr = khtml_puts(gw_trans->gw_html_req,
2716 if (kerr != KCGI_OK)
2718 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2719 if (kerr != KCGI_OK)
2721 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2722 KATTR_ID, "tags", KATTR__MAX);
2723 if (kerr != KCGI_OK)
2725 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2726 if (kerr != KCGI_OK)
2728 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2729 if (kerr != KCGI_OK)
2731 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2732 KATTR_ID, "tags_name", KATTR__MAX);
2733 if (kerr != KCGI_OK)
2735 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2736 gw_trans->repo_name, id_str) == -1) {
2737 error = got_error_from_errno("asprintf");
2740 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2741 KATTR_HREF, href_tag, KATTR__MAX);
2742 if (kerr != KCGI_OK)
2744 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2745 if (kerr != KCGI_OK)
2747 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2748 if (kerr != KCGI_OK)
2751 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2752 KATTR_ID, "navs_wrapper", KATTR__MAX);
2753 if (kerr != KCGI_OK)
2755 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2756 KATTR_ID, "navs", KATTR__MAX);
2757 if (kerr != KCGI_OK)
2760 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2761 KATTR_HREF, href_tag, KATTR__MAX);
2762 if (kerr != KCGI_OK)
2764 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2765 if (kerr != KCGI_OK)
2767 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2768 if (kerr != KCGI_OK)
2771 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2772 if (kerr != KCGI_OK)
2774 if (asprintf(&href_briefs,
2775 "?path=%s&action=briefs&commit=%s",
2776 gw_trans->repo_name, id_str) == -1) {
2777 error = got_error_from_errno("asprintf");
2780 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2781 KATTR_HREF, href_briefs, KATTR__MAX);
2782 if (kerr != KCGI_OK)
2784 kerr = khtml_puts(gw_trans->gw_html_req,
2786 if (kerr != KCGI_OK)
2788 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2789 if (kerr != KCGI_OK)
2792 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2793 if (kerr != KCGI_OK)
2796 if (asprintf(&href_commits,
2797 "?path=%s&action=commits&commit=%s",
2798 gw_trans->repo_name, id_str) == -1) {
2799 error = got_error_from_errno("asprintf");
2802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2803 KATTR_HREF, href_commits, KATTR__MAX);
2804 if (kerr != KCGI_OK)
2806 kerr = khtml_puts(gw_trans->gw_html_req,
2808 if (kerr != KCGI_OK)
2810 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2811 if (kerr != KCGI_OK)
2814 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2815 KATTR_ID, "dotted_line", KATTR__MAX);
2816 if (kerr != KCGI_OK)
2818 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2819 if (kerr != KCGI_OK)
2823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2824 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2825 if (kerr != KCGI_OK)
2827 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2828 if (kerr != KCGI_OK)
2830 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2831 if (kerr != KCGI_OK)
2833 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2834 KATTR_ID, "tag_info_date", KATTR__MAX);
2835 if (kerr != KCGI_OK)
2837 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2840 kerr = khtml_puts(gw_trans->gw_html_req,
2842 if (kerr != KCGI_OK)
2844 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2845 if (kerr != KCGI_OK)
2848 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2849 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2850 if (kerr != KCGI_OK)
2852 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2853 if (kerr != KCGI_OK)
2855 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2856 if (kerr != KCGI_OK)
2858 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2859 KATTR_ID, "tag_info_date", KATTR__MAX);
2860 if (kerr != KCGI_OK)
2862 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2863 if (kerr != KCGI_OK)
2865 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2866 if (kerr != KCGI_OK)
2869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2870 KATTR_ID, "tag_info", KATTR__MAX);
2871 if (kerr != KCGI_OK)
2873 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2874 if (kerr != KCGI_OK)
2880 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2881 if (kerr != KCGI_OK)
2884 if (limit && --limit == 0)
2888 got_object_tag_close(tag);
2901 href_commits = NULL;
2905 got_object_tag_close(tag);
2912 got_ref_list_free(&refs);
2913 if (error == NULL && kerr != KCGI_OK)
2914 error = gw_kcgi_error(kerr);
2919 gw_free_header(struct gw_header *header)
2922 free(header->author);
2923 free(header->committer);
2924 free(header->refs_str);
2925 free(header->commit_id);
2926 free(header->parent_id);
2927 free(header->tree_id);
2928 free(header->commit_msg);
2931 static struct gw_header *
2934 struct gw_header *header;
2936 header = malloc(sizeof(*header));
2940 header->path = NULL;
2941 SIMPLEQ_INIT(&header->refs);
2943 header->refs_str = NULL;
2944 header->commit_id = NULL;
2945 header->committer = NULL;
2946 header->author = NULL;
2947 header->parent_id = NULL;
2948 header->tree_id = NULL;
2949 header->commit_msg = NULL;
2954 static const struct got_error *
2955 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2956 int limit, struct got_object_id *id)
2958 const struct got_error *error = NULL;
2959 struct got_commit_graph *graph = NULL;
2960 struct got_commit_object *commit = NULL;
2962 error = got_commit_graph_open(&graph, header->path, 0);
2966 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
2972 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
2975 if (error->code == GOT_ERR_ITER_COMPLETED)
2982 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
2986 error = gw_get_commit(gw_trans, header, commit, id);
2990 struct gw_header *n_header = NULL;
2991 if ((n_header = gw_init_header()) == NULL) {
2992 error = got_error_from_errno("malloc");
2995 error = gw_get_commit(gw_trans, n_header, commit, id);
2998 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3001 if (error || (limit && --limit == 0))
3006 got_object_commit_close(commit);
3008 got_commit_graph_close(graph);
3012 static const struct got_error *
3013 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3014 struct got_commit_object *commit, struct got_object_id *id)
3016 const struct got_error *error = NULL;
3017 struct got_reflist_entry *re;
3018 struct got_object_id *id2 = NULL;
3019 struct got_object_qid *parent_id;
3020 char *commit_msg = NULL, *commit_msg0;
3023 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3026 struct got_tag_object *tag = NULL;
3029 if (got_ref_is_symbolic(re->ref))
3032 name = got_ref_get_name(re->ref);
3033 if (strncmp(name, "refs/", 5) == 0)
3035 if (strncmp(name, "got/", 4) == 0)
3037 if (strncmp(name, "heads/", 6) == 0)
3039 if (strncmp(name, "remotes/", 8) == 0)
3041 if (strncmp(name, "tags/", 5) == 0) {
3042 error = got_object_open_as_tag(&tag, gw_trans->repo,
3045 if (error->code != GOT_ERR_OBJ_TYPE)
3048 * Ref points at something other
3055 cmp = got_object_id_cmp(tag ?
3056 got_object_tag_get_object_id(tag) : re->id, id);
3058 got_object_tag_close(tag);
3061 s = header->refs_str;
3062 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3063 s ? ", " : "", name) == -1) {
3064 error = got_error_from_errno("asprintf");
3066 header->refs_str = NULL;
3072 error = got_object_id_str(&header->commit_id, id);
3076 error = got_object_id_str(&header->tree_id,
3077 got_object_commit_get_tree_id(commit));
3081 if (gw_trans->action == GW_DIFF) {
3082 parent_id = SIMPLEQ_FIRST(
3083 got_object_commit_get_parent_ids(commit));
3084 if (parent_id != NULL) {
3085 id2 = got_object_id_dup(parent_id->id);
3087 error = got_object_id_str(&header->parent_id, id2);
3092 header->parent_id = strdup("/dev/null");
3093 if (header->parent_id == NULL) {
3094 error = got_error_from_errno("strdup");
3100 header->committer_time =
3101 got_object_commit_get_committer_time(commit);
3104 strdup(got_object_commit_get_author(commit));
3105 if (header->author == NULL) {
3106 error = got_error_from_errno("strdup");
3110 strdup(got_object_commit_get_committer(commit));
3111 if (header->committer == NULL) {
3112 error = got_error_from_errno("strdup");
3115 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3119 commit_msg = commit_msg0;
3120 while (*commit_msg == '\n')
3123 header->commit_msg = strdup(commit_msg);
3124 if (header->commit_msg == NULL)
3125 error = got_error_from_errno("strdup");
3130 static const struct got_error *
3131 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3133 const struct got_error *error = NULL;
3134 char *in_repo_path = NULL;
3135 struct got_object_id *id = NULL;
3137 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3141 if (gw_trans->commit == NULL) {
3142 struct got_reference *head_ref;
3143 error = got_ref_open(&head_ref, gw_trans->repo,
3144 gw_trans->headref, 0);
3148 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3149 got_ref_close(head_ref);
3153 struct got_reference *ref;
3154 error = got_ref_open(&ref, gw_trans->repo, gw_trans->commit, 0);
3155 if (error == NULL) {
3157 error = got_ref_resolve(&id, gw_trans->repo, ref);
3161 error = got_object_get_type(&obj_type, gw_trans->repo,
3165 if (obj_type == GOT_OBJ_TYPE_TAG) {
3166 struct got_tag_object *tag;
3167 error = got_object_open_as_tag(&tag,
3168 gw_trans->repo, id);
3171 if (got_object_tag_get_object_type(tag) !=
3172 GOT_OBJ_TYPE_COMMIT) {
3173 got_object_tag_close(tag);
3174 error = got_error(GOT_ERR_OBJ_TYPE);
3178 id = got_object_id_dup(
3179 got_object_tag_get_object_id(tag));
3181 error = got_error_from_errno(
3182 "got_object_id_dup");
3183 got_object_tag_close(tag);
3186 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3187 error = got_error(GOT_ERR_OBJ_TYPE);
3191 error = got_repo_match_object_id_prefix(&id,
3192 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3198 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3199 gw_trans->repo_path, 1);
3204 header->path = strdup(in_repo_path);
3205 if (header->path == NULL) {
3206 error = got_error_from_errno("strdup");
3211 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3212 got_ref_cmp_by_name, NULL);
3216 error = gw_get_commits(gw_trans, header, limit, id);
3218 got_ref_list_free(&header->refs);
3228 char datebuf[11]; /* YYYY-MM-DD + NUL */
3231 struct gw_blame_cb_args {
3232 struct blame_line *lines;
3236 off_t *line_offsets;
3238 struct got_repository *repo;
3239 struct gw_trans *gw_trans;
3242 static const struct got_error *
3243 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3245 const struct got_error *err = NULL;
3246 struct gw_blame_cb_args *a = arg;
3247 struct blame_line *bline;
3249 size_t linesize = 0;
3250 struct got_commit_object *commit = NULL;
3253 time_t committer_time;
3254 enum kcgi_err kerr = KCGI_OK;
3256 if (nlines != a->nlines ||
3257 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3258 return got_error(GOT_ERR_RANGE);
3261 return NULL; /* no change in this commit */
3263 /* Annotate this line. */
3264 bline = &a->lines[lineno - 1];
3265 if (bline->annotated)
3267 err = got_object_id_str(&bline->id_str, id);
3271 err = got_object_open_as_commit(&commit, a->repo, id);
3275 bline->committer = strdup(got_object_commit_get_committer(commit));
3276 if (bline->committer == NULL) {
3277 err = got_error_from_errno("strdup");
3281 committer_time = got_object_commit_get_committer_time(commit);
3282 if (localtime_r(&committer_time, &tm) == NULL)
3283 return got_error_from_errno("localtime_r");
3284 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3285 &tm) >= sizeof(bline->datebuf)) {
3286 err = got_error(GOT_ERR_NO_SPACE);
3289 bline->annotated = 1;
3291 /* Print lines annotated so far. */
3292 bline = &a->lines[a->lineno_cur - 1];
3293 if (!bline->annotated)
3296 offset = a->line_offsets[a->lineno_cur - 1];
3297 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3298 err = got_error_from_errno("fseeko");
3302 while (bline->annotated) {
3303 char *smallerthan, *at, *nl, *committer;
3304 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3307 if (getline(&line, &linesize, a->f) == -1) {
3309 err = got_error_from_errno("getline");
3313 committer = bline->committer;
3314 smallerthan = strchr(committer, '<');
3315 if (smallerthan && smallerthan[1] != '\0')
3316 committer = smallerthan + 1;
3317 at = strchr(committer, '@');
3320 len = strlen(committer);
3322 committer[8] = '\0';
3324 nl = strchr(line, '\n');
3328 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3329 "blame_wrapper", KATTR__MAX);
3330 if (kerr != KCGI_OK)
3332 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3333 "blame_number", KATTR__MAX);
3334 if (kerr != KCGI_OK)
3336 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3337 a->lineno_cur) == -1)
3339 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3340 if (kerr != KCGI_OK)
3342 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3343 if (kerr != KCGI_OK)
3346 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3347 "blame_hash", KATTR__MAX);
3348 if (kerr != KCGI_OK)
3350 /* XXX repo_file could be NULL if not present in querystring */
3351 if (asprintf(&href_diff,
3352 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3353 a->gw_trans->repo_name, bline->id_str,
3354 a->gw_trans->repo_file, a->gw_trans->repo_folder ?
3355 a->gw_trans->repo_folder : "") == -1) {
3356 err = got_error_from_errno("asprintf");
3359 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3360 err = got_error_from_errno("asprintf");
3363 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3364 KATTR_HREF, href_diff, KATTR__MAX);
3365 if (kerr != KCGI_OK)
3367 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3368 if (kerr != KCGI_OK)
3370 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3371 if (kerr != KCGI_OK)
3374 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3375 "blame_date", KATTR__MAX);
3376 if (kerr != KCGI_OK)
3378 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3379 if (kerr != KCGI_OK)
3381 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3382 if (kerr != KCGI_OK)
3385 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3386 "blame_author", KATTR__MAX);
3387 if (kerr != KCGI_OK)
3389 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3390 if (kerr != KCGI_OK)
3392 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3393 if (kerr != KCGI_OK)
3396 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3397 "blame_code", KATTR__MAX);
3398 if (kerr != KCGI_OK)
3400 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3401 if (kerr != KCGI_OK)
3403 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3404 if (kerr != KCGI_OK)
3407 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3408 if (kerr != KCGI_OK)
3412 bline = &a->lines[a->lineno_cur - 1];
3420 got_object_commit_close(commit);
3422 if (err == NULL && kerr != KCGI_OK)
3423 err = gw_kcgi_error(kerr);
3427 static const struct got_error *
3428 gw_output_file_blame(struct gw_trans *gw_trans)
3430 const struct got_error *error = NULL;
3431 struct got_object_id *obj_id = NULL;
3432 struct got_object_id *commit_id = NULL;
3433 struct got_blob_object *blob = NULL;
3434 char *path = NULL, *in_repo_path = NULL;
3435 struct gw_blame_cb_args bca;
3439 /* XXX repo_file could be NULL if not present in querystring */
3440 if (asprintf(&path, "%s%s%s",
3441 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3442 gw_trans->repo_folder ? "/" : "",
3443 gw_trans->repo_file) == -1) {
3444 error = got_error_from_errno("asprintf");
3448 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3452 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3453 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3457 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3462 if (obj_id == NULL) {
3463 error = got_error(GOT_ERR_NO_OBJ);
3467 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3471 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3472 error = got_error(GOT_ERR_OBJ_TYPE);
3476 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3480 bca.f = got_opentemp();
3481 if (bca.f == NULL) {
3482 error = got_error_from_errno("got_opentemp");
3485 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3486 &bca.line_offsets, bca.f, blob);
3487 if (error || bca.nlines == 0)
3490 /* Don't include \n at EOF in the blame line count. */
3491 if (bca.line_offsets[bca.nlines - 1] == filesize)
3494 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3495 if (bca.lines == NULL) {
3496 error = got_error_from_errno("calloc");
3500 bca.nlines_prec = 0;
3506 bca.repo = gw_trans->repo;
3507 bca.gw_trans = gw_trans;
3509 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3512 free(bca.line_offsets);
3518 for (i = 0; i < bca.nlines; i++) {
3519 struct blame_line *bline = &bca.lines[i];
3520 free(bline->id_str);
3521 free(bline->committer);
3524 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3525 error = got_error_from_errno("fclose");
3527 got_object_blob_close(blob);
3531 static const struct got_error *
3532 gw_output_blob_buf(struct gw_trans *gw_trans)
3534 const struct got_error *error = NULL;
3535 struct got_object_id *obj_id = NULL;
3536 struct got_object_id *commit_id = NULL;
3537 struct got_blob_object *blob = NULL;
3538 char *path = NULL, *in_repo_path = NULL;
3539 int obj_type, set_mime = 0;
3542 enum kcgi_err kerr = KCGI_OK;
3544 /* XXX repo_file could be NULL if not present in querystring */
3545 if (asprintf(&path, "%s%s%s",
3546 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3547 gw_trans->repo_folder ? "/" : "",
3548 gw_trans->repo_file) == -1) {
3549 error = got_error_from_errno("asprintf");
3553 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3557 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3558 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3562 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3567 if (obj_id == NULL) {
3568 error = got_error(GOT_ERR_NO_OBJ);
3572 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3576 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3577 error = got_error(GOT_ERR_OBJ_TYPE);
3581 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3585 hdrlen = got_object_blob_get_hdrlen(blob);
3587 error = got_object_blob_read_block(&len, blob);
3590 buf = got_object_blob_get_read_buf(blob);
3593 * Skip blob object header first time around,
3594 * which also contains a zero byte.
3597 if (set_mime == 0) {
3598 if (isbinary(buf, len - hdrlen))
3599 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3601 gw_trans->mime = KMIME_TEXT_PLAIN;
3603 error = gw_display_index(gw_trans);
3607 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3616 got_object_blob_close(blob);
3617 if (error == NULL && kerr != KCGI_OK)
3618 error = gw_kcgi_error(kerr);
3622 static const struct got_error *
3623 gw_output_repo_tree(struct gw_trans *gw_trans)
3625 const struct got_error *error = NULL;
3626 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3627 struct got_tree_object *tree = NULL;
3628 char *path = NULL, *in_repo_path = NULL;
3629 char *id_str = NULL;
3630 char *build_folder = NULL;
3631 char *href_blob = NULL, *href_blame = NULL;
3632 const char *class = NULL;
3633 int nentries, i, class_flip = 0;
3634 enum kcgi_err kerr = KCGI_OK;
3636 if (gw_trans->repo_folder != NULL) {
3637 path = strdup(gw_trans->repo_folder);
3639 error = got_error_from_errno("strdup");
3643 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3644 gw_trans->repo_path, 1);
3648 path = in_repo_path;
3651 if (gw_trans->commit == NULL) {
3652 struct got_reference *head_ref;
3653 error = got_ref_open(&head_ref, gw_trans->repo,
3654 gw_trans->headref, 0);
3657 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
3660 got_ref_close(head_ref);
3663 error = got_repo_match_object_id(&commit_id, NULL,
3664 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3670 * XXX gw_trans->commit might have already been allocated in
3671 * gw_parse_querystring(); could we more cleanly seperate values
3672 * we received as arguments from values we compute ourselves?
3674 error = got_object_id_str(&gw_trans->commit, commit_id);
3678 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id, path);
3682 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
3686 nentries = got_object_tree_get_nentries(tree);
3687 for (i = 0; i < nentries; i++) {
3688 struct got_tree_entry *te;
3689 const char *modestr = "";
3692 te = got_object_tree_get_entry(tree, i);
3694 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3698 mode = got_tree_entry_get_mode(te);
3699 if (got_object_tree_entry_is_submodule(te))
3701 else if (S_ISLNK(mode))
3703 else if (S_ISDIR(mode))
3705 else if (mode & S_IXUSR)
3708 if (class_flip == 0) {
3709 class = "back_lightgray";
3712 class = "back_white";
3716 if (S_ISDIR(mode)) {
3717 if (asprintf(&build_folder, "%s/%s",
3718 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3719 got_tree_entry_get_name(te)) == -1) {
3720 error = got_error_from_errno("asprintf");
3723 if (asprintf(&href_blob,
3724 "?path=%s&action=%s&commit=%s&folder=%s",
3725 gw_trans->repo_name, gw_get_action_name(gw_trans),
3726 gw_trans->commit, build_folder) == -1) {
3727 error = got_error_from_errno("asprintf");
3731 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3732 KATTR_ID, "tree_wrapper", KATTR__MAX);
3733 if (kerr != KCGI_OK)
3735 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3736 KATTR_ID, "tree_line", KATTR_CLASS, class,
3738 if (kerr != KCGI_OK)
3740 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3741 KATTR_HREF, href_blob, KATTR_CLASS,
3742 "diff_directory", KATTR__MAX);
3743 if (kerr != KCGI_OK)
3745 kerr = khtml_puts(gw_trans->gw_html_req,
3746 got_tree_entry_get_name(te));
3747 if (kerr != KCGI_OK)
3749 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3750 if (kerr != KCGI_OK)
3752 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3753 if (kerr != KCGI_OK)
3755 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3756 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3758 if (kerr != KCGI_OK)
3760 kerr = khtml_entity(gw_trans->gw_html_req,
3762 if (kerr != KCGI_OK)
3764 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3765 if (kerr != KCGI_OK)
3768 if (asprintf(&href_blob,
3769 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3770 gw_trans->repo_name, "blob", gw_trans->commit,
3771 got_tree_entry_get_name(te),
3772 gw_trans->repo_folder ?
3773 gw_trans->repo_folder : "") == -1) {
3774 error = got_error_from_errno("asprintf");
3777 if (asprintf(&href_blame,
3778 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3779 gw_trans->repo_name, "blame", gw_trans->commit,
3780 got_tree_entry_get_name(te),
3781 gw_trans->repo_folder ?
3782 gw_trans->repo_folder : "") == -1) {
3783 error = got_error_from_errno("asprintf");
3787 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3788 KATTR_ID, "tree_wrapper", KATTR__MAX);
3789 if (kerr != KCGI_OK)
3791 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3792 KATTR_ID, "tree_line", KATTR_CLASS, class,
3794 if (kerr != KCGI_OK)
3796 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3797 KATTR_HREF, href_blob, KATTR__MAX);
3798 if (kerr != KCGI_OK)
3800 kerr = khtml_puts(gw_trans->gw_html_req,
3801 got_tree_entry_get_name(te));
3802 if (kerr != KCGI_OK)
3804 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3805 if (kerr != KCGI_OK)
3807 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3808 if (kerr != KCGI_OK)
3810 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3811 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3813 if (kerr != KCGI_OK)
3816 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3817 KATTR_HREF, href_blob, KATTR__MAX);
3818 if (kerr != KCGI_OK)
3820 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3821 if (kerr != KCGI_OK)
3823 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3824 if (kerr != KCGI_OK)
3827 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3828 if (kerr != KCGI_OK)
3831 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3832 KATTR_HREF, href_blame, KATTR__MAX);
3833 if (kerr != KCGI_OK)
3835 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3836 if (kerr != KCGI_OK)
3839 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3840 if (kerr != KCGI_OK)
3848 build_folder = NULL;
3852 got_object_tree_close(tree);
3859 if (error == NULL && kerr != KCGI_OK)
3860 error = gw_kcgi_error(kerr);
3864 static const struct got_error *
3865 gw_output_repo_heads(struct gw_trans *gw_trans)
3867 const struct got_error *error = NULL;
3868 struct got_reflist_head refs;
3869 struct got_reflist_entry *re;
3870 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3871 char *href_commits = NULL;
3872 enum kcgi_err kerr = KCGI_OK;
3874 SIMPLEQ_INIT(&refs);
3876 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
3877 got_ref_cmp_by_name, NULL);
3881 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3882 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3883 if (kerr != KCGI_OK)
3885 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3886 KATTR_ID, "summary_heads_title", KATTR__MAX);
3887 if (kerr != KCGI_OK)
3889 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3890 if (kerr != KCGI_OK)
3892 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3893 if (kerr != KCGI_OK)
3895 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3896 KATTR_ID, "summary_heads_content", KATTR__MAX);
3897 if (kerr != KCGI_OK)
3900 SIMPLEQ_FOREACH(re, &refs, entry) {
3901 const char *refname;
3903 if (got_ref_is_symbolic(re->ref))
3906 refname = got_ref_get_name(re->ref);
3907 if (refname == NULL) {
3908 error = got_error_from_errno("got_ref_to_str");
3912 if (strncmp(refname, "refs/heads/", 11) != 0)
3915 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3920 if (strncmp(refname, "refs/heads/", 11) == 0)
3923 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3924 KATTR_ID, "heads_wrapper", KATTR__MAX);
3925 if (kerr != KCGI_OK)
3927 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3928 KATTR_ID, "heads_age", KATTR__MAX);
3929 if (kerr != KCGI_OK)
3931 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
3932 if (kerr != KCGI_OK)
3934 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3935 if (kerr != KCGI_OK)
3937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3938 KATTR_ID, "heads_space", KATTR__MAX);
3939 if (kerr != KCGI_OK)
3941 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
3942 if (kerr != KCGI_OK)
3944 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3945 if (kerr != KCGI_OK)
3947 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3948 KATTR_ID, "head", KATTR__MAX);
3949 if (kerr != KCGI_OK)
3951 if (asprintf(&href_summary,
3952 "?path=%s&action=summary&headref=%s",
3953 gw_trans->repo_name, refname) == -1) {
3954 error = got_error_from_errno("asprintf");
3957 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
3958 href_summary, KATTR__MAX);
3959 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3960 if (kerr != KCGI_OK)
3962 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3963 if (kerr != KCGI_OK)
3966 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3967 "navs_wrapper", KATTR__MAX);
3968 if (kerr != KCGI_OK)
3970 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3971 "navs", KATTR__MAX);
3972 if (kerr != KCGI_OK)
3975 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
3976 href_summary, KATTR__MAX);
3977 if (kerr != KCGI_OK)
3979 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
3980 if (kerr != KCGI_OK)
3982 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3983 if (kerr != KCGI_OK)
3986 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3987 if (kerr != KCGI_OK)
3989 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
3990 gw_trans->repo_name, refname) == -1) {
3991 error = got_error_from_errno("asprintf");
3994 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
3995 href_briefs, KATTR__MAX);
3996 if (kerr != KCGI_OK)
3998 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
3999 if (kerr != KCGI_OK)
4001 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4002 if (kerr != KCGI_OK)
4005 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4006 if (kerr != KCGI_OK)
4009 if (asprintf(&href_commits,
4010 "?path=%s&action=commits&headref=%s",
4011 gw_trans->repo_name, refname) == -1) {
4012 error = got_error_from_errno("asprintf");
4015 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4016 href_commits, KATTR__MAX);
4017 if (kerr != KCGI_OK)
4019 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4020 if (kerr != KCGI_OK)
4022 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4023 if (kerr != KCGI_OK)
4026 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4027 "dotted_line", KATTR__MAX);
4028 if (kerr != KCGI_OK)
4030 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4031 if (kerr != KCGI_OK)
4034 href_summary = NULL;
4038 href_commits = NULL;
4041 got_ref_list_free(&refs);
4048 static const struct got_error *
4049 gw_output_site_link(struct gw_trans *gw_trans)
4051 const struct got_error *error = NULL;
4052 char *href_summary = NULL;
4053 enum kcgi_err kerr = KCGI_OK;
4055 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4056 "site_link", KATTR__MAX);
4057 if (kerr != KCGI_OK)
4059 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4061 if (kerr != KCGI_OK)
4063 kerr = khtml_puts(gw_trans->gw_html_req,
4064 gw_trans->gw_conf->got_site_link);
4065 if (kerr != KCGI_OK)
4067 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4068 if (kerr != KCGI_OK)
4071 if (gw_trans->repo_name != NULL) {
4072 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4073 if (kerr != KCGI_OK)
4075 if (asprintf(&href_summary, "?path=%s&action=summary",
4076 gw_trans->repo_name) == -1)
4078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4079 href_summary, KATTR__MAX);
4080 if (kerr != KCGI_OK)
4082 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4083 if (kerr != KCGI_OK)
4085 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4086 if (kerr != KCGI_OK)
4088 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4089 if (kerr != KCGI_OK)
4091 kerr = khtml_puts(gw_trans->gw_html_req,
4092 gw_get_action_name(gw_trans));
4093 if (kerr != KCGI_OK)
4097 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4098 if (kerr != KCGI_OK)
4105 static const struct got_error *
4106 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4108 const struct got_error *error = NULL;
4110 enum kcgi_err kerr = KCGI_OK;
4112 if (strncmp(buf, "-", 1) == 0)
4113 color = "diff_minus";
4114 else if (strncmp(buf, "+", 1) == 0)
4115 color = "diff_plus";
4116 else if (strncmp(buf, "@@", 2) == 0)
4117 color = "diff_chunk_header";
4118 else if (strncmp(buf, "@@", 2) == 0)
4119 color = "diff_chunk_header";
4120 else if (strncmp(buf, "commit +", 8) == 0)
4121 color = "diff_meta";
4122 else if (strncmp(buf, "commit -", 8) == 0)
4123 color = "diff_meta";
4124 else if (strncmp(buf, "blob +", 6) == 0)
4125 color = "diff_meta";
4126 else if (strncmp(buf, "blob -", 6) == 0)
4127 color = "diff_meta";
4128 else if (strncmp(buf, "file +", 6) == 0)
4129 color = "diff_meta";
4130 else if (strncmp(buf, "file -", 6) == 0)
4131 color = "diff_meta";
4132 else if (strncmp(buf, "from:", 5) == 0)
4133 color = "diff_author";
4134 else if (strncmp(buf, "via:", 4) == 0)
4135 color = "diff_author";
4136 else if (strncmp(buf, "date:", 5) == 0)
4137 color = "diff_date";
4138 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4139 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4140 if (error == NULL && kerr != KCGI_OK)
4141 error = gw_kcgi_error(kerr);
4146 main(int argc, char *argv[])
4148 const struct got_error *error = NULL;
4149 struct gw_trans *gw_trans;
4150 struct gw_dir *dir = NULL, *tdir;
4151 const char *page = "index";
4155 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4158 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4161 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4164 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4167 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4168 if (kerr != KCGI_OK) {
4169 error = gw_kcgi_error(kerr);
4173 if ((gw_trans->gw_conf =
4174 malloc(sizeof(struct gotweb_conf))) == NULL) {
4176 error = got_error_from_errno("malloc");
4180 TAILQ_INIT(&gw_trans->gw_dirs);
4181 TAILQ_INIT(&gw_trans->gw_headers);
4183 gw_trans->action = -1;
4185 gw_trans->repos_total = 0;
4186 gw_trans->repo_path = NULL;
4187 gw_trans->commit = NULL;
4188 gw_trans->headref = GOT_REF_HEAD;
4189 gw_trans->mime = KMIME_TEXT_HTML;
4190 gw_trans->gw_tmpl->key = gw_templs;
4191 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4192 gw_trans->gw_tmpl->arg = gw_trans;
4193 gw_trans->gw_tmpl->cb = gw_template;
4194 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4198 error = gw_parse_querystring(gw_trans);
4202 if (gw_trans->action == GW_BLOB)
4203 error = gw_blob(gw_trans);
4205 error = gw_display_index(gw_trans);
4208 free(gw_trans->gw_conf->got_repos_path);
4209 free(gw_trans->gw_conf->got_site_name);
4210 free(gw_trans->gw_conf->got_site_owner);
4211 free(gw_trans->gw_conf->got_site_link);
4212 free(gw_trans->gw_conf->got_logo);
4213 free(gw_trans->gw_conf->got_logo_url);
4214 free(gw_trans->gw_conf);
4215 free(gw_trans->commit);
4216 free(gw_trans->repo_path);
4218 got_repo_close(gw_trans->repo);
4220 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4222 free(dir->description);
4231 khttp_free(gw_trans->gw_req);