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 char *gw_get_repo_description(struct gw_trans *,
163 static char *gw_get_repo_owner(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 char *gw_get_clone_url(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_blame(struct gw_trans *gw_trans)
311 const struct got_error *error = NULL;
312 struct gw_header *header = NULL;
313 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
316 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
318 return got_error_from_errno("pledge");
320 if ((header = gw_init_header()) == NULL)
321 return got_error_from_errno("malloc");
323 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
327 error = gw_get_header(gw_trans, header, 1);
331 blame_html = gw_get_file_blame_blob(gw_trans);
333 if (blame_html == NULL) {
334 blame_html = strdup("");
335 if (blame_html == NULL) {
336 error = got_error_from_errno("strdup");
341 if (asprintf(&blame_html_disp, blame_header,
342 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
343 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
345 error = got_error_from_errno("asprintf");
349 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
350 error = got_error_from_errno("asprintf");
354 kerr = khttp_puts(gw_trans->gw_req, blame);
356 error = gw_kcgi_error(kerr);
358 got_ref_list_free(&header->refs);
359 gw_free_headers(header);
360 free(blame_html_disp);
366 static const struct got_error *
367 gw_blob(struct gw_trans *gw_trans)
369 const struct got_error *error = NULL;
370 struct gw_header *header = NULL;
374 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
376 return got_error_from_errno("pledge");
378 if ((header = gw_init_header()) == NULL)
379 return got_error_from_errno("malloc");
381 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
385 error = gw_get_header(gw_trans, header, 1);
389 blob = gw_get_file_blame_blob(gw_trans);
394 error = got_error_from_errno("strdup");
399 if (gw_trans->mime == KMIME_APP_OCTET_STREAM)
402 kerr = khttp_puts(gw_trans->gw_req, blob);
404 error = gw_kcgi_error(kerr);
407 got_ref_list_free(&header->refs);
408 gw_free_headers(header);
413 static const struct got_error *
414 gw_diff(struct gw_trans *gw_trans)
416 const struct got_error *error = NULL;
417 struct gw_header *header = NULL;
418 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
421 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
423 return got_error_from_errno("pledge");
425 if ((header = gw_init_header()) == NULL)
426 return got_error_from_errno("malloc");
428 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
432 error = gw_get_header(gw_trans, header, 1);
436 diff_html = gw_get_diff(gw_trans, header);
438 if (diff_html == NULL) {
439 diff_html = strdup("");
440 if (diff_html == NULL) {
441 error = got_error_from_errno("strdup");
446 if (asprintf(&diff_html_disp, diff_header,
447 gw_gen_diff_header(header->parent_id, header->commit_id),
448 gw_gen_commit_header(header->commit_id, header->refs_str),
449 gw_gen_tree_header(header->tree_id),
450 gw_gen_author_header(header->author),
451 gw_gen_committer_header(header->committer),
452 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
453 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
455 error = got_error_from_errno("asprintf");
459 if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
460 error = got_error_from_errno("asprintf");
464 kerr = khttp_puts(gw_trans->gw_req, diff);
466 error = gw_kcgi_error(kerr);
468 got_ref_list_free(&header->refs);
469 gw_free_headers(header);
470 free(diff_html_disp);
476 static const struct got_error *
477 gw_index(struct gw_trans *gw_trans)
479 const struct got_error *error = NULL;
480 struct gw_dir *gw_dir = NULL;
481 char *html, *navs, *next, *prev;
482 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
485 if (pledge("stdio rpath proc exec sendfd unveil",
487 error = got_error_from_errno("pledge");
491 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
495 error = gw_load_got_paths(gw_trans);
499 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
501 return gw_kcgi_error(kerr);
503 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
504 if (asprintf(&html, index_projects_empty,
505 gw_trans->gw_conf->got_repos_path) == -1)
506 return got_error_from_errno("asprintf");
507 kerr = khttp_puts(gw_trans->gw_req, html);
509 error = gw_kcgi_error(kerr);
514 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
517 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
518 if (gw_trans->page > 0 && (gw_trans->page *
519 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
528 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
529 gw_dir->name, gw_dir->name) == -1)
530 return got_error_from_errno("asprintf");
532 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
533 gw_dir->description, gw_dir->owner, gw_dir->age,
535 return got_error_from_errno("asprintf");
537 kerr = khttp_puts(gw_trans->gw_req, html);
541 return gw_kcgi_error(kerr);
543 if (gw_trans->gw_conf->got_max_repos_display == 0)
546 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
547 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
549 return gw_kcgi_error(kerr);
550 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
551 (gw_trans->page > 0) &&
552 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
553 prev_disp == gw_trans->repos_total)) {
554 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
556 return gw_kcgi_error(kerr);
559 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
560 (gw_trans->page > 0) &&
561 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
562 prev_disp == gw_trans->repos_total)) {
563 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
564 return got_error_from_errno("asprintf");
565 kerr = khttp_puts(gw_trans->gw_req, prev);
568 return gw_kcgi_error(kerr);
571 kerr = khttp_puts(gw_trans->gw_req, div_end);
573 return gw_kcgi_error(kerr);
575 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
576 next_disp == gw_trans->gw_conf->got_max_repos_display &&
577 dir_c != (gw_trans->page + 1) *
578 gw_trans->gw_conf->got_max_repos_display) {
579 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
580 return got_error_from_errno("calloc");
581 kerr = khttp_puts(gw_trans->gw_req, next);
584 return gw_kcgi_error(kerr);
585 kerr = khttp_puts(gw_trans->gw_req, div_end);
587 error = gw_kcgi_error(kerr);
592 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
593 (gw_trans->page > 0) &&
594 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
595 prev_disp == gw_trans->repos_total)) {
596 kerr = khttp_puts(gw_trans->gw_req, div_end);
598 return gw_kcgi_error(kerr);
606 static const struct got_error *
607 gw_commits(struct gw_trans *gw_trans)
609 const struct got_error *error = NULL;
610 char *commits_html, *commits_navs_html;
611 struct gw_header *header = NULL, *n_header = NULL;
614 if ((header = gw_init_header()) == NULL)
615 return got_error_from_errno("malloc");
617 if (pledge("stdio rpath proc exec sendfd unveil",
619 error = got_error_from_errno("pledge");
623 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
627 error = gw_get_header(gw_trans, header,
628 gw_trans->gw_conf->got_max_commits_display);
632 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
633 if (kerr != KCGI_OK) {
634 error = gw_kcgi_error(kerr);
637 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
638 if (asprintf(&commits_navs_html, commits_navs,
639 gw_trans->repo_name, n_header->commit_id,
640 gw_trans->repo_name, n_header->commit_id,
641 gw_trans->repo_name, n_header->commit_id) == -1) {
642 error = got_error_from_errno("asprintf");
646 if (asprintf(&commits_html, commits_line,
647 gw_gen_commit_header(n_header->commit_id,
649 gw_gen_author_header(n_header->author),
650 gw_gen_committer_header(n_header->committer),
651 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
652 TM_LONG)), gw_html_escape(n_header->commit_msg),
653 commits_navs_html) == -1) {
654 error = got_error_from_errno("asprintf");
657 kerr = khttp_puts(gw_trans->gw_req, commits_html);
658 if (kerr != KCGI_OK) {
659 error = gw_kcgi_error(kerr);
663 kerr = khttp_puts(gw_trans->gw_req, div_end);
665 error = gw_kcgi_error(kerr);
667 got_ref_list_free(&header->refs);
668 gw_free_headers(header);
669 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
670 gw_free_headers(n_header);
674 static const struct got_error *
675 gw_briefs(struct gw_trans *gw_trans)
677 const struct got_error *error = NULL;
678 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
679 struct gw_header *header = NULL, *n_header = NULL;
682 if ((header = gw_init_header()) == NULL)
683 return got_error_from_errno("malloc");
685 if (pledge("stdio rpath proc exec sendfd unveil",
687 error = got_error_from_errno("pledge");
691 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
695 if (gw_trans->action == GW_SUMMARY)
696 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
698 error = gw_get_header(gw_trans, header,
699 gw_trans->gw_conf->got_max_commits_display);
703 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
704 if (kerr != KCGI_OK) {
705 error = gw_kcgi_error(kerr);
709 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
710 if (asprintf(&briefs_navs_html, briefs_navs,
711 gw_trans->repo_name, n_header->commit_id,
712 gw_trans->repo_name, n_header->commit_id,
713 gw_trans->repo_name, n_header->commit_id) == -1) {
714 error = got_error_from_errno("asprintf");
717 newline = strchr(n_header->commit_msg, '\n');
720 if (asprintf(&briefs_html, briefs_line,
721 gw_get_time_str(n_header->committer_time, TM_DIFF),
722 n_header->author, gw_html_escape(n_header->commit_msg),
723 briefs_navs_html) == -1) {
724 error = got_error_from_errno("asprintf");
727 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
728 if (kerr != KCGI_OK) {
729 error = gw_kcgi_error(kerr);
733 kerr = khttp_puts(gw_trans->gw_req, div_end);
735 error = gw_kcgi_error(kerr);
737 got_ref_list_free(&header->refs);
738 gw_free_headers(header);
739 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
740 gw_free_headers(n_header);
744 static const struct got_error *
745 gw_summary(struct gw_trans *gw_trans)
747 const struct got_error *error = NULL;
748 char *description_html = NULL, *repo_owner_html = NULL;
749 char *age = NULL, *repo_age_html = NULL, *cloneurl_html = NULL;
750 char *tags = NULL, *tags_html = NULL;
751 char *heads = NULL, *heads_html = NULL;
754 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
755 return got_error_from_errno("pledge");
757 /* unveil is applied with gw_briefs below */
759 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
761 return gw_kcgi_error(kerr);
763 if (gw_trans->gw_conf->got_show_repo_description) {
764 if (gw_trans->gw_dir->description != NULL &&
765 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
766 if (asprintf(&description_html, description,
767 gw_trans->gw_dir->description) == -1) {
768 error = got_error_from_errno("asprintf");
772 kerr = khttp_puts(gw_trans->gw_req, description_html);
773 if (kerr != KCGI_OK) {
774 error = gw_kcgi_error(kerr);
780 if (gw_trans->gw_conf->got_show_repo_owner) {
781 if (gw_trans->gw_dir->owner != NULL &&
782 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
783 if (asprintf(&repo_owner_html, repo_owner,
784 gw_trans->gw_dir->owner) == -1) {
785 error = got_error_from_errno("asprintf");
789 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
790 if (kerr != KCGI_OK) {
791 error = gw_kcgi_error(kerr);
797 if (gw_trans->gw_conf->got_show_repo_age) {
798 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
799 "refs/heads", TM_LONG);
802 if (strcmp(age, "") != 0) {
803 if (asprintf(&repo_age_html, last_change, age) == -1) {
804 error = got_error_from_errno("asprintf");
808 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
809 if (kerr != KCGI_OK) {
810 error = gw_kcgi_error(kerr);
816 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
817 if (gw_trans->gw_dir->url != NULL &&
818 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
819 if (asprintf(&cloneurl_html, cloneurl,
820 gw_trans->gw_dir->url) == -1) {
821 error = got_error_from_errno("asprintf");
825 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
826 if (kerr != KCGI_OK) {
827 error = gw_kcgi_error(kerr);
832 kerr = khttp_puts(gw_trans->gw_req, div_end);
833 if (kerr != KCGI_OK) {
834 error = gw_kcgi_error(kerr);
838 error = gw_briefs(gw_trans);
842 tags = gw_get_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP, TAGBRIEF);
843 heads = gw_get_repo_heads(gw_trans);
845 if (tags != NULL && strcmp(tags, "") != 0) {
846 if (asprintf(&tags_html, summary_tags, tags) == -1) {
847 error = got_error_from_errno("asprintf");
850 kerr = khttp_puts(gw_trans->gw_req, tags_html);
851 if (kerr != KCGI_OK) {
852 error = gw_kcgi_error(kerr);
857 if (heads != NULL && strcmp(heads, "") != 0) {
858 if (asprintf(&heads_html, summary_heads, heads) == -1) {
859 error = got_error_from_errno("asprintf");
862 kerr = khttp_puts(gw_trans->gw_req, heads_html);
863 if (kerr != KCGI_OK) {
864 error = gw_kcgi_error(kerr);
869 free(description_html);
870 free(repo_owner_html);
881 static const struct got_error *
882 gw_tree(struct gw_trans *gw_trans)
884 const struct got_error *error = NULL;
885 struct gw_header *header = NULL;
886 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
889 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
890 return got_error_from_errno("pledge");
892 if ((header = gw_init_header()) == NULL)
893 return got_error_from_errno("malloc");
895 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
899 error = gw_get_header(gw_trans, header, 1);
903 tree_html = gw_get_repo_tree(gw_trans);
905 if (tree_html == NULL) {
906 tree_html = strdup("");
907 if (tree_html == NULL) {
908 error = got_error_from_errno("strdup");
913 if (asprintf(&tree_html_disp, tree_header,
914 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
915 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
917 error = got_error_from_errno("asprintf");
921 if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
922 error = got_error_from_errno("asprintf");
926 kerr = khttp_puts(gw_trans->gw_req, tree);
928 error = gw_kcgi_error(kerr);
930 got_ref_list_free(&header->refs);
931 gw_free_headers(header);
932 free(tree_html_disp);
938 static const struct got_error *
939 gw_tag(struct gw_trans *gw_trans)
941 const struct got_error *error = NULL;
942 struct gw_header *header = NULL;
943 char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
946 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
947 return got_error_from_errno("pledge");
949 if ((header = gw_init_header()) == NULL)
950 return got_error_from_errno("malloc");
952 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
956 error = gw_get_header(gw_trans, header, 1);
960 tag_html = gw_get_repo_tags(gw_trans, header, 1, TAGFULL);
961 if (tag_html == NULL) {
962 tag_html = strdup("");
963 if (tag_html == NULL) {
964 error = got_error_from_errno("strdup");
969 if (asprintf(&tag_html_disp, tag_header,
970 gw_gen_commit_header(header->commit_id, header->refs_str),
971 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
973 error = got_error_from_errno("asprintf");
977 if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
978 error = got_error_from_errno("asprintf");
982 kerr = khttp_puts(gw_trans->gw_req, tag);
984 error = gw_kcgi_error(kerr);
986 got_ref_list_free(&header->refs);
987 gw_free_headers(header);
994 static const struct got_error *
995 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
997 const struct got_error *error = NULL;
1002 if (asprintf(&dir_test, "%s/%s/%s",
1003 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1004 GOTWEB_GIT_DIR) == -1)
1005 return got_error_from_errno("asprintf");
1007 dt = opendir(dir_test);
1011 gw_dir->path = strdup(dir_test);
1016 if (asprintf(&dir_test, "%s/%s/%s",
1017 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1018 GOTWEB_GOT_DIR) == -1)
1019 return got_error_from_errno("asprintf");
1021 dt = opendir(dir_test);
1026 error = got_error(GOT_ERR_NOT_GIT_REPO);
1030 if (asprintf(&dir_test, "%s/%s",
1031 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1032 return got_error_from_errno("asprintf");
1034 gw_dir->path = strdup(dir_test);
1037 gw_dir->description = gw_get_repo_description(gw_trans,
1039 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
1040 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1041 "refs/heads", TM_DIFF);
1044 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
1053 static const struct got_error *
1054 gw_load_got_paths(struct gw_trans *gw_trans)
1056 const struct got_error *error = NULL;
1058 struct dirent **sd_dent;
1059 struct gw_dir *gw_dir;
1061 unsigned int d_cnt, d_i;
1063 d = opendir(gw_trans->gw_conf->got_repos_path);
1065 error = got_error_from_errno2("opendir",
1066 gw_trans->gw_conf->got_repos_path);
1070 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1073 error = got_error_from_errno2("scandir",
1074 gw_trans->gw_conf->got_repos_path);
1078 for (d_i = 0; d_i < d_cnt; d_i++) {
1079 if (gw_trans->gw_conf->got_max_repos > 0 &&
1080 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1081 break; /* account for parent and self */
1083 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1084 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1087 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1088 return got_error_from_errno("gw_dir malloc");
1090 error = gw_load_got_path(gw_trans, gw_dir);
1091 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1096 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1097 !got_path_dir_is_empty(gw_dir->path)) {
1098 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1100 gw_trans->repos_total++;
1108 static const struct got_error *
1109 gw_parse_querystring(struct gw_trans *gw_trans)
1111 const struct got_error *error = NULL;
1113 struct gw_query_action *action = NULL;
1116 if (gw_trans->gw_req->fieldnmap[0]) {
1117 error = got_error_from_errno("bad parse");
1119 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1120 /* define gw_trans->repo_path */
1121 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1122 return got_error_from_errno("asprintf");
1124 if (asprintf(&gw_trans->repo_path, "%s/%s",
1125 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1126 return got_error_from_errno("asprintf");
1128 /* get action and set function */
1129 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1130 for (i = 0; i < nitems(gw_query_funcs); i++) {
1131 action = &gw_query_funcs[i];
1132 if (action->func_name == NULL)
1135 if (strcmp(action->func_name,
1136 p->parsed.s) == 0) {
1137 gw_trans->action = i;
1138 if (asprintf(&gw_trans->action_name,
1139 "%s", action->func_name) == -1)
1141 got_error_from_errno(
1150 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1151 if (asprintf(&gw_trans->commit, "%s",
1153 return got_error_from_errno("asprintf");
1155 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1156 if (asprintf(&gw_trans->repo_file, "%s",
1158 return got_error_from_errno("asprintf");
1160 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1161 if (asprintf(&gw_trans->repo_folder, "%s",
1163 return got_error_from_errno("asprintf");
1165 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1166 if (asprintf(&gw_trans->headref, "%s",
1168 return got_error_from_errno("asprintf");
1170 if (action == NULL) {
1171 error = got_error_from_errno("invalid action");
1174 if ((gw_trans->gw_dir =
1175 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1176 return got_error_from_errno("gw_dir malloc");
1178 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1182 gw_trans->action = GW_INDEX;
1184 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1185 gw_trans->page = p->parsed.i;
1190 static struct gw_dir *
1191 gw_init_gw_dir(char *dir)
1193 struct gw_dir *gw_dir;
1195 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1198 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1204 static const struct got_error *
1205 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1209 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1210 if (kerr != KCGI_OK)
1211 return gw_kcgi_error(kerr);
1212 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1214 if (kerr != KCGI_OK)
1215 return gw_kcgi_error(kerr);
1216 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1218 if (kerr != KCGI_OK)
1219 return gw_kcgi_error(kerr);
1220 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1222 if (kerr != KCGI_OK)
1223 return gw_kcgi_error(kerr);
1224 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1225 if (kerr != KCGI_OK)
1226 return gw_kcgi_error(kerr);
1227 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1229 if (kerr != KCGI_OK)
1230 return gw_kcgi_error(kerr);
1232 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1233 kerr = khttp_head(gw_trans->gw_req,
1234 kresps[KRESP_CONTENT_DISPOSITION],
1235 "attachment; filename=%s", gw_trans->repo_file);
1236 if (kerr != KCGI_OK)
1237 return gw_kcgi_error(kerr);
1240 kerr = khttp_body(gw_trans->gw_req);
1241 return gw_kcgi_error(kerr);
1244 static const struct got_error *
1245 gw_display_index(struct gw_trans *gw_trans)
1247 const struct got_error *error;
1250 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1254 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1256 return gw_kcgi_error(kerr);
1258 if (gw_trans->action != GW_BLOB) {
1259 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1260 gw_query_funcs[gw_trans->action].template);
1261 if (kerr != KCGI_OK) {
1262 khtml_close(gw_trans->gw_html_req);
1263 return gw_kcgi_error(kerr);
1267 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1271 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1273 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1276 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1279 khttp_puts(gw_trans->gw_req, err->msg);
1280 khtml_close(gw_trans->gw_html_req);
1284 gw_template(size_t key, void *arg)
1286 const struct got_error *error = NULL;
1287 struct gw_trans *gw_trans = arg;
1288 char *gw_got_link, *gw_site_link;
1289 char *site_owner_name, *site_owner_name_h;
1293 khttp_puts(gw_trans->gw_req, head);
1296 gw_got_link = gw_get_got_link(gw_trans);
1297 if (gw_got_link != NULL)
1298 khttp_puts(gw_trans->gw_req, gw_got_link);
1302 case (TEMPL_SITEPATH):
1303 gw_site_link = gw_get_site_link(gw_trans);
1304 if (gw_site_link != NULL)
1305 khttp_puts(gw_trans->gw_req, gw_site_link);
1310 if (gw_trans->gw_conf->got_site_name != NULL)
1311 khtml_puts(gw_trans->gw_html_req,
1312 gw_trans->gw_conf->got_site_name);
1315 case (TEMPL_SEARCH):
1316 khttp_puts(gw_trans->gw_req, search);
1318 case(TEMPL_SITEOWNER):
1319 if (gw_trans->gw_conf->got_site_owner != NULL &&
1320 gw_trans->gw_conf->got_show_site_owner) {
1322 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1323 if (asprintf(&site_owner_name_h, site_owner,
1324 site_owner_name) == -1)
1327 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1328 free(site_owner_name);
1329 free(site_owner_name_h);
1332 case(TEMPL_CONTENT):
1333 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1335 khttp_puts(gw_trans->gw_req, error->msg);
1344 gw_gen_commit_header(char *str1, char *str2)
1346 char *return_html = NULL, *ref_str = NULL;
1348 if (strcmp(str2, "") != 0) {
1349 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1350 return_html = strdup("");
1354 ref_str = strdup("");
1357 if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1358 return_html = strdup("");
1365 gw_gen_diff_header(char *str1, char *str2)
1367 char *return_html = NULL;
1369 if (asprintf(&return_html, header_diff_html, str1, str2) == -1)
1370 return_html = strdup("");
1376 gw_gen_author_header(char *str)
1378 char *return_html = NULL;
1380 if (asprintf(&return_html, header_author_html, str) == -1)
1381 return_html = strdup("");
1387 gw_gen_committer_header(char *str)
1389 char *return_html = NULL;
1391 if (asprintf(&return_html, header_committer_html, str) == -1)
1392 return_html = strdup("");
1398 gw_gen_age_header(char *str)
1400 char *return_html = NULL;
1402 if (asprintf(&return_html, header_age_html, str) == -1)
1403 return_html = strdup("");
1409 gw_gen_commit_msg_header(char *str)
1411 char *return_html = NULL;
1413 if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1414 return_html = strdup("");
1420 gw_gen_tree_header(char *str)
1422 char *return_html = NULL;
1424 if (asprintf(&return_html, header_tree_html, str) == -1)
1425 return_html = strdup("");
1431 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1434 char *description = NULL, *d_file = NULL;
1437 if (gw_trans->gw_conf->got_show_repo_description == 0)
1440 if (asprintf(&d_file, "%s/description", dir) == -1)
1443 if ((f = fopen(d_file, "r")) == NULL)
1446 fseek(f, 0, SEEK_END);
1448 fseek(f, 0, SEEK_SET);
1449 if ((description = calloc(len, sizeof(char *))) == NULL)
1452 fread(description, 1, len, f);
1461 gw_get_time_str(time_t committer_time, int ref_tm)
1465 char *years = "years ago", *months = "months ago";
1466 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1467 char *minutes = "minutes ago", *seconds = "seconds ago";
1468 char *now = "right now";
1474 diff_time = time(NULL) - committer_time;
1475 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1476 if (asprintf(&repo_age, "%lld %s",
1477 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1479 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1480 if (asprintf(&repo_age, "%lld %s",
1481 (diff_time / 60 / 60 / 24 / (365 / 12)),
1484 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1485 if (asprintf(&repo_age, "%lld %s",
1486 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1488 } else if (diff_time > 60 * 60 * 24 * 2) {
1489 if (asprintf(&repo_age, "%lld %s",
1490 (diff_time / 60 / 60 / 24), days) == -1)
1492 } else if (diff_time > 60 * 60 * 2) {
1493 if (asprintf(&repo_age, "%lld %s",
1494 (diff_time / 60 / 60), hours) == -1)
1496 } else if (diff_time > 60 * 2) {
1497 if (asprintf(&repo_age, "%lld %s", (diff_time / 60),
1500 } else if (diff_time > 2) {
1501 if (asprintf(&repo_age, "%lld %s", diff_time,
1505 if (asprintf(&repo_age, "%s", now) == -1)
1510 if (gmtime_r(&committer_time, &tm) == NULL)
1513 s = asctime_r(&tm, datebuf);
1517 if (asprintf(&repo_age, "%s UTC", datebuf) == -1)
1524 static const struct got_error *
1525 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
1526 char *repo_ref, int ref_tm)
1528 const struct got_error *error = NULL;
1529 struct got_object_id *id = NULL;
1530 struct got_repository *repo = NULL;
1531 struct got_commit_object *commit = NULL;
1532 struct got_reflist_head refs;
1533 struct got_reflist_entry *re;
1534 struct got_reference *head_ref;
1536 time_t committer_time = 0, cmp_time = 0;
1537 const char *refname;
1540 SIMPLEQ_INIT(&refs);
1542 if (repo_ref == NULL)
1545 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1548 if (gw_trans->gw_conf->got_show_repo_age == 0) {
1549 *repo_age = strdup("");
1550 if (*repo_age == NULL)
1551 return got_error_from_errno("strdup");
1555 error = got_repo_open(&repo, dir, NULL);
1560 error = got_ref_list(&refs, repo, "refs/heads",
1561 got_ref_cmp_by_name, NULL);
1563 error = got_ref_list(&refs, repo, repo_ref,
1564 got_ref_cmp_by_name, NULL);
1568 SIMPLEQ_FOREACH(re, &refs, entry) {
1570 refname = strdup(repo_ref);
1572 refname = got_ref_get_name(re->ref);
1573 error = got_ref_open(&head_ref, repo, refname, 0);
1577 error = got_ref_resolve(&id, repo, head_ref);
1578 got_ref_close(head_ref);
1582 error = got_object_open_as_commit(&commit, repo, id);
1587 got_object_commit_get_committer_time(commit);
1589 if (cmp_time < committer_time)
1590 cmp_time = committer_time;
1593 if (cmp_time != 0) {
1594 committer_time = cmp_time;
1595 *repo_age = gw_get_time_str(committer_time, ref_tm);
1597 *repo_age = strdup("");
1598 if (*repo_age == NULL)
1599 error = got_error_from_errno("strdup");
1602 got_ref_list_free(&refs);
1608 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1610 const struct got_error *error;
1612 struct got_object_id *id1 = NULL, *id2 = NULL;
1613 struct buf *diffbuf = NULL;
1614 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL;
1615 char *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1623 error = buf_alloc(&diffbuf, 0);
1627 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1631 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1632 error = got_repo_match_object_id(&id1, &label1,
1633 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1638 error = got_repo_match_object_id(&id2, &label2,
1639 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1643 error = got_object_get_type(&obj_type, header->repo, id2);
1647 case GOT_OBJ_TYPE_BLOB:
1648 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1651 case GOT_OBJ_TYPE_TREE:
1652 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1655 case GOT_OBJ_TYPE_COMMIT:
1656 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1660 error = got_error(GOT_ERR_OBJ_TYPE);
1663 if ((buf = calloc(128, sizeof(char *))) == NULL)
1666 fseek(f, 0, SEEK_SET);
1668 while ((fgets(buf, 2048, f)) != NULL) {
1670 while (*n_buf == '\n')
1672 newline = strchr(n_buf, '\n');
1676 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1677 if (buf_color == NULL)
1680 error = buf_puts(&newsize, diffbuf, buf_color);
1684 error = buf_puts(&newsize, diffbuf, div_end);
1689 if (buf_len(diffbuf) > 0) {
1690 error = buf_putc(diffbuf, '\0');
1691 diff_html = strdup(buf_get(diffbuf));
1710 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1713 char *owner = NULL, *d_file = NULL;
1714 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1715 char *comp, *pos, *buf;
1718 if (gw_trans->gw_conf->got_show_repo_owner == 0)
1721 if (asprintf(&d_file, "%s/config", dir) == -1)
1724 if ((f = fopen(d_file, "r")) == NULL)
1727 if ((buf = calloc(128, sizeof(char *))) == NULL)
1730 while ((fgets(buf, 128, f)) != NULL) {
1731 if ((pos = strstr(buf, gotweb)) != NULL)
1734 if ((pos = strstr(buf, gitweb)) != NULL)
1743 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1748 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1751 for (i = 0; i < 2; i++) {
1752 owner = strsep(&buf, "\"");
1766 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1769 char *url = NULL, *d_file = NULL;
1772 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
1775 if ((f = fopen(d_file, "r")) == NULL)
1778 fseek(f, 0, SEEK_END);
1780 fseek(f, 0, SEEK_SET);
1782 if ((url = calloc(len, sizeof(char *))) == NULL)
1785 fread(url, 1, len, f);
1792 gw_get_repo_tags(struct gw_trans *gw_trans, struct gw_header *header, int limit,
1795 const struct got_error *error = NULL;
1796 struct got_repository *repo = NULL;
1797 struct got_reflist_head refs;
1798 struct got_reflist_entry *re;
1799 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL;
1800 char *age = NULL, *newline;
1801 struct buf *diffbuf = NULL;
1804 SIMPLEQ_INIT(&refs);
1806 error = buf_alloc(&diffbuf, 0);
1810 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1814 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1818 SIMPLEQ_FOREACH(re, &refs, entry) {
1819 const char *refname;
1820 char *refstr, *tag_commit0, *tag_commit, *id_str;
1823 struct got_object_id *id;
1824 struct got_tag_object *tag;
1826 refname = got_ref_get_name(re->ref);
1827 if (strncmp(refname, "refs/tags/", 10) != 0)
1830 refstr = got_ref_to_str(re->ref);
1831 if (refstr == NULL) {
1832 error = got_error_from_errno("got_ref_to_str");
1836 error = got_ref_resolve(&id, repo, re->ref);
1839 error = got_object_open_as_tag(&tag, repo, id);
1844 tagger = got_object_tag_get_tagger(tag);
1845 tagger_time = got_object_tag_get_tagger_time(tag);
1847 error = got_object_id_str(&id_str,
1848 got_object_tag_get_object_id(tag));
1852 tag_commit0 = strdup(got_object_tag_get_message(tag));
1854 if (tag_commit0 == NULL) {
1855 error = got_error_from_errno("strdup");
1859 tag_commit = tag_commit0;
1860 while (*tag_commit == '\n')
1865 newline = strchr(tag_commit, '\n');
1869 if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1871 error = got_error_from_errno("asprintf");
1875 if (asprintf(&tags_navs_disp, tags_navs,
1876 gw_trans->repo_name, id_str, gw_trans->repo_name,
1877 id_str, gw_trans->repo_name, id_str,
1878 gw_trans->repo_name, id_str) == -1) {
1879 error = got_error_from_errno("asprintf");
1883 if (asprintf(&tag_row, tags_row, age, refname,
1884 tag_commit, tags_navs_disp) == -1) {
1885 error = got_error_from_errno("asprintf");
1889 free(tags_navs_disp);
1892 if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1894 error = got_error_from_errno("asprintf");
1897 if (asprintf(&tag_row, tag_info, age,
1898 gw_html_escape(tagger),
1899 gw_html_escape(tag_commit)) == -1) {
1900 error = got_error_from_errno("asprintf");
1908 got_object_tag_close(tag);
1910 error = buf_puts(&newsize, diffbuf, tag_row);
1918 if (error || (limit && --limit == 0))
1922 if (buf_len(diffbuf) > 0) {
1923 error = buf_putc(diffbuf, '\0');
1924 tags = strdup(buf_get(diffbuf));
1928 got_ref_list_free(&refs);
1930 got_repo_close(repo);
1938 gw_free_headers(struct gw_header *header)
1942 if (header->commit != NULL)
1943 got_object_commit_close(header->commit);
1945 got_repo_close(header->repo);
1946 free(header->refs_str);
1947 free(header->commit_id);
1948 free(header->parent_id);
1949 free(header->tree_id);
1950 free(header->author);
1951 free(header->committer);
1952 free(header->commit_msg);
1955 static struct gw_header *
1958 struct gw_header *header;
1960 header = malloc(sizeof(*header));
1964 header->repo = NULL;
1965 header->commit = NULL;
1967 header->path = NULL;
1968 SIMPLEQ_INIT(&header->refs);
1973 static const struct got_error *
1974 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1977 const struct got_error *error = NULL;
1978 struct got_commit_graph *graph = NULL;
1980 error = got_commit_graph_open(&graph, header->path, 0);
1984 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1990 error = got_commit_graph_iter_next(&header->id, graph,
1991 header->repo, NULL, NULL);
1993 if (error->code == GOT_ERR_ITER_COMPLETED)
1997 if (header->id == NULL)
2000 error = got_object_open_as_commit(&header->commit, header->repo,
2005 error = gw_get_commit(gw_trans, header);
2007 struct gw_header *n_header = NULL;
2008 if ((n_header = gw_init_header()) == NULL) {
2009 error = got_error_from_errno("malloc");
2013 n_header->refs_str = strdup(header->refs_str);
2014 n_header->commit_id = strdup(header->commit_id);
2015 n_header->parent_id = strdup(header->parent_id);
2016 n_header->tree_id = strdup(header->tree_id);
2017 n_header->author = strdup(header->author);
2018 n_header->committer = strdup(header->committer);
2019 n_header->commit_msg = strdup(header->commit_msg);
2020 n_header->committer_time = header->committer_time;
2021 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2024 if (error || (limit && --limit == 0))
2029 got_commit_graph_close(graph);
2033 static const struct got_error *
2034 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2036 const struct got_error *error = NULL;
2037 struct got_reflist_entry *re;
2038 struct got_object_id *id2 = NULL;
2039 struct got_object_qid *parent_id;
2040 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2043 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2046 struct got_tag_object *tag = NULL;
2049 name = got_ref_get_name(re->ref);
2050 if (strcmp(name, GOT_REF_HEAD) == 0)
2052 if (strncmp(name, "refs/", 5) == 0)
2054 if (strncmp(name, "got/", 4) == 0)
2056 if (strncmp(name, "heads/", 6) == 0)
2058 if (strncmp(name, "remotes/", 8) == 0)
2060 if (strncmp(name, "tags/", 5) == 0) {
2061 error = got_object_open_as_tag(&tag, header->repo,
2064 if (error->code != GOT_ERR_OBJ_TYPE)
2067 * Ref points at something other
2074 cmp = got_object_id_cmp(tag ?
2075 got_object_tag_get_object_id(tag) : re->id, header->id);
2077 got_object_tag_close(tag);
2081 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2082 s ? ", " : "", name) == -1) {
2083 error = got_error_from_errno("asprintf");
2087 header->refs_str = strdup(refs_str);
2091 if (refs_str == NULL)
2092 header->refs_str = strdup("");
2095 error = got_object_id_str(&header->commit_id, header->id);
2099 error = got_object_id_str(&header->tree_id,
2100 got_object_commit_get_tree_id(header->commit));
2104 if (gw_trans->action == GW_DIFF) {
2105 parent_id = SIMPLEQ_FIRST(
2106 got_object_commit_get_parent_ids(header->commit));
2107 if (parent_id != NULL) {
2108 id2 = got_object_id_dup(parent_id->id);
2110 error = got_object_id_str(&header->parent_id, id2);
2115 header->parent_id = strdup("/dev/null");
2117 header->parent_id = strdup("");
2119 header->committer_time =
2120 got_object_commit_get_committer_time(header->commit);
2122 if (gw_trans->action != GW_BRIEFS && gw_trans->action != GW_SUMMARY) {
2123 header->author = strdup(
2124 gw_html_escape(got_object_commit_get_author(header->commit))
2127 header->author = strdup(
2128 got_object_commit_get_author(header->commit)
2132 header->committer = strdup(
2133 gw_html_escape(got_object_commit_get_committer(header->commit))
2136 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2140 commit_msg = commit_msg0;
2141 while (*commit_msg == '\n')
2144 header->commit_msg = strdup(commit_msg);
2149 static const struct got_error *
2150 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2152 const struct got_error *error = NULL;
2153 char *in_repo_path = NULL;
2155 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2159 if (gw_trans->commit == NULL) {
2160 struct got_reference *head_ref;
2161 error = got_ref_open(&head_ref, header->repo,
2162 gw_trans->headref, 0);
2166 error = got_ref_resolve(&header->id, header->repo, head_ref);
2167 got_ref_close(head_ref);
2171 error = got_object_open_as_commit(&header->commit,
2172 header->repo, header->id);
2174 struct got_reference *ref;
2175 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2176 if (error == NULL) {
2178 error = got_ref_resolve(&header->id, header->repo, ref);
2182 error = got_object_get_type(&obj_type, header->repo,
2186 if (obj_type == GOT_OBJ_TYPE_TAG) {
2187 struct got_tag_object *tag;
2188 error = got_object_open_as_tag(&tag,
2189 header->repo, header->id);
2192 if (got_object_tag_get_object_type(tag) !=
2193 GOT_OBJ_TYPE_COMMIT) {
2194 got_object_tag_close(tag);
2195 error = got_error(GOT_ERR_OBJ_TYPE);
2199 header->id = got_object_id_dup(
2200 got_object_tag_get_object_id(tag));
2201 if (header->id == NULL)
2202 error = got_error_from_errno(
2203 "got_object_id_dup");
2204 got_object_tag_close(tag);
2207 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2208 error = got_error(GOT_ERR_OBJ_TYPE);
2211 error = got_object_open_as_commit(&header->commit,
2212 header->repo, header->id);
2216 if (header->commit == NULL) {
2217 error = got_repo_match_object_id_prefix(&header->id,
2218 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2223 error = got_repo_match_object_id_prefix(&header->id,
2224 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2228 error = got_repo_map_path(&in_repo_path, header->repo,
2229 gw_trans->repo_path, 1);
2234 header->path = strdup(in_repo_path);
2238 error = got_ref_list(&header->refs, header->repo, NULL,
2239 got_ref_cmp_by_name, NULL);
2243 error = gw_get_commits(gw_trans, header, limit);
2251 char datebuf[11]; /* YYYY-MM-DD + NUL */
2254 struct gw_blame_cb_args {
2255 struct blame_line *lines;
2259 off_t *line_offsets;
2261 struct got_repository *repo;
2262 struct gw_trans *gw_trans;
2263 struct buf *blamebuf;
2266 static const struct got_error *
2267 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2269 const struct got_error *err = NULL;
2270 struct gw_blame_cb_args *a = arg;
2271 struct blame_line *bline;
2273 size_t linesize = 0, newsize;
2274 struct got_commit_object *commit = NULL;
2277 time_t committer_time;
2279 if (nlines != a->nlines ||
2280 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2281 return got_error(GOT_ERR_RANGE);
2284 return NULL; /* no change in this commit */
2286 /* Annotate this line. */
2287 bline = &a->lines[lineno - 1];
2288 if (bline->annotated)
2290 err = got_object_id_str(&bline->id_str, id);
2294 err = got_object_open_as_commit(&commit, a->repo, id);
2298 bline->committer = strdup(got_object_commit_get_committer(commit));
2299 if (bline->committer == NULL) {
2300 err = got_error_from_errno("strdup");
2304 committer_time = got_object_commit_get_committer_time(commit);
2305 if (localtime_r(&committer_time, &tm) == NULL)
2306 return got_error_from_errno("localtime_r");
2307 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2308 &tm) >= sizeof(bline->datebuf)) {
2309 err = got_error(GOT_ERR_NO_SPACE);
2312 bline->annotated = 1;
2314 /* Print lines annotated so far. */
2315 bline = &a->lines[a->lineno_cur - 1];
2316 if (!bline->annotated)
2319 offset = a->line_offsets[a->lineno_cur - 1];
2320 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2321 err = got_error_from_errno("fseeko");
2325 while (bline->annotated) {
2326 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2327 *line_escape = NULL;
2330 if (getline(&line, &linesize, a->f) == -1) {
2332 err = got_error_from_errno("getline");
2336 committer = bline->committer;
2337 smallerthan = strchr(committer, '<');
2338 if (smallerthan && smallerthan[1] != '\0')
2339 committer = smallerthan + 1;
2340 at = strchr(committer, '@');
2343 len = strlen(committer);
2345 committer[8] = '\0';
2347 nl = strchr(line, '\n');
2351 if (strcmp(line, "") != 0)
2352 line_escape = strdup(gw_html_escape(line));
2354 line_escape = strdup("");
2356 if (a->gw_trans->repo_folder == NULL)
2357 a->gw_trans->repo_folder = strdup("");
2358 if (a->gw_trans->repo_folder == NULL)
2360 asprintf(&blame_row, blame_line, a->nlines_prec, a->lineno_cur,
2361 a->gw_trans->repo_name, bline->id_str,
2362 a->gw_trans->repo_file, a->gw_trans->repo_folder,
2363 bline->id_str, bline->datebuf, committer, line_escape);
2365 err = buf_puts(&newsize, a->blamebuf, blame_row);
2369 bline = &a->lines[a->lineno_cur - 1];
2376 got_object_commit_close(commit);
2382 gw_get_file_blame_blob(struct gw_trans *gw_trans)
2384 const struct got_error *error = NULL;
2385 struct got_repository *repo = NULL;
2386 struct got_object_id *obj_id = NULL;
2387 struct got_object_id *commit_id = NULL;
2388 struct got_blob_object *blob = NULL;
2389 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL;
2390 char *folder = NULL;
2391 struct gw_blame_cb_args bca;
2396 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2400 if (gw_trans->repo_folder != NULL) {
2401 if (asprintf(&folder, "%s/", gw_trans->repo_folder) == -1) {
2402 error = got_error_from_errno("asprintf");
2406 folder = strdup("");
2408 if (asprintf(&path, "%s%s", folder, gw_trans->repo_file) == -1) {
2409 error = got_error_from_errno("asprintf");
2414 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2418 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2419 GOT_OBJ_TYPE_COMMIT, 1, repo);
2423 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2427 if (obj_id == NULL) {
2428 error = got_error(GOT_ERR_NO_OBJ);
2432 error = got_object_get_type(&obj_type, repo, obj_id);
2436 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2437 error = got_error(GOT_ERR_OBJ_TYPE);
2441 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2445 error = buf_alloc(&bca.blamebuf, 0);
2449 bca.f = got_opentemp();
2450 if (bca.f == NULL) {
2451 error = got_error_from_errno("got_opentemp");
2454 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2455 &bca.line_offsets, bca.f, blob);
2456 if (error || bca.nlines == 0)
2459 if (gw_trans->action == GW_BLOB) {
2460 int len, p, p_check, t = 0, t_check = 50;
2462 fseek(bca.f, 0, SEEK_END);
2463 p_check = len = ftell(bca.f) + 1;
2464 fseek(bca.f, 0, SEEK_SET);
2466 if ((blame_html = calloc(len, sizeof(char *))) == NULL)
2469 fread(blame_html, 1, len, bca.f);
2471 for (p = 0; p < p_check; p++) {
2472 if (isprint(blame_html[p]) == 0)
2473 if (iscntrl(blame_html[p]) == 0)
2478 * Anything over zero is most likely not plain text,
2479 * but let's be sure. Perhaps there's a better way to
2480 * check in the future.
2484 gw_trans->mime = KMIME_APP_OCTET_STREAM;
2486 gw_trans->mime = KMIME_TEXT_PLAIN;
2488 error = gw_display_index(gw_trans);
2490 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2491 kerr = khttp_write(gw_trans->gw_req, blame_html, len);
2492 if (kerr != KCGI_OK)
2493 error = gw_kcgi_error(kerr);
2498 /* Don't include \n at EOF in the blame line count. */
2499 if (bca.line_offsets[bca.nlines - 1] == filesize)
2502 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2503 if (bca.lines == NULL) {
2504 error = got_error_from_errno("calloc");
2508 bca.nlines_prec = 0;
2515 bca.gw_trans = gw_trans;
2517 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2521 if (buf_len(bca.blamebuf) > 0) {
2522 error = buf_putc(bca.blamebuf, '\0');
2523 blame_html = strdup(buf_get(bca.blamebuf));
2526 free(bca.line_offsets);
2533 if (gw_trans->action != GW_BLOB && bca.lines) {
2534 for (i = 0; i < bca.nlines; i++) {
2535 struct blame_line *bline = &bca.lines[i];
2536 free(bline->id_str);
2537 free(bline->committer);
2543 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2546 error = got_object_blob_close(blob);
2550 error = got_repo_close(repo);
2558 gw_get_repo_tree(struct gw_trans *gw_trans)
2560 const struct got_error *error = NULL;
2561 struct got_repository *repo = NULL;
2562 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2563 struct got_tree_object *tree = NULL;
2564 struct buf *diffbuf = NULL;
2566 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2567 *tree_row = NULL, *id_str, *class = NULL;
2568 int nentries, i, class_flip = 0;
2570 error = buf_alloc(&diffbuf, 0);
2574 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2578 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2582 if (gw_trans->repo_folder != NULL)
2583 path = strdup(gw_trans->repo_folder);
2584 else if (in_repo_path) {
2586 path = in_repo_path;
2589 if (gw_trans->commit == NULL) {
2590 struct got_reference *head_ref;
2591 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2595 error = got_ref_resolve(&commit_id, repo, head_ref);
2596 got_ref_close(head_ref);
2599 error = got_repo_match_object_id(&commit_id, NULL,
2600 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2604 error = got_object_id_str(&gw_trans->commit, commit_id);
2608 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2612 error = got_object_open_as_tree(&tree, repo, tree_id);
2616 nentries = got_object_tree_get_nentries(tree);
2618 for (i = 0; i < nentries; i++) {
2619 struct got_tree_entry *te;
2620 const char *modestr = "";
2621 char *id = NULL, *url_html = NULL;
2623 te = got_object_tree_get_entry(tree, i);
2625 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2629 if (asprintf(&id, "%s", id_str) == -1) {
2630 error = got_error_from_errno("asprintf");
2635 mode_t mode = got_tree_entry_get_mode(te);
2637 if (got_object_tree_entry_is_submodule(te))
2639 else if (S_ISLNK(mode))
2641 else if (S_ISDIR(mode))
2643 else if (mode & S_IXUSR)
2646 if (class_flip == 0) {
2647 class = strdup("back_lightgray");
2650 class = strdup("back_white");
2654 char *build_folder = NULL;
2655 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2656 if (gw_trans->repo_folder != NULL) {
2657 if (asprintf(&build_folder, "%s/%s",
2658 gw_trans->repo_folder,
2659 got_tree_entry_get_name(te)) == -1) {
2661 got_error_from_errno("asprintf");
2665 if (asprintf(&build_folder, "/%s",
2666 got_tree_entry_get_name(te)) == -1)
2670 if (asprintf(&url_html, folder_html,
2671 gw_trans->repo_name, gw_trans->action_name,
2672 gw_trans->commit, build_folder,
2673 got_tree_entry_get_name(te), modestr) == -1) {
2674 error = got_error_from_errno("asprintf");
2678 if (asprintf(&tree_row, tree_line, class, url_html,
2680 error = got_error_from_errno("asprintf");
2685 if (gw_trans->repo_folder != NULL) {
2686 if (asprintf(&build_folder, "%s",
2687 gw_trans->repo_folder) == -1) {
2689 got_error_from_errno("asprintf");
2693 build_folder = strdup("");
2695 if (asprintf(&url_html, file_html, gw_trans->repo_name,
2696 "blob", gw_trans->commit,
2697 got_tree_entry_get_name(te), build_folder,
2698 got_tree_entry_get_name(te), modestr) == -1) {
2699 error = got_error_from_errno("asprintf");
2703 if (asprintf(&tree_row, tree_line_with_navs, class,
2704 url_html, class, gw_trans->repo_name, "blob",
2705 gw_trans->commit, got_tree_entry_get_name(te),
2706 build_folder, "blob", gw_trans->repo_name,
2707 "blame", gw_trans->commit,
2708 got_tree_entry_get_name(te), build_folder,
2710 error = got_error_from_errno("asprintf");
2719 error = buf_puts(&newsize, diffbuf, tree_row);
2729 if (buf_len(diffbuf) > 0) {
2730 error = buf_putc(diffbuf, '\0');
2731 tree_html = strdup(buf_get(diffbuf));
2735 got_object_tree_close(tree);
2737 got_repo_close(repo);
2749 gw_get_repo_heads(struct gw_trans *gw_trans)
2751 const struct got_error *error = NULL;
2752 struct got_repository *repo = NULL;
2753 struct got_reflist_head refs;
2754 struct got_reflist_entry *re;
2755 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2756 struct buf *diffbuf = NULL;
2759 SIMPLEQ_INIT(&refs);
2761 error = buf_alloc(&diffbuf, 0);
2765 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2769 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2774 SIMPLEQ_FOREACH(re, &refs, entry) {
2777 refname = strdup(got_ref_get_name(re->ref));
2778 if (refname == NULL) {
2779 error = got_error_from_errno("got_ref_to_str");
2783 if (strncmp(refname, "refs/heads/", 11) != 0) {
2788 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
2793 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2794 refname, gw_trans->repo_name, refname,
2795 gw_trans->repo_name, refname, gw_trans->repo_name,
2797 error = got_error_from_errno("asprintf");
2801 if (strncmp(refname, "refs/heads/", 11) == 0)
2804 if (asprintf(&head_row, heads_row, age, refname,
2805 head_navs_disp) == -1) {
2806 error = got_error_from_errno("asprintf");
2810 error = buf_puts(&newsize, diffbuf, head_row);
2812 free(head_navs_disp);
2816 if (buf_len(diffbuf) > 0) {
2817 error = buf_putc(diffbuf, '\0');
2818 heads = strdup(buf_get(diffbuf));
2822 got_ref_list_free(&refs);
2824 got_repo_close(repo);
2832 gw_get_got_link(struct gw_trans *gw_trans)
2836 if (asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2837 gw_trans->gw_conf->got_logo) == -1)
2844 gw_get_site_link(struct gw_trans *gw_trans)
2846 char *link, *repo = "", *action = "";
2848 if (gw_trans->repo_name != NULL)
2849 if (asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2850 "</a>", gw_trans->repo_name, gw_trans->repo_name) == -1)
2853 if (gw_trans->action_name != NULL)
2854 if (asprintf(&action, " / %s", gw_trans->action_name) == -1)
2857 if (asprintf(&link, site_link, GOTWEB,
2858 gw_trans->gw_conf->got_site_link, repo, action) == -1)
2865 gw_colordiff_line(char *buf)
2867 const struct got_error *error = NULL;
2868 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2869 struct buf *diffbuf = NULL;
2872 error = buf_alloc(&diffbuf, 0);
2878 if (strncmp(buf, "-", 1) == 0)
2879 color = "diff_minus";
2880 if (strncmp(buf, "+", 1) == 0)
2881 color = "diff_plus";
2882 if (strncmp(buf, "@@", 2) == 0)
2883 color = "diff_chunk_header";
2884 if (strncmp(buf, "@@", 2) == 0)
2885 color = "diff_chunk_header";
2886 if (strncmp(buf, "commit +", 8) == 0)
2887 color = "diff_meta";
2888 if (strncmp(buf, "commit -", 8) == 0)
2889 color = "diff_meta";
2890 if (strncmp(buf, "blob +", 6) == 0)
2891 color = "diff_meta";
2892 if (strncmp(buf, "blob -", 6) == 0)
2893 color = "diff_meta";
2894 if (strncmp(buf, "file +", 6) == 0)
2895 color = "diff_meta";
2896 if (strncmp(buf, "file -", 6) == 0)
2897 color = "diff_meta";
2898 if (strncmp(buf, "from:", 5) == 0)
2899 color = "diff_author";
2900 if (strncmp(buf, "via:", 4) == 0)
2901 color = "diff_author";
2902 if (strncmp(buf, "date:", 5) == 0)
2903 color = "diff_date";
2905 if (asprintf(&div_diff_line_div, div_diff_line, color) == -1)
2908 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2912 error = buf_puts(&newsize, diffbuf, buf);
2916 if (buf_len(diffbuf) > 0) {
2917 error = buf_putc(diffbuf, '\0');
2918 colorized_line = strdup(buf_get(diffbuf));
2922 free(div_diff_line_div);
2923 return colorized_line;
2927 gw_html_escape(const char *html)
2929 char *escaped_str = NULL, *buf;
2931 size_t sz, i, buff_sz = 2048;
2933 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2939 if ((sz = strlen(html)) == 0)
2942 /* only work with buff_sz */
2946 for (i = 0; i < sz; i++) {
2950 strcat(buf, ">");
2953 strcat(buf, "&");
2956 strcat(buf, "<");
2959 strcat(buf, """);
2962 strcat(buf, "'");
2965 strcat(buf, "<br />");
2971 asprintf(&escaped_str, "%s", buf);
2977 main(int argc, char *argv[])
2979 const struct got_error *error = NULL;
2980 struct gw_trans *gw_trans;
2981 struct gw_dir *dir = NULL, *tdir;
2982 const char *page = "index";
2986 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2989 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2992 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2995 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2998 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2999 if (kerr != KCGI_OK) {
3000 error = gw_kcgi_error(kerr);
3004 if ((gw_trans->gw_conf =
3005 malloc(sizeof(struct gotweb_conf))) == NULL) {
3007 error = got_error_from_errno("malloc");
3011 TAILQ_INIT(&gw_trans->gw_dirs);
3012 TAILQ_INIT(&gw_trans->gw_headers);
3015 gw_trans->repos_total = 0;
3016 gw_trans->repo_path = NULL;
3017 gw_trans->commit = NULL;
3018 gw_trans->headref = strdup(GOT_REF_HEAD);
3019 gw_trans->mime = KMIME_TEXT_HTML;
3020 gw_trans->gw_tmpl->key = gw_templs;
3021 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3022 gw_trans->gw_tmpl->arg = gw_trans;
3023 gw_trans->gw_tmpl->cb = gw_template;
3024 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3028 error = gw_parse_querystring(gw_trans);
3032 if (gw_trans->action == GW_BLOB)
3033 error = gw_blob(gw_trans);
3035 error = gw_display_index(gw_trans);
3038 gw_trans->mime = KMIME_TEXT_PLAIN;
3039 gw_trans->action = GW_ERR;
3040 gw_display_error(gw_trans, error);
3043 free(gw_trans->gw_conf->got_repos_path);
3044 free(gw_trans->gw_conf->got_site_name);
3045 free(gw_trans->gw_conf->got_site_owner);
3046 free(gw_trans->gw_conf->got_site_link);
3047 free(gw_trans->gw_conf->got_logo);
3048 free(gw_trans->gw_conf->got_logo_url);
3049 free(gw_trans->gw_conf);
3050 free(gw_trans->commit);
3051 free(gw_trans->repo_path);
3052 free(gw_trans->repo_name);
3053 free(gw_trans->repo_file);
3054 free(gw_trans->action_name);
3055 free(gw_trans->headref);
3057 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3059 free(dir->description);
3068 khttp_free(gw_trans->gw_req);