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;
63 const char *repo_name;
71 unsigned int repos_total;
76 TAILQ_ENTRY(gw_header) entry;
77 struct got_repository *repo;
78 struct got_reflist_head refs;
79 struct got_commit_object *commit;
80 struct got_object_id *id;
84 char *commit_id; /* id_str1 */
85 char *parent_id; /* id_str2 */
90 time_t committer_time;
94 TAILQ_ENTRY(gw_dir) entry;
135 static const char *const gw_templs[TEMPL__MAX] = {
145 static const struct kvalid gw_keys[KEY__ZMAX] = {
146 { kvalid_stringne, "action" },
147 { kvalid_stringne, "commit" },
148 { kvalid_stringne, "file" },
149 { kvalid_stringne, "folder" },
150 { kvalid_stringne, "headref" },
151 { kvalid_int, "page" },
152 { kvalid_stringne, "path" },
155 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
156 static struct gw_header *gw_init_header(void);
158 static void gw_free_headers(struct gw_header *);
159 static void gw_display_error(struct gw_trans *,
160 const struct got_error *);
162 static int gw_template(size_t, void *);
164 static const struct got_error *gw_strdup_string(char **, char *,
166 static const struct got_error *gw_get_repo_description(char **,
167 struct gw_trans *, char *);
168 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
170 static const struct got_error *gw_get_time_str(char **, time_t, int);
171 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
172 char *, char *, int);
173 static const struct got_error *gw_output_file_blame(struct gw_trans *);
174 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
175 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
176 static const struct got_error *gw_output_diff(struct gw_trans *,
178 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
179 struct gw_header *, int, int);
180 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
181 static const struct got_error *gw_output_site_link(struct gw_trans *);
182 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
184 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
186 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
188 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
190 static const struct got_error *gw_gen_author_header(struct gw_trans *,
192 static const struct got_error *gw_gen_age_header(struct gw_trans *,
194 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
196 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
198 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
199 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
201 static const struct got_error *gw_display_index(struct gw_trans *);
202 static const struct got_error *gw_get_header(struct gw_trans *,
203 struct gw_header *, int);
204 static const struct got_error *gw_get_commits(struct gw_trans *,
205 struct gw_header *, int);
206 static const struct got_error *gw_get_commit(struct gw_trans *,
208 static const struct got_error *gw_apply_unveil(const char *);
209 static const struct got_error *gw_blame_cb(void *, int, int,
210 struct got_object_id *);
211 static const struct got_error *gw_load_got_paths(struct gw_trans *);
212 static const struct got_error *gw_load_got_path(struct gw_trans *,
214 static const struct got_error *gw_parse_querystring(struct gw_trans *);
215 static const struct got_error *gw_blame(struct gw_trans *);
216 static const struct got_error *gw_blob(struct gw_trans *);
217 static const struct got_error *gw_diff(struct gw_trans *);
218 static const struct got_error *gw_index(struct gw_trans *);
219 static const struct got_error *gw_commits(struct gw_trans *);
220 static const struct got_error *gw_briefs(struct gw_trans *);
221 static const struct got_error *gw_summary(struct gw_trans *);
222 static const struct got_error *gw_tree(struct gw_trans *);
223 static const struct got_error *gw_tag(struct gw_trans *);
225 struct gw_query_action {
226 unsigned int func_id;
227 const char *func_name;
228 const struct got_error *(*func_main)(struct gw_trans *);
232 enum gw_query_actions {
245 static struct gw_query_action gw_query_funcs[] = {
246 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
247 { GW_BLOB, "blob", NULL, NULL },
248 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
249 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
250 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
251 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
252 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
253 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
254 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
255 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
259 gw_get_action_name(struct gw_trans *gw_trans)
261 return gw_query_funcs[gw_trans->action].func_name;
264 static const struct got_error *
265 gw_kcgi_error(enum kcgi_err kerr)
270 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
271 return got_error(GOT_ERR_CANCELLED);
273 if (kerr == KCGI_ENOMEM)
274 return got_error_set_errno(ENOMEM,
275 kcgi_strerror(kerr));
277 if (kerr == KCGI_ENFILE)
278 return got_error_set_errno(ENFILE,
279 kcgi_strerror(kerr));
281 if (kerr == KCGI_EAGAIN)
282 return got_error_set_errno(EAGAIN,
283 kcgi_strerror(kerr));
285 if (kerr == KCGI_FORM)
286 return got_error_msg(GOT_ERR_IO,
287 kcgi_strerror(kerr));
289 return got_error_from_errno(kcgi_strerror(kerr));
292 static const struct got_error *
293 gw_apply_unveil(const char *repo_path)
295 const struct got_error *err;
297 if (repo_path && unveil(repo_path, "r") != 0)
298 return got_error_from_errno2("unveil", repo_path);
300 if (unveil("/tmp", "rwc") != 0)
301 return got_error_from_errno2("unveil", "/tmp");
303 err = got_privsep_unveil_exec_helpers();
307 if (unveil(NULL, NULL) != 0)
308 return got_error_from_errno("unveil");
313 static const struct got_error *
314 gw_strdup_string(char **s, char *str1, const char *str2)
317 /* XXX and what is the value of errno ?!? */
318 return got_error_from_errno("strdup");
324 return got_error_from_errno("strdup");
329 isbinary(const uint8_t *buf, size_t n)
333 for (i = 0; i < n; i++)
339 static const struct got_error *
340 gw_blame(struct gw_trans *gw_trans)
342 const struct got_error *error = NULL;
343 struct gw_header *header = NULL;
347 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
349 return got_error_from_errno("pledge");
351 if ((header = gw_init_header()) == NULL)
352 return got_error_from_errno("malloc");
354 error = gw_apply_unveil(gw_trans->gw_dir->path);
358 error = gw_get_header(gw_trans, header, 1);
362 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
363 "blame_header_wrapper", KATTR__MAX);
366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
367 "blame_header", KATTR__MAX);
370 error = gw_get_time_str(&age, header->committer_time,
374 error = gw_gen_age_header(gw_trans, age ?age : "");
377 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
380 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
383 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
384 "dotted_line", KATTR__MAX);
387 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
392 "blame", KATTR__MAX);
395 error = gw_output_file_blame(gw_trans);
398 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
400 got_ref_list_free(&header->refs);
401 gw_free_headers(header);
402 if (error == NULL && kerr != KCGI_OK)
403 error = gw_kcgi_error(kerr);
407 static const struct got_error *
408 gw_blob(struct gw_trans *gw_trans)
410 const struct got_error *error = NULL;
411 struct gw_header *header = NULL;
413 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
415 return got_error_from_errno("pledge");
417 if ((header = gw_init_header()) == NULL)
418 return got_error_from_errno("malloc");
420 error = gw_apply_unveil(gw_trans->gw_dir->path);
424 error = gw_get_header(gw_trans, header, 1);
428 error = gw_output_blob_buf(gw_trans);
430 got_ref_list_free(&header->refs);
431 gw_free_headers(header);
435 static const struct got_error *
436 gw_diff(struct gw_trans *gw_trans)
438 const struct got_error *error = NULL;
439 struct gw_header *header = NULL;
441 enum kcgi_err kerr = KCGI_OK;
443 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
445 return got_error_from_errno("pledge");
447 if ((header = gw_init_header()) == NULL)
448 return got_error_from_errno("malloc");
450 error = gw_apply_unveil(gw_trans->gw_dir->path);
454 error = gw_get_header(gw_trans, header, 1);
458 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
459 "diff_header_wrapper", KATTR__MAX);
462 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
463 "diff_header", KATTR__MAX);
466 error = gw_gen_diff_header(gw_trans, header->parent_id,
470 error = gw_gen_commit_header(gw_trans, header->commit_id,
474 error = gw_gen_tree_header(gw_trans, header->tree_id);
477 error = gw_gen_author_header(gw_trans, header->author);
480 error = gw_gen_committer_header(gw_trans, header->author);
483 error = gw_get_time_str(&age, header->committer_time,
487 error = gw_gen_age_header(gw_trans, age ?age : "");
490 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
493 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
496 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
497 "dotted_line", KATTR__MAX);
500 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
504 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
508 error = gw_output_diff(gw_trans, header);
512 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
514 got_ref_list_free(&header->refs);
515 gw_free_headers(header);
517 if (error == NULL && kerr != KCGI_OK)
518 error = gw_kcgi_error(kerr);
522 static const struct got_error *
523 gw_index(struct gw_trans *gw_trans)
525 const struct got_error *error = NULL;
526 struct gw_dir *gw_dir = NULL;
527 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
528 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
529 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
532 if (pledge("stdio rpath proc exec sendfd unveil",
534 error = got_error_from_errno("pledge");
538 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
542 error = gw_load_got_paths(gw_trans);
546 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
547 "index_header", KATTR__MAX);
549 return gw_kcgi_error(kerr);
550 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
551 "index_header_project", KATTR__MAX);
553 return gw_kcgi_error(kerr);
554 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
556 return gw_kcgi_error(kerr);
557 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
559 return gw_kcgi_error(kerr);
561 if (gw_trans->gw_conf->got_show_repo_description) {
562 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
563 "index_header_description", KATTR__MAX);
565 return gw_kcgi_error(kerr);
566 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
568 return gw_kcgi_error(kerr);
569 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
571 return gw_kcgi_error(kerr);
574 if (gw_trans->gw_conf->got_show_repo_owner) {
575 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
576 "index_header_owner", KATTR__MAX);
578 return gw_kcgi_error(kerr);
579 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
581 return gw_kcgi_error(kerr);
582 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
584 return gw_kcgi_error(kerr);
587 if (gw_trans->gw_conf->got_show_repo_age) {
588 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
589 "index_header_age", KATTR__MAX);
591 return gw_kcgi_error(kerr);
592 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
594 return gw_kcgi_error(kerr);
595 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
597 return gw_kcgi_error(kerr);
600 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
602 return gw_kcgi_error(kerr);
604 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
605 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
606 "index_wrapper", KATTR__MAX);
608 return gw_kcgi_error(kerr);
609 kerr = khtml_puts(gw_trans->gw_html_req,
610 "No repositories found in ");
612 return gw_kcgi_error(kerr);
613 kerr = khtml_puts(gw_trans->gw_html_req,
614 gw_trans->gw_conf->got_repos_path);
616 return gw_kcgi_error(kerr);
617 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
619 return gw_kcgi_error(kerr);
620 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
621 "dotted_line", KATTR__MAX);
623 return gw_kcgi_error(kerr);
624 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
626 return gw_kcgi_error(kerr);
630 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
633 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
634 if (gw_trans->page > 0 && (gw_trans->page *
635 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
642 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
643 "index_wrapper", KATTR__MAX);
647 if (asprintf(&href_summary, "?path=%s&action=summary",
648 gw_dir->name) == -1) {
649 error = got_error_from_errno("asprintf");
652 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
653 "index_project", KATTR__MAX);
656 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
657 href_summary, KATTR__MAX);
660 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
663 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
666 if (gw_trans->gw_conf->got_show_repo_description) {
667 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
668 KATTR_ID, "index_project_description", KATTR__MAX);
671 kerr = khtml_puts(gw_trans->gw_html_req,
672 gw_dir->description ? gw_dir->description : "");
675 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
679 if (gw_trans->gw_conf->got_show_repo_owner) {
680 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
681 KATTR_ID, "index_project_owner", KATTR__MAX);
684 kerr = khtml_puts(gw_trans->gw_html_req,
685 gw_dir->owner ? gw_dir->owner : "");
688 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
692 if (gw_trans->gw_conf->got_show_repo_age) {
693 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
694 KATTR_ID, "index_project_age", KATTR__MAX);
697 kerr = khtml_puts(gw_trans->gw_html_req,
698 gw_dir->age ? gw_dir->age : "");
701 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
706 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
707 "navs_wrapper", KATTR__MAX);
710 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
715 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
716 href_summary, KATTR__MAX);
719 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
722 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
726 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
730 if (asprintf(&href_briefs, "?path=%s&action=briefs",
731 gw_dir->name) == -1) {
732 error = got_error_from_errno("asprintf");
735 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
736 href_briefs, KATTR__MAX);
739 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
742 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
744 error = gw_kcgi_error(kerr);
746 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
750 if (asprintf(&href_commits, "?path=%s&action=commits",
751 gw_dir->name) == -1) {
752 error = got_error_from_errno("asprintf");
755 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
756 href_commits, KATTR__MAX);
759 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
762 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
766 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
770 if (asprintf(&href_tree, "?path=%s&action=tree",
771 gw_dir->name) == -1) {
772 error = got_error_from_errno("asprintf");
775 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
776 href_tree, KATTR__MAX);
779 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
783 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
786 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
787 "dotted_line", KATTR__MAX);
790 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
803 if (gw_trans->gw_conf->got_max_repos_display == 0)
806 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
807 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
808 KATTR_ID, "np_wrapper", KATTR__MAX);
811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
812 KATTR_ID, "nav_prev", KATTR__MAX);
815 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
816 (gw_trans->page > 0) &&
817 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
818 prev_disp == gw_trans->repos_total)) {
819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
820 KATTR_ID, "np_wrapper", KATTR__MAX);
823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
824 KATTR_ID, "nav_prev", KATTR__MAX);
829 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
830 (gw_trans->page > 0) &&
831 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
832 prev_disp == gw_trans->repos_total)) {
833 if (asprintf(&href_prev, "?page=%d",
834 gw_trans->page - 1) == -1) {
835 error = got_error_from_errno("asprintf");
838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
839 KATTR_HREF, href_prev, KATTR__MAX);
842 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
845 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
850 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
852 return gw_kcgi_error(kerr);
854 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
855 next_disp == gw_trans->gw_conf->got_max_repos_display &&
856 dir_c != (gw_trans->page + 1) *
857 gw_trans->gw_conf->got_max_repos_display) {
858 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
859 KATTR_ID, "nav_next", KATTR__MAX);
862 if (asprintf(&href_next, "?page=%d",
863 gw_trans->page + 1) == -1) {
864 error = got_error_from_errno("calloc");
867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
868 KATTR_HREF, href_next, KATTR__MAX);
871 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
874 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
881 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
882 (gw_trans->page > 0) &&
883 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
884 prev_disp == gw_trans->repos_total)) {
885 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
898 if (error == NULL && kerr != KCGI_OK)
899 error = gw_kcgi_error(kerr);
903 static const struct got_error *
904 gw_commits(struct gw_trans *gw_trans)
906 const struct got_error *error = NULL;
907 struct gw_header *header = NULL, *n_header = NULL;
909 char *href_diff = NULL, *href_blob = NULL;
910 enum kcgi_err kerr = KCGI_OK;
912 if ((header = gw_init_header()) == NULL)
913 return got_error_from_errno("malloc");
915 if (pledge("stdio rpath proc exec sendfd unveil",
917 error = got_error_from_errno("pledge");
921 error = gw_apply_unveil(gw_trans->gw_dir->path);
925 error = gw_get_header(gw_trans, header,
926 gw_trans->gw_conf->got_max_commits_display);
930 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
931 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
932 "commits_line_wrapper", KATTR__MAX);
935 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
939 error = gw_gen_author_header(gw_trans, n_header->author);
942 error = gw_gen_committer_header(gw_trans, n_header->author);
945 error = gw_get_time_str(&age, n_header->committer_time,
949 error = gw_gen_age_header(gw_trans, age ?age : "");
952 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
956 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
957 "dotted_line", KATTR__MAX);
960 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
964 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
965 "commit", KATTR__MAX);
968 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
971 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
975 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
976 gw_trans->repo_name, n_header->commit_id) == -1) {
977 error = got_error_from_errno("asprintf");
980 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
981 KATTR_ID, "navs_wrapper", KATTR__MAX);
984 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
985 KATTR_ID, "navs", KATTR__MAX);
988 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
989 KATTR_HREF, href_diff, KATTR__MAX);
992 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
995 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
999 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1000 if (kerr != KCGI_OK)
1003 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1004 gw_trans->repo_name, n_header->commit_id) == -1) {
1005 error = got_error_from_errno("asprintf");
1008 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1009 KATTR_HREF, href_blob, KATTR__MAX);
1010 if (kerr != KCGI_OK)
1012 khtml_puts(gw_trans->gw_html_req, "tree");
1013 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1014 if (kerr != KCGI_OK)
1016 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1017 if (kerr != KCGI_OK)
1020 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1021 "solid_line", KATTR__MAX);
1022 if (kerr != KCGI_OK)
1024 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1025 if (kerr != KCGI_OK)
1032 got_ref_list_free(&header->refs);
1033 gw_free_headers(header);
1034 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1035 gw_free_headers(n_header);
1039 if (error == NULL && kerr != KCGI_OK)
1040 error = gw_kcgi_error(kerr);
1044 static const struct got_error *
1045 gw_briefs(struct gw_trans *gw_trans)
1047 const struct got_error *error = NULL;
1048 struct gw_header *header = NULL, *n_header = NULL;
1049 char *age = NULL, *age_html = NULL;
1050 char *href_diff = NULL, *href_blob = NULL;
1051 char *newline, *smallerthan;
1052 enum kcgi_err kerr = KCGI_OK;
1054 if ((header = gw_init_header()) == NULL)
1055 return got_error_from_errno("malloc");
1057 if (pledge("stdio rpath proc exec sendfd unveil",
1059 error = got_error_from_errno("pledge");
1063 error = gw_apply_unveil(gw_trans->gw_dir->path);
1067 if (gw_trans->action == GW_SUMMARY)
1068 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1070 error = gw_get_header(gw_trans, header,
1071 gw_trans->gw_conf->got_max_commits_display);
1075 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1076 error = gw_get_time_str(&age, n_header->committer_time,
1081 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1082 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1083 if (kerr != KCGI_OK)
1086 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1087 KATTR_ID, "briefs_age", KATTR__MAX);
1088 if (kerr != KCGI_OK)
1090 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
1091 error = got_error_from_errno("asprintf");
1094 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
1095 if (kerr != KCGI_OK)
1097 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1098 if (kerr != KCGI_OK)
1101 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1102 KATTR_ID, "briefs_author", KATTR__MAX);
1103 if (kerr != KCGI_OK)
1105 smallerthan = strchr(n_header->author, '<');
1107 *smallerthan = '\0';
1108 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1109 if (kerr != KCGI_OK)
1111 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1112 if (kerr != KCGI_OK)
1115 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1116 gw_trans->repo_name, n_header->commit_id) == -1) {
1117 error = got_error_from_errno("asprintf");
1120 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1121 KATTR_ID, "briefs_log", KATTR__MAX);
1122 if (kerr != KCGI_OK)
1124 newline = strchr(n_header->commit_msg, '\n');
1127 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1128 KATTR_HREF, href_diff, KATTR__MAX);
1129 if (kerr != KCGI_OK)
1131 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1132 if (kerr != KCGI_OK)
1134 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1135 if (kerr != KCGI_OK)
1138 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1139 KATTR_ID, "navs_wrapper", KATTR__MAX);
1140 if (kerr != KCGI_OK)
1142 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1143 KATTR_ID, "navs", KATTR__MAX);
1144 if (kerr != KCGI_OK)
1146 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1147 KATTR_HREF, href_diff, KATTR__MAX);
1148 if (kerr != KCGI_OK)
1150 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1151 if (kerr != KCGI_OK)
1153 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1154 if (kerr != KCGI_OK)
1157 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1158 if (kerr != KCGI_OK)
1161 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1162 gw_trans->repo_name, n_header->commit_id) == -1) {
1163 error = got_error_from_errno("asprintf");
1166 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1167 KATTR_HREF, href_blob, KATTR__MAX);
1168 if (kerr != KCGI_OK)
1170 khtml_puts(gw_trans->gw_html_req, "tree");
1171 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1172 if (kerr != KCGI_OK)
1174 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1175 if (kerr != KCGI_OK)
1178 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1179 KATTR_ID, "dotted_line", KATTR__MAX);
1180 if (kerr != KCGI_OK)
1182 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1183 if (kerr != KCGI_OK)
1196 got_ref_list_free(&header->refs);
1197 gw_free_headers(header);
1198 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1199 gw_free_headers(n_header);
1204 if (error == NULL && kerr != KCGI_OK)
1205 error = gw_kcgi_error(kerr);
1209 static const struct got_error *
1210 gw_summary(struct gw_trans *gw_trans)
1212 const struct got_error *error = NULL;
1214 enum kcgi_err kerr = KCGI_OK;
1216 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1217 return got_error_from_errno("pledge");
1219 /* unveil is applied with gw_briefs below */
1221 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1222 "summary_wrapper", KATTR__MAX);
1223 if (kerr != KCGI_OK)
1224 return gw_kcgi_error(kerr);
1226 if (gw_trans->gw_conf->got_show_repo_description &&
1227 gw_trans->gw_dir->description != NULL &&
1228 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1229 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1230 KATTR_ID, "description_title", KATTR__MAX);
1231 if (kerr != KCGI_OK)
1233 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1234 if (kerr != KCGI_OK)
1236 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1237 if (kerr != KCGI_OK)
1239 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1240 KATTR_ID, "description", KATTR__MAX);
1241 if (kerr != KCGI_OK)
1243 kerr = khtml_puts(gw_trans->gw_html_req,
1244 gw_trans->gw_dir->description);
1245 if (kerr != KCGI_OK)
1247 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1248 if (kerr != KCGI_OK)
1252 if (gw_trans->gw_conf->got_show_repo_owner &&
1253 gw_trans->gw_dir->owner != NULL &&
1254 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1255 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1256 KATTR_ID, "repo_owner_title", KATTR__MAX);
1257 if (kerr != KCGI_OK)
1259 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1260 if (kerr != KCGI_OK)
1262 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1263 if (kerr != KCGI_OK)
1265 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1266 KATTR_ID, "repo_owner", KATTR__MAX);
1267 if (kerr != KCGI_OK)
1269 kerr = khtml_puts(gw_trans->gw_html_req,
1270 gw_trans->gw_dir->owner);
1271 if (kerr != KCGI_OK)
1273 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1274 if (kerr != KCGI_OK)
1278 if (gw_trans->gw_conf->got_show_repo_age) {
1279 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1280 "refs/heads", TM_LONG);
1284 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1285 KATTR_ID, "last_change_title", KATTR__MAX);
1286 if (kerr != KCGI_OK)
1288 kerr = khtml_puts(gw_trans->gw_html_req,
1290 if (kerr != KCGI_OK)
1292 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1293 if (kerr != KCGI_OK)
1295 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1296 KATTR_ID, "last_change", KATTR__MAX);
1297 if (kerr != KCGI_OK)
1299 kerr = khtml_puts(gw_trans->gw_html_req, age);
1300 if (kerr != KCGI_OK)
1302 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1303 if (kerr != KCGI_OK)
1308 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1309 gw_trans->gw_dir->url != NULL &&
1310 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1311 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1312 KATTR_ID, "cloneurl_title", KATTR__MAX);
1313 if (kerr != KCGI_OK)
1315 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1316 if (kerr != KCGI_OK)
1318 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1319 if (kerr != KCGI_OK)
1321 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1322 KATTR_ID, "cloneurl", KATTR__MAX);
1323 if (kerr != KCGI_OK)
1325 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1326 if (kerr != KCGI_OK)
1328 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1329 if (kerr != KCGI_OK)
1333 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1334 if (kerr != KCGI_OK)
1337 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1338 "briefs_title_wrapper", KATTR__MAX);
1339 if (kerr != KCGI_OK)
1341 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1342 "briefs_title", KATTR__MAX);
1343 if (kerr != KCGI_OK)
1345 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1346 if (kerr != KCGI_OK)
1348 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1349 if (kerr != KCGI_OK)
1351 error = gw_briefs(gw_trans);
1355 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1360 error = gw_output_repo_heads(gw_trans);
1363 if (error == NULL && kerr != KCGI_OK)
1364 error = gw_kcgi_error(kerr);
1368 static const struct got_error *
1369 gw_tree(struct gw_trans *gw_trans)
1371 const struct got_error *error = NULL;
1372 struct gw_header *header = NULL;
1373 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1374 char *age = NULL, *age_html = NULL;
1377 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1378 return got_error_from_errno("pledge");
1380 if ((header = gw_init_header()) == NULL)
1381 return got_error_from_errno("malloc");
1383 error = gw_apply_unveil(gw_trans->gw_dir->path);
1387 error = gw_get_header(gw_trans, header, 1);
1391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1392 "tree_header_wrapper", KATTR__MAX);
1393 if (kerr != KCGI_OK)
1395 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1396 "tree_header", KATTR__MAX);
1397 if (kerr != KCGI_OK)
1399 error = gw_gen_tree_header(gw_trans, header->tree_id);
1402 error = gw_get_time_str(&age, header->committer_time,
1406 error = gw_gen_age_header(gw_trans, age ?age : "");
1409 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1412 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1413 if (kerr != KCGI_OK)
1415 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1416 "dotted_line", KATTR__MAX);
1417 if (kerr != KCGI_OK)
1419 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1420 if (kerr != KCGI_OK)
1423 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1424 "tree", KATTR__MAX);
1425 if (kerr != KCGI_OK)
1427 error = gw_output_repo_tree(gw_trans);
1431 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1433 got_ref_list_free(&header->refs);
1434 gw_free_headers(header);
1435 free(tree_html_disp);
1440 if (error == NULL && kerr != KCGI_OK)
1441 error = gw_kcgi_error(kerr);
1445 static const struct got_error *
1446 gw_tag(struct gw_trans *gw_trans)
1448 const struct got_error *error = NULL;
1449 struct gw_header *header = NULL;
1452 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1453 return got_error_from_errno("pledge");
1455 if ((header = gw_init_header()) == NULL)
1456 return got_error_from_errno("malloc");
1458 error = gw_apply_unveil(gw_trans->gw_dir->path);
1462 error = gw_get_header(gw_trans, header, 1);
1466 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1467 "tag_header_wrapper", KATTR__MAX);
1468 if (kerr != KCGI_OK)
1470 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1471 "tag_header", KATTR__MAX);
1472 if (kerr != KCGI_OK)
1474 error = gw_gen_commit_header(gw_trans, header->commit_id,
1478 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1481 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1482 if (kerr != KCGI_OK)
1484 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1485 "dotted_line", KATTR__MAX);
1486 if (kerr != KCGI_OK)
1488 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1489 if (kerr != KCGI_OK)
1492 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1493 "tree", KATTR__MAX);
1494 if (kerr != KCGI_OK)
1497 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1501 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1503 got_ref_list_free(&header->refs);
1504 gw_free_headers(header);
1505 if (error == NULL && kerr != KCGI_OK)
1506 error = gw_kcgi_error(kerr);
1510 static const struct got_error *
1511 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1513 const struct got_error *error = NULL;
1518 if (asprintf(&dir_test, "%s/%s/%s",
1519 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1520 GOTWEB_GIT_DIR) == -1)
1521 return got_error_from_errno("asprintf");
1523 dt = opendir(dir_test);
1527 error = gw_strdup_string(&gw_dir->path, dir_test, NULL);
1534 if (asprintf(&dir_test, "%s/%s/%s",
1535 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1536 GOTWEB_GOT_DIR) == -1)
1537 return got_error_from_errno("asprintf");
1539 dt = opendir(dir_test);
1544 error = got_error(GOT_ERR_NOT_GIT_REPO);
1548 if (asprintf(&dir_test, "%s/%s",
1549 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1550 return got_error_from_errno("asprintf");
1552 error = gw_strdup_string(&gw_dir->path, dir_test, NULL);
1558 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1562 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1565 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1566 "refs/heads", TM_DIFF);
1569 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1577 static const struct got_error *
1578 gw_load_got_paths(struct gw_trans *gw_trans)
1580 const struct got_error *error = NULL;
1582 struct dirent **sd_dent;
1583 struct gw_dir *gw_dir;
1585 unsigned int d_cnt, d_i;
1587 d = opendir(gw_trans->gw_conf->got_repos_path);
1589 error = got_error_from_errno2("opendir",
1590 gw_trans->gw_conf->got_repos_path);
1594 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1597 error = got_error_from_errno2("scandir",
1598 gw_trans->gw_conf->got_repos_path);
1602 for (d_i = 0; d_i < d_cnt; d_i++) {
1603 if (gw_trans->gw_conf->got_max_repos > 0 &&
1604 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1605 break; /* account for parent and self */
1607 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1608 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1611 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1615 error = gw_load_got_path(gw_trans, gw_dir);
1616 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1623 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1624 !got_path_dir_is_empty(gw_dir->path)) {
1625 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1627 gw_trans->repos_total++;
1635 static const struct got_error *
1636 gw_parse_querystring(struct gw_trans *gw_trans)
1638 const struct got_error *error = NULL;
1640 struct gw_query_action *action = NULL;
1643 if (gw_trans->gw_req->fieldnmap[0]) {
1644 error = got_error_from_errno("bad parse");
1646 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1647 /* define gw_trans->repo_path */
1648 gw_trans->repo_name = p->parsed.s;
1650 if (asprintf(&gw_trans->repo_path, "%s/%s",
1651 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1652 return got_error_from_errno("asprintf");
1654 /* get action and set function */
1655 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1656 for (i = 0; i < nitems(gw_query_funcs); i++) {
1657 action = &gw_query_funcs[i];
1658 if (action->func_name == NULL ||
1659 strcmp(action->func_name, p->parsed.s))
1662 gw_trans->action = i;
1666 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1667 if (asprintf(&gw_trans->commit, "%s",
1669 return got_error_from_errno("asprintf");
1672 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE])) {
1673 if (asprintf(&gw_trans->repo_file, "%s",
1675 return got_error_from_errno("asprintf");
1678 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1679 if (asprintf(&gw_trans->repo_folder, "%s",
1681 return got_error_from_errno("asprintf");
1684 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF])) {
1685 if (asprintf(&gw_trans->headref, "%s",
1687 return got_error_from_errno("asprintf");
1690 if (action == NULL) {
1691 error = got_error_from_errno("invalid action");
1694 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1698 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1702 gw_trans->action = GW_INDEX;
1704 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1705 gw_trans->page = p->parsed.i;
1710 static const struct got_error *
1711 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1713 const struct got_error *error;
1715 *gw_dir = malloc(sizeof(**gw_dir));
1716 if (*gw_dir == NULL)
1717 return got_error_from_errno("malloc");
1719 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1720 error = got_error_from_errno("asprintf");
1729 static const struct got_error *
1730 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1734 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1735 if (kerr != KCGI_OK)
1736 return gw_kcgi_error(kerr);
1737 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1739 if (kerr != KCGI_OK)
1740 return gw_kcgi_error(kerr);
1741 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1743 if (kerr != KCGI_OK)
1744 return gw_kcgi_error(kerr);
1745 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1747 if (kerr != KCGI_OK)
1748 return gw_kcgi_error(kerr);
1749 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1750 if (kerr != KCGI_OK)
1751 return gw_kcgi_error(kerr);
1752 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1754 if (kerr != KCGI_OK)
1755 return gw_kcgi_error(kerr);
1757 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1758 kerr = khttp_head(gw_trans->gw_req,
1759 kresps[KRESP_CONTENT_DISPOSITION],
1760 "attachment; filename=%s", gw_trans->repo_file);
1761 if (kerr != KCGI_OK)
1762 return gw_kcgi_error(kerr);
1765 kerr = khttp_body(gw_trans->gw_req);
1766 return gw_kcgi_error(kerr);
1769 static const struct got_error *
1770 gw_display_index(struct gw_trans *gw_trans)
1772 const struct got_error *error;
1775 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1779 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1780 if (kerr != KCGI_OK)
1781 return gw_kcgi_error(kerr);
1783 if (gw_trans->action != GW_BLOB) {
1784 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1785 gw_query_funcs[gw_trans->action].template);
1786 if (kerr != KCGI_OK) {
1787 khtml_close(gw_trans->gw_html_req);
1788 return gw_kcgi_error(kerr);
1792 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1796 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1798 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1801 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1803 khtml_puts(gw_trans->gw_html_req, err->msg);
1804 khtml_close(gw_trans->gw_html_req);
1808 gw_template(size_t key, void *arg)
1810 const struct got_error *error = NULL;
1812 struct gw_trans *gw_trans = arg;
1813 char *img_src = NULL;
1817 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1818 KATTR_NAME, "viewport",
1819 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1821 if (kerr != KCGI_OK)
1823 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1824 if (kerr != KCGI_OK)
1826 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1827 KATTR_CHARSET, "utf-8",
1829 if (kerr != KCGI_OK)
1831 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1832 if (kerr != KCGI_OK)
1834 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1835 KATTR_NAME, "msapplication-TileColor",
1836 KATTR_CONTENT, "#da532c", KATTR__MAX);
1837 if (kerr != KCGI_OK)
1839 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1840 if (kerr != KCGI_OK)
1842 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1843 KATTR_NAME, "theme-color",
1844 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1845 if (kerr != KCGI_OK)
1847 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1848 if (kerr != KCGI_OK)
1850 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1851 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1852 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1853 if (kerr != KCGI_OK)
1855 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1856 if (kerr != KCGI_OK)
1858 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1859 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1860 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1861 if (kerr != KCGI_OK)
1863 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1864 if (kerr != KCGI_OK)
1866 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1867 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1868 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1869 if (kerr != KCGI_OK)
1871 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1872 if (kerr != KCGI_OK)
1874 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1875 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1877 if (kerr != KCGI_OK)
1879 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1880 if (kerr != KCGI_OK)
1882 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1883 KATTR_REL, "mask-icon", KATTR_HREF,
1884 "/safari-pinned-tab.svg", KATTR__MAX);
1885 if (kerr != KCGI_OK)
1887 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1888 if (kerr != KCGI_OK)
1890 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1891 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1892 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1893 if (kerr != KCGI_OK)
1895 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1896 if (kerr != KCGI_OK)
1900 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1901 KATTR_ID, "got_link", KATTR__MAX);
1902 if (kerr != KCGI_OK)
1904 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1905 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1906 KATTR_TARGET, "_sotd", KATTR__MAX);
1907 if (kerr != KCGI_OK)
1909 if (asprintf(&img_src, "/%s",
1910 gw_trans->gw_conf->got_logo) == -1)
1912 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1913 KATTR_SRC, img_src, KATTR__MAX);
1914 if (kerr != KCGI_OK) {
1918 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1919 if (kerr != KCGI_OK) {
1924 case (TEMPL_SITEPATH):
1925 error = gw_output_site_link(gw_trans);
1930 if (gw_trans->gw_conf->got_site_name != NULL) {
1931 kerr = khtml_puts(gw_trans->gw_html_req,
1932 gw_trans->gw_conf->got_site_name);
1933 if (kerr != KCGI_OK)
1937 case (TEMPL_SEARCH):
1939 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1940 "search", KATTR__MAX);
1941 if (kerr != KCGI_OK)
1943 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1944 KATTR_METHOD, "POST", KATTR__MAX);
1945 if (kerr != KCGI_OK)
1947 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1948 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1949 KATTR_MAXLENGTH, "50", KATTR__MAX);
1950 if (kerr != KCGI_OK)
1952 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1954 if (kerr != KCGI_OK)
1956 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1957 if (kerr != KCGI_OK)
1959 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1960 if (kerr != KCGI_OK)
1963 case(TEMPL_SITEOWNER):
1964 if (gw_trans->gw_conf->got_site_owner != NULL &&
1965 gw_trans->gw_conf->got_show_site_owner) {
1966 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1967 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1968 if (kerr != KCGI_OK)
1970 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1971 KATTR_ID, "site_owner", KATTR__MAX);
1972 if (kerr != KCGI_OK)
1974 kerr = khtml_puts(gw_trans->gw_html_req,
1975 gw_trans->gw_conf->got_site_owner);
1976 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1977 if (kerr != KCGI_OK)
1981 case(TEMPL_CONTENT):
1982 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1984 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1985 KATTR_ID, "tmpl_err", KATTR__MAX);
1986 if (kerr != KCGI_OK)
1988 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1989 if (kerr != KCGI_OK)
1991 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1992 if (kerr != KCGI_OK)
1994 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1995 if (kerr != KCGI_OK)
2005 static const struct got_error *
2006 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2008 const struct got_error *error = NULL;
2009 char *ref_str = NULL;
2010 enum kcgi_err kerr = KCGI_OK;
2012 if (strcmp(str2, "") != 0) {
2013 if (asprintf(&ref_str, "(%s)", str2) == -1) {
2014 error = got_error_from_errno("asprintf");
2018 error = gw_strdup_string(&ref_str, "", NULL);
2023 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2024 KATTR_ID, "header_commit_title", KATTR__MAX);
2025 if (kerr != KCGI_OK)
2027 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2028 if (kerr != KCGI_OK)
2030 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2031 if (kerr != KCGI_OK)
2033 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2034 KATTR_ID, "header_commit", KATTR__MAX);
2035 if (kerr != KCGI_OK)
2037 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2038 if (kerr != KCGI_OK)
2040 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2041 if (kerr != KCGI_OK)
2043 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
2044 if (kerr != KCGI_OK)
2046 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2048 if (error == NULL && kerr != KCGI_OK)
2049 error = gw_kcgi_error(kerr);
2053 static const struct got_error *
2054 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2056 const struct got_error *error = NULL;
2057 enum kcgi_err kerr = KCGI_OK;
2059 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2060 KATTR_ID, "header_diff_title", KATTR__MAX);
2061 if (kerr != KCGI_OK)
2063 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2064 if (kerr != KCGI_OK)
2066 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2067 if (kerr != KCGI_OK)
2069 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2070 KATTR_ID, "header_diff", KATTR__MAX);
2071 if (kerr != KCGI_OK)
2073 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2074 if (kerr != KCGI_OK)
2076 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2077 if (kerr != KCGI_OK)
2079 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2080 if (kerr != KCGI_OK)
2082 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2084 if (error == NULL && kerr != KCGI_OK)
2085 error = gw_kcgi_error(kerr);
2089 static const struct got_error *
2090 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2092 const struct got_error *error = NULL;
2093 enum kcgi_err kerr = KCGI_OK;
2095 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2096 KATTR_ID, "header_age_title", KATTR__MAX);
2097 if (kerr != KCGI_OK)
2099 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2100 if (kerr != KCGI_OK)
2102 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2103 if (kerr != KCGI_OK)
2105 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2106 KATTR_ID, "header_age", KATTR__MAX);
2107 if (kerr != KCGI_OK)
2109 kerr = khtml_puts(gw_trans->gw_html_req, str);
2110 if (kerr != KCGI_OK)
2112 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2114 if (error == NULL && kerr != KCGI_OK)
2115 error = gw_kcgi_error(kerr);
2119 static const struct got_error *
2120 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2122 const struct got_error *error = NULL;
2125 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2126 KATTR_ID, "header_author_title", KATTR__MAX);
2127 if (kerr != KCGI_OK)
2129 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2130 if (kerr != KCGI_OK)
2132 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2133 if (kerr != KCGI_OK)
2135 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2136 KATTR_ID, "header_author", KATTR__MAX);
2137 if (kerr != KCGI_OK)
2139 kerr = khtml_puts(gw_trans->gw_html_req, str);
2140 if (kerr != KCGI_OK)
2142 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2144 if (error == NULL && kerr != KCGI_OK)
2145 error = gw_kcgi_error(kerr);
2149 static const struct got_error *
2150 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2152 const struct got_error *error = NULL;
2155 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2156 KATTR_ID, "header_committer_title", KATTR__MAX);
2157 if (kerr != KCGI_OK)
2159 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2160 if (kerr != KCGI_OK)
2162 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2163 if (kerr != KCGI_OK)
2165 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2166 KATTR_ID, "header_committer", KATTR__MAX);
2167 if (kerr != KCGI_OK)
2169 kerr = khtml_puts(gw_trans->gw_html_req, str);
2170 if (kerr != KCGI_OK)
2172 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2174 if (error == NULL && kerr != KCGI_OK)
2175 error = gw_kcgi_error(kerr);
2179 static const struct got_error *
2180 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2182 const struct got_error *error = NULL;
2185 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2186 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2187 if (kerr != KCGI_OK)
2189 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2190 if (kerr != KCGI_OK)
2192 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2193 if (kerr != KCGI_OK)
2195 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2196 KATTR_ID, "header_commit_msg", KATTR__MAX);
2197 if (kerr != KCGI_OK)
2199 kerr = khttp_puts(gw_trans->gw_req, str);
2200 if (kerr != KCGI_OK)
2202 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2204 if (error == NULL && kerr != KCGI_OK)
2205 error = gw_kcgi_error(kerr);
2209 static const struct got_error *
2210 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2212 const struct got_error *error = NULL;
2215 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2216 KATTR_ID, "header_tree_title", KATTR__MAX);
2217 if (kerr != KCGI_OK)
2219 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2220 if (kerr != KCGI_OK)
2222 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2223 if (kerr != KCGI_OK)
2225 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2226 KATTR_ID, "header_tree", KATTR__MAX);
2227 if (kerr != KCGI_OK)
2229 kerr = khtml_puts(gw_trans->gw_html_req, str);
2230 if (kerr != KCGI_OK)
2232 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2234 if (error == NULL && kerr != KCGI_OK)
2235 error = gw_kcgi_error(kerr);
2239 static const struct got_error *
2240 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2243 const struct got_error *error = NULL;
2245 char *d_file = NULL;
2249 *description = NULL;
2250 if (gw_trans->gw_conf->got_show_repo_description == 0)
2253 if (asprintf(&d_file, "%s/description", dir) == -1)
2254 return got_error_from_errno("asprintf");
2256 f = fopen(d_file, "r");
2258 if (errno == ENOENT || errno == EACCES)
2260 error = got_error_from_errno2("fopen", d_file);
2264 if (fseek(f, 0, SEEK_END) == -1) {
2265 error = got_ferror(f, GOT_ERR_IO);
2270 error = got_ferror(f, GOT_ERR_IO);
2273 if (fseek(f, 0, SEEK_SET) == -1) {
2274 error = got_ferror(f, GOT_ERR_IO);
2277 *description = calloc(len + 1, sizeof(**description));
2278 if (*description == NULL) {
2279 error = got_error_from_errno("calloc");
2283 n = fread(*description, 1, len, f);
2284 if (n == 0 && ferror(f))
2285 error = got_ferror(f, GOT_ERR_IO);
2287 if (f != NULL && fclose(f) == -1 && error == NULL)
2288 error = got_error_from_errno("fclose");
2293 static const struct got_error *
2294 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2298 char *years = "years ago", *months = "months ago";
2299 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2300 char *minutes = "minutes ago", *seconds = "seconds ago";
2301 char *now = "right now";
2309 diff_time = time(NULL) - committer_time;
2310 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2311 if (asprintf(repo_age, "%lld %s",
2312 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2313 return got_error_from_errno("asprintf");
2314 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2315 if (asprintf(repo_age, "%lld %s",
2316 (diff_time / 60 / 60 / 24 / (365 / 12)),
2318 return got_error_from_errno("asprintf");
2319 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2320 if (asprintf(repo_age, "%lld %s",
2321 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2322 return got_error_from_errno("asprintf");
2323 } else if (diff_time > 60 * 60 * 24 * 2) {
2324 if (asprintf(repo_age, "%lld %s",
2325 (diff_time / 60 / 60 / 24), days) == -1)
2326 return got_error_from_errno("asprintf");
2327 } else if (diff_time > 60 * 60 * 2) {
2328 if (asprintf(repo_age, "%lld %s",
2329 (diff_time / 60 / 60), hours) == -1)
2330 return got_error_from_errno("asprintf");
2331 } else if (diff_time > 60 * 2) {
2332 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2334 return got_error_from_errno("asprintf");
2335 } else if (diff_time > 2) {
2336 if (asprintf(repo_age, "%lld %s", diff_time,
2338 return got_error_from_errno("asprintf");
2340 if (asprintf(repo_age, "%s", now) == -1)
2341 return got_error_from_errno("asprintf");
2345 if (gmtime_r(&committer_time, &tm) == NULL)
2346 return got_error_from_errno("gmtime_r");
2348 s = asctime_r(&tm, datebuf);
2350 return got_error_from_errno("asctime_r");
2352 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2353 return got_error_from_errno("asprintf");
2359 static const struct got_error *
2360 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2361 char *repo_ref, int ref_tm)
2363 const struct got_error *error = NULL;
2364 struct got_object_id *id = NULL;
2365 struct got_repository *repo = NULL;
2366 struct got_commit_object *commit = NULL;
2367 struct got_reflist_head refs;
2368 struct got_reflist_entry *re;
2369 struct got_reference *head_ref;
2371 time_t committer_time = 0, cmp_time = 0;
2375 SIMPLEQ_INIT(&refs);
2377 if (repo_ref == NULL)
2380 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2383 if (gw_trans->gw_conf->got_show_repo_age == 0)
2386 error = got_repo_open(&repo, dir, NULL);
2391 error = got_ref_list(&refs, repo, "refs/heads",
2392 got_ref_cmp_by_name, NULL);
2394 error = got_ref_list(&refs, repo, repo_ref,
2395 got_ref_cmp_by_name, NULL);
2399 SIMPLEQ_FOREACH(re, &refs, entry) {
2401 error = gw_strdup_string(&refname, repo_ref, NULL);
2405 error = gw_strdup_string(&refname, NULL,
2406 got_ref_get_name(re->ref));
2410 error = got_ref_open(&head_ref, repo, refname, 0);
2414 error = got_ref_resolve(&id, repo, head_ref);
2415 got_ref_close(head_ref);
2419 error = got_object_open_as_commit(&commit, repo, id);
2424 got_object_commit_get_committer_time(commit);
2426 if (cmp_time < committer_time)
2427 cmp_time = committer_time;
2430 if (cmp_time != 0) {
2431 committer_time = cmp_time;
2432 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2435 got_ref_list_free(&refs);
2440 static const struct got_error *
2441 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2443 const struct got_error *error;
2445 struct got_object_id *id1 = NULL, *id2 = NULL;
2446 char *label1 = NULL, *label2 = NULL, *line = NULL;
2448 size_t linesize = 0;
2450 enum kcgi_err kerr = KCGI_OK;
2456 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2460 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2461 error = got_repo_match_object_id(&id1, &label1,
2462 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2467 error = got_repo_match_object_id(&id2, &label2,
2468 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2472 error = got_object_get_type(&obj_type, header->repo, id2);
2476 case GOT_OBJ_TYPE_BLOB:
2477 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2480 case GOT_OBJ_TYPE_TREE:
2481 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2484 case GOT_OBJ_TYPE_COMMIT:
2485 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2489 error = got_error(GOT_ERR_OBJ_TYPE);
2494 if (fseek(f, 0, SEEK_SET) == -1) {
2495 error = got_ferror(f, GOT_ERR_IO);
2499 while ((linelen = getline(&line, &linesize, f)) != -1) {
2500 error = gw_colordiff_line(gw_trans, line);
2503 /* XXX: KHTML_PRETTY breaks this */
2504 kerr = khtml_puts(gw_trans->gw_html_req, line);
2505 if (kerr != KCGI_OK)
2507 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2508 if (kerr != KCGI_OK)
2511 if (linelen == -1 && ferror(f))
2512 error = got_error_from_errno("getline");
2514 if (f && fclose(f) == -1 && error == NULL)
2515 error = got_error_from_errno("fclose");
2522 if (error == NULL && kerr != KCGI_OK)
2523 error = gw_kcgi_error(kerr);
2527 static const struct got_error *
2528 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2530 const struct got_error *error = NULL;
2531 struct got_repository *repo;
2532 const char *gitconfig_owner;
2536 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2539 error = got_repo_open(&repo, dir, NULL);
2542 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2543 if (gitconfig_owner)
2544 error = gw_strdup_string(owner, NULL, gitconfig_owner);
2545 got_repo_close(repo);
2549 static const struct got_error *
2550 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2552 const struct got_error *error = NULL;
2554 char *d_file = NULL;
2560 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2561 return got_error_from_errno("asprintf");
2563 f = fopen(d_file, "r");
2565 if (errno != ENOENT && errno != EACCES)
2566 error = got_error_from_errno2("fopen", d_file);
2570 if (fseek(f, 0, SEEK_END) == -1) {
2571 error = got_ferror(f, GOT_ERR_IO);
2576 error = got_ferror(f, GOT_ERR_IO);
2579 if (fseek(f, 0, SEEK_SET) == -1) {
2580 error = got_ferror(f, GOT_ERR_IO);
2584 *url = calloc(len + 1, sizeof(**url));
2586 error = got_error_from_errno("calloc");
2590 n = fread(*url, 1, len, f);
2591 if (n == 0 && ferror(f))
2592 error = got_ferror(f, GOT_ERR_IO);
2594 if (f && fclose(f) == -1 && error == NULL)
2595 error = got_error_from_errno("fclose");
2600 static const struct got_error *
2601 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2602 int limit, int tag_type)
2604 const struct got_error *error = NULL;
2605 struct got_repository *repo = NULL;
2606 struct got_reflist_head refs;
2607 struct got_reflist_entry *re;
2609 char *id_str = NULL, *newline, *href_commits = NULL;
2610 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2611 struct got_tag_object *tag = NULL;
2612 enum kcgi_err kerr = KCGI_OK;
2613 int summary_header_displayed = 0;
2615 SIMPLEQ_INIT(&refs);
2617 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2621 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2625 SIMPLEQ_FOREACH(re, &refs, entry) {
2626 const char *refname;
2628 const char *tag_commit;
2630 struct got_object_id *id;
2631 struct got_commit_object *commit = NULL;
2633 refname = got_ref_get_name(re->ref);
2634 if (strncmp(refname, "refs/tags/", 10) != 0)
2638 error = got_ref_resolve(&id, repo, re->ref);
2642 error = got_object_open_as_tag(&tag, repo, id);
2644 if (error->code != GOT_ERR_OBJ_TYPE) {
2648 /* "lightweight" tag */
2649 error = got_object_open_as_commit(&commit, repo, id);
2654 tagger = got_object_commit_get_committer(commit);
2656 got_object_commit_get_committer_time(commit);
2657 error = got_object_id_str(&id_str, id);
2661 tagger = got_object_tag_get_tagger(tag);
2662 tagger_time = got_object_tag_get_tagger_time(tag);
2663 error = got_object_id_str(&id_str,
2664 got_object_tag_get_object_id(tag));
2669 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2670 strlen(id_str)) != 0)
2674 error = got_object_commit_get_logmsg(&tag_commit0,
2678 got_object_commit_close(commit);
2680 tag_commit0 = strdup(got_object_tag_get_message(tag));
2681 if (tag_commit0 == NULL) {
2682 error = got_error_from_errno("strdup");
2687 tag_commit = tag_commit0;
2688 while (*tag_commit == '\n')
2693 newline = strchr(tag_commit, '\n');
2697 if (summary_header_displayed == 0) {
2698 kerr = khtml_attr(gw_trans->gw_html_req,
2699 KELEM_DIV, KATTR_ID,
2700 "summary_tags_title_wrapper", KATTR__MAX);
2701 if (kerr != KCGI_OK)
2703 kerr = khtml_attr(gw_trans->gw_html_req,
2704 KELEM_DIV, KATTR_ID,
2705 "summary_tags_title", KATTR__MAX);
2706 if (kerr != KCGI_OK)
2708 kerr = khtml_puts(gw_trans->gw_html_req,
2710 if (kerr != KCGI_OK)
2712 kerr = khtml_closeelem(gw_trans->gw_html_req,
2714 if (kerr != KCGI_OK)
2716 kerr = khtml_attr(gw_trans->gw_html_req,
2717 KELEM_DIV, KATTR_ID,
2718 "summary_tags_content", KATTR__MAX);
2719 if (kerr != KCGI_OK)
2721 summary_header_displayed = 1;
2724 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2725 KATTR_ID, "tags_wrapper", KATTR__MAX);
2726 if (kerr != KCGI_OK)
2728 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2729 KATTR_ID, "tags_age", KATTR__MAX);
2730 if (kerr != KCGI_OK)
2732 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2735 kerr = khtml_puts(gw_trans->gw_html_req,
2737 if (kerr != KCGI_OK)
2739 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2740 if (kerr != KCGI_OK)
2742 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2743 KATTR_ID, "tags", KATTR__MAX);
2744 if (kerr != KCGI_OK)
2746 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2747 if (kerr != KCGI_OK)
2749 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2750 if (kerr != KCGI_OK)
2752 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2753 KATTR_ID, "tags_name", KATTR__MAX);
2754 if (kerr != KCGI_OK)
2756 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2757 gw_trans->repo_name, id_str) == -1) {
2758 error = got_error_from_errno("asprintf");
2761 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2762 KATTR_HREF, href_tag, KATTR__MAX);
2763 if (kerr != KCGI_OK)
2765 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2766 if (kerr != KCGI_OK)
2768 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2769 if (kerr != KCGI_OK)
2772 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2773 KATTR_ID, "navs_wrapper", KATTR__MAX);
2774 if (kerr != KCGI_OK)
2776 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2777 KATTR_ID, "navs", KATTR__MAX);
2778 if (kerr != KCGI_OK)
2781 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2782 KATTR_HREF, href_tag, KATTR__MAX);
2783 if (kerr != KCGI_OK)
2785 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2786 if (kerr != KCGI_OK)
2788 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2789 if (kerr != KCGI_OK)
2792 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2793 if (kerr != KCGI_OK)
2795 if (asprintf(&href_briefs,
2796 "?path=%s&action=briefs&commit=%s",
2797 gw_trans->repo_name, id_str) == -1) {
2798 error = got_error_from_errno("asprintf");
2801 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2802 KATTR_HREF, href_briefs, KATTR__MAX);
2803 if (kerr != KCGI_OK)
2805 kerr = khtml_puts(gw_trans->gw_html_req,
2807 if (kerr != KCGI_OK)
2809 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2810 if (kerr != KCGI_OK)
2813 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2814 if (kerr != KCGI_OK)
2817 if (asprintf(&href_commits,
2818 "?path=%s&action=commits&commit=%s",
2819 gw_trans->repo_name, id_str) == -1) {
2820 error = got_error_from_errno("asprintf");
2823 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2824 KATTR_HREF, href_commits, KATTR__MAX);
2825 if (kerr != KCGI_OK)
2827 kerr = khtml_puts(gw_trans->gw_html_req,
2829 if (kerr != KCGI_OK)
2831 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2832 if (kerr != KCGI_OK)
2835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2836 KATTR_ID, "dotted_line", KATTR__MAX);
2837 if (kerr != KCGI_OK)
2839 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2840 if (kerr != KCGI_OK)
2844 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2845 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2846 if (kerr != KCGI_OK)
2848 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2849 if (kerr != KCGI_OK)
2851 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2852 if (kerr != KCGI_OK)
2854 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2855 KATTR_ID, "tag_info_date", KATTR__MAX);
2856 if (kerr != KCGI_OK)
2858 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2861 kerr = khtml_puts(gw_trans->gw_html_req,
2863 if (kerr != KCGI_OK)
2865 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2866 if (kerr != KCGI_OK)
2869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2870 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2871 if (kerr != KCGI_OK)
2873 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2874 if (kerr != KCGI_OK)
2876 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2877 if (kerr != KCGI_OK)
2879 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2880 KATTR_ID, "tag_info_date", KATTR__MAX);
2881 if (kerr != KCGI_OK)
2883 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2884 if (kerr != KCGI_OK)
2886 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2887 if (kerr != KCGI_OK)
2890 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2891 KATTR_ID, "tag_info", KATTR__MAX);
2892 if (kerr != KCGI_OK)
2894 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2895 if (kerr != KCGI_OK)
2901 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2902 if (kerr != KCGI_OK)
2905 if (limit && --limit == 0)
2909 got_object_tag_close(tag);
2922 href_commits = NULL;
2926 got_object_tag_close(tag);
2933 got_ref_list_free(&refs);
2935 got_repo_close(repo);
2936 if (error == NULL && kerr != KCGI_OK)
2937 error = gw_kcgi_error(kerr);
2942 gw_free_headers(struct gw_header *header)
2946 if (header->commit != NULL)
2947 got_object_commit_close(header->commit);
2949 got_repo_close(header->repo);
2950 free(header->refs_str);
2951 free(header->commit_id);
2952 free(header->author);
2953 free(header->committer);
2954 free(header->parent_id);
2955 free(header->tree_id);
2956 free(header->commit_msg);
2959 static struct gw_header *
2962 struct gw_header *header;
2964 header = malloc(sizeof(*header));
2968 header->repo = NULL;
2969 header->commit = NULL;
2971 header->path = NULL;
2972 SIMPLEQ_INIT(&header->refs);
2977 static const struct got_error *
2978 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2981 const struct got_error *error = NULL;
2982 struct got_commit_graph *graph = NULL;
2984 error = got_commit_graph_open(&graph, header->path, 0);
2988 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2994 error = got_commit_graph_iter_next(&header->id, graph,
2995 header->repo, NULL, NULL);
2997 if (error->code == GOT_ERR_ITER_COMPLETED)
3001 if (header->id == NULL)
3004 error = got_object_open_as_commit(&header->commit, header->repo,
3009 error = gw_get_commit(gw_trans, header);
3011 struct gw_header *n_header = NULL;
3012 if ((n_header = gw_init_header()) == NULL) {
3013 error = got_error_from_errno("malloc");
3017 error = gw_strdup_string(&n_header->refs_str,
3018 header->refs_str, NULL);
3021 error = gw_strdup_string(&n_header->commit_id,
3022 header->commit_id, NULL);
3025 error = gw_strdup_string(&n_header->parent_id,
3026 header->parent_id, NULL);
3029 error = gw_strdup_string(&n_header->tree_id,
3030 header->tree_id, NULL);
3033 error = gw_strdup_string(&n_header->author,
3034 header->author, NULL);
3037 error = gw_strdup_string(&n_header->committer,
3038 header->committer, NULL);
3041 error = gw_strdup_string(&n_header->commit_msg,
3042 header->commit_msg, NULL);
3045 n_header->committer_time = header->committer_time;
3046 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3049 if (error || (limit && --limit == 0))
3054 got_commit_graph_close(graph);
3058 static const struct got_error *
3059 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3061 const struct got_error *error = NULL;
3062 struct got_reflist_entry *re;
3063 struct got_object_id *id2 = NULL;
3064 struct got_object_qid *parent_id;
3065 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
3068 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3071 struct got_tag_object *tag = NULL;
3074 name = got_ref_get_name(re->ref);
3075 if (strcmp(name, GOT_REF_HEAD) == 0)
3077 if (strncmp(name, "refs/", 5) == 0)
3079 if (strncmp(name, "got/", 4) == 0)
3081 if (strncmp(name, "heads/", 6) == 0)
3083 if (strncmp(name, "remotes/", 8) == 0)
3085 if (strncmp(name, "tags/", 5) == 0) {
3086 error = got_object_open_as_tag(&tag, header->repo,
3089 if (error->code != GOT_ERR_OBJ_TYPE)
3092 * Ref points at something other
3099 cmp = got_object_id_cmp(tag ?
3100 got_object_tag_get_object_id(tag) : re->id, header->id);
3102 got_object_tag_close(tag);
3106 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
3107 s ? ", " : "", name) == -1) {
3108 error = got_error_from_errno("asprintf");
3112 error = gw_strdup_string(&header->refs_str, refs_str, NULL);
3118 if (refs_str == NULL) {
3119 error = gw_strdup_string(&header->refs_str, "", NULL);
3125 error = got_object_id_str(&header->commit_id, header->id);
3129 error = got_object_id_str(&header->tree_id,
3130 got_object_commit_get_tree_id(header->commit));
3134 if (gw_trans->action == GW_DIFF) {
3135 parent_id = SIMPLEQ_FIRST(
3136 got_object_commit_get_parent_ids(header->commit));
3137 if (parent_id != NULL) {
3138 id2 = got_object_id_dup(parent_id->id);
3140 error = got_object_id_str(&header->parent_id, id2);
3145 error = gw_strdup_string(&header->parent_id,
3151 error = gw_strdup_string(&header->parent_id, "", NULL);
3156 header->committer_time =
3157 got_object_commit_get_committer_time(header->commit);
3159 error = gw_strdup_string(&header->author, NULL,
3160 got_object_commit_get_author(header->commit));
3163 error = gw_strdup_string(&header->committer, NULL,
3164 got_object_commit_get_committer(header->commit));
3167 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3171 commit_msg = commit_msg0;
3172 while (*commit_msg == '\n')
3175 error = gw_strdup_string(&header->commit_msg, commit_msg, NULL);
3180 static const struct got_error *
3181 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3183 const struct got_error *error = NULL;
3184 char *in_repo_path = NULL;
3186 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3190 if (gw_trans->commit == NULL) {
3191 struct got_reference *head_ref;
3192 error = got_ref_open(&head_ref, header->repo,
3193 gw_trans->headref, 0);
3197 error = got_ref_resolve(&header->id, header->repo, head_ref);
3198 got_ref_close(head_ref);
3202 error = got_object_open_as_commit(&header->commit,
3203 header->repo, header->id);
3205 struct got_reference *ref;
3206 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3207 if (error == NULL) {
3209 error = got_ref_resolve(&header->id, header->repo, ref);
3213 error = got_object_get_type(&obj_type, header->repo,
3217 if (obj_type == GOT_OBJ_TYPE_TAG) {
3218 struct got_tag_object *tag;
3219 error = got_object_open_as_tag(&tag,
3220 header->repo, header->id);
3223 if (got_object_tag_get_object_type(tag) !=
3224 GOT_OBJ_TYPE_COMMIT) {
3225 got_object_tag_close(tag);
3226 error = got_error(GOT_ERR_OBJ_TYPE);
3230 header->id = got_object_id_dup(
3231 got_object_tag_get_object_id(tag));
3232 if (header->id == NULL)
3233 error = got_error_from_errno(
3234 "got_object_id_dup");
3235 got_object_tag_close(tag);
3238 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3239 error = got_error(GOT_ERR_OBJ_TYPE);
3242 error = got_object_open_as_commit(&header->commit,
3243 header->repo, header->id);
3247 if (header->commit == NULL) {
3248 error = got_repo_match_object_id_prefix(&header->id,
3249 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3254 error = got_repo_match_object_id_prefix(&header->id,
3255 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3259 error = got_repo_map_path(&in_repo_path, header->repo,
3260 gw_trans->repo_path, 1);
3265 error = gw_strdup_string(&header->path, in_repo_path, NULL);
3273 error = got_ref_list(&header->refs, header->repo, NULL,
3274 got_ref_cmp_by_name, NULL);
3278 error = gw_get_commits(gw_trans, header, limit);
3286 char datebuf[11]; /* YYYY-MM-DD + NUL */
3289 struct gw_blame_cb_args {
3290 struct blame_line *lines;
3294 off_t *line_offsets;
3296 struct got_repository *repo;
3297 struct gw_trans *gw_trans;
3300 static const struct got_error *
3301 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3303 const struct got_error *err = NULL;
3304 struct gw_blame_cb_args *a = arg;
3305 struct blame_line *bline;
3307 size_t linesize = 0;
3308 struct got_commit_object *commit = NULL;
3311 time_t committer_time;
3312 enum kcgi_err kerr = KCGI_OK;
3314 if (nlines != a->nlines ||
3315 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3316 return got_error(GOT_ERR_RANGE);
3319 return NULL; /* no change in this commit */
3321 /* Annotate this line. */
3322 bline = &a->lines[lineno - 1];
3323 if (bline->annotated)
3325 err = got_object_id_str(&bline->id_str, id);
3329 err = got_object_open_as_commit(&commit, a->repo, id);
3333 err = gw_strdup_string(&bline->committer, NULL,
3334 got_object_commit_get_committer(commit));
3338 committer_time = got_object_commit_get_committer_time(commit);
3339 if (localtime_r(&committer_time, &tm) == NULL)
3340 return got_error_from_errno("localtime_r");
3341 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3342 &tm) >= sizeof(bline->datebuf)) {
3343 err = got_error(GOT_ERR_NO_SPACE);
3346 bline->annotated = 1;
3348 /* Print lines annotated so far. */
3349 bline = &a->lines[a->lineno_cur - 1];
3350 if (!bline->annotated)
3353 offset = a->line_offsets[a->lineno_cur - 1];
3354 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3355 err = got_error_from_errno("fseeko");
3359 while (bline->annotated) {
3360 char *smallerthan, *at, *nl, *committer;
3361 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3364 if (getline(&line, &linesize, a->f) == -1) {
3366 err = got_error_from_errno("getline");
3370 committer = bline->committer;
3371 smallerthan = strchr(committer, '<');
3372 if (smallerthan && smallerthan[1] != '\0')
3373 committer = smallerthan + 1;
3374 at = strchr(committer, '@');
3377 len = strlen(committer);
3379 committer[8] = '\0';
3381 nl = strchr(line, '\n');
3385 if (a->gw_trans->repo_folder == NULL) {
3386 err = gw_strdup_string(&a->gw_trans->repo_folder, "",
3391 if (a->gw_trans->repo_folder == NULL)
3394 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3395 "blame_wrapper", KATTR__MAX);
3396 if (kerr != KCGI_OK)
3398 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3399 "blame_number", KATTR__MAX);
3400 if (kerr != KCGI_OK)
3402 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3403 a->lineno_cur) == -1)
3405 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3406 if (kerr != KCGI_OK)
3408 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3409 if (kerr != KCGI_OK)
3412 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3413 "blame_hash", KATTR__MAX);
3414 if (kerr != KCGI_OK)
3416 if (asprintf(&href_diff,
3417 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3418 a->gw_trans->repo_name, bline->id_str,
3419 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3420 err = got_error_from_errno("asprintf");
3423 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3424 err = got_error_from_errno("asprintf");
3427 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3428 KATTR_HREF, href_diff, KATTR__MAX);
3429 if (kerr != KCGI_OK)
3431 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3432 if (kerr != KCGI_OK)
3434 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3435 if (kerr != KCGI_OK)
3438 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3439 "blame_date", KATTR__MAX);
3440 if (kerr != KCGI_OK)
3442 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3443 if (kerr != KCGI_OK)
3445 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3446 if (kerr != KCGI_OK)
3449 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3450 "blame_author", KATTR__MAX);
3451 if (kerr != KCGI_OK)
3453 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3454 if (kerr != KCGI_OK)
3456 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3457 if (kerr != KCGI_OK)
3460 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3461 "blame_code", KATTR__MAX);
3462 if (kerr != KCGI_OK)
3464 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3465 if (kerr != KCGI_OK)
3467 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3468 if (kerr != KCGI_OK)
3471 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3472 if (kerr != KCGI_OK)
3476 bline = &a->lines[a->lineno_cur - 1];
3484 got_object_commit_close(commit);
3486 if (err == NULL && kerr != KCGI_OK)
3487 err = gw_kcgi_error(kerr);
3491 static const struct got_error *
3492 gw_output_file_blame(struct gw_trans *gw_trans)
3494 const struct got_error *error = NULL;
3495 struct got_repository *repo = NULL;
3496 struct got_object_id *obj_id = NULL;
3497 struct got_object_id *commit_id = NULL;
3498 struct got_blob_object *blob = NULL;
3499 char *path = NULL, *in_repo_path = NULL;
3500 struct gw_blame_cb_args bca;
3504 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3508 if (asprintf(&path, "%s%s%s",
3509 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3510 gw_trans->repo_folder ? "/" : "",
3511 gw_trans->repo_file) == -1) {
3512 error = got_error_from_errno("asprintf");
3516 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3520 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3521 GOT_OBJ_TYPE_COMMIT, 1, repo);
3525 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3529 if (obj_id == NULL) {
3530 error = got_error(GOT_ERR_NO_OBJ);
3534 error = got_object_get_type(&obj_type, repo, obj_id);
3538 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3539 error = got_error(GOT_ERR_OBJ_TYPE);
3543 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3547 bca.f = got_opentemp();
3548 if (bca.f == NULL) {
3549 error = got_error_from_errno("got_opentemp");
3552 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3553 &bca.line_offsets, bca.f, blob);
3554 if (error || bca.nlines == 0)
3557 /* Don't include \n at EOF in the blame line count. */
3558 if (bca.line_offsets[bca.nlines - 1] == filesize)
3561 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3562 if (bca.lines == NULL) {
3563 error = got_error_from_errno("calloc");
3567 bca.nlines_prec = 0;
3574 bca.gw_trans = gw_trans;
3576 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3579 free(bca.line_offsets);
3585 for (i = 0; i < bca.nlines; i++) {
3586 struct blame_line *bline = &bca.lines[i];
3587 free(bline->id_str);
3588 free(bline->committer);
3591 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3592 error = got_error_from_errno("fclose");
3594 got_object_blob_close(blob);
3596 got_repo_close(repo);
3600 static const struct got_error *
3601 gw_output_blob_buf(struct gw_trans *gw_trans)
3603 const struct got_error *error = NULL;
3604 struct got_repository *repo = NULL;
3605 struct got_object_id *obj_id = NULL;
3606 struct got_object_id *commit_id = NULL;
3607 struct got_blob_object *blob = NULL;
3608 char *path = NULL, *in_repo_path = NULL;
3609 int obj_type, set_mime = 0;
3612 enum kcgi_err kerr = KCGI_OK;
3614 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3618 if (asprintf(&path, "%s%s%s",
3619 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3620 gw_trans->repo_folder ? "/" : "",
3621 gw_trans->repo_file) == -1) {
3622 error = got_error_from_errno("asprintf");
3626 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3630 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3631 GOT_OBJ_TYPE_COMMIT, 1, repo);
3635 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3639 if (obj_id == NULL) {
3640 error = got_error(GOT_ERR_NO_OBJ);
3644 error = got_object_get_type(&obj_type, repo, obj_id);
3648 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3649 error = got_error(GOT_ERR_OBJ_TYPE);
3653 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3657 hdrlen = got_object_blob_get_hdrlen(blob);
3659 error = got_object_blob_read_block(&len, blob);
3662 buf = got_object_blob_get_read_buf(blob);
3665 * Skip blob object header first time around,
3666 * which also contains a zero byte.
3669 if (set_mime == 0) {
3670 if (isbinary(buf, len - hdrlen))
3671 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3673 gw_trans->mime = KMIME_TEXT_PLAIN;
3675 error = gw_display_index(gw_trans);
3679 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3688 got_object_blob_close(blob);
3690 got_repo_close(repo);
3691 if (error == NULL && kerr != KCGI_OK)
3692 error = gw_kcgi_error(kerr);
3696 static const struct got_error *
3697 gw_output_repo_tree(struct gw_trans *gw_trans)
3699 const struct got_error *error = NULL;
3700 struct got_repository *repo = NULL;
3701 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3702 struct got_tree_object *tree = NULL;
3703 char *path = NULL, *in_repo_path = NULL;
3704 char *id_str = NULL;
3705 char *build_folder = NULL;
3706 char *href_blob = NULL, *href_blame = NULL;
3707 const char *class = NULL;
3708 int nentries, i, class_flip = 0;
3709 enum kcgi_err kerr = KCGI_OK;
3711 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3715 if (gw_trans->repo_folder != NULL) {
3716 error = gw_strdup_string(&path, gw_trans->repo_folder, NULL);
3720 error = got_repo_map_path(&in_repo_path, repo,
3721 gw_trans->repo_path, 1);
3725 path = in_repo_path;
3728 if (gw_trans->commit == NULL) {
3729 struct got_reference *head_ref;
3730 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3733 error = got_ref_resolve(&commit_id, repo, head_ref);
3736 got_ref_close(head_ref);
3739 error = got_repo_match_object_id(&commit_id, NULL,
3740 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3746 * XXX gw_trans->commit might have already been allocated in
3747 * gw_parse_querystring(); could we more cleanly seperate values
3748 * we received as arguments from values we compute ourselves?
3750 error = got_object_id_str(&gw_trans->commit, commit_id);
3754 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3758 error = got_object_open_as_tree(&tree, repo, tree_id);
3762 nentries = got_object_tree_get_nentries(tree);
3763 for (i = 0; i < nentries; i++) {
3764 struct got_tree_entry *te;
3765 const char *modestr = "";
3768 te = got_object_tree_get_entry(tree, i);
3770 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3774 mode = got_tree_entry_get_mode(te);
3775 if (got_object_tree_entry_is_submodule(te))
3777 else if (S_ISLNK(mode))
3779 else if (S_ISDIR(mode))
3781 else if (mode & S_IXUSR)
3784 if (class_flip == 0) {
3785 class = "back_lightgray";
3788 class = "back_white";
3792 if (S_ISDIR(mode)) {
3793 if (asprintf(&build_folder, "%s/%s",
3794 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3795 got_tree_entry_get_name(te)) == -1) {
3796 error = got_error_from_errno(
3800 if (asprintf(&href_blob,
3801 "?path=%s&action=%s&commit=%s&folder=%s",
3802 gw_trans->repo_name, gw_get_action_name(gw_trans),
3803 gw_trans->commit, build_folder) == -1) {
3804 error = got_error_from_errno("asprintf");
3808 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3809 KATTR_ID, "tree_wrapper", KATTR__MAX);
3810 if (kerr != KCGI_OK)
3812 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3813 KATTR_ID, "tree_line", KATTR_CLASS, class,
3815 if (kerr != KCGI_OK)
3817 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3818 KATTR_HREF, href_blob, KATTR_CLASS,
3819 "diff_directory", KATTR__MAX);
3820 if (kerr != KCGI_OK)
3822 kerr = khtml_puts(gw_trans->gw_html_req,
3823 got_tree_entry_get_name(te));
3824 if (kerr != KCGI_OK)
3826 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3827 if (kerr != KCGI_OK)
3829 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3830 if (kerr != KCGI_OK)
3832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3833 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3835 if (kerr != KCGI_OK)
3837 kerr = khtml_entity(gw_trans->gw_html_req,
3839 if (kerr != KCGI_OK)
3841 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3842 if (kerr != KCGI_OK)
3845 if (asprintf(&href_blob,
3846 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3847 gw_trans->repo_name, "blob", gw_trans->commit,
3848 got_tree_entry_get_name(te),
3849 gw_trans->repo_folder ?
3850 gw_trans->repo_folder : "") == -1) {
3851 error = got_error_from_errno("asprintf");
3854 if (asprintf(&href_blame,
3855 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3856 gw_trans->repo_name, "blame", gw_trans->commit,
3857 got_tree_entry_get_name(te),
3858 gw_trans->repo_folder ?
3859 gw_trans->repo_folder : "") == -1) {
3860 error = got_error_from_errno("asprintf");
3864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3865 KATTR_ID, "tree_wrapper", KATTR__MAX);
3866 if (kerr != KCGI_OK)
3868 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3869 KATTR_ID, "tree_line", KATTR_CLASS, class,
3871 if (kerr != KCGI_OK)
3873 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3874 KATTR_HREF, href_blob, KATTR__MAX);
3875 if (kerr != KCGI_OK)
3877 kerr = khtml_puts(gw_trans->gw_html_req,
3878 got_tree_entry_get_name(te));
3879 if (kerr != KCGI_OK)
3881 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3882 if (kerr != KCGI_OK)
3884 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3885 if (kerr != KCGI_OK)
3887 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3888 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3890 if (kerr != KCGI_OK)
3893 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3894 KATTR_HREF, href_blob, KATTR__MAX);
3895 if (kerr != KCGI_OK)
3897 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3898 if (kerr != KCGI_OK)
3900 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3901 if (kerr != KCGI_OK)
3904 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3905 if (kerr != KCGI_OK)
3908 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3909 KATTR_HREF, href_blame, KATTR__MAX);
3910 if (kerr != KCGI_OK)
3912 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3913 if (kerr != KCGI_OK)
3916 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3917 if (kerr != KCGI_OK)
3925 build_folder = NULL;
3929 got_object_tree_close(tree);
3931 got_repo_close(repo);
3938 if (error == NULL && kerr != KCGI_OK)
3939 error = gw_kcgi_error(kerr);
3943 static const struct got_error *
3944 gw_output_repo_heads(struct gw_trans *gw_trans)
3946 const struct got_error *error = NULL;
3947 struct got_repository *repo = NULL;
3948 struct got_reflist_head refs;
3949 struct got_reflist_entry *re;
3950 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3951 char *href_commits = NULL;
3952 enum kcgi_err kerr = KCGI_OK;
3954 SIMPLEQ_INIT(&refs);
3956 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3960 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3965 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3966 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3967 if (kerr != KCGI_OK)
3969 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3970 KATTR_ID, "summary_heads_title", KATTR__MAX);
3971 if (kerr != KCGI_OK)
3973 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3974 if (kerr != KCGI_OK)
3976 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3977 if (kerr != KCGI_OK)
3979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3980 KATTR_ID, "summary_heads_content", KATTR__MAX);
3981 if (kerr != KCGI_OK)
3984 SIMPLEQ_FOREACH(re, &refs, entry) {
3987 error = gw_strdup_string(&refname, NULL,
3988 got_ref_get_name(re->ref));
3992 if (strncmp(refname, "refs/heads/", 11) != 0) {
3997 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4002 if (strncmp(refname, "refs/heads/", 11) == 0)
4005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4006 KATTR_ID, "heads_wrapper", KATTR__MAX);
4007 if (kerr != KCGI_OK)
4009 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4010 KATTR_ID, "heads_age", KATTR__MAX);
4011 if (kerr != KCGI_OK)
4013 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4014 if (kerr != KCGI_OK)
4016 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4017 if (kerr != KCGI_OK)
4019 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4020 KATTR_ID, "heads_space", KATTR__MAX);
4021 if (kerr != KCGI_OK)
4023 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4024 if (kerr != KCGI_OK)
4026 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4027 if (kerr != KCGI_OK)
4029 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4030 KATTR_ID, "head", KATTR__MAX);
4031 if (kerr != KCGI_OK)
4033 if (asprintf(&href_summary,
4034 "?path=%s&action=summary&headref=%s",
4035 gw_trans->repo_name, refname) == -1) {
4036 error = got_error_from_errno("asprintf");
4039 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4040 href_summary, KATTR__MAX);
4041 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4042 if (kerr != KCGI_OK)
4044 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4045 if (kerr != KCGI_OK)
4048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4049 "navs_wrapper", KATTR__MAX);
4050 if (kerr != KCGI_OK)
4052 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4053 "navs", KATTR__MAX);
4054 if (kerr != KCGI_OK)
4057 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4058 href_summary, KATTR__MAX);
4059 if (kerr != KCGI_OK)
4061 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4062 if (kerr != KCGI_OK)
4064 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4065 if (kerr != KCGI_OK)
4068 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4069 if (kerr != KCGI_OK)
4071 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4072 gw_trans->repo_name, refname) == -1) {
4073 error = got_error_from_errno("asprintf");
4076 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4077 href_briefs, KATTR__MAX);
4078 if (kerr != KCGI_OK)
4080 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4081 if (kerr != KCGI_OK)
4083 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4084 if (kerr != KCGI_OK)
4087 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4088 if (kerr != KCGI_OK)
4091 if (asprintf(&href_commits,
4092 "?path=%s&action=commits&headref=%s",
4093 gw_trans->repo_name, refname) == -1) {
4094 error = got_error_from_errno("asprintf");
4097 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4098 href_commits, KATTR__MAX);
4099 if (kerr != KCGI_OK)
4101 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4102 if (kerr != KCGI_OK)
4104 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4105 if (kerr != KCGI_OK)
4108 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4109 "dotted_line", KATTR__MAX);
4110 if (kerr != KCGI_OK)
4112 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4113 if (kerr != KCGI_OK)
4116 href_summary = NULL;
4120 href_commits = NULL;
4123 got_ref_list_free(&refs);
4128 got_repo_close(repo);
4132 static const struct got_error *
4133 gw_output_site_link(struct gw_trans *gw_trans)
4135 const struct got_error *error = NULL;
4136 char *href_summary = NULL;
4137 enum kcgi_err kerr = KCGI_OK;
4139 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4140 "site_link", KATTR__MAX);
4141 if (kerr != KCGI_OK)
4143 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4145 if (kerr != KCGI_OK)
4147 kerr = khtml_puts(gw_trans->gw_html_req,
4148 gw_trans->gw_conf->got_site_link);
4149 if (kerr != KCGI_OK)
4151 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4152 if (kerr != KCGI_OK)
4155 if (gw_trans->repo_name != NULL) {
4156 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4157 if (kerr != KCGI_OK)
4159 if (asprintf(&href_summary, "?path=%s&action=summary",
4160 gw_trans->repo_name) == -1)
4162 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4163 href_summary, KATTR__MAX);
4164 if (kerr != KCGI_OK)
4166 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4167 if (kerr != KCGI_OK)
4169 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4170 if (kerr != KCGI_OK)
4172 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4173 if (kerr != KCGI_OK)
4175 kerr = khtml_puts(gw_trans->gw_html_req,
4176 gw_get_action_name(gw_trans));
4177 if (kerr != KCGI_OK)
4181 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4182 if (kerr != KCGI_OK)
4189 static const struct got_error *
4190 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4192 const struct got_error *error = NULL;
4194 enum kcgi_err kerr = KCGI_OK;
4196 if (strncmp(buf, "-", 1) == 0)
4197 color = "diff_minus";
4198 else if (strncmp(buf, "+", 1) == 0)
4199 color = "diff_plus";
4200 else if (strncmp(buf, "@@", 2) == 0)
4201 color = "diff_chunk_header";
4202 else if (strncmp(buf, "@@", 2) == 0)
4203 color = "diff_chunk_header";
4204 else if (strncmp(buf, "commit +", 8) == 0)
4205 color = "diff_meta";
4206 else if (strncmp(buf, "commit -", 8) == 0)
4207 color = "diff_meta";
4208 else if (strncmp(buf, "blob +", 6) == 0)
4209 color = "diff_meta";
4210 else if (strncmp(buf, "blob -", 6) == 0)
4211 color = "diff_meta";
4212 else if (strncmp(buf, "file +", 6) == 0)
4213 color = "diff_meta";
4214 else if (strncmp(buf, "file -", 6) == 0)
4215 color = "diff_meta";
4216 else if (strncmp(buf, "from:", 5) == 0)
4217 color = "diff_author";
4218 else if (strncmp(buf, "via:", 4) == 0)
4219 color = "diff_author";
4220 else if (strncmp(buf, "date:", 5) == 0)
4221 color = "diff_date";
4222 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4223 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4224 if (error == NULL && kerr != KCGI_OK)
4225 error = gw_kcgi_error(kerr);
4230 main(int argc, char *argv[])
4232 const struct got_error *error = NULL;
4233 struct gw_trans *gw_trans;
4234 struct gw_dir *dir = NULL, *tdir;
4235 const char *page = "index";
4239 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4242 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4245 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4248 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4251 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4252 if (kerr != KCGI_OK) {
4253 error = gw_kcgi_error(kerr);
4257 if ((gw_trans->gw_conf =
4258 malloc(sizeof(struct gotweb_conf))) == NULL) {
4260 error = got_error_from_errno("malloc");
4264 TAILQ_INIT(&gw_trans->gw_dirs);
4265 TAILQ_INIT(&gw_trans->gw_headers);
4268 gw_trans->repos_total = 0;
4269 gw_trans->repo_path = NULL;
4270 gw_trans->commit = NULL;
4271 error = gw_strdup_string(&gw_trans->headref, GOT_REF_HEAD, NULL);
4274 gw_trans->mime = KMIME_TEXT_HTML;
4275 gw_trans->gw_tmpl->key = gw_templs;
4276 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4277 gw_trans->gw_tmpl->arg = gw_trans;
4278 gw_trans->gw_tmpl->cb = gw_template;
4279 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4283 error = gw_parse_querystring(gw_trans);
4287 if (gw_trans->action == GW_BLOB)
4288 error = gw_blob(gw_trans);
4290 error = gw_display_index(gw_trans);
4293 gw_trans->mime = KMIME_TEXT_PLAIN;
4294 gw_trans->action = GW_ERR;
4295 gw_display_error(gw_trans, error);
4298 free(gw_trans->gw_conf->got_repos_path);
4299 free(gw_trans->gw_conf->got_site_name);
4300 free(gw_trans->gw_conf->got_site_owner);
4301 free(gw_trans->gw_conf->got_site_link);
4302 free(gw_trans->gw_conf->got_logo);
4303 free(gw_trans->gw_conf->got_logo_url);
4304 free(gw_trans->gw_conf);
4305 free(gw_trans->commit);
4306 free(gw_trans->repo_path);
4307 free(gw_trans->repo_file);
4308 free(gw_trans->headref);
4310 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4312 free(dir->description);
4321 khttp_free(gw_trans->gw_req);