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 char *author;
91 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 **, struct gw_trans *,
162 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 **, struct gw_trans *);
169 static const struct got_error *gw_get_file_read_blob(char **, size_t *,
170 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(char **, 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 char *gw_get_repo_heads(struct gw_trans *);
177 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *, char *);
178 static char *gw_get_got_link(struct gw_trans *);
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(char **, char *);
183 static char *gw_gen_commit_header(char *, char*);
184 static char *gw_gen_diff_header(char *, char*);
185 static char *gw_gen_author_header(char *);
186 static char *gw_gen_committer_header(char *);
187 static char *gw_gen_commit_msg_header(char *);
188 static char *gw_gen_tree_header(char *);
190 static void gw_free_headers(struct gw_header *);
191 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
192 enum kmime);
193 static const struct got_error* gw_display_index(struct gw_trans *);
194 static void gw_display_error(struct gw_trans *,
195 const struct got_error *);
197 static int gw_template(size_t, void *);
199 static const struct got_error* gw_get_header(struct gw_trans *,
200 struct gw_header *, int);
201 static const struct got_error* gw_get_commits(struct gw_trans *,
202 struct gw_header *, int);
203 static const struct got_error* gw_get_commit(struct gw_trans *,
204 struct gw_header *);
205 static const struct got_error* gw_apply_unveil(const char *, const char *);
206 static const struct got_error* gw_blame_cb(void *, int, int,
207 struct got_object_id *);
208 static const struct got_error* gw_load_got_paths(struct gw_trans *);
209 static const struct got_error* gw_load_got_path(struct gw_trans *,
210 struct gw_dir *);
211 static const struct got_error* gw_parse_querystring(struct gw_trans *);
213 static const struct got_error* gw_blame(struct gw_trans *);
214 static const struct got_error* gw_blob(struct gw_trans *);
215 static const struct got_error* gw_diff(struct gw_trans *);
216 static const struct got_error* gw_index(struct gw_trans *);
217 static const struct got_error* gw_commits(struct gw_trans *);
218 static const struct got_error* gw_briefs(struct gw_trans *);
219 static const struct got_error* gw_summary(struct gw_trans *);
220 static const struct got_error* gw_tree(struct gw_trans *);
221 static const struct got_error* gw_tag(struct gw_trans *);
223 struct gw_query_action {
224 unsigned int func_id;
225 const char *func_name;
226 const struct got_error *(*func_main)(struct gw_trans *);
227 char *template;
228 };
230 enum gw_query_actions {
231 GW_BLAME,
232 GW_BLOB,
233 GW_BRIEFS,
234 GW_COMMITS,
235 GW_DIFF,
236 GW_ERR,
237 GW_INDEX,
238 GW_SUMMARY,
239 GW_TAG,
240 GW_TREE,
241 };
243 static struct gw_query_action gw_query_funcs[] = {
244 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
245 { GW_BLOB, "blob", NULL, NULL },
246 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
247 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
248 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
249 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
250 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
251 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
252 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
253 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
254 };
256 static const struct got_error *
257 gw_kcgi_error(enum kcgi_err kerr)
259 if (kerr == KCGI_OK)
260 return NULL;
262 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
263 return got_error(GOT_ERR_CANCELLED);
265 if (kerr == KCGI_ENOMEM)
266 return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
268 if (kerr == KCGI_ENFILE)
269 return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
271 if (kerr == KCGI_EAGAIN)
272 return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
274 if (kerr == KCGI_FORM)
275 return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
277 return got_error_from_errno(kcgi_strerror(kerr));
280 static const struct got_error *
281 gw_apply_unveil(const char *repo_path, const char *repo_file)
283 const struct got_error *err;
285 if (repo_path && repo_file) {
286 char *full_path;
287 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
288 return got_error_from_errno("asprintf unveil");
289 if (unveil(full_path, "r") != 0)
290 return got_error_from_errno2("unveil", full_path);
293 if (repo_path && unveil(repo_path, "r") != 0)
294 return got_error_from_errno2("unveil", repo_path);
296 if (unveil("/tmp", "rwc") != 0)
297 return got_error_from_errno2("unveil", "/tmp");
299 err = got_privsep_unveil_exec_helpers();
300 if (err != NULL)
301 return err;
303 if (unveil(NULL, NULL) != 0)
304 return got_error_from_errno("unveil");
306 return NULL;
309 static const struct got_error *
310 gw_empty_string(char **s)
312 *s = strdup("");
313 if (*s == NULL)
314 return got_error_from_errno("strdup");
315 return NULL;
318 static int
319 isbinary(const char *buf, size_t n)
321 return (memchr(buf, '\0', n) != NULL);
325 static const struct got_error *
326 gw_blame(struct gw_trans *gw_trans)
328 const struct got_error *error = NULL;
329 struct gw_header *header = NULL;
330 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
331 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
332 enum kcgi_err kerr;
334 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
335 NULL) == -1)
336 return got_error_from_errno("pledge");
338 if ((header = gw_init_header()) == NULL)
339 return got_error_from_errno("malloc");
341 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
342 if (error)
343 goto done;
345 error = gw_get_header(gw_trans, header, 1);
346 if (error)
347 goto done;
349 error = gw_get_file_blame_blob(&blame_html, gw_trans);
350 if (error)
351 goto done;
353 error = gw_get_time_str(&age, header->committer_time, TM_LONG);
354 if (error)
355 goto done;
356 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
357 error = got_error_from_errno("asprintf");
358 goto done;
361 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
362 if (error)
363 goto done;
364 if (asprintf(&blame_html_disp, blame_header, age_html,
365 gw_gen_commit_msg_header(escaped_commit_msg), blame_html) == -1) {
366 error = got_error_from_errno("asprintf");
367 goto done;
370 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
371 error = got_error_from_errno("asprintf");
372 goto done;
375 kerr = khttp_puts(gw_trans->gw_req, blame);
376 if (kerr != KCGI_OK)
377 error = gw_kcgi_error(kerr);
378 done:
379 got_ref_list_free(&header->refs);
380 gw_free_headers(header);
381 free(blame_html_disp);
382 free(blame_html);
383 free(blame);
384 free(escaped_commit_msg);
385 return error;
388 static const struct got_error *
389 gw_blob(struct gw_trans *gw_trans)
391 const struct got_error *error = NULL;
392 struct gw_header *header = NULL;
393 char *content = NULL;
394 size_t filesize = 0;
395 enum kcgi_err kerr;
397 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
398 NULL) == -1)
399 return got_error_from_errno("pledge");
401 if ((header = gw_init_header()) == NULL)
402 return got_error_from_errno("malloc");
404 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
405 if (error)
406 goto done;
408 error = gw_get_header(gw_trans, header, 1);
409 if (error)
410 goto done;
412 error = gw_get_file_read_blob(&content, &filesize, gw_trans);
413 if (error)
414 goto done;
416 if (isbinary(content, filesize))
417 gw_trans->mime = KMIME_APP_OCTET_STREAM;
418 else
419 gw_trans->mime = KMIME_TEXT_PLAIN;
421 error = gw_display_index(gw_trans);
422 if (error)
423 goto done;
425 kerr = khttp_write(gw_trans->gw_req, content, filesize);
426 if (kerr != KCGI_OK)
427 error = gw_kcgi_error(kerr);
428 done:
429 got_ref_list_free(&header->refs);
430 gw_free_headers(header);
431 free(content);
432 return error;
435 static const struct got_error *
436 gw_diff(struct gw_trans *gw_trans)
438 const struct got_error *error = NULL;
439 struct gw_header *header = NULL;
440 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
441 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
442 enum kcgi_err kerr;
444 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
445 NULL) == -1)
446 return got_error_from_errno("pledge");
448 if ((header = gw_init_header()) == NULL)
449 return got_error_from_errno("malloc");
451 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
452 if (error)
453 goto done;
455 error = gw_get_header(gw_trans, header, 1);
456 if (error)
457 goto done;
459 error = gw_get_diff(&diff_html, gw_trans, header);
460 if (error)
461 goto done;
463 error = gw_get_time_str(&age, header->committer_time, TM_LONG);
464 if (error)
465 goto done;
466 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
467 error = got_error_from_errno("asprintf");
468 goto done;
470 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
471 if (error)
472 goto done;
473 if (asprintf(&diff_html_disp, diff_header,
474 gw_gen_diff_header(header->parent_id, header->commit_id),
475 gw_gen_commit_header(header->commit_id, header->refs_str),
476 gw_gen_tree_header(header->tree_id),
477 gw_gen_author_header(header->author),
478 gw_gen_committer_header(header->committer), age_html,
479 gw_gen_commit_msg_header(escaped_commit_msg),
480 diff_html ? diff_html : "") == -1) {
481 error = got_error_from_errno("asprintf");
482 goto done;
485 if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
486 error = got_error_from_errno("asprintf");
487 goto done;
490 kerr = khttp_puts(gw_trans->gw_req, diff);
491 if (kerr != KCGI_OK)
492 error = gw_kcgi_error(kerr);
493 done:
494 got_ref_list_free(&header->refs);
495 gw_free_headers(header);
496 free(diff_html_disp);
497 free(diff_html);
498 free(diff);
499 free(age);
500 free(age_html);
501 free(escaped_commit_msg);
502 return error;
505 static const struct got_error *
506 gw_index(struct gw_trans *gw_trans)
508 const struct got_error *error = NULL;
509 struct gw_dir *gw_dir = NULL;
510 char *html, *navs, *next, *prev;
511 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
512 enum kcgi_err kerr;
514 if (pledge("stdio rpath proc exec sendfd unveil",
515 NULL) == -1) {
516 error = got_error_from_errno("pledge");
517 return error;
520 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
521 if (error)
522 return error;
524 error = gw_load_got_paths(gw_trans);
525 if (error)
526 return error;
528 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
529 if (kerr != KCGI_OK)
530 return gw_kcgi_error(kerr);
532 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
533 if (asprintf(&html, index_projects_empty,
534 gw_trans->gw_conf->got_repos_path) == -1)
535 return got_error_from_errno("asprintf");
536 kerr = khttp_puts(gw_trans->gw_req, html);
537 if (kerr != KCGI_OK)
538 error = gw_kcgi_error(kerr);
539 free(html);
540 return error;
543 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
544 dir_c++;
546 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
547 if (gw_trans->page > 0 && (gw_trans->page *
548 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
549 prev_disp++;
550 continue;
553 prev_disp++;
555 if (error)
556 return error;
557 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
558 gw_dir->name, gw_dir->name) == -1)
559 return got_error_from_errno("asprintf");
561 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
562 gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
563 gw_dir->age,
564 navs) == -1)
565 return got_error_from_errno("asprintf");
567 kerr = khttp_puts(gw_trans->gw_req, html);
568 free(navs);
569 free(html);
570 if (kerr != KCGI_OK)
571 return gw_kcgi_error(kerr);
573 if (gw_trans->gw_conf->got_max_repos_display == 0)
574 continue;
576 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
577 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
578 if (kerr != KCGI_OK)
579 return gw_kcgi_error(kerr);
580 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
581 (gw_trans->page > 0) &&
582 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
583 prev_disp == gw_trans->repos_total)) {
584 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
585 if (kerr != KCGI_OK)
586 return gw_kcgi_error(kerr);
589 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
590 (gw_trans->page > 0) &&
591 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
592 prev_disp == gw_trans->repos_total)) {
593 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
594 return got_error_from_errno("asprintf");
595 kerr = khttp_puts(gw_trans->gw_req, prev);
596 free(prev);
597 if (kerr != KCGI_OK)
598 return gw_kcgi_error(kerr);
601 kerr = khttp_puts(gw_trans->gw_req, div_end);
602 if (kerr != KCGI_OK)
603 return gw_kcgi_error(kerr);
605 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
606 next_disp == gw_trans->gw_conf->got_max_repos_display &&
607 dir_c != (gw_trans->page + 1) *
608 gw_trans->gw_conf->got_max_repos_display) {
609 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
610 return got_error_from_errno("calloc");
611 kerr = khttp_puts(gw_trans->gw_req, next);
612 free(next);
613 if (kerr != KCGI_OK)
614 return gw_kcgi_error(kerr);
615 kerr = khttp_puts(gw_trans->gw_req, div_end);
616 if (kerr != KCGI_OK)
617 error = gw_kcgi_error(kerr);
618 next_disp = 0;
619 break;
622 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
623 (gw_trans->page > 0) &&
624 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
625 prev_disp == gw_trans->repos_total)) {
626 kerr = khttp_puts(gw_trans->gw_req, div_end);
627 if (kerr != KCGI_OK)
628 return gw_kcgi_error(kerr);
631 next_disp++;
633 return error;
636 static const struct got_error *
637 gw_commits(struct gw_trans *gw_trans)
639 const struct got_error *error = NULL;
640 char *commits_html, *commits_navs_html;
641 struct gw_header *header = NULL, *n_header = NULL;
642 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
643 enum kcgi_err kerr;
645 if ((header = gw_init_header()) == NULL)
646 return got_error_from_errno("malloc");
648 if (pledge("stdio rpath proc exec sendfd unveil",
649 NULL) == -1) {
650 error = got_error_from_errno("pledge");
651 goto done;
654 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
655 if (error)
656 goto done;
658 error = gw_get_header(gw_trans, header,
659 gw_trans->gw_conf->got_max_commits_display);
660 if (error)
661 goto done;
663 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
664 if (kerr != KCGI_OK) {
665 error = gw_kcgi_error(kerr);
666 goto done;
668 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
669 if (asprintf(&commits_navs_html, commits_navs,
670 gw_trans->repo_name, n_header->commit_id,
671 gw_trans->repo_name, n_header->commit_id,
672 gw_trans->repo_name, n_header->commit_id) == -1) {
673 error = got_error_from_errno("asprintf");
674 goto done;
676 error = gw_get_time_str(&age, n_header->committer_time,
677 TM_LONG);
678 if (error)
679 goto done;
680 if (asprintf(&age_html, header_age_html, age ? age : "")
681 == -1) {
682 error = got_error_from_errno("asprintf");
683 goto done;
685 error = gw_html_escape(&escaped_commit_msg,
686 n_header->commit_msg);
687 if (error)
688 goto done;
689 if (asprintf(&commits_html, commits_line,
690 gw_gen_commit_header(n_header->commit_id,
691 n_header->refs_str),
692 gw_gen_author_header(n_header->author),
693 gw_gen_committer_header(n_header->committer),
694 age_html, escaped_commit_msg,
695 commits_navs_html) == -1) {
696 error = got_error_from_errno("asprintf");
697 goto done;
699 free(age);
700 age = NULL;
701 free(age_html);
702 age_html = NULL;
703 free(escaped_commit_msg);
704 escaped_commit_msg = NULL;
705 kerr = khttp_puts(gw_trans->gw_req, commits_html);
706 if (kerr != KCGI_OK) {
707 error = gw_kcgi_error(kerr);
708 goto done;
711 kerr = khttp_puts(gw_trans->gw_req, div_end);
712 if (kerr != KCGI_OK)
713 error = gw_kcgi_error(kerr);
714 done:
715 got_ref_list_free(&header->refs);
716 gw_free_headers(header);
717 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
718 gw_free_headers(n_header);
719 free(age);
720 free(age_html);
721 free(escaped_commit_msg);
722 return error;
725 static const struct got_error *
726 gw_briefs(struct gw_trans *gw_trans)
728 const struct got_error *error = NULL;
729 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
730 struct gw_header *header = NULL, *n_header = NULL;
731 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
732 enum kcgi_err kerr;
734 if ((header = gw_init_header()) == NULL)
735 return got_error_from_errno("malloc");
737 if (pledge("stdio rpath proc exec sendfd unveil",
738 NULL) == -1) {
739 error = got_error_from_errno("pledge");
740 goto done;
743 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
744 if (error)
745 goto done;
747 if (gw_trans->action == GW_SUMMARY)
748 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
749 else
750 error = gw_get_header(gw_trans, header,
751 gw_trans->gw_conf->got_max_commits_display);
752 if (error)
753 goto done;
755 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
756 if (kerr != KCGI_OK) {
757 error = gw_kcgi_error(kerr);
758 goto done;
761 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
762 if (asprintf(&briefs_navs_html, briefs_navs,
763 gw_trans->repo_name, n_header->commit_id,
764 gw_trans->repo_name, n_header->commit_id,
765 gw_trans->repo_name, n_header->commit_id) == -1) {
766 error = got_error_from_errno("asprintf");
767 goto done;
769 newline = strchr(n_header->commit_msg, '\n');
770 if (newline)
771 *newline = '\0';
772 error = gw_get_time_str(&age, n_header->committer_time,
773 TM_DIFF);
774 if (error)
775 goto done;
776 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
777 error = got_error_from_errno("asprintf");
778 goto done;
780 error = gw_html_escape(&escaped_commit_msg,
781 n_header->commit_msg);
782 if (error)
783 goto done;
784 if (asprintf(&briefs_html, briefs_line, age_html,
785 n_header->author, escaped_commit_msg,
786 briefs_navs_html) == -1) {
787 error = got_error_from_errno("asprintf");
788 goto done;
790 free(age);
791 age = NULL;
792 free(age_html);
793 age_html = NULL;
794 free(escaped_commit_msg);
795 escaped_commit_msg = NULL;
796 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
797 if (kerr != KCGI_OK) {
798 error = gw_kcgi_error(kerr);
799 goto done;
802 kerr = khttp_puts(gw_trans->gw_req, div_end);
803 if (kerr != KCGI_OK)
804 error = gw_kcgi_error(kerr);
805 done:
806 got_ref_list_free(&header->refs);
807 gw_free_headers(header);
808 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
809 gw_free_headers(n_header);
810 free(age);
811 free(age_html);
812 free(escaped_commit_msg);
813 return error;
816 static const struct got_error *
817 gw_summary(struct gw_trans *gw_trans)
819 const struct got_error *error = NULL;
820 char *description_html = NULL, *repo_owner_html = NULL;
821 char *age = NULL, *repo_age_html = NULL, *cloneurl_html = NULL;
822 char *tags = NULL, *tags_html = NULL;
823 char *heads = NULL, *heads_html = NULL;
824 enum kcgi_err kerr;
826 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
827 return got_error_from_errno("pledge");
829 /* unveil is applied with gw_briefs below */
831 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
832 if (kerr != KCGI_OK)
833 return gw_kcgi_error(kerr);
835 if (gw_trans->gw_conf->got_show_repo_description) {
836 if (gw_trans->gw_dir->description != NULL &&
837 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
838 if (asprintf(&description_html, description,
839 gw_trans->gw_dir->description) == -1) {
840 error = got_error_from_errno("asprintf");
841 goto done;
844 kerr = khttp_puts(gw_trans->gw_req, description_html);
845 if (kerr != KCGI_OK) {
846 error = gw_kcgi_error(kerr);
847 goto done;
852 if (gw_trans->gw_conf->got_show_repo_owner &&
853 gw_trans->gw_dir->owner != NULL) {
854 if (asprintf(&repo_owner_html, repo_owner,
855 gw_trans->gw_dir->owner) == -1) {
856 error = got_error_from_errno("asprintf");
857 goto done;
860 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
861 if (kerr != KCGI_OK) {
862 error = gw_kcgi_error(kerr);
863 goto done;
867 if (gw_trans->gw_conf->got_show_repo_age) {
868 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
869 "refs/heads", TM_LONG);
870 if (error)
871 goto done;
872 if (age != NULL) {
873 if (asprintf(&repo_age_html, last_change, age) == -1) {
874 error = got_error_from_errno("asprintf");
875 goto done;
878 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
879 if (kerr != KCGI_OK) {
880 error = gw_kcgi_error(kerr);
881 goto done;
886 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
887 if (gw_trans->gw_dir->url != NULL &&
888 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
889 if (asprintf(&cloneurl_html, cloneurl,
890 gw_trans->gw_dir->url) == -1) {
891 error = got_error_from_errno("asprintf");
892 goto done;
895 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
896 if (kerr != KCGI_OK) {
897 error = gw_kcgi_error(kerr);
898 goto done;
902 kerr = khttp_puts(gw_trans->gw_req, div_end);
903 if (kerr != KCGI_OK) {
904 error = gw_kcgi_error(kerr);
905 goto done;
908 error = gw_briefs(gw_trans);
909 if (error)
910 goto done;
912 error = gw_get_repo_tags(&tags, gw_trans, NULL, D_MAXSLCOMMDISP,
913 TAGBRIEF);
914 if (error)
915 goto done;
917 heads = gw_get_repo_heads(gw_trans);
919 if (tags != NULL && strcmp(tags, "") != 0) {
920 if (asprintf(&tags_html, summary_tags, tags) == -1) {
921 error = got_error_from_errno("asprintf");
922 goto done;
924 kerr = khttp_puts(gw_trans->gw_req, tags_html);
925 if (kerr != KCGI_OK) {
926 error = gw_kcgi_error(kerr);
927 goto done;
931 if (heads != NULL && strcmp(heads, "") != 0) {
932 if (asprintf(&heads_html, summary_heads, heads) == -1) {
933 error = got_error_from_errno("asprintf");
934 goto done;
936 kerr = khttp_puts(gw_trans->gw_req, heads_html);
937 if (kerr != KCGI_OK) {
938 error = gw_kcgi_error(kerr);
939 goto done;
942 done:
943 free(description_html);
944 free(repo_owner_html);
945 free(age);
946 free(repo_age_html);
947 free(cloneurl_html);
948 free(tags);
949 free(tags_html);
950 free(heads);
951 free(heads_html);
952 return error;
955 static const struct got_error *
956 gw_tree(struct gw_trans *gw_trans)
958 const struct got_error *error = NULL;
959 struct gw_header *header = NULL;
960 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
961 char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
962 enum kcgi_err kerr;
964 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
965 return got_error_from_errno("pledge");
967 if ((header = gw_init_header()) == NULL)
968 return got_error_from_errno("malloc");
970 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
971 if (error)
972 goto done;
974 error = gw_get_header(gw_trans, header, 1);
975 if (error)
976 goto done;
978 error = gw_get_repo_tree(&tree_html, gw_trans);
979 if (error)
980 goto done;
982 error = gw_get_time_str(&age, header->committer_time, TM_LONG);
983 if (error)
984 goto done;
985 if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
986 error = got_error_from_errno("asprintf");
987 goto done;
989 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
990 if (error)
991 goto done;
992 if (asprintf(&tree_html_disp, tree_header, age_html,
993 gw_gen_commit_msg_header(escaped_commit_msg),
994 tree_html ? tree_html : "") == -1) {
995 error = got_error_from_errno("asprintf");
996 goto done;
999 if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
1000 error = got_error_from_errno("asprintf");
1001 goto done;
1004 kerr = khttp_puts(gw_trans->gw_req, tree);
1005 if (kerr != KCGI_OK)
1006 error = gw_kcgi_error(kerr);
1007 done:
1008 got_ref_list_free(&header->refs);
1009 gw_free_headers(header);
1010 free(tree_html_disp);
1011 free(tree_html);
1012 free(tree);
1013 free(age);
1014 free(age_html);
1015 free(escaped_commit_msg);
1016 return error;
1019 static const struct got_error *
1020 gw_tag(struct gw_trans *gw_trans)
1022 const struct got_error *error = NULL;
1023 struct gw_header *header = NULL;
1024 char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
1025 char *escaped_commit_msg = NULL;
1026 enum kcgi_err kerr;
1028 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1029 return got_error_from_errno("pledge");
1031 if ((header = gw_init_header()) == NULL)
1032 return got_error_from_errno("malloc");
1034 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1035 if (error)
1036 goto done;
1038 error = gw_get_header(gw_trans, header, 1);
1039 if (error)
1040 goto done;
1042 error = gw_get_repo_tags(&tag_html, gw_trans, header, 1, TAGFULL);
1043 if (error)
1044 goto done;
1046 error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1047 if (error)
1048 goto done;
1049 if (asprintf(&tag_html_disp, tag_header,
1050 gw_gen_commit_header(header->commit_id, header->refs_str),
1051 gw_gen_commit_msg_header(escaped_commit_msg),
1052 tag_html ? tag_html : "") == -1) {
1053 error = got_error_from_errno("asprintf");
1054 goto done;
1057 if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
1058 error = got_error_from_errno("asprintf");
1059 goto done;
1062 kerr = khttp_puts(gw_trans->gw_req, tag);
1063 if (kerr != KCGI_OK)
1064 error = gw_kcgi_error(kerr);
1065 done:
1066 got_ref_list_free(&header->refs);
1067 gw_free_headers(header);
1068 free(tag_html_disp);
1069 free(tag_html);
1070 free(tag);
1071 free(escaped_commit_msg);
1072 return error;
1075 static const struct got_error *
1076 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1078 const struct got_error *error = NULL;
1079 DIR *dt;
1080 char *dir_test;
1081 int opened = 0;
1083 if (asprintf(&dir_test, "%s/%s/%s",
1084 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1085 GOTWEB_GIT_DIR) == -1)
1086 return got_error_from_errno("asprintf");
1088 dt = opendir(dir_test);
1089 if (dt == NULL) {
1090 free(dir_test);
1091 } else {
1092 gw_dir->path = strdup(dir_test);
1093 opened = 1;
1094 goto done;
1097 if (asprintf(&dir_test, "%s/%s/%s",
1098 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1099 GOTWEB_GOT_DIR) == -1)
1100 return got_error_from_errno("asprintf");
1102 dt = opendir(dir_test);
1103 if (dt == NULL)
1104 free(dir_test);
1105 else {
1106 opened = 1;
1107 error = got_error(GOT_ERR_NOT_GIT_REPO);
1108 goto errored;
1111 if (asprintf(&dir_test, "%s/%s",
1112 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1113 return got_error_from_errno("asprintf");
1115 gw_dir->path = strdup(dir_test);
1117 done:
1118 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1119 gw_dir->path);
1120 if (error)
1121 goto errored;
1122 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1123 if (error)
1124 goto errored;
1125 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1126 "refs/heads", TM_DIFF);
1127 if (error)
1128 goto errored;
1129 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1130 errored:
1131 free(dir_test);
1132 if (opened)
1133 closedir(dt);
1134 return error;
1137 static const struct got_error *
1138 gw_load_got_paths(struct gw_trans *gw_trans)
1140 const struct got_error *error = NULL;
1141 DIR *d;
1142 struct dirent **sd_dent;
1143 struct gw_dir *gw_dir;
1144 struct stat st;
1145 unsigned int d_cnt, d_i;
1147 d = opendir(gw_trans->gw_conf->got_repos_path);
1148 if (d == NULL) {
1149 error = got_error_from_errno2("opendir",
1150 gw_trans->gw_conf->got_repos_path);
1151 return error;
1154 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1155 alphasort);
1156 if (d_cnt == -1) {
1157 error = got_error_from_errno2("scandir",
1158 gw_trans->gw_conf->got_repos_path);
1159 return error;
1162 for (d_i = 0; d_i < d_cnt; d_i++) {
1163 if (gw_trans->gw_conf->got_max_repos > 0 &&
1164 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1165 break; /* account for parent and self */
1167 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1168 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1169 continue;
1171 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1172 return got_error_from_errno("gw_dir malloc");
1174 error = gw_load_got_path(gw_trans, gw_dir);
1175 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1176 continue;
1177 else if (error)
1178 return error;
1180 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1181 !got_path_dir_is_empty(gw_dir->path)) {
1182 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1183 entry);
1184 gw_trans->repos_total++;
1188 closedir(d);
1189 return error;
1192 static const struct got_error *
1193 gw_parse_querystring(struct gw_trans *gw_trans)
1195 const struct got_error *error = NULL;
1196 struct kpair *p;
1197 struct gw_query_action *action = NULL;
1198 unsigned int i;
1200 if (gw_trans->gw_req->fieldnmap[0]) {
1201 error = got_error_from_errno("bad parse");
1202 return error;
1203 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1204 /* define gw_trans->repo_path */
1205 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1206 return got_error_from_errno("asprintf");
1208 if (asprintf(&gw_trans->repo_path, "%s/%s",
1209 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1210 return got_error_from_errno("asprintf");
1212 /* get action and set function */
1213 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1214 for (i = 0; i < nitems(gw_query_funcs); i++) {
1215 action = &gw_query_funcs[i];
1216 if (action->func_name == NULL)
1217 continue;
1219 if (strcmp(action->func_name,
1220 p->parsed.s) == 0) {
1221 gw_trans->action = i;
1222 if (asprintf(&gw_trans->action_name,
1223 "%s", action->func_name) == -1)
1224 return
1225 got_error_from_errno(
1226 "asprintf");
1228 break;
1231 action = NULL;
1234 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1235 if (asprintf(&gw_trans->commit, "%s",
1236 p->parsed.s) == -1)
1237 return got_error_from_errno("asprintf");
1239 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1240 if (asprintf(&gw_trans->repo_file, "%s",
1241 p->parsed.s) == -1)
1242 return got_error_from_errno("asprintf");
1244 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1245 if (asprintf(&gw_trans->repo_folder, "%s",
1246 p->parsed.s) == -1)
1247 return got_error_from_errno("asprintf");
1249 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1250 if (asprintf(&gw_trans->headref, "%s",
1251 p->parsed.s) == -1)
1252 return got_error_from_errno("asprintf");
1254 if (action == NULL) {
1255 error = got_error_from_errno("invalid action");
1256 return error;
1258 if ((gw_trans->gw_dir =
1259 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1260 return got_error_from_errno("gw_dir malloc");
1262 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1263 if (error)
1264 return error;
1265 } else
1266 gw_trans->action = GW_INDEX;
1268 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1269 gw_trans->page = p->parsed.i;
1271 return error;
1274 static struct gw_dir *
1275 gw_init_gw_dir(char *dir)
1277 struct gw_dir *gw_dir;
1279 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1280 return NULL;
1282 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1283 return NULL;
1285 return gw_dir;
1288 static const struct got_error *
1289 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1291 enum kcgi_err kerr;
1293 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1294 if (kerr != KCGI_OK)
1295 return gw_kcgi_error(kerr);
1296 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1297 khttps[code]);
1298 if (kerr != KCGI_OK)
1299 return gw_kcgi_error(kerr);
1300 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1301 kmimetypes[mime]);
1302 if (kerr != KCGI_OK)
1303 return gw_kcgi_error(kerr);
1304 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1305 "nosniff");
1306 if (kerr != KCGI_OK)
1307 return gw_kcgi_error(kerr);
1308 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1309 if (kerr != KCGI_OK)
1310 return gw_kcgi_error(kerr);
1311 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1312 "1; mode=block");
1313 if (kerr != KCGI_OK)
1314 return gw_kcgi_error(kerr);
1316 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1317 kerr = khttp_head(gw_trans->gw_req,
1318 kresps[KRESP_CONTENT_DISPOSITION],
1319 "attachment; filename=%s", gw_trans->repo_file);
1320 if (kerr != KCGI_OK)
1321 return gw_kcgi_error(kerr);
1324 kerr = khttp_body(gw_trans->gw_req);
1325 return gw_kcgi_error(kerr);
1328 static const struct got_error *
1329 gw_display_index(struct gw_trans *gw_trans)
1331 const struct got_error *error;
1332 enum kcgi_err kerr;
1334 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1335 if (error)
1336 return error;
1338 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1339 if (kerr)
1340 return gw_kcgi_error(kerr);
1342 if (gw_trans->action != GW_BLOB) {
1343 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1344 gw_query_funcs[gw_trans->action].template);
1345 if (kerr != KCGI_OK) {
1346 khtml_close(gw_trans->gw_html_req);
1347 return gw_kcgi_error(kerr);
1351 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1354 static void
1355 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1357 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1358 return;
1360 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1361 return;
1362 khtml_puts(gw_trans->gw_html_req, err->msg);
1363 khtml_close(gw_trans->gw_html_req);
1366 static int
1367 gw_template(size_t key, void *arg)
1369 const struct got_error *error = NULL;
1370 struct gw_trans *gw_trans = arg;
1371 char *gw_got_link, *gw_site_link;
1372 char *site_owner_name, *site_owner_name_h;
1374 switch (key) {
1375 case (TEMPL_HEAD):
1376 khttp_puts(gw_trans->gw_req, head);
1377 break;
1378 case(TEMPL_HEADER):
1379 gw_got_link = gw_get_got_link(gw_trans);
1380 if (gw_got_link != NULL)
1381 khttp_puts(gw_trans->gw_req, gw_got_link);
1383 free(gw_got_link);
1384 break;
1385 case (TEMPL_SITEPATH):
1386 gw_site_link = gw_get_site_link(gw_trans);
1387 if (gw_site_link != NULL)
1388 khttp_puts(gw_trans->gw_req, gw_site_link);
1390 free(gw_site_link);
1391 break;
1392 case(TEMPL_TITLE):
1393 if (gw_trans->gw_conf->got_site_name != NULL)
1394 khtml_puts(gw_trans->gw_html_req,
1395 gw_trans->gw_conf->got_site_name);
1397 break;
1398 case (TEMPL_SEARCH):
1399 khttp_puts(gw_trans->gw_req, search);
1400 break;
1401 case(TEMPL_SITEOWNER):
1402 if (gw_trans->gw_conf->got_site_owner != NULL &&
1403 gw_trans->gw_conf->got_show_site_owner) {
1404 error = gw_html_escape(&site_owner_name,
1405 gw_trans->gw_conf->got_site_owner);
1406 if (error)
1407 return 0;
1408 if (asprintf(&site_owner_name_h, site_owner,
1409 site_owner_name) == -1) {
1410 free(site_owner_name);
1411 return 0;
1413 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1414 free(site_owner_name);
1415 free(site_owner_name_h);
1417 break;
1418 case(TEMPL_CONTENT):
1419 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1420 if (error)
1421 khttp_puts(gw_trans->gw_req, error->msg);
1422 break;
1423 default:
1424 return 0;
1426 return 1;
1429 static char *
1430 gw_gen_commit_header(char *str1, char *str2)
1432 char *return_html = NULL, *ref_str = NULL;
1434 if (strcmp(str2, "") != 0) {
1435 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1436 return_html = strdup("");
1437 return return_html;
1439 } else
1440 ref_str = strdup("");
1443 if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1444 return_html = strdup("");
1446 free(ref_str);
1447 return return_html;
1450 static char *
1451 gw_gen_diff_header(char *str1, char *str2)
1453 char *return_html = NULL;
1455 if (asprintf(&return_html, header_diff_html, str1, str2) == -1)
1456 return_html = strdup("");
1458 return return_html;
1461 static char *
1462 gw_gen_author_header(char *str)
1464 char *return_html = NULL;
1466 if (asprintf(&return_html, header_author_html, str) == -1)
1467 return_html = strdup("");
1469 return return_html;
1472 static char *
1473 gw_gen_committer_header(char *str)
1475 char *return_html = NULL;
1477 if (asprintf(&return_html, header_committer_html, str) == -1)
1478 return_html = strdup("");
1480 return return_html;
1483 static char *
1484 gw_gen_commit_msg_header(char *str)
1486 char *return_html = NULL;
1488 if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1489 return_html = strdup("");
1491 return return_html;
1494 static char *
1495 gw_gen_tree_header(char *str)
1497 char *return_html = NULL;
1499 if (asprintf(&return_html, header_tree_html, str) == -1)
1500 return_html = strdup("");
1502 return return_html;
1505 static const struct got_error *
1506 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
1507 char *dir)
1509 const struct got_error *error = NULL;
1510 FILE *f = NULL;
1511 char *d_file = NULL;
1512 unsigned int len;
1513 size_t n;
1515 *description = NULL;
1516 if (gw_trans->gw_conf->got_show_repo_description == 0)
1517 return gw_empty_string(description);
1519 if (asprintf(&d_file, "%s/description", dir) == -1)
1520 return got_error_from_errno("asprintf");
1522 f = fopen(d_file, "r");
1523 if (f == NULL) {
1524 if (errno == ENOENT || errno == EACCES)
1525 return gw_empty_string(description);
1526 error = got_error_from_errno2("fopen", d_file);
1527 goto done;
1530 if (fseek(f, 0, SEEK_END) == -1) {
1531 error = got_ferror(f, GOT_ERR_IO);
1532 goto done;
1534 len = ftell(f);
1535 if (len == -1) {
1536 error = got_ferror(f, GOT_ERR_IO);
1537 goto done;
1539 if (fseek(f, 0, SEEK_SET) == -1) {
1540 error = got_ferror(f, GOT_ERR_IO);
1541 goto done;
1543 *description = calloc(len + 1, sizeof(**description));
1544 if (*description == NULL) {
1545 error = got_error_from_errno("calloc");
1546 goto done;
1549 n = fread(*description, 1, len, f);
1550 if (n == 0 && ferror(f))
1551 error = got_ferror(f, GOT_ERR_IO);
1552 done:
1553 if (f != NULL && fclose(f) == -1 && error == NULL)
1554 error = got_error_from_errno("fclose");
1555 free(d_file);
1556 return error;
1559 static const struct got_error *
1560 gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1562 struct tm tm;
1563 time_t diff_time;
1564 char *years = "years ago", *months = "months ago";
1565 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1566 char *minutes = "minutes ago", *seconds = "seconds ago";
1567 char *now = "right now";
1568 char *s;
1569 char datebuf[29];
1571 *repo_age = NULL;
1573 switch (ref_tm) {
1574 case TM_DIFF:
1575 diff_time = time(NULL) - committer_time;
1576 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1577 if (asprintf(repo_age, "%lld %s",
1578 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1579 return got_error_from_errno("asprintf");
1580 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1581 if (asprintf(repo_age, "%lld %s",
1582 (diff_time / 60 / 60 / 24 / (365 / 12)),
1583 months) == -1)
1584 return got_error_from_errno("asprintf");
1585 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1586 if (asprintf(repo_age, "%lld %s",
1587 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1588 return got_error_from_errno("asprintf");
1589 } else if (diff_time > 60 * 60 * 24 * 2) {
1590 if (asprintf(repo_age, "%lld %s",
1591 (diff_time / 60 / 60 / 24), days) == -1)
1592 return got_error_from_errno("asprintf");
1593 } else if (diff_time > 60 * 60 * 2) {
1594 if (asprintf(repo_age, "%lld %s",
1595 (diff_time / 60 / 60), hours) == -1)
1596 return got_error_from_errno("asprintf");
1597 } else if (diff_time > 60 * 2) {
1598 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1599 minutes) == -1)
1600 return got_error_from_errno("asprintf");
1601 } else if (diff_time > 2) {
1602 if (asprintf(repo_age, "%lld %s", diff_time,
1603 seconds) == -1)
1604 return got_error_from_errno("asprintf");
1605 } else {
1606 if (asprintf(repo_age, "%s", now) == -1)
1607 return got_error_from_errno("asprintf");
1609 break;
1610 case TM_LONG:
1611 if (gmtime_r(&committer_time, &tm) == NULL)
1612 return got_error_from_errno("gmtime_r");
1614 s = asctime_r(&tm, datebuf);
1615 if (s == NULL)
1616 return got_error_from_errno("asctime_r");
1618 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1619 return got_error_from_errno("asprintf");
1620 break;
1622 return NULL;
1625 static const struct got_error *
1626 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
1627 char *repo_ref, int ref_tm)
1629 const struct got_error *error = NULL;
1630 struct got_object_id *id = NULL;
1631 struct got_repository *repo = NULL;
1632 struct got_commit_object *commit = NULL;
1633 struct got_reflist_head refs;
1634 struct got_reflist_entry *re;
1635 struct got_reference *head_ref;
1636 int is_head = 0;
1637 time_t committer_time = 0, cmp_time = 0;
1638 const char *refname;
1640 *repo_age = NULL;
1641 SIMPLEQ_INIT(&refs);
1643 if (repo_ref == NULL)
1644 return NULL;
1646 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1647 is_head = 1;
1649 if (gw_trans->gw_conf->got_show_repo_age == 0)
1650 return NULL;
1652 error = got_repo_open(&repo, dir, NULL);
1653 if (error)
1654 goto done;
1656 if (is_head)
1657 error = got_ref_list(&refs, repo, "refs/heads",
1658 got_ref_cmp_by_name, NULL);
1659 else
1660 error = got_ref_list(&refs, repo, repo_ref,
1661 got_ref_cmp_by_name, NULL);
1662 if (error)
1663 goto done;
1665 SIMPLEQ_FOREACH(re, &refs, entry) {
1666 if (is_head)
1667 refname = strdup(repo_ref);
1668 else
1669 refname = got_ref_get_name(re->ref);
1670 error = got_ref_open(&head_ref, repo, refname, 0);
1671 if (error)
1672 goto done;
1674 error = got_ref_resolve(&id, repo, head_ref);
1675 got_ref_close(head_ref);
1676 if (error)
1677 goto done;
1679 error = got_object_open_as_commit(&commit, repo, id);
1680 if (error)
1681 goto done;
1683 committer_time =
1684 got_object_commit_get_committer_time(commit);
1686 if (cmp_time < committer_time)
1687 cmp_time = committer_time;
1690 if (cmp_time != 0) {
1691 committer_time = cmp_time;
1692 error = gw_get_time_str(repo_age, committer_time, ref_tm);
1694 done:
1695 got_ref_list_free(&refs);
1696 free(id);
1697 return error;
1700 static const struct got_error *
1701 gw_get_diff(char **diff_html, struct gw_trans *gw_trans,
1702 struct gw_header *header)
1704 const struct got_error *error;
1705 FILE *f = NULL;
1706 struct got_object_id *id1 = NULL, *id2 = NULL;
1707 struct buf *diffbuf = NULL;
1708 char *label1 = NULL, *label2 = NULL, *line = NULL;
1709 char *diff_line_html = NULL;
1710 int obj_type;
1711 size_t newsize, linesize = 0;
1712 ssize_t linelen;
1714 f = got_opentemp();
1715 if (f == NULL)
1716 return NULL;
1718 error = buf_alloc(&diffbuf, 0);
1719 if (error)
1720 goto done;
1722 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1723 if (error)
1724 goto done;
1726 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1727 error = got_repo_match_object_id(&id1, &label1,
1728 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1729 if (error)
1730 goto done;
1733 error = got_repo_match_object_id(&id2, &label2,
1734 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1735 if (error)
1736 goto done;
1738 error = got_object_get_type(&obj_type, header->repo, id2);
1739 if (error)
1740 goto done;
1741 switch (obj_type) {
1742 case GOT_OBJ_TYPE_BLOB:
1743 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1744 header->repo, f);
1745 break;
1746 case GOT_OBJ_TYPE_TREE:
1747 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1748 header->repo, f);
1749 break;
1750 case GOT_OBJ_TYPE_COMMIT:
1751 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1752 header->repo, f);
1753 break;
1754 default:
1755 error = got_error(GOT_ERR_OBJ_TYPE);
1757 if (error)
1758 goto done;
1760 if (fseek(f, 0, SEEK_SET) == -1) {
1761 error = got_ferror(f, GOT_ERR_IO);
1762 goto done;
1765 while ((linelen = getline(&line, &linesize, f)) != -1) {
1766 char *escaped_line;
1767 error = gw_html_escape(&escaped_line, line);
1768 if (error)
1769 goto done;
1771 error = gw_colordiff_line(&diff_line_html, escaped_line);
1772 if (error)
1773 goto done;
1775 error = buf_puts(&newsize, diffbuf, diff_line_html);
1776 if (error)
1777 goto done;
1779 error = buf_puts(&newsize, diffbuf, div_end);
1780 if (error)
1781 goto done;
1783 if (linelen == -1 && ferror(f)) {
1784 error = got_error_from_errno("getline");
1785 goto done;
1788 if (buf_len(diffbuf) > 0) {
1789 error = buf_putc(diffbuf, '\0');
1790 if (error)
1791 goto done;
1792 *diff_html = strdup(buf_get(diffbuf));
1793 if (*diff_html == NULL)
1794 error = got_error_from_errno("strdup");
1796 done:
1797 if (f && fclose(f) == -1 && error == NULL)
1798 error = got_error_from_errno("fclose");
1799 free(diff_line_html);
1800 free(line);
1801 free(diffbuf);
1802 free(label1);
1803 free(label2);
1804 free(id1);
1805 free(id2);
1807 return error;
1810 static const struct got_error *
1811 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
1813 const struct got_error *error = NULL;
1814 struct got_repository *repo;
1815 const char *gitconfig_owner;
1817 *owner = NULL;
1819 if (gw_trans->gw_conf->got_show_repo_owner == 0)
1820 return NULL;
1822 error = got_repo_open(&repo, dir, NULL);
1823 if (error)
1824 return error;
1825 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
1826 if (gitconfig_owner) {
1827 *owner = strdup(gitconfig_owner);
1828 if (*owner == NULL)
1829 error = got_error_from_errno("strdup");
1831 got_repo_close(repo);
1832 return error;
1835 static const struct got_error *
1836 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
1838 const struct got_error *error = NULL;
1839 FILE *f;
1840 char *d_file = NULL;
1841 unsigned int len;
1842 size_t n;
1844 *url = NULL;
1846 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
1847 return got_error_from_errno("asprintf");
1849 f = fopen(d_file, "r");
1850 if (f == NULL) {
1851 if (errno != ENOENT && errno != EACCES)
1852 error = got_error_from_errno2("fopen", d_file);
1853 goto done;
1856 if (fseek(f, 0, SEEK_END) == -1) {
1857 error = got_ferror(f, GOT_ERR_IO);
1858 goto done;
1860 len = ftell(f);
1861 if (len == -1) {
1862 error = got_ferror(f, GOT_ERR_IO);
1863 goto done;
1865 if (fseek(f, 0, SEEK_SET) == -1) {
1866 error = got_ferror(f, GOT_ERR_IO);
1867 goto done;
1870 *url = calloc(len + 1, sizeof(**url));
1871 if (*url == NULL) {
1872 error = got_error_from_errno("calloc");
1873 goto done;
1876 n = fread(*url, 1, len, f);
1877 if (n == 0 && ferror(f))
1878 error = got_ferror(f, GOT_ERR_IO);
1879 done:
1880 if (f && fclose(f) == -1 && error == NULL)
1881 error = got_error_from_errno("fclose");
1882 free(d_file);
1883 return NULL;
1886 static const struct got_error *
1887 gw_get_repo_tags(char **tag_html, struct gw_trans *gw_trans,
1888 struct gw_header *header, int limit, int tag_type)
1890 const struct got_error *error = NULL;
1891 struct got_repository *repo = NULL;
1892 struct got_reflist_head refs;
1893 struct got_reflist_entry *re;
1894 char *tag_row = NULL, *tags_navs_disp = NULL;
1895 char *age = NULL, *age_html = NULL, *newline;
1896 char *escaped_tagger = NULL, *escaped_tag_commit = NULL;
1897 char *id_str = NULL, *refstr = NULL;
1898 char *tag_commit0 = NULL;
1899 struct buf *diffbuf = NULL;
1900 size_t newsize;
1901 struct got_tag_object *tag = NULL;
1903 *tag_html = NULL;
1905 SIMPLEQ_INIT(&refs);
1907 error = buf_alloc(&diffbuf, 0);
1908 if (error)
1909 return NULL;
1911 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1912 if (error)
1913 goto done;
1915 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1916 if (error)
1917 goto done;
1919 SIMPLEQ_FOREACH(re, &refs, entry) {
1920 const char *refname;
1921 const char *tagger;
1922 const char *tag_commit;
1923 time_t tagger_time;
1924 struct got_object_id *id;
1926 refname = got_ref_get_name(re->ref);
1927 if (strncmp(refname, "refs/tags/", 10) != 0)
1928 continue;
1929 refname += 10;
1930 refstr = got_ref_to_str(re->ref);
1931 if (refstr == NULL) {
1932 error = got_error_from_errno("got_ref_to_str");
1933 goto done;
1936 error = got_ref_resolve(&id, repo, re->ref);
1937 if (error)
1938 goto done;
1939 error = got_object_open_as_tag(&tag, repo, id);
1940 free(id);
1941 if (error)
1942 goto done;
1944 tagger = got_object_tag_get_tagger(tag);
1945 tagger_time = got_object_tag_get_tagger_time(tag);
1947 error = got_object_id_str(&id_str,
1948 got_object_tag_get_object_id(tag));
1949 if (error)
1950 goto done;
1952 tag_commit0 = strdup(got_object_tag_get_message(tag));
1953 if (tag_commit0 == NULL) {
1954 error = got_error_from_errno("strdup");
1955 goto done;
1958 tag_commit = tag_commit0;
1959 while (*tag_commit == '\n')
1960 tag_commit++;
1962 switch (tag_type) {
1963 case TAGBRIEF:
1964 newline = strchr(tag_commit, '\n');
1965 if (newline)
1966 *newline = '\0';
1968 error = gw_get_time_str(&age, tagger_time, TM_DIFF);
1969 if (error)
1970 goto done;
1972 if (asprintf(&tags_navs_disp, tags_navs,
1973 gw_trans->repo_name, id_str, gw_trans->repo_name,
1974 id_str, gw_trans->repo_name, id_str,
1975 gw_trans->repo_name, id_str) == -1) {
1976 error = got_error_from_errno("asprintf");
1977 goto done;
1980 if (asprintf(&tag_row, tags_row, age ? age : "",
1981 refname, tag_commit, tags_navs_disp) == -1) {
1982 error = got_error_from_errno("asprintf");
1983 goto done;
1986 free(tags_navs_disp);
1987 break;
1988 case TAGFULL:
1989 error = gw_get_time_str(&age, tagger_time, TM_LONG);
1990 if (error)
1991 goto done;
1992 error = gw_html_escape(&escaped_tagger, tagger);
1993 if (error)
1994 goto done;
1995 error = gw_html_escape(&escaped_tag_commit, tag_commit);
1996 if (error)
1997 goto done;
1998 if (asprintf(&tag_row, tag_info, age ? age : "",
1999 escaped_tagger, escaped_tag_commit) == -1) {
2000 error = got_error_from_errno("asprintf");
2001 goto done;
2003 break;
2004 default:
2005 break;
2008 error = buf_puts(&newsize, diffbuf, tag_row);
2009 if (error)
2010 goto done;
2012 if (limit && --limit == 0)
2013 break;
2015 got_object_tag_close(tag);
2016 tag = NULL;
2017 free(id_str);
2018 id_str = NULL;
2019 free(refstr);
2020 refstr = NULL;
2021 free(age);
2022 age = NULL;
2023 free(age_html);
2024 age_html = NULL;
2025 free(escaped_tagger);
2026 escaped_tagger = NULL;
2027 free(escaped_tag_commit);
2028 escaped_tag_commit = NULL;
2029 free(tag_commit0);
2030 tag_commit0 = NULL;
2031 free(tag_row);
2032 tag_row = NULL;
2035 if (buf_len(diffbuf) > 0) {
2036 error = buf_putc(diffbuf, '\0');
2037 *tag_html = strdup(buf_get(diffbuf));
2038 if (*tag_html == NULL)
2039 error = got_error_from_errno("strdup");
2041 done:
2042 if (tag)
2043 got_object_tag_close(tag);
2044 free(id_str);
2045 free(refstr);
2046 free(age);
2047 free(age_html);
2048 free(escaped_tagger);
2049 free(escaped_tag_commit);
2050 free(tag_commit0);
2051 free(tag_row);
2052 buf_free(diffbuf);
2053 got_ref_list_free(&refs);
2054 if (repo)
2055 got_repo_close(repo);
2056 return error;
2059 static void
2060 gw_free_headers(struct gw_header *header)
2062 free(header->id);
2063 free(header->path);
2064 if (header->commit != NULL)
2065 got_object_commit_close(header->commit);
2066 if (header->repo)
2067 got_repo_close(header->repo);
2068 free(header->refs_str);
2069 free(header->commit_id);
2070 free(header->parent_id);
2071 free(header->tree_id);
2072 free(header->author);
2073 free(header->committer);
2074 free(header->commit_msg);
2077 static struct gw_header *
2078 gw_init_header()
2080 struct gw_header *header;
2082 header = malloc(sizeof(*header));
2083 if (header == NULL)
2084 return NULL;
2086 header->repo = NULL;
2087 header->commit = NULL;
2088 header->id = NULL;
2089 header->path = NULL;
2090 SIMPLEQ_INIT(&header->refs);
2092 return header;
2095 static const struct got_error *
2096 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2097 int limit)
2099 const struct got_error *error = NULL;
2100 struct got_commit_graph *graph = NULL;
2102 error = got_commit_graph_open(&graph, header->path, 0);
2103 if (error)
2104 goto done;
2106 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2107 NULL, NULL);
2108 if (error)
2109 goto done;
2111 for (;;) {
2112 error = got_commit_graph_iter_next(&header->id, graph,
2113 header->repo, NULL, NULL);
2114 if (error) {
2115 if (error->code == GOT_ERR_ITER_COMPLETED)
2116 error = NULL;
2117 goto done;
2119 if (header->id == NULL)
2120 goto done;
2122 error = got_object_open_as_commit(&header->commit, header->repo,
2123 header->id);
2124 if (error)
2125 goto done;
2127 error = gw_get_commit(gw_trans, header);
2128 if (limit > 1) {
2129 struct gw_header *n_header = NULL;
2130 if ((n_header = gw_init_header()) == NULL) {
2131 error = got_error_from_errno("malloc");
2132 goto done;
2135 n_header->refs_str = strdup(header->refs_str);
2136 n_header->commit_id = strdup(header->commit_id);
2137 n_header->parent_id = strdup(header->parent_id);
2138 n_header->tree_id = strdup(header->tree_id);
2139 n_header->author = strdup(header->author);
2140 n_header->committer = strdup(header->committer);
2141 n_header->commit_msg = strdup(header->commit_msg);
2142 n_header->committer_time = header->committer_time;
2143 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2144 entry);
2146 if (error || (limit && --limit == 0))
2147 break;
2149 done:
2150 if (graph)
2151 got_commit_graph_close(graph);
2152 return error;
2155 static const struct got_error *
2156 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2158 const struct got_error *error = NULL;
2159 struct got_reflist_entry *re;
2160 struct got_object_id *id2 = NULL;
2161 struct got_object_qid *parent_id;
2162 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2164 /*print commit*/
2165 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2166 char *s;
2167 const char *name;
2168 struct got_tag_object *tag = NULL;
2169 int cmp;
2171 name = got_ref_get_name(re->ref);
2172 if (strcmp(name, GOT_REF_HEAD) == 0)
2173 continue;
2174 if (strncmp(name, "refs/", 5) == 0)
2175 name += 5;
2176 if (strncmp(name, "got/", 4) == 0)
2177 continue;
2178 if (strncmp(name, "heads/", 6) == 0)
2179 name += 6;
2180 if (strncmp(name, "remotes/", 8) == 0)
2181 name += 8;
2182 if (strncmp(name, "tags/", 5) == 0) {
2183 error = got_object_open_as_tag(&tag, header->repo,
2184 re->id);
2185 if (error) {
2186 if (error->code != GOT_ERR_OBJ_TYPE)
2187 continue;
2189 * Ref points at something other
2190 * than a tag.
2192 error = NULL;
2193 tag = NULL;
2196 cmp = got_object_id_cmp(tag ?
2197 got_object_tag_get_object_id(tag) : re->id, header->id);
2198 if (tag)
2199 got_object_tag_close(tag);
2200 if (cmp != 0)
2201 continue;
2202 s = refs_str;
2203 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2204 s ? ", " : "", name) == -1) {
2205 error = got_error_from_errno("asprintf");
2206 free(s);
2207 return error;
2209 header->refs_str = strdup(refs_str);
2210 free(s);
2213 if (refs_str == NULL)
2214 header->refs_str = strdup("");
2215 free(refs_str);
2217 error = got_object_id_str(&header->commit_id, header->id);
2218 if (error)
2219 return error;
2221 error = got_object_id_str(&header->tree_id,
2222 got_object_commit_get_tree_id(header->commit));
2223 if (error)
2224 return error;
2226 if (gw_trans->action == GW_DIFF) {
2227 parent_id = SIMPLEQ_FIRST(
2228 got_object_commit_get_parent_ids(header->commit));
2229 if (parent_id != NULL) {
2230 id2 = got_object_id_dup(parent_id->id);
2231 free (parent_id);
2232 error = got_object_id_str(&header->parent_id, id2);
2233 if (error)
2234 return error;
2235 free(id2);
2236 } else
2237 header->parent_id = strdup("/dev/null");
2238 } else
2239 header->parent_id = strdup("");
2241 header->committer_time =
2242 got_object_commit_get_committer_time(header->commit);
2244 error = gw_html_escape(&header->author,
2245 got_object_commit_get_author(header->commit));
2246 if (error)
2247 return error;
2248 error = gw_html_escape(&header->committer,
2249 got_object_commit_get_committer(header->commit));
2250 if (error)
2251 return error;
2253 /* XXX Doesn't the log message require escaping? */
2254 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2255 if (error)
2256 return error;
2258 commit_msg = commit_msg0;
2259 while (*commit_msg == '\n')
2260 commit_msg++;
2262 header->commit_msg = strdup(commit_msg);
2263 free(commit_msg0);
2264 return error;
2267 static const struct got_error *
2268 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2270 const struct got_error *error = NULL;
2271 char *in_repo_path = NULL;
2273 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2274 if (error)
2275 return error;
2277 if (gw_trans->commit == NULL) {
2278 struct got_reference *head_ref;
2279 error = got_ref_open(&head_ref, header->repo,
2280 gw_trans->headref, 0);
2281 if (error)
2282 return error;
2284 error = got_ref_resolve(&header->id, header->repo, head_ref);
2285 got_ref_close(head_ref);
2286 if (error)
2287 return error;
2289 error = got_object_open_as_commit(&header->commit,
2290 header->repo, header->id);
2291 } else {
2292 struct got_reference *ref;
2293 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2294 if (error == NULL) {
2295 int obj_type;
2296 error = got_ref_resolve(&header->id, header->repo, ref);
2297 got_ref_close(ref);
2298 if (error)
2299 return error;
2300 error = got_object_get_type(&obj_type, header->repo,
2301 header->id);
2302 if (error)
2303 return error;
2304 if (obj_type == GOT_OBJ_TYPE_TAG) {
2305 struct got_tag_object *tag;
2306 error = got_object_open_as_tag(&tag,
2307 header->repo, header->id);
2308 if (error)
2309 return error;
2310 if (got_object_tag_get_object_type(tag) !=
2311 GOT_OBJ_TYPE_COMMIT) {
2312 got_object_tag_close(tag);
2313 error = got_error(GOT_ERR_OBJ_TYPE);
2314 return error;
2316 free(header->id);
2317 header->id = got_object_id_dup(
2318 got_object_tag_get_object_id(tag));
2319 if (header->id == NULL)
2320 error = got_error_from_errno(
2321 "got_object_id_dup");
2322 got_object_tag_close(tag);
2323 if (error)
2324 return error;
2325 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2326 error = got_error(GOT_ERR_OBJ_TYPE);
2327 return error;
2329 error = got_object_open_as_commit(&header->commit,
2330 header->repo, header->id);
2331 if (error)
2332 return error;
2334 if (header->commit == NULL) {
2335 error = got_repo_match_object_id_prefix(&header->id,
2336 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2337 header->repo);
2338 if (error)
2339 return error;
2341 error = got_repo_match_object_id_prefix(&header->id,
2342 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2343 header->repo);
2346 error = got_repo_map_path(&in_repo_path, header->repo,
2347 gw_trans->repo_path, 1);
2348 if (error)
2349 return error;
2351 if (in_repo_path) {
2352 header->path = strdup(in_repo_path);
2354 free(in_repo_path);
2356 error = got_ref_list(&header->refs, header->repo, NULL,
2357 got_ref_cmp_by_name, NULL);
2358 if (error)
2359 return error;
2361 error = gw_get_commits(gw_trans, header, limit);
2362 return error;
2365 struct blame_line {
2366 int annotated;
2367 char *id_str;
2368 char *committer;
2369 char datebuf[11]; /* YYYY-MM-DD + NUL */
2372 struct gw_blame_cb_args {
2373 struct blame_line *lines;
2374 int nlines;
2375 int nlines_prec;
2376 int lineno_cur;
2377 off_t *line_offsets;
2378 FILE *f;
2379 struct got_repository *repo;
2380 struct gw_trans *gw_trans;
2381 struct buf *blamebuf;
2384 static const struct got_error *
2385 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2387 const struct got_error *err = NULL;
2388 struct gw_blame_cb_args *a = arg;
2389 struct blame_line *bline;
2390 char *line = NULL;
2391 size_t linesize = 0, newsize;
2392 struct got_commit_object *commit = NULL;
2393 off_t offset;
2394 struct tm tm;
2395 time_t committer_time;
2397 if (nlines != a->nlines ||
2398 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2399 return got_error(GOT_ERR_RANGE);
2401 if (lineno == -1)
2402 return NULL; /* no change in this commit */
2404 /* Annotate this line. */
2405 bline = &a->lines[lineno - 1];
2406 if (bline->annotated)
2407 return NULL;
2408 err = got_object_id_str(&bline->id_str, id);
2409 if (err)
2410 return err;
2412 err = got_object_open_as_commit(&commit, a->repo, id);
2413 if (err)
2414 goto done;
2416 bline->committer = strdup(got_object_commit_get_committer(commit));
2417 if (bline->committer == NULL) {
2418 err = got_error_from_errno("strdup");
2419 goto done;
2422 committer_time = got_object_commit_get_committer_time(commit);
2423 if (localtime_r(&committer_time, &tm) == NULL)
2424 return got_error_from_errno("localtime_r");
2425 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2426 &tm) >= sizeof(bline->datebuf)) {
2427 err = got_error(GOT_ERR_NO_SPACE);
2428 goto done;
2430 bline->annotated = 1;
2432 /* Print lines annotated so far. */
2433 bline = &a->lines[a->lineno_cur - 1];
2434 if (!bline->annotated)
2435 goto done;
2437 offset = a->line_offsets[a->lineno_cur - 1];
2438 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2439 err = got_error_from_errno("fseeko");
2440 goto done;
2443 while (bline->annotated) {
2444 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2445 *line_escape = NULL;
2446 size_t len;
2448 if (getline(&line, &linesize, a->f) == -1) {
2449 if (ferror(a->f))
2450 err = got_error_from_errno("getline");
2451 break;
2454 committer = bline->committer;
2455 smallerthan = strchr(committer, '<');
2456 if (smallerthan && smallerthan[1] != '\0')
2457 committer = smallerthan + 1;
2458 at = strchr(committer, '@');
2459 if (at)
2460 *at = '\0';
2461 len = strlen(committer);
2462 if (len >= 9)
2463 committer[8] = '\0';
2465 nl = strchr(line, '\n');
2466 if (nl)
2467 *nl = '\0';
2469 err = gw_html_escape(&line_escape, line);
2470 if (err)
2471 goto err;
2473 if (a->gw_trans->repo_folder == NULL)
2474 a->gw_trans->repo_folder = strdup("");
2475 if (a->gw_trans->repo_folder == NULL)
2476 goto err;
2477 asprintf(&blame_row, blame_line, a->nlines_prec, a->lineno_cur,
2478 a->gw_trans->repo_name, bline->id_str,
2479 a->gw_trans->repo_file, a->gw_trans->repo_folder,
2480 bline->id_str, bline->datebuf, committer, line_escape);
2481 a->lineno_cur++;
2482 err = buf_puts(&newsize, a->blamebuf, blame_row);
2483 if (err)
2484 return err;
2486 bline = &a->lines[a->lineno_cur - 1];
2487 err:
2488 free(line_escape);
2489 free(blame_row);
2491 done:
2492 if (commit)
2493 got_object_commit_close(commit);
2494 free(line);
2495 return err;
2498 static const struct got_error *
2499 gw_get_file_blame_blob(char **blame_html, struct gw_trans *gw_trans)
2501 const struct got_error *error = NULL;
2502 struct got_repository *repo = NULL;
2503 struct got_object_id *obj_id = NULL;
2504 struct got_object_id *commit_id = NULL;
2505 struct got_blob_object *blob = NULL;
2506 char *path = NULL, *in_repo_path = NULL;
2507 struct gw_blame_cb_args bca;
2508 int i, obj_type;
2509 size_t filesize;
2511 *blame_html = NULL;
2513 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2514 if (error)
2515 return error;
2517 if (asprintf(&path, "%s%s%s",
2518 gw_trans->repo_folder ? gw_trans->repo_folder : "",
2519 gw_trans->repo_folder ? "/" : "",
2520 gw_trans->repo_file) == -1) {
2521 error = got_error_from_errno("asprintf");
2522 goto done;
2525 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2526 if (error)
2527 goto done;
2529 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2530 GOT_OBJ_TYPE_COMMIT, 1, repo);
2531 if (error)
2532 goto done;
2534 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2535 if (error)
2536 goto done;
2538 if (obj_id == NULL) {
2539 error = got_error(GOT_ERR_NO_OBJ);
2540 goto done;
2543 error = got_object_get_type(&obj_type, repo, obj_id);
2544 if (error)
2545 goto done;
2547 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2548 error = got_error(GOT_ERR_OBJ_TYPE);
2549 goto done;
2552 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2553 if (error)
2554 goto done;
2556 error = buf_alloc(&bca.blamebuf, 0);
2557 if (error)
2558 goto done;
2560 bca.f = got_opentemp();
2561 if (bca.f == NULL) {
2562 error = got_error_from_errno("got_opentemp");
2563 goto done;
2565 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2566 &bca.line_offsets, bca.f, blob);
2567 if (error || bca.nlines == 0)
2568 goto done;
2570 /* Don't include \n at EOF in the blame line count. */
2571 if (bca.line_offsets[bca.nlines - 1] == filesize)
2572 bca.nlines--;
2574 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2575 if (bca.lines == NULL) {
2576 error = got_error_from_errno("calloc");
2577 goto done;
2579 bca.lineno_cur = 1;
2580 bca.nlines_prec = 0;
2581 i = bca.nlines;
2582 while (i > 0) {
2583 i /= 10;
2584 bca.nlines_prec++;
2586 bca.repo = repo;
2587 bca.gw_trans = gw_trans;
2589 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2590 NULL, NULL);
2591 if (error)
2592 goto done;
2593 if (buf_len(bca.blamebuf) > 0) {
2594 error = buf_putc(bca.blamebuf, '\0');
2595 if (error)
2596 goto done;
2597 *blame_html = strdup(buf_get(bca.blamebuf));
2598 if (*blame_html == NULL) {
2599 error = got_error_from_errno("strdup");
2600 goto done;
2603 done:
2604 free(bca.line_offsets);
2605 free(bca.blamebuf);
2606 free(in_repo_path);
2607 free(commit_id);
2608 free(obj_id);
2609 free(path);
2611 for (i = 0; i < bca.nlines; i++) {
2612 struct blame_line *bline = &bca.lines[i];
2613 free(bline->id_str);
2614 free(bline->committer);
2616 free(bca.lines);
2617 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2618 error = got_error_from_errno("fclose");
2619 if (blob)
2620 got_object_blob_close(blob);
2621 if (repo)
2622 got_repo_close(repo);
2623 return error;
2626 static const struct got_error *
2627 gw_get_file_read_blob(char **blobstr, size_t *filesize, struct gw_trans *gw_trans)
2629 const struct got_error *error = NULL;
2630 struct got_repository *repo = NULL;
2631 struct got_object_id *obj_id = NULL;
2632 struct got_object_id *commit_id = NULL;
2633 struct got_blob_object *blob = NULL;
2634 char *path = NULL, *in_repo_path = NULL;
2635 int obj_type;
2636 size_t n;
2637 FILE *f = NULL;
2639 *blobstr = NULL;
2640 *filesize = 0;
2642 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2643 if (error)
2644 return error;
2646 if (asprintf(&path, "%s%s%s",
2647 gw_trans->repo_folder ? gw_trans->repo_folder : "",
2648 gw_trans->repo_folder ? "/" : "",
2649 gw_trans->repo_file) == -1) {
2650 error = got_error_from_errno("asprintf");
2651 goto done;
2654 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2655 if (error)
2656 goto done;
2658 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2659 GOT_OBJ_TYPE_COMMIT, 1, repo);
2660 if (error)
2661 goto done;
2663 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2664 if (error)
2665 goto done;
2667 if (obj_id == NULL) {
2668 error = got_error(GOT_ERR_NO_OBJ);
2669 goto done;
2672 error = got_object_get_type(&obj_type, repo, obj_id);
2673 if (error)
2674 goto done;
2676 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2677 error = got_error(GOT_ERR_OBJ_TYPE);
2678 goto done;
2681 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2682 if (error)
2683 goto done;
2685 f = got_opentemp();
2686 if (f == NULL) {
2687 error = got_error_from_errno("got_opentemp");
2688 goto done;
2690 error = got_object_blob_dump_to_file(filesize, NULL, NULL, f, blob);
2691 if (error)
2692 goto done;
2694 /* XXX This will fail on large files... */
2695 *blobstr = calloc(*filesize + 1, sizeof(**blobstr));
2696 if (*blobstr == NULL) {
2697 error = got_error_from_errno("calloc");
2698 goto done;
2701 n = fread(*blobstr, 1, *filesize, f);
2702 if (n == 0) {
2703 if (ferror(f))
2704 error = got_ferror(f, GOT_ERR_IO);
2705 goto done;
2707 done:
2708 free(in_repo_path);
2709 free(commit_id);
2710 free(obj_id);
2711 free(path);
2712 if (blob)
2713 got_object_blob_close(blob);
2714 if (repo)
2715 got_repo_close(repo);
2716 if (f != NULL && fclose(f) == -1 && error == NULL)
2717 error = got_error_from_errno("fclose");
2718 if (error) {
2719 free(*blobstr);
2720 *blobstr = NULL;
2721 *filesize = 0;
2723 return error;
2726 static const struct got_error *
2727 gw_get_repo_tree(char **tree_html, struct gw_trans *gw_trans)
2729 const struct got_error *error = NULL;
2730 struct got_repository *repo = NULL;
2731 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2732 struct got_tree_object *tree = NULL;
2733 struct buf *diffbuf = NULL;
2734 size_t newsize;
2735 char *path = NULL, *in_repo_path = NULL, *tree_row = NULL;
2736 char *id_str = NULL;
2737 char *build_folder = NULL;
2738 char *url_html = NULL;
2739 const char *class = NULL;
2740 int nentries, i, class_flip = 0;
2742 *tree_html = NULL;
2744 error = buf_alloc(&diffbuf, 0);
2745 if (error)
2746 return error;
2748 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2749 if (error)
2750 goto done;
2752 if (gw_trans->repo_folder != NULL)
2753 path = strdup(gw_trans->repo_folder);
2754 else {
2755 error = got_repo_map_path(&in_repo_path, repo,
2756 gw_trans->repo_path, 1);
2757 if (error)
2758 goto done;
2759 free(path);
2760 path = in_repo_path;
2763 if (gw_trans->commit == NULL) {
2764 struct got_reference *head_ref;
2765 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2766 if (error)
2767 goto done;
2768 error = got_ref_resolve(&commit_id, repo, head_ref);
2769 if (error)
2770 goto done;
2771 got_ref_close(head_ref);
2773 } else {
2774 error = got_repo_match_object_id(&commit_id, NULL,
2775 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2776 if (error)
2777 goto done;
2780 error = got_object_id_str(&gw_trans->commit, commit_id);
2781 if (error)
2782 goto done;
2784 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2785 if (error)
2786 goto done;
2788 error = got_object_open_as_tree(&tree, repo, tree_id);
2789 if (error)
2790 goto done;
2792 nentries = got_object_tree_get_nentries(tree);
2793 for (i = 0; i < nentries; i++) {
2794 struct got_tree_entry *te;
2795 const char *modestr = "";
2796 mode_t mode;
2798 te = got_object_tree_get_entry(tree, i);
2800 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2801 if (error)
2802 goto done;
2804 mode = got_tree_entry_get_mode(te);
2805 if (got_object_tree_entry_is_submodule(te))
2806 modestr = "$";
2807 else if (S_ISLNK(mode))
2808 modestr = "@";
2809 else if (S_ISDIR(mode))
2810 modestr = "/";
2811 else if (mode & S_IXUSR)
2812 modestr = "*";
2814 if (class_flip == 0) {
2815 class = "back_lightgray";
2816 class_flip = 1;
2817 } else {
2818 class = "back_white";
2819 class_flip = 0;
2822 if (S_ISDIR(mode)) {
2823 if (asprintf(&build_folder, "%s%s%s",
2824 gw_trans->repo_folder ? "/" : "",
2825 gw_trans->repo_folder ? gw_trans->repo_folder : "",
2826 got_tree_entry_get_name(te)) == -1) {
2827 error = got_error_from_errno(
2828 "asprintf");
2829 goto done;
2832 if (asprintf(&url_html, folder_html,
2833 gw_trans->repo_name, gw_trans->action_name,
2834 gw_trans->commit, build_folder,
2835 got_tree_entry_get_name(te), modestr) == -1) {
2836 error = got_error_from_errno("asprintf");
2837 goto done;
2839 if (asprintf(&tree_row, tree_line, class, url_html,
2840 class) == -1) {
2841 error = got_error_from_errno("asprintf");
2842 goto done;
2844 } else {
2845 if (asprintf(&url_html, file_html, gw_trans->repo_name,
2846 "blob", gw_trans->commit,
2847 got_tree_entry_get_name(te),
2848 gw_trans->repo_folder ? gw_trans->repo_folder : "",
2849 got_tree_entry_get_name(te), modestr) == -1) {
2850 error = got_error_from_errno("asprintf");
2851 goto done;
2854 if (asprintf(&tree_row, tree_line_with_navs, class,
2855 url_html, class, gw_trans->repo_name, "blob",
2856 gw_trans->commit, got_tree_entry_get_name(te),
2857 gw_trans->repo_folder ? gw_trans->repo_folder : "",
2858 "blob", gw_trans->repo_name,
2859 "blame", gw_trans->commit,
2860 got_tree_entry_get_name(te),
2861 gw_trans->repo_folder ? gw_trans->repo_folder : "",
2862 "blame") == -1) {
2863 error = got_error_from_errno("asprintf");
2864 goto done;
2868 error = buf_puts(&newsize, diffbuf, tree_row);
2869 if (error)
2870 goto done;
2872 free(id_str);
2873 id_str = NULL;
2874 free(url_html);
2875 url_html = NULL;
2876 free(tree_row);
2877 tree_row = NULL;
2878 free(build_folder);
2879 build_folder = NULL;
2882 if (buf_len(diffbuf) > 0) {
2883 error = buf_putc(diffbuf, '\0');
2884 if (error)
2885 goto done;
2886 *tree_html = strdup(buf_get(diffbuf));
2887 if (*tree_html == NULL) {
2888 error = got_error_from_errno("strdup");
2889 goto done;
2892 done:
2893 if (tree)
2894 got_object_tree_close(tree);
2895 if (repo)
2896 got_repo_close(repo);
2898 free(id_str);
2899 free(url_html);
2900 free(tree_row);
2901 free(in_repo_path);
2902 free(tree_id);
2903 free(diffbuf);
2904 free(build_folder);
2905 return error;
2908 static char *
2909 gw_get_repo_heads(struct gw_trans *gw_trans)
2911 const struct got_error *error = NULL;
2912 struct got_repository *repo = NULL;
2913 struct got_reflist_head refs;
2914 struct got_reflist_entry *re;
2915 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2916 struct buf *diffbuf = NULL;
2917 size_t newsize;
2919 SIMPLEQ_INIT(&refs);
2921 error = buf_alloc(&diffbuf, 0);
2922 if (error)
2923 return NULL;
2925 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2926 if (error)
2927 goto done;
2929 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2930 NULL);
2931 if (error)
2932 goto done;
2934 SIMPLEQ_FOREACH(re, &refs, entry) {
2935 char *refname;
2937 refname = strdup(got_ref_get_name(re->ref));
2938 if (refname == NULL) {
2939 error = got_error_from_errno("got_ref_to_str");
2940 goto done;
2943 if (strncmp(refname, "refs/heads/", 11) != 0) {
2944 free(refname);
2945 continue;
2948 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
2949 refname, TM_DIFF);
2950 if (error)
2951 goto done;
2953 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2954 refname, gw_trans->repo_name, refname,
2955 gw_trans->repo_name, refname, gw_trans->repo_name,
2956 refname) == -1) {
2957 error = got_error_from_errno("asprintf");
2958 goto done;
2961 if (strncmp(refname, "refs/heads/", 11) == 0)
2962 refname += 11;
2964 if (asprintf(&head_row, heads_row, age, refname,
2965 head_navs_disp) == -1) {
2966 error = got_error_from_errno("asprintf");
2967 goto done;
2970 error = buf_puts(&newsize, diffbuf, head_row);
2972 free(head_navs_disp);
2973 free(head_row);
2976 if (buf_len(diffbuf) > 0) {
2977 error = buf_putc(diffbuf, '\0');
2978 heads = strdup(buf_get(diffbuf));
2980 done:
2981 buf_free(diffbuf);
2982 got_ref_list_free(&refs);
2983 if (repo)
2984 got_repo_close(repo);
2985 if (error)
2986 return NULL;
2987 else
2988 return heads;
2991 static char *
2992 gw_get_got_link(struct gw_trans *gw_trans)
2994 char *link;
2996 if (asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2997 gw_trans->gw_conf->got_logo) == -1)
2998 return NULL;
3000 return link;
3003 static char *
3004 gw_get_site_link(struct gw_trans *gw_trans)
3006 char *link = NULL, *repo = NULL, *action = NULL;
3008 if (gw_trans->repo_name != NULL &&
3009 asprintf(&repo, " / <a href='?path=%s&action=summary'>%s</a>",
3010 gw_trans->repo_name, gw_trans->repo_name) == -1)
3011 return NULL;
3013 if (gw_trans->action_name != NULL &&
3014 asprintf(&action, " / %s", gw_trans->action_name) == -1) {
3015 free(repo);
3016 return NULL;
3019 if (asprintf(&link, site_link, GOTWEB,
3020 gw_trans->gw_conf->got_site_link,
3021 repo ? repo : "", action ? action : "") == -1) {
3022 free(repo);
3023 free(action);
3024 return NULL;
3027 free(repo);
3028 free(action);
3029 return link;
3032 static const struct got_error *
3033 gw_colordiff_line(char **colorized_line, char *buf)
3035 const struct got_error *error = NULL;
3036 char *div_diff_line_div = NULL, *color = NULL;
3037 struct buf *diffbuf = NULL;
3038 size_t newsize;
3040 *colorized_line = NULL;
3042 error = buf_alloc(&diffbuf, 0);
3043 if (error)
3044 return error;
3046 if (strncmp(buf, "-", 1) == 0)
3047 color = "diff_minus";
3048 else if (strncmp(buf, "+", 1) == 0)
3049 color = "diff_plus";
3050 else if (strncmp(buf, "@@", 2) == 0)
3051 color = "diff_chunk_header";
3052 else if (strncmp(buf, "@@", 2) == 0)
3053 color = "diff_chunk_header";
3054 else if (strncmp(buf, "commit +", 8) == 0)
3055 color = "diff_meta";
3056 else if (strncmp(buf, "commit -", 8) == 0)
3057 color = "diff_meta";
3058 else if (strncmp(buf, "blob +", 6) == 0)
3059 color = "diff_meta";
3060 else if (strncmp(buf, "blob -", 6) == 0)
3061 color = "diff_meta";
3062 else if (strncmp(buf, "file +", 6) == 0)
3063 color = "diff_meta";
3064 else if (strncmp(buf, "file -", 6) == 0)
3065 color = "diff_meta";
3066 else if (strncmp(buf, "from:", 5) == 0)
3067 color = "diff_author";
3068 else if (strncmp(buf, "via:", 4) == 0)
3069 color = "diff_author";
3070 else if (strncmp(buf, "date:", 5) == 0)
3071 color = "diff_date";
3073 if (asprintf(&div_diff_line_div, div_diff_line, color ? color : "")
3074 == -1) {
3075 error = got_error_from_errno("asprintf");
3076 goto done;
3079 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
3080 if (error)
3081 goto done;
3083 error = buf_puts(&newsize, diffbuf, buf);
3084 if (error)
3085 goto done;
3087 if (buf_len(diffbuf) > 0) {
3088 error = buf_putc(diffbuf, '\0');
3089 *colorized_line = strdup(buf_get(diffbuf));
3090 if (*colorized_line == NULL)
3091 error = got_error_from_errno("strdup");
3093 done:
3094 free(diffbuf);
3095 free(div_diff_line_div);
3096 return error;
3100 * XXX This function should not exist.
3101 * We should let khtml_puts(3) handle HTML escaping.
3103 static const struct got_error *
3104 gw_html_escape(char **escaped_html, const char *orig_html)
3106 const struct got_error *error = NULL;
3107 struct escape_pair {
3108 char c;
3109 const char *s;
3110 } esc[] = {
3111 { '>', "&gt;" },
3112 { '<', "&lt;" },
3113 { '&', "&amp;" },
3114 { '"', "&quot;" },
3115 { '\'', "&apos;" },
3116 { '\n', "<br />" },
3118 size_t orig_len, len;
3119 int i, j, x;
3121 orig_len = strlen(orig_html);
3122 len = orig_len;
3123 for (i = 0; i < orig_len; i++) {
3124 for (j = 0; j < nitems(esc); j++) {
3125 if (orig_html[i] != esc[j].c)
3126 continue;
3127 len += strlen(esc[j].s) - 1 /* escaped char */;
3131 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
3132 if (*escaped_html == NULL)
3133 return got_error_from_errno("calloc");
3135 x = 0;
3136 for (i = 0; i < orig_len; i++) {
3137 int escaped = 0;
3138 for (j = 0; j < nitems(esc); j++) {
3139 if (orig_html[i] != esc[j].c)
3140 continue;
3142 if (strlcat(*escaped_html, esc[j].s, len + 1)
3143 >= len + 1) {
3144 error = got_error(GOT_ERR_NO_SPACE);
3145 goto done;
3147 x += strlen(esc[j].s);
3148 escaped = 1;
3149 break;
3151 if (!escaped) {
3152 (*escaped_html)[x] = orig_html[i];
3153 x++;
3156 done:
3157 if (error) {
3158 free(*escaped_html);
3159 *escaped_html = NULL;
3160 } else {
3161 (*escaped_html)[x] = '\0';
3163 return error;
3166 int
3167 main(int argc, char *argv[])
3169 const struct got_error *error = NULL;
3170 struct gw_trans *gw_trans;
3171 struct gw_dir *dir = NULL, *tdir;
3172 const char *page = "index";
3173 int gw_malloc = 1;
3174 enum kcgi_err kerr;
3176 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3177 errx(1, "malloc");
3179 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3180 errx(1, "malloc");
3182 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3183 errx(1, "malloc");
3185 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3186 errx(1, "malloc");
3188 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3189 if (kerr != KCGI_OK) {
3190 error = gw_kcgi_error(kerr);
3191 goto done;
3194 if ((gw_trans->gw_conf =
3195 malloc(sizeof(struct gotweb_conf))) == NULL) {
3196 gw_malloc = 0;
3197 error = got_error_from_errno("malloc");
3198 goto done;
3201 TAILQ_INIT(&gw_trans->gw_dirs);
3202 TAILQ_INIT(&gw_trans->gw_headers);
3204 gw_trans->page = 0;
3205 gw_trans->repos_total = 0;
3206 gw_trans->repo_path = NULL;
3207 gw_trans->commit = NULL;
3208 gw_trans->headref = strdup(GOT_REF_HEAD);
3209 gw_trans->mime = KMIME_TEXT_HTML;
3210 gw_trans->gw_tmpl->key = gw_templs;
3211 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3212 gw_trans->gw_tmpl->arg = gw_trans;
3213 gw_trans->gw_tmpl->cb = gw_template;
3214 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3215 if (error)
3216 goto done;
3218 error = gw_parse_querystring(gw_trans);
3219 if (error)
3220 goto done;
3222 if (gw_trans->action == GW_BLOB)
3223 error = gw_blob(gw_trans);
3224 else
3225 error = gw_display_index(gw_trans);
3226 done:
3227 if (error) {
3228 gw_trans->mime = KMIME_TEXT_PLAIN;
3229 gw_trans->action = GW_ERR;
3230 gw_display_error(gw_trans, error);
3232 if (gw_malloc) {
3233 free(gw_trans->gw_conf->got_repos_path);
3234 free(gw_trans->gw_conf->got_site_name);
3235 free(gw_trans->gw_conf->got_site_owner);
3236 free(gw_trans->gw_conf->got_site_link);
3237 free(gw_trans->gw_conf->got_logo);
3238 free(gw_trans->gw_conf->got_logo_url);
3239 free(gw_trans->gw_conf);
3240 free(gw_trans->commit);
3241 free(gw_trans->repo_path);
3242 free(gw_trans->repo_name);
3243 free(gw_trans->repo_file);
3244 free(gw_trans->action_name);
3245 free(gw_trans->headref);
3247 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3248 free(dir->name);
3249 free(dir->description);
3250 free(dir->age);
3251 free(dir->url);
3252 free(dir->path);
3253 free(dir);
3258 khttp_free(gw_trans->gw_req);
3259 return 0;