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 = malloc(sizeof(struct gw_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 = malloc(sizeof(struct gw_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);
592 got_ref_list_free(&header->refs);
593 gw_free_headers(header);
594 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
595 gw_free_headers(n_header);
599 static const struct got_error *
600 gw_briefs(struct gw_trans *gw_trans)
602 const struct got_error *error = NULL;
603 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
604 struct gw_header *header = NULL, *n_header = NULL;
607 if ((header = malloc(sizeof(struct gw_header))) == NULL)
608 return got_error_from_errno("malloc");
610 if (pledge("stdio rpath proc exec sendfd unveil",
612 error = got_error_from_errno("pledge");
616 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
620 if (gw_trans->action == GW_SUMMARY)
621 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
623 error = gw_get_header(gw_trans, header,
624 gw_trans->gw_conf->got_max_commits_display);
629 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
631 return gw_kcgi_error(kerr);
633 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
634 if ((asprintf(&briefs_navs_html, briefs_navs,
635 gw_trans->repo_name, n_header->commit_id,
636 gw_trans->repo_name, n_header->commit_id,
637 gw_trans->repo_name, n_header->commit_id)) == -1)
638 return got_error_from_errno("asprintf");
639 newline = strchr(n_header->commit_msg, '\n');
642 if ((asprintf(&briefs_html, briefs_line,
643 gw_get_time_str(n_header->committer_time, TM_DIFF),
644 n_header->author, gw_html_escape(n_header->commit_msg),
645 briefs_navs_html)) == -1)
646 return got_error_from_errno("asprintf");
647 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
649 return gw_kcgi_error(kerr);
651 kerr = khttp_puts(gw_trans->gw_req, div_end);
653 error = gw_kcgi_error(kerr);
655 got_ref_list_free(&header->refs);
656 gw_free_headers(header);
657 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
658 gw_free_headers(n_header);
662 static const struct got_error *
663 gw_summary(struct gw_trans *gw_trans)
665 const struct got_error *error = NULL;
666 char *description_html, *repo_owner_html, *repo_age_html,
667 *cloneurl_html, *tags, *heads, *tags_html,
671 if (pledge("stdio rpath proc exec sendfd unveil",
673 error = got_error_from_errno("pledge");
677 /* unveil is applied with gw_briefs below */
679 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
681 return gw_kcgi_error(kerr);
683 if (gw_trans->gw_conf->got_show_repo_description) {
684 if (gw_trans->gw_dir->description != NULL &&
685 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
686 if ((asprintf(&description_html, description,
687 gw_trans->gw_dir->description)) == -1)
688 return got_error_from_errno("asprintf");
690 kerr = khttp_puts(gw_trans->gw_req, description_html);
691 free(description_html);
693 return gw_kcgi_error(kerr);
697 if (gw_trans->gw_conf->got_show_repo_owner) {
698 if (gw_trans->gw_dir->owner != NULL &&
699 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
700 if ((asprintf(&repo_owner_html, repo_owner,
701 gw_trans->gw_dir->owner)) == -1)
702 return got_error_from_errno("asprintf");
704 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
705 free(repo_owner_html);
707 return gw_kcgi_error(kerr);
711 if (gw_trans->gw_conf->got_show_repo_age) {
712 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
713 "refs/heads", TM_LONG);
714 if (age != NULL && (strcmp(age, "") != 0)) {
715 if ((asprintf(&repo_age_html, last_change, age)) == -1)
716 return got_error_from_errno("asprintf");
718 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
722 return gw_kcgi_error(kerr);
726 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
727 if (gw_trans->gw_dir->url != NULL &&
728 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
729 if ((asprintf(&cloneurl_html, cloneurl,
730 gw_trans->gw_dir->url)) == -1)
731 return got_error_from_errno("asprintf");
733 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
736 return gw_kcgi_error(kerr);
739 kerr = khttp_puts(gw_trans->gw_req, div_end);
741 return gw_kcgi_error(kerr);
743 error = gw_briefs(gw_trans);
747 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
748 heads = gw_get_repo_heads(gw_trans);
750 if (tags != NULL && strcmp(tags, "") != 0) {
751 if ((asprintf(&tags_html, summary_tags,
753 return got_error_from_errno("asprintf");
754 kerr = khttp_puts(gw_trans->gw_req, tags_html);
758 return gw_kcgi_error(kerr);
761 if (heads != NULL && strcmp(heads, "") != 0) {
762 if ((asprintf(&heads_html, summary_heads,
764 return got_error_from_errno("asprintf");
765 kerr = khttp_puts(gw_trans->gw_req, heads_html);
769 return gw_kcgi_error(kerr);
774 static const struct got_error *
775 gw_tree(struct gw_trans *gw_trans)
777 const struct got_error *error = NULL;
778 struct gw_header *header = NULL;
779 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
782 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
783 return got_error_from_errno("pledge");
785 if ((header = malloc(sizeof(struct gw_header))) == NULL)
786 return got_error_from_errno("malloc");
788 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
792 error = gw_get_header(gw_trans, header, 1);
796 tree_html = gw_get_repo_tree(gw_trans);
798 if (tree_html == NULL)
799 tree_html = strdup("");
801 if ((asprintf(&tree_html_disp, tree_header,
802 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
803 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
805 return got_error_from_errno("asprintf");
807 if ((asprintf(&tree, tree_wrapper, tree_html_disp)) == -1)
808 return got_error_from_errno("asprintf");
810 kerr = khttp_puts(gw_trans->gw_req, tree);
812 error = gw_kcgi_error(kerr);
813 got_ref_list_free(&header->refs);
814 gw_free_headers(header);
815 free(tree_html_disp);
821 static const struct got_error *
822 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
824 const struct got_error *error = NULL;
829 if ((asprintf(&dir_test, "%s/%s/%s",
830 gw_trans->gw_conf->got_repos_path, gw_dir->name,
831 GOTWEB_GIT_DIR)) == -1)
832 return got_error_from_errno("asprintf");
834 dt = opendir(dir_test);
838 gw_dir->path = strdup(dir_test);
843 if ((asprintf(&dir_test, "%s/%s/%s",
844 gw_trans->gw_conf->got_repos_path, gw_dir->name,
845 GOTWEB_GOT_DIR)) == -1)
846 return got_error_from_errno("asprintf");
848 dt = opendir(dir_test);
853 error = got_error(GOT_ERR_NOT_GIT_REPO);
857 if ((asprintf(&dir_test, "%s/%s",
858 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
859 return got_error_from_errno("asprintf");
861 gw_dir->path = strdup(dir_test);
864 gw_dir->description = gw_get_repo_description(gw_trans,
866 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
867 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
869 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
878 static const struct got_error *
879 gw_load_got_paths(struct gw_trans *gw_trans)
881 const struct got_error *error = NULL;
883 struct dirent **sd_dent;
884 struct gw_dir *gw_dir;
886 unsigned int d_cnt, d_i;
888 d = opendir(gw_trans->gw_conf->got_repos_path);
890 error = got_error_from_errno2("opendir",
891 gw_trans->gw_conf->got_repos_path);
895 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
898 error = got_error_from_errno2("scandir",
899 gw_trans->gw_conf->got_repos_path);
903 for (d_i = 0; d_i < d_cnt; d_i++) {
904 if (gw_trans->gw_conf->got_max_repos > 0 &&
905 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
906 break; /* account for parent and self */
908 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
909 strcmp(sd_dent[d_i]->d_name, "..") == 0)
912 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
913 return got_error_from_errno("gw_dir malloc");
915 error = gw_load_got_path(gw_trans, gw_dir);
916 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
921 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
922 !got_path_dir_is_empty(gw_dir->path)) {
923 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
925 gw_trans->repos_total++;
933 static const struct got_error *
934 gw_parse_querystring(struct gw_trans *gw_trans)
936 const struct got_error *error = NULL;
938 struct gw_query_action *action = NULL;
941 if (gw_trans->gw_req->fieldnmap[0]) {
942 error = got_error_from_errno("bad parse");
944 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
945 /* define gw_trans->repo_path */
946 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
947 return got_error_from_errno("asprintf");
949 if ((asprintf(&gw_trans->repo_path, "%s/%s",
950 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
951 return got_error_from_errno("asprintf");
953 /* get action and set function */
954 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
955 for (i = 0; i < nitems(gw_query_funcs); i++) {
956 action = &gw_query_funcs[i];
957 if (action->func_name == NULL)
960 if (strcmp(action->func_name,
962 gw_trans->action = i;
963 if ((asprintf(&gw_trans->action_name,
964 "%s", action->func_name)) == -1)
966 got_error_from_errno(
975 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
976 if ((asprintf(&gw_trans->commit, "%s",
978 return got_error_from_errno("asprintf");
980 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
981 if ((asprintf(&gw_trans->repo_file, "%s",
983 return got_error_from_errno("asprintf");
985 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
986 if ((asprintf(&gw_trans->repo_folder, "%s",
988 return got_error_from_errno("asprintf");
990 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
991 if ((asprintf(&gw_trans->headref, "%s",
993 return got_error_from_errno("asprintf");
995 if (action == NULL) {
996 error = got_error_from_errno("invalid action");
999 if ((gw_trans->gw_dir =
1000 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1001 return got_error_from_errno("gw_dir malloc");
1003 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1007 gw_trans->action = GW_INDEX;
1009 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1010 gw_trans->page = p->parsed.i;
1012 /* if (gw_trans->action == GW_RAW) */
1013 /* gw_trans->mime = KMIME_TEXT_PLAIN; */
1018 static struct gw_dir *
1019 gw_init_gw_dir(char *dir)
1021 struct gw_dir *gw_dir;
1023 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1026 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1032 static const struct got_error *
1033 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1037 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1038 if (kerr != KCGI_OK)
1039 return gw_kcgi_error(kerr);
1040 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1042 if (kerr != KCGI_OK)
1043 return gw_kcgi_error(kerr);
1044 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1046 if (kerr != KCGI_OK)
1047 return gw_kcgi_error(kerr);
1048 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1049 if (kerr != KCGI_OK)
1050 return gw_kcgi_error(kerr);
1051 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1052 if (kerr != KCGI_OK)
1053 return gw_kcgi_error(kerr);
1054 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1055 if (kerr != KCGI_OK)
1056 return gw_kcgi_error(kerr);
1058 kerr = khttp_body(gw_trans->gw_req);
1059 return gw_kcgi_error(kerr);
1062 static const struct got_error *
1063 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
1067 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1068 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1071 kerr = khttp_puts(gw_trans->gw_req, err->msg);
1073 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1074 gw_query_funcs[gw_trans->action].template);
1075 if (kerr != KCGI_OK)
1076 return gw_kcgi_error(kerr);
1078 kerr = khtml_close(gw_trans->gw_html_req);
1079 return gw_kcgi_error(kerr);
1083 gw_template(size_t key, void *arg)
1085 const struct got_error *error = NULL;
1086 struct gw_trans *gw_trans = arg;
1087 char *gw_got_link, *gw_site_link;
1088 char *site_owner_name, *site_owner_name_h;
1092 khttp_puts(gw_trans->gw_req, head);
1095 gw_got_link = gw_get_got_link(gw_trans);
1096 if (gw_got_link != NULL)
1097 khttp_puts(gw_trans->gw_req, gw_got_link);
1101 case (TEMPL_SITEPATH):
1102 gw_site_link = gw_get_site_link(gw_trans);
1103 if (gw_site_link != NULL)
1104 khttp_puts(gw_trans->gw_req, gw_site_link);
1109 if (gw_trans->gw_conf->got_site_name != NULL)
1110 khtml_puts(gw_trans->gw_html_req,
1111 gw_trans->gw_conf->got_site_name);
1114 case (TEMPL_SEARCH):
1115 khttp_puts(gw_trans->gw_req, search);
1117 case(TEMPL_SITEOWNER):
1118 if (gw_trans->gw_conf->got_site_owner != NULL &&
1119 gw_trans->gw_conf->got_show_site_owner) {
1121 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1122 if ((asprintf(&site_owner_name_h, site_owner,
1127 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1128 free(site_owner_name);
1129 free(site_owner_name_h);
1132 case(TEMPL_CONTENT):
1133 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1135 khttp_puts(gw_trans->gw_req, error->msg);
1144 gw_gen_commit_header(char *str1, char *str2)
1146 char *return_html = NULL, *ref_str = NULL;
1148 if (strcmp(str2, "") != 0) {
1149 if ((asprintf(&ref_str, "(%s)", str2)) == -1) {
1150 return_html = strdup("");
1154 ref_str = strdup("");
1157 if ((asprintf(&return_html, header_commit_html, str1, ref_str)) == -1)
1158 return_html = strdup("");
1165 gw_gen_diff_header(char *str1, char *str2)
1167 char *return_html = NULL;
1169 if ((asprintf(&return_html, header_diff_html, str1, str2)) == -1)
1170 return_html = strdup("");
1176 gw_gen_author_header(char *str)
1178 char *return_html = NULL;
1180 if ((asprintf(&return_html, header_author_html, str)) == -1)
1181 return_html = strdup("");
1187 gw_gen_committer_header(char *str)
1189 char *return_html = NULL;
1191 if ((asprintf(&return_html, header_committer_html, str)) == -1)
1192 return_html = strdup("");
1198 gw_gen_age_header(char *str)
1200 char *return_html = NULL;
1202 if ((asprintf(&return_html, header_age_html, str)) == -1)
1203 return_html = strdup("");
1209 gw_gen_commit_msg_header(char *str)
1211 char *return_html = NULL;
1213 if ((asprintf(&return_html, header_commit_msg_html, str)) == -1)
1214 return_html = strdup("");
1220 gw_gen_tree_header(char *str)
1222 char *return_html = NULL;
1224 if ((asprintf(&return_html, header_tree_html, str)) == -1)
1225 return_html = strdup("");
1231 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1234 char *description = NULL, *d_file = NULL;
1237 if (gw_trans->gw_conf->got_show_repo_description == false)
1240 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1243 if ((f = fopen(d_file, "r")) == NULL)
1246 fseek(f, 0, SEEK_END);
1248 fseek(f, 0, SEEK_SET);
1249 if ((description = calloc(len, sizeof(char *))) == NULL)
1252 fread(description, 1, len, f);
1257 if ((asprintf(&description, "%s", "")) == -1)
1264 gw_get_time_str(time_t committer_time, int ref_tm)
1268 char *years = "years ago", *months = "months ago";
1269 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1270 char *minutes = "minutes ago", *seconds = "seconds ago";
1271 char *now = "right now";
1277 diff_time = time(NULL) - committer_time;
1278 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1279 if ((asprintf(&repo_age, "%lld %s",
1280 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1282 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1283 if ((asprintf(&repo_age, "%lld %s",
1284 (diff_time / 60 / 60 / 24 / (365 / 12)),
1287 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1288 if ((asprintf(&repo_age, "%lld %s",
1289 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1291 } else if (diff_time > 60 * 60 * 24 * 2) {
1292 if ((asprintf(&repo_age, "%lld %s",
1293 (diff_time / 60 / 60 / 24), days)) == -1)
1295 } else if (diff_time > 60 * 60 * 2) {
1296 if ((asprintf(&repo_age, "%lld %s",
1297 (diff_time / 60 / 60), hours)) == -1)
1299 } else if (diff_time > 60 * 2) {
1300 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1303 } else if (diff_time > 2) {
1304 if ((asprintf(&repo_age, "%lld %s", diff_time,
1308 if ((asprintf(&repo_age, "%s", now)) == -1)
1313 if (gmtime_r(&committer_time, &tm) == NULL)
1316 s = asctime_r(&tm, datebuf);
1320 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1328 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1331 const struct got_error *error = NULL;
1332 struct got_object_id *id = NULL;
1333 struct got_repository *repo = NULL;
1334 struct got_commit_object *commit = NULL;
1335 struct got_reflist_head refs;
1336 struct got_reflist_entry *re;
1337 struct got_reference *head_ref;
1339 time_t committer_time = 0, cmp_time = 0;
1340 const char *refname;
1341 char *repo_age = NULL;
1343 if (repo_ref == NULL)
1346 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1349 SIMPLEQ_INIT(&refs);
1350 if (gw_trans->gw_conf->got_show_repo_age == false) {
1351 if ((asprintf(&repo_age, "")) == -1)
1356 error = got_repo_open(&repo, dir, NULL);
1361 error = got_ref_list(&refs, repo, "refs/heads",
1362 got_ref_cmp_by_name, NULL);
1364 error = got_ref_list(&refs, repo, repo_ref,
1365 got_ref_cmp_by_name, NULL);
1369 SIMPLEQ_FOREACH(re, &refs, entry) {
1371 refname = strdup(repo_ref);
1373 refname = got_ref_get_name(re->ref);
1374 error = got_ref_open(&head_ref, repo, refname, 0);
1378 error = got_ref_resolve(&id, repo, head_ref);
1379 got_ref_close(head_ref);
1383 error = got_object_open_as_commit(&commit, repo, id);
1388 got_object_commit_get_committer_time(commit);
1390 if (cmp_time < committer_time)
1391 cmp_time = committer_time;
1394 if (cmp_time != 0) {
1395 committer_time = cmp_time;
1396 repo_age = gw_get_time_str(committer_time, ref_tm);
1398 if ((asprintf(&repo_age, "")) == -1)
1400 got_ref_list_free(&refs);
1404 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1411 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1413 const struct got_error *error;
1415 struct got_object_id *id1 = NULL, *id2 = NULL;
1416 struct buf *diffbuf = NULL;
1417 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1418 *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1426 error = buf_alloc(&diffbuf, 0);
1430 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1434 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1435 error = got_repo_match_object_id(&id1, &label1,
1436 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1441 error = got_repo_match_object_id(&id2, &label2,
1442 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1446 error = got_object_get_type(&obj_type, header->repo, id2);
1450 case GOT_OBJ_TYPE_BLOB:
1451 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1454 case GOT_OBJ_TYPE_TREE:
1455 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1458 case GOT_OBJ_TYPE_COMMIT:
1459 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1463 error = got_error(GOT_ERR_OBJ_TYPE);
1466 if ((buf = calloc(128, sizeof(char *))) == NULL)
1469 fseek(f, 0, SEEK_SET);
1471 while ((fgets(buf, 2048, f)) != NULL) {
1473 while (*n_buf == '\n')
1475 newline = strchr(n_buf, '\n');
1479 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1480 if (buf_color == NULL)
1483 error = buf_puts(&newsize, diffbuf, buf_color);
1487 error = buf_puts(&newsize, diffbuf, div_end);
1492 if (buf_len(diffbuf) > 0) {
1493 error = buf_putc(diffbuf, '\0');
1494 diff_html = strdup(buf_get(diffbuf));
1513 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1516 char *owner = NULL, *d_file = NULL;
1517 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1518 char *comp, *pos, *buf;
1521 if (gw_trans->gw_conf->got_show_repo_owner == false)
1524 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1527 if ((f = fopen(d_file, "r")) == NULL)
1530 if ((buf = calloc(128, sizeof(char *))) == NULL)
1533 while ((fgets(buf, 128, f)) != NULL) {
1534 if ((pos = strstr(buf, gotweb)) != NULL)
1537 if ((pos = strstr(buf, gitweb)) != NULL)
1546 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1551 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1554 for (i = 0; i < 2; i++) {
1555 owner = strsep(&buf, "\"");
1565 if ((asprintf(&owner, "%s", "")) == -1)
1572 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1575 char *url = NULL, *d_file = NULL;
1578 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1581 if ((f = fopen(d_file, "r")) == NULL)
1584 fseek(f, 0, SEEK_END);
1586 fseek(f, 0, SEEK_SET);
1588 if ((url = calloc(len, sizeof(char *))) == NULL)
1591 fread(url, 1, len, f);
1598 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
1600 const struct got_error *error = NULL;
1601 struct got_repository *repo = NULL;
1602 struct got_reflist_head refs;
1603 struct got_reflist_entry *re;
1604 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1607 struct buf *diffbuf = NULL;
1610 error = buf_alloc(&diffbuf, 0);
1613 SIMPLEQ_INIT(&refs);
1615 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1619 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1623 SIMPLEQ_FOREACH(re, &refs, entry) {
1624 const char *refname;
1625 char *refstr, *tag_commit0, *tag_commit, *id_str;
1627 struct got_object_id *id;
1628 struct got_tag_object *tag;
1630 refname = got_ref_get_name(re->ref);
1631 if (strncmp(refname, "refs/tags/", 10) != 0)
1634 refstr = got_ref_to_str(re->ref);
1635 if (refstr == NULL) {
1636 error = got_error_from_errno("got_ref_to_str");
1640 error = got_ref_resolve(&id, repo, re->ref);
1643 error = got_object_open_as_tag(&tag, repo, id);
1648 tagger_time = got_object_tag_get_tagger_time(tag);
1650 error = got_object_id_str(&id_str,
1651 got_object_tag_get_object_id(tag));
1655 tag_commit0 = strdup(got_object_tag_get_message(tag));
1657 if (tag_commit0 == NULL) {
1658 error = got_error_from_errno("strdup");
1662 tag_commit = tag_commit0;
1663 while (*tag_commit == '\n')
1668 newline = strchr(tag_commit, '\n');
1672 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1674 error = got_error_from_errno("asprintf");
1678 if ((asprintf(&tags_navs_disp, tags_navs,
1679 gw_trans->repo_name, id_str, gw_trans->repo_name,
1680 id_str, gw_trans->repo_name, id_str,
1681 gw_trans->repo_name, id_str)) == -1) {
1682 error = got_error_from_errno("asprintf");
1686 if ((asprintf(&tag_row, tags_row, age, refname,
1687 tag_commit, tags_navs_disp)) == -1) {
1688 error = got_error_from_errno("asprintf");
1692 free(tags_navs_disp);
1700 got_object_tag_close(tag);
1702 error = buf_puts(&newsize, diffbuf, tag_row);
1710 if (error || (limit && --limit == 0))
1714 if (buf_len(diffbuf) > 0) {
1715 error = buf_putc(diffbuf, '\0');
1716 tags = strdup(buf_get(diffbuf));
1720 got_ref_list_free(&refs);
1722 got_repo_close(repo);
1730 gw_free_headers(struct gw_header *header)
1734 if (header->commit != NULL)
1735 got_object_commit_close(header->commit);
1737 got_repo_close(header->repo);
1738 free(header->refs_str);
1739 free(header->commit_id);
1740 free(header->parent_id);
1741 free(header->tree_id);
1742 free(header->author);
1743 free(header->committer);
1744 free(header->commit_msg);
1747 static struct gw_header *
1750 struct gw_header *header;
1752 if ((header = malloc(sizeof(*header))) == NULL)
1755 header->repo = NULL;
1756 header->commit = NULL;
1758 header->path = NULL;
1763 static const struct got_error *
1764 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1767 const struct got_error *error = NULL;
1768 struct got_commit_graph *graph = NULL;
1770 error = got_commit_graph_open(&graph, header->path, 0);
1774 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1780 error = got_commit_graph_iter_next(&header->id, graph,
1781 header->repo, NULL, NULL);
1783 if (error->code == GOT_ERR_ITER_COMPLETED)
1787 if (header->id == NULL)
1790 error = got_object_open_as_commit(&header->commit, header->repo,
1795 error = gw_get_commit(gw_trans, header);
1797 struct gw_header *n_header = NULL;
1798 if ((n_header = gw_init_header()) == NULL)
1799 error = got_error_from_errno("malloc");
1801 n_header->refs_str = strdup(header->refs_str);
1802 n_header->commit_id = strdup(header->commit_id);
1803 n_header->parent_id = strdup(header->parent_id);
1804 n_header->tree_id = strdup(header->tree_id);
1805 n_header->author = strdup(header->author);
1806 n_header->committer = strdup(header->committer);
1807 n_header->commit_msg = strdup(header->commit_msg);
1808 n_header->committer_time = header->committer_time;
1809 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
1812 if (error || (limit && --limit == 0))
1817 got_commit_graph_close(graph);
1821 static const struct got_error *
1822 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
1824 const struct got_error *error = NULL;
1825 struct got_reflist_entry *re;
1826 struct got_object_id *id2 = NULL;
1827 struct got_object_qid *parent_id;
1828 char *refs_str = NULL,
1829 *commit_msg = NULL, *commit_msg0;
1832 SIMPLEQ_FOREACH(re, &header->refs, entry) {
1835 struct got_tag_object *tag = NULL;
1838 name = got_ref_get_name(re->ref);
1839 if (strcmp(name, GOT_REF_HEAD) == 0)
1841 if (strncmp(name, "refs/", 5) == 0)
1843 if (strncmp(name, "got/", 4) == 0)
1845 if (strncmp(name, "heads/", 6) == 0)
1847 if (strncmp(name, "remotes/", 8) == 0)
1849 if (strncmp(name, "tags/", 5) == 0) {
1850 error = got_object_open_as_tag(&tag, header->repo,
1853 if (error->code != GOT_ERR_OBJ_TYPE)
1856 * Ref points at something other
1863 cmp = got_object_id_cmp(tag ?
1864 got_object_tag_get_object_id(tag) : re->id, header->id);
1866 got_object_tag_close(tag);
1870 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1871 s ? ", " : "", name)) == -1) {
1872 error = got_error_from_errno("asprintf");
1876 header->refs_str = strdup(refs_str);
1880 if (refs_str == NULL)
1881 header->refs_str = strdup("");
1884 error = got_object_id_str(&header->commit_id, header->id);
1888 error = got_object_id_str(&header->tree_id,
1889 got_object_commit_get_tree_id(header->commit));
1893 if (gw_trans->action == GW_DIFF) {
1894 parent_id = SIMPLEQ_FIRST(
1895 got_object_commit_get_parent_ids(header->commit));
1896 if (parent_id != NULL) {
1897 id2 = got_object_id_dup(parent_id->id);
1899 error = got_object_id_str(&header->parent_id, id2);
1904 header->parent_id = strdup("/dev/null");
1906 header->parent_id = strdup("");
1908 header->committer_time =
1909 got_object_commit_get_committer_time(header->commit);
1910 header->author = strdup(got_object_commit_get_author(header->commit));
1912 strdup(got_object_commit_get_committer(header->commit));
1914 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
1918 commit_msg = commit_msg0;
1919 while (*commit_msg == '\n')
1922 header->commit_msg = strdup(commit_msg);
1927 static const struct got_error *
1928 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
1930 const struct got_error *error = NULL;
1931 char *in_repo_path = NULL;
1933 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1937 SIMPLEQ_INIT(&header->refs);
1939 if (gw_trans->commit == NULL) {
1940 struct got_reference *head_ref;
1941 error = got_ref_open(&head_ref, header->repo,
1942 gw_trans->headref, 0);
1946 error = got_ref_resolve(&header->id, header->repo, head_ref);
1947 got_ref_close(head_ref);
1951 error = got_object_open_as_commit(&header->commit,
1952 header->repo, header->id);
1954 struct got_reference *ref;
1955 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
1956 if (error == NULL) {
1958 error = got_ref_resolve(&header->id, header->repo, ref);
1962 error = got_object_get_type(&obj_type, header->repo,
1966 if (obj_type == GOT_OBJ_TYPE_TAG) {
1967 struct got_tag_object *tag;
1968 error = got_object_open_as_tag(&tag,
1969 header->repo, header->id);
1972 if (got_object_tag_get_object_type(tag) !=
1973 GOT_OBJ_TYPE_COMMIT) {
1974 got_object_tag_close(tag);
1975 error = got_error(GOT_ERR_OBJ_TYPE);
1979 header->id = got_object_id_dup(
1980 got_object_tag_get_object_id(tag));
1981 if (header->id == NULL)
1982 error = got_error_from_errno(
1983 "got_object_id_dup");
1984 got_object_tag_close(tag);
1987 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1988 error = got_error(GOT_ERR_OBJ_TYPE);
1991 error = got_object_open_as_commit(&header->commit,
1992 header->repo, header->id);
1996 if (header->commit == NULL) {
1997 error = got_repo_match_object_id_prefix(&header->id,
1998 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2003 error = got_repo_match_object_id_prefix(&header->id,
2004 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2008 error = got_repo_map_path(&in_repo_path, header->repo,
2009 gw_trans->repo_path, 1);
2014 header->path = strdup(in_repo_path);
2018 error = got_ref_list(&header->refs, header->repo, NULL,
2019 got_ref_cmp_by_name, NULL);
2023 error = gw_get_commits(gw_trans, header, limit);
2031 char datebuf[11]; /* YYYY-MM-DD + NUL */
2034 struct gw_blame_cb_args {
2035 struct blame_line *lines;
2039 off_t *line_offsets;
2041 struct got_repository *repo;
2042 struct gw_trans *gw_trans;
2043 struct buf *blamebuf;
2046 static const struct got_error *
2047 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2049 const struct got_error *err = NULL;
2050 struct gw_blame_cb_args *a = arg;
2051 struct blame_line *bline;
2053 size_t linesize = 0, newsize;
2054 struct got_commit_object *commit = NULL;
2057 time_t committer_time;
2059 if (nlines != a->nlines ||
2060 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2061 return got_error(GOT_ERR_RANGE);
2064 return NULL; /* no change in this commit */
2066 /* Annotate this line. */
2067 bline = &a->lines[lineno - 1];
2068 if (bline->annotated)
2070 err = got_object_id_str(&bline->id_str, id);
2074 err = got_object_open_as_commit(&commit, a->repo, id);
2078 bline->committer = strdup(got_object_commit_get_committer(commit));
2079 if (bline->committer == NULL) {
2080 err = got_error_from_errno("strdup");
2084 committer_time = got_object_commit_get_committer_time(commit);
2085 if (localtime_r(&committer_time, &tm) == NULL)
2086 return got_error_from_errno("localtime_r");
2087 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2088 &tm) >= sizeof(bline->datebuf)) {
2089 err = got_error(GOT_ERR_NO_SPACE);
2092 bline->annotated = 1;
2094 /* Print lines annotated so far. */
2095 bline = &a->lines[a->lineno_cur - 1];
2096 if (!bline->annotated)
2099 offset = a->line_offsets[a->lineno_cur - 1];
2100 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2101 err = got_error_from_errno("fseeko");
2105 while (bline->annotated) {
2106 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2107 *line_escape = NULL;
2110 if (getline(&line, &linesize, a->f) == -1) {
2112 err = got_error_from_errno("getline");
2116 committer = bline->committer;
2117 smallerthan = strchr(committer, '<');
2118 if (smallerthan && smallerthan[1] != '\0')
2119 committer = smallerthan + 1;
2120 at = strchr(committer, '@');
2123 len = strlen(committer);
2125 committer[8] = '\0';
2127 nl = strchr(line, '\n');
2131 if (strcmp(line, "") != 0)
2132 line_escape = strdup(gw_html_escape(line));
2134 line_escape = strdup("");
2136 asprintf(&blame_row, blame_line, a->nlines_prec,
2137 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2140 err = buf_puts(&newsize, a->blamebuf, blame_row);
2144 bline = &a->lines[a->lineno_cur - 1];
2150 got_object_commit_close(commit);
2156 gw_get_file_blame(struct gw_trans *gw_trans)
2158 const struct got_error *error = NULL;
2159 struct got_repository *repo = NULL;
2160 struct got_object_id *obj_id = NULL;
2161 struct got_object_id *commit_id = NULL;
2162 struct got_blob_object *blob = NULL;
2163 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2165 struct gw_blame_cb_args bca;
2169 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2173 if (gw_trans->repo_folder != NULL) {
2174 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2175 error = got_error_from_errno("asprintf");
2179 folder = strdup("");
2181 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2182 error = got_error_from_errno("asprintf");
2187 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2191 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2192 GOT_OBJ_TYPE_COMMIT, 1, repo);
2196 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2200 if (obj_id == NULL) {
2201 error = got_error(GOT_ERR_NO_OBJ);
2205 error = got_object_get_type(&obj_type, repo, obj_id);
2209 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2210 error = got_error(GOT_ERR_OBJ_TYPE);
2214 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2218 error = buf_alloc(&bca.blamebuf, 0);
2222 bca.f = got_opentemp();
2223 if (bca.f == NULL) {
2224 error = got_error_from_errno("got_opentemp");
2227 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2228 &bca.line_offsets, bca.f, blob);
2229 if (error || bca.nlines == 0)
2232 /* Don't include \n at EOF in the blame line count. */
2233 if (bca.line_offsets[bca.nlines - 1] == filesize)
2236 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2237 if (bca.lines == NULL) {
2238 error = got_error_from_errno("calloc");
2242 bca.nlines_prec = 0;
2249 bca.gw_trans = gw_trans;
2251 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2253 if (buf_len(bca.blamebuf) > 0) {
2254 error = buf_putc(bca.blamebuf, '\0');
2255 blame_html = strdup(buf_get(bca.blamebuf));
2265 error = got_object_blob_close(blob);
2267 error = got_repo_close(repo);
2271 for (i = 0; i < bca.nlines; i++) {
2272 struct blame_line *bline = &bca.lines[i];
2273 free(bline->id_str);
2274 free(bline->committer);
2278 free(bca.line_offsets);
2279 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2280 error = got_error_from_errno("fclose");
2288 gw_get_repo_tree(struct gw_trans *gw_trans)
2290 const struct got_error *error = NULL;
2291 struct got_repository *repo = NULL;
2292 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2293 struct got_tree_object *tree = NULL;
2294 struct buf *diffbuf = NULL;
2296 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2297 *tree_row = NULL, *id_str;
2300 error = buf_alloc(&diffbuf, 0);
2304 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2308 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2312 if (gw_trans->repo_folder != NULL)
2313 path = strdup(gw_trans->repo_folder);
2314 else if (in_repo_path) {
2316 path = in_repo_path;
2319 if (gw_trans->commit == NULL) {
2320 struct got_reference *head_ref;
2321 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2325 error = got_ref_resolve(&commit_id, repo, head_ref);
2326 got_ref_close(head_ref);
2329 error = got_repo_match_object_id(&commit_id, NULL,
2330 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2334 error = got_object_id_str(&gw_trans->commit, commit_id);
2338 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2342 error = got_object_open_as_tree(&tree, repo, tree_id);
2346 nentries = got_object_tree_get_nentries(tree);
2348 for (i = 0; i < nentries; i++) {
2349 struct got_tree_entry *te;
2350 const char *modestr = "";
2351 char *id = NULL, *url_html = NULL;
2353 te = got_object_tree_get_entry(tree, i);
2355 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2359 if ((asprintf(&id, "%s", id_str)) == -1) {
2360 error = got_error_from_errno("asprintf");
2365 mode_t mode = got_tree_entry_get_mode(te);
2367 if (got_object_tree_entry_is_submodule(te))
2369 else if (S_ISLNK(mode))
2371 else if (S_ISDIR(mode))
2373 else if (mode & S_IXUSR)
2376 char *build_folder = NULL;
2377 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2378 if (gw_trans->repo_folder != NULL) {
2379 if ((asprintf(&build_folder, "%s/%s",
2380 gw_trans->repo_folder,
2381 got_tree_entry_get_name(te))) == -1) {
2383 got_error_from_errno("asprintf");
2387 if (asprintf(&build_folder, "%s",
2388 got_tree_entry_get_name(te)) == -1)
2392 if ((asprintf(&url_html, folder_html,
2393 gw_trans->repo_name, gw_trans->action_name,
2394 gw_trans->commit, build_folder,
2395 got_tree_entry_get_name(te), modestr)) == -1) {
2396 error = got_error_from_errno("asprintf");
2400 if (gw_trans->repo_folder != NULL) {
2401 if ((asprintf(&build_folder, "%s",
2402 gw_trans->repo_folder)) == -1) {
2404 got_error_from_errno("asprintf");
2408 build_folder = strdup("");
2410 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2411 "blame", gw_trans->commit,
2412 got_tree_entry_get_name(te), build_folder,
2413 got_tree_entry_get_name(te), modestr)) == -1) {
2414 error = got_error_from_errno("asprintf");
2423 if ((asprintf(&tree_row, tree_line, url_html)) == -1) {
2424 error = got_error_from_errno("asprintf");
2427 error = buf_puts(&newsize, diffbuf, tree_row);
2437 if (buf_len(diffbuf) > 0) {
2438 error = buf_putc(diffbuf, '\0');
2439 tree_html = strdup(buf_get(diffbuf));
2443 got_object_tree_close(tree);
2445 got_repo_close(repo);
2457 gw_get_repo_heads(struct gw_trans *gw_trans)
2459 const struct got_error *error = NULL;
2460 struct got_repository *repo = NULL;
2461 struct got_reflist_head refs;
2462 struct got_reflist_entry *re;
2463 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2464 struct buf *diffbuf = NULL;
2467 error = buf_alloc(&diffbuf, 0);
2471 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2475 SIMPLEQ_INIT(&refs);
2476 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2481 SIMPLEQ_FOREACH(re, &refs, entry) {
2484 refname = strdup(got_ref_get_name(re->ref));
2485 if (refname == NULL) {
2486 error = got_error_from_errno("got_ref_to_str");
2490 if (strncmp(refname, "refs/heads/", 11) != 0) {
2495 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2498 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2499 refname, gw_trans->repo_name, refname,
2500 gw_trans->repo_name, refname, gw_trans->repo_name,
2502 error = got_error_from_errno("asprintf");
2506 if (strncmp(refname, "refs/heads/", 11) == 0)
2509 if ((asprintf(&head_row, heads_row, age, refname,
2510 head_navs_disp)) == -1) {
2511 error = got_error_from_errno("asprintf");
2515 error = buf_puts(&newsize, diffbuf, head_row);
2517 free(head_navs_disp);
2521 if (buf_len(diffbuf) > 0) {
2522 error = buf_putc(diffbuf, '\0');
2523 heads = strdup(buf_get(diffbuf));
2527 got_ref_list_free(&refs);
2529 got_repo_close(repo);
2537 gw_get_got_link(struct gw_trans *gw_trans)
2541 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2542 gw_trans->gw_conf->got_logo)) == -1)
2549 gw_get_site_link(struct gw_trans *gw_trans)
2551 char *link, *repo = "", *action = "";
2553 if (gw_trans->repo_name != NULL)
2554 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2555 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2558 if (gw_trans->action_name != NULL)
2559 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2562 if ((asprintf(&link, site_link, GOTWEB,
2563 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2570 gw_colordiff_line(char *buf)
2572 const struct got_error *error = NULL;
2573 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2574 struct buf *diffbuf = NULL;
2577 error = buf_alloc(&diffbuf, 0);
2583 if (strncmp(buf, "-", 1) == 0)
2584 color = "diff_minus";
2585 if (strncmp(buf, "+", 1) == 0)
2586 color = "diff_plus";
2587 if (strncmp(buf, "@@", 2) == 0)
2588 color = "diff_chunk_header";
2589 if (strncmp(buf, "@@", 2) == 0)
2590 color = "diff_chunk_header";
2591 if (strncmp(buf, "commit +", 8) == 0)
2592 color = "diff_meta";
2593 if (strncmp(buf, "commit -", 8) == 0)
2594 color = "diff_meta";
2595 if (strncmp(buf, "blob +", 6) == 0)
2596 color = "diff_meta";
2597 if (strncmp(buf, "blob -", 6) == 0)
2598 color = "diff_meta";
2599 if (strncmp(buf, "file +", 6) == 0)
2600 color = "diff_meta";
2601 if (strncmp(buf, "file -", 6) == 0)
2602 color = "diff_meta";
2603 if (strncmp(buf, "from:", 5) == 0)
2604 color = "diff_author";
2605 if (strncmp(buf, "via:", 4) == 0)
2606 color = "diff_author";
2607 if (strncmp(buf, "date:", 5) == 0)
2608 color = "diff_date";
2610 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2613 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2617 error = buf_puts(&newsize, diffbuf, buf);
2621 if (buf_len(diffbuf) > 0) {
2622 error = buf_putc(diffbuf, '\0');
2623 colorized_line = strdup(buf_get(diffbuf));
2627 free(div_diff_line_div);
2628 return colorized_line;
2632 gw_html_escape(const char *html)
2634 char *escaped_str = NULL, *buf;
2636 size_t sz, i, buff_sz = 2048;
2638 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2644 if ((sz = strlen(html)) == 0)
2647 /* only work with buff_sz */
2651 for (i = 0; i < sz; i++) {
2655 strcat(buf, ">");
2658 strcat(buf, "&");
2661 strcat(buf, "<");
2664 strcat(buf, """);
2667 strcat(buf, "'");
2670 strcat(buf, "<br />");
2676 asprintf(&escaped_str, "%s", buf);
2682 main(int argc, char *argv[])
2684 const struct got_error *error = NULL;
2685 struct gw_trans *gw_trans;
2686 struct gw_dir *dir = NULL, *tdir;
2687 const char *page = "index";
2691 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2694 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2697 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2700 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2703 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2704 if (kerr != KCGI_OK) {
2705 error = gw_kcgi_error(kerr);
2709 if ((gw_trans->gw_conf =
2710 malloc(sizeof(struct gotweb_conf))) == NULL) {
2712 error = got_error_from_errno("malloc");
2716 TAILQ_INIT(&gw_trans->gw_dirs);
2717 TAILQ_INIT(&gw_trans->gw_headers);
2720 gw_trans->repos_total = 0;
2721 gw_trans->repo_path = NULL;
2722 gw_trans->commit = NULL;
2723 gw_trans->headref = strdup(GOT_REF_HEAD);
2724 gw_trans->mime = KMIME_TEXT_HTML;
2725 gw_trans->gw_tmpl->key = gw_templs;
2726 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2727 gw_trans->gw_tmpl->arg = gw_trans;
2728 gw_trans->gw_tmpl->cb = gw_template;
2729 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2733 gw_trans->mime = KMIME_TEXT_PLAIN;
2734 gw_trans->action = GW_ERR;
2735 gw_display_index(gw_trans, error);
2739 error = gw_parse_querystring(gw_trans);
2743 error = gw_display_index(gw_trans, error);
2749 free(gw_trans->gw_conf->got_repos_path);
2750 free(gw_trans->gw_conf->got_www_path);
2751 free(gw_trans->gw_conf->got_site_name);
2752 free(gw_trans->gw_conf->got_site_owner);
2753 free(gw_trans->gw_conf->got_site_link);
2754 free(gw_trans->gw_conf->got_logo);
2755 free(gw_trans->gw_conf->got_logo_url);
2756 free(gw_trans->gw_conf);
2757 free(gw_trans->commit);
2758 free(gw_trans->repo_path);
2759 free(gw_trans->repo_name);
2760 free(gw_trans->repo_file);
2761 free(gw_trans->action_name);
2762 free(gw_trans->headref);
2764 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2766 free(dir->description);
2775 khttp_free(gw_trans->gw_req);
2776 return EXIT_SUCCESS;