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 if (header)
412 got_ref_list_free(&header->refs);
413 gw_free_headers(header);
414 free(diff_html_disp);
415 free(diff_html);
416 free(diff);
417 return error;
420 static const struct got_error *
421 gw_index(struct gw_trans *gw_trans)
423 const struct got_error *error = NULL;
424 struct gw_dir *gw_dir = NULL;
425 char *html, *navs, *next, *prev;
426 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
427 enum kcgi_err kerr;
429 if (pledge("stdio rpath proc exec sendfd unveil",
430 NULL) == -1) {
431 error = got_error_from_errno("pledge");
432 return error;
435 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
436 if (error)
437 return error;
439 error = gw_load_got_paths(gw_trans);
440 if (error)
441 return error;
443 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
444 if (kerr != KCGI_OK)
445 return gw_kcgi_error(kerr);
447 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
448 if (asprintf(&html, index_projects_empty,
449 gw_trans->gw_conf->got_repos_path) == -1)
450 return got_error_from_errno("asprintf");
451 kerr = khttp_puts(gw_trans->gw_req, html);
452 if (kerr != KCGI_OK)
453 error = gw_kcgi_error(kerr);
454 free(html);
455 return error;
458 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
459 dir_c++;
461 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
462 if (gw_trans->page > 0 && (gw_trans->page *
463 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
464 prev_disp++;
465 continue;
468 prev_disp++;
470 if (error)
471 return error;
472 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
473 gw_dir->name, gw_dir->name)) == -1)
474 return got_error_from_errno("asprintf");
476 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
477 gw_dir->description, gw_dir->owner, gw_dir->age,
478 navs)) == -1)
479 return got_error_from_errno("asprintf");
481 kerr = khttp_puts(gw_trans->gw_req, html);
482 free(navs);
483 free(html);
484 if (kerr != KCGI_OK)
485 return gw_kcgi_error(kerr);
487 if (gw_trans->gw_conf->got_max_repos_display == 0)
488 continue;
490 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
491 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
492 if (kerr != KCGI_OK)
493 return gw_kcgi_error(kerr);
494 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
495 (gw_trans->page > 0) &&
496 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
497 prev_disp == gw_trans->repos_total)) {
498 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
499 if (kerr != KCGI_OK)
500 return gw_kcgi_error(kerr);
503 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
504 (gw_trans->page > 0) &&
505 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
506 prev_disp == gw_trans->repos_total)) {
507 if ((asprintf(&prev, nav_prev,
508 gw_trans->page - 1)) == -1)
509 return got_error_from_errno("asprintf");
510 kerr = khttp_puts(gw_trans->gw_req, prev);
511 free(prev);
512 if (kerr != KCGI_OK)
513 return gw_kcgi_error(kerr);
516 kerr = khttp_puts(gw_trans->gw_req, div_end);
517 if (kerr != KCGI_OK)
518 return gw_kcgi_error(kerr);
520 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
521 next_disp == gw_trans->gw_conf->got_max_repos_display &&
522 dir_c != (gw_trans->page + 1) *
523 gw_trans->gw_conf->got_max_repos_display) {
524 if ((asprintf(&next, nav_next,
525 gw_trans->page + 1)) == -1)
526 return got_error_from_errno("calloc");
527 kerr = khttp_puts(gw_trans->gw_req, next);
528 free(next);
529 if (kerr != KCGI_OK)
530 return gw_kcgi_error(kerr);
531 kerr = khttp_puts(gw_trans->gw_req, div_end);
532 if (kerr != KCGI_OK)
533 error = gw_kcgi_error(kerr);
534 next_disp = 0;
535 break;
538 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
539 (gw_trans->page > 0) &&
540 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
541 prev_disp == gw_trans->repos_total)) {
542 kerr = khttp_puts(gw_trans->gw_req, div_end);
543 if (kerr != KCGI_OK)
544 return gw_kcgi_error(kerr);
547 next_disp++;
549 return error;
552 static const struct got_error *
553 gw_commits(struct gw_trans *gw_trans)
555 const struct got_error *error = NULL;
556 char *commits_html, *commits_navs_html;
557 struct gw_header *header = NULL, *n_header = NULL;
558 enum kcgi_err kerr;
560 if ((header = gw_init_header()) == NULL)
561 return got_error_from_errno("malloc");
563 if (pledge("stdio rpath proc exec sendfd unveil",
564 NULL) == -1) {
565 error = got_error_from_errno("pledge");
566 return error;
569 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
570 if (error)
571 return error;
573 error = gw_get_header(gw_trans, header,
574 gw_trans->gw_conf->got_max_commits_display);
576 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
577 if (kerr != KCGI_OK)
578 return gw_kcgi_error(kerr);
579 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
580 if ((asprintf(&commits_navs_html, commits_navs,
581 gw_trans->repo_name, n_header->commit_id,
582 gw_trans->repo_name, n_header->commit_id,
583 gw_trans->repo_name, n_header->commit_id)) == -1)
584 return got_error_from_errno("asprintf");
586 if ((asprintf(&commits_html, commits_line,
587 gw_gen_commit_header(n_header->commit_id,
588 n_header->refs_str),
589 gw_gen_author_header(n_header->author),
590 gw_gen_committer_header(n_header->committer),
591 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
592 TM_LONG)), gw_html_escape(n_header->commit_msg),
593 commits_navs_html)) == -1)
594 return got_error_from_errno("asprintf");
595 kerr = khttp_puts(gw_trans->gw_req, commits_html);
596 if (kerr != KCGI_OK)
597 return gw_kcgi_error(kerr);
599 kerr = khttp_puts(gw_trans->gw_req, div_end);
600 if (kerr != KCGI_OK)
601 error = gw_kcgi_error(kerr);
603 if (header)
604 got_ref_list_free(&header->refs);
605 gw_free_headers(header);
606 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
607 gw_free_headers(n_header);
608 return error;
611 static const struct got_error *
612 gw_briefs(struct gw_trans *gw_trans)
614 const struct got_error *error = NULL;
615 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
616 struct gw_header *header = NULL, *n_header = NULL;
617 enum kcgi_err kerr;
619 if ((header = gw_init_header()) == NULL)
620 return got_error_from_errno("malloc");
622 if (pledge("stdio rpath proc exec sendfd unveil",
623 NULL) == -1) {
624 error = got_error_from_errno("pledge");
625 return error;
628 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
629 if (error)
630 return error;
632 if (gw_trans->action == GW_SUMMARY)
633 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
634 else
635 error = gw_get_header(gw_trans, header,
636 gw_trans->gw_conf->got_max_commits_display);
638 if (error)
639 return error;
641 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
642 if (kerr != KCGI_OK)
643 return gw_kcgi_error(kerr);
645 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
646 if ((asprintf(&briefs_navs_html, briefs_navs,
647 gw_trans->repo_name, n_header->commit_id,
648 gw_trans->repo_name, n_header->commit_id,
649 gw_trans->repo_name, n_header->commit_id)) == -1)
650 return got_error_from_errno("asprintf");
651 newline = strchr(n_header->commit_msg, '\n');
652 if (newline)
653 *newline = '\0';
654 if ((asprintf(&briefs_html, briefs_line,
655 gw_get_time_str(n_header->committer_time, TM_DIFF),
656 n_header->author, gw_html_escape(n_header->commit_msg),
657 briefs_navs_html)) == -1)
658 return got_error_from_errno("asprintf");
659 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
660 if (kerr != KCGI_OK)
661 return gw_kcgi_error(kerr);
663 kerr = khttp_puts(gw_trans->gw_req, div_end);
664 if (kerr != KCGI_OK)
665 error = gw_kcgi_error(kerr);
667 if (header)
668 got_ref_list_free(&header->refs);
669 gw_free_headers(header);
670 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
671 gw_free_headers(n_header);
672 return error;
675 static const struct got_error *
676 gw_summary(struct gw_trans *gw_trans)
678 const struct got_error *error = NULL;
679 char *description_html, *repo_owner_html, *repo_age_html,
680 *cloneurl_html, *tags, *heads, *tags_html,
681 *heads_html, *age;
682 enum kcgi_err kerr;
684 if (pledge("stdio rpath proc exec sendfd unveil",
685 NULL) == -1) {
686 error = got_error_from_errno("pledge");
687 return error;
690 /* unveil is applied with gw_briefs below */
692 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
693 if (kerr != KCGI_OK)
694 return gw_kcgi_error(kerr);
696 if (gw_trans->gw_conf->got_show_repo_description) {
697 if (gw_trans->gw_dir->description != NULL &&
698 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
699 if ((asprintf(&description_html, description,
700 gw_trans->gw_dir->description)) == -1)
701 return got_error_from_errno("asprintf");
703 kerr = khttp_puts(gw_trans->gw_req, description_html);
704 free(description_html);
705 if (kerr != KCGI_OK)
706 return gw_kcgi_error(kerr);
710 if (gw_trans->gw_conf->got_show_repo_owner) {
711 if (gw_trans->gw_dir->owner != NULL &&
712 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
713 if ((asprintf(&repo_owner_html, repo_owner,
714 gw_trans->gw_dir->owner)) == -1)
715 return got_error_from_errno("asprintf");
717 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
718 free(repo_owner_html);
719 if (kerr != KCGI_OK)
720 return gw_kcgi_error(kerr);
724 if (gw_trans->gw_conf->got_show_repo_age) {
725 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
726 "refs/heads", TM_LONG);
727 if (age != NULL && (strcmp(age, "") != 0)) {
728 if ((asprintf(&repo_age_html, last_change, age)) == -1)
729 return got_error_from_errno("asprintf");
731 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
732 free(repo_age_html);
733 free(age);
734 if (kerr != KCGI_OK)
735 return gw_kcgi_error(kerr);
739 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
740 if (gw_trans->gw_dir->url != NULL &&
741 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
742 if ((asprintf(&cloneurl_html, cloneurl,
743 gw_trans->gw_dir->url)) == -1)
744 return got_error_from_errno("asprintf");
746 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
747 free(cloneurl_html);
748 if (kerr != KCGI_OK)
749 return gw_kcgi_error(kerr);
752 kerr = khttp_puts(gw_trans->gw_req, div_end);
753 if (kerr != KCGI_OK)
754 return gw_kcgi_error(kerr);
756 error = gw_briefs(gw_trans);
757 if (error)
758 return error;
760 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
761 heads = gw_get_repo_heads(gw_trans);
763 if (tags != NULL && strcmp(tags, "") != 0) {
764 if ((asprintf(&tags_html, summary_tags,
765 tags)) == -1)
766 return got_error_from_errno("asprintf");
767 kerr = khttp_puts(gw_trans->gw_req, tags_html);
768 free(tags_html);
769 free(tags);
770 if (kerr != KCGI_OK)
771 return gw_kcgi_error(kerr);
774 if (heads != NULL && strcmp(heads, "") != 0) {
775 if ((asprintf(&heads_html, summary_heads,
776 heads)) == -1)
777 return got_error_from_errno("asprintf");
778 kerr = khttp_puts(gw_trans->gw_req, heads_html);
779 free(heads_html);
780 free(heads);
781 if (kerr != KCGI_OK)
782 return gw_kcgi_error(kerr);
784 return error;
787 static const struct got_error *
788 gw_tree(struct gw_trans *gw_trans)
790 const struct got_error *error = NULL;
791 struct gw_header *header = NULL;
792 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
793 enum kcgi_err kerr;
795 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
796 return got_error_from_errno("pledge");
798 if ((header = gw_init_header()) == NULL)
799 return got_error_from_errno("malloc");
801 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
802 if (error)
803 return error;
805 error = gw_get_header(gw_trans, header, 1);
806 if (error)
807 return error;
809 tree_html = gw_get_repo_tree(gw_trans);
811 if (tree_html == NULL)
812 tree_html = strdup("");
814 if ((asprintf(&tree_html_disp, tree_header,
815 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
816 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
817 tree_html)) == -1)
818 return got_error_from_errno("asprintf");
820 if ((asprintf(&tree, tree_wrapper, tree_html_disp)) == -1)
821 return got_error_from_errno("asprintf");
823 kerr = khttp_puts(gw_trans->gw_req, tree);
824 if (kerr != KCGI_OK)
825 error = gw_kcgi_error(kerr);
826 if (header)
827 got_ref_list_free(&header->refs);
828 gw_free_headers(header);
829 free(tree_html_disp);
830 free(tree_html);
831 free(tree);
832 return error;
835 static const struct got_error *
836 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
838 const struct got_error *error = NULL;
839 DIR *dt;
840 char *dir_test;
841 int opened = 0;
843 if ((asprintf(&dir_test, "%s/%s/%s",
844 gw_trans->gw_conf->got_repos_path, gw_dir->name,
845 GOTWEB_GIT_DIR)) == -1)
846 return got_error_from_errno("asprintf");
848 dt = opendir(dir_test);
849 if (dt == NULL) {
850 free(dir_test);
851 } else {
852 gw_dir->path = strdup(dir_test);
853 opened = 1;
854 goto done;
857 if ((asprintf(&dir_test, "%s/%s/%s",
858 gw_trans->gw_conf->got_repos_path, gw_dir->name,
859 GOTWEB_GOT_DIR)) == -1)
860 return got_error_from_errno("asprintf");
862 dt = opendir(dir_test);
863 if (dt == NULL)
864 free(dir_test);
865 else {
866 opened = 1;
867 error = got_error(GOT_ERR_NOT_GIT_REPO);
868 goto errored;
871 if ((asprintf(&dir_test, "%s/%s",
872 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
873 return got_error_from_errno("asprintf");
875 gw_dir->path = strdup(dir_test);
877 done:
878 gw_dir->description = gw_get_repo_description(gw_trans,
879 gw_dir->path);
880 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
881 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
882 TM_DIFF);
883 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
885 errored:
886 free(dir_test);
887 if (opened)
888 closedir(dt);
889 return error;
892 static const struct got_error *
893 gw_load_got_paths(struct gw_trans *gw_trans)
895 const struct got_error *error = NULL;
896 DIR *d;
897 struct dirent **sd_dent;
898 struct gw_dir *gw_dir;
899 struct stat st;
900 unsigned int d_cnt, d_i;
902 d = opendir(gw_trans->gw_conf->got_repos_path);
903 if (d == NULL) {
904 error = got_error_from_errno2("opendir",
905 gw_trans->gw_conf->got_repos_path);
906 return error;
909 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
910 alphasort);
911 if (d_cnt == -1) {
912 error = got_error_from_errno2("scandir",
913 gw_trans->gw_conf->got_repos_path);
914 return error;
917 for (d_i = 0; d_i < d_cnt; d_i++) {
918 if (gw_trans->gw_conf->got_max_repos > 0 &&
919 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
920 break; /* account for parent and self */
922 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
923 strcmp(sd_dent[d_i]->d_name, "..") == 0)
924 continue;
926 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
927 return got_error_from_errno("gw_dir malloc");
929 error = gw_load_got_path(gw_trans, gw_dir);
930 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
931 continue;
932 else if (error)
933 return error;
935 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
936 !got_path_dir_is_empty(gw_dir->path)) {
937 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
938 entry);
939 gw_trans->repos_total++;
943 closedir(d);
944 return error;
947 static const struct got_error *
948 gw_parse_querystring(struct gw_trans *gw_trans)
950 const struct got_error *error = NULL;
951 struct kpair *p;
952 struct gw_query_action *action = NULL;
953 unsigned int i;
955 if (gw_trans->gw_req->fieldnmap[0]) {
956 error = got_error_from_errno("bad parse");
957 return error;
958 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
959 /* define gw_trans->repo_path */
960 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
961 return got_error_from_errno("asprintf");
963 if ((asprintf(&gw_trans->repo_path, "%s/%s",
964 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
965 return got_error_from_errno("asprintf");
967 /* get action and set function */
968 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
969 for (i = 0; i < nitems(gw_query_funcs); i++) {
970 action = &gw_query_funcs[i];
971 if (action->func_name == NULL)
972 continue;
974 if (strcmp(action->func_name,
975 p->parsed.s) == 0) {
976 gw_trans->action = i;
977 if ((asprintf(&gw_trans->action_name,
978 "%s", action->func_name)) == -1)
979 return
980 got_error_from_errno(
981 "asprintf");
983 break;
986 action = NULL;
989 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
990 if ((asprintf(&gw_trans->commit, "%s",
991 p->parsed.s)) == -1)
992 return got_error_from_errno("asprintf");
994 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
995 if ((asprintf(&gw_trans->repo_file, "%s",
996 p->parsed.s)) == -1)
997 return got_error_from_errno("asprintf");
999 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1000 if ((asprintf(&gw_trans->repo_folder, "%s",
1001 p->parsed.s)) == -1)
1002 return got_error_from_errno("asprintf");
1004 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1005 if ((asprintf(&gw_trans->headref, "%s",
1006 p->parsed.s)) == -1)
1007 return got_error_from_errno("asprintf");
1009 if (action == NULL) {
1010 error = got_error_from_errno("invalid action");
1011 return error;
1013 if ((gw_trans->gw_dir =
1014 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1015 return got_error_from_errno("gw_dir malloc");
1017 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1018 if (error)
1019 return error;
1020 } else
1021 gw_trans->action = GW_INDEX;
1023 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1024 gw_trans->page = p->parsed.i;
1026 /* if (gw_trans->action == GW_RAW) */
1027 /* gw_trans->mime = KMIME_TEXT_PLAIN; */
1029 return error;
1032 static struct gw_dir *
1033 gw_init_gw_dir(char *dir)
1035 struct gw_dir *gw_dir;
1037 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1038 return NULL;
1040 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1041 return NULL;
1043 return gw_dir;
1046 static const struct got_error *
1047 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1049 enum kcgi_err kerr;
1051 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1052 if (kerr != KCGI_OK)
1053 return gw_kcgi_error(kerr);
1054 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1055 khttps[code]);
1056 if (kerr != KCGI_OK)
1057 return gw_kcgi_error(kerr);
1058 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1059 kmimetypes[mime]);
1060 if (kerr != KCGI_OK)
1061 return gw_kcgi_error(kerr);
1062 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1063 if (kerr != KCGI_OK)
1064 return gw_kcgi_error(kerr);
1065 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1066 if (kerr != KCGI_OK)
1067 return gw_kcgi_error(kerr);
1068 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1069 if (kerr != KCGI_OK)
1070 return gw_kcgi_error(kerr);
1072 kerr = khttp_body(gw_trans->gw_req);
1073 return gw_kcgi_error(kerr);
1076 static const struct got_error *
1077 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
1079 enum kcgi_err kerr;
1081 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1082 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1084 if (err)
1085 kerr = khttp_puts(gw_trans->gw_req, err->msg);
1086 else
1087 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1088 gw_query_funcs[gw_trans->action].template);
1089 if (kerr != KCGI_OK)
1090 return gw_kcgi_error(kerr);
1092 kerr = khtml_close(gw_trans->gw_html_req);
1093 return gw_kcgi_error(kerr);
1096 static int
1097 gw_template(size_t key, void *arg)
1099 const struct got_error *error = NULL;
1100 struct gw_trans *gw_trans = arg;
1101 char *gw_got_link, *gw_site_link;
1102 char *site_owner_name, *site_owner_name_h;
1104 switch (key) {
1105 case (TEMPL_HEAD):
1106 khttp_puts(gw_trans->gw_req, head);
1107 break;
1108 case(TEMPL_HEADER):
1109 gw_got_link = gw_get_got_link(gw_trans);
1110 if (gw_got_link != NULL)
1111 khttp_puts(gw_trans->gw_req, gw_got_link);
1113 free(gw_got_link);
1114 break;
1115 case (TEMPL_SITEPATH):
1116 gw_site_link = gw_get_site_link(gw_trans);
1117 if (gw_site_link != NULL)
1118 khttp_puts(gw_trans->gw_req, gw_site_link);
1120 free(gw_site_link);
1121 break;
1122 case(TEMPL_TITLE):
1123 if (gw_trans->gw_conf->got_site_name != NULL)
1124 khtml_puts(gw_trans->gw_html_req,
1125 gw_trans->gw_conf->got_site_name);
1127 break;
1128 case (TEMPL_SEARCH):
1129 khttp_puts(gw_trans->gw_req, search);
1130 break;
1131 case(TEMPL_SITEOWNER):
1132 if (gw_trans->gw_conf->got_site_owner != NULL &&
1133 gw_trans->gw_conf->got_show_site_owner) {
1134 site_owner_name =
1135 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1136 if ((asprintf(&site_owner_name_h, site_owner,
1137 site_owner_name))
1138 == -1)
1139 return 0;
1141 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1142 free(site_owner_name);
1143 free(site_owner_name_h);
1145 break;
1146 case(TEMPL_CONTENT):
1147 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1148 if (error)
1149 khttp_puts(gw_trans->gw_req, error->msg);
1150 break;
1151 default:
1152 return 0;
1154 return 1;
1157 static char *
1158 gw_gen_commit_header(char *str1, char *str2)
1160 char *return_html = NULL, *ref_str = NULL;
1162 if (strcmp(str2, "") != 0) {
1163 if ((asprintf(&ref_str, "(%s)", str2)) == -1) {
1164 return_html = strdup("");
1165 return return_html;
1167 } else
1168 ref_str = strdup("");
1171 if ((asprintf(&return_html, header_commit_html, str1, ref_str)) == -1)
1172 return_html = strdup("");
1174 free(ref_str);
1175 return return_html;
1178 static char *
1179 gw_gen_diff_header(char *str1, char *str2)
1181 char *return_html = NULL;
1183 if ((asprintf(&return_html, header_diff_html, str1, str2)) == -1)
1184 return_html = strdup("");
1186 return return_html;
1189 static char *
1190 gw_gen_author_header(char *str)
1192 char *return_html = NULL;
1194 if ((asprintf(&return_html, header_author_html, str)) == -1)
1195 return_html = strdup("");
1197 return return_html;
1200 static char *
1201 gw_gen_committer_header(char *str)
1203 char *return_html = NULL;
1205 if ((asprintf(&return_html, header_committer_html, str)) == -1)
1206 return_html = strdup("");
1208 return return_html;
1211 static char *
1212 gw_gen_age_header(char *str)
1214 char *return_html = NULL;
1216 if ((asprintf(&return_html, header_age_html, str)) == -1)
1217 return_html = strdup("");
1219 return return_html;
1222 static char *
1223 gw_gen_commit_msg_header(char *str)
1225 char *return_html = NULL;
1227 if ((asprintf(&return_html, header_commit_msg_html, str)) == -1)
1228 return_html = strdup("");
1230 return return_html;
1233 static char *
1234 gw_gen_tree_header(char *str)
1236 char *return_html = NULL;
1238 if ((asprintf(&return_html, header_tree_html, str)) == -1)
1239 return_html = strdup("");
1241 return return_html;
1244 static char *
1245 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1247 FILE *f;
1248 char *description = NULL, *d_file = NULL;
1249 unsigned int len;
1251 if (gw_trans->gw_conf->got_show_repo_description == false)
1252 goto err;
1254 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1255 goto err;
1257 if ((f = fopen(d_file, "r")) == NULL)
1258 goto err;
1260 fseek(f, 0, SEEK_END);
1261 len = ftell(f) + 1;
1262 fseek(f, 0, SEEK_SET);
1263 if ((description = calloc(len, sizeof(char *))) == NULL)
1264 goto err;
1266 fread(description, 1, len, f);
1267 fclose(f);
1268 free(d_file);
1269 return description;
1270 err:
1271 if ((asprintf(&description, "%s", "")) == -1)
1272 return NULL;
1274 return description;
1277 static char *
1278 gw_get_time_str(time_t committer_time, int ref_tm)
1280 struct tm tm;
1281 time_t diff_time;
1282 char *years = "years ago", *months = "months ago";
1283 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1284 char *minutes = "minutes ago", *seconds = "seconds ago";
1285 char *now = "right now";
1286 char *repo_age, *s;
1287 char datebuf[29];
1289 switch (ref_tm) {
1290 case TM_DIFF:
1291 diff_time = time(NULL) - committer_time;
1292 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1293 if ((asprintf(&repo_age, "%lld %s",
1294 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1295 return NULL;
1296 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1297 if ((asprintf(&repo_age, "%lld %s",
1298 (diff_time / 60 / 60 / 24 / (365 / 12)),
1299 months)) == -1)
1300 return NULL;
1301 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1302 if ((asprintf(&repo_age, "%lld %s",
1303 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1304 return NULL;
1305 } else if (diff_time > 60 * 60 * 24 * 2) {
1306 if ((asprintf(&repo_age, "%lld %s",
1307 (diff_time / 60 / 60 / 24), days)) == -1)
1308 return NULL;
1309 } else if (diff_time > 60 * 60 * 2) {
1310 if ((asprintf(&repo_age, "%lld %s",
1311 (diff_time / 60 / 60), hours)) == -1)
1312 return NULL;
1313 } else if (diff_time > 60 * 2) {
1314 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1315 minutes)) == -1)
1316 return NULL;
1317 } else if (diff_time > 2) {
1318 if ((asprintf(&repo_age, "%lld %s", diff_time,
1319 seconds)) == -1)
1320 return NULL;
1321 } else {
1322 if ((asprintf(&repo_age, "%s", now)) == -1)
1323 return NULL;
1325 break;
1326 case TM_LONG:
1327 if (gmtime_r(&committer_time, &tm) == NULL)
1328 return NULL;
1330 s = asctime_r(&tm, datebuf);
1331 if (s == NULL)
1332 return NULL;
1334 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1335 return NULL;
1336 break;
1338 return repo_age;
1341 static char *
1342 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1343 int ref_tm)
1345 const struct got_error *error = NULL;
1346 struct got_object_id *id = NULL;
1347 struct got_repository *repo = NULL;
1348 struct got_commit_object *commit = NULL;
1349 struct got_reflist_head refs;
1350 struct got_reflist_entry *re;
1351 struct got_reference *head_ref;
1352 int is_head = 0;
1353 time_t committer_time = 0, cmp_time = 0;
1354 const char *refname;
1355 char *repo_age = NULL;
1357 if (repo_ref == NULL)
1358 return NULL;
1360 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1361 is_head = 1;
1363 SIMPLEQ_INIT(&refs);
1364 if (gw_trans->gw_conf->got_show_repo_age == false) {
1365 if ((asprintf(&repo_age, "")) == -1)
1366 return NULL;
1367 return repo_age;
1370 error = got_repo_open(&repo, dir, NULL);
1371 if (error)
1372 goto err;
1374 if (is_head)
1375 error = got_ref_list(&refs, repo, "refs/heads",
1376 got_ref_cmp_by_name, NULL);
1377 else
1378 error = got_ref_list(&refs, repo, repo_ref,
1379 got_ref_cmp_by_name, NULL);
1380 if (error)
1381 goto err;
1383 SIMPLEQ_FOREACH(re, &refs, entry) {
1384 if (is_head)
1385 refname = strdup(repo_ref);
1386 else
1387 refname = got_ref_get_name(re->ref);
1388 error = got_ref_open(&head_ref, repo, refname, 0);
1389 if (error)
1390 goto err;
1392 error = got_ref_resolve(&id, repo, head_ref);
1393 got_ref_close(head_ref);
1394 if (error)
1395 goto err;
1397 error = got_object_open_as_commit(&commit, repo, id);
1398 if (error)
1399 goto err;
1401 committer_time =
1402 got_object_commit_get_committer_time(commit);
1404 if (cmp_time < committer_time)
1405 cmp_time = committer_time;
1408 if (cmp_time != 0) {
1409 committer_time = cmp_time;
1410 repo_age = gw_get_time_str(committer_time, ref_tm);
1411 } else
1412 if ((asprintf(&repo_age, "")) == -1)
1413 return NULL;
1414 got_ref_list_free(&refs);
1415 free(id);
1416 return repo_age;
1417 err:
1418 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1419 return NULL;
1421 return repo_age;
1424 static char *
1425 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1427 const struct got_error *error;
1428 FILE *f = NULL;
1429 struct got_object_id *id1 = NULL, *id2 = NULL;
1430 struct buf *diffbuf = NULL;
1431 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1432 *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1433 int obj_type;
1434 size_t newsize;
1436 f = got_opentemp();
1437 if (f == NULL)
1438 return NULL;
1440 error = buf_alloc(&diffbuf, 0);
1441 if (error)
1442 return NULL;
1444 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1445 if (error)
1446 goto done;
1448 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1449 error = got_repo_match_object_id(&id1, &label1,
1450 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1451 if (error)
1452 goto done;
1455 error = got_repo_match_object_id(&id2, &label2,
1456 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1457 if (error)
1458 goto done;
1460 error = got_object_get_type(&obj_type, header->repo, id2);
1461 if (error)
1462 goto done;
1463 switch (obj_type) {
1464 case GOT_OBJ_TYPE_BLOB:
1465 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1466 header->repo, f);
1467 break;
1468 case GOT_OBJ_TYPE_TREE:
1469 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1470 header->repo, f);
1471 break;
1472 case GOT_OBJ_TYPE_COMMIT:
1473 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1474 header->repo, f);
1475 break;
1476 default:
1477 error = got_error(GOT_ERR_OBJ_TYPE);
1480 if ((buf = calloc(128, sizeof(char *))) == NULL)
1481 goto done;
1483 fseek(f, 0, SEEK_SET);
1485 while ((fgets(buf, 2048, f)) != NULL) {
1486 n_buf = buf;
1487 while (*n_buf == '\n')
1488 n_buf++;
1489 newline = strchr(n_buf, '\n');
1490 if (newline)
1491 *newline = ' ';
1493 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1494 if (buf_color == NULL)
1495 continue;
1497 error = buf_puts(&newsize, diffbuf, buf_color);
1498 if (error)
1499 return NULL;
1501 error = buf_puts(&newsize, diffbuf, div_end);
1502 if (error)
1503 return NULL;
1506 if (buf_len(diffbuf) > 0) {
1507 error = buf_putc(diffbuf, '\0');
1508 diff_html = strdup(buf_get(diffbuf));
1510 done:
1511 fclose(f);
1512 free(buf_color);
1513 free(buf);
1514 free(diffbuf);
1515 free(label1);
1516 free(label2);
1517 free(id1);
1518 free(id2);
1520 if (error)
1521 return NULL;
1522 else
1523 return diff_html;
1526 static char *
1527 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1529 FILE *f;
1530 char *owner = NULL, *d_file = NULL;
1531 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1532 char *comp, *pos, *buf;
1533 unsigned int i;
1535 if (gw_trans->gw_conf->got_show_repo_owner == false)
1536 goto err;
1538 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1539 goto err;
1541 if ((f = fopen(d_file, "r")) == NULL)
1542 goto err;
1544 if ((buf = calloc(128, sizeof(char *))) == NULL)
1545 goto err;
1547 while ((fgets(buf, 128, f)) != NULL) {
1548 if ((pos = strstr(buf, gotweb)) != NULL)
1549 break;
1551 if ((pos = strstr(buf, gitweb)) != NULL)
1552 break;
1555 if (pos == NULL)
1556 goto err;
1558 do {
1559 fgets(buf, 128, f);
1560 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1562 if (comp == NULL)
1563 goto err;
1565 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1566 goto err;
1568 for (i = 0; i < 2; i++) {
1569 owner = strsep(&buf, "\"");
1572 if (owner == NULL)
1573 goto err;
1575 fclose(f);
1576 free(d_file);
1577 return owner;
1578 err:
1579 if ((asprintf(&owner, "%s", "")) == -1)
1580 return NULL;
1582 return owner;
1585 static char *
1586 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1588 FILE *f;
1589 char *url = NULL, *d_file = NULL;
1590 unsigned int len;
1592 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1593 return NULL;
1595 if ((f = fopen(d_file, "r")) == NULL)
1596 return NULL;
1598 fseek(f, 0, SEEK_END);
1599 len = ftell(f) + 1;
1600 fseek(f, 0, SEEK_SET);
1602 if ((url = calloc(len, sizeof(char *))) == NULL)
1603 return NULL;
1605 fread(url, 1, len, f);
1606 fclose(f);
1607 free(d_file);
1608 return url;
1611 static char *
1612 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
1614 const struct got_error *error = NULL;
1615 struct got_repository *repo = NULL;
1616 struct got_reflist_head refs;
1617 struct got_reflist_entry *re;
1618 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1619 *age = NULL;
1620 char *newline;
1621 struct buf *diffbuf = NULL;
1622 size_t newsize;
1624 error = buf_alloc(&diffbuf, 0);
1625 if (error)
1626 return NULL;
1627 SIMPLEQ_INIT(&refs);
1629 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1630 if (error)
1631 goto done;
1633 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1634 if (error)
1635 goto done;
1637 SIMPLEQ_FOREACH(re, &refs, entry) {
1638 const char *refname;
1639 char *refstr, *tag_commit0, *tag_commit, *id_str;
1640 time_t tagger_time;
1641 struct got_object_id *id;
1642 struct got_tag_object *tag;
1644 refname = got_ref_get_name(re->ref);
1645 if (strncmp(refname, "refs/tags/", 10) != 0)
1646 continue;
1647 refname += 10;
1648 refstr = got_ref_to_str(re->ref);
1649 if (refstr == NULL) {
1650 error = got_error_from_errno("got_ref_to_str");
1651 goto done;
1654 error = got_ref_resolve(&id, repo, re->ref);
1655 if (error)
1656 goto done;
1657 error = got_object_open_as_tag(&tag, repo, id);
1658 free(id);
1659 if (error)
1660 goto done;
1662 tagger_time = got_object_tag_get_tagger_time(tag);
1664 error = got_object_id_str(&id_str,
1665 got_object_tag_get_object_id(tag));
1666 if (error)
1667 goto done;
1669 tag_commit0 = strdup(got_object_tag_get_message(tag));
1671 if (tag_commit0 == NULL) {
1672 error = got_error_from_errno("strdup");
1673 goto done;
1676 tag_commit = tag_commit0;
1677 while (*tag_commit == '\n')
1678 tag_commit++;
1680 switch (tag_type) {
1681 case TAGBRIEF:
1682 newline = strchr(tag_commit, '\n');
1683 if (newline)
1684 *newline = '\0';
1686 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1687 TM_DIFF))) == -1) {
1688 error = got_error_from_errno("asprintf");
1689 goto done;
1692 if ((asprintf(&tags_navs_disp, tags_navs,
1693 gw_trans->repo_name, id_str, gw_trans->repo_name,
1694 id_str, gw_trans->repo_name, id_str,
1695 gw_trans->repo_name, id_str)) == -1) {
1696 error = got_error_from_errno("asprintf");
1697 goto done;
1700 if ((asprintf(&tag_row, tags_row, age, refname,
1701 tag_commit, tags_navs_disp)) == -1) {
1702 error = got_error_from_errno("asprintf");
1703 goto done;
1706 free(tags_navs_disp);
1707 break;
1708 case TAGFULL:
1709 break;
1710 default:
1711 break;
1714 got_object_tag_close(tag);
1716 error = buf_puts(&newsize, diffbuf, tag_row);
1718 free(id_str);
1719 free(refstr);
1720 free(age);
1721 free(tag_commit0);
1722 free(tag_row);
1724 if (error || (limit && --limit == 0))
1725 break;
1728 if (buf_len(diffbuf) > 0) {
1729 error = buf_putc(diffbuf, '\0');
1730 tags = strdup(buf_get(diffbuf));
1732 done:
1733 buf_free(diffbuf);
1734 got_ref_list_free(&refs);
1735 if (repo)
1736 got_repo_close(repo);
1737 if (error)
1738 return NULL;
1739 else
1740 return tags;
1743 static void
1744 gw_free_headers(struct gw_header *header)
1746 free(header->id);
1747 free(header->path);
1748 if (header->commit != NULL)
1749 got_object_commit_close(header->commit);
1750 if (header->repo)
1751 got_repo_close(header->repo);
1752 free(header->refs_str);
1753 free(header->commit_id);
1754 free(header->parent_id);
1755 free(header->tree_id);
1756 free(header->author);
1757 free(header->committer);
1758 free(header->commit_msg);
1761 static struct gw_header *
1762 gw_init_header()
1764 struct gw_header *header;
1766 header = malloc(sizeof(*header));
1767 if (header == NULL)
1768 return NULL;
1770 header->repo = NULL;
1771 header->commit = NULL;
1772 header->id = NULL;
1773 header->path = NULL;
1774 SIMPLEQ_INIT(&header->refs);
1776 return header;
1779 static const struct got_error *
1780 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1781 int limit)
1783 const struct got_error *error = NULL;
1784 struct got_commit_graph *graph = NULL;
1786 error = got_commit_graph_open(&graph, header->path, 0);
1787 if (error)
1788 goto done;
1790 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1791 NULL, NULL);
1792 if (error)
1793 goto done;
1795 for (;;) {
1796 error = got_commit_graph_iter_next(&header->id, graph,
1797 header->repo, NULL, NULL);
1798 if (error) {
1799 if (error->code == GOT_ERR_ITER_COMPLETED)
1800 error = NULL;
1801 goto done;
1803 if (header->id == NULL)
1804 goto done;
1806 error = got_object_open_as_commit(&header->commit, header->repo,
1807 header->id);
1808 if (error)
1809 goto done;
1811 error = gw_get_commit(gw_trans, header);
1812 if (limit > 1) {
1813 struct gw_header *n_header = NULL;
1814 if ((n_header = gw_init_header()) == NULL)
1815 error = got_error_from_errno("malloc");
1817 n_header->refs_str = strdup(header->refs_str);
1818 n_header->commit_id = strdup(header->commit_id);
1819 n_header->parent_id = strdup(header->parent_id);
1820 n_header->tree_id = strdup(header->tree_id);
1821 n_header->author = strdup(header->author);
1822 n_header->committer = strdup(header->committer);
1823 n_header->commit_msg = strdup(header->commit_msg);
1824 n_header->committer_time = header->committer_time;
1825 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
1826 entry);
1828 if (error || (limit && --limit == 0))
1829 break;
1831 done:
1832 if (graph)
1833 got_commit_graph_close(graph);
1834 return error;
1837 static const struct got_error *
1838 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
1840 const struct got_error *error = NULL;
1841 struct got_reflist_entry *re;
1842 struct got_object_id *id2 = NULL;
1843 struct got_object_qid *parent_id;
1844 char *refs_str = NULL,
1845 *commit_msg = NULL, *commit_msg0;
1847 /*print commit*/
1848 SIMPLEQ_FOREACH(re, &header->refs, entry) {
1849 char *s;
1850 const char *name;
1851 struct got_tag_object *tag = NULL;
1852 int cmp;
1854 name = got_ref_get_name(re->ref);
1855 if (strcmp(name, GOT_REF_HEAD) == 0)
1856 continue;
1857 if (strncmp(name, "refs/", 5) == 0)
1858 name += 5;
1859 if (strncmp(name, "got/", 4) == 0)
1860 continue;
1861 if (strncmp(name, "heads/", 6) == 0)
1862 name += 6;
1863 if (strncmp(name, "remotes/", 8) == 0)
1864 name += 8;
1865 if (strncmp(name, "tags/", 5) == 0) {
1866 error = got_object_open_as_tag(&tag, header->repo,
1867 re->id);
1868 if (error) {
1869 if (error->code != GOT_ERR_OBJ_TYPE)
1870 continue;
1872 * Ref points at something other
1873 * than a tag.
1875 error = NULL;
1876 tag = NULL;
1879 cmp = got_object_id_cmp(tag ?
1880 got_object_tag_get_object_id(tag) : re->id, header->id);
1881 if (tag)
1882 got_object_tag_close(tag);
1883 if (cmp != 0)
1884 continue;
1885 s = refs_str;
1886 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1887 s ? ", " : "", name)) == -1) {
1888 error = got_error_from_errno("asprintf");
1889 free(s);
1890 return error;
1892 header->refs_str = strdup(refs_str);
1893 free(s);
1896 if (refs_str == NULL)
1897 header->refs_str = strdup("");
1898 free(refs_str);
1900 error = got_object_id_str(&header->commit_id, header->id);
1901 if (error)
1902 return error;
1904 error = got_object_id_str(&header->tree_id,
1905 got_object_commit_get_tree_id(header->commit));
1906 if (error)
1907 return error;
1909 if (gw_trans->action == GW_DIFF) {
1910 parent_id = SIMPLEQ_FIRST(
1911 got_object_commit_get_parent_ids(header->commit));
1912 if (parent_id != NULL) {
1913 id2 = got_object_id_dup(parent_id->id);
1914 free (parent_id);
1915 error = got_object_id_str(&header->parent_id, id2);
1916 if (error)
1917 return error;
1918 free(id2);
1919 } else
1920 header->parent_id = strdup("/dev/null");
1921 } else
1922 header->parent_id = strdup("");
1924 header->committer_time =
1925 got_object_commit_get_committer_time(header->commit);
1926 header->author = strdup(got_object_commit_get_author(header->commit));
1927 header->committer =
1928 strdup(got_object_commit_get_committer(header->commit));
1930 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
1931 if (error)
1932 return error;
1934 commit_msg = commit_msg0;
1935 while (*commit_msg == '\n')
1936 commit_msg++;
1938 header->commit_msg = strdup(commit_msg);
1939 free(commit_msg0);
1940 return error;
1943 static const struct got_error *
1944 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
1946 const struct got_error *error = NULL;
1947 char *in_repo_path = NULL;
1949 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1950 if (error)
1951 return error;
1953 if (gw_trans->commit == NULL) {
1954 struct got_reference *head_ref;
1955 error = got_ref_open(&head_ref, header->repo,
1956 gw_trans->headref, 0);
1957 if (error)
1958 return error;
1960 error = got_ref_resolve(&header->id, header->repo, head_ref);
1961 got_ref_close(head_ref);
1962 if (error)
1963 return error;
1965 error = got_object_open_as_commit(&header->commit,
1966 header->repo, header->id);
1967 } else {
1968 struct got_reference *ref;
1969 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
1970 if (error == NULL) {
1971 int obj_type;
1972 error = got_ref_resolve(&header->id, header->repo, ref);
1973 got_ref_close(ref);
1974 if (error)
1975 return error;
1976 error = got_object_get_type(&obj_type, header->repo,
1977 header->id);
1978 if (error)
1979 return error;
1980 if (obj_type == GOT_OBJ_TYPE_TAG) {
1981 struct got_tag_object *tag;
1982 error = got_object_open_as_tag(&tag,
1983 header->repo, header->id);
1984 if (error)
1985 return error;
1986 if (got_object_tag_get_object_type(tag) !=
1987 GOT_OBJ_TYPE_COMMIT) {
1988 got_object_tag_close(tag);
1989 error = got_error(GOT_ERR_OBJ_TYPE);
1990 return error;
1992 free(header->id);
1993 header->id = got_object_id_dup(
1994 got_object_tag_get_object_id(tag));
1995 if (header->id == NULL)
1996 error = got_error_from_errno(
1997 "got_object_id_dup");
1998 got_object_tag_close(tag);
1999 if (error)
2000 return error;
2001 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2002 error = got_error(GOT_ERR_OBJ_TYPE);
2003 return error;
2005 error = got_object_open_as_commit(&header->commit,
2006 header->repo, header->id);
2007 if (error)
2008 return error;
2010 if (header->commit == NULL) {
2011 error = got_repo_match_object_id_prefix(&header->id,
2012 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2013 header->repo);
2014 if (error)
2015 return error;
2017 error = got_repo_match_object_id_prefix(&header->id,
2018 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2019 header->repo);
2022 error = got_repo_map_path(&in_repo_path, header->repo,
2023 gw_trans->repo_path, 1);
2024 if (error)
2025 return error;
2027 if (in_repo_path) {
2028 header->path = strdup(in_repo_path);
2030 free(in_repo_path);
2032 error = got_ref_list(&header->refs, header->repo, NULL,
2033 got_ref_cmp_by_name, NULL);
2034 if (error)
2035 return error;
2037 error = gw_get_commits(gw_trans, header, limit);
2038 return error;
2041 struct blame_line {
2042 int annotated;
2043 char *id_str;
2044 char *committer;
2045 char datebuf[11]; /* YYYY-MM-DD + NUL */
2048 struct gw_blame_cb_args {
2049 struct blame_line *lines;
2050 int nlines;
2051 int nlines_prec;
2052 int lineno_cur;
2053 off_t *line_offsets;
2054 FILE *f;
2055 struct got_repository *repo;
2056 struct gw_trans *gw_trans;
2057 struct buf *blamebuf;
2060 static const struct got_error *
2061 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2063 const struct got_error *err = NULL;
2064 struct gw_blame_cb_args *a = arg;
2065 struct blame_line *bline;
2066 char *line = NULL;
2067 size_t linesize = 0, newsize;
2068 struct got_commit_object *commit = NULL;
2069 off_t offset;
2070 struct tm tm;
2071 time_t committer_time;
2073 if (nlines != a->nlines ||
2074 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2075 return got_error(GOT_ERR_RANGE);
2077 if (lineno == -1)
2078 return NULL; /* no change in this commit */
2080 /* Annotate this line. */
2081 bline = &a->lines[lineno - 1];
2082 if (bline->annotated)
2083 return NULL;
2084 err = got_object_id_str(&bline->id_str, id);
2085 if (err)
2086 return err;
2088 err = got_object_open_as_commit(&commit, a->repo, id);
2089 if (err)
2090 goto done;
2092 bline->committer = strdup(got_object_commit_get_committer(commit));
2093 if (bline->committer == NULL) {
2094 err = got_error_from_errno("strdup");
2095 goto done;
2098 committer_time = got_object_commit_get_committer_time(commit);
2099 if (localtime_r(&committer_time, &tm) == NULL)
2100 return got_error_from_errno("localtime_r");
2101 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2102 &tm) >= sizeof(bline->datebuf)) {
2103 err = got_error(GOT_ERR_NO_SPACE);
2104 goto done;
2106 bline->annotated = 1;
2108 /* Print lines annotated so far. */
2109 bline = &a->lines[a->lineno_cur - 1];
2110 if (!bline->annotated)
2111 goto done;
2113 offset = a->line_offsets[a->lineno_cur - 1];
2114 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2115 err = got_error_from_errno("fseeko");
2116 goto done;
2119 while (bline->annotated) {
2120 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2121 *line_escape = NULL;
2122 size_t len;
2124 if (getline(&line, &linesize, a->f) == -1) {
2125 if (ferror(a->f))
2126 err = got_error_from_errno("getline");
2127 break;
2130 committer = bline->committer;
2131 smallerthan = strchr(committer, '<');
2132 if (smallerthan && smallerthan[1] != '\0')
2133 committer = smallerthan + 1;
2134 at = strchr(committer, '@');
2135 if (at)
2136 *at = '\0';
2137 len = strlen(committer);
2138 if (len >= 9)
2139 committer[8] = '\0';
2141 nl = strchr(line, '\n');
2142 if (nl)
2143 *nl = '\0';
2145 if (strcmp(line, "") != 0)
2146 line_escape = strdup(gw_html_escape(line));
2147 else
2148 line_escape = strdup("");
2150 asprintf(&blame_row, blame_line, a->nlines_prec,
2151 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2152 line_escape);
2153 a->lineno_cur++;
2154 err = buf_puts(&newsize, a->blamebuf, blame_row);
2155 if (err)
2156 return err;
2158 bline = &a->lines[a->lineno_cur - 1];
2159 free(line_escape);
2160 free(blame_row);
2162 done:
2163 if (commit)
2164 got_object_commit_close(commit);
2165 free(line);
2166 return err;
2169 static char*
2170 gw_get_file_blame(struct gw_trans *gw_trans)
2172 const struct got_error *error = NULL;
2173 struct got_repository *repo = NULL;
2174 struct got_object_id *obj_id = NULL;
2175 struct got_object_id *commit_id = NULL;
2176 struct got_blob_object *blob = NULL;
2177 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2178 *folder = NULL;
2179 struct gw_blame_cb_args bca;
2180 int i, obj_type;
2181 size_t filesize;
2183 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2184 if (error)
2185 goto done;
2187 if (gw_trans->repo_folder != NULL) {
2188 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2189 error = got_error_from_errno("asprintf");
2190 goto done;
2192 } else
2193 folder = strdup("");
2195 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2196 error = got_error_from_errno("asprintf");
2197 goto done;
2199 free(folder);
2201 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2202 if (error)
2203 goto done;
2205 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2206 GOT_OBJ_TYPE_COMMIT, 1, repo);
2207 if (error)
2208 goto done;
2210 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2211 if (error)
2212 goto done;
2214 if (obj_id == NULL) {
2215 error = got_error(GOT_ERR_NO_OBJ);
2216 goto done;
2219 error = got_object_get_type(&obj_type, repo, obj_id);
2220 if (error)
2221 goto done;
2223 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2224 error = got_error(GOT_ERR_OBJ_TYPE);
2225 goto done;
2228 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2229 if (error)
2230 goto done;
2232 error = buf_alloc(&bca.blamebuf, 0);
2233 if (error)
2234 goto done;
2236 bca.f = got_opentemp();
2237 if (bca.f == NULL) {
2238 error = got_error_from_errno("got_opentemp");
2239 goto done;
2241 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2242 &bca.line_offsets, bca.f, blob);
2243 if (error || bca.nlines == 0)
2244 goto done;
2246 /* Don't include \n at EOF in the blame line count. */
2247 if (bca.line_offsets[bca.nlines - 1] == filesize)
2248 bca.nlines--;
2250 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2251 if (bca.lines == NULL) {
2252 error = got_error_from_errno("calloc");
2253 goto done;
2255 bca.lineno_cur = 1;
2256 bca.nlines_prec = 0;
2257 i = bca.nlines;
2258 while (i > 0) {
2259 i /= 10;
2260 bca.nlines_prec++;
2262 bca.repo = repo;
2263 bca.gw_trans = gw_trans;
2265 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2266 NULL, NULL);
2267 if (buf_len(bca.blamebuf) > 0) {
2268 error = buf_putc(bca.blamebuf, '\0');
2269 blame_html = strdup(buf_get(bca.blamebuf));
2271 done:
2272 free(bca.blamebuf);
2273 free(in_repo_path);
2274 free(commit_id);
2275 free(obj_id);
2276 free(path);
2278 if (blob)
2279 error = got_object_blob_close(blob);
2280 if (repo)
2281 error = got_repo_close(repo);
2282 if (error)
2283 return NULL;
2284 if (bca.lines) {
2285 for (i = 0; i < bca.nlines; i++) {
2286 struct blame_line *bline = &bca.lines[i];
2287 free(bline->id_str);
2288 free(bline->committer);
2290 free(bca.lines);
2292 free(bca.line_offsets);
2293 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2294 error = got_error_from_errno("fclose");
2295 if (error)
2296 return NULL;
2297 else
2298 return blame_html;
2301 static char*
2302 gw_get_repo_tree(struct gw_trans *gw_trans)
2304 const struct got_error *error = NULL;
2305 struct got_repository *repo = NULL;
2306 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2307 struct got_tree_object *tree = NULL;
2308 struct buf *diffbuf = NULL;
2309 size_t newsize;
2310 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2311 *tree_row = NULL, *id_str;
2312 int nentries, i;
2314 error = buf_alloc(&diffbuf, 0);
2315 if (error)
2316 return NULL;
2318 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2319 if (error)
2320 goto done;
2322 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2323 if (error)
2324 goto done;
2326 if (gw_trans->repo_folder != NULL)
2327 path = strdup(gw_trans->repo_folder);
2328 else if (in_repo_path) {
2329 free(path);
2330 path = in_repo_path;
2333 if (gw_trans->commit == NULL) {
2334 struct got_reference *head_ref;
2335 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2336 if (error)
2337 goto done;
2339 error = got_ref_resolve(&commit_id, repo, head_ref);
2340 got_ref_close(head_ref);
2342 } else
2343 error = got_repo_match_object_id(&commit_id, NULL,
2344 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2345 if (error)
2346 goto done;
2348 error = got_object_id_str(&gw_trans->commit, commit_id);
2349 if (error)
2350 goto done;
2352 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2353 if (error)
2354 goto done;
2356 error = got_object_open_as_tree(&tree, repo, tree_id);
2357 if (error)
2358 goto done;
2360 nentries = got_object_tree_get_nentries(tree);
2362 for (i = 0; i < nentries; i++) {
2363 struct got_tree_entry *te;
2364 const char *modestr = "";
2365 char *id = NULL, *url_html = NULL;
2367 te = got_object_tree_get_entry(tree, i);
2369 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2370 if (error)
2371 goto done;
2373 if ((asprintf(&id, "%s", id_str)) == -1) {
2374 error = got_error_from_errno("asprintf");
2375 free(id_str);
2376 goto done;
2379 mode_t mode = got_tree_entry_get_mode(te);
2381 if (got_object_tree_entry_is_submodule(te))
2382 modestr = "$";
2383 else if (S_ISLNK(mode))
2384 modestr = "@";
2385 else if (S_ISDIR(mode))
2386 modestr = "/";
2387 else if (mode & S_IXUSR)
2388 modestr = "*";
2390 char *build_folder = NULL;
2391 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2392 if (gw_trans->repo_folder != NULL) {
2393 if ((asprintf(&build_folder, "%s/%s",
2394 gw_trans->repo_folder,
2395 got_tree_entry_get_name(te))) == -1) {
2396 error =
2397 got_error_from_errno("asprintf");
2398 goto done;
2400 } else {
2401 if (asprintf(&build_folder, "%s",
2402 got_tree_entry_get_name(te)) == -1)
2403 goto done;
2406 if ((asprintf(&url_html, folder_html,
2407 gw_trans->repo_name, gw_trans->action_name,
2408 gw_trans->commit, build_folder,
2409 got_tree_entry_get_name(te), modestr)) == -1) {
2410 error = got_error_from_errno("asprintf");
2411 goto done;
2413 } else {
2414 if (gw_trans->repo_folder != NULL) {
2415 if ((asprintf(&build_folder, "%s",
2416 gw_trans->repo_folder)) == -1) {
2417 error =
2418 got_error_from_errno("asprintf");
2419 goto done;
2421 } else
2422 build_folder = strdup("");
2424 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2425 "blame", gw_trans->commit,
2426 got_tree_entry_get_name(te), build_folder,
2427 got_tree_entry_get_name(te), modestr)) == -1) {
2428 error = got_error_from_errno("asprintf");
2429 goto done;
2432 free(build_folder);
2434 if (error)
2435 goto done;
2437 if ((asprintf(&tree_row, tree_line, url_html)) == -1) {
2438 error = got_error_from_errno("asprintf");
2439 goto done;
2441 error = buf_puts(&newsize, diffbuf, tree_row);
2442 if (error)
2443 goto done;
2445 free(id);
2446 free(id_str);
2447 free(url_html);
2448 free(tree_row);
2451 if (buf_len(diffbuf) > 0) {
2452 error = buf_putc(diffbuf, '\0');
2453 tree_html = strdup(buf_get(diffbuf));
2455 done:
2456 if (tree)
2457 got_object_tree_close(tree);
2458 if (repo)
2459 got_repo_close(repo);
2461 free(in_repo_path);
2462 free(tree_id);
2463 free(diffbuf);
2464 if (error)
2465 return NULL;
2466 else
2467 return tree_html;
2470 static char *
2471 gw_get_repo_heads(struct gw_trans *gw_trans)
2473 const struct got_error *error = NULL;
2474 struct got_repository *repo = NULL;
2475 struct got_reflist_head refs;
2476 struct got_reflist_entry *re;
2477 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2478 struct buf *diffbuf = NULL;
2479 size_t newsize;
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 SIMPLEQ_INIT(&refs);
2490 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2491 NULL);
2492 if (error)
2493 goto done;
2495 SIMPLEQ_FOREACH(re, &refs, entry) {
2496 char *refname;
2498 refname = strdup(got_ref_get_name(re->ref));
2499 if (refname == NULL) {
2500 error = got_error_from_errno("got_ref_to_str");
2501 goto done;
2504 if (strncmp(refname, "refs/heads/", 11) != 0) {
2505 free(refname);
2506 continue;
2509 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2510 TM_DIFF);
2512 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2513 refname, gw_trans->repo_name, refname,
2514 gw_trans->repo_name, refname, gw_trans->repo_name,
2515 refname)) == -1) {
2516 error = got_error_from_errno("asprintf");
2517 goto done;
2520 if (strncmp(refname, "refs/heads/", 11) == 0)
2521 refname += 11;
2523 if ((asprintf(&head_row, heads_row, age, refname,
2524 head_navs_disp)) == -1) {
2525 error = got_error_from_errno("asprintf");
2526 goto done;
2529 error = buf_puts(&newsize, diffbuf, head_row);
2531 free(head_navs_disp);
2532 free(head_row);
2535 if (buf_len(diffbuf) > 0) {
2536 error = buf_putc(diffbuf, '\0');
2537 heads = strdup(buf_get(diffbuf));
2539 done:
2540 buf_free(diffbuf);
2541 got_ref_list_free(&refs);
2542 if (repo)
2543 got_repo_close(repo);
2544 if (error)
2545 return NULL;
2546 else
2547 return heads;
2550 static char *
2551 gw_get_got_link(struct gw_trans *gw_trans)
2553 char *link;
2555 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2556 gw_trans->gw_conf->got_logo)) == -1)
2557 return NULL;
2559 return link;
2562 static char *
2563 gw_get_site_link(struct gw_trans *gw_trans)
2565 char *link, *repo = "", *action = "";
2567 if (gw_trans->repo_name != NULL)
2568 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2569 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2570 return NULL;
2572 if (gw_trans->action_name != NULL)
2573 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2574 return NULL;
2576 if ((asprintf(&link, site_link, GOTWEB,
2577 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2578 return NULL;
2580 return link;
2583 static char *
2584 gw_colordiff_line(char *buf)
2586 const struct got_error *error = NULL;
2587 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2588 struct buf *diffbuf = NULL;
2589 size_t newsize;
2591 error = buf_alloc(&diffbuf, 0);
2592 if (error)
2593 return NULL;
2595 if (buf == NULL)
2596 return NULL;
2597 if (strncmp(buf, "-", 1) == 0)
2598 color = "diff_minus";
2599 if (strncmp(buf, "+", 1) == 0)
2600 color = "diff_plus";
2601 if (strncmp(buf, "@@", 2) == 0)
2602 color = "diff_chunk_header";
2603 if (strncmp(buf, "@@", 2) == 0)
2604 color = "diff_chunk_header";
2605 if (strncmp(buf, "commit +", 8) == 0)
2606 color = "diff_meta";
2607 if (strncmp(buf, "commit -", 8) == 0)
2608 color = "diff_meta";
2609 if (strncmp(buf, "blob +", 6) == 0)
2610 color = "diff_meta";
2611 if (strncmp(buf, "blob -", 6) == 0)
2612 color = "diff_meta";
2613 if (strncmp(buf, "file +", 6) == 0)
2614 color = "diff_meta";
2615 if (strncmp(buf, "file -", 6) == 0)
2616 color = "diff_meta";
2617 if (strncmp(buf, "from:", 5) == 0)
2618 color = "diff_author";
2619 if (strncmp(buf, "via:", 4) == 0)
2620 color = "diff_author";
2621 if (strncmp(buf, "date:", 5) == 0)
2622 color = "diff_date";
2624 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2625 return NULL;
2627 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2628 if (error)
2629 return NULL;
2631 error = buf_puts(&newsize, diffbuf, buf);
2632 if (error)
2633 return NULL;
2635 if (buf_len(diffbuf) > 0) {
2636 error = buf_putc(diffbuf, '\0');
2637 colorized_line = strdup(buf_get(diffbuf));
2640 free(diffbuf);
2641 free(div_diff_line_div);
2642 return colorized_line;
2645 static char *
2646 gw_html_escape(const char *html)
2648 char *escaped_str = NULL, *buf;
2649 char c[1];
2650 size_t sz, i, buff_sz = 2048;
2652 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2653 return NULL;
2655 if (html == NULL)
2656 return NULL;
2657 else
2658 if ((sz = strlen(html)) == 0)
2659 return NULL;
2661 /* only work with buff_sz */
2662 if (buff_sz < sz)
2663 sz = buff_sz;
2665 for (i = 0; i < sz; i++) {
2666 c[0] = html[i];
2667 switch (c[0]) {
2668 case ('>'):
2669 strcat(buf, "&gt;");
2670 break;
2671 case ('&'):
2672 strcat(buf, "&amp;");
2673 break;
2674 case ('<'):
2675 strcat(buf, "&lt;");
2676 break;
2677 case ('"'):
2678 strcat(buf, "&quot;");
2679 break;
2680 case ('\''):
2681 strcat(buf, "&apos;");
2682 break;
2683 case ('\n'):
2684 strcat(buf, "<br />");
2685 default:
2686 strcat(buf, &c[0]);
2687 break;
2690 asprintf(&escaped_str, "%s", buf);
2691 free(buf);
2692 return escaped_str;
2695 int
2696 main(int argc, char *argv[])
2698 const struct got_error *error = NULL;
2699 struct gw_trans *gw_trans;
2700 struct gw_dir *dir = NULL, *tdir;
2701 const char *page = "index";
2702 int gw_malloc = 1;
2703 enum kcgi_err kerr;
2705 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2706 errx(1, "malloc");
2708 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2709 errx(1, "malloc");
2711 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2712 errx(1, "malloc");
2714 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2715 errx(1, "malloc");
2717 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2718 if (kerr != KCGI_OK) {
2719 error = gw_kcgi_error(kerr);
2720 goto err;
2723 if ((gw_trans->gw_conf =
2724 malloc(sizeof(struct gotweb_conf))) == NULL) {
2725 gw_malloc = 0;
2726 error = got_error_from_errno("malloc");
2727 goto err;
2730 TAILQ_INIT(&gw_trans->gw_dirs);
2731 TAILQ_INIT(&gw_trans->gw_headers);
2733 gw_trans->page = 0;
2734 gw_trans->repos_total = 0;
2735 gw_trans->repo_path = NULL;
2736 gw_trans->commit = NULL;
2737 gw_trans->headref = strdup(GOT_REF_HEAD);
2738 gw_trans->mime = KMIME_TEXT_HTML;
2739 gw_trans->gw_tmpl->key = gw_templs;
2740 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2741 gw_trans->gw_tmpl->arg = gw_trans;
2742 gw_trans->gw_tmpl->cb = gw_template;
2743 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2745 err:
2746 if (error) {
2747 gw_trans->mime = KMIME_TEXT_PLAIN;
2748 gw_trans->action = GW_ERR;
2749 gw_display_index(gw_trans, error);
2750 goto done;
2753 error = gw_parse_querystring(gw_trans);
2754 if (error)
2755 goto err;
2757 error = gw_display_index(gw_trans, error);
2758 if (error)
2759 goto err;
2761 done:
2762 if (gw_malloc) {
2763 free(gw_trans->gw_conf->got_repos_path);
2764 free(gw_trans->gw_conf->got_www_path);
2765 free(gw_trans->gw_conf->got_site_name);
2766 free(gw_trans->gw_conf->got_site_owner);
2767 free(gw_trans->gw_conf->got_site_link);
2768 free(gw_trans->gw_conf->got_logo);
2769 free(gw_trans->gw_conf->got_logo_url);
2770 free(gw_trans->gw_conf);
2771 free(gw_trans->commit);
2772 free(gw_trans->repo_path);
2773 free(gw_trans->repo_name);
2774 free(gw_trans->repo_file);
2775 free(gw_trans->action_name);
2776 free(gw_trans->headref);
2778 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2779 free(dir->name);
2780 free(dir->description);
2781 free(dir->age);
2782 free(dir->url);
2783 free(dir->path);
2784 free(dir);
2789 khttp_free(gw_trans->gw_req);
2790 return EXIT_SUCCESS;