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_log(struct trans *, const char *,
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_logbriefs(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_LOGBRIEFS, "logbriefs", gw_logbriefs, "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;
396 char *log, *log_html;
398 log = gw_get_repo_log(gw_trans, NULL, NULL,
399 gw_trans->gw_conf->got_max_commits_display, 1);
401 if (log != NULL && strcmp(log, "") != 0) {
402 if ((asprintf(&log_html, logs, log)) == -1)
403 return got_error_from_errno("asprintf");
404 khttp_puts(gw_trans->gw_req, log_html);
411 static const struct got_error *
412 gw_raw(struct trans *gw_trans)
414 const struct got_error *error = NULL;
419 static const struct got_error *
420 gw_logbriefs(struct trans *gw_trans)
422 const struct got_error *error = NULL;
423 char *logbriefs, *logbriefs_html;
425 logbriefs = gw_get_repo_log(gw_trans, NULL, NULL,
426 gw_trans->gw_conf->got_max_commits_display, 0);
428 if (logbriefs != NULL && strcmp(logbriefs, "") != 0) {
429 if ((asprintf(&logbriefs_html, summary_logbriefs,
431 return got_error_from_errno("asprintf");
432 khttp_puts(gw_trans->gw_req, logbriefs_html);
433 free(logbriefs_html);
439 static const struct got_error *
440 gw_snapshot(struct trans *gw_trans)
442 const struct got_error *error = NULL;
447 static const struct got_error *
448 gw_summary(struct trans *gw_trans)
450 const struct got_error *error = NULL;
451 char *description_html, *repo_owner_html, *repo_age_html,
452 *cloneurl_html, *logbriefs, *tags, *heads, *logbriefs_html,
453 *tags_html, *heads_html, *age;
455 error = apply_unveil(gw_trans->gw_dir->path, NULL);
459 khttp_puts(gw_trans->gw_req, summary_wrapper);
460 if (gw_trans->gw_conf->got_show_repo_description) {
461 if (gw_trans->gw_dir->description != NULL &&
462 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
463 if ((asprintf(&description_html, description,
464 gw_trans->gw_dir->description)) == -1)
465 return got_error_from_errno("asprintf");
467 khttp_puts(gw_trans->gw_req, description_html);
468 free(description_html);
472 if (gw_trans->gw_conf->got_show_repo_owner) {
473 if (gw_trans->gw_dir->owner != NULL &&
474 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
475 if ((asprintf(&repo_owner_html, repo_owner,
476 gw_trans->gw_dir->owner)) == -1)
477 return got_error_from_errno("asprintf");
479 khttp_puts(gw_trans->gw_req, repo_owner_html);
480 free(repo_owner_html);
484 if (gw_trans->gw_conf->got_show_repo_age) {
485 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
486 "refs/heads", TM_LONG);
487 if (age != NULL && (strcmp(age, "") != 0)) {
488 if ((asprintf(&repo_age_html, last_change, age)) == -1)
489 return got_error_from_errno("asprintf");
491 khttp_puts(gw_trans->gw_req, repo_age_html);
497 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
498 if (gw_trans->gw_dir->url != NULL &&
499 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
500 if ((asprintf(&cloneurl_html, cloneurl,
501 gw_trans->gw_dir->url)) == -1)
502 return got_error_from_errno("asprintf");
504 khttp_puts(gw_trans->gw_req, cloneurl_html);
508 khttp_puts(gw_trans->gw_req, div_end);
510 logbriefs = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
511 tags = gw_get_repo_tags(gw_trans);
512 heads = gw_get_repo_heads(gw_trans);
514 if (logbriefs != NULL && strcmp(logbriefs, "") != 0) {
515 if ((asprintf(&logbriefs_html, summary_logbriefs,
517 return got_error_from_errno("asprintf");
518 khttp_puts(gw_trans->gw_req, logbriefs_html);
519 free(logbriefs_html);
523 if (tags != NULL && strcmp(tags, "") != 0) {
524 if ((asprintf(&tags_html, summary_tags,
526 return got_error_from_errno("asprintf");
527 khttp_puts(gw_trans->gw_req, tags_html);
532 if (heads != NULL && strcmp(heads, "") != 0) {
533 if ((asprintf(&heads_html, summary_heads,
535 return got_error_from_errno("asprintf");
536 khttp_puts(gw_trans->gw_req, heads_html);
544 static const struct got_error *
545 gw_tree(struct trans *gw_trans)
547 const struct got_error *error = NULL;
552 static const struct got_error *
553 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
555 const struct got_error *error = NULL;
560 if ((asprintf(&dir_test, "%s/%s/%s",
561 gw_trans->gw_conf->got_repos_path, gw_dir->name,
562 GOTWEB_GIT_DIR)) == -1)
563 return got_error_from_errno("asprintf");
565 dt = opendir(dir_test);
569 gw_dir->path = strdup(dir_test);
574 if ((asprintf(&dir_test, "%s/%s/%s",
575 gw_trans->gw_conf->got_repos_path, gw_dir->name,
576 GOTWEB_GOT_DIR)) == -1)
577 return got_error_from_errno("asprintf");
579 dt = opendir(dir_test);
584 error = got_error(GOT_ERR_NOT_GIT_REPO);
588 if ((asprintf(&dir_test, "%s/%s",
589 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
590 return got_error_from_errno("asprintf");
592 gw_dir->path = strdup(dir_test);
595 gw_dir->description = gw_get_repo_description(gw_trans,
597 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
598 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
600 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
609 static const struct got_error *
610 gw_load_got_paths(struct trans *gw_trans)
612 const struct got_error *error = NULL;
614 struct dirent **sd_dent;
615 struct gw_dir *gw_dir;
617 unsigned int d_cnt, d_i;
619 d = opendir(gw_trans->gw_conf->got_repos_path);
621 error = got_error_from_errno2("opendir",
622 gw_trans->gw_conf->got_repos_path);
626 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
629 error = got_error_from_errno2("scandir",
630 gw_trans->gw_conf->got_repos_path);
634 for (d_i = 0; d_i < d_cnt; d_i++) {
635 if (gw_trans->gw_conf->got_max_repos > 0 &&
636 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
637 break; /* account for parent and self */
639 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
640 strcmp(sd_dent[d_i]->d_name, "..") == 0)
643 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
644 return got_error_from_errno("gw_dir malloc");
646 error = gw_load_got_path(gw_trans, gw_dir);
647 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
652 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
653 !got_path_dir_is_empty(gw_dir->path)) {
654 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
656 gw_trans->repos_total++;
664 static const struct got_error *
665 gw_parse_querystring(struct trans *gw_trans)
667 const struct got_error *error = NULL;
669 struct gw_query_action *action = NULL;
672 if (gw_trans->gw_req->fieldnmap[0]) {
673 error = got_error_from_errno("bad parse");
675 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
676 /* define gw_trans->repo_path */
677 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
678 return got_error_from_errno("asprintf");
680 if ((asprintf(&gw_trans->repo_path, "%s/%s",
681 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
682 return got_error_from_errno("asprintf");
684 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
685 if ((asprintf(&gw_trans->commit, "%s",
687 return got_error_from_errno("asprintf");
689 /* get action and set function */
690 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
691 for (i = 0; i < nitems(gw_query_funcs); i++) {
692 action = &gw_query_funcs[i];
693 if (action->func_name == NULL)
696 if (strcmp(action->func_name,
698 gw_trans->action = i;
699 if ((asprintf(&gw_trans->action_name,
700 "%s", action->func_name)) == -1)
702 got_error_from_errno(
711 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
712 if ((asprintf(&gw_trans->repo_file, "%s",
714 return got_error_from_errno("asprintf");
716 if (action == NULL) {
717 error = got_error_from_errno("invalid action");
720 if ((gw_trans->gw_dir =
721 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
722 return got_error_from_errno("gw_dir malloc");
724 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
728 gw_trans->action = GW_INDEX;
730 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
731 gw_trans->page = p->parsed.i;
733 if (gw_trans->action == GW_RAW)
734 gw_trans->mime = KMIME_TEXT_PLAIN;
739 static struct gw_dir *
740 gw_init_gw_dir(char *dir)
742 struct gw_dir *gw_dir;
744 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
747 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
753 static const struct got_error*
754 match_logmsg(int *have_match, struct got_object_id *id,
755 struct got_commit_object *commit, regex_t *regex)
757 const struct got_error *err = NULL;
759 char *id_str = NULL, *logmsg = NULL;
763 err = got_object_id_str(&id_str, id);
767 err = got_object_commit_get_logmsg(&logmsg, commit);
771 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
780 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
782 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
783 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
785 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
787 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
788 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
789 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
790 khttp_body(gw_trans->gw_req);
794 gw_display_index(struct trans *gw_trans, const struct got_error *err)
796 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
797 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
800 khttp_puts(gw_trans->gw_req, err->msg);
802 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
803 gw_query_funcs[gw_trans->action].template);
805 khtml_close(gw_trans->gw_html_req);
809 gw_template(size_t key, void *arg)
811 const struct got_error *error = NULL;
812 struct trans *gw_trans = arg;
813 char *gw_got_link, *gw_site_link;
814 char *site_owner_name, *site_owner_name_h;
818 khttp_puts(gw_trans->gw_req, head);
821 gw_got_link = gw_get_got_link(gw_trans);
822 if (gw_got_link != NULL)
823 khttp_puts(gw_trans->gw_req, gw_got_link);
827 case (TEMPL_SITEPATH):
828 gw_site_link = gw_get_site_link(gw_trans);
829 if (gw_site_link != NULL)
830 khttp_puts(gw_trans->gw_req, gw_site_link);
835 if (gw_trans->gw_conf->got_site_name != NULL)
836 khtml_puts(gw_trans->gw_html_req,
837 gw_trans->gw_conf->got_site_name);
841 khttp_puts(gw_trans->gw_req, search);
843 case(TEMPL_SITEOWNER):
844 if (gw_trans->gw_conf->got_site_owner != NULL &&
845 gw_trans->gw_conf->got_show_site_owner) {
847 gw_html_escape(gw_trans->gw_conf->got_site_owner);
848 if ((asprintf(&site_owner_name_h, site_owner,
853 khttp_puts(gw_trans->gw_req, site_owner_name_h);
854 free(site_owner_name);
855 free(site_owner_name_h);
859 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
861 khttp_puts(gw_trans->gw_req, error->msg);
872 gw_get_repo_description(struct trans *gw_trans, char *dir)
875 char *description = NULL, *d_file = NULL;
878 if (gw_trans->gw_conf->got_show_repo_description == false)
881 if ((asprintf(&d_file, "%s/description", dir)) == -1)
884 if ((f = fopen(d_file, "r")) == NULL)
887 fseek(f, 0, SEEK_END);
889 fseek(f, 0, SEEK_SET);
890 if ((description = calloc(len, sizeof(char *))) == NULL)
893 fread(description, 1, len, f);
898 if ((asprintf(&description, "%s", "")) == -1)
905 gw_get_time_str(time_t committer_time, int ref_tm)
909 char *years = "years ago", *months = "months ago";
910 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
911 char *minutes = "minutes ago", *seconds = "seconds ago";
912 char *now = "right now";
914 char datebuf[BUFFER_SIZE];
918 diff_time = time(NULL) - committer_time;
919 if (diff_time > 60 * 60 * 24 * 365 * 2) {
920 if ((asprintf(&repo_age, "%lld %s",
921 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
923 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
924 if ((asprintf(&repo_age, "%lld %s",
925 (diff_time / 60 / 60 / 24 / (365 / 12)),
928 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
929 if ((asprintf(&repo_age, "%lld %s",
930 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
932 } else if (diff_time > 60 * 60 * 24 * 2) {
933 if ((asprintf(&repo_age, "%lld %s",
934 (diff_time / 60 / 60 / 24), days)) == -1)
936 } else if (diff_time > 60 * 60 * 2) {
937 if ((asprintf(&repo_age, "%lld %s",
938 (diff_time / 60 / 60), hours)) == -1)
940 } else if (diff_time > 60 * 2) {
941 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
944 } else if (diff_time > 2) {
945 if ((asprintf(&repo_age, "%lld %s", diff_time,
949 if ((asprintf(&repo_age, "%s", now)) == -1)
954 if (gmtime_r(&committer_time, &tm) == NULL)
957 s = asctime_r(&tm, datebuf);
961 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
969 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
971 const struct got_error *error = NULL;
972 struct got_object_id *id = NULL;
973 struct got_repository *repo = NULL;
974 struct got_commit_object *commit = NULL;
975 struct got_reflist_head refs;
976 struct got_reflist_entry *re;
977 struct got_reference *head_ref;
978 time_t committer_time = 0, cmp_time = 0;
979 char *repo_age = NULL;
981 if (repo_ref == NULL)
985 if (gw_trans->gw_conf->got_show_repo_age == false) {
986 asprintf(&repo_age, "");
989 error = got_repo_open(&repo, dir, NULL);
993 error = got_ref_list(&refs, repo, repo_ref, got_ref_cmp_by_name,
999 SIMPLEQ_FOREACH(re, &refs, entry) {
1000 refname = got_ref_get_name(re->ref);
1001 error = got_ref_open(&head_ref, repo, refname, 0);
1005 error = got_ref_resolve(&id, repo, head_ref);
1006 got_ref_close(head_ref);
1010 /* here is what breaks tags, so adjust */
1011 error = got_object_open_as_commit(&commit, repo, id);
1016 got_object_commit_get_committer_time(commit);
1018 if (cmp_time < committer_time)
1019 cmp_time = committer_time;
1022 if (cmp_time != 0) {
1023 committer_time = cmp_time;
1024 repo_age = gw_get_time_str(committer_time, ref_tm);
1026 if ((asprintf(&repo_age, "")) == -1)
1028 got_ref_list_free(&refs);
1032 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1039 gw_get_repo_owner(struct trans *gw_trans, char *dir)
1042 char *owner = NULL, *d_file = NULL;
1043 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1044 char *comp, *pos, *buf;
1047 if (gw_trans->gw_conf->got_show_repo_owner == false)
1050 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1053 if ((f = fopen(d_file, "r")) == NULL)
1056 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1059 while ((fgets(buf, BUFFER_SIZE, f)) != NULL) {
1060 if ((pos = strstr(buf, gotweb)) != NULL)
1063 if ((pos = strstr(buf, gitweb)) != NULL)
1071 fgets(buf, BUFFER_SIZE, f);
1072 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1077 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1080 for (i = 0; i < 2; i++) {
1081 owner = strsep(&buf, "\"");
1091 if ((asprintf(&owner, "%s", "")) == -1)
1098 gw_get_clone_url(struct trans *gw_trans, char *dir)
1101 char *url = NULL, *d_file = NULL;
1104 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1107 if ((f = fopen(d_file, "r")) == NULL)
1110 fseek(f, 0, SEEK_END);
1112 fseek(f, 0, SEEK_SET);
1114 if ((url = calloc(len, sizeof(char *))) == NULL)
1117 fread(url, 1, len, f);
1124 gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1125 char *start_commit, int limit, int full_log)
1127 const struct got_error *error;
1128 struct got_repository *repo = NULL;
1129 struct got_reflist_head refs;
1130 struct got_reflist_entry *re;
1131 struct got_commit_object *commit = NULL;
1132 struct got_object_id *id = NULL;
1133 struct got_commit_graph *graph = NULL;
1134 char *logbriefs = NULL, *id_str = NULL, *path = NULL,
1135 *in_repo_path = NULL, *commit_row = NULL, *commit_age = NULL,
1136 *commit_author = NULL, *commit_log = NULL, *commit_log0, *newline,
1137 *logbriefs_navs_html = NULL;
1141 struct buf *diffbuf;
1142 time_t committer_time;
1144 if (search_pattern &&
1145 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB |
1149 SIMPLEQ_INIT(&refs);
1151 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1155 error = buf_alloc(&diffbuf, BUFFER_SIZE);
1159 if (start_commit == NULL) {
1160 struct got_reference *head_ref;
1161 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1164 error = got_ref_resolve(&id, repo, head_ref);
1165 got_ref_close(head_ref);
1168 error = got_object_open_as_commit(&commit, repo, id);
1170 struct got_reference *ref;
1171 error = got_ref_open(&ref, repo, start_commit, 0);
1172 if (error == NULL) {
1174 error = got_ref_resolve(&id, repo, ref);
1178 error = got_object_get_type(&obj_type, repo, id);
1181 if (obj_type == GOT_OBJ_TYPE_TAG) {
1182 struct got_tag_object *tag;
1183 error = got_object_open_as_tag(&tag, repo, id);
1186 if (got_object_tag_get_object_type(tag) !=
1187 GOT_OBJ_TYPE_COMMIT) {
1188 got_object_tag_close(tag);
1189 error = got_error(GOT_ERR_OBJ_TYPE);
1193 id = got_object_id_dup(
1194 got_object_tag_get_object_id(tag));
1196 error = got_error_from_errno(
1197 "got_object_id_dup");
1198 got_object_tag_close(tag);
1201 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1202 error = got_error(GOT_ERR_OBJ_TYPE);
1205 error = got_object_open_as_commit(&commit, repo, id);
1209 if (commit == NULL) {
1210 error = got_repo_match_object_id_prefix(&id,
1211 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1220 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1226 path = in_repo_path;
1229 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1233 error = got_commit_graph_open(&graph, id, path, 0, repo);
1237 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
1242 struct got_commit_object *commit_disp;
1244 error = got_commit_graph_iter_next(&id, graph);
1246 if (error->code == GOT_ERR_ITER_COMPLETED) {
1250 if (error->code != GOT_ERR_ITER_NEED_MORE)
1252 error = got_commit_graph_fetch_commits(graph, 1, repo,
1262 error = got_object_open_as_commit(&commit_disp, repo, id);
1266 if (search_pattern) {
1267 error = match_logmsg(&have_match, id, commit_disp,
1270 got_object_commit_close(commit_disp);
1273 if (have_match == 0) {
1274 got_object_commit_close(commit_disp);
1279 SIMPLEQ_FOREACH(re, &refs, entry) {
1281 struct got_tag_object *tag = NULL;
1284 name = got_ref_get_name(re->ref);
1285 if (strcmp(name, GOT_REF_HEAD) == 0)
1287 if (strncmp(name, "refs/", 5) == 0)
1289 if (strncmp(name, "got/", 4) == 0)
1291 if (strncmp(name, "heads/", 6) == 0)
1293 if (strncmp(name, "remotes/", 8) == 0)
1295 if (strncmp(name, "tags/", 5) == 0) {
1296 error = got_object_open_as_tag(&tag, repo,
1299 if (error->code != GOT_ERR_OBJ_TYPE)
1302 * Ref points at something other
1309 cmp = got_object_id_cmp(tag ?
1310 got_object_tag_get_object_id(tag) : re->id, id);
1312 got_object_tag_close(tag);
1317 got_ref_list_free(&refs);
1320 error = got_object_id_str(&id_str, id);
1325 got_object_commit_get_committer_time(commit_disp);
1329 asprintf(&commit_age, "%s",
1330 gw_get_time_str(committer_time, TM_LONG));
1331 asprintf(&commit_author, "%s",
1332 got_object_commit_get_author(commit_disp));
1333 error = got_object_commit_get_logmsg(&commit_log0,
1336 commit_log = strdup("");
1338 commit_log = gw_html_escape(commit_log0);
1339 asprintf(&logbriefs_navs_html, logbriefs_navs,
1340 gw_trans->repo_name, id_str, gw_trans->repo_name,
1341 id_str, gw_trans->repo_name, id_str,
1342 gw_trans->repo_name, id_str);
1343 asprintf(&commit_row, logs_row, id_str,
1344 gw_html_escape(commit_author), commit_age,
1345 commit_log, logbriefs_navs_html);
1346 error = buf_append(&newsize, diffbuf, commit_row,
1347 strlen(commit_row));
1349 asprintf(&commit_age, "%s",
1350 gw_get_time_str(committer_time, TM_DIFF));
1351 asprintf(&commit_author, "%s",
1352 got_object_commit_get_author(commit_disp));
1353 error = got_object_commit_get_logmsg(&commit_log0,
1356 commit_log = strdup("");
1358 commit_log = commit_log0;
1359 while (*commit_log == '\n')
1361 newline = strchr(commit_log, '\n');
1365 asprintf(&logbriefs_navs_html, logbriefs_navs,
1366 gw_trans->repo_name, id_str, gw_trans->repo_name,
1367 id_str, gw_trans->repo_name, id_str,
1368 gw_trans->repo_name, id_str);
1369 asprintf(&commit_row, logbriefs_row, commit_age,
1370 commit_author, commit_log, logbriefs_navs_html);
1371 error = buf_append(&newsize, diffbuf, commit_row,
1372 strlen(commit_row));
1376 free(commit_author);
1378 free(logbriefs_navs_html);
1382 commit_author = NULL;
1384 logbriefs_navs_html = NULL;
1388 got_object_commit_close(commit_disp);
1389 if (error || (limit && --limit == 0))
1392 logbriefs = strdup(diffbuf->cb_buf);
1393 got_object_commit_close(commit);
1400 error = got_repo_close(repo);
1410 got_repo_close(repo);
1411 got_ref_list_free(&refs);
1415 got_commit_graph_close(graph);
1420 gw_get_repo_tags(struct trans *gw_trans)
1424 asprintf(&tags, tags_row, "30 min ago", "1.0.0", "tag 1.0.0",
1430 gw_get_repo_heads(struct trans *gw_trans)
1434 asprintf(&heads, heads_row, "30 min ago", "master", heads_navs);
1439 gw_get_got_link(struct trans *gw_trans)
1443 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
1444 gw_trans->gw_conf->got_logo)) == -1)
1451 gw_get_site_link(struct trans *gw_trans)
1453 char *link, *repo = "", *action = "";
1455 if (gw_trans->repo_name != NULL)
1456 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
1457 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
1460 if (gw_trans->action_name != NULL)
1461 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
1464 if ((asprintf(&link, site_link, GOTWEB,
1465 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
1472 gw_html_escape(const char *html)
1474 char *escaped_str = NULL, *buf;
1478 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1484 if ((sz = strlen(html)) == 0)
1487 /* only work with BUFFER_SIZE */
1488 if (BUFFER_SIZE < sz)
1491 for (i = 0; i < sz; i++) {
1495 strcat(buf, ">");
1498 strcat(buf, "&");
1501 strcat(buf, "<");
1504 strcat(buf, """);
1507 strcat(buf, "'");
1510 strcat(buf, "<br />");
1516 asprintf(&escaped_str, "%s", buf);
1524 const struct got_error *error = NULL;
1525 struct trans *gw_trans;
1526 struct gw_dir *dir = NULL, *tdir;
1527 const char *page = "index";
1530 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
1533 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
1536 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
1539 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
1542 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
1544 errx(1, "khttp_parse");
1546 if ((gw_trans->gw_conf =
1547 malloc(sizeof(struct gotweb_conf))) == NULL) {
1549 error = got_error_from_errno("malloc");
1553 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
1554 error = got_error_from_errno("pledge");
1558 TAILQ_INIT(&gw_trans->gw_dirs);
1561 gw_trans->repos_total = 0;
1562 gw_trans->repo_path = NULL;
1563 gw_trans->commit = NULL;
1564 gw_trans->mime = KMIME_TEXT_HTML;
1565 gw_trans->gw_tmpl->key = templs;
1566 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
1567 gw_trans->gw_tmpl->arg = gw_trans;
1568 gw_trans->gw_tmpl->cb = gw_template;
1569 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
1573 gw_trans->mime = KMIME_TEXT_PLAIN;
1574 gw_trans->action = GW_ERR;
1575 gw_display_index(gw_trans, error);
1579 error = gw_parse_querystring(gw_trans);
1583 gw_display_index(gw_trans, error);
1587 free(gw_trans->gw_conf->got_repos_path);
1588 free(gw_trans->gw_conf->got_www_path);
1589 free(gw_trans->gw_conf->got_site_name);
1590 free(gw_trans->gw_conf->got_site_owner);
1591 free(gw_trans->gw_conf->got_site_link);
1592 free(gw_trans->gw_conf->got_logo);
1593 free(gw_trans->gw_conf->got_logo_url);
1594 free(gw_trans->gw_conf);
1595 free(gw_trans->commit);
1596 free(gw_trans->repo_path);
1597 free(gw_trans->repo_name);
1598 free(gw_trans->repo_file);
1599 free(gw_trans->action_name);
1601 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
1603 free(dir->description);
1612 khttp_free(gw_trans->gw_req);
1613 return EXIT_SUCCESS;