Blob


1 /*
2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2014, 2015, 2017 Kristaps Dzonsons <kristaps@bsd.lv>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/queue.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
23 #include <dirent.h>
24 #include <err.h>
25 #include <regex.h>
26 #include <stdarg.h>
27 #include <stdbool.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 trans {
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
63 struct kreq *gw_req;
64 char *repo_name;
65 char *repo_path;
66 char *commit;
67 char *repo_file;
68 char *action_name;
69 char *headref;
70 unsigned int action;
71 unsigned int page;
72 unsigned int repos_total;
73 enum kmime mime;
74 };
76 enum gw_key {
77 KEY_PATH,
78 KEY_ACTION,
79 KEY_COMMIT_ID,
80 KEY_FILE,
81 KEY_PAGE,
82 KEY_HEADREF,
83 KEY__MAX
84 };
86 struct gw_dir {
87 TAILQ_ENTRY(gw_dir) entry;
88 char *name;
89 char *owner;
90 char *description;
91 char *url;
92 char *age;
93 char *path;
94 };
96 enum tmpl {
97 TEMPL_HEAD,
98 TEMPL_HEADER,
99 TEMPL_SITEPATH,
100 TEMPL_SITEOWNER,
101 TEMPL_TITLE,
102 TEMPL_SEARCH,
103 TEMPL_CONTENT,
104 TEMPL__MAX
105 };
107 enum ref_tm {
108 TM_DIFF,
109 TM_LONG,
110 };
112 enum logs {
113 LOGBRIEF,
114 LOGCOMMIT,
115 LOGFULL,
116 LOGTREE,
117 LOGDIFF,
118 LOGBLAME,
119 LOGTAG,
120 };
122 enum tags {
123 TAGBRIEF,
124 TAGFULL,
125 };
127 struct buf {
128 u_char *cb_buf;
129 size_t cb_size;
130 size_t cb_len;
131 };
133 static const char *const templs[TEMPL__MAX] = {
134 "head",
135 "header",
136 "sitepath",
137 "siteowner",
138 "title",
139 "search",
140 "content",
141 };
143 static const struct kvalid gw_keys[KEY__MAX] = {
144 { kvalid_stringne, "path" },
145 { kvalid_stringne, "action" },
146 { kvalid_stringne, "commit" },
147 { kvalid_stringne, "file" },
148 { kvalid_int, "page" },
149 { kvalid_stringne, "headref" },
150 };
152 int gw_get_repo_log_count(struct trans *, char *);
154 static struct gw_dir *gw_init_gw_dir(char *);
156 static char *gw_get_repo_description(struct trans *,
157 char *);
158 static char *gw_get_repo_owner(struct trans *,
159 char *);
160 static char *gw_get_time_str(time_t, int);
161 static char *gw_get_repo_age(struct trans *,
162 char *, char *, int);
163 static char *gw_get_repo_log(struct trans *, const char *,
164 char *, int, int);
165 static char *gw_get_repo_tags(struct trans *, int, int);
166 static char *gw_get_repo_heads(struct trans *);
167 static char *gw_get_clone_url(struct trans *, char *);
168 static char *gw_get_got_link(struct trans *);
169 static char *gw_get_site_link(struct trans *);
170 static char *gw_html_escape(const char *);
172 static void gw_display_open(struct trans *, enum khttp,
173 enum kmime);
174 static void gw_display_index(struct trans *,
175 const struct got_error *);
177 static int gw_template(size_t, void *);
179 static const struct got_error* apply_unveil(const char *, const char *);
180 static const struct got_error* cmp_tags(void *, int *,
181 struct got_reference *,
182 struct got_reference *);
183 static const struct got_error* gw_load_got_paths(struct trans *);
184 static const struct got_error* gw_load_got_path(struct trans *,
185 struct gw_dir *);
186 static const struct got_error* gw_parse_querystring(struct trans *);
187 static const struct got_error* match_logmsg(int *, struct got_object_id *,
188 struct got_commit_object *, regex_t *);
190 static const struct got_error* gw_blame(struct trans *);
191 static const struct got_error* gw_blob(struct trans *);
192 static const struct got_error* gw_blobdiff(struct trans *);
193 static const struct got_error* gw_commit(struct trans *);
194 static const struct got_error* gw_commitdiff(struct trans *);
195 static const struct got_error* gw_history(struct trans *);
196 static const struct got_error* gw_index(struct trans *);
197 static const struct got_error* gw_log(struct trans *);
198 static const struct got_error* gw_raw(struct trans *);
199 static const struct got_error* gw_logbriefs(struct trans *);
200 static const struct got_error* gw_snapshot(struct trans *);
201 static const struct got_error* gw_summary(struct trans *);
202 static const struct got_error* gw_tag(struct trans *);
203 static const struct got_error* gw_tree(struct trans *);
205 struct gw_query_action {
206 unsigned int func_id;
207 const char *func_name;
208 const struct got_error *(*func_main)(struct trans *);
209 char *template;
210 };
212 enum gw_query_actions {
213 GW_BLAME,
214 GW_BLOB,
215 GW_BLOBDIFF,
216 GW_COMMIT,
217 GW_COMMITDIFF,
218 GW_ERR,
219 GW_HISTORY,
220 GW_INDEX,
221 GW_LOG,
222 GW_RAW,
223 GW_LOGBRIEFS,
224 GW_SNAPSHOT,
225 GW_SUMMARY,
226 GW_TAG,
227 GW_TREE,
228 };
230 static struct gw_query_action gw_query_funcs[] = {
231 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
232 { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
233 { GW_BLOBDIFF, "blobdiff", gw_blobdiff, "gw_tmpl/index.tmpl" },
234 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
235 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
236 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
237 { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
238 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
239 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
240 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
241 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
242 { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
243 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
244 { GW_TAG, "tag", gw_tag, "gw_tmpl/index.tmpl" },
245 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
246 };
248 static const struct got_error *
249 apply_unveil(const char *repo_path, const char *repo_file)
251 const struct got_error *err;
253 if (repo_path && repo_file) {
254 char *full_path;
255 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
256 return got_error_from_errno("asprintf unveil");
257 if (unveil(full_path, "r") != 0)
258 return got_error_from_errno2("unveil", full_path);
261 if (repo_path && unveil(repo_path, "r") != 0)
262 return got_error_from_errno2("unveil", repo_path);
264 if (unveil("/tmp", "rwc") != 0)
265 return got_error_from_errno2("unveil", "/tmp");
267 err = got_privsep_unveil_exec_helpers();
268 if (err != NULL)
269 return err;
271 if (unveil(NULL, NULL) != 0)
272 return got_error_from_errno("unveil");
274 return NULL;
277 static const struct got_error *
278 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
279 struct got_reference *ref2)
281 const struct got_error *err = NULL;
282 struct got_repository *repo = arg;
283 struct got_object_id *id1, *id2 = NULL;
284 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
285 time_t time1, time2;
287 *cmp = 0;
289 err = got_ref_resolve(&id1, repo, ref1);
290 if (err)
291 return err;
292 err = got_object_open_as_tag(&tag1, repo, id1);
293 if (err)
294 goto done;
296 err = got_ref_resolve(&id2, repo, ref2);
297 if (err)
298 goto done;
299 err = got_object_open_as_tag(&tag2, repo, id2);
300 if (err)
301 goto done;
303 time1 = got_object_tag_get_tagger_time(tag1);
304 time2 = got_object_tag_get_tagger_time(tag2);
306 /* Put latest tags first. */
307 if (time1 < time2)
308 *cmp = 1;
309 else if (time1 > time2)
310 *cmp = -1;
311 else
312 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
313 done:
314 free(id1);
315 free(id2);
316 if (tag1)
317 got_object_tag_close(tag1);
318 if (tag2)
319 got_object_tag_close(tag2);
320 return err;
323 int
324 gw_get_repo_log_count(struct trans *gw_trans, char *start_commit)
326 const struct got_error *error;
327 struct got_repository *repo = NULL;
328 struct got_reflist_head refs;
329 struct got_commit_object *commit = NULL;
330 struct got_object_id *id = NULL;
331 struct got_commit_graph *graph = NULL;
332 char *in_repo_path = NULL, *path = NULL;
333 int log_count = 0;
335 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
336 if (error != NULL)
337 return 0;
339 SIMPLEQ_INIT(&refs);
341 if (start_commit == NULL) {
342 struct got_reference *head_ref;
343 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
344 if (error != NULL)
345 goto done;
347 error = got_ref_resolve(&id, repo, head_ref);
348 got_ref_close(head_ref);
349 if (error != NULL)
350 goto done;
352 error = got_object_open_as_commit(&commit, repo, id);
353 } else {
354 struct got_reference *ref;
355 error = got_ref_open(&ref, repo, start_commit, 0);
356 if (error == NULL) {
357 int obj_type;
358 error = got_ref_resolve(&id, repo, ref);
359 got_ref_close(ref);
360 if (error != NULL)
361 goto done;
362 error = got_object_get_type(&obj_type, repo, id);
363 if (error != NULL)
364 goto done;
365 if (obj_type == GOT_OBJ_TYPE_TAG) {
366 struct got_tag_object *tag;
367 error = got_object_open_as_tag(&tag, repo, id);
368 if (error != NULL)
369 goto done;
370 if (got_object_tag_get_object_type(tag) !=
371 GOT_OBJ_TYPE_COMMIT) {
372 got_object_tag_close(tag);
373 error = got_error(GOT_ERR_OBJ_TYPE);
374 goto done;
376 free(id);
377 id = got_object_id_dup(
378 got_object_tag_get_object_id(tag));
379 if (id == NULL)
380 error = got_error_from_errno(
381 "got_object_id_dup");
382 got_object_tag_close(tag);
383 if (error)
384 goto done;
385 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
386 error = got_error(GOT_ERR_OBJ_TYPE);
387 goto done;
389 error = got_object_open_as_commit(&commit, repo, id);
390 if (error != NULL)
391 goto done;
393 if (commit == NULL) {
394 error = got_repo_match_object_id_prefix(&id,
395 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
396 if (error != NULL)
397 goto done;
399 error = got_repo_match_object_id_prefix(&id,
400 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
401 if (error != NULL)
402 goto done;
405 error = got_object_open_as_commit(&commit, repo, id);
406 if (error != NULL)
407 goto done;
409 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
410 if (error != NULL)
411 goto done;
413 if (in_repo_path) {
414 free(path);
415 path = in_repo_path;
418 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
419 if (error)
420 goto done;
422 error = got_commit_graph_open(&graph, path, 0);
423 if (error)
424 goto done;
426 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
427 if (error)
428 goto done;
430 for (;;) {
431 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
432 NULL);
433 if (error) {
434 if (error->code == GOT_ERR_ITER_COMPLETED)
435 error = NULL;
436 break;
438 if (id == NULL)
439 break;
441 if (error)
442 break;
443 log_count++;
445 done:
446 if (graph)
447 got_commit_graph_close(graph);
448 if (repo) {
449 error = got_repo_close(repo);
450 if (error != NULL)
451 return 0;
453 if (error) {
454 khttp_puts(gw_trans->gw_req, "Error: ");
455 khttp_puts(gw_trans->gw_req, error->msg);
456 return 0;
457 } else
458 return log_count;
461 static const struct got_error *
462 gw_blame(struct trans *gw_trans)
464 const struct got_error *error = NULL;
466 return error;
469 static const struct got_error *
470 gw_blob(struct trans *gw_trans)
472 const struct got_error *error = NULL;
474 return error;
477 static const struct got_error *
478 gw_blobdiff(struct trans *gw_trans)
480 const struct got_error *error = NULL;
482 return error;
485 static const struct got_error *
486 gw_commit(struct trans *gw_trans)
488 const struct got_error *error = NULL;
489 char *log, *log_html;
491 error = apply_unveil(gw_trans->gw_dir->path, NULL);
492 if (error)
493 return error;
495 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
497 if (log != NULL && strcmp(log, "") != 0) {
498 if ((asprintf(&log_html, log_commit, log)) == -1)
499 return got_error_from_errno("asprintf");
500 khttp_puts(gw_trans->gw_req, log_html);
501 free(log_html);
502 free(log);
504 return error;
507 static const struct got_error *
508 gw_commitdiff(struct trans *gw_trans)
510 const struct got_error *error = NULL;
511 char *log, *log_html;
513 error = apply_unveil(gw_trans->gw_dir->path, NULL);
514 if (error)
515 return error;
517 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
519 if (log != NULL && strcmp(log, "") != 0) {
520 if ((asprintf(&log_html, log_diff, log)) == -1)
521 return got_error_from_errno("asprintf");
522 khttp_puts(gw_trans->gw_req, log_html);
523 free(log_html);
524 free(log);
526 return error;
529 static const struct got_error *
530 gw_history(struct trans *gw_trans)
532 const struct got_error *error = NULL;
534 return error;
537 static const struct got_error *
538 gw_index(struct trans *gw_trans)
540 const struct got_error *error = NULL;
541 struct gw_dir *gw_dir = NULL;
542 char *html, *navs, *next, *prev;
543 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
545 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
546 if (error)
547 return error;
549 error = gw_load_got_paths(gw_trans);
550 if (error)
551 return error;
553 khttp_puts(gw_trans->gw_req, index_projects_header);
555 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
556 dir_c++;
558 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
559 if (gw_trans->page > 0 && (gw_trans->page *
560 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
561 prev_disp++;
562 continue;
565 prev_disp++;
566 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
567 gw_dir->name, gw_dir->name)) == -1)
568 return got_error_from_errno("asprintf");
570 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
571 gw_dir->description, gw_dir->owner, gw_dir->age,
572 navs)) == -1)
573 return got_error_from_errno("asprintf");
575 khttp_puts(gw_trans->gw_req, html);
577 free(navs);
578 free(html);
580 if (gw_trans->gw_conf->got_max_repos_display == 0)
581 continue;
583 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
584 khttp_puts(gw_trans->gw_req, np_wrapper_start);
585 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
586 (gw_trans->page > 0) &&
587 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
588 prev_disp == gw_trans->repos_total))
589 khttp_puts(gw_trans->gw_req, np_wrapper_start);
591 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
592 (gw_trans->page > 0) &&
593 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
594 prev_disp == gw_trans->repos_total)) {
595 if ((asprintf(&prev, nav_prev,
596 gw_trans->page - 1)) == -1)
597 return got_error_from_errno("asprintf");
598 khttp_puts(gw_trans->gw_req, prev);
599 free(prev);
602 khttp_puts(gw_trans->gw_req, div_end);
604 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
605 next_disp == gw_trans->gw_conf->got_max_repos_display &&
606 dir_c != (gw_trans->page + 1) *
607 gw_trans->gw_conf->got_max_repos_display) {
608 if ((asprintf(&next, nav_next,
609 gw_trans->page + 1)) == -1)
610 return got_error_from_errno("calloc");
611 khttp_puts(gw_trans->gw_req, next);
612 khttp_puts(gw_trans->gw_req, div_end);
613 free(next);
614 next_disp = 0;
615 break;
618 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
619 (gw_trans->page > 0) &&
620 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
621 prev_disp == gw_trans->repos_total))
622 khttp_puts(gw_trans->gw_req, div_end);
624 next_disp++;
626 return error;
629 static const struct got_error *
630 gw_log(struct trans *gw_trans)
632 const struct got_error *error = NULL;
633 char *log, *log_html;
635 error = apply_unveil(gw_trans->gw_dir->path, NULL);
636 if (error)
637 return error;
639 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
640 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
642 if (log != NULL && strcmp(log, "") != 0) {
643 if ((asprintf(&log_html, logs, log)) == -1)
644 return got_error_from_errno("asprintf");
645 khttp_puts(gw_trans->gw_req, log_html);
646 free(log_html);
647 free(log);
649 return error;
652 static const struct got_error *
653 gw_raw(struct trans *gw_trans)
655 const struct got_error *error = NULL;
657 return error;
660 static const struct got_error *
661 gw_logbriefs(struct trans *gw_trans)
663 const struct got_error *error = NULL;
664 char *log, *log_html;
666 error = apply_unveil(gw_trans->gw_dir->path, NULL);
667 if (error)
668 return error;
670 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
671 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
673 if (log != NULL && strcmp(log, "") != 0) {
674 if ((asprintf(&log_html, summary_logbriefs,
675 log)) == -1)
676 return got_error_from_errno("asprintf");
677 khttp_puts(gw_trans->gw_req, log_html);
678 free(log_html);
679 free(log);
681 return error;
684 static const struct got_error *
685 gw_snapshot(struct trans *gw_trans)
687 const struct got_error *error = NULL;
689 return error;
692 static const struct got_error *
693 gw_summary(struct trans *gw_trans)
695 const struct got_error *error = NULL;
696 char *description_html, *repo_owner_html, *repo_age_html,
697 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
698 *heads_html, *age;
700 error = apply_unveil(gw_trans->gw_dir->path, NULL);
701 if (error)
702 return error;
704 khttp_puts(gw_trans->gw_req, summary_wrapper);
705 if (gw_trans->gw_conf->got_show_repo_description) {
706 if (gw_trans->gw_dir->description != NULL &&
707 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
708 if ((asprintf(&description_html, description,
709 gw_trans->gw_dir->description)) == -1)
710 return got_error_from_errno("asprintf");
712 khttp_puts(gw_trans->gw_req, description_html);
713 free(description_html);
717 if (gw_trans->gw_conf->got_show_repo_owner) {
718 if (gw_trans->gw_dir->owner != NULL &&
719 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
720 if ((asprintf(&repo_owner_html, repo_owner,
721 gw_trans->gw_dir->owner)) == -1)
722 return got_error_from_errno("asprintf");
724 khttp_puts(gw_trans->gw_req, repo_owner_html);
725 free(repo_owner_html);
729 if (gw_trans->gw_conf->got_show_repo_age) {
730 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
731 "refs/heads", TM_LONG);
732 if (age != NULL && (strcmp(age, "") != 0)) {
733 if ((asprintf(&repo_age_html, last_change, age)) == -1)
734 return got_error_from_errno("asprintf");
736 khttp_puts(gw_trans->gw_req, repo_age_html);
737 free(repo_age_html);
738 free(age);
742 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
743 if (gw_trans->gw_dir->url != NULL &&
744 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
745 if ((asprintf(&cloneurl_html, cloneurl,
746 gw_trans->gw_dir->url)) == -1)
747 return got_error_from_errno("asprintf");
749 khttp_puts(gw_trans->gw_req, cloneurl_html);
750 free(cloneurl_html);
753 khttp_puts(gw_trans->gw_req, div_end);
755 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
756 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
757 heads = gw_get_repo_heads(gw_trans);
759 if (log != NULL && strcmp(log, "") != 0) {
760 if ((asprintf(&log_html, summary_logbriefs,
761 log)) == -1)
762 return got_error_from_errno("asprintf");
763 khttp_puts(gw_trans->gw_req, log_html);
764 free(log_html);
765 free(log);
768 if (tags != NULL && strcmp(tags, "") != 0) {
769 if ((asprintf(&tags_html, summary_tags,
770 tags)) == -1)
771 return got_error_from_errno("asprintf");
772 khttp_puts(gw_trans->gw_req, tags_html);
773 free(tags_html);
774 free(tags);
777 if (heads != NULL && strcmp(heads, "") != 0) {
778 if ((asprintf(&heads_html, summary_heads,
779 heads)) == -1)
780 return got_error_from_errno("asprintf");
781 khttp_puts(gw_trans->gw_req, heads_html);
782 free(heads_html);
783 free(heads);
785 return error;
788 static const struct got_error *
789 gw_tag(struct trans *gw_trans)
791 const struct got_error *error = NULL;
792 char *log, *log_html;
794 error = apply_unveil(gw_trans->gw_dir->path, NULL);
795 if (error)
796 return error;
798 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTAG);
800 if (log != NULL && strcmp(log, "") != 0) {
801 if ((asprintf(&log_html, log_tag, log)) == -1)
802 return got_error_from_errno("asprintf");
803 khttp_puts(gw_trans->gw_req, log_html);
804 free(log_html);
805 free(log);
807 return error;
810 static const struct got_error *
811 gw_tree(struct trans *gw_trans)
813 const struct got_error *error = NULL;
814 char *log, *log_html;
816 error = apply_unveil(gw_trans->gw_dir->path, NULL);
817 if (error)
818 return error;
820 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
822 if (log != NULL && strcmp(log, "") != 0) {
823 if ((asprintf(&log_html, log_tree, log)) == -1)
824 return got_error_from_errno("asprintf");
825 khttp_puts(gw_trans->gw_req, log_html);
826 free(log_html);
827 free(log);
829 return error;
832 static const struct got_error *
833 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
835 const struct got_error *error = NULL;
836 DIR *dt;
837 char *dir_test;
838 int opened = 0;
840 if ((asprintf(&dir_test, "%s/%s/%s",
841 gw_trans->gw_conf->got_repos_path, gw_dir->name,
842 GOTWEB_GIT_DIR)) == -1)
843 return got_error_from_errno("asprintf");
845 dt = opendir(dir_test);
846 if (dt == NULL) {
847 free(dir_test);
848 } else {
849 gw_dir->path = strdup(dir_test);
850 opened = 1;
851 goto done;
854 if ((asprintf(&dir_test, "%s/%s/%s",
855 gw_trans->gw_conf->got_repos_path, gw_dir->name,
856 GOTWEB_GOT_DIR)) == -1)
857 return got_error_from_errno("asprintf");
859 dt = opendir(dir_test);
860 if (dt == NULL)
861 free(dir_test);
862 else {
863 opened = 1;
864 error = got_error(GOT_ERR_NOT_GIT_REPO);
865 goto errored;
868 if ((asprintf(&dir_test, "%s/%s",
869 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
870 return got_error_from_errno("asprintf");
872 gw_dir->path = strdup(dir_test);
874 done:
875 gw_dir->description = gw_get_repo_description(gw_trans,
876 gw_dir->path);
877 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
878 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
879 TM_DIFF);
880 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
882 errored:
883 free(dir_test);
884 if (opened)
885 closedir(dt);
886 return error;
889 static const struct got_error *
890 gw_load_got_paths(struct trans *gw_trans)
892 const struct got_error *error = NULL;
893 DIR *d;
894 struct dirent **sd_dent;
895 struct gw_dir *gw_dir;
896 struct stat st;
897 unsigned int d_cnt, d_i;
899 d = opendir(gw_trans->gw_conf->got_repos_path);
900 if (d == NULL) {
901 error = got_error_from_errno2("opendir",
902 gw_trans->gw_conf->got_repos_path);
903 return error;
906 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
907 alphasort);
908 if (d_cnt == -1) {
909 error = got_error_from_errno2("scandir",
910 gw_trans->gw_conf->got_repos_path);
911 return error;
914 for (d_i = 0; d_i < d_cnt; d_i++) {
915 if (gw_trans->gw_conf->got_max_repos > 0 &&
916 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
917 break; /* account for parent and self */
919 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
920 strcmp(sd_dent[d_i]->d_name, "..") == 0)
921 continue;
923 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
924 return got_error_from_errno("gw_dir malloc");
926 error = gw_load_got_path(gw_trans, gw_dir);
927 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
928 continue;
929 else if (error)
930 return error;
932 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
933 !got_path_dir_is_empty(gw_dir->path)) {
934 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
935 entry);
936 gw_trans->repos_total++;
940 closedir(d);
941 return error;
944 static const struct got_error *
945 gw_parse_querystring(struct trans *gw_trans)
947 const struct got_error *error = NULL;
948 struct kpair *p;
949 struct gw_query_action *action = NULL;
950 unsigned int i;
952 if (gw_trans->gw_req->fieldnmap[0]) {
953 error = got_error_from_errno("bad parse");
954 return error;
955 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
956 /* define gw_trans->repo_path */
957 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
958 return got_error_from_errno("asprintf");
960 if ((asprintf(&gw_trans->repo_path, "%s/%s",
961 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
962 return got_error_from_errno("asprintf");
964 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
965 if ((asprintf(&gw_trans->commit, "%s",
966 p->parsed.s)) == -1)
967 return got_error_from_errno("asprintf");
969 /* get action and set function */
970 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
971 for (i = 0; i < nitems(gw_query_funcs); i++) {
972 action = &gw_query_funcs[i];
973 if (action->func_name == NULL)
974 continue;
976 if (strcmp(action->func_name,
977 p->parsed.s) == 0) {
978 gw_trans->action = i;
979 if ((asprintf(&gw_trans->action_name,
980 "%s", action->func_name)) == -1)
981 return
982 got_error_from_errno(
983 "asprintf");
985 break;
988 action = NULL;
991 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
992 if ((asprintf(&gw_trans->repo_file, "%s",
993 p->parsed.s)) == -1)
994 return got_error_from_errno("asprintf");
996 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
997 if ((asprintf(&gw_trans->headref, "%s",
998 p->parsed.s)) == -1)
999 return got_error_from_errno("asprintf");
1001 if (action == NULL) {
1002 error = got_error_from_errno("invalid action");
1003 return error;
1005 if ((gw_trans->gw_dir =
1006 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1007 return got_error_from_errno("gw_dir malloc");
1009 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1010 if (error)
1011 return error;
1012 } else
1013 gw_trans->action = GW_INDEX;
1015 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1016 gw_trans->page = p->parsed.i;
1018 if (gw_trans->action == GW_RAW)
1019 gw_trans->mime = KMIME_TEXT_PLAIN;
1021 return error;
1024 static struct gw_dir *
1025 gw_init_gw_dir(char *dir)
1027 struct gw_dir *gw_dir;
1029 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1030 return NULL;
1032 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1033 return NULL;
1035 return gw_dir;
1038 static const struct got_error*
1039 match_logmsg(int *have_match, struct got_object_id *id,
1040 struct got_commit_object *commit, regex_t *regex)
1042 const struct got_error *err = NULL;
1043 regmatch_t regmatch;
1044 char *id_str = NULL, *logmsg = NULL;
1046 *have_match = 0;
1048 err = got_object_id_str(&id_str, id);
1049 if (err)
1050 return err;
1052 err = got_object_commit_get_logmsg(&logmsg, commit);
1053 if (err)
1054 goto done;
1056 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1057 *have_match = 1;
1058 done:
1059 free(id_str);
1060 free(logmsg);
1061 return err;
1064 static void
1065 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
1067 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1068 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1069 khttps[code]);
1070 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1071 kmimetypes[mime]);
1072 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1073 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1074 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1075 khttp_body(gw_trans->gw_req);
1078 static void
1079 gw_display_index(struct trans *gw_trans, const struct got_error *err)
1081 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1082 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1084 if (err)
1085 khttp_puts(gw_trans->gw_req, err->msg);
1086 else
1087 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1088 gw_query_funcs[gw_trans->action].template);
1090 khtml_close(gw_trans->gw_html_req);
1093 static int
1094 gw_template(size_t key, void *arg)
1096 const struct got_error *error = NULL;
1097 struct trans *gw_trans = arg;
1098 char *gw_got_link, *gw_site_link;
1099 char *site_owner_name, *site_owner_name_h;
1101 switch (key) {
1102 case (TEMPL_HEAD):
1103 khttp_puts(gw_trans->gw_req, head);
1104 break;
1105 case(TEMPL_HEADER):
1106 gw_got_link = gw_get_got_link(gw_trans);
1107 if (gw_got_link != NULL)
1108 khttp_puts(gw_trans->gw_req, gw_got_link);
1110 free(gw_got_link);
1111 break;
1112 case (TEMPL_SITEPATH):
1113 gw_site_link = gw_get_site_link(gw_trans);
1114 if (gw_site_link != NULL)
1115 khttp_puts(gw_trans->gw_req, gw_site_link);
1117 free(gw_site_link);
1118 break;
1119 case(TEMPL_TITLE):
1120 if (gw_trans->gw_conf->got_site_name != NULL)
1121 khtml_puts(gw_trans->gw_html_req,
1122 gw_trans->gw_conf->got_site_name);
1124 break;
1125 case (TEMPL_SEARCH):
1126 khttp_puts(gw_trans->gw_req, search);
1127 break;
1128 case(TEMPL_SITEOWNER):
1129 if (gw_trans->gw_conf->got_site_owner != NULL &&
1130 gw_trans->gw_conf->got_show_site_owner) {
1131 site_owner_name =
1132 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1133 if ((asprintf(&site_owner_name_h, site_owner,
1134 site_owner_name))
1135 == -1)
1136 return 0;
1138 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1139 free(site_owner_name);
1140 free(site_owner_name_h);
1142 break;
1143 case(TEMPL_CONTENT):
1144 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1145 if (error)
1146 khttp_puts(gw_trans->gw_req, error->msg);
1148 break;
1149 default:
1150 return 0;
1151 break;
1153 return 1;
1156 static char *
1157 gw_get_repo_description(struct trans *gw_trans, char *dir)
1159 FILE *f;
1160 char *description = NULL, *d_file = NULL;
1161 unsigned int len;
1163 if (gw_trans->gw_conf->got_show_repo_description == false)
1164 goto err;
1166 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1167 goto err;
1169 if ((f = fopen(d_file, "r")) == NULL)
1170 goto err;
1172 fseek(f, 0, SEEK_END);
1173 len = ftell(f) + 1;
1174 fseek(f, 0, SEEK_SET);
1175 if ((description = calloc(len, sizeof(char *))) == NULL)
1176 goto err;
1178 fread(description, 1, len, f);
1179 fclose(f);
1180 free(d_file);
1181 return description;
1182 err:
1183 if ((asprintf(&description, "%s", "")) == -1)
1184 return NULL;
1186 return description;
1189 static char *
1190 gw_get_time_str(time_t committer_time, int ref_tm)
1192 struct tm tm;
1193 time_t diff_time;
1194 char *years = "years ago", *months = "months ago";
1195 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1196 char *minutes = "minutes ago", *seconds = "seconds ago";
1197 char *now = "right now";
1198 char *repo_age, *s;
1199 char datebuf[29];
1201 switch (ref_tm) {
1202 case TM_DIFF:
1203 diff_time = time(NULL) - committer_time;
1204 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1205 if ((asprintf(&repo_age, "%lld %s",
1206 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1207 return NULL;
1208 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1209 if ((asprintf(&repo_age, "%lld %s",
1210 (diff_time / 60 / 60 / 24 / (365 / 12)),
1211 months)) == -1)
1212 return NULL;
1213 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1214 if ((asprintf(&repo_age, "%lld %s",
1215 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1216 return NULL;
1217 } else if (diff_time > 60 * 60 * 24 * 2) {
1218 if ((asprintf(&repo_age, "%lld %s",
1219 (diff_time / 60 / 60 / 24), days)) == -1)
1220 return NULL;
1221 } else if (diff_time > 60 * 60 * 2) {
1222 if ((asprintf(&repo_age, "%lld %s",
1223 (diff_time / 60 / 60), hours)) == -1)
1224 return NULL;
1225 } else if (diff_time > 60 * 2) {
1226 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1227 minutes)) == -1)
1228 return NULL;
1229 } else if (diff_time > 2) {
1230 if ((asprintf(&repo_age, "%lld %s", diff_time,
1231 seconds)) == -1)
1232 return NULL;
1233 } else {
1234 if ((asprintf(&repo_age, "%s", now)) == -1)
1235 return NULL;
1237 break;
1238 case TM_LONG:
1239 if (gmtime_r(&committer_time, &tm) == NULL)
1240 return NULL;
1242 s = asctime_r(&tm, datebuf);
1243 if (s == NULL)
1244 return NULL;
1246 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1247 return NULL;
1248 break;
1250 return repo_age;
1253 static char *
1254 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
1256 const struct got_error *error = NULL;
1257 struct got_object_id *id = NULL;
1258 struct got_repository *repo = NULL;
1259 struct got_commit_object *commit = NULL;
1260 struct got_reflist_head refs;
1261 struct got_reflist_entry *re;
1262 struct got_reference *head_ref;
1263 int is_head = 0;
1264 time_t committer_time = 0, cmp_time = 0;
1265 const char *refname;
1266 char *repo_age = NULL;
1268 if (repo_ref == NULL)
1269 return NULL;
1271 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1272 is_head = 1;
1274 SIMPLEQ_INIT(&refs);
1275 if (gw_trans->gw_conf->got_show_repo_age == false) {
1276 if ((asprintf(&repo_age, "")) == -1)
1277 return NULL;
1278 return repo_age;
1281 error = got_repo_open(&repo, dir, NULL);
1282 if (error != NULL)
1283 goto err;
1285 if (is_head)
1286 error = got_ref_list(&refs, repo, "refs/heads",
1287 got_ref_cmp_by_name, NULL);
1288 else
1289 error = got_ref_list(&refs, repo, repo_ref,
1290 got_ref_cmp_by_name, NULL);
1291 if (error != NULL)
1292 goto err;
1294 SIMPLEQ_FOREACH(re, &refs, entry) {
1295 if (is_head)
1296 refname = strdup(repo_ref);
1297 else
1298 refname = got_ref_get_name(re->ref);
1299 error = got_ref_open(&head_ref, repo, refname, 0);
1300 if (error != NULL)
1301 goto err;
1303 error = got_ref_resolve(&id, repo, head_ref);
1304 got_ref_close(head_ref);
1305 if (error != NULL)
1306 goto err;
1308 error = got_object_open_as_commit(&commit, repo, id);
1309 if (error != NULL)
1310 goto err;
1312 committer_time =
1313 got_object_commit_get_committer_time(commit);
1315 if (cmp_time < committer_time)
1316 cmp_time = committer_time;
1319 if (cmp_time != 0) {
1320 committer_time = cmp_time;
1321 repo_age = gw_get_time_str(committer_time, ref_tm);
1322 } else
1323 if ((asprintf(&repo_age, "")) == -1)
1324 return NULL;
1325 got_ref_list_free(&refs);
1326 free(id);
1327 return repo_age;
1328 err:
1329 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1330 return NULL;
1332 return repo_age;
1335 static char *
1336 gw_get_repo_owner(struct trans *gw_trans, char *dir)
1338 FILE *f;
1339 char *owner = NULL, *d_file = NULL;
1340 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1341 char *comp, *pos, *buf;
1342 unsigned int i;
1344 if (gw_trans->gw_conf->got_show_repo_owner == false)
1345 goto err;
1347 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1348 goto err;
1350 if ((f = fopen(d_file, "r")) == NULL)
1351 goto err;
1353 if ((buf = calloc(128, sizeof(char *))) == NULL)
1354 goto err;
1356 while ((fgets(buf, 128, f)) != NULL) {
1357 if ((pos = strstr(buf, gotweb)) != NULL)
1358 break;
1360 if ((pos = strstr(buf, gitweb)) != NULL)
1361 break;
1364 if (pos == NULL)
1365 goto err;
1367 do {
1368 fgets(buf, 128, f);
1369 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1371 if (comp == NULL)
1372 goto err;
1374 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1375 goto err;
1377 for (i = 0; i < 2; i++) {
1378 owner = strsep(&buf, "\"");
1381 if (owner == NULL)
1382 goto err;
1384 fclose(f);
1385 free(d_file);
1386 return owner;
1387 err:
1388 if ((asprintf(&owner, "%s", "")) == -1)
1389 return NULL;
1391 return owner;
1394 static char *
1395 gw_get_clone_url(struct trans *gw_trans, char *dir)
1397 FILE *f;
1398 char *url = NULL, *d_file = NULL;
1399 unsigned int len;
1401 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1402 return NULL;
1404 if ((f = fopen(d_file, "r")) == NULL)
1405 return NULL;
1407 fseek(f, 0, SEEK_END);
1408 len = ftell(f) + 1;
1409 fseek(f, 0, SEEK_SET);
1411 if ((url = calloc(len, sizeof(char *))) == NULL)
1412 return NULL;
1414 fread(url, 1, len, f);
1415 fclose(f);
1416 free(d_file);
1417 return url;
1420 static char *
1421 gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1422 char *start_commit, int limit, int log_type)
1424 const struct got_error *error;
1425 struct got_repository *repo = NULL;
1426 struct got_reflist_head refs;
1427 struct got_reflist_entry *re;
1428 struct got_commit_object *commit = NULL;
1429 struct got_object_id *id1 = NULL, *id2 = NULL;
1430 struct got_object_qid *parent_id;
1431 struct got_commit_graph *graph = NULL;
1432 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1433 *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1434 *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1435 *commit_commit_disp = NULL, *commit_age_diff = NULL,
1436 *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1437 *commit_age_long_disp = NULL, *commit_author = NULL,
1438 *commit_author_disp = NULL, *commit_committer = NULL,
1439 *commit_committer_disp = NULL, *commit_log = NULL,
1440 *commit_log_disp = NULL, *commit_parent = NULL,
1441 *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1442 *log_tree_html = NULL, *log_commit_html = NULL,
1443 *log_diff_html = NULL, *commit_tree = NULL,
1444 *commit_tree_disp = NULL, *log_tag_html = NULL;
1445 char *commit_log0, *newline;
1446 regex_t regex;
1447 int have_match, log_count = 0;
1448 size_t newsize;
1449 struct buf *diffbuf = NULL;
1450 time_t committer_time;
1452 if (gw_trans->action == GW_LOG || gw_trans->action == GW_LOGBRIEFS)
1453 log_count = gw_get_repo_log_count(gw_trans, start_commit);
1455 error = buf_alloc(&diffbuf, 0);
1456 if (error != NULL)
1457 return NULL;
1459 if (search_pattern &&
1460 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB |
1461 REG_NEWLINE))
1462 return NULL;
1464 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1465 if (error != NULL)
1466 return NULL;
1468 SIMPLEQ_INIT(&refs);
1470 if (start_commit == NULL) {
1471 struct got_reference *head_ref;
1472 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1473 if (error != NULL)
1474 goto done;
1476 error = got_ref_resolve(&id1, repo, head_ref);
1477 got_ref_close(head_ref);
1478 if (error != NULL)
1479 goto done;
1481 error = got_object_open_as_commit(&commit, repo, id1);
1482 } else {
1483 struct got_reference *ref;
1484 error = got_ref_open(&ref, repo, start_commit, 0);
1485 if (error == NULL) {
1486 int obj_type;
1487 error = got_ref_resolve(&id1, repo, ref);
1488 got_ref_close(ref);
1489 if (error != NULL)
1490 goto done;
1491 error = got_object_get_type(&obj_type, repo, id1);
1492 if (error != NULL)
1493 goto done;
1494 if (obj_type == GOT_OBJ_TYPE_TAG) {
1495 struct got_tag_object *tag;
1496 error = got_object_open_as_tag(&tag, repo, id1);
1497 if (error != NULL)
1498 goto done;
1499 if (got_object_tag_get_object_type(tag) !=
1500 GOT_OBJ_TYPE_COMMIT) {
1501 got_object_tag_close(tag);
1502 error = got_error(GOT_ERR_OBJ_TYPE);
1503 goto done;
1505 free(id1);
1506 id1 = got_object_id_dup(
1507 got_object_tag_get_object_id(tag));
1508 if (id1 == NULL)
1509 error = got_error_from_errno(
1510 "got_object_id_dup");
1511 got_object_tag_close(tag);
1512 if (error)
1513 goto done;
1514 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1515 error = got_error(GOT_ERR_OBJ_TYPE);
1516 goto done;
1518 error = got_object_open_as_commit(&commit, repo, id1);
1519 if (error != NULL)
1520 goto done;
1522 if (commit == NULL) {
1523 error = got_repo_match_object_id_prefix(&id1,
1524 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1525 if (error != NULL)
1526 goto done;
1528 error = got_repo_match_object_id_prefix(&id1,
1529 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1532 if (error != NULL)
1533 goto done;
1535 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1536 if (error != NULL)
1537 goto done;
1539 if (in_repo_path) {
1540 free(path);
1541 path = in_repo_path;
1544 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1545 if (error)
1546 goto done;
1548 error = got_commit_graph_open(&graph, path, 0);
1549 if (error)
1550 goto done;
1552 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1553 if (error)
1554 goto done;
1556 for (;;) {
1557 error = got_commit_graph_iter_next(&id1, graph, repo, NULL,
1558 NULL);
1559 if (error) {
1560 if (error->code == GOT_ERR_ITER_COMPLETED)
1561 error = NULL;
1562 break;
1564 if (id1 == NULL)
1565 break;
1567 error = got_object_open_as_commit(&commit, repo, id1);
1568 if (error)
1569 break;
1571 if (search_pattern) {
1572 error = match_logmsg(&have_match, id1, commit,
1573 &regex);
1574 if (error) {
1575 got_object_commit_close(commit);
1576 break;
1578 if (have_match == 0) {
1579 got_object_commit_close(commit);
1580 continue;
1584 SIMPLEQ_FOREACH(re, &refs, entry) {
1585 char *s;
1586 const char *name;
1587 struct got_tag_object *tag = NULL;
1588 int cmp;
1590 name = got_ref_get_name(re->ref);
1591 if (strcmp(name, GOT_REF_HEAD) == 0)
1592 continue;
1593 if (strncmp(name, "refs/", 5) == 0)
1594 name += 5;
1595 if (strncmp(name, "got/", 4) == 0)
1596 continue;
1597 if (strncmp(name, "heads/", 6) == 0)
1598 name += 6;
1599 if (strncmp(name, "remotes/", 8) == 0)
1600 name += 8;
1601 if (strncmp(name, "tags/", 5) == 0) {
1602 error = got_object_open_as_tag(&tag, repo,
1603 re->id);
1604 if (error) {
1605 if (error->code != GOT_ERR_OBJ_TYPE)
1606 continue;
1608 * Ref points at something other
1609 * than a tag.
1611 error = NULL;
1612 tag = NULL;
1615 cmp = got_object_id_cmp(tag ?
1616 got_object_tag_get_object_id(tag) : re->id, id1);
1617 if (tag)
1618 got_object_tag_close(tag);
1619 if (cmp != 0)
1620 continue;
1621 s = refs_str;
1622 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1623 s ? ", " : "", name)) == -1) {
1624 error = got_error_from_errno("asprintf");
1625 free(s);
1626 goto done;
1628 free(s);
1631 if (refs_str == NULL)
1632 refs_str_disp = strdup("");
1633 else {
1634 if ((asprintf(&refs_str_disp, "(%s)",
1635 refs_str)) == -1) {
1636 error = got_error_from_errno("asprintf");
1637 free(refs_str);
1638 goto done;
1642 error = got_object_id_str(&id_str1, id1);
1643 if (error)
1644 goto done;
1646 error = got_object_id_str(&treeid,
1647 got_object_commit_get_tree_id(commit));
1648 if (error)
1649 goto done;
1651 if (gw_trans->action == GW_COMMIT ||
1652 gw_trans->action == GW_COMMITDIFF) {
1653 parent_id =
1654 SIMPLEQ_FIRST(
1655 got_object_commit_get_parent_ids(commit));
1656 if (parent_id != NULL) {
1657 id2 = got_object_id_dup(parent_id->id);
1658 free (parent_id);
1659 error = got_object_id_str(&id_str2, id2);
1660 if (error)
1661 goto done;
1662 free(id2);
1663 } else
1664 id_str2 = strdup("/dev/null");
1667 committer_time =
1668 got_object_commit_get_committer_time(commit);
1670 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1671 error = got_error_from_errno("asprintf");
1672 goto done;
1675 if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1676 error = got_error_from_errno("asprintf");
1677 goto done;
1680 if ((asprintf(&commit_tree_disp, commit_tree_html,
1681 treeid)) == -1) {
1682 error = got_error_from_errno("asprintf");
1683 goto done;
1686 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1687 id_str1)) == -1) {
1688 error = got_error_from_errno("asprintf");
1689 goto done;
1692 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1693 error = got_error_from_errno("asprintf");
1694 goto done;
1697 if ((asprintf(&commit_commit_disp, commit_commit_html,
1698 commit_commit, refs_str_disp)) == -1) {
1699 error = got_error_from_errno("asprintf");
1700 goto done;
1703 if ((asprintf(&commit_age_long, "%s",
1704 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1705 error = got_error_from_errno("asprintf");
1706 goto done;
1709 if ((asprintf(&commit_age_long_disp, commit_age_html,
1710 commit_age_long)) == -1) {
1711 error = got_error_from_errno("asprintf");
1712 goto done;
1715 if ((asprintf(&commit_age_diff, "%s",
1716 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1717 error = got_error_from_errno("asprintf");
1718 goto done;
1721 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1722 commit_age_diff)) == -1) {
1723 error = got_error_from_errno("asprintf");
1724 goto done;
1727 if ((asprintf(&commit_author, "%s",
1728 got_object_commit_get_author(commit))) == -1) {
1729 error = got_error_from_errno("asprintf");
1730 goto done;
1733 if ((asprintf(&commit_author_disp, commit_author_html,
1734 gw_html_escape(commit_author))) == -1) {
1735 error = got_error_from_errno("asprintf");
1736 goto done;
1739 if ((asprintf(&commit_committer, "%s",
1740 got_object_commit_get_committer(commit))) == -1) {
1741 error = got_error_from_errno("asprintf");
1742 goto done;
1745 if ((asprintf(&commit_committer_disp, commit_committer_html,
1746 gw_html_escape(commit_committer))) == -1) {
1747 error = got_error_from_errno("asprintf");
1748 goto done;
1751 if (strcmp(commit_author, commit_committer) == 0) {
1752 free(commit_committer_disp);
1753 commit_committer_disp = strdup("");
1756 error = got_object_commit_get_logmsg(&commit_log0, commit);
1757 if (error)
1758 goto done;
1760 commit_log = commit_log0;
1761 while (*commit_log == '\n')
1762 commit_log++;
1764 switch(log_type) {
1765 case (LOGBRIEF):
1766 newline = strchr(commit_log, '\n');
1767 if (newline)
1768 *newline = '\0';
1770 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1771 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1772 id_str1, gw_trans->repo_name, id_str1,
1773 gw_trans->repo_name, id_str1)) == -1) {
1774 error = got_error_from_errno("asprintf");
1775 goto done;
1778 if ((asprintf(&commit_row, logbriefs_row,
1779 commit_age_diff, commit_author, commit_log,
1780 logbriefs_navs_html)) == -1) {
1781 error = got_error_from_errno("asprintf");
1782 goto done;
1785 free(logbriefs_navs_html);
1786 logbriefs_navs_html = NULL;
1787 break;
1788 case (LOGFULL):
1789 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1790 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1791 id_str1, gw_trans->repo_name, id_str1,
1792 gw_trans->repo_name, id_str1)) == -1) {
1793 error = got_error_from_errno("asprintf");
1794 goto done;
1797 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1798 commit_author_disp, commit_committer_disp,
1799 commit_age_long_disp, gw_html_escape(commit_log),
1800 logbriefs_navs_html)) == -1) {
1801 error = got_error_from_errno("asprintf");
1802 goto done;
1805 free(logbriefs_navs_html);
1806 logbriefs_navs_html = NULL;
1807 break;
1808 case (LOGTAG):
1809 log_tag_html = strdup("tag log here");
1811 if ((asprintf(&commit_row, log_tag_row,
1812 gw_html_escape(commit_log), log_tag_html)) == -1) {
1813 error = got_error_from_errno("asprintf");
1814 goto done;
1817 free(log_tag_html);
1818 break;
1819 case (LOGTREE):
1820 log_tree_html = strdup("log tree here");
1822 if ((asprintf(&commit_row, log_tree_row,
1823 gw_html_escape(commit_log), log_tree_html)) == -1) {
1824 error = got_error_from_errno("asprintf");
1825 goto done;
1828 free(log_tree_html);
1829 break;
1830 case (LOGCOMMIT):
1831 if ((asprintf(&commit_log_disp, commit_log_html,
1832 gw_html_escape(commit_log))) == -1) {
1833 error = got_error_from_errno("asprintf");
1834 goto done;
1837 log_commit_html = strdup("commit here");
1839 if ((asprintf(&commit_row, log_commit_row,
1840 commit_diff_disp, commit_commit_disp,
1841 commit_tree_disp, commit_author_disp,
1842 commit_committer_disp, commit_age_long_disp,
1843 commit_log_disp, log_commit_html)) == -1) {
1844 error = got_error_from_errno("asprintf");
1845 goto done;
1847 free(commit_log_disp);
1848 free(log_commit_html);
1850 break;
1851 case (LOGDIFF):
1852 if ((asprintf(&commit_log_disp, commit_log_html,
1853 gw_html_escape(commit_log))) == -1) {
1854 error = got_error_from_errno("asprintf");
1855 goto done;
1858 log_diff_html = strdup("diff here");
1860 if ((asprintf(&commit_row, log_diff_row,
1861 commit_diff_disp, commit_commit_disp,
1862 commit_tree_disp, commit_author_disp,
1863 commit_committer_disp, commit_age_long_disp,
1864 commit_log_disp, log_diff_html)) == -1) {
1865 error = got_error_from_errno("asprintf");
1866 goto done;
1868 free(commit_log_disp);
1869 free(log_diff_html);
1871 break;
1872 default:
1873 return NULL;
1876 error = buf_puts(&newsize, diffbuf, commit_row);
1878 free(commit_parent);
1879 free(commit_diff_disp);
1880 free(commit_tree_disp);
1881 free(commit_age_diff);
1882 free(commit_age_diff_disp);
1883 free(commit_age_long);
1884 free(commit_age_long_disp);
1885 free(commit_author);
1886 free(commit_author_disp);
1887 free(commit_committer);
1888 free(commit_committer_disp);
1889 free(commit_log0);
1890 free(commit_row);
1891 free(refs_str_disp);
1892 free(refs_str);
1893 refs_str = NULL;
1894 free(id_str1);
1895 id_str1 = NULL;
1896 free(id_str2);
1897 id_str2 = NULL;
1899 if (error || (limit && --limit == 0))
1900 break;
1903 if (error)
1904 goto done;
1906 if (buf_len(diffbuf) > 0) {
1907 error = buf_putc(diffbuf, '\0');
1908 logs = strdup(buf_get(diffbuf));
1910 done:
1911 buf_free(diffbuf);
1912 if (commit != NULL)
1913 got_object_commit_close(commit);
1914 if (search_pattern)
1915 regfree(&regex);
1916 if (graph)
1917 got_commit_graph_close(graph);
1918 if (repo) {
1919 error = got_repo_close(repo);
1920 if (error != NULL)
1921 return NULL;
1923 if (error) {
1924 khttp_puts(gw_trans->gw_req, "Error: ");
1925 khttp_puts(gw_trans->gw_req, error->msg);
1926 return NULL;
1927 } else
1928 return logs;
1931 static char *
1932 gw_get_repo_tags(struct trans *gw_trans, int limit, int tag_type)
1934 const struct got_error *error = NULL;
1935 struct got_repository *repo = NULL;
1936 struct got_reflist_head refs;
1937 struct got_reflist_entry *re;
1938 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1939 *age = NULL;
1940 char *newline;
1941 struct buf *diffbuf = NULL;
1942 size_t newsize;
1944 error = buf_alloc(&diffbuf, 0);
1945 if (error != NULL)
1946 return NULL;
1947 SIMPLEQ_INIT(&refs);
1949 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1950 if (error != NULL)
1951 goto done;
1953 error = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
1954 if (error)
1955 goto done;
1957 SIMPLEQ_FOREACH(re, &refs, entry) {
1958 const char *refname;
1959 char *refstr, *tag_log0, *tag_log, *id_str;
1960 time_t tagger_time;
1961 struct got_object_id *id;
1962 struct got_tag_object *tag;
1964 refname = got_ref_get_name(re->ref);
1965 if (strncmp(refname, "refs/tags/", 10) != 0)
1966 continue;
1967 refname += 10;
1968 refstr = got_ref_to_str(re->ref);
1969 if (refstr == NULL) {
1970 error = got_error_from_errno("got_ref_to_str");
1971 goto done;
1974 error = got_ref_resolve(&id, repo, re->ref);
1975 if (error)
1976 goto done;
1977 error = got_object_open_as_tag(&tag, repo, id);
1978 free(id);
1979 if (error)
1980 goto done;
1982 tagger_time = got_object_tag_get_tagger_time(tag);
1984 error = got_object_id_str(&id_str,
1985 got_object_tag_get_object_id(tag));
1986 if (error)
1987 goto done;
1989 tag_log0 = strdup(got_object_tag_get_message(tag));
1991 if (tag_log0 == NULL) {
1992 error = got_error_from_errno("strdup");
1993 goto done;
1996 tag_log = tag_log0;
1997 while (*tag_log == '\n')
1998 tag_log++;
2000 switch (tag_type) {
2001 case TAGBRIEF:
2002 newline = strchr(tag_log, '\n');
2003 if (newline)
2004 *newline = '\0';
2006 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
2007 TM_DIFF))) == -1) {
2008 error = got_error_from_errno("asprintf");
2009 goto done;
2012 if ((asprintf(&tags_navs_disp, tags_navs,
2013 gw_trans->repo_name, id_str, gw_trans->repo_name,
2014 id_str, gw_trans->repo_name, id_str,
2015 gw_trans->repo_name, id_str)) == -1) {
2016 error = got_error_from_errno("asprintf");
2017 goto done;
2020 if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
2021 tags_navs_disp)) == -1) {
2022 error = got_error_from_errno("asprintf");
2023 goto done;
2026 free(tags_navs_disp);
2027 break;
2028 case TAGFULL:
2029 break;
2030 default:
2031 break;
2034 got_object_tag_close(tag);
2036 error = buf_puts(&newsize, diffbuf, tag_row);
2038 free(id_str);
2039 free(refstr);
2040 free(age);
2041 free(tag_log0);
2042 free(tag_row);
2044 if (error || (limit && --limit == 0))
2045 break;
2048 if (buf_len(diffbuf) > 0) {
2049 error = buf_putc(diffbuf, '\0');
2050 tags = strdup(buf_get(diffbuf));
2052 done:
2053 buf_free(diffbuf);
2054 got_ref_list_free(&refs);
2055 if (repo)
2056 got_repo_close(repo);
2057 if (error)
2058 return NULL;
2059 else
2060 return tags;
2063 static char *
2064 gw_get_repo_heads(struct trans *gw_trans)
2066 const struct got_error *error = NULL;
2067 struct got_repository *repo = NULL;
2068 struct got_reflist_head refs;
2069 struct got_reflist_entry *re;
2070 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2071 struct buf *diffbuf = NULL;
2072 size_t newsize;
2074 error = buf_alloc(&diffbuf, 0);
2075 if (error != NULL)
2076 return NULL;
2078 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2079 if (error != NULL)
2080 goto done;
2082 SIMPLEQ_INIT(&refs);
2083 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2084 NULL);
2085 if (error)
2086 goto done;
2088 SIMPLEQ_FOREACH(re, &refs, entry) {
2089 char *refname;
2091 refname = strdup(got_ref_get_name(re->ref));
2092 if (refname == NULL) {
2093 error = got_error_from_errno("got_ref_to_str");
2094 goto done;
2097 if (strncmp(refname, "refs/heads/", 11) != 0) {
2098 free(refname);
2099 continue;
2102 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2103 TM_DIFF);
2105 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2106 refname, gw_trans->repo_name, refname,
2107 gw_trans->repo_name, refname, gw_trans->repo_name,
2108 refname)) == -1) {
2109 error = got_error_from_errno("asprintf");
2110 goto done;
2113 if (strncmp(refname, "refs/heads/", 11) == 0)
2114 refname += 11;
2116 if ((asprintf(&head_row, heads_row, age, refname,
2117 head_navs_disp)) == -1) {
2118 error = got_error_from_errno("asprintf");
2119 goto done;
2122 error = buf_puts(&newsize, diffbuf, head_row);
2124 free(head_navs_disp);
2125 free(head_row);
2128 if (buf_len(diffbuf) > 0) {
2129 error = buf_putc(diffbuf, '\0');
2130 heads = strdup(buf_get(diffbuf));
2132 done:
2133 buf_free(diffbuf);
2134 got_ref_list_free(&refs);
2135 if (repo)
2136 got_repo_close(repo);
2137 if (error)
2138 return NULL;
2139 else
2140 return heads;
2143 static char *
2144 gw_get_got_link(struct trans *gw_trans)
2146 char *link;
2148 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2149 gw_trans->gw_conf->got_logo)) == -1)
2150 return NULL;
2152 return link;
2155 static char *
2156 gw_get_site_link(struct trans *gw_trans)
2158 char *link, *repo = "", *action = "";
2160 if (gw_trans->repo_name != NULL)
2161 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2162 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2163 return NULL;
2165 if (gw_trans->action_name != NULL)
2166 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2167 return NULL;
2169 if ((asprintf(&link, site_link, GOTWEB,
2170 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2171 return NULL;
2173 return link;
2176 static char *
2177 gw_html_escape(const char *html)
2179 char *escaped_str = NULL, *buf;
2180 char c[1];
2181 size_t sz, i, buff_sz = 2048;
2183 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2184 return NULL;
2186 if (html == NULL)
2187 return NULL;
2188 else
2189 if ((sz = strlen(html)) == 0)
2190 return NULL;
2192 /* only work with buff_sz */
2193 if (buff_sz < sz)
2194 sz = buff_sz;
2196 for (i = 0; i < sz; i++) {
2197 c[0] = html[i];
2198 switch (c[0]) {
2199 case ('>'):
2200 strcat(buf, "&gt;");
2201 break;
2202 case ('&'):
2203 strcat(buf, "&amp;");
2204 break;
2205 case ('<'):
2206 strcat(buf, "&lt;");
2207 break;
2208 case ('"'):
2209 strcat(buf, "&quot;");
2210 break;
2211 case ('\''):
2212 strcat(buf, "&apos;");
2213 break;
2214 case ('\n'):
2215 strcat(buf, "<br />");
2216 case ('|'):
2217 strcat(buf, " ");
2218 default:
2219 strcat(buf, &c[0]);
2220 break;
2223 asprintf(&escaped_str, "%s", buf);
2224 free(buf);
2225 return escaped_str;
2228 int
2229 main()
2231 const struct got_error *error = NULL;
2232 struct trans *gw_trans;
2233 struct gw_dir *dir = NULL, *tdir;
2234 const char *page = "index";
2235 int gw_malloc = 1;
2237 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
2238 errx(1, "malloc");
2240 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2241 errx(1, "malloc");
2243 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2244 errx(1, "malloc");
2246 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2247 errx(1, "malloc");
2249 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
2250 &page, 1, 0))
2251 errx(1, "khttp_parse");
2253 if ((gw_trans->gw_conf =
2254 malloc(sizeof(struct gotweb_conf))) == NULL) {
2255 gw_malloc = 0;
2256 error = got_error_from_errno("malloc");
2257 goto err;
2260 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
2261 error = got_error_from_errno("pledge");
2262 goto err;
2265 TAILQ_INIT(&gw_trans->gw_dirs);
2267 gw_trans->page = 0;
2268 gw_trans->repos_total = 0;
2269 gw_trans->repo_path = NULL;
2270 gw_trans->commit = NULL;
2271 gw_trans->headref = strdup(GOT_REF_HEAD);
2272 gw_trans->mime = KMIME_TEXT_HTML;
2273 gw_trans->gw_tmpl->key = templs;
2274 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2275 gw_trans->gw_tmpl->arg = gw_trans;
2276 gw_trans->gw_tmpl->cb = gw_template;
2277 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2279 err:
2280 if (error) {
2281 gw_trans->mime = KMIME_TEXT_PLAIN;
2282 gw_trans->action = GW_ERR;
2283 gw_display_index(gw_trans, error);
2284 goto done;
2287 error = gw_parse_querystring(gw_trans);
2288 if (error)
2289 goto err;
2291 gw_display_index(gw_trans, error);
2293 done:
2294 if (gw_malloc) {
2295 free(gw_trans->gw_conf->got_repos_path);
2296 free(gw_trans->gw_conf->got_www_path);
2297 free(gw_trans->gw_conf->got_site_name);
2298 free(gw_trans->gw_conf->got_site_owner);
2299 free(gw_trans->gw_conf->got_site_link);
2300 free(gw_trans->gw_conf->got_logo);
2301 free(gw_trans->gw_conf->got_logo_url);
2302 free(gw_trans->gw_conf);
2303 free(gw_trans->commit);
2304 free(gw_trans->repo_path);
2305 free(gw_trans->repo_name);
2306 free(gw_trans->repo_file);
2307 free(gw_trans->action_name);
2308 free(gw_trans->headref);
2310 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2311 free(dir->name);
2312 free(dir->description);
2313 free(dir->age);
2314 free(dir->url);
2315 free(dir->path);
2316 free(dir);
2321 khttp_free(gw_trans->gw_req);
2322 return EXIT_SUCCESS;