Blob


1 /*
2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
22 #include <dirent.h>
23 #include <err.h>
24 #include <errno.h>
25 #include <regex.h>
26 #include <stdarg.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include <got_object.h>
34 #include <got_reference.h>
35 #include <got_repository.h>
36 #include <got_path.h>
37 #include <got_cancel.h>
38 #include <got_worktree.h>
39 #include <got_diff.h>
40 #include <got_commit_graph.h>
41 #include <got_blame.h>
42 #include <got_privsep.h>
43 #include <got_opentemp.h>
45 #include <kcgi.h>
46 #include <kcgihtml.h>
48 #include "buf.h"
49 #include "gotweb.h"
50 #include "gotweb_ui.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 struct gw_trans {
57 TAILQ_HEAD(headers, gw_header) gw_headers;
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 *repo_folder;
69 char *action_name;
70 char *headref;
71 unsigned int action;
72 unsigned int page;
73 unsigned int repos_total;
74 enum kmime mime;
75 };
77 struct gw_header {
78 TAILQ_ENTRY(gw_header) entry;
79 struct got_repository *repo;
80 struct got_reflist_head refs;
81 struct got_commit_object *commit;
82 struct got_object_id *id;
83 char *path;
85 char *refs_str;
86 char *commit_id; /* id_str1 */
87 char *parent_id; /* id_str2 */
88 char *tree_id;
89 char *author;
90 char *committer;
91 char *commit_msg;
92 time_t committer_time;
93 };
95 struct gw_dir {
96 TAILQ_ENTRY(gw_dir) entry;
97 char *name;
98 char *owner;
99 char *description;
100 char *url;
101 char *age;
102 char *path;
103 };
105 enum gw_key {
106 KEY_ACTION,
107 KEY_COMMIT_ID,
108 KEY_FILE,
109 KEY_FOLDER,
110 KEY_HEADREF,
111 KEY_PAGE,
112 KEY_PATH,
113 KEY__ZMAX
114 };
116 enum gw_tmpl {
117 TEMPL_CONTENT,
118 TEMPL_HEAD,
119 TEMPL_HEADER,
120 TEMPL_SEARCH,
121 TEMPL_SITEPATH,
122 TEMPL_SITEOWNER,
123 TEMPL_TITLE,
124 TEMPL__MAX
125 };
127 enum gw_ref_tm {
128 TM_DIFF,
129 TM_LONG,
130 };
132 enum gw_tags {
133 TAGBRIEF,
134 TAGFULL,
135 };
137 static const char *const gw_templs[TEMPL__MAX] = {
138 "content",
139 "head",
140 "header",
141 "search",
142 "sitepath",
143 "siteowner",
144 "title",
145 };
147 static const struct kvalid gw_keys[KEY__ZMAX] = {
148 { kvalid_stringne, "action" },
149 { kvalid_stringne, "commit" },
150 { kvalid_stringne, "file" },
151 { kvalid_stringne, "folder" },
152 { kvalid_stringne, "headref" },
153 { kvalid_int, "page" },
154 { kvalid_stringne, "path" },
155 };
157 static struct gw_dir *gw_init_gw_dir(char *);
158 static struct gw_header *gw_init_header(void);
160 static char *gw_get_repo_description(struct gw_trans *,
161 char *);
162 static char *gw_get_repo_owner(struct gw_trans *,
163 char *);
164 static char *gw_get_time_str(time_t, int);
165 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
166 char *, char *, int);
167 static char *gw_get_file_blame(struct gw_trans *);
168 static char *gw_get_repo_tree(struct gw_trans *);
169 static char *gw_get_diff(struct gw_trans *,
170 struct gw_header *);
171 static char *gw_get_repo_tags(struct gw_trans *,
172 struct gw_header *, int, int);
173 static char *gw_get_repo_heads(struct gw_trans *);
174 static char *gw_get_clone_url(struct gw_trans *, char *);
175 static char *gw_get_got_link(struct gw_trans *);
176 static char *gw_get_site_link(struct gw_trans *);
177 static char *gw_html_escape(const char *);
178 static char *gw_colordiff_line(char *);
180 static char *gw_gen_commit_header(char *, char*);
181 static char *gw_gen_diff_header(char *, char*);
182 static char *gw_gen_author_header(char *);
183 static char *gw_gen_committer_header(char *);
184 static char *gw_gen_age_header(char *);
185 static char *gw_gen_commit_msg_header(char *);
186 static char *gw_gen_tree_header(char *);
188 static void gw_free_headers(struct gw_header *);
189 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
190 enum kmime);
191 static const struct got_error* gw_display_index(struct gw_trans *);
192 static void gw_display_error(struct gw_trans *,
193 const struct got_error *);
195 static int gw_template(size_t, void *);
197 static const struct got_error* gw_get_header(struct gw_trans *,
198 struct gw_header *, int);
199 static const struct got_error* gw_get_commits(struct gw_trans *,
200 struct gw_header *, int);
201 static const struct got_error* gw_get_commit(struct gw_trans *,
202 struct gw_header *);
203 static const struct got_error* gw_apply_unveil(const char *, const char *);
204 static const struct got_error* gw_blame_cb(void *, int, int,
205 struct got_object_id *);
206 static const struct got_error* gw_load_got_paths(struct gw_trans *);
207 static const struct got_error* gw_load_got_path(struct gw_trans *,
208 struct gw_dir *);
209 static const struct got_error* gw_parse_querystring(struct gw_trans *);
211 static const struct got_error* gw_blame(struct gw_trans *);
212 static const struct got_error* gw_diff(struct gw_trans *);
213 static const struct got_error* gw_index(struct gw_trans *);
214 static const struct got_error* gw_commits(struct gw_trans *);
215 static const struct got_error* gw_briefs(struct gw_trans *);
216 static const struct got_error* gw_summary(struct gw_trans *);
217 static const struct got_error* gw_tree(struct gw_trans *);
218 static const struct got_error* gw_tag(struct gw_trans *);
220 struct gw_query_action {
221 unsigned int func_id;
222 const char *func_name;
223 const struct got_error *(*func_main)(struct gw_trans *);
224 char *template;
225 };
227 enum gw_query_actions {
228 GW_BLAME,
229 GW_BRIEFS,
230 GW_COMMITS,
231 GW_DIFF,
232 GW_ERR,
233 GW_INDEX,
234 GW_SUMMARY,
235 GW_TAG,
236 GW_TREE,
237 };
239 static struct gw_query_action gw_query_funcs[] = {
240 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
241 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
242 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
243 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
244 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
245 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
246 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
247 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
248 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
249 };
251 static const struct got_error *
252 gw_kcgi_error(enum kcgi_err kerr)
254 if (kerr == KCGI_OK)
255 return NULL;
257 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
258 return got_error(GOT_ERR_CANCELLED);
260 if (kerr == KCGI_ENOMEM)
261 return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
263 if (kerr == KCGI_ENFILE)
264 return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
266 if (kerr == KCGI_EAGAIN)
267 return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
269 if (kerr == KCGI_FORM)
270 return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
272 return got_error_from_errno(kcgi_strerror(kerr));
275 static const struct got_error *
276 gw_apply_unveil(const char *repo_path, const char *repo_file)
278 const struct got_error *err;
280 if (repo_path && repo_file) {
281 char *full_path;
282 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
283 return got_error_from_errno("asprintf unveil");
284 if (unveil(full_path, "r") != 0)
285 return got_error_from_errno2("unveil", full_path);
288 if (repo_path && unveil(repo_path, "r") != 0)
289 return got_error_from_errno2("unveil", repo_path);
291 if (unveil("/tmp", "rwc") != 0)
292 return got_error_from_errno2("unveil", "/tmp");
294 err = got_privsep_unveil_exec_helpers();
295 if (err != NULL)
296 return err;
298 if (unveil(NULL, NULL) != 0)
299 return got_error_from_errno("unveil");
301 return NULL;
304 static const struct got_error *
305 gw_blame(struct gw_trans *gw_trans)
307 const struct got_error *error = NULL;
308 struct gw_header *header = NULL;
309 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
310 enum kcgi_err kerr;
312 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
313 NULL) == -1)
314 return got_error_from_errno("pledge");
316 if ((header = gw_init_header()) == NULL)
317 return got_error_from_errno("malloc");
319 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
320 if (error)
321 goto done;
323 error = gw_get_header(gw_trans, header, 1);
324 if (error)
325 goto done;
327 blame_html = gw_get_file_blame(gw_trans);
329 if (blame_html == NULL) {
330 blame_html = strdup("");
331 if (blame_html == NULL) {
332 error = got_error_from_errno("strdup");
333 goto done;
337 if (asprintf(&blame_html_disp, blame_header,
338 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
339 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
340 blame_html) == -1) {
341 error = got_error_from_errno("asprintf");
342 goto done;
345 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
346 error = got_error_from_errno("asprintf");
347 goto done;
350 kerr = khttp_puts(gw_trans->gw_req, blame);
351 if (kerr != KCGI_OK)
352 error = gw_kcgi_error(kerr);
353 done:
354 got_ref_list_free(&header->refs);
355 gw_free_headers(header);
356 free(blame_html_disp);
357 free(blame_html);
358 free(blame);
359 return error;
362 static const struct got_error *
363 gw_diff(struct gw_trans *gw_trans)
365 const struct got_error *error = NULL;
366 struct gw_header *header = NULL;
367 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
368 enum kcgi_err kerr;
370 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
371 NULL) == -1)
372 return got_error_from_errno("pledge");
374 if ((header = gw_init_header()) == NULL)
375 return got_error_from_errno("malloc");
377 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
378 if (error)
379 goto done;
381 error = gw_get_header(gw_trans, header, 1);
382 if (error)
383 goto done;
385 diff_html = gw_get_diff(gw_trans, header);
387 if (diff_html == NULL) {
388 diff_html = strdup("");
389 if (diff_html == NULL) {
390 error = got_error_from_errno("strdup");
391 goto done;
395 if (asprintf(&diff_html_disp, diff_header,
396 gw_gen_diff_header(header->parent_id, header->commit_id),
397 gw_gen_commit_header(header->commit_id, header->refs_str),
398 gw_gen_tree_header(header->tree_id),
399 gw_gen_author_header(header->author),
400 gw_gen_committer_header(header->committer),
401 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
402 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
403 diff_html) == -1) {
404 error = got_error_from_errno("asprintf");
405 goto done;
408 if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
409 error = got_error_from_errno("asprintf");
410 goto done;
413 kerr = khttp_puts(gw_trans->gw_req, diff);
414 if (kerr != KCGI_OK)
415 error = gw_kcgi_error(kerr);
416 done:
417 got_ref_list_free(&header->refs);
418 gw_free_headers(header);
419 free(diff_html_disp);
420 free(diff_html);
421 free(diff);
422 return error;
425 static const struct got_error *
426 gw_index(struct gw_trans *gw_trans)
428 const struct got_error *error = NULL;
429 struct gw_dir *gw_dir = NULL;
430 char *html, *navs, *next, *prev;
431 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
432 enum kcgi_err kerr;
434 if (pledge("stdio rpath proc exec sendfd unveil",
435 NULL) == -1) {
436 error = got_error_from_errno("pledge");
437 return error;
440 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
441 if (error)
442 return error;
444 error = gw_load_got_paths(gw_trans);
445 if (error)
446 return error;
448 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
449 if (kerr != KCGI_OK)
450 return gw_kcgi_error(kerr);
452 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
453 if (asprintf(&html, index_projects_empty,
454 gw_trans->gw_conf->got_repos_path) == -1)
455 return got_error_from_errno("asprintf");
456 kerr = khttp_puts(gw_trans->gw_req, html);
457 if (kerr != KCGI_OK)
458 error = gw_kcgi_error(kerr);
459 free(html);
460 return error;
463 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
464 dir_c++;
466 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
467 if (gw_trans->page > 0 && (gw_trans->page *
468 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
469 prev_disp++;
470 continue;
473 prev_disp++;
475 if (error)
476 return error;
477 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
478 gw_dir->name, gw_dir->name) == -1)
479 return got_error_from_errno("asprintf");
481 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
482 gw_dir->description, gw_dir->owner, gw_dir->age,
483 navs) == -1)
484 return got_error_from_errno("asprintf");
486 kerr = khttp_puts(gw_trans->gw_req, html);
487 free(navs);
488 free(html);
489 if (kerr != KCGI_OK)
490 return gw_kcgi_error(kerr);
492 if (gw_trans->gw_conf->got_max_repos_display == 0)
493 continue;
495 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
496 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
497 if (kerr != KCGI_OK)
498 return gw_kcgi_error(kerr);
499 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
500 (gw_trans->page > 0) &&
501 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
502 prev_disp == gw_trans->repos_total)) {
503 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
504 if (kerr != KCGI_OK)
505 return gw_kcgi_error(kerr);
508 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
509 (gw_trans->page > 0) &&
510 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
511 prev_disp == gw_trans->repos_total)) {
512 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
513 return got_error_from_errno("asprintf");
514 kerr = khttp_puts(gw_trans->gw_req, prev);
515 free(prev);
516 if (kerr != KCGI_OK)
517 return gw_kcgi_error(kerr);
520 kerr = khttp_puts(gw_trans->gw_req, div_end);
521 if (kerr != KCGI_OK)
522 return gw_kcgi_error(kerr);
524 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
525 next_disp == gw_trans->gw_conf->got_max_repos_display &&
526 dir_c != (gw_trans->page + 1) *
527 gw_trans->gw_conf->got_max_repos_display) {
528 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
529 return got_error_from_errno("calloc");
530 kerr = khttp_puts(gw_trans->gw_req, next);
531 free(next);
532 if (kerr != KCGI_OK)
533 return gw_kcgi_error(kerr);
534 kerr = khttp_puts(gw_trans->gw_req, div_end);
535 if (kerr != KCGI_OK)
536 error = gw_kcgi_error(kerr);
537 next_disp = 0;
538 break;
541 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
542 (gw_trans->page > 0) &&
543 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
544 prev_disp == gw_trans->repos_total)) {
545 kerr = khttp_puts(gw_trans->gw_req, div_end);
546 if (kerr != KCGI_OK)
547 return gw_kcgi_error(kerr);
550 next_disp++;
552 return error;
555 static const struct got_error *
556 gw_commits(struct gw_trans *gw_trans)
558 const struct got_error *error = NULL;
559 char *commits_html, *commits_navs_html;
560 struct gw_header *header = NULL, *n_header = NULL;
561 enum kcgi_err kerr;
563 if ((header = gw_init_header()) == NULL)
564 return got_error_from_errno("malloc");
566 if (pledge("stdio rpath proc exec sendfd unveil",
567 NULL) == -1) {
568 error = got_error_from_errno("pledge");
569 goto done;
572 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
573 if (error)
574 goto done;
576 error = gw_get_header(gw_trans, header,
577 gw_trans->gw_conf->got_max_commits_display);
578 if (error)
579 goto done;
581 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
582 if (kerr != KCGI_OK) {
583 error = gw_kcgi_error(kerr);
584 goto done;
586 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
587 if (asprintf(&commits_navs_html, commits_navs,
588 gw_trans->repo_name, n_header->commit_id,
589 gw_trans->repo_name, n_header->commit_id,
590 gw_trans->repo_name, n_header->commit_id) == -1) {
591 error = got_error_from_errno("asprintf");
592 goto done;
595 if (asprintf(&commits_html, commits_line,
596 gw_gen_commit_header(n_header->commit_id,
597 n_header->refs_str),
598 gw_gen_author_header(n_header->author),
599 gw_gen_committer_header(n_header->committer),
600 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
601 TM_LONG)), gw_html_escape(n_header->commit_msg),
602 commits_navs_html) == -1) {
603 error = got_error_from_errno("asprintf");
604 goto done;
606 kerr = khttp_puts(gw_trans->gw_req, commits_html);
607 if (kerr != KCGI_OK) {
608 error = gw_kcgi_error(kerr);
609 goto done;
612 kerr = khttp_puts(gw_trans->gw_req, div_end);
613 if (kerr != KCGI_OK)
614 error = gw_kcgi_error(kerr);
615 done:
616 got_ref_list_free(&header->refs);
617 gw_free_headers(header);
618 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
619 gw_free_headers(n_header);
620 return error;
623 static const struct got_error *
624 gw_briefs(struct gw_trans *gw_trans)
626 const struct got_error *error = NULL;
627 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
628 struct gw_header *header = NULL, *n_header = NULL;
629 enum kcgi_err kerr;
631 if ((header = gw_init_header()) == NULL)
632 return got_error_from_errno("malloc");
634 if (pledge("stdio rpath proc exec sendfd unveil",
635 NULL) == -1) {
636 error = got_error_from_errno("pledge");
637 goto done;
640 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
641 if (error)
642 goto done;
644 if (gw_trans->action == GW_SUMMARY)
645 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
646 else
647 error = gw_get_header(gw_trans, header,
648 gw_trans->gw_conf->got_max_commits_display);
649 if (error)
650 goto done;
652 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
653 if (kerr != KCGI_OK) {
654 error = gw_kcgi_error(kerr);
655 goto done;
658 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
659 if (asprintf(&briefs_navs_html, briefs_navs,
660 gw_trans->repo_name, n_header->commit_id,
661 gw_trans->repo_name, n_header->commit_id,
662 gw_trans->repo_name, n_header->commit_id) == -1) {
663 error = got_error_from_errno("asprintf");
664 goto done;
666 newline = strchr(n_header->commit_msg, '\n');
667 if (newline)
668 *newline = '\0';
669 if (asprintf(&briefs_html, briefs_line,
670 gw_get_time_str(n_header->committer_time, TM_DIFF),
671 n_header->author, gw_html_escape(n_header->commit_msg),
672 briefs_navs_html) == -1) {
673 error = got_error_from_errno("asprintf");
674 goto done;
676 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
677 if (kerr != KCGI_OK) {
678 error = gw_kcgi_error(kerr);
679 goto done;
682 kerr = khttp_puts(gw_trans->gw_req, div_end);
683 if (kerr != KCGI_OK)
684 error = gw_kcgi_error(kerr);
685 done:
686 got_ref_list_free(&header->refs);
687 gw_free_headers(header);
688 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
689 gw_free_headers(n_header);
690 return error;
693 static const struct got_error *
694 gw_summary(struct gw_trans *gw_trans)
696 const struct got_error *error = NULL;
697 char *description_html = NULL, *repo_owner_html = NULL;
698 char *age = NULL, *repo_age_html = NULL, *cloneurl_html = NULL;
699 char *tags = NULL, *tags_html = NULL;
700 char *heads = NULL, *heads_html = NULL;
701 enum kcgi_err kerr;
703 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
704 return got_error_from_errno("pledge");
706 /* unveil is applied with gw_briefs below */
708 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
709 if (kerr != KCGI_OK)
710 return gw_kcgi_error(kerr);
712 if (gw_trans->gw_conf->got_show_repo_description) {
713 if (gw_trans->gw_dir->description != NULL &&
714 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
715 if (asprintf(&description_html, description,
716 gw_trans->gw_dir->description) == -1) {
717 error = got_error_from_errno("asprintf");
718 goto done;
721 kerr = khttp_puts(gw_trans->gw_req, description_html);
722 if (kerr != KCGI_OK) {
723 error = gw_kcgi_error(kerr);
724 goto done;
729 if (gw_trans->gw_conf->got_show_repo_owner) {
730 if (gw_trans->gw_dir->owner != NULL &&
731 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
732 if (asprintf(&repo_owner_html, repo_owner,
733 gw_trans->gw_dir->owner) == -1) {
734 error = got_error_from_errno("asprintf");
735 goto done;
738 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
739 if (kerr != KCGI_OK) {
740 error = gw_kcgi_error(kerr);
741 goto done;
746 if (gw_trans->gw_conf->got_show_repo_age) {
747 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
748 "refs/heads", TM_LONG);
749 if (error)
750 goto done;
751 if (strcmp(age, "") != 0) {
752 if (asprintf(&repo_age_html, last_change, age) == -1) {
753 error = got_error_from_errno("asprintf");
754 goto done;
757 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
758 if (kerr != KCGI_OK) {
759 error = gw_kcgi_error(kerr);
760 goto done;
765 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
766 if (gw_trans->gw_dir->url != NULL &&
767 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
768 if (asprintf(&cloneurl_html, cloneurl,
769 gw_trans->gw_dir->url) == -1) {
770 error = got_error_from_errno("asprintf");
771 goto done;
774 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
775 if (kerr != KCGI_OK) {
776 error = gw_kcgi_error(kerr);
777 goto done;
781 kerr = khttp_puts(gw_trans->gw_req, div_end);
782 if (kerr != KCGI_OK) {
783 error = gw_kcgi_error(kerr);
784 goto done;
787 error = gw_briefs(gw_trans);
788 if (error)
789 goto done;
791 tags = gw_get_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP, TAGBRIEF);
792 heads = gw_get_repo_heads(gw_trans);
794 if (tags != NULL && strcmp(tags, "") != 0) {
795 if (asprintf(&tags_html, summary_tags, tags) == -1) {
796 error = got_error_from_errno("asprintf");
797 goto done;
799 kerr = khttp_puts(gw_trans->gw_req, tags_html);
800 if (kerr != KCGI_OK) {
801 error = gw_kcgi_error(kerr);
802 goto done;
806 if (heads != NULL && strcmp(heads, "") != 0) {
807 if (asprintf(&heads_html, summary_heads, heads) == -1) {
808 error = got_error_from_errno("asprintf");
809 goto done;
811 kerr = khttp_puts(gw_trans->gw_req, heads_html);
812 if (kerr != KCGI_OK) {
813 error = gw_kcgi_error(kerr);
814 goto done;
817 done:
818 free(description_html);
819 free(repo_owner_html);
820 free(age);
821 free(repo_age_html);
822 free(cloneurl_html);
823 free(tags);
824 free(tags_html);
825 free(heads);
826 free(heads_html);
827 return error;
830 static const struct got_error *
831 gw_tree(struct gw_trans *gw_trans)
833 const struct got_error *error = NULL;
834 struct gw_header *header = NULL;
835 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
836 enum kcgi_err kerr;
838 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
839 return got_error_from_errno("pledge");
841 if ((header = gw_init_header()) == NULL)
842 return got_error_from_errno("malloc");
844 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
845 if (error)
846 goto done;
848 error = gw_get_header(gw_trans, header, 1);
849 if (error)
850 goto done;
852 tree_html = gw_get_repo_tree(gw_trans);
854 if (tree_html == NULL) {
855 tree_html = strdup("");
856 if (tree_html == NULL) {
857 error = got_error_from_errno("strdup");
858 goto done;
862 if (asprintf(&tree_html_disp, tree_header,
863 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
864 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
865 tree_html) == -1) {
866 error = got_error_from_errno("asprintf");
867 goto done;
870 if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
871 error = got_error_from_errno("asprintf");
872 goto done;
875 kerr = khttp_puts(gw_trans->gw_req, tree);
876 if (kerr != KCGI_OK)
877 error = gw_kcgi_error(kerr);
878 done:
879 got_ref_list_free(&header->refs);
880 gw_free_headers(header);
881 free(tree_html_disp);
882 free(tree_html);
883 free(tree);
884 return error;
887 static const struct got_error *
888 gw_tag(struct gw_trans *gw_trans)
890 const struct got_error *error = NULL;
891 struct gw_header *header = NULL;
892 char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
893 enum kcgi_err kerr;
895 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
896 return got_error_from_errno("pledge");
898 if ((header = gw_init_header()) == NULL)
899 return got_error_from_errno("malloc");
901 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
902 if (error)
903 goto done;
905 error = gw_get_header(gw_trans, header, 1);
906 if (error)
907 goto done;
909 tag_html = gw_get_repo_tags(gw_trans, header, 1, TAGFULL);
910 if (tag_html == NULL) {
911 tag_html = strdup("");
912 if (tag_html == NULL) {
913 error = got_error_from_errno("strdup");
914 goto done;
918 if (asprintf(&tag_html_disp, tag_header,
919 gw_gen_commit_header(header->commit_id, header->refs_str),
920 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
921 tag_html) == -1) {
922 error = got_error_from_errno("asprintf");
923 goto done;
926 if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
927 error = got_error_from_errno("asprintf");
928 goto done;
931 kerr = khttp_puts(gw_trans->gw_req, tag);
932 if (kerr != KCGI_OK)
933 error = gw_kcgi_error(kerr);
934 done:
935 got_ref_list_free(&header->refs);
936 gw_free_headers(header);
937 free(tag_html_disp);
938 free(tag_html);
939 free(tag);
940 return error;
943 static const struct got_error *
944 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
946 const struct got_error *error = NULL;
947 DIR *dt;
948 char *dir_test;
949 int opened = 0;
951 if (asprintf(&dir_test, "%s/%s/%s",
952 gw_trans->gw_conf->got_repos_path, gw_dir->name,
953 GOTWEB_GIT_DIR) == -1)
954 return got_error_from_errno("asprintf");
956 dt = opendir(dir_test);
957 if (dt == NULL) {
958 free(dir_test);
959 } else {
960 gw_dir->path = strdup(dir_test);
961 opened = 1;
962 goto done;
965 if (asprintf(&dir_test, "%s/%s/%s",
966 gw_trans->gw_conf->got_repos_path, gw_dir->name,
967 GOTWEB_GOT_DIR) == -1)
968 return got_error_from_errno("asprintf");
970 dt = opendir(dir_test);
971 if (dt == NULL)
972 free(dir_test);
973 else {
974 opened = 1;
975 error = got_error(GOT_ERR_NOT_GIT_REPO);
976 goto errored;
979 if (asprintf(&dir_test, "%s/%s",
980 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
981 return got_error_from_errno("asprintf");
983 gw_dir->path = strdup(dir_test);
985 done:
986 gw_dir->description = gw_get_repo_description(gw_trans,
987 gw_dir->path);
988 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
989 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
990 "refs/heads", TM_DIFF);
991 if (error)
992 goto errored;
993 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
995 errored:
996 free(dir_test);
997 if (opened)
998 closedir(dt);
999 return error;
1002 static const struct got_error *
1003 gw_load_got_paths(struct gw_trans *gw_trans)
1005 const struct got_error *error = NULL;
1006 DIR *d;
1007 struct dirent **sd_dent;
1008 struct gw_dir *gw_dir;
1009 struct stat st;
1010 unsigned int d_cnt, d_i;
1012 d = opendir(gw_trans->gw_conf->got_repos_path);
1013 if (d == NULL) {
1014 error = got_error_from_errno2("opendir",
1015 gw_trans->gw_conf->got_repos_path);
1016 return error;
1019 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1020 alphasort);
1021 if (d_cnt == -1) {
1022 error = got_error_from_errno2("scandir",
1023 gw_trans->gw_conf->got_repos_path);
1024 return error;
1027 for (d_i = 0; d_i < d_cnt; d_i++) {
1028 if (gw_trans->gw_conf->got_max_repos > 0 &&
1029 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1030 break; /* account for parent and self */
1032 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1033 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1034 continue;
1036 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1037 return got_error_from_errno("gw_dir malloc");
1039 error = gw_load_got_path(gw_trans, gw_dir);
1040 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1041 continue;
1042 else if (error)
1043 return error;
1045 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1046 !got_path_dir_is_empty(gw_dir->path)) {
1047 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1048 entry);
1049 gw_trans->repos_total++;
1053 closedir(d);
1054 return error;
1057 static const struct got_error *
1058 gw_parse_querystring(struct gw_trans *gw_trans)
1060 const struct got_error *error = NULL;
1061 struct kpair *p;
1062 struct gw_query_action *action = NULL;
1063 unsigned int i;
1065 if (gw_trans->gw_req->fieldnmap[0]) {
1066 error = got_error_from_errno("bad parse");
1067 return error;
1068 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1069 /* define gw_trans->repo_path */
1070 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1071 return got_error_from_errno("asprintf");
1073 if (asprintf(&gw_trans->repo_path, "%s/%s",
1074 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1075 return got_error_from_errno("asprintf");
1077 /* get action and set function */
1078 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1079 for (i = 0; i < nitems(gw_query_funcs); i++) {
1080 action = &gw_query_funcs[i];
1081 if (action->func_name == NULL)
1082 continue;
1084 if (strcmp(action->func_name,
1085 p->parsed.s) == 0) {
1086 gw_trans->action = i;
1087 if (asprintf(&gw_trans->action_name,
1088 "%s", action->func_name) == -1)
1089 return
1090 got_error_from_errno(
1091 "asprintf");
1093 break;
1096 action = NULL;
1099 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1100 if (asprintf(&gw_trans->commit, "%s",
1101 p->parsed.s) == -1)
1102 return got_error_from_errno("asprintf");
1104 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1105 if (asprintf(&gw_trans->repo_file, "%s",
1106 p->parsed.s) == -1)
1107 return got_error_from_errno("asprintf");
1109 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1110 if (asprintf(&gw_trans->repo_folder, "%s",
1111 p->parsed.s) == -1)
1112 return got_error_from_errno("asprintf");
1114 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1115 if (asprintf(&gw_trans->headref, "%s",
1116 p->parsed.s) == -1)
1117 return got_error_from_errno("asprintf");
1119 if (action == NULL) {
1120 error = got_error_from_errno("invalid action");
1121 return error;
1123 if ((gw_trans->gw_dir =
1124 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1125 return got_error_from_errno("gw_dir malloc");
1127 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1128 if (error)
1129 return error;
1130 } else
1131 gw_trans->action = GW_INDEX;
1133 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1134 gw_trans->page = p->parsed.i;
1136 /* if (gw_trans->action == GW_RAW) */
1137 /* gw_trans->mime = KMIME_TEXT_PLAIN; */
1139 return error;
1142 static struct gw_dir *
1143 gw_init_gw_dir(char *dir)
1145 struct gw_dir *gw_dir;
1147 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1148 return NULL;
1150 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1151 return NULL;
1153 return gw_dir;
1156 static const struct got_error *
1157 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1159 enum kcgi_err kerr;
1161 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1162 if (kerr != KCGI_OK)
1163 return gw_kcgi_error(kerr);
1164 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1165 khttps[code]);
1166 if (kerr != KCGI_OK)
1167 return gw_kcgi_error(kerr);
1168 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1169 kmimetypes[mime]);
1170 if (kerr != KCGI_OK)
1171 return gw_kcgi_error(kerr);
1172 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1173 if (kerr != KCGI_OK)
1174 return gw_kcgi_error(kerr);
1175 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1176 if (kerr != KCGI_OK)
1177 return gw_kcgi_error(kerr);
1178 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1179 if (kerr != KCGI_OK)
1180 return gw_kcgi_error(kerr);
1182 kerr = khttp_body(gw_trans->gw_req);
1183 return gw_kcgi_error(kerr);
1186 static const struct got_error *
1187 gw_display_index(struct gw_trans *gw_trans)
1189 const struct got_error *error;
1190 enum kcgi_err kerr;
1192 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1193 if (error)
1194 return error;
1196 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1197 if (kerr)
1198 return gw_kcgi_error(kerr);
1200 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1201 gw_query_funcs[gw_trans->action].template);
1202 if (kerr != KCGI_OK) {
1203 khtml_close(gw_trans->gw_html_req);
1204 return gw_kcgi_error(kerr);
1207 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1210 static void
1211 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1213 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1214 return;
1216 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1217 return;
1219 khttp_puts(gw_trans->gw_req, err->msg);
1220 khtml_close(gw_trans->gw_html_req);
1223 static int
1224 gw_template(size_t key, void *arg)
1226 const struct got_error *error = NULL;
1227 struct gw_trans *gw_trans = arg;
1228 char *gw_got_link, *gw_site_link;
1229 char *site_owner_name, *site_owner_name_h;
1231 switch (key) {
1232 case (TEMPL_HEAD):
1233 khttp_puts(gw_trans->gw_req, head);
1234 break;
1235 case(TEMPL_HEADER):
1236 gw_got_link = gw_get_got_link(gw_trans);
1237 if (gw_got_link != NULL)
1238 khttp_puts(gw_trans->gw_req, gw_got_link);
1240 free(gw_got_link);
1241 break;
1242 case (TEMPL_SITEPATH):
1243 gw_site_link = gw_get_site_link(gw_trans);
1244 if (gw_site_link != NULL)
1245 khttp_puts(gw_trans->gw_req, gw_site_link);
1247 free(gw_site_link);
1248 break;
1249 case(TEMPL_TITLE):
1250 if (gw_trans->gw_conf->got_site_name != NULL)
1251 khtml_puts(gw_trans->gw_html_req,
1252 gw_trans->gw_conf->got_site_name);
1254 break;
1255 case (TEMPL_SEARCH):
1256 khttp_puts(gw_trans->gw_req, search);
1257 break;
1258 case(TEMPL_SITEOWNER):
1259 if (gw_trans->gw_conf->got_site_owner != NULL &&
1260 gw_trans->gw_conf->got_show_site_owner) {
1261 site_owner_name =
1262 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1263 if (asprintf(&site_owner_name_h, site_owner,
1264 site_owner_name) == -1)
1265 return 0;
1267 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1268 free(site_owner_name);
1269 free(site_owner_name_h);
1271 break;
1272 case(TEMPL_CONTENT):
1273 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1274 if (error)
1275 khttp_puts(gw_trans->gw_req, error->msg);
1276 break;
1277 default:
1278 return 0;
1280 return 1;
1283 static char *
1284 gw_gen_commit_header(char *str1, char *str2)
1286 char *return_html = NULL, *ref_str = NULL;
1288 if (strcmp(str2, "") != 0) {
1289 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1290 return_html = strdup("");
1291 return return_html;
1293 } else
1294 ref_str = strdup("");
1297 if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1298 return_html = strdup("");
1300 free(ref_str);
1301 return return_html;
1304 static char *
1305 gw_gen_diff_header(char *str1, char *str2)
1307 char *return_html = NULL;
1309 if (asprintf(&return_html, header_diff_html, str1, str2) == -1)
1310 return_html = strdup("");
1312 return return_html;
1315 static char *
1316 gw_gen_author_header(char *str)
1318 char *return_html = NULL;
1320 if (asprintf(&return_html, header_author_html, str) == -1)
1321 return_html = strdup("");
1323 return return_html;
1326 static char *
1327 gw_gen_committer_header(char *str)
1329 char *return_html = NULL;
1331 if (asprintf(&return_html, header_committer_html, str) == -1)
1332 return_html = strdup("");
1334 return return_html;
1337 static char *
1338 gw_gen_age_header(char *str)
1340 char *return_html = NULL;
1342 if (asprintf(&return_html, header_age_html, str) == -1)
1343 return_html = strdup("");
1345 return return_html;
1348 static char *
1349 gw_gen_commit_msg_header(char *str)
1351 char *return_html = NULL;
1353 if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1354 return_html = strdup("");
1356 return return_html;
1359 static char *
1360 gw_gen_tree_header(char *str)
1362 char *return_html = NULL;
1364 if (asprintf(&return_html, header_tree_html, str) == -1)
1365 return_html = strdup("");
1367 return return_html;
1370 static char *
1371 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1373 FILE *f;
1374 char *description = NULL, *d_file = NULL;
1375 unsigned int len;
1377 if (gw_trans->gw_conf->got_show_repo_description == 0)
1378 goto err;
1380 if (asprintf(&d_file, "%s/description", dir) == -1)
1381 goto err;
1383 if ((f = fopen(d_file, "r")) == NULL)
1384 goto err;
1386 fseek(f, 0, SEEK_END);
1387 len = ftell(f) + 1;
1388 fseek(f, 0, SEEK_SET);
1389 if ((description = calloc(len, sizeof(char *))) == NULL)
1390 goto err;
1392 fread(description, 1, len, f);
1393 fclose(f);
1394 free(d_file);
1395 return description;
1396 err:
1397 return strdup("");
1400 static char *
1401 gw_get_time_str(time_t committer_time, int ref_tm)
1403 struct tm tm;
1404 time_t diff_time;
1405 char *years = "years ago", *months = "months ago";
1406 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1407 char *minutes = "minutes ago", *seconds = "seconds ago";
1408 char *now = "right now";
1409 char *repo_age, *s;
1410 char datebuf[29];
1412 switch (ref_tm) {
1413 case TM_DIFF:
1414 diff_time = time(NULL) - committer_time;
1415 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1416 if (asprintf(&repo_age, "%lld %s",
1417 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1418 return NULL;
1419 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1420 if (asprintf(&repo_age, "%lld %s",
1421 (diff_time / 60 / 60 / 24 / (365 / 12)),
1422 months) == -1)
1423 return NULL;
1424 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1425 if (asprintf(&repo_age, "%lld %s",
1426 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1427 return NULL;
1428 } else if (diff_time > 60 * 60 * 24 * 2) {
1429 if (asprintf(&repo_age, "%lld %s",
1430 (diff_time / 60 / 60 / 24), days) == -1)
1431 return NULL;
1432 } else if (diff_time > 60 * 60 * 2) {
1433 if (asprintf(&repo_age, "%lld %s",
1434 (diff_time / 60 / 60), hours) == -1)
1435 return NULL;
1436 } else if (diff_time > 60 * 2) {
1437 if (asprintf(&repo_age, "%lld %s", (diff_time / 60),
1438 minutes) == -1)
1439 return NULL;
1440 } else if (diff_time > 2) {
1441 if (asprintf(&repo_age, "%lld %s", diff_time,
1442 seconds) == -1)
1443 return NULL;
1444 } else {
1445 if (asprintf(&repo_age, "%s", now) == -1)
1446 return NULL;
1448 break;
1449 case TM_LONG:
1450 if (gmtime_r(&committer_time, &tm) == NULL)
1451 return NULL;
1453 s = asctime_r(&tm, datebuf);
1454 if (s == NULL)
1455 return NULL;
1457 if (asprintf(&repo_age, "%s UTC", datebuf) == -1)
1458 return NULL;
1459 break;
1461 return repo_age;
1464 static const struct got_error *
1465 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
1466 char *repo_ref, int ref_tm)
1468 const struct got_error *error = NULL;
1469 struct got_object_id *id = NULL;
1470 struct got_repository *repo = NULL;
1471 struct got_commit_object *commit = NULL;
1472 struct got_reflist_head refs;
1473 struct got_reflist_entry *re;
1474 struct got_reference *head_ref;
1475 int is_head = 0;
1476 time_t committer_time = 0, cmp_time = 0;
1477 const char *refname;
1479 *repo_age = NULL;
1480 SIMPLEQ_INIT(&refs);
1482 if (repo_ref == NULL)
1483 return NULL;
1485 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1486 is_head = 1;
1488 if (gw_trans->gw_conf->got_show_repo_age == 0) {
1489 *repo_age = strdup("");
1490 if (*repo_age == NULL)
1491 return got_error_from_errno("strdup");
1492 return NULL;
1495 error = got_repo_open(&repo, dir, NULL);
1496 if (error)
1497 goto done;
1499 if (is_head)
1500 error = got_ref_list(&refs, repo, "refs/heads",
1501 got_ref_cmp_by_name, NULL);
1502 else
1503 error = got_ref_list(&refs, repo, repo_ref,
1504 got_ref_cmp_by_name, NULL);
1505 if (error)
1506 goto done;
1508 SIMPLEQ_FOREACH(re, &refs, entry) {
1509 if (is_head)
1510 refname = strdup(repo_ref);
1511 else
1512 refname = got_ref_get_name(re->ref);
1513 error = got_ref_open(&head_ref, repo, refname, 0);
1514 if (error)
1515 goto done;
1517 error = got_ref_resolve(&id, repo, head_ref);
1518 got_ref_close(head_ref);
1519 if (error)
1520 goto done;
1522 error = got_object_open_as_commit(&commit, repo, id);
1523 if (error)
1524 goto done;
1526 committer_time =
1527 got_object_commit_get_committer_time(commit);
1529 if (cmp_time < committer_time)
1530 cmp_time = committer_time;
1533 if (cmp_time != 0) {
1534 committer_time = cmp_time;
1535 *repo_age = gw_get_time_str(committer_time, ref_tm);
1536 } else {
1537 *repo_age = strdup("");
1538 if (*repo_age == NULL)
1539 error = got_error_from_errno("strdup");
1541 done:
1542 got_ref_list_free(&refs);
1543 free(id);
1544 return error;
1547 static char *
1548 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1550 const struct got_error *error;
1551 FILE *f = NULL;
1552 struct got_object_id *id1 = NULL, *id2 = NULL;
1553 struct buf *diffbuf = NULL;
1554 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1555 *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1556 int obj_type;
1557 size_t newsize;
1559 f = got_opentemp();
1560 if (f == NULL)
1561 return NULL;
1563 error = buf_alloc(&diffbuf, 0);
1564 if (error)
1565 return NULL;
1567 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1568 if (error)
1569 goto done;
1571 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1572 error = got_repo_match_object_id(&id1, &label1,
1573 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1574 if (error)
1575 goto done;
1578 error = got_repo_match_object_id(&id2, &label2,
1579 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1580 if (error)
1581 goto done;
1583 error = got_object_get_type(&obj_type, header->repo, id2);
1584 if (error)
1585 goto done;
1586 switch (obj_type) {
1587 case GOT_OBJ_TYPE_BLOB:
1588 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1589 header->repo, f);
1590 break;
1591 case GOT_OBJ_TYPE_TREE:
1592 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1593 header->repo, f);
1594 break;
1595 case GOT_OBJ_TYPE_COMMIT:
1596 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1597 header->repo, f);
1598 break;
1599 default:
1600 error = got_error(GOT_ERR_OBJ_TYPE);
1603 if ((buf = calloc(128, sizeof(char *))) == NULL)
1604 goto done;
1606 fseek(f, 0, SEEK_SET);
1608 while ((fgets(buf, 2048, f)) != NULL) {
1609 n_buf = buf;
1610 while (*n_buf == '\n')
1611 n_buf++;
1612 newline = strchr(n_buf, '\n');
1613 if (newline)
1614 *newline = ' ';
1616 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1617 if (buf_color == NULL)
1618 continue;
1620 error = buf_puts(&newsize, diffbuf, buf_color);
1621 if (error)
1622 return NULL;
1624 error = buf_puts(&newsize, diffbuf, div_end);
1625 if (error)
1626 return NULL;
1629 if (buf_len(diffbuf) > 0) {
1630 error = buf_putc(diffbuf, '\0');
1631 diff_html = strdup(buf_get(diffbuf));
1633 done:
1634 fclose(f);
1635 free(buf_color);
1636 free(buf);
1637 free(diffbuf);
1638 free(label1);
1639 free(label2);
1640 free(id1);
1641 free(id2);
1643 if (error)
1644 return NULL;
1645 else
1646 return diff_html;
1649 static char *
1650 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1652 FILE *f;
1653 char *owner = NULL, *d_file = NULL;
1654 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1655 char *comp, *pos, *buf;
1656 unsigned int i;
1658 if (gw_trans->gw_conf->got_show_repo_owner == 0)
1659 goto err;
1661 if (asprintf(&d_file, "%s/config", dir) == -1)
1662 goto err;
1664 if ((f = fopen(d_file, "r")) == NULL)
1665 goto err;
1667 if ((buf = calloc(128, sizeof(char *))) == NULL)
1668 goto err;
1670 while ((fgets(buf, 128, f)) != NULL) {
1671 if ((pos = strstr(buf, gotweb)) != NULL)
1672 break;
1674 if ((pos = strstr(buf, gitweb)) != NULL)
1675 break;
1678 if (pos == NULL)
1679 goto err;
1681 do {
1682 fgets(buf, 128, f);
1683 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1685 if (comp == NULL)
1686 goto err;
1688 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1689 goto err;
1691 for (i = 0; i < 2; i++) {
1692 owner = strsep(&buf, "\"");
1695 if (owner == NULL)
1696 goto err;
1698 fclose(f);
1699 free(d_file);
1700 return owner;
1701 err:
1702 return strdup("");
1705 static char *
1706 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1708 FILE *f;
1709 char *url = NULL, *d_file = NULL;
1710 unsigned int len;
1712 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
1713 return NULL;
1715 if ((f = fopen(d_file, "r")) == NULL)
1716 return NULL;
1718 fseek(f, 0, SEEK_END);
1719 len = ftell(f) + 1;
1720 fseek(f, 0, SEEK_SET);
1722 if ((url = calloc(len, sizeof(char *))) == NULL)
1723 return NULL;
1725 fread(url, 1, len, f);
1726 fclose(f);
1727 free(d_file);
1728 return url;
1731 static char *
1732 gw_get_repo_tags(struct gw_trans *gw_trans, struct gw_header *header, int limit,
1733 int tag_type)
1735 const struct got_error *error = NULL;
1736 struct got_repository *repo = NULL;
1737 struct got_reflist_head refs;
1738 struct got_reflist_entry *re;
1739 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1740 *age = NULL;
1741 char *newline;
1742 struct buf *diffbuf = NULL;
1743 size_t newsize;
1745 SIMPLEQ_INIT(&refs);
1747 error = buf_alloc(&diffbuf, 0);
1748 if (error)
1749 return NULL;
1751 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1752 if (error)
1753 goto done;
1755 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1756 if (error)
1757 goto done;
1759 SIMPLEQ_FOREACH(re, &refs, entry) {
1760 const char *refname;
1761 char *refstr, *tag_commit0, *tag_commit, *id_str;
1762 const char *tagger;
1763 time_t tagger_time;
1764 struct got_object_id *id;
1765 struct got_tag_object *tag;
1767 refname = got_ref_get_name(re->ref);
1768 if (strncmp(refname, "refs/tags/", 10) != 0)
1769 continue;
1770 refname += 10;
1771 refstr = got_ref_to_str(re->ref);
1772 if (refstr == NULL) {
1773 error = got_error_from_errno("got_ref_to_str");
1774 goto done;
1777 error = got_ref_resolve(&id, repo, re->ref);
1778 if (error)
1779 goto done;
1780 error = got_object_open_as_tag(&tag, repo, id);
1781 free(id);
1782 if (error)
1783 goto done;
1785 tagger = got_object_tag_get_tagger(tag);
1786 tagger_time = got_object_tag_get_tagger_time(tag);
1788 error = got_object_id_str(&id_str,
1789 got_object_tag_get_object_id(tag));
1790 if (error)
1791 goto done;
1793 tag_commit0 = strdup(got_object_tag_get_message(tag));
1795 if (tag_commit0 == NULL) {
1796 error = got_error_from_errno("strdup");
1797 goto done;
1800 tag_commit = tag_commit0;
1801 while (*tag_commit == '\n')
1802 tag_commit++;
1804 switch (tag_type) {
1805 case TAGBRIEF:
1806 newline = strchr(tag_commit, '\n');
1807 if (newline)
1808 *newline = '\0';
1810 if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1811 TM_DIFF)) == -1) {
1812 error = got_error_from_errno("asprintf");
1813 goto done;
1816 if (asprintf(&tags_navs_disp, tags_navs,
1817 gw_trans->repo_name, id_str, gw_trans->repo_name,
1818 id_str, gw_trans->repo_name, id_str,
1819 gw_trans->repo_name, id_str) == -1) {
1820 error = got_error_from_errno("asprintf");
1821 goto done;
1824 if (asprintf(&tag_row, tags_row, age, refname,
1825 tag_commit, tags_navs_disp) == -1) {
1826 error = got_error_from_errno("asprintf");
1827 goto done;
1830 free(tags_navs_disp);
1831 break;
1832 case TAGFULL:
1833 if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1834 TM_LONG)) == -1) {
1835 error = got_error_from_errno("asprintf");
1836 goto done;
1838 if (asprintf(&tag_row, tag_info, age,
1839 gw_html_escape(tagger),
1840 gw_html_escape(tag_commit)) == -1) {
1841 error = got_error_from_errno("asprintf");
1842 goto done;
1844 break;
1845 default:
1846 break;
1849 got_object_tag_close(tag);
1851 error = buf_puts(&newsize, diffbuf, tag_row);
1853 free(id_str);
1854 free(refstr);
1855 free(age);
1856 free(tag_commit0);
1857 free(tag_row);
1859 if (error || (limit && --limit == 0))
1860 break;
1863 if (buf_len(diffbuf) > 0) {
1864 error = buf_putc(diffbuf, '\0');
1865 tags = strdup(buf_get(diffbuf));
1867 done:
1868 buf_free(diffbuf);
1869 got_ref_list_free(&refs);
1870 if (repo)
1871 got_repo_close(repo);
1872 if (error)
1873 return NULL;
1874 else
1875 return tags;
1878 static void
1879 gw_free_headers(struct gw_header *header)
1881 free(header->id);
1882 free(header->path);
1883 if (header->commit != NULL)
1884 got_object_commit_close(header->commit);
1885 if (header->repo)
1886 got_repo_close(header->repo);
1887 free(header->refs_str);
1888 free(header->commit_id);
1889 free(header->parent_id);
1890 free(header->tree_id);
1891 free(header->author);
1892 free(header->committer);
1893 free(header->commit_msg);
1896 static struct gw_header *
1897 gw_init_header()
1899 struct gw_header *header;
1901 header = malloc(sizeof(*header));
1902 if (header == NULL)
1903 return NULL;
1905 header->repo = NULL;
1906 header->commit = NULL;
1907 header->id = NULL;
1908 header->path = NULL;
1909 SIMPLEQ_INIT(&header->refs);
1911 return header;
1914 static const struct got_error *
1915 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1916 int limit)
1918 const struct got_error *error = NULL;
1919 struct got_commit_graph *graph = NULL;
1921 error = got_commit_graph_open(&graph, header->path, 0);
1922 if (error)
1923 goto done;
1925 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1926 NULL, NULL);
1927 if (error)
1928 goto done;
1930 for (;;) {
1931 error = got_commit_graph_iter_next(&header->id, graph,
1932 header->repo, NULL, NULL);
1933 if (error) {
1934 if (error->code == GOT_ERR_ITER_COMPLETED)
1935 error = NULL;
1936 goto done;
1938 if (header->id == NULL)
1939 goto done;
1941 error = got_object_open_as_commit(&header->commit, header->repo,
1942 header->id);
1943 if (error)
1944 goto done;
1946 error = gw_get_commit(gw_trans, header);
1947 if (limit > 1) {
1948 struct gw_header *n_header = NULL;
1949 if ((n_header = gw_init_header()) == NULL) {
1950 error = got_error_from_errno("malloc");
1951 goto done;
1954 n_header->refs_str = strdup(header->refs_str);
1955 n_header->commit_id = strdup(header->commit_id);
1956 n_header->parent_id = strdup(header->parent_id);
1957 n_header->tree_id = strdup(header->tree_id);
1958 n_header->author = strdup(header->author);
1959 n_header->committer = strdup(header->committer);
1960 n_header->commit_msg = strdup(header->commit_msg);
1961 n_header->committer_time = header->committer_time;
1962 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
1963 entry);
1965 if (error || (limit && --limit == 0))
1966 break;
1968 done:
1969 if (graph)
1970 got_commit_graph_close(graph);
1971 return error;
1974 static const struct got_error *
1975 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
1977 const struct got_error *error = NULL;
1978 struct got_reflist_entry *re;
1979 struct got_object_id *id2 = NULL;
1980 struct got_object_qid *parent_id;
1981 char *refs_str = NULL,
1982 *commit_msg = NULL, *commit_msg0;
1984 /*print commit*/
1985 SIMPLEQ_FOREACH(re, &header->refs, entry) {
1986 char *s;
1987 const char *name;
1988 struct got_tag_object *tag = NULL;
1989 int cmp;
1991 name = got_ref_get_name(re->ref);
1992 if (strcmp(name, GOT_REF_HEAD) == 0)
1993 continue;
1994 if (strncmp(name, "refs/", 5) == 0)
1995 name += 5;
1996 if (strncmp(name, "got/", 4) == 0)
1997 continue;
1998 if (strncmp(name, "heads/", 6) == 0)
1999 name += 6;
2000 if (strncmp(name, "remotes/", 8) == 0)
2001 name += 8;
2002 if (strncmp(name, "tags/", 5) == 0) {
2003 error = got_object_open_as_tag(&tag, header->repo,
2004 re->id);
2005 if (error) {
2006 if (error->code != GOT_ERR_OBJ_TYPE)
2007 continue;
2009 * Ref points at something other
2010 * than a tag.
2012 error = NULL;
2013 tag = NULL;
2016 cmp = got_object_id_cmp(tag ?
2017 got_object_tag_get_object_id(tag) : re->id, header->id);
2018 if (tag)
2019 got_object_tag_close(tag);
2020 if (cmp != 0)
2021 continue;
2022 s = refs_str;
2023 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2024 s ? ", " : "", name) == -1) {
2025 error = got_error_from_errno("asprintf");
2026 free(s);
2027 return error;
2029 header->refs_str = strdup(refs_str);
2030 free(s);
2033 if (refs_str == NULL)
2034 header->refs_str = strdup("");
2035 free(refs_str);
2037 error = got_object_id_str(&header->commit_id, header->id);
2038 if (error)
2039 return error;
2041 error = got_object_id_str(&header->tree_id,
2042 got_object_commit_get_tree_id(header->commit));
2043 if (error)
2044 return error;
2046 if (gw_trans->action == GW_DIFF) {
2047 parent_id = SIMPLEQ_FIRST(
2048 got_object_commit_get_parent_ids(header->commit));
2049 if (parent_id != NULL) {
2050 id2 = got_object_id_dup(parent_id->id);
2051 free (parent_id);
2052 error = got_object_id_str(&header->parent_id, id2);
2053 if (error)
2054 return error;
2055 free(id2);
2056 } else
2057 header->parent_id = strdup("/dev/null");
2058 } else
2059 header->parent_id = strdup("");
2061 header->committer_time =
2062 got_object_commit_get_committer_time(header->commit);
2064 if (gw_trans->action != GW_BRIEFS && gw_trans->action != GW_SUMMARY) {
2065 header->author = strdup(
2066 gw_html_escape(got_object_commit_get_author(header->commit))
2068 } else {
2069 header->author = strdup(
2070 got_object_commit_get_author(header->commit)
2074 header->committer = strdup(
2075 gw_html_escape(got_object_commit_get_committer(header->commit))
2078 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2079 if (error)
2080 return error;
2082 commit_msg = commit_msg0;
2083 while (*commit_msg == '\n')
2084 commit_msg++;
2086 header->commit_msg = strdup(commit_msg);
2087 free(commit_msg0);
2088 return error;
2091 static const struct got_error *
2092 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2094 const struct got_error *error = NULL;
2095 char *in_repo_path = NULL;
2097 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2098 if (error)
2099 return error;
2101 if (gw_trans->commit == NULL) {
2102 struct got_reference *head_ref;
2103 error = got_ref_open(&head_ref, header->repo,
2104 gw_trans->headref, 0);
2105 if (error)
2106 return error;
2108 error = got_ref_resolve(&header->id, header->repo, head_ref);
2109 got_ref_close(head_ref);
2110 if (error)
2111 return error;
2113 error = got_object_open_as_commit(&header->commit,
2114 header->repo, header->id);
2115 } else {
2116 struct got_reference *ref;
2117 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2118 if (error == NULL) {
2119 int obj_type;
2120 error = got_ref_resolve(&header->id, header->repo, ref);
2121 got_ref_close(ref);
2122 if (error)
2123 return error;
2124 error = got_object_get_type(&obj_type, header->repo,
2125 header->id);
2126 if (error)
2127 return error;
2128 if (obj_type == GOT_OBJ_TYPE_TAG) {
2129 struct got_tag_object *tag;
2130 error = got_object_open_as_tag(&tag,
2131 header->repo, header->id);
2132 if (error)
2133 return error;
2134 if (got_object_tag_get_object_type(tag) !=
2135 GOT_OBJ_TYPE_COMMIT) {
2136 got_object_tag_close(tag);
2137 error = got_error(GOT_ERR_OBJ_TYPE);
2138 return error;
2140 free(header->id);
2141 header->id = got_object_id_dup(
2142 got_object_tag_get_object_id(tag));
2143 if (header->id == NULL)
2144 error = got_error_from_errno(
2145 "got_object_id_dup");
2146 got_object_tag_close(tag);
2147 if (error)
2148 return error;
2149 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2150 error = got_error(GOT_ERR_OBJ_TYPE);
2151 return error;
2153 error = got_object_open_as_commit(&header->commit,
2154 header->repo, header->id);
2155 if (error)
2156 return error;
2158 if (header->commit == NULL) {
2159 error = got_repo_match_object_id_prefix(&header->id,
2160 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2161 header->repo);
2162 if (error)
2163 return error;
2165 error = got_repo_match_object_id_prefix(&header->id,
2166 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2167 header->repo);
2170 error = got_repo_map_path(&in_repo_path, header->repo,
2171 gw_trans->repo_path, 1);
2172 if (error)
2173 return error;
2175 if (in_repo_path) {
2176 header->path = strdup(in_repo_path);
2178 free(in_repo_path);
2180 error = got_ref_list(&header->refs, header->repo, NULL,
2181 got_ref_cmp_by_name, NULL);
2182 if (error)
2183 return error;
2185 error = gw_get_commits(gw_trans, header, limit);
2186 return error;
2189 struct blame_line {
2190 int annotated;
2191 char *id_str;
2192 char *committer;
2193 char datebuf[11]; /* YYYY-MM-DD + NUL */
2196 struct gw_blame_cb_args {
2197 struct blame_line *lines;
2198 int nlines;
2199 int nlines_prec;
2200 int lineno_cur;
2201 off_t *line_offsets;
2202 FILE *f;
2203 struct got_repository *repo;
2204 struct gw_trans *gw_trans;
2205 struct buf *blamebuf;
2208 static const struct got_error *
2209 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2211 const struct got_error *err = NULL;
2212 struct gw_blame_cb_args *a = arg;
2213 struct blame_line *bline;
2214 char *line = NULL;
2215 size_t linesize = 0, newsize;
2216 struct got_commit_object *commit = NULL;
2217 off_t offset;
2218 struct tm tm;
2219 time_t committer_time;
2221 if (nlines != a->nlines ||
2222 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2223 return got_error(GOT_ERR_RANGE);
2225 if (lineno == -1)
2226 return NULL; /* no change in this commit */
2228 /* Annotate this line. */
2229 bline = &a->lines[lineno - 1];
2230 if (bline->annotated)
2231 return NULL;
2232 err = got_object_id_str(&bline->id_str, id);
2233 if (err)
2234 return err;
2236 err = got_object_open_as_commit(&commit, a->repo, id);
2237 if (err)
2238 goto done;
2240 bline->committer = strdup(got_object_commit_get_committer(commit));
2241 if (bline->committer == NULL) {
2242 err = got_error_from_errno("strdup");
2243 goto done;
2246 committer_time = got_object_commit_get_committer_time(commit);
2247 if (localtime_r(&committer_time, &tm) == NULL)
2248 return got_error_from_errno("localtime_r");
2249 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2250 &tm) >= sizeof(bline->datebuf)) {
2251 err = got_error(GOT_ERR_NO_SPACE);
2252 goto done;
2254 bline->annotated = 1;
2256 /* Print lines annotated so far. */
2257 bline = &a->lines[a->lineno_cur - 1];
2258 if (!bline->annotated)
2259 goto done;
2261 offset = a->line_offsets[a->lineno_cur - 1];
2262 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2263 err = got_error_from_errno("fseeko");
2264 goto done;
2267 while (bline->annotated) {
2268 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2269 *line_escape = NULL;
2270 size_t len;
2272 if (getline(&line, &linesize, a->f) == -1) {
2273 if (ferror(a->f))
2274 err = got_error_from_errno("getline");
2275 break;
2278 committer = bline->committer;
2279 smallerthan = strchr(committer, '<');
2280 if (smallerthan && smallerthan[1] != '\0')
2281 committer = smallerthan + 1;
2282 at = strchr(committer, '@');
2283 if (at)
2284 *at = '\0';
2285 len = strlen(committer);
2286 if (len >= 9)
2287 committer[8] = '\0';
2289 nl = strchr(line, '\n');
2290 if (nl)
2291 *nl = '\0';
2293 if (strcmp(line, "") != 0)
2294 line_escape = strdup(gw_html_escape(line));
2295 else
2296 line_escape = strdup("");
2298 asprintf(&blame_row, blame_line, a->nlines_prec,
2299 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2300 line_escape);
2301 a->lineno_cur++;
2302 err = buf_puts(&newsize, a->blamebuf, blame_row);
2303 if (err)
2304 return err;
2306 bline = &a->lines[a->lineno_cur - 1];
2307 free(line_escape);
2308 free(blame_row);
2310 done:
2311 if (commit)
2312 got_object_commit_close(commit);
2313 free(line);
2314 return err;
2317 static char*
2318 gw_get_file_blame(struct gw_trans *gw_trans)
2320 const struct got_error *error = NULL;
2321 struct got_repository *repo = NULL;
2322 struct got_object_id *obj_id = NULL;
2323 struct got_object_id *commit_id = NULL;
2324 struct got_blob_object *blob = NULL;
2325 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2326 *folder = NULL;
2327 struct gw_blame_cb_args bca;
2328 int i, obj_type;
2329 size_t filesize;
2331 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2332 if (error)
2333 goto done;
2335 if (gw_trans->repo_folder != NULL) {
2336 if (asprintf(&folder, "%s/", gw_trans->repo_folder) == -1) {
2337 error = got_error_from_errno("asprintf");
2338 goto done;
2340 } else
2341 folder = strdup("");
2343 if (asprintf(&path, "%s%s", folder, gw_trans->repo_file) == -1) {
2344 error = got_error_from_errno("asprintf");
2345 goto done;
2347 free(folder);
2349 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2350 if (error)
2351 goto done;
2353 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2354 GOT_OBJ_TYPE_COMMIT, 1, repo);
2355 if (error)
2356 goto done;
2358 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2359 if (error)
2360 goto done;
2362 if (obj_id == NULL) {
2363 error = got_error(GOT_ERR_NO_OBJ);
2364 goto done;
2367 error = got_object_get_type(&obj_type, repo, obj_id);
2368 if (error)
2369 goto done;
2371 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2372 error = got_error(GOT_ERR_OBJ_TYPE);
2373 goto done;
2376 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2377 if (error)
2378 goto done;
2380 error = buf_alloc(&bca.blamebuf, 0);
2381 if (error)
2382 goto done;
2384 bca.f = got_opentemp();
2385 if (bca.f == NULL) {
2386 error = got_error_from_errno("got_opentemp");
2387 goto done;
2389 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2390 &bca.line_offsets, bca.f, blob);
2391 if (error || bca.nlines == 0)
2392 goto done;
2394 /* Don't include \n at EOF in the blame line count. */
2395 if (bca.line_offsets[bca.nlines - 1] == filesize)
2396 bca.nlines--;
2398 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2399 if (bca.lines == NULL) {
2400 error = got_error_from_errno("calloc");
2401 goto done;
2403 bca.lineno_cur = 1;
2404 bca.nlines_prec = 0;
2405 i = bca.nlines;
2406 while (i > 0) {
2407 i /= 10;
2408 bca.nlines_prec++;
2410 bca.repo = repo;
2411 bca.gw_trans = gw_trans;
2413 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2414 NULL, NULL);
2415 if (buf_len(bca.blamebuf) > 0) {
2416 error = buf_putc(bca.blamebuf, '\0');
2417 blame_html = strdup(buf_get(bca.blamebuf));
2419 done:
2420 free(bca.blamebuf);
2421 free(in_repo_path);
2422 free(commit_id);
2423 free(obj_id);
2424 free(path);
2426 if (blob)
2427 error = got_object_blob_close(blob);
2428 if (repo)
2429 error = got_repo_close(repo);
2430 if (error)
2431 return NULL;
2432 if (bca.lines) {
2433 for (i = 0; i < bca.nlines; i++) {
2434 struct blame_line *bline = &bca.lines[i];
2435 free(bline->id_str);
2436 free(bline->committer);
2438 free(bca.lines);
2440 free(bca.line_offsets);
2441 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2442 error = got_error_from_errno("fclose");
2443 if (error)
2444 return NULL;
2445 else
2446 return blame_html;
2449 static char*
2450 gw_get_repo_tree(struct gw_trans *gw_trans)
2452 const struct got_error *error = NULL;
2453 struct got_repository *repo = NULL;
2454 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2455 struct got_tree_object *tree = NULL;
2456 struct buf *diffbuf = NULL;
2457 size_t newsize;
2458 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2459 *tree_row = NULL, *id_str, *class = NULL;
2460 int nentries, i, class_flip = 0;
2462 error = buf_alloc(&diffbuf, 0);
2463 if (error)
2464 return NULL;
2466 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2467 if (error)
2468 goto done;
2470 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2471 if (error)
2472 goto done;
2474 if (gw_trans->repo_folder != NULL)
2475 path = strdup(gw_trans->repo_folder);
2476 else if (in_repo_path) {
2477 free(path);
2478 path = in_repo_path;
2481 if (gw_trans->commit == NULL) {
2482 struct got_reference *head_ref;
2483 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2484 if (error)
2485 goto done;
2487 error = got_ref_resolve(&commit_id, repo, head_ref);
2488 got_ref_close(head_ref);
2490 } else
2491 error = got_repo_match_object_id(&commit_id, NULL,
2492 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2493 if (error)
2494 goto done;
2496 error = got_object_id_str(&gw_trans->commit, commit_id);
2497 if (error)
2498 goto done;
2500 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2501 if (error)
2502 goto done;
2504 error = got_object_open_as_tree(&tree, repo, tree_id);
2505 if (error)
2506 goto done;
2508 nentries = got_object_tree_get_nentries(tree);
2510 for (i = 0; i < nentries; i++) {
2511 struct got_tree_entry *te;
2512 const char *modestr = "";
2513 char *id = NULL, *url_html = NULL;
2515 te = got_object_tree_get_entry(tree, i);
2517 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2518 if (error)
2519 goto done;
2521 if (asprintf(&id, "%s", id_str) == -1) {
2522 error = got_error_from_errno("asprintf");
2523 free(id_str);
2524 goto done;
2527 mode_t mode = got_tree_entry_get_mode(te);
2529 if (got_object_tree_entry_is_submodule(te))
2530 modestr = "$";
2531 else if (S_ISLNK(mode))
2532 modestr = "@";
2533 else if (S_ISDIR(mode))
2534 modestr = "/";
2535 else if (mode & S_IXUSR)
2536 modestr = "*";
2538 if (class_flip == 0) {
2539 class = strdup("back_lightgray");
2540 class_flip = 1;
2541 } else {
2542 class = strdup("back_white");
2543 class_flip = 0;
2546 char *build_folder = NULL;
2547 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2548 if (gw_trans->repo_folder != NULL) {
2549 if (asprintf(&build_folder, "%s/%s",
2550 gw_trans->repo_folder,
2551 got_tree_entry_get_name(te)) == -1) {
2552 error =
2553 got_error_from_errno("asprintf");
2554 goto done;
2556 } else {
2557 if (asprintf(&build_folder, "%s",
2558 got_tree_entry_get_name(te)) == -1)
2559 goto done;
2562 if (asprintf(&url_html, folder_html,
2563 gw_trans->repo_name, gw_trans->action_name,
2564 gw_trans->commit, build_folder,
2565 got_tree_entry_get_name(te), modestr) == -1) {
2566 error = got_error_from_errno("asprintf");
2567 goto done;
2569 } else {
2570 if (gw_trans->repo_folder != NULL) {
2571 if (asprintf(&build_folder, "%s",
2572 gw_trans->repo_folder) == -1) {
2573 error =
2574 got_error_from_errno("asprintf");
2575 goto done;
2577 } else
2578 build_folder = strdup("");
2580 if (asprintf(&url_html, file_html, gw_trans->repo_name,
2581 "blame", gw_trans->commit,
2582 got_tree_entry_get_name(te), build_folder,
2583 got_tree_entry_get_name(te), modestr) == -1) {
2584 error = got_error_from_errno("asprintf");
2585 goto done;
2588 free(build_folder);
2590 if (error)
2591 goto done;
2593 if ((asprintf(&tree_row, tree_line, class, url_html)) == -1) {
2594 error = got_error_from_errno("asprintf");
2595 goto done;
2597 error = buf_puts(&newsize, diffbuf, tree_row);
2598 if (error)
2599 goto done;
2601 free(id);
2602 free(id_str);
2603 free(url_html);
2604 free(tree_row);
2607 if (buf_len(diffbuf) > 0) {
2608 error = buf_putc(diffbuf, '\0');
2609 tree_html = strdup(buf_get(diffbuf));
2611 done:
2612 if (tree)
2613 got_object_tree_close(tree);
2614 if (repo)
2615 got_repo_close(repo);
2617 free(in_repo_path);
2618 free(tree_id);
2619 free(diffbuf);
2620 if (error)
2621 return NULL;
2622 else
2623 return tree_html;
2626 static char *
2627 gw_get_repo_heads(struct gw_trans *gw_trans)
2629 const struct got_error *error = NULL;
2630 struct got_repository *repo = NULL;
2631 struct got_reflist_head refs;
2632 struct got_reflist_entry *re;
2633 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2634 struct buf *diffbuf = NULL;
2635 size_t newsize;
2637 SIMPLEQ_INIT(&refs);
2639 error = buf_alloc(&diffbuf, 0);
2640 if (error)
2641 return NULL;
2643 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2644 if (error)
2645 goto done;
2647 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2648 NULL);
2649 if (error)
2650 goto done;
2652 SIMPLEQ_FOREACH(re, &refs, entry) {
2653 char *refname;
2655 refname = strdup(got_ref_get_name(re->ref));
2656 if (refname == NULL) {
2657 error = got_error_from_errno("got_ref_to_str");
2658 goto done;
2661 if (strncmp(refname, "refs/heads/", 11) != 0) {
2662 free(refname);
2663 continue;
2666 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path, refname,
2667 TM_DIFF);
2668 if (error)
2669 goto done;
2671 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2672 refname, gw_trans->repo_name, refname,
2673 gw_trans->repo_name, refname, gw_trans->repo_name,
2674 refname) == -1) {
2675 error = got_error_from_errno("asprintf");
2676 goto done;
2679 if (strncmp(refname, "refs/heads/", 11) == 0)
2680 refname += 11;
2682 if (asprintf(&head_row, heads_row, age, refname,
2683 head_navs_disp) == -1) {
2684 error = got_error_from_errno("asprintf");
2685 goto done;
2688 error = buf_puts(&newsize, diffbuf, head_row);
2690 free(head_navs_disp);
2691 free(head_row);
2694 if (buf_len(diffbuf) > 0) {
2695 error = buf_putc(diffbuf, '\0');
2696 heads = strdup(buf_get(diffbuf));
2698 done:
2699 buf_free(diffbuf);
2700 got_ref_list_free(&refs);
2701 if (repo)
2702 got_repo_close(repo);
2703 if (error)
2704 return NULL;
2705 else
2706 return heads;
2709 static char *
2710 gw_get_got_link(struct gw_trans *gw_trans)
2712 char *link;
2714 if (asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2715 gw_trans->gw_conf->got_logo) == -1)
2716 return NULL;
2718 return link;
2721 static char *
2722 gw_get_site_link(struct gw_trans *gw_trans)
2724 char *link, *repo = "", *action = "";
2726 if (gw_trans->repo_name != NULL)
2727 if (asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2728 "</a>", gw_trans->repo_name, gw_trans->repo_name) == -1)
2729 return NULL;
2731 if (gw_trans->action_name != NULL)
2732 if (asprintf(&action, " / %s", gw_trans->action_name) == -1)
2733 return NULL;
2735 if (asprintf(&link, site_link, GOTWEB,
2736 gw_trans->gw_conf->got_site_link, repo, action) == -1)
2737 return NULL;
2739 return link;
2742 static char *
2743 gw_colordiff_line(char *buf)
2745 const struct got_error *error = NULL;
2746 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2747 struct buf *diffbuf = NULL;
2748 size_t newsize;
2750 error = buf_alloc(&diffbuf, 0);
2751 if (error)
2752 return NULL;
2754 if (buf == NULL)
2755 return NULL;
2756 if (strncmp(buf, "-", 1) == 0)
2757 color = "diff_minus";
2758 if (strncmp(buf, "+", 1) == 0)
2759 color = "diff_plus";
2760 if (strncmp(buf, "@@", 2) == 0)
2761 color = "diff_chunk_header";
2762 if (strncmp(buf, "@@", 2) == 0)
2763 color = "diff_chunk_header";
2764 if (strncmp(buf, "commit +", 8) == 0)
2765 color = "diff_meta";
2766 if (strncmp(buf, "commit -", 8) == 0)
2767 color = "diff_meta";
2768 if (strncmp(buf, "blob +", 6) == 0)
2769 color = "diff_meta";
2770 if (strncmp(buf, "blob -", 6) == 0)
2771 color = "diff_meta";
2772 if (strncmp(buf, "file +", 6) == 0)
2773 color = "diff_meta";
2774 if (strncmp(buf, "file -", 6) == 0)
2775 color = "diff_meta";
2776 if (strncmp(buf, "from:", 5) == 0)
2777 color = "diff_author";
2778 if (strncmp(buf, "via:", 4) == 0)
2779 color = "diff_author";
2780 if (strncmp(buf, "date:", 5) == 0)
2781 color = "diff_date";
2783 if (asprintf(&div_diff_line_div, div_diff_line, color) == -1)
2784 return NULL;
2786 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2787 if (error)
2788 return NULL;
2790 error = buf_puts(&newsize, diffbuf, buf);
2791 if (error)
2792 return NULL;
2794 if (buf_len(diffbuf) > 0) {
2795 error = buf_putc(diffbuf, '\0');
2796 colorized_line = strdup(buf_get(diffbuf));
2799 free(diffbuf);
2800 free(div_diff_line_div);
2801 return colorized_line;
2804 static char *
2805 gw_html_escape(const char *html)
2807 char *escaped_str = NULL, *buf;
2808 char c[1];
2809 size_t sz, i, buff_sz = 2048;
2811 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2812 return NULL;
2814 if (html == NULL)
2815 return NULL;
2816 else
2817 if ((sz = strlen(html)) == 0)
2818 return NULL;
2820 /* only work with buff_sz */
2821 if (buff_sz < sz)
2822 sz = buff_sz;
2824 for (i = 0; i < sz; i++) {
2825 c[0] = html[i];
2826 switch (c[0]) {
2827 case ('>'):
2828 strcat(buf, "&gt;");
2829 break;
2830 case ('&'):
2831 strcat(buf, "&amp;");
2832 break;
2833 case ('<'):
2834 strcat(buf, "&lt;");
2835 break;
2836 case ('"'):
2837 strcat(buf, "&quot;");
2838 break;
2839 case ('\''):
2840 strcat(buf, "&apos;");
2841 break;
2842 case ('\n'):
2843 strcat(buf, "<br />");
2844 default:
2845 strcat(buf, &c[0]);
2846 break;
2849 asprintf(&escaped_str, "%s", buf);
2850 free(buf);
2851 return escaped_str;
2854 int
2855 main(int argc, char *argv[])
2857 const struct got_error *error = NULL;
2858 struct gw_trans *gw_trans;
2859 struct gw_dir *dir = NULL, *tdir;
2860 const char *page = "index";
2861 int gw_malloc = 1;
2862 enum kcgi_err kerr;
2864 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2865 errx(1, "malloc");
2867 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2868 errx(1, "malloc");
2870 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2871 errx(1, "malloc");
2873 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2874 errx(1, "malloc");
2876 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2877 if (kerr != KCGI_OK) {
2878 error = gw_kcgi_error(kerr);
2879 goto done;
2882 if ((gw_trans->gw_conf =
2883 malloc(sizeof(struct gotweb_conf))) == NULL) {
2884 gw_malloc = 0;
2885 error = got_error_from_errno("malloc");
2886 goto done;
2889 TAILQ_INIT(&gw_trans->gw_dirs);
2890 TAILQ_INIT(&gw_trans->gw_headers);
2892 gw_trans->page = 0;
2893 gw_trans->repos_total = 0;
2894 gw_trans->repo_path = NULL;
2895 gw_trans->commit = NULL;
2896 gw_trans->headref = strdup(GOT_REF_HEAD);
2897 gw_trans->mime = KMIME_TEXT_HTML;
2898 gw_trans->gw_tmpl->key = gw_templs;
2899 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2900 gw_trans->gw_tmpl->arg = gw_trans;
2901 gw_trans->gw_tmpl->cb = gw_template;
2902 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2903 if (error)
2904 goto done;
2906 error = gw_parse_querystring(gw_trans);
2907 if (error)
2908 goto done;
2910 error = gw_display_index(gw_trans);
2911 done:
2912 if (error) {
2913 gw_trans->mime = KMIME_TEXT_PLAIN;
2914 gw_trans->action = GW_ERR;
2915 gw_display_error(gw_trans, error);
2917 if (gw_malloc) {
2918 free(gw_trans->gw_conf->got_repos_path);
2919 free(gw_trans->gw_conf->got_site_name);
2920 free(gw_trans->gw_conf->got_site_owner);
2921 free(gw_trans->gw_conf->got_site_link);
2922 free(gw_trans->gw_conf->got_logo);
2923 free(gw_trans->gw_conf->got_logo_url);
2924 free(gw_trans->gw_conf);
2925 free(gw_trans->commit);
2926 free(gw_trans->repo_path);
2927 free(gw_trans->repo_name);
2928 free(gw_trans->repo_file);
2929 free(gw_trans->action_name);
2930 free(gw_trans->headref);
2932 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2933 free(dir->name);
2934 free(dir->description);
2935 free(dir->age);
2936 free(dir->url);
2937 free(dir->path);
2938 free(dir);
2943 khttp_free(gw_trans->gw_req);
2944 return 0;