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 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
833 KATTR_ID, "np_wrapper", KATTR__MAX);
836 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
837 KATTR_ID, "nav_prev", KATTR__MAX);
840 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
841 (gw_trans->page > 0) &&
842 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
843 prev_disp == gw_trans->repos_total)) {
844 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
845 KATTR_ID, "np_wrapper", KATTR__MAX);
848 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
849 KATTR_ID, "nav_prev", KATTR__MAX);
854 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
855 (gw_trans->page > 0) &&
856 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
857 prev_disp == gw_trans->repos_total)) {
858 if (asprintf(&href_prev, "?page=%d",
859 gw_trans->page - 1) == -1) {
860 error = got_error_from_errno("asprintf");
863 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
864 KATTR_HREF, href_prev, KATTR__MAX);
867 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
870 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
875 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
877 return gw_kcgi_error(kerr);
879 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
880 next_disp == gw_trans->gw_conf->got_max_repos_display &&
881 dir_c != (gw_trans->page + 1) *
882 gw_trans->gw_conf->got_max_repos_display) {
883 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
884 KATTR_ID, "nav_next", KATTR__MAX);
887 if (asprintf(&href_next, "?page=%d",
888 gw_trans->page + 1) == -1) {
889 error = got_error_from_errno("calloc");
892 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
893 KATTR_HREF, href_next, KATTR__MAX);
896 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
899 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
906 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
907 (gw_trans->page > 0) &&
908 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
909 prev_disp == gw_trans->repos_total)) {
910 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
923 if (error == NULL && kerr != KCGI_OK)
924 error = gw_kcgi_error(kerr);
928 static const struct got_error *
929 gw_commits(struct gw_trans *gw_trans)
931 const struct got_error *error = NULL;
932 struct gw_header *header = NULL, *n_header = NULL;
933 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
934 char *href_prev = NULL, *href_next = NULL;
935 enum kcgi_err kerr = KCGI_OK;
937 if ((header = gw_init_header()) == NULL)
938 return got_error_from_errno("malloc");
940 if (pledge("stdio rpath proc exec sendfd unveil",
942 error = got_error_from_errno("pledge");
946 error = gw_apply_unveil(gw_trans->gw_dir->path);
950 error = gw_get_header(gw_trans, header,
951 gw_trans->gw_conf->got_max_commits_display);
955 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
956 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
957 "commits_line_wrapper", KATTR__MAX);
960 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
964 error = gw_gen_author_header(gw_trans, n_header->author);
967 error = gw_gen_committer_header(gw_trans, n_header->author);
970 error = gw_get_time_str(&age, n_header->committer_time,
974 error = gw_gen_age_header(gw_trans, age ?age : "");
977 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
981 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
982 "dotted_line", KATTR__MAX);
985 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
989 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
990 "commit", KATTR__MAX);
993 kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
996 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1000 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1001 gw_trans->repo_name, n_header->commit_id) == -1) {
1002 error = got_error_from_errno("asprintf");
1005 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1006 KATTR_ID, "navs_wrapper", KATTR__MAX);
1007 if (kerr != KCGI_OK)
1009 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1010 KATTR_ID, "navs", KATTR__MAX);
1011 if (kerr != KCGI_OK)
1013 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1014 KATTR_HREF, href_diff, KATTR__MAX);
1015 if (kerr != KCGI_OK)
1017 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1018 if (kerr != KCGI_OK)
1020 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1021 if (kerr != KCGI_OK)
1024 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1025 if (kerr != KCGI_OK)
1028 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1029 gw_trans->repo_name, n_header->commit_id) == -1) {
1030 error = got_error_from_errno("asprintf");
1033 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1034 KATTR_HREF, href_blob, KATTR__MAX);
1035 if (kerr != KCGI_OK)
1037 khtml_puts(gw_trans->gw_html_req, "tree");
1038 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1039 if (kerr != KCGI_OK)
1041 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1042 if (kerr != KCGI_OK)
1045 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1046 "solid_line", KATTR__MAX);
1047 if (kerr != KCGI_OK)
1049 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1050 if (kerr != KCGI_OK)
1057 if (gw_trans->next_id || gw_trans->page > 0) {
1058 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1059 KATTR_ID, "np_wrapper", KATTR__MAX);
1060 if (kerr != KCGI_OK)
1062 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1063 KATTR_ID, "nav_prev", KATTR__MAX);
1064 if (kerr != KCGI_OK)
1068 if (gw_trans->page > 0) {
1069 if (asprintf(&href_prev,
1070 "?path=%s&page=%d&action=commits&commit=%s&prev=%s",
1071 gw_trans->repo_name, gw_trans->page - 1,
1072 gw_trans->prev_id ? gw_trans->prev_id : "",
1073 gw_trans->prev_prev_id ?
1074 gw_trans->prev_prev_id : "") == -1) {
1075 error = got_error_from_errno("asprintf");
1078 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1079 KATTR_HREF, href_prev, KATTR__MAX);
1080 if (kerr != KCGI_OK)
1082 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1083 if (kerr != KCGI_OK)
1085 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1086 if (kerr != KCGI_OK)
1090 if (gw_trans->next_id || gw_trans->page > 0) {
1091 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1092 if (kerr != KCGI_OK)
1093 return gw_kcgi_error(kerr);
1096 if (gw_trans->next_id) {
1097 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1098 KATTR_ID, "nav_next", KATTR__MAX);
1099 if (kerr != KCGI_OK)
1101 if (asprintf(&href_next,
1102 "?path=%s&page=%d&action=commits" \
1103 "&commit=%s&prev=%s&prev_prev=%s",
1104 gw_trans->repo_name, gw_trans->page + 1,
1106 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1108 gw_trans->prev_id : "") == -1) {
1109 error = got_error_from_errno("calloc");
1112 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1113 KATTR_HREF, href_next, KATTR__MAX);
1114 if (kerr != KCGI_OK)
1116 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1117 if (kerr != KCGI_OK)
1119 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1120 if (kerr != KCGI_OK)
1124 if (gw_trans->next_id || gw_trans->page > 0) {
1125 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1126 if (kerr != KCGI_OK)
1130 gw_free_header(header);
1131 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1132 gw_free_header(n_header);
1138 if (error == NULL && kerr != KCGI_OK)
1139 error = gw_kcgi_error(kerr);
1143 static const struct got_error *
1144 gw_briefs(struct gw_trans *gw_trans)
1146 const struct got_error *error = NULL;
1147 struct gw_header *header = NULL, *n_header = NULL;
1148 char *age = NULL, *href_diff = NULL, *href_blob = NULL;
1149 char *href_prev = NULL, *href_next = NULL;
1150 char *newline, *smallerthan;
1151 enum kcgi_err kerr = KCGI_OK;
1153 if ((header = gw_init_header()) == NULL)
1154 return got_error_from_errno("malloc");
1156 if (pledge("stdio rpath proc exec sendfd unveil",
1158 error = got_error_from_errno("pledge");
1162 if (gw_trans->action != GW_SUMMARY) {
1163 error = gw_apply_unveil(gw_trans->gw_dir->path);
1168 if (gw_trans->action == GW_SUMMARY)
1169 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1171 error = gw_get_header(gw_trans, header,
1172 gw_trans->gw_conf->got_max_commits_display);
1176 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1177 error = gw_get_time_str(&age, n_header->committer_time,
1182 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1183 KATTR_ID, "briefs_wrapper", KATTR__MAX);
1184 if (kerr != KCGI_OK)
1187 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1188 KATTR_ID, "briefs_age", KATTR__MAX);
1189 if (kerr != KCGI_OK)
1191 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1192 if (kerr != KCGI_OK)
1194 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1195 if (kerr != KCGI_OK)
1198 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1199 KATTR_ID, "briefs_author", KATTR__MAX);
1200 if (kerr != KCGI_OK)
1202 smallerthan = strchr(n_header->author, '<');
1204 *smallerthan = '\0';
1205 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1206 if (kerr != KCGI_OK)
1208 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1209 if (kerr != KCGI_OK)
1212 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1213 gw_trans->repo_name, n_header->commit_id) == -1) {
1214 error = got_error_from_errno("asprintf");
1217 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1218 KATTR_ID, "briefs_log", KATTR__MAX);
1219 if (kerr != KCGI_OK)
1221 newline = strchr(n_header->commit_msg, '\n');
1224 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1225 KATTR_HREF, href_diff, KATTR__MAX);
1226 if (kerr != KCGI_OK)
1228 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1229 if (kerr != KCGI_OK)
1231 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1232 if (kerr != KCGI_OK)
1235 if (n_header->refs_str) {
1236 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1237 if (kerr != KCGI_OK)
1239 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1240 KATTR_ID, "refs_str", KATTR__MAX);
1241 if (kerr != KCGI_OK)
1243 kerr = khtml_puts(gw_trans->gw_html_req, "(");
1244 if (kerr != KCGI_OK)
1246 kerr = khtml_puts(gw_trans->gw_html_req,
1247 n_header->refs_str);
1248 if (kerr != KCGI_OK)
1250 kerr = khtml_puts(gw_trans->gw_html_req, ")");
1251 if (kerr != KCGI_OK)
1253 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1254 if (kerr != KCGI_OK)
1258 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1259 if (kerr != KCGI_OK)
1262 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1263 KATTR_ID, "navs_wrapper", KATTR__MAX);
1264 if (kerr != KCGI_OK)
1266 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1267 KATTR_ID, "navs", KATTR__MAX);
1268 if (kerr != KCGI_OK)
1270 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1271 KATTR_HREF, href_diff, KATTR__MAX);
1272 if (kerr != KCGI_OK)
1274 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1275 if (kerr != KCGI_OK)
1277 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1278 if (kerr != KCGI_OK)
1281 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1282 if (kerr != KCGI_OK)
1285 if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1286 gw_trans->repo_name, n_header->commit_id) == -1) {
1287 error = got_error_from_errno("asprintf");
1290 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1291 KATTR_HREF, href_blob, KATTR__MAX);
1292 if (kerr != KCGI_OK)
1294 khtml_puts(gw_trans->gw_html_req, "tree");
1295 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1296 if (kerr != KCGI_OK)
1298 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1299 if (kerr != KCGI_OK)
1302 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1303 KATTR_ID, "dotted_line", KATTR__MAX);
1304 if (kerr != KCGI_OK)
1306 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1307 if (kerr != KCGI_OK)
1318 if (gw_trans->next_id || gw_trans->page > 0) {
1319 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1320 KATTR_ID, "np_wrapper", KATTR__MAX);
1321 if (kerr != KCGI_OK)
1323 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1324 KATTR_ID, "nav_prev", KATTR__MAX);
1325 if (kerr != KCGI_OK)
1329 if (gw_trans->page > 0) {
1330 if (asprintf(&href_prev,
1331 "?path=%s&page=%d&action=briefs&commit=%s&prev=%s",
1332 gw_trans->repo_name, gw_trans->page - 1,
1333 gw_trans->prev_id ? gw_trans->prev_id : "",
1334 gw_trans->prev_prev_id ?
1335 gw_trans->prev_prev_id : "") == -1) {
1336 error = got_error_from_errno("asprintf");
1339 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1340 KATTR_HREF, href_prev, KATTR__MAX);
1341 if (kerr != KCGI_OK)
1343 kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1344 if (kerr != KCGI_OK)
1346 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1347 if (kerr != KCGI_OK)
1351 if (gw_trans->next_id || gw_trans->page > 0) {
1352 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1353 if (kerr != KCGI_OK)
1354 return gw_kcgi_error(kerr);
1357 if (gw_trans->next_id) {
1358 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1359 KATTR_ID, "nav_next", KATTR__MAX);
1360 if (kerr != KCGI_OK)
1362 if (asprintf(&href_next,
1363 "?path=%s&page=%d&action=briefs" \
1364 "&commit=%s&prev=%s&prev_prev=%s",
1365 gw_trans->repo_name, gw_trans->page + 1,
1367 gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1369 gw_trans->prev_id : "") == -1) {
1370 error = got_error_from_errno("calloc");
1373 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1374 KATTR_HREF, href_next, KATTR__MAX);
1375 if (kerr != KCGI_OK)
1377 kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1378 if (kerr != KCGI_OK)
1380 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1381 if (kerr != KCGI_OK)
1385 if (gw_trans->next_id || gw_trans->page > 0) {
1386 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1387 if (kerr != KCGI_OK)
1391 gw_free_header(header);
1392 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1393 gw_free_header(n_header);
1399 if (error == NULL && kerr != KCGI_OK)
1400 error = gw_kcgi_error(kerr);
1404 static const struct got_error *
1405 gw_summary(struct gw_trans *gw_trans)
1407 const struct got_error *error = NULL;
1409 enum kcgi_err kerr = KCGI_OK;
1411 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1412 return got_error_from_errno("pledge");
1414 error = gw_apply_unveil(gw_trans->gw_dir->path);
1418 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1419 "summary_wrapper", KATTR__MAX);
1420 if (kerr != KCGI_OK)
1421 return gw_kcgi_error(kerr);
1423 if (gw_trans->gw_conf->got_show_repo_description &&
1424 gw_trans->gw_dir->description != NULL &&
1425 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1426 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1427 KATTR_ID, "description_title", KATTR__MAX);
1428 if (kerr != KCGI_OK)
1430 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1431 if (kerr != KCGI_OK)
1433 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1434 if (kerr != KCGI_OK)
1436 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1437 KATTR_ID, "description", KATTR__MAX);
1438 if (kerr != KCGI_OK)
1440 kerr = khtml_puts(gw_trans->gw_html_req,
1441 gw_trans->gw_dir->description);
1442 if (kerr != KCGI_OK)
1444 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1445 if (kerr != KCGI_OK)
1449 if (gw_trans->gw_conf->got_show_repo_owner &&
1450 gw_trans->gw_dir->owner != NULL &&
1451 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1452 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1453 KATTR_ID, "repo_owner_title", KATTR__MAX);
1454 if (kerr != KCGI_OK)
1456 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1457 if (kerr != KCGI_OK)
1459 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1460 if (kerr != KCGI_OK)
1462 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1463 KATTR_ID, "repo_owner", KATTR__MAX);
1464 if (kerr != KCGI_OK)
1466 kerr = khtml_puts(gw_trans->gw_html_req,
1467 gw_trans->gw_dir->owner);
1468 if (kerr != KCGI_OK)
1470 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1471 if (kerr != KCGI_OK)
1475 if (gw_trans->gw_conf->got_show_repo_age) {
1476 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1481 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1482 KATTR_ID, "last_change_title", KATTR__MAX);
1483 if (kerr != KCGI_OK)
1485 kerr = khtml_puts(gw_trans->gw_html_req,
1487 if (kerr != KCGI_OK)
1489 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1490 if (kerr != KCGI_OK)
1492 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1493 KATTR_ID, "last_change", KATTR__MAX);
1494 if (kerr != KCGI_OK)
1496 kerr = khtml_puts(gw_trans->gw_html_req, age);
1497 if (kerr != KCGI_OK)
1499 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1500 if (kerr != KCGI_OK)
1505 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1506 gw_trans->gw_dir->url != NULL &&
1507 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1508 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1509 KATTR_ID, "cloneurl_title", KATTR__MAX);
1510 if (kerr != KCGI_OK)
1512 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1513 if (kerr != KCGI_OK)
1515 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1516 if (kerr != KCGI_OK)
1518 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1519 KATTR_ID, "cloneurl", KATTR__MAX);
1520 if (kerr != KCGI_OK)
1522 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1523 if (kerr != KCGI_OK)
1525 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1526 if (kerr != KCGI_OK)
1530 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1531 if (kerr != KCGI_OK)
1534 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1535 "briefs_title_wrapper", KATTR__MAX);
1536 if (kerr != KCGI_OK)
1538 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1539 "briefs_title", KATTR__MAX);
1540 if (kerr != KCGI_OK)
1542 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1543 if (kerr != KCGI_OK)
1545 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1546 if (kerr != KCGI_OK)
1548 error = gw_briefs(gw_trans);
1552 error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1557 error = gw_output_repo_heads(gw_trans);
1560 if (error == NULL && kerr != KCGI_OK)
1561 error = gw_kcgi_error(kerr);
1565 static const struct got_error *
1566 gw_tree(struct gw_trans *gw_trans)
1568 const struct got_error *error = NULL;
1569 struct gw_header *header = NULL;
1570 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1574 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1575 return got_error_from_errno("pledge");
1577 if ((header = gw_init_header()) == NULL)
1578 return got_error_from_errno("malloc");
1580 error = gw_apply_unveil(gw_trans->gw_dir->path);
1584 error = gw_get_header(gw_trans, header, 1);
1588 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1589 "tree_header_wrapper", KATTR__MAX);
1590 if (kerr != KCGI_OK)
1592 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1593 "tree_header", KATTR__MAX);
1594 if (kerr != KCGI_OK)
1596 error = gw_gen_tree_header(gw_trans, header->tree_id);
1599 error = gw_get_time_str(&age, header->committer_time,
1603 error = gw_gen_age_header(gw_trans, age ?age : "");
1606 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1609 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1610 if (kerr != KCGI_OK)
1612 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1613 "dotted_line", KATTR__MAX);
1614 if (kerr != KCGI_OK)
1616 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1617 if (kerr != KCGI_OK)
1620 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1621 "tree", KATTR__MAX);
1622 if (kerr != KCGI_OK)
1624 error = gw_output_repo_tree(gw_trans);
1628 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1630 gw_free_header(header);
1631 free(tree_html_disp);
1635 if (error == NULL && kerr != KCGI_OK)
1636 error = gw_kcgi_error(kerr);
1640 static const struct got_error *
1641 gw_tag(struct gw_trans *gw_trans)
1643 const struct got_error *error = NULL;
1644 struct gw_header *header = NULL;
1647 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1648 return got_error_from_errno("pledge");
1650 if ((header = gw_init_header()) == NULL)
1651 return got_error_from_errno("malloc");
1653 error = gw_apply_unveil(gw_trans->gw_dir->path);
1657 if (gw_trans->commit_id == NULL) {
1658 error = got_error_msg(GOT_ERR_QUERYSTRING,
1659 "commit required in querystring");
1663 error = gw_get_header(gw_trans, header, 1);
1667 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1668 "tag_header_wrapper", KATTR__MAX);
1669 if (kerr != KCGI_OK)
1671 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1672 "tag_header", KATTR__MAX);
1673 if (kerr != KCGI_OK)
1675 error = gw_gen_commit_header(gw_trans, header->commit_id,
1679 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1682 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1683 if (kerr != KCGI_OK)
1685 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1686 "dotted_line", KATTR__MAX);
1687 if (kerr != KCGI_OK)
1689 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1690 if (kerr != KCGI_OK)
1693 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1694 "tree", KATTR__MAX);
1695 if (kerr != KCGI_OK)
1698 error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1702 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1704 gw_free_header(header);
1705 if (error == NULL && kerr != KCGI_OK)
1706 error = gw_kcgi_error(kerr);
1710 static const struct got_error *
1711 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1713 const struct got_error *error = NULL;
1718 if (asprintf(&dir_test, "%s/%s/%s",
1719 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1720 GOTWEB_GIT_DIR) == -1)
1721 return got_error_from_errno("asprintf");
1723 dt = opendir(dir_test);
1727 gw_dir->path = strdup(dir_test);
1728 if (gw_dir->path == NULL) {
1730 error = got_error_from_errno("strdup");
1737 if (asprintf(&dir_test, "%s/%s/%s",
1738 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1739 GOTWEB_GOT_DIR) == -1) {
1741 error = got_error_from_errno("asprintf");
1745 dt = opendir(dir_test);
1750 error = got_error(GOT_ERR_NOT_GIT_REPO);
1754 if (asprintf(&dir_test, "%s/%s",
1755 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1756 error = got_error_from_errno("asprintf");
1761 gw_dir->path = strdup(dir_test);
1762 if (gw_dir->path == NULL) {
1764 error = got_error_from_errno("strdup");
1768 dt = opendir(dir_test);
1770 error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1775 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1779 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1782 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1786 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1790 if (dt && closedir(dt) == -1 && error == NULL)
1791 error = got_error_from_errno("closedir");
1795 static const struct got_error *
1796 gw_load_got_paths(struct gw_trans *gw_trans)
1798 const struct got_error *error = NULL;
1800 struct dirent **sd_dent;
1801 struct gw_dir *gw_dir;
1803 unsigned int d_cnt, d_i;
1805 d = opendir(gw_trans->gw_conf->got_repos_path);
1807 error = got_error_from_errno2("opendir",
1808 gw_trans->gw_conf->got_repos_path);
1812 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1815 error = got_error_from_errno2("scandir",
1816 gw_trans->gw_conf->got_repos_path);
1820 for (d_i = 0; d_i < d_cnt; d_i++) {
1821 if (gw_trans->gw_conf->got_max_repos > 0 &&
1822 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1823 break; /* account for parent and self */
1825 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1826 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1829 error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1833 error = gw_load_got_path(gw_trans, gw_dir);
1834 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1841 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1842 !got_path_dir_is_empty(gw_dir->path)) {
1843 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1845 gw_trans->repos_total++;
1849 if (d && closedir(d) == -1 && error == NULL)
1850 error = got_error_from_errno("closedir");
1854 static const struct got_error *
1855 gw_parse_querystring(struct gw_trans *gw_trans)
1857 const struct got_error *error = NULL;
1859 struct gw_query_action *action = NULL;
1862 if (gw_trans->gw_req->fieldnmap[0]) {
1863 return got_error(GOT_ERR_QUERYSTRING);
1864 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1865 /* define gw_trans->repo_path */
1866 gw_trans->repo_name = p->parsed.s;
1868 if (asprintf(&gw_trans->repo_path, "%s/%s",
1869 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1870 return got_error_from_errno("asprintf");
1872 /* get action and set function */
1873 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1874 for (i = 0; i < nitems(gw_query_funcs); i++) {
1875 action = &gw_query_funcs[i];
1876 if (action->func_name == NULL)
1878 if (strcmp(action->func_name,
1879 p->parsed.s) == 0) {
1880 gw_trans->action = i;
1885 if (gw_trans->action == -1) {
1886 gw_trans->action = GW_ERR;
1887 gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
1888 p != NULL ? "bad action in querystring" :
1889 "no action in querystring");
1893 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1894 if (asprintf(&gw_trans->commit_id, "%s",
1896 return got_error_from_errno("asprintf");
1899 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1900 gw_trans->repo_file = p->parsed.s;
1902 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1903 if (asprintf(&gw_trans->repo_folder, "%s",
1905 return got_error_from_errno("asprintf");
1908 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
1909 if (asprintf(&gw_trans->prev_id, "%s",
1911 return got_error_from_errno("asprintf");
1914 if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
1915 if (asprintf(&gw_trans->prev_prev_id, "%s",
1917 return got_error_from_errno("asprintf");
1920 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1921 gw_trans->headref = p->parsed.s;
1923 error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1927 gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1929 gw_trans->action = GW_INDEX;
1931 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1932 gw_trans->page = p->parsed.i;
1937 static const struct got_error *
1938 gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1940 const struct got_error *error;
1942 *gw_dir = malloc(sizeof(**gw_dir));
1943 if (*gw_dir == NULL)
1944 return got_error_from_errno("malloc");
1946 if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1947 error = got_error_from_errno("asprintf");
1956 static const struct got_error *
1957 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1961 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1962 if (kerr != KCGI_OK)
1963 return gw_kcgi_error(kerr);
1964 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1966 if (kerr != KCGI_OK)
1967 return gw_kcgi_error(kerr);
1968 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1970 if (kerr != KCGI_OK)
1971 return gw_kcgi_error(kerr);
1972 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1974 if (kerr != KCGI_OK)
1975 return gw_kcgi_error(kerr);
1976 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1977 if (kerr != KCGI_OK)
1978 return gw_kcgi_error(kerr);
1979 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1981 if (kerr != KCGI_OK)
1982 return gw_kcgi_error(kerr);
1984 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1985 kerr = khttp_head(gw_trans->gw_req,
1986 kresps[KRESP_CONTENT_DISPOSITION],
1987 "attachment; filename=%s", gw_trans->repo_file);
1988 if (kerr != KCGI_OK)
1989 return gw_kcgi_error(kerr);
1992 kerr = khttp_body(gw_trans->gw_req);
1993 return gw_kcgi_error(kerr);
1996 static const struct got_error *
1997 gw_display_index(struct gw_trans *gw_trans)
1999 const struct got_error *error;
2002 /* catch early querystring errors */
2003 if (gw_trans->error)
2004 gw_trans->action = GW_ERR;
2006 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2010 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2011 if (kerr != KCGI_OK)
2012 return gw_kcgi_error(kerr);
2014 if (gw_trans->action != GW_BLOB) {
2015 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2016 gw_query_funcs[gw_trans->action].template);
2017 if (kerr != KCGI_OK) {
2018 khtml_close(gw_trans->gw_html_req);
2019 return gw_kcgi_error(kerr);
2023 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2026 static const struct got_error *
2027 gw_error(struct gw_trans *gw_trans)
2031 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2033 return gw_kcgi_error(kerr);
2037 gw_template(size_t key, void *arg)
2039 const struct got_error *error = NULL;
2041 struct gw_trans *gw_trans = arg;
2042 char *img_src = NULL;
2046 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2047 KATTR_NAME, "viewport",
2048 KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
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_CHARSET, "utf-8",
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, "msapplication-TileColor",
2065 KATTR_CONTENT, "#da532c", 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_META,
2072 KATTR_NAME, "theme-color",
2073 KATTR_CONTENT, "#ffffff", 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, "apple-touch-icon", KATTR_SIZES, "180x180",
2081 KATTR_HREF, "/apple-touch-icon.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 "32x32", KATTR_HREF, "/favicon-32x32.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, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2097 "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
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, "manifest", KATTR_HREF, "/site.webmanifest",
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, "mask-icon", KATTR_HREF,
2113 "/safari-pinned-tab.svg", KATTR__MAX);
2114 if (kerr != KCGI_OK)
2116 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2117 if (kerr != KCGI_OK)
2119 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2120 KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2121 KATTR_HREF, "/gotweb.css", KATTR__MAX);
2122 if (kerr != KCGI_OK)
2124 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2125 if (kerr != KCGI_OK)
2129 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2130 KATTR_ID, "got_link", KATTR__MAX);
2131 if (kerr != KCGI_OK)
2133 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2134 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2135 KATTR_TARGET, "_sotd", KATTR__MAX);
2136 if (kerr != KCGI_OK)
2138 if (asprintf(&img_src, "/%s",
2139 gw_trans->gw_conf->got_logo) == -1)
2141 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2142 KATTR_SRC, img_src, KATTR__MAX);
2143 if (kerr != KCGI_OK) {
2147 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2148 if (kerr != KCGI_OK) {
2153 case (TEMPL_SITEPATH):
2154 error = gw_output_site_link(gw_trans);
2159 if (gw_trans->gw_conf->got_site_name != NULL) {
2160 kerr = khtml_puts(gw_trans->gw_html_req,
2161 gw_trans->gw_conf->got_site_name);
2162 if (kerr != KCGI_OK)
2166 case (TEMPL_SEARCH):
2168 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2169 "search", KATTR__MAX);
2170 if (kerr != KCGI_OK)
2172 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2173 KATTR_METHOD, "POST", KATTR__MAX);
2174 if (kerr != KCGI_OK)
2176 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2177 "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2178 KATTR_MAXLENGTH, "50", KATTR__MAX);
2179 if (kerr != KCGI_OK)
2181 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2183 if (kerr != KCGI_OK)
2185 kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2186 if (kerr != KCGI_OK)
2188 kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2189 if (kerr != KCGI_OK)
2192 case(TEMPL_SITEOWNER):
2193 if (gw_trans->gw_conf->got_site_owner != NULL &&
2194 gw_trans->gw_conf->got_show_site_owner) {
2195 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2196 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2197 if (kerr != KCGI_OK)
2199 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2200 KATTR_ID, "site_owner", KATTR__MAX);
2201 if (kerr != KCGI_OK)
2203 kerr = khtml_puts(gw_trans->gw_html_req,
2204 gw_trans->gw_conf->got_site_owner);
2205 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2206 if (kerr != KCGI_OK)
2210 case(TEMPL_CONTENT):
2211 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2213 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2214 KATTR_ID, "tmpl_err", KATTR__MAX);
2215 if (kerr != KCGI_OK)
2217 kerr = khttp_puts(gw_trans->gw_req, "Error: ");
2218 if (kerr != KCGI_OK)
2220 kerr = khttp_puts(gw_trans->gw_req, error->msg);
2221 if (kerr != KCGI_OK)
2223 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2224 if (kerr != KCGI_OK)
2234 static const struct got_error *
2235 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2237 const struct got_error *error = NULL;
2238 enum kcgi_err kerr = KCGI_OK;
2240 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2241 KATTR_ID, "header_commit_title", KATTR__MAX);
2242 if (kerr != KCGI_OK)
2244 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2245 if (kerr != KCGI_OK)
2247 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2248 if (kerr != KCGI_OK)
2250 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2251 KATTR_ID, "header_commit", KATTR__MAX);
2252 if (kerr != KCGI_OK)
2254 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2255 if (kerr != KCGI_OK)
2257 kerr = khtml_puts(gw_trans->gw_html_req, " ");
2258 if (kerr != KCGI_OK)
2261 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2262 KATTR_ID, "refs_str", KATTR__MAX);
2263 if (kerr != KCGI_OK)
2265 kerr = khtml_puts(gw_trans->gw_html_req, "(");
2266 if (kerr != KCGI_OK)
2268 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2269 if (kerr != KCGI_OK)
2271 kerr = khtml_puts(gw_trans->gw_html_req, ")");
2272 if (kerr != KCGI_OK)
2274 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2275 if (kerr != KCGI_OK)
2278 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2280 if (error == NULL && kerr != KCGI_OK)
2281 error = gw_kcgi_error(kerr);
2285 static const struct got_error *
2286 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2288 const struct got_error *error = NULL;
2289 enum kcgi_err kerr = KCGI_OK;
2291 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2292 KATTR_ID, "header_diff_title", KATTR__MAX);
2293 if (kerr != KCGI_OK)
2295 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2296 if (kerr != KCGI_OK)
2298 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2299 if (kerr != KCGI_OK)
2301 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2302 KATTR_ID, "header_diff", KATTR__MAX);
2303 if (kerr != KCGI_OK)
2306 kerr = khtml_puts(gw_trans->gw_html_req, str1);
2307 if (kerr != KCGI_OK)
2310 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2311 if (kerr != KCGI_OK)
2313 kerr = khtml_puts(gw_trans->gw_html_req, str2);
2314 if (kerr != KCGI_OK)
2316 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2318 if (error == NULL && kerr != KCGI_OK)
2319 error = gw_kcgi_error(kerr);
2323 static const struct got_error *
2324 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2326 const struct got_error *error = NULL;
2327 enum kcgi_err kerr = KCGI_OK;
2329 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2330 KATTR_ID, "header_age_title", KATTR__MAX);
2331 if (kerr != KCGI_OK)
2333 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2334 if (kerr != KCGI_OK)
2336 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2337 if (kerr != KCGI_OK)
2339 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2340 KATTR_ID, "header_age", KATTR__MAX);
2341 if (kerr != KCGI_OK)
2343 kerr = khtml_puts(gw_trans->gw_html_req, str);
2344 if (kerr != KCGI_OK)
2346 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2348 if (error == NULL && kerr != KCGI_OK)
2349 error = gw_kcgi_error(kerr);
2353 static const struct got_error *
2354 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2356 const struct got_error *error = NULL;
2359 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2360 KATTR_ID, "header_author_title", KATTR__MAX);
2361 if (kerr != KCGI_OK)
2363 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2364 if (kerr != KCGI_OK)
2366 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2367 if (kerr != KCGI_OK)
2369 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2370 KATTR_ID, "header_author", KATTR__MAX);
2371 if (kerr != KCGI_OK)
2373 kerr = khtml_puts(gw_trans->gw_html_req, str);
2374 if (kerr != KCGI_OK)
2376 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2378 if (error == NULL && kerr != KCGI_OK)
2379 error = gw_kcgi_error(kerr);
2383 static const struct got_error *
2384 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2386 const struct got_error *error = NULL;
2389 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2390 KATTR_ID, "header_committer_title", KATTR__MAX);
2391 if (kerr != KCGI_OK)
2393 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2394 if (kerr != KCGI_OK)
2396 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2397 if (kerr != KCGI_OK)
2399 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2400 KATTR_ID, "header_committer", KATTR__MAX);
2401 if (kerr != KCGI_OK)
2403 kerr = khtml_puts(gw_trans->gw_html_req, str);
2404 if (kerr != KCGI_OK)
2406 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2408 if (error == NULL && kerr != KCGI_OK)
2409 error = gw_kcgi_error(kerr);
2413 static const struct got_error *
2414 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2416 const struct got_error *error = NULL;
2419 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2420 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2421 if (kerr != KCGI_OK)
2423 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2424 if (kerr != KCGI_OK)
2426 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2427 if (kerr != KCGI_OK)
2429 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2430 KATTR_ID, "header_commit_msg", KATTR__MAX);
2431 if (kerr != KCGI_OK)
2433 kerr = khttp_puts(gw_trans->gw_req, str);
2434 if (kerr != KCGI_OK)
2436 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2438 if (error == NULL && kerr != KCGI_OK)
2439 error = gw_kcgi_error(kerr);
2443 static const struct got_error *
2444 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2446 const struct got_error *error = NULL;
2449 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2450 KATTR_ID, "header_tree_title", KATTR__MAX);
2451 if (kerr != KCGI_OK)
2453 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2454 if (kerr != KCGI_OK)
2456 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2457 if (kerr != KCGI_OK)
2459 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2460 KATTR_ID, "header_tree", KATTR__MAX);
2461 if (kerr != KCGI_OK)
2463 kerr = khtml_puts(gw_trans->gw_html_req, str);
2464 if (kerr != KCGI_OK)
2466 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2468 if (error == NULL && kerr != KCGI_OK)
2469 error = gw_kcgi_error(kerr);
2473 static const struct got_error *
2474 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2477 const struct got_error *error = NULL;
2479 char *d_file = NULL;
2483 *description = NULL;
2484 if (gw_trans->gw_conf->got_show_repo_description == 0)
2487 if (asprintf(&d_file, "%s/description", dir) == -1)
2488 return got_error_from_errno("asprintf");
2490 f = fopen(d_file, "r");
2492 if (errno == ENOENT || errno == EACCES)
2494 error = got_error_from_errno2("fopen", d_file);
2498 if (fseek(f, 0, SEEK_END) == -1) {
2499 error = got_ferror(f, GOT_ERR_IO);
2504 error = got_ferror(f, GOT_ERR_IO);
2507 if (fseek(f, 0, SEEK_SET) == -1) {
2508 error = got_ferror(f, GOT_ERR_IO);
2511 *description = calloc(len + 1, sizeof(**description));
2512 if (*description == NULL) {
2513 error = got_error_from_errno("calloc");
2517 n = fread(*description, 1, len, f);
2518 if (n == 0 && ferror(f))
2519 error = got_ferror(f, GOT_ERR_IO);
2521 if (f != NULL && fclose(f) == -1 && error == NULL)
2522 error = got_error_from_errno("fclose");
2527 static const struct got_error *
2528 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2532 char *years = "years ago", *months = "months ago";
2533 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2534 char *minutes = "minutes ago", *seconds = "seconds ago";
2535 char *now = "right now";
2543 diff_time = time(NULL) - committer_time;
2544 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2545 if (asprintf(repo_age, "%lld %s",
2546 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2547 return got_error_from_errno("asprintf");
2548 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2549 if (asprintf(repo_age, "%lld %s",
2550 (diff_time / 60 / 60 / 24 / (365 / 12)),
2552 return got_error_from_errno("asprintf");
2553 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2554 if (asprintf(repo_age, "%lld %s",
2555 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2556 return got_error_from_errno("asprintf");
2557 } else if (diff_time > 60 * 60 * 24 * 2) {
2558 if (asprintf(repo_age, "%lld %s",
2559 (diff_time / 60 / 60 / 24), days) == -1)
2560 return got_error_from_errno("asprintf");
2561 } else if (diff_time > 60 * 60 * 2) {
2562 if (asprintf(repo_age, "%lld %s",
2563 (diff_time / 60 / 60), hours) == -1)
2564 return got_error_from_errno("asprintf");
2565 } else if (diff_time > 60 * 2) {
2566 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2568 return got_error_from_errno("asprintf");
2569 } else if (diff_time > 2) {
2570 if (asprintf(repo_age, "%lld %s", diff_time,
2572 return got_error_from_errno("asprintf");
2574 if (asprintf(repo_age, "%s", now) == -1)
2575 return got_error_from_errno("asprintf");
2579 if (gmtime_r(&committer_time, &tm) == NULL)
2580 return got_error_from_errno("gmtime_r");
2582 s = asctime_r(&tm, datebuf);
2584 return got_error_from_errno("asctime_r");
2586 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2587 return got_error_from_errno("asprintf");
2593 static const struct got_error *
2594 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2595 const char *refname, int ref_tm)
2597 const struct got_error *error = NULL;
2598 struct got_repository *repo = NULL;
2599 struct got_commit_object *commit = NULL;
2600 struct got_reflist_head refs;
2601 struct got_reflist_entry *re;
2602 time_t committer_time = 0, cmp_time = 0;
2605 SIMPLEQ_INIT(&refs);
2607 if (gw_trans->gw_conf->got_show_repo_age == 0)
2611 repo = gw_trans->repo;
2613 error = got_repo_open(&repo, dir, NULL);
2618 error = got_ref_list(&refs, repo, "refs/heads",
2619 got_ref_cmp_by_name, NULL);
2624 * Find the youngest branch tip in the repository, or the age of
2625 * the a specific branch tip if a name was provided by the caller.
2627 SIMPLEQ_FOREACH(re, &refs, entry) {
2628 struct got_object_id *id = NULL;
2630 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2633 error = got_ref_resolve(&id, repo, re->ref);
2637 error = got_object_open_as_commit(&commit, repo, id);
2643 got_object_commit_get_committer_time(commit);
2644 got_object_commit_close(commit);
2645 if (cmp_time < committer_time)
2646 cmp_time = committer_time;
2652 if (cmp_time != 0) {
2653 committer_time = cmp_time;
2654 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2657 got_ref_list_free(&refs);
2658 if (gw_trans->repo == NULL)
2659 got_repo_close(repo);
2663 static const struct got_error *
2664 gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2666 const struct got_error *error;
2668 struct got_object_id *id1 = NULL, *id2 = NULL;
2669 char *label1 = NULL, *label2 = NULL, *line = NULL;
2671 size_t linesize = 0;
2673 enum kcgi_err kerr = KCGI_OK;
2679 if (header->parent_id != NULL &&
2680 strncmp(header->parent_id, "/dev/null", 9) != 0) {
2681 error = got_repo_match_object_id(&id1, &label1,
2682 header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2687 error = got_repo_match_object_id(&id2, &label2,
2688 header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2692 error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2696 case GOT_OBJ_TYPE_BLOB:
2697 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2700 case GOT_OBJ_TYPE_TREE:
2701 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2704 case GOT_OBJ_TYPE_COMMIT:
2705 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2709 error = got_error(GOT_ERR_OBJ_TYPE);
2714 if (fseek(f, 0, SEEK_SET) == -1) {
2715 error = got_ferror(f, GOT_ERR_IO);
2719 while ((linelen = getline(&line, &linesize, f)) != -1) {
2720 error = gw_colordiff_line(gw_trans, line);
2723 /* XXX: KHTML_PRETTY breaks this */
2724 kerr = khtml_puts(gw_trans->gw_html_req, line);
2725 if (kerr != KCGI_OK)
2727 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2728 if (kerr != KCGI_OK)
2731 if (linelen == -1 && ferror(f))
2732 error = got_error_from_errno("getline");
2734 if (f && fclose(f) == -1 && error == NULL)
2735 error = got_error_from_errno("fclose");
2742 if (error == NULL && kerr != KCGI_OK)
2743 error = gw_kcgi_error(kerr);
2747 static const struct got_error *
2748 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2750 const struct got_error *error = NULL;
2751 struct got_repository *repo;
2752 const char *gitconfig_owner;
2756 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2759 error = got_repo_open(&repo, dir, NULL);
2762 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2763 if (gitconfig_owner) {
2764 *owner = strdup(gitconfig_owner);
2766 error = got_error_from_errno("strdup");
2768 got_repo_close(repo);
2772 static const struct got_error *
2773 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2775 const struct got_error *error = NULL;
2777 char *d_file = NULL;
2783 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2784 return got_error_from_errno("asprintf");
2786 f = fopen(d_file, "r");
2788 if (errno != ENOENT && errno != EACCES)
2789 error = got_error_from_errno2("fopen", d_file);
2793 if (fseek(f, 0, SEEK_END) == -1) {
2794 error = got_ferror(f, GOT_ERR_IO);
2799 error = got_ferror(f, GOT_ERR_IO);
2802 if (fseek(f, 0, SEEK_SET) == -1) {
2803 error = got_ferror(f, GOT_ERR_IO);
2807 *url = calloc(len + 1, sizeof(**url));
2809 error = got_error_from_errno("calloc");
2813 n = fread(*url, 1, len, f);
2814 if (n == 0 && ferror(f))
2815 error = got_ferror(f, GOT_ERR_IO);
2817 if (f && fclose(f) == -1 && error == NULL)
2818 error = got_error_from_errno("fclose");
2823 static const struct got_error *
2824 gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2825 int limit, int tag_type)
2827 const struct got_error *error = NULL;
2828 struct got_reflist_head refs;
2829 struct got_reflist_entry *re;
2831 char *id_str = NULL, *newline, *href_commits = NULL;
2832 char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2833 struct got_tag_object *tag = NULL;
2834 enum kcgi_err kerr = KCGI_OK;
2835 int summary_header_displayed = 0;
2837 SIMPLEQ_INIT(&refs);
2839 error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2840 got_ref_cmp_tags, gw_trans->repo);
2844 SIMPLEQ_FOREACH(re, &refs, entry) {
2845 const char *refname;
2847 const char *tag_commit;
2849 struct got_object_id *id;
2850 struct got_commit_object *commit = NULL;
2852 refname = got_ref_get_name(re->ref);
2853 if (strncmp(refname, "refs/tags/", 10) != 0)
2857 error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2861 error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2863 if (error->code != GOT_ERR_OBJ_TYPE) {
2867 /* "lightweight" tag */
2868 error = got_object_open_as_commit(&commit,
2869 gw_trans->repo, id);
2874 tagger = got_object_commit_get_committer(commit);
2876 got_object_commit_get_committer_time(commit);
2877 error = got_object_id_str(&id_str, id);
2881 tagger = got_object_tag_get_tagger(tag);
2882 tagger_time = got_object_tag_get_tagger_time(tag);
2883 error = got_object_id_str(&id_str,
2884 got_object_tag_get_object_id(tag));
2889 if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2890 strlen(id_str)) != 0)
2894 error = got_object_commit_get_logmsg(&tag_commit0,
2898 got_object_commit_close(commit);
2900 tag_commit0 = strdup(got_object_tag_get_message(tag));
2901 if (tag_commit0 == NULL) {
2902 error = got_error_from_errno("strdup");
2907 tag_commit = tag_commit0;
2908 while (*tag_commit == '\n')
2913 newline = strchr(tag_commit, '\n');
2917 if (summary_header_displayed == 0) {
2918 kerr = khtml_attr(gw_trans->gw_html_req,
2919 KELEM_DIV, KATTR_ID,
2920 "summary_tags_title_wrapper", KATTR__MAX);
2921 if (kerr != KCGI_OK)
2923 kerr = khtml_attr(gw_trans->gw_html_req,
2924 KELEM_DIV, KATTR_ID,
2925 "summary_tags_title", KATTR__MAX);
2926 if (kerr != KCGI_OK)
2928 kerr = khtml_puts(gw_trans->gw_html_req,
2930 if (kerr != KCGI_OK)
2932 kerr = khtml_closeelem(gw_trans->gw_html_req,
2934 if (kerr != KCGI_OK)
2936 kerr = khtml_attr(gw_trans->gw_html_req,
2937 KELEM_DIV, KATTR_ID,
2938 "summary_tags_content", KATTR__MAX);
2939 if (kerr != KCGI_OK)
2941 summary_header_displayed = 1;
2944 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2945 KATTR_ID, "tags_wrapper", KATTR__MAX);
2946 if (kerr != KCGI_OK)
2948 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2949 KATTR_ID, "tags_age", KATTR__MAX);
2950 if (kerr != KCGI_OK)
2952 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2955 kerr = khtml_puts(gw_trans->gw_html_req,
2957 if (kerr != KCGI_OK)
2959 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2960 if (kerr != KCGI_OK)
2962 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2963 KATTR_ID, "tags", KATTR__MAX);
2964 if (kerr != KCGI_OK)
2966 kerr = khtml_puts(gw_trans->gw_html_req, refname);
2967 if (kerr != KCGI_OK)
2969 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2970 if (kerr != KCGI_OK)
2972 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2973 KATTR_ID, "tags_name", KATTR__MAX);
2974 if (kerr != KCGI_OK)
2976 if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2977 gw_trans->repo_name, id_str) == -1) {
2978 error = got_error_from_errno("asprintf");
2981 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2982 KATTR_HREF, href_tag, KATTR__MAX);
2983 if (kerr != KCGI_OK)
2985 kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2986 if (kerr != KCGI_OK)
2988 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2989 if (kerr != KCGI_OK)
2992 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2993 KATTR_ID, "navs_wrapper", KATTR__MAX);
2994 if (kerr != KCGI_OK)
2996 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2997 KATTR_ID, "navs", KATTR__MAX);
2998 if (kerr != KCGI_OK)
3001 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3002 KATTR_HREF, href_tag, KATTR__MAX);
3003 if (kerr != KCGI_OK)
3005 kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3006 if (kerr != KCGI_OK)
3008 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3009 if (kerr != KCGI_OK)
3012 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3013 if (kerr != KCGI_OK)
3015 if (asprintf(&href_briefs,
3016 "?path=%s&action=briefs&commit=%s",
3017 gw_trans->repo_name, id_str) == -1) {
3018 error = got_error_from_errno("asprintf");
3021 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3022 KATTR_HREF, href_briefs, KATTR__MAX);
3023 if (kerr != KCGI_OK)
3025 kerr = khtml_puts(gw_trans->gw_html_req,
3027 if (kerr != KCGI_OK)
3029 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3030 if (kerr != KCGI_OK)
3033 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3034 if (kerr != KCGI_OK)
3037 if (asprintf(&href_commits,
3038 "?path=%s&action=commits&commit=%s",
3039 gw_trans->repo_name, id_str) == -1) {
3040 error = got_error_from_errno("asprintf");
3043 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3044 KATTR_HREF, href_commits, KATTR__MAX);
3045 if (kerr != KCGI_OK)
3047 kerr = khtml_puts(gw_trans->gw_html_req,
3049 if (kerr != KCGI_OK)
3051 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3052 if (kerr != KCGI_OK)
3055 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3056 KATTR_ID, "dotted_line", KATTR__MAX);
3057 if (kerr != KCGI_OK)
3059 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3060 if (kerr != KCGI_OK)
3064 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3065 KATTR_ID, "tag_info_date_title", KATTR__MAX);
3066 if (kerr != KCGI_OK)
3068 kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3069 if (kerr != KCGI_OK)
3071 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3072 if (kerr != KCGI_OK)
3074 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3075 KATTR_ID, "tag_info_date", KATTR__MAX);
3076 if (kerr != KCGI_OK)
3078 error = gw_get_time_str(&age, tagger_time, TM_LONG);
3081 kerr = khtml_puts(gw_trans->gw_html_req,
3083 if (kerr != KCGI_OK)
3085 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3086 if (kerr != KCGI_OK)
3089 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3090 KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3091 if (kerr != KCGI_OK)
3093 kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3094 if (kerr != KCGI_OK)
3096 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3097 if (kerr != KCGI_OK)
3099 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3100 KATTR_ID, "tag_info_date", KATTR__MAX);
3101 if (kerr != KCGI_OK)
3103 kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3104 if (kerr != KCGI_OK)
3106 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3107 if (kerr != KCGI_OK)
3110 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3111 KATTR_ID, "tag_info", KATTR__MAX);
3112 if (kerr != KCGI_OK)
3114 kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3115 if (kerr != KCGI_OK)
3121 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3122 if (kerr != KCGI_OK)
3125 if (limit && --limit == 0)
3129 got_object_tag_close(tag);
3142 href_commits = NULL;
3146 got_object_tag_close(tag);
3153 got_ref_list_free(&refs);
3154 if (error == NULL && kerr != KCGI_OK)
3155 error = gw_kcgi_error(kerr);
3160 gw_free_header(struct gw_header *header)
3163 free(header->author);
3164 free(header->committer);
3165 free(header->refs_str);
3166 free(header->commit_id);
3167 free(header->parent_id);
3168 free(header->tree_id);
3169 free(header->commit_msg);
3172 static struct gw_header *
3175 struct gw_header *header;
3177 header = malloc(sizeof(*header));
3181 header->path = NULL;
3182 SIMPLEQ_INIT(&header->refs);
3184 header->refs_str = NULL;
3185 header->commit_id = NULL;
3186 header->committer = NULL;
3187 header->author = NULL;
3188 header->parent_id = NULL;
3189 header->tree_id = NULL;
3190 header->commit_msg = NULL;
3195 static const struct got_error *
3196 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3197 int limit, struct got_object_id *id)
3199 const struct got_error *error = NULL;
3200 struct got_commit_graph *graph = NULL;
3201 struct got_commit_object *commit = NULL;
3202 int chk_next = 0, chk_multi = 0, prev_set = 0;
3204 error = got_commit_graph_open(&graph, header->path, 0);
3208 error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3214 error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3217 if (error->code == GOT_ERR_ITER_COMPLETED)
3224 error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3227 if (limit == 1 && chk_multi == 0) {
3228 error = gw_get_commit(gw_trans, header, commit, id);
3233 struct gw_header *n_header = NULL;
3234 if ((n_header = gw_init_header()) == NULL) {
3235 error = got_error_from_errno("malloc");
3238 error = got_ref_list(&n_header->refs, gw_trans->repo,
3239 NULL, got_ref_cmp_by_name, NULL);
3243 error = gw_get_commit(gw_trans, n_header, commit, id);
3246 got_ref_list_free(&n_header->refs);
3249 * we have a commit_id now, so copy it to next_prev_id
3250 * for navigation through gw_briefs and gw_commits
3252 if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3253 (gw_trans->action == GW_BRIEFS ||
3254 gw_trans->action == GW_COMMITS ||
3255 gw_trans->action == GW_SUMMARY)) {
3257 gw_trans->next_prev_id =
3258 strdup(n_header->commit_id);
3259 if (gw_trans->next_prev_id == NULL) {
3260 error = got_error_from_errno("strdup");
3266 * check for one more commit before breaking,
3267 * so we know whether to navicate through gw_briefs
3268 * gw_commits and gw_summary
3270 if (chk_next && (gw_trans->action == GW_BRIEFS||
3271 gw_trans->action == GW_COMMITS ||
3272 gw_trans->action == GW_SUMMARY)) {
3273 gw_trans->next_id = strdup(n_header->commit_id);
3274 if (gw_trans->next_id == NULL)
3275 error = got_error_from_errno("strdup");
3279 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3282 if (error || (limit && --limit == 0)) {
3290 got_object_commit_close(commit);
3292 got_commit_graph_close(graph);
3296 static const struct got_error *
3297 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3298 struct got_commit_object *commit, struct got_object_id *id)
3300 const struct got_error *error = NULL;
3301 struct got_reflist_entry *re;
3302 struct got_object_id *id2 = NULL;
3303 struct got_object_qid *parent_id;
3304 char *commit_msg = NULL, *commit_msg0;
3307 SIMPLEQ_FOREACH(re, &header->refs, entry) {
3310 struct got_tag_object *tag = NULL;
3313 if (got_ref_is_symbolic(re->ref))
3316 name = got_ref_get_name(re->ref);
3317 if (strncmp(name, "refs/", 5) == 0)
3319 if (strncmp(name, "got/", 4) == 0)
3321 if (strncmp(name, "heads/", 6) == 0)
3323 if (strncmp(name, "remotes/", 8) == 0)
3325 if (strncmp(name, "tags/", 5) == 0) {
3326 error = got_object_open_as_tag(&tag, gw_trans->repo,
3329 if (error->code != GOT_ERR_OBJ_TYPE)
3332 * Ref points at something other
3339 cmp = got_object_id_cmp(tag ?
3340 got_object_tag_get_object_id(tag) : re->id, id);
3342 got_object_tag_close(tag);
3345 s = header->refs_str;
3346 if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3347 s ? ", " : "", name) == -1) {
3348 error = got_error_from_errno("asprintf");
3350 header->refs_str = NULL;
3356 error = got_object_id_str(&header->commit_id, id);
3360 error = got_object_id_str(&header->tree_id,
3361 got_object_commit_get_tree_id(commit));
3365 if (gw_trans->action == GW_DIFF) {
3366 parent_id = SIMPLEQ_FIRST(
3367 got_object_commit_get_parent_ids(commit));
3368 if (parent_id != NULL) {
3369 id2 = got_object_id_dup(parent_id->id);
3371 error = got_object_id_str(&header->parent_id, id2);
3376 header->parent_id = strdup("/dev/null");
3377 if (header->parent_id == NULL) {
3378 error = got_error_from_errno("strdup");
3384 header->committer_time =
3385 got_object_commit_get_committer_time(commit);
3388 strdup(got_object_commit_get_author(commit));
3389 if (header->author == NULL) {
3390 error = got_error_from_errno("strdup");
3394 strdup(got_object_commit_get_committer(commit));
3395 if (header->committer == NULL) {
3396 error = got_error_from_errno("strdup");
3399 error = got_object_commit_get_logmsg(&commit_msg0, commit);
3403 commit_msg = commit_msg0;
3404 while (*commit_msg == '\n')
3407 header->commit_msg = strdup(commit_msg);
3408 if (header->commit_msg == NULL)
3409 error = got_error_from_errno("strdup");
3414 static const struct got_error *
3415 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3417 const struct got_error *error = NULL;
3418 char *in_repo_path = NULL;
3419 struct got_object_id *id = NULL;
3421 error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3425 if (gw_trans->commit_id == NULL) {
3426 struct got_reference *head_ref;
3427 error = got_ref_open(&head_ref, gw_trans->repo,
3428 gw_trans->headref, 0);
3432 error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3433 got_ref_close(head_ref);
3437 struct got_reference *ref;
3439 error = got_ref_open(&ref, gw_trans->repo,
3440 gw_trans->commit_id, 0);
3441 if (error == NULL) {
3443 error = got_ref_resolve(&id, gw_trans->repo, ref);
3447 error = got_object_get_type(&obj_type, gw_trans->repo,
3451 if (obj_type == GOT_OBJ_TYPE_TAG) {
3452 struct got_tag_object *tag;
3453 error = got_object_open_as_tag(&tag,
3454 gw_trans->repo, id);
3457 if (got_object_tag_get_object_type(tag) !=
3458 GOT_OBJ_TYPE_COMMIT) {
3459 got_object_tag_close(tag);
3460 error = got_error(GOT_ERR_OBJ_TYPE);
3464 id = got_object_id_dup(
3465 got_object_tag_get_object_id(tag));
3467 error = got_error_from_errno(
3468 "got_object_id_dup");
3469 got_object_tag_close(tag);
3472 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3473 error = got_error(GOT_ERR_OBJ_TYPE);
3477 error = got_repo_match_object_id_prefix(&id,
3478 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3484 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3485 gw_trans->repo_path, 1);
3490 header->path = strdup(in_repo_path);
3491 if (header->path == NULL) {
3492 error = got_error_from_errno("strdup");
3497 error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3498 got_ref_cmp_by_name, NULL);
3502 error = gw_get_commits(gw_trans, header, limit, id);
3504 got_ref_list_free(&header->refs);
3514 char datebuf[11]; /* YYYY-MM-DD + NUL */
3517 struct gw_blame_cb_args {
3518 struct blame_line *lines;
3522 off_t *line_offsets;
3524 struct got_repository *repo;
3525 struct gw_trans *gw_trans;
3528 static const struct got_error *
3529 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3531 const struct got_error *err = NULL;
3532 struct gw_blame_cb_args *a = arg;
3533 struct blame_line *bline;
3535 size_t linesize = 0;
3536 struct got_commit_object *commit = NULL;
3539 time_t committer_time;
3540 enum kcgi_err kerr = KCGI_OK;
3542 if (nlines != a->nlines ||
3543 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3544 return got_error(GOT_ERR_RANGE);
3547 return NULL; /* no change in this commit */
3549 /* Annotate this line. */
3550 bline = &a->lines[lineno - 1];
3551 if (bline->annotated)
3553 err = got_object_id_str(&bline->id_str, id);
3557 err = got_object_open_as_commit(&commit, a->repo, id);
3561 bline->committer = strdup(got_object_commit_get_committer(commit));
3562 if (bline->committer == NULL) {
3563 err = got_error_from_errno("strdup");
3567 committer_time = got_object_commit_get_committer_time(commit);
3568 if (localtime_r(&committer_time, &tm) == NULL)
3569 return got_error_from_errno("localtime_r");
3570 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3571 &tm) >= sizeof(bline->datebuf)) {
3572 err = got_error(GOT_ERR_NO_SPACE);
3575 bline->annotated = 1;
3577 /* Print lines annotated so far. */
3578 bline = &a->lines[a->lineno_cur - 1];
3579 if (!bline->annotated)
3582 offset = a->line_offsets[a->lineno_cur - 1];
3583 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3584 err = got_error_from_errno("fseeko");
3588 while (bline->annotated) {
3589 char *smallerthan, *at, *nl, *committer;
3590 char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3593 if (getline(&line, &linesize, a->f) == -1) {
3595 err = got_error_from_errno("getline");
3599 committer = bline->committer;
3600 smallerthan = strchr(committer, '<');
3601 if (smallerthan && smallerthan[1] != '\0')
3602 committer = smallerthan + 1;
3603 at = strchr(committer, '@');
3606 len = strlen(committer);
3608 committer[8] = '\0';
3610 nl = strchr(line, '\n');
3614 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3615 "blame_wrapper", KATTR__MAX);
3616 if (kerr != KCGI_OK)
3618 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3619 "blame_number", KATTR__MAX);
3620 if (kerr != KCGI_OK)
3622 if (asprintf(&lineno, "%.*d", a->nlines_prec,
3623 a->lineno_cur) == -1)
3625 kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3626 if (kerr != KCGI_OK)
3628 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3629 if (kerr != KCGI_OK)
3632 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3633 "blame_hash", KATTR__MAX);
3634 if (kerr != KCGI_OK)
3637 if (asprintf(&href_diff,
3638 "?path=%s&action=diff&commit=%s",
3639 a->gw_trans->repo_name, bline->id_str) == -1) {
3640 err = got_error_from_errno("asprintf");
3643 if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3644 err = got_error_from_errno("asprintf");
3647 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3648 KATTR_HREF, href_diff, KATTR__MAX);
3649 if (kerr != KCGI_OK)
3651 kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3652 if (kerr != KCGI_OK)
3654 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3655 if (kerr != KCGI_OK)
3658 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3659 "blame_date", KATTR__MAX);
3660 if (kerr != KCGI_OK)
3662 kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3663 if (kerr != KCGI_OK)
3665 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3666 if (kerr != KCGI_OK)
3669 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3670 "blame_author", KATTR__MAX);
3671 if (kerr != KCGI_OK)
3673 kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3674 if (kerr != KCGI_OK)
3676 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3677 if (kerr != KCGI_OK)
3680 kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3681 "blame_code", KATTR__MAX);
3682 if (kerr != KCGI_OK)
3684 kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3685 if (kerr != KCGI_OK)
3687 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3688 if (kerr != KCGI_OK)
3691 kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3692 if (kerr != KCGI_OK)
3696 bline = &a->lines[a->lineno_cur - 1];
3704 got_object_commit_close(commit);
3706 if (err == NULL && kerr != KCGI_OK)
3707 err = gw_kcgi_error(kerr);
3711 static const struct got_error *
3712 gw_output_file_blame(struct gw_trans *gw_trans)
3714 const struct got_error *error = NULL;
3715 struct got_object_id *obj_id = NULL;
3716 struct got_object_id *commit_id = NULL;
3717 struct got_blob_object *blob = NULL;
3718 char *path = NULL, *in_repo_path = NULL;
3719 struct gw_blame_cb_args bca;
3723 if (asprintf(&path, "%s%s%s",
3724 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3725 gw_trans->repo_folder ? "/" : "",
3726 gw_trans->repo_file) == -1) {
3727 error = got_error_from_errno("asprintf");
3731 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3735 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3736 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3740 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3745 if (obj_id == NULL) {
3746 error = got_error(GOT_ERR_NO_OBJ);
3750 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3754 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3755 error = got_error(GOT_ERR_OBJ_TYPE);
3759 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3763 bca.f = got_opentemp();
3764 if (bca.f == NULL) {
3765 error = got_error_from_errno("got_opentemp");
3768 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3769 &bca.line_offsets, bca.f, blob);
3770 if (error || bca.nlines == 0)
3773 /* Don't include \n at EOF in the blame line count. */
3774 if (bca.line_offsets[bca.nlines - 1] == filesize)
3777 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3778 if (bca.lines == NULL) {
3779 error = got_error_from_errno("calloc");
3783 bca.nlines_prec = 0;
3789 bca.repo = gw_trans->repo;
3790 bca.gw_trans = gw_trans;
3792 error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3801 free(bca.line_offsets);
3802 for (i = 0; i < bca.nlines; i++) {
3803 struct blame_line *bline = &bca.lines[i];
3804 free(bline->id_str);
3805 free(bline->committer);
3808 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3809 error = got_error_from_errno("fclose");
3812 got_object_blob_close(blob);
3816 static const struct got_error *
3817 gw_output_blob_buf(struct gw_trans *gw_trans)
3819 const struct got_error *error = NULL;
3820 struct got_object_id *obj_id = NULL;
3821 struct got_object_id *commit_id = NULL;
3822 struct got_blob_object *blob = NULL;
3823 char *path = NULL, *in_repo_path = NULL;
3824 int obj_type, set_mime = 0;
3827 enum kcgi_err kerr = KCGI_OK;
3829 if (asprintf(&path, "%s%s%s",
3830 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3831 gw_trans->repo_folder ? "/" : "",
3832 gw_trans->repo_file) == -1) {
3833 error = got_error_from_errno("asprintf");
3837 error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3841 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3842 GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3846 error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3851 if (obj_id == NULL) {
3852 error = got_error(GOT_ERR_NO_OBJ);
3856 error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3860 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3861 error = got_error(GOT_ERR_OBJ_TYPE);
3865 error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3869 hdrlen = got_object_blob_get_hdrlen(blob);
3871 error = got_object_blob_read_block(&len, blob);
3874 buf = got_object_blob_get_read_buf(blob);
3877 * Skip blob object header first time around,
3878 * which also contains a zero byte.
3881 if (set_mime == 0) {
3882 if (isbinary(buf, len - hdrlen))
3883 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3885 gw_trans->mime = KMIME_TEXT_PLAIN;
3887 error = gw_display_index(gw_trans);
3891 kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3892 if (kerr != KCGI_OK)
3902 got_object_blob_close(blob);
3903 if (error == NULL && kerr != KCGI_OK)
3904 error = gw_kcgi_error(kerr);
3908 static const struct got_error *
3909 gw_output_repo_tree(struct gw_trans *gw_trans)
3911 const struct got_error *error = NULL;
3912 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3913 struct got_tree_object *tree = NULL;
3914 char *path = NULL, *in_repo_path = NULL;
3915 char *id_str = NULL;
3916 char *build_folder = NULL;
3917 char *href_blob = NULL, *href_blame = NULL;
3918 const char *class = NULL;
3919 int nentries, i, class_flip = 0;
3920 enum kcgi_err kerr = KCGI_OK;
3922 if (gw_trans->repo_folder != NULL) {
3923 path = strdup(gw_trans->repo_folder);
3925 error = got_error_from_errno("strdup");
3929 error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3930 gw_trans->repo_path, 1);
3934 path = in_repo_path;
3937 if (gw_trans->commit_id == NULL) {
3938 struct got_reference *head_ref;
3939 error = got_ref_open(&head_ref, gw_trans->repo,
3940 gw_trans->headref, 0);
3943 error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
3946 got_ref_close(head_ref);
3948 * gw_trans->commit_id was not parsed from the querystring
3949 * we hit this code path from gw_index, where we don't know the
3950 * commit values for the tree link yet, so set
3951 * gw_trans->commit_id here to continue further into the tree
3953 error = got_object_id_str(&gw_trans->commit_id, commit_id);
3958 error = got_repo_match_object_id(&commit_id, NULL,
3959 gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
3965 error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
3970 error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
3974 nentries = got_object_tree_get_nentries(tree);
3975 for (i = 0; i < nentries; i++) {
3976 struct got_tree_entry *te;
3977 const char *modestr = "";
3980 te = got_object_tree_get_entry(tree, i);
3982 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3986 mode = got_tree_entry_get_mode(te);
3987 if (got_object_tree_entry_is_submodule(te))
3989 else if (S_ISLNK(mode))
3991 else if (S_ISDIR(mode))
3993 else if (mode & S_IXUSR)
3996 if (class_flip == 0) {
3997 class = "back_lightgray";
4000 class = "back_white";
4004 if (S_ISDIR(mode)) {
4005 if (asprintf(&build_folder, "%s/%s",
4006 gw_trans->repo_folder ? gw_trans->repo_folder : "",
4007 got_tree_entry_get_name(te)) == -1) {
4008 error = got_error_from_errno("asprintf");
4011 if (asprintf(&href_blob,
4012 "?path=%s&action=%s&commit=%s&folder=%s",
4013 gw_trans->repo_name, gw_get_action_name(gw_trans),
4014 gw_trans->commit_id, build_folder) == -1) {
4015 error = got_error_from_errno("asprintf");
4019 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4020 KATTR_ID, "tree_wrapper", KATTR__MAX);
4021 if (kerr != KCGI_OK)
4023 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4024 KATTR_ID, "tree_line", KATTR_CLASS, class,
4026 if (kerr != KCGI_OK)
4028 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4029 KATTR_HREF, href_blob, KATTR_CLASS,
4030 "diff_directory", KATTR__MAX);
4031 if (kerr != KCGI_OK)
4033 kerr = khtml_puts(gw_trans->gw_html_req,
4034 got_tree_entry_get_name(te));
4035 if (kerr != KCGI_OK)
4037 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4038 if (kerr != KCGI_OK)
4040 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4041 if (kerr != KCGI_OK)
4043 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4044 KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4046 if (kerr != KCGI_OK)
4048 kerr = khtml_entity(gw_trans->gw_html_req,
4050 if (kerr != KCGI_OK)
4052 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4053 if (kerr != KCGI_OK)
4056 if (asprintf(&href_blob,
4057 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4058 gw_trans->repo_name, "blob", gw_trans->commit_id,
4059 got_tree_entry_get_name(te),
4060 gw_trans->repo_folder ?
4061 gw_trans->repo_folder : "") == -1) {
4062 error = got_error_from_errno("asprintf");
4065 if (asprintf(&href_blame,
4066 "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4067 gw_trans->repo_name, "blame", gw_trans->commit_id,
4068 got_tree_entry_get_name(te),
4069 gw_trans->repo_folder ?
4070 gw_trans->repo_folder : "") == -1) {
4071 error = got_error_from_errno("asprintf");
4075 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4076 KATTR_ID, "tree_wrapper", KATTR__MAX);
4077 if (kerr != KCGI_OK)
4079 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4080 KATTR_ID, "tree_line", KATTR_CLASS, class,
4082 if (kerr != KCGI_OK)
4084 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4085 KATTR_HREF, href_blob, KATTR__MAX);
4086 if (kerr != KCGI_OK)
4088 kerr = khtml_puts(gw_trans->gw_html_req,
4089 got_tree_entry_get_name(te));
4090 if (kerr != KCGI_OK)
4092 kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4093 if (kerr != KCGI_OK)
4095 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4096 if (kerr != KCGI_OK)
4098 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4099 KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4101 if (kerr != KCGI_OK)
4104 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4105 KATTR_HREF, href_blob, KATTR__MAX);
4106 if (kerr != KCGI_OK)
4108 kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4109 if (kerr != KCGI_OK)
4111 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4112 if (kerr != KCGI_OK)
4115 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4116 if (kerr != KCGI_OK)
4119 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4120 KATTR_HREF, href_blame, KATTR__MAX);
4121 if (kerr != KCGI_OK)
4123 kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4124 if (kerr != KCGI_OK)
4127 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4128 if (kerr != KCGI_OK)
4136 build_folder = NULL;
4140 got_object_tree_close(tree);
4147 if (error == NULL && kerr != KCGI_OK)
4148 error = gw_kcgi_error(kerr);
4152 static const struct got_error *
4153 gw_output_repo_heads(struct gw_trans *gw_trans)
4155 const struct got_error *error = NULL;
4156 struct got_reflist_head refs;
4157 struct got_reflist_entry *re;
4158 char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4159 char *href_commits = NULL;
4160 enum kcgi_err kerr = KCGI_OK;
4162 SIMPLEQ_INIT(&refs);
4164 error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4165 got_ref_cmp_by_name, NULL);
4169 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4170 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4171 if (kerr != KCGI_OK)
4173 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4174 KATTR_ID, "summary_heads_title", KATTR__MAX);
4175 if (kerr != KCGI_OK)
4177 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4178 if (kerr != KCGI_OK)
4180 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4181 if (kerr != KCGI_OK)
4183 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4184 KATTR_ID, "summary_heads_content", KATTR__MAX);
4185 if (kerr != KCGI_OK)
4188 SIMPLEQ_FOREACH(re, &refs, entry) {
4189 const char *refname;
4191 if (got_ref_is_symbolic(re->ref))
4194 refname = got_ref_get_name(re->ref);
4195 if (strncmp(refname, "refs/heads/", 11) != 0)
4198 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4203 if (strncmp(refname, "refs/heads/", 11) == 0)
4206 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4207 KATTR_ID, "heads_wrapper", KATTR__MAX);
4208 if (kerr != KCGI_OK)
4210 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4211 KATTR_ID, "heads_age", KATTR__MAX);
4212 if (kerr != KCGI_OK)
4214 kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4215 if (kerr != KCGI_OK)
4217 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4218 if (kerr != KCGI_OK)
4220 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4221 KATTR_ID, "heads_space", KATTR__MAX);
4222 if (kerr != KCGI_OK)
4224 kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4225 if (kerr != KCGI_OK)
4227 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4228 if (kerr != KCGI_OK)
4230 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4231 KATTR_ID, "head", KATTR__MAX);
4232 if (kerr != KCGI_OK)
4234 if (asprintf(&href_summary,
4235 "?path=%s&action=summary&headref=%s",
4236 gw_trans->repo_name, refname) == -1) {
4237 error = got_error_from_errno("asprintf");
4240 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4241 href_summary, KATTR__MAX);
4242 kerr = khtml_puts(gw_trans->gw_html_req, refname);
4243 if (kerr != KCGI_OK)
4245 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4246 if (kerr != KCGI_OK)
4249 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4250 "navs_wrapper", KATTR__MAX);
4251 if (kerr != KCGI_OK)
4253 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4254 "navs", KATTR__MAX);
4255 if (kerr != KCGI_OK)
4258 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4259 href_summary, KATTR__MAX);
4260 if (kerr != KCGI_OK)
4262 kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4263 if (kerr != KCGI_OK)
4265 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4266 if (kerr != KCGI_OK)
4269 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4270 if (kerr != KCGI_OK)
4272 if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4273 gw_trans->repo_name, refname) == -1) {
4274 error = got_error_from_errno("asprintf");
4277 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4278 href_briefs, KATTR__MAX);
4279 if (kerr != KCGI_OK)
4281 kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4282 if (kerr != KCGI_OK)
4284 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4285 if (kerr != KCGI_OK)
4288 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4289 if (kerr != KCGI_OK)
4292 if (asprintf(&href_commits,
4293 "?path=%s&action=commits&headref=%s",
4294 gw_trans->repo_name, refname) == -1) {
4295 error = got_error_from_errno("asprintf");
4298 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4299 href_commits, KATTR__MAX);
4300 if (kerr != KCGI_OK)
4302 kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4303 if (kerr != KCGI_OK)
4305 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4306 if (kerr != KCGI_OK)
4309 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4310 "dotted_line", KATTR__MAX);
4311 if (kerr != KCGI_OK)
4313 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4314 if (kerr != KCGI_OK)
4317 href_summary = NULL;
4321 href_commits = NULL;
4324 got_ref_list_free(&refs);
4331 static const struct got_error *
4332 gw_output_site_link(struct gw_trans *gw_trans)
4334 const struct got_error *error = NULL;
4335 char *href_summary = NULL;
4336 enum kcgi_err kerr = KCGI_OK;
4338 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4339 "site_link", KATTR__MAX);
4340 if (kerr != KCGI_OK)
4342 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4344 if (kerr != KCGI_OK)
4346 kerr = khtml_puts(gw_trans->gw_html_req,
4347 gw_trans->gw_conf->got_site_link);
4348 if (kerr != KCGI_OK)
4350 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4351 if (kerr != KCGI_OK)
4354 if (gw_trans->repo_name != NULL) {
4355 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4356 if (kerr != KCGI_OK)
4358 if (asprintf(&href_summary, "?path=%s&action=summary",
4359 gw_trans->repo_name) == -1)
4361 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4362 href_summary, KATTR__MAX);
4363 if (kerr != KCGI_OK)
4365 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4366 if (kerr != KCGI_OK)
4368 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4369 if (kerr != KCGI_OK)
4371 kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4372 if (kerr != KCGI_OK)
4374 kerr = khtml_puts(gw_trans->gw_html_req,
4375 gw_get_action_name(gw_trans));
4376 if (kerr != KCGI_OK)
4380 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4381 if (kerr != KCGI_OK)
4388 static const struct got_error *
4389 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4391 const struct got_error *error = NULL;
4393 enum kcgi_err kerr = KCGI_OK;
4395 if (strncmp(buf, "-", 1) == 0)
4396 color = "diff_minus";
4397 else if (strncmp(buf, "+", 1) == 0)
4398 color = "diff_plus";
4399 else if (strncmp(buf, "@@", 2) == 0)
4400 color = "diff_chunk_header";
4401 else if (strncmp(buf, "@@", 2) == 0)
4402 color = "diff_chunk_header";
4403 else if (strncmp(buf, "commit +", 8) == 0)
4404 color = "diff_meta";
4405 else if (strncmp(buf, "commit -", 8) == 0)
4406 color = "diff_meta";
4407 else if (strncmp(buf, "blob +", 6) == 0)
4408 color = "diff_meta";
4409 else if (strncmp(buf, "blob -", 6) == 0)
4410 color = "diff_meta";
4411 else if (strncmp(buf, "file +", 6) == 0)
4412 color = "diff_meta";
4413 else if (strncmp(buf, "file -", 6) == 0)
4414 color = "diff_meta";
4415 else if (strncmp(buf, "from:", 5) == 0)
4416 color = "diff_author";
4417 else if (strncmp(buf, "via:", 4) == 0)
4418 color = "diff_author";
4419 else if (strncmp(buf, "date:", 5) == 0)
4420 color = "diff_date";
4421 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4422 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4423 if (error == NULL && kerr != KCGI_OK)
4424 error = gw_kcgi_error(kerr);
4429 main(int argc, char *argv[])
4431 const struct got_error *error = NULL;
4432 struct gw_trans *gw_trans;
4433 struct gw_dir *dir = NULL, *tdir;
4434 const char *page = "index";
4438 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4441 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4444 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4447 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4450 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4451 if (kerr != KCGI_OK) {
4452 error = gw_kcgi_error(kerr);
4456 if ((gw_trans->gw_conf =
4457 malloc(sizeof(struct gotweb_conf))) == NULL) {
4459 error = got_error_from_errno("malloc");
4463 TAILQ_INIT(&gw_trans->gw_dirs);
4464 TAILQ_INIT(&gw_trans->gw_headers);
4466 gw_trans->action = -1;
4468 gw_trans->repos_total = 0;
4469 gw_trans->repo_path = NULL;
4470 gw_trans->commit_id = NULL;
4471 gw_trans->next_id = NULL;
4472 gw_trans->next_prev_id = NULL;
4473 gw_trans->prev_id = NULL;
4474 gw_trans->prev_prev_id = NULL;
4475 gw_trans->headref = GOT_REF_HEAD;
4476 gw_trans->mime = KMIME_TEXT_HTML;
4477 gw_trans->gw_tmpl->key = gw_templs;
4478 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4479 gw_trans->gw_tmpl->arg = gw_trans;
4480 gw_trans->gw_tmpl->cb = gw_template;
4481 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4485 error = gw_parse_querystring(gw_trans);
4489 if (gw_trans->action == GW_BLOB)
4490 error = gw_blob(gw_trans);
4492 error = gw_display_index(gw_trans);
4495 free(gw_trans->gw_conf->got_repos_path);
4496 free(gw_trans->gw_conf->got_site_name);
4497 free(gw_trans->gw_conf->got_site_owner);
4498 free(gw_trans->gw_conf->got_site_link);
4499 free(gw_trans->gw_conf->got_logo);
4500 free(gw_trans->gw_conf->got_logo_url);
4501 free(gw_trans->gw_conf);
4502 free(gw_trans->commit_id);
4503 free(gw_trans->next_id);
4504 free(gw_trans->next_prev_id);
4505 free(gw_trans->prev_id);
4506 free(gw_trans->prev_prev_id);
4507 free(gw_trans->repo_path);
4509 got_repo_close(gw_trans->repo);
4511 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4513 free(dir->description);
4522 khttp_free(gw_trans->gw_req);