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>
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(headers, gw_header) gw_headers;
59 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
60 struct gw_dir *gw_dir;
61 struct gotweb_conf *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
74 unsigned int repos_total;
79 TAILQ_ENTRY(gw_header) entry;
80 struct got_repository *repo;
81 struct got_reflist_head refs;
82 struct got_commit_object *commit;
83 struct got_object_id *id;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
93 time_t committer_time;
97 TAILQ_ENTRY(gw_dir) entry;
138 static const char *const gw_templs[TEMPL__MAX] = {
148 static const struct kvalid gw_keys[KEY__ZMAX] = {
149 { kvalid_stringne, "action" },
150 { kvalid_stringne, "commit" },
151 { kvalid_stringne, "file" },
152 { kvalid_stringne, "folder" },
153 { kvalid_stringne, "headref" },
154 { kvalid_int, "page" },
155 { kvalid_stringne, "path" },
158 static struct gw_dir *gw_init_gw_dir(char *);
159 static struct gw_header *gw_init_header(void);
161 static const struct got_error *gw_get_repo_description(char **, struct gw_trans *,
163 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
165 static char *gw_get_time_str(time_t, int);
166 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
167 char *, char *, int);
168 static char *gw_get_file_blame_blob(struct gw_trans *);
169 static char *gw_get_repo_tree(struct gw_trans *);
170 static char *gw_get_diff(struct gw_trans *,
172 static char *gw_get_repo_tags(struct gw_trans *,
173 struct gw_header *, int, int);
174 static char *gw_get_repo_heads(struct gw_trans *);
175 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *, char *);
176 static char *gw_get_got_link(struct gw_trans *);
177 static char *gw_get_site_link(struct gw_trans *);
178 static char *gw_html_escape(const char *);
179 static char *gw_colordiff_line(char *);
181 static char *gw_gen_commit_header(char *, char*);
182 static char *gw_gen_diff_header(char *, char*);
183 static char *gw_gen_author_header(char *);
184 static char *gw_gen_committer_header(char *);
185 static char *gw_gen_age_header(char *);
186 static char *gw_gen_commit_msg_header(char *);
187 static char *gw_gen_tree_header(char *);
189 static void gw_free_headers(struct gw_header *);
190 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
192 static const struct got_error* gw_display_index(struct gw_trans *);
193 static void gw_display_error(struct gw_trans *,
194 const struct got_error *);
196 static int gw_template(size_t, void *);
198 static const struct got_error* gw_get_header(struct gw_trans *,
199 struct gw_header *, int);
200 static const struct got_error* gw_get_commits(struct gw_trans *,
201 struct gw_header *, int);
202 static const struct got_error* gw_get_commit(struct gw_trans *,
204 static const struct got_error* gw_apply_unveil(const char *, const char *);
205 static const struct got_error* gw_blame_cb(void *, int, int,
206 struct got_object_id *);
207 static const struct got_error* gw_load_got_paths(struct gw_trans *);
208 static const struct got_error* gw_load_got_path(struct gw_trans *,
210 static const struct got_error* gw_parse_querystring(struct gw_trans *);
212 static const struct got_error* gw_blame(struct gw_trans *);
213 static const struct got_error* gw_blob(struct gw_trans *);
214 static const struct got_error* gw_diff(struct gw_trans *);
215 static const struct got_error* gw_index(struct gw_trans *);
216 static const struct got_error* gw_commits(struct gw_trans *);
217 static const struct got_error* gw_briefs(struct gw_trans *);
218 static const struct got_error* gw_summary(struct gw_trans *);
219 static const struct got_error* gw_tree(struct gw_trans *);
220 static const struct got_error* gw_tag(struct gw_trans *);
222 struct gw_query_action {
223 unsigned int func_id;
224 const char *func_name;
225 const struct got_error *(*func_main)(struct gw_trans *);
229 enum gw_query_actions {
242 static struct gw_query_action gw_query_funcs[] = {
243 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
244 { GW_BLOB, "blob", NULL, NULL },
245 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
246 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
247 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
248 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
249 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
250 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
251 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
252 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
255 static const struct got_error *
256 gw_kcgi_error(enum kcgi_err kerr)
261 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
262 return got_error(GOT_ERR_CANCELLED);
264 if (kerr == KCGI_ENOMEM)
265 return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
267 if (kerr == KCGI_ENFILE)
268 return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
270 if (kerr == KCGI_EAGAIN)
271 return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
273 if (kerr == KCGI_FORM)
274 return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
276 return got_error_from_errno(kcgi_strerror(kerr));
279 static const struct got_error *
280 gw_apply_unveil(const char *repo_path, const char *repo_file)
282 const struct got_error *err;
284 if (repo_path && repo_file) {
286 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
287 return got_error_from_errno("asprintf unveil");
288 if (unveil(full_path, "r") != 0)
289 return got_error_from_errno2("unveil", full_path);
292 if (repo_path && unveil(repo_path, "r") != 0)
293 return got_error_from_errno2("unveil", repo_path);
295 if (unveil("/tmp", "rwc") != 0)
296 return got_error_from_errno2("unveil", "/tmp");
298 err = got_privsep_unveil_exec_helpers();
302 if (unveil(NULL, NULL) != 0)
303 return got_error_from_errno("unveil");
308 static const struct got_error *
309 gw_empty_string(char **s)
313 return got_error_from_errno("strdup");
317 static const struct got_error *
318 gw_blame(struct gw_trans *gw_trans)
320 const struct got_error *error = NULL;
321 struct gw_header *header = NULL;
322 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
325 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
327 return got_error_from_errno("pledge");
329 if ((header = gw_init_header()) == NULL)
330 return got_error_from_errno("malloc");
332 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
336 error = gw_get_header(gw_trans, header, 1);
340 blame_html = gw_get_file_blame_blob(gw_trans);
342 if (blame_html == NULL) {
343 blame_html = strdup("");
344 if (blame_html == NULL) {
345 error = got_error_from_errno("strdup");
350 if (asprintf(&blame_html_disp, blame_header,
351 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
352 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
354 error = got_error_from_errno("asprintf");
358 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
359 error = got_error_from_errno("asprintf");
363 kerr = khttp_puts(gw_trans->gw_req, blame);
365 error = gw_kcgi_error(kerr);
367 got_ref_list_free(&header->refs);
368 gw_free_headers(header);
369 free(blame_html_disp);
375 static const struct got_error *
376 gw_blob(struct gw_trans *gw_trans)
378 const struct got_error *error = NULL;
379 struct gw_header *header = NULL;
383 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
385 return got_error_from_errno("pledge");
387 if ((header = gw_init_header()) == NULL)
388 return got_error_from_errno("malloc");
390 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
394 error = gw_get_header(gw_trans, header, 1);
398 blob = gw_get_file_blame_blob(gw_trans);
403 error = got_error_from_errno("strdup");
408 if (gw_trans->mime == KMIME_APP_OCTET_STREAM)
411 kerr = khttp_puts(gw_trans->gw_req, blob);
413 error = gw_kcgi_error(kerr);
416 got_ref_list_free(&header->refs);
417 gw_free_headers(header);
422 static const struct got_error *
423 gw_diff(struct gw_trans *gw_trans)
425 const struct got_error *error = NULL;
426 struct gw_header *header = NULL;
427 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
430 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
432 return got_error_from_errno("pledge");
434 if ((header = gw_init_header()) == NULL)
435 return got_error_from_errno("malloc");
437 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
441 error = gw_get_header(gw_trans, header, 1);
445 diff_html = gw_get_diff(gw_trans, header);
447 if (diff_html == NULL) {
448 diff_html = strdup("");
449 if (diff_html == NULL) {
450 error = got_error_from_errno("strdup");
455 if (asprintf(&diff_html_disp, diff_header,
456 gw_gen_diff_header(header->parent_id, header->commit_id),
457 gw_gen_commit_header(header->commit_id, header->refs_str),
458 gw_gen_tree_header(header->tree_id),
459 gw_gen_author_header(header->author),
460 gw_gen_committer_header(header->committer),
461 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
462 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
464 error = got_error_from_errno("asprintf");
468 if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
469 error = got_error_from_errno("asprintf");
473 kerr = khttp_puts(gw_trans->gw_req, diff);
475 error = gw_kcgi_error(kerr);
477 got_ref_list_free(&header->refs);
478 gw_free_headers(header);
479 free(diff_html_disp);
485 static const struct got_error *
486 gw_index(struct gw_trans *gw_trans)
488 const struct got_error *error = NULL;
489 struct gw_dir *gw_dir = NULL;
490 char *html, *navs, *next, *prev;
491 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
494 if (pledge("stdio rpath proc exec sendfd unveil",
496 error = got_error_from_errno("pledge");
500 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
504 error = gw_load_got_paths(gw_trans);
508 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
510 return gw_kcgi_error(kerr);
512 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
513 if (asprintf(&html, index_projects_empty,
514 gw_trans->gw_conf->got_repos_path) == -1)
515 return got_error_from_errno("asprintf");
516 kerr = khttp_puts(gw_trans->gw_req, html);
518 error = gw_kcgi_error(kerr);
523 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
526 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
527 if (gw_trans->page > 0 && (gw_trans->page *
528 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
537 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
538 gw_dir->name, gw_dir->name) == -1)
539 return got_error_from_errno("asprintf");
541 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
542 gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
545 return got_error_from_errno("asprintf");
547 kerr = khttp_puts(gw_trans->gw_req, html);
551 return gw_kcgi_error(kerr);
553 if (gw_trans->gw_conf->got_max_repos_display == 0)
556 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
557 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
559 return gw_kcgi_error(kerr);
560 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
561 (gw_trans->page > 0) &&
562 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
563 prev_disp == gw_trans->repos_total)) {
564 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
566 return gw_kcgi_error(kerr);
569 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
570 (gw_trans->page > 0) &&
571 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
572 prev_disp == gw_trans->repos_total)) {
573 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
574 return got_error_from_errno("asprintf");
575 kerr = khttp_puts(gw_trans->gw_req, prev);
578 return gw_kcgi_error(kerr);
581 kerr = khttp_puts(gw_trans->gw_req, div_end);
583 return gw_kcgi_error(kerr);
585 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
586 next_disp == gw_trans->gw_conf->got_max_repos_display &&
587 dir_c != (gw_trans->page + 1) *
588 gw_trans->gw_conf->got_max_repos_display) {
589 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
590 return got_error_from_errno("calloc");
591 kerr = khttp_puts(gw_trans->gw_req, next);
594 return gw_kcgi_error(kerr);
595 kerr = khttp_puts(gw_trans->gw_req, div_end);
597 error = gw_kcgi_error(kerr);
602 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
603 (gw_trans->page > 0) &&
604 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
605 prev_disp == gw_trans->repos_total)) {
606 kerr = khttp_puts(gw_trans->gw_req, div_end);
608 return gw_kcgi_error(kerr);
616 static const struct got_error *
617 gw_commits(struct gw_trans *gw_trans)
619 const struct got_error *error = NULL;
620 char *commits_html, *commits_navs_html;
621 struct gw_header *header = NULL, *n_header = NULL;
624 if ((header = gw_init_header()) == NULL)
625 return got_error_from_errno("malloc");
627 if (pledge("stdio rpath proc exec sendfd unveil",
629 error = got_error_from_errno("pledge");
633 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
637 error = gw_get_header(gw_trans, header,
638 gw_trans->gw_conf->got_max_commits_display);
642 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
643 if (kerr != KCGI_OK) {
644 error = gw_kcgi_error(kerr);
647 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
648 if (asprintf(&commits_navs_html, commits_navs,
649 gw_trans->repo_name, n_header->commit_id,
650 gw_trans->repo_name, n_header->commit_id,
651 gw_trans->repo_name, n_header->commit_id) == -1) {
652 error = got_error_from_errno("asprintf");
656 if (asprintf(&commits_html, commits_line,
657 gw_gen_commit_header(n_header->commit_id,
659 gw_gen_author_header(n_header->author),
660 gw_gen_committer_header(n_header->committer),
661 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
662 TM_LONG)), gw_html_escape(n_header->commit_msg),
663 commits_navs_html) == -1) {
664 error = got_error_from_errno("asprintf");
667 kerr = khttp_puts(gw_trans->gw_req, commits_html);
668 if (kerr != KCGI_OK) {
669 error = gw_kcgi_error(kerr);
673 kerr = khttp_puts(gw_trans->gw_req, div_end);
675 error = gw_kcgi_error(kerr);
677 got_ref_list_free(&header->refs);
678 gw_free_headers(header);
679 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
680 gw_free_headers(n_header);
684 static const struct got_error *
685 gw_briefs(struct gw_trans *gw_trans)
687 const struct got_error *error = NULL;
688 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
689 struct gw_header *header = NULL, *n_header = NULL;
692 if ((header = gw_init_header()) == NULL)
693 return got_error_from_errno("malloc");
695 if (pledge("stdio rpath proc exec sendfd unveil",
697 error = got_error_from_errno("pledge");
701 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
705 if (gw_trans->action == GW_SUMMARY)
706 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
708 error = gw_get_header(gw_trans, header,
709 gw_trans->gw_conf->got_max_commits_display);
713 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
714 if (kerr != KCGI_OK) {
715 error = gw_kcgi_error(kerr);
719 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
720 if (asprintf(&briefs_navs_html, briefs_navs,
721 gw_trans->repo_name, n_header->commit_id,
722 gw_trans->repo_name, n_header->commit_id,
723 gw_trans->repo_name, n_header->commit_id) == -1) {
724 error = got_error_from_errno("asprintf");
727 newline = strchr(n_header->commit_msg, '\n');
730 if (asprintf(&briefs_html, briefs_line,
731 gw_get_time_str(n_header->committer_time, TM_DIFF),
732 n_header->author, gw_html_escape(n_header->commit_msg),
733 briefs_navs_html) == -1) {
734 error = got_error_from_errno("asprintf");
737 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
738 if (kerr != KCGI_OK) {
739 error = gw_kcgi_error(kerr);
743 kerr = khttp_puts(gw_trans->gw_req, div_end);
745 error = gw_kcgi_error(kerr);
747 got_ref_list_free(&header->refs);
748 gw_free_headers(header);
749 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
750 gw_free_headers(n_header);
754 static const struct got_error *
755 gw_summary(struct gw_trans *gw_trans)
757 const struct got_error *error = NULL;
758 char *description_html = NULL, *repo_owner_html = NULL;
759 char *age = NULL, *repo_age_html = NULL, *cloneurl_html = NULL;
760 char *tags = NULL, *tags_html = NULL;
761 char *heads = NULL, *heads_html = NULL;
764 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
765 return got_error_from_errno("pledge");
767 /* unveil is applied with gw_briefs below */
769 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
771 return gw_kcgi_error(kerr);
773 if (gw_trans->gw_conf->got_show_repo_description) {
774 if (gw_trans->gw_dir->description != NULL &&
775 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
776 if (asprintf(&description_html, description,
777 gw_trans->gw_dir->description) == -1) {
778 error = got_error_from_errno("asprintf");
782 kerr = khttp_puts(gw_trans->gw_req, description_html);
783 if (kerr != KCGI_OK) {
784 error = gw_kcgi_error(kerr);
790 if (gw_trans->gw_conf->got_show_repo_owner &&
791 gw_trans->gw_dir->owner != NULL) {
792 if (asprintf(&repo_owner_html, repo_owner,
793 gw_trans->gw_dir->owner) == -1) {
794 error = got_error_from_errno("asprintf");
798 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
799 if (kerr != KCGI_OK) {
800 error = gw_kcgi_error(kerr);
805 if (gw_trans->gw_conf->got_show_repo_age) {
806 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
807 "refs/heads", TM_LONG);
810 if (strcmp(age, "") != 0) {
811 if (asprintf(&repo_age_html, last_change, age) == -1) {
812 error = got_error_from_errno("asprintf");
816 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
817 if (kerr != KCGI_OK) {
818 error = gw_kcgi_error(kerr);
824 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
825 if (gw_trans->gw_dir->url != NULL &&
826 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
827 if (asprintf(&cloneurl_html, cloneurl,
828 gw_trans->gw_dir->url) == -1) {
829 error = got_error_from_errno("asprintf");
833 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
834 if (kerr != KCGI_OK) {
835 error = gw_kcgi_error(kerr);
840 kerr = khttp_puts(gw_trans->gw_req, div_end);
841 if (kerr != KCGI_OK) {
842 error = gw_kcgi_error(kerr);
846 error = gw_briefs(gw_trans);
850 tags = gw_get_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP, TAGBRIEF);
851 heads = gw_get_repo_heads(gw_trans);
853 if (tags != NULL && strcmp(tags, "") != 0) {
854 if (asprintf(&tags_html, summary_tags, tags) == -1) {
855 error = got_error_from_errno("asprintf");
858 kerr = khttp_puts(gw_trans->gw_req, tags_html);
859 if (kerr != KCGI_OK) {
860 error = gw_kcgi_error(kerr);
865 if (heads != NULL && strcmp(heads, "") != 0) {
866 if (asprintf(&heads_html, summary_heads, heads) == -1) {
867 error = got_error_from_errno("asprintf");
870 kerr = khttp_puts(gw_trans->gw_req, heads_html);
871 if (kerr != KCGI_OK) {
872 error = gw_kcgi_error(kerr);
877 free(description_html);
878 free(repo_owner_html);
889 static const struct got_error *
890 gw_tree(struct gw_trans *gw_trans)
892 const struct got_error *error = NULL;
893 struct gw_header *header = NULL;
894 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
897 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
898 return got_error_from_errno("pledge");
900 if ((header = gw_init_header()) == NULL)
901 return got_error_from_errno("malloc");
903 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
907 error = gw_get_header(gw_trans, header, 1);
911 tree_html = gw_get_repo_tree(gw_trans);
913 if (tree_html == NULL) {
914 tree_html = strdup("");
915 if (tree_html == NULL) {
916 error = got_error_from_errno("strdup");
921 if (asprintf(&tree_html_disp, tree_header,
922 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
923 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
925 error = got_error_from_errno("asprintf");
929 if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
930 error = got_error_from_errno("asprintf");
934 kerr = khttp_puts(gw_trans->gw_req, tree);
936 error = gw_kcgi_error(kerr);
938 got_ref_list_free(&header->refs);
939 gw_free_headers(header);
940 free(tree_html_disp);
946 static const struct got_error *
947 gw_tag(struct gw_trans *gw_trans)
949 const struct got_error *error = NULL;
950 struct gw_header *header = NULL;
951 char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
954 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
955 return got_error_from_errno("pledge");
957 if ((header = gw_init_header()) == NULL)
958 return got_error_from_errno("malloc");
960 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
964 error = gw_get_header(gw_trans, header, 1);
968 tag_html = gw_get_repo_tags(gw_trans, header, 1, TAGFULL);
969 if (tag_html == NULL) {
970 tag_html = strdup("");
971 if (tag_html == NULL) {
972 error = got_error_from_errno("strdup");
977 if (asprintf(&tag_html_disp, tag_header,
978 gw_gen_commit_header(header->commit_id, header->refs_str),
979 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
981 error = got_error_from_errno("asprintf");
985 if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
986 error = got_error_from_errno("asprintf");
990 kerr = khttp_puts(gw_trans->gw_req, tag);
992 error = gw_kcgi_error(kerr);
994 got_ref_list_free(&header->refs);
995 gw_free_headers(header);
1002 static const struct got_error *
1003 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1005 const struct got_error *error = NULL;
1010 if (asprintf(&dir_test, "%s/%s/%s",
1011 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1012 GOTWEB_GIT_DIR) == -1)
1013 return got_error_from_errno("asprintf");
1015 dt = opendir(dir_test);
1019 gw_dir->path = strdup(dir_test);
1024 if (asprintf(&dir_test, "%s/%s/%s",
1025 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1026 GOTWEB_GOT_DIR) == -1)
1027 return got_error_from_errno("asprintf");
1029 dt = opendir(dir_test);
1034 error = got_error(GOT_ERR_NOT_GIT_REPO);
1038 if (asprintf(&dir_test, "%s/%s",
1039 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1040 return got_error_from_errno("asprintf");
1042 gw_dir->path = strdup(dir_test);
1045 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1049 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1052 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1053 "refs/heads", TM_DIFF);
1056 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1064 static const struct got_error *
1065 gw_load_got_paths(struct gw_trans *gw_trans)
1067 const struct got_error *error = NULL;
1069 struct dirent **sd_dent;
1070 struct gw_dir *gw_dir;
1072 unsigned int d_cnt, d_i;
1074 d = opendir(gw_trans->gw_conf->got_repos_path);
1076 error = got_error_from_errno2("opendir",
1077 gw_trans->gw_conf->got_repos_path);
1081 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1084 error = got_error_from_errno2("scandir",
1085 gw_trans->gw_conf->got_repos_path);
1089 for (d_i = 0; d_i < d_cnt; d_i++) {
1090 if (gw_trans->gw_conf->got_max_repos > 0 &&
1091 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1092 break; /* account for parent and self */
1094 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1095 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1098 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1099 return got_error_from_errno("gw_dir malloc");
1101 error = gw_load_got_path(gw_trans, gw_dir);
1102 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1107 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1108 !got_path_dir_is_empty(gw_dir->path)) {
1109 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1111 gw_trans->repos_total++;
1119 static const struct got_error *
1120 gw_parse_querystring(struct gw_trans *gw_trans)
1122 const struct got_error *error = NULL;
1124 struct gw_query_action *action = NULL;
1127 if (gw_trans->gw_req->fieldnmap[0]) {
1128 error = got_error_from_errno("bad parse");
1130 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1131 /* define gw_trans->repo_path */
1132 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1133 return got_error_from_errno("asprintf");
1135 if (asprintf(&gw_trans->repo_path, "%s/%s",
1136 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1137 return got_error_from_errno("asprintf");
1139 /* get action and set function */
1140 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1141 for (i = 0; i < nitems(gw_query_funcs); i++) {
1142 action = &gw_query_funcs[i];
1143 if (action->func_name == NULL)
1146 if (strcmp(action->func_name,
1147 p->parsed.s) == 0) {
1148 gw_trans->action = i;
1149 if (asprintf(&gw_trans->action_name,
1150 "%s", action->func_name) == -1)
1152 got_error_from_errno(
1161 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1162 if (asprintf(&gw_trans->commit, "%s",
1164 return got_error_from_errno("asprintf");
1166 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1167 if (asprintf(&gw_trans->repo_file, "%s",
1169 return got_error_from_errno("asprintf");
1171 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1172 if (asprintf(&gw_trans->repo_folder, "%s",
1174 return got_error_from_errno("asprintf");
1176 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1177 if (asprintf(&gw_trans->headref, "%s",
1179 return got_error_from_errno("asprintf");
1181 if (action == NULL) {
1182 error = got_error_from_errno("invalid action");
1185 if ((gw_trans->gw_dir =
1186 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1187 return got_error_from_errno("gw_dir malloc");
1189 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1193 gw_trans->action = GW_INDEX;
1195 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1196 gw_trans->page = p->parsed.i;
1201 static struct gw_dir *
1202 gw_init_gw_dir(char *dir)
1204 struct gw_dir *gw_dir;
1206 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1209 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1215 static const struct got_error *
1216 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1220 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1221 if (kerr != KCGI_OK)
1222 return gw_kcgi_error(kerr);
1223 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1225 if (kerr != KCGI_OK)
1226 return gw_kcgi_error(kerr);
1227 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1229 if (kerr != KCGI_OK)
1230 return gw_kcgi_error(kerr);
1231 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1233 if (kerr != KCGI_OK)
1234 return gw_kcgi_error(kerr);
1235 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1236 if (kerr != KCGI_OK)
1237 return gw_kcgi_error(kerr);
1238 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1240 if (kerr != KCGI_OK)
1241 return gw_kcgi_error(kerr);
1243 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1244 kerr = khttp_head(gw_trans->gw_req,
1245 kresps[KRESP_CONTENT_DISPOSITION],
1246 "attachment; filename=%s", gw_trans->repo_file);
1247 if (kerr != KCGI_OK)
1248 return gw_kcgi_error(kerr);
1251 kerr = khttp_body(gw_trans->gw_req);
1252 return gw_kcgi_error(kerr);
1255 static const struct got_error *
1256 gw_display_index(struct gw_trans *gw_trans)
1258 const struct got_error *error;
1261 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1265 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1267 return gw_kcgi_error(kerr);
1269 if (gw_trans->action != GW_BLOB) {
1270 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1271 gw_query_funcs[gw_trans->action].template);
1272 if (kerr != KCGI_OK) {
1273 khtml_close(gw_trans->gw_html_req);
1274 return gw_kcgi_error(kerr);
1278 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1282 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1284 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1287 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1290 khttp_puts(gw_trans->gw_req, err->msg);
1291 khtml_close(gw_trans->gw_html_req);
1295 gw_template(size_t key, void *arg)
1297 const struct got_error *error = NULL;
1298 struct gw_trans *gw_trans = arg;
1299 char *gw_got_link, *gw_site_link;
1300 char *site_owner_name, *site_owner_name_h;
1304 khttp_puts(gw_trans->gw_req, head);
1307 gw_got_link = gw_get_got_link(gw_trans);
1308 if (gw_got_link != NULL)
1309 khttp_puts(gw_trans->gw_req, gw_got_link);
1313 case (TEMPL_SITEPATH):
1314 gw_site_link = gw_get_site_link(gw_trans);
1315 if (gw_site_link != NULL)
1316 khttp_puts(gw_trans->gw_req, gw_site_link);
1321 if (gw_trans->gw_conf->got_site_name != NULL)
1322 khtml_puts(gw_trans->gw_html_req,
1323 gw_trans->gw_conf->got_site_name);
1326 case (TEMPL_SEARCH):
1327 khttp_puts(gw_trans->gw_req, search);
1329 case(TEMPL_SITEOWNER):
1330 if (gw_trans->gw_conf->got_site_owner != NULL &&
1331 gw_trans->gw_conf->got_show_site_owner) {
1333 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1334 if (asprintf(&site_owner_name_h, site_owner,
1335 site_owner_name) == -1)
1338 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1339 free(site_owner_name);
1340 free(site_owner_name_h);
1343 case(TEMPL_CONTENT):
1344 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1346 khttp_puts(gw_trans->gw_req, error->msg);
1355 gw_gen_commit_header(char *str1, char *str2)
1357 char *return_html = NULL, *ref_str = NULL;
1359 if (strcmp(str2, "") != 0) {
1360 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1361 return_html = strdup("");
1365 ref_str = strdup("");
1368 if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1369 return_html = strdup("");
1376 gw_gen_diff_header(char *str1, char *str2)
1378 char *return_html = NULL;
1380 if (asprintf(&return_html, header_diff_html, str1, str2) == -1)
1381 return_html = strdup("");
1387 gw_gen_author_header(char *str)
1389 char *return_html = NULL;
1391 if (asprintf(&return_html, header_author_html, str) == -1)
1392 return_html = strdup("");
1398 gw_gen_committer_header(char *str)
1400 char *return_html = NULL;
1402 if (asprintf(&return_html, header_committer_html, str) == -1)
1403 return_html = strdup("");
1409 gw_gen_age_header(char *str)
1411 char *return_html = NULL;
1413 if (asprintf(&return_html, header_age_html, str) == -1)
1414 return_html = strdup("");
1420 gw_gen_commit_msg_header(char *str)
1422 char *return_html = NULL;
1424 if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1425 return_html = strdup("");
1431 gw_gen_tree_header(char *str)
1433 char *return_html = NULL;
1435 if (asprintf(&return_html, header_tree_html, str) == -1)
1436 return_html = strdup("");
1441 static const struct got_error *
1442 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
1445 const struct got_error *error = NULL;
1447 char *d_file = NULL;
1451 *description = NULL;
1452 if (gw_trans->gw_conf->got_show_repo_description == 0)
1453 return gw_empty_string(description);
1455 if (asprintf(&d_file, "%s/description", dir) == -1)
1456 return got_error_from_errno("asprintf");
1458 f = fopen(d_file, "r");
1460 if (errno == ENOENT || errno == EACCES)
1461 return gw_empty_string(description);
1462 error = got_error_from_errno2("fopen", d_file);
1466 if (fseek(f, 0, SEEK_END) == -1) {
1467 error = got_ferror(f, GOT_ERR_IO);
1472 error = got_ferror(f, GOT_ERR_IO);
1475 if (fseek(f, 0, SEEK_SET) == -1) {
1476 error = got_ferror(f, GOT_ERR_IO);
1479 *description = calloc(len + 1, sizeof(**description));
1480 if (*description == NULL) {
1481 error = got_error_from_errno("calloc");
1485 n = fread(*description, 1, len, f);
1486 if (n == 0 && ferror(f))
1487 error = got_ferror(f, GOT_ERR_IO);
1489 if (f != NULL && fclose(f) == -1 && error == NULL)
1490 error = got_error_from_errno("fclose");
1496 gw_get_time_str(time_t committer_time, int ref_tm)
1500 char *years = "years ago", *months = "months ago";
1501 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1502 char *minutes = "minutes ago", *seconds = "seconds ago";
1503 char *now = "right now";
1509 diff_time = time(NULL) - committer_time;
1510 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1511 if (asprintf(&repo_age, "%lld %s",
1512 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1514 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1515 if (asprintf(&repo_age, "%lld %s",
1516 (diff_time / 60 / 60 / 24 / (365 / 12)),
1519 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1520 if (asprintf(&repo_age, "%lld %s",
1521 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1523 } else if (diff_time > 60 * 60 * 24 * 2) {
1524 if (asprintf(&repo_age, "%lld %s",
1525 (diff_time / 60 / 60 / 24), days) == -1)
1527 } else if (diff_time > 60 * 60 * 2) {
1528 if (asprintf(&repo_age, "%lld %s",
1529 (diff_time / 60 / 60), hours) == -1)
1531 } else if (diff_time > 60 * 2) {
1532 if (asprintf(&repo_age, "%lld %s", (diff_time / 60),
1535 } else if (diff_time > 2) {
1536 if (asprintf(&repo_age, "%lld %s", diff_time,
1540 if (asprintf(&repo_age, "%s", now) == -1)
1545 if (gmtime_r(&committer_time, &tm) == NULL)
1548 s = asctime_r(&tm, datebuf);
1552 if (asprintf(&repo_age, "%s UTC", datebuf) == -1)
1559 static const struct got_error *
1560 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
1561 char *repo_ref, int ref_tm)
1563 const struct got_error *error = NULL;
1564 struct got_object_id *id = NULL;
1565 struct got_repository *repo = NULL;
1566 struct got_commit_object *commit = NULL;
1567 struct got_reflist_head refs;
1568 struct got_reflist_entry *re;
1569 struct got_reference *head_ref;
1571 time_t committer_time = 0, cmp_time = 0;
1572 const char *refname;
1575 SIMPLEQ_INIT(&refs);
1577 if (repo_ref == NULL)
1580 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1583 if (gw_trans->gw_conf->got_show_repo_age == 0) {
1584 *repo_age = strdup("");
1585 if (*repo_age == NULL)
1586 return got_error_from_errno("strdup");
1590 error = got_repo_open(&repo, dir, NULL);
1595 error = got_ref_list(&refs, repo, "refs/heads",
1596 got_ref_cmp_by_name, NULL);
1598 error = got_ref_list(&refs, repo, repo_ref,
1599 got_ref_cmp_by_name, NULL);
1603 SIMPLEQ_FOREACH(re, &refs, entry) {
1605 refname = strdup(repo_ref);
1607 refname = got_ref_get_name(re->ref);
1608 error = got_ref_open(&head_ref, repo, refname, 0);
1612 error = got_ref_resolve(&id, repo, head_ref);
1613 got_ref_close(head_ref);
1617 error = got_object_open_as_commit(&commit, repo, id);
1622 got_object_commit_get_committer_time(commit);
1624 if (cmp_time < committer_time)
1625 cmp_time = committer_time;
1628 if (cmp_time != 0) {
1629 committer_time = cmp_time;
1630 *repo_age = gw_get_time_str(committer_time, ref_tm);
1632 *repo_age = strdup("");
1633 if (*repo_age == NULL)
1634 error = got_error_from_errno("strdup");
1637 got_ref_list_free(&refs);
1643 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1645 const struct got_error *error;
1647 struct got_object_id *id1 = NULL, *id2 = NULL;
1648 struct buf *diffbuf = NULL;
1649 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL;
1650 char *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1658 error = buf_alloc(&diffbuf, 0);
1662 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1666 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1667 error = got_repo_match_object_id(&id1, &label1,
1668 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1673 error = got_repo_match_object_id(&id2, &label2,
1674 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1678 error = got_object_get_type(&obj_type, header->repo, id2);
1682 case GOT_OBJ_TYPE_BLOB:
1683 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1686 case GOT_OBJ_TYPE_TREE:
1687 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1690 case GOT_OBJ_TYPE_COMMIT:
1691 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1695 error = got_error(GOT_ERR_OBJ_TYPE);
1701 if ((buf = calloc(128, sizeof(char *))) == NULL)
1704 if (fseek(f, 0, SEEK_SET) == -1)
1707 while ((fgets(buf, 2048, f)) != NULL) {
1711 while (*n_buf == '\n')
1713 newline = strchr(n_buf, '\n');
1717 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1718 if (buf_color == NULL)
1721 error = buf_puts(&newsize, diffbuf, buf_color);
1725 error = buf_puts(&newsize, diffbuf, div_end);
1730 if (buf_len(diffbuf) > 0) {
1731 error = buf_putc(diffbuf, '\0');
1732 diff_html = strdup(buf_get(diffbuf));
1750 static const struct got_error *
1751 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
1753 const struct got_error *error = NULL;
1754 struct got_repository *repo;
1755 const char *gitconfig_owner;
1759 if (gw_trans->gw_conf->got_show_repo_owner == 0)
1762 error = got_repo_open(&repo, dir, NULL);
1765 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
1766 if (gitconfig_owner) {
1767 *owner = strdup(gitconfig_owner);
1769 error = got_error_from_errno("strdup");
1771 got_repo_close(repo);
1775 static const struct got_error *
1776 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
1778 const struct got_error *error = NULL;
1780 char *d_file = NULL;
1786 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
1787 return got_error_from_errno("asprintf");
1789 f = fopen(d_file, "r");
1791 if (errno != ENOENT && errno != EACCES)
1792 error = got_error_from_errno2("fopen", d_file);
1796 if (fseek(f, 0, SEEK_END) == -1) {
1797 error = got_ferror(f, GOT_ERR_IO);
1802 error = got_ferror(f, GOT_ERR_IO);
1805 if (fseek(f, 0, SEEK_SET) == -1) {
1806 error = got_ferror(f, GOT_ERR_IO);
1810 *url = calloc(len + 1, sizeof(**url));
1812 error = got_error_from_errno("calloc");
1816 n = fread(*url, 1, len, f);
1817 if (n == 0 && ferror(f))
1818 error = got_ferror(f, GOT_ERR_IO);
1820 if (f && fclose(f) == -1 && error == NULL)
1821 error = got_error_from_errno("fclose");
1827 gw_get_repo_tags(struct gw_trans *gw_trans, struct gw_header *header, int limit,
1830 const struct got_error *error = NULL;
1831 struct got_repository *repo = NULL;
1832 struct got_reflist_head refs;
1833 struct got_reflist_entry *re;
1834 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL;
1835 char *age = NULL, *newline;
1836 struct buf *diffbuf = NULL;
1839 SIMPLEQ_INIT(&refs);
1841 error = buf_alloc(&diffbuf, 0);
1845 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1849 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1853 SIMPLEQ_FOREACH(re, &refs, entry) {
1854 const char *refname;
1855 char *refstr, *tag_commit0, *tag_commit, *id_str;
1858 struct got_object_id *id;
1859 struct got_tag_object *tag;
1861 refname = got_ref_get_name(re->ref);
1862 if (strncmp(refname, "refs/tags/", 10) != 0)
1865 refstr = got_ref_to_str(re->ref);
1866 if (refstr == NULL) {
1867 error = got_error_from_errno("got_ref_to_str");
1871 error = got_ref_resolve(&id, repo, re->ref);
1874 error = got_object_open_as_tag(&tag, repo, id);
1879 tagger = got_object_tag_get_tagger(tag);
1880 tagger_time = got_object_tag_get_tagger_time(tag);
1882 error = got_object_id_str(&id_str,
1883 got_object_tag_get_object_id(tag));
1887 tag_commit0 = strdup(got_object_tag_get_message(tag));
1889 if (tag_commit0 == NULL) {
1890 error = got_error_from_errno("strdup");
1894 tag_commit = tag_commit0;
1895 while (*tag_commit == '\n')
1900 newline = strchr(tag_commit, '\n');
1904 if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1906 error = got_error_from_errno("asprintf");
1910 if (asprintf(&tags_navs_disp, tags_navs,
1911 gw_trans->repo_name, id_str, gw_trans->repo_name,
1912 id_str, gw_trans->repo_name, id_str,
1913 gw_trans->repo_name, id_str) == -1) {
1914 error = got_error_from_errno("asprintf");
1918 if (asprintf(&tag_row, tags_row, age, refname,
1919 tag_commit, tags_navs_disp) == -1) {
1920 error = got_error_from_errno("asprintf");
1924 free(tags_navs_disp);
1927 if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1929 error = got_error_from_errno("asprintf");
1932 if (asprintf(&tag_row, tag_info, age,
1933 gw_html_escape(tagger),
1934 gw_html_escape(tag_commit)) == -1) {
1935 error = got_error_from_errno("asprintf");
1943 got_object_tag_close(tag);
1945 error = buf_puts(&newsize, diffbuf, tag_row);
1953 if (error || (limit && --limit == 0))
1957 if (buf_len(diffbuf) > 0) {
1958 error = buf_putc(diffbuf, '\0');
1959 tags = strdup(buf_get(diffbuf));
1963 got_ref_list_free(&refs);
1965 got_repo_close(repo);
1973 gw_free_headers(struct gw_header *header)
1977 if (header->commit != NULL)
1978 got_object_commit_close(header->commit);
1980 got_repo_close(header->repo);
1981 free(header->refs_str);
1982 free(header->commit_id);
1983 free(header->parent_id);
1984 free(header->tree_id);
1985 free(header->author);
1986 free(header->committer);
1987 free(header->commit_msg);
1990 static struct gw_header *
1993 struct gw_header *header;
1995 header = malloc(sizeof(*header));
1999 header->repo = NULL;
2000 header->commit = NULL;
2002 header->path = NULL;
2003 SIMPLEQ_INIT(&header->refs);
2008 static const struct got_error *
2009 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2012 const struct got_error *error = NULL;
2013 struct got_commit_graph *graph = NULL;
2015 error = got_commit_graph_open(&graph, header->path, 0);
2019 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2025 error = got_commit_graph_iter_next(&header->id, graph,
2026 header->repo, NULL, NULL);
2028 if (error->code == GOT_ERR_ITER_COMPLETED)
2032 if (header->id == NULL)
2035 error = got_object_open_as_commit(&header->commit, header->repo,
2040 error = gw_get_commit(gw_trans, header);
2042 struct gw_header *n_header = NULL;
2043 if ((n_header = gw_init_header()) == NULL) {
2044 error = got_error_from_errno("malloc");
2048 n_header->refs_str = strdup(header->refs_str);
2049 n_header->commit_id = strdup(header->commit_id);
2050 n_header->parent_id = strdup(header->parent_id);
2051 n_header->tree_id = strdup(header->tree_id);
2052 n_header->author = strdup(header->author);
2053 n_header->committer = strdup(header->committer);
2054 n_header->commit_msg = strdup(header->commit_msg);
2055 n_header->committer_time = header->committer_time;
2056 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2059 if (error || (limit && --limit == 0))
2064 got_commit_graph_close(graph);
2068 static const struct got_error *
2069 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2071 const struct got_error *error = NULL;
2072 struct got_reflist_entry *re;
2073 struct got_object_id *id2 = NULL;
2074 struct got_object_qid *parent_id;
2075 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2078 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2081 struct got_tag_object *tag = NULL;
2084 name = got_ref_get_name(re->ref);
2085 if (strcmp(name, GOT_REF_HEAD) == 0)
2087 if (strncmp(name, "refs/", 5) == 0)
2089 if (strncmp(name, "got/", 4) == 0)
2091 if (strncmp(name, "heads/", 6) == 0)
2093 if (strncmp(name, "remotes/", 8) == 0)
2095 if (strncmp(name, "tags/", 5) == 0) {
2096 error = got_object_open_as_tag(&tag, header->repo,
2099 if (error->code != GOT_ERR_OBJ_TYPE)
2102 * Ref points at something other
2109 cmp = got_object_id_cmp(tag ?
2110 got_object_tag_get_object_id(tag) : re->id, header->id);
2112 got_object_tag_close(tag);
2116 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2117 s ? ", " : "", name) == -1) {
2118 error = got_error_from_errno("asprintf");
2122 header->refs_str = strdup(refs_str);
2126 if (refs_str == NULL)
2127 header->refs_str = strdup("");
2130 error = got_object_id_str(&header->commit_id, header->id);
2134 error = got_object_id_str(&header->tree_id,
2135 got_object_commit_get_tree_id(header->commit));
2139 if (gw_trans->action == GW_DIFF) {
2140 parent_id = SIMPLEQ_FIRST(
2141 got_object_commit_get_parent_ids(header->commit));
2142 if (parent_id != NULL) {
2143 id2 = got_object_id_dup(parent_id->id);
2145 error = got_object_id_str(&header->parent_id, id2);
2150 header->parent_id = strdup("/dev/null");
2152 header->parent_id = strdup("");
2154 header->committer_time =
2155 got_object_commit_get_committer_time(header->commit);
2157 if (gw_trans->action != GW_BRIEFS && gw_trans->action != GW_SUMMARY) {
2158 header->author = strdup(
2159 gw_html_escape(got_object_commit_get_author(header->commit))
2162 header->author = strdup(
2163 got_object_commit_get_author(header->commit)
2167 header->committer = strdup(
2168 gw_html_escape(got_object_commit_get_committer(header->commit))
2171 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2175 commit_msg = commit_msg0;
2176 while (*commit_msg == '\n')
2179 header->commit_msg = strdup(commit_msg);
2184 static const struct got_error *
2185 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2187 const struct got_error *error = NULL;
2188 char *in_repo_path = NULL;
2190 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2194 if (gw_trans->commit == NULL) {
2195 struct got_reference *head_ref;
2196 error = got_ref_open(&head_ref, header->repo,
2197 gw_trans->headref, 0);
2201 error = got_ref_resolve(&header->id, header->repo, head_ref);
2202 got_ref_close(head_ref);
2206 error = got_object_open_as_commit(&header->commit,
2207 header->repo, header->id);
2209 struct got_reference *ref;
2210 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2211 if (error == NULL) {
2213 error = got_ref_resolve(&header->id, header->repo, ref);
2217 error = got_object_get_type(&obj_type, header->repo,
2221 if (obj_type == GOT_OBJ_TYPE_TAG) {
2222 struct got_tag_object *tag;
2223 error = got_object_open_as_tag(&tag,
2224 header->repo, header->id);
2227 if (got_object_tag_get_object_type(tag) !=
2228 GOT_OBJ_TYPE_COMMIT) {
2229 got_object_tag_close(tag);
2230 error = got_error(GOT_ERR_OBJ_TYPE);
2234 header->id = got_object_id_dup(
2235 got_object_tag_get_object_id(tag));
2236 if (header->id == NULL)
2237 error = got_error_from_errno(
2238 "got_object_id_dup");
2239 got_object_tag_close(tag);
2242 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2243 error = got_error(GOT_ERR_OBJ_TYPE);
2246 error = got_object_open_as_commit(&header->commit,
2247 header->repo, header->id);
2251 if (header->commit == NULL) {
2252 error = got_repo_match_object_id_prefix(&header->id,
2253 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2258 error = got_repo_match_object_id_prefix(&header->id,
2259 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2263 error = got_repo_map_path(&in_repo_path, header->repo,
2264 gw_trans->repo_path, 1);
2269 header->path = strdup(in_repo_path);
2273 error = got_ref_list(&header->refs, header->repo, NULL,
2274 got_ref_cmp_by_name, NULL);
2278 error = gw_get_commits(gw_trans, header, limit);
2286 char datebuf[11]; /* YYYY-MM-DD + NUL */
2289 struct gw_blame_cb_args {
2290 struct blame_line *lines;
2294 off_t *line_offsets;
2296 struct got_repository *repo;
2297 struct gw_trans *gw_trans;
2298 struct buf *blamebuf;
2301 static const struct got_error *
2302 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2304 const struct got_error *err = NULL;
2305 struct gw_blame_cb_args *a = arg;
2306 struct blame_line *bline;
2308 size_t linesize = 0, newsize;
2309 struct got_commit_object *commit = NULL;
2312 time_t committer_time;
2314 if (nlines != a->nlines ||
2315 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2316 return got_error(GOT_ERR_RANGE);
2319 return NULL; /* no change in this commit */
2321 /* Annotate this line. */
2322 bline = &a->lines[lineno - 1];
2323 if (bline->annotated)
2325 err = got_object_id_str(&bline->id_str, id);
2329 err = got_object_open_as_commit(&commit, a->repo, id);
2333 bline->committer = strdup(got_object_commit_get_committer(commit));
2334 if (bline->committer == NULL) {
2335 err = got_error_from_errno("strdup");
2339 committer_time = got_object_commit_get_committer_time(commit);
2340 if (localtime_r(&committer_time, &tm) == NULL)
2341 return got_error_from_errno("localtime_r");
2342 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2343 &tm) >= sizeof(bline->datebuf)) {
2344 err = got_error(GOT_ERR_NO_SPACE);
2347 bline->annotated = 1;
2349 /* Print lines annotated so far. */
2350 bline = &a->lines[a->lineno_cur - 1];
2351 if (!bline->annotated)
2354 offset = a->line_offsets[a->lineno_cur - 1];
2355 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2356 err = got_error_from_errno("fseeko");
2360 while (bline->annotated) {
2361 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2362 *line_escape = NULL;
2365 if (getline(&line, &linesize, a->f) == -1) {
2367 err = got_error_from_errno("getline");
2371 committer = bline->committer;
2372 smallerthan = strchr(committer, '<');
2373 if (smallerthan && smallerthan[1] != '\0')
2374 committer = smallerthan + 1;
2375 at = strchr(committer, '@');
2378 len = strlen(committer);
2380 committer[8] = '\0';
2382 nl = strchr(line, '\n');
2386 if (strcmp(line, "") != 0)
2387 line_escape = strdup(gw_html_escape(line));
2389 line_escape = strdup("");
2391 if (a->gw_trans->repo_folder == NULL)
2392 a->gw_trans->repo_folder = strdup("");
2393 if (a->gw_trans->repo_folder == NULL)
2395 asprintf(&blame_row, blame_line, a->nlines_prec, a->lineno_cur,
2396 a->gw_trans->repo_name, bline->id_str,
2397 a->gw_trans->repo_file, a->gw_trans->repo_folder,
2398 bline->id_str, bline->datebuf, committer, line_escape);
2400 err = buf_puts(&newsize, a->blamebuf, blame_row);
2404 bline = &a->lines[a->lineno_cur - 1];
2411 got_object_commit_close(commit);
2417 isbinary(const char *buf, size_t n)
2419 return (memchr(buf, '\0', n) != NULL);
2423 gw_get_file_blame_blob(struct gw_trans *gw_trans)
2425 const struct got_error *error = NULL;
2426 struct got_repository *repo = NULL;
2427 struct got_object_id *obj_id = NULL;
2428 struct got_object_id *commit_id = NULL;
2429 struct got_blob_object *blob = NULL;
2430 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL;
2431 char *folder = NULL;
2432 struct gw_blame_cb_args bca;
2437 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2441 if (gw_trans->repo_folder != NULL) {
2442 if (asprintf(&folder, "%s/", gw_trans->repo_folder) == -1) {
2443 error = got_error_from_errno("asprintf");
2447 folder = strdup("");
2449 if (asprintf(&path, "%s%s", folder, gw_trans->repo_file) == -1) {
2450 error = got_error_from_errno("asprintf");
2455 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2459 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2460 GOT_OBJ_TYPE_COMMIT, 1, repo);
2464 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2468 if (obj_id == NULL) {
2469 error = got_error(GOT_ERR_NO_OBJ);
2473 error = got_object_get_type(&obj_type, repo, obj_id);
2477 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2478 error = got_error(GOT_ERR_OBJ_TYPE);
2482 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2486 error = buf_alloc(&bca.blamebuf, 0);
2490 bca.f = got_opentemp();
2491 if (bca.f == NULL) {
2492 error = got_error_from_errno("got_opentemp");
2495 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2496 &bca.line_offsets, bca.f, blob);
2497 if (error || bca.nlines == 0)
2500 if (gw_trans->action == GW_BLOB) {
2504 if (fseek(bca.f, 0, SEEK_END) == -1)
2506 len = ftell(bca.f) + 1;
2509 if (fseek(bca.f, 0, SEEK_SET) == -1)
2512 if ((blame_html = calloc(len, sizeof(char *))) == NULL)
2515 n = fread(blame_html, 1, len, bca.f);
2519 error = got_ferror(bca.f, GOT_ERR_IO);
2523 if (isbinary(blame_html, n))
2524 gw_trans->mime = KMIME_APP_OCTET_STREAM;
2526 gw_trans->mime = KMIME_TEXT_PLAIN;
2528 error = gw_display_index(gw_trans);
2530 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2531 kerr = khttp_write(gw_trans->gw_req, blame_html, len);
2532 if (kerr != KCGI_OK)
2533 error = gw_kcgi_error(kerr);
2538 /* Don't include \n at EOF in the blame line count. */
2539 if (bca.line_offsets[bca.nlines - 1] == filesize)
2542 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2543 if (bca.lines == NULL) {
2544 error = got_error_from_errno("calloc");
2548 bca.nlines_prec = 0;
2555 bca.gw_trans = gw_trans;
2557 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2561 if (buf_len(bca.blamebuf) > 0) {
2562 error = buf_putc(bca.blamebuf, '\0');
2563 blame_html = strdup(buf_get(bca.blamebuf));
2566 free(bca.line_offsets);
2573 if (gw_trans->action != GW_BLOB && bca.lines) {
2574 for (i = 0; i < bca.nlines; i++) {
2575 struct blame_line *bline = &bca.lines[i];
2576 free(bline->id_str);
2577 free(bline->committer);
2583 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2586 error = got_object_blob_close(blob);
2590 error = got_repo_close(repo);
2598 gw_get_repo_tree(struct gw_trans *gw_trans)
2600 const struct got_error *error = NULL;
2601 struct got_repository *repo = NULL;
2602 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2603 struct got_tree_object *tree = NULL;
2604 struct buf *diffbuf = NULL;
2606 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2607 *tree_row = NULL, *id_str, *class = NULL;
2608 int nentries, i, class_flip = 0;
2610 error = buf_alloc(&diffbuf, 0);
2614 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2618 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2622 if (gw_trans->repo_folder != NULL)
2623 path = strdup(gw_trans->repo_folder);
2624 else if (in_repo_path) {
2626 path = in_repo_path;
2629 if (gw_trans->commit == NULL) {
2630 struct got_reference *head_ref;
2631 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2635 error = got_ref_resolve(&commit_id, repo, head_ref);
2636 got_ref_close(head_ref);
2639 error = got_repo_match_object_id(&commit_id, NULL,
2640 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2644 error = got_object_id_str(&gw_trans->commit, commit_id);
2648 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2652 error = got_object_open_as_tree(&tree, repo, tree_id);
2656 nentries = got_object_tree_get_nentries(tree);
2658 for (i = 0; i < nentries; i++) {
2659 struct got_tree_entry *te;
2660 const char *modestr = "";
2661 char *id = NULL, *url_html = NULL;
2663 te = got_object_tree_get_entry(tree, i);
2665 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2669 if (asprintf(&id, "%s", id_str) == -1) {
2670 error = got_error_from_errno("asprintf");
2675 mode_t mode = got_tree_entry_get_mode(te);
2677 if (got_object_tree_entry_is_submodule(te))
2679 else if (S_ISLNK(mode))
2681 else if (S_ISDIR(mode))
2683 else if (mode & S_IXUSR)
2686 if (class_flip == 0) {
2687 class = strdup("back_lightgray");
2690 class = strdup("back_white");
2694 char *build_folder = NULL;
2695 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2696 if (gw_trans->repo_folder != NULL) {
2697 if (asprintf(&build_folder, "%s/%s",
2698 gw_trans->repo_folder,
2699 got_tree_entry_get_name(te)) == -1) {
2701 got_error_from_errno("asprintf");
2705 if (asprintf(&build_folder, "/%s",
2706 got_tree_entry_get_name(te)) == -1)
2710 if (asprintf(&url_html, folder_html,
2711 gw_trans->repo_name, gw_trans->action_name,
2712 gw_trans->commit, build_folder,
2713 got_tree_entry_get_name(te), modestr) == -1) {
2714 error = got_error_from_errno("asprintf");
2718 if (asprintf(&tree_row, tree_line, class, url_html,
2720 error = got_error_from_errno("asprintf");
2725 if (gw_trans->repo_folder != NULL) {
2726 if (asprintf(&build_folder, "%s",
2727 gw_trans->repo_folder) == -1) {
2729 got_error_from_errno("asprintf");
2733 build_folder = strdup("");
2735 if (asprintf(&url_html, file_html, gw_trans->repo_name,
2736 "blob", gw_trans->commit,
2737 got_tree_entry_get_name(te), build_folder,
2738 got_tree_entry_get_name(te), modestr) == -1) {
2739 error = got_error_from_errno("asprintf");
2743 if (asprintf(&tree_row, tree_line_with_navs, class,
2744 url_html, class, gw_trans->repo_name, "blob",
2745 gw_trans->commit, got_tree_entry_get_name(te),
2746 build_folder, "blob", gw_trans->repo_name,
2747 "blame", gw_trans->commit,
2748 got_tree_entry_get_name(te), build_folder,
2750 error = got_error_from_errno("asprintf");
2759 error = buf_puts(&newsize, diffbuf, tree_row);
2769 if (buf_len(diffbuf) > 0) {
2770 error = buf_putc(diffbuf, '\0');
2771 tree_html = strdup(buf_get(diffbuf));
2775 got_object_tree_close(tree);
2777 got_repo_close(repo);
2789 gw_get_repo_heads(struct gw_trans *gw_trans)
2791 const struct got_error *error = NULL;
2792 struct got_repository *repo = NULL;
2793 struct got_reflist_head refs;
2794 struct got_reflist_entry *re;
2795 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2796 struct buf *diffbuf = NULL;
2799 SIMPLEQ_INIT(&refs);
2801 error = buf_alloc(&diffbuf, 0);
2805 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2809 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2814 SIMPLEQ_FOREACH(re, &refs, entry) {
2817 refname = strdup(got_ref_get_name(re->ref));
2818 if (refname == NULL) {
2819 error = got_error_from_errno("got_ref_to_str");
2823 if (strncmp(refname, "refs/heads/", 11) != 0) {
2828 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
2833 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2834 refname, gw_trans->repo_name, refname,
2835 gw_trans->repo_name, refname, gw_trans->repo_name,
2837 error = got_error_from_errno("asprintf");
2841 if (strncmp(refname, "refs/heads/", 11) == 0)
2844 if (asprintf(&head_row, heads_row, age, refname,
2845 head_navs_disp) == -1) {
2846 error = got_error_from_errno("asprintf");
2850 error = buf_puts(&newsize, diffbuf, head_row);
2852 free(head_navs_disp);
2856 if (buf_len(diffbuf) > 0) {
2857 error = buf_putc(diffbuf, '\0');
2858 heads = strdup(buf_get(diffbuf));
2862 got_ref_list_free(&refs);
2864 got_repo_close(repo);
2872 gw_get_got_link(struct gw_trans *gw_trans)
2876 if (asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2877 gw_trans->gw_conf->got_logo) == -1)
2884 gw_get_site_link(struct gw_trans *gw_trans)
2886 char *link, *repo = "", *action = "";
2888 if (gw_trans->repo_name != NULL)
2889 if (asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2890 "</a>", gw_trans->repo_name, gw_trans->repo_name) == -1)
2893 if (gw_trans->action_name != NULL)
2894 if (asprintf(&action, " / %s", gw_trans->action_name) == -1)
2897 if (asprintf(&link, site_link, GOTWEB,
2898 gw_trans->gw_conf->got_site_link, repo, action) == -1)
2905 gw_colordiff_line(char *buf)
2907 const struct got_error *error = NULL;
2908 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2909 struct buf *diffbuf = NULL;
2912 error = buf_alloc(&diffbuf, 0);
2918 if (strncmp(buf, "-", 1) == 0)
2919 color = "diff_minus";
2920 if (strncmp(buf, "+", 1) == 0)
2921 color = "diff_plus";
2922 if (strncmp(buf, "@@", 2) == 0)
2923 color = "diff_chunk_header";
2924 if (strncmp(buf, "@@", 2) == 0)
2925 color = "diff_chunk_header";
2926 if (strncmp(buf, "commit +", 8) == 0)
2927 color = "diff_meta";
2928 if (strncmp(buf, "commit -", 8) == 0)
2929 color = "diff_meta";
2930 if (strncmp(buf, "blob +", 6) == 0)
2931 color = "diff_meta";
2932 if (strncmp(buf, "blob -", 6) == 0)
2933 color = "diff_meta";
2934 if (strncmp(buf, "file +", 6) == 0)
2935 color = "diff_meta";
2936 if (strncmp(buf, "file -", 6) == 0)
2937 color = "diff_meta";
2938 if (strncmp(buf, "from:", 5) == 0)
2939 color = "diff_author";
2940 if (strncmp(buf, "via:", 4) == 0)
2941 color = "diff_author";
2942 if (strncmp(buf, "date:", 5) == 0)
2943 color = "diff_date";
2945 if (asprintf(&div_diff_line_div, div_diff_line, color) == -1)
2948 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2952 error = buf_puts(&newsize, diffbuf, buf);
2956 if (buf_len(diffbuf) > 0) {
2957 error = buf_putc(diffbuf, '\0');
2958 colorized_line = strdup(buf_get(diffbuf));
2962 free(div_diff_line_div);
2963 return colorized_line;
2967 gw_html_escape(const char *html)
2969 char *escaped_str = NULL, *buf;
2971 size_t sz, i, buff_sz = 2048;
2973 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2979 if ((sz = strlen(html)) == 0)
2982 /* only work with buff_sz */
2986 for (i = 0; i < sz; i++) {
2990 strcat(buf, ">");
2993 strcat(buf, "&");
2996 strcat(buf, "<");
2999 strcat(buf, """);
3002 strcat(buf, "'");
3005 strcat(buf, "<br />");
3011 asprintf(&escaped_str, "%s", buf);
3017 main(int argc, char *argv[])
3019 const struct got_error *error = NULL;
3020 struct gw_trans *gw_trans;
3021 struct gw_dir *dir = NULL, *tdir;
3022 const char *page = "index";
3026 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3029 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3032 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3035 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3038 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3039 if (kerr != KCGI_OK) {
3040 error = gw_kcgi_error(kerr);
3044 if ((gw_trans->gw_conf =
3045 malloc(sizeof(struct gotweb_conf))) == NULL) {
3047 error = got_error_from_errno("malloc");
3051 TAILQ_INIT(&gw_trans->gw_dirs);
3052 TAILQ_INIT(&gw_trans->gw_headers);
3055 gw_trans->repos_total = 0;
3056 gw_trans->repo_path = NULL;
3057 gw_trans->commit = NULL;
3058 gw_trans->headref = strdup(GOT_REF_HEAD);
3059 gw_trans->mime = KMIME_TEXT_HTML;
3060 gw_trans->gw_tmpl->key = gw_templs;
3061 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3062 gw_trans->gw_tmpl->arg = gw_trans;
3063 gw_trans->gw_tmpl->cb = gw_template;
3064 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3068 error = gw_parse_querystring(gw_trans);
3072 if (gw_trans->action == GW_BLOB)
3073 error = gw_blob(gw_trans);
3075 error = gw_display_index(gw_trans);
3078 gw_trans->mime = KMIME_TEXT_PLAIN;
3079 gw_trans->action = GW_ERR;
3080 gw_display_error(gw_trans, error);
3083 free(gw_trans->gw_conf->got_repos_path);
3084 free(gw_trans->gw_conf->got_site_name);
3085 free(gw_trans->gw_conf->got_site_owner);
3086 free(gw_trans->gw_conf->got_site_link);
3087 free(gw_trans->gw_conf->got_logo);
3088 free(gw_trans->gw_conf->got_logo_url);
3089 free(gw_trans->gw_conf);
3090 free(gw_trans->commit);
3091 free(gw_trans->repo_path);
3092 free(gw_trans->repo_name);
3093 free(gw_trans->repo_file);
3094 free(gw_trans->action_name);
3095 free(gw_trans->headref);
3097 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3099 free(dir->description);
3108 khttp_free(gw_trans->gw_req);