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;
329 enum kcgi_err kerr = KCGI_OK;
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 /* check querystring */
343 if (gw_trans->repo_file == NULL) {
344 error = got_error_msg(GOT_ERR_QUERYSTRING,
345 "file required in querystring");
348 if (gw_trans->commit_id == NULL) {
349 error = got_error_msg(GOT_ERR_QUERYSTRING,
350 "commit required in querystring");
354 error = gw_get_header(gw_trans, header, 1);
357 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
358 "blame_header_wrapper", KATTR__MAX);
361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
362 "blame_header", KATTR__MAX);
365 error = gw_get_time_str(&age, header->committer_time,
369 error = gw_gen_age_header(gw_trans, age ?age : "");
372 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
375 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
379 "dotted_line", KATTR__MAX);
382 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
387 "blame", KATTR__MAX);
390 error = gw_output_file_blame(gw_trans);
393 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
395 gw_free_header(header);
396 if (error == NULL && kerr != KCGI_OK)
397 error = gw_kcgi_error(kerr);
401 static const struct got_error *
402 gw_blob(struct gw_trans *gw_trans)
404 const struct got_error *error = NULL, *err = NULL;
405 struct gw_header *header = NULL;
406 enum kcgi_err kerr = KCGI_OK;
408 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
410 return got_error_from_errno("pledge");
412 if ((header = gw_init_header()) == NULL)
413 return got_error_from_errno("malloc");
415 error = gw_apply_unveil(gw_trans->gw_dir->path);
419 /* check querystring */
420 if (gw_trans->repo_file == NULL) {
421 error = got_error_msg(GOT_ERR_QUERYSTRING,
422 "file required in querystring");
425 if (gw_trans->commit_id == NULL) {
426 error = got_error_msg(GOT_ERR_QUERYSTRING,
427 "commit required in querystring");
430 error = gw_get_header(gw_trans, header, 1);
434 error = gw_output_blob_buf(gw_trans);
438 gw_trans->mime = KMIME_TEXT_PLAIN;
439 err = gw_display_index(gw_trans);
444 kerr = khttp_puts(gw_trans->gw_req, error->msg);
447 gw_free_header(header);
448 if (error == NULL && kerr != KCGI_OK)
449 error = gw_kcgi_error(kerr);
453 static const struct got_error *
454 gw_diff(struct gw_trans *gw_trans)
456 const struct got_error *error = NULL;
457 struct gw_header *header = NULL;
459 enum kcgi_err kerr = KCGI_OK;
461 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
463 return got_error_from_errno("pledge");
465 if ((header = gw_init_header()) == NULL)
466 return got_error_from_errno("malloc");
468 error = gw_apply_unveil(gw_trans->gw_dir->path);
472 error = gw_get_header(gw_trans, header, 1);
476 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
477 "diff_header_wrapper", KATTR__MAX);
480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
481 "diff_header", KATTR__MAX);
484 error = gw_gen_diff_header(gw_trans, header->parent_id,
488 error = gw_gen_commit_header(gw_trans, header->commit_id,
492 error = gw_gen_tree_header(gw_trans, header->tree_id);
495 error = gw_gen_author_header(gw_trans, header->author);
498 error = gw_gen_committer_header(gw_trans, header->author);
501 error = gw_get_time_str(&age, header->committer_time,
505 error = gw_gen_age_header(gw_trans, age ?age : "");
508 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
511 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
515 "dotted_line", KATTR__MAX);
518 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
522 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
526 error = gw_output_diff(gw_trans, header);
530 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
532 gw_free_header(header);
534 if (error == NULL && kerr != KCGI_OK)
535 error = gw_kcgi_error(kerr);
539 static const struct got_error *
540 gw_index(struct gw_trans *gw_trans)
542 const struct got_error *error = NULL;
543 struct gw_dir *gw_dir = NULL;
544 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
545 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
546 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
549 if (pledge("stdio rpath proc exec sendfd unveil",
551 error = got_error_from_errno("pledge");
555 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
559 error = gw_load_got_paths(gw_trans);
563 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
564 "index_header", KATTR__MAX);
566 return gw_kcgi_error(kerr);
567 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
568 "index_header_project", KATTR__MAX);
570 return gw_kcgi_error(kerr);
571 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
573 return gw_kcgi_error(kerr);
574 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
576 return gw_kcgi_error(kerr);
578 if (gw_trans->gw_conf->got_show_repo_description) {
579 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
580 "index_header_description", KATTR__MAX);
582 return gw_kcgi_error(kerr);
583 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
585 return gw_kcgi_error(kerr);
586 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
588 return gw_kcgi_error(kerr);
591 if (gw_trans->gw_conf->got_show_repo_owner) {
592 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
593 "index_header_owner", KATTR__MAX);
595 return gw_kcgi_error(kerr);
596 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
598 return gw_kcgi_error(kerr);
599 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
601 return gw_kcgi_error(kerr);
604 if (gw_trans->gw_conf->got_show_repo_age) {
605 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
606 "index_header_age", KATTR__MAX);
608 return gw_kcgi_error(kerr);
609 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
611 return gw_kcgi_error(kerr);
612 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
614 return gw_kcgi_error(kerr);
617 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
619 return gw_kcgi_error(kerr);
621 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
622 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
623 "index_wrapper", KATTR__MAX);
625 return gw_kcgi_error(kerr);
626 kerr = khtml_puts(gw_trans->gw_html_req,
627 "No repositories found in ");
629 return gw_kcgi_error(kerr);
630 kerr = khtml_puts(gw_trans->gw_html_req,
631 gw_trans->gw_conf->got_repos_path);
633 return gw_kcgi_error(kerr);
634 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
636 return gw_kcgi_error(kerr);
637 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
638 "dotted_line", KATTR__MAX);
640 return gw_kcgi_error(kerr);
641 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
643 return gw_kcgi_error(kerr);
647 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
650 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
651 if (gw_trans->page > 0 && (gw_trans->page *
652 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
659 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
660 "index_wrapper", KATTR__MAX);
664 if (asprintf(&href_summary, "?path=%s&action=summary",
665 gw_dir->name) == -1) {
666 error = got_error_from_errno("asprintf");
669 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
670 "index_project", KATTR__MAX);
673 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
674 href_summary, KATTR__MAX);
677 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
680 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
683 if (gw_trans->gw_conf->got_show_repo_description) {
684 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
685 KATTR_ID, "index_project_description", KATTR__MAX);
688 kerr = khtml_puts(gw_trans->gw_html_req,
689 gw_dir->description ? gw_dir->description : "");
692 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
696 if (gw_trans->gw_conf->got_show_repo_owner) {
697 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
698 KATTR_ID, "index_project_owner", KATTR__MAX);
701 kerr = khtml_puts(gw_trans->gw_html_req,
702 gw_dir->owner ? gw_dir->owner : "");
705 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
709 if (gw_trans->gw_conf->got_show_repo_age) {
710 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
711 KATTR_ID, "index_project_age", KATTR__MAX);
714 kerr = khtml_puts(gw_trans->gw_html_req,
715 gw_dir->age ? gw_dir->age : "");
718 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
723 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
724 "navs_wrapper", KATTR__MAX);
727 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
733 href_summary, KATTR__MAX);
736 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
739 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
743 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
747 if (asprintf(&href_briefs, "?path=%s&action=briefs",
748 gw_dir->name) == -1) {
749 error = got_error_from_errno("asprintf");
752 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
753 href_briefs, KATTR__MAX);
756 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
759 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
761 error = gw_kcgi_error(kerr);
763 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
767 if (asprintf(&href_commits, "?path=%s&action=commits",
768 gw_dir->name) == -1) {
769 error = got_error_from_errno("asprintf");
772 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
773 href_commits, KATTR__MAX);
776 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
779 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
783 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
787 if (asprintf(&href_tree, "?path=%s&action=tree",
788 gw_dir->name) == -1) {
789 error = got_error_from_errno("asprintf");
792 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
793 href_tree, KATTR__MAX);
796 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
800 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
803 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
804 "dotted_line", KATTR__MAX);
807 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
820 if (gw_trans->gw_conf->got_max_repos_display == 0)
823 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
824 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
825 KATTR_ID, "np_wrapper", KATTR__MAX);
828 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
829 KATTR_ID, "nav_prev", KATTR__MAX);
832 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
833 (gw_trans->page > 0) &&
834 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
835 prev_disp == gw_trans->repos_total)) {
836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
837 KATTR_ID, "np_wrapper", KATTR__MAX);
840 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
841 KATTR_ID, "nav_prev", KATTR__MAX);
846 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
847 (gw_trans->page > 0) &&
848 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
849 prev_disp == gw_trans->repos_total)) {
850 if (asprintf(&href_prev, "?page=%d",
851 gw_trans->page - 1) == -1) {
852 error = got_error_from_errno("asprintf");
855 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
856 KATTR_HREF, href_prev, KATTR__MAX);
859 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
862 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
867 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
869 return gw_kcgi_error(kerr);
871 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
872 next_disp == gw_trans->gw_conf->got_max_repos_display &&
873 dir_c != (gw_trans->page + 1) *
874 gw_trans->gw_conf->got_max_repos_display) {
875 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
876 KATTR_ID, "nav_next", KATTR__MAX);
879 if (asprintf(&href_next, "?page=%d",
880 gw_trans->page + 1) == -1) {
881 error = got_error_from_errno("calloc");
884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
885 KATTR_HREF, href_next, KATTR__MAX);
888 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
891 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
898 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
899 (gw_trans->page > 0) &&
900 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
901 prev_disp == gw_trans->repos_total)) {
902 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
915 if (error == NULL && kerr != KCGI_OK)
916 error = gw_kcgi_error(kerr);
920 static const struct got_error *
921 gw_commits(struct gw_trans *gw_trans)
923 const struct got_error *error = NULL;
924 struct gw_header *header = NULL, *n_header = NULL;
926 char *href_diff = NULL, *href_blob = NULL;
927 enum kcgi_err kerr = KCGI_OK;
929 if ((header = gw_init_header()) == NULL)
930 return got_error_from_errno("malloc");
932 if (pledge("stdio rpath proc exec sendfd unveil",
934 error = got_error_from_errno("pledge");
938 error = gw_apply_unveil(gw_trans->gw_dir->path);
942 error = gw_get_header(gw_trans, header,
943 gw_trans->gw_conf->got_max_commits_display);
947 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
948 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
949 "commits_line_wrapper", KATTR__MAX);
952 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
956 error = gw_gen_author_header(gw_trans, n_header->author);
959 error = gw_gen_committer_header(gw_trans, n_header->author);
962 error = gw_get_time_str(&age, n_header->committer_time,
966 error = gw_gen_age_header(gw_trans, age ?age : "");
969 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
973 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
974 "dotted_line", KATTR__MAX);
977 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
981 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
982 "commit", KATTR__MAX);
985 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
988 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
992 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
993 gw_trans->repo_name, n_header->commit_id) == -1) {
994 error = got_error_from_errno("asprintf");
997 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
998 KATTR_ID, "navs_wrapper", KATTR__MAX);
1001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1002 KATTR_ID, "navs", KATTR__MAX);
1003 if (kerr != KCGI_OK)
1005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1006 KATTR_HREF, href_diff, KATTR__MAX);
1007 if (kerr != KCGI_OK)
1009 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1010 if (kerr != KCGI_OK)
1012 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1013 if (kerr != KCGI_OK)
1016 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1017 if (kerr != KCGI_OK)
1020 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1021 gw_trans->repo_name, n_header->commit_id) == -1) {
1022 error = got_error_from_errno("asprintf");
1025 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1026 KATTR_HREF, href_blob, KATTR__MAX);
1027 if (kerr != KCGI_OK)
1029 khtml_puts(gw_trans->gw_html_req, "tree");
1030 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1031 if (kerr != KCGI_OK)
1033 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1034 if (kerr != KCGI_OK)
1037 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1038 "solid_line", KATTR__MAX);
1039 if (kerr != KCGI_OK)
1041 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1042 if (kerr != KCGI_OK)
1049 gw_free_header(header);
1050 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1051 gw_free_header(n_header);
1055 if (error == NULL && kerr != KCGI_OK)
1056 error = gw_kcgi_error(kerr);
1060 static const struct got_error *
1061 gw_briefs(struct gw_trans *gw_trans)
1063 const struct got_error *error = NULL;
1064 struct gw_header *header = NULL, *n_header = NULL;
1066 char *href_diff = NULL, *href_blob = NULL;
1067 char *newline, *smallerthan;
1068 enum kcgi_err kerr = KCGI_OK;
1070 if ((header = gw_init_header()) == NULL)
1071 return got_error_from_errno("malloc");
1073 if (pledge("stdio rpath proc exec sendfd unveil",
1075 error = got_error_from_errno("pledge");
1079 if (gw_trans->action != GW_SUMMARY) {
1080 error = gw_apply_unveil(gw_trans->gw_dir->path);
1085 if (gw_trans->action == GW_SUMMARY)
1086 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1088 error = gw_get_header(gw_trans, header,
1089 gw_trans->gw_conf->got_max_commits_display);
1093 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1094 error = gw_get_time_str(&age, n_header->committer_time,
1099 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1100 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1101 if (kerr != KCGI_OK)
1104 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1105 KATTR_ID, "briefs_age", KATTR__MAX);
1106 if (kerr != KCGI_OK)
1108 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1109 if (kerr != KCGI_OK)
1111 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1112 if (kerr != KCGI_OK)
1115 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1116 KATTR_ID, "briefs_author", KATTR__MAX);
1117 if (kerr != KCGI_OK)
1119 smallerthan = strchr(n_header->author, '<');
1121 *smallerthan = '\0';
1122 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1123 if (kerr != KCGI_OK)
1125 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1126 if (kerr != KCGI_OK)
1129 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1130 gw_trans->repo_name, n_header->commit_id) == -1) {
1131 error = got_error_from_errno("asprintf");
1134 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1135 KATTR_ID, "briefs_log", KATTR__MAX);
1136 if (kerr != KCGI_OK)
1138 newline = strchr(n_header->commit_msg, '\n');
1141 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1142 KATTR_HREF, href_diff, KATTR__MAX);
1143 if (kerr != KCGI_OK)
1145 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1146 if (kerr != KCGI_OK)
1148 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1149 if (kerr != KCGI_OK)
1152 if (n_header->refs_str) {
1153 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1154 if (kerr != KCGI_OK)
1156 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1157 KATTR_ID, "refs_str", KATTR__MAX);
1158 if (kerr != KCGI_OK)
1160 kerr = khtml_puts(gw_trans->gw_html_req, "(");
1161 if (kerr != KCGI_OK)
1163 kerr = khtml_puts(gw_trans->gw_html_req,
1164 n_header->refs_str);
1165 if (kerr != KCGI_OK)
1167 kerr = khtml_puts(gw_trans->gw_html_req, ")");
1168 if (kerr != KCGI_OK)
1170 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1171 if (kerr != KCGI_OK)
1175 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1176 if (kerr != KCGI_OK)
1179 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1180 KATTR_ID, "navs_wrapper", KATTR__MAX);
1181 if (kerr != KCGI_OK)
1183 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1184 KATTR_ID, "navs", KATTR__MAX);
1185 if (kerr != KCGI_OK)
1187 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1188 KATTR_HREF, href_diff, KATTR__MAX);
1189 if (kerr != KCGI_OK)
1191 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1192 if (kerr != KCGI_OK)
1194 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1195 if (kerr != KCGI_OK)
1198 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1199 if (kerr != KCGI_OK)
1202 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1203 gw_trans->repo_name, n_header->commit_id) == -1) {
1204 error = got_error_from_errno("asprintf");
1207 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1208 KATTR_HREF, href_blob, KATTR__MAX);
1209 if (kerr != KCGI_OK)
1211 khtml_puts(gw_trans->gw_html_req, "tree");
1212 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1213 if (kerr != KCGI_OK)
1215 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1216 if (kerr != KCGI_OK)
1219 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1220 KATTR_ID, "dotted_line", KATTR__MAX);
1221 if (kerr != KCGI_OK)
1223 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1224 if (kerr != KCGI_OK)
1235 gw_free_header(header);
1236 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1237 gw_free_header(n_header);
1241 if (error == NULL && kerr != KCGI_OK)
1242 error = gw_kcgi_error(kerr);
1246 static const struct got_error *
1247 gw_summary(struct gw_trans *gw_trans)
1249 const struct got_error *error = NULL;
1251 enum kcgi_err kerr = KCGI_OK;
1253 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1254 return got_error_from_errno("pledge");
1256 error = gw_apply_unveil(gw_trans->gw_dir->path);
1260 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1261 "summary_wrapper", KATTR__MAX);
1262 if (kerr != KCGI_OK)
1263 return gw_kcgi_error(kerr);
1265 if (gw_trans->gw_conf->got_show_repo_description &&
1266 gw_trans->gw_dir->description != NULL &&
1267 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1268 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1269 KATTR_ID, "description_title", KATTR__MAX);
1270 if (kerr != KCGI_OK)
1272 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1273 if (kerr != KCGI_OK)
1275 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1276 if (kerr != KCGI_OK)
1278 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1279 KATTR_ID, "description", KATTR__MAX);
1280 if (kerr != KCGI_OK)
1282 kerr = khtml_puts(gw_trans->gw_html_req,
1283 gw_trans->gw_dir->description);
1284 if (kerr != KCGI_OK)
1286 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1287 if (kerr != KCGI_OK)
1291 if (gw_trans->gw_conf->got_show_repo_owner &&
1292 gw_trans->gw_dir->owner != NULL &&
1293 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1294 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1295 KATTR_ID, "repo_owner_title", KATTR__MAX);
1296 if (kerr != KCGI_OK)
1298 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1299 if (kerr != KCGI_OK)
1301 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1302 if (kerr != KCGI_OK)
1304 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1305 KATTR_ID, "repo_owner", KATTR__MAX);
1306 if (kerr != KCGI_OK)
1308 kerr = khtml_puts(gw_trans->gw_html_req,
1309 gw_trans->gw_dir->owner);
1310 if (kerr != KCGI_OK)
1312 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1313 if (kerr != KCGI_OK)
1317 if (gw_trans->gw_conf->got_show_repo_age) {
1318 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1323 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1324 KATTR_ID, "last_change_title", KATTR__MAX);
1325 if (kerr != KCGI_OK)
1327 kerr = khtml_puts(gw_trans->gw_html_req,
1329 if (kerr != KCGI_OK)
1331 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1332 if (kerr != KCGI_OK)
1334 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1335 KATTR_ID, "last_change", KATTR__MAX);
1336 if (kerr != KCGI_OK)
1338 kerr = khtml_puts(gw_trans->gw_html_req, age);
1339 if (kerr != KCGI_OK)
1341 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1342 if (kerr != KCGI_OK)
1347 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1348 gw_trans->gw_dir->url != NULL &&
1349 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1350 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1351 KATTR_ID, "cloneurl_title", KATTR__MAX);
1352 if (kerr != KCGI_OK)
1354 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1355 if (kerr != KCGI_OK)
1357 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1358 if (kerr != KCGI_OK)
1360 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1361 KATTR_ID, "cloneurl", KATTR__MAX);
1362 if (kerr != KCGI_OK)
1364 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1365 if (kerr != KCGI_OK)
1367 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1368 if (kerr != KCGI_OK)
1372 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1373 if (kerr != KCGI_OK)
1376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1377 "briefs_title_wrapper", KATTR__MAX);
1378 if (kerr != KCGI_OK)
1380 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1381 "briefs_title", KATTR__MAX);
1382 if (kerr != KCGI_OK)
1384 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1385 if (kerr != KCGI_OK)
1387 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1388 if (kerr != KCGI_OK)
1390 error = gw_briefs(gw_trans);
1394 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1399 error = gw_output_repo_heads(gw_trans);
1402 if (error == NULL && kerr != KCGI_OK)
1403 error = gw_kcgi_error(kerr);
1407 static const struct got_error *
1408 gw_tree(struct gw_trans *gw_trans)
1410 const struct got_error *error = NULL;
1411 struct gw_header *header = NULL;
1412 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1416 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1417 return got_error_from_errno("pledge");
1419 if ((header = gw_init_header()) == NULL)
1420 return got_error_from_errno("malloc");
1422 error = gw_apply_unveil(gw_trans->gw_dir->path);
1426 error = gw_get_header(gw_trans, header, 1);
1430 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1431 "tree_header_wrapper", KATTR__MAX);
1432 if (kerr != KCGI_OK)
1434 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1435 "tree_header", KATTR__MAX);
1436 if (kerr != KCGI_OK)
1438 error = gw_gen_tree_header(gw_trans, header->tree_id);
1441 error = gw_get_time_str(&age, header->committer_time,
1445 error = gw_gen_age_header(gw_trans, age ?age : "");
1448 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1451 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1452 if (kerr != KCGI_OK)
1454 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1455 "dotted_line", KATTR__MAX);
1456 if (kerr != KCGI_OK)
1458 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1459 if (kerr != KCGI_OK)
1462 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1463 "tree", KATTR__MAX);
1464 if (kerr != KCGI_OK)
1466 error = gw_output_repo_tree(gw_trans);
1470 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1472 gw_free_header(header);
1473 free(tree_html_disp);
1477 if (error == NULL && kerr != KCGI_OK)
1478 error = gw_kcgi_error(kerr);
1482 static const struct got_error *
1483 gw_tag(struct gw_trans *gw_trans)
1485 const struct got_error *error = NULL;
1486 struct gw_header *header = NULL;
1489 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1490 return got_error_from_errno("pledge");
1492 if ((header = gw_init_header()) == NULL)
1493 return got_error_from_errno("malloc");
1495 error = gw_apply_unveil(gw_trans->gw_dir->path);
1499 if (gw_trans->commit_id == NULL) {
1500 error = got_error_msg(GOT_ERR_QUERYSTRING,
1501 "commit required in querystring");
1505 error = gw_get_header(gw_trans, header, 1);
1509 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1510 "tag_header_wrapper", KATTR__MAX);
1511 if (kerr != KCGI_OK)
1513 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1514 "tag_header", KATTR__MAX);
1515 if (kerr != KCGI_OK)
1517 error = gw_gen_commit_header(gw_trans, header->commit_id,
1521 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1524 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1525 if (kerr != KCGI_OK)
1527 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1528 "dotted_line", KATTR__MAX);
1529 if (kerr != KCGI_OK)
1531 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1532 if (kerr != KCGI_OK)
1535 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1536 "tree", KATTR__MAX);
1537 if (kerr != KCGI_OK)
1540 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1544 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1546 gw_free_header(header);
1547 if (error == NULL && kerr != KCGI_OK)
1548 error = gw_kcgi_error(kerr);
1552 static const struct got_error *
1553 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1555 const struct got_error *error = NULL;
1560 if (asprintf(&dir_test, "%s/%s/%s",
1561 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1562 GOTWEB_GIT_DIR) == -1)
1563 return got_error_from_errno("asprintf");
1565 dt = opendir(dir_test);
1569 gw_dir->path = strdup(dir_test);
1570 if (gw_dir->path == NULL) {
1572 error = got_error_from_errno("strdup");
1579 if (asprintf(&dir_test, "%s/%s/%s",
1580 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1581 GOTWEB_GOT_DIR) == -1) {
1583 error = got_error_from_errno("asprintf");
1587 dt = opendir(dir_test);
1592 error = got_error(GOT_ERR_NOT_GIT_REPO);
1596 if (asprintf(&dir_test, "%s/%s",
1597 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1598 error = got_error_from_errno("asprintf");
1603 gw_dir->path = strdup(dir_test);
1604 if (gw_dir->path == NULL) {
1606 error = got_error_from_errno("strdup");
1610 dt = opendir(dir_test);
1612 error = got_error_from_errno2("bad path", dir_test);
1617 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1621 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1624 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1628 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1632 if (d && closedir(dt) == -1 && error == NULL)
1633 error = got_error_from_errno("closedir");
1637 static const struct got_error *
1638 gw_load_got_paths(struct gw_trans *gw_trans)
1640 const struct got_error *error = NULL;
1642 struct dirent **sd_dent;
1643 struct gw_dir *gw_dir;
1645 unsigned int d_cnt, d_i;
1647 d = opendir(gw_trans->gw_conf->got_repos_path);
1649 error = got_error_from_errno2("opendir",
1650 gw_trans->gw_conf->got_repos_path);
1654 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1657 error = got_error_from_errno2("scandir",
1658 gw_trans->gw_conf->got_repos_path);
1662 for (d_i = 0; d_i < d_cnt; d_i++) {
1663 if (gw_trans->gw_conf->got_max_repos > 0 &&
1664 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1665 break; /* account for parent and self */
1667 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1668 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1671 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1675 error = gw_load_got_path(gw_trans, gw_dir);
1676 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1683 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1684 !got_path_dir_is_empty(gw_dir->path)) {
1685 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1687 gw_trans->repos_total++;
1691 if (d && closedir(d) == -1 && error == NULL)
1692 error = got_error_from_errno("closedir");
1696 static const struct got_error *
1697 gw_parse_querystring(struct gw_trans *gw_trans)
1699 const struct got_error *error = NULL;
1701 struct gw_query_action *action = NULL;
1704 if (gw_trans->gw_req->fieldnmap[0]) {
1705 error = got_error_from_errno("bad parse");
1707 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1708 /* define gw_trans->repo_path */
1709 gw_trans->repo_name = p->parsed.s;
1711 if (asprintf(&gw_trans->repo_path, "%s/%s",
1712 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1713 return got_error_from_errno("asprintf");
1715 /* get action and set function */
1716 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1717 for (i = 0; i < nitems(gw_query_funcs); i++) {
1718 action = &gw_query_funcs[i];
1719 if (action->func_name == NULL)
1721 if (strcmp(action->func_name,
1722 p->parsed.s) == 0) {
1723 gw_trans->action = i;
1728 if (gw_trans->action == -1) {
1729 gw_trans->action = GW_ERR;
1730 gw_trans->error = got_error_from_errno("bad action");
1734 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1735 if (asprintf(&gw_trans->commit_id, "%s",
1737 return got_error_from_errno("asprintf");
1740 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1741 gw_trans->repo_file = p->parsed.s;
1743 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1744 if (asprintf(&gw_trans->repo_folder, "%s",
1746 return got_error_from_errno("asprintf");
1749 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1750 gw_trans->headref = p->parsed.s;
1752 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1756 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1758 gw_trans->action = GW_INDEX;
1760 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1761 gw_trans->page = p->parsed.i;
1766 static const struct got_error *
1767 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1769 const struct got_error *error;
1771 *gw_dir = malloc(sizeof(**gw_dir));
1772 if (*gw_dir == NULL)
1773 return got_error_from_errno("malloc");
1775 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1776 error = got_error_from_errno("asprintf");
1785 static const struct got_error *
1786 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1790 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1791 if (kerr != KCGI_OK)
1792 return gw_kcgi_error(kerr);
1793 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1795 if (kerr != KCGI_OK)
1796 return gw_kcgi_error(kerr);
1797 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1799 if (kerr != KCGI_OK)
1800 return gw_kcgi_error(kerr);
1801 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1803 if (kerr != KCGI_OK)
1804 return gw_kcgi_error(kerr);
1805 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1806 if (kerr != KCGI_OK)
1807 return gw_kcgi_error(kerr);
1808 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1810 if (kerr != KCGI_OK)
1811 return gw_kcgi_error(kerr);
1813 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1814 kerr = khttp_head(gw_trans->gw_req,
1815 kresps[KRESP_CONTENT_DISPOSITION],
1816 "attachment; filename=%s", gw_trans->repo_file);
1817 if (kerr != KCGI_OK)
1818 return gw_kcgi_error(kerr);
1821 kerr = khttp_body(gw_trans->gw_req);
1822 return gw_kcgi_error(kerr);
1825 static const struct got_error *
1826 gw_display_index(struct gw_trans *gw_trans)
1828 const struct got_error *error;
1831 /* catch early querystring errors */
1832 if (gw_trans->error)
1833 gw_trans->action = GW_ERR;
1835 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1839 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1840 if (kerr != KCGI_OK)
1841 return gw_kcgi_error(kerr);
1843 if (gw_trans->action != GW_BLOB) {
1844 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1845 gw_query_funcs[gw_trans->action].template);
1846 if (kerr != KCGI_OK) {
1847 khtml_close(gw_trans->gw_html_req);
1848 return gw_kcgi_error(kerr);
1852 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1855 static const struct got_error *
1856 gw_error(struct gw_trans *gw_trans)
1860 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
1862 return gw_kcgi_error(kerr);
1866 gw_template(size_t key, void *arg)
1868 const struct got_error *error = NULL;
1870 struct gw_trans *gw_trans = arg;
1871 char *img_src = NULL;
1875 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1876 KATTR_NAME, "viewport",
1877 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1879 if (kerr != KCGI_OK)
1881 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1882 if (kerr != KCGI_OK)
1884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1885 KATTR_CHARSET, "utf-8",
1887 if (kerr != KCGI_OK)
1889 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1890 if (kerr != KCGI_OK)
1892 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1893 KATTR_NAME, "msapplication-TileColor",
1894 KATTR_CONTENT, "#da532c", KATTR__MAX);
1895 if (kerr != KCGI_OK)
1897 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1898 if (kerr != KCGI_OK)
1900 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1901 KATTR_NAME, "theme-color",
1902 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1903 if (kerr != KCGI_OK)
1905 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1906 if (kerr != KCGI_OK)
1908 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1909 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1910 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1911 if (kerr != KCGI_OK)
1913 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1914 if (kerr != KCGI_OK)
1916 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1917 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1918 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1919 if (kerr != KCGI_OK)
1921 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1922 if (kerr != KCGI_OK)
1924 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1925 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1926 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1927 if (kerr != KCGI_OK)
1929 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1930 if (kerr != KCGI_OK)
1932 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1933 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1935 if (kerr != KCGI_OK)
1937 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1938 if (kerr != KCGI_OK)
1940 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1941 KATTR_REL, "mask-icon", KATTR_HREF,
1942 "/safari-pinned-tab.svg", KATTR__MAX);
1943 if (kerr != KCGI_OK)
1945 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1946 if (kerr != KCGI_OK)
1948 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1949 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1950 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1951 if (kerr != KCGI_OK)
1953 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1954 if (kerr != KCGI_OK)
1958 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1959 KATTR_ID, "got_link", KATTR__MAX);
1960 if (kerr != KCGI_OK)
1962 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1963 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1964 KATTR_TARGET, "_sotd", KATTR__MAX);
1965 if (kerr != KCGI_OK)
1967 if (asprintf(&img_src, "/%s",
1968 gw_trans->gw_conf->got_logo) == -1)
1970 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1971 KATTR_SRC, img_src, KATTR__MAX);
1972 if (kerr != KCGI_OK) {
1976 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1977 if (kerr != KCGI_OK) {
1982 case (TEMPL_SITEPATH):
1983 error = gw_output_site_link(gw_trans);
1988 if (gw_trans->gw_conf->got_site_name != NULL) {
1989 kerr = khtml_puts(gw_trans->gw_html_req,
1990 gw_trans->gw_conf->got_site_name);
1991 if (kerr != KCGI_OK)
1995 case (TEMPL_SEARCH):
1997 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1998 "search", KATTR__MAX);
1999 if (kerr != KCGI_OK)
2001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2002 KATTR_METHOD, "POST", KATTR__MAX);
2003 if (kerr != KCGI_OK)
2005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2006 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2007 KATTR_MAXLENGTH, "50", KATTR__MAX);
2008 if (kerr != KCGI_OK)
2010 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2012 if (kerr != KCGI_OK)
2014 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2015 if (kerr != KCGI_OK)
2017 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2018 if (kerr != KCGI_OK)
2021 case(TEMPL_SITEOWNER):
2022 if (gw_trans->gw_conf->got_site_owner != NULL &&
2023 gw_trans->gw_conf->got_show_site_owner) {
2024 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2025 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2026 if (kerr != KCGI_OK)
2028 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2029 KATTR_ID, "site_owner", KATTR__MAX);
2030 if (kerr != KCGI_OK)
2032 kerr = khtml_puts(gw_trans->gw_html_req,
2033 gw_trans->gw_conf->got_site_owner);
2034 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2035 if (kerr != KCGI_OK)
2039 case(TEMPL_CONTENT):
2040 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2042 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2043 KATTR_ID, "tmpl_err", KATTR__MAX);
2044 if (kerr != KCGI_OK)
2046 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
2047 if (kerr != KCGI_OK)
2049 kerr = khttp_puts(gw_trans->gw_req, error->msg);
2050 if (kerr != KCGI_OK)
2052 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2053 if (kerr != KCGI_OK)
2063 static const struct got_error *
2064 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2066 const struct got_error *error = NULL;
2067 enum kcgi_err kerr = KCGI_OK;
2069 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2070 KATTR_ID, "header_commit_title", KATTR__MAX);
2071 if (kerr != KCGI_OK)
2073 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2074 if (kerr != KCGI_OK)
2076 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2077 if (kerr != KCGI_OK)
2079 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2080 KATTR_ID, "header_commit", KATTR__MAX);
2081 if (kerr != KCGI_OK)
2083 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2084 if (kerr != KCGI_OK)
2086 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2087 if (kerr != KCGI_OK)
2090 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2091 KATTR_ID, "refs_str", KATTR__MAX);
2092 if (kerr != KCGI_OK)
2094 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2095 if (kerr != KCGI_OK)
2097 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2098 if (kerr != KCGI_OK)
2100 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2101 if (kerr != KCGI_OK)
2103 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2104 if (kerr != KCGI_OK)
2107 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2109 if (error == NULL && kerr != KCGI_OK)
2110 error = gw_kcgi_error(kerr);
2114 static const struct got_error *
2115 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2117 const struct got_error *error = NULL;
2118 enum kcgi_err kerr = KCGI_OK;
2120 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2121 KATTR_ID, "header_diff_title", KATTR__MAX);
2122 if (kerr != KCGI_OK)
2124 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2125 if (kerr != KCGI_OK)
2127 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2128 if (kerr != KCGI_OK)
2130 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2131 KATTR_ID, "header_diff", KATTR__MAX);
2132 if (kerr != KCGI_OK)
2135 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2136 if (kerr != KCGI_OK)
2139 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2140 if (kerr != KCGI_OK)
2142 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2143 if (kerr != KCGI_OK)
2145 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2147 if (error == NULL && kerr != KCGI_OK)
2148 error = gw_kcgi_error(kerr);
2152 static const struct got_error *
2153 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2155 const struct got_error *error = NULL;
2156 enum kcgi_err kerr = KCGI_OK;
2158 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2159 KATTR_ID, "header_age_title", KATTR__MAX);
2160 if (kerr != KCGI_OK)
2162 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2163 if (kerr != KCGI_OK)
2165 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2166 if (kerr != KCGI_OK)
2168 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2169 KATTR_ID, "header_age", KATTR__MAX);
2170 if (kerr != KCGI_OK)
2172 kerr = khtml_puts(gw_trans->gw_html_req, str);
2173 if (kerr != KCGI_OK)
2175 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2177 if (error == NULL && kerr != KCGI_OK)
2178 error = gw_kcgi_error(kerr);
2182 static const struct got_error *
2183 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2185 const struct got_error *error = NULL;
2188 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2189 KATTR_ID, "header_author_title", KATTR__MAX);
2190 if (kerr != KCGI_OK)
2192 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2193 if (kerr != KCGI_OK)
2195 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2196 if (kerr != KCGI_OK)
2198 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2199 KATTR_ID, "header_author", KATTR__MAX);
2200 if (kerr != KCGI_OK)
2202 kerr = khtml_puts(gw_trans->gw_html_req, str);
2203 if (kerr != KCGI_OK)
2205 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2207 if (error == NULL && kerr != KCGI_OK)
2208 error = gw_kcgi_error(kerr);
2212 static const struct got_error *
2213 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2215 const struct got_error *error = NULL;
2218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2219 KATTR_ID, "header_committer_title", KATTR__MAX);
2220 if (kerr != KCGI_OK)
2222 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2223 if (kerr != KCGI_OK)
2225 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2226 if (kerr != KCGI_OK)
2228 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2229 KATTR_ID, "header_committer", KATTR__MAX);
2230 if (kerr != KCGI_OK)
2232 kerr = khtml_puts(gw_trans->gw_html_req, str);
2233 if (kerr != KCGI_OK)
2235 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2237 if (error == NULL && kerr != KCGI_OK)
2238 error = gw_kcgi_error(kerr);
2242 static const struct got_error *
2243 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2245 const struct got_error *error = NULL;
2248 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2249 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2250 if (kerr != KCGI_OK)
2252 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2253 if (kerr != KCGI_OK)
2255 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2256 if (kerr != KCGI_OK)
2258 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2259 KATTR_ID, "header_commit_msg", KATTR__MAX);
2260 if (kerr != KCGI_OK)
2262 kerr = khttp_puts(gw_trans->gw_req, str);
2263 if (kerr != KCGI_OK)
2265 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2267 if (error == NULL && kerr != KCGI_OK)
2268 error = gw_kcgi_error(kerr);
2272 static const struct got_error *
2273 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2275 const struct got_error *error = NULL;
2278 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2279 KATTR_ID, "header_tree_title", KATTR__MAX);
2280 if (kerr != KCGI_OK)
2282 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2283 if (kerr != KCGI_OK)
2285 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2286 if (kerr != KCGI_OK)
2288 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2289 KATTR_ID, "header_tree", KATTR__MAX);
2290 if (kerr != KCGI_OK)
2292 kerr = khtml_puts(gw_trans->gw_html_req, str);
2293 if (kerr != KCGI_OK)
2295 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2297 if (error == NULL && kerr != KCGI_OK)
2298 error = gw_kcgi_error(kerr);
2302 static const struct got_error *
2303 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2306 const struct got_error *error = NULL;
2308 char *d_file = NULL;
2312 *description = NULL;
2313 if (gw_trans->gw_conf->got_show_repo_description == 0)
2316 if (asprintf(&d_file, "%s/description", dir) == -1)
2317 return got_error_from_errno("asprintf");
2319 f = fopen(d_file, "r");
2321 if (errno == ENOENT || errno == EACCES)
2323 error = got_error_from_errno2("fopen", d_file);
2327 if (fseek(f, 0, SEEK_END) == -1) {
2328 error = got_ferror(f, GOT_ERR_IO);
2333 error = got_ferror(f, GOT_ERR_IO);
2336 if (fseek(f, 0, SEEK_SET) == -1) {
2337 error = got_ferror(f, GOT_ERR_IO);
2340 *description = calloc(len + 1, sizeof(**description));
2341 if (*description == NULL) {
2342 error = got_error_from_errno("calloc");
2346 n = fread(*description, 1, len, f);
2347 if (n == 0 && ferror(f))
2348 error = got_ferror(f, GOT_ERR_IO);
2350 if (f != NULL && fclose(f) == -1 && error == NULL)
2351 error = got_error_from_errno("fclose");
2356 static const struct got_error *
2357 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2361 char *years = "years ago", *months = "months ago";
2362 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2363 char *minutes = "minutes ago", *seconds = "seconds ago";
2364 char *now = "right now";
2372 diff_time = time(NULL) - committer_time;
2373 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2374 if (asprintf(repo_age, "%lld %s",
2375 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2376 return got_error_from_errno("asprintf");
2377 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2378 if (asprintf(repo_age, "%lld %s",
2379 (diff_time / 60 / 60 / 24 / (365 / 12)),
2381 return got_error_from_errno("asprintf");
2382 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2383 if (asprintf(repo_age, "%lld %s",
2384 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2385 return got_error_from_errno("asprintf");
2386 } else if (diff_time > 60 * 60 * 24 * 2) {
2387 if (asprintf(repo_age, "%lld %s",
2388 (diff_time / 60 / 60 / 24), days) == -1)
2389 return got_error_from_errno("asprintf");
2390 } else if (diff_time > 60 * 60 * 2) {
2391 if (asprintf(repo_age, "%lld %s",
2392 (diff_time / 60 / 60), hours) == -1)
2393 return got_error_from_errno("asprintf");
2394 } else if (diff_time > 60 * 2) {
2395 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2397 return got_error_from_errno("asprintf");
2398 } else if (diff_time > 2) {
2399 if (asprintf(repo_age, "%lld %s", diff_time,
2401 return got_error_from_errno("asprintf");
2403 if (asprintf(repo_age, "%s", now) == -1)
2404 return got_error_from_errno("asprintf");
2408 if (gmtime_r(&committer_time, &tm) == NULL)
2409 return got_error_from_errno("gmtime_r");
2411 s = asctime_r(&tm, datebuf);
2413 return got_error_from_errno("asctime_r");
2415 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2416 return got_error_from_errno("asprintf");
2422 static const struct got_error *
2423 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2424 const char *refname, int ref_tm)
2426 const struct got_error *error = NULL;
2427 struct got_repository *repo = NULL;
2428 struct got_commit_object *commit = NULL;
2429 struct got_reflist_head refs;
2430 struct got_reflist_entry *re;
2431 time_t committer_time = 0, cmp_time = 0;
2434 SIMPLEQ_INIT(&refs);
2436 if (gw_trans->gw_conf->got_show_repo_age == 0)
2440 repo = gw_trans->repo;
2442 error = got_repo_open(&repo, dir, NULL);
2447 error = got_ref_list(&refs, repo, "refs/heads",
2448 got_ref_cmp_by_name, NULL);
2453 * Find the youngest branch tip in the repository, or the age of
2454 * the a specific branch tip if a name was provided by the caller.
2456 SIMPLEQ_FOREACH(re, &refs, entry) {
2457 struct got_object_id *id = NULL;
2459 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2462 error = got_ref_resolve(&id, repo, re->ref);
2466 error = got_object_open_as_commit(&commit, repo, id);
2472 got_object_commit_get_committer_time(commit);
2473 got_object_commit_close(commit);
2474 if (cmp_time < committer_time)
2475 cmp_time = committer_time;
2481 if (cmp_time != 0) {
2482 committer_time = cmp_time;
2483 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2486 got_ref_list_free(&refs);
2487 if (gw_trans->repo == NULL)
2488 got_repo_close(repo);
2492 static const struct got_error *
2493 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2495 const struct got_error *error;
2497 struct got_object_id *id1 = NULL, *id2 = NULL;
2498 char *label1 = NULL, *label2 = NULL, *line = NULL;
2500 size_t linesize = 0;
2502 enum kcgi_err kerr = KCGI_OK;
2508 if (header->parent_id != NULL &&
2509 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2510 error = got_repo_match_object_id(&id1, &label1,
2511 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2516 error = got_repo_match_object_id(&id2, &label2,
2517 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2521 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2525 case GOT_OBJ_TYPE_BLOB:
2526 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2529 case GOT_OBJ_TYPE_TREE:
2530 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2533 case GOT_OBJ_TYPE_COMMIT:
2534 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2538 error = got_error(GOT_ERR_OBJ_TYPE);
2543 if (fseek(f, 0, SEEK_SET) == -1) {
2544 error = got_ferror(f, GOT_ERR_IO);
2548 while ((linelen = getline(&line, &linesize, f)) != -1) {
2549 error = gw_colordiff_line(gw_trans, line);
2552 /* XXX: KHTML_PRETTY breaks this */
2553 kerr = khtml_puts(gw_trans->gw_html_req, line);
2554 if (kerr != KCGI_OK)
2556 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2557 if (kerr != KCGI_OK)
2560 if (linelen == -1 && ferror(f))
2561 error = got_error_from_errno("getline");
2563 if (f && fclose(f) == -1 && error == NULL)
2564 error = got_error_from_errno("fclose");
2571 if (error == NULL && kerr != KCGI_OK)
2572 error = gw_kcgi_error(kerr);
2576 static const struct got_error *
2577 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2579 const struct got_error *error = NULL;
2580 struct got_repository *repo;
2581 const char *gitconfig_owner;
2585 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2588 error = got_repo_open(&repo, dir, NULL);
2591 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2592 if (gitconfig_owner) {
2593 *owner = strdup(gitconfig_owner);
2595 error = got_error_from_errno("strdup");
2597 got_repo_close(repo);
2601 static const struct got_error *
2602 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2604 const struct got_error *error = NULL;
2606 char *d_file = NULL;
2612 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2613 return got_error_from_errno("asprintf");
2615 f = fopen(d_file, "r");
2617 if (errno != ENOENT && errno != EACCES)
2618 error = got_error_from_errno2("fopen", d_file);
2622 if (fseek(f, 0, SEEK_END) == -1) {
2623 error = got_ferror(f, GOT_ERR_IO);
2628 error = got_ferror(f, GOT_ERR_IO);
2631 if (fseek(f, 0, SEEK_SET) == -1) {
2632 error = got_ferror(f, GOT_ERR_IO);
2636 *url = calloc(len + 1, sizeof(**url));
2638 error = got_error_from_errno("calloc");
2642 n = fread(*url, 1, len, f);
2643 if (n == 0 && ferror(f))
2644 error = got_ferror(f, GOT_ERR_IO);
2646 if (f && fclose(f) == -1 && error == NULL)
2647 error = got_error_from_errno("fclose");
2652 static const struct got_error *
2653 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2654 int limit, int tag_type)
2656 const struct got_error *error = NULL;
2657 struct got_reflist_head refs;
2658 struct got_reflist_entry *re;
2660 char *id_str = NULL, *newline, *href_commits = NULL;
2661 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2662 struct got_tag_object *tag = NULL;
2663 enum kcgi_err kerr = KCGI_OK;
2664 int summary_header_displayed = 0;
2666 SIMPLEQ_INIT(&refs);
2668 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2669 got_ref_cmp_tags, gw_trans->repo);
2673 SIMPLEQ_FOREACH(re, &refs, entry) {
2674 const char *refname;
2676 const char *tag_commit;
2678 struct got_object_id *id;
2679 struct got_commit_object *commit = NULL;
2681 refname = got_ref_get_name(re->ref);
2682 if (strncmp(refname, "refs/tags/", 10) != 0)
2686 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2690 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2692 if (error->code != GOT_ERR_OBJ_TYPE) {
2696 /* "lightweight" tag */
2697 error = got_object_open_as_commit(&commit,
2698 gw_trans->repo, id);
2703 tagger = got_object_commit_get_committer(commit);
2705 got_object_commit_get_committer_time(commit);
2706 error = got_object_id_str(&id_str, id);
2710 tagger = got_object_tag_get_tagger(tag);
2711 tagger_time = got_object_tag_get_tagger_time(tag);
2712 error = got_object_id_str(&id_str,
2713 got_object_tag_get_object_id(tag));
2718 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2719 strlen(id_str)) != 0)
2723 error = got_object_commit_get_logmsg(&tag_commit0,
2727 got_object_commit_close(commit);
2729 tag_commit0 = strdup(got_object_tag_get_message(tag));
2730 if (tag_commit0 == NULL) {
2731 error = got_error_from_errno("strdup");
2736 tag_commit = tag_commit0;
2737 while (*tag_commit == '\n')
2742 newline = strchr(tag_commit, '\n');
2746 if (summary_header_displayed == 0) {
2747 kerr = khtml_attr(gw_trans->gw_html_req,
2748 KELEM_DIV, KATTR_ID,
2749 "summary_tags_title_wrapper", KATTR__MAX);
2750 if (kerr != KCGI_OK)
2752 kerr = khtml_attr(gw_trans->gw_html_req,
2753 KELEM_DIV, KATTR_ID,
2754 "summary_tags_title", KATTR__MAX);
2755 if (kerr != KCGI_OK)
2757 kerr = khtml_puts(gw_trans->gw_html_req,
2759 if (kerr != KCGI_OK)
2761 kerr = khtml_closeelem(gw_trans->gw_html_req,
2763 if (kerr != KCGI_OK)
2765 kerr = khtml_attr(gw_trans->gw_html_req,
2766 KELEM_DIV, KATTR_ID,
2767 "summary_tags_content", KATTR__MAX);
2768 if (kerr != KCGI_OK)
2770 summary_header_displayed = 1;
2773 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2774 KATTR_ID, "tags_wrapper", KATTR__MAX);
2775 if (kerr != KCGI_OK)
2777 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2778 KATTR_ID, "tags_age", KATTR__MAX);
2779 if (kerr != KCGI_OK)
2781 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
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)
2791 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2792 KATTR_ID, "tags", KATTR__MAX);
2793 if (kerr != KCGI_OK)
2795 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2796 if (kerr != KCGI_OK)
2798 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2799 if (kerr != KCGI_OK)
2801 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2802 KATTR_ID, "tags_name", KATTR__MAX);
2803 if (kerr != KCGI_OK)
2805 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2806 gw_trans->repo_name, id_str) == -1) {
2807 error = got_error_from_errno("asprintf");
2810 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2811 KATTR_HREF, href_tag, KATTR__MAX);
2812 if (kerr != KCGI_OK)
2814 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2815 if (kerr != KCGI_OK)
2817 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2818 if (kerr != KCGI_OK)
2821 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2822 KATTR_ID, "navs_wrapper", KATTR__MAX);
2823 if (kerr != KCGI_OK)
2825 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2826 KATTR_ID, "navs", KATTR__MAX);
2827 if (kerr != KCGI_OK)
2830 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2831 KATTR_HREF, href_tag, KATTR__MAX);
2832 if (kerr != KCGI_OK)
2834 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2835 if (kerr != KCGI_OK)
2837 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2838 if (kerr != KCGI_OK)
2841 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2842 if (kerr != KCGI_OK)
2844 if (asprintf(&href_briefs,
2845 "?path=%s&action=briefs&commit=%s",
2846 gw_trans->repo_name, id_str) == -1) {
2847 error = got_error_from_errno("asprintf");
2850 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2851 KATTR_HREF, href_briefs, KATTR__MAX);
2852 if (kerr != KCGI_OK)
2854 kerr = khtml_puts(gw_trans->gw_html_req,
2856 if (kerr != KCGI_OK)
2858 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2859 if (kerr != KCGI_OK)
2862 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2863 if (kerr != KCGI_OK)
2866 if (asprintf(&href_commits,
2867 "?path=%s&action=commits&commit=%s",
2868 gw_trans->repo_name, id_str) == -1) {
2869 error = got_error_from_errno("asprintf");
2872 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2873 KATTR_HREF, href_commits, KATTR__MAX);
2874 if (kerr != KCGI_OK)
2876 kerr = khtml_puts(gw_trans->gw_html_req,
2878 if (kerr != KCGI_OK)
2880 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2881 if (kerr != KCGI_OK)
2884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2885 KATTR_ID, "dotted_line", KATTR__MAX);
2886 if (kerr != KCGI_OK)
2888 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2889 if (kerr != KCGI_OK)
2893 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2894 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2895 if (kerr != KCGI_OK)
2897 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2898 if (kerr != KCGI_OK)
2900 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2901 if (kerr != KCGI_OK)
2903 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2904 KATTR_ID, "tag_info_date", KATTR__MAX);
2905 if (kerr != KCGI_OK)
2907 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2910 kerr = khtml_puts(gw_trans->gw_html_req,
2912 if (kerr != KCGI_OK)
2914 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2915 if (kerr != KCGI_OK)
2918 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2919 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2920 if (kerr != KCGI_OK)
2922 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2923 if (kerr != KCGI_OK)
2925 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2926 if (kerr != KCGI_OK)
2928 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2929 KATTR_ID, "tag_info_date", KATTR__MAX);
2930 if (kerr != KCGI_OK)
2932 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2933 if (kerr != KCGI_OK)
2935 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2936 if (kerr != KCGI_OK)
2939 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2940 KATTR_ID, "tag_info", KATTR__MAX);
2941 if (kerr != KCGI_OK)
2943 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2944 if (kerr != KCGI_OK)
2950 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2951 if (kerr != KCGI_OK)
2954 if (limit && --limit == 0)
2958 got_object_tag_close(tag);
2971 href_commits = NULL;
2975 got_object_tag_close(tag);
2982 got_ref_list_free(&refs);
2983 if (error == NULL && kerr != KCGI_OK)
2984 error = gw_kcgi_error(kerr);
2989 gw_free_header(struct gw_header *header)
2992 free(header->author);
2993 free(header->committer);
2994 free(header->refs_str);
2995 free(header->commit_id);
2996 free(header->parent_id);
2997 free(header->tree_id);
2998 free(header->commit_msg);
3001 static struct gw_header *
3004 struct gw_header *header;
3006 header = malloc(sizeof(*header));
3010 header->path = NULL;
3011 SIMPLEQ_INIT(&header->refs);
3013 header->refs_str = NULL;
3014 header->commit_id = NULL;
3015 header->committer = NULL;
3016 header->author = NULL;
3017 header->parent_id = NULL;
3018 header->tree_id = NULL;
3019 header->commit_msg = NULL;
3024 static const struct got_error *
3025 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3026 int limit, struct got_object_id *id)
3028 const struct got_error *error = NULL;
3029 struct got_commit_graph *graph = NULL;
3030 struct got_commit_object *commit = NULL;
3032 error = got_commit_graph_open(&graph, header->path, 0);
3036 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3042 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3045 if (error->code == GOT_ERR_ITER_COMPLETED)
3052 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3056 error = gw_get_commit(gw_trans, header, commit, id);
3060 struct gw_header *n_header = NULL;
3061 if ((n_header = gw_init_header()) == NULL) {
3062 error = got_error_from_errno("malloc");
3065 error = got_ref_list(&n_header->refs, gw_trans->repo,
3066 NULL, got_ref_cmp_by_name, NULL);
3070 error = gw_get_commit(gw_trans, n_header, commit, id);
3073 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3076 if (error || (limit && --limit == 0))
3081 got_object_commit_close(commit);
3083 got_commit_graph_close(graph);
3087 static const struct got_error *
3088 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3089 struct got_commit_object *commit, struct got_object_id *id)
3091 const struct got_error *error = NULL;
3092 struct got_reflist_entry *re;
3093 struct got_object_id *id2 = NULL;
3094 struct got_object_qid *parent_id;
3095 char *commit_msg = NULL, *commit_msg0;
3098 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3101 struct got_tag_object *tag = NULL;
3104 if (got_ref_is_symbolic(re->ref))
3107 name = got_ref_get_name(re->ref);
3108 if (strncmp(name, "refs/", 5) == 0)
3110 if (strncmp(name, "got/", 4) == 0)
3112 if (strncmp(name, "heads/", 6) == 0)
3114 if (strncmp(name, "remotes/", 8) == 0)
3116 if (strncmp(name, "tags/", 5) == 0) {
3117 error = got_object_open_as_tag(&tag, gw_trans->repo,
3120 if (error->code != GOT_ERR_OBJ_TYPE)
3123 * Ref points at something other
3130 cmp = got_object_id_cmp(tag ?
3131 got_object_tag_get_object_id(tag) : re->id, id);
3133 got_object_tag_close(tag);
3136 s = header->refs_str;
3137 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3138 s ? ", " : "", name) == -1) {
3139 error = got_error_from_errno("asprintf");
3141 header->refs_str = NULL;
3147 error = got_object_id_str(&header->commit_id, id);
3151 error = got_object_id_str(&header->tree_id,
3152 got_object_commit_get_tree_id(commit));
3156 if (gw_trans->action == GW_DIFF) {
3157 parent_id = SIMPLEQ_FIRST(
3158 got_object_commit_get_parent_ids(commit));
3159 if (parent_id != NULL) {
3160 id2 = got_object_id_dup(parent_id->id);
3162 error = got_object_id_str(&header->parent_id, id2);
3167 header->parent_id = strdup("/dev/null");
3168 if (header->parent_id == NULL) {
3169 error = got_error_from_errno("strdup");
3175 header->committer_time =
3176 got_object_commit_get_committer_time(commit);
3179 strdup(got_object_commit_get_author(commit));
3180 if (header->author == NULL) {
3181 error = got_error_from_errno("strdup");
3185 strdup(got_object_commit_get_committer(commit));
3186 if (header->committer == NULL) {
3187 error = got_error_from_errno("strdup");
3190 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3194 commit_msg = commit_msg0;
3195 while (*commit_msg == '\n')
3198 header->commit_msg = strdup(commit_msg);
3199 if (header->commit_msg == NULL)
3200 error = got_error_from_errno("strdup");
3205 static const struct got_error *
3206 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3208 const struct got_error *error = NULL;
3209 char *in_repo_path = NULL;
3210 struct got_object_id *id = NULL;
3212 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3216 if (gw_trans->commit_id == NULL) {
3217 struct got_reference *head_ref;
3218 error = got_ref_open(&head_ref, gw_trans->repo,
3219 gw_trans->headref, 0);
3223 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3224 got_ref_close(head_ref);
3228 struct got_reference *ref;
3229 error = got_ref_open(&ref, gw_trans->repo,
3230 gw_trans->commit_id, 0);
3231 if (error == NULL) {
3233 error = got_ref_resolve(&id, gw_trans->repo, ref);
3237 error = got_object_get_type(&obj_type, gw_trans->repo,
3241 if (obj_type == GOT_OBJ_TYPE_TAG) {
3242 struct got_tag_object *tag;
3243 error = got_object_open_as_tag(&tag,
3244 gw_trans->repo, id);
3247 if (got_object_tag_get_object_type(tag) !=
3248 GOT_OBJ_TYPE_COMMIT) {
3249 got_object_tag_close(tag);
3250 error = got_error(GOT_ERR_OBJ_TYPE);
3254 id = got_object_id_dup(
3255 got_object_tag_get_object_id(tag));
3257 error = got_error_from_errno(
3258 "got_object_id_dup");
3259 got_object_tag_close(tag);
3262 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3263 error = got_error(GOT_ERR_OBJ_TYPE);
3267 error = got_repo_match_object_id_prefix(&id,
3268 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3274 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3275 gw_trans->repo_path, 1);
3280 header->path = strdup(in_repo_path);
3281 if (header->path == NULL) {
3282 error = got_error_from_errno("strdup");
3287 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3288 got_ref_cmp_by_name, NULL);
3292 error = gw_get_commits(gw_trans, header, limit, id);
3294 got_ref_list_free(&header->refs);
3304 char datebuf[11]; /* YYYY-MM-DD + NUL */
3307 struct gw_blame_cb_args {
3308 struct blame_line *lines;
3312 off_t *line_offsets;
3314 struct got_repository *repo;
3315 struct gw_trans *gw_trans;
3318 static const struct got_error *
3319 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3321 const struct got_error *err = NULL;
3322 struct gw_blame_cb_args *a = arg;
3323 struct blame_line *bline;
3325 size_t linesize = 0;
3326 struct got_commit_object *commit = NULL;
3329 time_t committer_time;
3330 enum kcgi_err kerr = KCGI_OK;
3332 if (nlines != a->nlines ||
3333 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3334 return got_error(GOT_ERR_RANGE);
3337 return NULL; /* no change in this commit */
3339 /* Annotate this line. */
3340 bline = &a->lines[lineno - 1];
3341 if (bline->annotated)
3343 err = got_object_id_str(&bline->id_str, id);
3347 err = got_object_open_as_commit(&commit, a->repo, id);
3351 bline->committer = strdup(got_object_commit_get_committer(commit));
3352 if (bline->committer == NULL) {
3353 err = got_error_from_errno("strdup");
3357 committer_time = got_object_commit_get_committer_time(commit);
3358 if (localtime_r(&committer_time, &tm) == NULL)
3359 return got_error_from_errno("localtime_r");
3360 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3361 &tm) >= sizeof(bline->datebuf)) {
3362 err = got_error(GOT_ERR_NO_SPACE);
3365 bline->annotated = 1;
3367 /* Print lines annotated so far. */
3368 bline = &a->lines[a->lineno_cur - 1];
3369 if (!bline->annotated)
3372 offset = a->line_offsets[a->lineno_cur - 1];
3373 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3374 err = got_error_from_errno("fseeko");
3378 while (bline->annotated) {
3379 char *smallerthan, *at, *nl, *committer;
3380 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3383 if (getline(&line, &linesize, a->f) == -1) {
3385 err = got_error_from_errno("getline");
3389 committer = bline->committer;
3390 smallerthan = strchr(committer, '<');
3391 if (smallerthan && smallerthan[1] != '\0')
3392 committer = smallerthan + 1;
3393 at = strchr(committer, '@');
3396 len = strlen(committer);
3398 committer[8] = '\0';
3400 nl = strchr(line, '\n');
3404 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3405 "blame_wrapper", KATTR__MAX);
3406 if (kerr != KCGI_OK)
3408 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3409 "blame_number", KATTR__MAX);
3410 if (kerr != KCGI_OK)
3412 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3413 a->lineno_cur) == -1)
3415 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3416 if (kerr != KCGI_OK)
3418 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3419 if (kerr != KCGI_OK)
3422 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3423 "blame_hash", KATTR__MAX);
3424 if (kerr != KCGI_OK)
3427 if (asprintf(&href_diff,
3428 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3429 a->gw_trans->repo_name, bline->id_str,
3430 a->gw_trans->repo_file, a->gw_trans->repo_folder ?
3431 a->gw_trans->repo_folder : "") == -1) {
3432 err = got_error_from_errno("asprintf");
3435 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3436 err = got_error_from_errno("asprintf");
3439 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3440 KATTR_HREF, href_diff, KATTR__MAX);
3441 if (kerr != KCGI_OK)
3443 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3444 if (kerr != KCGI_OK)
3446 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3447 if (kerr != KCGI_OK)
3450 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3451 "blame_date", KATTR__MAX);
3452 if (kerr != KCGI_OK)
3454 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3455 if (kerr != KCGI_OK)
3457 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3458 if (kerr != KCGI_OK)
3461 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3462 "blame_author", KATTR__MAX);
3463 if (kerr != KCGI_OK)
3465 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3466 if (kerr != KCGI_OK)
3468 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3469 if (kerr != KCGI_OK)
3472 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3473 "blame_code", KATTR__MAX);
3474 if (kerr != KCGI_OK)
3476 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3477 if (kerr != KCGI_OK)
3479 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3480 if (kerr != KCGI_OK)
3483 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3484 if (kerr != KCGI_OK)
3488 bline = &a->lines[a->lineno_cur - 1];
3496 got_object_commit_close(commit);
3498 if (err == NULL && kerr != KCGI_OK)
3499 err = gw_kcgi_error(kerr);
3503 static const struct got_error *
3504 gw_output_file_blame(struct gw_trans *gw_trans)
3506 const struct got_error *error = NULL;
3507 struct got_object_id *obj_id = NULL;
3508 struct got_object_id *commit_id = NULL;
3509 struct got_blob_object *blob = NULL;
3510 char *path = NULL, *in_repo_path = NULL;
3511 struct gw_blame_cb_args bca;
3515 if (asprintf(&path, "%s%s%s",
3516 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3517 gw_trans->repo_folder ? "/" : "",
3518 gw_trans->repo_file) == -1) {
3519 error = got_error_from_errno("asprintf");
3523 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3527 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3528 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3532 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3537 if (obj_id == NULL) {
3538 error = got_error(GOT_ERR_NO_OBJ);
3542 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3546 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3547 error = got_error(GOT_ERR_OBJ_TYPE);
3551 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3555 bca.f = got_opentemp();
3556 if (bca.f == NULL) {
3557 error = got_error_from_errno("got_opentemp");
3560 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3561 &bca.line_offsets, bca.f, blob);
3562 if (error || bca.nlines == 0)
3565 /* Don't include \n at EOF in the blame line count. */
3566 if (bca.line_offsets[bca.nlines - 1] == filesize)
3569 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3570 if (bca.lines == NULL) {
3571 error = got_error_from_errno("calloc");
3575 bca.nlines_prec = 0;
3581 bca.repo = gw_trans->repo;
3582 bca.gw_trans = gw_trans;
3584 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3593 free(bca.line_offsets);
3594 for (i = 0; i < bca.nlines; i++) {
3595 struct blame_line *bline = &bca.lines[i];
3596 free(bline->id_str);
3597 free(bline->committer);
3600 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3601 error = got_error_from_errno("fclose");
3604 got_object_blob_close(blob);
3608 static const struct got_error *
3609 gw_output_blob_buf(struct gw_trans *gw_trans)
3611 const struct got_error *error = NULL;
3612 struct got_object_id *obj_id = NULL;
3613 struct got_object_id *commit_id = NULL;
3614 struct got_blob_object *blob = NULL;
3615 char *path = NULL, *in_repo_path = NULL;
3616 int obj_type, set_mime = 0;
3619 enum kcgi_err kerr = KCGI_OK;
3621 if (asprintf(&path, "%s%s%s",
3622 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3623 gw_trans->repo_folder ? "/" : "",
3624 gw_trans->repo_file) == -1) {
3625 error = got_error_from_errno("asprintf");
3629 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3633 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3634 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3638 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3643 if (obj_id == NULL) {
3644 error = got_error(GOT_ERR_NO_OBJ);
3648 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3652 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3653 error = got_error(GOT_ERR_OBJ_TYPE);
3657 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3661 hdrlen = got_object_blob_get_hdrlen(blob);
3663 error = got_object_blob_read_block(&len, blob);
3666 buf = got_object_blob_get_read_buf(blob);
3669 * Skip blob object header first time around,
3670 * which also contains a zero byte.
3673 if (set_mime == 0) {
3674 if (isbinary(buf, len - hdrlen))
3675 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3677 gw_trans->mime = KMIME_TEXT_PLAIN;
3679 error = gw_display_index(gw_trans);
3683 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3692 got_object_blob_close(blob);
3693 if (error == NULL && kerr != KCGI_OK)
3694 error = gw_kcgi_error(kerr);
3698 static const struct got_error *
3699 gw_output_repo_tree(struct gw_trans *gw_trans)
3701 const struct got_error *error = NULL;
3702 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3703 struct got_tree_object *tree = NULL;
3704 char *path = NULL, *in_repo_path = NULL;
3705 char *id_str = NULL;
3706 char *build_folder = NULL;
3707 char *href_blob = NULL, *href_blame = NULL;
3708 const char *class = NULL;
3709 int nentries, i, class_flip = 0;
3710 enum kcgi_err kerr = KCGI_OK;
3712 if (gw_trans->repo_folder != NULL) {
3713 path = strdup(gw_trans->repo_folder);
3715 error = got_error_from_errno("strdup");
3719 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3720 gw_trans->repo_path, 1);
3724 path = in_repo_path;
3727 if (gw_trans->commit_id == NULL) {
3728 struct got_reference *head_ref;
3729 error = got_ref_open(&head_ref, gw_trans->repo,
3730 gw_trans->headref, 0);
3733 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
3736 got_ref_close(head_ref);
3738 * gw_trans->commit_id was not parsed from the querystring
3739 * we hit this code path from gw_index, where we don't know the
3740 * commit values for the tree link yet, so set
3741 * gw_trans->commit_id here to continue further into the tree
3743 error = got_object_id_str(&gw_trans->commit_id, commit_id);
3748 error = got_repo_match_object_id(&commit_id, NULL,
3749 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
3755 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
3760 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
3764 nentries = got_object_tree_get_nentries(tree);
3765 for (i = 0; i < nentries; i++) {
3766 struct got_tree_entry *te;
3767 const char *modestr = "";
3770 te = got_object_tree_get_entry(tree, i);
3772 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3776 mode = got_tree_entry_get_mode(te);
3777 if (got_object_tree_entry_is_submodule(te))
3779 else if (S_ISLNK(mode))
3781 else if (S_ISDIR(mode))
3783 else if (mode & S_IXUSR)
3786 if (class_flip == 0) {
3787 class = "back_lightgray";
3790 class = "back_white";
3794 if (S_ISDIR(mode)) {
3795 if (asprintf(&build_folder, "%s/%s",
3796 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3797 got_tree_entry_get_name(te)) == -1) {
3798 error = got_error_from_errno("asprintf");
3801 if (asprintf(&href_blob,
3802 "?path=%s&action=%s&commit=%s&folder=%s",
3803 gw_trans->repo_name, gw_get_action_name(gw_trans),
3804 gw_trans->commit_id, build_folder) == -1) {
3805 error = got_error_from_errno("asprintf");
3809 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3810 KATTR_ID, "tree_wrapper", KATTR__MAX);
3811 if (kerr != KCGI_OK)
3813 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3814 KATTR_ID, "tree_line", KATTR_CLASS, class,
3816 if (kerr != KCGI_OK)
3818 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3819 KATTR_HREF, href_blob, KATTR_CLASS,
3820 "diff_directory", KATTR__MAX);
3821 if (kerr != KCGI_OK)
3823 kerr = khtml_puts(gw_trans->gw_html_req,
3824 got_tree_entry_get_name(te));
3825 if (kerr != KCGI_OK)
3827 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3828 if (kerr != KCGI_OK)
3830 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3831 if (kerr != KCGI_OK)
3833 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3834 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3836 if (kerr != KCGI_OK)
3838 kerr = khtml_entity(gw_trans->gw_html_req,
3840 if (kerr != KCGI_OK)
3842 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3843 if (kerr != KCGI_OK)
3846 if (asprintf(&href_blob,
3847 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3848 gw_trans->repo_name, "blob", gw_trans->commit_id,
3849 got_tree_entry_get_name(te),
3850 gw_trans->repo_folder ?
3851 gw_trans->repo_folder : "") == -1) {
3852 error = got_error_from_errno("asprintf");
3855 if (asprintf(&href_blame,
3856 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3857 gw_trans->repo_name, "blame", gw_trans->commit_id,
3858 got_tree_entry_get_name(te),
3859 gw_trans->repo_folder ?
3860 gw_trans->repo_folder : "") == -1) {
3861 error = got_error_from_errno("asprintf");
3865 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3866 KATTR_ID, "tree_wrapper", KATTR__MAX);
3867 if (kerr != KCGI_OK)
3869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3870 KATTR_ID, "tree_line", KATTR_CLASS, class,
3872 if (kerr != KCGI_OK)
3874 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3875 KATTR_HREF, href_blob, KATTR__MAX);
3876 if (kerr != KCGI_OK)
3878 kerr = khtml_puts(gw_trans->gw_html_req,
3879 got_tree_entry_get_name(te));
3880 if (kerr != KCGI_OK)
3882 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3883 if (kerr != KCGI_OK)
3885 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3886 if (kerr != KCGI_OK)
3888 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3889 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3891 if (kerr != KCGI_OK)
3894 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3895 KATTR_HREF, href_blob, KATTR__MAX);
3896 if (kerr != KCGI_OK)
3898 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3899 if (kerr != KCGI_OK)
3901 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3902 if (kerr != KCGI_OK)
3905 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3906 if (kerr != KCGI_OK)
3909 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3910 KATTR_HREF, href_blame, KATTR__MAX);
3911 if (kerr != KCGI_OK)
3913 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3914 if (kerr != KCGI_OK)
3917 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3918 if (kerr != KCGI_OK)
3926 build_folder = NULL;
3930 got_object_tree_close(tree);
3937 if (error == NULL && kerr != KCGI_OK)
3938 error = gw_kcgi_error(kerr);
3942 static const struct got_error *
3943 gw_output_repo_heads(struct gw_trans *gw_trans)
3945 const struct got_error *error = NULL;
3946 struct got_reflist_head refs;
3947 struct got_reflist_entry *re;
3948 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3949 char *href_commits = NULL;
3950 enum kcgi_err kerr = KCGI_OK;
3952 SIMPLEQ_INIT(&refs);
3954 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
3955 got_ref_cmp_by_name, NULL);
3959 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3960 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3961 if (kerr != KCGI_OK)
3963 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3964 KATTR_ID, "summary_heads_title", KATTR__MAX);
3965 if (kerr != KCGI_OK)
3967 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3968 if (kerr != KCGI_OK)
3970 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3971 if (kerr != KCGI_OK)
3973 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3974 KATTR_ID, "summary_heads_content", KATTR__MAX);
3975 if (kerr != KCGI_OK)
3978 SIMPLEQ_FOREACH(re, &refs, entry) {
3979 const char *refname;
3981 if (got_ref_is_symbolic(re->ref))
3984 refname = got_ref_get_name(re->ref);
3985 if (refname == NULL) {
3986 error = got_error_from_errno("got_ref_to_str");
3990 if (strncmp(refname, "refs/heads/", 11) != 0)
3993 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3998 if (strncmp(refname, "refs/heads/", 11) == 0)
4001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4002 KATTR_ID, "heads_wrapper", KATTR__MAX);
4003 if (kerr != KCGI_OK)
4005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4006 KATTR_ID, "heads_age", KATTR__MAX);
4007 if (kerr != KCGI_OK)
4009 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4010 if (kerr != KCGI_OK)
4012 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4013 if (kerr != KCGI_OK)
4015 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4016 KATTR_ID, "heads_space", KATTR__MAX);
4017 if (kerr != KCGI_OK)
4019 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4020 if (kerr != KCGI_OK)
4022 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4023 if (kerr != KCGI_OK)
4025 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4026 KATTR_ID, "head", KATTR__MAX);
4027 if (kerr != KCGI_OK)
4029 if (asprintf(&href_summary,
4030 "?path=%s&action=summary&headref=%s",
4031 gw_trans->repo_name, refname) == -1) {
4032 error = got_error_from_errno("asprintf");
4035 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4036 href_summary, KATTR__MAX);
4037 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4038 if (kerr != KCGI_OK)
4040 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4041 if (kerr != KCGI_OK)
4044 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4045 "navs_wrapper", KATTR__MAX);
4046 if (kerr != KCGI_OK)
4048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4049 "navs", KATTR__MAX);
4050 if (kerr != KCGI_OK)
4053 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4054 href_summary, KATTR__MAX);
4055 if (kerr != KCGI_OK)
4057 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4058 if (kerr != KCGI_OK)
4060 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4061 if (kerr != KCGI_OK)
4064 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4065 if (kerr != KCGI_OK)
4067 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4068 gw_trans->repo_name, refname) == -1) {
4069 error = got_error_from_errno("asprintf");
4072 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4073 href_briefs, KATTR__MAX);
4074 if (kerr != KCGI_OK)
4076 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4077 if (kerr != KCGI_OK)
4079 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4080 if (kerr != KCGI_OK)
4083 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4084 if (kerr != KCGI_OK)
4087 if (asprintf(&href_commits,
4088 "?path=%s&action=commits&headref=%s",
4089 gw_trans->repo_name, refname) == -1) {
4090 error = got_error_from_errno("asprintf");
4093 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4094 href_commits, KATTR__MAX);
4095 if (kerr != KCGI_OK)
4097 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4098 if (kerr != KCGI_OK)
4100 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4101 if (kerr != KCGI_OK)
4104 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4105 "dotted_line", KATTR__MAX);
4106 if (kerr != KCGI_OK)
4108 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4109 if (kerr != KCGI_OK)
4112 href_summary = NULL;
4116 href_commits = NULL;
4119 got_ref_list_free(&refs);
4126 static const struct got_error *
4127 gw_output_site_link(struct gw_trans *gw_trans)
4129 const struct got_error *error = NULL;
4130 char *href_summary = NULL;
4131 enum kcgi_err kerr = KCGI_OK;
4133 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4134 "site_link", KATTR__MAX);
4135 if (kerr != KCGI_OK)
4137 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4139 if (kerr != KCGI_OK)
4141 kerr = khtml_puts(gw_trans->gw_html_req,
4142 gw_trans->gw_conf->got_site_link);
4143 if (kerr != KCGI_OK)
4145 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4146 if (kerr != KCGI_OK)
4149 if (gw_trans->repo_name != NULL) {
4150 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4151 if (kerr != KCGI_OK)
4153 if (asprintf(&href_summary, "?path=%s&action=summary",
4154 gw_trans->repo_name) == -1)
4156 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4157 href_summary, KATTR__MAX);
4158 if (kerr != KCGI_OK)
4160 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4161 if (kerr != KCGI_OK)
4163 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4164 if (kerr != KCGI_OK)
4166 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4167 if (kerr != KCGI_OK)
4169 kerr = khtml_puts(gw_trans->gw_html_req,
4170 gw_get_action_name(gw_trans));
4171 if (kerr != KCGI_OK)
4175 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4176 if (kerr != KCGI_OK)
4183 static const struct got_error *
4184 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4186 const struct got_error *error = NULL;
4188 enum kcgi_err kerr = KCGI_OK;
4190 if (strncmp(buf, "-", 1) == 0)
4191 color = "diff_minus";
4192 else if (strncmp(buf, "+", 1) == 0)
4193 color = "diff_plus";
4194 else if (strncmp(buf, "@@", 2) == 0)
4195 color = "diff_chunk_header";
4196 else if (strncmp(buf, "@@", 2) == 0)
4197 color = "diff_chunk_header";
4198 else if (strncmp(buf, "commit +", 8) == 0)
4199 color = "diff_meta";
4200 else if (strncmp(buf, "commit -", 8) == 0)
4201 color = "diff_meta";
4202 else if (strncmp(buf, "blob +", 6) == 0)
4203 color = "diff_meta";
4204 else if (strncmp(buf, "blob -", 6) == 0)
4205 color = "diff_meta";
4206 else if (strncmp(buf, "file +", 6) == 0)
4207 color = "diff_meta";
4208 else if (strncmp(buf, "file -", 6) == 0)
4209 color = "diff_meta";
4210 else if (strncmp(buf, "from:", 5) == 0)
4211 color = "diff_author";
4212 else if (strncmp(buf, "via:", 4) == 0)
4213 color = "diff_author";
4214 else if (strncmp(buf, "date:", 5) == 0)
4215 color = "diff_date";
4216 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4217 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4218 if (error == NULL && kerr != KCGI_OK)
4219 error = gw_kcgi_error(kerr);
4224 main(int argc, char *argv[])
4226 const struct got_error *error = NULL;
4227 struct gw_trans *gw_trans;
4228 struct gw_dir *dir = NULL, *tdir;
4229 const char *page = "index";
4233 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4236 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4239 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4242 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4245 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4246 if (kerr != KCGI_OK) {
4247 error = gw_kcgi_error(kerr);
4251 if ((gw_trans->gw_conf =
4252 malloc(sizeof(struct gotweb_conf))) == NULL) {
4254 error = got_error_from_errno("malloc");
4258 TAILQ_INIT(&gw_trans->gw_dirs);
4259 TAILQ_INIT(&gw_trans->gw_headers);
4261 gw_trans->action = -1;
4263 gw_trans->repos_total = 0;
4264 gw_trans->repo_path = NULL;
4265 gw_trans->commit_id = NULL;
4266 gw_trans->headref = GOT_REF_HEAD;
4267 gw_trans->mime = KMIME_TEXT_HTML;
4268 gw_trans->gw_tmpl->key = gw_templs;
4269 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4270 gw_trans->gw_tmpl->arg = gw_trans;
4271 gw_trans->gw_tmpl->cb = gw_template;
4272 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4276 error = gw_parse_querystring(gw_trans);
4280 if (gw_trans->action == GW_BLOB)
4281 error = gw_blob(gw_trans);
4283 error = gw_display_index(gw_trans);
4286 free(gw_trans->gw_conf->got_repos_path);
4287 free(gw_trans->gw_conf->got_site_name);
4288 free(gw_trans->gw_conf->got_site_owner);
4289 free(gw_trans->gw_conf->got_site_link);
4290 free(gw_trans->gw_conf->got_logo);
4291 free(gw_trans->gw_conf->got_logo_url);
4292 free(gw_trans->gw_conf);
4293 free(gw_trans->commit_id);
4294 free(gw_trans->repo_path);
4296 got_repo_close(gw_trans->repo);
4298 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4300 free(dir->description);
4309 khttp_free(gw_trans->gw_req);