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("");
328 if ((asprintf(&blame_html_disp, blame_header,
329 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
330 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
331 blame_html)) == -1)
332 return got_error_from_errno("asprintf");
334 if ((asprintf(&blame, blame_wrapper, blame_html_disp)) == -1)
335 return got_error_from_errno("asprintf");
337 kerr = khttp_puts(gw_trans->gw_req, blame);
338 if (kerr != KCGI_OK)
339 error = gw_kcgi_error(kerr);
340 got_ref_list_free(&header->refs);
341 gw_free_headers(header);
342 free(blame_html_disp);
343 free(blame_html);
344 free(blame);
345 return error;
348 static const struct got_error *
349 gw_diff(struct gw_trans *gw_trans)
351 const struct got_error *error = NULL;
352 struct gw_header *header = NULL;
353 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
354 enum kcgi_err kerr;
356 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
357 NULL) == -1)
358 return got_error_from_errno("pledge");
360 if ((header = malloc(sizeof(struct gw_header))) == NULL)
361 return got_error_from_errno("malloc");
363 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
364 if (error)
365 return error;
367 error = gw_get_header(gw_trans, header, 1);
368 if (error)
369 return error;
371 diff_html = gw_get_diff(gw_trans, header);
373 if (diff_html == NULL)
374 diff_html = strdup("");
376 if ((asprintf(&diff_html_disp, diff_header,
377 gw_gen_diff_header(header->parent_id, header->commit_id),
378 gw_gen_commit_header(header->commit_id, header->refs_str),
379 gw_gen_tree_header(header->tree_id),
380 gw_gen_author_header(header->author),
381 gw_gen_committer_header(header->committer),
382 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
383 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
384 diff_html)) == -1)
385 return got_error_from_errno("asprintf");
387 if ((asprintf(&diff, diff_wrapper, diff_html_disp)) == -1)
388 return got_error_from_errno("asprintf");
390 kerr = khttp_puts(gw_trans->gw_req, diff);
391 if (kerr != KCGI_OK)
392 error = gw_kcgi_error(kerr);
393 got_ref_list_free(&header->refs);
394 gw_free_headers(header);
395 free(diff_html_disp);
396 free(diff_html);
397 free(diff);
398 return error;
401 static const struct got_error *
402 gw_index(struct gw_trans *gw_trans)
404 const struct got_error *error = NULL;
405 struct gw_dir *gw_dir = NULL;
406 char *html, *navs, *next, *prev;
407 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
408 enum kcgi_err kerr;
410 if (pledge("stdio rpath proc exec sendfd unveil",
411 NULL) == -1) {
412 error = got_error_from_errno("pledge");
413 return error;
416 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
417 if (error)
418 return error;
420 error = gw_load_got_paths(gw_trans);
421 if (error)
422 return error;
424 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
425 if (kerr != KCGI_OK)
426 return gw_kcgi_error(kerr);
428 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
429 if (asprintf(&html, index_projects_empty,
430 gw_trans->gw_conf->got_repos_path) == -1)
431 return got_error_from_errno("asprintf");
432 kerr = khttp_puts(gw_trans->gw_req, html);
433 if (kerr != KCGI_OK)
434 error = gw_kcgi_error(kerr);
435 free(html);
436 return error;
439 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
440 dir_c++;
442 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
443 if (gw_trans->page > 0 && (gw_trans->page *
444 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
445 prev_disp++;
446 continue;
449 prev_disp++;
451 if (error)
452 return error;
453 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
454 gw_dir->name, gw_dir->name)) == -1)
455 return got_error_from_errno("asprintf");
457 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
458 gw_dir->description, gw_dir->owner, gw_dir->age,
459 navs)) == -1)
460 return got_error_from_errno("asprintf");
462 kerr = khttp_puts(gw_trans->gw_req, html);
463 free(navs);
464 free(html);
465 if (kerr != KCGI_OK)
466 return gw_kcgi_error(kerr);
468 if (gw_trans->gw_conf->got_max_repos_display == 0)
469 continue;
471 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
472 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
473 if (kerr != KCGI_OK)
474 return gw_kcgi_error(kerr);
475 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
476 (gw_trans->page > 0) &&
477 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
478 prev_disp == gw_trans->repos_total)) {
479 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
480 if (kerr != KCGI_OK)
481 return gw_kcgi_error(kerr);
484 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
485 (gw_trans->page > 0) &&
486 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
487 prev_disp == gw_trans->repos_total)) {
488 if ((asprintf(&prev, nav_prev,
489 gw_trans->page - 1)) == -1)
490 return got_error_from_errno("asprintf");
491 kerr = khttp_puts(gw_trans->gw_req, prev);
492 free(prev);
493 if (kerr != KCGI_OK)
494 return gw_kcgi_error(kerr);
497 kerr = khttp_puts(gw_trans->gw_req, div_end);
498 if (kerr != KCGI_OK)
499 return gw_kcgi_error(kerr);
501 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
502 next_disp == gw_trans->gw_conf->got_max_repos_display &&
503 dir_c != (gw_trans->page + 1) *
504 gw_trans->gw_conf->got_max_repos_display) {
505 if ((asprintf(&next, nav_next,
506 gw_trans->page + 1)) == -1)
507 return got_error_from_errno("calloc");
508 kerr = khttp_puts(gw_trans->gw_req, next);
509 free(next);
510 if (kerr != KCGI_OK)
511 return gw_kcgi_error(kerr);
512 kerr = khttp_puts(gw_trans->gw_req, div_end);
513 if (kerr != KCGI_OK)
514 error = gw_kcgi_error(kerr);
515 next_disp = 0;
516 break;
519 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
520 (gw_trans->page > 0) &&
521 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
522 prev_disp == gw_trans->repos_total)) {
523 kerr = khttp_puts(gw_trans->gw_req, div_end);
524 if (kerr != KCGI_OK)
525 return gw_kcgi_error(kerr);
528 next_disp++;
530 return error;
533 static const struct got_error *
534 gw_commits(struct gw_trans *gw_trans)
536 const struct got_error *error = NULL;
537 char *commits_html, *commits_navs_html;
538 struct gw_header *header = NULL, *n_header = NULL;
539 enum kcgi_err kerr;
541 if ((header = malloc(sizeof(struct gw_header))) == NULL)
542 return got_error_from_errno("malloc");
544 if (pledge("stdio rpath proc exec sendfd unveil",
545 NULL) == -1) {
546 error = got_error_from_errno("pledge");
547 return error;
550 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
551 if (error)
552 return error;
554 error = gw_get_header(gw_trans, header,
555 gw_trans->gw_conf->got_max_commits_display);
557 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
558 if (kerr != KCGI_OK)
559 return gw_kcgi_error(kerr);
560 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
561 if ((asprintf(&commits_navs_html, commits_navs,
562 gw_trans->repo_name, n_header->commit_id,
563 gw_trans->repo_name, n_header->commit_id,
564 gw_trans->repo_name, n_header->commit_id)) == -1)
565 return got_error_from_errno("asprintf");
567 if ((asprintf(&commits_html, commits_line,
568 gw_gen_commit_header(n_header->commit_id,
569 n_header->refs_str),
570 gw_gen_author_header(n_header->author),
571 gw_gen_committer_header(n_header->committer),
572 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
573 TM_LONG)), gw_html_escape(n_header->commit_msg),
574 commits_navs_html)) == -1)
575 return got_error_from_errno("asprintf");
576 kerr = khttp_puts(gw_trans->gw_req, commits_html);
577 if (kerr != KCGI_OK)
578 return gw_kcgi_error(kerr);
580 kerr = khttp_puts(gw_trans->gw_req, div_end);
581 if (kerr != KCGI_OK)
582 error = gw_kcgi_error(kerr);
584 got_ref_list_free(&header->refs);
585 gw_free_headers(header);
586 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
587 gw_free_headers(n_header);
588 return error;
591 static const struct got_error *
592 gw_briefs(struct gw_trans *gw_trans)
594 const struct got_error *error = NULL;
595 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
596 struct gw_header *header = NULL, *n_header = NULL;
597 enum kcgi_err kerr;
599 if ((header = malloc(sizeof(struct gw_header))) == NULL)
600 return got_error_from_errno("malloc");
602 if (pledge("stdio rpath proc exec sendfd unveil",
603 NULL) == -1) {
604 error = got_error_from_errno("pledge");
605 return error;
608 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
609 if (error)
610 return error;
612 if (gw_trans->action == GW_SUMMARY)
613 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
614 else
615 error = gw_get_header(gw_trans, header,
616 gw_trans->gw_conf->got_max_commits_display);
618 if (error)
619 return error;
621 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
622 if (kerr != KCGI_OK)
623 return gw_kcgi_error(kerr);
625 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
626 if ((asprintf(&briefs_navs_html, briefs_navs,
627 gw_trans->repo_name, n_header->commit_id,
628 gw_trans->repo_name, n_header->commit_id,
629 gw_trans->repo_name, n_header->commit_id)) == -1)
630 return got_error_from_errno("asprintf");
631 newline = strchr(n_header->commit_msg, '\n');
632 if (newline)
633 *newline = '\0';
634 if ((asprintf(&briefs_html, briefs_line,
635 gw_get_time_str(n_header->committer_time, TM_DIFF),
636 n_header->author, gw_html_escape(n_header->commit_msg),
637 briefs_navs_html)) == -1)
638 return got_error_from_errno("asprintf");
639 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
640 if (kerr != KCGI_OK)
641 return gw_kcgi_error(kerr);
643 kerr = khttp_puts(gw_trans->gw_req, div_end);
644 if (kerr != KCGI_OK)
645 error = gw_kcgi_error(kerr);
647 got_ref_list_free(&header->refs);
648 gw_free_headers(header);
649 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
650 gw_free_headers(n_header);
651 return error;
654 static const struct got_error *
655 gw_summary(struct gw_trans *gw_trans)
657 const struct got_error *error = NULL;
658 char *description_html, *repo_owner_html, *repo_age_html,
659 *cloneurl_html, *tags, *heads, *tags_html,
660 *heads_html, *age;
661 enum kcgi_err kerr;
663 if (pledge("stdio rpath proc exec sendfd unveil",
664 NULL) == -1) {
665 error = got_error_from_errno("pledge");
666 return error;
669 /* unveil is applied with gw_briefs below */
671 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
672 if (kerr != KCGI_OK)
673 return gw_kcgi_error(kerr);
675 if (gw_trans->gw_conf->got_show_repo_description) {
676 if (gw_trans->gw_dir->description != NULL &&
677 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
678 if ((asprintf(&description_html, description,
679 gw_trans->gw_dir->description)) == -1)
680 return got_error_from_errno("asprintf");
682 kerr = khttp_puts(gw_trans->gw_req, description_html);
683 free(description_html);
684 if (kerr != KCGI_OK)
685 return gw_kcgi_error(kerr);
689 if (gw_trans->gw_conf->got_show_repo_owner) {
690 if (gw_trans->gw_dir->owner != NULL &&
691 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
692 if ((asprintf(&repo_owner_html, repo_owner,
693 gw_trans->gw_dir->owner)) == -1)
694 return got_error_from_errno("asprintf");
696 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
697 free(repo_owner_html);
698 if (kerr != KCGI_OK)
699 return gw_kcgi_error(kerr);
703 if (gw_trans->gw_conf->got_show_repo_age) {
704 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
705 "refs/heads", TM_LONG);
706 if (age != NULL && (strcmp(age, "") != 0)) {
707 if ((asprintf(&repo_age_html, last_change, age)) == -1)
708 return got_error_from_errno("asprintf");
710 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
711 free(repo_age_html);
712 free(age);
713 if (kerr != KCGI_OK)
714 return gw_kcgi_error(kerr);
718 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
719 if (gw_trans->gw_dir->url != NULL &&
720 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
721 if ((asprintf(&cloneurl_html, cloneurl,
722 gw_trans->gw_dir->url)) == -1)
723 return got_error_from_errno("asprintf");
725 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
726 free(cloneurl_html);
727 if (kerr != KCGI_OK)
728 return gw_kcgi_error(kerr);
731 kerr = khttp_puts(gw_trans->gw_req, div_end);
732 if (kerr != KCGI_OK)
733 return gw_kcgi_error(kerr);
735 error = gw_briefs(gw_trans);
736 if (error)
737 return error;
739 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
740 heads = gw_get_repo_heads(gw_trans);
742 if (tags != NULL && strcmp(tags, "") != 0) {
743 if ((asprintf(&tags_html, summary_tags,
744 tags)) == -1)
745 return got_error_from_errno("asprintf");
746 kerr = khttp_puts(gw_trans->gw_req, tags_html);
747 free(tags_html);
748 free(tags);
749 if (kerr != KCGI_OK)
750 return gw_kcgi_error(kerr);
753 if (heads != NULL && strcmp(heads, "") != 0) {
754 if ((asprintf(&heads_html, summary_heads,
755 heads)) == -1)
756 return got_error_from_errno("asprintf");
757 kerr = khttp_puts(gw_trans->gw_req, heads_html);
758 free(heads_html);
759 free(heads);
760 if (kerr != KCGI_OK)
761 return gw_kcgi_error(kerr);
763 return error;
766 static const struct got_error *
767 gw_tree(struct gw_trans *gw_trans)
769 const struct got_error *error = NULL;
770 struct gw_header *header = NULL;
771 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
772 enum kcgi_err kerr;
774 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
775 return got_error_from_errno("pledge");
777 if ((header = malloc(sizeof(struct gw_header))) == NULL)
778 return got_error_from_errno("malloc");
780 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
781 if (error)
782 return error;
784 error = gw_get_header(gw_trans, header, 1);
785 if (error)
786 return error;
788 tree_html = gw_get_repo_tree(gw_trans);
790 if (tree_html == NULL)
791 tree_html = strdup("");
793 if ((asprintf(&tree_html_disp, tree_header,
794 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
795 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
796 tree_html)) == -1)
797 return got_error_from_errno("asprintf");
799 if ((asprintf(&tree, tree_wrapper, tree_html_disp)) == -1)
800 return got_error_from_errno("asprintf");
802 kerr = khttp_puts(gw_trans->gw_req, tree);
803 if (kerr != KCGI_OK)
804 error = gw_kcgi_error(kerr);
805 got_ref_list_free(&header->refs);
806 gw_free_headers(header);
807 free(tree_html_disp);
808 free(tree_html);
809 free(tree);
810 return error;
813 static const struct got_error *
814 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
816 const struct got_error *error = NULL;
817 DIR *dt;
818 char *dir_test;
819 int opened = 0;
821 if ((asprintf(&dir_test, "%s/%s/%s",
822 gw_trans->gw_conf->got_repos_path, gw_dir->name,
823 GOTWEB_GIT_DIR)) == -1)
824 return got_error_from_errno("asprintf");
826 dt = opendir(dir_test);
827 if (dt == NULL) {
828 free(dir_test);
829 } else {
830 gw_dir->path = strdup(dir_test);
831 opened = 1;
832 goto done;
835 if ((asprintf(&dir_test, "%s/%s/%s",
836 gw_trans->gw_conf->got_repos_path, gw_dir->name,
837 GOTWEB_GOT_DIR)) == -1)
838 return got_error_from_errno("asprintf");
840 dt = opendir(dir_test);
841 if (dt == NULL)
842 free(dir_test);
843 else {
844 opened = 1;
845 error = got_error(GOT_ERR_NOT_GIT_REPO);
846 goto errored;
849 if ((asprintf(&dir_test, "%s/%s",
850 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
851 return got_error_from_errno("asprintf");
853 gw_dir->path = strdup(dir_test);
855 done:
856 gw_dir->description = gw_get_repo_description(gw_trans,
857 gw_dir->path);
858 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
859 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
860 TM_DIFF);
861 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
863 errored:
864 free(dir_test);
865 if (opened)
866 closedir(dt);
867 return error;
870 static const struct got_error *
871 gw_load_got_paths(struct gw_trans *gw_trans)
873 const struct got_error *error = NULL;
874 DIR *d;
875 struct dirent **sd_dent;
876 struct gw_dir *gw_dir;
877 struct stat st;
878 unsigned int d_cnt, d_i;
880 d = opendir(gw_trans->gw_conf->got_repos_path);
881 if (d == NULL) {
882 error = got_error_from_errno2("opendir",
883 gw_trans->gw_conf->got_repos_path);
884 return error;
887 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
888 alphasort);
889 if (d_cnt == -1) {
890 error = got_error_from_errno2("scandir",
891 gw_trans->gw_conf->got_repos_path);
892 return error;
895 for (d_i = 0; d_i < d_cnt; d_i++) {
896 if (gw_trans->gw_conf->got_max_repos > 0 &&
897 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
898 break; /* account for parent and self */
900 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
901 strcmp(sd_dent[d_i]->d_name, "..") == 0)
902 continue;
904 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
905 return got_error_from_errno("gw_dir malloc");
907 error = gw_load_got_path(gw_trans, gw_dir);
908 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
909 continue;
910 else if (error)
911 return error;
913 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
914 !got_path_dir_is_empty(gw_dir->path)) {
915 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
916 entry);
917 gw_trans->repos_total++;
921 closedir(d);
922 return error;
925 static const struct got_error *
926 gw_parse_querystring(struct gw_trans *gw_trans)
928 const struct got_error *error = NULL;
929 struct kpair *p;
930 struct gw_query_action *action = NULL;
931 unsigned int i;
933 if (gw_trans->gw_req->fieldnmap[0]) {
934 error = got_error_from_errno("bad parse");
935 return error;
936 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
937 /* define gw_trans->repo_path */
938 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
939 return got_error_from_errno("asprintf");
941 if ((asprintf(&gw_trans->repo_path, "%s/%s",
942 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
943 return got_error_from_errno("asprintf");
945 /* get action and set function */
946 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
947 for (i = 0; i < nitems(gw_query_funcs); i++) {
948 action = &gw_query_funcs[i];
949 if (action->func_name == NULL)
950 continue;
952 if (strcmp(action->func_name,
953 p->parsed.s) == 0) {
954 gw_trans->action = i;
955 if ((asprintf(&gw_trans->action_name,
956 "%s", action->func_name)) == -1)
957 return
958 got_error_from_errno(
959 "asprintf");
961 break;
964 action = NULL;
967 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
968 if ((asprintf(&gw_trans->commit, "%s",
969 p->parsed.s)) == -1)
970 return got_error_from_errno("asprintf");
972 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
973 if ((asprintf(&gw_trans->repo_file, "%s",
974 p->parsed.s)) == -1)
975 return got_error_from_errno("asprintf");
977 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
978 if ((asprintf(&gw_trans->repo_folder, "%s",
979 p->parsed.s)) == -1)
980 return got_error_from_errno("asprintf");
982 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
983 if ((asprintf(&gw_trans->headref, "%s",
984 p->parsed.s)) == -1)
985 return got_error_from_errno("asprintf");
987 if (action == NULL) {
988 error = got_error_from_errno("invalid action");
989 return error;
991 if ((gw_trans->gw_dir =
992 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
993 return got_error_from_errno("gw_dir malloc");
995 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
996 if (error)
997 return error;
998 } else
999 gw_trans->action = GW_INDEX;
1001 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1002 gw_trans->page = p->parsed.i;
1004 /* if (gw_trans->action == GW_RAW) */
1005 /* gw_trans->mime = KMIME_TEXT_PLAIN; */
1007 return error;
1010 static struct gw_dir *
1011 gw_init_gw_dir(char *dir)
1013 struct gw_dir *gw_dir;
1015 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1016 return NULL;
1018 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1019 return NULL;
1021 return gw_dir;
1024 static const struct got_error *
1025 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1027 enum kcgi_err kerr;
1029 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1030 if (kerr != KCGI_OK)
1031 return gw_kcgi_error(kerr);
1032 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1033 khttps[code]);
1034 if (kerr != KCGI_OK)
1035 return gw_kcgi_error(kerr);
1036 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1037 kmimetypes[mime]);
1038 if (kerr != KCGI_OK)
1039 return gw_kcgi_error(kerr);
1040 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1041 if (kerr != KCGI_OK)
1042 return gw_kcgi_error(kerr);
1043 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1044 if (kerr != KCGI_OK)
1045 return gw_kcgi_error(kerr);
1046 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1047 if (kerr != KCGI_OK)
1048 return gw_kcgi_error(kerr);
1050 kerr = khttp_body(gw_trans->gw_req);
1051 return gw_kcgi_error(kerr);
1054 static const struct got_error *
1055 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
1057 enum kcgi_err kerr;
1059 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1060 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1062 if (err)
1063 kerr = khttp_puts(gw_trans->gw_req, err->msg);
1064 else
1065 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1066 gw_query_funcs[gw_trans->action].template);
1067 if (kerr != KCGI_OK)
1068 return gw_kcgi_error(kerr);
1070 kerr = khtml_close(gw_trans->gw_html_req);
1071 return gw_kcgi_error(kerr);
1074 static int
1075 gw_template(size_t key, void *arg)
1077 const struct got_error *error = NULL;
1078 struct gw_trans *gw_trans = arg;
1079 char *gw_got_link, *gw_site_link;
1080 char *site_owner_name, *site_owner_name_h;
1082 switch (key) {
1083 case (TEMPL_HEAD):
1084 khttp_puts(gw_trans->gw_req, head);
1085 break;
1086 case(TEMPL_HEADER):
1087 gw_got_link = gw_get_got_link(gw_trans);
1088 if (gw_got_link != NULL)
1089 khttp_puts(gw_trans->gw_req, gw_got_link);
1091 free(gw_got_link);
1092 break;
1093 case (TEMPL_SITEPATH):
1094 gw_site_link = gw_get_site_link(gw_trans);
1095 if (gw_site_link != NULL)
1096 khttp_puts(gw_trans->gw_req, gw_site_link);
1098 free(gw_site_link);
1099 break;
1100 case(TEMPL_TITLE):
1101 if (gw_trans->gw_conf->got_site_name != NULL)
1102 khtml_puts(gw_trans->gw_html_req,
1103 gw_trans->gw_conf->got_site_name);
1105 break;
1106 case (TEMPL_SEARCH):
1107 khttp_puts(gw_trans->gw_req, search);
1108 break;
1109 case(TEMPL_SITEOWNER):
1110 if (gw_trans->gw_conf->got_site_owner != NULL &&
1111 gw_trans->gw_conf->got_show_site_owner) {
1112 site_owner_name =
1113 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1114 if ((asprintf(&site_owner_name_h, site_owner,
1115 site_owner_name))
1116 == -1)
1117 return 0;
1119 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1120 free(site_owner_name);
1121 free(site_owner_name_h);
1123 break;
1124 case(TEMPL_CONTENT):
1125 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1126 if (error)
1127 khttp_puts(gw_trans->gw_req, error->msg);
1128 break;
1129 default:
1130 return 0;
1132 return 1;
1135 static char *
1136 gw_gen_commit_header(char *str1, char *str2)
1138 char *return_html = NULL, *ref_str = NULL;
1140 if (strcmp(str2, "") != 0) {
1141 if ((asprintf(&ref_str, "(%s)", str2)) == -1) {
1142 return_html = strdup("");
1143 return return_html;
1145 } else
1146 ref_str = strdup("");
1149 if ((asprintf(&return_html, header_commit_html, str1, ref_str)) == -1)
1150 return_html = strdup("");
1152 free(ref_str);
1153 return return_html;
1156 static char *
1157 gw_gen_diff_header(char *str1, char *str2)
1159 char *return_html = NULL;
1161 if ((asprintf(&return_html, header_diff_html, str1, str2)) == -1)
1162 return_html = strdup("");
1164 return return_html;
1167 static char *
1168 gw_gen_author_header(char *str)
1170 char *return_html = NULL;
1172 if ((asprintf(&return_html, header_author_html, str)) == -1)
1173 return_html = strdup("");
1175 return return_html;
1178 static char *
1179 gw_gen_committer_header(char *str)
1181 char *return_html = NULL;
1183 if ((asprintf(&return_html, header_committer_html, str)) == -1)
1184 return_html = strdup("");
1186 return return_html;
1189 static char *
1190 gw_gen_age_header(char *str)
1192 char *return_html = NULL;
1194 if ((asprintf(&return_html, header_age_html, str)) == -1)
1195 return_html = strdup("");
1197 return return_html;
1200 static char *
1201 gw_gen_commit_msg_header(char *str)
1203 char *return_html = NULL;
1205 if ((asprintf(&return_html, header_commit_msg_html, str)) == -1)
1206 return_html = strdup("");
1208 return return_html;
1211 static char *
1212 gw_gen_tree_header(char *str)
1214 char *return_html = NULL;
1216 if ((asprintf(&return_html, header_tree_html, str)) == -1)
1217 return_html = strdup("");
1219 return return_html;
1222 static char *
1223 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1225 FILE *f;
1226 char *description = NULL, *d_file = NULL;
1227 unsigned int len;
1229 if (gw_trans->gw_conf->got_show_repo_description == false)
1230 goto err;
1232 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1233 goto err;
1235 if ((f = fopen(d_file, "r")) == NULL)
1236 goto err;
1238 fseek(f, 0, SEEK_END);
1239 len = ftell(f) + 1;
1240 fseek(f, 0, SEEK_SET);
1241 if ((description = calloc(len, sizeof(char *))) == NULL)
1242 goto err;
1244 fread(description, 1, len, f);
1245 fclose(f);
1246 free(d_file);
1247 return description;
1248 err:
1249 if ((asprintf(&description, "%s", "")) == -1)
1250 return NULL;
1252 return description;
1255 static char *
1256 gw_get_time_str(time_t committer_time, int ref_tm)
1258 struct tm tm;
1259 time_t diff_time;
1260 char *years = "years ago", *months = "months ago";
1261 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1262 char *minutes = "minutes ago", *seconds = "seconds ago";
1263 char *now = "right now";
1264 char *repo_age, *s;
1265 char datebuf[29];
1267 switch (ref_tm) {
1268 case TM_DIFF:
1269 diff_time = time(NULL) - committer_time;
1270 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1271 if ((asprintf(&repo_age, "%lld %s",
1272 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1273 return NULL;
1274 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1275 if ((asprintf(&repo_age, "%lld %s",
1276 (diff_time / 60 / 60 / 24 / (365 / 12)),
1277 months)) == -1)
1278 return NULL;
1279 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1280 if ((asprintf(&repo_age, "%lld %s",
1281 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1282 return NULL;
1283 } else if (diff_time > 60 * 60 * 24 * 2) {
1284 if ((asprintf(&repo_age, "%lld %s",
1285 (diff_time / 60 / 60 / 24), days)) == -1)
1286 return NULL;
1287 } else if (diff_time > 60 * 60 * 2) {
1288 if ((asprintf(&repo_age, "%lld %s",
1289 (diff_time / 60 / 60), hours)) == -1)
1290 return NULL;
1291 } else if (diff_time > 60 * 2) {
1292 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1293 minutes)) == -1)
1294 return NULL;
1295 } else if (diff_time > 2) {
1296 if ((asprintf(&repo_age, "%lld %s", diff_time,
1297 seconds)) == -1)
1298 return NULL;
1299 } else {
1300 if ((asprintf(&repo_age, "%s", now)) == -1)
1301 return NULL;
1303 break;
1304 case TM_LONG:
1305 if (gmtime_r(&committer_time, &tm) == NULL)
1306 return NULL;
1308 s = asctime_r(&tm, datebuf);
1309 if (s == NULL)
1310 return NULL;
1312 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1313 return NULL;
1314 break;
1316 return repo_age;
1319 static char *
1320 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1321 int ref_tm)
1323 const struct got_error *error = NULL;
1324 struct got_object_id *id = NULL;
1325 struct got_repository *repo = NULL;
1326 struct got_commit_object *commit = NULL;
1327 struct got_reflist_head refs;
1328 struct got_reflist_entry *re;
1329 struct got_reference *head_ref;
1330 int is_head = 0;
1331 time_t committer_time = 0, cmp_time = 0;
1332 const char *refname;
1333 char *repo_age = NULL;
1335 if (repo_ref == NULL)
1336 return NULL;
1338 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1339 is_head = 1;
1341 SIMPLEQ_INIT(&refs);
1342 if (gw_trans->gw_conf->got_show_repo_age == false) {
1343 if ((asprintf(&repo_age, "")) == -1)
1344 return NULL;
1345 return repo_age;
1348 error = got_repo_open(&repo, dir, NULL);
1349 if (error)
1350 goto err;
1352 if (is_head)
1353 error = got_ref_list(&refs, repo, "refs/heads",
1354 got_ref_cmp_by_name, NULL);
1355 else
1356 error = got_ref_list(&refs, repo, repo_ref,
1357 got_ref_cmp_by_name, NULL);
1358 if (error)
1359 goto err;
1361 SIMPLEQ_FOREACH(re, &refs, entry) {
1362 if (is_head)
1363 refname = strdup(repo_ref);
1364 else
1365 refname = got_ref_get_name(re->ref);
1366 error = got_ref_open(&head_ref, repo, refname, 0);
1367 if (error)
1368 goto err;
1370 error = got_ref_resolve(&id, repo, head_ref);
1371 got_ref_close(head_ref);
1372 if (error)
1373 goto err;
1375 error = got_object_open_as_commit(&commit, repo, id);
1376 if (error)
1377 goto err;
1379 committer_time =
1380 got_object_commit_get_committer_time(commit);
1382 if (cmp_time < committer_time)
1383 cmp_time = committer_time;
1386 if (cmp_time != 0) {
1387 committer_time = cmp_time;
1388 repo_age = gw_get_time_str(committer_time, ref_tm);
1389 } else
1390 if ((asprintf(&repo_age, "")) == -1)
1391 return NULL;
1392 got_ref_list_free(&refs);
1393 free(id);
1394 return repo_age;
1395 err:
1396 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1397 return NULL;
1399 return repo_age;
1402 static char *
1403 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1405 const struct got_error *error;
1406 FILE *f = NULL;
1407 struct got_object_id *id1 = NULL, *id2 = NULL;
1408 struct buf *diffbuf = NULL;
1409 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1410 *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1411 int obj_type;
1412 size_t newsize;
1414 f = got_opentemp();
1415 if (f == NULL)
1416 return NULL;
1418 error = buf_alloc(&diffbuf, 0);
1419 if (error)
1420 return NULL;
1422 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1423 if (error)
1424 goto done;
1426 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1427 error = got_repo_match_object_id(&id1, &label1,
1428 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1429 if (error)
1430 goto done;
1433 error = got_repo_match_object_id(&id2, &label2,
1434 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1435 if (error)
1436 goto done;
1438 error = got_object_get_type(&obj_type, header->repo, id2);
1439 if (error)
1440 goto done;
1441 switch (obj_type) {
1442 case GOT_OBJ_TYPE_BLOB:
1443 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1444 header->repo, f);
1445 break;
1446 case GOT_OBJ_TYPE_TREE:
1447 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1448 header->repo, f);
1449 break;
1450 case GOT_OBJ_TYPE_COMMIT:
1451 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1452 header->repo, f);
1453 break;
1454 default:
1455 error = got_error(GOT_ERR_OBJ_TYPE);
1458 if ((buf = calloc(128, sizeof(char *))) == NULL)
1459 goto done;
1461 fseek(f, 0, SEEK_SET);
1463 while ((fgets(buf, 2048, f)) != NULL) {
1464 n_buf = buf;
1465 while (*n_buf == '\n')
1466 n_buf++;
1467 newline = strchr(n_buf, '\n');
1468 if (newline)
1469 *newline = ' ';
1471 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1472 if (buf_color == NULL)
1473 continue;
1475 error = buf_puts(&newsize, diffbuf, buf_color);
1476 if (error)
1477 return NULL;
1479 error = buf_puts(&newsize, diffbuf, div_end);
1480 if (error)
1481 return NULL;
1484 if (buf_len(diffbuf) > 0) {
1485 error = buf_putc(diffbuf, '\0');
1486 diff_html = strdup(buf_get(diffbuf));
1488 done:
1489 fclose(f);
1490 free(buf_color);
1491 free(buf);
1492 free(diffbuf);
1493 free(label1);
1494 free(label2);
1495 free(id1);
1496 free(id2);
1498 if (error)
1499 return NULL;
1500 else
1501 return diff_html;
1504 static char *
1505 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1507 FILE *f;
1508 char *owner = NULL, *d_file = NULL;
1509 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1510 char *comp, *pos, *buf;
1511 unsigned int i;
1513 if (gw_trans->gw_conf->got_show_repo_owner == false)
1514 goto err;
1516 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1517 goto err;
1519 if ((f = fopen(d_file, "r")) == NULL)
1520 goto err;
1522 if ((buf = calloc(128, sizeof(char *))) == NULL)
1523 goto err;
1525 while ((fgets(buf, 128, f)) != NULL) {
1526 if ((pos = strstr(buf, gotweb)) != NULL)
1527 break;
1529 if ((pos = strstr(buf, gitweb)) != NULL)
1530 break;
1533 if (pos == NULL)
1534 goto err;
1536 do {
1537 fgets(buf, 128, f);
1538 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1540 if (comp == NULL)
1541 goto err;
1543 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1544 goto err;
1546 for (i = 0; i < 2; i++) {
1547 owner = strsep(&buf, "\"");
1550 if (owner == NULL)
1551 goto err;
1553 fclose(f);
1554 free(d_file);
1555 return owner;
1556 err:
1557 if ((asprintf(&owner, "%s", "")) == -1)
1558 return NULL;
1560 return owner;
1563 static char *
1564 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1566 FILE *f;
1567 char *url = NULL, *d_file = NULL;
1568 unsigned int len;
1570 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1571 return NULL;
1573 if ((f = fopen(d_file, "r")) == NULL)
1574 return NULL;
1576 fseek(f, 0, SEEK_END);
1577 len = ftell(f) + 1;
1578 fseek(f, 0, SEEK_SET);
1580 if ((url = calloc(len, sizeof(char *))) == NULL)
1581 return NULL;
1583 fread(url, 1, len, f);
1584 fclose(f);
1585 free(d_file);
1586 return url;
1589 static char *
1590 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
1592 const struct got_error *error = NULL;
1593 struct got_repository *repo = NULL;
1594 struct got_reflist_head refs;
1595 struct got_reflist_entry *re;
1596 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1597 *age = NULL;
1598 char *newline;
1599 struct buf *diffbuf = NULL;
1600 size_t newsize;
1602 error = buf_alloc(&diffbuf, 0);
1603 if (error)
1604 return NULL;
1605 SIMPLEQ_INIT(&refs);
1607 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1608 if (error)
1609 goto done;
1611 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1612 if (error)
1613 goto done;
1615 SIMPLEQ_FOREACH(re, &refs, entry) {
1616 const char *refname;
1617 char *refstr, *tag_commit0, *tag_commit, *id_str;
1618 time_t tagger_time;
1619 struct got_object_id *id;
1620 struct got_tag_object *tag;
1622 refname = got_ref_get_name(re->ref);
1623 if (strncmp(refname, "refs/tags/", 10) != 0)
1624 continue;
1625 refname += 10;
1626 refstr = got_ref_to_str(re->ref);
1627 if (refstr == NULL) {
1628 error = got_error_from_errno("got_ref_to_str");
1629 goto done;
1632 error = got_ref_resolve(&id, repo, re->ref);
1633 if (error)
1634 goto done;
1635 error = got_object_open_as_tag(&tag, repo, id);
1636 free(id);
1637 if (error)
1638 goto done;
1640 tagger_time = got_object_tag_get_tagger_time(tag);
1642 error = got_object_id_str(&id_str,
1643 got_object_tag_get_object_id(tag));
1644 if (error)
1645 goto done;
1647 tag_commit0 = strdup(got_object_tag_get_message(tag));
1649 if (tag_commit0 == NULL) {
1650 error = got_error_from_errno("strdup");
1651 goto done;
1654 tag_commit = tag_commit0;
1655 while (*tag_commit == '\n')
1656 tag_commit++;
1658 switch (tag_type) {
1659 case TAGBRIEF:
1660 newline = strchr(tag_commit, '\n');
1661 if (newline)
1662 *newline = '\0';
1664 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1665 TM_DIFF))) == -1) {
1666 error = got_error_from_errno("asprintf");
1667 goto done;
1670 if ((asprintf(&tags_navs_disp, tags_navs,
1671 gw_trans->repo_name, id_str, gw_trans->repo_name,
1672 id_str, gw_trans->repo_name, id_str,
1673 gw_trans->repo_name, id_str)) == -1) {
1674 error = got_error_from_errno("asprintf");
1675 goto done;
1678 if ((asprintf(&tag_row, tags_row, age, refname,
1679 tag_commit, tags_navs_disp)) == -1) {
1680 error = got_error_from_errno("asprintf");
1681 goto done;
1684 free(tags_navs_disp);
1685 break;
1686 case TAGFULL:
1687 break;
1688 default:
1689 break;
1692 got_object_tag_close(tag);
1694 error = buf_puts(&newsize, diffbuf, tag_row);
1696 free(id_str);
1697 free(refstr);
1698 free(age);
1699 free(tag_commit0);
1700 free(tag_row);
1702 if (error || (limit && --limit == 0))
1703 break;
1706 if (buf_len(diffbuf) > 0) {
1707 error = buf_putc(diffbuf, '\0');
1708 tags = strdup(buf_get(diffbuf));
1710 done:
1711 buf_free(diffbuf);
1712 got_ref_list_free(&refs);
1713 if (repo)
1714 got_repo_close(repo);
1715 if (error)
1716 return NULL;
1717 else
1718 return tags;
1721 static void
1722 gw_free_headers(struct gw_header *header)
1724 free(header->id);
1725 free(header->path);
1726 if (header->commit != NULL)
1727 got_object_commit_close(header->commit);
1728 if (header->repo)
1729 got_repo_close(header->repo);
1730 free(header->refs_str);
1731 free(header->commit_id);
1732 free(header->parent_id);
1733 free(header->tree_id);
1734 free(header->author);
1735 free(header->committer);
1736 free(header->commit_msg);
1739 static struct gw_header *
1740 gw_init_header()
1742 struct gw_header *header;
1744 if ((header = malloc(sizeof(*header))) == NULL)
1745 return NULL;
1747 header->repo = NULL;
1748 header->commit = NULL;
1749 header->id = NULL;
1750 header->path = NULL;
1752 return header;
1755 static const struct got_error *
1756 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1757 int limit)
1759 const struct got_error *error = NULL;
1760 struct got_commit_graph *graph = NULL;
1762 error = got_commit_graph_open(&graph, header->path, 0);
1763 if (error)
1764 goto done;
1766 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1767 NULL, NULL);
1768 if (error)
1769 goto done;
1771 for (;;) {
1772 error = got_commit_graph_iter_next(&header->id, graph,
1773 header->repo, NULL, NULL);
1774 if (error) {
1775 if (error->code == GOT_ERR_ITER_COMPLETED)
1776 error = NULL;
1777 goto done;
1779 if (header->id == NULL)
1780 goto done;
1782 error = got_object_open_as_commit(&header->commit, header->repo,
1783 header->id);
1784 if (error)
1785 goto done;
1787 error = gw_get_commit(gw_trans, header);
1788 if (limit > 1) {
1789 struct gw_header *n_header = NULL;
1790 if ((n_header = gw_init_header()) == NULL)
1791 error = got_error_from_errno("malloc");
1793 n_header->refs_str = strdup(header->refs_str);
1794 n_header->commit_id = strdup(header->commit_id);
1795 n_header->parent_id = strdup(header->parent_id);
1796 n_header->tree_id = strdup(header->tree_id);
1797 n_header->author = strdup(header->author);
1798 n_header->committer = strdup(header->committer);
1799 n_header->commit_msg = strdup(header->commit_msg);
1800 n_header->committer_time = header->committer_time;
1801 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
1802 entry);
1804 if (error || (limit && --limit == 0))
1805 break;
1807 done:
1808 if (graph)
1809 got_commit_graph_close(graph);
1810 return error;
1813 static const struct got_error *
1814 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
1816 const struct got_error *error = NULL;
1817 struct got_reflist_entry *re;
1818 struct got_object_id *id2 = NULL;
1819 struct got_object_qid *parent_id;
1820 char *refs_str = NULL,
1821 *commit_msg = NULL, *commit_msg0;
1823 /*print commit*/
1824 SIMPLEQ_FOREACH(re, &header->refs, entry) {
1825 char *s;
1826 const char *name;
1827 struct got_tag_object *tag = NULL;
1828 int cmp;
1830 name = got_ref_get_name(re->ref);
1831 if (strcmp(name, GOT_REF_HEAD) == 0)
1832 continue;
1833 if (strncmp(name, "refs/", 5) == 0)
1834 name += 5;
1835 if (strncmp(name, "got/", 4) == 0)
1836 continue;
1837 if (strncmp(name, "heads/", 6) == 0)
1838 name += 6;
1839 if (strncmp(name, "remotes/", 8) == 0)
1840 name += 8;
1841 if (strncmp(name, "tags/", 5) == 0) {
1842 error = got_object_open_as_tag(&tag, header->repo,
1843 re->id);
1844 if (error) {
1845 if (error->code != GOT_ERR_OBJ_TYPE)
1846 continue;
1848 * Ref points at something other
1849 * than a tag.
1851 error = NULL;
1852 tag = NULL;
1855 cmp = got_object_id_cmp(tag ?
1856 got_object_tag_get_object_id(tag) : re->id, header->id);
1857 if (tag)
1858 got_object_tag_close(tag);
1859 if (cmp != 0)
1860 continue;
1861 s = refs_str;
1862 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1863 s ? ", " : "", name)) == -1) {
1864 error = got_error_from_errno("asprintf");
1865 free(s);
1866 return error;
1868 header->refs_str = strdup(refs_str);
1869 free(s);
1872 if (refs_str == NULL)
1873 header->refs_str = strdup("");
1874 free(refs_str);
1876 error = got_object_id_str(&header->commit_id, header->id);
1877 if (error)
1878 return error;
1880 error = got_object_id_str(&header->tree_id,
1881 got_object_commit_get_tree_id(header->commit));
1882 if (error)
1883 return error;
1885 if (gw_trans->action == GW_DIFF) {
1886 parent_id = SIMPLEQ_FIRST(
1887 got_object_commit_get_parent_ids(header->commit));
1888 if (parent_id != NULL) {
1889 id2 = got_object_id_dup(parent_id->id);
1890 free (parent_id);
1891 error = got_object_id_str(&header->parent_id, id2);
1892 if (error)
1893 return error;
1894 free(id2);
1895 } else
1896 header->parent_id = strdup("/dev/null");
1897 } else
1898 header->parent_id = strdup("");
1900 header->committer_time =
1901 got_object_commit_get_committer_time(header->commit);
1902 header->author = strdup(got_object_commit_get_author(header->commit));
1903 header->committer =
1904 strdup(got_object_commit_get_committer(header->commit));
1906 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
1907 if (error)
1908 return error;
1910 commit_msg = commit_msg0;
1911 while (*commit_msg == '\n')
1912 commit_msg++;
1914 header->commit_msg = strdup(commit_msg);
1915 free(commit_msg0);
1916 return error;
1919 static const struct got_error *
1920 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
1922 const struct got_error *error = NULL;
1923 char *in_repo_path = NULL;
1925 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1926 if (error)
1927 return error;
1929 SIMPLEQ_INIT(&header->refs);
1931 if (gw_trans->commit == NULL) {
1932 struct got_reference *head_ref;
1933 error = got_ref_open(&head_ref, header->repo,
1934 gw_trans->headref, 0);
1935 if (error)
1936 return error;
1938 error = got_ref_resolve(&header->id, header->repo, head_ref);
1939 got_ref_close(head_ref);
1940 if (error)
1941 return error;
1943 error = got_object_open_as_commit(&header->commit,
1944 header->repo, header->id);
1945 } else {
1946 struct got_reference *ref;
1947 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
1948 if (error == NULL) {
1949 int obj_type;
1950 error = got_ref_resolve(&header->id, header->repo, ref);
1951 got_ref_close(ref);
1952 if (error)
1953 return error;
1954 error = got_object_get_type(&obj_type, header->repo,
1955 header->id);
1956 if (error)
1957 return error;
1958 if (obj_type == GOT_OBJ_TYPE_TAG) {
1959 struct got_tag_object *tag;
1960 error = got_object_open_as_tag(&tag,
1961 header->repo, header->id);
1962 if (error)
1963 return error;
1964 if (got_object_tag_get_object_type(tag) !=
1965 GOT_OBJ_TYPE_COMMIT) {
1966 got_object_tag_close(tag);
1967 error = got_error(GOT_ERR_OBJ_TYPE);
1968 return error;
1970 free(header->id);
1971 header->id = got_object_id_dup(
1972 got_object_tag_get_object_id(tag));
1973 if (header->id == NULL)
1974 error = got_error_from_errno(
1975 "got_object_id_dup");
1976 got_object_tag_close(tag);
1977 if (error)
1978 return error;
1979 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1980 error = got_error(GOT_ERR_OBJ_TYPE);
1981 return error;
1983 error = got_object_open_as_commit(&header->commit,
1984 header->repo, header->id);
1985 if (error)
1986 return error;
1988 if (header->commit == NULL) {
1989 error = got_repo_match_object_id_prefix(&header->id,
1990 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
1991 header->repo);
1992 if (error)
1993 return error;
1995 error = got_repo_match_object_id_prefix(&header->id,
1996 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
1997 header->repo);
2000 error = got_repo_map_path(&in_repo_path, header->repo,
2001 gw_trans->repo_path, 1);
2002 if (error)
2003 return error;
2005 if (in_repo_path) {
2006 header->path = strdup(in_repo_path);
2008 free(in_repo_path);
2010 error = got_ref_list(&header->refs, header->repo, NULL,
2011 got_ref_cmp_by_name, NULL);
2012 if (error)
2013 return error;
2015 error = gw_get_commits(gw_trans, header, limit);
2016 return error;
2019 struct blame_line {
2020 int annotated;
2021 char *id_str;
2022 char *committer;
2023 char datebuf[11]; /* YYYY-MM-DD + NUL */
2026 struct gw_blame_cb_args {
2027 struct blame_line *lines;
2028 int nlines;
2029 int nlines_prec;
2030 int lineno_cur;
2031 off_t *line_offsets;
2032 FILE *f;
2033 struct got_repository *repo;
2034 struct gw_trans *gw_trans;
2035 struct buf *blamebuf;
2038 static const struct got_error *
2039 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2041 const struct got_error *err = NULL;
2042 struct gw_blame_cb_args *a = arg;
2043 struct blame_line *bline;
2044 char *line = NULL;
2045 size_t linesize = 0, newsize;
2046 struct got_commit_object *commit = NULL;
2047 off_t offset;
2048 struct tm tm;
2049 time_t committer_time;
2051 if (nlines != a->nlines ||
2052 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2053 return got_error(GOT_ERR_RANGE);
2055 if (lineno == -1)
2056 return NULL; /* no change in this commit */
2058 /* Annotate this line. */
2059 bline = &a->lines[lineno - 1];
2060 if (bline->annotated)
2061 return NULL;
2062 err = got_object_id_str(&bline->id_str, id);
2063 if (err)
2064 return err;
2066 err = got_object_open_as_commit(&commit, a->repo, id);
2067 if (err)
2068 goto done;
2070 bline->committer = strdup(got_object_commit_get_committer(commit));
2071 if (bline->committer == NULL) {
2072 err = got_error_from_errno("strdup");
2073 goto done;
2076 committer_time = got_object_commit_get_committer_time(commit);
2077 if (localtime_r(&committer_time, &tm) == NULL)
2078 return got_error_from_errno("localtime_r");
2079 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2080 &tm) >= sizeof(bline->datebuf)) {
2081 err = got_error(GOT_ERR_NO_SPACE);
2082 goto done;
2084 bline->annotated = 1;
2086 /* Print lines annotated so far. */
2087 bline = &a->lines[a->lineno_cur - 1];
2088 if (!bline->annotated)
2089 goto done;
2091 offset = a->line_offsets[a->lineno_cur - 1];
2092 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2093 err = got_error_from_errno("fseeko");
2094 goto done;
2097 while (bline->annotated) {
2098 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2099 *line_escape = NULL;
2100 size_t len;
2102 if (getline(&line, &linesize, a->f) == -1) {
2103 if (ferror(a->f))
2104 err = got_error_from_errno("getline");
2105 break;
2108 committer = bline->committer;
2109 smallerthan = strchr(committer, '<');
2110 if (smallerthan && smallerthan[1] != '\0')
2111 committer = smallerthan + 1;
2112 at = strchr(committer, '@');
2113 if (at)
2114 *at = '\0';
2115 len = strlen(committer);
2116 if (len >= 9)
2117 committer[8] = '\0';
2119 nl = strchr(line, '\n');
2120 if (nl)
2121 *nl = '\0';
2123 if (strcmp(line, "") != 0)
2124 line_escape = strdup(gw_html_escape(line));
2125 else
2126 line_escape = strdup("");
2128 asprintf(&blame_row, blame_line, a->nlines_prec,
2129 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2130 line_escape);
2131 a->lineno_cur++;
2132 err = buf_puts(&newsize, a->blamebuf, blame_row);
2133 if (err)
2134 return err;
2136 bline = &a->lines[a->lineno_cur - 1];
2137 free(line_escape);
2138 free(blame_row);
2140 done:
2141 if (commit)
2142 got_object_commit_close(commit);
2143 free(line);
2144 return err;
2147 static char*
2148 gw_get_file_blame(struct gw_trans *gw_trans)
2150 const struct got_error *error = NULL;
2151 struct got_repository *repo = NULL;
2152 struct got_object_id *obj_id = NULL;
2153 struct got_object_id *commit_id = NULL;
2154 struct got_blob_object *blob = NULL;
2155 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2156 *folder = NULL;
2157 struct gw_blame_cb_args bca;
2158 int i, obj_type;
2159 size_t filesize;
2161 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2162 if (error)
2163 goto done;
2165 if (gw_trans->repo_folder != NULL) {
2166 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2167 error = got_error_from_errno("asprintf");
2168 goto done;
2170 } else
2171 folder = strdup("");
2173 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2174 error = got_error_from_errno("asprintf");
2175 goto done;
2177 free(folder);
2179 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2180 if (error)
2181 goto done;
2183 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2184 GOT_OBJ_TYPE_COMMIT, 1, repo);
2185 if (error)
2186 goto done;
2188 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2189 if (error)
2190 goto done;
2192 if (obj_id == NULL) {
2193 error = got_error(GOT_ERR_NO_OBJ);
2194 goto done;
2197 error = got_object_get_type(&obj_type, repo, obj_id);
2198 if (error)
2199 goto done;
2201 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2202 error = got_error(GOT_ERR_OBJ_TYPE);
2203 goto done;
2206 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2207 if (error)
2208 goto done;
2210 error = buf_alloc(&bca.blamebuf, 0);
2211 if (error)
2212 goto done;
2214 bca.f = got_opentemp();
2215 if (bca.f == NULL) {
2216 error = got_error_from_errno("got_opentemp");
2217 goto done;
2219 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2220 &bca.line_offsets, bca.f, blob);
2221 if (error || bca.nlines == 0)
2222 goto done;
2224 /* Don't include \n at EOF in the blame line count. */
2225 if (bca.line_offsets[bca.nlines - 1] == filesize)
2226 bca.nlines--;
2228 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2229 if (bca.lines == NULL) {
2230 error = got_error_from_errno("calloc");
2231 goto done;
2233 bca.lineno_cur = 1;
2234 bca.nlines_prec = 0;
2235 i = bca.nlines;
2236 while (i > 0) {
2237 i /= 10;
2238 bca.nlines_prec++;
2240 bca.repo = repo;
2241 bca.gw_trans = gw_trans;
2243 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2244 NULL, NULL);
2245 if (buf_len(bca.blamebuf) > 0) {
2246 error = buf_putc(bca.blamebuf, '\0');
2247 blame_html = strdup(buf_get(bca.blamebuf));
2249 done:
2250 free(bca.blamebuf);
2251 free(in_repo_path);
2252 free(commit_id);
2253 free(obj_id);
2254 free(path);
2256 if (blob)
2257 error = got_object_blob_close(blob);
2258 if (repo)
2259 error = got_repo_close(repo);
2260 if (error)
2261 return NULL;
2262 if (bca.lines) {
2263 for (i = 0; i < bca.nlines; i++) {
2264 struct blame_line *bline = &bca.lines[i];
2265 free(bline->id_str);
2266 free(bline->committer);
2268 free(bca.lines);
2270 free(bca.line_offsets);
2271 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2272 error = got_error_from_errno("fclose");
2273 if (error)
2274 return NULL;
2275 else
2276 return blame_html;
2279 static char*
2280 gw_get_repo_tree(struct gw_trans *gw_trans)
2282 const struct got_error *error = NULL;
2283 struct got_repository *repo = NULL;
2284 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2285 struct got_tree_object *tree = NULL;
2286 struct buf *diffbuf = NULL;
2287 size_t newsize;
2288 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2289 *tree_row = NULL, *id_str;
2290 int nentries, i;
2292 error = buf_alloc(&diffbuf, 0);
2293 if (error)
2294 return NULL;
2296 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2297 if (error)
2298 goto done;
2300 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2301 if (error)
2302 goto done;
2304 if (gw_trans->repo_folder != NULL)
2305 path = strdup(gw_trans->repo_folder);
2306 else if (in_repo_path) {
2307 free(path);
2308 path = in_repo_path;
2311 if (gw_trans->commit == NULL) {
2312 struct got_reference *head_ref;
2313 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2314 if (error)
2315 goto done;
2317 error = got_ref_resolve(&commit_id, repo, head_ref);
2318 got_ref_close(head_ref);
2320 } else
2321 error = got_repo_match_object_id(&commit_id, NULL,
2322 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2323 if (error)
2324 goto done;
2326 error = got_object_id_str(&gw_trans->commit, commit_id);
2327 if (error)
2328 goto done;
2330 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2331 if (error)
2332 goto done;
2334 error = got_object_open_as_tree(&tree, repo, tree_id);
2335 if (error)
2336 goto done;
2338 nentries = got_object_tree_get_nentries(tree);
2340 for (i = 0; i < nentries; i++) {
2341 struct got_tree_entry *te;
2342 const char *modestr = "";
2343 char *id = NULL, *url_html = NULL;
2345 te = got_object_tree_get_entry(tree, i);
2347 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2348 if (error)
2349 goto done;
2351 if ((asprintf(&id, "%s", id_str)) == -1) {
2352 error = got_error_from_errno("asprintf");
2353 free(id_str);
2354 goto done;
2357 mode_t mode = got_tree_entry_get_mode(te);
2359 if (got_object_tree_entry_is_submodule(te))
2360 modestr = "$";
2361 else if (S_ISLNK(mode))
2362 modestr = "@";
2363 else if (S_ISDIR(mode))
2364 modestr = "/";
2365 else if (mode & S_IXUSR)
2366 modestr = "*";
2368 char *build_folder = NULL;
2369 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2370 if (gw_trans->repo_folder != NULL) {
2371 if ((asprintf(&build_folder, "%s/%s",
2372 gw_trans->repo_folder,
2373 got_tree_entry_get_name(te))) == -1) {
2374 error =
2375 got_error_from_errno("asprintf");
2376 goto done;
2378 } else {
2379 if (asprintf(&build_folder, "%s",
2380 got_tree_entry_get_name(te)) == -1)
2381 goto done;
2384 if ((asprintf(&url_html, folder_html,
2385 gw_trans->repo_name, gw_trans->action_name,
2386 gw_trans->commit, build_folder,
2387 got_tree_entry_get_name(te), modestr)) == -1) {
2388 error = got_error_from_errno("asprintf");
2389 goto done;
2391 } else {
2392 if (gw_trans->repo_folder != NULL) {
2393 if ((asprintf(&build_folder, "%s",
2394 gw_trans->repo_folder)) == -1) {
2395 error =
2396 got_error_from_errno("asprintf");
2397 goto done;
2399 } else
2400 build_folder = strdup("");
2402 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2403 "blame", gw_trans->commit,
2404 got_tree_entry_get_name(te), build_folder,
2405 got_tree_entry_get_name(te), modestr)) == -1) {
2406 error = got_error_from_errno("asprintf");
2407 goto done;
2410 free(build_folder);
2412 if (error)
2413 goto done;
2415 if ((asprintf(&tree_row, tree_line, url_html)) == -1) {
2416 error = got_error_from_errno("asprintf");
2417 goto done;
2419 error = buf_puts(&newsize, diffbuf, tree_row);
2420 if (error)
2421 goto done;
2423 free(id);
2424 free(id_str);
2425 free(url_html);
2426 free(tree_row);
2429 if (buf_len(diffbuf) > 0) {
2430 error = buf_putc(diffbuf, '\0');
2431 tree_html = strdup(buf_get(diffbuf));
2433 done:
2434 if (tree)
2435 got_object_tree_close(tree);
2436 if (repo)
2437 got_repo_close(repo);
2439 free(in_repo_path);
2440 free(tree_id);
2441 free(diffbuf);
2442 if (error)
2443 return NULL;
2444 else
2445 return tree_html;
2448 static char *
2449 gw_get_repo_heads(struct gw_trans *gw_trans)
2451 const struct got_error *error = NULL;
2452 struct got_repository *repo = NULL;
2453 struct got_reflist_head refs;
2454 struct got_reflist_entry *re;
2455 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2456 struct buf *diffbuf = NULL;
2457 size_t newsize;
2459 error = buf_alloc(&diffbuf, 0);
2460 if (error)
2461 return NULL;
2463 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2464 if (error)
2465 goto done;
2467 SIMPLEQ_INIT(&refs);
2468 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2469 NULL);
2470 if (error)
2471 goto done;
2473 SIMPLEQ_FOREACH(re, &refs, entry) {
2474 char *refname;
2476 refname = strdup(got_ref_get_name(re->ref));
2477 if (refname == NULL) {
2478 error = got_error_from_errno("got_ref_to_str");
2479 goto done;
2482 if (strncmp(refname, "refs/heads/", 11) != 0) {
2483 free(refname);
2484 continue;
2487 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2488 TM_DIFF);
2490 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2491 refname, gw_trans->repo_name, refname,
2492 gw_trans->repo_name, refname, gw_trans->repo_name,
2493 refname)) == -1) {
2494 error = got_error_from_errno("asprintf");
2495 goto done;
2498 if (strncmp(refname, "refs/heads/", 11) == 0)
2499 refname += 11;
2501 if ((asprintf(&head_row, heads_row, age, refname,
2502 head_navs_disp)) == -1) {
2503 error = got_error_from_errno("asprintf");
2504 goto done;
2507 error = buf_puts(&newsize, diffbuf, head_row);
2509 free(head_navs_disp);
2510 free(head_row);
2513 if (buf_len(diffbuf) > 0) {
2514 error = buf_putc(diffbuf, '\0');
2515 heads = strdup(buf_get(diffbuf));
2517 done:
2518 buf_free(diffbuf);
2519 got_ref_list_free(&refs);
2520 if (repo)
2521 got_repo_close(repo);
2522 if (error)
2523 return NULL;
2524 else
2525 return heads;
2528 static char *
2529 gw_get_got_link(struct gw_trans *gw_trans)
2531 char *link;
2533 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2534 gw_trans->gw_conf->got_logo)) == -1)
2535 return NULL;
2537 return link;
2540 static char *
2541 gw_get_site_link(struct gw_trans *gw_trans)
2543 char *link, *repo = "", *action = "";
2545 if (gw_trans->repo_name != NULL)
2546 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2547 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2548 return NULL;
2550 if (gw_trans->action_name != NULL)
2551 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2552 return NULL;
2554 if ((asprintf(&link, site_link, GOTWEB,
2555 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2556 return NULL;
2558 return link;
2561 static char *
2562 gw_colordiff_line(char *buf)
2564 const struct got_error *error = NULL;
2565 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2566 struct buf *diffbuf = NULL;
2567 size_t newsize;
2569 error = buf_alloc(&diffbuf, 0);
2570 if (error)
2571 return NULL;
2573 if (buf == NULL)
2574 return NULL;
2575 if (strncmp(buf, "-", 1) == 0)
2576 color = "diff_minus";
2577 if (strncmp(buf, "+", 1) == 0)
2578 color = "diff_plus";
2579 if (strncmp(buf, "@@", 2) == 0)
2580 color = "diff_chunk_header";
2581 if (strncmp(buf, "@@", 2) == 0)
2582 color = "diff_chunk_header";
2583 if (strncmp(buf, "commit +", 8) == 0)
2584 color = "diff_meta";
2585 if (strncmp(buf, "commit -", 8) == 0)
2586 color = "diff_meta";
2587 if (strncmp(buf, "blob +", 6) == 0)
2588 color = "diff_meta";
2589 if (strncmp(buf, "blob -", 6) == 0)
2590 color = "diff_meta";
2591 if (strncmp(buf, "file +", 6) == 0)
2592 color = "diff_meta";
2593 if (strncmp(buf, "file -", 6) == 0)
2594 color = "diff_meta";
2595 if (strncmp(buf, "from:", 5) == 0)
2596 color = "diff_author";
2597 if (strncmp(buf, "via:", 4) == 0)
2598 color = "diff_author";
2599 if (strncmp(buf, "date:", 5) == 0)
2600 color = "diff_date";
2602 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2603 return NULL;
2605 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2606 if (error)
2607 return NULL;
2609 error = buf_puts(&newsize, diffbuf, buf);
2610 if (error)
2611 return NULL;
2613 if (buf_len(diffbuf) > 0) {
2614 error = buf_putc(diffbuf, '\0');
2615 colorized_line = strdup(buf_get(diffbuf));
2618 free(diffbuf);
2619 free(div_diff_line_div);
2620 return colorized_line;
2623 static char *
2624 gw_html_escape(const char *html)
2626 char *escaped_str = NULL, *buf;
2627 char c[1];
2628 size_t sz, i, buff_sz = 2048;
2630 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2631 return NULL;
2633 if (html == NULL)
2634 return NULL;
2635 else
2636 if ((sz = strlen(html)) == 0)
2637 return NULL;
2639 /* only work with buff_sz */
2640 if (buff_sz < sz)
2641 sz = buff_sz;
2643 for (i = 0; i < sz; i++) {
2644 c[0] = html[i];
2645 switch (c[0]) {
2646 case ('>'):
2647 strcat(buf, "&gt;");
2648 break;
2649 case ('&'):
2650 strcat(buf, "&amp;");
2651 break;
2652 case ('<'):
2653 strcat(buf, "&lt;");
2654 break;
2655 case ('"'):
2656 strcat(buf, "&quot;");
2657 break;
2658 case ('\''):
2659 strcat(buf, "&apos;");
2660 break;
2661 case ('\n'):
2662 strcat(buf, "<br />");
2663 default:
2664 strcat(buf, &c[0]);
2665 break;
2668 asprintf(&escaped_str, "%s", buf);
2669 free(buf);
2670 return escaped_str;
2673 int
2674 main(int argc, char *argv[])
2676 const struct got_error *error = NULL;
2677 struct gw_trans *gw_trans;
2678 struct gw_dir *dir = NULL, *tdir;
2679 const char *page = "index";
2680 int gw_malloc = 1;
2681 enum kcgi_err kerr;
2683 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2684 errx(1, "malloc");
2686 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2687 errx(1, "malloc");
2689 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2690 errx(1, "malloc");
2692 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2693 errx(1, "malloc");
2695 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2696 if (kerr != KCGI_OK) {
2697 error = gw_kcgi_error(kerr);
2698 goto err;
2701 if ((gw_trans->gw_conf =
2702 malloc(sizeof(struct gotweb_conf))) == NULL) {
2703 gw_malloc = 0;
2704 error = got_error_from_errno("malloc");
2705 goto err;
2708 TAILQ_INIT(&gw_trans->gw_dirs);
2709 TAILQ_INIT(&gw_trans->gw_headers);
2711 gw_trans->page = 0;
2712 gw_trans->repos_total = 0;
2713 gw_trans->repo_path = NULL;
2714 gw_trans->commit = NULL;
2715 gw_trans->headref = strdup(GOT_REF_HEAD);
2716 gw_trans->mime = KMIME_TEXT_HTML;
2717 gw_trans->gw_tmpl->key = gw_templs;
2718 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2719 gw_trans->gw_tmpl->arg = gw_trans;
2720 gw_trans->gw_tmpl->cb = gw_template;
2721 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2723 err:
2724 if (error) {
2725 gw_trans->mime = KMIME_TEXT_PLAIN;
2726 gw_trans->action = GW_ERR;
2727 gw_display_index(gw_trans, error);
2728 goto done;
2731 error = gw_parse_querystring(gw_trans);
2732 if (error)
2733 goto err;
2735 error = gw_display_index(gw_trans, error);
2736 if (error)
2737 goto err;
2739 done:
2740 if (gw_malloc) {
2741 free(gw_trans->gw_conf->got_repos_path);
2742 free(gw_trans->gw_conf->got_www_path);
2743 free(gw_trans->gw_conf->got_site_name);
2744 free(gw_trans->gw_conf->got_site_owner);
2745 free(gw_trans->gw_conf->got_site_link);
2746 free(gw_trans->gw_conf->got_logo);
2747 free(gw_trans->gw_conf->got_logo_url);
2748 free(gw_trans->gw_conf);
2749 free(gw_trans->commit);
2750 free(gw_trans->repo_path);
2751 free(gw_trans->repo_name);
2752 free(gw_trans->repo_file);
2753 free(gw_trans->action_name);
2754 free(gw_trans->headref);
2756 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2757 free(dir->name);
2758 free(dir->description);
2759 free(dir->age);
2760 free(dir->url);
2761 free(dir->path);
2762 free(dir);
2767 khttp_free(gw_trans->gw_req);
2768 return EXIT_SUCCESS;