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>
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct gw_dir *gw_dir;
59 struct gotweb_conf *gw_conf;
60 struct ktemplate *gw_tmpl;
61 struct khtmlreq *gw_html_req;
72 unsigned int repos_total;
77 TAILQ_ENTRY(gw_header) entry;
78 struct got_repository *repo;
79 struct got_reflist_head refs;
80 struct got_commit_object *commit;
81 struct got_object_id *id;
85 char *commit_id; /* id_str1 */
86 char *parent_id; /* id_str2 */
89 const char *committer;
91 time_t committer_time;
95 TAILQ_ENTRY(gw_dir) entry;
136 static const char *const gw_templs[TEMPL__MAX] = {
146 static const struct kvalid gw_keys[KEY__ZMAX] = {
147 { kvalid_stringne, "action" },
148 { kvalid_stringne, "commit" },
149 { kvalid_stringne, "file" },
150 { kvalid_stringne, "folder" },
151 { kvalid_stringne, "headref" },
152 { kvalid_int, "page" },
153 { kvalid_stringne, "path" },
156 static struct gw_dir *gw_init_gw_dir(char *);
157 static struct gw_header *gw_init_header(void);
159 static const struct got_error *gw_get_repo_description(char **,
160 struct gw_trans *, char *);
161 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
163 static const struct got_error *gw_get_time_str(char **, time_t, int);
164 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
165 char *, char *, int);
166 static const struct got_error *gw_output_file_blame(struct gw_trans *);
167 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
168 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
169 static const struct got_error *gw_output_diff(struct gw_trans *,
171 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
172 struct gw_header *, int, int);
173 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
174 static const struct got_error *gw_output_site_link(struct gw_trans *);
175 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
177 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
179 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
181 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
183 static const struct got_error *gw_gen_author_header(struct gw_trans *,
185 static const struct got_error *gw_gen_age_header(struct gw_trans *,
187 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
189 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
191 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
193 static void gw_free_headers(struct gw_header *);
194 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
196 static const struct got_error* gw_display_index(struct gw_trans *);
197 static void gw_display_error(struct gw_trans *,
198 const struct got_error *);
200 static int gw_template(size_t, void *);
202 static const struct got_error* gw_get_header(struct gw_trans *,
203 struct gw_header *, int);
204 static const struct got_error* gw_get_commits(struct gw_trans *,
205 struct gw_header *, int);
206 static const struct got_error* gw_get_commit(struct gw_trans *,
208 static const struct got_error* gw_apply_unveil(const char *, const char *);
209 static const struct got_error* gw_blame_cb(void *, int, int,
210 struct got_object_id *);
211 static const struct got_error* gw_load_got_paths(struct gw_trans *);
212 static const struct got_error* gw_load_got_path(struct gw_trans *,
214 static const struct got_error* gw_parse_querystring(struct gw_trans *);
216 static const struct got_error* gw_blame(struct gw_trans *);
217 static const struct got_error* gw_blob(struct gw_trans *);
218 static const struct got_error* gw_diff(struct gw_trans *);
219 static const struct got_error* gw_index(struct gw_trans *);
220 static const struct got_error* gw_commits(struct gw_trans *);
221 static const struct got_error* gw_briefs(struct gw_trans *);
222 static const struct got_error* gw_summary(struct gw_trans *);
223 static const struct got_error* gw_tree(struct gw_trans *);
224 static const struct got_error* gw_tag(struct gw_trans *);
226 struct gw_query_action {
227 unsigned int func_id;
228 const char *func_name;
229 const struct got_error *(*func_main)(struct gw_trans *);
233 enum gw_query_actions {
246 static struct gw_query_action gw_query_funcs[] = {
247 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
248 { GW_BLOB, "blob", NULL, NULL },
249 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
250 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
251 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
252 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
253 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
254 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
255 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
256 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
259 static const struct got_error *
260 gw_kcgi_error(enum kcgi_err kerr)
265 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
266 return got_error(GOT_ERR_CANCELLED);
268 if (kerr == KCGI_ENOMEM)
269 return got_error_set_errno(ENOMEM,
270 kcgi_strerror(kerr));
272 if (kerr == KCGI_ENFILE)
273 return got_error_set_errno(ENFILE,
274 kcgi_strerror(kerr));
276 if (kerr == KCGI_EAGAIN)
277 return got_error_set_errno(EAGAIN,
278 kcgi_strerror(kerr));
280 if (kerr == KCGI_FORM)
281 return got_error_msg(GOT_ERR_IO,
282 kcgi_strerror(kerr));
284 return got_error_from_errno(kcgi_strerror(kerr));
287 static const struct got_error *
288 gw_apply_unveil(const char *repo_path, const char *repo_file)
290 const struct got_error *err;
292 if (repo_path && repo_file) {
294 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
295 return got_error_from_errno("asprintf unveil");
296 if (unveil(full_path, "r") != 0)
297 return got_error_from_errno2("unveil", full_path);
300 if (repo_path && unveil(repo_path, "r") != 0)
301 return got_error_from_errno2("unveil", repo_path);
303 if (unveil("/tmp", "rwc") != 0)
304 return got_error_from_errno2("unveil", "/tmp");
306 err = got_privsep_unveil_exec_helpers();
310 if (unveil(NULL, NULL) != 0)
311 return got_error_from_errno("unveil");
316 static const struct got_error *
317 gw_empty_string(char **s)
321 return got_error_from_errno("strdup");
326 isbinary(const uint8_t *buf, size_t n)
330 for (i = 0; i < n; i++)
336 static const struct got_error *
337 gw_blame(struct gw_trans *gw_trans)
339 const struct got_error *error = NULL;
340 struct gw_header *header = NULL;
344 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
346 return got_error_from_errno("pledge");
348 if ((header = gw_init_header()) == NULL)
349 return got_error_from_errno("malloc");
351 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
355 error = gw_get_header(gw_trans, header, 1);
359 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
360 "blame_header_wrapper", KATTR__MAX);
363 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
364 "blame_header", KATTR__MAX);
367 error = gw_get_time_str(&age, header->committer_time,
371 error = gw_gen_age_header(gw_trans, age ?age : "");
374 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
377 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
380 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
381 "dotted_line", KATTR__MAX);
384 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
388 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
389 "blame", KATTR__MAX);
392 error = gw_output_file_blame(gw_trans);
395 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
397 got_ref_list_free(&header->refs);
398 gw_free_headers(header);
399 if (error == NULL && kerr != KCGI_OK)
400 error = gw_kcgi_error(kerr);
404 static const struct got_error *
405 gw_blob(struct gw_trans *gw_trans)
407 const struct got_error *error = NULL;
408 struct gw_header *header = NULL;
410 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
412 return got_error_from_errno("pledge");
414 if ((header = gw_init_header()) == NULL)
415 return got_error_from_errno("malloc");
417 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
421 error = gw_get_header(gw_trans, header, 1);
425 error = gw_output_blob_buf(gw_trans);
427 got_ref_list_free(&header->refs);
428 gw_free_headers(header);
432 static const struct got_error *
433 gw_diff(struct gw_trans *gw_trans)
435 const struct got_error *error = NULL;
436 struct gw_header *header = NULL;
438 enum kcgi_err kerr = KCGI_OK;
440 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
442 return got_error_from_errno("pledge");
444 if ((header = gw_init_header()) == NULL)
445 return got_error_from_errno("malloc");
447 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
451 error = gw_get_header(gw_trans, header, 1);
455 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
456 "diff_header_wrapper", KATTR__MAX);
459 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
460 "diff_header", KATTR__MAX);
463 error = gw_gen_diff_header(gw_trans, header->parent_id,
467 error = gw_gen_commit_header(gw_trans, header->commit_id,
471 error = gw_gen_tree_header(gw_trans, header->tree_id);
474 error = gw_gen_author_header(gw_trans, header->author);
477 error = gw_gen_committer_header(gw_trans, header->author);
480 error = gw_get_time_str(&age, header->committer_time,
484 error = gw_gen_age_header(gw_trans, age ?age : "");
487 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
490 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
493 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
494 "dotted_line", KATTR__MAX);
497 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
501 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
505 error = gw_output_diff(gw_trans, header);
509 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
513 got_ref_list_free(&header->refs);
514 gw_free_headers(header);
516 if (error == NULL && kerr != KCGI_OK)
517 error = gw_kcgi_error(kerr);
521 static const struct got_error *
522 gw_index(struct gw_trans *gw_trans)
524 const struct got_error *error = NULL;
525 struct gw_dir *gw_dir = NULL;
526 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
527 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
528 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
531 if (pledge("stdio rpath proc exec sendfd unveil",
533 error = got_error_from_errno("pledge");
537 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
541 error = gw_load_got_paths(gw_trans);
545 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
546 "index_header", KATTR__MAX);
548 return gw_kcgi_error(kerr);
549 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
550 "index_header_project", KATTR__MAX);
552 return gw_kcgi_error(kerr);
553 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
555 return gw_kcgi_error(kerr);
556 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
558 return gw_kcgi_error(kerr);
560 if (gw_trans->gw_conf->got_show_repo_description) {
561 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
562 "index_header_description", KATTR__MAX);
564 return gw_kcgi_error(kerr);
565 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
567 return gw_kcgi_error(kerr);
568 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
570 return gw_kcgi_error(kerr);
573 if (gw_trans->gw_conf->got_show_repo_owner) {
574 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
575 "index_header_owner", KATTR__MAX);
577 return gw_kcgi_error(kerr);
578 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
580 return gw_kcgi_error(kerr);
581 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
583 return gw_kcgi_error(kerr);
586 if (gw_trans->gw_conf->got_show_repo_age) {
587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
588 "index_header_age", KATTR__MAX);
590 return gw_kcgi_error(kerr);
591 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
593 return gw_kcgi_error(kerr);
594 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
596 return gw_kcgi_error(kerr);
599 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
601 return gw_kcgi_error(kerr);
603 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
604 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
605 "index_wrapper", KATTR__MAX);
607 return gw_kcgi_error(kerr);
608 kerr = khtml_puts(gw_trans->gw_html_req,
609 "No repositories found in ");
611 return gw_kcgi_error(kerr);
612 kerr = khtml_puts(gw_trans->gw_html_req,
613 gw_trans->gw_conf->got_repos_path);
615 return gw_kcgi_error(kerr);
616 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
618 return gw_kcgi_error(kerr);
619 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
620 "dotted_line", KATTR__MAX);
622 return gw_kcgi_error(kerr);
623 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
625 return gw_kcgi_error(kerr);
629 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
632 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
633 if (gw_trans->page > 0 && (gw_trans->page *
634 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
641 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
642 "index_wrapper", KATTR__MAX);
644 return gw_kcgi_error(kerr);
646 if (asprintf(&href_summary, "?path=%s&action=summary",
648 return got_error_from_errno("asprintf");
649 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
650 "index_project", KATTR__MAX);
653 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
654 href_summary, KATTR__MAX);
657 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
660 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
663 if (gw_trans->gw_conf->got_show_repo_description) {
664 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
665 KATTR_ID, "index_project_description", KATTR__MAX);
668 kerr = khtml_puts(gw_trans->gw_html_req,
669 gw_dir->description ? gw_dir->description : "");
672 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
676 if (gw_trans->gw_conf->got_show_repo_owner) {
677 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
678 KATTR_ID, "index_project_owner", KATTR__MAX);
681 kerr = khtml_puts(gw_trans->gw_html_req,
682 gw_dir->owner ? gw_dir->owner : "");
685 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
689 if (gw_trans->gw_conf->got_show_repo_age) {
690 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
691 KATTR_ID, "index_project_age", KATTR__MAX);
694 kerr = khtml_puts(gw_trans->gw_html_req,
695 gw_dir->age ? gw_dir->age : "");
698 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
703 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
704 "navs_wrapper", KATTR__MAX);
707 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
712 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
713 href_summary, KATTR__MAX);
716 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
719 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
723 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
727 if (asprintf(&href_briefs, "?path=%s&action=briefs",
728 gw_dir->name) == -1) {
729 error = got_error_from_errno("asprintf");
732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
733 href_briefs, KATTR__MAX);
736 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
739 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
741 error = gw_kcgi_error(kerr);
743 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
747 if (asprintf(&href_commits, "?path=%s&action=commits",
748 gw_dir->name) == -1) {
749 error = got_error_from_errno("asprintf");
752 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
753 href_commits, KATTR__MAX);
756 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
759 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
763 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
767 if (asprintf(&href_tree, "?path=%s&action=tree",
768 gw_dir->name) == -1) {
769 error = got_error_from_errno("asprintf");
772 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
773 href_tree, KATTR__MAX);
776 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
780 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
783 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
784 "dotted_line", KATTR__MAX);
787 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
800 if (gw_trans->gw_conf->got_max_repos_display == 0)
803 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
804 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
805 KATTR_ID, "np_wrapper", KATTR__MAX);
808 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
809 KATTR_ID, "nav_prev", KATTR__MAX);
812 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
813 (gw_trans->page > 0) &&
814 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
815 prev_disp == gw_trans->repos_total)) {
816 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
817 KATTR_ID, "np_wrapper", KATTR__MAX);
820 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
821 KATTR_ID, "nav_prev", KATTR__MAX);
826 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
827 (gw_trans->page > 0) &&
828 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
829 prev_disp == gw_trans->repos_total)) {
830 if (asprintf(&href_prev, "?page=%d",
831 gw_trans->page - 1) == -1) {
832 error = got_error_from_errno("asprintf");
835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
836 KATTR_HREF, href_prev, KATTR__MAX);
840 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
843 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
848 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
850 return gw_kcgi_error(kerr);
852 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
853 next_disp == gw_trans->gw_conf->got_max_repos_display &&
854 dir_c != (gw_trans->page + 1) *
855 gw_trans->gw_conf->got_max_repos_display) {
856 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
857 KATTR_ID, "nav_next", KATTR__MAX);
860 if (asprintf(&href_next, "?page=%d",
861 gw_trans->page + 1) == -1) {
862 error = got_error_from_errno("calloc");
865 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
866 KATTR_HREF, href_next, KATTR__MAX);
870 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
873 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
880 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
881 (gw_trans->page > 0) &&
882 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
883 prev_disp == gw_trans->repos_total)) {
884 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
895 if (error == NULL && kerr != KCGI_OK)
896 error = gw_kcgi_error(kerr);
900 static const struct got_error *
901 gw_commits(struct gw_trans *gw_trans)
903 const struct got_error *error = NULL;
904 struct gw_header *header = NULL, *n_header = NULL;
906 char *href_diff = NULL, *href_blob = NULL;
907 enum kcgi_err kerr = KCGI_OK;
909 if ((header = gw_init_header()) == NULL)
910 return got_error_from_errno("malloc");
912 if (pledge("stdio rpath proc exec sendfd unveil",
914 error = got_error_from_errno("pledge");
918 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
922 error = gw_get_header(gw_trans, header,
923 gw_trans->gw_conf->got_max_commits_display);
927 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
928 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
929 "commits_line_wrapper", KATTR__MAX);
932 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
936 error = gw_gen_author_header(gw_trans, n_header->author);
939 error = gw_gen_committer_header(gw_trans, n_header->author);
942 error = gw_get_time_str(&age, n_header->committer_time,
946 error = gw_gen_age_header(gw_trans, age ?age : "");
949 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
953 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
954 "dotted_line", KATTR__MAX);
957 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
961 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
962 "commit", KATTR__MAX);
965 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
968 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
972 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
973 gw_trans->repo_name, n_header->commit_id) == -1) {
974 error = got_error_from_errno("asprintf");
977 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
978 KATTR_ID, "navs_wrapper", KATTR__MAX);
981 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
982 KATTR_ID, "navs", KATTR__MAX);
985 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
986 KATTR_HREF, href_diff, KATTR__MAX);
989 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
992 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
996 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1000 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1001 gw_trans->repo_name, n_header->commit_id) == -1) {
1002 error = got_error_from_errno("asprintf");
1005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1006 KATTR_HREF, href_blob, KATTR__MAX);
1007 if (kerr != KCGI_OK)
1009 khtml_puts(gw_trans->gw_html_req, "tree");
1010 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1011 if (kerr != KCGI_OK)
1013 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1014 if (kerr != KCGI_OK)
1017 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1018 "solid_line", KATTR__MAX);
1019 if (kerr != KCGI_OK)
1021 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1022 if (kerr != KCGI_OK)
1029 got_ref_list_free(&header->refs);
1030 gw_free_headers(header);
1031 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1032 gw_free_headers(n_header);
1036 if (error == NULL && kerr != KCGI_OK)
1037 error = gw_kcgi_error(kerr);
1041 static const struct got_error *
1042 gw_briefs(struct gw_trans *gw_trans)
1044 const struct got_error *error = NULL;
1045 struct gw_header *header = NULL, *n_header = NULL;
1046 char *age = NULL, *age_html = NULL;
1047 char *href_diff = NULL, *href_blob = NULL;
1048 char *newline, *smallerthan;
1049 enum kcgi_err kerr = KCGI_OK;
1051 if ((header = gw_init_header()) == NULL)
1052 return got_error_from_errno("malloc");
1054 if (pledge("stdio rpath proc exec sendfd unveil",
1056 error = got_error_from_errno("pledge");
1060 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1064 if (gw_trans->action == GW_SUMMARY)
1065 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1067 error = gw_get_header(gw_trans, header,
1068 gw_trans->gw_conf->got_max_commits_display);
1072 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1073 error = gw_get_time_str(&age, n_header->committer_time,
1078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1079 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1080 if (kerr != KCGI_OK)
1083 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1084 KATTR_ID, "briefs_age", KATTR__MAX);
1085 if (kerr != KCGI_OK)
1087 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
1088 error = got_error_from_errno("asprintf");
1091 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
1092 if (kerr != KCGI_OK)
1094 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1095 if (kerr != KCGI_OK)
1098 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1099 KATTR_ID, "briefs_author", KATTR__MAX);
1100 if (kerr != KCGI_OK)
1102 smallerthan = strchr(n_header->author, '<');
1104 *smallerthan = '\0';
1105 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1106 if (kerr != KCGI_OK)
1108 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1109 if (kerr != KCGI_OK)
1112 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1113 gw_trans->repo_name, n_header->commit_id) == -1) {
1114 error = got_error_from_errno("asprintf");
1117 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1118 KATTR_ID, "briefs_log", KATTR__MAX);
1119 if (kerr != KCGI_OK)
1121 newline = strchr(n_header->commit_msg, '\n');
1124 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1125 KATTR_HREF, href_diff, KATTR__MAX);
1126 if (kerr != KCGI_OK)
1128 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1129 if (kerr != KCGI_OK)
1131 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1132 if (kerr != KCGI_OK)
1135 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1136 KATTR_ID, "navs_wrapper", KATTR__MAX);
1137 if (kerr != KCGI_OK)
1139 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1140 KATTR_ID, "navs", KATTR__MAX);
1141 if (kerr != KCGI_OK)
1143 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1144 KATTR_HREF, href_diff, KATTR__MAX);
1145 if (kerr != KCGI_OK)
1147 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1148 if (kerr != KCGI_OK)
1150 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1151 if (kerr != KCGI_OK)
1154 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1155 if (kerr != KCGI_OK)
1158 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1159 gw_trans->repo_name, n_header->commit_id) == -1) {
1160 error = got_error_from_errno("asprintf");
1163 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1164 KATTR_HREF, href_blob, KATTR__MAX);
1165 if (kerr != KCGI_OK)
1167 khtml_puts(gw_trans->gw_html_req, "tree");
1168 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1169 if (kerr != KCGI_OK)
1171 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1172 if (kerr != KCGI_OK)
1175 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1176 KATTR_ID, "dotted_line", KATTR__MAX);
1177 if (kerr != KCGI_OK)
1179 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1180 if (kerr != KCGI_OK)
1193 got_ref_list_free(&header->refs);
1194 gw_free_headers(header);
1195 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1196 gw_free_headers(n_header);
1201 if (error == NULL && kerr != KCGI_OK)
1202 error = gw_kcgi_error(kerr);
1206 static const struct got_error *
1207 gw_summary(struct gw_trans *gw_trans)
1209 const struct got_error *error = NULL;
1211 enum kcgi_err kerr = KCGI_OK;
1213 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1214 return got_error_from_errno("pledge");
1216 /* unveil is applied with gw_briefs below */
1218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1219 "summary_wrapper", KATTR__MAX);
1220 if (kerr != KCGI_OK)
1221 return gw_kcgi_error(kerr);
1223 if (gw_trans->gw_conf->got_show_repo_description &&
1224 gw_trans->gw_dir->description != NULL &&
1225 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1226 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1227 KATTR_ID, "description_title", KATTR__MAX);
1228 if (kerr != KCGI_OK)
1230 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1231 if (kerr != KCGI_OK)
1233 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1234 if (kerr != KCGI_OK)
1236 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1237 KATTR_ID, "description", KATTR__MAX);
1238 if (kerr != KCGI_OK)
1240 kerr = khtml_puts(gw_trans->gw_html_req,
1241 gw_trans->gw_dir->description);
1242 if (kerr != KCGI_OK)
1244 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1245 if (kerr != KCGI_OK)
1249 if (gw_trans->gw_conf->got_show_repo_owner &&
1250 gw_trans->gw_dir->owner != NULL) {
1251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1252 KATTR_ID, "repo_owner_title", KATTR__MAX);
1253 if (kerr != KCGI_OK)
1255 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1256 if (kerr != KCGI_OK)
1258 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1259 if (kerr != KCGI_OK)
1261 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1262 KATTR_ID, "repo_owner", KATTR__MAX);
1263 if (kerr != KCGI_OK)
1265 kerr = khtml_puts(gw_trans->gw_html_req,
1266 gw_trans->gw_dir->owner);
1267 if (kerr != KCGI_OK)
1269 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1270 if (kerr != KCGI_OK)
1274 if (gw_trans->gw_conf->got_show_repo_age) {
1275 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1276 "refs/heads", TM_LONG);
1280 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1281 KATTR_ID, "last_change_title", KATTR__MAX);
1282 if (kerr != KCGI_OK)
1284 kerr = khtml_puts(gw_trans->gw_html_req,
1286 if (kerr != KCGI_OK)
1288 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1289 if (kerr != KCGI_OK)
1291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1292 KATTR_ID, "last_change", KATTR__MAX);
1293 if (kerr != KCGI_OK)
1295 kerr = khtml_puts(gw_trans->gw_html_req, age);
1296 if (kerr != KCGI_OK)
1298 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1299 if (kerr != KCGI_OK)
1304 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1305 gw_trans->gw_dir->url != NULL &&
1306 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1307 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1308 KATTR_ID, "cloneurl_title", KATTR__MAX);
1309 if (kerr != KCGI_OK)
1311 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1312 if (kerr != KCGI_OK)
1314 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1315 if (kerr != KCGI_OK)
1317 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1318 KATTR_ID, "cloneurl", KATTR__MAX);
1319 if (kerr != KCGI_OK)
1321 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1322 if (kerr != KCGI_OK)
1324 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1325 if (kerr != KCGI_OK)
1329 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1330 if (kerr != KCGI_OK)
1333 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1334 "briefs_title_wrapper", KATTR__MAX);
1335 if (kerr != KCGI_OK)
1337 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1338 "briefs_title", KATTR__MAX);
1339 if (kerr != KCGI_OK)
1341 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1342 if (kerr != KCGI_OK)
1344 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1345 if (kerr != KCGI_OK)
1347 error = gw_briefs(gw_trans);
1351 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1356 error = gw_output_repo_heads(gw_trans);
1359 if (error == NULL && kerr != KCGI_OK)
1360 error = gw_kcgi_error(kerr);
1364 static const struct got_error *
1365 gw_tree(struct gw_trans *gw_trans)
1367 const struct got_error *error = NULL;
1368 struct gw_header *header = NULL;
1369 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1370 char *age = NULL, *age_html = NULL;
1373 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1374 return got_error_from_errno("pledge");
1376 if ((header = gw_init_header()) == NULL)
1377 return got_error_from_errno("malloc");
1379 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1383 error = gw_get_header(gw_trans, header, 1);
1387 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1388 "tree_header_wrapper", KATTR__MAX);
1389 if (kerr != KCGI_OK)
1391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1392 "tree_header", KATTR__MAX);
1393 if (kerr != KCGI_OK)
1395 error = gw_gen_tree_header(gw_trans, header->tree_id);
1398 error = gw_get_time_str(&age, header->committer_time,
1402 error = gw_gen_age_header(gw_trans, age ?age : "");
1405 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1408 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1409 if (kerr != KCGI_OK)
1411 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1412 "dotted_line", KATTR__MAX);
1413 if (kerr != KCGI_OK)
1415 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1416 if (kerr != KCGI_OK)
1419 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1420 "tree", KATTR__MAX);
1421 if (kerr != KCGI_OK)
1423 error = gw_output_repo_tree(gw_trans);
1427 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1429 got_ref_list_free(&header->refs);
1430 gw_free_headers(header);
1431 free(tree_html_disp);
1436 if (error == NULL && kerr != KCGI_OK)
1437 error = gw_kcgi_error(kerr);
1441 static const struct got_error *
1442 gw_tag(struct gw_trans *gw_trans)
1444 const struct got_error *error = NULL;
1445 struct gw_header *header = NULL;
1448 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1449 return got_error_from_errno("pledge");
1451 if ((header = gw_init_header()) == NULL)
1452 return got_error_from_errno("malloc");
1454 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1458 error = gw_get_header(gw_trans, header, 1);
1462 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1463 "tag_header_wrapper", KATTR__MAX);
1464 if (kerr != KCGI_OK)
1466 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1467 "tag_header", KATTR__MAX);
1468 if (kerr != KCGI_OK)
1470 error = gw_gen_commit_header(gw_trans, header->commit_id,
1474 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1477 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1478 if (kerr != KCGI_OK)
1480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1481 "dotted_line", KATTR__MAX);
1482 if (kerr != KCGI_OK)
1484 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1485 if (kerr != KCGI_OK)
1488 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1489 "tree", KATTR__MAX);
1490 if (kerr != KCGI_OK)
1493 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1497 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1499 got_ref_list_free(&header->refs);
1500 gw_free_headers(header);
1501 if (error == NULL && kerr != KCGI_OK)
1502 error = gw_kcgi_error(kerr);
1506 static const struct got_error *
1507 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1509 const struct got_error *error = NULL;
1514 if (asprintf(&dir_test, "%s/%s/%s",
1515 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1516 GOTWEB_GIT_DIR) == -1)
1517 return got_error_from_errno("asprintf");
1519 dt = opendir(dir_test);
1523 gw_dir->path = strdup(dir_test);
1528 if (asprintf(&dir_test, "%s/%s/%s",
1529 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1530 GOTWEB_GOT_DIR) == -1)
1531 return got_error_from_errno("asprintf");
1533 dt = opendir(dir_test);
1538 error = got_error(GOT_ERR_NOT_GIT_REPO);
1542 if (asprintf(&dir_test, "%s/%s",
1543 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1544 return got_error_from_errno("asprintf");
1546 gw_dir->path = strdup(dir_test);
1548 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1552 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1555 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1556 "refs/heads", TM_DIFF);
1559 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1567 static const struct got_error *
1568 gw_load_got_paths(struct gw_trans *gw_trans)
1570 const struct got_error *error = NULL;
1572 struct dirent **sd_dent;
1573 struct gw_dir *gw_dir;
1575 unsigned int d_cnt, d_i;
1577 d = opendir(gw_trans->gw_conf->got_repos_path);
1579 error = got_error_from_errno2("opendir",
1580 gw_trans->gw_conf->got_repos_path);
1584 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1587 error = got_error_from_errno2("scandir",
1588 gw_trans->gw_conf->got_repos_path);
1592 for (d_i = 0; d_i < d_cnt; d_i++) {
1593 if (gw_trans->gw_conf->got_max_repos > 0 &&
1594 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1595 break; /* account for parent and self */
1597 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1598 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1601 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1602 return got_error_from_errno("gw_dir malloc");
1604 error = gw_load_got_path(gw_trans, gw_dir);
1605 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1612 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1613 !got_path_dir_is_empty(gw_dir->path)) {
1614 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1616 gw_trans->repos_total++;
1624 static const struct got_error *
1625 gw_parse_querystring(struct gw_trans *gw_trans)
1627 const struct got_error *error = NULL;
1629 struct gw_query_action *action = NULL;
1632 if (gw_trans->gw_req->fieldnmap[0]) {
1633 error = got_error_from_errno("bad parse");
1635 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1636 /* define gw_trans->repo_path */
1637 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1638 return got_error_from_errno("asprintf");
1640 if (asprintf(&gw_trans->repo_path, "%s/%s",
1641 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1642 return got_error_from_errno("asprintf");
1644 /* get action and set function */
1645 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1646 for (i = 0; i < nitems(gw_query_funcs); i++) {
1647 action = &gw_query_funcs[i];
1648 if (action->func_name == NULL)
1651 if (strcmp(action->func_name,
1652 p->parsed.s) == 0) {
1653 gw_trans->action = i;
1654 if (asprintf(&gw_trans->action_name,
1655 "%s", action->func_name) == -1)
1657 got_error_from_errno(
1666 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1667 if (asprintf(&gw_trans->commit, "%s",
1669 return got_error_from_errno("asprintf");
1671 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1672 if (asprintf(&gw_trans->repo_file, "%s",
1674 return got_error_from_errno("asprintf");
1676 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1677 if (asprintf(&gw_trans->repo_folder, "%s",
1679 return got_error_from_errno("asprintf");
1681 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1682 if (asprintf(&gw_trans->headref, "%s",
1684 return got_error_from_errno("asprintf");
1686 if (action == NULL) {
1687 error = got_error_from_errno("invalid action");
1690 if ((gw_trans->gw_dir =
1691 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1692 return got_error_from_errno("gw_dir malloc");
1694 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1698 gw_trans->action = GW_INDEX;
1700 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1701 gw_trans->page = p->parsed.i;
1706 static struct gw_dir *
1707 gw_init_gw_dir(char *dir)
1709 struct gw_dir *gw_dir;
1711 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1714 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1720 static const struct got_error *
1721 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1725 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1726 if (kerr != KCGI_OK)
1727 return gw_kcgi_error(kerr);
1728 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1730 if (kerr != KCGI_OK)
1731 return gw_kcgi_error(kerr);
1732 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1734 if (kerr != KCGI_OK)
1735 return gw_kcgi_error(kerr);
1736 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1738 if (kerr != KCGI_OK)
1739 return gw_kcgi_error(kerr);
1740 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1741 if (kerr != KCGI_OK)
1742 return gw_kcgi_error(kerr);
1743 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1745 if (kerr != KCGI_OK)
1746 return gw_kcgi_error(kerr);
1748 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1749 kerr = khttp_head(gw_trans->gw_req,
1750 kresps[KRESP_CONTENT_DISPOSITION],
1751 "attachment; filename=%s", gw_trans->repo_file);
1752 if (kerr != KCGI_OK)
1753 return gw_kcgi_error(kerr);
1756 kerr = khttp_body(gw_trans->gw_req);
1757 return gw_kcgi_error(kerr);
1760 static const struct got_error *
1761 gw_display_index(struct gw_trans *gw_trans)
1763 const struct got_error *error;
1766 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1770 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1771 if (kerr != KCGI_OK)
1772 return gw_kcgi_error(kerr);
1774 if (gw_trans->action != GW_BLOB) {
1775 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1776 gw_query_funcs[gw_trans->action].template);
1777 if (kerr != KCGI_OK) {
1778 khtml_close(gw_trans->gw_html_req);
1779 return gw_kcgi_error(kerr);
1783 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1787 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1789 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1792 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1794 khtml_puts(gw_trans->gw_html_req, err->msg);
1795 khtml_close(gw_trans->gw_html_req);
1799 gw_template(size_t key, void *arg)
1801 const struct got_error *error = NULL;
1803 struct gw_trans *gw_trans = arg;
1804 char *img_src = NULL;
1808 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1809 KATTR_CONTENT, "initial-scale=1.0, user-scalable=no",
1811 if (kerr != KCGI_OK)
1813 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1814 if (kerr != KCGI_OK)
1816 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1817 KATTR_CHARSET, "utf-8",
1819 if (kerr != KCGI_OK)
1821 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1822 if (kerr != KCGI_OK)
1824 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1825 KATTR_NAME, "msapplication-TileColor",
1826 KATTR_CONTENT, "#da532c", KATTR__MAX);
1827 if (kerr != KCGI_OK)
1829 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1830 if (kerr != KCGI_OK)
1832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1833 KATTR_NAME, "theme-color",
1834 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1835 if (kerr != KCGI_OK)
1837 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1838 if (kerr != KCGI_OK)
1840 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1841 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1842 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1843 if (kerr != KCGI_OK)
1845 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1846 if (kerr != KCGI_OK)
1848 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1849 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1850 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1851 if (kerr != KCGI_OK)
1853 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1854 if (kerr != KCGI_OK)
1856 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1857 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1858 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1859 if (kerr != KCGI_OK)
1861 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1862 if (kerr != KCGI_OK)
1864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1865 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1867 if (kerr != KCGI_OK)
1869 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1870 if (kerr != KCGI_OK)
1872 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1873 KATTR_REL, "mask-icon", KATTR_HREF,
1874 "/safari-pinned-tab.svg", KATTR__MAX);
1875 if (kerr != KCGI_OK)
1877 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1878 if (kerr != KCGI_OK)
1880 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1881 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1882 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1883 if (kerr != KCGI_OK)
1885 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1886 if (kerr != KCGI_OK)
1890 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1891 KATTR_ID, "got_link", KATTR__MAX);
1892 if (kerr != KCGI_OK)
1894 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1895 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1896 KATTR_TARGET, "_sotd", KATTR__MAX);
1897 if (kerr != KCGI_OK)
1899 if (asprintf(&img_src, "/%s",
1900 gw_trans->gw_conf->got_logo) == -1)
1902 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1903 KATTR_SRC, img_src, KATTR__MAX);
1904 if (kerr != KCGI_OK) {
1908 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1909 if (kerr != KCGI_OK) {
1914 case (TEMPL_SITEPATH):
1915 error = gw_output_site_link(gw_trans);
1920 if (gw_trans->gw_conf->got_site_name != NULL) {
1921 kerr = khtml_puts(gw_trans->gw_html_req,
1922 gw_trans->gw_conf->got_site_name);
1923 if (kerr != KCGI_OK)
1927 case (TEMPL_SEARCH):
1929 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1930 "search", KATTR__MAX);
1931 if (kerr != KCGI_OK)
1933 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1934 KATTR_METHOD, "POST", KATTR__MAX);
1935 if (kerr != KCGI_OK)
1937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1938 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1939 KATTR_MAXLENGTH, "50", KATTR__MAX);
1940 if (kerr != KCGI_OK)
1942 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1944 if (kerr != KCGI_OK)
1946 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1947 if (kerr != KCGI_OK)
1949 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1950 if (kerr != KCGI_OK)
1953 case(TEMPL_SITEOWNER):
1954 if (gw_trans->gw_conf->got_site_owner != NULL &&
1955 gw_trans->gw_conf->got_show_site_owner) {
1956 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1957 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1958 if (kerr != KCGI_OK)
1960 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1961 KATTR_ID, "site_owner", KATTR__MAX);
1962 if (kerr != KCGI_OK)
1964 kerr = khtml_puts(gw_trans->gw_html_req,
1965 gw_trans->gw_conf->got_site_owner);
1966 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1967 if (kerr != KCGI_OK)
1971 case(TEMPL_CONTENT):
1972 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1974 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1975 KATTR_ID, "tmpl_err", KATTR__MAX);
1976 if (kerr != KCGI_OK)
1978 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1979 if (kerr != KCGI_OK)
1981 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1982 if (kerr != KCGI_OK)
1984 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1985 if (kerr != KCGI_OK)
1995 static const struct got_error *
1996 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1998 const struct got_error *error = NULL;
1999 char *ref_str = NULL;
2000 enum kcgi_err kerr = KCGI_OK;
2002 if (strcmp(str2, "") != 0) {
2003 if (asprintf(&ref_str, "(%s)", str2) == -1) {
2004 error = got_error_from_errno("asprintf");
2008 ref_str = strdup("");
2010 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2011 KATTR_ID, "header_commit_title", KATTR__MAX);
2012 if (kerr != KCGI_OK)
2014 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2015 if (kerr != KCGI_OK)
2017 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2018 if (kerr != KCGI_OK)
2020 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2021 KATTR_ID, "header_commit", KATTR__MAX);
2022 if (kerr != KCGI_OK)
2024 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2025 if (kerr != KCGI_OK)
2027 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2028 if (kerr != KCGI_OK)
2030 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
2031 if (kerr != KCGI_OK)
2033 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2035 if (error == NULL && kerr != KCGI_OK)
2036 error = gw_kcgi_error(kerr);
2040 static const struct got_error *
2041 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2043 const struct got_error *error = NULL;
2044 enum kcgi_err kerr = KCGI_OK;
2046 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2047 KATTR_ID, "header_diff_title", KATTR__MAX);
2048 if (kerr != KCGI_OK)
2050 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2051 if (kerr != KCGI_OK)
2053 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2054 if (kerr != KCGI_OK)
2056 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2057 KATTR_ID, "header_diff", KATTR__MAX);
2058 if (kerr != KCGI_OK)
2060 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2061 if (kerr != KCGI_OK)
2063 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2064 if (kerr != KCGI_OK)
2066 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2067 if (kerr != KCGI_OK)
2069 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2071 if (error == NULL && kerr != KCGI_OK)
2072 error = gw_kcgi_error(kerr);
2076 static const struct got_error *
2077 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2079 const struct got_error *error = NULL;
2080 enum kcgi_err kerr = KCGI_OK;
2082 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2083 KATTR_ID, "header_age_title", KATTR__MAX);
2084 if (kerr != KCGI_OK)
2086 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2087 if (kerr != KCGI_OK)
2089 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2090 if (kerr != KCGI_OK)
2092 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2093 KATTR_ID, "header_age", KATTR__MAX);
2094 if (kerr != KCGI_OK)
2096 kerr = khtml_puts(gw_trans->gw_html_req, str);
2097 if (kerr != KCGI_OK)
2099 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2101 if (error == NULL && kerr != KCGI_OK)
2102 error = gw_kcgi_error(kerr);
2106 static const struct got_error *
2107 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2109 const struct got_error *error = NULL;
2112 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2113 KATTR_ID, "header_author_title", KATTR__MAX);
2114 if (kerr != KCGI_OK)
2116 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2117 if (kerr != KCGI_OK)
2119 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2120 if (kerr != KCGI_OK)
2122 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2123 KATTR_ID, "header_author", KATTR__MAX);
2124 if (kerr != KCGI_OK)
2126 kerr = khtml_puts(gw_trans->gw_html_req, str);
2127 if (kerr != KCGI_OK)
2129 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2131 if (error == NULL && kerr != KCGI_OK)
2132 error = gw_kcgi_error(kerr);
2136 static const struct got_error *
2137 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2139 const struct got_error *error = NULL;
2142 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2143 KATTR_ID, "header_committer_title", KATTR__MAX);
2144 if (kerr != KCGI_OK)
2146 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2147 if (kerr != KCGI_OK)
2149 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2150 if (kerr != KCGI_OK)
2152 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2153 KATTR_ID, "header_committer", KATTR__MAX);
2154 if (kerr != KCGI_OK)
2156 kerr = khtml_puts(gw_trans->gw_html_req, str);
2157 if (kerr != KCGI_OK)
2159 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2161 if (error == NULL && kerr != KCGI_OK)
2162 error = gw_kcgi_error(kerr);
2166 static const struct got_error *
2167 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2169 const struct got_error *error = NULL;
2172 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2173 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2174 if (kerr != KCGI_OK)
2176 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2177 if (kerr != KCGI_OK)
2179 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2180 if (kerr != KCGI_OK)
2182 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2183 KATTR_ID, "header_commit_msg", KATTR__MAX);
2184 if (kerr != KCGI_OK)
2186 kerr = khttp_puts(gw_trans->gw_req, str);
2187 if (kerr != KCGI_OK)
2189 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2191 if (error == NULL && kerr != KCGI_OK)
2192 error = gw_kcgi_error(kerr);
2196 static const struct got_error *
2197 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2199 const struct got_error *error = NULL;
2202 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2203 KATTR_ID, "header_tree_title", KATTR__MAX);
2204 if (kerr != KCGI_OK)
2206 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2207 if (kerr != KCGI_OK)
2209 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2210 if (kerr != KCGI_OK)
2212 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2213 KATTR_ID, "header_tree", KATTR__MAX);
2214 if (kerr != KCGI_OK)
2216 kerr = khtml_puts(gw_trans->gw_html_req, str);
2217 if (kerr != KCGI_OK)
2219 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2221 if (error == NULL && kerr != KCGI_OK)
2222 error = gw_kcgi_error(kerr);
2226 static const struct got_error *
2227 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2230 const struct got_error *error = NULL;
2232 char *d_file = NULL;
2236 *description = NULL;
2237 if (gw_trans->gw_conf->got_show_repo_description == 0)
2238 return gw_empty_string(description);
2240 if (asprintf(&d_file, "%s/description", dir) == -1)
2241 return got_error_from_errno("asprintf");
2243 f = fopen(d_file, "r");
2245 if (errno == ENOENT || errno == EACCES)
2246 return gw_empty_string(description);
2247 error = got_error_from_errno2("fopen", d_file);
2251 if (fseek(f, 0, SEEK_END) == -1) {
2252 error = got_ferror(f, GOT_ERR_IO);
2257 error = got_ferror(f, GOT_ERR_IO);
2260 if (fseek(f, 0, SEEK_SET) == -1) {
2261 error = got_ferror(f, GOT_ERR_IO);
2264 *description = calloc(len + 1, sizeof(**description));
2265 if (*description == NULL) {
2266 error = got_error_from_errno("calloc");
2270 n = fread(*description, 1, len, f);
2271 if (n == 0 && ferror(f))
2272 error = got_ferror(f, GOT_ERR_IO);
2274 if (f != NULL && fclose(f) == -1 && error == NULL)
2275 error = got_error_from_errno("fclose");
2280 static const struct got_error *
2281 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2285 char *years = "years ago", *months = "months ago";
2286 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2287 char *minutes = "minutes ago", *seconds = "seconds ago";
2288 char *now = "right now";
2296 diff_time = time(NULL) - committer_time;
2297 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2298 if (asprintf(repo_age, "%lld %s",
2299 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2300 return got_error_from_errno("asprintf");
2301 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2302 if (asprintf(repo_age, "%lld %s",
2303 (diff_time / 60 / 60 / 24 / (365 / 12)),
2305 return got_error_from_errno("asprintf");
2306 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2307 if (asprintf(repo_age, "%lld %s",
2308 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2309 return got_error_from_errno("asprintf");
2310 } else if (diff_time > 60 * 60 * 24 * 2) {
2311 if (asprintf(repo_age, "%lld %s",
2312 (diff_time / 60 / 60 / 24), days) == -1)
2313 return got_error_from_errno("asprintf");
2314 } else if (diff_time > 60 * 60 * 2) {
2315 if (asprintf(repo_age, "%lld %s",
2316 (diff_time / 60 / 60), hours) == -1)
2317 return got_error_from_errno("asprintf");
2318 } else if (diff_time > 60 * 2) {
2319 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2321 return got_error_from_errno("asprintf");
2322 } else if (diff_time > 2) {
2323 if (asprintf(repo_age, "%lld %s", diff_time,
2325 return got_error_from_errno("asprintf");
2327 if (asprintf(repo_age, "%s", now) == -1)
2328 return got_error_from_errno("asprintf");
2332 if (gmtime_r(&committer_time, &tm) == NULL)
2333 return got_error_from_errno("gmtime_r");
2335 s = asctime_r(&tm, datebuf);
2337 return got_error_from_errno("asctime_r");
2339 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2340 return got_error_from_errno("asprintf");
2346 static const struct got_error *
2347 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2348 char *repo_ref, int ref_tm)
2350 const struct got_error *error = NULL;
2351 struct got_object_id *id = NULL;
2352 struct got_repository *repo = NULL;
2353 struct got_commit_object *commit = NULL;
2354 struct got_reflist_head refs;
2355 struct got_reflist_entry *re;
2356 struct got_reference *head_ref;
2358 time_t committer_time = 0, cmp_time = 0;
2359 const char *refname;
2362 SIMPLEQ_INIT(&refs);
2364 if (repo_ref == NULL)
2367 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2370 if (gw_trans->gw_conf->got_show_repo_age == 0)
2373 error = got_repo_open(&repo, dir, NULL);
2378 error = got_ref_list(&refs, repo, "refs/heads",
2379 got_ref_cmp_by_name, NULL);
2381 error = got_ref_list(&refs, repo, repo_ref,
2382 got_ref_cmp_by_name, NULL);
2386 SIMPLEQ_FOREACH(re, &refs, entry) {
2388 refname = strdup(repo_ref);
2390 refname = got_ref_get_name(re->ref);
2391 error = got_ref_open(&head_ref, repo, refname, 0);
2395 error = got_ref_resolve(&id, repo, head_ref);
2396 got_ref_close(head_ref);
2400 error = got_object_open_as_commit(&commit, repo, id);
2405 got_object_commit_get_committer_time(commit);
2407 if (cmp_time < committer_time)
2408 cmp_time = committer_time;
2411 if (cmp_time != 0) {
2412 committer_time = cmp_time;
2413 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2416 got_ref_list_free(&refs);
2421 static const struct got_error *
2422 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2424 const struct got_error *error;
2426 struct got_object_id *id1 = NULL, *id2 = NULL;
2427 char *label1 = NULL, *label2 = NULL, *line = NULL;
2429 size_t linesize = 0;
2431 enum kcgi_err kerr = KCGI_OK;
2437 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2441 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2442 error = got_repo_match_object_id(&id1, &label1,
2443 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2448 error = got_repo_match_object_id(&id2, &label2,
2449 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2453 error = got_object_get_type(&obj_type, header->repo, id2);
2457 case GOT_OBJ_TYPE_BLOB:
2458 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2461 case GOT_OBJ_TYPE_TREE:
2462 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2465 case GOT_OBJ_TYPE_COMMIT:
2466 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2470 error = got_error(GOT_ERR_OBJ_TYPE);
2475 if (fseek(f, 0, SEEK_SET) == -1) {
2476 error = got_ferror(f, GOT_ERR_IO);
2480 while ((linelen = getline(&line, &linesize, f)) != -1) {
2481 error = gw_colordiff_line(gw_trans, line);
2484 /* XXX: KHTML_PRETTY breaks this */
2485 kerr = khtml_puts(gw_trans->gw_html_req, line);
2486 if (kerr != KCGI_OK)
2488 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2489 if (kerr != KCGI_OK)
2492 if (linelen == -1 && ferror(f))
2493 error = got_error_from_errno("getline");
2495 if (f && fclose(f) == -1 && error == NULL)
2496 error = got_error_from_errno("fclose");
2503 if (error == NULL && kerr != KCGI_OK)
2504 error = gw_kcgi_error(kerr);
2508 static const struct got_error *
2509 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2511 const struct got_error *error = NULL;
2512 struct got_repository *repo;
2513 const char *gitconfig_owner;
2517 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2520 error = got_repo_open(&repo, dir, NULL);
2523 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2524 if (gitconfig_owner) {
2525 *owner = strdup(gitconfig_owner);
2527 error = got_error_from_errno("strdup");
2529 got_repo_close(repo);
2533 static const struct got_error *
2534 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2536 const struct got_error *error = NULL;
2538 char *d_file = NULL;
2544 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2545 return got_error_from_errno("asprintf");
2547 f = fopen(d_file, "r");
2549 if (errno != ENOENT && errno != EACCES)
2550 error = got_error_from_errno2("fopen", d_file);
2554 if (fseek(f, 0, SEEK_END) == -1) {
2555 error = got_ferror(f, GOT_ERR_IO);
2560 error = got_ferror(f, GOT_ERR_IO);
2563 if (fseek(f, 0, SEEK_SET) == -1) {
2564 error = got_ferror(f, GOT_ERR_IO);
2568 *url = calloc(len + 1, sizeof(**url));
2570 error = got_error_from_errno("calloc");
2574 n = fread(*url, 1, len, f);
2575 if (n == 0 && ferror(f))
2576 error = got_ferror(f, GOT_ERR_IO);
2578 if (f && fclose(f) == -1 && error == NULL)
2579 error = got_error_from_errno("fclose");
2584 static const struct got_error *
2585 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2586 int limit, int tag_type)
2588 const struct got_error *error = NULL;
2589 struct got_repository *repo = NULL;
2590 struct got_reflist_head refs;
2591 struct got_reflist_entry *re;
2593 char *id_str = NULL, *newline, *href_commits = NULL;
2594 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2595 struct got_tag_object *tag = NULL;
2596 enum kcgi_err kerr = KCGI_OK;
2597 int summary_header_displayed = 0;
2599 SIMPLEQ_INIT(&refs);
2601 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2605 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2609 SIMPLEQ_FOREACH(re, &refs, entry) {
2610 const char *refname;
2612 const char *tag_commit;
2614 struct got_object_id *id;
2615 struct got_commit_object *commit = NULL;
2617 refname = got_ref_get_name(re->ref);
2618 if (strncmp(refname, "refs/tags/", 10) != 0)
2622 error = got_ref_resolve(&id, repo, re->ref);
2626 error = got_object_open_as_tag(&tag, repo, id);
2628 if (error->code != GOT_ERR_OBJ_TYPE) {
2632 /* "lightweight" tag */
2633 error = got_object_open_as_commit(&commit, repo, id);
2638 tagger = got_object_commit_get_committer(commit);
2640 got_object_commit_get_committer_time(commit);
2641 error = got_object_id_str(&id_str, id);
2647 tagger = got_object_tag_get_tagger(tag);
2648 tagger_time = got_object_tag_get_tagger_time(tag);
2649 error = got_object_id_str(&id_str,
2650 got_object_tag_get_object_id(tag));
2657 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2658 strlen(id_str)) != 0)
2662 error = got_object_commit_get_logmsg(&tag_commit0,
2666 got_object_commit_close(commit);
2668 tag_commit0 = strdup(got_object_tag_get_message(tag));
2669 if (tag_commit0 == NULL) {
2670 error = got_error_from_errno("strdup");
2675 tag_commit = tag_commit0;
2676 while (*tag_commit == '\n')
2681 newline = strchr(tag_commit, '\n');
2685 if (summary_header_displayed == 0) {
2686 kerr = khtml_attr(gw_trans->gw_html_req,
2687 KELEM_DIV, KATTR_ID,
2688 "summary_tags_title_wrapper", KATTR__MAX);
2689 if (kerr != KCGI_OK)
2691 kerr = khtml_attr(gw_trans->gw_html_req,
2692 KELEM_DIV, KATTR_ID,
2693 "summary_tags_title", KATTR__MAX);
2694 if (kerr != KCGI_OK)
2696 kerr = khtml_puts(gw_trans->gw_html_req,
2698 if (kerr != KCGI_OK)
2700 kerr = khtml_closeelem(gw_trans->gw_html_req,
2702 if (kerr != KCGI_OK)
2704 kerr = khtml_attr(gw_trans->gw_html_req,
2705 KELEM_DIV, KATTR_ID,
2706 "summary_tags_content", KATTR__MAX);
2707 if (kerr != KCGI_OK)
2709 summary_header_displayed = 1;
2712 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2713 KATTR_ID, "tags_wrapper", KATTR__MAX);
2714 if (kerr != KCGI_OK)
2716 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2717 KATTR_ID, "tags_age", KATTR__MAX);
2718 if (kerr != KCGI_OK)
2720 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2723 kerr = khtml_puts(gw_trans->gw_html_req,
2725 if (kerr != KCGI_OK)
2727 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2728 if (kerr != KCGI_OK)
2730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2731 KATTR_ID, "tags", KATTR__MAX);
2732 if (kerr != KCGI_OK)
2734 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2735 if (kerr != KCGI_OK)
2737 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2738 if (kerr != KCGI_OK)
2740 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2741 KATTR_ID, "tags_name", KATTR__MAX);
2742 if (kerr != KCGI_OK)
2744 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2745 gw_trans->repo_name, id_str) == -1) {
2746 error = got_error_from_errno("asprintf");
2749 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2750 KATTR_HREF, href_tag, KATTR__MAX);
2751 if (kerr != KCGI_OK)
2753 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2754 if (kerr != KCGI_OK)
2756 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2757 if (kerr != KCGI_OK)
2760 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2761 KATTR_ID, "navs_wrapper", KATTR__MAX);
2762 if (kerr != KCGI_OK)
2764 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2765 KATTR_ID, "navs", KATTR__MAX);
2766 if (kerr != KCGI_OK)
2769 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2770 KATTR_HREF, href_tag, KATTR__MAX);
2771 if (kerr != KCGI_OK)
2773 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2774 if (kerr != KCGI_OK)
2776 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2777 if (kerr != KCGI_OK)
2780 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2781 if (kerr != KCGI_OK)
2783 if (asprintf(&href_briefs,
2784 "?path=%s&action=briefs&commit=%s",
2785 gw_trans->repo_name, id_str) == -1) {
2786 error = got_error_from_errno("asprintf");
2789 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2790 KATTR_HREF, href_briefs, KATTR__MAX);
2791 if (kerr != KCGI_OK)
2793 kerr = khtml_puts(gw_trans->gw_html_req,
2795 if (kerr != KCGI_OK)
2797 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2798 if (kerr != KCGI_OK)
2801 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2802 if (kerr != KCGI_OK)
2805 if (asprintf(&href_commits,
2806 "?path=%s&action=commits&commit=%s",
2807 gw_trans->repo_name, id_str) == -1) {
2808 error = got_error_from_errno("asprintf");
2811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2812 KATTR_HREF, href_commits, KATTR__MAX);
2813 if (kerr != KCGI_OK)
2815 kerr = khtml_puts(gw_trans->gw_html_req,
2817 if (kerr != KCGI_OK)
2819 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2820 if (kerr != KCGI_OK)
2823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2824 KATTR_ID, "dotted_line", KATTR__MAX);
2825 if (kerr != KCGI_OK)
2827 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2828 if (kerr != KCGI_OK)
2832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2833 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2834 if (kerr != KCGI_OK)
2836 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2837 if (kerr != KCGI_OK)
2839 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2840 if (kerr != KCGI_OK)
2842 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2843 KATTR_ID, "tag_info_date", KATTR__MAX);
2844 if (kerr != KCGI_OK)
2846 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2849 kerr = khtml_puts(gw_trans->gw_html_req,
2851 if (kerr != KCGI_OK)
2853 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2854 if (kerr != KCGI_OK)
2857 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2858 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2859 if (kerr != KCGI_OK)
2861 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2862 if (kerr != KCGI_OK)
2864 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2865 if (kerr != KCGI_OK)
2867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2868 KATTR_ID, "tag_info_date", KATTR__MAX);
2869 if (kerr != KCGI_OK)
2871 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2872 if (kerr != KCGI_OK)
2874 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2875 if (kerr != KCGI_OK)
2878 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2879 KATTR_ID, "tag_info", KATTR__MAX);
2880 if (kerr != KCGI_OK)
2882 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2883 if (kerr != KCGI_OK)
2889 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2890 if (kerr != KCGI_OK)
2893 if (limit && --limit == 0)
2897 got_object_tag_close(tag);
2910 href_commits = NULL;
2914 got_object_tag_close(tag);
2921 got_ref_list_free(&refs);
2923 got_repo_close(repo);
2924 if (error == NULL && kerr != KCGI_OK)
2925 error = gw_kcgi_error(kerr);
2930 gw_free_headers(struct gw_header *header)
2934 if (header->commit != NULL)
2935 got_object_commit_close(header->commit);
2937 got_repo_close(header->repo);
2938 free(header->refs_str);
2939 free(header->commit_id);
2940 free(header->parent_id);
2941 free(header->tree_id);
2942 free(header->commit_msg);
2945 static struct gw_header *
2948 struct gw_header *header;
2950 header = malloc(sizeof(*header));
2954 header->repo = NULL;
2955 header->commit = NULL;
2957 header->path = NULL;
2958 SIMPLEQ_INIT(&header->refs);
2963 static const struct got_error *
2964 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2967 const struct got_error *error = NULL;
2968 struct got_commit_graph *graph = NULL;
2970 error = got_commit_graph_open(&graph, header->path, 0);
2974 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2980 error = got_commit_graph_iter_next(&header->id, graph,
2981 header->repo, NULL, NULL);
2983 if (error->code == GOT_ERR_ITER_COMPLETED)
2987 if (header->id == NULL)
2990 error = got_object_open_as_commit(&header->commit, header->repo,
2995 error = gw_get_commit(gw_trans, header);
2997 struct gw_header *n_header = NULL;
2998 if ((n_header = gw_init_header()) == NULL) {
2999 error = got_error_from_errno("malloc");
3003 n_header->refs_str = strdup(header->refs_str);
3004 n_header->commit_id = strdup(header->commit_id);
3005 n_header->parent_id = strdup(header->parent_id);
3006 n_header->tree_id = strdup(header->tree_id);
3007 n_header->author = strdup(header->author);
3008 n_header->committer = strdup(header->committer);
3009 n_header->commit_msg = strdup(header->commit_msg);
3010 n_header->committer_time = header->committer_time;
3011 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3014 if (error || (limit && --limit == 0))
3019 got_commit_graph_close(graph);
3023 static const struct got_error *
3024 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3026 const struct got_error *error = NULL;
3027 struct got_reflist_entry *re;
3028 struct got_object_id *id2 = NULL;
3029 struct got_object_qid *parent_id;
3030 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
3033 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3036 struct got_tag_object *tag = NULL;
3039 name = got_ref_get_name(re->ref);
3040 if (strcmp(name, GOT_REF_HEAD) == 0)
3042 if (strncmp(name, "refs/", 5) == 0)
3044 if (strncmp(name, "got/", 4) == 0)
3046 if (strncmp(name, "heads/", 6) == 0)
3048 if (strncmp(name, "remotes/", 8) == 0)
3050 if (strncmp(name, "tags/", 5) == 0) {
3051 error = got_object_open_as_tag(&tag, header->repo,
3054 if (error->code != GOT_ERR_OBJ_TYPE)
3057 * Ref points at something other
3064 cmp = got_object_id_cmp(tag ?
3065 got_object_tag_get_object_id(tag) : re->id, header->id);
3067 got_object_tag_close(tag);
3071 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
3072 s ? ", " : "", name) == -1) {
3073 error = got_error_from_errno("asprintf");
3077 header->refs_str = strdup(refs_str);
3081 if (refs_str == NULL)
3082 header->refs_str = strdup("");
3085 error = got_object_id_str(&header->commit_id, header->id);
3089 error = got_object_id_str(&header->tree_id,
3090 got_object_commit_get_tree_id(header->commit));
3094 if (gw_trans->action == GW_DIFF) {
3095 parent_id = SIMPLEQ_FIRST(
3096 got_object_commit_get_parent_ids(header->commit));
3097 if (parent_id != NULL) {
3098 id2 = got_object_id_dup(parent_id->id);
3100 error = got_object_id_str(&header->parent_id, id2);
3105 header->parent_id = strdup("/dev/null");
3107 header->parent_id = strdup("");
3109 header->committer_time =
3110 got_object_commit_get_committer_time(header->commit);
3113 got_object_commit_get_author(header->commit);
3115 got_object_commit_get_committer(header->commit);
3118 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3122 commit_msg = commit_msg0;
3123 while (*commit_msg == '\n')
3126 header->commit_msg = strdup(commit_msg);
3131 static const struct got_error *
3132 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3134 const struct got_error *error = NULL;
3135 char *in_repo_path = NULL;
3137 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3141 if (gw_trans->commit == NULL) {
3142 struct got_reference *head_ref;
3143 error = got_ref_open(&head_ref, header->repo,
3144 gw_trans->headref, 0);
3148 error = got_ref_resolve(&header->id, header->repo, head_ref);
3149 got_ref_close(head_ref);
3153 error = got_object_open_as_commit(&header->commit,
3154 header->repo, header->id);
3156 struct got_reference *ref;
3157 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3158 if (error == NULL) {
3160 error = got_ref_resolve(&header->id, header->repo, ref);
3164 error = got_object_get_type(&obj_type, header->repo,
3168 if (obj_type == GOT_OBJ_TYPE_TAG) {
3169 struct got_tag_object *tag;
3170 error = got_object_open_as_tag(&tag,
3171 header->repo, header->id);
3174 if (got_object_tag_get_object_type(tag) !=
3175 GOT_OBJ_TYPE_COMMIT) {
3176 got_object_tag_close(tag);
3177 error = got_error(GOT_ERR_OBJ_TYPE);
3181 header->id = got_object_id_dup(
3182 got_object_tag_get_object_id(tag));
3183 if (header->id == NULL)
3184 error = got_error_from_errno(
3185 "got_object_id_dup");
3186 got_object_tag_close(tag);
3189 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3190 error = got_error(GOT_ERR_OBJ_TYPE);
3193 error = got_object_open_as_commit(&header->commit,
3194 header->repo, header->id);
3198 if (header->commit == NULL) {
3199 error = got_repo_match_object_id_prefix(&header->id,
3200 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3205 error = got_repo_match_object_id_prefix(&header->id,
3206 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3210 error = got_repo_map_path(&in_repo_path, header->repo,
3211 gw_trans->repo_path, 1);
3216 header->path = strdup(in_repo_path);
3220 error = got_ref_list(&header->refs, header->repo, NULL,
3221 got_ref_cmp_by_name, NULL);
3225 error = gw_get_commits(gw_trans, header, limit);
3233 char datebuf[11]; /* YYYY-MM-DD + NUL */
3236 struct gw_blame_cb_args {
3237 struct blame_line *lines;
3241 off_t *line_offsets;
3243 struct got_repository *repo;
3244 struct gw_trans *gw_trans;
3247 static const struct got_error *
3248 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3250 const struct got_error *err = NULL;
3251 struct gw_blame_cb_args *a = arg;
3252 struct blame_line *bline;
3254 size_t linesize = 0;
3255 struct got_commit_object *commit = NULL;
3258 time_t committer_time;
3259 enum kcgi_err kerr = KCGI_OK;
3261 if (nlines != a->nlines ||
3262 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3263 return got_error(GOT_ERR_RANGE);
3266 return NULL; /* no change in this commit */
3268 /* Annotate this line. */
3269 bline = &a->lines[lineno - 1];
3270 if (bline->annotated)
3272 err = got_object_id_str(&bline->id_str, id);
3276 err = got_object_open_as_commit(&commit, a->repo, id);
3280 bline->committer = strdup(got_object_commit_get_committer(commit));
3281 if (bline->committer == NULL) {
3282 err = got_error_from_errno("strdup");
3286 committer_time = got_object_commit_get_committer_time(commit);
3287 if (localtime_r(&committer_time, &tm) == NULL)
3288 return got_error_from_errno("localtime_r");
3289 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3290 &tm) >= sizeof(bline->datebuf)) {
3291 err = got_error(GOT_ERR_NO_SPACE);
3294 bline->annotated = 1;
3296 /* Print lines annotated so far. */
3297 bline = &a->lines[a->lineno_cur - 1];
3298 if (!bline->annotated)
3301 offset = a->line_offsets[a->lineno_cur - 1];
3302 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3303 err = got_error_from_errno("fseeko");
3307 while (bline->annotated) {
3308 char *smallerthan, *at, *nl, *committer;
3309 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3312 if (getline(&line, &linesize, a->f) == -1) {
3314 err = got_error_from_errno("getline");
3318 committer = bline->committer;
3319 smallerthan = strchr(committer, '<');
3320 if (smallerthan && smallerthan[1] != '\0')
3321 committer = smallerthan + 1;
3322 at = strchr(committer, '@');
3325 len = strlen(committer);
3327 committer[8] = '\0';
3329 nl = strchr(line, '\n');
3333 if (a->gw_trans->repo_folder == NULL)
3334 a->gw_trans->repo_folder = strdup("");
3335 if (a->gw_trans->repo_folder == NULL)
3338 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3339 "blame_wrapper", KATTR__MAX);
3340 if (kerr != KCGI_OK)
3342 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3343 "blame_number", KATTR__MAX);
3344 if (kerr != KCGI_OK)
3346 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3347 a->lineno_cur) == -1)
3349 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3350 if (kerr != KCGI_OK)
3352 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3353 if (kerr != KCGI_OK)
3356 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3357 "blame_hash", KATTR__MAX);
3358 if (kerr != KCGI_OK)
3360 if (asprintf(&href_diff,
3361 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3362 a->gw_trans->repo_name, bline->id_str,
3363 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3364 err = got_error_from_errno("asprintf");
3367 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3368 err = got_error_from_errno("asprintf");
3371 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3372 KATTR_HREF, href_diff, KATTR__MAX);
3373 if (kerr != KCGI_OK)
3375 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3376 if (kerr != KCGI_OK)
3378 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3379 if (kerr != KCGI_OK)
3382 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3383 "blame_date", KATTR__MAX);
3384 if (kerr != KCGI_OK)
3386 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3387 if (kerr != KCGI_OK)
3389 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3390 if (kerr != KCGI_OK)
3393 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3394 "blame_author", KATTR__MAX);
3395 if (kerr != KCGI_OK)
3397 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3398 if (kerr != KCGI_OK)
3400 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3401 if (kerr != KCGI_OK)
3404 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3405 "blame_code", KATTR__MAX);
3406 if (kerr != KCGI_OK)
3408 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3409 if (kerr != KCGI_OK)
3411 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3412 if (kerr != KCGI_OK)
3415 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3416 if (kerr != KCGI_OK)
3420 bline = &a->lines[a->lineno_cur - 1];
3428 got_object_commit_close(commit);
3430 if (err == NULL && kerr != KCGI_OK)
3431 err = gw_kcgi_error(kerr);
3435 static const struct got_error *
3436 gw_output_file_blame(struct gw_trans *gw_trans)
3438 const struct got_error *error = NULL;
3439 struct got_repository *repo = NULL;
3440 struct got_object_id *obj_id = NULL;
3441 struct got_object_id *commit_id = NULL;
3442 struct got_blob_object *blob = NULL;
3443 char *path = NULL, *in_repo_path = NULL;
3444 struct gw_blame_cb_args bca;
3448 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3452 if (asprintf(&path, "%s%s%s",
3453 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3454 gw_trans->repo_folder ? "/" : "",
3455 gw_trans->repo_file) == -1) {
3456 error = got_error_from_errno("asprintf");
3460 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3464 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3465 GOT_OBJ_TYPE_COMMIT, 1, repo);
3469 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3473 if (obj_id == NULL) {
3474 error = got_error(GOT_ERR_NO_OBJ);
3478 error = got_object_get_type(&obj_type, repo, obj_id);
3482 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3483 error = got_error(GOT_ERR_OBJ_TYPE);
3487 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3491 bca.f = got_opentemp();
3492 if (bca.f == NULL) {
3493 error = got_error_from_errno("got_opentemp");
3496 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3497 &bca.line_offsets, bca.f, blob);
3498 if (error || bca.nlines == 0)
3501 /* Don't include \n at EOF in the blame line count. */
3502 if (bca.line_offsets[bca.nlines - 1] == filesize)
3505 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3506 if (bca.lines == NULL) {
3507 error = got_error_from_errno("calloc");
3511 bca.nlines_prec = 0;
3518 bca.gw_trans = gw_trans;
3520 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3525 free(bca.line_offsets);
3531 for (i = 0; i < bca.nlines; i++) {
3532 struct blame_line *bline = &bca.lines[i];
3533 free(bline->id_str);
3534 free(bline->committer);
3537 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3538 error = got_error_from_errno("fclose");
3540 got_object_blob_close(blob);
3542 got_repo_close(repo);
3546 static const struct got_error *
3547 gw_output_blob_buf(struct gw_trans *gw_trans)
3549 const struct got_error *error = NULL;
3550 struct got_repository *repo = NULL;
3551 struct got_object_id *obj_id = NULL;
3552 struct got_object_id *commit_id = NULL;
3553 struct got_blob_object *blob = NULL;
3554 char *path = NULL, *in_repo_path = NULL;
3555 int obj_type, set_mime = 0;
3558 enum kcgi_err kerr = KCGI_OK;
3560 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3564 if (asprintf(&path, "%s%s%s",
3565 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3566 gw_trans->repo_folder ? "/" : "",
3567 gw_trans->repo_file) == -1) {
3568 error = got_error_from_errno("asprintf");
3572 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3576 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3577 GOT_OBJ_TYPE_COMMIT, 1, repo);
3581 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3585 if (obj_id == NULL) {
3586 error = got_error(GOT_ERR_NO_OBJ);
3590 error = got_object_get_type(&obj_type, repo, obj_id);
3594 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3595 error = got_error(GOT_ERR_OBJ_TYPE);
3599 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3603 hdrlen = got_object_blob_get_hdrlen(blob);
3605 error = got_object_blob_read_block(&len, blob);
3608 buf = got_object_blob_get_read_buf(blob);
3611 * Skip blob object header first time around,
3612 * which also contains a zero byte.
3615 if (set_mime == 0) {
3616 if (isbinary(buf, len - hdrlen))
3617 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3619 gw_trans->mime = KMIME_TEXT_PLAIN;
3621 error = gw_display_index(gw_trans);
3625 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3634 got_object_blob_close(blob);
3636 got_repo_close(repo);
3637 if (error == NULL && kerr != KCGI_OK)
3638 error = gw_kcgi_error(kerr);
3642 static const struct got_error *
3643 gw_output_repo_tree(struct gw_trans *gw_trans)
3645 const struct got_error *error = NULL;
3646 struct got_repository *repo = NULL;
3647 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3648 struct got_tree_object *tree = NULL;
3649 char *path = NULL, *in_repo_path = NULL;
3650 char *id_str = NULL;
3651 char *build_folder = NULL;
3652 char *href_blob = NULL, *href_blame = NULL;
3653 const char *class = NULL;
3654 int nentries, i, class_flip = 0;
3655 enum kcgi_err kerr = KCGI_OK;
3657 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3661 if (gw_trans->repo_folder != NULL)
3662 path = strdup(gw_trans->repo_folder);
3664 error = got_repo_map_path(&in_repo_path, repo,
3665 gw_trans->repo_path, 1);
3669 path = in_repo_path;
3672 if (gw_trans->commit == NULL) {
3673 struct got_reference *head_ref;
3674 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3677 error = got_ref_resolve(&commit_id, repo, head_ref);
3680 got_ref_close(head_ref);
3683 error = got_repo_match_object_id(&commit_id, NULL,
3684 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3689 error = got_object_id_str(&gw_trans->commit, commit_id);
3693 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3697 error = got_object_open_as_tree(&tree, repo, tree_id);
3701 nentries = got_object_tree_get_nentries(tree);
3702 for (i = 0; i < nentries; i++) {
3703 struct got_tree_entry *te;
3704 const char *modestr = "";
3707 te = got_object_tree_get_entry(tree, i);
3709 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3713 mode = got_tree_entry_get_mode(te);
3714 if (got_object_tree_entry_is_submodule(te))
3716 else if (S_ISLNK(mode))
3718 else if (S_ISDIR(mode))
3720 else if (mode & S_IXUSR)
3723 if (class_flip == 0) {
3724 class = "back_lightgray";
3727 class = "back_white";
3731 if (S_ISDIR(mode)) {
3732 if (asprintf(&build_folder, "%s/%s",
3733 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3734 got_tree_entry_get_name(te)) == -1) {
3735 error = got_error_from_errno(
3739 if (asprintf(&href_blob,
3740 "?path=%s&action=%s&commit=%s&folder=%s",
3741 gw_trans->repo_name, gw_trans->action_name,
3742 gw_trans->commit, build_folder) == -1) {
3743 error = got_error_from_errno("asprintf");
3747 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3748 KATTR_ID, "tree_wrapper", KATTR__MAX);
3749 if (kerr != KCGI_OK)
3751 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3752 KATTR_ID, "tree_line", KATTR_CLASS, class,
3754 if (kerr != KCGI_OK)
3756 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3757 KATTR_HREF, href_blob, KATTR_CLASS,
3758 "diff_directory", KATTR__MAX);
3759 if (kerr != KCGI_OK)
3761 kerr = khtml_puts(gw_trans->gw_html_req,
3762 got_tree_entry_get_name(te));
3763 if (kerr != KCGI_OK)
3765 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3766 if (kerr != KCGI_OK)
3768 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3769 if (kerr != KCGI_OK)
3771 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3772 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3774 if (kerr != KCGI_OK)
3776 kerr = khtml_entity(gw_trans->gw_html_req,
3778 if (kerr != KCGI_OK)
3780 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3781 if (kerr != KCGI_OK)
3784 if (asprintf(&href_blob,
3785 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3786 gw_trans->repo_name, "blob", gw_trans->commit,
3787 got_tree_entry_get_name(te),
3788 gw_trans->repo_folder ?
3789 gw_trans->repo_folder : "") == -1) {
3790 error = got_error_from_errno("asprintf");
3793 if (asprintf(&href_blame,
3794 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3795 gw_trans->repo_name, "blame", gw_trans->commit,
3796 got_tree_entry_get_name(te),
3797 gw_trans->repo_folder ?
3798 gw_trans->repo_folder : "") == -1) {
3799 error = got_error_from_errno("asprintf");
3803 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3804 KATTR_ID, "tree_wrapper", KATTR__MAX);
3805 if (kerr != KCGI_OK)
3807 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3808 KATTR_ID, "tree_line", KATTR_CLASS, class,
3810 if (kerr != KCGI_OK)
3812 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3813 KATTR_HREF, href_blob, KATTR__MAX);
3814 if (kerr != KCGI_OK)
3816 kerr = khtml_puts(gw_trans->gw_html_req,
3817 got_tree_entry_get_name(te));
3818 if (kerr != KCGI_OK)
3820 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3821 if (kerr != KCGI_OK)
3823 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3824 if (kerr != KCGI_OK)
3826 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3827 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3829 if (kerr != KCGI_OK)
3832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3833 KATTR_HREF, href_blob, KATTR__MAX);
3834 if (kerr != KCGI_OK)
3836 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3837 if (kerr != KCGI_OK)
3839 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3840 if (kerr != KCGI_OK)
3843 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3844 if (kerr != KCGI_OK)
3847 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3848 KATTR_HREF, href_blame, KATTR__MAX);
3849 if (kerr != KCGI_OK)
3851 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3852 if (kerr != KCGI_OK)
3855 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3856 if (kerr != KCGI_OK)
3864 build_folder = NULL;
3868 got_object_tree_close(tree);
3870 got_repo_close(repo);
3877 if (error == NULL && kerr != KCGI_OK)
3878 error = gw_kcgi_error(kerr);
3882 static const struct got_error *
3883 gw_output_repo_heads(struct gw_trans *gw_trans)
3885 const struct got_error *error = NULL;
3886 struct got_repository *repo = NULL;
3887 struct got_reflist_head refs;
3888 struct got_reflist_entry *re;
3889 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3890 char *href_commits = NULL;
3891 enum kcgi_err kerr = KCGI_OK;
3893 SIMPLEQ_INIT(&refs);
3895 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3899 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3904 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3905 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3906 if (kerr != KCGI_OK)
3908 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3909 KATTR_ID, "summary_heads_title", KATTR__MAX);
3910 if (kerr != KCGI_OK)
3912 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3913 if (kerr != KCGI_OK)
3915 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3916 if (kerr != KCGI_OK)
3918 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3919 KATTR_ID, "summary_heads_content", KATTR__MAX);
3920 if (kerr != KCGI_OK)
3923 SIMPLEQ_FOREACH(re, &refs, entry) {
3926 refname = strdup(got_ref_get_name(re->ref));
3927 if (refname == NULL) {
3928 error = got_error_from_errno("got_ref_to_str");
3932 if (strncmp(refname, "refs/heads/", 11) != 0) {
3937 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3942 if (strncmp(refname, "refs/heads/", 11) == 0)
3945 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3946 KATTR_ID, "heads_wrapper", KATTR__MAX);
3947 if (kerr != KCGI_OK)
3949 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3950 KATTR_ID, "heads_age", KATTR__MAX);
3951 if (kerr != KCGI_OK)
3953 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
3954 if (kerr != KCGI_OK)
3956 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3957 if (kerr != KCGI_OK)
3959 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3960 KATTR_ID, "heads_space", KATTR__MAX);
3961 if (kerr != KCGI_OK)
3963 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
3964 if (kerr != KCGI_OK)
3966 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3967 if (kerr != KCGI_OK)
3969 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3970 KATTR_ID, "head", KATTR__MAX);
3971 if (kerr != KCGI_OK)
3973 if (asprintf(&href_summary,
3974 "?path=%s&action=summary&headref=%s",
3975 gw_trans->repo_name, refname) == -1) {
3976 error = got_error_from_errno("asprintf");
3979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
3980 href_summary, KATTR__MAX);
3981 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3982 if (kerr != KCGI_OK)
3984 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3985 if (kerr != KCGI_OK)
3988 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3989 "navs_wrapper", KATTR__MAX);
3990 if (kerr != KCGI_OK)
3992 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3993 "navs", KATTR__MAX);
3994 if (kerr != KCGI_OK)
3997 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
3998 href_summary, KATTR__MAX);
3999 if (kerr != KCGI_OK)
4001 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4002 if (kerr != KCGI_OK)
4004 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4005 if (kerr != KCGI_OK)
4008 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4009 if (kerr != KCGI_OK)
4011 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4012 gw_trans->repo_name, refname) == -1) {
4013 error = got_error_from_errno("asprintf");
4016 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4017 href_briefs, KATTR__MAX);
4018 if (kerr != KCGI_OK)
4020 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4021 if (kerr != KCGI_OK)
4023 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4024 if (kerr != KCGI_OK)
4027 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4028 if (kerr != KCGI_OK)
4031 if (asprintf(&href_commits,
4032 "?path=%s&action=commits&headref=%s",
4033 gw_trans->repo_name, refname) == -1) {
4034 error = got_error_from_errno("asprintf");
4037 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4038 href_commits, KATTR__MAX);
4039 if (kerr != KCGI_OK)
4041 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4042 if (kerr != KCGI_OK)
4044 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4045 if (kerr != KCGI_OK)
4048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4049 "dotted_line", KATTR__MAX);
4050 if (kerr != KCGI_OK)
4052 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4053 if (kerr != KCGI_OK)
4057 href_summary = NULL;
4061 href_commits = NULL;
4064 got_ref_list_free(&refs);
4069 got_repo_close(repo);
4073 static const struct got_error *
4074 gw_output_site_link(struct gw_trans *gw_trans)
4076 const struct got_error *error = NULL;
4077 char *href_summary = NULL;
4078 enum kcgi_err kerr = KCGI_OK;
4080 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4081 "site_link", KATTR__MAX);
4082 if (kerr != KCGI_OK)
4084 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4086 if (kerr != KCGI_OK)
4088 kerr = khtml_puts(gw_trans->gw_html_req,
4089 gw_trans->gw_conf->got_site_link);
4090 if (kerr != KCGI_OK)
4092 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4093 if (kerr != KCGI_OK)
4096 if (gw_trans->repo_name != NULL) {
4097 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4098 if (kerr != KCGI_OK)
4100 if (asprintf(&href_summary, "?path=%s&action=summary",
4101 gw_trans->repo_name) == -1)
4103 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4104 href_summary, KATTR__MAX);
4105 if (kerr != KCGI_OK)
4107 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4108 if (kerr != KCGI_OK)
4110 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4111 if (kerr != KCGI_OK)
4113 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4114 if (kerr != KCGI_OK)
4116 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->action_name);
4117 if (kerr != KCGI_OK)
4121 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4122 if (kerr != KCGI_OK)
4129 static const struct got_error *
4130 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4132 const struct got_error *error = NULL;
4134 enum kcgi_err kerr = KCGI_OK;
4136 if (strncmp(buf, "-", 1) == 0)
4137 color = "diff_minus";
4138 else if (strncmp(buf, "+", 1) == 0)
4139 color = "diff_plus";
4140 else if (strncmp(buf, "@@", 2) == 0)
4141 color = "diff_chunk_header";
4142 else if (strncmp(buf, "@@", 2) == 0)
4143 color = "diff_chunk_header";
4144 else if (strncmp(buf, "commit +", 8) == 0)
4145 color = "diff_meta";
4146 else if (strncmp(buf, "commit -", 8) == 0)
4147 color = "diff_meta";
4148 else if (strncmp(buf, "blob +", 6) == 0)
4149 color = "diff_meta";
4150 else if (strncmp(buf, "blob -", 6) == 0)
4151 color = "diff_meta";
4152 else if (strncmp(buf, "file +", 6) == 0)
4153 color = "diff_meta";
4154 else if (strncmp(buf, "file -", 6) == 0)
4155 color = "diff_meta";
4156 else if (strncmp(buf, "from:", 5) == 0)
4157 color = "diff_author";
4158 else if (strncmp(buf, "via:", 4) == 0)
4159 color = "diff_author";
4160 else if (strncmp(buf, "date:", 5) == 0)
4161 color = "diff_date";
4162 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4163 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4164 if (error == NULL && kerr != KCGI_OK)
4165 error = gw_kcgi_error(kerr);
4170 main(int argc, char *argv[])
4172 const struct got_error *error = NULL;
4173 struct gw_trans *gw_trans;
4174 struct gw_dir *dir = NULL, *tdir;
4175 const char *page = "index";
4179 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4182 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4185 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4188 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4191 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4192 if (kerr != KCGI_OK) {
4193 error = gw_kcgi_error(kerr);
4197 if ((gw_trans->gw_conf =
4198 malloc(sizeof(struct gotweb_conf))) == NULL) {
4200 error = got_error_from_errno("malloc");
4204 TAILQ_INIT(&gw_trans->gw_dirs);
4205 TAILQ_INIT(&gw_trans->gw_headers);
4208 gw_trans->repos_total = 0;
4209 gw_trans->repo_path = NULL;
4210 gw_trans->commit = NULL;
4211 gw_trans->headref = strdup(GOT_REF_HEAD);
4212 gw_trans->mime = KMIME_TEXT_HTML;
4213 gw_trans->gw_tmpl->key = gw_templs;
4214 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4215 gw_trans->gw_tmpl->arg = gw_trans;
4216 gw_trans->gw_tmpl->cb = gw_template;
4217 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4221 error = gw_parse_querystring(gw_trans);
4225 if (gw_trans->action == GW_BLOB)
4226 error = gw_blob(gw_trans);
4228 error = gw_display_index(gw_trans);
4231 gw_trans->mime = KMIME_TEXT_PLAIN;
4232 gw_trans->action = GW_ERR;
4233 gw_display_error(gw_trans, error);
4236 free(gw_trans->gw_conf->got_repos_path);
4237 free(gw_trans->gw_conf->got_site_name);
4238 free(gw_trans->gw_conf->got_site_owner);
4239 free(gw_trans->gw_conf->got_site_link);
4240 free(gw_trans->gw_conf->got_logo);
4241 free(gw_trans->gw_conf->got_logo_url);
4242 free(gw_trans->gw_conf);
4243 free(gw_trans->commit);
4244 free(gw_trans->repo_path);
4245 free(gw_trans->repo_name);
4246 free(gw_trans->repo_file);
4247 free(gw_trans->action_name);
4248 free(gw_trans->headref);
4250 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4252 free(dir->description);
4261 khttp_free(gw_trans->gw_req);