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 */
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 const struct got_error *gw_init_gw_dir(struct gw_dir **, char *);
157 static struct gw_header *gw_init_header(void);
159 static void gw_free_headers(struct gw_header *);
160 static void gw_display_error(struct gw_trans *,
161 const struct got_error *);
163 static int gw_template(size_t, void *);
165 static const struct got_error *gw_strdup_string(char **, char *,
167 static const struct got_error *gw_get_repo_description(char **,
168 struct gw_trans *, char *);
169 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
171 static const struct got_error *gw_get_time_str(char **, time_t, int);
172 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
173 char *, char *, int);
174 static const struct got_error *gw_output_file_blame(struct gw_trans *);
175 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
176 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
177 static const struct got_error *gw_output_diff(struct gw_trans *,
179 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
180 struct gw_header *, int, int);
181 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
182 static const struct got_error *gw_output_site_link(struct gw_trans *);
183 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
185 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
187 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
189 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
191 static const struct got_error *gw_gen_author_header(struct gw_trans *,
193 static const struct got_error *gw_gen_age_header(struct gw_trans *,
195 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
197 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
199 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
200 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
202 static const struct got_error *gw_display_index(struct gw_trans *);
203 static const struct got_error *gw_get_header(struct gw_trans *,
204 struct gw_header *, int);
205 static const struct got_error *gw_get_commits(struct gw_trans *,
206 struct gw_header *, int);
207 static const struct got_error *gw_get_commit(struct gw_trans *,
209 static const struct got_error *gw_apply_unveil(const char *);
210 static const struct got_error *gw_blame_cb(void *, int, int,
211 struct got_object_id *);
212 static const struct got_error *gw_load_got_paths(struct gw_trans *);
213 static const struct got_error *gw_load_got_path(struct gw_trans *,
215 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)
290 const struct got_error *err;
292 if (repo_path && unveil(repo_path, "r") != 0)
293 return got_error_from_errno2("unveil", repo_path);
295 if (unveil("/tmp", "rwc") != 0)
296 return got_error_from_errno2("unveil", "/tmp");
298 err = got_privsep_unveil_exec_helpers();
302 if (unveil(NULL, NULL) != 0)
303 return got_error_from_errno("unveil");
308 static const struct got_error *
309 gw_strdup_string(char **s, char *str1, const char *str2)
312 return got_error_from_errno("strdup");
318 return got_error_from_errno("strdup");
323 isbinary(const uint8_t *buf, size_t n)
327 for (i = 0; i < n; i++)
333 static const struct got_error *
334 gw_blame(struct gw_trans *gw_trans)
336 const struct got_error *error = NULL;
337 struct gw_header *header = NULL;
341 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
343 return got_error_from_errno("pledge");
345 if ((header = gw_init_header()) == NULL)
346 return got_error_from_errno("malloc");
348 error = gw_apply_unveil(gw_trans->gw_dir->path);
352 error = gw_get_header(gw_trans, header, 1);
356 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
357 "blame_header_wrapper", KATTR__MAX);
360 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
361 "blame_header", KATTR__MAX);
364 error = gw_get_time_str(&age, header->committer_time,
368 error = gw_gen_age_header(gw_trans, age ?age : "");
371 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
374 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
378 "dotted_line", KATTR__MAX);
381 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
385 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
386 "blame", KATTR__MAX);
389 error = gw_output_file_blame(gw_trans);
392 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
394 got_ref_list_free(&header->refs);
395 gw_free_headers(header);
396 if (error == NULL && kerr != KCGI_OK)
397 error = gw_kcgi_error(kerr);
401 static const struct got_error *
402 gw_blob(struct gw_trans *gw_trans)
404 const struct got_error *error = NULL;
405 struct gw_header *header = NULL;
407 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
409 return got_error_from_errno("pledge");
411 if ((header = gw_init_header()) == NULL)
412 return got_error_from_errno("malloc");
414 error = gw_apply_unveil(gw_trans->gw_dir->path);
418 error = gw_get_header(gw_trans, header, 1);
422 error = gw_output_blob_buf(gw_trans);
424 got_ref_list_free(&header->refs);
425 gw_free_headers(header);
429 static const struct got_error *
430 gw_diff(struct gw_trans *gw_trans)
432 const struct got_error *error = NULL;
433 struct gw_header *header = NULL;
435 enum kcgi_err kerr = KCGI_OK;
437 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
439 return got_error_from_errno("pledge");
441 if ((header = gw_init_header()) == NULL)
442 return got_error_from_errno("malloc");
444 error = gw_apply_unveil(gw_trans->gw_dir->path);
448 error = gw_get_header(gw_trans, header, 1);
452 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
453 "diff_header_wrapper", KATTR__MAX);
456 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
457 "diff_header", KATTR__MAX);
460 error = gw_gen_diff_header(gw_trans, header->parent_id,
464 error = gw_gen_commit_header(gw_trans, header->commit_id,
468 error = gw_gen_tree_header(gw_trans, header->tree_id);
471 error = gw_gen_author_header(gw_trans, header->author);
474 error = gw_gen_committer_header(gw_trans, header->author);
477 error = gw_get_time_str(&age, header->committer_time,
481 error = gw_gen_age_header(gw_trans, age ?age : "");
484 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
487 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
490 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
491 "dotted_line", KATTR__MAX);
494 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
498 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
502 error = gw_output_diff(gw_trans, header);
506 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
508 got_ref_list_free(&header->refs);
509 gw_free_headers(header);
511 if (error == NULL && kerr != KCGI_OK)
512 error = gw_kcgi_error(kerr);
516 static const struct got_error *
517 gw_index(struct gw_trans *gw_trans)
519 const struct got_error *error = NULL;
520 struct gw_dir *gw_dir = NULL;
521 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
522 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
523 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
526 if (pledge("stdio rpath proc exec sendfd unveil",
528 error = got_error_from_errno("pledge");
532 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
536 error = gw_load_got_paths(gw_trans);
540 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
541 "index_header", KATTR__MAX);
543 return gw_kcgi_error(kerr);
544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
545 "index_header_project", KATTR__MAX);
547 return gw_kcgi_error(kerr);
548 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
550 return gw_kcgi_error(kerr);
551 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
553 return gw_kcgi_error(kerr);
555 if (gw_trans->gw_conf->got_show_repo_description) {
556 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
557 "index_header_description", KATTR__MAX);
559 return gw_kcgi_error(kerr);
560 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
562 return gw_kcgi_error(kerr);
563 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
565 return gw_kcgi_error(kerr);
568 if (gw_trans->gw_conf->got_show_repo_owner) {
569 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
570 "index_header_owner", KATTR__MAX);
572 return gw_kcgi_error(kerr);
573 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
575 return gw_kcgi_error(kerr);
576 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
578 return gw_kcgi_error(kerr);
581 if (gw_trans->gw_conf->got_show_repo_age) {
582 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
583 "index_header_age", KATTR__MAX);
585 return gw_kcgi_error(kerr);
586 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
588 return gw_kcgi_error(kerr);
589 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
591 return gw_kcgi_error(kerr);
594 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
596 return gw_kcgi_error(kerr);
598 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
599 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
600 "index_wrapper", KATTR__MAX);
602 return gw_kcgi_error(kerr);
603 kerr = khtml_puts(gw_trans->gw_html_req,
604 "No repositories found in ");
606 return gw_kcgi_error(kerr);
607 kerr = khtml_puts(gw_trans->gw_html_req,
608 gw_trans->gw_conf->got_repos_path);
610 return gw_kcgi_error(kerr);
611 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
613 return gw_kcgi_error(kerr);
614 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
615 "dotted_line", KATTR__MAX);
617 return gw_kcgi_error(kerr);
618 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
620 return gw_kcgi_error(kerr);
624 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
627 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
628 if (gw_trans->page > 0 && (gw_trans->page *
629 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
636 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
637 "index_wrapper", KATTR__MAX);
641 if (asprintf(&href_summary, "?path=%s&action=summary",
642 gw_dir->name) == -1) {
643 error = got_error_from_errno("asprintf");
646 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
647 "index_project", KATTR__MAX);
650 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
651 href_summary, KATTR__MAX);
654 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
657 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
660 if (gw_trans->gw_conf->got_show_repo_description) {
661 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
662 KATTR_ID, "index_project_description", KATTR__MAX);
665 kerr = khtml_puts(gw_trans->gw_html_req,
666 gw_dir->description ? gw_dir->description : "");
669 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
673 if (gw_trans->gw_conf->got_show_repo_owner) {
674 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
675 KATTR_ID, "index_project_owner", KATTR__MAX);
678 kerr = khtml_puts(gw_trans->gw_html_req,
679 gw_dir->owner ? gw_dir->owner : "");
682 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
686 if (gw_trans->gw_conf->got_show_repo_age) {
687 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
688 KATTR_ID, "index_project_age", KATTR__MAX);
691 kerr = khtml_puts(gw_trans->gw_html_req,
692 gw_dir->age ? gw_dir->age : "");
695 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
700 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
701 "navs_wrapper", KATTR__MAX);
704 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
709 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
710 href_summary, KATTR__MAX);
713 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
716 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
720 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
724 if (asprintf(&href_briefs, "?path=%s&action=briefs",
725 gw_dir->name) == -1) {
726 error = got_error_from_errno("asprintf");
729 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
730 href_briefs, KATTR__MAX);
733 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
736 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
738 error = gw_kcgi_error(kerr);
740 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
744 if (asprintf(&href_commits, "?path=%s&action=commits",
745 gw_dir->name) == -1) {
746 error = got_error_from_errno("asprintf");
749 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
750 href_commits, KATTR__MAX);
753 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
756 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
760 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
764 if (asprintf(&href_tree, "?path=%s&action=tree",
765 gw_dir->name) == -1) {
766 error = got_error_from_errno("asprintf");
769 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
770 href_tree, KATTR__MAX);
773 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
777 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
780 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
781 "dotted_line", KATTR__MAX);
784 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
797 if (gw_trans->gw_conf->got_max_repos_display == 0)
800 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
801 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
802 KATTR_ID, "np_wrapper", KATTR__MAX);
805 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
806 KATTR_ID, "nav_prev", KATTR__MAX);
809 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
810 (gw_trans->page > 0) &&
811 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
812 prev_disp == gw_trans->repos_total)) {
813 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
814 KATTR_ID, "np_wrapper", KATTR__MAX);
817 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
818 KATTR_ID, "nav_prev", KATTR__MAX);
823 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
824 (gw_trans->page > 0) &&
825 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
826 prev_disp == gw_trans->repos_total)) {
827 if (asprintf(&href_prev, "?page=%d",
828 gw_trans->page - 1) == -1) {
829 error = got_error_from_errno("asprintf");
832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
833 KATTR_HREF, href_prev, KATTR__MAX);
836 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
839 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
844 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
846 return gw_kcgi_error(kerr);
848 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
849 next_disp == gw_trans->gw_conf->got_max_repos_display &&
850 dir_c != (gw_trans->page + 1) *
851 gw_trans->gw_conf->got_max_repos_display) {
852 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
853 KATTR_ID, "nav_next", KATTR__MAX);
856 if (asprintf(&href_next, "?page=%d",
857 gw_trans->page + 1) == -1) {
858 error = got_error_from_errno("calloc");
861 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
862 KATTR_HREF, href_next, KATTR__MAX);
865 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
868 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
875 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
876 (gw_trans->page > 0) &&
877 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
878 prev_disp == gw_trans->repos_total)) {
879 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
892 if (error == NULL && kerr != KCGI_OK)
893 error = gw_kcgi_error(kerr);
897 static const struct got_error *
898 gw_commits(struct gw_trans *gw_trans)
900 const struct got_error *error = NULL;
901 struct gw_header *header = NULL, *n_header = NULL;
903 char *href_diff = NULL, *href_blob = NULL;
904 enum kcgi_err kerr = KCGI_OK;
906 if ((header = gw_init_header()) == NULL)
907 return got_error_from_errno("malloc");
909 if (pledge("stdio rpath proc exec sendfd unveil",
911 error = got_error_from_errno("pledge");
915 error = gw_apply_unveil(gw_trans->gw_dir->path);
919 error = gw_get_header(gw_trans, header,
920 gw_trans->gw_conf->got_max_commits_display);
924 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
925 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
926 "commits_line_wrapper", KATTR__MAX);
929 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
933 error = gw_gen_author_header(gw_trans, n_header->author);
936 error = gw_gen_committer_header(gw_trans, n_header->author);
939 error = gw_get_time_str(&age, n_header->committer_time,
943 error = gw_gen_age_header(gw_trans, age ?age : "");
946 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
950 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
951 "dotted_line", KATTR__MAX);
954 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
958 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
959 "commit", KATTR__MAX);
962 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
965 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
969 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
970 gw_trans->repo_name, n_header->commit_id) == -1) {
971 error = got_error_from_errno("asprintf");
974 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
975 KATTR_ID, "navs_wrapper", KATTR__MAX);
978 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
979 KATTR_ID, "navs", KATTR__MAX);
982 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
983 KATTR_HREF, href_diff, KATTR__MAX);
986 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
989 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
993 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
997 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
998 gw_trans->repo_name, n_header->commit_id) == -1) {
999 error = got_error_from_errno("asprintf");
1002 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1003 KATTR_HREF, href_blob, KATTR__MAX);
1004 if (kerr != KCGI_OK)
1006 khtml_puts(gw_trans->gw_html_req, "tree");
1007 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1008 if (kerr != KCGI_OK)
1010 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1011 if (kerr != KCGI_OK)
1014 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1015 "solid_line", KATTR__MAX);
1016 if (kerr != KCGI_OK)
1018 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1019 if (kerr != KCGI_OK)
1026 got_ref_list_free(&header->refs);
1027 gw_free_headers(header);
1028 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1029 gw_free_headers(n_header);
1033 if (error == NULL && kerr != KCGI_OK)
1034 error = gw_kcgi_error(kerr);
1038 static const struct got_error *
1039 gw_briefs(struct gw_trans *gw_trans)
1041 const struct got_error *error = NULL;
1042 struct gw_header *header = NULL, *n_header = NULL;
1043 char *age = NULL, *age_html = NULL;
1044 char *href_diff = NULL, *href_blob = NULL;
1045 char *newline, *smallerthan;
1046 enum kcgi_err kerr = KCGI_OK;
1048 if ((header = gw_init_header()) == NULL)
1049 return got_error_from_errno("malloc");
1051 if (pledge("stdio rpath proc exec sendfd unveil",
1053 error = got_error_from_errno("pledge");
1057 error = gw_apply_unveil(gw_trans->gw_dir->path);
1061 if (gw_trans->action == GW_SUMMARY)
1062 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1064 error = gw_get_header(gw_trans, header,
1065 gw_trans->gw_conf->got_max_commits_display);
1069 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1070 error = gw_get_time_str(&age, n_header->committer_time,
1075 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1076 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1077 if (kerr != KCGI_OK)
1080 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1081 KATTR_ID, "briefs_age", KATTR__MAX);
1082 if (kerr != KCGI_OK)
1084 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
1085 error = got_error_from_errno("asprintf");
1088 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
1089 if (kerr != KCGI_OK)
1091 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1092 if (kerr != KCGI_OK)
1095 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1096 KATTR_ID, "briefs_author", KATTR__MAX);
1097 if (kerr != KCGI_OK)
1099 smallerthan = strchr(n_header->author, '<');
1101 *smallerthan = '\0';
1102 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1103 if (kerr != KCGI_OK)
1105 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1106 if (kerr != KCGI_OK)
1109 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1110 gw_trans->repo_name, n_header->commit_id) == -1) {
1111 error = got_error_from_errno("asprintf");
1114 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1115 KATTR_ID, "briefs_log", KATTR__MAX);
1116 if (kerr != KCGI_OK)
1118 newline = strchr(n_header->commit_msg, '\n');
1121 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1122 KATTR_HREF, href_diff, KATTR__MAX);
1123 if (kerr != KCGI_OK)
1125 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1126 if (kerr != KCGI_OK)
1128 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1129 if (kerr != KCGI_OK)
1132 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1133 KATTR_ID, "navs_wrapper", KATTR__MAX);
1134 if (kerr != KCGI_OK)
1136 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1137 KATTR_ID, "navs", KATTR__MAX);
1138 if (kerr != KCGI_OK)
1140 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1141 KATTR_HREF, href_diff, KATTR__MAX);
1142 if (kerr != KCGI_OK)
1144 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1145 if (kerr != KCGI_OK)
1147 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1148 if (kerr != KCGI_OK)
1151 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1152 if (kerr != KCGI_OK)
1155 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1156 gw_trans->repo_name, n_header->commit_id) == -1) {
1157 error = got_error_from_errno("asprintf");
1160 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1161 KATTR_HREF, href_blob, KATTR__MAX);
1162 if (kerr != KCGI_OK)
1164 khtml_puts(gw_trans->gw_html_req, "tree");
1165 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1166 if (kerr != KCGI_OK)
1168 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1169 if (kerr != KCGI_OK)
1172 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1173 KATTR_ID, "dotted_line", KATTR__MAX);
1174 if (kerr != KCGI_OK)
1176 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1177 if (kerr != KCGI_OK)
1190 got_ref_list_free(&header->refs);
1191 gw_free_headers(header);
1192 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1193 gw_free_headers(n_header);
1198 if (error == NULL && kerr != KCGI_OK)
1199 error = gw_kcgi_error(kerr);
1203 static const struct got_error *
1204 gw_summary(struct gw_trans *gw_trans)
1206 const struct got_error *error = NULL;
1208 enum kcgi_err kerr = KCGI_OK;
1210 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1211 return got_error_from_errno("pledge");
1213 /* unveil is applied with gw_briefs below */
1215 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1216 "summary_wrapper", KATTR__MAX);
1217 if (kerr != KCGI_OK)
1218 return gw_kcgi_error(kerr);
1220 if (gw_trans->gw_conf->got_show_repo_description &&
1221 gw_trans->gw_dir->description != NULL &&
1222 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1224 KATTR_ID, "description_title", KATTR__MAX);
1225 if (kerr != KCGI_OK)
1227 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1228 if (kerr != KCGI_OK)
1230 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1231 if (kerr != KCGI_OK)
1233 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1234 KATTR_ID, "description", KATTR__MAX);
1235 if (kerr != KCGI_OK)
1237 kerr = khtml_puts(gw_trans->gw_html_req,
1238 gw_trans->gw_dir->description);
1239 if (kerr != KCGI_OK)
1241 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1242 if (kerr != KCGI_OK)
1246 if (gw_trans->gw_conf->got_show_repo_owner &&
1247 gw_trans->gw_dir->owner != NULL &&
1248 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1249 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1250 KATTR_ID, "repo_owner_title", KATTR__MAX);
1251 if (kerr != KCGI_OK)
1253 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1254 if (kerr != KCGI_OK)
1256 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1257 if (kerr != KCGI_OK)
1259 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1260 KATTR_ID, "repo_owner", KATTR__MAX);
1261 if (kerr != KCGI_OK)
1263 kerr = khtml_puts(gw_trans->gw_html_req,
1264 gw_trans->gw_dir->owner);
1265 if (kerr != KCGI_OK)
1267 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1268 if (kerr != KCGI_OK)
1272 if (gw_trans->gw_conf->got_show_repo_age) {
1273 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1274 "refs/heads", TM_LONG);
1278 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1279 KATTR_ID, "last_change_title", KATTR__MAX);
1280 if (kerr != KCGI_OK)
1282 kerr = khtml_puts(gw_trans->gw_html_req,
1284 if (kerr != KCGI_OK)
1286 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1287 if (kerr != KCGI_OK)
1289 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1290 KATTR_ID, "last_change", KATTR__MAX);
1291 if (kerr != KCGI_OK)
1293 kerr = khtml_puts(gw_trans->gw_html_req, age);
1294 if (kerr != KCGI_OK)
1296 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1297 if (kerr != KCGI_OK)
1302 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1303 gw_trans->gw_dir->url != NULL &&
1304 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1305 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1306 KATTR_ID, "cloneurl_title", KATTR__MAX);
1307 if (kerr != KCGI_OK)
1309 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1310 if (kerr != KCGI_OK)
1312 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1313 if (kerr != KCGI_OK)
1315 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1316 KATTR_ID, "cloneurl", KATTR__MAX);
1317 if (kerr != KCGI_OK)
1319 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1320 if (kerr != KCGI_OK)
1322 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1323 if (kerr != KCGI_OK)
1327 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1328 if (kerr != KCGI_OK)
1331 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1332 "briefs_title_wrapper", KATTR__MAX);
1333 if (kerr != KCGI_OK)
1335 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1336 "briefs_title", KATTR__MAX);
1337 if (kerr != KCGI_OK)
1339 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1340 if (kerr != KCGI_OK)
1342 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1343 if (kerr != KCGI_OK)
1345 error = gw_briefs(gw_trans);
1349 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1354 error = gw_output_repo_heads(gw_trans);
1357 if (error == NULL && kerr != KCGI_OK)
1358 error = gw_kcgi_error(kerr);
1362 static const struct got_error *
1363 gw_tree(struct gw_trans *gw_trans)
1365 const struct got_error *error = NULL;
1366 struct gw_header *header = NULL;
1367 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1368 char *age = NULL, *age_html = NULL;
1371 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1372 return got_error_from_errno("pledge");
1374 if ((header = gw_init_header()) == NULL)
1375 return got_error_from_errno("malloc");
1377 error = gw_apply_unveil(gw_trans->gw_dir->path);
1381 error = gw_get_header(gw_trans, header, 1);
1385 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1386 "tree_header_wrapper", KATTR__MAX);
1387 if (kerr != KCGI_OK)
1389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1390 "tree_header", KATTR__MAX);
1391 if (kerr != KCGI_OK)
1393 error = gw_gen_tree_header(gw_trans, header->tree_id);
1396 error = gw_get_time_str(&age, header->committer_time,
1400 error = gw_gen_age_header(gw_trans, age ?age : "");
1403 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1406 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1407 if (kerr != KCGI_OK)
1409 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1410 "dotted_line", KATTR__MAX);
1411 if (kerr != KCGI_OK)
1413 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1414 if (kerr != KCGI_OK)
1417 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1418 "tree", KATTR__MAX);
1419 if (kerr != KCGI_OK)
1421 error = gw_output_repo_tree(gw_trans);
1425 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1427 got_ref_list_free(&header->refs);
1428 gw_free_headers(header);
1429 free(tree_html_disp);
1434 if (error == NULL && kerr != KCGI_OK)
1435 error = gw_kcgi_error(kerr);
1439 static const struct got_error *
1440 gw_tag(struct gw_trans *gw_trans)
1442 const struct got_error *error = NULL;
1443 struct gw_header *header = NULL;
1446 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1447 return got_error_from_errno("pledge");
1449 if ((header = gw_init_header()) == NULL)
1450 return got_error_from_errno("malloc");
1452 error = gw_apply_unveil(gw_trans->gw_dir->path);
1456 error = gw_get_header(gw_trans, header, 1);
1460 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1461 "tag_header_wrapper", KATTR__MAX);
1462 if (kerr != KCGI_OK)
1464 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1465 "tag_header", KATTR__MAX);
1466 if (kerr != KCGI_OK)
1468 error = gw_gen_commit_header(gw_trans, header->commit_id,
1472 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1475 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1476 if (kerr != KCGI_OK)
1478 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1479 "dotted_line", KATTR__MAX);
1480 if (kerr != KCGI_OK)
1482 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1483 if (kerr != KCGI_OK)
1486 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1487 "tree", KATTR__MAX);
1488 if (kerr != KCGI_OK)
1491 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1495 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1497 got_ref_list_free(&header->refs);
1498 gw_free_headers(header);
1499 if (error == NULL && kerr != KCGI_OK)
1500 error = gw_kcgi_error(kerr);
1504 static const struct got_error *
1505 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1507 const struct got_error *error = NULL;
1512 if (asprintf(&dir_test, "%s/%s/%s",
1513 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1514 GOTWEB_GIT_DIR) == -1)
1515 return got_error_from_errno("asprintf");
1517 dt = opendir(dir_test);
1521 error = gw_strdup_string(&gw_dir->path, dir_test, NULL);
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 error = gw_strdup_string(&gw_dir->path, dir_test, NULL);
1552 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1556 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1559 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1560 "refs/heads", TM_DIFF);
1563 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1571 static const struct got_error *
1572 gw_load_got_paths(struct gw_trans *gw_trans)
1574 const struct got_error *error = NULL;
1576 struct dirent **sd_dent;
1577 struct gw_dir *gw_dir;
1579 unsigned int d_cnt, d_i;
1581 d = opendir(gw_trans->gw_conf->got_repos_path);
1583 error = got_error_from_errno2("opendir",
1584 gw_trans->gw_conf->got_repos_path);
1588 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1591 error = got_error_from_errno2("scandir",
1592 gw_trans->gw_conf->got_repos_path);
1596 for (d_i = 0; d_i < d_cnt; d_i++) {
1597 if (gw_trans->gw_conf->got_max_repos > 0 &&
1598 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1599 break; /* account for parent and self */
1601 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1602 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1605 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1609 error = gw_load_got_path(gw_trans, gw_dir);
1610 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1617 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1618 !got_path_dir_is_empty(gw_dir->path)) {
1619 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1621 gw_trans->repos_total++;
1629 static const struct got_error *
1630 gw_parse_querystring(struct gw_trans *gw_trans)
1632 const struct got_error *error = NULL;
1634 struct gw_query_action *action = NULL;
1637 if (gw_trans->gw_req->fieldnmap[0]) {
1638 error = got_error_from_errno("bad parse");
1640 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1641 /* define gw_trans->repo_path */
1642 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1643 return got_error_from_errno("asprintf");
1645 if (asprintf(&gw_trans->repo_path, "%s/%s",
1646 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1647 return got_error_from_errno("asprintf");
1649 /* get action and set function */
1650 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1651 for (i = 0; i < nitems(gw_query_funcs); i++) {
1652 action = &gw_query_funcs[i];
1653 if (action->func_name == NULL)
1656 if (strcmp(action->func_name,
1657 p->parsed.s) == 0) {
1658 gw_trans->action = i;
1659 if (asprintf(&gw_trans->action_name,
1660 "%s", action->func_name) == -1)
1662 got_error_from_errno(
1671 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1672 if (asprintf(&gw_trans->commit, "%s",
1674 return got_error_from_errno("asprintf");
1676 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1677 if (asprintf(&gw_trans->repo_file, "%s",
1679 return got_error_from_errno("asprintf");
1681 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1682 if (asprintf(&gw_trans->repo_folder, "%s",
1684 return got_error_from_errno("asprintf");
1686 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1687 if (asprintf(&gw_trans->headref, "%s",
1689 return got_error_from_errno("asprintf");
1691 if (action == NULL) {
1692 error = got_error_from_errno("invalid action");
1695 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1699 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1703 gw_trans->action = GW_INDEX;
1705 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1706 gw_trans->page = p->parsed.i;
1711 static const struct got_error *
1712 gw_init_gw_dir(struct gw_dir **gw_dir, char *dir)
1714 const struct got_error *error;
1716 *gw_dir = malloc(sizeof(**gw_dir));
1717 if (*gw_dir == NULL)
1718 return got_error_from_errno("malloc");
1720 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1721 error = got_error_from_errno("asprintf");
1730 static const struct got_error *
1731 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1735 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1736 if (kerr != KCGI_OK)
1737 return gw_kcgi_error(kerr);
1738 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1740 if (kerr != KCGI_OK)
1741 return gw_kcgi_error(kerr);
1742 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1744 if (kerr != KCGI_OK)
1745 return gw_kcgi_error(kerr);
1746 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1748 if (kerr != KCGI_OK)
1749 return gw_kcgi_error(kerr);
1750 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1751 if (kerr != KCGI_OK)
1752 return gw_kcgi_error(kerr);
1753 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1755 if (kerr != KCGI_OK)
1756 return gw_kcgi_error(kerr);
1758 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1759 kerr = khttp_head(gw_trans->gw_req,
1760 kresps[KRESP_CONTENT_DISPOSITION],
1761 "attachment; filename=%s", gw_trans->repo_file);
1762 if (kerr != KCGI_OK)
1763 return gw_kcgi_error(kerr);
1766 kerr = khttp_body(gw_trans->gw_req);
1767 return gw_kcgi_error(kerr);
1770 static const struct got_error *
1771 gw_display_index(struct gw_trans *gw_trans)
1773 const struct got_error *error;
1776 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1780 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1781 if (kerr != KCGI_OK)
1782 return gw_kcgi_error(kerr);
1784 if (gw_trans->action != GW_BLOB) {
1785 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1786 gw_query_funcs[gw_trans->action].template);
1787 if (kerr != KCGI_OK) {
1788 khtml_close(gw_trans->gw_html_req);
1789 return gw_kcgi_error(kerr);
1793 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1797 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1799 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1802 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1804 khtml_puts(gw_trans->gw_html_req, err->msg);
1805 khtml_close(gw_trans->gw_html_req);
1809 gw_template(size_t key, void *arg)
1811 const struct got_error *error = NULL;
1813 struct gw_trans *gw_trans = arg;
1814 char *img_src = NULL;
1818 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1819 KATTR_NAME, "viewport",
1820 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1822 if (kerr != KCGI_OK)
1824 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1825 if (kerr != KCGI_OK)
1827 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1828 KATTR_CHARSET, "utf-8",
1830 if (kerr != KCGI_OK)
1832 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1833 if (kerr != KCGI_OK)
1835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1836 KATTR_NAME, "msapplication-TileColor",
1837 KATTR_CONTENT, "#da532c", KATTR__MAX);
1838 if (kerr != KCGI_OK)
1840 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1841 if (kerr != KCGI_OK)
1843 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1844 KATTR_NAME, "theme-color",
1845 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1846 if (kerr != KCGI_OK)
1848 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1849 if (kerr != KCGI_OK)
1851 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1852 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1853 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1854 if (kerr != KCGI_OK)
1856 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1857 if (kerr != KCGI_OK)
1859 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1860 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1861 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1862 if (kerr != KCGI_OK)
1864 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1865 if (kerr != KCGI_OK)
1867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1868 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1869 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1870 if (kerr != KCGI_OK)
1872 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1873 if (kerr != KCGI_OK)
1875 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1876 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1878 if (kerr != KCGI_OK)
1880 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1881 if (kerr != KCGI_OK)
1883 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1884 KATTR_REL, "mask-icon", KATTR_HREF,
1885 "/safari-pinned-tab.svg", KATTR__MAX);
1886 if (kerr != KCGI_OK)
1888 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1889 if (kerr != KCGI_OK)
1891 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1892 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1893 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1894 if (kerr != KCGI_OK)
1896 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1897 if (kerr != KCGI_OK)
1901 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1902 KATTR_ID, "got_link", KATTR__MAX);
1903 if (kerr != KCGI_OK)
1905 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1906 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1907 KATTR_TARGET, "_sotd", KATTR__MAX);
1908 if (kerr != KCGI_OK)
1910 if (asprintf(&img_src, "/%s",
1911 gw_trans->gw_conf->got_logo) == -1)
1913 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1914 KATTR_SRC, img_src, KATTR__MAX);
1915 if (kerr != KCGI_OK) {
1919 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1920 if (kerr != KCGI_OK) {
1925 case (TEMPL_SITEPATH):
1926 error = gw_output_site_link(gw_trans);
1931 if (gw_trans->gw_conf->got_site_name != NULL) {
1932 kerr = khtml_puts(gw_trans->gw_html_req,
1933 gw_trans->gw_conf->got_site_name);
1934 if (kerr != KCGI_OK)
1938 case (TEMPL_SEARCH):
1940 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1941 "search", KATTR__MAX);
1942 if (kerr != KCGI_OK)
1944 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1945 KATTR_METHOD, "POST", KATTR__MAX);
1946 if (kerr != KCGI_OK)
1948 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1949 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1950 KATTR_MAXLENGTH, "50", KATTR__MAX);
1951 if (kerr != KCGI_OK)
1953 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1955 if (kerr != KCGI_OK)
1957 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1958 if (kerr != KCGI_OK)
1960 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1961 if (kerr != KCGI_OK)
1964 case(TEMPL_SITEOWNER):
1965 if (gw_trans->gw_conf->got_site_owner != NULL &&
1966 gw_trans->gw_conf->got_show_site_owner) {
1967 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1968 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1969 if (kerr != KCGI_OK)
1971 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1972 KATTR_ID, "site_owner", KATTR__MAX);
1973 if (kerr != KCGI_OK)
1975 kerr = khtml_puts(gw_trans->gw_html_req,
1976 gw_trans->gw_conf->got_site_owner);
1977 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1978 if (kerr != KCGI_OK)
1982 case(TEMPL_CONTENT):
1983 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1985 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1986 KATTR_ID, "tmpl_err", KATTR__MAX);
1987 if (kerr != KCGI_OK)
1989 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1990 if (kerr != KCGI_OK)
1992 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1993 if (kerr != KCGI_OK)
1995 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1996 if (kerr != KCGI_OK)
2006 static const struct got_error *
2007 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2009 const struct got_error *error = NULL;
2010 char *ref_str = NULL;
2011 enum kcgi_err kerr = KCGI_OK;
2013 if (strcmp(str2, "") != 0) {
2014 if (asprintf(&ref_str, "(%s)", str2) == -1) {
2015 error = got_error_from_errno("asprintf");
2019 error = gw_strdup_string(&ref_str, "", NULL);
2024 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2025 KATTR_ID, "header_commit_title", KATTR__MAX);
2026 if (kerr != KCGI_OK)
2028 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2029 if (kerr != KCGI_OK)
2031 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2032 if (kerr != KCGI_OK)
2034 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2035 KATTR_ID, "header_commit", KATTR__MAX);
2036 if (kerr != KCGI_OK)
2038 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2039 if (kerr != KCGI_OK)
2041 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2042 if (kerr != KCGI_OK)
2044 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
2045 if (kerr != KCGI_OK)
2047 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2049 if (error == NULL && kerr != KCGI_OK)
2050 error = gw_kcgi_error(kerr);
2054 static const struct got_error *
2055 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2057 const struct got_error *error = NULL;
2058 enum kcgi_err kerr = KCGI_OK;
2060 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2061 KATTR_ID, "header_diff_title", KATTR__MAX);
2062 if (kerr != KCGI_OK)
2064 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2065 if (kerr != KCGI_OK)
2067 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2068 if (kerr != KCGI_OK)
2070 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2071 KATTR_ID, "header_diff", KATTR__MAX);
2072 if (kerr != KCGI_OK)
2074 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2075 if (kerr != KCGI_OK)
2077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2078 if (kerr != KCGI_OK)
2080 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2081 if (kerr != KCGI_OK)
2083 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2085 if (error == NULL && kerr != KCGI_OK)
2086 error = gw_kcgi_error(kerr);
2090 static const struct got_error *
2091 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2093 const struct got_error *error = NULL;
2094 enum kcgi_err kerr = KCGI_OK;
2096 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2097 KATTR_ID, "header_age_title", KATTR__MAX);
2098 if (kerr != KCGI_OK)
2100 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2101 if (kerr != KCGI_OK)
2103 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2104 if (kerr != KCGI_OK)
2106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2107 KATTR_ID, "header_age", KATTR__MAX);
2108 if (kerr != KCGI_OK)
2110 kerr = khtml_puts(gw_trans->gw_html_req, str);
2111 if (kerr != KCGI_OK)
2113 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2115 if (error == NULL && kerr != KCGI_OK)
2116 error = gw_kcgi_error(kerr);
2120 static const struct got_error *
2121 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2123 const struct got_error *error = NULL;
2126 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2127 KATTR_ID, "header_author_title", KATTR__MAX);
2128 if (kerr != KCGI_OK)
2130 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2131 if (kerr != KCGI_OK)
2133 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2134 if (kerr != KCGI_OK)
2136 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2137 KATTR_ID, "header_author", KATTR__MAX);
2138 if (kerr != KCGI_OK)
2140 kerr = khtml_puts(gw_trans->gw_html_req, str);
2141 if (kerr != KCGI_OK)
2143 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2145 if (error == NULL && kerr != KCGI_OK)
2146 error = gw_kcgi_error(kerr);
2150 static const struct got_error *
2151 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2153 const struct got_error *error = NULL;
2156 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2157 KATTR_ID, "header_committer_title", KATTR__MAX);
2158 if (kerr != KCGI_OK)
2160 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2161 if (kerr != KCGI_OK)
2163 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2164 if (kerr != KCGI_OK)
2166 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2167 KATTR_ID, "header_committer", KATTR__MAX);
2168 if (kerr != KCGI_OK)
2170 kerr = khtml_puts(gw_trans->gw_html_req, str);
2171 if (kerr != KCGI_OK)
2173 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2175 if (error == NULL && kerr != KCGI_OK)
2176 error = gw_kcgi_error(kerr);
2180 static const struct got_error *
2181 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2183 const struct got_error *error = NULL;
2186 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2187 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2188 if (kerr != KCGI_OK)
2190 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2191 if (kerr != KCGI_OK)
2193 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2194 if (kerr != KCGI_OK)
2196 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2197 KATTR_ID, "header_commit_msg", KATTR__MAX);
2198 if (kerr != KCGI_OK)
2200 kerr = khttp_puts(gw_trans->gw_req, str);
2201 if (kerr != KCGI_OK)
2203 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2205 if (error == NULL && kerr != KCGI_OK)
2206 error = gw_kcgi_error(kerr);
2210 static const struct got_error *
2211 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2213 const struct got_error *error = NULL;
2216 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2217 KATTR_ID, "header_tree_title", KATTR__MAX);
2218 if (kerr != KCGI_OK)
2220 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2221 if (kerr != KCGI_OK)
2223 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2224 if (kerr != KCGI_OK)
2226 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2227 KATTR_ID, "header_tree", KATTR__MAX);
2228 if (kerr != KCGI_OK)
2230 kerr = khtml_puts(gw_trans->gw_html_req, str);
2231 if (kerr != KCGI_OK)
2233 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2235 if (error == NULL && kerr != KCGI_OK)
2236 error = gw_kcgi_error(kerr);
2240 static const struct got_error *
2241 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2244 const struct got_error *error = NULL;
2246 char *d_file = NULL;
2250 *description = NULL;
2251 if (gw_trans->gw_conf->got_show_repo_description == 0)
2254 if (asprintf(&d_file, "%s/description", dir) == -1)
2255 return got_error_from_errno("asprintf");
2257 f = fopen(d_file, "r");
2259 if (errno == ENOENT || errno == EACCES)
2261 error = got_error_from_errno2("fopen", d_file);
2265 if (fseek(f, 0, SEEK_END) == -1) {
2266 error = got_ferror(f, GOT_ERR_IO);
2271 error = got_ferror(f, GOT_ERR_IO);
2274 if (fseek(f, 0, SEEK_SET) == -1) {
2275 error = got_ferror(f, GOT_ERR_IO);
2278 *description = calloc(len + 1, sizeof(**description));
2279 if (*description == NULL) {
2280 error = got_error_from_errno("calloc");
2284 n = fread(*description, 1, len, f);
2285 if (n == 0 && ferror(f))
2286 error = got_ferror(f, GOT_ERR_IO);
2288 if (f != NULL && fclose(f) == -1 && error == NULL)
2289 error = got_error_from_errno("fclose");
2294 static const struct got_error *
2295 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2299 char *years = "years ago", *months = "months ago";
2300 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2301 char *minutes = "minutes ago", *seconds = "seconds ago";
2302 char *now = "right now";
2310 diff_time = time(NULL) - committer_time;
2311 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2312 if (asprintf(repo_age, "%lld %s",
2313 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2314 return got_error_from_errno("asprintf");
2315 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2316 if (asprintf(repo_age, "%lld %s",
2317 (diff_time / 60 / 60 / 24 / (365 / 12)),
2319 return got_error_from_errno("asprintf");
2320 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2321 if (asprintf(repo_age, "%lld %s",
2322 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2323 return got_error_from_errno("asprintf");
2324 } else if (diff_time > 60 * 60 * 24 * 2) {
2325 if (asprintf(repo_age, "%lld %s",
2326 (diff_time / 60 / 60 / 24), days) == -1)
2327 return got_error_from_errno("asprintf");
2328 } else if (diff_time > 60 * 60 * 2) {
2329 if (asprintf(repo_age, "%lld %s",
2330 (diff_time / 60 / 60), hours) == -1)
2331 return got_error_from_errno("asprintf");
2332 } else if (diff_time > 60 * 2) {
2333 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2335 return got_error_from_errno("asprintf");
2336 } else if (diff_time > 2) {
2337 if (asprintf(repo_age, "%lld %s", diff_time,
2339 return got_error_from_errno("asprintf");
2341 if (asprintf(repo_age, "%s", now) == -1)
2342 return got_error_from_errno("asprintf");
2346 if (gmtime_r(&committer_time, &tm) == NULL)
2347 return got_error_from_errno("gmtime_r");
2349 s = asctime_r(&tm, datebuf);
2351 return got_error_from_errno("asctime_r");
2353 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2354 return got_error_from_errno("asprintf");
2360 static const struct got_error *
2361 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2362 char *repo_ref, int ref_tm)
2364 const struct got_error *error = NULL;
2365 struct got_object_id *id = NULL;
2366 struct got_repository *repo = NULL;
2367 struct got_commit_object *commit = NULL;
2368 struct got_reflist_head refs;
2369 struct got_reflist_entry *re;
2370 struct got_reference *head_ref;
2372 time_t committer_time = 0, cmp_time = 0;
2376 SIMPLEQ_INIT(&refs);
2378 if (repo_ref == NULL)
2381 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2384 if (gw_trans->gw_conf->got_show_repo_age == 0)
2387 error = got_repo_open(&repo, dir, NULL);
2392 error = got_ref_list(&refs, repo, "refs/heads",
2393 got_ref_cmp_by_name, NULL);
2395 error = got_ref_list(&refs, repo, repo_ref,
2396 got_ref_cmp_by_name, NULL);
2400 SIMPLEQ_FOREACH(re, &refs, entry) {
2402 error = gw_strdup_string(&refname, repo_ref, NULL);
2406 error = gw_strdup_string(&refname, NULL,
2407 got_ref_get_name(re->ref));
2411 error = got_ref_open(&head_ref, repo, refname, 0);
2415 error = got_ref_resolve(&id, repo, head_ref);
2416 got_ref_close(head_ref);
2420 error = got_object_open_as_commit(&commit, repo, id);
2425 got_object_commit_get_committer_time(commit);
2427 if (cmp_time < committer_time)
2428 cmp_time = committer_time;
2431 if (cmp_time != 0) {
2432 committer_time = cmp_time;
2433 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2436 got_ref_list_free(&refs);
2441 static const struct got_error *
2442 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2444 const struct got_error *error;
2446 struct got_object_id *id1 = NULL, *id2 = NULL;
2447 char *label1 = NULL, *label2 = NULL, *line = NULL;
2449 size_t linesize = 0;
2451 enum kcgi_err kerr = KCGI_OK;
2457 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2461 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2462 error = got_repo_match_object_id(&id1, &label1,
2463 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2468 error = got_repo_match_object_id(&id2, &label2,
2469 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2473 error = got_object_get_type(&obj_type, header->repo, id2);
2477 case GOT_OBJ_TYPE_BLOB:
2478 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2481 case GOT_OBJ_TYPE_TREE:
2482 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2485 case GOT_OBJ_TYPE_COMMIT:
2486 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2490 error = got_error(GOT_ERR_OBJ_TYPE);
2495 if (fseek(f, 0, SEEK_SET) == -1) {
2496 error = got_ferror(f, GOT_ERR_IO);
2500 while ((linelen = getline(&line, &linesize, f)) != -1) {
2501 error = gw_colordiff_line(gw_trans, line);
2504 /* XXX: KHTML_PRETTY breaks this */
2505 kerr = khtml_puts(gw_trans->gw_html_req, line);
2506 if (kerr != KCGI_OK)
2508 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2509 if (kerr != KCGI_OK)
2512 if (linelen == -1 && ferror(f))
2513 error = got_error_from_errno("getline");
2515 if (f && fclose(f) == -1 && error == NULL)
2516 error = got_error_from_errno("fclose");
2523 if (error == NULL && kerr != KCGI_OK)
2524 error = gw_kcgi_error(kerr);
2528 static const struct got_error *
2529 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2531 const struct got_error *error = NULL;
2532 struct got_repository *repo;
2533 const char *gitconfig_owner;
2537 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2540 error = got_repo_open(&repo, dir, NULL);
2543 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2544 if (gitconfig_owner)
2545 error = gw_strdup_string(owner, NULL, gitconfig_owner);
2546 got_repo_close(repo);
2550 static const struct got_error *
2551 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2553 const struct got_error *error = NULL;
2555 char *d_file = NULL;
2561 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2562 return got_error_from_errno("asprintf");
2564 f = fopen(d_file, "r");
2566 if (errno != ENOENT && errno != EACCES)
2567 error = got_error_from_errno2("fopen", d_file);
2571 if (fseek(f, 0, SEEK_END) == -1) {
2572 error = got_ferror(f, GOT_ERR_IO);
2577 error = got_ferror(f, GOT_ERR_IO);
2580 if (fseek(f, 0, SEEK_SET) == -1) {
2581 error = got_ferror(f, GOT_ERR_IO);
2585 *url = calloc(len + 1, sizeof(**url));
2587 error = got_error_from_errno("calloc");
2591 n = fread(*url, 1, len, f);
2592 if (n == 0 && ferror(f))
2593 error = got_ferror(f, GOT_ERR_IO);
2595 if (f && fclose(f) == -1 && error == NULL)
2596 error = got_error_from_errno("fclose");
2601 static const struct got_error *
2602 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2603 int limit, int tag_type)
2605 const struct got_error *error = NULL;
2606 struct got_repository *repo = NULL;
2607 struct got_reflist_head refs;
2608 struct got_reflist_entry *re;
2610 char *id_str = NULL, *newline, *href_commits = NULL;
2611 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2612 struct got_tag_object *tag = NULL;
2613 enum kcgi_err kerr = KCGI_OK;
2614 int summary_header_displayed = 0;
2616 SIMPLEQ_INIT(&refs);
2618 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2622 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2626 SIMPLEQ_FOREACH(re, &refs, entry) {
2627 const char *refname;
2629 const char *tag_commit;
2631 struct got_object_id *id;
2632 struct got_commit_object *commit = NULL;
2634 refname = got_ref_get_name(re->ref);
2635 if (strncmp(refname, "refs/tags/", 10) != 0)
2639 error = got_ref_resolve(&id, repo, re->ref);
2643 error = got_object_open_as_tag(&tag, repo, id);
2645 if (error->code != GOT_ERR_OBJ_TYPE) {
2649 /* "lightweight" tag */
2650 error = got_object_open_as_commit(&commit, repo, id);
2655 tagger = got_object_commit_get_committer(commit);
2657 got_object_commit_get_committer_time(commit);
2658 error = got_object_id_str(&id_str, id);
2662 tagger = got_object_tag_get_tagger(tag);
2663 tagger_time = got_object_tag_get_tagger_time(tag);
2664 error = got_object_id_str(&id_str,
2665 got_object_tag_get_object_id(tag));
2670 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2671 strlen(id_str)) != 0)
2675 error = got_object_commit_get_logmsg(&tag_commit0,
2679 got_object_commit_close(commit);
2681 tag_commit0 = strdup(got_object_tag_get_message(tag));
2682 if (tag_commit0 == NULL) {
2683 error = got_error_from_errno("strdup");
2688 tag_commit = tag_commit0;
2689 while (*tag_commit == '\n')
2694 newline = strchr(tag_commit, '\n');
2698 if (summary_header_displayed == 0) {
2699 kerr = khtml_attr(gw_trans->gw_html_req,
2700 KELEM_DIV, KATTR_ID,
2701 "summary_tags_title_wrapper", KATTR__MAX);
2702 if (kerr != KCGI_OK)
2704 kerr = khtml_attr(gw_trans->gw_html_req,
2705 KELEM_DIV, KATTR_ID,
2706 "summary_tags_title", KATTR__MAX);
2707 if (kerr != KCGI_OK)
2709 kerr = khtml_puts(gw_trans->gw_html_req,
2711 if (kerr != KCGI_OK)
2713 kerr = khtml_closeelem(gw_trans->gw_html_req,
2715 if (kerr != KCGI_OK)
2717 kerr = khtml_attr(gw_trans->gw_html_req,
2718 KELEM_DIV, KATTR_ID,
2719 "summary_tags_content", KATTR__MAX);
2720 if (kerr != KCGI_OK)
2722 summary_header_displayed = 1;
2725 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2726 KATTR_ID, "tags_wrapper", KATTR__MAX);
2727 if (kerr != KCGI_OK)
2729 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2730 KATTR_ID, "tags_age", KATTR__MAX);
2731 if (kerr != KCGI_OK)
2733 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2736 kerr = khtml_puts(gw_trans->gw_html_req,
2738 if (kerr != KCGI_OK)
2740 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2741 if (kerr != KCGI_OK)
2743 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2744 KATTR_ID, "tags", KATTR__MAX);
2745 if (kerr != KCGI_OK)
2747 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2748 if (kerr != KCGI_OK)
2750 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2751 if (kerr != KCGI_OK)
2753 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2754 KATTR_ID, "tags_name", KATTR__MAX);
2755 if (kerr != KCGI_OK)
2757 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2758 gw_trans->repo_name, id_str) == -1) {
2759 error = got_error_from_errno("asprintf");
2762 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2763 KATTR_HREF, href_tag, KATTR__MAX);
2764 if (kerr != KCGI_OK)
2766 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2767 if (kerr != KCGI_OK)
2769 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2770 if (kerr != KCGI_OK)
2773 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2774 KATTR_ID, "navs_wrapper", KATTR__MAX);
2775 if (kerr != KCGI_OK)
2777 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2778 KATTR_ID, "navs", KATTR__MAX);
2779 if (kerr != KCGI_OK)
2782 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2783 KATTR_HREF, href_tag, KATTR__MAX);
2784 if (kerr != KCGI_OK)
2786 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2787 if (kerr != KCGI_OK)
2789 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2790 if (kerr != KCGI_OK)
2793 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2794 if (kerr != KCGI_OK)
2796 if (asprintf(&href_briefs,
2797 "?path=%s&action=briefs&commit=%s",
2798 gw_trans->repo_name, id_str) == -1) {
2799 error = got_error_from_errno("asprintf");
2802 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2803 KATTR_HREF, href_briefs, KATTR__MAX);
2804 if (kerr != KCGI_OK)
2806 kerr = khtml_puts(gw_trans->gw_html_req,
2808 if (kerr != KCGI_OK)
2810 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2811 if (kerr != KCGI_OK)
2814 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2815 if (kerr != KCGI_OK)
2818 if (asprintf(&href_commits,
2819 "?path=%s&action=commits&commit=%s",
2820 gw_trans->repo_name, id_str) == -1) {
2821 error = got_error_from_errno("asprintf");
2824 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2825 KATTR_HREF, href_commits, KATTR__MAX);
2826 if (kerr != KCGI_OK)
2828 kerr = khtml_puts(gw_trans->gw_html_req,
2830 if (kerr != KCGI_OK)
2832 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2833 if (kerr != KCGI_OK)
2836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2837 KATTR_ID, "dotted_line", KATTR__MAX);
2838 if (kerr != KCGI_OK)
2840 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2841 if (kerr != KCGI_OK)
2845 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2846 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2847 if (kerr != KCGI_OK)
2849 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2850 if (kerr != KCGI_OK)
2852 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2853 if (kerr != KCGI_OK)
2855 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2856 KATTR_ID, "tag_info_date", KATTR__MAX);
2857 if (kerr != KCGI_OK)
2859 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2862 kerr = khtml_puts(gw_trans->gw_html_req,
2864 if (kerr != KCGI_OK)
2866 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2867 if (kerr != KCGI_OK)
2870 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2871 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2872 if (kerr != KCGI_OK)
2874 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2875 if (kerr != KCGI_OK)
2877 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2878 if (kerr != KCGI_OK)
2880 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2881 KATTR_ID, "tag_info_date", KATTR__MAX);
2882 if (kerr != KCGI_OK)
2884 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2885 if (kerr != KCGI_OK)
2887 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2888 if (kerr != KCGI_OK)
2891 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2892 KATTR_ID, "tag_info", KATTR__MAX);
2893 if (kerr != KCGI_OK)
2895 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2896 if (kerr != KCGI_OK)
2902 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2903 if (kerr != KCGI_OK)
2906 if (limit && --limit == 0)
2910 got_object_tag_close(tag);
2923 href_commits = NULL;
2927 got_object_tag_close(tag);
2934 got_ref_list_free(&refs);
2936 got_repo_close(repo);
2937 if (error == NULL && kerr != KCGI_OK)
2938 error = gw_kcgi_error(kerr);
2943 gw_free_headers(struct gw_header *header)
2947 if (header->commit != NULL)
2948 got_object_commit_close(header->commit);
2950 got_repo_close(header->repo);
2951 free(header->refs_str);
2952 free(header->commit_id);
2953 free(header->author);
2954 free(header->committer);
2955 free(header->parent_id);
2956 free(header->tree_id);
2957 free(header->commit_msg);
2960 static struct gw_header *
2963 struct gw_header *header;
2965 header = malloc(sizeof(*header));
2969 header->repo = NULL;
2970 header->commit = NULL;
2972 header->path = NULL;
2973 SIMPLEQ_INIT(&header->refs);
2978 static const struct got_error *
2979 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2982 const struct got_error *error = NULL;
2983 struct got_commit_graph *graph = NULL;
2985 error = got_commit_graph_open(&graph, header->path, 0);
2989 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2995 error = got_commit_graph_iter_next(&header->id, graph,
2996 header->repo, NULL, NULL);
2998 if (error->code == GOT_ERR_ITER_COMPLETED)
3002 if (header->id == NULL)
3005 error = got_object_open_as_commit(&header->commit, header->repo,
3010 error = gw_get_commit(gw_trans, header);
3012 struct gw_header *n_header = NULL;
3013 if ((n_header = gw_init_header()) == NULL) {
3014 error = got_error_from_errno("malloc");
3018 error = gw_strdup_string(&n_header->refs_str,
3019 header->refs_str, NULL);
3022 error = gw_strdup_string(&n_header->commit_id,
3023 header->commit_id, NULL);
3026 error = gw_strdup_string(&n_header->parent_id,
3027 header->parent_id, NULL);
3030 error = gw_strdup_string(&n_header->tree_id,
3031 header->tree_id, NULL);
3034 error = gw_strdup_string(&n_header->author,
3035 header->author, NULL);
3038 error = gw_strdup_string(&n_header->committer,
3039 header->committer, NULL);
3042 error = gw_strdup_string(&n_header->commit_msg,
3043 header->commit_msg, NULL);
3046 n_header->committer_time = header->committer_time;
3047 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3050 if (error || (limit && --limit == 0))
3055 got_commit_graph_close(graph);
3059 static const struct got_error *
3060 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3062 const struct got_error *error = NULL;
3063 struct got_reflist_entry *re;
3064 struct got_object_id *id2 = NULL;
3065 struct got_object_qid *parent_id;
3066 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
3069 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3072 struct got_tag_object *tag = NULL;
3075 name = got_ref_get_name(re->ref);
3076 if (strcmp(name, GOT_REF_HEAD) == 0)
3078 if (strncmp(name, "refs/", 5) == 0)
3080 if (strncmp(name, "got/", 4) == 0)
3082 if (strncmp(name, "heads/", 6) == 0)
3084 if (strncmp(name, "remotes/", 8) == 0)
3086 if (strncmp(name, "tags/", 5) == 0) {
3087 error = got_object_open_as_tag(&tag, header->repo,
3090 if (error->code != GOT_ERR_OBJ_TYPE)
3093 * Ref points at something other
3100 cmp = got_object_id_cmp(tag ?
3101 got_object_tag_get_object_id(tag) : re->id, header->id);
3103 got_object_tag_close(tag);
3107 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
3108 s ? ", " : "", name) == -1) {
3109 error = got_error_from_errno("asprintf");
3113 error = gw_strdup_string(&header->refs_str, refs_str, NULL);
3119 if (refs_str == NULL) {
3120 error = gw_strdup_string(&header->refs_str, "", NULL);
3126 error = got_object_id_str(&header->commit_id, header->id);
3130 error = got_object_id_str(&header->tree_id,
3131 got_object_commit_get_tree_id(header->commit));
3135 if (gw_trans->action == GW_DIFF) {
3136 parent_id = SIMPLEQ_FIRST(
3137 got_object_commit_get_parent_ids(header->commit));
3138 if (parent_id != NULL) {
3139 id2 = got_object_id_dup(parent_id->id);
3141 error = got_object_id_str(&header->parent_id, id2);
3146 error = gw_strdup_string(&header->parent_id,
3152 error = gw_strdup_string(&header->parent_id, "", NULL);
3157 header->committer_time =
3158 got_object_commit_get_committer_time(header->commit);
3160 error = gw_strdup_string(&header->author, NULL,
3161 got_object_commit_get_author(header->commit));
3164 error = gw_strdup_string(&header->committer, NULL,
3165 got_object_commit_get_committer(header->commit));
3168 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3172 commit_msg = commit_msg0;
3173 while (*commit_msg == '\n')
3176 error = gw_strdup_string(&header->commit_msg, commit_msg, NULL);
3181 static const struct got_error *
3182 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3184 const struct got_error *error = NULL;
3185 char *in_repo_path = NULL;
3187 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3191 if (gw_trans->commit == NULL) {
3192 struct got_reference *head_ref;
3193 error = got_ref_open(&head_ref, header->repo,
3194 gw_trans->headref, 0);
3198 error = got_ref_resolve(&header->id, header->repo, head_ref);
3199 got_ref_close(head_ref);
3203 error = got_object_open_as_commit(&header->commit,
3204 header->repo, header->id);
3206 struct got_reference *ref;
3207 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3208 if (error == NULL) {
3210 error = got_ref_resolve(&header->id, header->repo, ref);
3214 error = got_object_get_type(&obj_type, header->repo,
3218 if (obj_type == GOT_OBJ_TYPE_TAG) {
3219 struct got_tag_object *tag;
3220 error = got_object_open_as_tag(&tag,
3221 header->repo, header->id);
3224 if (got_object_tag_get_object_type(tag) !=
3225 GOT_OBJ_TYPE_COMMIT) {
3226 got_object_tag_close(tag);
3227 error = got_error(GOT_ERR_OBJ_TYPE);
3231 header->id = got_object_id_dup(
3232 got_object_tag_get_object_id(tag));
3233 if (header->id == NULL)
3234 error = got_error_from_errno(
3235 "got_object_id_dup");
3236 got_object_tag_close(tag);
3239 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3240 error = got_error(GOT_ERR_OBJ_TYPE);
3243 error = got_object_open_as_commit(&header->commit,
3244 header->repo, header->id);
3248 if (header->commit == NULL) {
3249 error = got_repo_match_object_id_prefix(&header->id,
3250 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3255 error = got_repo_match_object_id_prefix(&header->id,
3256 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3260 error = got_repo_map_path(&in_repo_path, header->repo,
3261 gw_trans->repo_path, 1);
3266 error = gw_strdup_string(&header->path, in_repo_path, NULL);
3274 error = got_ref_list(&header->refs, header->repo, NULL,
3275 got_ref_cmp_by_name, NULL);
3279 error = gw_get_commits(gw_trans, header, limit);
3287 char datebuf[11]; /* YYYY-MM-DD + NUL */
3290 struct gw_blame_cb_args {
3291 struct blame_line *lines;
3295 off_t *line_offsets;
3297 struct got_repository *repo;
3298 struct gw_trans *gw_trans;
3301 static const struct got_error *
3302 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3304 const struct got_error *err = NULL;
3305 struct gw_blame_cb_args *a = arg;
3306 struct blame_line *bline;
3308 size_t linesize = 0;
3309 struct got_commit_object *commit = NULL;
3312 time_t committer_time;
3313 enum kcgi_err kerr = KCGI_OK;
3315 if (nlines != a->nlines ||
3316 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3317 return got_error(GOT_ERR_RANGE);
3320 return NULL; /* no change in this commit */
3322 /* Annotate this line. */
3323 bline = &a->lines[lineno - 1];
3324 if (bline->annotated)
3326 err = got_object_id_str(&bline->id_str, id);
3330 err = got_object_open_as_commit(&commit, a->repo, id);
3334 err = gw_strdup_string(&bline->committer, NULL,
3335 got_object_commit_get_committer(commit));
3339 committer_time = got_object_commit_get_committer_time(commit);
3340 if (localtime_r(&committer_time, &tm) == NULL)
3341 return got_error_from_errno("localtime_r");
3342 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3343 &tm) >= sizeof(bline->datebuf)) {
3344 err = got_error(GOT_ERR_NO_SPACE);
3347 bline->annotated = 1;
3349 /* Print lines annotated so far. */
3350 bline = &a->lines[a->lineno_cur - 1];
3351 if (!bline->annotated)
3354 offset = a->line_offsets[a->lineno_cur - 1];
3355 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3356 err = got_error_from_errno("fseeko");
3360 while (bline->annotated) {
3361 char *smallerthan, *at, *nl, *committer;
3362 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3365 if (getline(&line, &linesize, a->f) == -1) {
3367 err = got_error_from_errno("getline");
3371 committer = bline->committer;
3372 smallerthan = strchr(committer, '<');
3373 if (smallerthan && smallerthan[1] != '\0')
3374 committer = smallerthan + 1;
3375 at = strchr(committer, '@');
3378 len = strlen(committer);
3380 committer[8] = '\0';
3382 nl = strchr(line, '\n');
3386 if (a->gw_trans->repo_folder == NULL) {
3387 err = gw_strdup_string(&a->gw_trans->repo_folder, "",
3392 if (a->gw_trans->repo_folder == NULL)
3395 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3396 "blame_wrapper", KATTR__MAX);
3397 if (kerr != KCGI_OK)
3399 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3400 "blame_number", KATTR__MAX);
3401 if (kerr != KCGI_OK)
3403 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3404 a->lineno_cur) == -1)
3406 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3407 if (kerr != KCGI_OK)
3409 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3410 if (kerr != KCGI_OK)
3413 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3414 "blame_hash", KATTR__MAX);
3415 if (kerr != KCGI_OK)
3417 if (asprintf(&href_diff,
3418 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3419 a->gw_trans->repo_name, bline->id_str,
3420 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3421 err = got_error_from_errno("asprintf");
3424 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3425 err = got_error_from_errno("asprintf");
3428 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3429 KATTR_HREF, href_diff, KATTR__MAX);
3430 if (kerr != KCGI_OK)
3432 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3433 if (kerr != KCGI_OK)
3435 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3436 if (kerr != KCGI_OK)
3439 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3440 "blame_date", KATTR__MAX);
3441 if (kerr != KCGI_OK)
3443 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3444 if (kerr != KCGI_OK)
3446 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3447 if (kerr != KCGI_OK)
3450 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3451 "blame_author", KATTR__MAX);
3452 if (kerr != KCGI_OK)
3454 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3455 if (kerr != KCGI_OK)
3457 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3458 if (kerr != KCGI_OK)
3461 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3462 "blame_code", KATTR__MAX);
3463 if (kerr != KCGI_OK)
3465 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3466 if (kerr != KCGI_OK)
3468 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3469 if (kerr != KCGI_OK)
3472 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3473 if (kerr != KCGI_OK)
3477 bline = &a->lines[a->lineno_cur - 1];
3485 got_object_commit_close(commit);
3487 if (err == NULL && kerr != KCGI_OK)
3488 err = gw_kcgi_error(kerr);
3492 static const struct got_error *
3493 gw_output_file_blame(struct gw_trans *gw_trans)
3495 const struct got_error *error = NULL;
3496 struct got_repository *repo = NULL;
3497 struct got_object_id *obj_id = NULL;
3498 struct got_object_id *commit_id = NULL;
3499 struct got_blob_object *blob = NULL;
3500 char *path = NULL, *in_repo_path = NULL;
3501 struct gw_blame_cb_args bca;
3505 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3509 if (asprintf(&path, "%s%s%s",
3510 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3511 gw_trans->repo_folder ? "/" : "",
3512 gw_trans->repo_file) == -1) {
3513 error = got_error_from_errno("asprintf");
3517 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3521 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3522 GOT_OBJ_TYPE_COMMIT, 1, repo);
3526 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3530 if (obj_id == NULL) {
3531 error = got_error(GOT_ERR_NO_OBJ);
3535 error = got_object_get_type(&obj_type, repo, obj_id);
3539 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3540 error = got_error(GOT_ERR_OBJ_TYPE);
3544 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3548 bca.f = got_opentemp();
3549 if (bca.f == NULL) {
3550 error = got_error_from_errno("got_opentemp");
3553 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3554 &bca.line_offsets, bca.f, blob);
3555 if (error || bca.nlines == 0)
3558 /* Don't include \n at EOF in the blame line count. */
3559 if (bca.line_offsets[bca.nlines - 1] == filesize)
3562 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3563 if (bca.lines == NULL) {
3564 error = got_error_from_errno("calloc");
3568 bca.nlines_prec = 0;
3575 bca.gw_trans = gw_trans;
3577 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3580 free(bca.line_offsets);
3586 for (i = 0; i < bca.nlines; i++) {
3587 struct blame_line *bline = &bca.lines[i];
3588 free(bline->id_str);
3589 free(bline->committer);
3592 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3593 error = got_error_from_errno("fclose");
3595 got_object_blob_close(blob);
3597 got_repo_close(repo);
3601 static const struct got_error *
3602 gw_output_blob_buf(struct gw_trans *gw_trans)
3604 const struct got_error *error = NULL;
3605 struct got_repository *repo = NULL;
3606 struct got_object_id *obj_id = NULL;
3607 struct got_object_id *commit_id = NULL;
3608 struct got_blob_object *blob = NULL;
3609 char *path = NULL, *in_repo_path = NULL;
3610 int obj_type, set_mime = 0;
3613 enum kcgi_err kerr = KCGI_OK;
3615 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3619 if (asprintf(&path, "%s%s%s",
3620 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3621 gw_trans->repo_folder ? "/" : "",
3622 gw_trans->repo_file) == -1) {
3623 error = got_error_from_errno("asprintf");
3627 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3631 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3632 GOT_OBJ_TYPE_COMMIT, 1, repo);
3636 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3640 if (obj_id == NULL) {
3641 error = got_error(GOT_ERR_NO_OBJ);
3645 error = got_object_get_type(&obj_type, repo, obj_id);
3649 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3650 error = got_error(GOT_ERR_OBJ_TYPE);
3654 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3658 hdrlen = got_object_blob_get_hdrlen(blob);
3660 error = got_object_blob_read_block(&len, blob);
3663 buf = got_object_blob_get_read_buf(blob);
3666 * Skip blob object header first time around,
3667 * which also contains a zero byte.
3670 if (set_mime == 0) {
3671 if (isbinary(buf, len - hdrlen))
3672 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3674 gw_trans->mime = KMIME_TEXT_PLAIN;
3676 error = gw_display_index(gw_trans);
3680 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3689 got_object_blob_close(blob);
3691 got_repo_close(repo);
3692 if (error == NULL && kerr != KCGI_OK)
3693 error = gw_kcgi_error(kerr);
3697 static const struct got_error *
3698 gw_output_repo_tree(struct gw_trans *gw_trans)
3700 const struct got_error *error = NULL;
3701 struct got_repository *repo = NULL;
3702 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3703 struct got_tree_object *tree = NULL;
3704 char *path = NULL, *in_repo_path = NULL;
3705 char *id_str = NULL;
3706 char *build_folder = NULL;
3707 char *href_blob = NULL, *href_blame = NULL;
3708 const char *class = NULL;
3709 int nentries, i, class_flip = 0;
3710 enum kcgi_err kerr = KCGI_OK;
3712 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3716 if (gw_trans->repo_folder != NULL) {
3717 error = gw_strdup_string(&path, gw_trans->repo_folder, NULL);
3721 error = got_repo_map_path(&in_repo_path, repo,
3722 gw_trans->repo_path, 1);
3726 path = in_repo_path;
3729 if (gw_trans->commit == NULL) {
3730 struct got_reference *head_ref;
3731 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3734 error = got_ref_resolve(&commit_id, repo, head_ref);
3737 got_ref_close(head_ref);
3740 error = got_repo_match_object_id(&commit_id, NULL,
3741 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3746 error = got_object_id_str(&gw_trans->commit, commit_id);
3750 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3754 error = got_object_open_as_tree(&tree, repo, tree_id);
3758 nentries = got_object_tree_get_nentries(tree);
3759 for (i = 0; i < nentries; i++) {
3760 struct got_tree_entry *te;
3761 const char *modestr = "";
3764 te = got_object_tree_get_entry(tree, i);
3766 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3770 mode = got_tree_entry_get_mode(te);
3771 if (got_object_tree_entry_is_submodule(te))
3773 else if (S_ISLNK(mode))
3775 else if (S_ISDIR(mode))
3777 else if (mode & S_IXUSR)
3780 if (class_flip == 0) {
3781 class = "back_lightgray";
3784 class = "back_white";
3788 if (S_ISDIR(mode)) {
3789 if (asprintf(&build_folder, "%s/%s",
3790 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3791 got_tree_entry_get_name(te)) == -1) {
3792 error = got_error_from_errno(
3796 if (asprintf(&href_blob,
3797 "?path=%s&action=%s&commit=%s&folder=%s",
3798 gw_trans->repo_name, gw_trans->action_name,
3799 gw_trans->commit, build_folder) == -1) {
3800 error = got_error_from_errno("asprintf");
3804 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3805 KATTR_ID, "tree_wrapper", KATTR__MAX);
3806 if (kerr != KCGI_OK)
3808 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3809 KATTR_ID, "tree_line", KATTR_CLASS, class,
3811 if (kerr != KCGI_OK)
3813 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3814 KATTR_HREF, href_blob, KATTR_CLASS,
3815 "diff_directory", KATTR__MAX);
3816 if (kerr != KCGI_OK)
3818 kerr = khtml_puts(gw_trans->gw_html_req,
3819 got_tree_entry_get_name(te));
3820 if (kerr != KCGI_OK)
3822 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3823 if (kerr != KCGI_OK)
3825 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3826 if (kerr != KCGI_OK)
3828 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3829 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3831 if (kerr != KCGI_OK)
3833 kerr = khtml_entity(gw_trans->gw_html_req,
3835 if (kerr != KCGI_OK)
3837 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3838 if (kerr != KCGI_OK)
3841 if (asprintf(&href_blob,
3842 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3843 gw_trans->repo_name, "blob", gw_trans->commit,
3844 got_tree_entry_get_name(te),
3845 gw_trans->repo_folder ?
3846 gw_trans->repo_folder : "") == -1) {
3847 error = got_error_from_errno("asprintf");
3850 if (asprintf(&href_blame,
3851 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3852 gw_trans->repo_name, "blame", gw_trans->commit,
3853 got_tree_entry_get_name(te),
3854 gw_trans->repo_folder ?
3855 gw_trans->repo_folder : "") == -1) {
3856 error = got_error_from_errno("asprintf");
3860 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3861 KATTR_ID, "tree_wrapper", KATTR__MAX);
3862 if (kerr != KCGI_OK)
3864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3865 KATTR_ID, "tree_line", KATTR_CLASS, class,
3867 if (kerr != KCGI_OK)
3869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3870 KATTR_HREF, href_blob, KATTR__MAX);
3871 if (kerr != KCGI_OK)
3873 kerr = khtml_puts(gw_trans->gw_html_req,
3874 got_tree_entry_get_name(te));
3875 if (kerr != KCGI_OK)
3877 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3878 if (kerr != KCGI_OK)
3880 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3881 if (kerr != KCGI_OK)
3883 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3884 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3886 if (kerr != KCGI_OK)
3889 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3890 KATTR_HREF, href_blob, KATTR__MAX);
3891 if (kerr != KCGI_OK)
3893 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3894 if (kerr != KCGI_OK)
3896 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3897 if (kerr != KCGI_OK)
3900 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3901 if (kerr != KCGI_OK)
3904 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3905 KATTR_HREF, href_blame, KATTR__MAX);
3906 if (kerr != KCGI_OK)
3908 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3909 if (kerr != KCGI_OK)
3912 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3913 if (kerr != KCGI_OK)
3921 build_folder = NULL;
3925 got_object_tree_close(tree);
3927 got_repo_close(repo);
3934 if (error == NULL && kerr != KCGI_OK)
3935 error = gw_kcgi_error(kerr);
3939 static const struct got_error *
3940 gw_output_repo_heads(struct gw_trans *gw_trans)
3942 const struct got_error *error = NULL;
3943 struct got_repository *repo = NULL;
3944 struct got_reflist_head refs;
3945 struct got_reflist_entry *re;
3946 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3947 char *href_commits = NULL;
3948 enum kcgi_err kerr = KCGI_OK;
3950 SIMPLEQ_INIT(&refs);
3952 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3956 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3961 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3962 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3963 if (kerr != KCGI_OK)
3965 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3966 KATTR_ID, "summary_heads_title", KATTR__MAX);
3967 if (kerr != KCGI_OK)
3969 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3970 if (kerr != KCGI_OK)
3972 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3973 if (kerr != KCGI_OK)
3975 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3976 KATTR_ID, "summary_heads_content", KATTR__MAX);
3977 if (kerr != KCGI_OK)
3980 SIMPLEQ_FOREACH(re, &refs, entry) {
3983 error = gw_strdup_string(&refname, NULL,
3984 got_ref_get_name(re->ref));
3988 if (strncmp(refname, "refs/heads/", 11) != 0) {
3993 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3998 if (strncmp(refname, "refs/heads/", 11) == 0)
4001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4002 KATTR_ID, "heads_wrapper", KATTR__MAX);
4003 if (kerr != KCGI_OK)
4005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4006 KATTR_ID, "heads_age", KATTR__MAX);
4007 if (kerr != KCGI_OK)
4009 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4010 if (kerr != KCGI_OK)
4012 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4013 if (kerr != KCGI_OK)
4015 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4016 KATTR_ID, "heads_space", KATTR__MAX);
4017 if (kerr != KCGI_OK)
4019 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4020 if (kerr != KCGI_OK)
4022 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4023 if (kerr != KCGI_OK)
4025 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4026 KATTR_ID, "head", KATTR__MAX);
4027 if (kerr != KCGI_OK)
4029 if (asprintf(&href_summary,
4030 "?path=%s&action=summary&headref=%s",
4031 gw_trans->repo_name, refname) == -1) {
4032 error = got_error_from_errno("asprintf");
4035 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4036 href_summary, KATTR__MAX);
4037 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4038 if (kerr != KCGI_OK)
4040 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4041 if (kerr != KCGI_OK)
4044 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4045 "navs_wrapper", KATTR__MAX);
4046 if (kerr != KCGI_OK)
4048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4049 "navs", KATTR__MAX);
4050 if (kerr != KCGI_OK)
4053 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4054 href_summary, KATTR__MAX);
4055 if (kerr != KCGI_OK)
4057 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4058 if (kerr != KCGI_OK)
4060 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4061 if (kerr != KCGI_OK)
4064 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4065 if (kerr != KCGI_OK)
4067 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4068 gw_trans->repo_name, refname) == -1) {
4069 error = got_error_from_errno("asprintf");
4072 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4073 href_briefs, KATTR__MAX);
4074 if (kerr != KCGI_OK)
4076 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4077 if (kerr != KCGI_OK)
4079 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4080 if (kerr != KCGI_OK)
4083 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4084 if (kerr != KCGI_OK)
4087 if (asprintf(&href_commits,
4088 "?path=%s&action=commits&headref=%s",
4089 gw_trans->repo_name, refname) == -1) {
4090 error = got_error_from_errno("asprintf");
4093 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4094 href_commits, KATTR__MAX);
4095 if (kerr != KCGI_OK)
4097 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4098 if (kerr != KCGI_OK)
4100 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4101 if (kerr != KCGI_OK)
4104 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4105 "dotted_line", KATTR__MAX);
4106 if (kerr != KCGI_OK)
4108 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4109 if (kerr != KCGI_OK)
4112 href_summary = NULL;
4116 href_commits = NULL;
4119 got_ref_list_free(&refs);
4124 got_repo_close(repo);
4128 static const struct got_error *
4129 gw_output_site_link(struct gw_trans *gw_trans)
4131 const struct got_error *error = NULL;
4132 char *href_summary = NULL;
4133 enum kcgi_err kerr = KCGI_OK;
4135 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4136 "site_link", KATTR__MAX);
4137 if (kerr != KCGI_OK)
4139 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4141 if (kerr != KCGI_OK)
4143 kerr = khtml_puts(gw_trans->gw_html_req,
4144 gw_trans->gw_conf->got_site_link);
4145 if (kerr != KCGI_OK)
4147 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4148 if (kerr != KCGI_OK)
4151 if (gw_trans->repo_name != NULL) {
4152 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4153 if (kerr != KCGI_OK)
4155 if (asprintf(&href_summary, "?path=%s&action=summary",
4156 gw_trans->repo_name) == -1)
4158 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4159 href_summary, KATTR__MAX);
4160 if (kerr != KCGI_OK)
4162 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4163 if (kerr != KCGI_OK)
4165 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4166 if (kerr != KCGI_OK)
4168 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4169 if (kerr != KCGI_OK)
4171 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->action_name);
4172 if (kerr != KCGI_OK)
4176 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4177 if (kerr != KCGI_OK)
4184 static const struct got_error *
4185 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4187 const struct got_error *error = NULL;
4189 enum kcgi_err kerr = KCGI_OK;
4191 if (strncmp(buf, "-", 1) == 0)
4192 color = "diff_minus";
4193 else if (strncmp(buf, "+", 1) == 0)
4194 color = "diff_plus";
4195 else if (strncmp(buf, "@@", 2) == 0)
4196 color = "diff_chunk_header";
4197 else if (strncmp(buf, "@@", 2) == 0)
4198 color = "diff_chunk_header";
4199 else if (strncmp(buf, "commit +", 8) == 0)
4200 color = "diff_meta";
4201 else if (strncmp(buf, "commit -", 8) == 0)
4202 color = "diff_meta";
4203 else if (strncmp(buf, "blob +", 6) == 0)
4204 color = "diff_meta";
4205 else if (strncmp(buf, "blob -", 6) == 0)
4206 color = "diff_meta";
4207 else if (strncmp(buf, "file +", 6) == 0)
4208 color = "diff_meta";
4209 else if (strncmp(buf, "file -", 6) == 0)
4210 color = "diff_meta";
4211 else if (strncmp(buf, "from:", 5) == 0)
4212 color = "diff_author";
4213 else if (strncmp(buf, "via:", 4) == 0)
4214 color = "diff_author";
4215 else if (strncmp(buf, "date:", 5) == 0)
4216 color = "diff_date";
4217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4218 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4219 if (error == NULL && kerr != KCGI_OK)
4220 error = gw_kcgi_error(kerr);
4225 main(int argc, char *argv[])
4227 const struct got_error *error = NULL;
4228 struct gw_trans *gw_trans;
4229 struct gw_dir *dir = NULL, *tdir;
4230 const char *page = "index";
4234 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4237 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4240 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4243 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4246 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4247 if (kerr != KCGI_OK) {
4248 error = gw_kcgi_error(kerr);
4252 if ((gw_trans->gw_conf =
4253 malloc(sizeof(struct gotweb_conf))) == NULL) {
4255 error = got_error_from_errno("malloc");
4259 TAILQ_INIT(&gw_trans->gw_dirs);
4260 TAILQ_INIT(&gw_trans->gw_headers);
4263 gw_trans->repos_total = 0;
4264 gw_trans->repo_path = NULL;
4265 gw_trans->commit = NULL;
4266 error = gw_strdup_string(&gw_trans->headref, GOT_REF_HEAD, NULL);
4269 gw_trans->mime = KMIME_TEXT_HTML;
4270 gw_trans->gw_tmpl->key = gw_templs;
4271 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4272 gw_trans->gw_tmpl->arg = gw_trans;
4273 gw_trans->gw_tmpl->cb = gw_template;
4274 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4278 error = gw_parse_querystring(gw_trans);
4282 if (gw_trans->action == GW_BLOB)
4283 error = gw_blob(gw_trans);
4285 error = gw_display_index(gw_trans);
4288 gw_trans->mime = KMIME_TEXT_PLAIN;
4289 gw_trans->action = GW_ERR;
4290 gw_display_error(gw_trans, error);
4293 free(gw_trans->gw_conf->got_repos_path);
4294 free(gw_trans->gw_conf->got_site_name);
4295 free(gw_trans->gw_conf->got_site_owner);
4296 free(gw_trans->gw_conf->got_site_link);
4297 free(gw_trans->gw_conf->got_logo);
4298 free(gw_trans->gw_conf->got_logo_url);
4299 free(gw_trans->gw_conf);
4300 free(gw_trans->commit);
4301 free(gw_trans->repo_path);
4302 free(gw_trans->repo_name);
4303 free(gw_trans->repo_file);
4304 free(gw_trans->action_name);
4305 free(gw_trans->headref);
4307 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4309 free(dir->description);
4318 khttp_free(gw_trans->gw_req);