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>
35 #include <got_error.h>
36 #include <got_object.h>
37 #include <got_reference.h>
38 #include <got_repository.h>
40 #include <got_cancel.h>
41 #include <got_worktree.h>
43 #include <got_commit_graph.h>
44 #include <got_blame.h>
45 #include <got_privsep.h>
46 #include <got_opentemp.h>
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 TAILQ_HEAD(headers, gw_header) gw_headers;
59 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
60 struct got_repository *repo;
61 struct gw_dir *gw_dir;
62 struct gotweb_config *gw_conf;
63 struct ktemplate *gw_tmpl;
64 struct khtmlreq *gw_html_req;
66 const struct got_error *error;
67 const char *repo_name;
72 const char *repo_file;
77 unsigned int repos_total;
83 TAILQ_ENTRY(gw_header) entry;
84 struct got_reflist_head refs;
88 char *commit_id; /* id_str1 */
89 char *parent_id; /* id_str2 */
94 time_t committer_time;
98 TAILQ_ENTRY(gw_dir) entry;
140 static const char *const gw_templs[TEMPL__MAX] = {
150 static const struct kvalid gw_keys[KEY__ZMAX] = {
151 { kvalid_stringne, "action" },
152 { kvalid_stringne, "commit" },
153 { kvalid_stringne, "file" },
154 { kvalid_stringne, "folder" },
155 { kvalid_stringne, "headref" },
156 { kvalid_int, "page" },
157 { kvalid_stringne, "path" },
158 { kvalid_stringne, "prev" },
161 static struct gw_header *gw_init_header(void);
163 static void gw_free_header(struct gw_header *);
165 static int gw_template(size_t, void *);
167 static const struct got_error *gw_error(struct gw_trans *);
168 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
169 static const struct got_error *gw_get_repo_description(char **,
170 struct gw_trans *, char *);
171 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
173 static const struct got_error *gw_get_time_str(char **, time_t, int);
174 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
175 char *, const char *, int);
176 static const struct got_error *gw_output_file_blame(struct gw_trans *,
178 static const struct got_error *gw_output_blob_buf(struct gw_trans *,
180 static const struct got_error *gw_output_repo_tree(struct gw_trans *,
182 static const struct got_error *gw_output_diff(struct gw_trans *,
184 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
185 struct gw_header *, int, int);
186 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
187 static const struct got_error *gw_output_site_link(struct gw_trans *);
188 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
190 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
192 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
194 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
196 static const struct got_error *gw_gen_author_header(struct gw_trans *,
198 static const struct got_error *gw_gen_age_header(struct gw_trans *,
200 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
202 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
204 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
205 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
207 static const struct got_error *gw_display_index(struct gw_trans *);
208 static const struct got_error *gw_get_header(struct gw_trans *,
209 struct gw_header *, int);
210 static const struct got_error *gw_get_commits(struct gw_trans *,
211 struct gw_header *, int,
212 struct got_object_id *);
213 static const struct got_error *gw_get_commit(struct gw_trans *,
215 struct got_commit_object *,
216 struct got_object_id *);
217 static const struct got_error *gw_apply_unveil(const char *);
218 static const struct got_error *gw_blame_cb(void *, int, int,
219 struct got_commit_object *,
220 struct got_object_id *);
221 static const struct got_error *gw_load_got_paths(struct gw_trans *);
222 static const struct got_error *gw_load_got_path(struct gw_trans *,
224 static const struct got_error *gw_parse_querystring(struct gw_trans *);
225 static const struct got_error *gw_blame(struct gw_trans *);
226 static const struct got_error *gw_blob(struct gw_trans *);
227 static const struct got_error *gw_diff(struct gw_trans *);
228 static const struct got_error *gw_index(struct gw_trans *);
229 static const struct got_error *gw_commits(struct gw_trans *);
230 static const struct got_error *gw_briefs(struct gw_trans *);
231 static const struct got_error *gw_summary(struct gw_trans *);
232 static const struct got_error *gw_tree(struct gw_trans *);
233 static const struct got_error *gw_tag(struct gw_trans *);
234 static const struct got_error *gw_tags(struct gw_trans *);
236 struct gw_query_action {
237 unsigned int func_id;
238 const char *func_name;
239 const struct got_error *(*func_main)(struct gw_trans *);
240 const char *template;
243 enum gw_query_actions {
257 static const struct gw_query_action gw_query_funcs[] = {
258 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
259 { GW_BLOB, "blob", NULL, NULL },
260 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
261 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
262 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
263 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
264 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
265 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
266 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
267 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
268 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
272 gw_get_action_name(struct gw_trans *gw_trans)
274 return gw_query_funcs[gw_trans->action].func_name;
277 static const struct got_error *
278 gw_kcgi_error(enum kcgi_err kerr)
283 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
284 return got_error(GOT_ERR_CANCELLED);
286 if (kerr == KCGI_ENOMEM)
287 return got_error_set_errno(ENOMEM,
288 kcgi_strerror(kerr));
290 if (kerr == KCGI_ENFILE)
291 return got_error_set_errno(ENFILE,
292 kcgi_strerror(kerr));
294 if (kerr == KCGI_EAGAIN)
295 return got_error_set_errno(EAGAIN,
296 kcgi_strerror(kerr));
298 if (kerr == KCGI_FORM)
299 return got_error_msg(GOT_ERR_IO,
300 kcgi_strerror(kerr));
302 return got_error_from_errno(kcgi_strerror(kerr));
305 static const struct got_error *
306 gw_apply_unveil(const char *repo_path)
308 const struct got_error *err;
311 if (unveil("gmon.out", "rwc") != 0)
312 return got_error_from_errno2("unveil", "gmon.out");
314 if (repo_path && unveil(repo_path, "r") != 0)
315 return got_error_from_errno2("unveil", repo_path);
317 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
318 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
320 err = got_privsep_unveil_exec_helpers();
324 if (unveil(NULL, NULL) != 0)
325 return got_error_from_errno("unveil");
331 isbinary(const uint8_t *buf, size_t n)
335 for (i = 0; i < n; i++)
341 static const struct got_error *
342 gw_blame(struct gw_trans *gw_trans)
344 const struct got_error *error = NULL;
345 struct gw_header *header = NULL;
347 enum kcgi_err kerr = KCGI_OK;
350 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
352 return got_error_from_errno("pledge");
354 if ((header = gw_init_header()) == NULL)
355 return got_error_from_errno("malloc");
357 error = gw_apply_unveil(gw_trans->gw_dir->path);
361 /* check querystring */
362 if (gw_trans->repo_file == NULL) {
363 error = got_error_msg(GOT_ERR_QUERYSTRING,
364 "file required in querystring");
367 if (gw_trans->commit_id == NULL) {
368 error = got_error_msg(GOT_ERR_QUERYSTRING,
369 "commit required in querystring");
373 error = gw_get_header(gw_trans, header, 1);
376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
377 "blame_header_wrapper", KATTR__MAX);
380 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
381 "blame_header", KATTR__MAX);
384 error = gw_get_time_str(&age, header->committer_time,
388 error = gw_gen_age_header(gw_trans, age ?age : "");
391 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
394 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
397 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
398 "dotted_line", KATTR__MAX);
401 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
405 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
406 "blame", KATTR__MAX);
409 error = gw_output_file_blame(gw_trans, header);
412 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
414 gw_free_header(header);
415 if (error == NULL && kerr != KCGI_OK)
416 error = gw_kcgi_error(kerr);
420 static const struct got_error *
421 gw_blob(struct gw_trans *gw_trans)
423 const struct got_error *error = NULL, *err = NULL;
424 struct gw_header *header = NULL;
425 enum kcgi_err kerr = KCGI_OK;
428 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
430 return got_error_from_errno("pledge");
432 if ((header = gw_init_header()) == NULL)
433 return got_error_from_errno("malloc");
435 error = gw_apply_unveil(gw_trans->gw_dir->path);
439 /* check querystring */
440 if (gw_trans->repo_file == NULL) {
441 error = got_error_msg(GOT_ERR_QUERYSTRING,
442 "file required in querystring");
445 if (gw_trans->commit_id == NULL) {
446 error = got_error_msg(GOT_ERR_QUERYSTRING,
447 "commit required in querystring");
450 error = gw_get_header(gw_trans, header, 1);
454 error = gw_output_blob_buf(gw_trans, header);
457 gw_trans->mime = KMIME_TEXT_PLAIN;
458 err = gw_display_index(gw_trans);
463 kerr = khttp_puts(gw_trans->gw_req, error->msg);
466 gw_free_header(header);
467 if (error == NULL && kerr != KCGI_OK)
468 error = gw_kcgi_error(kerr);
472 static const struct got_error *
473 gw_diff(struct gw_trans *gw_trans)
475 const struct got_error *error = NULL;
476 struct gw_header *header = NULL;
478 enum kcgi_err kerr = KCGI_OK;
481 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
483 return got_error_from_errno("pledge");
485 if ((header = gw_init_header()) == NULL)
486 return got_error_from_errno("malloc");
488 error = gw_apply_unveil(gw_trans->gw_dir->path);
492 error = gw_get_header(gw_trans, header, 1);
496 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
497 "diff_header_wrapper", KATTR__MAX);
500 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
501 "diff_header", KATTR__MAX);
504 error = gw_gen_diff_header(gw_trans, header->parent_id,
508 error = gw_gen_commit_header(gw_trans, header->commit_id,
512 error = gw_gen_tree_header(gw_trans, header->tree_id);
515 error = gw_gen_author_header(gw_trans, header->author);
518 error = gw_gen_committer_header(gw_trans, header->author);
521 error = gw_get_time_str(&age, header->committer_time,
525 error = gw_gen_age_header(gw_trans, age ?age : "");
528 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
531 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
534 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
535 "dotted_line", KATTR__MAX);
538 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
542 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
546 error = gw_output_diff(gw_trans, header);
550 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
552 gw_free_header(header);
554 if (error == NULL && kerr != KCGI_OK)
555 error = gw_kcgi_error(kerr);
559 static const struct got_error *
560 gw_index(struct gw_trans *gw_trans)
562 const struct got_error *error = NULL;
563 struct gw_dir *gw_dir = NULL;
564 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
565 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
566 char *href_tags = NULL;
567 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
568 enum kcgi_err kerr = KCGI_OK;
571 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
573 error = got_error_from_errno("pledge");
577 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
581 error = gw_load_got_paths(gw_trans);
585 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
586 "index_header", KATTR__MAX);
588 return gw_kcgi_error(kerr);
589 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
590 "index_header_project", KATTR__MAX);
592 return gw_kcgi_error(kerr);
593 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
595 return gw_kcgi_error(kerr);
596 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
598 return gw_kcgi_error(kerr);
600 if (gw_trans->gw_conf->got_show_repo_description) {
601 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
602 "index_header_description", KATTR__MAX);
604 return gw_kcgi_error(kerr);
605 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
607 return gw_kcgi_error(kerr);
608 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
610 return gw_kcgi_error(kerr);
613 if (gw_trans->gw_conf->got_show_repo_owner) {
614 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
615 "index_header_owner", KATTR__MAX);
617 return gw_kcgi_error(kerr);
618 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
620 return gw_kcgi_error(kerr);
621 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
623 return gw_kcgi_error(kerr);
626 if (gw_trans->gw_conf->got_show_repo_age) {
627 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
628 "index_header_age", KATTR__MAX);
630 return gw_kcgi_error(kerr);
631 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
633 return gw_kcgi_error(kerr);
634 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
636 return gw_kcgi_error(kerr);
639 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
641 return gw_kcgi_error(kerr);
643 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
644 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
645 "index_wrapper", KATTR__MAX);
647 return gw_kcgi_error(kerr);
648 kerr = khtml_printf(gw_trans->gw_html_req,
649 "No repositories found in %s",
650 gw_trans->gw_conf->got_repos_path);
652 return gw_kcgi_error(kerr);
653 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
655 return gw_kcgi_error(kerr);
656 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
657 "dotted_line", KATTR__MAX);
659 return gw_kcgi_error(kerr);
660 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
662 return gw_kcgi_error(kerr);
666 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
669 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
670 if (gw_trans->page > 0 && (gw_trans->page *
671 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
678 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
679 "index_wrapper", KATTR__MAX);
683 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
684 gw_dir->name, "action", "summary", NULL);
685 if (href_summary == NULL) {
686 error = got_error_from_errno("khttp_urlpart");
689 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
690 "index_project", KATTR__MAX);
693 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
694 href_summary, KATTR__MAX);
697 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
700 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
703 if (gw_trans->gw_conf->got_show_repo_description) {
704 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
705 KATTR_ID, "index_project_description", KATTR__MAX);
708 kerr = khtml_puts(gw_trans->gw_html_req,
709 gw_dir->description ? gw_dir->description : "");
712 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
716 if (gw_trans->gw_conf->got_show_repo_owner) {
717 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
718 KATTR_ID, "index_project_owner", KATTR__MAX);
721 kerr = khtml_puts(gw_trans->gw_html_req,
722 gw_dir->owner ? gw_dir->owner : "");
725 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
729 if (gw_trans->gw_conf->got_show_repo_age) {
730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
731 KATTR_ID, "index_project_age", KATTR__MAX);
734 kerr = khtml_puts(gw_trans->gw_html_req,
735 gw_dir->age ? gw_dir->age : "");
738 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
743 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
744 "navs_wrapper", KATTR__MAX);
747 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
752 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
753 href_summary, KATTR__MAX);
756 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
759 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
763 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
767 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
768 gw_dir->name, "action", "briefs", NULL);
769 if (href_briefs == NULL) {
770 error = got_error_from_errno("khttp_urlpart");
773 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
774 href_briefs, KATTR__MAX);
777 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
780 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
782 error = gw_kcgi_error(kerr);
784 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
788 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
789 gw_dir->name, "action", "commits", NULL);
790 if (href_commits == NULL) {
791 error = got_error_from_errno("khttp_urlpart");
794 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
795 href_commits, KATTR__MAX);
798 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
801 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
805 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
809 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
810 gw_dir->name, "action", "tags", NULL);
811 if (href_tags == NULL) {
812 error = got_error_from_errno("khttp_urlpart");
815 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
816 href_tags, KATTR__MAX);
819 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
822 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
826 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
830 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
831 gw_dir->name, "action", "tree", NULL);
832 if (href_tree == NULL) {
833 error = got_error_from_errno("khttp_urlpart");
836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
837 href_tree, KATTR__MAX);
840 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
844 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
847 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
848 "dotted_line", KATTR__MAX);
851 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
866 if (gw_trans->gw_conf->got_max_repos_display == 0)
869 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
870 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
871 (gw_trans->page > 0) &&
872 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
873 prev_disp == gw_trans->repos_total))) {
874 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
875 KATTR_ID, "np_wrapper", KATTR__MAX);
878 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
879 KATTR_ID, "nav_prev", KATTR__MAX);
884 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
885 (gw_trans->page > 0) &&
886 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
887 prev_disp == gw_trans->repos_total)) {
888 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
889 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
890 if (href_prev == NULL) {
891 error = got_error_from_errno("khttp_urlpartx");
894 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
895 KATTR_HREF, href_prev, KATTR__MAX);
898 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
901 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
906 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
908 return gw_kcgi_error(kerr);
910 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
911 next_disp == gw_trans->gw_conf->got_max_repos_display &&
912 dir_c != (gw_trans->page + 1) *
913 gw_trans->gw_conf->got_max_repos_display) {
914 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
915 KATTR_ID, "nav_next", KATTR__MAX);
918 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
919 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
920 if (href_next == NULL) {
921 error = got_error_from_errno("khttp_urlpartx");
924 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
925 KATTR_HREF, href_next, KATTR__MAX);
928 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
931 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
938 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
939 (gw_trans->page > 0) &&
940 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
941 prev_disp == gw_trans->repos_total)) {
942 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
956 if (error == NULL && kerr != KCGI_OK)
957 error = gw_kcgi_error(kerr);
961 static const struct got_error *
962 gw_commits(struct gw_trans *gw_trans)
964 const struct got_error *error = NULL;
965 struct gw_header *header = NULL, *n_header = NULL;
966 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
967 char *href_prev = NULL, *href_next = NULL;
968 enum kcgi_err kerr = KCGI_OK;
969 int commit_found = 0;
971 if ((header = gw_init_header()) == NULL)
972 return got_error_from_errno("malloc");
975 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
977 error = got_error_from_errno("pledge");
981 error = gw_apply_unveil(gw_trans->gw_dir->path);
985 error = gw_get_header(gw_trans, header,
986 gw_trans->gw_conf->got_max_commits_display);
990 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
991 if (commit_found == 0 && gw_trans->commit_id != NULL) {
992 if (strcmp(gw_trans->commit_id,
993 n_header->commit_id) != 0)
998 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
999 "commits_line_wrapper", KATTR__MAX);
1000 if (kerr != KCGI_OK)
1002 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
1003 n_header->refs_str);
1006 error = gw_gen_author_header(gw_trans, n_header->author);
1009 error = gw_gen_committer_header(gw_trans, n_header->author);
1012 error = gw_get_time_str(&age, n_header->committer_time,
1016 error = gw_gen_age_header(gw_trans, age ?age : "");
1019 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1020 if (kerr != KCGI_OK)
1023 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1024 "dotted_line", KATTR__MAX);
1025 if (kerr != KCGI_OK)
1027 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1028 if (kerr != KCGI_OK)
1031 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1032 "commit", KATTR__MAX);
1033 if (kerr != KCGI_OK)
1035 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1036 if (kerr != KCGI_OK)
1038 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1039 if (kerr != KCGI_OK)
1042 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1043 gw_trans->repo_name, "action", "diff", "commit",
1044 n_header->commit_id, NULL);
1045 if (href_diff == NULL) {
1046 error = got_error_from_errno("khttp_urlpart");
1049 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1050 KATTR_ID, "navs_wrapper", KATTR__MAX);
1051 if (kerr != KCGI_OK)
1053 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1054 KATTR_ID, "navs", KATTR__MAX);
1055 if (kerr != KCGI_OK)
1057 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1058 KATTR_HREF, href_diff, KATTR__MAX);
1059 if (kerr != KCGI_OK)
1061 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1062 if (kerr != KCGI_OK)
1064 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1065 if (kerr != KCGI_OK)
1068 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1069 if (kerr != KCGI_OK)
1072 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1073 gw_trans->repo_name, "action", "tree", "commit",
1074 n_header->commit_id, NULL);
1075 if (href_tree == NULL) {
1076 error = got_error_from_errno("khttp_urlpart");
1079 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1080 KATTR_HREF, href_tree, KATTR__MAX);
1081 if (kerr != KCGI_OK)
1083 khtml_puts(gw_trans->gw_html_req, "tree");
1084 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1085 if (kerr != KCGI_OK)
1087 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1088 if (kerr != KCGI_OK)
1091 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1092 "solid_line", KATTR__MAX);
1093 if (kerr != KCGI_OK)
1095 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1096 if (kerr != KCGI_OK)
1103 if (gw_trans->next_id || gw_trans->prev_id) {
1104 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1105 KATTR_ID, "np_wrapper", KATTR__MAX);
1106 if (kerr != KCGI_OK)
1108 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1109 KATTR_ID, "nav_prev", KATTR__MAX);
1110 if (kerr != KCGI_OK)
1114 if (gw_trans->prev_id) {
1115 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1116 KATTRX_STRING, gw_trans->repo_name, "page",
1117 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1118 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1119 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1120 if (href_prev == NULL) {
1121 error = got_error_from_errno("khttp_urlpartx");
1124 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1125 KATTR_HREF, href_prev, KATTR__MAX);
1126 if (kerr != KCGI_OK)
1128 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1129 if (kerr != KCGI_OK)
1131 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1132 if (kerr != KCGI_OK)
1136 if (gw_trans->next_id || gw_trans->page > 0) {
1137 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1138 if (kerr != KCGI_OK)
1139 return gw_kcgi_error(kerr);
1142 if (gw_trans->next_id) {
1143 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1144 KATTR_ID, "nav_next", KATTR__MAX);
1145 if (kerr != KCGI_OK)
1147 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1148 KATTRX_STRING, gw_trans->repo_name, "page",
1149 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1150 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1151 gw_trans->next_id, NULL);
1152 if (href_next == NULL) {
1153 error = got_error_from_errno("khttp_urlpartx");
1156 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1157 KATTR_HREF, href_next, KATTR__MAX);
1158 if (kerr != KCGI_OK)
1160 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1161 if (kerr != KCGI_OK)
1163 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1164 if (kerr != KCGI_OK)
1168 if (gw_trans->next_id || gw_trans->page > 0) {
1169 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1170 if (kerr != KCGI_OK)
1174 gw_free_header(header);
1175 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1176 gw_free_header(n_header);
1182 if (error == NULL && kerr != KCGI_OK)
1183 error = gw_kcgi_error(kerr);
1187 static const struct got_error *
1188 gw_briefs(struct gw_trans *gw_trans)
1190 const struct got_error *error = NULL;
1191 struct gw_header *header = NULL, *n_header = NULL;
1192 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1193 char *href_prev = NULL, *href_next = NULL;
1194 char *newline, *smallerthan;
1195 enum kcgi_err kerr = KCGI_OK;
1196 int commit_found = 0;
1198 if ((header = gw_init_header()) == NULL)
1199 return got_error_from_errno("malloc");
1202 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
1204 error = got_error_from_errno("pledge");
1208 if (gw_trans->action != GW_SUMMARY) {
1209 error = gw_apply_unveil(gw_trans->gw_dir->path);
1214 if (gw_trans->action == GW_SUMMARY)
1215 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1217 error = gw_get_header(gw_trans, header,
1218 gw_trans->gw_conf->got_max_commits_display);
1222 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1223 if (commit_found == 0 && gw_trans->commit_id != NULL) {
1224 if (strcmp(gw_trans->commit_id,
1225 n_header->commit_id) != 0)
1230 error = gw_get_time_str(&age, n_header->committer_time,
1235 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1236 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1237 if (kerr != KCGI_OK)
1240 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1241 KATTR_ID, "briefs_age", KATTR__MAX);
1242 if (kerr != KCGI_OK)
1244 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1245 if (kerr != KCGI_OK)
1247 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1248 if (kerr != KCGI_OK)
1251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1252 KATTR_ID, "briefs_author", KATTR__MAX);
1253 if (kerr != KCGI_OK)
1255 smallerthan = strchr(n_header->author, '<');
1257 *smallerthan = '\0';
1258 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1259 if (kerr != KCGI_OK)
1261 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1262 if (kerr != KCGI_OK)
1265 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1266 gw_trans->repo_name, "action", "diff", "commit",
1267 n_header->commit_id, NULL);
1268 if (href_diff == NULL) {
1269 error = got_error_from_errno("khttp_urlpart");
1272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1273 KATTR_ID, "briefs_log", KATTR__MAX);
1274 if (kerr != KCGI_OK)
1276 newline = strchr(n_header->commit_msg, '\n');
1279 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1280 KATTR_HREF, href_diff, KATTR__MAX);
1281 if (kerr != KCGI_OK)
1283 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1284 if (kerr != KCGI_OK)
1286 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1287 if (kerr != KCGI_OK)
1290 if (n_header->refs_str) {
1291 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1292 if (kerr != KCGI_OK)
1294 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1295 KATTR_ID, "refs_str", KATTR__MAX);
1296 if (kerr != KCGI_OK)
1298 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1299 n_header->refs_str);
1300 if (kerr != KCGI_OK)
1302 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1303 if (kerr != KCGI_OK)
1307 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1308 if (kerr != KCGI_OK)
1311 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1312 KATTR_ID, "navs_wrapper", KATTR__MAX);
1313 if (kerr != KCGI_OK)
1315 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1316 KATTR_ID, "navs", KATTR__MAX);
1317 if (kerr != KCGI_OK)
1319 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1320 KATTR_HREF, href_diff, KATTR__MAX);
1321 if (kerr != KCGI_OK)
1323 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1324 if (kerr != KCGI_OK)
1326 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1327 if (kerr != KCGI_OK)
1330 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1331 if (kerr != KCGI_OK)
1334 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1335 gw_trans->repo_name, "action", "tree", "commit",
1336 n_header->commit_id, NULL);
1337 if (href_tree == NULL) {
1338 error = got_error_from_errno("khttp_urlpart");
1341 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1342 KATTR_HREF, href_tree, KATTR__MAX);
1343 if (kerr != KCGI_OK)
1345 khtml_puts(gw_trans->gw_html_req, "tree");
1346 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1347 if (kerr != KCGI_OK)
1349 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1350 if (kerr != KCGI_OK)
1353 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1354 KATTR_ID, "dotted_line", KATTR__MAX);
1355 if (kerr != KCGI_OK)
1357 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1358 if (kerr != KCGI_OK)
1369 if (gw_trans->next_id || gw_trans->prev_id) {
1370 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1371 KATTR_ID, "np_wrapper", KATTR__MAX);
1372 if (kerr != KCGI_OK)
1374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1375 KATTR_ID, "nav_prev", KATTR__MAX);
1376 if (kerr != KCGI_OK)
1380 if (gw_trans->prev_id) {
1381 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1382 KATTRX_STRING, gw_trans->repo_name, "page",
1383 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1384 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1385 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1386 if (href_prev == NULL) {
1387 error = got_error_from_errno("khttp_urlpartx");
1390 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1391 KATTR_HREF, href_prev, KATTR__MAX);
1392 if (kerr != KCGI_OK)
1394 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1395 if (kerr != KCGI_OK)
1397 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1398 if (kerr != KCGI_OK)
1402 if (gw_trans->next_id || gw_trans->page > 0) {
1403 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1404 if (kerr != KCGI_OK)
1405 return gw_kcgi_error(kerr);
1408 if (gw_trans->next_id) {
1409 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1410 KATTR_ID, "nav_next", KATTR__MAX);
1411 if (kerr != KCGI_OK)
1414 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1415 KATTRX_STRING, gw_trans->repo_name, "page",
1416 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1417 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1418 gw_trans->next_id, NULL);
1419 if (href_next == NULL) {
1420 error = got_error_from_errno("khttp_urlpartx");
1423 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1424 KATTR_HREF, href_next, KATTR__MAX);
1425 if (kerr != KCGI_OK)
1427 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1428 if (kerr != KCGI_OK)
1430 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1431 if (kerr != KCGI_OK)
1435 if (gw_trans->next_id || gw_trans->page > 0) {
1436 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1437 if (kerr != KCGI_OK)
1441 gw_free_header(header);
1442 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1443 gw_free_header(n_header);
1449 if (error == NULL && kerr != KCGI_OK)
1450 error = gw_kcgi_error(kerr);
1454 static const struct got_error *
1455 gw_summary(struct gw_trans *gw_trans)
1457 const struct got_error *error = NULL;
1459 enum kcgi_err kerr = KCGI_OK;
1462 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
1464 return got_error_from_errno("pledge");
1466 error = gw_apply_unveil(gw_trans->gw_dir->path);
1470 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1471 "summary_wrapper", KATTR__MAX);
1472 if (kerr != KCGI_OK)
1473 return gw_kcgi_error(kerr);
1475 if (gw_trans->gw_conf->got_show_repo_description &&
1476 gw_trans->gw_dir->description != NULL &&
1477 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1478 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1479 KATTR_ID, "description_title", KATTR__MAX);
1480 if (kerr != KCGI_OK)
1482 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1483 if (kerr != KCGI_OK)
1485 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1486 if (kerr != KCGI_OK)
1488 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1489 KATTR_ID, "description", KATTR__MAX);
1490 if (kerr != KCGI_OK)
1492 kerr = khtml_puts(gw_trans->gw_html_req,
1493 gw_trans->gw_dir->description);
1494 if (kerr != KCGI_OK)
1496 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1497 if (kerr != KCGI_OK)
1501 if (gw_trans->gw_conf->got_show_repo_owner &&
1502 gw_trans->gw_dir->owner != NULL &&
1503 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1504 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1505 KATTR_ID, "repo_owner_title", KATTR__MAX);
1506 if (kerr != KCGI_OK)
1508 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1509 if (kerr != KCGI_OK)
1511 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1512 if (kerr != KCGI_OK)
1514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1515 KATTR_ID, "repo_owner", KATTR__MAX);
1516 if (kerr != KCGI_OK)
1518 kerr = khtml_puts(gw_trans->gw_html_req,
1519 gw_trans->gw_dir->owner);
1520 if (kerr != KCGI_OK)
1522 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1523 if (kerr != KCGI_OK)
1527 if (gw_trans->gw_conf->got_show_repo_age) {
1528 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1534 KATTR_ID, "last_change_title", KATTR__MAX);
1535 if (kerr != KCGI_OK)
1537 kerr = khtml_puts(gw_trans->gw_html_req,
1539 if (kerr != KCGI_OK)
1541 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1542 if (kerr != KCGI_OK)
1544 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1545 KATTR_ID, "last_change", KATTR__MAX);
1546 if (kerr != KCGI_OK)
1548 kerr = khtml_puts(gw_trans->gw_html_req, age);
1549 if (kerr != KCGI_OK)
1551 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1552 if (kerr != KCGI_OK)
1557 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1558 gw_trans->gw_dir->url != NULL &&
1559 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1560 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1561 KATTR_ID, "cloneurl_title", KATTR__MAX);
1562 if (kerr != KCGI_OK)
1564 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1565 if (kerr != KCGI_OK)
1567 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1568 if (kerr != KCGI_OK)
1570 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1571 KATTR_ID, "cloneurl", KATTR__MAX);
1572 if (kerr != KCGI_OK)
1574 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1575 if (kerr != KCGI_OK)
1577 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1578 if (kerr != KCGI_OK)
1582 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1583 if (kerr != KCGI_OK)
1586 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1587 "briefs_title_wrapper", KATTR__MAX);
1588 if (kerr != KCGI_OK)
1590 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1591 "briefs_title", KATTR__MAX);
1592 if (kerr != KCGI_OK)
1594 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1595 if (kerr != KCGI_OK)
1597 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1598 if (kerr != KCGI_OK)
1600 error = gw_briefs(gw_trans);
1604 error = gw_tags(gw_trans);
1608 error = gw_output_repo_heads(gw_trans);
1611 if (error == NULL && kerr != KCGI_OK)
1612 error = gw_kcgi_error(kerr);
1616 static const struct got_error *
1617 gw_tree(struct gw_trans *gw_trans)
1619 const struct got_error *error = NULL;
1620 struct gw_header *header = NULL;
1621 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1623 enum kcgi_err kerr = KCGI_OK;
1626 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
1628 return got_error_from_errno("pledge");
1630 if ((header = gw_init_header()) == NULL)
1631 return got_error_from_errno("malloc");
1633 error = gw_apply_unveil(gw_trans->gw_dir->path);
1637 error = gw_get_header(gw_trans, header, 1);
1641 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1642 "tree_header_wrapper", KATTR__MAX);
1643 if (kerr != KCGI_OK)
1645 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1646 "tree_header", KATTR__MAX);
1647 if (kerr != KCGI_OK)
1649 error = gw_gen_tree_header(gw_trans, header->tree_id);
1652 error = gw_get_time_str(&age, header->committer_time,
1656 error = gw_gen_age_header(gw_trans, age ?age : "");
1659 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1662 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1663 if (kerr != KCGI_OK)
1665 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1666 "dotted_line", KATTR__MAX);
1667 if (kerr != KCGI_OK)
1669 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1670 if (kerr != KCGI_OK)
1673 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1674 "tree", KATTR__MAX);
1675 if (kerr != KCGI_OK)
1677 error = gw_output_repo_tree(gw_trans, header);
1681 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1683 gw_free_header(header);
1684 free(tree_html_disp);
1688 if (error == NULL && kerr != KCGI_OK)
1689 error = gw_kcgi_error(kerr);
1693 static const struct got_error *
1694 gw_tags(struct gw_trans *gw_trans)
1696 const struct got_error *error = NULL;
1697 struct gw_header *header = NULL;
1698 char *href_next = NULL, *href_prev = NULL;
1699 enum kcgi_err kerr = KCGI_OK;
1702 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
1704 return got_error_from_errno("pledge");
1706 if ((header = gw_init_header()) == NULL)
1707 return got_error_from_errno("malloc");
1709 if (gw_trans->action != GW_SUMMARY) {
1710 error = gw_apply_unveil(gw_trans->gw_dir->path);
1715 error = gw_get_header(gw_trans, header, 1);
1719 if (gw_trans->action == GW_SUMMARY) {
1720 gw_trans->next_id = NULL;
1721 error = gw_output_repo_tags(gw_trans, header,
1722 D_MAXSLCOMMDISP, TAGBRIEF);
1726 error = gw_output_repo_tags(gw_trans, header,
1727 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1732 if (gw_trans->next_id || gw_trans->page > 0) {
1733 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1734 KATTR_ID, "np_wrapper", KATTR__MAX);
1735 if (kerr != KCGI_OK)
1737 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1738 KATTR_ID, "nav_prev", KATTR__MAX);
1739 if (kerr != KCGI_OK)
1743 if (gw_trans->prev_id) {
1744 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1745 KATTRX_STRING, gw_trans->repo_name, "page",
1746 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1747 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1748 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1749 if (href_prev == NULL) {
1750 error = got_error_from_errno("khttp_urlpartx");
1753 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1754 KATTR_HREF, href_prev, KATTR__MAX);
1755 if (kerr != KCGI_OK)
1757 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1758 if (kerr != KCGI_OK)
1760 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1761 if (kerr != KCGI_OK)
1765 if (gw_trans->next_id || gw_trans->page > 0) {
1766 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1767 if (kerr != KCGI_OK)
1768 return gw_kcgi_error(kerr);
1771 if (gw_trans->next_id) {
1772 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1773 KATTR_ID, "nav_next", KATTR__MAX);
1774 if (kerr != KCGI_OK)
1776 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1777 KATTRX_STRING, gw_trans->repo_name, "page",
1778 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1779 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1780 gw_trans->next_id, NULL);
1781 if (href_next == NULL) {
1782 error = got_error_from_errno("khttp_urlpartx");
1785 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1786 KATTR_HREF, href_next, KATTR__MAX);
1787 if (kerr != KCGI_OK)
1789 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1790 if (kerr != KCGI_OK)
1792 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1793 if (kerr != KCGI_OK)
1797 if (gw_trans->next_id || gw_trans->page > 0) {
1798 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1799 if (kerr != KCGI_OK)
1803 gw_free_header(header);
1806 if (error == NULL && kerr != KCGI_OK)
1807 error = gw_kcgi_error(kerr);
1811 static const struct got_error *
1812 gw_tag(struct gw_trans *gw_trans)
1814 const struct got_error *error = NULL;
1815 struct gw_header *header = NULL;
1816 enum kcgi_err kerr = KCGI_OK;
1819 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil", NULL) == -1)
1820 return got_error_from_errno("pledge");
1822 if ((header = gw_init_header()) == NULL)
1823 return got_error_from_errno("malloc");
1825 error = gw_apply_unveil(gw_trans->gw_dir->path);
1829 if (gw_trans->commit_id == NULL) {
1830 error = got_error_msg(GOT_ERR_QUERYSTRING,
1831 "commit required in querystring");
1835 error = gw_get_header(gw_trans, header, 1);
1839 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1840 "tag_header_wrapper", KATTR__MAX);
1841 if (kerr != KCGI_OK)
1843 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1844 "tag_header", KATTR__MAX);
1845 if (kerr != KCGI_OK)
1847 error = gw_gen_commit_header(gw_trans, header->commit_id,
1851 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1854 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1855 if (kerr != KCGI_OK)
1857 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1858 "dotted_line", KATTR__MAX);
1859 if (kerr != KCGI_OK)
1861 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1862 if (kerr != KCGI_OK)
1865 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1866 "tree", KATTR__MAX);
1867 if (kerr != KCGI_OK)
1870 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1874 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1876 gw_free_header(header);
1877 if (error == NULL && kerr != KCGI_OK)
1878 error = gw_kcgi_error(kerr);
1882 static const struct got_error *
1883 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1885 const struct got_error *error = NULL;
1890 if (asprintf(&dir_test, "%s/%s/%s",
1891 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1892 GOTWEB_GIT_DIR) == -1)
1893 return got_error_from_errno("asprintf");
1895 dt = opendir(dir_test);
1899 gw_dir->path = strdup(dir_test);
1900 if (gw_dir->path == NULL) {
1902 error = got_error_from_errno("strdup");
1909 if (asprintf(&dir_test, "%s/%s/%s",
1910 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1911 GOTWEB_GOT_DIR) == -1) {
1913 error = got_error_from_errno("asprintf");
1917 dt = opendir(dir_test);
1922 error = got_error(GOT_ERR_NOT_GIT_REPO);
1926 if (asprintf(&dir_test, "%s/%s",
1927 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1928 error = got_error_from_errno("asprintf");
1933 gw_dir->path = strdup(dir_test);
1934 if (gw_dir->path == NULL) {
1936 error = got_error_from_errno("strdup");
1940 dt = opendir(dir_test);
1942 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1947 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1951 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1954 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1958 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1962 if (dt && closedir(dt) == -1 && error == NULL)
1963 error = got_error_from_errno("closedir");
1967 static const struct got_error *
1968 gw_load_got_paths(struct gw_trans *gw_trans)
1970 const struct got_error *error = NULL;
1972 struct dirent **sd_dent;
1973 struct gw_dir *gw_dir;
1975 unsigned int d_cnt, d_i;
1977 d = opendir(gw_trans->gw_conf->got_repos_path);
1979 error = got_error_from_errno2("opendir",
1980 gw_trans->gw_conf->got_repos_path);
1984 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1987 error = got_error_from_errno2("scandir",
1988 gw_trans->gw_conf->got_repos_path);
1992 for (d_i = 0; d_i < d_cnt; d_i++) {
1993 if (gw_trans->gw_conf->got_max_repos > 0 &&
1994 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1995 break; /* account for parent and self */
1997 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1998 strcmp(sd_dent[d_i]->d_name, "..") == 0)
2001 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
2005 error = gw_load_got_path(gw_trans, gw_dir);
2006 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
2009 } else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
2012 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
2013 !got_path_dir_is_empty(gw_dir->path)) {
2014 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
2016 gw_trans->repos_total++;
2020 if (d && closedir(d) == -1 && error == NULL)
2021 error = got_error_from_errno("closedir");
2025 static const struct got_error *
2026 gw_parse_querystring(struct gw_trans *gw_trans)
2028 const struct got_error *error = NULL;
2030 const struct gw_query_action *action = NULL;
2033 if (gw_trans->gw_req->fieldnmap[0]) {
2034 return got_error(GOT_ERR_QUERYSTRING);
2035 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2036 /* define gw_trans->repo_path */
2037 gw_trans->repo_name = p->parsed.s;
2039 if (asprintf(&gw_trans->repo_path, "%s/%s",
2040 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2041 return got_error_from_errno("asprintf");
2043 /* get action and set function */
2044 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2045 for (i = 0; i < nitems(gw_query_funcs); i++) {
2046 action = &gw_query_funcs[i];
2047 if (action->func_name == NULL)
2049 if (strcmp(action->func_name,
2050 p->parsed.s) == 0) {
2051 gw_trans->action = i;
2056 if (gw_trans->action == -1) {
2057 gw_trans->action = GW_ERR;
2058 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2059 p != NULL ? "bad action in querystring" :
2060 "no action in querystring");
2064 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2065 if (asprintf(&gw_trans->commit_id, "%s",
2067 return got_error_from_errno("asprintf");
2070 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2071 gw_trans->repo_file = p->parsed.s;
2073 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2074 if (asprintf(&gw_trans->repo_folder, "%s",
2076 return got_error_from_errno("asprintf");
2079 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2080 if (asprintf(&gw_trans->prev_id, "%s",
2082 return got_error_from_errno("asprintf");
2085 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2086 gw_trans->headref = p->parsed.s;
2088 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2092 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2094 gw_trans->action = GW_INDEX;
2096 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2097 gw_trans->page = p->parsed.i;
2102 static const struct got_error *
2103 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2105 const struct got_error *error;
2107 *gw_dir = malloc(sizeof(**gw_dir));
2108 if (*gw_dir == NULL)
2109 return got_error_from_errno("malloc");
2111 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2112 error = got_error_from_errno("asprintf");
2121 static const struct got_error *
2122 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2124 enum kcgi_err kerr = KCGI_OK;
2126 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2127 if (kerr != KCGI_OK)
2128 return gw_kcgi_error(kerr);
2129 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2131 if (kerr != KCGI_OK)
2132 return gw_kcgi_error(kerr);
2133 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2135 if (kerr != KCGI_OK)
2136 return gw_kcgi_error(kerr);
2137 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2139 if (kerr != KCGI_OK)
2140 return gw_kcgi_error(kerr);
2141 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2142 if (kerr != KCGI_OK)
2143 return gw_kcgi_error(kerr);
2144 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2146 if (kerr != KCGI_OK)
2147 return gw_kcgi_error(kerr);
2149 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2150 kerr = khttp_head(gw_trans->gw_req,
2151 kresps[KRESP_CONTENT_DISPOSITION],
2152 "attachment; filename=%s", gw_trans->repo_file);
2153 if (kerr != KCGI_OK)
2154 return gw_kcgi_error(kerr);
2157 kerr = khttp_body(gw_trans->gw_req);
2158 return gw_kcgi_error(kerr);
2161 static const struct got_error *
2162 gw_display_index(struct gw_trans *gw_trans)
2164 const struct got_error *error;
2165 enum kcgi_err kerr = KCGI_OK;
2167 /* catch early querystring errors */
2168 if (gw_trans->error)
2169 gw_trans->action = GW_ERR;
2171 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2175 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2176 if (kerr != KCGI_OK)
2177 return gw_kcgi_error(kerr);
2179 if (gw_trans->action != GW_BLOB) {
2180 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2181 gw_query_funcs[gw_trans->action].template);
2182 if (kerr != KCGI_OK) {
2183 khtml_close(gw_trans->gw_html_req);
2184 return gw_kcgi_error(kerr);
2188 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2191 static const struct got_error *
2192 gw_error(struct gw_trans *gw_trans)
2194 enum kcgi_err kerr = KCGI_OK;
2196 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2198 return gw_kcgi_error(kerr);
2202 gw_template(size_t key, void *arg)
2204 const struct got_error *error = NULL;
2205 enum kcgi_err kerr = KCGI_OK;
2206 struct gw_trans *gw_trans = arg;
2207 char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
2208 char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
2210 if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
2211 "/apple-touch-icon.png") == -1)
2213 if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
2214 "/favicon-32x32.png") == -1)
2216 if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
2217 "/favicon-16x16.png") == -1)
2219 if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
2220 "/site.webmanifest") == -1)
2222 if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
2223 "/safari-pinned-tab.svg") == -1)
2225 if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
2226 "/gotweb.css") == -1)
2228 if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
2229 gw_trans->gw_conf->got_www_path ? "/" : "",
2230 gw_trans->gw_conf->got_logo) == -1)
2235 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2236 KATTR_NAME, "viewport",
2237 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2239 if (kerr != KCGI_OK)
2241 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2242 if (kerr != KCGI_OK)
2244 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2245 KATTR_CHARSET, "utf-8",
2247 if (kerr != KCGI_OK)
2249 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2250 if (kerr != KCGI_OK)
2252 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2253 KATTR_NAME, "msapplication-TileColor",
2254 KATTR_CONTENT, "#da532c", KATTR__MAX);
2255 if (kerr != KCGI_OK)
2257 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2258 if (kerr != KCGI_OK)
2260 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2261 KATTR_NAME, "theme-color",
2262 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2263 if (kerr != KCGI_OK)
2265 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2266 if (kerr != KCGI_OK)
2268 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2269 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2270 KATTR_HREF, ati, KATTR__MAX);
2271 if (kerr != KCGI_OK)
2273 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2274 if (kerr != KCGI_OK)
2276 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2277 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2278 "32x32", KATTR_HREF, fic32, KATTR__MAX);
2279 if (kerr != KCGI_OK)
2281 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2282 if (kerr != KCGI_OK)
2284 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2285 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2286 "16x16", KATTR_HREF, fic16, KATTR__MAX);
2287 if (kerr != KCGI_OK)
2289 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2290 if (kerr != KCGI_OK)
2292 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2293 KATTR_REL, "manifest", KATTR_HREF, swm,
2295 if (kerr != KCGI_OK)
2297 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2298 if (kerr != KCGI_OK)
2300 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2301 KATTR_REL, "mask-icon", KATTR_HREF,
2303 if (kerr != KCGI_OK)
2305 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2306 if (kerr != KCGI_OK)
2308 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2309 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2310 KATTR_HREF, css, KATTR__MAX);
2311 if (kerr != KCGI_OK)
2313 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2314 if (kerr != KCGI_OK)
2318 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2319 KATTR_ID, "got_link", KATTR__MAX);
2320 if (kerr != KCGI_OK)
2322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2323 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2324 KATTR_TARGET, "_sotd", KATTR__MAX);
2325 if (kerr != KCGI_OK)
2327 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2328 KATTR_SRC, logo, KATTR__MAX);
2329 if (kerr != KCGI_OK)
2331 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2332 if (kerr != KCGI_OK)
2335 case (TEMPL_SITEPATH):
2336 error = gw_output_site_link(gw_trans);
2341 if (gw_trans->gw_conf->got_site_name != NULL) {
2342 kerr = khtml_puts(gw_trans->gw_html_req,
2343 gw_trans->gw_conf->got_site_name);
2344 if (kerr != KCGI_OK)
2348 case (TEMPL_SEARCH):
2350 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2351 "search", KATTR__MAX);
2352 if (kerr != KCGI_OK)
2354 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2355 KATTR_METHOD, "POST", KATTR__MAX);
2356 if (kerr != KCGI_OK)
2358 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2359 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2360 KATTR_MAXLENGTH, "50", KATTR__MAX);
2361 if (kerr != KCGI_OK)
2363 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2365 if (kerr != KCGI_OK)
2367 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2368 if (kerr != KCGI_OK)
2370 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2371 if (kerr != KCGI_OK)
2374 case(TEMPL_SITEOWNER):
2375 if (gw_trans->gw_conf->got_site_owner != NULL &&
2376 gw_trans->gw_conf->got_show_site_owner) {
2377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2378 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2379 if (kerr != KCGI_OK)
2381 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2382 KATTR_ID, "site_owner", KATTR__MAX);
2383 if (kerr != KCGI_OK)
2385 kerr = khtml_puts(gw_trans->gw_html_req,
2386 gw_trans->gw_conf->got_site_owner);
2387 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2388 if (kerr != KCGI_OK)
2392 case(TEMPL_CONTENT):
2393 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2395 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2396 KATTR_ID, "tmpl_err", KATTR__MAX);
2397 if (kerr != KCGI_OK)
2399 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2401 if (kerr != KCGI_OK)
2403 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2404 if (kerr != KCGI_OK)
2430 static const struct got_error *
2431 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2433 const struct got_error *error = NULL;
2434 enum kcgi_err kerr = KCGI_OK;
2436 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2437 KATTR_ID, "header_commit_title", KATTR__MAX);
2438 if (kerr != KCGI_OK)
2440 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2441 if (kerr != KCGI_OK)
2443 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2444 if (kerr != KCGI_OK)
2446 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2447 KATTR_ID, "header_commit", KATTR__MAX);
2448 if (kerr != KCGI_OK)
2450 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2451 if (kerr != KCGI_OK)
2454 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2455 KATTR_ID, "refs_str", KATTR__MAX);
2456 if (kerr != KCGI_OK)
2458 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2459 if (kerr != KCGI_OK)
2461 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2462 if (kerr != KCGI_OK)
2465 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2467 if (error == NULL && kerr != KCGI_OK)
2468 error = gw_kcgi_error(kerr);
2472 static const struct got_error *
2473 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2475 const struct got_error *error = NULL;
2476 enum kcgi_err kerr = KCGI_OK;
2478 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2479 KATTR_ID, "header_diff_title", KATTR__MAX);
2480 if (kerr != KCGI_OK)
2482 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2483 if (kerr != KCGI_OK)
2485 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2486 if (kerr != KCGI_OK)
2488 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2489 KATTR_ID, "header_diff", KATTR__MAX);
2490 if (kerr != KCGI_OK)
2493 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2494 if (kerr != KCGI_OK)
2497 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2498 if (kerr != KCGI_OK)
2500 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2501 if (kerr != KCGI_OK)
2503 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2505 if (error == NULL && kerr != KCGI_OK)
2506 error = gw_kcgi_error(kerr);
2510 static const struct got_error *
2511 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2513 const struct got_error *error = NULL;
2514 enum kcgi_err kerr = KCGI_OK;
2516 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2517 KATTR_ID, "header_age_title", KATTR__MAX);
2518 if (kerr != KCGI_OK)
2520 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2521 if (kerr != KCGI_OK)
2523 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2524 if (kerr != KCGI_OK)
2526 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2527 KATTR_ID, "header_age", KATTR__MAX);
2528 if (kerr != KCGI_OK)
2530 kerr = khtml_puts(gw_trans->gw_html_req, str);
2531 if (kerr != KCGI_OK)
2533 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2535 if (error == NULL && kerr != KCGI_OK)
2536 error = gw_kcgi_error(kerr);
2540 static const struct got_error *
2541 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2543 const struct got_error *error = NULL;
2544 enum kcgi_err kerr = KCGI_OK;
2546 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2547 KATTR_ID, "header_author_title", KATTR__MAX);
2548 if (kerr != KCGI_OK)
2550 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2551 if (kerr != KCGI_OK)
2553 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2554 if (kerr != KCGI_OK)
2556 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2557 KATTR_ID, "header_author", KATTR__MAX);
2558 if (kerr != KCGI_OK)
2560 kerr = khtml_puts(gw_trans->gw_html_req, str);
2561 if (kerr != KCGI_OK)
2563 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2565 if (error == NULL && kerr != KCGI_OK)
2566 error = gw_kcgi_error(kerr);
2570 static const struct got_error *
2571 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2573 const struct got_error *error = NULL;
2574 enum kcgi_err kerr = KCGI_OK;
2576 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2577 KATTR_ID, "header_committer_title", KATTR__MAX);
2578 if (kerr != KCGI_OK)
2580 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2581 if (kerr != KCGI_OK)
2583 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2584 if (kerr != KCGI_OK)
2586 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2587 KATTR_ID, "header_committer", KATTR__MAX);
2588 if (kerr != KCGI_OK)
2590 kerr = khtml_puts(gw_trans->gw_html_req, str);
2591 if (kerr != KCGI_OK)
2593 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2595 if (error == NULL && kerr != KCGI_OK)
2596 error = gw_kcgi_error(kerr);
2600 static const struct got_error *
2601 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2603 const struct got_error *error = NULL;
2604 enum kcgi_err kerr = KCGI_OK;
2606 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2607 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2608 if (kerr != KCGI_OK)
2610 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2611 if (kerr != KCGI_OK)
2613 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2614 if (kerr != KCGI_OK)
2616 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2617 KATTR_ID, "header_commit_msg", KATTR__MAX);
2618 if (kerr != KCGI_OK)
2620 kerr = khttp_puts(gw_trans->gw_req, str);
2621 if (kerr != KCGI_OK)
2623 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2625 if (error == NULL && kerr != KCGI_OK)
2626 error = gw_kcgi_error(kerr);
2630 static const struct got_error *
2631 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2633 const struct got_error *error = NULL;
2634 enum kcgi_err kerr = KCGI_OK;
2636 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2637 KATTR_ID, "header_tree_title", KATTR__MAX);
2638 if (kerr != KCGI_OK)
2640 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2641 if (kerr != KCGI_OK)
2643 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2644 if (kerr != KCGI_OK)
2646 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2647 KATTR_ID, "header_tree", KATTR__MAX);
2648 if (kerr != KCGI_OK)
2650 kerr = khtml_puts(gw_trans->gw_html_req, str);
2651 if (kerr != KCGI_OK)
2653 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2655 if (error == NULL && kerr != KCGI_OK)
2656 error = gw_kcgi_error(kerr);
2660 static const struct got_error *
2661 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2664 const struct got_error *error = NULL;
2666 char *d_file = NULL;
2670 *description = NULL;
2671 if (gw_trans->gw_conf->got_show_repo_description == 0)
2674 if (asprintf(&d_file, "%s/description", dir) == -1)
2675 return got_error_from_errno("asprintf");
2677 f = fopen(d_file, "re");
2679 if (errno == ENOENT || errno == EACCES)
2681 error = got_error_from_errno2("fopen", d_file);
2685 if (fseek(f, 0, SEEK_END) == -1) {
2686 error = got_ferror(f, GOT_ERR_IO);
2691 error = got_ferror(f, GOT_ERR_IO);
2694 if (fseek(f, 0, SEEK_SET) == -1) {
2695 error = got_ferror(f, GOT_ERR_IO);
2698 *description = calloc(len + 1, sizeof(**description));
2699 if (*description == NULL) {
2700 error = got_error_from_errno("calloc");
2704 n = fread(*description, 1, len, f);
2705 if (n == 0 && ferror(f))
2706 error = got_ferror(f, GOT_ERR_IO);
2708 if (f != NULL && fclose(f) == EOF && error == NULL)
2709 error = got_error_from_errno("fclose");
2714 static const struct got_error *
2715 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2719 const char *years = "years ago", *months = "months ago";
2720 const char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2721 const char *minutes = "minutes ago", *seconds = "seconds ago";
2722 const char *now = "right now";
2730 diff_time = time(NULL) - committer_time;
2731 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2732 if (asprintf(repo_age, "%lld %s",
2733 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2734 return got_error_from_errno("asprintf");
2735 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2736 if (asprintf(repo_age, "%lld %s",
2737 (diff_time / 60 / 60 / 24 / (365 / 12)),
2739 return got_error_from_errno("asprintf");
2740 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2741 if (asprintf(repo_age, "%lld %s",
2742 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2743 return got_error_from_errno("asprintf");
2744 } else if (diff_time > 60 * 60 * 24 * 2) {
2745 if (asprintf(repo_age, "%lld %s",
2746 (diff_time / 60 / 60 / 24), days) == -1)
2747 return got_error_from_errno("asprintf");
2748 } else if (diff_time > 60 * 60 * 2) {
2749 if (asprintf(repo_age, "%lld %s",
2750 (diff_time / 60 / 60), hours) == -1)
2751 return got_error_from_errno("asprintf");
2752 } else if (diff_time > 60 * 2) {
2753 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2755 return got_error_from_errno("asprintf");
2756 } else if (diff_time > 2) {
2757 if (asprintf(repo_age, "%lld %s", diff_time,
2759 return got_error_from_errno("asprintf");
2761 if (asprintf(repo_age, "%s", now) == -1)
2762 return got_error_from_errno("asprintf");
2766 if (gmtime_r(&committer_time, &tm) == NULL)
2767 return got_error_from_errno("gmtime_r");
2769 s = asctime_r(&tm, datebuf);
2771 return got_error_from_errno("asctime_r");
2773 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2774 return got_error_from_errno("asprintf");
2780 static const struct got_error *
2781 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2782 const char *refname, int ref_tm)
2784 const struct got_error *error = NULL;
2785 struct got_repository *repo = NULL;
2786 struct got_commit_object *commit = NULL;
2787 struct got_reflist_head refs;
2788 struct got_reflist_entry *re;
2789 time_t committer_time = 0, cmp_time = 0;
2794 if (gw_trans->gw_conf->got_show_repo_age == 0)
2798 repo = gw_trans->repo;
2800 error = got_repo_open(&repo, dir, NULL, gw_trans->pack_fds);
2805 error = got_ref_list(&refs, repo, "refs/heads",
2806 got_ref_cmp_by_name, NULL);
2811 * Find the youngest branch tip in the repository, or the age of
2812 * the a specific branch tip if a name was provided by the caller.
2814 TAILQ_FOREACH(re, &refs, entry) {
2815 struct got_object_id *id = NULL;
2817 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2820 error = got_ref_resolve(&id, repo, re->ref);
2824 error = got_object_open_as_commit(&commit, repo, id);
2830 got_object_commit_get_committer_time(commit);
2831 got_object_commit_close(commit);
2832 if (cmp_time < committer_time)
2833 cmp_time = committer_time;
2839 if (cmp_time != 0) {
2840 committer_time = cmp_time;
2841 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2844 got_ref_list_free(&refs);
2845 if (gw_trans->repo == NULL) {
2846 const struct got_error *close_err = got_repo_close(repo);
2853 static const struct got_error *
2854 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2856 const struct got_error *error;
2857 FILE *f = NULL, *f1 = NULL, *f2 = NULL;
2858 int fd1 = -1, fd2 = -1;
2859 struct got_object_id *id1 = NULL, *id2 = NULL;
2860 char *label1 = NULL, *label2 = NULL, *line = NULL;
2862 size_t linesize = 0;
2864 enum kcgi_err kerr = KCGI_OK;
2870 f1 = got_opentemp();
2872 error = got_error_from_errno("got_opentemp");
2876 f2 = got_opentemp();
2878 error = got_error_from_errno("got_opentemp");
2882 fd1 = got_opentempfd();
2884 error = got_error_from_errno("got_opentempfd");
2888 fd2 = got_opentempfd();
2890 error = got_error_from_errno("got_opentempfd");
2894 if (header->parent_id != NULL &&
2895 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2896 error = got_repo_match_object_id(&id1, &label1,
2897 header->parent_id, GOT_OBJ_TYPE_ANY,
2898 &header->refs, gw_trans->repo);
2903 error = got_repo_match_object_id(&id2, &label2,
2904 header->commit_id, GOT_OBJ_TYPE_ANY, &header->refs,
2909 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2913 case GOT_OBJ_TYPE_BLOB:
2914 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
2915 fd1, fd2, id1, id2, NULL, NULL, 3, 0, 0,
2918 case GOT_OBJ_TYPE_TREE:
2919 error = got_diff_objects_as_trees(NULL, NULL, f1, f2,
2920 fd1, fd2, id1, id2, NULL, "", "", 3, 0, 0,
2923 case GOT_OBJ_TYPE_COMMIT:
2924 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
2925 fd1, fd2, id1, id2, NULL, 3, 0, 0, gw_trans->repo, f);
2928 error = got_error(GOT_ERR_OBJ_TYPE);
2933 if (fseek(f, 0, SEEK_SET) == -1) {
2934 error = got_ferror(f, GOT_ERR_IO);
2938 while ((linelen = getline(&line, &linesize, f)) != -1) {
2939 error = gw_colordiff_line(gw_trans, line);
2942 /* XXX: KHTML_PRETTY breaks this */
2943 kerr = khtml_puts(gw_trans->gw_html_req, line);
2944 if (kerr != KCGI_OK)
2946 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2947 if (kerr != KCGI_OK)
2950 if (linelen == -1 && ferror(f))
2951 error = got_error_from_errno("getline");
2953 if (f && fclose(f) == EOF && error == NULL)
2954 error = got_error_from_errno("fclose");
2955 if (f1 && fclose(f1) == EOF && error == NULL)
2956 error = got_error_from_errno("fclose");
2957 if (f2 && fclose(f2) == EOF && error == NULL)
2958 error = got_error_from_errno("fclose");
2959 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
2960 error = got_error_from_errno("close");
2961 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
2962 error = got_error_from_errno("close");
2969 if (error == NULL && kerr != KCGI_OK)
2970 error = gw_kcgi_error(kerr);
2974 static const struct got_error *
2975 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2977 const struct got_error *error = NULL, *close_err;
2978 struct got_repository *repo;
2979 const char *gitconfig_owner;
2983 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2986 error = got_repo_open(&repo, dir, NULL, gw_trans->pack_fds);
2990 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2991 if (gitconfig_owner) {
2992 *owner = strdup(gitconfig_owner);
2994 error = got_error_from_errno("strdup");
2996 close_err = got_repo_close(repo);
3002 static const struct got_error *
3003 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
3005 const struct got_error *error = NULL;
3007 char *d_file = NULL;
3013 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
3014 return got_error_from_errno("asprintf");
3016 f = fopen(d_file, "re");
3018 if (errno != ENOENT && errno != EACCES)
3019 error = got_error_from_errno2("fopen", d_file);
3023 if (fseek(f, 0, SEEK_END) == -1) {
3024 error = got_ferror(f, GOT_ERR_IO);
3029 error = got_ferror(f, GOT_ERR_IO);
3032 if (fseek(f, 0, SEEK_SET) == -1) {
3033 error = got_ferror(f, GOT_ERR_IO);
3037 *url = calloc(len + 1, sizeof(**url));
3039 error = got_error_from_errno("calloc");
3043 n = fread(*url, 1, len, f);
3044 if (n == 0 && ferror(f))
3045 error = got_ferror(f, GOT_ERR_IO);
3047 if (f && fclose(f) == EOF && error == NULL)
3048 error = got_error_from_errno("fclose");
3053 static const struct got_error *
3054 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3055 int limit, int tag_type)
3057 const struct got_error *error = NULL;
3058 struct got_reflist_head refs;
3059 struct got_reflist_entry *re;
3061 char *id_str = NULL, *newline, *href_commits = NULL;
3062 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3063 struct got_tag_object *tag = NULL;
3064 enum kcgi_err kerr = KCGI_OK;
3065 int summary_header_displayed = 0, chk_next = 0;
3066 int tag_count = 0, commit_found = 0, c_cnt = 0;
3070 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3071 got_ref_cmp_tags, gw_trans->repo);
3075 TAILQ_FOREACH(re, &refs, entry) {
3076 const char *refname;
3078 const char *tag_commit;
3080 struct got_object_id *id;
3081 struct got_commit_object *commit = NULL;
3083 refname = got_ref_get_name(re->ref);
3084 if (strncmp(refname, "refs/tags/", 10) != 0)
3088 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3092 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3094 if (error->code != GOT_ERR_OBJ_TYPE) {
3098 /* "lightweight" tag */
3099 error = got_object_open_as_commit(&commit,
3100 gw_trans->repo, id);
3105 tagger = got_object_commit_get_committer(commit);
3107 got_object_commit_get_committer_time(commit);
3108 error = got_object_id_str(&id_str, id);
3112 tagger = got_object_tag_get_tagger(tag);
3113 tagger_time = got_object_tag_get_tagger_time(tag);
3114 error = got_object_id_str(&id_str,
3115 got_object_tag_get_object_id(tag));
3120 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3121 strlen(id_str)) != 0)
3124 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3125 commit_found == 0 && strncmp(id_str, gw_trans->commit_id,
3126 strlen(id_str)) != 0)
3134 gw_trans->next_id = strdup(id_str);
3135 if (gw_trans->next_id == NULL)
3136 error = got_error_from_errno("strdup");
3141 error = got_object_commit_get_logmsg(&tag_commit0,
3145 got_object_commit_close(commit);
3147 tag_commit0 = strdup(got_object_tag_get_message(tag));
3148 if (tag_commit0 == NULL) {
3149 error = got_error_from_errno("strdup");
3154 tag_commit = tag_commit0;
3155 while (*tag_commit == '\n')
3160 newline = strchr(tag_commit, '\n');
3164 if (summary_header_displayed == 0) {
3165 kerr = khtml_attr(gw_trans->gw_html_req,
3166 KELEM_DIV, KATTR_ID,
3167 "summary_tags_title_wrapper", KATTR__MAX);
3168 if (kerr != KCGI_OK)
3170 kerr = khtml_attr(gw_trans->gw_html_req,
3171 KELEM_DIV, KATTR_ID,
3172 "summary_tags_title", KATTR__MAX);
3173 if (kerr != KCGI_OK)
3175 kerr = khtml_puts(gw_trans->gw_html_req,
3177 if (kerr != KCGI_OK)
3179 kerr = khtml_closeelem(gw_trans->gw_html_req,
3181 if (kerr != KCGI_OK)
3183 kerr = khtml_attr(gw_trans->gw_html_req,
3184 KELEM_DIV, KATTR_ID,
3185 "summary_tags_content", KATTR__MAX);
3186 if (kerr != KCGI_OK)
3188 summary_header_displayed = 1;
3191 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3192 KATTR_ID, "tag_wrapper", KATTR__MAX);
3193 if (kerr != KCGI_OK)
3195 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3196 KATTR_ID, "tag_age", KATTR__MAX);
3197 if (kerr != KCGI_OK)
3199 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3202 kerr = khtml_puts(gw_trans->gw_html_req,
3204 if (kerr != KCGI_OK)
3206 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3207 if (kerr != KCGI_OK)
3209 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3210 KATTR_ID, "tag", KATTR__MAX);
3211 if (kerr != KCGI_OK)
3213 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3214 if (kerr != KCGI_OK)
3216 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3217 if (kerr != KCGI_OK)
3219 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3220 KATTR_ID, "tag_name", KATTR__MAX);
3221 if (kerr != KCGI_OK)
3224 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3225 gw_trans->repo_name, "action", "tag", "commit",
3227 if (href_tag == NULL) {
3228 error = got_error_from_errno("khttp_urlpart");
3231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3232 KATTR_HREF, href_tag, KATTR__MAX);
3233 if (kerr != KCGI_OK)
3235 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3236 if (kerr != KCGI_OK)
3238 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3239 if (kerr != KCGI_OK)
3242 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3243 KATTR_ID, "navs_wrapper", KATTR__MAX);
3244 if (kerr != KCGI_OK)
3246 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3247 KATTR_ID, "navs", KATTR__MAX);
3248 if (kerr != KCGI_OK)
3251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3252 KATTR_HREF, href_tag, KATTR__MAX);
3253 if (kerr != KCGI_OK)
3255 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3256 if (kerr != KCGI_OK)
3258 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3259 if (kerr != KCGI_OK)
3262 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3263 if (kerr != KCGI_OK)
3266 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3267 "path", gw_trans->repo_name, "action", "briefs",
3268 "commit", id_str, NULL);
3269 if (href_briefs == NULL) {
3270 error = got_error_from_errno("khttp_urlpart");
3273 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3274 KATTR_HREF, href_briefs, KATTR__MAX);
3275 if (kerr != KCGI_OK)
3277 kerr = khtml_puts(gw_trans->gw_html_req,
3279 if (kerr != KCGI_OK)
3281 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3282 if (kerr != KCGI_OK)
3285 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3286 if (kerr != KCGI_OK)
3289 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3290 "path", gw_trans->repo_name, "action", "commits",
3291 "commit", id_str, NULL);
3292 if (href_commits == NULL) {
3293 error = got_error_from_errno("khttp_urlpart");
3296 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3297 KATTR_HREF, href_commits, KATTR__MAX);
3298 if (kerr != KCGI_OK)
3300 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3301 if (kerr != KCGI_OK)
3303 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3304 if (kerr != KCGI_OK)
3307 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3308 KATTR_ID, "dotted_line", KATTR__MAX);
3309 if (kerr != KCGI_OK)
3311 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3312 if (kerr != KCGI_OK)
3316 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3317 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3318 if (kerr != KCGI_OK)
3320 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3321 if (kerr != KCGI_OK)
3323 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3324 if (kerr != KCGI_OK)
3326 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3327 KATTR_ID, "tag_info_date", KATTR__MAX);
3328 if (kerr != KCGI_OK)
3330 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3333 kerr = khtml_puts(gw_trans->gw_html_req,
3335 if (kerr != KCGI_OK)
3337 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3338 if (kerr != KCGI_OK)
3341 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3342 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3343 if (kerr != KCGI_OK)
3345 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3346 if (kerr != KCGI_OK)
3348 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3349 if (kerr != KCGI_OK)
3351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3352 KATTR_ID, "tag_info_date", KATTR__MAX);
3353 if (kerr != KCGI_OK)
3355 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3356 if (kerr != KCGI_OK)
3358 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3359 if (kerr != KCGI_OK)
3362 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3363 KATTR_ID, "tag_info", KATTR__MAX);
3364 if (kerr != KCGI_OK)
3366 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3367 if (kerr != KCGI_OK)
3373 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3374 if (kerr != KCGI_OK)
3377 if (limit && --limit == 0)
3381 got_object_tag_close(tag);
3394 href_commits = NULL;
3396 if (tag_count == 0) {
3397 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3398 "summary_tags_title_wrapper", KATTR__MAX);
3399 if (kerr != KCGI_OK)
3401 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3402 "summary_tags_title", KATTR__MAX);
3403 if (kerr != KCGI_OK)
3405 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3406 if (kerr != KCGI_OK)
3408 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3409 if (kerr != KCGI_OK)
3411 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3412 "summary_tags_content", KATTR__MAX);
3413 if (kerr != KCGI_OK)
3415 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3416 "tags_info", KATTR__MAX);
3417 if (kerr != KCGI_OK)
3419 kerr = khttp_puts(gw_trans->gw_req,
3420 "There are no tags for this repo.");
3421 if (kerr != KCGI_OK)
3423 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3428 TAILQ_FOREACH_REVERSE(re, &refs, got_reflist_head, entry) {
3429 const char *refname;
3430 struct got_object_id *id;
3431 struct got_commit_object *commit = NULL;
3433 refname = got_ref_get_name(re->ref);
3434 if (strncmp(refname, "refs/tags/", 10) != 0)
3438 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3442 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3444 if (error->code != GOT_ERR_OBJ_TYPE) {
3448 /* "lightweight" tag */
3449 error = got_object_open_as_commit(&commit,
3450 gw_trans->repo, id);
3455 error = got_object_id_str(&id_str, id);
3459 error = got_object_id_str(&id_str,
3460 got_object_tag_get_object_id(tag));
3465 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3466 strlen(id_str)) != 0)
3469 if (commit_found == 0 && tag_type == TAGBRIEF &&
3470 gw_trans->commit_id != NULL &&
3471 strncmp(id_str, gw_trans->commit_id, strlen(id_str)) != 0)
3476 if (gw_trans->commit_id != NULL &&
3477 strcmp(id_str, gw_trans->commit_id) != 0 &&
3478 (re == TAILQ_FIRST(&refs) ||
3479 c_cnt == gw_trans->gw_conf->got_max_commits_display)) {
3480 gw_trans->prev_id = strdup(id_str);
3481 if (gw_trans->prev_id == NULL) {
3482 error = got_error_from_errno("strdup");
3491 got_object_tag_close(tag);
3498 got_ref_list_free(&refs);
3499 if (error == NULL && kerr != KCGI_OK)
3500 error = gw_kcgi_error(kerr);
3505 gw_free_header(struct gw_header *header)
3508 free(header->author);
3509 free(header->committer);
3510 free(header->refs_str);
3511 free(header->commit_id);
3512 free(header->parent_id);
3513 free(header->tree_id);
3514 free(header->commit_msg);
3517 static struct gw_header *
3520 struct gw_header *header;
3522 header = malloc(sizeof(*header));
3526 header->path = NULL;
3527 TAILQ_INIT(&header->refs);
3529 header->refs_str = NULL;
3530 header->commit_id = NULL;
3531 header->committer = NULL;
3532 header->author = NULL;
3533 header->parent_id = NULL;
3534 header->tree_id = NULL;
3535 header->commit_msg = NULL;
3540 static const struct got_error *
3541 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3542 int limit, struct got_object_id *id)
3544 const struct got_error *error = NULL;
3545 struct got_commit_graph *graph = NULL;
3546 struct got_commit_object *commit = NULL;
3547 int chk_next = 0, chk_multi = 0, c_cnt = 0, commit_found = 0;
3548 struct gw_header *t_header = NULL;
3550 error = got_commit_graph_open(&graph, header->path, 0);
3554 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3560 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3563 if (error->code == GOT_ERR_ITER_COMPLETED)
3570 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3573 if (limit == 1 && chk_multi == 0 &&
3574 gw_trans->gw_conf->got_max_commits_display != 1) {
3575 error = gw_get_commit(gw_trans, header, commit, id);
3581 struct gw_header *n_header = NULL;
3582 if ((n_header = gw_init_header()) == NULL) {
3583 error = got_error_from_errno("malloc");
3586 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3588 error = got_ref_list(&n_header->refs, gw_trans->repo,
3589 NULL, got_ref_cmp_by_name, NULL);
3593 error = gw_get_commit(gw_trans, n_header, commit, id);
3596 got_ref_list_free(&n_header->refs);
3598 if (gw_trans->commit_id != NULL) {
3599 if (strcmp(gw_trans->commit_id,
3600 n_header->commit_id) == 0)
3606 * check for one more commit before breaking,
3607 * so we know whether to navigate through gw_briefs
3608 * gw_commits and gw_summary
3610 if (chk_next && (gw_trans->action == GW_BRIEFS ||
3611 gw_trans->action == GW_COMMITS ||
3612 gw_trans->action == GW_SUMMARY)) {
3613 gw_trans->next_id = strdup(n_header->commit_id);
3614 if (gw_trans->next_id == NULL)
3615 error = got_error_from_errno("strdup");
3616 TAILQ_REMOVE(&gw_trans->gw_headers, n_header,
3622 if (commit_found == 1 && (error || (limit && --limit == 0))) {
3629 if (gw_trans->prev_id == NULL && gw_trans->commit_id != NULL &&
3630 (gw_trans->action == GW_BRIEFS || gw_trans->action == GW_COMMITS)) {
3632 TAILQ_FOREACH_REVERSE(t_header, &gw_trans->gw_headers,
3634 if (commit_found == 0 &&
3635 strcmp(gw_trans->commit_id,
3636 t_header->commit_id) != 0)
3640 if (gw_trans->commit_id != NULL &&
3641 strcmp(gw_trans->commit_id,
3642 t_header->commit_id) != 0 &&
3643 (c_cnt == gw_trans->gw_conf->got_max_commits_display
3645 TAILQ_FIRST(&gw_trans->gw_headers))) {
3646 gw_trans->prev_id = strdup(t_header->commit_id);
3647 if (gw_trans->prev_id == NULL)
3648 error = got_error_from_errno("strdup");
3656 got_object_commit_close(commit);
3658 got_commit_graph_close(graph);
3662 static const struct got_error *
3663 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3664 struct got_commit_object *commit, struct got_object_id *id)
3666 const struct got_error *error = NULL;
3667 struct got_reflist_entry *re;
3668 struct got_object_id *id2 = NULL;
3669 struct got_object_qid *parent_id;
3670 char *commit_msg = NULL, *commit_msg0;
3673 TAILQ_FOREACH(re, &header->refs, entry) {
3676 struct got_tag_object *tag = NULL;
3677 struct got_object_id *ref_id;
3680 if (got_ref_is_symbolic(re->ref))
3683 name = got_ref_get_name(re->ref);
3684 if (strncmp(name, "refs/", 5) == 0)
3686 if (strncmp(name, "got/", 4) == 0)
3688 if (strncmp(name, "heads/", 6) == 0)
3690 if (strncmp(name, "remotes/", 8) == 0) {
3692 s = strstr(name, "/" GOT_REF_HEAD);
3693 if (s != NULL && s[strlen(s)] == '\0')
3696 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3699 if (strncmp(name, "tags/", 5) == 0) {
3700 error = got_object_open_as_tag(&tag, gw_trans->repo,
3703 if (error->code != GOT_ERR_OBJ_TYPE) {
3708 * Ref points at something other
3715 cmp = got_object_id_cmp(tag ?
3716 got_object_tag_get_object_id(tag) : ref_id, id);
3719 got_object_tag_close(tag);
3722 s = header->refs_str;
3723 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3724 s ? ", " : "", name) == -1) {
3725 error = got_error_from_errno("asprintf");
3727 header->refs_str = NULL;
3733 error = got_object_id_str(&header->commit_id, id);
3737 error = got_object_id_str(&header->tree_id,
3738 got_object_commit_get_tree_id(commit));
3742 if (gw_trans->action == GW_DIFF) {
3743 parent_id = STAILQ_FIRST(
3744 got_object_commit_get_parent_ids(commit));
3745 if (parent_id != NULL) {
3746 id2 = got_object_id_dup(&parent_id->id);
3748 error = got_object_id_str(&header->parent_id, id2);
3753 header->parent_id = strdup("/dev/null");
3754 if (header->parent_id == NULL) {
3755 error = got_error_from_errno("strdup");
3761 header->committer_time =
3762 got_object_commit_get_committer_time(commit);
3765 strdup(got_object_commit_get_author(commit));
3766 if (header->author == NULL) {
3767 error = got_error_from_errno("strdup");
3771 strdup(got_object_commit_get_committer(commit));
3772 if (header->committer == NULL) {
3773 error = got_error_from_errno("strdup");
3776 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3780 commit_msg = commit_msg0;
3781 while (*commit_msg == '\n')
3784 header->commit_msg = strdup(commit_msg);
3785 if (header->commit_msg == NULL)
3786 error = got_error_from_errno("strdup");
3791 static const struct got_error *
3792 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3794 const struct got_error *error = NULL;
3795 char *in_repo_path = NULL;
3796 struct got_object_id *id = NULL;
3797 struct got_reference *ref;
3799 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL,
3800 gw_trans->pack_fds);
3804 if (gw_trans->commit_id == NULL || gw_trans->action == GW_COMMITS ||
3805 gw_trans->action == GW_BRIEFS || gw_trans->action == GW_SUMMARY ||
3806 gw_trans->action == GW_TAGS) {
3807 error = got_ref_open(&ref, gw_trans->repo,
3808 gw_trans->headref, 0);
3812 error = got_ref_resolve(&id, gw_trans->repo, ref);
3817 error = got_ref_open(&ref, gw_trans->repo,
3818 gw_trans->commit_id, 0);
3819 if (error == NULL) {
3821 error = got_ref_resolve(&id, gw_trans->repo, ref);
3825 error = got_object_get_type(&obj_type, gw_trans->repo,
3829 if (obj_type == GOT_OBJ_TYPE_TAG) {
3830 struct got_tag_object *tag;
3831 error = got_object_open_as_tag(&tag,
3832 gw_trans->repo, id);
3835 if (got_object_tag_get_object_type(tag) !=
3836 GOT_OBJ_TYPE_COMMIT) {
3837 got_object_tag_close(tag);
3838 error = got_error(GOT_ERR_OBJ_TYPE);
3842 id = got_object_id_dup(
3843 got_object_tag_get_object_id(tag));
3845 error = got_error_from_errno(
3846 "got_object_id_dup");
3847 got_object_tag_close(tag);
3850 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3851 error = got_error(GOT_ERR_OBJ_TYPE);
3855 error = got_repo_match_object_id_prefix(&id,
3856 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3862 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3863 gw_trans->repo_path);
3868 header->path = strdup(in_repo_path);
3869 if (header->path == NULL) {
3870 error = got_error_from_errno("strdup");
3875 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3876 got_ref_cmp_by_name, NULL);
3880 error = gw_get_commits(gw_trans, header, limit, id);
3891 char datebuf[11]; /* YYYY-MM-DD + NUL */
3894 struct gw_blame_cb_args {
3895 struct blame_line *lines;
3899 off_t *line_offsets;
3901 struct got_repository *repo;
3902 struct gw_trans *gw_trans;
3905 static const struct got_error *
3906 gw_blame_cb(void *arg, int nlines, int lineno,
3907 struct got_commit_object *commit, struct got_object_id *id)
3909 const struct got_error *err = NULL;
3910 struct gw_blame_cb_args *a = arg;
3911 struct blame_line *bline;
3913 size_t linesize = 0;
3916 time_t committer_time;
3917 enum kcgi_err kerr = KCGI_OK;
3919 if (nlines != a->nlines ||
3920 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3921 return got_error(GOT_ERR_RANGE);
3924 return NULL; /* no change in this commit */
3926 /* Annotate this line. */
3927 bline = &a->lines[lineno - 1];
3928 if (bline->annotated)
3930 err = got_object_id_str(&bline->id_str, id);
3934 bline->committer = strdup(got_object_commit_get_committer(commit));
3935 if (bline->committer == NULL) {
3936 err = got_error_from_errno("strdup");
3940 committer_time = got_object_commit_get_committer_time(commit);
3941 if (gmtime_r(&committer_time, &tm) == NULL)
3942 return got_error_from_errno("gmtime_r");
3943 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3945 err = got_error(GOT_ERR_NO_SPACE);
3948 bline->annotated = 1;
3950 /* Print lines annotated so far. */
3951 bline = &a->lines[a->lineno_cur - 1];
3952 if (!bline->annotated)
3955 offset = a->line_offsets[a->lineno_cur - 1];
3956 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3957 err = got_error_from_errno("fseeko");
3961 while (bline->annotated) {
3962 char *smallerthan, *at, *nl, *committer;
3963 char *href_diff = NULL;
3966 if (getline(&line, &linesize, a->f) == -1) {
3968 err = got_error_from_errno("getline");
3972 committer = bline->committer;
3973 smallerthan = strchr(committer, '<');
3974 if (smallerthan && smallerthan[1] != '\0')
3975 committer = smallerthan + 1;
3976 at = strchr(committer, '@');
3979 len = strlen(committer);
3981 committer[8] = '\0';
3983 nl = strchr(line, '\n');
3987 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3988 "blame_wrapper", KATTR__MAX);
3989 if (kerr != KCGI_OK)
3991 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3992 "blame_number", KATTR__MAX);
3993 if (kerr != KCGI_OK)
3995 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3996 a->nlines_prec, a->lineno_cur);
3997 if (kerr != KCGI_OK)
3999 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4000 if (kerr != KCGI_OK)
4003 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4004 "blame_hash", KATTR__MAX);
4005 if (kerr != KCGI_OK)
4008 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
4009 a->gw_trans->repo_name, "action", "diff", "commit",
4010 bline->id_str, NULL);
4011 if (href_diff == NULL) {
4012 err = got_error_from_errno("khttp_urlpart");
4015 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
4016 KATTR_HREF, href_diff, KATTR__MAX);
4017 if (kerr != KCGI_OK)
4019 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
4021 if (kerr != KCGI_OK)
4023 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
4024 if (kerr != KCGI_OK)
4027 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4028 "blame_date", KATTR__MAX);
4029 if (kerr != KCGI_OK)
4031 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
4032 if (kerr != KCGI_OK)
4034 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4035 if (kerr != KCGI_OK)
4038 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4039 "blame_author", KATTR__MAX);
4040 if (kerr != KCGI_OK)
4042 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
4043 if (kerr != KCGI_OK)
4045 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4046 if (kerr != KCGI_OK)
4049 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4050 "blame_code", KATTR__MAX);
4051 if (kerr != KCGI_OK)
4053 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
4054 if (kerr != KCGI_OK)
4056 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4057 if (kerr != KCGI_OK)
4060 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4061 if (kerr != KCGI_OK)
4065 bline = &a->lines[a->lineno_cur - 1];
4071 if (err == NULL && kerr != KCGI_OK)
4072 err = gw_kcgi_error(kerr);
4076 static const struct got_error *
4077 gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
4079 const struct got_error *error = NULL;
4080 struct got_object_id *obj_id = NULL;
4081 struct got_object_id *commit_id = NULL;
4082 struct got_commit_object *commit = NULL;
4083 struct got_blob_object *blob = NULL;
4084 char *path = NULL, *in_repo_path = NULL;
4085 struct gw_blame_cb_args bca;
4086 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1;
4088 FILE *f1 = NULL, *f2 = NULL;
4090 fd1 = got_opentempfd();
4092 return got_error_from_errno("got_opentempfd");
4093 fd2 = got_opentempfd();
4095 error = got_error_from_errno("got_opentempfd");
4098 fd3 = got_opentempfd();
4100 error = got_error_from_errno("got_opentempfd");
4104 memset(&bca, 0, sizeof(bca));
4106 if (asprintf(&path, "%s%s%s",
4107 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4108 gw_trans->repo_folder ? "/" : "",
4109 gw_trans->repo_file) == -1) {
4110 error = got_error_from_errno("asprintf");
4114 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4118 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4119 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4123 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4127 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit,
4132 if (obj_id == NULL) {
4133 error = got_error(GOT_ERR_NO_OBJ);
4137 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4141 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4142 error = got_error(GOT_ERR_OBJ_TYPE);
4146 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192,
4151 bca.f = got_opentemp();
4152 if (bca.f == NULL) {
4153 error = got_error_from_errno("got_opentemp");
4156 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4157 &bca.line_offsets, bca.f, blob);
4158 if (error || bca.nlines == 0)
4161 /* Don't include \n at EOF in the blame line count. */
4162 if (bca.line_offsets[bca.nlines - 1] == filesize)
4165 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4166 if (bca.lines == NULL) {
4167 error = got_error_from_errno("calloc");
4171 bca.nlines_prec = 0;
4177 bca.repo = gw_trans->repo;
4178 bca.gw_trans = gw_trans;
4180 fd1 = got_opentempfd();
4182 error = got_error_from_errno("got_opentempfd");
4186 f1 = got_opentemp();
4188 error = got_error_from_errno("got_opentempfd");
4191 f2 = got_opentemp();
4193 error = got_error_from_errno("got_opentempfd");
4197 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4198 &bca, NULL, NULL, fd2, fd3, f1, f2);
4205 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
4206 error = got_error_from_errno("close");
4207 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
4208 error = got_error_from_errno("close");
4209 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
4210 error = got_error_from_errno("close");
4211 if (f1 && fclose(f1) == EOF && error == NULL)
4212 error = got_error_from_errno("fclose");
4213 if (f2 && fclose(f2) == EOF && error == NULL)
4214 error = got_error_from_errno("fclose");
4217 free(bca.line_offsets);
4218 for (i = 0; i < bca.nlines; i++) {
4219 struct blame_line *bline = &bca.lines[i];
4220 free(bline->id_str);
4221 free(bline->committer);
4224 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4225 error = got_error_from_errno("fclose");
4228 got_object_blob_close(blob);
4230 got_object_commit_close(commit);
4234 static const struct got_error *
4235 gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
4237 const struct got_error *error = NULL;
4238 struct got_object_id *obj_id = NULL;
4239 struct got_object_id *commit_id = NULL;
4240 struct got_commit_object *commit = NULL;
4241 struct got_blob_object *blob = NULL;
4242 char *path = NULL, *in_repo_path = NULL;
4243 int obj_type, set_mime = 0, fd = -1;
4246 enum kcgi_err kerr = KCGI_OK;
4248 fd = got_opentempfd();
4250 return got_error_from_errno("got_opentempfd");
4252 if (asprintf(&path, "%s%s%s",
4253 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4254 gw_trans->repo_folder ? "/" : "",
4255 gw_trans->repo_file) == -1) {
4256 error = got_error_from_errno("asprintf");
4260 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4264 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4265 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4269 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4273 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit,
4278 if (obj_id == NULL) {
4279 error = got_error(GOT_ERR_NO_OBJ);
4283 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4287 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4288 error = got_error(GOT_ERR_OBJ_TYPE);
4292 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192,
4297 hdrlen = got_object_blob_get_hdrlen(blob);
4299 error = got_object_blob_read_block(&len, blob);
4302 buf = got_object_blob_get_read_buf(blob);
4305 * Skip blob object header first time around,
4306 * which also contains a zero byte.
4309 if (set_mime == 0) {
4310 if (isbinary(buf, len - hdrlen))
4311 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4313 gw_trans->mime = KMIME_TEXT_PLAIN;
4315 error = gw_display_index(gw_trans);
4319 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4320 if (kerr != KCGI_OK)
4329 if (fd != -1 && close(fd) == -1 && error == NULL)
4330 error = got_error_from_errno("close");
4332 got_object_blob_close(blob);
4334 got_object_commit_close(commit);
4335 if (error == NULL && kerr != KCGI_OK)
4336 error = gw_kcgi_error(kerr);
4340 static const struct got_error *
4341 gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
4343 const struct got_error *error = NULL;
4344 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4345 struct got_tree_object *tree = NULL;
4346 struct got_commit_object *commit = NULL;
4347 char *path = NULL, *in_repo_path = NULL;
4348 char *id_str = NULL;
4349 char *build_folder = NULL;
4350 char *href_blob = NULL, *href_blame = NULL;
4351 const char *class = NULL;
4352 int nentries, i, class_flip = 0;
4353 enum kcgi_err kerr = KCGI_OK;
4355 if (gw_trans->repo_folder != NULL) {
4356 path = strdup(gw_trans->repo_folder);
4358 error = got_error_from_errno("strdup");
4362 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4363 gw_trans->repo_path);
4367 path = in_repo_path;
4370 if (gw_trans->commit_id == NULL) {
4371 struct got_reference *head_ref;
4372 error = got_ref_open(&head_ref, gw_trans->repo,
4373 gw_trans->headref, 0);
4376 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4379 got_ref_close(head_ref);
4381 * gw_trans->commit_id was not parsed from the querystring
4382 * we hit this code path from gw_index, where we don't know the
4383 * commit values for the tree link yet, so set
4384 * gw_trans->commit_id here to continue further into the tree
4386 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4391 error = got_repo_match_object_id(&commit_id, NULL,
4392 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
4398 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4402 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit,
4407 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4411 nentries = got_object_tree_get_nentries(tree);
4412 for (i = 0; i < nentries; i++) {
4413 struct got_tree_entry *te;
4414 const char *modestr = "";
4417 te = got_object_tree_get_entry(tree, i);
4419 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4423 mode = got_tree_entry_get_mode(te);
4424 if (got_object_tree_entry_is_submodule(te))
4426 else if (S_ISLNK(mode))
4428 else if (S_ISDIR(mode))
4430 else if (mode & S_IXUSR)
4433 if (class_flip == 0) {
4434 class = "back_lightgray";
4437 class = "back_white";
4441 if (S_ISDIR(mode)) {
4442 if (asprintf(&build_folder, "%s/%s",
4443 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4444 got_tree_entry_get_name(te)) == -1) {
4445 error = got_error_from_errno("asprintf");
4449 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4450 gw_trans->repo_name, "action",
4451 gw_get_action_name(gw_trans), "commit",
4452 gw_trans->commit_id, "folder", build_folder, NULL);
4453 if (href_blob == NULL) {
4454 error = got_error_from_errno("khttp_urlpart");
4457 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4458 KATTR_ID, "tree_wrapper", KATTR__MAX);
4459 if (kerr != KCGI_OK)
4461 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4462 KATTR_ID, "tree_line", KATTR_CLASS, class,
4464 if (kerr != KCGI_OK)
4466 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4467 KATTR_HREF, href_blob, KATTR_CLASS,
4468 "diff_directory", KATTR__MAX);
4469 if (kerr != KCGI_OK)
4471 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4472 got_tree_entry_get_name(te), modestr);
4473 if (kerr != KCGI_OK)
4475 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4476 if (kerr != KCGI_OK)
4478 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4479 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4481 if (kerr != KCGI_OK)
4483 kerr = khtml_entity(gw_trans->gw_html_req,
4485 if (kerr != KCGI_OK)
4487 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4488 if (kerr != KCGI_OK)
4491 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4492 gw_trans->repo_name, "action", "blob", "commit",
4493 gw_trans->commit_id, "file",
4494 got_tree_entry_get_name(te), "folder",
4495 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4497 if (href_blob == NULL) {
4498 error = got_error_from_errno("khttp_urlpart");
4501 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4502 gw_trans->repo_name, "action", "blame", "commit",
4503 gw_trans->commit_id, "file",
4504 got_tree_entry_get_name(te), "folder",
4505 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4507 if (href_blame == NULL) {
4508 error = got_error_from_errno("khttp_urlpart");
4511 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4512 KATTR_ID, "tree_wrapper", KATTR__MAX);
4513 if (kerr != KCGI_OK)
4515 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4516 KATTR_ID, "tree_line", KATTR_CLASS, class,
4518 if (kerr != KCGI_OK)
4520 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4521 KATTR_HREF, href_blob, KATTR__MAX);
4522 if (kerr != KCGI_OK)
4524 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4525 got_tree_entry_get_name(te), modestr);
4526 if (kerr != KCGI_OK)
4528 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4529 if (kerr != KCGI_OK)
4531 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4532 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4534 if (kerr != KCGI_OK)
4537 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4538 KATTR_HREF, href_blob, KATTR__MAX);
4539 if (kerr != KCGI_OK)
4541 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4542 if (kerr != KCGI_OK)
4544 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4545 if (kerr != KCGI_OK)
4548 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4549 if (kerr != KCGI_OK)
4552 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4553 KATTR_HREF, href_blame, KATTR__MAX);
4554 if (kerr != KCGI_OK)
4556 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4557 if (kerr != KCGI_OK)
4560 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4561 if (kerr != KCGI_OK)
4569 build_folder = NULL;
4573 got_object_tree_close(tree);
4575 got_object_commit_close(commit);
4582 if (error == NULL && kerr != KCGI_OK)
4583 error = gw_kcgi_error(kerr);
4587 static const struct got_error *
4588 gw_output_repo_heads(struct gw_trans *gw_trans)
4590 const struct got_error *error = NULL;
4591 struct got_reflist_head refs;
4592 struct got_reflist_entry *re;
4593 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4594 char *href_commits = NULL;
4595 enum kcgi_err kerr = KCGI_OK;
4599 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4600 got_ref_cmp_by_name, NULL);
4604 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4605 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4606 if (kerr != KCGI_OK)
4608 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4609 KATTR_ID, "summary_heads_title", KATTR__MAX);
4610 if (kerr != KCGI_OK)
4612 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4613 if (kerr != KCGI_OK)
4615 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4616 if (kerr != KCGI_OK)
4618 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4619 KATTR_ID, "summary_heads_content", KATTR__MAX);
4620 if (kerr != KCGI_OK)
4623 TAILQ_FOREACH(re, &refs, entry) {
4624 const char *refname;
4626 if (got_ref_is_symbolic(re->ref))
4629 refname = got_ref_get_name(re->ref);
4630 if (strncmp(refname, "refs/heads/", 11) != 0)
4633 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4638 if (strncmp(refname, "refs/heads/", 11) == 0)
4641 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4642 KATTR_ID, "heads_wrapper", KATTR__MAX);
4643 if (kerr != KCGI_OK)
4645 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4646 KATTR_ID, "heads_age", KATTR__MAX);
4647 if (kerr != KCGI_OK)
4649 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4650 if (kerr != KCGI_OK)
4652 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4653 if (kerr != KCGI_OK)
4655 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4656 KATTR_ID, "heads_space", KATTR__MAX);
4657 if (kerr != KCGI_OK)
4659 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4660 if (kerr != KCGI_OK)
4662 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4663 if (kerr != KCGI_OK)
4665 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4666 KATTR_ID, "head", KATTR__MAX);
4667 if (kerr != KCGI_OK)
4670 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4671 gw_trans->repo_name, "action", "summary", "headref",
4673 if (href_summary == NULL) {
4674 error = got_error_from_errno("khttp_urlpart");
4677 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4678 href_summary, KATTR__MAX);
4679 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4680 if (kerr != KCGI_OK)
4682 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4683 if (kerr != KCGI_OK)
4686 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4687 "navs_wrapper", KATTR__MAX);
4688 if (kerr != KCGI_OK)
4690 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4691 "navs", KATTR__MAX);
4692 if (kerr != KCGI_OK)
4695 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4696 href_summary, KATTR__MAX);
4697 if (kerr != KCGI_OK)
4699 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4700 if (kerr != KCGI_OK)
4702 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4703 if (kerr != KCGI_OK)
4706 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4707 if (kerr != KCGI_OK)
4710 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4711 gw_trans->repo_name, "action", "briefs", "headref",
4713 if (href_briefs == NULL) {
4714 error = got_error_from_errno("khttp_urlpart");
4717 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4718 href_briefs, KATTR__MAX);
4719 if (kerr != KCGI_OK)
4721 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4722 if (kerr != KCGI_OK)
4724 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4725 if (kerr != KCGI_OK)
4728 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4729 if (kerr != KCGI_OK)
4732 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4733 gw_trans->repo_name, "action", "commits", "headref",
4735 if (href_commits == NULL) {
4736 error = got_error_from_errno("khttp_urlpart");
4739 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4740 href_commits, KATTR__MAX);
4741 if (kerr != KCGI_OK)
4743 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4744 if (kerr != KCGI_OK)
4746 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4747 if (kerr != KCGI_OK)
4750 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4751 "dotted_line", KATTR__MAX);
4752 if (kerr != KCGI_OK)
4754 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4755 if (kerr != KCGI_OK)
4758 href_summary = NULL;
4762 href_commits = NULL;
4765 got_ref_list_free(&refs);
4772 static const struct got_error *
4773 gw_output_site_link(struct gw_trans *gw_trans)
4775 const struct got_error *error = NULL;
4776 char *href_summary = NULL;
4777 enum kcgi_err kerr = KCGI_OK;
4779 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4780 "site_link", KATTR__MAX);
4781 if (kerr != KCGI_OK)
4783 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4785 if (kerr != KCGI_OK)
4787 kerr = khtml_puts(gw_trans->gw_html_req,
4788 gw_trans->gw_conf->got_site_link);
4789 if (kerr != KCGI_OK)
4791 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4792 if (kerr != KCGI_OK)
4795 if (gw_trans->repo_name != NULL) {
4796 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4797 if (kerr != KCGI_OK)
4800 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4801 gw_trans->repo_name, "action", "summary", NULL);
4802 if (href_summary == NULL) {
4803 error = got_error_from_errno("khttp_urlpart");
4806 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4807 href_summary, KATTR__MAX);
4808 if (kerr != KCGI_OK)
4810 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4811 if (kerr != KCGI_OK)
4813 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4814 if (kerr != KCGI_OK)
4816 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4817 gw_get_action_name(gw_trans));
4818 if (kerr != KCGI_OK)
4822 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4823 if (kerr != KCGI_OK)
4827 if (error == NULL && kerr != KCGI_OK)
4828 error = gw_kcgi_error(kerr);
4832 static const struct got_error *
4833 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4835 const struct got_error *error = NULL;
4836 const char *color = NULL;
4837 enum kcgi_err kerr = KCGI_OK;
4839 if (strncmp(buf, "-", 1) == 0)
4840 color = "diff_minus";
4841 else if (strncmp(buf, "+", 1) == 0)
4842 color = "diff_plus";
4843 else if (strncmp(buf, "@@", 2) == 0)
4844 color = "diff_chunk_header";
4845 else if (strncmp(buf, "@@", 2) == 0)
4846 color = "diff_chunk_header";
4847 else if (strncmp(buf, "commit +", 8) == 0)
4848 color = "diff_meta";
4849 else if (strncmp(buf, "commit -", 8) == 0)
4850 color = "diff_meta";
4851 else if (strncmp(buf, "blob +", 6) == 0)
4852 color = "diff_meta";
4853 else if (strncmp(buf, "blob -", 6) == 0)
4854 color = "diff_meta";
4855 else if (strncmp(buf, "file +", 6) == 0)
4856 color = "diff_meta";
4857 else if (strncmp(buf, "file -", 6) == 0)
4858 color = "diff_meta";
4859 else if (strncmp(buf, "from:", 5) == 0)
4860 color = "diff_author";
4861 else if (strncmp(buf, "via:", 4) == 0)
4862 color = "diff_author";
4863 else if (strncmp(buf, "date:", 5) == 0)
4864 color = "diff_date";
4865 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4866 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4867 if (error == NULL && kerr != KCGI_OK)
4868 error = gw_kcgi_error(kerr);
4873 main(int argc, char *argv[])
4875 const struct got_error *error = NULL, *error2 = NULL;
4876 struct gw_trans *gw_trans;
4877 struct gw_dir *dir = NULL, *tdir;
4878 const char *page = "index";
4879 enum kcgi_err kerr = KCGI_OK;
4881 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4884 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4887 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4890 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4893 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4894 if (kerr != KCGI_OK) {
4895 error = gw_kcgi_error(kerr);
4899 TAILQ_INIT(&gw_trans->gw_dirs);
4900 TAILQ_INIT(&gw_trans->gw_headers);
4902 gw_trans->action = -1;
4904 gw_trans->repos_total = 0;
4905 gw_trans->repo_path = NULL;
4906 gw_trans->commit_id = NULL;
4907 gw_trans->next_id = NULL;
4908 gw_trans->prev_id = NULL;
4909 gw_trans->headref = GOT_REF_HEAD;
4910 gw_trans->mime = KMIME_TEXT_HTML;
4911 gw_trans->gw_tmpl->key = gw_templs;
4912 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4913 gw_trans->gw_tmpl->arg = gw_trans;
4914 gw_trans->gw_tmpl->cb = gw_template;
4916 error = got_repo_pack_fds_open(&gw_trans->pack_fds);
4920 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4924 error = gw_parse_querystring(gw_trans);
4928 if (gw_trans->repo) {
4929 const struct got_error *close_err;
4930 close_err = got_repo_close(gw_trans->repo);
4934 if (gw_trans->action == GW_BLOB)
4935 error = gw_blob(gw_trans);
4937 error = gw_display_index(gw_trans);
4940 gw_trans->error = error;
4941 gw_trans->action = GW_ERR;
4942 error2 = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
4944 goto cleanup; /* we can't display an error page */
4945 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
4946 if (kerr != KCGI_OK)
4947 goto cleanup; /* we can't display an error page */
4948 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
4949 gw_query_funcs[gw_trans->action].template);
4950 if (kerr != KCGI_OK) {
4951 khtml_close(gw_trans->gw_html_req);
4952 goto cleanup; /* we can't display an error page */
4957 if (gw_trans->pack_fds) {
4958 const struct got_error *pack_err =
4959 got_repo_pack_fds_close(gw_trans->pack_fds);
4962 gw_trans->pack_fds = NULL;
4964 free(gw_trans->gw_conf->got_repos_path);
4965 free(gw_trans->gw_conf->got_www_path);
4966 free(gw_trans->gw_conf->got_site_name);
4967 free(gw_trans->gw_conf->got_site_owner);
4968 free(gw_trans->gw_conf->got_site_link);
4969 free(gw_trans->gw_conf->got_logo);
4970 free(gw_trans->gw_conf->got_logo_url);
4971 free(gw_trans->gw_conf);
4972 free(gw_trans->commit_id);
4973 free(gw_trans->next_id);
4974 free(gw_trans->prev_id);
4975 free(gw_trans->repo_path);
4976 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4978 free(dir->description);
4985 khttp_free(gw_trans->gw_req);