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 <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 gw_trans {
58 TAILQ_HEAD(headers, gw_header) gw_headers;
59 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
60 struct gw_dir *gw_dir;
61 struct gotweb_conf *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
64 struct kreq *gw_req;
65 char *repo_name;
66 char *repo_path;
67 char *commit;
68 char *repo_file;
69 char *repo_folder;
70 char *action_name;
71 char *headref;
72 unsigned int action;
73 unsigned int page;
74 unsigned int repos_total;
75 enum kmime mime;
76 };
78 struct gw_header {
79 TAILQ_ENTRY(gw_header) entry;
80 struct got_repository *repo;
81 struct got_reflist_head refs;
82 struct got_commit_object *commit;
83 struct got_object_id *id;
84 char *path;
86 char *refs_str;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
89 char *tree_id;
90 char *author;
91 char *committer;
92 char *commit_msg;
93 time_t committer_time;
94 };
96 struct gw_dir {
97 TAILQ_ENTRY(gw_dir) entry;
98 char *name;
99 char *owner;
100 char *description;
101 char *url;
102 char *age;
103 char *path;
104 };
106 enum gw_key {
107 KEY_ACTION,
108 KEY_COMMIT_ID,
109 KEY_FILE,
110 KEY_FOLDER,
111 KEY_HEADREF,
112 KEY_PAGE,
113 KEY_PATH,
114 KEY__ZMAX
115 };
117 enum gw_tmpl {
118 TEMPL_CONTENT,
119 TEMPL_HEAD,
120 TEMPL_HEADER,
121 TEMPL_SEARCH,
122 TEMPL_SITEPATH,
123 TEMPL_SITEOWNER,
124 TEMPL_TITLE,
125 TEMPL__MAX
126 };
128 enum gw_ref_tm {
129 TM_DIFF,
130 TM_LONG,
131 };
133 enum gw_tags {
134 TAGBRIEF,
135 TAGFULL,
136 };
138 static const char *const gw_templs[TEMPL__MAX] = {
139 "content",
140 "head",
141 "header",
142 "search",
143 "sitepath",
144 "siteowner",
145 "title",
146 };
148 static const struct kvalid gw_keys[KEY__ZMAX] = {
149 { kvalid_stringne, "action" },
150 { kvalid_stringne, "commit" },
151 { kvalid_stringne, "file" },
152 { kvalid_stringne, "folder" },
153 { kvalid_stringne, "headref" },
154 { kvalid_int, "page" },
155 { kvalid_stringne, "path" },
156 };
158 static struct gw_dir *gw_init_gw_dir(char *);
159 static struct gw_header *gw_init_header(void);
161 static char *gw_get_repo_description(struct gw_trans *,
162 char *);
163 static char *gw_get_repo_owner(struct gw_trans *,
164 char *);
165 static char *gw_get_time_str(time_t, int);
166 static char *gw_get_repo_age(struct gw_trans *,
167 char *, char *, int);
168 static char *gw_get_file_blame(struct gw_trans *);
169 static char *gw_get_repo_tree(struct gw_trans *);
170 static char *gw_get_diff(struct gw_trans *,
171 struct gw_header *);
172 static char *gw_get_repo_tags(struct gw_trans *, 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 const struct got_error *);
194 static int gw_template(size_t, void *);
196 static const struct got_error* gw_get_header(struct gw_trans *,
197 struct gw_header *, int);
198 static const struct got_error* gw_get_commits(struct gw_trans *,
199 struct gw_header *, int);
200 static const struct got_error* gw_get_commit(struct gw_trans *,
201 struct gw_header *);
202 static const struct got_error* gw_apply_unveil(const char *, const char *);
203 static const struct got_error* gw_blame_cb(void *, int, int,
204 struct got_object_id *);
205 static const struct got_error* gw_load_got_paths(struct gw_trans *);
206 static const struct got_error* gw_load_got_path(struct gw_trans *,
207 struct gw_dir *);
208 static const struct got_error* gw_parse_querystring(struct gw_trans *);
210 static const struct got_error* gw_blame(struct gw_trans *);
211 static const struct got_error* gw_diff(struct gw_trans *);
212 static const struct got_error* gw_index(struct gw_trans *);
213 static const struct got_error* gw_commits(struct gw_trans *);
214 static const struct got_error* gw_briefs(struct gw_trans *);
215 static const struct got_error* gw_summary(struct gw_trans *);
216 static const struct got_error* gw_tree(struct gw_trans *);
218 struct gw_query_action {
219 unsigned int func_id;
220 const char *func_name;
221 const struct got_error *(*func_main)(struct gw_trans *);
222 char *template;
223 };
225 enum gw_query_actions {
226 GW_BLAME,
227 GW_BRIEFS,
228 GW_COMMITS,
229 GW_DIFF,
230 GW_ERR,
231 GW_INDEX,
232 GW_SUMMARY,
233 GW_TREE,
234 };
236 static struct gw_query_action gw_query_funcs[] = {
237 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
238 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
239 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
240 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
241 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
242 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
243 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
244 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
245 };
247 static const struct got_error *
248 gw_kcgi_error(enum kcgi_err kerr)
250 if (kerr == KCGI_OK)
251 return NULL;
253 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
254 return got_error(GOT_ERR_CANCELLED);
256 if (kerr == KCGI_ENOMEM)
257 return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
259 if (kerr == KCGI_ENFILE)
260 return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
262 if (kerr == KCGI_EAGAIN)
263 return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
265 if (kerr == KCGI_FORM)
266 return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
268 return got_error_from_errno(kcgi_strerror(kerr));
271 static const struct got_error *
272 gw_apply_unveil(const char *repo_path, const char *repo_file)
274 const struct got_error *err;
276 if (repo_path && repo_file) {
277 char *full_path;
278 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
279 return got_error_from_errno("asprintf unveil");
280 if (unveil(full_path, "r") != 0)
281 return got_error_from_errno2("unveil", full_path);
284 if (repo_path && unveil(repo_path, "r") != 0)
285 return got_error_from_errno2("unveil", repo_path);
287 if (unveil("/tmp", "rwc") != 0)
288 return got_error_from_errno2("unveil", "/tmp");
290 err = got_privsep_unveil_exec_helpers();
291 if (err != NULL)
292 return err;
294 if (unveil(NULL, NULL) != 0)
295 return got_error_from_errno("unveil");
297 return NULL;
300 static const struct got_error *
301 gw_blame(struct gw_trans *gw_trans)
303 const struct got_error *error = NULL;
304 struct gw_header *header = NULL;
305 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
306 enum kcgi_err kerr;
308 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
309 NULL) == -1)
310 return got_error_from_errno("pledge");
312 if ((header = gw_init_header()) == NULL)
313 return got_error_from_errno("malloc");
315 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
316 if (error)
317 return error;
319 error = gw_get_header(gw_trans, header, 1);
320 if (error)
321 return error;
323 blame_html = gw_get_file_blame(gw_trans);
325 if (blame_html == NULL) {
326 blame_html = strdup("");
327 if (blame_html == NULL)
328 return got_error_from_errno("strdup");
331 if ((asprintf(&blame_html_disp, blame_header,
332 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
333 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
334 blame_html)) == -1) {
335 error = got_error_from_errno("asprintf");
336 goto done;
339 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
340 error = got_error_from_errno("asprintf");
341 goto done;
344 kerr = khttp_puts(gw_trans->gw_req, blame);
345 if (kerr != KCGI_OK)
346 error = gw_kcgi_error(kerr);
347 done:
348 got_ref_list_free(&header->refs);
349 gw_free_headers(header);
350 free(blame_html_disp);
351 free(blame_html);
352 free(blame);
353 return error;
356 static const struct got_error *
357 gw_diff(struct gw_trans *gw_trans)
359 const struct got_error *error = NULL;
360 struct gw_header *header = NULL;
361 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
362 enum kcgi_err kerr;
364 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
365 NULL) == -1)
366 return got_error_from_errno("pledge");
368 if ((header = gw_init_header()) == NULL)
369 return got_error_from_errno("malloc");
371 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
372 if (error)
373 goto done;
375 error = gw_get_header(gw_trans, header, 1);
376 if (error)
377 goto done;
379 diff_html = gw_get_diff(gw_trans, header);
381 if (diff_html == NULL) {
382 diff_html = strdup("");
383 if (diff_html == NULL) {
384 error = got_error_from_errno("strdup");
385 goto done;
389 if (asprintf(&diff_html_disp, diff_header,
390 gw_gen_diff_header(header->parent_id, header->commit_id),
391 gw_gen_commit_header(header->commit_id, header->refs_str),
392 gw_gen_tree_header(header->tree_id),
393 gw_gen_author_header(header->author),
394 gw_gen_committer_header(header->committer),
395 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
396 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
397 diff_html) == -1) {
398 error = got_error_from_errno("asprintf");
399 goto done;
402 if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
403 error = got_error_from_errno("asprintf");
404 goto done;
407 kerr = khttp_puts(gw_trans->gw_req, diff);
408 if (kerr != KCGI_OK)
409 error = gw_kcgi_error(kerr);
410 done:
411 got_ref_list_free(&header->refs);
412 gw_free_headers(header);
413 free(diff_html_disp);
414 free(diff_html);
415 free(diff);
416 return error;
419 static const struct got_error *
420 gw_index(struct gw_trans *gw_trans)
422 const struct got_error *error = NULL;
423 struct gw_dir *gw_dir = NULL;
424 char *html, *navs, *next, *prev;
425 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
426 enum kcgi_err kerr;
428 if (pledge("stdio rpath proc exec sendfd unveil",
429 NULL) == -1) {
430 error = got_error_from_errno("pledge");
431 return error;
434 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
435 if (error)
436 return error;
438 error = gw_load_got_paths(gw_trans);
439 if (error)
440 return error;
442 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
443 if (kerr != KCGI_OK)
444 return gw_kcgi_error(kerr);
446 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
447 if (asprintf(&html, index_projects_empty,
448 gw_trans->gw_conf->got_repos_path) == -1)
449 return got_error_from_errno("asprintf");
450 kerr = khttp_puts(gw_trans->gw_req, html);
451 if (kerr != KCGI_OK)
452 error = gw_kcgi_error(kerr);
453 free(html);
454 return error;
457 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
458 dir_c++;
460 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
461 if (gw_trans->page > 0 && (gw_trans->page *
462 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
463 prev_disp++;
464 continue;
467 prev_disp++;
469 if (error)
470 return error;
471 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
472 gw_dir->name, gw_dir->name)) == -1)
473 return got_error_from_errno("asprintf");
475 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
476 gw_dir->description, gw_dir->owner, gw_dir->age,
477 navs)) == -1)
478 return got_error_from_errno("asprintf");
480 kerr = khttp_puts(gw_trans->gw_req, html);
481 free(navs);
482 free(html);
483 if (kerr != KCGI_OK)
484 return gw_kcgi_error(kerr);
486 if (gw_trans->gw_conf->got_max_repos_display == 0)
487 continue;
489 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
490 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
491 if (kerr != KCGI_OK)
492 return gw_kcgi_error(kerr);
493 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
494 (gw_trans->page > 0) &&
495 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
496 prev_disp == gw_trans->repos_total)) {
497 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
498 if (kerr != KCGI_OK)
499 return gw_kcgi_error(kerr);
502 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
503 (gw_trans->page > 0) &&
504 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
505 prev_disp == gw_trans->repos_total)) {
506 if ((asprintf(&prev, nav_prev,
507 gw_trans->page - 1)) == -1)
508 return got_error_from_errno("asprintf");
509 kerr = khttp_puts(gw_trans->gw_req, prev);
510 free(prev);
511 if (kerr != KCGI_OK)
512 return gw_kcgi_error(kerr);
515 kerr = khttp_puts(gw_trans->gw_req, div_end);
516 if (kerr != KCGI_OK)
517 return gw_kcgi_error(kerr);
519 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
520 next_disp == gw_trans->gw_conf->got_max_repos_display &&
521 dir_c != (gw_trans->page + 1) *
522 gw_trans->gw_conf->got_max_repos_display) {
523 if ((asprintf(&next, nav_next,
524 gw_trans->page + 1)) == -1)
525 return got_error_from_errno("calloc");
526 kerr = khttp_puts(gw_trans->gw_req, next);
527 free(next);
528 if (kerr != KCGI_OK)
529 return gw_kcgi_error(kerr);
530 kerr = khttp_puts(gw_trans->gw_req, div_end);
531 if (kerr != KCGI_OK)
532 error = gw_kcgi_error(kerr);
533 next_disp = 0;
534 break;
537 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
538 (gw_trans->page > 0) &&
539 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
540 prev_disp == gw_trans->repos_total)) {
541 kerr = khttp_puts(gw_trans->gw_req, div_end);
542 if (kerr != KCGI_OK)
543 return gw_kcgi_error(kerr);
546 next_disp++;
548 return error;
551 static const struct got_error *
552 gw_commits(struct gw_trans *gw_trans)
554 const struct got_error *error = NULL;
555 char *commits_html, *commits_navs_html;
556 struct gw_header *header = NULL, *n_header = NULL;
557 enum kcgi_err kerr;
559 if ((header = gw_init_header()) == NULL)
560 return got_error_from_errno("malloc");
562 if (pledge("stdio rpath proc exec sendfd unveil",
563 NULL) == -1) {
564 error = got_error_from_errno("pledge");
565 return error;
568 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
569 if (error)
570 return error;
572 error = gw_get_header(gw_trans, header,
573 gw_trans->gw_conf->got_max_commits_display);
575 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
576 if (kerr != KCGI_OK)
577 return gw_kcgi_error(kerr);
578 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
579 if ((asprintf(&commits_navs_html, commits_navs,
580 gw_trans->repo_name, n_header->commit_id,
581 gw_trans->repo_name, n_header->commit_id,
582 gw_trans->repo_name, n_header->commit_id)) == -1)
583 return got_error_from_errno("asprintf");
585 if ((asprintf(&commits_html, commits_line,
586 gw_gen_commit_header(n_header->commit_id,
587 n_header->refs_str),
588 gw_gen_author_header(n_header->author),
589 gw_gen_committer_header(n_header->committer),
590 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
591 TM_LONG)), gw_html_escape(n_header->commit_msg),
592 commits_navs_html)) == -1)
593 return got_error_from_errno("asprintf");
594 kerr = khttp_puts(gw_trans->gw_req, commits_html);
595 if (kerr != KCGI_OK)
596 return gw_kcgi_error(kerr);
598 kerr = khttp_puts(gw_trans->gw_req, div_end);
599 if (kerr != KCGI_OK)
600 error = gw_kcgi_error(kerr);
602 got_ref_list_free(&header->refs);
603 gw_free_headers(header);
604 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
605 gw_free_headers(n_header);
606 return error;
609 static const struct got_error *
610 gw_briefs(struct gw_trans *gw_trans)
612 const struct got_error *error = NULL;
613 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
614 struct gw_header *header = NULL, *n_header = NULL;
615 enum kcgi_err kerr;
617 if ((header = gw_init_header()) == NULL)
618 return got_error_from_errno("malloc");
620 if (pledge("stdio rpath proc exec sendfd unveil",
621 NULL) == -1) {
622 error = got_error_from_errno("pledge");
623 return error;
626 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
627 if (error)
628 return error;
630 if (gw_trans->action == GW_SUMMARY)
631 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
632 else
633 error = gw_get_header(gw_trans, header,
634 gw_trans->gw_conf->got_max_commits_display);
636 if (error)
637 return error;
639 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
640 if (kerr != KCGI_OK)
641 return gw_kcgi_error(kerr);
643 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
644 if ((asprintf(&briefs_navs_html, briefs_navs,
645 gw_trans->repo_name, n_header->commit_id,
646 gw_trans->repo_name, n_header->commit_id,
647 gw_trans->repo_name, n_header->commit_id)) == -1)
648 return got_error_from_errno("asprintf");
649 newline = strchr(n_header->commit_msg, '\n');
650 if (newline)
651 *newline = '\0';
652 if ((asprintf(&briefs_html, briefs_line,
653 gw_get_time_str(n_header->committer_time, TM_DIFF),
654 n_header->author, gw_html_escape(n_header->commit_msg),
655 briefs_navs_html)) == -1)
656 return got_error_from_errno("asprintf");
657 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
658 if (kerr != KCGI_OK)
659 return gw_kcgi_error(kerr);
661 kerr = khttp_puts(gw_trans->gw_req, div_end);
662 if (kerr != KCGI_OK)
663 error = gw_kcgi_error(kerr);
665 got_ref_list_free(&header->refs);
666 gw_free_headers(header);
667 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
668 gw_free_headers(n_header);
669 return error;
672 static const struct got_error *
673 gw_summary(struct gw_trans *gw_trans)
675 const struct got_error *error = NULL;
676 char *description_html, *repo_owner_html, *repo_age_html,
677 *cloneurl_html, *tags, *heads, *tags_html,
678 *heads_html, *age;
679 enum kcgi_err kerr;
681 if (pledge("stdio rpath proc exec sendfd unveil",
682 NULL) == -1) {
683 error = got_error_from_errno("pledge");
684 return error;
687 /* unveil is applied with gw_briefs below */
689 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
690 if (kerr != KCGI_OK)
691 return gw_kcgi_error(kerr);
693 if (gw_trans->gw_conf->got_show_repo_description) {
694 if (gw_trans->gw_dir->description != NULL &&
695 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
696 if ((asprintf(&description_html, description,
697 gw_trans->gw_dir->description)) == -1)
698 return got_error_from_errno("asprintf");
700 kerr = khttp_puts(gw_trans->gw_req, description_html);
701 free(description_html);
702 if (kerr != KCGI_OK)
703 return gw_kcgi_error(kerr);
707 if (gw_trans->gw_conf->got_show_repo_owner) {
708 if (gw_trans->gw_dir->owner != NULL &&
709 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
710 if ((asprintf(&repo_owner_html, repo_owner,
711 gw_trans->gw_dir->owner)) == -1)
712 return got_error_from_errno("asprintf");
714 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
715 free(repo_owner_html);
716 if (kerr != KCGI_OK)
717 return gw_kcgi_error(kerr);
721 if (gw_trans->gw_conf->got_show_repo_age) {
722 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
723 "refs/heads", TM_LONG);
724 if (age != NULL && (strcmp(age, "") != 0)) {
725 if ((asprintf(&repo_age_html, last_change, age)) == -1)
726 return got_error_from_errno("asprintf");
728 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
729 free(repo_age_html);
730 free(age);
731 if (kerr != KCGI_OK)
732 return gw_kcgi_error(kerr);
736 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
737 if (gw_trans->gw_dir->url != NULL &&
738 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
739 if ((asprintf(&cloneurl_html, cloneurl,
740 gw_trans->gw_dir->url)) == -1)
741 return got_error_from_errno("asprintf");
743 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
744 free(cloneurl_html);
745 if (kerr != KCGI_OK)
746 return gw_kcgi_error(kerr);
749 kerr = khttp_puts(gw_trans->gw_req, div_end);
750 if (kerr != KCGI_OK)
751 return gw_kcgi_error(kerr);
753 error = gw_briefs(gw_trans);
754 if (error)
755 return error;
757 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
758 heads = gw_get_repo_heads(gw_trans);
760 if (tags != NULL && strcmp(tags, "") != 0) {
761 if ((asprintf(&tags_html, summary_tags,
762 tags)) == -1)
763 return got_error_from_errno("asprintf");
764 kerr = khttp_puts(gw_trans->gw_req, tags_html);
765 free(tags_html);
766 free(tags);
767 if (kerr != KCGI_OK)
768 return gw_kcgi_error(kerr);
771 if (heads != NULL && strcmp(heads, "") != 0) {
772 if ((asprintf(&heads_html, summary_heads,
773 heads)) == -1)
774 return got_error_from_errno("asprintf");
775 kerr = khttp_puts(gw_trans->gw_req, heads_html);
776 free(heads_html);
777 free(heads);
778 if (kerr != KCGI_OK)
779 return gw_kcgi_error(kerr);
781 return error;
784 static const struct got_error *
785 gw_tree(struct gw_trans *gw_trans)
787 const struct got_error *error = NULL;
788 struct gw_header *header = NULL;
789 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
790 enum kcgi_err kerr;
792 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
793 return got_error_from_errno("pledge");
795 if ((header = gw_init_header()) == NULL)
796 return got_error_from_errno("malloc");
798 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
799 if (error)
800 return error;
802 error = gw_get_header(gw_trans, header, 1);
803 if (error)
804 return error;
806 tree_html = gw_get_repo_tree(gw_trans);
808 if (tree_html == NULL)
809 tree_html = strdup("");
811 if ((asprintf(&tree_html_disp, tree_header,
812 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
813 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
814 tree_html)) == -1)
815 return got_error_from_errno("asprintf");
817 if ((asprintf(&tree, tree_wrapper, tree_html_disp)) == -1)
818 return got_error_from_errno("asprintf");
820 kerr = khttp_puts(gw_trans->gw_req, tree);
821 if (kerr != KCGI_OK)
822 error = gw_kcgi_error(kerr);
823 got_ref_list_free(&header->refs);
824 gw_free_headers(header);
825 free(tree_html_disp);
826 free(tree_html);
827 free(tree);
828 return error;
831 static const struct got_error *
832 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
834 const struct got_error *error = NULL;
835 DIR *dt;
836 char *dir_test;
837 int opened = 0;
839 if ((asprintf(&dir_test, "%s/%s/%s",
840 gw_trans->gw_conf->got_repos_path, gw_dir->name,
841 GOTWEB_GIT_DIR)) == -1)
842 return got_error_from_errno("asprintf");
844 dt = opendir(dir_test);
845 if (dt == NULL) {
846 free(dir_test);
847 } else {
848 gw_dir->path = strdup(dir_test);
849 opened = 1;
850 goto done;
853 if ((asprintf(&dir_test, "%s/%s/%s",
854 gw_trans->gw_conf->got_repos_path, gw_dir->name,
855 GOTWEB_GOT_DIR)) == -1)
856 return got_error_from_errno("asprintf");
858 dt = opendir(dir_test);
859 if (dt == NULL)
860 free(dir_test);
861 else {
862 opened = 1;
863 error = got_error(GOT_ERR_NOT_GIT_REPO);
864 goto errored;
867 if ((asprintf(&dir_test, "%s/%s",
868 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
869 return got_error_from_errno("asprintf");
871 gw_dir->path = strdup(dir_test);
873 done:
874 gw_dir->description = gw_get_repo_description(gw_trans,
875 gw_dir->path);
876 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
877 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
878 TM_DIFF);
879 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
881 errored:
882 free(dir_test);
883 if (opened)
884 closedir(dt);
885 return error;
888 static const struct got_error *
889 gw_load_got_paths(struct gw_trans *gw_trans)
891 const struct got_error *error = NULL;
892 DIR *d;
893 struct dirent **sd_dent;
894 struct gw_dir *gw_dir;
895 struct stat st;
896 unsigned int d_cnt, d_i;
898 d = opendir(gw_trans->gw_conf->got_repos_path);
899 if (d == NULL) {
900 error = got_error_from_errno2("opendir",
901 gw_trans->gw_conf->got_repos_path);
902 return error;
905 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
906 alphasort);
907 if (d_cnt == -1) {
908 error = got_error_from_errno2("scandir",
909 gw_trans->gw_conf->got_repos_path);
910 return error;
913 for (d_i = 0; d_i < d_cnt; d_i++) {
914 if (gw_trans->gw_conf->got_max_repos > 0 &&
915 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
916 break; /* account for parent and self */
918 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
919 strcmp(sd_dent[d_i]->d_name, "..") == 0)
920 continue;
922 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
923 return got_error_from_errno("gw_dir malloc");
925 error = gw_load_got_path(gw_trans, gw_dir);
926 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
927 continue;
928 else if (error)
929 return error;
931 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
932 !got_path_dir_is_empty(gw_dir->path)) {
933 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
934 entry);
935 gw_trans->repos_total++;
939 closedir(d);
940 return error;
943 static const struct got_error *
944 gw_parse_querystring(struct gw_trans *gw_trans)
946 const struct got_error *error = NULL;
947 struct kpair *p;
948 struct gw_query_action *action = NULL;
949 unsigned int i;
951 if (gw_trans->gw_req->fieldnmap[0]) {
952 error = got_error_from_errno("bad parse");
953 return error;
954 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
955 /* define gw_trans->repo_path */
956 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
957 return got_error_from_errno("asprintf");
959 if ((asprintf(&gw_trans->repo_path, "%s/%s",
960 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
961 return got_error_from_errno("asprintf");
963 /* get action and set function */
964 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
965 for (i = 0; i < nitems(gw_query_funcs); i++) {
966 action = &gw_query_funcs[i];
967 if (action->func_name == NULL)
968 continue;
970 if (strcmp(action->func_name,
971 p->parsed.s) == 0) {
972 gw_trans->action = i;
973 if ((asprintf(&gw_trans->action_name,
974 "%s", action->func_name)) == -1)
975 return
976 got_error_from_errno(
977 "asprintf");
979 break;
982 action = NULL;
985 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
986 if ((asprintf(&gw_trans->commit, "%s",
987 p->parsed.s)) == -1)
988 return got_error_from_errno("asprintf");
990 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
991 if ((asprintf(&gw_trans->repo_file, "%s",
992 p->parsed.s)) == -1)
993 return got_error_from_errno("asprintf");
995 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
996 if ((asprintf(&gw_trans->repo_folder, "%s",
997 p->parsed.s)) == -1)
998 return got_error_from_errno("asprintf");
1000 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1001 if ((asprintf(&gw_trans->headref, "%s",
1002 p->parsed.s)) == -1)
1003 return got_error_from_errno("asprintf");
1005 if (action == NULL) {
1006 error = got_error_from_errno("invalid action");
1007 return error;
1009 if ((gw_trans->gw_dir =
1010 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1011 return got_error_from_errno("gw_dir malloc");
1013 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1014 if (error)
1015 return error;
1016 } else
1017 gw_trans->action = GW_INDEX;
1019 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1020 gw_trans->page = p->parsed.i;
1022 /* if (gw_trans->action == GW_RAW) */
1023 /* gw_trans->mime = KMIME_TEXT_PLAIN; */
1025 return error;
1028 static struct gw_dir *
1029 gw_init_gw_dir(char *dir)
1031 struct gw_dir *gw_dir;
1033 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1034 return NULL;
1036 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1037 return NULL;
1039 return gw_dir;
1042 static const struct got_error *
1043 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1045 enum kcgi_err kerr;
1047 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1048 if (kerr != KCGI_OK)
1049 return gw_kcgi_error(kerr);
1050 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1051 khttps[code]);
1052 if (kerr != KCGI_OK)
1053 return gw_kcgi_error(kerr);
1054 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1055 kmimetypes[mime]);
1056 if (kerr != KCGI_OK)
1057 return gw_kcgi_error(kerr);
1058 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1059 if (kerr != KCGI_OK)
1060 return gw_kcgi_error(kerr);
1061 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1062 if (kerr != KCGI_OK)
1063 return gw_kcgi_error(kerr);
1064 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1065 if (kerr != KCGI_OK)
1066 return gw_kcgi_error(kerr);
1068 kerr = khttp_body(gw_trans->gw_req);
1069 return gw_kcgi_error(kerr);
1072 static const struct got_error *
1073 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
1075 enum kcgi_err kerr;
1077 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1078 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1080 if (err)
1081 kerr = khttp_puts(gw_trans->gw_req, err->msg);
1082 else
1083 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1084 gw_query_funcs[gw_trans->action].template);
1085 if (kerr != KCGI_OK)
1086 return gw_kcgi_error(kerr);
1088 kerr = khtml_close(gw_trans->gw_html_req);
1089 return gw_kcgi_error(kerr);
1092 static int
1093 gw_template(size_t key, void *arg)
1095 const struct got_error *error = NULL;
1096 struct gw_trans *gw_trans = arg;
1097 char *gw_got_link, *gw_site_link;
1098 char *site_owner_name, *site_owner_name_h;
1100 switch (key) {
1101 case (TEMPL_HEAD):
1102 khttp_puts(gw_trans->gw_req, head);
1103 break;
1104 case(TEMPL_HEADER):
1105 gw_got_link = gw_get_got_link(gw_trans);
1106 if (gw_got_link != NULL)
1107 khttp_puts(gw_trans->gw_req, gw_got_link);
1109 free(gw_got_link);
1110 break;
1111 case (TEMPL_SITEPATH):
1112 gw_site_link = gw_get_site_link(gw_trans);
1113 if (gw_site_link != NULL)
1114 khttp_puts(gw_trans->gw_req, gw_site_link);
1116 free(gw_site_link);
1117 break;
1118 case(TEMPL_TITLE):
1119 if (gw_trans->gw_conf->got_site_name != NULL)
1120 khtml_puts(gw_trans->gw_html_req,
1121 gw_trans->gw_conf->got_site_name);
1123 break;
1124 case (TEMPL_SEARCH):
1125 khttp_puts(gw_trans->gw_req, search);
1126 break;
1127 case(TEMPL_SITEOWNER):
1128 if (gw_trans->gw_conf->got_site_owner != NULL &&
1129 gw_trans->gw_conf->got_show_site_owner) {
1130 site_owner_name =
1131 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1132 if ((asprintf(&site_owner_name_h, site_owner,
1133 site_owner_name))
1134 == -1)
1135 return 0;
1137 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1138 free(site_owner_name);
1139 free(site_owner_name_h);
1141 break;
1142 case(TEMPL_CONTENT):
1143 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1144 if (error)
1145 khttp_puts(gw_trans->gw_req, error->msg);
1146 break;
1147 default:
1148 return 0;
1150 return 1;
1153 static char *
1154 gw_gen_commit_header(char *str1, char *str2)
1156 char *return_html = NULL, *ref_str = NULL;
1158 if (strcmp(str2, "") != 0) {
1159 if ((asprintf(&ref_str, "(%s)", str2)) == -1) {
1160 return_html = strdup("");
1161 return return_html;
1163 } else
1164 ref_str = strdup("");
1167 if ((asprintf(&return_html, header_commit_html, str1, ref_str)) == -1)
1168 return_html = strdup("");
1170 free(ref_str);
1171 return return_html;
1174 static char *
1175 gw_gen_diff_header(char *str1, char *str2)
1177 char *return_html = NULL;
1179 if ((asprintf(&return_html, header_diff_html, str1, str2)) == -1)
1180 return_html = strdup("");
1182 return return_html;
1185 static char *
1186 gw_gen_author_header(char *str)
1188 char *return_html = NULL;
1190 if ((asprintf(&return_html, header_author_html, str)) == -1)
1191 return_html = strdup("");
1193 return return_html;
1196 static char *
1197 gw_gen_committer_header(char *str)
1199 char *return_html = NULL;
1201 if ((asprintf(&return_html, header_committer_html, str)) == -1)
1202 return_html = strdup("");
1204 return return_html;
1207 static char *
1208 gw_gen_age_header(char *str)
1210 char *return_html = NULL;
1212 if ((asprintf(&return_html, header_age_html, str)) == -1)
1213 return_html = strdup("");
1215 return return_html;
1218 static char *
1219 gw_gen_commit_msg_header(char *str)
1221 char *return_html = NULL;
1223 if ((asprintf(&return_html, header_commit_msg_html, str)) == -1)
1224 return_html = strdup("");
1226 return return_html;
1229 static char *
1230 gw_gen_tree_header(char *str)
1232 char *return_html = NULL;
1234 if ((asprintf(&return_html, header_tree_html, str)) == -1)
1235 return_html = strdup("");
1237 return return_html;
1240 static char *
1241 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1243 FILE *f;
1244 char *description = NULL, *d_file = NULL;
1245 unsigned int len;
1247 if (gw_trans->gw_conf->got_show_repo_description == false)
1248 goto err;
1250 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1251 goto err;
1253 if ((f = fopen(d_file, "r")) == NULL)
1254 goto err;
1256 fseek(f, 0, SEEK_END);
1257 len = ftell(f) + 1;
1258 fseek(f, 0, SEEK_SET);
1259 if ((description = calloc(len, sizeof(char *))) == NULL)
1260 goto err;
1262 fread(description, 1, len, f);
1263 fclose(f);
1264 free(d_file);
1265 return description;
1266 err:
1267 if ((asprintf(&description, "%s", "")) == -1)
1268 return NULL;
1270 return description;
1273 static char *
1274 gw_get_time_str(time_t committer_time, int ref_tm)
1276 struct tm tm;
1277 time_t diff_time;
1278 char *years = "years ago", *months = "months ago";
1279 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1280 char *minutes = "minutes ago", *seconds = "seconds ago";
1281 char *now = "right now";
1282 char *repo_age, *s;
1283 char datebuf[29];
1285 switch (ref_tm) {
1286 case TM_DIFF:
1287 diff_time = time(NULL) - committer_time;
1288 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1289 if ((asprintf(&repo_age, "%lld %s",
1290 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1291 return NULL;
1292 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1293 if ((asprintf(&repo_age, "%lld %s",
1294 (diff_time / 60 / 60 / 24 / (365 / 12)),
1295 months)) == -1)
1296 return NULL;
1297 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1298 if ((asprintf(&repo_age, "%lld %s",
1299 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1300 return NULL;
1301 } else if (diff_time > 60 * 60 * 24 * 2) {
1302 if ((asprintf(&repo_age, "%lld %s",
1303 (diff_time / 60 / 60 / 24), days)) == -1)
1304 return NULL;
1305 } else if (diff_time > 60 * 60 * 2) {
1306 if ((asprintf(&repo_age, "%lld %s",
1307 (diff_time / 60 / 60), hours)) == -1)
1308 return NULL;
1309 } else if (diff_time > 60 * 2) {
1310 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1311 minutes)) == -1)
1312 return NULL;
1313 } else if (diff_time > 2) {
1314 if ((asprintf(&repo_age, "%lld %s", diff_time,
1315 seconds)) == -1)
1316 return NULL;
1317 } else {
1318 if ((asprintf(&repo_age, "%s", now)) == -1)
1319 return NULL;
1321 break;
1322 case TM_LONG:
1323 if (gmtime_r(&committer_time, &tm) == NULL)
1324 return NULL;
1326 s = asctime_r(&tm, datebuf);
1327 if (s == NULL)
1328 return NULL;
1330 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1331 return NULL;
1332 break;
1334 return repo_age;
1337 static char *
1338 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1339 int ref_tm)
1341 const struct got_error *error = NULL;
1342 struct got_object_id *id = NULL;
1343 struct got_repository *repo = NULL;
1344 struct got_commit_object *commit = NULL;
1345 struct got_reflist_head refs;
1346 struct got_reflist_entry *re;
1347 struct got_reference *head_ref;
1348 int is_head = 0;
1349 time_t committer_time = 0, cmp_time = 0;
1350 const char *refname;
1351 char *repo_age = NULL;
1353 SIMPLEQ_INIT(&refs);
1355 if (repo_ref == NULL)
1356 return NULL;
1358 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1359 is_head = 1;
1361 if (gw_trans->gw_conf->got_show_repo_age == false) {
1362 if ((asprintf(&repo_age, "")) == -1)
1363 return NULL;
1364 return repo_age;
1367 error = got_repo_open(&repo, dir, NULL);
1368 if (error)
1369 goto err;
1371 if (is_head)
1372 error = got_ref_list(&refs, repo, "refs/heads",
1373 got_ref_cmp_by_name, NULL);
1374 else
1375 error = got_ref_list(&refs, repo, repo_ref,
1376 got_ref_cmp_by_name, NULL);
1377 if (error)
1378 goto err;
1380 SIMPLEQ_FOREACH(re, &refs, entry) {
1381 if (is_head)
1382 refname = strdup(repo_ref);
1383 else
1384 refname = got_ref_get_name(re->ref);
1385 error = got_ref_open(&head_ref, repo, refname, 0);
1386 if (error)
1387 goto err;
1389 error = got_ref_resolve(&id, repo, head_ref);
1390 got_ref_close(head_ref);
1391 if (error)
1392 goto err;
1394 error = got_object_open_as_commit(&commit, repo, id);
1395 if (error)
1396 goto err;
1398 committer_time =
1399 got_object_commit_get_committer_time(commit);
1401 if (cmp_time < committer_time)
1402 cmp_time = committer_time;
1405 if (cmp_time != 0) {
1406 committer_time = cmp_time;
1407 repo_age = gw_get_time_str(committer_time, ref_tm);
1408 } else
1409 if ((asprintf(&repo_age, "")) == -1)
1410 return NULL;
1411 got_ref_list_free(&refs);
1412 free(id);
1413 return repo_age;
1414 err:
1415 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1416 return NULL;
1418 return repo_age;
1421 static char *
1422 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1424 const struct got_error *error;
1425 FILE *f = NULL;
1426 struct got_object_id *id1 = NULL, *id2 = NULL;
1427 struct buf *diffbuf = NULL;
1428 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1429 *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1430 int obj_type;
1431 size_t newsize;
1433 f = got_opentemp();
1434 if (f == NULL)
1435 return NULL;
1437 error = buf_alloc(&diffbuf, 0);
1438 if (error)
1439 return NULL;
1441 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1442 if (error)
1443 goto done;
1445 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1446 error = got_repo_match_object_id(&id1, &label1,
1447 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1448 if (error)
1449 goto done;
1452 error = got_repo_match_object_id(&id2, &label2,
1453 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1454 if (error)
1455 goto done;
1457 error = got_object_get_type(&obj_type, header->repo, id2);
1458 if (error)
1459 goto done;
1460 switch (obj_type) {
1461 case GOT_OBJ_TYPE_BLOB:
1462 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1463 header->repo, f);
1464 break;
1465 case GOT_OBJ_TYPE_TREE:
1466 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1467 header->repo, f);
1468 break;
1469 case GOT_OBJ_TYPE_COMMIT:
1470 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1471 header->repo, f);
1472 break;
1473 default:
1474 error = got_error(GOT_ERR_OBJ_TYPE);
1477 if ((buf = calloc(128, sizeof(char *))) == NULL)
1478 goto done;
1480 fseek(f, 0, SEEK_SET);
1482 while ((fgets(buf, 2048, f)) != NULL) {
1483 n_buf = buf;
1484 while (*n_buf == '\n')
1485 n_buf++;
1486 newline = strchr(n_buf, '\n');
1487 if (newline)
1488 *newline = ' ';
1490 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1491 if (buf_color == NULL)
1492 continue;
1494 error = buf_puts(&newsize, diffbuf, buf_color);
1495 if (error)
1496 return NULL;
1498 error = buf_puts(&newsize, diffbuf, div_end);
1499 if (error)
1500 return NULL;
1503 if (buf_len(diffbuf) > 0) {
1504 error = buf_putc(diffbuf, '\0');
1505 diff_html = strdup(buf_get(diffbuf));
1507 done:
1508 fclose(f);
1509 free(buf_color);
1510 free(buf);
1511 free(diffbuf);
1512 free(label1);
1513 free(label2);
1514 free(id1);
1515 free(id2);
1517 if (error)
1518 return NULL;
1519 else
1520 return diff_html;
1523 static char *
1524 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1526 FILE *f;
1527 char *owner = NULL, *d_file = NULL;
1528 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1529 char *comp, *pos, *buf;
1530 unsigned int i;
1532 if (gw_trans->gw_conf->got_show_repo_owner == false)
1533 goto err;
1535 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1536 goto err;
1538 if ((f = fopen(d_file, "r")) == NULL)
1539 goto err;
1541 if ((buf = calloc(128, sizeof(char *))) == NULL)
1542 goto err;
1544 while ((fgets(buf, 128, f)) != NULL) {
1545 if ((pos = strstr(buf, gotweb)) != NULL)
1546 break;
1548 if ((pos = strstr(buf, gitweb)) != NULL)
1549 break;
1552 if (pos == NULL)
1553 goto err;
1555 do {
1556 fgets(buf, 128, f);
1557 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1559 if (comp == NULL)
1560 goto err;
1562 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1563 goto err;
1565 for (i = 0; i < 2; i++) {
1566 owner = strsep(&buf, "\"");
1569 if (owner == NULL)
1570 goto err;
1572 fclose(f);
1573 free(d_file);
1574 return owner;
1575 err:
1576 if ((asprintf(&owner, "%s", "")) == -1)
1577 return NULL;
1579 return owner;
1582 static char *
1583 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1585 FILE *f;
1586 char *url = NULL, *d_file = NULL;
1587 unsigned int len;
1589 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1590 return NULL;
1592 if ((f = fopen(d_file, "r")) == NULL)
1593 return NULL;
1595 fseek(f, 0, SEEK_END);
1596 len = ftell(f) + 1;
1597 fseek(f, 0, SEEK_SET);
1599 if ((url = calloc(len, sizeof(char *))) == NULL)
1600 return NULL;
1602 fread(url, 1, len, f);
1603 fclose(f);
1604 free(d_file);
1605 return url;
1608 static char *
1609 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
1611 const struct got_error *error = NULL;
1612 struct got_repository *repo = NULL;
1613 struct got_reflist_head refs;
1614 struct got_reflist_entry *re;
1615 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1616 *age = NULL;
1617 char *newline;
1618 struct buf *diffbuf = NULL;
1619 size_t newsize;
1621 SIMPLEQ_INIT(&refs);
1623 error = buf_alloc(&diffbuf, 0);
1624 if (error)
1625 return NULL;
1627 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1628 if (error)
1629 goto done;
1631 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1632 if (error)
1633 goto done;
1635 SIMPLEQ_FOREACH(re, &refs, entry) {
1636 const char *refname;
1637 char *refstr, *tag_commit0, *tag_commit, *id_str;
1638 time_t tagger_time;
1639 struct got_object_id *id;
1640 struct got_tag_object *tag;
1642 refname = got_ref_get_name(re->ref);
1643 if (strncmp(refname, "refs/tags/", 10) != 0)
1644 continue;
1645 refname += 10;
1646 refstr = got_ref_to_str(re->ref);
1647 if (refstr == NULL) {
1648 error = got_error_from_errno("got_ref_to_str");
1649 goto done;
1652 error = got_ref_resolve(&id, repo, re->ref);
1653 if (error)
1654 goto done;
1655 error = got_object_open_as_tag(&tag, repo, id);
1656 free(id);
1657 if (error)
1658 goto done;
1660 tagger_time = got_object_tag_get_tagger_time(tag);
1662 error = got_object_id_str(&id_str,
1663 got_object_tag_get_object_id(tag));
1664 if (error)
1665 goto done;
1667 tag_commit0 = strdup(got_object_tag_get_message(tag));
1669 if (tag_commit0 == NULL) {
1670 error = got_error_from_errno("strdup");
1671 goto done;
1674 tag_commit = tag_commit0;
1675 while (*tag_commit == '\n')
1676 tag_commit++;
1678 switch (tag_type) {
1679 case TAGBRIEF:
1680 newline = strchr(tag_commit, '\n');
1681 if (newline)
1682 *newline = '\0';
1684 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1685 TM_DIFF))) == -1) {
1686 error = got_error_from_errno("asprintf");
1687 goto done;
1690 if ((asprintf(&tags_navs_disp, tags_navs,
1691 gw_trans->repo_name, id_str, gw_trans->repo_name,
1692 id_str, gw_trans->repo_name, id_str,
1693 gw_trans->repo_name, id_str)) == -1) {
1694 error = got_error_from_errno("asprintf");
1695 goto done;
1698 if ((asprintf(&tag_row, tags_row, age, refname,
1699 tag_commit, tags_navs_disp)) == -1) {
1700 error = got_error_from_errno("asprintf");
1701 goto done;
1704 free(tags_navs_disp);
1705 break;
1706 case TAGFULL:
1707 break;
1708 default:
1709 break;
1712 got_object_tag_close(tag);
1714 error = buf_puts(&newsize, diffbuf, tag_row);
1716 free(id_str);
1717 free(refstr);
1718 free(age);
1719 free(tag_commit0);
1720 free(tag_row);
1722 if (error || (limit && --limit == 0))
1723 break;
1726 if (buf_len(diffbuf) > 0) {
1727 error = buf_putc(diffbuf, '\0');
1728 tags = strdup(buf_get(diffbuf));
1730 done:
1731 buf_free(diffbuf);
1732 got_ref_list_free(&refs);
1733 if (repo)
1734 got_repo_close(repo);
1735 if (error)
1736 return NULL;
1737 else
1738 return tags;
1741 static void
1742 gw_free_headers(struct gw_header *header)
1744 free(header->id);
1745 free(header->path);
1746 if (header->commit != NULL)
1747 got_object_commit_close(header->commit);
1748 if (header->repo)
1749 got_repo_close(header->repo);
1750 free(header->refs_str);
1751 free(header->commit_id);
1752 free(header->parent_id);
1753 free(header->tree_id);
1754 free(header->author);
1755 free(header->committer);
1756 free(header->commit_msg);
1759 static struct gw_header *
1760 gw_init_header()
1762 struct gw_header *header;
1764 header = malloc(sizeof(*header));
1765 if (header == NULL)
1766 return NULL;
1768 header->repo = NULL;
1769 header->commit = NULL;
1770 header->id = NULL;
1771 header->path = NULL;
1772 SIMPLEQ_INIT(&header->refs);
1774 return header;
1777 static const struct got_error *
1778 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1779 int limit)
1781 const struct got_error *error = NULL;
1782 struct got_commit_graph *graph = NULL;
1784 error = got_commit_graph_open(&graph, header->path, 0);
1785 if (error)
1786 goto done;
1788 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1789 NULL, NULL);
1790 if (error)
1791 goto done;
1793 for (;;) {
1794 error = got_commit_graph_iter_next(&header->id, graph,
1795 header->repo, NULL, NULL);
1796 if (error) {
1797 if (error->code == GOT_ERR_ITER_COMPLETED)
1798 error = NULL;
1799 goto done;
1801 if (header->id == NULL)
1802 goto done;
1804 error = got_object_open_as_commit(&header->commit, header->repo,
1805 header->id);
1806 if (error)
1807 goto done;
1809 error = gw_get_commit(gw_trans, header);
1810 if (limit > 1) {
1811 struct gw_header *n_header = NULL;
1812 if ((n_header = gw_init_header()) == NULL)
1813 error = got_error_from_errno("malloc");
1815 n_header->refs_str = strdup(header->refs_str);
1816 n_header->commit_id = strdup(header->commit_id);
1817 n_header->parent_id = strdup(header->parent_id);
1818 n_header->tree_id = strdup(header->tree_id);
1819 n_header->author = strdup(header->author);
1820 n_header->committer = strdup(header->committer);
1821 n_header->commit_msg = strdup(header->commit_msg);
1822 n_header->committer_time = header->committer_time;
1823 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
1824 entry);
1826 if (error || (limit && --limit == 0))
1827 break;
1829 done:
1830 if (graph)
1831 got_commit_graph_close(graph);
1832 return error;
1835 static const struct got_error *
1836 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
1838 const struct got_error *error = NULL;
1839 struct got_reflist_entry *re;
1840 struct got_object_id *id2 = NULL;
1841 struct got_object_qid *parent_id;
1842 char *refs_str = NULL,
1843 *commit_msg = NULL, *commit_msg0;
1845 /*print commit*/
1846 SIMPLEQ_FOREACH(re, &header->refs, entry) {
1847 char *s;
1848 const char *name;
1849 struct got_tag_object *tag = NULL;
1850 int cmp;
1852 name = got_ref_get_name(re->ref);
1853 if (strcmp(name, GOT_REF_HEAD) == 0)
1854 continue;
1855 if (strncmp(name, "refs/", 5) == 0)
1856 name += 5;
1857 if (strncmp(name, "got/", 4) == 0)
1858 continue;
1859 if (strncmp(name, "heads/", 6) == 0)
1860 name += 6;
1861 if (strncmp(name, "remotes/", 8) == 0)
1862 name += 8;
1863 if (strncmp(name, "tags/", 5) == 0) {
1864 error = got_object_open_as_tag(&tag, header->repo,
1865 re->id);
1866 if (error) {
1867 if (error->code != GOT_ERR_OBJ_TYPE)
1868 continue;
1870 * Ref points at something other
1871 * than a tag.
1873 error = NULL;
1874 tag = NULL;
1877 cmp = got_object_id_cmp(tag ?
1878 got_object_tag_get_object_id(tag) : re->id, header->id);
1879 if (tag)
1880 got_object_tag_close(tag);
1881 if (cmp != 0)
1882 continue;
1883 s = refs_str;
1884 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1885 s ? ", " : "", name)) == -1) {
1886 error = got_error_from_errno("asprintf");
1887 free(s);
1888 return error;
1890 header->refs_str = strdup(refs_str);
1891 free(s);
1894 if (refs_str == NULL)
1895 header->refs_str = strdup("");
1896 free(refs_str);
1898 error = got_object_id_str(&header->commit_id, header->id);
1899 if (error)
1900 return error;
1902 error = got_object_id_str(&header->tree_id,
1903 got_object_commit_get_tree_id(header->commit));
1904 if (error)
1905 return error;
1907 if (gw_trans->action == GW_DIFF) {
1908 parent_id = SIMPLEQ_FIRST(
1909 got_object_commit_get_parent_ids(header->commit));
1910 if (parent_id != NULL) {
1911 id2 = got_object_id_dup(parent_id->id);
1912 free (parent_id);
1913 error = got_object_id_str(&header->parent_id, id2);
1914 if (error)
1915 return error;
1916 free(id2);
1917 } else
1918 header->parent_id = strdup("/dev/null");
1919 } else
1920 header->parent_id = strdup("");
1922 header->committer_time =
1923 got_object_commit_get_committer_time(header->commit);
1924 header->author = strdup(got_object_commit_get_author(header->commit));
1925 header->committer =
1926 strdup(got_object_commit_get_committer(header->commit));
1928 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
1929 if (error)
1930 return error;
1932 commit_msg = commit_msg0;
1933 while (*commit_msg == '\n')
1934 commit_msg++;
1936 header->commit_msg = strdup(commit_msg);
1937 free(commit_msg0);
1938 return error;
1941 static const struct got_error *
1942 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
1944 const struct got_error *error = NULL;
1945 char *in_repo_path = NULL;
1947 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1948 if (error)
1949 return error;
1951 if (gw_trans->commit == NULL) {
1952 struct got_reference *head_ref;
1953 error = got_ref_open(&head_ref, header->repo,
1954 gw_trans->headref, 0);
1955 if (error)
1956 return error;
1958 error = got_ref_resolve(&header->id, header->repo, head_ref);
1959 got_ref_close(head_ref);
1960 if (error)
1961 return error;
1963 error = got_object_open_as_commit(&header->commit,
1964 header->repo, header->id);
1965 } else {
1966 struct got_reference *ref;
1967 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
1968 if (error == NULL) {
1969 int obj_type;
1970 error = got_ref_resolve(&header->id, header->repo, ref);
1971 got_ref_close(ref);
1972 if (error)
1973 return error;
1974 error = got_object_get_type(&obj_type, header->repo,
1975 header->id);
1976 if (error)
1977 return error;
1978 if (obj_type == GOT_OBJ_TYPE_TAG) {
1979 struct got_tag_object *tag;
1980 error = got_object_open_as_tag(&tag,
1981 header->repo, header->id);
1982 if (error)
1983 return error;
1984 if (got_object_tag_get_object_type(tag) !=
1985 GOT_OBJ_TYPE_COMMIT) {
1986 got_object_tag_close(tag);
1987 error = got_error(GOT_ERR_OBJ_TYPE);
1988 return error;
1990 free(header->id);
1991 header->id = got_object_id_dup(
1992 got_object_tag_get_object_id(tag));
1993 if (header->id == NULL)
1994 error = got_error_from_errno(
1995 "got_object_id_dup");
1996 got_object_tag_close(tag);
1997 if (error)
1998 return error;
1999 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2000 error = got_error(GOT_ERR_OBJ_TYPE);
2001 return error;
2003 error = got_object_open_as_commit(&header->commit,
2004 header->repo, header->id);
2005 if (error)
2006 return error;
2008 if (header->commit == NULL) {
2009 error = got_repo_match_object_id_prefix(&header->id,
2010 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2011 header->repo);
2012 if (error)
2013 return error;
2015 error = got_repo_match_object_id_prefix(&header->id,
2016 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2017 header->repo);
2020 error = got_repo_map_path(&in_repo_path, header->repo,
2021 gw_trans->repo_path, 1);
2022 if (error)
2023 return error;
2025 if (in_repo_path) {
2026 header->path = strdup(in_repo_path);
2028 free(in_repo_path);
2030 error = got_ref_list(&header->refs, header->repo, NULL,
2031 got_ref_cmp_by_name, NULL);
2032 if (error)
2033 return error;
2035 error = gw_get_commits(gw_trans, header, limit);
2036 return error;
2039 struct blame_line {
2040 int annotated;
2041 char *id_str;
2042 char *committer;
2043 char datebuf[11]; /* YYYY-MM-DD + NUL */
2046 struct gw_blame_cb_args {
2047 struct blame_line *lines;
2048 int nlines;
2049 int nlines_prec;
2050 int lineno_cur;
2051 off_t *line_offsets;
2052 FILE *f;
2053 struct got_repository *repo;
2054 struct gw_trans *gw_trans;
2055 struct buf *blamebuf;
2058 static const struct got_error *
2059 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2061 const struct got_error *err = NULL;
2062 struct gw_blame_cb_args *a = arg;
2063 struct blame_line *bline;
2064 char *line = NULL;
2065 size_t linesize = 0, newsize;
2066 struct got_commit_object *commit = NULL;
2067 off_t offset;
2068 struct tm tm;
2069 time_t committer_time;
2071 if (nlines != a->nlines ||
2072 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2073 return got_error(GOT_ERR_RANGE);
2075 if (lineno == -1)
2076 return NULL; /* no change in this commit */
2078 /* Annotate this line. */
2079 bline = &a->lines[lineno - 1];
2080 if (bline->annotated)
2081 return NULL;
2082 err = got_object_id_str(&bline->id_str, id);
2083 if (err)
2084 return err;
2086 err = got_object_open_as_commit(&commit, a->repo, id);
2087 if (err)
2088 goto done;
2090 bline->committer = strdup(got_object_commit_get_committer(commit));
2091 if (bline->committer == NULL) {
2092 err = got_error_from_errno("strdup");
2093 goto done;
2096 committer_time = got_object_commit_get_committer_time(commit);
2097 if (localtime_r(&committer_time, &tm) == NULL)
2098 return got_error_from_errno("localtime_r");
2099 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2100 &tm) >= sizeof(bline->datebuf)) {
2101 err = got_error(GOT_ERR_NO_SPACE);
2102 goto done;
2104 bline->annotated = 1;
2106 /* Print lines annotated so far. */
2107 bline = &a->lines[a->lineno_cur - 1];
2108 if (!bline->annotated)
2109 goto done;
2111 offset = a->line_offsets[a->lineno_cur - 1];
2112 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2113 err = got_error_from_errno("fseeko");
2114 goto done;
2117 while (bline->annotated) {
2118 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2119 *line_escape = NULL;
2120 size_t len;
2122 if (getline(&line, &linesize, a->f) == -1) {
2123 if (ferror(a->f))
2124 err = got_error_from_errno("getline");
2125 break;
2128 committer = bline->committer;
2129 smallerthan = strchr(committer, '<');
2130 if (smallerthan && smallerthan[1] != '\0')
2131 committer = smallerthan + 1;
2132 at = strchr(committer, '@');
2133 if (at)
2134 *at = '\0';
2135 len = strlen(committer);
2136 if (len >= 9)
2137 committer[8] = '\0';
2139 nl = strchr(line, '\n');
2140 if (nl)
2141 *nl = '\0';
2143 if (strcmp(line, "") != 0)
2144 line_escape = strdup(gw_html_escape(line));
2145 else
2146 line_escape = strdup("");
2148 asprintf(&blame_row, blame_line, a->nlines_prec,
2149 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2150 line_escape);
2151 a->lineno_cur++;
2152 err = buf_puts(&newsize, a->blamebuf, blame_row);
2153 if (err)
2154 return err;
2156 bline = &a->lines[a->lineno_cur - 1];
2157 free(line_escape);
2158 free(blame_row);
2160 done:
2161 if (commit)
2162 got_object_commit_close(commit);
2163 free(line);
2164 return err;
2167 static char*
2168 gw_get_file_blame(struct gw_trans *gw_trans)
2170 const struct got_error *error = NULL;
2171 struct got_repository *repo = NULL;
2172 struct got_object_id *obj_id = NULL;
2173 struct got_object_id *commit_id = NULL;
2174 struct got_blob_object *blob = NULL;
2175 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2176 *folder = NULL;
2177 struct gw_blame_cb_args bca;
2178 int i, obj_type;
2179 size_t filesize;
2181 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2182 if (error)
2183 goto done;
2185 if (gw_trans->repo_folder != NULL) {
2186 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2187 error = got_error_from_errno("asprintf");
2188 goto done;
2190 } else
2191 folder = strdup("");
2193 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2194 error = got_error_from_errno("asprintf");
2195 goto done;
2197 free(folder);
2199 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2200 if (error)
2201 goto done;
2203 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2204 GOT_OBJ_TYPE_COMMIT, 1, repo);
2205 if (error)
2206 goto done;
2208 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2209 if (error)
2210 goto done;
2212 if (obj_id == NULL) {
2213 error = got_error(GOT_ERR_NO_OBJ);
2214 goto done;
2217 error = got_object_get_type(&obj_type, repo, obj_id);
2218 if (error)
2219 goto done;
2221 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2222 error = got_error(GOT_ERR_OBJ_TYPE);
2223 goto done;
2226 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2227 if (error)
2228 goto done;
2230 error = buf_alloc(&bca.blamebuf, 0);
2231 if (error)
2232 goto done;
2234 bca.f = got_opentemp();
2235 if (bca.f == NULL) {
2236 error = got_error_from_errno("got_opentemp");
2237 goto done;
2239 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2240 &bca.line_offsets, bca.f, blob);
2241 if (error || bca.nlines == 0)
2242 goto done;
2244 /* Don't include \n at EOF in the blame line count. */
2245 if (bca.line_offsets[bca.nlines - 1] == filesize)
2246 bca.nlines--;
2248 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2249 if (bca.lines == NULL) {
2250 error = got_error_from_errno("calloc");
2251 goto done;
2253 bca.lineno_cur = 1;
2254 bca.nlines_prec = 0;
2255 i = bca.nlines;
2256 while (i > 0) {
2257 i /= 10;
2258 bca.nlines_prec++;
2260 bca.repo = repo;
2261 bca.gw_trans = gw_trans;
2263 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2264 NULL, NULL);
2265 if (buf_len(bca.blamebuf) > 0) {
2266 error = buf_putc(bca.blamebuf, '\0');
2267 blame_html = strdup(buf_get(bca.blamebuf));
2269 done:
2270 free(bca.blamebuf);
2271 free(in_repo_path);
2272 free(commit_id);
2273 free(obj_id);
2274 free(path);
2276 if (blob)
2277 error = got_object_blob_close(blob);
2278 if (repo)
2279 error = got_repo_close(repo);
2280 if (error)
2281 return NULL;
2282 if (bca.lines) {
2283 for (i = 0; i < bca.nlines; i++) {
2284 struct blame_line *bline = &bca.lines[i];
2285 free(bline->id_str);
2286 free(bline->committer);
2288 free(bca.lines);
2290 free(bca.line_offsets);
2291 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2292 error = got_error_from_errno("fclose");
2293 if (error)
2294 return NULL;
2295 else
2296 return blame_html;
2299 static char*
2300 gw_get_repo_tree(struct gw_trans *gw_trans)
2302 const struct got_error *error = NULL;
2303 struct got_repository *repo = NULL;
2304 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2305 struct got_tree_object *tree = NULL;
2306 struct buf *diffbuf = NULL;
2307 size_t newsize;
2308 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2309 *tree_row = NULL, *id_str;
2310 int nentries, i;
2312 error = buf_alloc(&diffbuf, 0);
2313 if (error)
2314 return NULL;
2316 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2317 if (error)
2318 goto done;
2320 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2321 if (error)
2322 goto done;
2324 if (gw_trans->repo_folder != NULL)
2325 path = strdup(gw_trans->repo_folder);
2326 else if (in_repo_path) {
2327 free(path);
2328 path = in_repo_path;
2331 if (gw_trans->commit == NULL) {
2332 struct got_reference *head_ref;
2333 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2334 if (error)
2335 goto done;
2337 error = got_ref_resolve(&commit_id, repo, head_ref);
2338 got_ref_close(head_ref);
2340 } else
2341 error = got_repo_match_object_id(&commit_id, NULL,
2342 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2343 if (error)
2344 goto done;
2346 error = got_object_id_str(&gw_trans->commit, commit_id);
2347 if (error)
2348 goto done;
2350 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2351 if (error)
2352 goto done;
2354 error = got_object_open_as_tree(&tree, repo, tree_id);
2355 if (error)
2356 goto done;
2358 nentries = got_object_tree_get_nentries(tree);
2360 for (i = 0; i < nentries; i++) {
2361 struct got_tree_entry *te;
2362 const char *modestr = "";
2363 char *id = NULL, *url_html = NULL;
2365 te = got_object_tree_get_entry(tree, i);
2367 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2368 if (error)
2369 goto done;
2371 if ((asprintf(&id, "%s", id_str)) == -1) {
2372 error = got_error_from_errno("asprintf");
2373 free(id_str);
2374 goto done;
2377 mode_t mode = got_tree_entry_get_mode(te);
2379 if (got_object_tree_entry_is_submodule(te))
2380 modestr = "$";
2381 else if (S_ISLNK(mode))
2382 modestr = "@";
2383 else if (S_ISDIR(mode))
2384 modestr = "/";
2385 else if (mode & S_IXUSR)
2386 modestr = "*";
2388 char *build_folder = NULL;
2389 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2390 if (gw_trans->repo_folder != NULL) {
2391 if ((asprintf(&build_folder, "%s/%s",
2392 gw_trans->repo_folder,
2393 got_tree_entry_get_name(te))) == -1) {
2394 error =
2395 got_error_from_errno("asprintf");
2396 goto done;
2398 } else {
2399 if (asprintf(&build_folder, "%s",
2400 got_tree_entry_get_name(te)) == -1)
2401 goto done;
2404 if ((asprintf(&url_html, folder_html,
2405 gw_trans->repo_name, gw_trans->action_name,
2406 gw_trans->commit, build_folder,
2407 got_tree_entry_get_name(te), modestr)) == -1) {
2408 error = got_error_from_errno("asprintf");
2409 goto done;
2411 } else {
2412 if (gw_trans->repo_folder != NULL) {
2413 if ((asprintf(&build_folder, "%s",
2414 gw_trans->repo_folder)) == -1) {
2415 error =
2416 got_error_from_errno("asprintf");
2417 goto done;
2419 } else
2420 build_folder = strdup("");
2422 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2423 "blame", gw_trans->commit,
2424 got_tree_entry_get_name(te), build_folder,
2425 got_tree_entry_get_name(te), modestr)) == -1) {
2426 error = got_error_from_errno("asprintf");
2427 goto done;
2430 free(build_folder);
2432 if (error)
2433 goto done;
2435 if ((asprintf(&tree_row, tree_line, url_html)) == -1) {
2436 error = got_error_from_errno("asprintf");
2437 goto done;
2439 error = buf_puts(&newsize, diffbuf, tree_row);
2440 if (error)
2441 goto done;
2443 free(id);
2444 free(id_str);
2445 free(url_html);
2446 free(tree_row);
2449 if (buf_len(diffbuf) > 0) {
2450 error = buf_putc(diffbuf, '\0');
2451 tree_html = strdup(buf_get(diffbuf));
2453 done:
2454 if (tree)
2455 got_object_tree_close(tree);
2456 if (repo)
2457 got_repo_close(repo);
2459 free(in_repo_path);
2460 free(tree_id);
2461 free(diffbuf);
2462 if (error)
2463 return NULL;
2464 else
2465 return tree_html;
2468 static char *
2469 gw_get_repo_heads(struct gw_trans *gw_trans)
2471 const struct got_error *error = NULL;
2472 struct got_repository *repo = NULL;
2473 struct got_reflist_head refs;
2474 struct got_reflist_entry *re;
2475 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2476 struct buf *diffbuf = NULL;
2477 size_t newsize;
2479 SIMPLEQ_INIT(&refs);
2481 error = buf_alloc(&diffbuf, 0);
2482 if (error)
2483 return NULL;
2485 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2486 if (error)
2487 goto done;
2489 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2490 NULL);
2491 if (error)
2492 goto done;
2494 SIMPLEQ_FOREACH(re, &refs, entry) {
2495 char *refname;
2497 refname = strdup(got_ref_get_name(re->ref));
2498 if (refname == NULL) {
2499 error = got_error_from_errno("got_ref_to_str");
2500 goto done;
2503 if (strncmp(refname, "refs/heads/", 11) != 0) {
2504 free(refname);
2505 continue;
2508 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2509 TM_DIFF);
2511 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2512 refname, gw_trans->repo_name, refname,
2513 gw_trans->repo_name, refname, gw_trans->repo_name,
2514 refname)) == -1) {
2515 error = got_error_from_errno("asprintf");
2516 goto done;
2519 if (strncmp(refname, "refs/heads/", 11) == 0)
2520 refname += 11;
2522 if ((asprintf(&head_row, heads_row, age, refname,
2523 head_navs_disp)) == -1) {
2524 error = got_error_from_errno("asprintf");
2525 goto done;
2528 error = buf_puts(&newsize, diffbuf, head_row);
2530 free(head_navs_disp);
2531 free(head_row);
2534 if (buf_len(diffbuf) > 0) {
2535 error = buf_putc(diffbuf, '\0');
2536 heads = strdup(buf_get(diffbuf));
2538 done:
2539 buf_free(diffbuf);
2540 got_ref_list_free(&refs);
2541 if (repo)
2542 got_repo_close(repo);
2543 if (error)
2544 return NULL;
2545 else
2546 return heads;
2549 static char *
2550 gw_get_got_link(struct gw_trans *gw_trans)
2552 char *link;
2554 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2555 gw_trans->gw_conf->got_logo)) == -1)
2556 return NULL;
2558 return link;
2561 static char *
2562 gw_get_site_link(struct gw_trans *gw_trans)
2564 char *link, *repo = "", *action = "";
2566 if (gw_trans->repo_name != NULL)
2567 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2568 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2569 return NULL;
2571 if (gw_trans->action_name != NULL)
2572 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2573 return NULL;
2575 if ((asprintf(&link, site_link, GOTWEB,
2576 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2577 return NULL;
2579 return link;
2582 static char *
2583 gw_colordiff_line(char *buf)
2585 const struct got_error *error = NULL;
2586 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2587 struct buf *diffbuf = NULL;
2588 size_t newsize;
2590 error = buf_alloc(&diffbuf, 0);
2591 if (error)
2592 return NULL;
2594 if (buf == NULL)
2595 return NULL;
2596 if (strncmp(buf, "-", 1) == 0)
2597 color = "diff_minus";
2598 if (strncmp(buf, "+", 1) == 0)
2599 color = "diff_plus";
2600 if (strncmp(buf, "@@", 2) == 0)
2601 color = "diff_chunk_header";
2602 if (strncmp(buf, "@@", 2) == 0)
2603 color = "diff_chunk_header";
2604 if (strncmp(buf, "commit +", 8) == 0)
2605 color = "diff_meta";
2606 if (strncmp(buf, "commit -", 8) == 0)
2607 color = "diff_meta";
2608 if (strncmp(buf, "blob +", 6) == 0)
2609 color = "diff_meta";
2610 if (strncmp(buf, "blob -", 6) == 0)
2611 color = "diff_meta";
2612 if (strncmp(buf, "file +", 6) == 0)
2613 color = "diff_meta";
2614 if (strncmp(buf, "file -", 6) == 0)
2615 color = "diff_meta";
2616 if (strncmp(buf, "from:", 5) == 0)
2617 color = "diff_author";
2618 if (strncmp(buf, "via:", 4) == 0)
2619 color = "diff_author";
2620 if (strncmp(buf, "date:", 5) == 0)
2621 color = "diff_date";
2623 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2624 return NULL;
2626 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2627 if (error)
2628 return NULL;
2630 error = buf_puts(&newsize, diffbuf, buf);
2631 if (error)
2632 return NULL;
2634 if (buf_len(diffbuf) > 0) {
2635 error = buf_putc(diffbuf, '\0');
2636 colorized_line = strdup(buf_get(diffbuf));
2639 free(diffbuf);
2640 free(div_diff_line_div);
2641 return colorized_line;
2644 static char *
2645 gw_html_escape(const char *html)
2647 char *escaped_str = NULL, *buf;
2648 char c[1];
2649 size_t sz, i, buff_sz = 2048;
2651 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2652 return NULL;
2654 if (html == NULL)
2655 return NULL;
2656 else
2657 if ((sz = strlen(html)) == 0)
2658 return NULL;
2660 /* only work with buff_sz */
2661 if (buff_sz < sz)
2662 sz = buff_sz;
2664 for (i = 0; i < sz; i++) {
2665 c[0] = html[i];
2666 switch (c[0]) {
2667 case ('>'):
2668 strcat(buf, "&gt;");
2669 break;
2670 case ('&'):
2671 strcat(buf, "&amp;");
2672 break;
2673 case ('<'):
2674 strcat(buf, "&lt;");
2675 break;
2676 case ('"'):
2677 strcat(buf, "&quot;");
2678 break;
2679 case ('\''):
2680 strcat(buf, "&apos;");
2681 break;
2682 case ('\n'):
2683 strcat(buf, "<br />");
2684 default:
2685 strcat(buf, &c[0]);
2686 break;
2689 asprintf(&escaped_str, "%s", buf);
2690 free(buf);
2691 return escaped_str;
2694 int
2695 main(int argc, char *argv[])
2697 const struct got_error *error = NULL;
2698 struct gw_trans *gw_trans;
2699 struct gw_dir *dir = NULL, *tdir;
2700 const char *page = "index";
2701 int gw_malloc = 1;
2702 enum kcgi_err kerr;
2704 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2705 errx(1, "malloc");
2707 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2708 errx(1, "malloc");
2710 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2711 errx(1, "malloc");
2713 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2714 errx(1, "malloc");
2716 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2717 if (kerr != KCGI_OK) {
2718 error = gw_kcgi_error(kerr);
2719 goto done;
2722 if ((gw_trans->gw_conf =
2723 malloc(sizeof(struct gotweb_conf))) == NULL) {
2724 gw_malloc = 0;
2725 error = got_error_from_errno("malloc");
2726 goto done;
2729 TAILQ_INIT(&gw_trans->gw_dirs);
2730 TAILQ_INIT(&gw_trans->gw_headers);
2732 gw_trans->page = 0;
2733 gw_trans->repos_total = 0;
2734 gw_trans->repo_path = NULL;
2735 gw_trans->commit = NULL;
2736 gw_trans->headref = strdup(GOT_REF_HEAD);
2737 gw_trans->mime = KMIME_TEXT_HTML;
2738 gw_trans->gw_tmpl->key = gw_templs;
2739 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2740 gw_trans->gw_tmpl->arg = gw_trans;
2741 gw_trans->gw_tmpl->cb = gw_template;
2742 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2743 if (error)
2744 goto done;
2746 error = gw_parse_querystring(gw_trans);
2747 if (error)
2748 goto done;
2750 error = gw_display_index(gw_trans, error);
2751 done:
2752 if (error) {
2753 gw_trans->mime = KMIME_TEXT_PLAIN;
2754 gw_trans->action = GW_ERR;
2755 gw_display_index(gw_trans, error);
2757 if (gw_malloc) {
2758 free(gw_trans->gw_conf->got_repos_path);
2759 free(gw_trans->gw_conf->got_www_path);
2760 free(gw_trans->gw_conf->got_site_name);
2761 free(gw_trans->gw_conf->got_site_owner);
2762 free(gw_trans->gw_conf->got_site_link);
2763 free(gw_trans->gw_conf->got_logo);
2764 free(gw_trans->gw_conf->got_logo_url);
2765 free(gw_trans->gw_conf);
2766 free(gw_trans->commit);
2767 free(gw_trans->repo_path);
2768 free(gw_trans->repo_name);
2769 free(gw_trans->repo_file);
2770 free(gw_trans->action_name);
2771 free(gw_trans->headref);
2773 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2774 free(dir->name);
2775 free(dir->description);
2776 free(dir->age);
2777 free(dir->url);
2778 free(dir->path);
2779 free(dir);
2784 khttp_free(gw_trans->gw_req);
2785 return EXIT_SUCCESS;