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>
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>
49 #include "gotweb_ui.h"
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
57 struct gw_dir *gw_dir;
58 struct gotweb_conf *gw_conf;
59 struct ktemplate *gw_tmpl;
60 struct khtmlreq *gw_html_req;
69 unsigned int repos_total;
83 TAILQ_ENTRY(gw_dir) entry;
108 static const char *const templs[TEMPL__MAX] = {
118 static const struct kvalid gw_keys[KEY__MAX] = {
119 { kvalid_stringne, "path" },
120 { kvalid_stringne, "action" },
121 { kvalid_stringne, "commit" },
122 { kvalid_stringne, "file" },
123 { kvalid_int, "page" },
126 static struct gw_dir *gw_init_gw_dir(char *);
128 static char *gw_get_repo_description(struct trans *,
130 static char *gw_get_repo_owner(struct trans *,
132 static char *gw_get_repo_age(struct trans *,
133 char *, char *, int);
134 static char *gw_get_repo_shortlog(struct trans *);
135 static char *gw_get_repo_tags(struct trans *);
136 static char *gw_get_repo_heads(struct trans *);
137 static char *gw_get_clone_url(struct trans *, char *);
138 static char *gw_get_got_link(struct trans *);
139 static char *gw_get_site_link(struct trans *);
140 static char *gw_html_escape(const char *);
142 static void gw_display_open(struct trans *, enum khttp,
144 static void gw_display_index(struct trans *,
145 const struct got_error *);
147 static int gw_template(size_t, void *);
149 static const struct got_error* apply_unveil(const char *, const char *);
150 static const struct got_error* gw_load_got_paths(struct trans *);
151 static const struct got_error* gw_load_got_path(struct trans *,
153 static const struct got_error* gw_parse_querystring(struct trans *);
155 static const struct got_error* gw_blame(struct trans *);
156 static const struct got_error* gw_blob(struct trans *);
157 static const struct got_error* gw_blob_diff(struct trans *);
158 static const struct got_error* gw_commit(struct trans *);
159 static const struct got_error* gw_commit_diff(struct trans *);
160 static const struct got_error* gw_history(struct trans *);
161 static const struct got_error* gw_index(struct trans *);
162 static const struct got_error* gw_log(struct trans *);
163 static const struct got_error* gw_raw(struct trans *);
164 static const struct got_error* gw_shortlog(struct trans *);
165 static const struct got_error* gw_snapshot(struct trans *);
166 static const struct got_error* gw_summary(struct trans *);
167 static const struct got_error* gw_tree(struct trans *);
169 struct gw_query_action {
170 unsigned int func_id;
171 const char *func_name;
172 const struct got_error *(*func_main)(struct trans *);
176 enum gw_query_actions {
193 static struct gw_query_action gw_query_funcs[] = {
194 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
195 { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
196 { GW_BLOBDIFF, "blobdiff", gw_blob_diff, "gw_tmpl/index.tmpl" },
197 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
198 { GW_COMMITDIFF, "commit_diff", gw_commit_diff, "gw_tmpl/index.tmpl" },
199 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
200 { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
201 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
202 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
203 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
204 { GW_SHORTLOG, "shortlog", gw_shortlog, "gw_tmpl/index.tmpl" },
205 { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
206 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
207 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
210 static const struct got_error *
211 apply_unveil(const char *repo_path, const char *repo_file)
213 const struct got_error *err;
215 if (repo_path && repo_file) {
217 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
218 return got_error_from_errno("asprintf unveil");
219 if (unveil(full_path, "r") != 0)
220 return got_error_from_errno2("unveil", full_path);
223 if (repo_path && unveil(repo_path, "r") != 0)
224 return got_error_from_errno2("unveil", repo_path);
226 if (unveil("/tmp", "rwc") != 0)
227 return got_error_from_errno2("unveil", "/tmp");
229 err = got_privsep_unveil_exec_helpers();
233 if (unveil(NULL, NULL) != 0)
234 return got_error_from_errno("unveil");
239 static const struct got_error *
240 gw_blame(struct trans *gw_trans)
242 const struct got_error *error = NULL;
247 static const struct got_error *
248 gw_blob(struct trans *gw_trans)
250 const struct got_error *error = NULL;
255 static const struct got_error *
256 gw_blob_diff(struct trans *gw_trans)
258 const struct got_error *error = NULL;
263 static const struct got_error *
264 gw_commit(struct trans *gw_trans)
266 const struct got_error *error = NULL;
271 static const struct got_error *
272 gw_commit_diff(struct trans *gw_trans)
274 const struct got_error *error = NULL;
279 static const struct got_error *
280 gw_history(struct trans *gw_trans)
282 const struct got_error *error = NULL;
287 static const struct got_error *
288 gw_index(struct trans *gw_trans)
290 const struct got_error *error = NULL;
291 struct gw_dir *gw_dir = NULL;
292 char *html, *navs, *next, *prev;
293 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
295 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
299 error = gw_load_got_paths(gw_trans);
303 khttp_puts(gw_trans->gw_req, index_projects_header);
305 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
308 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
309 if (gw_trans->page > 0 && (gw_trans->page *
310 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
316 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
317 gw_dir->name, gw_dir->name)) == -1)
318 return got_error_from_errno("asprintf");
320 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
321 gw_dir->description, gw_dir->owner, gw_dir->age,
323 return got_error_from_errno("asprintf");
325 khttp_puts(gw_trans->gw_req, html);
330 if (gw_trans->gw_conf->got_max_repos_display == 0)
333 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
334 khttp_puts(gw_trans->gw_req, np_wrapper_start);
335 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
336 (gw_trans->page > 0) &&
337 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
338 prev_disp == gw_trans->repos_total))
339 khttp_puts(gw_trans->gw_req, np_wrapper_start);
341 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
342 (gw_trans->page > 0) &&
343 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
344 prev_disp == gw_trans->repos_total)) {
345 if ((asprintf(&prev, nav_prev,
346 gw_trans->page - 1)) == -1)
347 return got_error_from_errno("asprintf");
348 khttp_puts(gw_trans->gw_req, prev);
352 khttp_puts(gw_trans->gw_req, div_end);
354 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
355 next_disp == gw_trans->gw_conf->got_max_repos_display &&
356 dir_c != (gw_trans->page + 1) *
357 gw_trans->gw_conf->got_max_repos_display) {
358 if ((asprintf(&next, nav_next,
359 gw_trans->page + 1)) == -1)
360 return got_error_from_errno("calloc");
361 khttp_puts(gw_trans->gw_req, next);
362 khttp_puts(gw_trans->gw_req, div_end);
368 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
369 (gw_trans->page > 0) &&
370 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
371 prev_disp == gw_trans->repos_total))
372 khttp_puts(gw_trans->gw_req, div_end);
379 static const struct got_error *
380 gw_log(struct trans *gw_trans)
382 const struct got_error *error = NULL;
387 static const struct got_error *
388 gw_raw(struct trans *gw_trans)
390 const struct got_error *error = NULL;
395 static const struct got_error *
396 gw_shortlog(struct trans *gw_trans)
398 const struct got_error *error = NULL;
403 static const struct got_error *
404 gw_snapshot(struct trans *gw_trans)
406 const struct got_error *error = NULL;
411 static const struct got_error *
412 gw_summary(struct trans *gw_trans)
414 const struct got_error *error = NULL;
415 char *description_html, *repo_owner_html, *repo_age_html,
416 *cloneurl_html, *shortlog, *tags, *heads, *shortlog_html,
417 *tags_html, *heads_html, *age;
419 error = apply_unveil(gw_trans->gw_dir->path, NULL);
423 khttp_puts(gw_trans->gw_req, summary_wrapper);
424 if (gw_trans->gw_conf->got_show_repo_description) {
425 if (gw_trans->gw_dir->description != NULL &&
426 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
427 if ((asprintf(&description_html, description,
428 gw_trans->gw_dir->description)) == -1)
429 return got_error_from_errno("asprintf");
431 khttp_puts(gw_trans->gw_req, description_html);
432 free(description_html);
436 if (gw_trans->gw_conf->got_show_repo_owner) {
437 if (gw_trans->gw_dir->owner != NULL &&
438 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
439 if ((asprintf(&repo_owner_html, repo_owner,
440 gw_trans->gw_dir->owner)) == -1)
441 return got_error_from_errno("asprintf");
443 khttp_puts(gw_trans->gw_req, repo_owner_html);
444 free(repo_owner_html);
448 if (gw_trans->gw_conf->got_show_repo_age) {
449 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
450 "refs/heads", TM_LONG);
451 if (age != NULL && (strcmp(age, "") != 0)) {
452 if ((asprintf(&repo_age_html, last_change, age)) == -1)
453 return got_error_from_errno("asprintf");
455 khttp_puts(gw_trans->gw_req, repo_age_html);
461 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
462 if (gw_trans->gw_dir->url != NULL &&
463 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
464 if ((asprintf(&cloneurl_html, cloneurl,
465 gw_trans->gw_dir->url)) == -1)
466 return got_error_from_errno("asprintf");
468 khttp_puts(gw_trans->gw_req, cloneurl_html);
472 khttp_puts(gw_trans->gw_req, div_end);
474 shortlog = gw_get_repo_shortlog(gw_trans);
475 tags = gw_get_repo_tags(gw_trans);
476 heads = gw_get_repo_heads(gw_trans);
478 if (shortlog != NULL && strcmp(shortlog, "") != 0) {
479 if ((asprintf(&shortlog_html, summary_shortlog,
481 return got_error_from_errno("asprintf");
482 khttp_puts(gw_trans->gw_req, shortlog_html);
487 if (tags != NULL && strcmp(tags, "") != 0) {
488 if ((asprintf(&tags_html, summary_tags,
490 return got_error_from_errno("asprintf");
491 khttp_puts(gw_trans->gw_req, tags_html);
496 if (heads != NULL && strcmp(heads, "") != 0) {
497 if ((asprintf(&heads_html, summary_heads,
499 return got_error_from_errno("asprintf");
500 khttp_puts(gw_trans->gw_req, heads_html);
508 static const struct got_error *
509 gw_tree(struct trans *gw_trans)
511 const struct got_error *error = NULL;
516 static const struct got_error *
517 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
519 const struct got_error *error = NULL;
524 if ((asprintf(&dir_test, "%s/%s/%s",
525 gw_trans->gw_conf->got_repos_path, gw_dir->name,
526 GOTWEB_GIT_DIR)) == -1)
527 return got_error_from_errno("asprintf");
529 dt = opendir(dir_test);
533 gw_dir->path = strdup(dir_test);
538 if ((asprintf(&dir_test, "%s/%s/%s",
539 gw_trans->gw_conf->got_repos_path, gw_dir->name,
540 GOTWEB_GOT_DIR)) == -1)
541 return got_error_from_errno("asprintf");
543 dt = opendir(dir_test);
548 error = got_error(GOT_ERR_NOT_GIT_REPO);
552 if ((asprintf(&dir_test, "%s/%s",
553 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
554 return got_error_from_errno("asprintf");
556 gw_dir->path = strdup(dir_test);
559 gw_dir->description = gw_get_repo_description(gw_trans,
561 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
562 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
564 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
573 static const struct got_error *
574 gw_load_got_paths(struct trans *gw_trans)
576 const struct got_error *error = NULL;
578 struct dirent **sd_dent;
579 struct gw_dir *gw_dir;
581 unsigned int d_cnt, d_i;
583 d = opendir(gw_trans->gw_conf->got_repos_path);
585 error = got_error_from_errno2("opendir",
586 gw_trans->gw_conf->got_repos_path);
590 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
593 error = got_error_from_errno2("scandir",
594 gw_trans->gw_conf->got_repos_path);
598 for (d_i = 0; d_i < d_cnt; d_i++) {
599 if (gw_trans->gw_conf->got_max_repos > 0 &&
600 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
601 break; /* account for parent and self */
603 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
604 strcmp(sd_dent[d_i]->d_name, "..") == 0)
607 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
608 return got_error_from_errno("gw_dir malloc");
610 error = gw_load_got_path(gw_trans, gw_dir);
611 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
616 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
617 !got_path_dir_is_empty(gw_dir->path)) {
618 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
620 gw_trans->repos_total++;
628 static const struct got_error *
629 gw_parse_querystring(struct trans *gw_trans)
631 const struct got_error *error = NULL;
633 struct gw_query_action *action = NULL;
636 if (gw_trans->gw_req->fieldnmap[0]) {
637 error = got_error_from_errno("bad parse");
639 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
640 /* define gw_trans->repo_path */
641 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
642 return got_error_from_errno("asprintf");
644 if ((asprintf(&gw_trans->repo_path, "%s/%s",
645 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
646 return got_error_from_errno("asprintf");
648 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
649 if ((asprintf(&gw_trans->commit, "%s",
651 return got_error_from_errno("asprintf");
653 /* get action and set function */
654 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
655 for (i = 0; i < nitems(gw_query_funcs); i++) {
656 action = &gw_query_funcs[i];
657 if (action->func_name == NULL)
660 if (strcmp(action->func_name,
662 gw_trans->action = i;
663 if ((asprintf(&gw_trans->action_name,
664 "%s", action->func_name)) == -1)
666 got_error_from_errno(
675 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
676 if ((asprintf(&gw_trans->repo_file, "%s",
678 return got_error_from_errno("asprintf");
680 if (action == NULL) {
681 error = got_error_from_errno("invalid action");
684 if ((gw_trans->gw_dir =
685 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
686 return got_error_from_errno("gw_dir malloc");
688 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
692 gw_trans->action = GW_INDEX;
694 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
695 gw_trans->page = p->parsed.i;
697 if (gw_trans->action == GW_RAW)
698 gw_trans->mime = KMIME_TEXT_PLAIN;
703 static struct gw_dir *
704 gw_init_gw_dir(char *dir)
706 struct gw_dir *gw_dir;
708 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
711 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
718 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
720 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
721 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
723 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
725 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
726 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
727 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
728 khttp_body(gw_trans->gw_req);
732 gw_display_index(struct trans *gw_trans, const struct got_error *err)
734 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
735 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
738 khttp_puts(gw_trans->gw_req, err->msg);
740 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
741 gw_query_funcs[gw_trans->action].template);
743 khtml_close(gw_trans->gw_html_req);
747 gw_template(size_t key, void *arg)
749 const struct got_error *error = NULL;
750 struct trans *gw_trans = arg;
751 char *gw_got_link, *gw_site_link;
752 char *site_owner_name, *site_owner_name_h;
756 khttp_puts(gw_trans->gw_req, head);
759 gw_got_link = gw_get_got_link(gw_trans);
760 if (gw_got_link != NULL)
761 khttp_puts(gw_trans->gw_req, gw_got_link);
765 case (TEMPL_SITEPATH):
766 gw_site_link = gw_get_site_link(gw_trans);
767 if (gw_site_link != NULL)
768 khttp_puts(gw_trans->gw_req, gw_site_link);
773 if (gw_trans->gw_conf->got_site_name != NULL)
774 khtml_puts(gw_trans->gw_html_req,
775 gw_trans->gw_conf->got_site_name);
779 khttp_puts(gw_trans->gw_req, search);
781 case(TEMPL_SITEOWNER):
782 if (gw_trans->gw_conf->got_site_owner != NULL &&
783 gw_trans->gw_conf->got_show_site_owner) {
785 gw_html_escape(gw_trans->gw_conf->got_site_owner);
786 if ((asprintf(&site_owner_name_h, site_owner,
791 khttp_puts(gw_trans->gw_req, site_owner_name_h);
792 free(site_owner_name);
793 free(site_owner_name_h);
797 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
799 khttp_puts(gw_trans->gw_req, error->msg);
810 gw_get_repo_description(struct trans *gw_trans, char *dir)
813 char *description = NULL, *d_file = NULL;
816 if (gw_trans->gw_conf->got_show_repo_description == false)
819 if ((asprintf(&d_file, "%s/description", dir)) == -1)
822 if ((f = fopen(d_file, "r")) == NULL)
825 fseek(f, 0, SEEK_END);
827 fseek(f, 0, SEEK_SET);
828 if ((description = calloc(len, sizeof(char *))) == NULL)
831 fread(description, 1, len, f);
836 if ((asprintf(&description, "%s", "")) == -1)
843 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
845 const struct got_error *error = NULL;
846 struct got_object_id *id = NULL;
847 struct got_repository *repo = NULL;
848 struct got_commit_object *commit = NULL;
849 struct got_reflist_head refs;
850 struct got_reflist_entry *re;
851 struct got_reference *head_ref;
853 time_t committer_time = 0, cmp_time = 0, diff_time;
854 char *repo_age = NULL, *years = "years ago", *months = "months ago";
855 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
856 char *minutes = "minutes ago", *seconds = "seconds ago";
857 char *now = "right now";
859 char datebuf[BUFFER_SIZE];
861 if (repo_ref == NULL)
865 if (gw_trans->gw_conf->got_show_repo_age == false) {
866 asprintf(&repo_age, "");
869 error = got_repo_open(&repo, dir, NULL);
873 error = got_ref_list(&refs, repo, repo_ref, got_ref_cmp_by_name,
879 SIMPLEQ_FOREACH(re, &refs, entry) {
880 refname = got_ref_get_name(re->ref);
881 error = got_ref_open(&head_ref, repo, refname, 0);
885 error = got_ref_resolve(&id, repo, head_ref);
886 got_ref_close(head_ref);
890 /* here is what breaks tags, so adjust */
891 error = got_object_open_as_commit(&commit, repo, id);
896 got_object_commit_get_committer_time(commit);
898 if (cmp_time < committer_time)
899 cmp_time = committer_time;
903 committer_time = cmp_time;
907 diff_time = time(NULL) - committer_time;
908 if (diff_time > 60 * 60 * 24 * 365 * 2) {
909 if ((asprintf(&repo_age, "%lld %s",
910 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
912 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
913 if ((asprintf(&repo_age, "%lld %s",
914 (diff_time / 60 / 60 / 24 / (365 / 12)),
917 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
918 if ((asprintf(&repo_age, "%lld %s",
919 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
921 } else if (diff_time > 60 * 60 * 24 * 2) {
922 if ((asprintf(&repo_age, "%lld %s",
923 (diff_time / 60 / 60 / 24), days)) == -1)
925 } else if (diff_time > 60 * 60 * 2) {
926 if ((asprintf(&repo_age, "%lld %s",
927 (diff_time / 60 / 60), hours)) == -1)
929 } else if (diff_time > 60 * 2) {
930 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
933 } else if (diff_time > 2) {
934 if ((asprintf(&repo_age, "%lld %s", diff_time,
938 if ((asprintf(&repo_age, "%s", now)) == -1)
944 if (gmtime_r(&committer_time, &tm) == NULL)
947 s = asctime_r(&tm, datebuf);
951 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
954 if ((asprintf(&repo_age, "")) == -1)
961 got_ref_list_free(&refs);
965 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
972 gw_get_repo_owner(struct trans *gw_trans, char *dir)
975 char *owner = NULL, *d_file = NULL;
976 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
977 char *comp, *pos, *buf;
980 if (gw_trans->gw_conf->got_show_repo_owner == false)
983 if ((asprintf(&d_file, "%s/config", dir)) == -1)
986 if ((f = fopen(d_file, "r")) == NULL)
989 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
992 while ((fgets(buf, BUFFER_SIZE, f)) != NULL) {
993 if ((pos = strstr(buf, gotweb)) != NULL)
996 if ((pos = strstr(buf, gitweb)) != NULL)
1004 fgets(buf, BUFFER_SIZE, f);
1005 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1010 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1013 for (i = 0; i < 2; i++) {
1014 owner = strsep(&buf, "\"");
1024 if ((asprintf(&owner, "%s", "")) == -1)
1031 gw_get_clone_url(struct trans *gw_trans, char *dir)
1034 char *url = NULL, *d_file = NULL;
1037 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1040 if ((f = fopen(d_file, "r")) == NULL)
1043 fseek(f, 0, SEEK_END);
1045 fseek(f, 0, SEEK_SET);
1047 if ((url = calloc(len, sizeof(char *))) == NULL)
1050 fread(url, 1, len, f);
1057 gw_get_repo_shortlog(struct trans *gw_trans)
1059 const struct got_error *error;
1060 struct got_repository *repo = NULL;
1061 struct got_reflist_head refs;
1062 struct got_commit_object *commit = NULL;
1063 struct got_object_id *id = NULL;
1064 char *start_commit = NULL, *head_ref_name = NULL;
1065 char *shortlog = NULL;
1067 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1071 if (start_commit == NULL) {
1072 struct got_reference *head_ref;
1073 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1076 error = got_ref_resolve(&id, repo, head_ref);
1077 got_ref_close(head_ref);
1080 error = got_object_open_as_commit(&commit, repo, id);
1082 struct got_reference *ref;
1083 error = got_ref_open(&ref, repo, start_commit, 0);
1084 if (error == NULL) {
1086 error = got_ref_resolve(&id, repo, ref);
1090 error = got_object_get_type(&obj_type, repo, id);
1093 if (obj_type == GOT_OBJ_TYPE_TAG) {
1094 struct got_tag_object *tag;
1095 error = got_object_open_as_tag(&tag, repo, id);
1098 if (got_object_tag_get_object_type(tag) !=
1099 GOT_OBJ_TYPE_COMMIT) {
1100 got_object_tag_close(tag);
1101 error = got_error(GOT_ERR_OBJ_TYPE);
1105 id = got_object_id_dup(
1106 got_object_tag_get_object_id(tag));
1108 error = got_error_from_errno(
1109 "got_object_id_dup");
1110 got_object_tag_close(tag);
1113 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1114 error = got_error(GOT_ERR_OBJ_TYPE);
1117 error = got_object_open_as_commit(&commit, repo, id);
1121 if (commit == NULL) {
1122 error = got_repo_match_object_id_prefix(&id,
1123 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1132 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1136 /* int diff_context = -1, show_patch = 0; */
1137 /* error = print_commits(id, repo, path, show_patch, search_pattern, */
1138 /* diff_context, limit, first_parent_traversal, &refs); */
1139 /* error = print_commit(commit, id, repo, path, 0, */
1141 got_object_commit_close(commit);
1143 asprintf(&shortlog, shortlog_row, "30 min ago", "Flan Author", "this is just a fake ass place holder", shortlog_navs);
1146 free(head_ref_name);
1148 got_repo_close(repo);
1149 got_ref_list_free(&refs);
1154 gw_get_repo_tags(struct trans *gw_trans)
1158 asprintf(&tags, tags_row, "30 min ago", "1.0.0", "tag 1.0.0", tags_navs);
1163 gw_get_repo_heads(struct trans *gw_trans)
1167 asprintf(&heads, heads_row, "30 min ago", "master", heads_navs);
1172 gw_get_got_link(struct trans *gw_trans)
1176 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
1177 gw_trans->gw_conf->got_logo)) == -1)
1184 gw_get_site_link(struct trans *gw_trans)
1186 char *link, *repo = "", *action = "";
1188 if (gw_trans->repo_name != NULL)
1189 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
1190 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
1193 if (gw_trans->action_name != NULL)
1194 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
1197 if ((asprintf(&link, site_link, GOTWEB,
1198 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
1205 gw_html_escape(const char *html)
1207 char *escaped_str = NULL, *buf;
1211 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1217 if ((sz = strlen(html)) == 0)
1220 /* only work with BUFFER_SIZE */
1221 if (BUFFER_SIZE < sz)
1224 for (i = 0; i < sz; i++) {
1228 strcat(buf, ">");
1231 strcat(buf, "&");
1234 strcat(buf, "<");
1237 strcat(buf, """);
1240 strcat(buf, "'");
1243 strcat(buf, "<br />");
1249 asprintf(&escaped_str, "%s", buf);
1257 const struct got_error *error = NULL;
1258 struct trans *gw_trans;
1259 struct gw_dir *dir = NULL, *tdir;
1260 const char *page = "index";
1261 bool gw_malloc = true;
1263 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
1266 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
1269 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
1272 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
1275 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
1277 errx(1, "khttp_parse");
1279 if ((gw_trans->gw_conf =
1280 malloc(sizeof(struct gotweb_conf))) == NULL) {
1282 error = got_error_from_errno("malloc");
1286 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
1287 error = got_error_from_errno("pledge");
1291 TAILQ_INIT(&gw_trans->gw_dirs);
1294 gw_trans->repos_total = 0;
1295 gw_trans->repo_path = NULL;
1296 gw_trans->commit = NULL;
1297 gw_trans->mime = KMIME_TEXT_HTML;
1298 gw_trans->gw_tmpl->key = templs;
1299 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
1300 gw_trans->gw_tmpl->arg = gw_trans;
1301 gw_trans->gw_tmpl->cb = gw_template;
1302 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
1306 gw_trans->mime = KMIME_TEXT_PLAIN;
1307 gw_trans->action = GW_ERR;
1308 gw_display_index(gw_trans, error);
1312 error = gw_parse_querystring(gw_trans);
1316 gw_display_index(gw_trans, error);
1320 free(gw_trans->gw_conf->got_repos_path);
1321 free(gw_trans->gw_conf->got_www_path);
1322 free(gw_trans->gw_conf->got_site_name);
1323 free(gw_trans->gw_conf->got_site_owner);
1324 free(gw_trans->gw_conf->got_site_link);
1325 free(gw_trans->gw_conf->got_logo);
1326 free(gw_trans->gw_conf->got_logo_url);
1327 free(gw_trans->gw_conf);
1328 free(gw_trans->commit);
1329 free(gw_trans->repo_path);
1330 free(gw_trans->repo_name);
1331 free(gw_trans->repo_file);
1332 free(gw_trans->action_name);
1334 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
1336 free(dir->description);
1345 khttp_free(gw_trans->gw_req);
1346 return EXIT_SUCCESS;