2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
20 #include <sys/types.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 TAILQ_HEAD(headers, gw_header) gw_headers;
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct got_repository *repo;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
64 const struct got_error *error;
65 const char *repo_name;
72 const char *repo_file;
77 unsigned int repos_total;
82 TAILQ_ENTRY(gw_header) entry;
83 struct got_reflist_head refs;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
93 time_t committer_time;
97 TAILQ_ENTRY(gw_dir) entry;
140 static const char *const gw_templs[TEMPL__MAX] = {
150 static const struct kvalid gw_keys[KEY__ZMAX] = {
151 { kvalid_stringne, "action" },
152 { kvalid_stringne, "commit" },
153 { kvalid_stringne, "file" },
154 { kvalid_stringne, "folder" },
155 { kvalid_stringne, "headref" },
156 { kvalid_int, "page" },
157 { kvalid_stringne, "path" },
158 { kvalid_stringne, "prev" },
159 { kvalid_stringne, "prev_prev" },
162 static struct gw_header *gw_init_header(void);
164 static void gw_free_header(struct gw_header *);
166 static int gw_template(size_t, void *);
168 static const struct got_error *gw_error(struct gw_trans *);
169 static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
170 static const struct got_error *gw_get_repo_description(char **,
171 struct gw_trans *, char *);
172 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
174 static const struct got_error *gw_get_time_str(char **, time_t, int);
175 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
176 char *, const char *, int);
177 static const struct got_error *gw_output_file_blame(struct gw_trans *);
178 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
179 static const struct got_error *gw_output_repo_tree(struct gw_trans *);
180 static const struct got_error *gw_output_diff(struct gw_trans *,
182 static const struct got_error *gw_output_repo_tags(struct gw_trans *,
183 struct gw_header *, int, int);
184 static const struct got_error *gw_output_repo_heads(struct gw_trans *);
185 static const struct got_error *gw_output_site_link(struct gw_trans *);
186 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
188 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
190 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
192 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
194 static const struct got_error *gw_gen_author_header(struct gw_trans *,
196 static const struct got_error *gw_gen_age_header(struct gw_trans *,
198 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
200 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
202 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
203 static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
205 static const struct got_error *gw_display_index(struct gw_trans *);
206 static const struct got_error *gw_get_header(struct gw_trans *,
207 struct gw_header *, int);
208 static const struct got_error *gw_get_commits(struct gw_trans *,
209 struct gw_header *, int,
210 struct got_object_id *);
211 static const struct got_error *gw_get_commit(struct gw_trans *,
213 struct got_commit_object *,
214 struct got_object_id *);
215 static const struct got_error *gw_apply_unveil(const char *);
216 static const struct got_error *gw_blame_cb(void *, int, int,
217 struct got_object_id *);
218 static const struct got_error *gw_load_got_paths(struct gw_trans *);
219 static const struct got_error *gw_load_got_path(struct gw_trans *,
221 static const struct got_error *gw_parse_querystring(struct gw_trans *);
222 static const struct got_error *gw_blame(struct gw_trans *);
223 static const struct got_error *gw_blob(struct gw_trans *);
224 static const struct got_error *gw_diff(struct gw_trans *);
225 static const struct got_error *gw_index(struct gw_trans *);
226 static const struct got_error *gw_commits(struct gw_trans *);
227 static const struct got_error *gw_briefs(struct gw_trans *);
228 static const struct got_error *gw_summary(struct gw_trans *);
229 static const struct got_error *gw_tree(struct gw_trans *);
230 static const struct got_error *gw_tag(struct gw_trans *);
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 {
252 static struct gw_query_action gw_query_funcs[] = {
253 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
254 { GW_BLOB, "blob", NULL, NULL },
255 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
256 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
257 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
258 { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
259 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
260 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
261 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
262 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
266 gw_get_action_name(struct gw_trans *gw_trans)
268 return gw_query_funcs[gw_trans->action].func_name;
271 static const struct got_error *
272 gw_kcgi_error(enum kcgi_err kerr)
277 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
278 return got_error(GOT_ERR_CANCELLED);
280 if (kerr == KCGI_ENOMEM)
281 return got_error_set_errno(ENOMEM,
282 kcgi_strerror(kerr));
284 if (kerr == KCGI_ENFILE)
285 return got_error_set_errno(ENFILE,
286 kcgi_strerror(kerr));
288 if (kerr == KCGI_EAGAIN)
289 return got_error_set_errno(EAGAIN,
290 kcgi_strerror(kerr));
292 if (kerr == KCGI_FORM)
293 return got_error_msg(GOT_ERR_IO,
294 kcgi_strerror(kerr));
296 return got_error_from_errno(kcgi_strerror(kerr));
299 static const struct got_error *
300 gw_apply_unveil(const char *repo_path)
302 const struct got_error *err;
304 if (repo_path && unveil(repo_path, "r") != 0)
305 return got_error_from_errno2("unveil", repo_path);
307 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
308 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
310 err = got_privsep_unveil_exec_helpers();
314 if (unveil(NULL, NULL) != 0)
315 return got_error_from_errno("unveil");
321 isbinary(const uint8_t *buf, size_t n)
325 for (i = 0; i < n; i++)
331 static const struct got_error *
332 gw_blame(struct gw_trans *gw_trans)
334 const struct got_error *error = NULL;
335 struct gw_header *header = NULL;
337 enum kcgi_err kerr = KCGI_OK;
339 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
341 return got_error_from_errno("pledge");
343 if ((header = gw_init_header()) == NULL)
344 return got_error_from_errno("malloc");
346 error = gw_apply_unveil(gw_trans->gw_dir->path);
350 /* check querystring */
351 if (gw_trans->repo_file == NULL) {
352 error = got_error_msg(GOT_ERR_QUERYSTRING,
353 "file required in querystring");
356 if (gw_trans->commit_id == NULL) {
357 error = got_error_msg(GOT_ERR_QUERYSTRING,
358 "commit required in querystring");
362 error = gw_get_header(gw_trans, header, 1);
365 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
366 "blame_header_wrapper", KATTR__MAX);
369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
370 "blame_header", KATTR__MAX);
373 error = gw_get_time_str(&age, header->committer_time,
377 error = gw_gen_age_header(gw_trans, age ?age : "");
380 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
383 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
386 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
387 "dotted_line", KATTR__MAX);
390 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
394 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
395 "blame", KATTR__MAX);
398 error = gw_output_file_blame(gw_trans);
401 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
403 gw_free_header(header);
404 if (error == NULL && kerr != KCGI_OK)
405 error = gw_kcgi_error(kerr);
409 static const struct got_error *
410 gw_blob(struct gw_trans *gw_trans)
412 const struct got_error *error = NULL, *err = NULL;
413 struct gw_header *header = NULL;
414 enum kcgi_err kerr = KCGI_OK;
416 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
418 return got_error_from_errno("pledge");
420 if ((header = gw_init_header()) == NULL)
421 return got_error_from_errno("malloc");
423 error = gw_apply_unveil(gw_trans->gw_dir->path);
427 /* check querystring */
428 if (gw_trans->repo_file == NULL) {
429 error = got_error_msg(GOT_ERR_QUERYSTRING,
430 "file required in querystring");
433 if (gw_trans->commit_id == NULL) {
434 error = got_error_msg(GOT_ERR_QUERYSTRING,
435 "commit required in querystring");
438 error = gw_get_header(gw_trans, header, 1);
442 error = gw_output_blob_buf(gw_trans);
446 gw_trans->mime = KMIME_TEXT_PLAIN;
447 err = gw_display_index(gw_trans);
452 kerr = khttp_puts(gw_trans->gw_req, error->msg);
455 gw_free_header(header);
456 if (error == NULL && kerr != KCGI_OK)
457 error = gw_kcgi_error(kerr);
461 static const struct got_error *
462 gw_diff(struct gw_trans *gw_trans)
464 const struct got_error *error = NULL;
465 struct gw_header *header = NULL;
467 enum kcgi_err kerr = KCGI_OK;
469 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
471 return got_error_from_errno("pledge");
473 if ((header = gw_init_header()) == NULL)
474 return got_error_from_errno("malloc");
476 error = gw_apply_unveil(gw_trans->gw_dir->path);
480 error = gw_get_header(gw_trans, header, 1);
484 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
485 "diff_header_wrapper", KATTR__MAX);
488 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
489 "diff_header", KATTR__MAX);
492 error = gw_gen_diff_header(gw_trans, header->parent_id,
496 error = gw_gen_commit_header(gw_trans, header->commit_id,
500 error = gw_gen_tree_header(gw_trans, header->tree_id);
503 error = gw_gen_author_header(gw_trans, header->author);
506 error = gw_gen_committer_header(gw_trans, header->author);
509 error = gw_get_time_str(&age, header->committer_time,
513 error = gw_gen_age_header(gw_trans, age ?age : "");
516 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
519 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
522 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
523 "dotted_line", KATTR__MAX);
526 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
530 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
534 error = gw_output_diff(gw_trans, header);
538 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
540 gw_free_header(header);
542 if (error == NULL && kerr != KCGI_OK)
543 error = gw_kcgi_error(kerr);
547 static const struct got_error *
548 gw_index(struct gw_trans *gw_trans)
550 const struct got_error *error = NULL;
551 struct gw_dir *gw_dir = NULL;
552 char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
553 char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
554 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
557 if (pledge("stdio rpath proc exec sendfd unveil",
559 error = got_error_from_errno("pledge");
563 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
567 error = gw_load_got_paths(gw_trans);
571 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
572 "index_header", KATTR__MAX);
574 return gw_kcgi_error(kerr);
575 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
576 "index_header_project", KATTR__MAX);
578 return gw_kcgi_error(kerr);
579 kerr = khtml_puts(gw_trans->gw_html_req, "Project");
581 return gw_kcgi_error(kerr);
582 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
584 return gw_kcgi_error(kerr);
586 if (gw_trans->gw_conf->got_show_repo_description) {
587 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
588 "index_header_description", KATTR__MAX);
590 return gw_kcgi_error(kerr);
591 kerr = khtml_puts(gw_trans->gw_html_req, "Description");
593 return gw_kcgi_error(kerr);
594 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
596 return gw_kcgi_error(kerr);
599 if (gw_trans->gw_conf->got_show_repo_owner) {
600 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
601 "index_header_owner", KATTR__MAX);
603 return gw_kcgi_error(kerr);
604 kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
606 return gw_kcgi_error(kerr);
607 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
609 return gw_kcgi_error(kerr);
612 if (gw_trans->gw_conf->got_show_repo_age) {
613 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
614 "index_header_age", KATTR__MAX);
616 return gw_kcgi_error(kerr);
617 kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
619 return gw_kcgi_error(kerr);
620 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
622 return gw_kcgi_error(kerr);
625 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
627 return gw_kcgi_error(kerr);
629 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
630 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
631 "index_wrapper", KATTR__MAX);
633 return gw_kcgi_error(kerr);
634 kerr = khtml_puts(gw_trans->gw_html_req,
635 "No repositories found in ");
637 return gw_kcgi_error(kerr);
638 kerr = khtml_puts(gw_trans->gw_html_req,
639 gw_trans->gw_conf->got_repos_path);
641 return gw_kcgi_error(kerr);
642 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
644 return gw_kcgi_error(kerr);
645 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
646 "dotted_line", KATTR__MAX);
648 return gw_kcgi_error(kerr);
649 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
651 return gw_kcgi_error(kerr);
655 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
658 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
659 if (gw_trans->page > 0 && (gw_trans->page *
660 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
667 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
668 "index_wrapper", KATTR__MAX);
672 if (asprintf(&href_summary, "?path=%s&action=summary",
673 gw_dir->name) == -1) {
674 error = got_error_from_errno("asprintf");
677 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
678 "index_project", KATTR__MAX);
681 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
682 href_summary, KATTR__MAX);
685 kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
688 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
691 if (gw_trans->gw_conf->got_show_repo_description) {
692 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
693 KATTR_ID, "index_project_description", KATTR__MAX);
696 kerr = khtml_puts(gw_trans->gw_html_req,
697 gw_dir->description ? gw_dir->description : "");
700 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
704 if (gw_trans->gw_conf->got_show_repo_owner) {
705 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
706 KATTR_ID, "index_project_owner", KATTR__MAX);
709 kerr = khtml_puts(gw_trans->gw_html_req,
710 gw_dir->owner ? gw_dir->owner : "");
713 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
717 if (gw_trans->gw_conf->got_show_repo_age) {
718 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
719 KATTR_ID, "index_project_age", KATTR__MAX);
722 kerr = khtml_puts(gw_trans->gw_html_req,
723 gw_dir->age ? gw_dir->age : "");
726 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
731 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
732 "navs_wrapper", KATTR__MAX);
735 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
740 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
741 href_summary, KATTR__MAX);
744 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
747 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
751 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
755 if (asprintf(&href_briefs, "?path=%s&action=briefs",
756 gw_dir->name) == -1) {
757 error = got_error_from_errno("asprintf");
760 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
761 href_briefs, KATTR__MAX);
764 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
767 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
769 error = gw_kcgi_error(kerr);
771 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
775 if (asprintf(&href_commits, "?path=%s&action=commits",
776 gw_dir->name) == -1) {
777 error = got_error_from_errno("asprintf");
780 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
781 href_commits, KATTR__MAX);
784 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
787 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
791 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
795 if (asprintf(&href_tree, "?path=%s&action=tree",
796 gw_dir->name) == -1) {
797 error = got_error_from_errno("asprintf");
800 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
801 href_tree, KATTR__MAX);
804 kerr = khtml_puts(gw_trans->gw_html_req, "tree");
808 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
812 "dotted_line", KATTR__MAX);
815 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
828 if (gw_trans->gw_conf->got_max_repos_display == 0)
831 if ((next_disp == gw_trans->gw_conf->got_max_repos_display) ||
832 ((gw_trans->gw_conf->got_max_repos_display > 0) &&
833 (gw_trans->page > 0) &&
834 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
835 prev_disp == gw_trans->repos_total))) {
836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
837 KATTR_ID, "np_wrapper", KATTR__MAX);
840 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
841 KATTR_ID, "nav_prev", KATTR__MAX);
846 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
847 (gw_trans->page > 0) &&
848 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
849 prev_disp == gw_trans->repos_total)) {
850 if (asprintf(&href_prev, "?page=%d",
851 gw_trans->page - 1) == -1) {
852 error = got_error_from_errno("asprintf");
855 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
856 KATTR_HREF, href_prev, KATTR__MAX);
859 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
862 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
867 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
869 return gw_kcgi_error(kerr);
871 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
872 next_disp == gw_trans->gw_conf->got_max_repos_display &&
873 dir_c != (gw_trans->page + 1) *
874 gw_trans->gw_conf->got_max_repos_display) {
875 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
876 KATTR_ID, "nav_next", KATTR__MAX);
879 if (asprintf(&href_next, "?page=%d",
880 gw_trans->page + 1) == -1) {
881 error = got_error_from_errno("calloc");
884 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
885 KATTR_HREF, href_next, KATTR__MAX);
888 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
891 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
898 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
899 (gw_trans->page > 0) &&
900 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
901 prev_disp == gw_trans->repos_total)) {
902 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
915 if (error == NULL && kerr != KCGI_OK)
916 error = gw_kcgi_error(kerr);
920 static const struct got_error *
921 gw_commits(struct gw_trans *gw_trans)
923 const struct got_error *error = NULL;
924 struct gw_header *header = NULL, *n_header = NULL;
925 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
926 char *href_prev = NULL, *href_next = NULL;
927 enum kcgi_err kerr = KCGI_OK;
929 if ((header = gw_init_header()) == NULL)
930 return got_error_from_errno("malloc");
932 if (pledge("stdio rpath proc exec sendfd unveil",
934 error = got_error_from_errno("pledge");
938 error = gw_apply_unveil(gw_trans->gw_dir->path);
942 error = gw_get_header(gw_trans, header,
943 gw_trans->gw_conf->got_max_commits_display);
947 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
948 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
949 "commits_line_wrapper", KATTR__MAX);
952 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
956 error = gw_gen_author_header(gw_trans, n_header->author);
959 error = gw_gen_committer_header(gw_trans, n_header->author);
962 error = gw_get_time_str(&age, n_header->committer_time,
966 error = gw_gen_age_header(gw_trans, age ?age : "");
969 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
973 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
974 "dotted_line", KATTR__MAX);
977 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
981 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
982 "commit", KATTR__MAX);
985 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
988 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
992 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
993 gw_trans->repo_name, n_header->commit_id) == -1) {
994 error = got_error_from_errno("asprintf");
997 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
998 KATTR_ID, "navs_wrapper", KATTR__MAX);
1001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1002 KATTR_ID, "navs", KATTR__MAX);
1003 if (kerr != KCGI_OK)
1005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1006 KATTR_HREF, href_diff, KATTR__MAX);
1007 if (kerr != KCGI_OK)
1009 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1010 if (kerr != KCGI_OK)
1012 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1013 if (kerr != KCGI_OK)
1016 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1017 if (kerr != KCGI_OK)
1020 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1021 gw_trans->repo_name, n_header->commit_id) == -1) {
1022 error = got_error_from_errno("asprintf");
1025 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1026 KATTR_HREF, href_blob, KATTR__MAX);
1027 if (kerr != KCGI_OK)
1029 khtml_puts(gw_trans->gw_html_req, "tree");
1030 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1031 if (kerr != KCGI_OK)
1033 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1034 if (kerr != KCGI_OK)
1037 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1038 "solid_line", KATTR__MAX);
1039 if (kerr != KCGI_OK)
1041 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1042 if (kerr != KCGI_OK)
1049 if (gw_trans->next_id || gw_trans->page > 0) {
1050 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1051 KATTR_ID, "np_wrapper", KATTR__MAX);
1052 if (kerr != KCGI_OK)
1054 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1055 KATTR_ID, "nav_prev", KATTR__MAX);
1056 if (kerr != KCGI_OK)
1060 if (gw_trans->page > 0 && gw_trans->prev_id) {
1061 if (asprintf(&href_prev,
1062 "?path=%s&page=%d&action=commits&commit=%s&prev=%s",
1063 gw_trans->repo_name, gw_trans->page - 1,
1064 gw_trans->prev_id ? gw_trans->prev_id : "",
1065 gw_trans->prev_prev_id ?
1066 gw_trans->prev_prev_id : "") == -1) {
1067 error = got_error_from_errno("asprintf");
1070 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1071 KATTR_HREF, href_prev, KATTR__MAX);
1072 if (kerr != KCGI_OK)
1074 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1075 if (kerr != KCGI_OK)
1077 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1078 if (kerr != KCGI_OK)
1082 if (gw_trans->next_id || gw_trans->page > 0) {
1083 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1084 if (kerr != KCGI_OK)
1085 return gw_kcgi_error(kerr);
1088 if (gw_trans->next_id) {
1089 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1090 KATTR_ID, "nav_next", KATTR__MAX);
1091 if (kerr != KCGI_OK)
1093 if (asprintf(&href_next,
1094 "?path=%s&page=%d&action=commits" \
1095 "&commit=%s&prev=%s&prev_prev=%s",
1096 gw_trans->repo_name, gw_trans->page + 1,
1098 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1100 gw_trans->prev_id : "") == -1) {
1101 error = got_error_from_errno("calloc");
1104 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1105 KATTR_HREF, href_next, KATTR__MAX);
1106 if (kerr != KCGI_OK)
1108 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1109 if (kerr != KCGI_OK)
1111 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1112 if (kerr != KCGI_OK)
1116 if (gw_trans->next_id || gw_trans->page > 0) {
1117 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1118 if (kerr != KCGI_OK)
1122 gw_free_header(header);
1123 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1124 gw_free_header(n_header);
1130 if (error == NULL && kerr != KCGI_OK)
1131 error = gw_kcgi_error(kerr);
1135 static const struct got_error *
1136 gw_briefs(struct gw_trans *gw_trans)
1138 const struct got_error *error = NULL;
1139 struct gw_header *header = NULL, *n_header = NULL;
1140 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
1141 char *href_prev = NULL, *href_next = NULL;
1142 char *newline, *smallerthan;
1143 enum kcgi_err kerr = KCGI_OK;
1145 if ((header = gw_init_header()) == NULL)
1146 return got_error_from_errno("malloc");
1148 if (pledge("stdio rpath proc exec sendfd unveil",
1150 error = got_error_from_errno("pledge");
1154 if (gw_trans->action != GW_SUMMARY) {
1155 error = gw_apply_unveil(gw_trans->gw_dir->path);
1160 if (gw_trans->action == GW_SUMMARY)
1161 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1163 error = gw_get_header(gw_trans, header,
1164 gw_trans->gw_conf->got_max_commits_display);
1168 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1169 error = gw_get_time_str(&age, n_header->committer_time,
1174 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1175 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1176 if (kerr != KCGI_OK)
1179 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1180 KATTR_ID, "briefs_age", KATTR__MAX);
1181 if (kerr != KCGI_OK)
1183 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1184 if (kerr != KCGI_OK)
1186 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1187 if (kerr != KCGI_OK)
1190 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1191 KATTR_ID, "briefs_author", KATTR__MAX);
1192 if (kerr != KCGI_OK)
1194 smallerthan = strchr(n_header->author, '<');
1196 *smallerthan = '\0';
1197 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1198 if (kerr != KCGI_OK)
1200 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1201 if (kerr != KCGI_OK)
1204 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1205 gw_trans->repo_name, n_header->commit_id) == -1) {
1206 error = got_error_from_errno("asprintf");
1209 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1210 KATTR_ID, "briefs_log", KATTR__MAX);
1211 if (kerr != KCGI_OK)
1213 newline = strchr(n_header->commit_msg, '\n');
1216 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1217 KATTR_HREF, href_diff, KATTR__MAX);
1218 if (kerr != KCGI_OK)
1220 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1221 if (kerr != KCGI_OK)
1223 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1224 if (kerr != KCGI_OK)
1227 if (n_header->refs_str) {
1228 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1229 if (kerr != KCGI_OK)
1231 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1232 KATTR_ID, "refs_str", KATTR__MAX);
1233 if (kerr != KCGI_OK)
1235 kerr = khtml_puts(gw_trans->gw_html_req, "(");
1236 if (kerr != KCGI_OK)
1238 kerr = khtml_puts(gw_trans->gw_html_req,
1239 n_header->refs_str);
1240 if (kerr != KCGI_OK)
1242 kerr = khtml_puts(gw_trans->gw_html_req, ")");
1243 if (kerr != KCGI_OK)
1245 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1246 if (kerr != KCGI_OK)
1250 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1251 if (kerr != KCGI_OK)
1254 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1255 KATTR_ID, "navs_wrapper", KATTR__MAX);
1256 if (kerr != KCGI_OK)
1258 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1259 KATTR_ID, "navs", KATTR__MAX);
1260 if (kerr != KCGI_OK)
1262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1263 KATTR_HREF, href_diff, KATTR__MAX);
1264 if (kerr != KCGI_OK)
1266 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1267 if (kerr != KCGI_OK)
1269 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1270 if (kerr != KCGI_OK)
1273 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1274 if (kerr != KCGI_OK)
1277 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1278 gw_trans->repo_name, n_header->commit_id) == -1) {
1279 error = got_error_from_errno("asprintf");
1282 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1283 KATTR_HREF, href_blob, KATTR__MAX);
1284 if (kerr != KCGI_OK)
1286 khtml_puts(gw_trans->gw_html_req, "tree");
1287 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1288 if (kerr != KCGI_OK)
1290 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1291 if (kerr != KCGI_OK)
1294 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1295 KATTR_ID, "dotted_line", KATTR__MAX);
1296 if (kerr != KCGI_OK)
1298 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1299 if (kerr != KCGI_OK)
1310 if (gw_trans->next_id || gw_trans->page > 0) {
1311 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1312 KATTR_ID, "np_wrapper", KATTR__MAX);
1313 if (kerr != KCGI_OK)
1315 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1316 KATTR_ID, "nav_prev", KATTR__MAX);
1317 if (kerr != KCGI_OK)
1321 if (gw_trans->page > 0 && gw_trans->prev_id) {
1322 if (asprintf(&href_prev,
1323 "?path=%s&page=%d&action=briefs&commit=%s&prev=%s",
1324 gw_trans->repo_name, gw_trans->page - 1,
1325 gw_trans->prev_id ? gw_trans->prev_id : "",
1326 gw_trans->prev_prev_id ?
1327 gw_trans->prev_prev_id : "") == -1) {
1328 error = got_error_from_errno("asprintf");
1331 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1332 KATTR_HREF, href_prev, KATTR__MAX);
1333 if (kerr != KCGI_OK)
1335 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1336 if (kerr != KCGI_OK)
1338 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1339 if (kerr != KCGI_OK)
1343 if (gw_trans->next_id || gw_trans->page > 0) {
1344 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1345 if (kerr != KCGI_OK)
1346 return gw_kcgi_error(kerr);
1349 if (gw_trans->next_id) {
1350 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1351 KATTR_ID, "nav_next", KATTR__MAX);
1352 if (kerr != KCGI_OK)
1354 if (asprintf(&href_next,
1355 "?path=%s&page=%d&action=briefs" \
1356 "&commit=%s&prev=%s&prev_prev=%s",
1357 gw_trans->repo_name, gw_trans->page + 1,
1359 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1361 gw_trans->prev_id : "") == -1) {
1362 error = got_error_from_errno("calloc");
1365 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1366 KATTR_HREF, href_next, KATTR__MAX);
1367 if (kerr != KCGI_OK)
1369 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1370 if (kerr != KCGI_OK)
1372 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1373 if (kerr != KCGI_OK)
1377 if (gw_trans->next_id || gw_trans->page > 0) {
1378 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1379 if (kerr != KCGI_OK)
1383 gw_free_header(header);
1384 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1385 gw_free_header(n_header);
1391 if (error == NULL && kerr != KCGI_OK)
1392 error = gw_kcgi_error(kerr);
1396 static const struct got_error *
1397 gw_summary(struct gw_trans *gw_trans)
1399 const struct got_error *error = NULL;
1401 enum kcgi_err kerr = KCGI_OK;
1403 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1404 return got_error_from_errno("pledge");
1406 error = gw_apply_unveil(gw_trans->gw_dir->path);
1410 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1411 "summary_wrapper", KATTR__MAX);
1412 if (kerr != KCGI_OK)
1413 return gw_kcgi_error(kerr);
1415 if (gw_trans->gw_conf->got_show_repo_description &&
1416 gw_trans->gw_dir->description != NULL &&
1417 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1418 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1419 KATTR_ID, "description_title", KATTR__MAX);
1420 if (kerr != KCGI_OK)
1422 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1423 if (kerr != KCGI_OK)
1425 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1426 if (kerr != KCGI_OK)
1428 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1429 KATTR_ID, "description", KATTR__MAX);
1430 if (kerr != KCGI_OK)
1432 kerr = khtml_puts(gw_trans->gw_html_req,
1433 gw_trans->gw_dir->description);
1434 if (kerr != KCGI_OK)
1436 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1437 if (kerr != KCGI_OK)
1441 if (gw_trans->gw_conf->got_show_repo_owner &&
1442 gw_trans->gw_dir->owner != NULL &&
1443 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1444 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1445 KATTR_ID, "repo_owner_title", KATTR__MAX);
1446 if (kerr != KCGI_OK)
1448 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1449 if (kerr != KCGI_OK)
1451 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1452 if (kerr != KCGI_OK)
1454 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1455 KATTR_ID, "repo_owner", KATTR__MAX);
1456 if (kerr != KCGI_OK)
1458 kerr = khtml_puts(gw_trans->gw_html_req,
1459 gw_trans->gw_dir->owner);
1460 if (kerr != KCGI_OK)
1462 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1463 if (kerr != KCGI_OK)
1467 if (gw_trans->gw_conf->got_show_repo_age) {
1468 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1473 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1474 KATTR_ID, "last_change_title", KATTR__MAX);
1475 if (kerr != KCGI_OK)
1477 kerr = khtml_puts(gw_trans->gw_html_req,
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, "last_change", KATTR__MAX);
1486 if (kerr != KCGI_OK)
1488 kerr = khtml_puts(gw_trans->gw_html_req, age);
1489 if (kerr != KCGI_OK)
1491 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1492 if (kerr != KCGI_OK)
1497 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1498 gw_trans->gw_dir->url != NULL &&
1499 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1500 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1501 KATTR_ID, "cloneurl_title", KATTR__MAX);
1502 if (kerr != KCGI_OK)
1504 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
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, "cloneurl", KATTR__MAX);
1512 if (kerr != KCGI_OK)
1514 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1515 if (kerr != KCGI_OK)
1517 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1518 if (kerr != KCGI_OK)
1522 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1523 if (kerr != KCGI_OK)
1526 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1527 "briefs_title_wrapper", KATTR__MAX);
1528 if (kerr != KCGI_OK)
1530 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1531 "briefs_title", KATTR__MAX);
1532 if (kerr != KCGI_OK)
1534 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1535 if (kerr != KCGI_OK)
1537 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1538 if (kerr != KCGI_OK)
1540 error = gw_briefs(gw_trans);
1544 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1549 error = gw_output_repo_heads(gw_trans);
1552 if (error == NULL && kerr != KCGI_OK)
1553 error = gw_kcgi_error(kerr);
1557 static const struct got_error *
1558 gw_tree(struct gw_trans *gw_trans)
1560 const struct got_error *error = NULL;
1561 struct gw_header *header = NULL;
1562 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1566 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1567 return got_error_from_errno("pledge");
1569 if ((header = gw_init_header()) == NULL)
1570 return got_error_from_errno("malloc");
1572 error = gw_apply_unveil(gw_trans->gw_dir->path);
1576 error = gw_get_header(gw_trans, header, 1);
1580 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1581 "tree_header_wrapper", KATTR__MAX);
1582 if (kerr != KCGI_OK)
1584 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1585 "tree_header", KATTR__MAX);
1586 if (kerr != KCGI_OK)
1588 error = gw_gen_tree_header(gw_trans, header->tree_id);
1591 error = gw_get_time_str(&age, header->committer_time,
1595 error = gw_gen_age_header(gw_trans, age ?age : "");
1598 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1601 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1602 if (kerr != KCGI_OK)
1604 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1605 "dotted_line", KATTR__MAX);
1606 if (kerr != KCGI_OK)
1608 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1609 if (kerr != KCGI_OK)
1612 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1613 "tree", KATTR__MAX);
1614 if (kerr != KCGI_OK)
1616 error = gw_output_repo_tree(gw_trans);
1620 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1622 gw_free_header(header);
1623 free(tree_html_disp);
1627 if (error == NULL && kerr != KCGI_OK)
1628 error = gw_kcgi_error(kerr);
1632 static const struct got_error *
1633 gw_tag(struct gw_trans *gw_trans)
1635 const struct got_error *error = NULL;
1636 struct gw_header *header = NULL;
1639 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1640 return got_error_from_errno("pledge");
1642 if ((header = gw_init_header()) == NULL)
1643 return got_error_from_errno("malloc");
1645 error = gw_apply_unveil(gw_trans->gw_dir->path);
1649 if (gw_trans->commit_id == NULL) {
1650 error = got_error_msg(GOT_ERR_QUERYSTRING,
1651 "commit required in querystring");
1655 error = gw_get_header(gw_trans, header, 1);
1659 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1660 "tag_header_wrapper", KATTR__MAX);
1661 if (kerr != KCGI_OK)
1663 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1664 "tag_header", KATTR__MAX);
1665 if (kerr != KCGI_OK)
1667 error = gw_gen_commit_header(gw_trans, header->commit_id,
1671 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1674 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1675 if (kerr != KCGI_OK)
1677 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1678 "dotted_line", KATTR__MAX);
1679 if (kerr != KCGI_OK)
1681 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1682 if (kerr != KCGI_OK)
1685 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1686 "tree", KATTR__MAX);
1687 if (kerr != KCGI_OK)
1690 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1694 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1696 gw_free_header(header);
1697 if (error == NULL && kerr != KCGI_OK)
1698 error = gw_kcgi_error(kerr);
1702 static const struct got_error *
1703 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1705 const struct got_error *error = NULL;
1710 if (asprintf(&dir_test, "%s/%s/%s",
1711 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1712 GOTWEB_GIT_DIR) == -1)
1713 return got_error_from_errno("asprintf");
1715 dt = opendir(dir_test);
1719 gw_dir->path = strdup(dir_test);
1720 if (gw_dir->path == NULL) {
1722 error = got_error_from_errno("strdup");
1729 if (asprintf(&dir_test, "%s/%s/%s",
1730 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1731 GOTWEB_GOT_DIR) == -1) {
1733 error = got_error_from_errno("asprintf");
1737 dt = opendir(dir_test);
1742 error = got_error(GOT_ERR_NOT_GIT_REPO);
1746 if (asprintf(&dir_test, "%s/%s",
1747 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1748 error = got_error_from_errno("asprintf");
1753 gw_dir->path = strdup(dir_test);
1754 if (gw_dir->path == NULL) {
1756 error = got_error_from_errno("strdup");
1760 dt = opendir(dir_test);
1762 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1767 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1771 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1774 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1778 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1782 if (dt && closedir(dt) == -1 && error == NULL)
1783 error = got_error_from_errno("closedir");
1787 static const struct got_error *
1788 gw_load_got_paths(struct gw_trans *gw_trans)
1790 const struct got_error *error = NULL;
1792 struct dirent **sd_dent;
1793 struct gw_dir *gw_dir;
1795 unsigned int d_cnt, d_i;
1797 d = opendir(gw_trans->gw_conf->got_repos_path);
1799 error = got_error_from_errno2("opendir",
1800 gw_trans->gw_conf->got_repos_path);
1804 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1807 error = got_error_from_errno2("scandir",
1808 gw_trans->gw_conf->got_repos_path);
1812 for (d_i = 0; d_i < d_cnt; d_i++) {
1813 if (gw_trans->gw_conf->got_max_repos > 0 &&
1814 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1815 break; /* account for parent and self */
1817 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1818 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1821 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1825 error = gw_load_got_path(gw_trans, gw_dir);
1826 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1833 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1834 !got_path_dir_is_empty(gw_dir->path)) {
1835 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1837 gw_trans->repos_total++;
1841 if (d && closedir(d) == -1 && error == NULL)
1842 error = got_error_from_errno("closedir");
1846 static const struct got_error *
1847 gw_parse_querystring(struct gw_trans *gw_trans)
1849 const struct got_error *error = NULL;
1851 struct gw_query_action *action = NULL;
1854 if (gw_trans->gw_req->fieldnmap[0]) {
1855 return got_error(GOT_ERR_QUERYSTRING);
1856 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1857 /* define gw_trans->repo_path */
1858 gw_trans->repo_name = p->parsed.s;
1860 if (asprintf(&gw_trans->repo_path, "%s/%s",
1861 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1862 return got_error_from_errno("asprintf");
1864 /* get action and set function */
1865 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1866 for (i = 0; i < nitems(gw_query_funcs); i++) {
1867 action = &gw_query_funcs[i];
1868 if (action->func_name == NULL)
1870 if (strcmp(action->func_name,
1871 p->parsed.s) == 0) {
1872 gw_trans->action = i;
1877 if (gw_trans->action == -1) {
1878 gw_trans->action = GW_ERR;
1879 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
1880 p != NULL ? "bad action in querystring" :
1881 "no action in querystring");
1885 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1886 if (asprintf(&gw_trans->commit_id, "%s",
1888 return got_error_from_errno("asprintf");
1891 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1892 gw_trans->repo_file = p->parsed.s;
1894 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1895 if (asprintf(&gw_trans->repo_folder, "%s",
1897 return got_error_from_errno("asprintf");
1900 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
1901 if (asprintf(&gw_trans->prev_id, "%s",
1903 return got_error_from_errno("asprintf");
1906 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
1907 if (asprintf(&gw_trans->prev_prev_id, "%s",
1909 return got_error_from_errno("asprintf");
1912 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1913 gw_trans->headref = p->parsed.s;
1915 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1919 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1921 gw_trans->action = GW_INDEX;
1923 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1924 gw_trans->page = p->parsed.i;
1929 static const struct got_error *
1930 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1932 const struct got_error *error;
1934 *gw_dir = malloc(sizeof(**gw_dir));
1935 if (*gw_dir == NULL)
1936 return got_error_from_errno("malloc");
1938 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1939 error = got_error_from_errno("asprintf");
1948 static const struct got_error *
1949 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1953 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1954 if (kerr != KCGI_OK)
1955 return gw_kcgi_error(kerr);
1956 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1958 if (kerr != KCGI_OK)
1959 return gw_kcgi_error(kerr);
1960 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1962 if (kerr != KCGI_OK)
1963 return gw_kcgi_error(kerr);
1964 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1966 if (kerr != KCGI_OK)
1967 return gw_kcgi_error(kerr);
1968 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1969 if (kerr != KCGI_OK)
1970 return gw_kcgi_error(kerr);
1971 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1973 if (kerr != KCGI_OK)
1974 return gw_kcgi_error(kerr);
1976 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1977 kerr = khttp_head(gw_trans->gw_req,
1978 kresps[KRESP_CONTENT_DISPOSITION],
1979 "attachment; filename=%s", gw_trans->repo_file);
1980 if (kerr != KCGI_OK)
1981 return gw_kcgi_error(kerr);
1984 kerr = khttp_body(gw_trans->gw_req);
1985 return gw_kcgi_error(kerr);
1988 static const struct got_error *
1989 gw_display_index(struct gw_trans *gw_trans)
1991 const struct got_error *error;
1994 /* catch early querystring errors */
1995 if (gw_trans->error)
1996 gw_trans->action = GW_ERR;
1998 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2002 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2003 if (kerr != KCGI_OK)
2004 return gw_kcgi_error(kerr);
2006 if (gw_trans->action != GW_BLOB) {
2007 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2008 gw_query_funcs[gw_trans->action].template);
2009 if (kerr != KCGI_OK) {
2010 khtml_close(gw_trans->gw_html_req);
2011 return gw_kcgi_error(kerr);
2015 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2018 static const struct got_error *
2019 gw_error(struct gw_trans *gw_trans)
2023 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2025 return gw_kcgi_error(kerr);
2029 gw_template(size_t key, void *arg)
2031 const struct got_error *error = NULL;
2033 struct gw_trans *gw_trans = arg;
2034 char *img_src = NULL;
2038 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2039 KATTR_NAME, "viewport",
2040 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2042 if (kerr != KCGI_OK)
2044 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2045 if (kerr != KCGI_OK)
2047 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2048 KATTR_CHARSET, "utf-8",
2050 if (kerr != KCGI_OK)
2052 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2053 if (kerr != KCGI_OK)
2055 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2056 KATTR_NAME, "msapplication-TileColor",
2057 KATTR_CONTENT, "#da532c", KATTR__MAX);
2058 if (kerr != KCGI_OK)
2060 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2061 if (kerr != KCGI_OK)
2063 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2064 KATTR_NAME, "theme-color",
2065 KATTR_CONTENT, "#ffffff", KATTR__MAX);
2066 if (kerr != KCGI_OK)
2068 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2069 if (kerr != KCGI_OK)
2071 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2072 KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2073 KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
2074 if (kerr != KCGI_OK)
2076 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2077 if (kerr != KCGI_OK)
2079 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2080 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2081 "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
2082 if (kerr != KCGI_OK)
2084 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2085 if (kerr != KCGI_OK)
2087 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2088 KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2089 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
2090 if (kerr != KCGI_OK)
2092 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2093 if (kerr != KCGI_OK)
2095 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2096 KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
2098 if (kerr != KCGI_OK)
2100 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2101 if (kerr != KCGI_OK)
2103 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2104 KATTR_REL, "mask-icon", KATTR_HREF,
2105 "/safari-pinned-tab.svg", KATTR__MAX);
2106 if (kerr != KCGI_OK)
2108 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2109 if (kerr != KCGI_OK)
2111 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2112 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2113 KATTR_HREF, "/gotweb.css", KATTR__MAX);
2114 if (kerr != KCGI_OK)
2116 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2117 if (kerr != KCGI_OK)
2121 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2122 KATTR_ID, "got_link", KATTR__MAX);
2123 if (kerr != KCGI_OK)
2125 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2126 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2127 KATTR_TARGET, "_sotd", KATTR__MAX);
2128 if (kerr != KCGI_OK)
2130 if (asprintf(&img_src, "/%s",
2131 gw_trans->gw_conf->got_logo) == -1)
2133 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2134 KATTR_SRC, img_src, KATTR__MAX);
2135 if (kerr != KCGI_OK) {
2139 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2140 if (kerr != KCGI_OK) {
2145 case (TEMPL_SITEPATH):
2146 error = gw_output_site_link(gw_trans);
2151 if (gw_trans->gw_conf->got_site_name != NULL) {
2152 kerr = khtml_puts(gw_trans->gw_html_req,
2153 gw_trans->gw_conf->got_site_name);
2154 if (kerr != KCGI_OK)
2158 case (TEMPL_SEARCH):
2160 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2161 "search", KATTR__MAX);
2162 if (kerr != KCGI_OK)
2164 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2165 KATTR_METHOD, "POST", KATTR__MAX);
2166 if (kerr != KCGI_OK)
2168 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2169 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2170 KATTR_MAXLENGTH, "50", KATTR__MAX);
2171 if (kerr != KCGI_OK)
2173 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2175 if (kerr != KCGI_OK)
2177 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2178 if (kerr != KCGI_OK)
2180 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2181 if (kerr != KCGI_OK)
2184 case(TEMPL_SITEOWNER):
2185 if (gw_trans->gw_conf->got_site_owner != NULL &&
2186 gw_trans->gw_conf->got_show_site_owner) {
2187 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2188 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2189 if (kerr != KCGI_OK)
2191 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2192 KATTR_ID, "site_owner", KATTR__MAX);
2193 if (kerr != KCGI_OK)
2195 kerr = khtml_puts(gw_trans->gw_html_req,
2196 gw_trans->gw_conf->got_site_owner);
2197 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2198 if (kerr != KCGI_OK)
2202 case(TEMPL_CONTENT):
2203 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2205 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2206 KATTR_ID, "tmpl_err", KATTR__MAX);
2207 if (kerr != KCGI_OK)
2209 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
2210 if (kerr != KCGI_OK)
2212 kerr = khttp_puts(gw_trans->gw_req, error->msg);
2213 if (kerr != KCGI_OK)
2215 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2216 if (kerr != KCGI_OK)
2226 static const struct got_error *
2227 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2229 const struct got_error *error = NULL;
2230 enum kcgi_err kerr = KCGI_OK;
2232 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2233 KATTR_ID, "header_commit_title", KATTR__MAX);
2234 if (kerr != KCGI_OK)
2236 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2237 if (kerr != KCGI_OK)
2239 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2240 if (kerr != KCGI_OK)
2242 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2243 KATTR_ID, "header_commit", KATTR__MAX);
2244 if (kerr != KCGI_OK)
2246 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2247 if (kerr != KCGI_OK)
2249 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2250 if (kerr != KCGI_OK)
2253 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2254 KATTR_ID, "refs_str", KATTR__MAX);
2255 if (kerr != KCGI_OK)
2257 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2258 if (kerr != KCGI_OK)
2260 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2261 if (kerr != KCGI_OK)
2263 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2264 if (kerr != KCGI_OK)
2266 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2267 if (kerr != KCGI_OK)
2270 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2272 if (error == NULL && kerr != KCGI_OK)
2273 error = gw_kcgi_error(kerr);
2277 static const struct got_error *
2278 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2280 const struct got_error *error = NULL;
2281 enum kcgi_err kerr = KCGI_OK;
2283 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2284 KATTR_ID, "header_diff_title", KATTR__MAX);
2285 if (kerr != KCGI_OK)
2287 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2288 if (kerr != KCGI_OK)
2290 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2291 if (kerr != KCGI_OK)
2293 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2294 KATTR_ID, "header_diff", KATTR__MAX);
2295 if (kerr != KCGI_OK)
2298 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2299 if (kerr != KCGI_OK)
2302 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2303 if (kerr != KCGI_OK)
2305 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2306 if (kerr != KCGI_OK)
2308 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2310 if (error == NULL && kerr != KCGI_OK)
2311 error = gw_kcgi_error(kerr);
2315 static const struct got_error *
2316 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2318 const struct got_error *error = NULL;
2319 enum kcgi_err kerr = KCGI_OK;
2321 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2322 KATTR_ID, "header_age_title", KATTR__MAX);
2323 if (kerr != KCGI_OK)
2325 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2326 if (kerr != KCGI_OK)
2328 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2329 if (kerr != KCGI_OK)
2331 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2332 KATTR_ID, "header_age", KATTR__MAX);
2333 if (kerr != KCGI_OK)
2335 kerr = khtml_puts(gw_trans->gw_html_req, str);
2336 if (kerr != KCGI_OK)
2338 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2340 if (error == NULL && kerr != KCGI_OK)
2341 error = gw_kcgi_error(kerr);
2345 static const struct got_error *
2346 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2348 const struct got_error *error = NULL;
2351 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2352 KATTR_ID, "header_author_title", KATTR__MAX);
2353 if (kerr != KCGI_OK)
2355 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2356 if (kerr != KCGI_OK)
2358 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2359 if (kerr != KCGI_OK)
2361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2362 KATTR_ID, "header_author", KATTR__MAX);
2363 if (kerr != KCGI_OK)
2365 kerr = khtml_puts(gw_trans->gw_html_req, str);
2366 if (kerr != KCGI_OK)
2368 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2370 if (error == NULL && kerr != KCGI_OK)
2371 error = gw_kcgi_error(kerr);
2375 static const struct got_error *
2376 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2378 const struct got_error *error = NULL;
2381 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2382 KATTR_ID, "header_committer_title", KATTR__MAX);
2383 if (kerr != KCGI_OK)
2385 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2386 if (kerr != KCGI_OK)
2388 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2389 if (kerr != KCGI_OK)
2391 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2392 KATTR_ID, "header_committer", KATTR__MAX);
2393 if (kerr != KCGI_OK)
2395 kerr = khtml_puts(gw_trans->gw_html_req, str);
2396 if (kerr != KCGI_OK)
2398 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2400 if (error == NULL && kerr != KCGI_OK)
2401 error = gw_kcgi_error(kerr);
2405 static const struct got_error *
2406 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2408 const struct got_error *error = NULL;
2411 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2412 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2413 if (kerr != KCGI_OK)
2415 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2416 if (kerr != KCGI_OK)
2418 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2419 if (kerr != KCGI_OK)
2421 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2422 KATTR_ID, "header_commit_msg", KATTR__MAX);
2423 if (kerr != KCGI_OK)
2425 kerr = khttp_puts(gw_trans->gw_req, str);
2426 if (kerr != KCGI_OK)
2428 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2430 if (error == NULL && kerr != KCGI_OK)
2431 error = gw_kcgi_error(kerr);
2435 static const struct got_error *
2436 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2438 const struct got_error *error = NULL;
2441 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2442 KATTR_ID, "header_tree_title", KATTR__MAX);
2443 if (kerr != KCGI_OK)
2445 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2446 if (kerr != KCGI_OK)
2448 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2449 if (kerr != KCGI_OK)
2451 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2452 KATTR_ID, "header_tree", KATTR__MAX);
2453 if (kerr != KCGI_OK)
2455 kerr = khtml_puts(gw_trans->gw_html_req, str);
2456 if (kerr != KCGI_OK)
2458 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2460 if (error == NULL && kerr != KCGI_OK)
2461 error = gw_kcgi_error(kerr);
2465 static const struct got_error *
2466 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2469 const struct got_error *error = NULL;
2471 char *d_file = NULL;
2475 *description = NULL;
2476 if (gw_trans->gw_conf->got_show_repo_description == 0)
2479 if (asprintf(&d_file, "%s/description", dir) == -1)
2480 return got_error_from_errno("asprintf");
2482 f = fopen(d_file, "r");
2484 if (errno == ENOENT || errno == EACCES)
2486 error = got_error_from_errno2("fopen", d_file);
2490 if (fseek(f, 0, SEEK_END) == -1) {
2491 error = got_ferror(f, GOT_ERR_IO);
2496 error = got_ferror(f, GOT_ERR_IO);
2499 if (fseek(f, 0, SEEK_SET) == -1) {
2500 error = got_ferror(f, GOT_ERR_IO);
2503 *description = calloc(len + 1, sizeof(**description));
2504 if (*description == NULL) {
2505 error = got_error_from_errno("calloc");
2509 n = fread(*description, 1, len, f);
2510 if (n == 0 && ferror(f))
2511 error = got_ferror(f, GOT_ERR_IO);
2513 if (f != NULL && fclose(f) == -1 && error == NULL)
2514 error = got_error_from_errno("fclose");
2519 static const struct got_error *
2520 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2524 char *years = "years ago", *months = "months ago";
2525 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2526 char *minutes = "minutes ago", *seconds = "seconds ago";
2527 char *now = "right now";
2535 diff_time = time(NULL) - committer_time;
2536 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2537 if (asprintf(repo_age, "%lld %s",
2538 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2539 return got_error_from_errno("asprintf");
2540 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2541 if (asprintf(repo_age, "%lld %s",
2542 (diff_time / 60 / 60 / 24 / (365 / 12)),
2544 return got_error_from_errno("asprintf");
2545 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2546 if (asprintf(repo_age, "%lld %s",
2547 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2548 return got_error_from_errno("asprintf");
2549 } else if (diff_time > 60 * 60 * 24 * 2) {
2550 if (asprintf(repo_age, "%lld %s",
2551 (diff_time / 60 / 60 / 24), days) == -1)
2552 return got_error_from_errno("asprintf");
2553 } else if (diff_time > 60 * 60 * 2) {
2554 if (asprintf(repo_age, "%lld %s",
2555 (diff_time / 60 / 60), hours) == -1)
2556 return got_error_from_errno("asprintf");
2557 } else if (diff_time > 60 * 2) {
2558 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2560 return got_error_from_errno("asprintf");
2561 } else if (diff_time > 2) {
2562 if (asprintf(repo_age, "%lld %s", diff_time,
2564 return got_error_from_errno("asprintf");
2566 if (asprintf(repo_age, "%s", now) == -1)
2567 return got_error_from_errno("asprintf");
2571 if (gmtime_r(&committer_time, &tm) == NULL)
2572 return got_error_from_errno("gmtime_r");
2574 s = asctime_r(&tm, datebuf);
2576 return got_error_from_errno("asctime_r");
2578 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2579 return got_error_from_errno("asprintf");
2585 static const struct got_error *
2586 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2587 const char *refname, int ref_tm)
2589 const struct got_error *error = NULL;
2590 struct got_repository *repo = NULL;
2591 struct got_commit_object *commit = NULL;
2592 struct got_reflist_head refs;
2593 struct got_reflist_entry *re;
2594 time_t committer_time = 0, cmp_time = 0;
2597 SIMPLEQ_INIT(&refs);
2599 if (gw_trans->gw_conf->got_show_repo_age == 0)
2603 repo = gw_trans->repo;
2605 error = got_repo_open(&repo, dir, NULL);
2610 error = got_ref_list(&refs, repo, "refs/heads",
2611 got_ref_cmp_by_name, NULL);
2616 * Find the youngest branch tip in the repository, or the age of
2617 * the a specific branch tip if a name was provided by the caller.
2619 SIMPLEQ_FOREACH(re, &refs, entry) {
2620 struct got_object_id *id = NULL;
2622 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2625 error = got_ref_resolve(&id, repo, re->ref);
2629 error = got_object_open_as_commit(&commit, repo, id);
2635 got_object_commit_get_committer_time(commit);
2636 got_object_commit_close(commit);
2637 if (cmp_time < committer_time)
2638 cmp_time = committer_time;
2644 if (cmp_time != 0) {
2645 committer_time = cmp_time;
2646 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2649 got_ref_list_free(&refs);
2650 if (gw_trans->repo == NULL)
2651 got_repo_close(repo);
2655 static const struct got_error *
2656 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2658 const struct got_error *error;
2660 struct got_object_id *id1 = NULL, *id2 = NULL;
2661 char *label1 = NULL, *label2 = NULL, *line = NULL;
2663 size_t linesize = 0;
2665 enum kcgi_err kerr = KCGI_OK;
2671 if (header->parent_id != NULL &&
2672 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2673 error = got_repo_match_object_id(&id1, &label1,
2674 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2679 error = got_repo_match_object_id(&id2, &label2,
2680 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2684 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2688 case GOT_OBJ_TYPE_BLOB:
2689 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2692 case GOT_OBJ_TYPE_TREE:
2693 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2696 case GOT_OBJ_TYPE_COMMIT:
2697 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2701 error = got_error(GOT_ERR_OBJ_TYPE);
2706 if (fseek(f, 0, SEEK_SET) == -1) {
2707 error = got_ferror(f, GOT_ERR_IO);
2711 while ((linelen = getline(&line, &linesize, f)) != -1) {
2712 error = gw_colordiff_line(gw_trans, line);
2715 /* XXX: KHTML_PRETTY breaks this */
2716 kerr = khtml_puts(gw_trans->gw_html_req, line);
2717 if (kerr != KCGI_OK)
2719 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2720 if (kerr != KCGI_OK)
2723 if (linelen == -1 && ferror(f))
2724 error = got_error_from_errno("getline");
2726 if (f && fclose(f) == -1 && error == NULL)
2727 error = got_error_from_errno("fclose");
2734 if (error == NULL && kerr != KCGI_OK)
2735 error = gw_kcgi_error(kerr);
2739 static const struct got_error *
2740 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2742 const struct got_error *error = NULL;
2743 struct got_repository *repo;
2744 const char *gitconfig_owner;
2748 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2751 error = got_repo_open(&repo, dir, NULL);
2754 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2755 if (gitconfig_owner) {
2756 *owner = strdup(gitconfig_owner);
2758 error = got_error_from_errno("strdup");
2760 got_repo_close(repo);
2764 static const struct got_error *
2765 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2767 const struct got_error *error = NULL;
2769 char *d_file = NULL;
2775 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2776 return got_error_from_errno("asprintf");
2778 f = fopen(d_file, "r");
2780 if (errno != ENOENT && errno != EACCES)
2781 error = got_error_from_errno2("fopen", d_file);
2785 if (fseek(f, 0, SEEK_END) == -1) {
2786 error = got_ferror(f, GOT_ERR_IO);
2791 error = got_ferror(f, GOT_ERR_IO);
2794 if (fseek(f, 0, SEEK_SET) == -1) {
2795 error = got_ferror(f, GOT_ERR_IO);
2799 *url = calloc(len + 1, sizeof(**url));
2801 error = got_error_from_errno("calloc");
2805 n = fread(*url, 1, len, f);
2806 if (n == 0 && ferror(f))
2807 error = got_ferror(f, GOT_ERR_IO);
2809 if (f && fclose(f) == -1 && error == NULL)
2810 error = got_error_from_errno("fclose");
2815 static const struct got_error *
2816 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2817 int limit, int tag_type)
2819 const struct got_error *error = NULL;
2820 struct got_reflist_head refs;
2821 struct got_reflist_entry *re;
2823 char *id_str = NULL, *newline, *href_commits = NULL;
2824 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2825 struct got_tag_object *tag = NULL;
2826 enum kcgi_err kerr = KCGI_OK;
2827 int summary_header_displayed = 0;
2829 SIMPLEQ_INIT(&refs);
2831 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2832 got_ref_cmp_tags, gw_trans->repo);
2836 SIMPLEQ_FOREACH(re, &refs, entry) {
2837 const char *refname;
2839 const char *tag_commit;
2841 struct got_object_id *id;
2842 struct got_commit_object *commit = NULL;
2844 refname = got_ref_get_name(re->ref);
2845 if (strncmp(refname, "refs/tags/", 10) != 0)
2849 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2853 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2855 if (error->code != GOT_ERR_OBJ_TYPE) {
2859 /* "lightweight" tag */
2860 error = got_object_open_as_commit(&commit,
2861 gw_trans->repo, id);
2866 tagger = got_object_commit_get_committer(commit);
2868 got_object_commit_get_committer_time(commit);
2869 error = got_object_id_str(&id_str, id);
2873 tagger = got_object_tag_get_tagger(tag);
2874 tagger_time = got_object_tag_get_tagger_time(tag);
2875 error = got_object_id_str(&id_str,
2876 got_object_tag_get_object_id(tag));
2881 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2882 strlen(id_str)) != 0)
2886 error = got_object_commit_get_logmsg(&tag_commit0,
2890 got_object_commit_close(commit);
2892 tag_commit0 = strdup(got_object_tag_get_message(tag));
2893 if (tag_commit0 == NULL) {
2894 error = got_error_from_errno("strdup");
2899 tag_commit = tag_commit0;
2900 while (*tag_commit == '\n')
2905 newline = strchr(tag_commit, '\n');
2909 if (summary_header_displayed == 0) {
2910 kerr = khtml_attr(gw_trans->gw_html_req,
2911 KELEM_DIV, KATTR_ID,
2912 "summary_tags_title_wrapper", KATTR__MAX);
2913 if (kerr != KCGI_OK)
2915 kerr = khtml_attr(gw_trans->gw_html_req,
2916 KELEM_DIV, KATTR_ID,
2917 "summary_tags_title", KATTR__MAX);
2918 if (kerr != KCGI_OK)
2920 kerr = khtml_puts(gw_trans->gw_html_req,
2922 if (kerr != KCGI_OK)
2924 kerr = khtml_closeelem(gw_trans->gw_html_req,
2926 if (kerr != KCGI_OK)
2928 kerr = khtml_attr(gw_trans->gw_html_req,
2929 KELEM_DIV, KATTR_ID,
2930 "summary_tags_content", KATTR__MAX);
2931 if (kerr != KCGI_OK)
2933 summary_header_displayed = 1;
2936 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2937 KATTR_ID, "tags_wrapper", KATTR__MAX);
2938 if (kerr != KCGI_OK)
2940 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2941 KATTR_ID, "tags_age", KATTR__MAX);
2942 if (kerr != KCGI_OK)
2944 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2947 kerr = khtml_puts(gw_trans->gw_html_req,
2949 if (kerr != KCGI_OK)
2951 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2952 if (kerr != KCGI_OK)
2954 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2955 KATTR_ID, "tags", KATTR__MAX);
2956 if (kerr != KCGI_OK)
2958 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2959 if (kerr != KCGI_OK)
2961 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2962 if (kerr != KCGI_OK)
2964 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2965 KATTR_ID, "tags_name", KATTR__MAX);
2966 if (kerr != KCGI_OK)
2968 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2969 gw_trans->repo_name, id_str) == -1) {
2970 error = got_error_from_errno("asprintf");
2973 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2974 KATTR_HREF, href_tag, KATTR__MAX);
2975 if (kerr != KCGI_OK)
2977 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2978 if (kerr != KCGI_OK)
2980 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2981 if (kerr != KCGI_OK)
2984 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2985 KATTR_ID, "navs_wrapper", KATTR__MAX);
2986 if (kerr != KCGI_OK)
2988 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2989 KATTR_ID, "navs", KATTR__MAX);
2990 if (kerr != KCGI_OK)
2993 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2994 KATTR_HREF, href_tag, KATTR__MAX);
2995 if (kerr != KCGI_OK)
2997 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
2998 if (kerr != KCGI_OK)
3000 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3001 if (kerr != KCGI_OK)
3004 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3005 if (kerr != KCGI_OK)
3007 if (asprintf(&href_briefs,
3008 "?path=%s&action=briefs&commit=%s",
3009 gw_trans->repo_name, id_str) == -1) {
3010 error = got_error_from_errno("asprintf");
3013 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3014 KATTR_HREF, href_briefs, KATTR__MAX);
3015 if (kerr != KCGI_OK)
3017 kerr = khtml_puts(gw_trans->gw_html_req,
3019 if (kerr != KCGI_OK)
3021 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3022 if (kerr != KCGI_OK)
3025 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3026 if (kerr != KCGI_OK)
3029 if (asprintf(&href_commits,
3030 "?path=%s&action=commits&commit=%s",
3031 gw_trans->repo_name, id_str) == -1) {
3032 error = got_error_from_errno("asprintf");
3035 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3036 KATTR_HREF, href_commits, KATTR__MAX);
3037 if (kerr != KCGI_OK)
3039 kerr = khtml_puts(gw_trans->gw_html_req,
3041 if (kerr != KCGI_OK)
3043 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3044 if (kerr != KCGI_OK)
3047 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3048 KATTR_ID, "dotted_line", KATTR__MAX);
3049 if (kerr != KCGI_OK)
3051 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3052 if (kerr != KCGI_OK)
3056 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3057 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3058 if (kerr != KCGI_OK)
3060 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3061 if (kerr != KCGI_OK)
3063 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3064 if (kerr != KCGI_OK)
3066 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3067 KATTR_ID, "tag_info_date", KATTR__MAX);
3068 if (kerr != KCGI_OK)
3070 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3073 kerr = khtml_puts(gw_trans->gw_html_req,
3075 if (kerr != KCGI_OK)
3077 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3078 if (kerr != KCGI_OK)
3081 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3082 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3083 if (kerr != KCGI_OK)
3085 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3086 if (kerr != KCGI_OK)
3088 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3089 if (kerr != KCGI_OK)
3091 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3092 KATTR_ID, "tag_info_date", KATTR__MAX);
3093 if (kerr != KCGI_OK)
3095 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3096 if (kerr != KCGI_OK)
3098 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3099 if (kerr != KCGI_OK)
3102 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3103 KATTR_ID, "tag_info", KATTR__MAX);
3104 if (kerr != KCGI_OK)
3106 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3107 if (kerr != KCGI_OK)
3113 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3114 if (kerr != KCGI_OK)
3117 if (limit && --limit == 0)
3121 got_object_tag_close(tag);
3134 href_commits = NULL;
3138 got_object_tag_close(tag);
3145 got_ref_list_free(&refs);
3146 if (error == NULL && kerr != KCGI_OK)
3147 error = gw_kcgi_error(kerr);
3152 gw_free_header(struct gw_header *header)
3155 free(header->author);
3156 free(header->committer);
3157 free(header->refs_str);
3158 free(header->commit_id);
3159 free(header->parent_id);
3160 free(header->tree_id);
3161 free(header->commit_msg);
3164 static struct gw_header *
3167 struct gw_header *header;
3169 header = malloc(sizeof(*header));
3173 header->path = NULL;
3174 SIMPLEQ_INIT(&header->refs);
3176 header->refs_str = NULL;
3177 header->commit_id = NULL;
3178 header->committer = NULL;
3179 header->author = NULL;
3180 header->parent_id = NULL;
3181 header->tree_id = NULL;
3182 header->commit_msg = NULL;
3187 static const struct got_error *
3188 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3189 int limit, struct got_object_id *id)
3191 const struct got_error *error = NULL;
3192 struct got_commit_graph *graph = NULL;
3193 struct got_commit_object *commit = NULL;
3194 int chk_next = 0, chk_multi = 0, prev_set = 0;
3196 error = got_commit_graph_open(&graph, header->path, 0);
3200 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3206 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3209 if (error->code == GOT_ERR_ITER_COMPLETED)
3216 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3219 if (limit == 1 && chk_multi == 0 &&
3220 gw_trans->gw_conf->got_max_commits_display != 1) {
3221 error = gw_get_commit(gw_trans, header, commit, id);
3226 struct gw_header *n_header = NULL;
3227 if ((n_header = gw_init_header()) == NULL) {
3228 error = got_error_from_errno("malloc");
3231 error = got_ref_list(&n_header->refs, gw_trans->repo,
3232 NULL, got_ref_cmp_by_name, NULL);
3236 error = gw_get_commit(gw_trans, n_header, commit, id);
3239 got_ref_list_free(&n_header->refs);
3242 * we have a commit_id now, so copy it to next_prev_id
3243 * for navigation through gw_briefs and gw_commits
3245 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3246 (gw_trans->action == GW_BRIEFS ||
3247 gw_trans->action == GW_COMMITS ||
3248 gw_trans->action == GW_SUMMARY)) {
3250 gw_trans->next_prev_id =
3251 strdup(n_header->commit_id);
3252 if (gw_trans->next_prev_id == NULL) {
3253 error = got_error_from_errno("strdup");
3259 * check for one more commit before breaking,
3260 * so we know whether to navicate through gw_briefs
3261 * gw_commits and gw_summary
3263 if (chk_next && (gw_trans->action == GW_BRIEFS||
3264 gw_trans->action == GW_COMMITS ||
3265 gw_trans->action == GW_SUMMARY)) {
3266 gw_trans->next_id = strdup(n_header->commit_id);
3267 if (gw_trans->next_id == NULL)
3268 error = got_error_from_errno("strdup");
3272 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3275 if (error || (limit && --limit == 0)) {
3283 got_object_commit_close(commit);
3285 got_commit_graph_close(graph);
3289 static const struct got_error *
3290 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3291 struct got_commit_object *commit, struct got_object_id *id)
3293 const struct got_error *error = NULL;
3294 struct got_reflist_entry *re;
3295 struct got_object_id *id2 = NULL;
3296 struct got_object_qid *parent_id;
3297 char *commit_msg = NULL, *commit_msg0;
3300 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3303 struct got_tag_object *tag = NULL;
3306 if (got_ref_is_symbolic(re->ref))
3309 name = got_ref_get_name(re->ref);
3310 if (strncmp(name, "refs/", 5) == 0)
3312 if (strncmp(name, "got/", 4) == 0)
3314 if (strncmp(name, "heads/", 6) == 0)
3316 if (strncmp(name, "remotes/", 8) == 0)
3318 if (strncmp(name, "tags/", 5) == 0) {
3319 error = got_object_open_as_tag(&tag, gw_trans->repo,
3322 if (error->code != GOT_ERR_OBJ_TYPE)
3325 * Ref points at something other
3332 cmp = got_object_id_cmp(tag ?
3333 got_object_tag_get_object_id(tag) : re->id, id);
3335 got_object_tag_close(tag);
3338 s = header->refs_str;
3339 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3340 s ? ", " : "", name) == -1) {
3341 error = got_error_from_errno("asprintf");
3343 header->refs_str = NULL;
3349 error = got_object_id_str(&header->commit_id, id);
3353 error = got_object_id_str(&header->tree_id,
3354 got_object_commit_get_tree_id(commit));
3358 if (gw_trans->action == GW_DIFF) {
3359 parent_id = SIMPLEQ_FIRST(
3360 got_object_commit_get_parent_ids(commit));
3361 if (parent_id != NULL) {
3362 id2 = got_object_id_dup(parent_id->id);
3364 error = got_object_id_str(&header->parent_id, id2);
3369 header->parent_id = strdup("/dev/null");
3370 if (header->parent_id == NULL) {
3371 error = got_error_from_errno("strdup");
3377 header->committer_time =
3378 got_object_commit_get_committer_time(commit);
3381 strdup(got_object_commit_get_author(commit));
3382 if (header->author == NULL) {
3383 error = got_error_from_errno("strdup");
3387 strdup(got_object_commit_get_committer(commit));
3388 if (header->committer == NULL) {
3389 error = got_error_from_errno("strdup");
3392 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3396 commit_msg = commit_msg0;
3397 while (*commit_msg == '\n')
3400 header->commit_msg = strdup(commit_msg);
3401 if (header->commit_msg == NULL)
3402 error = got_error_from_errno("strdup");
3407 static const struct got_error *
3408 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3410 const struct got_error *error = NULL;
3411 char *in_repo_path = NULL;
3412 struct got_object_id *id = NULL;
3414 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3418 if (gw_trans->commit_id == NULL) {
3419 struct got_reference *head_ref;
3420 error = got_ref_open(&head_ref, gw_trans->repo,
3421 gw_trans->headref, 0);
3425 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3426 got_ref_close(head_ref);
3430 struct got_reference *ref;
3432 error = got_ref_open(&ref, gw_trans->repo,
3433 gw_trans->commit_id, 0);
3434 if (error == NULL) {
3436 error = got_ref_resolve(&id, gw_trans->repo, ref);
3440 error = got_object_get_type(&obj_type, gw_trans->repo,
3444 if (obj_type == GOT_OBJ_TYPE_TAG) {
3445 struct got_tag_object *tag;
3446 error = got_object_open_as_tag(&tag,
3447 gw_trans->repo, id);
3450 if (got_object_tag_get_object_type(tag) !=
3451 GOT_OBJ_TYPE_COMMIT) {
3452 got_object_tag_close(tag);
3453 error = got_error(GOT_ERR_OBJ_TYPE);
3457 id = got_object_id_dup(
3458 got_object_tag_get_object_id(tag));
3460 error = got_error_from_errno(
3461 "got_object_id_dup");
3462 got_object_tag_close(tag);
3465 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3466 error = got_error(GOT_ERR_OBJ_TYPE);
3470 error = got_repo_match_object_id_prefix(&id,
3471 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3477 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3478 gw_trans->repo_path, 1);
3483 header->path = strdup(in_repo_path);
3484 if (header->path == NULL) {
3485 error = got_error_from_errno("strdup");
3490 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3491 got_ref_cmp_by_name, NULL);
3495 error = gw_get_commits(gw_trans, header, limit, id);
3497 got_ref_list_free(&header->refs);
3507 char datebuf[11]; /* YYYY-MM-DD + NUL */
3510 struct gw_blame_cb_args {
3511 struct blame_line *lines;
3515 off_t *line_offsets;
3517 struct got_repository *repo;
3518 struct gw_trans *gw_trans;
3521 static const struct got_error *
3522 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3524 const struct got_error *err = NULL;
3525 struct gw_blame_cb_args *a = arg;
3526 struct blame_line *bline;
3528 size_t linesize = 0;
3529 struct got_commit_object *commit = NULL;
3532 time_t committer_time;
3533 enum kcgi_err kerr = KCGI_OK;
3535 if (nlines != a->nlines ||
3536 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3537 return got_error(GOT_ERR_RANGE);
3540 return NULL; /* no change in this commit */
3542 /* Annotate this line. */
3543 bline = &a->lines[lineno - 1];
3544 if (bline->annotated)
3546 err = got_object_id_str(&bline->id_str, id);
3550 err = got_object_open_as_commit(&commit, a->repo, id);
3554 bline->committer = strdup(got_object_commit_get_committer(commit));
3555 if (bline->committer == NULL) {
3556 err = got_error_from_errno("strdup");
3560 committer_time = got_object_commit_get_committer_time(commit);
3561 if (localtime_r(&committer_time, &tm) == NULL)
3562 return got_error_from_errno("localtime_r");
3563 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3564 &tm) >= sizeof(bline->datebuf)) {
3565 err = got_error(GOT_ERR_NO_SPACE);
3568 bline->annotated = 1;
3570 /* Print lines annotated so far. */
3571 bline = &a->lines[a->lineno_cur - 1];
3572 if (!bline->annotated)
3575 offset = a->line_offsets[a->lineno_cur - 1];
3576 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3577 err = got_error_from_errno("fseeko");
3581 while (bline->annotated) {
3582 char *smallerthan, *at, *nl, *committer;
3583 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3586 if (getline(&line, &linesize, a->f) == -1) {
3588 err = got_error_from_errno("getline");
3592 committer = bline->committer;
3593 smallerthan = strchr(committer, '<');
3594 if (smallerthan && smallerthan[1] != '\0')
3595 committer = smallerthan + 1;
3596 at = strchr(committer, '@');
3599 len = strlen(committer);
3601 committer[8] = '\0';
3603 nl = strchr(line, '\n');
3607 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3608 "blame_wrapper", KATTR__MAX);
3609 if (kerr != KCGI_OK)
3611 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3612 "blame_number", KATTR__MAX);
3613 if (kerr != KCGI_OK)
3615 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3616 a->lineno_cur) == -1)
3618 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3619 if (kerr != KCGI_OK)
3621 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3622 if (kerr != KCGI_OK)
3625 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3626 "blame_hash", KATTR__MAX);
3627 if (kerr != KCGI_OK)
3630 if (asprintf(&href_diff,
3631 "?path=%s&action=diff&commit=%s",
3632 a->gw_trans->repo_name, bline->id_str) == -1) {
3633 err = got_error_from_errno("asprintf");
3636 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3637 err = got_error_from_errno("asprintf");
3640 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3641 KATTR_HREF, href_diff, KATTR__MAX);
3642 if (kerr != KCGI_OK)
3644 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3645 if (kerr != KCGI_OK)
3647 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3648 if (kerr != KCGI_OK)
3651 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3652 "blame_date", KATTR__MAX);
3653 if (kerr != KCGI_OK)
3655 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3656 if (kerr != KCGI_OK)
3658 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3659 if (kerr != KCGI_OK)
3662 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3663 "blame_author", KATTR__MAX);
3664 if (kerr != KCGI_OK)
3666 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3667 if (kerr != KCGI_OK)
3669 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3670 if (kerr != KCGI_OK)
3673 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3674 "blame_code", KATTR__MAX);
3675 if (kerr != KCGI_OK)
3677 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3678 if (kerr != KCGI_OK)
3680 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3681 if (kerr != KCGI_OK)
3684 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3685 if (kerr != KCGI_OK)
3689 bline = &a->lines[a->lineno_cur - 1];
3697 got_object_commit_close(commit);
3699 if (err == NULL && kerr != KCGI_OK)
3700 err = gw_kcgi_error(kerr);
3704 static const struct got_error *
3705 gw_output_file_blame(struct gw_trans *gw_trans)
3707 const struct got_error *error = NULL;
3708 struct got_object_id *obj_id = NULL;
3709 struct got_object_id *commit_id = NULL;
3710 struct got_blob_object *blob = NULL;
3711 char *path = NULL, *in_repo_path = NULL;
3712 struct gw_blame_cb_args bca;
3716 if (asprintf(&path, "%s%s%s",
3717 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3718 gw_trans->repo_folder ? "/" : "",
3719 gw_trans->repo_file) == -1) {
3720 error = got_error_from_errno("asprintf");
3724 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3728 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3729 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3733 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3738 if (obj_id == NULL) {
3739 error = got_error(GOT_ERR_NO_OBJ);
3743 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3747 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3748 error = got_error(GOT_ERR_OBJ_TYPE);
3752 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3756 bca.f = got_opentemp();
3757 if (bca.f == NULL) {
3758 error = got_error_from_errno("got_opentemp");
3761 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3762 &bca.line_offsets, bca.f, blob);
3763 if (error || bca.nlines == 0)
3766 /* Don't include \n at EOF in the blame line count. */
3767 if (bca.line_offsets[bca.nlines - 1] == filesize)
3770 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3771 if (bca.lines == NULL) {
3772 error = got_error_from_errno("calloc");
3776 bca.nlines_prec = 0;
3782 bca.repo = gw_trans->repo;
3783 bca.gw_trans = gw_trans;
3785 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3794 free(bca.line_offsets);
3795 for (i = 0; i < bca.nlines; i++) {
3796 struct blame_line *bline = &bca.lines[i];
3797 free(bline->id_str);
3798 free(bline->committer);
3801 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3802 error = got_error_from_errno("fclose");
3805 got_object_blob_close(blob);
3809 static const struct got_error *
3810 gw_output_blob_buf(struct gw_trans *gw_trans)
3812 const struct got_error *error = NULL;
3813 struct got_object_id *obj_id = NULL;
3814 struct got_object_id *commit_id = NULL;
3815 struct got_blob_object *blob = NULL;
3816 char *path = NULL, *in_repo_path = NULL;
3817 int obj_type, set_mime = 0;
3820 enum kcgi_err kerr = KCGI_OK;
3822 if (asprintf(&path, "%s%s%s",
3823 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3824 gw_trans->repo_folder ? "/" : "",
3825 gw_trans->repo_file) == -1) {
3826 error = got_error_from_errno("asprintf");
3830 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3834 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3835 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3839 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3844 if (obj_id == NULL) {
3845 error = got_error(GOT_ERR_NO_OBJ);
3849 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3853 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3854 error = got_error(GOT_ERR_OBJ_TYPE);
3858 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3862 hdrlen = got_object_blob_get_hdrlen(blob);
3864 error = got_object_blob_read_block(&len, blob);
3867 buf = got_object_blob_get_read_buf(blob);
3870 * Skip blob object header first time around,
3871 * which also contains a zero byte.
3874 if (set_mime == 0) {
3875 if (isbinary(buf, len - hdrlen))
3876 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3878 gw_trans->mime = KMIME_TEXT_PLAIN;
3880 error = gw_display_index(gw_trans);
3884 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3885 if (kerr != KCGI_OK)
3895 got_object_blob_close(blob);
3896 if (error == NULL && kerr != KCGI_OK)
3897 error = gw_kcgi_error(kerr);
3901 static const struct got_error *
3902 gw_output_repo_tree(struct gw_trans *gw_trans)
3904 const struct got_error *error = NULL;
3905 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3906 struct got_tree_object *tree = NULL;
3907 char *path = NULL, *in_repo_path = NULL;
3908 char *id_str = NULL;
3909 char *build_folder = NULL;
3910 char *href_blob = NULL, *href_blame = NULL;
3911 const char *class = NULL;
3912 int nentries, i, class_flip = 0;
3913 enum kcgi_err kerr = KCGI_OK;
3915 if (gw_trans->repo_folder != NULL) {
3916 path = strdup(gw_trans->repo_folder);
3918 error = got_error_from_errno("strdup");
3922 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3923 gw_trans->repo_path, 1);
3927 path = in_repo_path;
3930 if (gw_trans->commit_id == NULL) {
3931 struct got_reference *head_ref;
3932 error = got_ref_open(&head_ref, gw_trans->repo,
3933 gw_trans->headref, 0);
3936 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
3939 got_ref_close(head_ref);
3941 * gw_trans->commit_id was not parsed from the querystring
3942 * we hit this code path from gw_index, where we don't know the
3943 * commit values for the tree link yet, so set
3944 * gw_trans->commit_id here to continue further into the tree
3946 error = got_object_id_str(&gw_trans->commit_id, commit_id);
3951 error = got_repo_match_object_id(&commit_id, NULL,
3952 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
3958 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
3963 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
3967 nentries = got_object_tree_get_nentries(tree);
3968 for (i = 0; i < nentries; i++) {
3969 struct got_tree_entry *te;
3970 const char *modestr = "";
3973 te = got_object_tree_get_entry(tree, i);
3975 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3979 mode = got_tree_entry_get_mode(te);
3980 if (got_object_tree_entry_is_submodule(te))
3982 else if (S_ISLNK(mode))
3984 else if (S_ISDIR(mode))
3986 else if (mode & S_IXUSR)
3989 if (class_flip == 0) {
3990 class = "back_lightgray";
3993 class = "back_white";
3997 if (S_ISDIR(mode)) {
3998 if (asprintf(&build_folder, "%s/%s",
3999 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4000 got_tree_entry_get_name(te)) == -1) {
4001 error = got_error_from_errno("asprintf");
4004 if (asprintf(&href_blob,
4005 "?path=%s&action=%s&commit=%s&folder=%s",
4006 gw_trans->repo_name, gw_get_action_name(gw_trans),
4007 gw_trans->commit_id, build_folder) == -1) {
4008 error = got_error_from_errno("asprintf");
4012 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4013 KATTR_ID, "tree_wrapper", KATTR__MAX);
4014 if (kerr != KCGI_OK)
4016 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4017 KATTR_ID, "tree_line", KATTR_CLASS, class,
4019 if (kerr != KCGI_OK)
4021 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4022 KATTR_HREF, href_blob, KATTR_CLASS,
4023 "diff_directory", KATTR__MAX);
4024 if (kerr != KCGI_OK)
4026 kerr = khtml_puts(gw_trans->gw_html_req,
4027 got_tree_entry_get_name(te));
4028 if (kerr != KCGI_OK)
4030 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4031 if (kerr != KCGI_OK)
4033 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4034 if (kerr != KCGI_OK)
4036 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4037 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4039 if (kerr != KCGI_OK)
4041 kerr = khtml_entity(gw_trans->gw_html_req,
4043 if (kerr != KCGI_OK)
4045 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4046 if (kerr != KCGI_OK)
4049 if (asprintf(&href_blob,
4050 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4051 gw_trans->repo_name, "blob", gw_trans->commit_id,
4052 got_tree_entry_get_name(te),
4053 gw_trans->repo_folder ?
4054 gw_trans->repo_folder : "") == -1) {
4055 error = got_error_from_errno("asprintf");
4058 if (asprintf(&href_blame,
4059 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4060 gw_trans->repo_name, "blame", gw_trans->commit_id,
4061 got_tree_entry_get_name(te),
4062 gw_trans->repo_folder ?
4063 gw_trans->repo_folder : "") == -1) {
4064 error = got_error_from_errno("asprintf");
4068 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4069 KATTR_ID, "tree_wrapper", KATTR__MAX);
4070 if (kerr != KCGI_OK)
4072 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4073 KATTR_ID, "tree_line", KATTR_CLASS, class,
4075 if (kerr != KCGI_OK)
4077 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4078 KATTR_HREF, href_blob, KATTR__MAX);
4079 if (kerr != KCGI_OK)
4081 kerr = khtml_puts(gw_trans->gw_html_req,
4082 got_tree_entry_get_name(te));
4083 if (kerr != KCGI_OK)
4085 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4086 if (kerr != KCGI_OK)
4088 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4089 if (kerr != KCGI_OK)
4091 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4092 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4094 if (kerr != KCGI_OK)
4097 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4098 KATTR_HREF, href_blob, KATTR__MAX);
4099 if (kerr != KCGI_OK)
4101 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4102 if (kerr != KCGI_OK)
4104 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4105 if (kerr != KCGI_OK)
4108 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4109 if (kerr != KCGI_OK)
4112 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4113 KATTR_HREF, href_blame, KATTR__MAX);
4114 if (kerr != KCGI_OK)
4116 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4117 if (kerr != KCGI_OK)
4120 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4121 if (kerr != KCGI_OK)
4129 build_folder = NULL;
4133 got_object_tree_close(tree);
4140 if (error == NULL && kerr != KCGI_OK)
4141 error = gw_kcgi_error(kerr);
4145 static const struct got_error *
4146 gw_output_repo_heads(struct gw_trans *gw_trans)
4148 const struct got_error *error = NULL;
4149 struct got_reflist_head refs;
4150 struct got_reflist_entry *re;
4151 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4152 char *href_commits = NULL;
4153 enum kcgi_err kerr = KCGI_OK;
4155 SIMPLEQ_INIT(&refs);
4157 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4158 got_ref_cmp_by_name, NULL);
4162 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4163 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4164 if (kerr != KCGI_OK)
4166 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4167 KATTR_ID, "summary_heads_title", KATTR__MAX);
4168 if (kerr != KCGI_OK)
4170 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4171 if (kerr != KCGI_OK)
4173 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4174 if (kerr != KCGI_OK)
4176 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4177 KATTR_ID, "summary_heads_content", KATTR__MAX);
4178 if (kerr != KCGI_OK)
4181 SIMPLEQ_FOREACH(re, &refs, entry) {
4182 const char *refname;
4184 if (got_ref_is_symbolic(re->ref))
4187 refname = got_ref_get_name(re->ref);
4188 if (strncmp(refname, "refs/heads/", 11) != 0)
4191 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4196 if (strncmp(refname, "refs/heads/", 11) == 0)
4199 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4200 KATTR_ID, "heads_wrapper", KATTR__MAX);
4201 if (kerr != KCGI_OK)
4203 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4204 KATTR_ID, "heads_age", KATTR__MAX);
4205 if (kerr != KCGI_OK)
4207 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4208 if (kerr != KCGI_OK)
4210 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4211 if (kerr != KCGI_OK)
4213 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4214 KATTR_ID, "heads_space", KATTR__MAX);
4215 if (kerr != KCGI_OK)
4217 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4218 if (kerr != KCGI_OK)
4220 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4221 if (kerr != KCGI_OK)
4223 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4224 KATTR_ID, "head", KATTR__MAX);
4225 if (kerr != KCGI_OK)
4227 if (asprintf(&href_summary,
4228 "?path=%s&action=summary&headref=%s",
4229 gw_trans->repo_name, refname) == -1) {
4230 error = got_error_from_errno("asprintf");
4233 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4234 href_summary, KATTR__MAX);
4235 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4236 if (kerr != KCGI_OK)
4238 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4239 if (kerr != KCGI_OK)
4242 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4243 "navs_wrapper", KATTR__MAX);
4244 if (kerr != KCGI_OK)
4246 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4247 "navs", KATTR__MAX);
4248 if (kerr != KCGI_OK)
4251 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4252 href_summary, KATTR__MAX);
4253 if (kerr != KCGI_OK)
4255 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4256 if (kerr != KCGI_OK)
4258 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4259 if (kerr != KCGI_OK)
4262 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4263 if (kerr != KCGI_OK)
4265 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4266 gw_trans->repo_name, refname) == -1) {
4267 error = got_error_from_errno("asprintf");
4270 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4271 href_briefs, KATTR__MAX);
4272 if (kerr != KCGI_OK)
4274 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4275 if (kerr != KCGI_OK)
4277 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4278 if (kerr != KCGI_OK)
4281 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4282 if (kerr != KCGI_OK)
4285 if (asprintf(&href_commits,
4286 "?path=%s&action=commits&headref=%s",
4287 gw_trans->repo_name, refname) == -1) {
4288 error = got_error_from_errno("asprintf");
4291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4292 href_commits, KATTR__MAX);
4293 if (kerr != KCGI_OK)
4295 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4296 if (kerr != KCGI_OK)
4298 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4299 if (kerr != KCGI_OK)
4302 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4303 "dotted_line", KATTR__MAX);
4304 if (kerr != KCGI_OK)
4306 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4307 if (kerr != KCGI_OK)
4310 href_summary = NULL;
4314 href_commits = NULL;
4317 got_ref_list_free(&refs);
4324 static const struct got_error *
4325 gw_output_site_link(struct gw_trans *gw_trans)
4327 const struct got_error *error = NULL;
4328 char *href_summary = NULL;
4329 enum kcgi_err kerr = KCGI_OK;
4331 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4332 "site_link", KATTR__MAX);
4333 if (kerr != KCGI_OK)
4335 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4337 if (kerr != KCGI_OK)
4339 kerr = khtml_puts(gw_trans->gw_html_req,
4340 gw_trans->gw_conf->got_site_link);
4341 if (kerr != KCGI_OK)
4343 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4344 if (kerr != KCGI_OK)
4347 if (gw_trans->repo_name != NULL) {
4348 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4349 if (kerr != KCGI_OK)
4351 if (asprintf(&href_summary, "?path=%s&action=summary",
4352 gw_trans->repo_name) == -1)
4354 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4355 href_summary, KATTR__MAX);
4356 if (kerr != KCGI_OK)
4358 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4359 if (kerr != KCGI_OK)
4361 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4362 if (kerr != KCGI_OK)
4364 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4365 if (kerr != KCGI_OK)
4367 kerr = khtml_puts(gw_trans->gw_html_req,
4368 gw_get_action_name(gw_trans));
4369 if (kerr != KCGI_OK)
4373 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4374 if (kerr != KCGI_OK)
4381 static const struct got_error *
4382 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4384 const struct got_error *error = NULL;
4386 enum kcgi_err kerr = KCGI_OK;
4388 if (strncmp(buf, "-", 1) == 0)
4389 color = "diff_minus";
4390 else if (strncmp(buf, "+", 1) == 0)
4391 color = "diff_plus";
4392 else if (strncmp(buf, "@@", 2) == 0)
4393 color = "diff_chunk_header";
4394 else if (strncmp(buf, "@@", 2) == 0)
4395 color = "diff_chunk_header";
4396 else if (strncmp(buf, "commit +", 8) == 0)
4397 color = "diff_meta";
4398 else if (strncmp(buf, "commit -", 8) == 0)
4399 color = "diff_meta";
4400 else if (strncmp(buf, "blob +", 6) == 0)
4401 color = "diff_meta";
4402 else if (strncmp(buf, "blob -", 6) == 0)
4403 color = "diff_meta";
4404 else if (strncmp(buf, "file +", 6) == 0)
4405 color = "diff_meta";
4406 else if (strncmp(buf, "file -", 6) == 0)
4407 color = "diff_meta";
4408 else if (strncmp(buf, "from:", 5) == 0)
4409 color = "diff_author";
4410 else if (strncmp(buf, "via:", 4) == 0)
4411 color = "diff_author";
4412 else if (strncmp(buf, "date:", 5) == 0)
4413 color = "diff_date";
4414 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4415 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4416 if (error == NULL && kerr != KCGI_OK)
4417 error = gw_kcgi_error(kerr);
4422 main(int argc, char *argv[])
4424 const struct got_error *error = NULL;
4425 struct gw_trans *gw_trans;
4426 struct gw_dir *dir = NULL, *tdir;
4427 const char *page = "index";
4431 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4434 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4437 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4440 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4443 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4444 if (kerr != KCGI_OK) {
4445 error = gw_kcgi_error(kerr);
4449 if ((gw_trans->gw_conf =
4450 malloc(sizeof(struct gotweb_conf))) == NULL) {
4452 error = got_error_from_errno("malloc");
4456 TAILQ_INIT(&gw_trans->gw_dirs);
4457 TAILQ_INIT(&gw_trans->gw_headers);
4459 gw_trans->action = -1;
4461 gw_trans->repos_total = 0;
4462 gw_trans->repo_path = NULL;
4463 gw_trans->commit_id = NULL;
4464 gw_trans->next_id = NULL;
4465 gw_trans->next_prev_id = NULL;
4466 gw_trans->prev_id = NULL;
4467 gw_trans->prev_prev_id = NULL;
4468 gw_trans->headref = GOT_REF_HEAD;
4469 gw_trans->mime = KMIME_TEXT_HTML;
4470 gw_trans->gw_tmpl->key = gw_templs;
4471 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4472 gw_trans->gw_tmpl->arg = gw_trans;
4473 gw_trans->gw_tmpl->cb = gw_template;
4474 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4478 error = gw_parse_querystring(gw_trans);
4482 if (gw_trans->action == GW_BLOB)
4483 error = gw_blob(gw_trans);
4485 error = gw_display_index(gw_trans);
4488 free(gw_trans->gw_conf->got_repos_path);
4489 free(gw_trans->gw_conf->got_site_name);
4490 free(gw_trans->gw_conf->got_site_owner);
4491 free(gw_trans->gw_conf->got_site_link);
4492 free(gw_trans->gw_conf->got_logo);
4493 free(gw_trans->gw_conf->got_logo_url);
4494 free(gw_trans->gw_conf);
4495 free(gw_trans->commit_id);
4496 free(gw_trans->next_id);
4497 free(gw_trans->next_prev_id);
4498 free(gw_trans->prev_id);
4499 free(gw_trans->prev_prev_id);
4500 free(gw_trans->repo_path);
4502 got_repo_close(gw_trans->repo);
4504 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4506 free(dir->description);
4515 khttp_free(gw_trans->gw_req);