2 * Copyright (c) 2019 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2014, 2015, 2017 Kristaps Dzonsons <kristaps@bsd.lv>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
21 #include <sys/types.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
51 #include "gotweb_ui.h"
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
71 unsigned int repos_total;
85 TAILQ_ENTRY(gw_dir) entry;
111 /* buffer handle, buffer size, and data length */
117 static const char *const templs[TEMPL__MAX] = {
127 static const struct kvalid gw_keys[KEY__MAX] = {
128 { kvalid_stringne, "path" },
129 { kvalid_stringne, "action" },
130 { kvalid_stringne, "commit" },
131 { kvalid_stringne, "file" },
132 { kvalid_int, "page" },
135 static struct gw_dir *gw_init_gw_dir(char *);
137 static char *gw_get_repo_description(struct trans *,
139 static char *gw_get_repo_owner(struct trans *,
141 static char *gw_get_time_str(time_t, int);
142 static char *gw_get_repo_age(struct trans *,
143 char *, char *, int);
144 static char *gw_get_repo_shortlog(struct trans *,
146 static char *gw_get_repo_tags(struct trans *);
147 static char *gw_get_repo_heads(struct trans *);
148 static char *gw_get_clone_url(struct trans *, char *);
149 static char *gw_get_got_link(struct trans *);
150 static char *gw_get_site_link(struct trans *);
151 static char *gw_html_escape(const char *);
153 static void gw_display_open(struct trans *, enum khttp,
155 static void gw_display_index(struct trans *,
156 const struct got_error *);
158 static int gw_template(size_t, void *);
160 static const struct got_error* apply_unveil(const char *, const char *);
161 static const struct got_error* gw_load_got_paths(struct trans *);
162 static const struct got_error* gw_load_got_path(struct trans *,
164 static const struct got_error* gw_parse_querystring(struct trans *);
165 static const struct got_error* match_logmsg(int *, struct got_object_id *,
166 struct got_commit_object *, regex_t *);
168 static const struct got_error* gw_blame(struct trans *);
169 static const struct got_error* gw_blob(struct trans *);
170 static const struct got_error* gw_blob_diff(struct trans *);
171 static const struct got_error* gw_commit(struct trans *);
172 static const struct got_error* gw_commit_diff(struct trans *);
173 static const struct got_error* gw_history(struct trans *);
174 static const struct got_error* gw_index(struct trans *);
175 static const struct got_error* gw_log(struct trans *);
176 static const struct got_error* gw_raw(struct trans *);
177 static const struct got_error* gw_shortlog(struct trans *);
178 static const struct got_error* gw_snapshot(struct trans *);
179 static const struct got_error* gw_summary(struct trans *);
180 static const struct got_error* gw_tree(struct trans *);
182 struct gw_query_action {
183 unsigned int func_id;
184 const char *func_name;
185 const struct got_error *(*func_main)(struct trans *);
189 enum gw_query_actions {
206 static struct gw_query_action gw_query_funcs[] = {
207 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
208 { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
209 { GW_BLOBDIFF, "blobdiff", gw_blob_diff, "gw_tmpl/index.tmpl" },
210 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
211 { GW_COMMITDIFF, "commit_diff", gw_commit_diff, "gw_tmpl/index.tmpl" },
212 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
213 { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
214 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
215 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
216 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
217 { GW_SHORTLOG, "shortlog", gw_shortlog, "gw_tmpl/index.tmpl" },
218 { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
219 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
220 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
223 static const struct got_error *
224 apply_unveil(const char *repo_path, const char *repo_file)
226 const struct got_error *err;
228 if (repo_path && repo_file) {
230 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
231 return got_error_from_errno("asprintf unveil");
232 if (unveil(full_path, "r") != 0)
233 return got_error_from_errno2("unveil", full_path);
236 if (repo_path && unveil(repo_path, "r") != 0)
237 return got_error_from_errno2("unveil", repo_path);
239 if (unveil("/tmp", "rwc") != 0)
240 return got_error_from_errno2("unveil", "/tmp");
242 err = got_privsep_unveil_exec_helpers();
246 if (unveil(NULL, NULL) != 0)
247 return got_error_from_errno("unveil");
252 static const struct got_error *
253 gw_blame(struct trans *gw_trans)
255 const struct got_error *error = NULL;
260 static const struct got_error *
261 gw_blob(struct trans *gw_trans)
263 const struct got_error *error = NULL;
268 static const struct got_error *
269 gw_blob_diff(struct trans *gw_trans)
271 const struct got_error *error = NULL;
276 static const struct got_error *
277 gw_commit(struct trans *gw_trans)
279 const struct got_error *error = NULL;
284 static const struct got_error *
285 gw_commit_diff(struct trans *gw_trans)
287 const struct got_error *error = NULL;
292 static const struct got_error *
293 gw_history(struct trans *gw_trans)
295 const struct got_error *error = NULL;
300 static const struct got_error *
301 gw_index(struct trans *gw_trans)
303 const struct got_error *error = NULL;
304 struct gw_dir *gw_dir = NULL;
305 char *html, *navs, *next, *prev;
306 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
308 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
312 error = gw_load_got_paths(gw_trans);
316 khttp_puts(gw_trans->gw_req, index_projects_header);
318 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
321 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
322 if (gw_trans->page > 0 && (gw_trans->page *
323 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
329 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
330 gw_dir->name, gw_dir->name)) == -1)
331 return got_error_from_errno("asprintf");
333 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
334 gw_dir->description, gw_dir->owner, gw_dir->age,
336 return got_error_from_errno("asprintf");
338 khttp_puts(gw_trans->gw_req, html);
343 if (gw_trans->gw_conf->got_max_repos_display == 0)
346 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
347 khttp_puts(gw_trans->gw_req, np_wrapper_start);
348 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
349 (gw_trans->page > 0) &&
350 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
351 prev_disp == gw_trans->repos_total))
352 khttp_puts(gw_trans->gw_req, np_wrapper_start);
354 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
355 (gw_trans->page > 0) &&
356 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
357 prev_disp == gw_trans->repos_total)) {
358 if ((asprintf(&prev, nav_prev,
359 gw_trans->page - 1)) == -1)
360 return got_error_from_errno("asprintf");
361 khttp_puts(gw_trans->gw_req, prev);
365 khttp_puts(gw_trans->gw_req, div_end);
367 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
368 next_disp == gw_trans->gw_conf->got_max_repos_display &&
369 dir_c != (gw_trans->page + 1) *
370 gw_trans->gw_conf->got_max_repos_display) {
371 if ((asprintf(&next, nav_next,
372 gw_trans->page + 1)) == -1)
373 return got_error_from_errno("calloc");
374 khttp_puts(gw_trans->gw_req, next);
375 khttp_puts(gw_trans->gw_req, div_end);
381 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
382 (gw_trans->page > 0) &&
383 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
384 prev_disp == gw_trans->repos_total))
385 khttp_puts(gw_trans->gw_req, div_end);
392 static const struct got_error *
393 gw_log(struct trans *gw_trans)
395 const struct got_error *error = NULL;
400 static const struct got_error *
401 gw_raw(struct trans *gw_trans)
403 const struct got_error *error = NULL;
408 static const struct got_error *
409 gw_shortlog(struct trans *gw_trans)
411 const struct got_error *error = NULL;
416 static const struct got_error *
417 gw_snapshot(struct trans *gw_trans)
419 const struct got_error *error = NULL;
424 static const struct got_error *
425 gw_summary(struct trans *gw_trans)
427 const struct got_error *error = NULL;
428 char *description_html, *repo_owner_html, *repo_age_html,
429 *cloneurl_html, *shortlog, *tags, *heads, *shortlog_html,
430 *tags_html, *heads_html, *age;
432 error = apply_unveil(gw_trans->gw_dir->path, NULL);
436 khttp_puts(gw_trans->gw_req, summary_wrapper);
437 if (gw_trans->gw_conf->got_show_repo_description) {
438 if (gw_trans->gw_dir->description != NULL &&
439 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
440 if ((asprintf(&description_html, description,
441 gw_trans->gw_dir->description)) == -1)
442 return got_error_from_errno("asprintf");
444 khttp_puts(gw_trans->gw_req, description_html);
445 free(description_html);
449 if (gw_trans->gw_conf->got_show_repo_owner) {
450 if (gw_trans->gw_dir->owner != NULL &&
451 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
452 if ((asprintf(&repo_owner_html, repo_owner,
453 gw_trans->gw_dir->owner)) == -1)
454 return got_error_from_errno("asprintf");
456 khttp_puts(gw_trans->gw_req, repo_owner_html);
457 free(repo_owner_html);
461 if (gw_trans->gw_conf->got_show_repo_age) {
462 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
463 "refs/heads", TM_LONG);
464 if (age != NULL && (strcmp(age, "") != 0)) {
465 if ((asprintf(&repo_age_html, last_change, age)) == -1)
466 return got_error_from_errno("asprintf");
468 khttp_puts(gw_trans->gw_req, repo_age_html);
474 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
475 if (gw_trans->gw_dir->url != NULL &&
476 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
477 if ((asprintf(&cloneurl_html, cloneurl,
478 gw_trans->gw_dir->url)) == -1)
479 return got_error_from_errno("asprintf");
481 khttp_puts(gw_trans->gw_req, cloneurl_html);
485 khttp_puts(gw_trans->gw_req, div_end);
487 shortlog = gw_get_repo_shortlog(gw_trans, NULL);
488 tags = gw_get_repo_tags(gw_trans);
489 heads = gw_get_repo_heads(gw_trans);
491 if (shortlog != NULL && strcmp(shortlog, "") != 0) {
492 if ((asprintf(&shortlog_html, summary_shortlog,
494 return got_error_from_errno("asprintf");
495 khttp_puts(gw_trans->gw_req, shortlog_html);
500 if (tags != NULL && strcmp(tags, "") != 0) {
501 if ((asprintf(&tags_html, summary_tags,
503 return got_error_from_errno("asprintf");
504 khttp_puts(gw_trans->gw_req, tags_html);
509 if (heads != NULL && strcmp(heads, "") != 0) {
510 if ((asprintf(&heads_html, summary_heads,
512 return got_error_from_errno("asprintf");
513 khttp_puts(gw_trans->gw_req, heads_html);
521 static const struct got_error *
522 gw_tree(struct trans *gw_trans)
524 const struct got_error *error = NULL;
529 static const struct got_error *
530 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
532 const struct got_error *error = NULL;
537 if ((asprintf(&dir_test, "%s/%s/%s",
538 gw_trans->gw_conf->got_repos_path, gw_dir->name,
539 GOTWEB_GIT_DIR)) == -1)
540 return got_error_from_errno("asprintf");
542 dt = opendir(dir_test);
546 gw_dir->path = strdup(dir_test);
551 if ((asprintf(&dir_test, "%s/%s/%s",
552 gw_trans->gw_conf->got_repos_path, gw_dir->name,
553 GOTWEB_GOT_DIR)) == -1)
554 return got_error_from_errno("asprintf");
556 dt = opendir(dir_test);
561 error = got_error(GOT_ERR_NOT_GIT_REPO);
565 if ((asprintf(&dir_test, "%s/%s",
566 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
567 return got_error_from_errno("asprintf");
569 gw_dir->path = strdup(dir_test);
572 gw_dir->description = gw_get_repo_description(gw_trans,
574 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
575 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
577 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
586 static const struct got_error *
587 gw_load_got_paths(struct trans *gw_trans)
589 const struct got_error *error = NULL;
591 struct dirent **sd_dent;
592 struct gw_dir *gw_dir;
594 unsigned int d_cnt, d_i;
596 d = opendir(gw_trans->gw_conf->got_repos_path);
598 error = got_error_from_errno2("opendir",
599 gw_trans->gw_conf->got_repos_path);
603 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
606 error = got_error_from_errno2("scandir",
607 gw_trans->gw_conf->got_repos_path);
611 for (d_i = 0; d_i < d_cnt; d_i++) {
612 if (gw_trans->gw_conf->got_max_repos > 0 &&
613 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
614 break; /* account for parent and self */
616 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
617 strcmp(sd_dent[d_i]->d_name, "..") == 0)
620 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
621 return got_error_from_errno("gw_dir malloc");
623 error = gw_load_got_path(gw_trans, gw_dir);
624 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
629 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
630 !got_path_dir_is_empty(gw_dir->path)) {
631 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
633 gw_trans->repos_total++;
641 static const struct got_error *
642 gw_parse_querystring(struct trans *gw_trans)
644 const struct got_error *error = NULL;
646 struct gw_query_action *action = NULL;
649 if (gw_trans->gw_req->fieldnmap[0]) {
650 error = got_error_from_errno("bad parse");
652 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
653 /* define gw_trans->repo_path */
654 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
655 return got_error_from_errno("asprintf");
657 if ((asprintf(&gw_trans->repo_path, "%s/%s",
658 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
659 return got_error_from_errno("asprintf");
661 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
662 if ((asprintf(&gw_trans->commit, "%s",
664 return got_error_from_errno("asprintf");
666 /* get action and set function */
667 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
668 for (i = 0; i < nitems(gw_query_funcs); i++) {
669 action = &gw_query_funcs[i];
670 if (action->func_name == NULL)
673 if (strcmp(action->func_name,
675 gw_trans->action = i;
676 if ((asprintf(&gw_trans->action_name,
677 "%s", action->func_name)) == -1)
679 got_error_from_errno(
688 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
689 if ((asprintf(&gw_trans->repo_file, "%s",
691 return got_error_from_errno("asprintf");
693 if (action == NULL) {
694 error = got_error_from_errno("invalid action");
697 if ((gw_trans->gw_dir =
698 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
699 return got_error_from_errno("gw_dir malloc");
701 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
705 gw_trans->action = GW_INDEX;
707 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
708 gw_trans->page = p->parsed.i;
710 if (gw_trans->action == GW_RAW)
711 gw_trans->mime = KMIME_TEXT_PLAIN;
716 static struct gw_dir *
717 gw_init_gw_dir(char *dir)
719 struct gw_dir *gw_dir;
721 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
724 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
730 static const struct got_error*
731 match_logmsg(int *have_match, struct got_object_id *id,
732 struct got_commit_object *commit, regex_t *regex)
734 const struct got_error *err = NULL;
736 char *id_str = NULL, *logmsg = NULL;
740 err = got_object_id_str(&id_str, id);
744 err = got_object_commit_get_logmsg(&logmsg, commit);
748 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
757 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
759 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
760 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
762 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
764 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
765 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
766 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
767 khttp_body(gw_trans->gw_req);
771 gw_display_index(struct trans *gw_trans, const struct got_error *err)
773 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
774 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
777 khttp_puts(gw_trans->gw_req, err->msg);
779 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
780 gw_query_funcs[gw_trans->action].template);
782 khtml_close(gw_trans->gw_html_req);
786 gw_template(size_t key, void *arg)
788 const struct got_error *error = NULL;
789 struct trans *gw_trans = arg;
790 char *gw_got_link, *gw_site_link;
791 char *site_owner_name, *site_owner_name_h;
795 khttp_puts(gw_trans->gw_req, head);
798 gw_got_link = gw_get_got_link(gw_trans);
799 if (gw_got_link != NULL)
800 khttp_puts(gw_trans->gw_req, gw_got_link);
804 case (TEMPL_SITEPATH):
805 gw_site_link = gw_get_site_link(gw_trans);
806 if (gw_site_link != NULL)
807 khttp_puts(gw_trans->gw_req, gw_site_link);
812 if (gw_trans->gw_conf->got_site_name != NULL)
813 khtml_puts(gw_trans->gw_html_req,
814 gw_trans->gw_conf->got_site_name);
818 khttp_puts(gw_trans->gw_req, search);
820 case(TEMPL_SITEOWNER):
821 if (gw_trans->gw_conf->got_site_owner != NULL &&
822 gw_trans->gw_conf->got_show_site_owner) {
824 gw_html_escape(gw_trans->gw_conf->got_site_owner);
825 if ((asprintf(&site_owner_name_h, site_owner,
830 khttp_puts(gw_trans->gw_req, site_owner_name_h);
831 free(site_owner_name);
832 free(site_owner_name_h);
836 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
838 khttp_puts(gw_trans->gw_req, error->msg);
849 gw_get_repo_description(struct trans *gw_trans, char *dir)
852 char *description = NULL, *d_file = NULL;
855 if (gw_trans->gw_conf->got_show_repo_description == false)
858 if ((asprintf(&d_file, "%s/description", dir)) == -1)
861 if ((f = fopen(d_file, "r")) == NULL)
864 fseek(f, 0, SEEK_END);
866 fseek(f, 0, SEEK_SET);
867 if ((description = calloc(len, sizeof(char *))) == NULL)
870 fread(description, 1, len, f);
875 if ((asprintf(&description, "%s", "")) == -1)
882 gw_get_time_str(time_t committer_time, int ref_tm)
886 char *years = "years ago", *months = "months ago";
887 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
888 char *minutes = "minutes ago", *seconds = "seconds ago";
889 char *now = "right now";
891 char datebuf[BUFFER_SIZE];
895 diff_time = time(NULL) - committer_time;
896 if (diff_time > 60 * 60 * 24 * 365 * 2) {
897 if ((asprintf(&repo_age, "%lld %s",
898 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
900 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
901 if ((asprintf(&repo_age, "%lld %s",
902 (diff_time / 60 / 60 / 24 / (365 / 12)),
905 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
906 if ((asprintf(&repo_age, "%lld %s",
907 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
909 } else if (diff_time > 60 * 60 * 24 * 2) {
910 if ((asprintf(&repo_age, "%lld %s",
911 (diff_time / 60 / 60 / 24), days)) == -1)
913 } else if (diff_time > 60 * 60 * 2) {
914 if ((asprintf(&repo_age, "%lld %s",
915 (diff_time / 60 / 60), hours)) == -1)
917 } else if (diff_time > 60 * 2) {
918 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
921 } else if (diff_time > 2) {
922 if ((asprintf(&repo_age, "%lld %s", diff_time,
926 if ((asprintf(&repo_age, "%s", now)) == -1)
931 if (gmtime_r(&committer_time, &tm) == NULL)
934 s = asctime_r(&tm, datebuf);
938 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
946 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
948 const struct got_error *error = NULL;
949 struct got_object_id *id = NULL;
950 struct got_repository *repo = NULL;
951 struct got_commit_object *commit = NULL;
952 struct got_reflist_head refs;
953 struct got_reflist_entry *re;
954 struct got_reference *head_ref;
955 time_t committer_time = 0, cmp_time = 0;
956 char *repo_age = NULL;
958 if (repo_ref == NULL)
962 if (gw_trans->gw_conf->got_show_repo_age == false) {
963 asprintf(&repo_age, "");
966 error = got_repo_open(&repo, dir, NULL);
970 error = got_ref_list(&refs, repo, repo_ref, got_ref_cmp_by_name,
976 SIMPLEQ_FOREACH(re, &refs, entry) {
977 refname = got_ref_get_name(re->ref);
978 error = got_ref_open(&head_ref, repo, refname, 0);
982 error = got_ref_resolve(&id, repo, head_ref);
983 got_ref_close(head_ref);
987 /* here is what breaks tags, so adjust */
988 error = got_object_open_as_commit(&commit, repo, id);
993 got_object_commit_get_committer_time(commit);
995 if (cmp_time < committer_time)
996 cmp_time = committer_time;
1000 committer_time = cmp_time;
1001 repo_age = gw_get_time_str(committer_time, ref_tm);
1003 if ((asprintf(&repo_age, "")) == -1)
1005 got_ref_list_free(&refs);
1009 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1016 gw_get_repo_owner(struct trans *gw_trans, char *dir)
1019 char *owner = NULL, *d_file = NULL;
1020 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1021 char *comp, *pos, *buf;
1024 if (gw_trans->gw_conf->got_show_repo_owner == false)
1027 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1030 if ((f = fopen(d_file, "r")) == NULL)
1033 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1036 while ((fgets(buf, BUFFER_SIZE, f)) != NULL) {
1037 if ((pos = strstr(buf, gotweb)) != NULL)
1040 if ((pos = strstr(buf, gitweb)) != NULL)
1048 fgets(buf, BUFFER_SIZE, f);
1049 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1054 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1057 for (i = 0; i < 2; i++) {
1058 owner = strsep(&buf, "\"");
1068 if ((asprintf(&owner, "%s", "")) == -1)
1075 gw_get_clone_url(struct trans *gw_trans, char *dir)
1078 char *url = NULL, *d_file = NULL;
1081 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1084 if ((f = fopen(d_file, "r")) == NULL)
1087 fseek(f, 0, SEEK_END);
1089 fseek(f, 0, SEEK_SET);
1091 if ((url = calloc(len, sizeof(char *))) == NULL)
1094 fread(url, 1, len, f);
1101 gw_get_repo_shortlog(struct trans *gw_trans, const char *search_pattern)
1103 const struct got_error *error;
1104 struct got_repository *repo = NULL;
1105 struct got_reflist_head refs;
1106 struct got_reflist_entry *re;
1107 struct got_commit_object *commit = NULL;
1108 struct got_object_id *id = NULL;
1109 struct got_commit_graph *graph = NULL;
1110 char *start_commit = NULL, *shortlog = NULL, *id_str = NULL,
1111 *path = NULL, *in_repo_path = NULL, *commit_row = NULL,
1112 *commit_age = NULL, *commit_author = NULL, *commit_log = NULL,
1113 *shortlog_navs_html = NULL;
1115 int have_match, limit = D_MAXSLCOMMDISP;
1117 struct buf *diffbuf;
1118 time_t committer_time;
1120 if (search_pattern &&
1121 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB |
1125 SIMPLEQ_INIT(&refs);
1127 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1131 error = buf_alloc(&diffbuf, BUFFER_SIZE);
1135 if (start_commit == NULL) {
1136 struct got_reference *head_ref;
1137 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1140 error = got_ref_resolve(&id, repo, head_ref);
1141 got_ref_close(head_ref);
1144 error = got_object_open_as_commit(&commit, repo, id);
1146 struct got_reference *ref;
1147 error = got_ref_open(&ref, repo, start_commit, 0);
1148 if (error == NULL) {
1150 error = got_ref_resolve(&id, repo, ref);
1154 error = got_object_get_type(&obj_type, repo, id);
1157 if (obj_type == GOT_OBJ_TYPE_TAG) {
1158 struct got_tag_object *tag;
1159 error = got_object_open_as_tag(&tag, repo, id);
1162 if (got_object_tag_get_object_type(tag) !=
1163 GOT_OBJ_TYPE_COMMIT) {
1164 got_object_tag_close(tag);
1165 error = got_error(GOT_ERR_OBJ_TYPE);
1169 id = got_object_id_dup(
1170 got_object_tag_get_object_id(tag));
1172 error = got_error_from_errno(
1173 "got_object_id_dup");
1174 got_object_tag_close(tag);
1177 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1178 error = got_error(GOT_ERR_OBJ_TYPE);
1181 error = got_object_open_as_commit(&commit, repo, id);
1185 if (commit == NULL) {
1186 error = got_repo_match_object_id_prefix(&id,
1187 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1196 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1202 path = in_repo_path;
1205 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1209 error = got_commit_graph_open(&graph, id, path, 0, repo);
1213 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
1218 struct got_commit_object *commit_disp;
1220 error = got_commit_graph_iter_next(&id, graph);
1222 if (error->code == GOT_ERR_ITER_COMPLETED) {
1226 if (error->code != GOT_ERR_ITER_NEED_MORE)
1228 error = got_commit_graph_fetch_commits(graph, 1, repo,
1238 error = got_object_open_as_commit(&commit_disp, repo, id);
1242 if (search_pattern) {
1243 error = match_logmsg(&have_match, id, commit_disp,
1246 got_object_commit_close(commit_disp);
1249 if (have_match == 0) {
1250 got_object_commit_close(commit_disp);
1255 SIMPLEQ_FOREACH(re, &refs, entry) {
1257 struct got_tag_object *tag = NULL;
1260 name = got_ref_get_name(re->ref);
1261 if (strcmp(name, GOT_REF_HEAD) == 0)
1263 if (strncmp(name, "refs/", 5) == 0)
1265 if (strncmp(name, "got/", 4) == 0)
1267 if (strncmp(name, "heads/", 6) == 0)
1269 if (strncmp(name, "remotes/", 8) == 0)
1271 if (strncmp(name, "tags/", 5) == 0) {
1272 error = got_object_open_as_tag(&tag, repo,
1275 if (error->code != GOT_ERR_OBJ_TYPE)
1278 * Ref points at something other
1285 cmp = got_object_id_cmp(tag ?
1286 got_object_tag_get_object_id(tag) : re->id, id);
1288 got_object_tag_close(tag);
1294 error = got_object_id_str(&id_str, id);
1299 got_object_commit_get_committer_time(commit_disp);
1300 asprintf(&commit_age, "%s", gw_get_time_str(committer_time,
1302 asprintf(&commit_author, "%s",
1303 got_object_commit_get_author(commit_disp));
1304 error = got_object_commit_get_logmsg(&commit_log, commit_disp);
1306 commit_log = strdup("");
1307 asprintf(&shortlog_navs_html, shortlog_navs,
1308 gw_trans->repo_name, id_str, gw_trans->repo_name, id_str,
1309 gw_trans->repo_name, id_str, gw_trans->repo_name, id_str);
1310 asprintf(&commit_row, shortlog_row, commit_age, commit_author,
1311 commit_log, shortlog_navs_html);
1312 error = buf_append(&newsize, diffbuf, commit_row,
1313 strlen(commit_row));
1316 free(commit_author);
1318 free(shortlog_navs_html);
1322 commit_author = NULL;
1324 shortlog_navs_html = NULL;
1328 got_object_commit_close(commit_disp);
1329 if (error || (limit && --limit == 0))
1332 shortlog = strdup(diffbuf->cb_buf);
1333 got_object_commit_close(commit);
1340 error = got_repo_close(repo);
1345 got_ref_list_free(&refs);
1349 got_repo_close(repo);
1350 got_ref_list_free(&refs);
1354 got_commit_graph_close(graph);
1359 gw_get_repo_tags(struct trans *gw_trans)
1363 asprintf(&tags, tags_row, "30 min ago", "1.0.0", "tag 1.0.0", tags_navs);
1368 gw_get_repo_heads(struct trans *gw_trans)
1372 asprintf(&heads, heads_row, "30 min ago", "master", heads_navs);
1377 gw_get_got_link(struct trans *gw_trans)
1381 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
1382 gw_trans->gw_conf->got_logo)) == -1)
1389 gw_get_site_link(struct trans *gw_trans)
1391 char *link, *repo = "", *action = "";
1393 if (gw_trans->repo_name != NULL)
1394 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
1395 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
1398 if (gw_trans->action_name != NULL)
1399 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
1402 if ((asprintf(&link, site_link, GOTWEB,
1403 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
1410 gw_html_escape(const char *html)
1412 char *escaped_str = NULL, *buf;
1416 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1422 if ((sz = strlen(html)) == 0)
1425 /* only work with BUFFER_SIZE */
1426 if (BUFFER_SIZE < sz)
1429 for (i = 0; i < sz; i++) {
1433 strcat(buf, ">");
1436 strcat(buf, "&");
1439 strcat(buf, "<");
1442 strcat(buf, """);
1445 strcat(buf, "'");
1448 strcat(buf, "<br />");
1454 asprintf(&escaped_str, "%s", buf);
1462 const struct got_error *error = NULL;
1463 struct trans *gw_trans;
1464 struct gw_dir *dir = NULL, *tdir;
1465 const char *page = "index";
1466 bool gw_malloc = true;
1468 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
1471 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
1474 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
1477 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
1480 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
1482 errx(1, "khttp_parse");
1484 if ((gw_trans->gw_conf =
1485 malloc(sizeof(struct gotweb_conf))) == NULL) {
1487 error = got_error_from_errno("malloc");
1491 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
1492 error = got_error_from_errno("pledge");
1496 TAILQ_INIT(&gw_trans->gw_dirs);
1499 gw_trans->repos_total = 0;
1500 gw_trans->repo_path = NULL;
1501 gw_trans->commit = NULL;
1502 gw_trans->mime = KMIME_TEXT_HTML;
1503 gw_trans->gw_tmpl->key = templs;
1504 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
1505 gw_trans->gw_tmpl->arg = gw_trans;
1506 gw_trans->gw_tmpl->cb = gw_template;
1507 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
1511 gw_trans->mime = KMIME_TEXT_PLAIN;
1512 gw_trans->action = GW_ERR;
1513 gw_display_index(gw_trans, error);
1517 error = gw_parse_querystring(gw_trans);
1521 gw_display_index(gw_trans, error);
1525 free(gw_trans->gw_conf->got_repos_path);
1526 free(gw_trans->gw_conf->got_www_path);
1527 free(gw_trans->gw_conf->got_site_name);
1528 free(gw_trans->gw_conf->got_site_owner);
1529 free(gw_trans->gw_conf->got_site_link);
1530 free(gw_trans->gw_conf->got_logo);
1531 free(gw_trans->gw_conf->got_logo_url);
1532 free(gw_trans->gw_conf);
1533 free(gw_trans->commit);
1534 free(gw_trans->repo_path);
1535 free(gw_trans->repo_name);
1536 free(gw_trans->repo_file);
1537 free(gw_trans->action_name);
1539 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
1541 free(dir->description);
1550 khttp_free(gw_trans->gw_req);
1551 return EXIT_SUCCESS;