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* cmp_tags(void *, int *,
183 struct got_reference *,
184 struct got_reference *);
185 static const struct got_error* gw_blame_cb(void *, int, int,
186 struct got_object_id *);
187 static const struct got_error* gw_load_got_paths(struct gw_trans *);
188 static const struct got_error* gw_load_got_path(struct gw_trans *,
190 static const struct got_error* gw_parse_querystring(struct gw_trans *);
191 static const struct got_error* match_logmsg(int *, struct got_object_id *,
192 struct got_commit_object *, regex_t *);
194 static const struct got_error* gw_blame(struct gw_trans *);
195 static const struct got_error* gw_commit(struct gw_trans *);
196 static const struct got_error* gw_commitdiff(struct gw_trans *);
197 static const struct got_error* gw_index(struct gw_trans *);
198 static const struct got_error* gw_log(struct gw_trans *);
199 static const struct got_error* gw_raw(struct gw_trans *);
200 static const struct got_error* gw_logbriefs(struct gw_trans *);
201 static const struct got_error* gw_summary(struct gw_trans *);
202 static const struct got_error* gw_tag(struct gw_trans *);
203 static const struct got_error* gw_tree(struct gw_trans *);
205 struct gw_query_action {
206 unsigned int func_id;
207 const char *func_name;
208 const struct got_error *(*func_main)(struct gw_trans *);
212 enum gw_query_actions {
226 static struct gw_query_action gw_query_funcs[] = {
227 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
228 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
229 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
230 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
231 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
232 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
233 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
234 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
235 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
236 { GW_TAG, "tag", gw_tag, "gw_tmpl/index.tmpl" },
237 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
240 static const struct got_error *
241 gw_apply_unveil(const char *repo_path, const char *repo_file)
243 const struct got_error *err;
245 if (repo_path && repo_file) {
247 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
248 return got_error_from_errno("asprintf unveil");
249 if (unveil(full_path, "r") != 0)
250 return got_error_from_errno2("unveil", full_path);
253 if (repo_path && unveil(repo_path, "r") != 0)
254 return got_error_from_errno2("unveil", repo_path);
256 if (unveil("/tmp", "rwc") != 0)
257 return got_error_from_errno2("unveil", "/tmp");
259 err = got_privsep_unveil_exec_helpers();
263 if (unveil(NULL, NULL) != 0)
264 return got_error_from_errno("unveil");
269 static const struct got_error *
270 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
271 struct got_reference *ref2)
273 const struct got_error *err = NULL;
274 struct got_repository *repo = arg;
275 struct got_object_id *id1, *id2 = NULL;
276 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
281 err = got_ref_resolve(&id1, repo, ref1);
284 err = got_object_open_as_tag(&tag1, repo, id1);
288 err = got_ref_resolve(&id2, repo, ref2);
291 err = got_object_open_as_tag(&tag2, repo, id2);
295 time1 = got_object_tag_get_tagger_time(tag1);
296 time2 = got_object_tag_get_tagger_time(tag2);
298 /* Put latest tags first. */
301 else if (time1 > time2)
304 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
309 got_object_tag_close(tag1);
311 got_object_tag_close(tag2);
316 gw_get_repo_log_count(struct gw_trans *gw_trans, char *start_commit)
318 const struct got_error *error;
319 struct got_repository *repo = NULL;
320 struct got_reflist_head refs;
321 struct got_commit_object *commit = NULL;
322 struct got_object_id *id = NULL;
323 struct got_commit_graph *graph = NULL;
324 char *in_repo_path = NULL, *path = NULL;
327 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
333 if (start_commit == NULL) {
334 struct got_reference *head_ref;
335 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
339 error = got_ref_resolve(&id, repo, head_ref);
340 got_ref_close(head_ref);
344 error = got_object_open_as_commit(&commit, repo, id);
346 struct got_reference *ref;
347 error = got_ref_open(&ref, repo, start_commit, 0);
350 error = got_ref_resolve(&id, repo, ref);
354 error = got_object_get_type(&obj_type, repo, id);
357 if (obj_type == GOT_OBJ_TYPE_TAG) {
358 struct got_tag_object *tag;
359 error = got_object_open_as_tag(&tag, repo, id);
362 if (got_object_tag_get_object_type(tag) !=
363 GOT_OBJ_TYPE_COMMIT) {
364 got_object_tag_close(tag);
365 error = got_error(GOT_ERR_OBJ_TYPE);
369 id = got_object_id_dup(
370 got_object_tag_get_object_id(tag));
372 error = got_error_from_errno(
373 "got_object_id_dup");
374 got_object_tag_close(tag);
377 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
378 error = got_error(GOT_ERR_OBJ_TYPE);
381 error = got_object_open_as_commit(&commit, repo, id);
385 if (commit == NULL) {
386 error = got_repo_match_object_id_prefix(&id,
387 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
391 error = got_repo_match_object_id_prefix(&id,
392 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
397 error = got_object_open_as_commit(&commit, repo, id);
401 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
410 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
414 error = got_commit_graph_open(&graph, path, 0);
418 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
423 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
426 if (error->code == GOT_ERR_ITER_COMPLETED)
440 got_commit_graph_close(graph);
442 error = got_repo_close(repo);
447 khttp_puts(gw_trans->gw_req, "Error: ");
448 khttp_puts(gw_trans->gw_req, error->msg);
454 static const struct got_error *
455 gw_blame(struct gw_trans *gw_trans)
457 const struct got_error *error = NULL;
459 char *log, *log_html;
461 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
465 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGBLAME);
467 if (log != NULL && strcmp(log, "") != 0) {
468 if ((asprintf(&log_html, log_blame, log)) == -1)
469 return got_error_from_errno("asprintf");
470 khttp_puts(gw_trans->gw_req, log_html);
477 static const struct got_error *
478 gw_commit(struct gw_trans *gw_trans)
480 const struct got_error *error = NULL;
481 char *log, *log_html;
483 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
487 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
489 if (log != NULL && strcmp(log, "") != 0) {
490 if ((asprintf(&log_html, log_commit, log)) == -1)
491 return got_error_from_errno("asprintf");
492 khttp_puts(gw_trans->gw_req, log_html);
499 static const struct got_error *
500 gw_commitdiff(struct gw_trans *gw_trans)
502 const struct got_error *error = NULL;
503 char *log, *log_html;
505 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
509 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
511 if (log != NULL && strcmp(log, "") != 0) {
512 if ((asprintf(&log_html, log_diff, log)) == -1)
513 return got_error_from_errno("asprintf");
514 khttp_puts(gw_trans->gw_req, log_html);
521 static const struct got_error *
522 gw_index(struct gw_trans *gw_trans)
524 const struct got_error *error = NULL;
525 struct gw_dir *gw_dir = NULL;
526 char *html, *navs, *next, *prev;
527 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
529 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
533 error = gw_load_got_paths(gw_trans);
537 khttp_puts(gw_trans->gw_req, index_projects_header);
539 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
542 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
543 if (gw_trans->page > 0 && (gw_trans->page *
544 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
550 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
551 gw_dir->name, gw_dir->name)) == -1)
552 return got_error_from_errno("asprintf");
554 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
555 gw_dir->description, gw_dir->owner, gw_dir->age,
557 return got_error_from_errno("asprintf");
559 khttp_puts(gw_trans->gw_req, html);
564 if (gw_trans->gw_conf->got_max_repos_display == 0)
567 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
568 khttp_puts(gw_trans->gw_req, np_wrapper_start);
569 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
570 (gw_trans->page > 0) &&
571 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
572 prev_disp == gw_trans->repos_total))
573 khttp_puts(gw_trans->gw_req, np_wrapper_start);
575 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
576 (gw_trans->page > 0) &&
577 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
578 prev_disp == gw_trans->repos_total)) {
579 if ((asprintf(&prev, nav_prev,
580 gw_trans->page - 1)) == -1)
581 return got_error_from_errno("asprintf");
582 khttp_puts(gw_trans->gw_req, prev);
586 khttp_puts(gw_trans->gw_req, div_end);
588 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
589 next_disp == gw_trans->gw_conf->got_max_repos_display &&
590 dir_c != (gw_trans->page + 1) *
591 gw_trans->gw_conf->got_max_repos_display) {
592 if ((asprintf(&next, nav_next,
593 gw_trans->page + 1)) == -1)
594 return got_error_from_errno("calloc");
595 khttp_puts(gw_trans->gw_req, next);
596 khttp_puts(gw_trans->gw_req, div_end);
602 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
603 (gw_trans->page > 0) &&
604 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
605 prev_disp == gw_trans->repos_total))
606 khttp_puts(gw_trans->gw_req, div_end);
613 static const struct got_error *
614 gw_log(struct gw_trans *gw_trans)
616 const struct got_error *error = NULL;
617 char *log, *log_html;
619 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
623 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
624 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
626 if (log != NULL && strcmp(log, "") != 0) {
627 if ((asprintf(&log_html, logs, log)) == -1)
628 return got_error_from_errno("asprintf");
629 khttp_puts(gw_trans->gw_req, log_html);
636 static const struct got_error *
637 gw_raw(struct gw_trans *gw_trans)
639 const struct got_error *error = NULL;
644 static const struct got_error *
645 gw_logbriefs(struct gw_trans *gw_trans)
647 const struct got_error *error = NULL;
648 char *log, *log_html;
650 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
654 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
655 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
657 if (log != NULL && strcmp(log, "") != 0) {
658 if ((asprintf(&log_html, summary_logbriefs,
660 return got_error_from_errno("asprintf");
661 khttp_puts(gw_trans->gw_req, log_html);
668 static const struct got_error *
669 gw_summary(struct gw_trans *gw_trans)
671 const struct got_error *error = NULL;
672 char *description_html, *repo_owner_html, *repo_age_html,
673 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
676 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
680 khttp_puts(gw_trans->gw_req, summary_wrapper);
681 if (gw_trans->gw_conf->got_show_repo_description) {
682 if (gw_trans->gw_dir->description != NULL &&
683 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
684 if ((asprintf(&description_html, description,
685 gw_trans->gw_dir->description)) == -1)
686 return got_error_from_errno("asprintf");
688 khttp_puts(gw_trans->gw_req, description_html);
689 free(description_html);
693 if (gw_trans->gw_conf->got_show_repo_owner) {
694 if (gw_trans->gw_dir->owner != NULL &&
695 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
696 if ((asprintf(&repo_owner_html, repo_owner,
697 gw_trans->gw_dir->owner)) == -1)
698 return got_error_from_errno("asprintf");
700 khttp_puts(gw_trans->gw_req, repo_owner_html);
701 free(repo_owner_html);
705 if (gw_trans->gw_conf->got_show_repo_age) {
706 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
707 "refs/heads", TM_LONG);
708 if (age != NULL && (strcmp(age, "") != 0)) {
709 if ((asprintf(&repo_age_html, last_change, age)) == -1)
710 return got_error_from_errno("asprintf");
712 khttp_puts(gw_trans->gw_req, repo_age_html);
718 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
719 if (gw_trans->gw_dir->url != NULL &&
720 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
721 if ((asprintf(&cloneurl_html, cloneurl,
722 gw_trans->gw_dir->url)) == -1)
723 return got_error_from_errno("asprintf");
725 khttp_puts(gw_trans->gw_req, cloneurl_html);
729 khttp_puts(gw_trans->gw_req, div_end);
731 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
732 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
733 heads = gw_get_repo_heads(gw_trans);
735 if (log != NULL && strcmp(log, "") != 0) {
736 if ((asprintf(&log_html, summary_logbriefs,
738 return got_error_from_errno("asprintf");
739 khttp_puts(gw_trans->gw_req, log_html);
744 if (tags != NULL && strcmp(tags, "") != 0) {
745 if ((asprintf(&tags_html, summary_tags,
747 return got_error_from_errno("asprintf");
748 khttp_puts(gw_trans->gw_req, tags_html);
753 if (heads != NULL && strcmp(heads, "") != 0) {
754 if ((asprintf(&heads_html, summary_heads,
756 return got_error_from_errno("asprintf");
757 khttp_puts(gw_trans->gw_req, heads_html);
764 static const struct got_error *
765 gw_tag(struct gw_trans *gw_trans)
767 const struct got_error *error = NULL;
768 char *log, *log_html;
770 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
774 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTAG);
776 if (log != NULL && strcmp(log, "") != 0) {
777 if ((asprintf(&log_html, log_tag, log)) == -1)
778 return got_error_from_errno("asprintf");
779 khttp_puts(gw_trans->gw_req, log_html);
786 static const struct got_error *
787 gw_tree(struct gw_trans *gw_trans)
789 const struct got_error *error = NULL;
790 char *log, *log_html;
792 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
796 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
798 if (log != NULL && strcmp(log, "") != 0) {
799 if ((asprintf(&log_html, log_tree, log)) == -1)
800 return got_error_from_errno("asprintf");
801 khttp_puts(gw_trans->gw_req, log_html);
808 static const struct got_error *
809 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
811 const struct got_error *error = NULL;
816 if ((asprintf(&dir_test, "%s/%s/%s",
817 gw_trans->gw_conf->got_repos_path, gw_dir->name,
818 GOTWEB_GIT_DIR)) == -1)
819 return got_error_from_errno("asprintf");
821 dt = opendir(dir_test);
825 gw_dir->path = strdup(dir_test);
830 if ((asprintf(&dir_test, "%s/%s/%s",
831 gw_trans->gw_conf->got_repos_path, gw_dir->name,
832 GOTWEB_GOT_DIR)) == -1)
833 return got_error_from_errno("asprintf");
835 dt = opendir(dir_test);
840 error = got_error(GOT_ERR_NOT_GIT_REPO);
844 if ((asprintf(&dir_test, "%s/%s",
845 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
846 return got_error_from_errno("asprintf");
848 gw_dir->path = strdup(dir_test);
851 gw_dir->description = gw_get_repo_description(gw_trans,
853 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
854 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
856 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
865 static const struct got_error *
866 gw_load_got_paths(struct gw_trans *gw_trans)
868 const struct got_error *error = NULL;
870 struct dirent **sd_dent;
871 struct gw_dir *gw_dir;
873 unsigned int d_cnt, d_i;
875 d = opendir(gw_trans->gw_conf->got_repos_path);
877 error = got_error_from_errno2("opendir",
878 gw_trans->gw_conf->got_repos_path);
882 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
885 error = got_error_from_errno2("scandir",
886 gw_trans->gw_conf->got_repos_path);
890 for (d_i = 0; d_i < d_cnt; d_i++) {
891 if (gw_trans->gw_conf->got_max_repos > 0 &&
892 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
893 break; /* account for parent and self */
895 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
896 strcmp(sd_dent[d_i]->d_name, "..") == 0)
899 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
900 return got_error_from_errno("gw_dir malloc");
902 error = gw_load_got_path(gw_trans, gw_dir);
903 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
908 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
909 !got_path_dir_is_empty(gw_dir->path)) {
910 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
912 gw_trans->repos_total++;
920 static const struct got_error *
921 gw_parse_querystring(struct gw_trans *gw_trans)
923 const struct got_error *error = NULL;
925 struct gw_query_action *action = NULL;
928 if (gw_trans->gw_req->fieldnmap[0]) {
929 error = got_error_from_errno("bad parse");
931 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
932 /* define gw_trans->repo_path */
933 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
934 return got_error_from_errno("asprintf");
936 if ((asprintf(&gw_trans->repo_path, "%s/%s",
937 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
938 return got_error_from_errno("asprintf");
940 /* get action and set function */
941 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
942 for (i = 0; i < nitems(gw_query_funcs); i++) {
943 action = &gw_query_funcs[i];
944 if (action->func_name == NULL)
947 if (strcmp(action->func_name,
949 gw_trans->action = i;
950 if ((asprintf(&gw_trans->action_name,
951 "%s", action->func_name)) == -1)
953 got_error_from_errno(
962 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
963 if ((asprintf(&gw_trans->commit, "%s",
965 return got_error_from_errno("asprintf");
967 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
968 if ((asprintf(&gw_trans->repo_file, "%s",
970 return got_error_from_errno("asprintf");
972 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
973 if ((asprintf(&gw_trans->repo_folder, "%s",
975 return got_error_from_errno("asprintf");
977 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
978 if ((asprintf(&gw_trans->headref, "%s",
980 return got_error_from_errno("asprintf");
982 if (action == NULL) {
983 error = got_error_from_errno("invalid action");
986 if ((gw_trans->gw_dir =
987 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
988 return got_error_from_errno("gw_dir malloc");
990 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
994 gw_trans->action = GW_INDEX;
996 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
997 gw_trans->page = p->parsed.i;
999 if (gw_trans->action == GW_RAW)
1000 gw_trans->mime = KMIME_TEXT_PLAIN;
1005 static struct gw_dir *
1006 gw_init_gw_dir(char *dir)
1008 struct gw_dir *gw_dir;
1010 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1013 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1019 static const struct got_error*
1020 match_logmsg(int *have_match, struct got_object_id *id,
1021 struct got_commit_object *commit, regex_t *regex)
1023 const struct got_error *err = NULL;
1024 regmatch_t regmatch;
1025 char *id_str = NULL, *logmsg = NULL;
1029 err = got_object_id_str(&id_str, id);
1033 err = got_object_commit_get_logmsg(&logmsg, commit);
1037 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1046 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1048 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1049 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1051 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1053 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1054 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1055 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1056 khttp_body(gw_trans->gw_req);
1060 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
1062 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1063 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1066 khttp_puts(gw_trans->gw_req, err->msg);
1068 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1069 gw_query_funcs[gw_trans->action].template);
1071 khtml_close(gw_trans->gw_html_req);
1075 gw_template(size_t key, void *arg)
1077 const struct got_error *error = NULL;
1078 struct gw_trans *gw_trans = arg;
1079 char *gw_got_link, *gw_site_link;
1080 char *site_owner_name, *site_owner_name_h;
1084 khttp_puts(gw_trans->gw_req, head);
1087 gw_got_link = gw_get_got_link(gw_trans);
1088 if (gw_got_link != NULL)
1089 khttp_puts(gw_trans->gw_req, gw_got_link);
1093 case (TEMPL_SITEPATH):
1094 gw_site_link = gw_get_site_link(gw_trans);
1095 if (gw_site_link != NULL)
1096 khttp_puts(gw_trans->gw_req, gw_site_link);
1101 if (gw_trans->gw_conf->got_site_name != NULL)
1102 khtml_puts(gw_trans->gw_html_req,
1103 gw_trans->gw_conf->got_site_name);
1106 case (TEMPL_SEARCH):
1107 khttp_puts(gw_trans->gw_req, search);
1109 case(TEMPL_SITEOWNER):
1110 if (gw_trans->gw_conf->got_site_owner != NULL &&
1111 gw_trans->gw_conf->got_show_site_owner) {
1113 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1114 if ((asprintf(&site_owner_name_h, site_owner,
1119 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1120 free(site_owner_name);
1121 free(site_owner_name_h);
1124 case(TEMPL_CONTENT):
1125 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1127 khttp_puts(gw_trans->gw_req, error->msg);
1138 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1141 char *description = NULL, *d_file = NULL;
1144 if (gw_trans->gw_conf->got_show_repo_description == false)
1147 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1150 if ((f = fopen(d_file, "r")) == NULL)
1153 fseek(f, 0, SEEK_END);
1155 fseek(f, 0, SEEK_SET);
1156 if ((description = calloc(len, sizeof(char *))) == NULL)
1159 fread(description, 1, len, f);
1164 if ((asprintf(&description, "%s", "")) == -1)
1171 gw_get_time_str(time_t committer_time, int ref_tm)
1175 char *years = "years ago", *months = "months ago";
1176 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1177 char *minutes = "minutes ago", *seconds = "seconds ago";
1178 char *now = "right now";
1184 diff_time = time(NULL) - committer_time;
1185 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1186 if ((asprintf(&repo_age, "%lld %s",
1187 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1189 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1190 if ((asprintf(&repo_age, "%lld %s",
1191 (diff_time / 60 / 60 / 24 / (365 / 12)),
1194 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1195 if ((asprintf(&repo_age, "%lld %s",
1196 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1198 } else if (diff_time > 60 * 60 * 24 * 2) {
1199 if ((asprintf(&repo_age, "%lld %s",
1200 (diff_time / 60 / 60 / 24), days)) == -1)
1202 } else if (diff_time > 60 * 60 * 2) {
1203 if ((asprintf(&repo_age, "%lld %s",
1204 (diff_time / 60 / 60), hours)) == -1)
1206 } else if (diff_time > 60 * 2) {
1207 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1210 } else if (diff_time > 2) {
1211 if ((asprintf(&repo_age, "%lld %s", diff_time,
1215 if ((asprintf(&repo_age, "%s", now)) == -1)
1220 if (gmtime_r(&committer_time, &tm) == NULL)
1223 s = asctime_r(&tm, datebuf);
1227 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1235 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1238 const struct got_error *error = NULL;
1239 struct got_object_id *id = NULL;
1240 struct got_repository *repo = NULL;
1241 struct got_commit_object *commit = NULL;
1242 struct got_reflist_head refs;
1243 struct got_reflist_entry *re;
1244 struct got_reference *head_ref;
1246 time_t committer_time = 0, cmp_time = 0;
1247 const char *refname;
1248 char *repo_age = NULL;
1250 if (repo_ref == NULL)
1253 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1256 SIMPLEQ_INIT(&refs);
1257 if (gw_trans->gw_conf->got_show_repo_age == false) {
1258 if ((asprintf(&repo_age, "")) == -1)
1263 error = got_repo_open(&repo, dir, NULL);
1268 error = got_ref_list(&refs, repo, "refs/heads",
1269 got_ref_cmp_by_name, NULL);
1271 error = got_ref_list(&refs, repo, repo_ref,
1272 got_ref_cmp_by_name, NULL);
1276 SIMPLEQ_FOREACH(re, &refs, entry) {
1278 refname = strdup(repo_ref);
1280 refname = got_ref_get_name(re->ref);
1281 error = got_ref_open(&head_ref, repo, refname, 0);
1285 error = got_ref_resolve(&id, repo, head_ref);
1286 got_ref_close(head_ref);
1290 error = got_object_open_as_commit(&commit, repo, id);
1295 got_object_commit_get_committer_time(commit);
1297 if (cmp_time < committer_time)
1298 cmp_time = committer_time;
1301 if (cmp_time != 0) {
1302 committer_time = cmp_time;
1303 repo_age = gw_get_time_str(committer_time, ref_tm);
1305 if ((asprintf(&repo_age, "")) == -1)
1307 got_ref_list_free(&refs);
1311 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1318 gw_get_repo_diff(struct gw_trans *gw_trans, char *id_str1, char *id_str2)
1320 const struct got_error *error;
1322 struct got_object_id *id1 = NULL, *id2 = NULL;
1323 struct got_repository *repo = NULL;
1324 struct buf *diffbuf = NULL;
1325 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1334 error = buf_alloc(&diffbuf, 0);
1338 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1342 error = got_repo_match_object_id(&id1, &label1, id_str1,
1343 GOT_OBJ_TYPE_ANY, 1, repo);
1348 error = got_repo_match_object_id(&id2, &label2, id_str2,
1349 GOT_OBJ_TYPE_ANY, 1, repo);
1353 error = got_object_get_type(&type2, repo, id2);
1358 error = got_object_get_type(&type1, repo, id1);
1362 if (id_str2 && type1 != type2) {
1363 error = got_error(GOT_ERR_OBJ_TYPE);
1368 case GOT_OBJ_TYPE_BLOB:
1369 error = got_diff_objects_as_blobs(id2, id1, NULL, NULL, 3, 0,
1372 case GOT_OBJ_TYPE_TREE:
1373 error = got_diff_objects_as_trees(id2, id1, "", "", 3, 0, repo,
1376 case GOT_OBJ_TYPE_COMMIT:
1377 error = got_diff_objects_as_commits(id2, id1, 3, 0, repo, f);
1380 error = got_error(GOT_ERR_OBJ_TYPE);
1383 if ((buf = calloc(128, sizeof(char *))) == NULL)
1386 fseek(f, 0, SEEK_SET);
1388 while ((fgets(buf, 128, f)) != NULL) {
1389 buf_color = gw_colordiff_line(buf);
1390 error = buf_puts(&newsize, diffbuf, buf_color);
1394 error = buf_puts(&newsize, diffbuf, div_end);
1399 if (buf_len(diffbuf) > 0) {
1400 error = buf_putc(diffbuf, '\0');
1401 diff_html = strdup(buf_get(diffbuf));
1413 got_repo_close(repo);
1422 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1425 char *owner = NULL, *d_file = NULL;
1426 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1427 char *comp, *pos, *buf;
1430 if (gw_trans->gw_conf->got_show_repo_owner == false)
1433 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1436 if ((f = fopen(d_file, "r")) == NULL)
1439 if ((buf = calloc(128, sizeof(char *))) == NULL)
1442 while ((fgets(buf, 128, f)) != NULL) {
1443 if ((pos = strstr(buf, gotweb)) != NULL)
1446 if ((pos = strstr(buf, gitweb)) != NULL)
1455 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1460 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1463 for (i = 0; i < 2; i++) {
1464 owner = strsep(&buf, "\"");
1474 if ((asprintf(&owner, "%s", "")) == -1)
1481 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1484 char *url = NULL, *d_file = NULL;
1487 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1490 if ((f = fopen(d_file, "r")) == NULL)
1493 fseek(f, 0, SEEK_END);
1495 fseek(f, 0, SEEK_SET);
1497 if ((url = calloc(len, sizeof(char *))) == NULL)
1500 fread(url, 1, len, f);
1507 gw_get_repo_log(struct gw_trans *gw_trans, const char *search_pattern,
1508 char *start_commit, int limit, int log_type)
1510 const struct got_error *error;
1511 struct got_repository *repo = NULL;
1512 struct got_reflist_head refs;
1513 struct got_reflist_entry *re;
1514 struct got_commit_object *commit = NULL;
1515 struct got_object_id *id1 = NULL, *id2 = NULL;
1516 struct got_object_qid *parent_id;
1517 struct got_commit_graph *graph = NULL;
1518 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1519 *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1520 *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1521 *commit_commit_disp = NULL, *commit_age_diff = NULL,
1522 *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1523 *commit_age_long_disp = NULL, *commit_author = NULL,
1524 *commit_author_disp = NULL, *commit_committer = NULL,
1525 *commit_committer_disp = NULL, *commit_log = NULL,
1526 *commit_log_disp = NULL, *commit_parent = NULL,
1527 *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1528 *log_tree_html = NULL, *log_commit_html = NULL,
1529 *log_diff_html = NULL, *commit_tree = NULL,
1530 *commit_tree_disp = NULL, *log_tag_html = NULL,
1531 *log_blame_html = NULL;
1532 char *commit_log0, *newline;
1534 int have_match, log_count = 0, has_parent = 1;
1536 struct buf *diffbuf = NULL;
1537 time_t committer_time;
1539 if (gw_trans->action == GW_LOG || gw_trans->action == GW_LOGBRIEFS)
1540 log_count = gw_get_repo_log_count(gw_trans, start_commit);
1542 error = buf_alloc(&diffbuf, 0);
1546 if (search_pattern &&
1547 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB |
1551 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1555 SIMPLEQ_INIT(&refs);
1557 if (start_commit == NULL) {
1558 struct got_reference *head_ref;
1559 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1563 error = got_ref_resolve(&id1, repo, head_ref);
1564 got_ref_close(head_ref);
1568 error = got_object_open_as_commit(&commit, repo, id1);
1570 struct got_reference *ref;
1571 error = got_ref_open(&ref, repo, start_commit, 0);
1572 if (error == NULL) {
1574 error = got_ref_resolve(&id1, repo, ref);
1578 error = got_object_get_type(&obj_type, repo, id1);
1581 if (obj_type == GOT_OBJ_TYPE_TAG) {
1582 struct got_tag_object *tag;
1583 error = got_object_open_as_tag(&tag, repo, id1);
1586 if (got_object_tag_get_object_type(tag) !=
1587 GOT_OBJ_TYPE_COMMIT) {
1588 got_object_tag_close(tag);
1589 error = got_error(GOT_ERR_OBJ_TYPE);
1593 id1 = got_object_id_dup(
1594 got_object_tag_get_object_id(tag));
1596 error = got_error_from_errno(
1597 "got_object_id_dup");
1598 got_object_tag_close(tag);
1601 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1602 error = got_error(GOT_ERR_OBJ_TYPE);
1605 error = got_object_open_as_commit(&commit, repo, id1);
1609 if (commit == NULL) {
1610 error = got_repo_match_object_id_prefix(&id1,
1611 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1615 error = got_repo_match_object_id_prefix(&id1,
1616 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1622 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1628 path = in_repo_path;
1631 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1635 error = got_commit_graph_open(&graph, path, 0);
1639 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1644 error = got_commit_graph_iter_next(&id1, graph, repo, NULL,
1647 if (error->code == GOT_ERR_ITER_COMPLETED)
1654 error = got_object_open_as_commit(&commit, repo, id1);
1658 if (search_pattern) {
1659 error = match_logmsg(&have_match, id1, commit,
1662 got_object_commit_close(commit);
1665 if (have_match == 0) {
1666 got_object_commit_close(commit);
1671 SIMPLEQ_FOREACH(re, &refs, entry) {
1674 struct got_tag_object *tag = NULL;
1677 name = got_ref_get_name(re->ref);
1678 if (strcmp(name, GOT_REF_HEAD) == 0)
1680 if (strncmp(name, "refs/", 5) == 0)
1682 if (strncmp(name, "got/", 4) == 0)
1684 if (strncmp(name, "heads/", 6) == 0)
1686 if (strncmp(name, "remotes/", 8) == 0)
1688 if (strncmp(name, "tags/", 5) == 0) {
1689 error = got_object_open_as_tag(&tag, repo,
1692 if (error->code != GOT_ERR_OBJ_TYPE)
1695 * Ref points at something other
1702 cmp = got_object_id_cmp(tag ?
1703 got_object_tag_get_object_id(tag) : re->id, id1);
1705 got_object_tag_close(tag);
1709 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1710 s ? ", " : "", name)) == -1) {
1711 error = got_error_from_errno("asprintf");
1718 if (refs_str == NULL)
1719 refs_str_disp = strdup("");
1721 if ((asprintf(&refs_str_disp, "(%s)",
1723 error = got_error_from_errno("asprintf");
1729 error = got_object_id_str(&id_str1, id1);
1733 error = got_object_id_str(&treeid,
1734 got_object_commit_get_tree_id(commit));
1738 if (gw_trans->action == GW_COMMIT ||
1739 gw_trans->action == GW_COMMITDIFF) {
1742 got_object_commit_get_parent_ids(commit));
1743 if (parent_id != NULL) {
1744 id2 = got_object_id_dup(parent_id->id);
1746 error = got_object_id_str(&id_str2, id2);
1752 id_str2 = strdup("/dev/null");
1757 got_object_commit_get_committer_time(commit);
1759 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1760 error = got_error_from_errno("asprintf");
1764 if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1765 error = got_error_from_errno("asprintf");
1769 if ((asprintf(&commit_tree_disp, commit_tree_html,
1771 error = got_error_from_errno("asprintf");
1775 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1777 error = got_error_from_errno("asprintf");
1781 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1782 error = got_error_from_errno("asprintf");
1786 if ((asprintf(&commit_commit_disp, commit_commit_html,
1787 commit_commit, refs_str_disp)) == -1) {
1788 error = got_error_from_errno("asprintf");
1792 if ((asprintf(&commit_age_long, "%s",
1793 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1794 error = got_error_from_errno("asprintf");
1798 if ((asprintf(&commit_age_long_disp, commit_age_html,
1799 commit_age_long)) == -1) {
1800 error = got_error_from_errno("asprintf");
1804 if ((asprintf(&commit_age_diff, "%s",
1805 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1806 error = got_error_from_errno("asprintf");
1810 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1811 commit_age_diff)) == -1) {
1812 error = got_error_from_errno("asprintf");
1816 if ((asprintf(&commit_author, "%s",
1817 got_object_commit_get_author(commit))) == -1) {
1818 error = got_error_from_errno("asprintf");
1822 if ((asprintf(&commit_author_disp, commit_author_html,
1823 gw_html_escape(commit_author))) == -1) {
1824 error = got_error_from_errno("asprintf");
1828 if ((asprintf(&commit_committer, "%s",
1829 got_object_commit_get_committer(commit))) == -1) {
1830 error = got_error_from_errno("asprintf");
1834 if ((asprintf(&commit_committer_disp, commit_committer_html,
1835 gw_html_escape(commit_committer))) == -1) {
1836 error = got_error_from_errno("asprintf");
1840 if (strcmp(commit_author, commit_committer) == 0) {
1841 free(commit_committer_disp);
1842 commit_committer_disp = strdup("");
1845 error = got_object_commit_get_logmsg(&commit_log0, commit);
1849 commit_log = commit_log0;
1850 while (*commit_log == '\n')
1855 newline = strchr(commit_log, '\n');
1859 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1860 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1861 id_str1, gw_trans->repo_name, id_str1,
1862 gw_trans->repo_name, id_str1)) == -1) {
1863 error = got_error_from_errno("asprintf");
1867 if ((asprintf(&commit_row, logbriefs_row,
1868 commit_age_diff, commit_author, commit_log,
1869 logbriefs_navs_html)) == -1) {
1870 error = got_error_from_errno("asprintf");
1874 free(logbriefs_navs_html);
1875 logbriefs_navs_html = NULL;
1878 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1879 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1880 id_str1, gw_trans->repo_name, id_str1,
1881 gw_trans->repo_name, id_str1)) == -1) {
1882 error = got_error_from_errno("asprintf");
1886 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1887 commit_author_disp, commit_committer_disp,
1888 commit_age_long_disp, gw_html_escape(commit_log),
1889 logbriefs_navs_html)) == -1) {
1890 error = got_error_from_errno("asprintf");
1894 free(logbriefs_navs_html);
1895 logbriefs_navs_html = NULL;
1898 log_tag_html = strdup("tag log here");
1900 if ((asprintf(&commit_row, log_tag_row,
1901 gw_html_escape(commit_log), log_tag_html)) == -1) {
1902 error = got_error_from_errno("asprintf");
1909 log_blame_html = gw_get_file_blame(gw_trans,
1912 if ((asprintf(&commit_row, log_blame_row,
1913 gw_html_escape(commit_log), log_blame_html)) == -1) {
1914 error = got_error_from_errno("asprintf");
1918 free(log_blame_html);
1921 log_tree_html = gw_get_repo_tree(gw_trans,
1924 if ((asprintf(&commit_row, log_tree_row,
1925 gw_html_escape(commit_log), log_tree_html)) == -1) {
1926 error = got_error_from_errno("asprintf");
1930 free(log_tree_html);
1933 if ((asprintf(&commit_log_disp, commit_log_html,
1934 gw_html_escape(commit_log))) == -1) {
1935 error = got_error_from_errno("asprintf");
1939 log_commit_html = strdup("commit here");
1941 if ((asprintf(&commit_row, log_commit_row,
1942 commit_diff_disp, commit_commit_disp,
1943 commit_tree_disp, commit_author_disp,
1944 commit_committer_disp, commit_age_long_disp,
1945 commit_log_disp, log_commit_html)) == -1) {
1946 error = got_error_from_errno("asprintf");
1949 free(commit_log_disp);
1950 free(log_commit_html);
1954 if ((asprintf(&commit_log_disp, commit_log_html,
1955 gw_html_escape(commit_log))) == -1) {
1956 error = got_error_from_errno("asprintf");
1961 log_diff_html = gw_get_repo_diff(gw_trans,
1962 commit_commit, commit_parent);
1964 log_diff_html = gw_get_repo_diff(gw_trans,
1965 commit_commit, NULL);
1967 if ((asprintf(&commit_row, log_diff_row,
1968 commit_diff_disp, commit_commit_disp,
1969 commit_tree_disp, commit_author_disp,
1970 commit_committer_disp, commit_age_long_disp,
1971 commit_log_disp, log_diff_html)) == -1) {
1972 error = got_error_from_errno("asprintf");
1975 free(commit_log_disp);
1976 free(log_diff_html);
1983 error = buf_puts(&newsize, diffbuf, commit_row);
1985 free(commit_parent);
1986 free(commit_diff_disp);
1987 free(commit_tree_disp);
1988 free(commit_age_diff);
1989 free(commit_age_diff_disp);
1990 free(commit_age_long);
1991 free(commit_age_long_disp);
1992 free(commit_author);
1993 free(commit_author_disp);
1994 free(commit_committer);
1995 free(commit_committer_disp);
1998 free(refs_str_disp);
2006 if (error || (limit && --limit == 0))
2013 if (buf_len(diffbuf) > 0) {
2014 error = buf_putc(diffbuf, '\0');
2015 logs = strdup(buf_get(diffbuf));
2021 got_object_commit_close(commit);
2025 got_commit_graph_close(graph);
2027 error = got_repo_close(repo);
2032 khttp_puts(gw_trans->gw_req, "Error: ");
2033 khttp_puts(gw_trans->gw_req, error->msg);
2040 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
2042 const struct got_error *error = NULL;
2043 struct got_repository *repo = NULL;
2044 struct got_reflist_head refs;
2045 struct got_reflist_entry *re;
2046 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
2049 struct buf *diffbuf = NULL;
2052 error = buf_alloc(&diffbuf, 0);
2055 SIMPLEQ_INIT(&refs);
2057 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2061 error = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
2065 SIMPLEQ_FOREACH(re, &refs, entry) {
2066 const char *refname;
2067 char *refstr, *tag_log0, *tag_log, *id_str;
2069 struct got_object_id *id;
2070 struct got_tag_object *tag;
2072 refname = got_ref_get_name(re->ref);
2073 if (strncmp(refname, "refs/tags/", 10) != 0)
2076 refstr = got_ref_to_str(re->ref);
2077 if (refstr == NULL) {
2078 error = got_error_from_errno("got_ref_to_str");
2082 error = got_ref_resolve(&id, repo, re->ref);
2085 error = got_object_open_as_tag(&tag, repo, id);
2090 tagger_time = got_object_tag_get_tagger_time(tag);
2092 error = got_object_id_str(&id_str,
2093 got_object_tag_get_object_id(tag));
2097 tag_log0 = strdup(got_object_tag_get_message(tag));
2099 if (tag_log0 == NULL) {
2100 error = got_error_from_errno("strdup");
2105 while (*tag_log == '\n')
2110 newline = strchr(tag_log, '\n');
2114 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
2116 error = got_error_from_errno("asprintf");
2120 if ((asprintf(&tags_navs_disp, tags_navs,
2121 gw_trans->repo_name, id_str, gw_trans->repo_name,
2122 id_str, gw_trans->repo_name, id_str,
2123 gw_trans->repo_name, id_str)) == -1) {
2124 error = got_error_from_errno("asprintf");
2128 if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
2129 tags_navs_disp)) == -1) {
2130 error = got_error_from_errno("asprintf");
2134 free(tags_navs_disp);
2142 got_object_tag_close(tag);
2144 error = buf_puts(&newsize, diffbuf, tag_row);
2152 if (error || (limit && --limit == 0))
2156 if (buf_len(diffbuf) > 0) {
2157 error = buf_putc(diffbuf, '\0');
2158 tags = strdup(buf_get(diffbuf));
2162 got_ref_list_free(&refs);
2164 got_repo_close(repo);
2175 char datebuf[11]; /* YYYY-MM-DD + NUL */
2178 struct gw_blame_cb_args {
2179 struct blame_line *lines;
2183 off_t *line_offsets;
2185 struct got_repository *repo;
2186 struct gw_trans *gw_trans;
2187 struct buf *blamebuf;
2190 static const struct got_error *
2191 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2193 const struct got_error *err = NULL;
2194 struct gw_blame_cb_args *a = arg;
2195 struct blame_line *bline;
2197 size_t linesize = 0, newsize;
2198 struct got_commit_object *commit = NULL;
2201 time_t committer_time;
2203 if (nlines != a->nlines ||
2204 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2205 return got_error(GOT_ERR_RANGE);
2208 return NULL; /* no change in this commit */
2210 /* Annotate this line. */
2211 bline = &a->lines[lineno - 1];
2212 if (bline->annotated)
2214 err = got_object_id_str(&bline->id_str, id);
2218 err = got_object_open_as_commit(&commit, a->repo, id);
2222 bline->committer = strdup(got_object_commit_get_committer(commit));
2223 if (bline->committer == NULL) {
2224 err = got_error_from_errno("strdup");
2228 committer_time = got_object_commit_get_committer_time(commit);
2229 if (localtime_r(&committer_time, &tm) == NULL)
2230 return got_error_from_errno("localtime_r");
2231 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2232 &tm) >= sizeof(bline->datebuf)) {
2233 err = got_error(GOT_ERR_NO_SPACE);
2236 bline->annotated = 1;
2238 /* Print lines annotated so far. */
2239 bline = &a->lines[a->lineno_cur - 1];
2240 if (!bline->annotated)
2243 offset = a->line_offsets[a->lineno_cur - 1];
2244 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2245 err = got_error_from_errno("fseeko");
2249 while (bline->annotated) {
2250 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2251 *line_escape = NULL;
2254 if (getline(&line, &linesize, a->f) == -1) {
2256 err = got_error_from_errno("getline");
2260 committer = bline->committer;
2261 smallerthan = strchr(committer, '<');
2262 if (smallerthan && smallerthan[1] != '\0')
2263 committer = smallerthan + 1;
2264 at = strchr(committer, '@');
2267 len = strlen(committer);
2269 committer[8] = '\0';
2271 nl = strchr(line, '\n');
2275 if (strcmp(line, "") != 0)
2276 line_escape = strdup(gw_html_escape(line));
2278 line_escape = strdup("");
2280 asprintf(&blame_row, log_blame_line, a->nlines_prec,
2281 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2284 err = buf_puts(&newsize, a->blamebuf, blame_row);
2288 bline = &a->lines[a->lineno_cur - 1];
2294 got_object_commit_close(commit);
2300 gw_get_file_blame(struct gw_trans *gw_trans, char *commit_str)
2302 const struct got_error *error = NULL;
2303 struct got_repository *repo = NULL;
2304 struct got_object_id *obj_id = NULL;
2305 struct got_object_id *commit_id = NULL;
2306 struct got_blob_object *blob = NULL;
2307 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2309 struct gw_blame_cb_args bca;
2313 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2317 if (gw_trans->repo_folder != NULL) {
2318 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2319 error = got_error_from_errno("asprintf");
2323 folder = strdup("");
2325 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2326 error = got_error_from_errno("asprintf");
2331 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2335 error = got_repo_match_object_id(&commit_id, NULL, commit_str,
2336 GOT_OBJ_TYPE_COMMIT, 1, repo);
2340 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2344 if (obj_id == NULL) {
2345 error = got_error(GOT_ERR_NO_OBJ);
2349 error = got_object_get_type(&obj_type, repo, obj_id);
2353 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2354 error = got_error(GOT_ERR_OBJ_TYPE);
2358 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2362 error = buf_alloc(&bca.blamebuf, 0);
2366 bca.f = got_opentemp();
2367 if (bca.f == NULL) {
2368 error = got_error_from_errno("got_opentemp");
2371 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2372 &bca.line_offsets, bca.f, blob);
2373 if (error || bca.nlines == 0)
2376 /* Don't include \n at EOF in the blame line count. */
2377 if (bca.line_offsets[bca.nlines - 1] == filesize)
2380 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2381 if (bca.lines == NULL) {
2382 error = got_error_from_errno("calloc");
2386 bca.nlines_prec = 0;
2393 bca.gw_trans = gw_trans;
2395 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2397 if (buf_len(bca.blamebuf) > 0) {
2398 error = buf_putc(bca.blamebuf, '\0');
2399 blame_html = strdup(buf_get(bca.blamebuf));
2409 error = got_object_blob_close(blob);
2411 error = got_repo_close(repo);
2415 for (i = 0; i < bca.nlines; i++) {
2416 struct blame_line *bline = &bca.lines[i];
2417 free(bline->id_str);
2418 free(bline->committer);
2422 free(bca.line_offsets);
2423 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2424 error = got_error_from_errno("fclose");
2432 gw_get_repo_tree(struct gw_trans *gw_trans, char *commit_str)
2434 const struct got_error *error = NULL;
2435 struct got_repository *repo = NULL;
2436 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2437 struct got_tree_object *tree = NULL;
2438 struct buf *diffbuf = NULL;
2440 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2441 *tree_row = NULL, *id_str;
2444 error = buf_alloc(&diffbuf, 0);
2448 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2452 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2456 if (gw_trans->repo_folder != NULL)
2457 path = strdup(gw_trans->repo_folder);
2458 else if (in_repo_path) {
2460 path = in_repo_path;
2463 if (commit_str == NULL) {
2464 struct got_reference *head_ref;
2465 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2469 error = got_ref_resolve(&commit_id, repo, head_ref);
2470 got_ref_close(head_ref);
2473 error = got_repo_match_object_id(&commit_id, NULL, commit_str,
2474 GOT_OBJ_TYPE_COMMIT, 1, repo);
2478 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2482 error = got_object_open_as_tree(&tree, repo, tree_id);
2486 nentries = got_object_tree_get_nentries(tree);
2488 for (i = 0; i < nentries; i++) {
2489 struct got_tree_entry *te;
2490 const char *modestr = "";
2491 char *id = NULL, *url_html = NULL;
2493 te = got_object_tree_get_entry(tree, i);
2495 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2499 if ((asprintf(&id, "%s", id_str)) == -1) {
2500 error = got_error_from_errno("asprintf");
2505 mode_t mode = got_tree_entry_get_mode(te);
2507 if (got_object_tree_entry_is_submodule(te))
2509 else if (S_ISLNK(mode))
2511 else if (S_ISDIR(mode))
2513 else if (mode & S_IXUSR)
2516 char *build_folder = NULL;
2517 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2518 if (gw_trans->repo_folder != NULL) {
2519 if ((asprintf(&build_folder, "%s/%s",
2520 gw_trans->repo_folder,
2521 got_tree_entry_get_name(te))) == -1) {
2523 got_error_from_errno("asprintf");
2527 if (asprintf(&build_folder, "%s",
2528 got_tree_entry_get_name(te)) == -1)
2532 if ((asprintf(&url_html, folder_html,
2533 gw_trans->repo_name, gw_trans->action_name,
2534 gw_trans->commit, build_folder,
2535 got_tree_entry_get_name(te), modestr)) == -1) {
2536 error = got_error_from_errno("asprintf");
2540 if (gw_trans->repo_folder != NULL) {
2541 if ((asprintf(&build_folder, "%s",
2542 gw_trans->repo_folder)) == -1) {
2544 got_error_from_errno("asprintf");
2548 build_folder = strdup("");
2550 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2551 "blame", gw_trans->commit,
2552 got_tree_entry_get_name(te), build_folder,
2553 got_tree_entry_get_name(te), modestr)) == -1) {
2554 error = got_error_from_errno("asprintf");
2563 if ((asprintf(&tree_row, trees_row, "", url_html)) == -1) {
2564 error = got_error_from_errno("asprintf");
2567 error = buf_puts(&newsize, diffbuf, tree_row);
2577 if (buf_len(diffbuf) > 0) {
2578 error = buf_putc(diffbuf, '\0');
2579 tree_html = strdup(buf_get(diffbuf));
2583 got_object_tree_close(tree);
2585 got_repo_close(repo);
2597 gw_get_repo_heads(struct gw_trans *gw_trans)
2599 const struct got_error *error = NULL;
2600 struct got_repository *repo = NULL;
2601 struct got_reflist_head refs;
2602 struct got_reflist_entry *re;
2603 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2604 struct buf *diffbuf = NULL;
2607 error = buf_alloc(&diffbuf, 0);
2611 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2615 SIMPLEQ_INIT(&refs);
2616 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2621 SIMPLEQ_FOREACH(re, &refs, entry) {
2624 refname = strdup(got_ref_get_name(re->ref));
2625 if (refname == NULL) {
2626 error = got_error_from_errno("got_ref_to_str");
2630 if (strncmp(refname, "refs/heads/", 11) != 0) {
2635 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2638 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2639 refname, gw_trans->repo_name, refname,
2640 gw_trans->repo_name, refname, gw_trans->repo_name,
2642 error = got_error_from_errno("asprintf");
2646 if (strncmp(refname, "refs/heads/", 11) == 0)
2649 if ((asprintf(&head_row, heads_row, age, refname,
2650 head_navs_disp)) == -1) {
2651 error = got_error_from_errno("asprintf");
2655 error = buf_puts(&newsize, diffbuf, head_row);
2657 free(head_navs_disp);
2661 if (buf_len(diffbuf) > 0) {
2662 error = buf_putc(diffbuf, '\0');
2663 heads = strdup(buf_get(diffbuf));
2667 got_ref_list_free(&refs);
2669 got_repo_close(repo);
2677 gw_get_got_link(struct gw_trans *gw_trans)
2681 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2682 gw_trans->gw_conf->got_logo)) == -1)
2689 gw_get_site_link(struct gw_trans *gw_trans)
2691 char *link, *repo = "", *action = "";
2693 if (gw_trans->repo_name != NULL)
2694 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2695 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2698 if (gw_trans->action_name != NULL)
2699 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2702 if ((asprintf(&link, site_link, GOTWEB,
2703 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2710 gw_colordiff_line(char *buf)
2712 const struct got_error *error = NULL;
2713 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2714 struct buf *diffbuf = NULL;
2717 error = buf_alloc(&diffbuf, 0);
2721 if (strncmp(buf, "-", 1) == 0)
2722 color = "diff_minus";
2723 if (strncmp(buf, "+", 1) == 0)
2724 color = "diff_plus";
2725 if (strncmp(buf, "@@", 2) == 0)
2726 color = "diff_chunk_header";
2727 if (strncmp(buf, "@@", 2) == 0)
2728 color = "diff_chunk_header";
2729 if (strncmp(buf, "commit +", 8) == 0)
2730 color = "diff_meta";
2731 if (strncmp(buf, "commit -", 8) == 0)
2732 color = "diff_meta";
2733 if (strncmp(buf, "blob +", 6) == 0)
2734 color = "diff_meta";
2735 if (strncmp(buf, "blob -", 6) == 0)
2736 color = "diff_meta";
2737 if (strncmp(buf, "file +", 6) == 0)
2738 color = "diff_meta";
2739 if (strncmp(buf, "file -", 6) == 0)
2740 color = "diff_meta";
2741 if (strncmp(buf, "from:", 5) == 0)
2742 color = "diff_author";
2743 if (strncmp(buf, "via:", 4) == 0)
2744 color = "diff_author";
2745 if (strncmp(buf, "date:", 5) == 0)
2746 color = "diff_date";
2748 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2751 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2755 error = buf_puts(&newsize, diffbuf, buf);
2759 if (buf_len(diffbuf) > 0) {
2760 error = buf_putc(diffbuf, '\0');
2761 colorized_line = strdup(buf_get(diffbuf));
2765 free(div_diff_line_div);
2766 return colorized_line;
2770 gw_html_escape(const char *html)
2772 char *escaped_str = NULL, *buf;
2774 size_t sz, i, buff_sz = 2048;
2776 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2782 if ((sz = strlen(html)) == 0)
2785 /* only work with buff_sz */
2789 for (i = 0; i < sz; i++) {
2793 strcat(buf, ">");
2796 strcat(buf, "&");
2799 strcat(buf, "<");
2802 strcat(buf, """);
2805 strcat(buf, "'");
2808 strcat(buf, "<br />");
2814 asprintf(&escaped_str, "%s", buf);
2822 const struct got_error *error = NULL;
2823 struct gw_trans *gw_trans;
2824 struct gw_dir *dir = NULL, *tdir;
2825 const char *page = "index";
2828 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2831 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2834 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2837 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2840 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX,
2842 errx(1, "khttp_parse");
2844 if ((gw_trans->gw_conf =
2845 malloc(sizeof(struct gotweb_conf))) == NULL) {
2847 error = got_error_from_errno("malloc");
2851 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
2853 error = got_error_from_errno("pledge");
2857 TAILQ_INIT(&gw_trans->gw_dirs);
2860 gw_trans->repos_total = 0;
2861 gw_trans->repo_path = NULL;
2862 gw_trans->commit = NULL;
2863 gw_trans->headref = strdup(GOT_REF_HEAD);
2864 gw_trans->mime = KMIME_TEXT_HTML;
2865 gw_trans->gw_tmpl->key = gw_templs;
2866 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2867 gw_trans->gw_tmpl->arg = gw_trans;
2868 gw_trans->gw_tmpl->cb = gw_template;
2869 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2873 gw_trans->mime = KMIME_TEXT_PLAIN;
2874 gw_trans->action = GW_ERR;
2875 gw_display_index(gw_trans, error);
2879 error = gw_parse_querystring(gw_trans);
2883 gw_display_index(gw_trans, error);
2887 free(gw_trans->gw_conf->got_repos_path);
2888 free(gw_trans->gw_conf->got_www_path);
2889 free(gw_trans->gw_conf->got_site_name);
2890 free(gw_trans->gw_conf->got_site_owner);
2891 free(gw_trans->gw_conf->got_site_link);
2892 free(gw_trans->gw_conf->got_logo);
2893 free(gw_trans->gw_conf->got_logo_url);
2894 free(gw_trans->gw_conf);
2895 free(gw_trans->commit);
2896 free(gw_trans->repo_path);
2897 free(gw_trans->repo_name);
2898 free(gw_trans->repo_file);
2899 free(gw_trans->action_name);
2900 free(gw_trans->headref);
2902 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2904 free(dir->description);
2913 khttp_free(gw_trans->gw_req);
2914 return EXIT_SUCCESS;