2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
20 #include <sys/types.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct got_repository *repo;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
64 const struct got_error *error;
65 const char *repo_name;
72 const char *repo_file;
77 unsigned int repos_total;
82 TAILQ_ENTRY(gw_header) entry;
83 struct got_reflist_head refs;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
93 time_t committer_time;
97 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" },
159 { kvalid_stringne, "prev_prev" },
162 static struct gw_header *gw_init_header(void);
164 static void gw_free_header(struct gw_header *);
166 static int gw_template(size_t, void *);
168 static const struct got_error *gw_error(struct gw_trans *);
169 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
170 static const struct got_error *gw_get_repo_description(char **,
171 struct gw_trans *, char *);
172 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
174 static const struct got_error *gw_get_time_str(char **, time_t, int);
175 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
176 char *, const char *, int);
177 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 *);
179 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
180 static const struct got_error *gw_output_diff(struct gw_trans *,
182 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
183 struct gw_header *, int, int);
184 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
185 static const struct got_error *gw_output_site_link(struct gw_trans *);
186 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
188 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
190 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
192 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
194 static const struct got_error *gw_gen_author_header(struct gw_trans *,
196 static const struct got_error *gw_gen_age_header(struct gw_trans *,
198 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
200 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
202 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
203 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
205 static const struct got_error *gw_display_index(struct gw_trans *);
206 static const struct got_error *gw_get_header(struct gw_trans *,
207 struct gw_header *, int);
208 static const struct got_error *gw_get_commits(struct gw_trans *,
209 struct gw_header *, int,
210 struct got_object_id *);
211 static const struct got_error *gw_get_commit(struct gw_trans *,
213 struct got_commit_object *,
214 struct got_object_id *);
215 static const struct got_error *gw_apply_unveil(const char *);
216 static const struct got_error *gw_blame_cb(void *, int, int,
217 struct got_object_id *);
218 static const struct got_error *gw_load_got_paths(struct gw_trans *);
219 static const struct got_error *gw_load_got_path(struct gw_trans *,
221 static const struct got_error *gw_parse_querystring(struct gw_trans *);
222 static const struct got_error *gw_blame(struct gw_trans *);
223 static const struct got_error *gw_blob(struct gw_trans *);
224 static const struct got_error *gw_diff(struct gw_trans *);
225 static const struct got_error *gw_index(struct gw_trans *);
226 static const struct got_error *gw_commits(struct gw_trans *);
227 static const struct got_error *gw_briefs(struct gw_trans *);
228 static const struct got_error *gw_summary(struct gw_trans *);
229 static const struct got_error *gw_tree(struct gw_trans *);
230 static const struct got_error *gw_tag(struct gw_trans *);
231 static const struct got_error *gw_tags(struct gw_trans *);
233 struct gw_query_action {
234 unsigned int func_id;
235 const char *func_name;
236 const struct got_error *(*func_main)(struct gw_trans *);
240 enum gw_query_actions {
254 static struct gw_query_action gw_query_funcs[] = {
255 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
256 { GW_BLOB, "blob", NULL, NULL },
257 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
258 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
259 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
260 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
261 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
262 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
263 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
264 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
265 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
269 gw_get_action_name(struct gw_trans *gw_trans)
271 return gw_query_funcs[gw_trans->action].func_name;
274 static const struct got_error *
275 gw_kcgi_error(enum kcgi_err kerr)
280 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
281 return got_error(GOT_ERR_CANCELLED);
283 if (kerr == KCGI_ENOMEM)
284 return got_error_set_errno(ENOMEM,
285 kcgi_strerror(kerr));
287 if (kerr == KCGI_ENFILE)
288 return got_error_set_errno(ENFILE,
289 kcgi_strerror(kerr));
291 if (kerr == KCGI_EAGAIN)
292 return got_error_set_errno(EAGAIN,
293 kcgi_strerror(kerr));
295 if (kerr == KCGI_FORM)
296 return got_error_msg(GOT_ERR_IO,
297 kcgi_strerror(kerr));
299 return got_error_from_errno(kcgi_strerror(kerr));
302 static const struct got_error *
303 gw_apply_unveil(const char *repo_path)
305 const struct got_error *err;
307 if (repo_path && unveil(repo_path, "r") != 0)
308 return got_error_from_errno2("unveil", repo_path);
310 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
311 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
313 err = got_privsep_unveil_exec_helpers();
317 if (unveil(NULL, NULL) != 0)
318 return got_error_from_errno("unveil");
324 isbinary(const uint8_t *buf, size_t n)
328 for (i = 0; i < n; i++)
334 static const struct got_error *
335 gw_blame(struct gw_trans *gw_trans)
337 const struct got_error *error = NULL;
338 struct gw_header *header = NULL;
340 enum kcgi_err kerr = KCGI_OK;
342 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
344 return got_error_from_errno("pledge");
346 if ((header = gw_init_header()) == NULL)
347 return got_error_from_errno("malloc");
349 error = gw_apply_unveil(gw_trans->gw_dir->path);
353 /* check querystring */
354 if (gw_trans->repo_file == NULL) {
355 error = got_error_msg(GOT_ERR_QUERYSTRING,
356 "file required in querystring");
359 if (gw_trans->commit_id == NULL) {
360 error = got_error_msg(GOT_ERR_QUERYSTRING,
361 "commit required in querystring");
365 error = gw_get_header(gw_trans, header, 1);
368 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
369 "blame_header_wrapper", KATTR__MAX);
372 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
373 "blame_header", KATTR__MAX);
376 error = gw_get_time_str(&age, header->committer_time,
380 error = gw_gen_age_header(gw_trans, age ?age : "");
383 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
386 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
390 "dotted_line", KATTR__MAX);
393 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
397 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
398 "blame", KATTR__MAX);
401 error = gw_output_file_blame(gw_trans);
404 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
406 gw_free_header(header);
407 if (error == NULL && kerr != KCGI_OK)
408 error = gw_kcgi_error(kerr);
412 static const struct got_error *
413 gw_blob(struct gw_trans *gw_trans)
415 const struct got_error *error = NULL, *err = NULL;
416 struct gw_header *header = NULL;
417 enum kcgi_err kerr = KCGI_OK;
419 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
421 return got_error_from_errno("pledge");
423 if ((header = gw_init_header()) == NULL)
424 return got_error_from_errno("malloc");
426 error = gw_apply_unveil(gw_trans->gw_dir->path);
430 /* check querystring */
431 if (gw_trans->repo_file == NULL) {
432 error = got_error_msg(GOT_ERR_QUERYSTRING,
433 "file required in querystring");
436 if (gw_trans->commit_id == NULL) {
437 error = got_error_msg(GOT_ERR_QUERYSTRING,
438 "commit required in querystring");
441 error = gw_get_header(gw_trans, header, 1);
445 error = gw_output_blob_buf(gw_trans);
449 gw_trans->mime = KMIME_TEXT_PLAIN;
450 err = gw_display_index(gw_trans);
455 kerr = khttp_puts(gw_trans->gw_req, error->msg);
458 gw_free_header(header);
459 if (error == NULL && kerr != KCGI_OK)
460 error = gw_kcgi_error(kerr);
464 static const struct got_error *
465 gw_diff(struct gw_trans *gw_trans)
467 const struct got_error *error = NULL;
468 struct gw_header *header = NULL;
470 enum kcgi_err kerr = KCGI_OK;
472 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
474 return got_error_from_errno("pledge");
476 if ((header = gw_init_header()) == NULL)
477 return got_error_from_errno("malloc");
479 error = gw_apply_unveil(gw_trans->gw_dir->path);
483 error = gw_get_header(gw_trans, header, 1);
487 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
488 "diff_header_wrapper", KATTR__MAX);
491 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
492 "diff_header", KATTR__MAX);
495 error = gw_gen_diff_header(gw_trans, header->parent_id,
499 error = gw_gen_commit_header(gw_trans, header->commit_id,
503 error = gw_gen_tree_header(gw_trans, header->tree_id);
506 error = gw_gen_author_header(gw_trans, header->author);
509 error = gw_gen_committer_header(gw_trans, header->author);
512 error = gw_get_time_str(&age, header->committer_time,
516 error = gw_gen_age_header(gw_trans, age ?age : "");
519 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
522 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
525 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
526 "dotted_line", KATTR__MAX);
529 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
533 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
537 error = gw_output_diff(gw_trans, header);
541 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
543 gw_free_header(header);
545 if (error == NULL && kerr != KCGI_OK)
546 error = gw_kcgi_error(kerr);
550 static const struct got_error *
551 gw_index(struct gw_trans *gw_trans)
553 const struct got_error *error = NULL;
554 struct gw_dir *gw_dir = NULL;
555 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
556 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
557 char *href_tags = NULL;
558 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
559 enum kcgi_err kerr = KCGI_OK;
561 if (pledge("stdio rpath proc exec sendfd unveil",
563 error = got_error_from_errno("pledge");
567 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
571 error = gw_load_got_paths(gw_trans);
575 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
576 "index_header", KATTR__MAX);
578 return gw_kcgi_error(kerr);
579 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
580 "index_header_project", KATTR__MAX);
582 return gw_kcgi_error(kerr);
583 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
585 return gw_kcgi_error(kerr);
586 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
588 return gw_kcgi_error(kerr);
590 if (gw_trans->gw_conf->got_show_repo_description) {
591 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
592 "index_header_description", KATTR__MAX);
594 return gw_kcgi_error(kerr);
595 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
597 return gw_kcgi_error(kerr);
598 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
600 return gw_kcgi_error(kerr);
603 if (gw_trans->gw_conf->got_show_repo_owner) {
604 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
605 "index_header_owner", KATTR__MAX);
607 return gw_kcgi_error(kerr);
608 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
610 return gw_kcgi_error(kerr);
611 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
613 return gw_kcgi_error(kerr);
616 if (gw_trans->gw_conf->got_show_repo_age) {
617 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
618 "index_header_age", KATTR__MAX);
620 return gw_kcgi_error(kerr);
621 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
623 return gw_kcgi_error(kerr);
624 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
626 return gw_kcgi_error(kerr);
629 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
631 return gw_kcgi_error(kerr);
633 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
634 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
635 "index_wrapper", KATTR__MAX);
637 return gw_kcgi_error(kerr);
638 kerr = khtml_puts(gw_trans->gw_html_req,
639 "No repositories found in ");
641 return gw_kcgi_error(kerr);
642 kerr = khtml_puts(gw_trans->gw_html_req,
643 gw_trans->gw_conf->got_repos_path);
645 return gw_kcgi_error(kerr);
646 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
648 return gw_kcgi_error(kerr);
649 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
650 "dotted_line", KATTR__MAX);
652 return gw_kcgi_error(kerr);
653 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
655 return gw_kcgi_error(kerr);
659 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
662 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
663 if (gw_trans->page > 0 && (gw_trans->page *
664 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
671 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
672 "index_wrapper", KATTR__MAX);
676 if (asprintf(&href_summary, "?path=%s&action=summary",
677 gw_dir->name) == -1) {
678 error = got_error_from_errno("asprintf");
681 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
682 "index_project", KATTR__MAX);
685 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
686 href_summary, KATTR__MAX);
689 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
692 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
695 if (gw_trans->gw_conf->got_show_repo_description) {
696 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
697 KATTR_ID, "index_project_description", KATTR__MAX);
700 kerr = khtml_puts(gw_trans->gw_html_req,
701 gw_dir->description ? gw_dir->description : "");
704 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
708 if (gw_trans->gw_conf->got_show_repo_owner) {
709 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
710 KATTR_ID, "index_project_owner", KATTR__MAX);
713 kerr = khtml_puts(gw_trans->gw_html_req,
714 gw_dir->owner ? gw_dir->owner : "");
717 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
721 if (gw_trans->gw_conf->got_show_repo_age) {
722 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
723 KATTR_ID, "index_project_age", KATTR__MAX);
726 kerr = khtml_puts(gw_trans->gw_html_req,
727 gw_dir->age ? gw_dir->age : "");
730 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
735 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
736 "navs_wrapper", KATTR__MAX);
739 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
744 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
745 href_summary, KATTR__MAX);
748 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
751 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
755 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
759 if (asprintf(&href_briefs, "?path=%s&action=briefs",
760 gw_dir->name) == -1) {
761 error = got_error_from_errno("asprintf");
764 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
765 href_briefs, KATTR__MAX);
768 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
771 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
773 error = gw_kcgi_error(kerr);
775 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
779 if (asprintf(&href_commits, "?path=%s&action=commits",
780 gw_dir->name) == -1) {
781 error = got_error_from_errno("asprintf");
784 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
785 href_commits, KATTR__MAX);
788 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
791 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
795 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
799 if (asprintf(&href_tags, "?path=%s&action=tags",
800 gw_dir->name) == -1) {
801 error = got_error_from_errno("asprintf");
804 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
805 href_tags, KATTR__MAX);
808 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
811 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
815 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
819 if (asprintf(&href_tree, "?path=%s&action=tree",
820 gw_dir->name) == -1) {
821 error = got_error_from_errno("asprintf");
824 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
825 href_tree, KATTR__MAX);
828 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
832 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
835 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
836 "dotted_line", KATTR__MAX);
839 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
854 if (gw_trans->gw_conf->got_max_repos_display == 0)
857 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
858 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
859 (gw_trans->page > 0) &&
860 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
861 prev_disp == gw_trans->repos_total))) {
862 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
863 KATTR_ID, "np_wrapper", KATTR__MAX);
866 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
867 KATTR_ID, "nav_prev", KATTR__MAX);
872 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
873 (gw_trans->page > 0) &&
874 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
875 prev_disp == gw_trans->repos_total)) {
876 if (asprintf(&href_prev, "?page=%d",
877 gw_trans->page - 1) == -1) {
878 error = got_error_from_errno("asprintf");
881 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
882 KATTR_HREF, href_prev, KATTR__MAX);
885 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
888 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
893 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
895 return gw_kcgi_error(kerr);
897 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
898 next_disp == gw_trans->gw_conf->got_max_repos_display &&
899 dir_c != (gw_trans->page + 1) *
900 gw_trans->gw_conf->got_max_repos_display) {
901 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
902 KATTR_ID, "nav_next", KATTR__MAX);
905 if (asprintf(&href_next, "?page=%d",
906 gw_trans->page + 1) == -1) {
907 error = got_error_from_errno("calloc");
910 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
911 KATTR_HREF, href_next, KATTR__MAX);
914 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
917 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
924 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
925 (gw_trans->page > 0) &&
926 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
927 prev_disp == gw_trans->repos_total)) {
928 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
942 if (error == NULL && kerr != KCGI_OK)
943 error = gw_kcgi_error(kerr);
947 static const struct got_error *
948 gw_commits(struct gw_trans *gw_trans)
950 const struct got_error *error = NULL;
951 struct gw_header *header = NULL, *n_header = NULL;
952 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
953 char *href_prev = NULL, *href_next = NULL;
954 enum kcgi_err kerr = KCGI_OK;
956 if ((header = gw_init_header()) == NULL)
957 return got_error_from_errno("malloc");
959 if (pledge("stdio rpath proc exec sendfd unveil",
961 error = got_error_from_errno("pledge");
965 error = gw_apply_unveil(gw_trans->gw_dir->path);
969 error = gw_get_header(gw_trans, header,
970 gw_trans->gw_conf->got_max_commits_display);
974 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
975 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
976 "commits_line_wrapper", KATTR__MAX);
979 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
983 error = gw_gen_author_header(gw_trans, n_header->author);
986 error = gw_gen_committer_header(gw_trans, n_header->author);
989 error = gw_get_time_str(&age, n_header->committer_time,
993 error = gw_gen_age_header(gw_trans, age ?age : "");
996 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1000 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1001 "dotted_line", KATTR__MAX);
1002 if (kerr != KCGI_OK)
1004 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1005 if (kerr != KCGI_OK)
1008 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1009 "commit", KATTR__MAX);
1010 if (kerr != KCGI_OK)
1012 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1013 if (kerr != KCGI_OK)
1015 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1016 if (kerr != KCGI_OK)
1019 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1020 gw_trans->repo_name, n_header->commit_id) == -1) {
1021 error = got_error_from_errno("asprintf");
1024 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1025 KATTR_ID, "navs_wrapper", KATTR__MAX);
1026 if (kerr != KCGI_OK)
1028 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1029 KATTR_ID, "navs", KATTR__MAX);
1030 if (kerr != KCGI_OK)
1032 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1033 KATTR_HREF, href_diff, KATTR__MAX);
1034 if (kerr != KCGI_OK)
1036 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1037 if (kerr != KCGI_OK)
1039 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1040 if (kerr != KCGI_OK)
1043 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1044 if (kerr != KCGI_OK)
1047 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1048 gw_trans->repo_name, n_header->commit_id) == -1) {
1049 error = got_error_from_errno("asprintf");
1052 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1053 KATTR_HREF, href_blob, KATTR__MAX);
1054 if (kerr != KCGI_OK)
1056 khtml_puts(gw_trans->gw_html_req, "tree");
1057 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1058 if (kerr != KCGI_OK)
1060 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1061 if (kerr != KCGI_OK)
1064 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1065 "solid_line", KATTR__MAX);
1066 if (kerr != KCGI_OK)
1068 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1069 if (kerr != KCGI_OK)
1076 if (gw_trans->next_id || gw_trans->page > 0) {
1077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1078 KATTR_ID, "np_wrapper", KATTR__MAX);
1079 if (kerr != KCGI_OK)
1081 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1082 KATTR_ID, "nav_prev", KATTR__MAX);
1083 if (kerr != KCGI_OK)
1087 if (gw_trans->page > 0 && gw_trans->prev_id) {
1088 if (asprintf(&href_prev,
1089 "?path=%s&page=%d&action=commits&commit=%s&prev=%s",
1090 gw_trans->repo_name, gw_trans->page - 1,
1091 gw_trans->prev_id ? gw_trans->prev_id : "",
1092 gw_trans->prev_prev_id ?
1093 gw_trans->prev_prev_id : "") == -1) {
1094 error = got_error_from_errno("asprintf");
1097 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1098 KATTR_HREF, href_prev, KATTR__MAX);
1099 if (kerr != KCGI_OK)
1101 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1102 if (kerr != KCGI_OK)
1104 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1105 if (kerr != KCGI_OK)
1109 if (gw_trans->next_id || gw_trans->page > 0) {
1110 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1111 if (kerr != KCGI_OK)
1112 return gw_kcgi_error(kerr);
1115 if (gw_trans->next_id) {
1116 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1117 KATTR_ID, "nav_next", KATTR__MAX);
1118 if (kerr != KCGI_OK)
1120 if (asprintf(&href_next,
1121 "?path=%s&page=%d&action=commits" \
1122 "&commit=%s&prev=%s&prev_prev=%s",
1123 gw_trans->repo_name, gw_trans->page + 1,
1125 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1127 gw_trans->prev_id : "") == -1) {
1128 error = got_error_from_errno("calloc");
1131 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1132 KATTR_HREF, href_next, KATTR__MAX);
1133 if (kerr != KCGI_OK)
1135 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1136 if (kerr != KCGI_OK)
1138 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1139 if (kerr != KCGI_OK)
1143 if (gw_trans->next_id || gw_trans->page > 0) {
1144 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1145 if (kerr != KCGI_OK)
1149 gw_free_header(header);
1150 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1151 gw_free_header(n_header);
1157 if (error == NULL && kerr != KCGI_OK)
1158 error = gw_kcgi_error(kerr);
1162 static const struct got_error *
1163 gw_briefs(struct gw_trans *gw_trans)
1165 const struct got_error *error = NULL;
1166 struct gw_header *header = NULL, *n_header = NULL;
1167 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
1168 char *href_prev = NULL, *href_next = NULL;
1169 char *newline, *smallerthan;
1170 enum kcgi_err kerr = KCGI_OK;
1172 if ((header = gw_init_header()) == NULL)
1173 return got_error_from_errno("malloc");
1175 if (pledge("stdio rpath proc exec sendfd unveil",
1177 error = got_error_from_errno("pledge");
1181 if (gw_trans->action != GW_SUMMARY) {
1182 error = gw_apply_unveil(gw_trans->gw_dir->path);
1187 if (gw_trans->action == GW_SUMMARY)
1188 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1190 error = gw_get_header(gw_trans, header,
1191 gw_trans->gw_conf->got_max_commits_display);
1195 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1196 error = gw_get_time_str(&age, n_header->committer_time,
1201 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1202 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1203 if (kerr != KCGI_OK)
1206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1207 KATTR_ID, "briefs_age", KATTR__MAX);
1208 if (kerr != KCGI_OK)
1210 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1211 if (kerr != KCGI_OK)
1213 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1214 if (kerr != KCGI_OK)
1217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1218 KATTR_ID, "briefs_author", KATTR__MAX);
1219 if (kerr != KCGI_OK)
1221 smallerthan = strchr(n_header->author, '<');
1223 *smallerthan = '\0';
1224 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1225 if (kerr != KCGI_OK)
1227 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1228 if (kerr != KCGI_OK)
1231 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1232 gw_trans->repo_name, n_header->commit_id) == -1) {
1233 error = got_error_from_errno("asprintf");
1236 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1237 KATTR_ID, "briefs_log", KATTR__MAX);
1238 if (kerr != KCGI_OK)
1240 newline = strchr(n_header->commit_msg, '\n');
1243 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1244 KATTR_HREF, href_diff, KATTR__MAX);
1245 if (kerr != KCGI_OK)
1247 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1248 if (kerr != KCGI_OK)
1250 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1251 if (kerr != KCGI_OK)
1254 if (n_header->refs_str) {
1255 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1256 if (kerr != KCGI_OK)
1258 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1259 KATTR_ID, "refs_str", KATTR__MAX);
1260 if (kerr != KCGI_OK)
1262 kerr = khtml_puts(gw_trans->gw_html_req, "(");
1263 if (kerr != KCGI_OK)
1265 kerr = khtml_puts(gw_trans->gw_html_req,
1266 n_header->refs_str);
1267 if (kerr != KCGI_OK)
1269 kerr = khtml_puts(gw_trans->gw_html_req, ")");
1270 if (kerr != KCGI_OK)
1272 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1273 if (kerr != KCGI_OK)
1277 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1278 if (kerr != KCGI_OK)
1281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1282 KATTR_ID, "navs_wrapper", KATTR__MAX);
1283 if (kerr != KCGI_OK)
1285 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1286 KATTR_ID, "navs", KATTR__MAX);
1287 if (kerr != KCGI_OK)
1289 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1290 KATTR_HREF, href_diff, KATTR__MAX);
1291 if (kerr != KCGI_OK)
1293 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1294 if (kerr != KCGI_OK)
1296 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1297 if (kerr != KCGI_OK)
1300 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1301 if (kerr != KCGI_OK)
1304 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1305 gw_trans->repo_name, n_header->commit_id) == -1) {
1306 error = got_error_from_errno("asprintf");
1309 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1310 KATTR_HREF, href_blob, KATTR__MAX);
1311 if (kerr != KCGI_OK)
1313 khtml_puts(gw_trans->gw_html_req, "tree");
1314 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1315 if (kerr != KCGI_OK)
1317 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1318 if (kerr != KCGI_OK)
1321 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1322 KATTR_ID, "dotted_line", KATTR__MAX);
1323 if (kerr != KCGI_OK)
1325 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1326 if (kerr != KCGI_OK)
1337 if (gw_trans->next_id || gw_trans->page > 0) {
1338 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1339 KATTR_ID, "np_wrapper", KATTR__MAX);
1340 if (kerr != KCGI_OK)
1342 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1343 KATTR_ID, "nav_prev", KATTR__MAX);
1344 if (kerr != KCGI_OK)
1348 if (gw_trans->page > 0 && gw_trans->prev_id) {
1349 if (asprintf(&href_prev,
1350 "?path=%s&page=%d&action=briefs&commit=%s&prev=%s",
1351 gw_trans->repo_name, gw_trans->page - 1,
1352 gw_trans->prev_id ? gw_trans->prev_id : "",
1353 gw_trans->prev_prev_id ?
1354 gw_trans->prev_prev_id : "") == -1) {
1355 error = got_error_from_errno("asprintf");
1358 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1359 KATTR_HREF, href_prev, KATTR__MAX);
1360 if (kerr != KCGI_OK)
1362 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1363 if (kerr != KCGI_OK)
1365 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1366 if (kerr != KCGI_OK)
1370 if (gw_trans->next_id || gw_trans->page > 0) {
1371 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1372 if (kerr != KCGI_OK)
1373 return gw_kcgi_error(kerr);
1376 if (gw_trans->next_id) {
1377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1378 KATTR_ID, "nav_next", KATTR__MAX);
1379 if (kerr != KCGI_OK)
1381 if (asprintf(&href_next,
1382 "?path=%s&page=%d&action=briefs" \
1383 "&commit=%s&prev=%s&prev_prev=%s",
1384 gw_trans->repo_name, gw_trans->page + 1,
1386 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1388 gw_trans->prev_id : "") == -1) {
1389 error = got_error_from_errno("calloc");
1392 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1393 KATTR_HREF, href_next, KATTR__MAX);
1394 if (kerr != KCGI_OK)
1396 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1397 if (kerr != KCGI_OK)
1399 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1400 if (kerr != KCGI_OK)
1404 if (gw_trans->next_id || gw_trans->page > 0) {
1405 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1406 if (kerr != KCGI_OK)
1410 gw_free_header(header);
1411 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1412 gw_free_header(n_header);
1418 if (error == NULL && kerr != KCGI_OK)
1419 error = gw_kcgi_error(kerr);
1423 static const struct got_error *
1424 gw_summary(struct gw_trans *gw_trans)
1426 const struct got_error *error = NULL;
1428 enum kcgi_err kerr = KCGI_OK;
1430 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1431 return got_error_from_errno("pledge");
1433 error = gw_apply_unveil(gw_trans->gw_dir->path);
1437 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1438 "summary_wrapper", KATTR__MAX);
1439 if (kerr != KCGI_OK)
1440 return gw_kcgi_error(kerr);
1442 if (gw_trans->gw_conf->got_show_repo_description &&
1443 gw_trans->gw_dir->description != NULL &&
1444 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1445 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1446 KATTR_ID, "description_title", KATTR__MAX);
1447 if (kerr != KCGI_OK)
1449 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1450 if (kerr != KCGI_OK)
1452 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1453 if (kerr != KCGI_OK)
1455 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1456 KATTR_ID, "description", KATTR__MAX);
1457 if (kerr != KCGI_OK)
1459 kerr = khtml_puts(gw_trans->gw_html_req,
1460 gw_trans->gw_dir->description);
1461 if (kerr != KCGI_OK)
1463 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1464 if (kerr != KCGI_OK)
1468 if (gw_trans->gw_conf->got_show_repo_owner &&
1469 gw_trans->gw_dir->owner != NULL &&
1470 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1471 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1472 KATTR_ID, "repo_owner_title", KATTR__MAX);
1473 if (kerr != KCGI_OK)
1475 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1476 if (kerr != KCGI_OK)
1478 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1479 if (kerr != KCGI_OK)
1481 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1482 KATTR_ID, "repo_owner", KATTR__MAX);
1483 if (kerr != KCGI_OK)
1485 kerr = khtml_puts(gw_trans->gw_html_req,
1486 gw_trans->gw_dir->owner);
1487 if (kerr != KCGI_OK)
1489 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1490 if (kerr != KCGI_OK)
1494 if (gw_trans->gw_conf->got_show_repo_age) {
1495 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1500 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1501 KATTR_ID, "last_change_title", KATTR__MAX);
1502 if (kerr != KCGI_OK)
1504 kerr = khtml_puts(gw_trans->gw_html_req,
1506 if (kerr != KCGI_OK)
1508 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1509 if (kerr != KCGI_OK)
1511 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1512 KATTR_ID, "last_change", KATTR__MAX);
1513 if (kerr != KCGI_OK)
1515 kerr = khtml_puts(gw_trans->gw_html_req, age);
1516 if (kerr != KCGI_OK)
1518 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1519 if (kerr != KCGI_OK)
1524 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1525 gw_trans->gw_dir->url != NULL &&
1526 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1527 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1528 KATTR_ID, "cloneurl_title", KATTR__MAX);
1529 if (kerr != KCGI_OK)
1531 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1532 if (kerr != KCGI_OK)
1534 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1535 if (kerr != KCGI_OK)
1537 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1538 KATTR_ID, "cloneurl", KATTR__MAX);
1539 if (kerr != KCGI_OK)
1541 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1542 if (kerr != KCGI_OK)
1544 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1545 if (kerr != KCGI_OK)
1549 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1550 if (kerr != KCGI_OK)
1553 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1554 "briefs_title_wrapper", KATTR__MAX);
1555 if (kerr != KCGI_OK)
1557 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1558 "briefs_title", KATTR__MAX);
1559 if (kerr != KCGI_OK)
1561 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1562 if (kerr != KCGI_OK)
1564 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1565 if (kerr != KCGI_OK)
1567 error = gw_briefs(gw_trans);
1571 error = gw_tags(gw_trans);
1575 error = gw_output_repo_heads(gw_trans);
1578 if (error == NULL && kerr != KCGI_OK)
1579 error = gw_kcgi_error(kerr);
1583 static const struct got_error *
1584 gw_tree(struct gw_trans *gw_trans)
1586 const struct got_error *error = NULL;
1587 struct gw_header *header = NULL;
1588 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1590 enum kcgi_err kerr = KCGI_OK;
1592 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1593 return got_error_from_errno("pledge");
1595 if ((header = gw_init_header()) == NULL)
1596 return got_error_from_errno("malloc");
1598 error = gw_apply_unveil(gw_trans->gw_dir->path);
1602 error = gw_get_header(gw_trans, header, 1);
1606 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1607 "tree_header_wrapper", KATTR__MAX);
1608 if (kerr != KCGI_OK)
1610 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1611 "tree_header", KATTR__MAX);
1612 if (kerr != KCGI_OK)
1614 error = gw_gen_tree_header(gw_trans, header->tree_id);
1617 error = gw_get_time_str(&age, header->committer_time,
1621 error = gw_gen_age_header(gw_trans, age ?age : "");
1624 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1627 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1628 if (kerr != KCGI_OK)
1630 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1631 "dotted_line", KATTR__MAX);
1632 if (kerr != KCGI_OK)
1634 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1635 if (kerr != KCGI_OK)
1638 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1639 "tree", KATTR__MAX);
1640 if (kerr != KCGI_OK)
1642 error = gw_output_repo_tree(gw_trans);
1646 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1648 gw_free_header(header);
1649 free(tree_html_disp);
1653 if (error == NULL && kerr != KCGI_OK)
1654 error = gw_kcgi_error(kerr);
1658 static const struct got_error *
1659 gw_tags(struct gw_trans *gw_trans)
1661 const struct got_error *error = NULL;
1662 struct gw_header *header = NULL;
1663 char *href_next = NULL, *href_prev = NULL;
1664 enum kcgi_err kerr = KCGI_OK;
1666 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1667 return got_error_from_errno("pledge");
1669 if ((header = gw_init_header()) == NULL)
1670 return got_error_from_errno("malloc");
1672 if (gw_trans->action != GW_SUMMARY) {
1673 error = gw_apply_unveil(gw_trans->gw_dir->path);
1678 error = gw_get_header(gw_trans, header, 1);
1682 if (gw_trans->action == GW_SUMMARY) {
1683 gw_trans->next_id = NULL;
1684 error = gw_output_repo_tags(gw_trans, header,
1685 D_MAXSLCOMMDISP, TAGBRIEF);
1689 error = gw_output_repo_tags(gw_trans, header,
1690 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1695 if (gw_trans->next_id || gw_trans->page > 0) {
1696 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1697 KATTR_ID, "np_wrapper", KATTR__MAX);
1698 if (kerr != KCGI_OK)
1700 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1701 KATTR_ID, "nav_prev", KATTR__MAX);
1702 if (kerr != KCGI_OK)
1706 if (gw_trans->page > 0 && gw_trans->prev_id) {
1707 if (asprintf(&href_prev,
1708 "?path=%s&page=%d&action=tags&commit=%s&prev=%s",
1709 gw_trans->repo_name, gw_trans->page - 1,
1710 gw_trans->prev_id ? gw_trans->prev_id : "",
1711 gw_trans->prev_prev_id ?
1712 gw_trans->prev_prev_id : "") == -1) {
1713 error = got_error_from_errno("asprintf");
1716 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1717 KATTR_HREF, href_prev, KATTR__MAX);
1718 if (kerr != KCGI_OK)
1720 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1721 if (kerr != KCGI_OK)
1723 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1724 if (kerr != KCGI_OK)
1728 if (gw_trans->next_id || gw_trans->page > 0) {
1729 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1730 if (kerr != KCGI_OK)
1731 return gw_kcgi_error(kerr);
1734 if (gw_trans->next_id) {
1735 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1736 KATTR_ID, "nav_next", KATTR__MAX);
1737 if (kerr != KCGI_OK)
1739 if (asprintf(&href_next,
1740 "?path=%s&page=%d&action=tags" \
1741 "&commit=%s&prev=%s&prev_prev=%s",
1742 gw_trans->repo_name, gw_trans->page + 1,
1744 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1746 gw_trans->prev_id : "") == -1) {
1747 error = got_error_from_errno("calloc");
1750 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1751 KATTR_HREF, href_next, KATTR__MAX);
1752 if (kerr != KCGI_OK)
1754 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1755 if (kerr != KCGI_OK)
1757 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1758 if (kerr != KCGI_OK)
1762 if (gw_trans->next_id || gw_trans->page > 0) {
1763 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1764 if (kerr != KCGI_OK)
1768 gw_free_header(header);
1771 if (error == NULL && kerr != KCGI_OK)
1772 error = gw_kcgi_error(kerr);
1776 static const struct got_error *
1777 gw_tag(struct gw_trans *gw_trans)
1779 const struct got_error *error = NULL;
1780 struct gw_header *header = NULL;
1781 enum kcgi_err kerr = KCGI_OK;
1783 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1784 return got_error_from_errno("pledge");
1786 if ((header = gw_init_header()) == NULL)
1787 return got_error_from_errno("malloc");
1789 error = gw_apply_unveil(gw_trans->gw_dir->path);
1793 if (gw_trans->commit_id == NULL) {
1794 error = got_error_msg(GOT_ERR_QUERYSTRING,
1795 "commit required in querystring");
1799 error = gw_get_header(gw_trans, header, 1);
1803 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1804 "tag_header_wrapper", KATTR__MAX);
1805 if (kerr != KCGI_OK)
1807 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1808 "tag_header", KATTR__MAX);
1809 if (kerr != KCGI_OK)
1811 error = gw_gen_commit_header(gw_trans, header->commit_id,
1815 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1818 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1819 if (kerr != KCGI_OK)
1821 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1822 "dotted_line", KATTR__MAX);
1823 if (kerr != KCGI_OK)
1825 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1826 if (kerr != KCGI_OK)
1829 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1830 "tree", KATTR__MAX);
1831 if (kerr != KCGI_OK)
1834 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1838 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1840 gw_free_header(header);
1841 if (error == NULL && kerr != KCGI_OK)
1842 error = gw_kcgi_error(kerr);
1846 static const struct got_error *
1847 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1849 const struct got_error *error = NULL;
1854 if (asprintf(&dir_test, "%s/%s/%s",
1855 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1856 GOTWEB_GIT_DIR) == -1)
1857 return got_error_from_errno("asprintf");
1859 dt = opendir(dir_test);
1863 gw_dir->path = strdup(dir_test);
1864 if (gw_dir->path == NULL) {
1866 error = got_error_from_errno("strdup");
1873 if (asprintf(&dir_test, "%s/%s/%s",
1874 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1875 GOTWEB_GOT_DIR) == -1) {
1877 error = got_error_from_errno("asprintf");
1881 dt = opendir(dir_test);
1886 error = got_error(GOT_ERR_NOT_GIT_REPO);
1890 if (asprintf(&dir_test, "%s/%s",
1891 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1892 error = got_error_from_errno("asprintf");
1897 gw_dir->path = strdup(dir_test);
1898 if (gw_dir->path == NULL) {
1900 error = got_error_from_errno("strdup");
1904 dt = opendir(dir_test);
1906 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1911 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1915 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1918 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1922 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1926 if (dt && closedir(dt) == -1 && error == NULL)
1927 error = got_error_from_errno("closedir");
1931 static const struct got_error *
1932 gw_load_got_paths(struct gw_trans *gw_trans)
1934 const struct got_error *error = NULL;
1936 struct dirent **sd_dent;
1937 struct gw_dir *gw_dir;
1939 unsigned int d_cnt, d_i;
1941 d = opendir(gw_trans->gw_conf->got_repos_path);
1943 error = got_error_from_errno2("opendir",
1944 gw_trans->gw_conf->got_repos_path);
1948 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1951 error = got_error_from_errno2("scandir",
1952 gw_trans->gw_conf->got_repos_path);
1956 for (d_i = 0; d_i < d_cnt; d_i++) {
1957 if (gw_trans->gw_conf->got_max_repos > 0 &&
1958 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1959 break; /* account for parent and self */
1961 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1962 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1965 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1969 error = gw_load_got_path(gw_trans, gw_dir);
1970 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1977 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1978 !got_path_dir_is_empty(gw_dir->path)) {
1979 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1981 gw_trans->repos_total++;
1985 if (d && closedir(d) == -1 && error == NULL)
1986 error = got_error_from_errno("closedir");
1990 static const struct got_error *
1991 gw_parse_querystring(struct gw_trans *gw_trans)
1993 const struct got_error *error = NULL;
1995 struct gw_query_action *action = NULL;
1998 if (gw_trans->gw_req->fieldnmap[0]) {
1999 return got_error(GOT_ERR_QUERYSTRING);
2000 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2001 /* define gw_trans->repo_path */
2002 gw_trans->repo_name = p->parsed.s;
2004 if (asprintf(&gw_trans->repo_path, "%s/%s",
2005 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2006 return got_error_from_errno("asprintf");
2008 /* get action and set function */
2009 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2010 for (i = 0; i < nitems(gw_query_funcs); i++) {
2011 action = &gw_query_funcs[i];
2012 if (action->func_name == NULL)
2014 if (strcmp(action->func_name,
2015 p->parsed.s) == 0) {
2016 gw_trans->action = i;
2021 if (gw_trans->action == -1) {
2022 gw_trans->action = GW_ERR;
2023 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2024 p != NULL ? "bad action in querystring" :
2025 "no action in querystring");
2029 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2030 if (asprintf(&gw_trans->commit_id, "%s",
2032 return got_error_from_errno("asprintf");
2035 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2036 gw_trans->repo_file = p->parsed.s;
2038 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2039 if (asprintf(&gw_trans->repo_folder, "%s",
2041 return got_error_from_errno("asprintf");
2044 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2045 if (asprintf(&gw_trans->prev_id, "%s",
2047 return got_error_from_errno("asprintf");
2050 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
2051 if (asprintf(&gw_trans->prev_prev_id, "%s",
2053 return got_error_from_errno("asprintf");
2056 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2057 gw_trans->headref = p->parsed.s;
2059 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2063 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2065 gw_trans->action = GW_INDEX;
2067 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2068 gw_trans->page = p->parsed.i;
2073 static const struct got_error *
2074 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2076 const struct got_error *error;
2078 *gw_dir = malloc(sizeof(**gw_dir));
2079 if (*gw_dir == NULL)
2080 return got_error_from_errno("malloc");
2082 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2083 error = got_error_from_errno("asprintf");
2092 static const struct got_error *
2093 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2095 enum kcgi_err kerr = KCGI_OK;
2097 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2098 if (kerr != KCGI_OK)
2099 return gw_kcgi_error(kerr);
2100 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2102 if (kerr != KCGI_OK)
2103 return gw_kcgi_error(kerr);
2104 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2106 if (kerr != KCGI_OK)
2107 return gw_kcgi_error(kerr);
2108 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2110 if (kerr != KCGI_OK)
2111 return gw_kcgi_error(kerr);
2112 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2113 if (kerr != KCGI_OK)
2114 return gw_kcgi_error(kerr);
2115 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2117 if (kerr != KCGI_OK)
2118 return gw_kcgi_error(kerr);
2120 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2121 kerr = khttp_head(gw_trans->gw_req,
2122 kresps[KRESP_CONTENT_DISPOSITION],
2123 "attachment; filename=%s", gw_trans->repo_file);
2124 if (kerr != KCGI_OK)
2125 return gw_kcgi_error(kerr);
2128 kerr = khttp_body(gw_trans->gw_req);
2129 return gw_kcgi_error(kerr);
2132 static const struct got_error *
2133 gw_display_index(struct gw_trans *gw_trans)
2135 const struct got_error *error;
2136 enum kcgi_err kerr = KCGI_OK;
2138 /* catch early querystring errors */
2139 if (gw_trans->error)
2140 gw_trans->action = GW_ERR;
2142 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2146 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2147 if (kerr != KCGI_OK)
2148 return gw_kcgi_error(kerr);
2150 if (gw_trans->action != GW_BLOB) {
2151 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2152 gw_query_funcs[gw_trans->action].template);
2153 if (kerr != KCGI_OK) {
2154 khtml_close(gw_trans->gw_html_req);
2155 return gw_kcgi_error(kerr);
2159 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2162 static const struct got_error *
2163 gw_error(struct gw_trans *gw_trans)
2165 enum kcgi_err kerr = KCGI_OK;
2167 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2169 return gw_kcgi_error(kerr);
2173 gw_template(size_t key, void *arg)
2175 const struct got_error *error = NULL;
2176 enum kcgi_err kerr = KCGI_OK;
2177 struct gw_trans *gw_trans = arg;
2178 char *img_src = NULL;
2182 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2183 KATTR_NAME, "viewport",
2184 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2186 if (kerr != KCGI_OK)
2188 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2189 if (kerr != KCGI_OK)
2191 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2192 KATTR_CHARSET, "utf-8",
2194 if (kerr != KCGI_OK)
2196 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2197 if (kerr != KCGI_OK)
2199 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2200 KATTR_NAME, "msapplication-TileColor",
2201 KATTR_CONTENT, "#da532c", KATTR__MAX);
2202 if (kerr != KCGI_OK)
2204 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2205 if (kerr != KCGI_OK)
2207 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2208 KATTR_NAME, "theme-color",
2209 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2210 if (kerr != KCGI_OK)
2212 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2213 if (kerr != KCGI_OK)
2215 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2216 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2217 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
2218 if (kerr != KCGI_OK)
2220 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2221 if (kerr != KCGI_OK)
2223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2224 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2225 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
2226 if (kerr != KCGI_OK)
2228 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2229 if (kerr != KCGI_OK)
2231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2232 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2233 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
2234 if (kerr != KCGI_OK)
2236 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2237 if (kerr != KCGI_OK)
2239 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2240 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
2242 if (kerr != KCGI_OK)
2244 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2245 if (kerr != KCGI_OK)
2247 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2248 KATTR_REL, "mask-icon", KATTR_HREF,
2249 "/safari-pinned-tab.svg", KATTR__MAX);
2250 if (kerr != KCGI_OK)
2252 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2253 if (kerr != KCGI_OK)
2255 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2256 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2257 KATTR_HREF, "/gotweb.css", KATTR__MAX);
2258 if (kerr != KCGI_OK)
2260 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2261 if (kerr != KCGI_OK)
2265 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2266 KATTR_ID, "got_link", KATTR__MAX);
2267 if (kerr != KCGI_OK)
2269 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2270 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2271 KATTR_TARGET, "_sotd", KATTR__MAX);
2272 if (kerr != KCGI_OK)
2274 if (asprintf(&img_src, "/%s",
2275 gw_trans->gw_conf->got_logo) == -1)
2277 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2278 KATTR_SRC, img_src, KATTR__MAX);
2279 if (kerr != KCGI_OK) {
2283 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2284 if (kerr != KCGI_OK) {
2289 case (TEMPL_SITEPATH):
2290 error = gw_output_site_link(gw_trans);
2295 if (gw_trans->gw_conf->got_site_name != NULL) {
2296 kerr = khtml_puts(gw_trans->gw_html_req,
2297 gw_trans->gw_conf->got_site_name);
2298 if (kerr != KCGI_OK)
2302 case (TEMPL_SEARCH):
2304 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2305 "search", KATTR__MAX);
2306 if (kerr != KCGI_OK)
2308 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2309 KATTR_METHOD, "POST", KATTR__MAX);
2310 if (kerr != KCGI_OK)
2312 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2313 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2314 KATTR_MAXLENGTH, "50", KATTR__MAX);
2315 if (kerr != KCGI_OK)
2317 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2319 if (kerr != KCGI_OK)
2321 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2322 if (kerr != KCGI_OK)
2324 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2325 if (kerr != KCGI_OK)
2328 case(TEMPL_SITEOWNER):
2329 if (gw_trans->gw_conf->got_site_owner != NULL &&
2330 gw_trans->gw_conf->got_show_site_owner) {
2331 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2332 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2333 if (kerr != KCGI_OK)
2335 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2336 KATTR_ID, "site_owner", KATTR__MAX);
2337 if (kerr != KCGI_OK)
2339 kerr = khtml_puts(gw_trans->gw_html_req,
2340 gw_trans->gw_conf->got_site_owner);
2341 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2342 if (kerr != KCGI_OK)
2346 case(TEMPL_CONTENT):
2347 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2349 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2350 KATTR_ID, "tmpl_err", KATTR__MAX);
2351 if (kerr != KCGI_OK)
2353 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
2354 if (kerr != KCGI_OK)
2356 kerr = khttp_puts(gw_trans->gw_req, error->msg);
2357 if (kerr != KCGI_OK)
2359 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2360 if (kerr != KCGI_OK)
2370 static const struct got_error *
2371 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2373 const struct got_error *error = NULL;
2374 enum kcgi_err kerr = KCGI_OK;
2376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2377 KATTR_ID, "header_commit_title", KATTR__MAX);
2378 if (kerr != KCGI_OK)
2380 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2381 if (kerr != KCGI_OK)
2383 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2384 if (kerr != KCGI_OK)
2386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2387 KATTR_ID, "header_commit", KATTR__MAX);
2388 if (kerr != KCGI_OK)
2390 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2391 if (kerr != KCGI_OK)
2393 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2394 if (kerr != KCGI_OK)
2397 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2398 KATTR_ID, "refs_str", KATTR__MAX);
2399 if (kerr != KCGI_OK)
2401 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2402 if (kerr != KCGI_OK)
2404 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2405 if (kerr != KCGI_OK)
2407 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2408 if (kerr != KCGI_OK)
2410 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2411 if (kerr != KCGI_OK)
2414 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2416 if (error == NULL && kerr != KCGI_OK)
2417 error = gw_kcgi_error(kerr);
2421 static const struct got_error *
2422 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2424 const struct got_error *error = NULL;
2425 enum kcgi_err kerr = KCGI_OK;
2427 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2428 KATTR_ID, "header_diff_title", KATTR__MAX);
2429 if (kerr != KCGI_OK)
2431 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2432 if (kerr != KCGI_OK)
2434 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2435 if (kerr != KCGI_OK)
2437 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2438 KATTR_ID, "header_diff", KATTR__MAX);
2439 if (kerr != KCGI_OK)
2442 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2443 if (kerr != KCGI_OK)
2446 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2447 if (kerr != KCGI_OK)
2449 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2450 if (kerr != KCGI_OK)
2452 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2454 if (error == NULL && kerr != KCGI_OK)
2455 error = gw_kcgi_error(kerr);
2459 static const struct got_error *
2460 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2462 const struct got_error *error = NULL;
2463 enum kcgi_err kerr = KCGI_OK;
2465 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2466 KATTR_ID, "header_age_title", KATTR__MAX);
2467 if (kerr != KCGI_OK)
2469 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2470 if (kerr != KCGI_OK)
2472 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2473 if (kerr != KCGI_OK)
2475 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2476 KATTR_ID, "header_age", KATTR__MAX);
2477 if (kerr != KCGI_OK)
2479 kerr = khtml_puts(gw_trans->gw_html_req, str);
2480 if (kerr != KCGI_OK)
2482 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2484 if (error == NULL && kerr != KCGI_OK)
2485 error = gw_kcgi_error(kerr);
2489 static const struct got_error *
2490 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2492 const struct got_error *error = NULL;
2493 enum kcgi_err kerr = KCGI_OK;
2495 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2496 KATTR_ID, "header_author_title", KATTR__MAX);
2497 if (kerr != KCGI_OK)
2499 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2500 if (kerr != KCGI_OK)
2502 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2503 if (kerr != KCGI_OK)
2505 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2506 KATTR_ID, "header_author", KATTR__MAX);
2507 if (kerr != KCGI_OK)
2509 kerr = khtml_puts(gw_trans->gw_html_req, str);
2510 if (kerr != KCGI_OK)
2512 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2514 if (error == NULL && kerr != KCGI_OK)
2515 error = gw_kcgi_error(kerr);
2519 static const struct got_error *
2520 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2522 const struct got_error *error = NULL;
2523 enum kcgi_err kerr = KCGI_OK;
2525 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2526 KATTR_ID, "header_committer_title", KATTR__MAX);
2527 if (kerr != KCGI_OK)
2529 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2530 if (kerr != KCGI_OK)
2532 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2533 if (kerr != KCGI_OK)
2535 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2536 KATTR_ID, "header_committer", KATTR__MAX);
2537 if (kerr != KCGI_OK)
2539 kerr = khtml_puts(gw_trans->gw_html_req, str);
2540 if (kerr != KCGI_OK)
2542 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2544 if (error == NULL && kerr != KCGI_OK)
2545 error = gw_kcgi_error(kerr);
2549 static const struct got_error *
2550 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2552 const struct got_error *error = NULL;
2553 enum kcgi_err kerr = KCGI_OK;
2555 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2556 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2557 if (kerr != KCGI_OK)
2559 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2560 if (kerr != KCGI_OK)
2562 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2563 if (kerr != KCGI_OK)
2565 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2566 KATTR_ID, "header_commit_msg", KATTR__MAX);
2567 if (kerr != KCGI_OK)
2569 kerr = khttp_puts(gw_trans->gw_req, str);
2570 if (kerr != KCGI_OK)
2572 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2574 if (error == NULL && kerr != KCGI_OK)
2575 error = gw_kcgi_error(kerr);
2579 static const struct got_error *
2580 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2582 const struct got_error *error = NULL;
2583 enum kcgi_err kerr = KCGI_OK;
2585 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2586 KATTR_ID, "header_tree_title", KATTR__MAX);
2587 if (kerr != KCGI_OK)
2589 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2590 if (kerr != KCGI_OK)
2592 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2593 if (kerr != KCGI_OK)
2595 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2596 KATTR_ID, "header_tree", KATTR__MAX);
2597 if (kerr != KCGI_OK)
2599 kerr = khtml_puts(gw_trans->gw_html_req, str);
2600 if (kerr != KCGI_OK)
2602 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2604 if (error == NULL && kerr != KCGI_OK)
2605 error = gw_kcgi_error(kerr);
2609 static const struct got_error *
2610 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2613 const struct got_error *error = NULL;
2615 char *d_file = NULL;
2619 *description = NULL;
2620 if (gw_trans->gw_conf->got_show_repo_description == 0)
2623 if (asprintf(&d_file, "%s/description", dir) == -1)
2624 return got_error_from_errno("asprintf");
2626 f = fopen(d_file, "r");
2628 if (errno == ENOENT || errno == EACCES)
2630 error = got_error_from_errno2("fopen", d_file);
2634 if (fseek(f, 0, SEEK_END) == -1) {
2635 error = got_ferror(f, GOT_ERR_IO);
2640 error = got_ferror(f, GOT_ERR_IO);
2643 if (fseek(f, 0, SEEK_SET) == -1) {
2644 error = got_ferror(f, GOT_ERR_IO);
2647 *description = calloc(len + 1, sizeof(**description));
2648 if (*description == NULL) {
2649 error = got_error_from_errno("calloc");
2653 n = fread(*description, 1, len, f);
2654 if (n == 0 && ferror(f))
2655 error = got_ferror(f, GOT_ERR_IO);
2657 if (f != NULL && fclose(f) == -1 && error == NULL)
2658 error = got_error_from_errno("fclose");
2663 static const struct got_error *
2664 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2668 char *years = "years ago", *months = "months ago";
2669 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2670 char *minutes = "minutes ago", *seconds = "seconds ago";
2671 char *now = "right now";
2679 diff_time = time(NULL) - committer_time;
2680 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2681 if (asprintf(repo_age, "%lld %s",
2682 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2683 return got_error_from_errno("asprintf");
2684 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2685 if (asprintf(repo_age, "%lld %s",
2686 (diff_time / 60 / 60 / 24 / (365 / 12)),
2688 return got_error_from_errno("asprintf");
2689 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2690 if (asprintf(repo_age, "%lld %s",
2691 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2692 return got_error_from_errno("asprintf");
2693 } else if (diff_time > 60 * 60 * 24 * 2) {
2694 if (asprintf(repo_age, "%lld %s",
2695 (diff_time / 60 / 60 / 24), days) == -1)
2696 return got_error_from_errno("asprintf");
2697 } else if (diff_time > 60 * 60 * 2) {
2698 if (asprintf(repo_age, "%lld %s",
2699 (diff_time / 60 / 60), hours) == -1)
2700 return got_error_from_errno("asprintf");
2701 } else if (diff_time > 60 * 2) {
2702 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2704 return got_error_from_errno("asprintf");
2705 } else if (diff_time > 2) {
2706 if (asprintf(repo_age, "%lld %s", diff_time,
2708 return got_error_from_errno("asprintf");
2710 if (asprintf(repo_age, "%s", now) == -1)
2711 return got_error_from_errno("asprintf");
2715 if (gmtime_r(&committer_time, &tm) == NULL)
2716 return got_error_from_errno("gmtime_r");
2718 s = asctime_r(&tm, datebuf);
2720 return got_error_from_errno("asctime_r");
2722 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2723 return got_error_from_errno("asprintf");
2729 static const struct got_error *
2730 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2731 const char *refname, int ref_tm)
2733 const struct got_error *error = NULL;
2734 struct got_repository *repo = NULL;
2735 struct got_commit_object *commit = NULL;
2736 struct got_reflist_head refs;
2737 struct got_reflist_entry *re;
2738 time_t committer_time = 0, cmp_time = 0;
2741 SIMPLEQ_INIT(&refs);
2743 if (gw_trans->gw_conf->got_show_repo_age == 0)
2747 repo = gw_trans->repo;
2749 error = got_repo_open(&repo, dir, NULL);
2754 error = got_ref_list(&refs, repo, "refs/heads",
2755 got_ref_cmp_by_name, NULL);
2760 * Find the youngest branch tip in the repository, or the age of
2761 * the a specific branch tip if a name was provided by the caller.
2763 SIMPLEQ_FOREACH(re, &refs, entry) {
2764 struct got_object_id *id = NULL;
2766 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2769 error = got_ref_resolve(&id, repo, re->ref);
2773 error = got_object_open_as_commit(&commit, repo, id);
2779 got_object_commit_get_committer_time(commit);
2780 got_object_commit_close(commit);
2781 if (cmp_time < committer_time)
2782 cmp_time = committer_time;
2788 if (cmp_time != 0) {
2789 committer_time = cmp_time;
2790 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2793 got_ref_list_free(&refs);
2794 if (gw_trans->repo == NULL)
2795 got_repo_close(repo);
2799 static const struct got_error *
2800 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2802 const struct got_error *error;
2804 struct got_object_id *id1 = NULL, *id2 = NULL;
2805 char *label1 = NULL, *label2 = NULL, *line = NULL;
2807 size_t linesize = 0;
2809 enum kcgi_err kerr = KCGI_OK;
2815 if (header->parent_id != NULL &&
2816 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2817 error = got_repo_match_object_id(&id1, &label1,
2818 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2823 error = got_repo_match_object_id(&id2, &label2,
2824 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2828 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2832 case GOT_OBJ_TYPE_BLOB:
2833 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2836 case GOT_OBJ_TYPE_TREE:
2837 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2840 case GOT_OBJ_TYPE_COMMIT:
2841 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2845 error = got_error(GOT_ERR_OBJ_TYPE);
2850 if (fseek(f, 0, SEEK_SET) == -1) {
2851 error = got_ferror(f, GOT_ERR_IO);
2855 while ((linelen = getline(&line, &linesize, f)) != -1) {
2856 error = gw_colordiff_line(gw_trans, line);
2859 /* XXX: KHTML_PRETTY breaks this */
2860 kerr = khtml_puts(gw_trans->gw_html_req, line);
2861 if (kerr != KCGI_OK)
2863 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2864 if (kerr != KCGI_OK)
2867 if (linelen == -1 && ferror(f))
2868 error = got_error_from_errno("getline");
2870 if (f && fclose(f) == -1 && error == NULL)
2871 error = got_error_from_errno("fclose");
2878 if (error == NULL && kerr != KCGI_OK)
2879 error = gw_kcgi_error(kerr);
2883 static const struct got_error *
2884 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2886 const struct got_error *error = NULL;
2887 struct got_repository *repo;
2888 const char *gitconfig_owner;
2892 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2895 error = got_repo_open(&repo, dir, NULL);
2898 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2899 if (gitconfig_owner) {
2900 *owner = strdup(gitconfig_owner);
2902 error = got_error_from_errno("strdup");
2904 got_repo_close(repo);
2908 static const struct got_error *
2909 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2911 const struct got_error *error = NULL;
2913 char *d_file = NULL;
2919 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2920 return got_error_from_errno("asprintf");
2922 f = fopen(d_file, "r");
2924 if (errno != ENOENT && errno != EACCES)
2925 error = got_error_from_errno2("fopen", d_file);
2929 if (fseek(f, 0, SEEK_END) == -1) {
2930 error = got_ferror(f, GOT_ERR_IO);
2935 error = got_ferror(f, GOT_ERR_IO);
2938 if (fseek(f, 0, SEEK_SET) == -1) {
2939 error = got_ferror(f, GOT_ERR_IO);
2943 *url = calloc(len + 1, sizeof(**url));
2945 error = got_error_from_errno("calloc");
2949 n = fread(*url, 1, len, f);
2950 if (n == 0 && ferror(f))
2951 error = got_ferror(f, GOT_ERR_IO);
2953 if (f && fclose(f) == -1 && error == NULL)
2954 error = got_error_from_errno("fclose");
2959 static const struct got_error *
2960 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2961 int limit, int tag_type)
2963 const struct got_error *error = NULL;
2964 struct got_reflist_head refs;
2965 struct got_reflist_entry *re;
2967 char *id_str = NULL, *newline, *href_commits = NULL;
2968 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2969 struct got_tag_object *tag = NULL;
2970 enum kcgi_err kerr = KCGI_OK;
2971 int summary_header_displayed = 0, start_tag = 0, chk_next = 0;
2972 int prev_set = 0, tag_count = 0;
2974 SIMPLEQ_INIT(&refs);
2976 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2977 got_ref_cmp_tags, gw_trans->repo);
2981 SIMPLEQ_FOREACH(re, &refs, entry) {
2982 const char *refname;
2984 const char *tag_commit;
2986 struct got_object_id *id;
2987 struct got_commit_object *commit = NULL;
2989 refname = got_ref_get_name(re->ref);
2990 if (strncmp(refname, "refs/tags/", 10) != 0)
2994 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2998 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3000 if (error->code != GOT_ERR_OBJ_TYPE) {
3004 /* "lightweight" tag */
3005 error = got_object_open_as_commit(&commit,
3006 gw_trans->repo, id);
3011 tagger = got_object_commit_get_committer(commit);
3013 got_object_commit_get_committer_time(commit);
3014 error = got_object_id_str(&id_str, id);
3018 tagger = got_object_tag_get_tagger(tag);
3019 tagger_time = got_object_tag_get_tagger_time(tag);
3020 error = got_object_id_str(&id_str,
3021 got_object_tag_get_object_id(tag));
3026 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3027 strlen(id_str)) != 0)
3030 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3031 start_tag == 0 && strncmp(id_str, header->commit_id,
3032 strlen(id_str)) != 0) {
3040 if (prev_set == 0 && start_tag == 1) {
3041 gw_trans->next_prev_id = strdup(id_str);
3042 if (gw_trans->next_prev_id == NULL) {
3043 error = got_error_from_errno("strdup");
3050 gw_trans->next_id = strdup(id_str);
3051 if (gw_trans->next_id == NULL)
3052 error = got_error_from_errno("strdup");
3057 error = got_object_commit_get_logmsg(&tag_commit0,
3061 got_object_commit_close(commit);
3063 tag_commit0 = strdup(got_object_tag_get_message(tag));
3064 if (tag_commit0 == NULL) {
3065 error = got_error_from_errno("strdup");
3070 tag_commit = tag_commit0;
3071 while (*tag_commit == '\n')
3076 newline = strchr(tag_commit, '\n');
3080 if (summary_header_displayed == 0) {
3081 kerr = khtml_attr(gw_trans->gw_html_req,
3082 KELEM_DIV, KATTR_ID,
3083 "summary_tags_title_wrapper", KATTR__MAX);
3084 if (kerr != KCGI_OK)
3086 kerr = khtml_attr(gw_trans->gw_html_req,
3087 KELEM_DIV, KATTR_ID,
3088 "summary_tags_title", KATTR__MAX);
3089 if (kerr != KCGI_OK)
3091 kerr = khtml_puts(gw_trans->gw_html_req,
3093 if (kerr != KCGI_OK)
3095 kerr = khtml_closeelem(gw_trans->gw_html_req,
3097 if (kerr != KCGI_OK)
3099 kerr = khtml_attr(gw_trans->gw_html_req,
3100 KELEM_DIV, KATTR_ID,
3101 "summary_tags_content", KATTR__MAX);
3102 if (kerr != KCGI_OK)
3104 summary_header_displayed = 1;
3107 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3108 KATTR_ID, "tag_wrapper", KATTR__MAX);
3109 if (kerr != KCGI_OK)
3111 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3112 KATTR_ID, "tag_age", KATTR__MAX);
3113 if (kerr != KCGI_OK)
3115 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3118 kerr = khtml_puts(gw_trans->gw_html_req,
3120 if (kerr != KCGI_OK)
3122 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3123 if (kerr != KCGI_OK)
3125 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3126 KATTR_ID, "tag", KATTR__MAX);
3127 if (kerr != KCGI_OK)
3129 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3130 if (kerr != KCGI_OK)
3132 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3133 if (kerr != KCGI_OK)
3135 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3136 KATTR_ID, "tag_name", KATTR__MAX);
3137 if (kerr != KCGI_OK)
3139 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
3140 gw_trans->repo_name, id_str) == -1) {
3141 error = got_error_from_errno("asprintf");
3144 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3145 KATTR_HREF, href_tag, KATTR__MAX);
3146 if (kerr != KCGI_OK)
3148 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3149 if (kerr != KCGI_OK)
3151 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3152 if (kerr != KCGI_OK)
3155 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3156 KATTR_ID, "navs_wrapper", KATTR__MAX);
3157 if (kerr != KCGI_OK)
3159 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3160 KATTR_ID, "navs", KATTR__MAX);
3161 if (kerr != KCGI_OK)
3164 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3165 KATTR_HREF, href_tag, KATTR__MAX);
3166 if (kerr != KCGI_OK)
3168 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3169 if (kerr != KCGI_OK)
3171 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3172 if (kerr != KCGI_OK)
3175 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3176 if (kerr != KCGI_OK)
3178 if (asprintf(&href_briefs,
3179 "?path=%s&action=briefs&commit=%s",
3180 gw_trans->repo_name, id_str) == -1) {
3181 error = got_error_from_errno("asprintf");
3184 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3185 KATTR_HREF, href_briefs, KATTR__MAX);
3186 if (kerr != KCGI_OK)
3188 kerr = khtml_puts(gw_trans->gw_html_req,
3190 if (kerr != KCGI_OK)
3192 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3193 if (kerr != KCGI_OK)
3196 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3197 if (kerr != KCGI_OK)
3200 if (asprintf(&href_commits,
3201 "?path=%s&action=commits&commit=%s",
3202 gw_trans->repo_name, id_str) == -1) {
3203 error = got_error_from_errno("asprintf");
3206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3207 KATTR_HREF, href_commits, KATTR__MAX);
3208 if (kerr != KCGI_OK)
3210 kerr = khtml_puts(gw_trans->gw_html_req,
3212 if (kerr != KCGI_OK)
3214 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3215 if (kerr != KCGI_OK)
3218 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3219 KATTR_ID, "dotted_line", KATTR__MAX);
3220 if (kerr != KCGI_OK)
3222 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3223 if (kerr != KCGI_OK)
3227 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3228 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3229 if (kerr != KCGI_OK)
3231 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3232 if (kerr != KCGI_OK)
3234 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3235 if (kerr != KCGI_OK)
3237 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3238 KATTR_ID, "tag_info_date", KATTR__MAX);
3239 if (kerr != KCGI_OK)
3241 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3244 kerr = khtml_puts(gw_trans->gw_html_req,
3246 if (kerr != KCGI_OK)
3248 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3249 if (kerr != KCGI_OK)
3252 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3253 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3254 if (kerr != KCGI_OK)
3256 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3257 if (kerr != KCGI_OK)
3259 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3260 if (kerr != KCGI_OK)
3262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3263 KATTR_ID, "tag_info_date", KATTR__MAX);
3264 if (kerr != KCGI_OK)
3266 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3267 if (kerr != KCGI_OK)
3269 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3270 if (kerr != KCGI_OK)
3273 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3274 KATTR_ID, "tag_info", KATTR__MAX);
3275 if (kerr != KCGI_OK)
3277 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3278 if (kerr != KCGI_OK)
3284 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3285 if (kerr != KCGI_OK)
3288 if (limit && --limit == 0)
3292 got_object_tag_close(tag);
3305 href_commits = NULL;
3307 if (tag_count == 0) {
3308 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3309 "summary_tags_title_wrapper", KATTR__MAX);
3310 if (kerr != KCGI_OK)
3312 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3313 "summary_tags_title", KATTR__MAX);
3314 if (kerr != KCGI_OK)
3316 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3317 if (kerr != KCGI_OK)
3319 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3320 if (kerr != KCGI_OK)
3322 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3323 "summary_tags_content", KATTR__MAX);
3324 if (kerr != KCGI_OK)
3326 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3327 "tags_info", KATTR__MAX);
3328 if (kerr != KCGI_OK)
3330 kerr = khttp_puts(gw_trans->gw_req,
3331 "There are no tags for this repo.");
3332 if (kerr != KCGI_OK)
3334 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3338 got_object_tag_close(tag);
3345 got_ref_list_free(&refs);
3346 if (error == NULL && kerr != KCGI_OK)
3347 error = gw_kcgi_error(kerr);
3352 gw_free_header(struct gw_header *header)
3355 free(header->author);
3356 free(header->committer);
3357 free(header->refs_str);
3358 free(header->commit_id);
3359 free(header->parent_id);
3360 free(header->tree_id);
3361 free(header->commit_msg);
3364 static struct gw_header *
3367 struct gw_header *header;
3369 header = malloc(sizeof(*header));
3373 header->path = NULL;
3374 SIMPLEQ_INIT(&header->refs);
3376 header->refs_str = NULL;
3377 header->commit_id = NULL;
3378 header->committer = NULL;
3379 header->author = NULL;
3380 header->parent_id = NULL;
3381 header->tree_id = NULL;
3382 header->commit_msg = NULL;
3387 static const struct got_error *
3388 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3389 int limit, struct got_object_id *id)
3391 const struct got_error *error = NULL;
3392 struct got_commit_graph *graph = NULL;
3393 struct got_commit_object *commit = NULL;
3394 int chk_next = 0, chk_multi = 0, prev_set = 0;
3396 error = got_commit_graph_open(&graph, header->path, 0);
3400 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3406 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3409 if (error->code == GOT_ERR_ITER_COMPLETED)
3416 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3419 if (limit == 1 && chk_multi == 0 &&
3420 gw_trans->gw_conf->got_max_commits_display != 1) {
3421 error = gw_get_commit(gw_trans, header, commit, id);
3426 struct gw_header *n_header = NULL;
3427 if ((n_header = gw_init_header()) == NULL) {
3428 error = got_error_from_errno("malloc");
3431 error = got_ref_list(&n_header->refs, gw_trans->repo,
3432 NULL, got_ref_cmp_by_name, NULL);
3436 error = gw_get_commit(gw_trans, n_header, commit, id);
3439 got_ref_list_free(&n_header->refs);
3442 * we have a commit_id now, so copy it to next_prev_id
3443 * for navigation through gw_briefs and gw_commits
3445 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3446 (gw_trans->action == GW_BRIEFS ||
3447 gw_trans->action == GW_COMMITS ||
3448 gw_trans->action == GW_SUMMARY)) {
3450 gw_trans->next_prev_id =
3451 strdup(n_header->commit_id);
3452 if (gw_trans->next_prev_id == NULL) {
3453 error = got_error_from_errno("strdup");
3459 * check for one more commit before breaking,
3460 * so we know whether to navicate through gw_briefs
3461 * gw_commits and gw_summary
3463 if (chk_next && (gw_trans->action == GW_BRIEFS||
3464 gw_trans->action == GW_COMMITS ||
3465 gw_trans->action == GW_SUMMARY)) {
3466 gw_trans->next_id = strdup(n_header->commit_id);
3467 if (gw_trans->next_id == NULL)
3468 error = got_error_from_errno("strdup");
3472 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3475 if (error || (limit && --limit == 0)) {
3483 got_object_commit_close(commit);
3485 got_commit_graph_close(graph);
3489 static const struct got_error *
3490 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3491 struct got_commit_object *commit, struct got_object_id *id)
3493 const struct got_error *error = NULL;
3494 struct got_reflist_entry *re;
3495 struct got_object_id *id2 = NULL;
3496 struct got_object_qid *parent_id;
3497 char *commit_msg = NULL, *commit_msg0;
3500 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3503 struct got_tag_object *tag = NULL;
3506 if (got_ref_is_symbolic(re->ref))
3509 name = got_ref_get_name(re->ref);
3510 if (strncmp(name, "refs/", 5) == 0)
3512 if (strncmp(name, "got/", 4) == 0)
3514 if (strncmp(name, "heads/", 6) == 0)
3516 if (strncmp(name, "remotes/", 8) == 0)
3518 if (strncmp(name, "tags/", 5) == 0) {
3519 error = got_object_open_as_tag(&tag, gw_trans->repo,
3522 if (error->code != GOT_ERR_OBJ_TYPE)
3525 * Ref points at something other
3532 cmp = got_object_id_cmp(tag ?
3533 got_object_tag_get_object_id(tag) : re->id, id);
3535 got_object_tag_close(tag);
3538 s = header->refs_str;
3539 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3540 s ? ", " : "", name) == -1) {
3541 error = got_error_from_errno("asprintf");
3543 header->refs_str = NULL;
3549 error = got_object_id_str(&header->commit_id, id);
3553 error = got_object_id_str(&header->tree_id,
3554 got_object_commit_get_tree_id(commit));
3558 if (gw_trans->action == GW_DIFF) {
3559 parent_id = SIMPLEQ_FIRST(
3560 got_object_commit_get_parent_ids(commit));
3561 if (parent_id != NULL) {
3562 id2 = got_object_id_dup(parent_id->id);
3564 error = got_object_id_str(&header->parent_id, id2);
3569 header->parent_id = strdup("/dev/null");
3570 if (header->parent_id == NULL) {
3571 error = got_error_from_errno("strdup");
3577 header->committer_time =
3578 got_object_commit_get_committer_time(commit);
3581 strdup(got_object_commit_get_author(commit));
3582 if (header->author == NULL) {
3583 error = got_error_from_errno("strdup");
3587 strdup(got_object_commit_get_committer(commit));
3588 if (header->committer == NULL) {
3589 error = got_error_from_errno("strdup");
3592 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3596 commit_msg = commit_msg0;
3597 while (*commit_msg == '\n')
3600 header->commit_msg = strdup(commit_msg);
3601 if (header->commit_msg == NULL)
3602 error = got_error_from_errno("strdup");
3607 static const struct got_error *
3608 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3610 const struct got_error *error = NULL;
3611 char *in_repo_path = NULL;
3612 struct got_object_id *id = NULL;
3614 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3618 if (gw_trans->commit_id == NULL) {
3619 struct got_reference *head_ref;
3620 error = got_ref_open(&head_ref, gw_trans->repo,
3621 gw_trans->headref, 0);
3625 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3626 got_ref_close(head_ref);
3630 struct got_reference *ref;
3632 error = got_ref_open(&ref, gw_trans->repo,
3633 gw_trans->commit_id, 0);
3634 if (error == NULL) {
3636 error = got_ref_resolve(&id, gw_trans->repo, ref);
3640 error = got_object_get_type(&obj_type, gw_trans->repo,
3644 if (obj_type == GOT_OBJ_TYPE_TAG) {
3645 struct got_tag_object *tag;
3646 error = got_object_open_as_tag(&tag,
3647 gw_trans->repo, id);
3650 if (got_object_tag_get_object_type(tag) !=
3651 GOT_OBJ_TYPE_COMMIT) {
3652 got_object_tag_close(tag);
3653 error = got_error(GOT_ERR_OBJ_TYPE);
3657 id = got_object_id_dup(
3658 got_object_tag_get_object_id(tag));
3660 error = got_error_from_errno(
3661 "got_object_id_dup");
3662 got_object_tag_close(tag);
3665 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3666 error = got_error(GOT_ERR_OBJ_TYPE);
3670 error = got_repo_match_object_id_prefix(&id,
3671 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3677 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3678 gw_trans->repo_path, 1);
3683 header->path = strdup(in_repo_path);
3684 if (header->path == NULL) {
3685 error = got_error_from_errno("strdup");
3690 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3691 got_ref_cmp_by_name, NULL);
3695 error = gw_get_commits(gw_trans, header, limit, id);
3697 got_ref_list_free(&header->refs);
3707 char datebuf[11]; /* YYYY-MM-DD + NUL */
3710 struct gw_blame_cb_args {
3711 struct blame_line *lines;
3715 off_t *line_offsets;
3717 struct got_repository *repo;
3718 struct gw_trans *gw_trans;
3721 static const struct got_error *
3722 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3724 const struct got_error *err = NULL;
3725 struct gw_blame_cb_args *a = arg;
3726 struct blame_line *bline;
3728 size_t linesize = 0;
3729 struct got_commit_object *commit = NULL;
3732 time_t committer_time;
3733 enum kcgi_err kerr = KCGI_OK;
3735 if (nlines != a->nlines ||
3736 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3737 return got_error(GOT_ERR_RANGE);
3740 return NULL; /* no change in this commit */
3742 /* Annotate this line. */
3743 bline = &a->lines[lineno - 1];
3744 if (bline->annotated)
3746 err = got_object_id_str(&bline->id_str, id);
3750 err = got_object_open_as_commit(&commit, a->repo, id);
3754 bline->committer = strdup(got_object_commit_get_committer(commit));
3755 if (bline->committer == NULL) {
3756 err = got_error_from_errno("strdup");
3760 committer_time = got_object_commit_get_committer_time(commit);
3761 if (localtime_r(&committer_time, &tm) == NULL)
3762 return got_error_from_errno("localtime_r");
3763 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3764 &tm) >= sizeof(bline->datebuf)) {
3765 err = got_error(GOT_ERR_NO_SPACE);
3768 bline->annotated = 1;
3770 /* Print lines annotated so far. */
3771 bline = &a->lines[a->lineno_cur - 1];
3772 if (!bline->annotated)
3775 offset = a->line_offsets[a->lineno_cur - 1];
3776 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3777 err = got_error_from_errno("fseeko");
3781 while (bline->annotated) {
3782 char *smallerthan, *at, *nl, *committer;
3783 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3786 if (getline(&line, &linesize, a->f) == -1) {
3788 err = got_error_from_errno("getline");
3792 committer = bline->committer;
3793 smallerthan = strchr(committer, '<');
3794 if (smallerthan && smallerthan[1] != '\0')
3795 committer = smallerthan + 1;
3796 at = strchr(committer, '@');
3799 len = strlen(committer);
3801 committer[8] = '\0';
3803 nl = strchr(line, '\n');
3807 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3808 "blame_wrapper", KATTR__MAX);
3809 if (kerr != KCGI_OK)
3811 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3812 "blame_number", KATTR__MAX);
3813 if (kerr != KCGI_OK)
3815 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3816 a->lineno_cur) == -1)
3818 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3819 if (kerr != KCGI_OK)
3821 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3822 if (kerr != KCGI_OK)
3825 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3826 "blame_hash", KATTR__MAX);
3827 if (kerr != KCGI_OK)
3830 if (asprintf(&href_diff,
3831 "?path=%s&action=diff&commit=%s",
3832 a->gw_trans->repo_name, bline->id_str) == -1) {
3833 err = got_error_from_errno("asprintf");
3836 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3837 err = got_error_from_errno("asprintf");
3840 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3841 KATTR_HREF, href_diff, KATTR__MAX);
3842 if (kerr != KCGI_OK)
3844 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3845 if (kerr != KCGI_OK)
3847 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3848 if (kerr != KCGI_OK)
3851 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3852 "blame_date", KATTR__MAX);
3853 if (kerr != KCGI_OK)
3855 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3856 if (kerr != KCGI_OK)
3858 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3859 if (kerr != KCGI_OK)
3862 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3863 "blame_author", KATTR__MAX);
3864 if (kerr != KCGI_OK)
3866 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3867 if (kerr != KCGI_OK)
3869 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3870 if (kerr != KCGI_OK)
3873 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3874 "blame_code", KATTR__MAX);
3875 if (kerr != KCGI_OK)
3877 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3878 if (kerr != KCGI_OK)
3880 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3881 if (kerr != KCGI_OK)
3884 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3885 if (kerr != KCGI_OK)
3889 bline = &a->lines[a->lineno_cur - 1];
3897 got_object_commit_close(commit);
3899 if (err == NULL && kerr != KCGI_OK)
3900 err = gw_kcgi_error(kerr);
3904 static const struct got_error *
3905 gw_output_file_blame(struct gw_trans *gw_trans)
3907 const struct got_error *error = NULL;
3908 struct got_object_id *obj_id = NULL;
3909 struct got_object_id *commit_id = NULL;
3910 struct got_blob_object *blob = NULL;
3911 char *path = NULL, *in_repo_path = NULL;
3912 struct gw_blame_cb_args bca;
3916 if (asprintf(&path, "%s%s%s",
3917 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3918 gw_trans->repo_folder ? "/" : "",
3919 gw_trans->repo_file) == -1) {
3920 error = got_error_from_errno("asprintf");
3924 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3928 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3929 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3933 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3938 if (obj_id == NULL) {
3939 error = got_error(GOT_ERR_NO_OBJ);
3943 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3947 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3948 error = got_error(GOT_ERR_OBJ_TYPE);
3952 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3956 bca.f = got_opentemp();
3957 if (bca.f == NULL) {
3958 error = got_error_from_errno("got_opentemp");
3961 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3962 &bca.line_offsets, bca.f, blob);
3963 if (error || bca.nlines == 0)
3966 /* Don't include \n at EOF in the blame line count. */
3967 if (bca.line_offsets[bca.nlines - 1] == filesize)
3970 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3971 if (bca.lines == NULL) {
3972 error = got_error_from_errno("calloc");
3976 bca.nlines_prec = 0;
3982 bca.repo = gw_trans->repo;
3983 bca.gw_trans = gw_trans;
3985 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3994 free(bca.line_offsets);
3995 for (i = 0; i < bca.nlines; i++) {
3996 struct blame_line *bline = &bca.lines[i];
3997 free(bline->id_str);
3998 free(bline->committer);
4001 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4002 error = got_error_from_errno("fclose");
4005 got_object_blob_close(blob);
4009 static const struct got_error *
4010 gw_output_blob_buf(struct gw_trans *gw_trans)
4012 const struct got_error *error = NULL;
4013 struct got_object_id *obj_id = NULL;
4014 struct got_object_id *commit_id = NULL;
4015 struct got_blob_object *blob = NULL;
4016 char *path = NULL, *in_repo_path = NULL;
4017 int obj_type, set_mime = 0;
4020 enum kcgi_err kerr = KCGI_OK;
4022 if (asprintf(&path, "%s%s%s",
4023 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4024 gw_trans->repo_folder ? "/" : "",
4025 gw_trans->repo_file) == -1) {
4026 error = got_error_from_errno("asprintf");
4030 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
4034 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4035 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
4039 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4044 if (obj_id == NULL) {
4045 error = got_error(GOT_ERR_NO_OBJ);
4049 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4053 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4054 error = got_error(GOT_ERR_OBJ_TYPE);
4058 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4062 hdrlen = got_object_blob_get_hdrlen(blob);
4064 error = got_object_blob_read_block(&len, blob);
4067 buf = got_object_blob_get_read_buf(blob);
4070 * Skip blob object header first time around,
4071 * which also contains a zero byte.
4074 if (set_mime == 0) {
4075 if (isbinary(buf, len - hdrlen))
4076 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4078 gw_trans->mime = KMIME_TEXT_PLAIN;
4080 error = gw_display_index(gw_trans);
4084 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4085 if (kerr != KCGI_OK)
4095 got_object_blob_close(blob);
4096 if (error == NULL && kerr != KCGI_OK)
4097 error = gw_kcgi_error(kerr);
4101 static const struct got_error *
4102 gw_output_repo_tree(struct gw_trans *gw_trans)
4104 const struct got_error *error = NULL;
4105 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4106 struct got_tree_object *tree = NULL;
4107 char *path = NULL, *in_repo_path = NULL;
4108 char *id_str = NULL;
4109 char *build_folder = NULL;
4110 char *href_blob = NULL, *href_blame = NULL;
4111 const char *class = NULL;
4112 int nentries, i, class_flip = 0;
4113 enum kcgi_err kerr = KCGI_OK;
4115 if (gw_trans->repo_folder != NULL) {
4116 path = strdup(gw_trans->repo_folder);
4118 error = got_error_from_errno("strdup");
4122 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4123 gw_trans->repo_path, 1);
4127 path = in_repo_path;
4130 if (gw_trans->commit_id == NULL) {
4131 struct got_reference *head_ref;
4132 error = got_ref_open(&head_ref, gw_trans->repo,
4133 gw_trans->headref, 0);
4136 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4139 got_ref_close(head_ref);
4141 * gw_trans->commit_id was not parsed from the querystring
4142 * we hit this code path from gw_index, where we don't know the
4143 * commit values for the tree link yet, so set
4144 * gw_trans->commit_id here to continue further into the tree
4146 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4151 error = got_repo_match_object_id(&commit_id, NULL,
4152 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
4158 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4163 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4167 nentries = got_object_tree_get_nentries(tree);
4168 for (i = 0; i < nentries; i++) {
4169 struct got_tree_entry *te;
4170 const char *modestr = "";
4173 te = got_object_tree_get_entry(tree, i);
4175 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4179 mode = got_tree_entry_get_mode(te);
4180 if (got_object_tree_entry_is_submodule(te))
4182 else if (S_ISLNK(mode))
4184 else if (S_ISDIR(mode))
4186 else if (mode & S_IXUSR)
4189 if (class_flip == 0) {
4190 class = "back_lightgray";
4193 class = "back_white";
4197 if (S_ISDIR(mode)) {
4198 if (asprintf(&build_folder, "%s/%s",
4199 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4200 got_tree_entry_get_name(te)) == -1) {
4201 error = got_error_from_errno("asprintf");
4204 if (asprintf(&href_blob,
4205 "?path=%s&action=%s&commit=%s&folder=%s",
4206 gw_trans->repo_name, gw_get_action_name(gw_trans),
4207 gw_trans->commit_id, build_folder) == -1) {
4208 error = got_error_from_errno("asprintf");
4212 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4213 KATTR_ID, "tree_wrapper", KATTR__MAX);
4214 if (kerr != KCGI_OK)
4216 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4217 KATTR_ID, "tree_line", KATTR_CLASS, class,
4219 if (kerr != KCGI_OK)
4221 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4222 KATTR_HREF, href_blob, KATTR_CLASS,
4223 "diff_directory", KATTR__MAX);
4224 if (kerr != KCGI_OK)
4226 kerr = khtml_puts(gw_trans->gw_html_req,
4227 got_tree_entry_get_name(te));
4228 if (kerr != KCGI_OK)
4230 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4231 if (kerr != KCGI_OK)
4233 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4234 if (kerr != KCGI_OK)
4236 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4237 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4239 if (kerr != KCGI_OK)
4241 kerr = khtml_entity(gw_trans->gw_html_req,
4243 if (kerr != KCGI_OK)
4245 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4246 if (kerr != KCGI_OK)
4249 if (asprintf(&href_blob,
4250 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4251 gw_trans->repo_name, "blob", gw_trans->commit_id,
4252 got_tree_entry_get_name(te),
4253 gw_trans->repo_folder ?
4254 gw_trans->repo_folder : "") == -1) {
4255 error = got_error_from_errno("asprintf");
4258 if (asprintf(&href_blame,
4259 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4260 gw_trans->repo_name, "blame", gw_trans->commit_id,
4261 got_tree_entry_get_name(te),
4262 gw_trans->repo_folder ?
4263 gw_trans->repo_folder : "") == -1) {
4264 error = got_error_from_errno("asprintf");
4268 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4269 KATTR_ID, "tree_wrapper", KATTR__MAX);
4270 if (kerr != KCGI_OK)
4272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4273 KATTR_ID, "tree_line", KATTR_CLASS, class,
4275 if (kerr != KCGI_OK)
4277 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4278 KATTR_HREF, href_blob, KATTR__MAX);
4279 if (kerr != KCGI_OK)
4281 kerr = khtml_puts(gw_trans->gw_html_req,
4282 got_tree_entry_get_name(te));
4283 if (kerr != KCGI_OK)
4285 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4286 if (kerr != KCGI_OK)
4288 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4289 if (kerr != KCGI_OK)
4291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4292 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4294 if (kerr != KCGI_OK)
4297 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4298 KATTR_HREF, href_blob, KATTR__MAX);
4299 if (kerr != KCGI_OK)
4301 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4302 if (kerr != KCGI_OK)
4304 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4305 if (kerr != KCGI_OK)
4308 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4309 if (kerr != KCGI_OK)
4312 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4313 KATTR_HREF, href_blame, KATTR__MAX);
4314 if (kerr != KCGI_OK)
4316 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4317 if (kerr != KCGI_OK)
4320 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4321 if (kerr != KCGI_OK)
4329 build_folder = NULL;
4333 got_object_tree_close(tree);
4340 if (error == NULL && kerr != KCGI_OK)
4341 error = gw_kcgi_error(kerr);
4345 static const struct got_error *
4346 gw_output_repo_heads(struct gw_trans *gw_trans)
4348 const struct got_error *error = NULL;
4349 struct got_reflist_head refs;
4350 struct got_reflist_entry *re;
4351 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4352 char *href_commits = NULL;
4353 enum kcgi_err kerr = KCGI_OK;
4355 SIMPLEQ_INIT(&refs);
4357 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4358 got_ref_cmp_by_name, NULL);
4362 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4363 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4364 if (kerr != KCGI_OK)
4366 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4367 KATTR_ID, "summary_heads_title", KATTR__MAX);
4368 if (kerr != KCGI_OK)
4370 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4371 if (kerr != KCGI_OK)
4373 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4374 if (kerr != KCGI_OK)
4376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4377 KATTR_ID, "summary_heads_content", KATTR__MAX);
4378 if (kerr != KCGI_OK)
4381 SIMPLEQ_FOREACH(re, &refs, entry) {
4382 const char *refname;
4384 if (got_ref_is_symbolic(re->ref))
4387 refname = got_ref_get_name(re->ref);
4388 if (strncmp(refname, "refs/heads/", 11) != 0)
4391 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4396 if (strncmp(refname, "refs/heads/", 11) == 0)
4399 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4400 KATTR_ID, "heads_wrapper", KATTR__MAX);
4401 if (kerr != KCGI_OK)
4403 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4404 KATTR_ID, "heads_age", KATTR__MAX);
4405 if (kerr != KCGI_OK)
4407 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4408 if (kerr != KCGI_OK)
4410 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4411 if (kerr != KCGI_OK)
4413 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4414 KATTR_ID, "heads_space", KATTR__MAX);
4415 if (kerr != KCGI_OK)
4417 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4418 if (kerr != KCGI_OK)
4420 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4421 if (kerr != KCGI_OK)
4423 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4424 KATTR_ID, "head", KATTR__MAX);
4425 if (kerr != KCGI_OK)
4427 if (asprintf(&href_summary,
4428 "?path=%s&action=summary&headref=%s",
4429 gw_trans->repo_name, refname) == -1) {
4430 error = got_error_from_errno("asprintf");
4433 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4434 href_summary, KATTR__MAX);
4435 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4436 if (kerr != KCGI_OK)
4438 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4439 if (kerr != KCGI_OK)
4442 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4443 "navs_wrapper", KATTR__MAX);
4444 if (kerr != KCGI_OK)
4446 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4447 "navs", KATTR__MAX);
4448 if (kerr != KCGI_OK)
4451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4452 href_summary, KATTR__MAX);
4453 if (kerr != KCGI_OK)
4455 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4456 if (kerr != KCGI_OK)
4458 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4459 if (kerr != KCGI_OK)
4462 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4463 if (kerr != KCGI_OK)
4465 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4466 gw_trans->repo_name, refname) == -1) {
4467 error = got_error_from_errno("asprintf");
4470 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4471 href_briefs, KATTR__MAX);
4472 if (kerr != KCGI_OK)
4474 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4475 if (kerr != KCGI_OK)
4477 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4478 if (kerr != KCGI_OK)
4481 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4482 if (kerr != KCGI_OK)
4485 if (asprintf(&href_commits,
4486 "?path=%s&action=commits&headref=%s",
4487 gw_trans->repo_name, refname) == -1) {
4488 error = got_error_from_errno("asprintf");
4491 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4492 href_commits, KATTR__MAX);
4493 if (kerr != KCGI_OK)
4495 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4496 if (kerr != KCGI_OK)
4498 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4499 if (kerr != KCGI_OK)
4502 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4503 "dotted_line", KATTR__MAX);
4504 if (kerr != KCGI_OK)
4506 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4507 if (kerr != KCGI_OK)
4510 href_summary = NULL;
4514 href_commits = NULL;
4517 got_ref_list_free(&refs);
4524 static const struct got_error *
4525 gw_output_site_link(struct gw_trans *gw_trans)
4527 const struct got_error *error = NULL;
4528 char *href_summary = NULL;
4529 enum kcgi_err kerr = KCGI_OK;
4531 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4532 "site_link", KATTR__MAX);
4533 if (kerr != KCGI_OK)
4535 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4537 if (kerr != KCGI_OK)
4539 kerr = khtml_puts(gw_trans->gw_html_req,
4540 gw_trans->gw_conf->got_site_link);
4541 if (kerr != KCGI_OK)
4543 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4544 if (kerr != KCGI_OK)
4547 if (gw_trans->repo_name != NULL) {
4548 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4549 if (kerr != KCGI_OK)
4551 if (asprintf(&href_summary, "?path=%s&action=summary",
4552 gw_trans->repo_name) == -1)
4554 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4555 href_summary, KATTR__MAX);
4556 if (kerr != KCGI_OK)
4558 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4559 if (kerr != KCGI_OK)
4561 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4562 if (kerr != KCGI_OK)
4564 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4565 if (kerr != KCGI_OK)
4567 kerr = khtml_puts(gw_trans->gw_html_req,
4568 gw_get_action_name(gw_trans));
4569 if (kerr != KCGI_OK)
4573 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4574 if (kerr != KCGI_OK)
4581 static const struct got_error *
4582 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4584 const struct got_error *error = NULL;
4586 enum kcgi_err kerr = KCGI_OK;
4588 if (strncmp(buf, "-", 1) == 0)
4589 color = "diff_minus";
4590 else if (strncmp(buf, "+", 1) == 0)
4591 color = "diff_plus";
4592 else if (strncmp(buf, "@@", 2) == 0)
4593 color = "diff_chunk_header";
4594 else if (strncmp(buf, "@@", 2) == 0)
4595 color = "diff_chunk_header";
4596 else if (strncmp(buf, "commit +", 8) == 0)
4597 color = "diff_meta";
4598 else if (strncmp(buf, "commit -", 8) == 0)
4599 color = "diff_meta";
4600 else if (strncmp(buf, "blob +", 6) == 0)
4601 color = "diff_meta";
4602 else if (strncmp(buf, "blob -", 6) == 0)
4603 color = "diff_meta";
4604 else if (strncmp(buf, "file +", 6) == 0)
4605 color = "diff_meta";
4606 else if (strncmp(buf, "file -", 6) == 0)
4607 color = "diff_meta";
4608 else if (strncmp(buf, "from:", 5) == 0)
4609 color = "diff_author";
4610 else if (strncmp(buf, "via:", 4) == 0)
4611 color = "diff_author";
4612 else if (strncmp(buf, "date:", 5) == 0)
4613 color = "diff_date";
4614 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4615 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4616 if (error == NULL && kerr != KCGI_OK)
4617 error = gw_kcgi_error(kerr);
4622 main(int argc, char *argv[])
4624 const struct got_error *error = NULL;
4625 struct gw_trans *gw_trans;
4626 struct gw_dir *dir = NULL, *tdir;
4627 const char *page = "index";
4629 enum kcgi_err kerr = KCGI_OK;
4631 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4634 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4637 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4640 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4643 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4644 if (kerr != KCGI_OK) {
4645 error = gw_kcgi_error(kerr);
4649 if ((gw_trans->gw_conf =
4650 malloc(sizeof(struct gotweb_conf))) == NULL) {
4652 error = got_error_from_errno("malloc");
4656 TAILQ_INIT(&gw_trans->gw_dirs);
4657 TAILQ_INIT(&gw_trans->gw_headers);
4659 gw_trans->action = -1;
4661 gw_trans->repos_total = 0;
4662 gw_trans->repo_path = NULL;
4663 gw_trans->commit_id = NULL;
4664 gw_trans->next_id = NULL;
4665 gw_trans->next_prev_id = NULL;
4666 gw_trans->prev_id = NULL;
4667 gw_trans->prev_prev_id = NULL;
4668 gw_trans->headref = GOT_REF_HEAD;
4669 gw_trans->mime = KMIME_TEXT_HTML;
4670 gw_trans->gw_tmpl->key = gw_templs;
4671 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4672 gw_trans->gw_tmpl->arg = gw_trans;
4673 gw_trans->gw_tmpl->cb = gw_template;
4674 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4678 error = gw_parse_querystring(gw_trans);
4682 if (gw_trans->action == GW_BLOB)
4683 error = gw_blob(gw_trans);
4685 error = gw_display_index(gw_trans);
4688 free(gw_trans->gw_conf->got_repos_path);
4689 free(gw_trans->gw_conf->got_site_name);
4690 free(gw_trans->gw_conf->got_site_owner);
4691 free(gw_trans->gw_conf->got_site_link);
4692 free(gw_trans->gw_conf->got_logo);
4693 free(gw_trans->gw_conf->got_logo_url);
4694 free(gw_trans->gw_conf);
4695 free(gw_trans->commit_id);
4696 free(gw_trans->next_id);
4697 free(gw_trans->next_prev_id);
4698 free(gw_trans->prev_id);
4699 free(gw_trans->prev_prev_id);
4700 free(gw_trans->repo_path);
4702 got_repo_close(gw_trans->repo);
4704 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4706 free(dir->description);
4715 khttp_free(gw_trans->gw_req);