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 const struct got_error *gw_get_time_str(char **, time_t, int);
166 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
167 char *, char *, int);
168 static const struct got_error *gw_get_file_blame_blob(char **, struct gw_trans *);
169 static const struct got_error *gw_get_file_read_blob(char **, size_t *,
171 static const struct got_error *gw_get_repo_tree(char **, struct gw_trans *);
172 static const struct got_error *gw_get_diff(char **, struct gw_trans *,
174 static const struct got_error *gw_get_repo_tags(char **, struct gw_trans *,
175 struct gw_header *, int, int);
176 static char *gw_get_repo_heads(struct gw_trans *);
177 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *, char *);
178 static char *gw_get_got_link(struct gw_trans *);
179 static char *gw_get_site_link(struct gw_trans *);
180 static const struct got_error *gw_html_escape(char **, const char *);
181 static const struct got_error *gw_colordiff_line(char **, char *);
183 static char *gw_gen_commit_header(char *, char*);
184 static char *gw_gen_diff_header(char *, char*);
185 static char *gw_gen_author_header(char *);
186 static char *gw_gen_committer_header(char *);
187 static char *gw_gen_commit_msg_header(char *);
188 static char *gw_gen_tree_header(char *);
190 static void gw_free_headers(struct gw_header *);
191 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
193 static const struct got_error* gw_display_index(struct gw_trans *);
194 static void gw_display_error(struct gw_trans *,
195 const struct got_error *);
197 static int gw_template(size_t, void *);
199 static const struct got_error* gw_get_header(struct gw_trans *,
200 struct gw_header *, int);
201 static const struct got_error* gw_get_commits(struct gw_trans *,
202 struct gw_header *, int);
203 static const struct got_error* gw_get_commit(struct gw_trans *,
205 static const struct got_error* gw_apply_unveil(const char *, const char *);
206 static const struct got_error* gw_blame_cb(void *, int, int,
207 struct got_object_id *);
208 static const struct got_error* gw_load_got_paths(struct gw_trans *);
209 static const struct got_error* gw_load_got_path(struct gw_trans *,
211 static const struct got_error* gw_parse_querystring(struct gw_trans *);
213 static const struct got_error* gw_blame(struct gw_trans *);
214 static const struct got_error* gw_blob(struct gw_trans *);
215 static const struct got_error* gw_diff(struct gw_trans *);
216 static const struct got_error* gw_index(struct gw_trans *);
217 static const struct got_error* gw_commits(struct gw_trans *);
218 static const struct got_error* gw_briefs(struct gw_trans *);
219 static const struct got_error* gw_summary(struct gw_trans *);
220 static const struct got_error* gw_tree(struct gw_trans *);
221 static const struct got_error* gw_tag(struct gw_trans *);
223 struct gw_query_action {
224 unsigned int func_id;
225 const char *func_name;
226 const struct got_error *(*func_main)(struct gw_trans *);
230 enum gw_query_actions {
243 static struct gw_query_action gw_query_funcs[] = {
244 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
245 { GW_BLOB, "blob", NULL, NULL },
246 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
247 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
248 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
249 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
250 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
251 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
252 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
253 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
256 static const struct got_error *
257 gw_kcgi_error(enum kcgi_err kerr)
262 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
263 return got_error(GOT_ERR_CANCELLED);
265 if (kerr == KCGI_ENOMEM)
266 return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
268 if (kerr == KCGI_ENFILE)
269 return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
271 if (kerr == KCGI_EAGAIN)
272 return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
274 if (kerr == KCGI_FORM)
275 return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
277 return got_error_from_errno(kcgi_strerror(kerr));
280 static const struct got_error *
281 gw_apply_unveil(const char *repo_path, const char *repo_file)
283 const struct got_error *err;
285 if (repo_path && repo_file) {
287 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
288 return got_error_from_errno("asprintf unveil");
289 if (unveil(full_path, "r") != 0)
290 return got_error_from_errno2("unveil", full_path);
293 if (repo_path && unveil(repo_path, "r") != 0)
294 return got_error_from_errno2("unveil", repo_path);
296 if (unveil("/tmp", "rwc") != 0)
297 return got_error_from_errno2("unveil", "/tmp");
299 err = got_privsep_unveil_exec_helpers();
303 if (unveil(NULL, NULL) != 0)
304 return got_error_from_errno("unveil");
309 static const struct got_error *
310 gw_empty_string(char **s)
314 return got_error_from_errno("strdup");
319 isbinary(const char *buf, size_t n)
321 return (memchr(buf, '\0', n) != NULL);
325 static const struct got_error *
326 gw_blame(struct gw_trans *gw_trans)
328 const struct got_error *error = NULL;
329 struct gw_header *header = NULL;
330 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
331 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
334 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
336 return got_error_from_errno("pledge");
338 if ((header = gw_init_header()) == NULL)
339 return got_error_from_errno("malloc");
341 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
345 error = gw_get_header(gw_trans, header, 1);
349 error = gw_get_file_blame_blob(&blame_html, gw_trans);
353 error = gw_get_time_str(&age, header->committer_time, TM_LONG);
356 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
357 error = got_error_from_errno("asprintf");
361 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
364 if (asprintf(&blame_html_disp, blame_header, age_html,
365 gw_gen_commit_msg_header(escaped_commit_msg), blame_html) == -1) {
366 error = got_error_from_errno("asprintf");
370 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
371 error = got_error_from_errno("asprintf");
375 kerr = khttp_puts(gw_trans->gw_req, blame);
377 error = gw_kcgi_error(kerr);
379 got_ref_list_free(&header->refs);
380 gw_free_headers(header);
381 free(blame_html_disp);
384 free(escaped_commit_msg);
388 static const struct got_error *
389 gw_blob(struct gw_trans *gw_trans)
391 const struct got_error *error = NULL;
392 struct gw_header *header = NULL;
393 char *content = NULL;
397 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
399 return got_error_from_errno("pledge");
401 if ((header = gw_init_header()) == NULL)
402 return got_error_from_errno("malloc");
404 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
408 error = gw_get_header(gw_trans, header, 1);
412 error = gw_get_file_read_blob(&content, &filesize, gw_trans);
416 if (isbinary(content, filesize))
417 gw_trans->mime = KMIME_APP_OCTET_STREAM;
419 gw_trans->mime = KMIME_TEXT_PLAIN;
421 error = gw_display_index(gw_trans);
425 kerr = khttp_write(gw_trans->gw_req, content, filesize);
427 error = gw_kcgi_error(kerr);
429 got_ref_list_free(&header->refs);
430 gw_free_headers(header);
435 static const struct got_error *
436 gw_diff(struct gw_trans *gw_trans)
438 const struct got_error *error = NULL;
439 struct gw_header *header = NULL;
440 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
441 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
444 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
446 return got_error_from_errno("pledge");
448 if ((header = gw_init_header()) == NULL)
449 return got_error_from_errno("malloc");
451 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
455 error = gw_get_header(gw_trans, header, 1);
459 error = gw_get_diff(&diff_html, gw_trans, header);
463 error = gw_get_time_str(&age, header->committer_time, TM_LONG);
466 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
467 error = got_error_from_errno("asprintf");
470 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
473 if (asprintf(&diff_html_disp, diff_header,
474 gw_gen_diff_header(header->parent_id, header->commit_id),
475 gw_gen_commit_header(header->commit_id, header->refs_str),
476 gw_gen_tree_header(header->tree_id),
477 gw_gen_author_header(header->author),
478 gw_gen_committer_header(header->committer), age_html,
479 gw_gen_commit_msg_header(escaped_commit_msg),
480 diff_html ? diff_html : "") == -1) {
481 error = got_error_from_errno("asprintf");
485 if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
486 error = got_error_from_errno("asprintf");
490 kerr = khttp_puts(gw_trans->gw_req, diff);
492 error = gw_kcgi_error(kerr);
494 got_ref_list_free(&header->refs);
495 gw_free_headers(header);
496 free(diff_html_disp);
501 free(escaped_commit_msg);
505 static const struct got_error *
506 gw_index(struct gw_trans *gw_trans)
508 const struct got_error *error = NULL;
509 struct gw_dir *gw_dir = NULL;
510 char *html, *navs, *next, *prev;
511 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
514 if (pledge("stdio rpath proc exec sendfd unveil",
516 error = got_error_from_errno("pledge");
520 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
524 error = gw_load_got_paths(gw_trans);
528 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
530 return gw_kcgi_error(kerr);
532 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
533 if (asprintf(&html, index_projects_empty,
534 gw_trans->gw_conf->got_repos_path) == -1)
535 return got_error_from_errno("asprintf");
536 kerr = khttp_puts(gw_trans->gw_req, html);
538 error = gw_kcgi_error(kerr);
543 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
546 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
547 if (gw_trans->page > 0 && (gw_trans->page *
548 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
557 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
558 gw_dir->name, gw_dir->name) == -1)
559 return got_error_from_errno("asprintf");
561 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
562 gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
565 return got_error_from_errno("asprintf");
567 kerr = khttp_puts(gw_trans->gw_req, html);
571 return gw_kcgi_error(kerr);
573 if (gw_trans->gw_conf->got_max_repos_display == 0)
576 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
577 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
579 return gw_kcgi_error(kerr);
580 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
581 (gw_trans->page > 0) &&
582 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
583 prev_disp == gw_trans->repos_total)) {
584 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
586 return gw_kcgi_error(kerr);
589 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
590 (gw_trans->page > 0) &&
591 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
592 prev_disp == gw_trans->repos_total)) {
593 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
594 return got_error_from_errno("asprintf");
595 kerr = khttp_puts(gw_trans->gw_req, prev);
598 return gw_kcgi_error(kerr);
601 kerr = khttp_puts(gw_trans->gw_req, div_end);
603 return gw_kcgi_error(kerr);
605 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
606 next_disp == gw_trans->gw_conf->got_max_repos_display &&
607 dir_c != (gw_trans->page + 1) *
608 gw_trans->gw_conf->got_max_repos_display) {
609 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
610 return got_error_from_errno("calloc");
611 kerr = khttp_puts(gw_trans->gw_req, next);
614 return gw_kcgi_error(kerr);
615 kerr = khttp_puts(gw_trans->gw_req, div_end);
617 error = gw_kcgi_error(kerr);
622 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
623 (gw_trans->page > 0) &&
624 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
625 prev_disp == gw_trans->repos_total)) {
626 kerr = khttp_puts(gw_trans->gw_req, div_end);
628 return gw_kcgi_error(kerr);
636 static const struct got_error *
637 gw_commits(struct gw_trans *gw_trans)
639 const struct got_error *error = NULL;
640 char *commits_html, *commits_navs_html;
641 struct gw_header *header = NULL, *n_header = NULL;
642 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
645 if ((header = gw_init_header()) == NULL)
646 return got_error_from_errno("malloc");
648 if (pledge("stdio rpath proc exec sendfd unveil",
650 error = got_error_from_errno("pledge");
654 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
658 error = gw_get_header(gw_trans, header,
659 gw_trans->gw_conf->got_max_commits_display);
663 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
664 if (kerr != KCGI_OK) {
665 error = gw_kcgi_error(kerr);
668 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
669 if (asprintf(&commits_navs_html, commits_navs,
670 gw_trans->repo_name, n_header->commit_id,
671 gw_trans->repo_name, n_header->commit_id,
672 gw_trans->repo_name, n_header->commit_id) == -1) {
673 error = got_error_from_errno("asprintf");
676 error = gw_get_time_str(&age, n_header->committer_time,
680 if (asprintf(&age_html, header_age_html, age ? age : "")
682 error = got_error_from_errno("asprintf");
685 error = gw_html_escape(&escaped_commit_msg,
686 n_header->commit_msg);
689 if (asprintf(&commits_html, commits_line,
690 gw_gen_commit_header(n_header->commit_id,
692 gw_gen_author_header(n_header->author),
693 gw_gen_committer_header(n_header->committer),
694 age_html, escaped_commit_msg,
695 commits_navs_html) == -1) {
696 error = got_error_from_errno("asprintf");
703 free(escaped_commit_msg);
704 escaped_commit_msg = NULL;
705 kerr = khttp_puts(gw_trans->gw_req, commits_html);
706 if (kerr != KCGI_OK) {
707 error = gw_kcgi_error(kerr);
711 kerr = khttp_puts(gw_trans->gw_req, div_end);
713 error = gw_kcgi_error(kerr);
715 got_ref_list_free(&header->refs);
716 gw_free_headers(header);
717 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
718 gw_free_headers(n_header);
721 free(escaped_commit_msg);
725 static const struct got_error *
726 gw_briefs(struct gw_trans *gw_trans)
728 const struct got_error *error = NULL;
729 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
730 struct gw_header *header = NULL, *n_header = NULL;
731 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
734 if ((header = gw_init_header()) == NULL)
735 return got_error_from_errno("malloc");
737 if (pledge("stdio rpath proc exec sendfd unveil",
739 error = got_error_from_errno("pledge");
743 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
747 if (gw_trans->action == GW_SUMMARY)
748 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
750 error = gw_get_header(gw_trans, header,
751 gw_trans->gw_conf->got_max_commits_display);
755 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
756 if (kerr != KCGI_OK) {
757 error = gw_kcgi_error(kerr);
761 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
762 if (asprintf(&briefs_navs_html, briefs_navs,
763 gw_trans->repo_name, n_header->commit_id,
764 gw_trans->repo_name, n_header->commit_id,
765 gw_trans->repo_name, n_header->commit_id) == -1) {
766 error = got_error_from_errno("asprintf");
769 newline = strchr(n_header->commit_msg, '\n');
772 error = gw_get_time_str(&age, n_header->committer_time,
776 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
777 error = got_error_from_errno("asprintf");
780 error = gw_html_escape(&escaped_commit_msg,
781 n_header->commit_msg);
784 if (asprintf(&briefs_html, briefs_line, age_html,
785 n_header->author, escaped_commit_msg,
786 briefs_navs_html) == -1) {
787 error = got_error_from_errno("asprintf");
794 free(escaped_commit_msg);
795 escaped_commit_msg = NULL;
796 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
797 if (kerr != KCGI_OK) {
798 error = gw_kcgi_error(kerr);
802 kerr = khttp_puts(gw_trans->gw_req, div_end);
804 error = gw_kcgi_error(kerr);
806 got_ref_list_free(&header->refs);
807 gw_free_headers(header);
808 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
809 gw_free_headers(n_header);
812 free(escaped_commit_msg);
816 static const struct got_error *
817 gw_summary(struct gw_trans *gw_trans)
819 const struct got_error *error = NULL;
820 char *description_html = NULL, *repo_owner_html = NULL;
821 char *age = NULL, *repo_age_html = NULL, *cloneurl_html = NULL;
822 char *tags = NULL, *tags_html = NULL;
823 char *heads = NULL, *heads_html = NULL;
826 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
827 return got_error_from_errno("pledge");
829 /* unveil is applied with gw_briefs below */
831 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
833 return gw_kcgi_error(kerr);
835 if (gw_trans->gw_conf->got_show_repo_description) {
836 if (gw_trans->gw_dir->description != NULL &&
837 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
838 if (asprintf(&description_html, description,
839 gw_trans->gw_dir->description) == -1) {
840 error = got_error_from_errno("asprintf");
844 kerr = khttp_puts(gw_trans->gw_req, description_html);
845 if (kerr != KCGI_OK) {
846 error = gw_kcgi_error(kerr);
852 if (gw_trans->gw_conf->got_show_repo_owner &&
853 gw_trans->gw_dir->owner != NULL) {
854 if (asprintf(&repo_owner_html, repo_owner,
855 gw_trans->gw_dir->owner) == -1) {
856 error = got_error_from_errno("asprintf");
860 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
861 if (kerr != KCGI_OK) {
862 error = gw_kcgi_error(kerr);
867 if (gw_trans->gw_conf->got_show_repo_age) {
868 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
869 "refs/heads", TM_LONG);
873 if (asprintf(&repo_age_html, last_change, age) == -1) {
874 error = got_error_from_errno("asprintf");
878 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
879 if (kerr != KCGI_OK) {
880 error = gw_kcgi_error(kerr);
886 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
887 if (gw_trans->gw_dir->url != NULL &&
888 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
889 if (asprintf(&cloneurl_html, cloneurl,
890 gw_trans->gw_dir->url) == -1) {
891 error = got_error_from_errno("asprintf");
895 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
896 if (kerr != KCGI_OK) {
897 error = gw_kcgi_error(kerr);
902 kerr = khttp_puts(gw_trans->gw_req, div_end);
903 if (kerr != KCGI_OK) {
904 error = gw_kcgi_error(kerr);
908 error = gw_briefs(gw_trans);
912 error = gw_get_repo_tags(&tags, gw_trans, NULL, D_MAXSLCOMMDISP,
917 heads = gw_get_repo_heads(gw_trans);
919 if (tags != NULL && strcmp(tags, "") != 0) {
920 if (asprintf(&tags_html, summary_tags, tags) == -1) {
921 error = got_error_from_errno("asprintf");
924 kerr = khttp_puts(gw_trans->gw_req, tags_html);
925 if (kerr != KCGI_OK) {
926 error = gw_kcgi_error(kerr);
931 if (heads != NULL && strcmp(heads, "") != 0) {
932 if (asprintf(&heads_html, summary_heads, heads) == -1) {
933 error = got_error_from_errno("asprintf");
936 kerr = khttp_puts(gw_trans->gw_req, heads_html);
937 if (kerr != KCGI_OK) {
938 error = gw_kcgi_error(kerr);
943 free(description_html);
944 free(repo_owner_html);
955 static const struct got_error *
956 gw_tree(struct gw_trans *gw_trans)
958 const struct got_error *error = NULL;
959 struct gw_header *header = NULL;
960 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
961 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
964 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
965 return got_error_from_errno("pledge");
967 if ((header = gw_init_header()) == NULL)
968 return got_error_from_errno("malloc");
970 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
974 error = gw_get_header(gw_trans, header, 1);
978 error = gw_get_repo_tree(&tree_html, gw_trans);
982 error = gw_get_time_str(&age, header->committer_time, TM_LONG);
985 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
986 error = got_error_from_errno("asprintf");
989 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
992 if (asprintf(&tree_html_disp, tree_header, age_html,
993 gw_gen_commit_msg_header(escaped_commit_msg),
994 tree_html ? tree_html : "") == -1) {
995 error = got_error_from_errno("asprintf");
999 if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
1000 error = got_error_from_errno("asprintf");
1004 kerr = khttp_puts(gw_trans->gw_req, tree);
1005 if (kerr != KCGI_OK)
1006 error = gw_kcgi_error(kerr);
1008 got_ref_list_free(&header->refs);
1009 gw_free_headers(header);
1010 free(tree_html_disp);
1015 free(escaped_commit_msg);
1019 static const struct got_error *
1020 gw_tag(struct gw_trans *gw_trans)
1022 const struct got_error *error = NULL;
1023 struct gw_header *header = NULL;
1024 char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
1025 char *escaped_commit_msg = NULL;
1028 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1029 return got_error_from_errno("pledge");
1031 if ((header = gw_init_header()) == NULL)
1032 return got_error_from_errno("malloc");
1034 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1038 error = gw_get_header(gw_trans, header, 1);
1042 error = gw_get_repo_tags(&tag_html, gw_trans, header, 1, TAGFULL);
1046 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1049 if (asprintf(&tag_html_disp, tag_header,
1050 gw_gen_commit_header(header->commit_id, header->refs_str),
1051 gw_gen_commit_msg_header(escaped_commit_msg),
1052 tag_html ? tag_html : "") == -1) {
1053 error = got_error_from_errno("asprintf");
1057 if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
1058 error = got_error_from_errno("asprintf");
1062 kerr = khttp_puts(gw_trans->gw_req, tag);
1063 if (kerr != KCGI_OK)
1064 error = gw_kcgi_error(kerr);
1066 got_ref_list_free(&header->refs);
1067 gw_free_headers(header);
1068 free(tag_html_disp);
1071 free(escaped_commit_msg);
1075 static const struct got_error *
1076 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1078 const struct got_error *error = NULL;
1083 if (asprintf(&dir_test, "%s/%s/%s",
1084 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1085 GOTWEB_GIT_DIR) == -1)
1086 return got_error_from_errno("asprintf");
1088 dt = opendir(dir_test);
1092 gw_dir->path = strdup(dir_test);
1097 if (asprintf(&dir_test, "%s/%s/%s",
1098 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1099 GOTWEB_GOT_DIR) == -1)
1100 return got_error_from_errno("asprintf");
1102 dt = opendir(dir_test);
1107 error = got_error(GOT_ERR_NOT_GIT_REPO);
1111 if (asprintf(&dir_test, "%s/%s",
1112 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1113 return got_error_from_errno("asprintf");
1115 gw_dir->path = strdup(dir_test);
1118 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1122 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1125 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1126 "refs/heads", TM_DIFF);
1129 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1137 static const struct got_error *
1138 gw_load_got_paths(struct gw_trans *gw_trans)
1140 const struct got_error *error = NULL;
1142 struct dirent **sd_dent;
1143 struct gw_dir *gw_dir;
1145 unsigned int d_cnt, d_i;
1147 d = opendir(gw_trans->gw_conf->got_repos_path);
1149 error = got_error_from_errno2("opendir",
1150 gw_trans->gw_conf->got_repos_path);
1154 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1157 error = got_error_from_errno2("scandir",
1158 gw_trans->gw_conf->got_repos_path);
1162 for (d_i = 0; d_i < d_cnt; d_i++) {
1163 if (gw_trans->gw_conf->got_max_repos > 0 &&
1164 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1165 break; /* account for parent and self */
1167 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1168 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1171 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1172 return got_error_from_errno("gw_dir malloc");
1174 error = gw_load_got_path(gw_trans, gw_dir);
1175 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1180 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1181 !got_path_dir_is_empty(gw_dir->path)) {
1182 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1184 gw_trans->repos_total++;
1192 static const struct got_error *
1193 gw_parse_querystring(struct gw_trans *gw_trans)
1195 const struct got_error *error = NULL;
1197 struct gw_query_action *action = NULL;
1200 if (gw_trans->gw_req->fieldnmap[0]) {
1201 error = got_error_from_errno("bad parse");
1203 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1204 /* define gw_trans->repo_path */
1205 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1206 return got_error_from_errno("asprintf");
1208 if (asprintf(&gw_trans->repo_path, "%s/%s",
1209 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1210 return got_error_from_errno("asprintf");
1212 /* get action and set function */
1213 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1214 for (i = 0; i < nitems(gw_query_funcs); i++) {
1215 action = &gw_query_funcs[i];
1216 if (action->func_name == NULL)
1219 if (strcmp(action->func_name,
1220 p->parsed.s) == 0) {
1221 gw_trans->action = i;
1222 if (asprintf(&gw_trans->action_name,
1223 "%s", action->func_name) == -1)
1225 got_error_from_errno(
1234 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1235 if (asprintf(&gw_trans->commit, "%s",
1237 return got_error_from_errno("asprintf");
1239 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1240 if (asprintf(&gw_trans->repo_file, "%s",
1242 return got_error_from_errno("asprintf");
1244 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1245 if (asprintf(&gw_trans->repo_folder, "%s",
1247 return got_error_from_errno("asprintf");
1249 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1250 if (asprintf(&gw_trans->headref, "%s",
1252 return got_error_from_errno("asprintf");
1254 if (action == NULL) {
1255 error = got_error_from_errno("invalid action");
1258 if ((gw_trans->gw_dir =
1259 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1260 return got_error_from_errno("gw_dir malloc");
1262 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1266 gw_trans->action = GW_INDEX;
1268 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1269 gw_trans->page = p->parsed.i;
1274 static struct gw_dir *
1275 gw_init_gw_dir(char *dir)
1277 struct gw_dir *gw_dir;
1279 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1282 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1288 static const struct got_error *
1289 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1293 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1294 if (kerr != KCGI_OK)
1295 return gw_kcgi_error(kerr);
1296 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1298 if (kerr != KCGI_OK)
1299 return gw_kcgi_error(kerr);
1300 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1302 if (kerr != KCGI_OK)
1303 return gw_kcgi_error(kerr);
1304 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1306 if (kerr != KCGI_OK)
1307 return gw_kcgi_error(kerr);
1308 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1309 if (kerr != KCGI_OK)
1310 return gw_kcgi_error(kerr);
1311 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1313 if (kerr != KCGI_OK)
1314 return gw_kcgi_error(kerr);
1316 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1317 kerr = khttp_head(gw_trans->gw_req,
1318 kresps[KRESP_CONTENT_DISPOSITION],
1319 "attachment; filename=%s", gw_trans->repo_file);
1320 if (kerr != KCGI_OK)
1321 return gw_kcgi_error(kerr);
1324 kerr = khttp_body(gw_trans->gw_req);
1325 return gw_kcgi_error(kerr);
1328 static const struct got_error *
1329 gw_display_index(struct gw_trans *gw_trans)
1331 const struct got_error *error;
1334 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1338 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1340 return gw_kcgi_error(kerr);
1342 if (gw_trans->action != GW_BLOB) {
1343 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1344 gw_query_funcs[gw_trans->action].template);
1345 if (kerr != KCGI_OK) {
1346 khtml_close(gw_trans->gw_html_req);
1347 return gw_kcgi_error(kerr);
1351 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1355 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1357 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1360 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1362 khtml_puts(gw_trans->gw_html_req, err->msg);
1363 khtml_close(gw_trans->gw_html_req);
1367 gw_template(size_t key, void *arg)
1369 const struct got_error *error = NULL;
1370 struct gw_trans *gw_trans = arg;
1371 char *gw_got_link, *gw_site_link;
1372 char *site_owner_name, *site_owner_name_h;
1376 khttp_puts(gw_trans->gw_req, head);
1379 gw_got_link = gw_get_got_link(gw_trans);
1380 if (gw_got_link != NULL)
1381 khttp_puts(gw_trans->gw_req, gw_got_link);
1385 case (TEMPL_SITEPATH):
1386 gw_site_link = gw_get_site_link(gw_trans);
1387 if (gw_site_link != NULL)
1388 khttp_puts(gw_trans->gw_req, gw_site_link);
1393 if (gw_trans->gw_conf->got_site_name != NULL)
1394 khtml_puts(gw_trans->gw_html_req,
1395 gw_trans->gw_conf->got_site_name);
1398 case (TEMPL_SEARCH):
1399 khttp_puts(gw_trans->gw_req, search);
1401 case(TEMPL_SITEOWNER):
1402 if (gw_trans->gw_conf->got_site_owner != NULL &&
1403 gw_trans->gw_conf->got_show_site_owner) {
1404 error = gw_html_escape(&site_owner_name,
1405 gw_trans->gw_conf->got_site_owner);
1408 if (asprintf(&site_owner_name_h, site_owner,
1409 site_owner_name) == -1) {
1410 free(site_owner_name);
1413 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1414 free(site_owner_name);
1415 free(site_owner_name_h);
1418 case(TEMPL_CONTENT):
1419 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1421 khttp_puts(gw_trans->gw_req, error->msg);
1430 gw_gen_commit_header(char *str1, char *str2)
1432 char *return_html = NULL, *ref_str = NULL;
1434 if (strcmp(str2, "") != 0) {
1435 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1436 return_html = strdup("");
1440 ref_str = strdup("");
1443 if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1444 return_html = strdup("");
1451 gw_gen_diff_header(char *str1, char *str2)
1453 char *return_html = NULL;
1455 if (asprintf(&return_html, header_diff_html, str1, str2) == -1)
1456 return_html = strdup("");
1462 gw_gen_author_header(char *str)
1464 char *return_html = NULL;
1466 if (asprintf(&return_html, header_author_html, str) == -1)
1467 return_html = strdup("");
1473 gw_gen_committer_header(char *str)
1475 char *return_html = NULL;
1477 if (asprintf(&return_html, header_committer_html, str) == -1)
1478 return_html = strdup("");
1484 gw_gen_commit_msg_header(char *str)
1486 char *return_html = NULL;
1488 if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1489 return_html = strdup("");
1495 gw_gen_tree_header(char *str)
1497 char *return_html = NULL;
1499 if (asprintf(&return_html, header_tree_html, str) == -1)
1500 return_html = strdup("");
1505 static const struct got_error *
1506 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
1509 const struct got_error *error = NULL;
1511 char *d_file = NULL;
1515 *description = NULL;
1516 if (gw_trans->gw_conf->got_show_repo_description == 0)
1517 return gw_empty_string(description);
1519 if (asprintf(&d_file, "%s/description", dir) == -1)
1520 return got_error_from_errno("asprintf");
1522 f = fopen(d_file, "r");
1524 if (errno == ENOENT || errno == EACCES)
1525 return gw_empty_string(description);
1526 error = got_error_from_errno2("fopen", d_file);
1530 if (fseek(f, 0, SEEK_END) == -1) {
1531 error = got_ferror(f, GOT_ERR_IO);
1536 error = got_ferror(f, GOT_ERR_IO);
1539 if (fseek(f, 0, SEEK_SET) == -1) {
1540 error = got_ferror(f, GOT_ERR_IO);
1543 *description = calloc(len + 1, sizeof(**description));
1544 if (*description == NULL) {
1545 error = got_error_from_errno("calloc");
1549 n = fread(*description, 1, len, f);
1550 if (n == 0 && ferror(f))
1551 error = got_ferror(f, GOT_ERR_IO);
1553 if (f != NULL && fclose(f) == -1 && error == NULL)
1554 error = got_error_from_errno("fclose");
1559 static const struct got_error *
1560 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1564 char *years = "years ago", *months = "months ago";
1565 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1566 char *minutes = "minutes ago", *seconds = "seconds ago";
1567 char *now = "right now";
1575 diff_time = time(NULL) - committer_time;
1576 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1577 if (asprintf(repo_age, "%lld %s",
1578 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1579 return got_error_from_errno("asprintf");
1580 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1581 if (asprintf(repo_age, "%lld %s",
1582 (diff_time / 60 / 60 / 24 / (365 / 12)),
1584 return got_error_from_errno("asprintf");
1585 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1586 if (asprintf(repo_age, "%lld %s",
1587 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1588 return got_error_from_errno("asprintf");
1589 } else if (diff_time > 60 * 60 * 24 * 2) {
1590 if (asprintf(repo_age, "%lld %s",
1591 (diff_time / 60 / 60 / 24), days) == -1)
1592 return got_error_from_errno("asprintf");
1593 } else if (diff_time > 60 * 60 * 2) {
1594 if (asprintf(repo_age, "%lld %s",
1595 (diff_time / 60 / 60), hours) == -1)
1596 return got_error_from_errno("asprintf");
1597 } else if (diff_time > 60 * 2) {
1598 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1600 return got_error_from_errno("asprintf");
1601 } else if (diff_time > 2) {
1602 if (asprintf(repo_age, "%lld %s", diff_time,
1604 return got_error_from_errno("asprintf");
1606 if (asprintf(repo_age, "%s", now) == -1)
1607 return got_error_from_errno("asprintf");
1611 if (gmtime_r(&committer_time, &tm) == NULL)
1612 return got_error_from_errno("gmtime_r");
1614 s = asctime_r(&tm, datebuf);
1616 return got_error_from_errno("asctime_r");
1618 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1619 return got_error_from_errno("asprintf");
1625 static const struct got_error *
1626 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
1627 char *repo_ref, int ref_tm)
1629 const struct got_error *error = NULL;
1630 struct got_object_id *id = NULL;
1631 struct got_repository *repo = NULL;
1632 struct got_commit_object *commit = NULL;
1633 struct got_reflist_head refs;
1634 struct got_reflist_entry *re;
1635 struct got_reference *head_ref;
1637 time_t committer_time = 0, cmp_time = 0;
1638 const char *refname;
1641 SIMPLEQ_INIT(&refs);
1643 if (repo_ref == NULL)
1646 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1649 if (gw_trans->gw_conf->got_show_repo_age == 0)
1652 error = got_repo_open(&repo, dir, NULL);
1657 error = got_ref_list(&refs, repo, "refs/heads",
1658 got_ref_cmp_by_name, NULL);
1660 error = got_ref_list(&refs, repo, repo_ref,
1661 got_ref_cmp_by_name, NULL);
1665 SIMPLEQ_FOREACH(re, &refs, entry) {
1667 refname = strdup(repo_ref);
1669 refname = got_ref_get_name(re->ref);
1670 error = got_ref_open(&head_ref, repo, refname, 0);
1674 error = got_ref_resolve(&id, repo, head_ref);
1675 got_ref_close(head_ref);
1679 error = got_object_open_as_commit(&commit, repo, id);
1684 got_object_commit_get_committer_time(commit);
1686 if (cmp_time < committer_time)
1687 cmp_time = committer_time;
1690 if (cmp_time != 0) {
1691 committer_time = cmp_time;
1692 error = gw_get_time_str(repo_age, committer_time, ref_tm);
1695 got_ref_list_free(&refs);
1700 static const struct got_error *
1701 gw_get_diff(char **diff_html, struct gw_trans *gw_trans,
1702 struct gw_header *header)
1704 const struct got_error *error;
1706 struct got_object_id *id1 = NULL, *id2 = NULL;
1707 struct buf *diffbuf = NULL;
1708 char *label1 = NULL, *label2 = NULL, *line = NULL;
1709 char *diff_line_html = NULL;
1711 size_t newsize, linesize = 0;
1718 error = buf_alloc(&diffbuf, 0);
1722 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1726 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1727 error = got_repo_match_object_id(&id1, &label1,
1728 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1733 error = got_repo_match_object_id(&id2, &label2,
1734 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1738 error = got_object_get_type(&obj_type, header->repo, id2);
1742 case GOT_OBJ_TYPE_BLOB:
1743 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1746 case GOT_OBJ_TYPE_TREE:
1747 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1750 case GOT_OBJ_TYPE_COMMIT:
1751 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1755 error = got_error(GOT_ERR_OBJ_TYPE);
1760 if (fseek(f, 0, SEEK_SET) == -1) {
1761 error = got_ferror(f, GOT_ERR_IO);
1765 while ((linelen = getline(&line, &linesize, f)) != -1) {
1767 error = gw_html_escape(&escaped_line, line);
1771 error = gw_colordiff_line(&diff_line_html, escaped_line);
1775 error = buf_puts(&newsize, diffbuf, diff_line_html);
1779 error = buf_puts(&newsize, diffbuf, div_end);
1783 if (linelen == -1 && ferror(f)) {
1784 error = got_error_from_errno("getline");
1788 if (buf_len(diffbuf) > 0) {
1789 error = buf_putc(diffbuf, '\0');
1792 *diff_html = strdup(buf_get(diffbuf));
1793 if (*diff_html == NULL)
1794 error = got_error_from_errno("strdup");
1797 if (f && fclose(f) == -1 && error == NULL)
1798 error = got_error_from_errno("fclose");
1799 free(diff_line_html);
1810 static const struct got_error *
1811 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
1813 const struct got_error *error = NULL;
1814 struct got_repository *repo;
1815 const char *gitconfig_owner;
1819 if (gw_trans->gw_conf->got_show_repo_owner == 0)
1822 error = got_repo_open(&repo, dir, NULL);
1825 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
1826 if (gitconfig_owner) {
1827 *owner = strdup(gitconfig_owner);
1829 error = got_error_from_errno("strdup");
1831 got_repo_close(repo);
1835 static const struct got_error *
1836 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
1838 const struct got_error *error = NULL;
1840 char *d_file = NULL;
1846 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
1847 return got_error_from_errno("asprintf");
1849 f = fopen(d_file, "r");
1851 if (errno != ENOENT && errno != EACCES)
1852 error = got_error_from_errno2("fopen", d_file);
1856 if (fseek(f, 0, SEEK_END) == -1) {
1857 error = got_ferror(f, GOT_ERR_IO);
1862 error = got_ferror(f, GOT_ERR_IO);
1865 if (fseek(f, 0, SEEK_SET) == -1) {
1866 error = got_ferror(f, GOT_ERR_IO);
1870 *url = calloc(len + 1, sizeof(**url));
1872 error = got_error_from_errno("calloc");
1876 n = fread(*url, 1, len, f);
1877 if (n == 0 && ferror(f))
1878 error = got_ferror(f, GOT_ERR_IO);
1880 if (f && fclose(f) == -1 && error == NULL)
1881 error = got_error_from_errno("fclose");
1886 static const struct got_error *
1887 gw_get_repo_tags(char **tag_html, struct gw_trans *gw_trans,
1888 struct gw_header *header, int limit, int tag_type)
1890 const struct got_error *error = NULL;
1891 struct got_repository *repo = NULL;
1892 struct got_reflist_head refs;
1893 struct got_reflist_entry *re;
1894 char *tag_row = NULL, *tags_navs_disp = NULL;
1895 char *age = NULL, *age_html = NULL, *newline;
1896 char *escaped_tagger = NULL, *escaped_tag_commit = NULL;
1897 char *id_str = NULL, *refstr = NULL;
1898 char *tag_commit0 = NULL;
1899 struct buf *diffbuf = NULL;
1901 struct got_tag_object *tag = NULL;
1905 SIMPLEQ_INIT(&refs);
1907 error = buf_alloc(&diffbuf, 0);
1911 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1915 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1919 SIMPLEQ_FOREACH(re, &refs, entry) {
1920 const char *refname;
1922 const char *tag_commit;
1924 struct got_object_id *id;
1926 refname = got_ref_get_name(re->ref);
1927 if (strncmp(refname, "refs/tags/", 10) != 0)
1930 refstr = got_ref_to_str(re->ref);
1931 if (refstr == NULL) {
1932 error = got_error_from_errno("got_ref_to_str");
1936 error = got_ref_resolve(&id, repo, re->ref);
1939 error = got_object_open_as_tag(&tag, repo, id);
1944 tagger = got_object_tag_get_tagger(tag);
1945 tagger_time = got_object_tag_get_tagger_time(tag);
1947 error = got_object_id_str(&id_str,
1948 got_object_tag_get_object_id(tag));
1952 tag_commit0 = strdup(got_object_tag_get_message(tag));
1953 if (tag_commit0 == NULL) {
1954 error = got_error_from_errno("strdup");
1958 tag_commit = tag_commit0;
1959 while (*tag_commit == '\n')
1964 newline = strchr(tag_commit, '\n');
1968 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
1972 if (asprintf(&tags_navs_disp, tags_navs,
1973 gw_trans->repo_name, id_str, gw_trans->repo_name,
1974 id_str, gw_trans->repo_name, id_str,
1975 gw_trans->repo_name, id_str) == -1) {
1976 error = got_error_from_errno("asprintf");
1980 if (asprintf(&tag_row, tags_row, age ? age : "",
1981 refname, tag_commit, tags_navs_disp) == -1) {
1982 error = got_error_from_errno("asprintf");
1986 free(tags_navs_disp);
1989 error = gw_get_time_str(&age, tagger_time, TM_LONG);
1992 error = gw_html_escape(&escaped_tagger, tagger);
1995 error = gw_html_escape(&escaped_tag_commit, tag_commit);
1998 if (asprintf(&tag_row, tag_info, age ? age : "",
1999 escaped_tagger, escaped_tag_commit) == -1) {
2000 error = got_error_from_errno("asprintf");
2008 error = buf_puts(&newsize, diffbuf, tag_row);
2012 if (limit && --limit == 0)
2015 got_object_tag_close(tag);
2025 free(escaped_tagger);
2026 escaped_tagger = NULL;
2027 free(escaped_tag_commit);
2028 escaped_tag_commit = NULL;
2035 if (buf_len(diffbuf) > 0) {
2036 error = buf_putc(diffbuf, '\0');
2037 *tag_html = strdup(buf_get(diffbuf));
2038 if (*tag_html == NULL)
2039 error = got_error_from_errno("strdup");
2043 got_object_tag_close(tag);
2048 free(escaped_tagger);
2049 free(escaped_tag_commit);
2053 got_ref_list_free(&refs);
2055 got_repo_close(repo);
2060 gw_free_headers(struct gw_header *header)
2064 if (header->commit != NULL)
2065 got_object_commit_close(header->commit);
2067 got_repo_close(header->repo);
2068 free(header->refs_str);
2069 free(header->commit_id);
2070 free(header->parent_id);
2071 free(header->tree_id);
2072 free(header->author);
2073 free(header->committer);
2074 free(header->commit_msg);
2077 static struct gw_header *
2080 struct gw_header *header;
2082 header = malloc(sizeof(*header));
2086 header->repo = NULL;
2087 header->commit = NULL;
2089 header->path = NULL;
2090 SIMPLEQ_INIT(&header->refs);
2095 static const struct got_error *
2096 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2099 const struct got_error *error = NULL;
2100 struct got_commit_graph *graph = NULL;
2102 error = got_commit_graph_open(&graph, header->path, 0);
2106 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2112 error = got_commit_graph_iter_next(&header->id, graph,
2113 header->repo, NULL, NULL);
2115 if (error->code == GOT_ERR_ITER_COMPLETED)
2119 if (header->id == NULL)
2122 error = got_object_open_as_commit(&header->commit, header->repo,
2127 error = gw_get_commit(gw_trans, header);
2129 struct gw_header *n_header = NULL;
2130 if ((n_header = gw_init_header()) == NULL) {
2131 error = got_error_from_errno("malloc");
2135 n_header->refs_str = strdup(header->refs_str);
2136 n_header->commit_id = strdup(header->commit_id);
2137 n_header->parent_id = strdup(header->parent_id);
2138 n_header->tree_id = strdup(header->tree_id);
2139 n_header->author = strdup(header->author);
2140 n_header->committer = strdup(header->committer);
2141 n_header->commit_msg = strdup(header->commit_msg);
2142 n_header->committer_time = header->committer_time;
2143 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2146 if (error || (limit && --limit == 0))
2151 got_commit_graph_close(graph);
2155 static const struct got_error *
2156 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2158 const struct got_error *error = NULL;
2159 struct got_reflist_entry *re;
2160 struct got_object_id *id2 = NULL;
2161 struct got_object_qid *parent_id;
2162 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2165 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2168 struct got_tag_object *tag = NULL;
2171 name = got_ref_get_name(re->ref);
2172 if (strcmp(name, GOT_REF_HEAD) == 0)
2174 if (strncmp(name, "refs/", 5) == 0)
2176 if (strncmp(name, "got/", 4) == 0)
2178 if (strncmp(name, "heads/", 6) == 0)
2180 if (strncmp(name, "remotes/", 8) == 0)
2182 if (strncmp(name, "tags/", 5) == 0) {
2183 error = got_object_open_as_tag(&tag, header->repo,
2186 if (error->code != GOT_ERR_OBJ_TYPE)
2189 * Ref points at something other
2196 cmp = got_object_id_cmp(tag ?
2197 got_object_tag_get_object_id(tag) : re->id, header->id);
2199 got_object_tag_close(tag);
2203 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2204 s ? ", " : "", name) == -1) {
2205 error = got_error_from_errno("asprintf");
2209 header->refs_str = strdup(refs_str);
2213 if (refs_str == NULL)
2214 header->refs_str = strdup("");
2217 error = got_object_id_str(&header->commit_id, header->id);
2221 error = got_object_id_str(&header->tree_id,
2222 got_object_commit_get_tree_id(header->commit));
2226 if (gw_trans->action == GW_DIFF) {
2227 parent_id = SIMPLEQ_FIRST(
2228 got_object_commit_get_parent_ids(header->commit));
2229 if (parent_id != NULL) {
2230 id2 = got_object_id_dup(parent_id->id);
2232 error = got_object_id_str(&header->parent_id, id2);
2237 header->parent_id = strdup("/dev/null");
2239 header->parent_id = strdup("");
2241 header->committer_time =
2242 got_object_commit_get_committer_time(header->commit);
2244 error = gw_html_escape(&header->author,
2245 got_object_commit_get_author(header->commit));
2248 error = gw_html_escape(&header->committer,
2249 got_object_commit_get_committer(header->commit));
2253 /* XXX Doesn't the log message require escaping? */
2254 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2258 commit_msg = commit_msg0;
2259 while (*commit_msg == '\n')
2262 header->commit_msg = strdup(commit_msg);
2267 static const struct got_error *
2268 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2270 const struct got_error *error = NULL;
2271 char *in_repo_path = NULL;
2273 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2277 if (gw_trans->commit == NULL) {
2278 struct got_reference *head_ref;
2279 error = got_ref_open(&head_ref, header->repo,
2280 gw_trans->headref, 0);
2284 error = got_ref_resolve(&header->id, header->repo, head_ref);
2285 got_ref_close(head_ref);
2289 error = got_object_open_as_commit(&header->commit,
2290 header->repo, header->id);
2292 struct got_reference *ref;
2293 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2294 if (error == NULL) {
2296 error = got_ref_resolve(&header->id, header->repo, ref);
2300 error = got_object_get_type(&obj_type, header->repo,
2304 if (obj_type == GOT_OBJ_TYPE_TAG) {
2305 struct got_tag_object *tag;
2306 error = got_object_open_as_tag(&tag,
2307 header->repo, header->id);
2310 if (got_object_tag_get_object_type(tag) !=
2311 GOT_OBJ_TYPE_COMMIT) {
2312 got_object_tag_close(tag);
2313 error = got_error(GOT_ERR_OBJ_TYPE);
2317 header->id = got_object_id_dup(
2318 got_object_tag_get_object_id(tag));
2319 if (header->id == NULL)
2320 error = got_error_from_errno(
2321 "got_object_id_dup");
2322 got_object_tag_close(tag);
2325 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2326 error = got_error(GOT_ERR_OBJ_TYPE);
2329 error = got_object_open_as_commit(&header->commit,
2330 header->repo, header->id);
2334 if (header->commit == NULL) {
2335 error = got_repo_match_object_id_prefix(&header->id,
2336 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2341 error = got_repo_match_object_id_prefix(&header->id,
2342 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2346 error = got_repo_map_path(&in_repo_path, header->repo,
2347 gw_trans->repo_path, 1);
2352 header->path = strdup(in_repo_path);
2356 error = got_ref_list(&header->refs, header->repo, NULL,
2357 got_ref_cmp_by_name, NULL);
2361 error = gw_get_commits(gw_trans, header, limit);
2369 char datebuf[11]; /* YYYY-MM-DD + NUL */
2372 struct gw_blame_cb_args {
2373 struct blame_line *lines;
2377 off_t *line_offsets;
2379 struct got_repository *repo;
2380 struct gw_trans *gw_trans;
2381 struct buf *blamebuf;
2384 static const struct got_error *
2385 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2387 const struct got_error *err = NULL;
2388 struct gw_blame_cb_args *a = arg;
2389 struct blame_line *bline;
2391 size_t linesize = 0, newsize;
2392 struct got_commit_object *commit = NULL;
2395 time_t committer_time;
2397 if (nlines != a->nlines ||
2398 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2399 return got_error(GOT_ERR_RANGE);
2402 return NULL; /* no change in this commit */
2404 /* Annotate this line. */
2405 bline = &a->lines[lineno - 1];
2406 if (bline->annotated)
2408 err = got_object_id_str(&bline->id_str, id);
2412 err = got_object_open_as_commit(&commit, a->repo, id);
2416 bline->committer = strdup(got_object_commit_get_committer(commit));
2417 if (bline->committer == NULL) {
2418 err = got_error_from_errno("strdup");
2422 committer_time = got_object_commit_get_committer_time(commit);
2423 if (localtime_r(&committer_time, &tm) == NULL)
2424 return got_error_from_errno("localtime_r");
2425 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2426 &tm) >= sizeof(bline->datebuf)) {
2427 err = got_error(GOT_ERR_NO_SPACE);
2430 bline->annotated = 1;
2432 /* Print lines annotated so far. */
2433 bline = &a->lines[a->lineno_cur - 1];
2434 if (!bline->annotated)
2437 offset = a->line_offsets[a->lineno_cur - 1];
2438 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2439 err = got_error_from_errno("fseeko");
2443 while (bline->annotated) {
2444 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2445 *line_escape = NULL;
2448 if (getline(&line, &linesize, a->f) == -1) {
2450 err = got_error_from_errno("getline");
2454 committer = bline->committer;
2455 smallerthan = strchr(committer, '<');
2456 if (smallerthan && smallerthan[1] != '\0')
2457 committer = smallerthan + 1;
2458 at = strchr(committer, '@');
2461 len = strlen(committer);
2463 committer[8] = '\0';
2465 nl = strchr(line, '\n');
2469 err = gw_html_escape(&line_escape, line);
2473 if (a->gw_trans->repo_folder == NULL)
2474 a->gw_trans->repo_folder = strdup("");
2475 if (a->gw_trans->repo_folder == NULL)
2477 asprintf(&blame_row, blame_line, a->nlines_prec, a->lineno_cur,
2478 a->gw_trans->repo_name, bline->id_str,
2479 a->gw_trans->repo_file, a->gw_trans->repo_folder,
2480 bline->id_str, bline->datebuf, committer, line_escape);
2482 err = buf_puts(&newsize, a->blamebuf, blame_row);
2486 bline = &a->lines[a->lineno_cur - 1];
2493 got_object_commit_close(commit);
2498 static const struct got_error *
2499 gw_get_file_blame_blob(char **blame_html, struct gw_trans *gw_trans)
2501 const struct got_error *error = NULL;
2502 struct got_repository *repo = NULL;
2503 struct got_object_id *obj_id = NULL;
2504 struct got_object_id *commit_id = NULL;
2505 struct got_blob_object *blob = NULL;
2506 char *path = NULL, *in_repo_path = NULL;
2507 struct gw_blame_cb_args bca;
2513 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2517 if (asprintf(&path, "%s%s%s",
2518 gw_trans->repo_folder ? gw_trans->repo_folder : "",
2519 gw_trans->repo_folder ? "/" : "",
2520 gw_trans->repo_file) == -1) {
2521 error = got_error_from_errno("asprintf");
2525 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2529 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2530 GOT_OBJ_TYPE_COMMIT, 1, repo);
2534 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2538 if (obj_id == NULL) {
2539 error = got_error(GOT_ERR_NO_OBJ);
2543 error = got_object_get_type(&obj_type, repo, obj_id);
2547 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2548 error = got_error(GOT_ERR_OBJ_TYPE);
2552 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2556 error = buf_alloc(&bca.blamebuf, 0);
2560 bca.f = got_opentemp();
2561 if (bca.f == NULL) {
2562 error = got_error_from_errno("got_opentemp");
2565 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2566 &bca.line_offsets, bca.f, blob);
2567 if (error || bca.nlines == 0)
2570 /* Don't include \n at EOF in the blame line count. */
2571 if (bca.line_offsets[bca.nlines - 1] == filesize)
2574 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2575 if (bca.lines == NULL) {
2576 error = got_error_from_errno("calloc");
2580 bca.nlines_prec = 0;
2587 bca.gw_trans = gw_trans;
2589 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2593 if (buf_len(bca.blamebuf) > 0) {
2594 error = buf_putc(bca.blamebuf, '\0');
2597 *blame_html = strdup(buf_get(bca.blamebuf));
2598 if (*blame_html == NULL) {
2599 error = got_error_from_errno("strdup");
2604 free(bca.line_offsets);
2611 for (i = 0; i < bca.nlines; i++) {
2612 struct blame_line *bline = &bca.lines[i];
2613 free(bline->id_str);
2614 free(bline->committer);
2617 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2618 error = got_error_from_errno("fclose");
2620 got_object_blob_close(blob);
2622 got_repo_close(repo);
2626 static const struct got_error *
2627 gw_get_file_read_blob(char **blobstr, size_t *filesize, struct gw_trans *gw_trans)
2629 const struct got_error *error = NULL;
2630 struct got_repository *repo = NULL;
2631 struct got_object_id *obj_id = NULL;
2632 struct got_object_id *commit_id = NULL;
2633 struct got_blob_object *blob = NULL;
2634 char *path = NULL, *in_repo_path = NULL;
2642 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2646 if (asprintf(&path, "%s%s%s",
2647 gw_trans->repo_folder ? gw_trans->repo_folder : "",
2648 gw_trans->repo_folder ? "/" : "",
2649 gw_trans->repo_file) == -1) {
2650 error = got_error_from_errno("asprintf");
2654 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2658 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2659 GOT_OBJ_TYPE_COMMIT, 1, repo);
2663 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2667 if (obj_id == NULL) {
2668 error = got_error(GOT_ERR_NO_OBJ);
2672 error = got_object_get_type(&obj_type, repo, obj_id);
2676 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2677 error = got_error(GOT_ERR_OBJ_TYPE);
2681 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2687 error = got_error_from_errno("got_opentemp");
2690 error = got_object_blob_dump_to_file(filesize, NULL, NULL, f, blob);
2694 /* XXX This will fail on large files... */
2695 *blobstr = calloc(*filesize + 1, sizeof(**blobstr));
2696 if (*blobstr == NULL) {
2697 error = got_error_from_errno("calloc");
2701 n = fread(*blobstr, 1, *filesize, f);
2704 error = got_ferror(f, GOT_ERR_IO);
2713 got_object_blob_close(blob);
2715 got_repo_close(repo);
2716 if (f != NULL && fclose(f) == -1 && error == NULL)
2717 error = got_error_from_errno("fclose");
2726 static const struct got_error *
2727 gw_get_repo_tree(char **tree_html, struct gw_trans *gw_trans)
2729 const struct got_error *error = NULL;
2730 struct got_repository *repo = NULL;
2731 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2732 struct got_tree_object *tree = NULL;
2733 struct buf *diffbuf = NULL;
2735 char *path = NULL, *in_repo_path = NULL, *tree_row = NULL;
2736 char *id_str = NULL;
2737 char *build_folder = NULL;
2738 char *url_html = NULL;
2739 const char *class = NULL;
2740 int nentries, i, class_flip = 0;
2744 error = buf_alloc(&diffbuf, 0);
2748 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2752 if (gw_trans->repo_folder != NULL)
2753 path = strdup(gw_trans->repo_folder);
2755 error = got_repo_map_path(&in_repo_path, repo,
2756 gw_trans->repo_path, 1);
2760 path = in_repo_path;
2763 if (gw_trans->commit == NULL) {
2764 struct got_reference *head_ref;
2765 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2768 error = got_ref_resolve(&commit_id, repo, head_ref);
2771 got_ref_close(head_ref);
2774 error = got_repo_match_object_id(&commit_id, NULL,
2775 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2780 error = got_object_id_str(&gw_trans->commit, commit_id);
2784 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2788 error = got_object_open_as_tree(&tree, repo, tree_id);
2792 nentries = got_object_tree_get_nentries(tree);
2793 for (i = 0; i < nentries; i++) {
2794 struct got_tree_entry *te;
2795 const char *modestr = "";
2798 te = got_object_tree_get_entry(tree, i);
2800 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2804 mode = got_tree_entry_get_mode(te);
2805 if (got_object_tree_entry_is_submodule(te))
2807 else if (S_ISLNK(mode))
2809 else if (S_ISDIR(mode))
2811 else if (mode & S_IXUSR)
2814 if (class_flip == 0) {
2815 class = "back_lightgray";
2818 class = "back_white";
2822 if (S_ISDIR(mode)) {
2823 if (asprintf(&build_folder, "%s%s%s",
2824 gw_trans->repo_folder ? "/" : "",
2825 gw_trans->repo_folder ? gw_trans->repo_folder : "",
2826 got_tree_entry_get_name(te)) == -1) {
2827 error = got_error_from_errno(
2832 if (asprintf(&url_html, folder_html,
2833 gw_trans->repo_name, gw_trans->action_name,
2834 gw_trans->commit, build_folder,
2835 got_tree_entry_get_name(te), modestr) == -1) {
2836 error = got_error_from_errno("asprintf");
2839 if (asprintf(&tree_row, tree_line, class, url_html,
2841 error = got_error_from_errno("asprintf");
2845 if (asprintf(&url_html, file_html, gw_trans->repo_name,
2846 "blob", gw_trans->commit,
2847 got_tree_entry_get_name(te),
2848 gw_trans->repo_folder ? gw_trans->repo_folder : "",
2849 got_tree_entry_get_name(te), modestr) == -1) {
2850 error = got_error_from_errno("asprintf");
2854 if (asprintf(&tree_row, tree_line_with_navs, class,
2855 url_html, class, gw_trans->repo_name, "blob",
2856 gw_trans->commit, got_tree_entry_get_name(te),
2857 gw_trans->repo_folder ? gw_trans->repo_folder : "",
2858 "blob", gw_trans->repo_name,
2859 "blame", gw_trans->commit,
2860 got_tree_entry_get_name(te),
2861 gw_trans->repo_folder ? gw_trans->repo_folder : "",
2863 error = got_error_from_errno("asprintf");
2868 error = buf_puts(&newsize, diffbuf, tree_row);
2879 build_folder = NULL;
2882 if (buf_len(diffbuf) > 0) {
2883 error = buf_putc(diffbuf, '\0');
2886 *tree_html = strdup(buf_get(diffbuf));
2887 if (*tree_html == NULL) {
2888 error = got_error_from_errno("strdup");
2894 got_object_tree_close(tree);
2896 got_repo_close(repo);
2909 gw_get_repo_heads(struct gw_trans *gw_trans)
2911 const struct got_error *error = NULL;
2912 struct got_repository *repo = NULL;
2913 struct got_reflist_head refs;
2914 struct got_reflist_entry *re;
2915 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2916 struct buf *diffbuf = NULL;
2919 SIMPLEQ_INIT(&refs);
2921 error = buf_alloc(&diffbuf, 0);
2925 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2929 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2934 SIMPLEQ_FOREACH(re, &refs, entry) {
2937 refname = strdup(got_ref_get_name(re->ref));
2938 if (refname == NULL) {
2939 error = got_error_from_errno("got_ref_to_str");
2943 if (strncmp(refname, "refs/heads/", 11) != 0) {
2948 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
2953 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2954 refname, gw_trans->repo_name, refname,
2955 gw_trans->repo_name, refname, gw_trans->repo_name,
2957 error = got_error_from_errno("asprintf");
2961 if (strncmp(refname, "refs/heads/", 11) == 0)
2964 if (asprintf(&head_row, heads_row, age, refname,
2965 head_navs_disp) == -1) {
2966 error = got_error_from_errno("asprintf");
2970 error = buf_puts(&newsize, diffbuf, head_row);
2972 free(head_navs_disp);
2976 if (buf_len(diffbuf) > 0) {
2977 error = buf_putc(diffbuf, '\0');
2978 heads = strdup(buf_get(diffbuf));
2982 got_ref_list_free(&refs);
2984 got_repo_close(repo);
2992 gw_get_got_link(struct gw_trans *gw_trans)
2996 if (asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2997 gw_trans->gw_conf->got_logo) == -1)
3004 gw_get_site_link(struct gw_trans *gw_trans)
3006 char *link = NULL, *repo = NULL, *action = NULL;
3008 if (gw_trans->repo_name != NULL &&
3009 asprintf(&repo, " / <a href='?path=%s&action=summary'>%s</a>",
3010 gw_trans->repo_name, gw_trans->repo_name) == -1)
3013 if (gw_trans->action_name != NULL &&
3014 asprintf(&action, " / %s", gw_trans->action_name) == -1) {
3019 if (asprintf(&link, site_link, GOTWEB,
3020 gw_trans->gw_conf->got_site_link,
3021 repo ? repo : "", action ? action : "") == -1) {
3032 static const struct got_error *
3033 gw_colordiff_line(char **colorized_line, char *buf)
3035 const struct got_error *error = NULL;
3036 char *div_diff_line_div = NULL, *color = NULL;
3037 struct buf *diffbuf = NULL;
3040 *colorized_line = NULL;
3042 error = buf_alloc(&diffbuf, 0);
3046 if (strncmp(buf, "-", 1) == 0)
3047 color = "diff_minus";
3048 else if (strncmp(buf, "+", 1) == 0)
3049 color = "diff_plus";
3050 else if (strncmp(buf, "@@", 2) == 0)
3051 color = "diff_chunk_header";
3052 else if (strncmp(buf, "@@", 2) == 0)
3053 color = "diff_chunk_header";
3054 else if (strncmp(buf, "commit +", 8) == 0)
3055 color = "diff_meta";
3056 else if (strncmp(buf, "commit -", 8) == 0)
3057 color = "diff_meta";
3058 else if (strncmp(buf, "blob +", 6) == 0)
3059 color = "diff_meta";
3060 else if (strncmp(buf, "blob -", 6) == 0)
3061 color = "diff_meta";
3062 else if (strncmp(buf, "file +", 6) == 0)
3063 color = "diff_meta";
3064 else if (strncmp(buf, "file -", 6) == 0)
3065 color = "diff_meta";
3066 else if (strncmp(buf, "from:", 5) == 0)
3067 color = "diff_author";
3068 else if (strncmp(buf, "via:", 4) == 0)
3069 color = "diff_author";
3070 else if (strncmp(buf, "date:", 5) == 0)
3071 color = "diff_date";
3073 if (asprintf(&div_diff_line_div, div_diff_line, color ? color : "")
3075 error = got_error_from_errno("asprintf");
3079 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
3083 error = buf_puts(&newsize, diffbuf, buf);
3087 if (buf_len(diffbuf) > 0) {
3088 error = buf_putc(diffbuf, '\0');
3089 *colorized_line = strdup(buf_get(diffbuf));
3090 if (*colorized_line == NULL)
3091 error = got_error_from_errno("strdup");
3095 free(div_diff_line_div);
3100 * XXX This function should not exist.
3101 * We should let khtml_puts(3) handle HTML escaping.
3103 static const struct got_error *
3104 gw_html_escape(char **escaped_html, const char *orig_html)
3106 const struct got_error *error = NULL;
3107 struct escape_pair {
3118 size_t orig_len, len;
3121 orig_len = strlen(orig_html);
3123 for (i = 0; i < orig_len; i++) {
3124 for (j = 0; j < nitems(esc); j++) {
3125 if (orig_html[i] != esc[j].c)
3127 len += strlen(esc[j].s) - 1 /* escaped char */;
3131 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
3132 if (*escaped_html == NULL)
3133 return got_error_from_errno("calloc");
3136 for (i = 0; i < orig_len; i++) {
3138 for (j = 0; j < nitems(esc); j++) {
3139 if (orig_html[i] != esc[j].c)
3142 if (strlcat(*escaped_html, esc[j].s, len + 1)
3144 error = got_error(GOT_ERR_NO_SPACE);
3147 x += strlen(esc[j].s);
3152 (*escaped_html)[x] = orig_html[i];
3158 free(*escaped_html);
3159 *escaped_html = NULL;
3161 (*escaped_html)[x] = '\0';
3167 main(int argc, char *argv[])
3169 const struct got_error *error = NULL;
3170 struct gw_trans *gw_trans;
3171 struct gw_dir *dir = NULL, *tdir;
3172 const char *page = "index";
3176 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3179 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3182 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3185 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3188 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3189 if (kerr != KCGI_OK) {
3190 error = gw_kcgi_error(kerr);
3194 if ((gw_trans->gw_conf =
3195 malloc(sizeof(struct gotweb_conf))) == NULL) {
3197 error = got_error_from_errno("malloc");
3201 TAILQ_INIT(&gw_trans->gw_dirs);
3202 TAILQ_INIT(&gw_trans->gw_headers);
3205 gw_trans->repos_total = 0;
3206 gw_trans->repo_path = NULL;
3207 gw_trans->commit = NULL;
3208 gw_trans->headref = strdup(GOT_REF_HEAD);
3209 gw_trans->mime = KMIME_TEXT_HTML;
3210 gw_trans->gw_tmpl->key = gw_templs;
3211 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3212 gw_trans->gw_tmpl->arg = gw_trans;
3213 gw_trans->gw_tmpl->cb = gw_template;
3214 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3218 error = gw_parse_querystring(gw_trans);
3222 if (gw_trans->action == GW_BLOB)
3223 error = gw_blob(gw_trans);
3225 error = gw_display_index(gw_trans);
3228 gw_trans->mime = KMIME_TEXT_PLAIN;
3229 gw_trans->action = GW_ERR;
3230 gw_display_error(gw_trans, error);
3233 free(gw_trans->gw_conf->got_repos_path);
3234 free(gw_trans->gw_conf->got_site_name);
3235 free(gw_trans->gw_conf->got_site_owner);
3236 free(gw_trans->gw_conf->got_site_link);
3237 free(gw_trans->gw_conf->got_logo);
3238 free(gw_trans->gw_conf->got_logo_url);
3239 free(gw_trans->gw_conf);
3240 free(gw_trans->commit);
3241 free(gw_trans->repo_path);
3242 free(gw_trans->repo_name);
3243 free(gw_trans->repo_file);
3244 free(gw_trans->action_name);
3245 free(gw_trans->headref);
3247 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3249 free(dir->description);
3258 khttp_free(gw_trans->gw_req);