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 struct gw_dir *gw_init_gw_dir(char *);
157 static struct gw_header *gw_init_header(void);
159 static const struct got_error *gw_strdup_string(char **, char *,
161 static const struct got_error *gw_get_repo_description(char **,
162 struct gw_trans *, char *);
163 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
165 static const struct got_error *gw_get_time_str(char **, time_t, int);
166 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
167 char *, char *, int);
168 static const struct got_error *gw_output_file_blame(struct gw_trans *);
169 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
170 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
171 static const struct got_error *gw_output_diff(struct gw_trans *,
173 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
174 struct gw_header *, int, int);
175 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
176 static const struct got_error *gw_output_site_link(struct gw_trans *);
177 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
179 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
181 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
183 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
185 static const struct got_error *gw_gen_author_header(struct gw_trans *,
187 static const struct got_error *gw_gen_age_header(struct gw_trans *,
189 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
191 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
193 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
195 static void gw_free_headers(struct gw_header *);
196 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
198 static const struct got_error* gw_display_index(struct gw_trans *);
199 static void gw_display_error(struct gw_trans *,
200 const struct got_error *);
202 static int gw_template(size_t, void *);
204 static const struct got_error* gw_get_header(struct gw_trans *,
205 struct gw_header *, int);
206 static const struct got_error* gw_get_commits(struct gw_trans *,
207 struct gw_header *, int);
208 static const struct got_error* gw_get_commit(struct gw_trans *,
210 static const struct got_error* gw_apply_unveil(const char *, const char *);
211 static const struct got_error* gw_blame_cb(void *, int, int,
212 struct got_object_id *);
213 static const struct got_error* gw_load_got_paths(struct gw_trans *);
214 static const struct got_error* gw_load_got_path(struct gw_trans *,
216 static const struct got_error* gw_parse_querystring(struct gw_trans *);
218 static const struct got_error* gw_blame(struct gw_trans *);
219 static const struct got_error* gw_blob(struct gw_trans *);
220 static const struct got_error* gw_diff(struct gw_trans *);
221 static const struct got_error* gw_index(struct gw_trans *);
222 static const struct got_error* gw_commits(struct gw_trans *);
223 static const struct got_error* gw_briefs(struct gw_trans *);
224 static const struct got_error* gw_summary(struct gw_trans *);
225 static const struct got_error* gw_tree(struct gw_trans *);
226 static const struct got_error* gw_tag(struct gw_trans *);
228 struct gw_query_action {
229 unsigned int func_id;
230 const char *func_name;
231 const struct got_error *(*func_main)(struct gw_trans *);
235 enum gw_query_actions {
248 static struct gw_query_action gw_query_funcs[] = {
249 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
250 { GW_BLOB, "blob", NULL, NULL },
251 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
252 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
253 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
254 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
255 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
256 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
257 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
258 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
261 static const struct got_error *
262 gw_kcgi_error(enum kcgi_err kerr)
267 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
268 return got_error(GOT_ERR_CANCELLED);
270 if (kerr == KCGI_ENOMEM)
271 return got_error_set_errno(ENOMEM,
272 kcgi_strerror(kerr));
274 if (kerr == KCGI_ENFILE)
275 return got_error_set_errno(ENFILE,
276 kcgi_strerror(kerr));
278 if (kerr == KCGI_EAGAIN)
279 return got_error_set_errno(EAGAIN,
280 kcgi_strerror(kerr));
282 if (kerr == KCGI_FORM)
283 return got_error_msg(GOT_ERR_IO,
284 kcgi_strerror(kerr));
286 return got_error_from_errno(kcgi_strerror(kerr));
289 static const struct got_error *
290 gw_apply_unveil(const char *repo_path, const char *repo_file)
292 const struct got_error *err;
294 if (repo_path && repo_file) {
296 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
297 return got_error_from_errno("asprintf unveil");
298 if (unveil(full_path, "r") != 0)
299 return got_error_from_errno2("unveil", full_path);
302 if (repo_path && unveil(repo_path, "r") != 0)
303 return got_error_from_errno2("unveil", repo_path);
305 if (unveil("/tmp", "rwc") != 0)
306 return got_error_from_errno2("unveil", "/tmp");
308 err = got_privsep_unveil_exec_helpers();
312 if (unveil(NULL, NULL) != 0)
313 return got_error_from_errno("unveil");
318 static const struct got_error *
319 gw_strdup_string(char **s, char *str1, const char *str2)
322 return got_error_from_errno("strdup");
329 return got_error_from_errno("strdup");
334 isbinary(const uint8_t *buf, size_t n)
338 for (i = 0; i < n; i++)
344 static const struct got_error *
345 gw_blame(struct gw_trans *gw_trans)
347 const struct got_error *error = NULL;
348 struct gw_header *header = NULL;
352 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
354 return got_error_from_errno("pledge");
356 if ((header = gw_init_header()) == NULL)
357 return got_error_from_errno("malloc");
359 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
363 error = gw_get_header(gw_trans, header, 1);
367 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
368 "blame_header_wrapper", KATTR__MAX);
371 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
372 "blame_header", KATTR__MAX);
375 error = gw_get_time_str(&age, header->committer_time,
379 error = gw_gen_age_header(gw_trans, age ?age : "");
382 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
385 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
388 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
389 "dotted_line", KATTR__MAX);
392 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
396 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
397 "blame", KATTR__MAX);
400 error = gw_output_file_blame(gw_trans);
403 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
405 got_ref_list_free(&header->refs);
406 gw_free_headers(header);
407 if (error == NULL && kerr != KCGI_OK)
408 error = gw_kcgi_error(kerr);
412 static const struct got_error *
413 gw_blob(struct gw_trans *gw_trans)
415 const struct got_error *error = NULL;
416 struct gw_header *header = NULL;
418 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
420 return got_error_from_errno("pledge");
422 if ((header = gw_init_header()) == NULL)
423 return got_error_from_errno("malloc");
425 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
429 error = gw_get_header(gw_trans, header, 1);
433 error = gw_output_blob_buf(gw_trans);
435 got_ref_list_free(&header->refs);
436 gw_free_headers(header);
440 static const struct got_error *
441 gw_diff(struct gw_trans *gw_trans)
443 const struct got_error *error = NULL;
444 struct gw_header *header = NULL;
446 enum kcgi_err kerr = KCGI_OK;
448 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
450 return got_error_from_errno("pledge");
452 if ((header = gw_init_header()) == NULL)
453 return got_error_from_errno("malloc");
455 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
459 error = gw_get_header(gw_trans, header, 1);
463 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
464 "diff_header_wrapper", KATTR__MAX);
467 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
468 "diff_header", KATTR__MAX);
471 error = gw_gen_diff_header(gw_trans, header->parent_id,
475 error = gw_gen_commit_header(gw_trans, header->commit_id,
479 error = gw_gen_tree_header(gw_trans, header->tree_id);
482 error = gw_gen_author_header(gw_trans, header->author);
485 error = gw_gen_committer_header(gw_trans, header->author);
488 error = gw_get_time_str(&age, header->committer_time,
492 error = gw_gen_age_header(gw_trans, age ?age : "");
495 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
498 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
501 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
502 "dotted_line", KATTR__MAX);
505 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
509 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
513 error = gw_output_diff(gw_trans, header);
517 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
521 got_ref_list_free(&header->refs);
522 gw_free_headers(header);
524 if (error == NULL && kerr != KCGI_OK)
525 error = gw_kcgi_error(kerr);
529 static const struct got_error *
530 gw_index(struct gw_trans *gw_trans)
532 const struct got_error *error = NULL;
533 struct gw_dir *gw_dir = NULL;
534 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
535 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
536 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
539 if (pledge("stdio rpath proc exec sendfd unveil",
541 error = got_error_from_errno("pledge");
545 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
549 error = gw_load_got_paths(gw_trans);
553 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
554 "index_header", KATTR__MAX);
556 return gw_kcgi_error(kerr);
557 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
558 "index_header_project", KATTR__MAX);
560 return gw_kcgi_error(kerr);
561 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
563 return gw_kcgi_error(kerr);
564 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
566 return gw_kcgi_error(kerr);
568 if (gw_trans->gw_conf->got_show_repo_description) {
569 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
570 "index_header_description", KATTR__MAX);
572 return gw_kcgi_error(kerr);
573 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
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_owner) {
582 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
583 "index_header_owner", KATTR__MAX);
585 return gw_kcgi_error(kerr);
586 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
588 return gw_kcgi_error(kerr);
589 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
591 return gw_kcgi_error(kerr);
594 if (gw_trans->gw_conf->got_show_repo_age) {
595 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
596 "index_header_age", KATTR__MAX);
598 return gw_kcgi_error(kerr);
599 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
601 return gw_kcgi_error(kerr);
602 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
604 return gw_kcgi_error(kerr);
607 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
609 return gw_kcgi_error(kerr);
611 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
612 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
613 "index_wrapper", KATTR__MAX);
615 return gw_kcgi_error(kerr);
616 kerr = khtml_puts(gw_trans->gw_html_req,
617 "No repositories found in ");
619 return gw_kcgi_error(kerr);
620 kerr = khtml_puts(gw_trans->gw_html_req,
621 gw_trans->gw_conf->got_repos_path);
623 return gw_kcgi_error(kerr);
624 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
626 return gw_kcgi_error(kerr);
627 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
628 "dotted_line", KATTR__MAX);
630 return gw_kcgi_error(kerr);
631 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
633 return gw_kcgi_error(kerr);
637 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
640 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
641 if (gw_trans->page > 0 && (gw_trans->page *
642 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
649 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
650 "index_wrapper", KATTR__MAX);
652 return gw_kcgi_error(kerr);
654 if (asprintf(&href_summary, "?path=%s&action=summary",
656 return got_error_from_errno("asprintf");
657 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
658 "index_project", KATTR__MAX);
661 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
662 href_summary, KATTR__MAX);
665 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
668 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
671 if (gw_trans->gw_conf->got_show_repo_description) {
672 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
673 KATTR_ID, "index_project_description", KATTR__MAX);
676 kerr = khtml_puts(gw_trans->gw_html_req,
677 gw_dir->description ? gw_dir->description : "");
680 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
684 if (gw_trans->gw_conf->got_show_repo_owner) {
685 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
686 KATTR_ID, "index_project_owner", KATTR__MAX);
689 kerr = khtml_puts(gw_trans->gw_html_req,
690 gw_dir->owner ? gw_dir->owner : "");
693 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
697 if (gw_trans->gw_conf->got_show_repo_age) {
698 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
699 KATTR_ID, "index_project_age", KATTR__MAX);
702 kerr = khtml_puts(gw_trans->gw_html_req,
703 gw_dir->age ? gw_dir->age : "");
706 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
711 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
712 "navs_wrapper", KATTR__MAX);
715 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
720 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
721 href_summary, KATTR__MAX);
724 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
727 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
731 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
735 if (asprintf(&href_briefs, "?path=%s&action=briefs",
736 gw_dir->name) == -1) {
737 error = got_error_from_errno("asprintf");
740 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
741 href_briefs, KATTR__MAX);
744 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
747 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
749 error = gw_kcgi_error(kerr);
751 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
755 if (asprintf(&href_commits, "?path=%s&action=commits",
756 gw_dir->name) == -1) {
757 error = got_error_from_errno("asprintf");
760 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
761 href_commits, KATTR__MAX);
764 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
767 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
771 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
775 if (asprintf(&href_tree, "?path=%s&action=tree",
776 gw_dir->name) == -1) {
777 error = got_error_from_errno("asprintf");
780 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
781 href_tree, KATTR__MAX);
784 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
788 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
791 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
792 "dotted_line", KATTR__MAX);
795 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
808 if (gw_trans->gw_conf->got_max_repos_display == 0)
811 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
812 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
813 KATTR_ID, "np_wrapper", KATTR__MAX);
816 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
817 KATTR_ID, "nav_prev", KATTR__MAX);
820 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
821 (gw_trans->page > 0) &&
822 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
823 prev_disp == gw_trans->repos_total)) {
824 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
825 KATTR_ID, "np_wrapper", KATTR__MAX);
828 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
829 KATTR_ID, "nav_prev", KATTR__MAX);
834 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
835 (gw_trans->page > 0) &&
836 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
837 prev_disp == gw_trans->repos_total)) {
838 if (asprintf(&href_prev, "?page=%d",
839 gw_trans->page - 1) == -1) {
840 error = got_error_from_errno("asprintf");
843 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
844 KATTR_HREF, href_prev, KATTR__MAX);
848 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
851 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
856 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
858 return gw_kcgi_error(kerr);
860 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
861 next_disp == gw_trans->gw_conf->got_max_repos_display &&
862 dir_c != (gw_trans->page + 1) *
863 gw_trans->gw_conf->got_max_repos_display) {
864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
865 KATTR_ID, "nav_next", KATTR__MAX);
868 if (asprintf(&href_next, "?page=%d",
869 gw_trans->page + 1) == -1) {
870 error = got_error_from_errno("calloc");
873 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
874 KATTR_HREF, href_next, KATTR__MAX);
878 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
881 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
888 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
889 (gw_trans->page > 0) &&
890 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
891 prev_disp == gw_trans->repos_total)) {
892 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
903 if (error == NULL && kerr != KCGI_OK)
904 error = gw_kcgi_error(kerr);
908 static const struct got_error *
909 gw_commits(struct gw_trans *gw_trans)
911 const struct got_error *error = NULL;
912 struct gw_header *header = NULL, *n_header = NULL;
914 char *href_diff = NULL, *href_blob = NULL;
915 enum kcgi_err kerr = KCGI_OK;
917 if ((header = gw_init_header()) == NULL)
918 return got_error_from_errno("malloc");
920 if (pledge("stdio rpath proc exec sendfd unveil",
922 error = got_error_from_errno("pledge");
926 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
930 error = gw_get_header(gw_trans, header,
931 gw_trans->gw_conf->got_max_commits_display);
935 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
936 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
937 "commits_line_wrapper", KATTR__MAX);
940 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
944 error = gw_gen_author_header(gw_trans, n_header->author);
947 error = gw_gen_committer_header(gw_trans, n_header->author);
950 error = gw_get_time_str(&age, n_header->committer_time,
954 error = gw_gen_age_header(gw_trans, age ?age : "");
957 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
961 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
962 "dotted_line", KATTR__MAX);
965 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
969 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
970 "commit", KATTR__MAX);
973 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
976 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
980 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
981 gw_trans->repo_name, n_header->commit_id) == -1) {
982 error = got_error_from_errno("asprintf");
985 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
986 KATTR_ID, "navs_wrapper", KATTR__MAX);
989 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
990 KATTR_ID, "navs", KATTR__MAX);
993 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
994 KATTR_HREF, href_diff, KATTR__MAX);
997 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1000 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1001 if (kerr != KCGI_OK)
1004 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1005 if (kerr != KCGI_OK)
1008 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1009 gw_trans->repo_name, n_header->commit_id) == -1) {
1010 error = got_error_from_errno("asprintf");
1013 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1014 KATTR_HREF, href_blob, KATTR__MAX);
1015 if (kerr != KCGI_OK)
1017 khtml_puts(gw_trans->gw_html_req, "tree");
1018 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1019 if (kerr != KCGI_OK)
1021 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1022 if (kerr != KCGI_OK)
1025 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1026 "solid_line", KATTR__MAX);
1027 if (kerr != KCGI_OK)
1029 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1030 if (kerr != KCGI_OK)
1037 got_ref_list_free(&header->refs);
1038 gw_free_headers(header);
1039 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1040 gw_free_headers(n_header);
1044 if (error == NULL && kerr != KCGI_OK)
1045 error = gw_kcgi_error(kerr);
1049 static const struct got_error *
1050 gw_briefs(struct gw_trans *gw_trans)
1052 const struct got_error *error = NULL;
1053 struct gw_header *header = NULL, *n_header = NULL;
1054 char *age = NULL, *age_html = NULL;
1055 char *href_diff = NULL, *href_blob = NULL;
1056 char *newline, *smallerthan;
1057 enum kcgi_err kerr = KCGI_OK;
1059 if ((header = gw_init_header()) == NULL)
1060 return got_error_from_errno("malloc");
1062 if (pledge("stdio rpath proc exec sendfd unveil",
1064 error = got_error_from_errno("pledge");
1068 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1072 if (gw_trans->action == GW_SUMMARY)
1073 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1075 error = gw_get_header(gw_trans, header,
1076 gw_trans->gw_conf->got_max_commits_display);
1080 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1081 error = gw_get_time_str(&age, n_header->committer_time,
1086 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1087 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1088 if (kerr != KCGI_OK)
1091 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1092 KATTR_ID, "briefs_age", KATTR__MAX);
1093 if (kerr != KCGI_OK)
1095 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
1096 error = got_error_from_errno("asprintf");
1099 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
1100 if (kerr != KCGI_OK)
1102 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1103 if (kerr != KCGI_OK)
1106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1107 KATTR_ID, "briefs_author", KATTR__MAX);
1108 if (kerr != KCGI_OK)
1110 smallerthan = strchr(n_header->author, '<');
1112 *smallerthan = '\0';
1113 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1114 if (kerr != KCGI_OK)
1116 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1117 if (kerr != KCGI_OK)
1120 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1121 gw_trans->repo_name, n_header->commit_id) == -1) {
1122 error = got_error_from_errno("asprintf");
1125 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1126 KATTR_ID, "briefs_log", KATTR__MAX);
1127 if (kerr != KCGI_OK)
1129 newline = strchr(n_header->commit_msg, '\n');
1132 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1133 KATTR_HREF, href_diff, KATTR__MAX);
1134 if (kerr != KCGI_OK)
1136 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1137 if (kerr != KCGI_OK)
1139 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1140 if (kerr != KCGI_OK)
1143 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1144 KATTR_ID, "navs_wrapper", KATTR__MAX);
1145 if (kerr != KCGI_OK)
1147 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1148 KATTR_ID, "navs", KATTR__MAX);
1149 if (kerr != KCGI_OK)
1151 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1152 KATTR_HREF, href_diff, KATTR__MAX);
1153 if (kerr != KCGI_OK)
1155 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1156 if (kerr != KCGI_OK)
1158 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1159 if (kerr != KCGI_OK)
1162 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1163 if (kerr != KCGI_OK)
1166 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1167 gw_trans->repo_name, n_header->commit_id) == -1) {
1168 error = got_error_from_errno("asprintf");
1171 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1172 KATTR_HREF, href_blob, KATTR__MAX);
1173 if (kerr != KCGI_OK)
1175 khtml_puts(gw_trans->gw_html_req, "tree");
1176 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1177 if (kerr != KCGI_OK)
1179 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1180 if (kerr != KCGI_OK)
1183 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1184 KATTR_ID, "dotted_line", KATTR__MAX);
1185 if (kerr != KCGI_OK)
1187 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1188 if (kerr != KCGI_OK)
1201 got_ref_list_free(&header->refs);
1202 gw_free_headers(header);
1203 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1204 gw_free_headers(n_header);
1209 if (error == NULL && kerr != KCGI_OK)
1210 error = gw_kcgi_error(kerr);
1214 static const struct got_error *
1215 gw_summary(struct gw_trans *gw_trans)
1217 const struct got_error *error = NULL;
1219 enum kcgi_err kerr = KCGI_OK;
1221 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1222 return got_error_from_errno("pledge");
1224 /* unveil is applied with gw_briefs below */
1226 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1227 "summary_wrapper", KATTR__MAX);
1228 if (kerr != KCGI_OK)
1229 return gw_kcgi_error(kerr);
1231 if (gw_trans->gw_conf->got_show_repo_description &&
1232 gw_trans->gw_dir->description != NULL &&
1233 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1234 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1235 KATTR_ID, "description_title", KATTR__MAX);
1236 if (kerr != KCGI_OK)
1238 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1239 if (kerr != KCGI_OK)
1241 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1242 if (kerr != KCGI_OK)
1244 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1245 KATTR_ID, "description", KATTR__MAX);
1246 if (kerr != KCGI_OK)
1248 kerr = khtml_puts(gw_trans->gw_html_req,
1249 gw_trans->gw_dir->description);
1250 if (kerr != KCGI_OK)
1252 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1253 if (kerr != KCGI_OK)
1257 if (gw_trans->gw_conf->got_show_repo_owner &&
1258 gw_trans->gw_dir->owner != NULL &&
1259 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1260 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1261 KATTR_ID, "repo_owner_title", KATTR__MAX);
1262 if (kerr != KCGI_OK)
1264 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1265 if (kerr != KCGI_OK)
1267 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1268 if (kerr != KCGI_OK)
1270 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1271 KATTR_ID, "repo_owner", KATTR__MAX);
1272 if (kerr != KCGI_OK)
1274 kerr = khtml_puts(gw_trans->gw_html_req,
1275 gw_trans->gw_dir->owner);
1276 if (kerr != KCGI_OK)
1278 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1279 if (kerr != KCGI_OK)
1283 if (gw_trans->gw_conf->got_show_repo_age) {
1284 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1285 "refs/heads", TM_LONG);
1289 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1290 KATTR_ID, "last_change_title", KATTR__MAX);
1291 if (kerr != KCGI_OK)
1293 kerr = khtml_puts(gw_trans->gw_html_req,
1295 if (kerr != KCGI_OK)
1297 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1298 if (kerr != KCGI_OK)
1300 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1301 KATTR_ID, "last_change", KATTR__MAX);
1302 if (kerr != KCGI_OK)
1304 kerr = khtml_puts(gw_trans->gw_html_req, age);
1305 if (kerr != KCGI_OK)
1307 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1308 if (kerr != KCGI_OK)
1313 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1314 gw_trans->gw_dir->url != NULL &&
1315 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1316 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1317 KATTR_ID, "cloneurl_title", KATTR__MAX);
1318 if (kerr != KCGI_OK)
1320 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1321 if (kerr != KCGI_OK)
1323 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1324 if (kerr != KCGI_OK)
1326 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1327 KATTR_ID, "cloneurl", KATTR__MAX);
1328 if (kerr != KCGI_OK)
1330 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1331 if (kerr != KCGI_OK)
1333 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1334 if (kerr != KCGI_OK)
1338 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1339 if (kerr != KCGI_OK)
1342 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1343 "briefs_title_wrapper", KATTR__MAX);
1344 if (kerr != KCGI_OK)
1346 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1347 "briefs_title", KATTR__MAX);
1348 if (kerr != KCGI_OK)
1350 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1351 if (kerr != KCGI_OK)
1353 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1354 if (kerr != KCGI_OK)
1356 error = gw_briefs(gw_trans);
1360 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1365 error = gw_output_repo_heads(gw_trans);
1368 if (error == NULL && kerr != KCGI_OK)
1369 error = gw_kcgi_error(kerr);
1373 static const struct got_error *
1374 gw_tree(struct gw_trans *gw_trans)
1376 const struct got_error *error = NULL;
1377 struct gw_header *header = NULL;
1378 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1379 char *age = NULL, *age_html = NULL;
1382 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1383 return got_error_from_errno("pledge");
1385 if ((header = gw_init_header()) == NULL)
1386 return got_error_from_errno("malloc");
1388 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1392 error = gw_get_header(gw_trans, header, 1);
1396 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1397 "tree_header_wrapper", KATTR__MAX);
1398 if (kerr != KCGI_OK)
1400 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1401 "tree_header", KATTR__MAX);
1402 if (kerr != KCGI_OK)
1404 error = gw_gen_tree_header(gw_trans, header->tree_id);
1407 error = gw_get_time_str(&age, header->committer_time,
1411 error = gw_gen_age_header(gw_trans, age ?age : "");
1414 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1417 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1418 if (kerr != KCGI_OK)
1420 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1421 "dotted_line", KATTR__MAX);
1422 if (kerr != KCGI_OK)
1424 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1425 if (kerr != KCGI_OK)
1428 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1429 "tree", KATTR__MAX);
1430 if (kerr != KCGI_OK)
1432 error = gw_output_repo_tree(gw_trans);
1436 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1438 got_ref_list_free(&header->refs);
1439 gw_free_headers(header);
1440 free(tree_html_disp);
1445 if (error == NULL && kerr != KCGI_OK)
1446 error = gw_kcgi_error(kerr);
1450 static const struct got_error *
1451 gw_tag(struct gw_trans *gw_trans)
1453 const struct got_error *error = NULL;
1454 struct gw_header *header = NULL;
1457 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1458 return got_error_from_errno("pledge");
1460 if ((header = gw_init_header()) == NULL)
1461 return got_error_from_errno("malloc");
1463 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1467 error = gw_get_header(gw_trans, header, 1);
1471 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1472 "tag_header_wrapper", KATTR__MAX);
1473 if (kerr != KCGI_OK)
1475 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1476 "tag_header", KATTR__MAX);
1477 if (kerr != KCGI_OK)
1479 error = gw_gen_commit_header(gw_trans, header->commit_id,
1483 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1486 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1487 if (kerr != KCGI_OK)
1489 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1490 "dotted_line", KATTR__MAX);
1491 if (kerr != KCGI_OK)
1493 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1494 if (kerr != KCGI_OK)
1497 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1498 "tree", KATTR__MAX);
1499 if (kerr != KCGI_OK)
1502 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1506 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1508 got_ref_list_free(&header->refs);
1509 gw_free_headers(header);
1510 if (error == NULL && kerr != KCGI_OK)
1511 error = gw_kcgi_error(kerr);
1515 static const struct got_error *
1516 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1518 const struct got_error *error = NULL;
1523 if (asprintf(&dir_test, "%s/%s/%s",
1524 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1525 GOTWEB_GIT_DIR) == -1)
1526 return got_error_from_errno("asprintf");
1528 dt = opendir(dir_test);
1532 error = gw_strdup_string(&gw_dir->path, dir_test, NULL);
1539 if (asprintf(&dir_test, "%s/%s/%s",
1540 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1541 GOTWEB_GOT_DIR) == -1)
1542 return got_error_from_errno("asprintf");
1544 dt = opendir(dir_test);
1549 error = got_error(GOT_ERR_NOT_GIT_REPO);
1553 if (asprintf(&dir_test, "%s/%s",
1554 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1555 return got_error_from_errno("asprintf");
1557 error = gw_strdup_string(&gw_dir->path, dir_test, NULL);
1563 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1567 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1570 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1571 "refs/heads", TM_DIFF);
1574 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1582 static const struct got_error *
1583 gw_load_got_paths(struct gw_trans *gw_trans)
1585 const struct got_error *error = NULL;
1587 struct dirent **sd_dent;
1588 struct gw_dir *gw_dir;
1590 unsigned int d_cnt, d_i;
1592 d = opendir(gw_trans->gw_conf->got_repos_path);
1594 error = got_error_from_errno2("opendir",
1595 gw_trans->gw_conf->got_repos_path);
1599 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1602 error = got_error_from_errno2("scandir",
1603 gw_trans->gw_conf->got_repos_path);
1607 for (d_i = 0; d_i < d_cnt; d_i++) {
1608 if (gw_trans->gw_conf->got_max_repos > 0 &&
1609 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1610 break; /* account for parent and self */
1612 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1613 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1616 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1617 return got_error_from_errno("gw_dir malloc");
1619 error = gw_load_got_path(gw_trans, gw_dir);
1620 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1627 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1628 !got_path_dir_is_empty(gw_dir->path)) {
1629 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1631 gw_trans->repos_total++;
1639 static const struct got_error *
1640 gw_parse_querystring(struct gw_trans *gw_trans)
1642 const struct got_error *error = NULL;
1644 struct gw_query_action *action = NULL;
1647 if (gw_trans->gw_req->fieldnmap[0]) {
1648 error = got_error_from_errno("bad parse");
1650 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1651 /* define gw_trans->repo_path */
1652 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1653 return got_error_from_errno("asprintf");
1655 if (asprintf(&gw_trans->repo_path, "%s/%s",
1656 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1657 return got_error_from_errno("asprintf");
1659 /* get action and set function */
1660 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1661 for (i = 0; i < nitems(gw_query_funcs); i++) {
1662 action = &gw_query_funcs[i];
1663 if (action->func_name == NULL)
1666 if (strcmp(action->func_name,
1667 p->parsed.s) == 0) {
1668 gw_trans->action = i;
1669 if (asprintf(&gw_trans->action_name,
1670 "%s", action->func_name) == -1)
1672 got_error_from_errno(
1681 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1682 if (asprintf(&gw_trans->commit, "%s",
1684 return got_error_from_errno("asprintf");
1686 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1687 if (asprintf(&gw_trans->repo_file, "%s",
1689 return got_error_from_errno("asprintf");
1691 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1692 if (asprintf(&gw_trans->repo_folder, "%s",
1694 return got_error_from_errno("asprintf");
1696 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1697 if (asprintf(&gw_trans->headref, "%s",
1699 return got_error_from_errno("asprintf");
1701 if (action == NULL) {
1702 error = got_error_from_errno("invalid action");
1705 if ((gw_trans->gw_dir =
1706 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1707 return got_error_from_errno("gw_dir malloc");
1709 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1713 gw_trans->action = GW_INDEX;
1715 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1716 gw_trans->page = p->parsed.i;
1721 static struct gw_dir *
1722 gw_init_gw_dir(char *dir)
1724 struct gw_dir *gw_dir;
1726 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1729 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1735 static const struct got_error *
1736 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1740 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1741 if (kerr != KCGI_OK)
1742 return gw_kcgi_error(kerr);
1743 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1745 if (kerr != KCGI_OK)
1746 return gw_kcgi_error(kerr);
1747 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1749 if (kerr != KCGI_OK)
1750 return gw_kcgi_error(kerr);
1751 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1753 if (kerr != KCGI_OK)
1754 return gw_kcgi_error(kerr);
1755 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1756 if (kerr != KCGI_OK)
1757 return gw_kcgi_error(kerr);
1758 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1760 if (kerr != KCGI_OK)
1761 return gw_kcgi_error(kerr);
1763 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1764 kerr = khttp_head(gw_trans->gw_req,
1765 kresps[KRESP_CONTENT_DISPOSITION],
1766 "attachment; filename=%s", gw_trans->repo_file);
1767 if (kerr != KCGI_OK)
1768 return gw_kcgi_error(kerr);
1771 kerr = khttp_body(gw_trans->gw_req);
1772 return gw_kcgi_error(kerr);
1775 static const struct got_error *
1776 gw_display_index(struct gw_trans *gw_trans)
1778 const struct got_error *error;
1781 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1785 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1786 if (kerr != KCGI_OK)
1787 return gw_kcgi_error(kerr);
1789 if (gw_trans->action != GW_BLOB) {
1790 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1791 gw_query_funcs[gw_trans->action].template);
1792 if (kerr != KCGI_OK) {
1793 khtml_close(gw_trans->gw_html_req);
1794 return gw_kcgi_error(kerr);
1798 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1802 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1804 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1807 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1809 khtml_puts(gw_trans->gw_html_req, err->msg);
1810 khtml_close(gw_trans->gw_html_req);
1814 gw_template(size_t key, void *arg)
1816 const struct got_error *error = NULL;
1818 struct gw_trans *gw_trans = arg;
1819 char *img_src = NULL;
1823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1824 KATTR_CONTENT, "initial-scale=1.0, user-scalable=no",
1826 if (kerr != KCGI_OK)
1828 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1829 if (kerr != KCGI_OK)
1831 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1832 KATTR_CHARSET, "utf-8",
1834 if (kerr != KCGI_OK)
1836 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1837 if (kerr != KCGI_OK)
1839 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1840 KATTR_NAME, "msapplication-TileColor",
1841 KATTR_CONTENT, "#da532c", KATTR__MAX);
1842 if (kerr != KCGI_OK)
1844 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1845 if (kerr != KCGI_OK)
1847 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1848 KATTR_NAME, "theme-color",
1849 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1850 if (kerr != KCGI_OK)
1852 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1853 if (kerr != KCGI_OK)
1855 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1856 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1857 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1858 if (kerr != KCGI_OK)
1860 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1861 if (kerr != KCGI_OK)
1863 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1864 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1865 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1866 if (kerr != KCGI_OK)
1868 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1869 if (kerr != KCGI_OK)
1871 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1872 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1873 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1874 if (kerr != KCGI_OK)
1876 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1877 if (kerr != KCGI_OK)
1879 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1880 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1882 if (kerr != KCGI_OK)
1884 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1885 if (kerr != KCGI_OK)
1887 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1888 KATTR_REL, "mask-icon", KATTR_HREF,
1889 "/safari-pinned-tab.svg", KATTR__MAX);
1890 if (kerr != KCGI_OK)
1892 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1893 if (kerr != KCGI_OK)
1895 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1896 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1897 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1898 if (kerr != KCGI_OK)
1900 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1901 if (kerr != KCGI_OK)
1905 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1906 KATTR_ID, "got_link", KATTR__MAX);
1907 if (kerr != KCGI_OK)
1909 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1910 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1911 KATTR_TARGET, "_sotd", KATTR__MAX);
1912 if (kerr != KCGI_OK)
1914 if (asprintf(&img_src, "/%s",
1915 gw_trans->gw_conf->got_logo) == -1)
1917 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1918 KATTR_SRC, img_src, KATTR__MAX);
1919 if (kerr != KCGI_OK) {
1923 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1924 if (kerr != KCGI_OK) {
1929 case (TEMPL_SITEPATH):
1930 error = gw_output_site_link(gw_trans);
1935 if (gw_trans->gw_conf->got_site_name != NULL) {
1936 kerr = khtml_puts(gw_trans->gw_html_req,
1937 gw_trans->gw_conf->got_site_name);
1938 if (kerr != KCGI_OK)
1942 case (TEMPL_SEARCH):
1944 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1945 "search", KATTR__MAX);
1946 if (kerr != KCGI_OK)
1948 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1949 KATTR_METHOD, "POST", KATTR__MAX);
1950 if (kerr != KCGI_OK)
1952 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1953 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1954 KATTR_MAXLENGTH, "50", KATTR__MAX);
1955 if (kerr != KCGI_OK)
1957 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1959 if (kerr != KCGI_OK)
1961 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1962 if (kerr != KCGI_OK)
1964 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1965 if (kerr != KCGI_OK)
1968 case(TEMPL_SITEOWNER):
1969 if (gw_trans->gw_conf->got_site_owner != NULL &&
1970 gw_trans->gw_conf->got_show_site_owner) {
1971 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1972 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1973 if (kerr != KCGI_OK)
1975 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1976 KATTR_ID, "site_owner", KATTR__MAX);
1977 if (kerr != KCGI_OK)
1979 kerr = khtml_puts(gw_trans->gw_html_req,
1980 gw_trans->gw_conf->got_site_owner);
1981 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1982 if (kerr != KCGI_OK)
1986 case(TEMPL_CONTENT):
1987 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1989 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1990 KATTR_ID, "tmpl_err", KATTR__MAX);
1991 if (kerr != KCGI_OK)
1993 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1994 if (kerr != KCGI_OK)
1996 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1997 if (kerr != KCGI_OK)
1999 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2000 if (kerr != KCGI_OK)
2010 static const struct got_error *
2011 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2013 const struct got_error *error = NULL;
2014 char *ref_str = NULL;
2015 enum kcgi_err kerr = KCGI_OK;
2017 if (strcmp(str2, "") != 0) {
2018 if (asprintf(&ref_str, "(%s)", str2) == -1) {
2019 error = got_error_from_errno("asprintf");
2023 error = gw_strdup_string(&ref_str, "", NULL);
2028 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2029 KATTR_ID, "header_commit_title", KATTR__MAX);
2030 if (kerr != KCGI_OK)
2032 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2033 if (kerr != KCGI_OK)
2035 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2036 if (kerr != KCGI_OK)
2038 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2039 KATTR_ID, "header_commit", KATTR__MAX);
2040 if (kerr != KCGI_OK)
2042 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2043 if (kerr != KCGI_OK)
2045 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2046 if (kerr != KCGI_OK)
2048 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
2049 if (kerr != KCGI_OK)
2051 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2053 if (error == NULL && kerr != KCGI_OK)
2054 error = gw_kcgi_error(kerr);
2058 static const struct got_error *
2059 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2061 const struct got_error *error = NULL;
2062 enum kcgi_err kerr = KCGI_OK;
2064 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2065 KATTR_ID, "header_diff_title", KATTR__MAX);
2066 if (kerr != KCGI_OK)
2068 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2069 if (kerr != KCGI_OK)
2071 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2072 if (kerr != KCGI_OK)
2074 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2075 KATTR_ID, "header_diff", KATTR__MAX);
2076 if (kerr != KCGI_OK)
2078 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2079 if (kerr != KCGI_OK)
2081 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2082 if (kerr != KCGI_OK)
2084 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2085 if (kerr != KCGI_OK)
2087 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2089 if (error == NULL && kerr != KCGI_OK)
2090 error = gw_kcgi_error(kerr);
2094 static const struct got_error *
2095 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2097 const struct got_error *error = NULL;
2098 enum kcgi_err kerr = KCGI_OK;
2100 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2101 KATTR_ID, "header_age_title", KATTR__MAX);
2102 if (kerr != KCGI_OK)
2104 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2105 if (kerr != KCGI_OK)
2107 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2108 if (kerr != KCGI_OK)
2110 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2111 KATTR_ID, "header_age", KATTR__MAX);
2112 if (kerr != KCGI_OK)
2114 kerr = khtml_puts(gw_trans->gw_html_req, str);
2115 if (kerr != KCGI_OK)
2117 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2119 if (error == NULL && kerr != KCGI_OK)
2120 error = gw_kcgi_error(kerr);
2124 static const struct got_error *
2125 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2127 const struct got_error *error = NULL;
2130 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2131 KATTR_ID, "header_author_title", KATTR__MAX);
2132 if (kerr != KCGI_OK)
2134 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2135 if (kerr != KCGI_OK)
2137 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2138 if (kerr != KCGI_OK)
2140 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2141 KATTR_ID, "header_author", KATTR__MAX);
2142 if (kerr != KCGI_OK)
2144 kerr = khtml_puts(gw_trans->gw_html_req, str);
2145 if (kerr != KCGI_OK)
2147 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2149 if (error == NULL && kerr != KCGI_OK)
2150 error = gw_kcgi_error(kerr);
2154 static const struct got_error *
2155 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2157 const struct got_error *error = NULL;
2160 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2161 KATTR_ID, "header_committer_title", KATTR__MAX);
2162 if (kerr != KCGI_OK)
2164 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2165 if (kerr != KCGI_OK)
2167 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2168 if (kerr != KCGI_OK)
2170 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2171 KATTR_ID, "header_committer", KATTR__MAX);
2172 if (kerr != KCGI_OK)
2174 kerr = khtml_puts(gw_trans->gw_html_req, str);
2175 if (kerr != KCGI_OK)
2177 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2179 if (error == NULL && kerr != KCGI_OK)
2180 error = gw_kcgi_error(kerr);
2184 static const struct got_error *
2185 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2187 const struct got_error *error = NULL;
2190 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2191 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2192 if (kerr != KCGI_OK)
2194 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2195 if (kerr != KCGI_OK)
2197 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2198 if (kerr != KCGI_OK)
2200 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2201 KATTR_ID, "header_commit_msg", KATTR__MAX);
2202 if (kerr != KCGI_OK)
2204 kerr = khttp_puts(gw_trans->gw_req, str);
2205 if (kerr != KCGI_OK)
2207 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2209 if (error == NULL && kerr != KCGI_OK)
2210 error = gw_kcgi_error(kerr);
2214 static const struct got_error *
2215 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2217 const struct got_error *error = NULL;
2220 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2221 KATTR_ID, "header_tree_title", KATTR__MAX);
2222 if (kerr != KCGI_OK)
2224 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2225 if (kerr != KCGI_OK)
2227 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2228 if (kerr != KCGI_OK)
2230 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2231 KATTR_ID, "header_tree", KATTR__MAX);
2232 if (kerr != KCGI_OK)
2234 kerr = khtml_puts(gw_trans->gw_html_req, str);
2235 if (kerr != KCGI_OK)
2237 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2239 if (error == NULL && kerr != KCGI_OK)
2240 error = gw_kcgi_error(kerr);
2244 static const struct got_error *
2245 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2248 const struct got_error *error = NULL;
2250 char *d_file = NULL;
2254 *description = NULL;
2255 if (gw_trans->gw_conf->got_show_repo_description == 0)
2258 if (asprintf(&d_file, "%s/description", dir) == -1)
2259 return got_error_from_errno("asprintf");
2261 f = fopen(d_file, "r");
2263 if (errno == ENOENT || errno == EACCES)
2265 error = got_error_from_errno2("fopen", d_file);
2269 if (fseek(f, 0, SEEK_END) == -1) {
2270 error = got_ferror(f, GOT_ERR_IO);
2275 error = got_ferror(f, GOT_ERR_IO);
2278 if (fseek(f, 0, SEEK_SET) == -1) {
2279 error = got_ferror(f, GOT_ERR_IO);
2282 *description = calloc(len + 1, sizeof(**description));
2283 if (*description == NULL) {
2284 error = got_error_from_errno("calloc");
2288 n = fread(*description, 1, len, f);
2289 if (n == 0 && ferror(f))
2290 error = got_ferror(f, GOT_ERR_IO);
2292 if (f != NULL && fclose(f) == -1 && error == NULL)
2293 error = got_error_from_errno("fclose");
2298 static const struct got_error *
2299 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2303 char *years = "years ago", *months = "months ago";
2304 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2305 char *minutes = "minutes ago", *seconds = "seconds ago";
2306 char *now = "right now";
2314 diff_time = time(NULL) - committer_time;
2315 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2316 if (asprintf(repo_age, "%lld %s",
2317 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2318 return got_error_from_errno("asprintf");
2319 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2320 if (asprintf(repo_age, "%lld %s",
2321 (diff_time / 60 / 60 / 24 / (365 / 12)),
2323 return got_error_from_errno("asprintf");
2324 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2325 if (asprintf(repo_age, "%lld %s",
2326 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2327 return got_error_from_errno("asprintf");
2328 } else if (diff_time > 60 * 60 * 24 * 2) {
2329 if (asprintf(repo_age, "%lld %s",
2330 (diff_time / 60 / 60 / 24), days) == -1)
2331 return got_error_from_errno("asprintf");
2332 } else if (diff_time > 60 * 60 * 2) {
2333 if (asprintf(repo_age, "%lld %s",
2334 (diff_time / 60 / 60), hours) == -1)
2335 return got_error_from_errno("asprintf");
2336 } else if (diff_time > 60 * 2) {
2337 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2339 return got_error_from_errno("asprintf");
2340 } else if (diff_time > 2) {
2341 if (asprintf(repo_age, "%lld %s", diff_time,
2343 return got_error_from_errno("asprintf");
2345 if (asprintf(repo_age, "%s", now) == -1)
2346 return got_error_from_errno("asprintf");
2350 if (gmtime_r(&committer_time, &tm) == NULL)
2351 return got_error_from_errno("gmtime_r");
2353 s = asctime_r(&tm, datebuf);
2355 return got_error_from_errno("asctime_r");
2357 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2358 return got_error_from_errno("asprintf");
2364 static const struct got_error *
2365 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2366 char *repo_ref, int ref_tm)
2368 const struct got_error *error = NULL;
2369 struct got_object_id *id = NULL;
2370 struct got_repository *repo = NULL;
2371 struct got_commit_object *commit = NULL;
2372 struct got_reflist_head refs;
2373 struct got_reflist_entry *re;
2374 struct got_reference *head_ref;
2376 time_t committer_time = 0, cmp_time = 0;
2380 SIMPLEQ_INIT(&refs);
2382 if (repo_ref == NULL)
2385 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2388 if (gw_trans->gw_conf->got_show_repo_age == 0)
2391 error = got_repo_open(&repo, dir, NULL);
2396 error = got_ref_list(&refs, repo, "refs/heads",
2397 got_ref_cmp_by_name, NULL);
2399 error = got_ref_list(&refs, repo, repo_ref,
2400 got_ref_cmp_by_name, NULL);
2404 SIMPLEQ_FOREACH(re, &refs, entry) {
2406 error = gw_strdup_string(&refname, repo_ref, NULL);
2410 error = gw_strdup_string(&refname, NULL,
2411 got_ref_get_name(re->ref));
2415 error = got_ref_open(&head_ref, repo, refname, 0);
2419 error = got_ref_resolve(&id, repo, head_ref);
2420 got_ref_close(head_ref);
2424 error = got_object_open_as_commit(&commit, repo, id);
2429 got_object_commit_get_committer_time(commit);
2431 if (cmp_time < committer_time)
2432 cmp_time = committer_time;
2435 if (cmp_time != 0) {
2436 committer_time = cmp_time;
2437 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2440 got_ref_list_free(&refs);
2445 static const struct got_error *
2446 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2448 const struct got_error *error;
2450 struct got_object_id *id1 = NULL, *id2 = NULL;
2451 char *label1 = NULL, *label2 = NULL, *line = NULL;
2453 size_t linesize = 0;
2455 enum kcgi_err kerr = KCGI_OK;
2461 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2465 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2466 error = got_repo_match_object_id(&id1, &label1,
2467 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2472 error = got_repo_match_object_id(&id2, &label2,
2473 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2477 error = got_object_get_type(&obj_type, header->repo, id2);
2481 case GOT_OBJ_TYPE_BLOB:
2482 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2485 case GOT_OBJ_TYPE_TREE:
2486 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2489 case GOT_OBJ_TYPE_COMMIT:
2490 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2494 error = got_error(GOT_ERR_OBJ_TYPE);
2499 if (fseek(f, 0, SEEK_SET) == -1) {
2500 error = got_ferror(f, GOT_ERR_IO);
2504 while ((linelen = getline(&line, &linesize, f)) != -1) {
2505 error = gw_colordiff_line(gw_trans, line);
2508 /* XXX: KHTML_PRETTY breaks this */
2509 kerr = khtml_puts(gw_trans->gw_html_req, line);
2510 if (kerr != KCGI_OK)
2512 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2513 if (kerr != KCGI_OK)
2516 if (linelen == -1 && ferror(f))
2517 error = got_error_from_errno("getline");
2519 if (f && fclose(f) == -1 && error == NULL)
2520 error = got_error_from_errno("fclose");
2527 if (error == NULL && kerr != KCGI_OK)
2528 error = gw_kcgi_error(kerr);
2532 static const struct got_error *
2533 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2535 const struct got_error *error = NULL;
2536 struct got_repository *repo;
2537 const char *gitconfig_owner;
2541 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2544 error = got_repo_open(&repo, dir, NULL);
2547 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2548 if (gitconfig_owner)
2549 error = gw_strdup_string(owner, NULL, gitconfig_owner);
2550 got_repo_close(repo);
2554 static const struct got_error *
2555 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2557 const struct got_error *error = NULL;
2559 char *d_file = NULL;
2565 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2566 return got_error_from_errno("asprintf");
2568 f = fopen(d_file, "r");
2570 if (errno != ENOENT && errno != EACCES)
2571 error = got_error_from_errno2("fopen", d_file);
2575 if (fseek(f, 0, SEEK_END) == -1) {
2576 error = got_ferror(f, GOT_ERR_IO);
2581 error = got_ferror(f, GOT_ERR_IO);
2584 if (fseek(f, 0, SEEK_SET) == -1) {
2585 error = got_ferror(f, GOT_ERR_IO);
2589 *url = calloc(len + 1, sizeof(**url));
2591 error = got_error_from_errno("calloc");
2595 n = fread(*url, 1, len, f);
2596 if (n == 0 && ferror(f))
2597 error = got_ferror(f, GOT_ERR_IO);
2599 if (f && fclose(f) == -1 && error == NULL)
2600 error = got_error_from_errno("fclose");
2605 static const struct got_error *
2606 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2607 int limit, int tag_type)
2609 const struct got_error *error = NULL;
2610 struct got_repository *repo = NULL;
2611 struct got_reflist_head refs;
2612 struct got_reflist_entry *re;
2614 char *id_str = NULL, *newline, *href_commits = NULL;
2615 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2616 struct got_tag_object *tag = NULL;
2617 enum kcgi_err kerr = KCGI_OK;
2618 int summary_header_displayed = 0;
2620 SIMPLEQ_INIT(&refs);
2622 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2626 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2630 SIMPLEQ_FOREACH(re, &refs, entry) {
2631 const char *refname;
2633 const char *tag_commit;
2635 struct got_object_id *id;
2636 struct got_commit_object *commit = NULL;
2638 refname = got_ref_get_name(re->ref);
2639 if (strncmp(refname, "refs/tags/", 10) != 0)
2643 error = got_ref_resolve(&id, repo, re->ref);
2647 error = got_object_open_as_tag(&tag, repo, id);
2649 if (error->code != GOT_ERR_OBJ_TYPE) {
2653 /* "lightweight" tag */
2654 error = got_object_open_as_commit(&commit, repo, id);
2659 tagger = got_object_commit_get_committer(commit);
2661 got_object_commit_get_committer_time(commit);
2662 error = got_object_id_str(&id_str, id);
2668 tagger = got_object_tag_get_tagger(tag);
2669 tagger_time = got_object_tag_get_tagger_time(tag);
2670 error = got_object_id_str(&id_str,
2671 got_object_tag_get_object_id(tag));
2678 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2679 strlen(id_str)) != 0)
2683 error = got_object_commit_get_logmsg(&tag_commit0,
2687 got_object_commit_close(commit);
2689 tag_commit0 = strdup(got_object_tag_get_message(tag));
2690 if (tag_commit0 == NULL) {
2691 error = got_error_from_errno("strdup");
2696 tag_commit = tag_commit0;
2697 while (*tag_commit == '\n')
2702 newline = strchr(tag_commit, '\n');
2706 if (summary_header_displayed == 0) {
2707 kerr = khtml_attr(gw_trans->gw_html_req,
2708 KELEM_DIV, KATTR_ID,
2709 "summary_tags_title_wrapper", KATTR__MAX);
2710 if (kerr != KCGI_OK)
2712 kerr = khtml_attr(gw_trans->gw_html_req,
2713 KELEM_DIV, KATTR_ID,
2714 "summary_tags_title", KATTR__MAX);
2715 if (kerr != KCGI_OK)
2717 kerr = khtml_puts(gw_trans->gw_html_req,
2719 if (kerr != KCGI_OK)
2721 kerr = khtml_closeelem(gw_trans->gw_html_req,
2723 if (kerr != KCGI_OK)
2725 kerr = khtml_attr(gw_trans->gw_html_req,
2726 KELEM_DIV, KATTR_ID,
2727 "summary_tags_content", KATTR__MAX);
2728 if (kerr != KCGI_OK)
2730 summary_header_displayed = 1;
2733 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2734 KATTR_ID, "tags_wrapper", KATTR__MAX);
2735 if (kerr != KCGI_OK)
2737 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2738 KATTR_ID, "tags_age", KATTR__MAX);
2739 if (kerr != KCGI_OK)
2741 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2744 kerr = khtml_puts(gw_trans->gw_html_req,
2746 if (kerr != KCGI_OK)
2748 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2749 if (kerr != KCGI_OK)
2751 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2752 KATTR_ID, "tags", KATTR__MAX);
2753 if (kerr != KCGI_OK)
2755 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2756 if (kerr != KCGI_OK)
2758 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2759 if (kerr != KCGI_OK)
2761 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2762 KATTR_ID, "tags_name", KATTR__MAX);
2763 if (kerr != KCGI_OK)
2765 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2766 gw_trans->repo_name, id_str) == -1) {
2767 error = got_error_from_errno("asprintf");
2770 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2771 KATTR_HREF, href_tag, KATTR__MAX);
2772 if (kerr != KCGI_OK)
2774 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2775 if (kerr != KCGI_OK)
2777 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2778 if (kerr != KCGI_OK)
2781 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2782 KATTR_ID, "navs_wrapper", KATTR__MAX);
2783 if (kerr != KCGI_OK)
2785 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2786 KATTR_ID, "navs", KATTR__MAX);
2787 if (kerr != KCGI_OK)
2790 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2791 KATTR_HREF, href_tag, KATTR__MAX);
2792 if (kerr != KCGI_OK)
2794 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2795 if (kerr != KCGI_OK)
2797 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2798 if (kerr != KCGI_OK)
2801 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2802 if (kerr != KCGI_OK)
2804 if (asprintf(&href_briefs,
2805 "?path=%s&action=briefs&commit=%s",
2806 gw_trans->repo_name, id_str) == -1) {
2807 error = got_error_from_errno("asprintf");
2810 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2811 KATTR_HREF, href_briefs, KATTR__MAX);
2812 if (kerr != KCGI_OK)
2814 kerr = khtml_puts(gw_trans->gw_html_req,
2816 if (kerr != KCGI_OK)
2818 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2819 if (kerr != KCGI_OK)
2822 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2823 if (kerr != KCGI_OK)
2826 if (asprintf(&href_commits,
2827 "?path=%s&action=commits&commit=%s",
2828 gw_trans->repo_name, id_str) == -1) {
2829 error = got_error_from_errno("asprintf");
2832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2833 KATTR_HREF, href_commits, KATTR__MAX);
2834 if (kerr != KCGI_OK)
2836 kerr = khtml_puts(gw_trans->gw_html_req,
2838 if (kerr != KCGI_OK)
2840 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2841 if (kerr != KCGI_OK)
2844 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2845 KATTR_ID, "dotted_line", KATTR__MAX);
2846 if (kerr != KCGI_OK)
2848 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2849 if (kerr != KCGI_OK)
2853 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2854 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2855 if (kerr != KCGI_OK)
2857 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2858 if (kerr != KCGI_OK)
2860 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2861 if (kerr != KCGI_OK)
2863 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2864 KATTR_ID, "tag_info_date", KATTR__MAX);
2865 if (kerr != KCGI_OK)
2867 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2870 kerr = khtml_puts(gw_trans->gw_html_req,
2872 if (kerr != KCGI_OK)
2874 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2875 if (kerr != KCGI_OK)
2878 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2879 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2880 if (kerr != KCGI_OK)
2882 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2883 if (kerr != KCGI_OK)
2885 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2886 if (kerr != KCGI_OK)
2888 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2889 KATTR_ID, "tag_info_date", KATTR__MAX);
2890 if (kerr != KCGI_OK)
2892 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2893 if (kerr != KCGI_OK)
2895 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2896 if (kerr != KCGI_OK)
2899 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2900 KATTR_ID, "tag_info", KATTR__MAX);
2901 if (kerr != KCGI_OK)
2903 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2904 if (kerr != KCGI_OK)
2910 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2911 if (kerr != KCGI_OK)
2914 if (limit && --limit == 0)
2918 got_object_tag_close(tag);
2931 href_commits = NULL;
2935 got_object_tag_close(tag);
2942 got_ref_list_free(&refs);
2944 got_repo_close(repo);
2945 if (error == NULL && kerr != KCGI_OK)
2946 error = gw_kcgi_error(kerr);
2951 gw_free_headers(struct gw_header *header)
2955 if (header->commit != NULL)
2956 got_object_commit_close(header->commit);
2958 got_repo_close(header->repo);
2959 free(header->refs_str);
2960 free(header->commit_id);
2961 free(header->author);
2962 free(header->committer);
2963 free(header->parent_id);
2964 free(header->tree_id);
2965 free(header->commit_msg);
2968 static struct gw_header *
2971 struct gw_header *header;
2973 header = malloc(sizeof(*header));
2977 header->repo = NULL;
2978 header->commit = NULL;
2980 header->path = NULL;
2981 SIMPLEQ_INIT(&header->refs);
2986 static const struct got_error *
2987 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2990 const struct got_error *error = NULL;
2991 struct got_commit_graph *graph = NULL;
2993 error = got_commit_graph_open(&graph, header->path, 0);
2997 error = got_commit_graph_iter_start(graph, header->id, header->repo,
3003 error = got_commit_graph_iter_next(&header->id, graph,
3004 header->repo, NULL, NULL);
3006 if (error->code == GOT_ERR_ITER_COMPLETED)
3010 if (header->id == NULL)
3013 error = got_object_open_as_commit(&header->commit, header->repo,
3018 error = gw_get_commit(gw_trans, header);
3020 struct gw_header *n_header = NULL;
3021 if ((n_header = gw_init_header()) == NULL) {
3022 error = got_error_from_errno("malloc");
3026 error = gw_strdup_string(&n_header->refs_str,
3027 header->refs_str, NULL);
3030 error = gw_strdup_string(&n_header->commit_id,
3031 header->commit_id, NULL);
3034 error = gw_strdup_string(&n_header->parent_id,
3035 header->parent_id, NULL);
3038 error = gw_strdup_string(&n_header->tree_id,
3039 header->tree_id, NULL);
3042 error = gw_strdup_string(&n_header->author,
3043 header->author, NULL);
3046 error = gw_strdup_string(&n_header->committer,
3047 header->committer, NULL);
3050 error = gw_strdup_string(&n_header->commit_msg,
3051 header->commit_msg, NULL);
3054 n_header->committer_time = header->committer_time;
3055 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3058 if (error || (limit && --limit == 0))
3063 got_commit_graph_close(graph);
3067 static const struct got_error *
3068 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3070 const struct got_error *error = NULL;
3071 struct got_reflist_entry *re;
3072 struct got_object_id *id2 = NULL;
3073 struct got_object_qid *parent_id;
3074 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
3077 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3080 struct got_tag_object *tag = NULL;
3083 name = got_ref_get_name(re->ref);
3084 if (strcmp(name, GOT_REF_HEAD) == 0)
3086 if (strncmp(name, "refs/", 5) == 0)
3088 if (strncmp(name, "got/", 4) == 0)
3090 if (strncmp(name, "heads/", 6) == 0)
3092 if (strncmp(name, "remotes/", 8) == 0)
3094 if (strncmp(name, "tags/", 5) == 0) {
3095 error = got_object_open_as_tag(&tag, header->repo,
3098 if (error->code != GOT_ERR_OBJ_TYPE)
3101 * Ref points at something other
3108 cmp = got_object_id_cmp(tag ?
3109 got_object_tag_get_object_id(tag) : re->id, header->id);
3111 got_object_tag_close(tag);
3115 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
3116 s ? ", " : "", name) == -1) {
3117 error = got_error_from_errno("asprintf");
3121 error = gw_strdup_string(&header->refs_str, refs_str, NULL);
3127 if (refs_str == NULL) {
3128 error = gw_strdup_string(&header->refs_str, "", NULL);
3134 error = got_object_id_str(&header->commit_id, header->id);
3138 error = got_object_id_str(&header->tree_id,
3139 got_object_commit_get_tree_id(header->commit));
3143 if (gw_trans->action == GW_DIFF) {
3144 parent_id = SIMPLEQ_FIRST(
3145 got_object_commit_get_parent_ids(header->commit));
3146 if (parent_id != NULL) {
3147 id2 = got_object_id_dup(parent_id->id);
3149 error = got_object_id_str(&header->parent_id, id2);
3154 error = gw_strdup_string(&header->parent_id,
3160 error = gw_strdup_string(&header->parent_id, "", NULL);
3165 header->committer_time =
3166 got_object_commit_get_committer_time(header->commit);
3168 error = gw_strdup_string(&header->author, NULL,
3169 got_object_commit_get_author(header->commit));
3172 error = gw_strdup_string(&header->committer, NULL,
3173 got_object_commit_get_committer(header->commit));
3176 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3180 commit_msg = commit_msg0;
3181 while (*commit_msg == '\n')
3184 error = gw_strdup_string(&header->commit_msg, commit_msg, NULL);
3189 static const struct got_error *
3190 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3192 const struct got_error *error = NULL;
3193 char *in_repo_path = NULL;
3195 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3199 if (gw_trans->commit == NULL) {
3200 struct got_reference *head_ref;
3201 error = got_ref_open(&head_ref, header->repo,
3202 gw_trans->headref, 0);
3206 error = got_ref_resolve(&header->id, header->repo, head_ref);
3207 got_ref_close(head_ref);
3211 error = got_object_open_as_commit(&header->commit,
3212 header->repo, header->id);
3214 struct got_reference *ref;
3215 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3216 if (error == NULL) {
3218 error = got_ref_resolve(&header->id, header->repo, ref);
3222 error = got_object_get_type(&obj_type, header->repo,
3226 if (obj_type == GOT_OBJ_TYPE_TAG) {
3227 struct got_tag_object *tag;
3228 error = got_object_open_as_tag(&tag,
3229 header->repo, header->id);
3232 if (got_object_tag_get_object_type(tag) !=
3233 GOT_OBJ_TYPE_COMMIT) {
3234 got_object_tag_close(tag);
3235 error = got_error(GOT_ERR_OBJ_TYPE);
3239 header->id = got_object_id_dup(
3240 got_object_tag_get_object_id(tag));
3241 if (header->id == NULL)
3242 error = got_error_from_errno(
3243 "got_object_id_dup");
3244 got_object_tag_close(tag);
3247 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3248 error = got_error(GOT_ERR_OBJ_TYPE);
3251 error = got_object_open_as_commit(&header->commit,
3252 header->repo, header->id);
3256 if (header->commit == NULL) {
3257 error = got_repo_match_object_id_prefix(&header->id,
3258 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3263 error = got_repo_match_object_id_prefix(&header->id,
3264 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3268 error = got_repo_map_path(&in_repo_path, header->repo,
3269 gw_trans->repo_path, 1);
3274 error = gw_strdup_string(&header->path, in_repo_path, NULL);
3282 error = got_ref_list(&header->refs, header->repo, NULL,
3283 got_ref_cmp_by_name, NULL);
3287 error = gw_get_commits(gw_trans, header, limit);
3295 char datebuf[11]; /* YYYY-MM-DD + NUL */
3298 struct gw_blame_cb_args {
3299 struct blame_line *lines;
3303 off_t *line_offsets;
3305 struct got_repository *repo;
3306 struct gw_trans *gw_trans;
3309 static const struct got_error *
3310 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3312 const struct got_error *err = NULL;
3313 struct gw_blame_cb_args *a = arg;
3314 struct blame_line *bline;
3316 size_t linesize = 0;
3317 struct got_commit_object *commit = NULL;
3320 time_t committer_time;
3321 enum kcgi_err kerr = KCGI_OK;
3323 if (nlines != a->nlines ||
3324 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3325 return got_error(GOT_ERR_RANGE);
3328 return NULL; /* no change in this commit */
3330 /* Annotate this line. */
3331 bline = &a->lines[lineno - 1];
3332 if (bline->annotated)
3334 err = got_object_id_str(&bline->id_str, id);
3338 err = got_object_open_as_commit(&commit, a->repo, id);
3342 err = gw_strdup_string(&bline->committer, NULL,
3343 got_object_commit_get_committer(commit));
3347 committer_time = got_object_commit_get_committer_time(commit);
3348 if (localtime_r(&committer_time, &tm) == NULL)
3349 return got_error_from_errno("localtime_r");
3350 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3351 &tm) >= sizeof(bline->datebuf)) {
3352 err = got_error(GOT_ERR_NO_SPACE);
3355 bline->annotated = 1;
3357 /* Print lines annotated so far. */
3358 bline = &a->lines[a->lineno_cur - 1];
3359 if (!bline->annotated)
3362 offset = a->line_offsets[a->lineno_cur - 1];
3363 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3364 err = got_error_from_errno("fseeko");
3368 while (bline->annotated) {
3369 char *smallerthan, *at, *nl, *committer;
3370 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3373 if (getline(&line, &linesize, a->f) == -1) {
3375 err = got_error_from_errno("getline");
3379 committer = bline->committer;
3380 smallerthan = strchr(committer, '<');
3381 if (smallerthan && smallerthan[1] != '\0')
3382 committer = smallerthan + 1;
3383 at = strchr(committer, '@');
3386 len = strlen(committer);
3388 committer[8] = '\0';
3390 nl = strchr(line, '\n');
3394 if (a->gw_trans->repo_folder == NULL) {
3395 err = gw_strdup_string(&a->gw_trans->repo_folder, "",
3400 if (a->gw_trans->repo_folder == NULL)
3403 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3404 "blame_wrapper", KATTR__MAX);
3405 if (kerr != KCGI_OK)
3407 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3408 "blame_number", KATTR__MAX);
3409 if (kerr != KCGI_OK)
3411 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3412 a->lineno_cur) == -1)
3414 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3415 if (kerr != KCGI_OK)
3417 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3418 if (kerr != KCGI_OK)
3421 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3422 "blame_hash", KATTR__MAX);
3423 if (kerr != KCGI_OK)
3425 if (asprintf(&href_diff,
3426 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3427 a->gw_trans->repo_name, bline->id_str,
3428 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3429 err = got_error_from_errno("asprintf");
3432 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3433 err = got_error_from_errno("asprintf");
3436 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3437 KATTR_HREF, href_diff, KATTR__MAX);
3438 if (kerr != KCGI_OK)
3440 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3441 if (kerr != KCGI_OK)
3443 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3444 if (kerr != KCGI_OK)
3447 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3448 "blame_date", KATTR__MAX);
3449 if (kerr != KCGI_OK)
3451 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3452 if (kerr != KCGI_OK)
3454 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3455 if (kerr != KCGI_OK)
3458 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3459 "blame_author", KATTR__MAX);
3460 if (kerr != KCGI_OK)
3462 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3463 if (kerr != KCGI_OK)
3465 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3466 if (kerr != KCGI_OK)
3469 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3470 "blame_code", KATTR__MAX);
3471 if (kerr != KCGI_OK)
3473 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3474 if (kerr != KCGI_OK)
3476 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3477 if (kerr != KCGI_OK)
3480 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3481 if (kerr != KCGI_OK)
3485 bline = &a->lines[a->lineno_cur - 1];
3493 got_object_commit_close(commit);
3495 if (err == NULL && kerr != KCGI_OK)
3496 err = gw_kcgi_error(kerr);
3500 static const struct got_error *
3501 gw_output_file_blame(struct gw_trans *gw_trans)
3503 const struct got_error *error = NULL;
3504 struct got_repository *repo = NULL;
3505 struct got_object_id *obj_id = NULL;
3506 struct got_object_id *commit_id = NULL;
3507 struct got_blob_object *blob = NULL;
3508 char *path = NULL, *in_repo_path = NULL;
3509 struct gw_blame_cb_args bca;
3513 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3517 if (asprintf(&path, "%s%s%s",
3518 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3519 gw_trans->repo_folder ? "/" : "",
3520 gw_trans->repo_file) == -1) {
3521 error = got_error_from_errno("asprintf");
3525 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3529 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3530 GOT_OBJ_TYPE_COMMIT, 1, repo);
3534 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3538 if (obj_id == NULL) {
3539 error = got_error(GOT_ERR_NO_OBJ);
3543 error = got_object_get_type(&obj_type, repo, obj_id);
3547 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3548 error = got_error(GOT_ERR_OBJ_TYPE);
3552 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3556 bca.f = got_opentemp();
3557 if (bca.f == NULL) {
3558 error = got_error_from_errno("got_opentemp");
3561 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3562 &bca.line_offsets, bca.f, blob);
3563 if (error || bca.nlines == 0)
3566 /* Don't include \n at EOF in the blame line count. */
3567 if (bca.line_offsets[bca.nlines - 1] == filesize)
3570 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3571 if (bca.lines == NULL) {
3572 error = got_error_from_errno("calloc");
3576 bca.nlines_prec = 0;
3583 bca.gw_trans = gw_trans;
3585 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3590 free(bca.line_offsets);
3596 for (i = 0; i < bca.nlines; i++) {
3597 struct blame_line *bline = &bca.lines[i];
3598 free(bline->id_str);
3599 free(bline->committer);
3602 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3603 error = got_error_from_errno("fclose");
3605 got_object_blob_close(blob);
3607 got_repo_close(repo);
3611 static const struct got_error *
3612 gw_output_blob_buf(struct gw_trans *gw_trans)
3614 const struct got_error *error = NULL;
3615 struct got_repository *repo = NULL;
3616 struct got_object_id *obj_id = NULL;
3617 struct got_object_id *commit_id = NULL;
3618 struct got_blob_object *blob = NULL;
3619 char *path = NULL, *in_repo_path = NULL;
3620 int obj_type, set_mime = 0;
3623 enum kcgi_err kerr = KCGI_OK;
3625 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3629 if (asprintf(&path, "%s%s%s",
3630 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3631 gw_trans->repo_folder ? "/" : "",
3632 gw_trans->repo_file) == -1) {
3633 error = got_error_from_errno("asprintf");
3637 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3641 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3642 GOT_OBJ_TYPE_COMMIT, 1, repo);
3646 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3650 if (obj_id == NULL) {
3651 error = got_error(GOT_ERR_NO_OBJ);
3655 error = got_object_get_type(&obj_type, repo, obj_id);
3659 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3660 error = got_error(GOT_ERR_OBJ_TYPE);
3664 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3668 hdrlen = got_object_blob_get_hdrlen(blob);
3670 error = got_object_blob_read_block(&len, blob);
3673 buf = got_object_blob_get_read_buf(blob);
3676 * Skip blob object header first time around,
3677 * which also contains a zero byte.
3680 if (set_mime == 0) {
3681 if (isbinary(buf, len - hdrlen))
3682 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3684 gw_trans->mime = KMIME_TEXT_PLAIN;
3686 error = gw_display_index(gw_trans);
3690 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3699 got_object_blob_close(blob);
3701 got_repo_close(repo);
3702 if (error == NULL && kerr != KCGI_OK)
3703 error = gw_kcgi_error(kerr);
3707 static const struct got_error *
3708 gw_output_repo_tree(struct gw_trans *gw_trans)
3710 const struct got_error *error = NULL;
3711 struct got_repository *repo = NULL;
3712 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3713 struct got_tree_object *tree = NULL;
3714 char *path = NULL, *in_repo_path = NULL;
3715 char *id_str = NULL;
3716 char *build_folder = NULL;
3717 char *href_blob = NULL, *href_blame = NULL;
3718 const char *class = NULL;
3719 int nentries, i, class_flip = 0;
3720 enum kcgi_err kerr = KCGI_OK;
3722 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3726 if (gw_trans->repo_folder != NULL) {
3727 error = gw_strdup_string(&path, gw_trans->repo_folder, NULL);
3731 error = got_repo_map_path(&in_repo_path, repo,
3732 gw_trans->repo_path, 1);
3736 path = in_repo_path;
3739 if (gw_trans->commit == NULL) {
3740 struct got_reference *head_ref;
3741 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3744 error = got_ref_resolve(&commit_id, repo, head_ref);
3747 got_ref_close(head_ref);
3750 error = got_repo_match_object_id(&commit_id, NULL,
3751 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3756 error = got_object_id_str(&gw_trans->commit, commit_id);
3760 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3764 error = got_object_open_as_tree(&tree, repo, tree_id);
3768 nentries = got_object_tree_get_nentries(tree);
3769 for (i = 0; i < nentries; i++) {
3770 struct got_tree_entry *te;
3771 const char *modestr = "";
3774 te = got_object_tree_get_entry(tree, i);
3776 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3780 mode = got_tree_entry_get_mode(te);
3781 if (got_object_tree_entry_is_submodule(te))
3783 else if (S_ISLNK(mode))
3785 else if (S_ISDIR(mode))
3787 else if (mode & S_IXUSR)
3790 if (class_flip == 0) {
3791 class = "back_lightgray";
3794 class = "back_white";
3798 if (S_ISDIR(mode)) {
3799 if (asprintf(&build_folder, "%s/%s",
3800 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3801 got_tree_entry_get_name(te)) == -1) {
3802 error = got_error_from_errno(
3806 if (asprintf(&href_blob,
3807 "?path=%s&action=%s&commit=%s&folder=%s",
3808 gw_trans->repo_name, gw_trans->action_name,
3809 gw_trans->commit, build_folder) == -1) {
3810 error = got_error_from_errno("asprintf");
3814 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3815 KATTR_ID, "tree_wrapper", KATTR__MAX);
3816 if (kerr != KCGI_OK)
3818 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3819 KATTR_ID, "tree_line", KATTR_CLASS, class,
3821 if (kerr != KCGI_OK)
3823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3824 KATTR_HREF, href_blob, KATTR_CLASS,
3825 "diff_directory", KATTR__MAX);
3826 if (kerr != KCGI_OK)
3828 kerr = khtml_puts(gw_trans->gw_html_req,
3829 got_tree_entry_get_name(te));
3830 if (kerr != KCGI_OK)
3832 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3833 if (kerr != KCGI_OK)
3835 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3836 if (kerr != KCGI_OK)
3838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3839 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3841 if (kerr != KCGI_OK)
3843 kerr = khtml_entity(gw_trans->gw_html_req,
3845 if (kerr != KCGI_OK)
3847 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3848 if (kerr != KCGI_OK)
3851 if (asprintf(&href_blob,
3852 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3853 gw_trans->repo_name, "blob", gw_trans->commit,
3854 got_tree_entry_get_name(te),
3855 gw_trans->repo_folder ?
3856 gw_trans->repo_folder : "") == -1) {
3857 error = got_error_from_errno("asprintf");
3860 if (asprintf(&href_blame,
3861 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3862 gw_trans->repo_name, "blame", gw_trans->commit,
3863 got_tree_entry_get_name(te),
3864 gw_trans->repo_folder ?
3865 gw_trans->repo_folder : "") == -1) {
3866 error = got_error_from_errno("asprintf");
3870 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3871 KATTR_ID, "tree_wrapper", KATTR__MAX);
3872 if (kerr != KCGI_OK)
3874 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3875 KATTR_ID, "tree_line", KATTR_CLASS, class,
3877 if (kerr != KCGI_OK)
3879 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3880 KATTR_HREF, href_blob, KATTR__MAX);
3881 if (kerr != KCGI_OK)
3883 kerr = khtml_puts(gw_trans->gw_html_req,
3884 got_tree_entry_get_name(te));
3885 if (kerr != KCGI_OK)
3887 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3888 if (kerr != KCGI_OK)
3890 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3891 if (kerr != KCGI_OK)
3893 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3894 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3896 if (kerr != KCGI_OK)
3899 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3900 KATTR_HREF, href_blob, KATTR__MAX);
3901 if (kerr != KCGI_OK)
3903 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3904 if (kerr != KCGI_OK)
3906 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3907 if (kerr != KCGI_OK)
3910 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3911 if (kerr != KCGI_OK)
3914 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3915 KATTR_HREF, href_blame, KATTR__MAX);
3916 if (kerr != KCGI_OK)
3918 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3919 if (kerr != KCGI_OK)
3922 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3923 if (kerr != KCGI_OK)
3931 build_folder = NULL;
3935 got_object_tree_close(tree);
3937 got_repo_close(repo);
3944 if (error == NULL && kerr != KCGI_OK)
3945 error = gw_kcgi_error(kerr);
3949 static const struct got_error *
3950 gw_output_repo_heads(struct gw_trans *gw_trans)
3952 const struct got_error *error = NULL;
3953 struct got_repository *repo = NULL;
3954 struct got_reflist_head refs;
3955 struct got_reflist_entry *re;
3956 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3957 char *href_commits = NULL;
3958 enum kcgi_err kerr = KCGI_OK;
3960 SIMPLEQ_INIT(&refs);
3962 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3966 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3971 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3972 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3973 if (kerr != KCGI_OK)
3975 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3976 KATTR_ID, "summary_heads_title", KATTR__MAX);
3977 if (kerr != KCGI_OK)
3979 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3980 if (kerr != KCGI_OK)
3982 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3983 if (kerr != KCGI_OK)
3985 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3986 KATTR_ID, "summary_heads_content", KATTR__MAX);
3987 if (kerr != KCGI_OK)
3990 SIMPLEQ_FOREACH(re, &refs, entry) {
3993 error = gw_strdup_string(&refname, NULL,
3994 got_ref_get_name(re->ref));
3998 if (strncmp(refname, "refs/heads/", 11) != 0) {
4003 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4008 if (strncmp(refname, "refs/heads/", 11) == 0)
4011 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4012 KATTR_ID, "heads_wrapper", KATTR__MAX);
4013 if (kerr != KCGI_OK)
4015 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4016 KATTR_ID, "heads_age", KATTR__MAX);
4017 if (kerr != KCGI_OK)
4019 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
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, "heads_space", KATTR__MAX);
4027 if (kerr != KCGI_OK)
4029 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4030 if (kerr != KCGI_OK)
4032 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4033 if (kerr != KCGI_OK)
4035 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4036 KATTR_ID, "head", KATTR__MAX);
4037 if (kerr != KCGI_OK)
4039 if (asprintf(&href_summary,
4040 "?path=%s&action=summary&headref=%s",
4041 gw_trans->repo_name, refname) == -1) {
4042 error = got_error_from_errno("asprintf");
4045 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4046 href_summary, KATTR__MAX);
4047 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4048 if (kerr != KCGI_OK)
4050 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4051 if (kerr != KCGI_OK)
4054 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4055 "navs_wrapper", KATTR__MAX);
4056 if (kerr != KCGI_OK)
4058 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4059 "navs", KATTR__MAX);
4060 if (kerr != KCGI_OK)
4063 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4064 href_summary, KATTR__MAX);
4065 if (kerr != KCGI_OK)
4067 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4068 if (kerr != KCGI_OK)
4070 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4071 if (kerr != KCGI_OK)
4074 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4075 if (kerr != KCGI_OK)
4077 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4078 gw_trans->repo_name, refname) == -1) {
4079 error = got_error_from_errno("asprintf");
4082 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4083 href_briefs, KATTR__MAX);
4084 if (kerr != KCGI_OK)
4086 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4087 if (kerr != KCGI_OK)
4089 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4090 if (kerr != KCGI_OK)
4093 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4094 if (kerr != KCGI_OK)
4097 if (asprintf(&href_commits,
4098 "?path=%s&action=commits&headref=%s",
4099 gw_trans->repo_name, refname) == -1) {
4100 error = got_error_from_errno("asprintf");
4103 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4104 href_commits, KATTR__MAX);
4105 if (kerr != KCGI_OK)
4107 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4108 if (kerr != KCGI_OK)
4110 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4111 if (kerr != KCGI_OK)
4114 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4115 "dotted_line", KATTR__MAX);
4116 if (kerr != KCGI_OK)
4118 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4119 if (kerr != KCGI_OK)
4123 href_summary = NULL;
4127 href_commits = NULL;
4130 got_ref_list_free(&refs);
4135 got_repo_close(repo);
4139 static const struct got_error *
4140 gw_output_site_link(struct gw_trans *gw_trans)
4142 const struct got_error *error = NULL;
4143 char *href_summary = NULL;
4144 enum kcgi_err kerr = KCGI_OK;
4146 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4147 "site_link", KATTR__MAX);
4148 if (kerr != KCGI_OK)
4150 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4152 if (kerr != KCGI_OK)
4154 kerr = khtml_puts(gw_trans->gw_html_req,
4155 gw_trans->gw_conf->got_site_link);
4156 if (kerr != KCGI_OK)
4158 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4159 if (kerr != KCGI_OK)
4162 if (gw_trans->repo_name != NULL) {
4163 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4164 if (kerr != KCGI_OK)
4166 if (asprintf(&href_summary, "?path=%s&action=summary",
4167 gw_trans->repo_name) == -1)
4169 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4170 href_summary, KATTR__MAX);
4171 if (kerr != KCGI_OK)
4173 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4174 if (kerr != KCGI_OK)
4176 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4177 if (kerr != KCGI_OK)
4179 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4180 if (kerr != KCGI_OK)
4182 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->action_name);
4183 if (kerr != KCGI_OK)
4187 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4188 if (kerr != KCGI_OK)
4195 static const struct got_error *
4196 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4198 const struct got_error *error = NULL;
4200 enum kcgi_err kerr = KCGI_OK;
4202 if (strncmp(buf, "-", 1) == 0)
4203 color = "diff_minus";
4204 else if (strncmp(buf, "+", 1) == 0)
4205 color = "diff_plus";
4206 else if (strncmp(buf, "@@", 2) == 0)
4207 color = "diff_chunk_header";
4208 else if (strncmp(buf, "@@", 2) == 0)
4209 color = "diff_chunk_header";
4210 else if (strncmp(buf, "commit +", 8) == 0)
4211 color = "diff_meta";
4212 else if (strncmp(buf, "commit -", 8) == 0)
4213 color = "diff_meta";
4214 else if (strncmp(buf, "blob +", 6) == 0)
4215 color = "diff_meta";
4216 else if (strncmp(buf, "blob -", 6) == 0)
4217 color = "diff_meta";
4218 else if (strncmp(buf, "file +", 6) == 0)
4219 color = "diff_meta";
4220 else if (strncmp(buf, "file -", 6) == 0)
4221 color = "diff_meta";
4222 else if (strncmp(buf, "from:", 5) == 0)
4223 color = "diff_author";
4224 else if (strncmp(buf, "via:", 4) == 0)
4225 color = "diff_author";
4226 else if (strncmp(buf, "date:", 5) == 0)
4227 color = "diff_date";
4228 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4229 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4230 if (error == NULL && kerr != KCGI_OK)
4231 error = gw_kcgi_error(kerr);
4236 main(int argc, char *argv[])
4238 const struct got_error *error = NULL;
4239 struct gw_trans *gw_trans;
4240 struct gw_dir *dir = NULL, *tdir;
4241 const char *page = "index";
4245 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4248 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4251 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4254 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4257 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4258 if (kerr != KCGI_OK) {
4259 error = gw_kcgi_error(kerr);
4263 if ((gw_trans->gw_conf =
4264 malloc(sizeof(struct gotweb_conf))) == NULL) {
4266 error = got_error_from_errno("malloc");
4270 TAILQ_INIT(&gw_trans->gw_dirs);
4271 TAILQ_INIT(&gw_trans->gw_headers);
4274 gw_trans->repos_total = 0;
4275 gw_trans->repo_path = NULL;
4276 gw_trans->commit = NULL;
4277 error = gw_strdup_string(&gw_trans->headref, GOT_REF_HEAD, NULL);
4280 gw_trans->mime = KMIME_TEXT_HTML;
4281 gw_trans->gw_tmpl->key = gw_templs;
4282 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4283 gw_trans->gw_tmpl->arg = gw_trans;
4284 gw_trans->gw_tmpl->cb = gw_template;
4285 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4289 error = gw_parse_querystring(gw_trans);
4293 if (gw_trans->action == GW_BLOB)
4294 error = gw_blob(gw_trans);
4296 error = gw_display_index(gw_trans);
4299 gw_trans->mime = KMIME_TEXT_PLAIN;
4300 gw_trans->action = GW_ERR;
4301 gw_display_error(gw_trans, error);
4304 free(gw_trans->gw_conf->got_repos_path);
4305 free(gw_trans->gw_conf->got_site_name);
4306 free(gw_trans->gw_conf->got_site_owner);
4307 free(gw_trans->gw_conf->got_site_link);
4308 free(gw_trans->gw_conf->got_logo);
4309 free(gw_trans->gw_conf->got_logo_url);
4310 free(gw_trans->gw_conf);
4311 free(gw_trans->commit);
4312 free(gw_trans->repo_path);
4313 free(gw_trans->repo_name);
4314 free(gw_trans->repo_file);
4315 free(gw_trans->action_name);
4316 free(gw_trans->headref);
4318 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4320 free(dir->description);
4329 khttp_free(gw_trans->gw_req);