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 *,
173 struct gw_header *, int, int);
174 static char *gw_get_repo_heads(struct gw_trans *);
175 static char *gw_get_clone_url(struct gw_trans *, char *);
176 static char *gw_get_got_link(struct gw_trans *);
177 static char *gw_get_site_link(struct gw_trans *);
178 static char *gw_html_escape(const char *);
179 static char *gw_colordiff_line(char *);
181 static char *gw_gen_commit_header(char *, char*);
182 static char *gw_gen_diff_header(char *, char*);
183 static char *gw_gen_author_header(char *);
184 static char *gw_gen_committer_header(char *);
185 static char *gw_gen_age_header(char *);
186 static char *gw_gen_commit_msg_header(char *);
187 static char *gw_gen_tree_header(char *);
189 static void gw_free_headers(struct gw_header *);
190 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
191 enum kmime);
192 static const struct got_error* gw_display_index(struct gw_trans *,
193 const struct got_error *);
195 static int gw_template(size_t, void *);
197 static const struct got_error* gw_get_header(struct gw_trans *,
198 struct gw_header *, int);
199 static const struct got_error* gw_get_commits(struct gw_trans *,
200 struct gw_header *, int);
201 static const struct got_error* gw_get_commit(struct gw_trans *,
202 struct gw_header *);
203 static const struct got_error* gw_apply_unveil(const char *, const char *);
204 static const struct got_error* gw_blame_cb(void *, int, int,
205 struct got_object_id *);
206 static const struct got_error* gw_load_got_paths(struct gw_trans *);
207 static const struct got_error* gw_load_got_path(struct gw_trans *,
208 struct gw_dir *);
209 static const struct got_error* gw_parse_querystring(struct gw_trans *);
211 static const struct got_error* gw_blame(struct gw_trans *);
212 static const struct got_error* gw_diff(struct gw_trans *);
213 static const struct got_error* gw_index(struct gw_trans *);
214 static const struct got_error* gw_commits(struct gw_trans *);
215 static const struct got_error* gw_briefs(struct gw_trans *);
216 static const struct got_error* gw_summary(struct gw_trans *);
217 static const struct got_error* gw_tree(struct gw_trans *);
218 static const struct got_error* gw_tag(struct gw_trans *);
220 struct gw_query_action {
221 unsigned int func_id;
222 const char *func_name;
223 const struct got_error *(*func_main)(struct gw_trans *);
224 char *template;
225 };
227 enum gw_query_actions {
228 GW_BLAME,
229 GW_BRIEFS,
230 GW_COMMITS,
231 GW_DIFF,
232 GW_ERR,
233 GW_INDEX,
234 GW_SUMMARY,
235 GW_TAG,
236 GW_TREE,
237 };
239 static struct gw_query_action gw_query_funcs[] = {
240 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
241 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
242 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
243 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
244 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
245 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
246 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
247 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
248 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
249 };
251 static const struct got_error *
252 gw_kcgi_error(enum kcgi_err kerr)
254 if (kerr == KCGI_OK)
255 return NULL;
257 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
258 return got_error(GOT_ERR_CANCELLED);
260 if (kerr == KCGI_ENOMEM)
261 return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
263 if (kerr == KCGI_ENFILE)
264 return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
266 if (kerr == KCGI_EAGAIN)
267 return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
269 if (kerr == KCGI_FORM)
270 return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
272 return got_error_from_errno(kcgi_strerror(kerr));
275 static const struct got_error *
276 gw_apply_unveil(const char *repo_path, const char *repo_file)
278 const struct got_error *err;
280 if (repo_path && repo_file) {
281 char *full_path;
282 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
283 return got_error_from_errno("asprintf unveil");
284 if (unveil(full_path, "r") != 0)
285 return got_error_from_errno2("unveil", full_path);
288 if (repo_path && unveil(repo_path, "r") != 0)
289 return got_error_from_errno2("unveil", repo_path);
291 if (unveil("/tmp", "rwc") != 0)
292 return got_error_from_errno2("unveil", "/tmp");
294 err = got_privsep_unveil_exec_helpers();
295 if (err != NULL)
296 return err;
298 if (unveil(NULL, NULL) != 0)
299 return got_error_from_errno("unveil");
301 return NULL;
304 static const struct got_error *
305 gw_blame(struct gw_trans *gw_trans)
307 const struct got_error *error = NULL;
308 struct gw_header *header = NULL;
309 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
310 enum kcgi_err kerr;
312 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
313 NULL) == -1)
314 return got_error_from_errno("pledge");
316 if ((header = gw_init_header()) == NULL)
317 return got_error_from_errno("malloc");
319 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
320 if (error)
321 return error;
323 error = gw_get_header(gw_trans, header, 1);
324 if (error)
325 return error;
327 blame_html = gw_get_file_blame(gw_trans);
329 if (blame_html == NULL) {
330 blame_html = strdup("");
331 if (blame_html == NULL)
332 return got_error_from_errno("strdup");
335 if ((asprintf(&blame_html_disp, blame_header,
336 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
337 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
338 blame_html)) == -1) {
339 error = got_error_from_errno("asprintf");
340 goto done;
343 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
344 error = got_error_from_errno("asprintf");
345 goto done;
348 kerr = khttp_puts(gw_trans->gw_req, blame);
349 if (kerr != KCGI_OK)
350 error = gw_kcgi_error(kerr);
351 done:
352 got_ref_list_free(&header->refs);
353 gw_free_headers(header);
354 free(blame_html_disp);
355 free(blame_html);
356 free(blame);
357 return error;
360 static const struct got_error *
361 gw_diff(struct gw_trans *gw_trans)
363 const struct got_error *error = NULL;
364 struct gw_header *header = NULL;
365 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
366 enum kcgi_err kerr;
368 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
369 NULL) == -1)
370 return got_error_from_errno("pledge");
372 if ((header = gw_init_header()) == NULL)
373 return got_error_from_errno("malloc");
375 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
376 if (error)
377 goto done;
379 error = gw_get_header(gw_trans, header, 1);
380 if (error)
381 goto done;
383 diff_html = gw_get_diff(gw_trans, header);
385 if (diff_html == NULL) {
386 diff_html = strdup("");
387 if (diff_html == NULL) {
388 error = got_error_from_errno("strdup");
389 goto done;
393 if (asprintf(&diff_html_disp, diff_header,
394 gw_gen_diff_header(header->parent_id, header->commit_id),
395 gw_gen_commit_header(header->commit_id, header->refs_str),
396 gw_gen_tree_header(header->tree_id),
397 gw_gen_author_header(header->author),
398 gw_gen_committer_header(header->committer),
399 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
400 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
401 diff_html) == -1) {
402 error = got_error_from_errno("asprintf");
403 goto done;
406 if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
407 error = got_error_from_errno("asprintf");
408 goto done;
411 kerr = khttp_puts(gw_trans->gw_req, diff);
412 if (kerr != KCGI_OK)
413 error = gw_kcgi_error(kerr);
414 done:
415 got_ref_list_free(&header->refs);
416 gw_free_headers(header);
417 free(diff_html_disp);
418 free(diff_html);
419 free(diff);
420 return error;
423 static const struct got_error *
424 gw_index(struct gw_trans *gw_trans)
426 const struct got_error *error = NULL;
427 struct gw_dir *gw_dir = NULL;
428 char *html, *navs, *next, *prev;
429 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
430 enum kcgi_err kerr;
432 if (pledge("stdio rpath proc exec sendfd unveil",
433 NULL) == -1) {
434 error = got_error_from_errno("pledge");
435 return error;
438 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
439 if (error)
440 return error;
442 error = gw_load_got_paths(gw_trans);
443 if (error)
444 return error;
446 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
447 if (kerr != KCGI_OK)
448 return gw_kcgi_error(kerr);
450 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
451 if (asprintf(&html, index_projects_empty,
452 gw_trans->gw_conf->got_repos_path) == -1)
453 return got_error_from_errno("asprintf");
454 kerr = khttp_puts(gw_trans->gw_req, html);
455 if (kerr != KCGI_OK)
456 error = gw_kcgi_error(kerr);
457 free(html);
458 return error;
461 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
462 dir_c++;
464 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
465 if (gw_trans->page > 0 && (gw_trans->page *
466 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
467 prev_disp++;
468 continue;
471 prev_disp++;
473 if (error)
474 return error;
475 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
476 gw_dir->name, gw_dir->name)) == -1)
477 return got_error_from_errno("asprintf");
479 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
480 gw_dir->description, gw_dir->owner, gw_dir->age,
481 navs)) == -1)
482 return got_error_from_errno("asprintf");
484 kerr = khttp_puts(gw_trans->gw_req, html);
485 free(navs);
486 free(html);
487 if (kerr != KCGI_OK)
488 return gw_kcgi_error(kerr);
490 if (gw_trans->gw_conf->got_max_repos_display == 0)
491 continue;
493 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
494 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
495 if (kerr != KCGI_OK)
496 return gw_kcgi_error(kerr);
497 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
498 (gw_trans->page > 0) &&
499 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
500 prev_disp == gw_trans->repos_total)) {
501 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
502 if (kerr != KCGI_OK)
503 return gw_kcgi_error(kerr);
506 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
507 (gw_trans->page > 0) &&
508 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
509 prev_disp == gw_trans->repos_total)) {
510 if ((asprintf(&prev, nav_prev,
511 gw_trans->page - 1)) == -1)
512 return got_error_from_errno("asprintf");
513 kerr = khttp_puts(gw_trans->gw_req, prev);
514 free(prev);
515 if (kerr != KCGI_OK)
516 return gw_kcgi_error(kerr);
519 kerr = khttp_puts(gw_trans->gw_req, div_end);
520 if (kerr != KCGI_OK)
521 return gw_kcgi_error(kerr);
523 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
524 next_disp == gw_trans->gw_conf->got_max_repos_display &&
525 dir_c != (gw_trans->page + 1) *
526 gw_trans->gw_conf->got_max_repos_display) {
527 if ((asprintf(&next, nav_next,
528 gw_trans->page + 1)) == -1)
529 return got_error_from_errno("calloc");
530 kerr = khttp_puts(gw_trans->gw_req, next);
531 free(next);
532 if (kerr != KCGI_OK)
533 return gw_kcgi_error(kerr);
534 kerr = khttp_puts(gw_trans->gw_req, div_end);
535 if (kerr != KCGI_OK)
536 error = gw_kcgi_error(kerr);
537 next_disp = 0;
538 break;
541 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
542 (gw_trans->page > 0) &&
543 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
544 prev_disp == gw_trans->repos_total)) {
545 kerr = khttp_puts(gw_trans->gw_req, div_end);
546 if (kerr != KCGI_OK)
547 return gw_kcgi_error(kerr);
550 next_disp++;
552 return error;
555 static const struct got_error *
556 gw_commits(struct gw_trans *gw_trans)
558 const struct got_error *error = NULL;
559 char *commits_html, *commits_navs_html;
560 struct gw_header *header = NULL, *n_header = NULL;
561 enum kcgi_err kerr;
563 if ((header = gw_init_header()) == NULL)
564 return got_error_from_errno("malloc");
566 if (pledge("stdio rpath proc exec sendfd unveil",
567 NULL) == -1) {
568 error = got_error_from_errno("pledge");
569 return error;
572 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
573 if (error)
574 return error;
576 error = gw_get_header(gw_trans, header,
577 gw_trans->gw_conf->got_max_commits_display);
579 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
580 if (kerr != KCGI_OK)
581 return gw_kcgi_error(kerr);
582 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
583 if ((asprintf(&commits_navs_html, commits_navs,
584 gw_trans->repo_name, n_header->commit_id,
585 gw_trans->repo_name, n_header->commit_id,
586 gw_trans->repo_name, n_header->commit_id)) == -1)
587 return got_error_from_errno("asprintf");
589 if ((asprintf(&commits_html, commits_line,
590 gw_gen_commit_header(n_header->commit_id,
591 n_header->refs_str),
592 gw_gen_author_header(n_header->author),
593 gw_gen_committer_header(n_header->committer),
594 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
595 TM_LONG)), gw_html_escape(n_header->commit_msg),
596 commits_navs_html)) == -1)
597 return got_error_from_errno("asprintf");
598 kerr = khttp_puts(gw_trans->gw_req, commits_html);
599 if (kerr != KCGI_OK)
600 return gw_kcgi_error(kerr);
602 kerr = khttp_puts(gw_trans->gw_req, div_end);
603 if (kerr != KCGI_OK)
604 error = gw_kcgi_error(kerr);
606 got_ref_list_free(&header->refs);
607 gw_free_headers(header);
608 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
609 gw_free_headers(n_header);
610 return error;
613 static const struct got_error *
614 gw_briefs(struct gw_trans *gw_trans)
616 const struct got_error *error = NULL;
617 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
618 struct gw_header *header = NULL, *n_header = NULL;
619 enum kcgi_err kerr;
621 if ((header = gw_init_header()) == NULL)
622 return got_error_from_errno("malloc");
624 if (pledge("stdio rpath proc exec sendfd unveil",
625 NULL) == -1) {
626 error = got_error_from_errno("pledge");
627 return error;
630 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
631 if (error)
632 return error;
634 if (gw_trans->action == GW_SUMMARY)
635 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
636 else
637 error = gw_get_header(gw_trans, header,
638 gw_trans->gw_conf->got_max_commits_display);
640 if (error)
641 return error;
643 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
644 if (kerr != KCGI_OK)
645 return gw_kcgi_error(kerr);
647 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
648 if ((asprintf(&briefs_navs_html, briefs_navs,
649 gw_trans->repo_name, n_header->commit_id,
650 gw_trans->repo_name, n_header->commit_id,
651 gw_trans->repo_name, n_header->commit_id)) == -1)
652 return got_error_from_errno("asprintf");
653 newline = strchr(n_header->commit_msg, '\n');
654 if (newline)
655 *newline = '\0';
656 if ((asprintf(&briefs_html, briefs_line,
657 gw_get_time_str(n_header->committer_time, TM_DIFF),
658 n_header->author, gw_html_escape(n_header->commit_msg),
659 briefs_navs_html)) == -1)
660 return got_error_from_errno("asprintf");
661 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
662 if (kerr != KCGI_OK)
663 return gw_kcgi_error(kerr);
665 kerr = khttp_puts(gw_trans->gw_req, div_end);
666 if (kerr != KCGI_OK)
667 error = gw_kcgi_error(kerr);
669 got_ref_list_free(&header->refs);
670 gw_free_headers(header);
671 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
672 gw_free_headers(n_header);
673 return error;
676 static const struct got_error *
677 gw_summary(struct gw_trans *gw_trans)
679 const struct got_error *error = NULL;
680 char *description_html, *repo_owner_html, *repo_age_html,
681 *cloneurl_html, *tags, *heads, *tags_html,
682 *heads_html, *age;
683 enum kcgi_err kerr;
685 if (pledge("stdio rpath proc exec sendfd unveil",
686 NULL) == -1) {
687 error = got_error_from_errno("pledge");
688 return error;
691 /* unveil is applied with gw_briefs below */
693 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
694 if (kerr != KCGI_OK)
695 return gw_kcgi_error(kerr);
697 if (gw_trans->gw_conf->got_show_repo_description) {
698 if (gw_trans->gw_dir->description != NULL &&
699 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
700 if ((asprintf(&description_html, description,
701 gw_trans->gw_dir->description)) == -1)
702 return got_error_from_errno("asprintf");
704 kerr = khttp_puts(gw_trans->gw_req, description_html);
705 free(description_html);
706 if (kerr != KCGI_OK)
707 return gw_kcgi_error(kerr);
711 if (gw_trans->gw_conf->got_show_repo_owner) {
712 if (gw_trans->gw_dir->owner != NULL &&
713 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
714 if ((asprintf(&repo_owner_html, repo_owner,
715 gw_trans->gw_dir->owner)) == -1)
716 return got_error_from_errno("asprintf");
718 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
719 free(repo_owner_html);
720 if (kerr != KCGI_OK)
721 return gw_kcgi_error(kerr);
725 if (gw_trans->gw_conf->got_show_repo_age) {
726 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
727 "refs/heads", TM_LONG);
728 if (age != NULL && (strcmp(age, "") != 0)) {
729 if ((asprintf(&repo_age_html, last_change, age)) == -1)
730 return got_error_from_errno("asprintf");
732 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
733 free(repo_age_html);
734 free(age);
735 if (kerr != KCGI_OK)
736 return gw_kcgi_error(kerr);
740 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
741 if (gw_trans->gw_dir->url != NULL &&
742 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
743 if ((asprintf(&cloneurl_html, cloneurl,
744 gw_trans->gw_dir->url)) == -1)
745 return got_error_from_errno("asprintf");
747 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
748 free(cloneurl_html);
749 if (kerr != KCGI_OK)
750 return gw_kcgi_error(kerr);
753 kerr = khttp_puts(gw_trans->gw_req, div_end);
754 if (kerr != KCGI_OK)
755 return gw_kcgi_error(kerr);
757 error = gw_briefs(gw_trans);
758 if (error)
759 return error;
761 tags = gw_get_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP, TAGBRIEF);
762 heads = gw_get_repo_heads(gw_trans);
764 if (tags != NULL && strcmp(tags, "") != 0) {
765 if ((asprintf(&tags_html, summary_tags,
766 tags)) == -1)
767 return got_error_from_errno("asprintf");
768 kerr = khttp_puts(gw_trans->gw_req, tags_html);
769 free(tags_html);
770 free(tags);
771 if (kerr != KCGI_OK)
772 return gw_kcgi_error(kerr);
775 if (heads != NULL && strcmp(heads, "") != 0) {
776 if ((asprintf(&heads_html, summary_heads,
777 heads)) == -1)
778 return got_error_from_errno("asprintf");
779 kerr = khttp_puts(gw_trans->gw_req, heads_html);
780 free(heads_html);
781 free(heads);
782 if (kerr != KCGI_OK)
783 return gw_kcgi_error(kerr);
785 return error;
788 static const struct got_error *
789 gw_tree(struct gw_trans *gw_trans)
791 const struct got_error *error = NULL;
792 struct gw_header *header = NULL;
793 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
794 enum kcgi_err kerr;
796 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
797 return got_error_from_errno("pledge");
799 if ((header = gw_init_header()) == NULL)
800 return got_error_from_errno("malloc");
802 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
803 if (error)
804 return error;
806 error = gw_get_header(gw_trans, header, 1);
807 if (error)
808 return error;
810 tree_html = gw_get_repo_tree(gw_trans);
812 if (tree_html == NULL) {
813 tree_html = strdup("");
814 if (tree_html == NULL) {
815 error = got_error_from_errno("strdup");
816 goto done;
820 if ((asprintf(&tree_html_disp, tree_header,
821 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
822 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
823 tree_html)) == -1)
824 return got_error_from_errno("asprintf");
826 if ((asprintf(&tree, tree_wrapper, tree_html_disp)) == -1)
827 return got_error_from_errno("asprintf");
829 kerr = khttp_puts(gw_trans->gw_req, tree);
830 if (kerr != KCGI_OK)
831 error = gw_kcgi_error(kerr);
832 done:
833 got_ref_list_free(&header->refs);
834 gw_free_headers(header);
835 free(tree_html_disp);
836 free(tree_html);
837 free(tree);
838 return error;
841 static const struct got_error *
842 gw_tag(struct gw_trans *gw_trans)
844 const struct got_error *error = NULL;
845 struct gw_header *header = NULL;
846 char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
847 enum kcgi_err kerr;
849 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
850 return got_error_from_errno("pledge");
852 if ((header = gw_init_header()) == NULL)
853 return got_error_from_errno("malloc");
855 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
856 if (error)
857 return error;
859 error = gw_get_header(gw_trans, header, 1);
860 if (error)
861 return error;
863 tag_html = gw_get_repo_tags(gw_trans, header, 1, TAGFULL);
865 if (tag_html == NULL) {
866 tag_html = strdup("");
867 if (tag_html == NULL) {
868 error = got_error_from_errno("strdup");
869 goto done;
873 if ((asprintf(&tag_html_disp, tag_header,
874 gw_gen_commit_header(header->commit_id, header->refs_str),
875 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
876 tag_html)) == -1)
877 return got_error_from_errno("asprintf");
879 if ((asprintf(&tag, tag_wrapper, tag_html_disp)) == -1)
880 return got_error_from_errno("asprintf");
882 kerr = khttp_puts(gw_trans->gw_req, tag);
883 if (kerr != KCGI_OK)
884 error = gw_kcgi_error(kerr);
885 done:
886 got_ref_list_free(&header->refs);
887 gw_free_headers(header);
888 free(tag_html_disp);
889 free(tag_html);
890 free(tag);
891 return error;
894 static const struct got_error *
895 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
897 const struct got_error *error = NULL;
898 DIR *dt;
899 char *dir_test;
900 int opened = 0;
902 if ((asprintf(&dir_test, "%s/%s/%s",
903 gw_trans->gw_conf->got_repos_path, gw_dir->name,
904 GOTWEB_GIT_DIR)) == -1)
905 return got_error_from_errno("asprintf");
907 dt = opendir(dir_test);
908 if (dt == NULL) {
909 free(dir_test);
910 } else {
911 gw_dir->path = strdup(dir_test);
912 opened = 1;
913 goto done;
916 if ((asprintf(&dir_test, "%s/%s/%s",
917 gw_trans->gw_conf->got_repos_path, gw_dir->name,
918 GOTWEB_GOT_DIR)) == -1)
919 return got_error_from_errno("asprintf");
921 dt = opendir(dir_test);
922 if (dt == NULL)
923 free(dir_test);
924 else {
925 opened = 1;
926 error = got_error(GOT_ERR_NOT_GIT_REPO);
927 goto errored;
930 if ((asprintf(&dir_test, "%s/%s",
931 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
932 return got_error_from_errno("asprintf");
934 gw_dir->path = strdup(dir_test);
936 done:
937 gw_dir->description = gw_get_repo_description(gw_trans,
938 gw_dir->path);
939 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
940 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
941 TM_DIFF);
942 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
944 errored:
945 free(dir_test);
946 if (opened)
947 closedir(dt);
948 return error;
951 static const struct got_error *
952 gw_load_got_paths(struct gw_trans *gw_trans)
954 const struct got_error *error = NULL;
955 DIR *d;
956 struct dirent **sd_dent;
957 struct gw_dir *gw_dir;
958 struct stat st;
959 unsigned int d_cnt, d_i;
961 d = opendir(gw_trans->gw_conf->got_repos_path);
962 if (d == NULL) {
963 error = got_error_from_errno2("opendir",
964 gw_trans->gw_conf->got_repos_path);
965 return error;
968 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
969 alphasort);
970 if (d_cnt == -1) {
971 error = got_error_from_errno2("scandir",
972 gw_trans->gw_conf->got_repos_path);
973 return error;
976 for (d_i = 0; d_i < d_cnt; d_i++) {
977 if (gw_trans->gw_conf->got_max_repos > 0 &&
978 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
979 break; /* account for parent and self */
981 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
982 strcmp(sd_dent[d_i]->d_name, "..") == 0)
983 continue;
985 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
986 return got_error_from_errno("gw_dir malloc");
988 error = gw_load_got_path(gw_trans, gw_dir);
989 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
990 continue;
991 else if (error)
992 return error;
994 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
995 !got_path_dir_is_empty(gw_dir->path)) {
996 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
997 entry);
998 gw_trans->repos_total++;
1002 closedir(d);
1003 return error;
1006 static const struct got_error *
1007 gw_parse_querystring(struct gw_trans *gw_trans)
1009 const struct got_error *error = NULL;
1010 struct kpair *p;
1011 struct gw_query_action *action = NULL;
1012 unsigned int i;
1014 if (gw_trans->gw_req->fieldnmap[0]) {
1015 error = got_error_from_errno("bad parse");
1016 return error;
1017 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1018 /* define gw_trans->repo_path */
1019 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
1020 return got_error_from_errno("asprintf");
1022 if ((asprintf(&gw_trans->repo_path, "%s/%s",
1023 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
1024 return got_error_from_errno("asprintf");
1026 /* get action and set function */
1027 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1028 for (i = 0; i < nitems(gw_query_funcs); i++) {
1029 action = &gw_query_funcs[i];
1030 if (action->func_name == NULL)
1031 continue;
1033 if (strcmp(action->func_name,
1034 p->parsed.s) == 0) {
1035 gw_trans->action = i;
1036 if ((asprintf(&gw_trans->action_name,
1037 "%s", action->func_name)) == -1)
1038 return
1039 got_error_from_errno(
1040 "asprintf");
1042 break;
1045 action = NULL;
1048 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1049 if ((asprintf(&gw_trans->commit, "%s",
1050 p->parsed.s)) == -1)
1051 return got_error_from_errno("asprintf");
1053 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1054 if ((asprintf(&gw_trans->repo_file, "%s",
1055 p->parsed.s)) == -1)
1056 return got_error_from_errno("asprintf");
1058 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1059 if ((asprintf(&gw_trans->repo_folder, "%s",
1060 p->parsed.s)) == -1)
1061 return got_error_from_errno("asprintf");
1063 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1064 if ((asprintf(&gw_trans->headref, "%s",
1065 p->parsed.s)) == -1)
1066 return got_error_from_errno("asprintf");
1068 if (action == NULL) {
1069 error = got_error_from_errno("invalid action");
1070 return error;
1072 if ((gw_trans->gw_dir =
1073 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1074 return got_error_from_errno("gw_dir malloc");
1076 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1077 if (error)
1078 return error;
1079 } else
1080 gw_trans->action = GW_INDEX;
1082 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1083 gw_trans->page = p->parsed.i;
1085 /* if (gw_trans->action == GW_RAW) */
1086 /* gw_trans->mime = KMIME_TEXT_PLAIN; */
1088 return error;
1091 static struct gw_dir *
1092 gw_init_gw_dir(char *dir)
1094 struct gw_dir *gw_dir;
1096 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1097 return NULL;
1099 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1100 return NULL;
1102 return gw_dir;
1105 static const struct got_error *
1106 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1108 enum kcgi_err kerr;
1110 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1111 if (kerr != KCGI_OK)
1112 return gw_kcgi_error(kerr);
1113 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1114 khttps[code]);
1115 if (kerr != KCGI_OK)
1116 return gw_kcgi_error(kerr);
1117 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1118 kmimetypes[mime]);
1119 if (kerr != KCGI_OK)
1120 return gw_kcgi_error(kerr);
1121 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1122 if (kerr != KCGI_OK)
1123 return gw_kcgi_error(kerr);
1124 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1125 if (kerr != KCGI_OK)
1126 return gw_kcgi_error(kerr);
1127 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1128 if (kerr != KCGI_OK)
1129 return gw_kcgi_error(kerr);
1131 kerr = khttp_body(gw_trans->gw_req);
1132 return gw_kcgi_error(kerr);
1135 static const struct got_error *
1136 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
1138 enum kcgi_err kerr;
1140 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1141 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1143 if (err)
1144 kerr = khttp_puts(gw_trans->gw_req, err->msg);
1145 else
1146 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1147 gw_query_funcs[gw_trans->action].template);
1148 if (kerr != KCGI_OK)
1149 return gw_kcgi_error(kerr);
1151 kerr = khtml_close(gw_trans->gw_html_req);
1152 return gw_kcgi_error(kerr);
1155 static int
1156 gw_template(size_t key, void *arg)
1158 const struct got_error *error = NULL;
1159 struct gw_trans *gw_trans = arg;
1160 char *gw_got_link, *gw_site_link;
1161 char *site_owner_name, *site_owner_name_h;
1163 switch (key) {
1164 case (TEMPL_HEAD):
1165 khttp_puts(gw_trans->gw_req, head);
1166 break;
1167 case(TEMPL_HEADER):
1168 gw_got_link = gw_get_got_link(gw_trans);
1169 if (gw_got_link != NULL)
1170 khttp_puts(gw_trans->gw_req, gw_got_link);
1172 free(gw_got_link);
1173 break;
1174 case (TEMPL_SITEPATH):
1175 gw_site_link = gw_get_site_link(gw_trans);
1176 if (gw_site_link != NULL)
1177 khttp_puts(gw_trans->gw_req, gw_site_link);
1179 free(gw_site_link);
1180 break;
1181 case(TEMPL_TITLE):
1182 if (gw_trans->gw_conf->got_site_name != NULL)
1183 khtml_puts(gw_trans->gw_html_req,
1184 gw_trans->gw_conf->got_site_name);
1186 break;
1187 case (TEMPL_SEARCH):
1188 khttp_puts(gw_trans->gw_req, search);
1189 break;
1190 case(TEMPL_SITEOWNER):
1191 if (gw_trans->gw_conf->got_site_owner != NULL &&
1192 gw_trans->gw_conf->got_show_site_owner) {
1193 site_owner_name =
1194 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1195 if ((asprintf(&site_owner_name_h, site_owner,
1196 site_owner_name))
1197 == -1)
1198 return 0;
1200 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1201 free(site_owner_name);
1202 free(site_owner_name_h);
1204 break;
1205 case(TEMPL_CONTENT):
1206 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1207 if (error)
1208 khttp_puts(gw_trans->gw_req, error->msg);
1209 break;
1210 default:
1211 return 0;
1213 return 1;
1216 static char *
1217 gw_gen_commit_header(char *str1, char *str2)
1219 char *return_html = NULL, *ref_str = NULL;
1221 if (strcmp(str2, "") != 0) {
1222 if ((asprintf(&ref_str, "(%s)", str2)) == -1) {
1223 return_html = strdup("");
1224 return return_html;
1226 } else
1227 ref_str = strdup("");
1230 if ((asprintf(&return_html, header_commit_html, str1, ref_str)) == -1)
1231 return_html = strdup("");
1233 free(ref_str);
1234 return return_html;
1237 static char *
1238 gw_gen_diff_header(char *str1, char *str2)
1240 char *return_html = NULL;
1242 if ((asprintf(&return_html, header_diff_html, str1, str2)) == -1)
1243 return_html = strdup("");
1245 return return_html;
1248 static char *
1249 gw_gen_author_header(char *str)
1251 char *return_html = NULL;
1253 if ((asprintf(&return_html, header_author_html, str)) == -1)
1254 return_html = strdup("");
1256 return return_html;
1259 static char *
1260 gw_gen_committer_header(char *str)
1262 char *return_html = NULL;
1264 if ((asprintf(&return_html, header_committer_html, str)) == -1)
1265 return_html = strdup("");
1267 return return_html;
1270 static char *
1271 gw_gen_age_header(char *str)
1273 char *return_html = NULL;
1275 if ((asprintf(&return_html, header_age_html, str)) == -1)
1276 return_html = strdup("");
1278 return return_html;
1281 static char *
1282 gw_gen_commit_msg_header(char *str)
1284 char *return_html = NULL;
1286 if ((asprintf(&return_html, header_commit_msg_html, str)) == -1)
1287 return_html = strdup("");
1289 return return_html;
1292 static char *
1293 gw_gen_tree_header(char *str)
1295 char *return_html = NULL;
1297 if ((asprintf(&return_html, header_tree_html, str)) == -1)
1298 return_html = strdup("");
1300 return return_html;
1303 static char *
1304 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1306 FILE *f;
1307 char *description = NULL, *d_file = NULL;
1308 unsigned int len;
1310 if (gw_trans->gw_conf->got_show_repo_description == false)
1311 goto err;
1313 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1314 goto err;
1316 if ((f = fopen(d_file, "r")) == NULL)
1317 goto err;
1319 fseek(f, 0, SEEK_END);
1320 len = ftell(f) + 1;
1321 fseek(f, 0, SEEK_SET);
1322 if ((description = calloc(len, sizeof(char *))) == NULL)
1323 goto err;
1325 fread(description, 1, len, f);
1326 fclose(f);
1327 free(d_file);
1328 return description;
1329 err:
1330 if ((asprintf(&description, "%s", "")) == -1)
1331 return NULL;
1333 return description;
1336 static char *
1337 gw_get_time_str(time_t committer_time, int ref_tm)
1339 struct tm tm;
1340 time_t diff_time;
1341 char *years = "years ago", *months = "months ago";
1342 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1343 char *minutes = "minutes ago", *seconds = "seconds ago";
1344 char *now = "right now";
1345 char *repo_age, *s;
1346 char datebuf[29];
1348 switch (ref_tm) {
1349 case TM_DIFF:
1350 diff_time = time(NULL) - committer_time;
1351 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1352 if ((asprintf(&repo_age, "%lld %s",
1353 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1354 return NULL;
1355 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1356 if ((asprintf(&repo_age, "%lld %s",
1357 (diff_time / 60 / 60 / 24 / (365 / 12)),
1358 months)) == -1)
1359 return NULL;
1360 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1361 if ((asprintf(&repo_age, "%lld %s",
1362 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1363 return NULL;
1364 } else if (diff_time > 60 * 60 * 24 * 2) {
1365 if ((asprintf(&repo_age, "%lld %s",
1366 (diff_time / 60 / 60 / 24), days)) == -1)
1367 return NULL;
1368 } else if (diff_time > 60 * 60 * 2) {
1369 if ((asprintf(&repo_age, "%lld %s",
1370 (diff_time / 60 / 60), hours)) == -1)
1371 return NULL;
1372 } else if (diff_time > 60 * 2) {
1373 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1374 minutes)) == -1)
1375 return NULL;
1376 } else if (diff_time > 2) {
1377 if ((asprintf(&repo_age, "%lld %s", diff_time,
1378 seconds)) == -1)
1379 return NULL;
1380 } else {
1381 if ((asprintf(&repo_age, "%s", now)) == -1)
1382 return NULL;
1384 break;
1385 case TM_LONG:
1386 if (gmtime_r(&committer_time, &tm) == NULL)
1387 return NULL;
1389 s = asctime_r(&tm, datebuf);
1390 if (s == NULL)
1391 return NULL;
1393 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1394 return NULL;
1395 break;
1397 return repo_age;
1400 static char *
1401 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1402 int ref_tm)
1404 const struct got_error *error = NULL;
1405 struct got_object_id *id = NULL;
1406 struct got_repository *repo = NULL;
1407 struct got_commit_object *commit = NULL;
1408 struct got_reflist_head refs;
1409 struct got_reflist_entry *re;
1410 struct got_reference *head_ref;
1411 int is_head = 0;
1412 time_t committer_time = 0, cmp_time = 0;
1413 const char *refname;
1414 char *repo_age = NULL;
1416 SIMPLEQ_INIT(&refs);
1418 if (repo_ref == NULL)
1419 return NULL;
1421 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1422 is_head = 1;
1424 if (gw_trans->gw_conf->got_show_repo_age == false) {
1425 if ((asprintf(&repo_age, "")) == -1)
1426 return NULL;
1427 return repo_age;
1430 error = got_repo_open(&repo, dir, NULL);
1431 if (error)
1432 goto err;
1434 if (is_head)
1435 error = got_ref_list(&refs, repo, "refs/heads",
1436 got_ref_cmp_by_name, NULL);
1437 else
1438 error = got_ref_list(&refs, repo, repo_ref,
1439 got_ref_cmp_by_name, NULL);
1440 if (error)
1441 goto err;
1443 SIMPLEQ_FOREACH(re, &refs, entry) {
1444 if (is_head)
1445 refname = strdup(repo_ref);
1446 else
1447 refname = got_ref_get_name(re->ref);
1448 error = got_ref_open(&head_ref, repo, refname, 0);
1449 if (error)
1450 goto err;
1452 error = got_ref_resolve(&id, repo, head_ref);
1453 got_ref_close(head_ref);
1454 if (error)
1455 goto err;
1457 error = got_object_open_as_commit(&commit, repo, id);
1458 if (error)
1459 goto err;
1461 committer_time =
1462 got_object_commit_get_committer_time(commit);
1464 if (cmp_time < committer_time)
1465 cmp_time = committer_time;
1468 if (cmp_time != 0) {
1469 committer_time = cmp_time;
1470 repo_age = gw_get_time_str(committer_time, ref_tm);
1471 } else
1472 if ((asprintf(&repo_age, "")) == -1)
1473 return NULL;
1474 got_ref_list_free(&refs);
1475 free(id);
1476 return repo_age;
1477 err:
1478 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1479 return NULL;
1481 return repo_age;
1484 static char *
1485 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1487 const struct got_error *error;
1488 FILE *f = NULL;
1489 struct got_object_id *id1 = NULL, *id2 = NULL;
1490 struct buf *diffbuf = NULL;
1491 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1492 *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1493 int obj_type;
1494 size_t newsize;
1496 f = got_opentemp();
1497 if (f == NULL)
1498 return NULL;
1500 error = buf_alloc(&diffbuf, 0);
1501 if (error)
1502 return NULL;
1504 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1505 if (error)
1506 goto done;
1508 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1509 error = got_repo_match_object_id(&id1, &label1,
1510 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1511 if (error)
1512 goto done;
1515 error = got_repo_match_object_id(&id2, &label2,
1516 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1517 if (error)
1518 goto done;
1520 error = got_object_get_type(&obj_type, header->repo, id2);
1521 if (error)
1522 goto done;
1523 switch (obj_type) {
1524 case GOT_OBJ_TYPE_BLOB:
1525 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1526 header->repo, f);
1527 break;
1528 case GOT_OBJ_TYPE_TREE:
1529 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1530 header->repo, f);
1531 break;
1532 case GOT_OBJ_TYPE_COMMIT:
1533 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1534 header->repo, f);
1535 break;
1536 default:
1537 error = got_error(GOT_ERR_OBJ_TYPE);
1540 if ((buf = calloc(128, sizeof(char *))) == NULL)
1541 goto done;
1543 fseek(f, 0, SEEK_SET);
1545 while ((fgets(buf, 2048, f)) != NULL) {
1546 n_buf = buf;
1547 while (*n_buf == '\n')
1548 n_buf++;
1549 newline = strchr(n_buf, '\n');
1550 if (newline)
1551 *newline = ' ';
1553 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1554 if (buf_color == NULL)
1555 continue;
1557 error = buf_puts(&newsize, diffbuf, buf_color);
1558 if (error)
1559 return NULL;
1561 error = buf_puts(&newsize, diffbuf, div_end);
1562 if (error)
1563 return NULL;
1566 if (buf_len(diffbuf) > 0) {
1567 error = buf_putc(diffbuf, '\0');
1568 diff_html = strdup(buf_get(diffbuf));
1570 done:
1571 fclose(f);
1572 free(buf_color);
1573 free(buf);
1574 free(diffbuf);
1575 free(label1);
1576 free(label2);
1577 free(id1);
1578 free(id2);
1580 if (error)
1581 return NULL;
1582 else
1583 return diff_html;
1586 static char *
1587 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1589 FILE *f;
1590 char *owner = NULL, *d_file = NULL;
1591 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1592 char *comp, *pos, *buf;
1593 unsigned int i;
1595 if (gw_trans->gw_conf->got_show_repo_owner == false)
1596 goto err;
1598 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1599 goto err;
1601 if ((f = fopen(d_file, "r")) == NULL)
1602 goto err;
1604 if ((buf = calloc(128, sizeof(char *))) == NULL)
1605 goto err;
1607 while ((fgets(buf, 128, f)) != NULL) {
1608 if ((pos = strstr(buf, gotweb)) != NULL)
1609 break;
1611 if ((pos = strstr(buf, gitweb)) != NULL)
1612 break;
1615 if (pos == NULL)
1616 goto err;
1618 do {
1619 fgets(buf, 128, f);
1620 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1622 if (comp == NULL)
1623 goto err;
1625 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1626 goto err;
1628 for (i = 0; i < 2; i++) {
1629 owner = strsep(&buf, "\"");
1632 if (owner == NULL)
1633 goto err;
1635 fclose(f);
1636 free(d_file);
1637 return owner;
1638 err:
1639 if ((asprintf(&owner, "%s", "")) == -1)
1640 return NULL;
1642 return owner;
1645 static char *
1646 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1648 FILE *f;
1649 char *url = NULL, *d_file = NULL;
1650 unsigned int len;
1652 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1653 return NULL;
1655 if ((f = fopen(d_file, "r")) == NULL)
1656 return NULL;
1658 fseek(f, 0, SEEK_END);
1659 len = ftell(f) + 1;
1660 fseek(f, 0, SEEK_SET);
1662 if ((url = calloc(len, sizeof(char *))) == NULL)
1663 return NULL;
1665 fread(url, 1, len, f);
1666 fclose(f);
1667 free(d_file);
1668 return url;
1671 static char *
1672 gw_get_repo_tags(struct gw_trans *gw_trans, struct gw_header *header, int limit,
1673 int tag_type)
1675 const struct got_error *error = NULL;
1676 struct got_repository *repo = NULL;
1677 struct got_reflist_head refs;
1678 struct got_reflist_entry *re;
1679 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1680 *age = NULL;
1681 char *newline;
1682 struct buf *diffbuf = NULL;
1683 size_t newsize;
1685 SIMPLEQ_INIT(&refs);
1687 error = buf_alloc(&diffbuf, 0);
1688 if (error)
1689 return NULL;
1691 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1692 if (error)
1693 goto done;
1695 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1696 if (error)
1697 goto done;
1699 SIMPLEQ_FOREACH(re, &refs, entry) {
1700 const char *refname;
1701 char *refstr, *tag_commit0, *tag_commit, *id_str;
1702 const char *tagger;
1703 time_t tagger_time;
1704 struct got_object_id *id;
1705 struct got_tag_object *tag;
1707 refname = got_ref_get_name(re->ref);
1708 if (strncmp(refname, "refs/tags/", 10) != 0)
1709 continue;
1710 refname += 10;
1711 refstr = got_ref_to_str(re->ref);
1712 if (refstr == NULL) {
1713 error = got_error_from_errno("got_ref_to_str");
1714 goto done;
1717 error = got_ref_resolve(&id, repo, re->ref);
1718 if (error)
1719 goto done;
1720 error = got_object_open_as_tag(&tag, repo, id);
1721 free(id);
1722 if (error)
1723 goto done;
1725 tagger = got_object_tag_get_tagger(tag);
1726 tagger_time = got_object_tag_get_tagger_time(tag);
1728 error = got_object_id_str(&id_str,
1729 got_object_tag_get_object_id(tag));
1730 if (error)
1731 goto done;
1733 tag_commit0 = strdup(got_object_tag_get_message(tag));
1735 if (tag_commit0 == NULL) {
1736 error = got_error_from_errno("strdup");
1737 goto done;
1740 tag_commit = tag_commit0;
1741 while (*tag_commit == '\n')
1742 tag_commit++;
1744 switch (tag_type) {
1745 case TAGBRIEF:
1746 newline = strchr(tag_commit, '\n');
1747 if (newline)
1748 *newline = '\0';
1750 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1751 TM_DIFF))) == -1) {
1752 error = got_error_from_errno("asprintf");
1753 goto done;
1756 if ((asprintf(&tags_navs_disp, tags_navs,
1757 gw_trans->repo_name, id_str, gw_trans->repo_name,
1758 id_str, gw_trans->repo_name, id_str,
1759 gw_trans->repo_name, id_str)) == -1) {
1760 error = got_error_from_errno("asprintf");
1761 goto done;
1764 if ((asprintf(&tag_row, tags_row, age, refname,
1765 tag_commit, tags_navs_disp)) == -1) {
1766 error = got_error_from_errno("asprintf");
1767 goto done;
1770 free(tags_navs_disp);
1771 break;
1772 case TAGFULL:
1773 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1774 TM_LONG))) == -1) {
1775 error = got_error_from_errno("asprintf");
1776 goto done;
1778 if ((asprintf(&tag_row, tag_info, age,
1779 gw_html_escape(tagger),
1780 gw_html_escape(tag_commit))) == -1) {
1781 error = got_error_from_errno("asprintf");
1782 goto done;
1784 break;
1785 default:
1786 break;
1789 got_object_tag_close(tag);
1791 error = buf_puts(&newsize, diffbuf, tag_row);
1793 free(id_str);
1794 free(refstr);
1795 free(age);
1796 free(tag_commit0);
1797 free(tag_row);
1799 if (error || (limit && --limit == 0))
1800 break;
1803 if (buf_len(diffbuf) > 0) {
1804 error = buf_putc(diffbuf, '\0');
1805 tags = strdup(buf_get(diffbuf));
1807 done:
1808 buf_free(diffbuf);
1809 got_ref_list_free(&refs);
1810 if (repo)
1811 got_repo_close(repo);
1812 if (error)
1813 return NULL;
1814 else
1815 return tags;
1818 static void
1819 gw_free_headers(struct gw_header *header)
1821 free(header->id);
1822 free(header->path);
1823 if (header->commit != NULL)
1824 got_object_commit_close(header->commit);
1825 if (header->repo)
1826 got_repo_close(header->repo);
1827 free(header->refs_str);
1828 free(header->commit_id);
1829 free(header->parent_id);
1830 free(header->tree_id);
1831 free(header->author);
1832 free(header->committer);
1833 free(header->commit_msg);
1836 static struct gw_header *
1837 gw_init_header()
1839 struct gw_header *header;
1841 header = malloc(sizeof(*header));
1842 if (header == NULL)
1843 return NULL;
1845 header->repo = NULL;
1846 header->commit = NULL;
1847 header->id = NULL;
1848 header->path = NULL;
1849 SIMPLEQ_INIT(&header->refs);
1851 return header;
1854 static const struct got_error *
1855 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1856 int limit)
1858 const struct got_error *error = NULL;
1859 struct got_commit_graph *graph = NULL;
1861 error = got_commit_graph_open(&graph, header->path, 0);
1862 if (error)
1863 goto done;
1865 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1866 NULL, NULL);
1867 if (error)
1868 goto done;
1870 for (;;) {
1871 error = got_commit_graph_iter_next(&header->id, graph,
1872 header->repo, NULL, NULL);
1873 if (error) {
1874 if (error->code == GOT_ERR_ITER_COMPLETED)
1875 error = NULL;
1876 goto done;
1878 if (header->id == NULL)
1879 goto done;
1881 error = got_object_open_as_commit(&header->commit, header->repo,
1882 header->id);
1883 if (error)
1884 goto done;
1886 error = gw_get_commit(gw_trans, header);
1887 if (limit > 1) {
1888 struct gw_header *n_header = NULL;
1889 if ((n_header = gw_init_header()) == NULL)
1890 error = got_error_from_errno("malloc");
1892 n_header->refs_str = strdup(header->refs_str);
1893 n_header->commit_id = strdup(header->commit_id);
1894 n_header->parent_id = strdup(header->parent_id);
1895 n_header->tree_id = strdup(header->tree_id);
1896 n_header->author = strdup(header->author);
1897 n_header->committer = strdup(header->committer);
1898 n_header->commit_msg = strdup(header->commit_msg);
1899 n_header->committer_time = header->committer_time;
1900 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
1901 entry);
1903 if (error || (limit && --limit == 0))
1904 break;
1906 done:
1907 if (graph)
1908 got_commit_graph_close(graph);
1909 return error;
1912 static const struct got_error *
1913 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
1915 const struct got_error *error = NULL;
1916 struct got_reflist_entry *re;
1917 struct got_object_id *id2 = NULL;
1918 struct got_object_qid *parent_id;
1919 char *refs_str = NULL,
1920 *commit_msg = NULL, *commit_msg0;
1922 /*print commit*/
1923 SIMPLEQ_FOREACH(re, &header->refs, entry) {
1924 char *s;
1925 const char *name;
1926 struct got_tag_object *tag = NULL;
1927 int cmp;
1929 name = got_ref_get_name(re->ref);
1930 if (strcmp(name, GOT_REF_HEAD) == 0)
1931 continue;
1932 if (strncmp(name, "refs/", 5) == 0)
1933 name += 5;
1934 if (strncmp(name, "got/", 4) == 0)
1935 continue;
1936 if (strncmp(name, "heads/", 6) == 0)
1937 name += 6;
1938 if (strncmp(name, "remotes/", 8) == 0)
1939 name += 8;
1940 if (strncmp(name, "tags/", 5) == 0) {
1941 error = got_object_open_as_tag(&tag, header->repo,
1942 re->id);
1943 if (error) {
1944 if (error->code != GOT_ERR_OBJ_TYPE)
1945 continue;
1947 * Ref points at something other
1948 * than a tag.
1950 error = NULL;
1951 tag = NULL;
1954 cmp = got_object_id_cmp(tag ?
1955 got_object_tag_get_object_id(tag) : re->id, header->id);
1956 if (tag)
1957 got_object_tag_close(tag);
1958 if (cmp != 0)
1959 continue;
1960 s = refs_str;
1961 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1962 s ? ", " : "", name)) == -1) {
1963 error = got_error_from_errno("asprintf");
1964 free(s);
1965 return error;
1967 header->refs_str = strdup(refs_str);
1968 free(s);
1971 if (refs_str == NULL)
1972 header->refs_str = strdup("");
1973 free(refs_str);
1975 error = got_object_id_str(&header->commit_id, header->id);
1976 if (error)
1977 return error;
1979 error = got_object_id_str(&header->tree_id,
1980 got_object_commit_get_tree_id(header->commit));
1981 if (error)
1982 return error;
1984 if (gw_trans->action == GW_DIFF) {
1985 parent_id = SIMPLEQ_FIRST(
1986 got_object_commit_get_parent_ids(header->commit));
1987 if (parent_id != NULL) {
1988 id2 = got_object_id_dup(parent_id->id);
1989 free (parent_id);
1990 error = got_object_id_str(&header->parent_id, id2);
1991 if (error)
1992 return error;
1993 free(id2);
1994 } else
1995 header->parent_id = strdup("/dev/null");
1996 } else
1997 header->parent_id = strdup("");
1999 header->committer_time =
2000 got_object_commit_get_committer_time(header->commit);
2002 if (gw_trans->action != GW_BRIEFS && gw_trans->action != GW_SUMMARY) {
2003 header->author = strdup(
2004 gw_html_escape(got_object_commit_get_author(header->commit))
2006 } else {
2007 header->author = strdup(
2008 got_object_commit_get_author(header->commit)
2012 header->committer = strdup(
2013 gw_html_escape(got_object_commit_get_committer(header->commit))
2016 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2017 if (error)
2018 return error;
2020 commit_msg = commit_msg0;
2021 while (*commit_msg == '\n')
2022 commit_msg++;
2024 header->commit_msg = strdup(commit_msg);
2025 free(commit_msg0);
2026 return error;
2029 static const struct got_error *
2030 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2032 const struct got_error *error = NULL;
2033 char *in_repo_path = NULL;
2035 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2036 if (error)
2037 return error;
2039 if (gw_trans->commit == NULL) {
2040 struct got_reference *head_ref;
2041 error = got_ref_open(&head_ref, header->repo,
2042 gw_trans->headref, 0);
2043 if (error)
2044 return error;
2046 error = got_ref_resolve(&header->id, header->repo, head_ref);
2047 got_ref_close(head_ref);
2048 if (error)
2049 return error;
2051 error = got_object_open_as_commit(&header->commit,
2052 header->repo, header->id);
2053 } else {
2054 struct got_reference *ref;
2055 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2056 if (error == NULL) {
2057 int obj_type;
2058 error = got_ref_resolve(&header->id, header->repo, ref);
2059 got_ref_close(ref);
2060 if (error)
2061 return error;
2062 error = got_object_get_type(&obj_type, header->repo,
2063 header->id);
2064 if (error)
2065 return error;
2066 if (obj_type == GOT_OBJ_TYPE_TAG) {
2067 struct got_tag_object *tag;
2068 error = got_object_open_as_tag(&tag,
2069 header->repo, header->id);
2070 if (error)
2071 return error;
2072 if (got_object_tag_get_object_type(tag) !=
2073 GOT_OBJ_TYPE_COMMIT) {
2074 got_object_tag_close(tag);
2075 error = got_error(GOT_ERR_OBJ_TYPE);
2076 return error;
2078 free(header->id);
2079 header->id = got_object_id_dup(
2080 got_object_tag_get_object_id(tag));
2081 if (header->id == NULL)
2082 error = got_error_from_errno(
2083 "got_object_id_dup");
2084 got_object_tag_close(tag);
2085 if (error)
2086 return error;
2087 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2088 error = got_error(GOT_ERR_OBJ_TYPE);
2089 return error;
2091 error = got_object_open_as_commit(&header->commit,
2092 header->repo, header->id);
2093 if (error)
2094 return error;
2096 if (header->commit == NULL) {
2097 error = got_repo_match_object_id_prefix(&header->id,
2098 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2099 header->repo);
2100 if (error)
2101 return error;
2103 error = got_repo_match_object_id_prefix(&header->id,
2104 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2105 header->repo);
2108 error = got_repo_map_path(&in_repo_path, header->repo,
2109 gw_trans->repo_path, 1);
2110 if (error)
2111 return error;
2113 if (in_repo_path) {
2114 header->path = strdup(in_repo_path);
2116 free(in_repo_path);
2118 error = got_ref_list(&header->refs, header->repo, NULL,
2119 got_ref_cmp_by_name, NULL);
2120 if (error)
2121 return error;
2123 error = gw_get_commits(gw_trans, header, limit);
2124 return error;
2127 struct blame_line {
2128 int annotated;
2129 char *id_str;
2130 char *committer;
2131 char datebuf[11]; /* YYYY-MM-DD + NUL */
2134 struct gw_blame_cb_args {
2135 struct blame_line *lines;
2136 int nlines;
2137 int nlines_prec;
2138 int lineno_cur;
2139 off_t *line_offsets;
2140 FILE *f;
2141 struct got_repository *repo;
2142 struct gw_trans *gw_trans;
2143 struct buf *blamebuf;
2146 static const struct got_error *
2147 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2149 const struct got_error *err = NULL;
2150 struct gw_blame_cb_args *a = arg;
2151 struct blame_line *bline;
2152 char *line = NULL;
2153 size_t linesize = 0, newsize;
2154 struct got_commit_object *commit = NULL;
2155 off_t offset;
2156 struct tm tm;
2157 time_t committer_time;
2159 if (nlines != a->nlines ||
2160 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2161 return got_error(GOT_ERR_RANGE);
2163 if (lineno == -1)
2164 return NULL; /* no change in this commit */
2166 /* Annotate this line. */
2167 bline = &a->lines[lineno - 1];
2168 if (bline->annotated)
2169 return NULL;
2170 err = got_object_id_str(&bline->id_str, id);
2171 if (err)
2172 return err;
2174 err = got_object_open_as_commit(&commit, a->repo, id);
2175 if (err)
2176 goto done;
2178 bline->committer = strdup(got_object_commit_get_committer(commit));
2179 if (bline->committer == NULL) {
2180 err = got_error_from_errno("strdup");
2181 goto done;
2184 committer_time = got_object_commit_get_committer_time(commit);
2185 if (localtime_r(&committer_time, &tm) == NULL)
2186 return got_error_from_errno("localtime_r");
2187 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2188 &tm) >= sizeof(bline->datebuf)) {
2189 err = got_error(GOT_ERR_NO_SPACE);
2190 goto done;
2192 bline->annotated = 1;
2194 /* Print lines annotated so far. */
2195 bline = &a->lines[a->lineno_cur - 1];
2196 if (!bline->annotated)
2197 goto done;
2199 offset = a->line_offsets[a->lineno_cur - 1];
2200 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2201 err = got_error_from_errno("fseeko");
2202 goto done;
2205 while (bline->annotated) {
2206 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2207 *line_escape = NULL;
2208 size_t len;
2210 if (getline(&line, &linesize, a->f) == -1) {
2211 if (ferror(a->f))
2212 err = got_error_from_errno("getline");
2213 break;
2216 committer = bline->committer;
2217 smallerthan = strchr(committer, '<');
2218 if (smallerthan && smallerthan[1] != '\0')
2219 committer = smallerthan + 1;
2220 at = strchr(committer, '@');
2221 if (at)
2222 *at = '\0';
2223 len = strlen(committer);
2224 if (len >= 9)
2225 committer[8] = '\0';
2227 nl = strchr(line, '\n');
2228 if (nl)
2229 *nl = '\0';
2231 if (strcmp(line, "") != 0)
2232 line_escape = strdup(gw_html_escape(line));
2233 else
2234 line_escape = strdup("");
2236 asprintf(&blame_row, blame_line, a->nlines_prec,
2237 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2238 line_escape);
2239 a->lineno_cur++;
2240 err = buf_puts(&newsize, a->blamebuf, blame_row);
2241 if (err)
2242 return err;
2244 bline = &a->lines[a->lineno_cur - 1];
2245 free(line_escape);
2246 free(blame_row);
2248 done:
2249 if (commit)
2250 got_object_commit_close(commit);
2251 free(line);
2252 return err;
2255 static char*
2256 gw_get_file_blame(struct gw_trans *gw_trans)
2258 const struct got_error *error = NULL;
2259 struct got_repository *repo = NULL;
2260 struct got_object_id *obj_id = NULL;
2261 struct got_object_id *commit_id = NULL;
2262 struct got_blob_object *blob = NULL;
2263 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2264 *folder = NULL;
2265 struct gw_blame_cb_args bca;
2266 int i, obj_type;
2267 size_t filesize;
2269 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2270 if (error)
2271 goto done;
2273 if (gw_trans->repo_folder != NULL) {
2274 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2275 error = got_error_from_errno("asprintf");
2276 goto done;
2278 } else
2279 folder = strdup("");
2281 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2282 error = got_error_from_errno("asprintf");
2283 goto done;
2285 free(folder);
2287 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2288 if (error)
2289 goto done;
2291 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2292 GOT_OBJ_TYPE_COMMIT, 1, repo);
2293 if (error)
2294 goto done;
2296 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2297 if (error)
2298 goto done;
2300 if (obj_id == NULL) {
2301 error = got_error(GOT_ERR_NO_OBJ);
2302 goto done;
2305 error = got_object_get_type(&obj_type, repo, obj_id);
2306 if (error)
2307 goto done;
2309 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2310 error = got_error(GOT_ERR_OBJ_TYPE);
2311 goto done;
2314 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2315 if (error)
2316 goto done;
2318 error = buf_alloc(&bca.blamebuf, 0);
2319 if (error)
2320 goto done;
2322 bca.f = got_opentemp();
2323 if (bca.f == NULL) {
2324 error = got_error_from_errno("got_opentemp");
2325 goto done;
2327 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2328 &bca.line_offsets, bca.f, blob);
2329 if (error || bca.nlines == 0)
2330 goto done;
2332 /* Don't include \n at EOF in the blame line count. */
2333 if (bca.line_offsets[bca.nlines - 1] == filesize)
2334 bca.nlines--;
2336 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2337 if (bca.lines == NULL) {
2338 error = got_error_from_errno("calloc");
2339 goto done;
2341 bca.lineno_cur = 1;
2342 bca.nlines_prec = 0;
2343 i = bca.nlines;
2344 while (i > 0) {
2345 i /= 10;
2346 bca.nlines_prec++;
2348 bca.repo = repo;
2349 bca.gw_trans = gw_trans;
2351 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2352 NULL, NULL);
2353 if (buf_len(bca.blamebuf) > 0) {
2354 error = buf_putc(bca.blamebuf, '\0');
2355 blame_html = strdup(buf_get(bca.blamebuf));
2357 done:
2358 free(bca.blamebuf);
2359 free(in_repo_path);
2360 free(commit_id);
2361 free(obj_id);
2362 free(path);
2364 if (blob)
2365 error = got_object_blob_close(blob);
2366 if (repo)
2367 error = got_repo_close(repo);
2368 if (error)
2369 return NULL;
2370 if (bca.lines) {
2371 for (i = 0; i < bca.nlines; i++) {
2372 struct blame_line *bline = &bca.lines[i];
2373 free(bline->id_str);
2374 free(bline->committer);
2376 free(bca.lines);
2378 free(bca.line_offsets);
2379 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2380 error = got_error_from_errno("fclose");
2381 if (error)
2382 return NULL;
2383 else
2384 return blame_html;
2387 static char*
2388 gw_get_repo_tree(struct gw_trans *gw_trans)
2390 const struct got_error *error = NULL;
2391 struct got_repository *repo = NULL;
2392 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2393 struct got_tree_object *tree = NULL;
2394 struct buf *diffbuf = NULL;
2395 size_t newsize;
2396 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2397 *tree_row = NULL, *id_str;
2398 int nentries, i;
2400 error = buf_alloc(&diffbuf, 0);
2401 if (error)
2402 return NULL;
2404 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2405 if (error)
2406 goto done;
2408 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2409 if (error)
2410 goto done;
2412 if (gw_trans->repo_folder != NULL)
2413 path = strdup(gw_trans->repo_folder);
2414 else if (in_repo_path) {
2415 free(path);
2416 path = in_repo_path;
2419 if (gw_trans->commit == NULL) {
2420 struct got_reference *head_ref;
2421 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2422 if (error)
2423 goto done;
2425 error = got_ref_resolve(&commit_id, repo, head_ref);
2426 got_ref_close(head_ref);
2428 } else
2429 error = got_repo_match_object_id(&commit_id, NULL,
2430 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2431 if (error)
2432 goto done;
2434 error = got_object_id_str(&gw_trans->commit, commit_id);
2435 if (error)
2436 goto done;
2438 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2439 if (error)
2440 goto done;
2442 error = got_object_open_as_tree(&tree, repo, tree_id);
2443 if (error)
2444 goto done;
2446 nentries = got_object_tree_get_nentries(tree);
2448 for (i = 0; i < nentries; i++) {
2449 struct got_tree_entry *te;
2450 const char *modestr = "";
2451 char *id = NULL, *url_html = NULL;
2453 te = got_object_tree_get_entry(tree, i);
2455 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2456 if (error)
2457 goto done;
2459 if ((asprintf(&id, "%s", id_str)) == -1) {
2460 error = got_error_from_errno("asprintf");
2461 free(id_str);
2462 goto done;
2465 mode_t mode = got_tree_entry_get_mode(te);
2467 if (got_object_tree_entry_is_submodule(te))
2468 modestr = "$";
2469 else if (S_ISLNK(mode))
2470 modestr = "@";
2471 else if (S_ISDIR(mode))
2472 modestr = "/";
2473 else if (mode & S_IXUSR)
2474 modestr = "*";
2476 char *build_folder = NULL;
2477 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2478 if (gw_trans->repo_folder != NULL) {
2479 if ((asprintf(&build_folder, "%s/%s",
2480 gw_trans->repo_folder,
2481 got_tree_entry_get_name(te))) == -1) {
2482 error =
2483 got_error_from_errno("asprintf");
2484 goto done;
2486 } else {
2487 if (asprintf(&build_folder, "%s",
2488 got_tree_entry_get_name(te)) == -1)
2489 goto done;
2492 if ((asprintf(&url_html, folder_html,
2493 gw_trans->repo_name, gw_trans->action_name,
2494 gw_trans->commit, build_folder,
2495 got_tree_entry_get_name(te), modestr)) == -1) {
2496 error = got_error_from_errno("asprintf");
2497 goto done;
2499 } else {
2500 if (gw_trans->repo_folder != NULL) {
2501 if ((asprintf(&build_folder, "%s",
2502 gw_trans->repo_folder)) == -1) {
2503 error =
2504 got_error_from_errno("asprintf");
2505 goto done;
2507 } else
2508 build_folder = strdup("");
2510 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2511 "blame", gw_trans->commit,
2512 got_tree_entry_get_name(te), build_folder,
2513 got_tree_entry_get_name(te), modestr)) == -1) {
2514 error = got_error_from_errno("asprintf");
2515 goto done;
2518 free(build_folder);
2520 if (error)
2521 goto done;
2523 if ((asprintf(&tree_row, tree_line, url_html)) == -1) {
2524 error = got_error_from_errno("asprintf");
2525 goto done;
2527 error = buf_puts(&newsize, diffbuf, tree_row);
2528 if (error)
2529 goto done;
2531 free(id);
2532 free(id_str);
2533 free(url_html);
2534 free(tree_row);
2537 if (buf_len(diffbuf) > 0) {
2538 error = buf_putc(diffbuf, '\0');
2539 tree_html = strdup(buf_get(diffbuf));
2541 done:
2542 if (tree)
2543 got_object_tree_close(tree);
2544 if (repo)
2545 got_repo_close(repo);
2547 free(in_repo_path);
2548 free(tree_id);
2549 free(diffbuf);
2550 if (error)
2551 return NULL;
2552 else
2553 return tree_html;
2556 static char *
2557 gw_get_repo_heads(struct gw_trans *gw_trans)
2559 const struct got_error *error = NULL;
2560 struct got_repository *repo = NULL;
2561 struct got_reflist_head refs;
2562 struct got_reflist_entry *re;
2563 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2564 struct buf *diffbuf = NULL;
2565 size_t newsize;
2567 SIMPLEQ_INIT(&refs);
2569 error = buf_alloc(&diffbuf, 0);
2570 if (error)
2571 return NULL;
2573 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2574 if (error)
2575 goto done;
2577 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2578 NULL);
2579 if (error)
2580 goto done;
2582 SIMPLEQ_FOREACH(re, &refs, entry) {
2583 char *refname;
2585 refname = strdup(got_ref_get_name(re->ref));
2586 if (refname == NULL) {
2587 error = got_error_from_errno("got_ref_to_str");
2588 goto done;
2591 if (strncmp(refname, "refs/heads/", 11) != 0) {
2592 free(refname);
2593 continue;
2596 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2597 TM_DIFF);
2599 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2600 refname, gw_trans->repo_name, refname,
2601 gw_trans->repo_name, refname, gw_trans->repo_name,
2602 refname)) == -1) {
2603 error = got_error_from_errno("asprintf");
2604 goto done;
2607 if (strncmp(refname, "refs/heads/", 11) == 0)
2608 refname += 11;
2610 if ((asprintf(&head_row, heads_row, age, refname,
2611 head_navs_disp)) == -1) {
2612 error = got_error_from_errno("asprintf");
2613 goto done;
2616 error = buf_puts(&newsize, diffbuf, head_row);
2618 free(head_navs_disp);
2619 free(head_row);
2622 if (buf_len(diffbuf) > 0) {
2623 error = buf_putc(diffbuf, '\0');
2624 heads = strdup(buf_get(diffbuf));
2626 done:
2627 buf_free(diffbuf);
2628 got_ref_list_free(&refs);
2629 if (repo)
2630 got_repo_close(repo);
2631 if (error)
2632 return NULL;
2633 else
2634 return heads;
2637 static char *
2638 gw_get_got_link(struct gw_trans *gw_trans)
2640 char *link;
2642 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2643 gw_trans->gw_conf->got_logo)) == -1)
2644 return NULL;
2646 return link;
2649 static char *
2650 gw_get_site_link(struct gw_trans *gw_trans)
2652 char *link, *repo = "", *action = "";
2654 if (gw_trans->repo_name != NULL)
2655 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2656 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2657 return NULL;
2659 if (gw_trans->action_name != NULL)
2660 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2661 return NULL;
2663 if ((asprintf(&link, site_link, GOTWEB,
2664 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2665 return NULL;
2667 return link;
2670 static char *
2671 gw_colordiff_line(char *buf)
2673 const struct got_error *error = NULL;
2674 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2675 struct buf *diffbuf = NULL;
2676 size_t newsize;
2678 error = buf_alloc(&diffbuf, 0);
2679 if (error)
2680 return NULL;
2682 if (buf == NULL)
2683 return NULL;
2684 if (strncmp(buf, "-", 1) == 0)
2685 color = "diff_minus";
2686 if (strncmp(buf, "+", 1) == 0)
2687 color = "diff_plus";
2688 if (strncmp(buf, "@@", 2) == 0)
2689 color = "diff_chunk_header";
2690 if (strncmp(buf, "@@", 2) == 0)
2691 color = "diff_chunk_header";
2692 if (strncmp(buf, "commit +", 8) == 0)
2693 color = "diff_meta";
2694 if (strncmp(buf, "commit -", 8) == 0)
2695 color = "diff_meta";
2696 if (strncmp(buf, "blob +", 6) == 0)
2697 color = "diff_meta";
2698 if (strncmp(buf, "blob -", 6) == 0)
2699 color = "diff_meta";
2700 if (strncmp(buf, "file +", 6) == 0)
2701 color = "diff_meta";
2702 if (strncmp(buf, "file -", 6) == 0)
2703 color = "diff_meta";
2704 if (strncmp(buf, "from:", 5) == 0)
2705 color = "diff_author";
2706 if (strncmp(buf, "via:", 4) == 0)
2707 color = "diff_author";
2708 if (strncmp(buf, "date:", 5) == 0)
2709 color = "diff_date";
2711 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2712 return NULL;
2714 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2715 if (error)
2716 return NULL;
2718 error = buf_puts(&newsize, diffbuf, buf);
2719 if (error)
2720 return NULL;
2722 if (buf_len(diffbuf) > 0) {
2723 error = buf_putc(diffbuf, '\0');
2724 colorized_line = strdup(buf_get(diffbuf));
2727 free(diffbuf);
2728 free(div_diff_line_div);
2729 return colorized_line;
2732 static char *
2733 gw_html_escape(const char *html)
2735 char *escaped_str = NULL, *buf;
2736 char c[1];
2737 size_t sz, i, buff_sz = 2048;
2739 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2740 return NULL;
2742 if (html == NULL)
2743 return NULL;
2744 else
2745 if ((sz = strlen(html)) == 0)
2746 return NULL;
2748 /* only work with buff_sz */
2749 if (buff_sz < sz)
2750 sz = buff_sz;
2752 for (i = 0; i < sz; i++) {
2753 c[0] = html[i];
2754 switch (c[0]) {
2755 case ('>'):
2756 strcat(buf, "&gt;");
2757 break;
2758 case ('&'):
2759 strcat(buf, "&amp;");
2760 break;
2761 case ('<'):
2762 strcat(buf, "&lt;");
2763 break;
2764 case ('"'):
2765 strcat(buf, "&quot;");
2766 break;
2767 case ('\''):
2768 strcat(buf, "&apos;");
2769 break;
2770 case ('\n'):
2771 strcat(buf, "<br />");
2772 default:
2773 strcat(buf, &c[0]);
2774 break;
2777 asprintf(&escaped_str, "%s", buf);
2778 free(buf);
2779 return escaped_str;
2782 int
2783 main(int argc, char *argv[])
2785 const struct got_error *error = NULL;
2786 struct gw_trans *gw_trans;
2787 struct gw_dir *dir = NULL, *tdir;
2788 const char *page = "index";
2789 int gw_malloc = 1;
2790 enum kcgi_err kerr;
2792 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2793 errx(1, "malloc");
2795 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2796 errx(1, "malloc");
2798 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2799 errx(1, "malloc");
2801 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2802 errx(1, "malloc");
2804 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2805 if (kerr != KCGI_OK) {
2806 error = gw_kcgi_error(kerr);
2807 goto done;
2810 if ((gw_trans->gw_conf =
2811 malloc(sizeof(struct gotweb_conf))) == NULL) {
2812 gw_malloc = 0;
2813 error = got_error_from_errno("malloc");
2814 goto done;
2817 TAILQ_INIT(&gw_trans->gw_dirs);
2818 TAILQ_INIT(&gw_trans->gw_headers);
2820 gw_trans->page = 0;
2821 gw_trans->repos_total = 0;
2822 gw_trans->repo_path = NULL;
2823 gw_trans->commit = NULL;
2824 gw_trans->headref = strdup(GOT_REF_HEAD);
2825 gw_trans->mime = KMIME_TEXT_HTML;
2826 gw_trans->gw_tmpl->key = gw_templs;
2827 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2828 gw_trans->gw_tmpl->arg = gw_trans;
2829 gw_trans->gw_tmpl->cb = gw_template;
2830 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2831 if (error)
2832 goto done;
2834 error = gw_parse_querystring(gw_trans);
2835 if (error)
2836 goto done;
2838 error = gw_display_index(gw_trans, error);
2839 done:
2840 if (error) {
2841 gw_trans->mime = KMIME_TEXT_PLAIN;
2842 gw_trans->action = GW_ERR;
2843 gw_display_index(gw_trans, error);
2845 if (gw_malloc) {
2846 free(gw_trans->gw_conf->got_repos_path);
2847 free(gw_trans->gw_conf->got_site_name);
2848 free(gw_trans->gw_conf->got_site_owner);
2849 free(gw_trans->gw_conf->got_site_link);
2850 free(gw_trans->gw_conf->got_logo);
2851 free(gw_trans->gw_conf->got_logo_url);
2852 free(gw_trans->gw_conf);
2853 free(gw_trans->commit);
2854 free(gw_trans->repo_path);
2855 free(gw_trans->repo_name);
2856 free(gw_trans->repo_file);
2857 free(gw_trans->action_name);
2858 free(gw_trans->headref);
2860 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2861 free(dir->name);
2862 free(dir->description);
2863 free(dir->age);
2864 free(dir->url);
2865 free(dir->path);
2866 free(dir);
2871 khttp_free(gw_trans->gw_req);
2872 return EXIT_SUCCESS;