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 char *gw_get_time_str(time_t, int);
166 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
167 char *, char *, int);
168 static char *gw_get_file_blame_blob(struct gw_trans *);
169 static char *gw_get_repo_tree(struct gw_trans *);
170 static char *gw_get_diff(struct gw_trans *,
171 struct gw_header *);
172 static char *gw_get_repo_tags(struct gw_trans *,
173 struct gw_header *, int, int);
174 static char *gw_get_repo_heads(struct gw_trans *);
175 static const struct got_error *gw_get_clone_url(char **, struct gw_trans *, char *);
176 static char *gw_get_got_link(struct gw_trans *);
177 static char *gw_get_site_link(struct gw_trans *);
178 static char *gw_html_escape(const char *);
179 static char *gw_colordiff_line(char *);
181 static char *gw_gen_commit_header(char *, char*);
182 static char *gw_gen_diff_header(char *, char*);
183 static char *gw_gen_author_header(char *);
184 static char *gw_gen_committer_header(char *);
185 static char *gw_gen_age_header(char *);
186 static char *gw_gen_commit_msg_header(char *);
187 static char *gw_gen_tree_header(char *);
189 static void gw_free_headers(struct gw_header *);
190 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
191 enum kmime);
192 static const struct got_error* gw_display_index(struct gw_trans *);
193 static void gw_display_error(struct gw_trans *,
194 const struct got_error *);
196 static int gw_template(size_t, void *);
198 static const struct got_error* gw_get_header(struct gw_trans *,
199 struct gw_header *, int);
200 static const struct got_error* gw_get_commits(struct gw_trans *,
201 struct gw_header *, int);
202 static const struct got_error* gw_get_commit(struct gw_trans *,
203 struct gw_header *);
204 static const struct got_error* gw_apply_unveil(const char *, const char *);
205 static const struct got_error* gw_blame_cb(void *, int, int,
206 struct got_object_id *);
207 static const struct got_error* gw_load_got_paths(struct gw_trans *);
208 static const struct got_error* gw_load_got_path(struct gw_trans *,
209 struct gw_dir *);
210 static const struct got_error* gw_parse_querystring(struct gw_trans *);
212 static const struct got_error* gw_blame(struct gw_trans *);
213 static const struct got_error* gw_blob(struct gw_trans *);
214 static const struct got_error* gw_diff(struct gw_trans *);
215 static const struct got_error* gw_index(struct gw_trans *);
216 static const struct got_error* gw_commits(struct gw_trans *);
217 static const struct got_error* gw_briefs(struct gw_trans *);
218 static const struct got_error* gw_summary(struct gw_trans *);
219 static const struct got_error* gw_tree(struct gw_trans *);
220 static const struct got_error* gw_tag(struct gw_trans *);
222 struct gw_query_action {
223 unsigned int func_id;
224 const char *func_name;
225 const struct got_error *(*func_main)(struct gw_trans *);
226 char *template;
227 };
229 enum gw_query_actions {
230 GW_BLAME,
231 GW_BLOB,
232 GW_BRIEFS,
233 GW_COMMITS,
234 GW_DIFF,
235 GW_ERR,
236 GW_INDEX,
237 GW_SUMMARY,
238 GW_TAG,
239 GW_TREE,
240 };
242 static struct gw_query_action gw_query_funcs[] = {
243 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
244 { GW_BLOB, "blob", NULL, NULL },
245 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
246 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
247 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
248 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
249 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
250 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
251 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
252 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
253 };
255 static const struct got_error *
256 gw_kcgi_error(enum kcgi_err kerr)
258 if (kerr == KCGI_OK)
259 return NULL;
261 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
262 return got_error(GOT_ERR_CANCELLED);
264 if (kerr == KCGI_ENOMEM)
265 return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
267 if (kerr == KCGI_ENFILE)
268 return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
270 if (kerr == KCGI_EAGAIN)
271 return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
273 if (kerr == KCGI_FORM)
274 return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
276 return got_error_from_errno(kcgi_strerror(kerr));
279 static const struct got_error *
280 gw_apply_unveil(const char *repo_path, const char *repo_file)
282 const struct got_error *err;
284 if (repo_path && repo_file) {
285 char *full_path;
286 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
287 return got_error_from_errno("asprintf unveil");
288 if (unveil(full_path, "r") != 0)
289 return got_error_from_errno2("unveil", full_path);
292 if (repo_path && unveil(repo_path, "r") != 0)
293 return got_error_from_errno2("unveil", repo_path);
295 if (unveil("/tmp", "rwc") != 0)
296 return got_error_from_errno2("unveil", "/tmp");
298 err = got_privsep_unveil_exec_helpers();
299 if (err != NULL)
300 return err;
302 if (unveil(NULL, NULL) != 0)
303 return got_error_from_errno("unveil");
305 return NULL;
308 static const struct got_error *
309 gw_empty_string(char **s)
311 *s = strdup("");
312 if (*s == NULL)
313 return got_error_from_errno("strdup");
314 return NULL;
317 static const struct got_error *
318 gw_blame(struct gw_trans *gw_trans)
320 const struct got_error *error = NULL;
321 struct gw_header *header = NULL;
322 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
323 enum kcgi_err kerr;
325 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
326 NULL) == -1)
327 return got_error_from_errno("pledge");
329 if ((header = gw_init_header()) == NULL)
330 return got_error_from_errno("malloc");
332 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
333 if (error)
334 goto done;
336 error = gw_get_header(gw_trans, header, 1);
337 if (error)
338 goto done;
340 blame_html = gw_get_file_blame_blob(gw_trans);
342 if (blame_html == NULL) {
343 blame_html = strdup("");
344 if (blame_html == NULL) {
345 error = got_error_from_errno("strdup");
346 goto done;
350 if (asprintf(&blame_html_disp, blame_header,
351 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
352 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
353 blame_html) == -1) {
354 error = got_error_from_errno("asprintf");
355 goto done;
358 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
359 error = got_error_from_errno("asprintf");
360 goto done;
363 kerr = khttp_puts(gw_trans->gw_req, blame);
364 if (kerr != KCGI_OK)
365 error = gw_kcgi_error(kerr);
366 done:
367 got_ref_list_free(&header->refs);
368 gw_free_headers(header);
369 free(blame_html_disp);
370 free(blame_html);
371 free(blame);
372 return error;
375 static const struct got_error *
376 gw_blob(struct gw_trans *gw_trans)
378 const struct got_error *error = NULL;
379 struct gw_header *header = NULL;
380 char *blob = NULL;
381 enum kcgi_err kerr;
383 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
384 NULL) == -1)
385 return got_error_from_errno("pledge");
387 if ((header = gw_init_header()) == NULL)
388 return got_error_from_errno("malloc");
390 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
391 if (error)
392 goto done;
394 error = gw_get_header(gw_trans, header, 1);
395 if (error)
396 goto done;
398 blob = gw_get_file_blame_blob(gw_trans);
400 if (blob == NULL) {
401 blob = strdup("");
402 if (blob == NULL) {
403 error = got_error_from_errno("strdup");
404 goto done;
408 if (gw_trans->mime == KMIME_APP_OCTET_STREAM)
409 goto done;
410 else {
411 kerr = khttp_puts(gw_trans->gw_req, blob);
412 if (kerr != KCGI_OK)
413 error = gw_kcgi_error(kerr);
415 done:
416 got_ref_list_free(&header->refs);
417 gw_free_headers(header);
418 free(blob);
419 return error;
422 static const struct got_error *
423 gw_diff(struct gw_trans *gw_trans)
425 const struct got_error *error = NULL;
426 struct gw_header *header = NULL;
427 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
428 enum kcgi_err kerr;
430 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
431 NULL) == -1)
432 return got_error_from_errno("pledge");
434 if ((header = gw_init_header()) == NULL)
435 return got_error_from_errno("malloc");
437 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
438 if (error)
439 goto done;
441 error = gw_get_header(gw_trans, header, 1);
442 if (error)
443 goto done;
445 diff_html = gw_get_diff(gw_trans, header);
447 if (diff_html == NULL) {
448 diff_html = strdup("");
449 if (diff_html == NULL) {
450 error = got_error_from_errno("strdup");
451 goto done;
455 if (asprintf(&diff_html_disp, diff_header,
456 gw_gen_diff_header(header->parent_id, header->commit_id),
457 gw_gen_commit_header(header->commit_id, header->refs_str),
458 gw_gen_tree_header(header->tree_id),
459 gw_gen_author_header(header->author),
460 gw_gen_committer_header(header->committer),
461 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
462 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
463 diff_html) == -1) {
464 error = got_error_from_errno("asprintf");
465 goto done;
468 if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
469 error = got_error_from_errno("asprintf");
470 goto done;
473 kerr = khttp_puts(gw_trans->gw_req, diff);
474 if (kerr != KCGI_OK)
475 error = gw_kcgi_error(kerr);
476 done:
477 got_ref_list_free(&header->refs);
478 gw_free_headers(header);
479 free(diff_html_disp);
480 free(diff_html);
481 free(diff);
482 return error;
485 static const struct got_error *
486 gw_index(struct gw_trans *gw_trans)
488 const struct got_error *error = NULL;
489 struct gw_dir *gw_dir = NULL;
490 char *html, *navs, *next, *prev;
491 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
492 enum kcgi_err kerr;
494 if (pledge("stdio rpath proc exec sendfd unveil",
495 NULL) == -1) {
496 error = got_error_from_errno("pledge");
497 return error;
500 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
501 if (error)
502 return error;
504 error = gw_load_got_paths(gw_trans);
505 if (error)
506 return error;
508 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
509 if (kerr != KCGI_OK)
510 return gw_kcgi_error(kerr);
512 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
513 if (asprintf(&html, index_projects_empty,
514 gw_trans->gw_conf->got_repos_path) == -1)
515 return got_error_from_errno("asprintf");
516 kerr = khttp_puts(gw_trans->gw_req, html);
517 if (kerr != KCGI_OK)
518 error = gw_kcgi_error(kerr);
519 free(html);
520 return error;
523 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
524 dir_c++;
526 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
527 if (gw_trans->page > 0 && (gw_trans->page *
528 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
529 prev_disp++;
530 continue;
533 prev_disp++;
535 if (error)
536 return error;
537 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
538 gw_dir->name, gw_dir->name) == -1)
539 return got_error_from_errno("asprintf");
541 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
542 gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
543 gw_dir->age,
544 navs) == -1)
545 return got_error_from_errno("asprintf");
547 kerr = khttp_puts(gw_trans->gw_req, html);
548 free(navs);
549 free(html);
550 if (kerr != KCGI_OK)
551 return gw_kcgi_error(kerr);
553 if (gw_trans->gw_conf->got_max_repos_display == 0)
554 continue;
556 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
557 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
558 if (kerr != KCGI_OK)
559 return gw_kcgi_error(kerr);
560 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
561 (gw_trans->page > 0) &&
562 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
563 prev_disp == gw_trans->repos_total)) {
564 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
565 if (kerr != KCGI_OK)
566 return gw_kcgi_error(kerr);
569 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
570 (gw_trans->page > 0) &&
571 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
572 prev_disp == gw_trans->repos_total)) {
573 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
574 return got_error_from_errno("asprintf");
575 kerr = khttp_puts(gw_trans->gw_req, prev);
576 free(prev);
577 if (kerr != KCGI_OK)
578 return gw_kcgi_error(kerr);
581 kerr = khttp_puts(gw_trans->gw_req, div_end);
582 if (kerr != KCGI_OK)
583 return gw_kcgi_error(kerr);
585 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
586 next_disp == gw_trans->gw_conf->got_max_repos_display &&
587 dir_c != (gw_trans->page + 1) *
588 gw_trans->gw_conf->got_max_repos_display) {
589 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
590 return got_error_from_errno("calloc");
591 kerr = khttp_puts(gw_trans->gw_req, next);
592 free(next);
593 if (kerr != KCGI_OK)
594 return gw_kcgi_error(kerr);
595 kerr = khttp_puts(gw_trans->gw_req, div_end);
596 if (kerr != KCGI_OK)
597 error = gw_kcgi_error(kerr);
598 next_disp = 0;
599 break;
602 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
603 (gw_trans->page > 0) &&
604 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
605 prev_disp == gw_trans->repos_total)) {
606 kerr = khttp_puts(gw_trans->gw_req, div_end);
607 if (kerr != KCGI_OK)
608 return gw_kcgi_error(kerr);
611 next_disp++;
613 return error;
616 static const struct got_error *
617 gw_commits(struct gw_trans *gw_trans)
619 const struct got_error *error = NULL;
620 char *commits_html, *commits_navs_html;
621 struct gw_header *header = NULL, *n_header = NULL;
622 enum kcgi_err kerr;
624 if ((header = gw_init_header()) == NULL)
625 return got_error_from_errno("malloc");
627 if (pledge("stdio rpath proc exec sendfd unveil",
628 NULL) == -1) {
629 error = got_error_from_errno("pledge");
630 goto done;
633 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
634 if (error)
635 goto done;
637 error = gw_get_header(gw_trans, header,
638 gw_trans->gw_conf->got_max_commits_display);
639 if (error)
640 goto done;
642 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
643 if (kerr != KCGI_OK) {
644 error = gw_kcgi_error(kerr);
645 goto done;
647 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
648 if (asprintf(&commits_navs_html, commits_navs,
649 gw_trans->repo_name, n_header->commit_id,
650 gw_trans->repo_name, n_header->commit_id,
651 gw_trans->repo_name, n_header->commit_id) == -1) {
652 error = got_error_from_errno("asprintf");
653 goto done;
656 if (asprintf(&commits_html, commits_line,
657 gw_gen_commit_header(n_header->commit_id,
658 n_header->refs_str),
659 gw_gen_author_header(n_header->author),
660 gw_gen_committer_header(n_header->committer),
661 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
662 TM_LONG)), gw_html_escape(n_header->commit_msg),
663 commits_navs_html) == -1) {
664 error = got_error_from_errno("asprintf");
665 goto done;
667 kerr = khttp_puts(gw_trans->gw_req, commits_html);
668 if (kerr != KCGI_OK) {
669 error = gw_kcgi_error(kerr);
670 goto done;
673 kerr = khttp_puts(gw_trans->gw_req, div_end);
674 if (kerr != KCGI_OK)
675 error = gw_kcgi_error(kerr);
676 done:
677 got_ref_list_free(&header->refs);
678 gw_free_headers(header);
679 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
680 gw_free_headers(n_header);
681 return error;
684 static const struct got_error *
685 gw_briefs(struct gw_trans *gw_trans)
687 const struct got_error *error = NULL;
688 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
689 struct gw_header *header = NULL, *n_header = NULL;
690 enum kcgi_err kerr;
692 if ((header = gw_init_header()) == NULL)
693 return got_error_from_errno("malloc");
695 if (pledge("stdio rpath proc exec sendfd unveil",
696 NULL) == -1) {
697 error = got_error_from_errno("pledge");
698 goto done;
701 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
702 if (error)
703 goto done;
705 if (gw_trans->action == GW_SUMMARY)
706 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
707 else
708 error = gw_get_header(gw_trans, header,
709 gw_trans->gw_conf->got_max_commits_display);
710 if (error)
711 goto done;
713 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
714 if (kerr != KCGI_OK) {
715 error = gw_kcgi_error(kerr);
716 goto done;
719 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
720 if (asprintf(&briefs_navs_html, briefs_navs,
721 gw_trans->repo_name, n_header->commit_id,
722 gw_trans->repo_name, n_header->commit_id,
723 gw_trans->repo_name, n_header->commit_id) == -1) {
724 error = got_error_from_errno("asprintf");
725 goto done;
727 newline = strchr(n_header->commit_msg, '\n');
728 if (newline)
729 *newline = '\0';
730 if (asprintf(&briefs_html, briefs_line,
731 gw_get_time_str(n_header->committer_time, TM_DIFF),
732 n_header->author, gw_html_escape(n_header->commit_msg),
733 briefs_navs_html) == -1) {
734 error = got_error_from_errno("asprintf");
735 goto done;
737 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
738 if (kerr != KCGI_OK) {
739 error = gw_kcgi_error(kerr);
740 goto done;
743 kerr = khttp_puts(gw_trans->gw_req, div_end);
744 if (kerr != KCGI_OK)
745 error = gw_kcgi_error(kerr);
746 done:
747 got_ref_list_free(&header->refs);
748 gw_free_headers(header);
749 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
750 gw_free_headers(n_header);
751 return error;
754 static const struct got_error *
755 gw_summary(struct gw_trans *gw_trans)
757 const struct got_error *error = NULL;
758 char *description_html = NULL, *repo_owner_html = NULL;
759 char *age = NULL, *repo_age_html = NULL, *cloneurl_html = NULL;
760 char *tags = NULL, *tags_html = NULL;
761 char *heads = NULL, *heads_html = NULL;
762 enum kcgi_err kerr;
764 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
765 return got_error_from_errno("pledge");
767 /* unveil is applied with gw_briefs below */
769 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
770 if (kerr != KCGI_OK)
771 return gw_kcgi_error(kerr);
773 if (gw_trans->gw_conf->got_show_repo_description) {
774 if (gw_trans->gw_dir->description != NULL &&
775 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
776 if (asprintf(&description_html, description,
777 gw_trans->gw_dir->description) == -1) {
778 error = got_error_from_errno("asprintf");
779 goto done;
782 kerr = khttp_puts(gw_trans->gw_req, description_html);
783 if (kerr != KCGI_OK) {
784 error = gw_kcgi_error(kerr);
785 goto done;
790 if (gw_trans->gw_conf->got_show_repo_owner &&
791 gw_trans->gw_dir->owner != NULL) {
792 if (asprintf(&repo_owner_html, repo_owner,
793 gw_trans->gw_dir->owner) == -1) {
794 error = got_error_from_errno("asprintf");
795 goto done;
798 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
799 if (kerr != KCGI_OK) {
800 error = gw_kcgi_error(kerr);
801 goto done;
805 if (gw_trans->gw_conf->got_show_repo_age) {
806 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
807 "refs/heads", TM_LONG);
808 if (error)
809 goto done;
810 if (strcmp(age, "") != 0) {
811 if (asprintf(&repo_age_html, last_change, age) == -1) {
812 error = got_error_from_errno("asprintf");
813 goto done;
816 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
817 if (kerr != KCGI_OK) {
818 error = gw_kcgi_error(kerr);
819 goto done;
824 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
825 if (gw_trans->gw_dir->url != NULL &&
826 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
827 if (asprintf(&cloneurl_html, cloneurl,
828 gw_trans->gw_dir->url) == -1) {
829 error = got_error_from_errno("asprintf");
830 goto done;
833 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
834 if (kerr != KCGI_OK) {
835 error = gw_kcgi_error(kerr);
836 goto done;
840 kerr = khttp_puts(gw_trans->gw_req, div_end);
841 if (kerr != KCGI_OK) {
842 error = gw_kcgi_error(kerr);
843 goto done;
846 error = gw_briefs(gw_trans);
847 if (error)
848 goto done;
850 tags = gw_get_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP, TAGBRIEF);
851 heads = gw_get_repo_heads(gw_trans);
853 if (tags != NULL && strcmp(tags, "") != 0) {
854 if (asprintf(&tags_html, summary_tags, tags) == -1) {
855 error = got_error_from_errno("asprintf");
856 goto done;
858 kerr = khttp_puts(gw_trans->gw_req, tags_html);
859 if (kerr != KCGI_OK) {
860 error = gw_kcgi_error(kerr);
861 goto done;
865 if (heads != NULL && strcmp(heads, "") != 0) {
866 if (asprintf(&heads_html, summary_heads, heads) == -1) {
867 error = got_error_from_errno("asprintf");
868 goto done;
870 kerr = khttp_puts(gw_trans->gw_req, heads_html);
871 if (kerr != KCGI_OK) {
872 error = gw_kcgi_error(kerr);
873 goto done;
876 done:
877 free(description_html);
878 free(repo_owner_html);
879 free(age);
880 free(repo_age_html);
881 free(cloneurl_html);
882 free(tags);
883 free(tags_html);
884 free(heads);
885 free(heads_html);
886 return error;
889 static const struct got_error *
890 gw_tree(struct gw_trans *gw_trans)
892 const struct got_error *error = NULL;
893 struct gw_header *header = NULL;
894 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
895 enum kcgi_err kerr;
897 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
898 return got_error_from_errno("pledge");
900 if ((header = gw_init_header()) == NULL)
901 return got_error_from_errno("malloc");
903 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
904 if (error)
905 goto done;
907 error = gw_get_header(gw_trans, header, 1);
908 if (error)
909 goto done;
911 tree_html = gw_get_repo_tree(gw_trans);
913 if (tree_html == NULL) {
914 tree_html = strdup("");
915 if (tree_html == NULL) {
916 error = got_error_from_errno("strdup");
917 goto done;
921 if (asprintf(&tree_html_disp, tree_header,
922 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
923 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
924 tree_html) == -1) {
925 error = got_error_from_errno("asprintf");
926 goto done;
929 if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
930 error = got_error_from_errno("asprintf");
931 goto done;
934 kerr = khttp_puts(gw_trans->gw_req, tree);
935 if (kerr != KCGI_OK)
936 error = gw_kcgi_error(kerr);
937 done:
938 got_ref_list_free(&header->refs);
939 gw_free_headers(header);
940 free(tree_html_disp);
941 free(tree_html);
942 free(tree);
943 return error;
946 static const struct got_error *
947 gw_tag(struct gw_trans *gw_trans)
949 const struct got_error *error = NULL;
950 struct gw_header *header = NULL;
951 char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
952 enum kcgi_err kerr;
954 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
955 return got_error_from_errno("pledge");
957 if ((header = gw_init_header()) == NULL)
958 return got_error_from_errno("malloc");
960 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
961 if (error)
962 goto done;
964 error = gw_get_header(gw_trans, header, 1);
965 if (error)
966 goto done;
968 tag_html = gw_get_repo_tags(gw_trans, header, 1, TAGFULL);
969 if (tag_html == NULL) {
970 tag_html = strdup("");
971 if (tag_html == NULL) {
972 error = got_error_from_errno("strdup");
973 goto done;
977 if (asprintf(&tag_html_disp, tag_header,
978 gw_gen_commit_header(header->commit_id, header->refs_str),
979 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
980 tag_html) == -1) {
981 error = got_error_from_errno("asprintf");
982 goto done;
985 if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
986 error = got_error_from_errno("asprintf");
987 goto done;
990 kerr = khttp_puts(gw_trans->gw_req, tag);
991 if (kerr != KCGI_OK)
992 error = gw_kcgi_error(kerr);
993 done:
994 got_ref_list_free(&header->refs);
995 gw_free_headers(header);
996 free(tag_html_disp);
997 free(tag_html);
998 free(tag);
999 return error;
1002 static const struct got_error *
1003 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1005 const struct got_error *error = NULL;
1006 DIR *dt;
1007 char *dir_test;
1008 int opened = 0;
1010 if (asprintf(&dir_test, "%s/%s/%s",
1011 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1012 GOTWEB_GIT_DIR) == -1)
1013 return got_error_from_errno("asprintf");
1015 dt = opendir(dir_test);
1016 if (dt == NULL) {
1017 free(dir_test);
1018 } else {
1019 gw_dir->path = strdup(dir_test);
1020 opened = 1;
1021 goto done;
1024 if (asprintf(&dir_test, "%s/%s/%s",
1025 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1026 GOTWEB_GOT_DIR) == -1)
1027 return got_error_from_errno("asprintf");
1029 dt = opendir(dir_test);
1030 if (dt == NULL)
1031 free(dir_test);
1032 else {
1033 opened = 1;
1034 error = got_error(GOT_ERR_NOT_GIT_REPO);
1035 goto errored;
1038 if (asprintf(&dir_test, "%s/%s",
1039 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1040 return got_error_from_errno("asprintf");
1042 gw_dir->path = strdup(dir_test);
1044 done:
1045 error = gw_get_repo_description(&gw_dir->description, gw_trans,
1046 gw_dir->path);
1047 if (error)
1048 goto errored;
1049 error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1050 if (error)
1051 goto errored;
1052 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1053 "refs/heads", TM_DIFF);
1054 if (error)
1055 goto errored;
1056 error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1057 errored:
1058 free(dir_test);
1059 if (opened)
1060 closedir(dt);
1061 return error;
1064 static const struct got_error *
1065 gw_load_got_paths(struct gw_trans *gw_trans)
1067 const struct got_error *error = NULL;
1068 DIR *d;
1069 struct dirent **sd_dent;
1070 struct gw_dir *gw_dir;
1071 struct stat st;
1072 unsigned int d_cnt, d_i;
1074 d = opendir(gw_trans->gw_conf->got_repos_path);
1075 if (d == NULL) {
1076 error = got_error_from_errno2("opendir",
1077 gw_trans->gw_conf->got_repos_path);
1078 return error;
1081 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1082 alphasort);
1083 if (d_cnt == -1) {
1084 error = got_error_from_errno2("scandir",
1085 gw_trans->gw_conf->got_repos_path);
1086 return error;
1089 for (d_i = 0; d_i < d_cnt; d_i++) {
1090 if (gw_trans->gw_conf->got_max_repos > 0 &&
1091 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1092 break; /* account for parent and self */
1094 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1095 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1096 continue;
1098 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1099 return got_error_from_errno("gw_dir malloc");
1101 error = gw_load_got_path(gw_trans, gw_dir);
1102 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1103 continue;
1104 else if (error)
1105 return error;
1107 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1108 !got_path_dir_is_empty(gw_dir->path)) {
1109 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1110 entry);
1111 gw_trans->repos_total++;
1115 closedir(d);
1116 return error;
1119 static const struct got_error *
1120 gw_parse_querystring(struct gw_trans *gw_trans)
1122 const struct got_error *error = NULL;
1123 struct kpair *p;
1124 struct gw_query_action *action = NULL;
1125 unsigned int i;
1127 if (gw_trans->gw_req->fieldnmap[0]) {
1128 error = got_error_from_errno("bad parse");
1129 return error;
1130 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1131 /* define gw_trans->repo_path */
1132 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1133 return got_error_from_errno("asprintf");
1135 if (asprintf(&gw_trans->repo_path, "%s/%s",
1136 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1137 return got_error_from_errno("asprintf");
1139 /* get action and set function */
1140 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1141 for (i = 0; i < nitems(gw_query_funcs); i++) {
1142 action = &gw_query_funcs[i];
1143 if (action->func_name == NULL)
1144 continue;
1146 if (strcmp(action->func_name,
1147 p->parsed.s) == 0) {
1148 gw_trans->action = i;
1149 if (asprintf(&gw_trans->action_name,
1150 "%s", action->func_name) == -1)
1151 return
1152 got_error_from_errno(
1153 "asprintf");
1155 break;
1158 action = NULL;
1161 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1162 if (asprintf(&gw_trans->commit, "%s",
1163 p->parsed.s) == -1)
1164 return got_error_from_errno("asprintf");
1166 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1167 if (asprintf(&gw_trans->repo_file, "%s",
1168 p->parsed.s) == -1)
1169 return got_error_from_errno("asprintf");
1171 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1172 if (asprintf(&gw_trans->repo_folder, "%s",
1173 p->parsed.s) == -1)
1174 return got_error_from_errno("asprintf");
1176 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1177 if (asprintf(&gw_trans->headref, "%s",
1178 p->parsed.s) == -1)
1179 return got_error_from_errno("asprintf");
1181 if (action == NULL) {
1182 error = got_error_from_errno("invalid action");
1183 return error;
1185 if ((gw_trans->gw_dir =
1186 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1187 return got_error_from_errno("gw_dir malloc");
1189 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1190 if (error)
1191 return error;
1192 } else
1193 gw_trans->action = GW_INDEX;
1195 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1196 gw_trans->page = p->parsed.i;
1198 return error;
1201 static struct gw_dir *
1202 gw_init_gw_dir(char *dir)
1204 struct gw_dir *gw_dir;
1206 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1207 return NULL;
1209 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1210 return NULL;
1212 return gw_dir;
1215 static const struct got_error *
1216 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1218 enum kcgi_err kerr;
1220 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1221 if (kerr != KCGI_OK)
1222 return gw_kcgi_error(kerr);
1223 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1224 khttps[code]);
1225 if (kerr != KCGI_OK)
1226 return gw_kcgi_error(kerr);
1227 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1228 kmimetypes[mime]);
1229 if (kerr != KCGI_OK)
1230 return gw_kcgi_error(kerr);
1231 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1232 "nosniff");
1233 if (kerr != KCGI_OK)
1234 return gw_kcgi_error(kerr);
1235 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1236 if (kerr != KCGI_OK)
1237 return gw_kcgi_error(kerr);
1238 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1239 "1; mode=block");
1240 if (kerr != KCGI_OK)
1241 return gw_kcgi_error(kerr);
1243 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1244 kerr = khttp_head(gw_trans->gw_req,
1245 kresps[KRESP_CONTENT_DISPOSITION],
1246 "attachment; filename=%s", gw_trans->repo_file);
1247 if (kerr != KCGI_OK)
1248 return gw_kcgi_error(kerr);
1251 kerr = khttp_body(gw_trans->gw_req);
1252 return gw_kcgi_error(kerr);
1255 static const struct got_error *
1256 gw_display_index(struct gw_trans *gw_trans)
1258 const struct got_error *error;
1259 enum kcgi_err kerr;
1261 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1262 if (error)
1263 return error;
1265 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1266 if (kerr)
1267 return gw_kcgi_error(kerr);
1269 if (gw_trans->action != GW_BLOB) {
1270 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1271 gw_query_funcs[gw_trans->action].template);
1272 if (kerr != KCGI_OK) {
1273 khtml_close(gw_trans->gw_html_req);
1274 return gw_kcgi_error(kerr);
1278 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1281 static void
1282 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1284 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1285 return;
1287 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1288 return;
1290 khttp_puts(gw_trans->gw_req, err->msg);
1291 khtml_close(gw_trans->gw_html_req);
1294 static int
1295 gw_template(size_t key, void *arg)
1297 const struct got_error *error = NULL;
1298 struct gw_trans *gw_trans = arg;
1299 char *gw_got_link, *gw_site_link;
1300 char *site_owner_name, *site_owner_name_h;
1302 switch (key) {
1303 case (TEMPL_HEAD):
1304 khttp_puts(gw_trans->gw_req, head);
1305 break;
1306 case(TEMPL_HEADER):
1307 gw_got_link = gw_get_got_link(gw_trans);
1308 if (gw_got_link != NULL)
1309 khttp_puts(gw_trans->gw_req, gw_got_link);
1311 free(gw_got_link);
1312 break;
1313 case (TEMPL_SITEPATH):
1314 gw_site_link = gw_get_site_link(gw_trans);
1315 if (gw_site_link != NULL)
1316 khttp_puts(gw_trans->gw_req, gw_site_link);
1318 free(gw_site_link);
1319 break;
1320 case(TEMPL_TITLE):
1321 if (gw_trans->gw_conf->got_site_name != NULL)
1322 khtml_puts(gw_trans->gw_html_req,
1323 gw_trans->gw_conf->got_site_name);
1325 break;
1326 case (TEMPL_SEARCH):
1327 khttp_puts(gw_trans->gw_req, search);
1328 break;
1329 case(TEMPL_SITEOWNER):
1330 if (gw_trans->gw_conf->got_site_owner != NULL &&
1331 gw_trans->gw_conf->got_show_site_owner) {
1332 site_owner_name =
1333 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1334 if (asprintf(&site_owner_name_h, site_owner,
1335 site_owner_name) == -1)
1336 return 0;
1338 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1339 free(site_owner_name);
1340 free(site_owner_name_h);
1342 break;
1343 case(TEMPL_CONTENT):
1344 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1345 if (error)
1346 khttp_puts(gw_trans->gw_req, error->msg);
1347 break;
1348 default:
1349 return 0;
1351 return 1;
1354 static char *
1355 gw_gen_commit_header(char *str1, char *str2)
1357 char *return_html = NULL, *ref_str = NULL;
1359 if (strcmp(str2, "") != 0) {
1360 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1361 return_html = strdup("");
1362 return return_html;
1364 } else
1365 ref_str = strdup("");
1368 if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1369 return_html = strdup("");
1371 free(ref_str);
1372 return return_html;
1375 static char *
1376 gw_gen_diff_header(char *str1, char *str2)
1378 char *return_html = NULL;
1380 if (asprintf(&return_html, header_diff_html, str1, str2) == -1)
1381 return_html = strdup("");
1383 return return_html;
1386 static char *
1387 gw_gen_author_header(char *str)
1389 char *return_html = NULL;
1391 if (asprintf(&return_html, header_author_html, str) == -1)
1392 return_html = strdup("");
1394 return return_html;
1397 static char *
1398 gw_gen_committer_header(char *str)
1400 char *return_html = NULL;
1402 if (asprintf(&return_html, header_committer_html, str) == -1)
1403 return_html = strdup("");
1405 return return_html;
1408 static char *
1409 gw_gen_age_header(char *str)
1411 char *return_html = NULL;
1413 if (asprintf(&return_html, header_age_html, str) == -1)
1414 return_html = strdup("");
1416 return return_html;
1419 static char *
1420 gw_gen_commit_msg_header(char *str)
1422 char *return_html = NULL;
1424 if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1425 return_html = strdup("");
1427 return return_html;
1430 static char *
1431 gw_gen_tree_header(char *str)
1433 char *return_html = NULL;
1435 if (asprintf(&return_html, header_tree_html, str) == -1)
1436 return_html = strdup("");
1438 return return_html;
1441 static const struct got_error *
1442 gw_get_repo_description(char **description, struct gw_trans *gw_trans,
1443 char *dir)
1445 const struct got_error *error = NULL;
1446 FILE *f = NULL;
1447 char *d_file = NULL;
1448 unsigned int len;
1449 size_t n;
1451 *description = NULL;
1452 if (gw_trans->gw_conf->got_show_repo_description == 0)
1453 return gw_empty_string(description);
1455 if (asprintf(&d_file, "%s/description", dir) == -1)
1456 return got_error_from_errno("asprintf");
1458 f = fopen(d_file, "r");
1459 if (f == NULL) {
1460 if (errno == ENOENT || errno == EACCES)
1461 return gw_empty_string(description);
1462 error = got_error_from_errno2("fopen", d_file);
1463 goto done;
1466 if (fseek(f, 0, SEEK_END) == -1) {
1467 error = got_ferror(f, GOT_ERR_IO);
1468 goto done;
1470 len = ftell(f);
1471 if (len == -1) {
1472 error = got_ferror(f, GOT_ERR_IO);
1473 goto done;
1475 if (fseek(f, 0, SEEK_SET) == -1) {
1476 error = got_ferror(f, GOT_ERR_IO);
1477 goto done;
1479 *description = calloc(len + 1, sizeof(**description));
1480 if (*description == NULL) {
1481 error = got_error_from_errno("calloc");
1482 goto done;
1485 n = fread(*description, 1, len, f);
1486 if (n == 0 && ferror(f))
1487 error = got_ferror(f, GOT_ERR_IO);
1488 done:
1489 if (f != NULL && fclose(f) == -1 && error == NULL)
1490 error = got_error_from_errno("fclose");
1491 free(d_file);
1492 return error;
1495 static char *
1496 gw_get_time_str(time_t committer_time, int ref_tm)
1498 struct tm tm;
1499 time_t diff_time;
1500 char *years = "years ago", *months = "months ago";
1501 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1502 char *minutes = "minutes ago", *seconds = "seconds ago";
1503 char *now = "right now";
1504 char *repo_age, *s;
1505 char datebuf[29];
1507 switch (ref_tm) {
1508 case TM_DIFF:
1509 diff_time = time(NULL) - committer_time;
1510 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1511 if (asprintf(&repo_age, "%lld %s",
1512 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1513 return NULL;
1514 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1515 if (asprintf(&repo_age, "%lld %s",
1516 (diff_time / 60 / 60 / 24 / (365 / 12)),
1517 months) == -1)
1518 return NULL;
1519 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1520 if (asprintf(&repo_age, "%lld %s",
1521 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1522 return NULL;
1523 } else if (diff_time > 60 * 60 * 24 * 2) {
1524 if (asprintf(&repo_age, "%lld %s",
1525 (diff_time / 60 / 60 / 24), days) == -1)
1526 return NULL;
1527 } else if (diff_time > 60 * 60 * 2) {
1528 if (asprintf(&repo_age, "%lld %s",
1529 (diff_time / 60 / 60), hours) == -1)
1530 return NULL;
1531 } else if (diff_time > 60 * 2) {
1532 if (asprintf(&repo_age, "%lld %s", (diff_time / 60),
1533 minutes) == -1)
1534 return NULL;
1535 } else if (diff_time > 2) {
1536 if (asprintf(&repo_age, "%lld %s", diff_time,
1537 seconds) == -1)
1538 return NULL;
1539 } else {
1540 if (asprintf(&repo_age, "%s", now) == -1)
1541 return NULL;
1543 break;
1544 case TM_LONG:
1545 if (gmtime_r(&committer_time, &tm) == NULL)
1546 return NULL;
1548 s = asctime_r(&tm, datebuf);
1549 if (s == NULL)
1550 return NULL;
1552 if (asprintf(&repo_age, "%s UTC", datebuf) == -1)
1553 return NULL;
1554 break;
1556 return repo_age;
1559 static const struct got_error *
1560 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
1561 char *repo_ref, int ref_tm)
1563 const struct got_error *error = NULL;
1564 struct got_object_id *id = NULL;
1565 struct got_repository *repo = NULL;
1566 struct got_commit_object *commit = NULL;
1567 struct got_reflist_head refs;
1568 struct got_reflist_entry *re;
1569 struct got_reference *head_ref;
1570 int is_head = 0;
1571 time_t committer_time = 0, cmp_time = 0;
1572 const char *refname;
1574 *repo_age = NULL;
1575 SIMPLEQ_INIT(&refs);
1577 if (repo_ref == NULL)
1578 return NULL;
1580 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1581 is_head = 1;
1583 if (gw_trans->gw_conf->got_show_repo_age == 0) {
1584 *repo_age = strdup("");
1585 if (*repo_age == NULL)
1586 return got_error_from_errno("strdup");
1587 return NULL;
1590 error = got_repo_open(&repo, dir, NULL);
1591 if (error)
1592 goto done;
1594 if (is_head)
1595 error = got_ref_list(&refs, repo, "refs/heads",
1596 got_ref_cmp_by_name, NULL);
1597 else
1598 error = got_ref_list(&refs, repo, repo_ref,
1599 got_ref_cmp_by_name, NULL);
1600 if (error)
1601 goto done;
1603 SIMPLEQ_FOREACH(re, &refs, entry) {
1604 if (is_head)
1605 refname = strdup(repo_ref);
1606 else
1607 refname = got_ref_get_name(re->ref);
1608 error = got_ref_open(&head_ref, repo, refname, 0);
1609 if (error)
1610 goto done;
1612 error = got_ref_resolve(&id, repo, head_ref);
1613 got_ref_close(head_ref);
1614 if (error)
1615 goto done;
1617 error = got_object_open_as_commit(&commit, repo, id);
1618 if (error)
1619 goto done;
1621 committer_time =
1622 got_object_commit_get_committer_time(commit);
1624 if (cmp_time < committer_time)
1625 cmp_time = committer_time;
1628 if (cmp_time != 0) {
1629 committer_time = cmp_time;
1630 *repo_age = gw_get_time_str(committer_time, ref_tm);
1631 } else {
1632 *repo_age = strdup("");
1633 if (*repo_age == NULL)
1634 error = got_error_from_errno("strdup");
1636 done:
1637 got_ref_list_free(&refs);
1638 free(id);
1639 return error;
1642 static char *
1643 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1645 const struct got_error *error;
1646 FILE *f = NULL;
1647 struct got_object_id *id1 = NULL, *id2 = NULL;
1648 struct buf *diffbuf = NULL;
1649 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL;
1650 char *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1651 int obj_type;
1652 size_t newsize;
1654 f = got_opentemp();
1655 if (f == NULL)
1656 return NULL;
1658 error = buf_alloc(&diffbuf, 0);
1659 if (error)
1660 return NULL;
1662 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1663 if (error)
1664 goto done;
1666 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1667 error = got_repo_match_object_id(&id1, &label1,
1668 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1669 if (error)
1670 goto done;
1673 error = got_repo_match_object_id(&id2, &label2,
1674 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1675 if (error)
1676 goto done;
1678 error = got_object_get_type(&obj_type, header->repo, id2);
1679 if (error)
1680 goto done;
1681 switch (obj_type) {
1682 case GOT_OBJ_TYPE_BLOB:
1683 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1684 header->repo, f);
1685 break;
1686 case GOT_OBJ_TYPE_TREE:
1687 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1688 header->repo, f);
1689 break;
1690 case GOT_OBJ_TYPE_COMMIT:
1691 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1692 header->repo, f);
1693 break;
1694 default:
1695 error = got_error(GOT_ERR_OBJ_TYPE);
1698 if (error)
1699 goto done;
1701 if ((buf = calloc(128, sizeof(char *))) == NULL)
1702 goto done;
1704 if (fseek(f, 0, SEEK_SET) == -1)
1705 goto done;
1707 while ((fgets(buf, 2048, f)) != NULL) {
1708 if (ferror(f))
1709 goto done;
1710 n_buf = buf;
1711 while (*n_buf == '\n')
1712 n_buf++;
1713 newline = strchr(n_buf, '\n');
1714 if (newline)
1715 *newline = ' ';
1717 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1718 if (buf_color == NULL)
1719 continue;
1721 error = buf_puts(&newsize, diffbuf, buf_color);
1722 if (error)
1723 goto done;
1725 error = buf_puts(&newsize, diffbuf, div_end);
1726 if (error)
1727 goto done;
1730 if (buf_len(diffbuf) > 0) {
1731 error = buf_putc(diffbuf, '\0');
1732 diff_html = strdup(buf_get(diffbuf));
1734 done:
1735 fclose(f);
1736 free(buf_color);
1737 free(buf);
1738 free(diffbuf);
1739 free(label1);
1740 free(label2);
1741 free(id1);
1742 free(id2);
1744 if (error)
1745 return NULL;
1746 else
1747 return diff_html;
1750 static const struct got_error *
1751 gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
1753 const struct got_error *error = NULL;
1754 struct got_repository *repo;
1755 const char *gitconfig_owner;
1757 *owner = NULL;
1759 if (gw_trans->gw_conf->got_show_repo_owner == 0)
1760 return NULL;
1762 error = got_repo_open(&repo, dir, NULL);
1763 if (error)
1764 return error;
1765 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
1766 if (gitconfig_owner) {
1767 *owner = strdup(gitconfig_owner);
1768 if (*owner == NULL)
1769 error = got_error_from_errno("strdup");
1771 got_repo_close(repo);
1772 return error;
1775 static const struct got_error *
1776 gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
1778 const struct got_error *error = NULL;
1779 FILE *f;
1780 char *d_file = NULL;
1781 unsigned int len;
1782 size_t n;
1784 *url = NULL;
1786 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
1787 return got_error_from_errno("asprintf");
1789 f = fopen(d_file, "r");
1790 if (f == NULL) {
1791 if (errno != ENOENT && errno != EACCES)
1792 error = got_error_from_errno2("fopen", d_file);
1793 goto done;
1796 if (fseek(f, 0, SEEK_END) == -1) {
1797 error = got_ferror(f, GOT_ERR_IO);
1798 goto done;
1800 len = ftell(f);
1801 if (len == -1) {
1802 error = got_ferror(f, GOT_ERR_IO);
1803 goto done;
1805 if (fseek(f, 0, SEEK_SET) == -1) {
1806 error = got_ferror(f, GOT_ERR_IO);
1807 goto done;
1810 *url = calloc(len + 1, sizeof(**url));
1811 if (*url == NULL) {
1812 error = got_error_from_errno("calloc");
1813 goto done;
1816 n = fread(*url, 1, len, f);
1817 if (n == 0 && ferror(f))
1818 error = got_ferror(f, GOT_ERR_IO);
1819 done:
1820 if (f && fclose(f) == -1 && error == NULL)
1821 error = got_error_from_errno("fclose");
1822 free(d_file);
1823 return NULL;
1826 static char *
1827 gw_get_repo_tags(struct gw_trans *gw_trans, struct gw_header *header, int limit,
1828 int tag_type)
1830 const struct got_error *error = NULL;
1831 struct got_repository *repo = NULL;
1832 struct got_reflist_head refs;
1833 struct got_reflist_entry *re;
1834 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL;
1835 char *age = NULL, *newline;
1836 struct buf *diffbuf = NULL;
1837 size_t newsize;
1839 SIMPLEQ_INIT(&refs);
1841 error = buf_alloc(&diffbuf, 0);
1842 if (error)
1843 return NULL;
1845 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1846 if (error)
1847 goto done;
1849 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1850 if (error)
1851 goto done;
1853 SIMPLEQ_FOREACH(re, &refs, entry) {
1854 const char *refname;
1855 char *refstr, *tag_commit0, *tag_commit, *id_str;
1856 const char *tagger;
1857 time_t tagger_time;
1858 struct got_object_id *id;
1859 struct got_tag_object *tag;
1861 refname = got_ref_get_name(re->ref);
1862 if (strncmp(refname, "refs/tags/", 10) != 0)
1863 continue;
1864 refname += 10;
1865 refstr = got_ref_to_str(re->ref);
1866 if (refstr == NULL) {
1867 error = got_error_from_errno("got_ref_to_str");
1868 goto done;
1871 error = got_ref_resolve(&id, repo, re->ref);
1872 if (error)
1873 goto done;
1874 error = got_object_open_as_tag(&tag, repo, id);
1875 free(id);
1876 if (error)
1877 goto done;
1879 tagger = got_object_tag_get_tagger(tag);
1880 tagger_time = got_object_tag_get_tagger_time(tag);
1882 error = got_object_id_str(&id_str,
1883 got_object_tag_get_object_id(tag));
1884 if (error)
1885 goto done;
1887 tag_commit0 = strdup(got_object_tag_get_message(tag));
1889 if (tag_commit0 == NULL) {
1890 error = got_error_from_errno("strdup");
1891 goto done;
1894 tag_commit = tag_commit0;
1895 while (*tag_commit == '\n')
1896 tag_commit++;
1898 switch (tag_type) {
1899 case TAGBRIEF:
1900 newline = strchr(tag_commit, '\n');
1901 if (newline)
1902 *newline = '\0';
1904 if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1905 TM_DIFF)) == -1) {
1906 error = got_error_from_errno("asprintf");
1907 goto done;
1910 if (asprintf(&tags_navs_disp, tags_navs,
1911 gw_trans->repo_name, id_str, gw_trans->repo_name,
1912 id_str, gw_trans->repo_name, id_str,
1913 gw_trans->repo_name, id_str) == -1) {
1914 error = got_error_from_errno("asprintf");
1915 goto done;
1918 if (asprintf(&tag_row, tags_row, age, refname,
1919 tag_commit, tags_navs_disp) == -1) {
1920 error = got_error_from_errno("asprintf");
1921 goto done;
1924 free(tags_navs_disp);
1925 break;
1926 case TAGFULL:
1927 if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1928 TM_LONG)) == -1) {
1929 error = got_error_from_errno("asprintf");
1930 goto done;
1932 if (asprintf(&tag_row, tag_info, age,
1933 gw_html_escape(tagger),
1934 gw_html_escape(tag_commit)) == -1) {
1935 error = got_error_from_errno("asprintf");
1936 goto done;
1938 break;
1939 default:
1940 break;
1943 got_object_tag_close(tag);
1945 error = buf_puts(&newsize, diffbuf, tag_row);
1947 free(id_str);
1948 free(refstr);
1949 free(age);
1950 free(tag_commit0);
1951 free(tag_row);
1953 if (error || (limit && --limit == 0))
1954 break;
1957 if (buf_len(diffbuf) > 0) {
1958 error = buf_putc(diffbuf, '\0');
1959 tags = strdup(buf_get(diffbuf));
1961 done:
1962 buf_free(diffbuf);
1963 got_ref_list_free(&refs);
1964 if (repo)
1965 got_repo_close(repo);
1966 if (error)
1967 return NULL;
1968 else
1969 return tags;
1972 static void
1973 gw_free_headers(struct gw_header *header)
1975 free(header->id);
1976 free(header->path);
1977 if (header->commit != NULL)
1978 got_object_commit_close(header->commit);
1979 if (header->repo)
1980 got_repo_close(header->repo);
1981 free(header->refs_str);
1982 free(header->commit_id);
1983 free(header->parent_id);
1984 free(header->tree_id);
1985 free(header->author);
1986 free(header->committer);
1987 free(header->commit_msg);
1990 static struct gw_header *
1991 gw_init_header()
1993 struct gw_header *header;
1995 header = malloc(sizeof(*header));
1996 if (header == NULL)
1997 return NULL;
1999 header->repo = NULL;
2000 header->commit = NULL;
2001 header->id = NULL;
2002 header->path = NULL;
2003 SIMPLEQ_INIT(&header->refs);
2005 return header;
2008 static const struct got_error *
2009 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2010 int limit)
2012 const struct got_error *error = NULL;
2013 struct got_commit_graph *graph = NULL;
2015 error = got_commit_graph_open(&graph, header->path, 0);
2016 if (error)
2017 goto done;
2019 error = got_commit_graph_iter_start(graph, header->id, header->repo,
2020 NULL, NULL);
2021 if (error)
2022 goto done;
2024 for (;;) {
2025 error = got_commit_graph_iter_next(&header->id, graph,
2026 header->repo, NULL, NULL);
2027 if (error) {
2028 if (error->code == GOT_ERR_ITER_COMPLETED)
2029 error = NULL;
2030 goto done;
2032 if (header->id == NULL)
2033 goto done;
2035 error = got_object_open_as_commit(&header->commit, header->repo,
2036 header->id);
2037 if (error)
2038 goto done;
2040 error = gw_get_commit(gw_trans, header);
2041 if (limit > 1) {
2042 struct gw_header *n_header = NULL;
2043 if ((n_header = gw_init_header()) == NULL) {
2044 error = got_error_from_errno("malloc");
2045 goto done;
2048 n_header->refs_str = strdup(header->refs_str);
2049 n_header->commit_id = strdup(header->commit_id);
2050 n_header->parent_id = strdup(header->parent_id);
2051 n_header->tree_id = strdup(header->tree_id);
2052 n_header->author = strdup(header->author);
2053 n_header->committer = strdup(header->committer);
2054 n_header->commit_msg = strdup(header->commit_msg);
2055 n_header->committer_time = header->committer_time;
2056 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2057 entry);
2059 if (error || (limit && --limit == 0))
2060 break;
2062 done:
2063 if (graph)
2064 got_commit_graph_close(graph);
2065 return error;
2068 static const struct got_error *
2069 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2071 const struct got_error *error = NULL;
2072 struct got_reflist_entry *re;
2073 struct got_object_id *id2 = NULL;
2074 struct got_object_qid *parent_id;
2075 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2077 /*print commit*/
2078 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2079 char *s;
2080 const char *name;
2081 struct got_tag_object *tag = NULL;
2082 int cmp;
2084 name = got_ref_get_name(re->ref);
2085 if (strcmp(name, GOT_REF_HEAD) == 0)
2086 continue;
2087 if (strncmp(name, "refs/", 5) == 0)
2088 name += 5;
2089 if (strncmp(name, "got/", 4) == 0)
2090 continue;
2091 if (strncmp(name, "heads/", 6) == 0)
2092 name += 6;
2093 if (strncmp(name, "remotes/", 8) == 0)
2094 name += 8;
2095 if (strncmp(name, "tags/", 5) == 0) {
2096 error = got_object_open_as_tag(&tag, header->repo,
2097 re->id);
2098 if (error) {
2099 if (error->code != GOT_ERR_OBJ_TYPE)
2100 continue;
2102 * Ref points at something other
2103 * than a tag.
2105 error = NULL;
2106 tag = NULL;
2109 cmp = got_object_id_cmp(tag ?
2110 got_object_tag_get_object_id(tag) : re->id, header->id);
2111 if (tag)
2112 got_object_tag_close(tag);
2113 if (cmp != 0)
2114 continue;
2115 s = refs_str;
2116 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2117 s ? ", " : "", name) == -1) {
2118 error = got_error_from_errno("asprintf");
2119 free(s);
2120 return error;
2122 header->refs_str = strdup(refs_str);
2123 free(s);
2126 if (refs_str == NULL)
2127 header->refs_str = strdup("");
2128 free(refs_str);
2130 error = got_object_id_str(&header->commit_id, header->id);
2131 if (error)
2132 return error;
2134 error = got_object_id_str(&header->tree_id,
2135 got_object_commit_get_tree_id(header->commit));
2136 if (error)
2137 return error;
2139 if (gw_trans->action == GW_DIFF) {
2140 parent_id = SIMPLEQ_FIRST(
2141 got_object_commit_get_parent_ids(header->commit));
2142 if (parent_id != NULL) {
2143 id2 = got_object_id_dup(parent_id->id);
2144 free (parent_id);
2145 error = got_object_id_str(&header->parent_id, id2);
2146 if (error)
2147 return error;
2148 free(id2);
2149 } else
2150 header->parent_id = strdup("/dev/null");
2151 } else
2152 header->parent_id = strdup("");
2154 header->committer_time =
2155 got_object_commit_get_committer_time(header->commit);
2157 if (gw_trans->action != GW_BRIEFS && gw_trans->action != GW_SUMMARY) {
2158 header->author = strdup(
2159 gw_html_escape(got_object_commit_get_author(header->commit))
2161 } else {
2162 header->author = strdup(
2163 got_object_commit_get_author(header->commit)
2167 header->committer = strdup(
2168 gw_html_escape(got_object_commit_get_committer(header->commit))
2171 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2172 if (error)
2173 return error;
2175 commit_msg = commit_msg0;
2176 while (*commit_msg == '\n')
2177 commit_msg++;
2179 header->commit_msg = strdup(commit_msg);
2180 free(commit_msg0);
2181 return error;
2184 static const struct got_error *
2185 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2187 const struct got_error *error = NULL;
2188 char *in_repo_path = NULL;
2190 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2191 if (error)
2192 return error;
2194 if (gw_trans->commit == NULL) {
2195 struct got_reference *head_ref;
2196 error = got_ref_open(&head_ref, header->repo,
2197 gw_trans->headref, 0);
2198 if (error)
2199 return error;
2201 error = got_ref_resolve(&header->id, header->repo, head_ref);
2202 got_ref_close(head_ref);
2203 if (error)
2204 return error;
2206 error = got_object_open_as_commit(&header->commit,
2207 header->repo, header->id);
2208 } else {
2209 struct got_reference *ref;
2210 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2211 if (error == NULL) {
2212 int obj_type;
2213 error = got_ref_resolve(&header->id, header->repo, ref);
2214 got_ref_close(ref);
2215 if (error)
2216 return error;
2217 error = got_object_get_type(&obj_type, header->repo,
2218 header->id);
2219 if (error)
2220 return error;
2221 if (obj_type == GOT_OBJ_TYPE_TAG) {
2222 struct got_tag_object *tag;
2223 error = got_object_open_as_tag(&tag,
2224 header->repo, header->id);
2225 if (error)
2226 return error;
2227 if (got_object_tag_get_object_type(tag) !=
2228 GOT_OBJ_TYPE_COMMIT) {
2229 got_object_tag_close(tag);
2230 error = got_error(GOT_ERR_OBJ_TYPE);
2231 return error;
2233 free(header->id);
2234 header->id = got_object_id_dup(
2235 got_object_tag_get_object_id(tag));
2236 if (header->id == NULL)
2237 error = got_error_from_errno(
2238 "got_object_id_dup");
2239 got_object_tag_close(tag);
2240 if (error)
2241 return error;
2242 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2243 error = got_error(GOT_ERR_OBJ_TYPE);
2244 return error;
2246 error = got_object_open_as_commit(&header->commit,
2247 header->repo, header->id);
2248 if (error)
2249 return error;
2251 if (header->commit == NULL) {
2252 error = got_repo_match_object_id_prefix(&header->id,
2253 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2254 header->repo);
2255 if (error)
2256 return error;
2258 error = got_repo_match_object_id_prefix(&header->id,
2259 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2260 header->repo);
2263 error = got_repo_map_path(&in_repo_path, header->repo,
2264 gw_trans->repo_path, 1);
2265 if (error)
2266 return error;
2268 if (in_repo_path) {
2269 header->path = strdup(in_repo_path);
2271 free(in_repo_path);
2273 error = got_ref_list(&header->refs, header->repo, NULL,
2274 got_ref_cmp_by_name, NULL);
2275 if (error)
2276 return error;
2278 error = gw_get_commits(gw_trans, header, limit);
2279 return error;
2282 struct blame_line {
2283 int annotated;
2284 char *id_str;
2285 char *committer;
2286 char datebuf[11]; /* YYYY-MM-DD + NUL */
2289 struct gw_blame_cb_args {
2290 struct blame_line *lines;
2291 int nlines;
2292 int nlines_prec;
2293 int lineno_cur;
2294 off_t *line_offsets;
2295 FILE *f;
2296 struct got_repository *repo;
2297 struct gw_trans *gw_trans;
2298 struct buf *blamebuf;
2301 static const struct got_error *
2302 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2304 const struct got_error *err = NULL;
2305 struct gw_blame_cb_args *a = arg;
2306 struct blame_line *bline;
2307 char *line = NULL;
2308 size_t linesize = 0, newsize;
2309 struct got_commit_object *commit = NULL;
2310 off_t offset;
2311 struct tm tm;
2312 time_t committer_time;
2314 if (nlines != a->nlines ||
2315 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2316 return got_error(GOT_ERR_RANGE);
2318 if (lineno == -1)
2319 return NULL; /* no change in this commit */
2321 /* Annotate this line. */
2322 bline = &a->lines[lineno - 1];
2323 if (bline->annotated)
2324 return NULL;
2325 err = got_object_id_str(&bline->id_str, id);
2326 if (err)
2327 return err;
2329 err = got_object_open_as_commit(&commit, a->repo, id);
2330 if (err)
2331 goto done;
2333 bline->committer = strdup(got_object_commit_get_committer(commit));
2334 if (bline->committer == NULL) {
2335 err = got_error_from_errno("strdup");
2336 goto done;
2339 committer_time = got_object_commit_get_committer_time(commit);
2340 if (localtime_r(&committer_time, &tm) == NULL)
2341 return got_error_from_errno("localtime_r");
2342 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2343 &tm) >= sizeof(bline->datebuf)) {
2344 err = got_error(GOT_ERR_NO_SPACE);
2345 goto done;
2347 bline->annotated = 1;
2349 /* Print lines annotated so far. */
2350 bline = &a->lines[a->lineno_cur - 1];
2351 if (!bline->annotated)
2352 goto done;
2354 offset = a->line_offsets[a->lineno_cur - 1];
2355 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2356 err = got_error_from_errno("fseeko");
2357 goto done;
2360 while (bline->annotated) {
2361 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2362 *line_escape = NULL;
2363 size_t len;
2365 if (getline(&line, &linesize, a->f) == -1) {
2366 if (ferror(a->f))
2367 err = got_error_from_errno("getline");
2368 break;
2371 committer = bline->committer;
2372 smallerthan = strchr(committer, '<');
2373 if (smallerthan && smallerthan[1] != '\0')
2374 committer = smallerthan + 1;
2375 at = strchr(committer, '@');
2376 if (at)
2377 *at = '\0';
2378 len = strlen(committer);
2379 if (len >= 9)
2380 committer[8] = '\0';
2382 nl = strchr(line, '\n');
2383 if (nl)
2384 *nl = '\0';
2386 if (strcmp(line, "") != 0)
2387 line_escape = strdup(gw_html_escape(line));
2388 else
2389 line_escape = strdup("");
2391 if (a->gw_trans->repo_folder == NULL)
2392 a->gw_trans->repo_folder = strdup("");
2393 if (a->gw_trans->repo_folder == NULL)
2394 goto err;
2395 asprintf(&blame_row, blame_line, a->nlines_prec, a->lineno_cur,
2396 a->gw_trans->repo_name, bline->id_str,
2397 a->gw_trans->repo_file, a->gw_trans->repo_folder,
2398 bline->id_str, bline->datebuf, committer, line_escape);
2399 a->lineno_cur++;
2400 err = buf_puts(&newsize, a->blamebuf, blame_row);
2401 if (err)
2402 return err;
2404 bline = &a->lines[a->lineno_cur - 1];
2405 err:
2406 free(line_escape);
2407 free(blame_row);
2409 done:
2410 if (commit)
2411 got_object_commit_close(commit);
2412 free(line);
2413 return err;
2416 static int
2417 isbinary(const char *buf, size_t n)
2419 return (memchr(buf, '\0', n) != NULL);
2422 static char*
2423 gw_get_file_blame_blob(struct gw_trans *gw_trans)
2425 const struct got_error *error = NULL;
2426 struct got_repository *repo = NULL;
2427 struct got_object_id *obj_id = NULL;
2428 struct got_object_id *commit_id = NULL;
2429 struct got_blob_object *blob = NULL;
2430 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL;
2431 char *folder = NULL;
2432 struct gw_blame_cb_args bca;
2433 int i, obj_type;
2434 size_t filesize;
2435 enum kcgi_err kerr;
2437 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2438 if (error)
2439 goto done;
2441 if (gw_trans->repo_folder != NULL) {
2442 if (asprintf(&folder, "%s/", gw_trans->repo_folder) == -1) {
2443 error = got_error_from_errno("asprintf");
2444 goto done;
2446 } else
2447 folder = strdup("");
2449 if (asprintf(&path, "%s%s", folder, gw_trans->repo_file) == -1) {
2450 error = got_error_from_errno("asprintf");
2451 goto done;
2453 free(folder);
2455 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2456 if (error)
2457 goto done;
2459 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2460 GOT_OBJ_TYPE_COMMIT, 1, repo);
2461 if (error)
2462 goto done;
2464 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2465 if (error)
2466 goto done;
2468 if (obj_id == NULL) {
2469 error = got_error(GOT_ERR_NO_OBJ);
2470 goto done;
2473 error = got_object_get_type(&obj_type, repo, obj_id);
2474 if (error)
2475 goto done;
2477 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2478 error = got_error(GOT_ERR_OBJ_TYPE);
2479 goto done;
2482 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2483 if (error)
2484 goto done;
2486 error = buf_alloc(&bca.blamebuf, 0);
2487 if (error)
2488 goto done;
2490 bca.f = got_opentemp();
2491 if (bca.f == NULL) {
2492 error = got_error_from_errno("got_opentemp");
2493 goto done;
2495 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2496 &bca.line_offsets, bca.f, blob);
2497 if (error || bca.nlines == 0)
2498 goto done;
2500 if (gw_trans->action == GW_BLOB) {
2501 int len;
2502 size_t n;
2504 if (fseek(bca.f, 0, SEEK_END) == -1)
2505 goto done;
2506 len = ftell(bca.f) + 1;
2507 if (ferror(bca.f))
2508 goto done;
2509 if (fseek(bca.f, 0, SEEK_SET) == -1)
2510 goto done;;
2512 if ((blame_html = calloc(len, sizeof(char *))) == NULL)
2513 goto done;
2515 n = fread(blame_html, 1, len, bca.f);
2516 if (ferror(bca.f))
2517 goto done;
2518 if (n == -1) {
2519 error = got_ferror(bca.f, GOT_ERR_IO);
2520 goto done;
2523 if (isbinary(blame_html, n))
2524 gw_trans->mime = KMIME_APP_OCTET_STREAM;
2525 else
2526 gw_trans->mime = KMIME_TEXT_PLAIN;
2528 error = gw_display_index(gw_trans);
2530 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2531 kerr = khttp_write(gw_trans->gw_req, blame_html, len);
2532 if (kerr != KCGI_OK)
2533 error = gw_kcgi_error(kerr);
2535 goto done;
2538 /* Don't include \n at EOF in the blame line count. */
2539 if (bca.line_offsets[bca.nlines - 1] == filesize)
2540 bca.nlines--;
2542 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2543 if (bca.lines == NULL) {
2544 error = got_error_from_errno("calloc");
2545 goto done;
2547 bca.lineno_cur = 1;
2548 bca.nlines_prec = 0;
2549 i = bca.nlines;
2550 while (i > 0) {
2551 i /= 10;
2552 bca.nlines_prec++;
2554 bca.repo = repo;
2555 bca.gw_trans = gw_trans;
2557 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2558 NULL, NULL);
2559 if (error)
2560 goto done;
2561 if (buf_len(bca.blamebuf) > 0) {
2562 error = buf_putc(bca.blamebuf, '\0');
2563 blame_html = strdup(buf_get(bca.blamebuf));
2565 done:
2566 free(bca.line_offsets);
2567 free(bca.blamebuf);
2568 free(in_repo_path);
2569 free(commit_id);
2570 free(obj_id);
2571 free(path);
2573 if (gw_trans->action != GW_BLOB && bca.lines) {
2574 for (i = 0; i < bca.nlines; i++) {
2575 struct blame_line *bline = &bca.lines[i];
2576 free(bline->id_str);
2577 free(bline->committer);
2579 free(bca.lines);
2581 if (error)
2582 return NULL;
2583 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2584 return NULL;
2585 if (blob)
2586 error = got_object_blob_close(blob);
2587 if (error)
2588 return NULL;
2589 if (repo)
2590 error = got_repo_close(repo);
2591 if (error)
2592 return NULL;
2593 else
2594 return blame_html;
2597 static char*
2598 gw_get_repo_tree(struct gw_trans *gw_trans)
2600 const struct got_error *error = NULL;
2601 struct got_repository *repo = NULL;
2602 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2603 struct got_tree_object *tree = NULL;
2604 struct buf *diffbuf = NULL;
2605 size_t newsize;
2606 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2607 *tree_row = NULL, *id_str, *class = NULL;
2608 int nentries, i, class_flip = 0;
2610 error = buf_alloc(&diffbuf, 0);
2611 if (error)
2612 return NULL;
2614 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2615 if (error)
2616 goto done;
2618 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2619 if (error)
2620 goto done;
2622 if (gw_trans->repo_folder != NULL)
2623 path = strdup(gw_trans->repo_folder);
2624 else if (in_repo_path) {
2625 free(path);
2626 path = in_repo_path;
2629 if (gw_trans->commit == NULL) {
2630 struct got_reference *head_ref;
2631 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2632 if (error)
2633 goto done;
2635 error = got_ref_resolve(&commit_id, repo, head_ref);
2636 got_ref_close(head_ref);
2638 } else
2639 error = got_repo_match_object_id(&commit_id, NULL,
2640 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2641 if (error)
2642 goto done;
2644 error = got_object_id_str(&gw_trans->commit, commit_id);
2645 if (error)
2646 goto done;
2648 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2649 if (error)
2650 goto done;
2652 error = got_object_open_as_tree(&tree, repo, tree_id);
2653 if (error)
2654 goto done;
2656 nentries = got_object_tree_get_nentries(tree);
2658 for (i = 0; i < nentries; i++) {
2659 struct got_tree_entry *te;
2660 const char *modestr = "";
2661 char *id = NULL, *url_html = NULL;
2663 te = got_object_tree_get_entry(tree, i);
2665 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2666 if (error)
2667 goto done;
2669 if (asprintf(&id, "%s", id_str) == -1) {
2670 error = got_error_from_errno("asprintf");
2671 free(id_str);
2672 goto done;
2675 mode_t mode = got_tree_entry_get_mode(te);
2677 if (got_object_tree_entry_is_submodule(te))
2678 modestr = "$";
2679 else if (S_ISLNK(mode))
2680 modestr = "@";
2681 else if (S_ISDIR(mode))
2682 modestr = "/";
2683 else if (mode & S_IXUSR)
2684 modestr = "*";
2686 if (class_flip == 0) {
2687 class = strdup("back_lightgray");
2688 class_flip = 1;
2689 } else {
2690 class = strdup("back_white");
2691 class_flip = 0;
2694 char *build_folder = NULL;
2695 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2696 if (gw_trans->repo_folder != NULL) {
2697 if (asprintf(&build_folder, "%s/%s",
2698 gw_trans->repo_folder,
2699 got_tree_entry_get_name(te)) == -1) {
2700 error =
2701 got_error_from_errno("asprintf");
2702 goto done;
2704 } else {
2705 if (asprintf(&build_folder, "/%s",
2706 got_tree_entry_get_name(te)) == -1)
2707 goto done;
2710 if (asprintf(&url_html, folder_html,
2711 gw_trans->repo_name, gw_trans->action_name,
2712 gw_trans->commit, build_folder,
2713 got_tree_entry_get_name(te), modestr) == -1) {
2714 error = got_error_from_errno("asprintf");
2715 goto done;
2718 if (asprintf(&tree_row, tree_line, class, url_html,
2719 class) == -1) {
2720 error = got_error_from_errno("asprintf");
2721 goto done;
2724 } else {
2725 if (gw_trans->repo_folder != NULL) {
2726 if (asprintf(&build_folder, "%s",
2727 gw_trans->repo_folder) == -1) {
2728 error =
2729 got_error_from_errno("asprintf");
2730 goto done;
2732 } else
2733 build_folder = strdup("");
2735 if (asprintf(&url_html, file_html, gw_trans->repo_name,
2736 "blob", gw_trans->commit,
2737 got_tree_entry_get_name(te), build_folder,
2738 got_tree_entry_get_name(te), modestr) == -1) {
2739 error = got_error_from_errno("asprintf");
2740 goto done;
2743 if (asprintf(&tree_row, tree_line_with_navs, class,
2744 url_html, class, gw_trans->repo_name, "blob",
2745 gw_trans->commit, got_tree_entry_get_name(te),
2746 build_folder, "blob", gw_trans->repo_name,
2747 "blame", gw_trans->commit,
2748 got_tree_entry_get_name(te), build_folder,
2749 "blame") == -1) {
2750 error = got_error_from_errno("asprintf");
2751 goto done;
2754 free(build_folder);
2756 if (error)
2757 goto done;
2759 error = buf_puts(&newsize, diffbuf, tree_row);
2760 if (error)
2761 goto done;
2763 free(id);
2764 free(id_str);
2765 free(url_html);
2766 free(tree_row);
2769 if (buf_len(diffbuf) > 0) {
2770 error = buf_putc(diffbuf, '\0');
2771 tree_html = strdup(buf_get(diffbuf));
2773 done:
2774 if (tree)
2775 got_object_tree_close(tree);
2776 if (repo)
2777 got_repo_close(repo);
2779 free(in_repo_path);
2780 free(tree_id);
2781 free(diffbuf);
2782 if (error)
2783 return NULL;
2784 else
2785 return tree_html;
2788 static char *
2789 gw_get_repo_heads(struct gw_trans *gw_trans)
2791 const struct got_error *error = NULL;
2792 struct got_repository *repo = NULL;
2793 struct got_reflist_head refs;
2794 struct got_reflist_entry *re;
2795 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2796 struct buf *diffbuf = NULL;
2797 size_t newsize;
2799 SIMPLEQ_INIT(&refs);
2801 error = buf_alloc(&diffbuf, 0);
2802 if (error)
2803 return NULL;
2805 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2806 if (error)
2807 goto done;
2809 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2810 NULL);
2811 if (error)
2812 goto done;
2814 SIMPLEQ_FOREACH(re, &refs, entry) {
2815 char *refname;
2817 refname = strdup(got_ref_get_name(re->ref));
2818 if (refname == NULL) {
2819 error = got_error_from_errno("got_ref_to_str");
2820 goto done;
2823 if (strncmp(refname, "refs/heads/", 11) != 0) {
2824 free(refname);
2825 continue;
2828 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
2829 refname, TM_DIFF);
2830 if (error)
2831 goto done;
2833 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2834 refname, gw_trans->repo_name, refname,
2835 gw_trans->repo_name, refname, gw_trans->repo_name,
2836 refname) == -1) {
2837 error = got_error_from_errno("asprintf");
2838 goto done;
2841 if (strncmp(refname, "refs/heads/", 11) == 0)
2842 refname += 11;
2844 if (asprintf(&head_row, heads_row, age, refname,
2845 head_navs_disp) == -1) {
2846 error = got_error_from_errno("asprintf");
2847 goto done;
2850 error = buf_puts(&newsize, diffbuf, head_row);
2852 free(head_navs_disp);
2853 free(head_row);
2856 if (buf_len(diffbuf) > 0) {
2857 error = buf_putc(diffbuf, '\0');
2858 heads = strdup(buf_get(diffbuf));
2860 done:
2861 buf_free(diffbuf);
2862 got_ref_list_free(&refs);
2863 if (repo)
2864 got_repo_close(repo);
2865 if (error)
2866 return NULL;
2867 else
2868 return heads;
2871 static char *
2872 gw_get_got_link(struct gw_trans *gw_trans)
2874 char *link;
2876 if (asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2877 gw_trans->gw_conf->got_logo) == -1)
2878 return NULL;
2880 return link;
2883 static char *
2884 gw_get_site_link(struct gw_trans *gw_trans)
2886 char *link, *repo = "", *action = "";
2888 if (gw_trans->repo_name != NULL)
2889 if (asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2890 "</a>", gw_trans->repo_name, gw_trans->repo_name) == -1)
2891 return NULL;
2893 if (gw_trans->action_name != NULL)
2894 if (asprintf(&action, " / %s", gw_trans->action_name) == -1)
2895 return NULL;
2897 if (asprintf(&link, site_link, GOTWEB,
2898 gw_trans->gw_conf->got_site_link, repo, action) == -1)
2899 return NULL;
2901 return link;
2904 static char *
2905 gw_colordiff_line(char *buf)
2907 const struct got_error *error = NULL;
2908 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2909 struct buf *diffbuf = NULL;
2910 size_t newsize;
2912 error = buf_alloc(&diffbuf, 0);
2913 if (error)
2914 return NULL;
2916 if (buf == NULL)
2917 return NULL;
2918 if (strncmp(buf, "-", 1) == 0)
2919 color = "diff_minus";
2920 if (strncmp(buf, "+", 1) == 0)
2921 color = "diff_plus";
2922 if (strncmp(buf, "@@", 2) == 0)
2923 color = "diff_chunk_header";
2924 if (strncmp(buf, "@@", 2) == 0)
2925 color = "diff_chunk_header";
2926 if (strncmp(buf, "commit +", 8) == 0)
2927 color = "diff_meta";
2928 if (strncmp(buf, "commit -", 8) == 0)
2929 color = "diff_meta";
2930 if (strncmp(buf, "blob +", 6) == 0)
2931 color = "diff_meta";
2932 if (strncmp(buf, "blob -", 6) == 0)
2933 color = "diff_meta";
2934 if (strncmp(buf, "file +", 6) == 0)
2935 color = "diff_meta";
2936 if (strncmp(buf, "file -", 6) == 0)
2937 color = "diff_meta";
2938 if (strncmp(buf, "from:", 5) == 0)
2939 color = "diff_author";
2940 if (strncmp(buf, "via:", 4) == 0)
2941 color = "diff_author";
2942 if (strncmp(buf, "date:", 5) == 0)
2943 color = "diff_date";
2945 if (asprintf(&div_diff_line_div, div_diff_line, color) == -1)
2946 return NULL;
2948 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2949 if (error)
2950 return NULL;
2952 error = buf_puts(&newsize, diffbuf, buf);
2953 if (error)
2954 return NULL;
2956 if (buf_len(diffbuf) > 0) {
2957 error = buf_putc(diffbuf, '\0');
2958 colorized_line = strdup(buf_get(diffbuf));
2961 free(diffbuf);
2962 free(div_diff_line_div);
2963 return colorized_line;
2966 static char *
2967 gw_html_escape(const char *html)
2969 char *escaped_str = NULL, *buf;
2970 char c[1];
2971 size_t sz, i, buff_sz = 2048;
2973 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2974 return NULL;
2976 if (html == NULL)
2977 return NULL;
2978 else
2979 if ((sz = strlen(html)) == 0)
2980 return NULL;
2982 /* only work with buff_sz */
2983 if (buff_sz < sz)
2984 sz = buff_sz;
2986 for (i = 0; i < sz; i++) {
2987 c[0] = html[i];
2988 switch (c[0]) {
2989 case ('>'):
2990 strcat(buf, "&gt;");
2991 break;
2992 case ('&'):
2993 strcat(buf, "&amp;");
2994 break;
2995 case ('<'):
2996 strcat(buf, "&lt;");
2997 break;
2998 case ('"'):
2999 strcat(buf, "&quot;");
3000 break;
3001 case ('\''):
3002 strcat(buf, "&apos;");
3003 break;
3004 case ('\n'):
3005 strcat(buf, "<br />");
3006 default:
3007 strcat(buf, &c[0]);
3008 break;
3011 asprintf(&escaped_str, "%s", buf);
3012 free(buf);
3013 return escaped_str;
3016 int
3017 main(int argc, char *argv[])
3019 const struct got_error *error = NULL;
3020 struct gw_trans *gw_trans;
3021 struct gw_dir *dir = NULL, *tdir;
3022 const char *page = "index";
3023 int gw_malloc = 1;
3024 enum kcgi_err kerr;
3026 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3027 errx(1, "malloc");
3029 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3030 errx(1, "malloc");
3032 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3033 errx(1, "malloc");
3035 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3036 errx(1, "malloc");
3038 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3039 if (kerr != KCGI_OK) {
3040 error = gw_kcgi_error(kerr);
3041 goto done;
3044 if ((gw_trans->gw_conf =
3045 malloc(sizeof(struct gotweb_conf))) == NULL) {
3046 gw_malloc = 0;
3047 error = got_error_from_errno("malloc");
3048 goto done;
3051 TAILQ_INIT(&gw_trans->gw_dirs);
3052 TAILQ_INIT(&gw_trans->gw_headers);
3054 gw_trans->page = 0;
3055 gw_trans->repos_total = 0;
3056 gw_trans->repo_path = NULL;
3057 gw_trans->commit = NULL;
3058 gw_trans->headref = strdup(GOT_REF_HEAD);
3059 gw_trans->mime = KMIME_TEXT_HTML;
3060 gw_trans->gw_tmpl->key = gw_templs;
3061 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3062 gw_trans->gw_tmpl->arg = gw_trans;
3063 gw_trans->gw_tmpl->cb = gw_template;
3064 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3065 if (error)
3066 goto done;
3068 error = gw_parse_querystring(gw_trans);
3069 if (error)
3070 goto done;
3072 if (gw_trans->action == GW_BLOB)
3073 error = gw_blob(gw_trans);
3074 else
3075 error = gw_display_index(gw_trans);
3076 done:
3077 if (error) {
3078 gw_trans->mime = KMIME_TEXT_PLAIN;
3079 gw_trans->action = GW_ERR;
3080 gw_display_error(gw_trans, error);
3082 if (gw_malloc) {
3083 free(gw_trans->gw_conf->got_repos_path);
3084 free(gw_trans->gw_conf->got_site_name);
3085 free(gw_trans->gw_conf->got_site_owner);
3086 free(gw_trans->gw_conf->got_site_link);
3087 free(gw_trans->gw_conf->got_logo);
3088 free(gw_trans->gw_conf->got_logo_url);
3089 free(gw_trans->gw_conf);
3090 free(gw_trans->commit);
3091 free(gw_trans->repo_path);
3092 free(gw_trans->repo_name);
3093 free(gw_trans->repo_file);
3094 free(gw_trans->action_name);
3095 free(gw_trans->headref);
3097 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3098 free(dir->name);
3099 free(dir->description);
3100 free(dir->age);
3101 free(dir->url);
3102 free(dir->path);
3103 free(dir);
3108 khttp_free(gw_trans->gw_req);
3109 return 0;