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>
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 TAILQ_HEAD(headers, gw_header) gw_headers;
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
73 unsigned int repos_total;
78 TAILQ_ENTRY(gw_header) entry;
79 struct got_repository *repo;
80 struct got_reflist_head refs;
81 struct got_commit_object *commit;
82 struct got_object_id *id;
86 char *commit_id; /* id_str1 */
87 char *parent_id; /* id_str2 */
90 const char *committer;
92 time_t committer_time;
96 TAILQ_ENTRY(gw_dir) entry;
137 static const char *const gw_templs[TEMPL__MAX] = {
147 static const struct kvalid gw_keys[KEY__ZMAX] = {
148 { kvalid_stringne, "action" },
149 { kvalid_stringne, "commit" },
150 { kvalid_stringne, "file" },
151 { kvalid_stringne, "folder" },
152 { kvalid_stringne, "headref" },
153 { kvalid_int, "page" },
154 { kvalid_stringne, "path" },
157 static struct gw_dir *gw_init_gw_dir(char *);
158 static struct gw_header *gw_init_header(void);
160 static const struct got_error *gw_get_repo_description(char **,
161 struct gw_trans *, char *);
162 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
164 static const struct got_error *gw_get_time_str(char **, time_t, int);
165 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
166 char *, char *, int);
167 static const struct got_error *gw_output_file_blame(struct gw_trans *);
168 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
169 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
170 static const struct got_error *gw_output_diff(struct gw_trans *,
172 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
173 struct gw_header *, int, int);
174 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
175 static const struct got_error *gw_output_site_link(struct gw_trans *);
176 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
178 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
180 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
182 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
184 static const struct got_error *gw_gen_author_header(struct gw_trans *,
186 static const struct got_error *gw_gen_age_header(struct gw_trans *,
188 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
190 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
192 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
194 static void gw_free_headers(struct gw_header *);
195 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
197 static const struct got_error* gw_display_index(struct gw_trans *);
198 static void gw_display_error(struct gw_trans *,
199 const struct got_error *);
201 static int gw_template(size_t, void *);
203 static const struct got_error* gw_get_header(struct gw_trans *,
204 struct gw_header *, int);
205 static const struct got_error* gw_get_commits(struct gw_trans *,
206 struct gw_header *, int);
207 static const struct got_error* gw_get_commit(struct gw_trans *,
209 static const struct got_error* gw_apply_unveil(const char *, const char *);
210 static const struct got_error* gw_blame_cb(void *, int, int,
211 struct got_object_id *);
212 static const struct got_error* gw_load_got_paths(struct gw_trans *);
213 static const struct got_error* gw_load_got_path(struct gw_trans *,
215 static const struct got_error* gw_parse_querystring(struct gw_trans *);
217 static const struct got_error* gw_blame(struct gw_trans *);
218 static const struct got_error* gw_blob(struct gw_trans *);
219 static const struct got_error* gw_diff(struct gw_trans *);
220 static const struct got_error* gw_index(struct gw_trans *);
221 static const struct got_error* gw_commits(struct gw_trans *);
222 static const struct got_error* gw_briefs(struct gw_trans *);
223 static const struct got_error* gw_summary(struct gw_trans *);
224 static const struct got_error* gw_tree(struct gw_trans *);
225 static const struct got_error* gw_tag(struct gw_trans *);
227 struct gw_query_action {
228 unsigned int func_id;
229 const char *func_name;
230 const struct got_error *(*func_main)(struct gw_trans *);
234 enum gw_query_actions {
247 static struct gw_query_action gw_query_funcs[] = {
248 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
249 { GW_BLOB, "blob", NULL, NULL },
250 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
251 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
252 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
253 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
254 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
255 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
256 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
257 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
260 static const struct got_error *
261 gw_kcgi_error(enum kcgi_err kerr)
266 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
267 return got_error(GOT_ERR_CANCELLED);
269 if (kerr == KCGI_ENOMEM)
270 return got_error_set_errno(ENOMEM,
271 kcgi_strerror(kerr));
273 if (kerr == KCGI_ENFILE)
274 return got_error_set_errno(ENFILE,
275 kcgi_strerror(kerr));
277 if (kerr == KCGI_EAGAIN)
278 return got_error_set_errno(EAGAIN,
279 kcgi_strerror(kerr));
281 if (kerr == KCGI_FORM)
282 return got_error_msg(GOT_ERR_IO,
283 kcgi_strerror(kerr));
285 return got_error_from_errno(kcgi_strerror(kerr));
288 static const struct got_error *
289 gw_apply_unveil(const char *repo_path, const char *repo_file)
291 const struct got_error *err;
293 if (repo_path && repo_file) {
295 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
296 return got_error_from_errno("asprintf unveil");
297 if (unveil(full_path, "r") != 0)
298 return got_error_from_errno2("unveil", full_path);
301 if (repo_path && unveil(repo_path, "r") != 0)
302 return got_error_from_errno2("unveil", repo_path);
304 if (unveil("/tmp", "rwc") != 0)
305 return got_error_from_errno2("unveil", "/tmp");
307 err = got_privsep_unveil_exec_helpers();
311 if (unveil(NULL, NULL) != 0)
312 return got_error_from_errno("unveil");
317 static const struct got_error *
318 gw_empty_string(char **s)
322 return got_error_from_errno("strdup");
327 isbinary(const uint8_t *buf, size_t n)
331 for (i = 0; i < n; i++)
337 static const struct got_error *
338 gw_blame(struct gw_trans *gw_trans)
340 const struct got_error *error = NULL;
341 struct gw_header *header = NULL;
345 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
347 return got_error_from_errno("pledge");
349 if ((header = gw_init_header()) == NULL)
350 return got_error_from_errno("malloc");
352 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
356 error = gw_get_header(gw_trans, header, 1);
360 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
361 "blame_header_wrapper", KATTR__MAX);
364 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
365 "blame_header", KATTR__MAX);
368 error = gw_get_time_str(&age, header->committer_time,
372 error = gw_gen_age_header(gw_trans, age ?age : "");
375 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
378 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
381 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
382 "dotted_line", KATTR__MAX);
385 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
390 "blame", KATTR__MAX);
393 error = gw_output_file_blame(gw_trans);
396 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
398 got_ref_list_free(&header->refs);
399 gw_free_headers(header);
400 if (error == NULL && kerr != KCGI_OK)
401 error = gw_kcgi_error(kerr);
405 static const struct got_error *
406 gw_blob(struct gw_trans *gw_trans)
408 const struct got_error *error = NULL;
409 struct gw_header *header = NULL;
411 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
413 return got_error_from_errno("pledge");
415 if ((header = gw_init_header()) == NULL)
416 return got_error_from_errno("malloc");
418 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
422 error = gw_get_header(gw_trans, header, 1);
426 error = gw_output_blob_buf(gw_trans);
428 got_ref_list_free(&header->refs);
429 gw_free_headers(header);
433 static const struct got_error *
434 gw_diff(struct gw_trans *gw_trans)
436 const struct got_error *error = NULL;
437 struct gw_header *header = NULL;
439 enum kcgi_err kerr = KCGI_OK;
441 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
443 return got_error_from_errno("pledge");
445 if ((header = gw_init_header()) == NULL)
446 return got_error_from_errno("malloc");
448 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
452 error = gw_get_header(gw_trans, header, 1);
456 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
457 "diff_header_wrapper", KATTR__MAX);
460 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
461 "diff_header", KATTR__MAX);
464 error = gw_gen_diff_header(gw_trans, header->parent_id,
468 error = gw_gen_commit_header(gw_trans, header->commit_id,
472 error = gw_gen_tree_header(gw_trans, header->tree_id);
475 error = gw_gen_author_header(gw_trans, header->author);
478 error = gw_gen_committer_header(gw_trans, header->author);
481 error = gw_get_time_str(&age, header->committer_time,
485 error = gw_gen_age_header(gw_trans, age ?age : "");
488 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
491 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
494 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
495 "dotted_line", KATTR__MAX);
498 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
502 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
506 error = gw_output_diff(gw_trans, header);
510 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, NULL);
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);
645 return gw_kcgi_error(kerr);
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);
654 if (kerr != KCGI_OK) {
655 error = gw_kcgi_error(kerr);
658 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
659 href_summary, KATTR__MAX);
660 if (kerr != KCGI_OK) {
661 error = gw_kcgi_error(kerr);
664 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
665 if (kerr != KCGI_OK) {
666 error = gw_kcgi_error(kerr);
669 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
670 if (kerr != KCGI_OK) {
671 error = gw_kcgi_error(kerr);
674 if (gw_trans->gw_conf->got_show_repo_description) {
675 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
676 KATTR_ID, "index_project_description", KATTR__MAX);
677 if (kerr != KCGI_OK) {
678 error = gw_kcgi_error(kerr);
681 kerr = khtml_puts(gw_trans->gw_html_req,
682 gw_dir->description ? gw_dir->description : "");
683 if (kerr != KCGI_OK) {
684 error = gw_kcgi_error(kerr);
687 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
688 if (kerr != KCGI_OK) {
689 error = gw_kcgi_error(kerr);
693 if (gw_trans->gw_conf->got_show_repo_owner) {
694 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
695 KATTR_ID, "index_project_owner", KATTR__MAX);
696 if (kerr != KCGI_OK) {
697 error = gw_kcgi_error(kerr);
700 kerr = khtml_puts(gw_trans->gw_html_req,
701 gw_dir->owner ? gw_dir->owner : "");
702 if (kerr != KCGI_OK) {
703 error = gw_kcgi_error(kerr);
706 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
707 if (kerr != KCGI_OK) {
708 error = gw_kcgi_error(kerr);
712 if (gw_trans->gw_conf->got_show_repo_age) {
713 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
714 KATTR_ID, "index_project_age", KATTR__MAX);
715 if (kerr != KCGI_OK) {
716 error = gw_kcgi_error(kerr);
719 kerr = khtml_puts(gw_trans->gw_html_req,
720 gw_dir->age ? gw_dir->age : "");
721 if (kerr != KCGI_OK) {
722 error = gw_kcgi_error(kerr);
725 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
726 if (kerr != KCGI_OK) {
727 error = gw_kcgi_error(kerr);
732 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
733 "navs_wrapper", KATTR__MAX);
734 if (kerr != KCGI_OK) {
735 error = gw_kcgi_error(kerr);
738 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
740 if (kerr != KCGI_OK) {
741 error = gw_kcgi_error(kerr);
745 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
746 href_summary, KATTR__MAX);
747 if (kerr != KCGI_OK) {
748 error = gw_kcgi_error(kerr);
751 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
752 if (kerr != KCGI_OK) {
753 error = gw_kcgi_error(kerr);
756 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
757 if (kerr != KCGI_OK) {
758 error = gw_kcgi_error(kerr);
762 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
763 if (kerr != KCGI_OK) {
764 error = gw_kcgi_error(kerr);
768 if (asprintf(&href_briefs, "?path=%s&action=briefs",
769 gw_dir->name) == -1) {
770 error = got_error_from_errno("asprintf");
773 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
774 href_briefs, KATTR__MAX);
775 if (kerr != KCGI_OK) {
776 error = gw_kcgi_error(kerr);
779 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
780 if (kerr != KCGI_OK) {
781 error = gw_kcgi_error(kerr);
784 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
785 if (kerr != KCGI_OK) {
786 error = gw_kcgi_error(kerr);
790 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
791 if (kerr != KCGI_OK) {
792 error = gw_kcgi_error(kerr);
796 if (asprintf(&href_commits, "?path=%s&action=commits",
797 gw_dir->name) == -1) {
798 error = got_error_from_errno("asprintf");
801 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
802 href_commits, KATTR__MAX);
803 if (kerr != KCGI_OK) {
804 error = gw_kcgi_error(kerr);
807 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
808 if (kerr != KCGI_OK) {
809 error = gw_kcgi_error(kerr);
812 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
813 if (kerr != KCGI_OK) {
814 error = gw_kcgi_error(kerr);
818 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
819 if (kerr != KCGI_OK) {
820 error = gw_kcgi_error(kerr);
824 if (asprintf(&href_tree, "?path=%s&action=tree",
825 gw_dir->name) == -1) {
826 error = got_error_from_errno("asprintf");
829 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
830 href_tree, KATTR__MAX);
831 if (kerr != KCGI_OK) {
832 error = gw_kcgi_error(kerr);
835 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
836 if (kerr != KCGI_OK) {
837 error = gw_kcgi_error(kerr);
841 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
842 if (kerr != KCGI_OK) {
843 error = gw_kcgi_error(kerr);
846 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
847 "dotted_line", KATTR__MAX);
848 if (kerr != KCGI_OK) {
849 error = gw_kcgi_error(kerr);
852 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
853 if (kerr != KCGI_OK) {
854 error = gw_kcgi_error(kerr);
867 if (gw_trans->gw_conf->got_max_repos_display == 0)
870 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
871 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
872 KATTR_ID, "np_wrapper", KATTR__MAX);
873 if (kerr != KCGI_OK) {
874 error = gw_kcgi_error(kerr);
877 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
878 KATTR_ID, "nav_prev", KATTR__MAX);
879 if (kerr != KCGI_OK) {
880 error = gw_kcgi_error(kerr);
883 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
884 (gw_trans->page > 0) &&
885 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
886 prev_disp == gw_trans->repos_total)) {
887 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
888 KATTR_ID, "np_wrapper", KATTR__MAX);
889 if (kerr != KCGI_OK) {
890 error = gw_kcgi_error(kerr);
893 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
894 KATTR_ID, "nav_prev", KATTR__MAX);
895 if (kerr != KCGI_OK) {
896 error = gw_kcgi_error(kerr);
901 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
902 (gw_trans->page > 0) &&
903 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
904 prev_disp == gw_trans->repos_total)) {
905 if (asprintf(&href_prev, "?page=%d",
906 gw_trans->page - 1) == -1) {
907 error = got_error_from_errno("asprintf");
910 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
911 KATTR_HREF, href_prev, KATTR__MAX);
913 if (kerr != KCGI_OK) {
914 error = gw_kcgi_error(kerr);
917 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
918 if (kerr != KCGI_OK) {
919 error = gw_kcgi_error(kerr);
922 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
923 if (kerr != KCGI_OK) {
924 error = gw_kcgi_error(kerr);
929 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
931 return gw_kcgi_error(kerr);
933 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
934 next_disp == gw_trans->gw_conf->got_max_repos_display &&
935 dir_c != (gw_trans->page + 1) *
936 gw_trans->gw_conf->got_max_repos_display) {
937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
938 KATTR_ID, "nav_next", KATTR__MAX);
939 if (kerr != KCGI_OK) {
940 error = gw_kcgi_error(kerr);
943 if (asprintf(&href_next, "?page=%d",
944 gw_trans->page + 1) == -1) {
945 error = got_error_from_errno("calloc");
948 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
949 KATTR_HREF, href_next, KATTR__MAX);
951 if (kerr != KCGI_OK) {
952 error = gw_kcgi_error(kerr);
955 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
956 if (kerr != KCGI_OK) {
957 error = gw_kcgi_error(kerr);
960 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
961 if (kerr != KCGI_OK) {
962 error = gw_kcgi_error(kerr);
969 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
970 (gw_trans->page > 0) &&
971 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
972 prev_disp == gw_trans->repos_total)) {
973 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
974 if (kerr != KCGI_OK) {
975 error = gw_kcgi_error(kerr);
989 static const struct got_error *
990 gw_commits(struct gw_trans *gw_trans)
992 const struct got_error *error = NULL;
993 struct gw_header *header = NULL, *n_header = NULL;
995 char *href_diff = NULL, *href_blob = NULL;
996 enum kcgi_err kerr = KCGI_OK;
998 if ((header = gw_init_header()) == NULL)
999 return got_error_from_errno("malloc");
1001 if (pledge("stdio rpath proc exec sendfd unveil",
1003 error = got_error_from_errno("pledge");
1007 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1011 error = gw_get_header(gw_trans, header,
1012 gw_trans->gw_conf->got_max_commits_display);
1016 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1017 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1018 "commits_line_wrapper", KATTR__MAX);
1019 if (kerr != KCGI_OK)
1021 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
1022 n_header->refs_str);
1025 error = gw_gen_author_header(gw_trans, n_header->author);
1028 error = gw_gen_committer_header(gw_trans, n_header->author);
1031 error = gw_get_time_str(&age, n_header->committer_time,
1035 error = gw_gen_age_header(gw_trans, age ?age : "");
1038 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1039 if (kerr != KCGI_OK)
1042 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1043 "dotted_line", KATTR__MAX);
1044 if (kerr != KCGI_OK)
1046 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1047 if (kerr != KCGI_OK)
1050 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1051 "commit", KATTR__MAX);
1052 if (kerr != KCGI_OK)
1054 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1055 if (kerr != KCGI_OK)
1057 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1058 if (kerr != KCGI_OK)
1061 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1062 gw_trans->repo_name, n_header->commit_id) == -1) {
1063 error = got_error_from_errno("asprintf");
1066 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1067 KATTR_ID, "navs_wrapper", KATTR__MAX);
1068 if (kerr != KCGI_OK)
1070 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1071 KATTR_ID, "navs", KATTR__MAX);
1072 if (kerr != KCGI_OK)
1074 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1075 KATTR_HREF, href_diff, KATTR__MAX);
1076 if (kerr != KCGI_OK)
1078 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1079 if (kerr != KCGI_OK)
1081 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1082 if (kerr != KCGI_OK)
1085 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1086 if (kerr != KCGI_OK)
1089 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1090 gw_trans->repo_name, n_header->commit_id) == -1) {
1091 error = got_error_from_errno("asprintf");
1094 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1095 KATTR_HREF, href_blob, KATTR__MAX);
1096 if (kerr != KCGI_OK)
1098 khtml_puts(gw_trans->gw_html_req, "tree");
1099 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1100 if (kerr != KCGI_OK)
1102 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1103 if (kerr != KCGI_OK)
1106 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1107 "solid_line", KATTR__MAX);
1108 if (kerr != KCGI_OK)
1110 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1111 if (kerr != KCGI_OK)
1118 got_ref_list_free(&header->refs);
1119 gw_free_headers(header);
1120 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1121 gw_free_headers(n_header);
1125 if (error == NULL && kerr != KCGI_OK)
1126 error = gw_kcgi_error(kerr);
1130 static const struct got_error *
1131 gw_briefs(struct gw_trans *gw_trans)
1133 const struct got_error *error = NULL;
1134 struct gw_header *header = NULL, *n_header = NULL;
1135 char *age = NULL, *age_html = NULL;
1136 char *href_diff = NULL, *href_blob = NULL;
1137 char *newline, *smallerthan;
1138 enum kcgi_err kerr = KCGI_OK;
1140 if ((header = gw_init_header()) == NULL)
1141 return got_error_from_errno("malloc");
1143 if (pledge("stdio rpath proc exec sendfd unveil",
1145 error = got_error_from_errno("pledge");
1149 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1153 if (gw_trans->action == GW_SUMMARY)
1154 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1156 error = gw_get_header(gw_trans, header,
1157 gw_trans->gw_conf->got_max_commits_display);
1161 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1162 error = gw_get_time_str(&age, n_header->committer_time,
1167 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1168 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1169 if (kerr != KCGI_OK)
1172 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1173 KATTR_ID, "briefs_age", KATTR__MAX);
1174 if (kerr != KCGI_OK)
1176 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
1177 error = got_error_from_errno("asprintf");
1180 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
1181 if (kerr != KCGI_OK)
1183 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1184 if (kerr != KCGI_OK)
1187 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1188 KATTR_ID, "briefs_author", KATTR__MAX);
1189 if (kerr != KCGI_OK)
1191 smallerthan = strchr(n_header->author, '<');
1193 *smallerthan = '\0';
1194 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1195 if (kerr != KCGI_OK)
1197 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1198 if (kerr != KCGI_OK)
1201 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1202 gw_trans->repo_name, n_header->commit_id) == -1) {
1203 error = got_error_from_errno("asprintf");
1206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1207 KATTR_ID, "briefs_log", KATTR__MAX);
1208 if (kerr != KCGI_OK)
1210 newline = strchr(n_header->commit_msg, '\n');
1213 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1214 KATTR_HREF, href_diff, KATTR__MAX);
1215 if (kerr != KCGI_OK)
1217 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1218 if (kerr != KCGI_OK)
1220 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1221 if (kerr != KCGI_OK)
1224 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1225 KATTR_ID, "navs_wrapper", KATTR__MAX);
1226 if (kerr != KCGI_OK)
1228 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1229 KATTR_ID, "navs", KATTR__MAX);
1230 if (kerr != KCGI_OK)
1232 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1233 KATTR_HREF, href_diff, KATTR__MAX);
1234 if (kerr != KCGI_OK)
1236 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1237 if (kerr != KCGI_OK)
1239 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1240 if (kerr != KCGI_OK)
1243 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1244 if (kerr != KCGI_OK)
1247 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1248 gw_trans->repo_name, n_header->commit_id) == -1) {
1249 error = got_error_from_errno("asprintf");
1252 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1253 KATTR_HREF, href_blob, KATTR__MAX);
1254 if (kerr != KCGI_OK)
1256 khtml_puts(gw_trans->gw_html_req, "tree");
1257 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1258 if (kerr != KCGI_OK)
1260 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1261 if (kerr != KCGI_OK)
1264 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1265 KATTR_ID, "dotted_line", KATTR__MAX);
1266 if (kerr != KCGI_OK)
1268 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1269 if (kerr != KCGI_OK)
1282 got_ref_list_free(&header->refs);
1283 gw_free_headers(header);
1284 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1285 gw_free_headers(n_header);
1290 if (error == NULL && kerr != KCGI_OK)
1291 error = gw_kcgi_error(kerr);
1295 static const struct got_error *
1296 gw_summary(struct gw_trans *gw_trans)
1298 const struct got_error *error = NULL;
1300 enum kcgi_err kerr = KCGI_OK;
1302 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1303 return got_error_from_errno("pledge");
1305 /* unveil is applied with gw_briefs below */
1307 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1308 "summary_wrapper", KATTR__MAX);
1309 if (kerr != KCGI_OK)
1310 return gw_kcgi_error(kerr);
1312 if (gw_trans->gw_conf->got_show_repo_description &&
1313 gw_trans->gw_dir->description != NULL &&
1314 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1315 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1316 KATTR_ID, "description_title", KATTR__MAX);
1317 if (kerr != KCGI_OK)
1319 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1320 if (kerr != KCGI_OK)
1322 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1323 if (kerr != KCGI_OK)
1325 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1326 KATTR_ID, "description", KATTR__MAX);
1327 if (kerr != KCGI_OK)
1329 kerr = khtml_puts(gw_trans->gw_html_req,
1330 gw_trans->gw_dir->description);
1331 if (kerr != KCGI_OK)
1333 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1334 if (kerr != KCGI_OK)
1338 if (gw_trans->gw_conf->got_show_repo_owner &&
1339 gw_trans->gw_dir->owner != NULL) {
1340 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1341 KATTR_ID, "repo_owner_title", KATTR__MAX);
1342 if (kerr != KCGI_OK)
1344 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1345 if (kerr != KCGI_OK)
1347 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1348 if (kerr != KCGI_OK)
1350 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1351 KATTR_ID, "repo_owner", KATTR__MAX);
1352 if (kerr != KCGI_OK)
1354 kerr = khtml_puts(gw_trans->gw_html_req,
1355 gw_trans->gw_dir->owner);
1356 if (kerr != KCGI_OK)
1358 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1359 if (kerr != KCGI_OK)
1363 if (gw_trans->gw_conf->got_show_repo_age) {
1364 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1365 "refs/heads", TM_LONG);
1369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1370 KATTR_ID, "last_change_title", KATTR__MAX);
1371 if (kerr != KCGI_OK)
1373 kerr = khtml_puts(gw_trans->gw_html_req,
1375 if (kerr != KCGI_OK)
1377 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1378 if (kerr != KCGI_OK)
1380 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1381 KATTR_ID, "last_change", KATTR__MAX);
1382 if (kerr != KCGI_OK)
1384 kerr = khtml_puts(gw_trans->gw_html_req, age);
1385 if (kerr != KCGI_OK)
1387 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1388 if (kerr != KCGI_OK)
1393 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1394 gw_trans->gw_dir->url != NULL &&
1395 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1396 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1397 KATTR_ID, "cloneurl_title", KATTR__MAX);
1398 if (kerr != KCGI_OK)
1400 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1401 if (kerr != KCGI_OK)
1403 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1404 if (kerr != KCGI_OK)
1406 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1407 KATTR_ID, "cloneurl", KATTR__MAX);
1408 if (kerr != KCGI_OK)
1410 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1411 if (kerr != KCGI_OK)
1413 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1414 if (kerr != KCGI_OK)
1418 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1419 if (kerr != KCGI_OK)
1422 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1423 "briefs_title_wrapper", KATTR__MAX);
1424 if (kerr != KCGI_OK)
1426 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1427 "briefs_title", KATTR__MAX);
1428 if (kerr != KCGI_OK)
1430 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1431 if (kerr != KCGI_OK)
1433 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1434 if (kerr != KCGI_OK)
1436 error = gw_briefs(gw_trans);
1440 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1445 error = gw_output_repo_heads(gw_trans);
1448 if (error == NULL && kerr != KCGI_OK)
1449 error = gw_kcgi_error(kerr);
1453 static const struct got_error *
1454 gw_tree(struct gw_trans *gw_trans)
1456 const struct got_error *error = NULL;
1457 struct gw_header *header = NULL;
1458 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1459 char *age = NULL, *age_html = NULL;
1462 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1463 return got_error_from_errno("pledge");
1465 if ((header = gw_init_header()) == NULL)
1466 return got_error_from_errno("malloc");
1468 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1472 error = gw_get_header(gw_trans, header, 1);
1476 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1477 "tree_header_wrapper", KATTR__MAX);
1478 if (kerr != KCGI_OK)
1480 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1481 "tree_header", KATTR__MAX);
1482 if (kerr != KCGI_OK)
1484 error = gw_gen_tree_header(gw_trans, header->tree_id);
1487 error = gw_get_time_str(&age, header->committer_time,
1491 error = gw_gen_age_header(gw_trans, age ?age : "");
1494 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1497 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1498 if (kerr != KCGI_OK)
1500 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1501 "dotted_line", KATTR__MAX);
1502 if (kerr != KCGI_OK)
1504 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1505 if (kerr != KCGI_OK)
1508 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1509 "tree", KATTR__MAX);
1510 if (kerr != KCGI_OK)
1512 error = gw_output_repo_tree(gw_trans);
1516 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1518 got_ref_list_free(&header->refs);
1519 gw_free_headers(header);
1520 free(tree_html_disp);
1525 if (error == NULL && kerr != KCGI_OK)
1526 error = gw_kcgi_error(kerr);
1530 static const struct got_error *
1531 gw_tag(struct gw_trans *gw_trans)
1533 const struct got_error *error = NULL;
1534 struct gw_header *header = NULL;
1537 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1538 return got_error_from_errno("pledge");
1540 if ((header = gw_init_header()) == NULL)
1541 return got_error_from_errno("malloc");
1543 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1547 error = gw_get_header(gw_trans, header, 1);
1551 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1552 "tag_header_wrapper", KATTR__MAX);
1553 if (kerr != KCGI_OK)
1555 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1556 "tag_header", KATTR__MAX);
1557 if (kerr != KCGI_OK)
1559 error = gw_gen_commit_header(gw_trans, header->commit_id,
1563 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1566 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1567 if (kerr != KCGI_OK)
1569 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1570 "dotted_line", KATTR__MAX);
1571 if (kerr != KCGI_OK)
1573 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1574 if (kerr != KCGI_OK)
1577 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1578 "tree", KATTR__MAX);
1579 if (kerr != KCGI_OK)
1582 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1586 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1588 got_ref_list_free(&header->refs);
1589 gw_free_headers(header);
1590 if (error == NULL && kerr != KCGI_OK)
1591 error = gw_kcgi_error(kerr);
1595 static const struct got_error *
1596 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1598 const struct got_error *error = NULL;
1603 if (asprintf(&dir_test, "%s/%s/%s",
1604 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1605 GOTWEB_GIT_DIR) == -1)
1606 return got_error_from_errno("asprintf");
1608 dt = opendir(dir_test);
1612 gw_dir->path = strdup(dir_test);
1617 if (asprintf(&dir_test, "%s/%s/%s",
1618 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1619 GOTWEB_GOT_DIR) == -1)
1620 return got_error_from_errno("asprintf");
1622 dt = opendir(dir_test);
1627 error = got_error(GOT_ERR_NOT_GIT_REPO);
1631 if (asprintf(&dir_test, "%s/%s",
1632 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1633 return got_error_from_errno("asprintf");
1635 gw_dir->path = strdup(dir_test);
1637 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1641 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1644 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1645 "refs/heads", TM_DIFF);
1648 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1656 static const struct got_error *
1657 gw_load_got_paths(struct gw_trans *gw_trans)
1659 const struct got_error *error = NULL;
1661 struct dirent **sd_dent;
1662 struct gw_dir *gw_dir;
1664 unsigned int d_cnt, d_i;
1666 d = opendir(gw_trans->gw_conf->got_repos_path);
1668 error = got_error_from_errno2("opendir",
1669 gw_trans->gw_conf->got_repos_path);
1673 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1676 error = got_error_from_errno2("scandir",
1677 gw_trans->gw_conf->got_repos_path);
1681 for (d_i = 0; d_i < d_cnt; d_i++) {
1682 if (gw_trans->gw_conf->got_max_repos > 0 &&
1683 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1684 break; /* account for parent and self */
1686 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1687 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1690 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1691 return got_error_from_errno("gw_dir malloc");
1693 error = gw_load_got_path(gw_trans, gw_dir);
1694 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1701 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1702 !got_path_dir_is_empty(gw_dir->path)) {
1703 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1705 gw_trans->repos_total++;
1713 static const struct got_error *
1714 gw_parse_querystring(struct gw_trans *gw_trans)
1716 const struct got_error *error = NULL;
1718 struct gw_query_action *action = NULL;
1721 if (gw_trans->gw_req->fieldnmap[0]) {
1722 error = got_error_from_errno("bad parse");
1724 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1725 /* define gw_trans->repo_path */
1726 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1727 return got_error_from_errno("asprintf");
1729 if (asprintf(&gw_trans->repo_path, "%s/%s",
1730 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1731 return got_error_from_errno("asprintf");
1733 /* get action and set function */
1734 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1735 for (i = 0; i < nitems(gw_query_funcs); i++) {
1736 action = &gw_query_funcs[i];
1737 if (action->func_name == NULL)
1740 if (strcmp(action->func_name,
1741 p->parsed.s) == 0) {
1742 gw_trans->action = i;
1743 if (asprintf(&gw_trans->action_name,
1744 "%s", action->func_name) == -1)
1746 got_error_from_errno(
1755 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1756 if (asprintf(&gw_trans->commit, "%s",
1758 return got_error_from_errno("asprintf");
1760 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1761 if (asprintf(&gw_trans->repo_file, "%s",
1763 return got_error_from_errno("asprintf");
1765 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1766 if (asprintf(&gw_trans->repo_folder, "%s",
1768 return got_error_from_errno("asprintf");
1770 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1771 if (asprintf(&gw_trans->headref, "%s",
1773 return got_error_from_errno("asprintf");
1775 if (action == NULL) {
1776 error = got_error_from_errno("invalid action");
1779 if ((gw_trans->gw_dir =
1780 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1781 return got_error_from_errno("gw_dir malloc");
1783 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1787 gw_trans->action = GW_INDEX;
1789 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1790 gw_trans->page = p->parsed.i;
1795 static struct gw_dir *
1796 gw_init_gw_dir(char *dir)
1798 struct gw_dir *gw_dir;
1800 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1803 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1809 static const struct got_error *
1810 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1814 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1815 if (kerr != KCGI_OK)
1816 return gw_kcgi_error(kerr);
1817 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1819 if (kerr != KCGI_OK)
1820 return gw_kcgi_error(kerr);
1821 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1823 if (kerr != KCGI_OK)
1824 return gw_kcgi_error(kerr);
1825 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1827 if (kerr != KCGI_OK)
1828 return gw_kcgi_error(kerr);
1829 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1830 if (kerr != KCGI_OK)
1831 return gw_kcgi_error(kerr);
1832 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1834 if (kerr != KCGI_OK)
1835 return gw_kcgi_error(kerr);
1837 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1838 kerr = khttp_head(gw_trans->gw_req,
1839 kresps[KRESP_CONTENT_DISPOSITION],
1840 "attachment; filename=%s", gw_trans->repo_file);
1841 if (kerr != KCGI_OK)
1842 return gw_kcgi_error(kerr);
1845 kerr = khttp_body(gw_trans->gw_req);
1846 return gw_kcgi_error(kerr);
1849 static const struct got_error *
1850 gw_display_index(struct gw_trans *gw_trans)
1852 const struct got_error *error;
1855 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1859 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1860 if (kerr != KCGI_OK)
1861 return gw_kcgi_error(kerr);
1863 if (gw_trans->action != GW_BLOB) {
1864 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1865 gw_query_funcs[gw_trans->action].template);
1866 if (kerr != KCGI_OK) {
1867 khtml_close(gw_trans->gw_html_req);
1868 return gw_kcgi_error(kerr);
1872 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1876 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1878 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1881 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1883 khtml_puts(gw_trans->gw_html_req, err->msg);
1884 khtml_close(gw_trans->gw_html_req);
1888 gw_template(size_t key, void *arg)
1890 const struct got_error *error = NULL;
1892 struct gw_trans *gw_trans = arg;
1893 char *img_src = NULL;
1897 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1898 KATTR_CONTENT, "initial-scale=1.0, user-scalable=no",
1900 if (kerr != KCGI_OK)
1902 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1903 if (kerr != KCGI_OK)
1905 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1906 KATTR_CHARSET, "utf-8",
1908 if (kerr != KCGI_OK)
1910 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1911 if (kerr != KCGI_OK)
1913 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1914 KATTR_NAME, "msapplication-TileColor",
1915 KATTR_CONTENT, "#da532c", KATTR__MAX);
1916 if (kerr != KCGI_OK)
1918 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1919 if (kerr != KCGI_OK)
1921 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
1922 KATTR_NAME, "theme-color",
1923 KATTR_CONTENT, "#ffffff", KATTR__MAX);
1924 if (kerr != KCGI_OK)
1926 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1927 if (kerr != KCGI_OK)
1929 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1930 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
1931 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
1932 if (kerr != KCGI_OK)
1934 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1935 if (kerr != KCGI_OK)
1937 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1938 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1939 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
1940 if (kerr != KCGI_OK)
1942 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1943 if (kerr != KCGI_OK)
1945 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1946 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
1947 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
1948 if (kerr != KCGI_OK)
1950 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1951 if (kerr != KCGI_OK)
1953 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1954 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
1956 if (kerr != KCGI_OK)
1958 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1959 if (kerr != KCGI_OK)
1961 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1962 KATTR_REL, "mask-icon", KATTR_HREF,
1963 "/safari-pinned-tab.svg", KATTR__MAX);
1964 if (kerr != KCGI_OK)
1966 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1967 if (kerr != KCGI_OK)
1969 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
1970 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
1971 KATTR_HREF, "/gotweb.css", KATTR__MAX);
1972 if (kerr != KCGI_OK)
1974 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1975 if (kerr != KCGI_OK)
1979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1980 KATTR_ID, "got_link", KATTR__MAX);
1981 if (kerr != KCGI_OK)
1983 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1984 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1985 KATTR_TARGET, "_sotd", KATTR__MAX);
1986 if (kerr != KCGI_OK)
1988 if (asprintf(&img_src, "/%s",
1989 gw_trans->gw_conf->got_logo) == -1)
1991 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1992 KATTR_SRC, img_src, KATTR__MAX);
1993 if (kerr != KCGI_OK) {
1997 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1998 if (kerr != KCGI_OK) {
2003 case (TEMPL_SITEPATH):
2004 error = gw_output_site_link(gw_trans);
2009 if (gw_trans->gw_conf->got_site_name != NULL) {
2010 kerr = khtml_puts(gw_trans->gw_html_req,
2011 gw_trans->gw_conf->got_site_name);
2012 if (kerr != KCGI_OK)
2016 case (TEMPL_SEARCH):
2018 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2019 "search", KATTR__MAX);
2020 if (kerr != KCGI_OK)
2022 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2023 KATTR_METHOD, "POST", KATTR__MAX);
2024 if (kerr != KCGI_OK)
2026 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2027 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2028 KATTR_MAXLENGTH, "50", KATTR__MAX);
2029 if (kerr != KCGI_OK)
2031 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2033 if (kerr != KCGI_OK)
2035 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2036 if (kerr != KCGI_OK)
2038 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2039 if (kerr != KCGI_OK)
2042 case(TEMPL_SITEOWNER):
2043 if (gw_trans->gw_conf->got_site_owner != NULL &&
2044 gw_trans->gw_conf->got_show_site_owner) {
2045 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2046 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2047 if (kerr != KCGI_OK)
2049 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2050 KATTR_ID, "site_owner", KATTR__MAX);
2051 if (kerr != KCGI_OK)
2053 kerr = khtml_puts(gw_trans->gw_html_req,
2054 gw_trans->gw_conf->got_site_owner);
2055 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2056 if (kerr != KCGI_OK)
2060 case(TEMPL_CONTENT):
2061 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2063 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2064 KATTR_ID, "tmpl_err", KATTR__MAX);
2065 if (kerr != KCGI_OK)
2067 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
2068 if (kerr != KCGI_OK)
2070 kerr = khttp_puts(gw_trans->gw_req, error->msg);
2071 if (kerr != KCGI_OK)
2073 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2074 if (kerr != KCGI_OK)
2084 static const struct got_error *
2085 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2087 const struct got_error *error = NULL;
2088 char *ref_str = NULL;
2089 enum kcgi_err kerr = KCGI_OK;
2091 if (strcmp(str2, "") != 0) {
2092 if (asprintf(&ref_str, "(%s)", str2) == -1) {
2093 error = got_error_from_errno("asprintf");
2097 ref_str = strdup("");
2099 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2100 KATTR_ID, "header_commit_title", KATTR__MAX);
2101 if (kerr != KCGI_OK)
2103 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2104 if (kerr != KCGI_OK)
2106 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2107 if (kerr != KCGI_OK)
2109 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2110 KATTR_ID, "header_commit", KATTR__MAX);
2111 if (kerr != KCGI_OK)
2113 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2114 if (kerr != KCGI_OK)
2116 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2117 if (kerr != KCGI_OK)
2119 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
2120 if (kerr != KCGI_OK)
2122 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2124 if (error == NULL && kerr != KCGI_OK)
2125 error = gw_kcgi_error(kerr);
2129 static const struct got_error *
2130 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2132 const struct got_error *error = NULL;
2133 enum kcgi_err kerr = KCGI_OK;
2135 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2136 KATTR_ID, "header_diff_title", KATTR__MAX);
2137 if (kerr != KCGI_OK)
2139 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2140 if (kerr != KCGI_OK)
2142 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2143 if (kerr != KCGI_OK)
2145 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2146 KATTR_ID, "header_diff", KATTR__MAX);
2147 if (kerr != KCGI_OK)
2149 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2150 if (kerr != KCGI_OK)
2152 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2153 if (kerr != KCGI_OK)
2155 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2156 if (kerr != KCGI_OK)
2158 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2160 if (error == NULL && kerr != KCGI_OK)
2161 error = gw_kcgi_error(kerr);
2165 static const struct got_error *
2166 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2168 const struct got_error *error = NULL;
2169 enum kcgi_err kerr = KCGI_OK;
2171 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2172 KATTR_ID, "header_age_title", KATTR__MAX);
2173 if (kerr != KCGI_OK)
2175 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2176 if (kerr != KCGI_OK)
2178 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2179 if (kerr != KCGI_OK)
2181 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2182 KATTR_ID, "header_age", KATTR__MAX);
2183 if (kerr != KCGI_OK)
2185 kerr = khtml_puts(gw_trans->gw_html_req, str);
2186 if (kerr != KCGI_OK)
2188 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2190 if (error == NULL && kerr != KCGI_OK)
2191 error = gw_kcgi_error(kerr);
2195 static const struct got_error *
2196 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2198 const struct got_error *error = NULL;
2201 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2202 KATTR_ID, "header_author_title", KATTR__MAX);
2203 if (kerr != KCGI_OK)
2205 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2206 if (kerr != KCGI_OK)
2208 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2209 if (kerr != KCGI_OK)
2211 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2212 KATTR_ID, "header_author", KATTR__MAX);
2213 if (kerr != KCGI_OK)
2215 kerr = khtml_puts(gw_trans->gw_html_req, str);
2216 if (kerr != KCGI_OK)
2218 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2220 if (error == NULL && kerr != KCGI_OK)
2221 error = gw_kcgi_error(kerr);
2225 static const struct got_error *
2226 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2228 const struct got_error *error = NULL;
2231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2232 KATTR_ID, "header_committer_title", KATTR__MAX);
2233 if (kerr != KCGI_OK)
2235 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2236 if (kerr != KCGI_OK)
2238 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2239 if (kerr != KCGI_OK)
2241 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2242 KATTR_ID, "header_committer", KATTR__MAX);
2243 if (kerr != KCGI_OK)
2245 kerr = khtml_puts(gw_trans->gw_html_req, str);
2246 if (kerr != KCGI_OK)
2248 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2250 if (error == NULL && kerr != KCGI_OK)
2251 error = gw_kcgi_error(kerr);
2255 static const struct got_error *
2256 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2258 const struct got_error *error = NULL;
2261 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2262 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2263 if (kerr != KCGI_OK)
2265 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2266 if (kerr != KCGI_OK)
2268 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2269 if (kerr != KCGI_OK)
2271 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2272 KATTR_ID, "header_commit_msg", KATTR__MAX);
2273 if (kerr != KCGI_OK)
2275 kerr = khttp_puts(gw_trans->gw_req, str);
2276 if (kerr != KCGI_OK)
2278 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2280 if (error == NULL && kerr != KCGI_OK)
2281 error = gw_kcgi_error(kerr);
2285 static const struct got_error *
2286 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2288 const struct got_error *error = NULL;
2291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2292 KATTR_ID, "header_tree_title", KATTR__MAX);
2293 if (kerr != KCGI_OK)
2295 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2296 if (kerr != KCGI_OK)
2298 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2299 if (kerr != KCGI_OK)
2301 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2302 KATTR_ID, "header_tree", KATTR__MAX);
2303 if (kerr != KCGI_OK)
2305 kerr = khtml_puts(gw_trans->gw_html_req, str);
2306 if (kerr != KCGI_OK)
2308 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2310 if (error == NULL && kerr != KCGI_OK)
2311 error = gw_kcgi_error(kerr);
2315 static const struct got_error *
2316 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2319 const struct got_error *error = NULL;
2321 char *d_file = NULL;
2325 *description = NULL;
2326 if (gw_trans->gw_conf->got_show_repo_description == 0)
2327 return gw_empty_string(description);
2329 if (asprintf(&d_file, "%s/description", dir) == -1)
2330 return got_error_from_errno("asprintf");
2332 f = fopen(d_file, "r");
2334 if (errno == ENOENT || errno == EACCES)
2335 return gw_empty_string(description);
2336 error = got_error_from_errno2("fopen", d_file);
2340 if (fseek(f, 0, SEEK_END) == -1) {
2341 error = got_ferror(f, GOT_ERR_IO);
2346 error = got_ferror(f, GOT_ERR_IO);
2349 if (fseek(f, 0, SEEK_SET) == -1) {
2350 error = got_ferror(f, GOT_ERR_IO);
2353 *description = calloc(len + 1, sizeof(**description));
2354 if (*description == NULL) {
2355 error = got_error_from_errno("calloc");
2359 n = fread(*description, 1, len, f);
2360 if (n == 0 && ferror(f))
2361 error = got_ferror(f, GOT_ERR_IO);
2363 if (f != NULL && fclose(f) == -1 && error == NULL)
2364 error = got_error_from_errno("fclose");
2369 static const struct got_error *
2370 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2374 char *years = "years ago", *months = "months ago";
2375 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2376 char *minutes = "minutes ago", *seconds = "seconds ago";
2377 char *now = "right now";
2385 diff_time = time(NULL) - committer_time;
2386 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2387 if (asprintf(repo_age, "%lld %s",
2388 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2389 return got_error_from_errno("asprintf");
2390 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2391 if (asprintf(repo_age, "%lld %s",
2392 (diff_time / 60 / 60 / 24 / (365 / 12)),
2394 return got_error_from_errno("asprintf");
2395 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2396 if (asprintf(repo_age, "%lld %s",
2397 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2398 return got_error_from_errno("asprintf");
2399 } else if (diff_time > 60 * 60 * 24 * 2) {
2400 if (asprintf(repo_age, "%lld %s",
2401 (diff_time / 60 / 60 / 24), days) == -1)
2402 return got_error_from_errno("asprintf");
2403 } else if (diff_time > 60 * 60 * 2) {
2404 if (asprintf(repo_age, "%lld %s",
2405 (diff_time / 60 / 60), hours) == -1)
2406 return got_error_from_errno("asprintf");
2407 } else if (diff_time > 60 * 2) {
2408 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2410 return got_error_from_errno("asprintf");
2411 } else if (diff_time > 2) {
2412 if (asprintf(repo_age, "%lld %s", diff_time,
2414 return got_error_from_errno("asprintf");
2416 if (asprintf(repo_age, "%s", now) == -1)
2417 return got_error_from_errno("asprintf");
2421 if (gmtime_r(&committer_time, &tm) == NULL)
2422 return got_error_from_errno("gmtime_r");
2424 s = asctime_r(&tm, datebuf);
2426 return got_error_from_errno("asctime_r");
2428 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2429 return got_error_from_errno("asprintf");
2435 static const struct got_error *
2436 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2437 char *repo_ref, int ref_tm)
2439 const struct got_error *error = NULL;
2440 struct got_object_id *id = NULL;
2441 struct got_repository *repo = NULL;
2442 struct got_commit_object *commit = NULL;
2443 struct got_reflist_head refs;
2444 struct got_reflist_entry *re;
2445 struct got_reference *head_ref;
2447 time_t committer_time = 0, cmp_time = 0;
2448 const char *refname;
2451 SIMPLEQ_INIT(&refs);
2453 if (repo_ref == NULL)
2456 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2459 if (gw_trans->gw_conf->got_show_repo_age == 0)
2462 error = got_repo_open(&repo, dir, NULL);
2467 error = got_ref_list(&refs, repo, "refs/heads",
2468 got_ref_cmp_by_name, NULL);
2470 error = got_ref_list(&refs, repo, repo_ref,
2471 got_ref_cmp_by_name, NULL);
2475 SIMPLEQ_FOREACH(re, &refs, entry) {
2477 refname = strdup(repo_ref);
2479 refname = got_ref_get_name(re->ref);
2480 error = got_ref_open(&head_ref, repo, refname, 0);
2484 error = got_ref_resolve(&id, repo, head_ref);
2485 got_ref_close(head_ref);
2489 error = got_object_open_as_commit(&commit, repo, id);
2494 got_object_commit_get_committer_time(commit);
2496 if (cmp_time < committer_time)
2497 cmp_time = committer_time;
2500 if (cmp_time != 0) {
2501 committer_time = cmp_time;
2502 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2505 got_ref_list_free(&refs);
2510 static const struct got_error *
2511 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2513 const struct got_error *error;
2515 struct got_object_id *id1 = NULL, *id2 = NULL;
2516 char *label1 = NULL, *label2 = NULL, *line = NULL;
2518 size_t linesize = 0;
2520 enum kcgi_err kerr = KCGI_OK;
2526 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2530 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2531 error = got_repo_match_object_id(&id1, &label1,
2532 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2537 error = got_repo_match_object_id(&id2, &label2,
2538 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2542 error = got_object_get_type(&obj_type, header->repo, id2);
2546 case GOT_OBJ_TYPE_BLOB:
2547 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2550 case GOT_OBJ_TYPE_TREE:
2551 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2554 case GOT_OBJ_TYPE_COMMIT:
2555 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2559 error = got_error(GOT_ERR_OBJ_TYPE);
2564 if (fseek(f, 0, SEEK_SET) == -1) {
2565 error = got_ferror(f, GOT_ERR_IO);
2569 while ((linelen = getline(&line, &linesize, f)) != -1) {
2570 error = gw_colordiff_line(gw_trans, line);
2573 /* XXX: KHTML_PRETTY breaks this */
2574 kerr = khtml_puts(gw_trans->gw_html_req, line);
2575 if (kerr != KCGI_OK)
2577 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2578 if (kerr != KCGI_OK)
2581 if (linelen == -1 && ferror(f))
2582 error = got_error_from_errno("getline");
2584 if (f && fclose(f) == -1 && error == NULL)
2585 error = got_error_from_errno("fclose");
2592 if (error == NULL && kerr != KCGI_OK)
2593 error = gw_kcgi_error(kerr);
2597 static const struct got_error *
2598 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2600 const struct got_error *error = NULL;
2601 struct got_repository *repo;
2602 const char *gitconfig_owner;
2606 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2609 error = got_repo_open(&repo, dir, NULL);
2612 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2613 if (gitconfig_owner) {
2614 *owner = strdup(gitconfig_owner);
2616 error = got_error_from_errno("strdup");
2618 got_repo_close(repo);
2622 static const struct got_error *
2623 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2625 const struct got_error *error = NULL;
2627 char *d_file = NULL;
2633 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2634 return got_error_from_errno("asprintf");
2636 f = fopen(d_file, "r");
2638 if (errno != ENOENT && errno != EACCES)
2639 error = got_error_from_errno2("fopen", d_file);
2643 if (fseek(f, 0, SEEK_END) == -1) {
2644 error = got_ferror(f, GOT_ERR_IO);
2649 error = got_ferror(f, GOT_ERR_IO);
2652 if (fseek(f, 0, SEEK_SET) == -1) {
2653 error = got_ferror(f, GOT_ERR_IO);
2657 *url = calloc(len + 1, sizeof(**url));
2659 error = got_error_from_errno("calloc");
2663 n = fread(*url, 1, len, f);
2664 if (n == 0 && ferror(f))
2665 error = got_ferror(f, GOT_ERR_IO);
2667 if (f && fclose(f) == -1 && error == NULL)
2668 error = got_error_from_errno("fclose");
2673 static const struct got_error *
2674 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2675 int limit, int tag_type)
2677 const struct got_error *error = NULL;
2678 struct got_repository *repo = NULL;
2679 struct got_reflist_head refs;
2680 struct got_reflist_entry *re;
2682 char *id_str = NULL, *newline, *href_commits = NULL;
2683 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2684 struct got_tag_object *tag = NULL;
2685 enum kcgi_err kerr = KCGI_OK;
2686 int summary_header_displayed = 0;
2688 SIMPLEQ_INIT(&refs);
2690 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2694 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2698 SIMPLEQ_FOREACH(re, &refs, entry) {
2699 const char *refname;
2701 const char *tag_commit;
2703 struct got_object_id *id;
2704 struct got_commit_object *commit = NULL;
2706 refname = got_ref_get_name(re->ref);
2707 if (strncmp(refname, "refs/tags/", 10) != 0)
2711 error = got_ref_resolve(&id, repo, re->ref);
2715 error = got_object_open_as_tag(&tag, repo, id);
2717 if (error->code != GOT_ERR_OBJ_TYPE) {
2721 /* "lightweight" tag */
2722 error = got_object_open_as_commit(&commit, repo, id);
2727 tagger = got_object_commit_get_committer(commit);
2729 got_object_commit_get_committer_time(commit);
2730 error = got_object_id_str(&id_str, id);
2736 tagger = got_object_tag_get_tagger(tag);
2737 tagger_time = got_object_tag_get_tagger_time(tag);
2738 error = got_object_id_str(&id_str,
2739 got_object_tag_get_object_id(tag));
2746 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2747 strlen(id_str)) != 0)
2751 error = got_object_commit_get_logmsg(&tag_commit0,
2755 got_object_commit_close(commit);
2757 tag_commit0 = strdup(got_object_tag_get_message(tag));
2758 if (tag_commit0 == NULL) {
2759 error = got_error_from_errno("strdup");
2764 tag_commit = tag_commit0;
2765 while (*tag_commit == '\n')
2770 newline = strchr(tag_commit, '\n');
2774 if (summary_header_displayed == 0) {
2775 kerr = khtml_attr(gw_trans->gw_html_req,
2776 KELEM_DIV, KATTR_ID,
2777 "summary_tags_title_wrapper", KATTR__MAX);
2778 if (kerr != KCGI_OK)
2780 kerr = khtml_attr(gw_trans->gw_html_req,
2781 KELEM_DIV, KATTR_ID,
2782 "summary_tags_title", KATTR__MAX);
2783 if (kerr != KCGI_OK)
2785 kerr = khtml_puts(gw_trans->gw_html_req,
2787 if (kerr != KCGI_OK)
2789 kerr = khtml_closeelem(gw_trans->gw_html_req,
2791 if (kerr != KCGI_OK)
2793 kerr = khtml_attr(gw_trans->gw_html_req,
2794 KELEM_DIV, KATTR_ID,
2795 "summary_tags_content", KATTR__MAX);
2796 if (kerr != KCGI_OK)
2798 summary_header_displayed = 1;
2801 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2802 KATTR_ID, "tags_wrapper", KATTR__MAX);
2803 if (kerr != KCGI_OK)
2805 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2806 KATTR_ID, "tags_age", KATTR__MAX);
2807 if (kerr != KCGI_OK)
2809 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2812 kerr = khtml_puts(gw_trans->gw_html_req,
2814 if (kerr != KCGI_OK)
2816 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2817 if (kerr != KCGI_OK)
2819 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2820 KATTR_ID, "tags", KATTR__MAX);
2821 if (kerr != KCGI_OK)
2823 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2824 if (kerr != KCGI_OK)
2826 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2827 if (kerr != KCGI_OK)
2829 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2830 KATTR_ID, "tags_name", KATTR__MAX);
2831 if (kerr != KCGI_OK)
2833 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2834 gw_trans->repo_name, id_str) == -1) {
2835 error = got_error_from_errno("asprintf");
2838 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2839 KATTR_HREF, href_tag, KATTR__MAX);
2840 if (kerr != KCGI_OK)
2842 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2843 if (kerr != KCGI_OK)
2845 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2846 if (kerr != KCGI_OK)
2849 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2850 KATTR_ID, "navs_wrapper", KATTR__MAX);
2851 if (kerr != KCGI_OK)
2853 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2854 KATTR_ID, "navs", KATTR__MAX);
2855 if (kerr != KCGI_OK)
2858 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2859 KATTR_HREF, href_tag, KATTR__MAX);
2860 if (kerr != KCGI_OK)
2862 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2863 if (kerr != KCGI_OK)
2865 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2866 if (kerr != KCGI_OK)
2869 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2870 if (kerr != KCGI_OK)
2872 if (asprintf(&href_briefs,
2873 "?path=%s&action=briefs&commit=%s",
2874 gw_trans->repo_name, id_str) == -1) {
2875 error = got_error_from_errno("asprintf");
2878 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2879 KATTR_HREF, href_briefs, KATTR__MAX);
2880 if (kerr != KCGI_OK)
2882 kerr = khtml_puts(gw_trans->gw_html_req,
2884 if (kerr != KCGI_OK)
2886 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2887 if (kerr != KCGI_OK)
2890 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
2891 if (kerr != KCGI_OK)
2894 if (asprintf(&href_commits,
2895 "?path=%s&action=commits&commit=%s",
2896 gw_trans->repo_name, id_str) == -1) {
2897 error = got_error_from_errno("asprintf");
2900 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2901 KATTR_HREF, href_commits, KATTR__MAX);
2902 if (kerr != KCGI_OK)
2904 kerr = khtml_puts(gw_trans->gw_html_req,
2906 if (kerr != KCGI_OK)
2908 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2909 if (kerr != KCGI_OK)
2912 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2913 KATTR_ID, "dotted_line", KATTR__MAX);
2914 if (kerr != KCGI_OK)
2916 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2917 if (kerr != KCGI_OK)
2921 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2922 KATTR_ID, "tag_info_date_title", KATTR__MAX);
2923 if (kerr != KCGI_OK)
2925 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
2926 if (kerr != KCGI_OK)
2928 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2929 if (kerr != KCGI_OK)
2931 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2932 KATTR_ID, "tag_info_date", KATTR__MAX);
2933 if (kerr != KCGI_OK)
2935 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2938 kerr = khtml_puts(gw_trans->gw_html_req,
2940 if (kerr != KCGI_OK)
2942 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2943 if (kerr != KCGI_OK)
2946 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2947 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
2948 if (kerr != KCGI_OK)
2950 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
2951 if (kerr != KCGI_OK)
2953 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2954 if (kerr != KCGI_OK)
2956 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2957 KATTR_ID, "tag_info_date", KATTR__MAX);
2958 if (kerr != KCGI_OK)
2960 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
2961 if (kerr != KCGI_OK)
2963 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2964 if (kerr != KCGI_OK)
2967 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2968 KATTR_ID, "tag_info", KATTR__MAX);
2969 if (kerr != KCGI_OK)
2971 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
2972 if (kerr != KCGI_OK)
2978 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2979 if (kerr != KCGI_OK)
2982 if (limit && --limit == 0)
2986 got_object_tag_close(tag);
2999 href_commits = NULL;
3003 got_object_tag_close(tag);
3010 got_ref_list_free(&refs);
3012 got_repo_close(repo);
3013 if (error == NULL && kerr != KCGI_OK)
3014 error = gw_kcgi_error(kerr);
3019 gw_free_headers(struct gw_header *header)
3023 if (header->commit != NULL)
3024 got_object_commit_close(header->commit);
3026 got_repo_close(header->repo);
3027 free(header->refs_str);
3028 free(header->commit_id);
3029 free(header->parent_id);
3030 free(header->tree_id);
3031 free(header->commit_msg);
3034 static struct gw_header *
3037 struct gw_header *header;
3039 header = malloc(sizeof(*header));
3043 header->repo = NULL;
3044 header->commit = NULL;
3046 header->path = NULL;
3047 SIMPLEQ_INIT(&header->refs);
3052 static const struct got_error *
3053 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3056 const struct got_error *error = NULL;
3057 struct got_commit_graph *graph = NULL;
3059 error = got_commit_graph_open(&graph, header->path, 0);
3063 error = got_commit_graph_iter_start(graph, header->id, header->repo,
3069 error = got_commit_graph_iter_next(&header->id, graph,
3070 header->repo, NULL, NULL);
3072 if (error->code == GOT_ERR_ITER_COMPLETED)
3076 if (header->id == NULL)
3079 error = got_object_open_as_commit(&header->commit, header->repo,
3084 error = gw_get_commit(gw_trans, header);
3086 struct gw_header *n_header = NULL;
3087 if ((n_header = gw_init_header()) == NULL) {
3088 error = got_error_from_errno("malloc");
3092 n_header->refs_str = strdup(header->refs_str);
3093 n_header->commit_id = strdup(header->commit_id);
3094 n_header->parent_id = strdup(header->parent_id);
3095 n_header->tree_id = strdup(header->tree_id);
3096 n_header->author = strdup(header->author);
3097 n_header->committer = strdup(header->committer);
3098 n_header->commit_msg = strdup(header->commit_msg);
3099 n_header->committer_time = header->committer_time;
3100 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3103 if (error || (limit && --limit == 0))
3108 got_commit_graph_close(graph);
3112 static const struct got_error *
3113 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
3115 const struct got_error *error = NULL;
3116 struct got_reflist_entry *re;
3117 struct got_object_id *id2 = NULL;
3118 struct got_object_qid *parent_id;
3119 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
3122 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3125 struct got_tag_object *tag = NULL;
3128 name = got_ref_get_name(re->ref);
3129 if (strcmp(name, GOT_REF_HEAD) == 0)
3131 if (strncmp(name, "refs/", 5) == 0)
3133 if (strncmp(name, "got/", 4) == 0)
3135 if (strncmp(name, "heads/", 6) == 0)
3137 if (strncmp(name, "remotes/", 8) == 0)
3139 if (strncmp(name, "tags/", 5) == 0) {
3140 error = got_object_open_as_tag(&tag, header->repo,
3143 if (error->code != GOT_ERR_OBJ_TYPE)
3146 * Ref points at something other
3153 cmp = got_object_id_cmp(tag ?
3154 got_object_tag_get_object_id(tag) : re->id, header->id);
3156 got_object_tag_close(tag);
3160 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
3161 s ? ", " : "", name) == -1) {
3162 error = got_error_from_errno("asprintf");
3166 header->refs_str = strdup(refs_str);
3170 if (refs_str == NULL)
3171 header->refs_str = strdup("");
3174 error = got_object_id_str(&header->commit_id, header->id);
3178 error = got_object_id_str(&header->tree_id,
3179 got_object_commit_get_tree_id(header->commit));
3183 if (gw_trans->action == GW_DIFF) {
3184 parent_id = SIMPLEQ_FIRST(
3185 got_object_commit_get_parent_ids(header->commit));
3186 if (parent_id != NULL) {
3187 id2 = got_object_id_dup(parent_id->id);
3189 error = got_object_id_str(&header->parent_id, id2);
3194 header->parent_id = strdup("/dev/null");
3196 header->parent_id = strdup("");
3198 header->committer_time =
3199 got_object_commit_get_committer_time(header->commit);
3202 got_object_commit_get_author(header->commit);
3204 got_object_commit_get_committer(header->commit);
3207 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
3211 commit_msg = commit_msg0;
3212 while (*commit_msg == '\n')
3215 header->commit_msg = strdup(commit_msg);
3220 static const struct got_error *
3221 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3223 const struct got_error *error = NULL;
3224 char *in_repo_path = NULL;
3226 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
3230 if (gw_trans->commit == NULL) {
3231 struct got_reference *head_ref;
3232 error = got_ref_open(&head_ref, header->repo,
3233 gw_trans->headref, 0);
3237 error = got_ref_resolve(&header->id, header->repo, head_ref);
3238 got_ref_close(head_ref);
3242 error = got_object_open_as_commit(&header->commit,
3243 header->repo, header->id);
3245 struct got_reference *ref;
3246 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
3247 if (error == NULL) {
3249 error = got_ref_resolve(&header->id, header->repo, ref);
3253 error = got_object_get_type(&obj_type, header->repo,
3257 if (obj_type == GOT_OBJ_TYPE_TAG) {
3258 struct got_tag_object *tag;
3259 error = got_object_open_as_tag(&tag,
3260 header->repo, header->id);
3263 if (got_object_tag_get_object_type(tag) !=
3264 GOT_OBJ_TYPE_COMMIT) {
3265 got_object_tag_close(tag);
3266 error = got_error(GOT_ERR_OBJ_TYPE);
3270 header->id = got_object_id_dup(
3271 got_object_tag_get_object_id(tag));
3272 if (header->id == NULL)
3273 error = got_error_from_errno(
3274 "got_object_id_dup");
3275 got_object_tag_close(tag);
3278 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3279 error = got_error(GOT_ERR_OBJ_TYPE);
3282 error = got_object_open_as_commit(&header->commit,
3283 header->repo, header->id);
3287 if (header->commit == NULL) {
3288 error = got_repo_match_object_id_prefix(&header->id,
3289 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3294 error = got_repo_match_object_id_prefix(&header->id,
3295 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
3299 error = got_repo_map_path(&in_repo_path, header->repo,
3300 gw_trans->repo_path, 1);
3305 header->path = strdup(in_repo_path);
3309 error = got_ref_list(&header->refs, header->repo, NULL,
3310 got_ref_cmp_by_name, NULL);
3314 error = gw_get_commits(gw_trans, header, limit);
3322 char datebuf[11]; /* YYYY-MM-DD + NUL */
3325 struct gw_blame_cb_args {
3326 struct blame_line *lines;
3330 off_t *line_offsets;
3332 struct got_repository *repo;
3333 struct gw_trans *gw_trans;
3336 static const struct got_error *
3337 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3339 const struct got_error *err = NULL;
3340 struct gw_blame_cb_args *a = arg;
3341 struct blame_line *bline;
3343 size_t linesize = 0;
3344 struct got_commit_object *commit = NULL;
3347 time_t committer_time;
3348 enum kcgi_err kerr = KCGI_OK;
3350 if (nlines != a->nlines ||
3351 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3352 return got_error(GOT_ERR_RANGE);
3355 return NULL; /* no change in this commit */
3357 /* Annotate this line. */
3358 bline = &a->lines[lineno - 1];
3359 if (bline->annotated)
3361 err = got_object_id_str(&bline->id_str, id);
3365 err = got_object_open_as_commit(&commit, a->repo, id);
3369 bline->committer = strdup(got_object_commit_get_committer(commit));
3370 if (bline->committer == NULL) {
3371 err = got_error_from_errno("strdup");
3375 committer_time = got_object_commit_get_committer_time(commit);
3376 if (localtime_r(&committer_time, &tm) == NULL)
3377 return got_error_from_errno("localtime_r");
3378 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3379 &tm) >= sizeof(bline->datebuf)) {
3380 err = got_error(GOT_ERR_NO_SPACE);
3383 bline->annotated = 1;
3385 /* Print lines annotated so far. */
3386 bline = &a->lines[a->lineno_cur - 1];
3387 if (!bline->annotated)
3390 offset = a->line_offsets[a->lineno_cur - 1];
3391 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3392 err = got_error_from_errno("fseeko");
3396 while (bline->annotated) {
3397 char *smallerthan, *at, *nl, *committer;
3398 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3401 if (getline(&line, &linesize, a->f) == -1) {
3403 err = got_error_from_errno("getline");
3407 committer = bline->committer;
3408 smallerthan = strchr(committer, '<');
3409 if (smallerthan && smallerthan[1] != '\0')
3410 committer = smallerthan + 1;
3411 at = strchr(committer, '@');
3414 len = strlen(committer);
3416 committer[8] = '\0';
3418 nl = strchr(line, '\n');
3422 if (a->gw_trans->repo_folder == NULL)
3423 a->gw_trans->repo_folder = strdup("");
3424 if (a->gw_trans->repo_folder == NULL)
3427 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3428 "blame_wrapper", KATTR__MAX);
3429 if (kerr != KCGI_OK)
3431 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3432 "blame_number", KATTR__MAX);
3433 if (kerr != KCGI_OK)
3435 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3436 a->lineno_cur) == -1)
3438 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3439 if (kerr != KCGI_OK)
3441 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3442 if (kerr != KCGI_OK)
3445 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3446 "blame_hash", KATTR__MAX);
3447 if (kerr != KCGI_OK)
3449 if (asprintf(&href_diff,
3450 "?path=%s&action=diff&commit=%s&file=%s&folder=%s",
3451 a->gw_trans->repo_name, bline->id_str,
3452 a->gw_trans->repo_file, a->gw_trans->repo_folder) == -1) {
3453 err = got_error_from_errno("asprintf");
3456 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3457 err = got_error_from_errno("asprintf");
3460 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3461 KATTR_HREF, href_diff, KATTR__MAX);
3462 if (kerr != KCGI_OK)
3464 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3465 if (kerr != KCGI_OK)
3467 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3468 if (kerr != KCGI_OK)
3471 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3472 "blame_date", KATTR__MAX);
3473 if (kerr != KCGI_OK)
3475 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3476 if (kerr != KCGI_OK)
3478 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3479 if (kerr != KCGI_OK)
3482 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3483 "blame_author", KATTR__MAX);
3484 if (kerr != KCGI_OK)
3486 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3487 if (kerr != KCGI_OK)
3489 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3490 if (kerr != KCGI_OK)
3493 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3494 "blame_code", KATTR__MAX);
3495 if (kerr != KCGI_OK)
3497 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3498 if (kerr != KCGI_OK)
3500 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3501 if (kerr != KCGI_OK)
3504 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3505 if (kerr != KCGI_OK)
3509 bline = &a->lines[a->lineno_cur - 1];
3517 got_object_commit_close(commit);
3519 if (err == NULL && kerr != KCGI_OK)
3520 err = gw_kcgi_error(kerr);
3524 static const struct got_error *
3525 gw_output_file_blame(struct gw_trans *gw_trans)
3527 const struct got_error *error = NULL;
3528 struct got_repository *repo = NULL;
3529 struct got_object_id *obj_id = NULL;
3530 struct got_object_id *commit_id = NULL;
3531 struct got_blob_object *blob = NULL;
3532 char *path = NULL, *in_repo_path = NULL;
3533 struct gw_blame_cb_args bca;
3537 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3541 if (asprintf(&path, "%s%s%s",
3542 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3543 gw_trans->repo_folder ? "/" : "",
3544 gw_trans->repo_file) == -1) {
3545 error = got_error_from_errno("asprintf");
3549 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3553 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3554 GOT_OBJ_TYPE_COMMIT, 1, repo);
3558 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3562 if (obj_id == NULL) {
3563 error = got_error(GOT_ERR_NO_OBJ);
3567 error = got_object_get_type(&obj_type, repo, obj_id);
3571 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3572 error = got_error(GOT_ERR_OBJ_TYPE);
3576 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3580 bca.f = got_opentemp();
3581 if (bca.f == NULL) {
3582 error = got_error_from_errno("got_opentemp");
3585 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3586 &bca.line_offsets, bca.f, blob);
3587 if (error || bca.nlines == 0)
3590 /* Don't include \n at EOF in the blame line count. */
3591 if (bca.line_offsets[bca.nlines - 1] == filesize)
3594 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3595 if (bca.lines == NULL) {
3596 error = got_error_from_errno("calloc");
3600 bca.nlines_prec = 0;
3607 bca.gw_trans = gw_trans;
3609 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3614 free(bca.line_offsets);
3620 for (i = 0; i < bca.nlines; i++) {
3621 struct blame_line *bline = &bca.lines[i];
3622 free(bline->id_str);
3623 free(bline->committer);
3626 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3627 error = got_error_from_errno("fclose");
3629 got_object_blob_close(blob);
3631 got_repo_close(repo);
3635 static const struct got_error *
3636 gw_output_blob_buf(struct gw_trans *gw_trans)
3638 const struct got_error *error = NULL;
3639 struct got_repository *repo = NULL;
3640 struct got_object_id *obj_id = NULL;
3641 struct got_object_id *commit_id = NULL;
3642 struct got_blob_object *blob = NULL;
3643 char *path = NULL, *in_repo_path = NULL;
3644 int obj_type, set_mime = 0;
3647 enum kcgi_err kerr = KCGI_OK;
3649 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3653 if (asprintf(&path, "%s%s%s",
3654 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3655 gw_trans->repo_folder ? "/" : "",
3656 gw_trans->repo_file) == -1) {
3657 error = got_error_from_errno("asprintf");
3661 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3665 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3666 GOT_OBJ_TYPE_COMMIT, 1, repo);
3670 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3674 if (obj_id == NULL) {
3675 error = got_error(GOT_ERR_NO_OBJ);
3679 error = got_object_get_type(&obj_type, repo, obj_id);
3683 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3684 error = got_error(GOT_ERR_OBJ_TYPE);
3688 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3692 hdrlen = got_object_blob_get_hdrlen(blob);
3694 error = got_object_blob_read_block(&len, blob);
3697 buf = got_object_blob_get_read_buf(blob);
3700 * Skip blob object header first time around,
3701 * which also contains a zero byte.
3704 if (set_mime == 0) {
3705 if (isbinary(buf, len - hdrlen))
3706 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3708 gw_trans->mime = KMIME_TEXT_PLAIN;
3710 error = gw_display_index(gw_trans);
3714 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3723 got_object_blob_close(blob);
3725 got_repo_close(repo);
3726 if (error == NULL && kerr != KCGI_OK)
3727 error = gw_kcgi_error(kerr);
3731 static const struct got_error *
3732 gw_output_repo_tree(struct gw_trans *gw_trans)
3734 const struct got_error *error = NULL;
3735 struct got_repository *repo = NULL;
3736 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3737 struct got_tree_object *tree = NULL;
3738 char *path = NULL, *in_repo_path = NULL;
3739 char *id_str = NULL;
3740 char *build_folder = NULL;
3741 char *href_blob = NULL, *href_blame = NULL;
3742 const char *class = NULL;
3743 int nentries, i, class_flip = 0;
3744 enum kcgi_err kerr = KCGI_OK;
3746 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3750 if (gw_trans->repo_folder != NULL)
3751 path = strdup(gw_trans->repo_folder);
3753 error = got_repo_map_path(&in_repo_path, repo,
3754 gw_trans->repo_path, 1);
3758 path = in_repo_path;
3761 if (gw_trans->commit == NULL) {
3762 struct got_reference *head_ref;
3763 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3766 error = got_ref_resolve(&commit_id, repo, head_ref);
3769 got_ref_close(head_ref);
3772 error = got_repo_match_object_id(&commit_id, NULL,
3773 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3778 error = got_object_id_str(&gw_trans->commit, commit_id);
3782 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3786 error = got_object_open_as_tree(&tree, repo, tree_id);
3790 nentries = got_object_tree_get_nentries(tree);
3791 for (i = 0; i < nentries; i++) {
3792 struct got_tree_entry *te;
3793 const char *modestr = "";
3796 te = got_object_tree_get_entry(tree, i);
3798 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3802 mode = got_tree_entry_get_mode(te);
3803 if (got_object_tree_entry_is_submodule(te))
3805 else if (S_ISLNK(mode))
3807 else if (S_ISDIR(mode))
3809 else if (mode & S_IXUSR)
3812 if (class_flip == 0) {
3813 class = "back_lightgray";
3816 class = "back_white";
3820 if (S_ISDIR(mode)) {
3821 if (asprintf(&build_folder, "%s/%s",
3822 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3823 got_tree_entry_get_name(te)) == -1) {
3824 error = got_error_from_errno(
3828 if (asprintf(&href_blob,
3829 "?path=%s&action=%s&commit=%s&folder=%s",
3830 gw_trans->repo_name, gw_trans->action_name,
3831 gw_trans->commit, build_folder) == -1) {
3832 error = got_error_from_errno("asprintf");
3836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3837 KATTR_ID, "tree_wrapper", KATTR__MAX);
3838 if (kerr != KCGI_OK)
3840 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3841 KATTR_ID, "tree_line", KATTR_CLASS, class,
3843 if (kerr != KCGI_OK)
3845 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3846 KATTR_HREF, href_blob, KATTR_CLASS,
3847 "diff_directory", KATTR__MAX);
3848 if (kerr != KCGI_OK)
3850 kerr = khtml_puts(gw_trans->gw_html_req,
3851 got_tree_entry_get_name(te));
3852 if (kerr != KCGI_OK)
3854 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3855 if (kerr != KCGI_OK)
3857 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3858 if (kerr != KCGI_OK)
3860 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3861 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
3863 if (kerr != KCGI_OK)
3865 kerr = khtml_entity(gw_trans->gw_html_req,
3867 if (kerr != KCGI_OK)
3869 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3870 if (kerr != KCGI_OK)
3873 if (asprintf(&href_blob,
3874 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3875 gw_trans->repo_name, "blob", gw_trans->commit,
3876 got_tree_entry_get_name(te),
3877 gw_trans->repo_folder ?
3878 gw_trans->repo_folder : "") == -1) {
3879 error = got_error_from_errno("asprintf");
3882 if (asprintf(&href_blame,
3883 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
3884 gw_trans->repo_name, "blame", gw_trans->commit,
3885 got_tree_entry_get_name(te),
3886 gw_trans->repo_folder ?
3887 gw_trans->repo_folder : "") == -1) {
3888 error = got_error_from_errno("asprintf");
3892 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3893 KATTR_ID, "tree_wrapper", KATTR__MAX);
3894 if (kerr != KCGI_OK)
3896 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3897 KATTR_ID, "tree_line", KATTR_CLASS, class,
3899 if (kerr != KCGI_OK)
3901 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3902 KATTR_HREF, href_blob, KATTR__MAX);
3903 if (kerr != KCGI_OK)
3905 kerr = khtml_puts(gw_trans->gw_html_req,
3906 got_tree_entry_get_name(te));
3907 if (kerr != KCGI_OK)
3909 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
3910 if (kerr != KCGI_OK)
3912 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3913 if (kerr != KCGI_OK)
3915 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3916 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
3918 if (kerr != KCGI_OK)
3921 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3922 KATTR_HREF, href_blob, KATTR__MAX);
3923 if (kerr != KCGI_OK)
3925 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
3926 if (kerr != KCGI_OK)
3928 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3929 if (kerr != KCGI_OK)
3932 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3933 if (kerr != KCGI_OK)
3936 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3937 KATTR_HREF, href_blame, KATTR__MAX);
3938 if (kerr != KCGI_OK)
3940 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
3941 if (kerr != KCGI_OK)
3944 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3945 if (kerr != KCGI_OK)
3953 build_folder = NULL;
3957 got_object_tree_close(tree);
3959 got_repo_close(repo);
3966 if (error == NULL && kerr != KCGI_OK)
3967 error = gw_kcgi_error(kerr);
3971 static const struct got_error *
3972 gw_output_repo_heads(struct gw_trans *gw_trans)
3974 const struct got_error *error = NULL;
3975 struct got_repository *repo = NULL;
3976 struct got_reflist_head refs;
3977 struct got_reflist_entry *re;
3978 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
3979 char *href_commits = NULL;
3980 enum kcgi_err kerr = KCGI_OK;
3982 SIMPLEQ_INIT(&refs);
3984 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3988 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3993 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3994 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
3995 if (kerr != KCGI_OK)
3997 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3998 KATTR_ID, "summary_heads_title", KATTR__MAX);
3999 if (kerr != KCGI_OK)
4001 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4002 if (kerr != KCGI_OK)
4004 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4005 if (kerr != KCGI_OK)
4007 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4008 KATTR_ID, "summary_heads_content", KATTR__MAX);
4009 if (kerr != KCGI_OK)
4012 SIMPLEQ_FOREACH(re, &refs, entry) {
4015 refname = strdup(got_ref_get_name(re->ref));
4016 if (refname == NULL) {
4017 error = got_error_from_errno("got_ref_to_str");
4021 if (strncmp(refname, "refs/heads/", 11) != 0) {
4026 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4031 if (strncmp(refname, "refs/heads/", 11) == 0)
4034 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4035 KATTR_ID, "heads_wrapper", KATTR__MAX);
4036 if (kerr != KCGI_OK)
4038 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4039 KATTR_ID, "heads_age", KATTR__MAX);
4040 if (kerr != KCGI_OK)
4042 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4043 if (kerr != KCGI_OK)
4045 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4046 if (kerr != KCGI_OK)
4048 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4049 KATTR_ID, "heads_space", KATTR__MAX);
4050 if (kerr != KCGI_OK)
4052 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4053 if (kerr != KCGI_OK)
4055 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4056 if (kerr != KCGI_OK)
4058 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4059 KATTR_ID, "head", KATTR__MAX);
4060 if (kerr != KCGI_OK)
4062 if (asprintf(&href_summary,
4063 "?path=%s&action=summary&headref=%s",
4064 gw_trans->repo_name, refname) == -1) {
4065 error = got_error_from_errno("asprintf");
4068 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4069 href_summary, KATTR__MAX);
4070 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4071 if (kerr != KCGI_OK)
4073 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4074 if (kerr != KCGI_OK)
4077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4078 "navs_wrapper", KATTR__MAX);
4079 if (kerr != KCGI_OK)
4081 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4082 "navs", KATTR__MAX);
4083 if (kerr != KCGI_OK)
4086 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4087 href_summary, KATTR__MAX);
4088 if (kerr != KCGI_OK)
4090 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4091 if (kerr != KCGI_OK)
4093 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4094 if (kerr != KCGI_OK)
4097 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4098 if (kerr != KCGI_OK)
4100 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4101 gw_trans->repo_name, refname) == -1) {
4102 error = got_error_from_errno("asprintf");
4105 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4106 href_briefs, KATTR__MAX);
4107 if (kerr != KCGI_OK)
4109 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4110 if (kerr != KCGI_OK)
4112 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4113 if (kerr != KCGI_OK)
4116 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4117 if (kerr != KCGI_OK)
4120 if (asprintf(&href_commits,
4121 "?path=%s&action=commits&headref=%s",
4122 gw_trans->repo_name, refname) == -1) {
4123 error = got_error_from_errno("asprintf");
4126 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4127 href_commits, KATTR__MAX);
4128 if (kerr != KCGI_OK)
4130 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4131 if (kerr != KCGI_OK)
4133 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4134 if (kerr != KCGI_OK)
4137 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4138 "dotted_line", KATTR__MAX);
4139 if (kerr != KCGI_OK)
4141 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4142 if (kerr != KCGI_OK)
4146 href_summary = NULL;
4150 href_commits = NULL;
4153 got_ref_list_free(&refs);
4158 got_repo_close(repo);
4162 static const struct got_error *
4163 gw_output_site_link(struct gw_trans *gw_trans)
4165 const struct got_error *error = NULL;
4166 char *href_summary = NULL;
4167 enum kcgi_err kerr = KCGI_OK;
4169 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4170 "site_link", KATTR__MAX);
4171 if (kerr != KCGI_OK)
4173 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4175 if (kerr != KCGI_OK)
4177 kerr = khtml_puts(gw_trans->gw_html_req,
4178 gw_trans->gw_conf->got_site_link);
4179 if (kerr != KCGI_OK)
4181 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4182 if (kerr != KCGI_OK)
4185 if (gw_trans->repo_name != NULL) {
4186 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4187 if (kerr != KCGI_OK)
4189 if (asprintf(&href_summary, "?path=%s&action=summary",
4190 gw_trans->repo_name) == -1)
4192 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4193 href_summary, KATTR__MAX);
4194 if (kerr != KCGI_OK)
4196 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4197 if (kerr != KCGI_OK)
4199 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4200 if (kerr != KCGI_OK)
4202 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4203 if (kerr != KCGI_OK)
4205 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->action_name);
4206 if (kerr != KCGI_OK)
4210 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4211 if (kerr != KCGI_OK)
4218 static const struct got_error *
4219 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4221 const struct got_error *error = NULL;
4223 enum kcgi_err kerr = KCGI_OK;
4225 if (strncmp(buf, "-", 1) == 0)
4226 color = "diff_minus";
4227 else if (strncmp(buf, "+", 1) == 0)
4228 color = "diff_plus";
4229 else if (strncmp(buf, "@@", 2) == 0)
4230 color = "diff_chunk_header";
4231 else if (strncmp(buf, "@@", 2) == 0)
4232 color = "diff_chunk_header";
4233 else if (strncmp(buf, "commit +", 8) == 0)
4234 color = "diff_meta";
4235 else if (strncmp(buf, "commit -", 8) == 0)
4236 color = "diff_meta";
4237 else if (strncmp(buf, "blob +", 6) == 0)
4238 color = "diff_meta";
4239 else if (strncmp(buf, "blob -", 6) == 0)
4240 color = "diff_meta";
4241 else if (strncmp(buf, "file +", 6) == 0)
4242 color = "diff_meta";
4243 else if (strncmp(buf, "file -", 6) == 0)
4244 color = "diff_meta";
4245 else if (strncmp(buf, "from:", 5) == 0)
4246 color = "diff_author";
4247 else if (strncmp(buf, "via:", 4) == 0)
4248 color = "diff_author";
4249 else if (strncmp(buf, "date:", 5) == 0)
4250 color = "diff_date";
4251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4252 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4253 if (error == NULL && kerr != KCGI_OK)
4254 error = gw_kcgi_error(kerr);
4259 main(int argc, char *argv[])
4261 const struct got_error *error = NULL;
4262 struct gw_trans *gw_trans;
4263 struct gw_dir *dir = NULL, *tdir;
4264 const char *page = "index";
4268 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4271 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4274 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4277 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4280 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4281 if (kerr != KCGI_OK) {
4282 error = gw_kcgi_error(kerr);
4286 if ((gw_trans->gw_conf =
4287 malloc(sizeof(struct gotweb_conf))) == NULL) {
4289 error = got_error_from_errno("malloc");
4293 TAILQ_INIT(&gw_trans->gw_dirs);
4294 TAILQ_INIT(&gw_trans->gw_headers);
4297 gw_trans->repos_total = 0;
4298 gw_trans->repo_path = NULL;
4299 gw_trans->commit = NULL;
4300 gw_trans->headref = strdup(GOT_REF_HEAD);
4301 gw_trans->mime = KMIME_TEXT_HTML;
4302 gw_trans->gw_tmpl->key = gw_templs;
4303 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4304 gw_trans->gw_tmpl->arg = gw_trans;
4305 gw_trans->gw_tmpl->cb = gw_template;
4306 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4310 error = gw_parse_querystring(gw_trans);
4314 if (gw_trans->action == GW_BLOB)
4315 error = gw_blob(gw_trans);
4317 error = gw_display_index(gw_trans);
4320 gw_trans->mime = KMIME_TEXT_PLAIN;
4321 gw_trans->action = GW_ERR;
4322 gw_display_error(gw_trans, error);
4325 free(gw_trans->gw_conf->got_repos_path);
4326 free(gw_trans->gw_conf->got_site_name);
4327 free(gw_trans->gw_conf->got_site_owner);
4328 free(gw_trans->gw_conf->got_site_link);
4329 free(gw_trans->gw_conf->got_logo);
4330 free(gw_trans->gw_conf->got_logo_url);
4331 free(gw_trans->gw_conf);
4332 free(gw_trans->commit);
4333 free(gw_trans->repo_path);
4334 free(gw_trans->repo_name);
4335 free(gw_trans->repo_file);
4336 free(gw_trans->action_name);
4337 free(gw_trans->headref);
4339 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4341 free(dir->description);
4350 khttp_free(gw_trans->gw_req);