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 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 = gw_init_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 if (header)
593 got_ref_list_free(&header->refs);
594 gw_free_headers(header);
595 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
596 gw_free_headers(n_header);
597 return error;
600 static const struct got_error *
601 gw_briefs(struct gw_trans *gw_trans)
603 const struct got_error *error = NULL;
604 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
605 struct gw_header *header = NULL, *n_header = NULL;
606 enum kcgi_err kerr;
608 if ((header = gw_init_header()) == NULL)
609 return got_error_from_errno("malloc");
611 if (pledge("stdio rpath proc exec sendfd unveil",
612 NULL) == -1) {
613 error = got_error_from_errno("pledge");
614 return error;
617 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
618 if (error)
619 return error;
621 if (gw_trans->action == GW_SUMMARY)
622 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
623 else
624 error = gw_get_header(gw_trans, header,
625 gw_trans->gw_conf->got_max_commits_display);
627 if (error)
628 return error;
630 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
631 if (kerr != KCGI_OK)
632 return gw_kcgi_error(kerr);
634 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
635 if ((asprintf(&briefs_navs_html, briefs_navs,
636 gw_trans->repo_name, n_header->commit_id,
637 gw_trans->repo_name, n_header->commit_id,
638 gw_trans->repo_name, n_header->commit_id)) == -1)
639 return got_error_from_errno("asprintf");
640 newline = strchr(n_header->commit_msg, '\n');
641 if (newline)
642 *newline = '\0';
643 if ((asprintf(&briefs_html, briefs_line,
644 gw_get_time_str(n_header->committer_time, TM_DIFF),
645 n_header->author, gw_html_escape(n_header->commit_msg),
646 briefs_navs_html)) == -1)
647 return got_error_from_errno("asprintf");
648 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
649 if (kerr != KCGI_OK)
650 return gw_kcgi_error(kerr);
652 kerr = khttp_puts(gw_trans->gw_req, div_end);
653 if (kerr != KCGI_OK)
654 error = gw_kcgi_error(kerr);
656 if (header)
657 got_ref_list_free(&header->refs);
658 gw_free_headers(header);
659 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
660 gw_free_headers(n_header);
661 return error;
664 static const struct got_error *
665 gw_summary(struct gw_trans *gw_trans)
667 const struct got_error *error = NULL;
668 char *description_html, *repo_owner_html, *repo_age_html,
669 *cloneurl_html, *tags, *heads, *tags_html,
670 *heads_html, *age;
671 enum kcgi_err kerr;
673 if (pledge("stdio rpath proc exec sendfd unveil",
674 NULL) == -1) {
675 error = got_error_from_errno("pledge");
676 return error;
679 /* unveil is applied with gw_briefs below */
681 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
682 if (kerr != KCGI_OK)
683 return gw_kcgi_error(kerr);
685 if (gw_trans->gw_conf->got_show_repo_description) {
686 if (gw_trans->gw_dir->description != NULL &&
687 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
688 if ((asprintf(&description_html, description,
689 gw_trans->gw_dir->description)) == -1)
690 return got_error_from_errno("asprintf");
692 kerr = khttp_puts(gw_trans->gw_req, description_html);
693 free(description_html);
694 if (kerr != KCGI_OK)
695 return gw_kcgi_error(kerr);
699 if (gw_trans->gw_conf->got_show_repo_owner) {
700 if (gw_trans->gw_dir->owner != NULL &&
701 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
702 if ((asprintf(&repo_owner_html, repo_owner,
703 gw_trans->gw_dir->owner)) == -1)
704 return got_error_from_errno("asprintf");
706 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
707 free(repo_owner_html);
708 if (kerr != KCGI_OK)
709 return gw_kcgi_error(kerr);
713 if (gw_trans->gw_conf->got_show_repo_age) {
714 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
715 "refs/heads", TM_LONG);
716 if (age != NULL && (strcmp(age, "") != 0)) {
717 if ((asprintf(&repo_age_html, last_change, age)) == -1)
718 return got_error_from_errno("asprintf");
720 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
721 free(repo_age_html);
722 free(age);
723 if (kerr != KCGI_OK)
724 return gw_kcgi_error(kerr);
728 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
729 if (gw_trans->gw_dir->url != NULL &&
730 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
731 if ((asprintf(&cloneurl_html, cloneurl,
732 gw_trans->gw_dir->url)) == -1)
733 return got_error_from_errno("asprintf");
735 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
736 free(cloneurl_html);
737 if (kerr != KCGI_OK)
738 return gw_kcgi_error(kerr);
741 kerr = khttp_puts(gw_trans->gw_req, div_end);
742 if (kerr != KCGI_OK)
743 return gw_kcgi_error(kerr);
745 error = gw_briefs(gw_trans);
746 if (error)
747 return error;
749 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
750 heads = gw_get_repo_heads(gw_trans);
752 if (tags != NULL && strcmp(tags, "") != 0) {
753 if ((asprintf(&tags_html, summary_tags,
754 tags)) == -1)
755 return got_error_from_errno("asprintf");
756 kerr = khttp_puts(gw_trans->gw_req, tags_html);
757 free(tags_html);
758 free(tags);
759 if (kerr != KCGI_OK)
760 return gw_kcgi_error(kerr);
763 if (heads != NULL && strcmp(heads, "") != 0) {
764 if ((asprintf(&heads_html, summary_heads,
765 heads)) == -1)
766 return got_error_from_errno("asprintf");
767 kerr = khttp_puts(gw_trans->gw_req, heads_html);
768 free(heads_html);
769 free(heads);
770 if (kerr != KCGI_OK)
771 return gw_kcgi_error(kerr);
773 return error;
776 static const struct got_error *
777 gw_tree(struct gw_trans *gw_trans)
779 const struct got_error *error = NULL;
780 struct gw_header *header = NULL;
781 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
782 enum kcgi_err kerr;
784 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
785 return got_error_from_errno("pledge");
787 if ((header = gw_init_header()) == NULL)
788 return got_error_from_errno("malloc");
790 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
791 if (error)
792 return error;
794 error = gw_get_header(gw_trans, header, 1);
795 if (error)
796 return error;
798 tree_html = gw_get_repo_tree(gw_trans);
800 if (tree_html == NULL)
801 tree_html = strdup("");
803 if ((asprintf(&tree_html_disp, tree_header,
804 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
805 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
806 tree_html)) == -1)
807 return got_error_from_errno("asprintf");
809 if ((asprintf(&tree, tree_wrapper, tree_html_disp)) == -1)
810 return got_error_from_errno("asprintf");
812 kerr = khttp_puts(gw_trans->gw_req, tree);
813 if (kerr != KCGI_OK)
814 error = gw_kcgi_error(kerr);
815 if (header)
816 got_ref_list_free(&header->refs);
817 gw_free_headers(header);
818 free(tree_html_disp);
819 free(tree_html);
820 free(tree);
821 return error;
824 static const struct got_error *
825 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
827 const struct got_error *error = NULL;
828 DIR *dt;
829 char *dir_test;
830 int opened = 0;
832 if ((asprintf(&dir_test, "%s/%s/%s",
833 gw_trans->gw_conf->got_repos_path, gw_dir->name,
834 GOTWEB_GIT_DIR)) == -1)
835 return got_error_from_errno("asprintf");
837 dt = opendir(dir_test);
838 if (dt == NULL) {
839 free(dir_test);
840 } else {
841 gw_dir->path = strdup(dir_test);
842 opened = 1;
843 goto done;
846 if ((asprintf(&dir_test, "%s/%s/%s",
847 gw_trans->gw_conf->got_repos_path, gw_dir->name,
848 GOTWEB_GOT_DIR)) == -1)
849 return got_error_from_errno("asprintf");
851 dt = opendir(dir_test);
852 if (dt == NULL)
853 free(dir_test);
854 else {
855 opened = 1;
856 error = got_error(GOT_ERR_NOT_GIT_REPO);
857 goto errored;
860 if ((asprintf(&dir_test, "%s/%s",
861 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
862 return got_error_from_errno("asprintf");
864 gw_dir->path = strdup(dir_test);
866 done:
867 gw_dir->description = gw_get_repo_description(gw_trans,
868 gw_dir->path);
869 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
870 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
871 TM_DIFF);
872 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
874 errored:
875 free(dir_test);
876 if (opened)
877 closedir(dt);
878 return error;
881 static const struct got_error *
882 gw_load_got_paths(struct gw_trans *gw_trans)
884 const struct got_error *error = NULL;
885 DIR *d;
886 struct dirent **sd_dent;
887 struct gw_dir *gw_dir;
888 struct stat st;
889 unsigned int d_cnt, d_i;
891 d = opendir(gw_trans->gw_conf->got_repos_path);
892 if (d == NULL) {
893 error = got_error_from_errno2("opendir",
894 gw_trans->gw_conf->got_repos_path);
895 return error;
898 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
899 alphasort);
900 if (d_cnt == -1) {
901 error = got_error_from_errno2("scandir",
902 gw_trans->gw_conf->got_repos_path);
903 return error;
906 for (d_i = 0; d_i < d_cnt; d_i++) {
907 if (gw_trans->gw_conf->got_max_repos > 0 &&
908 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
909 break; /* account for parent and self */
911 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
912 strcmp(sd_dent[d_i]->d_name, "..") == 0)
913 continue;
915 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
916 return got_error_from_errno("gw_dir malloc");
918 error = gw_load_got_path(gw_trans, gw_dir);
919 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
920 continue;
921 else if (error)
922 return error;
924 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
925 !got_path_dir_is_empty(gw_dir->path)) {
926 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
927 entry);
928 gw_trans->repos_total++;
932 closedir(d);
933 return error;
936 static const struct got_error *
937 gw_parse_querystring(struct gw_trans *gw_trans)
939 const struct got_error *error = NULL;
940 struct kpair *p;
941 struct gw_query_action *action = NULL;
942 unsigned int i;
944 if (gw_trans->gw_req->fieldnmap[0]) {
945 error = got_error_from_errno("bad parse");
946 return error;
947 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
948 /* define gw_trans->repo_path */
949 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
950 return got_error_from_errno("asprintf");
952 if ((asprintf(&gw_trans->repo_path, "%s/%s",
953 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
954 return got_error_from_errno("asprintf");
956 /* get action and set function */
957 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
958 for (i = 0; i < nitems(gw_query_funcs); i++) {
959 action = &gw_query_funcs[i];
960 if (action->func_name == NULL)
961 continue;
963 if (strcmp(action->func_name,
964 p->parsed.s) == 0) {
965 gw_trans->action = i;
966 if ((asprintf(&gw_trans->action_name,
967 "%s", action->func_name)) == -1)
968 return
969 got_error_from_errno(
970 "asprintf");
972 break;
975 action = NULL;
978 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
979 if ((asprintf(&gw_trans->commit, "%s",
980 p->parsed.s)) == -1)
981 return got_error_from_errno("asprintf");
983 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
984 if ((asprintf(&gw_trans->repo_file, "%s",
985 p->parsed.s)) == -1)
986 return got_error_from_errno("asprintf");
988 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
989 if ((asprintf(&gw_trans->repo_folder, "%s",
990 p->parsed.s)) == -1)
991 return got_error_from_errno("asprintf");
993 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
994 if ((asprintf(&gw_trans->headref, "%s",
995 p->parsed.s)) == -1)
996 return got_error_from_errno("asprintf");
998 if (action == NULL) {
999 error = got_error_from_errno("invalid action");
1000 return error;
1002 if ((gw_trans->gw_dir =
1003 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1004 return got_error_from_errno("gw_dir malloc");
1006 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1007 if (error)
1008 return error;
1009 } else
1010 gw_trans->action = GW_INDEX;
1012 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1013 gw_trans->page = p->parsed.i;
1015 /* if (gw_trans->action == GW_RAW) */
1016 /* gw_trans->mime = KMIME_TEXT_PLAIN; */
1018 return error;
1021 static struct gw_dir *
1022 gw_init_gw_dir(char *dir)
1024 struct gw_dir *gw_dir;
1026 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1027 return NULL;
1029 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1030 return NULL;
1032 return gw_dir;
1035 static const struct got_error *
1036 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1038 enum kcgi_err kerr;
1040 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1041 if (kerr != KCGI_OK)
1042 return gw_kcgi_error(kerr);
1043 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1044 khttps[code]);
1045 if (kerr != KCGI_OK)
1046 return gw_kcgi_error(kerr);
1047 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1048 kmimetypes[mime]);
1049 if (kerr != KCGI_OK)
1050 return gw_kcgi_error(kerr);
1051 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1052 if (kerr != KCGI_OK)
1053 return gw_kcgi_error(kerr);
1054 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1055 if (kerr != KCGI_OK)
1056 return gw_kcgi_error(kerr);
1057 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1058 if (kerr != KCGI_OK)
1059 return gw_kcgi_error(kerr);
1061 kerr = khttp_body(gw_trans->gw_req);
1062 return gw_kcgi_error(kerr);
1065 static const struct got_error *
1066 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
1068 enum kcgi_err kerr;
1070 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1071 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1073 if (err)
1074 kerr = khttp_puts(gw_trans->gw_req, err->msg);
1075 else
1076 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1077 gw_query_funcs[gw_trans->action].template);
1078 if (kerr != KCGI_OK)
1079 return gw_kcgi_error(kerr);
1081 kerr = khtml_close(gw_trans->gw_html_req);
1082 return gw_kcgi_error(kerr);
1085 static int
1086 gw_template(size_t key, void *arg)
1088 const struct got_error *error = NULL;
1089 struct gw_trans *gw_trans = arg;
1090 char *gw_got_link, *gw_site_link;
1091 char *site_owner_name, *site_owner_name_h;
1093 switch (key) {
1094 case (TEMPL_HEAD):
1095 khttp_puts(gw_trans->gw_req, head);
1096 break;
1097 case(TEMPL_HEADER):
1098 gw_got_link = gw_get_got_link(gw_trans);
1099 if (gw_got_link != NULL)
1100 khttp_puts(gw_trans->gw_req, gw_got_link);
1102 free(gw_got_link);
1103 break;
1104 case (TEMPL_SITEPATH):
1105 gw_site_link = gw_get_site_link(gw_trans);
1106 if (gw_site_link != NULL)
1107 khttp_puts(gw_trans->gw_req, gw_site_link);
1109 free(gw_site_link);
1110 break;
1111 case(TEMPL_TITLE):
1112 if (gw_trans->gw_conf->got_site_name != NULL)
1113 khtml_puts(gw_trans->gw_html_req,
1114 gw_trans->gw_conf->got_site_name);
1116 break;
1117 case (TEMPL_SEARCH):
1118 khttp_puts(gw_trans->gw_req, search);
1119 break;
1120 case(TEMPL_SITEOWNER):
1121 if (gw_trans->gw_conf->got_site_owner != NULL &&
1122 gw_trans->gw_conf->got_show_site_owner) {
1123 site_owner_name =
1124 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1125 if ((asprintf(&site_owner_name_h, site_owner,
1126 site_owner_name))
1127 == -1)
1128 return 0;
1130 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1131 free(site_owner_name);
1132 free(site_owner_name_h);
1134 break;
1135 case(TEMPL_CONTENT):
1136 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1137 if (error)
1138 khttp_puts(gw_trans->gw_req, error->msg);
1139 break;
1140 default:
1141 return 0;
1143 return 1;
1146 static char *
1147 gw_gen_commit_header(char *str1, char *str2)
1149 char *return_html = NULL, *ref_str = NULL;
1151 if (strcmp(str2, "") != 0) {
1152 if ((asprintf(&ref_str, "(%s)", str2)) == -1) {
1153 return_html = strdup("");
1154 return return_html;
1156 } else
1157 ref_str = strdup("");
1160 if ((asprintf(&return_html, header_commit_html, str1, ref_str)) == -1)
1161 return_html = strdup("");
1163 free(ref_str);
1164 return return_html;
1167 static char *
1168 gw_gen_diff_header(char *str1, char *str2)
1170 char *return_html = NULL;
1172 if ((asprintf(&return_html, header_diff_html, str1, str2)) == -1)
1173 return_html = strdup("");
1175 return return_html;
1178 static char *
1179 gw_gen_author_header(char *str)
1181 char *return_html = NULL;
1183 if ((asprintf(&return_html, header_author_html, str)) == -1)
1184 return_html = strdup("");
1186 return return_html;
1189 static char *
1190 gw_gen_committer_header(char *str)
1192 char *return_html = NULL;
1194 if ((asprintf(&return_html, header_committer_html, str)) == -1)
1195 return_html = strdup("");
1197 return return_html;
1200 static char *
1201 gw_gen_age_header(char *str)
1203 char *return_html = NULL;
1205 if ((asprintf(&return_html, header_age_html, str)) == -1)
1206 return_html = strdup("");
1208 return return_html;
1211 static char *
1212 gw_gen_commit_msg_header(char *str)
1214 char *return_html = NULL;
1216 if ((asprintf(&return_html, header_commit_msg_html, str)) == -1)
1217 return_html = strdup("");
1219 return return_html;
1222 static char *
1223 gw_gen_tree_header(char *str)
1225 char *return_html = NULL;
1227 if ((asprintf(&return_html, header_tree_html, str)) == -1)
1228 return_html = strdup("");
1230 return return_html;
1233 static char *
1234 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1236 FILE *f;
1237 char *description = NULL, *d_file = NULL;
1238 unsigned int len;
1240 if (gw_trans->gw_conf->got_show_repo_description == false)
1241 goto err;
1243 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1244 goto err;
1246 if ((f = fopen(d_file, "r")) == NULL)
1247 goto err;
1249 fseek(f, 0, SEEK_END);
1250 len = ftell(f) + 1;
1251 fseek(f, 0, SEEK_SET);
1252 if ((description = calloc(len, sizeof(char *))) == NULL)
1253 goto err;
1255 fread(description, 1, len, f);
1256 fclose(f);
1257 free(d_file);
1258 return description;
1259 err:
1260 if ((asprintf(&description, "%s", "")) == -1)
1261 return NULL;
1263 return description;
1266 static char *
1267 gw_get_time_str(time_t committer_time, int ref_tm)
1269 struct tm tm;
1270 time_t diff_time;
1271 char *years = "years ago", *months = "months ago";
1272 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1273 char *minutes = "minutes ago", *seconds = "seconds ago";
1274 char *now = "right now";
1275 char *repo_age, *s;
1276 char datebuf[29];
1278 switch (ref_tm) {
1279 case TM_DIFF:
1280 diff_time = time(NULL) - committer_time;
1281 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1282 if ((asprintf(&repo_age, "%lld %s",
1283 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1284 return NULL;
1285 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1286 if ((asprintf(&repo_age, "%lld %s",
1287 (diff_time / 60 / 60 / 24 / (365 / 12)),
1288 months)) == -1)
1289 return NULL;
1290 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1291 if ((asprintf(&repo_age, "%lld %s",
1292 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1293 return NULL;
1294 } else if (diff_time > 60 * 60 * 24 * 2) {
1295 if ((asprintf(&repo_age, "%lld %s",
1296 (diff_time / 60 / 60 / 24), days)) == -1)
1297 return NULL;
1298 } else if (diff_time > 60 * 60 * 2) {
1299 if ((asprintf(&repo_age, "%lld %s",
1300 (diff_time / 60 / 60), hours)) == -1)
1301 return NULL;
1302 } else if (diff_time > 60 * 2) {
1303 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1304 minutes)) == -1)
1305 return NULL;
1306 } else if (diff_time > 2) {
1307 if ((asprintf(&repo_age, "%lld %s", diff_time,
1308 seconds)) == -1)
1309 return NULL;
1310 } else {
1311 if ((asprintf(&repo_age, "%s", now)) == -1)
1312 return NULL;
1314 break;
1315 case TM_LONG:
1316 if (gmtime_r(&committer_time, &tm) == NULL)
1317 return NULL;
1319 s = asctime_r(&tm, datebuf);
1320 if (s == NULL)
1321 return NULL;
1323 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1324 return NULL;
1325 break;
1327 return repo_age;
1330 static char *
1331 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1332 int ref_tm)
1334 const struct got_error *error = NULL;
1335 struct got_object_id *id = NULL;
1336 struct got_repository *repo = NULL;
1337 struct got_commit_object *commit = NULL;
1338 struct got_reflist_head refs;
1339 struct got_reflist_entry *re;
1340 struct got_reference *head_ref;
1341 int is_head = 0;
1342 time_t committer_time = 0, cmp_time = 0;
1343 const char *refname;
1344 char *repo_age = NULL;
1346 if (repo_ref == NULL)
1347 return NULL;
1349 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1350 is_head = 1;
1352 SIMPLEQ_INIT(&refs);
1353 if (gw_trans->gw_conf->got_show_repo_age == false) {
1354 if ((asprintf(&repo_age, "")) == -1)
1355 return NULL;
1356 return repo_age;
1359 error = got_repo_open(&repo, dir, NULL);
1360 if (error)
1361 goto err;
1363 if (is_head)
1364 error = got_ref_list(&refs, repo, "refs/heads",
1365 got_ref_cmp_by_name, NULL);
1366 else
1367 error = got_ref_list(&refs, repo, repo_ref,
1368 got_ref_cmp_by_name, NULL);
1369 if (error)
1370 goto err;
1372 SIMPLEQ_FOREACH(re, &refs, entry) {
1373 if (is_head)
1374 refname = strdup(repo_ref);
1375 else
1376 refname = got_ref_get_name(re->ref);
1377 error = got_ref_open(&head_ref, repo, refname, 0);
1378 if (error)
1379 goto err;
1381 error = got_ref_resolve(&id, repo, head_ref);
1382 got_ref_close(head_ref);
1383 if (error)
1384 goto err;
1386 error = got_object_open_as_commit(&commit, repo, id);
1387 if (error)
1388 goto err;
1390 committer_time =
1391 got_object_commit_get_committer_time(commit);
1393 if (cmp_time < committer_time)
1394 cmp_time = committer_time;
1397 if (cmp_time != 0) {
1398 committer_time = cmp_time;
1399 repo_age = gw_get_time_str(committer_time, ref_tm);
1400 } else
1401 if ((asprintf(&repo_age, "")) == -1)
1402 return NULL;
1403 got_ref_list_free(&refs);
1404 free(id);
1405 return repo_age;
1406 err:
1407 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1408 return NULL;
1410 return repo_age;
1413 static char *
1414 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1416 const struct got_error *error;
1417 FILE *f = NULL;
1418 struct got_object_id *id1 = NULL, *id2 = NULL;
1419 struct buf *diffbuf = NULL;
1420 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1421 *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1422 int obj_type;
1423 size_t newsize;
1425 f = got_opentemp();
1426 if (f == NULL)
1427 return NULL;
1429 error = buf_alloc(&diffbuf, 0);
1430 if (error)
1431 return NULL;
1433 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1434 if (error)
1435 goto done;
1437 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1438 error = got_repo_match_object_id(&id1, &label1,
1439 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1440 if (error)
1441 goto done;
1444 error = got_repo_match_object_id(&id2, &label2,
1445 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1446 if (error)
1447 goto done;
1449 error = got_object_get_type(&obj_type, header->repo, id2);
1450 if (error)
1451 goto done;
1452 switch (obj_type) {
1453 case GOT_OBJ_TYPE_BLOB:
1454 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1455 header->repo, f);
1456 break;
1457 case GOT_OBJ_TYPE_TREE:
1458 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1459 header->repo, f);
1460 break;
1461 case GOT_OBJ_TYPE_COMMIT:
1462 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1463 header->repo, f);
1464 break;
1465 default:
1466 error = got_error(GOT_ERR_OBJ_TYPE);
1469 if ((buf = calloc(128, sizeof(char *))) == NULL)
1470 goto done;
1472 fseek(f, 0, SEEK_SET);
1474 while ((fgets(buf, 2048, f)) != NULL) {
1475 n_buf = buf;
1476 while (*n_buf == '\n')
1477 n_buf++;
1478 newline = strchr(n_buf, '\n');
1479 if (newline)
1480 *newline = ' ';
1482 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1483 if (buf_color == NULL)
1484 continue;
1486 error = buf_puts(&newsize, diffbuf, buf_color);
1487 if (error)
1488 return NULL;
1490 error = buf_puts(&newsize, diffbuf, div_end);
1491 if (error)
1492 return NULL;
1495 if (buf_len(diffbuf) > 0) {
1496 error = buf_putc(diffbuf, '\0');
1497 diff_html = strdup(buf_get(diffbuf));
1499 done:
1500 fclose(f);
1501 free(buf_color);
1502 free(buf);
1503 free(diffbuf);
1504 free(label1);
1505 free(label2);
1506 free(id1);
1507 free(id2);
1509 if (error)
1510 return NULL;
1511 else
1512 return diff_html;
1515 static char *
1516 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1518 FILE *f;
1519 char *owner = NULL, *d_file = NULL;
1520 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1521 char *comp, *pos, *buf;
1522 unsigned int i;
1524 if (gw_trans->gw_conf->got_show_repo_owner == false)
1525 goto err;
1527 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1528 goto err;
1530 if ((f = fopen(d_file, "r")) == NULL)
1531 goto err;
1533 if ((buf = calloc(128, sizeof(char *))) == NULL)
1534 goto err;
1536 while ((fgets(buf, 128, f)) != NULL) {
1537 if ((pos = strstr(buf, gotweb)) != NULL)
1538 break;
1540 if ((pos = strstr(buf, gitweb)) != NULL)
1541 break;
1544 if (pos == NULL)
1545 goto err;
1547 do {
1548 fgets(buf, 128, f);
1549 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1551 if (comp == NULL)
1552 goto err;
1554 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1555 goto err;
1557 for (i = 0; i < 2; i++) {
1558 owner = strsep(&buf, "\"");
1561 if (owner == NULL)
1562 goto err;
1564 fclose(f);
1565 free(d_file);
1566 return owner;
1567 err:
1568 if ((asprintf(&owner, "%s", "")) == -1)
1569 return NULL;
1571 return owner;
1574 static char *
1575 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1577 FILE *f;
1578 char *url = NULL, *d_file = NULL;
1579 unsigned int len;
1581 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1582 return NULL;
1584 if ((f = fopen(d_file, "r")) == NULL)
1585 return NULL;
1587 fseek(f, 0, SEEK_END);
1588 len = ftell(f) + 1;
1589 fseek(f, 0, SEEK_SET);
1591 if ((url = calloc(len, sizeof(char *))) == NULL)
1592 return NULL;
1594 fread(url, 1, len, f);
1595 fclose(f);
1596 free(d_file);
1597 return url;
1600 static char *
1601 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
1603 const struct got_error *error = NULL;
1604 struct got_repository *repo = NULL;
1605 struct got_reflist_head refs;
1606 struct got_reflist_entry *re;
1607 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1608 *age = NULL;
1609 char *newline;
1610 struct buf *diffbuf = NULL;
1611 size_t newsize;
1613 error = buf_alloc(&diffbuf, 0);
1614 if (error)
1615 return NULL;
1616 SIMPLEQ_INIT(&refs);
1618 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1619 if (error)
1620 goto done;
1622 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1623 if (error)
1624 goto done;
1626 SIMPLEQ_FOREACH(re, &refs, entry) {
1627 const char *refname;
1628 char *refstr, *tag_commit0, *tag_commit, *id_str;
1629 time_t tagger_time;
1630 struct got_object_id *id;
1631 struct got_tag_object *tag;
1633 refname = got_ref_get_name(re->ref);
1634 if (strncmp(refname, "refs/tags/", 10) != 0)
1635 continue;
1636 refname += 10;
1637 refstr = got_ref_to_str(re->ref);
1638 if (refstr == NULL) {
1639 error = got_error_from_errno("got_ref_to_str");
1640 goto done;
1643 error = got_ref_resolve(&id, repo, re->ref);
1644 if (error)
1645 goto done;
1646 error = got_object_open_as_tag(&tag, repo, id);
1647 free(id);
1648 if (error)
1649 goto done;
1651 tagger_time = got_object_tag_get_tagger_time(tag);
1653 error = got_object_id_str(&id_str,
1654 got_object_tag_get_object_id(tag));
1655 if (error)
1656 goto done;
1658 tag_commit0 = strdup(got_object_tag_get_message(tag));
1660 if (tag_commit0 == NULL) {
1661 error = got_error_from_errno("strdup");
1662 goto done;
1665 tag_commit = tag_commit0;
1666 while (*tag_commit == '\n')
1667 tag_commit++;
1669 switch (tag_type) {
1670 case TAGBRIEF:
1671 newline = strchr(tag_commit, '\n');
1672 if (newline)
1673 *newline = '\0';
1675 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1676 TM_DIFF))) == -1) {
1677 error = got_error_from_errno("asprintf");
1678 goto done;
1681 if ((asprintf(&tags_navs_disp, tags_navs,
1682 gw_trans->repo_name, id_str, gw_trans->repo_name,
1683 id_str, gw_trans->repo_name, id_str,
1684 gw_trans->repo_name, id_str)) == -1) {
1685 error = got_error_from_errno("asprintf");
1686 goto done;
1689 if ((asprintf(&tag_row, tags_row, age, refname,
1690 tag_commit, tags_navs_disp)) == -1) {
1691 error = got_error_from_errno("asprintf");
1692 goto done;
1695 free(tags_navs_disp);
1696 break;
1697 case TAGFULL:
1698 break;
1699 default:
1700 break;
1703 got_object_tag_close(tag);
1705 error = buf_puts(&newsize, diffbuf, tag_row);
1707 free(id_str);
1708 free(refstr);
1709 free(age);
1710 free(tag_commit0);
1711 free(tag_row);
1713 if (error || (limit && --limit == 0))
1714 break;
1717 if (buf_len(diffbuf) > 0) {
1718 error = buf_putc(diffbuf, '\0');
1719 tags = strdup(buf_get(diffbuf));
1721 done:
1722 buf_free(diffbuf);
1723 got_ref_list_free(&refs);
1724 if (repo)
1725 got_repo_close(repo);
1726 if (error)
1727 return NULL;
1728 else
1729 return tags;
1732 static void
1733 gw_free_headers(struct gw_header *header)
1735 free(header->id);
1736 free(header->path);
1737 if (header->commit != NULL)
1738 got_object_commit_close(header->commit);
1739 if (header->repo)
1740 got_repo_close(header->repo);
1741 free(header->refs_str);
1742 free(header->commit_id);
1743 free(header->parent_id);
1744 free(header->tree_id);
1745 free(header->author);
1746 free(header->committer);
1747 free(header->commit_msg);
1750 static struct gw_header *
1751 gw_init_header()
1753 struct gw_header *header;
1755 header = malloc(sizeof(*header));
1756 if (header == NULL)
1757 return NULL;
1759 header->repo = NULL;
1760 header->commit = NULL;
1761 header->id = NULL;
1762 header->path = NULL;
1763 SIMPLEQ_INIT(&header->refs);
1765 return header;
1768 static const struct got_error *
1769 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1770 int limit)
1772 const struct got_error *error = NULL;
1773 struct got_commit_graph *graph = NULL;
1775 error = got_commit_graph_open(&graph, header->path, 0);
1776 if (error)
1777 goto done;
1779 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1780 NULL, NULL);
1781 if (error)
1782 goto done;
1784 for (;;) {
1785 error = got_commit_graph_iter_next(&header->id, graph,
1786 header->repo, NULL, NULL);
1787 if (error) {
1788 if (error->code == GOT_ERR_ITER_COMPLETED)
1789 error = NULL;
1790 goto done;
1792 if (header->id == NULL)
1793 goto done;
1795 error = got_object_open_as_commit(&header->commit, header->repo,
1796 header->id);
1797 if (error)
1798 goto done;
1800 error = gw_get_commit(gw_trans, header);
1801 if (limit > 1) {
1802 struct gw_header *n_header = NULL;
1803 if ((n_header = gw_init_header()) == NULL)
1804 error = got_error_from_errno("malloc");
1806 n_header->refs_str = strdup(header->refs_str);
1807 n_header->commit_id = strdup(header->commit_id);
1808 n_header->parent_id = strdup(header->parent_id);
1809 n_header->tree_id = strdup(header->tree_id);
1810 n_header->author = strdup(header->author);
1811 n_header->committer = strdup(header->committer);
1812 n_header->commit_msg = strdup(header->commit_msg);
1813 n_header->committer_time = header->committer_time;
1814 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
1815 entry);
1817 if (error || (limit && --limit == 0))
1818 break;
1820 done:
1821 if (graph)
1822 got_commit_graph_close(graph);
1823 return error;
1826 static const struct got_error *
1827 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
1829 const struct got_error *error = NULL;
1830 struct got_reflist_entry *re;
1831 struct got_object_id *id2 = NULL;
1832 struct got_object_qid *parent_id;
1833 char *refs_str = NULL,
1834 *commit_msg = NULL, *commit_msg0;
1836 /*print commit*/
1837 SIMPLEQ_FOREACH(re, &header->refs, entry) {
1838 char *s;
1839 const char *name;
1840 struct got_tag_object *tag = NULL;
1841 int cmp;
1843 name = got_ref_get_name(re->ref);
1844 if (strcmp(name, GOT_REF_HEAD) == 0)
1845 continue;
1846 if (strncmp(name, "refs/", 5) == 0)
1847 name += 5;
1848 if (strncmp(name, "got/", 4) == 0)
1849 continue;
1850 if (strncmp(name, "heads/", 6) == 0)
1851 name += 6;
1852 if (strncmp(name, "remotes/", 8) == 0)
1853 name += 8;
1854 if (strncmp(name, "tags/", 5) == 0) {
1855 error = got_object_open_as_tag(&tag, header->repo,
1856 re->id);
1857 if (error) {
1858 if (error->code != GOT_ERR_OBJ_TYPE)
1859 continue;
1861 * Ref points at something other
1862 * than a tag.
1864 error = NULL;
1865 tag = NULL;
1868 cmp = got_object_id_cmp(tag ?
1869 got_object_tag_get_object_id(tag) : re->id, header->id);
1870 if (tag)
1871 got_object_tag_close(tag);
1872 if (cmp != 0)
1873 continue;
1874 s = refs_str;
1875 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1876 s ? ", " : "", name)) == -1) {
1877 error = got_error_from_errno("asprintf");
1878 free(s);
1879 return error;
1881 header->refs_str = strdup(refs_str);
1882 free(s);
1885 if (refs_str == NULL)
1886 header->refs_str = strdup("");
1887 free(refs_str);
1889 error = got_object_id_str(&header->commit_id, header->id);
1890 if (error)
1891 return error;
1893 error = got_object_id_str(&header->tree_id,
1894 got_object_commit_get_tree_id(header->commit));
1895 if (error)
1896 return error;
1898 if (gw_trans->action == GW_DIFF) {
1899 parent_id = SIMPLEQ_FIRST(
1900 got_object_commit_get_parent_ids(header->commit));
1901 if (parent_id != NULL) {
1902 id2 = got_object_id_dup(parent_id->id);
1903 free (parent_id);
1904 error = got_object_id_str(&header->parent_id, id2);
1905 if (error)
1906 return error;
1907 free(id2);
1908 } else
1909 header->parent_id = strdup("/dev/null");
1910 } else
1911 header->parent_id = strdup("");
1913 header->committer_time =
1914 got_object_commit_get_committer_time(header->commit);
1915 header->author = strdup(got_object_commit_get_author(header->commit));
1916 header->committer =
1917 strdup(got_object_commit_get_committer(header->commit));
1919 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
1920 if (error)
1921 return error;
1923 commit_msg = commit_msg0;
1924 while (*commit_msg == '\n')
1925 commit_msg++;
1927 header->commit_msg = strdup(commit_msg);
1928 free(commit_msg0);
1929 return error;
1932 static const struct got_error *
1933 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
1935 const struct got_error *error = NULL;
1936 char *in_repo_path = NULL;
1938 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1939 if (error)
1940 return error;
1942 if (gw_trans->commit == NULL) {
1943 struct got_reference *head_ref;
1944 error = got_ref_open(&head_ref, header->repo,
1945 gw_trans->headref, 0);
1946 if (error)
1947 return error;
1949 error = got_ref_resolve(&header->id, header->repo, head_ref);
1950 got_ref_close(head_ref);
1951 if (error)
1952 return error;
1954 error = got_object_open_as_commit(&header->commit,
1955 header->repo, header->id);
1956 } else {
1957 struct got_reference *ref;
1958 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
1959 if (error == NULL) {
1960 int obj_type;
1961 error = got_ref_resolve(&header->id, header->repo, ref);
1962 got_ref_close(ref);
1963 if (error)
1964 return error;
1965 error = got_object_get_type(&obj_type, header->repo,
1966 header->id);
1967 if (error)
1968 return error;
1969 if (obj_type == GOT_OBJ_TYPE_TAG) {
1970 struct got_tag_object *tag;
1971 error = got_object_open_as_tag(&tag,
1972 header->repo, header->id);
1973 if (error)
1974 return error;
1975 if (got_object_tag_get_object_type(tag) !=
1976 GOT_OBJ_TYPE_COMMIT) {
1977 got_object_tag_close(tag);
1978 error = got_error(GOT_ERR_OBJ_TYPE);
1979 return error;
1981 free(header->id);
1982 header->id = got_object_id_dup(
1983 got_object_tag_get_object_id(tag));
1984 if (header->id == NULL)
1985 error = got_error_from_errno(
1986 "got_object_id_dup");
1987 got_object_tag_close(tag);
1988 if (error)
1989 return error;
1990 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1991 error = got_error(GOT_ERR_OBJ_TYPE);
1992 return error;
1994 error = got_object_open_as_commit(&header->commit,
1995 header->repo, header->id);
1996 if (error)
1997 return error;
1999 if (header->commit == NULL) {
2000 error = got_repo_match_object_id_prefix(&header->id,
2001 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2002 header->repo);
2003 if (error)
2004 return error;
2006 error = got_repo_match_object_id_prefix(&header->id,
2007 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2008 header->repo);
2011 error = got_repo_map_path(&in_repo_path, header->repo,
2012 gw_trans->repo_path, 1);
2013 if (error)
2014 return error;
2016 if (in_repo_path) {
2017 header->path = strdup(in_repo_path);
2019 free(in_repo_path);
2021 error = got_ref_list(&header->refs, header->repo, NULL,
2022 got_ref_cmp_by_name, NULL);
2023 if (error)
2024 return error;
2026 error = gw_get_commits(gw_trans, header, limit);
2027 return error;
2030 struct blame_line {
2031 int annotated;
2032 char *id_str;
2033 char *committer;
2034 char datebuf[11]; /* YYYY-MM-DD + NUL */
2037 struct gw_blame_cb_args {
2038 struct blame_line *lines;
2039 int nlines;
2040 int nlines_prec;
2041 int lineno_cur;
2042 off_t *line_offsets;
2043 FILE *f;
2044 struct got_repository *repo;
2045 struct gw_trans *gw_trans;
2046 struct buf *blamebuf;
2049 static const struct got_error *
2050 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2052 const struct got_error *err = NULL;
2053 struct gw_blame_cb_args *a = arg;
2054 struct blame_line *bline;
2055 char *line = NULL;
2056 size_t linesize = 0, newsize;
2057 struct got_commit_object *commit = NULL;
2058 off_t offset;
2059 struct tm tm;
2060 time_t committer_time;
2062 if (nlines != a->nlines ||
2063 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2064 return got_error(GOT_ERR_RANGE);
2066 if (lineno == -1)
2067 return NULL; /* no change in this commit */
2069 /* Annotate this line. */
2070 bline = &a->lines[lineno - 1];
2071 if (bline->annotated)
2072 return NULL;
2073 err = got_object_id_str(&bline->id_str, id);
2074 if (err)
2075 return err;
2077 err = got_object_open_as_commit(&commit, a->repo, id);
2078 if (err)
2079 goto done;
2081 bline->committer = strdup(got_object_commit_get_committer(commit));
2082 if (bline->committer == NULL) {
2083 err = got_error_from_errno("strdup");
2084 goto done;
2087 committer_time = got_object_commit_get_committer_time(commit);
2088 if (localtime_r(&committer_time, &tm) == NULL)
2089 return got_error_from_errno("localtime_r");
2090 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2091 &tm) >= sizeof(bline->datebuf)) {
2092 err = got_error(GOT_ERR_NO_SPACE);
2093 goto done;
2095 bline->annotated = 1;
2097 /* Print lines annotated so far. */
2098 bline = &a->lines[a->lineno_cur - 1];
2099 if (!bline->annotated)
2100 goto done;
2102 offset = a->line_offsets[a->lineno_cur - 1];
2103 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2104 err = got_error_from_errno("fseeko");
2105 goto done;
2108 while (bline->annotated) {
2109 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2110 *line_escape = NULL;
2111 size_t len;
2113 if (getline(&line, &linesize, a->f) == -1) {
2114 if (ferror(a->f))
2115 err = got_error_from_errno("getline");
2116 break;
2119 committer = bline->committer;
2120 smallerthan = strchr(committer, '<');
2121 if (smallerthan && smallerthan[1] != '\0')
2122 committer = smallerthan + 1;
2123 at = strchr(committer, '@');
2124 if (at)
2125 *at = '\0';
2126 len = strlen(committer);
2127 if (len >= 9)
2128 committer[8] = '\0';
2130 nl = strchr(line, '\n');
2131 if (nl)
2132 *nl = '\0';
2134 if (strcmp(line, "") != 0)
2135 line_escape = strdup(gw_html_escape(line));
2136 else
2137 line_escape = strdup("");
2139 asprintf(&blame_row, blame_line, a->nlines_prec,
2140 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2141 line_escape);
2142 a->lineno_cur++;
2143 err = buf_puts(&newsize, a->blamebuf, blame_row);
2144 if (err)
2145 return err;
2147 bline = &a->lines[a->lineno_cur - 1];
2148 free(line_escape);
2149 free(blame_row);
2151 done:
2152 if (commit)
2153 got_object_commit_close(commit);
2154 free(line);
2155 return err;
2158 static char*
2159 gw_get_file_blame(struct gw_trans *gw_trans)
2161 const struct got_error *error = NULL;
2162 struct got_repository *repo = NULL;
2163 struct got_object_id *obj_id = NULL;
2164 struct got_object_id *commit_id = NULL;
2165 struct got_blob_object *blob = NULL;
2166 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2167 *folder = NULL;
2168 struct gw_blame_cb_args bca;
2169 int i, obj_type;
2170 size_t filesize;
2172 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2173 if (error)
2174 goto done;
2176 if (gw_trans->repo_folder != NULL) {
2177 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2178 error = got_error_from_errno("asprintf");
2179 goto done;
2181 } else
2182 folder = strdup("");
2184 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2185 error = got_error_from_errno("asprintf");
2186 goto done;
2188 free(folder);
2190 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2191 if (error)
2192 goto done;
2194 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2195 GOT_OBJ_TYPE_COMMIT, 1, repo);
2196 if (error)
2197 goto done;
2199 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2200 if (error)
2201 goto done;
2203 if (obj_id == NULL) {
2204 error = got_error(GOT_ERR_NO_OBJ);
2205 goto done;
2208 error = got_object_get_type(&obj_type, repo, obj_id);
2209 if (error)
2210 goto done;
2212 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2213 error = got_error(GOT_ERR_OBJ_TYPE);
2214 goto done;
2217 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2218 if (error)
2219 goto done;
2221 error = buf_alloc(&bca.blamebuf, 0);
2222 if (error)
2223 goto done;
2225 bca.f = got_opentemp();
2226 if (bca.f == NULL) {
2227 error = got_error_from_errno("got_opentemp");
2228 goto done;
2230 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2231 &bca.line_offsets, bca.f, blob);
2232 if (error || bca.nlines == 0)
2233 goto done;
2235 /* Don't include \n at EOF in the blame line count. */
2236 if (bca.line_offsets[bca.nlines - 1] == filesize)
2237 bca.nlines--;
2239 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2240 if (bca.lines == NULL) {
2241 error = got_error_from_errno("calloc");
2242 goto done;
2244 bca.lineno_cur = 1;
2245 bca.nlines_prec = 0;
2246 i = bca.nlines;
2247 while (i > 0) {
2248 i /= 10;
2249 bca.nlines_prec++;
2251 bca.repo = repo;
2252 bca.gw_trans = gw_trans;
2254 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2255 NULL, NULL);
2256 if (buf_len(bca.blamebuf) > 0) {
2257 error = buf_putc(bca.blamebuf, '\0');
2258 blame_html = strdup(buf_get(bca.blamebuf));
2260 done:
2261 free(bca.blamebuf);
2262 free(in_repo_path);
2263 free(commit_id);
2264 free(obj_id);
2265 free(path);
2267 if (blob)
2268 error = got_object_blob_close(blob);
2269 if (repo)
2270 error = got_repo_close(repo);
2271 if (error)
2272 return NULL;
2273 if (bca.lines) {
2274 for (i = 0; i < bca.nlines; i++) {
2275 struct blame_line *bline = &bca.lines[i];
2276 free(bline->id_str);
2277 free(bline->committer);
2279 free(bca.lines);
2281 free(bca.line_offsets);
2282 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2283 error = got_error_from_errno("fclose");
2284 if (error)
2285 return NULL;
2286 else
2287 return blame_html;
2290 static char*
2291 gw_get_repo_tree(struct gw_trans *gw_trans)
2293 const struct got_error *error = NULL;
2294 struct got_repository *repo = NULL;
2295 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2296 struct got_tree_object *tree = NULL;
2297 struct buf *diffbuf = NULL;
2298 size_t newsize;
2299 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2300 *tree_row = NULL, *id_str;
2301 int nentries, i;
2303 error = buf_alloc(&diffbuf, 0);
2304 if (error)
2305 return NULL;
2307 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2308 if (error)
2309 goto done;
2311 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2312 if (error)
2313 goto done;
2315 if (gw_trans->repo_folder != NULL)
2316 path = strdup(gw_trans->repo_folder);
2317 else if (in_repo_path) {
2318 free(path);
2319 path = in_repo_path;
2322 if (gw_trans->commit == NULL) {
2323 struct got_reference *head_ref;
2324 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2325 if (error)
2326 goto done;
2328 error = got_ref_resolve(&commit_id, repo, head_ref);
2329 got_ref_close(head_ref);
2331 } else
2332 error = got_repo_match_object_id(&commit_id, NULL,
2333 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2334 if (error)
2335 goto done;
2337 error = got_object_id_str(&gw_trans->commit, commit_id);
2338 if (error)
2339 goto done;
2341 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2342 if (error)
2343 goto done;
2345 error = got_object_open_as_tree(&tree, repo, tree_id);
2346 if (error)
2347 goto done;
2349 nentries = got_object_tree_get_nentries(tree);
2351 for (i = 0; i < nentries; i++) {
2352 struct got_tree_entry *te;
2353 const char *modestr = "";
2354 char *id = NULL, *url_html = NULL;
2356 te = got_object_tree_get_entry(tree, i);
2358 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2359 if (error)
2360 goto done;
2362 if ((asprintf(&id, "%s", id_str)) == -1) {
2363 error = got_error_from_errno("asprintf");
2364 free(id_str);
2365 goto done;
2368 mode_t mode = got_tree_entry_get_mode(te);
2370 if (got_object_tree_entry_is_submodule(te))
2371 modestr = "$";
2372 else if (S_ISLNK(mode))
2373 modestr = "@";
2374 else if (S_ISDIR(mode))
2375 modestr = "/";
2376 else if (mode & S_IXUSR)
2377 modestr = "*";
2379 char *build_folder = NULL;
2380 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2381 if (gw_trans->repo_folder != NULL) {
2382 if ((asprintf(&build_folder, "%s/%s",
2383 gw_trans->repo_folder,
2384 got_tree_entry_get_name(te))) == -1) {
2385 error =
2386 got_error_from_errno("asprintf");
2387 goto done;
2389 } else {
2390 if (asprintf(&build_folder, "%s",
2391 got_tree_entry_get_name(te)) == -1)
2392 goto done;
2395 if ((asprintf(&url_html, folder_html,
2396 gw_trans->repo_name, gw_trans->action_name,
2397 gw_trans->commit, build_folder,
2398 got_tree_entry_get_name(te), modestr)) == -1) {
2399 error = got_error_from_errno("asprintf");
2400 goto done;
2402 } else {
2403 if (gw_trans->repo_folder != NULL) {
2404 if ((asprintf(&build_folder, "%s",
2405 gw_trans->repo_folder)) == -1) {
2406 error =
2407 got_error_from_errno("asprintf");
2408 goto done;
2410 } else
2411 build_folder = strdup("");
2413 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2414 "blame", gw_trans->commit,
2415 got_tree_entry_get_name(te), build_folder,
2416 got_tree_entry_get_name(te), modestr)) == -1) {
2417 error = got_error_from_errno("asprintf");
2418 goto done;
2421 free(build_folder);
2423 if (error)
2424 goto done;
2426 if ((asprintf(&tree_row, tree_line, url_html)) == -1) {
2427 error = got_error_from_errno("asprintf");
2428 goto done;
2430 error = buf_puts(&newsize, diffbuf, tree_row);
2431 if (error)
2432 goto done;
2434 free(id);
2435 free(id_str);
2436 free(url_html);
2437 free(tree_row);
2440 if (buf_len(diffbuf) > 0) {
2441 error = buf_putc(diffbuf, '\0');
2442 tree_html = strdup(buf_get(diffbuf));
2444 done:
2445 if (tree)
2446 got_object_tree_close(tree);
2447 if (repo)
2448 got_repo_close(repo);
2450 free(in_repo_path);
2451 free(tree_id);
2452 free(diffbuf);
2453 if (error)
2454 return NULL;
2455 else
2456 return tree_html;
2459 static char *
2460 gw_get_repo_heads(struct gw_trans *gw_trans)
2462 const struct got_error *error = NULL;
2463 struct got_repository *repo = NULL;
2464 struct got_reflist_head refs;
2465 struct got_reflist_entry *re;
2466 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2467 struct buf *diffbuf = NULL;
2468 size_t newsize;
2470 error = buf_alloc(&diffbuf, 0);
2471 if (error)
2472 return NULL;
2474 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2475 if (error)
2476 goto done;
2478 SIMPLEQ_INIT(&refs);
2479 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2480 NULL);
2481 if (error)
2482 goto done;
2484 SIMPLEQ_FOREACH(re, &refs, entry) {
2485 char *refname;
2487 refname = strdup(got_ref_get_name(re->ref));
2488 if (refname == NULL) {
2489 error = got_error_from_errno("got_ref_to_str");
2490 goto done;
2493 if (strncmp(refname, "refs/heads/", 11) != 0) {
2494 free(refname);
2495 continue;
2498 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2499 TM_DIFF);
2501 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2502 refname, gw_trans->repo_name, refname,
2503 gw_trans->repo_name, refname, gw_trans->repo_name,
2504 refname)) == -1) {
2505 error = got_error_from_errno("asprintf");
2506 goto done;
2509 if (strncmp(refname, "refs/heads/", 11) == 0)
2510 refname += 11;
2512 if ((asprintf(&head_row, heads_row, age, refname,
2513 head_navs_disp)) == -1) {
2514 error = got_error_from_errno("asprintf");
2515 goto done;
2518 error = buf_puts(&newsize, diffbuf, head_row);
2520 free(head_navs_disp);
2521 free(head_row);
2524 if (buf_len(diffbuf) > 0) {
2525 error = buf_putc(diffbuf, '\0');
2526 heads = strdup(buf_get(diffbuf));
2528 done:
2529 buf_free(diffbuf);
2530 got_ref_list_free(&refs);
2531 if (repo)
2532 got_repo_close(repo);
2533 if (error)
2534 return NULL;
2535 else
2536 return heads;
2539 static char *
2540 gw_get_got_link(struct gw_trans *gw_trans)
2542 char *link;
2544 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2545 gw_trans->gw_conf->got_logo)) == -1)
2546 return NULL;
2548 return link;
2551 static char *
2552 gw_get_site_link(struct gw_trans *gw_trans)
2554 char *link, *repo = "", *action = "";
2556 if (gw_trans->repo_name != NULL)
2557 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2558 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2559 return NULL;
2561 if (gw_trans->action_name != NULL)
2562 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2563 return NULL;
2565 if ((asprintf(&link, site_link, GOTWEB,
2566 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2567 return NULL;
2569 return link;
2572 static char *
2573 gw_colordiff_line(char *buf)
2575 const struct got_error *error = NULL;
2576 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2577 struct buf *diffbuf = NULL;
2578 size_t newsize;
2580 error = buf_alloc(&diffbuf, 0);
2581 if (error)
2582 return NULL;
2584 if (buf == NULL)
2585 return NULL;
2586 if (strncmp(buf, "-", 1) == 0)
2587 color = "diff_minus";
2588 if (strncmp(buf, "+", 1) == 0)
2589 color = "diff_plus";
2590 if (strncmp(buf, "@@", 2) == 0)
2591 color = "diff_chunk_header";
2592 if (strncmp(buf, "@@", 2) == 0)
2593 color = "diff_chunk_header";
2594 if (strncmp(buf, "commit +", 8) == 0)
2595 color = "diff_meta";
2596 if (strncmp(buf, "commit -", 8) == 0)
2597 color = "diff_meta";
2598 if (strncmp(buf, "blob +", 6) == 0)
2599 color = "diff_meta";
2600 if (strncmp(buf, "blob -", 6) == 0)
2601 color = "diff_meta";
2602 if (strncmp(buf, "file +", 6) == 0)
2603 color = "diff_meta";
2604 if (strncmp(buf, "file -", 6) == 0)
2605 color = "diff_meta";
2606 if (strncmp(buf, "from:", 5) == 0)
2607 color = "diff_author";
2608 if (strncmp(buf, "via:", 4) == 0)
2609 color = "diff_author";
2610 if (strncmp(buf, "date:", 5) == 0)
2611 color = "diff_date";
2613 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2614 return NULL;
2616 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2617 if (error)
2618 return NULL;
2620 error = buf_puts(&newsize, diffbuf, buf);
2621 if (error)
2622 return NULL;
2624 if (buf_len(diffbuf) > 0) {
2625 error = buf_putc(diffbuf, '\0');
2626 colorized_line = strdup(buf_get(diffbuf));
2629 free(diffbuf);
2630 free(div_diff_line_div);
2631 return colorized_line;
2634 static char *
2635 gw_html_escape(const char *html)
2637 char *escaped_str = NULL, *buf;
2638 char c[1];
2639 size_t sz, i, buff_sz = 2048;
2641 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2642 return NULL;
2644 if (html == NULL)
2645 return NULL;
2646 else
2647 if ((sz = strlen(html)) == 0)
2648 return NULL;
2650 /* only work with buff_sz */
2651 if (buff_sz < sz)
2652 sz = buff_sz;
2654 for (i = 0; i < sz; i++) {
2655 c[0] = html[i];
2656 switch (c[0]) {
2657 case ('>'):
2658 strcat(buf, "&gt;");
2659 break;
2660 case ('&'):
2661 strcat(buf, "&amp;");
2662 break;
2663 case ('<'):
2664 strcat(buf, "&lt;");
2665 break;
2666 case ('"'):
2667 strcat(buf, "&quot;");
2668 break;
2669 case ('\''):
2670 strcat(buf, "&apos;");
2671 break;
2672 case ('\n'):
2673 strcat(buf, "<br />");
2674 default:
2675 strcat(buf, &c[0]);
2676 break;
2679 asprintf(&escaped_str, "%s", buf);
2680 free(buf);
2681 return escaped_str;
2684 int
2685 main(int argc, char *argv[])
2687 const struct got_error *error = NULL;
2688 struct gw_trans *gw_trans;
2689 struct gw_dir *dir = NULL, *tdir;
2690 const char *page = "index";
2691 int gw_malloc = 1;
2692 enum kcgi_err kerr;
2694 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2695 errx(1, "malloc");
2697 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2698 errx(1, "malloc");
2700 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2701 errx(1, "malloc");
2703 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2704 errx(1, "malloc");
2706 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2707 if (kerr != KCGI_OK) {
2708 error = gw_kcgi_error(kerr);
2709 goto err;
2712 if ((gw_trans->gw_conf =
2713 malloc(sizeof(struct gotweb_conf))) == NULL) {
2714 gw_malloc = 0;
2715 error = got_error_from_errno("malloc");
2716 goto err;
2719 TAILQ_INIT(&gw_trans->gw_dirs);
2720 TAILQ_INIT(&gw_trans->gw_headers);
2722 gw_trans->page = 0;
2723 gw_trans->repos_total = 0;
2724 gw_trans->repo_path = NULL;
2725 gw_trans->commit = NULL;
2726 gw_trans->headref = strdup(GOT_REF_HEAD);
2727 gw_trans->mime = KMIME_TEXT_HTML;
2728 gw_trans->gw_tmpl->key = gw_templs;
2729 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2730 gw_trans->gw_tmpl->arg = gw_trans;
2731 gw_trans->gw_tmpl->cb = gw_template;
2732 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2734 err:
2735 if (error) {
2736 gw_trans->mime = KMIME_TEXT_PLAIN;
2737 gw_trans->action = GW_ERR;
2738 gw_display_index(gw_trans, error);
2739 goto done;
2742 error = gw_parse_querystring(gw_trans);
2743 if (error)
2744 goto err;
2746 error = gw_display_index(gw_trans, error);
2747 if (error)
2748 goto err;
2750 done:
2751 if (gw_malloc) {
2752 free(gw_trans->gw_conf->got_repos_path);
2753 free(gw_trans->gw_conf->got_www_path);
2754 free(gw_trans->gw_conf->got_site_name);
2755 free(gw_trans->gw_conf->got_site_owner);
2756 free(gw_trans->gw_conf->got_site_link);
2757 free(gw_trans->gw_conf->got_logo);
2758 free(gw_trans->gw_conf->got_logo_url);
2759 free(gw_trans->gw_conf);
2760 free(gw_trans->commit);
2761 free(gw_trans->repo_path);
2762 free(gw_trans->repo_name);
2763 free(gw_trans->repo_file);
2764 free(gw_trans->action_name);
2765 free(gw_trans->headref);
2767 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2768 free(dir->name);
2769 free(dir->description);
2770 free(dir->age);
2771 free(dir->url);
2772 free(dir->path);
2773 free(dir);
2778 khttp_free(gw_trans->gw_req);
2779 return EXIT_SUCCESS;