Blob


1 /*
2 * Copyright (c) 2019 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 };
121 enum tags {
122 TAGBRIEF,
123 TAGFULL,
124 };
126 struct buf {
127 /* buffer handle, buffer size, and data length */
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 static struct gw_dir *gw_init_gw_dir(char *);
154 static char *gw_get_repo_description(struct trans *,
155 char *);
156 static char *gw_get_repo_owner(struct trans *,
157 char *);
158 static char *gw_get_time_str(time_t, int);
159 static char *gw_get_repo_age(struct trans *,
160 char *, char *, int);
161 static char *gw_get_repo_log(struct trans *, const char *,
162 char *, int, int);
163 static char *gw_get_repo_tags(struct trans *, int, int);
164 static char *gw_get_repo_heads(struct trans *);
165 static char *gw_get_clone_url(struct trans *, char *);
166 static char *gw_get_got_link(struct trans *);
167 static char *gw_get_site_link(struct trans *);
168 static char *gw_html_escape(const char *);
170 static void gw_display_open(struct trans *, enum khttp,
171 enum kmime);
172 static void gw_display_index(struct trans *,
173 const struct got_error *);
175 static int gw_template(size_t, void *);
177 static const struct got_error* apply_unveil(const char *, const char *);
178 static const struct got_error* cmp_tags(void *, int *,
179 struct got_reference *,
180 struct got_reference *);
181 static const struct got_error* gw_load_got_paths(struct trans *);
182 static const struct got_error* gw_load_got_path(struct trans *,
183 struct gw_dir *);
184 static const struct got_error* gw_parse_querystring(struct trans *);
185 static const struct got_error* match_logmsg(int *, struct got_object_id *,
186 struct got_commit_object *, regex_t *);
188 static const struct got_error* gw_blame(struct trans *);
189 static const struct got_error* gw_blob(struct trans *);
190 static const struct got_error* gw_blobdiff(struct trans *);
191 static const struct got_error* gw_commit(struct trans *);
192 static const struct got_error* gw_commitdiff(struct trans *);
193 static const struct got_error* gw_history(struct trans *);
194 static const struct got_error* gw_index(struct trans *);
195 static const struct got_error* gw_log(struct trans *);
196 static const struct got_error* gw_raw(struct trans *);
197 static const struct got_error* gw_logbriefs(struct trans *);
198 static const struct got_error* gw_snapshot(struct trans *);
199 static const struct got_error* gw_summary(struct trans *);
200 static const struct got_error* gw_tree(struct trans *);
202 struct gw_query_action {
203 unsigned int func_id;
204 const char *func_name;
205 const struct got_error *(*func_main)(struct trans *);
206 char *template;
207 };
209 enum gw_query_actions {
210 GW_BLAME,
211 GW_BLOB,
212 GW_BLOBDIFF,
213 GW_COMMIT,
214 GW_COMMITDIFF,
215 GW_ERR,
216 GW_HISTORY,
217 GW_INDEX,
218 GW_LOG,
219 GW_RAW,
220 GW_LOGBRIEFS,
221 GW_SNAPSHOT,
222 GW_SUMMARY,
223 GW_TREE
224 };
226 static struct gw_query_action gw_query_funcs[] = {
227 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
228 { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
229 { GW_BLOBDIFF, "blobdiff", gw_blobdiff, "gw_tmpl/index.tmpl" },
230 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
231 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
232 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
233 { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
234 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
235 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
236 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
237 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
238 { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
239 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
240 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
241 };
243 static const struct got_error *
244 apply_unveil(const char *repo_path, const char *repo_file)
246 const struct got_error *err;
248 if (repo_path && repo_file) {
249 char *full_path;
250 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
251 return got_error_from_errno("asprintf unveil");
252 if (unveil(full_path, "r") != 0)
253 return got_error_from_errno2("unveil", full_path);
256 if (repo_path && unveil(repo_path, "r") != 0)
257 return got_error_from_errno2("unveil", repo_path);
259 if (unveil("/tmp", "rwc") != 0)
260 return got_error_from_errno2("unveil", "/tmp");
262 err = got_privsep_unveil_exec_helpers();
263 if (err != NULL)
264 return err;
266 if (unveil(NULL, NULL) != 0)
267 return got_error_from_errno("unveil");
269 return NULL;
272 static const struct got_error *
273 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
274 struct got_reference *ref2)
276 const struct got_error *err = NULL;
277 struct got_repository *repo = arg;
278 struct got_object_id *id1, *id2 = NULL;
279 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
280 time_t time1, time2;
282 *cmp = 0;
284 err = got_ref_resolve(&id1, repo, ref1);
285 if (err)
286 return err;
287 err = got_object_open_as_tag(&tag1, repo, id1);
288 if (err)
289 goto done;
291 err = got_ref_resolve(&id2, repo, ref2);
292 if (err)
293 goto done;
294 err = got_object_open_as_tag(&tag2, repo, id2);
295 if (err)
296 goto done;
298 time1 = got_object_tag_get_tagger_time(tag1);
299 time2 = got_object_tag_get_tagger_time(tag2);
301 /* Put latest tags first. */
302 if (time1 < time2)
303 *cmp = 1;
304 else if (time1 > time2)
305 *cmp = -1;
306 else
307 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
308 done:
309 free(id1);
310 free(id2);
311 if (tag1)
312 got_object_tag_close(tag1);
313 if (tag2)
314 got_object_tag_close(tag2);
315 return err;
318 static const struct got_error *
319 gw_blame(struct trans *gw_trans)
321 const struct got_error *error = NULL;
323 return error;
326 static const struct got_error *
327 gw_blob(struct trans *gw_trans)
329 const struct got_error *error = NULL;
331 return error;
334 static const struct got_error *
335 gw_blobdiff(struct trans *gw_trans)
337 const struct got_error *error = NULL;
339 return error;
342 static const struct got_error *
343 gw_commit(struct trans *gw_trans)
345 const struct got_error *error = NULL;
346 char *log, *log_html;
348 error = apply_unveil(gw_trans->gw_dir->path, NULL);
349 if (error)
350 return error;
352 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
354 if (log != NULL && strcmp(log, "") != 0) {
355 if ((asprintf(&log_html, log_commit, log)) == -1)
356 return got_error_from_errno("asprintf");
357 khttp_puts(gw_trans->gw_req, log_html);
358 free(log_html);
359 free(log);
361 return error;
364 static const struct got_error *
365 gw_commitdiff(struct trans *gw_trans)
367 const struct got_error *error = NULL;
368 char *log, *log_html;
370 error = apply_unveil(gw_trans->gw_dir->path, NULL);
371 if (error)
372 return error;
374 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
376 if (log != NULL && strcmp(log, "") != 0) {
377 if ((asprintf(&log_html, log_diff, log)) == -1)
378 return got_error_from_errno("asprintf");
379 khttp_puts(gw_trans->gw_req, log_html);
380 free(log_html);
381 free(log);
383 return error;
386 static const struct got_error *
387 gw_history(struct trans *gw_trans)
389 const struct got_error *error = NULL;
391 return error;
394 static const struct got_error *
395 gw_index(struct trans *gw_trans)
397 const struct got_error *error = NULL;
398 struct gw_dir *gw_dir = NULL;
399 char *html, *navs, *next, *prev;
400 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
402 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
403 if (error)
404 return error;
406 error = gw_load_got_paths(gw_trans);
407 if (error)
408 return error;
410 khttp_puts(gw_trans->gw_req, index_projects_header);
412 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
413 dir_c++;
415 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
416 if (gw_trans->page > 0 && (gw_trans->page *
417 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
418 prev_disp++;
419 continue;
422 prev_disp++;
423 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
424 gw_dir->name, gw_dir->name)) == -1)
425 return got_error_from_errno("asprintf");
427 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
428 gw_dir->description, gw_dir->owner, gw_dir->age,
429 navs)) == -1)
430 return got_error_from_errno("asprintf");
432 khttp_puts(gw_trans->gw_req, html);
434 free(navs);
435 free(html);
437 if (gw_trans->gw_conf->got_max_repos_display == 0)
438 continue;
440 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
441 khttp_puts(gw_trans->gw_req, np_wrapper_start);
442 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
443 (gw_trans->page > 0) &&
444 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
445 prev_disp == gw_trans->repos_total))
446 khttp_puts(gw_trans->gw_req, np_wrapper_start);
448 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
449 (gw_trans->page > 0) &&
450 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
451 prev_disp == gw_trans->repos_total)) {
452 if ((asprintf(&prev, nav_prev,
453 gw_trans->page - 1)) == -1)
454 return got_error_from_errno("asprintf");
455 khttp_puts(gw_trans->gw_req, prev);
456 free(prev);
459 khttp_puts(gw_trans->gw_req, div_end);
461 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
462 next_disp == gw_trans->gw_conf->got_max_repos_display &&
463 dir_c != (gw_trans->page + 1) *
464 gw_trans->gw_conf->got_max_repos_display) {
465 if ((asprintf(&next, nav_next,
466 gw_trans->page + 1)) == -1)
467 return got_error_from_errno("calloc");
468 khttp_puts(gw_trans->gw_req, next);
469 khttp_puts(gw_trans->gw_req, div_end);
470 free(next);
471 next_disp = 0;
472 break;
475 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
476 (gw_trans->page > 0) &&
477 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
478 prev_disp == gw_trans->repos_total))
479 khttp_puts(gw_trans->gw_req, div_end);
481 next_disp++;
483 return error;
486 static const struct got_error *
487 gw_log(struct trans *gw_trans)
489 const struct got_error *error = NULL;
490 char *log, *log_html;
492 error = apply_unveil(gw_trans->gw_dir->path, NULL);
493 if (error)
494 return error;
496 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
497 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
499 if (log != NULL && strcmp(log, "") != 0) {
500 if ((asprintf(&log_html, logs, log)) == -1)
501 return got_error_from_errno("asprintf");
502 khttp_puts(gw_trans->gw_req, log_html);
503 free(log_html);
504 free(log);
506 return error;
509 static const struct got_error *
510 gw_raw(struct trans *gw_trans)
512 const struct got_error *error = NULL;
514 return error;
517 static const struct got_error *
518 gw_logbriefs(struct trans *gw_trans)
520 const struct got_error *error = NULL;
521 char *log, *log_html;
523 error = apply_unveil(gw_trans->gw_dir->path, NULL);
524 if (error)
525 return error;
527 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
528 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
530 if (log != NULL && strcmp(log, "") != 0) {
531 if ((asprintf(&log_html, summary_logbriefs,
532 log)) == -1)
533 return got_error_from_errno("asprintf");
534 khttp_puts(gw_trans->gw_req, log_html);
535 free(log_html);
536 free(log);
538 return error;
541 static const struct got_error *
542 gw_snapshot(struct trans *gw_trans)
544 const struct got_error *error = NULL;
546 return error;
549 static const struct got_error *
550 gw_summary(struct trans *gw_trans)
552 const struct got_error *error = NULL;
553 char *description_html, *repo_owner_html, *repo_age_html,
554 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
555 *heads_html, *age;
557 error = apply_unveil(gw_trans->gw_dir->path, NULL);
558 if (error)
559 return error;
561 khttp_puts(gw_trans->gw_req, summary_wrapper);
562 if (gw_trans->gw_conf->got_show_repo_description) {
563 if (gw_trans->gw_dir->description != NULL &&
564 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
565 if ((asprintf(&description_html, description,
566 gw_trans->gw_dir->description)) == -1)
567 return got_error_from_errno("asprintf");
569 khttp_puts(gw_trans->gw_req, description_html);
570 free(description_html);
574 if (gw_trans->gw_conf->got_show_repo_owner) {
575 if (gw_trans->gw_dir->owner != NULL &&
576 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
577 if ((asprintf(&repo_owner_html, repo_owner,
578 gw_trans->gw_dir->owner)) == -1)
579 return got_error_from_errno("asprintf");
581 khttp_puts(gw_trans->gw_req, repo_owner_html);
582 free(repo_owner_html);
586 if (gw_trans->gw_conf->got_show_repo_age) {
587 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
588 "refs/heads", TM_LONG);
589 if (age != NULL && (strcmp(age, "") != 0)) {
590 if ((asprintf(&repo_age_html, last_change, age)) == -1)
591 return got_error_from_errno("asprintf");
593 khttp_puts(gw_trans->gw_req, repo_age_html);
594 free(repo_age_html);
595 free(age);
599 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
600 if (gw_trans->gw_dir->url != NULL &&
601 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
602 if ((asprintf(&cloneurl_html, cloneurl,
603 gw_trans->gw_dir->url)) == -1)
604 return got_error_from_errno("asprintf");
606 khttp_puts(gw_trans->gw_req, cloneurl_html);
607 free(cloneurl_html);
610 khttp_puts(gw_trans->gw_req, div_end);
612 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
613 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
614 heads = gw_get_repo_heads(gw_trans);
616 if (log != NULL && strcmp(log, "") != 0) {
617 if ((asprintf(&log_html, summary_logbriefs,
618 log)) == -1)
619 return got_error_from_errno("asprintf");
620 khttp_puts(gw_trans->gw_req, log_html);
621 free(log_html);
622 free(log);
625 if (tags != NULL && strcmp(tags, "") != 0) {
626 if ((asprintf(&tags_html, summary_tags,
627 tags)) == -1)
628 return got_error_from_errno("asprintf");
629 khttp_puts(gw_trans->gw_req, tags_html);
630 free(tags_html);
631 free(tags);
634 if (heads != NULL && strcmp(heads, "") != 0) {
635 if ((asprintf(&heads_html, summary_heads,
636 heads)) == -1)
637 return got_error_from_errno("asprintf");
638 khttp_puts(gw_trans->gw_req, heads_html);
639 free(heads_html);
640 free(heads);
643 return error;
646 static const struct got_error *
647 gw_tree(struct trans *gw_trans)
649 const struct got_error *error = NULL;
650 char *log, *log_html;
652 error = apply_unveil(gw_trans->gw_dir->path, NULL);
653 if (error)
654 return error;
656 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
658 if (log != NULL && strcmp(log, "") != 0) {
659 if ((asprintf(&log_html, log_tree, log)) == -1)
660 return got_error_from_errno("asprintf");
661 khttp_puts(gw_trans->gw_req, log_html);
662 free(log_html);
663 free(log);
665 return error;
668 static const struct got_error *
669 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
671 const struct got_error *error = NULL;
672 DIR *dt;
673 char *dir_test;
674 int opened = 0;
676 if ((asprintf(&dir_test, "%s/%s/%s",
677 gw_trans->gw_conf->got_repos_path, gw_dir->name,
678 GOTWEB_GIT_DIR)) == -1)
679 return got_error_from_errno("asprintf");
681 dt = opendir(dir_test);
682 if (dt == NULL) {
683 free(dir_test);
684 } else {
685 gw_dir->path = strdup(dir_test);
686 opened = 1;
687 goto done;
690 if ((asprintf(&dir_test, "%s/%s/%s",
691 gw_trans->gw_conf->got_repos_path, gw_dir->name,
692 GOTWEB_GOT_DIR)) == -1)
693 return got_error_from_errno("asprintf");
695 dt = opendir(dir_test);
696 if (dt == NULL)
697 free(dir_test);
698 else {
699 opened = 1;
700 error = got_error(GOT_ERR_NOT_GIT_REPO);
701 goto errored;
704 if ((asprintf(&dir_test, "%s/%s",
705 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
706 return got_error_from_errno("asprintf");
708 gw_dir->path = strdup(dir_test);
710 done:
711 gw_dir->description = gw_get_repo_description(gw_trans,
712 gw_dir->path);
713 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
714 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
715 TM_DIFF);
716 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
718 errored:
719 free(dir_test);
720 if (opened)
721 closedir(dt);
722 return error;
725 static const struct got_error *
726 gw_load_got_paths(struct trans *gw_trans)
728 const struct got_error *error = NULL;
729 DIR *d;
730 struct dirent **sd_dent;
731 struct gw_dir *gw_dir;
732 struct stat st;
733 unsigned int d_cnt, d_i;
735 d = opendir(gw_trans->gw_conf->got_repos_path);
736 if (d == NULL) {
737 error = got_error_from_errno2("opendir",
738 gw_trans->gw_conf->got_repos_path);
739 return error;
742 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
743 alphasort);
744 if (d_cnt == -1) {
745 error = got_error_from_errno2("scandir",
746 gw_trans->gw_conf->got_repos_path);
747 return error;
750 for (d_i = 0; d_i < d_cnt; d_i++) {
751 if (gw_trans->gw_conf->got_max_repos > 0 &&
752 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
753 break; /* account for parent and self */
755 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
756 strcmp(sd_dent[d_i]->d_name, "..") == 0)
757 continue;
759 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
760 return got_error_from_errno("gw_dir malloc");
762 error = gw_load_got_path(gw_trans, gw_dir);
763 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
764 continue;
765 else if (error)
766 return error;
768 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
769 !got_path_dir_is_empty(gw_dir->path)) {
770 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
771 entry);
772 gw_trans->repos_total++;
776 closedir(d);
777 return error;
780 static const struct got_error *
781 gw_parse_querystring(struct trans *gw_trans)
783 const struct got_error *error = NULL;
784 struct kpair *p;
785 struct gw_query_action *action = NULL;
786 unsigned int i;
788 if (gw_trans->gw_req->fieldnmap[0]) {
789 error = got_error_from_errno("bad parse");
790 return error;
791 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
792 /* define gw_trans->repo_path */
793 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
794 return got_error_from_errno("asprintf");
796 if ((asprintf(&gw_trans->repo_path, "%s/%s",
797 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
798 return got_error_from_errno("asprintf");
800 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
801 if ((asprintf(&gw_trans->commit, "%s",
802 p->parsed.s)) == -1)
803 return got_error_from_errno("asprintf");
805 /* get action and set function */
806 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
807 for (i = 0; i < nitems(gw_query_funcs); i++) {
808 action = &gw_query_funcs[i];
809 if (action->func_name == NULL)
810 continue;
812 if (strcmp(action->func_name,
813 p->parsed.s) == 0) {
814 gw_trans->action = i;
815 if ((asprintf(&gw_trans->action_name,
816 "%s", action->func_name)) == -1)
817 return
818 got_error_from_errno(
819 "asprintf");
821 break;
824 action = NULL;
827 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
828 if ((asprintf(&gw_trans->repo_file, "%s",
829 p->parsed.s)) == -1)
830 return got_error_from_errno("asprintf");
832 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
833 if ((asprintf(&gw_trans->headref, "%s",
834 p->parsed.s)) == -1)
835 return got_error_from_errno("asprintf");
837 if (action == NULL) {
838 error = got_error_from_errno("invalid action");
839 return error;
841 if ((gw_trans->gw_dir =
842 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
843 return got_error_from_errno("gw_dir malloc");
845 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
846 if (error)
847 return error;
848 } else
849 gw_trans->action = GW_INDEX;
851 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
852 gw_trans->page = p->parsed.i;
854 if (gw_trans->action == GW_RAW)
855 gw_trans->mime = KMIME_TEXT_PLAIN;
857 return error;
860 static struct gw_dir *
861 gw_init_gw_dir(char *dir)
863 struct gw_dir *gw_dir;
865 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
866 return NULL;
868 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
869 return NULL;
871 return gw_dir;
874 static const struct got_error*
875 match_logmsg(int *have_match, struct got_object_id *id,
876 struct got_commit_object *commit, regex_t *regex)
878 const struct got_error *err = NULL;
879 regmatch_t regmatch;
880 char *id_str = NULL, *logmsg = NULL;
882 *have_match = 0;
884 err = got_object_id_str(&id_str, id);
885 if (err)
886 return err;
888 err = got_object_commit_get_logmsg(&logmsg, commit);
889 if (err)
890 goto done;
892 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
893 *have_match = 1;
894 done:
895 free(id_str);
896 free(logmsg);
897 return err;
900 static void
901 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
903 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
904 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
905 khttps[code]);
906 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
907 kmimetypes[mime]);
908 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
909 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
910 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
911 khttp_body(gw_trans->gw_req);
914 static void
915 gw_display_index(struct trans *gw_trans, const struct got_error *err)
917 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
918 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
920 if (err)
921 khttp_puts(gw_trans->gw_req, err->msg);
922 else
923 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
924 gw_query_funcs[gw_trans->action].template);
926 khtml_close(gw_trans->gw_html_req);
929 static int
930 gw_template(size_t key, void *arg)
932 const struct got_error *error = NULL;
933 struct trans *gw_trans = arg;
934 char *gw_got_link, *gw_site_link;
935 char *site_owner_name, *site_owner_name_h;
937 switch (key) {
938 case (TEMPL_HEAD):
939 khttp_puts(gw_trans->gw_req, head);
940 break;
941 case(TEMPL_HEADER):
942 gw_got_link = gw_get_got_link(gw_trans);
943 if (gw_got_link != NULL)
944 khttp_puts(gw_trans->gw_req, gw_got_link);
946 free(gw_got_link);
947 break;
948 case (TEMPL_SITEPATH):
949 gw_site_link = gw_get_site_link(gw_trans);
950 if (gw_site_link != NULL)
951 khttp_puts(gw_trans->gw_req, gw_site_link);
953 free(gw_site_link);
954 break;
955 case(TEMPL_TITLE):
956 if (gw_trans->gw_conf->got_site_name != NULL)
957 khtml_puts(gw_trans->gw_html_req,
958 gw_trans->gw_conf->got_site_name);
960 break;
961 case (TEMPL_SEARCH):
962 khttp_puts(gw_trans->gw_req, search);
963 break;
964 case(TEMPL_SITEOWNER):
965 if (gw_trans->gw_conf->got_site_owner != NULL &&
966 gw_trans->gw_conf->got_show_site_owner) {
967 site_owner_name =
968 gw_html_escape(gw_trans->gw_conf->got_site_owner);
969 if ((asprintf(&site_owner_name_h, site_owner,
970 site_owner_name))
971 == -1)
972 return 0;
974 khttp_puts(gw_trans->gw_req, site_owner_name_h);
975 free(site_owner_name);
976 free(site_owner_name_h);
978 break;
979 case(TEMPL_CONTENT):
980 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
981 if (error)
982 khttp_puts(gw_trans->gw_req, error->msg);
984 break;
985 default:
986 return 0;
987 break;
989 return 1;
992 static char *
993 gw_get_repo_description(struct trans *gw_trans, char *dir)
995 FILE *f;
996 char *description = NULL, *d_file = NULL;
997 unsigned int len;
999 if (gw_trans->gw_conf->got_show_repo_description == false)
1000 goto err;
1002 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1003 goto err;
1005 if ((f = fopen(d_file, "r")) == NULL)
1006 goto err;
1008 fseek(f, 0, SEEK_END);
1009 len = ftell(f) + 1;
1010 fseek(f, 0, SEEK_SET);
1011 if ((description = calloc(len, sizeof(char *))) == NULL)
1012 goto err;
1014 fread(description, 1, len, f);
1015 fclose(f);
1016 free(d_file);
1017 return description;
1018 err:
1019 if ((asprintf(&description, "%s", "")) == -1)
1020 return NULL;
1022 return description;
1025 static char *
1026 gw_get_time_str(time_t committer_time, int ref_tm)
1028 struct tm tm;
1029 time_t diff_time;
1030 char *years = "years ago", *months = "months ago";
1031 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1032 char *minutes = "minutes ago", *seconds = "seconds ago";
1033 char *now = "right now";
1034 char *repo_age, *s;
1035 char datebuf[BUFFER_SIZE];
1037 switch (ref_tm) {
1038 case TM_DIFF:
1039 diff_time = time(NULL) - committer_time;
1040 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1041 if ((asprintf(&repo_age, "%lld %s",
1042 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1043 return NULL;
1044 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1045 if ((asprintf(&repo_age, "%lld %s",
1046 (diff_time / 60 / 60 / 24 / (365 / 12)),
1047 months)) == -1)
1048 return NULL;
1049 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1050 if ((asprintf(&repo_age, "%lld %s",
1051 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1052 return NULL;
1053 } else if (diff_time > 60 * 60 * 24 * 2) {
1054 if ((asprintf(&repo_age, "%lld %s",
1055 (diff_time / 60 / 60 / 24), days)) == -1)
1056 return NULL;
1057 } else if (diff_time > 60 * 60 * 2) {
1058 if ((asprintf(&repo_age, "%lld %s",
1059 (diff_time / 60 / 60), hours)) == -1)
1060 return NULL;
1061 } else if (diff_time > 60 * 2) {
1062 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1063 minutes)) == -1)
1064 return NULL;
1065 } else if (diff_time > 2) {
1066 if ((asprintf(&repo_age, "%lld %s", diff_time,
1067 seconds)) == -1)
1068 return NULL;
1069 } else {
1070 if ((asprintf(&repo_age, "%s", now)) == -1)
1071 return NULL;
1073 break;
1074 case TM_LONG:
1075 if (gmtime_r(&committer_time, &tm) == NULL)
1076 return NULL;
1078 s = asctime_r(&tm, datebuf);
1079 if (s == NULL)
1080 return NULL;
1082 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1083 return NULL;
1084 break;
1086 return repo_age;
1089 static char *
1090 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
1092 const struct got_error *error = NULL;
1093 struct got_object_id *id = NULL;
1094 struct got_repository *repo = NULL;
1095 struct got_commit_object *commit = NULL;
1096 struct got_reflist_head refs;
1097 struct got_reflist_entry *re;
1098 struct got_reference *head_ref;
1099 int is_head = 0;
1100 time_t committer_time = 0, cmp_time = 0;
1101 const char *refname;
1102 char *repo_age = NULL;
1104 if (repo_ref == NULL)
1105 return NULL;
1107 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1108 is_head = 1;
1110 SIMPLEQ_INIT(&refs);
1111 if (gw_trans->gw_conf->got_show_repo_age == false) {
1112 if ((asprintf(&repo_age, "")) == -1)
1113 return NULL;
1114 return repo_age;
1117 error = got_repo_open(&repo, dir, NULL);
1118 if (error != NULL)
1119 goto err;
1121 if (is_head)
1122 error = got_ref_list(&refs, repo, "refs/heads",
1123 got_ref_cmp_by_name, NULL);
1124 else
1125 error = got_ref_list(&refs, repo, repo_ref,
1126 got_ref_cmp_by_name, NULL);
1127 if (error != NULL)
1128 goto err;
1130 SIMPLEQ_FOREACH(re, &refs, entry) {
1131 if (is_head)
1132 refname = strdup(repo_ref);
1133 else
1134 refname = got_ref_get_name(re->ref);
1135 error = got_ref_open(&head_ref, repo, refname, 0);
1136 if (error != NULL)
1137 goto err;
1139 error = got_ref_resolve(&id, repo, head_ref);
1140 got_ref_close(head_ref);
1141 if (error != NULL)
1142 goto err;
1144 error = got_object_open_as_commit(&commit, repo, id);
1145 if (error != NULL)
1146 goto err;
1148 committer_time =
1149 got_object_commit_get_committer_time(commit);
1151 if (cmp_time < committer_time)
1152 cmp_time = committer_time;
1155 if (cmp_time != 0) {
1156 committer_time = cmp_time;
1157 repo_age = gw_get_time_str(committer_time, ref_tm);
1158 } else
1159 if ((asprintf(&repo_age, "")) == -1)
1160 return NULL;
1161 got_ref_list_free(&refs);
1162 free(id);
1163 return repo_age;
1164 err:
1165 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1166 return NULL;
1168 return repo_age;
1171 static char *
1172 gw_get_repo_owner(struct trans *gw_trans, char *dir)
1174 FILE *f;
1175 char *owner = NULL, *d_file = NULL;
1176 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1177 char *comp, *pos, *buf;
1178 unsigned int i;
1180 if (gw_trans->gw_conf->got_show_repo_owner == false)
1181 goto err;
1183 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1184 goto err;
1186 if ((f = fopen(d_file, "r")) == NULL)
1187 goto err;
1189 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1190 goto err;
1192 while ((fgets(buf, BUFFER_SIZE, f)) != NULL) {
1193 if ((pos = strstr(buf, gotweb)) != NULL)
1194 break;
1196 if ((pos = strstr(buf, gitweb)) != NULL)
1197 break;
1200 if (pos == NULL)
1201 goto err;
1203 do {
1204 fgets(buf, BUFFER_SIZE, f);
1205 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1207 if (comp == NULL)
1208 goto err;
1210 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1211 goto err;
1213 for (i = 0; i < 2; i++) {
1214 owner = strsep(&buf, "\"");
1217 if (owner == NULL)
1218 goto err;
1220 fclose(f);
1221 free(d_file);
1222 return owner;
1223 err:
1224 if ((asprintf(&owner, "%s", "")) == -1)
1225 return NULL;
1227 return owner;
1230 static char *
1231 gw_get_clone_url(struct trans *gw_trans, char *dir)
1233 FILE *f;
1234 char *url = NULL, *d_file = NULL;
1235 unsigned int len;
1237 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1238 return NULL;
1240 if ((f = fopen(d_file, "r")) == NULL)
1241 return NULL;
1243 fseek(f, 0, SEEK_END);
1244 len = ftell(f) + 1;
1245 fseek(f, 0, SEEK_SET);
1247 if ((url = calloc(len, sizeof(char *))) == NULL)
1248 return NULL;
1250 fread(url, 1, len, f);
1251 fclose(f);
1252 free(d_file);
1253 return url;
1256 static char *
1257 gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1258 char *start_commit, int limit, int log_type)
1260 const struct got_error *error;
1261 struct got_repository *repo = NULL;
1262 struct got_reflist_head refs;
1263 struct got_reflist_entry *re;
1264 struct got_commit_object *commit = NULL;
1265 struct got_object_id *id1 = NULL, *id2 = NULL;
1266 struct got_object_qid *parent_id;
1267 struct got_commit_graph *graph = NULL;
1268 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1269 *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1270 *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1271 *commit_commit_disp = NULL, *commit_age_diff = NULL,
1272 *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1273 *commit_age_long_disp = NULL, *commit_author = NULL,
1274 *commit_author_disp = NULL, *commit_committer = NULL,
1275 *commit_committer_disp = NULL, *commit_log = NULL,
1276 *commit_log_disp = NULL, *commit_parent = NULL,
1277 *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1278 *log_tree_html = NULL, *log_commit_html = NULL,
1279 *log_diff_html = NULL, *commit_tree = NULL,
1280 *commit_tree_disp = NULL;
1281 char *commit_log0, *newline;
1282 regex_t regex;
1283 int have_match;
1284 size_t newsize;
1285 struct buf *diffbuf = NULL;
1286 time_t committer_time;
1288 if (search_pattern &&
1289 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB |
1290 REG_NEWLINE))
1291 return NULL;
1293 SIMPLEQ_INIT(&refs);
1295 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1296 if (error != NULL)
1297 goto done;
1299 error = buf_alloc(&diffbuf, BUFFER_SIZE);
1300 if (error != NULL)
1301 goto done;
1303 if (start_commit == NULL) {
1304 struct got_reference *head_ref;
1305 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1306 if (error != NULL)
1307 goto done;
1308 error = got_ref_resolve(&id1, repo, head_ref);
1309 got_ref_close(head_ref);
1310 if (error != NULL)
1311 goto done;
1312 error = got_object_open_as_commit(&commit, repo, id1);
1313 } else {
1314 struct got_reference *ref;
1315 error = got_ref_open(&ref, repo, start_commit, 0);
1316 if (error == NULL) {
1317 int obj_type;
1318 error = got_ref_resolve(&id1, repo, ref);
1319 got_ref_close(ref);
1320 if (error != NULL)
1321 goto done;
1322 error = got_object_get_type(&obj_type, repo, id1);
1323 if (error != NULL)
1324 goto done;
1325 if (obj_type == GOT_OBJ_TYPE_TAG) {
1326 struct got_tag_object *tag;
1327 error = got_object_open_as_tag(&tag, repo, id1);
1328 if (error != NULL)
1329 goto done;
1330 if (got_object_tag_get_object_type(tag) !=
1331 GOT_OBJ_TYPE_COMMIT) {
1332 got_object_tag_close(tag);
1333 error = got_error(GOT_ERR_OBJ_TYPE);
1334 goto done;
1336 free(id1);
1337 id1 = got_object_id_dup(
1338 got_object_tag_get_object_id(tag));
1339 if (id1 == NULL)
1340 error = got_error_from_errno(
1341 "got_object_id_dup");
1342 got_object_tag_close(tag);
1343 if (error)
1344 goto done;
1345 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1346 error = got_error(GOT_ERR_OBJ_TYPE);
1347 goto done;
1349 error = got_object_open_as_commit(&commit, repo, id1);
1350 if (error != NULL)
1351 goto done;
1353 if (commit == NULL) {
1354 error = got_repo_match_object_id_prefix(&id1,
1355 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1356 if (error != NULL)
1357 goto done;
1359 error = got_repo_match_object_id_prefix(&id1,
1360 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1361 if (error != NULL)
1362 goto done;
1365 if (error != NULL)
1366 goto done;
1367 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1368 if (error != NULL)
1369 goto done;
1371 if (in_repo_path) {
1372 free(path);
1373 path = in_repo_path;
1376 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1377 if (error)
1378 goto done;
1380 error = got_commit_graph_open(&graph, id1, path, 0, repo);
1381 if (error)
1382 goto done;
1384 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1385 if (error)
1386 goto done;
1388 for (;;) {
1389 error = got_commit_graph_iter_next(&id1, graph);
1390 if (error) {
1391 if (error->code == GOT_ERR_ITER_COMPLETED) {
1392 error = NULL;
1393 break;
1395 if (error->code != GOT_ERR_ITER_NEED_MORE)
1396 break;
1397 error = got_commit_graph_fetch_commits(graph, 1, repo,
1398 NULL, NULL);
1399 if (error)
1400 break;
1401 else
1402 continue;
1404 if (id1 == NULL)
1405 break;
1407 error = got_object_open_as_commit(&commit, repo, id1);
1408 if (error)
1409 break;
1411 if (search_pattern) {
1412 error = match_logmsg(&have_match, id1, commit,
1413 &regex);
1414 if (error) {
1415 got_object_commit_close(commit);
1416 break;
1418 if (have_match == 0) {
1419 got_object_commit_close(commit);
1420 continue;
1424 SIMPLEQ_FOREACH(re, &refs, entry) {
1425 char *s;
1426 const char *name;
1427 struct got_tag_object *tag = NULL;
1428 int cmp;
1430 name = got_ref_get_name(re->ref);
1431 if (strcmp(name, GOT_REF_HEAD) == 0)
1432 continue;
1433 if (strncmp(name, "refs/", 5) == 0)
1434 name += 5;
1435 if (strncmp(name, "got/", 4) == 0)
1436 continue;
1437 if (strncmp(name, "heads/", 6) == 0)
1438 name += 6;
1439 if (strncmp(name, "remotes/", 8) == 0)
1440 name += 8;
1441 if (strncmp(name, "tags/", 5) == 0) {
1442 error = got_object_open_as_tag(&tag, repo,
1443 re->id);
1444 if (error) {
1445 if (error->code != GOT_ERR_OBJ_TYPE)
1446 continue;
1448 * Ref points at something other
1449 * than a tag.
1451 error = NULL;
1452 tag = NULL;
1455 cmp = got_object_id_cmp(tag ?
1456 got_object_tag_get_object_id(tag) : re->id, id1);
1457 if (tag)
1458 got_object_tag_close(tag);
1459 if (cmp != 0)
1460 continue;
1461 s = refs_str;
1462 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1463 s ? ", " : "", name)) == -1) {
1464 error = got_error_from_errno("asprintf");
1465 free(s);
1466 goto done;
1468 free(s);
1471 if (refs_str == NULL)
1472 refs_str_disp = strdup("");
1473 else {
1474 if ((asprintf(&refs_str_disp, "(%s)",
1475 refs_str)) == -1) {
1476 error = got_error_from_errno("asprintf");
1477 free(refs_str);
1478 goto done;
1482 error = got_object_id_str(&id_str1, id1);
1483 if (error)
1484 goto done;
1486 error = got_object_id_str(&treeid,
1487 got_object_commit_get_tree_id(commit));
1488 if (error)
1489 goto done;
1491 if (gw_trans->action == GW_COMMIT ||
1492 gw_trans->action == GW_COMMITDIFF) {
1493 parent_id =
1494 SIMPLEQ_FIRST(
1495 got_object_commit_get_parent_ids(commit));
1496 if (parent_id != NULL) {
1497 id2 = got_object_id_dup(parent_id->id);
1498 free (parent_id);
1499 error = got_object_id_str(&id_str2, id2);
1500 if (error)
1501 goto done;
1502 free(id2);
1503 } else
1504 id_str2 = strdup("/dev/null");
1507 committer_time =
1508 got_object_commit_get_committer_time(commit);
1510 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1511 error = got_error_from_errno("asprintf");
1512 goto done;
1515 if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1516 error = got_error_from_errno("asprintf");
1517 goto done;
1520 if ((asprintf(&commit_tree_disp, commit_tree_html,
1521 treeid)) == -1) {
1522 error = got_error_from_errno("asprintf");
1523 goto done;
1526 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1527 id_str1)) == -1) {
1528 error = got_error_from_errno("asprintf");
1529 goto done;
1532 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1533 error = got_error_from_errno("asprintf");
1534 goto done;
1537 if ((asprintf(&commit_commit_disp, commit_commit_html,
1538 commit_commit, refs_str_disp)) == -1) {
1539 error = got_error_from_errno("asprintf");
1540 goto done;
1543 if ((asprintf(&commit_age_long, "%s",
1544 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1545 error = got_error_from_errno("asprintf");
1546 goto done;
1549 if ((asprintf(&commit_age_long_disp, commit_age_html,
1550 commit_age_long)) == -1) {
1551 error = got_error_from_errno("asprintf");
1552 goto done;
1555 if ((asprintf(&commit_age_diff, "%s",
1556 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1557 error = got_error_from_errno("asprintf");
1558 goto done;
1561 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1562 commit_age_diff)) == -1) {
1563 error = got_error_from_errno("asprintf");
1564 goto done;
1567 if ((asprintf(&commit_author, "%s",
1568 got_object_commit_get_author(commit))) == -1) {
1569 error = got_error_from_errno("asprintf");
1570 goto done;
1573 if ((asprintf(&commit_author_disp, commit_author_html,
1574 gw_html_escape(commit_author))) == -1) {
1575 error = got_error_from_errno("asprintf");
1576 goto done;
1579 if ((asprintf(&commit_committer, "%s",
1580 got_object_commit_get_committer(commit))) == -1) {
1581 error = got_error_from_errno("asprintf");
1582 goto done;
1585 if ((asprintf(&commit_committer_disp, commit_committer_html,
1586 gw_html_escape(commit_committer))) == -1) {
1587 error = got_error_from_errno("asprintf");
1588 goto done;
1591 if (strcmp(commit_author, commit_committer) == 0) {
1592 free(commit_committer_disp);
1593 commit_committer_disp = strdup("");
1596 error = got_object_commit_get_logmsg(&commit_log0, commit);
1597 if (error)
1598 goto done;
1600 commit_log = commit_log0;
1601 while (*commit_log == '\n')
1602 commit_log++;
1604 switch(log_type) {
1605 case (LOGBRIEF):
1606 newline = strchr(commit_log, '\n');
1607 if (newline)
1608 *newline = '\0';
1610 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1611 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1612 id_str1, gw_trans->repo_name, id_str1,
1613 gw_trans->repo_name, id_str1)) == -1) {
1614 error = got_error_from_errno("asprintf");
1615 goto done;
1618 if ((asprintf(&commit_row, logbriefs_row,
1619 commit_age_diff, commit_author, commit_log,
1620 logbriefs_navs_html)) == -1) {
1621 error = got_error_from_errno("asprintf");
1622 goto done;
1625 free(logbriefs_navs_html);
1626 logbriefs_navs_html = NULL;
1627 break;
1628 case (LOGFULL):
1629 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1630 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1631 id_str1, gw_trans->repo_name, id_str1,
1632 gw_trans->repo_name, id_str1)) == -1) {
1633 error = got_error_from_errno("asprintf");
1634 goto done;
1637 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1638 commit_author_disp, commit_committer_disp,
1639 commit_age_long_disp, gw_html_escape(commit_log),
1640 logbriefs_navs_html)) == -1) {
1641 error = got_error_from_errno("asprintf");
1642 goto done;
1645 free(logbriefs_navs_html);
1646 logbriefs_navs_html = NULL;
1647 break;
1648 case (LOGTREE):
1649 log_tree_html = strdup("log tree here");
1651 if ((asprintf(&commit_row, log_tree_row,
1652 gw_html_escape(commit_log), log_tree_html)) == -1) {
1653 error = got_error_from_errno("asprintf");
1654 goto done;
1657 free(log_tree_html);
1658 break;
1659 case (LOGCOMMIT):
1660 if ((asprintf(&commit_log_disp, commit_log_html,
1661 gw_html_escape(commit_log))) == -1) {
1662 error = got_error_from_errno("asprintf");
1663 goto done;
1666 log_commit_html = strdup("commit here");
1668 if ((asprintf(&commit_row, log_commit_row,
1669 commit_diff_disp, commit_commit_disp,
1670 commit_tree_disp, commit_author_disp,
1671 commit_committer_disp, commit_age_long_disp,
1672 commit_log_disp, log_commit_html)) == -1) {
1673 error = got_error_from_errno("asprintf");
1674 goto done;
1676 free(commit_log_disp);
1677 free(log_commit_html);
1679 break;
1680 case (LOGDIFF):
1681 if ((asprintf(&commit_log_disp, commit_log_html,
1682 gw_html_escape(commit_log))) == -1) {
1683 error = got_error_from_errno("asprintf");
1684 goto done;
1687 log_diff_html = strdup("diff here");
1689 if ((asprintf(&commit_row, log_diff_row,
1690 commit_diff_disp, commit_commit_disp,
1691 commit_tree_disp, commit_author_disp,
1692 commit_committer_disp, commit_age_long_disp,
1693 commit_log_disp, log_diff_html)) == -1) {
1694 error = got_error_from_errno("asprintf");
1695 goto done;
1697 free(commit_log_disp);
1698 free(log_diff_html);
1700 break;
1701 default:
1702 return NULL;
1705 error = buf_append(&newsize, diffbuf, commit_row,
1706 strlen(commit_row));
1708 free(commit_parent);
1709 free(commit_diff_disp);
1710 free(commit_tree_disp);
1711 free(commit_age_diff);
1712 free(commit_age_diff_disp);
1713 free(commit_age_long);
1714 free(commit_age_long_disp);
1715 free(commit_author);
1716 free(commit_author_disp);
1717 free(commit_committer);
1718 free(commit_committer_disp);
1719 free(commit_log0);
1720 free(commit_row);
1721 free(refs_str_disp);
1722 free(refs_str);
1723 refs_str = NULL;
1724 free(id_str1);
1725 id_str1 = NULL;
1726 free(id_str2);
1727 id_str2 = NULL;
1729 if (error || (limit && --limit == 0))
1730 break;
1733 if (error)
1734 goto done;
1736 logs = strdup(diffbuf->cb_buf);
1737 done:
1738 buf_free(diffbuf);
1739 if (commit != NULL)
1740 got_object_commit_close(commit);
1741 if (search_pattern)
1742 regfree(&regex);
1743 if (graph)
1744 got_commit_graph_close(graph);
1745 if (repo) {
1746 error = got_repo_close(repo);
1747 if (error != NULL)
1748 return NULL;
1750 if (error) {
1751 khttp_puts(gw_trans->gw_req, "Error: ");
1752 khttp_puts(gw_trans->gw_req, error->msg);
1753 return NULL;
1754 } else
1755 return logs;
1758 static char *
1759 gw_get_repo_tags(struct trans *gw_trans, int limit, int tag_type)
1761 const struct got_error *error = NULL;
1762 struct got_repository *repo = NULL;
1763 struct got_reflist_head refs;
1764 struct got_reflist_entry *re;
1765 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1766 *age = NULL;
1767 char *newline;
1768 struct buf *diffbuf = NULL;
1769 size_t newsize;
1771 error = buf_alloc(&diffbuf, BUFFER_SIZE);
1772 if (error != NULL)
1773 goto done;
1775 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1776 if (error != NULL)
1777 goto done;
1779 SIMPLEQ_INIT(&refs);
1781 error = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
1782 if (error)
1783 goto done;
1785 SIMPLEQ_FOREACH(re, &refs, entry) {
1786 const char *refname;
1787 char *refstr, *tag_log0, *tag_log, *id_str;
1788 time_t tagger_time;
1789 struct got_object_id *id;
1790 struct got_tag_object *tag;
1792 refname = got_ref_get_name(re->ref);
1793 if (strncmp(refname, "refs/tags/", 10) != 0)
1794 continue;
1795 refname += 10;
1796 refstr = got_ref_to_str(re->ref);
1797 if (refstr == NULL) {
1798 error = got_error_from_errno("got_ref_to_str");
1799 goto done;
1802 error = got_ref_resolve(&id, repo, re->ref);
1803 if (error)
1804 goto done;
1805 error = got_object_open_as_tag(&tag, repo, id);
1806 free(id);
1807 if (error)
1808 goto done;
1810 tagger_time = got_object_tag_get_tagger_time(tag);
1812 error = got_object_id_str(&id_str,
1813 got_object_tag_get_object_id(tag));
1814 if (error)
1815 goto done;
1817 tag_log0 = strdup(got_object_tag_get_message(tag));
1819 got_object_tag_close(tag);
1820 if (tag_log0 == NULL) {
1821 error = got_error_from_errno("strdup");
1822 goto done;
1825 tag_log = tag_log0;
1826 while (*tag_log == '\n')
1827 tag_log++;
1829 switch (tag_type) {
1830 case TAGBRIEF:
1831 newline = strchr(tag_log, '\n');
1832 if (newline)
1833 *newline = '\0';
1835 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1836 TM_DIFF))) == -1) {
1837 error = got_error_from_errno("asprintf");
1838 goto done;
1841 if ((asprintf(&tags_navs_disp, tags_navs,
1842 gw_trans->repo_name, id_str, gw_trans->repo_name,
1843 id_str, gw_trans->repo_name, id_str,
1844 gw_trans->repo_name, id_str)) == -1) {
1845 error = got_error_from_errno("asprintf");
1846 goto done;
1849 if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
1850 tags_navs_disp)) == -1) {
1851 error = got_error_from_errno("asprintf");
1852 goto done;
1855 free(tags_navs_disp);
1856 break;
1857 case TAGFULL:
1858 break;
1859 default:
1860 break;
1862 /* /1* printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr); *1/ */
1863 /* free(refstr); */
1865 /* error = got_ref_resolve(&id, repo, re->ref); */
1866 /* if (error) */
1867 /* break; */
1868 /* error = got_object_open_as_tag(&tag, repo, id); */
1869 /* free(id); */
1870 /* if (error) */
1871 /* break; */
1872 /* /1* printf("from: %s\n", got_object_tag_get_tagger(tag)); *1/ */
1873 /* tagger_time = got_object_tag_get_tagger_time(tag); */
1874 /* /1* datestr = get_datestr(&tagger_time, datebuf); *1/ */
1875 /* /1* if (datestr) *1/ */
1876 /* /1* printf("date: %s UTC\n", datestr); *1/ */
1877 /* error = got_object_id_str(&id_str, */
1878 /* got_object_tag_get_object_id(tag)); */
1879 /* if (error) */
1880 /* break; */
1881 /* switch (got_object_tag_get_object_type(tag)) { */
1882 /* case GOT_OBJ_TYPE_BLOB: */
1883 /* /1* printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str); *1/ */
1884 /* break; */
1885 /* case GOT_OBJ_TYPE_TREE: */
1886 /* /1* printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str); *1/ */
1887 /* break; */
1888 /* case GOT_OBJ_TYPE_COMMIT: */
1889 /* /1* printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str); *1/ */
1890 /* break; */
1891 /* case GOT_OBJ_TYPE_TAG: */
1892 /* /1* printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str); *1/ */
1893 /* break; */
1894 /* default: */
1895 /* break; */
1896 /* } */
1897 /* free(id_str); */
1898 /* tagmsg0 = strdup(got_object_tag_get_message(tag)); */
1899 /* got_object_tag_close(tag); */
1900 /* if (tagmsg0 == NULL) { */
1901 /* error = got_error_from_errno("strdup"); */
1902 /* break; */
1903 /* } */
1905 /* tagmsg = tagmsg0; */
1906 /* do { */
1907 /* line = strsep(&tagmsg, "\n"); */
1908 /* if (line) */
1909 /* printf(" %s\n", line); */
1910 /* } while (line); */
1911 /* free(tagmsg0); */
1912 /* error = buf_append(&newsize, diffbuf, tags_row, */
1913 /* strlen(tags_row)); */
1914 error = buf_append(&newsize, diffbuf, tag_row, strlen(tag_row));
1916 free(tag_log0);
1917 free(tag_row);
1918 free(age);
1919 free(id_str);
1920 id_str = NULL;
1922 if (error || (limit && --limit == 0))
1923 break;
1925 tags = strdup(diffbuf->cb_buf);
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 char *
1938 gw_get_repo_heads(struct trans *gw_trans)
1940 const struct got_error *error = NULL;
1941 struct got_repository *repo = NULL;
1942 struct got_reflist_head refs;
1943 struct got_reflist_entry *re;
1944 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
1945 struct buf *diffbuf = NULL;
1946 size_t newsize;
1948 error = buf_alloc(&diffbuf, BUFFER_SIZE);
1949 if (error != NULL)
1950 goto done;
1952 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1953 if (error != NULL)
1954 goto done;
1956 SIMPLEQ_INIT(&refs);
1957 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
1958 NULL);
1959 if (error)
1960 goto done;
1962 SIMPLEQ_FOREACH(re, &refs, entry) {
1963 char *refname;
1965 refname = strdup(got_ref_get_name(re->ref));
1966 if (refname == NULL) {
1967 error = got_error_from_errno("got_ref_to_str");
1968 goto done;
1971 if (strncmp(refname, "refs/heads/", 11) != 0) {
1972 free(refname);
1973 continue;
1976 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
1977 TM_DIFF);
1979 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
1980 refname, gw_trans->repo_name, refname,
1981 gw_trans->repo_name, refname, gw_trans->repo_name,
1982 refname)) == -1) {
1983 error = got_error_from_errno("asprintf");
1984 goto done;
1987 if (strncmp(refname, "refs/heads/", 11) == 0)
1988 refname += 11;
1990 if ((asprintf(&head_row, heads_row, age, refname,
1991 head_navs_disp)) == -1) {
1992 error = got_error_from_errno("asprintf");
1993 goto done;
1996 error = buf_append(&newsize, diffbuf, head_row,
1997 strlen(head_row));
1999 free(head_navs_disp);
2000 free(head_row);
2003 heads = strdup(diffbuf->cb_buf);
2004 done:
2005 buf_free(diffbuf);
2006 got_ref_list_free(&refs);
2007 if (repo)
2008 got_repo_close(repo);
2009 if (error)
2010 return NULL;
2011 else
2012 return heads;
2015 static char *
2016 gw_get_got_link(struct trans *gw_trans)
2018 char *link;
2020 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2021 gw_trans->gw_conf->got_logo)) == -1)
2022 return NULL;
2024 return link;
2027 static char *
2028 gw_get_site_link(struct trans *gw_trans)
2030 char *link, *repo = "", *action = "";
2032 if (gw_trans->repo_name != NULL)
2033 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2034 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2035 return NULL;
2037 if (gw_trans->action_name != NULL)
2038 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2039 return NULL;
2041 if ((asprintf(&link, site_link, GOTWEB,
2042 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2043 return NULL;
2045 return link;
2048 static char *
2049 gw_html_escape(const char *html)
2051 char *escaped_str = NULL, *buf;
2052 char c[1];
2053 size_t sz, i;
2055 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
2056 return NULL;
2058 if (html == NULL)
2059 return NULL;
2060 else
2061 if ((sz = strlen(html)) == 0)
2062 return NULL;
2064 /* only work with BUFFER_SIZE */
2065 if (BUFFER_SIZE < sz)
2066 sz = BUFFER_SIZE;
2068 for (i = 0; i < sz; i++) {
2069 c[0] = html[i];
2070 switch (c[0]) {
2071 case ('>'):
2072 strcat(buf, "&gt;");
2073 break;
2074 case ('&'):
2075 strcat(buf, "&amp;");
2076 break;
2077 case ('<'):
2078 strcat(buf, "&lt;");
2079 break;
2080 case ('"'):
2081 strcat(buf, "&quot;");
2082 break;
2083 case ('\''):
2084 strcat(buf, "&apos;");
2085 break;
2086 case ('\n'):
2087 strcat(buf, "<br />");
2088 case ('|'):
2089 strcat(buf, " ");
2090 default:
2091 strcat(buf, &c[0]);
2092 break;
2095 asprintf(&escaped_str, "%s", buf);
2096 free(buf);
2097 return escaped_str;
2100 int
2101 main()
2103 const struct got_error *error = NULL;
2104 struct trans *gw_trans;
2105 struct gw_dir *dir = NULL, *tdir;
2106 const char *page = "index";
2107 int gw_malloc = 1;
2109 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
2110 errx(1, "malloc");
2112 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2113 errx(1, "malloc");
2115 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2116 errx(1, "malloc");
2118 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2119 errx(1, "malloc");
2121 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
2122 &page, 1, 0))
2123 errx(1, "khttp_parse");
2125 if ((gw_trans->gw_conf =
2126 malloc(sizeof(struct gotweb_conf))) == NULL) {
2127 gw_malloc = 0;
2128 error = got_error_from_errno("malloc");
2129 goto err;
2132 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
2133 error = got_error_from_errno("pledge");
2134 goto err;
2137 TAILQ_INIT(&gw_trans->gw_dirs);
2139 gw_trans->page = 0;
2140 gw_trans->repos_total = 0;
2141 gw_trans->repo_path = NULL;
2142 gw_trans->commit = NULL;
2143 gw_trans->headref = strdup(GOT_REF_HEAD);
2144 gw_trans->mime = KMIME_TEXT_HTML;
2145 gw_trans->gw_tmpl->key = templs;
2146 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2147 gw_trans->gw_tmpl->arg = gw_trans;
2148 gw_trans->gw_tmpl->cb = gw_template;
2149 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2151 err:
2152 if (error) {
2153 gw_trans->mime = KMIME_TEXT_PLAIN;
2154 gw_trans->action = GW_ERR;
2155 gw_display_index(gw_trans, error);
2156 goto done;
2159 error = gw_parse_querystring(gw_trans);
2160 if (error)
2161 goto err;
2163 gw_display_index(gw_trans, error);
2165 done:
2166 if (gw_malloc) {
2167 free(gw_trans->gw_conf->got_repos_path);
2168 free(gw_trans->gw_conf->got_www_path);
2169 free(gw_trans->gw_conf->got_site_name);
2170 free(gw_trans->gw_conf->got_site_owner);
2171 free(gw_trans->gw_conf->got_site_link);
2172 free(gw_trans->gw_conf->got_logo);
2173 free(gw_trans->gw_conf->got_logo_url);
2174 free(gw_trans->gw_conf);
2175 free(gw_trans->commit);
2176 free(gw_trans->repo_path);
2177 free(gw_trans->repo_name);
2178 free(gw_trans->repo_file);
2179 free(gw_trans->action_name);
2180 free(gw_trans->headref);
2182 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2183 free(dir->name);
2184 free(dir->description);
2185 free(dir->age);
2186 free(dir->url);
2187 free(dir->path);
2188 free(dir);
2193 khttp_free(gw_trans->gw_req);
2194 return EXIT_SUCCESS;