2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
20 #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>
50 #include "gotweb_ui.h"
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct gw_dir *gw_dir;
59 struct gotweb_conf *gw_conf;
60 struct ktemplate *gw_tmpl;
61 struct khtmlreq *gw_html_req;
72 unsigned int repos_total;
88 TAILQ_ENTRY(gw_dir) entry;
128 static const char *const gw_templs[TEMPL__MAX] = {
138 static const struct kvalid gw_keys[KEY__ZMAX] = {
139 { kvalid_stringne, "action" },
140 { kvalid_stringne, "commit" },
141 { kvalid_stringne, "file" },
142 { kvalid_stringne, "folder" },
143 { kvalid_stringne, "headref" },
144 { kvalid_int, "page" },
145 { kvalid_stringne, "path" },
148 static struct gw_dir *gw_init_gw_dir(char *);
150 static char *gw_get_repo_description(struct gw_trans *,
152 static char *gw_get_repo_owner(struct gw_trans *,
154 static char *gw_get_time_str(time_t, int);
155 static char *gw_get_repo_age(struct gw_trans *,
156 char *, char *, int);
157 static char *gw_get_repo_log(struct gw_trans *,
158 const char *, char *, int, int);
159 static char *gw_get_file_blame(struct gw_trans *, char *);
160 static char *gw_get_repo_tree(struct gw_trans *, char *);
161 static char *gw_get_repo_diff(struct gw_trans *, char *,
163 static char *gw_get_repo_tags(struct gw_trans *, int, int);
164 static char *gw_get_repo_heads(struct gw_trans *);
165 static char *gw_get_clone_url(struct gw_trans *, char *);
166 static char *gw_get_got_link(struct gw_trans *);
167 static char *gw_get_site_link(struct gw_trans *);
168 static char *gw_html_escape(const char *);
169 static char *gw_colordiff_line(char *);
171 static void gw_display_open(struct gw_trans *, enum khttp,
173 static void gw_display_index(struct gw_trans *,
174 const struct got_error *);
176 static int gw_template(size_t, void *);
178 static const struct got_error* gw_apply_unveil(const char *, const char *);
179 static const struct got_error* gw_blame_cb(void *, int, int,
180 struct got_object_id *);
181 static const struct got_error* gw_load_got_paths(struct gw_trans *);
182 static const struct got_error* gw_load_got_path(struct gw_trans *,
184 static const struct got_error* gw_parse_querystring(struct gw_trans *);
185 static const struct got_error* gw_match_logmsg(int *, struct got_object_id *,
186 struct got_commit_object *, regex_t *);
188 static const struct got_error* gw_blame(struct gw_trans *);
189 static const struct got_error* gw_commit(struct gw_trans *);
190 static const struct got_error* gw_commitdiff(struct gw_trans *);
191 static const struct got_error* gw_index(struct gw_trans *);
192 static const struct got_error* gw_log(struct gw_trans *);
193 static const struct got_error* gw_raw(struct gw_trans *);
194 static const struct got_error* gw_logbriefs(struct gw_trans *);
195 static const struct got_error* gw_summary(struct gw_trans *);
196 static const struct got_error* gw_tag(struct gw_trans *);
197 static const struct got_error* gw_tree(struct gw_trans *);
199 struct gw_query_action {
200 unsigned int func_id;
201 const char *func_name;
202 const struct got_error *(*func_main)(struct gw_trans *);
206 enum gw_query_actions {
220 static struct gw_query_action gw_query_funcs[] = {
221 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
222 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
223 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
224 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
225 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
226 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
227 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
228 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
229 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
230 { GW_TAG, "tag", gw_tag, "gw_tmpl/index.tmpl" },
231 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
234 static const struct got_error *
235 gw_apply_unveil(const char *repo_path, const char *repo_file)
237 const struct got_error *err;
239 if (repo_path && repo_file) {
241 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
242 return got_error_from_errno("asprintf unveil");
243 if (unveil(full_path, "r") != 0)
244 return got_error_from_errno2("unveil", full_path);
247 if (repo_path && unveil(repo_path, "r") != 0)
248 return got_error_from_errno2("unveil", repo_path);
250 if (unveil("/tmp", "rwc") != 0)
251 return got_error_from_errno2("unveil", "/tmp");
253 err = got_privsep_unveil_exec_helpers();
257 if (unveil(NULL, NULL) != 0)
258 return got_error_from_errno("unveil");
263 static const struct got_error *
264 gw_blame(struct gw_trans *gw_trans)
266 const struct got_error *error = NULL;
268 char *log, *log_html;
270 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
272 error = got_error_from_errno("pledge");
276 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
280 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGBLAME);
282 if (log != NULL && strcmp(log, "") != 0) {
283 if ((asprintf(&log_html, log_blame, log)) == -1)
284 return got_error_from_errno("asprintf");
285 khttp_puts(gw_trans->gw_req, log_html);
292 static const struct got_error *
293 gw_commit(struct gw_trans *gw_trans)
295 const struct got_error *error = NULL;
296 char *log, *log_html;
298 if (pledge("stdio rpath proc exec sendfd unveil",
300 error = got_error_from_errno("pledge");
304 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
308 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
310 if (log != NULL && strcmp(log, "") != 0) {
311 if ((asprintf(&log_html, log_commit, log)) == -1)
312 return got_error_from_errno("asprintf");
313 khttp_puts(gw_trans->gw_req, log_html);
320 static const struct got_error *
321 gw_commitdiff(struct gw_trans *gw_trans)
323 const struct got_error *error = NULL;
324 char *log, *log_html;
326 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
328 error = got_error_from_errno("pledge");
332 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
336 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
338 if (log != NULL && strcmp(log, "") != 0) {
339 if ((asprintf(&log_html, log_diff, log)) == -1)
340 return got_error_from_errno("asprintf");
341 khttp_puts(gw_trans->gw_req, log_html);
348 static const struct got_error *
349 gw_index(struct gw_trans *gw_trans)
351 const struct got_error *error = NULL;
352 struct gw_dir *gw_dir = NULL;
353 char *html, *navs, *next, *prev;
354 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
356 if (pledge("stdio rpath proc exec sendfd unveil",
358 error = got_error_from_errno("pledge");
362 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
366 error = gw_load_got_paths(gw_trans);
370 khttp_puts(gw_trans->gw_req, index_projects_header);
372 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
375 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
376 if (gw_trans->page > 0 && (gw_trans->page *
377 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
383 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
384 gw_dir->name, gw_dir->name)) == -1)
385 return got_error_from_errno("asprintf");
387 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
388 gw_dir->description, gw_dir->owner, gw_dir->age,
390 return got_error_from_errno("asprintf");
392 khttp_puts(gw_trans->gw_req, html);
397 if (gw_trans->gw_conf->got_max_repos_display == 0)
400 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
401 khttp_puts(gw_trans->gw_req, np_wrapper_start);
402 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
403 (gw_trans->page > 0) &&
404 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
405 prev_disp == gw_trans->repos_total))
406 khttp_puts(gw_trans->gw_req, np_wrapper_start);
408 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
409 (gw_trans->page > 0) &&
410 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
411 prev_disp == gw_trans->repos_total)) {
412 if ((asprintf(&prev, nav_prev,
413 gw_trans->page - 1)) == -1)
414 return got_error_from_errno("asprintf");
415 khttp_puts(gw_trans->gw_req, prev);
419 khttp_puts(gw_trans->gw_req, div_end);
421 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
422 next_disp == gw_trans->gw_conf->got_max_repos_display &&
423 dir_c != (gw_trans->page + 1) *
424 gw_trans->gw_conf->got_max_repos_display) {
425 if ((asprintf(&next, nav_next,
426 gw_trans->page + 1)) == -1)
427 return got_error_from_errno("calloc");
428 khttp_puts(gw_trans->gw_req, next);
429 khttp_puts(gw_trans->gw_req, div_end);
435 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
436 (gw_trans->page > 0) &&
437 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
438 prev_disp == gw_trans->repos_total))
439 khttp_puts(gw_trans->gw_req, div_end);
446 static const struct got_error *
447 gw_log(struct gw_trans *gw_trans)
449 const struct got_error *error = NULL;
450 char *log, *log_html;
452 if (pledge("stdio rpath proc exec sendfd unveil",
454 error = got_error_from_errno("pledge");
458 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
462 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
463 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
465 if (log != NULL && strcmp(log, "") != 0) {
466 if ((asprintf(&log_html, logs, log)) == -1)
467 return got_error_from_errno("asprintf");
468 khttp_puts(gw_trans->gw_req, log_html);
475 static const struct got_error *
476 gw_raw(struct gw_trans *gw_trans)
478 const struct got_error *error = NULL;
483 static const struct got_error *
484 gw_logbriefs(struct gw_trans *gw_trans)
486 const struct got_error *error = NULL;
487 char *log, *log_html;
489 if (pledge("stdio rpath proc exec sendfd unveil",
491 error = got_error_from_errno("pledge");
495 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
499 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
500 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
502 if (log != NULL && strcmp(log, "") != 0) {
503 if ((asprintf(&log_html, summary_logbriefs,
505 return got_error_from_errno("asprintf");
506 khttp_puts(gw_trans->gw_req, log_html);
513 static const struct got_error *
514 gw_summary(struct gw_trans *gw_trans)
516 const struct got_error *error = NULL;
517 char *description_html, *repo_owner_html, *repo_age_html,
518 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
521 if (pledge("stdio rpath proc exec sendfd unveil",
523 error = got_error_from_errno("pledge");
527 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
531 khttp_puts(gw_trans->gw_req, summary_wrapper);
532 if (gw_trans->gw_conf->got_show_repo_description) {
533 if (gw_trans->gw_dir->description != NULL &&
534 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
535 if ((asprintf(&description_html, description,
536 gw_trans->gw_dir->description)) == -1)
537 return got_error_from_errno("asprintf");
539 khttp_puts(gw_trans->gw_req, description_html);
540 free(description_html);
544 if (gw_trans->gw_conf->got_show_repo_owner) {
545 if (gw_trans->gw_dir->owner != NULL &&
546 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
547 if ((asprintf(&repo_owner_html, repo_owner,
548 gw_trans->gw_dir->owner)) == -1)
549 return got_error_from_errno("asprintf");
551 khttp_puts(gw_trans->gw_req, repo_owner_html);
552 free(repo_owner_html);
556 if (gw_trans->gw_conf->got_show_repo_age) {
557 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
558 "refs/heads", TM_LONG);
559 if (age != NULL && (strcmp(age, "") != 0)) {
560 if ((asprintf(&repo_age_html, last_change, age)) == -1)
561 return got_error_from_errno("asprintf");
563 khttp_puts(gw_trans->gw_req, repo_age_html);
569 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
570 if (gw_trans->gw_dir->url != NULL &&
571 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
572 if ((asprintf(&cloneurl_html, cloneurl,
573 gw_trans->gw_dir->url)) == -1)
574 return got_error_from_errno("asprintf");
576 khttp_puts(gw_trans->gw_req, cloneurl_html);
580 khttp_puts(gw_trans->gw_req, div_end);
582 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
583 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
584 heads = gw_get_repo_heads(gw_trans);
586 if (log != NULL && strcmp(log, "") != 0) {
587 if ((asprintf(&log_html, summary_logbriefs,
589 return got_error_from_errno("asprintf");
590 khttp_puts(gw_trans->gw_req, log_html);
595 if (tags != NULL && strcmp(tags, "") != 0) {
596 if ((asprintf(&tags_html, summary_tags,
598 return got_error_from_errno("asprintf");
599 khttp_puts(gw_trans->gw_req, tags_html);
604 if (heads != NULL && strcmp(heads, "") != 0) {
605 if ((asprintf(&heads_html, summary_heads,
607 return got_error_from_errno("asprintf");
608 khttp_puts(gw_trans->gw_req, heads_html);
615 static const struct got_error *
616 gw_tag(struct gw_trans *gw_trans)
618 const struct got_error *error = NULL;
619 char *log, *log_html;
621 if (pledge("stdio rpath proc exec sendfd unveil",
623 error = got_error_from_errno("pledge");
627 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
631 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTAG);
633 if (log != NULL && strcmp(log, "") != 0) {
634 if ((asprintf(&log_html, log_tag, log)) == -1)
635 return got_error_from_errno("asprintf");
636 khttp_puts(gw_trans->gw_req, log_html);
643 static const struct got_error *
644 gw_tree(struct gw_trans *gw_trans)
646 const struct got_error *error = NULL;
647 char *log, *log_html;
649 if (pledge("stdio rpath proc exec sendfd unveil",
651 error = got_error_from_errno("pledge");
655 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
659 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
661 if (log != NULL && strcmp(log, "") != 0) {
662 if ((asprintf(&log_html, log_tree, log)) == -1)
663 return got_error_from_errno("asprintf");
664 khttp_puts(gw_trans->gw_req, log_html);
671 static const struct got_error *
672 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
674 const struct got_error *error = NULL;
679 if ((asprintf(&dir_test, "%s/%s/%s",
680 gw_trans->gw_conf->got_repos_path, gw_dir->name,
681 GOTWEB_GIT_DIR)) == -1)
682 return got_error_from_errno("asprintf");
684 dt = opendir(dir_test);
688 gw_dir->path = strdup(dir_test);
693 if ((asprintf(&dir_test, "%s/%s/%s",
694 gw_trans->gw_conf->got_repos_path, gw_dir->name,
695 GOTWEB_GOT_DIR)) == -1)
696 return got_error_from_errno("asprintf");
698 dt = opendir(dir_test);
703 error = got_error(GOT_ERR_NOT_GIT_REPO);
707 if ((asprintf(&dir_test, "%s/%s",
708 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
709 return got_error_from_errno("asprintf");
711 gw_dir->path = strdup(dir_test);
714 gw_dir->description = gw_get_repo_description(gw_trans,
716 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
717 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
719 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
728 static const struct got_error *
729 gw_load_got_paths(struct gw_trans *gw_trans)
731 const struct got_error *error = NULL;
733 struct dirent **sd_dent;
734 struct gw_dir *gw_dir;
736 unsigned int d_cnt, d_i;
738 d = opendir(gw_trans->gw_conf->got_repos_path);
740 error = got_error_from_errno2("opendir",
741 gw_trans->gw_conf->got_repos_path);
745 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
748 error = got_error_from_errno2("scandir",
749 gw_trans->gw_conf->got_repos_path);
753 for (d_i = 0; d_i < d_cnt; d_i++) {
754 if (gw_trans->gw_conf->got_max_repos > 0 &&
755 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
756 break; /* account for parent and self */
758 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
759 strcmp(sd_dent[d_i]->d_name, "..") == 0)
762 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
763 return got_error_from_errno("gw_dir malloc");
765 error = gw_load_got_path(gw_trans, gw_dir);
766 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
771 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
772 !got_path_dir_is_empty(gw_dir->path)) {
773 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
775 gw_trans->repos_total++;
783 static const struct got_error *
784 gw_parse_querystring(struct gw_trans *gw_trans)
786 const struct got_error *error = NULL;
788 struct gw_query_action *action = NULL;
791 if (gw_trans->gw_req->fieldnmap[0]) {
792 error = got_error_from_errno("bad parse");
794 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
795 /* define gw_trans->repo_path */
796 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
797 return got_error_from_errno("asprintf");
799 if ((asprintf(&gw_trans->repo_path, "%s/%s",
800 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
801 return got_error_from_errno("asprintf");
803 /* get action and set function */
804 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
805 for (i = 0; i < nitems(gw_query_funcs); i++) {
806 action = &gw_query_funcs[i];
807 if (action->func_name == NULL)
810 if (strcmp(action->func_name,
812 gw_trans->action = i;
813 if ((asprintf(&gw_trans->action_name,
814 "%s", action->func_name)) == -1)
816 got_error_from_errno(
825 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
826 if ((asprintf(&gw_trans->commit, "%s",
828 return got_error_from_errno("asprintf");
830 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
831 if ((asprintf(&gw_trans->repo_file, "%s",
833 return got_error_from_errno("asprintf");
835 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
836 if ((asprintf(&gw_trans->repo_folder, "%s",
838 return got_error_from_errno("asprintf");
840 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
841 if ((asprintf(&gw_trans->headref, "%s",
843 return got_error_from_errno("asprintf");
845 if (action == NULL) {
846 error = got_error_from_errno("invalid action");
849 if ((gw_trans->gw_dir =
850 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
851 return got_error_from_errno("gw_dir malloc");
853 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
857 gw_trans->action = GW_INDEX;
859 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
860 gw_trans->page = p->parsed.i;
862 if (gw_trans->action == GW_RAW)
863 gw_trans->mime = KMIME_TEXT_PLAIN;
868 static struct gw_dir *
869 gw_init_gw_dir(char *dir)
871 struct gw_dir *gw_dir;
873 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
876 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
882 static const struct got_error*
883 gw_match_logmsg(int *have_match, struct got_object_id *id,
884 struct got_commit_object *commit, regex_t *regex)
886 const struct got_error *err = NULL;
888 char *id_str = NULL, *logmsg = NULL;
892 err = got_object_id_str(&id_str, id);
896 err = got_object_commit_get_logmsg(&logmsg, commit);
900 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
909 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
911 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
912 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
914 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
916 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
917 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
918 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
919 khttp_body(gw_trans->gw_req);
923 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
925 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
926 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
929 khttp_puts(gw_trans->gw_req, err->msg);
931 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
932 gw_query_funcs[gw_trans->action].template);
934 khtml_close(gw_trans->gw_html_req);
938 gw_template(size_t key, void *arg)
940 const struct got_error *error = NULL;
941 struct gw_trans *gw_trans = arg;
942 char *gw_got_link, *gw_site_link;
943 char *site_owner_name, *site_owner_name_h;
947 khttp_puts(gw_trans->gw_req, head);
950 gw_got_link = gw_get_got_link(gw_trans);
951 if (gw_got_link != NULL)
952 khttp_puts(gw_trans->gw_req, gw_got_link);
956 case (TEMPL_SITEPATH):
957 gw_site_link = gw_get_site_link(gw_trans);
958 if (gw_site_link != NULL)
959 khttp_puts(gw_trans->gw_req, gw_site_link);
964 if (gw_trans->gw_conf->got_site_name != NULL)
965 khtml_puts(gw_trans->gw_html_req,
966 gw_trans->gw_conf->got_site_name);
970 khttp_puts(gw_trans->gw_req, search);
972 case(TEMPL_SITEOWNER):
973 if (gw_trans->gw_conf->got_site_owner != NULL &&
974 gw_trans->gw_conf->got_show_site_owner) {
976 gw_html_escape(gw_trans->gw_conf->got_site_owner);
977 if ((asprintf(&site_owner_name_h, site_owner,
982 khttp_puts(gw_trans->gw_req, site_owner_name_h);
983 free(site_owner_name);
984 free(site_owner_name_h);
988 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
990 khttp_puts(gw_trans->gw_req, error->msg);
1001 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1004 char *description = NULL, *d_file = NULL;
1007 if (gw_trans->gw_conf->got_show_repo_description == false)
1010 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1013 if ((f = fopen(d_file, "r")) == NULL)
1016 fseek(f, 0, SEEK_END);
1018 fseek(f, 0, SEEK_SET);
1019 if ((description = calloc(len, sizeof(char *))) == NULL)
1022 fread(description, 1, len, f);
1027 if ((asprintf(&description, "%s", "")) == -1)
1034 gw_get_time_str(time_t committer_time, int ref_tm)
1038 char *years = "years ago", *months = "months ago";
1039 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1040 char *minutes = "minutes ago", *seconds = "seconds ago";
1041 char *now = "right now";
1047 diff_time = time(NULL) - committer_time;
1048 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1049 if ((asprintf(&repo_age, "%lld %s",
1050 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1052 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1053 if ((asprintf(&repo_age, "%lld %s",
1054 (diff_time / 60 / 60 / 24 / (365 / 12)),
1057 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1058 if ((asprintf(&repo_age, "%lld %s",
1059 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1061 } else if (diff_time > 60 * 60 * 24 * 2) {
1062 if ((asprintf(&repo_age, "%lld %s",
1063 (diff_time / 60 / 60 / 24), days)) == -1)
1065 } else if (diff_time > 60 * 60 * 2) {
1066 if ((asprintf(&repo_age, "%lld %s",
1067 (diff_time / 60 / 60), hours)) == -1)
1069 } else if (diff_time > 60 * 2) {
1070 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1073 } else if (diff_time > 2) {
1074 if ((asprintf(&repo_age, "%lld %s", diff_time,
1078 if ((asprintf(&repo_age, "%s", now)) == -1)
1083 if (gmtime_r(&committer_time, &tm) == NULL)
1086 s = asctime_r(&tm, datebuf);
1090 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1098 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1101 const struct got_error *error = NULL;
1102 struct got_object_id *id = NULL;
1103 struct got_repository *repo = NULL;
1104 struct got_commit_object *commit = NULL;
1105 struct got_reflist_head refs;
1106 struct got_reflist_entry *re;
1107 struct got_reference *head_ref;
1109 time_t committer_time = 0, cmp_time = 0;
1110 const char *refname;
1111 char *repo_age = NULL;
1113 if (repo_ref == NULL)
1116 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1119 SIMPLEQ_INIT(&refs);
1120 if (gw_trans->gw_conf->got_show_repo_age == false) {
1121 if ((asprintf(&repo_age, "")) == -1)
1126 error = got_repo_open(&repo, dir, NULL);
1131 error = got_ref_list(&refs, repo, "refs/heads",
1132 got_ref_cmp_by_name, NULL);
1134 error = got_ref_list(&refs, repo, repo_ref,
1135 got_ref_cmp_by_name, NULL);
1139 SIMPLEQ_FOREACH(re, &refs, entry) {
1141 refname = strdup(repo_ref);
1143 refname = got_ref_get_name(re->ref);
1144 error = got_ref_open(&head_ref, repo, refname, 0);
1148 error = got_ref_resolve(&id, repo, head_ref);
1149 got_ref_close(head_ref);
1153 error = got_object_open_as_commit(&commit, repo, id);
1158 got_object_commit_get_committer_time(commit);
1160 if (cmp_time < committer_time)
1161 cmp_time = committer_time;
1164 if (cmp_time != 0) {
1165 committer_time = cmp_time;
1166 repo_age = gw_get_time_str(committer_time, ref_tm);
1168 if ((asprintf(&repo_age, "")) == -1)
1170 got_ref_list_free(&refs);
1174 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1181 gw_get_repo_diff(struct gw_trans *gw_trans, char *id_str1, char *id_str2)
1183 const struct got_error *error;
1185 struct got_object_id *id1 = NULL, *id2 = NULL;
1186 struct got_repository *repo = NULL;
1187 struct buf *diffbuf = NULL;
1188 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1197 error = buf_alloc(&diffbuf, 0);
1201 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1205 error = got_repo_match_object_id(&id1, &label1, id_str1,
1206 GOT_OBJ_TYPE_ANY, 1, repo);
1211 error = got_repo_match_object_id(&id2, &label2, id_str2,
1212 GOT_OBJ_TYPE_ANY, 1, repo);
1216 error = got_object_get_type(&type2, repo, id2);
1221 error = got_object_get_type(&type1, repo, id1);
1225 if (id_str2 && type1 != type2) {
1226 error = got_error(GOT_ERR_OBJ_TYPE);
1231 case GOT_OBJ_TYPE_BLOB:
1232 error = got_diff_objects_as_blobs(id2, id1, NULL, NULL, 3, 0,
1235 case GOT_OBJ_TYPE_TREE:
1236 error = got_diff_objects_as_trees(id2, id1, "", "", 3, 0, repo,
1239 case GOT_OBJ_TYPE_COMMIT:
1240 error = got_diff_objects_as_commits(id2, id1, 3, 0, repo, f);
1243 error = got_error(GOT_ERR_OBJ_TYPE);
1246 if ((buf = calloc(128, sizeof(char *))) == NULL)
1249 fseek(f, 0, SEEK_SET);
1251 while ((fgets(buf, 128, f)) != NULL) {
1252 buf_color = gw_colordiff_line(buf);
1253 error = buf_puts(&newsize, diffbuf, buf_color);
1257 error = buf_puts(&newsize, diffbuf, div_end);
1262 if (buf_len(diffbuf) > 0) {
1263 error = buf_putc(diffbuf, '\0');
1264 diff_html = strdup(buf_get(diffbuf));
1276 got_repo_close(repo);
1285 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1288 char *owner = NULL, *d_file = NULL;
1289 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1290 char *comp, *pos, *buf;
1293 if (gw_trans->gw_conf->got_show_repo_owner == false)
1296 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1299 if ((f = fopen(d_file, "r")) == NULL)
1302 if ((buf = calloc(128, sizeof(char *))) == NULL)
1305 while ((fgets(buf, 128, f)) != NULL) {
1306 if ((pos = strstr(buf, gotweb)) != NULL)
1309 if ((pos = strstr(buf, gitweb)) != NULL)
1318 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1323 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1326 for (i = 0; i < 2; i++) {
1327 owner = strsep(&buf, "\"");
1337 if ((asprintf(&owner, "%s", "")) == -1)
1344 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1347 char *url = NULL, *d_file = NULL;
1350 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1353 if ((f = fopen(d_file, "r")) == NULL)
1356 fseek(f, 0, SEEK_END);
1358 fseek(f, 0, SEEK_SET);
1360 if ((url = calloc(len, sizeof(char *))) == NULL)
1363 fread(url, 1, len, f);
1370 gw_get_repo_log(struct gw_trans *gw_trans, const char *search_pattern,
1371 char *start_commit, int limit, int log_type)
1373 const struct got_error *error;
1374 struct got_repository *repo = NULL;
1375 struct got_reflist_head refs;
1376 struct got_reflist_entry *re;
1377 struct got_commit_object *commit = NULL;
1378 struct got_object_id *id1 = NULL, *id2 = NULL;
1379 struct got_object_qid *parent_id;
1380 struct got_commit_graph *graph = NULL;
1381 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1382 *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1383 *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1384 *commit_commit_disp = NULL, *commit_age_diff = NULL,
1385 *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1386 *commit_age_long_disp = NULL, *commit_author = NULL,
1387 *commit_author_disp = NULL, *commit_committer = NULL,
1388 *commit_committer_disp = NULL, *commit_log = NULL,
1389 *commit_log_disp = NULL, *commit_parent = NULL,
1390 *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1391 *log_tree_html = NULL, *log_commit_html = NULL,
1392 *log_diff_html = NULL, *commit_tree = NULL,
1393 *commit_tree_disp = NULL, *log_tag_html = NULL,
1394 *log_blame_html = NULL;
1395 char *commit_log0, *newline;
1397 int have_match, has_parent = 1;
1399 struct buf *diffbuf = NULL;
1400 time_t committer_time;
1402 error = buf_alloc(&diffbuf, 0);
1406 if (search_pattern &&
1407 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB |
1411 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1415 SIMPLEQ_INIT(&refs);
1417 if (start_commit == NULL) {
1418 struct got_reference *head_ref;
1419 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1423 error = got_ref_resolve(&id1, repo, head_ref);
1424 got_ref_close(head_ref);
1428 error = got_object_open_as_commit(&commit, repo, id1);
1430 struct got_reference *ref;
1431 error = got_ref_open(&ref, repo, start_commit, 0);
1432 if (error == NULL) {
1434 error = got_ref_resolve(&id1, repo, ref);
1438 error = got_object_get_type(&obj_type, repo, id1);
1441 if (obj_type == GOT_OBJ_TYPE_TAG) {
1442 struct got_tag_object *tag;
1443 error = got_object_open_as_tag(&tag, repo, id1);
1446 if (got_object_tag_get_object_type(tag) !=
1447 GOT_OBJ_TYPE_COMMIT) {
1448 got_object_tag_close(tag);
1449 error = got_error(GOT_ERR_OBJ_TYPE);
1453 id1 = got_object_id_dup(
1454 got_object_tag_get_object_id(tag));
1456 error = got_error_from_errno(
1457 "got_object_id_dup");
1458 got_object_tag_close(tag);
1461 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1462 error = got_error(GOT_ERR_OBJ_TYPE);
1465 error = got_object_open_as_commit(&commit, repo, id1);
1469 if (commit == NULL) {
1470 error = got_repo_match_object_id_prefix(&id1,
1471 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1475 error = got_repo_match_object_id_prefix(&id1,
1476 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1482 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1488 path = in_repo_path;
1491 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1495 error = got_commit_graph_open(&graph, path, 0);
1499 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1504 error = got_commit_graph_iter_next(&id1, graph, repo, NULL,
1507 if (error->code == GOT_ERR_ITER_COMPLETED)
1514 error = got_object_open_as_commit(&commit, repo, id1);
1518 if (search_pattern) {
1519 error = gw_match_logmsg(&have_match, id1, commit,
1522 got_object_commit_close(commit);
1525 if (have_match == 0) {
1526 got_object_commit_close(commit);
1531 SIMPLEQ_FOREACH(re, &refs, entry) {
1534 struct got_tag_object *tag = NULL;
1537 name = got_ref_get_name(re->ref);
1538 if (strcmp(name, GOT_REF_HEAD) == 0)
1540 if (strncmp(name, "refs/", 5) == 0)
1542 if (strncmp(name, "got/", 4) == 0)
1544 if (strncmp(name, "heads/", 6) == 0)
1546 if (strncmp(name, "remotes/", 8) == 0)
1548 if (strncmp(name, "tags/", 5) == 0) {
1549 error = got_object_open_as_tag(&tag, repo,
1552 if (error->code != GOT_ERR_OBJ_TYPE)
1555 * Ref points at something other
1562 cmp = got_object_id_cmp(tag ?
1563 got_object_tag_get_object_id(tag) : re->id, id1);
1565 got_object_tag_close(tag);
1569 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1570 s ? ", " : "", name)) == -1) {
1571 error = got_error_from_errno("asprintf");
1578 if (refs_str == NULL)
1579 refs_str_disp = strdup("");
1581 if ((asprintf(&refs_str_disp, "(%s)",
1583 error = got_error_from_errno("asprintf");
1589 error = got_object_id_str(&id_str1, id1);
1593 error = got_object_id_str(&treeid,
1594 got_object_commit_get_tree_id(commit));
1598 if (gw_trans->action == GW_COMMIT ||
1599 gw_trans->action == GW_COMMITDIFF) {
1602 got_object_commit_get_parent_ids(commit));
1603 if (parent_id != NULL) {
1604 id2 = got_object_id_dup(parent_id->id);
1606 error = got_object_id_str(&id_str2, id2);
1612 id_str2 = strdup("/dev/null");
1617 got_object_commit_get_committer_time(commit);
1619 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1620 error = got_error_from_errno("asprintf");
1624 if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1625 error = got_error_from_errno("asprintf");
1629 if ((asprintf(&commit_tree_disp, commit_tree_html,
1631 error = got_error_from_errno("asprintf");
1635 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1637 error = got_error_from_errno("asprintf");
1641 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1642 error = got_error_from_errno("asprintf");
1646 if ((asprintf(&commit_commit_disp, commit_commit_html,
1647 commit_commit, refs_str_disp)) == -1) {
1648 error = got_error_from_errno("asprintf");
1652 if ((asprintf(&commit_age_long, "%s",
1653 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1654 error = got_error_from_errno("asprintf");
1658 if ((asprintf(&commit_age_long_disp, commit_age_html,
1659 commit_age_long)) == -1) {
1660 error = got_error_from_errno("asprintf");
1664 if ((asprintf(&commit_age_diff, "%s",
1665 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1666 error = got_error_from_errno("asprintf");
1670 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1671 commit_age_diff)) == -1) {
1672 error = got_error_from_errno("asprintf");
1676 if ((asprintf(&commit_author, "%s",
1677 got_object_commit_get_author(commit))) == -1) {
1678 error = got_error_from_errno("asprintf");
1682 if ((asprintf(&commit_author_disp, commit_author_html,
1683 gw_html_escape(commit_author))) == -1) {
1684 error = got_error_from_errno("asprintf");
1688 if ((asprintf(&commit_committer, "%s",
1689 got_object_commit_get_committer(commit))) == -1) {
1690 error = got_error_from_errno("asprintf");
1694 if ((asprintf(&commit_committer_disp, commit_committer_html,
1695 gw_html_escape(commit_committer))) == -1) {
1696 error = got_error_from_errno("asprintf");
1700 if (strcmp(commit_author, commit_committer) == 0) {
1701 free(commit_committer_disp);
1702 commit_committer_disp = strdup("");
1705 error = got_object_commit_get_logmsg(&commit_log0, commit);
1709 commit_log = commit_log0;
1710 while (*commit_log == '\n')
1715 newline = strchr(commit_log, '\n');
1719 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1720 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1721 id_str1, gw_trans->repo_name, id_str1,
1722 gw_trans->repo_name, id_str1)) == -1) {
1723 error = got_error_from_errno("asprintf");
1727 if ((asprintf(&commit_row, logbriefs_row,
1728 commit_age_diff, commit_author, commit_log,
1729 logbriefs_navs_html)) == -1) {
1730 error = got_error_from_errno("asprintf");
1734 free(logbriefs_navs_html);
1735 logbriefs_navs_html = NULL;
1738 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1739 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1740 id_str1, gw_trans->repo_name, id_str1,
1741 gw_trans->repo_name, id_str1)) == -1) {
1742 error = got_error_from_errno("asprintf");
1746 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1747 commit_author_disp, commit_committer_disp,
1748 commit_age_long_disp, gw_html_escape(commit_log),
1749 logbriefs_navs_html)) == -1) {
1750 error = got_error_from_errno("asprintf");
1754 free(logbriefs_navs_html);
1755 logbriefs_navs_html = NULL;
1758 log_tag_html = strdup("tag log here");
1760 if ((asprintf(&commit_row, log_tag_row,
1761 gw_html_escape(commit_log), log_tag_html)) == -1) {
1762 error = got_error_from_errno("asprintf");
1769 log_blame_html = gw_get_file_blame(gw_trans,
1772 if ((asprintf(&commit_row, log_blame_row,
1773 gw_html_escape(commit_log), log_blame_html)) == -1) {
1774 error = got_error_from_errno("asprintf");
1778 free(log_blame_html);
1781 log_tree_html = gw_get_repo_tree(gw_trans,
1784 if ((asprintf(&commit_row, log_tree_row,
1785 gw_html_escape(commit_log), log_tree_html)) == -1) {
1786 error = got_error_from_errno("asprintf");
1790 free(log_tree_html);
1793 if ((asprintf(&commit_log_disp, commit_log_html,
1794 gw_html_escape(commit_log))) == -1) {
1795 error = got_error_from_errno("asprintf");
1799 log_commit_html = strdup("commit here");
1801 if ((asprintf(&commit_row, log_commit_row,
1802 commit_diff_disp, commit_commit_disp,
1803 commit_tree_disp, commit_author_disp,
1804 commit_committer_disp, commit_age_long_disp,
1805 commit_log_disp, log_commit_html)) == -1) {
1806 error = got_error_from_errno("asprintf");
1809 free(commit_log_disp);
1810 free(log_commit_html);
1814 if ((asprintf(&commit_log_disp, commit_log_html,
1815 gw_html_escape(commit_log))) == -1) {
1816 error = got_error_from_errno("asprintf");
1821 log_diff_html = gw_get_repo_diff(gw_trans,
1822 commit_commit, commit_parent);
1824 log_diff_html = gw_get_repo_diff(gw_trans,
1825 commit_commit, NULL);
1827 if ((asprintf(&commit_row, log_diff_row,
1828 commit_diff_disp, commit_commit_disp,
1829 commit_tree_disp, commit_author_disp,
1830 commit_committer_disp, commit_age_long_disp,
1831 commit_log_disp, log_diff_html)) == -1) {
1832 error = got_error_from_errno("asprintf");
1835 free(commit_log_disp);
1836 free(log_diff_html);
1843 error = buf_puts(&newsize, diffbuf, commit_row);
1845 free(commit_parent);
1846 free(commit_diff_disp);
1847 free(commit_tree_disp);
1848 free(commit_age_diff);
1849 free(commit_age_diff_disp);
1850 free(commit_age_long);
1851 free(commit_age_long_disp);
1852 free(commit_author);
1853 free(commit_author_disp);
1854 free(commit_committer);
1855 free(commit_committer_disp);
1858 free(refs_str_disp);
1866 if (error || (limit && --limit == 0))
1873 if (buf_len(diffbuf) > 0) {
1874 error = buf_putc(diffbuf, '\0');
1875 logs = strdup(buf_get(diffbuf));
1881 got_object_commit_close(commit);
1885 got_commit_graph_close(graph);
1887 error = got_repo_close(repo);
1892 khttp_puts(gw_trans->gw_req, "Error: ");
1893 khttp_puts(gw_trans->gw_req, error->msg);
1900 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
1902 const struct got_error *error = NULL;
1903 struct got_repository *repo = NULL;
1904 struct got_reflist_head refs;
1905 struct got_reflist_entry *re;
1906 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1909 struct buf *diffbuf = NULL;
1912 error = buf_alloc(&diffbuf, 0);
1915 SIMPLEQ_INIT(&refs);
1917 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1921 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1925 SIMPLEQ_FOREACH(re, &refs, entry) {
1926 const char *refname;
1927 char *refstr, *tag_log0, *tag_log, *id_str;
1929 struct got_object_id *id;
1930 struct got_tag_object *tag;
1932 refname = got_ref_get_name(re->ref);
1933 if (strncmp(refname, "refs/tags/", 10) != 0)
1936 refstr = got_ref_to_str(re->ref);
1937 if (refstr == NULL) {
1938 error = got_error_from_errno("got_ref_to_str");
1942 error = got_ref_resolve(&id, repo, re->ref);
1945 error = got_object_open_as_tag(&tag, repo, id);
1950 tagger_time = got_object_tag_get_tagger_time(tag);
1952 error = got_object_id_str(&id_str,
1953 got_object_tag_get_object_id(tag));
1957 tag_log0 = strdup(got_object_tag_get_message(tag));
1959 if (tag_log0 == NULL) {
1960 error = got_error_from_errno("strdup");
1965 while (*tag_log == '\n')
1970 newline = strchr(tag_log, '\n');
1974 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1976 error = got_error_from_errno("asprintf");
1980 if ((asprintf(&tags_navs_disp, tags_navs,
1981 gw_trans->repo_name, id_str, gw_trans->repo_name,
1982 id_str, gw_trans->repo_name, id_str,
1983 gw_trans->repo_name, id_str)) == -1) {
1984 error = got_error_from_errno("asprintf");
1988 if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
1989 tags_navs_disp)) == -1) {
1990 error = got_error_from_errno("asprintf");
1994 free(tags_navs_disp);
2002 got_object_tag_close(tag);
2004 error = buf_puts(&newsize, diffbuf, tag_row);
2012 if (error || (limit && --limit == 0))
2016 if (buf_len(diffbuf) > 0) {
2017 error = buf_putc(diffbuf, '\0');
2018 tags = strdup(buf_get(diffbuf));
2022 got_ref_list_free(&refs);
2024 got_repo_close(repo);
2035 char datebuf[11]; /* YYYY-MM-DD + NUL */
2038 struct gw_blame_cb_args {
2039 struct blame_line *lines;
2043 off_t *line_offsets;
2045 struct got_repository *repo;
2046 struct gw_trans *gw_trans;
2047 struct buf *blamebuf;
2050 static const struct got_error *
2051 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2053 const struct got_error *err = NULL;
2054 struct gw_blame_cb_args *a = arg;
2055 struct blame_line *bline;
2057 size_t linesize = 0, newsize;
2058 struct got_commit_object *commit = NULL;
2061 time_t committer_time;
2063 if (nlines != a->nlines ||
2064 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2065 return got_error(GOT_ERR_RANGE);
2068 return NULL; /* no change in this commit */
2070 /* Annotate this line. */
2071 bline = &a->lines[lineno - 1];
2072 if (bline->annotated)
2074 err = got_object_id_str(&bline->id_str, id);
2078 err = got_object_open_as_commit(&commit, a->repo, id);
2082 bline->committer = strdup(got_object_commit_get_committer(commit));
2083 if (bline->committer == NULL) {
2084 err = got_error_from_errno("strdup");
2088 committer_time = got_object_commit_get_committer_time(commit);
2089 if (localtime_r(&committer_time, &tm) == NULL)
2090 return got_error_from_errno("localtime_r");
2091 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2092 &tm) >= sizeof(bline->datebuf)) {
2093 err = got_error(GOT_ERR_NO_SPACE);
2096 bline->annotated = 1;
2098 /* Print lines annotated so far. */
2099 bline = &a->lines[a->lineno_cur - 1];
2100 if (!bline->annotated)
2103 offset = a->line_offsets[a->lineno_cur - 1];
2104 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2105 err = got_error_from_errno("fseeko");
2109 while (bline->annotated) {
2110 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2111 *line_escape = NULL;
2114 if (getline(&line, &linesize, a->f) == -1) {
2116 err = got_error_from_errno("getline");
2120 committer = bline->committer;
2121 smallerthan = strchr(committer, '<');
2122 if (smallerthan && smallerthan[1] != '\0')
2123 committer = smallerthan + 1;
2124 at = strchr(committer, '@');
2127 len = strlen(committer);
2129 committer[8] = '\0';
2131 nl = strchr(line, '\n');
2135 if (strcmp(line, "") != 0)
2136 line_escape = strdup(gw_html_escape(line));
2138 line_escape = strdup("");
2140 asprintf(&blame_row, log_blame_line, a->nlines_prec,
2141 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2144 err = buf_puts(&newsize, a->blamebuf, blame_row);
2148 bline = &a->lines[a->lineno_cur - 1];
2154 got_object_commit_close(commit);
2160 gw_get_file_blame(struct gw_trans *gw_trans, char *commit_str)
2162 const struct got_error *error = NULL;
2163 struct got_repository *repo = NULL;
2164 struct got_object_id *obj_id = NULL;
2165 struct got_object_id *commit_id = NULL;
2166 struct got_blob_object *blob = NULL;
2167 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2169 struct gw_blame_cb_args bca;
2173 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2177 if (gw_trans->repo_folder != NULL) {
2178 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2179 error = got_error_from_errno("asprintf");
2183 folder = strdup("");
2185 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2186 error = got_error_from_errno("asprintf");
2191 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2195 error = got_repo_match_object_id(&commit_id, NULL, commit_str,
2196 GOT_OBJ_TYPE_COMMIT, 1, repo);
2200 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2204 if (obj_id == NULL) {
2205 error = got_error(GOT_ERR_NO_OBJ);
2209 error = got_object_get_type(&obj_type, repo, obj_id);
2213 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2214 error = got_error(GOT_ERR_OBJ_TYPE);
2218 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2222 error = buf_alloc(&bca.blamebuf, 0);
2226 bca.f = got_opentemp();
2227 if (bca.f == NULL) {
2228 error = got_error_from_errno("got_opentemp");
2231 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2232 &bca.line_offsets, bca.f, blob);
2233 if (error || bca.nlines == 0)
2236 /* Don't include \n at EOF in the blame line count. */
2237 if (bca.line_offsets[bca.nlines - 1] == filesize)
2240 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2241 if (bca.lines == NULL) {
2242 error = got_error_from_errno("calloc");
2246 bca.nlines_prec = 0;
2253 bca.gw_trans = gw_trans;
2255 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2257 if (buf_len(bca.blamebuf) > 0) {
2258 error = buf_putc(bca.blamebuf, '\0');
2259 blame_html = strdup(buf_get(bca.blamebuf));
2269 error = got_object_blob_close(blob);
2271 error = got_repo_close(repo);
2275 for (i = 0; i < bca.nlines; i++) {
2276 struct blame_line *bline = &bca.lines[i];
2277 free(bline->id_str);
2278 free(bline->committer);
2282 free(bca.line_offsets);
2283 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2284 error = got_error_from_errno("fclose");
2292 gw_get_repo_tree(struct gw_trans *gw_trans, char *commit_str)
2294 const struct got_error *error = NULL;
2295 struct got_repository *repo = NULL;
2296 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2297 struct got_tree_object *tree = NULL;
2298 struct buf *diffbuf = NULL;
2300 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2301 *tree_row = NULL, *id_str;
2304 error = buf_alloc(&diffbuf, 0);
2308 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2312 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2316 if (gw_trans->repo_folder != NULL)
2317 path = strdup(gw_trans->repo_folder);
2318 else if (in_repo_path) {
2320 path = in_repo_path;
2323 if (commit_str == NULL) {
2324 struct got_reference *head_ref;
2325 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2329 error = got_ref_resolve(&commit_id, repo, head_ref);
2330 got_ref_close(head_ref);
2333 error = got_repo_match_object_id(&commit_id, NULL, commit_str,
2334 GOT_OBJ_TYPE_COMMIT, 1, repo);
2338 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2342 error = got_object_open_as_tree(&tree, repo, tree_id);
2346 nentries = got_object_tree_get_nentries(tree);
2348 for (i = 0; i < nentries; i++) {
2349 struct got_tree_entry *te;
2350 const char *modestr = "";
2351 char *id = NULL, *url_html = NULL;
2353 te = got_object_tree_get_entry(tree, i);
2355 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2359 if ((asprintf(&id, "%s", id_str)) == -1) {
2360 error = got_error_from_errno("asprintf");
2365 mode_t mode = got_tree_entry_get_mode(te);
2367 if (got_object_tree_entry_is_submodule(te))
2369 else if (S_ISLNK(mode))
2371 else if (S_ISDIR(mode))
2373 else if (mode & S_IXUSR)
2376 char *build_folder = NULL;
2377 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2378 if (gw_trans->repo_folder != NULL) {
2379 if ((asprintf(&build_folder, "%s/%s",
2380 gw_trans->repo_folder,
2381 got_tree_entry_get_name(te))) == -1) {
2383 got_error_from_errno("asprintf");
2387 if (asprintf(&build_folder, "%s",
2388 got_tree_entry_get_name(te)) == -1)
2392 if ((asprintf(&url_html, folder_html,
2393 gw_trans->repo_name, gw_trans->action_name,
2394 gw_trans->commit, build_folder,
2395 got_tree_entry_get_name(te), modestr)) == -1) {
2396 error = got_error_from_errno("asprintf");
2400 if (gw_trans->repo_folder != NULL) {
2401 if ((asprintf(&build_folder, "%s",
2402 gw_trans->repo_folder)) == -1) {
2404 got_error_from_errno("asprintf");
2408 build_folder = strdup("");
2410 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2411 "blame", gw_trans->commit,
2412 got_tree_entry_get_name(te), build_folder,
2413 got_tree_entry_get_name(te), modestr)) == -1) {
2414 error = got_error_from_errno("asprintf");
2423 if ((asprintf(&tree_row, trees_row, "", url_html)) == -1) {
2424 error = got_error_from_errno("asprintf");
2427 error = buf_puts(&newsize, diffbuf, tree_row);
2437 if (buf_len(diffbuf) > 0) {
2438 error = buf_putc(diffbuf, '\0');
2439 tree_html = strdup(buf_get(diffbuf));
2443 got_object_tree_close(tree);
2445 got_repo_close(repo);
2457 gw_get_repo_heads(struct gw_trans *gw_trans)
2459 const struct got_error *error = NULL;
2460 struct got_repository *repo = NULL;
2461 struct got_reflist_head refs;
2462 struct got_reflist_entry *re;
2463 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2464 struct buf *diffbuf = NULL;
2467 error = buf_alloc(&diffbuf, 0);
2471 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2475 SIMPLEQ_INIT(&refs);
2476 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2481 SIMPLEQ_FOREACH(re, &refs, entry) {
2484 refname = strdup(got_ref_get_name(re->ref));
2485 if (refname == NULL) {
2486 error = got_error_from_errno("got_ref_to_str");
2490 if (strncmp(refname, "refs/heads/", 11) != 0) {
2495 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2498 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2499 refname, gw_trans->repo_name, refname,
2500 gw_trans->repo_name, refname, gw_trans->repo_name,
2502 error = got_error_from_errno("asprintf");
2506 if (strncmp(refname, "refs/heads/", 11) == 0)
2509 if ((asprintf(&head_row, heads_row, age, refname,
2510 head_navs_disp)) == -1) {
2511 error = got_error_from_errno("asprintf");
2515 error = buf_puts(&newsize, diffbuf, head_row);
2517 free(head_navs_disp);
2521 if (buf_len(diffbuf) > 0) {
2522 error = buf_putc(diffbuf, '\0');
2523 heads = strdup(buf_get(diffbuf));
2527 got_ref_list_free(&refs);
2529 got_repo_close(repo);
2537 gw_get_got_link(struct gw_trans *gw_trans)
2541 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2542 gw_trans->gw_conf->got_logo)) == -1)
2549 gw_get_site_link(struct gw_trans *gw_trans)
2551 char *link, *repo = "", *action = "";
2553 if (gw_trans->repo_name != NULL)
2554 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2555 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2558 if (gw_trans->action_name != NULL)
2559 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2562 if ((asprintf(&link, site_link, GOTWEB,
2563 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2570 gw_colordiff_line(char *buf)
2572 const struct got_error *error = NULL;
2573 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2574 struct buf *diffbuf = NULL;
2577 error = buf_alloc(&diffbuf, 0);
2581 if (strncmp(buf, "-", 1) == 0)
2582 color = "diff_minus";
2583 if (strncmp(buf, "+", 1) == 0)
2584 color = "diff_plus";
2585 if (strncmp(buf, "@@", 2) == 0)
2586 color = "diff_chunk_header";
2587 if (strncmp(buf, "@@", 2) == 0)
2588 color = "diff_chunk_header";
2589 if (strncmp(buf, "commit +", 8) == 0)
2590 color = "diff_meta";
2591 if (strncmp(buf, "commit -", 8) == 0)
2592 color = "diff_meta";
2593 if (strncmp(buf, "blob +", 6) == 0)
2594 color = "diff_meta";
2595 if (strncmp(buf, "blob -", 6) == 0)
2596 color = "diff_meta";
2597 if (strncmp(buf, "file +", 6) == 0)
2598 color = "diff_meta";
2599 if (strncmp(buf, "file -", 6) == 0)
2600 color = "diff_meta";
2601 if (strncmp(buf, "from:", 5) == 0)
2602 color = "diff_author";
2603 if (strncmp(buf, "via:", 4) == 0)
2604 color = "diff_author";
2605 if (strncmp(buf, "date:", 5) == 0)
2606 color = "diff_date";
2608 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2611 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2615 error = buf_puts(&newsize, diffbuf, buf);
2619 if (buf_len(diffbuf) > 0) {
2620 error = buf_putc(diffbuf, '\0');
2621 colorized_line = strdup(buf_get(diffbuf));
2625 free(div_diff_line_div);
2626 return colorized_line;
2630 gw_html_escape(const char *html)
2632 char *escaped_str = NULL, *buf;
2634 size_t sz, i, buff_sz = 2048;
2636 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2642 if ((sz = strlen(html)) == 0)
2645 /* only work with buff_sz */
2649 for (i = 0; i < sz; i++) {
2653 strcat(buf, ">");
2656 strcat(buf, "&");
2659 strcat(buf, "<");
2662 strcat(buf, """);
2665 strcat(buf, "'");
2668 strcat(buf, "<br />");
2674 asprintf(&escaped_str, "%s", buf);
2680 main(int argc, char *argv[])
2682 const struct got_error *error = NULL;
2683 struct gw_trans *gw_trans;
2684 struct gw_dir *dir = NULL, *tdir;
2685 const char *page = "index";
2688 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2691 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2694 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2697 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2700 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX,
2702 errx(1, "khttp_parse");
2704 if ((gw_trans->gw_conf =
2705 malloc(sizeof(struct gotweb_conf))) == NULL) {
2707 error = got_error_from_errno("malloc");
2711 TAILQ_INIT(&gw_trans->gw_dirs);
2714 gw_trans->repos_total = 0;
2715 gw_trans->repo_path = NULL;
2716 gw_trans->commit = NULL;
2717 gw_trans->headref = strdup(GOT_REF_HEAD);
2718 gw_trans->mime = KMIME_TEXT_HTML;
2719 gw_trans->gw_tmpl->key = gw_templs;
2720 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2721 gw_trans->gw_tmpl->arg = gw_trans;
2722 gw_trans->gw_tmpl->cb = gw_template;
2723 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2727 gw_trans->mime = KMIME_TEXT_PLAIN;
2728 gw_trans->action = GW_ERR;
2729 gw_display_index(gw_trans, error);
2733 error = gw_parse_querystring(gw_trans);
2737 gw_display_index(gw_trans, error);
2741 free(gw_trans->gw_conf->got_repos_path);
2742 free(gw_trans->gw_conf->got_www_path);
2743 free(gw_trans->gw_conf->got_site_name);
2744 free(gw_trans->gw_conf->got_site_owner);
2745 free(gw_trans->gw_conf->got_site_link);
2746 free(gw_trans->gw_conf->got_logo);
2747 free(gw_trans->gw_conf->got_logo_url);
2748 free(gw_trans->gw_conf);
2749 free(gw_trans->commit);
2750 free(gw_trans->repo_path);
2751 free(gw_trans->repo_name);
2752 free(gw_trans->repo_file);
2753 free(gw_trans->action_name);
2754 free(gw_trans->headref);
2756 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2758 free(dir->description);
2767 khttp_free(gw_trans->gw_req);
2768 return EXIT_SUCCESS;