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 };
119 struct buf {
120 /* buffer handle, buffer size, and data length */
121 u_char *cb_buf;
122 size_t cb_size;
123 size_t cb_len;
124 };
126 static const char *const templs[TEMPL__MAX] = {
127 "head",
128 "header",
129 "sitepath",
130 "siteowner",
131 "title",
132 "search",
133 "content",
134 };
136 static const struct kvalid gw_keys[KEY__MAX] = {
137 { kvalid_stringne, "path" },
138 { kvalid_stringne, "action" },
139 { kvalid_stringne, "commit" },
140 { kvalid_stringne, "file" },
141 { kvalid_int, "page" },
142 { kvalid_stringne, "headref" },
143 };
145 static struct gw_dir *gw_init_gw_dir(char *);
147 static char *gw_get_repo_description(struct trans *,
148 char *);
149 static char *gw_get_repo_owner(struct trans *,
150 char *);
151 static char *gw_get_time_str(time_t, int);
152 static char *gw_get_repo_age(struct trans *,
153 char *, char *, int);
154 static char *gw_get_repo_log(struct trans *, const char *,
155 char *, int, int);
156 static char *gw_get_repo_tags(struct trans *);
157 static char *gw_get_repo_heads(struct trans *);
158 static char *gw_get_clone_url(struct trans *, char *);
159 static char *gw_get_got_link(struct trans *);
160 static char *gw_get_site_link(struct trans *);
161 static char *gw_html_escape(const char *);
163 static void gw_display_open(struct trans *, enum khttp,
164 enum kmime);
165 static void gw_display_index(struct trans *,
166 const struct got_error *);
168 static int gw_template(size_t, void *);
170 static const struct got_error* apply_unveil(const char *, const char *);
171 static const struct got_error* gw_load_got_paths(struct trans *);
172 static const struct got_error* gw_load_got_path(struct trans *,
173 struct gw_dir *);
174 static const struct got_error* gw_parse_querystring(struct trans *);
175 static const struct got_error* match_logmsg(int *, struct got_object_id *,
176 struct got_commit_object *, regex_t *);
178 static const struct got_error* gw_blame(struct trans *);
179 static const struct got_error* gw_blob(struct trans *);
180 static const struct got_error* gw_blobdiff(struct trans *);
181 static const struct got_error* gw_commit(struct trans *);
182 static const struct got_error* gw_commitdiff(struct trans *);
183 static const struct got_error* gw_history(struct trans *);
184 static const struct got_error* gw_index(struct trans *);
185 static const struct got_error* gw_log(struct trans *);
186 static const struct got_error* gw_raw(struct trans *);
187 static const struct got_error* gw_logbriefs(struct trans *);
188 static const struct got_error* gw_snapshot(struct trans *);
189 static const struct got_error* gw_summary(struct trans *);
190 static const struct got_error* gw_tree(struct trans *);
192 struct gw_query_action {
193 unsigned int func_id;
194 const char *func_name;
195 const struct got_error *(*func_main)(struct trans *);
196 char *template;
197 };
199 enum gw_query_actions {
200 GW_BLAME,
201 GW_BLOB,
202 GW_BLOBDIFF,
203 GW_COMMIT,
204 GW_COMMITDIFF,
205 GW_ERR,
206 GW_HISTORY,
207 GW_INDEX,
208 GW_LOG,
209 GW_RAW,
210 GW_LOGBRIEFS,
211 GW_SNAPSHOT,
212 GW_SUMMARY,
213 GW_TREE
214 };
216 static struct gw_query_action gw_query_funcs[] = {
217 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
218 { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
219 { GW_BLOBDIFF, "blobdiff", gw_blobdiff, "gw_tmpl/index.tmpl" },
220 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
221 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
222 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
223 { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
224 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
225 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
226 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
227 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
228 { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
229 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
230 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
231 };
233 static const struct got_error *
234 apply_unveil(const char *repo_path, const char *repo_file)
236 const struct got_error *err;
238 if (repo_path && repo_file) {
239 char *full_path;
240 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
241 return got_error_from_errno("asprintf unveil");
242 if (unveil(full_path, "r") != 0)
243 return got_error_from_errno2("unveil", full_path);
246 if (repo_path && unveil(repo_path, "r") != 0)
247 return got_error_from_errno2("unveil", repo_path);
249 if (unveil("/tmp", "rwc") != 0)
250 return got_error_from_errno2("unveil", "/tmp");
252 err = got_privsep_unveil_exec_helpers();
253 if (err != NULL)
254 return err;
256 if (unveil(NULL, NULL) != 0)
257 return got_error_from_errno("unveil");
259 return NULL;
262 static const struct got_error *
263 gw_blame(struct trans *gw_trans)
265 const struct got_error *error = NULL;
267 return error;
270 static const struct got_error *
271 gw_blob(struct trans *gw_trans)
273 const struct got_error *error = NULL;
275 return error;
278 static const struct got_error *
279 gw_blobdiff(struct trans *gw_trans)
281 const struct got_error *error = NULL;
283 return error;
286 static const struct got_error *
287 gw_commit(struct trans *gw_trans)
289 const struct got_error *error = NULL;
290 char *log, *log_html;
292 error = apply_unveil(gw_trans->gw_dir->path, NULL);
293 if (error)
294 return error;
296 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
298 if (log != NULL && strcmp(log, "") != 0) {
299 if ((asprintf(&log_html, log_commit, log)) == -1)
300 return got_error_from_errno("asprintf");
301 khttp_puts(gw_trans->gw_req, log_html);
302 free(log_html);
303 free(log);
305 return error;
308 static const struct got_error *
309 gw_commitdiff(struct trans *gw_trans)
311 const struct got_error *error = NULL;
313 return error;
316 static const struct got_error *
317 gw_history(struct trans *gw_trans)
319 const struct got_error *error = NULL;
321 return error;
324 static const struct got_error *
325 gw_index(struct trans *gw_trans)
327 const struct got_error *error = NULL;
328 struct gw_dir *gw_dir = NULL;
329 char *html, *navs, *next, *prev;
330 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
332 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
333 if (error)
334 return error;
336 error = gw_load_got_paths(gw_trans);
337 if (error)
338 return error;
340 khttp_puts(gw_trans->gw_req, index_projects_header);
342 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
343 dir_c++;
345 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
346 if (gw_trans->page > 0 && (gw_trans->page *
347 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
348 prev_disp++;
349 continue;
352 prev_disp++;
353 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
354 gw_dir->name, gw_dir->name)) == -1)
355 return got_error_from_errno("asprintf");
357 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
358 gw_dir->description, gw_dir->owner, gw_dir->age,
359 navs)) == -1)
360 return got_error_from_errno("asprintf");
362 khttp_puts(gw_trans->gw_req, html);
364 free(navs);
365 free(html);
367 if (gw_trans->gw_conf->got_max_repos_display == 0)
368 continue;
370 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
371 khttp_puts(gw_trans->gw_req, np_wrapper_start);
372 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
373 (gw_trans->page > 0) &&
374 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
375 prev_disp == gw_trans->repos_total))
376 khttp_puts(gw_trans->gw_req, np_wrapper_start);
378 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
379 (gw_trans->page > 0) &&
380 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
381 prev_disp == gw_trans->repos_total)) {
382 if ((asprintf(&prev, nav_prev,
383 gw_trans->page - 1)) == -1)
384 return got_error_from_errno("asprintf");
385 khttp_puts(gw_trans->gw_req, prev);
386 free(prev);
389 khttp_puts(gw_trans->gw_req, div_end);
391 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
392 next_disp == gw_trans->gw_conf->got_max_repos_display &&
393 dir_c != (gw_trans->page + 1) *
394 gw_trans->gw_conf->got_max_repos_display) {
395 if ((asprintf(&next, nav_next,
396 gw_trans->page + 1)) == -1)
397 return got_error_from_errno("calloc");
398 khttp_puts(gw_trans->gw_req, next);
399 khttp_puts(gw_trans->gw_req, div_end);
400 free(next);
401 next_disp = 0;
402 break;
405 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
406 (gw_trans->page > 0) &&
407 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
408 prev_disp == gw_trans->repos_total))
409 khttp_puts(gw_trans->gw_req, div_end);
411 next_disp++;
413 return error;
416 static const struct got_error *
417 gw_log(struct trans *gw_trans)
419 const struct got_error *error = NULL;
420 char *log, *log_html;
422 error = apply_unveil(gw_trans->gw_dir->path, NULL);
423 if (error)
424 return error;
426 log = gw_get_repo_log(gw_trans, NULL, NULL,
427 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
429 if (log != NULL && strcmp(log, "") != 0) {
430 if ((asprintf(&log_html, logs, log)) == -1)
431 return got_error_from_errno("asprintf");
432 khttp_puts(gw_trans->gw_req, log_html);
433 free(log_html);
434 free(log);
436 return error;
439 static const struct got_error *
440 gw_raw(struct trans *gw_trans)
442 const struct got_error *error = NULL;
444 return error;
447 static const struct got_error *
448 gw_logbriefs(struct trans *gw_trans)
450 const struct got_error *error = NULL;
451 char *log, *log_html;
453 error = apply_unveil(gw_trans->gw_dir->path, NULL);
454 if (error)
455 return error;
457 log = gw_get_repo_log(gw_trans, NULL, NULL,
458 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
460 if (log != NULL && strcmp(log, "") != 0) {
461 if ((asprintf(&log_html, summary_logbriefs,
462 log)) == -1)
463 return got_error_from_errno("asprintf");
464 khttp_puts(gw_trans->gw_req, log_html);
465 free(log_html);
466 free(log);
468 return error;
471 static const struct got_error *
472 gw_snapshot(struct trans *gw_trans)
474 const struct got_error *error = NULL;
476 return error;
479 static const struct got_error *
480 gw_summary(struct trans *gw_trans)
482 const struct got_error *error = NULL;
483 char *description_html, *repo_owner_html, *repo_age_html,
484 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
485 *heads_html, *age;
487 error = apply_unveil(gw_trans->gw_dir->path, NULL);
488 if (error)
489 return error;
491 khttp_puts(gw_trans->gw_req, summary_wrapper);
492 if (gw_trans->gw_conf->got_show_repo_description) {
493 if (gw_trans->gw_dir->description != NULL &&
494 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
495 if ((asprintf(&description_html, description,
496 gw_trans->gw_dir->description)) == -1)
497 return got_error_from_errno("asprintf");
499 khttp_puts(gw_trans->gw_req, description_html);
500 free(description_html);
504 if (gw_trans->gw_conf->got_show_repo_owner) {
505 if (gw_trans->gw_dir->owner != NULL &&
506 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
507 if ((asprintf(&repo_owner_html, repo_owner,
508 gw_trans->gw_dir->owner)) == -1)
509 return got_error_from_errno("asprintf");
511 khttp_puts(gw_trans->gw_req, repo_owner_html);
512 free(repo_owner_html);
516 if (gw_trans->gw_conf->got_show_repo_age) {
517 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
518 "refs/heads", TM_LONG);
519 if (age != NULL && (strcmp(age, "") != 0)) {
520 if ((asprintf(&repo_age_html, last_change, age)) == -1)
521 return got_error_from_errno("asprintf");
523 khttp_puts(gw_trans->gw_req, repo_age_html);
524 free(repo_age_html);
525 free(age);
529 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
530 if (gw_trans->gw_dir->url != NULL &&
531 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
532 if ((asprintf(&cloneurl_html, cloneurl,
533 gw_trans->gw_dir->url)) == -1)
534 return got_error_from_errno("asprintf");
536 khttp_puts(gw_trans->gw_req, cloneurl_html);
537 free(cloneurl_html);
540 khttp_puts(gw_trans->gw_req, div_end);
542 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
543 tags = gw_get_repo_tags(gw_trans);
544 heads = gw_get_repo_heads(gw_trans);
546 if (log != NULL && strcmp(log, "") != 0) {
547 if ((asprintf(&log_html, summary_logbriefs,
548 log)) == -1)
549 return got_error_from_errno("asprintf");
550 khttp_puts(gw_trans->gw_req, log_html);
551 free(log_html);
552 free(log);
555 if (tags != NULL && strcmp(tags, "") != 0) {
556 if ((asprintf(&tags_html, summary_tags,
557 tags)) == -1)
558 return got_error_from_errno("asprintf");
559 khttp_puts(gw_trans->gw_req, tags_html);
560 free(tags_html);
561 free(tags);
564 if (heads != NULL && strcmp(heads, "") != 0) {
565 if ((asprintf(&heads_html, summary_heads,
566 heads)) == -1)
567 return got_error_from_errno("asprintf");
568 khttp_puts(gw_trans->gw_req, heads_html);
569 free(heads_html);
570 free(heads);
573 return error;
576 static const struct got_error *
577 gw_tree(struct trans *gw_trans)
579 const struct got_error *error = NULL;
580 char *log, *log_html;
582 error = apply_unveil(gw_trans->gw_dir->path, NULL);
583 if (error)
584 return error;
586 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
588 if (log != NULL && strcmp(log, "") != 0) {
589 if ((asprintf(&log_html, log_tree, log)) == -1)
590 return got_error_from_errno("asprintf");
591 khttp_puts(gw_trans->gw_req, log_html);
592 free(log_html);
593 free(log);
595 return error;
598 static const struct got_error *
599 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
601 const struct got_error *error = NULL;
602 DIR *dt;
603 char *dir_test;
604 int opened = 0;
606 if ((asprintf(&dir_test, "%s/%s/%s",
607 gw_trans->gw_conf->got_repos_path, gw_dir->name,
608 GOTWEB_GIT_DIR)) == -1)
609 return got_error_from_errno("asprintf");
611 dt = opendir(dir_test);
612 if (dt == NULL) {
613 free(dir_test);
614 } else {
615 gw_dir->path = strdup(dir_test);
616 opened = 1;
617 goto done;
620 if ((asprintf(&dir_test, "%s/%s/%s",
621 gw_trans->gw_conf->got_repos_path, gw_dir->name,
622 GOTWEB_GOT_DIR)) == -1)
623 return got_error_from_errno("asprintf");
625 dt = opendir(dir_test);
626 if (dt == NULL)
627 free(dir_test);
628 else {
629 opened = 1;
630 error = got_error(GOT_ERR_NOT_GIT_REPO);
631 goto errored;
634 if ((asprintf(&dir_test, "%s/%s",
635 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
636 return got_error_from_errno("asprintf");
638 gw_dir->path = strdup(dir_test);
640 done:
641 gw_dir->description = gw_get_repo_description(gw_trans,
642 gw_dir->path);
643 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
644 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
645 TM_DIFF);
646 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
648 errored:
649 free(dir_test);
650 if (opened)
651 closedir(dt);
652 return error;
655 static const struct got_error *
656 gw_load_got_paths(struct trans *gw_trans)
658 const struct got_error *error = NULL;
659 DIR *d;
660 struct dirent **sd_dent;
661 struct gw_dir *gw_dir;
662 struct stat st;
663 unsigned int d_cnt, d_i;
665 d = opendir(gw_trans->gw_conf->got_repos_path);
666 if (d == NULL) {
667 error = got_error_from_errno2("opendir",
668 gw_trans->gw_conf->got_repos_path);
669 return error;
672 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
673 alphasort);
674 if (d_cnt == -1) {
675 error = got_error_from_errno2("scandir",
676 gw_trans->gw_conf->got_repos_path);
677 return error;
680 for (d_i = 0; d_i < d_cnt; d_i++) {
681 if (gw_trans->gw_conf->got_max_repos > 0 &&
682 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
683 break; /* account for parent and self */
685 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
686 strcmp(sd_dent[d_i]->d_name, "..") == 0)
687 continue;
689 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
690 return got_error_from_errno("gw_dir malloc");
692 error = gw_load_got_path(gw_trans, gw_dir);
693 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
694 continue;
695 else if (error)
696 return error;
698 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
699 !got_path_dir_is_empty(gw_dir->path)) {
700 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
701 entry);
702 gw_trans->repos_total++;
706 closedir(d);
707 return error;
710 static const struct got_error *
711 gw_parse_querystring(struct trans *gw_trans)
713 const struct got_error *error = NULL;
714 struct kpair *p;
715 struct gw_query_action *action = NULL;
716 unsigned int i;
718 if (gw_trans->gw_req->fieldnmap[0]) {
719 error = got_error_from_errno("bad parse");
720 return error;
721 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
722 /* define gw_trans->repo_path */
723 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
724 return got_error_from_errno("asprintf");
726 if ((asprintf(&gw_trans->repo_path, "%s/%s",
727 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
728 return got_error_from_errno("asprintf");
730 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
731 if ((asprintf(&gw_trans->commit, "%s",
732 p->parsed.s)) == -1)
733 return got_error_from_errno("asprintf");
735 /* get action and set function */
736 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
737 for (i = 0; i < nitems(gw_query_funcs); i++) {
738 action = &gw_query_funcs[i];
739 if (action->func_name == NULL)
740 continue;
742 if (strcmp(action->func_name,
743 p->parsed.s) == 0) {
744 gw_trans->action = i;
745 if ((asprintf(&gw_trans->action_name,
746 "%s", action->func_name)) == -1)
747 return
748 got_error_from_errno(
749 "asprintf");
751 break;
754 action = NULL;
757 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
758 if ((asprintf(&gw_trans->repo_file, "%s",
759 p->parsed.s)) == -1)
760 return got_error_from_errno("asprintf");
762 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
763 if ((asprintf(&gw_trans->headref, "%s",
764 p->parsed.s)) == -1)
765 return got_error_from_errno("asprintf");
767 if (action == NULL) {
768 error = got_error_from_errno("invalid action");
769 return error;
771 if ((gw_trans->gw_dir =
772 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
773 return got_error_from_errno("gw_dir malloc");
775 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
776 if (error)
777 return error;
778 } else
779 gw_trans->action = GW_INDEX;
781 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
782 gw_trans->page = p->parsed.i;
784 if (gw_trans->action == GW_RAW)
785 gw_trans->mime = KMIME_TEXT_PLAIN;
787 return error;
790 static struct gw_dir *
791 gw_init_gw_dir(char *dir)
793 struct gw_dir *gw_dir;
795 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
796 return NULL;
798 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
799 return NULL;
801 return gw_dir;
804 static const struct got_error*
805 match_logmsg(int *have_match, struct got_object_id *id,
806 struct got_commit_object *commit, regex_t *regex)
808 const struct got_error *err = NULL;
809 regmatch_t regmatch;
810 char *id_str = NULL, *logmsg = NULL;
812 *have_match = 0;
814 err = got_object_id_str(&id_str, id);
815 if (err)
816 return err;
818 err = got_object_commit_get_logmsg(&logmsg, commit);
819 if (err)
820 goto done;
822 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
823 *have_match = 1;
824 done:
825 free(id_str);
826 free(logmsg);
827 return err;
830 static void
831 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
833 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
834 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
835 khttps[code]);
836 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
837 kmimetypes[mime]);
838 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
839 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
840 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
841 khttp_body(gw_trans->gw_req);
844 static void
845 gw_display_index(struct trans *gw_trans, const struct got_error *err)
847 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
848 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
850 if (err)
851 khttp_puts(gw_trans->gw_req, err->msg);
852 else
853 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
854 gw_query_funcs[gw_trans->action].template);
856 khtml_close(gw_trans->gw_html_req);
859 static int
860 gw_template(size_t key, void *arg)
862 const struct got_error *error = NULL;
863 struct trans *gw_trans = arg;
864 char *gw_got_link, *gw_site_link;
865 char *site_owner_name, *site_owner_name_h;
867 switch (key) {
868 case (TEMPL_HEAD):
869 khttp_puts(gw_trans->gw_req, head);
870 break;
871 case(TEMPL_HEADER):
872 gw_got_link = gw_get_got_link(gw_trans);
873 if (gw_got_link != NULL)
874 khttp_puts(gw_trans->gw_req, gw_got_link);
876 free(gw_got_link);
877 break;
878 case (TEMPL_SITEPATH):
879 gw_site_link = gw_get_site_link(gw_trans);
880 if (gw_site_link != NULL)
881 khttp_puts(gw_trans->gw_req, gw_site_link);
883 free(gw_site_link);
884 break;
885 case(TEMPL_TITLE):
886 if (gw_trans->gw_conf->got_site_name != NULL)
887 khtml_puts(gw_trans->gw_html_req,
888 gw_trans->gw_conf->got_site_name);
890 break;
891 case (TEMPL_SEARCH):
892 khttp_puts(gw_trans->gw_req, search);
893 break;
894 case(TEMPL_SITEOWNER):
895 if (gw_trans->gw_conf->got_site_owner != NULL &&
896 gw_trans->gw_conf->got_show_site_owner) {
897 site_owner_name =
898 gw_html_escape(gw_trans->gw_conf->got_site_owner);
899 if ((asprintf(&site_owner_name_h, site_owner,
900 site_owner_name))
901 == -1)
902 return 0;
904 khttp_puts(gw_trans->gw_req, site_owner_name_h);
905 free(site_owner_name);
906 free(site_owner_name_h);
908 break;
909 case(TEMPL_CONTENT):
910 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
911 if (error)
912 khttp_puts(gw_trans->gw_req, error->msg);
914 break;
915 default:
916 return 0;
917 break;
919 return 1;
922 static char *
923 gw_get_repo_description(struct trans *gw_trans, char *dir)
925 FILE *f;
926 char *description = NULL, *d_file = NULL;
927 unsigned int len;
929 if (gw_trans->gw_conf->got_show_repo_description == false)
930 goto err;
932 if ((asprintf(&d_file, "%s/description", dir)) == -1)
933 goto err;
935 if ((f = fopen(d_file, "r")) == NULL)
936 goto err;
938 fseek(f, 0, SEEK_END);
939 len = ftell(f) + 1;
940 fseek(f, 0, SEEK_SET);
941 if ((description = calloc(len, sizeof(char *))) == NULL)
942 goto err;
944 fread(description, 1, len, f);
945 fclose(f);
946 free(d_file);
947 return description;
948 err:
949 if ((asprintf(&description, "%s", "")) == -1)
950 return NULL;
952 return description;
955 static char *
956 gw_get_time_str(time_t committer_time, int ref_tm)
958 struct tm tm;
959 time_t diff_time;
960 char *years = "years ago", *months = "months ago";
961 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
962 char *minutes = "minutes ago", *seconds = "seconds ago";
963 char *now = "right now";
964 char *repo_age, *s;
965 char datebuf[BUFFER_SIZE];
967 switch (ref_tm) {
968 case TM_DIFF:
969 diff_time = time(NULL) - committer_time;
970 if (diff_time > 60 * 60 * 24 * 365 * 2) {
971 if ((asprintf(&repo_age, "%lld %s",
972 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
973 return NULL;
974 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
975 if ((asprintf(&repo_age, "%lld %s",
976 (diff_time / 60 / 60 / 24 / (365 / 12)),
977 months)) == -1)
978 return NULL;
979 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
980 if ((asprintf(&repo_age, "%lld %s",
981 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
982 return NULL;
983 } else if (diff_time > 60 * 60 * 24 * 2) {
984 if ((asprintf(&repo_age, "%lld %s",
985 (diff_time / 60 / 60 / 24), days)) == -1)
986 return NULL;
987 } else if (diff_time > 60 * 60 * 2) {
988 if ((asprintf(&repo_age, "%lld %s",
989 (diff_time / 60 / 60), hours)) == -1)
990 return NULL;
991 } else if (diff_time > 60 * 2) {
992 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
993 minutes)) == -1)
994 return NULL;
995 } else if (diff_time > 2) {
996 if ((asprintf(&repo_age, "%lld %s", diff_time,
997 seconds)) == -1)
998 return NULL;
999 } else {
1000 if ((asprintf(&repo_age, "%s", now)) == -1)
1001 return NULL;
1003 break;
1004 case TM_LONG:
1005 if (gmtime_r(&committer_time, &tm) == NULL)
1006 return NULL;
1008 s = asctime_r(&tm, datebuf);
1009 if (s == NULL)
1010 return NULL;
1012 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1013 return NULL;
1014 break;
1016 return repo_age;
1019 static char *
1020 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
1022 const struct got_error *error = NULL;
1023 struct got_object_id *id = NULL;
1024 struct got_repository *repo = NULL;
1025 struct got_commit_object *commit = NULL;
1026 struct got_reflist_head refs;
1027 struct got_reflist_entry *re;
1028 struct got_reference *head_ref;
1029 time_t committer_time = 0, cmp_time = 0;
1030 char *repo_age = NULL;
1032 if (repo_ref == NULL)
1033 return NULL;
1035 SIMPLEQ_INIT(&refs);
1036 if (gw_trans->gw_conf->got_show_repo_age == false) {
1037 if ((asprintf(&repo_age, "")) == -1)
1038 return NULL;
1039 return repo_age;
1041 error = got_repo_open(&repo, dir, NULL);
1042 if (error != NULL)
1043 goto err;
1045 error = got_ref_list(&refs, repo, repo_ref, got_ref_cmp_by_name,
1046 NULL);
1047 if (error != NULL)
1048 goto err;
1050 const char *refname;
1051 SIMPLEQ_FOREACH(re, &refs, entry) {
1052 refname = got_ref_get_name(re->ref);
1053 error = got_ref_open(&head_ref, repo, refname, 0);
1054 if (error != NULL)
1055 goto err;
1057 error = got_ref_resolve(&id, repo, head_ref);
1058 got_ref_close(head_ref);
1059 if (error != NULL)
1060 goto err;
1062 /* here is what breaks tags, so adjust */
1063 error = got_object_open_as_commit(&commit, repo, id);
1064 if (error != NULL)
1065 goto err;
1067 committer_time =
1068 got_object_commit_get_committer_time(commit);
1070 if (cmp_time < committer_time)
1071 cmp_time = committer_time;
1074 if (cmp_time != 0) {
1075 committer_time = cmp_time;
1076 repo_age = gw_get_time_str(committer_time, ref_tm);
1077 } else
1078 if ((asprintf(&repo_age, "")) == -1)
1079 return NULL;
1080 got_ref_list_free(&refs);
1081 free(id);
1082 return repo_age;
1083 err:
1084 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1085 return NULL;
1087 return repo_age;
1090 static char *
1091 gw_get_repo_owner(struct trans *gw_trans, char *dir)
1093 FILE *f;
1094 char *owner = NULL, *d_file = NULL;
1095 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1096 char *comp, *pos, *buf;
1097 unsigned int i;
1099 if (gw_trans->gw_conf->got_show_repo_owner == false)
1100 goto err;
1102 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1103 goto err;
1105 if ((f = fopen(d_file, "r")) == NULL)
1106 goto err;
1108 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1109 goto err;
1111 while ((fgets(buf, BUFFER_SIZE, f)) != NULL) {
1112 if ((pos = strstr(buf, gotweb)) != NULL)
1113 break;
1115 if ((pos = strstr(buf, gitweb)) != NULL)
1116 break;
1119 if (pos == NULL)
1120 goto err;
1122 do {
1123 fgets(buf, BUFFER_SIZE, f);
1124 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1126 if (comp == NULL)
1127 goto err;
1129 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1130 goto err;
1132 for (i = 0; i < 2; i++) {
1133 owner = strsep(&buf, "\"");
1136 if (owner == NULL)
1137 goto err;
1139 fclose(f);
1140 free(d_file);
1141 return owner;
1142 err:
1143 if ((asprintf(&owner, "%s", "")) == -1)
1144 return NULL;
1146 return owner;
1149 static char *
1150 gw_get_clone_url(struct trans *gw_trans, char *dir)
1152 FILE *f;
1153 char *url = NULL, *d_file = NULL;
1154 unsigned int len;
1156 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1157 return NULL;
1159 if ((f = fopen(d_file, "r")) == NULL)
1160 return NULL;
1162 fseek(f, 0, SEEK_END);
1163 len = ftell(f) + 1;
1164 fseek(f, 0, SEEK_SET);
1166 if ((url = calloc(len, sizeof(char *))) == NULL)
1167 return NULL;
1169 fread(url, 1, len, f);
1170 fclose(f);
1171 free(d_file);
1172 return url;
1175 static char *
1176 gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1177 char *start_commit, int limit, int log_type)
1179 const struct got_error *error;
1180 struct got_repository *repo = NULL;
1181 struct got_reflist_head refs;
1182 struct got_reflist_entry *re;
1183 struct got_commit_object *commit = NULL;
1184 struct got_object_id *id1 = NULL, *id2 = NULL;
1185 struct got_object_qid *parent_id;
1186 struct got_commit_graph *graph = NULL;
1187 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL,
1188 *path = NULL, *refs_str_disp = NULL,
1189 *refs_str = NULL, *in_repo_path = NULL, *commit_row = NULL,
1190 *commit_commit = NULL, *commit_commit_disp = NULL,
1191 *commit_age_diff = NULL, *commit_age_diff_disp = NULL,
1192 *commit_age_long = NULL, *commit_age_long_disp = NULL,
1193 *commit_author = NULL, *commit_author_disp = NULL,
1194 *commit_committer = NULL, *commit_committer_disp = NULL,
1195 *commit_log = NULL, *commit_log_disp = NULL,
1196 *commit_parent = NULL, *commit_diff_disp = NULL, *commit_log0,
1197 *newline, *logbriefs_navs_html = NULL,
1198 *log_tree_html = NULL, *log_commit_html = NULL;
1199 regex_t regex;
1200 int have_match;
1201 size_t newsize;
1202 struct buf *diffbuf = NULL;
1203 time_t committer_time;
1205 if (search_pattern &&
1206 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB |
1207 REG_NEWLINE))
1208 return NULL;
1210 SIMPLEQ_INIT(&refs);
1212 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1213 if (error != NULL)
1214 goto done;
1216 error = buf_alloc(&diffbuf, BUFFER_SIZE);
1217 if (error != NULL)
1218 goto done;
1220 if (start_commit == NULL) {
1221 struct got_reference *head_ref;
1222 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1223 if (error != NULL)
1224 goto done;
1225 error = got_ref_resolve(&id1, repo, head_ref);
1226 got_ref_close(head_ref);
1227 if (error != NULL)
1228 goto done;
1229 error = got_object_open_as_commit(&commit, repo, id1);
1230 } else {
1231 struct got_reference *ref;
1232 error = got_ref_open(&ref, repo, start_commit, 0);
1233 if (error == NULL) {
1234 int obj_type;
1235 error = got_ref_resolve(&id1, repo, ref);
1236 got_ref_close(ref);
1237 if (error != NULL)
1238 goto done;
1239 error = got_object_get_type(&obj_type, repo, id1);
1240 if (error != NULL)
1241 goto done;
1242 if (obj_type == GOT_OBJ_TYPE_TAG) {
1243 struct got_tag_object *tag;
1244 error = got_object_open_as_tag(&tag, repo, id1);
1245 if (error != NULL)
1246 goto done;
1247 if (got_object_tag_get_object_type(tag) !=
1248 GOT_OBJ_TYPE_COMMIT) {
1249 got_object_tag_close(tag);
1250 error = got_error(GOT_ERR_OBJ_TYPE);
1251 goto done;
1253 free(id1);
1254 id1 = got_object_id_dup(
1255 got_object_tag_get_object_id(tag));
1256 if (id1 == NULL)
1257 error = got_error_from_errno(
1258 "got_object_id_dup");
1259 got_object_tag_close(tag);
1260 if (error)
1261 goto done;
1262 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1263 error = got_error(GOT_ERR_OBJ_TYPE);
1264 goto done;
1266 error = got_object_open_as_commit(&commit, repo, id1);
1267 if (error != NULL)
1268 goto done;
1270 if (commit == NULL) {
1271 error = got_repo_match_object_id_prefix(&id1,
1272 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1273 if (error != NULL)
1274 goto done;
1276 error = got_repo_match_object_id_prefix(&id1,
1277 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1278 if (error != NULL)
1279 goto done;
1282 if (error != NULL)
1283 goto done;
1284 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1285 if (error != NULL)
1286 goto done;
1288 if (in_repo_path) {
1289 free(path);
1290 path = in_repo_path;
1293 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1294 if (error)
1295 goto done;
1297 error = got_commit_graph_open(&graph, id1, path, 0, repo);
1298 if (error)
1299 goto done;
1301 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1302 if (error)
1303 goto done;
1305 for (;;) {
1306 error = got_commit_graph_iter_next(&id1, graph);
1307 if (error) {
1308 if (error->code == GOT_ERR_ITER_COMPLETED) {
1309 error = NULL;
1310 break;
1312 if (error->code != GOT_ERR_ITER_NEED_MORE)
1313 break;
1314 error = got_commit_graph_fetch_commits(graph, 1, repo,
1315 NULL, NULL);
1316 if (error)
1317 break;
1318 else
1319 continue;
1321 if (id1 == NULL)
1322 break;
1324 error = got_object_open_as_commit(&commit, repo, id1);
1325 if (error)
1326 break;
1328 if (search_pattern) {
1329 error = match_logmsg(&have_match, id1, commit,
1330 &regex);
1331 if (error) {
1332 got_object_commit_close(commit);
1333 break;
1335 if (have_match == 0) {
1336 got_object_commit_close(commit);
1337 continue;
1341 SIMPLEQ_FOREACH(re, &refs, entry) {
1342 char *s;
1343 const char *name;
1344 struct got_tag_object *tag = NULL;
1345 int cmp;
1347 name = got_ref_get_name(re->ref);
1348 if (strcmp(name, GOT_REF_HEAD) == 0)
1349 continue;
1350 if (strncmp(name, "refs/", 5) == 0)
1351 name += 5;
1352 if (strncmp(name, "got/", 4) == 0)
1353 continue;
1354 if (strncmp(name, "heads/", 6) == 0)
1355 name += 6;
1356 if (strncmp(name, "remotes/", 8) == 0)
1357 name += 8;
1358 if (strncmp(name, "tags/", 5) == 0) {
1359 error = got_object_open_as_tag(&tag, repo,
1360 re->id);
1361 if (error) {
1362 if (error->code != GOT_ERR_OBJ_TYPE)
1363 continue;
1365 * Ref points at something other
1366 * than a tag.
1368 error = NULL;
1369 tag = NULL;
1372 cmp = got_object_id_cmp(tag ?
1373 got_object_tag_get_object_id(tag) : re->id, id1);
1374 if (tag)
1375 got_object_tag_close(tag);
1376 if (cmp != 0)
1377 continue;
1378 s = refs_str;
1379 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1380 s ? ", " : "", name)) == -1) {
1381 error = got_error_from_errno("asprintf");
1382 free(s);
1383 goto done;
1385 free(s);
1388 if (refs_str == NULL)
1389 refs_str_disp = strdup("");
1390 else {
1391 if ((asprintf(&refs_str_disp, "(%s)",
1392 refs_str)) == -1) {
1393 error = got_error_from_errno("asprintf");
1394 free(refs_str);
1395 goto done;
1399 /* commit id */
1400 error = got_object_id_str(&id_str1, id1);
1401 if (error)
1402 goto done;
1404 if (gw_trans->action == GW_COMMIT) {
1405 parent_id =
1406 SIMPLEQ_FIRST(
1407 got_object_commit_get_parent_ids(commit));
1408 if (parent_id != NULL) {
1409 id2 = got_object_id_dup(parent_id->id);
1410 free (parent_id);
1411 error = got_object_id_str(&id_str2, id2);
1412 if (error)
1413 goto done;
1414 } else
1415 id_str2 = strdup("/dev/null");
1418 committer_time =
1419 got_object_commit_get_committer_time(commit);
1421 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1422 error = got_error_from_errno("asprintf");
1423 goto done;
1426 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1427 id_str1)) == -1) {
1428 error = got_error_from_errno("asprintf");
1429 goto done;
1432 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1433 error = got_error_from_errno("asprintf");
1434 goto done;
1437 if ((asprintf(&commit_commit_disp, commit_commit_html,
1438 commit_commit, refs_str_disp)) == -1) {
1439 error = got_error_from_errno("asprintf");
1440 goto done;
1443 if ((asprintf(&commit_age_long, "%s",
1444 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1445 error = got_error_from_errno("asprintf");
1446 goto done;
1449 if ((asprintf(&commit_age_long_disp, commit_age_html,
1450 commit_age_long)) == -1) {
1451 error = got_error_from_errno("asprintf");
1452 goto done;
1455 if ((asprintf(&commit_age_diff, "%s",
1456 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1457 error = got_error_from_errno("asprintf");
1458 goto done;
1461 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1462 commit_age_diff)) == -1) {
1463 error = got_error_from_errno("asprintf");
1464 goto done;
1467 if ((asprintf(&commit_author, "%s",
1468 got_object_commit_get_author(commit))) == -1) {
1469 error = got_error_from_errno("asprintf");
1470 goto done;
1473 if ((asprintf(&commit_author_disp, commit_author_html,
1474 gw_html_escape(commit_author))) == -1) {
1475 error = got_error_from_errno("asprintf");
1476 goto done;
1479 if ((asprintf(&commit_committer, "%s",
1480 got_object_commit_get_committer(commit))) == -1) {
1481 error = got_error_from_errno("asprintf");
1482 goto done;
1485 if ((asprintf(&commit_committer_disp, commit_committer_html,
1486 gw_html_escape(commit_committer))) == -1) {
1487 error = got_error_from_errno("asprintf");
1488 goto done;
1491 if (strcmp(commit_author, commit_committer) == 0) {
1492 free(commit_committer_disp);
1493 commit_committer_disp = strdup("");
1496 error = got_object_commit_get_logmsg(&commit_log0, commit);
1497 if (error)
1498 goto done;
1500 switch(log_type) {
1501 case (LOGBRIEF):
1502 commit_log = commit_log0;
1503 while (*commit_log == '\n')
1504 commit_log++;
1505 newline = strchr(commit_log, '\n');
1506 if (newline)
1507 *newline = '\0';
1509 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1510 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1511 id_str1, gw_trans->repo_name, id_str1,
1512 gw_trans->repo_name, id_str1)) == -1) {
1513 error = got_error_from_errno("asprintf");
1514 goto done;
1517 if ((asprintf(&commit_row, logbriefs_row,
1518 commit_age_diff, commit_author, commit_log,
1519 logbriefs_navs_html)) == -1) {
1520 error = got_error_from_errno("asprintf");
1521 goto done;
1524 free(logbriefs_navs_html);
1525 logbriefs_navs_html = NULL;
1526 break;
1527 case (LOGFULL):
1528 commit_log = commit_log0;
1529 while (*commit_log == '\n')
1530 commit_log++;
1531 newline = strchr(commit_log, '\n');
1532 if (newline)
1533 *newline = '\n';
1535 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1536 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1537 id_str1, gw_trans->repo_name, id_str1,
1538 gw_trans->repo_name, id_str1)) == -1) {
1539 error = got_error_from_errno("asprintf");
1540 goto done;
1543 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1544 commit_author_disp, commit_committer_disp,
1545 commit_age_long_disp, gw_html_escape(commit_log),
1546 logbriefs_navs_html)) == -1) {
1547 error = got_error_from_errno("asprintf");
1548 goto done;
1551 free(logbriefs_navs_html);
1552 logbriefs_navs_html = NULL;
1553 break;
1554 case (LOGTREE):
1555 commit_log = gw_html_escape(commit_log0);
1557 log_tree_html = strdup("log tree here");
1559 if ((asprintf(&commit_row, log_tree_row, commit_log,
1560 log_tree_html)) == -1) {
1561 error = got_error_from_errno("asprintf");
1562 goto done;
1565 free(log_tree_html);
1566 break;
1567 case (LOGCOMMIT):
1568 commit_log = commit_log0;
1569 while (*commit_log == '\n')
1570 commit_log++;
1571 newline = strchr(commit_log, '\n');
1572 if (newline)
1573 *newline = '\n';
1575 if ((asprintf(&commit_log_disp, commit_log_html,
1576 gw_html_escape(commit_log))) == -1) {
1577 error = got_error_from_errno("asprintf");
1578 goto done;
1581 log_commit_html = strdup("commit here");
1583 if ((asprintf(&commit_row, log_commit_row,
1584 commit_diff_disp, commit_commit_disp,
1585 commit_author_disp, commit_committer_disp,
1586 commit_age_long_disp, commit_log_disp,
1587 log_commit_html)) == -1) {
1588 error = got_error_from_errno("asprintf");
1589 goto done;
1591 free(commit_log_disp);
1592 free(log_commit_html);
1594 break;
1595 default:
1596 return NULL;
1599 error = buf_append(&newsize, diffbuf, commit_row,
1600 strlen(commit_row));
1602 free(commit_parent);
1603 free(commit_diff_disp);
1604 free(commit_age_diff);
1605 free(commit_age_diff_disp);
1606 free(commit_age_long);
1607 free(commit_age_long_disp);
1608 free(commit_author);
1609 free(commit_author_disp);
1610 free(commit_committer);
1611 free(commit_committer_disp);
1612 free(commit_log0);
1613 free(commit_row);
1614 free(refs_str_disp);
1615 free(refs_str);
1616 refs_str = NULL;
1617 free(id_str1);
1618 id_str1 = NULL;
1619 free(id_str2);
1620 id_str2 = NULL;
1622 if (error || (limit && --limit == 0))
1623 break;
1626 if (error)
1627 goto done;
1629 logs = strdup(diffbuf->cb_buf);
1630 done:
1631 buf_free(diffbuf);
1632 if (commit != NULL)
1633 got_object_commit_close(commit);
1634 if (search_pattern)
1635 regfree(&regex);
1636 if (graph)
1637 got_commit_graph_close(graph);
1638 if (repo) {
1639 error = got_repo_close(repo);
1640 if (error != NULL)
1641 return NULL;
1643 if (error) {
1644 khttp_puts(gw_trans->gw_req, "Error: ");
1645 khttp_puts(gw_trans->gw_req, error->msg);
1646 return NULL;
1647 } else
1648 return logs;
1651 static char *
1652 gw_get_repo_tags(struct trans *gw_trans)
1654 char *tags = NULL;
1656 asprintf(&tags, tags_row, "30 min ago", "1.0.0", "tag 1.0.0",
1657 tags_navs);
1658 return tags;
1661 static char *
1662 gw_get_repo_heads(struct trans *gw_trans)
1664 char *heads = NULL;
1666 asprintf(&heads, heads_row, "30 min ago", "master", heads_navs);
1667 return heads;
1670 static char *
1671 gw_get_got_link(struct trans *gw_trans)
1673 char *link;
1675 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
1676 gw_trans->gw_conf->got_logo)) == -1)
1677 return NULL;
1679 return link;
1682 static char *
1683 gw_get_site_link(struct trans *gw_trans)
1685 char *link, *repo = "", *action = "";
1687 if (gw_trans->repo_name != NULL)
1688 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
1689 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
1690 return NULL;
1692 if (gw_trans->action_name != NULL)
1693 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
1694 return NULL;
1696 if ((asprintf(&link, site_link, GOTWEB,
1697 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
1698 return NULL;
1700 return link;
1703 static char *
1704 gw_html_escape(const char *html)
1706 char *escaped_str = NULL, *buf;
1707 char c[1];
1708 size_t sz, i;
1710 if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1711 return NULL;
1713 if (html == NULL)
1714 return NULL;
1715 else
1716 if ((sz = strlen(html)) == 0)
1717 return NULL;
1719 /* only work with BUFFER_SIZE */
1720 if (BUFFER_SIZE < sz)
1721 sz = BUFFER_SIZE;
1723 for (i = 0; i < sz; i++) {
1724 c[0] = html[i];
1725 switch (c[0]) {
1726 case ('>'):
1727 strcat(buf, "&gt;");
1728 break;
1729 case ('&'):
1730 strcat(buf, "&amp;");
1731 break;
1732 case ('<'):
1733 strcat(buf, "&lt;");
1734 break;
1735 case ('"'):
1736 strcat(buf, "&quot;");
1737 break;
1738 case ('\''):
1739 strcat(buf, "&apos;");
1740 break;
1741 case ('\n'):
1742 strcat(buf, "<br />");
1743 case ('|'):
1744 strcat(buf, " ");
1745 default:
1746 strcat(buf, &c[0]);
1747 break;
1750 asprintf(&escaped_str, "%s", buf);
1751 free(buf);
1752 return escaped_str;
1755 int
1756 main()
1758 const struct got_error *error = NULL;
1759 struct trans *gw_trans;
1760 struct gw_dir *dir = NULL, *tdir;
1761 const char *page = "index";
1762 int gw_malloc = 1;
1764 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
1765 errx(1, "malloc");
1767 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
1768 errx(1, "malloc");
1770 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
1771 errx(1, "malloc");
1773 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
1774 errx(1, "malloc");
1776 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
1777 &page, 1, 0))
1778 errx(1, "khttp_parse");
1780 if ((gw_trans->gw_conf =
1781 malloc(sizeof(struct gotweb_conf))) == NULL) {
1782 gw_malloc = 0;
1783 error = got_error_from_errno("malloc");
1784 goto err;
1787 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
1788 error = got_error_from_errno("pledge");
1789 goto err;
1792 TAILQ_INIT(&gw_trans->gw_dirs);
1794 gw_trans->page = 0;
1795 gw_trans->repos_total = 0;
1796 gw_trans->repo_path = NULL;
1797 gw_trans->commit = NULL;
1798 gw_trans->headref = strdup(GOT_REF_HEAD);
1799 gw_trans->mime = KMIME_TEXT_HTML;
1800 gw_trans->gw_tmpl->key = templs;
1801 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
1802 gw_trans->gw_tmpl->arg = gw_trans;
1803 gw_trans->gw_tmpl->cb = gw_template;
1804 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
1806 err:
1807 if (error) {
1808 gw_trans->mime = KMIME_TEXT_PLAIN;
1809 gw_trans->action = GW_ERR;
1810 gw_display_index(gw_trans, error);
1811 goto done;
1814 error = gw_parse_querystring(gw_trans);
1815 if (error)
1816 goto err;
1818 gw_display_index(gw_trans, error);
1820 done:
1821 if (gw_malloc) {
1822 free(gw_trans->gw_conf->got_repos_path);
1823 free(gw_trans->gw_conf->got_www_path);
1824 free(gw_trans->gw_conf->got_site_name);
1825 free(gw_trans->gw_conf->got_site_owner);
1826 free(gw_trans->gw_conf->got_site_link);
1827 free(gw_trans->gw_conf->got_logo);
1828 free(gw_trans->gw_conf->got_logo_url);
1829 free(gw_trans->gw_conf);
1830 free(gw_trans->commit);
1831 free(gw_trans->repo_path);
1832 free(gw_trans->repo_name);
1833 free(gw_trans->repo_file);
1834 free(gw_trans->action_name);
1835 free(gw_trans->headref);
1837 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
1838 free(dir->name);
1839 free(dir->description);
1840 free(dir->age);
1841 free(dir->url);
1842 free(dir->path);
1843 free(dir);
1848 khttp_free(gw_trans->gw_req);
1849 return EXIT_SUCCESS;