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);
642 return error;
645 static const struct got_error *
646 gw_tree(struct trans *gw_trans)
648 const struct got_error *error = NULL;
649 char *log, *log_html;
651 error = apply_unveil(gw_trans->gw_dir->path, NULL);
652 if (error)
653 return error;
655 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
657 if (log != NULL && strcmp(log, "") != 0) {
658 if ((asprintf(&log_html, log_tree, log)) == -1)
659 return got_error_from_errno("asprintf");
660 khttp_puts(gw_trans->gw_req, log_html);
661 free(log_html);
662 free(log);
664 return error;
667 static const struct got_error *
668 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
670 const struct got_error *error = NULL;
671 DIR *dt;
672 char *dir_test;
673 int opened = 0;
675 if ((asprintf(&dir_test, "%s/%s/%s",
676 gw_trans->gw_conf->got_repos_path, gw_dir->name,
677 GOTWEB_GIT_DIR)) == -1)
678 return got_error_from_errno("asprintf");
680 dt = opendir(dir_test);
681 if (dt == NULL) {
682 free(dir_test);
683 } else {
684 gw_dir->path = strdup(dir_test);
685 opened = 1;
686 goto done;
689 if ((asprintf(&dir_test, "%s/%s/%s",
690 gw_trans->gw_conf->got_repos_path, gw_dir->name,
691 GOTWEB_GOT_DIR)) == -1)
692 return got_error_from_errno("asprintf");
694 dt = opendir(dir_test);
695 if (dt == NULL)
696 free(dir_test);
697 else {
698 opened = 1;
699 error = got_error(GOT_ERR_NOT_GIT_REPO);
700 goto errored;
703 if ((asprintf(&dir_test, "%s/%s",
704 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
705 return got_error_from_errno("asprintf");
707 gw_dir->path = strdup(dir_test);
709 done:
710 gw_dir->description = gw_get_repo_description(gw_trans,
711 gw_dir->path);
712 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
713 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
714 TM_DIFF);
715 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
717 errored:
718 free(dir_test);
719 if (opened)
720 closedir(dt);
721 return error;
724 static const struct got_error *
725 gw_load_got_paths(struct trans *gw_trans)
727 const struct got_error *error = NULL;
728 DIR *d;
729 struct dirent **sd_dent;
730 struct gw_dir *gw_dir;
731 struct stat st;
732 unsigned int d_cnt, d_i;
734 d = opendir(gw_trans->gw_conf->got_repos_path);
735 if (d == NULL) {
736 error = got_error_from_errno2("opendir",
737 gw_trans->gw_conf->got_repos_path);
738 return error;
741 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
742 alphasort);
743 if (d_cnt == -1) {
744 error = got_error_from_errno2("scandir",
745 gw_trans->gw_conf->got_repos_path);
746 return error;
749 for (d_i = 0; d_i < d_cnt; d_i++) {
750 if (gw_trans->gw_conf->got_max_repos > 0 &&
751 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
752 break; /* account for parent and self */
754 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
755 strcmp(sd_dent[d_i]->d_name, "..") == 0)
756 continue;
758 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
759 return got_error_from_errno("gw_dir malloc");
761 error = gw_load_got_path(gw_trans, gw_dir);
762 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
763 continue;
764 else if (error)
765 return error;
767 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
768 !got_path_dir_is_empty(gw_dir->path)) {
769 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
770 entry);
771 gw_trans->repos_total++;
775 closedir(d);
776 return error;
779 static const struct got_error *
780 gw_parse_querystring(struct trans *gw_trans)
782 const struct got_error *error = NULL;
783 struct kpair *p;
784 struct gw_query_action *action = NULL;
785 unsigned int i;
787 if (gw_trans->gw_req->fieldnmap[0]) {
788 error = got_error_from_errno("bad parse");
789 return error;
790 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
791 /* define gw_trans->repo_path */
792 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
793 return got_error_from_errno("asprintf");
795 if ((asprintf(&gw_trans->repo_path, "%s/%s",
796 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
797 return got_error_from_errno("asprintf");
799 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
800 if ((asprintf(&gw_trans->commit, "%s",
801 p->parsed.s)) == -1)
802 return got_error_from_errno("asprintf");
804 /* get action and set function */
805 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
806 for (i = 0; i < nitems(gw_query_funcs); i++) {
807 action = &gw_query_funcs[i];
808 if (action->func_name == NULL)
809 continue;
811 if (strcmp(action->func_name,
812 p->parsed.s) == 0) {
813 gw_trans->action = i;
814 if ((asprintf(&gw_trans->action_name,
815 "%s", action->func_name)) == -1)
816 return
817 got_error_from_errno(
818 "asprintf");
820 break;
823 action = NULL;
826 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
827 if ((asprintf(&gw_trans->repo_file, "%s",
828 p->parsed.s)) == -1)
829 return got_error_from_errno("asprintf");
831 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
832 if ((asprintf(&gw_trans->headref, "%s",
833 p->parsed.s)) == -1)
834 return got_error_from_errno("asprintf");
836 if (action == NULL) {
837 error = got_error_from_errno("invalid action");
838 return error;
840 if ((gw_trans->gw_dir =
841 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
842 return got_error_from_errno("gw_dir malloc");
844 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
845 if (error)
846 return error;
847 } else
848 gw_trans->action = GW_INDEX;
850 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
851 gw_trans->page = p->parsed.i;
853 if (gw_trans->action == GW_RAW)
854 gw_trans->mime = KMIME_TEXT_PLAIN;
856 return error;
859 static struct gw_dir *
860 gw_init_gw_dir(char *dir)
862 struct gw_dir *gw_dir;
864 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
865 return NULL;
867 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
868 return NULL;
870 return gw_dir;
873 static const struct got_error*
874 match_logmsg(int *have_match, struct got_object_id *id,
875 struct got_commit_object *commit, regex_t *regex)
877 const struct got_error *err = NULL;
878 regmatch_t regmatch;
879 char *id_str = NULL, *logmsg = NULL;
881 *have_match = 0;
883 err = got_object_id_str(&id_str, id);
884 if (err)
885 return err;
887 err = got_object_commit_get_logmsg(&logmsg, commit);
888 if (err)
889 goto done;
891 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
892 *have_match = 1;
893 done:
894 free(id_str);
895 free(logmsg);
896 return err;
899 static void
900 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
902 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
903 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
904 khttps[code]);
905 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
906 kmimetypes[mime]);
907 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
908 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
909 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
910 khttp_body(gw_trans->gw_req);
913 static void
914 gw_display_index(struct trans *gw_trans, const struct got_error *err)
916 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
917 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
919 if (err)
920 khttp_puts(gw_trans->gw_req, err->msg);
921 else
922 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
923 gw_query_funcs[gw_trans->action].template);
925 khtml_close(gw_trans->gw_html_req);
928 static int
929 gw_template(size_t key, void *arg)
931 const struct got_error *error = NULL;
932 struct trans *gw_trans = arg;
933 char *gw_got_link, *gw_site_link;
934 char *site_owner_name, *site_owner_name_h;
936 switch (key) {
937 case (TEMPL_HEAD):
938 khttp_puts(gw_trans->gw_req, head);
939 break;
940 case(TEMPL_HEADER):
941 gw_got_link = gw_get_got_link(gw_trans);
942 if (gw_got_link != NULL)
943 khttp_puts(gw_trans->gw_req, gw_got_link);
945 free(gw_got_link);
946 break;
947 case (TEMPL_SITEPATH):
948 gw_site_link = gw_get_site_link(gw_trans);
949 if (gw_site_link != NULL)
950 khttp_puts(gw_trans->gw_req, gw_site_link);
952 free(gw_site_link);
953 break;
954 case(TEMPL_TITLE):
955 if (gw_trans->gw_conf->got_site_name != NULL)
956 khtml_puts(gw_trans->gw_html_req,
957 gw_trans->gw_conf->got_site_name);
959 break;
960 case (TEMPL_SEARCH):
961 khttp_puts(gw_trans->gw_req, search);
962 break;
963 case(TEMPL_SITEOWNER):
964 if (gw_trans->gw_conf->got_site_owner != NULL &&
965 gw_trans->gw_conf->got_show_site_owner) {
966 site_owner_name =
967 gw_html_escape(gw_trans->gw_conf->got_site_owner);
968 if ((asprintf(&site_owner_name_h, site_owner,
969 site_owner_name))
970 == -1)
971 return 0;
973 khttp_puts(gw_trans->gw_req, site_owner_name_h);
974 free(site_owner_name);
975 free(site_owner_name_h);
977 break;
978 case(TEMPL_CONTENT):
979 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
980 if (error)
981 khttp_puts(gw_trans->gw_req, error->msg);
983 break;
984 default:
985 return 0;
986 break;
988 return 1;
991 static char *
992 gw_get_repo_description(struct trans *gw_trans, char *dir)
994 FILE *f;
995 char *description = NULL, *d_file = NULL;
996 unsigned int len;
998 if (gw_trans->gw_conf->got_show_repo_description == false)
999 goto err;
1001 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1002 goto err;
1004 if ((f = fopen(d_file, "r")) == NULL)
1005 goto err;
1007 fseek(f, 0, SEEK_END);
1008 len = ftell(f) + 1;
1009 fseek(f, 0, SEEK_SET);
1010 if ((description = calloc(len, sizeof(char *))) == NULL)
1011 goto err;
1013 fread(description, 1, len, f);
1014 fclose(f);
1015 free(d_file);
1016 return description;
1017 err:
1018 if ((asprintf(&description, "%s", "")) == -1)
1019 return NULL;
1021 return description;
1024 static char *
1025 gw_get_time_str(time_t committer_time, int ref_tm)
1027 struct tm tm;
1028 time_t diff_time;
1029 char *years = "years ago", *months = "months ago";
1030 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1031 char *minutes = "minutes ago", *seconds = "seconds ago";
1032 char *now = "right now";
1033 char *repo_age, *s;
1034 char datebuf[29];
1036 switch (ref_tm) {
1037 case TM_DIFF:
1038 diff_time = time(NULL) - committer_time;
1039 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1040 if ((asprintf(&repo_age, "%lld %s",
1041 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1042 return NULL;
1043 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1044 if ((asprintf(&repo_age, "%lld %s",
1045 (diff_time / 60 / 60 / 24 / (365 / 12)),
1046 months)) == -1)
1047 return NULL;
1048 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1049 if ((asprintf(&repo_age, "%lld %s",
1050 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1051 return NULL;
1052 } else if (diff_time > 60 * 60 * 24 * 2) {
1053 if ((asprintf(&repo_age, "%lld %s",
1054 (diff_time / 60 / 60 / 24), days)) == -1)
1055 return NULL;
1056 } else if (diff_time > 60 * 60 * 2) {
1057 if ((asprintf(&repo_age, "%lld %s",
1058 (diff_time / 60 / 60), hours)) == -1)
1059 return NULL;
1060 } else if (diff_time > 60 * 2) {
1061 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1062 minutes)) == -1)
1063 return NULL;
1064 } else if (diff_time > 2) {
1065 if ((asprintf(&repo_age, "%lld %s", diff_time,
1066 seconds)) == -1)
1067 return NULL;
1068 } else {
1069 if ((asprintf(&repo_age, "%s", now)) == -1)
1070 return NULL;
1072 break;
1073 case TM_LONG:
1074 if (gmtime_r(&committer_time, &tm) == NULL)
1075 return NULL;
1077 s = asctime_r(&tm, datebuf);
1078 if (s == NULL)
1079 return NULL;
1081 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1082 return NULL;
1083 break;
1085 return repo_age;
1088 static char *
1089 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
1091 const struct got_error *error = NULL;
1092 struct got_object_id *id = NULL;
1093 struct got_repository *repo = NULL;
1094 struct got_commit_object *commit = NULL;
1095 struct got_reflist_head refs;
1096 struct got_reflist_entry *re;
1097 struct got_reference *head_ref;
1098 int is_head = 0;
1099 time_t committer_time = 0, cmp_time = 0;
1100 const char *refname;
1101 char *repo_age = NULL;
1103 if (repo_ref == NULL)
1104 return NULL;
1106 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1107 is_head = 1;
1109 SIMPLEQ_INIT(&refs);
1110 if (gw_trans->gw_conf->got_show_repo_age == false) {
1111 if ((asprintf(&repo_age, "")) == -1)
1112 return NULL;
1113 return repo_age;
1116 error = got_repo_open(&repo, dir, NULL);
1117 if (error != NULL)
1118 goto err;
1120 if (is_head)
1121 error = got_ref_list(&refs, repo, "refs/heads",
1122 got_ref_cmp_by_name, NULL);
1123 else
1124 error = got_ref_list(&refs, repo, repo_ref,
1125 got_ref_cmp_by_name, NULL);
1126 if (error != NULL)
1127 goto err;
1129 SIMPLEQ_FOREACH(re, &refs, entry) {
1130 if (is_head)
1131 refname = strdup(repo_ref);
1132 else
1133 refname = got_ref_get_name(re->ref);
1134 error = got_ref_open(&head_ref, repo, refname, 0);
1135 if (error != NULL)
1136 goto err;
1138 error = got_ref_resolve(&id, repo, head_ref);
1139 got_ref_close(head_ref);
1140 if (error != NULL)
1141 goto err;
1143 error = got_object_open_as_commit(&commit, repo, id);
1144 if (error != NULL)
1145 goto err;
1147 committer_time =
1148 got_object_commit_get_committer_time(commit);
1150 if (cmp_time < committer_time)
1151 cmp_time = committer_time;
1154 if (cmp_time != 0) {
1155 committer_time = cmp_time;
1156 repo_age = gw_get_time_str(committer_time, ref_tm);
1157 } else
1158 if ((asprintf(&repo_age, "")) == -1)
1159 return NULL;
1160 got_ref_list_free(&refs);
1161 free(id);
1162 return repo_age;
1163 err:
1164 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1165 return NULL;
1167 return repo_age;
1170 static char *
1171 gw_get_repo_owner(struct trans *gw_trans, char *dir)
1173 FILE *f;
1174 char *owner = NULL, *d_file = NULL;
1175 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1176 char *comp, *pos, *buf;
1177 unsigned int i;
1179 if (gw_trans->gw_conf->got_show_repo_owner == false)
1180 goto err;
1182 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1183 goto err;
1185 if ((f = fopen(d_file, "r")) == NULL)
1186 goto err;
1188 if ((buf = calloc(128, sizeof(char *))) == NULL)
1189 goto err;
1191 while ((fgets(buf, 128, f)) != NULL) {
1192 if ((pos = strstr(buf, gotweb)) != NULL)
1193 break;
1195 if ((pos = strstr(buf, gitweb)) != NULL)
1196 break;
1199 if (pos == NULL)
1200 goto err;
1202 do {
1203 fgets(buf, 128, f);
1204 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1206 if (comp == NULL)
1207 goto err;
1209 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1210 goto err;
1212 for (i = 0; i < 2; i++) {
1213 owner = strsep(&buf, "\"");
1216 if (owner == NULL)
1217 goto err;
1219 fclose(f);
1220 free(d_file);
1221 return owner;
1222 err:
1223 if ((asprintf(&owner, "%s", "")) == -1)
1224 return NULL;
1226 return owner;
1229 static char *
1230 gw_get_clone_url(struct trans *gw_trans, char *dir)
1232 FILE *f;
1233 char *url = NULL, *d_file = NULL;
1234 unsigned int len;
1236 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1237 return NULL;
1239 if ((f = fopen(d_file, "r")) == NULL)
1240 return NULL;
1242 fseek(f, 0, SEEK_END);
1243 len = ftell(f) + 1;
1244 fseek(f, 0, SEEK_SET);
1246 if ((url = calloc(len, sizeof(char *))) == NULL)
1247 return NULL;
1249 fread(url, 1, len, f);
1250 fclose(f);
1251 free(d_file);
1252 return url;
1255 static char *
1256 gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1257 char *start_commit, int limit, int log_type)
1259 const struct got_error *error;
1260 struct got_repository *repo = NULL;
1261 struct got_reflist_head refs;
1262 struct got_reflist_entry *re;
1263 struct got_commit_object *commit = NULL;
1264 struct got_object_id *id1 = NULL, *id2 = NULL;
1265 struct got_object_qid *parent_id;
1266 struct got_commit_graph *graph = NULL;
1267 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1268 *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1269 *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1270 *commit_commit_disp = NULL, *commit_age_diff = NULL,
1271 *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1272 *commit_age_long_disp = NULL, *commit_author = NULL,
1273 *commit_author_disp = NULL, *commit_committer = NULL,
1274 *commit_committer_disp = NULL, *commit_log = NULL,
1275 *commit_log_disp = NULL, *commit_parent = NULL,
1276 *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1277 *log_tree_html = NULL, *log_commit_html = NULL,
1278 *log_diff_html = NULL, *commit_tree = NULL,
1279 *commit_tree_disp = NULL;
1280 char *commit_log0, *newline;
1281 regex_t regex;
1282 int have_match;
1283 size_t newsize;
1284 struct buf *diffbuf = NULL;
1285 time_t committer_time;
1287 error = buf_alloc(&diffbuf, 0);
1288 if (error != NULL)
1289 return NULL;
1291 if (search_pattern &&
1292 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB |
1293 REG_NEWLINE))
1294 return NULL;
1296 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1297 if (error != NULL)
1298 return NULL;
1300 SIMPLEQ_INIT(&refs);
1302 if (start_commit == NULL) {
1303 struct got_reference *head_ref;
1304 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1305 if (error != NULL)
1306 goto done;
1308 error = got_ref_resolve(&id1, repo, head_ref);
1309 got_ref_close(head_ref);
1310 if (error != NULL)
1311 goto done;
1313 error = got_object_open_as_commit(&commit, repo, id1);
1314 } else {
1315 struct got_reference *ref;
1316 error = got_ref_open(&ref, repo, start_commit, 0);
1317 if (error == NULL) {
1318 int obj_type;
1319 error = got_ref_resolve(&id1, repo, ref);
1320 got_ref_close(ref);
1321 if (error != NULL)
1322 goto done;
1323 error = got_object_get_type(&obj_type, repo, id1);
1324 if (error != NULL)
1325 goto done;
1326 if (obj_type == GOT_OBJ_TYPE_TAG) {
1327 struct got_tag_object *tag;
1328 error = got_object_open_as_tag(&tag, repo, id1);
1329 if (error != NULL)
1330 goto done;
1331 if (got_object_tag_get_object_type(tag) !=
1332 GOT_OBJ_TYPE_COMMIT) {
1333 got_object_tag_close(tag);
1334 error = got_error(GOT_ERR_OBJ_TYPE);
1335 goto done;
1337 free(id1);
1338 id1 = got_object_id_dup(
1339 got_object_tag_get_object_id(tag));
1340 if (id1 == NULL)
1341 error = got_error_from_errno(
1342 "got_object_id_dup");
1343 got_object_tag_close(tag);
1344 if (error)
1345 goto done;
1346 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1347 error = got_error(GOT_ERR_OBJ_TYPE);
1348 goto done;
1350 error = got_object_open_as_commit(&commit, repo, id1);
1351 if (error != NULL)
1352 goto done;
1354 if (commit == NULL) {
1355 error = got_repo_match_object_id_prefix(&id1,
1356 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1357 if (error != NULL)
1358 goto done;
1360 error = got_repo_match_object_id_prefix(&id1,
1361 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1362 if (error != NULL)
1363 goto done;
1366 if (error != NULL)
1367 goto done;
1369 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1370 if (error != NULL)
1371 goto done;
1373 if (in_repo_path) {
1374 free(path);
1375 path = in_repo_path;
1378 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1379 if (error)
1380 goto done;
1382 error = got_commit_graph_open(&graph, id1, path, 0, repo);
1383 if (error)
1384 goto done;
1386 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1387 if (error)
1388 goto done;
1390 for (;;) {
1391 error = got_commit_graph_iter_next(&id1, graph);
1392 if (error) {
1393 if (error->code == GOT_ERR_ITER_COMPLETED) {
1394 error = NULL;
1395 break;
1397 if (error->code != GOT_ERR_ITER_NEED_MORE)
1398 break;
1399 error = got_commit_graph_fetch_commits(graph, 1, repo,
1400 NULL, NULL);
1401 if (error)
1402 break;
1403 else
1404 continue;
1406 if (id1 == NULL)
1407 break;
1409 error = got_object_open_as_commit(&commit, repo, id1);
1410 if (error)
1411 break;
1413 if (search_pattern) {
1414 error = match_logmsg(&have_match, id1, commit,
1415 &regex);
1416 if (error) {
1417 got_object_commit_close(commit);
1418 break;
1420 if (have_match == 0) {
1421 got_object_commit_close(commit);
1422 continue;
1426 SIMPLEQ_FOREACH(re, &refs, entry) {
1427 char *s;
1428 const char *name;
1429 struct got_tag_object *tag = NULL;
1430 int cmp;
1432 name = got_ref_get_name(re->ref);
1433 if (strcmp(name, GOT_REF_HEAD) == 0)
1434 continue;
1435 if (strncmp(name, "refs/", 5) == 0)
1436 name += 5;
1437 if (strncmp(name, "got/", 4) == 0)
1438 continue;
1439 if (strncmp(name, "heads/", 6) == 0)
1440 name += 6;
1441 if (strncmp(name, "remotes/", 8) == 0)
1442 name += 8;
1443 if (strncmp(name, "tags/", 5) == 0) {
1444 error = got_object_open_as_tag(&tag, repo,
1445 re->id);
1446 if (error) {
1447 if (error->code != GOT_ERR_OBJ_TYPE)
1448 continue;
1450 * Ref points at something other
1451 * than a tag.
1453 error = NULL;
1454 tag = NULL;
1457 cmp = got_object_id_cmp(tag ?
1458 got_object_tag_get_object_id(tag) : re->id, id1);
1459 if (tag)
1460 got_object_tag_close(tag);
1461 if (cmp != 0)
1462 continue;
1463 s = refs_str;
1464 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1465 s ? ", " : "", name)) == -1) {
1466 error = got_error_from_errno("asprintf");
1467 free(s);
1468 goto done;
1470 free(s);
1473 if (refs_str == NULL)
1474 refs_str_disp = strdup("");
1475 else {
1476 if ((asprintf(&refs_str_disp, "(%s)",
1477 refs_str)) == -1) {
1478 error = got_error_from_errno("asprintf");
1479 free(refs_str);
1480 goto done;
1484 error = got_object_id_str(&id_str1, id1);
1485 if (error)
1486 goto done;
1488 error = got_object_id_str(&treeid,
1489 got_object_commit_get_tree_id(commit));
1490 if (error)
1491 goto done;
1493 if (gw_trans->action == GW_COMMIT ||
1494 gw_trans->action == GW_COMMITDIFF) {
1495 parent_id =
1496 SIMPLEQ_FIRST(
1497 got_object_commit_get_parent_ids(commit));
1498 if (parent_id != NULL) {
1499 id2 = got_object_id_dup(parent_id->id);
1500 free (parent_id);
1501 error = got_object_id_str(&id_str2, id2);
1502 if (error)
1503 goto done;
1504 free(id2);
1505 } else
1506 id_str2 = strdup("/dev/null");
1509 committer_time =
1510 got_object_commit_get_committer_time(commit);
1512 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1513 error = got_error_from_errno("asprintf");
1514 goto done;
1517 if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1518 error = got_error_from_errno("asprintf");
1519 goto done;
1522 if ((asprintf(&commit_tree_disp, commit_tree_html,
1523 treeid)) == -1) {
1524 error = got_error_from_errno("asprintf");
1525 goto done;
1528 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1529 id_str1)) == -1) {
1530 error = got_error_from_errno("asprintf");
1531 goto done;
1534 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1535 error = got_error_from_errno("asprintf");
1536 goto done;
1539 if ((asprintf(&commit_commit_disp, commit_commit_html,
1540 commit_commit, refs_str_disp)) == -1) {
1541 error = got_error_from_errno("asprintf");
1542 goto done;
1545 if ((asprintf(&commit_age_long, "%s",
1546 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1547 error = got_error_from_errno("asprintf");
1548 goto done;
1551 if ((asprintf(&commit_age_long_disp, commit_age_html,
1552 commit_age_long)) == -1) {
1553 error = got_error_from_errno("asprintf");
1554 goto done;
1557 if ((asprintf(&commit_age_diff, "%s",
1558 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1559 error = got_error_from_errno("asprintf");
1560 goto done;
1563 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1564 commit_age_diff)) == -1) {
1565 error = got_error_from_errno("asprintf");
1566 goto done;
1569 if ((asprintf(&commit_author, "%s",
1570 got_object_commit_get_author(commit))) == -1) {
1571 error = got_error_from_errno("asprintf");
1572 goto done;
1575 if ((asprintf(&commit_author_disp, commit_author_html,
1576 gw_html_escape(commit_author))) == -1) {
1577 error = got_error_from_errno("asprintf");
1578 goto done;
1581 if ((asprintf(&commit_committer, "%s",
1582 got_object_commit_get_committer(commit))) == -1) {
1583 error = got_error_from_errno("asprintf");
1584 goto done;
1587 if ((asprintf(&commit_committer_disp, commit_committer_html,
1588 gw_html_escape(commit_committer))) == -1) {
1589 error = got_error_from_errno("asprintf");
1590 goto done;
1593 if (strcmp(commit_author, commit_committer) == 0) {
1594 free(commit_committer_disp);
1595 commit_committer_disp = strdup("");
1598 error = got_object_commit_get_logmsg(&commit_log0, commit);
1599 if (error)
1600 goto done;
1602 commit_log = commit_log0;
1603 while (*commit_log == '\n')
1604 commit_log++;
1606 switch(log_type) {
1607 case (LOGBRIEF):
1608 newline = strchr(commit_log, '\n');
1609 if (newline)
1610 *newline = '\0';
1612 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1613 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1614 id_str1, gw_trans->repo_name, id_str1,
1615 gw_trans->repo_name, id_str1)) == -1) {
1616 error = got_error_from_errno("asprintf");
1617 goto done;
1620 if ((asprintf(&commit_row, logbriefs_row,
1621 commit_age_diff, commit_author, commit_log,
1622 logbriefs_navs_html)) == -1) {
1623 error = got_error_from_errno("asprintf");
1624 goto done;
1627 free(logbriefs_navs_html);
1628 logbriefs_navs_html = NULL;
1629 break;
1630 case (LOGFULL):
1631 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1632 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1633 id_str1, gw_trans->repo_name, id_str1,
1634 gw_trans->repo_name, id_str1)) == -1) {
1635 error = got_error_from_errno("asprintf");
1636 goto done;
1639 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1640 commit_author_disp, commit_committer_disp,
1641 commit_age_long_disp, gw_html_escape(commit_log),
1642 logbriefs_navs_html)) == -1) {
1643 error = got_error_from_errno("asprintf");
1644 goto done;
1647 free(logbriefs_navs_html);
1648 logbriefs_navs_html = NULL;
1649 break;
1650 case (LOGTREE):
1651 log_tree_html = strdup("log tree here");
1653 if ((asprintf(&commit_row, log_tree_row,
1654 gw_html_escape(commit_log), log_tree_html)) == -1) {
1655 error = got_error_from_errno("asprintf");
1656 goto done;
1659 free(log_tree_html);
1660 break;
1661 case (LOGCOMMIT):
1662 if ((asprintf(&commit_log_disp, commit_log_html,
1663 gw_html_escape(commit_log))) == -1) {
1664 error = got_error_from_errno("asprintf");
1665 goto done;
1668 log_commit_html = strdup("commit here");
1670 if ((asprintf(&commit_row, log_commit_row,
1671 commit_diff_disp, commit_commit_disp,
1672 commit_tree_disp, commit_author_disp,
1673 commit_committer_disp, commit_age_long_disp,
1674 commit_log_disp, log_commit_html)) == -1) {
1675 error = got_error_from_errno("asprintf");
1676 goto done;
1678 free(commit_log_disp);
1679 free(log_commit_html);
1681 break;
1682 case (LOGDIFF):
1683 if ((asprintf(&commit_log_disp, commit_log_html,
1684 gw_html_escape(commit_log))) == -1) {
1685 error = got_error_from_errno("asprintf");
1686 goto done;
1689 log_diff_html = strdup("diff here");
1691 if ((asprintf(&commit_row, log_diff_row,
1692 commit_diff_disp, commit_commit_disp,
1693 commit_tree_disp, commit_author_disp,
1694 commit_committer_disp, commit_age_long_disp,
1695 commit_log_disp, log_diff_html)) == -1) {
1696 error = got_error_from_errno("asprintf");
1697 goto done;
1699 free(commit_log_disp);
1700 free(log_diff_html);
1702 break;
1703 default:
1704 return NULL;
1707 error = buf_puts(&newsize, diffbuf, commit_row);
1709 free(commit_parent);
1710 free(commit_diff_disp);
1711 free(commit_tree_disp);
1712 free(commit_age_diff);
1713 free(commit_age_diff_disp);
1714 free(commit_age_long);
1715 free(commit_age_long_disp);
1716 free(commit_author);
1717 free(commit_author_disp);
1718 free(commit_committer);
1719 free(commit_committer_disp);
1720 free(commit_log0);
1721 free(commit_row);
1722 free(refs_str_disp);
1723 free(refs_str);
1724 refs_str = NULL;
1725 free(id_str1);
1726 id_str1 = NULL;
1727 free(id_str2);
1728 id_str2 = NULL;
1730 if (error || (limit && --limit == 0))
1731 break;
1734 if (error)
1735 goto done;
1737 if (buf_len(diffbuf) > 0) {
1738 error = buf_putc(diffbuf, '\0');
1739 logs = strdup(buf_get(diffbuf));
1741 done:
1742 buf_free(diffbuf);
1743 if (commit != NULL)
1744 got_object_commit_close(commit);
1745 if (search_pattern)
1746 regfree(&regex);
1747 if (graph)
1748 got_commit_graph_close(graph);
1749 if (repo) {
1750 error = got_repo_close(repo);
1751 if (error != NULL)
1752 return NULL;
1754 if (error) {
1755 khttp_puts(gw_trans->gw_req, "Error: ");
1756 khttp_puts(gw_trans->gw_req, error->msg);
1757 return NULL;
1758 } else
1759 return logs;
1762 static char *
1763 gw_get_repo_tags(struct trans *gw_trans, int limit, int tag_type)
1765 const struct got_error *error = NULL;
1766 struct got_repository *repo = NULL;
1767 struct got_reflist_head refs;
1768 struct got_reflist_entry *re;
1769 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1770 *age = NULL;
1771 char *newline;
1772 struct buf *diffbuf = NULL;
1773 size_t newsize;
1775 error = buf_alloc(&diffbuf, 0);
1776 if (error != NULL)
1777 return NULL;
1778 SIMPLEQ_INIT(&refs);
1780 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1781 if (error != NULL)
1782 goto done;
1784 error = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
1785 if (error)
1786 goto done;
1788 SIMPLEQ_FOREACH(re, &refs, entry) {
1789 const char *refname;
1790 char *refstr, *tag_log0, *tag_log, *id_str;
1791 time_t tagger_time;
1792 struct got_object_id *id;
1793 struct got_tag_object *tag;
1795 refname = got_ref_get_name(re->ref);
1796 if (strncmp(refname, "refs/tags/", 10) != 0)
1797 continue;
1798 refname += 10;
1799 refstr = got_ref_to_str(re->ref);
1800 if (refstr == NULL) {
1801 error = got_error_from_errno("got_ref_to_str");
1802 goto done;
1805 error = got_ref_resolve(&id, repo, re->ref);
1806 if (error)
1807 goto done;
1808 error = got_object_open_as_tag(&tag, repo, id);
1809 free(id);
1810 if (error)
1811 goto done;
1813 tagger_time = got_object_tag_get_tagger_time(tag);
1815 error = got_object_id_str(&id_str,
1816 got_object_tag_get_object_id(tag));
1817 if (error)
1818 goto done;
1820 tag_log0 = strdup(got_object_tag_get_message(tag));
1822 if (tag_log0 == NULL) {
1823 error = got_error_from_errno("strdup");
1824 goto done;
1827 tag_log = tag_log0;
1828 while (*tag_log == '\n')
1829 tag_log++;
1831 switch (tag_type) {
1832 case TAGBRIEF:
1833 newline = strchr(tag_log, '\n');
1834 if (newline)
1835 *newline = '\0';
1837 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1838 TM_DIFF))) == -1) {
1839 error = got_error_from_errno("asprintf");
1840 goto done;
1843 if ((asprintf(&tags_navs_disp, tags_navs,
1844 gw_trans->repo_name, id_str, gw_trans->repo_name,
1845 id_str, gw_trans->repo_name, id_str,
1846 gw_trans->repo_name, id_str)) == -1) {
1847 error = got_error_from_errno("asprintf");
1848 goto done;
1851 if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
1852 tags_navs_disp)) == -1) {
1853 error = got_error_from_errno("asprintf");
1854 goto done;
1857 free(tags_navs_disp);
1858 break;
1859 case TAGFULL:
1860 break;
1861 default:
1862 break;
1865 got_object_tag_close(tag);
1867 error = buf_puts(&newsize, diffbuf, tag_row);
1869 free(id_str);
1870 free(refstr);
1871 free(age);
1872 free(tag_log0);
1873 free(tag_row);
1875 if (error || (limit && --limit == 0))
1876 break;
1879 if (buf_len(diffbuf) > 0) {
1880 error = buf_putc(diffbuf, '\0');
1881 tags = strdup(buf_get(diffbuf));
1883 done:
1884 buf_free(diffbuf);
1885 got_ref_list_free(&refs);
1886 if (repo)
1887 got_repo_close(repo);
1888 if (error)
1889 return NULL;
1890 else
1891 return tags;
1894 static char *
1895 gw_get_repo_heads(struct trans *gw_trans)
1897 const struct got_error *error = NULL;
1898 struct got_repository *repo = NULL;
1899 struct got_reflist_head refs;
1900 struct got_reflist_entry *re;
1901 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
1902 struct buf *diffbuf = NULL;
1903 size_t newsize;
1905 error = buf_alloc(&diffbuf, 0);
1906 if (error != NULL)
1907 return NULL;
1909 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1910 if (error != NULL)
1911 goto done;
1913 SIMPLEQ_INIT(&refs);
1914 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
1915 NULL);
1916 if (error)
1917 goto done;
1919 SIMPLEQ_FOREACH(re, &refs, entry) {
1920 char *refname;
1922 refname = strdup(got_ref_get_name(re->ref));
1923 if (refname == NULL) {
1924 error = got_error_from_errno("got_ref_to_str");
1925 goto done;
1928 if (strncmp(refname, "refs/heads/", 11) != 0) {
1929 free(refname);
1930 continue;
1933 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
1934 TM_DIFF);
1936 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
1937 refname, gw_trans->repo_name, refname,
1938 gw_trans->repo_name, refname, gw_trans->repo_name,
1939 refname)) == -1) {
1940 error = got_error_from_errno("asprintf");
1941 goto done;
1944 if (strncmp(refname, "refs/heads/", 11) == 0)
1945 refname += 11;
1947 if ((asprintf(&head_row, heads_row, age, refname,
1948 head_navs_disp)) == -1) {
1949 error = got_error_from_errno("asprintf");
1950 goto done;
1953 error = buf_puts(&newsize, diffbuf, head_row);
1955 free(head_navs_disp);
1956 free(head_row);
1959 if (buf_len(diffbuf) > 0) {
1960 error = buf_putc(diffbuf, '\0');
1961 heads = strdup(buf_get(diffbuf));
1963 done:
1964 buf_free(diffbuf);
1965 got_ref_list_free(&refs);
1966 if (repo)
1967 got_repo_close(repo);
1968 if (error)
1969 return NULL;
1970 else
1971 return heads;
1974 static char *
1975 gw_get_got_link(struct trans *gw_trans)
1977 char *link;
1979 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
1980 gw_trans->gw_conf->got_logo)) == -1)
1981 return NULL;
1983 return link;
1986 static char *
1987 gw_get_site_link(struct trans *gw_trans)
1989 char *link, *repo = "", *action = "";
1991 if (gw_trans->repo_name != NULL)
1992 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
1993 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
1994 return NULL;
1996 if (gw_trans->action_name != NULL)
1997 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
1998 return NULL;
2000 if ((asprintf(&link, site_link, GOTWEB,
2001 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2002 return NULL;
2004 return link;
2007 static char *
2008 gw_html_escape(const char *html)
2010 char *escaped_str = NULL, *buf;
2011 char c[1];
2012 size_t sz, i, buff_sz = 2048;
2014 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2015 return NULL;
2017 if (html == NULL)
2018 return NULL;
2019 else
2020 if ((sz = strlen(html)) == 0)
2021 return NULL;
2023 /* only work with buff_sz */
2024 if (buff_sz < sz)
2025 sz = buff_sz;
2027 for (i = 0; i < sz; i++) {
2028 c[0] = html[i];
2029 switch (c[0]) {
2030 case ('>'):
2031 strcat(buf, "&gt;");
2032 break;
2033 case ('&'):
2034 strcat(buf, "&amp;");
2035 break;
2036 case ('<'):
2037 strcat(buf, "&lt;");
2038 break;
2039 case ('"'):
2040 strcat(buf, "&quot;");
2041 break;
2042 case ('\''):
2043 strcat(buf, "&apos;");
2044 break;
2045 case ('\n'):
2046 strcat(buf, "<br />");
2047 case ('|'):
2048 strcat(buf, " ");
2049 default:
2050 strcat(buf, &c[0]);
2051 break;
2054 asprintf(&escaped_str, "%s", buf);
2055 free(buf);
2056 return escaped_str;
2059 int
2060 main()
2062 const struct got_error *error = NULL;
2063 struct trans *gw_trans;
2064 struct gw_dir *dir = NULL, *tdir;
2065 const char *page = "index";
2066 int gw_malloc = 1;
2068 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
2069 errx(1, "malloc");
2071 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2072 errx(1, "malloc");
2074 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2075 errx(1, "malloc");
2077 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2078 errx(1, "malloc");
2080 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
2081 &page, 1, 0))
2082 errx(1, "khttp_parse");
2084 if ((gw_trans->gw_conf =
2085 malloc(sizeof(struct gotweb_conf))) == NULL) {
2086 gw_malloc = 0;
2087 error = got_error_from_errno("malloc");
2088 goto err;
2091 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
2092 error = got_error_from_errno("pledge");
2093 goto err;
2096 TAILQ_INIT(&gw_trans->gw_dirs);
2098 gw_trans->page = 0;
2099 gw_trans->repos_total = 0;
2100 gw_trans->repo_path = NULL;
2101 gw_trans->commit = NULL;
2102 gw_trans->headref = strdup(GOT_REF_HEAD);
2103 gw_trans->mime = KMIME_TEXT_HTML;
2104 gw_trans->gw_tmpl->key = templs;
2105 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2106 gw_trans->gw_tmpl->arg = gw_trans;
2107 gw_trans->gw_tmpl->cb = gw_template;
2108 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2110 err:
2111 if (error) {
2112 gw_trans->mime = KMIME_TEXT_PLAIN;
2113 gw_trans->action = GW_ERR;
2114 gw_display_index(gw_trans, error);
2115 goto done;
2118 error = gw_parse_querystring(gw_trans);
2119 if (error)
2120 goto err;
2122 gw_display_index(gw_trans, error);
2124 done:
2125 if (gw_malloc) {
2126 free(gw_trans->gw_conf->got_repos_path);
2127 free(gw_trans->gw_conf->got_www_path);
2128 free(gw_trans->gw_conf->got_site_name);
2129 free(gw_trans->gw_conf->got_site_owner);
2130 free(gw_trans->gw_conf->got_site_link);
2131 free(gw_trans->gw_conf->got_logo);
2132 free(gw_trans->gw_conf->got_logo_url);
2133 free(gw_trans->gw_conf);
2134 free(gw_trans->commit);
2135 free(gw_trans->repo_path);
2136 free(gw_trans->repo_name);
2137 free(gw_trans->repo_file);
2138 free(gw_trans->action_name);
2139 free(gw_trans->headref);
2141 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2142 free(dir->name);
2143 free(dir->description);
2144 free(dir->age);
2145 free(dir->url);
2146 free(dir->path);
2147 free(dir);
2152 khttp_free(gw_trans->gw_req);
2153 return EXIT_SUCCESS;