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;
127 /* buffer handle, buffer size, and data length */
133 static const char *const templs[TEMPL__MAX] = {
143 static const struct kvalid gw_keys[KEY__MAX] = {
144 { kvalid_stringne, "path" },
145 { kvalid_stringne, "action" },
146 { kvalid_stringne, "commit" },
147 { kvalid_stringne, "file" },
148 { kvalid_int, "page" },
149 { kvalid_stringne, "headref" },
152 static struct gw_dir *gw_init_gw_dir(char *);
154 static char *gw_get_repo_description(struct trans *,
156 static char *gw_get_repo_owner(struct trans *,
158 static char *gw_get_time_str(time_t, int);
159 static char *gw_get_repo_age(struct trans *,
160 char *, char *, int);
161 static char *gw_get_repo_log(struct trans *, const char *,
163 static char *gw_get_repo_tags(struct trans *, int, int);
164 static char *gw_get_repo_heads(struct trans *);
165 static char *gw_get_clone_url(struct trans *, char *);
166 static char *gw_get_got_link(struct trans *);
167 static char *gw_get_site_link(struct trans *);
168 static char *gw_html_escape(const char *);
170 static void gw_display_open(struct trans *, enum khttp,
172 static void gw_display_index(struct trans *,
173 const struct got_error *);
175 static int gw_template(size_t, void *);
177 static const struct got_error* apply_unveil(const char *, const char *);
178 static const struct got_error* cmp_tags(void *, int *,
179 struct got_reference *,
180 struct got_reference *);
181 static const struct got_error* gw_load_got_paths(struct trans *);
182 static const struct got_error* gw_load_got_path(struct trans *,
184 static const struct got_error* gw_parse_querystring(struct trans *);
185 static const struct got_error* match_logmsg(int *, struct got_object_id *,
186 struct got_commit_object *, regex_t *);
188 static const struct got_error* gw_blame(struct trans *);
189 static const struct got_error* gw_blob(struct trans *);
190 static const struct got_error* gw_blobdiff(struct trans *);
191 static const struct got_error* gw_commit(struct trans *);
192 static const struct got_error* gw_commitdiff(struct trans *);
193 static const struct got_error* gw_history(struct trans *);
194 static const struct got_error* gw_index(struct trans *);
195 static const struct got_error* gw_log(struct trans *);
196 static const struct got_error* gw_raw(struct trans *);
197 static const struct got_error* gw_logbriefs(struct trans *);
198 static const struct got_error* gw_snapshot(struct trans *);
199 static const struct got_error* gw_summary(struct trans *);
200 static const struct got_error* gw_tree(struct trans *);
202 struct gw_query_action {
203 unsigned int func_id;
204 const char *func_name;
205 const struct got_error *(*func_main)(struct trans *);
209 enum gw_query_actions {
226 static struct gw_query_action gw_query_funcs[] = {
227 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
228 { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
229 { GW_BLOBDIFF, "blobdiff", gw_blobdiff, "gw_tmpl/index.tmpl" },
230 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
231 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
232 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
233 { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
234 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
235 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
236 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
237 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
238 { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
239 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
240 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
243 static const struct got_error *
244 apply_unveil(const char *repo_path, const char *repo_file)
246 const struct got_error *err;
248 if (repo_path && repo_file) {
250 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
251 return got_error_from_errno("asprintf unveil");
252 if (unveil(full_path, "r") != 0)
253 return got_error_from_errno2("unveil", full_path);
256 if (repo_path && unveil(repo_path, "r") != 0)
257 return got_error_from_errno2("unveil", repo_path);
259 if (unveil("/tmp", "rwc") != 0)
260 return got_error_from_errno2("unveil", "/tmp");
262 err = got_privsep_unveil_exec_helpers();
266 if (unveil(NULL, NULL) != 0)
267 return got_error_from_errno("unveil");
272 static const struct got_error *
273 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
274 struct got_reference *ref2)
276 const struct got_error *err = NULL;
277 struct got_repository *repo = arg;
278 struct got_object_id *id1, *id2 = NULL;
279 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
284 err = got_ref_resolve(&id1, repo, ref1);
287 err = got_object_open_as_tag(&tag1, repo, id1);
291 err = got_ref_resolve(&id2, repo, ref2);
294 err = got_object_open_as_tag(&tag2, repo, id2);
298 time1 = got_object_tag_get_tagger_time(tag1);
299 time2 = got_object_tag_get_tagger_time(tag2);
301 /* Put latest tags first. */
304 else if (time1 > time2)
307 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
312 got_object_tag_close(tag1);
314 got_object_tag_close(tag2);
318 static const struct got_error *
319 gw_blame(struct trans *gw_trans)
321 const struct got_error *error = NULL;
326 static const struct got_error *
327 gw_blob(struct trans *gw_trans)
329 const struct got_error *error = NULL;
334 static const struct got_error *
335 gw_blobdiff(struct trans *gw_trans)
337 const struct got_error *error = NULL;
342 static const struct got_error *
343 gw_commit(struct trans *gw_trans)
345 const struct got_error *error = NULL;
346 char *log, *log_html;
348 error = apply_unveil(gw_trans->gw_dir->path, NULL);
352 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
354 if (log != NULL && strcmp(log, "") != 0) {
355 if ((asprintf(&log_html, log_commit, log)) == -1)
356 return got_error_from_errno("asprintf");
357 khttp_puts(gw_trans->gw_req, log_html);
364 static const struct got_error *
365 gw_commitdiff(struct trans *gw_trans)
367 const struct got_error *error = NULL;
368 char *log, *log_html;
370 error = apply_unveil(gw_trans->gw_dir->path, NULL);
374 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
376 if (log != NULL && strcmp(log, "") != 0) {
377 if ((asprintf(&log_html, log_diff, log)) == -1)
378 return got_error_from_errno("asprintf");
379 khttp_puts(gw_trans->gw_req, log_html);
386 static const struct got_error *
387 gw_history(struct trans *gw_trans)
389 const struct got_error *error = NULL;
394 static const struct got_error *
395 gw_index(struct trans *gw_trans)
397 const struct got_error *error = NULL;
398 struct gw_dir *gw_dir = NULL;
399 char *html, *navs, *next, *prev;
400 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
402 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
406 error = gw_load_got_paths(gw_trans);
410 khttp_puts(gw_trans->gw_req, index_projects_header);
412 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
415 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
416 if (gw_trans->page > 0 && (gw_trans->page *
417 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
423 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
424 gw_dir->name, gw_dir->name)) == -1)
425 return got_error_from_errno("asprintf");
427 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
428 gw_dir->description, gw_dir->owner, gw_dir->age,
430 return got_error_from_errno("asprintf");
432 khttp_puts(gw_trans->gw_req, html);
437 if (gw_trans->gw_conf->got_max_repos_display == 0)
440 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
441 khttp_puts(gw_trans->gw_req, np_wrapper_start);
442 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
443 (gw_trans->page > 0) &&
444 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
445 prev_disp == gw_trans->repos_total))
446 khttp_puts(gw_trans->gw_req, np_wrapper_start);
448 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
449 (gw_trans->page > 0) &&
450 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
451 prev_disp == gw_trans->repos_total)) {
452 if ((asprintf(&prev, nav_prev,
453 gw_trans->page - 1)) == -1)
454 return got_error_from_errno("asprintf");
455 khttp_puts(gw_trans->gw_req, prev);
459 khttp_puts(gw_trans->gw_req, div_end);
461 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
462 next_disp == gw_trans->gw_conf->got_max_repos_display &&
463 dir_c != (gw_trans->page + 1) *
464 gw_trans->gw_conf->got_max_repos_display) {
465 if ((asprintf(&next, nav_next,
466 gw_trans->page + 1)) == -1)
467 return got_error_from_errno("calloc");
468 khttp_puts(gw_trans->gw_req, next);
469 khttp_puts(gw_trans->gw_req, div_end);
475 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
476 (gw_trans->page > 0) &&
477 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
478 prev_disp == gw_trans->repos_total))
479 khttp_puts(gw_trans->gw_req, div_end);
486 static const struct got_error *
487 gw_log(struct trans *gw_trans)
489 const struct got_error *error = NULL;
490 char *log, *log_html;
492 error = apply_unveil(gw_trans->gw_dir->path, NULL);
496 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
497 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
499 if (log != NULL && strcmp(log, "") != 0) {
500 if ((asprintf(&log_html, logs, log)) == -1)
501 return got_error_from_errno("asprintf");
502 khttp_puts(gw_trans->gw_req, log_html);
509 static const struct got_error *
510 gw_raw(struct trans *gw_trans)
512 const struct got_error *error = NULL;
517 static const struct got_error *
518 gw_logbriefs(struct trans *gw_trans)
520 const struct got_error *error = NULL;
521 char *log, *log_html;
523 error = apply_unveil(gw_trans->gw_dir->path, NULL);
527 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
528 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
530 if (log != NULL && strcmp(log, "") != 0) {
531 if ((asprintf(&log_html, summary_logbriefs,
533 return got_error_from_errno("asprintf");
534 khttp_puts(gw_trans->gw_req, log_html);
541 static const struct got_error *
542 gw_snapshot(struct trans *gw_trans)
544 const struct got_error *error = NULL;
549 static const struct got_error *
550 gw_summary(struct trans *gw_trans)
552 const struct got_error *error = NULL;
553 char *description_html, *repo_owner_html, *repo_age_html,
554 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
557 error = apply_unveil(gw_trans->gw_dir->path, NULL);
561 khttp_puts(gw_trans->gw_req, summary_wrapper);
562 if (gw_trans->gw_conf->got_show_repo_description) {
563 if (gw_trans->gw_dir->description != NULL &&
564 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
565 if ((asprintf(&description_html, description,
566 gw_trans->gw_dir->description)) == -1)
567 return got_error_from_errno("asprintf");
569 khttp_puts(gw_trans->gw_req, description_html);
570 free(description_html);
574 if (gw_trans->gw_conf->got_show_repo_owner) {
575 if (gw_trans->gw_dir->owner != NULL &&
576 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
577 if ((asprintf(&repo_owner_html, repo_owner,
578 gw_trans->gw_dir->owner)) == -1)
579 return got_error_from_errno("asprintf");
581 khttp_puts(gw_trans->gw_req, repo_owner_html);
582 free(repo_owner_html);
586 if (gw_trans->gw_conf->got_show_repo_age) {
587 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
588 "refs/heads", TM_LONG);
589 if (age != NULL && (strcmp(age, "") != 0)) {
590 if ((asprintf(&repo_age_html, last_change, age)) == -1)
591 return got_error_from_errno("asprintf");
593 khttp_puts(gw_trans->gw_req, repo_age_html);
599 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
600 if (gw_trans->gw_dir->url != NULL &&
601 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
602 if ((asprintf(&cloneurl_html, cloneurl,
603 gw_trans->gw_dir->url)) == -1)
604 return got_error_from_errno("asprintf");
606 khttp_puts(gw_trans->gw_req, cloneurl_html);
610 khttp_puts(gw_trans->gw_req, div_end);
612 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
613 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
614 heads = gw_get_repo_heads(gw_trans);
616 if (log != NULL && strcmp(log, "") != 0) {
617 if ((asprintf(&log_html, summary_logbriefs,
619 return got_error_from_errno("asprintf");
620 khttp_puts(gw_trans->gw_req, log_html);
625 if (tags != NULL && strcmp(tags, "") != 0) {
626 if ((asprintf(&tags_html, summary_tags,
628 return got_error_from_errno("asprintf");
629 khttp_puts(gw_trans->gw_req, tags_html);
634 if (heads != NULL && strcmp(heads, "") != 0) {
635 if ((asprintf(&heads_html, summary_heads,
637 return got_error_from_errno("asprintf");
638 khttp_puts(gw_trans->gw_req, heads_html);
646 static const struct got_error *
647 gw_tree(struct trans *gw_trans)
649 const struct got_error *error = NULL;
650 char *log, *log_html;
652 error = apply_unveil(gw_trans->gw_dir->path, NULL);
656 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
658 if (log != NULL && strcmp(log, "") != 0) {
659 if ((asprintf(&log_html, log_tree, log)) == -1)
660 return got_error_from_errno("asprintf");
661 khttp_puts(gw_trans->gw_req, log_html);
668 static const struct got_error *
669 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
671 const struct got_error *error = NULL;
676 if ((asprintf(&dir_test, "%s/%s/%s",
677 gw_trans->gw_conf->got_repos_path, gw_dir->name,
678 GOTWEB_GIT_DIR)) == -1)
679 return got_error_from_errno("asprintf");
681 dt = opendir(dir_test);
685 gw_dir->path = strdup(dir_test);
690 if ((asprintf(&dir_test, "%s/%s/%s",
691 gw_trans->gw_conf->got_repos_path, gw_dir->name,
692 GOTWEB_GOT_DIR)) == -1)
693 return got_error_from_errno("asprintf");
695 dt = opendir(dir_test);
700 error = got_error(GOT_ERR_NOT_GIT_REPO);
704 if ((asprintf(&dir_test, "%s/%s",
705 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
706 return got_error_from_errno("asprintf");
708 gw_dir->path = strdup(dir_test);
711 gw_dir->description = gw_get_repo_description(gw_trans,
713 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
714 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
716 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
725 static const struct got_error *
726 gw_load_got_paths(struct trans *gw_trans)
728 const struct got_error *error = NULL;
730 struct dirent **sd_dent;
731 struct gw_dir *gw_dir;
733 unsigned int d_cnt, d_i;
735 d = opendir(gw_trans->gw_conf->got_repos_path);
737 error = got_error_from_errno2("opendir",
738 gw_trans->gw_conf->got_repos_path);
742 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
745 error = got_error_from_errno2("scandir",
746 gw_trans->gw_conf->got_repos_path);
750 for (d_i = 0; d_i < d_cnt; d_i++) {
751 if (gw_trans->gw_conf->got_max_repos > 0 &&
752 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
753 break; /* account for parent and self */
755 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
756 strcmp(sd_dent[d_i]->d_name, "..") == 0)
759 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
760 return got_error_from_errno("gw_dir malloc");
762 error = gw_load_got_path(gw_trans, gw_dir);
763 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
768 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
769 !got_path_dir_is_empty(gw_dir->path)) {
770 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
772 gw_trans->repos_total++;
780 static const struct got_error *
781 gw_parse_querystring(struct trans *gw_trans)
783 const struct got_error *error = NULL;
785 struct gw_query_action *action = NULL;
788 if (gw_trans->gw_req->fieldnmap[0]) {
789 error = got_error_from_errno("bad parse");
791 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
792 /* define gw_trans->repo_path */
793 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
794 return got_error_from_errno("asprintf");
796 if ((asprintf(&gw_trans->repo_path, "%s/%s",
797 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
798 return got_error_from_errno("asprintf");
800 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
801 if ((asprintf(&gw_trans->commit, "%s",
803 return got_error_from_errno("asprintf");
805 /* get action and set function */
806 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
807 for (i = 0; i < nitems(gw_query_funcs); i++) {
808 action = &gw_query_funcs[i];
809 if (action->func_name == NULL)
812 if (strcmp(action->func_name,
814 gw_trans->action = i;
815 if ((asprintf(&gw_trans->action_name,
816 "%s", action->func_name)) == -1)
818 got_error_from_errno(
827 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
828 if ((asprintf(&gw_trans->repo_file, "%s",
830 return got_error_from_errno("asprintf");
832 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
833 if ((asprintf(&gw_trans->headref, "%s",
835 return got_error_from_errno("asprintf");
837 if (action == NULL) {
838 error = got_error_from_errno("invalid action");
841 if ((gw_trans->gw_dir =
842 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
843 return got_error_from_errno("gw_dir malloc");
845 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
849 gw_trans->action = GW_INDEX;
851 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
852 gw_trans->page = p->parsed.i;
854 if (gw_trans->action == GW_RAW)
855 gw_trans->mime = KMIME_TEXT_PLAIN;
860 static struct gw_dir *
861 gw_init_gw_dir(char *dir)
863 struct gw_dir *gw_dir;
865 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
868 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
874 static const struct got_error*
875 match_logmsg(int *have_match, struct got_object_id *id,
876 struct got_commit_object *commit, regex_t *regex)
878 const struct got_error *err = NULL;
880 char *id_str = NULL, *logmsg = NULL;
884 err = got_object_id_str(&id_str, id);
888 err = got_object_commit_get_logmsg(&logmsg, commit);
892 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
901 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
903 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
904 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
906 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
908 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
909 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
910 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
911 khttp_body(gw_trans->gw_req);
915 gw_display_index(struct trans *gw_trans, const struct got_error *err)
917 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
918 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
921 khttp_puts(gw_trans->gw_req, err->msg);
923 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
924 gw_query_funcs[gw_trans->action].template);
926 khtml_close(gw_trans->gw_html_req);
930 gw_template(size_t key, void *arg)
932 const struct got_error *error = NULL;
933 struct trans *gw_trans = arg;
934 char *gw_got_link, *gw_site_link;
935 char *site_owner_name, *site_owner_name_h;
939 khttp_puts(gw_trans->gw_req, head);
942 gw_got_link = gw_get_got_link(gw_trans);
943 if (gw_got_link != NULL)
944 khttp_puts(gw_trans->gw_req, gw_got_link);
948 case (TEMPL_SITEPATH):
949 gw_site_link = gw_get_site_link(gw_trans);
950 if (gw_site_link != NULL)
951 khttp_puts(gw_trans->gw_req, gw_site_link);
956 if (gw_trans->gw_conf->got_site_name != NULL)
957 khtml_puts(gw_trans->gw_html_req,
958 gw_trans->gw_conf->got_site_name);
962 khttp_puts(gw_trans->gw_req, search);
964 case(TEMPL_SITEOWNER):
965 if (gw_trans->gw_conf->got_site_owner != NULL &&
966 gw_trans->gw_conf->got_show_site_owner) {
968 gw_html_escape(gw_trans->gw_conf->got_site_owner);
969 if ((asprintf(&site_owner_name_h, site_owner,
974 khttp_puts(gw_trans->gw_req, site_owner_name_h);
975 free(site_owner_name);
976 free(site_owner_name_h);
980 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
982 khttp_puts(gw_trans->gw_req, error->msg);
993 gw_get_repo_description(struct trans *gw_trans, char *dir)
996 char *description = NULL, *d_file = NULL;
999 if (gw_trans->gw_conf->got_show_repo_description == false)
1002 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1005 if ((f = fopen(d_file, "r")) == NULL)
1008 fseek(f, 0, SEEK_END);
1010 fseek(f, 0, SEEK_SET);
1011 if ((description = calloc(len, sizeof(char *))) == NULL)
1014 fread(description, 1, len, f);
1019 if ((asprintf(&description, "%s", "")) == -1)
1026 gw_get_time_str(time_t committer_time, int ref_tm)
1030 char *years = "years ago", *months = "months ago";
1031 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1032 char *minutes = "minutes ago", *seconds = "seconds ago";
1033 char *now = "right now";
1035 char datebuf[BUFFER_SIZE];
1039 diff_time = time(NULL) - committer_time;
1040 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1041 if ((asprintf(&repo_age, "%lld %s",
1042 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1044 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1045 if ((asprintf(&repo_age, "%lld %s",
1046 (diff_time / 60 / 60 / 24 / (365 / 12)),
1049 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1050 if ((asprintf(&repo_age, "%lld %s",
1051 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1053 } else if (diff_time > 60 * 60 * 24 * 2) {
1054 if ((asprintf(&repo_age, "%lld %s",
1055 (diff_time / 60 / 60 / 24), days)) == -1)
1057 } else if (diff_time > 60 * 60 * 2) {
1058 if ((asprintf(&repo_age, "%lld %s",
1059 (diff_time / 60 / 60), hours)) == -1)
1061 } else if (diff_time > 60 * 2) {
1062 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1065 } else if (diff_time > 2) {
1066 if ((asprintf(&repo_age, "%lld %s", diff_time,
1070 if ((asprintf(&repo_age, "%s", now)) == -1)
1075 if (gmtime_r(&committer_time, &tm) == NULL)
1078 s = asctime_r(&tm, datebuf);
1082 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1090 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
1092 const struct got_error *error = NULL;
1093 struct got_object_id *id = NULL;
1094 struct got_repository *repo = NULL;
1095 struct got_commit_object *commit = NULL;
1096 struct got_reflist_head refs;
1097 struct got_reflist_entry *re;
1098 struct got_reference *head_ref;
1100 time_t committer_time = 0, cmp_time = 0;
1101 const char *refname;
1102 char *repo_age = NULL;
1104 if (repo_ref == NULL)
1107 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1110 SIMPLEQ_INIT(&refs);
1111 if (gw_trans->gw_conf->got_show_repo_age == false) {
1112 if ((asprintf(&repo_age, "")) == -1)
1117 error = got_repo_open(&repo, dir, NULL);
1122 error = got_ref_list(&refs, repo, "refs/heads",
1123 got_ref_cmp_by_name, NULL);
1125 error = got_ref_list(&refs, repo, repo_ref,
1126 got_ref_cmp_by_name, NULL);
1130 SIMPLEQ_FOREACH(re, &refs, entry) {
1132 refname = strdup(repo_ref);
1134 refname = got_ref_get_name(re->ref);
1135 error = got_ref_open(&head_ref, repo, refname, 0);
1139 error = got_ref_resolve(&id, repo, head_ref);
1140 got_ref_close(head_ref);
1144 error = got_object_open_as_commit(&commit, repo, id);
1149 got_object_commit_get_committer_time(commit);
1151 if (cmp_time < committer_time)
1152 cmp_time = committer_time;
1155 if (cmp_time != 0) {
1156 committer_time = cmp_time;
1157 repo_age = gw_get_time_str(committer_time, ref_tm);
1159 if ((asprintf(&repo_age, "")) == -1)
1161 got_ref_list_free(&refs);
1165 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1172 gw_get_repo_owner(struct trans *gw_trans, char *dir)
1175 char *owner = NULL, *d_file = NULL;
1176 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1177 char *comp, *pos, *buf;
1180 if (gw_trans->gw_conf->got_show_repo_owner == false)
1183 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1186 if ((f = fopen(d_file, "r")) == NULL)
1189 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1192 while ((fgets(buf, BUFFER_SIZE, f)) != NULL) {
1193 if ((pos = strstr(buf, gotweb)) != NULL)
1196 if ((pos = strstr(buf, gitweb)) != NULL)
1204 fgets(buf, BUFFER_SIZE, f);
1205 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1210 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1213 for (i = 0; i < 2; i++) {
1214 owner = strsep(&buf, "\"");
1224 if ((asprintf(&owner, "%s", "")) == -1)
1231 gw_get_clone_url(struct trans *gw_trans, char *dir)
1234 char *url = NULL, *d_file = NULL;
1237 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1240 if ((f = fopen(d_file, "r")) == NULL)
1243 fseek(f, 0, SEEK_END);
1245 fseek(f, 0, SEEK_SET);
1247 if ((url = calloc(len, sizeof(char *))) == NULL)
1250 fread(url, 1, len, f);
1257 gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1258 char *start_commit, int limit, int log_type)
1260 const struct got_error *error;
1261 struct got_repository *repo = NULL;
1262 struct got_reflist_head refs;
1263 struct got_reflist_entry *re;
1264 struct got_commit_object *commit = NULL;
1265 struct got_object_id *id1 = NULL, *id2 = NULL;
1266 struct got_object_qid *parent_id;
1267 struct got_commit_graph *graph = NULL;
1268 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1269 *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1270 *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1271 *commit_commit_disp = NULL, *commit_age_diff = NULL,
1272 *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1273 *commit_age_long_disp = NULL, *commit_author = NULL,
1274 *commit_author_disp = NULL, *commit_committer = NULL,
1275 *commit_committer_disp = NULL, *commit_log = NULL,
1276 *commit_log_disp = NULL, *commit_parent = NULL,
1277 *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1278 *log_tree_html = NULL, *log_commit_html = NULL,
1279 *log_diff_html = NULL, *commit_tree = NULL,
1280 *commit_tree_disp = NULL;
1281 char *commit_log0, *newline;
1285 struct buf *diffbuf = NULL;
1286 time_t committer_time;
1288 if (search_pattern &&
1289 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB |
1293 SIMPLEQ_INIT(&refs);
1295 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1299 error = buf_alloc(&diffbuf, BUFFER_SIZE);
1303 if (start_commit == NULL) {
1304 struct got_reference *head_ref;
1305 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1308 error = got_ref_resolve(&id1, repo, head_ref);
1309 got_ref_close(head_ref);
1312 error = got_object_open_as_commit(&commit, repo, id1);
1314 struct got_reference *ref;
1315 error = got_ref_open(&ref, repo, start_commit, 0);
1316 if (error == NULL) {
1318 error = got_ref_resolve(&id1, repo, ref);
1322 error = got_object_get_type(&obj_type, repo, id1);
1325 if (obj_type == GOT_OBJ_TYPE_TAG) {
1326 struct got_tag_object *tag;
1327 error = got_object_open_as_tag(&tag, repo, id1);
1330 if (got_object_tag_get_object_type(tag) !=
1331 GOT_OBJ_TYPE_COMMIT) {
1332 got_object_tag_close(tag);
1333 error = got_error(GOT_ERR_OBJ_TYPE);
1337 id1 = got_object_id_dup(
1338 got_object_tag_get_object_id(tag));
1340 error = got_error_from_errno(
1341 "got_object_id_dup");
1342 got_object_tag_close(tag);
1345 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1346 error = got_error(GOT_ERR_OBJ_TYPE);
1349 error = got_object_open_as_commit(&commit, repo, id1);
1353 if (commit == NULL) {
1354 error = got_repo_match_object_id_prefix(&id1,
1355 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1359 error = got_repo_match_object_id_prefix(&id1,
1360 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1367 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1373 path = in_repo_path;
1376 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1380 error = got_commit_graph_open(&graph, id1, path, 0, repo);
1384 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1389 error = got_commit_graph_iter_next(&id1, graph);
1391 if (error->code == GOT_ERR_ITER_COMPLETED) {
1395 if (error->code != GOT_ERR_ITER_NEED_MORE)
1397 error = got_commit_graph_fetch_commits(graph, 1, repo,
1407 error = got_object_open_as_commit(&commit, repo, id1);
1411 if (search_pattern) {
1412 error = match_logmsg(&have_match, id1, commit,
1415 got_object_commit_close(commit);
1418 if (have_match == 0) {
1419 got_object_commit_close(commit);
1424 SIMPLEQ_FOREACH(re, &refs, entry) {
1427 struct got_tag_object *tag = NULL;
1430 name = got_ref_get_name(re->ref);
1431 if (strcmp(name, GOT_REF_HEAD) == 0)
1433 if (strncmp(name, "refs/", 5) == 0)
1435 if (strncmp(name, "got/", 4) == 0)
1437 if (strncmp(name, "heads/", 6) == 0)
1439 if (strncmp(name, "remotes/", 8) == 0)
1441 if (strncmp(name, "tags/", 5) == 0) {
1442 error = got_object_open_as_tag(&tag, repo,
1445 if (error->code != GOT_ERR_OBJ_TYPE)
1448 * Ref points at something other
1455 cmp = got_object_id_cmp(tag ?
1456 got_object_tag_get_object_id(tag) : re->id, id1);
1458 got_object_tag_close(tag);
1462 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1463 s ? ", " : "", name)) == -1) {
1464 error = got_error_from_errno("asprintf");
1471 if (refs_str == NULL)
1472 refs_str_disp = strdup("");
1474 if ((asprintf(&refs_str_disp, "(%s)",
1476 error = got_error_from_errno("asprintf");
1482 error = got_object_id_str(&id_str1, id1);
1486 error = got_object_id_str(&treeid,
1487 got_object_commit_get_tree_id(commit));
1491 if (gw_trans->action == GW_COMMIT ||
1492 gw_trans->action == GW_COMMITDIFF) {
1495 got_object_commit_get_parent_ids(commit));
1496 if (parent_id != NULL) {
1497 id2 = got_object_id_dup(parent_id->id);
1499 error = got_object_id_str(&id_str2, id2);
1504 id_str2 = strdup("/dev/null");
1508 got_object_commit_get_committer_time(commit);
1510 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1511 error = got_error_from_errno("asprintf");
1515 if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1516 error = got_error_from_errno("asprintf");
1520 if ((asprintf(&commit_tree_disp, commit_tree_html,
1522 error = got_error_from_errno("asprintf");
1526 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1528 error = got_error_from_errno("asprintf");
1532 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1533 error = got_error_from_errno("asprintf");
1537 if ((asprintf(&commit_commit_disp, commit_commit_html,
1538 commit_commit, refs_str_disp)) == -1) {
1539 error = got_error_from_errno("asprintf");
1543 if ((asprintf(&commit_age_long, "%s",
1544 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1545 error = got_error_from_errno("asprintf");
1549 if ((asprintf(&commit_age_long_disp, commit_age_html,
1550 commit_age_long)) == -1) {
1551 error = got_error_from_errno("asprintf");
1555 if ((asprintf(&commit_age_diff, "%s",
1556 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1557 error = got_error_from_errno("asprintf");
1561 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1562 commit_age_diff)) == -1) {
1563 error = got_error_from_errno("asprintf");
1567 if ((asprintf(&commit_author, "%s",
1568 got_object_commit_get_author(commit))) == -1) {
1569 error = got_error_from_errno("asprintf");
1573 if ((asprintf(&commit_author_disp, commit_author_html,
1574 gw_html_escape(commit_author))) == -1) {
1575 error = got_error_from_errno("asprintf");
1579 if ((asprintf(&commit_committer, "%s",
1580 got_object_commit_get_committer(commit))) == -1) {
1581 error = got_error_from_errno("asprintf");
1585 if ((asprintf(&commit_committer_disp, commit_committer_html,
1586 gw_html_escape(commit_committer))) == -1) {
1587 error = got_error_from_errno("asprintf");
1591 if (strcmp(commit_author, commit_committer) == 0) {
1592 free(commit_committer_disp);
1593 commit_committer_disp = strdup("");
1596 error = got_object_commit_get_logmsg(&commit_log0, commit);
1600 commit_log = commit_log0;
1601 while (*commit_log == '\n')
1606 newline = strchr(commit_log, '\n');
1610 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1611 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1612 id_str1, gw_trans->repo_name, id_str1,
1613 gw_trans->repo_name, id_str1)) == -1) {
1614 error = got_error_from_errno("asprintf");
1618 if ((asprintf(&commit_row, logbriefs_row,
1619 commit_age_diff, commit_author, commit_log,
1620 logbriefs_navs_html)) == -1) {
1621 error = got_error_from_errno("asprintf");
1625 free(logbriefs_navs_html);
1626 logbriefs_navs_html = NULL;
1629 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1630 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1631 id_str1, gw_trans->repo_name, id_str1,
1632 gw_trans->repo_name, id_str1)) == -1) {
1633 error = got_error_from_errno("asprintf");
1637 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1638 commit_author_disp, commit_committer_disp,
1639 commit_age_long_disp, gw_html_escape(commit_log),
1640 logbriefs_navs_html)) == -1) {
1641 error = got_error_from_errno("asprintf");
1645 free(logbriefs_navs_html);
1646 logbriefs_navs_html = NULL;
1649 log_tree_html = strdup("log tree here");
1651 if ((asprintf(&commit_row, log_tree_row,
1652 gw_html_escape(commit_log), log_tree_html)) == -1) {
1653 error = got_error_from_errno("asprintf");
1657 free(log_tree_html);
1660 if ((asprintf(&commit_log_disp, commit_log_html,
1661 gw_html_escape(commit_log))) == -1) {
1662 error = got_error_from_errno("asprintf");
1666 log_commit_html = strdup("commit here");
1668 if ((asprintf(&commit_row, log_commit_row,
1669 commit_diff_disp, commit_commit_disp,
1670 commit_tree_disp, commit_author_disp,
1671 commit_committer_disp, commit_age_long_disp,
1672 commit_log_disp, log_commit_html)) == -1) {
1673 error = got_error_from_errno("asprintf");
1676 free(commit_log_disp);
1677 free(log_commit_html);
1681 if ((asprintf(&commit_log_disp, commit_log_html,
1682 gw_html_escape(commit_log))) == -1) {
1683 error = got_error_from_errno("asprintf");
1687 log_diff_html = strdup("diff here");
1689 if ((asprintf(&commit_row, log_diff_row,
1690 commit_diff_disp, commit_commit_disp,
1691 commit_tree_disp, commit_author_disp,
1692 commit_committer_disp, commit_age_long_disp,
1693 commit_log_disp, log_diff_html)) == -1) {
1694 error = got_error_from_errno("asprintf");
1697 free(commit_log_disp);
1698 free(log_diff_html);
1705 error = buf_append(&newsize, diffbuf, commit_row,
1706 strlen(commit_row));
1708 free(commit_parent);
1709 free(commit_diff_disp);
1710 free(commit_tree_disp);
1711 free(commit_age_diff);
1712 free(commit_age_diff_disp);
1713 free(commit_age_long);
1714 free(commit_age_long_disp);
1715 free(commit_author);
1716 free(commit_author_disp);
1717 free(commit_committer);
1718 free(commit_committer_disp);
1721 free(refs_str_disp);
1729 if (error || (limit && --limit == 0))
1736 logs = strdup(diffbuf->cb_buf);
1740 got_object_commit_close(commit);
1744 got_commit_graph_close(graph);
1746 error = got_repo_close(repo);
1751 khttp_puts(gw_trans->gw_req, "Error: ");
1752 khttp_puts(gw_trans->gw_req, error->msg);
1759 gw_get_repo_tags(struct trans *gw_trans, int limit, int tag_type)
1761 const struct got_error *error = NULL;
1762 struct got_repository *repo = NULL;
1763 struct got_reflist_head refs;
1764 struct got_reflist_entry *re;
1765 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1768 struct buf *diffbuf = NULL;
1771 error = buf_alloc(&diffbuf, BUFFER_SIZE);
1775 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1779 SIMPLEQ_INIT(&refs);
1781 error = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
1785 SIMPLEQ_FOREACH(re, &refs, entry) {
1786 const char *refname;
1787 char *refstr, *tag_log0, *tag_log, *id_str;
1789 struct got_object_id *id;
1790 struct got_tag_object *tag;
1792 refname = got_ref_get_name(re->ref);
1793 if (strncmp(refname, "refs/tags/", 10) != 0)
1796 refstr = got_ref_to_str(re->ref);
1797 if (refstr == NULL) {
1798 error = got_error_from_errno("got_ref_to_str");
1802 error = got_ref_resolve(&id, repo, re->ref);
1805 error = got_object_open_as_tag(&tag, repo, id);
1810 tagger_time = got_object_tag_get_tagger_time(tag);
1812 error = got_object_id_str(&id_str,
1813 got_object_tag_get_object_id(tag));
1817 tag_log0 = strdup(got_object_tag_get_message(tag));
1819 got_object_tag_close(tag);
1820 if (tag_log0 == NULL) {
1821 error = got_error_from_errno("strdup");
1826 while (*tag_log == '\n')
1831 newline = strchr(tag_log, '\n');
1835 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1837 error = got_error_from_errno("asprintf");
1841 if ((asprintf(&tags_navs_disp, tags_navs,
1842 gw_trans->repo_name, id_str, gw_trans->repo_name,
1843 id_str, gw_trans->repo_name, id_str,
1844 gw_trans->repo_name, id_str)) == -1) {
1845 error = got_error_from_errno("asprintf");
1849 if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
1850 tags_navs_disp)) == -1) {
1851 error = got_error_from_errno("asprintf");
1855 free(tags_navs_disp);
1862 /* /1* printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr); *1/ */
1865 /* error = got_ref_resolve(&id, repo, re->ref); */
1868 /* error = got_object_open_as_tag(&tag, repo, id); */
1872 /* /1* printf("from: %s\n", got_object_tag_get_tagger(tag)); *1/ */
1873 /* tagger_time = got_object_tag_get_tagger_time(tag); */
1874 /* /1* datestr = get_datestr(&tagger_time, datebuf); *1/ */
1875 /* /1* if (datestr) *1/ */
1876 /* /1* printf("date: %s UTC\n", datestr); *1/ */
1877 /* error = got_object_id_str(&id_str, */
1878 /* got_object_tag_get_object_id(tag)); */
1881 /* switch (got_object_tag_get_object_type(tag)) { */
1882 /* case GOT_OBJ_TYPE_BLOB: */
1883 /* /1* printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str); *1/ */
1885 /* case GOT_OBJ_TYPE_TREE: */
1886 /* /1* printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str); *1/ */
1888 /* case GOT_OBJ_TYPE_COMMIT: */
1889 /* /1* printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str); *1/ */
1891 /* case GOT_OBJ_TYPE_TAG: */
1892 /* /1* printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str); *1/ */
1898 /* tagmsg0 = strdup(got_object_tag_get_message(tag)); */
1899 /* got_object_tag_close(tag); */
1900 /* if (tagmsg0 == NULL) { */
1901 /* error = got_error_from_errno("strdup"); */
1905 /* tagmsg = tagmsg0; */
1907 /* line = strsep(&tagmsg, "\n"); */
1909 /* printf(" %s\n", line); */
1910 /* } while (line); */
1911 /* free(tagmsg0); */
1912 /* error = buf_append(&newsize, diffbuf, tags_row, */
1913 /* strlen(tags_row)); */
1914 error = buf_append(&newsize, diffbuf, tag_row, strlen(tag_row));
1922 if (error || (limit && --limit == 0))
1925 tags = strdup(diffbuf->cb_buf);
1928 got_ref_list_free(&refs);
1930 got_repo_close(repo);
1938 gw_get_repo_heads(struct trans *gw_trans)
1940 const struct got_error *error = NULL;
1941 struct got_repository *repo = NULL;
1942 struct got_reflist_head refs;
1943 struct got_reflist_entry *re;
1944 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
1945 struct buf *diffbuf = NULL;
1948 error = buf_alloc(&diffbuf, BUFFER_SIZE);
1952 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1956 SIMPLEQ_INIT(&refs);
1957 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
1962 SIMPLEQ_FOREACH(re, &refs, entry) {
1965 refname = strdup(got_ref_get_name(re->ref));
1966 if (refname == NULL) {
1967 error = got_error_from_errno("got_ref_to_str");
1971 if (strncmp(refname, "refs/heads/", 11) != 0) {
1976 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
1979 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
1980 refname, gw_trans->repo_name, refname,
1981 gw_trans->repo_name, refname, gw_trans->repo_name,
1983 error = got_error_from_errno("asprintf");
1987 if (strncmp(refname, "refs/heads/", 11) == 0)
1990 if ((asprintf(&head_row, heads_row, age, refname,
1991 head_navs_disp)) == -1) {
1992 error = got_error_from_errno("asprintf");
1996 error = buf_append(&newsize, diffbuf, head_row,
1999 free(head_navs_disp);
2003 heads = strdup(diffbuf->cb_buf);
2006 got_ref_list_free(&refs);
2008 got_repo_close(repo);
2016 gw_get_got_link(struct trans *gw_trans)
2020 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2021 gw_trans->gw_conf->got_logo)) == -1)
2028 gw_get_site_link(struct trans *gw_trans)
2030 char *link, *repo = "", *action = "";
2032 if (gw_trans->repo_name != NULL)
2033 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2034 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2037 if (gw_trans->action_name != NULL)
2038 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2041 if ((asprintf(&link, site_link, GOTWEB,
2042 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2049 gw_html_escape(const char *html)
2051 char *escaped_str = NULL, *buf;
2055 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
2061 if ((sz = strlen(html)) == 0)
2064 /* only work with BUFFER_SIZE */
2065 if (BUFFER_SIZE < sz)
2068 for (i = 0; i < sz; i++) {
2072 strcat(buf, ">");
2075 strcat(buf, "&");
2078 strcat(buf, "<");
2081 strcat(buf, """);
2084 strcat(buf, "'");
2087 strcat(buf, "<br />");
2095 asprintf(&escaped_str, "%s", buf);
2103 const struct got_error *error = NULL;
2104 struct trans *gw_trans;
2105 struct gw_dir *dir = NULL, *tdir;
2106 const char *page = "index";
2109 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
2112 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2115 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2118 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2121 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
2123 errx(1, "khttp_parse");
2125 if ((gw_trans->gw_conf =
2126 malloc(sizeof(struct gotweb_conf))) == NULL) {
2128 error = got_error_from_errno("malloc");
2132 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
2133 error = got_error_from_errno("pledge");
2137 TAILQ_INIT(&gw_trans->gw_dirs);
2140 gw_trans->repos_total = 0;
2141 gw_trans->repo_path = NULL;
2142 gw_trans->commit = NULL;
2143 gw_trans->headref = strdup(GOT_REF_HEAD);
2144 gw_trans->mime = KMIME_TEXT_HTML;
2145 gw_trans->gw_tmpl->key = templs;
2146 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2147 gw_trans->gw_tmpl->arg = gw_trans;
2148 gw_trans->gw_tmpl->cb = gw_template;
2149 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2153 gw_trans->mime = KMIME_TEXT_PLAIN;
2154 gw_trans->action = GW_ERR;
2155 gw_display_index(gw_trans, error);
2159 error = gw_parse_querystring(gw_trans);
2163 gw_display_index(gw_trans, error);
2167 free(gw_trans->gw_conf->got_repos_path);
2168 free(gw_trans->gw_conf->got_www_path);
2169 free(gw_trans->gw_conf->got_site_name);
2170 free(gw_trans->gw_conf->got_site_owner);
2171 free(gw_trans->gw_conf->got_site_link);
2172 free(gw_trans->gw_conf->got_logo);
2173 free(gw_trans->gw_conf->got_logo_url);
2174 free(gw_trans->gw_conf);
2175 free(gw_trans->commit);
2176 free(gw_trans->repo_path);
2177 free(gw_trans->repo_name);
2178 free(gw_trans->repo_file);
2179 free(gw_trans->action_name);
2180 free(gw_trans->headref);
2182 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2184 free(dir->description);
2193 khttp_free(gw_trans->gw_req);
2194 return EXIT_SUCCESS;