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.
19 #include <sys/types.h>
33 #include <got_error.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct got_repository *repo;
59 struct gw_dir *gw_dir;
60 struct gotweb_config *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
64 const struct got_error *error;
65 const char *repo_name;
70 const char *repo_file;
75 unsigned int repos_total;
80 TAILQ_ENTRY(gw_header) entry;
81 struct got_reflist_head refs;
85 char *commit_id; /* id_str1 */
86 char *parent_id; /* id_str2 */
91 time_t committer_time;
95 TAILQ_ENTRY(gw_dir) entry;
137 static const char *const gw_templs[TEMPL__MAX] = {
147 static const struct kvalid gw_keys[KEY__ZMAX] = {
148 { kvalid_stringne, "action" },
149 { kvalid_stringne, "commit" },
150 { kvalid_stringne, "file" },
151 { kvalid_stringne, "folder" },
152 { kvalid_stringne, "headref" },
153 { kvalid_int, "page" },
154 { kvalid_stringne, "path" },
155 { kvalid_stringne, "prev" },
158 static struct gw_header *gw_init_header(void);
160 static void gw_free_header(struct gw_header *);
162 static int gw_template(size_t, void *);
164 static const struct got_error *gw_error(struct gw_trans *);
165 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
166 static const struct got_error *gw_get_repo_description(char **,
167 struct gw_trans *, char *);
168 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
170 static const struct got_error *gw_get_time_str(char **, time_t, int);
171 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
172 char *, const char *, int);
173 static const struct got_error *gw_output_file_blame(struct gw_trans *,
175 static const struct got_error *gw_output_blob_buf(struct gw_trans *,
177 static const struct got_error *gw_output_repo_tree(struct gw_trans *,
179 static const struct got_error *gw_output_diff(struct gw_trans *,
181 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
182 struct gw_header *, int, int);
183 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
184 static const struct got_error *gw_output_site_link(struct gw_trans *);
185 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
187 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
189 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
191 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
193 static const struct got_error *gw_gen_author_header(struct gw_trans *,
195 static const struct got_error *gw_gen_age_header(struct gw_trans *,
197 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
199 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
201 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
202 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
204 static const struct got_error *gw_display_index(struct gw_trans *);
205 static const struct got_error *gw_get_header(struct gw_trans *,
206 struct gw_header *, int);
207 static const struct got_error *gw_get_commits(struct gw_trans *,
208 struct gw_header *, int,
209 struct got_object_id *);
210 static const struct got_error *gw_get_commit(struct gw_trans *,
212 struct got_commit_object *,
213 struct got_object_id *);
214 static const struct got_error *gw_apply_unveil(const char *);
215 static const struct got_error *gw_blame_cb(void *, int, int,
216 struct got_object_id *);
217 static const struct got_error *gw_load_got_paths(struct gw_trans *);
218 static const struct got_error *gw_load_got_path(struct gw_trans *,
220 static const struct got_error *gw_parse_querystring(struct gw_trans *);
221 static const struct got_error *gw_blame(struct gw_trans *);
222 static const struct got_error *gw_blob(struct gw_trans *);
223 static const struct got_error *gw_diff(struct gw_trans *);
224 static const struct got_error *gw_index(struct gw_trans *);
225 static const struct got_error *gw_commits(struct gw_trans *);
226 static const struct got_error *gw_briefs(struct gw_trans *);
227 static const struct got_error *gw_summary(struct gw_trans *);
228 static const struct got_error *gw_tree(struct gw_trans *);
229 static const struct got_error *gw_tag(struct gw_trans *);
230 static const struct got_error *gw_tags(struct gw_trans *);
232 struct gw_query_action {
233 unsigned int func_id;
234 const char *func_name;
235 const struct got_error *(*func_main)(struct gw_trans *);
239 enum gw_query_actions {
253 static struct gw_query_action gw_query_funcs[] = {
254 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
255 { GW_BLOB, "blob", NULL, NULL },
256 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
257 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
258 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
259 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
260 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
261 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
262 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
263 { GW_TAGS, "tags", gw_tags, "gw_tmpl/tags.tmpl" },
264 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
268 gw_get_action_name(struct gw_trans *gw_trans)
270 return gw_query_funcs[gw_trans->action].func_name;
273 static const struct got_error *
274 gw_kcgi_error(enum kcgi_err kerr)
279 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
280 return got_error(GOT_ERR_CANCELLED);
282 if (kerr == KCGI_ENOMEM)
283 return got_error_set_errno(ENOMEM,
284 kcgi_strerror(kerr));
286 if (kerr == KCGI_ENFILE)
287 return got_error_set_errno(ENFILE,
288 kcgi_strerror(kerr));
290 if (kerr == KCGI_EAGAIN)
291 return got_error_set_errno(EAGAIN,
292 kcgi_strerror(kerr));
294 if (kerr == KCGI_FORM)
295 return got_error_msg(GOT_ERR_IO,
296 kcgi_strerror(kerr));
298 return got_error_from_errno(kcgi_strerror(kerr));
301 static const struct got_error *
302 gw_apply_unveil(const char *repo_path)
304 const struct got_error *err;
307 if (unveil("gmon.out", "rwc") != 0)
308 return got_error_from_errno2("unveil", "gmon.out");
310 if (repo_path && unveil(repo_path, "r") != 0)
311 return got_error_from_errno2("unveil", repo_path);
313 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
314 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
316 err = got_privsep_unveil_exec_helpers();
320 if (unveil(NULL, NULL) != 0)
321 return got_error_from_errno("unveil");
327 isbinary(const uint8_t *buf, size_t n)
331 for (i = 0; i < n; i++)
337 static const struct got_error *
338 gw_blame(struct gw_trans *gw_trans)
340 const struct got_error *error = NULL;
341 struct gw_header *header = NULL;
343 enum kcgi_err kerr = KCGI_OK;
346 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
348 return got_error_from_errno("pledge");
350 if ((header = gw_init_header()) == NULL)
351 return got_error_from_errno("malloc");
353 error = gw_apply_unveil(gw_trans->gw_dir->path);
357 /* check querystring */
358 if (gw_trans->repo_file == NULL) {
359 error = got_error_msg(GOT_ERR_QUERYSTRING,
360 "file required in querystring");
363 if (gw_trans->commit_id == NULL) {
364 error = got_error_msg(GOT_ERR_QUERYSTRING,
365 "commit required in querystring");
369 error = gw_get_header(gw_trans, header, 1);
372 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
373 "blame_header_wrapper", KATTR__MAX);
376 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
377 "blame_header", KATTR__MAX);
380 error = gw_get_time_str(&age, header->committer_time,
384 error = gw_gen_age_header(gw_trans, age ?age : "");
387 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
390 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
393 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
394 "dotted_line", KATTR__MAX);
397 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
401 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
402 "blame", KATTR__MAX);
405 error = gw_output_file_blame(gw_trans, header);
408 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
410 gw_free_header(header);
411 if (error == NULL && kerr != KCGI_OK)
412 error = gw_kcgi_error(kerr);
416 static const struct got_error *
417 gw_blob(struct gw_trans *gw_trans)
419 const struct got_error *error = NULL, *err = NULL;
420 struct gw_header *header = NULL;
421 enum kcgi_err kerr = KCGI_OK;
424 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
426 return got_error_from_errno("pledge");
428 if ((header = gw_init_header()) == NULL)
429 return got_error_from_errno("malloc");
431 error = gw_apply_unveil(gw_trans->gw_dir->path);
435 /* check querystring */
436 if (gw_trans->repo_file == NULL) {
437 error = got_error_msg(GOT_ERR_QUERYSTRING,
438 "file required in querystring");
441 if (gw_trans->commit_id == NULL) {
442 error = got_error_msg(GOT_ERR_QUERYSTRING,
443 "commit required in querystring");
446 error = gw_get_header(gw_trans, header, 1);
450 error = gw_output_blob_buf(gw_trans, header);
454 gw_trans->mime = KMIME_TEXT_PLAIN;
455 err = gw_display_index(gw_trans);
460 kerr = khttp_puts(gw_trans->gw_req, error->msg);
463 gw_free_header(header);
464 if (error == NULL && kerr != KCGI_OK)
465 error = gw_kcgi_error(kerr);
469 static const struct got_error *
470 gw_diff(struct gw_trans *gw_trans)
472 const struct got_error *error = NULL;
473 struct gw_header *header = NULL;
475 enum kcgi_err kerr = KCGI_OK;
478 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
480 return got_error_from_errno("pledge");
482 if ((header = gw_init_header()) == NULL)
483 return got_error_from_errno("malloc");
485 error = gw_apply_unveil(gw_trans->gw_dir->path);
489 error = gw_get_header(gw_trans, header, 1);
493 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
494 "diff_header_wrapper", KATTR__MAX);
497 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
498 "diff_header", KATTR__MAX);
501 error = gw_gen_diff_header(gw_trans, header->parent_id,
505 error = gw_gen_commit_header(gw_trans, header->commit_id,
509 error = gw_gen_tree_header(gw_trans, header->tree_id);
512 error = gw_gen_author_header(gw_trans, header->author);
515 error = gw_gen_committer_header(gw_trans, header->author);
518 error = gw_get_time_str(&age, header->committer_time,
522 error = gw_gen_age_header(gw_trans, age ?age : "");
525 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
528 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
531 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
532 "dotted_line", KATTR__MAX);
535 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
539 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
543 error = gw_output_diff(gw_trans, header);
547 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
549 gw_free_header(header);
551 if (error == NULL && kerr != KCGI_OK)
552 error = gw_kcgi_error(kerr);
556 static const struct got_error *
557 gw_index(struct gw_trans *gw_trans)
559 const struct got_error *error = NULL;
560 struct gw_dir *gw_dir = NULL;
561 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
562 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
563 char *href_tags = NULL;
564 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
565 enum kcgi_err kerr = KCGI_OK;
568 if (pledge("stdio rpath proc exec sendfd unveil",
570 error = got_error_from_errno("pledge");
574 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
578 error = gw_load_got_paths(gw_trans);
582 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
583 "index_header", KATTR__MAX);
585 return gw_kcgi_error(kerr);
586 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
587 "index_header_project", KATTR__MAX);
589 return gw_kcgi_error(kerr);
590 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
592 return gw_kcgi_error(kerr);
593 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
595 return gw_kcgi_error(kerr);
597 if (gw_trans->gw_conf->got_show_repo_description) {
598 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
599 "index_header_description", KATTR__MAX);
601 return gw_kcgi_error(kerr);
602 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
604 return gw_kcgi_error(kerr);
605 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
607 return gw_kcgi_error(kerr);
610 if (gw_trans->gw_conf->got_show_repo_owner) {
611 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
612 "index_header_owner", KATTR__MAX);
614 return gw_kcgi_error(kerr);
615 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
617 return gw_kcgi_error(kerr);
618 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
620 return gw_kcgi_error(kerr);
623 if (gw_trans->gw_conf->got_show_repo_age) {
624 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
625 "index_header_age", KATTR__MAX);
627 return gw_kcgi_error(kerr);
628 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
630 return gw_kcgi_error(kerr);
631 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
633 return gw_kcgi_error(kerr);
636 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
638 return gw_kcgi_error(kerr);
640 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
641 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
642 "index_wrapper", KATTR__MAX);
644 return gw_kcgi_error(kerr);
645 kerr = khtml_printf(gw_trans->gw_html_req,
646 "No repositories found in %s",
647 gw_trans->gw_conf->got_repos_path);
649 return gw_kcgi_error(kerr);
650 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
652 return gw_kcgi_error(kerr);
653 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
654 "dotted_line", KATTR__MAX);
656 return gw_kcgi_error(kerr);
657 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
659 return gw_kcgi_error(kerr);
663 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
666 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
667 if (gw_trans->page > 0 && (gw_trans->page *
668 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
675 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
676 "index_wrapper", KATTR__MAX);
680 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
681 gw_dir->name, "action", "summary", NULL);
682 if (href_summary == NULL) {
683 error = got_error_from_errno("khttp_urlpart");
686 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
687 "index_project", KATTR__MAX);
690 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
691 href_summary, KATTR__MAX);
694 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
697 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
700 if (gw_trans->gw_conf->got_show_repo_description) {
701 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
702 KATTR_ID, "index_project_description", KATTR__MAX);
705 kerr = khtml_puts(gw_trans->gw_html_req,
706 gw_dir->description ? gw_dir->description : "");
709 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
713 if (gw_trans->gw_conf->got_show_repo_owner) {
714 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
715 KATTR_ID, "index_project_owner", KATTR__MAX);
718 kerr = khtml_puts(gw_trans->gw_html_req,
719 gw_dir->owner ? gw_dir->owner : "");
722 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
726 if (gw_trans->gw_conf->got_show_repo_age) {
727 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
728 KATTR_ID, "index_project_age", KATTR__MAX);
731 kerr = khtml_puts(gw_trans->gw_html_req,
732 gw_dir->age ? gw_dir->age : "");
735 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
740 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
741 "navs_wrapper", KATTR__MAX);
744 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
749 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
750 href_summary, KATTR__MAX);
753 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
756 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
760 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
764 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
765 gw_dir->name, "action", "briefs", NULL);
766 if (href_briefs == NULL) {
767 error = got_error_from_errno("khttp_urlpart");
770 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
771 href_briefs, KATTR__MAX);
774 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
777 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
779 error = gw_kcgi_error(kerr);
781 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
785 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
786 gw_dir->name, "action", "commits", NULL);
787 if (href_commits == NULL) {
788 error = got_error_from_errno("khttp_urlpart");
791 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
792 href_commits, KATTR__MAX);
795 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
798 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
802 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
806 href_tags = khttp_urlpart(NULL, NULL, "gotweb", "path",
807 gw_dir->name, "action", "tags", NULL);
808 if (href_tags == NULL) {
809 error = got_error_from_errno("khttp_urlpart");
812 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
813 href_tags, KATTR__MAX);
816 kerr = khtml_puts(gw_trans->gw_html_req, "tags");
819 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
823 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
827 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
828 gw_dir->name, "action", "tree", NULL);
829 if (href_tree == NULL) {
830 error = got_error_from_errno("khttp_urlpart");
833 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
834 href_tree, KATTR__MAX);
837 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
841 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
844 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
845 "dotted_line", KATTR__MAX);
848 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
863 if (gw_trans->gw_conf->got_max_repos_display == 0)
866 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
867 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
868 (gw_trans->page > 0) &&
869 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
870 prev_disp == gw_trans->repos_total))) {
871 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
872 KATTR_ID, "np_wrapper", KATTR__MAX);
875 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
876 KATTR_ID, "nav_prev", KATTR__MAX);
881 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
882 (gw_trans->page > 0) &&
883 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
884 prev_disp == gw_trans->repos_total)) {
885 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "page",
886 KATTRX_INT, (int64_t)(gw_trans->page - 1), NULL);
887 if (href_prev == NULL) {
888 error = got_error_from_errno("khttp_urlpartx");
891 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
892 KATTR_HREF, href_prev, KATTR__MAX);
895 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
898 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
903 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
905 return gw_kcgi_error(kerr);
907 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
908 next_disp == gw_trans->gw_conf->got_max_repos_display &&
909 dir_c != (gw_trans->page + 1) *
910 gw_trans->gw_conf->got_max_repos_display) {
911 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
912 KATTR_ID, "nav_next", KATTR__MAX);
915 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "page",
916 KATTRX_INT, (int64_t)(gw_trans->page + 1), NULL);
917 if (href_next == NULL) {
918 error = got_error_from_errno("khttp_urlpartx");
921 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
922 KATTR_HREF, href_next, KATTR__MAX);
925 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
928 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
935 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
936 (gw_trans->page > 0) &&
937 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
938 prev_disp == gw_trans->repos_total)) {
939 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
953 if (error == NULL && kerr != KCGI_OK)
954 error = gw_kcgi_error(kerr);
958 static const struct got_error *
959 gw_commits(struct gw_trans *gw_trans)
961 const struct got_error *error = NULL;
962 struct gw_header *header = NULL, *n_header = NULL;
963 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
964 char *href_prev = NULL, *href_next = NULL;
965 enum kcgi_err kerr = KCGI_OK;
966 int commit_found = 0;
968 if ((header = gw_init_header()) == NULL)
969 return got_error_from_errno("malloc");
972 if (pledge("stdio rpath proc exec sendfd unveil",
974 error = got_error_from_errno("pledge");
978 error = gw_apply_unveil(gw_trans->gw_dir->path);
982 error = gw_get_header(gw_trans, header,
983 gw_trans->gw_conf->got_max_commits_display);
987 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
988 if (commit_found == 0 && gw_trans->commit_id != NULL) {
989 if (strcmp(gw_trans->commit_id,
990 n_header->commit_id) != 0)
995 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
996 "commits_line_wrapper", KATTR__MAX);
999 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
1000 n_header->refs_str);
1003 error = gw_gen_author_header(gw_trans, n_header->author);
1006 error = gw_gen_committer_header(gw_trans, n_header->author);
1009 error = gw_get_time_str(&age, n_header->committer_time,
1013 error = gw_gen_age_header(gw_trans, age ?age : "");
1016 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1017 if (kerr != KCGI_OK)
1020 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1021 "dotted_line", KATTR__MAX);
1022 if (kerr != KCGI_OK)
1024 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1025 if (kerr != KCGI_OK)
1028 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1029 "commit", KATTR__MAX);
1030 if (kerr != KCGI_OK)
1032 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
1033 if (kerr != KCGI_OK)
1035 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1036 if (kerr != KCGI_OK)
1039 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1040 gw_trans->repo_name, "action", "diff", "commit",
1041 n_header->commit_id, NULL);
1042 if (href_diff == NULL) {
1043 error = got_error_from_errno("khttp_urlpart");
1046 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1047 KATTR_ID, "navs_wrapper", KATTR__MAX);
1048 if (kerr != KCGI_OK)
1050 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1051 KATTR_ID, "navs", KATTR__MAX);
1052 if (kerr != KCGI_OK)
1054 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1055 KATTR_HREF, href_diff, KATTR__MAX);
1056 if (kerr != KCGI_OK)
1058 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1059 if (kerr != KCGI_OK)
1061 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1062 if (kerr != KCGI_OK)
1065 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1066 if (kerr != KCGI_OK)
1069 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1070 gw_trans->repo_name, "action", "tree", "commit",
1071 n_header->commit_id, NULL);
1072 if (href_tree == NULL) {
1073 error = got_error_from_errno("khttp_urlpart");
1076 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1077 KATTR_HREF, href_tree, KATTR__MAX);
1078 if (kerr != KCGI_OK)
1080 khtml_puts(gw_trans->gw_html_req, "tree");
1081 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1082 if (kerr != KCGI_OK)
1084 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1085 if (kerr != KCGI_OK)
1088 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1089 "solid_line", KATTR__MAX);
1090 if (kerr != KCGI_OK)
1092 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1093 if (kerr != KCGI_OK)
1100 if (gw_trans->next_id || gw_trans->prev_id) {
1101 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1102 KATTR_ID, "np_wrapper", KATTR__MAX);
1103 if (kerr != KCGI_OK)
1105 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1106 KATTR_ID, "nav_prev", KATTR__MAX);
1107 if (kerr != KCGI_OK)
1111 if (gw_trans->prev_id) {
1112 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1113 KATTRX_STRING, gw_trans->repo_name, "page",
1114 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1115 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1116 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1117 if (href_prev == NULL) {
1118 error = got_error_from_errno("khttp_urlpartx");
1121 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1122 KATTR_HREF, href_prev, KATTR__MAX);
1123 if (kerr != KCGI_OK)
1125 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1126 if (kerr != KCGI_OK)
1128 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1129 if (kerr != KCGI_OK)
1133 if (gw_trans->next_id || gw_trans->page > 0) {
1134 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1135 if (kerr != KCGI_OK)
1136 return gw_kcgi_error(kerr);
1139 if (gw_trans->next_id) {
1140 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1141 KATTR_ID, "nav_next", KATTR__MAX);
1142 if (kerr != KCGI_OK)
1144 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1145 KATTRX_STRING, gw_trans->repo_name, "page",
1146 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1147 KATTRX_STRING, "commits", "commit", KATTRX_STRING,
1148 gw_trans->next_id, NULL);
1149 if (href_next == NULL) {
1150 error = got_error_from_errno("khttp_urlpartx");
1153 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1154 KATTR_HREF, href_next, KATTR__MAX);
1155 if (kerr != KCGI_OK)
1157 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1158 if (kerr != KCGI_OK)
1160 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1161 if (kerr != KCGI_OK)
1165 if (gw_trans->next_id || gw_trans->page > 0) {
1166 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1167 if (kerr != KCGI_OK)
1171 gw_free_header(header);
1172 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1173 gw_free_header(n_header);
1179 if (error == NULL && kerr != KCGI_OK)
1180 error = gw_kcgi_error(kerr);
1184 static const struct got_error *
1185 gw_briefs(struct gw_trans *gw_trans)
1187 const struct got_error *error = NULL;
1188 struct gw_header *header = NULL, *n_header = NULL;
1189 char *age = NULL, *href_diff = NULL, *href_tree = NULL;
1190 char *href_prev = NULL, *href_next = NULL;
1191 char *newline, *smallerthan;
1192 enum kcgi_err kerr = KCGI_OK;
1193 int commit_found = 0;
1195 if ((header = gw_init_header()) == NULL)
1196 return got_error_from_errno("malloc");
1199 if (pledge("stdio rpath proc exec sendfd unveil",
1201 error = got_error_from_errno("pledge");
1205 if (gw_trans->action != GW_SUMMARY) {
1206 error = gw_apply_unveil(gw_trans->gw_dir->path);
1211 if (gw_trans->action == GW_SUMMARY)
1212 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1214 error = gw_get_header(gw_trans, header,
1215 gw_trans->gw_conf->got_max_commits_display);
1219 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1220 if (commit_found == 0 && gw_trans->commit_id != NULL) {
1221 if (strcmp(gw_trans->commit_id,
1222 n_header->commit_id) != 0)
1227 error = gw_get_time_str(&age, n_header->committer_time,
1232 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1233 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1234 if (kerr != KCGI_OK)
1237 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1238 KATTR_ID, "briefs_age", KATTR__MAX);
1239 if (kerr != KCGI_OK)
1241 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1242 if (kerr != KCGI_OK)
1244 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1245 if (kerr != KCGI_OK)
1248 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1249 KATTR_ID, "briefs_author", KATTR__MAX);
1250 if (kerr != KCGI_OK)
1252 smallerthan = strchr(n_header->author, '<');
1254 *smallerthan = '\0';
1255 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1256 if (kerr != KCGI_OK)
1258 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1259 if (kerr != KCGI_OK)
1262 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
1263 gw_trans->repo_name, "action", "diff", "commit",
1264 n_header->commit_id, NULL);
1265 if (href_diff == NULL) {
1266 error = got_error_from_errno("khttp_urlpart");
1269 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1270 KATTR_ID, "briefs_log", KATTR__MAX);
1271 if (kerr != KCGI_OK)
1273 newline = strchr(n_header->commit_msg, '\n');
1276 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1277 KATTR_HREF, href_diff, KATTR__MAX);
1278 if (kerr != KCGI_OK)
1280 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1281 if (kerr != KCGI_OK)
1283 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1284 if (kerr != KCGI_OK)
1287 if (n_header->refs_str) {
1288 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1289 if (kerr != KCGI_OK)
1291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1292 KATTR_ID, "refs_str", KATTR__MAX);
1293 if (kerr != KCGI_OK)
1295 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)",
1296 n_header->refs_str);
1297 if (kerr != KCGI_OK)
1299 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1300 if (kerr != KCGI_OK)
1304 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1305 if (kerr != KCGI_OK)
1308 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1309 KATTR_ID, "navs_wrapper", KATTR__MAX);
1310 if (kerr != KCGI_OK)
1312 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1313 KATTR_ID, "navs", KATTR__MAX);
1314 if (kerr != KCGI_OK)
1316 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1317 KATTR_HREF, href_diff, KATTR__MAX);
1318 if (kerr != KCGI_OK)
1320 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1321 if (kerr != KCGI_OK)
1323 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1324 if (kerr != KCGI_OK)
1327 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1328 if (kerr != KCGI_OK)
1331 href_tree = khttp_urlpart(NULL, NULL, "gotweb", "path",
1332 gw_trans->repo_name, "action", "tree", "commit",
1333 n_header->commit_id, NULL);
1334 if (href_tree == NULL) {
1335 error = got_error_from_errno("khttp_urlpart");
1338 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1339 KATTR_HREF, href_tree, KATTR__MAX);
1340 if (kerr != KCGI_OK)
1342 khtml_puts(gw_trans->gw_html_req, "tree");
1343 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1344 if (kerr != KCGI_OK)
1346 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1347 if (kerr != KCGI_OK)
1350 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1351 KATTR_ID, "dotted_line", KATTR__MAX);
1352 if (kerr != KCGI_OK)
1354 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1355 if (kerr != KCGI_OK)
1366 if (gw_trans->next_id || gw_trans->prev_id) {
1367 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1368 KATTR_ID, "np_wrapper", KATTR__MAX);
1369 if (kerr != KCGI_OK)
1371 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1372 KATTR_ID, "nav_prev", KATTR__MAX);
1373 if (kerr != KCGI_OK)
1377 if (gw_trans->prev_id) {
1378 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1379 KATTRX_STRING, gw_trans->repo_name, "page",
1380 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1381 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1382 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1383 if (href_prev == NULL) {
1384 error = got_error_from_errno("khttp_urlpartx");
1387 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1388 KATTR_HREF, href_prev, KATTR__MAX);
1389 if (kerr != KCGI_OK)
1391 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1392 if (kerr != KCGI_OK)
1394 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1395 if (kerr != KCGI_OK)
1399 if (gw_trans->next_id || gw_trans->page > 0) {
1400 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1401 if (kerr != KCGI_OK)
1402 return gw_kcgi_error(kerr);
1405 if (gw_trans->next_id) {
1406 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1407 KATTR_ID, "nav_next", KATTR__MAX);
1408 if (kerr != KCGI_OK)
1411 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1412 KATTRX_STRING, gw_trans->repo_name, "page",
1413 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1414 KATTRX_STRING, "briefs", "commit", KATTRX_STRING,
1415 gw_trans->next_id, NULL);
1416 if (href_next == NULL) {
1417 error = got_error_from_errno("khttp_urlpartx");
1420 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1421 KATTR_HREF, href_next, KATTR__MAX);
1422 if (kerr != KCGI_OK)
1424 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1425 if (kerr != KCGI_OK)
1427 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1428 if (kerr != KCGI_OK)
1432 if (gw_trans->next_id || gw_trans->page > 0) {
1433 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1434 if (kerr != KCGI_OK)
1438 gw_free_header(header);
1439 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1440 gw_free_header(n_header);
1446 if (error == NULL && kerr != KCGI_OK)
1447 error = gw_kcgi_error(kerr);
1451 static const struct got_error *
1452 gw_summary(struct gw_trans *gw_trans)
1454 const struct got_error *error = NULL;
1456 enum kcgi_err kerr = KCGI_OK;
1459 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1460 return got_error_from_errno("pledge");
1462 error = gw_apply_unveil(gw_trans->gw_dir->path);
1466 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1467 "summary_wrapper", KATTR__MAX);
1468 if (kerr != KCGI_OK)
1469 return gw_kcgi_error(kerr);
1471 if (gw_trans->gw_conf->got_show_repo_description &&
1472 gw_trans->gw_dir->description != NULL &&
1473 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1474 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1475 KATTR_ID, "description_title", KATTR__MAX);
1476 if (kerr != KCGI_OK)
1478 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1479 if (kerr != KCGI_OK)
1481 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1482 if (kerr != KCGI_OK)
1484 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1485 KATTR_ID, "description", KATTR__MAX);
1486 if (kerr != KCGI_OK)
1488 kerr = khtml_puts(gw_trans->gw_html_req,
1489 gw_trans->gw_dir->description);
1490 if (kerr != KCGI_OK)
1492 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1493 if (kerr != KCGI_OK)
1497 if (gw_trans->gw_conf->got_show_repo_owner &&
1498 gw_trans->gw_dir->owner != NULL &&
1499 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1500 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1501 KATTR_ID, "repo_owner_title", KATTR__MAX);
1502 if (kerr != KCGI_OK)
1504 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1505 if (kerr != KCGI_OK)
1507 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1508 if (kerr != KCGI_OK)
1510 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1511 KATTR_ID, "repo_owner", KATTR__MAX);
1512 if (kerr != KCGI_OK)
1514 kerr = khtml_puts(gw_trans->gw_html_req,
1515 gw_trans->gw_dir->owner);
1516 if (kerr != KCGI_OK)
1518 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1519 if (kerr != KCGI_OK)
1523 if (gw_trans->gw_conf->got_show_repo_age) {
1524 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1529 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1530 KATTR_ID, "last_change_title", KATTR__MAX);
1531 if (kerr != KCGI_OK)
1533 kerr = khtml_puts(gw_trans->gw_html_req,
1535 if (kerr != KCGI_OK)
1537 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1538 if (kerr != KCGI_OK)
1540 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1541 KATTR_ID, "last_change", KATTR__MAX);
1542 if (kerr != KCGI_OK)
1544 kerr = khtml_puts(gw_trans->gw_html_req, age);
1545 if (kerr != KCGI_OK)
1547 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1548 if (kerr != KCGI_OK)
1553 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1554 gw_trans->gw_dir->url != NULL &&
1555 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1556 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1557 KATTR_ID, "cloneurl_title", KATTR__MAX);
1558 if (kerr != KCGI_OK)
1560 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1561 if (kerr != KCGI_OK)
1563 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1564 if (kerr != KCGI_OK)
1566 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1567 KATTR_ID, "cloneurl", KATTR__MAX);
1568 if (kerr != KCGI_OK)
1570 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1571 if (kerr != KCGI_OK)
1573 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1574 if (kerr != KCGI_OK)
1578 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1579 if (kerr != KCGI_OK)
1582 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1583 "briefs_title_wrapper", KATTR__MAX);
1584 if (kerr != KCGI_OK)
1586 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1587 "briefs_title", KATTR__MAX);
1588 if (kerr != KCGI_OK)
1590 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1591 if (kerr != KCGI_OK)
1593 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1594 if (kerr != KCGI_OK)
1596 error = gw_briefs(gw_trans);
1600 error = gw_tags(gw_trans);
1604 error = gw_output_repo_heads(gw_trans);
1607 if (error == NULL && kerr != KCGI_OK)
1608 error = gw_kcgi_error(kerr);
1612 static const struct got_error *
1613 gw_tree(struct gw_trans *gw_trans)
1615 const struct got_error *error = NULL;
1616 struct gw_header *header = NULL;
1617 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1619 enum kcgi_err kerr = KCGI_OK;
1622 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1623 return got_error_from_errno("pledge");
1625 if ((header = gw_init_header()) == NULL)
1626 return got_error_from_errno("malloc");
1628 error = gw_apply_unveil(gw_trans->gw_dir->path);
1632 error = gw_get_header(gw_trans, header, 1);
1636 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1637 "tree_header_wrapper", KATTR__MAX);
1638 if (kerr != KCGI_OK)
1640 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1641 "tree_header", KATTR__MAX);
1642 if (kerr != KCGI_OK)
1644 error = gw_gen_tree_header(gw_trans, header->tree_id);
1647 error = gw_get_time_str(&age, header->committer_time,
1651 error = gw_gen_age_header(gw_trans, age ?age : "");
1654 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1657 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1658 if (kerr != KCGI_OK)
1660 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1661 "dotted_line", KATTR__MAX);
1662 if (kerr != KCGI_OK)
1664 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1665 if (kerr != KCGI_OK)
1668 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1669 "tree", KATTR__MAX);
1670 if (kerr != KCGI_OK)
1672 error = gw_output_repo_tree(gw_trans, header);
1676 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1678 gw_free_header(header);
1679 free(tree_html_disp);
1683 if (error == NULL && kerr != KCGI_OK)
1684 error = gw_kcgi_error(kerr);
1688 static const struct got_error *
1689 gw_tags(struct gw_trans *gw_trans)
1691 const struct got_error *error = NULL;
1692 struct gw_header *header = NULL;
1693 char *href_next = NULL, *href_prev = NULL;
1694 enum kcgi_err kerr = KCGI_OK;
1697 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1698 return got_error_from_errno("pledge");
1700 if ((header = gw_init_header()) == NULL)
1701 return got_error_from_errno("malloc");
1703 if (gw_trans->action != GW_SUMMARY) {
1704 error = gw_apply_unveil(gw_trans->gw_dir->path);
1709 error = gw_get_header(gw_trans, header, 1);
1713 if (gw_trans->action == GW_SUMMARY) {
1714 gw_trans->next_id = NULL;
1715 error = gw_output_repo_tags(gw_trans, header,
1716 D_MAXSLCOMMDISP, TAGBRIEF);
1720 error = gw_output_repo_tags(gw_trans, header,
1721 gw_trans->gw_conf->got_max_commits_display, TAGBRIEF);
1726 if (gw_trans->next_id || gw_trans->page > 0) {
1727 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1728 KATTR_ID, "np_wrapper", KATTR__MAX);
1729 if (kerr != KCGI_OK)
1731 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1732 KATTR_ID, "nav_prev", KATTR__MAX);
1733 if (kerr != KCGI_OK)
1737 if (gw_trans->prev_id) {
1738 href_prev = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1739 KATTRX_STRING, gw_trans->repo_name, "page",
1740 KATTRX_INT, (int64_t) (gw_trans->page - 1), "action",
1741 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1742 gw_trans->prev_id ? gw_trans->prev_id : "", NULL);
1743 if (href_prev == NULL) {
1744 error = got_error_from_errno("khttp_urlpartx");
1747 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1748 KATTR_HREF, href_prev, KATTR__MAX);
1749 if (kerr != KCGI_OK)
1751 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1752 if (kerr != KCGI_OK)
1754 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1755 if (kerr != KCGI_OK)
1759 if (gw_trans->next_id || gw_trans->page > 0) {
1760 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1761 if (kerr != KCGI_OK)
1762 return gw_kcgi_error(kerr);
1765 if (gw_trans->next_id) {
1766 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1767 KATTR_ID, "nav_next", KATTR__MAX);
1768 if (kerr != KCGI_OK)
1770 href_next = khttp_urlpartx(NULL, NULL, "gotweb", "path",
1771 KATTRX_STRING, gw_trans->repo_name, "page",
1772 KATTRX_INT, (int64_t) (gw_trans->page + 1), "action",
1773 KATTRX_STRING, "tags", "commit", KATTRX_STRING,
1774 gw_trans->next_id, NULL);
1775 if (href_next == NULL) {
1776 error = got_error_from_errno("khttp_urlpartx");
1779 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1780 KATTR_HREF, href_next, KATTR__MAX);
1781 if (kerr != KCGI_OK)
1783 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1784 if (kerr != KCGI_OK)
1786 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1787 if (kerr != KCGI_OK)
1791 if (gw_trans->next_id || gw_trans->page > 0) {
1792 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1793 if (kerr != KCGI_OK)
1797 gw_free_header(header);
1800 if (error == NULL && kerr != KCGI_OK)
1801 error = gw_kcgi_error(kerr);
1805 static const struct got_error *
1806 gw_tag(struct gw_trans *gw_trans)
1808 const struct got_error *error = NULL;
1809 struct gw_header *header = NULL;
1810 enum kcgi_err kerr = KCGI_OK;
1813 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1814 return got_error_from_errno("pledge");
1816 if ((header = gw_init_header()) == NULL)
1817 return got_error_from_errno("malloc");
1819 error = gw_apply_unveil(gw_trans->gw_dir->path);
1823 if (gw_trans->commit_id == NULL) {
1824 error = got_error_msg(GOT_ERR_QUERYSTRING,
1825 "commit required in querystring");
1829 error = gw_get_header(gw_trans, header, 1);
1833 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1834 "tag_header_wrapper", KATTR__MAX);
1835 if (kerr != KCGI_OK)
1837 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1838 "tag_header", KATTR__MAX);
1839 if (kerr != KCGI_OK)
1841 error = gw_gen_commit_header(gw_trans, header->commit_id,
1845 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1848 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1849 if (kerr != KCGI_OK)
1851 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1852 "dotted_line", KATTR__MAX);
1853 if (kerr != KCGI_OK)
1855 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1856 if (kerr != KCGI_OK)
1859 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1860 "tree", KATTR__MAX);
1861 if (kerr != KCGI_OK)
1864 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1868 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1870 gw_free_header(header);
1871 if (error == NULL && kerr != KCGI_OK)
1872 error = gw_kcgi_error(kerr);
1876 static const struct got_error *
1877 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1879 const struct got_error *error = NULL;
1884 if (asprintf(&dir_test, "%s/%s/%s",
1885 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1886 GOTWEB_GIT_DIR) == -1)
1887 return got_error_from_errno("asprintf");
1889 dt = opendir(dir_test);
1893 gw_dir->path = strdup(dir_test);
1894 if (gw_dir->path == NULL) {
1896 error = got_error_from_errno("strdup");
1903 if (asprintf(&dir_test, "%s/%s/%s",
1904 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1905 GOTWEB_GOT_DIR) == -1) {
1907 error = got_error_from_errno("asprintf");
1911 dt = opendir(dir_test);
1916 error = got_error(GOT_ERR_NOT_GIT_REPO);
1920 if (asprintf(&dir_test, "%s/%s",
1921 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1922 error = got_error_from_errno("asprintf");
1927 gw_dir->path = strdup(dir_test);
1928 if (gw_dir->path == NULL) {
1930 error = got_error_from_errno("strdup");
1934 dt = opendir(dir_test);
1936 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1941 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1945 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1948 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1952 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1956 if (dt && closedir(dt) == -1 && error == NULL)
1957 error = got_error_from_errno("closedir");
1961 static const struct got_error *
1962 gw_load_got_paths(struct gw_trans *gw_trans)
1964 const struct got_error *error = NULL;
1966 struct dirent **sd_dent;
1967 struct gw_dir *gw_dir;
1969 unsigned int d_cnt, d_i;
1971 d = opendir(gw_trans->gw_conf->got_repos_path);
1973 error = got_error_from_errno2("opendir",
1974 gw_trans->gw_conf->got_repos_path);
1978 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1981 error = got_error_from_errno2("scandir",
1982 gw_trans->gw_conf->got_repos_path);
1986 for (d_i = 0; d_i < d_cnt; d_i++) {
1987 if (gw_trans->gw_conf->got_max_repos > 0 &&
1988 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1989 break; /* account for parent and self */
1991 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1992 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1995 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1999 error = gw_load_got_path(gw_trans, gw_dir);
2000 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
2003 } else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
2006 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
2007 !got_path_dir_is_empty(gw_dir->path)) {
2008 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
2010 gw_trans->repos_total++;
2014 if (d && closedir(d) == -1 && error == NULL)
2015 error = got_error_from_errno("closedir");
2019 static const struct got_error *
2020 gw_parse_querystring(struct gw_trans *gw_trans)
2022 const struct got_error *error = NULL;
2024 struct gw_query_action *action = NULL;
2027 if (gw_trans->gw_req->fieldnmap[0]) {
2028 return got_error(GOT_ERR_QUERYSTRING);
2029 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
2030 /* define gw_trans->repo_path */
2031 gw_trans->repo_name = p->parsed.s;
2033 if (asprintf(&gw_trans->repo_path, "%s/%s",
2034 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
2035 return got_error_from_errno("asprintf");
2037 /* get action and set function */
2038 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
2039 for (i = 0; i < nitems(gw_query_funcs); i++) {
2040 action = &gw_query_funcs[i];
2041 if (action->func_name == NULL)
2043 if (strcmp(action->func_name,
2044 p->parsed.s) == 0) {
2045 gw_trans->action = i;
2050 if (gw_trans->action == -1) {
2051 gw_trans->action = GW_ERR;
2052 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
2053 p != NULL ? "bad action in querystring" :
2054 "no action in querystring");
2058 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
2059 if (asprintf(&gw_trans->commit_id, "%s",
2061 return got_error_from_errno("asprintf");
2064 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
2065 gw_trans->repo_file = p->parsed.s;
2067 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
2068 if (asprintf(&gw_trans->repo_folder, "%s",
2070 return got_error_from_errno("asprintf");
2073 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
2074 if (asprintf(&gw_trans->prev_id, "%s",
2076 return got_error_from_errno("asprintf");
2079 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
2080 gw_trans->headref = p->parsed.s;
2082 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
2086 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
2088 gw_trans->action = GW_INDEX;
2090 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
2091 gw_trans->page = p->parsed.i;
2096 static const struct got_error *
2097 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
2099 const struct got_error *error;
2101 *gw_dir = malloc(sizeof(**gw_dir));
2102 if (*gw_dir == NULL)
2103 return got_error_from_errno("malloc");
2105 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
2106 error = got_error_from_errno("asprintf");
2115 static const struct got_error *
2116 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
2118 enum kcgi_err kerr = KCGI_OK;
2120 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
2121 if (kerr != KCGI_OK)
2122 return gw_kcgi_error(kerr);
2123 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
2125 if (kerr != KCGI_OK)
2126 return gw_kcgi_error(kerr);
2127 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
2129 if (kerr != KCGI_OK)
2130 return gw_kcgi_error(kerr);
2131 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
2133 if (kerr != KCGI_OK)
2134 return gw_kcgi_error(kerr);
2135 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
2136 if (kerr != KCGI_OK)
2137 return gw_kcgi_error(kerr);
2138 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
2140 if (kerr != KCGI_OK)
2141 return gw_kcgi_error(kerr);
2143 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2144 kerr = khttp_head(gw_trans->gw_req,
2145 kresps[KRESP_CONTENT_DISPOSITION],
2146 "attachment; filename=%s", gw_trans->repo_file);
2147 if (kerr != KCGI_OK)
2148 return gw_kcgi_error(kerr);
2151 kerr = khttp_body(gw_trans->gw_req);
2152 return gw_kcgi_error(kerr);
2155 static const struct got_error *
2156 gw_display_index(struct gw_trans *gw_trans)
2158 const struct got_error *error;
2159 enum kcgi_err kerr = KCGI_OK;
2161 /* catch early querystring errors */
2162 if (gw_trans->error)
2163 gw_trans->action = GW_ERR;
2165 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2169 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2170 if (kerr != KCGI_OK)
2171 return gw_kcgi_error(kerr);
2173 if (gw_trans->action != GW_BLOB) {
2174 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2175 gw_query_funcs[gw_trans->action].template);
2176 if (kerr != KCGI_OK) {
2177 khtml_close(gw_trans->gw_html_req);
2178 return gw_kcgi_error(kerr);
2182 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2185 static const struct got_error *
2186 gw_error(struct gw_trans *gw_trans)
2188 enum kcgi_err kerr = KCGI_OK;
2190 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2192 return gw_kcgi_error(kerr);
2196 gw_template(size_t key, void *arg)
2198 const struct got_error *error = NULL;
2199 enum kcgi_err kerr = KCGI_OK;
2200 struct gw_trans *gw_trans = arg;
2201 char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
2202 char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
2204 if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
2205 "/apple-touch-icon.png") == -1)
2207 if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
2208 "/favicon-32x32.png") == -1)
2210 if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
2211 "/favicon-16x16.png") == -1)
2213 if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
2214 "/site.webmanifest") == -1)
2216 if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
2217 "/safari-pinned-tab.svg") == -1)
2219 if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
2220 "/gotweb.css") == -1)
2222 if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
2223 gw_trans->gw_conf->got_www_path ? "/" : "",
2224 gw_trans->gw_conf->got_logo) == -1)
2229 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2230 KATTR_NAME, "viewport",
2231 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2233 if (kerr != KCGI_OK)
2235 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2236 if (kerr != KCGI_OK)
2238 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2239 KATTR_CHARSET, "utf-8",
2241 if (kerr != KCGI_OK)
2243 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2244 if (kerr != KCGI_OK)
2246 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2247 KATTR_NAME, "msapplication-TileColor",
2248 KATTR_CONTENT, "#da532c", KATTR__MAX);
2249 if (kerr != KCGI_OK)
2251 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2252 if (kerr != KCGI_OK)
2254 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2255 KATTR_NAME, "theme-color",
2256 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2257 if (kerr != KCGI_OK)
2259 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2260 if (kerr != KCGI_OK)
2262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2263 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2264 KATTR_HREF, ati, KATTR__MAX);
2265 if (kerr != KCGI_OK)
2267 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2268 if (kerr != KCGI_OK)
2270 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2271 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2272 "32x32", KATTR_HREF, fic32, KATTR__MAX);
2273 if (kerr != KCGI_OK)
2275 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2276 if (kerr != KCGI_OK)
2278 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2279 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2280 "16x16", KATTR_HREF, fic16, KATTR__MAX);
2281 if (kerr != KCGI_OK)
2283 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2284 if (kerr != KCGI_OK)
2286 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2287 KATTR_REL, "manifest", KATTR_HREF, swm,
2289 if (kerr != KCGI_OK)
2291 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2292 if (kerr != KCGI_OK)
2294 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2295 KATTR_REL, "mask-icon", KATTR_HREF,
2297 if (kerr != KCGI_OK)
2299 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2300 if (kerr != KCGI_OK)
2302 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2303 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2304 KATTR_HREF, css, KATTR__MAX);
2305 if (kerr != KCGI_OK)
2307 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2308 if (kerr != KCGI_OK)
2312 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2313 KATTR_ID, "got_link", KATTR__MAX);
2314 if (kerr != KCGI_OK)
2316 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2317 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2318 KATTR_TARGET, "_sotd", KATTR__MAX);
2319 if (kerr != KCGI_OK)
2321 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2322 KATTR_SRC, logo, KATTR__MAX);
2323 if (kerr != KCGI_OK)
2325 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2326 if (kerr != KCGI_OK)
2329 case (TEMPL_SITEPATH):
2330 error = gw_output_site_link(gw_trans);
2335 if (gw_trans->gw_conf->got_site_name != NULL) {
2336 kerr = khtml_puts(gw_trans->gw_html_req,
2337 gw_trans->gw_conf->got_site_name);
2338 if (kerr != KCGI_OK)
2342 case (TEMPL_SEARCH):
2344 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2345 "search", KATTR__MAX);
2346 if (kerr != KCGI_OK)
2348 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2349 KATTR_METHOD, "POST", KATTR__MAX);
2350 if (kerr != KCGI_OK)
2352 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2353 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2354 KATTR_MAXLENGTH, "50", KATTR__MAX);
2355 if (kerr != KCGI_OK)
2357 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2359 if (kerr != KCGI_OK)
2361 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2362 if (kerr != KCGI_OK)
2364 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2365 if (kerr != KCGI_OK)
2368 case(TEMPL_SITEOWNER):
2369 if (gw_trans->gw_conf->got_site_owner != NULL &&
2370 gw_trans->gw_conf->got_show_site_owner) {
2371 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2372 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2373 if (kerr != KCGI_OK)
2375 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2376 KATTR_ID, "site_owner", KATTR__MAX);
2377 if (kerr != KCGI_OK)
2379 kerr = khtml_puts(gw_trans->gw_html_req,
2380 gw_trans->gw_conf->got_site_owner);
2381 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2382 if (kerr != KCGI_OK)
2386 case(TEMPL_CONTENT):
2387 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2390 KATTR_ID, "tmpl_err", KATTR__MAX);
2391 if (kerr != KCGI_OK)
2393 kerr = khttp_printf(gw_trans->gw_req, "Error: %s",
2395 if (kerr != KCGI_OK)
2397 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2398 if (kerr != KCGI_OK)
2424 static const struct got_error *
2425 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2427 const struct got_error *error = NULL;
2428 enum kcgi_err kerr = KCGI_OK;
2430 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2431 KATTR_ID, "header_commit_title", KATTR__MAX);
2432 if (kerr != KCGI_OK)
2434 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2435 if (kerr != KCGI_OK)
2437 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2438 if (kerr != KCGI_OK)
2440 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2441 KATTR_ID, "header_commit", KATTR__MAX);
2442 if (kerr != KCGI_OK)
2444 kerr = khtml_printf(gw_trans->gw_html_req, "%s ", str1);
2445 if (kerr != KCGI_OK)
2448 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2449 KATTR_ID, "refs_str", KATTR__MAX);
2450 if (kerr != KCGI_OK)
2452 kerr = khtml_printf(gw_trans->gw_html_req, "(%s)", str2);
2453 if (kerr != KCGI_OK)
2455 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2456 if (kerr != KCGI_OK)
2459 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2461 if (error == NULL && kerr != KCGI_OK)
2462 error = gw_kcgi_error(kerr);
2466 static const struct got_error *
2467 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2469 const struct got_error *error = NULL;
2470 enum kcgi_err kerr = KCGI_OK;
2472 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2473 KATTR_ID, "header_diff_title", KATTR__MAX);
2474 if (kerr != KCGI_OK)
2476 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2477 if (kerr != KCGI_OK)
2479 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2480 if (kerr != KCGI_OK)
2482 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2483 KATTR_ID, "header_diff", KATTR__MAX);
2484 if (kerr != KCGI_OK)
2487 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2488 if (kerr != KCGI_OK)
2491 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2492 if (kerr != KCGI_OK)
2494 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2495 if (kerr != KCGI_OK)
2497 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2499 if (error == NULL && kerr != KCGI_OK)
2500 error = gw_kcgi_error(kerr);
2504 static const struct got_error *
2505 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2507 const struct got_error *error = NULL;
2508 enum kcgi_err kerr = KCGI_OK;
2510 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2511 KATTR_ID, "header_age_title", KATTR__MAX);
2512 if (kerr != KCGI_OK)
2514 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2515 if (kerr != KCGI_OK)
2517 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2518 if (kerr != KCGI_OK)
2520 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2521 KATTR_ID, "header_age", KATTR__MAX);
2522 if (kerr != KCGI_OK)
2524 kerr = khtml_puts(gw_trans->gw_html_req, str);
2525 if (kerr != KCGI_OK)
2527 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2529 if (error == NULL && kerr != KCGI_OK)
2530 error = gw_kcgi_error(kerr);
2534 static const struct got_error *
2535 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2537 const struct got_error *error = NULL;
2538 enum kcgi_err kerr = KCGI_OK;
2540 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2541 KATTR_ID, "header_author_title", KATTR__MAX);
2542 if (kerr != KCGI_OK)
2544 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2545 if (kerr != KCGI_OK)
2547 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2548 if (kerr != KCGI_OK)
2550 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2551 KATTR_ID, "header_author", KATTR__MAX);
2552 if (kerr != KCGI_OK)
2554 kerr = khtml_puts(gw_trans->gw_html_req, str);
2555 if (kerr != KCGI_OK)
2557 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2559 if (error == NULL && kerr != KCGI_OK)
2560 error = gw_kcgi_error(kerr);
2564 static const struct got_error *
2565 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2567 const struct got_error *error = NULL;
2568 enum kcgi_err kerr = KCGI_OK;
2570 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2571 KATTR_ID, "header_committer_title", KATTR__MAX);
2572 if (kerr != KCGI_OK)
2574 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2575 if (kerr != KCGI_OK)
2577 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2578 if (kerr != KCGI_OK)
2580 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2581 KATTR_ID, "header_committer", KATTR__MAX);
2582 if (kerr != KCGI_OK)
2584 kerr = khtml_puts(gw_trans->gw_html_req, str);
2585 if (kerr != KCGI_OK)
2587 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2589 if (error == NULL && kerr != KCGI_OK)
2590 error = gw_kcgi_error(kerr);
2594 static const struct got_error *
2595 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2597 const struct got_error *error = NULL;
2598 enum kcgi_err kerr = KCGI_OK;
2600 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2601 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2602 if (kerr != KCGI_OK)
2604 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2605 if (kerr != KCGI_OK)
2607 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2608 if (kerr != KCGI_OK)
2610 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2611 KATTR_ID, "header_commit_msg", KATTR__MAX);
2612 if (kerr != KCGI_OK)
2614 kerr = khttp_puts(gw_trans->gw_req, str);
2615 if (kerr != KCGI_OK)
2617 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2619 if (error == NULL && kerr != KCGI_OK)
2620 error = gw_kcgi_error(kerr);
2624 static const struct got_error *
2625 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2627 const struct got_error *error = NULL;
2628 enum kcgi_err kerr = KCGI_OK;
2630 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2631 KATTR_ID, "header_tree_title", KATTR__MAX);
2632 if (kerr != KCGI_OK)
2634 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2635 if (kerr != KCGI_OK)
2637 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2638 if (kerr != KCGI_OK)
2640 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2641 KATTR_ID, "header_tree", KATTR__MAX);
2642 if (kerr != KCGI_OK)
2644 kerr = khtml_puts(gw_trans->gw_html_req, str);
2645 if (kerr != KCGI_OK)
2647 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2649 if (error == NULL && kerr != KCGI_OK)
2650 error = gw_kcgi_error(kerr);
2654 static const struct got_error *
2655 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2658 const struct got_error *error = NULL;
2660 char *d_file = NULL;
2664 *description = NULL;
2665 if (gw_trans->gw_conf->got_show_repo_description == 0)
2668 if (asprintf(&d_file, "%s/description", dir) == -1)
2669 return got_error_from_errno("asprintf");
2671 f = fopen(d_file, "re");
2673 if (errno == ENOENT || errno == EACCES)
2675 error = got_error_from_errno2("fopen", d_file);
2679 if (fseek(f, 0, SEEK_END) == -1) {
2680 error = got_ferror(f, GOT_ERR_IO);
2685 error = got_ferror(f, GOT_ERR_IO);
2688 if (fseek(f, 0, SEEK_SET) == -1) {
2689 error = got_ferror(f, GOT_ERR_IO);
2692 *description = calloc(len + 1, sizeof(**description));
2693 if (*description == NULL) {
2694 error = got_error_from_errno("calloc");
2698 n = fread(*description, 1, len, f);
2699 if (n == 0 && ferror(f))
2700 error = got_ferror(f, GOT_ERR_IO);
2702 if (f != NULL && fclose(f) == EOF && error == NULL)
2703 error = got_error_from_errno("fclose");
2708 static const struct got_error *
2709 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2713 char *years = "years ago", *months = "months ago";
2714 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2715 char *minutes = "minutes ago", *seconds = "seconds ago";
2716 char *now = "right now";
2724 diff_time = time(NULL) - committer_time;
2725 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2726 if (asprintf(repo_age, "%lld %s",
2727 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2728 return got_error_from_errno("asprintf");
2729 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2730 if (asprintf(repo_age, "%lld %s",
2731 (diff_time / 60 / 60 / 24 / (365 / 12)),
2733 return got_error_from_errno("asprintf");
2734 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2735 if (asprintf(repo_age, "%lld %s",
2736 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2737 return got_error_from_errno("asprintf");
2738 } else if (diff_time > 60 * 60 * 24 * 2) {
2739 if (asprintf(repo_age, "%lld %s",
2740 (diff_time / 60 / 60 / 24), days) == -1)
2741 return got_error_from_errno("asprintf");
2742 } else if (diff_time > 60 * 60 * 2) {
2743 if (asprintf(repo_age, "%lld %s",
2744 (diff_time / 60 / 60), hours) == -1)
2745 return got_error_from_errno("asprintf");
2746 } else if (diff_time > 60 * 2) {
2747 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2749 return got_error_from_errno("asprintf");
2750 } else if (diff_time > 2) {
2751 if (asprintf(repo_age, "%lld %s", diff_time,
2753 return got_error_from_errno("asprintf");
2755 if (asprintf(repo_age, "%s", now) == -1)
2756 return got_error_from_errno("asprintf");
2760 if (gmtime_r(&committer_time, &tm) == NULL)
2761 return got_error_from_errno("gmtime_r");
2763 s = asctime_r(&tm, datebuf);
2765 return got_error_from_errno("asctime_r");
2767 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2768 return got_error_from_errno("asprintf");
2774 static const struct got_error *
2775 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2776 const char *refname, int ref_tm)
2778 const struct got_error *error = NULL;
2779 struct got_repository *repo = NULL;
2780 struct got_commit_object *commit = NULL;
2781 struct got_reflist_head refs;
2782 struct got_reflist_entry *re;
2783 time_t committer_time = 0, cmp_time = 0;
2788 if (gw_trans->gw_conf->got_show_repo_age == 0)
2792 repo = gw_trans->repo;
2794 error = got_repo_open(&repo, dir, NULL);
2799 error = got_ref_list(&refs, repo, "refs/heads",
2800 got_ref_cmp_by_name, NULL);
2805 * Find the youngest branch tip in the repository, or the age of
2806 * the a specific branch tip if a name was provided by the caller.
2808 TAILQ_FOREACH(re, &refs, entry) {
2809 struct got_object_id *id = NULL;
2811 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2814 error = got_ref_resolve(&id, repo, re->ref);
2818 error = got_object_open_as_commit(&commit, repo, id);
2824 got_object_commit_get_committer_time(commit);
2825 got_object_commit_close(commit);
2826 if (cmp_time < committer_time)
2827 cmp_time = committer_time;
2833 if (cmp_time != 0) {
2834 committer_time = cmp_time;
2835 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2838 got_ref_list_free(&refs);
2839 if (gw_trans->repo == NULL) {
2840 const struct got_error *close_err = got_repo_close(repo);
2847 static const struct got_error *
2848 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2850 const struct got_error *error;
2852 struct got_object_id *id1 = NULL, *id2 = NULL;
2853 char *label1 = NULL, *label2 = NULL, *line = NULL;
2855 size_t linesize = 0;
2857 enum kcgi_err kerr = KCGI_OK;
2863 if (header->parent_id != NULL &&
2864 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2865 error = got_repo_match_object_id(&id1, &label1,
2866 header->parent_id, GOT_OBJ_TYPE_ANY,
2867 &header->refs, gw_trans->repo);
2872 error = got_repo_match_object_id(&id2, &label2,
2873 header->commit_id, GOT_OBJ_TYPE_ANY, &header->refs,
2878 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2882 case GOT_OBJ_TYPE_BLOB:
2883 error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
2884 NULL, NULL, 3, 0, 0, gw_trans->repo, f);
2886 case GOT_OBJ_TYPE_TREE:
2887 error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
2888 NULL, "", "", 3, 0, 0, gw_trans->repo, f);
2890 case GOT_OBJ_TYPE_COMMIT:
2891 error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
2892 NULL, 3, 0, 0, gw_trans->repo, f);
2895 error = got_error(GOT_ERR_OBJ_TYPE);
2900 if (fseek(f, 0, SEEK_SET) == -1) {
2901 error = got_ferror(f, GOT_ERR_IO);
2905 while ((linelen = getline(&line, &linesize, f)) != -1) {
2906 error = gw_colordiff_line(gw_trans, line);
2909 /* XXX: KHTML_PRETTY breaks this */
2910 kerr = khtml_puts(gw_trans->gw_html_req, line);
2911 if (kerr != KCGI_OK)
2913 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2914 if (kerr != KCGI_OK)
2917 if (linelen == -1 && ferror(f))
2918 error = got_error_from_errno("getline");
2920 if (f && fclose(f) == EOF && error == NULL)
2921 error = got_error_from_errno("fclose");
2928 if (error == NULL && kerr != KCGI_OK)
2929 error = gw_kcgi_error(kerr);
2933 static const struct got_error *
2934 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2936 const struct got_error *error = NULL, *close_err;
2937 struct got_repository *repo;
2938 const char *gitconfig_owner;
2942 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2945 error = got_repo_open(&repo, dir, NULL);
2948 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2949 if (gitconfig_owner) {
2950 *owner = strdup(gitconfig_owner);
2952 error = got_error_from_errno("strdup");
2954 close_err = got_repo_close(repo);
2960 static const struct got_error *
2961 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2963 const struct got_error *error = NULL;
2965 char *d_file = NULL;
2971 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2972 return got_error_from_errno("asprintf");
2974 f = fopen(d_file, "re");
2976 if (errno != ENOENT && errno != EACCES)
2977 error = got_error_from_errno2("fopen", d_file);
2981 if (fseek(f, 0, SEEK_END) == -1) {
2982 error = got_ferror(f, GOT_ERR_IO);
2987 error = got_ferror(f, GOT_ERR_IO);
2990 if (fseek(f, 0, SEEK_SET) == -1) {
2991 error = got_ferror(f, GOT_ERR_IO);
2995 *url = calloc(len + 1, sizeof(**url));
2997 error = got_error_from_errno("calloc");
3001 n = fread(*url, 1, len, f);
3002 if (n == 0 && ferror(f))
3003 error = got_ferror(f, GOT_ERR_IO);
3005 if (f && fclose(f) == EOF && error == NULL)
3006 error = got_error_from_errno("fclose");
3011 static const struct got_error *
3012 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
3013 int limit, int tag_type)
3015 const struct got_error *error = NULL;
3016 struct got_reflist_head refs;
3017 struct got_reflist_entry *re;
3019 char *id_str = NULL, *newline, *href_commits = NULL;
3020 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
3021 struct got_tag_object *tag = NULL;
3022 enum kcgi_err kerr = KCGI_OK;
3023 int summary_header_displayed = 0, chk_next = 0;
3024 int tag_count = 0, commit_found = 0, c_cnt = 0;
3028 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
3029 got_ref_cmp_tags, gw_trans->repo);
3033 TAILQ_FOREACH(re, &refs, entry) {
3034 const char *refname;
3036 const char *tag_commit;
3038 struct got_object_id *id;
3039 struct got_commit_object *commit = NULL;
3041 refname = got_ref_get_name(re->ref);
3042 if (strncmp(refname, "refs/tags/", 10) != 0)
3046 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3050 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3052 if (error->code != GOT_ERR_OBJ_TYPE) {
3056 /* "lightweight" tag */
3057 error = got_object_open_as_commit(&commit,
3058 gw_trans->repo, id);
3063 tagger = got_object_commit_get_committer(commit);
3065 got_object_commit_get_committer_time(commit);
3066 error = got_object_id_str(&id_str, id);
3070 tagger = got_object_tag_get_tagger(tag);
3071 tagger_time = got_object_tag_get_tagger_time(tag);
3072 error = got_object_id_str(&id_str,
3073 got_object_tag_get_object_id(tag));
3078 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3079 strlen(id_str)) != 0)
3082 if (tag_type == TAGBRIEF && gw_trans->commit_id &&
3083 commit_found == 0 && strncmp(id_str, gw_trans->commit_id,
3084 strlen(id_str)) != 0)
3092 gw_trans->next_id = strdup(id_str);
3093 if (gw_trans->next_id == NULL)
3094 error = got_error_from_errno("strdup");
3099 error = got_object_commit_get_logmsg(&tag_commit0,
3103 got_object_commit_close(commit);
3105 tag_commit0 = strdup(got_object_tag_get_message(tag));
3106 if (tag_commit0 == NULL) {
3107 error = got_error_from_errno("strdup");
3112 tag_commit = tag_commit0;
3113 while (*tag_commit == '\n')
3118 newline = strchr(tag_commit, '\n');
3122 if (summary_header_displayed == 0) {
3123 kerr = khtml_attr(gw_trans->gw_html_req,
3124 KELEM_DIV, KATTR_ID,
3125 "summary_tags_title_wrapper", KATTR__MAX);
3126 if (kerr != KCGI_OK)
3128 kerr = khtml_attr(gw_trans->gw_html_req,
3129 KELEM_DIV, KATTR_ID,
3130 "summary_tags_title", KATTR__MAX);
3131 if (kerr != KCGI_OK)
3133 kerr = khtml_puts(gw_trans->gw_html_req,
3135 if (kerr != KCGI_OK)
3137 kerr = khtml_closeelem(gw_trans->gw_html_req,
3139 if (kerr != KCGI_OK)
3141 kerr = khtml_attr(gw_trans->gw_html_req,
3142 KELEM_DIV, KATTR_ID,
3143 "summary_tags_content", KATTR__MAX);
3144 if (kerr != KCGI_OK)
3146 summary_header_displayed = 1;
3149 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3150 KATTR_ID, "tag_wrapper", KATTR__MAX);
3151 if (kerr != KCGI_OK)
3153 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3154 KATTR_ID, "tag_age", KATTR__MAX);
3155 if (kerr != KCGI_OK)
3157 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
3160 kerr = khtml_puts(gw_trans->gw_html_req,
3162 if (kerr != KCGI_OK)
3164 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3165 if (kerr != KCGI_OK)
3167 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3168 KATTR_ID, "tag", KATTR__MAX);
3169 if (kerr != KCGI_OK)
3171 kerr = khtml_puts(gw_trans->gw_html_req, refname);
3172 if (kerr != KCGI_OK)
3174 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3175 if (kerr != KCGI_OK)
3177 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3178 KATTR_ID, "tag_name", KATTR__MAX);
3179 if (kerr != KCGI_OK)
3182 href_tag = khttp_urlpart(NULL, NULL, "gotweb", "path",
3183 gw_trans->repo_name, "action", "tag", "commit",
3185 if (href_tag == NULL) {
3186 error = got_error_from_errno("khttp_urlpart");
3189 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3190 KATTR_HREF, href_tag, KATTR__MAX);
3191 if (kerr != KCGI_OK)
3193 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
3194 if (kerr != KCGI_OK)
3196 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3197 if (kerr != KCGI_OK)
3200 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3201 KATTR_ID, "navs_wrapper", KATTR__MAX);
3202 if (kerr != KCGI_OK)
3204 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3205 KATTR_ID, "navs", KATTR__MAX);
3206 if (kerr != KCGI_OK)
3209 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3210 KATTR_HREF, href_tag, KATTR__MAX);
3211 if (kerr != KCGI_OK)
3213 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3214 if (kerr != KCGI_OK)
3216 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3217 if (kerr != KCGI_OK)
3220 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3221 if (kerr != KCGI_OK)
3224 href_briefs = khttp_urlpart(NULL, NULL, "gotweb",
3225 "path", gw_trans->repo_name, "action", "briefs",
3226 "commit", id_str, NULL);
3227 if (href_briefs == NULL) {
3228 error = got_error_from_errno("khttp_urlpart");
3231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3232 KATTR_HREF, href_briefs, KATTR__MAX);
3233 if (kerr != KCGI_OK)
3235 kerr = khtml_puts(gw_trans->gw_html_req,
3237 if (kerr != KCGI_OK)
3239 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3240 if (kerr != KCGI_OK)
3243 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3244 if (kerr != KCGI_OK)
3247 href_commits = khttp_urlpart(NULL, NULL, "gotweb",
3248 "path", gw_trans->repo_name, "action", "commits",
3249 "commit", id_str, NULL);
3250 if (href_commits == NULL) {
3251 error = got_error_from_errno("khttp_urlpart");
3254 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3255 KATTR_HREF, href_commits, KATTR__MAX);
3256 if (kerr != KCGI_OK)
3258 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
3259 if (kerr != KCGI_OK)
3261 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3262 if (kerr != KCGI_OK)
3265 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3266 KATTR_ID, "dotted_line", KATTR__MAX);
3267 if (kerr != KCGI_OK)
3269 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3270 if (kerr != KCGI_OK)
3274 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3275 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3276 if (kerr != KCGI_OK)
3278 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3279 if (kerr != KCGI_OK)
3281 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3282 if (kerr != KCGI_OK)
3284 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3285 KATTR_ID, "tag_info_date", KATTR__MAX);
3286 if (kerr != KCGI_OK)
3288 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3291 kerr = khtml_puts(gw_trans->gw_html_req,
3293 if (kerr != KCGI_OK)
3295 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3296 if (kerr != KCGI_OK)
3299 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3300 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3301 if (kerr != KCGI_OK)
3303 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3304 if (kerr != KCGI_OK)
3306 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3307 if (kerr != KCGI_OK)
3309 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3310 KATTR_ID, "tag_info_date", KATTR__MAX);
3311 if (kerr != KCGI_OK)
3313 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3314 if (kerr != KCGI_OK)
3316 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3317 if (kerr != KCGI_OK)
3320 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3321 KATTR_ID, "tag_info", KATTR__MAX);
3322 if (kerr != KCGI_OK)
3324 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3325 if (kerr != KCGI_OK)
3331 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3332 if (kerr != KCGI_OK)
3335 if (limit && --limit == 0)
3339 got_object_tag_close(tag);
3352 href_commits = NULL;
3354 if (tag_count == 0) {
3355 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3356 "summary_tags_title_wrapper", KATTR__MAX);
3357 if (kerr != KCGI_OK)
3359 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3360 "summary_tags_title", KATTR__MAX);
3361 if (kerr != KCGI_OK)
3363 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
3364 if (kerr != KCGI_OK)
3366 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3367 if (kerr != KCGI_OK)
3369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3370 "summary_tags_content", KATTR__MAX);
3371 if (kerr != KCGI_OK)
3373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3374 "tags_info", KATTR__MAX);
3375 if (kerr != KCGI_OK)
3377 kerr = khttp_puts(gw_trans->gw_req,
3378 "There are no tags for this repo.");
3379 if (kerr != KCGI_OK)
3381 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3386 TAILQ_FOREACH_REVERSE(re, &refs, got_reflist_head, entry) {
3387 const char *refname;
3390 struct got_object_id *id;
3391 struct got_commit_object *commit = NULL;
3393 refname = got_ref_get_name(re->ref);
3394 if (strncmp(refname, "refs/tags/", 10) != 0)
3398 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
3402 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
3404 if (error->code != GOT_ERR_OBJ_TYPE) {
3408 /* "lightweight" tag */
3409 error = got_object_open_as_commit(&commit,
3410 gw_trans->repo, id);
3415 tagger = got_object_commit_get_committer(commit);
3417 got_object_commit_get_committer_time(commit);
3418 error = got_object_id_str(&id_str, id);
3422 tagger = got_object_tag_get_tagger(tag);
3423 tagger_time = got_object_tag_get_tagger_time(tag);
3424 error = got_object_id_str(&id_str,
3425 got_object_tag_get_object_id(tag));
3430 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
3431 strlen(id_str)) != 0)
3434 if (commit_found == 0 && tag_type == TAGBRIEF &&
3435 gw_trans->commit_id != NULL &&
3436 strncmp(id_str, gw_trans->commit_id, strlen(id_str)) != 0)
3441 if (gw_trans->commit_id != NULL &&
3442 strcmp(id_str, gw_trans->commit_id) != 0 &&
3443 (re == TAILQ_FIRST(&refs) ||
3444 c_cnt == gw_trans->gw_conf->got_max_commits_display)) {
3445 gw_trans->prev_id = strdup(id_str);
3446 if (gw_trans->prev_id == NULL) {
3447 error = got_error_from_errno("strdup");
3456 got_object_tag_close(tag);
3463 got_ref_list_free(&refs);
3464 if (error == NULL && kerr != KCGI_OK)
3465 error = gw_kcgi_error(kerr);
3470 gw_free_header(struct gw_header *header)
3473 free(header->author);
3474 free(header->committer);
3475 free(header->refs_str);
3476 free(header->commit_id);
3477 free(header->parent_id);
3478 free(header->tree_id);
3479 free(header->commit_msg);
3482 static struct gw_header *
3485 struct gw_header *header;
3487 header = malloc(sizeof(*header));
3491 header->path = NULL;
3492 TAILQ_INIT(&header->refs);
3494 header->refs_str = NULL;
3495 header->commit_id = NULL;
3496 header->committer = NULL;
3497 header->author = NULL;
3498 header->parent_id = NULL;
3499 header->tree_id = NULL;
3500 header->commit_msg = NULL;
3505 static const struct got_error *
3506 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3507 int limit, struct got_object_id *id)
3509 const struct got_error *error = NULL;
3510 struct got_commit_graph *graph = NULL;
3511 struct got_commit_object *commit = NULL;
3512 int chk_next = 0, chk_multi = 0, c_cnt = 0, commit_found = 0;
3513 struct gw_header *t_header = NULL;
3515 error = got_commit_graph_open(&graph, header->path, 0);
3519 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3525 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3528 if (error->code == GOT_ERR_ITER_COMPLETED)
3535 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3538 if (limit == 1 && chk_multi == 0 &&
3539 gw_trans->gw_conf->got_max_commits_display != 1) {
3540 error = gw_get_commit(gw_trans, header, commit, id);
3546 struct gw_header *n_header = NULL;
3547 if ((n_header = gw_init_header()) == NULL) {
3548 error = got_error_from_errno("malloc");
3551 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3553 error = got_ref_list(&n_header->refs, gw_trans->repo,
3554 NULL, got_ref_cmp_by_name, NULL);
3558 error = gw_get_commit(gw_trans, n_header, commit, id);
3561 got_ref_list_free(&n_header->refs);
3563 if (gw_trans->commit_id != NULL) {
3564 if (strcmp(gw_trans->commit_id,
3565 n_header->commit_id) == 0)
3571 * check for one more commit before breaking,
3572 * so we know whether to navicate through gw_briefs
3573 * gw_commits and gw_summary
3575 if (chk_next && (gw_trans->action == GW_BRIEFS ||
3576 gw_trans->action == GW_COMMITS ||
3577 gw_trans->action == GW_SUMMARY)) {
3578 gw_trans->next_id = strdup(n_header->commit_id);
3579 if (gw_trans->next_id == NULL)
3580 error = got_error_from_errno("strdup");
3581 TAILQ_REMOVE(&gw_trans->gw_headers, n_header,
3587 if (commit_found == 1 && (error || (limit && --limit == 0))) {
3594 if (gw_trans->prev_id == NULL && gw_trans->commit_id != NULL &&
3595 (gw_trans->action == GW_BRIEFS || gw_trans->action == GW_COMMITS)) {
3597 TAILQ_FOREACH_REVERSE(t_header, &gw_trans->gw_headers,
3599 if (commit_found == 0 &&
3600 strcmp(gw_trans->commit_id,
3601 t_header->commit_id) != 0)
3605 if (gw_trans->commit_id != NULL &&
3606 strcmp(gw_trans->commit_id,
3607 t_header->commit_id) != 0 &&
3608 (c_cnt == gw_trans->gw_conf->got_max_commits_display
3610 TAILQ_FIRST(&gw_trans->gw_headers))) {
3611 gw_trans->prev_id = strdup(t_header->commit_id);
3612 if (gw_trans->prev_id == NULL)
3613 error = got_error_from_errno("strdup");
3621 got_object_commit_close(commit);
3623 got_commit_graph_close(graph);
3627 static const struct got_error *
3628 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3629 struct got_commit_object *commit, struct got_object_id *id)
3631 const struct got_error *error = NULL;
3632 struct got_reflist_entry *re;
3633 struct got_object_id *id2 = NULL;
3634 struct got_object_qid *parent_id;
3635 char *commit_msg = NULL, *commit_msg0;
3638 TAILQ_FOREACH(re, &header->refs, entry) {
3641 struct got_tag_object *tag = NULL;
3642 struct got_object_id *ref_id;
3645 if (got_ref_is_symbolic(re->ref))
3648 name = got_ref_get_name(re->ref);
3649 if (strncmp(name, "refs/", 5) == 0)
3651 if (strncmp(name, "got/", 4) == 0)
3653 if (strncmp(name, "heads/", 6) == 0)
3655 if (strncmp(name, "remotes/", 8) == 0) {
3657 s = strstr(name, "/" GOT_REF_HEAD);
3658 if (s != NULL && s[strlen(s)] == '\0')
3661 error = got_ref_resolve(&ref_id, gw_trans->repo, re->ref);
3664 if (strncmp(name, "tags/", 5) == 0) {
3665 error = got_object_open_as_tag(&tag, gw_trans->repo,
3668 if (error->code != GOT_ERR_OBJ_TYPE) {
3673 * Ref points at something other
3680 cmp = got_object_id_cmp(tag ?
3681 got_object_tag_get_object_id(tag) : ref_id, id);
3684 got_object_tag_close(tag);
3687 s = header->refs_str;
3688 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3689 s ? ", " : "", name) == -1) {
3690 error = got_error_from_errno("asprintf");
3692 header->refs_str = NULL;
3698 error = got_object_id_str(&header->commit_id, id);
3702 error = got_object_id_str(&header->tree_id,
3703 got_object_commit_get_tree_id(commit));
3707 if (gw_trans->action == GW_DIFF) {
3708 parent_id = STAILQ_FIRST(
3709 got_object_commit_get_parent_ids(commit));
3710 if (parent_id != NULL) {
3711 id2 = got_object_id_dup(parent_id->id);
3713 error = got_object_id_str(&header->parent_id, id2);
3718 header->parent_id = strdup("/dev/null");
3719 if (header->parent_id == NULL) {
3720 error = got_error_from_errno("strdup");
3726 header->committer_time =
3727 got_object_commit_get_committer_time(commit);
3730 strdup(got_object_commit_get_author(commit));
3731 if (header->author == NULL) {
3732 error = got_error_from_errno("strdup");
3736 strdup(got_object_commit_get_committer(commit));
3737 if (header->committer == NULL) {
3738 error = got_error_from_errno("strdup");
3741 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3745 commit_msg = commit_msg0;
3746 while (*commit_msg == '\n')
3749 header->commit_msg = strdup(commit_msg);
3750 if (header->commit_msg == NULL)
3751 error = got_error_from_errno("strdup");
3756 static const struct got_error *
3757 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3759 const struct got_error *error = NULL;
3760 char *in_repo_path = NULL;
3761 struct got_object_id *id = NULL;
3762 struct got_reference *ref;
3764 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3768 if (gw_trans->commit_id == NULL || gw_trans->action == GW_COMMITS ||
3769 gw_trans->action == GW_BRIEFS || gw_trans->action == GW_SUMMARY ||
3770 gw_trans->action == GW_TAGS) {
3771 error = got_ref_open(&ref, gw_trans->repo,
3772 gw_trans->headref, 0);
3776 error = got_ref_resolve(&id, gw_trans->repo, ref);
3781 error = got_ref_open(&ref, gw_trans->repo,
3782 gw_trans->commit_id, 0);
3783 if (error == NULL) {
3785 error = got_ref_resolve(&id, gw_trans->repo, ref);
3789 error = got_object_get_type(&obj_type, gw_trans->repo,
3793 if (obj_type == GOT_OBJ_TYPE_TAG) {
3794 struct got_tag_object *tag;
3795 error = got_object_open_as_tag(&tag,
3796 gw_trans->repo, id);
3799 if (got_object_tag_get_object_type(tag) !=
3800 GOT_OBJ_TYPE_COMMIT) {
3801 got_object_tag_close(tag);
3802 error = got_error(GOT_ERR_OBJ_TYPE);
3806 id = got_object_id_dup(
3807 got_object_tag_get_object_id(tag));
3809 error = got_error_from_errno(
3810 "got_object_id_dup");
3811 got_object_tag_close(tag);
3814 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3815 error = got_error(GOT_ERR_OBJ_TYPE);
3819 error = got_repo_match_object_id_prefix(&id,
3820 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3826 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3827 gw_trans->repo_path);
3832 header->path = strdup(in_repo_path);
3833 if (header->path == NULL) {
3834 error = got_error_from_errno("strdup");
3839 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3840 got_ref_cmp_by_name, NULL);
3844 error = gw_get_commits(gw_trans, header, limit, id);
3846 got_ref_list_free(&header->refs);
3856 char datebuf[11]; /* YYYY-MM-DD + NUL */
3859 struct gw_blame_cb_args {
3860 struct blame_line *lines;
3864 off_t *line_offsets;
3866 struct got_repository *repo;
3867 struct gw_trans *gw_trans;
3870 static const struct got_error *
3871 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3873 const struct got_error *err = NULL;
3874 struct gw_blame_cb_args *a = arg;
3875 struct blame_line *bline;
3877 size_t linesize = 0;
3878 struct got_commit_object *commit = NULL;
3881 time_t committer_time;
3882 enum kcgi_err kerr = KCGI_OK;
3884 if (nlines != a->nlines ||
3885 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3886 return got_error(GOT_ERR_RANGE);
3889 return NULL; /* no change in this commit */
3891 /* Annotate this line. */
3892 bline = &a->lines[lineno - 1];
3893 if (bline->annotated)
3895 err = got_object_id_str(&bline->id_str, id);
3899 err = got_object_open_as_commit(&commit, a->repo, id);
3903 bline->committer = strdup(got_object_commit_get_committer(commit));
3904 if (bline->committer == NULL) {
3905 err = got_error_from_errno("strdup");
3909 committer_time = got_object_commit_get_committer_time(commit);
3910 if (gmtime_r(&committer_time, &tm) == NULL)
3911 return got_error_from_errno("gmtime_r");
3912 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3914 err = got_error(GOT_ERR_NO_SPACE);
3917 bline->annotated = 1;
3919 /* Print lines annotated so far. */
3920 bline = &a->lines[a->lineno_cur - 1];
3921 if (!bline->annotated)
3924 offset = a->line_offsets[a->lineno_cur - 1];
3925 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3926 err = got_error_from_errno("fseeko");
3930 while (bline->annotated) {
3931 char *smallerthan, *at, *nl, *committer;
3932 char *href_diff = NULL;
3935 if (getline(&line, &linesize, a->f) == -1) {
3937 err = got_error_from_errno("getline");
3941 committer = bline->committer;
3942 smallerthan = strchr(committer, '<');
3943 if (smallerthan && smallerthan[1] != '\0')
3944 committer = smallerthan + 1;
3945 at = strchr(committer, '@');
3948 len = strlen(committer);
3950 committer[8] = '\0';
3952 nl = strchr(line, '\n');
3956 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3957 "blame_wrapper", KATTR__MAX);
3958 if (kerr != KCGI_OK)
3960 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3961 "blame_number", KATTR__MAX);
3962 if (kerr != KCGI_OK)
3964 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.*d",
3965 a->nlines_prec, a->lineno_cur);
3966 if (kerr != KCGI_OK)
3968 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3969 if (kerr != KCGI_OK)
3972 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3973 "blame_hash", KATTR__MAX);
3974 if (kerr != KCGI_OK)
3977 href_diff = khttp_urlpart(NULL, NULL, "gotweb", "path",
3978 a->gw_trans->repo_name, "action", "diff", "commit",
3979 bline->id_str, NULL);
3980 if (href_diff == NULL) {
3981 err = got_error_from_errno("khttp_urlpart");
3984 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3985 KATTR_HREF, href_diff, KATTR__MAX);
3986 if (kerr != KCGI_OK)
3988 kerr = khtml_printf(a->gw_trans->gw_html_req, "%.8s",
3990 if (kerr != KCGI_OK)
3992 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3993 if (kerr != KCGI_OK)
3996 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3997 "blame_date", KATTR__MAX);
3998 if (kerr != KCGI_OK)
4000 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
4001 if (kerr != KCGI_OK)
4003 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4004 if (kerr != KCGI_OK)
4007 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4008 "blame_author", KATTR__MAX);
4009 if (kerr != KCGI_OK)
4011 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
4012 if (kerr != KCGI_OK)
4014 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4015 if (kerr != KCGI_OK)
4018 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4019 "blame_code", KATTR__MAX);
4020 if (kerr != KCGI_OK)
4022 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
4023 if (kerr != KCGI_OK)
4025 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4026 if (kerr != KCGI_OK)
4029 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
4030 if (kerr != KCGI_OK)
4034 bline = &a->lines[a->lineno_cur - 1];
4040 got_object_commit_close(commit);
4042 if (err == NULL && kerr != KCGI_OK)
4043 err = gw_kcgi_error(kerr);
4047 static const struct got_error *
4048 gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
4050 const struct got_error *error = NULL;
4051 struct got_object_id *obj_id = NULL;
4052 struct got_object_id *commit_id = NULL;
4053 struct got_blob_object *blob = NULL;
4054 char *path = NULL, *in_repo_path = NULL;
4055 struct gw_blame_cb_args bca;
4059 if (asprintf(&path, "%s%s%s",
4060 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4061 gw_trans->repo_folder ? "/" : "",
4062 gw_trans->repo_file) == -1) {
4063 error = got_error_from_errno("asprintf");
4067 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path);
4071 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
4072 GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
4076 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
4081 if (obj_id == NULL) {
4082 error = got_error(GOT_ERR_NO_OBJ);
4086 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4090 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4091 error = got_error(GOT_ERR_OBJ_TYPE);
4095 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4099 bca.f = got_opentemp();
4100 if (bca.f == NULL) {
4101 error = got_error_from_errno("got_opentemp");
4104 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4105 &bca.line_offsets, bca.f, blob);
4106 if (error || bca.nlines == 0)
4109 /* Don't include \n at EOF in the blame line count. */
4110 if (bca.line_offsets[bca.nlines - 1] == filesize)
4113 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4114 if (bca.lines == NULL) {
4115 error = got_error_from_errno("calloc");
4119 bca.nlines_prec = 0;
4125 bca.repo = gw_trans->repo;
4126 bca.gw_trans = gw_trans;
4128 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
4137 free(bca.line_offsets);
4138 for (i = 0; i < bca.nlines; i++) {
4139 struct blame_line *bline = &bca.lines[i];
4140 free(bline->id_str);
4141 free(bline->committer);
4144 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4145 error = got_error_from_errno("fclose");
4148 got_object_blob_close(blob);
4152 static const struct got_error *
4153 gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
4155 const struct got_error *error = NULL;
4156 struct got_object_id *obj_id = NULL;
4157 struct got_object_id *commit_id = 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_id_by_path(&obj_id, gw_trans->repo, commit_id,
4187 if (obj_id == NULL) {
4188 error = got_error(GOT_ERR_NO_OBJ);
4192 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
4196 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4197 error = got_error(GOT_ERR_OBJ_TYPE);
4201 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
4205 hdrlen = got_object_blob_get_hdrlen(blob);
4207 error = got_object_blob_read_block(&len, blob);
4210 buf = got_object_blob_get_read_buf(blob);
4213 * Skip blob object header first time around,
4214 * which also contains a zero byte.
4217 if (set_mime == 0) {
4218 if (isbinary(buf, len - hdrlen))
4219 gw_trans->mime = KMIME_APP_OCTET_STREAM;
4221 gw_trans->mime = KMIME_TEXT_PLAIN;
4223 error = gw_display_index(gw_trans);
4227 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
4228 if (kerr != KCGI_OK)
4238 got_object_blob_close(blob);
4239 if (error == NULL && kerr != KCGI_OK)
4240 error = gw_kcgi_error(kerr);
4244 static const struct got_error *
4245 gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
4247 const struct got_error *error = NULL;
4248 struct got_object_id *tree_id = NULL, *commit_id = NULL;
4249 struct got_tree_object *tree = NULL;
4250 char *path = NULL, *in_repo_path = NULL;
4251 char *id_str = NULL;
4252 char *build_folder = NULL;
4253 char *href_blob = NULL, *href_blame = NULL;
4254 const char *class = NULL;
4255 int nentries, i, class_flip = 0;
4256 enum kcgi_err kerr = KCGI_OK;
4258 if (gw_trans->repo_folder != NULL) {
4259 path = strdup(gw_trans->repo_folder);
4261 error = got_error_from_errno("strdup");
4265 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
4266 gw_trans->repo_path);
4270 path = in_repo_path;
4273 if (gw_trans->commit_id == NULL) {
4274 struct got_reference *head_ref;
4275 error = got_ref_open(&head_ref, gw_trans->repo,
4276 gw_trans->headref, 0);
4279 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
4282 got_ref_close(head_ref);
4284 * gw_trans->commit_id was not parsed from the querystring
4285 * we hit this code path from gw_index, where we don't know the
4286 * commit values for the tree link yet, so set
4287 * gw_trans->commit_id here to continue further into the tree
4289 error = got_object_id_str(&gw_trans->commit_id, commit_id);
4294 error = got_repo_match_object_id(&commit_id, NULL,
4295 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
4301 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
4306 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
4310 nentries = got_object_tree_get_nentries(tree);
4311 for (i = 0; i < nentries; i++) {
4312 struct got_tree_entry *te;
4313 const char *modestr = "";
4316 te = got_object_tree_get_entry(tree, i);
4318 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
4322 mode = got_tree_entry_get_mode(te);
4323 if (got_object_tree_entry_is_submodule(te))
4325 else if (S_ISLNK(mode))
4327 else if (S_ISDIR(mode))
4329 else if (mode & S_IXUSR)
4332 if (class_flip == 0) {
4333 class = "back_lightgray";
4336 class = "back_white";
4340 if (S_ISDIR(mode)) {
4341 if (asprintf(&build_folder, "%s/%s",
4342 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4343 got_tree_entry_get_name(te)) == -1) {
4344 error = got_error_from_errno("asprintf");
4348 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4349 gw_trans->repo_name, "action",
4350 gw_get_action_name(gw_trans), "commit",
4351 gw_trans->commit_id, "folder", build_folder, NULL);
4352 if (href_blob == NULL) {
4353 error = got_error_from_errno("khttp_urlpart");
4356 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4357 KATTR_ID, "tree_wrapper", KATTR__MAX);
4358 if (kerr != KCGI_OK)
4360 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4361 KATTR_ID, "tree_line", KATTR_CLASS, class,
4363 if (kerr != KCGI_OK)
4365 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4366 KATTR_HREF, href_blob, KATTR_CLASS,
4367 "diff_directory", KATTR__MAX);
4368 if (kerr != KCGI_OK)
4370 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4371 got_tree_entry_get_name(te), modestr);
4372 if (kerr != KCGI_OK)
4374 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4375 if (kerr != KCGI_OK)
4377 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4378 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4380 if (kerr != KCGI_OK)
4382 kerr = khtml_entity(gw_trans->gw_html_req,
4384 if (kerr != KCGI_OK)
4386 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4387 if (kerr != KCGI_OK)
4390 href_blob = khttp_urlpart(NULL, NULL, "gotweb", "path",
4391 gw_trans->repo_name, "action", "blob", "commit",
4392 gw_trans->commit_id, "file",
4393 got_tree_entry_get_name(te), "folder",
4394 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4396 if (href_blob == NULL) {
4397 error = got_error_from_errno("khttp_urlpart");
4400 href_blame = khttp_urlpart(NULL, NULL, "gotweb", "path",
4401 gw_trans->repo_name, "action", "blame", "commit",
4402 gw_trans->commit_id, "file",
4403 got_tree_entry_get_name(te), "folder",
4404 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4406 if (href_blame == NULL) {
4407 error = got_error_from_errno("khttp_urlpart");
4410 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4411 KATTR_ID, "tree_wrapper", KATTR__MAX);
4412 if (kerr != KCGI_OK)
4414 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4415 KATTR_ID, "tree_line", KATTR_CLASS, class,
4417 if (kerr != KCGI_OK)
4419 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4420 KATTR_HREF, href_blob, KATTR__MAX);
4421 if (kerr != KCGI_OK)
4423 kerr = khtml_printf(gw_trans->gw_html_req, "%s%s",
4424 got_tree_entry_get_name(te), modestr);
4425 if (kerr != KCGI_OK)
4427 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4428 if (kerr != KCGI_OK)
4430 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4431 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4433 if (kerr != KCGI_OK)
4436 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4437 KATTR_HREF, href_blob, KATTR__MAX);
4438 if (kerr != KCGI_OK)
4440 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4441 if (kerr != KCGI_OK)
4443 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4444 if (kerr != KCGI_OK)
4447 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4448 if (kerr != KCGI_OK)
4451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4452 KATTR_HREF, href_blame, KATTR__MAX);
4453 if (kerr != KCGI_OK)
4455 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4456 if (kerr != KCGI_OK)
4459 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4460 if (kerr != KCGI_OK)
4468 build_folder = NULL;
4472 got_object_tree_close(tree);
4479 if (error == NULL && kerr != KCGI_OK)
4480 error = gw_kcgi_error(kerr);
4484 static const struct got_error *
4485 gw_output_repo_heads(struct gw_trans *gw_trans)
4487 const struct got_error *error = NULL;
4488 struct got_reflist_head refs;
4489 struct got_reflist_entry *re;
4490 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4491 char *href_commits = NULL;
4492 enum kcgi_err kerr = KCGI_OK;
4496 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4497 got_ref_cmp_by_name, NULL);
4501 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4502 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4503 if (kerr != KCGI_OK)
4505 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4506 KATTR_ID, "summary_heads_title", KATTR__MAX);
4507 if (kerr != KCGI_OK)
4509 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4510 if (kerr != KCGI_OK)
4512 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4513 if (kerr != KCGI_OK)
4515 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4516 KATTR_ID, "summary_heads_content", KATTR__MAX);
4517 if (kerr != KCGI_OK)
4520 TAILQ_FOREACH(re, &refs, entry) {
4521 const char *refname;
4523 if (got_ref_is_symbolic(re->ref))
4526 refname = got_ref_get_name(re->ref);
4527 if (strncmp(refname, "refs/heads/", 11) != 0)
4530 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4535 if (strncmp(refname, "refs/heads/", 11) == 0)
4538 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4539 KATTR_ID, "heads_wrapper", KATTR__MAX);
4540 if (kerr != KCGI_OK)
4542 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4543 KATTR_ID, "heads_age", KATTR__MAX);
4544 if (kerr != KCGI_OK)
4546 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4547 if (kerr != KCGI_OK)
4549 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4550 if (kerr != KCGI_OK)
4552 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4553 KATTR_ID, "heads_space", KATTR__MAX);
4554 if (kerr != KCGI_OK)
4556 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4557 if (kerr != KCGI_OK)
4559 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4560 if (kerr != KCGI_OK)
4562 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4563 KATTR_ID, "head", KATTR__MAX);
4564 if (kerr != KCGI_OK)
4567 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4568 gw_trans->repo_name, "action", "summary", "headref",
4570 if (href_summary == NULL) {
4571 error = got_error_from_errno("khttp_urlpart");
4574 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4575 href_summary, KATTR__MAX);
4576 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4577 if (kerr != KCGI_OK)
4579 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4580 if (kerr != KCGI_OK)
4583 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4584 "navs_wrapper", KATTR__MAX);
4585 if (kerr != KCGI_OK)
4587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4588 "navs", KATTR__MAX);
4589 if (kerr != KCGI_OK)
4592 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4593 href_summary, KATTR__MAX);
4594 if (kerr != KCGI_OK)
4596 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4597 if (kerr != KCGI_OK)
4599 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4600 if (kerr != KCGI_OK)
4603 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4604 if (kerr != KCGI_OK)
4607 href_briefs = khttp_urlpart(NULL, NULL, "gotweb", "path",
4608 gw_trans->repo_name, "action", "briefs", "headref",
4610 if (href_briefs == NULL) {
4611 error = got_error_from_errno("khttp_urlpart");
4614 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4615 href_briefs, KATTR__MAX);
4616 if (kerr != KCGI_OK)
4618 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4619 if (kerr != KCGI_OK)
4621 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4622 if (kerr != KCGI_OK)
4625 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4626 if (kerr != KCGI_OK)
4629 href_commits = khttp_urlpart(NULL, NULL, "gotweb", "path",
4630 gw_trans->repo_name, "action", "commits", "headref",
4632 if (href_commits == NULL) {
4633 error = got_error_from_errno("khttp_urlpart");
4636 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4637 href_commits, KATTR__MAX);
4638 if (kerr != KCGI_OK)
4640 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4641 if (kerr != KCGI_OK)
4643 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4644 if (kerr != KCGI_OK)
4647 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4648 "dotted_line", KATTR__MAX);
4649 if (kerr != KCGI_OK)
4651 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4652 if (kerr != KCGI_OK)
4655 href_summary = NULL;
4659 href_commits = NULL;
4662 got_ref_list_free(&refs);
4669 static const struct got_error *
4670 gw_output_site_link(struct gw_trans *gw_trans)
4672 const struct got_error *error = NULL;
4673 char *href_summary = NULL;
4674 enum kcgi_err kerr = KCGI_OK;
4676 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4677 "site_link", KATTR__MAX);
4678 if (kerr != KCGI_OK)
4680 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4682 if (kerr != KCGI_OK)
4684 kerr = khtml_puts(gw_trans->gw_html_req,
4685 gw_trans->gw_conf->got_site_link);
4686 if (kerr != KCGI_OK)
4688 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4689 if (kerr != KCGI_OK)
4692 if (gw_trans->repo_name != NULL) {
4693 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4694 if (kerr != KCGI_OK)
4697 href_summary = khttp_urlpart(NULL, NULL, "gotweb", "path",
4698 gw_trans->repo_name, "action", "summary", NULL);
4699 if (href_summary == NULL) {
4700 error = got_error_from_errno("khttp_urlpart");
4703 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4704 href_summary, KATTR__MAX);
4705 if (kerr != KCGI_OK)
4707 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4708 if (kerr != KCGI_OK)
4710 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4711 if (kerr != KCGI_OK)
4713 kerr = khtml_printf(gw_trans->gw_html_req, " / %s",
4714 gw_get_action_name(gw_trans));
4715 if (kerr != KCGI_OK)
4719 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4720 if (kerr != KCGI_OK)
4724 if (error == NULL && kerr != KCGI_OK)
4725 error = gw_kcgi_error(kerr);
4729 static const struct got_error *
4730 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4732 const struct got_error *error = NULL;
4734 enum kcgi_err kerr = KCGI_OK;
4736 if (strncmp(buf, "-", 1) == 0)
4737 color = "diff_minus";
4738 else if (strncmp(buf, "+", 1) == 0)
4739 color = "diff_plus";
4740 else if (strncmp(buf, "@@", 2) == 0)
4741 color = "diff_chunk_header";
4742 else if (strncmp(buf, "@@", 2) == 0)
4743 color = "diff_chunk_header";
4744 else if (strncmp(buf, "commit +", 8) == 0)
4745 color = "diff_meta";
4746 else if (strncmp(buf, "commit -", 8) == 0)
4747 color = "diff_meta";
4748 else if (strncmp(buf, "blob +", 6) == 0)
4749 color = "diff_meta";
4750 else if (strncmp(buf, "blob -", 6) == 0)
4751 color = "diff_meta";
4752 else if (strncmp(buf, "file +", 6) == 0)
4753 color = "diff_meta";
4754 else if (strncmp(buf, "file -", 6) == 0)
4755 color = "diff_meta";
4756 else if (strncmp(buf, "from:", 5) == 0)
4757 color = "diff_author";
4758 else if (strncmp(buf, "via:", 4) == 0)
4759 color = "diff_author";
4760 else if (strncmp(buf, "date:", 5) == 0)
4761 color = "diff_date";
4762 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4763 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4764 if (error == NULL && kerr != KCGI_OK)
4765 error = gw_kcgi_error(kerr);
4770 main(int argc, char *argv[])
4772 const struct got_error *error = NULL, *error2 = NULL;
4773 struct gw_trans *gw_trans;
4774 struct gw_dir *dir = NULL, *tdir;
4775 const char *page = "index";
4776 enum kcgi_err kerr = KCGI_OK;
4778 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4781 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4784 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4787 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4790 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4791 if (kerr != KCGI_OK) {
4792 error = gw_kcgi_error(kerr);
4796 TAILQ_INIT(&gw_trans->gw_dirs);
4797 TAILQ_INIT(&gw_trans->gw_headers);
4799 gw_trans->action = -1;
4801 gw_trans->repos_total = 0;
4802 gw_trans->repo_path = NULL;
4803 gw_trans->commit_id = NULL;
4804 gw_trans->next_id = NULL;
4805 gw_trans->prev_id = NULL;
4806 gw_trans->headref = GOT_REF_HEAD;
4807 gw_trans->mime = KMIME_TEXT_HTML;
4808 gw_trans->gw_tmpl->key = gw_templs;
4809 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4810 gw_trans->gw_tmpl->arg = gw_trans;
4811 gw_trans->gw_tmpl->cb = gw_template;
4813 error = parse_gotweb_config(&gw_trans->gw_conf, GOTWEB_CONF);
4817 error = gw_parse_querystring(gw_trans);
4821 if (gw_trans->action == GW_BLOB)
4822 error = gw_blob(gw_trans);
4824 error = gw_display_index(gw_trans);
4826 if (gw_trans->repo) {
4827 const struct got_error *close_err;
4828 close_err = got_repo_close(gw_trans->repo);
4833 gw_trans->error = error;
4834 gw_trans->action = GW_ERR;
4835 error2 = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
4837 goto cleanup; /* we can't display an error page */
4838 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
4839 if (kerr != KCGI_OK)
4840 goto cleanup; /* we can't display an error page */
4841 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
4842 gw_query_funcs[gw_trans->action].template);
4843 if (kerr != KCGI_OK) {
4844 khtml_close(gw_trans->gw_html_req);
4845 goto cleanup; /* we can't display an error page */
4850 free(gw_trans->gw_conf->got_repos_path);
4851 free(gw_trans->gw_conf->got_www_path);
4852 free(gw_trans->gw_conf->got_site_name);
4853 free(gw_trans->gw_conf->got_site_owner);
4854 free(gw_trans->gw_conf->got_site_link);
4855 free(gw_trans->gw_conf->got_logo);
4856 free(gw_trans->gw_conf->got_logo_url);
4857 free(gw_trans->gw_conf);
4858 free(gw_trans->commit_id);
4859 free(gw_trans->next_id);
4860 free(gw_trans->prev_id);
4861 free(gw_trans->repo_path);
4862 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4864 free(dir->description);
4871 khttp_free(gw_trans->gw_req);