Blob


1 /*
2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2014, 2015, 2017 Kristaps Dzonsons <kristaps@bsd.lv>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/queue.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
23 #include <dirent.h>
24 #include <err.h>
25 #include <regex.h>
26 #include <stdarg.h>
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
37 #include <got_path.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
40 #include <got_diff.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
46 #include <kcgi.h>
47 #include <kcgihtml.h>
49 #include "buf.h"
50 #include "gotweb.h"
51 #include "gotweb_ui.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 struct trans {
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
63 struct kreq *gw_req;
64 char *repo_name;
65 char *repo_path;
66 char *commit;
67 char *repo_file;
68 char *action_name;
69 char *headref;
70 unsigned int action;
71 unsigned int page;
72 unsigned int repos_total;
73 enum kmime mime;
74 };
76 enum gw_key {
77 KEY_PATH,
78 KEY_ACTION,
79 KEY_COMMIT_ID,
80 KEY_FILE,
81 KEY_PAGE,
82 KEY_HEADREF,
83 KEY__MAX
84 };
86 struct gw_dir {
87 TAILQ_ENTRY(gw_dir) entry;
88 char *name;
89 char *owner;
90 char *description;
91 char *url;
92 char *age;
93 char *path;
94 };
96 enum tmpl {
97 TEMPL_HEAD,
98 TEMPL_HEADER,
99 TEMPL_SITEPATH,
100 TEMPL_SITEOWNER,
101 TEMPL_TITLE,
102 TEMPL_SEARCH,
103 TEMPL_CONTENT,
104 TEMPL__MAX
105 };
107 enum ref_tm {
108 TM_DIFF,
109 TM_LONG,
110 };
112 enum logs {
113 LOGBRIEF,
114 LOGCOMMIT,
115 LOGFULL,
116 LOGTREE,
117 LOGDIFF,
118 LOGBLAME,
119 LOGTAG,
120 };
122 enum tags {
123 TAGBRIEF,
124 TAGFULL,
125 };
127 struct buf {
128 u_char *cb_buf;
129 size_t cb_size;
130 size_t cb_len;
131 };
133 static const char *const templs[TEMPL__MAX] = {
134 "head",
135 "header",
136 "sitepath",
137 "siteowner",
138 "title",
139 "search",
140 "content",
141 };
143 static const struct kvalid gw_keys[KEY__MAX] = {
144 { kvalid_stringne, "path" },
145 { kvalid_stringne, "action" },
146 { kvalid_stringne, "commit" },
147 { kvalid_stringne, "file" },
148 { kvalid_int, "page" },
149 { kvalid_stringne, "headref" },
150 };
152 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_tag(struct trans *);
201 static const struct got_error* gw_tree(struct trans *);
203 struct gw_query_action {
204 unsigned int func_id;
205 const char *func_name;
206 const struct got_error *(*func_main)(struct trans *);
207 char *template;
208 };
210 enum gw_query_actions {
211 GW_BLAME,
212 GW_BLOB,
213 GW_BLOBDIFF,
214 GW_COMMIT,
215 GW_COMMITDIFF,
216 GW_ERR,
217 GW_HISTORY,
218 GW_INDEX,
219 GW_LOG,
220 GW_RAW,
221 GW_LOGBRIEFS,
222 GW_SNAPSHOT,
223 GW_SUMMARY,
224 GW_TAG,
225 GW_TREE,
226 };
228 static struct gw_query_action gw_query_funcs[] = {
229 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
230 { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
231 { GW_BLOBDIFF, "blobdiff", gw_blobdiff, "gw_tmpl/index.tmpl" },
232 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
233 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
234 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
235 { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
236 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
237 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
238 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
239 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
240 { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
241 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
242 { GW_TAG, "tag", gw_tag, "gw_tmpl/index.tmpl" },
243 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
244 };
246 static const struct got_error *
247 apply_unveil(const char *repo_path, const char *repo_file)
249 const struct got_error *err;
251 if (repo_path && repo_file) {
252 char *full_path;
253 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
254 return got_error_from_errno("asprintf unveil");
255 if (unveil(full_path, "r") != 0)
256 return got_error_from_errno2("unveil", full_path);
259 if (repo_path && unveil(repo_path, "r") != 0)
260 return got_error_from_errno2("unveil", repo_path);
262 if (unveil("/tmp", "rwc") != 0)
263 return got_error_from_errno2("unveil", "/tmp");
265 err = got_privsep_unveil_exec_helpers();
266 if (err != NULL)
267 return err;
269 if (unveil(NULL, NULL) != 0)
270 return got_error_from_errno("unveil");
272 return NULL;
275 static const struct got_error *
276 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
277 struct got_reference *ref2)
279 const struct got_error *err = NULL;
280 struct got_repository *repo = arg;
281 struct got_object_id *id1, *id2 = NULL;
282 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
283 time_t time1, time2;
285 *cmp = 0;
287 err = got_ref_resolve(&id1, repo, ref1);
288 if (err)
289 return err;
290 err = got_object_open_as_tag(&tag1, repo, id1);
291 if (err)
292 goto done;
294 err = got_ref_resolve(&id2, repo, ref2);
295 if (err)
296 goto done;
297 err = got_object_open_as_tag(&tag2, repo, id2);
298 if (err)
299 goto done;
301 time1 = got_object_tag_get_tagger_time(tag1);
302 time2 = got_object_tag_get_tagger_time(tag2);
304 /* Put latest tags first. */
305 if (time1 < time2)
306 *cmp = 1;
307 else if (time1 > time2)
308 *cmp = -1;
309 else
310 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
311 done:
312 free(id1);
313 free(id2);
314 if (tag1)
315 got_object_tag_close(tag1);
316 if (tag2)
317 got_object_tag_close(tag2);
318 return err;
321 static const struct got_error *
322 gw_blame(struct trans *gw_trans)
324 const struct got_error *error = NULL;
326 return error;
329 static const struct got_error *
330 gw_blob(struct trans *gw_trans)
332 const struct got_error *error = NULL;
334 return error;
337 static const struct got_error *
338 gw_blobdiff(struct trans *gw_trans)
340 const struct got_error *error = NULL;
342 return error;
345 static const struct got_error *
346 gw_commit(struct trans *gw_trans)
348 const struct got_error *error = NULL;
349 char *log, *log_html;
351 error = apply_unveil(gw_trans->gw_dir->path, NULL);
352 if (error)
353 return error;
355 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
357 if (log != NULL && strcmp(log, "") != 0) {
358 if ((asprintf(&log_html, log_commit, log)) == -1)
359 return got_error_from_errno("asprintf");
360 khttp_puts(gw_trans->gw_req, log_html);
361 free(log_html);
362 free(log);
364 return error;
367 static const struct got_error *
368 gw_commitdiff(struct trans *gw_trans)
370 const struct got_error *error = NULL;
371 char *log, *log_html;
373 error = apply_unveil(gw_trans->gw_dir->path, NULL);
374 if (error)
375 return error;
377 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
379 if (log != NULL && strcmp(log, "") != 0) {
380 if ((asprintf(&log_html, log_diff, log)) == -1)
381 return got_error_from_errno("asprintf");
382 khttp_puts(gw_trans->gw_req, log_html);
383 free(log_html);
384 free(log);
386 return error;
389 static const struct got_error *
390 gw_history(struct trans *gw_trans)
392 const struct got_error *error = NULL;
394 return error;
397 static const struct got_error *
398 gw_index(struct trans *gw_trans)
400 const struct got_error *error = NULL;
401 struct gw_dir *gw_dir = NULL;
402 char *html, *navs, *next, *prev;
403 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
405 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
406 if (error)
407 return error;
409 error = gw_load_got_paths(gw_trans);
410 if (error)
411 return error;
413 khttp_puts(gw_trans->gw_req, index_projects_header);
415 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
416 dir_c++;
418 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
419 if (gw_trans->page > 0 && (gw_trans->page *
420 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
421 prev_disp++;
422 continue;
425 prev_disp++;
426 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
427 gw_dir->name, gw_dir->name)) == -1)
428 return got_error_from_errno("asprintf");
430 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
431 gw_dir->description, gw_dir->owner, gw_dir->age,
432 navs)) == -1)
433 return got_error_from_errno("asprintf");
435 khttp_puts(gw_trans->gw_req, html);
437 free(navs);
438 free(html);
440 if (gw_trans->gw_conf->got_max_repos_display == 0)
441 continue;
443 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
444 khttp_puts(gw_trans->gw_req, np_wrapper_start);
445 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
446 (gw_trans->page > 0) &&
447 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
448 prev_disp == gw_trans->repos_total))
449 khttp_puts(gw_trans->gw_req, np_wrapper_start);
451 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
452 (gw_trans->page > 0) &&
453 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
454 prev_disp == gw_trans->repos_total)) {
455 if ((asprintf(&prev, nav_prev,
456 gw_trans->page - 1)) == -1)
457 return got_error_from_errno("asprintf");
458 khttp_puts(gw_trans->gw_req, prev);
459 free(prev);
462 khttp_puts(gw_trans->gw_req, div_end);
464 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
465 next_disp == gw_trans->gw_conf->got_max_repos_display &&
466 dir_c != (gw_trans->page + 1) *
467 gw_trans->gw_conf->got_max_repos_display) {
468 if ((asprintf(&next, nav_next,
469 gw_trans->page + 1)) == -1)
470 return got_error_from_errno("calloc");
471 khttp_puts(gw_trans->gw_req, next);
472 khttp_puts(gw_trans->gw_req, div_end);
473 free(next);
474 next_disp = 0;
475 break;
478 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
479 (gw_trans->page > 0) &&
480 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
481 prev_disp == gw_trans->repos_total))
482 khttp_puts(gw_trans->gw_req, div_end);
484 next_disp++;
486 return error;
489 static const struct got_error *
490 gw_log(struct trans *gw_trans)
492 const struct got_error *error = NULL;
493 char *log, *log_html;
495 error = apply_unveil(gw_trans->gw_dir->path, NULL);
496 if (error)
497 return error;
499 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
500 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
502 if (log != NULL && strcmp(log, "") != 0) {
503 if ((asprintf(&log_html, logs, log)) == -1)
504 return got_error_from_errno("asprintf");
505 khttp_puts(gw_trans->gw_req, log_html);
506 free(log_html);
507 free(log);
509 return error;
512 static const struct got_error *
513 gw_raw(struct trans *gw_trans)
515 const struct got_error *error = NULL;
517 return error;
520 static const struct got_error *
521 gw_logbriefs(struct trans *gw_trans)
523 const struct got_error *error = NULL;
524 char *log, *log_html;
526 error = apply_unveil(gw_trans->gw_dir->path, NULL);
527 if (error)
528 return error;
530 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
531 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
533 if (log != NULL && strcmp(log, "") != 0) {
534 if ((asprintf(&log_html, summary_logbriefs,
535 log)) == -1)
536 return got_error_from_errno("asprintf");
537 khttp_puts(gw_trans->gw_req, log_html);
538 free(log_html);
539 free(log);
541 return error;
544 static const struct got_error *
545 gw_snapshot(struct trans *gw_trans)
547 const struct got_error *error = NULL;
549 return error;
552 static const struct got_error *
553 gw_summary(struct trans *gw_trans)
555 const struct got_error *error = NULL;
556 char *description_html, *repo_owner_html, *repo_age_html,
557 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
558 *heads_html, *age;
560 error = apply_unveil(gw_trans->gw_dir->path, NULL);
561 if (error)
562 return error;
564 khttp_puts(gw_trans->gw_req, summary_wrapper);
565 if (gw_trans->gw_conf->got_show_repo_description) {
566 if (gw_trans->gw_dir->description != NULL &&
567 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
568 if ((asprintf(&description_html, description,
569 gw_trans->gw_dir->description)) == -1)
570 return got_error_from_errno("asprintf");
572 khttp_puts(gw_trans->gw_req, description_html);
573 free(description_html);
577 if (gw_trans->gw_conf->got_show_repo_owner) {
578 if (gw_trans->gw_dir->owner != NULL &&
579 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
580 if ((asprintf(&repo_owner_html, repo_owner,
581 gw_trans->gw_dir->owner)) == -1)
582 return got_error_from_errno("asprintf");
584 khttp_puts(gw_trans->gw_req, repo_owner_html);
585 free(repo_owner_html);
589 if (gw_trans->gw_conf->got_show_repo_age) {
590 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
591 "refs/heads", TM_LONG);
592 if (age != NULL && (strcmp(age, "") != 0)) {
593 if ((asprintf(&repo_age_html, last_change, age)) == -1)
594 return got_error_from_errno("asprintf");
596 khttp_puts(gw_trans->gw_req, repo_age_html);
597 free(repo_age_html);
598 free(age);
602 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
603 if (gw_trans->gw_dir->url != NULL &&
604 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
605 if ((asprintf(&cloneurl_html, cloneurl,
606 gw_trans->gw_dir->url)) == -1)
607 return got_error_from_errno("asprintf");
609 khttp_puts(gw_trans->gw_req, cloneurl_html);
610 free(cloneurl_html);
613 khttp_puts(gw_trans->gw_req, div_end);
615 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
616 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
617 heads = gw_get_repo_heads(gw_trans);
619 if (log != NULL && strcmp(log, "") != 0) {
620 if ((asprintf(&log_html, summary_logbriefs,
621 log)) == -1)
622 return got_error_from_errno("asprintf");
623 khttp_puts(gw_trans->gw_req, log_html);
624 free(log_html);
625 free(log);
628 if (tags != NULL && strcmp(tags, "") != 0) {
629 if ((asprintf(&tags_html, summary_tags,
630 tags)) == -1)
631 return got_error_from_errno("asprintf");
632 khttp_puts(gw_trans->gw_req, tags_html);
633 free(tags_html);
634 free(tags);
637 if (heads != NULL && strcmp(heads, "") != 0) {
638 if ((asprintf(&heads_html, summary_heads,
639 heads)) == -1)
640 return got_error_from_errno("asprintf");
641 khttp_puts(gw_trans->gw_req, heads_html);
642 free(heads_html);
643 free(heads);
645 return error;
648 static const struct got_error *
649 gw_tag(struct trans *gw_trans)
651 const struct got_error *error = NULL;
652 char *log, *log_html;
654 error = apply_unveil(gw_trans->gw_dir->path, NULL);
655 if (error)
656 return error;
658 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTAG);
660 if (log != NULL && strcmp(log, "") != 0) {
661 if ((asprintf(&log_html, log_tag, log)) == -1)
662 return got_error_from_errno("asprintf");
663 khttp_puts(gw_trans->gw_req, log_html);
664 free(log_html);
665 free(log);
667 return error;
670 static const struct got_error *
671 gw_tree(struct trans *gw_trans)
673 const struct got_error *error = NULL;
674 char *log, *log_html;
676 error = apply_unveil(gw_trans->gw_dir->path, NULL);
677 if (error)
678 return error;
680 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
682 if (log != NULL && strcmp(log, "") != 0) {
683 if ((asprintf(&log_html, log_tree, log)) == -1)
684 return got_error_from_errno("asprintf");
685 khttp_puts(gw_trans->gw_req, log_html);
686 free(log_html);
687 free(log);
689 return error;
692 static const struct got_error *
693 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
695 const struct got_error *error = NULL;
696 DIR *dt;
697 char *dir_test;
698 int opened = 0;
700 if ((asprintf(&dir_test, "%s/%s/%s",
701 gw_trans->gw_conf->got_repos_path, gw_dir->name,
702 GOTWEB_GIT_DIR)) == -1)
703 return got_error_from_errno("asprintf");
705 dt = opendir(dir_test);
706 if (dt == NULL) {
707 free(dir_test);
708 } else {
709 gw_dir->path = strdup(dir_test);
710 opened = 1;
711 goto done;
714 if ((asprintf(&dir_test, "%s/%s/%s",
715 gw_trans->gw_conf->got_repos_path, gw_dir->name,
716 GOTWEB_GOT_DIR)) == -1)
717 return got_error_from_errno("asprintf");
719 dt = opendir(dir_test);
720 if (dt == NULL)
721 free(dir_test);
722 else {
723 opened = 1;
724 error = got_error(GOT_ERR_NOT_GIT_REPO);
725 goto errored;
728 if ((asprintf(&dir_test, "%s/%s",
729 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
730 return got_error_from_errno("asprintf");
732 gw_dir->path = strdup(dir_test);
734 done:
735 gw_dir->description = gw_get_repo_description(gw_trans,
736 gw_dir->path);
737 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
738 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
739 TM_DIFF);
740 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
742 errored:
743 free(dir_test);
744 if (opened)
745 closedir(dt);
746 return error;
749 static const struct got_error *
750 gw_load_got_paths(struct trans *gw_trans)
752 const struct got_error *error = NULL;
753 DIR *d;
754 struct dirent **sd_dent;
755 struct gw_dir *gw_dir;
756 struct stat st;
757 unsigned int d_cnt, d_i;
759 d = opendir(gw_trans->gw_conf->got_repos_path);
760 if (d == NULL) {
761 error = got_error_from_errno2("opendir",
762 gw_trans->gw_conf->got_repos_path);
763 return error;
766 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
767 alphasort);
768 if (d_cnt == -1) {
769 error = got_error_from_errno2("scandir",
770 gw_trans->gw_conf->got_repos_path);
771 return error;
774 for (d_i = 0; d_i < d_cnt; d_i++) {
775 if (gw_trans->gw_conf->got_max_repos > 0 &&
776 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
777 break; /* account for parent and self */
779 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
780 strcmp(sd_dent[d_i]->d_name, "..") == 0)
781 continue;
783 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
784 return got_error_from_errno("gw_dir malloc");
786 error = gw_load_got_path(gw_trans, gw_dir);
787 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
788 continue;
789 else if (error)
790 return error;
792 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
793 !got_path_dir_is_empty(gw_dir->path)) {
794 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
795 entry);
796 gw_trans->repos_total++;
800 closedir(d);
801 return error;
804 static const struct got_error *
805 gw_parse_querystring(struct trans *gw_trans)
807 const struct got_error *error = NULL;
808 struct kpair *p;
809 struct gw_query_action *action = NULL;
810 unsigned int i;
812 if (gw_trans->gw_req->fieldnmap[0]) {
813 error = got_error_from_errno("bad parse");
814 return error;
815 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
816 /* define gw_trans->repo_path */
817 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
818 return got_error_from_errno("asprintf");
820 if ((asprintf(&gw_trans->repo_path, "%s/%s",
821 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
822 return got_error_from_errno("asprintf");
824 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
825 if ((asprintf(&gw_trans->commit, "%s",
826 p->parsed.s)) == -1)
827 return got_error_from_errno("asprintf");
829 /* get action and set function */
830 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
831 for (i = 0; i < nitems(gw_query_funcs); i++) {
832 action = &gw_query_funcs[i];
833 if (action->func_name == NULL)
834 continue;
836 if (strcmp(action->func_name,
837 p->parsed.s) == 0) {
838 gw_trans->action = i;
839 if ((asprintf(&gw_trans->action_name,
840 "%s", action->func_name)) == -1)
841 return
842 got_error_from_errno(
843 "asprintf");
845 break;
848 action = NULL;
851 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
852 if ((asprintf(&gw_trans->repo_file, "%s",
853 p->parsed.s)) == -1)
854 return got_error_from_errno("asprintf");
856 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
857 if ((asprintf(&gw_trans->headref, "%s",
858 p->parsed.s)) == -1)
859 return got_error_from_errno("asprintf");
861 if (action == NULL) {
862 error = got_error_from_errno("invalid action");
863 return error;
865 if ((gw_trans->gw_dir =
866 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
867 return got_error_from_errno("gw_dir malloc");
869 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
870 if (error)
871 return error;
872 } else
873 gw_trans->action = GW_INDEX;
875 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
876 gw_trans->page = p->parsed.i;
878 if (gw_trans->action == GW_RAW)
879 gw_trans->mime = KMIME_TEXT_PLAIN;
881 return error;
884 static struct gw_dir *
885 gw_init_gw_dir(char *dir)
887 struct gw_dir *gw_dir;
889 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
890 return NULL;
892 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
893 return NULL;
895 return gw_dir;
898 static const struct got_error*
899 match_logmsg(int *have_match, struct got_object_id *id,
900 struct got_commit_object *commit, regex_t *regex)
902 const struct got_error *err = NULL;
903 regmatch_t regmatch;
904 char *id_str = NULL, *logmsg = NULL;
906 *have_match = 0;
908 err = got_object_id_str(&id_str, id);
909 if (err)
910 return err;
912 err = got_object_commit_get_logmsg(&logmsg, commit);
913 if (err)
914 goto done;
916 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
917 *have_match = 1;
918 done:
919 free(id_str);
920 free(logmsg);
921 return err;
924 static void
925 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
927 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
928 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
929 khttps[code]);
930 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
931 kmimetypes[mime]);
932 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
933 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
934 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
935 khttp_body(gw_trans->gw_req);
938 static void
939 gw_display_index(struct trans *gw_trans, const struct got_error *err)
941 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
942 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
944 if (err)
945 khttp_puts(gw_trans->gw_req, err->msg);
946 else
947 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
948 gw_query_funcs[gw_trans->action].template);
950 khtml_close(gw_trans->gw_html_req);
953 static int
954 gw_template(size_t key, void *arg)
956 const struct got_error *error = NULL;
957 struct trans *gw_trans = arg;
958 char *gw_got_link, *gw_site_link;
959 char *site_owner_name, *site_owner_name_h;
961 switch (key) {
962 case (TEMPL_HEAD):
963 khttp_puts(gw_trans->gw_req, head);
964 break;
965 case(TEMPL_HEADER):
966 gw_got_link = gw_get_got_link(gw_trans);
967 if (gw_got_link != NULL)
968 khttp_puts(gw_trans->gw_req, gw_got_link);
970 free(gw_got_link);
971 break;
972 case (TEMPL_SITEPATH):
973 gw_site_link = gw_get_site_link(gw_trans);
974 if (gw_site_link != NULL)
975 khttp_puts(gw_trans->gw_req, gw_site_link);
977 free(gw_site_link);
978 break;
979 case(TEMPL_TITLE):
980 if (gw_trans->gw_conf->got_site_name != NULL)
981 khtml_puts(gw_trans->gw_html_req,
982 gw_trans->gw_conf->got_site_name);
984 break;
985 case (TEMPL_SEARCH):
986 khttp_puts(gw_trans->gw_req, search);
987 break;
988 case(TEMPL_SITEOWNER):
989 if (gw_trans->gw_conf->got_site_owner != NULL &&
990 gw_trans->gw_conf->got_show_site_owner) {
991 site_owner_name =
992 gw_html_escape(gw_trans->gw_conf->got_site_owner);
993 if ((asprintf(&site_owner_name_h, site_owner,
994 site_owner_name))
995 == -1)
996 return 0;
998 khttp_puts(gw_trans->gw_req, site_owner_name_h);
999 free(site_owner_name);
1000 free(site_owner_name_h);
1002 break;
1003 case(TEMPL_CONTENT):
1004 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1005 if (error)
1006 khttp_puts(gw_trans->gw_req, error->msg);
1008 break;
1009 default:
1010 return 0;
1011 break;
1013 return 1;
1016 static char *
1017 gw_get_repo_description(struct trans *gw_trans, char *dir)
1019 FILE *f;
1020 char *description = NULL, *d_file = NULL;
1021 unsigned int len;
1023 if (gw_trans->gw_conf->got_show_repo_description == false)
1024 goto err;
1026 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1027 goto err;
1029 if ((f = fopen(d_file, "r")) == NULL)
1030 goto err;
1032 fseek(f, 0, SEEK_END);
1033 len = ftell(f) + 1;
1034 fseek(f, 0, SEEK_SET);
1035 if ((description = calloc(len, sizeof(char *))) == NULL)
1036 goto err;
1038 fread(description, 1, len, f);
1039 fclose(f);
1040 free(d_file);
1041 return description;
1042 err:
1043 if ((asprintf(&description, "%s", "")) == -1)
1044 return NULL;
1046 return description;
1049 static char *
1050 gw_get_time_str(time_t committer_time, int ref_tm)
1052 struct tm tm;
1053 time_t diff_time;
1054 char *years = "years ago", *months = "months ago";
1055 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1056 char *minutes = "minutes ago", *seconds = "seconds ago";
1057 char *now = "right now";
1058 char *repo_age, *s;
1059 char datebuf[29];
1061 switch (ref_tm) {
1062 case TM_DIFF:
1063 diff_time = time(NULL) - committer_time;
1064 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1065 if ((asprintf(&repo_age, "%lld %s",
1066 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1067 return NULL;
1068 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1069 if ((asprintf(&repo_age, "%lld %s",
1070 (diff_time / 60 / 60 / 24 / (365 / 12)),
1071 months)) == -1)
1072 return NULL;
1073 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1074 if ((asprintf(&repo_age, "%lld %s",
1075 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1076 return NULL;
1077 } else if (diff_time > 60 * 60 * 24 * 2) {
1078 if ((asprintf(&repo_age, "%lld %s",
1079 (diff_time / 60 / 60 / 24), days)) == -1)
1080 return NULL;
1081 } else if (diff_time > 60 * 60 * 2) {
1082 if ((asprintf(&repo_age, "%lld %s",
1083 (diff_time / 60 / 60), hours)) == -1)
1084 return NULL;
1085 } else if (diff_time > 60 * 2) {
1086 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1087 minutes)) == -1)
1088 return NULL;
1089 } else if (diff_time > 2) {
1090 if ((asprintf(&repo_age, "%lld %s", diff_time,
1091 seconds)) == -1)
1092 return NULL;
1093 } else {
1094 if ((asprintf(&repo_age, "%s", now)) == -1)
1095 return NULL;
1097 break;
1098 case TM_LONG:
1099 if (gmtime_r(&committer_time, &tm) == NULL)
1100 return NULL;
1102 s = asctime_r(&tm, datebuf);
1103 if (s == NULL)
1104 return NULL;
1106 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1107 return NULL;
1108 break;
1110 return repo_age;
1113 static char *
1114 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
1116 const struct got_error *error = NULL;
1117 struct got_object_id *id = NULL;
1118 struct got_repository *repo = NULL;
1119 struct got_commit_object *commit = NULL;
1120 struct got_reflist_head refs;
1121 struct got_reflist_entry *re;
1122 struct got_reference *head_ref;
1123 int is_head = 0;
1124 time_t committer_time = 0, cmp_time = 0;
1125 const char *refname;
1126 char *repo_age = NULL;
1128 if (repo_ref == NULL)
1129 return NULL;
1131 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1132 is_head = 1;
1134 SIMPLEQ_INIT(&refs);
1135 if (gw_trans->gw_conf->got_show_repo_age == false) {
1136 if ((asprintf(&repo_age, "")) == -1)
1137 return NULL;
1138 return repo_age;
1141 error = got_repo_open(&repo, dir, NULL);
1142 if (error != NULL)
1143 goto err;
1145 if (is_head)
1146 error = got_ref_list(&refs, repo, "refs/heads",
1147 got_ref_cmp_by_name, NULL);
1148 else
1149 error = got_ref_list(&refs, repo, repo_ref,
1150 got_ref_cmp_by_name, NULL);
1151 if (error != NULL)
1152 goto err;
1154 SIMPLEQ_FOREACH(re, &refs, entry) {
1155 if (is_head)
1156 refname = strdup(repo_ref);
1157 else
1158 refname = got_ref_get_name(re->ref);
1159 error = got_ref_open(&head_ref, repo, refname, 0);
1160 if (error != NULL)
1161 goto err;
1163 error = got_ref_resolve(&id, repo, head_ref);
1164 got_ref_close(head_ref);
1165 if (error != NULL)
1166 goto err;
1168 error = got_object_open_as_commit(&commit, repo, id);
1169 if (error != NULL)
1170 goto err;
1172 committer_time =
1173 got_object_commit_get_committer_time(commit);
1175 if (cmp_time < committer_time)
1176 cmp_time = committer_time;
1179 if (cmp_time != 0) {
1180 committer_time = cmp_time;
1181 repo_age = gw_get_time_str(committer_time, ref_tm);
1182 } else
1183 if ((asprintf(&repo_age, "")) == -1)
1184 return NULL;
1185 got_ref_list_free(&refs);
1186 free(id);
1187 return repo_age;
1188 err:
1189 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1190 return NULL;
1192 return repo_age;
1195 static char *
1196 gw_get_repo_owner(struct trans *gw_trans, char *dir)
1198 FILE *f;
1199 char *owner = NULL, *d_file = NULL;
1200 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1201 char *comp, *pos, *buf;
1202 unsigned int i;
1204 if (gw_trans->gw_conf->got_show_repo_owner == false)
1205 goto err;
1207 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1208 goto err;
1210 if ((f = fopen(d_file, "r")) == NULL)
1211 goto err;
1213 if ((buf = calloc(128, sizeof(char *))) == NULL)
1214 goto err;
1216 while ((fgets(buf, 128, f)) != NULL) {
1217 if ((pos = strstr(buf, gotweb)) != NULL)
1218 break;
1220 if ((pos = strstr(buf, gitweb)) != NULL)
1221 break;
1224 if (pos == NULL)
1225 goto err;
1227 do {
1228 fgets(buf, 128, f);
1229 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1231 if (comp == NULL)
1232 goto err;
1234 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1235 goto err;
1237 for (i = 0; i < 2; i++) {
1238 owner = strsep(&buf, "\"");
1241 if (owner == NULL)
1242 goto err;
1244 fclose(f);
1245 free(d_file);
1246 return owner;
1247 err:
1248 if ((asprintf(&owner, "%s", "")) == -1)
1249 return NULL;
1251 return owner;
1254 static char *
1255 gw_get_clone_url(struct trans *gw_trans, char *dir)
1257 FILE *f;
1258 char *url = NULL, *d_file = NULL;
1259 unsigned int len;
1261 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1262 return NULL;
1264 if ((f = fopen(d_file, "r")) == NULL)
1265 return NULL;
1267 fseek(f, 0, SEEK_END);
1268 len = ftell(f) + 1;
1269 fseek(f, 0, SEEK_SET);
1271 if ((url = calloc(len, sizeof(char *))) == NULL)
1272 return NULL;
1274 fread(url, 1, len, f);
1275 fclose(f);
1276 free(d_file);
1277 return url;
1280 static char *
1281 gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1282 char *start_commit, int limit, int log_type)
1284 const struct got_error *error;
1285 struct got_repository *repo = NULL;
1286 struct got_reflist_head refs;
1287 struct got_reflist_entry *re;
1288 struct got_commit_object *commit = NULL;
1289 struct got_object_id *id1 = NULL, *id2 = NULL;
1290 struct got_object_qid *parent_id;
1291 struct got_commit_graph *graph = NULL;
1292 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1293 *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1294 *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1295 *commit_commit_disp = NULL, *commit_age_diff = NULL,
1296 *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1297 *commit_age_long_disp = NULL, *commit_author = NULL,
1298 *commit_author_disp = NULL, *commit_committer = NULL,
1299 *commit_committer_disp = NULL, *commit_log = NULL,
1300 *commit_log_disp = NULL, *commit_parent = NULL,
1301 *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1302 *log_tree_html = NULL, *log_commit_html = NULL,
1303 *log_diff_html = NULL, *commit_tree = NULL,
1304 *commit_tree_disp = NULL, *log_tag_html = NULL;
1305 char *commit_log0, *newline;
1306 regex_t regex;
1307 int have_match;
1308 size_t newsize;
1309 struct buf *diffbuf = NULL;
1310 time_t committer_time;
1312 error = buf_alloc(&diffbuf, 0);
1313 if (error != NULL)
1314 return NULL;
1316 if (search_pattern &&
1317 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB |
1318 REG_NEWLINE))
1319 return NULL;
1321 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1322 if (error != NULL)
1323 return NULL;
1325 SIMPLEQ_INIT(&refs);
1327 if (start_commit == NULL) {
1328 struct got_reference *head_ref;
1329 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1330 if (error != NULL)
1331 goto done;
1333 error = got_ref_resolve(&id1, repo, head_ref);
1334 got_ref_close(head_ref);
1335 if (error != NULL)
1336 goto done;
1338 error = got_object_open_as_commit(&commit, repo, id1);
1339 } else {
1340 struct got_reference *ref;
1341 error = got_ref_open(&ref, repo, start_commit, 0);
1342 if (error == NULL) {
1343 int obj_type;
1344 error = got_ref_resolve(&id1, repo, ref);
1345 got_ref_close(ref);
1346 if (error != NULL)
1347 goto done;
1348 error = got_object_get_type(&obj_type, repo, id1);
1349 if (error != NULL)
1350 goto done;
1351 if (obj_type == GOT_OBJ_TYPE_TAG) {
1352 struct got_tag_object *tag;
1353 error = got_object_open_as_tag(&tag, repo, id1);
1354 if (error != NULL)
1355 goto done;
1356 if (got_object_tag_get_object_type(tag) !=
1357 GOT_OBJ_TYPE_COMMIT) {
1358 got_object_tag_close(tag);
1359 error = got_error(GOT_ERR_OBJ_TYPE);
1360 goto done;
1362 free(id1);
1363 id1 = got_object_id_dup(
1364 got_object_tag_get_object_id(tag));
1365 if (id1 == NULL)
1366 error = got_error_from_errno(
1367 "got_object_id_dup");
1368 got_object_tag_close(tag);
1369 if (error)
1370 goto done;
1371 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1372 error = got_error(GOT_ERR_OBJ_TYPE);
1373 goto done;
1375 error = got_object_open_as_commit(&commit, repo, id1);
1376 if (error != NULL)
1377 goto done;
1379 if (commit == NULL) {
1380 error = got_repo_match_object_id_prefix(&id1,
1381 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1382 if (error != NULL)
1383 goto done;
1385 error = got_repo_match_object_id_prefix(&id1,
1386 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1387 if (error != NULL)
1388 goto done;
1391 if (error != NULL)
1392 goto done;
1394 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1395 if (error != NULL)
1396 goto done;
1398 if (in_repo_path) {
1399 free(path);
1400 path = in_repo_path;
1403 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1404 if (error)
1405 goto done;
1407 error = got_commit_graph_open(&graph, id1, path, 0, repo);
1408 if (error)
1409 goto done;
1411 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1412 if (error)
1413 goto done;
1415 for (;;) {
1416 error = got_commit_graph_iter_next(&id1, graph);
1417 if (error) {
1418 if (error->code == GOT_ERR_ITER_COMPLETED) {
1419 error = NULL;
1420 break;
1422 if (error->code != GOT_ERR_ITER_NEED_MORE)
1423 break;
1424 error = got_commit_graph_fetch_commits(graph, 1, repo,
1425 NULL, NULL);
1426 if (error)
1427 break;
1428 else
1429 continue;
1431 if (id1 == NULL)
1432 break;
1434 error = got_object_open_as_commit(&commit, repo, id1);
1435 if (error)
1436 break;
1438 if (search_pattern) {
1439 error = match_logmsg(&have_match, id1, commit,
1440 &regex);
1441 if (error) {
1442 got_object_commit_close(commit);
1443 break;
1445 if (have_match == 0) {
1446 got_object_commit_close(commit);
1447 continue;
1451 SIMPLEQ_FOREACH(re, &refs, entry) {
1452 char *s;
1453 const char *name;
1454 struct got_tag_object *tag = NULL;
1455 int cmp;
1457 name = got_ref_get_name(re->ref);
1458 if (strcmp(name, GOT_REF_HEAD) == 0)
1459 continue;
1460 if (strncmp(name, "refs/", 5) == 0)
1461 name += 5;
1462 if (strncmp(name, "got/", 4) == 0)
1463 continue;
1464 if (strncmp(name, "heads/", 6) == 0)
1465 name += 6;
1466 if (strncmp(name, "remotes/", 8) == 0)
1467 name += 8;
1468 if (strncmp(name, "tags/", 5) == 0) {
1469 error = got_object_open_as_tag(&tag, repo,
1470 re->id);
1471 if (error) {
1472 if (error->code != GOT_ERR_OBJ_TYPE)
1473 continue;
1475 * Ref points at something other
1476 * than a tag.
1478 error = NULL;
1479 tag = NULL;
1482 cmp = got_object_id_cmp(tag ?
1483 got_object_tag_get_object_id(tag) : re->id, id1);
1484 if (tag)
1485 got_object_tag_close(tag);
1486 if (cmp != 0)
1487 continue;
1488 s = refs_str;
1489 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1490 s ? ", " : "", name)) == -1) {
1491 error = got_error_from_errno("asprintf");
1492 free(s);
1493 goto done;
1495 free(s);
1498 if (refs_str == NULL)
1499 refs_str_disp = strdup("");
1500 else {
1501 if ((asprintf(&refs_str_disp, "(%s)",
1502 refs_str)) == -1) {
1503 error = got_error_from_errno("asprintf");
1504 free(refs_str);
1505 goto done;
1509 error = got_object_id_str(&id_str1, id1);
1510 if (error)
1511 goto done;
1513 error = got_object_id_str(&treeid,
1514 got_object_commit_get_tree_id(commit));
1515 if (error)
1516 goto done;
1518 if (gw_trans->action == GW_COMMIT ||
1519 gw_trans->action == GW_COMMITDIFF) {
1520 parent_id =
1521 SIMPLEQ_FIRST(
1522 got_object_commit_get_parent_ids(commit));
1523 if (parent_id != NULL) {
1524 id2 = got_object_id_dup(parent_id->id);
1525 free (parent_id);
1526 error = got_object_id_str(&id_str2, id2);
1527 if (error)
1528 goto done;
1529 free(id2);
1530 } else
1531 id_str2 = strdup("/dev/null");
1534 committer_time =
1535 got_object_commit_get_committer_time(commit);
1537 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1538 error = got_error_from_errno("asprintf");
1539 goto done;
1542 if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1543 error = got_error_from_errno("asprintf");
1544 goto done;
1547 if ((asprintf(&commit_tree_disp, commit_tree_html,
1548 treeid)) == -1) {
1549 error = got_error_from_errno("asprintf");
1550 goto done;
1553 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1554 id_str1)) == -1) {
1555 error = got_error_from_errno("asprintf");
1556 goto done;
1559 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1560 error = got_error_from_errno("asprintf");
1561 goto done;
1564 if ((asprintf(&commit_commit_disp, commit_commit_html,
1565 commit_commit, refs_str_disp)) == -1) {
1566 error = got_error_from_errno("asprintf");
1567 goto done;
1570 if ((asprintf(&commit_age_long, "%s",
1571 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1572 error = got_error_from_errno("asprintf");
1573 goto done;
1576 if ((asprintf(&commit_age_long_disp, commit_age_html,
1577 commit_age_long)) == -1) {
1578 error = got_error_from_errno("asprintf");
1579 goto done;
1582 if ((asprintf(&commit_age_diff, "%s",
1583 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1584 error = got_error_from_errno("asprintf");
1585 goto done;
1588 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1589 commit_age_diff)) == -1) {
1590 error = got_error_from_errno("asprintf");
1591 goto done;
1594 if ((asprintf(&commit_author, "%s",
1595 got_object_commit_get_author(commit))) == -1) {
1596 error = got_error_from_errno("asprintf");
1597 goto done;
1600 if ((asprintf(&commit_author_disp, commit_author_html,
1601 gw_html_escape(commit_author))) == -1) {
1602 error = got_error_from_errno("asprintf");
1603 goto done;
1606 if ((asprintf(&commit_committer, "%s",
1607 got_object_commit_get_committer(commit))) == -1) {
1608 error = got_error_from_errno("asprintf");
1609 goto done;
1612 if ((asprintf(&commit_committer_disp, commit_committer_html,
1613 gw_html_escape(commit_committer))) == -1) {
1614 error = got_error_from_errno("asprintf");
1615 goto done;
1618 if (strcmp(commit_author, commit_committer) == 0) {
1619 free(commit_committer_disp);
1620 commit_committer_disp = strdup("");
1623 error = got_object_commit_get_logmsg(&commit_log0, commit);
1624 if (error)
1625 goto done;
1627 commit_log = commit_log0;
1628 while (*commit_log == '\n')
1629 commit_log++;
1631 switch(log_type) {
1632 case (LOGBRIEF):
1633 newline = strchr(commit_log, '\n');
1634 if (newline)
1635 *newline = '\0';
1637 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1638 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1639 id_str1, gw_trans->repo_name, id_str1,
1640 gw_trans->repo_name, id_str1)) == -1) {
1641 error = got_error_from_errno("asprintf");
1642 goto done;
1645 if ((asprintf(&commit_row, logbriefs_row,
1646 commit_age_diff, commit_author, commit_log,
1647 logbriefs_navs_html)) == -1) {
1648 error = got_error_from_errno("asprintf");
1649 goto done;
1652 free(logbriefs_navs_html);
1653 logbriefs_navs_html = NULL;
1654 break;
1655 case (LOGFULL):
1656 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1657 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1658 id_str1, gw_trans->repo_name, id_str1,
1659 gw_trans->repo_name, id_str1)) == -1) {
1660 error = got_error_from_errno("asprintf");
1661 goto done;
1664 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1665 commit_author_disp, commit_committer_disp,
1666 commit_age_long_disp, gw_html_escape(commit_log),
1667 logbriefs_navs_html)) == -1) {
1668 error = got_error_from_errno("asprintf");
1669 goto done;
1672 free(logbriefs_navs_html);
1673 logbriefs_navs_html = NULL;
1674 break;
1675 case (LOGTAG):
1676 log_tag_html = strdup("tag log here");
1678 if ((asprintf(&commit_row, log_tag_row,
1679 gw_html_escape(commit_log), log_tag_html)) == -1) {
1680 error = got_error_from_errno("asprintf");
1681 goto done;
1684 free(log_tag_html);
1685 break;
1686 case (LOGTREE):
1687 log_tree_html = strdup("log tree here");
1689 if ((asprintf(&commit_row, log_tree_row,
1690 gw_html_escape(commit_log), log_tree_html)) == -1) {
1691 error = got_error_from_errno("asprintf");
1692 goto done;
1695 free(log_tree_html);
1696 break;
1697 case (LOGCOMMIT):
1698 if ((asprintf(&commit_log_disp, commit_log_html,
1699 gw_html_escape(commit_log))) == -1) {
1700 error = got_error_from_errno("asprintf");
1701 goto done;
1704 log_commit_html = strdup("commit here");
1706 if ((asprintf(&commit_row, log_commit_row,
1707 commit_diff_disp, commit_commit_disp,
1708 commit_tree_disp, commit_author_disp,
1709 commit_committer_disp, commit_age_long_disp,
1710 commit_log_disp, log_commit_html)) == -1) {
1711 error = got_error_from_errno("asprintf");
1712 goto done;
1714 free(commit_log_disp);
1715 free(log_commit_html);
1717 break;
1718 case (LOGDIFF):
1719 if ((asprintf(&commit_log_disp, commit_log_html,
1720 gw_html_escape(commit_log))) == -1) {
1721 error = got_error_from_errno("asprintf");
1722 goto done;
1725 log_diff_html = strdup("diff here");
1727 if ((asprintf(&commit_row, log_diff_row,
1728 commit_diff_disp, commit_commit_disp,
1729 commit_tree_disp, commit_author_disp,
1730 commit_committer_disp, commit_age_long_disp,
1731 commit_log_disp, log_diff_html)) == -1) {
1732 error = got_error_from_errno("asprintf");
1733 goto done;
1735 free(commit_log_disp);
1736 free(log_diff_html);
1738 break;
1739 default:
1740 return NULL;
1743 error = buf_puts(&newsize, diffbuf, commit_row);
1745 free(commit_parent);
1746 free(commit_diff_disp);
1747 free(commit_tree_disp);
1748 free(commit_age_diff);
1749 free(commit_age_diff_disp);
1750 free(commit_age_long);
1751 free(commit_age_long_disp);
1752 free(commit_author);
1753 free(commit_author_disp);
1754 free(commit_committer);
1755 free(commit_committer_disp);
1756 free(commit_log0);
1757 free(commit_row);
1758 free(refs_str_disp);
1759 free(refs_str);
1760 refs_str = NULL;
1761 free(id_str1);
1762 id_str1 = NULL;
1763 free(id_str2);
1764 id_str2 = NULL;
1766 if (error || (limit && --limit == 0))
1767 break;
1770 if (error)
1771 goto done;
1773 if (buf_len(diffbuf) > 0) {
1774 error = buf_putc(diffbuf, '\0');
1775 logs = strdup(buf_get(diffbuf));
1777 done:
1778 buf_free(diffbuf);
1779 if (commit != NULL)
1780 got_object_commit_close(commit);
1781 if (search_pattern)
1782 regfree(&regex);
1783 if (graph)
1784 got_commit_graph_close(graph);
1785 if (repo) {
1786 error = got_repo_close(repo);
1787 if (error != NULL)
1788 return NULL;
1790 if (error) {
1791 khttp_puts(gw_trans->gw_req, "Error: ");
1792 khttp_puts(gw_trans->gw_req, error->msg);
1793 return NULL;
1794 } else
1795 return logs;
1798 static char *
1799 gw_get_repo_tags(struct trans *gw_trans, int limit, int tag_type)
1801 const struct got_error *error = NULL;
1802 struct got_repository *repo = NULL;
1803 struct got_reflist_head refs;
1804 struct got_reflist_entry *re;
1805 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1806 *age = NULL;
1807 char *newline;
1808 struct buf *diffbuf = NULL;
1809 size_t newsize;
1811 error = buf_alloc(&diffbuf, 0);
1812 if (error != NULL)
1813 return NULL;
1814 SIMPLEQ_INIT(&refs);
1816 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1817 if (error != NULL)
1818 goto done;
1820 error = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
1821 if (error)
1822 goto done;
1824 SIMPLEQ_FOREACH(re, &refs, entry) {
1825 const char *refname;
1826 char *refstr, *tag_log0, *tag_log, *id_str;
1827 time_t tagger_time;
1828 struct got_object_id *id;
1829 struct got_tag_object *tag;
1831 refname = got_ref_get_name(re->ref);
1832 if (strncmp(refname, "refs/tags/", 10) != 0)
1833 continue;
1834 refname += 10;
1835 refstr = got_ref_to_str(re->ref);
1836 if (refstr == NULL) {
1837 error = got_error_from_errno("got_ref_to_str");
1838 goto done;
1841 error = got_ref_resolve(&id, repo, re->ref);
1842 if (error)
1843 goto done;
1844 error = got_object_open_as_tag(&tag, repo, id);
1845 free(id);
1846 if (error)
1847 goto done;
1849 tagger_time = got_object_tag_get_tagger_time(tag);
1851 error = got_object_id_str(&id_str,
1852 got_object_tag_get_object_id(tag));
1853 if (error)
1854 goto done;
1856 tag_log0 = strdup(got_object_tag_get_message(tag));
1858 if (tag_log0 == NULL) {
1859 error = got_error_from_errno("strdup");
1860 goto done;
1863 tag_log = tag_log0;
1864 while (*tag_log == '\n')
1865 tag_log++;
1867 switch (tag_type) {
1868 case TAGBRIEF:
1869 newline = strchr(tag_log, '\n');
1870 if (newline)
1871 *newline = '\0';
1873 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1874 TM_DIFF))) == -1) {
1875 error = got_error_from_errno("asprintf");
1876 goto done;
1879 if ((asprintf(&tags_navs_disp, tags_navs,
1880 gw_trans->repo_name, id_str, gw_trans->repo_name,
1881 id_str, gw_trans->repo_name, id_str,
1882 gw_trans->repo_name, id_str)) == -1) {
1883 error = got_error_from_errno("asprintf");
1884 goto done;
1887 if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
1888 tags_navs_disp)) == -1) {
1889 error = got_error_from_errno("asprintf");
1890 goto done;
1893 free(tags_navs_disp);
1894 break;
1895 case TAGFULL:
1896 break;
1897 default:
1898 break;
1901 got_object_tag_close(tag);
1903 error = buf_puts(&newsize, diffbuf, tag_row);
1905 free(id_str);
1906 free(refstr);
1907 free(age);
1908 free(tag_log0);
1909 free(tag_row);
1911 if (error || (limit && --limit == 0))
1912 break;
1915 if (buf_len(diffbuf) > 0) {
1916 error = buf_putc(diffbuf, '\0');
1917 tags = strdup(buf_get(diffbuf));
1919 done:
1920 buf_free(diffbuf);
1921 got_ref_list_free(&refs);
1922 if (repo)
1923 got_repo_close(repo);
1924 if (error)
1925 return NULL;
1926 else
1927 return tags;
1930 static char *
1931 gw_get_repo_heads(struct trans *gw_trans)
1933 const struct got_error *error = NULL;
1934 struct got_repository *repo = NULL;
1935 struct got_reflist_head refs;
1936 struct got_reflist_entry *re;
1937 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
1938 struct buf *diffbuf = NULL;
1939 size_t newsize;
1941 error = buf_alloc(&diffbuf, 0);
1942 if (error != NULL)
1943 return NULL;
1945 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1946 if (error != NULL)
1947 goto done;
1949 SIMPLEQ_INIT(&refs);
1950 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
1951 NULL);
1952 if (error)
1953 goto done;
1955 SIMPLEQ_FOREACH(re, &refs, entry) {
1956 char *refname;
1958 refname = strdup(got_ref_get_name(re->ref));
1959 if (refname == NULL) {
1960 error = got_error_from_errno("got_ref_to_str");
1961 goto done;
1964 if (strncmp(refname, "refs/heads/", 11) != 0) {
1965 free(refname);
1966 continue;
1969 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
1970 TM_DIFF);
1972 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
1973 refname, gw_trans->repo_name, refname,
1974 gw_trans->repo_name, refname, gw_trans->repo_name,
1975 refname)) == -1) {
1976 error = got_error_from_errno("asprintf");
1977 goto done;
1980 if (strncmp(refname, "refs/heads/", 11) == 0)
1981 refname += 11;
1983 if ((asprintf(&head_row, heads_row, age, refname,
1984 head_navs_disp)) == -1) {
1985 error = got_error_from_errno("asprintf");
1986 goto done;
1989 error = buf_puts(&newsize, diffbuf, head_row);
1991 free(head_navs_disp);
1992 free(head_row);
1995 if (buf_len(diffbuf) > 0) {
1996 error = buf_putc(diffbuf, '\0');
1997 heads = strdup(buf_get(diffbuf));
1999 done:
2000 buf_free(diffbuf);
2001 got_ref_list_free(&refs);
2002 if (repo)
2003 got_repo_close(repo);
2004 if (error)
2005 return NULL;
2006 else
2007 return heads;
2010 static char *
2011 gw_get_got_link(struct trans *gw_trans)
2013 char *link;
2015 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2016 gw_trans->gw_conf->got_logo)) == -1)
2017 return NULL;
2019 return link;
2022 static char *
2023 gw_get_site_link(struct trans *gw_trans)
2025 char *link, *repo = "", *action = "";
2027 if (gw_trans->repo_name != NULL)
2028 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2029 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2030 return NULL;
2032 if (gw_trans->action_name != NULL)
2033 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2034 return NULL;
2036 if ((asprintf(&link, site_link, GOTWEB,
2037 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2038 return NULL;
2040 return link;
2043 static char *
2044 gw_html_escape(const char *html)
2046 char *escaped_str = NULL, *buf;
2047 char c[1];
2048 size_t sz, i, buff_sz = 2048;
2050 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2051 return NULL;
2053 if (html == NULL)
2054 return NULL;
2055 else
2056 if ((sz = strlen(html)) == 0)
2057 return NULL;
2059 /* only work with buff_sz */
2060 if (buff_sz < sz)
2061 sz = buff_sz;
2063 for (i = 0; i < sz; i++) {
2064 c[0] = html[i];
2065 switch (c[0]) {
2066 case ('>'):
2067 strcat(buf, "&gt;");
2068 break;
2069 case ('&'):
2070 strcat(buf, "&amp;");
2071 break;
2072 case ('<'):
2073 strcat(buf, "&lt;");
2074 break;
2075 case ('"'):
2076 strcat(buf, "&quot;");
2077 break;
2078 case ('\''):
2079 strcat(buf, "&apos;");
2080 break;
2081 case ('\n'):
2082 strcat(buf, "<br />");
2083 case ('|'):
2084 strcat(buf, " ");
2085 default:
2086 strcat(buf, &c[0]);
2087 break;
2090 asprintf(&escaped_str, "%s", buf);
2091 free(buf);
2092 return escaped_str;
2095 int
2096 main()
2098 const struct got_error *error = NULL;
2099 struct trans *gw_trans;
2100 struct gw_dir *dir = NULL, *tdir;
2101 const char *page = "index";
2102 int gw_malloc = 1;
2104 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
2105 errx(1, "malloc");
2107 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2108 errx(1, "malloc");
2110 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2111 errx(1, "malloc");
2113 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2114 errx(1, "malloc");
2116 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
2117 &page, 1, 0))
2118 errx(1, "khttp_parse");
2120 if ((gw_trans->gw_conf =
2121 malloc(sizeof(struct gotweb_conf))) == NULL) {
2122 gw_malloc = 0;
2123 error = got_error_from_errno("malloc");
2124 goto err;
2127 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
2128 error = got_error_from_errno("pledge");
2129 goto err;
2132 TAILQ_INIT(&gw_trans->gw_dirs);
2134 gw_trans->page = 0;
2135 gw_trans->repos_total = 0;
2136 gw_trans->repo_path = NULL;
2137 gw_trans->commit = NULL;
2138 gw_trans->headref = strdup(GOT_REF_HEAD);
2139 gw_trans->mime = KMIME_TEXT_HTML;
2140 gw_trans->gw_tmpl->key = templs;
2141 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2142 gw_trans->gw_tmpl->arg = gw_trans;
2143 gw_trans->gw_tmpl->cb = gw_template;
2144 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2146 err:
2147 if (error) {
2148 gw_trans->mime = KMIME_TEXT_PLAIN;
2149 gw_trans->action = GW_ERR;
2150 gw_display_index(gw_trans, error);
2151 goto done;
2154 error = gw_parse_querystring(gw_trans);
2155 if (error)
2156 goto err;
2158 gw_display_index(gw_trans, error);
2160 done:
2161 if (gw_malloc) {
2162 free(gw_trans->gw_conf->got_repos_path);
2163 free(gw_trans->gw_conf->got_www_path);
2164 free(gw_trans->gw_conf->got_site_name);
2165 free(gw_trans->gw_conf->got_site_owner);
2166 free(gw_trans->gw_conf->got_site_link);
2167 free(gw_trans->gw_conf->got_logo);
2168 free(gw_trans->gw_conf->got_logo_url);
2169 free(gw_trans->gw_conf);
2170 free(gw_trans->commit);
2171 free(gw_trans->repo_path);
2172 free(gw_trans->repo_name);
2173 free(gw_trans->repo_file);
2174 free(gw_trans->action_name);
2175 free(gw_trans->headref);
2177 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2178 free(dir->name);
2179 free(dir->description);
2180 free(dir->age);
2181 free(dir->url);
2182 free(dir->path);
2183 free(dir);
2188 khttp_free(gw_trans->gw_req);
2189 return EXIT_SUCCESS;