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);
1636 static const struct got_error *
1637 gw_load_got_paths(struct gw_trans *gw_trans)
1639 const struct got_error *error = NULL;
1641 struct dirent **sd_dent;
1642 struct gw_dir *gw_dir;
1644 unsigned int d_cnt, d_i;
1646 d = opendir(gw_trans->gw_conf->got_repos_path);
1648 error = got_error_from_errno2("opendir",
1649 gw_trans->gw_conf->got_repos_path);
1653 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1656 error = got_error_from_errno2("scandir",
1657 gw_trans->gw_conf->got_repos_path);
1661 for (d_i = 0; d_i < d_cnt; d_i++) {
1662 if (gw_trans->gw_conf->got_max_repos > 0 &&
1663 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1664 break; /* account for parent and self */
1666 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1667 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1670 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1674 error = gw_load_got_path(gw_trans, gw_dir);
1675 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1682 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1683 !got_path_dir_is_empty(gw_dir->path)) {
1684 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1686 gw_trans->repos_total++;
1694 static const struct got_error *
1695 gw_parse_querystring(struct gw_trans *gw_trans)
1697 const struct got_error *error = NULL;
1699 struct gw_query_action *action = NULL;
1702 if (gw_trans->gw_req->fieldnmap[0]) {
1703 error = got_error_from_errno("bad parse");
1705 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1706 /* define gw_trans->repo_path */
1707 gw_trans->repo_name = p->parsed.s;
1709 if (asprintf(&gw_trans->repo_path, "%s/%s",
1710 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1711 return got_error_from_errno("asprintf");
1713 /* get action and set function */
1714 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1715 for (i = 0; i < nitems(gw_query_funcs); i++) {
1716 action = &gw_query_funcs[i];
1717 if (action->func_name == NULL)
1719 if (strcmp(action->func_name,
1720 p->parsed.s) == 0) {
1721 gw_trans->action = i;
1726 if (gw_trans->action == -1) {
1727 gw_trans->action = GW_ERR;
1728 gw_trans->error = got_error_from_errno("bad action");
1732 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1733 if (asprintf(&gw_trans->commit_id, "%s",
1735 return got_error_from_errno("asprintf");
1738 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1739 gw_trans->repo_file = p->parsed.s;
1741 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1742 if (asprintf(&gw_trans->repo_folder, "%s",
1744 return got_error_from_errno("asprintf");
1747 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1748 gw_trans->headref = p->parsed.s;
1750 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1754 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1756 gw_trans->action = GW_INDEX;
1758 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1759 gw_trans->page = p->parsed.i;
1764 static const struct got_error *
1765 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1767 const struct got_error *error;
1769 *gw_dir = malloc(sizeof(**gw_dir));
1770 if (*gw_dir == NULL)
1771 return got_error_from_errno("malloc");
1773 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1774 error = got_error_from_errno("asprintf");
1783 static const struct got_error *
1784 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1788 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1789 if (kerr != KCGI_OK)
1790 return gw_kcgi_error(kerr);
1791 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1793 if (kerr != KCGI_OK)
1794 return gw_kcgi_error(kerr);
1795 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1797 if (kerr != KCGI_OK)
1798 return gw_kcgi_error(kerr);
1799 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1801 if (kerr != KCGI_OK)
1802 return gw_kcgi_error(kerr);
1803 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1804 if (kerr != KCGI_OK)
1805 return gw_kcgi_error(kerr);
1806 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1808 if (kerr != KCGI_OK)
1809 return gw_kcgi_error(kerr);
1811 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1812 kerr = khttp_head(gw_trans->gw_req,
1813 kresps[KRESP_CONTENT_DISPOSITION],
1814 "attachment; filename=%s", gw_trans->repo_file);
1815 if (kerr != KCGI_OK)
1816 return gw_kcgi_error(kerr);
1819 kerr = khttp_body(gw_trans->gw_req);
1820 return gw_kcgi_error(kerr);
1823 static const struct got_error *
1824 gw_display_index(struct gw_trans *gw_trans)
1826 const struct got_error *error;
1829 /* catch early querystring errors */
1830 if (gw_trans->error)
1831 gw_trans->action = GW_ERR;
1833 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1837 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1838 if (kerr != KCGI_OK)
1839 return gw_kcgi_error(kerr);
1841 if (gw_trans->action != GW_BLOB) {
1842 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1843 gw_query_funcs[gw_trans->action].template);
1844 if (kerr != KCGI_OK) {
1845 khtml_close(gw_trans->gw_html_req);
1846 return gw_kcgi_error(kerr);
1850 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1853 static const struct got_error *
1854 gw_error(struct gw_trans *gw_trans)
1858 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
1860 return gw_kcgi_error(kerr);
1864 gw_template(size_t key, void *arg)
1866 const struct got_error *error = NULL;
1868 struct gw_trans *gw_trans = arg;
1869 char *img_src = NULL;
1873 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1874 KATTR_NAME, "viewport",
1875 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1877 if (kerr != KCGI_OK)
1879 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1880 if (kerr != KCGI_OK)
1882 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1883 KATTR_CHARSET, "utf-8",
1885 if (kerr != KCGI_OK)
1887 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1888 if (kerr != KCGI_OK)
1890 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1891 KATTR_NAME, "msapplication-TileColor",
1892 KATTR_CONTENT, "#da532c", KATTR__MAX);
1893 if (kerr != KCGI_OK)
1895 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1896 if (kerr != KCGI_OK)
1898 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1899 KATTR_NAME, "theme-color",
1900 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1901 if (kerr != KCGI_OK)
1903 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1904 if (kerr != KCGI_OK)
1906 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1907 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1908 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1909 if (kerr != KCGI_OK)
1911 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1912 if (kerr != KCGI_OK)
1914 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1915 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1916 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1917 if (kerr != KCGI_OK)
1919 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1920 if (kerr != KCGI_OK)
1922 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1923 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1924 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1925 if (kerr != KCGI_OK)
1927 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1928 if (kerr != KCGI_OK)
1930 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1931 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1933 if (kerr != KCGI_OK)
1935 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1936 if (kerr != KCGI_OK)
1938 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1939 KATTR_REL, "mask-icon", KATTR_HREF,
1940 "/safari-pinned-tab.svg", KATTR__MAX);
1941 if (kerr != KCGI_OK)
1943 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1944 if (kerr != KCGI_OK)
1946 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1947 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1948 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1949 if (kerr != KCGI_OK)
1951 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1952 if (kerr != KCGI_OK)
1956 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1957 KATTR_ID, "got_link", KATTR__MAX);
1958 if (kerr != KCGI_OK)
1960 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1961 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1962 KATTR_TARGET, "_sotd", KATTR__MAX);
1963 if (kerr != KCGI_OK)
1965 if (asprintf(&img_src, "/%s",
1966 gw_trans->gw_conf->got_logo) == -1)
1968 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1969 KATTR_SRC, img_src, KATTR__MAX);
1970 if (kerr != KCGI_OK) {
1974 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1975 if (kerr != KCGI_OK) {
1980 case (TEMPL_SITEPATH):
1981 error = gw_output_site_link(gw_trans);
1986 if (gw_trans->gw_conf->got_site_name != NULL) {
1987 kerr = khtml_puts(gw_trans->gw_html_req,
1988 gw_trans->gw_conf->got_site_name);
1989 if (kerr != KCGI_OK)
1993 case (TEMPL_SEARCH):
1995 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1996 "search", KATTR__MAX);
1997 if (kerr != KCGI_OK)
1999 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2000 KATTR_METHOD, "POST", KATTR__MAX);
2001 if (kerr != KCGI_OK)
2003 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2004 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2005 KATTR_MAXLENGTH, "50", KATTR__MAX);
2006 if (kerr != KCGI_OK)
2008 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2010 if (kerr != KCGI_OK)
2012 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2013 if (kerr != KCGI_OK)
2015 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2016 if (kerr != KCGI_OK)
2019 case(TEMPL_SITEOWNER):
2020 if (gw_trans->gw_conf->got_site_owner != NULL &&
2021 gw_trans->gw_conf->got_show_site_owner) {
2022 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2023 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2024 if (kerr != KCGI_OK)
2026 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2027 KATTR_ID, "site_owner", KATTR__MAX);
2028 if (kerr != KCGI_OK)
2030 kerr = khtml_puts(gw_trans->gw_html_req,
2031 gw_trans->gw_conf->got_site_owner);
2032 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2033 if (kerr != KCGI_OK)
2037 case(TEMPL_CONTENT):
2038 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2040 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2041 KATTR_ID, "tmpl_err", KATTR__MAX);
2042 if (kerr != KCGI_OK)
2044 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
2045 if (kerr != KCGI_OK)
2047 kerr = khttp_puts(gw_trans->gw_req, error->msg);
2048 if (kerr != KCGI_OK)
2050 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2051 if (kerr != KCGI_OK)
2061 static const struct got_error *
2062 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2064 const struct got_error *error = NULL;
2065 enum kcgi_err kerr = KCGI_OK;
2067 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2068 KATTR_ID, "header_commit_title", KATTR__MAX);
2069 if (kerr != KCGI_OK)
2071 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2072 if (kerr != KCGI_OK)
2074 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2075 if (kerr != KCGI_OK)
2077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2078 KATTR_ID, "header_commit", KATTR__MAX);
2079 if (kerr != KCGI_OK)
2081 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2082 if (kerr != KCGI_OK)
2084 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2085 if (kerr != KCGI_OK)
2088 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2089 KATTR_ID, "refs_str", KATTR__MAX);
2090 if (kerr != KCGI_OK)
2092 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2093 if (kerr != KCGI_OK)
2095 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2096 if (kerr != KCGI_OK)
2098 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2099 if (kerr != KCGI_OK)
2101 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2102 if (kerr != KCGI_OK)
2105 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2107 if (error == NULL && kerr != KCGI_OK)
2108 error = gw_kcgi_error(kerr);
2112 static const struct got_error *
2113 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2115 const struct got_error *error = NULL;
2116 enum kcgi_err kerr = KCGI_OK;
2118 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2119 KATTR_ID, "header_diff_title", KATTR__MAX);
2120 if (kerr != KCGI_OK)
2122 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2123 if (kerr != KCGI_OK)
2125 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2126 if (kerr != KCGI_OK)
2128 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2129 KATTR_ID, "header_diff", KATTR__MAX);
2130 if (kerr != KCGI_OK)
2133 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2134 if (kerr != KCGI_OK)
2137 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2138 if (kerr != KCGI_OK)
2140 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2141 if (kerr != KCGI_OK)
2143 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2145 if (error == NULL && kerr != KCGI_OK)
2146 error = gw_kcgi_error(kerr);
2150 static const struct got_error *
2151 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2153 const struct got_error *error = NULL;
2154 enum kcgi_err kerr = KCGI_OK;
2156 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2157 KATTR_ID, "header_age_title", KATTR__MAX);
2158 if (kerr != KCGI_OK)
2160 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2161 if (kerr != KCGI_OK)
2163 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2164 if (kerr != KCGI_OK)
2166 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2167 KATTR_ID, "header_age", KATTR__MAX);
2168 if (kerr != KCGI_OK)
2170 kerr = khtml_puts(gw_trans->gw_html_req, str);
2171 if (kerr != KCGI_OK)
2173 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2175 if (error == NULL && kerr != KCGI_OK)
2176 error = gw_kcgi_error(kerr);
2180 static const struct got_error *
2181 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2183 const struct got_error *error = NULL;
2186 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2187 KATTR_ID, "header_author_title", KATTR__MAX);
2188 if (kerr != KCGI_OK)
2190 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2191 if (kerr != KCGI_OK)
2193 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2194 if (kerr != KCGI_OK)
2196 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2197 KATTR_ID, "header_author", KATTR__MAX);
2198 if (kerr != KCGI_OK)
2200 kerr = khtml_puts(gw_trans->gw_html_req, str);
2201 if (kerr != KCGI_OK)
2203 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2205 if (error == NULL && kerr != KCGI_OK)
2206 error = gw_kcgi_error(kerr);
2210 static const struct got_error *
2211 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2213 const struct got_error *error = NULL;
2216 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2217 KATTR_ID, "header_committer_title", KATTR__MAX);
2218 if (kerr != KCGI_OK)
2220 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2221 if (kerr != KCGI_OK)
2223 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2224 if (kerr != KCGI_OK)
2226 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2227 KATTR_ID, "header_committer", KATTR__MAX);
2228 if (kerr != KCGI_OK)
2230 kerr = khtml_puts(gw_trans->gw_html_req, str);
2231 if (kerr != KCGI_OK)
2233 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2235 if (error == NULL && kerr != KCGI_OK)
2236 error = gw_kcgi_error(kerr);
2240 static const struct got_error *
2241 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2243 const struct got_error *error = NULL;
2246 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2247 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2248 if (kerr != KCGI_OK)
2250 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2251 if (kerr != KCGI_OK)
2253 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2254 if (kerr != KCGI_OK)
2256 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2257 KATTR_ID, "header_commit_msg", KATTR__MAX);
2258 if (kerr != KCGI_OK)
2260 kerr = khttp_puts(gw_trans->gw_req, str);
2261 if (kerr != KCGI_OK)
2263 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2265 if (error == NULL && kerr != KCGI_OK)
2266 error = gw_kcgi_error(kerr);
2270 static const struct got_error *
2271 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2273 const struct got_error *error = NULL;
2276 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2277 KATTR_ID, "header_tree_title", KATTR__MAX);
2278 if (kerr != KCGI_OK)
2280 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2281 if (kerr != KCGI_OK)
2283 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2284 if (kerr != KCGI_OK)
2286 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2287 KATTR_ID, "header_tree", KATTR__MAX);
2288 if (kerr != KCGI_OK)
2290 kerr = khtml_puts(gw_trans->gw_html_req, str);
2291 if (kerr != KCGI_OK)
2293 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2295 if (error == NULL && kerr != KCGI_OK)
2296 error = gw_kcgi_error(kerr);
2300 static const struct got_error *
2301 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2304 const struct got_error *error = NULL;
2306 char *d_file = NULL;
2310 *description = NULL;
2311 if (gw_trans->gw_conf->got_show_repo_description == 0)
2314 if (asprintf(&d_file, "%s/description", dir) == -1)
2315 return got_error_from_errno("asprintf");
2317 f = fopen(d_file, "r");
2319 if (errno == ENOENT || errno == EACCES)
2321 error = got_error_from_errno2("fopen", d_file);
2325 if (fseek(f, 0, SEEK_END) == -1) {
2326 error = got_ferror(f, GOT_ERR_IO);
2331 error = got_ferror(f, GOT_ERR_IO);
2334 if (fseek(f, 0, SEEK_SET) == -1) {
2335 error = got_ferror(f, GOT_ERR_IO);
2338 *description = calloc(len + 1, sizeof(**description));
2339 if (*description == NULL) {
2340 error = got_error_from_errno("calloc");
2344 n = fread(*description, 1, len, f);
2345 if (n == 0 && ferror(f))
2346 error = got_ferror(f, GOT_ERR_IO);
2348 if (f != NULL && fclose(f) == -1 && error == NULL)
2349 error = got_error_from_errno("fclose");
2354 static const struct got_error *
2355 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2359 char *years = "years ago", *months = "months ago";
2360 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2361 char *minutes = "minutes ago", *seconds = "seconds ago";
2362 char *now = "right now";
2370 diff_time = time(NULL) - committer_time;
2371 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2372 if (asprintf(repo_age, "%lld %s",
2373 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2374 return got_error_from_errno("asprintf");
2375 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2376 if (asprintf(repo_age, "%lld %s",
2377 (diff_time / 60 / 60 / 24 / (365 / 12)),
2379 return got_error_from_errno("asprintf");
2380 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2381 if (asprintf(repo_age, "%lld %s",
2382 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2383 return got_error_from_errno("asprintf");
2384 } else if (diff_time > 60 * 60 * 24 * 2) {
2385 if (asprintf(repo_age, "%lld %s",
2386 (diff_time / 60 / 60 / 24), days) == -1)
2387 return got_error_from_errno("asprintf");
2388 } else if (diff_time > 60 * 60 * 2) {
2389 if (asprintf(repo_age, "%lld %s",
2390 (diff_time / 60 / 60), hours) == -1)
2391 return got_error_from_errno("asprintf");
2392 } else if (diff_time > 60 * 2) {
2393 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2395 return got_error_from_errno("asprintf");
2396 } else if (diff_time > 2) {
2397 if (asprintf(repo_age, "%lld %s", diff_time,
2399 return got_error_from_errno("asprintf");
2401 if (asprintf(repo_age, "%s", now) == -1)
2402 return got_error_from_errno("asprintf");
2406 if (gmtime_r(&committer_time, &tm) == NULL)
2407 return got_error_from_errno("gmtime_r");
2409 s = asctime_r(&tm, datebuf);
2411 return got_error_from_errno("asctime_r");
2413 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2414 return got_error_from_errno("asprintf");
2420 static const struct got_error *
2421 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2422 const char *refname, int ref_tm)
2424 const struct got_error *error = NULL;
2425 struct got_repository *repo = NULL;
2426 struct got_commit_object *commit = NULL;
2427 struct got_reflist_head refs;
2428 struct got_reflist_entry *re;
2429 time_t committer_time = 0, cmp_time = 0;
2432 SIMPLEQ_INIT(&refs);
2434 if (gw_trans->gw_conf->got_show_repo_age == 0)
2438 repo = gw_trans->repo;
2440 error = got_repo_open(&repo, dir, NULL);
2445 error = got_ref_list(&refs, repo, "refs/heads",
2446 got_ref_cmp_by_name, NULL);
2451 * Find the youngest branch tip in the repository, or the age of
2452 * the a specific branch tip if a name was provided by the caller.
2454 SIMPLEQ_FOREACH(re, &refs, entry) {
2455 struct got_object_id *id = NULL;
2457 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2460 error = got_ref_resolve(&id, repo, re->ref);
2464 error = got_object_open_as_commit(&commit, repo, id);
2470 got_object_commit_get_committer_time(commit);
2471 got_object_commit_close(commit);
2472 if (cmp_time < committer_time)
2473 cmp_time = committer_time;
2479 if (cmp_time != 0) {
2480 committer_time = cmp_time;
2481 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2484 got_ref_list_free(&refs);
2485 if (gw_trans->repo == NULL)
2486 got_repo_close(repo);
2490 static const struct got_error *
2491 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2493 const struct got_error *error;
2495 struct got_object_id *id1 = NULL, *id2 = NULL;
2496 char *label1 = NULL, *label2 = NULL, *line = NULL;
2498 size_t linesize = 0;
2500 enum kcgi_err kerr = KCGI_OK;
2506 if (header->parent_id != NULL &&
2507 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2508 error = got_repo_match_object_id(&id1, &label1,
2509 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2514 error = got_repo_match_object_id(&id2, &label2,
2515 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2519 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2523 case GOT_OBJ_TYPE_BLOB:
2524 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2527 case GOT_OBJ_TYPE_TREE:
2528 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2531 case GOT_OBJ_TYPE_COMMIT:
2532 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2536 error = got_error(GOT_ERR_OBJ_TYPE);
2541 if (fseek(f, 0, SEEK_SET) == -1) {
2542 error = got_ferror(f, GOT_ERR_IO);
2546 while ((linelen = getline(&line, &linesize, f)) != -1) {
2547 error = gw_colordiff_line(gw_trans, line);
2550 /* XXX: KHTML_PRETTY breaks this */
2551 kerr = khtml_puts(gw_trans->gw_html_req, line);
2552 if (kerr != KCGI_OK)
2554 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2555 if (kerr != KCGI_OK)
2558 if (linelen == -1 && ferror(f))
2559 error = got_error_from_errno("getline");
2561 if (f && fclose(f) == -1 && error == NULL)
2562 error = got_error_from_errno("fclose");
2569 if (error == NULL && kerr != KCGI_OK)
2570 error = gw_kcgi_error(kerr);
2574 static const struct got_error *
2575 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2577 const struct got_error *error = NULL;
2578 struct got_repository *repo;
2579 const char *gitconfig_owner;
2583 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2586 error = got_repo_open(&repo, dir, NULL);
2589 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2590 if (gitconfig_owner) {
2591 *owner = strdup(gitconfig_owner);
2593 error = got_error_from_errno("strdup");
2595 got_repo_close(repo);
2599 static const struct got_error *
2600 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2602 const struct got_error *error = NULL;
2604 char *d_file = NULL;
2610 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2611 return got_error_from_errno("asprintf");
2613 f = fopen(d_file, "r");
2615 if (errno != ENOENT && errno != EACCES)
2616 error = got_error_from_errno2("fopen", d_file);
2620 if (fseek(f, 0, SEEK_END) == -1) {
2621 error = got_ferror(f, GOT_ERR_IO);
2626 error = got_ferror(f, GOT_ERR_IO);
2629 if (fseek(f, 0, SEEK_SET) == -1) {
2630 error = got_ferror(f, GOT_ERR_IO);
2634 *url = calloc(len + 1, sizeof(**url));
2636 error = got_error_from_errno("calloc");
2640 n = fread(*url, 1, len, f);
2641 if (n == 0 && ferror(f))
2642 error = got_ferror(f, GOT_ERR_IO);
2644 if (f && fclose(f) == -1 && error == NULL)
2645 error = got_error_from_errno("fclose");
2650 static const struct got_error *
2651 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2652 int limit, int tag_type)
2654 const struct got_error *error = NULL;
2655 struct got_reflist_head refs;
2656 struct got_reflist_entry *re;
2658 char *id_str = NULL, *newline, *href_commits = NULL;
2659 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2660 struct got_tag_object *tag = NULL;
2661 enum kcgi_err kerr = KCGI_OK;
2662 int summary_header_displayed = 0;
2664 SIMPLEQ_INIT(&refs);
2666 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2667 got_ref_cmp_tags, gw_trans->repo);
2671 SIMPLEQ_FOREACH(re, &refs, entry) {
2672 const char *refname;
2674 const char *tag_commit;
2676 struct got_object_id *id;
2677 struct got_commit_object *commit = NULL;
2679 refname = got_ref_get_name(re->ref);
2680 if (strncmp(refname, "refs/tags/", 10) != 0)
2684 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2688 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2690 if (error->code != GOT_ERR_OBJ_TYPE) {
2694 /* "lightweight" tag */
2695 error = got_object_open_as_commit(&commit,
2696 gw_trans->repo, id);
2701 tagger = got_object_commit_get_committer(commit);
2703 got_object_commit_get_committer_time(commit);
2704 error = got_object_id_str(&id_str, id);
2708 tagger = got_object_tag_get_tagger(tag);
2709 tagger_time = got_object_tag_get_tagger_time(tag);
2710 error = got_object_id_str(&id_str,
2711 got_object_tag_get_object_id(tag));
2716 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2717 strlen(id_str)) != 0)
2721 error = got_object_commit_get_logmsg(&tag_commit0,
2725 got_object_commit_close(commit);
2727 tag_commit0 = strdup(got_object_tag_get_message(tag));
2728 if (tag_commit0 == NULL) {
2729 error = got_error_from_errno("strdup");
2734 tag_commit = tag_commit0;
2735 while (*tag_commit == '\n')
2740 newline = strchr(tag_commit, '\n');
2744 if (summary_header_displayed == 0) {
2745 kerr = khtml_attr(gw_trans->gw_html_req,
2746 KELEM_DIV, KATTR_ID,
2747 "summary_tags_title_wrapper", KATTR__MAX);
2748 if (kerr != KCGI_OK)
2750 kerr = khtml_attr(gw_trans->gw_html_req,
2751 KELEM_DIV, KATTR_ID,
2752 "summary_tags_title", KATTR__MAX);
2753 if (kerr != KCGI_OK)
2755 kerr = khtml_puts(gw_trans->gw_html_req,
2757 if (kerr != KCGI_OK)
2759 kerr = khtml_closeelem(gw_trans->gw_html_req,
2761 if (kerr != KCGI_OK)
2763 kerr = khtml_attr(gw_trans->gw_html_req,
2764 KELEM_DIV, KATTR_ID,
2765 "summary_tags_content", KATTR__MAX);
2766 if (kerr != KCGI_OK)
2768 summary_header_displayed = 1;
2771 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2772 KATTR_ID, "tags_wrapper", KATTR__MAX);
2773 if (kerr != KCGI_OK)
2775 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2776 KATTR_ID, "tags_age", KATTR__MAX);
2777 if (kerr != KCGI_OK)
2779 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2782 kerr = khtml_puts(gw_trans->gw_html_req,
2784 if (kerr != KCGI_OK)
2786 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2787 if (kerr != KCGI_OK)
2789 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2790 KATTR_ID, "tags", KATTR__MAX);
2791 if (kerr != KCGI_OK)
2793 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2794 if (kerr != KCGI_OK)
2796 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2797 if (kerr != KCGI_OK)
2799 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2800 KATTR_ID, "tags_name", KATTR__MAX);
2801 if (kerr != KCGI_OK)
2803 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2804 gw_trans->repo_name, id_str) == -1) {
2805 error = got_error_from_errno("asprintf");
2808 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2809 KATTR_HREF, href_tag, KATTR__MAX);
2810 if (kerr != KCGI_OK)
2812 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2813 if (kerr != KCGI_OK)
2815 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2816 if (kerr != KCGI_OK)
2819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2820 KATTR_ID, "navs_wrapper", KATTR__MAX);
2821 if (kerr != KCGI_OK)
2823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2824 KATTR_ID, "navs", KATTR__MAX);
2825 if (kerr != KCGI_OK)
2828 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2829 KATTR_HREF, href_tag, KATTR__MAX);
2830 if (kerr != KCGI_OK)
2832 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2833 if (kerr != KCGI_OK)
2835 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2836 if (kerr != KCGI_OK)
2839 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2840 if (kerr != KCGI_OK)
2842 if (asprintf(&href_briefs,
2843 "?path=%s&action=briefs&commit=%s",
2844 gw_trans->repo_name, id_str) == -1) {
2845 error = got_error_from_errno("asprintf");
2848 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2849 KATTR_HREF, href_briefs, KATTR__MAX);
2850 if (kerr != KCGI_OK)
2852 kerr = khtml_puts(gw_trans->gw_html_req,
2854 if (kerr != KCGI_OK)
2856 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2857 if (kerr != KCGI_OK)
2860 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2861 if (kerr != KCGI_OK)
2864 if (asprintf(&href_commits,
2865 "?path=%s&action=commits&commit=%s",
2866 gw_trans->repo_name, id_str) == -1) {
2867 error = got_error_from_errno("asprintf");
2870 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2871 KATTR_HREF, href_commits, KATTR__MAX);
2872 if (kerr != KCGI_OK)
2874 kerr = khtml_puts(gw_trans->gw_html_req,
2876 if (kerr != KCGI_OK)
2878 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2879 if (kerr != KCGI_OK)
2882 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2883 KATTR_ID, "dotted_line", KATTR__MAX);
2884 if (kerr != KCGI_OK)
2886 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2887 if (kerr != KCGI_OK)
2891 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2892 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2893 if (kerr != KCGI_OK)
2895 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2896 if (kerr != KCGI_OK)
2898 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2899 if (kerr != KCGI_OK)
2901 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2902 KATTR_ID, "tag_info_date", KATTR__MAX);
2903 if (kerr != KCGI_OK)
2905 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2908 kerr = khtml_puts(gw_trans->gw_html_req,
2910 if (kerr != KCGI_OK)
2912 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2913 if (kerr != KCGI_OK)
2916 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2917 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2918 if (kerr != KCGI_OK)
2920 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2921 if (kerr != KCGI_OK)
2923 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2924 if (kerr != KCGI_OK)
2926 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2927 KATTR_ID, "tag_info_date", KATTR__MAX);
2928 if (kerr != KCGI_OK)
2930 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2931 if (kerr != KCGI_OK)
2933 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2934 if (kerr != KCGI_OK)
2937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2938 KATTR_ID, "tag_info", KATTR__MAX);
2939 if (kerr != KCGI_OK)
2941 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2942 if (kerr != KCGI_OK)
2948 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2949 if (kerr != KCGI_OK)
2952 if (limit && --limit == 0)
2956 got_object_tag_close(tag);
2969 href_commits = NULL;
2973 got_object_tag_close(tag);
2980 got_ref_list_free(&refs);
2981 if (error == NULL && kerr != KCGI_OK)
2982 error = gw_kcgi_error(kerr);
2987 gw_free_header(struct gw_header *header)
2990 free(header->author);
2991 free(header->committer);
2992 free(header->refs_str);
2993 free(header->commit_id);
2994 free(header->parent_id);
2995 free(header->tree_id);
2996 free(header->commit_msg);
2999 static struct gw_header *
3002 struct gw_header *header;
3004 header = malloc(sizeof(*header));
3008 header->path = NULL;
3009 SIMPLEQ_INIT(&header->refs);
3011 header->refs_str = NULL;
3012 header->commit_id = NULL;
3013 header->committer = NULL;
3014 header->author = NULL;
3015 header->parent_id = NULL;
3016 header->tree_id = NULL;
3017 header->commit_msg = NULL;
3022 static const struct got_error *
3023 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3024 int limit, struct got_object_id *id)
3026 const struct got_error *error = NULL;
3027 struct got_commit_graph *graph = NULL;
3028 struct got_commit_object *commit = NULL;
3030 error = got_commit_graph_open(&graph, header->path, 0);
3034 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3040 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3043 if (error->code == GOT_ERR_ITER_COMPLETED)
3050 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3054 error = gw_get_commit(gw_trans, header, commit, id);
3058 struct gw_header *n_header = NULL;
3059 if ((n_header = gw_init_header()) == NULL) {
3060 error = got_error_from_errno("malloc");
3063 error = got_ref_list(&n_header->refs, gw_trans->repo,
3064 NULL, got_ref_cmp_by_name, NULL);
3068 error = gw_get_commit(gw_trans, n_header, commit, id);
3071 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3074 if (error || (limit && --limit == 0))
3079 got_object_commit_close(commit);
3081 got_commit_graph_close(graph);
3085 static const struct got_error *
3086 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3087 struct got_commit_object *commit, struct got_object_id *id)
3089 const struct got_error *error = NULL;
3090 struct got_reflist_entry *re;
3091 struct got_object_id *id2 = NULL;
3092 struct got_object_qid *parent_id;
3093 char *commit_msg = NULL, *commit_msg0;
3096 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3099 struct got_tag_object *tag = NULL;
3102 if (got_ref_is_symbolic(re->ref))
3105 name = got_ref_get_name(re->ref);
3106 if (strncmp(name, "refs/", 5) == 0)
3108 if (strncmp(name, "got/", 4) == 0)
3110 if (strncmp(name, "heads/", 6) == 0)
3112 if (strncmp(name, "remotes/", 8) == 0)
3114 if (strncmp(name, "tags/", 5) == 0) {
3115 error = got_object_open_as_tag(&tag, gw_trans->repo,
3118 if (error->code != GOT_ERR_OBJ_TYPE)
3121 * Ref points at something other
3128 cmp = got_object_id_cmp(tag ?
3129 got_object_tag_get_object_id(tag) : re->id, id);
3131 got_object_tag_close(tag);
3134 s = header->refs_str;
3135 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3136 s ? ", " : "", name) == -1) {
3137 error = got_error_from_errno("asprintf");
3139 header->refs_str = NULL;
3145 error = got_object_id_str(&header->commit_id, id);
3149 error = got_object_id_str(&header->tree_id,
3150 got_object_commit_get_tree_id(commit));
3154 if (gw_trans->action == GW_DIFF) {
3155 parent_id = SIMPLEQ_FIRST(
3156 got_object_commit_get_parent_ids(commit));
3157 if (parent_id != NULL) {
3158 id2 = got_object_id_dup(parent_id->id);
3160 error = got_object_id_str(&header->parent_id, id2);
3165 header->parent_id = strdup("/dev/null");
3166 if (header->parent_id == NULL) {
3167 error = got_error_from_errno("strdup");
3173 header->committer_time =
3174 got_object_commit_get_committer_time(commit);
3177 strdup(got_object_commit_get_author(commit));
3178 if (header->author == NULL) {
3179 error = got_error_from_errno("strdup");
3183 strdup(got_object_commit_get_committer(commit));
3184 if (header->committer == NULL) {
3185 error = got_error_from_errno("strdup");
3188 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3192 commit_msg = commit_msg0;
3193 while (*commit_msg == '\n')
3196 header->commit_msg = strdup(commit_msg);
3197 if (header->commit_msg == NULL)
3198 error = got_error_from_errno("strdup");
3203 static const struct got_error *
3204 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3206 const struct got_error *error = NULL;
3207 char *in_repo_path = NULL;
3208 struct got_object_id *id = NULL;
3210 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3214 if (gw_trans->commit_id == NULL) {
3215 struct got_reference *head_ref;
3216 error = got_ref_open(&head_ref, gw_trans->repo,
3217 gw_trans->headref, 0);
3221 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3222 got_ref_close(head_ref);
3226 struct got_reference *ref;
3227 error = got_ref_open(&ref, gw_trans->repo,
3228 gw_trans->commit_id, 0);
3229 if (error == NULL) {
3231 error = got_ref_resolve(&id, gw_trans->repo, ref);
3235 error = got_object_get_type(&obj_type, gw_trans->repo,
3239 if (obj_type == GOT_OBJ_TYPE_TAG) {
3240 struct got_tag_object *tag;
3241 error = got_object_open_as_tag(&tag,
3242 gw_trans->repo, id);
3245 if (got_object_tag_get_object_type(tag) !=
3246 GOT_OBJ_TYPE_COMMIT) {
3247 got_object_tag_close(tag);
3248 error = got_error(GOT_ERR_OBJ_TYPE);
3252 id = got_object_id_dup(
3253 got_object_tag_get_object_id(tag));
3255 error = got_error_from_errno(
3256 "got_object_id_dup");
3257 got_object_tag_close(tag);
3260 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3261 error = got_error(GOT_ERR_OBJ_TYPE);
3265 error = got_repo_match_object_id_prefix(&id,
3266 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3272 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3273 gw_trans->repo_path, 1);
3278 header->path = strdup(in_repo_path);
3279 if (header->path == NULL) {
3280 error = got_error_from_errno("strdup");
3285 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3286 got_ref_cmp_by_name, NULL);
3290 error = gw_get_commits(gw_trans, header, limit, id);
3292 got_ref_list_free(&header->refs);
3302 char datebuf[11]; /* YYYY-MM-DD + NUL */
3305 struct gw_blame_cb_args {
3306 struct blame_line *lines;
3310 off_t *line_offsets;
3312 struct got_repository *repo;
3313 struct gw_trans *gw_trans;
3316 static const struct got_error *
3317 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3319 const struct got_error *err = NULL;
3320 struct gw_blame_cb_args *a = arg;
3321 struct blame_line *bline;
3323 size_t linesize = 0;
3324 struct got_commit_object *commit = NULL;
3327 time_t committer_time;
3328 enum kcgi_err kerr = KCGI_OK;
3330 if (nlines != a->nlines ||
3331 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3332 return got_error(GOT_ERR_RANGE);
3335 return NULL; /* no change in this commit */
3337 /* Annotate this line. */
3338 bline = &a->lines[lineno - 1];
3339 if (bline->annotated)
3341 err = got_object_id_str(&bline->id_str, id);
3345 err = got_object_open_as_commit(&commit, a->repo, id);
3349 bline->committer = strdup(got_object_commit_get_committer(commit));
3350 if (bline->committer == NULL) {
3351 err = got_error_from_errno("strdup");
3355 committer_time = got_object_commit_get_committer_time(commit);
3356 if (localtime_r(&committer_time, &tm) == NULL)
3357 return got_error_from_errno("localtime_r");
3358 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3359 &tm) >= sizeof(bline->datebuf)) {
3360 err = got_error(GOT_ERR_NO_SPACE);
3363 bline->annotated = 1;
3365 /* Print lines annotated so far. */
3366 bline = &a->lines[a->lineno_cur - 1];
3367 if (!bline->annotated)
3370 offset = a->line_offsets[a->lineno_cur - 1];
3371 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3372 err = got_error_from_errno("fseeko");
3376 while (bline->annotated) {
3377 char *smallerthan, *at, *nl, *committer;
3378 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3381 if (getline(&line, &linesize, a->f) == -1) {
3383 err = got_error_from_errno("getline");
3387 committer = bline->committer;
3388 smallerthan = strchr(committer, '<');
3389 if (smallerthan && smallerthan[1] != '\0')
3390 committer = smallerthan + 1;
3391 at = strchr(committer, '@');
3394 len = strlen(committer);
3396 committer[8] = '\0';
3398 nl = strchr(line, '\n');
3402 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3403 "blame_wrapper", KATTR__MAX);
3404 if (kerr != KCGI_OK)
3406 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3407 "blame_number", KATTR__MAX);
3408 if (kerr != KCGI_OK)
3410 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3411 a->lineno_cur) == -1)
3413 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3414 if (kerr != KCGI_OK)
3416 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3417 if (kerr != KCGI_OK)
3420 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3421 "blame_hash", KATTR__MAX);
3422 if (kerr != KCGI_OK)
3425 if (asprintf(&href_diff,
3426 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3427 a->gw_trans->repo_name, bline->id_str,
3428 a->gw_trans->repo_file, a->gw_trans->repo_folder ?
3429 a->gw_trans->repo_folder : "") == -1) {
3430 err = got_error_from_errno("asprintf");
3433 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3434 err = got_error_from_errno("asprintf");
3437 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3438 KATTR_HREF, href_diff, KATTR__MAX);
3439 if (kerr != KCGI_OK)
3441 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3442 if (kerr != KCGI_OK)
3444 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3445 if (kerr != KCGI_OK)
3448 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3449 "blame_date", KATTR__MAX);
3450 if (kerr != KCGI_OK)
3452 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3453 if (kerr != KCGI_OK)
3455 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3456 if (kerr != KCGI_OK)
3459 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3460 "blame_author", KATTR__MAX);
3461 if (kerr != KCGI_OK)
3463 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3464 if (kerr != KCGI_OK)
3466 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3467 if (kerr != KCGI_OK)
3470 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3471 "blame_code", KATTR__MAX);
3472 if (kerr != KCGI_OK)
3474 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3475 if (kerr != KCGI_OK)
3477 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3478 if (kerr != KCGI_OK)
3481 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3482 if (kerr != KCGI_OK)
3486 bline = &a->lines[a->lineno_cur - 1];
3494 got_object_commit_close(commit);
3496 if (err == NULL && kerr != KCGI_OK)
3497 err = gw_kcgi_error(kerr);
3501 static const struct got_error *
3502 gw_output_file_blame(struct gw_trans *gw_trans)
3504 const struct got_error *error = NULL;
3505 struct got_object_id *obj_id = NULL;
3506 struct got_object_id *commit_id = NULL;
3507 struct got_blob_object *blob = NULL;
3508 char *path = NULL, *in_repo_path = NULL;
3509 struct gw_blame_cb_args bca;
3513 if (asprintf(&path, "%s%s%s",
3514 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3515 gw_trans->repo_folder ? "/" : "",
3516 gw_trans->repo_file) == -1) {
3517 error = got_error_from_errno("asprintf");
3521 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3525 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3526 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3530 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3535 if (obj_id == NULL) {
3536 error = got_error(GOT_ERR_NO_OBJ);
3540 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3544 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3545 error = got_error(GOT_ERR_OBJ_TYPE);
3549 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3553 bca.f = got_opentemp();
3554 if (bca.f == NULL) {
3555 error = got_error_from_errno("got_opentemp");
3558 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3559 &bca.line_offsets, bca.f, blob);
3560 if (error || bca.nlines == 0)
3563 /* Don't include \n at EOF in the blame line count. */
3564 if (bca.line_offsets[bca.nlines - 1] == filesize)
3567 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3568 if (bca.lines == NULL) {
3569 error = got_error_from_errno("calloc");
3573 bca.nlines_prec = 0;
3579 bca.repo = gw_trans->repo;
3580 bca.gw_trans = gw_trans;
3582 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3591 free(bca.line_offsets);
3592 for (i = 0; i < bca.nlines; i++) {
3593 struct blame_line *bline = &bca.lines[i];
3594 free(bline->id_str);
3595 free(bline->committer);
3598 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3599 error = got_error_from_errno("fclose");
3602 got_object_blob_close(blob);
3606 static const struct got_error *
3607 gw_output_blob_buf(struct gw_trans *gw_trans)
3609 const struct got_error *error = NULL;
3610 struct got_object_id *obj_id = NULL;
3611 struct got_object_id *commit_id = NULL;
3612 struct got_blob_object *blob = NULL;
3613 char *path = NULL, *in_repo_path = NULL;
3614 int obj_type, set_mime = 0;
3617 enum kcgi_err kerr = KCGI_OK;
3619 if (asprintf(&path, "%s%s%s",
3620 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3621 gw_trans->repo_folder ? "/" : "",
3622 gw_trans->repo_file) == -1) {
3623 error = got_error_from_errno("asprintf");
3627 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3631 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3632 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3636 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3641 if (obj_id == NULL) {
3642 error = got_error(GOT_ERR_NO_OBJ);
3646 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3650 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3651 error = got_error(GOT_ERR_OBJ_TYPE);
3655 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3659 hdrlen = got_object_blob_get_hdrlen(blob);
3661 error = got_object_blob_read_block(&len, blob);
3664 buf = got_object_blob_get_read_buf(blob);
3667 * Skip blob object header first time around,
3668 * which also contains a zero byte.
3671 if (set_mime == 0) {
3672 if (isbinary(buf, len - hdrlen))
3673 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3675 gw_trans->mime = KMIME_TEXT_PLAIN;
3677 error = gw_display_index(gw_trans);
3681 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3690 got_object_blob_close(blob);
3691 if (error == NULL && kerr != KCGI_OK)
3692 error = gw_kcgi_error(kerr);
3696 static const struct got_error *
3697 gw_output_repo_tree(struct gw_trans *gw_trans)
3699 const struct got_error *error = NULL;
3700 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3701 struct got_tree_object *tree = NULL;
3702 char *path = NULL, *in_repo_path = NULL;
3703 char *id_str = NULL;
3704 char *build_folder = NULL;
3705 char *href_blob = NULL, *href_blame = NULL;
3706 const char *class = NULL;
3707 int nentries, i, class_flip = 0;
3708 enum kcgi_err kerr = KCGI_OK;
3710 if (gw_trans->repo_folder != NULL) {
3711 path = strdup(gw_trans->repo_folder);
3713 error = got_error_from_errno("strdup");
3717 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3718 gw_trans->repo_path, 1);
3722 path = in_repo_path;
3725 if (gw_trans->commit_id == NULL) {
3726 struct got_reference *head_ref;
3727 error = got_ref_open(&head_ref, gw_trans->repo,
3728 gw_trans->headref, 0);
3731 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
3734 got_ref_close(head_ref);
3736 * gw_trans->commit_id was not parsed from the querystring
3737 * we hit this code path from gw_index, where we don't know the
3738 * commit values for the tree link yet, so set
3739 * gw_trans->commit_id here to continue further into the tree
3741 error = got_object_id_str(&gw_trans->commit_id, commit_id);
3746 error = got_repo_match_object_id(&commit_id, NULL,
3747 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
3753 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
3758 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
3762 nentries = got_object_tree_get_nentries(tree);
3763 for (i = 0; i < nentries; i++) {
3764 struct got_tree_entry *te;
3765 const char *modestr = "";
3768 te = got_object_tree_get_entry(tree, i);
3770 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3774 mode = got_tree_entry_get_mode(te);
3775 if (got_object_tree_entry_is_submodule(te))
3777 else if (S_ISLNK(mode))
3779 else if (S_ISDIR(mode))
3781 else if (mode & S_IXUSR)
3784 if (class_flip == 0) {
3785 class = "back_lightgray";
3788 class = "back_white";
3792 if (S_ISDIR(mode)) {
3793 if (asprintf(&build_folder, "%s/%s",
3794 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3795 got_tree_entry_get_name(te)) == -1) {
3796 error = got_error_from_errno("asprintf");
3799 if (asprintf(&href_blob,
3800 "?path=%s&action=%s&commit=%s&folder=%s",
3801 gw_trans->repo_name, gw_get_action_name(gw_trans),
3802 gw_trans->commit_id, build_folder) == -1) {
3803 error = got_error_from_errno("asprintf");
3807 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3808 KATTR_ID, "tree_wrapper", KATTR__MAX);
3809 if (kerr != KCGI_OK)
3811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3812 KATTR_ID, "tree_line", KATTR_CLASS, class,
3814 if (kerr != KCGI_OK)
3816 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3817 KATTR_HREF, href_blob, KATTR_CLASS,
3818 "diff_directory", KATTR__MAX);
3819 if (kerr != KCGI_OK)
3821 kerr = khtml_puts(gw_trans->gw_html_req,
3822 got_tree_entry_get_name(te));
3823 if (kerr != KCGI_OK)
3825 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3826 if (kerr != KCGI_OK)
3828 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3829 if (kerr != KCGI_OK)
3831 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3832 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3834 if (kerr != KCGI_OK)
3836 kerr = khtml_entity(gw_trans->gw_html_req,
3838 if (kerr != KCGI_OK)
3840 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3841 if (kerr != KCGI_OK)
3844 if (asprintf(&href_blob,
3845 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3846 gw_trans->repo_name, "blob", gw_trans->commit_id,
3847 got_tree_entry_get_name(te),
3848 gw_trans->repo_folder ?
3849 gw_trans->repo_folder : "") == -1) {
3850 error = got_error_from_errno("asprintf");
3853 if (asprintf(&href_blame,
3854 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3855 gw_trans->repo_name, "blame", gw_trans->commit_id,
3856 got_tree_entry_get_name(te),
3857 gw_trans->repo_folder ?
3858 gw_trans->repo_folder : "") == -1) {
3859 error = got_error_from_errno("asprintf");
3863 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3864 KATTR_ID, "tree_wrapper", KATTR__MAX);
3865 if (kerr != KCGI_OK)
3867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3868 KATTR_ID, "tree_line", KATTR_CLASS, class,
3870 if (kerr != KCGI_OK)
3872 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3873 KATTR_HREF, href_blob, KATTR__MAX);
3874 if (kerr != KCGI_OK)
3876 kerr = khtml_puts(gw_trans->gw_html_req,
3877 got_tree_entry_get_name(te));
3878 if (kerr != KCGI_OK)
3880 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3881 if (kerr != KCGI_OK)
3883 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3884 if (kerr != KCGI_OK)
3886 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3887 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3889 if (kerr != KCGI_OK)
3892 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3893 KATTR_HREF, href_blob, KATTR__MAX);
3894 if (kerr != KCGI_OK)
3896 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3897 if (kerr != KCGI_OK)
3899 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3900 if (kerr != KCGI_OK)
3903 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3904 if (kerr != KCGI_OK)
3907 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3908 KATTR_HREF, href_blame, KATTR__MAX);
3909 if (kerr != KCGI_OK)
3911 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3912 if (kerr != KCGI_OK)
3915 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3916 if (kerr != KCGI_OK)
3924 build_folder = NULL;
3928 got_object_tree_close(tree);
3935 if (error == NULL && kerr != KCGI_OK)
3936 error = gw_kcgi_error(kerr);
3940 static const struct got_error *
3941 gw_output_repo_heads(struct gw_trans *gw_trans)
3943 const struct got_error *error = NULL;
3944 struct got_reflist_head refs;
3945 struct got_reflist_entry *re;
3946 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3947 char *href_commits = NULL;
3948 enum kcgi_err kerr = KCGI_OK;
3950 SIMPLEQ_INIT(&refs);
3952 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
3953 got_ref_cmp_by_name, NULL);
3957 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3958 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3959 if (kerr != KCGI_OK)
3961 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3962 KATTR_ID, "summary_heads_title", KATTR__MAX);
3963 if (kerr != KCGI_OK)
3965 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3966 if (kerr != KCGI_OK)
3968 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3969 if (kerr != KCGI_OK)
3971 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3972 KATTR_ID, "summary_heads_content", KATTR__MAX);
3973 if (kerr != KCGI_OK)
3976 SIMPLEQ_FOREACH(re, &refs, entry) {
3977 const char *refname;
3979 if (got_ref_is_symbolic(re->ref))
3982 refname = got_ref_get_name(re->ref);
3983 if (refname == NULL) {
3984 error = got_error_from_errno("got_ref_to_str");
3988 if (strncmp(refname, "refs/heads/", 11) != 0)
3991 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3996 if (strncmp(refname, "refs/heads/", 11) == 0)
3999 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4000 KATTR_ID, "heads_wrapper", KATTR__MAX);
4001 if (kerr != KCGI_OK)
4003 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4004 KATTR_ID, "heads_age", KATTR__MAX);
4005 if (kerr != KCGI_OK)
4007 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4008 if (kerr != KCGI_OK)
4010 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4011 if (kerr != KCGI_OK)
4013 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4014 KATTR_ID, "heads_space", KATTR__MAX);
4015 if (kerr != KCGI_OK)
4017 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4018 if (kerr != KCGI_OK)
4020 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4021 if (kerr != KCGI_OK)
4023 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4024 KATTR_ID, "head", KATTR__MAX);
4025 if (kerr != KCGI_OK)
4027 if (asprintf(&href_summary,
4028 "?path=%s&action=summary&headref=%s",
4029 gw_trans->repo_name, refname) == -1) {
4030 error = got_error_from_errno("asprintf");
4033 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4034 href_summary, KATTR__MAX);
4035 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4036 if (kerr != KCGI_OK)
4038 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4039 if (kerr != KCGI_OK)
4042 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4043 "navs_wrapper", KATTR__MAX);
4044 if (kerr != KCGI_OK)
4046 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4047 "navs", KATTR__MAX);
4048 if (kerr != KCGI_OK)
4051 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4052 href_summary, KATTR__MAX);
4053 if (kerr != KCGI_OK)
4055 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4056 if (kerr != KCGI_OK)
4058 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4059 if (kerr != KCGI_OK)
4062 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4063 if (kerr != KCGI_OK)
4065 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4066 gw_trans->repo_name, refname) == -1) {
4067 error = got_error_from_errno("asprintf");
4070 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4071 href_briefs, KATTR__MAX);
4072 if (kerr != KCGI_OK)
4074 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4075 if (kerr != KCGI_OK)
4077 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4078 if (kerr != KCGI_OK)
4081 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4082 if (kerr != KCGI_OK)
4085 if (asprintf(&href_commits,
4086 "?path=%s&action=commits&headref=%s",
4087 gw_trans->repo_name, refname) == -1) {
4088 error = got_error_from_errno("asprintf");
4091 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4092 href_commits, KATTR__MAX);
4093 if (kerr != KCGI_OK)
4095 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4096 if (kerr != KCGI_OK)
4098 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4099 if (kerr != KCGI_OK)
4102 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4103 "dotted_line", KATTR__MAX);
4104 if (kerr != KCGI_OK)
4106 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4107 if (kerr != KCGI_OK)
4110 href_summary = NULL;
4114 href_commits = NULL;
4117 got_ref_list_free(&refs);
4124 static const struct got_error *
4125 gw_output_site_link(struct gw_trans *gw_trans)
4127 const struct got_error *error = NULL;
4128 char *href_summary = NULL;
4129 enum kcgi_err kerr = KCGI_OK;
4131 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4132 "site_link", KATTR__MAX);
4133 if (kerr != KCGI_OK)
4135 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4137 if (kerr != KCGI_OK)
4139 kerr = khtml_puts(gw_trans->gw_html_req,
4140 gw_trans->gw_conf->got_site_link);
4141 if (kerr != KCGI_OK)
4143 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4144 if (kerr != KCGI_OK)
4147 if (gw_trans->repo_name != NULL) {
4148 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4149 if (kerr != KCGI_OK)
4151 if (asprintf(&href_summary, "?path=%s&action=summary",
4152 gw_trans->repo_name) == -1)
4154 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4155 href_summary, KATTR__MAX);
4156 if (kerr != KCGI_OK)
4158 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4159 if (kerr != KCGI_OK)
4161 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4162 if (kerr != KCGI_OK)
4164 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4165 if (kerr != KCGI_OK)
4167 kerr = khtml_puts(gw_trans->gw_html_req,
4168 gw_get_action_name(gw_trans));
4169 if (kerr != KCGI_OK)
4173 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4174 if (kerr != KCGI_OK)
4181 static const struct got_error *
4182 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4184 const struct got_error *error = NULL;
4186 enum kcgi_err kerr = KCGI_OK;
4188 if (strncmp(buf, "-", 1) == 0)
4189 color = "diff_minus";
4190 else if (strncmp(buf, "+", 1) == 0)
4191 color = "diff_plus";
4192 else if (strncmp(buf, "@@", 2) == 0)
4193 color = "diff_chunk_header";
4194 else if (strncmp(buf, "@@", 2) == 0)
4195 color = "diff_chunk_header";
4196 else if (strncmp(buf, "commit +", 8) == 0)
4197 color = "diff_meta";
4198 else if (strncmp(buf, "commit -", 8) == 0)
4199 color = "diff_meta";
4200 else if (strncmp(buf, "blob +", 6) == 0)
4201 color = "diff_meta";
4202 else if (strncmp(buf, "blob -", 6) == 0)
4203 color = "diff_meta";
4204 else if (strncmp(buf, "file +", 6) == 0)
4205 color = "diff_meta";
4206 else if (strncmp(buf, "file -", 6) == 0)
4207 color = "diff_meta";
4208 else if (strncmp(buf, "from:", 5) == 0)
4209 color = "diff_author";
4210 else if (strncmp(buf, "via:", 4) == 0)
4211 color = "diff_author";
4212 else if (strncmp(buf, "date:", 5) == 0)
4213 color = "diff_date";
4214 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4215 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4216 if (error == NULL && kerr != KCGI_OK)
4217 error = gw_kcgi_error(kerr);
4222 main(int argc, char *argv[])
4224 const struct got_error *error = NULL;
4225 struct gw_trans *gw_trans;
4226 struct gw_dir *dir = NULL, *tdir;
4227 const char *page = "index";
4231 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4234 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4237 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4240 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4243 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4244 if (kerr != KCGI_OK) {
4245 error = gw_kcgi_error(kerr);
4249 if ((gw_trans->gw_conf =
4250 malloc(sizeof(struct gotweb_conf))) == NULL) {
4252 error = got_error_from_errno("malloc");
4256 TAILQ_INIT(&gw_trans->gw_dirs);
4257 TAILQ_INIT(&gw_trans->gw_headers);
4259 gw_trans->action = -1;
4261 gw_trans->repos_total = 0;
4262 gw_trans->repo_path = NULL;
4263 gw_trans->commit_id = NULL;
4264 gw_trans->headref = GOT_REF_HEAD;
4265 gw_trans->mime = KMIME_TEXT_HTML;
4266 gw_trans->gw_tmpl->key = gw_templs;
4267 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4268 gw_trans->gw_tmpl->arg = gw_trans;
4269 gw_trans->gw_tmpl->cb = gw_template;
4270 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4274 error = gw_parse_querystring(gw_trans);
4278 if (gw_trans->action == GW_BLOB)
4279 error = gw_blob(gw_trans);
4281 error = gw_display_index(gw_trans);
4284 free(gw_trans->gw_conf->got_repos_path);
4285 free(gw_trans->gw_conf->got_site_name);
4286 free(gw_trans->gw_conf->got_site_owner);
4287 free(gw_trans->gw_conf->got_site_link);
4288 free(gw_trans->gw_conf->got_logo);
4289 free(gw_trans->gw_conf->got_logo_url);
4290 free(gw_trans->gw_conf);
4291 free(gw_trans->commit_id);
4292 free(gw_trans->repo_path);
4294 got_repo_close(gw_trans->repo);
4296 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4298 free(dir->description);
4307 khttp_free(gw_trans->gw_req);