2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
20 #include <sys/types.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
51 #include "gotweb_ui.h"
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 TAILQ_HEAD(headers, gw_header) gw_headers;
59 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
60 struct gw_dir *gw_dir;
61 struct gotweb_conf *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
74 unsigned int repos_total;
79 TAILQ_ENTRY(gw_header) entry;
80 struct got_repository *repo;
81 struct got_reflist_head refs;
82 struct got_commit_object *commit;
83 struct got_object_id *id;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
93 time_t committer_time;
97 TAILQ_ENTRY(gw_dir) entry;
138 static const char *const gw_templs[TEMPL__MAX] = {
148 static const struct kvalid gw_keys[KEY__ZMAX] = {
149 { kvalid_stringne, "action" },
150 { kvalid_stringne, "commit" },
151 { kvalid_stringne, "file" },
152 { kvalid_stringne, "folder" },
153 { kvalid_stringne, "headref" },
154 { kvalid_int, "page" },
155 { kvalid_stringne, "path" },
158 static struct gw_dir *gw_init_gw_dir(char *);
159 static struct gw_header *gw_init_header(void);
161 static char *gw_get_repo_description(struct gw_trans *,
163 static char *gw_get_repo_owner(struct gw_trans *,
165 static char *gw_get_time_str(time_t, int);
166 static char *gw_get_repo_age(struct gw_trans *,
167 char *, char *, int);
168 static char *gw_get_file_blame(struct gw_trans *);
169 static char *gw_get_repo_tree(struct gw_trans *);
170 static char *gw_get_diff(struct gw_trans *,
172 static char *gw_get_repo_tags(struct gw_trans *, int, int);
173 static char *gw_get_repo_heads(struct gw_trans *);
174 static char *gw_get_clone_url(struct gw_trans *, char *);
175 static char *gw_get_got_link(struct gw_trans *);
176 static char *gw_get_site_link(struct gw_trans *);
177 static char *gw_html_escape(const char *);
178 static char *gw_colordiff_line(char *);
180 static char *gw_gen_commit_header(char *, char*);
181 static char *gw_gen_diff_header(char *, char*);
182 static char *gw_gen_author_header(char *);
183 static char *gw_gen_committer_header(char *);
184 static char *gw_gen_age_header(char *);
185 static char *gw_gen_commit_msg_header(char *);
186 static char *gw_gen_tree_header(char *);
188 static void gw_free_headers(struct gw_header *);
189 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
191 static const struct got_error* gw_display_index(struct gw_trans *,
192 const struct got_error *);
194 static int gw_template(size_t, void *);
196 static const struct got_error* gw_get_header(struct gw_trans *,
197 struct gw_header *, int);
198 static const struct got_error* gw_get_commits(struct gw_trans *,
199 struct gw_header *, int);
200 static const struct got_error* gw_get_commit(struct gw_trans *,
202 static const struct got_error* gw_apply_unveil(const char *, const char *);
203 static const struct got_error* gw_blame_cb(void *, int, int,
204 struct got_object_id *);
205 static const struct got_error* gw_load_got_paths(struct gw_trans *);
206 static const struct got_error* gw_load_got_path(struct gw_trans *,
208 static const struct got_error* gw_parse_querystring(struct gw_trans *);
210 static const struct got_error* gw_blame(struct gw_trans *);
211 static const struct got_error* gw_diff(struct gw_trans *);
212 static const struct got_error* gw_index(struct gw_trans *);
213 static const struct got_error* gw_commits(struct gw_trans *);
214 static const struct got_error* gw_briefs(struct gw_trans *);
215 static const struct got_error* gw_summary(struct gw_trans *);
216 static const struct got_error* gw_tree(struct gw_trans *);
218 struct gw_query_action {
219 unsigned int func_id;
220 const char *func_name;
221 const struct got_error *(*func_main)(struct gw_trans *);
225 enum gw_query_actions {
236 static struct gw_query_action gw_query_funcs[] = {
237 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
238 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
239 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
240 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
241 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
242 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
243 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
244 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
247 static const struct got_error *
248 gw_kcgi_error(enum kcgi_err kerr)
253 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
254 return got_error(GOT_ERR_CANCELLED);
256 if (kerr == KCGI_ENOMEM)
257 return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
259 if (kerr == KCGI_ENFILE)
260 return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
262 if (kerr == KCGI_EAGAIN)
263 return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
265 if (kerr == KCGI_FORM)
266 return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
268 return got_error_from_errno(kcgi_strerror(kerr));
271 static const struct got_error *
272 gw_apply_unveil(const char *repo_path, const char *repo_file)
274 const struct got_error *err;
276 if (repo_path && repo_file) {
278 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
279 return got_error_from_errno("asprintf unveil");
280 if (unveil(full_path, "r") != 0)
281 return got_error_from_errno2("unveil", full_path);
284 if (repo_path && unveil(repo_path, "r") != 0)
285 return got_error_from_errno2("unveil", repo_path);
287 if (unveil("/tmp", "rwc") != 0)
288 return got_error_from_errno2("unveil", "/tmp");
290 err = got_privsep_unveil_exec_helpers();
294 if (unveil(NULL, NULL) != 0)
295 return got_error_from_errno("unveil");
300 static const struct got_error *
301 gw_blame(struct gw_trans *gw_trans)
303 const struct got_error *error = NULL;
304 struct gw_header *header = NULL;
305 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
308 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
310 return got_error_from_errno("pledge");
312 if ((header = gw_init_header()) == NULL)
313 return got_error_from_errno("malloc");
315 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
319 error = gw_get_header(gw_trans, header, 1);
323 blame_html = gw_get_file_blame(gw_trans);
325 if (blame_html == NULL) {
326 blame_html = strdup("");
327 if (blame_html == NULL)
328 return got_error_from_errno("strdup");
331 if ((asprintf(&blame_html_disp, blame_header,
332 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
333 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
334 blame_html)) == -1) {
335 error = got_error_from_errno("asprintf");
339 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
340 error = got_error_from_errno("asprintf");
344 kerr = khttp_puts(gw_trans->gw_req, blame);
346 error = gw_kcgi_error(kerr);
348 got_ref_list_free(&header->refs);
349 gw_free_headers(header);
350 free(blame_html_disp);
356 static const struct got_error *
357 gw_diff(struct gw_trans *gw_trans)
359 const struct got_error *error = NULL;
360 struct gw_header *header = NULL;
361 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
364 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
366 return got_error_from_errno("pledge");
368 if ((header = gw_init_header()) == NULL)
369 return got_error_from_errno("malloc");
371 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
375 error = gw_get_header(gw_trans, header, 1);
379 diff_html = gw_get_diff(gw_trans, header);
381 if (diff_html == NULL)
382 diff_html = strdup("");
384 if ((asprintf(&diff_html_disp, diff_header,
385 gw_gen_diff_header(header->parent_id, header->commit_id),
386 gw_gen_commit_header(header->commit_id, header->refs_str),
387 gw_gen_tree_header(header->tree_id),
388 gw_gen_author_header(header->author),
389 gw_gen_committer_header(header->committer),
390 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
391 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
393 return got_error_from_errno("asprintf");
395 if ((asprintf(&diff, diff_wrapper, diff_html_disp)) == -1)
396 return got_error_from_errno("asprintf");
398 kerr = khttp_puts(gw_trans->gw_req, diff);
400 error = gw_kcgi_error(kerr);
401 got_ref_list_free(&header->refs);
402 gw_free_headers(header);
403 free(diff_html_disp);
409 static const struct got_error *
410 gw_index(struct gw_trans *gw_trans)
412 const struct got_error *error = NULL;
413 struct gw_dir *gw_dir = NULL;
414 char *html, *navs, *next, *prev;
415 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
418 if (pledge("stdio rpath proc exec sendfd unveil",
420 error = got_error_from_errno("pledge");
424 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
428 error = gw_load_got_paths(gw_trans);
432 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
434 return gw_kcgi_error(kerr);
436 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
437 if (asprintf(&html, index_projects_empty,
438 gw_trans->gw_conf->got_repos_path) == -1)
439 return got_error_from_errno("asprintf");
440 kerr = khttp_puts(gw_trans->gw_req, html);
442 error = gw_kcgi_error(kerr);
447 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
450 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
451 if (gw_trans->page > 0 && (gw_trans->page *
452 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
461 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
462 gw_dir->name, gw_dir->name)) == -1)
463 return got_error_from_errno("asprintf");
465 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
466 gw_dir->description, gw_dir->owner, gw_dir->age,
468 return got_error_from_errno("asprintf");
470 kerr = khttp_puts(gw_trans->gw_req, html);
474 return gw_kcgi_error(kerr);
476 if (gw_trans->gw_conf->got_max_repos_display == 0)
479 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
480 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
482 return gw_kcgi_error(kerr);
483 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
484 (gw_trans->page > 0) &&
485 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
486 prev_disp == gw_trans->repos_total)) {
487 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
489 return gw_kcgi_error(kerr);
492 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
493 (gw_trans->page > 0) &&
494 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
495 prev_disp == gw_trans->repos_total)) {
496 if ((asprintf(&prev, nav_prev,
497 gw_trans->page - 1)) == -1)
498 return got_error_from_errno("asprintf");
499 kerr = khttp_puts(gw_trans->gw_req, prev);
502 return gw_kcgi_error(kerr);
505 kerr = khttp_puts(gw_trans->gw_req, div_end);
507 return gw_kcgi_error(kerr);
509 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
510 next_disp == gw_trans->gw_conf->got_max_repos_display &&
511 dir_c != (gw_trans->page + 1) *
512 gw_trans->gw_conf->got_max_repos_display) {
513 if ((asprintf(&next, nav_next,
514 gw_trans->page + 1)) == -1)
515 return got_error_from_errno("calloc");
516 kerr = khttp_puts(gw_trans->gw_req, next);
519 return gw_kcgi_error(kerr);
520 kerr = khttp_puts(gw_trans->gw_req, div_end);
522 error = gw_kcgi_error(kerr);
527 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
528 (gw_trans->page > 0) &&
529 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
530 prev_disp == gw_trans->repos_total)) {
531 kerr = khttp_puts(gw_trans->gw_req, div_end);
533 return gw_kcgi_error(kerr);
541 static const struct got_error *
542 gw_commits(struct gw_trans *gw_trans)
544 const struct got_error *error = NULL;
545 char *commits_html, *commits_navs_html;
546 struct gw_header *header = NULL, *n_header = NULL;
549 if ((header = gw_init_header()) == NULL)
550 return got_error_from_errno("malloc");
552 if (pledge("stdio rpath proc exec sendfd unveil",
554 error = got_error_from_errno("pledge");
558 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
562 error = gw_get_header(gw_trans, header,
563 gw_trans->gw_conf->got_max_commits_display);
565 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
567 return gw_kcgi_error(kerr);
568 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
569 if ((asprintf(&commits_navs_html, commits_navs,
570 gw_trans->repo_name, n_header->commit_id,
571 gw_trans->repo_name, n_header->commit_id,
572 gw_trans->repo_name, n_header->commit_id)) == -1)
573 return got_error_from_errno("asprintf");
575 if ((asprintf(&commits_html, commits_line,
576 gw_gen_commit_header(n_header->commit_id,
578 gw_gen_author_header(n_header->author),
579 gw_gen_committer_header(n_header->committer),
580 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
581 TM_LONG)), gw_html_escape(n_header->commit_msg),
582 commits_navs_html)) == -1)
583 return got_error_from_errno("asprintf");
584 kerr = khttp_puts(gw_trans->gw_req, commits_html);
586 return gw_kcgi_error(kerr);
588 kerr = khttp_puts(gw_trans->gw_req, div_end);
590 error = gw_kcgi_error(kerr);
593 got_ref_list_free(&header->refs);
594 gw_free_headers(header);
595 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
596 gw_free_headers(n_header);
600 static const struct got_error *
601 gw_briefs(struct gw_trans *gw_trans)
603 const struct got_error *error = NULL;
604 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
605 struct gw_header *header = NULL, *n_header = NULL;
608 if ((header = gw_init_header()) == NULL)
609 return got_error_from_errno("malloc");
611 if (pledge("stdio rpath proc exec sendfd unveil",
613 error = got_error_from_errno("pledge");
617 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
621 if (gw_trans->action == GW_SUMMARY)
622 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
624 error = gw_get_header(gw_trans, header,
625 gw_trans->gw_conf->got_max_commits_display);
630 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
632 return gw_kcgi_error(kerr);
634 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
635 if ((asprintf(&briefs_navs_html, briefs_navs,
636 gw_trans->repo_name, n_header->commit_id,
637 gw_trans->repo_name, n_header->commit_id,
638 gw_trans->repo_name, n_header->commit_id)) == -1)
639 return got_error_from_errno("asprintf");
640 newline = strchr(n_header->commit_msg, '\n');
643 if ((asprintf(&briefs_html, briefs_line,
644 gw_get_time_str(n_header->committer_time, TM_DIFF),
645 n_header->author, gw_html_escape(n_header->commit_msg),
646 briefs_navs_html)) == -1)
647 return got_error_from_errno("asprintf");
648 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
650 return gw_kcgi_error(kerr);
652 kerr = khttp_puts(gw_trans->gw_req, div_end);
654 error = gw_kcgi_error(kerr);
657 got_ref_list_free(&header->refs);
658 gw_free_headers(header);
659 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
660 gw_free_headers(n_header);
664 static const struct got_error *
665 gw_summary(struct gw_trans *gw_trans)
667 const struct got_error *error = NULL;
668 char *description_html, *repo_owner_html, *repo_age_html,
669 *cloneurl_html, *tags, *heads, *tags_html,
673 if (pledge("stdio rpath proc exec sendfd unveil",
675 error = got_error_from_errno("pledge");
679 /* unveil is applied with gw_briefs below */
681 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
683 return gw_kcgi_error(kerr);
685 if (gw_trans->gw_conf->got_show_repo_description) {
686 if (gw_trans->gw_dir->description != NULL &&
687 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
688 if ((asprintf(&description_html, description,
689 gw_trans->gw_dir->description)) == -1)
690 return got_error_from_errno("asprintf");
692 kerr = khttp_puts(gw_trans->gw_req, description_html);
693 free(description_html);
695 return gw_kcgi_error(kerr);
699 if (gw_trans->gw_conf->got_show_repo_owner) {
700 if (gw_trans->gw_dir->owner != NULL &&
701 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
702 if ((asprintf(&repo_owner_html, repo_owner,
703 gw_trans->gw_dir->owner)) == -1)
704 return got_error_from_errno("asprintf");
706 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
707 free(repo_owner_html);
709 return gw_kcgi_error(kerr);
713 if (gw_trans->gw_conf->got_show_repo_age) {
714 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
715 "refs/heads", TM_LONG);
716 if (age != NULL && (strcmp(age, "") != 0)) {
717 if ((asprintf(&repo_age_html, last_change, age)) == -1)
718 return got_error_from_errno("asprintf");
720 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
724 return gw_kcgi_error(kerr);
728 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
729 if (gw_trans->gw_dir->url != NULL &&
730 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
731 if ((asprintf(&cloneurl_html, cloneurl,
732 gw_trans->gw_dir->url)) == -1)
733 return got_error_from_errno("asprintf");
735 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
738 return gw_kcgi_error(kerr);
741 kerr = khttp_puts(gw_trans->gw_req, div_end);
743 return gw_kcgi_error(kerr);
745 error = gw_briefs(gw_trans);
749 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
750 heads = gw_get_repo_heads(gw_trans);
752 if (tags != NULL && strcmp(tags, "") != 0) {
753 if ((asprintf(&tags_html, summary_tags,
755 return got_error_from_errno("asprintf");
756 kerr = khttp_puts(gw_trans->gw_req, tags_html);
760 return gw_kcgi_error(kerr);
763 if (heads != NULL && strcmp(heads, "") != 0) {
764 if ((asprintf(&heads_html, summary_heads,
766 return got_error_from_errno("asprintf");
767 kerr = khttp_puts(gw_trans->gw_req, heads_html);
771 return gw_kcgi_error(kerr);
776 static const struct got_error *
777 gw_tree(struct gw_trans *gw_trans)
779 const struct got_error *error = NULL;
780 struct gw_header *header = NULL;
781 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
784 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
785 return got_error_from_errno("pledge");
787 if ((header = gw_init_header()) == NULL)
788 return got_error_from_errno("malloc");
790 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
794 error = gw_get_header(gw_trans, header, 1);
798 tree_html = gw_get_repo_tree(gw_trans);
800 if (tree_html == NULL)
801 tree_html = strdup("");
803 if ((asprintf(&tree_html_disp, tree_header,
804 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
805 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
807 return got_error_from_errno("asprintf");
809 if ((asprintf(&tree, tree_wrapper, tree_html_disp)) == -1)
810 return got_error_from_errno("asprintf");
812 kerr = khttp_puts(gw_trans->gw_req, tree);
814 error = gw_kcgi_error(kerr);
816 got_ref_list_free(&header->refs);
817 gw_free_headers(header);
818 free(tree_html_disp);
824 static const struct got_error *
825 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
827 const struct got_error *error = NULL;
832 if ((asprintf(&dir_test, "%s/%s/%s",
833 gw_trans->gw_conf->got_repos_path, gw_dir->name,
834 GOTWEB_GIT_DIR)) == -1)
835 return got_error_from_errno("asprintf");
837 dt = opendir(dir_test);
841 gw_dir->path = strdup(dir_test);
846 if ((asprintf(&dir_test, "%s/%s/%s",
847 gw_trans->gw_conf->got_repos_path, gw_dir->name,
848 GOTWEB_GOT_DIR)) == -1)
849 return got_error_from_errno("asprintf");
851 dt = opendir(dir_test);
856 error = got_error(GOT_ERR_NOT_GIT_REPO);
860 if ((asprintf(&dir_test, "%s/%s",
861 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
862 return got_error_from_errno("asprintf");
864 gw_dir->path = strdup(dir_test);
867 gw_dir->description = gw_get_repo_description(gw_trans,
869 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
870 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
872 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
881 static const struct got_error *
882 gw_load_got_paths(struct gw_trans *gw_trans)
884 const struct got_error *error = NULL;
886 struct dirent **sd_dent;
887 struct gw_dir *gw_dir;
889 unsigned int d_cnt, d_i;
891 d = opendir(gw_trans->gw_conf->got_repos_path);
893 error = got_error_from_errno2("opendir",
894 gw_trans->gw_conf->got_repos_path);
898 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
901 error = got_error_from_errno2("scandir",
902 gw_trans->gw_conf->got_repos_path);
906 for (d_i = 0; d_i < d_cnt; d_i++) {
907 if (gw_trans->gw_conf->got_max_repos > 0 &&
908 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
909 break; /* account for parent and self */
911 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
912 strcmp(sd_dent[d_i]->d_name, "..") == 0)
915 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
916 return got_error_from_errno("gw_dir malloc");
918 error = gw_load_got_path(gw_trans, gw_dir);
919 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
924 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
925 !got_path_dir_is_empty(gw_dir->path)) {
926 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
928 gw_trans->repos_total++;
936 static const struct got_error *
937 gw_parse_querystring(struct gw_trans *gw_trans)
939 const struct got_error *error = NULL;
941 struct gw_query_action *action = NULL;
944 if (gw_trans->gw_req->fieldnmap[0]) {
945 error = got_error_from_errno("bad parse");
947 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
948 /* define gw_trans->repo_path */
949 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
950 return got_error_from_errno("asprintf");
952 if ((asprintf(&gw_trans->repo_path, "%s/%s",
953 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
954 return got_error_from_errno("asprintf");
956 /* get action and set function */
957 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
958 for (i = 0; i < nitems(gw_query_funcs); i++) {
959 action = &gw_query_funcs[i];
960 if (action->func_name == NULL)
963 if (strcmp(action->func_name,
965 gw_trans->action = i;
966 if ((asprintf(&gw_trans->action_name,
967 "%s", action->func_name)) == -1)
969 got_error_from_errno(
978 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
979 if ((asprintf(&gw_trans->commit, "%s",
981 return got_error_from_errno("asprintf");
983 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
984 if ((asprintf(&gw_trans->repo_file, "%s",
986 return got_error_from_errno("asprintf");
988 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
989 if ((asprintf(&gw_trans->repo_folder, "%s",
991 return got_error_from_errno("asprintf");
993 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
994 if ((asprintf(&gw_trans->headref, "%s",
996 return got_error_from_errno("asprintf");
998 if (action == NULL) {
999 error = got_error_from_errno("invalid action");
1002 if ((gw_trans->gw_dir =
1003 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1004 return got_error_from_errno("gw_dir malloc");
1006 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1010 gw_trans->action = GW_INDEX;
1012 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1013 gw_trans->page = p->parsed.i;
1015 /* if (gw_trans->action == GW_RAW) */
1016 /* gw_trans->mime = KMIME_TEXT_PLAIN; */
1021 static struct gw_dir *
1022 gw_init_gw_dir(char *dir)
1024 struct gw_dir *gw_dir;
1026 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1029 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1035 static const struct got_error *
1036 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1040 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1041 if (kerr != KCGI_OK)
1042 return gw_kcgi_error(kerr);
1043 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1045 if (kerr != KCGI_OK)
1046 return gw_kcgi_error(kerr);
1047 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1049 if (kerr != KCGI_OK)
1050 return gw_kcgi_error(kerr);
1051 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1052 if (kerr != KCGI_OK)
1053 return gw_kcgi_error(kerr);
1054 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1055 if (kerr != KCGI_OK)
1056 return gw_kcgi_error(kerr);
1057 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1058 if (kerr != KCGI_OK)
1059 return gw_kcgi_error(kerr);
1061 kerr = khttp_body(gw_trans->gw_req);
1062 return gw_kcgi_error(kerr);
1065 static const struct got_error *
1066 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
1070 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1071 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1074 kerr = khttp_puts(gw_trans->gw_req, err->msg);
1076 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1077 gw_query_funcs[gw_trans->action].template);
1078 if (kerr != KCGI_OK)
1079 return gw_kcgi_error(kerr);
1081 kerr = khtml_close(gw_trans->gw_html_req);
1082 return gw_kcgi_error(kerr);
1086 gw_template(size_t key, void *arg)
1088 const struct got_error *error = NULL;
1089 struct gw_trans *gw_trans = arg;
1090 char *gw_got_link, *gw_site_link;
1091 char *site_owner_name, *site_owner_name_h;
1095 khttp_puts(gw_trans->gw_req, head);
1098 gw_got_link = gw_get_got_link(gw_trans);
1099 if (gw_got_link != NULL)
1100 khttp_puts(gw_trans->gw_req, gw_got_link);
1104 case (TEMPL_SITEPATH):
1105 gw_site_link = gw_get_site_link(gw_trans);
1106 if (gw_site_link != NULL)
1107 khttp_puts(gw_trans->gw_req, gw_site_link);
1112 if (gw_trans->gw_conf->got_site_name != NULL)
1113 khtml_puts(gw_trans->gw_html_req,
1114 gw_trans->gw_conf->got_site_name);
1117 case (TEMPL_SEARCH):
1118 khttp_puts(gw_trans->gw_req, search);
1120 case(TEMPL_SITEOWNER):
1121 if (gw_trans->gw_conf->got_site_owner != NULL &&
1122 gw_trans->gw_conf->got_show_site_owner) {
1124 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1125 if ((asprintf(&site_owner_name_h, site_owner,
1130 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1131 free(site_owner_name);
1132 free(site_owner_name_h);
1135 case(TEMPL_CONTENT):
1136 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1138 khttp_puts(gw_trans->gw_req, error->msg);
1147 gw_gen_commit_header(char *str1, char *str2)
1149 char *return_html = NULL, *ref_str = NULL;
1151 if (strcmp(str2, "") != 0) {
1152 if ((asprintf(&ref_str, "(%s)", str2)) == -1) {
1153 return_html = strdup("");
1157 ref_str = strdup("");
1160 if ((asprintf(&return_html, header_commit_html, str1, ref_str)) == -1)
1161 return_html = strdup("");
1168 gw_gen_diff_header(char *str1, char *str2)
1170 char *return_html = NULL;
1172 if ((asprintf(&return_html, header_diff_html, str1, str2)) == -1)
1173 return_html = strdup("");
1179 gw_gen_author_header(char *str)
1181 char *return_html = NULL;
1183 if ((asprintf(&return_html, header_author_html, str)) == -1)
1184 return_html = strdup("");
1190 gw_gen_committer_header(char *str)
1192 char *return_html = NULL;
1194 if ((asprintf(&return_html, header_committer_html, str)) == -1)
1195 return_html = strdup("");
1201 gw_gen_age_header(char *str)
1203 char *return_html = NULL;
1205 if ((asprintf(&return_html, header_age_html, str)) == -1)
1206 return_html = strdup("");
1212 gw_gen_commit_msg_header(char *str)
1214 char *return_html = NULL;
1216 if ((asprintf(&return_html, header_commit_msg_html, str)) == -1)
1217 return_html = strdup("");
1223 gw_gen_tree_header(char *str)
1225 char *return_html = NULL;
1227 if ((asprintf(&return_html, header_tree_html, str)) == -1)
1228 return_html = strdup("");
1234 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1237 char *description = NULL, *d_file = NULL;
1240 if (gw_trans->gw_conf->got_show_repo_description == false)
1243 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1246 if ((f = fopen(d_file, "r")) == NULL)
1249 fseek(f, 0, SEEK_END);
1251 fseek(f, 0, SEEK_SET);
1252 if ((description = calloc(len, sizeof(char *))) == NULL)
1255 fread(description, 1, len, f);
1260 if ((asprintf(&description, "%s", "")) == -1)
1267 gw_get_time_str(time_t committer_time, int ref_tm)
1271 char *years = "years ago", *months = "months ago";
1272 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1273 char *minutes = "minutes ago", *seconds = "seconds ago";
1274 char *now = "right now";
1280 diff_time = time(NULL) - committer_time;
1281 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1282 if ((asprintf(&repo_age, "%lld %s",
1283 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1285 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1286 if ((asprintf(&repo_age, "%lld %s",
1287 (diff_time / 60 / 60 / 24 / (365 / 12)),
1290 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1291 if ((asprintf(&repo_age, "%lld %s",
1292 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1294 } else if (diff_time > 60 * 60 * 24 * 2) {
1295 if ((asprintf(&repo_age, "%lld %s",
1296 (diff_time / 60 / 60 / 24), days)) == -1)
1298 } else if (diff_time > 60 * 60 * 2) {
1299 if ((asprintf(&repo_age, "%lld %s",
1300 (diff_time / 60 / 60), hours)) == -1)
1302 } else if (diff_time > 60 * 2) {
1303 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1306 } else if (diff_time > 2) {
1307 if ((asprintf(&repo_age, "%lld %s", diff_time,
1311 if ((asprintf(&repo_age, "%s", now)) == -1)
1316 if (gmtime_r(&committer_time, &tm) == NULL)
1319 s = asctime_r(&tm, datebuf);
1323 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1331 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1334 const struct got_error *error = NULL;
1335 struct got_object_id *id = NULL;
1336 struct got_repository *repo = NULL;
1337 struct got_commit_object *commit = NULL;
1338 struct got_reflist_head refs;
1339 struct got_reflist_entry *re;
1340 struct got_reference *head_ref;
1342 time_t committer_time = 0, cmp_time = 0;
1343 const char *refname;
1344 char *repo_age = NULL;
1346 if (repo_ref == NULL)
1349 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1352 SIMPLEQ_INIT(&refs);
1353 if (gw_trans->gw_conf->got_show_repo_age == false) {
1354 if ((asprintf(&repo_age, "")) == -1)
1359 error = got_repo_open(&repo, dir, NULL);
1364 error = got_ref_list(&refs, repo, "refs/heads",
1365 got_ref_cmp_by_name, NULL);
1367 error = got_ref_list(&refs, repo, repo_ref,
1368 got_ref_cmp_by_name, NULL);
1372 SIMPLEQ_FOREACH(re, &refs, entry) {
1374 refname = strdup(repo_ref);
1376 refname = got_ref_get_name(re->ref);
1377 error = got_ref_open(&head_ref, repo, refname, 0);
1381 error = got_ref_resolve(&id, repo, head_ref);
1382 got_ref_close(head_ref);
1386 error = got_object_open_as_commit(&commit, repo, id);
1391 got_object_commit_get_committer_time(commit);
1393 if (cmp_time < committer_time)
1394 cmp_time = committer_time;
1397 if (cmp_time != 0) {
1398 committer_time = cmp_time;
1399 repo_age = gw_get_time_str(committer_time, ref_tm);
1401 if ((asprintf(&repo_age, "")) == -1)
1403 got_ref_list_free(&refs);
1407 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1414 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1416 const struct got_error *error;
1418 struct got_object_id *id1 = NULL, *id2 = NULL;
1419 struct buf *diffbuf = NULL;
1420 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1421 *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1429 error = buf_alloc(&diffbuf, 0);
1433 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1437 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1438 error = got_repo_match_object_id(&id1, &label1,
1439 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1444 error = got_repo_match_object_id(&id2, &label2,
1445 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1449 error = got_object_get_type(&obj_type, header->repo, id2);
1453 case GOT_OBJ_TYPE_BLOB:
1454 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1457 case GOT_OBJ_TYPE_TREE:
1458 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1461 case GOT_OBJ_TYPE_COMMIT:
1462 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1466 error = got_error(GOT_ERR_OBJ_TYPE);
1469 if ((buf = calloc(128, sizeof(char *))) == NULL)
1472 fseek(f, 0, SEEK_SET);
1474 while ((fgets(buf, 2048, f)) != NULL) {
1476 while (*n_buf == '\n')
1478 newline = strchr(n_buf, '\n');
1482 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1483 if (buf_color == NULL)
1486 error = buf_puts(&newsize, diffbuf, buf_color);
1490 error = buf_puts(&newsize, diffbuf, div_end);
1495 if (buf_len(diffbuf) > 0) {
1496 error = buf_putc(diffbuf, '\0');
1497 diff_html = strdup(buf_get(diffbuf));
1516 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1519 char *owner = NULL, *d_file = NULL;
1520 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1521 char *comp, *pos, *buf;
1524 if (gw_trans->gw_conf->got_show_repo_owner == false)
1527 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1530 if ((f = fopen(d_file, "r")) == NULL)
1533 if ((buf = calloc(128, sizeof(char *))) == NULL)
1536 while ((fgets(buf, 128, f)) != NULL) {
1537 if ((pos = strstr(buf, gotweb)) != NULL)
1540 if ((pos = strstr(buf, gitweb)) != NULL)
1549 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1554 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1557 for (i = 0; i < 2; i++) {
1558 owner = strsep(&buf, "\"");
1568 if ((asprintf(&owner, "%s", "")) == -1)
1575 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1578 char *url = NULL, *d_file = NULL;
1581 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1584 if ((f = fopen(d_file, "r")) == NULL)
1587 fseek(f, 0, SEEK_END);
1589 fseek(f, 0, SEEK_SET);
1591 if ((url = calloc(len, sizeof(char *))) == NULL)
1594 fread(url, 1, len, f);
1601 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
1603 const struct got_error *error = NULL;
1604 struct got_repository *repo = NULL;
1605 struct got_reflist_head refs;
1606 struct got_reflist_entry *re;
1607 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1610 struct buf *diffbuf = NULL;
1613 error = buf_alloc(&diffbuf, 0);
1616 SIMPLEQ_INIT(&refs);
1618 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1622 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1626 SIMPLEQ_FOREACH(re, &refs, entry) {
1627 const char *refname;
1628 char *refstr, *tag_commit0, *tag_commit, *id_str;
1630 struct got_object_id *id;
1631 struct got_tag_object *tag;
1633 refname = got_ref_get_name(re->ref);
1634 if (strncmp(refname, "refs/tags/", 10) != 0)
1637 refstr = got_ref_to_str(re->ref);
1638 if (refstr == NULL) {
1639 error = got_error_from_errno("got_ref_to_str");
1643 error = got_ref_resolve(&id, repo, re->ref);
1646 error = got_object_open_as_tag(&tag, repo, id);
1651 tagger_time = got_object_tag_get_tagger_time(tag);
1653 error = got_object_id_str(&id_str,
1654 got_object_tag_get_object_id(tag));
1658 tag_commit0 = strdup(got_object_tag_get_message(tag));
1660 if (tag_commit0 == NULL) {
1661 error = got_error_from_errno("strdup");
1665 tag_commit = tag_commit0;
1666 while (*tag_commit == '\n')
1671 newline = strchr(tag_commit, '\n');
1675 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1677 error = got_error_from_errno("asprintf");
1681 if ((asprintf(&tags_navs_disp, tags_navs,
1682 gw_trans->repo_name, id_str, gw_trans->repo_name,
1683 id_str, gw_trans->repo_name, id_str,
1684 gw_trans->repo_name, id_str)) == -1) {
1685 error = got_error_from_errno("asprintf");
1689 if ((asprintf(&tag_row, tags_row, age, refname,
1690 tag_commit, tags_navs_disp)) == -1) {
1691 error = got_error_from_errno("asprintf");
1695 free(tags_navs_disp);
1703 got_object_tag_close(tag);
1705 error = buf_puts(&newsize, diffbuf, tag_row);
1713 if (error || (limit && --limit == 0))
1717 if (buf_len(diffbuf) > 0) {
1718 error = buf_putc(diffbuf, '\0');
1719 tags = strdup(buf_get(diffbuf));
1723 got_ref_list_free(&refs);
1725 got_repo_close(repo);
1733 gw_free_headers(struct gw_header *header)
1737 if (header->commit != NULL)
1738 got_object_commit_close(header->commit);
1740 got_repo_close(header->repo);
1741 free(header->refs_str);
1742 free(header->commit_id);
1743 free(header->parent_id);
1744 free(header->tree_id);
1745 free(header->author);
1746 free(header->committer);
1747 free(header->commit_msg);
1750 static struct gw_header *
1753 struct gw_header *header;
1755 header = malloc(sizeof(*header));
1759 header->repo = NULL;
1760 header->commit = NULL;
1762 header->path = NULL;
1763 SIMPLEQ_INIT(&header->refs);
1768 static const struct got_error *
1769 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1772 const struct got_error *error = NULL;
1773 struct got_commit_graph *graph = NULL;
1775 error = got_commit_graph_open(&graph, header->path, 0);
1779 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1785 error = got_commit_graph_iter_next(&header->id, graph,
1786 header->repo, NULL, NULL);
1788 if (error->code == GOT_ERR_ITER_COMPLETED)
1792 if (header->id == NULL)
1795 error = got_object_open_as_commit(&header->commit, header->repo,
1800 error = gw_get_commit(gw_trans, header);
1802 struct gw_header *n_header = NULL;
1803 if ((n_header = gw_init_header()) == NULL)
1804 error = got_error_from_errno("malloc");
1806 n_header->refs_str = strdup(header->refs_str);
1807 n_header->commit_id = strdup(header->commit_id);
1808 n_header->parent_id = strdup(header->parent_id);
1809 n_header->tree_id = strdup(header->tree_id);
1810 n_header->author = strdup(header->author);
1811 n_header->committer = strdup(header->committer);
1812 n_header->commit_msg = strdup(header->commit_msg);
1813 n_header->committer_time = header->committer_time;
1814 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
1817 if (error || (limit && --limit == 0))
1822 got_commit_graph_close(graph);
1826 static const struct got_error *
1827 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
1829 const struct got_error *error = NULL;
1830 struct got_reflist_entry *re;
1831 struct got_object_id *id2 = NULL;
1832 struct got_object_qid *parent_id;
1833 char *refs_str = NULL,
1834 *commit_msg = NULL, *commit_msg0;
1837 SIMPLEQ_FOREACH(re, &header->refs, entry) {
1840 struct got_tag_object *tag = NULL;
1843 name = got_ref_get_name(re->ref);
1844 if (strcmp(name, GOT_REF_HEAD) == 0)
1846 if (strncmp(name, "refs/", 5) == 0)
1848 if (strncmp(name, "got/", 4) == 0)
1850 if (strncmp(name, "heads/", 6) == 0)
1852 if (strncmp(name, "remotes/", 8) == 0)
1854 if (strncmp(name, "tags/", 5) == 0) {
1855 error = got_object_open_as_tag(&tag, header->repo,
1858 if (error->code != GOT_ERR_OBJ_TYPE)
1861 * Ref points at something other
1868 cmp = got_object_id_cmp(tag ?
1869 got_object_tag_get_object_id(tag) : re->id, header->id);
1871 got_object_tag_close(tag);
1875 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1876 s ? ", " : "", name)) == -1) {
1877 error = got_error_from_errno("asprintf");
1881 header->refs_str = strdup(refs_str);
1885 if (refs_str == NULL)
1886 header->refs_str = strdup("");
1889 error = got_object_id_str(&header->commit_id, header->id);
1893 error = got_object_id_str(&header->tree_id,
1894 got_object_commit_get_tree_id(header->commit));
1898 if (gw_trans->action == GW_DIFF) {
1899 parent_id = SIMPLEQ_FIRST(
1900 got_object_commit_get_parent_ids(header->commit));
1901 if (parent_id != NULL) {
1902 id2 = got_object_id_dup(parent_id->id);
1904 error = got_object_id_str(&header->parent_id, id2);
1909 header->parent_id = strdup("/dev/null");
1911 header->parent_id = strdup("");
1913 header->committer_time =
1914 got_object_commit_get_committer_time(header->commit);
1915 header->author = strdup(got_object_commit_get_author(header->commit));
1917 strdup(got_object_commit_get_committer(header->commit));
1919 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
1923 commit_msg = commit_msg0;
1924 while (*commit_msg == '\n')
1927 header->commit_msg = strdup(commit_msg);
1932 static const struct got_error *
1933 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
1935 const struct got_error *error = NULL;
1936 char *in_repo_path = NULL;
1938 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1942 if (gw_trans->commit == NULL) {
1943 struct got_reference *head_ref;
1944 error = got_ref_open(&head_ref, header->repo,
1945 gw_trans->headref, 0);
1949 error = got_ref_resolve(&header->id, header->repo, head_ref);
1950 got_ref_close(head_ref);
1954 error = got_object_open_as_commit(&header->commit,
1955 header->repo, header->id);
1957 struct got_reference *ref;
1958 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
1959 if (error == NULL) {
1961 error = got_ref_resolve(&header->id, header->repo, ref);
1965 error = got_object_get_type(&obj_type, header->repo,
1969 if (obj_type == GOT_OBJ_TYPE_TAG) {
1970 struct got_tag_object *tag;
1971 error = got_object_open_as_tag(&tag,
1972 header->repo, header->id);
1975 if (got_object_tag_get_object_type(tag) !=
1976 GOT_OBJ_TYPE_COMMIT) {
1977 got_object_tag_close(tag);
1978 error = got_error(GOT_ERR_OBJ_TYPE);
1982 header->id = got_object_id_dup(
1983 got_object_tag_get_object_id(tag));
1984 if (header->id == NULL)
1985 error = got_error_from_errno(
1986 "got_object_id_dup");
1987 got_object_tag_close(tag);
1990 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1991 error = got_error(GOT_ERR_OBJ_TYPE);
1994 error = got_object_open_as_commit(&header->commit,
1995 header->repo, header->id);
1999 if (header->commit == NULL) {
2000 error = got_repo_match_object_id_prefix(&header->id,
2001 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2006 error = got_repo_match_object_id_prefix(&header->id,
2007 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2011 error = got_repo_map_path(&in_repo_path, header->repo,
2012 gw_trans->repo_path, 1);
2017 header->path = strdup(in_repo_path);
2021 error = got_ref_list(&header->refs, header->repo, NULL,
2022 got_ref_cmp_by_name, NULL);
2026 error = gw_get_commits(gw_trans, header, limit);
2034 char datebuf[11]; /* YYYY-MM-DD + NUL */
2037 struct gw_blame_cb_args {
2038 struct blame_line *lines;
2042 off_t *line_offsets;
2044 struct got_repository *repo;
2045 struct gw_trans *gw_trans;
2046 struct buf *blamebuf;
2049 static const struct got_error *
2050 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2052 const struct got_error *err = NULL;
2053 struct gw_blame_cb_args *a = arg;
2054 struct blame_line *bline;
2056 size_t linesize = 0, newsize;
2057 struct got_commit_object *commit = NULL;
2060 time_t committer_time;
2062 if (nlines != a->nlines ||
2063 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2064 return got_error(GOT_ERR_RANGE);
2067 return NULL; /* no change in this commit */
2069 /* Annotate this line. */
2070 bline = &a->lines[lineno - 1];
2071 if (bline->annotated)
2073 err = got_object_id_str(&bline->id_str, id);
2077 err = got_object_open_as_commit(&commit, a->repo, id);
2081 bline->committer = strdup(got_object_commit_get_committer(commit));
2082 if (bline->committer == NULL) {
2083 err = got_error_from_errno("strdup");
2087 committer_time = got_object_commit_get_committer_time(commit);
2088 if (localtime_r(&committer_time, &tm) == NULL)
2089 return got_error_from_errno("localtime_r");
2090 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2091 &tm) >= sizeof(bline->datebuf)) {
2092 err = got_error(GOT_ERR_NO_SPACE);
2095 bline->annotated = 1;
2097 /* Print lines annotated so far. */
2098 bline = &a->lines[a->lineno_cur - 1];
2099 if (!bline->annotated)
2102 offset = a->line_offsets[a->lineno_cur - 1];
2103 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2104 err = got_error_from_errno("fseeko");
2108 while (bline->annotated) {
2109 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2110 *line_escape = NULL;
2113 if (getline(&line, &linesize, a->f) == -1) {
2115 err = got_error_from_errno("getline");
2119 committer = bline->committer;
2120 smallerthan = strchr(committer, '<');
2121 if (smallerthan && smallerthan[1] != '\0')
2122 committer = smallerthan + 1;
2123 at = strchr(committer, '@');
2126 len = strlen(committer);
2128 committer[8] = '\0';
2130 nl = strchr(line, '\n');
2134 if (strcmp(line, "") != 0)
2135 line_escape = strdup(gw_html_escape(line));
2137 line_escape = strdup("");
2139 asprintf(&blame_row, blame_line, a->nlines_prec,
2140 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2143 err = buf_puts(&newsize, a->blamebuf, blame_row);
2147 bline = &a->lines[a->lineno_cur - 1];
2153 got_object_commit_close(commit);
2159 gw_get_file_blame(struct gw_trans *gw_trans)
2161 const struct got_error *error = NULL;
2162 struct got_repository *repo = NULL;
2163 struct got_object_id *obj_id = NULL;
2164 struct got_object_id *commit_id = NULL;
2165 struct got_blob_object *blob = NULL;
2166 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2168 struct gw_blame_cb_args bca;
2172 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2176 if (gw_trans->repo_folder != NULL) {
2177 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2178 error = got_error_from_errno("asprintf");
2182 folder = strdup("");
2184 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2185 error = got_error_from_errno("asprintf");
2190 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2194 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2195 GOT_OBJ_TYPE_COMMIT, 1, repo);
2199 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2203 if (obj_id == NULL) {
2204 error = got_error(GOT_ERR_NO_OBJ);
2208 error = got_object_get_type(&obj_type, repo, obj_id);
2212 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2213 error = got_error(GOT_ERR_OBJ_TYPE);
2217 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2221 error = buf_alloc(&bca.blamebuf, 0);
2225 bca.f = got_opentemp();
2226 if (bca.f == NULL) {
2227 error = got_error_from_errno("got_opentemp");
2230 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2231 &bca.line_offsets, bca.f, blob);
2232 if (error || bca.nlines == 0)
2235 /* Don't include \n at EOF in the blame line count. */
2236 if (bca.line_offsets[bca.nlines - 1] == filesize)
2239 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2240 if (bca.lines == NULL) {
2241 error = got_error_from_errno("calloc");
2245 bca.nlines_prec = 0;
2252 bca.gw_trans = gw_trans;
2254 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2256 if (buf_len(bca.blamebuf) > 0) {
2257 error = buf_putc(bca.blamebuf, '\0');
2258 blame_html = strdup(buf_get(bca.blamebuf));
2268 error = got_object_blob_close(blob);
2270 error = got_repo_close(repo);
2274 for (i = 0; i < bca.nlines; i++) {
2275 struct blame_line *bline = &bca.lines[i];
2276 free(bline->id_str);
2277 free(bline->committer);
2281 free(bca.line_offsets);
2282 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2283 error = got_error_from_errno("fclose");
2291 gw_get_repo_tree(struct gw_trans *gw_trans)
2293 const struct got_error *error = NULL;
2294 struct got_repository *repo = NULL;
2295 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2296 struct got_tree_object *tree = NULL;
2297 struct buf *diffbuf = NULL;
2299 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2300 *tree_row = NULL, *id_str;
2303 error = buf_alloc(&diffbuf, 0);
2307 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2311 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2315 if (gw_trans->repo_folder != NULL)
2316 path = strdup(gw_trans->repo_folder);
2317 else if (in_repo_path) {
2319 path = in_repo_path;
2322 if (gw_trans->commit == NULL) {
2323 struct got_reference *head_ref;
2324 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2328 error = got_ref_resolve(&commit_id, repo, head_ref);
2329 got_ref_close(head_ref);
2332 error = got_repo_match_object_id(&commit_id, NULL,
2333 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2337 error = got_object_id_str(&gw_trans->commit, commit_id);
2341 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2345 error = got_object_open_as_tree(&tree, repo, tree_id);
2349 nentries = got_object_tree_get_nentries(tree);
2351 for (i = 0; i < nentries; i++) {
2352 struct got_tree_entry *te;
2353 const char *modestr = "";
2354 char *id = NULL, *url_html = NULL;
2356 te = got_object_tree_get_entry(tree, i);
2358 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2362 if ((asprintf(&id, "%s", id_str)) == -1) {
2363 error = got_error_from_errno("asprintf");
2368 mode_t mode = got_tree_entry_get_mode(te);
2370 if (got_object_tree_entry_is_submodule(te))
2372 else if (S_ISLNK(mode))
2374 else if (S_ISDIR(mode))
2376 else if (mode & S_IXUSR)
2379 char *build_folder = NULL;
2380 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2381 if (gw_trans->repo_folder != NULL) {
2382 if ((asprintf(&build_folder, "%s/%s",
2383 gw_trans->repo_folder,
2384 got_tree_entry_get_name(te))) == -1) {
2386 got_error_from_errno("asprintf");
2390 if (asprintf(&build_folder, "%s",
2391 got_tree_entry_get_name(te)) == -1)
2395 if ((asprintf(&url_html, folder_html,
2396 gw_trans->repo_name, gw_trans->action_name,
2397 gw_trans->commit, build_folder,
2398 got_tree_entry_get_name(te), modestr)) == -1) {
2399 error = got_error_from_errno("asprintf");
2403 if (gw_trans->repo_folder != NULL) {
2404 if ((asprintf(&build_folder, "%s",
2405 gw_trans->repo_folder)) == -1) {
2407 got_error_from_errno("asprintf");
2411 build_folder = strdup("");
2413 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2414 "blame", gw_trans->commit,
2415 got_tree_entry_get_name(te), build_folder,
2416 got_tree_entry_get_name(te), modestr)) == -1) {
2417 error = got_error_from_errno("asprintf");
2426 if ((asprintf(&tree_row, tree_line, url_html)) == -1) {
2427 error = got_error_from_errno("asprintf");
2430 error = buf_puts(&newsize, diffbuf, tree_row);
2440 if (buf_len(diffbuf) > 0) {
2441 error = buf_putc(diffbuf, '\0');
2442 tree_html = strdup(buf_get(diffbuf));
2446 got_object_tree_close(tree);
2448 got_repo_close(repo);
2460 gw_get_repo_heads(struct gw_trans *gw_trans)
2462 const struct got_error *error = NULL;
2463 struct got_repository *repo = NULL;
2464 struct got_reflist_head refs;
2465 struct got_reflist_entry *re;
2466 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2467 struct buf *diffbuf = NULL;
2470 error = buf_alloc(&diffbuf, 0);
2474 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2478 SIMPLEQ_INIT(&refs);
2479 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2484 SIMPLEQ_FOREACH(re, &refs, entry) {
2487 refname = strdup(got_ref_get_name(re->ref));
2488 if (refname == NULL) {
2489 error = got_error_from_errno("got_ref_to_str");
2493 if (strncmp(refname, "refs/heads/", 11) != 0) {
2498 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2501 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2502 refname, gw_trans->repo_name, refname,
2503 gw_trans->repo_name, refname, gw_trans->repo_name,
2505 error = got_error_from_errno("asprintf");
2509 if (strncmp(refname, "refs/heads/", 11) == 0)
2512 if ((asprintf(&head_row, heads_row, age, refname,
2513 head_navs_disp)) == -1) {
2514 error = got_error_from_errno("asprintf");
2518 error = buf_puts(&newsize, diffbuf, head_row);
2520 free(head_navs_disp);
2524 if (buf_len(diffbuf) > 0) {
2525 error = buf_putc(diffbuf, '\0');
2526 heads = strdup(buf_get(diffbuf));
2530 got_ref_list_free(&refs);
2532 got_repo_close(repo);
2540 gw_get_got_link(struct gw_trans *gw_trans)
2544 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2545 gw_trans->gw_conf->got_logo)) == -1)
2552 gw_get_site_link(struct gw_trans *gw_trans)
2554 char *link, *repo = "", *action = "";
2556 if (gw_trans->repo_name != NULL)
2557 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2558 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2561 if (gw_trans->action_name != NULL)
2562 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2565 if ((asprintf(&link, site_link, GOTWEB,
2566 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2573 gw_colordiff_line(char *buf)
2575 const struct got_error *error = NULL;
2576 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2577 struct buf *diffbuf = NULL;
2580 error = buf_alloc(&diffbuf, 0);
2586 if (strncmp(buf, "-", 1) == 0)
2587 color = "diff_minus";
2588 if (strncmp(buf, "+", 1) == 0)
2589 color = "diff_plus";
2590 if (strncmp(buf, "@@", 2) == 0)
2591 color = "diff_chunk_header";
2592 if (strncmp(buf, "@@", 2) == 0)
2593 color = "diff_chunk_header";
2594 if (strncmp(buf, "commit +", 8) == 0)
2595 color = "diff_meta";
2596 if (strncmp(buf, "commit -", 8) == 0)
2597 color = "diff_meta";
2598 if (strncmp(buf, "blob +", 6) == 0)
2599 color = "diff_meta";
2600 if (strncmp(buf, "blob -", 6) == 0)
2601 color = "diff_meta";
2602 if (strncmp(buf, "file +", 6) == 0)
2603 color = "diff_meta";
2604 if (strncmp(buf, "file -", 6) == 0)
2605 color = "diff_meta";
2606 if (strncmp(buf, "from:", 5) == 0)
2607 color = "diff_author";
2608 if (strncmp(buf, "via:", 4) == 0)
2609 color = "diff_author";
2610 if (strncmp(buf, "date:", 5) == 0)
2611 color = "diff_date";
2613 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2616 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2620 error = buf_puts(&newsize, diffbuf, buf);
2624 if (buf_len(diffbuf) > 0) {
2625 error = buf_putc(diffbuf, '\0');
2626 colorized_line = strdup(buf_get(diffbuf));
2630 free(div_diff_line_div);
2631 return colorized_line;
2635 gw_html_escape(const char *html)
2637 char *escaped_str = NULL, *buf;
2639 size_t sz, i, buff_sz = 2048;
2641 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2647 if ((sz = strlen(html)) == 0)
2650 /* only work with buff_sz */
2654 for (i = 0; i < sz; i++) {
2658 strcat(buf, ">");
2661 strcat(buf, "&");
2664 strcat(buf, "<");
2667 strcat(buf, """);
2670 strcat(buf, "'");
2673 strcat(buf, "<br />");
2679 asprintf(&escaped_str, "%s", buf);
2685 main(int argc, char *argv[])
2687 const struct got_error *error = NULL;
2688 struct gw_trans *gw_trans;
2689 struct gw_dir *dir = NULL, *tdir;
2690 const char *page = "index";
2694 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2697 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2700 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2703 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2706 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2707 if (kerr != KCGI_OK) {
2708 error = gw_kcgi_error(kerr);
2712 if ((gw_trans->gw_conf =
2713 malloc(sizeof(struct gotweb_conf))) == NULL) {
2715 error = got_error_from_errno("malloc");
2719 TAILQ_INIT(&gw_trans->gw_dirs);
2720 TAILQ_INIT(&gw_trans->gw_headers);
2723 gw_trans->repos_total = 0;
2724 gw_trans->repo_path = NULL;
2725 gw_trans->commit = NULL;
2726 gw_trans->headref = strdup(GOT_REF_HEAD);
2727 gw_trans->mime = KMIME_TEXT_HTML;
2728 gw_trans->gw_tmpl->key = gw_templs;
2729 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2730 gw_trans->gw_tmpl->arg = gw_trans;
2731 gw_trans->gw_tmpl->cb = gw_template;
2732 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2736 gw_trans->mime = KMIME_TEXT_PLAIN;
2737 gw_trans->action = GW_ERR;
2738 gw_display_index(gw_trans, error);
2742 error = gw_parse_querystring(gw_trans);
2746 error = gw_display_index(gw_trans, error);
2752 free(gw_trans->gw_conf->got_repos_path);
2753 free(gw_trans->gw_conf->got_www_path);
2754 free(gw_trans->gw_conf->got_site_name);
2755 free(gw_trans->gw_conf->got_site_owner);
2756 free(gw_trans->gw_conf->got_site_link);
2757 free(gw_trans->gw_conf->got_logo);
2758 free(gw_trans->gw_conf->got_logo_url);
2759 free(gw_trans->gw_conf);
2760 free(gw_trans->commit);
2761 free(gw_trans->repo_path);
2762 free(gw_trans->repo_name);
2763 free(gw_trans->repo_file);
2764 free(gw_trans->action_name);
2765 free(gw_trans->headref);
2767 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2769 free(dir->description);
2778 khttp_free(gw_trans->gw_req);
2779 return EXIT_SUCCESS;