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;
66 const char *repo_file;
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 gw_trans->repo_file = p->parsed.s;
1675 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1676 if (asprintf(&gw_trans->repo_folder, "%s",
1678 return got_error_from_errno("asprintf");
1681 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF])) {
1682 if (asprintf(&gw_trans->headref, "%s",
1684 return got_error_from_errno("asprintf");
1687 if (action == NULL) {
1688 error = got_error_from_errno("invalid action");
1691 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1695 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1699 gw_trans->action = GW_INDEX;
1701 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1702 gw_trans->page = p->parsed.i;
1707 static const struct got_error *
1708 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1710 const struct got_error *error;
1712 *gw_dir = malloc(sizeof(**gw_dir));
1713 if (*gw_dir == NULL)
1714 return got_error_from_errno("malloc");
1716 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1717 error = got_error_from_errno("asprintf");
1726 static const struct got_error *
1727 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1731 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1732 if (kerr != KCGI_OK)
1733 return gw_kcgi_error(kerr);
1734 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1736 if (kerr != KCGI_OK)
1737 return gw_kcgi_error(kerr);
1738 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1740 if (kerr != KCGI_OK)
1741 return gw_kcgi_error(kerr);
1742 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1744 if (kerr != KCGI_OK)
1745 return gw_kcgi_error(kerr);
1746 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1747 if (kerr != KCGI_OK)
1748 return gw_kcgi_error(kerr);
1749 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1751 if (kerr != KCGI_OK)
1752 return gw_kcgi_error(kerr);
1754 /* XXX repo_file could be NULL if not present in querystring */
1755 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1756 kerr = khttp_head(gw_trans->gw_req,
1757 kresps[KRESP_CONTENT_DISPOSITION],
1758 "attachment; filename=%s", gw_trans->repo_file);
1759 if (kerr != KCGI_OK)
1760 return gw_kcgi_error(kerr);
1763 kerr = khttp_body(gw_trans->gw_req);
1764 return gw_kcgi_error(kerr);
1767 static const struct got_error *
1768 gw_display_index(struct gw_trans *gw_trans)
1770 const struct got_error *error;
1773 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1777 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1778 if (kerr != KCGI_OK)
1779 return gw_kcgi_error(kerr);
1781 if (gw_trans->action != GW_BLOB) {
1782 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1783 gw_query_funcs[gw_trans->action].template);
1784 if (kerr != KCGI_OK) {
1785 khtml_close(gw_trans->gw_html_req);
1786 return gw_kcgi_error(kerr);
1790 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1794 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1796 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1799 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1801 khtml_puts(gw_trans->gw_html_req, err->msg);
1802 khtml_close(gw_trans->gw_html_req);
1806 gw_template(size_t key, void *arg)
1808 const struct got_error *error = NULL;
1810 struct gw_trans *gw_trans = arg;
1811 char *img_src = NULL;
1815 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1816 KATTR_NAME, "viewport",
1817 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
1819 if (kerr != KCGI_OK)
1821 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1822 if (kerr != KCGI_OK)
1824 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1825 KATTR_CHARSET, "utf-8",
1827 if (kerr != KCGI_OK)
1829 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1830 if (kerr != KCGI_OK)
1832 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1833 KATTR_NAME, "msapplication-TileColor",
1834 KATTR_CONTENT, "#da532c", KATTR__MAX);
1835 if (kerr != KCGI_OK)
1837 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1838 if (kerr != KCGI_OK)
1840 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1841 KATTR_NAME, "theme-color",
1842 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1843 if (kerr != KCGI_OK)
1845 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1846 if (kerr != KCGI_OK)
1848 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1849 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1850 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1851 if (kerr != KCGI_OK)
1853 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1854 if (kerr != KCGI_OK)
1856 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1857 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1858 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1859 if (kerr != KCGI_OK)
1861 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1862 if (kerr != KCGI_OK)
1864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1865 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1866 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1867 if (kerr != KCGI_OK)
1869 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1870 if (kerr != KCGI_OK)
1872 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1873 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1875 if (kerr != KCGI_OK)
1877 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1878 if (kerr != KCGI_OK)
1880 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1881 KATTR_REL, "mask-icon", KATTR_HREF,
1882 "/safari-pinned-tab.svg", KATTR__MAX);
1883 if (kerr != KCGI_OK)
1885 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1886 if (kerr != KCGI_OK)
1888 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1889 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1890 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1891 if (kerr != KCGI_OK)
1893 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1894 if (kerr != KCGI_OK)
1898 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1899 KATTR_ID, "got_link", KATTR__MAX);
1900 if (kerr != KCGI_OK)
1902 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1903 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1904 KATTR_TARGET, "_sotd", KATTR__MAX);
1905 if (kerr != KCGI_OK)
1907 if (asprintf(&img_src, "/%s",
1908 gw_trans->gw_conf->got_logo) == -1)
1910 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1911 KATTR_SRC, img_src, KATTR__MAX);
1912 if (kerr != KCGI_OK) {
1916 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1917 if (kerr != KCGI_OK) {
1922 case (TEMPL_SITEPATH):
1923 error = gw_output_site_link(gw_trans);
1928 if (gw_trans->gw_conf->got_site_name != NULL) {
1929 kerr = khtml_puts(gw_trans->gw_html_req,
1930 gw_trans->gw_conf->got_site_name);
1931 if (kerr != KCGI_OK)
1935 case (TEMPL_SEARCH):
1937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1938 "search", KATTR__MAX);
1939 if (kerr != KCGI_OK)
1941 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
1942 KATTR_METHOD, "POST", KATTR__MAX);
1943 if (kerr != KCGI_OK)
1945 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
1946 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
1947 KATTR_MAXLENGTH, "50", KATTR__MAX);
1948 if (kerr != KCGI_OK)
1950 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
1952 if (kerr != KCGI_OK)
1954 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
1955 if (kerr != KCGI_OK)
1957 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
1958 if (kerr != KCGI_OK)
1961 case(TEMPL_SITEOWNER):
1962 if (gw_trans->gw_conf->got_site_owner != NULL &&
1963 gw_trans->gw_conf->got_show_site_owner) {
1964 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1965 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1966 if (kerr != KCGI_OK)
1968 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1969 KATTR_ID, "site_owner", KATTR__MAX);
1970 if (kerr != KCGI_OK)
1972 kerr = khtml_puts(gw_trans->gw_html_req,
1973 gw_trans->gw_conf->got_site_owner);
1974 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1975 if (kerr != KCGI_OK)
1979 case(TEMPL_CONTENT):
1980 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1982 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1983 KATTR_ID, "tmpl_err", KATTR__MAX);
1984 if (kerr != KCGI_OK)
1986 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
1987 if (kerr != KCGI_OK)
1989 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1990 if (kerr != KCGI_OK)
1992 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1993 if (kerr != KCGI_OK)
2003 static const struct got_error *
2004 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2006 const struct got_error *error = NULL;
2007 char *ref_str = NULL;
2008 enum kcgi_err kerr = KCGI_OK;
2010 if (strcmp(str2, "") != 0) {
2011 if (asprintf(&ref_str, "(%s)", str2) == -1) {
2012 error = got_error_from_errno("asprintf");
2016 error = gw_strdup_string(&ref_str, "", NULL);
2021 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2022 KATTR_ID, "header_commit_title", KATTR__MAX);
2023 if (kerr != KCGI_OK)
2025 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2026 if (kerr != KCGI_OK)
2028 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2029 if (kerr != KCGI_OK)
2031 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2032 KATTR_ID, "header_commit", KATTR__MAX);
2033 if (kerr != KCGI_OK)
2035 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2036 if (kerr != KCGI_OK)
2038 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2039 if (kerr != KCGI_OK)
2041 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
2042 if (kerr != KCGI_OK)
2044 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2046 if (error == NULL && kerr != KCGI_OK)
2047 error = gw_kcgi_error(kerr);
2051 static const struct got_error *
2052 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2054 const struct got_error *error = NULL;
2055 enum kcgi_err kerr = KCGI_OK;
2057 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2058 KATTR_ID, "header_diff_title", KATTR__MAX);
2059 if (kerr != KCGI_OK)
2061 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2062 if (kerr != KCGI_OK)
2064 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2065 if (kerr != KCGI_OK)
2067 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2068 KATTR_ID, "header_diff", KATTR__MAX);
2069 if (kerr != KCGI_OK)
2071 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2072 if (kerr != KCGI_OK)
2074 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2075 if (kerr != KCGI_OK)
2077 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2078 if (kerr != KCGI_OK)
2080 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2082 if (error == NULL && kerr != KCGI_OK)
2083 error = gw_kcgi_error(kerr);
2087 static const struct got_error *
2088 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2090 const struct got_error *error = NULL;
2091 enum kcgi_err kerr = KCGI_OK;
2093 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2094 KATTR_ID, "header_age_title", KATTR__MAX);
2095 if (kerr != KCGI_OK)
2097 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2098 if (kerr != KCGI_OK)
2100 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2101 if (kerr != KCGI_OK)
2103 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2104 KATTR_ID, "header_age", KATTR__MAX);
2105 if (kerr != KCGI_OK)
2107 kerr = khtml_puts(gw_trans->gw_html_req, str);
2108 if (kerr != KCGI_OK)
2110 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2112 if (error == NULL && kerr != KCGI_OK)
2113 error = gw_kcgi_error(kerr);
2117 static const struct got_error *
2118 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2120 const struct got_error *error = NULL;
2123 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2124 KATTR_ID, "header_author_title", KATTR__MAX);
2125 if (kerr != KCGI_OK)
2127 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2128 if (kerr != KCGI_OK)
2130 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2131 if (kerr != KCGI_OK)
2133 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2134 KATTR_ID, "header_author", KATTR__MAX);
2135 if (kerr != KCGI_OK)
2137 kerr = khtml_puts(gw_trans->gw_html_req, str);
2138 if (kerr != KCGI_OK)
2140 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2142 if (error == NULL && kerr != KCGI_OK)
2143 error = gw_kcgi_error(kerr);
2147 static const struct got_error *
2148 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2150 const struct got_error *error = NULL;
2153 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2154 KATTR_ID, "header_committer_title", KATTR__MAX);
2155 if (kerr != KCGI_OK)
2157 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2158 if (kerr != KCGI_OK)
2160 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2161 if (kerr != KCGI_OK)
2163 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2164 KATTR_ID, "header_committer", KATTR__MAX);
2165 if (kerr != KCGI_OK)
2167 kerr = khtml_puts(gw_trans->gw_html_req, str);
2168 if (kerr != KCGI_OK)
2170 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2172 if (error == NULL && kerr != KCGI_OK)
2173 error = gw_kcgi_error(kerr);
2177 static const struct got_error *
2178 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2180 const struct got_error *error = NULL;
2183 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2184 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2185 if (kerr != KCGI_OK)
2187 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2188 if (kerr != KCGI_OK)
2190 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2191 if (kerr != KCGI_OK)
2193 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2194 KATTR_ID, "header_commit_msg", KATTR__MAX);
2195 if (kerr != KCGI_OK)
2197 kerr = khttp_puts(gw_trans->gw_req, str);
2198 if (kerr != KCGI_OK)
2200 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2202 if (error == NULL && kerr != KCGI_OK)
2203 error = gw_kcgi_error(kerr);
2207 static const struct got_error *
2208 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2210 const struct got_error *error = NULL;
2213 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2214 KATTR_ID, "header_tree_title", KATTR__MAX);
2215 if (kerr != KCGI_OK)
2217 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2218 if (kerr != KCGI_OK)
2220 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2221 if (kerr != KCGI_OK)
2223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2224 KATTR_ID, "header_tree", KATTR__MAX);
2225 if (kerr != KCGI_OK)
2227 kerr = khtml_puts(gw_trans->gw_html_req, str);
2228 if (kerr != KCGI_OK)
2230 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2232 if (error == NULL && kerr != KCGI_OK)
2233 error = gw_kcgi_error(kerr);
2237 static const struct got_error *
2238 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2241 const struct got_error *error = NULL;
2243 char *d_file = NULL;
2247 *description = NULL;
2248 if (gw_trans->gw_conf->got_show_repo_description == 0)
2251 if (asprintf(&d_file, "%s/description", dir) == -1)
2252 return got_error_from_errno("asprintf");
2254 f = fopen(d_file, "r");
2256 if (errno == ENOENT || errno == EACCES)
2258 error = got_error_from_errno2("fopen", d_file);
2262 if (fseek(f, 0, SEEK_END) == -1) {
2263 error = got_ferror(f, GOT_ERR_IO);
2268 error = got_ferror(f, GOT_ERR_IO);
2271 if (fseek(f, 0, SEEK_SET) == -1) {
2272 error = got_ferror(f, GOT_ERR_IO);
2275 *description = calloc(len + 1, sizeof(**description));
2276 if (*description == NULL) {
2277 error = got_error_from_errno("calloc");
2281 n = fread(*description, 1, len, f);
2282 if (n == 0 && ferror(f))
2283 error = got_ferror(f, GOT_ERR_IO);
2285 if (f != NULL && fclose(f) == -1 && error == NULL)
2286 error = got_error_from_errno("fclose");
2291 static const struct got_error *
2292 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2296 char *years = "years ago", *months = "months ago";
2297 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2298 char *minutes = "minutes ago", *seconds = "seconds ago";
2299 char *now = "right now";
2307 diff_time = time(NULL) - committer_time;
2308 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2309 if (asprintf(repo_age, "%lld %s",
2310 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2311 return got_error_from_errno("asprintf");
2312 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2313 if (asprintf(repo_age, "%lld %s",
2314 (diff_time / 60 / 60 / 24 / (365 / 12)),
2316 return got_error_from_errno("asprintf");
2317 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2318 if (asprintf(repo_age, "%lld %s",
2319 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2320 return got_error_from_errno("asprintf");
2321 } else if (diff_time > 60 * 60 * 24 * 2) {
2322 if (asprintf(repo_age, "%lld %s",
2323 (diff_time / 60 / 60 / 24), days) == -1)
2324 return got_error_from_errno("asprintf");
2325 } else if (diff_time > 60 * 60 * 2) {
2326 if (asprintf(repo_age, "%lld %s",
2327 (diff_time / 60 / 60), hours) == -1)
2328 return got_error_from_errno("asprintf");
2329 } else if (diff_time > 60 * 2) {
2330 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2332 return got_error_from_errno("asprintf");
2333 } else if (diff_time > 2) {
2334 if (asprintf(repo_age, "%lld %s", diff_time,
2336 return got_error_from_errno("asprintf");
2338 if (asprintf(repo_age, "%s", now) == -1)
2339 return got_error_from_errno("asprintf");
2343 if (gmtime_r(&committer_time, &tm) == NULL)
2344 return got_error_from_errno("gmtime_r");
2346 s = asctime_r(&tm, datebuf);
2348 return got_error_from_errno("asctime_r");
2350 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2351 return got_error_from_errno("asprintf");
2357 static const struct got_error *
2358 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2359 char *repo_ref, int ref_tm)
2361 const struct got_error *error = NULL;
2362 struct got_object_id *id = NULL;
2363 struct got_repository *repo = NULL;
2364 struct got_commit_object *commit = NULL;
2365 struct got_reflist_head refs;
2366 struct got_reflist_entry *re;
2367 struct got_reference *head_ref;
2369 time_t committer_time = 0, cmp_time = 0;
2373 SIMPLEQ_INIT(&refs);
2375 if (repo_ref == NULL)
2378 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2381 if (gw_trans->gw_conf->got_show_repo_age == 0)
2384 error = got_repo_open(&repo, dir, NULL);
2389 error = got_ref_list(&refs, repo, "refs/heads",
2390 got_ref_cmp_by_name, NULL);
2392 error = got_ref_list(&refs, repo, repo_ref,
2393 got_ref_cmp_by_name, NULL);
2397 SIMPLEQ_FOREACH(re, &refs, entry) {
2399 error = gw_strdup_string(&refname, repo_ref, NULL);
2403 error = gw_strdup_string(&refname, NULL,
2404 got_ref_get_name(re->ref));
2408 error = got_ref_open(&head_ref, repo, refname, 0);
2412 error = got_ref_resolve(&id, repo, head_ref);
2413 got_ref_close(head_ref);
2417 error = got_object_open_as_commit(&commit, repo, id);
2422 got_object_commit_get_committer_time(commit);
2424 if (cmp_time < committer_time)
2425 cmp_time = committer_time;
2428 if (cmp_time != 0) {
2429 committer_time = cmp_time;
2430 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2433 got_ref_list_free(&refs);
2438 static const struct got_error *
2439 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2441 const struct got_error *error;
2443 struct got_object_id *id1 = NULL, *id2 = NULL;
2444 char *label1 = NULL, *label2 = NULL, *line = NULL;
2446 size_t linesize = 0;
2448 enum kcgi_err kerr = KCGI_OK;
2454 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2458 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2459 error = got_repo_match_object_id(&id1, &label1,
2460 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2465 error = got_repo_match_object_id(&id2, &label2,
2466 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2470 error = got_object_get_type(&obj_type, header->repo, id2);
2474 case GOT_OBJ_TYPE_BLOB:
2475 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2478 case GOT_OBJ_TYPE_TREE:
2479 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2482 case GOT_OBJ_TYPE_COMMIT:
2483 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2487 error = got_error(GOT_ERR_OBJ_TYPE);
2492 if (fseek(f, 0, SEEK_SET) == -1) {
2493 error = got_ferror(f, GOT_ERR_IO);
2497 while ((linelen = getline(&line, &linesize, f)) != -1) {
2498 error = gw_colordiff_line(gw_trans, line);
2501 /* XXX: KHTML_PRETTY breaks this */
2502 kerr = khtml_puts(gw_trans->gw_html_req, line);
2503 if (kerr != KCGI_OK)
2505 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2506 if (kerr != KCGI_OK)
2509 if (linelen == -1 && ferror(f))
2510 error = got_error_from_errno("getline");
2512 if (f && fclose(f) == -1 && error == NULL)
2513 error = got_error_from_errno("fclose");
2520 if (error == NULL && kerr != KCGI_OK)
2521 error = gw_kcgi_error(kerr);
2525 static const struct got_error *
2526 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2528 const struct got_error *error = NULL;
2529 struct got_repository *repo;
2530 const char *gitconfig_owner;
2534 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2537 error = got_repo_open(&repo, dir, NULL);
2540 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2541 if (gitconfig_owner)
2542 error = gw_strdup_string(owner, NULL, gitconfig_owner);
2543 got_repo_close(repo);
2547 static const struct got_error *
2548 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2550 const struct got_error *error = NULL;
2552 char *d_file = NULL;
2558 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2559 return got_error_from_errno("asprintf");
2561 f = fopen(d_file, "r");
2563 if (errno != ENOENT && errno != EACCES)
2564 error = got_error_from_errno2("fopen", d_file);
2568 if (fseek(f, 0, SEEK_END) == -1) {
2569 error = got_ferror(f, GOT_ERR_IO);
2574 error = got_ferror(f, GOT_ERR_IO);
2577 if (fseek(f, 0, SEEK_SET) == -1) {
2578 error = got_ferror(f, GOT_ERR_IO);
2582 *url = calloc(len + 1, sizeof(**url));
2584 error = got_error_from_errno("calloc");
2588 n = fread(*url, 1, len, f);
2589 if (n == 0 && ferror(f))
2590 error = got_ferror(f, GOT_ERR_IO);
2592 if (f && fclose(f) == -1 && error == NULL)
2593 error = got_error_from_errno("fclose");
2598 static const struct got_error *
2599 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2600 int limit, int tag_type)
2602 const struct got_error *error = NULL;
2603 struct got_repository *repo = NULL;
2604 struct got_reflist_head refs;
2605 struct got_reflist_entry *re;
2607 char *id_str = NULL, *newline, *href_commits = NULL;
2608 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2609 struct got_tag_object *tag = NULL;
2610 enum kcgi_err kerr = KCGI_OK;
2611 int summary_header_displayed = 0;
2613 SIMPLEQ_INIT(&refs);
2615 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2619 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2623 SIMPLEQ_FOREACH(re, &refs, entry) {
2624 const char *refname;
2626 const char *tag_commit;
2628 struct got_object_id *id;
2629 struct got_commit_object *commit = NULL;
2631 refname = got_ref_get_name(re->ref);
2632 if (strncmp(refname, "refs/tags/", 10) != 0)
2636 error = got_ref_resolve(&id, repo, re->ref);
2640 error = got_object_open_as_tag(&tag, repo, id);
2642 if (error->code != GOT_ERR_OBJ_TYPE) {
2646 /* "lightweight" tag */
2647 error = got_object_open_as_commit(&commit, repo, id);
2652 tagger = got_object_commit_get_committer(commit);
2654 got_object_commit_get_committer_time(commit);
2655 error = got_object_id_str(&id_str, id);
2659 tagger = got_object_tag_get_tagger(tag);
2660 tagger_time = got_object_tag_get_tagger_time(tag);
2661 error = got_object_id_str(&id_str,
2662 got_object_tag_get_object_id(tag));
2667 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2668 strlen(id_str)) != 0)
2672 error = got_object_commit_get_logmsg(&tag_commit0,
2676 got_object_commit_close(commit);
2678 tag_commit0 = strdup(got_object_tag_get_message(tag));
2679 if (tag_commit0 == NULL) {
2680 error = got_error_from_errno("strdup");
2685 tag_commit = tag_commit0;
2686 while (*tag_commit == '\n')
2691 newline = strchr(tag_commit, '\n');
2695 if (summary_header_displayed == 0) {
2696 kerr = khtml_attr(gw_trans->gw_html_req,
2697 KELEM_DIV, KATTR_ID,
2698 "summary_tags_title_wrapper", KATTR__MAX);
2699 if (kerr != KCGI_OK)
2701 kerr = khtml_attr(gw_trans->gw_html_req,
2702 KELEM_DIV, KATTR_ID,
2703 "summary_tags_title", KATTR__MAX);
2704 if (kerr != KCGI_OK)
2706 kerr = khtml_puts(gw_trans->gw_html_req,
2708 if (kerr != KCGI_OK)
2710 kerr = khtml_closeelem(gw_trans->gw_html_req,
2712 if (kerr != KCGI_OK)
2714 kerr = khtml_attr(gw_trans->gw_html_req,
2715 KELEM_DIV, KATTR_ID,
2716 "summary_tags_content", KATTR__MAX);
2717 if (kerr != KCGI_OK)
2719 summary_header_displayed = 1;
2722 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2723 KATTR_ID, "tags_wrapper", KATTR__MAX);
2724 if (kerr != KCGI_OK)
2726 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2727 KATTR_ID, "tags_age", KATTR__MAX);
2728 if (kerr != KCGI_OK)
2730 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2733 kerr = khtml_puts(gw_trans->gw_html_req,
2735 if (kerr != KCGI_OK)
2737 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2738 if (kerr != KCGI_OK)
2740 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2741 KATTR_ID, "tags", KATTR__MAX);
2742 if (kerr != KCGI_OK)
2744 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2745 if (kerr != KCGI_OK)
2747 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2748 if (kerr != KCGI_OK)
2750 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2751 KATTR_ID, "tags_name", KATTR__MAX);
2752 if (kerr != KCGI_OK)
2754 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2755 gw_trans->repo_name, id_str) == -1) {
2756 error = got_error_from_errno("asprintf");
2759 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2760 KATTR_HREF, href_tag, KATTR__MAX);
2761 if (kerr != KCGI_OK)
2763 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2764 if (kerr != KCGI_OK)
2766 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2767 if (kerr != KCGI_OK)
2770 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2771 KATTR_ID, "navs_wrapper", KATTR__MAX);
2772 if (kerr != KCGI_OK)
2774 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2775 KATTR_ID, "navs", KATTR__MAX);
2776 if (kerr != KCGI_OK)
2779 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2780 KATTR_HREF, href_tag, KATTR__MAX);
2781 if (kerr != KCGI_OK)
2783 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2784 if (kerr != KCGI_OK)
2786 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2787 if (kerr != KCGI_OK)
2790 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2791 if (kerr != KCGI_OK)
2793 if (asprintf(&href_briefs,
2794 "?path=%s&action=briefs&commit=%s",
2795 gw_trans->repo_name, id_str) == -1) {
2796 error = got_error_from_errno("asprintf");
2799 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2800 KATTR_HREF, href_briefs, KATTR__MAX);
2801 if (kerr != KCGI_OK)
2803 kerr = khtml_puts(gw_trans->gw_html_req,
2805 if (kerr != KCGI_OK)
2807 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2808 if (kerr != KCGI_OK)
2811 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2812 if (kerr != KCGI_OK)
2815 if (asprintf(&href_commits,
2816 "?path=%s&action=commits&commit=%s",
2817 gw_trans->repo_name, id_str) == -1) {
2818 error = got_error_from_errno("asprintf");
2821 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2822 KATTR_HREF, href_commits, KATTR__MAX);
2823 if (kerr != KCGI_OK)
2825 kerr = khtml_puts(gw_trans->gw_html_req,
2827 if (kerr != KCGI_OK)
2829 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2830 if (kerr != KCGI_OK)
2833 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2834 KATTR_ID, "dotted_line", KATTR__MAX);
2835 if (kerr != KCGI_OK)
2837 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2838 if (kerr != KCGI_OK)
2842 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2843 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2844 if (kerr != KCGI_OK)
2846 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2847 if (kerr != KCGI_OK)
2849 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2850 if (kerr != KCGI_OK)
2852 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2853 KATTR_ID, "tag_info_date", KATTR__MAX);
2854 if (kerr != KCGI_OK)
2856 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2859 kerr = khtml_puts(gw_trans->gw_html_req,
2861 if (kerr != KCGI_OK)
2863 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2864 if (kerr != KCGI_OK)
2867 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2868 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2869 if (kerr != KCGI_OK)
2871 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2872 if (kerr != KCGI_OK)
2874 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2875 if (kerr != KCGI_OK)
2877 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2878 KATTR_ID, "tag_info_date", KATTR__MAX);
2879 if (kerr != KCGI_OK)
2881 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2882 if (kerr != KCGI_OK)
2884 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2885 if (kerr != KCGI_OK)
2888 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2889 KATTR_ID, "tag_info", KATTR__MAX);
2890 if (kerr != KCGI_OK)
2892 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2893 if (kerr != KCGI_OK)
2899 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2900 if (kerr != KCGI_OK)
2903 if (limit && --limit == 0)
2907 got_object_tag_close(tag);
2920 href_commits = NULL;
2924 got_object_tag_close(tag);
2931 got_ref_list_free(&refs);
2933 got_repo_close(repo);
2934 if (error == NULL && kerr != KCGI_OK)
2935 error = gw_kcgi_error(kerr);
2940 gw_free_headers(struct gw_header *header)
2944 if (header->commit != NULL)
2945 got_object_commit_close(header->commit);
2947 got_repo_close(header->repo);
2948 free(header->refs_str);
2949 free(header->commit_id);
2950 free(header->author);
2951 free(header->committer);
2952 free(header->parent_id);
2953 free(header->tree_id);
2954 free(header->commit_msg);
2957 static struct gw_header *
2960 struct gw_header *header;
2962 header = malloc(sizeof(*header));
2966 header->repo = NULL;
2967 header->commit = NULL;
2969 header->path = NULL;
2970 SIMPLEQ_INIT(&header->refs);
2975 static const struct got_error *
2976 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2979 const struct got_error *error = NULL;
2980 struct got_commit_graph *graph = NULL;
2982 error = got_commit_graph_open(&graph, header->path, 0);
2986 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2992 error = got_commit_graph_iter_next(&header->id, graph,
2993 header->repo, NULL, NULL);
2995 if (error->code == GOT_ERR_ITER_COMPLETED)
2999 if (header->id == NULL)
3002 error = got_object_open_as_commit(&header->commit, header->repo,
3007 error = gw_get_commit(gw_trans, header);
3009 struct gw_header *n_header = NULL;
3010 if ((n_header = gw_init_header()) == NULL) {
3011 error = got_error_from_errno("malloc");
3015 error = gw_strdup_string(&n_header->refs_str,
3016 header->refs_str, NULL);
3019 error = gw_strdup_string(&n_header->commit_id,
3020 header->commit_id, NULL);
3023 error = gw_strdup_string(&n_header->parent_id,
3024 header->parent_id, NULL);
3027 error = gw_strdup_string(&n_header->tree_id,
3028 header->tree_id, NULL);
3031 error = gw_strdup_string(&n_header->author,
3032 header->author, NULL);
3035 error = gw_strdup_string(&n_header->committer,
3036 header->committer, NULL);
3039 error = gw_strdup_string(&n_header->commit_msg,
3040 header->commit_msg, NULL);
3043 n_header->committer_time = header->committer_time;
3044 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3047 if (error || (limit && --limit == 0))
3052 got_commit_graph_close(graph);
3056 static const struct got_error *
3057 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3059 const struct got_error *error = NULL;
3060 struct got_reflist_entry *re;
3061 struct got_object_id *id2 = NULL;
3062 struct got_object_qid *parent_id;
3063 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
3066 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3069 struct got_tag_object *tag = NULL;
3072 name = got_ref_get_name(re->ref);
3073 if (strcmp(name, GOT_REF_HEAD) == 0)
3075 if (strncmp(name, "refs/", 5) == 0)
3077 if (strncmp(name, "got/", 4) == 0)
3079 if (strncmp(name, "heads/", 6) == 0)
3081 if (strncmp(name, "remotes/", 8) == 0)
3083 if (strncmp(name, "tags/", 5) == 0) {
3084 error = got_object_open_as_tag(&tag, header->repo,
3087 if (error->code != GOT_ERR_OBJ_TYPE)
3090 * Ref points at something other
3097 cmp = got_object_id_cmp(tag ?
3098 got_object_tag_get_object_id(tag) : re->id, header->id);
3100 got_object_tag_close(tag);
3104 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
3105 s ? ", " : "", name) == -1) {
3106 error = got_error_from_errno("asprintf");
3110 error = gw_strdup_string(&header->refs_str, refs_str, NULL);
3116 if (refs_str == NULL) {
3117 error = gw_strdup_string(&header->refs_str, "", NULL);
3123 error = got_object_id_str(&header->commit_id, header->id);
3127 error = got_object_id_str(&header->tree_id,
3128 got_object_commit_get_tree_id(header->commit));
3132 if (gw_trans->action == GW_DIFF) {
3133 parent_id = SIMPLEQ_FIRST(
3134 got_object_commit_get_parent_ids(header->commit));
3135 if (parent_id != NULL) {
3136 id2 = got_object_id_dup(parent_id->id);
3138 error = got_object_id_str(&header->parent_id, id2);
3143 error = gw_strdup_string(&header->parent_id,
3149 error = gw_strdup_string(&header->parent_id, "", NULL);
3154 header->committer_time =
3155 got_object_commit_get_committer_time(header->commit);
3157 error = gw_strdup_string(&header->author, NULL,
3158 got_object_commit_get_author(header->commit));
3161 error = gw_strdup_string(&header->committer, NULL,
3162 got_object_commit_get_committer(header->commit));
3165 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3169 commit_msg = commit_msg0;
3170 while (*commit_msg == '\n')
3173 error = gw_strdup_string(&header->commit_msg, commit_msg, NULL);
3178 static const struct got_error *
3179 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3181 const struct got_error *error = NULL;
3182 char *in_repo_path = NULL;
3184 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3188 if (gw_trans->commit == NULL) {
3189 struct got_reference *head_ref;
3190 error = got_ref_open(&head_ref, header->repo,
3191 gw_trans->headref, 0);
3195 error = got_ref_resolve(&header->id, header->repo, head_ref);
3196 got_ref_close(head_ref);
3200 error = got_object_open_as_commit(&header->commit,
3201 header->repo, header->id);
3203 struct got_reference *ref;
3204 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3205 if (error == NULL) {
3207 error = got_ref_resolve(&header->id, header->repo, ref);
3211 error = got_object_get_type(&obj_type, header->repo,
3215 if (obj_type == GOT_OBJ_TYPE_TAG) {
3216 struct got_tag_object *tag;
3217 error = got_object_open_as_tag(&tag,
3218 header->repo, header->id);
3221 if (got_object_tag_get_object_type(tag) !=
3222 GOT_OBJ_TYPE_COMMIT) {
3223 got_object_tag_close(tag);
3224 error = got_error(GOT_ERR_OBJ_TYPE);
3228 header->id = got_object_id_dup(
3229 got_object_tag_get_object_id(tag));
3230 if (header->id == NULL)
3231 error = got_error_from_errno(
3232 "got_object_id_dup");
3233 got_object_tag_close(tag);
3236 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3237 error = got_error(GOT_ERR_OBJ_TYPE);
3240 error = got_object_open_as_commit(&header->commit,
3241 header->repo, header->id);
3245 if (header->commit == NULL) {
3246 error = got_repo_match_object_id_prefix(&header->id,
3247 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3252 error = got_repo_match_object_id_prefix(&header->id,
3253 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3257 error = got_repo_map_path(&in_repo_path, header->repo,
3258 gw_trans->repo_path, 1);
3263 error = gw_strdup_string(&header->path, in_repo_path, NULL);
3271 error = got_ref_list(&header->refs, header->repo, NULL,
3272 got_ref_cmp_by_name, NULL);
3276 error = gw_get_commits(gw_trans, header, limit);
3284 char datebuf[11]; /* YYYY-MM-DD + NUL */
3287 struct gw_blame_cb_args {
3288 struct blame_line *lines;
3292 off_t *line_offsets;
3294 struct got_repository *repo;
3295 struct gw_trans *gw_trans;
3298 static const struct got_error *
3299 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3301 const struct got_error *err = NULL;
3302 struct gw_blame_cb_args *a = arg;
3303 struct blame_line *bline;
3305 size_t linesize = 0;
3306 struct got_commit_object *commit = NULL;
3309 time_t committer_time;
3310 enum kcgi_err kerr = KCGI_OK;
3312 if (nlines != a->nlines ||
3313 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3314 return got_error(GOT_ERR_RANGE);
3317 return NULL; /* no change in this commit */
3319 /* Annotate this line. */
3320 bline = &a->lines[lineno - 1];
3321 if (bline->annotated)
3323 err = got_object_id_str(&bline->id_str, id);
3327 err = got_object_open_as_commit(&commit, a->repo, id);
3331 err = gw_strdup_string(&bline->committer, NULL,
3332 got_object_commit_get_committer(commit));
3336 committer_time = got_object_commit_get_committer_time(commit);
3337 if (localtime_r(&committer_time, &tm) == NULL)
3338 return got_error_from_errno("localtime_r");
3339 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3340 &tm) >= sizeof(bline->datebuf)) {
3341 err = got_error(GOT_ERR_NO_SPACE);
3344 bline->annotated = 1;
3346 /* Print lines annotated so far. */
3347 bline = &a->lines[a->lineno_cur - 1];
3348 if (!bline->annotated)
3351 offset = a->line_offsets[a->lineno_cur - 1];
3352 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3353 err = got_error_from_errno("fseeko");
3357 while (bline->annotated) {
3358 char *smallerthan, *at, *nl, *committer;
3359 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3362 if (getline(&line, &linesize, a->f) == -1) {
3364 err = got_error_from_errno("getline");
3368 committer = bline->committer;
3369 smallerthan = strchr(committer, '<');
3370 if (smallerthan && smallerthan[1] != '\0')
3371 committer = smallerthan + 1;
3372 at = strchr(committer, '@');
3375 len = strlen(committer);
3377 committer[8] = '\0';
3379 nl = strchr(line, '\n');
3383 if (a->gw_trans->repo_folder == NULL) {
3384 err = gw_strdup_string(&a->gw_trans->repo_folder, "",
3389 if (a->gw_trans->repo_folder == NULL)
3392 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3393 "blame_wrapper", KATTR__MAX);
3394 if (kerr != KCGI_OK)
3396 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3397 "blame_number", KATTR__MAX);
3398 if (kerr != KCGI_OK)
3400 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3401 a->lineno_cur) == -1)
3403 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3404 if (kerr != KCGI_OK)
3406 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3407 if (kerr != KCGI_OK)
3410 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3411 "blame_hash", KATTR__MAX);
3412 if (kerr != KCGI_OK)
3414 /* XXX repo_file could be NULL if not present in querystring */
3415 if (asprintf(&href_diff,
3416 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3417 a->gw_trans->repo_name, bline->id_str,
3418 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3419 err = got_error_from_errno("asprintf");
3422 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3423 err = got_error_from_errno("asprintf");
3426 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3427 KATTR_HREF, href_diff, KATTR__MAX);
3428 if (kerr != KCGI_OK)
3430 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3431 if (kerr != KCGI_OK)
3433 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3434 if (kerr != KCGI_OK)
3437 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3438 "blame_date", KATTR__MAX);
3439 if (kerr != KCGI_OK)
3441 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3442 if (kerr != KCGI_OK)
3444 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3445 if (kerr != KCGI_OK)
3448 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3449 "blame_author", KATTR__MAX);
3450 if (kerr != KCGI_OK)
3452 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3453 if (kerr != KCGI_OK)
3455 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3456 if (kerr != KCGI_OK)
3459 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3460 "blame_code", KATTR__MAX);
3461 if (kerr != KCGI_OK)
3463 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3464 if (kerr != KCGI_OK)
3466 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3467 if (kerr != KCGI_OK)
3470 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3471 if (kerr != KCGI_OK)
3475 bline = &a->lines[a->lineno_cur - 1];
3483 got_object_commit_close(commit);
3485 if (err == NULL && kerr != KCGI_OK)
3486 err = gw_kcgi_error(kerr);
3490 static const struct got_error *
3491 gw_output_file_blame(struct gw_trans *gw_trans)
3493 const struct got_error *error = NULL;
3494 struct got_repository *repo = NULL;
3495 struct got_object_id *obj_id = NULL;
3496 struct got_object_id *commit_id = NULL;
3497 struct got_blob_object *blob = NULL;
3498 char *path = NULL, *in_repo_path = NULL;
3499 struct gw_blame_cb_args bca;
3503 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3507 /* XXX repo_file could be NULL if not present in querystring */
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 /* XXX repo_file could be NULL if not present in querystring */
3619 if (asprintf(&path, "%s%s%s",
3620 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3621 gw_trans->repo_folder ? "/" : "",
3622 gw_trans->repo_file) == -1) {
3623 error = got_error_from_errno("asprintf");
3627 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3631 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3632 GOT_OBJ_TYPE_COMMIT, 1, repo);
3636 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3640 if (obj_id == NULL) {
3641 error = got_error(GOT_ERR_NO_OBJ);
3645 error = got_object_get_type(&obj_type, repo, obj_id);
3649 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3650 error = got_error(GOT_ERR_OBJ_TYPE);
3654 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3658 hdrlen = got_object_blob_get_hdrlen(blob);
3660 error = got_object_blob_read_block(&len, blob);
3663 buf = got_object_blob_get_read_buf(blob);
3666 * Skip blob object header first time around,
3667 * which also contains a zero byte.
3670 if (set_mime == 0) {
3671 if (isbinary(buf, len - hdrlen))
3672 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3674 gw_trans->mime = KMIME_TEXT_PLAIN;
3676 error = gw_display_index(gw_trans);
3680 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3689 got_object_blob_close(blob);
3691 got_repo_close(repo);
3692 if (error == NULL && kerr != KCGI_OK)
3693 error = gw_kcgi_error(kerr);
3697 static const struct got_error *
3698 gw_output_repo_tree(struct gw_trans *gw_trans)
3700 const struct got_error *error = NULL;
3701 struct got_repository *repo = NULL;
3702 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3703 struct got_tree_object *tree = NULL;
3704 char *path = NULL, *in_repo_path = NULL;
3705 char *id_str = NULL;
3706 char *build_folder = NULL;
3707 char *href_blob = NULL, *href_blame = NULL;
3708 const char *class = NULL;
3709 int nentries, i, class_flip = 0;
3710 enum kcgi_err kerr = KCGI_OK;
3712 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3716 if (gw_trans->repo_folder != NULL) {
3717 error = gw_strdup_string(&path, gw_trans->repo_folder, NULL);
3721 error = got_repo_map_path(&in_repo_path, repo,
3722 gw_trans->repo_path, 1);
3726 path = in_repo_path;
3729 if (gw_trans->commit == NULL) {
3730 struct got_reference *head_ref;
3731 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3734 error = got_ref_resolve(&commit_id, repo, head_ref);
3737 got_ref_close(head_ref);
3740 error = got_repo_match_object_id(&commit_id, NULL,
3741 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3747 * XXX gw_trans->commit might have already been allocated in
3748 * gw_parse_querystring(); could we more cleanly seperate values
3749 * we received as arguments from values we compute ourselves?
3751 error = got_object_id_str(&gw_trans->commit, commit_id);
3755 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3759 error = got_object_open_as_tree(&tree, repo, tree_id);
3763 nentries = got_object_tree_get_nentries(tree);
3764 for (i = 0; i < nentries; i++) {
3765 struct got_tree_entry *te;
3766 const char *modestr = "";
3769 te = got_object_tree_get_entry(tree, i);
3771 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3775 mode = got_tree_entry_get_mode(te);
3776 if (got_object_tree_entry_is_submodule(te))
3778 else if (S_ISLNK(mode))
3780 else if (S_ISDIR(mode))
3782 else if (mode & S_IXUSR)
3785 if (class_flip == 0) {
3786 class = "back_lightgray";
3789 class = "back_white";
3793 if (S_ISDIR(mode)) {
3794 if (asprintf(&build_folder, "%s/%s",
3795 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3796 got_tree_entry_get_name(te)) == -1) {
3797 error = got_error_from_errno(
3801 if (asprintf(&href_blob,
3802 "?path=%s&action=%s&commit=%s&folder=%s",
3803 gw_trans->repo_name, gw_get_action_name(gw_trans),
3804 gw_trans->commit, build_folder) == -1) {
3805 error = got_error_from_errno("asprintf");
3809 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3810 KATTR_ID, "tree_wrapper", KATTR__MAX);
3811 if (kerr != KCGI_OK)
3813 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3814 KATTR_ID, "tree_line", KATTR_CLASS, class,
3816 if (kerr != KCGI_OK)
3818 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3819 KATTR_HREF, href_blob, KATTR_CLASS,
3820 "diff_directory", KATTR__MAX);
3821 if (kerr != KCGI_OK)
3823 kerr = khtml_puts(gw_trans->gw_html_req,
3824 got_tree_entry_get_name(te));
3825 if (kerr != KCGI_OK)
3827 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3828 if (kerr != KCGI_OK)
3830 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3831 if (kerr != KCGI_OK)
3833 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3834 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3836 if (kerr != KCGI_OK)
3838 kerr = khtml_entity(gw_trans->gw_html_req,
3840 if (kerr != KCGI_OK)
3842 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3843 if (kerr != KCGI_OK)
3846 if (asprintf(&href_blob,
3847 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3848 gw_trans->repo_name, "blob", gw_trans->commit,
3849 got_tree_entry_get_name(te),
3850 gw_trans->repo_folder ?
3851 gw_trans->repo_folder : "") == -1) {
3852 error = got_error_from_errno("asprintf");
3855 if (asprintf(&href_blame,
3856 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3857 gw_trans->repo_name, "blame", gw_trans->commit,
3858 got_tree_entry_get_name(te),
3859 gw_trans->repo_folder ?
3860 gw_trans->repo_folder : "") == -1) {
3861 error = got_error_from_errno("asprintf");
3865 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3866 KATTR_ID, "tree_wrapper", KATTR__MAX);
3867 if (kerr != KCGI_OK)
3869 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3870 KATTR_ID, "tree_line", KATTR_CLASS, class,
3872 if (kerr != KCGI_OK)
3874 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3875 KATTR_HREF, href_blob, KATTR__MAX);
3876 if (kerr != KCGI_OK)
3878 kerr = khtml_puts(gw_trans->gw_html_req,
3879 got_tree_entry_get_name(te));
3880 if (kerr != KCGI_OK)
3882 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3883 if (kerr != KCGI_OK)
3885 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3886 if (kerr != KCGI_OK)
3888 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3889 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3891 if (kerr != KCGI_OK)
3894 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3895 KATTR_HREF, href_blob, KATTR__MAX);
3896 if (kerr != KCGI_OK)
3898 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3899 if (kerr != KCGI_OK)
3901 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3902 if (kerr != KCGI_OK)
3905 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3906 if (kerr != KCGI_OK)
3909 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3910 KATTR_HREF, href_blame, KATTR__MAX);
3911 if (kerr != KCGI_OK)
3913 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3914 if (kerr != KCGI_OK)
3917 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3918 if (kerr != KCGI_OK)
3926 build_folder = NULL;
3930 got_object_tree_close(tree);
3932 got_repo_close(repo);
3939 if (error == NULL && kerr != KCGI_OK)
3940 error = gw_kcgi_error(kerr);
3944 static const struct got_error *
3945 gw_output_repo_heads(struct gw_trans *gw_trans)
3947 const struct got_error *error = NULL;
3948 struct got_repository *repo = NULL;
3949 struct got_reflist_head refs;
3950 struct got_reflist_entry *re;
3951 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3952 char *href_commits = NULL;
3953 enum kcgi_err kerr = KCGI_OK;
3955 SIMPLEQ_INIT(&refs);
3957 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3961 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3966 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3967 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3968 if (kerr != KCGI_OK)
3970 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3971 KATTR_ID, "summary_heads_title", KATTR__MAX);
3972 if (kerr != KCGI_OK)
3974 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
3975 if (kerr != KCGI_OK)
3977 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3978 if (kerr != KCGI_OK)
3980 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3981 KATTR_ID, "summary_heads_content", KATTR__MAX);
3982 if (kerr != KCGI_OK)
3985 SIMPLEQ_FOREACH(re, &refs, entry) {
3988 error = gw_strdup_string(&refname, NULL,
3989 got_ref_get_name(re->ref));
3993 if (strncmp(refname, "refs/heads/", 11) != 0) {
3998 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4003 if (strncmp(refname, "refs/heads/", 11) == 0)
4006 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4007 KATTR_ID, "heads_wrapper", KATTR__MAX);
4008 if (kerr != KCGI_OK)
4010 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4011 KATTR_ID, "heads_age", KATTR__MAX);
4012 if (kerr != KCGI_OK)
4014 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4015 if (kerr != KCGI_OK)
4017 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4018 if (kerr != KCGI_OK)
4020 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4021 KATTR_ID, "heads_space", KATTR__MAX);
4022 if (kerr != KCGI_OK)
4024 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4025 if (kerr != KCGI_OK)
4027 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4028 if (kerr != KCGI_OK)
4030 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4031 KATTR_ID, "head", KATTR__MAX);
4032 if (kerr != KCGI_OK)
4034 if (asprintf(&href_summary,
4035 "?path=%s&action=summary&headref=%s",
4036 gw_trans->repo_name, refname) == -1) {
4037 error = got_error_from_errno("asprintf");
4040 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4041 href_summary, KATTR__MAX);
4042 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4043 if (kerr != KCGI_OK)
4045 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4046 if (kerr != KCGI_OK)
4049 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4050 "navs_wrapper", KATTR__MAX);
4051 if (kerr != KCGI_OK)
4053 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4054 "navs", KATTR__MAX);
4055 if (kerr != KCGI_OK)
4058 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4059 href_summary, KATTR__MAX);
4060 if (kerr != KCGI_OK)
4062 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4063 if (kerr != KCGI_OK)
4065 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4066 if (kerr != KCGI_OK)
4069 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4070 if (kerr != KCGI_OK)
4072 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4073 gw_trans->repo_name, refname) == -1) {
4074 error = got_error_from_errno("asprintf");
4077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4078 href_briefs, KATTR__MAX);
4079 if (kerr != KCGI_OK)
4081 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4082 if (kerr != KCGI_OK)
4084 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4085 if (kerr != KCGI_OK)
4088 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4089 if (kerr != KCGI_OK)
4092 if (asprintf(&href_commits,
4093 "?path=%s&action=commits&headref=%s",
4094 gw_trans->repo_name, refname) == -1) {
4095 error = got_error_from_errno("asprintf");
4098 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4099 href_commits, KATTR__MAX);
4100 if (kerr != KCGI_OK)
4102 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4103 if (kerr != KCGI_OK)
4105 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4106 if (kerr != KCGI_OK)
4109 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4110 "dotted_line", KATTR__MAX);
4111 if (kerr != KCGI_OK)
4113 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4114 if (kerr != KCGI_OK)
4117 href_summary = NULL;
4121 href_commits = NULL;
4124 got_ref_list_free(&refs);
4129 got_repo_close(repo);
4133 static const struct got_error *
4134 gw_output_site_link(struct gw_trans *gw_trans)
4136 const struct got_error *error = NULL;
4137 char *href_summary = NULL;
4138 enum kcgi_err kerr = KCGI_OK;
4140 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4141 "site_link", KATTR__MAX);
4142 if (kerr != KCGI_OK)
4144 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4146 if (kerr != KCGI_OK)
4148 kerr = khtml_puts(gw_trans->gw_html_req,
4149 gw_trans->gw_conf->got_site_link);
4150 if (kerr != KCGI_OK)
4152 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4153 if (kerr != KCGI_OK)
4156 if (gw_trans->repo_name != NULL) {
4157 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4158 if (kerr != KCGI_OK)
4160 if (asprintf(&href_summary, "?path=%s&action=summary",
4161 gw_trans->repo_name) == -1)
4163 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4164 href_summary, KATTR__MAX);
4165 if (kerr != KCGI_OK)
4167 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4168 if (kerr != KCGI_OK)
4170 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4171 if (kerr != KCGI_OK)
4173 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4174 if (kerr != KCGI_OK)
4176 kerr = khtml_puts(gw_trans->gw_html_req,
4177 gw_get_action_name(gw_trans));
4178 if (kerr != KCGI_OK)
4182 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4183 if (kerr != KCGI_OK)
4190 static const struct got_error *
4191 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4193 const struct got_error *error = NULL;
4195 enum kcgi_err kerr = KCGI_OK;
4197 if (strncmp(buf, "-", 1) == 0)
4198 color = "diff_minus";
4199 else if (strncmp(buf, "+", 1) == 0)
4200 color = "diff_plus";
4201 else if (strncmp(buf, "@@", 2) == 0)
4202 color = "diff_chunk_header";
4203 else if (strncmp(buf, "@@", 2) == 0)
4204 color = "diff_chunk_header";
4205 else if (strncmp(buf, "commit +", 8) == 0)
4206 color = "diff_meta";
4207 else if (strncmp(buf, "commit -", 8) == 0)
4208 color = "diff_meta";
4209 else if (strncmp(buf, "blob +", 6) == 0)
4210 color = "diff_meta";
4211 else if (strncmp(buf, "blob -", 6) == 0)
4212 color = "diff_meta";
4213 else if (strncmp(buf, "file +", 6) == 0)
4214 color = "diff_meta";
4215 else if (strncmp(buf, "file -", 6) == 0)
4216 color = "diff_meta";
4217 else if (strncmp(buf, "from:", 5) == 0)
4218 color = "diff_author";
4219 else if (strncmp(buf, "via:", 4) == 0)
4220 color = "diff_author";
4221 else if (strncmp(buf, "date:", 5) == 0)
4222 color = "diff_date";
4223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4224 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4225 if (error == NULL && kerr != KCGI_OK)
4226 error = gw_kcgi_error(kerr);
4231 main(int argc, char *argv[])
4233 const struct got_error *error = NULL;
4234 struct gw_trans *gw_trans;
4235 struct gw_dir *dir = NULL, *tdir;
4236 const char *page = "index";
4240 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4243 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4246 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4249 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4252 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4253 if (kerr != KCGI_OK) {
4254 error = gw_kcgi_error(kerr);
4258 if ((gw_trans->gw_conf =
4259 malloc(sizeof(struct gotweb_conf))) == NULL) {
4261 error = got_error_from_errno("malloc");
4265 TAILQ_INIT(&gw_trans->gw_dirs);
4266 TAILQ_INIT(&gw_trans->gw_headers);
4269 gw_trans->repos_total = 0;
4270 gw_trans->repo_path = NULL;
4271 gw_trans->commit = NULL;
4272 error = gw_strdup_string(&gw_trans->headref, GOT_REF_HEAD, NULL);
4275 gw_trans->mime = KMIME_TEXT_HTML;
4276 gw_trans->gw_tmpl->key = gw_templs;
4277 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4278 gw_trans->gw_tmpl->arg = gw_trans;
4279 gw_trans->gw_tmpl->cb = gw_template;
4280 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4284 error = gw_parse_querystring(gw_trans);
4288 if (gw_trans->action == GW_BLOB)
4289 error = gw_blob(gw_trans);
4291 error = gw_display_index(gw_trans);
4294 gw_trans->mime = KMIME_TEXT_PLAIN;
4295 gw_trans->action = GW_ERR;
4296 gw_display_error(gw_trans, error);
4299 free(gw_trans->gw_conf->got_repos_path);
4300 free(gw_trans->gw_conf->got_site_name);
4301 free(gw_trans->gw_conf->got_site_owner);
4302 free(gw_trans->gw_conf->got_site_link);
4303 free(gw_trans->gw_conf->got_logo);
4304 free(gw_trans->gw_conf->got_logo_url);
4305 free(gw_trans->gw_conf);
4306 free(gw_trans->commit);
4307 free(gw_trans->repo_path);
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);