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;
72 unsigned int repos_total;
87 TAILQ_ENTRY(gw_dir) entry;
120 /* buffer handle, buffer size, and data length */
126 static const char *const templs[TEMPL__MAX] = {
136 static const struct kvalid gw_keys[KEY__MAX] = {
137 { kvalid_stringne, "path" },
138 { kvalid_stringne, "action" },
139 { kvalid_stringne, "commit" },
140 { kvalid_stringne, "file" },
141 { kvalid_int, "page" },
142 { kvalid_stringne, "headref" },
145 static struct gw_dir *gw_init_gw_dir(char *);
147 static char *gw_get_repo_description(struct trans *,
149 static char *gw_get_repo_owner(struct trans *,
151 static char *gw_get_time_str(time_t, int);
152 static char *gw_get_repo_age(struct trans *,
153 char *, char *, int);
154 static char *gw_get_repo_log(struct trans *, const char *,
156 static char *gw_get_repo_tags(struct trans *);
157 static char *gw_get_repo_heads(struct trans *);
158 static char *gw_get_clone_url(struct trans *, char *);
159 static char *gw_get_got_link(struct trans *);
160 static char *gw_get_site_link(struct trans *);
161 static char *gw_html_escape(const char *);
163 static void gw_display_open(struct trans *, enum khttp,
165 static void gw_display_index(struct trans *,
166 const struct got_error *);
168 static int gw_template(size_t, void *);
170 static const struct got_error* apply_unveil(const char *, const char *);
171 static const struct got_error* gw_load_got_paths(struct trans *);
172 static const struct got_error* gw_load_got_path(struct trans *,
174 static const struct got_error* gw_parse_querystring(struct trans *);
175 static const struct got_error* match_logmsg(int *, struct got_object_id *,
176 struct got_commit_object *, regex_t *);
178 static const struct got_error* gw_blame(struct trans *);
179 static const struct got_error* gw_blob(struct trans *);
180 static const struct got_error* gw_blobdiff(struct trans *);
181 static const struct got_error* gw_commit(struct trans *);
182 static const struct got_error* gw_commitdiff(struct trans *);
183 static const struct got_error* gw_history(struct trans *);
184 static const struct got_error* gw_index(struct trans *);
185 static const struct got_error* gw_log(struct trans *);
186 static const struct got_error* gw_raw(struct trans *);
187 static const struct got_error* gw_logbriefs(struct trans *);
188 static const struct got_error* gw_snapshot(struct trans *);
189 static const struct got_error* gw_summary(struct trans *);
190 static const struct got_error* gw_tree(struct trans *);
192 struct gw_query_action {
193 unsigned int func_id;
194 const char *func_name;
195 const struct got_error *(*func_main)(struct trans *);
199 enum gw_query_actions {
216 static struct gw_query_action gw_query_funcs[] = {
217 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
218 { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
219 { GW_BLOBDIFF, "blobdiff", gw_blobdiff, "gw_tmpl/index.tmpl" },
220 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
221 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
222 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
223 { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
224 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
225 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
226 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
227 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
228 { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
229 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
230 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
233 static const struct got_error *
234 apply_unveil(const char *repo_path, const char *repo_file)
236 const struct got_error *err;
238 if (repo_path && repo_file) {
240 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
241 return got_error_from_errno("asprintf unveil");
242 if (unveil(full_path, "r") != 0)
243 return got_error_from_errno2("unveil", full_path);
246 if (repo_path && unveil(repo_path, "r") != 0)
247 return got_error_from_errno2("unveil", repo_path);
249 if (unveil("/tmp", "rwc") != 0)
250 return got_error_from_errno2("unveil", "/tmp");
252 err = got_privsep_unveil_exec_helpers();
256 if (unveil(NULL, NULL) != 0)
257 return got_error_from_errno("unveil");
262 static const struct got_error *
263 gw_blame(struct trans *gw_trans)
265 const struct got_error *error = NULL;
270 static const struct got_error *
271 gw_blob(struct trans *gw_trans)
273 const struct got_error *error = NULL;
278 static const struct got_error *
279 gw_blobdiff(struct trans *gw_trans)
281 const struct got_error *error = NULL;
286 static const struct got_error *
287 gw_commit(struct trans *gw_trans)
289 const struct got_error *error = NULL;
290 char *log, *log_html;
292 error = apply_unveil(gw_trans->gw_dir->path, NULL);
296 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
298 if (log != NULL && strcmp(log, "") != 0) {
299 if ((asprintf(&log_html, log_commit, log)) == -1)
300 return got_error_from_errno("asprintf");
301 khttp_puts(gw_trans->gw_req, log_html);
308 static const struct got_error *
309 gw_commitdiff(struct trans *gw_trans)
311 const struct got_error *error = NULL;
316 static const struct got_error *
317 gw_history(struct trans *gw_trans)
319 const struct got_error *error = NULL;
324 static const struct got_error *
325 gw_index(struct trans *gw_trans)
327 const struct got_error *error = NULL;
328 struct gw_dir *gw_dir = NULL;
329 char *html, *navs, *next, *prev;
330 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
332 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
336 error = gw_load_got_paths(gw_trans);
340 khttp_puts(gw_trans->gw_req, index_projects_header);
342 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
345 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
346 if (gw_trans->page > 0 && (gw_trans->page *
347 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
353 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
354 gw_dir->name, gw_dir->name)) == -1)
355 return got_error_from_errno("asprintf");
357 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
358 gw_dir->description, gw_dir->owner, gw_dir->age,
360 return got_error_from_errno("asprintf");
362 khttp_puts(gw_trans->gw_req, html);
367 if (gw_trans->gw_conf->got_max_repos_display == 0)
370 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
371 khttp_puts(gw_trans->gw_req, np_wrapper_start);
372 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
373 (gw_trans->page > 0) &&
374 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
375 prev_disp == gw_trans->repos_total))
376 khttp_puts(gw_trans->gw_req, np_wrapper_start);
378 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
379 (gw_trans->page > 0) &&
380 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
381 prev_disp == gw_trans->repos_total)) {
382 if ((asprintf(&prev, nav_prev,
383 gw_trans->page - 1)) == -1)
384 return got_error_from_errno("asprintf");
385 khttp_puts(gw_trans->gw_req, prev);
389 khttp_puts(gw_trans->gw_req, div_end);
391 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
392 next_disp == gw_trans->gw_conf->got_max_repos_display &&
393 dir_c != (gw_trans->page + 1) *
394 gw_trans->gw_conf->got_max_repos_display) {
395 if ((asprintf(&next, nav_next,
396 gw_trans->page + 1)) == -1)
397 return got_error_from_errno("calloc");
398 khttp_puts(gw_trans->gw_req, next);
399 khttp_puts(gw_trans->gw_req, div_end);
405 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
406 (gw_trans->page > 0) &&
407 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
408 prev_disp == gw_trans->repos_total))
409 khttp_puts(gw_trans->gw_req, div_end);
416 static const struct got_error *
417 gw_log(struct trans *gw_trans)
419 const struct got_error *error = NULL;
420 char *log, *log_html;
422 error = apply_unveil(gw_trans->gw_dir->path, NULL);
426 log = gw_get_repo_log(gw_trans, NULL, NULL,
427 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
429 if (log != NULL && strcmp(log, "") != 0) {
430 if ((asprintf(&log_html, logs, log)) == -1)
431 return got_error_from_errno("asprintf");
432 khttp_puts(gw_trans->gw_req, log_html);
439 static const struct got_error *
440 gw_raw(struct trans *gw_trans)
442 const struct got_error *error = NULL;
447 static const struct got_error *
448 gw_logbriefs(struct trans *gw_trans)
450 const struct got_error *error = NULL;
451 char *log, *log_html;
453 error = apply_unveil(gw_trans->gw_dir->path, NULL);
457 log = gw_get_repo_log(gw_trans, NULL, NULL,
458 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
460 if (log != NULL && strcmp(log, "") != 0) {
461 if ((asprintf(&log_html, summary_logbriefs,
463 return got_error_from_errno("asprintf");
464 khttp_puts(gw_trans->gw_req, log_html);
471 static const struct got_error *
472 gw_snapshot(struct trans *gw_trans)
474 const struct got_error *error = NULL;
479 static const struct got_error *
480 gw_summary(struct trans *gw_trans)
482 const struct got_error *error = NULL;
483 char *description_html, *repo_owner_html, *repo_age_html,
484 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
487 error = apply_unveil(gw_trans->gw_dir->path, NULL);
491 khttp_puts(gw_trans->gw_req, summary_wrapper);
492 if (gw_trans->gw_conf->got_show_repo_description) {
493 if (gw_trans->gw_dir->description != NULL &&
494 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
495 if ((asprintf(&description_html, description,
496 gw_trans->gw_dir->description)) == -1)
497 return got_error_from_errno("asprintf");
499 khttp_puts(gw_trans->gw_req, description_html);
500 free(description_html);
504 if (gw_trans->gw_conf->got_show_repo_owner) {
505 if (gw_trans->gw_dir->owner != NULL &&
506 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
507 if ((asprintf(&repo_owner_html, repo_owner,
508 gw_trans->gw_dir->owner)) == -1)
509 return got_error_from_errno("asprintf");
511 khttp_puts(gw_trans->gw_req, repo_owner_html);
512 free(repo_owner_html);
516 if (gw_trans->gw_conf->got_show_repo_age) {
517 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
518 "refs/heads", TM_LONG);
519 if (age != NULL && (strcmp(age, "") != 0)) {
520 if ((asprintf(&repo_age_html, last_change, age)) == -1)
521 return got_error_from_errno("asprintf");
523 khttp_puts(gw_trans->gw_req, repo_age_html);
529 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
530 if (gw_trans->gw_dir->url != NULL &&
531 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
532 if ((asprintf(&cloneurl_html, cloneurl,
533 gw_trans->gw_dir->url)) == -1)
534 return got_error_from_errno("asprintf");
536 khttp_puts(gw_trans->gw_req, cloneurl_html);
540 khttp_puts(gw_trans->gw_req, div_end);
542 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
543 tags = gw_get_repo_tags(gw_trans);
544 heads = gw_get_repo_heads(gw_trans);
546 if (log != NULL && strcmp(log, "") != 0) {
547 if ((asprintf(&log_html, summary_logbriefs,
549 return got_error_from_errno("asprintf");
550 khttp_puts(gw_trans->gw_req, log_html);
555 if (tags != NULL && strcmp(tags, "") != 0) {
556 if ((asprintf(&tags_html, summary_tags,
558 return got_error_from_errno("asprintf");
559 khttp_puts(gw_trans->gw_req, tags_html);
564 if (heads != NULL && strcmp(heads, "") != 0) {
565 if ((asprintf(&heads_html, summary_heads,
567 return got_error_from_errno("asprintf");
568 khttp_puts(gw_trans->gw_req, heads_html);
576 static const struct got_error *
577 gw_tree(struct trans *gw_trans)
579 const struct got_error *error = NULL;
580 char *log, *log_html;
582 error = apply_unveil(gw_trans->gw_dir->path, NULL);
586 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
588 if (log != NULL && strcmp(log, "") != 0) {
589 if ((asprintf(&log_html, log_tree, log)) == -1)
590 return got_error_from_errno("asprintf");
591 khttp_puts(gw_trans->gw_req, log_html);
598 static const struct got_error *
599 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
601 const struct got_error *error = NULL;
606 if ((asprintf(&dir_test, "%s/%s/%s",
607 gw_trans->gw_conf->got_repos_path, gw_dir->name,
608 GOTWEB_GIT_DIR)) == -1)
609 return got_error_from_errno("asprintf");
611 dt = opendir(dir_test);
615 gw_dir->path = strdup(dir_test);
620 if ((asprintf(&dir_test, "%s/%s/%s",
621 gw_trans->gw_conf->got_repos_path, gw_dir->name,
622 GOTWEB_GOT_DIR)) == -1)
623 return got_error_from_errno("asprintf");
625 dt = opendir(dir_test);
630 error = got_error(GOT_ERR_NOT_GIT_REPO);
634 if ((asprintf(&dir_test, "%s/%s",
635 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
636 return got_error_from_errno("asprintf");
638 gw_dir->path = strdup(dir_test);
641 gw_dir->description = gw_get_repo_description(gw_trans,
643 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
644 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
646 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
655 static const struct got_error *
656 gw_load_got_paths(struct trans *gw_trans)
658 const struct got_error *error = NULL;
660 struct dirent **sd_dent;
661 struct gw_dir *gw_dir;
663 unsigned int d_cnt, d_i;
665 d = opendir(gw_trans->gw_conf->got_repos_path);
667 error = got_error_from_errno2("opendir",
668 gw_trans->gw_conf->got_repos_path);
672 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
675 error = got_error_from_errno2("scandir",
676 gw_trans->gw_conf->got_repos_path);
680 for (d_i = 0; d_i < d_cnt; d_i++) {
681 if (gw_trans->gw_conf->got_max_repos > 0 &&
682 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
683 break; /* account for parent and self */
685 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
686 strcmp(sd_dent[d_i]->d_name, "..") == 0)
689 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
690 return got_error_from_errno("gw_dir malloc");
692 error = gw_load_got_path(gw_trans, gw_dir);
693 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
698 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
699 !got_path_dir_is_empty(gw_dir->path)) {
700 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
702 gw_trans->repos_total++;
710 static const struct got_error *
711 gw_parse_querystring(struct trans *gw_trans)
713 const struct got_error *error = NULL;
715 struct gw_query_action *action = NULL;
718 if (gw_trans->gw_req->fieldnmap[0]) {
719 error = got_error_from_errno("bad parse");
721 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
722 /* define gw_trans->repo_path */
723 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
724 return got_error_from_errno("asprintf");
726 if ((asprintf(&gw_trans->repo_path, "%s/%s",
727 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
728 return got_error_from_errno("asprintf");
730 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
731 if ((asprintf(&gw_trans->commit, "%s",
733 return got_error_from_errno("asprintf");
735 /* get action and set function */
736 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
737 for (i = 0; i < nitems(gw_query_funcs); i++) {
738 action = &gw_query_funcs[i];
739 if (action->func_name == NULL)
742 if (strcmp(action->func_name,
744 gw_trans->action = i;
745 if ((asprintf(&gw_trans->action_name,
746 "%s", action->func_name)) == -1)
748 got_error_from_errno(
757 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
758 if ((asprintf(&gw_trans->repo_file, "%s",
760 return got_error_from_errno("asprintf");
762 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
763 if ((asprintf(&gw_trans->headref, "%s",
765 return got_error_from_errno("asprintf");
767 if (action == NULL) {
768 error = got_error_from_errno("invalid action");
771 if ((gw_trans->gw_dir =
772 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
773 return got_error_from_errno("gw_dir malloc");
775 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
779 gw_trans->action = GW_INDEX;
781 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
782 gw_trans->page = p->parsed.i;
784 if (gw_trans->action == GW_RAW)
785 gw_trans->mime = KMIME_TEXT_PLAIN;
790 static struct gw_dir *
791 gw_init_gw_dir(char *dir)
793 struct gw_dir *gw_dir;
795 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
798 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
804 static const struct got_error*
805 match_logmsg(int *have_match, struct got_object_id *id,
806 struct got_commit_object *commit, regex_t *regex)
808 const struct got_error *err = NULL;
810 char *id_str = NULL, *logmsg = NULL;
814 err = got_object_id_str(&id_str, id);
818 err = got_object_commit_get_logmsg(&logmsg, commit);
822 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
831 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
833 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
834 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
836 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
838 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
839 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
840 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
841 khttp_body(gw_trans->gw_req);
845 gw_display_index(struct trans *gw_trans, const struct got_error *err)
847 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
848 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
851 khttp_puts(gw_trans->gw_req, err->msg);
853 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
854 gw_query_funcs[gw_trans->action].template);
856 khtml_close(gw_trans->gw_html_req);
860 gw_template(size_t key, void *arg)
862 const struct got_error *error = NULL;
863 struct trans *gw_trans = arg;
864 char *gw_got_link, *gw_site_link;
865 char *site_owner_name, *site_owner_name_h;
869 khttp_puts(gw_trans->gw_req, head);
872 gw_got_link = gw_get_got_link(gw_trans);
873 if (gw_got_link != NULL)
874 khttp_puts(gw_trans->gw_req, gw_got_link);
878 case (TEMPL_SITEPATH):
879 gw_site_link = gw_get_site_link(gw_trans);
880 if (gw_site_link != NULL)
881 khttp_puts(gw_trans->gw_req, gw_site_link);
886 if (gw_trans->gw_conf->got_site_name != NULL)
887 khtml_puts(gw_trans->gw_html_req,
888 gw_trans->gw_conf->got_site_name);
892 khttp_puts(gw_trans->gw_req, search);
894 case(TEMPL_SITEOWNER):
895 if (gw_trans->gw_conf->got_site_owner != NULL &&
896 gw_trans->gw_conf->got_show_site_owner) {
898 gw_html_escape(gw_trans->gw_conf->got_site_owner);
899 if ((asprintf(&site_owner_name_h, site_owner,
904 khttp_puts(gw_trans->gw_req, site_owner_name_h);
905 free(site_owner_name);
906 free(site_owner_name_h);
910 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
912 khttp_puts(gw_trans->gw_req, error->msg);
923 gw_get_repo_description(struct trans *gw_trans, char *dir)
926 char *description = NULL, *d_file = NULL;
929 if (gw_trans->gw_conf->got_show_repo_description == false)
932 if ((asprintf(&d_file, "%s/description", dir)) == -1)
935 if ((f = fopen(d_file, "r")) == NULL)
938 fseek(f, 0, SEEK_END);
940 fseek(f, 0, SEEK_SET);
941 if ((description = calloc(len, sizeof(char *))) == NULL)
944 fread(description, 1, len, f);
949 if ((asprintf(&description, "%s", "")) == -1)
956 gw_get_time_str(time_t committer_time, int ref_tm)
960 char *years = "years ago", *months = "months ago";
961 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
962 char *minutes = "minutes ago", *seconds = "seconds ago";
963 char *now = "right now";
965 char datebuf[BUFFER_SIZE];
969 diff_time = time(NULL) - committer_time;
970 if (diff_time > 60 * 60 * 24 * 365 * 2) {
971 if ((asprintf(&repo_age, "%lld %s",
972 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
974 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
975 if ((asprintf(&repo_age, "%lld %s",
976 (diff_time / 60 / 60 / 24 / (365 / 12)),
979 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
980 if ((asprintf(&repo_age, "%lld %s",
981 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
983 } else if (diff_time > 60 * 60 * 24 * 2) {
984 if ((asprintf(&repo_age, "%lld %s",
985 (diff_time / 60 / 60 / 24), days)) == -1)
987 } else if (diff_time > 60 * 60 * 2) {
988 if ((asprintf(&repo_age, "%lld %s",
989 (diff_time / 60 / 60), hours)) == -1)
991 } else if (diff_time > 60 * 2) {
992 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
995 } else if (diff_time > 2) {
996 if ((asprintf(&repo_age, "%lld %s", diff_time,
1000 if ((asprintf(&repo_age, "%s", now)) == -1)
1005 if (gmtime_r(&committer_time, &tm) == NULL)
1008 s = asctime_r(&tm, datebuf);
1012 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1020 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
1022 const struct got_error *error = NULL;
1023 struct got_object_id *id = NULL;
1024 struct got_repository *repo = NULL;
1025 struct got_commit_object *commit = NULL;
1026 struct got_reflist_head refs;
1027 struct got_reflist_entry *re;
1028 struct got_reference *head_ref;
1029 time_t committer_time = 0, cmp_time = 0;
1030 char *repo_age = NULL;
1032 if (repo_ref == NULL)
1035 SIMPLEQ_INIT(&refs);
1036 if (gw_trans->gw_conf->got_show_repo_age == false) {
1037 if ((asprintf(&repo_age, "")) == -1)
1041 error = got_repo_open(&repo, dir, NULL);
1045 error = got_ref_list(&refs, repo, repo_ref, got_ref_cmp_by_name,
1050 const char *refname;
1051 SIMPLEQ_FOREACH(re, &refs, entry) {
1052 refname = got_ref_get_name(re->ref);
1053 error = got_ref_open(&head_ref, repo, refname, 0);
1057 error = got_ref_resolve(&id, repo, head_ref);
1058 got_ref_close(head_ref);
1062 /* here is what breaks tags, so adjust */
1063 error = got_object_open_as_commit(&commit, repo, id);
1068 got_object_commit_get_committer_time(commit);
1070 if (cmp_time < committer_time)
1071 cmp_time = committer_time;
1074 if (cmp_time != 0) {
1075 committer_time = cmp_time;
1076 repo_age = gw_get_time_str(committer_time, ref_tm);
1078 if ((asprintf(&repo_age, "")) == -1)
1080 got_ref_list_free(&refs);
1084 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1091 gw_get_repo_owner(struct trans *gw_trans, char *dir)
1094 char *owner = NULL, *d_file = NULL;
1095 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1096 char *comp, *pos, *buf;
1099 if (gw_trans->gw_conf->got_show_repo_owner == false)
1102 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1105 if ((f = fopen(d_file, "r")) == NULL)
1108 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1111 while ((fgets(buf, BUFFER_SIZE, f)) != NULL) {
1112 if ((pos = strstr(buf, gotweb)) != NULL)
1115 if ((pos = strstr(buf, gitweb)) != NULL)
1123 fgets(buf, BUFFER_SIZE, f);
1124 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1129 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1132 for (i = 0; i < 2; i++) {
1133 owner = strsep(&buf, "\"");
1143 if ((asprintf(&owner, "%s", "")) == -1)
1150 gw_get_clone_url(struct trans *gw_trans, char *dir)
1153 char *url = NULL, *d_file = NULL;
1156 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1159 if ((f = fopen(d_file, "r")) == NULL)
1162 fseek(f, 0, SEEK_END);
1164 fseek(f, 0, SEEK_SET);
1166 if ((url = calloc(len, sizeof(char *))) == NULL)
1169 fread(url, 1, len, f);
1176 gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1177 char *start_commit, int limit, int log_type)
1179 const struct got_error *error;
1180 struct got_repository *repo = NULL;
1181 struct got_reflist_head refs;
1182 struct got_reflist_entry *re;
1183 struct got_commit_object *commit = NULL;
1184 struct got_object_id *id1 = NULL, *id2 = NULL;
1185 struct got_object_qid *parent_id;
1186 struct got_commit_graph *graph = NULL;
1187 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL,
1188 *path = NULL, *refs_str_disp = NULL,
1189 *refs_str = NULL, *in_repo_path = NULL, *commit_row = NULL,
1190 *commit_commit = NULL, *commit_commit_disp = NULL,
1191 *commit_age_diff = NULL, *commit_age_diff_disp = NULL,
1192 *commit_age_long = NULL, *commit_age_long_disp = NULL,
1193 *commit_author = NULL, *commit_author_disp = NULL,
1194 *commit_committer = NULL, *commit_committer_disp = NULL,
1195 *commit_log = NULL, *commit_log_disp = NULL,
1196 *commit_parent = NULL, *commit_diff_disp = NULL, *commit_log0,
1197 *newline, *logbriefs_navs_html = NULL,
1198 *log_tree_html = NULL, *log_commit_html = NULL;
1202 struct buf *diffbuf = NULL;
1203 time_t committer_time;
1205 if (search_pattern &&
1206 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB |
1210 SIMPLEQ_INIT(&refs);
1212 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1216 error = buf_alloc(&diffbuf, BUFFER_SIZE);
1220 if (start_commit == NULL) {
1221 struct got_reference *head_ref;
1222 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1225 error = got_ref_resolve(&id1, repo, head_ref);
1226 got_ref_close(head_ref);
1229 error = got_object_open_as_commit(&commit, repo, id1);
1231 struct got_reference *ref;
1232 error = got_ref_open(&ref, repo, start_commit, 0);
1233 if (error == NULL) {
1235 error = got_ref_resolve(&id1, repo, ref);
1239 error = got_object_get_type(&obj_type, repo, id1);
1242 if (obj_type == GOT_OBJ_TYPE_TAG) {
1243 struct got_tag_object *tag;
1244 error = got_object_open_as_tag(&tag, repo, id1);
1247 if (got_object_tag_get_object_type(tag) !=
1248 GOT_OBJ_TYPE_COMMIT) {
1249 got_object_tag_close(tag);
1250 error = got_error(GOT_ERR_OBJ_TYPE);
1254 id1 = got_object_id_dup(
1255 got_object_tag_get_object_id(tag));
1257 error = got_error_from_errno(
1258 "got_object_id_dup");
1259 got_object_tag_close(tag);
1262 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1263 error = got_error(GOT_ERR_OBJ_TYPE);
1266 error = got_object_open_as_commit(&commit, repo, id1);
1270 if (commit == NULL) {
1271 error = got_repo_match_object_id_prefix(&id1,
1272 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1276 error = got_repo_match_object_id_prefix(&id1,
1277 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1284 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1290 path = in_repo_path;
1293 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1297 error = got_commit_graph_open(&graph, id1, path, 0, repo);
1301 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1306 error = got_commit_graph_iter_next(&id1, graph);
1308 if (error->code == GOT_ERR_ITER_COMPLETED) {
1312 if (error->code != GOT_ERR_ITER_NEED_MORE)
1314 error = got_commit_graph_fetch_commits(graph, 1, repo,
1324 error = got_object_open_as_commit(&commit, repo, id1);
1328 if (search_pattern) {
1329 error = match_logmsg(&have_match, id1, commit,
1332 got_object_commit_close(commit);
1335 if (have_match == 0) {
1336 got_object_commit_close(commit);
1341 SIMPLEQ_FOREACH(re, &refs, entry) {
1344 struct got_tag_object *tag = NULL;
1347 name = got_ref_get_name(re->ref);
1348 if (strcmp(name, GOT_REF_HEAD) == 0)
1350 if (strncmp(name, "refs/", 5) == 0)
1352 if (strncmp(name, "got/", 4) == 0)
1354 if (strncmp(name, "heads/", 6) == 0)
1356 if (strncmp(name, "remotes/", 8) == 0)
1358 if (strncmp(name, "tags/", 5) == 0) {
1359 error = got_object_open_as_tag(&tag, repo,
1362 if (error->code != GOT_ERR_OBJ_TYPE)
1365 * Ref points at something other
1372 cmp = got_object_id_cmp(tag ?
1373 got_object_tag_get_object_id(tag) : re->id, id1);
1375 got_object_tag_close(tag);
1379 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1380 s ? ", " : "", name)) == -1) {
1381 error = got_error_from_errno("asprintf");
1388 if (refs_str == NULL)
1389 refs_str_disp = strdup("");
1391 if ((asprintf(&refs_str_disp, "(%s)",
1393 error = got_error_from_errno("asprintf");
1400 error = got_object_id_str(&id_str1, id1);
1404 if (gw_trans->action == GW_COMMIT) {
1407 got_object_commit_get_parent_ids(commit));
1408 if (parent_id != NULL) {
1409 id2 = got_object_id_dup(parent_id->id);
1411 error = got_object_id_str(&id_str2, id2);
1415 id_str2 = strdup("/dev/null");
1419 got_object_commit_get_committer_time(commit);
1421 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1422 error = got_error_from_errno("asprintf");
1426 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1428 error = got_error_from_errno("asprintf");
1432 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1433 error = got_error_from_errno("asprintf");
1437 if ((asprintf(&commit_commit_disp, commit_commit_html,
1438 commit_commit, refs_str_disp)) == -1) {
1439 error = got_error_from_errno("asprintf");
1443 if ((asprintf(&commit_age_long, "%s",
1444 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1445 error = got_error_from_errno("asprintf");
1449 if ((asprintf(&commit_age_long_disp, commit_age_html,
1450 commit_age_long)) == -1) {
1451 error = got_error_from_errno("asprintf");
1455 if ((asprintf(&commit_age_diff, "%s",
1456 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1457 error = got_error_from_errno("asprintf");
1461 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1462 commit_age_diff)) == -1) {
1463 error = got_error_from_errno("asprintf");
1467 if ((asprintf(&commit_author, "%s",
1468 got_object_commit_get_author(commit))) == -1) {
1469 error = got_error_from_errno("asprintf");
1473 if ((asprintf(&commit_author_disp, commit_author_html,
1474 gw_html_escape(commit_author))) == -1) {
1475 error = got_error_from_errno("asprintf");
1479 if ((asprintf(&commit_committer, "%s",
1480 got_object_commit_get_committer(commit))) == -1) {
1481 error = got_error_from_errno("asprintf");
1485 if ((asprintf(&commit_committer_disp, commit_committer_html,
1486 gw_html_escape(commit_committer))) == -1) {
1487 error = got_error_from_errno("asprintf");
1491 if (strcmp(commit_author, commit_committer) == 0) {
1492 free(commit_committer_disp);
1493 commit_committer_disp = strdup("");
1496 error = got_object_commit_get_logmsg(&commit_log0, commit);
1502 commit_log = commit_log0;
1503 while (*commit_log == '\n')
1505 newline = strchr(commit_log, '\n');
1509 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1510 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1511 id_str1, gw_trans->repo_name, id_str1,
1512 gw_trans->repo_name, id_str1)) == -1) {
1513 error = got_error_from_errno("asprintf");
1517 if ((asprintf(&commit_row, logbriefs_row,
1518 commit_age_diff, commit_author, commit_log,
1519 logbriefs_navs_html)) == -1) {
1520 error = got_error_from_errno("asprintf");
1524 free(logbriefs_navs_html);
1525 logbriefs_navs_html = NULL;
1528 commit_log = commit_log0;
1529 while (*commit_log == '\n')
1531 newline = strchr(commit_log, '\n');
1535 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1536 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1537 id_str1, gw_trans->repo_name, id_str1,
1538 gw_trans->repo_name, id_str1)) == -1) {
1539 error = got_error_from_errno("asprintf");
1543 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1544 commit_author_disp, commit_committer_disp,
1545 commit_age_long_disp, gw_html_escape(commit_log),
1546 logbriefs_navs_html)) == -1) {
1547 error = got_error_from_errno("asprintf");
1551 free(logbriefs_navs_html);
1552 logbriefs_navs_html = NULL;
1555 commit_log = gw_html_escape(commit_log0);
1557 log_tree_html = strdup("log tree here");
1559 if ((asprintf(&commit_row, log_tree_row, commit_log,
1560 log_tree_html)) == -1) {
1561 error = got_error_from_errno("asprintf");
1565 free(log_tree_html);
1568 commit_log = commit_log0;
1569 while (*commit_log == '\n')
1571 newline = strchr(commit_log, '\n');
1575 if ((asprintf(&commit_log_disp, commit_log_html,
1576 gw_html_escape(commit_log))) == -1) {
1577 error = got_error_from_errno("asprintf");
1581 log_commit_html = strdup("commit here");
1583 if ((asprintf(&commit_row, log_commit_row,
1584 commit_diff_disp, commit_commit_disp,
1585 commit_author_disp, commit_committer_disp,
1586 commit_age_long_disp, commit_log_disp,
1587 log_commit_html)) == -1) {
1588 error = got_error_from_errno("asprintf");
1591 free(commit_log_disp);
1592 free(log_commit_html);
1599 error = buf_append(&newsize, diffbuf, commit_row,
1600 strlen(commit_row));
1602 free(commit_parent);
1603 free(commit_diff_disp);
1604 free(commit_age_diff);
1605 free(commit_age_diff_disp);
1606 free(commit_age_long);
1607 free(commit_age_long_disp);
1608 free(commit_author);
1609 free(commit_author_disp);
1610 free(commit_committer);
1611 free(commit_committer_disp);
1614 free(refs_str_disp);
1622 if (error || (limit && --limit == 0))
1629 logs = strdup(diffbuf->cb_buf);
1633 got_object_commit_close(commit);
1637 got_commit_graph_close(graph);
1639 error = got_repo_close(repo);
1644 khttp_puts(gw_trans->gw_req, "Error: ");
1645 khttp_puts(gw_trans->gw_req, error->msg);
1652 gw_get_repo_tags(struct trans *gw_trans)
1656 asprintf(&tags, tags_row, "30 min ago", "1.0.0", "tag 1.0.0",
1662 gw_get_repo_heads(struct trans *gw_trans)
1666 asprintf(&heads, heads_row, "30 min ago", "master", heads_navs);
1671 gw_get_got_link(struct trans *gw_trans)
1675 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
1676 gw_trans->gw_conf->got_logo)) == -1)
1683 gw_get_site_link(struct trans *gw_trans)
1685 char *link, *repo = "", *action = "";
1687 if (gw_trans->repo_name != NULL)
1688 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
1689 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
1692 if (gw_trans->action_name != NULL)
1693 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
1696 if ((asprintf(&link, site_link, GOTWEB,
1697 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
1704 gw_html_escape(const char *html)
1706 char *escaped_str = NULL, *buf;
1710 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1716 if ((sz = strlen(html)) == 0)
1719 /* only work with BUFFER_SIZE */
1720 if (BUFFER_SIZE < sz)
1723 for (i = 0; i < sz; i++) {
1727 strcat(buf, ">");
1730 strcat(buf, "&");
1733 strcat(buf, "<");
1736 strcat(buf, """);
1739 strcat(buf, "'");
1742 strcat(buf, "<br />");
1750 asprintf(&escaped_str, "%s", buf);
1758 const struct got_error *error = NULL;
1759 struct trans *gw_trans;
1760 struct gw_dir *dir = NULL, *tdir;
1761 const char *page = "index";
1764 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
1767 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
1770 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
1773 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
1776 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
1778 errx(1, "khttp_parse");
1780 if ((gw_trans->gw_conf =
1781 malloc(sizeof(struct gotweb_conf))) == NULL) {
1783 error = got_error_from_errno("malloc");
1787 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
1788 error = got_error_from_errno("pledge");
1792 TAILQ_INIT(&gw_trans->gw_dirs);
1795 gw_trans->repos_total = 0;
1796 gw_trans->repo_path = NULL;
1797 gw_trans->commit = NULL;
1798 gw_trans->headref = strdup(GOT_REF_HEAD);
1799 gw_trans->mime = KMIME_TEXT_HTML;
1800 gw_trans->gw_tmpl->key = templs;
1801 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
1802 gw_trans->gw_tmpl->arg = gw_trans;
1803 gw_trans->gw_tmpl->cb = gw_template;
1804 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
1808 gw_trans->mime = KMIME_TEXT_PLAIN;
1809 gw_trans->action = GW_ERR;
1810 gw_display_index(gw_trans, error);
1814 error = gw_parse_querystring(gw_trans);
1818 gw_display_index(gw_trans, error);
1822 free(gw_trans->gw_conf->got_repos_path);
1823 free(gw_trans->gw_conf->got_www_path);
1824 free(gw_trans->gw_conf->got_site_name);
1825 free(gw_trans->gw_conf->got_site_owner);
1826 free(gw_trans->gw_conf->got_site_link);
1827 free(gw_trans->gw_conf->got_logo);
1828 free(gw_trans->gw_conf->got_logo_url);
1829 free(gw_trans->gw_conf);
1830 free(gw_trans->commit);
1831 free(gw_trans->repo_path);
1832 free(gw_trans->repo_name);
1833 free(gw_trans->repo_file);
1834 free(gw_trans->action_name);
1835 free(gw_trans->headref);
1837 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
1839 free(dir->description);
1848 khttp_free(gw_trans->gw_req);
1849 return EXIT_SUCCESS;