Blob


1 /*
2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
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.
8 *
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.
16 */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <regex.h>
27 #include <stdarg.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
37 #include <got_path.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
40 #include <got_diff.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
46 #include <kcgi.h>
47 #include <kcgihtml.h>
49 #include "buf.h"
50 #include "gotweb.h"
51 #include "gotweb_ui.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 struct gw_trans {
58 TAILQ_HEAD(headers, gw_header) gw_headers;
59 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
60 struct gw_dir *gw_dir;
61 struct gotweb_conf *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
64 struct kreq *gw_req;
65 char *repo_name;
66 char *repo_path;
67 char *commit;
68 char *repo_file;
69 char *repo_folder;
70 char *action_name;
71 char *headref;
72 unsigned int action;
73 unsigned int page;
74 unsigned int repos_total;
75 enum kmime mime;
76 };
78 struct gw_header {
79 TAILQ_ENTRY(gw_header) entry;
80 struct got_repository *repo;
81 struct got_reflist_head refs;
82 struct got_commit_object *commit;
83 struct got_object_id *id;
84 char *path;
86 char *refs_str;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
89 char *tree_id;
90 const char *author;
91 const char *committer;
92 char *commit_msg;
93 time_t committer_time;
94 };
96 struct gw_dir {
97 TAILQ_ENTRY(gw_dir) entry;
98 char *name;
99 char *owner;
100 char *description;
101 char *url;
102 char *age;
103 char *path;
104 };
106 enum gw_key {
107 KEY_ACTION,
108 KEY_COMMIT_ID,
109 KEY_FILE,
110 KEY_FOLDER,
111 KEY_HEADREF,
112 KEY_PAGE,
113 KEY_PATH,
114 KEY__ZMAX
115 };
117 enum gw_tmpl {
118 TEMPL_CONTENT,
119 TEMPL_HEAD,
120 TEMPL_HEADER,
121 TEMPL_SEARCH,
122 TEMPL_SITEPATH,
123 TEMPL_SITEOWNER,
124 TEMPL_TITLE,
125 TEMPL__MAX
126 };
128 enum gw_ref_tm {
129 TM_DIFF,
130 TM_LONG,
131 };
133 enum gw_tags {
134 TAGBRIEF,
135 TAGFULL,
136 };
138 static const char *const gw_templs[TEMPL__MAX] = {
139 "content",
140 "head",
141 "header",
142 "search",
143 "sitepath",
144 "siteowner",
145 "title",
146 };
148 static const struct kvalid gw_keys[KEY__ZMAX] = {
149 { kvalid_stringne, "action" },
150 { kvalid_stringne, "commit" },
151 { kvalid_stringne, "file" },
152 { kvalid_stringne, "folder" },
153 { kvalid_stringne, "headref" },
154 { kvalid_int, "page" },
155 { kvalid_stringne, "path" },
156 };
158 static struct gw_dir *gw_init_gw_dir(char *);
159 static struct gw_header *gw_init_header(void);
161 static const struct got_error *gw_get_repo_description(char **,
162 struct gw_trans *, char *);
163 static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
164 char *);
165 static const struct got_error *gw_get_time_str(char **, time_t, int);
166 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
167 char *, char *, int);
168 static const struct got_error *gw_get_file_blame_blob(char **,
169 struct gw_trans *);
170 static const struct got_error *gw_output_blob_buf(struct gw_trans *);
171 static const struct got_error *gw_get_repo_tree(char **, struct gw_trans *);
172 static const struct got_error *gw_get_diff(struct gw_trans *,
173 struct gw_header *);
174 static const struct got_error *gw_get_repo_tags(char **, struct gw_trans *,
175 struct gw_header *, int, int);
176 static const struct got_error *gw_get_repo_heads(char **, struct gw_trans *);
177 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
178 char *);
179 static char *gw_get_site_link(struct gw_trans *);
180 static const struct got_error *gw_html_escape(char **, const char *);
181 static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
183 static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
184 char*);
185 static char *gw_gen_commit_header_old(char *, char*);
186 static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
187 char*);
188 static const struct got_error *gw_gen_author_header(struct gw_trans *,
189 const char *);
190 static const struct got_error *gw_gen_age_header(struct gw_trans *,
191 const char *);
192 static const struct got_error *gw_gen_committer_header(struct gw_trans *,
193 const char *);
194 static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
195 char *);
196 static char *gw_gen_commit_msg_header_old(char *);
197 static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
199 static void gw_free_headers(struct gw_header *);
200 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
201 enum kmime);
202 static const struct got_error* gw_display_index(struct gw_trans *);
203 static void gw_display_error(struct gw_trans *,
204 const struct got_error *);
206 static int gw_template(size_t, void *);
208 static const struct got_error* gw_get_header(struct gw_trans *,
209 struct gw_header *, int);
210 static const struct got_error* gw_get_commits(struct gw_trans *,
211 struct gw_header *, int);
212 static const struct got_error* gw_get_commit(struct gw_trans *,
213 struct gw_header *);
214 static const struct got_error* gw_apply_unveil(const char *, const char *);
215 static const struct got_error* gw_blame_cb(void *, int, int,
216 struct got_object_id *);
217 static const struct got_error* gw_load_got_paths(struct gw_trans *);
218 static const struct got_error* gw_load_got_path(struct gw_trans *,
219 struct gw_dir *);
220 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 *);
236 char *template;
237 };
239 enum gw_query_actions {
240 GW_BLAME,
241 GW_BLOB,
242 GW_BRIEFS,
243 GW_COMMITS,
244 GW_DIFF,
245 GW_ERR,
246 GW_INDEX,
247 GW_SUMMARY,
248 GW_TAG,
249 GW_TREE,
250 };
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, NULL, NULL, "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" },
263 };
265 static const struct got_error *
266 gw_kcgi_error(enum kcgi_err kerr)
268 if (kerr == KCGI_OK)
269 return NULL;
271 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
272 return got_error(GOT_ERR_CANCELLED);
274 if (kerr == KCGI_ENOMEM)
275 return got_error_set_errno(ENOMEM,
276 kcgi_strerror(kerr));
278 if (kerr == KCGI_ENFILE)
279 return got_error_set_errno(ENFILE,
280 kcgi_strerror(kerr));
282 if (kerr == KCGI_EAGAIN)
283 return got_error_set_errno(EAGAIN,
284 kcgi_strerror(kerr));
286 if (kerr == KCGI_FORM)
287 return got_error_msg(GOT_ERR_IO,
288 kcgi_strerror(kerr));
290 return got_error_from_errno(kcgi_strerror(kerr));
293 static const struct got_error *
294 gw_apply_unveil(const char *repo_path, const char *repo_file)
296 const struct got_error *err;
298 if (repo_path && repo_file) {
299 char *full_path;
300 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
301 return got_error_from_errno("asprintf unveil");
302 if (unveil(full_path, "r") != 0)
303 return got_error_from_errno2("unveil", full_path);
306 if (repo_path && unveil(repo_path, "r") != 0)
307 return got_error_from_errno2("unveil", repo_path);
309 if (unveil("/tmp", "rwc") != 0)
310 return got_error_from_errno2("unveil", "/tmp");
312 err = got_privsep_unveil_exec_helpers();
313 if (err != NULL)
314 return err;
316 if (unveil(NULL, NULL) != 0)
317 return got_error_from_errno("unveil");
319 return NULL;
322 static const struct got_error *
323 gw_empty_string(char **s)
325 *s = strdup("");
326 if (*s == NULL)
327 return got_error_from_errno("strdup");
328 return NULL;
331 static int
332 isbinary(const uint8_t *buf, size_t n)
334 size_t i;
336 for (i = 0; i < n; i++)
337 if (buf[i] == 0)
338 return 1;
339 return 0;
342 static const struct got_error *
343 gw_blame(struct gw_trans *gw_trans)
345 const struct got_error *error = NULL;
346 struct gw_header *header = NULL;
347 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
348 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
349 enum kcgi_err kerr;
351 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
352 NULL) == -1)
353 return got_error_from_errno("pledge");
355 if ((header = gw_init_header()) == NULL)
356 return got_error_from_errno("malloc");
358 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
359 if (error)
360 goto done;
362 error = gw_get_header(gw_trans, header, 1);
363 if (error)
364 goto done;
366 error = gw_get_file_blame_blob(&blame_html, gw_trans);
367 if (error)
368 goto done;
370 error = gw_get_time_str(&age, header->committer_time, TM_LONG);
371 if (error)
372 goto done;
373 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
374 error = got_error_from_errno("asprintf");
375 goto done;
378 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
379 if (error)
380 goto done;
381 if (asprintf(&blame_html_disp, blame_header, age_html,
382 gw_gen_commit_msg_header_old(escaped_commit_msg), blame_html) == -1) {
383 error = got_error_from_errno("asprintf");
384 goto done;
387 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
388 error = got_error_from_errno("asprintf");
389 goto done;
392 kerr = khttp_puts(gw_trans->gw_req, blame);
393 if (kerr != KCGI_OK)
394 error = gw_kcgi_error(kerr);
395 done:
396 got_ref_list_free(&header->refs);
397 gw_free_headers(header);
398 free(blame_html_disp);
399 free(blame_html);
400 free(blame);
401 free(escaped_commit_msg);
402 return error;
405 static const struct got_error *
406 gw_blob(struct gw_trans *gw_trans)
408 const struct got_error *error = NULL;
409 struct gw_header *header = NULL;
411 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
412 NULL) == -1)
413 return got_error_from_errno("pledge");
415 if ((header = gw_init_header()) == NULL)
416 return got_error_from_errno("malloc");
418 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
419 if (error)
420 goto done;
422 error = gw_get_header(gw_trans, header, 1);
423 if (error)
424 goto done;
426 error = gw_output_blob_buf(gw_trans);
427 done:
428 got_ref_list_free(&header->refs);
429 gw_free_headers(header);
430 return error;
433 static const struct got_error *
434 gw_diff(struct gw_trans *gw_trans)
436 const struct got_error *error = NULL;
437 struct gw_header *header = NULL;
438 char *age = NULL, *escaped_commit_msg = NULL;
439 enum kcgi_err kerr = KCGI_OK;
441 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
442 NULL) == -1)
443 return got_error_from_errno("pledge");
445 if ((header = gw_init_header()) == NULL)
446 return got_error_from_errno("malloc");
448 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
449 if (error)
450 goto done;
452 error = gw_get_header(gw_trans, header, 1);
453 if (error)
454 goto done;
456 /* diff header */
457 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
458 "diff_header_wrapper", KATTR__MAX);
459 if (kerr != KCGI_OK)
460 goto done;
461 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
462 "diff_header", KATTR__MAX);
463 if (kerr != KCGI_OK)
464 goto done;
465 error = gw_gen_diff_header(gw_trans, header->parent_id,
466 header->commit_id);
467 if (error)
468 goto done;
469 error = gw_gen_commit_header(gw_trans, header->commit_id,
470 header->refs_str);
471 if (error)
472 goto done;
473 error = gw_gen_tree_header(gw_trans, header->tree_id);
474 if (error)
475 goto done;
476 error = gw_gen_author_header(gw_trans, header->author);
477 if (error)
478 goto done;
479 error = gw_gen_committer_header(gw_trans, header->author);
480 if (error)
481 goto done;
482 error = gw_get_time_str(&age, header->committer_time,
483 TM_LONG);
484 if (error)
485 goto done;
486 error = gw_gen_age_header(gw_trans, age ?age : "");
487 if (error)
488 goto done;
489 /*
490 * XXX: keeping this for now, since kcgihtml does not convert
491 * \n into <br /> yet.
492 */
493 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
494 if (error)
495 goto done;
496 error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
497 if (error)
498 goto done;
499 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
500 if (kerr != KCGI_OK)
501 goto done;
502 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
503 "dotted_line", KATTR__MAX);
504 if (kerr != KCGI_OK)
505 goto done;
506 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
507 if (kerr != KCGI_OK)
508 goto done;
510 /* diff */
511 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
512 "diff", KATTR__MAX);
513 if (kerr != KCGI_OK)
514 goto done;
515 error = gw_get_diff(gw_trans, header);
516 if (error)
517 goto done;
519 /* diff content close */
520 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
521 if (kerr != KCGI_OK)
522 goto done;
523 done:
524 got_ref_list_free(&header->refs);
525 gw_free_headers(header);
526 free(age);
527 free(escaped_commit_msg);
528 if (error == NULL && kerr != KCGI_OK)
529 error = gw_kcgi_error(kerr);
530 return error;
533 static const struct got_error *
534 gw_index(struct gw_trans *gw_trans)
536 const struct got_error *error = NULL;
537 struct gw_dir *gw_dir = NULL;
538 char *html, *navs, *next, *prev;
539 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
540 enum kcgi_err kerr;
542 if (pledge("stdio rpath proc exec sendfd unveil",
543 NULL) == -1) {
544 error = got_error_from_errno("pledge");
545 return error;
548 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
549 if (error)
550 return error;
552 error = gw_load_got_paths(gw_trans);
553 if (error)
554 return error;
556 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
557 if (kerr != KCGI_OK)
558 return gw_kcgi_error(kerr);
560 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
561 if (asprintf(&html, index_projects_empty,
562 gw_trans->gw_conf->got_repos_path) == -1)
563 return got_error_from_errno("asprintf");
564 kerr = khttp_puts(gw_trans->gw_req, html);
565 if (kerr != KCGI_OK)
566 error = gw_kcgi_error(kerr);
567 free(html);
568 return error;
571 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
572 dir_c++;
574 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
575 if (gw_trans->page > 0 && (gw_trans->page *
576 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
577 prev_disp++;
578 continue;
581 prev_disp++;
583 if (error)
584 return error;
585 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
586 gw_dir->name, gw_dir->name) == -1)
587 return got_error_from_errno("asprintf");
589 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
590 gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
591 gw_dir->age,
592 navs) == -1)
593 return got_error_from_errno("asprintf");
595 kerr = khttp_puts(gw_trans->gw_req, html);
596 free(navs);
597 free(html);
598 if (kerr != KCGI_OK)
599 return gw_kcgi_error(kerr);
601 if (gw_trans->gw_conf->got_max_repos_display == 0)
602 continue;
604 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
605 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
606 if (kerr != KCGI_OK)
607 return gw_kcgi_error(kerr);
608 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
609 (gw_trans->page > 0) &&
610 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
611 prev_disp == gw_trans->repos_total)) {
612 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
613 if (kerr != KCGI_OK)
614 return gw_kcgi_error(kerr);
617 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
618 (gw_trans->page > 0) &&
619 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
620 prev_disp == gw_trans->repos_total)) {
621 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
622 return got_error_from_errno("asprintf");
623 kerr = khttp_puts(gw_trans->gw_req, prev);
624 free(prev);
625 if (kerr != KCGI_OK)
626 return gw_kcgi_error(kerr);
629 kerr = khttp_puts(gw_trans->gw_req, div_end);
630 if (kerr != KCGI_OK)
631 return gw_kcgi_error(kerr);
633 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
634 next_disp == gw_trans->gw_conf->got_max_repos_display &&
635 dir_c != (gw_trans->page + 1) *
636 gw_trans->gw_conf->got_max_repos_display) {
637 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
638 return got_error_from_errno("calloc");
639 kerr = khttp_puts(gw_trans->gw_req, next);
640 free(next);
641 if (kerr != KCGI_OK)
642 return gw_kcgi_error(kerr);
643 kerr = khttp_puts(gw_trans->gw_req, div_end);
644 if (kerr != KCGI_OK)
645 error = gw_kcgi_error(kerr);
646 next_disp = 0;
647 break;
650 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
651 (gw_trans->page > 0) &&
652 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
653 prev_disp == gw_trans->repos_total)) {
654 kerr = khttp_puts(gw_trans->gw_req, div_end);
655 if (kerr != KCGI_OK)
656 return gw_kcgi_error(kerr);
659 next_disp++;
661 return error;
664 static const struct got_error *
665 gw_commits(struct gw_trans *gw_trans)
667 const struct got_error *error = NULL;
668 struct gw_header *header = NULL, *n_header = NULL;
669 char *age = NULL, *escaped_commit_msg = NULL;
670 char *href_diff = NULL, *href_tree = NULL;
671 enum kcgi_err kerr = KCGI_OK;
673 if ((header = gw_init_header()) == NULL)
674 return got_error_from_errno("malloc");
676 if (pledge("stdio rpath proc exec sendfd unveil",
677 NULL) == -1) {
678 error = got_error_from_errno("pledge");
679 goto done;
682 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
683 if (error)
684 goto done;
686 error = gw_get_header(gw_trans, header,
687 gw_trans->gw_conf->got_max_commits_display);
688 if (error)
689 goto done;
691 /* commit content */
692 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
693 /* commit line */
694 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
695 "commits_line_wrapper", KATTR__MAX);
696 if (kerr != KCGI_OK)
697 goto done;
698 error = gw_gen_commit_header(gw_trans, n_header->commit_id,
699 n_header->refs_str);
700 if (error)
701 goto done;
702 error = gw_gen_author_header(gw_trans, n_header->author);
703 if (error)
704 goto done;
705 error = gw_gen_committer_header(gw_trans, n_header->author);
706 if (error)
707 goto done;
708 error = gw_get_time_str(&age, n_header->committer_time,
709 TM_LONG);
710 if (error)
711 goto done;
712 error = gw_gen_age_header(gw_trans, age ?age : "");
713 if (error)
714 goto done;
715 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
716 if (kerr != KCGI_OK)
717 goto done;
719 /* dotted line */
720 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
721 "dotted_line", KATTR__MAX);
722 if (kerr != KCGI_OK)
723 goto done;
724 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
725 if (kerr != KCGI_OK)
726 goto done;
728 /* commit */
729 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
730 "commit", KATTR__MAX);
731 if (kerr != KCGI_OK)
732 goto done;
733 /*
734 * XXX: keeping this for now, since kcgihtml does not convert
735 * \n into <br /> yet.
736 */
737 error = gw_html_escape(&escaped_commit_msg,
738 n_header->commit_msg);
739 if (error)
740 goto done;
741 kerr = khttp_puts(gw_trans->gw_req, escaped_commit_msg);
742 if (kerr != KCGI_OK)
743 goto done;
744 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
745 if (kerr != KCGI_OK)
746 goto done;
748 /* navs */
750 /* XXX: create gen code for this */
751 /* build diff nav */
752 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
753 gw_trans->repo_name, n_header->commit_id) == -1) {
754 error = got_error_from_errno("asprintf");
755 goto done;
757 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
758 KATTR_ID, "navs_wrapper", KATTR__MAX);
759 if (kerr != KCGI_OK)
760 goto done;
761 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
762 KATTR_ID, "navs", KATTR__MAX);
763 if (kerr != KCGI_OK)
764 goto done;
765 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
766 KATTR_HREF, href_diff, KATTR__MAX);
767 if (kerr != KCGI_OK)
768 goto done;
769 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
770 if (kerr != KCGI_OK)
771 goto done;
772 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
773 if (kerr != KCGI_OK)
774 goto done;
776 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
777 if (kerr != KCGI_OK)
778 goto done;
780 /* XXX: create gen code for this */
781 /* build tree nav */
782 if (asprintf(&href_tree, "?path=%s&action=tree&commit=%s",
783 gw_trans->repo_name, n_header->commit_id) == -1) {
784 error = got_error_from_errno("asprintf");
785 goto done;
787 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
788 KATTR_HREF, href_tree, KATTR__MAX);
789 if (kerr != KCGI_OK)
790 goto done;
791 khtml_puts(gw_trans->gw_html_req, "tree");
792 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
793 if (kerr != KCGI_OK)
794 goto done;
795 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
796 if (kerr != KCGI_OK)
797 goto done;
799 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
800 "solid_line", KATTR__MAX);
801 if (kerr != KCGI_OK)
802 goto done;
803 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
804 if (kerr != KCGI_OK)
805 goto done;
807 free(age);
808 age = NULL;
809 free(escaped_commit_msg);
810 escaped_commit_msg = NULL;
812 done:
813 got_ref_list_free(&header->refs);
814 gw_free_headers(header);
815 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
816 gw_free_headers(n_header);
817 free(age);
818 free(href_diff);
819 free(href_tree);
820 free(escaped_commit_msg);
821 if (error == NULL && kerr != KCGI_OK)
822 error = gw_kcgi_error(kerr);
823 return error;
826 static const struct got_error *
827 gw_briefs(struct gw_trans *gw_trans)
829 const struct got_error *error = NULL;
830 struct gw_header *header = NULL, *n_header = NULL;
831 char *age = NULL, *age_html = NULL;
832 char *href_diff = NULL, *href_tree = NULL;
833 char *newline, *smallerthan;
834 enum kcgi_err kerr = KCGI_OK;
836 if ((header = gw_init_header()) == NULL)
837 return got_error_from_errno("malloc");
839 if (pledge("stdio rpath proc exec sendfd unveil",
840 NULL) == -1) {
841 error = got_error_from_errno("pledge");
842 goto done;
845 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
846 if (error)
847 goto done;
849 if (gw_trans->action == GW_SUMMARY)
850 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
851 else
852 error = gw_get_header(gw_trans, header,
853 gw_trans->gw_conf->got_max_commits_display);
854 if (error)
855 goto done;
857 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
858 error = gw_get_time_str(&age, n_header->committer_time,
859 TM_DIFF);
860 if (error)
861 goto done;
863 /* briefs wrapper */
864 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
865 KATTR_ID, "briefs_wrapper", KATTR__MAX);
866 if (kerr != KCGI_OK)
867 goto done;
869 /* briefs age */
870 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
871 KATTR_ID, "briefs_age", KATTR__MAX);
872 if (kerr != KCGI_OK)
873 goto done;
874 if (asprintf(&age_html, "%s", age ? age : "") == -1) {
875 error = got_error_from_errno("asprintf");
876 goto done;
878 kerr = khtml_puts(gw_trans->gw_html_req, age_html);
879 if (kerr != KCGI_OK)
880 goto done;
881 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
882 if (kerr != KCGI_OK)
883 goto done;
885 /* briefs author */
886 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
887 KATTR_ID, "briefs_author", KATTR__MAX);
888 if (kerr != KCGI_OK)
889 goto done;
890 smallerthan = strchr(n_header->author, '<');
891 if (smallerthan)
892 *smallerthan = '\0';
893 kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
894 if (kerr != KCGI_OK)
895 goto done;
896 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
897 if (kerr != KCGI_OK)
898 goto done;
900 /* briefs log */
901 if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
902 gw_trans->repo_name, n_header->commit_id) == -1) {
903 error = got_error_from_errno("asprintf");
904 goto done;
906 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
907 KATTR_ID, "briefs_log", KATTR__MAX);
908 if (kerr != KCGI_OK)
909 goto done;
910 newline = strchr(n_header->commit_msg, '\n');
911 if (newline)
912 *newline = '\0';
913 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
914 KATTR_HREF, href_diff, KATTR__MAX);
915 if (kerr != KCGI_OK)
916 goto done;
917 kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
918 if (kerr != KCGI_OK)
919 goto done;
920 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
921 if (kerr != KCGI_OK)
922 goto done;
924 /* build diff nav */
925 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
926 KATTR_ID, "navs_wrapper", KATTR__MAX);
927 if (kerr != KCGI_OK)
928 goto done;
929 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
930 KATTR_ID, "navs", KATTR__MAX);
931 if (kerr != KCGI_OK)
932 goto done;
933 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
934 KATTR_HREF, href_diff, KATTR__MAX);
935 if (kerr != KCGI_OK)
936 goto done;
937 kerr = khtml_puts(gw_trans->gw_html_req, "diff");
938 if (kerr != KCGI_OK)
939 goto done;
940 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
941 if (kerr != KCGI_OK)
942 goto done;
944 kerr = khtml_puts(gw_trans->gw_html_req, " | ");
945 if (kerr != KCGI_OK)
946 goto done;
948 /* build tree nav */
949 if (asprintf(&href_tree, "?path=%s&action=tree&commit=%s",
950 gw_trans->repo_name, n_header->commit_id) == -1) {
951 error = got_error_from_errno("asprintf");
952 goto done;
954 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
955 KATTR_HREF, href_tree, KATTR__MAX);
956 if (kerr != KCGI_OK)
957 goto done;
958 khtml_puts(gw_trans->gw_html_req, "tree");
959 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
960 if (kerr != KCGI_OK)
961 goto done;
962 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
963 if (kerr != KCGI_OK)
964 goto done;
966 /* dotted line */
967 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
968 KATTR_ID, "dotted_line", KATTR__MAX);
969 if (kerr != KCGI_OK)
970 goto done;
971 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
972 if (kerr != KCGI_OK)
973 goto done;
975 free(age);
976 age = NULL;
977 free(age_html);
978 age_html = NULL;
979 free(href_diff);
980 href_diff = NULL;
981 free(href_tree);
982 href_tree = NULL;
984 done:
985 got_ref_list_free(&header->refs);
986 gw_free_headers(header);
987 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
988 gw_free_headers(n_header);
989 free(age);
990 free(age_html);
991 free(href_diff);
992 free(href_tree);
993 if (error == NULL && kerr != KCGI_OK)
994 error = gw_kcgi_error(kerr);
995 return error;
998 static const struct got_error *
999 gw_summary(struct gw_trans *gw_trans)
1001 const struct got_error *error = NULL;
1002 char *age = NULL, *tags = NULL, *heads = NULL;
1003 enum kcgi_err kerr = KCGI_OK;
1005 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1006 return got_error_from_errno("pledge");
1008 /* unveil is applied with gw_briefs below */
1010 /* summary wrapper */
1011 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1012 "summary_wrapper", KATTR__MAX);
1013 if (kerr != KCGI_OK)
1014 return gw_kcgi_error(kerr);
1016 /* description */
1017 if (gw_trans->gw_conf->got_show_repo_description &&
1018 gw_trans->gw_dir->description != NULL &&
1019 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1020 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1021 KATTR_ID, "description_title", KATTR__MAX);
1022 if (kerr != KCGI_OK)
1023 goto done;
1024 kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1025 if (kerr != KCGI_OK)
1026 goto done;
1027 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1028 if (kerr != KCGI_OK)
1029 goto done;
1030 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1031 KATTR_ID, "description", KATTR__MAX);
1032 if (kerr != KCGI_OK)
1033 goto done;
1034 kerr = khtml_puts(gw_trans->gw_html_req,
1035 gw_trans->gw_dir->description);
1036 if (kerr != KCGI_OK)
1037 goto done;
1038 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1039 if (kerr != KCGI_OK)
1040 goto done;
1043 /* repo owner */
1044 if (gw_trans->gw_conf->got_show_repo_owner &&
1045 gw_trans->gw_dir->owner != NULL) {
1046 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1047 KATTR_ID, "repo_owner_title", KATTR__MAX);
1048 if (kerr != KCGI_OK)
1049 goto done;
1050 kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1051 if (kerr != KCGI_OK)
1052 goto done;
1053 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1054 if (kerr != KCGI_OK)
1055 goto done;
1056 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1057 KATTR_ID, "repo_owner", KATTR__MAX);
1058 if (kerr != KCGI_OK)
1059 goto done;
1060 kerr = khtml_puts(gw_trans->gw_html_req,
1061 gw_trans->gw_dir->owner);
1062 if (kerr != KCGI_OK)
1063 goto done;
1064 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1065 if (kerr != KCGI_OK)
1066 goto done;
1069 /* last change */
1070 if (gw_trans->gw_conf->got_show_repo_age) {
1071 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1072 "refs/heads", TM_LONG);
1073 if (error)
1074 goto done;
1075 if (age != NULL) {
1076 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1077 KATTR_ID, "last_change_title", KATTR__MAX);
1078 if (kerr != KCGI_OK)
1079 goto done;
1080 kerr = khtml_puts(gw_trans->gw_html_req,
1081 "Last Change: ");
1082 if (kerr != KCGI_OK)
1083 goto done;
1084 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1085 if (kerr != KCGI_OK)
1086 goto done;
1087 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1088 KATTR_ID, "last_change", KATTR__MAX);
1089 if (kerr != KCGI_OK)
1090 goto done;
1091 kerr = khtml_puts(gw_trans->gw_html_req, age);
1092 if (kerr != KCGI_OK)
1093 goto done;
1094 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1095 if (kerr != KCGI_OK)
1096 goto done;
1100 /* cloneurl */
1101 if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1102 gw_trans->gw_dir->url != NULL &&
1103 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1104 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1105 KATTR_ID, "cloneurl_title", KATTR__MAX);
1106 if (kerr != KCGI_OK)
1107 goto done;
1108 kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1109 if (kerr != KCGI_OK)
1110 goto done;
1111 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1112 if (kerr != KCGI_OK)
1113 goto done;
1114 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1115 KATTR_ID, "cloneurl", KATTR__MAX);
1116 if (kerr != KCGI_OK)
1117 goto done;
1118 kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1119 if (kerr != KCGI_OK)
1120 goto done;
1121 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1122 if (kerr != KCGI_OK)
1123 goto done;
1126 /* close summary wrapper */
1127 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1128 if (kerr != KCGI_OK)
1129 goto done;
1131 /* commit briefs header */
1132 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1133 "briefs_title_wrapper", KATTR__MAX);
1134 if (kerr != KCGI_OK)
1135 goto done;
1136 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1137 "briefs_title", KATTR__MAX);
1138 if (kerr != KCGI_OK)
1139 goto done;
1140 kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1141 if (kerr != KCGI_OK)
1142 goto done;
1143 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1144 if (kerr != KCGI_OK)
1145 goto done;
1146 error = gw_briefs(gw_trans);
1147 if (error)
1148 goto done;
1150 /* tags */
1151 error = gw_get_repo_tags(&tags, gw_trans, NULL, D_MAXSLCOMMDISP,
1152 TAGBRIEF);
1153 if (error)
1154 goto done;
1156 if (tags != NULL && strcmp(tags, "") != 0) {
1157 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1158 KATTR_ID, "summary_tags_title_wrapper", KATTR__MAX);
1159 if (kerr != KCGI_OK)
1160 goto done;
1161 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1162 KATTR_ID, "summary_tags_title", KATTR__MAX);
1163 if (kerr != KCGI_OK)
1164 goto done;
1165 kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
1166 if (kerr != KCGI_OK)
1167 goto done;
1168 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1169 if (kerr != KCGI_OK)
1170 goto done;
1171 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1172 KATTR_ID, "summary_tags_content", KATTR__MAX);
1173 if (kerr != KCGI_OK)
1174 goto done;
1175 kerr = khttp_puts(gw_trans->gw_req, tags);
1176 if (kerr != KCGI_OK)
1177 goto done;
1178 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1179 if (kerr != KCGI_OK)
1180 goto done;
1183 /* heads */
1184 error = gw_get_repo_heads(&heads, gw_trans);
1185 if (error)
1186 goto done;
1187 if (heads != NULL && strcmp(heads, "") != 0) {
1188 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1189 KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
1190 if (kerr != KCGI_OK)
1191 goto done;
1192 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1193 KATTR_ID, "summary_heads_title", KATTR__MAX);
1194 if (kerr != KCGI_OK)
1195 goto done;
1196 kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
1197 if (kerr != KCGI_OK)
1198 goto done;
1199 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1200 if (kerr != KCGI_OK)
1201 goto done;
1202 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1203 KATTR_ID, "summary_heads_content", KATTR__MAX);
1204 if (kerr != KCGI_OK)
1205 goto done;
1206 kerr = khttp_puts(gw_trans->gw_req, heads);
1207 if (kerr != KCGI_OK)
1208 goto done;
1209 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1210 if (kerr != KCGI_OK)
1211 goto done;
1213 done:
1214 free(age);
1215 free(tags);
1216 free(heads);
1217 if (error == NULL && kerr != KCGI_OK)
1218 error = gw_kcgi_error(kerr);
1219 return error;
1222 static const struct got_error *
1223 gw_tree(struct gw_trans *gw_trans)
1225 const struct got_error *error = NULL;
1226 struct gw_header *header = NULL;
1227 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1228 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
1229 enum kcgi_err kerr;
1231 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1232 return got_error_from_errno("pledge");
1234 if ((header = gw_init_header()) == NULL)
1235 return got_error_from_errno("malloc");
1237 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1238 if (error)
1239 goto done;
1241 error = gw_get_header(gw_trans, header, 1);
1242 if (error)
1243 goto done;
1245 error = gw_get_repo_tree(&tree_html, gw_trans);
1246 if (error)
1247 goto done;
1249 error = gw_get_time_str(&age, header->committer_time, TM_LONG);
1250 if (error)
1251 goto done;
1252 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
1253 error = got_error_from_errno("asprintf");
1254 goto done;
1256 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1257 if (error)
1258 goto done;
1259 if (asprintf(&tree_html_disp, tree_header, age_html,
1260 gw_gen_commit_msg_header_old(escaped_commit_msg),
1261 tree_html ? tree_html : "") == -1) {
1262 error = got_error_from_errno("asprintf");
1263 goto done;
1266 if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
1267 error = got_error_from_errno("asprintf");
1268 goto done;
1271 kerr = khttp_puts(gw_trans->gw_req, tree);
1272 if (kerr != KCGI_OK)
1273 error = gw_kcgi_error(kerr);
1274 done:
1275 got_ref_list_free(&header->refs);
1276 gw_free_headers(header);
1277 free(tree_html_disp);
1278 free(tree_html);
1279 free(tree);
1280 free(age);
1281 free(age_html);
1282 free(escaped_commit_msg);
1283 return error;
1286 static const struct got_error *
1287 gw_tag(struct gw_trans *gw_trans)
1289 const struct got_error *error = NULL;
1290 struct gw_header *header = NULL;
1291 char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
1292 char *escaped_commit_msg = NULL;
1293 enum kcgi_err kerr;
1295 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1296 return got_error_from_errno("pledge");
1298 if ((header = gw_init_header()) == NULL)
1299 return got_error_from_errno("malloc");
1301 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1302 if (error)
1303 goto done;
1305 error = gw_get_header(gw_trans, header, 1);
1306 if (error)
1307 goto done;
1309 error = gw_get_repo_tags(&tag_html, gw_trans, header, 1, TAGFULL);
1310 if (error)
1311 goto done;
1313 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1314 if (error)
1315 goto done;
1316 if (asprintf(&tag_html_disp, tag_header,
1317 gw_gen_commit_header_old(header->commit_id, header->refs_str),
1318 gw_gen_commit_msg_header_old(escaped_commit_msg),
1319 tag_html ? tag_html : "") == -1) {
1320 error = got_error_from_errno("asprintf");
1321 goto done;
1324 if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
1325 error = got_error_from_errno("asprintf");
1326 goto done;
1329 kerr = khttp_puts(gw_trans->gw_req, tag);
1330 if (kerr != KCGI_OK)
1331 error = gw_kcgi_error(kerr);
1332 done:
1333 got_ref_list_free(&header->refs);
1334 gw_free_headers(header);
1335 free(tag_html_disp);
1336 free(tag_html);
1337 free(tag);
1338 free(escaped_commit_msg);
1339 return error;
1342 static const struct got_error *
1343 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1345 const struct got_error *error = NULL;
1346 DIR *dt;
1347 char *dir_test;
1348 int opened = 0;
1350 if (asprintf(&dir_test, "%s/%s/%s",
1351 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1352 GOTWEB_GIT_DIR) == -1)
1353 return got_error_from_errno("asprintf");
1355 dt = opendir(dir_test);
1356 if (dt == NULL) {
1357 free(dir_test);
1358 } else {
1359 gw_dir->path = strdup(dir_test);
1360 opened = 1;
1361 goto done;
1364 if (asprintf(&dir_test, "%s/%s/%s",
1365 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1366 GOTWEB_GOT_DIR) == -1)
1367 return got_error_from_errno("asprintf");
1369 dt = opendir(dir_test);
1370 if (dt == NULL)
1371 free(dir_test);
1372 else {
1373 opened = 1;
1374 error = got_error(GOT_ERR_NOT_GIT_REPO);
1375 goto errored;
1378 if (asprintf(&dir_test, "%s/%s",
1379 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1380 return got_error_from_errno("asprintf");
1382 gw_dir->path = strdup(dir_test);
1384 done:
1385 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1386 gw_dir->path);
1387 if (error)
1388 goto errored;
1389 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1390 if (error)
1391 goto errored;
1392 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1393 "refs/heads", TM_DIFF);
1394 if (error)
1395 goto errored;
1396 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1397 errored:
1398 free(dir_test);
1399 if (opened)
1400 closedir(dt);
1401 return error;
1404 static const struct got_error *
1405 gw_load_got_paths(struct gw_trans *gw_trans)
1407 const struct got_error *error = NULL;
1408 DIR *d;
1409 struct dirent **sd_dent;
1410 struct gw_dir *gw_dir;
1411 struct stat st;
1412 unsigned int d_cnt, d_i;
1414 d = opendir(gw_trans->gw_conf->got_repos_path);
1415 if (d == NULL) {
1416 error = got_error_from_errno2("opendir",
1417 gw_trans->gw_conf->got_repos_path);
1418 return error;
1421 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1422 alphasort);
1423 if (d_cnt == -1) {
1424 error = got_error_from_errno2("scandir",
1425 gw_trans->gw_conf->got_repos_path);
1426 return error;
1429 for (d_i = 0; d_i < d_cnt; d_i++) {
1430 if (gw_trans->gw_conf->got_max_repos > 0 &&
1431 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1432 break; /* account for parent and self */
1434 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1435 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1436 continue;
1438 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1439 return got_error_from_errno("gw_dir malloc");
1441 error = gw_load_got_path(gw_trans, gw_dir);
1442 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1443 continue;
1444 else if (error)
1445 return error;
1447 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1448 !got_path_dir_is_empty(gw_dir->path)) {
1449 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1450 entry);
1451 gw_trans->repos_total++;
1455 closedir(d);
1456 return error;
1459 static const struct got_error *
1460 gw_parse_querystring(struct gw_trans *gw_trans)
1462 const struct got_error *error = NULL;
1463 struct kpair *p;
1464 struct gw_query_action *action = NULL;
1465 unsigned int i;
1467 if (gw_trans->gw_req->fieldnmap[0]) {
1468 error = got_error_from_errno("bad parse");
1469 return error;
1470 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1471 /* define gw_trans->repo_path */
1472 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1473 return got_error_from_errno("asprintf");
1475 if (asprintf(&gw_trans->repo_path, "%s/%s",
1476 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1477 return got_error_from_errno("asprintf");
1479 /* get action and set function */
1480 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1481 for (i = 0; i < nitems(gw_query_funcs); i++) {
1482 action = &gw_query_funcs[i];
1483 if (action->func_name == NULL)
1484 continue;
1486 if (strcmp(action->func_name,
1487 p->parsed.s) == 0) {
1488 gw_trans->action = i;
1489 if (asprintf(&gw_trans->action_name,
1490 "%s", action->func_name) == -1)
1491 return
1492 got_error_from_errno(
1493 "asprintf");
1495 break;
1498 action = NULL;
1501 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1502 if (asprintf(&gw_trans->commit, "%s",
1503 p->parsed.s) == -1)
1504 return got_error_from_errno("asprintf");
1506 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1507 if (asprintf(&gw_trans->repo_file, "%s",
1508 p->parsed.s) == -1)
1509 return got_error_from_errno("asprintf");
1511 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1512 if (asprintf(&gw_trans->repo_folder, "%s",
1513 p->parsed.s) == -1)
1514 return got_error_from_errno("asprintf");
1516 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1517 if (asprintf(&gw_trans->headref, "%s",
1518 p->parsed.s) == -1)
1519 return got_error_from_errno("asprintf");
1521 if (action == NULL) {
1522 error = got_error_from_errno("invalid action");
1523 return error;
1525 if ((gw_trans->gw_dir =
1526 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1527 return got_error_from_errno("gw_dir malloc");
1529 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1530 if (error)
1531 return error;
1532 } else
1533 gw_trans->action = GW_INDEX;
1535 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1536 gw_trans->page = p->parsed.i;
1538 return error;
1541 static struct gw_dir *
1542 gw_init_gw_dir(char *dir)
1544 struct gw_dir *gw_dir;
1546 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1547 return NULL;
1549 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1550 return NULL;
1552 return gw_dir;
1555 static const struct got_error *
1556 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1558 enum kcgi_err kerr;
1560 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1561 if (kerr != KCGI_OK)
1562 return gw_kcgi_error(kerr);
1563 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1564 khttps[code]);
1565 if (kerr != KCGI_OK)
1566 return gw_kcgi_error(kerr);
1567 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1568 kmimetypes[mime]);
1569 if (kerr != KCGI_OK)
1570 return gw_kcgi_error(kerr);
1571 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1572 "nosniff");
1573 if (kerr != KCGI_OK)
1574 return gw_kcgi_error(kerr);
1575 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1576 if (kerr != KCGI_OK)
1577 return gw_kcgi_error(kerr);
1578 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1579 "1; mode=block");
1580 if (kerr != KCGI_OK)
1581 return gw_kcgi_error(kerr);
1583 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1584 kerr = khttp_head(gw_trans->gw_req,
1585 kresps[KRESP_CONTENT_DISPOSITION],
1586 "attachment; filename=%s", gw_trans->repo_file);
1587 if (kerr != KCGI_OK)
1588 return gw_kcgi_error(kerr);
1591 kerr = khttp_body(gw_trans->gw_req);
1592 return gw_kcgi_error(kerr);
1595 static const struct got_error *
1596 gw_display_index(struct gw_trans *gw_trans)
1598 const struct got_error *error;
1599 enum kcgi_err kerr;
1601 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1602 if (error)
1603 return error;
1605 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1606 if (kerr != KCGI_OK)
1607 return gw_kcgi_error(kerr);
1609 if (gw_trans->action != GW_BLOB) {
1610 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1611 gw_query_funcs[gw_trans->action].template);
1612 if (kerr != KCGI_OK) {
1613 khtml_close(gw_trans->gw_html_req);
1614 return gw_kcgi_error(kerr);
1618 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1621 static void
1622 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1624 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1625 return;
1627 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1628 return;
1629 khtml_puts(gw_trans->gw_html_req, err->msg);
1630 khtml_close(gw_trans->gw_html_req);
1633 static int
1634 gw_template(size_t key, void *arg)
1636 const struct got_error *error = NULL;
1637 enum kcgi_err kerr;
1638 struct gw_trans *gw_trans = arg;
1639 char *gw_site_link, *img_src = NULL;
1641 switch (key) {
1642 case (TEMPL_HEAD):
1643 kerr = khttp_puts(gw_trans->gw_req, head);
1644 if (kerr != KCGI_OK)
1645 return 0;
1646 break;
1647 case(TEMPL_HEADER):
1648 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1649 KATTR_ID, "got_link", KATTR__MAX);
1650 if (kerr != KCGI_OK)
1651 return 0;
1652 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1653 KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1654 KATTR_TARGET, "_sotd", KATTR__MAX);
1655 if (kerr != KCGI_OK)
1656 return 0;
1657 if (asprintf(&img_src, "/%s",
1658 gw_trans->gw_conf->got_logo) == -1)
1659 return 0;
1660 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1661 KATTR_SRC, img_src, KATTR__MAX);
1662 if (kerr != KCGI_OK) {
1663 free(img_src);
1664 return 0;
1666 kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1667 if (kerr != KCGI_OK) {
1668 free(img_src);
1669 return 0;
1671 break;
1672 case (TEMPL_SITEPATH):
1673 gw_site_link = gw_get_site_link(gw_trans);
1674 if (gw_site_link != NULL) {
1675 kerr = khttp_puts(gw_trans->gw_req, gw_site_link);
1676 if (kerr != KCGI_OK) {
1677 free(gw_site_link);
1678 return 0;
1681 free(gw_site_link);
1682 break;
1683 case(TEMPL_TITLE):
1684 if (gw_trans->gw_conf->got_site_name != NULL) {
1685 kerr = khtml_puts(gw_trans->gw_html_req,
1686 gw_trans->gw_conf->got_site_name);
1687 if (kerr != KCGI_OK)
1688 return 0;
1690 break;
1691 case (TEMPL_SEARCH):
1692 kerr = khttp_puts(gw_trans->gw_req, search);
1693 if (kerr != KCGI_OK)
1694 return 0;
1695 break;
1696 case(TEMPL_SITEOWNER):
1697 if (gw_trans->gw_conf->got_site_owner != NULL &&
1698 gw_trans->gw_conf->got_show_site_owner) {
1699 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1700 KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1701 if (kerr != KCGI_OK)
1702 return 0;
1703 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1704 KATTR_ID, "site_owner", KATTR__MAX);
1705 if (kerr != KCGI_OK)
1706 return 0;
1707 kerr = khtml_puts(gw_trans->gw_html_req,
1708 gw_trans->gw_conf->got_site_owner);
1709 kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1710 if (kerr != KCGI_OK)
1711 return 0;
1713 break;
1714 case(TEMPL_CONTENT):
1715 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1716 if (error) {
1717 kerr = khttp_puts(gw_trans->gw_req, error->msg);
1718 if (kerr != KCGI_OK)
1719 return 0;
1721 break;
1722 default:
1723 return 0;
1725 return 1;
1728 static char *
1729 gw_gen_commit_header_old(char *str1, char *str2)
1731 char *return_html = NULL, *ref_str = NULL;
1733 if (strcmp(str2, "") != 0) {
1734 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1735 return_html = strdup("");
1736 return return_html;
1738 } else
1739 ref_str = strdup("");
1741 if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1742 return_html = strdup("");
1744 free(ref_str);
1745 return return_html;
1748 static const struct got_error *
1749 gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1751 const struct got_error *error = NULL;
1752 char *ref_str = NULL;
1753 enum kcgi_err kerr = KCGI_OK;
1755 if (strcmp(str2, "") != 0) {
1756 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1757 error = got_error_from_errno("asprintf");
1758 goto done;
1760 } else
1761 ref_str = strdup("");
1763 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1764 KATTR_ID, "header_commit_title", KATTR__MAX);
1765 if (kerr != KCGI_OK)
1766 goto done;
1767 kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
1768 if (kerr != KCGI_OK)
1769 goto done;
1770 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1771 if (kerr != KCGI_OK)
1772 goto done;
1773 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1774 KATTR_ID, "header_commit", KATTR__MAX);
1775 if (kerr != KCGI_OK)
1776 goto done;
1777 kerr = khtml_puts(gw_trans->gw_html_req, str1);
1778 if (kerr != KCGI_OK)
1779 goto done;
1780 kerr = khtml_puts(gw_trans->gw_html_req, " ");
1781 if (kerr != KCGI_OK)
1782 goto done;
1783 kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
1784 if (kerr != KCGI_OK)
1785 goto done;
1786 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1787 if (kerr != KCGI_OK)
1788 goto done;
1789 done:
1790 if (error == NULL && kerr != KCGI_OK)
1791 error = gw_kcgi_error(kerr);
1792 return error;
1795 static const struct got_error *
1796 gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
1798 const struct got_error *error = NULL;
1799 enum kcgi_err kerr = KCGI_OK;
1801 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1802 KATTR_ID, "header_diff_title", KATTR__MAX);
1803 if (kerr != KCGI_OK)
1804 goto done;
1805 kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
1806 if (kerr != KCGI_OK)
1807 goto done;
1808 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1809 if (kerr != KCGI_OK)
1810 goto done;
1811 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1812 KATTR_ID, "header_diff", KATTR__MAX);
1813 if (kerr != KCGI_OK)
1814 goto done;
1815 kerr = khtml_puts(gw_trans->gw_html_req, str1);
1816 if (kerr != KCGI_OK)
1817 goto done;
1818 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
1819 if (kerr != KCGI_OK)
1820 goto done;
1821 kerr = khtml_puts(gw_trans->gw_html_req, str2);
1822 if (kerr != KCGI_OK)
1823 goto done;
1824 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1825 if (kerr != KCGI_OK)
1826 goto done;
1827 done:
1828 if (error == NULL && kerr != KCGI_OK)
1829 error = gw_kcgi_error(kerr);
1830 return error;
1833 static const struct got_error *
1834 gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
1836 const struct got_error *error = NULL;
1837 enum kcgi_err kerr = KCGI_OK;
1839 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1840 KATTR_ID, "header_age_title", KATTR__MAX);
1841 if (kerr != KCGI_OK)
1842 goto done;
1843 kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
1844 if (kerr != KCGI_OK)
1845 goto done;
1846 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1847 if (kerr != KCGI_OK)
1848 goto done;
1849 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1850 KATTR_ID, "header_age", KATTR__MAX);
1851 if (kerr != KCGI_OK)
1852 goto done;
1853 kerr = khtml_puts(gw_trans->gw_html_req, str);
1854 if (kerr != KCGI_OK)
1855 goto done;
1856 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1857 if (kerr != KCGI_OK)
1858 goto done;
1859 done:
1860 if (error == NULL && kerr != KCGI_OK)
1861 error = gw_kcgi_error(kerr);
1862 return error;
1865 static const struct got_error *
1866 gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
1868 const struct got_error *error = NULL;
1869 enum kcgi_err kerr;
1871 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1872 KATTR_ID, "header_author_title", KATTR__MAX);
1873 if (kerr != KCGI_OK)
1874 goto done;
1875 kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
1876 if (kerr != KCGI_OK)
1877 goto done;
1878 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1879 if (kerr != KCGI_OK)
1880 goto done;
1881 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1882 KATTR_ID, "header_author", KATTR__MAX);
1883 if (kerr != KCGI_OK)
1884 goto done;
1885 kerr = khtml_puts(gw_trans->gw_html_req, str);
1886 if (kerr != KCGI_OK)
1887 goto done;
1888 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1889 if (kerr != KCGI_OK)
1890 goto done;
1891 done:
1892 if (error == NULL && kerr != KCGI_OK)
1893 error = gw_kcgi_error(kerr);
1894 return error;
1897 static const struct got_error *
1898 gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
1900 const struct got_error *error = NULL;
1901 enum kcgi_err kerr;
1903 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1904 KATTR_ID, "header_committer_title", KATTR__MAX);
1905 if (kerr != KCGI_OK)
1906 goto done;
1907 kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
1908 if (kerr != KCGI_OK)
1909 goto done;
1910 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1911 if (kerr != KCGI_OK)
1912 goto done;
1913 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1914 KATTR_ID, "header_committer", KATTR__MAX);
1915 if (kerr != KCGI_OK)
1916 goto done;
1917 kerr = khtml_puts(gw_trans->gw_html_req, str);
1918 if (kerr != KCGI_OK)
1919 goto done;
1920 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1921 if (kerr != KCGI_OK)
1922 goto done;
1923 done:
1924 if (error == NULL && kerr != KCGI_OK)
1925 error = gw_kcgi_error(kerr);
1926 return error;
1929 static const struct got_error *
1930 gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
1932 const struct got_error *error = NULL;
1933 enum kcgi_err kerr;
1935 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1936 KATTR_ID, "header_commit_msg_title", KATTR__MAX);
1937 if (kerr != KCGI_OK)
1938 goto done;
1939 kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
1940 if (kerr != KCGI_OK)
1941 goto done;
1942 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1943 if (kerr != KCGI_OK)
1944 goto done;
1945 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1946 KATTR_ID, "header_commit_msg", KATTR__MAX);
1947 if (kerr != KCGI_OK)
1948 goto done;
1949 kerr = khttp_puts(gw_trans->gw_req, str);
1950 if (kerr != KCGI_OK)
1951 goto done;
1952 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1953 if (kerr != KCGI_OK)
1954 goto done;
1955 done:
1956 if (error == NULL && kerr != KCGI_OK)
1957 error = gw_kcgi_error(kerr);
1958 return error;
1961 /* XXX: slated for deletion */
1962 static char *
1963 gw_gen_commit_msg_header_old(char *str)
1965 char *return_html = NULL;
1967 if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1968 return_html = strdup("");
1970 return return_html;
1973 static const struct got_error *
1974 gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
1976 const struct got_error *error = NULL;
1977 enum kcgi_err kerr;
1979 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1980 KATTR_ID, "header_tree_title", KATTR__MAX);
1981 if (kerr != KCGI_OK)
1982 goto done;
1983 kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
1984 if (kerr != KCGI_OK)
1985 goto done;
1986 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1987 if (kerr != KCGI_OK)
1988 goto done;
1989 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1990 KATTR_ID, "header_tree", KATTR__MAX);
1991 if (kerr != KCGI_OK)
1992 goto done;
1993 kerr = khtml_puts(gw_trans->gw_html_req, str);
1994 if (kerr != KCGI_OK)
1995 goto done;
1996 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1997 if (kerr != KCGI_OK)
1998 goto done;
1999 done:
2000 if (error == NULL && kerr != KCGI_OK)
2001 error = gw_kcgi_error(kerr);
2002 return error;
2005 static const struct got_error *
2006 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2007 char *dir)
2009 const struct got_error *error = NULL;
2010 FILE *f = NULL;
2011 char *d_file = NULL;
2012 unsigned int len;
2013 size_t n;
2015 *description = NULL;
2016 if (gw_trans->gw_conf->got_show_repo_description == 0)
2017 return gw_empty_string(description);
2019 if (asprintf(&d_file, "%s/description", dir) == -1)
2020 return got_error_from_errno("asprintf");
2022 f = fopen(d_file, "r");
2023 if (f == NULL) {
2024 if (errno == ENOENT || errno == EACCES)
2025 return gw_empty_string(description);
2026 error = got_error_from_errno2("fopen", d_file);
2027 goto done;
2030 if (fseek(f, 0, SEEK_END) == -1) {
2031 error = got_ferror(f, GOT_ERR_IO);
2032 goto done;
2034 len = ftell(f);
2035 if (len == -1) {
2036 error = got_ferror(f, GOT_ERR_IO);
2037 goto done;
2039 if (fseek(f, 0, SEEK_SET) == -1) {
2040 error = got_ferror(f, GOT_ERR_IO);
2041 goto done;
2043 *description = calloc(len + 1, sizeof(**description));
2044 if (*description == NULL) {
2045 error = got_error_from_errno("calloc");
2046 goto done;
2049 n = fread(*description, 1, len, f);
2050 if (n == 0 && ferror(f))
2051 error = got_ferror(f, GOT_ERR_IO);
2052 done:
2053 if (f != NULL && fclose(f) == -1 && error == NULL)
2054 error = got_error_from_errno("fclose");
2055 free(d_file);
2056 return error;
2059 static const struct got_error *
2060 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2062 struct tm tm;
2063 time_t diff_time;
2064 char *years = "years ago", *months = "months ago";
2065 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2066 char *minutes = "minutes ago", *seconds = "seconds ago";
2067 char *now = "right now";
2068 char *s;
2069 char datebuf[29];
2071 *repo_age = NULL;
2073 switch (ref_tm) {
2074 case TM_DIFF:
2075 diff_time = time(NULL) - committer_time;
2076 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2077 if (asprintf(repo_age, "%lld %s",
2078 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2079 return got_error_from_errno("asprintf");
2080 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2081 if (asprintf(repo_age, "%lld %s",
2082 (diff_time / 60 / 60 / 24 / (365 / 12)),
2083 months) == -1)
2084 return got_error_from_errno("asprintf");
2085 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2086 if (asprintf(repo_age, "%lld %s",
2087 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2088 return got_error_from_errno("asprintf");
2089 } else if (diff_time > 60 * 60 * 24 * 2) {
2090 if (asprintf(repo_age, "%lld %s",
2091 (diff_time / 60 / 60 / 24), days) == -1)
2092 return got_error_from_errno("asprintf");
2093 } else if (diff_time > 60 * 60 * 2) {
2094 if (asprintf(repo_age, "%lld %s",
2095 (diff_time / 60 / 60), hours) == -1)
2096 return got_error_from_errno("asprintf");
2097 } else if (diff_time > 60 * 2) {
2098 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2099 minutes) == -1)
2100 return got_error_from_errno("asprintf");
2101 } else if (diff_time > 2) {
2102 if (asprintf(repo_age, "%lld %s", diff_time,
2103 seconds) == -1)
2104 return got_error_from_errno("asprintf");
2105 } else {
2106 if (asprintf(repo_age, "%s", now) == -1)
2107 return got_error_from_errno("asprintf");
2109 break;
2110 case TM_LONG:
2111 if (gmtime_r(&committer_time, &tm) == NULL)
2112 return got_error_from_errno("gmtime_r");
2114 s = asctime_r(&tm, datebuf);
2115 if (s == NULL)
2116 return got_error_from_errno("asctime_r");
2118 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2119 return got_error_from_errno("asprintf");
2120 break;
2122 return NULL;
2125 static const struct got_error *
2126 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2127 char *repo_ref, int ref_tm)
2129 const struct got_error *error = NULL;
2130 struct got_object_id *id = NULL;
2131 struct got_repository *repo = NULL;
2132 struct got_commit_object *commit = NULL;
2133 struct got_reflist_head refs;
2134 struct got_reflist_entry *re;
2135 struct got_reference *head_ref;
2136 int is_head = 0;
2137 time_t committer_time = 0, cmp_time = 0;
2138 const char *refname;
2140 *repo_age = NULL;
2141 SIMPLEQ_INIT(&refs);
2143 if (repo_ref == NULL)
2144 return NULL;
2146 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2147 is_head = 1;
2149 if (gw_trans->gw_conf->got_show_repo_age == 0)
2150 return NULL;
2152 error = got_repo_open(&repo, dir, NULL);
2153 if (error)
2154 goto done;
2156 if (is_head)
2157 error = got_ref_list(&refs, repo, "refs/heads",
2158 got_ref_cmp_by_name, NULL);
2159 else
2160 error = got_ref_list(&refs, repo, repo_ref,
2161 got_ref_cmp_by_name, NULL);
2162 if (error)
2163 goto done;
2165 SIMPLEQ_FOREACH(re, &refs, entry) {
2166 if (is_head)
2167 refname = strdup(repo_ref);
2168 else
2169 refname = got_ref_get_name(re->ref);
2170 error = got_ref_open(&head_ref, repo, refname, 0);
2171 if (error)
2172 goto done;
2174 error = got_ref_resolve(&id, repo, head_ref);
2175 got_ref_close(head_ref);
2176 if (error)
2177 goto done;
2179 error = got_object_open_as_commit(&commit, repo, id);
2180 if (error)
2181 goto done;
2183 committer_time =
2184 got_object_commit_get_committer_time(commit);
2186 if (cmp_time < committer_time)
2187 cmp_time = committer_time;
2190 if (cmp_time != 0) {
2191 committer_time = cmp_time;
2192 error = gw_get_time_str(repo_age, committer_time, ref_tm);
2194 done:
2195 got_ref_list_free(&refs);
2196 free(id);
2197 return error;
2200 static const struct got_error *
2201 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
2203 const struct got_error *error;
2204 FILE *f = NULL;
2205 struct got_object_id *id1 = NULL, *id2 = NULL;
2206 char *label1 = NULL, *label2 = NULL, *line = NULL;
2207 int obj_type;
2208 size_t linesize = 0;
2209 ssize_t linelen;
2210 enum kcgi_err kerr = KCGI_OK;
2212 f = got_opentemp();
2213 if (f == NULL)
2214 return NULL;
2216 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2217 if (error)
2218 goto done;
2220 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2221 error = got_repo_match_object_id(&id1, &label1,
2222 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2223 if (error)
2224 goto done;
2227 error = got_repo_match_object_id(&id2, &label2,
2228 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2229 if (error)
2230 goto done;
2232 error = got_object_get_type(&obj_type, header->repo, id2);
2233 if (error)
2234 goto done;
2235 switch (obj_type) {
2236 case GOT_OBJ_TYPE_BLOB:
2237 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2238 header->repo, f);
2239 break;
2240 case GOT_OBJ_TYPE_TREE:
2241 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2242 header->repo, f);
2243 break;
2244 case GOT_OBJ_TYPE_COMMIT:
2245 error = got_diff_objects_as_commits(id1, id2, 3, 0,
2246 header->repo, f);
2247 break;
2248 default:
2249 error = got_error(GOT_ERR_OBJ_TYPE);
2251 if (error)
2252 goto done;
2254 if (fseek(f, 0, SEEK_SET) == -1) {
2255 error = got_ferror(f, GOT_ERR_IO);
2256 goto done;
2259 while ((linelen = getline(&line, &linesize, f)) != -1) {
2260 error = gw_colordiff_line(gw_trans, line);
2261 if (error)
2262 goto done;
2263 /* XXX: KHTML_PRETTY breaks this */
2264 kerr = khtml_puts(gw_trans->gw_html_req, line);
2265 if (kerr != KCGI_OK)
2266 goto done;
2267 kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2268 if (kerr != KCGI_OK)
2269 goto done;
2271 if (linelen == -1 && ferror(f))
2272 error = got_error_from_errno("getline");
2273 done:
2274 if (f && fclose(f) == -1 && error == NULL)
2275 error = got_error_from_errno("fclose");
2276 free(line);
2277 free(label1);
2278 free(label2);
2279 free(id1);
2280 free(id2);
2282 if (error == NULL && kerr != KCGI_OK)
2283 error = gw_kcgi_error(kerr);
2284 return error;
2287 static const struct got_error *
2288 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2290 const struct got_error *error = NULL;
2291 struct got_repository *repo;
2292 const char *gitconfig_owner;
2294 *owner = NULL;
2296 if (gw_trans->gw_conf->got_show_repo_owner == 0)
2297 return NULL;
2299 error = got_repo_open(&repo, dir, NULL);
2300 if (error)
2301 return error;
2302 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2303 if (gitconfig_owner) {
2304 *owner = strdup(gitconfig_owner);
2305 if (*owner == NULL)
2306 error = got_error_from_errno("strdup");
2308 got_repo_close(repo);
2309 return error;
2312 static const struct got_error *
2313 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2315 const struct got_error *error = NULL;
2316 FILE *f;
2317 char *d_file = NULL;
2318 unsigned int len;
2319 size_t n;
2321 *url = NULL;
2323 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2324 return got_error_from_errno("asprintf");
2326 f = fopen(d_file, "r");
2327 if (f == NULL) {
2328 if (errno != ENOENT && errno != EACCES)
2329 error = got_error_from_errno2("fopen", d_file);
2330 goto done;
2333 if (fseek(f, 0, SEEK_END) == -1) {
2334 error = got_ferror(f, GOT_ERR_IO);
2335 goto done;
2337 len = ftell(f);
2338 if (len == -1) {
2339 error = got_ferror(f, GOT_ERR_IO);
2340 goto done;
2342 if (fseek(f, 0, SEEK_SET) == -1) {
2343 error = got_ferror(f, GOT_ERR_IO);
2344 goto done;
2347 *url = calloc(len + 1, sizeof(**url));
2348 if (*url == NULL) {
2349 error = got_error_from_errno("calloc");
2350 goto done;
2353 n = fread(*url, 1, len, f);
2354 if (n == 0 && ferror(f))
2355 error = got_ferror(f, GOT_ERR_IO);
2356 done:
2357 if (f && fclose(f) == -1 && error == NULL)
2358 error = got_error_from_errno("fclose");
2359 free(d_file);
2360 return NULL;
2363 static const struct got_error *
2364 gw_get_repo_tags(char **tag_html, struct gw_trans *gw_trans,
2365 struct gw_header *header, int limit, int tag_type)
2367 const struct got_error *error = NULL;
2368 struct got_repository *repo = NULL;
2369 struct got_reflist_head refs;
2370 struct got_reflist_entry *re;
2371 char *tag_row = NULL, *tags_navs_disp = NULL;
2372 char *age = NULL, *age_html = NULL, *newline;
2373 char *escaped_tagger = NULL, *escaped_tag_commit = NULL;
2374 char *id_str = NULL, *refstr = NULL;
2375 char *tag_commit0 = NULL;
2376 struct buf *diffbuf = NULL;
2377 size_t newsize;
2378 struct got_tag_object *tag = NULL;
2380 *tag_html = NULL;
2382 SIMPLEQ_INIT(&refs);
2384 error = buf_alloc(&diffbuf, 0);
2385 if (error)
2386 return NULL;
2388 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2389 if (error)
2390 goto done;
2392 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2393 if (error)
2394 goto done;
2396 SIMPLEQ_FOREACH(re, &refs, entry) {
2397 const char *refname;
2398 const char *tagger;
2399 const char *tag_commit;
2400 time_t tagger_time;
2401 struct got_object_id *id;
2403 refname = got_ref_get_name(re->ref);
2404 if (strncmp(refname, "refs/tags/", 10) != 0)
2405 continue;
2406 refname += 10;
2407 refstr = got_ref_to_str(re->ref);
2408 if (refstr == NULL) {
2409 error = got_error_from_errno("got_ref_to_str");
2410 goto done;
2413 error = got_ref_resolve(&id, repo, re->ref);
2414 if (error)
2415 goto done;
2416 error = got_object_open_as_tag(&tag, repo, id);
2417 free(id);
2418 if (error)
2419 goto done;
2421 tagger = got_object_tag_get_tagger(tag);
2422 tagger_time = got_object_tag_get_tagger_time(tag);
2424 error = got_object_id_str(&id_str,
2425 got_object_tag_get_object_id(tag));
2426 if (error)
2427 goto done;
2429 tag_commit0 = strdup(got_object_tag_get_message(tag));
2430 if (tag_commit0 == NULL) {
2431 error = got_error_from_errno("strdup");
2432 goto done;
2435 tag_commit = tag_commit0;
2436 while (*tag_commit == '\n')
2437 tag_commit++;
2439 switch (tag_type) {
2440 case TAGBRIEF:
2441 newline = strchr(tag_commit, '\n');
2442 if (newline)
2443 *newline = '\0';
2445 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2446 if (error)
2447 goto done;
2449 if (asprintf(&tags_navs_disp, tags_navs,
2450 gw_trans->repo_name, id_str, gw_trans->repo_name,
2451 id_str, gw_trans->repo_name, id_str,
2452 gw_trans->repo_name, id_str) == -1) {
2453 error = got_error_from_errno("asprintf");
2454 goto done;
2457 if (asprintf(&tag_row, tags_row, age ? age : "",
2458 refname, tag_commit, tags_navs_disp) == -1) {
2459 error = got_error_from_errno("asprintf");
2460 goto done;
2463 free(tags_navs_disp);
2464 break;
2465 case TAGFULL:
2466 error = gw_get_time_str(&age, tagger_time, TM_LONG);
2467 if (error)
2468 goto done;
2469 error = gw_html_escape(&escaped_tagger, tagger);
2470 if (error)
2471 goto done;
2472 error = gw_html_escape(&escaped_tag_commit, tag_commit);
2473 if (error)
2474 goto done;
2475 if (asprintf(&tag_row, tag_info, age ? age : "",
2476 escaped_tagger, escaped_tag_commit) == -1) {
2477 error = got_error_from_errno("asprintf");
2478 goto done;
2480 break;
2481 default:
2482 break;
2485 error = buf_puts(&newsize, diffbuf, tag_row);
2486 if (error)
2487 goto done;
2489 if (limit && --limit == 0)
2490 break;
2492 got_object_tag_close(tag);
2493 tag = NULL;
2494 free(id_str);
2495 id_str = NULL;
2496 free(refstr);
2497 refstr = NULL;
2498 free(age);
2499 age = NULL;
2500 free(age_html);
2501 age_html = NULL;
2502 free(escaped_tagger);
2503 escaped_tagger = NULL;
2504 free(escaped_tag_commit);
2505 escaped_tag_commit = NULL;
2506 free(tag_commit0);
2507 tag_commit0 = NULL;
2508 free(tag_row);
2509 tag_row = NULL;
2512 if (buf_len(diffbuf) > 0) {
2513 error = buf_putc(diffbuf, '\0');
2514 *tag_html = strdup(buf_get(diffbuf));
2515 if (*tag_html == NULL)
2516 error = got_error_from_errno("strdup");
2518 done:
2519 if (tag)
2520 got_object_tag_close(tag);
2521 free(id_str);
2522 free(refstr);
2523 free(age);
2524 free(age_html);
2525 free(escaped_tagger);
2526 free(escaped_tag_commit);
2527 free(tag_commit0);
2528 free(tag_row);
2529 buf_free(diffbuf);
2530 got_ref_list_free(&refs);
2531 if (repo)
2532 got_repo_close(repo);
2533 return error;
2536 static void
2537 gw_free_headers(struct gw_header *header)
2539 free(header->id);
2540 free(header->path);
2541 if (header->commit != NULL)
2542 got_object_commit_close(header->commit);
2543 if (header->repo)
2544 got_repo_close(header->repo);
2545 free(header->refs_str);
2546 free(header->commit_id);
2547 free(header->parent_id);
2548 free(header->tree_id);
2549 free(header->commit_msg);
2552 static struct gw_header *
2553 gw_init_header()
2555 struct gw_header *header;
2557 header = malloc(sizeof(*header));
2558 if (header == NULL)
2559 return NULL;
2561 header->repo = NULL;
2562 header->commit = NULL;
2563 header->id = NULL;
2564 header->path = NULL;
2565 SIMPLEQ_INIT(&header->refs);
2567 return header;
2570 static const struct got_error *
2571 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2572 int limit)
2574 const struct got_error *error = NULL;
2575 struct got_commit_graph *graph = NULL;
2577 error = got_commit_graph_open(&graph, header->path, 0);
2578 if (error)
2579 goto done;
2581 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2582 NULL, NULL);
2583 if (error)
2584 goto done;
2586 for (;;) {
2587 error = got_commit_graph_iter_next(&header->id, graph,
2588 header->repo, NULL, NULL);
2589 if (error) {
2590 if (error->code == GOT_ERR_ITER_COMPLETED)
2591 error = NULL;
2592 goto done;
2594 if (header->id == NULL)
2595 goto done;
2597 error = got_object_open_as_commit(&header->commit, header->repo,
2598 header->id);
2599 if (error)
2600 goto done;
2602 error = gw_get_commit(gw_trans, header);
2603 if (limit > 1) {
2604 struct gw_header *n_header = NULL;
2605 if ((n_header = gw_init_header()) == NULL) {
2606 error = got_error_from_errno("malloc");
2607 goto done;
2610 n_header->refs_str = strdup(header->refs_str);
2611 n_header->commit_id = strdup(header->commit_id);
2612 n_header->parent_id = strdup(header->parent_id);
2613 n_header->tree_id = strdup(header->tree_id);
2614 n_header->author = strdup(header->author);
2615 n_header->committer = strdup(header->committer);
2616 n_header->commit_msg = strdup(header->commit_msg);
2617 n_header->committer_time = header->committer_time;
2618 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2619 entry);
2621 if (error || (limit && --limit == 0))
2622 break;
2624 done:
2625 if (graph)
2626 got_commit_graph_close(graph);
2627 return error;
2630 static const struct got_error *
2631 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2633 const struct got_error *error = NULL;
2634 struct got_reflist_entry *re;
2635 struct got_object_id *id2 = NULL;
2636 struct got_object_qid *parent_id;
2637 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2639 /*print commit*/
2640 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2641 char *s;
2642 const char *name;
2643 struct got_tag_object *tag = NULL;
2644 int cmp;
2646 name = got_ref_get_name(re->ref);
2647 if (strcmp(name, GOT_REF_HEAD) == 0)
2648 continue;
2649 if (strncmp(name, "refs/", 5) == 0)
2650 name += 5;
2651 if (strncmp(name, "got/", 4) == 0)
2652 continue;
2653 if (strncmp(name, "heads/", 6) == 0)
2654 name += 6;
2655 if (strncmp(name, "remotes/", 8) == 0)
2656 name += 8;
2657 if (strncmp(name, "tags/", 5) == 0) {
2658 error = got_object_open_as_tag(&tag, header->repo,
2659 re->id);
2660 if (error) {
2661 if (error->code != GOT_ERR_OBJ_TYPE)
2662 continue;
2664 * Ref points at something other
2665 * than a tag.
2667 error = NULL;
2668 tag = NULL;
2671 cmp = got_object_id_cmp(tag ?
2672 got_object_tag_get_object_id(tag) : re->id, header->id);
2673 if (tag)
2674 got_object_tag_close(tag);
2675 if (cmp != 0)
2676 continue;
2677 s = refs_str;
2678 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2679 s ? ", " : "", name) == -1) {
2680 error = got_error_from_errno("asprintf");
2681 free(s);
2682 return error;
2684 header->refs_str = strdup(refs_str);
2685 free(s);
2688 if (refs_str == NULL)
2689 header->refs_str = strdup("");
2690 free(refs_str);
2692 error = got_object_id_str(&header->commit_id, header->id);
2693 if (error)
2694 return error;
2696 error = got_object_id_str(&header->tree_id,
2697 got_object_commit_get_tree_id(header->commit));
2698 if (error)
2699 return error;
2701 if (gw_trans->action == GW_DIFF) {
2702 parent_id = SIMPLEQ_FIRST(
2703 got_object_commit_get_parent_ids(header->commit));
2704 if (parent_id != NULL) {
2705 id2 = got_object_id_dup(parent_id->id);
2706 free (parent_id);
2707 error = got_object_id_str(&header->parent_id, id2);
2708 if (error)
2709 return error;
2710 free(id2);
2711 } else
2712 header->parent_id = strdup("/dev/null");
2713 } else
2714 header->parent_id = strdup("");
2716 header->committer_time =
2717 got_object_commit_get_committer_time(header->commit);
2719 header->author =
2720 got_object_commit_get_author(header->commit);
2721 header->committer =
2722 got_object_commit_get_committer(header->commit);
2723 if (error)
2724 return error;
2726 /* XXX Doesn't the log message require escaping? */
2727 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2728 if (error)
2729 return error;
2731 commit_msg = commit_msg0;
2732 while (*commit_msg == '\n')
2733 commit_msg++;
2735 header->commit_msg = strdup(commit_msg);
2736 free(commit_msg0);
2737 return error;
2740 static const struct got_error *
2741 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2743 const struct got_error *error = NULL;
2744 char *in_repo_path = NULL;
2746 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2747 if (error)
2748 return error;
2750 if (gw_trans->commit == NULL) {
2751 struct got_reference *head_ref;
2752 error = got_ref_open(&head_ref, header->repo,
2753 gw_trans->headref, 0);
2754 if (error)
2755 return error;
2757 error = got_ref_resolve(&header->id, header->repo, head_ref);
2758 got_ref_close(head_ref);
2759 if (error)
2760 return error;
2762 error = got_object_open_as_commit(&header->commit,
2763 header->repo, header->id);
2764 } else {
2765 struct got_reference *ref;
2766 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2767 if (error == NULL) {
2768 int obj_type;
2769 error = got_ref_resolve(&header->id, header->repo, ref);
2770 got_ref_close(ref);
2771 if (error)
2772 return error;
2773 error = got_object_get_type(&obj_type, header->repo,
2774 header->id);
2775 if (error)
2776 return error;
2777 if (obj_type == GOT_OBJ_TYPE_TAG) {
2778 struct got_tag_object *tag;
2779 error = got_object_open_as_tag(&tag,
2780 header->repo, header->id);
2781 if (error)
2782 return error;
2783 if (got_object_tag_get_object_type(tag) !=
2784 GOT_OBJ_TYPE_COMMIT) {
2785 got_object_tag_close(tag);
2786 error = got_error(GOT_ERR_OBJ_TYPE);
2787 return error;
2789 free(header->id);
2790 header->id = got_object_id_dup(
2791 got_object_tag_get_object_id(tag));
2792 if (header->id == NULL)
2793 error = got_error_from_errno(
2794 "got_object_id_dup");
2795 got_object_tag_close(tag);
2796 if (error)
2797 return error;
2798 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2799 error = got_error(GOT_ERR_OBJ_TYPE);
2800 return error;
2802 error = got_object_open_as_commit(&header->commit,
2803 header->repo, header->id);
2804 if (error)
2805 return error;
2807 if (header->commit == NULL) {
2808 error = got_repo_match_object_id_prefix(&header->id,
2809 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2810 header->repo);
2811 if (error)
2812 return error;
2814 error = got_repo_match_object_id_prefix(&header->id,
2815 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2816 header->repo);
2819 error = got_repo_map_path(&in_repo_path, header->repo,
2820 gw_trans->repo_path, 1);
2821 if (error)
2822 return error;
2824 if (in_repo_path) {
2825 header->path = strdup(in_repo_path);
2827 free(in_repo_path);
2829 error = got_ref_list(&header->refs, header->repo, NULL,
2830 got_ref_cmp_by_name, NULL);
2831 if (error)
2832 return error;
2834 error = gw_get_commits(gw_trans, header, limit);
2835 return error;
2838 struct blame_line {
2839 int annotated;
2840 char *id_str;
2841 char *committer;
2842 char datebuf[11]; /* YYYY-MM-DD + NUL */
2845 struct gw_blame_cb_args {
2846 struct blame_line *lines;
2847 int nlines;
2848 int nlines_prec;
2849 int lineno_cur;
2850 off_t *line_offsets;
2851 FILE *f;
2852 struct got_repository *repo;
2853 struct gw_trans *gw_trans;
2854 struct buf *blamebuf;
2857 static const struct got_error *
2858 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2860 const struct got_error *err = NULL;
2861 struct gw_blame_cb_args *a = arg;
2862 struct blame_line *bline;
2863 char *line = NULL;
2864 size_t linesize = 0, newsize;
2865 struct got_commit_object *commit = NULL;
2866 off_t offset;
2867 struct tm tm;
2868 time_t committer_time;
2870 if (nlines != a->nlines ||
2871 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2872 return got_error(GOT_ERR_RANGE);
2874 if (lineno == -1)
2875 return NULL; /* no change in this commit */
2877 /* Annotate this line. */
2878 bline = &a->lines[lineno - 1];
2879 if (bline->annotated)
2880 return NULL;
2881 err = got_object_id_str(&bline->id_str, id);
2882 if (err)
2883 return err;
2885 err = got_object_open_as_commit(&commit, a->repo, id);
2886 if (err)
2887 goto done;
2889 bline->committer = strdup(got_object_commit_get_committer(commit));
2890 if (bline->committer == NULL) {
2891 err = got_error_from_errno("strdup");
2892 goto done;
2895 committer_time = got_object_commit_get_committer_time(commit);
2896 if (localtime_r(&committer_time, &tm) == NULL)
2897 return got_error_from_errno("localtime_r");
2898 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2899 &tm) >= sizeof(bline->datebuf)) {
2900 err = got_error(GOT_ERR_NO_SPACE);
2901 goto done;
2903 bline->annotated = 1;
2905 /* Print lines annotated so far. */
2906 bline = &a->lines[a->lineno_cur - 1];
2907 if (!bline->annotated)
2908 goto done;
2910 offset = a->line_offsets[a->lineno_cur - 1];
2911 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2912 err = got_error_from_errno("fseeko");
2913 goto done;
2916 while (bline->annotated) {
2917 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2918 *line_escape = NULL;
2919 size_t len;
2921 if (getline(&line, &linesize, a->f) == -1) {
2922 if (ferror(a->f))
2923 err = got_error_from_errno("getline");
2924 break;
2927 committer = bline->committer;
2928 smallerthan = strchr(committer, '<');
2929 if (smallerthan && smallerthan[1] != '\0')
2930 committer = smallerthan + 1;
2931 at = strchr(committer, '@');
2932 if (at)
2933 *at = '\0';
2934 len = strlen(committer);
2935 if (len >= 9)
2936 committer[8] = '\0';
2938 nl = strchr(line, '\n');
2939 if (nl)
2940 *nl = '\0';
2942 err = gw_html_escape(&line_escape, line);
2943 if (err)
2944 goto err;
2946 if (a->gw_trans->repo_folder == NULL)
2947 a->gw_trans->repo_folder = strdup("");
2948 if (a->gw_trans->repo_folder == NULL)
2949 goto err;
2950 asprintf(&blame_row, blame_line, a->nlines_prec, a->lineno_cur,
2951 a->gw_trans->repo_name, bline->id_str,
2952 a->gw_trans->repo_file, a->gw_trans->repo_folder,
2953 bline->id_str, bline->datebuf, committer, line_escape);
2954 a->lineno_cur++;
2955 err = buf_puts(&newsize, a->blamebuf, blame_row);
2956 if (err)
2957 return err;
2959 bline = &a->lines[a->lineno_cur - 1];
2960 err:
2961 free(line_escape);
2962 free(blame_row);
2964 done:
2965 if (commit)
2966 got_object_commit_close(commit);
2967 free(line);
2968 return err;
2971 static const struct got_error *
2972 gw_get_file_blame_blob(char **blame_html, struct gw_trans *gw_trans)
2974 const struct got_error *error = NULL;
2975 struct got_repository *repo = NULL;
2976 struct got_object_id *obj_id = NULL;
2977 struct got_object_id *commit_id = NULL;
2978 struct got_blob_object *blob = NULL;
2979 char *path = NULL, *in_repo_path = NULL;
2980 struct gw_blame_cb_args bca;
2981 int i, obj_type;
2982 size_t filesize;
2984 *blame_html = NULL;
2986 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2987 if (error)
2988 return error;
2990 if (asprintf(&path, "%s%s%s",
2991 gw_trans->repo_folder ? gw_trans->repo_folder : "",
2992 gw_trans->repo_folder ? "/" : "",
2993 gw_trans->repo_file) == -1) {
2994 error = got_error_from_errno("asprintf");
2995 goto done;
2998 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2999 if (error)
3000 goto done;
3002 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3003 GOT_OBJ_TYPE_COMMIT, 1, repo);
3004 if (error)
3005 goto done;
3007 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3008 if (error)
3009 goto done;
3011 if (obj_id == NULL) {
3012 error = got_error(GOT_ERR_NO_OBJ);
3013 goto done;
3016 error = got_object_get_type(&obj_type, repo, obj_id);
3017 if (error)
3018 goto done;
3020 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3021 error = got_error(GOT_ERR_OBJ_TYPE);
3022 goto done;
3025 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3026 if (error)
3027 goto done;
3029 error = buf_alloc(&bca.blamebuf, 0);
3030 if (error)
3031 goto done;
3033 bca.f = got_opentemp();
3034 if (bca.f == NULL) {
3035 error = got_error_from_errno("got_opentemp");
3036 goto done;
3038 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3039 &bca.line_offsets, bca.f, blob);
3040 if (error || bca.nlines == 0)
3041 goto done;
3043 /* Don't include \n at EOF in the blame line count. */
3044 if (bca.line_offsets[bca.nlines - 1] == filesize)
3045 bca.nlines--;
3047 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3048 if (bca.lines == NULL) {
3049 error = got_error_from_errno("calloc");
3050 goto done;
3052 bca.lineno_cur = 1;
3053 bca.nlines_prec = 0;
3054 i = bca.nlines;
3055 while (i > 0) {
3056 i /= 10;
3057 bca.nlines_prec++;
3059 bca.repo = repo;
3060 bca.gw_trans = gw_trans;
3062 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3063 NULL, NULL);
3064 if (error)
3065 goto done;
3066 if (buf_len(bca.blamebuf) > 0) {
3067 error = buf_putc(bca.blamebuf, '\0');
3068 if (error)
3069 goto done;
3070 *blame_html = strdup(buf_get(bca.blamebuf));
3071 if (*blame_html == NULL) {
3072 error = got_error_from_errno("strdup");
3073 goto done;
3076 done:
3077 free(bca.line_offsets);
3078 free(bca.blamebuf);
3079 free(in_repo_path);
3080 free(commit_id);
3081 free(obj_id);
3082 free(path);
3084 for (i = 0; i < bca.nlines; i++) {
3085 struct blame_line *bline = &bca.lines[i];
3086 free(bline->id_str);
3087 free(bline->committer);
3089 free(bca.lines);
3090 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3091 error = got_error_from_errno("fclose");
3092 if (blob)
3093 got_object_blob_close(blob);
3094 if (repo)
3095 got_repo_close(repo);
3096 return error;
3099 static const struct got_error *
3100 gw_output_blob_buf(struct gw_trans *gw_trans)
3102 const struct got_error *error = NULL;
3103 struct got_repository *repo = NULL;
3104 struct got_object_id *obj_id = NULL;
3105 struct got_object_id *commit_id = NULL;
3106 struct got_blob_object *blob = NULL;
3107 char *path = NULL, *in_repo_path = NULL;
3108 int obj_type, set_mime = 0;
3109 size_t len, hdrlen;
3110 const uint8_t *buf;
3111 enum kcgi_err kerr = KCGI_OK;
3113 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3114 if (error)
3115 return error;
3117 if (asprintf(&path, "%s%s%s",
3118 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3119 gw_trans->repo_folder ? "/" : "",
3120 gw_trans->repo_file) == -1) {
3121 error = got_error_from_errno("asprintf");
3122 goto done;
3125 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3126 if (error)
3127 goto done;
3129 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3130 GOT_OBJ_TYPE_COMMIT, 1, repo);
3131 if (error)
3132 goto done;
3134 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3135 if (error)
3136 goto done;
3138 if (obj_id == NULL) {
3139 error = got_error(GOT_ERR_NO_OBJ);
3140 goto done;
3143 error = got_object_get_type(&obj_type, repo, obj_id);
3144 if (error)
3145 goto done;
3147 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3148 error = got_error(GOT_ERR_OBJ_TYPE);
3149 goto done;
3152 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3153 if (error)
3154 goto done;
3156 hdrlen = got_object_blob_get_hdrlen(blob);
3157 do {
3158 error = got_object_blob_read_block(&len, blob);
3159 if (error)
3160 goto done;
3161 if (len == 0)
3162 break;
3163 buf = got_object_blob_get_read_buf(blob);
3166 * Skip blob object header first time around,
3167 * which also contains a zero byte.
3169 buf += hdrlen;
3170 if (set_mime == 0) {
3171 if (isbinary(buf, len - hdrlen))
3172 gw_trans->mime = KMIME_APP_OCTET_STREAM;
3173 else
3174 gw_trans->mime = KMIME_TEXT_PLAIN;
3175 set_mime = 1;
3176 error = gw_display_index(gw_trans);
3177 if (error)
3178 goto done;
3180 khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3181 hdrlen = 0;
3182 } while (len != 0);
3183 done:
3184 free(in_repo_path);
3185 free(commit_id);
3186 free(obj_id);
3187 free(path);
3188 if (blob)
3189 got_object_blob_close(blob);
3190 if (repo)
3191 got_repo_close(repo);
3192 if (error == NULL && kerr != KCGI_OK)
3193 error = gw_kcgi_error(kerr);
3194 return error;
3197 static const struct got_error *
3198 gw_get_repo_tree(char **tree_html, struct gw_trans *gw_trans)
3200 const struct got_error *error = NULL;
3201 struct got_repository *repo = NULL;
3202 struct got_object_id *tree_id = NULL, *commit_id = NULL;
3203 struct got_tree_object *tree = NULL;
3204 struct buf *diffbuf = NULL;
3205 size_t newsize;
3206 char *path = NULL, *in_repo_path = NULL, *tree_row = NULL;
3207 char *id_str = NULL;
3208 char *build_folder = NULL;
3209 char *url_html = NULL;
3210 const char *class = NULL;
3211 int nentries, i, class_flip = 0;
3213 *tree_html = NULL;
3215 error = buf_alloc(&diffbuf, 0);
3216 if (error)
3217 return error;
3219 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3220 if (error)
3221 goto done;
3223 if (gw_trans->repo_folder != NULL)
3224 path = strdup(gw_trans->repo_folder);
3225 else {
3226 error = got_repo_map_path(&in_repo_path, repo,
3227 gw_trans->repo_path, 1);
3228 if (error)
3229 goto done;
3230 free(path);
3231 path = in_repo_path;
3234 if (gw_trans->commit == NULL) {
3235 struct got_reference *head_ref;
3236 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3237 if (error)
3238 goto done;
3239 error = got_ref_resolve(&commit_id, repo, head_ref);
3240 if (error)
3241 goto done;
3242 got_ref_close(head_ref);
3244 } else {
3245 error = got_repo_match_object_id(&commit_id, NULL,
3246 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3247 if (error)
3248 goto done;
3251 error = got_object_id_str(&gw_trans->commit, commit_id);
3252 if (error)
3253 goto done;
3255 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3256 if (error)
3257 goto done;
3259 error = got_object_open_as_tree(&tree, repo, tree_id);
3260 if (error)
3261 goto done;
3263 nentries = got_object_tree_get_nentries(tree);
3264 for (i = 0; i < nentries; i++) {
3265 struct got_tree_entry *te;
3266 const char *modestr = "";
3267 mode_t mode;
3269 te = got_object_tree_get_entry(tree, i);
3271 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3272 if (error)
3273 goto done;
3275 mode = got_tree_entry_get_mode(te);
3276 if (got_object_tree_entry_is_submodule(te))
3277 modestr = "$";
3278 else if (S_ISLNK(mode))
3279 modestr = "@";
3280 else if (S_ISDIR(mode))
3281 modestr = "/";
3282 else if (mode & S_IXUSR)
3283 modestr = "*";
3285 if (class_flip == 0) {
3286 class = "back_lightgray";
3287 class_flip = 1;
3288 } else {
3289 class = "back_white";
3290 class_flip = 0;
3293 if (S_ISDIR(mode)) {
3294 if (asprintf(&build_folder, "%s/%s",
3295 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3296 got_tree_entry_get_name(te)) == -1) {
3297 error = got_error_from_errno(
3298 "asprintf");
3299 goto done;
3302 if (asprintf(&url_html, folder_html,
3303 gw_trans->repo_name, gw_trans->action_name,
3304 gw_trans->commit, build_folder,
3305 got_tree_entry_get_name(te), modestr) == -1) {
3306 error = got_error_from_errno("asprintf");
3307 goto done;
3309 if (asprintf(&tree_row, tree_line, class, url_html,
3310 class) == -1) {
3311 error = got_error_from_errno("asprintf");
3312 goto done;
3314 } else {
3315 if (asprintf(&url_html, file_html, gw_trans->repo_name,
3316 "blob", gw_trans->commit,
3317 got_tree_entry_get_name(te),
3318 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3319 got_tree_entry_get_name(te), modestr) == -1) {
3320 error = got_error_from_errno("asprintf");
3321 goto done;
3324 if (asprintf(&tree_row, tree_line_with_navs, class,
3325 url_html, class, gw_trans->repo_name, "blob",
3326 gw_trans->commit, got_tree_entry_get_name(te),
3327 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3328 "blob", gw_trans->repo_name,
3329 "blame", gw_trans->commit,
3330 got_tree_entry_get_name(te),
3331 gw_trans->repo_folder ? gw_trans->repo_folder : "",
3332 "blame") == -1) {
3333 error = got_error_from_errno("asprintf");
3334 goto done;
3338 error = buf_puts(&newsize, diffbuf, tree_row);
3339 if (error)
3340 goto done;
3342 free(id_str);
3343 id_str = NULL;
3344 free(url_html);
3345 url_html = NULL;
3346 free(tree_row);
3347 tree_row = NULL;
3348 free(build_folder);
3349 build_folder = NULL;
3352 if (buf_len(diffbuf) > 0) {
3353 error = buf_putc(diffbuf, '\0');
3354 if (error)
3355 goto done;
3356 *tree_html = strdup(buf_get(diffbuf));
3357 if (*tree_html == NULL) {
3358 error = got_error_from_errno("strdup");
3359 goto done;
3362 done:
3363 if (tree)
3364 got_object_tree_close(tree);
3365 if (repo)
3366 got_repo_close(repo);
3368 free(id_str);
3369 free(url_html);
3370 free(tree_row);
3371 free(in_repo_path);
3372 free(tree_id);
3373 free(diffbuf);
3374 free(build_folder);
3375 return error;
3378 static const struct got_error *
3379 gw_get_repo_heads(char **head_html, struct gw_trans *gw_trans)
3381 const struct got_error *error = NULL;
3382 struct got_repository *repo = NULL;
3383 struct got_reflist_head refs;
3384 struct got_reflist_entry *re;
3385 char *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
3386 struct buf *diffbuf = NULL;
3387 size_t newsize;
3389 *head_html = NULL;
3391 SIMPLEQ_INIT(&refs);
3393 error = buf_alloc(&diffbuf, 0);
3394 if (error)
3395 return NULL;
3397 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3398 if (error)
3399 goto done;
3401 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3402 NULL);
3403 if (error)
3404 goto done;
3406 SIMPLEQ_FOREACH(re, &refs, entry) {
3407 char *refname;
3409 refname = strdup(got_ref_get_name(re->ref));
3410 if (refname == NULL) {
3411 error = got_error_from_errno("got_ref_to_str");
3412 goto done;
3415 if (strncmp(refname, "refs/heads/", 11) != 0) {
3416 free(refname);
3417 continue;
3420 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3421 refname, TM_DIFF);
3422 if (error)
3423 goto done;
3425 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
3426 refname, gw_trans->repo_name, refname,
3427 gw_trans->repo_name, refname, gw_trans->repo_name,
3428 refname) == -1) {
3429 error = got_error_from_errno("asprintf");
3430 goto done;
3433 if (strncmp(refname, "refs/heads/", 11) == 0)
3434 refname += 11;
3436 if (asprintf(&head_row, heads_row, age, refname,
3437 head_navs_disp) == -1) {
3438 error = got_error_from_errno("asprintf");
3439 goto done;
3442 error = buf_puts(&newsize, diffbuf, head_row);
3444 free(head_navs_disp);
3445 free(head_row);
3448 if (buf_len(diffbuf) > 0) {
3449 error = buf_putc(diffbuf, '\0');
3450 *head_html = strdup(buf_get(diffbuf));
3451 if (*head_html == NULL)
3452 error = got_error_from_errno("strdup");
3454 done:
3455 buf_free(diffbuf);
3456 got_ref_list_free(&refs);
3457 if (repo)
3458 got_repo_close(repo);
3459 return error;
3462 static char *
3463 gw_get_site_link(struct gw_trans *gw_trans)
3465 char *link = NULL, *repo = NULL, *action = NULL;
3467 if (gw_trans->repo_name != NULL &&
3468 asprintf(&repo, " / <a href='?path=%s&action=summary'>%s</a>",
3469 gw_trans->repo_name, gw_trans->repo_name) == -1)
3470 return NULL;
3472 if (gw_trans->action_name != NULL &&
3473 asprintf(&action, " / %s", gw_trans->action_name) == -1) {
3474 free(repo);
3475 return NULL;
3478 if (asprintf(&link, site_link, GOTWEB,
3479 gw_trans->gw_conf->got_site_link,
3480 repo ? repo : "", action ? action : "") == -1) {
3481 free(repo);
3482 free(action);
3483 return NULL;
3486 free(repo);
3487 free(action);
3488 return link;
3491 static const struct got_error *
3492 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
3494 const struct got_error *error = NULL;
3495 char *color = NULL;
3496 enum kcgi_err kerr = KCGI_OK;
3498 if (strncmp(buf, "-", 1) == 0)
3499 color = "diff_minus";
3500 else if (strncmp(buf, "+", 1) == 0)
3501 color = "diff_plus";
3502 else if (strncmp(buf, "@@", 2) == 0)
3503 color = "diff_chunk_header";
3504 else if (strncmp(buf, "@@", 2) == 0)
3505 color = "diff_chunk_header";
3506 else if (strncmp(buf, "commit +", 8) == 0)
3507 color = "diff_meta";
3508 else if (strncmp(buf, "commit -", 8) == 0)
3509 color = "diff_meta";
3510 else if (strncmp(buf, "blob +", 6) == 0)
3511 color = "diff_meta";
3512 else if (strncmp(buf, "blob -", 6) == 0)
3513 color = "diff_meta";
3514 else if (strncmp(buf, "file +", 6) == 0)
3515 color = "diff_meta";
3516 else if (strncmp(buf, "file -", 6) == 0)
3517 color = "diff_meta";
3518 else if (strncmp(buf, "from:", 5) == 0)
3519 color = "diff_author";
3520 else if (strncmp(buf, "via:", 4) == 0)
3521 color = "diff_author";
3522 else if (strncmp(buf, "date:", 5) == 0)
3523 color = "diff_date";
3524 kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3525 "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
3526 if (error == NULL && kerr != KCGI_OK)
3527 error = gw_kcgi_error(kerr);
3528 return error;
3532 * XXX This function should not exist.
3533 * We should let khtml_puts(3) handle HTML escaping.
3535 static const struct got_error *
3536 gw_html_escape(char **escaped_html, const char *orig_html)
3538 const struct got_error *error = NULL;
3539 struct escape_pair {
3540 char c;
3541 const char *s;
3542 } esc[] = {
3543 { '>', "&gt;" },
3544 { '<', "&lt;" },
3545 { '&', "&amp;" },
3546 { '"', "&quot;" },
3547 { '\'', "&apos;" },
3548 { '\n', "<br />" },
3550 size_t orig_len, len;
3551 int i, j, x;
3553 orig_len = strlen(orig_html);
3554 len = orig_len;
3555 for (i = 0; i < orig_len; i++) {
3556 for (j = 0; j < nitems(esc); j++) {
3557 if (orig_html[i] != esc[j].c)
3558 continue;
3559 len += strlen(esc[j].s) - 1 /* escaped char */;
3563 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
3564 if (*escaped_html == NULL)
3565 return got_error_from_errno("calloc");
3567 x = 0;
3568 for (i = 0; i < orig_len; i++) {
3569 int escaped = 0;
3570 for (j = 0; j < nitems(esc); j++) {
3571 if (orig_html[i] != esc[j].c)
3572 continue;
3574 if (strlcat(*escaped_html, esc[j].s, len + 1)
3575 >= len + 1) {
3576 error = got_error(GOT_ERR_NO_SPACE);
3577 goto done;
3579 x += strlen(esc[j].s);
3580 escaped = 1;
3581 break;
3583 if (!escaped) {
3584 (*escaped_html)[x] = orig_html[i];
3585 x++;
3588 done:
3589 if (error) {
3590 free(*escaped_html);
3591 *escaped_html = NULL;
3592 } else {
3593 (*escaped_html)[x] = '\0';
3595 return error;
3598 int
3599 main(int argc, char *argv[])
3601 const struct got_error *error = NULL;
3602 struct gw_trans *gw_trans;
3603 struct gw_dir *dir = NULL, *tdir;
3604 const char *page = "index";
3605 int gw_malloc = 1;
3606 enum kcgi_err kerr;
3608 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3609 errx(1, "malloc");
3611 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3612 errx(1, "malloc");
3614 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3615 errx(1, "malloc");
3617 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3618 errx(1, "malloc");
3620 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3621 if (kerr != KCGI_OK) {
3622 error = gw_kcgi_error(kerr);
3623 goto done;
3626 if ((gw_trans->gw_conf =
3627 malloc(sizeof(struct gotweb_conf))) == NULL) {
3628 gw_malloc = 0;
3629 error = got_error_from_errno("malloc");
3630 goto done;
3633 TAILQ_INIT(&gw_trans->gw_dirs);
3634 TAILQ_INIT(&gw_trans->gw_headers);
3636 gw_trans->page = 0;
3637 gw_trans->repos_total = 0;
3638 gw_trans->repo_path = NULL;
3639 gw_trans->commit = NULL;
3640 gw_trans->headref = strdup(GOT_REF_HEAD);
3641 gw_trans->mime = KMIME_TEXT_HTML;
3642 gw_trans->gw_tmpl->key = gw_templs;
3643 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3644 gw_trans->gw_tmpl->arg = gw_trans;
3645 gw_trans->gw_tmpl->cb = gw_template;
3646 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3647 if (error)
3648 goto done;
3650 error = gw_parse_querystring(gw_trans);
3651 if (error)
3652 goto done;
3654 if (gw_trans->action == GW_BLOB)
3655 error = gw_blob(gw_trans);
3656 else
3657 error = gw_display_index(gw_trans);
3658 done:
3659 if (error) {
3660 gw_trans->mime = KMIME_TEXT_PLAIN;
3661 gw_trans->action = GW_ERR;
3662 gw_display_error(gw_trans, error);
3664 if (gw_malloc) {
3665 free(gw_trans->gw_conf->got_repos_path);
3666 free(gw_trans->gw_conf->got_site_name);
3667 free(gw_trans->gw_conf->got_site_owner);
3668 free(gw_trans->gw_conf->got_site_link);
3669 free(gw_trans->gw_conf->got_logo);
3670 free(gw_trans->gw_conf->got_logo_url);
3671 free(gw_trans->gw_conf);
3672 free(gw_trans->commit);
3673 free(gw_trans->repo_path);
3674 free(gw_trans->repo_name);
3675 free(gw_trans->repo_file);
3676 free(gw_trans->action_name);
3677 free(gw_trans->headref);
3679 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3680 free(dir->name);
3681 free(dir->description);
3682 free(dir->age);
3683 free(dir->url);
3684 free(dir->path);
3685 free(dir);
3690 khttp_free(gw_trans->gw_req);
3691 return 0;