2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
20 #include <sys/types.h>
35 #include <got_error.h>
36 #include <got_object.h>
37 #include <got_reference.h>
38 #include <got_repository.h>
40 #include <got_cancel.h>
41 #include <got_worktree.h>
43 #include <got_commit_graph.h>
44 #include <got_blame.h>
45 #include <got_privsep.h>
46 #include <got_opentemp.h>
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 TAILQ_HEAD(headers, gw_header) gw_headers;
59 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
60 struct got_repository *repo;
61 struct gw_dir *gw_dir;
62 struct gotweb_config *gw_conf;
63 struct ktemplate *gw_tmpl;
64 struct khtmlreq *gw_html_req;
66 const struct got_error *error;
67 const char *repo_name;
72 const char *repo_file;
77 unsigned int repos_total;
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;
139 static const char *const gw_templs[TEMPL__MAX] = {
149 static const struct kvalid gw_keys[KEY__ZMAX] = {
150 { kvalid_stringne, "action" },
151 { kvalid_stringne, "commit" },
152 { kvalid_stringne, "file" },
153 { kvalid_stringne, "folder" },
154 { kvalid_stringne, "headref" },
155 { kvalid_int, "page" },
156 { kvalid_stringne, "path" },
157 { kvalid_stringne, "prev" },
160 static struct gw_header *gw_init_header(void);
162 static void gw_free_header(struct gw_header *);
164 static int gw_template(size_t, void *);
166 static const struct got_error *gw_error(struct gw_trans *);
167 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
168 static const struct got_error *gw_get_repo_description(char **,
169 struct gw_trans *, char *);
170 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
172 static const struct got_error *gw_get_time_str(char **, time_t, int);
173 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
174 char *, const char *, int);
175 static const struct got_error *gw_output_file_blame(struct gw_trans *,
177 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 *,
181 static const struct got_error *gw_output_diff(struct gw_trans *,
183 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
184 struct gw_header *, int, int);
185 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
186 static const struct got_error *gw_output_site_link(struct gw_trans *);
187 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
189 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
191 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
193 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
195 static const struct got_error *gw_gen_author_header(struct gw_trans *,
197 static const struct got_error *gw_gen_age_header(struct gw_trans *,
199 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
201 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
203 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
204 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
206 static const struct got_error *gw_display_index(struct gw_trans *);
207 static const struct got_error *gw_get_header(struct gw_trans *,
208 struct gw_header *, int);
209 static const struct got_error *gw_get_commits(struct gw_trans *,
210 struct gw_header *, int,
211 struct got_object_id *);
212 static const struct got_error *gw_get_commit(struct gw_trans *,
214 struct got_commit_object *,
215 struct got_object_id *);
216 static const struct got_error *gw_apply_unveil(const char *);
217 static const struct got_error *gw_blame_cb(void *, int, int,
218 struct got_commit_object *,
219 struct got_object_id *);
220 static const struct got_error *gw_load_got_paths(struct gw_trans *);
221 static const struct got_error *gw_load_got_path(struct gw_trans *,
223 static const struct got_error *gw_parse_querystring(struct gw_trans *);
224 static const struct got_error *gw_blame(struct gw_trans *);
225 static const struct got_error *gw_blob(struct gw_trans *);
226 static const struct got_error *gw_diff(struct gw_trans *);
227 static const struct got_error *gw_index(struct gw_trans *);
228 static const struct got_error *gw_commits(struct gw_trans *);
229 static const struct got_error *gw_briefs(struct gw_trans *);
230 static const struct got_error *gw_summary(struct gw_trans *);
231 static const struct got_error *gw_tree(struct gw_trans *);
232 static const struct got_error *gw_tag(struct gw_trans *);
233 static const struct got_error *gw_tags(struct gw_trans *);
235 struct gw_query_action {
236 unsigned int func_id;
237 const char *func_name;
238 const struct got_error *(*func_main)(struct gw_trans *);
242 enum gw_query_actions {
256 static const struct gw_query_action gw_query_funcs[] = {
257 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
258 { GW_BLOB, "blob", NULL, NULL },
259 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
260 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
261 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
262 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
263 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
264 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
265 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
266 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
267 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
271 gw_get_action_name(struct gw_trans *gw_trans)
273 return gw_query_funcs[gw_trans->action].func_name;
276 static const struct got_error *
277 gw_kcgi_error(enum kcgi_err kerr)
282 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
283 return got_error(GOT_ERR_CANCELLED);
285 if (kerr == KCGI_ENOMEM)
286 return got_error_set_errno(ENOMEM,
287 kcgi_strerror(kerr));
289 if (kerr == KCGI_ENFILE)
290 return got_error_set_errno(ENFILE,
291 kcgi_strerror(kerr));
293 if (kerr == KCGI_EAGAIN)
294 return got_error_set_errno(EAGAIN,
295 kcgi_strerror(kerr));
297 if (kerr == KCGI_FORM)
298 return got_error_msg(GOT_ERR_IO,
299 kcgi_strerror(kerr));
301 return got_error_from_errno(kcgi_strerror(kerr));
304 static const struct got_error *
305 gw_apply_unveil(const char *repo_path)
307 const struct got_error *err;
310 if (unveil("gmon.out", "rwc") != 0)
311 return got_error_from_errno2("unveil", "gmon.out");
313 if (repo_path && unveil(repo_path, "r") != 0)
314 return got_error_from_errno2("unveil", repo_path);
316 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
317 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
319 err = got_privsep_unveil_exec_helpers();
323 if (unveil(NULL, NULL) != 0)
324 return got_error_from_errno("unveil");
330 isbinary(const uint8_t *buf, size_t n)
334 for (i = 0; i < n; i++)
340 static const struct got_error *
341 gw_blame(struct gw_trans *gw_trans)
343 const struct got_error *error = NULL;
344 struct gw_header *header = NULL;
346 enum kcgi_err kerr = KCGI_OK;
349 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
351 return got_error_from_errno("pledge");
353 if ((header = gw_init_header()) == NULL)
354 return got_error_from_errno("malloc");
356 error = gw_apply_unveil(gw_trans->gw_dir->path);
360 /* check querystring */
361 if (gw_trans->repo_file == NULL) {
362 error = got_error_msg(GOT_ERR_QUERYSTRING,
363 "file required in querystring");
366 if (gw_trans->commit_id == NULL) {
367 error = got_error_msg(GOT_ERR_QUERYSTRING,
368 "commit required in querystring");
372 error = gw_get_header(gw_trans, header, 1);
375 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
376 "blame_header_wrapper", KATTR__MAX);
379 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
380 "blame_header", KATTR__MAX);
383 error = gw_get_time_str(&age, header->committer_time,
387 error = gw_gen_age_header(gw_trans, age ?age : "");
390 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
393 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
396 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
397 "dotted_line", KATTR__MAX);
400 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
404 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
405 "blame", KATTR__MAX);
408 error = gw_output_file_blame(gw_trans, header);
411 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
413 gw_free_header(header);
414 if (error == NULL && kerr != KCGI_OK)
415 error = gw_kcgi_error(kerr);
419 static const struct got_error *
420 gw_blob(struct gw_trans *gw_trans)
422 const struct got_error *error = NULL, *err = NULL;
423 struct gw_header *header = NULL;
424 enum kcgi_err kerr = KCGI_OK;
427 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
429 return got_error_from_errno("pledge");
431 if ((header = gw_init_header()) == NULL)
432 return got_error_from_errno("malloc");
434 error = gw_apply_unveil(gw_trans->gw_dir->path);
438 /* check querystring */
439 if (gw_trans->repo_file == NULL) {
440 error = got_error_msg(GOT_ERR_QUERYSTRING,
441 "file required in querystring");
444 if (gw_trans->commit_id == NULL) {
445 error = got_error_msg(GOT_ERR_QUERYSTRING,
446 "commit required in querystring");
449 error = gw_get_header(gw_trans, header, 1);
453 error = gw_output_blob_buf(gw_trans, header);
457 gw_trans->mime = KMIME_TEXT_PLAIN;
458 err = gw_display_index(gw_trans);
463 kerr = khttp_puts(gw_trans->gw_req, error->msg);
466 gw_free_header(header);
467 if (error == NULL && kerr != KCGI_OK)
468 error = gw_kcgi_error(kerr);
472 static const struct got_error *
473 gw_diff(struct gw_trans *gw_trans)
475 const struct got_error *error = NULL;
476 struct gw_header *header = NULL;
478 enum kcgi_err kerr = KCGI_OK;
481 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
483 return got_error_from_errno("pledge");
485 if ((header = gw_init_header()) == NULL)
486 return got_error_from_errno("malloc");
488 error = gw_apply_unveil(gw_trans->gw_dir->path);
492 error = gw_get_header(gw_trans, header, 1);
496 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
497 "diff_header_wrapper", KATTR__MAX);
500 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
501 "diff_header", KATTR__MAX);
504 error = gw_gen_diff_header(gw_trans, header->parent_id,
508 error = gw_gen_commit_header(gw_trans, header->commit_id,
512 error = gw_gen_tree_header(gw_trans, header->tree_id);
515 error = gw_gen_author_header(gw_trans, header->author);
518 error = gw_gen_committer_header(gw_trans, header->author);
521 error = gw_get_time_str(&age, header->committer_time,
525 error = gw_gen_age_header(gw_trans, age ?age : "");
528 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
531 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
534 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
535 "dotted_line", KATTR__MAX);
538 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
542 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
546 error = gw_output_diff(gw_trans, header);
550 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
552 gw_free_header(header);
554 if (error == NULL && kerr != KCGI_OK)
555 error = gw_kcgi_error(kerr);
559 static const struct got_error *
560 gw_index(struct gw_trans *gw_trans)
562 const struct got_error *error = NULL;
563 struct gw_dir *gw_dir = NULL;
564 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
565 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
566 char *href_tags = NULL;
567 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
568 enum kcgi_err kerr = KCGI_OK;
571 if (pledge("stdio rpath proc exec sendfd unveil",
573 error = got_error_from_errno("pledge");
577 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
581 error = gw_load_got_paths(gw_trans);
585 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
586 "index_header", KATTR__MAX);
588 return gw_kcgi_error(kerr);
589 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
590 "index_header_project", KATTR__MAX);
592 return gw_kcgi_error(kerr);
593 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
595 return gw_kcgi_error(kerr);
596 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
598 return gw_kcgi_error(kerr);
600 if (gw_trans->gw_conf->got_show_repo_description) {
601 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
602 "index_header_description", KATTR__MAX);
604 return gw_kcgi_error(kerr);
605 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
607 return gw_kcgi_error(kerr);
608 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
610 return gw_kcgi_error(kerr);
613 if (gw_trans->gw_conf->got_show_repo_owner) {
614 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
615 "index_header_owner", KATTR__MAX);
617 return gw_kcgi_error(kerr);
618 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
620 return gw_kcgi_error(kerr);
621 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
623 return gw_kcgi_error(kerr);
626 if (gw_trans->gw_conf->got_show_repo_age) {
627 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
628 "index_header_age", KATTR__MAX);
630 return gw_kcgi_error(kerr);
631 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
633 return gw_kcgi_error(kerr);
634 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
636 return gw_kcgi_error(kerr);
639 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
641 return gw_kcgi_error(kerr);
643 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
644 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
645 "index_wrapper", KATTR__MAX);
647 return gw_kcgi_error(kerr);
648 kerr = khtml_printf(gw_trans->gw_html_req,
649 "No repositories found in %s",
650 gw_trans->gw_conf->got_repos_path);
652 return gw_kcgi_error(kerr);
653 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
655 return gw_kcgi_error(kerr);
656 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
657 "dotted_line", KATTR__MAX);
659 return gw_kcgi_error(kerr);
660 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
662 return gw_kcgi_error(kerr);
666 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
669 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
670 if (gw_trans->page > 0 && (gw_trans->page *
671 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
678 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
679 "index_wrapper", KATTR__MAX);
683 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
684 gw_dir->name, "action", "summary", NULL);
685 if (href_summary == NULL) {
686 error = got_error_from_errno("khttp_urlpart");
689 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
690 "index_project", KATTR__MAX);
693 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
694 href_summary, KATTR__MAX);
697 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
700 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
703 if (gw_trans->gw_conf->got_show_repo_description) {
704 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
705 KATTR_ID, "index_project_description", KATTR__MAX);
708 kerr = khtml_puts(gw_trans->gw_html_req,
709 gw_dir->description ? gw_dir->description : "");
712 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
716 if (gw_trans->gw_conf->got_show_repo_owner) {
717 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
718 KATTR_ID, "index_project_owner", KATTR__MAX);
721 kerr = khtml_puts(gw_trans->gw_html_req,
722 gw_dir->owner ? gw_dir->owner : "");
725 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
729 if (gw_trans->gw_conf->got_show_repo_age) {
730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
731 KATTR_ID, "index_project_age", KATTR__MAX);
734 kerr = khtml_puts(gw_trans->gw_html_req,
735 gw_dir->age ? gw_dir->age : "");
738 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
743 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
744 "navs_wrapper", KATTR__MAX);
747 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
752 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
753 href_summary, KATTR__MAX);
756 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
759 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
763 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
767 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
768 gw_dir->name, "action", "briefs", NULL);
769 if (href_briefs == NULL) {
770 error = got_error_from_errno("khttp_urlpart");
773 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
774 href_briefs, KATTR__MAX);
777 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
780 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
782 error = gw_kcgi_error(kerr);
784 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
788 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
789 gw_dir->name, "action", "commits", NULL);
790 if (href_commits == NULL) {
791 error = got_error_from_errno("khttp_urlpart");
794 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
795 href_commits, KATTR__MAX);
798 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
801 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
805 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
809 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
810 gw_dir->name, "action", "tags", NULL);
811 if (href_tags == NULL) {
812 error = got_error_from_errno("khttp_urlpart");
815 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
816 href_tags, KATTR__MAX);
819 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
822 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
826 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
830 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
831 gw_dir->name, "action", "tree", NULL);
832 if (href_tree == NULL) {
833 error = got_error_from_errno("khttp_urlpart");
836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
837 href_tree, KATTR__MAX);
840 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
844 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
847 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
848 "dotted_line", KATTR__MAX);
851 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
866 if (gw_trans->gw_conf->got_max_repos_display == 0)
869 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
870 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
871 (gw_trans->page > 0) &&
872 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
873 prev_disp == gw_trans->repos_total))) {
874 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
875 KATTR_ID, "np_wrapper", KATTR__MAX);
878 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
879 KATTR_ID, "nav_prev", KATTR__MAX);
884 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
885 (gw_trans->page > 0) &&
886 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
887 prev_disp == gw_trans->repos_total)) {
888 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
889 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
890 if (href_prev == NULL) {
891 error = got_error_from_errno("khttp_urlpartx");
894 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
895 KATTR_HREF, href_prev, KATTR__MAX);
898 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
901 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
906 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
908 return gw_kcgi_error(kerr);
910 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
911 next_disp == gw_trans->gw_conf->got_max_repos_display &&
912 dir_c != (gw_trans->page + 1) *
913 gw_trans->gw_conf->got_max_repos_display) {
914 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
915 KATTR_ID, "nav_next", KATTR__MAX);
918 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
919 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
920 if (href_next == NULL) {
921 error = got_error_from_errno("khttp_urlpartx");
924 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
925 KATTR_HREF, href_next, KATTR__MAX);
928 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
931 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
938 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
939 (gw_trans->page > 0) &&
940 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
941 prev_disp == gw_trans->repos_total)) {
942 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
956 if (error == NULL && kerr != KCGI_OK)
957 error = gw_kcgi_error(kerr);
961 static const struct got_error *
962 gw_commits(struct gw_trans *gw_trans)
964 const struct got_error *error = NULL;
965 struct gw_header *header = NULL, *n_header = NULL;
966 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
967 char *href_prev = NULL, *href_next = NULL;
968 enum kcgi_err kerr = KCGI_OK;
969 int commit_found = 0;
971 if ((header = gw_init_header()) == NULL)
972 return got_error_from_errno("malloc");
975 if (pledge("stdio rpath proc exec sendfd unveil",
977 error = got_error_from_errno("pledge");
981 error = gw_apply_unveil(gw_trans->gw_dir->path);
985 error = gw_get_header(gw_trans, header,
986 gw_trans->gw_conf->got_max_commits_display);
990 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
991 if (commit_found == 0 && gw_trans->commit_id != NULL) {
992 if (strcmp(gw_trans->commit_id,
993 n_header->commit_id) != 0)
998 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
999 "commits_line_wrapper", KATTR__MAX);
1000 if (kerr != KCGI_OK)
1002 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
1003 n_header->refs_str);
1006 error = gw_gen_author_header(gw_trans, n_header->author);
1009 error = gw_gen_committer_header(gw_trans, n_header->author);
1012 error = gw_get_time_str(&age, n_header->committer_time,
1016 error = gw_gen_age_header(gw_trans, age ?age : "");
1019 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1020 if (kerr != KCGI_OK)
1023 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1024 "dotted_line", KATTR__MAX);
1025 if (kerr != KCGI_OK)
1027 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1028 if (kerr != KCGI_OK)
1031 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1032 "commit", KATTR__MAX);
1033 if (kerr != KCGI_OK)
1035 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1036 if (kerr != KCGI_OK)
1038 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1039 if (kerr != KCGI_OK)
1042 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1043 gw_trans->repo_name, "action", "diff", "commit",
1044 n_header->commit_id, NULL);
1045 if (href_diff == NULL) {
1046 error = got_error_from_errno("khttp_urlpart");
1049 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1050 KATTR_ID, "navs_wrapper", KATTR__MAX);
1051 if (kerr != KCGI_OK)
1053 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1054 KATTR_ID, "navs", KATTR__MAX);
1055 if (kerr != KCGI_OK)
1057 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1058 KATTR_HREF, href_diff, KATTR__MAX);
1059 if (kerr != KCGI_OK)
1061 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1062 if (kerr != KCGI_OK)
1064 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1065 if (kerr != KCGI_OK)
1068 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1069 if (kerr != KCGI_OK)
1072 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1073 gw_trans->repo_name, "action", "tree", "commit",
1074 n_header->commit_id, NULL);
1075 if (href_tree == NULL) {
1076 error = got_error_from_errno("khttp_urlpart");
1079 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1080 KATTR_HREF, href_tree, KATTR__MAX);
1081 if (kerr != KCGI_OK)
1083 khtml_puts(gw_trans->gw_html_req, "tree");
1084 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1085 if (kerr != KCGI_OK)
1087 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1088 if (kerr != KCGI_OK)
1091 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1092 "solid_line", KATTR__MAX);
1093 if (kerr != KCGI_OK)
1095 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1096 if (kerr != KCGI_OK)
1103 if (gw_trans->next_id || gw_trans->prev_id) {
1104 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1105 KATTR_ID, "np_wrapper", KATTR__MAX);
1106 if (kerr != KCGI_OK)
1108 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1109 KATTR_ID, "nav_prev", KATTR__MAX);
1110 if (kerr != KCGI_OK)
1114 if (gw_trans->prev_id) {
1115 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1116 KATTRX_STRING, gw_trans->repo_name, "page",
1117 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1118 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1119 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1120 if (href_prev == NULL) {
1121 error = got_error_from_errno("khttp_urlpartx");
1124 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1125 KATTR_HREF, href_prev, KATTR__MAX);
1126 if (kerr != KCGI_OK)
1128 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1129 if (kerr != KCGI_OK)
1131 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1132 if (kerr != KCGI_OK)
1136 if (gw_trans->next_id || gw_trans->page > 0) {
1137 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1138 if (kerr != KCGI_OK)
1139 return gw_kcgi_error(kerr);
1142 if (gw_trans->next_id) {
1143 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1144 KATTR_ID, "nav_next", KATTR__MAX);
1145 if (kerr != KCGI_OK)
1147 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1148 KATTRX_STRING, gw_trans->repo_name, "page",
1149 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1150 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1151 gw_trans->next_id, NULL);
1152 if (href_next == NULL) {
1153 error = got_error_from_errno("khttp_urlpartx");
1156 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1157 KATTR_HREF, href_next, KATTR__MAX);
1158 if (kerr != KCGI_OK)
1160 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1161 if (kerr != KCGI_OK)
1163 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1164 if (kerr != KCGI_OK)
1168 if (gw_trans->next_id || gw_trans->page > 0) {
1169 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1170 if (kerr != KCGI_OK)
1174 gw_free_header(header);
1175 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1176 gw_free_header(n_header);
1182 if (error == NULL && kerr != KCGI_OK)
1183 error = gw_kcgi_error(kerr);
1187 static const struct got_error *
1188 gw_briefs(struct gw_trans *gw_trans)
1190 const struct got_error *error = NULL;
1191 struct gw_header *header = NULL, *n_header = NULL;
1192 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1193 char *href_prev = NULL, *href_next = NULL;
1194 char *newline, *smallerthan;
1195 enum kcgi_err kerr = KCGI_OK;
1196 int commit_found = 0;
1198 if ((header = gw_init_header()) == NULL)
1199 return got_error_from_errno("malloc");
1202 if (pledge("stdio rpath proc exec sendfd unveil",
1204 error = got_error_from_errno("pledge");
1208 if (gw_trans->action != GW_SUMMARY) {
1209 error = gw_apply_unveil(gw_trans->gw_dir->path);
1214 if (gw_trans->action == GW_SUMMARY)
1215 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1217 error = gw_get_header(gw_trans, header,
1218 gw_trans->gw_conf->got_max_commits_display);
1222 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1223 if (commit_found == 0 && gw_trans->commit_id != NULL) {
1224 if (strcmp(gw_trans->commit_id,
1225 n_header->commit_id) != 0)
1230 error = gw_get_time_str(&age, n_header->committer_time,
1235 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1236 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1237 if (kerr != KCGI_OK)
1240 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1241 KATTR_ID, "briefs_age", KATTR__MAX);
1242 if (kerr != KCGI_OK)
1244 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1245 if (kerr != KCGI_OK)
1247 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1248 if (kerr != KCGI_OK)
1251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1252 KATTR_ID, "briefs_author", KATTR__MAX);
1253 if (kerr != KCGI_OK)
1255 smallerthan = strchr(n_header->author, '<');
1257 *smallerthan = '\0';
1258 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1259 if (kerr != KCGI_OK)
1261 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1262 if (kerr != KCGI_OK)
1265 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1266 gw_trans->repo_name, "action", "diff", "commit",
1267 n_header->commit_id, NULL);
1268 if (href_diff == NULL) {
1269 error = got_error_from_errno("khttp_urlpart");
1272 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1273 KATTR_ID, "briefs_log", KATTR__MAX);
1274 if (kerr != KCGI_OK)
1276 newline = strchr(n_header->commit_msg, '\n');
1279 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1280 KATTR_HREF, href_diff, KATTR__MAX);
1281 if (kerr != KCGI_OK)
1283 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1284 if (kerr != KCGI_OK)
1286 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1287 if (kerr != KCGI_OK)
1290 if (n_header->refs_str) {
1291 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1292 if (kerr != KCGI_OK)
1294 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1295 KATTR_ID, "refs_str", KATTR__MAX);
1296 if (kerr != KCGI_OK)
1298 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1299 n_header->refs_str);
1300 if (kerr != KCGI_OK)
1302 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1303 if (kerr != KCGI_OK)
1307 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1308 if (kerr != KCGI_OK)
1311 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1312 KATTR_ID, "navs_wrapper", KATTR__MAX);
1313 if (kerr != KCGI_OK)
1315 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1316 KATTR_ID, "navs", KATTR__MAX);
1317 if (kerr != KCGI_OK)
1319 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1320 KATTR_HREF, href_diff, KATTR__MAX);
1321 if (kerr != KCGI_OK)
1323 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1324 if (kerr != KCGI_OK)
1326 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1327 if (kerr != KCGI_OK)
1330 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1331 if (kerr != KCGI_OK)
1334 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1335 gw_trans->repo_name, "action", "tree", "commit",
1336 n_header->commit_id, NULL);
1337 if (href_tree == NULL) {
1338 error = got_error_from_errno("khttp_urlpart");
1341 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1342 KATTR_HREF, href_tree, KATTR__MAX);
1343 if (kerr != KCGI_OK)
1345 khtml_puts(gw_trans->gw_html_req, "tree");
1346 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1347 if (kerr != KCGI_OK)
1349 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1350 if (kerr != KCGI_OK)
1353 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1354 KATTR_ID, "dotted_line", KATTR__MAX);
1355 if (kerr != KCGI_OK)
1357 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1358 if (kerr != KCGI_OK)
1369 if (gw_trans->next_id || gw_trans->prev_id) {
1370 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1371 KATTR_ID, "np_wrapper", KATTR__MAX);
1372 if (kerr != KCGI_OK)
1374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1375 KATTR_ID, "nav_prev", KATTR__MAX);
1376 if (kerr != KCGI_OK)
1380 if (gw_trans->prev_id) {
1381 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1382 KATTRX_STRING, gw_trans->repo_name, "page",
1383 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1384 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1385 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1386 if (href_prev == NULL) {
1387 error = got_error_from_errno("khttp_urlpartx");
1390 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1391 KATTR_HREF, href_prev, KATTR__MAX);
1392 if (kerr != KCGI_OK)
1394 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1395 if (kerr != KCGI_OK)
1397 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1398 if (kerr != KCGI_OK)
1402 if (gw_trans->next_id || gw_trans->page > 0) {
1403 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1404 if (kerr != KCGI_OK)
1405 return gw_kcgi_error(kerr);
1408 if (gw_trans->next_id) {
1409 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1410 KATTR_ID, "nav_next", KATTR__MAX);
1411 if (kerr != KCGI_OK)
1414 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1415 KATTRX_STRING, gw_trans->repo_name, "page",
1416 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1417 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1418 gw_trans->next_id, NULL);
1419 if (href_next == NULL) {
1420 error = got_error_from_errno("khttp_urlpartx");
1423 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1424 KATTR_HREF, href_next, KATTR__MAX);
1425 if (kerr != KCGI_OK)
1427 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1428 if (kerr != KCGI_OK)
1430 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1431 if (kerr != KCGI_OK)
1435 if (gw_trans->next_id || gw_trans->page > 0) {
1436 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1437 if (kerr != KCGI_OK)
1441 gw_free_header(header);
1442 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1443 gw_free_header(n_header);
1449 if (error == NULL && kerr != KCGI_OK)
1450 error = gw_kcgi_error(kerr);
1454 static const struct got_error *
1455 gw_summary(struct gw_trans *gw_trans)
1457 const struct got_error *error = NULL;
1459 enum kcgi_err kerr = KCGI_OK;
1462 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1463 return got_error_from_errno("pledge");
1465 error = gw_apply_unveil(gw_trans->gw_dir->path);
1469 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1470 "summary_wrapper", KATTR__MAX);
1471 if (kerr != KCGI_OK)
1472 return gw_kcgi_error(kerr);
1474 if (gw_trans->gw_conf->got_show_repo_description &&
1475 gw_trans->gw_dir->description != NULL &&
1476 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1477 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1478 KATTR_ID, "description_title", KATTR__MAX);
1479 if (kerr != KCGI_OK)
1481 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1482 if (kerr != KCGI_OK)
1484 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1485 if (kerr != KCGI_OK)
1487 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1488 KATTR_ID, "description", KATTR__MAX);
1489 if (kerr != KCGI_OK)
1491 kerr = khtml_puts(gw_trans->gw_html_req,
1492 gw_trans->gw_dir->description);
1493 if (kerr != KCGI_OK)
1495 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1496 if (kerr != KCGI_OK)
1500 if (gw_trans->gw_conf->got_show_repo_owner &&
1501 gw_trans->gw_dir->owner != NULL &&
1502 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1503 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1504 KATTR_ID, "repo_owner_title", KATTR__MAX);
1505 if (kerr != KCGI_OK)
1507 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1508 if (kerr != KCGI_OK)
1510 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1511 if (kerr != KCGI_OK)
1513 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1514 KATTR_ID, "repo_owner", KATTR__MAX);
1515 if (kerr != KCGI_OK)
1517 kerr = khtml_puts(gw_trans->gw_html_req,
1518 gw_trans->gw_dir->owner);
1519 if (kerr != KCGI_OK)
1521 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1522 if (kerr != KCGI_OK)
1526 if (gw_trans->gw_conf->got_show_repo_age) {
1527 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1532 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1533 KATTR_ID, "last_change_title", KATTR__MAX);
1534 if (kerr != KCGI_OK)
1536 kerr = khtml_puts(gw_trans->gw_html_req,
1538 if (kerr != KCGI_OK)
1540 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1541 if (kerr != KCGI_OK)
1543 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1544 KATTR_ID, "last_change", KATTR__MAX);
1545 if (kerr != KCGI_OK)
1547 kerr = khtml_puts(gw_trans->gw_html_req, age);
1548 if (kerr != KCGI_OK)
1550 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1551 if (kerr != KCGI_OK)
1556 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1557 gw_trans->gw_dir->url != NULL &&
1558 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1559 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1560 KATTR_ID, "cloneurl_title", KATTR__MAX);
1561 if (kerr != KCGI_OK)
1563 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1564 if (kerr != KCGI_OK)
1566 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1567 if (kerr != KCGI_OK)
1569 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1570 KATTR_ID, "cloneurl", KATTR__MAX);
1571 if (kerr != KCGI_OK)
1573 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1574 if (kerr != KCGI_OK)
1576 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1577 if (kerr != KCGI_OK)
1581 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1582 if (kerr != KCGI_OK)
1585 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1586 "briefs_title_wrapper", KATTR__MAX);
1587 if (kerr != KCGI_OK)
1589 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1590 "briefs_title", KATTR__MAX);
1591 if (kerr != KCGI_OK)
1593 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1594 if (kerr != KCGI_OK)
1596 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1597 if (kerr != KCGI_OK)
1599 error = gw_briefs(gw_trans);
1603 error = gw_tags(gw_trans);
1607 error = gw_output_repo_heads(gw_trans);
1610 if (error == NULL && kerr != KCGI_OK)
1611 error = gw_kcgi_error(kerr);
1615 static const struct got_error *
1616 gw_tree(struct gw_trans *gw_trans)
1618 const struct got_error *error = NULL;
1619 struct gw_header *header = NULL;
1620 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1622 enum kcgi_err kerr = KCGI_OK;
1625 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1626 return got_error_from_errno("pledge");
1628 if ((header = gw_init_header()) == NULL)
1629 return got_error_from_errno("malloc");
1631 error = gw_apply_unveil(gw_trans->gw_dir->path);
1635 error = gw_get_header(gw_trans, header, 1);
1639 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1640 "tree_header_wrapper", KATTR__MAX);
1641 if (kerr != KCGI_OK)
1643 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1644 "tree_header", KATTR__MAX);
1645 if (kerr != KCGI_OK)
1647 error = gw_gen_tree_header(gw_trans, header->tree_id);
1650 error = gw_get_time_str(&age, header->committer_time,
1654 error = gw_gen_age_header(gw_trans, age ?age : "");
1657 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1660 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1661 if (kerr != KCGI_OK)
1663 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1664 "dotted_line", KATTR__MAX);
1665 if (kerr != KCGI_OK)
1667 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1668 if (kerr != KCGI_OK)
1671 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1672 "tree", KATTR__MAX);
1673 if (kerr != KCGI_OK)
1675 error = gw_output_repo_tree(gw_trans, header);
1679 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1681 gw_free_header(header);
1682 free(tree_html_disp);
1686 if (error == NULL && kerr != KCGI_OK)
1687 error = gw_kcgi_error(kerr);
1691 static const struct got_error *
1692 gw_tags(struct gw_trans *gw_trans)
1694 const struct got_error *error = NULL;
1695 struct gw_header *header = NULL;
1696 char *href_next = NULL, *href_prev = NULL;
1697 enum kcgi_err kerr = KCGI_OK;
1700 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1701 return got_error_from_errno("pledge");
1703 if ((header = gw_init_header()) == NULL)
1704 return got_error_from_errno("malloc");
1706 if (gw_trans->action != GW_SUMMARY) {
1707 error = gw_apply_unveil(gw_trans->gw_dir->path);
1712 error = gw_get_header(gw_trans, header, 1);
1716 if (gw_trans->action == GW_SUMMARY) {
1717 gw_trans->next_id = NULL;
1718 error = gw_output_repo_tags(gw_trans, header,
1719 D_MAXSLCOMMDISP, TAGBRIEF);
1723 error = gw_output_repo_tags(gw_trans, header,
1724 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1729 if (gw_trans->next_id || gw_trans->page > 0) {
1730 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1731 KATTR_ID, "np_wrapper", KATTR__MAX);
1732 if (kerr != KCGI_OK)
1734 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1735 KATTR_ID, "nav_prev", KATTR__MAX);
1736 if (kerr != KCGI_OK)
1740 if (gw_trans->prev_id) {
1741 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1742 KATTRX_STRING, gw_trans->repo_name, "page",
1743 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1744 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1745 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1746 if (href_prev == NULL) {
1747 error = got_error_from_errno("khttp_urlpartx");
1750 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1751 KATTR_HREF, href_prev, KATTR__MAX);
1752 if (kerr != KCGI_OK)
1754 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1755 if (kerr != KCGI_OK)
1757 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1758 if (kerr != KCGI_OK)
1762 if (gw_trans->next_id || gw_trans->page > 0) {
1763 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1764 if (kerr != KCGI_OK)
1765 return gw_kcgi_error(kerr);
1768 if (gw_trans->next_id) {
1769 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1770 KATTR_ID, "nav_next", KATTR__MAX);
1771 if (kerr != KCGI_OK)
1773 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1774 KATTRX_STRING, gw_trans->repo_name, "page",
1775 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1776 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1777 gw_trans->next_id, NULL);
1778 if (href_next == NULL) {
1779 error = got_error_from_errno("khttp_urlpartx");
1782 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1783 KATTR_HREF, href_next, KATTR__MAX);
1784 if (kerr != KCGI_OK)
1786 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1787 if (kerr != KCGI_OK)
1789 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1790 if (kerr != KCGI_OK)
1794 if (gw_trans->next_id || gw_trans->page > 0) {
1795 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1796 if (kerr != KCGI_OK)
1800 gw_free_header(header);
1803 if (error == NULL && kerr != KCGI_OK)
1804 error = gw_kcgi_error(kerr);
1808 static const struct got_error *
1809 gw_tag(struct gw_trans *gw_trans)
1811 const struct got_error *error = NULL;
1812 struct gw_header *header = NULL;
1813 enum kcgi_err kerr = KCGI_OK;
1816 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1817 return got_error_from_errno("pledge");
1819 if ((header = gw_init_header()) == NULL)
1820 return got_error_from_errno("malloc");
1822 error = gw_apply_unveil(gw_trans->gw_dir->path);
1826 if (gw_trans->commit_id == NULL) {
1827 error = got_error_msg(GOT_ERR_QUERYSTRING,
1828 "commit required in querystring");
1832 error = gw_get_header(gw_trans, header, 1);
1836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1837 "tag_header_wrapper", KATTR__MAX);
1838 if (kerr != KCGI_OK)
1840 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1841 "tag_header", KATTR__MAX);
1842 if (kerr != KCGI_OK)
1844 error = gw_gen_commit_header(gw_trans, header->commit_id,
1848 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1851 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1852 if (kerr != KCGI_OK)
1854 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1855 "dotted_line", KATTR__MAX);
1856 if (kerr != KCGI_OK)
1858 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1859 if (kerr != KCGI_OK)
1862 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1863 "tree", KATTR__MAX);
1864 if (kerr != KCGI_OK)
1867 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1871 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1873 gw_free_header(header);
1874 if (error == NULL && kerr != KCGI_OK)
1875 error = gw_kcgi_error(kerr);
1879 static const struct got_error *
1880 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1882 const struct got_error *error = NULL;
1887 if (asprintf(&dir_test, "%s/%s/%s",
1888 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1889 GOTWEB_GIT_DIR) == -1)
1890 return got_error_from_errno("asprintf");
1892 dt = opendir(dir_test);
1896 gw_dir->path = strdup(dir_test);
1897 if (gw_dir->path == NULL) {
1899 error = got_error_from_errno("strdup");
1906 if (asprintf(&dir_test, "%s/%s/%s",
1907 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1908 GOTWEB_GOT_DIR) == -1) {
1910 error = got_error_from_errno("asprintf");
1914 dt = opendir(dir_test);
1919 error = got_error(GOT_ERR_NOT_GIT_REPO);
1923 if (asprintf(&dir_test, "%s/%s",
1924 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1925 error = got_error_from_errno("asprintf");
1930 gw_dir->path = strdup(dir_test);
1931 if (gw_dir->path == NULL) {
1933 error = got_error_from_errno("strdup");
1937 dt = opendir(dir_test);
1939 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1944 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1948 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1951 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1955 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1959 if (dt && closedir(dt) == -1 && error == NULL)
1960 error = got_error_from_errno("closedir");
1964 static const struct got_error *
1965 gw_load_got_paths(struct gw_trans *gw_trans)
1967 const struct got_error *error = NULL;
1969 struct dirent **sd_dent;
1970 struct gw_dir *gw_dir;
1972 unsigned int d_cnt, d_i;
1974 d = opendir(gw_trans->gw_conf->got_repos_path);
1976 error = got_error_from_errno2("opendir",
1977 gw_trans->gw_conf->got_repos_path);
1981 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1984 error = got_error_from_errno2("scandir",
1985 gw_trans->gw_conf->got_repos_path);
1989 for (d_i = 0; d_i < d_cnt; d_i++) {
1990 if (gw_trans->gw_conf->got_max_repos > 0 &&
1991 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1992 break; /* account for parent and self */
1994 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1995 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1998 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
2002 error = gw_load_got_path(gw_trans, gw_dir);
2003 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
2006 } else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
2009 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
2010 !got_path_dir_is_empty(gw_dir->path)) {
2011 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
2013 gw_trans->repos_total++;
2017 if (d && closedir(d) == -1 && error == NULL)
2018 error = got_error_from_errno("closedir");
2022 static const struct got_error *
2023 gw_parse_querystring(struct gw_trans *gw_trans)
2025 const struct got_error *error = NULL;
2027 const struct gw_query_action *action = NULL;
2030 if (gw_trans->gw_req->fieldnmap[0]) {
2031 return got_error(GOT_ERR_QUERYSTRING);
2032 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2033 /* define gw_trans->repo_path */
2034 gw_trans->repo_name = p->parsed.s;
2036 if (asprintf(&gw_trans->repo_path, "%s/%s",
2037 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2038 return got_error_from_errno("asprintf");
2040 /* get action and set function */
2041 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2042 for (i = 0; i < nitems(gw_query_funcs); i++) {
2043 action = &gw_query_funcs[i];
2044 if (action->func_name == NULL)
2046 if (strcmp(action->func_name,
2047 p->parsed.s) == 0) {
2048 gw_trans->action = i;
2053 if (gw_trans->action == -1) {
2054 gw_trans->action = GW_ERR;
2055 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2056 p != NULL ? "bad action in querystring" :
2057 "no action in querystring");
2061 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2062 if (asprintf(&gw_trans->commit_id, "%s",
2064 return got_error_from_errno("asprintf");
2067 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2068 gw_trans->repo_file = p->parsed.s;
2070 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2071 if (asprintf(&gw_trans->repo_folder, "%s",
2073 return got_error_from_errno("asprintf");
2076 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2077 if (asprintf(&gw_trans->prev_id, "%s",
2079 return got_error_from_errno("asprintf");
2082 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2083 gw_trans->headref = p->parsed.s;
2085 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2089 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2091 gw_trans->action = GW_INDEX;
2093 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2094 gw_trans->page = p->parsed.i;
2099 static const struct got_error *
2100 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2102 const struct got_error *error;
2104 *gw_dir = malloc(sizeof(**gw_dir));
2105 if (*gw_dir == NULL)
2106 return got_error_from_errno("malloc");
2108 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2109 error = got_error_from_errno("asprintf");
2118 static const struct got_error *
2119 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2121 enum kcgi_err kerr = KCGI_OK;
2123 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2124 if (kerr != KCGI_OK)
2125 return gw_kcgi_error(kerr);
2126 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2128 if (kerr != KCGI_OK)
2129 return gw_kcgi_error(kerr);
2130 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2132 if (kerr != KCGI_OK)
2133 return gw_kcgi_error(kerr);
2134 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2136 if (kerr != KCGI_OK)
2137 return gw_kcgi_error(kerr);
2138 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2139 if (kerr != KCGI_OK)
2140 return gw_kcgi_error(kerr);
2141 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2143 if (kerr != KCGI_OK)
2144 return gw_kcgi_error(kerr);
2146 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2147 kerr = khttp_head(gw_trans->gw_req,
2148 kresps[KRESP_CONTENT_DISPOSITION],
2149 "attachment; filename=%s", gw_trans->repo_file);
2150 if (kerr != KCGI_OK)
2151 return gw_kcgi_error(kerr);
2154 kerr = khttp_body(gw_trans->gw_req);
2155 return gw_kcgi_error(kerr);
2158 static const struct got_error *
2159 gw_display_index(struct gw_trans *gw_trans)
2161 const struct got_error *error;
2162 enum kcgi_err kerr = KCGI_OK;
2164 /* catch early querystring errors */
2165 if (gw_trans->error)
2166 gw_trans->action = GW_ERR;
2168 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2172 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2173 if (kerr != KCGI_OK)
2174 return gw_kcgi_error(kerr);
2176 if (gw_trans->action != GW_BLOB) {
2177 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2178 gw_query_funcs[gw_trans->action].template);
2179 if (kerr != KCGI_OK) {
2180 khtml_close(gw_trans->gw_html_req);
2181 return gw_kcgi_error(kerr);
2185 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2188 static const struct got_error *
2189 gw_error(struct gw_trans *gw_trans)
2191 enum kcgi_err kerr = KCGI_OK;
2193 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2195 return gw_kcgi_error(kerr);
2199 gw_template(size_t key, void *arg)
2201 const struct got_error *error = NULL;
2202 enum kcgi_err kerr = KCGI_OK;
2203 struct gw_trans *gw_trans = arg;
2204 char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
2205 char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
2207 if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
2208 "/apple-touch-icon.png") == -1)
2210 if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
2211 "/favicon-32x32.png") == -1)
2213 if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
2214 "/favicon-16x16.png") == -1)
2216 if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
2217 "/site.webmanifest") == -1)
2219 if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
2220 "/safari-pinned-tab.svg") == -1)
2222 if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
2223 "/gotweb.css") == -1)
2225 if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
2226 gw_trans->gw_conf->got_www_path ? "/" : "",
2227 gw_trans->gw_conf->got_logo) == -1)
2232 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2233 KATTR_NAME, "viewport",
2234 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2236 if (kerr != KCGI_OK)
2238 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2239 if (kerr != KCGI_OK)
2241 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2242 KATTR_CHARSET, "utf-8",
2244 if (kerr != KCGI_OK)
2246 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2247 if (kerr != KCGI_OK)
2249 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2250 KATTR_NAME, "msapplication-TileColor",
2251 KATTR_CONTENT, "#da532c", KATTR__MAX);
2252 if (kerr != KCGI_OK)
2254 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2255 if (kerr != KCGI_OK)
2257 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2258 KATTR_NAME, "theme-color",
2259 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2260 if (kerr != KCGI_OK)
2262 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2263 if (kerr != KCGI_OK)
2265 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2266 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2267 KATTR_HREF, ati, KATTR__MAX);
2268 if (kerr != KCGI_OK)
2270 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2271 if (kerr != KCGI_OK)
2273 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2274 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2275 "32x32", KATTR_HREF, fic32, KATTR__MAX);
2276 if (kerr != KCGI_OK)
2278 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2279 if (kerr != KCGI_OK)
2281 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2282 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2283 "16x16", KATTR_HREF, fic16, KATTR__MAX);
2284 if (kerr != KCGI_OK)
2286 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2287 if (kerr != KCGI_OK)
2289 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2290 KATTR_REL, "manifest", KATTR_HREF, swm,
2292 if (kerr != KCGI_OK)
2294 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2295 if (kerr != KCGI_OK)
2297 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2298 KATTR_REL, "mask-icon", KATTR_HREF,
2300 if (kerr != KCGI_OK)
2302 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2303 if (kerr != KCGI_OK)
2305 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2306 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2307 KATTR_HREF, css, KATTR__MAX);
2308 if (kerr != KCGI_OK)
2310 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2311 if (kerr != KCGI_OK)
2315 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2316 KATTR_ID, "got_link", KATTR__MAX);
2317 if (kerr != KCGI_OK)
2319 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2320 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2321 KATTR_TARGET, "_sotd", KATTR__MAX);
2322 if (kerr != KCGI_OK)
2324 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2325 KATTR_SRC, logo, KATTR__MAX);
2326 if (kerr != KCGI_OK)
2328 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2329 if (kerr != KCGI_OK)
2332 case (TEMPL_SITEPATH):
2333 error = gw_output_site_link(gw_trans);
2338 if (gw_trans->gw_conf->got_site_name != NULL) {
2339 kerr = khtml_puts(gw_trans->gw_html_req,
2340 gw_trans->gw_conf->got_site_name);
2341 if (kerr != KCGI_OK)
2345 case (TEMPL_SEARCH):
2347 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2348 "search", KATTR__MAX);
2349 if (kerr != KCGI_OK)
2351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2352 KATTR_METHOD, "POST", KATTR__MAX);
2353 if (kerr != KCGI_OK)
2355 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2356 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2357 KATTR_MAXLENGTH, "50", KATTR__MAX);
2358 if (kerr != KCGI_OK)
2360 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2362 if (kerr != KCGI_OK)
2364 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2365 if (kerr != KCGI_OK)
2367 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2368 if (kerr != KCGI_OK)
2371 case(TEMPL_SITEOWNER):
2372 if (gw_trans->gw_conf->got_site_owner != NULL &&
2373 gw_trans->gw_conf->got_show_site_owner) {
2374 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2375 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2376 if (kerr != KCGI_OK)
2378 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2379 KATTR_ID, "site_owner", KATTR__MAX);
2380 if (kerr != KCGI_OK)
2382 kerr = khtml_puts(gw_trans->gw_html_req,
2383 gw_trans->gw_conf->got_site_owner);
2384 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2385 if (kerr != KCGI_OK)
2389 case(TEMPL_CONTENT):
2390 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2392 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2393 KATTR_ID, "tmpl_err", KATTR__MAX);
2394 if (kerr != KCGI_OK)
2396 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2398 if (kerr != KCGI_OK)
2400 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2401 if (kerr != KCGI_OK)
2427 static const struct got_error *
2428 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2430 const struct got_error *error = NULL;
2431 enum kcgi_err kerr = KCGI_OK;
2433 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2434 KATTR_ID, "header_commit_title", KATTR__MAX);
2435 if (kerr != KCGI_OK)
2437 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2438 if (kerr != KCGI_OK)
2440 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2441 if (kerr != KCGI_OK)
2443 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2444 KATTR_ID, "header_commit", KATTR__MAX);
2445 if (kerr != KCGI_OK)
2447 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2448 if (kerr != KCGI_OK)
2451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2452 KATTR_ID, "refs_str", KATTR__MAX);
2453 if (kerr != KCGI_OK)
2455 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2456 if (kerr != KCGI_OK)
2458 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2459 if (kerr != KCGI_OK)
2462 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2464 if (error == NULL && kerr != KCGI_OK)
2465 error = gw_kcgi_error(kerr);
2469 static const struct got_error *
2470 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2472 const struct got_error *error = NULL;
2473 enum kcgi_err kerr = KCGI_OK;
2475 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2476 KATTR_ID, "header_diff_title", KATTR__MAX);
2477 if (kerr != KCGI_OK)
2479 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2480 if (kerr != KCGI_OK)
2482 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2483 if (kerr != KCGI_OK)
2485 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2486 KATTR_ID, "header_diff", KATTR__MAX);
2487 if (kerr != KCGI_OK)
2490 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2491 if (kerr != KCGI_OK)
2494 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2495 if (kerr != KCGI_OK)
2497 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2498 if (kerr != KCGI_OK)
2500 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2502 if (error == NULL && kerr != KCGI_OK)
2503 error = gw_kcgi_error(kerr);
2507 static const struct got_error *
2508 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2510 const struct got_error *error = NULL;
2511 enum kcgi_err kerr = KCGI_OK;
2513 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2514 KATTR_ID, "header_age_title", KATTR__MAX);
2515 if (kerr != KCGI_OK)
2517 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2518 if (kerr != KCGI_OK)
2520 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2521 if (kerr != KCGI_OK)
2523 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2524 KATTR_ID, "header_age", KATTR__MAX);
2525 if (kerr != KCGI_OK)
2527 kerr = khtml_puts(gw_trans->gw_html_req, str);
2528 if (kerr != KCGI_OK)
2530 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2532 if (error == NULL && kerr != KCGI_OK)
2533 error = gw_kcgi_error(kerr);
2537 static const struct got_error *
2538 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2540 const struct got_error *error = NULL;
2541 enum kcgi_err kerr = KCGI_OK;
2543 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2544 KATTR_ID, "header_author_title", KATTR__MAX);
2545 if (kerr != KCGI_OK)
2547 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2548 if (kerr != KCGI_OK)
2550 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2551 if (kerr != KCGI_OK)
2553 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2554 KATTR_ID, "header_author", KATTR__MAX);
2555 if (kerr != KCGI_OK)
2557 kerr = khtml_puts(gw_trans->gw_html_req, str);
2558 if (kerr != KCGI_OK)
2560 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2562 if (error == NULL && kerr != KCGI_OK)
2563 error = gw_kcgi_error(kerr);
2567 static const struct got_error *
2568 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2570 const struct got_error *error = NULL;
2571 enum kcgi_err kerr = KCGI_OK;
2573 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2574 KATTR_ID, "header_committer_title", KATTR__MAX);
2575 if (kerr != KCGI_OK)
2577 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2578 if (kerr != KCGI_OK)
2580 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2581 if (kerr != KCGI_OK)
2583 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2584 KATTR_ID, "header_committer", KATTR__MAX);
2585 if (kerr != KCGI_OK)
2587 kerr = khtml_puts(gw_trans->gw_html_req, str);
2588 if (kerr != KCGI_OK)
2590 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2592 if (error == NULL && kerr != KCGI_OK)
2593 error = gw_kcgi_error(kerr);
2597 static const struct got_error *
2598 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2600 const struct got_error *error = NULL;
2601 enum kcgi_err kerr = KCGI_OK;
2603 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2604 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2605 if (kerr != KCGI_OK)
2607 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2608 if (kerr != KCGI_OK)
2610 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2611 if (kerr != KCGI_OK)
2613 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2614 KATTR_ID, "header_commit_msg", KATTR__MAX);
2615 if (kerr != KCGI_OK)
2617 kerr = khttp_puts(gw_trans->gw_req, str);
2618 if (kerr != KCGI_OK)
2620 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2622 if (error == NULL && kerr != KCGI_OK)
2623 error = gw_kcgi_error(kerr);
2627 static const struct got_error *
2628 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2630 const struct got_error *error = NULL;
2631 enum kcgi_err kerr = KCGI_OK;
2633 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2634 KATTR_ID, "header_tree_title", KATTR__MAX);
2635 if (kerr != KCGI_OK)
2637 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2638 if (kerr != KCGI_OK)
2640 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2641 if (kerr != KCGI_OK)
2643 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2644 KATTR_ID, "header_tree", KATTR__MAX);
2645 if (kerr != KCGI_OK)
2647 kerr = khtml_puts(gw_trans->gw_html_req, str);
2648 if (kerr != KCGI_OK)
2650 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2652 if (error == NULL && kerr != KCGI_OK)
2653 error = gw_kcgi_error(kerr);
2657 static const struct got_error *
2658 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2661 const struct got_error *error = NULL;
2663 char *d_file = NULL;
2667 *description = NULL;
2668 if (gw_trans->gw_conf->got_show_repo_description == 0)
2671 if (asprintf(&d_file, "%s/description", dir) == -1)
2672 return got_error_from_errno("asprintf");
2674 f = fopen(d_file, "re");
2676 if (errno == ENOENT || errno == EACCES)
2678 error = got_error_from_errno2("fopen", d_file);
2682 if (fseek(f, 0, SEEK_END) == -1) {
2683 error = got_ferror(f, GOT_ERR_IO);
2688 error = got_ferror(f, GOT_ERR_IO);
2691 if (fseek(f, 0, SEEK_SET) == -1) {
2692 error = got_ferror(f, GOT_ERR_IO);
2695 *description = calloc(len + 1, sizeof(**description));
2696 if (*description == NULL) {
2697 error = got_error_from_errno("calloc");
2701 n = fread(*description, 1, len, f);
2702 if (n == 0 && ferror(f))
2703 error = got_ferror(f, GOT_ERR_IO);
2705 if (f != NULL && fclose(f) == EOF && error == NULL)
2706 error = got_error_from_errno("fclose");
2711 static const struct got_error *
2712 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2716 char *years = "years ago", *months = "months ago";
2717 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2718 char *minutes = "minutes ago", *seconds = "seconds ago";
2719 char *now = "right now";
2727 diff_time = time(NULL) - committer_time;
2728 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2729 if (asprintf(repo_age, "%lld %s",
2730 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2731 return got_error_from_errno("asprintf");
2732 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2733 if (asprintf(repo_age, "%lld %s",
2734 (diff_time / 60 / 60 / 24 / (365 / 12)),
2736 return got_error_from_errno("asprintf");
2737 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2738 if (asprintf(repo_age, "%lld %s",
2739 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2740 return got_error_from_errno("asprintf");
2741 } else if (diff_time > 60 * 60 * 24 * 2) {
2742 if (asprintf(repo_age, "%lld %s",
2743 (diff_time / 60 / 60 / 24), days) == -1)
2744 return got_error_from_errno("asprintf");
2745 } else if (diff_time > 60 * 60 * 2) {
2746 if (asprintf(repo_age, "%lld %s",
2747 (diff_time / 60 / 60), hours) == -1)
2748 return got_error_from_errno("asprintf");
2749 } else if (diff_time > 60 * 2) {
2750 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2752 return got_error_from_errno("asprintf");
2753 } else if (diff_time > 2) {
2754 if (asprintf(repo_age, "%lld %s", diff_time,
2756 return got_error_from_errno("asprintf");
2758 if (asprintf(repo_age, "%s", now) == -1)
2759 return got_error_from_errno("asprintf");
2763 if (gmtime_r(&committer_time, &tm) == NULL)
2764 return got_error_from_errno("gmtime_r");
2766 s = asctime_r(&tm, datebuf);
2768 return got_error_from_errno("asctime_r");
2770 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2771 return got_error_from_errno("asprintf");
2777 static const struct got_error *
2778 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2779 const char *refname, int ref_tm)
2781 const struct got_error *error = NULL;
2782 struct got_repository *repo = NULL;
2783 struct got_commit_object *commit = NULL;
2784 struct got_reflist_head refs;
2785 struct got_reflist_entry *re;
2786 time_t committer_time = 0, cmp_time = 0;
2791 if (gw_trans->gw_conf->got_show_repo_age == 0)
2795 repo = gw_trans->repo;
2797 error = got_repo_open(&repo, dir, NULL);
2802 error = got_ref_list(&refs, repo, "refs/heads",
2803 got_ref_cmp_by_name, NULL);
2808 * Find the youngest branch tip in the repository, or the age of
2809 * the a specific branch tip if a name was provided by the caller.
2811 TAILQ_FOREACH(re, &refs, entry) {
2812 struct got_object_id *id = NULL;
2814 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2817 error = got_ref_resolve(&id, repo, re->ref);
2821 error = got_object_open_as_commit(&commit, repo, id);
2827 got_object_commit_get_committer_time(commit);
2828 got_object_commit_close(commit);
2829 if (cmp_time < committer_time)
2830 cmp_time = committer_time;
2836 if (cmp_time != 0) {
2837 committer_time = cmp_time;
2838 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2841 got_ref_list_free(&refs);
2842 if (gw_trans->repo == NULL) {
2843 const struct got_error *close_err = got_repo_close(repo);
2850 static const struct got_error *
2851 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2853 const struct got_error *error;
2855 struct got_object_id *id1 = NULL, *id2 = NULL;
2856 char *label1 = NULL, *label2 = NULL, *line = NULL;
2858 size_t linesize = 0;
2860 enum kcgi_err kerr = KCGI_OK;
2866 if (header->parent_id != NULL &&
2867 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2868 error = got_repo_match_object_id(&id1, &label1,
2869 header->parent_id, GOT_OBJ_TYPE_ANY,
2870 &header->refs, gw_trans->repo);
2875 error = got_repo_match_object_id(&id2, &label2,
2876 header->commit_id, GOT_OBJ_TYPE_ANY, &header->refs,
2881 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2885 case GOT_OBJ_TYPE_BLOB:
2886 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
2887 NULL, NULL, 3, 0, 0, gw_trans->repo, f);
2889 case GOT_OBJ_TYPE_TREE:
2890 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
2891 NULL, "", "", 3, 0, 0, gw_trans->repo, f);
2893 case GOT_OBJ_TYPE_COMMIT:
2894 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
2895 NULL, 3, 0, 0, gw_trans->repo, f);
2898 error = got_error(GOT_ERR_OBJ_TYPE);
2903 if (fseek(f, 0, SEEK_SET) == -1) {
2904 error = got_ferror(f, GOT_ERR_IO);
2908 while ((linelen = getline(&line, &linesize, f)) != -1) {
2909 error = gw_colordiff_line(gw_trans, line);
2912 /* XXX: KHTML_PRETTY breaks this */
2913 kerr = khtml_puts(gw_trans->gw_html_req, line);
2914 if (kerr != KCGI_OK)
2916 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2917 if (kerr != KCGI_OK)
2920 if (linelen == -1 && ferror(f))
2921 error = got_error_from_errno("getline");
2923 if (f && fclose(f) == EOF && error == NULL)
2924 error = got_error_from_errno("fclose");
2931 if (error == NULL && kerr != KCGI_OK)
2932 error = gw_kcgi_error(kerr);
2936 static const struct got_error *
2937 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2939 const struct got_error *error = NULL, *close_err;
2940 struct got_repository *repo;
2941 const char *gitconfig_owner;
2945 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2948 error = got_repo_open(&repo, dir, NULL);
2951 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2952 if (gitconfig_owner) {
2953 *owner = strdup(gitconfig_owner);
2955 error = got_error_from_errno("strdup");
2957 close_err = got_repo_close(repo);
2963 static const struct got_error *
2964 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2966 const struct got_error *error = NULL;
2968 char *d_file = NULL;
2974 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2975 return got_error_from_errno("asprintf");
2977 f = fopen(d_file, "re");
2979 if (errno != ENOENT && errno != EACCES)
2980 error = got_error_from_errno2("fopen", d_file);
2984 if (fseek(f, 0, SEEK_END) == -1) {
2985 error = got_ferror(f, GOT_ERR_IO);
2990 error = got_ferror(f, GOT_ERR_IO);
2993 if (fseek(f, 0, SEEK_SET) == -1) {
2994 error = got_ferror(f, GOT_ERR_IO);
2998 *url = calloc(len + 1, sizeof(**url));
3000 error = got_error_from_errno("calloc");
3004 n = fread(*url, 1, len, f);
3005 if (n == 0 && ferror(f))
3006 error = got_ferror(f, GOT_ERR_IO);
3008 if (f && fclose(f) == EOF && error == NULL)
3009 error = got_error_from_errno("fclose");
3014 static const struct got_error *
3015 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3016 int limit, int tag_type)
3018 const struct got_error *error = NULL;
3019 struct got_reflist_head refs;
3020 struct got_reflist_entry *re;
3022 char *id_str = NULL, *newline, *href_commits = NULL;
3023 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3024 struct got_tag_object *tag = NULL;
3025 enum kcgi_err kerr = KCGI_OK;
3026 int summary_header_displayed = 0, chk_next = 0;
3027 int tag_count = 0, commit_found = 0, c_cnt = 0;
3031 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3032 got_ref_cmp_tags, gw_trans->repo);
3036 TAILQ_FOREACH(re, &refs, entry) {
3037 const char *refname;
3039 const char *tag_commit;
3041 struct got_object_id *id;
3042 struct got_commit_object *commit = NULL;
3044 refname = got_ref_get_name(re->ref);
3045 if (strncmp(refname, "refs/tags/", 10) != 0)
3049 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3053 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3055 if (error->code != GOT_ERR_OBJ_TYPE) {
3059 /* "lightweight" tag */
3060 error = got_object_open_as_commit(&commit,
3061 gw_trans->repo, id);
3066 tagger = got_object_commit_get_committer(commit);
3068 got_object_commit_get_committer_time(commit);
3069 error = got_object_id_str(&id_str, id);
3073 tagger = got_object_tag_get_tagger(tag);
3074 tagger_time = got_object_tag_get_tagger_time(tag);
3075 error = got_object_id_str(&id_str,
3076 got_object_tag_get_object_id(tag));
3081 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3082 strlen(id_str)) != 0)
3085 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3086 commit_found == 0 && strncmp(id_str, gw_trans->commit_id,
3087 strlen(id_str)) != 0)
3095 gw_trans->next_id = strdup(id_str);
3096 if (gw_trans->next_id == NULL)
3097 error = got_error_from_errno("strdup");
3102 error = got_object_commit_get_logmsg(&tag_commit0,
3106 got_object_commit_close(commit);
3108 tag_commit0 = strdup(got_object_tag_get_message(tag));
3109 if (tag_commit0 == NULL) {
3110 error = got_error_from_errno("strdup");
3115 tag_commit = tag_commit0;
3116 while (*tag_commit == '\n')
3121 newline = strchr(tag_commit, '\n');
3125 if (summary_header_displayed == 0) {
3126 kerr = khtml_attr(gw_trans->gw_html_req,
3127 KELEM_DIV, KATTR_ID,
3128 "summary_tags_title_wrapper", KATTR__MAX);
3129 if (kerr != KCGI_OK)
3131 kerr = khtml_attr(gw_trans->gw_html_req,
3132 KELEM_DIV, KATTR_ID,
3133 "summary_tags_title", KATTR__MAX);
3134 if (kerr != KCGI_OK)
3136 kerr = khtml_puts(gw_trans->gw_html_req,
3138 if (kerr != KCGI_OK)
3140 kerr = khtml_closeelem(gw_trans->gw_html_req,
3142 if (kerr != KCGI_OK)
3144 kerr = khtml_attr(gw_trans->gw_html_req,
3145 KELEM_DIV, KATTR_ID,
3146 "summary_tags_content", KATTR__MAX);
3147 if (kerr != KCGI_OK)
3149 summary_header_displayed = 1;
3152 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3153 KATTR_ID, "tag_wrapper", KATTR__MAX);
3154 if (kerr != KCGI_OK)
3156 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3157 KATTR_ID, "tag_age", KATTR__MAX);
3158 if (kerr != KCGI_OK)
3160 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3163 kerr = khtml_puts(gw_trans->gw_html_req,
3165 if (kerr != KCGI_OK)
3167 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3168 if (kerr != KCGI_OK)
3170 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3171 KATTR_ID, "tag", KATTR__MAX);
3172 if (kerr != KCGI_OK)
3174 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3175 if (kerr != KCGI_OK)
3177 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3178 if (kerr != KCGI_OK)
3180 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3181 KATTR_ID, "tag_name", KATTR__MAX);
3182 if (kerr != KCGI_OK)
3185 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3186 gw_trans->repo_name, "action", "tag", "commit",
3188 if (href_tag == NULL) {
3189 error = got_error_from_errno("khttp_urlpart");
3192 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3193 KATTR_HREF, href_tag, KATTR__MAX);
3194 if (kerr != KCGI_OK)
3196 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3197 if (kerr != KCGI_OK)
3199 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3200 if (kerr != KCGI_OK)
3203 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3204 KATTR_ID, "navs_wrapper", KATTR__MAX);
3205 if (kerr != KCGI_OK)
3207 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3208 KATTR_ID, "navs", KATTR__MAX);
3209 if (kerr != KCGI_OK)
3212 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3213 KATTR_HREF, href_tag, KATTR__MAX);
3214 if (kerr != KCGI_OK)
3216 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3217 if (kerr != KCGI_OK)
3219 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3220 if (kerr != KCGI_OK)
3223 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3224 if (kerr != KCGI_OK)
3227 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3228 "path", gw_trans->repo_name, "action", "briefs",
3229 "commit", id_str, NULL);
3230 if (href_briefs == NULL) {
3231 error = got_error_from_errno("khttp_urlpart");
3234 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3235 KATTR_HREF, href_briefs, KATTR__MAX);
3236 if (kerr != KCGI_OK)
3238 kerr = khtml_puts(gw_trans->gw_html_req,
3240 if (kerr != KCGI_OK)
3242 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3243 if (kerr != KCGI_OK)
3246 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3247 if (kerr != KCGI_OK)
3250 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3251 "path", gw_trans->repo_name, "action", "commits",
3252 "commit", id_str, NULL);
3253 if (href_commits == NULL) {
3254 error = got_error_from_errno("khttp_urlpart");
3257 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3258 KATTR_HREF, href_commits, KATTR__MAX);
3259 if (kerr != KCGI_OK)
3261 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3262 if (kerr != KCGI_OK)
3264 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3265 if (kerr != KCGI_OK)
3268 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3269 KATTR_ID, "dotted_line", KATTR__MAX);
3270 if (kerr != KCGI_OK)
3272 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3273 if (kerr != KCGI_OK)
3277 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3278 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3279 if (kerr != KCGI_OK)
3281 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3282 if (kerr != KCGI_OK)
3284 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3285 if (kerr != KCGI_OK)
3287 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3288 KATTR_ID, "tag_info_date", KATTR__MAX);
3289 if (kerr != KCGI_OK)
3291 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3294 kerr = khtml_puts(gw_trans->gw_html_req,
3296 if (kerr != KCGI_OK)
3298 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3299 if (kerr != KCGI_OK)
3302 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3303 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3304 if (kerr != KCGI_OK)
3306 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3307 if (kerr != KCGI_OK)
3309 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3310 if (kerr != KCGI_OK)
3312 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3313 KATTR_ID, "tag_info_date", KATTR__MAX);
3314 if (kerr != KCGI_OK)
3316 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3317 if (kerr != KCGI_OK)
3319 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3320 if (kerr != KCGI_OK)
3323 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3324 KATTR_ID, "tag_info", KATTR__MAX);
3325 if (kerr != KCGI_OK)
3327 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3328 if (kerr != KCGI_OK)
3334 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3335 if (kerr != KCGI_OK)
3338 if (limit && --limit == 0)
3342 got_object_tag_close(tag);
3355 href_commits = NULL;
3357 if (tag_count == 0) {
3358 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3359 "summary_tags_title_wrapper", KATTR__MAX);
3360 if (kerr != KCGI_OK)
3362 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3363 "summary_tags_title", KATTR__MAX);
3364 if (kerr != KCGI_OK)
3366 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3367 if (kerr != KCGI_OK)
3369 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3370 if (kerr != KCGI_OK)
3372 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3373 "summary_tags_content", KATTR__MAX);
3374 if (kerr != KCGI_OK)
3376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3377 "tags_info", KATTR__MAX);
3378 if (kerr != KCGI_OK)
3380 kerr = khttp_puts(gw_trans->gw_req,
3381 "There are no tags for this repo.");
3382 if (kerr != KCGI_OK)
3384 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3389 TAILQ_FOREACH_REVERSE(re, &refs, got_reflist_head, entry) {
3390 const char *refname;
3391 struct got_object_id *id;
3392 struct got_commit_object *commit = NULL;
3394 refname = got_ref_get_name(re->ref);
3395 if (strncmp(refname, "refs/tags/", 10) != 0)
3399 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3403 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3405 if (error->code != GOT_ERR_OBJ_TYPE) {
3409 /* "lightweight" tag */
3410 error = got_object_open_as_commit(&commit,
3411 gw_trans->repo, id);
3416 error = got_object_id_str(&id_str, id);
3420 error = got_object_id_str(&id_str,
3421 got_object_tag_get_object_id(tag));
3426 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3427 strlen(id_str)) != 0)
3430 if (commit_found == 0 && tag_type == TAGBRIEF &&
3431 gw_trans->commit_id != NULL &&
3432 strncmp(id_str, gw_trans->commit_id, strlen(id_str)) != 0)
3437 if (gw_trans->commit_id != NULL &&
3438 strcmp(id_str, gw_trans->commit_id) != 0 &&
3439 (re == TAILQ_FIRST(&refs) ||
3440 c_cnt == gw_trans->gw_conf->got_max_commits_display)) {
3441 gw_trans->prev_id = strdup(id_str);
3442 if (gw_trans->prev_id == NULL) {
3443 error = got_error_from_errno("strdup");
3452 got_object_tag_close(tag);
3459 got_ref_list_free(&refs);
3460 if (error == NULL && kerr != KCGI_OK)
3461 error = gw_kcgi_error(kerr);
3466 gw_free_header(struct gw_header *header)
3469 free(header->author);
3470 free(header->committer);
3471 free(header->refs_str);
3472 free(header->commit_id);
3473 free(header->parent_id);
3474 free(header->tree_id);
3475 free(header->commit_msg);
3478 static struct gw_header *
3481 struct gw_header *header;
3483 header = malloc(sizeof(*header));
3487 header->path = NULL;
3488 TAILQ_INIT(&header->refs);
3490 header->refs_str = NULL;
3491 header->commit_id = NULL;
3492 header->committer = NULL;
3493 header->author = NULL;
3494 header->parent_id = NULL;
3495 header->tree_id = NULL;
3496 header->commit_msg = NULL;
3501 static const struct got_error *
3502 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3503 int limit, struct got_object_id *id)
3505 const struct got_error *error = NULL;
3506 struct got_commit_graph *graph = NULL;
3507 struct got_commit_object *commit = NULL;
3508 int chk_next = 0, chk_multi = 0, c_cnt = 0, commit_found = 0;
3509 struct gw_header *t_header = NULL;
3511 error = got_commit_graph_open(&graph, header->path, 0);
3515 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3521 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3524 if (error->code == GOT_ERR_ITER_COMPLETED)
3531 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3534 if (limit == 1 && chk_multi == 0 &&
3535 gw_trans->gw_conf->got_max_commits_display != 1) {
3536 error = gw_get_commit(gw_trans, header, commit, id);
3542 struct gw_header *n_header = NULL;
3543 if ((n_header = gw_init_header()) == NULL) {
3544 error = got_error_from_errno("malloc");
3547 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3549 error = got_ref_list(&n_header->refs, gw_trans->repo,
3550 NULL, got_ref_cmp_by_name, NULL);
3554 error = gw_get_commit(gw_trans, n_header, commit, id);
3557 got_ref_list_free(&n_header->refs);
3559 if (gw_trans->commit_id != NULL) {
3560 if (strcmp(gw_trans->commit_id,
3561 n_header->commit_id) == 0)
3567 * check for one more commit before breaking,
3568 * so we know whether to navigate through gw_briefs
3569 * gw_commits and gw_summary
3571 if (chk_next && (gw_trans->action == GW_BRIEFS ||
3572 gw_trans->action == GW_COMMITS ||
3573 gw_trans->action == GW_SUMMARY)) {
3574 gw_trans->next_id = strdup(n_header->commit_id);
3575 if (gw_trans->next_id == NULL)
3576 error = got_error_from_errno("strdup");
3577 TAILQ_REMOVE(&gw_trans->gw_headers, n_header,
3583 if (commit_found == 1 && (error || (limit && --limit == 0))) {
3590 if (gw_trans->prev_id == NULL && gw_trans->commit_id != NULL &&
3591 (gw_trans->action == GW_BRIEFS || gw_trans->action == GW_COMMITS)) {
3593 TAILQ_FOREACH_REVERSE(t_header, &gw_trans->gw_headers,
3595 if (commit_found == 0 &&
3596 strcmp(gw_trans->commit_id,
3597 t_header->commit_id) != 0)
3601 if (gw_trans->commit_id != NULL &&
3602 strcmp(gw_trans->commit_id,
3603 t_header->commit_id) != 0 &&
3604 (c_cnt == gw_trans->gw_conf->got_max_commits_display
3606 TAILQ_FIRST(&gw_trans->gw_headers))) {
3607 gw_trans->prev_id = strdup(t_header->commit_id);
3608 if (gw_trans->prev_id == NULL)
3609 error = got_error_from_errno("strdup");
3617 got_object_commit_close(commit);
3619 got_commit_graph_close(graph);
3623 static const struct got_error *
3624 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3625 struct got_commit_object *commit, struct got_object_id *id)
3627 const struct got_error *error = NULL;
3628 struct got_reflist_entry *re;
3629 struct got_object_id *id2 = NULL;
3630 struct got_object_qid *parent_id;
3631 char *commit_msg = NULL, *commit_msg0;
3634 TAILQ_FOREACH(re, &header->refs, entry) {
3637 struct got_tag_object *tag = NULL;
3638 struct got_object_id *ref_id;
3641 if (got_ref_is_symbolic(re->ref))
3644 name = got_ref_get_name(re->ref);
3645 if (strncmp(name, "refs/", 5) == 0)
3647 if (strncmp(name, "got/", 4) == 0)
3649 if (strncmp(name, "heads/", 6) == 0)
3651 if (strncmp(name, "remotes/", 8) == 0) {
3653 s = strstr(name, "/" GOT_REF_HEAD);
3654 if (s != NULL && s[strlen(s)] == '\0')
3657 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3660 if (strncmp(name, "tags/", 5) == 0) {
3661 error = got_object_open_as_tag(&tag, gw_trans->repo,
3664 if (error->code != GOT_ERR_OBJ_TYPE) {
3669 * Ref points at something other
3676 cmp = got_object_id_cmp(tag ?
3677 got_object_tag_get_object_id(tag) : ref_id, id);
3680 got_object_tag_close(tag);
3683 s = header->refs_str;
3684 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3685 s ? ", " : "", name) == -1) {
3686 error = got_error_from_errno("asprintf");
3688 header->refs_str = NULL;
3694 error = got_object_id_str(&header->commit_id, id);
3698 error = got_object_id_str(&header->tree_id,
3699 got_object_commit_get_tree_id(commit));
3703 if (gw_trans->action == GW_DIFF) {
3704 parent_id = STAILQ_FIRST(
3705 got_object_commit_get_parent_ids(commit));
3706 if (parent_id != NULL) {
3707 id2 = got_object_id_dup(&parent_id->id);
3709 error = got_object_id_str(&header->parent_id, id2);
3714 header->parent_id = strdup("/dev/null");
3715 if (header->parent_id == NULL) {
3716 error = got_error_from_errno("strdup");
3722 header->committer_time =
3723 got_object_commit_get_committer_time(commit);
3726 strdup(got_object_commit_get_author(commit));
3727 if (header->author == NULL) {
3728 error = got_error_from_errno("strdup");
3732 strdup(got_object_commit_get_committer(commit));
3733 if (header->committer == NULL) {
3734 error = got_error_from_errno("strdup");
3737 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3741 commit_msg = commit_msg0;
3742 while (*commit_msg == '\n')
3745 header->commit_msg = strdup(commit_msg);
3746 if (header->commit_msg == NULL)
3747 error = got_error_from_errno("strdup");
3752 static const struct got_error *
3753 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3755 const struct got_error *error = NULL;
3756 char *in_repo_path = NULL;
3757 struct got_object_id *id = NULL;
3758 struct got_reference *ref;
3760 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3764 if (gw_trans->commit_id == NULL || gw_trans->action == GW_COMMITS ||
3765 gw_trans->action == GW_BRIEFS || gw_trans->action == GW_SUMMARY ||
3766 gw_trans->action == GW_TAGS) {
3767 error = got_ref_open(&ref, gw_trans->repo,
3768 gw_trans->headref, 0);
3772 error = got_ref_resolve(&id, gw_trans->repo, ref);
3777 error = got_ref_open(&ref, gw_trans->repo,
3778 gw_trans->commit_id, 0);
3779 if (error == NULL) {
3781 error = got_ref_resolve(&id, gw_trans->repo, ref);
3785 error = got_object_get_type(&obj_type, gw_trans->repo,
3789 if (obj_type == GOT_OBJ_TYPE_TAG) {
3790 struct got_tag_object *tag;
3791 error = got_object_open_as_tag(&tag,
3792 gw_trans->repo, id);
3795 if (got_object_tag_get_object_type(tag) !=
3796 GOT_OBJ_TYPE_COMMIT) {
3797 got_object_tag_close(tag);
3798 error = got_error(GOT_ERR_OBJ_TYPE);
3802 id = got_object_id_dup(
3803 got_object_tag_get_object_id(tag));
3805 error = got_error_from_errno(
3806 "got_object_id_dup");
3807 got_object_tag_close(tag);
3810 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3811 error = got_error(GOT_ERR_OBJ_TYPE);
3815 error = got_repo_match_object_id_prefix(&id,
3816 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3822 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3823 gw_trans->repo_path);
3828 header->path = strdup(in_repo_path);
3829 if (header->path == NULL) {
3830 error = got_error_from_errno("strdup");
3835 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3836 got_ref_cmp_by_name, NULL);
3840 error = gw_get_commits(gw_trans, header, limit, id);
3842 got_ref_list_free(&header->refs);
3852 char datebuf[11]; /* YYYY-MM-DD + NUL */
3855 struct gw_blame_cb_args {
3856 struct blame_line *lines;
3860 off_t *line_offsets;
3862 struct got_repository *repo;
3863 struct gw_trans *gw_trans;
3866 static const struct got_error *
3867 gw_blame_cb(void *arg, int nlines, int lineno,
3868 struct got_commit_object *commit, struct got_object_id *id)
3870 const struct got_error *err = NULL;
3871 struct gw_blame_cb_args *a = arg;
3872 struct blame_line *bline;
3874 size_t linesize = 0;
3877 time_t committer_time;
3878 enum kcgi_err kerr = KCGI_OK;
3880 if (nlines != a->nlines ||
3881 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3882 return got_error(GOT_ERR_RANGE);
3885 return NULL; /* no change in this commit */
3887 /* Annotate this line. */
3888 bline = &a->lines[lineno - 1];
3889 if (bline->annotated)
3891 err = got_object_id_str(&bline->id_str, id);
3895 bline->committer = strdup(got_object_commit_get_committer(commit));
3896 if (bline->committer == NULL) {
3897 err = got_error_from_errno("strdup");
3901 committer_time = got_object_commit_get_committer_time(commit);
3902 if (gmtime_r(&committer_time, &tm) == NULL)
3903 return got_error_from_errno("gmtime_r");
3904 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3906 err = got_error(GOT_ERR_NO_SPACE);
3909 bline->annotated = 1;
3911 /* Print lines annotated so far. */
3912 bline = &a->lines[a->lineno_cur - 1];
3913 if (!bline->annotated)
3916 offset = a->line_offsets[a->lineno_cur - 1];
3917 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3918 err = got_error_from_errno("fseeko");
3922 while (bline->annotated) {
3923 char *smallerthan, *at, *nl, *committer;
3924 char *href_diff = NULL;
3927 if (getline(&line, &linesize, a->f) == -1) {
3929 err = got_error_from_errno("getline");
3933 committer = bline->committer;
3934 smallerthan = strchr(committer, '<');
3935 if (smallerthan && smallerthan[1] != '\0')
3936 committer = smallerthan + 1;
3937 at = strchr(committer, '@');
3940 len = strlen(committer);
3942 committer[8] = '\0';
3944 nl = strchr(line, '\n');
3948 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3949 "blame_wrapper", KATTR__MAX);
3950 if (kerr != KCGI_OK)
3952 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3953 "blame_number", KATTR__MAX);
3954 if (kerr != KCGI_OK)
3956 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3957 a->nlines_prec, a->lineno_cur);
3958 if (kerr != KCGI_OK)
3960 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3961 if (kerr != KCGI_OK)
3964 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3965 "blame_hash", KATTR__MAX);
3966 if (kerr != KCGI_OK)
3969 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3970 a->gw_trans->repo_name, "action", "diff", "commit",
3971 bline->id_str, NULL);
3972 if (href_diff == NULL) {
3973 err = got_error_from_errno("khttp_urlpart");
3976 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3977 KATTR_HREF, href_diff, KATTR__MAX);
3978 if (kerr != KCGI_OK)
3980 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3982 if (kerr != KCGI_OK)
3984 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3985 if (kerr != KCGI_OK)
3988 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3989 "blame_date", KATTR__MAX);
3990 if (kerr != KCGI_OK)
3992 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3993 if (kerr != KCGI_OK)
3995 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3996 if (kerr != KCGI_OK)
3999 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4000 "blame_author", KATTR__MAX);
4001 if (kerr != KCGI_OK)
4003 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
4004 if (kerr != KCGI_OK)
4006 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4007 if (kerr != KCGI_OK)
4010 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4011 "blame_code", KATTR__MAX);
4012 if (kerr != KCGI_OK)
4014 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
4015 if (kerr != KCGI_OK)
4017 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4018 if (kerr != KCGI_OK)
4021 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4022 if (kerr != KCGI_OK)
4026 bline = &a->lines[a->lineno_cur - 1];
4032 if (err == NULL && kerr != KCGI_OK)
4033 err = gw_kcgi_error(kerr);
4037 static const struct got_error *
4038 gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
4040 const struct got_error *error = NULL;
4041 struct got_object_id *obj_id = NULL;
4042 struct got_object_id *commit_id = NULL;
4043 struct got_commit_object *commit = NULL;
4044 struct got_blob_object *blob = NULL;
4045 char *path = NULL, *in_repo_path = NULL;
4046 struct gw_blame_cb_args bca;
4050 memset(&bca, 0, sizeof(bca));
4052 if (asprintf(&path, "%s%s%s",
4053 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4054 gw_trans->repo_folder ? "/" : "",
4055 gw_trans->repo_file) == -1) {
4056 error = got_error_from_errno("asprintf");
4060 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4064 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4065 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4069 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4073 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit,
4078 if (obj_id == NULL) {
4079 error = got_error(GOT_ERR_NO_OBJ);
4083 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4087 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4088 error = got_error(GOT_ERR_OBJ_TYPE);
4092 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4096 bca.f = got_opentemp();
4097 if (bca.f == NULL) {
4098 error = got_error_from_errno("got_opentemp");
4101 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4102 &bca.line_offsets, bca.f, blob);
4103 if (error || bca.nlines == 0)
4106 /* Don't include \n at EOF in the blame line count. */
4107 if (bca.line_offsets[bca.nlines - 1] == filesize)
4110 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4111 if (bca.lines == NULL) {
4112 error = got_error_from_errno("calloc");
4116 bca.nlines_prec = 0;
4122 bca.repo = gw_trans->repo;
4123 bca.gw_trans = gw_trans;
4125 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4134 free(bca.line_offsets);
4135 for (i = 0; i < bca.nlines; i++) {
4136 struct blame_line *bline = &bca.lines[i];
4137 free(bline->id_str);
4138 free(bline->committer);
4141 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4142 error = got_error_from_errno("fclose");
4145 got_object_blob_close(blob);
4147 got_object_commit_close(commit);
4151 static const struct got_error *
4152 gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
4154 const struct got_error *error = NULL;
4155 struct got_object_id *obj_id = NULL;
4156 struct got_object_id *commit_id = NULL;
4157 struct got_commit_object *commit = NULL;
4158 struct got_blob_object *blob = NULL;
4159 char *path = NULL, *in_repo_path = NULL;
4160 int obj_type, set_mime = 0;
4163 enum kcgi_err kerr = KCGI_OK;
4165 if (asprintf(&path, "%s%s%s",
4166 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4167 gw_trans->repo_folder ? "/" : "",
4168 gw_trans->repo_file) == -1) {
4169 error = got_error_from_errno("asprintf");
4173 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4177 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4178 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4182 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4186 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit,
4191 if (obj_id == NULL) {
4192 error = got_error(GOT_ERR_NO_OBJ);
4196 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4200 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4201 error = got_error(GOT_ERR_OBJ_TYPE);
4205 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4209 hdrlen = got_object_blob_get_hdrlen(blob);
4211 error = got_object_blob_read_block(&len, blob);
4214 buf = got_object_blob_get_read_buf(blob);
4217 * Skip blob object header first time around,
4218 * which also contains a zero byte.
4221 if (set_mime == 0) {
4222 if (isbinary(buf, len - hdrlen))
4223 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4225 gw_trans->mime = KMIME_TEXT_PLAIN;
4227 error = gw_display_index(gw_trans);
4231 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4232 if (kerr != KCGI_OK)
4242 got_object_blob_close(blob);
4244 got_object_commit_close(commit);
4245 if (error == NULL && kerr != KCGI_OK)
4246 error = gw_kcgi_error(kerr);
4250 static const struct got_error *
4251 gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
4253 const struct got_error *error = NULL;
4254 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4255 struct got_tree_object *tree = NULL;
4256 struct got_commit_object *commit = NULL;
4257 char *path = NULL, *in_repo_path = NULL;
4258 char *id_str = NULL;
4259 char *build_folder = NULL;
4260 char *href_blob = NULL, *href_blame = NULL;
4261 const char *class = NULL;
4262 int nentries, i, class_flip = 0;
4263 enum kcgi_err kerr = KCGI_OK;
4265 if (gw_trans->repo_folder != NULL) {
4266 path = strdup(gw_trans->repo_folder);
4268 error = got_error_from_errno("strdup");
4272 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4273 gw_trans->repo_path);
4277 path = in_repo_path;
4280 if (gw_trans->commit_id == NULL) {
4281 struct got_reference *head_ref;
4282 error = got_ref_open(&head_ref, gw_trans->repo,
4283 gw_trans->headref, 0);
4286 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4289 got_ref_close(head_ref);
4291 * gw_trans->commit_id was not parsed from the querystring
4292 * we hit this code path from gw_index, where we don't know the
4293 * commit values for the tree link yet, so set
4294 * gw_trans->commit_id here to continue further into the tree
4296 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4301 error = got_repo_match_object_id(&commit_id, NULL,
4302 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
4308 error = got_object_open_as_commit(&commit, gw_trans->repo, commit_id);
4312 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit,
4317 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4321 nentries = got_object_tree_get_nentries(tree);
4322 for (i = 0; i < nentries; i++) {
4323 struct got_tree_entry *te;
4324 const char *modestr = "";
4327 te = got_object_tree_get_entry(tree, i);
4329 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4333 mode = got_tree_entry_get_mode(te);
4334 if (got_object_tree_entry_is_submodule(te))
4336 else if (S_ISLNK(mode))
4338 else if (S_ISDIR(mode))
4340 else if (mode & S_IXUSR)
4343 if (class_flip == 0) {
4344 class = "back_lightgray";
4347 class = "back_white";
4351 if (S_ISDIR(mode)) {
4352 if (asprintf(&build_folder, "%s/%s",
4353 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4354 got_tree_entry_get_name(te)) == -1) {
4355 error = got_error_from_errno("asprintf");
4359 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4360 gw_trans->repo_name, "action",
4361 gw_get_action_name(gw_trans), "commit",
4362 gw_trans->commit_id, "folder", build_folder, NULL);
4363 if (href_blob == NULL) {
4364 error = got_error_from_errno("khttp_urlpart");
4367 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4368 KATTR_ID, "tree_wrapper", KATTR__MAX);
4369 if (kerr != KCGI_OK)
4371 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4372 KATTR_ID, "tree_line", KATTR_CLASS, class,
4374 if (kerr != KCGI_OK)
4376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4377 KATTR_HREF, href_blob, KATTR_CLASS,
4378 "diff_directory", KATTR__MAX);
4379 if (kerr != KCGI_OK)
4381 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4382 got_tree_entry_get_name(te), modestr);
4383 if (kerr != KCGI_OK)
4385 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4386 if (kerr != KCGI_OK)
4388 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4389 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4391 if (kerr != KCGI_OK)
4393 kerr = khtml_entity(gw_trans->gw_html_req,
4395 if (kerr != KCGI_OK)
4397 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4398 if (kerr != KCGI_OK)
4401 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4402 gw_trans->repo_name, "action", "blob", "commit",
4403 gw_trans->commit_id, "file",
4404 got_tree_entry_get_name(te), "folder",
4405 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4407 if (href_blob == NULL) {
4408 error = got_error_from_errno("khttp_urlpart");
4411 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4412 gw_trans->repo_name, "action", "blame", "commit",
4413 gw_trans->commit_id, "file",
4414 got_tree_entry_get_name(te), "folder",
4415 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4417 if (href_blame == NULL) {
4418 error = got_error_from_errno("khttp_urlpart");
4421 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4422 KATTR_ID, "tree_wrapper", KATTR__MAX);
4423 if (kerr != KCGI_OK)
4425 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4426 KATTR_ID, "tree_line", KATTR_CLASS, class,
4428 if (kerr != KCGI_OK)
4430 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4431 KATTR_HREF, href_blob, KATTR__MAX);
4432 if (kerr != KCGI_OK)
4434 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4435 got_tree_entry_get_name(te), modestr);
4436 if (kerr != KCGI_OK)
4438 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4439 if (kerr != KCGI_OK)
4441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4442 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4444 if (kerr != KCGI_OK)
4447 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4448 KATTR_HREF, href_blob, KATTR__MAX);
4449 if (kerr != KCGI_OK)
4451 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4452 if (kerr != KCGI_OK)
4454 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4455 if (kerr != KCGI_OK)
4458 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4459 if (kerr != KCGI_OK)
4462 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4463 KATTR_HREF, href_blame, KATTR__MAX);
4464 if (kerr != KCGI_OK)
4466 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4467 if (kerr != KCGI_OK)
4470 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4471 if (kerr != KCGI_OK)
4479 build_folder = NULL;
4483 got_object_tree_close(tree);
4485 got_object_commit_close(commit);
4492 if (error == NULL && kerr != KCGI_OK)
4493 error = gw_kcgi_error(kerr);
4497 static const struct got_error *
4498 gw_output_repo_heads(struct gw_trans *gw_trans)
4500 const struct got_error *error = NULL;
4501 struct got_reflist_head refs;
4502 struct got_reflist_entry *re;
4503 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4504 char *href_commits = NULL;
4505 enum kcgi_err kerr = KCGI_OK;
4509 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4510 got_ref_cmp_by_name, NULL);
4514 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4515 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4516 if (kerr != KCGI_OK)
4518 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4519 KATTR_ID, "summary_heads_title", KATTR__MAX);
4520 if (kerr != KCGI_OK)
4522 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4523 if (kerr != KCGI_OK)
4525 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4526 if (kerr != KCGI_OK)
4528 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4529 KATTR_ID, "summary_heads_content", KATTR__MAX);
4530 if (kerr != KCGI_OK)
4533 TAILQ_FOREACH(re, &refs, entry) {
4534 const char *refname;
4536 if (got_ref_is_symbolic(re->ref))
4539 refname = got_ref_get_name(re->ref);
4540 if (strncmp(refname, "refs/heads/", 11) != 0)
4543 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4548 if (strncmp(refname, "refs/heads/", 11) == 0)
4551 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4552 KATTR_ID, "heads_wrapper", KATTR__MAX);
4553 if (kerr != KCGI_OK)
4555 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4556 KATTR_ID, "heads_age", KATTR__MAX);
4557 if (kerr != KCGI_OK)
4559 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4560 if (kerr != KCGI_OK)
4562 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4563 if (kerr != KCGI_OK)
4565 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4566 KATTR_ID, "heads_space", KATTR__MAX);
4567 if (kerr != KCGI_OK)
4569 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4570 if (kerr != KCGI_OK)
4572 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4573 if (kerr != KCGI_OK)
4575 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4576 KATTR_ID, "head", KATTR__MAX);
4577 if (kerr != KCGI_OK)
4580 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4581 gw_trans->repo_name, "action", "summary", "headref",
4583 if (href_summary == NULL) {
4584 error = got_error_from_errno("khttp_urlpart");
4587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4588 href_summary, KATTR__MAX);
4589 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4590 if (kerr != KCGI_OK)
4592 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4593 if (kerr != KCGI_OK)
4596 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4597 "navs_wrapper", KATTR__MAX);
4598 if (kerr != KCGI_OK)
4600 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4601 "navs", KATTR__MAX);
4602 if (kerr != KCGI_OK)
4605 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4606 href_summary, KATTR__MAX);
4607 if (kerr != KCGI_OK)
4609 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4610 if (kerr != KCGI_OK)
4612 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4613 if (kerr != KCGI_OK)
4616 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4617 if (kerr != KCGI_OK)
4620 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4621 gw_trans->repo_name, "action", "briefs", "headref",
4623 if (href_briefs == NULL) {
4624 error = got_error_from_errno("khttp_urlpart");
4627 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4628 href_briefs, KATTR__MAX);
4629 if (kerr != KCGI_OK)
4631 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4632 if (kerr != KCGI_OK)
4634 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4635 if (kerr != KCGI_OK)
4638 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4639 if (kerr != KCGI_OK)
4642 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4643 gw_trans->repo_name, "action", "commits", "headref",
4645 if (href_commits == NULL) {
4646 error = got_error_from_errno("khttp_urlpart");
4649 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4650 href_commits, KATTR__MAX);
4651 if (kerr != KCGI_OK)
4653 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4654 if (kerr != KCGI_OK)
4656 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4657 if (kerr != KCGI_OK)
4660 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4661 "dotted_line", KATTR__MAX);
4662 if (kerr != KCGI_OK)
4664 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4665 if (kerr != KCGI_OK)
4668 href_summary = NULL;
4672 href_commits = NULL;
4675 got_ref_list_free(&refs);
4682 static const struct got_error *
4683 gw_output_site_link(struct gw_trans *gw_trans)
4685 const struct got_error *error = NULL;
4686 char *href_summary = NULL;
4687 enum kcgi_err kerr = KCGI_OK;
4689 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4690 "site_link", KATTR__MAX);
4691 if (kerr != KCGI_OK)
4693 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4695 if (kerr != KCGI_OK)
4697 kerr = khtml_puts(gw_trans->gw_html_req,
4698 gw_trans->gw_conf->got_site_link);
4699 if (kerr != KCGI_OK)
4701 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4702 if (kerr != KCGI_OK)
4705 if (gw_trans->repo_name != NULL) {
4706 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4707 if (kerr != KCGI_OK)
4710 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4711 gw_trans->repo_name, "action", "summary", NULL);
4712 if (href_summary == NULL) {
4713 error = got_error_from_errno("khttp_urlpart");
4716 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4717 href_summary, KATTR__MAX);
4718 if (kerr != KCGI_OK)
4720 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4721 if (kerr != KCGI_OK)
4723 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4724 if (kerr != KCGI_OK)
4726 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4727 gw_get_action_name(gw_trans));
4728 if (kerr != KCGI_OK)
4732 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4733 if (kerr != KCGI_OK)
4737 if (error == NULL && kerr != KCGI_OK)
4738 error = gw_kcgi_error(kerr);
4742 static const struct got_error *
4743 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4745 const struct got_error *error = NULL;
4747 enum kcgi_err kerr = KCGI_OK;
4749 if (strncmp(buf, "-", 1) == 0)
4750 color = "diff_minus";
4751 else if (strncmp(buf, "+", 1) == 0)
4752 color = "diff_plus";
4753 else if (strncmp(buf, "@@", 2) == 0)
4754 color = "diff_chunk_header";
4755 else if (strncmp(buf, "@@", 2) == 0)
4756 color = "diff_chunk_header";
4757 else if (strncmp(buf, "commit +", 8) == 0)
4758 color = "diff_meta";
4759 else if (strncmp(buf, "commit -", 8) == 0)
4760 color = "diff_meta";
4761 else if (strncmp(buf, "blob +", 6) == 0)
4762 color = "diff_meta";
4763 else if (strncmp(buf, "blob -", 6) == 0)
4764 color = "diff_meta";
4765 else if (strncmp(buf, "file +", 6) == 0)
4766 color = "diff_meta";
4767 else if (strncmp(buf, "file -", 6) == 0)
4768 color = "diff_meta";
4769 else if (strncmp(buf, "from:", 5) == 0)
4770 color = "diff_author";
4771 else if (strncmp(buf, "via:", 4) == 0)
4772 color = "diff_author";
4773 else if (strncmp(buf, "date:", 5) == 0)
4774 color = "diff_date";
4775 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4776 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4777 if (error == NULL && kerr != KCGI_OK)
4778 error = gw_kcgi_error(kerr);
4783 main(int argc, char *argv[])
4785 const struct got_error *error = NULL, *error2 = NULL;
4786 struct gw_trans *gw_trans;
4787 struct gw_dir *dir = NULL, *tdir;
4788 const char *page = "index";
4789 enum kcgi_err kerr = KCGI_OK;
4791 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4794 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4797 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4800 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4803 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4804 if (kerr != KCGI_OK) {
4805 error = gw_kcgi_error(kerr);
4809 TAILQ_INIT(&gw_trans->gw_dirs);
4810 TAILQ_INIT(&gw_trans->gw_headers);
4812 gw_trans->action = -1;
4814 gw_trans->repos_total = 0;
4815 gw_trans->repo_path = NULL;
4816 gw_trans->commit_id = NULL;
4817 gw_trans->next_id = NULL;
4818 gw_trans->prev_id = NULL;
4819 gw_trans->headref = GOT_REF_HEAD;
4820 gw_trans->mime = KMIME_TEXT_HTML;
4821 gw_trans->gw_tmpl->key = gw_templs;
4822 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4823 gw_trans->gw_tmpl->arg = gw_trans;
4824 gw_trans->gw_tmpl->cb = gw_template;
4826 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4830 error = gw_parse_querystring(gw_trans);
4834 if (gw_trans->action == GW_BLOB)
4835 error = gw_blob(gw_trans);
4837 error = gw_display_index(gw_trans);
4839 if (gw_trans->repo) {
4840 const struct got_error *close_err;
4841 close_err = got_repo_close(gw_trans->repo);
4846 gw_trans->error = error;
4847 gw_trans->action = GW_ERR;
4848 error2 = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
4850 goto cleanup; /* we can't display an error page */
4851 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
4852 if (kerr != KCGI_OK)
4853 goto cleanup; /* we can't display an error page */
4854 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
4855 gw_query_funcs[gw_trans->action].template);
4856 if (kerr != KCGI_OK) {
4857 khtml_close(gw_trans->gw_html_req);
4858 goto cleanup; /* we can't display an error page */
4863 free(gw_trans->gw_conf->got_repos_path);
4864 free(gw_trans->gw_conf->got_www_path);
4865 free(gw_trans->gw_conf->got_site_name);
4866 free(gw_trans->gw_conf->got_site_owner);
4867 free(gw_trans->gw_conf->got_site_link);
4868 free(gw_trans->gw_conf->got_logo);
4869 free(gw_trans->gw_conf->got_logo_url);
4870 free(gw_trans->gw_conf);
4871 free(gw_trans->commit_id);
4872 free(gw_trans->next_id);
4873 free(gw_trans->prev_id);
4874 free(gw_trans->repo_path);
4875 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4877 free(dir->description);
4884 khttp_free(gw_trans->gw_req);