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 = malloc(sizeof(struct gw_header))) == NULL)
369 return got_error_from_errno("malloc");
371 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
372 if (error)
373 return error;
375 error = gw_get_header(gw_trans, header, 1);
376 if (error)
377 return error;
379 diff_html = gw_get_diff(gw_trans, header);
381 if (diff_html == NULL)
382 diff_html = strdup("");
384 if ((asprintf(&diff_html_disp, diff_header,
385 gw_gen_diff_header(header->parent_id, header->commit_id),
386 gw_gen_commit_header(header->commit_id, header->refs_str),
387 gw_gen_tree_header(header->tree_id),
388 gw_gen_author_header(header->author),
389 gw_gen_committer_header(header->committer),
390 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
391 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
392 diff_html)) == -1)
393 return got_error_from_errno("asprintf");
395 if ((asprintf(&diff, diff_wrapper, diff_html_disp)) == -1)
396 return got_error_from_errno("asprintf");
398 kerr = khttp_puts(gw_trans->gw_req, diff);
399 if (kerr != KCGI_OK)
400 error = gw_kcgi_error(kerr);
401 got_ref_list_free(&header->refs);
402 gw_free_headers(header);
403 free(diff_html_disp);
404 free(diff_html);
405 free(diff);
406 return error;
409 static const struct got_error *
410 gw_index(struct gw_trans *gw_trans)
412 const struct got_error *error = NULL;
413 struct gw_dir *gw_dir = NULL;
414 char *html, *navs, *next, *prev;
415 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
416 enum kcgi_err kerr;
418 if (pledge("stdio rpath proc exec sendfd unveil",
419 NULL) == -1) {
420 error = got_error_from_errno("pledge");
421 return error;
424 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
425 if (error)
426 return error;
428 error = gw_load_got_paths(gw_trans);
429 if (error)
430 return error;
432 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
433 if (kerr != KCGI_OK)
434 return gw_kcgi_error(kerr);
436 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
437 if (asprintf(&html, index_projects_empty,
438 gw_trans->gw_conf->got_repos_path) == -1)
439 return got_error_from_errno("asprintf");
440 kerr = khttp_puts(gw_trans->gw_req, html);
441 if (kerr != KCGI_OK)
442 error = gw_kcgi_error(kerr);
443 free(html);
444 return error;
447 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
448 dir_c++;
450 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
451 if (gw_trans->page > 0 && (gw_trans->page *
452 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
453 prev_disp++;
454 continue;
457 prev_disp++;
459 if (error)
460 return error;
461 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
462 gw_dir->name, gw_dir->name)) == -1)
463 return got_error_from_errno("asprintf");
465 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
466 gw_dir->description, gw_dir->owner, gw_dir->age,
467 navs)) == -1)
468 return got_error_from_errno("asprintf");
470 kerr = khttp_puts(gw_trans->gw_req, html);
471 free(navs);
472 free(html);
473 if (kerr != KCGI_OK)
474 return gw_kcgi_error(kerr);
476 if (gw_trans->gw_conf->got_max_repos_display == 0)
477 continue;
479 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
480 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
481 if (kerr != KCGI_OK)
482 return gw_kcgi_error(kerr);
483 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
484 (gw_trans->page > 0) &&
485 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
486 prev_disp == gw_trans->repos_total)) {
487 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
488 if (kerr != KCGI_OK)
489 return gw_kcgi_error(kerr);
492 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
493 (gw_trans->page > 0) &&
494 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
495 prev_disp == gw_trans->repos_total)) {
496 if ((asprintf(&prev, nav_prev,
497 gw_trans->page - 1)) == -1)
498 return got_error_from_errno("asprintf");
499 kerr = khttp_puts(gw_trans->gw_req, prev);
500 free(prev);
501 if (kerr != KCGI_OK)
502 return gw_kcgi_error(kerr);
505 kerr = khttp_puts(gw_trans->gw_req, div_end);
506 if (kerr != KCGI_OK)
507 return gw_kcgi_error(kerr);
509 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
510 next_disp == gw_trans->gw_conf->got_max_repos_display &&
511 dir_c != (gw_trans->page + 1) *
512 gw_trans->gw_conf->got_max_repos_display) {
513 if ((asprintf(&next, nav_next,
514 gw_trans->page + 1)) == -1)
515 return got_error_from_errno("calloc");
516 kerr = khttp_puts(gw_trans->gw_req, next);
517 free(next);
518 if (kerr != KCGI_OK)
519 return gw_kcgi_error(kerr);
520 kerr = khttp_puts(gw_trans->gw_req, div_end);
521 if (kerr != KCGI_OK)
522 error = gw_kcgi_error(kerr);
523 next_disp = 0;
524 break;
527 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
528 (gw_trans->page > 0) &&
529 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
530 prev_disp == gw_trans->repos_total)) {
531 kerr = khttp_puts(gw_trans->gw_req, div_end);
532 if (kerr != KCGI_OK)
533 return gw_kcgi_error(kerr);
536 next_disp++;
538 return error;
541 static const struct got_error *
542 gw_commits(struct gw_trans *gw_trans)
544 const struct got_error *error = NULL;
545 char *commits_html, *commits_navs_html;
546 struct gw_header *header = NULL, *n_header = NULL;
547 enum kcgi_err kerr;
549 if ((header = malloc(sizeof(struct gw_header))) == NULL)
550 return got_error_from_errno("malloc");
552 if (pledge("stdio rpath proc exec sendfd unveil",
553 NULL) == -1) {
554 error = got_error_from_errno("pledge");
555 return error;
558 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
559 if (error)
560 return error;
562 error = gw_get_header(gw_trans, header,
563 gw_trans->gw_conf->got_max_commits_display);
565 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
566 if (kerr != KCGI_OK)
567 return gw_kcgi_error(kerr);
568 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
569 if ((asprintf(&commits_navs_html, commits_navs,
570 gw_trans->repo_name, n_header->commit_id,
571 gw_trans->repo_name, n_header->commit_id,
572 gw_trans->repo_name, n_header->commit_id)) == -1)
573 return got_error_from_errno("asprintf");
575 if ((asprintf(&commits_html, commits_line,
576 gw_gen_commit_header(n_header->commit_id,
577 n_header->refs_str),
578 gw_gen_author_header(n_header->author),
579 gw_gen_committer_header(n_header->committer),
580 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
581 TM_LONG)), gw_html_escape(n_header->commit_msg),
582 commits_navs_html)) == -1)
583 return got_error_from_errno("asprintf");
584 kerr = khttp_puts(gw_trans->gw_req, commits_html);
585 if (kerr != KCGI_OK)
586 return gw_kcgi_error(kerr);
588 kerr = khttp_puts(gw_trans->gw_req, div_end);
589 if (kerr != KCGI_OK)
590 error = gw_kcgi_error(kerr);
592 got_ref_list_free(&header->refs);
593 gw_free_headers(header);
594 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
595 gw_free_headers(n_header);
596 return error;
599 static const struct got_error *
600 gw_briefs(struct gw_trans *gw_trans)
602 const struct got_error *error = NULL;
603 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
604 struct gw_header *header = NULL, *n_header = NULL;
605 enum kcgi_err kerr;
607 if ((header = malloc(sizeof(struct gw_header))) == NULL)
608 return got_error_from_errno("malloc");
610 if (pledge("stdio rpath proc exec sendfd unveil",
611 NULL) == -1) {
612 error = got_error_from_errno("pledge");
613 return error;
616 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
617 if (error)
618 return error;
620 if (gw_trans->action == GW_SUMMARY)
621 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
622 else
623 error = gw_get_header(gw_trans, header,
624 gw_trans->gw_conf->got_max_commits_display);
626 if (error)
627 return error;
629 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
630 if (kerr != KCGI_OK)
631 return gw_kcgi_error(kerr);
633 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
634 if ((asprintf(&briefs_navs_html, briefs_navs,
635 gw_trans->repo_name, n_header->commit_id,
636 gw_trans->repo_name, n_header->commit_id,
637 gw_trans->repo_name, n_header->commit_id)) == -1)
638 return got_error_from_errno("asprintf");
639 newline = strchr(n_header->commit_msg, '\n');
640 if (newline)
641 *newline = '\0';
642 if ((asprintf(&briefs_html, briefs_line,
643 gw_get_time_str(n_header->committer_time, TM_DIFF),
644 n_header->author, gw_html_escape(n_header->commit_msg),
645 briefs_navs_html)) == -1)
646 return got_error_from_errno("asprintf");
647 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
648 if (kerr != KCGI_OK)
649 return gw_kcgi_error(kerr);
651 kerr = khttp_puts(gw_trans->gw_req, div_end);
652 if (kerr != KCGI_OK)
653 error = gw_kcgi_error(kerr);
655 got_ref_list_free(&header->refs);
656 gw_free_headers(header);
657 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
658 gw_free_headers(n_header);
659 return error;
662 static const struct got_error *
663 gw_summary(struct gw_trans *gw_trans)
665 const struct got_error *error = NULL;
666 char *description_html, *repo_owner_html, *repo_age_html,
667 *cloneurl_html, *tags, *heads, *tags_html,
668 *heads_html, *age;
669 enum kcgi_err kerr;
671 if (pledge("stdio rpath proc exec sendfd unveil",
672 NULL) == -1) {
673 error = got_error_from_errno("pledge");
674 return error;
677 /* unveil is applied with gw_briefs below */
679 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
680 if (kerr != KCGI_OK)
681 return gw_kcgi_error(kerr);
683 if (gw_trans->gw_conf->got_show_repo_description) {
684 if (gw_trans->gw_dir->description != NULL &&
685 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
686 if ((asprintf(&description_html, description,
687 gw_trans->gw_dir->description)) == -1)
688 return got_error_from_errno("asprintf");
690 kerr = khttp_puts(gw_trans->gw_req, description_html);
691 free(description_html);
692 if (kerr != KCGI_OK)
693 return gw_kcgi_error(kerr);
697 if (gw_trans->gw_conf->got_show_repo_owner) {
698 if (gw_trans->gw_dir->owner != NULL &&
699 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
700 if ((asprintf(&repo_owner_html, repo_owner,
701 gw_trans->gw_dir->owner)) == -1)
702 return got_error_from_errno("asprintf");
704 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
705 free(repo_owner_html);
706 if (kerr != KCGI_OK)
707 return gw_kcgi_error(kerr);
711 if (gw_trans->gw_conf->got_show_repo_age) {
712 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
713 "refs/heads", TM_LONG);
714 if (age != NULL && (strcmp(age, "") != 0)) {
715 if ((asprintf(&repo_age_html, last_change, age)) == -1)
716 return got_error_from_errno("asprintf");
718 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
719 free(repo_age_html);
720 free(age);
721 if (kerr != KCGI_OK)
722 return gw_kcgi_error(kerr);
726 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
727 if (gw_trans->gw_dir->url != NULL &&
728 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
729 if ((asprintf(&cloneurl_html, cloneurl,
730 gw_trans->gw_dir->url)) == -1)
731 return got_error_from_errno("asprintf");
733 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
734 free(cloneurl_html);
735 if (kerr != KCGI_OK)
736 return gw_kcgi_error(kerr);
739 kerr = khttp_puts(gw_trans->gw_req, div_end);
740 if (kerr != KCGI_OK)
741 return gw_kcgi_error(kerr);
743 error = gw_briefs(gw_trans);
744 if (error)
745 return error;
747 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
748 heads = gw_get_repo_heads(gw_trans);
750 if (tags != NULL && strcmp(tags, "") != 0) {
751 if ((asprintf(&tags_html, summary_tags,
752 tags)) == -1)
753 return got_error_from_errno("asprintf");
754 kerr = khttp_puts(gw_trans->gw_req, tags_html);
755 free(tags_html);
756 free(tags);
757 if (kerr != KCGI_OK)
758 return gw_kcgi_error(kerr);
761 if (heads != NULL && strcmp(heads, "") != 0) {
762 if ((asprintf(&heads_html, summary_heads,
763 heads)) == -1)
764 return got_error_from_errno("asprintf");
765 kerr = khttp_puts(gw_trans->gw_req, heads_html);
766 free(heads_html);
767 free(heads);
768 if (kerr != KCGI_OK)
769 return gw_kcgi_error(kerr);
771 return error;
774 static const struct got_error *
775 gw_tree(struct gw_trans *gw_trans)
777 const struct got_error *error = NULL;
778 struct gw_header *header = NULL;
779 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
780 enum kcgi_err kerr;
782 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
783 return got_error_from_errno("pledge");
785 if ((header = malloc(sizeof(struct gw_header))) == NULL)
786 return got_error_from_errno("malloc");
788 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
789 if (error)
790 return error;
792 error = gw_get_header(gw_trans, header, 1);
793 if (error)
794 return error;
796 tree_html = gw_get_repo_tree(gw_trans);
798 if (tree_html == NULL)
799 tree_html = strdup("");
801 if ((asprintf(&tree_html_disp, tree_header,
802 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
803 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
804 tree_html)) == -1)
805 return got_error_from_errno("asprintf");
807 if ((asprintf(&tree, tree_wrapper, tree_html_disp)) == -1)
808 return got_error_from_errno("asprintf");
810 kerr = khttp_puts(gw_trans->gw_req, tree);
811 if (kerr != KCGI_OK)
812 error = gw_kcgi_error(kerr);
813 got_ref_list_free(&header->refs);
814 gw_free_headers(header);
815 free(tree_html_disp);
816 free(tree_html);
817 free(tree);
818 return error;
821 static const struct got_error *
822 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
824 const struct got_error *error = NULL;
825 DIR *dt;
826 char *dir_test;
827 int opened = 0;
829 if ((asprintf(&dir_test, "%s/%s/%s",
830 gw_trans->gw_conf->got_repos_path, gw_dir->name,
831 GOTWEB_GIT_DIR)) == -1)
832 return got_error_from_errno("asprintf");
834 dt = opendir(dir_test);
835 if (dt == NULL) {
836 free(dir_test);
837 } else {
838 gw_dir->path = strdup(dir_test);
839 opened = 1;
840 goto done;
843 if ((asprintf(&dir_test, "%s/%s/%s",
844 gw_trans->gw_conf->got_repos_path, gw_dir->name,
845 GOTWEB_GOT_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 opened = 1;
853 error = got_error(GOT_ERR_NOT_GIT_REPO);
854 goto errored;
857 if ((asprintf(&dir_test, "%s/%s",
858 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
859 return got_error_from_errno("asprintf");
861 gw_dir->path = strdup(dir_test);
863 done:
864 gw_dir->description = gw_get_repo_description(gw_trans,
865 gw_dir->path);
866 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
867 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
868 TM_DIFF);
869 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
871 errored:
872 free(dir_test);
873 if (opened)
874 closedir(dt);
875 return error;
878 static const struct got_error *
879 gw_load_got_paths(struct gw_trans *gw_trans)
881 const struct got_error *error = NULL;
882 DIR *d;
883 struct dirent **sd_dent;
884 struct gw_dir *gw_dir;
885 struct stat st;
886 unsigned int d_cnt, d_i;
888 d = opendir(gw_trans->gw_conf->got_repos_path);
889 if (d == NULL) {
890 error = got_error_from_errno2("opendir",
891 gw_trans->gw_conf->got_repos_path);
892 return error;
895 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
896 alphasort);
897 if (d_cnt == -1) {
898 error = got_error_from_errno2("scandir",
899 gw_trans->gw_conf->got_repos_path);
900 return error;
903 for (d_i = 0; d_i < d_cnt; d_i++) {
904 if (gw_trans->gw_conf->got_max_repos > 0 &&
905 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
906 break; /* account for parent and self */
908 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
909 strcmp(sd_dent[d_i]->d_name, "..") == 0)
910 continue;
912 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
913 return got_error_from_errno("gw_dir malloc");
915 error = gw_load_got_path(gw_trans, gw_dir);
916 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
917 continue;
918 else if (error)
919 return error;
921 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
922 !got_path_dir_is_empty(gw_dir->path)) {
923 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
924 entry);
925 gw_trans->repos_total++;
929 closedir(d);
930 return error;
933 static const struct got_error *
934 gw_parse_querystring(struct gw_trans *gw_trans)
936 const struct got_error *error = NULL;
937 struct kpair *p;
938 struct gw_query_action *action = NULL;
939 unsigned int i;
941 if (gw_trans->gw_req->fieldnmap[0]) {
942 error = got_error_from_errno("bad parse");
943 return error;
944 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
945 /* define gw_trans->repo_path */
946 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
947 return got_error_from_errno("asprintf");
949 if ((asprintf(&gw_trans->repo_path, "%s/%s",
950 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
951 return got_error_from_errno("asprintf");
953 /* get action and set function */
954 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
955 for (i = 0; i < nitems(gw_query_funcs); i++) {
956 action = &gw_query_funcs[i];
957 if (action->func_name == NULL)
958 continue;
960 if (strcmp(action->func_name,
961 p->parsed.s) == 0) {
962 gw_trans->action = i;
963 if ((asprintf(&gw_trans->action_name,
964 "%s", action->func_name)) == -1)
965 return
966 got_error_from_errno(
967 "asprintf");
969 break;
972 action = NULL;
975 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
976 if ((asprintf(&gw_trans->commit, "%s",
977 p->parsed.s)) == -1)
978 return got_error_from_errno("asprintf");
980 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
981 if ((asprintf(&gw_trans->repo_file, "%s",
982 p->parsed.s)) == -1)
983 return got_error_from_errno("asprintf");
985 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
986 if ((asprintf(&gw_trans->repo_folder, "%s",
987 p->parsed.s)) == -1)
988 return got_error_from_errno("asprintf");
990 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
991 if ((asprintf(&gw_trans->headref, "%s",
992 p->parsed.s)) == -1)
993 return got_error_from_errno("asprintf");
995 if (action == NULL) {
996 error = got_error_from_errno("invalid action");
997 return error;
999 if ((gw_trans->gw_dir =
1000 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1001 return got_error_from_errno("gw_dir malloc");
1003 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1004 if (error)
1005 return error;
1006 } else
1007 gw_trans->action = GW_INDEX;
1009 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1010 gw_trans->page = p->parsed.i;
1012 /* if (gw_trans->action == GW_RAW) */
1013 /* gw_trans->mime = KMIME_TEXT_PLAIN; */
1015 return error;
1018 static struct gw_dir *
1019 gw_init_gw_dir(char *dir)
1021 struct gw_dir *gw_dir;
1023 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1024 return NULL;
1026 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1027 return NULL;
1029 return gw_dir;
1032 static const struct got_error *
1033 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1035 enum kcgi_err kerr;
1037 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1038 if (kerr != KCGI_OK)
1039 return gw_kcgi_error(kerr);
1040 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1041 khttps[code]);
1042 if (kerr != KCGI_OK)
1043 return gw_kcgi_error(kerr);
1044 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1045 kmimetypes[mime]);
1046 if (kerr != KCGI_OK)
1047 return gw_kcgi_error(kerr);
1048 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1049 if (kerr != KCGI_OK)
1050 return gw_kcgi_error(kerr);
1051 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1052 if (kerr != KCGI_OK)
1053 return gw_kcgi_error(kerr);
1054 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1055 if (kerr != KCGI_OK)
1056 return gw_kcgi_error(kerr);
1058 kerr = khttp_body(gw_trans->gw_req);
1059 return gw_kcgi_error(kerr);
1062 static const struct got_error *
1063 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
1065 enum kcgi_err kerr;
1067 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1068 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1070 if (err)
1071 kerr = khttp_puts(gw_trans->gw_req, err->msg);
1072 else
1073 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1074 gw_query_funcs[gw_trans->action].template);
1075 if (kerr != KCGI_OK)
1076 return gw_kcgi_error(kerr);
1078 kerr = khtml_close(gw_trans->gw_html_req);
1079 return gw_kcgi_error(kerr);
1082 static int
1083 gw_template(size_t key, void *arg)
1085 const struct got_error *error = NULL;
1086 struct gw_trans *gw_trans = arg;
1087 char *gw_got_link, *gw_site_link;
1088 char *site_owner_name, *site_owner_name_h;
1090 switch (key) {
1091 case (TEMPL_HEAD):
1092 khttp_puts(gw_trans->gw_req, head);
1093 break;
1094 case(TEMPL_HEADER):
1095 gw_got_link = gw_get_got_link(gw_trans);
1096 if (gw_got_link != NULL)
1097 khttp_puts(gw_trans->gw_req, gw_got_link);
1099 free(gw_got_link);
1100 break;
1101 case (TEMPL_SITEPATH):
1102 gw_site_link = gw_get_site_link(gw_trans);
1103 if (gw_site_link != NULL)
1104 khttp_puts(gw_trans->gw_req, gw_site_link);
1106 free(gw_site_link);
1107 break;
1108 case(TEMPL_TITLE):
1109 if (gw_trans->gw_conf->got_site_name != NULL)
1110 khtml_puts(gw_trans->gw_html_req,
1111 gw_trans->gw_conf->got_site_name);
1113 break;
1114 case (TEMPL_SEARCH):
1115 khttp_puts(gw_trans->gw_req, search);
1116 break;
1117 case(TEMPL_SITEOWNER):
1118 if (gw_trans->gw_conf->got_site_owner != NULL &&
1119 gw_trans->gw_conf->got_show_site_owner) {
1120 site_owner_name =
1121 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1122 if ((asprintf(&site_owner_name_h, site_owner,
1123 site_owner_name))
1124 == -1)
1125 return 0;
1127 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1128 free(site_owner_name);
1129 free(site_owner_name_h);
1131 break;
1132 case(TEMPL_CONTENT):
1133 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1134 if (error)
1135 khttp_puts(gw_trans->gw_req, error->msg);
1136 break;
1137 default:
1138 return 0;
1140 return 1;
1143 static char *
1144 gw_gen_commit_header(char *str1, char *str2)
1146 char *return_html = NULL, *ref_str = NULL;
1148 if (strcmp(str2, "") != 0) {
1149 if ((asprintf(&ref_str, "(%s)", str2)) == -1) {
1150 return_html = strdup("");
1151 return return_html;
1153 } else
1154 ref_str = strdup("");
1157 if ((asprintf(&return_html, header_commit_html, str1, ref_str)) == -1)
1158 return_html = strdup("");
1160 free(ref_str);
1161 return return_html;
1164 static char *
1165 gw_gen_diff_header(char *str1, char *str2)
1167 char *return_html = NULL;
1169 if ((asprintf(&return_html, header_diff_html, str1, str2)) == -1)
1170 return_html = strdup("");
1172 return return_html;
1175 static char *
1176 gw_gen_author_header(char *str)
1178 char *return_html = NULL;
1180 if ((asprintf(&return_html, header_author_html, str)) == -1)
1181 return_html = strdup("");
1183 return return_html;
1186 static char *
1187 gw_gen_committer_header(char *str)
1189 char *return_html = NULL;
1191 if ((asprintf(&return_html, header_committer_html, str)) == -1)
1192 return_html = strdup("");
1194 return return_html;
1197 static char *
1198 gw_gen_age_header(char *str)
1200 char *return_html = NULL;
1202 if ((asprintf(&return_html, header_age_html, str)) == -1)
1203 return_html = strdup("");
1205 return return_html;
1208 static char *
1209 gw_gen_commit_msg_header(char *str)
1211 char *return_html = NULL;
1213 if ((asprintf(&return_html, header_commit_msg_html, str)) == -1)
1214 return_html = strdup("");
1216 return return_html;
1219 static char *
1220 gw_gen_tree_header(char *str)
1222 char *return_html = NULL;
1224 if ((asprintf(&return_html, header_tree_html, str)) == -1)
1225 return_html = strdup("");
1227 return return_html;
1230 static char *
1231 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1233 FILE *f;
1234 char *description = NULL, *d_file = NULL;
1235 unsigned int len;
1237 if (gw_trans->gw_conf->got_show_repo_description == false)
1238 goto err;
1240 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1241 goto err;
1243 if ((f = fopen(d_file, "r")) == NULL)
1244 goto err;
1246 fseek(f, 0, SEEK_END);
1247 len = ftell(f) + 1;
1248 fseek(f, 0, SEEK_SET);
1249 if ((description = calloc(len, sizeof(char *))) == NULL)
1250 goto err;
1252 fread(description, 1, len, f);
1253 fclose(f);
1254 free(d_file);
1255 return description;
1256 err:
1257 if ((asprintf(&description, "%s", "")) == -1)
1258 return NULL;
1260 return description;
1263 static char *
1264 gw_get_time_str(time_t committer_time, int ref_tm)
1266 struct tm tm;
1267 time_t diff_time;
1268 char *years = "years ago", *months = "months ago";
1269 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1270 char *minutes = "minutes ago", *seconds = "seconds ago";
1271 char *now = "right now";
1272 char *repo_age, *s;
1273 char datebuf[29];
1275 switch (ref_tm) {
1276 case TM_DIFF:
1277 diff_time = time(NULL) - committer_time;
1278 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1279 if ((asprintf(&repo_age, "%lld %s",
1280 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1281 return NULL;
1282 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1283 if ((asprintf(&repo_age, "%lld %s",
1284 (diff_time / 60 / 60 / 24 / (365 / 12)),
1285 months)) == -1)
1286 return NULL;
1287 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1288 if ((asprintf(&repo_age, "%lld %s",
1289 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1290 return NULL;
1291 } else if (diff_time > 60 * 60 * 24 * 2) {
1292 if ((asprintf(&repo_age, "%lld %s",
1293 (diff_time / 60 / 60 / 24), days)) == -1)
1294 return NULL;
1295 } else if (diff_time > 60 * 60 * 2) {
1296 if ((asprintf(&repo_age, "%lld %s",
1297 (diff_time / 60 / 60), hours)) == -1)
1298 return NULL;
1299 } else if (diff_time > 60 * 2) {
1300 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1301 minutes)) == -1)
1302 return NULL;
1303 } else if (diff_time > 2) {
1304 if ((asprintf(&repo_age, "%lld %s", diff_time,
1305 seconds)) == -1)
1306 return NULL;
1307 } else {
1308 if ((asprintf(&repo_age, "%s", now)) == -1)
1309 return NULL;
1311 break;
1312 case TM_LONG:
1313 if (gmtime_r(&committer_time, &tm) == NULL)
1314 return NULL;
1316 s = asctime_r(&tm, datebuf);
1317 if (s == NULL)
1318 return NULL;
1320 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1321 return NULL;
1322 break;
1324 return repo_age;
1327 static char *
1328 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1329 int ref_tm)
1331 const struct got_error *error = NULL;
1332 struct got_object_id *id = NULL;
1333 struct got_repository *repo = NULL;
1334 struct got_commit_object *commit = NULL;
1335 struct got_reflist_head refs;
1336 struct got_reflist_entry *re;
1337 struct got_reference *head_ref;
1338 int is_head = 0;
1339 time_t committer_time = 0, cmp_time = 0;
1340 const char *refname;
1341 char *repo_age = NULL;
1343 if (repo_ref == NULL)
1344 return NULL;
1346 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1347 is_head = 1;
1349 SIMPLEQ_INIT(&refs);
1350 if (gw_trans->gw_conf->got_show_repo_age == false) {
1351 if ((asprintf(&repo_age, "")) == -1)
1352 return NULL;
1353 return repo_age;
1356 error = got_repo_open(&repo, dir, NULL);
1357 if (error)
1358 goto err;
1360 if (is_head)
1361 error = got_ref_list(&refs, repo, "refs/heads",
1362 got_ref_cmp_by_name, NULL);
1363 else
1364 error = got_ref_list(&refs, repo, repo_ref,
1365 got_ref_cmp_by_name, NULL);
1366 if (error)
1367 goto err;
1369 SIMPLEQ_FOREACH(re, &refs, entry) {
1370 if (is_head)
1371 refname = strdup(repo_ref);
1372 else
1373 refname = got_ref_get_name(re->ref);
1374 error = got_ref_open(&head_ref, repo, refname, 0);
1375 if (error)
1376 goto err;
1378 error = got_ref_resolve(&id, repo, head_ref);
1379 got_ref_close(head_ref);
1380 if (error)
1381 goto err;
1383 error = got_object_open_as_commit(&commit, repo, id);
1384 if (error)
1385 goto err;
1387 committer_time =
1388 got_object_commit_get_committer_time(commit);
1390 if (cmp_time < committer_time)
1391 cmp_time = committer_time;
1394 if (cmp_time != 0) {
1395 committer_time = cmp_time;
1396 repo_age = gw_get_time_str(committer_time, ref_tm);
1397 } else
1398 if ((asprintf(&repo_age, "")) == -1)
1399 return NULL;
1400 got_ref_list_free(&refs);
1401 free(id);
1402 return repo_age;
1403 err:
1404 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1405 return NULL;
1407 return repo_age;
1410 static char *
1411 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1413 const struct got_error *error;
1414 FILE *f = NULL;
1415 struct got_object_id *id1 = NULL, *id2 = NULL;
1416 struct buf *diffbuf = NULL;
1417 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1418 *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1419 int obj_type;
1420 size_t newsize;
1422 f = got_opentemp();
1423 if (f == NULL)
1424 return NULL;
1426 error = buf_alloc(&diffbuf, 0);
1427 if (error)
1428 return NULL;
1430 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1431 if (error)
1432 goto done;
1434 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1435 error = got_repo_match_object_id(&id1, &label1,
1436 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1437 if (error)
1438 goto done;
1441 error = got_repo_match_object_id(&id2, &label2,
1442 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1443 if (error)
1444 goto done;
1446 error = got_object_get_type(&obj_type, header->repo, id2);
1447 if (error)
1448 goto done;
1449 switch (obj_type) {
1450 case GOT_OBJ_TYPE_BLOB:
1451 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1452 header->repo, f);
1453 break;
1454 case GOT_OBJ_TYPE_TREE:
1455 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1456 header->repo, f);
1457 break;
1458 case GOT_OBJ_TYPE_COMMIT:
1459 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1460 header->repo, f);
1461 break;
1462 default:
1463 error = got_error(GOT_ERR_OBJ_TYPE);
1466 if ((buf = calloc(128, sizeof(char *))) == NULL)
1467 goto done;
1469 fseek(f, 0, SEEK_SET);
1471 while ((fgets(buf, 2048, f)) != NULL) {
1472 n_buf = buf;
1473 while (*n_buf == '\n')
1474 n_buf++;
1475 newline = strchr(n_buf, '\n');
1476 if (newline)
1477 *newline = ' ';
1479 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1480 if (buf_color == NULL)
1481 continue;
1483 error = buf_puts(&newsize, diffbuf, buf_color);
1484 if (error)
1485 return NULL;
1487 error = buf_puts(&newsize, diffbuf, div_end);
1488 if (error)
1489 return NULL;
1492 if (buf_len(diffbuf) > 0) {
1493 error = buf_putc(diffbuf, '\0');
1494 diff_html = strdup(buf_get(diffbuf));
1496 done:
1497 fclose(f);
1498 free(buf_color);
1499 free(buf);
1500 free(diffbuf);
1501 free(label1);
1502 free(label2);
1503 free(id1);
1504 free(id2);
1506 if (error)
1507 return NULL;
1508 else
1509 return diff_html;
1512 static char *
1513 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1515 FILE *f;
1516 char *owner = NULL, *d_file = NULL;
1517 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1518 char *comp, *pos, *buf;
1519 unsigned int i;
1521 if (gw_trans->gw_conf->got_show_repo_owner == false)
1522 goto err;
1524 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1525 goto err;
1527 if ((f = fopen(d_file, "r")) == NULL)
1528 goto err;
1530 if ((buf = calloc(128, sizeof(char *))) == NULL)
1531 goto err;
1533 while ((fgets(buf, 128, f)) != NULL) {
1534 if ((pos = strstr(buf, gotweb)) != NULL)
1535 break;
1537 if ((pos = strstr(buf, gitweb)) != NULL)
1538 break;
1541 if (pos == NULL)
1542 goto err;
1544 do {
1545 fgets(buf, 128, f);
1546 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1548 if (comp == NULL)
1549 goto err;
1551 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1552 goto err;
1554 for (i = 0; i < 2; i++) {
1555 owner = strsep(&buf, "\"");
1558 if (owner == NULL)
1559 goto err;
1561 fclose(f);
1562 free(d_file);
1563 return owner;
1564 err:
1565 if ((asprintf(&owner, "%s", "")) == -1)
1566 return NULL;
1568 return owner;
1571 static char *
1572 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1574 FILE *f;
1575 char *url = NULL, *d_file = NULL;
1576 unsigned int len;
1578 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1579 return NULL;
1581 if ((f = fopen(d_file, "r")) == NULL)
1582 return NULL;
1584 fseek(f, 0, SEEK_END);
1585 len = ftell(f) + 1;
1586 fseek(f, 0, SEEK_SET);
1588 if ((url = calloc(len, sizeof(char *))) == NULL)
1589 return NULL;
1591 fread(url, 1, len, f);
1592 fclose(f);
1593 free(d_file);
1594 return url;
1597 static char *
1598 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
1600 const struct got_error *error = NULL;
1601 struct got_repository *repo = NULL;
1602 struct got_reflist_head refs;
1603 struct got_reflist_entry *re;
1604 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1605 *age = NULL;
1606 char *newline;
1607 struct buf *diffbuf = NULL;
1608 size_t newsize;
1610 error = buf_alloc(&diffbuf, 0);
1611 if (error)
1612 return NULL;
1613 SIMPLEQ_INIT(&refs);
1615 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1616 if (error)
1617 goto done;
1619 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1620 if (error)
1621 goto done;
1623 SIMPLEQ_FOREACH(re, &refs, entry) {
1624 const char *refname;
1625 char *refstr, *tag_commit0, *tag_commit, *id_str;
1626 time_t tagger_time;
1627 struct got_object_id *id;
1628 struct got_tag_object *tag;
1630 refname = got_ref_get_name(re->ref);
1631 if (strncmp(refname, "refs/tags/", 10) != 0)
1632 continue;
1633 refname += 10;
1634 refstr = got_ref_to_str(re->ref);
1635 if (refstr == NULL) {
1636 error = got_error_from_errno("got_ref_to_str");
1637 goto done;
1640 error = got_ref_resolve(&id, repo, re->ref);
1641 if (error)
1642 goto done;
1643 error = got_object_open_as_tag(&tag, repo, id);
1644 free(id);
1645 if (error)
1646 goto done;
1648 tagger_time = got_object_tag_get_tagger_time(tag);
1650 error = got_object_id_str(&id_str,
1651 got_object_tag_get_object_id(tag));
1652 if (error)
1653 goto done;
1655 tag_commit0 = strdup(got_object_tag_get_message(tag));
1657 if (tag_commit0 == NULL) {
1658 error = got_error_from_errno("strdup");
1659 goto done;
1662 tag_commit = tag_commit0;
1663 while (*tag_commit == '\n')
1664 tag_commit++;
1666 switch (tag_type) {
1667 case TAGBRIEF:
1668 newline = strchr(tag_commit, '\n');
1669 if (newline)
1670 *newline = '\0';
1672 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1673 TM_DIFF))) == -1) {
1674 error = got_error_from_errno("asprintf");
1675 goto done;
1678 if ((asprintf(&tags_navs_disp, tags_navs,
1679 gw_trans->repo_name, id_str, gw_trans->repo_name,
1680 id_str, gw_trans->repo_name, id_str,
1681 gw_trans->repo_name, id_str)) == -1) {
1682 error = got_error_from_errno("asprintf");
1683 goto done;
1686 if ((asprintf(&tag_row, tags_row, age, refname,
1687 tag_commit, tags_navs_disp)) == -1) {
1688 error = got_error_from_errno("asprintf");
1689 goto done;
1692 free(tags_navs_disp);
1693 break;
1694 case TAGFULL:
1695 break;
1696 default:
1697 break;
1700 got_object_tag_close(tag);
1702 error = buf_puts(&newsize, diffbuf, tag_row);
1704 free(id_str);
1705 free(refstr);
1706 free(age);
1707 free(tag_commit0);
1708 free(tag_row);
1710 if (error || (limit && --limit == 0))
1711 break;
1714 if (buf_len(diffbuf) > 0) {
1715 error = buf_putc(diffbuf, '\0');
1716 tags = strdup(buf_get(diffbuf));
1718 done:
1719 buf_free(diffbuf);
1720 got_ref_list_free(&refs);
1721 if (repo)
1722 got_repo_close(repo);
1723 if (error)
1724 return NULL;
1725 else
1726 return tags;
1729 static void
1730 gw_free_headers(struct gw_header *header)
1732 free(header->id);
1733 free(header->path);
1734 if (header->commit != NULL)
1735 got_object_commit_close(header->commit);
1736 if (header->repo)
1737 got_repo_close(header->repo);
1738 free(header->refs_str);
1739 free(header->commit_id);
1740 free(header->parent_id);
1741 free(header->tree_id);
1742 free(header->author);
1743 free(header->committer);
1744 free(header->commit_msg);
1747 static struct gw_header *
1748 gw_init_header()
1750 struct gw_header *header;
1752 if ((header = malloc(sizeof(*header))) == NULL)
1753 return NULL;
1755 header->repo = NULL;
1756 header->commit = NULL;
1757 header->id = NULL;
1758 header->path = NULL;
1760 return header;
1763 static const struct got_error *
1764 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1765 int limit)
1767 const struct got_error *error = NULL;
1768 struct got_commit_graph *graph = NULL;
1770 error = got_commit_graph_open(&graph, header->path, 0);
1771 if (error)
1772 goto done;
1774 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1775 NULL, NULL);
1776 if (error)
1777 goto done;
1779 for (;;) {
1780 error = got_commit_graph_iter_next(&header->id, graph,
1781 header->repo, NULL, NULL);
1782 if (error) {
1783 if (error->code == GOT_ERR_ITER_COMPLETED)
1784 error = NULL;
1785 goto done;
1787 if (header->id == NULL)
1788 goto done;
1790 error = got_object_open_as_commit(&header->commit, header->repo,
1791 header->id);
1792 if (error)
1793 goto done;
1795 error = gw_get_commit(gw_trans, header);
1796 if (limit > 1) {
1797 struct gw_header *n_header = NULL;
1798 if ((n_header = gw_init_header()) == NULL)
1799 error = got_error_from_errno("malloc");
1801 n_header->refs_str = strdup(header->refs_str);
1802 n_header->commit_id = strdup(header->commit_id);
1803 n_header->parent_id = strdup(header->parent_id);
1804 n_header->tree_id = strdup(header->tree_id);
1805 n_header->author = strdup(header->author);
1806 n_header->committer = strdup(header->committer);
1807 n_header->commit_msg = strdup(header->commit_msg);
1808 n_header->committer_time = header->committer_time;
1809 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
1810 entry);
1812 if (error || (limit && --limit == 0))
1813 break;
1815 done:
1816 if (graph)
1817 got_commit_graph_close(graph);
1818 return error;
1821 static const struct got_error *
1822 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
1824 const struct got_error *error = NULL;
1825 struct got_reflist_entry *re;
1826 struct got_object_id *id2 = NULL;
1827 struct got_object_qid *parent_id;
1828 char *refs_str = NULL,
1829 *commit_msg = NULL, *commit_msg0;
1831 /*print commit*/
1832 SIMPLEQ_FOREACH(re, &header->refs, entry) {
1833 char *s;
1834 const char *name;
1835 struct got_tag_object *tag = NULL;
1836 int cmp;
1838 name = got_ref_get_name(re->ref);
1839 if (strcmp(name, GOT_REF_HEAD) == 0)
1840 continue;
1841 if (strncmp(name, "refs/", 5) == 0)
1842 name += 5;
1843 if (strncmp(name, "got/", 4) == 0)
1844 continue;
1845 if (strncmp(name, "heads/", 6) == 0)
1846 name += 6;
1847 if (strncmp(name, "remotes/", 8) == 0)
1848 name += 8;
1849 if (strncmp(name, "tags/", 5) == 0) {
1850 error = got_object_open_as_tag(&tag, header->repo,
1851 re->id);
1852 if (error) {
1853 if (error->code != GOT_ERR_OBJ_TYPE)
1854 continue;
1856 * Ref points at something other
1857 * than a tag.
1859 error = NULL;
1860 tag = NULL;
1863 cmp = got_object_id_cmp(tag ?
1864 got_object_tag_get_object_id(tag) : re->id, header->id);
1865 if (tag)
1866 got_object_tag_close(tag);
1867 if (cmp != 0)
1868 continue;
1869 s = refs_str;
1870 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1871 s ? ", " : "", name)) == -1) {
1872 error = got_error_from_errno("asprintf");
1873 free(s);
1874 return error;
1876 header->refs_str = strdup(refs_str);
1877 free(s);
1880 if (refs_str == NULL)
1881 header->refs_str = strdup("");
1882 free(refs_str);
1884 error = got_object_id_str(&header->commit_id, header->id);
1885 if (error)
1886 return error;
1888 error = got_object_id_str(&header->tree_id,
1889 got_object_commit_get_tree_id(header->commit));
1890 if (error)
1891 return error;
1893 if (gw_trans->action == GW_DIFF) {
1894 parent_id = SIMPLEQ_FIRST(
1895 got_object_commit_get_parent_ids(header->commit));
1896 if (parent_id != NULL) {
1897 id2 = got_object_id_dup(parent_id->id);
1898 free (parent_id);
1899 error = got_object_id_str(&header->parent_id, id2);
1900 if (error)
1901 return error;
1902 free(id2);
1903 } else
1904 header->parent_id = strdup("/dev/null");
1905 } else
1906 header->parent_id = strdup("");
1908 header->committer_time =
1909 got_object_commit_get_committer_time(header->commit);
1910 header->author = strdup(got_object_commit_get_author(header->commit));
1911 header->committer =
1912 strdup(got_object_commit_get_committer(header->commit));
1914 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
1915 if (error)
1916 return error;
1918 commit_msg = commit_msg0;
1919 while (*commit_msg == '\n')
1920 commit_msg++;
1922 header->commit_msg = strdup(commit_msg);
1923 free(commit_msg0);
1924 return error;
1927 static const struct got_error *
1928 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
1930 const struct got_error *error = NULL;
1931 char *in_repo_path = NULL;
1933 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1934 if (error)
1935 return error;
1937 SIMPLEQ_INIT(&header->refs);
1939 if (gw_trans->commit == NULL) {
1940 struct got_reference *head_ref;
1941 error = got_ref_open(&head_ref, header->repo,
1942 gw_trans->headref, 0);
1943 if (error)
1944 return error;
1946 error = got_ref_resolve(&header->id, header->repo, head_ref);
1947 got_ref_close(head_ref);
1948 if (error)
1949 return error;
1951 error = got_object_open_as_commit(&header->commit,
1952 header->repo, header->id);
1953 } else {
1954 struct got_reference *ref;
1955 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
1956 if (error == NULL) {
1957 int obj_type;
1958 error = got_ref_resolve(&header->id, header->repo, ref);
1959 got_ref_close(ref);
1960 if (error)
1961 return error;
1962 error = got_object_get_type(&obj_type, header->repo,
1963 header->id);
1964 if (error)
1965 return error;
1966 if (obj_type == GOT_OBJ_TYPE_TAG) {
1967 struct got_tag_object *tag;
1968 error = got_object_open_as_tag(&tag,
1969 header->repo, header->id);
1970 if (error)
1971 return error;
1972 if (got_object_tag_get_object_type(tag) !=
1973 GOT_OBJ_TYPE_COMMIT) {
1974 got_object_tag_close(tag);
1975 error = got_error(GOT_ERR_OBJ_TYPE);
1976 return error;
1978 free(header->id);
1979 header->id = got_object_id_dup(
1980 got_object_tag_get_object_id(tag));
1981 if (header->id == NULL)
1982 error = got_error_from_errno(
1983 "got_object_id_dup");
1984 got_object_tag_close(tag);
1985 if (error)
1986 return error;
1987 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1988 error = got_error(GOT_ERR_OBJ_TYPE);
1989 return error;
1991 error = got_object_open_as_commit(&header->commit,
1992 header->repo, header->id);
1993 if (error)
1994 return error;
1996 if (header->commit == NULL) {
1997 error = got_repo_match_object_id_prefix(&header->id,
1998 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
1999 header->repo);
2000 if (error)
2001 return error;
2003 error = got_repo_match_object_id_prefix(&header->id,
2004 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2005 header->repo);
2008 error = got_repo_map_path(&in_repo_path, header->repo,
2009 gw_trans->repo_path, 1);
2010 if (error)
2011 return error;
2013 if (in_repo_path) {
2014 header->path = strdup(in_repo_path);
2016 free(in_repo_path);
2018 error = got_ref_list(&header->refs, header->repo, NULL,
2019 got_ref_cmp_by_name, NULL);
2020 if (error)
2021 return error;
2023 error = gw_get_commits(gw_trans, header, limit);
2024 return error;
2027 struct blame_line {
2028 int annotated;
2029 char *id_str;
2030 char *committer;
2031 char datebuf[11]; /* YYYY-MM-DD + NUL */
2034 struct gw_blame_cb_args {
2035 struct blame_line *lines;
2036 int nlines;
2037 int nlines_prec;
2038 int lineno_cur;
2039 off_t *line_offsets;
2040 FILE *f;
2041 struct got_repository *repo;
2042 struct gw_trans *gw_trans;
2043 struct buf *blamebuf;
2046 static const struct got_error *
2047 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2049 const struct got_error *err = NULL;
2050 struct gw_blame_cb_args *a = arg;
2051 struct blame_line *bline;
2052 char *line = NULL;
2053 size_t linesize = 0, newsize;
2054 struct got_commit_object *commit = NULL;
2055 off_t offset;
2056 struct tm tm;
2057 time_t committer_time;
2059 if (nlines != a->nlines ||
2060 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2061 return got_error(GOT_ERR_RANGE);
2063 if (lineno == -1)
2064 return NULL; /* no change in this commit */
2066 /* Annotate this line. */
2067 bline = &a->lines[lineno - 1];
2068 if (bline->annotated)
2069 return NULL;
2070 err = got_object_id_str(&bline->id_str, id);
2071 if (err)
2072 return err;
2074 err = got_object_open_as_commit(&commit, a->repo, id);
2075 if (err)
2076 goto done;
2078 bline->committer = strdup(got_object_commit_get_committer(commit));
2079 if (bline->committer == NULL) {
2080 err = got_error_from_errno("strdup");
2081 goto done;
2084 committer_time = got_object_commit_get_committer_time(commit);
2085 if (localtime_r(&committer_time, &tm) == NULL)
2086 return got_error_from_errno("localtime_r");
2087 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2088 &tm) >= sizeof(bline->datebuf)) {
2089 err = got_error(GOT_ERR_NO_SPACE);
2090 goto done;
2092 bline->annotated = 1;
2094 /* Print lines annotated so far. */
2095 bline = &a->lines[a->lineno_cur - 1];
2096 if (!bline->annotated)
2097 goto done;
2099 offset = a->line_offsets[a->lineno_cur - 1];
2100 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2101 err = got_error_from_errno("fseeko");
2102 goto done;
2105 while (bline->annotated) {
2106 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2107 *line_escape = NULL;
2108 size_t len;
2110 if (getline(&line, &linesize, a->f) == -1) {
2111 if (ferror(a->f))
2112 err = got_error_from_errno("getline");
2113 break;
2116 committer = bline->committer;
2117 smallerthan = strchr(committer, '<');
2118 if (smallerthan && smallerthan[1] != '\0')
2119 committer = smallerthan + 1;
2120 at = strchr(committer, '@');
2121 if (at)
2122 *at = '\0';
2123 len = strlen(committer);
2124 if (len >= 9)
2125 committer[8] = '\0';
2127 nl = strchr(line, '\n');
2128 if (nl)
2129 *nl = '\0';
2131 if (strcmp(line, "") != 0)
2132 line_escape = strdup(gw_html_escape(line));
2133 else
2134 line_escape = strdup("");
2136 asprintf(&blame_row, blame_line, a->nlines_prec,
2137 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2138 line_escape);
2139 a->lineno_cur++;
2140 err = buf_puts(&newsize, a->blamebuf, blame_row);
2141 if (err)
2142 return err;
2144 bline = &a->lines[a->lineno_cur - 1];
2145 free(line_escape);
2146 free(blame_row);
2148 done:
2149 if (commit)
2150 got_object_commit_close(commit);
2151 free(line);
2152 return err;
2155 static char*
2156 gw_get_file_blame(struct gw_trans *gw_trans)
2158 const struct got_error *error = NULL;
2159 struct got_repository *repo = NULL;
2160 struct got_object_id *obj_id = NULL;
2161 struct got_object_id *commit_id = NULL;
2162 struct got_blob_object *blob = NULL;
2163 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2164 *folder = NULL;
2165 struct gw_blame_cb_args bca;
2166 int i, obj_type;
2167 size_t filesize;
2169 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2170 if (error)
2171 goto done;
2173 if (gw_trans->repo_folder != NULL) {
2174 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2175 error = got_error_from_errno("asprintf");
2176 goto done;
2178 } else
2179 folder = strdup("");
2181 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2182 error = got_error_from_errno("asprintf");
2183 goto done;
2185 free(folder);
2187 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2188 if (error)
2189 goto done;
2191 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2192 GOT_OBJ_TYPE_COMMIT, 1, repo);
2193 if (error)
2194 goto done;
2196 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2197 if (error)
2198 goto done;
2200 if (obj_id == NULL) {
2201 error = got_error(GOT_ERR_NO_OBJ);
2202 goto done;
2205 error = got_object_get_type(&obj_type, repo, obj_id);
2206 if (error)
2207 goto done;
2209 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2210 error = got_error(GOT_ERR_OBJ_TYPE);
2211 goto done;
2214 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2215 if (error)
2216 goto done;
2218 error = buf_alloc(&bca.blamebuf, 0);
2219 if (error)
2220 goto done;
2222 bca.f = got_opentemp();
2223 if (bca.f == NULL) {
2224 error = got_error_from_errno("got_opentemp");
2225 goto done;
2227 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2228 &bca.line_offsets, bca.f, blob);
2229 if (error || bca.nlines == 0)
2230 goto done;
2232 /* Don't include \n at EOF in the blame line count. */
2233 if (bca.line_offsets[bca.nlines - 1] == filesize)
2234 bca.nlines--;
2236 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2237 if (bca.lines == NULL) {
2238 error = got_error_from_errno("calloc");
2239 goto done;
2241 bca.lineno_cur = 1;
2242 bca.nlines_prec = 0;
2243 i = bca.nlines;
2244 while (i > 0) {
2245 i /= 10;
2246 bca.nlines_prec++;
2248 bca.repo = repo;
2249 bca.gw_trans = gw_trans;
2251 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2252 NULL, NULL);
2253 if (buf_len(bca.blamebuf) > 0) {
2254 error = buf_putc(bca.blamebuf, '\0');
2255 blame_html = strdup(buf_get(bca.blamebuf));
2257 done:
2258 free(bca.blamebuf);
2259 free(in_repo_path);
2260 free(commit_id);
2261 free(obj_id);
2262 free(path);
2264 if (blob)
2265 error = got_object_blob_close(blob);
2266 if (repo)
2267 error = got_repo_close(repo);
2268 if (error)
2269 return NULL;
2270 if (bca.lines) {
2271 for (i = 0; i < bca.nlines; i++) {
2272 struct blame_line *bline = &bca.lines[i];
2273 free(bline->id_str);
2274 free(bline->committer);
2276 free(bca.lines);
2278 free(bca.line_offsets);
2279 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2280 error = got_error_from_errno("fclose");
2281 if (error)
2282 return NULL;
2283 else
2284 return blame_html;
2287 static char*
2288 gw_get_repo_tree(struct gw_trans *gw_trans)
2290 const struct got_error *error = NULL;
2291 struct got_repository *repo = NULL;
2292 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2293 struct got_tree_object *tree = NULL;
2294 struct buf *diffbuf = NULL;
2295 size_t newsize;
2296 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2297 *tree_row = NULL, *id_str;
2298 int nentries, i;
2300 error = buf_alloc(&diffbuf, 0);
2301 if (error)
2302 return NULL;
2304 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2305 if (error)
2306 goto done;
2308 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2309 if (error)
2310 goto done;
2312 if (gw_trans->repo_folder != NULL)
2313 path = strdup(gw_trans->repo_folder);
2314 else if (in_repo_path) {
2315 free(path);
2316 path = in_repo_path;
2319 if (gw_trans->commit == NULL) {
2320 struct got_reference *head_ref;
2321 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2322 if (error)
2323 goto done;
2325 error = got_ref_resolve(&commit_id, repo, head_ref);
2326 got_ref_close(head_ref);
2328 } else
2329 error = got_repo_match_object_id(&commit_id, NULL,
2330 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2331 if (error)
2332 goto done;
2334 error = got_object_id_str(&gw_trans->commit, commit_id);
2335 if (error)
2336 goto done;
2338 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2339 if (error)
2340 goto done;
2342 error = got_object_open_as_tree(&tree, repo, tree_id);
2343 if (error)
2344 goto done;
2346 nentries = got_object_tree_get_nentries(tree);
2348 for (i = 0; i < nentries; i++) {
2349 struct got_tree_entry *te;
2350 const char *modestr = "";
2351 char *id = NULL, *url_html = NULL;
2353 te = got_object_tree_get_entry(tree, i);
2355 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2356 if (error)
2357 goto done;
2359 if ((asprintf(&id, "%s", id_str)) == -1) {
2360 error = got_error_from_errno("asprintf");
2361 free(id_str);
2362 goto done;
2365 mode_t mode = got_tree_entry_get_mode(te);
2367 if (got_object_tree_entry_is_submodule(te))
2368 modestr = "$";
2369 else if (S_ISLNK(mode))
2370 modestr = "@";
2371 else if (S_ISDIR(mode))
2372 modestr = "/";
2373 else if (mode & S_IXUSR)
2374 modestr = "*";
2376 char *build_folder = NULL;
2377 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2378 if (gw_trans->repo_folder != NULL) {
2379 if ((asprintf(&build_folder, "%s/%s",
2380 gw_trans->repo_folder,
2381 got_tree_entry_get_name(te))) == -1) {
2382 error =
2383 got_error_from_errno("asprintf");
2384 goto done;
2386 } else {
2387 if (asprintf(&build_folder, "%s",
2388 got_tree_entry_get_name(te)) == -1)
2389 goto done;
2392 if ((asprintf(&url_html, folder_html,
2393 gw_trans->repo_name, gw_trans->action_name,
2394 gw_trans->commit, build_folder,
2395 got_tree_entry_get_name(te), modestr)) == -1) {
2396 error = got_error_from_errno("asprintf");
2397 goto done;
2399 } else {
2400 if (gw_trans->repo_folder != NULL) {
2401 if ((asprintf(&build_folder, "%s",
2402 gw_trans->repo_folder)) == -1) {
2403 error =
2404 got_error_from_errno("asprintf");
2405 goto done;
2407 } else
2408 build_folder = strdup("");
2410 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2411 "blame", gw_trans->commit,
2412 got_tree_entry_get_name(te), build_folder,
2413 got_tree_entry_get_name(te), modestr)) == -1) {
2414 error = got_error_from_errno("asprintf");
2415 goto done;
2418 free(build_folder);
2420 if (error)
2421 goto done;
2423 if ((asprintf(&tree_row, tree_line, url_html)) == -1) {
2424 error = got_error_from_errno("asprintf");
2425 goto done;
2427 error = buf_puts(&newsize, diffbuf, tree_row);
2428 if (error)
2429 goto done;
2431 free(id);
2432 free(id_str);
2433 free(url_html);
2434 free(tree_row);
2437 if (buf_len(diffbuf) > 0) {
2438 error = buf_putc(diffbuf, '\0');
2439 tree_html = strdup(buf_get(diffbuf));
2441 done:
2442 if (tree)
2443 got_object_tree_close(tree);
2444 if (repo)
2445 got_repo_close(repo);
2447 free(in_repo_path);
2448 free(tree_id);
2449 free(diffbuf);
2450 if (error)
2451 return NULL;
2452 else
2453 return tree_html;
2456 static char *
2457 gw_get_repo_heads(struct gw_trans *gw_trans)
2459 const struct got_error *error = NULL;
2460 struct got_repository *repo = NULL;
2461 struct got_reflist_head refs;
2462 struct got_reflist_entry *re;
2463 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2464 struct buf *diffbuf = NULL;
2465 size_t newsize;
2467 error = buf_alloc(&diffbuf, 0);
2468 if (error)
2469 return NULL;
2471 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2472 if (error)
2473 goto done;
2475 SIMPLEQ_INIT(&refs);
2476 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2477 NULL);
2478 if (error)
2479 goto done;
2481 SIMPLEQ_FOREACH(re, &refs, entry) {
2482 char *refname;
2484 refname = strdup(got_ref_get_name(re->ref));
2485 if (refname == NULL) {
2486 error = got_error_from_errno("got_ref_to_str");
2487 goto done;
2490 if (strncmp(refname, "refs/heads/", 11) != 0) {
2491 free(refname);
2492 continue;
2495 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2496 TM_DIFF);
2498 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2499 refname, gw_trans->repo_name, refname,
2500 gw_trans->repo_name, refname, gw_trans->repo_name,
2501 refname)) == -1) {
2502 error = got_error_from_errno("asprintf");
2503 goto done;
2506 if (strncmp(refname, "refs/heads/", 11) == 0)
2507 refname += 11;
2509 if ((asprintf(&head_row, heads_row, age, refname,
2510 head_navs_disp)) == -1) {
2511 error = got_error_from_errno("asprintf");
2512 goto done;
2515 error = buf_puts(&newsize, diffbuf, head_row);
2517 free(head_navs_disp);
2518 free(head_row);
2521 if (buf_len(diffbuf) > 0) {
2522 error = buf_putc(diffbuf, '\0');
2523 heads = strdup(buf_get(diffbuf));
2525 done:
2526 buf_free(diffbuf);
2527 got_ref_list_free(&refs);
2528 if (repo)
2529 got_repo_close(repo);
2530 if (error)
2531 return NULL;
2532 else
2533 return heads;
2536 static char *
2537 gw_get_got_link(struct gw_trans *gw_trans)
2539 char *link;
2541 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2542 gw_trans->gw_conf->got_logo)) == -1)
2543 return NULL;
2545 return link;
2548 static char *
2549 gw_get_site_link(struct gw_trans *gw_trans)
2551 char *link, *repo = "", *action = "";
2553 if (gw_trans->repo_name != NULL)
2554 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2555 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2556 return NULL;
2558 if (gw_trans->action_name != NULL)
2559 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2560 return NULL;
2562 if ((asprintf(&link, site_link, GOTWEB,
2563 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2564 return NULL;
2566 return link;
2569 static char *
2570 gw_colordiff_line(char *buf)
2572 const struct got_error *error = NULL;
2573 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2574 struct buf *diffbuf = NULL;
2575 size_t newsize;
2577 error = buf_alloc(&diffbuf, 0);
2578 if (error)
2579 return NULL;
2581 if (buf == NULL)
2582 return NULL;
2583 if (strncmp(buf, "-", 1) == 0)
2584 color = "diff_minus";
2585 if (strncmp(buf, "+", 1) == 0)
2586 color = "diff_plus";
2587 if (strncmp(buf, "@@", 2) == 0)
2588 color = "diff_chunk_header";
2589 if (strncmp(buf, "@@", 2) == 0)
2590 color = "diff_chunk_header";
2591 if (strncmp(buf, "commit +", 8) == 0)
2592 color = "diff_meta";
2593 if (strncmp(buf, "commit -", 8) == 0)
2594 color = "diff_meta";
2595 if (strncmp(buf, "blob +", 6) == 0)
2596 color = "diff_meta";
2597 if (strncmp(buf, "blob -", 6) == 0)
2598 color = "diff_meta";
2599 if (strncmp(buf, "file +", 6) == 0)
2600 color = "diff_meta";
2601 if (strncmp(buf, "file -", 6) == 0)
2602 color = "diff_meta";
2603 if (strncmp(buf, "from:", 5) == 0)
2604 color = "diff_author";
2605 if (strncmp(buf, "via:", 4) == 0)
2606 color = "diff_author";
2607 if (strncmp(buf, "date:", 5) == 0)
2608 color = "diff_date";
2610 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2611 return NULL;
2613 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2614 if (error)
2615 return NULL;
2617 error = buf_puts(&newsize, diffbuf, buf);
2618 if (error)
2619 return NULL;
2621 if (buf_len(diffbuf) > 0) {
2622 error = buf_putc(diffbuf, '\0');
2623 colorized_line = strdup(buf_get(diffbuf));
2626 free(diffbuf);
2627 free(div_diff_line_div);
2628 return colorized_line;
2631 static char *
2632 gw_html_escape(const char *html)
2634 char *escaped_str = NULL, *buf;
2635 char c[1];
2636 size_t sz, i, buff_sz = 2048;
2638 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2639 return NULL;
2641 if (html == NULL)
2642 return NULL;
2643 else
2644 if ((sz = strlen(html)) == 0)
2645 return NULL;
2647 /* only work with buff_sz */
2648 if (buff_sz < sz)
2649 sz = buff_sz;
2651 for (i = 0; i < sz; i++) {
2652 c[0] = html[i];
2653 switch (c[0]) {
2654 case ('>'):
2655 strcat(buf, "&gt;");
2656 break;
2657 case ('&'):
2658 strcat(buf, "&amp;");
2659 break;
2660 case ('<'):
2661 strcat(buf, "&lt;");
2662 break;
2663 case ('"'):
2664 strcat(buf, "&quot;");
2665 break;
2666 case ('\''):
2667 strcat(buf, "&apos;");
2668 break;
2669 case ('\n'):
2670 strcat(buf, "<br />");
2671 default:
2672 strcat(buf, &c[0]);
2673 break;
2676 asprintf(&escaped_str, "%s", buf);
2677 free(buf);
2678 return escaped_str;
2681 int
2682 main(int argc, char *argv[])
2684 const struct got_error *error = NULL;
2685 struct gw_trans *gw_trans;
2686 struct gw_dir *dir = NULL, *tdir;
2687 const char *page = "index";
2688 int gw_malloc = 1;
2689 enum kcgi_err kerr;
2691 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2692 errx(1, "malloc");
2694 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2695 errx(1, "malloc");
2697 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2698 errx(1, "malloc");
2700 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2701 errx(1, "malloc");
2703 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2704 if (kerr != KCGI_OK) {
2705 error = gw_kcgi_error(kerr);
2706 goto err;
2709 if ((gw_trans->gw_conf =
2710 malloc(sizeof(struct gotweb_conf))) == NULL) {
2711 gw_malloc = 0;
2712 error = got_error_from_errno("malloc");
2713 goto err;
2716 TAILQ_INIT(&gw_trans->gw_dirs);
2717 TAILQ_INIT(&gw_trans->gw_headers);
2719 gw_trans->page = 0;
2720 gw_trans->repos_total = 0;
2721 gw_trans->repo_path = NULL;
2722 gw_trans->commit = NULL;
2723 gw_trans->headref = strdup(GOT_REF_HEAD);
2724 gw_trans->mime = KMIME_TEXT_HTML;
2725 gw_trans->gw_tmpl->key = gw_templs;
2726 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2727 gw_trans->gw_tmpl->arg = gw_trans;
2728 gw_trans->gw_tmpl->cb = gw_template;
2729 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2731 err:
2732 if (error) {
2733 gw_trans->mime = KMIME_TEXT_PLAIN;
2734 gw_trans->action = GW_ERR;
2735 gw_display_index(gw_trans, error);
2736 goto done;
2739 error = gw_parse_querystring(gw_trans);
2740 if (error)
2741 goto err;
2743 error = gw_display_index(gw_trans, error);
2744 if (error)
2745 goto err;
2747 done:
2748 if (gw_malloc) {
2749 free(gw_trans->gw_conf->got_repos_path);
2750 free(gw_trans->gw_conf->got_www_path);
2751 free(gw_trans->gw_conf->got_site_name);
2752 free(gw_trans->gw_conf->got_site_owner);
2753 free(gw_trans->gw_conf->got_site_link);
2754 free(gw_trans->gw_conf->got_logo);
2755 free(gw_trans->gw_conf->got_logo_url);
2756 free(gw_trans->gw_conf);
2757 free(gw_trans->commit);
2758 free(gw_trans->repo_path);
2759 free(gw_trans->repo_name);
2760 free(gw_trans->repo_file);
2761 free(gw_trans->action_name);
2762 free(gw_trans->headref);
2764 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2765 free(dir->name);
2766 free(dir->description);
2767 free(dir->age);
2768 free(dir->url);
2769 free(dir->path);
2770 free(dir);
2775 khttp_free(gw_trans->gw_req);
2776 return EXIT_SUCCESS;