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>
33 #include <got_object.h>
34 #include <got_reference.h>
35 #include <got_repository.h>
37 #include <got_cancel.h>
38 #include <got_worktree.h>
40 #include <got_commit_graph.h>
41 #include <got_blame.h>
42 #include <got_privsep.h>
43 #include <got_opentemp.h>
50 #include "gotweb_ui.h"
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct gw_dir *gw_dir;
59 struct gotweb_conf *gw_conf;
60 struct ktemplate *gw_tmpl;
61 struct khtmlreq *gw_html_req;
72 unsigned int repos_total;
88 TAILQ_ENTRY(gw_dir) entry;
128 static const char *const gw_templs[TEMPL__MAX] = {
138 static const struct kvalid gw_keys[KEY__ZMAX] = {
139 { kvalid_stringne, "action" },
140 { kvalid_stringne, "commit" },
141 { kvalid_stringne, "file" },
142 { kvalid_stringne, "folder" },
143 { kvalid_stringne, "headref" },
144 { kvalid_int, "page" },
145 { kvalid_stringne, "path" },
148 int gw_get_repo_log_count(struct gw_trans *,
151 static struct gw_dir *gw_init_gw_dir(char *);
153 static char *gw_get_repo_description(struct gw_trans *,
155 static char *gw_get_repo_owner(struct gw_trans *,
157 static char *gw_get_time_str(time_t, int);
158 static char *gw_get_repo_age(struct gw_trans *,
159 char *, char *, int);
160 static char *gw_get_repo_log(struct gw_trans *,
161 const char *, char *, int, int);
162 static char *gw_get_file_blame(struct gw_trans *, char *);
163 static char *gw_get_repo_tree(struct gw_trans *, char *);
164 static char *gw_get_repo_diff(struct gw_trans *, char *,
166 static char *gw_get_repo_tags(struct gw_trans *, int, int);
167 static char *gw_get_repo_heads(struct gw_trans *);
168 static char *gw_get_clone_url(struct gw_trans *, char *);
169 static char *gw_get_got_link(struct gw_trans *);
170 static char *gw_get_site_link(struct gw_trans *);
171 static char *gw_html_escape(const char *);
172 static char *gw_colordiff_line(char *);
174 static void gw_display_open(struct gw_trans *, enum khttp,
176 static void gw_display_index(struct gw_trans *,
177 const struct got_error *);
179 static int gw_template(size_t, void *);
181 static const struct got_error* gw_apply_unveil(const char *, const char *);
182 static const struct got_error* gw_blame_cb(void *, int, int,
183 struct got_object_id *);
184 static const struct got_error* gw_load_got_paths(struct gw_trans *);
185 static const struct got_error* gw_load_got_path(struct gw_trans *,
187 static const struct got_error* gw_parse_querystring(struct gw_trans *);
188 static const struct got_error* gw_match_logmsg(int *, struct got_object_id *,
189 struct got_commit_object *, regex_t *);
191 static const struct got_error* gw_blame(struct gw_trans *);
192 static const struct got_error* gw_commit(struct gw_trans *);
193 static const struct got_error* gw_commitdiff(struct gw_trans *);
194 static const struct got_error* gw_index(struct gw_trans *);
195 static const struct got_error* gw_log(struct gw_trans *);
196 static const struct got_error* gw_raw(struct gw_trans *);
197 static const struct got_error* gw_logbriefs(struct gw_trans *);
198 static const struct got_error* gw_summary(struct gw_trans *);
199 static const struct got_error* gw_tag(struct gw_trans *);
200 static const struct got_error* gw_tree(struct gw_trans *);
202 struct gw_query_action {
203 unsigned int func_id;
204 const char *func_name;
205 const struct got_error *(*func_main)(struct gw_trans *);
209 enum gw_query_actions {
223 static struct gw_query_action gw_query_funcs[] = {
224 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
225 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
226 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
227 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
228 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
229 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
230 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
231 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
232 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
233 { GW_TAG, "tag", gw_tag, "gw_tmpl/index.tmpl" },
234 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
237 static const struct got_error *
238 gw_apply_unveil(const char *repo_path, const char *repo_file)
240 const struct got_error *err;
242 if (repo_path && repo_file) {
244 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
245 return got_error_from_errno("asprintf unveil");
246 if (unveil(full_path, "r") != 0)
247 return got_error_from_errno2("unveil", full_path);
250 if (repo_path && unveil(repo_path, "r") != 0)
251 return got_error_from_errno2("unveil", repo_path);
253 if (unveil("/tmp", "rwc") != 0)
254 return got_error_from_errno2("unveil", "/tmp");
256 err = got_privsep_unveil_exec_helpers();
260 if (unveil(NULL, NULL) != 0)
261 return got_error_from_errno("unveil");
267 gw_get_repo_log_count(struct gw_trans *gw_trans, char *start_commit)
269 const struct got_error *error;
270 struct got_repository *repo = NULL;
271 struct got_reflist_head refs;
272 struct got_commit_object *commit = NULL;
273 struct got_object_id *id = NULL;
274 struct got_commit_graph *graph = NULL;
275 char *in_repo_path = NULL, *path = NULL;
278 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
284 if (start_commit == NULL) {
285 struct got_reference *head_ref;
286 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
290 error = got_ref_resolve(&id, repo, head_ref);
291 got_ref_close(head_ref);
295 error = got_object_open_as_commit(&commit, repo, id);
297 struct got_reference *ref;
298 error = got_ref_open(&ref, repo, start_commit, 0);
301 error = got_ref_resolve(&id, repo, ref);
305 error = got_object_get_type(&obj_type, repo, id);
308 if (obj_type == GOT_OBJ_TYPE_TAG) {
309 struct got_tag_object *tag;
310 error = got_object_open_as_tag(&tag, repo, id);
313 if (got_object_tag_get_object_type(tag) !=
314 GOT_OBJ_TYPE_COMMIT) {
315 got_object_tag_close(tag);
316 error = got_error(GOT_ERR_OBJ_TYPE);
320 id = got_object_id_dup(
321 got_object_tag_get_object_id(tag));
323 error = got_error_from_errno(
324 "got_object_id_dup");
325 got_object_tag_close(tag);
328 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
329 error = got_error(GOT_ERR_OBJ_TYPE);
332 error = got_object_open_as_commit(&commit, repo, id);
336 if (commit == NULL) {
337 error = got_repo_match_object_id_prefix(&id,
338 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
342 error = got_repo_match_object_id_prefix(&id,
343 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
348 error = got_object_open_as_commit(&commit, repo, id);
352 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
361 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
365 error = got_commit_graph_open(&graph, path, 0);
369 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
374 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
377 if (error->code == GOT_ERR_ITER_COMPLETED)
391 got_commit_graph_close(graph);
393 error = got_repo_close(repo);
398 khttp_puts(gw_trans->gw_req, "Error: ");
399 khttp_puts(gw_trans->gw_req, error->msg);
405 static const struct got_error *
406 gw_blame(struct gw_trans *gw_trans)
408 const struct got_error *error = NULL;
410 char *log, *log_html;
412 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
416 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGBLAME);
418 if (log != NULL && strcmp(log, "") != 0) {
419 if ((asprintf(&log_html, log_blame, log)) == -1)
420 return got_error_from_errno("asprintf");
421 khttp_puts(gw_trans->gw_req, log_html);
428 static const struct got_error *
429 gw_commit(struct gw_trans *gw_trans)
431 const struct got_error *error = NULL;
432 char *log, *log_html;
434 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
438 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
440 if (log != NULL && strcmp(log, "") != 0) {
441 if ((asprintf(&log_html, log_commit, log)) == -1)
442 return got_error_from_errno("asprintf");
443 khttp_puts(gw_trans->gw_req, log_html);
450 static const struct got_error *
451 gw_commitdiff(struct gw_trans *gw_trans)
453 const struct got_error *error = NULL;
454 char *log, *log_html;
456 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
460 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
462 if (log != NULL && strcmp(log, "") != 0) {
463 if ((asprintf(&log_html, log_diff, log)) == -1)
464 return got_error_from_errno("asprintf");
465 khttp_puts(gw_trans->gw_req, log_html);
472 static const struct got_error *
473 gw_index(struct gw_trans *gw_trans)
475 const struct got_error *error = NULL;
476 struct gw_dir *gw_dir = NULL;
477 char *html, *navs, *next, *prev;
478 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
480 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
484 error = gw_load_got_paths(gw_trans);
488 khttp_puts(gw_trans->gw_req, index_projects_header);
490 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
493 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
494 if (gw_trans->page > 0 && (gw_trans->page *
495 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
501 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
502 gw_dir->name, gw_dir->name)) == -1)
503 return got_error_from_errno("asprintf");
505 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
506 gw_dir->description, gw_dir->owner, gw_dir->age,
508 return got_error_from_errno("asprintf");
510 khttp_puts(gw_trans->gw_req, html);
515 if (gw_trans->gw_conf->got_max_repos_display == 0)
518 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
519 khttp_puts(gw_trans->gw_req, np_wrapper_start);
520 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
521 (gw_trans->page > 0) &&
522 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
523 prev_disp == gw_trans->repos_total))
524 khttp_puts(gw_trans->gw_req, np_wrapper_start);
526 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
527 (gw_trans->page > 0) &&
528 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
529 prev_disp == gw_trans->repos_total)) {
530 if ((asprintf(&prev, nav_prev,
531 gw_trans->page - 1)) == -1)
532 return got_error_from_errno("asprintf");
533 khttp_puts(gw_trans->gw_req, prev);
537 khttp_puts(gw_trans->gw_req, div_end);
539 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
540 next_disp == gw_trans->gw_conf->got_max_repos_display &&
541 dir_c != (gw_trans->page + 1) *
542 gw_trans->gw_conf->got_max_repos_display) {
543 if ((asprintf(&next, nav_next,
544 gw_trans->page + 1)) == -1)
545 return got_error_from_errno("calloc");
546 khttp_puts(gw_trans->gw_req, next);
547 khttp_puts(gw_trans->gw_req, div_end);
553 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
554 (gw_trans->page > 0) &&
555 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
556 prev_disp == gw_trans->repos_total))
557 khttp_puts(gw_trans->gw_req, div_end);
564 static const struct got_error *
565 gw_log(struct gw_trans *gw_trans)
567 const struct got_error *error = NULL;
568 char *log, *log_html;
570 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
574 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
575 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
577 if (log != NULL && strcmp(log, "") != 0) {
578 if ((asprintf(&log_html, logs, log)) == -1)
579 return got_error_from_errno("asprintf");
580 khttp_puts(gw_trans->gw_req, log_html);
587 static const struct got_error *
588 gw_raw(struct gw_trans *gw_trans)
590 const struct got_error *error = NULL;
595 static const struct got_error *
596 gw_logbriefs(struct gw_trans *gw_trans)
598 const struct got_error *error = NULL;
599 char *log, *log_html;
601 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
605 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
606 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
608 if (log != NULL && strcmp(log, "") != 0) {
609 if ((asprintf(&log_html, summary_logbriefs,
611 return got_error_from_errno("asprintf");
612 khttp_puts(gw_trans->gw_req, log_html);
619 static const struct got_error *
620 gw_summary(struct gw_trans *gw_trans)
622 const struct got_error *error = NULL;
623 char *description_html, *repo_owner_html, *repo_age_html,
624 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
627 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
631 khttp_puts(gw_trans->gw_req, summary_wrapper);
632 if (gw_trans->gw_conf->got_show_repo_description) {
633 if (gw_trans->gw_dir->description != NULL &&
634 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
635 if ((asprintf(&description_html, description,
636 gw_trans->gw_dir->description)) == -1)
637 return got_error_from_errno("asprintf");
639 khttp_puts(gw_trans->gw_req, description_html);
640 free(description_html);
644 if (gw_trans->gw_conf->got_show_repo_owner) {
645 if (gw_trans->gw_dir->owner != NULL &&
646 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
647 if ((asprintf(&repo_owner_html, repo_owner,
648 gw_trans->gw_dir->owner)) == -1)
649 return got_error_from_errno("asprintf");
651 khttp_puts(gw_trans->gw_req, repo_owner_html);
652 free(repo_owner_html);
656 if (gw_trans->gw_conf->got_show_repo_age) {
657 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
658 "refs/heads", TM_LONG);
659 if (age != NULL && (strcmp(age, "") != 0)) {
660 if ((asprintf(&repo_age_html, last_change, age)) == -1)
661 return got_error_from_errno("asprintf");
663 khttp_puts(gw_trans->gw_req, repo_age_html);
669 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
670 if (gw_trans->gw_dir->url != NULL &&
671 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
672 if ((asprintf(&cloneurl_html, cloneurl,
673 gw_trans->gw_dir->url)) == -1)
674 return got_error_from_errno("asprintf");
676 khttp_puts(gw_trans->gw_req, cloneurl_html);
680 khttp_puts(gw_trans->gw_req, div_end);
682 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
683 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
684 heads = gw_get_repo_heads(gw_trans);
686 if (log != NULL && strcmp(log, "") != 0) {
687 if ((asprintf(&log_html, summary_logbriefs,
689 return got_error_from_errno("asprintf");
690 khttp_puts(gw_trans->gw_req, log_html);
695 if (tags != NULL && strcmp(tags, "") != 0) {
696 if ((asprintf(&tags_html, summary_tags,
698 return got_error_from_errno("asprintf");
699 khttp_puts(gw_trans->gw_req, tags_html);
704 if (heads != NULL && strcmp(heads, "") != 0) {
705 if ((asprintf(&heads_html, summary_heads,
707 return got_error_from_errno("asprintf");
708 khttp_puts(gw_trans->gw_req, heads_html);
715 static const struct got_error *
716 gw_tag(struct gw_trans *gw_trans)
718 const struct got_error *error = NULL;
719 char *log, *log_html;
721 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
725 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTAG);
727 if (log != NULL && strcmp(log, "") != 0) {
728 if ((asprintf(&log_html, log_tag, log)) == -1)
729 return got_error_from_errno("asprintf");
730 khttp_puts(gw_trans->gw_req, log_html);
737 static const struct got_error *
738 gw_tree(struct gw_trans *gw_trans)
740 const struct got_error *error = NULL;
741 char *log, *log_html;
743 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
747 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
749 if (log != NULL && strcmp(log, "") != 0) {
750 if ((asprintf(&log_html, log_tree, log)) == -1)
751 return got_error_from_errno("asprintf");
752 khttp_puts(gw_trans->gw_req, log_html);
759 static const struct got_error *
760 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
762 const struct got_error *error = NULL;
767 if ((asprintf(&dir_test, "%s/%s/%s",
768 gw_trans->gw_conf->got_repos_path, gw_dir->name,
769 GOTWEB_GIT_DIR)) == -1)
770 return got_error_from_errno("asprintf");
772 dt = opendir(dir_test);
776 gw_dir->path = strdup(dir_test);
781 if ((asprintf(&dir_test, "%s/%s/%s",
782 gw_trans->gw_conf->got_repos_path, gw_dir->name,
783 GOTWEB_GOT_DIR)) == -1)
784 return got_error_from_errno("asprintf");
786 dt = opendir(dir_test);
791 error = got_error(GOT_ERR_NOT_GIT_REPO);
795 if ((asprintf(&dir_test, "%s/%s",
796 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
797 return got_error_from_errno("asprintf");
799 gw_dir->path = strdup(dir_test);
802 gw_dir->description = gw_get_repo_description(gw_trans,
804 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
805 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
807 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
816 static const struct got_error *
817 gw_load_got_paths(struct gw_trans *gw_trans)
819 const struct got_error *error = NULL;
821 struct dirent **sd_dent;
822 struct gw_dir *gw_dir;
824 unsigned int d_cnt, d_i;
826 d = opendir(gw_trans->gw_conf->got_repos_path);
828 error = got_error_from_errno2("opendir",
829 gw_trans->gw_conf->got_repos_path);
833 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
836 error = got_error_from_errno2("scandir",
837 gw_trans->gw_conf->got_repos_path);
841 for (d_i = 0; d_i < d_cnt; d_i++) {
842 if (gw_trans->gw_conf->got_max_repos > 0 &&
843 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
844 break; /* account for parent and self */
846 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
847 strcmp(sd_dent[d_i]->d_name, "..") == 0)
850 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
851 return got_error_from_errno("gw_dir malloc");
853 error = gw_load_got_path(gw_trans, gw_dir);
854 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
859 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
860 !got_path_dir_is_empty(gw_dir->path)) {
861 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
863 gw_trans->repos_total++;
871 static const struct got_error *
872 gw_parse_querystring(struct gw_trans *gw_trans)
874 const struct got_error *error = NULL;
876 struct gw_query_action *action = NULL;
879 if (gw_trans->gw_req->fieldnmap[0]) {
880 error = got_error_from_errno("bad parse");
882 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
883 /* define gw_trans->repo_path */
884 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
885 return got_error_from_errno("asprintf");
887 if ((asprintf(&gw_trans->repo_path, "%s/%s",
888 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
889 return got_error_from_errno("asprintf");
891 /* get action and set function */
892 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
893 for (i = 0; i < nitems(gw_query_funcs); i++) {
894 action = &gw_query_funcs[i];
895 if (action->func_name == NULL)
898 if (strcmp(action->func_name,
900 gw_trans->action = i;
901 if ((asprintf(&gw_trans->action_name,
902 "%s", action->func_name)) == -1)
904 got_error_from_errno(
913 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
914 if ((asprintf(&gw_trans->commit, "%s",
916 return got_error_from_errno("asprintf");
918 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
919 if ((asprintf(&gw_trans->repo_file, "%s",
921 return got_error_from_errno("asprintf");
923 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
924 if ((asprintf(&gw_trans->repo_folder, "%s",
926 return got_error_from_errno("asprintf");
928 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
929 if ((asprintf(&gw_trans->headref, "%s",
931 return got_error_from_errno("asprintf");
933 if (action == NULL) {
934 error = got_error_from_errno("invalid action");
937 if ((gw_trans->gw_dir =
938 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
939 return got_error_from_errno("gw_dir malloc");
941 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
945 gw_trans->action = GW_INDEX;
947 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
948 gw_trans->page = p->parsed.i;
950 if (gw_trans->action == GW_RAW)
951 gw_trans->mime = KMIME_TEXT_PLAIN;
956 static struct gw_dir *
957 gw_init_gw_dir(char *dir)
959 struct gw_dir *gw_dir;
961 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
964 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
970 static const struct got_error*
971 gw_match_logmsg(int *have_match, struct got_object_id *id,
972 struct got_commit_object *commit, regex_t *regex)
974 const struct got_error *err = NULL;
976 char *id_str = NULL, *logmsg = NULL;
980 err = got_object_id_str(&id_str, id);
984 err = got_object_commit_get_logmsg(&logmsg, commit);
988 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
997 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
999 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1000 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1002 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1004 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1005 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1006 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1007 khttp_body(gw_trans->gw_req);
1011 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
1013 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1014 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1017 khttp_puts(gw_trans->gw_req, err->msg);
1019 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1020 gw_query_funcs[gw_trans->action].template);
1022 khtml_close(gw_trans->gw_html_req);
1026 gw_template(size_t key, void *arg)
1028 const struct got_error *error = NULL;
1029 struct gw_trans *gw_trans = arg;
1030 char *gw_got_link, *gw_site_link;
1031 char *site_owner_name, *site_owner_name_h;
1035 khttp_puts(gw_trans->gw_req, head);
1038 gw_got_link = gw_get_got_link(gw_trans);
1039 if (gw_got_link != NULL)
1040 khttp_puts(gw_trans->gw_req, gw_got_link);
1044 case (TEMPL_SITEPATH):
1045 gw_site_link = gw_get_site_link(gw_trans);
1046 if (gw_site_link != NULL)
1047 khttp_puts(gw_trans->gw_req, gw_site_link);
1052 if (gw_trans->gw_conf->got_site_name != NULL)
1053 khtml_puts(gw_trans->gw_html_req,
1054 gw_trans->gw_conf->got_site_name);
1057 case (TEMPL_SEARCH):
1058 khttp_puts(gw_trans->gw_req, search);
1060 case(TEMPL_SITEOWNER):
1061 if (gw_trans->gw_conf->got_site_owner != NULL &&
1062 gw_trans->gw_conf->got_show_site_owner) {
1064 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1065 if ((asprintf(&site_owner_name_h, site_owner,
1070 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1071 free(site_owner_name);
1072 free(site_owner_name_h);
1075 case(TEMPL_CONTENT):
1076 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1078 khttp_puts(gw_trans->gw_req, error->msg);
1089 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1092 char *description = NULL, *d_file = NULL;
1095 if (gw_trans->gw_conf->got_show_repo_description == false)
1098 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1101 if ((f = fopen(d_file, "r")) == NULL)
1104 fseek(f, 0, SEEK_END);
1106 fseek(f, 0, SEEK_SET);
1107 if ((description = calloc(len, sizeof(char *))) == NULL)
1110 fread(description, 1, len, f);
1115 if ((asprintf(&description, "%s", "")) == -1)
1122 gw_get_time_str(time_t committer_time, int ref_tm)
1126 char *years = "years ago", *months = "months ago";
1127 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1128 char *minutes = "minutes ago", *seconds = "seconds ago";
1129 char *now = "right now";
1135 diff_time = time(NULL) - committer_time;
1136 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1137 if ((asprintf(&repo_age, "%lld %s",
1138 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1140 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1141 if ((asprintf(&repo_age, "%lld %s",
1142 (diff_time / 60 / 60 / 24 / (365 / 12)),
1145 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1146 if ((asprintf(&repo_age, "%lld %s",
1147 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1149 } else if (diff_time > 60 * 60 * 24 * 2) {
1150 if ((asprintf(&repo_age, "%lld %s",
1151 (diff_time / 60 / 60 / 24), days)) == -1)
1153 } else if (diff_time > 60 * 60 * 2) {
1154 if ((asprintf(&repo_age, "%lld %s",
1155 (diff_time / 60 / 60), hours)) == -1)
1157 } else if (diff_time > 60 * 2) {
1158 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1161 } else if (diff_time > 2) {
1162 if ((asprintf(&repo_age, "%lld %s", diff_time,
1166 if ((asprintf(&repo_age, "%s", now)) == -1)
1171 if (gmtime_r(&committer_time, &tm) == NULL)
1174 s = asctime_r(&tm, datebuf);
1178 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1186 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1189 const struct got_error *error = NULL;
1190 struct got_object_id *id = NULL;
1191 struct got_repository *repo = NULL;
1192 struct got_commit_object *commit = NULL;
1193 struct got_reflist_head refs;
1194 struct got_reflist_entry *re;
1195 struct got_reference *head_ref;
1197 time_t committer_time = 0, cmp_time = 0;
1198 const char *refname;
1199 char *repo_age = NULL;
1201 if (repo_ref == NULL)
1204 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1207 SIMPLEQ_INIT(&refs);
1208 if (gw_trans->gw_conf->got_show_repo_age == false) {
1209 if ((asprintf(&repo_age, "")) == -1)
1214 error = got_repo_open(&repo, dir, NULL);
1219 error = got_ref_list(&refs, repo, "refs/heads",
1220 got_ref_cmp_by_name, NULL);
1222 error = got_ref_list(&refs, repo, repo_ref,
1223 got_ref_cmp_by_name, NULL);
1227 SIMPLEQ_FOREACH(re, &refs, entry) {
1229 refname = strdup(repo_ref);
1231 refname = got_ref_get_name(re->ref);
1232 error = got_ref_open(&head_ref, repo, refname, 0);
1236 error = got_ref_resolve(&id, repo, head_ref);
1237 got_ref_close(head_ref);
1241 error = got_object_open_as_commit(&commit, repo, id);
1246 got_object_commit_get_committer_time(commit);
1248 if (cmp_time < committer_time)
1249 cmp_time = committer_time;
1252 if (cmp_time != 0) {
1253 committer_time = cmp_time;
1254 repo_age = gw_get_time_str(committer_time, ref_tm);
1256 if ((asprintf(&repo_age, "")) == -1)
1258 got_ref_list_free(&refs);
1262 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1269 gw_get_repo_diff(struct gw_trans *gw_trans, char *id_str1, char *id_str2)
1271 const struct got_error *error;
1273 struct got_object_id *id1 = NULL, *id2 = NULL;
1274 struct got_repository *repo = NULL;
1275 struct buf *diffbuf = NULL;
1276 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1285 error = buf_alloc(&diffbuf, 0);
1289 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1293 error = got_repo_match_object_id(&id1, &label1, id_str1,
1294 GOT_OBJ_TYPE_ANY, 1, repo);
1299 error = got_repo_match_object_id(&id2, &label2, id_str2,
1300 GOT_OBJ_TYPE_ANY, 1, repo);
1304 error = got_object_get_type(&type2, repo, id2);
1309 error = got_object_get_type(&type1, repo, id1);
1313 if (id_str2 && type1 != type2) {
1314 error = got_error(GOT_ERR_OBJ_TYPE);
1319 case GOT_OBJ_TYPE_BLOB:
1320 error = got_diff_objects_as_blobs(id2, id1, NULL, NULL, 3, 0,
1323 case GOT_OBJ_TYPE_TREE:
1324 error = got_diff_objects_as_trees(id2, id1, "", "", 3, 0, repo,
1327 case GOT_OBJ_TYPE_COMMIT:
1328 error = got_diff_objects_as_commits(id2, id1, 3, 0, repo, f);
1331 error = got_error(GOT_ERR_OBJ_TYPE);
1334 if ((buf = calloc(128, sizeof(char *))) == NULL)
1337 fseek(f, 0, SEEK_SET);
1339 while ((fgets(buf, 128, f)) != NULL) {
1340 buf_color = gw_colordiff_line(buf);
1341 error = buf_puts(&newsize, diffbuf, buf_color);
1345 error = buf_puts(&newsize, diffbuf, div_end);
1350 if (buf_len(diffbuf) > 0) {
1351 error = buf_putc(diffbuf, '\0');
1352 diff_html = strdup(buf_get(diffbuf));
1364 got_repo_close(repo);
1373 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1376 char *owner = NULL, *d_file = NULL;
1377 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1378 char *comp, *pos, *buf;
1381 if (gw_trans->gw_conf->got_show_repo_owner == false)
1384 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1387 if ((f = fopen(d_file, "r")) == NULL)
1390 if ((buf = calloc(128, sizeof(char *))) == NULL)
1393 while ((fgets(buf, 128, f)) != NULL) {
1394 if ((pos = strstr(buf, gotweb)) != NULL)
1397 if ((pos = strstr(buf, gitweb)) != NULL)
1406 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1411 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1414 for (i = 0; i < 2; i++) {
1415 owner = strsep(&buf, "\"");
1425 if ((asprintf(&owner, "%s", "")) == -1)
1432 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1435 char *url = NULL, *d_file = NULL;
1438 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1441 if ((f = fopen(d_file, "r")) == NULL)
1444 fseek(f, 0, SEEK_END);
1446 fseek(f, 0, SEEK_SET);
1448 if ((url = calloc(len, sizeof(char *))) == NULL)
1451 fread(url, 1, len, f);
1458 gw_get_repo_log(struct gw_trans *gw_trans, const char *search_pattern,
1459 char *start_commit, int limit, int log_type)
1461 const struct got_error *error;
1462 struct got_repository *repo = NULL;
1463 struct got_reflist_head refs;
1464 struct got_reflist_entry *re;
1465 struct got_commit_object *commit = NULL;
1466 struct got_object_id *id1 = NULL, *id2 = NULL;
1467 struct got_object_qid *parent_id;
1468 struct got_commit_graph *graph = NULL;
1469 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1470 *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1471 *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1472 *commit_commit_disp = NULL, *commit_age_diff = NULL,
1473 *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1474 *commit_age_long_disp = NULL, *commit_author = NULL,
1475 *commit_author_disp = NULL, *commit_committer = NULL,
1476 *commit_committer_disp = NULL, *commit_log = NULL,
1477 *commit_log_disp = NULL, *commit_parent = NULL,
1478 *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1479 *log_tree_html = NULL, *log_commit_html = NULL,
1480 *log_diff_html = NULL, *commit_tree = NULL,
1481 *commit_tree_disp = NULL, *log_tag_html = NULL,
1482 *log_blame_html = NULL;
1483 char *commit_log0, *newline;
1485 int have_match, log_count = 0, has_parent = 1;
1487 struct buf *diffbuf = NULL;
1488 time_t committer_time;
1490 if (gw_trans->action == GW_LOG || gw_trans->action == GW_LOGBRIEFS)
1491 log_count = gw_get_repo_log_count(gw_trans, start_commit);
1493 error = buf_alloc(&diffbuf, 0);
1497 if (search_pattern &&
1498 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB |
1502 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1506 SIMPLEQ_INIT(&refs);
1508 if (start_commit == NULL) {
1509 struct got_reference *head_ref;
1510 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1514 error = got_ref_resolve(&id1, repo, head_ref);
1515 got_ref_close(head_ref);
1519 error = got_object_open_as_commit(&commit, repo, id1);
1521 struct got_reference *ref;
1522 error = got_ref_open(&ref, repo, start_commit, 0);
1523 if (error == NULL) {
1525 error = got_ref_resolve(&id1, repo, ref);
1529 error = got_object_get_type(&obj_type, repo, id1);
1532 if (obj_type == GOT_OBJ_TYPE_TAG) {
1533 struct got_tag_object *tag;
1534 error = got_object_open_as_tag(&tag, repo, id1);
1537 if (got_object_tag_get_object_type(tag) !=
1538 GOT_OBJ_TYPE_COMMIT) {
1539 got_object_tag_close(tag);
1540 error = got_error(GOT_ERR_OBJ_TYPE);
1544 id1 = got_object_id_dup(
1545 got_object_tag_get_object_id(tag));
1547 error = got_error_from_errno(
1548 "got_object_id_dup");
1549 got_object_tag_close(tag);
1552 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1553 error = got_error(GOT_ERR_OBJ_TYPE);
1556 error = got_object_open_as_commit(&commit, repo, id1);
1560 if (commit == NULL) {
1561 error = got_repo_match_object_id_prefix(&id1,
1562 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1566 error = got_repo_match_object_id_prefix(&id1,
1567 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1573 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1579 path = in_repo_path;
1582 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1586 error = got_commit_graph_open(&graph, path, 0);
1590 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1595 error = got_commit_graph_iter_next(&id1, graph, repo, NULL,
1598 if (error->code == GOT_ERR_ITER_COMPLETED)
1605 error = got_object_open_as_commit(&commit, repo, id1);
1609 if (search_pattern) {
1610 error = gw_match_logmsg(&have_match, id1, commit,
1613 got_object_commit_close(commit);
1616 if (have_match == 0) {
1617 got_object_commit_close(commit);
1622 SIMPLEQ_FOREACH(re, &refs, entry) {
1625 struct got_tag_object *tag = NULL;
1628 name = got_ref_get_name(re->ref);
1629 if (strcmp(name, GOT_REF_HEAD) == 0)
1631 if (strncmp(name, "refs/", 5) == 0)
1633 if (strncmp(name, "got/", 4) == 0)
1635 if (strncmp(name, "heads/", 6) == 0)
1637 if (strncmp(name, "remotes/", 8) == 0)
1639 if (strncmp(name, "tags/", 5) == 0) {
1640 error = got_object_open_as_tag(&tag, repo,
1643 if (error->code != GOT_ERR_OBJ_TYPE)
1646 * Ref points at something other
1653 cmp = got_object_id_cmp(tag ?
1654 got_object_tag_get_object_id(tag) : re->id, id1);
1656 got_object_tag_close(tag);
1660 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1661 s ? ", " : "", name)) == -1) {
1662 error = got_error_from_errno("asprintf");
1669 if (refs_str == NULL)
1670 refs_str_disp = strdup("");
1672 if ((asprintf(&refs_str_disp, "(%s)",
1674 error = got_error_from_errno("asprintf");
1680 error = got_object_id_str(&id_str1, id1);
1684 error = got_object_id_str(&treeid,
1685 got_object_commit_get_tree_id(commit));
1689 if (gw_trans->action == GW_COMMIT ||
1690 gw_trans->action == GW_COMMITDIFF) {
1693 got_object_commit_get_parent_ids(commit));
1694 if (parent_id != NULL) {
1695 id2 = got_object_id_dup(parent_id->id);
1697 error = got_object_id_str(&id_str2, id2);
1703 id_str2 = strdup("/dev/null");
1708 got_object_commit_get_committer_time(commit);
1710 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1711 error = got_error_from_errno("asprintf");
1715 if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1716 error = got_error_from_errno("asprintf");
1720 if ((asprintf(&commit_tree_disp, commit_tree_html,
1722 error = got_error_from_errno("asprintf");
1726 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1728 error = got_error_from_errno("asprintf");
1732 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1733 error = got_error_from_errno("asprintf");
1737 if ((asprintf(&commit_commit_disp, commit_commit_html,
1738 commit_commit, refs_str_disp)) == -1) {
1739 error = got_error_from_errno("asprintf");
1743 if ((asprintf(&commit_age_long, "%s",
1744 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1745 error = got_error_from_errno("asprintf");
1749 if ((asprintf(&commit_age_long_disp, commit_age_html,
1750 commit_age_long)) == -1) {
1751 error = got_error_from_errno("asprintf");
1755 if ((asprintf(&commit_age_diff, "%s",
1756 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1757 error = got_error_from_errno("asprintf");
1761 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1762 commit_age_diff)) == -1) {
1763 error = got_error_from_errno("asprintf");
1767 if ((asprintf(&commit_author, "%s",
1768 got_object_commit_get_author(commit))) == -1) {
1769 error = got_error_from_errno("asprintf");
1773 if ((asprintf(&commit_author_disp, commit_author_html,
1774 gw_html_escape(commit_author))) == -1) {
1775 error = got_error_from_errno("asprintf");
1779 if ((asprintf(&commit_committer, "%s",
1780 got_object_commit_get_committer(commit))) == -1) {
1781 error = got_error_from_errno("asprintf");
1785 if ((asprintf(&commit_committer_disp, commit_committer_html,
1786 gw_html_escape(commit_committer))) == -1) {
1787 error = got_error_from_errno("asprintf");
1791 if (strcmp(commit_author, commit_committer) == 0) {
1792 free(commit_committer_disp);
1793 commit_committer_disp = strdup("");
1796 error = got_object_commit_get_logmsg(&commit_log0, commit);
1800 commit_log = commit_log0;
1801 while (*commit_log == '\n')
1806 newline = strchr(commit_log, '\n');
1810 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1811 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1812 id_str1, gw_trans->repo_name, id_str1,
1813 gw_trans->repo_name, id_str1)) == -1) {
1814 error = got_error_from_errno("asprintf");
1818 if ((asprintf(&commit_row, logbriefs_row,
1819 commit_age_diff, commit_author, commit_log,
1820 logbriefs_navs_html)) == -1) {
1821 error = got_error_from_errno("asprintf");
1825 free(logbriefs_navs_html);
1826 logbriefs_navs_html = NULL;
1829 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1830 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1831 id_str1, gw_trans->repo_name, id_str1,
1832 gw_trans->repo_name, id_str1)) == -1) {
1833 error = got_error_from_errno("asprintf");
1837 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1838 commit_author_disp, commit_committer_disp,
1839 commit_age_long_disp, gw_html_escape(commit_log),
1840 logbriefs_navs_html)) == -1) {
1841 error = got_error_from_errno("asprintf");
1845 free(logbriefs_navs_html);
1846 logbriefs_navs_html = NULL;
1849 log_tag_html = strdup("tag log here");
1851 if ((asprintf(&commit_row, log_tag_row,
1852 gw_html_escape(commit_log), log_tag_html)) == -1) {
1853 error = got_error_from_errno("asprintf");
1860 log_blame_html = gw_get_file_blame(gw_trans,
1863 if ((asprintf(&commit_row, log_blame_row,
1864 gw_html_escape(commit_log), log_blame_html)) == -1) {
1865 error = got_error_from_errno("asprintf");
1869 free(log_blame_html);
1872 log_tree_html = gw_get_repo_tree(gw_trans,
1875 if ((asprintf(&commit_row, log_tree_row,
1876 gw_html_escape(commit_log), log_tree_html)) == -1) {
1877 error = got_error_from_errno("asprintf");
1881 free(log_tree_html);
1884 if ((asprintf(&commit_log_disp, commit_log_html,
1885 gw_html_escape(commit_log))) == -1) {
1886 error = got_error_from_errno("asprintf");
1890 log_commit_html = strdup("commit here");
1892 if ((asprintf(&commit_row, log_commit_row,
1893 commit_diff_disp, commit_commit_disp,
1894 commit_tree_disp, commit_author_disp,
1895 commit_committer_disp, commit_age_long_disp,
1896 commit_log_disp, log_commit_html)) == -1) {
1897 error = got_error_from_errno("asprintf");
1900 free(commit_log_disp);
1901 free(log_commit_html);
1905 if ((asprintf(&commit_log_disp, commit_log_html,
1906 gw_html_escape(commit_log))) == -1) {
1907 error = got_error_from_errno("asprintf");
1912 log_diff_html = gw_get_repo_diff(gw_trans,
1913 commit_commit, commit_parent);
1915 log_diff_html = gw_get_repo_diff(gw_trans,
1916 commit_commit, NULL);
1918 if ((asprintf(&commit_row, log_diff_row,
1919 commit_diff_disp, commit_commit_disp,
1920 commit_tree_disp, commit_author_disp,
1921 commit_committer_disp, commit_age_long_disp,
1922 commit_log_disp, log_diff_html)) == -1) {
1923 error = got_error_from_errno("asprintf");
1926 free(commit_log_disp);
1927 free(log_diff_html);
1934 error = buf_puts(&newsize, diffbuf, commit_row);
1936 free(commit_parent);
1937 free(commit_diff_disp);
1938 free(commit_tree_disp);
1939 free(commit_age_diff);
1940 free(commit_age_diff_disp);
1941 free(commit_age_long);
1942 free(commit_age_long_disp);
1943 free(commit_author);
1944 free(commit_author_disp);
1945 free(commit_committer);
1946 free(commit_committer_disp);
1949 free(refs_str_disp);
1957 if (error || (limit && --limit == 0))
1964 if (buf_len(diffbuf) > 0) {
1965 error = buf_putc(diffbuf, '\0');
1966 logs = strdup(buf_get(diffbuf));
1972 got_object_commit_close(commit);
1976 got_commit_graph_close(graph);
1978 error = got_repo_close(repo);
1983 khttp_puts(gw_trans->gw_req, "Error: ");
1984 khttp_puts(gw_trans->gw_req, error->msg);
1991 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
1993 const struct got_error *error = NULL;
1994 struct got_repository *repo = NULL;
1995 struct got_reflist_head refs;
1996 struct got_reflist_entry *re;
1997 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
2000 struct buf *diffbuf = NULL;
2003 error = buf_alloc(&diffbuf, 0);
2006 SIMPLEQ_INIT(&refs);
2008 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2012 error = got_ref_list(&refs, repo, "refs/tags", got_repo_cmp_tags, repo);
2016 SIMPLEQ_FOREACH(re, &refs, entry) {
2017 const char *refname;
2018 char *refstr, *tag_log0, *tag_log, *id_str;
2020 struct got_object_id *id;
2021 struct got_tag_object *tag;
2023 refname = got_ref_get_name(re->ref);
2024 if (strncmp(refname, "refs/tags/", 10) != 0)
2027 refstr = got_ref_to_str(re->ref);
2028 if (refstr == NULL) {
2029 error = got_error_from_errno("got_ref_to_str");
2033 error = got_ref_resolve(&id, repo, re->ref);
2036 error = got_object_open_as_tag(&tag, repo, id);
2041 tagger_time = got_object_tag_get_tagger_time(tag);
2043 error = got_object_id_str(&id_str,
2044 got_object_tag_get_object_id(tag));
2048 tag_log0 = strdup(got_object_tag_get_message(tag));
2050 if (tag_log0 == NULL) {
2051 error = got_error_from_errno("strdup");
2056 while (*tag_log == '\n')
2061 newline = strchr(tag_log, '\n');
2065 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
2067 error = got_error_from_errno("asprintf");
2071 if ((asprintf(&tags_navs_disp, tags_navs,
2072 gw_trans->repo_name, id_str, gw_trans->repo_name,
2073 id_str, gw_trans->repo_name, id_str,
2074 gw_trans->repo_name, id_str)) == -1) {
2075 error = got_error_from_errno("asprintf");
2079 if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
2080 tags_navs_disp)) == -1) {
2081 error = got_error_from_errno("asprintf");
2085 free(tags_navs_disp);
2093 got_object_tag_close(tag);
2095 error = buf_puts(&newsize, diffbuf, tag_row);
2103 if (error || (limit && --limit == 0))
2107 if (buf_len(diffbuf) > 0) {
2108 error = buf_putc(diffbuf, '\0');
2109 tags = strdup(buf_get(diffbuf));
2113 got_ref_list_free(&refs);
2115 got_repo_close(repo);
2126 char datebuf[11]; /* YYYY-MM-DD + NUL */
2129 struct gw_blame_cb_args {
2130 struct blame_line *lines;
2134 off_t *line_offsets;
2136 struct got_repository *repo;
2137 struct gw_trans *gw_trans;
2138 struct buf *blamebuf;
2141 static const struct got_error *
2142 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2144 const struct got_error *err = NULL;
2145 struct gw_blame_cb_args *a = arg;
2146 struct blame_line *bline;
2148 size_t linesize = 0, newsize;
2149 struct got_commit_object *commit = NULL;
2152 time_t committer_time;
2154 if (nlines != a->nlines ||
2155 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2156 return got_error(GOT_ERR_RANGE);
2159 return NULL; /* no change in this commit */
2161 /* Annotate this line. */
2162 bline = &a->lines[lineno - 1];
2163 if (bline->annotated)
2165 err = got_object_id_str(&bline->id_str, id);
2169 err = got_object_open_as_commit(&commit, a->repo, id);
2173 bline->committer = strdup(got_object_commit_get_committer(commit));
2174 if (bline->committer == NULL) {
2175 err = got_error_from_errno("strdup");
2179 committer_time = got_object_commit_get_committer_time(commit);
2180 if (localtime_r(&committer_time, &tm) == NULL)
2181 return got_error_from_errno("localtime_r");
2182 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2183 &tm) >= sizeof(bline->datebuf)) {
2184 err = got_error(GOT_ERR_NO_SPACE);
2187 bline->annotated = 1;
2189 /* Print lines annotated so far. */
2190 bline = &a->lines[a->lineno_cur - 1];
2191 if (!bline->annotated)
2194 offset = a->line_offsets[a->lineno_cur - 1];
2195 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2196 err = got_error_from_errno("fseeko");
2200 while (bline->annotated) {
2201 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2202 *line_escape = NULL;
2205 if (getline(&line, &linesize, a->f) == -1) {
2207 err = got_error_from_errno("getline");
2211 committer = bline->committer;
2212 smallerthan = strchr(committer, '<');
2213 if (smallerthan && smallerthan[1] != '\0')
2214 committer = smallerthan + 1;
2215 at = strchr(committer, '@');
2218 len = strlen(committer);
2220 committer[8] = '\0';
2222 nl = strchr(line, '\n');
2226 if (strcmp(line, "") != 0)
2227 line_escape = strdup(gw_html_escape(line));
2229 line_escape = strdup("");
2231 asprintf(&blame_row, log_blame_line, a->nlines_prec,
2232 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2235 err = buf_puts(&newsize, a->blamebuf, blame_row);
2239 bline = &a->lines[a->lineno_cur - 1];
2245 got_object_commit_close(commit);
2251 gw_get_file_blame(struct gw_trans *gw_trans, char *commit_str)
2253 const struct got_error *error = NULL;
2254 struct got_repository *repo = NULL;
2255 struct got_object_id *obj_id = NULL;
2256 struct got_object_id *commit_id = NULL;
2257 struct got_blob_object *blob = NULL;
2258 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2260 struct gw_blame_cb_args bca;
2264 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2268 if (gw_trans->repo_folder != NULL) {
2269 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2270 error = got_error_from_errno("asprintf");
2274 folder = strdup("");
2276 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2277 error = got_error_from_errno("asprintf");
2282 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2286 error = got_repo_match_object_id(&commit_id, NULL, commit_str,
2287 GOT_OBJ_TYPE_COMMIT, 1, repo);
2291 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2295 if (obj_id == NULL) {
2296 error = got_error(GOT_ERR_NO_OBJ);
2300 error = got_object_get_type(&obj_type, repo, obj_id);
2304 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2305 error = got_error(GOT_ERR_OBJ_TYPE);
2309 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2313 error = buf_alloc(&bca.blamebuf, 0);
2317 bca.f = got_opentemp();
2318 if (bca.f == NULL) {
2319 error = got_error_from_errno("got_opentemp");
2322 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2323 &bca.line_offsets, bca.f, blob);
2324 if (error || bca.nlines == 0)
2327 /* Don't include \n at EOF in the blame line count. */
2328 if (bca.line_offsets[bca.nlines - 1] == filesize)
2331 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2332 if (bca.lines == NULL) {
2333 error = got_error_from_errno("calloc");
2337 bca.nlines_prec = 0;
2344 bca.gw_trans = gw_trans;
2346 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2348 if (buf_len(bca.blamebuf) > 0) {
2349 error = buf_putc(bca.blamebuf, '\0');
2350 blame_html = strdup(buf_get(bca.blamebuf));
2360 error = got_object_blob_close(blob);
2362 error = got_repo_close(repo);
2366 for (i = 0; i < bca.nlines; i++) {
2367 struct blame_line *bline = &bca.lines[i];
2368 free(bline->id_str);
2369 free(bline->committer);
2373 free(bca.line_offsets);
2374 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2375 error = got_error_from_errno("fclose");
2383 gw_get_repo_tree(struct gw_trans *gw_trans, char *commit_str)
2385 const struct got_error *error = NULL;
2386 struct got_repository *repo = NULL;
2387 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2388 struct got_tree_object *tree = NULL;
2389 struct buf *diffbuf = NULL;
2391 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2392 *tree_row = NULL, *id_str;
2395 error = buf_alloc(&diffbuf, 0);
2399 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2403 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2407 if (gw_trans->repo_folder != NULL)
2408 path = strdup(gw_trans->repo_folder);
2409 else if (in_repo_path) {
2411 path = in_repo_path;
2414 if (commit_str == NULL) {
2415 struct got_reference *head_ref;
2416 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2420 error = got_ref_resolve(&commit_id, repo, head_ref);
2421 got_ref_close(head_ref);
2424 error = got_repo_match_object_id(&commit_id, NULL, commit_str,
2425 GOT_OBJ_TYPE_COMMIT, 1, repo);
2429 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2433 error = got_object_open_as_tree(&tree, repo, tree_id);
2437 nentries = got_object_tree_get_nentries(tree);
2439 for (i = 0; i < nentries; i++) {
2440 struct got_tree_entry *te;
2441 const char *modestr = "";
2442 char *id = NULL, *url_html = NULL;
2444 te = got_object_tree_get_entry(tree, i);
2446 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2450 if ((asprintf(&id, "%s", id_str)) == -1) {
2451 error = got_error_from_errno("asprintf");
2456 mode_t mode = got_tree_entry_get_mode(te);
2458 if (got_object_tree_entry_is_submodule(te))
2460 else if (S_ISLNK(mode))
2462 else if (S_ISDIR(mode))
2464 else if (mode & S_IXUSR)
2467 char *build_folder = NULL;
2468 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2469 if (gw_trans->repo_folder != NULL) {
2470 if ((asprintf(&build_folder, "%s/%s",
2471 gw_trans->repo_folder,
2472 got_tree_entry_get_name(te))) == -1) {
2474 got_error_from_errno("asprintf");
2478 if (asprintf(&build_folder, "%s",
2479 got_tree_entry_get_name(te)) == -1)
2483 if ((asprintf(&url_html, folder_html,
2484 gw_trans->repo_name, gw_trans->action_name,
2485 gw_trans->commit, build_folder,
2486 got_tree_entry_get_name(te), modestr)) == -1) {
2487 error = got_error_from_errno("asprintf");
2491 if (gw_trans->repo_folder != NULL) {
2492 if ((asprintf(&build_folder, "%s",
2493 gw_trans->repo_folder)) == -1) {
2495 got_error_from_errno("asprintf");
2499 build_folder = strdup("");
2501 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2502 "blame", gw_trans->commit,
2503 got_tree_entry_get_name(te), build_folder,
2504 got_tree_entry_get_name(te), modestr)) == -1) {
2505 error = got_error_from_errno("asprintf");
2514 if ((asprintf(&tree_row, trees_row, "", url_html)) == -1) {
2515 error = got_error_from_errno("asprintf");
2518 error = buf_puts(&newsize, diffbuf, tree_row);
2528 if (buf_len(diffbuf) > 0) {
2529 error = buf_putc(diffbuf, '\0');
2530 tree_html = strdup(buf_get(diffbuf));
2534 got_object_tree_close(tree);
2536 got_repo_close(repo);
2548 gw_get_repo_heads(struct gw_trans *gw_trans)
2550 const struct got_error *error = NULL;
2551 struct got_repository *repo = NULL;
2552 struct got_reflist_head refs;
2553 struct got_reflist_entry *re;
2554 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2555 struct buf *diffbuf = NULL;
2558 error = buf_alloc(&diffbuf, 0);
2562 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2566 SIMPLEQ_INIT(&refs);
2567 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2572 SIMPLEQ_FOREACH(re, &refs, entry) {
2575 refname = strdup(got_ref_get_name(re->ref));
2576 if (refname == NULL) {
2577 error = got_error_from_errno("got_ref_to_str");
2581 if (strncmp(refname, "refs/heads/", 11) != 0) {
2586 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2589 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2590 refname, gw_trans->repo_name, refname,
2591 gw_trans->repo_name, refname, gw_trans->repo_name,
2593 error = got_error_from_errno("asprintf");
2597 if (strncmp(refname, "refs/heads/", 11) == 0)
2600 if ((asprintf(&head_row, heads_row, age, refname,
2601 head_navs_disp)) == -1) {
2602 error = got_error_from_errno("asprintf");
2606 error = buf_puts(&newsize, diffbuf, head_row);
2608 free(head_navs_disp);
2612 if (buf_len(diffbuf) > 0) {
2613 error = buf_putc(diffbuf, '\0');
2614 heads = strdup(buf_get(diffbuf));
2618 got_ref_list_free(&refs);
2620 got_repo_close(repo);
2628 gw_get_got_link(struct gw_trans *gw_trans)
2632 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2633 gw_trans->gw_conf->got_logo)) == -1)
2640 gw_get_site_link(struct gw_trans *gw_trans)
2642 char *link, *repo = "", *action = "";
2644 if (gw_trans->repo_name != NULL)
2645 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2646 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2649 if (gw_trans->action_name != NULL)
2650 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2653 if ((asprintf(&link, site_link, GOTWEB,
2654 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2661 gw_colordiff_line(char *buf)
2663 const struct got_error *error = NULL;
2664 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2665 struct buf *diffbuf = NULL;
2668 error = buf_alloc(&diffbuf, 0);
2672 if (strncmp(buf, "-", 1) == 0)
2673 color = "diff_minus";
2674 if (strncmp(buf, "+", 1) == 0)
2675 color = "diff_plus";
2676 if (strncmp(buf, "@@", 2) == 0)
2677 color = "diff_chunk_header";
2678 if (strncmp(buf, "@@", 2) == 0)
2679 color = "diff_chunk_header";
2680 if (strncmp(buf, "commit +", 8) == 0)
2681 color = "diff_meta";
2682 if (strncmp(buf, "commit -", 8) == 0)
2683 color = "diff_meta";
2684 if (strncmp(buf, "blob +", 6) == 0)
2685 color = "diff_meta";
2686 if (strncmp(buf, "blob -", 6) == 0)
2687 color = "diff_meta";
2688 if (strncmp(buf, "file +", 6) == 0)
2689 color = "diff_meta";
2690 if (strncmp(buf, "file -", 6) == 0)
2691 color = "diff_meta";
2692 if (strncmp(buf, "from:", 5) == 0)
2693 color = "diff_author";
2694 if (strncmp(buf, "via:", 4) == 0)
2695 color = "diff_author";
2696 if (strncmp(buf, "date:", 5) == 0)
2697 color = "diff_date";
2699 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2702 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2706 error = buf_puts(&newsize, diffbuf, buf);
2710 if (buf_len(diffbuf) > 0) {
2711 error = buf_putc(diffbuf, '\0');
2712 colorized_line = strdup(buf_get(diffbuf));
2716 free(div_diff_line_div);
2717 return colorized_line;
2721 gw_html_escape(const char *html)
2723 char *escaped_str = NULL, *buf;
2725 size_t sz, i, buff_sz = 2048;
2727 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2733 if ((sz = strlen(html)) == 0)
2736 /* only work with buff_sz */
2740 for (i = 0; i < sz; i++) {
2744 strcat(buf, ">");
2747 strcat(buf, "&");
2750 strcat(buf, "<");
2753 strcat(buf, """);
2756 strcat(buf, "'");
2759 strcat(buf, "<br />");
2765 asprintf(&escaped_str, "%s", buf);
2773 const struct got_error *error = NULL;
2774 struct gw_trans *gw_trans;
2775 struct gw_dir *dir = NULL, *tdir;
2776 const char *page = "index";
2779 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2782 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2785 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2788 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2791 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX,
2793 errx(1, "khttp_parse");
2795 if ((gw_trans->gw_conf =
2796 malloc(sizeof(struct gotweb_conf))) == NULL) {
2798 error = got_error_from_errno("malloc");
2802 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
2804 error = got_error_from_errno("pledge");
2808 TAILQ_INIT(&gw_trans->gw_dirs);
2811 gw_trans->repos_total = 0;
2812 gw_trans->repo_path = NULL;
2813 gw_trans->commit = NULL;
2814 gw_trans->headref = strdup(GOT_REF_HEAD);
2815 gw_trans->mime = KMIME_TEXT_HTML;
2816 gw_trans->gw_tmpl->key = gw_templs;
2817 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2818 gw_trans->gw_tmpl->arg = gw_trans;
2819 gw_trans->gw_tmpl->cb = gw_template;
2820 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2824 gw_trans->mime = KMIME_TEXT_PLAIN;
2825 gw_trans->action = GW_ERR;
2826 gw_display_index(gw_trans, error);
2830 error = gw_parse_querystring(gw_trans);
2834 gw_display_index(gw_trans, error);
2838 free(gw_trans->gw_conf->got_repos_path);
2839 free(gw_trans->gw_conf->got_www_path);
2840 free(gw_trans->gw_conf->got_site_name);
2841 free(gw_trans->gw_conf->got_site_owner);
2842 free(gw_trans->gw_conf->got_site_link);
2843 free(gw_trans->gw_conf->got_logo);
2844 free(gw_trans->gw_conf->got_logo_url);
2845 free(gw_trans->gw_conf);
2846 free(gw_trans->commit);
2847 free(gw_trans->repo_path);
2848 free(gw_trans->repo_name);
2849 free(gw_trans->repo_file);
2850 free(gw_trans->action_name);
2851 free(gw_trans->headref);
2853 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2855 free(dir->description);
2864 khttp_free(gw_trans->gw_req);
2865 return EXIT_SUCCESS;