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 char *gw_get_repo_description(struct gw_trans *,
162 char *);
163 static char *gw_get_repo_owner(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 char *gw_get_clone_url(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_blame(struct gw_trans *gw_trans)
311 const struct got_error *error = NULL;
312 struct gw_header *header = NULL;
313 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
314 enum kcgi_err kerr;
316 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
317 NULL) == -1)
318 return got_error_from_errno("pledge");
320 if ((header = gw_init_header()) == NULL)
321 return got_error_from_errno("malloc");
323 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
324 if (error)
325 goto done;
327 error = gw_get_header(gw_trans, header, 1);
328 if (error)
329 goto done;
331 blame_html = gw_get_file_blame_blob(gw_trans);
333 if (blame_html == NULL) {
334 blame_html = strdup("");
335 if (blame_html == NULL) {
336 error = got_error_from_errno("strdup");
337 goto done;
341 if (asprintf(&blame_html_disp, blame_header,
342 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
343 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
344 blame_html) == -1) {
345 error = got_error_from_errno("asprintf");
346 goto done;
349 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
350 error = got_error_from_errno("asprintf");
351 goto done;
354 kerr = khttp_puts(gw_trans->gw_req, blame);
355 if (kerr != KCGI_OK)
356 error = gw_kcgi_error(kerr);
357 done:
358 got_ref_list_free(&header->refs);
359 gw_free_headers(header);
360 free(blame_html_disp);
361 free(blame_html);
362 free(blame);
363 return error;
366 static const struct got_error *
367 gw_blob(struct gw_trans *gw_trans)
369 const struct got_error *error = NULL;
370 struct gw_header *header = NULL;
371 char *blob = NULL;
372 enum kcgi_err kerr;
374 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
375 NULL) == -1)
376 return got_error_from_errno("pledge");
378 if ((header = gw_init_header()) == NULL)
379 return got_error_from_errno("malloc");
381 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
382 if (error)
383 goto done;
385 error = gw_get_header(gw_trans, header, 1);
386 if (error)
387 goto done;
389 blob = gw_get_file_blame_blob(gw_trans);
391 if (blob == NULL) {
392 blob = strdup("");
393 if (blob == NULL) {
394 error = got_error_from_errno("strdup");
395 goto done;
399 if (gw_trans->mime == KMIME_APP_OCTET_STREAM)
400 goto done;
401 else {
402 kerr = khttp_puts(gw_trans->gw_req, blob);
403 if (kerr != KCGI_OK)
404 error = gw_kcgi_error(kerr);
406 done:
407 got_ref_list_free(&header->refs);
408 gw_free_headers(header);
409 free(blob);
410 return error;
413 static const struct got_error *
414 gw_diff(struct gw_trans *gw_trans)
416 const struct got_error *error = NULL;
417 struct gw_header *header = NULL;
418 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
419 enum kcgi_err kerr;
421 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
422 NULL) == -1)
423 return got_error_from_errno("pledge");
425 if ((header = gw_init_header()) == NULL)
426 return got_error_from_errno("malloc");
428 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
429 if (error)
430 goto done;
432 error = gw_get_header(gw_trans, header, 1);
433 if (error)
434 goto done;
436 diff_html = gw_get_diff(gw_trans, header);
438 if (diff_html == NULL) {
439 diff_html = strdup("");
440 if (diff_html == NULL) {
441 error = got_error_from_errno("strdup");
442 goto done;
446 if (asprintf(&diff_html_disp, diff_header,
447 gw_gen_diff_header(header->parent_id, header->commit_id),
448 gw_gen_commit_header(header->commit_id, header->refs_str),
449 gw_gen_tree_header(header->tree_id),
450 gw_gen_author_header(header->author),
451 gw_gen_committer_header(header->committer),
452 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
453 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
454 diff_html) == -1) {
455 error = got_error_from_errno("asprintf");
456 goto done;
459 if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
460 error = got_error_from_errno("asprintf");
461 goto done;
464 kerr = khttp_puts(gw_trans->gw_req, diff);
465 if (kerr != KCGI_OK)
466 error = gw_kcgi_error(kerr);
467 done:
468 got_ref_list_free(&header->refs);
469 gw_free_headers(header);
470 free(diff_html_disp);
471 free(diff_html);
472 free(diff);
473 return error;
476 static const struct got_error *
477 gw_index(struct gw_trans *gw_trans)
479 const struct got_error *error = NULL;
480 struct gw_dir *gw_dir = NULL;
481 char *html, *navs, *next, *prev;
482 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
483 enum kcgi_err kerr;
485 if (pledge("stdio rpath proc exec sendfd unveil",
486 NULL) == -1) {
487 error = got_error_from_errno("pledge");
488 return error;
491 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
492 if (error)
493 return error;
495 error = gw_load_got_paths(gw_trans);
496 if (error)
497 return error;
499 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
500 if (kerr != KCGI_OK)
501 return gw_kcgi_error(kerr);
503 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
504 if (asprintf(&html, index_projects_empty,
505 gw_trans->gw_conf->got_repos_path) == -1)
506 return got_error_from_errno("asprintf");
507 kerr = khttp_puts(gw_trans->gw_req, html);
508 if (kerr != KCGI_OK)
509 error = gw_kcgi_error(kerr);
510 free(html);
511 return error;
514 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
515 dir_c++;
517 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
518 if (gw_trans->page > 0 && (gw_trans->page *
519 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
520 prev_disp++;
521 continue;
524 prev_disp++;
526 if (error)
527 return error;
528 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
529 gw_dir->name, gw_dir->name) == -1)
530 return got_error_from_errno("asprintf");
532 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
533 gw_dir->description, gw_dir->owner, gw_dir->age,
534 navs) == -1)
535 return got_error_from_errno("asprintf");
537 kerr = khttp_puts(gw_trans->gw_req, html);
538 free(navs);
539 free(html);
540 if (kerr != KCGI_OK)
541 return gw_kcgi_error(kerr);
543 if (gw_trans->gw_conf->got_max_repos_display == 0)
544 continue;
546 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
547 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
548 if (kerr != KCGI_OK)
549 return gw_kcgi_error(kerr);
550 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
551 (gw_trans->page > 0) &&
552 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
553 prev_disp == gw_trans->repos_total)) {
554 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
555 if (kerr != KCGI_OK)
556 return gw_kcgi_error(kerr);
559 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
560 (gw_trans->page > 0) &&
561 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
562 prev_disp == gw_trans->repos_total)) {
563 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
564 return got_error_from_errno("asprintf");
565 kerr = khttp_puts(gw_trans->gw_req, prev);
566 free(prev);
567 if (kerr != KCGI_OK)
568 return gw_kcgi_error(kerr);
571 kerr = khttp_puts(gw_trans->gw_req, div_end);
572 if (kerr != KCGI_OK)
573 return gw_kcgi_error(kerr);
575 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
576 next_disp == gw_trans->gw_conf->got_max_repos_display &&
577 dir_c != (gw_trans->page + 1) *
578 gw_trans->gw_conf->got_max_repos_display) {
579 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
580 return got_error_from_errno("calloc");
581 kerr = khttp_puts(gw_trans->gw_req, next);
582 free(next);
583 if (kerr != KCGI_OK)
584 return gw_kcgi_error(kerr);
585 kerr = khttp_puts(gw_trans->gw_req, div_end);
586 if (kerr != KCGI_OK)
587 error = gw_kcgi_error(kerr);
588 next_disp = 0;
589 break;
592 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
593 (gw_trans->page > 0) &&
594 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
595 prev_disp == gw_trans->repos_total)) {
596 kerr = khttp_puts(gw_trans->gw_req, div_end);
597 if (kerr != KCGI_OK)
598 return gw_kcgi_error(kerr);
601 next_disp++;
603 return error;
606 static const struct got_error *
607 gw_commits(struct gw_trans *gw_trans)
609 const struct got_error *error = NULL;
610 char *commits_html, *commits_navs_html;
611 struct gw_header *header = NULL, *n_header = NULL;
612 enum kcgi_err kerr;
614 if ((header = gw_init_header()) == NULL)
615 return got_error_from_errno("malloc");
617 if (pledge("stdio rpath proc exec sendfd unveil",
618 NULL) == -1) {
619 error = got_error_from_errno("pledge");
620 goto done;
623 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
624 if (error)
625 goto done;
627 error = gw_get_header(gw_trans, header,
628 gw_trans->gw_conf->got_max_commits_display);
629 if (error)
630 goto done;
632 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
633 if (kerr != KCGI_OK) {
634 error = gw_kcgi_error(kerr);
635 goto done;
637 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
638 if (asprintf(&commits_navs_html, commits_navs,
639 gw_trans->repo_name, n_header->commit_id,
640 gw_trans->repo_name, n_header->commit_id,
641 gw_trans->repo_name, n_header->commit_id) == -1) {
642 error = got_error_from_errno("asprintf");
643 goto done;
646 if (asprintf(&commits_html, commits_line,
647 gw_gen_commit_header(n_header->commit_id,
648 n_header->refs_str),
649 gw_gen_author_header(n_header->author),
650 gw_gen_committer_header(n_header->committer),
651 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
652 TM_LONG)), gw_html_escape(n_header->commit_msg),
653 commits_navs_html) == -1) {
654 error = got_error_from_errno("asprintf");
655 goto done;
657 kerr = khttp_puts(gw_trans->gw_req, commits_html);
658 if (kerr != KCGI_OK) {
659 error = gw_kcgi_error(kerr);
660 goto done;
663 kerr = khttp_puts(gw_trans->gw_req, div_end);
664 if (kerr != KCGI_OK)
665 error = gw_kcgi_error(kerr);
666 done:
667 got_ref_list_free(&header->refs);
668 gw_free_headers(header);
669 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
670 gw_free_headers(n_header);
671 return error;
674 static const struct got_error *
675 gw_briefs(struct gw_trans *gw_trans)
677 const struct got_error *error = NULL;
678 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
679 struct gw_header *header = NULL, *n_header = NULL;
680 enum kcgi_err kerr;
682 if ((header = gw_init_header()) == NULL)
683 return got_error_from_errno("malloc");
685 if (pledge("stdio rpath proc exec sendfd unveil",
686 NULL) == -1) {
687 error = got_error_from_errno("pledge");
688 goto done;
691 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
692 if (error)
693 goto done;
695 if (gw_trans->action == GW_SUMMARY)
696 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
697 else
698 error = gw_get_header(gw_trans, header,
699 gw_trans->gw_conf->got_max_commits_display);
700 if (error)
701 goto done;
703 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
704 if (kerr != KCGI_OK) {
705 error = gw_kcgi_error(kerr);
706 goto done;
709 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
710 if (asprintf(&briefs_navs_html, briefs_navs,
711 gw_trans->repo_name, n_header->commit_id,
712 gw_trans->repo_name, n_header->commit_id,
713 gw_trans->repo_name, n_header->commit_id) == -1) {
714 error = got_error_from_errno("asprintf");
715 goto done;
717 newline = strchr(n_header->commit_msg, '\n');
718 if (newline)
719 *newline = '\0';
720 if (asprintf(&briefs_html, briefs_line,
721 gw_get_time_str(n_header->committer_time, TM_DIFF),
722 n_header->author, gw_html_escape(n_header->commit_msg),
723 briefs_navs_html) == -1) {
724 error = got_error_from_errno("asprintf");
725 goto done;
727 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
728 if (kerr != KCGI_OK) {
729 error = gw_kcgi_error(kerr);
730 goto done;
733 kerr = khttp_puts(gw_trans->gw_req, div_end);
734 if (kerr != KCGI_OK)
735 error = gw_kcgi_error(kerr);
736 done:
737 got_ref_list_free(&header->refs);
738 gw_free_headers(header);
739 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
740 gw_free_headers(n_header);
741 return error;
744 static const struct got_error *
745 gw_summary(struct gw_trans *gw_trans)
747 const struct got_error *error = NULL;
748 char *description_html = NULL, *repo_owner_html = NULL;
749 char *age = NULL, *repo_age_html = NULL, *cloneurl_html = NULL;
750 char *tags = NULL, *tags_html = NULL;
751 char *heads = NULL, *heads_html = NULL;
752 enum kcgi_err kerr;
754 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
755 return got_error_from_errno("pledge");
757 /* unveil is applied with gw_briefs below */
759 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
760 if (kerr != KCGI_OK)
761 return gw_kcgi_error(kerr);
763 if (gw_trans->gw_conf->got_show_repo_description) {
764 if (gw_trans->gw_dir->description != NULL &&
765 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
766 if (asprintf(&description_html, description,
767 gw_trans->gw_dir->description) == -1) {
768 error = got_error_from_errno("asprintf");
769 goto done;
772 kerr = khttp_puts(gw_trans->gw_req, description_html);
773 if (kerr != KCGI_OK) {
774 error = gw_kcgi_error(kerr);
775 goto done;
780 if (gw_trans->gw_conf->got_show_repo_owner) {
781 if (gw_trans->gw_dir->owner != NULL &&
782 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
783 if (asprintf(&repo_owner_html, repo_owner,
784 gw_trans->gw_dir->owner) == -1) {
785 error = got_error_from_errno("asprintf");
786 goto done;
789 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
790 if (kerr != KCGI_OK) {
791 error = gw_kcgi_error(kerr);
792 goto done;
797 if (gw_trans->gw_conf->got_show_repo_age) {
798 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
799 "refs/heads", TM_LONG);
800 if (error)
801 goto done;
802 if (strcmp(age, "") != 0) {
803 if (asprintf(&repo_age_html, last_change, age) == -1) {
804 error = got_error_from_errno("asprintf");
805 goto done;
808 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
809 if (kerr != KCGI_OK) {
810 error = gw_kcgi_error(kerr);
811 goto done;
816 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
817 if (gw_trans->gw_dir->url != NULL &&
818 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
819 if (asprintf(&cloneurl_html, cloneurl,
820 gw_trans->gw_dir->url) == -1) {
821 error = got_error_from_errno("asprintf");
822 goto done;
825 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
826 if (kerr != KCGI_OK) {
827 error = gw_kcgi_error(kerr);
828 goto done;
832 kerr = khttp_puts(gw_trans->gw_req, div_end);
833 if (kerr != KCGI_OK) {
834 error = gw_kcgi_error(kerr);
835 goto done;
838 error = gw_briefs(gw_trans);
839 if (error)
840 goto done;
842 tags = gw_get_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP, TAGBRIEF);
843 heads = gw_get_repo_heads(gw_trans);
845 if (tags != NULL && strcmp(tags, "") != 0) {
846 if (asprintf(&tags_html, summary_tags, tags) == -1) {
847 error = got_error_from_errno("asprintf");
848 goto done;
850 kerr = khttp_puts(gw_trans->gw_req, tags_html);
851 if (kerr != KCGI_OK) {
852 error = gw_kcgi_error(kerr);
853 goto done;
857 if (heads != NULL && strcmp(heads, "") != 0) {
858 if (asprintf(&heads_html, summary_heads, heads) == -1) {
859 error = got_error_from_errno("asprintf");
860 goto done;
862 kerr = khttp_puts(gw_trans->gw_req, heads_html);
863 if (kerr != KCGI_OK) {
864 error = gw_kcgi_error(kerr);
865 goto done;
868 done:
869 free(description_html);
870 free(repo_owner_html);
871 free(age);
872 free(repo_age_html);
873 free(cloneurl_html);
874 free(tags);
875 free(tags_html);
876 free(heads);
877 free(heads_html);
878 return error;
881 static const struct got_error *
882 gw_tree(struct gw_trans *gw_trans)
884 const struct got_error *error = NULL;
885 struct gw_header *header = NULL;
886 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
887 enum kcgi_err kerr;
889 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
890 return got_error_from_errno("pledge");
892 if ((header = gw_init_header()) == NULL)
893 return got_error_from_errno("malloc");
895 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
896 if (error)
897 goto done;
899 error = gw_get_header(gw_trans, header, 1);
900 if (error)
901 goto done;
903 tree_html = gw_get_repo_tree(gw_trans);
905 if (tree_html == NULL) {
906 tree_html = strdup("");
907 if (tree_html == NULL) {
908 error = got_error_from_errno("strdup");
909 goto done;
913 if (asprintf(&tree_html_disp, tree_header,
914 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
915 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
916 tree_html) == -1) {
917 error = got_error_from_errno("asprintf");
918 goto done;
921 if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
922 error = got_error_from_errno("asprintf");
923 goto done;
926 kerr = khttp_puts(gw_trans->gw_req, tree);
927 if (kerr != KCGI_OK)
928 error = gw_kcgi_error(kerr);
929 done:
930 got_ref_list_free(&header->refs);
931 gw_free_headers(header);
932 free(tree_html_disp);
933 free(tree_html);
934 free(tree);
935 return error;
938 static const struct got_error *
939 gw_tag(struct gw_trans *gw_trans)
941 const struct got_error *error = NULL;
942 struct gw_header *header = NULL;
943 char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
944 enum kcgi_err kerr;
946 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
947 return got_error_from_errno("pledge");
949 if ((header = gw_init_header()) == NULL)
950 return got_error_from_errno("malloc");
952 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
953 if (error)
954 goto done;
956 error = gw_get_header(gw_trans, header, 1);
957 if (error)
958 goto done;
960 tag_html = gw_get_repo_tags(gw_trans, header, 1, TAGFULL);
961 if (tag_html == NULL) {
962 tag_html = strdup("");
963 if (tag_html == NULL) {
964 error = got_error_from_errno("strdup");
965 goto done;
969 if (asprintf(&tag_html_disp, tag_header,
970 gw_gen_commit_header(header->commit_id, header->refs_str),
971 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
972 tag_html) == -1) {
973 error = got_error_from_errno("asprintf");
974 goto done;
977 if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
978 error = got_error_from_errno("asprintf");
979 goto done;
982 kerr = khttp_puts(gw_trans->gw_req, tag);
983 if (kerr != KCGI_OK)
984 error = gw_kcgi_error(kerr);
985 done:
986 got_ref_list_free(&header->refs);
987 gw_free_headers(header);
988 free(tag_html_disp);
989 free(tag_html);
990 free(tag);
991 return error;
994 static const struct got_error *
995 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
997 const struct got_error *error = NULL;
998 DIR *dt;
999 char *dir_test;
1000 int opened = 0;
1002 if (asprintf(&dir_test, "%s/%s/%s",
1003 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1004 GOTWEB_GIT_DIR) == -1)
1005 return got_error_from_errno("asprintf");
1007 dt = opendir(dir_test);
1008 if (dt == NULL) {
1009 free(dir_test);
1010 } else {
1011 gw_dir->path = strdup(dir_test);
1012 opened = 1;
1013 goto done;
1016 if (asprintf(&dir_test, "%s/%s/%s",
1017 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1018 GOTWEB_GOT_DIR) == -1)
1019 return got_error_from_errno("asprintf");
1021 dt = opendir(dir_test);
1022 if (dt == NULL)
1023 free(dir_test);
1024 else {
1025 opened = 1;
1026 error = got_error(GOT_ERR_NOT_GIT_REPO);
1027 goto errored;
1030 if (asprintf(&dir_test, "%s/%s",
1031 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1032 return got_error_from_errno("asprintf");
1034 gw_dir->path = strdup(dir_test);
1036 done:
1037 gw_dir->description = gw_get_repo_description(gw_trans,
1038 gw_dir->path);
1039 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
1040 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1041 "refs/heads", TM_DIFF);
1042 if (error)
1043 goto errored;
1044 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
1046 errored:
1047 free(dir_test);
1048 if (opened)
1049 closedir(dt);
1050 return error;
1053 static const struct got_error *
1054 gw_load_got_paths(struct gw_trans *gw_trans)
1056 const struct got_error *error = NULL;
1057 DIR *d;
1058 struct dirent **sd_dent;
1059 struct gw_dir *gw_dir;
1060 struct stat st;
1061 unsigned int d_cnt, d_i;
1063 d = opendir(gw_trans->gw_conf->got_repos_path);
1064 if (d == NULL) {
1065 error = got_error_from_errno2("opendir",
1066 gw_trans->gw_conf->got_repos_path);
1067 return error;
1070 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1071 alphasort);
1072 if (d_cnt == -1) {
1073 error = got_error_from_errno2("scandir",
1074 gw_trans->gw_conf->got_repos_path);
1075 return error;
1078 for (d_i = 0; d_i < d_cnt; d_i++) {
1079 if (gw_trans->gw_conf->got_max_repos > 0 &&
1080 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1081 break; /* account for parent and self */
1083 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1084 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1085 continue;
1087 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1088 return got_error_from_errno("gw_dir malloc");
1090 error = gw_load_got_path(gw_trans, gw_dir);
1091 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1092 continue;
1093 else if (error)
1094 return error;
1096 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1097 !got_path_dir_is_empty(gw_dir->path)) {
1098 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1099 entry);
1100 gw_trans->repos_total++;
1104 closedir(d);
1105 return error;
1108 static const struct got_error *
1109 gw_parse_querystring(struct gw_trans *gw_trans)
1111 const struct got_error *error = NULL;
1112 struct kpair *p;
1113 struct gw_query_action *action = NULL;
1114 unsigned int i;
1116 if (gw_trans->gw_req->fieldnmap[0]) {
1117 error = got_error_from_errno("bad parse");
1118 return error;
1119 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1120 /* define gw_trans->repo_path */
1121 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1122 return got_error_from_errno("asprintf");
1124 if (asprintf(&gw_trans->repo_path, "%s/%s",
1125 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1126 return got_error_from_errno("asprintf");
1128 /* get action and set function */
1129 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1130 for (i = 0; i < nitems(gw_query_funcs); i++) {
1131 action = &gw_query_funcs[i];
1132 if (action->func_name == NULL)
1133 continue;
1135 if (strcmp(action->func_name,
1136 p->parsed.s) == 0) {
1137 gw_trans->action = i;
1138 if (asprintf(&gw_trans->action_name,
1139 "%s", action->func_name) == -1)
1140 return
1141 got_error_from_errno(
1142 "asprintf");
1144 break;
1147 action = NULL;
1150 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1151 if (asprintf(&gw_trans->commit, "%s",
1152 p->parsed.s) == -1)
1153 return got_error_from_errno("asprintf");
1155 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1156 if (asprintf(&gw_trans->repo_file, "%s",
1157 p->parsed.s) == -1)
1158 return got_error_from_errno("asprintf");
1160 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1161 if (asprintf(&gw_trans->repo_folder, "%s",
1162 p->parsed.s) == -1)
1163 return got_error_from_errno("asprintf");
1165 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1166 if (asprintf(&gw_trans->headref, "%s",
1167 p->parsed.s) == -1)
1168 return got_error_from_errno("asprintf");
1170 if (action == NULL) {
1171 error = got_error_from_errno("invalid action");
1172 return error;
1174 if ((gw_trans->gw_dir =
1175 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1176 return got_error_from_errno("gw_dir malloc");
1178 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1179 if (error)
1180 return error;
1181 } else
1182 gw_trans->action = GW_INDEX;
1184 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1185 gw_trans->page = p->parsed.i;
1187 return error;
1190 static struct gw_dir *
1191 gw_init_gw_dir(char *dir)
1193 struct gw_dir *gw_dir;
1195 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1196 return NULL;
1198 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1199 return NULL;
1201 return gw_dir;
1204 static const struct got_error *
1205 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1207 enum kcgi_err kerr;
1209 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1210 if (kerr != KCGI_OK)
1211 return gw_kcgi_error(kerr);
1212 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1213 khttps[code]);
1214 if (kerr != KCGI_OK)
1215 return gw_kcgi_error(kerr);
1216 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1217 kmimetypes[mime]);
1218 if (kerr != KCGI_OK)
1219 return gw_kcgi_error(kerr);
1220 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1221 "nosniff");
1222 if (kerr != KCGI_OK)
1223 return gw_kcgi_error(kerr);
1224 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1225 if (kerr != KCGI_OK)
1226 return gw_kcgi_error(kerr);
1227 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1228 "1; mode=block");
1229 if (kerr != KCGI_OK)
1230 return gw_kcgi_error(kerr);
1232 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1233 kerr = khttp_head(gw_trans->gw_req,
1234 kresps[KRESP_CONTENT_DISPOSITION],
1235 "attachment; filename=%s", gw_trans->repo_file);
1236 if (kerr != KCGI_OK)
1237 return gw_kcgi_error(kerr);
1240 kerr = khttp_body(gw_trans->gw_req);
1241 return gw_kcgi_error(kerr);
1244 static const struct got_error *
1245 gw_display_index(struct gw_trans *gw_trans)
1247 const struct got_error *error;
1248 enum kcgi_err kerr;
1250 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1251 if (error)
1252 return error;
1254 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1255 if (kerr)
1256 return gw_kcgi_error(kerr);
1258 if (gw_trans->action != GW_BLOB) {
1259 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1260 gw_query_funcs[gw_trans->action].template);
1261 if (kerr != KCGI_OK) {
1262 khtml_close(gw_trans->gw_html_req);
1263 return gw_kcgi_error(kerr);
1267 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1270 static void
1271 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1273 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1274 return;
1276 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1277 return;
1279 khttp_puts(gw_trans->gw_req, err->msg);
1280 khtml_close(gw_trans->gw_html_req);
1283 static int
1284 gw_template(size_t key, void *arg)
1286 const struct got_error *error = NULL;
1287 struct gw_trans *gw_trans = arg;
1288 char *gw_got_link, *gw_site_link;
1289 char *site_owner_name, *site_owner_name_h;
1291 switch (key) {
1292 case (TEMPL_HEAD):
1293 khttp_puts(gw_trans->gw_req, head);
1294 break;
1295 case(TEMPL_HEADER):
1296 gw_got_link = gw_get_got_link(gw_trans);
1297 if (gw_got_link != NULL)
1298 khttp_puts(gw_trans->gw_req, gw_got_link);
1300 free(gw_got_link);
1301 break;
1302 case (TEMPL_SITEPATH):
1303 gw_site_link = gw_get_site_link(gw_trans);
1304 if (gw_site_link != NULL)
1305 khttp_puts(gw_trans->gw_req, gw_site_link);
1307 free(gw_site_link);
1308 break;
1309 case(TEMPL_TITLE):
1310 if (gw_trans->gw_conf->got_site_name != NULL)
1311 khtml_puts(gw_trans->gw_html_req,
1312 gw_trans->gw_conf->got_site_name);
1314 break;
1315 case (TEMPL_SEARCH):
1316 khttp_puts(gw_trans->gw_req, search);
1317 break;
1318 case(TEMPL_SITEOWNER):
1319 if (gw_trans->gw_conf->got_site_owner != NULL &&
1320 gw_trans->gw_conf->got_show_site_owner) {
1321 site_owner_name =
1322 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1323 if (asprintf(&site_owner_name_h, site_owner,
1324 site_owner_name) == -1)
1325 return 0;
1327 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1328 free(site_owner_name);
1329 free(site_owner_name_h);
1331 break;
1332 case(TEMPL_CONTENT):
1333 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1334 if (error)
1335 khttp_puts(gw_trans->gw_req, error->msg);
1336 break;
1337 default:
1338 return 0;
1340 return 1;
1343 static char *
1344 gw_gen_commit_header(char *str1, char *str2)
1346 char *return_html = NULL, *ref_str = NULL;
1348 if (strcmp(str2, "") != 0) {
1349 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1350 return_html = strdup("");
1351 return return_html;
1353 } else
1354 ref_str = strdup("");
1357 if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1358 return_html = strdup("");
1360 free(ref_str);
1361 return return_html;
1364 static char *
1365 gw_gen_diff_header(char *str1, char *str2)
1367 char *return_html = NULL;
1369 if (asprintf(&return_html, header_diff_html, str1, str2) == -1)
1370 return_html = strdup("");
1372 return return_html;
1375 static char *
1376 gw_gen_author_header(char *str)
1378 char *return_html = NULL;
1380 if (asprintf(&return_html, header_author_html, str) == -1)
1381 return_html = strdup("");
1383 return return_html;
1386 static char *
1387 gw_gen_committer_header(char *str)
1389 char *return_html = NULL;
1391 if (asprintf(&return_html, header_committer_html, str) == -1)
1392 return_html = strdup("");
1394 return return_html;
1397 static char *
1398 gw_gen_age_header(char *str)
1400 char *return_html = NULL;
1402 if (asprintf(&return_html, header_age_html, str) == -1)
1403 return_html = strdup("");
1405 return return_html;
1408 static char *
1409 gw_gen_commit_msg_header(char *str)
1411 char *return_html = NULL;
1413 if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1414 return_html = strdup("");
1416 return return_html;
1419 static char *
1420 gw_gen_tree_header(char *str)
1422 char *return_html = NULL;
1424 if (asprintf(&return_html, header_tree_html, str) == -1)
1425 return_html = strdup("");
1427 return return_html;
1430 static char *
1431 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1433 FILE *f;
1434 char *description = NULL, *d_file = NULL;
1435 unsigned int len;
1437 if (gw_trans->gw_conf->got_show_repo_description == 0)
1438 goto err;
1440 if (asprintf(&d_file, "%s/description", dir) == -1)
1441 goto err;
1443 if ((f = fopen(d_file, "r")) == NULL)
1444 goto err;
1446 fseek(f, 0, SEEK_END);
1447 len = ftell(f) + 1;
1448 fseek(f, 0, SEEK_SET);
1449 if ((description = calloc(len, sizeof(char *))) == NULL)
1450 goto err;
1452 fread(description, 1, len, f);
1453 fclose(f);
1454 free(d_file);
1455 return description;
1456 err:
1457 return strdup("");
1460 static char *
1461 gw_get_time_str(time_t committer_time, int ref_tm)
1463 struct tm tm;
1464 time_t diff_time;
1465 char *years = "years ago", *months = "months ago";
1466 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1467 char *minutes = "minutes ago", *seconds = "seconds ago";
1468 char *now = "right now";
1469 char *repo_age, *s;
1470 char datebuf[29];
1472 switch (ref_tm) {
1473 case TM_DIFF:
1474 diff_time = time(NULL) - committer_time;
1475 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1476 if (asprintf(&repo_age, "%lld %s",
1477 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1478 return NULL;
1479 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1480 if (asprintf(&repo_age, "%lld %s",
1481 (diff_time / 60 / 60 / 24 / (365 / 12)),
1482 months) == -1)
1483 return NULL;
1484 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1485 if (asprintf(&repo_age, "%lld %s",
1486 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1487 return NULL;
1488 } else if (diff_time > 60 * 60 * 24 * 2) {
1489 if (asprintf(&repo_age, "%lld %s",
1490 (diff_time / 60 / 60 / 24), days) == -1)
1491 return NULL;
1492 } else if (diff_time > 60 * 60 * 2) {
1493 if (asprintf(&repo_age, "%lld %s",
1494 (diff_time / 60 / 60), hours) == -1)
1495 return NULL;
1496 } else if (diff_time > 60 * 2) {
1497 if (asprintf(&repo_age, "%lld %s", (diff_time / 60),
1498 minutes) == -1)
1499 return NULL;
1500 } else if (diff_time > 2) {
1501 if (asprintf(&repo_age, "%lld %s", diff_time,
1502 seconds) == -1)
1503 return NULL;
1504 } else {
1505 if (asprintf(&repo_age, "%s", now) == -1)
1506 return NULL;
1508 break;
1509 case TM_LONG:
1510 if (gmtime_r(&committer_time, &tm) == NULL)
1511 return NULL;
1513 s = asctime_r(&tm, datebuf);
1514 if (s == NULL)
1515 return NULL;
1517 if (asprintf(&repo_age, "%s UTC", datebuf) == -1)
1518 return NULL;
1519 break;
1521 return repo_age;
1524 static const struct got_error *
1525 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
1526 char *repo_ref, int ref_tm)
1528 const struct got_error *error = NULL;
1529 struct got_object_id *id = NULL;
1530 struct got_repository *repo = NULL;
1531 struct got_commit_object *commit = NULL;
1532 struct got_reflist_head refs;
1533 struct got_reflist_entry *re;
1534 struct got_reference *head_ref;
1535 int is_head = 0;
1536 time_t committer_time = 0, cmp_time = 0;
1537 const char *refname;
1539 *repo_age = NULL;
1540 SIMPLEQ_INIT(&refs);
1542 if (repo_ref == NULL)
1543 return NULL;
1545 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1546 is_head = 1;
1548 if (gw_trans->gw_conf->got_show_repo_age == 0) {
1549 *repo_age = strdup("");
1550 if (*repo_age == NULL)
1551 return got_error_from_errno("strdup");
1552 return NULL;
1555 error = got_repo_open(&repo, dir, NULL);
1556 if (error)
1557 goto done;
1559 if (is_head)
1560 error = got_ref_list(&refs, repo, "refs/heads",
1561 got_ref_cmp_by_name, NULL);
1562 else
1563 error = got_ref_list(&refs, repo, repo_ref,
1564 got_ref_cmp_by_name, NULL);
1565 if (error)
1566 goto done;
1568 SIMPLEQ_FOREACH(re, &refs, entry) {
1569 if (is_head)
1570 refname = strdup(repo_ref);
1571 else
1572 refname = got_ref_get_name(re->ref);
1573 error = got_ref_open(&head_ref, repo, refname, 0);
1574 if (error)
1575 goto done;
1577 error = got_ref_resolve(&id, repo, head_ref);
1578 got_ref_close(head_ref);
1579 if (error)
1580 goto done;
1582 error = got_object_open_as_commit(&commit, repo, id);
1583 if (error)
1584 goto done;
1586 committer_time =
1587 got_object_commit_get_committer_time(commit);
1589 if (cmp_time < committer_time)
1590 cmp_time = committer_time;
1593 if (cmp_time != 0) {
1594 committer_time = cmp_time;
1595 *repo_age = gw_get_time_str(committer_time, ref_tm);
1596 } else {
1597 *repo_age = strdup("");
1598 if (*repo_age == NULL)
1599 error = got_error_from_errno("strdup");
1601 done:
1602 got_ref_list_free(&refs);
1603 free(id);
1604 return error;
1607 static char *
1608 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1610 const struct got_error *error;
1611 FILE *f = NULL;
1612 struct got_object_id *id1 = NULL, *id2 = NULL;
1613 struct buf *diffbuf = NULL;
1614 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL;
1615 char *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1616 int obj_type;
1617 size_t newsize;
1619 f = got_opentemp();
1620 if (f == NULL)
1621 return NULL;
1623 error = buf_alloc(&diffbuf, 0);
1624 if (error)
1625 return NULL;
1627 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1628 if (error)
1629 goto done;
1631 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1632 error = got_repo_match_object_id(&id1, &label1,
1633 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1634 if (error)
1635 goto done;
1638 error = got_repo_match_object_id(&id2, &label2,
1639 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1640 if (error)
1641 goto done;
1643 error = got_object_get_type(&obj_type, header->repo, id2);
1644 if (error)
1645 goto done;
1646 switch (obj_type) {
1647 case GOT_OBJ_TYPE_BLOB:
1648 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1649 header->repo, f);
1650 break;
1651 case GOT_OBJ_TYPE_TREE:
1652 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1653 header->repo, f);
1654 break;
1655 case GOT_OBJ_TYPE_COMMIT:
1656 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1657 header->repo, f);
1658 break;
1659 default:
1660 error = got_error(GOT_ERR_OBJ_TYPE);
1663 if ((buf = calloc(128, sizeof(char *))) == NULL)
1664 goto done;
1666 fseek(f, 0, SEEK_SET);
1668 while ((fgets(buf, 2048, f)) != NULL) {
1669 n_buf = buf;
1670 while (*n_buf == '\n')
1671 n_buf++;
1672 newline = strchr(n_buf, '\n');
1673 if (newline)
1674 *newline = ' ';
1676 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1677 if (buf_color == NULL)
1678 continue;
1680 error = buf_puts(&newsize, diffbuf, buf_color);
1681 if (error)
1682 return NULL;
1684 error = buf_puts(&newsize, diffbuf, div_end);
1685 if (error)
1686 return NULL;
1689 if (buf_len(diffbuf) > 0) {
1690 error = buf_putc(diffbuf, '\0');
1691 diff_html = strdup(buf_get(diffbuf));
1693 done:
1694 fclose(f);
1695 free(buf_color);
1696 free(buf);
1697 free(diffbuf);
1698 free(label1);
1699 free(label2);
1700 free(id1);
1701 free(id2);
1703 if (error)
1704 return NULL;
1705 else
1706 return diff_html;
1709 static char *
1710 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1712 FILE *f;
1713 char *owner = NULL, *d_file = NULL;
1714 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1715 char *comp, *pos, *buf;
1716 unsigned int i;
1718 if (gw_trans->gw_conf->got_show_repo_owner == 0)
1719 goto err;
1721 if (asprintf(&d_file, "%s/config", dir) == -1)
1722 goto err;
1724 if ((f = fopen(d_file, "r")) == NULL)
1725 goto err;
1727 if ((buf = calloc(128, sizeof(char *))) == NULL)
1728 goto err;
1730 while ((fgets(buf, 128, f)) != NULL) {
1731 if ((pos = strstr(buf, gotweb)) != NULL)
1732 break;
1734 if ((pos = strstr(buf, gitweb)) != NULL)
1735 break;
1738 if (pos == NULL)
1739 goto err;
1741 do {
1742 fgets(buf, 128, f);
1743 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1745 if (comp == NULL)
1746 goto err;
1748 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1749 goto err;
1751 for (i = 0; i < 2; i++) {
1752 owner = strsep(&buf, "\"");
1755 if (owner == NULL)
1756 goto err;
1758 fclose(f);
1759 free(d_file);
1760 return owner;
1761 err:
1762 return strdup("");
1765 static char *
1766 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1768 FILE *f;
1769 char *url = NULL, *d_file = NULL;
1770 unsigned int len;
1772 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
1773 return NULL;
1775 if ((f = fopen(d_file, "r")) == NULL)
1776 return NULL;
1778 fseek(f, 0, SEEK_END);
1779 len = ftell(f) + 1;
1780 fseek(f, 0, SEEK_SET);
1782 if ((url = calloc(len, sizeof(char *))) == NULL)
1783 return NULL;
1785 fread(url, 1, len, f);
1786 fclose(f);
1787 free(d_file);
1788 return url;
1791 static char *
1792 gw_get_repo_tags(struct gw_trans *gw_trans, struct gw_header *header, int limit,
1793 int tag_type)
1795 const struct got_error *error = NULL;
1796 struct got_repository *repo = NULL;
1797 struct got_reflist_head refs;
1798 struct got_reflist_entry *re;
1799 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL;
1800 char *age = NULL, *newline;
1801 struct buf *diffbuf = NULL;
1802 size_t newsize;
1804 SIMPLEQ_INIT(&refs);
1806 error = buf_alloc(&diffbuf, 0);
1807 if (error)
1808 return NULL;
1810 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1811 if (error)
1812 goto done;
1814 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1815 if (error)
1816 goto done;
1818 SIMPLEQ_FOREACH(re, &refs, entry) {
1819 const char *refname;
1820 char *refstr, *tag_commit0, *tag_commit, *id_str;
1821 const char *tagger;
1822 time_t tagger_time;
1823 struct got_object_id *id;
1824 struct got_tag_object *tag;
1826 refname = got_ref_get_name(re->ref);
1827 if (strncmp(refname, "refs/tags/", 10) != 0)
1828 continue;
1829 refname += 10;
1830 refstr = got_ref_to_str(re->ref);
1831 if (refstr == NULL) {
1832 error = got_error_from_errno("got_ref_to_str");
1833 goto done;
1836 error = got_ref_resolve(&id, repo, re->ref);
1837 if (error)
1838 goto done;
1839 error = got_object_open_as_tag(&tag, repo, id);
1840 free(id);
1841 if (error)
1842 goto done;
1844 tagger = got_object_tag_get_tagger(tag);
1845 tagger_time = got_object_tag_get_tagger_time(tag);
1847 error = got_object_id_str(&id_str,
1848 got_object_tag_get_object_id(tag));
1849 if (error)
1850 goto done;
1852 tag_commit0 = strdup(got_object_tag_get_message(tag));
1854 if (tag_commit0 == NULL) {
1855 error = got_error_from_errno("strdup");
1856 goto done;
1859 tag_commit = tag_commit0;
1860 while (*tag_commit == '\n')
1861 tag_commit++;
1863 switch (tag_type) {
1864 case TAGBRIEF:
1865 newline = strchr(tag_commit, '\n');
1866 if (newline)
1867 *newline = '\0';
1869 if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1870 TM_DIFF)) == -1) {
1871 error = got_error_from_errno("asprintf");
1872 goto done;
1875 if (asprintf(&tags_navs_disp, tags_navs,
1876 gw_trans->repo_name, id_str, gw_trans->repo_name,
1877 id_str, gw_trans->repo_name, id_str,
1878 gw_trans->repo_name, id_str) == -1) {
1879 error = got_error_from_errno("asprintf");
1880 goto done;
1883 if (asprintf(&tag_row, tags_row, age, refname,
1884 tag_commit, tags_navs_disp) == -1) {
1885 error = got_error_from_errno("asprintf");
1886 goto done;
1889 free(tags_navs_disp);
1890 break;
1891 case TAGFULL:
1892 if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1893 TM_LONG)) == -1) {
1894 error = got_error_from_errno("asprintf");
1895 goto done;
1897 if (asprintf(&tag_row, tag_info, age,
1898 gw_html_escape(tagger),
1899 gw_html_escape(tag_commit)) == -1) {
1900 error = got_error_from_errno("asprintf");
1901 goto done;
1903 break;
1904 default:
1905 break;
1908 got_object_tag_close(tag);
1910 error = buf_puts(&newsize, diffbuf, tag_row);
1912 free(id_str);
1913 free(refstr);
1914 free(age);
1915 free(tag_commit0);
1916 free(tag_row);
1918 if (error || (limit && --limit == 0))
1919 break;
1922 if (buf_len(diffbuf) > 0) {
1923 error = buf_putc(diffbuf, '\0');
1924 tags = strdup(buf_get(diffbuf));
1926 done:
1927 buf_free(diffbuf);
1928 got_ref_list_free(&refs);
1929 if (repo)
1930 got_repo_close(repo);
1931 if (error)
1932 return NULL;
1933 else
1934 return tags;
1937 static void
1938 gw_free_headers(struct gw_header *header)
1940 free(header->id);
1941 free(header->path);
1942 if (header->commit != NULL)
1943 got_object_commit_close(header->commit);
1944 if (header->repo)
1945 got_repo_close(header->repo);
1946 free(header->refs_str);
1947 free(header->commit_id);
1948 free(header->parent_id);
1949 free(header->tree_id);
1950 free(header->author);
1951 free(header->committer);
1952 free(header->commit_msg);
1955 static struct gw_header *
1956 gw_init_header()
1958 struct gw_header *header;
1960 header = malloc(sizeof(*header));
1961 if (header == NULL)
1962 return NULL;
1964 header->repo = NULL;
1965 header->commit = NULL;
1966 header->id = NULL;
1967 header->path = NULL;
1968 SIMPLEQ_INIT(&header->refs);
1970 return header;
1973 static const struct got_error *
1974 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1975 int limit)
1977 const struct got_error *error = NULL;
1978 struct got_commit_graph *graph = NULL;
1980 error = got_commit_graph_open(&graph, header->path, 0);
1981 if (error)
1982 goto done;
1984 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1985 NULL, NULL);
1986 if (error)
1987 goto done;
1989 for (;;) {
1990 error = got_commit_graph_iter_next(&header->id, graph,
1991 header->repo, NULL, NULL);
1992 if (error) {
1993 if (error->code == GOT_ERR_ITER_COMPLETED)
1994 error = NULL;
1995 goto done;
1997 if (header->id == NULL)
1998 goto done;
2000 error = got_object_open_as_commit(&header->commit, header->repo,
2001 header->id);
2002 if (error)
2003 goto done;
2005 error = gw_get_commit(gw_trans, header);
2006 if (limit > 1) {
2007 struct gw_header *n_header = NULL;
2008 if ((n_header = gw_init_header()) == NULL) {
2009 error = got_error_from_errno("malloc");
2010 goto done;
2013 n_header->refs_str = strdup(header->refs_str);
2014 n_header->commit_id = strdup(header->commit_id);
2015 n_header->parent_id = strdup(header->parent_id);
2016 n_header->tree_id = strdup(header->tree_id);
2017 n_header->author = strdup(header->author);
2018 n_header->committer = strdup(header->committer);
2019 n_header->commit_msg = strdup(header->commit_msg);
2020 n_header->committer_time = header->committer_time;
2021 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2022 entry);
2024 if (error || (limit && --limit == 0))
2025 break;
2027 done:
2028 if (graph)
2029 got_commit_graph_close(graph);
2030 return error;
2033 static const struct got_error *
2034 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2036 const struct got_error *error = NULL;
2037 struct got_reflist_entry *re;
2038 struct got_object_id *id2 = NULL;
2039 struct got_object_qid *parent_id;
2040 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2042 /*print commit*/
2043 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2044 char *s;
2045 const char *name;
2046 struct got_tag_object *tag = NULL;
2047 int cmp;
2049 name = got_ref_get_name(re->ref);
2050 if (strcmp(name, GOT_REF_HEAD) == 0)
2051 continue;
2052 if (strncmp(name, "refs/", 5) == 0)
2053 name += 5;
2054 if (strncmp(name, "got/", 4) == 0)
2055 continue;
2056 if (strncmp(name, "heads/", 6) == 0)
2057 name += 6;
2058 if (strncmp(name, "remotes/", 8) == 0)
2059 name += 8;
2060 if (strncmp(name, "tags/", 5) == 0) {
2061 error = got_object_open_as_tag(&tag, header->repo,
2062 re->id);
2063 if (error) {
2064 if (error->code != GOT_ERR_OBJ_TYPE)
2065 continue;
2067 * Ref points at something other
2068 * than a tag.
2070 error = NULL;
2071 tag = NULL;
2074 cmp = got_object_id_cmp(tag ?
2075 got_object_tag_get_object_id(tag) : re->id, header->id);
2076 if (tag)
2077 got_object_tag_close(tag);
2078 if (cmp != 0)
2079 continue;
2080 s = refs_str;
2081 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2082 s ? ", " : "", name) == -1) {
2083 error = got_error_from_errno("asprintf");
2084 free(s);
2085 return error;
2087 header->refs_str = strdup(refs_str);
2088 free(s);
2091 if (refs_str == NULL)
2092 header->refs_str = strdup("");
2093 free(refs_str);
2095 error = got_object_id_str(&header->commit_id, header->id);
2096 if (error)
2097 return error;
2099 error = got_object_id_str(&header->tree_id,
2100 got_object_commit_get_tree_id(header->commit));
2101 if (error)
2102 return error;
2104 if (gw_trans->action == GW_DIFF) {
2105 parent_id = SIMPLEQ_FIRST(
2106 got_object_commit_get_parent_ids(header->commit));
2107 if (parent_id != NULL) {
2108 id2 = got_object_id_dup(parent_id->id);
2109 free (parent_id);
2110 error = got_object_id_str(&header->parent_id, id2);
2111 if (error)
2112 return error;
2113 free(id2);
2114 } else
2115 header->parent_id = strdup("/dev/null");
2116 } else
2117 header->parent_id = strdup("");
2119 header->committer_time =
2120 got_object_commit_get_committer_time(header->commit);
2122 if (gw_trans->action != GW_BRIEFS && gw_trans->action != GW_SUMMARY) {
2123 header->author = strdup(
2124 gw_html_escape(got_object_commit_get_author(header->commit))
2126 } else {
2127 header->author = strdup(
2128 got_object_commit_get_author(header->commit)
2132 header->committer = strdup(
2133 gw_html_escape(got_object_commit_get_committer(header->commit))
2136 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2137 if (error)
2138 return error;
2140 commit_msg = commit_msg0;
2141 while (*commit_msg == '\n')
2142 commit_msg++;
2144 header->commit_msg = strdup(commit_msg);
2145 free(commit_msg0);
2146 return error;
2149 static const struct got_error *
2150 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2152 const struct got_error *error = NULL;
2153 char *in_repo_path = NULL;
2155 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2156 if (error)
2157 return error;
2159 if (gw_trans->commit == NULL) {
2160 struct got_reference *head_ref;
2161 error = got_ref_open(&head_ref, header->repo,
2162 gw_trans->headref, 0);
2163 if (error)
2164 return error;
2166 error = got_ref_resolve(&header->id, header->repo, head_ref);
2167 got_ref_close(head_ref);
2168 if (error)
2169 return error;
2171 error = got_object_open_as_commit(&header->commit,
2172 header->repo, header->id);
2173 } else {
2174 struct got_reference *ref;
2175 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2176 if (error == NULL) {
2177 int obj_type;
2178 error = got_ref_resolve(&header->id, header->repo, ref);
2179 got_ref_close(ref);
2180 if (error)
2181 return error;
2182 error = got_object_get_type(&obj_type, header->repo,
2183 header->id);
2184 if (error)
2185 return error;
2186 if (obj_type == GOT_OBJ_TYPE_TAG) {
2187 struct got_tag_object *tag;
2188 error = got_object_open_as_tag(&tag,
2189 header->repo, header->id);
2190 if (error)
2191 return error;
2192 if (got_object_tag_get_object_type(tag) !=
2193 GOT_OBJ_TYPE_COMMIT) {
2194 got_object_tag_close(tag);
2195 error = got_error(GOT_ERR_OBJ_TYPE);
2196 return error;
2198 free(header->id);
2199 header->id = got_object_id_dup(
2200 got_object_tag_get_object_id(tag));
2201 if (header->id == NULL)
2202 error = got_error_from_errno(
2203 "got_object_id_dup");
2204 got_object_tag_close(tag);
2205 if (error)
2206 return error;
2207 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2208 error = got_error(GOT_ERR_OBJ_TYPE);
2209 return error;
2211 error = got_object_open_as_commit(&header->commit,
2212 header->repo, header->id);
2213 if (error)
2214 return error;
2216 if (header->commit == NULL) {
2217 error = got_repo_match_object_id_prefix(&header->id,
2218 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2219 header->repo);
2220 if (error)
2221 return error;
2223 error = got_repo_match_object_id_prefix(&header->id,
2224 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2225 header->repo);
2228 error = got_repo_map_path(&in_repo_path, header->repo,
2229 gw_trans->repo_path, 1);
2230 if (error)
2231 return error;
2233 if (in_repo_path) {
2234 header->path = strdup(in_repo_path);
2236 free(in_repo_path);
2238 error = got_ref_list(&header->refs, header->repo, NULL,
2239 got_ref_cmp_by_name, NULL);
2240 if (error)
2241 return error;
2243 error = gw_get_commits(gw_trans, header, limit);
2244 return error;
2247 struct blame_line {
2248 int annotated;
2249 char *id_str;
2250 char *committer;
2251 char datebuf[11]; /* YYYY-MM-DD + NUL */
2254 struct gw_blame_cb_args {
2255 struct blame_line *lines;
2256 int nlines;
2257 int nlines_prec;
2258 int lineno_cur;
2259 off_t *line_offsets;
2260 FILE *f;
2261 struct got_repository *repo;
2262 struct gw_trans *gw_trans;
2263 struct buf *blamebuf;
2266 static const struct got_error *
2267 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2269 const struct got_error *err = NULL;
2270 struct gw_blame_cb_args *a = arg;
2271 struct blame_line *bline;
2272 char *line = NULL;
2273 size_t linesize = 0, newsize;
2274 struct got_commit_object *commit = NULL;
2275 off_t offset;
2276 struct tm tm;
2277 time_t committer_time;
2279 if (nlines != a->nlines ||
2280 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2281 return got_error(GOT_ERR_RANGE);
2283 if (lineno == -1)
2284 return NULL; /* no change in this commit */
2286 /* Annotate this line. */
2287 bline = &a->lines[lineno - 1];
2288 if (bline->annotated)
2289 return NULL;
2290 err = got_object_id_str(&bline->id_str, id);
2291 if (err)
2292 return err;
2294 err = got_object_open_as_commit(&commit, a->repo, id);
2295 if (err)
2296 goto done;
2298 bline->committer = strdup(got_object_commit_get_committer(commit));
2299 if (bline->committer == NULL) {
2300 err = got_error_from_errno("strdup");
2301 goto done;
2304 committer_time = got_object_commit_get_committer_time(commit);
2305 if (localtime_r(&committer_time, &tm) == NULL)
2306 return got_error_from_errno("localtime_r");
2307 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2308 &tm) >= sizeof(bline->datebuf)) {
2309 err = got_error(GOT_ERR_NO_SPACE);
2310 goto done;
2312 bline->annotated = 1;
2314 /* Print lines annotated so far. */
2315 bline = &a->lines[a->lineno_cur - 1];
2316 if (!bline->annotated)
2317 goto done;
2319 offset = a->line_offsets[a->lineno_cur - 1];
2320 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2321 err = got_error_from_errno("fseeko");
2322 goto done;
2325 while (bline->annotated) {
2326 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2327 *line_escape = NULL;
2328 size_t len;
2330 if (getline(&line, &linesize, a->f) == -1) {
2331 if (ferror(a->f))
2332 err = got_error_from_errno("getline");
2333 break;
2336 committer = bline->committer;
2337 smallerthan = strchr(committer, '<');
2338 if (smallerthan && smallerthan[1] != '\0')
2339 committer = smallerthan + 1;
2340 at = strchr(committer, '@');
2341 if (at)
2342 *at = '\0';
2343 len = strlen(committer);
2344 if (len >= 9)
2345 committer[8] = '\0';
2347 nl = strchr(line, '\n');
2348 if (nl)
2349 *nl = '\0';
2351 if (strcmp(line, "") != 0)
2352 line_escape = strdup(gw_html_escape(line));
2353 else
2354 line_escape = strdup("");
2356 if (a->gw_trans->repo_folder == NULL)
2357 a->gw_trans->repo_folder = strdup("");
2358 if (a->gw_trans->repo_folder == NULL)
2359 goto err;
2360 asprintf(&blame_row, blame_line, a->nlines_prec, a->lineno_cur,
2361 a->gw_trans->repo_name, bline->id_str,
2362 a->gw_trans->repo_file, a->gw_trans->repo_folder,
2363 bline->id_str, bline->datebuf, committer, line_escape);
2364 a->lineno_cur++;
2365 err = buf_puts(&newsize, a->blamebuf, blame_row);
2366 if (err)
2367 return err;
2369 bline = &a->lines[a->lineno_cur - 1];
2370 err:
2371 free(line_escape);
2372 free(blame_row);
2374 done:
2375 if (commit)
2376 got_object_commit_close(commit);
2377 free(line);
2378 return err;
2381 static int
2382 isbinary(const char *buf, size_t n)
2384 return (memchr(buf, '\0', n) != NULL);
2387 static char*
2388 gw_get_file_blame_blob(struct gw_trans *gw_trans)
2390 const struct got_error *error = NULL;
2391 struct got_repository *repo = NULL;
2392 struct got_object_id *obj_id = NULL;
2393 struct got_object_id *commit_id = NULL;
2394 struct got_blob_object *blob = NULL;
2395 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL;
2396 char *folder = NULL;
2397 struct gw_blame_cb_args bca;
2398 int i, obj_type;
2399 size_t filesize;
2400 enum kcgi_err kerr;
2402 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2403 if (error)
2404 goto done;
2406 if (gw_trans->repo_folder != NULL) {
2407 if (asprintf(&folder, "%s/", gw_trans->repo_folder) == -1) {
2408 error = got_error_from_errno("asprintf");
2409 goto done;
2411 } else
2412 folder = strdup("");
2414 if (asprintf(&path, "%s%s", folder, gw_trans->repo_file) == -1) {
2415 error = got_error_from_errno("asprintf");
2416 goto done;
2418 free(folder);
2420 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2421 if (error)
2422 goto done;
2424 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2425 GOT_OBJ_TYPE_COMMIT, 1, repo);
2426 if (error)
2427 goto done;
2429 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2430 if (error)
2431 goto done;
2433 if (obj_id == NULL) {
2434 error = got_error(GOT_ERR_NO_OBJ);
2435 goto done;
2438 error = got_object_get_type(&obj_type, repo, obj_id);
2439 if (error)
2440 goto done;
2442 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2443 error = got_error(GOT_ERR_OBJ_TYPE);
2444 goto done;
2447 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2448 if (error)
2449 goto done;
2451 error = buf_alloc(&bca.blamebuf, 0);
2452 if (error)
2453 goto done;
2455 bca.f = got_opentemp();
2456 if (bca.f == NULL) {
2457 error = got_error_from_errno("got_opentemp");
2458 goto done;
2460 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2461 &bca.line_offsets, bca.f, blob);
2462 if (error || bca.nlines == 0)
2463 goto done;
2465 if (gw_trans->action == GW_BLOB) {
2466 int len;
2467 size_t n;
2469 fseek(bca.f, 0, SEEK_END);
2470 len = ftell(bca.f) + 1;
2471 fseek(bca.f, 0, SEEK_SET);
2473 if ((blame_html = calloc(len, sizeof(char *))) == NULL)
2474 goto done;
2476 n = fread(blame_html, 1, len, bca.f);
2477 if (n == -1) {
2478 error = got_ferror(bca.f, GOT_ERR_IO);
2479 goto done;
2482 if (isbinary(blame_html, n))
2483 gw_trans->mime = KMIME_APP_OCTET_STREAM;
2484 else
2485 gw_trans->mime = KMIME_TEXT_PLAIN;
2487 error = gw_display_index(gw_trans);
2489 if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
2490 kerr = khttp_write(gw_trans->gw_req, blame_html, len);
2491 if (kerr != KCGI_OK)
2492 error = gw_kcgi_error(kerr);
2494 goto done;
2497 /* Don't include \n at EOF in the blame line count. */
2498 if (bca.line_offsets[bca.nlines - 1] == filesize)
2499 bca.nlines--;
2501 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2502 if (bca.lines == NULL) {
2503 error = got_error_from_errno("calloc");
2504 goto done;
2506 bca.lineno_cur = 1;
2507 bca.nlines_prec = 0;
2508 i = bca.nlines;
2509 while (i > 0) {
2510 i /= 10;
2511 bca.nlines_prec++;
2513 bca.repo = repo;
2514 bca.gw_trans = gw_trans;
2516 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2517 NULL, NULL);
2518 if (error)
2519 goto done;
2520 if (buf_len(bca.blamebuf) > 0) {
2521 error = buf_putc(bca.blamebuf, '\0');
2522 blame_html = strdup(buf_get(bca.blamebuf));
2524 done:
2525 free(bca.line_offsets);
2526 free(bca.blamebuf);
2527 free(in_repo_path);
2528 free(commit_id);
2529 free(obj_id);
2530 free(path);
2532 if (gw_trans->action != GW_BLOB && bca.lines) {
2533 for (i = 0; i < bca.nlines; i++) {
2534 struct blame_line *bline = &bca.lines[i];
2535 free(bline->id_str);
2536 free(bline->committer);
2538 free(bca.lines);
2540 if (error)
2541 return NULL;
2542 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2543 return NULL;
2544 if (blob)
2545 error = got_object_blob_close(blob);
2546 if (error)
2547 return NULL;
2548 if (repo)
2549 error = got_repo_close(repo);
2550 if (error)
2551 return NULL;
2552 else
2553 return blame_html;
2556 static char*
2557 gw_get_repo_tree(struct gw_trans *gw_trans)
2559 const struct got_error *error = NULL;
2560 struct got_repository *repo = NULL;
2561 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2562 struct got_tree_object *tree = NULL;
2563 struct buf *diffbuf = NULL;
2564 size_t newsize;
2565 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2566 *tree_row = NULL, *id_str, *class = NULL;
2567 int nentries, i, class_flip = 0;
2569 error = buf_alloc(&diffbuf, 0);
2570 if (error)
2571 return NULL;
2573 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2574 if (error)
2575 goto done;
2577 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2578 if (error)
2579 goto done;
2581 if (gw_trans->repo_folder != NULL)
2582 path = strdup(gw_trans->repo_folder);
2583 else if (in_repo_path) {
2584 free(path);
2585 path = in_repo_path;
2588 if (gw_trans->commit == NULL) {
2589 struct got_reference *head_ref;
2590 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2591 if (error)
2592 goto done;
2594 error = got_ref_resolve(&commit_id, repo, head_ref);
2595 got_ref_close(head_ref);
2597 } else
2598 error = got_repo_match_object_id(&commit_id, NULL,
2599 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2600 if (error)
2601 goto done;
2603 error = got_object_id_str(&gw_trans->commit, commit_id);
2604 if (error)
2605 goto done;
2607 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2608 if (error)
2609 goto done;
2611 error = got_object_open_as_tree(&tree, repo, tree_id);
2612 if (error)
2613 goto done;
2615 nentries = got_object_tree_get_nentries(tree);
2617 for (i = 0; i < nentries; i++) {
2618 struct got_tree_entry *te;
2619 const char *modestr = "";
2620 char *id = NULL, *url_html = NULL;
2622 te = got_object_tree_get_entry(tree, i);
2624 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2625 if (error)
2626 goto done;
2628 if (asprintf(&id, "%s", id_str) == -1) {
2629 error = got_error_from_errno("asprintf");
2630 free(id_str);
2631 goto done;
2634 mode_t mode = got_tree_entry_get_mode(te);
2636 if (got_object_tree_entry_is_submodule(te))
2637 modestr = "$";
2638 else if (S_ISLNK(mode))
2639 modestr = "@";
2640 else if (S_ISDIR(mode))
2641 modestr = "/";
2642 else if (mode & S_IXUSR)
2643 modestr = "*";
2645 if (class_flip == 0) {
2646 class = strdup("back_lightgray");
2647 class_flip = 1;
2648 } else {
2649 class = strdup("back_white");
2650 class_flip = 0;
2653 char *build_folder = NULL;
2654 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2655 if (gw_trans->repo_folder != NULL) {
2656 if (asprintf(&build_folder, "%s/%s",
2657 gw_trans->repo_folder,
2658 got_tree_entry_get_name(te)) == -1) {
2659 error =
2660 got_error_from_errno("asprintf");
2661 goto done;
2663 } else {
2664 if (asprintf(&build_folder, "/%s",
2665 got_tree_entry_get_name(te)) == -1)
2666 goto done;
2669 if (asprintf(&url_html, folder_html,
2670 gw_trans->repo_name, gw_trans->action_name,
2671 gw_trans->commit, build_folder,
2672 got_tree_entry_get_name(te), modestr) == -1) {
2673 error = got_error_from_errno("asprintf");
2674 goto done;
2677 if (asprintf(&tree_row, tree_line, class, url_html,
2678 class) == -1) {
2679 error = got_error_from_errno("asprintf");
2680 goto done;
2683 } else {
2684 if (gw_trans->repo_folder != NULL) {
2685 if (asprintf(&build_folder, "%s",
2686 gw_trans->repo_folder) == -1) {
2687 error =
2688 got_error_from_errno("asprintf");
2689 goto done;
2691 } else
2692 build_folder = strdup("");
2694 if (asprintf(&url_html, file_html, gw_trans->repo_name,
2695 "blob", gw_trans->commit,
2696 got_tree_entry_get_name(te), build_folder,
2697 got_tree_entry_get_name(te), modestr) == -1) {
2698 error = got_error_from_errno("asprintf");
2699 goto done;
2702 if (asprintf(&tree_row, tree_line_with_navs, class,
2703 url_html, class, gw_trans->repo_name, "blob",
2704 gw_trans->commit, got_tree_entry_get_name(te),
2705 build_folder, "blob", gw_trans->repo_name,
2706 "blame", gw_trans->commit,
2707 got_tree_entry_get_name(te), build_folder,
2708 "blame") == -1) {
2709 error = got_error_from_errno("asprintf");
2710 goto done;
2713 free(build_folder);
2715 if (error)
2716 goto done;
2718 error = buf_puts(&newsize, diffbuf, tree_row);
2719 if (error)
2720 goto done;
2722 free(id);
2723 free(id_str);
2724 free(url_html);
2725 free(tree_row);
2728 if (buf_len(diffbuf) > 0) {
2729 error = buf_putc(diffbuf, '\0');
2730 tree_html = strdup(buf_get(diffbuf));
2732 done:
2733 if (tree)
2734 got_object_tree_close(tree);
2735 if (repo)
2736 got_repo_close(repo);
2738 free(in_repo_path);
2739 free(tree_id);
2740 free(diffbuf);
2741 if (error)
2742 return NULL;
2743 else
2744 return tree_html;
2747 static char *
2748 gw_get_repo_heads(struct gw_trans *gw_trans)
2750 const struct got_error *error = NULL;
2751 struct got_repository *repo = NULL;
2752 struct got_reflist_head refs;
2753 struct got_reflist_entry *re;
2754 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2755 struct buf *diffbuf = NULL;
2756 size_t newsize;
2758 SIMPLEQ_INIT(&refs);
2760 error = buf_alloc(&diffbuf, 0);
2761 if (error)
2762 return NULL;
2764 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2765 if (error)
2766 goto done;
2768 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2769 NULL);
2770 if (error)
2771 goto done;
2773 SIMPLEQ_FOREACH(re, &refs, entry) {
2774 char *refname;
2776 refname = strdup(got_ref_get_name(re->ref));
2777 if (refname == NULL) {
2778 error = got_error_from_errno("got_ref_to_str");
2779 goto done;
2782 if (strncmp(refname, "refs/heads/", 11) != 0) {
2783 free(refname);
2784 continue;
2787 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
2788 refname, TM_DIFF);
2789 if (error)
2790 goto done;
2792 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2793 refname, gw_trans->repo_name, refname,
2794 gw_trans->repo_name, refname, gw_trans->repo_name,
2795 refname) == -1) {
2796 error = got_error_from_errno("asprintf");
2797 goto done;
2800 if (strncmp(refname, "refs/heads/", 11) == 0)
2801 refname += 11;
2803 if (asprintf(&head_row, heads_row, age, refname,
2804 head_navs_disp) == -1) {
2805 error = got_error_from_errno("asprintf");
2806 goto done;
2809 error = buf_puts(&newsize, diffbuf, head_row);
2811 free(head_navs_disp);
2812 free(head_row);
2815 if (buf_len(diffbuf) > 0) {
2816 error = buf_putc(diffbuf, '\0');
2817 heads = strdup(buf_get(diffbuf));
2819 done:
2820 buf_free(diffbuf);
2821 got_ref_list_free(&refs);
2822 if (repo)
2823 got_repo_close(repo);
2824 if (error)
2825 return NULL;
2826 else
2827 return heads;
2830 static char *
2831 gw_get_got_link(struct gw_trans *gw_trans)
2833 char *link;
2835 if (asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2836 gw_trans->gw_conf->got_logo) == -1)
2837 return NULL;
2839 return link;
2842 static char *
2843 gw_get_site_link(struct gw_trans *gw_trans)
2845 char *link, *repo = "", *action = "";
2847 if (gw_trans->repo_name != NULL)
2848 if (asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2849 "</a>", gw_trans->repo_name, gw_trans->repo_name) == -1)
2850 return NULL;
2852 if (gw_trans->action_name != NULL)
2853 if (asprintf(&action, " / %s", gw_trans->action_name) == -1)
2854 return NULL;
2856 if (asprintf(&link, site_link, GOTWEB,
2857 gw_trans->gw_conf->got_site_link, repo, action) == -1)
2858 return NULL;
2860 return link;
2863 static char *
2864 gw_colordiff_line(char *buf)
2866 const struct got_error *error = NULL;
2867 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2868 struct buf *diffbuf = NULL;
2869 size_t newsize;
2871 error = buf_alloc(&diffbuf, 0);
2872 if (error)
2873 return NULL;
2875 if (buf == NULL)
2876 return NULL;
2877 if (strncmp(buf, "-", 1) == 0)
2878 color = "diff_minus";
2879 if (strncmp(buf, "+", 1) == 0)
2880 color = "diff_plus";
2881 if (strncmp(buf, "@@", 2) == 0)
2882 color = "diff_chunk_header";
2883 if (strncmp(buf, "@@", 2) == 0)
2884 color = "diff_chunk_header";
2885 if (strncmp(buf, "commit +", 8) == 0)
2886 color = "diff_meta";
2887 if (strncmp(buf, "commit -", 8) == 0)
2888 color = "diff_meta";
2889 if (strncmp(buf, "blob +", 6) == 0)
2890 color = "diff_meta";
2891 if (strncmp(buf, "blob -", 6) == 0)
2892 color = "diff_meta";
2893 if (strncmp(buf, "file +", 6) == 0)
2894 color = "diff_meta";
2895 if (strncmp(buf, "file -", 6) == 0)
2896 color = "diff_meta";
2897 if (strncmp(buf, "from:", 5) == 0)
2898 color = "diff_author";
2899 if (strncmp(buf, "via:", 4) == 0)
2900 color = "diff_author";
2901 if (strncmp(buf, "date:", 5) == 0)
2902 color = "diff_date";
2904 if (asprintf(&div_diff_line_div, div_diff_line, color) == -1)
2905 return NULL;
2907 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2908 if (error)
2909 return NULL;
2911 error = buf_puts(&newsize, diffbuf, buf);
2912 if (error)
2913 return NULL;
2915 if (buf_len(diffbuf) > 0) {
2916 error = buf_putc(diffbuf, '\0');
2917 colorized_line = strdup(buf_get(diffbuf));
2920 free(diffbuf);
2921 free(div_diff_line_div);
2922 return colorized_line;
2925 static char *
2926 gw_html_escape(const char *html)
2928 char *escaped_str = NULL, *buf;
2929 char c[1];
2930 size_t sz, i, buff_sz = 2048;
2932 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2933 return NULL;
2935 if (html == NULL)
2936 return NULL;
2937 else
2938 if ((sz = strlen(html)) == 0)
2939 return NULL;
2941 /* only work with buff_sz */
2942 if (buff_sz < sz)
2943 sz = buff_sz;
2945 for (i = 0; i < sz; i++) {
2946 c[0] = html[i];
2947 switch (c[0]) {
2948 case ('>'):
2949 strcat(buf, "&gt;");
2950 break;
2951 case ('&'):
2952 strcat(buf, "&amp;");
2953 break;
2954 case ('<'):
2955 strcat(buf, "&lt;");
2956 break;
2957 case ('"'):
2958 strcat(buf, "&quot;");
2959 break;
2960 case ('\''):
2961 strcat(buf, "&apos;");
2962 break;
2963 case ('\n'):
2964 strcat(buf, "<br />");
2965 default:
2966 strcat(buf, &c[0]);
2967 break;
2970 asprintf(&escaped_str, "%s", buf);
2971 free(buf);
2972 return escaped_str;
2975 int
2976 main(int argc, char *argv[])
2978 const struct got_error *error = NULL;
2979 struct gw_trans *gw_trans;
2980 struct gw_dir *dir = NULL, *tdir;
2981 const char *page = "index";
2982 int gw_malloc = 1;
2983 enum kcgi_err kerr;
2985 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2986 errx(1, "malloc");
2988 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2989 errx(1, "malloc");
2991 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2992 errx(1, "malloc");
2994 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2995 errx(1, "malloc");
2997 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2998 if (kerr != KCGI_OK) {
2999 error = gw_kcgi_error(kerr);
3000 goto done;
3003 if ((gw_trans->gw_conf =
3004 malloc(sizeof(struct gotweb_conf))) == NULL) {
3005 gw_malloc = 0;
3006 error = got_error_from_errno("malloc");
3007 goto done;
3010 TAILQ_INIT(&gw_trans->gw_dirs);
3011 TAILQ_INIT(&gw_trans->gw_headers);
3013 gw_trans->page = 0;
3014 gw_trans->repos_total = 0;
3015 gw_trans->repo_path = NULL;
3016 gw_trans->commit = NULL;
3017 gw_trans->headref = strdup(GOT_REF_HEAD);
3018 gw_trans->mime = KMIME_TEXT_HTML;
3019 gw_trans->gw_tmpl->key = gw_templs;
3020 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3021 gw_trans->gw_tmpl->arg = gw_trans;
3022 gw_trans->gw_tmpl->cb = gw_template;
3023 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3024 if (error)
3025 goto done;
3027 error = gw_parse_querystring(gw_trans);
3028 if (error)
3029 goto done;
3031 if (gw_trans->action == GW_BLOB)
3032 error = gw_blob(gw_trans);
3033 else
3034 error = gw_display_index(gw_trans);
3035 done:
3036 if (error) {
3037 gw_trans->mime = KMIME_TEXT_PLAIN;
3038 gw_trans->action = GW_ERR;
3039 gw_display_error(gw_trans, error);
3041 if (gw_malloc) {
3042 free(gw_trans->gw_conf->got_repos_path);
3043 free(gw_trans->gw_conf->got_site_name);
3044 free(gw_trans->gw_conf->got_site_owner);
3045 free(gw_trans->gw_conf->got_site_link);
3046 free(gw_trans->gw_conf->got_logo);
3047 free(gw_trans->gw_conf->got_logo_url);
3048 free(gw_trans->gw_conf);
3049 free(gw_trans->commit);
3050 free(gw_trans->repo_path);
3051 free(gw_trans->repo_name);
3052 free(gw_trans->repo_file);
3053 free(gw_trans->action_name);
3054 free(gw_trans->headref);
3056 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3057 free(dir->name);
3058 free(dir->description);
3059 free(dir->age);
3060 free(dir->url);
3061 free(dir->path);
3062 free(dir);
3067 khttp_free(gw_trans->gw_req);
3068 return 0;