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 <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include <got_object.h>
34 #include <got_reference.h>
35 #include <got_repository.h>
36 #include <got_path.h>
37 #include <got_cancel.h>
38 #include <got_worktree.h>
39 #include <got_diff.h>
40 #include <got_commit_graph.h>
41 #include <got_blame.h>
42 #include <got_privsep.h>
43 #include <got_opentemp.h>
45 #include <kcgi.h>
46 #include <kcgihtml.h>
48 #include "buf.h"
49 #include "gotweb.h"
50 #include "gotweb_ui.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 struct gw_trans {
57 TAILQ_HEAD(headers, gw_header) gw_headers;
58 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 struct gw_dir *gw_dir;
60 struct gotweb_conf *gw_conf;
61 struct ktemplate *gw_tmpl;
62 struct khtmlreq *gw_html_req;
63 struct kreq *gw_req;
64 char *repo_name;
65 char *repo_path;
66 char *commit;
67 char *repo_file;
68 char *repo_folder;
69 char *action_name;
70 char *headref;
71 unsigned int action;
72 unsigned int page;
73 unsigned int repos_total;
74 enum kmime mime;
75 };
77 struct gw_header {
78 TAILQ_ENTRY(gw_header) entry;
79 struct got_repository *repo;
80 struct got_reflist_head refs;
81 struct got_commit_object *commit;
82 struct got_object_id *id;
83 char *path;
85 char *refs_str;
86 char *commit_id; /* id_str1 */
87 char *parent_id; /* id_str2 */
88 char *tree_id;
89 char *author;
90 char *committer;
91 char *commit_msg;
92 time_t committer_time;
93 };
95 struct gw_dir {
96 TAILQ_ENTRY(gw_dir) entry;
97 char *name;
98 char *owner;
99 char *description;
100 char *url;
101 char *age;
102 char *path;
103 };
105 enum gw_key {
106 KEY_ACTION,
107 KEY_COMMIT_ID,
108 KEY_FILE,
109 KEY_FOLDER,
110 KEY_HEADREF,
111 KEY_PAGE,
112 KEY_PATH,
113 KEY__ZMAX
114 };
116 enum gw_tmpl {
117 TEMPL_CONTENT,
118 TEMPL_HEAD,
119 TEMPL_HEADER,
120 TEMPL_SEARCH,
121 TEMPL_SITEPATH,
122 TEMPL_SITEOWNER,
123 TEMPL_TITLE,
124 TEMPL__MAX
125 };
127 enum gw_ref_tm {
128 TM_DIFF,
129 TM_LONG,
130 };
132 enum gw_tags {
133 TAGBRIEF,
134 TAGFULL,
135 };
137 static const char *const gw_templs[TEMPL__MAX] = {
138 "content",
139 "head",
140 "header",
141 "search",
142 "sitepath",
143 "siteowner",
144 "title",
145 };
147 static const struct kvalid gw_keys[KEY__ZMAX] = {
148 { kvalid_stringne, "action" },
149 { kvalid_stringne, "commit" },
150 { kvalid_stringne, "file" },
151 { kvalid_stringne, "folder" },
152 { kvalid_stringne, "headref" },
153 { kvalid_int, "page" },
154 { kvalid_stringne, "path" },
155 };
157 static struct gw_dir *gw_init_gw_dir(char *);
158 static struct gw_header *gw_init_header(void);
160 static char *gw_get_repo_description(struct gw_trans *,
161 char *);
162 static char *gw_get_repo_owner(struct gw_trans *,
163 char *);
164 static char *gw_get_time_str(time_t, int);
165 static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
166 char *, char *, int);
167 static char *gw_get_file_blame_blob(struct gw_trans *);
168 static char *gw_get_repo_tree(struct gw_trans *);
169 static char *gw_get_diff(struct gw_trans *,
170 struct gw_header *);
171 static char *gw_get_repo_tags(struct gw_trans *,
172 struct gw_header *, 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 static void gw_display_error(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_blob(struct gw_trans *);
213 static const struct got_error* gw_diff(struct gw_trans *);
214 static const struct got_error* gw_index(struct gw_trans *);
215 static const struct got_error* gw_commits(struct gw_trans *);
216 static const struct got_error* gw_briefs(struct gw_trans *);
217 static const struct got_error* gw_summary(struct gw_trans *);
218 static const struct got_error* gw_tree(struct gw_trans *);
219 static const struct got_error* gw_tag(struct gw_trans *);
221 struct gw_query_action {
222 unsigned int func_id;
223 const char *func_name;
224 const struct got_error *(*func_main)(struct gw_trans *);
225 char *template;
226 };
228 enum gw_query_actions {
229 GW_BLAME,
230 GW_BLOB,
231 GW_BRIEFS,
232 GW_COMMITS,
233 GW_DIFF,
234 GW_ERR,
235 GW_INDEX,
236 GW_SUMMARY,
237 GW_TAG,
238 GW_TREE,
239 };
241 static struct gw_query_action gw_query_funcs[] = {
242 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
243 { GW_BLOB, "blob", gw_blob, "gw_tmpl/blob.tmpl" },
244 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
245 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
246 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
247 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
248 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
249 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
250 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
251 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
252 };
254 static const struct got_error *
255 gw_kcgi_error(enum kcgi_err kerr)
257 if (kerr == KCGI_OK)
258 return NULL;
260 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
261 return got_error(GOT_ERR_CANCELLED);
263 if (kerr == KCGI_ENOMEM)
264 return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
266 if (kerr == KCGI_ENFILE)
267 return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
269 if (kerr == KCGI_EAGAIN)
270 return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
272 if (kerr == KCGI_FORM)
273 return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
275 return got_error_from_errno(kcgi_strerror(kerr));
278 static const struct got_error *
279 gw_apply_unveil(const char *repo_path, const char *repo_file)
281 const struct got_error *err;
283 if (repo_path && repo_file) {
284 char *full_path;
285 if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
286 return got_error_from_errno("asprintf unveil");
287 if (unveil(full_path, "r") != 0)
288 return got_error_from_errno2("unveil", full_path);
291 if (repo_path && unveil(repo_path, "r") != 0)
292 return got_error_from_errno2("unveil", repo_path);
294 if (unveil("/tmp", "rwc") != 0)
295 return got_error_from_errno2("unveil", "/tmp");
297 err = got_privsep_unveil_exec_helpers();
298 if (err != NULL)
299 return err;
301 if (unveil(NULL, NULL) != 0)
302 return got_error_from_errno("unveil");
304 return NULL;
307 static const struct got_error *
308 gw_blame(struct gw_trans *gw_trans)
310 const struct got_error *error = NULL;
311 struct gw_header *header = NULL;
312 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
313 enum kcgi_err kerr;
315 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
316 NULL) == -1)
317 return got_error_from_errno("pledge");
319 if ((header = gw_init_header()) == NULL)
320 return got_error_from_errno("malloc");
322 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
323 if (error)
324 goto done;
326 error = gw_get_header(gw_trans, header, 1);
327 if (error)
328 goto done;
330 blame_html = gw_get_file_blame_blob(gw_trans);
332 if (blame_html == NULL) {
333 blame_html = strdup("");
334 if (blame_html == NULL) {
335 error = got_error_from_errno("strdup");
336 goto done;
340 if (asprintf(&blame_html_disp, blame_header,
341 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
342 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
343 blame_html) == -1) {
344 error = got_error_from_errno("asprintf");
345 goto done;
348 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
349 error = got_error_from_errno("asprintf");
350 goto done;
353 kerr = khttp_puts(gw_trans->gw_req, blame);
354 if (kerr != KCGI_OK)
355 error = gw_kcgi_error(kerr);
356 done:
357 got_ref_list_free(&header->refs);
358 gw_free_headers(header);
359 free(blame_html_disp);
360 free(blame_html);
361 free(blame);
362 return error;
365 static const struct got_error *
366 gw_blob(struct gw_trans *gw_trans)
368 const struct got_error *error = NULL;
369 struct gw_header *header = NULL;
370 char *blob = NULL;
371 enum kcgi_err kerr;
373 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
374 NULL) == -1)
375 return got_error_from_errno("pledge");
377 if ((header = gw_init_header()) == NULL)
378 return got_error_from_errno("malloc");
380 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
381 if (error)
382 goto done;
384 error = gw_get_header(gw_trans, header, 1);
385 if (error)
386 goto done;
388 blob = gw_get_file_blame_blob(gw_trans);
390 if (blob == NULL) {
391 blob = strdup("");
392 if (blob == NULL) {
393 error = got_error_from_errno("strdup");
394 goto done;
398 kerr = khttp_puts(gw_trans->gw_req, blob);
399 if (kerr != KCGI_OK)
400 error = gw_kcgi_error(kerr);
401 done:
402 got_ref_list_free(&header->refs);
403 gw_free_headers(header);
404 free(blob);
405 return error;
408 static const struct got_error *
409 gw_diff(struct gw_trans *gw_trans)
411 const struct got_error *error = NULL;
412 struct gw_header *header = NULL;
413 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
414 enum kcgi_err kerr;
416 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
417 NULL) == -1)
418 return got_error_from_errno("pledge");
420 if ((header = gw_init_header()) == NULL)
421 return got_error_from_errno("malloc");
423 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
424 if (error)
425 goto done;
427 error = gw_get_header(gw_trans, header, 1);
428 if (error)
429 goto done;
431 diff_html = gw_get_diff(gw_trans, header);
433 if (diff_html == NULL) {
434 diff_html = strdup("");
435 if (diff_html == NULL) {
436 error = got_error_from_errno("strdup");
437 goto done;
441 if (asprintf(&diff_html_disp, diff_header,
442 gw_gen_diff_header(header->parent_id, header->commit_id),
443 gw_gen_commit_header(header->commit_id, header->refs_str),
444 gw_gen_tree_header(header->tree_id),
445 gw_gen_author_header(header->author),
446 gw_gen_committer_header(header->committer),
447 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
448 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
449 diff_html) == -1) {
450 error = got_error_from_errno("asprintf");
451 goto done;
454 if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
455 error = got_error_from_errno("asprintf");
456 goto done;
459 kerr = khttp_puts(gw_trans->gw_req, diff);
460 if (kerr != KCGI_OK)
461 error = gw_kcgi_error(kerr);
462 done:
463 got_ref_list_free(&header->refs);
464 gw_free_headers(header);
465 free(diff_html_disp);
466 free(diff_html);
467 free(diff);
468 return error;
471 static const struct got_error *
472 gw_index(struct gw_trans *gw_trans)
474 const struct got_error *error = NULL;
475 struct gw_dir *gw_dir = NULL;
476 char *html, *navs, *next, *prev;
477 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
478 enum kcgi_err kerr;
480 if (pledge("stdio rpath proc exec sendfd unveil",
481 NULL) == -1) {
482 error = got_error_from_errno("pledge");
483 return error;
486 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
487 if (error)
488 return error;
490 error = gw_load_got_paths(gw_trans);
491 if (error)
492 return error;
494 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
495 if (kerr != KCGI_OK)
496 return gw_kcgi_error(kerr);
498 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
499 if (asprintf(&html, index_projects_empty,
500 gw_trans->gw_conf->got_repos_path) == -1)
501 return got_error_from_errno("asprintf");
502 kerr = khttp_puts(gw_trans->gw_req, html);
503 if (kerr != KCGI_OK)
504 error = gw_kcgi_error(kerr);
505 free(html);
506 return error;
509 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
510 dir_c++;
512 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
513 if (gw_trans->page > 0 && (gw_trans->page *
514 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
515 prev_disp++;
516 continue;
519 prev_disp++;
521 if (error)
522 return error;
523 if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
524 gw_dir->name, gw_dir->name) == -1)
525 return got_error_from_errno("asprintf");
527 if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
528 gw_dir->description, gw_dir->owner, gw_dir->age,
529 navs) == -1)
530 return got_error_from_errno("asprintf");
532 kerr = khttp_puts(gw_trans->gw_req, html);
533 free(navs);
534 free(html);
535 if (kerr != KCGI_OK)
536 return gw_kcgi_error(kerr);
538 if (gw_trans->gw_conf->got_max_repos_display == 0)
539 continue;
541 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
542 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
543 if (kerr != KCGI_OK)
544 return gw_kcgi_error(kerr);
545 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
546 (gw_trans->page > 0) &&
547 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
548 prev_disp == gw_trans->repos_total)) {
549 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
550 if (kerr != KCGI_OK)
551 return gw_kcgi_error(kerr);
554 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
555 (gw_trans->page > 0) &&
556 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
557 prev_disp == gw_trans->repos_total)) {
558 if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
559 return got_error_from_errno("asprintf");
560 kerr = khttp_puts(gw_trans->gw_req, prev);
561 free(prev);
562 if (kerr != KCGI_OK)
563 return gw_kcgi_error(kerr);
566 kerr = khttp_puts(gw_trans->gw_req, div_end);
567 if (kerr != KCGI_OK)
568 return gw_kcgi_error(kerr);
570 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
571 next_disp == gw_trans->gw_conf->got_max_repos_display &&
572 dir_c != (gw_trans->page + 1) *
573 gw_trans->gw_conf->got_max_repos_display) {
574 if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
575 return got_error_from_errno("calloc");
576 kerr = khttp_puts(gw_trans->gw_req, next);
577 free(next);
578 if (kerr != KCGI_OK)
579 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);
583 next_disp = 0;
584 break;
587 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
588 (gw_trans->page > 0) &&
589 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
590 prev_disp == gw_trans->repos_total)) {
591 kerr = khttp_puts(gw_trans->gw_req, div_end);
592 if (kerr != KCGI_OK)
593 return gw_kcgi_error(kerr);
596 next_disp++;
598 return error;
601 static const struct got_error *
602 gw_commits(struct gw_trans *gw_trans)
604 const struct got_error *error = NULL;
605 char *commits_html, *commits_navs_html;
606 struct gw_header *header = NULL, *n_header = NULL;
607 enum kcgi_err kerr;
609 if ((header = gw_init_header()) == NULL)
610 return got_error_from_errno("malloc");
612 if (pledge("stdio rpath proc exec sendfd unveil",
613 NULL) == -1) {
614 error = got_error_from_errno("pledge");
615 goto done;
618 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
619 if (error)
620 goto done;
622 error = gw_get_header(gw_trans, header,
623 gw_trans->gw_conf->got_max_commits_display);
624 if (error)
625 goto done;
627 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
628 if (kerr != KCGI_OK) {
629 error = gw_kcgi_error(kerr);
630 goto done;
632 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
633 if (asprintf(&commits_navs_html, commits_navs,
634 gw_trans->repo_name, n_header->commit_id,
635 gw_trans->repo_name, n_header->commit_id,
636 gw_trans->repo_name, n_header->commit_id) == -1) {
637 error = got_error_from_errno("asprintf");
638 goto done;
641 if (asprintf(&commits_html, commits_line,
642 gw_gen_commit_header(n_header->commit_id,
643 n_header->refs_str),
644 gw_gen_author_header(n_header->author),
645 gw_gen_committer_header(n_header->committer),
646 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
647 TM_LONG)), gw_html_escape(n_header->commit_msg),
648 commits_navs_html) == -1) {
649 error = got_error_from_errno("asprintf");
650 goto done;
652 kerr = khttp_puts(gw_trans->gw_req, commits_html);
653 if (kerr != KCGI_OK) {
654 error = gw_kcgi_error(kerr);
655 goto done;
658 kerr = khttp_puts(gw_trans->gw_req, div_end);
659 if (kerr != KCGI_OK)
660 error = gw_kcgi_error(kerr);
661 done:
662 got_ref_list_free(&header->refs);
663 gw_free_headers(header);
664 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
665 gw_free_headers(n_header);
666 return error;
669 static const struct got_error *
670 gw_briefs(struct gw_trans *gw_trans)
672 const struct got_error *error = NULL;
673 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
674 struct gw_header *header = NULL, *n_header = NULL;
675 enum kcgi_err kerr;
677 if ((header = gw_init_header()) == NULL)
678 return got_error_from_errno("malloc");
680 if (pledge("stdio rpath proc exec sendfd unveil",
681 NULL) == -1) {
682 error = got_error_from_errno("pledge");
683 goto done;
686 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
687 if (error)
688 goto done;
690 if (gw_trans->action == GW_SUMMARY)
691 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
692 else
693 error = gw_get_header(gw_trans, header,
694 gw_trans->gw_conf->got_max_commits_display);
695 if (error)
696 goto done;
698 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
699 if (kerr != KCGI_OK) {
700 error = gw_kcgi_error(kerr);
701 goto done;
704 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
705 if (asprintf(&briefs_navs_html, briefs_navs,
706 gw_trans->repo_name, n_header->commit_id,
707 gw_trans->repo_name, n_header->commit_id,
708 gw_trans->repo_name, n_header->commit_id) == -1) {
709 error = got_error_from_errno("asprintf");
710 goto done;
712 newline = strchr(n_header->commit_msg, '\n');
713 if (newline)
714 *newline = '\0';
715 if (asprintf(&briefs_html, briefs_line,
716 gw_get_time_str(n_header->committer_time, TM_DIFF),
717 n_header->author, gw_html_escape(n_header->commit_msg),
718 briefs_navs_html) == -1) {
719 error = got_error_from_errno("asprintf");
720 goto done;
722 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
723 if (kerr != KCGI_OK) {
724 error = gw_kcgi_error(kerr);
725 goto done;
728 kerr = khttp_puts(gw_trans->gw_req, div_end);
729 if (kerr != KCGI_OK)
730 error = gw_kcgi_error(kerr);
731 done:
732 got_ref_list_free(&header->refs);
733 gw_free_headers(header);
734 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
735 gw_free_headers(n_header);
736 return error;
739 static const struct got_error *
740 gw_summary(struct gw_trans *gw_trans)
742 const struct got_error *error = NULL;
743 char *description_html = NULL, *repo_owner_html = NULL;
744 char *age = NULL, *repo_age_html = NULL, *cloneurl_html = NULL;
745 char *tags = NULL, *tags_html = NULL;
746 char *heads = NULL, *heads_html = NULL;
747 enum kcgi_err kerr;
749 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
750 return got_error_from_errno("pledge");
752 /* unveil is applied with gw_briefs below */
754 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
755 if (kerr != KCGI_OK)
756 return gw_kcgi_error(kerr);
758 if (gw_trans->gw_conf->got_show_repo_description) {
759 if (gw_trans->gw_dir->description != NULL &&
760 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
761 if (asprintf(&description_html, description,
762 gw_trans->gw_dir->description) == -1) {
763 error = got_error_from_errno("asprintf");
764 goto done;
767 kerr = khttp_puts(gw_trans->gw_req, description_html);
768 if (kerr != KCGI_OK) {
769 error = gw_kcgi_error(kerr);
770 goto done;
775 if (gw_trans->gw_conf->got_show_repo_owner) {
776 if (gw_trans->gw_dir->owner != NULL &&
777 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
778 if (asprintf(&repo_owner_html, repo_owner,
779 gw_trans->gw_dir->owner) == -1) {
780 error = got_error_from_errno("asprintf");
781 goto done;
784 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
785 if (kerr != KCGI_OK) {
786 error = gw_kcgi_error(kerr);
787 goto done;
792 if (gw_trans->gw_conf->got_show_repo_age) {
793 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
794 "refs/heads", TM_LONG);
795 if (error)
796 goto done;
797 if (strcmp(age, "") != 0) {
798 if (asprintf(&repo_age_html, last_change, age) == -1) {
799 error = got_error_from_errno("asprintf");
800 goto done;
803 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
804 if (kerr != KCGI_OK) {
805 error = gw_kcgi_error(kerr);
806 goto done;
811 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
812 if (gw_trans->gw_dir->url != NULL &&
813 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
814 if (asprintf(&cloneurl_html, cloneurl,
815 gw_trans->gw_dir->url) == -1) {
816 error = got_error_from_errno("asprintf");
817 goto done;
820 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
821 if (kerr != KCGI_OK) {
822 error = gw_kcgi_error(kerr);
823 goto done;
827 kerr = khttp_puts(gw_trans->gw_req, div_end);
828 if (kerr != KCGI_OK) {
829 error = gw_kcgi_error(kerr);
830 goto done;
833 error = gw_briefs(gw_trans);
834 if (error)
835 goto done;
837 tags = gw_get_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP, TAGBRIEF);
838 heads = gw_get_repo_heads(gw_trans);
840 if (tags != NULL && strcmp(tags, "") != 0) {
841 if (asprintf(&tags_html, summary_tags, tags) == -1) {
842 error = got_error_from_errno("asprintf");
843 goto done;
845 kerr = khttp_puts(gw_trans->gw_req, tags_html);
846 if (kerr != KCGI_OK) {
847 error = gw_kcgi_error(kerr);
848 goto done;
852 if (heads != NULL && strcmp(heads, "") != 0) {
853 if (asprintf(&heads_html, summary_heads, heads) == -1) {
854 error = got_error_from_errno("asprintf");
855 goto done;
857 kerr = khttp_puts(gw_trans->gw_req, heads_html);
858 if (kerr != KCGI_OK) {
859 error = gw_kcgi_error(kerr);
860 goto done;
863 done:
864 free(description_html);
865 free(repo_owner_html);
866 free(age);
867 free(repo_age_html);
868 free(cloneurl_html);
869 free(tags);
870 free(tags_html);
871 free(heads);
872 free(heads_html);
873 return error;
876 static const struct got_error *
877 gw_tree(struct gw_trans *gw_trans)
879 const struct got_error *error = NULL;
880 struct gw_header *header = NULL;
881 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
882 enum kcgi_err kerr;
884 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
885 return got_error_from_errno("pledge");
887 if ((header = gw_init_header()) == NULL)
888 return got_error_from_errno("malloc");
890 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
891 if (error)
892 goto done;
894 error = gw_get_header(gw_trans, header, 1);
895 if (error)
896 goto done;
898 tree_html = gw_get_repo_tree(gw_trans);
900 if (tree_html == NULL) {
901 tree_html = strdup("");
902 if (tree_html == NULL) {
903 error = got_error_from_errno("strdup");
904 goto done;
908 if (asprintf(&tree_html_disp, tree_header,
909 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
910 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
911 tree_html) == -1) {
912 error = got_error_from_errno("asprintf");
913 goto done;
916 if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
917 error = got_error_from_errno("asprintf");
918 goto done;
921 kerr = khttp_puts(gw_trans->gw_req, tree);
922 if (kerr != KCGI_OK)
923 error = gw_kcgi_error(kerr);
924 done:
925 got_ref_list_free(&header->refs);
926 gw_free_headers(header);
927 free(tree_html_disp);
928 free(tree_html);
929 free(tree);
930 return error;
933 static const struct got_error *
934 gw_tag(struct gw_trans *gw_trans)
936 const struct got_error *error = NULL;
937 struct gw_header *header = NULL;
938 char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
939 enum kcgi_err kerr;
941 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
942 return got_error_from_errno("pledge");
944 if ((header = gw_init_header()) == NULL)
945 return got_error_from_errno("malloc");
947 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
948 if (error)
949 goto done;
951 error = gw_get_header(gw_trans, header, 1);
952 if (error)
953 goto done;
955 tag_html = gw_get_repo_tags(gw_trans, header, 1, TAGFULL);
956 if (tag_html == NULL) {
957 tag_html = strdup("");
958 if (tag_html == NULL) {
959 error = got_error_from_errno("strdup");
960 goto done;
964 if (asprintf(&tag_html_disp, tag_header,
965 gw_gen_commit_header(header->commit_id, header->refs_str),
966 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
967 tag_html) == -1) {
968 error = got_error_from_errno("asprintf");
969 goto done;
972 if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
973 error = got_error_from_errno("asprintf");
974 goto done;
977 kerr = khttp_puts(gw_trans->gw_req, tag);
978 if (kerr != KCGI_OK)
979 error = gw_kcgi_error(kerr);
980 done:
981 got_ref_list_free(&header->refs);
982 gw_free_headers(header);
983 free(tag_html_disp);
984 free(tag_html);
985 free(tag);
986 return error;
989 static const struct got_error *
990 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
992 const struct got_error *error = NULL;
993 DIR *dt;
994 char *dir_test;
995 int opened = 0;
997 if (asprintf(&dir_test, "%s/%s/%s",
998 gw_trans->gw_conf->got_repos_path, gw_dir->name,
999 GOTWEB_GIT_DIR) == -1)
1000 return got_error_from_errno("asprintf");
1002 dt = opendir(dir_test);
1003 if (dt == NULL) {
1004 free(dir_test);
1005 } else {
1006 gw_dir->path = strdup(dir_test);
1007 opened = 1;
1008 goto done;
1011 if (asprintf(&dir_test, "%s/%s/%s",
1012 gw_trans->gw_conf->got_repos_path, gw_dir->name,
1013 GOTWEB_GOT_DIR) == -1)
1014 return got_error_from_errno("asprintf");
1016 dt = opendir(dir_test);
1017 if (dt == NULL)
1018 free(dir_test);
1019 else {
1020 opened = 1;
1021 error = got_error(GOT_ERR_NOT_GIT_REPO);
1022 goto errored;
1025 if (asprintf(&dir_test, "%s/%s",
1026 gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1027 return got_error_from_errno("asprintf");
1029 gw_dir->path = strdup(dir_test);
1031 done:
1032 gw_dir->description = gw_get_repo_description(gw_trans,
1033 gw_dir->path);
1034 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
1035 error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1036 "refs/heads", TM_DIFF);
1037 if (error)
1038 goto errored;
1039 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
1041 errored:
1042 free(dir_test);
1043 if (opened)
1044 closedir(dt);
1045 return error;
1048 static const struct got_error *
1049 gw_load_got_paths(struct gw_trans *gw_trans)
1051 const struct got_error *error = NULL;
1052 DIR *d;
1053 struct dirent **sd_dent;
1054 struct gw_dir *gw_dir;
1055 struct stat st;
1056 unsigned int d_cnt, d_i;
1058 d = opendir(gw_trans->gw_conf->got_repos_path);
1059 if (d == NULL) {
1060 error = got_error_from_errno2("opendir",
1061 gw_trans->gw_conf->got_repos_path);
1062 return error;
1065 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1066 alphasort);
1067 if (d_cnt == -1) {
1068 error = got_error_from_errno2("scandir",
1069 gw_trans->gw_conf->got_repos_path);
1070 return error;
1073 for (d_i = 0; d_i < d_cnt; d_i++) {
1074 if (gw_trans->gw_conf->got_max_repos > 0 &&
1075 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1076 break; /* account for parent and self */
1078 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1079 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1080 continue;
1082 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1083 return got_error_from_errno("gw_dir malloc");
1085 error = gw_load_got_path(gw_trans, gw_dir);
1086 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1087 continue;
1088 else if (error)
1089 return error;
1091 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1092 !got_path_dir_is_empty(gw_dir->path)) {
1093 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1094 entry);
1095 gw_trans->repos_total++;
1099 closedir(d);
1100 return error;
1103 static const struct got_error *
1104 gw_parse_querystring(struct gw_trans *gw_trans)
1106 const struct got_error *error = NULL;
1107 struct kpair *p;
1108 struct gw_query_action *action = NULL;
1109 unsigned int i;
1111 if (gw_trans->gw_req->fieldnmap[0]) {
1112 error = got_error_from_errno("bad parse");
1113 return error;
1114 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1115 /* define gw_trans->repo_path */
1116 if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1117 return got_error_from_errno("asprintf");
1119 if (asprintf(&gw_trans->repo_path, "%s/%s",
1120 gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1121 return got_error_from_errno("asprintf");
1123 /* get action and set function */
1124 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1125 for (i = 0; i < nitems(gw_query_funcs); i++) {
1126 action = &gw_query_funcs[i];
1127 if (action->func_name == NULL)
1128 continue;
1130 if (strcmp(action->func_name,
1131 p->parsed.s) == 0) {
1132 gw_trans->action = i;
1133 if (asprintf(&gw_trans->action_name,
1134 "%s", action->func_name) == -1)
1135 return
1136 got_error_from_errno(
1137 "asprintf");
1139 break;
1142 action = NULL;
1145 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1146 if (asprintf(&gw_trans->commit, "%s",
1147 p->parsed.s) == -1)
1148 return got_error_from_errno("asprintf");
1150 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1151 if (asprintf(&gw_trans->repo_file, "%s",
1152 p->parsed.s) == -1)
1153 return got_error_from_errno("asprintf");
1155 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1156 if (asprintf(&gw_trans->repo_folder, "%s",
1157 p->parsed.s) == -1)
1158 return got_error_from_errno("asprintf");
1160 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1161 if (asprintf(&gw_trans->headref, "%s",
1162 p->parsed.s) == -1)
1163 return got_error_from_errno("asprintf");
1165 if (action == NULL) {
1166 error = got_error_from_errno("invalid action");
1167 return error;
1169 if ((gw_trans->gw_dir =
1170 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1171 return got_error_from_errno("gw_dir malloc");
1173 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1174 if (error)
1175 return error;
1176 } else
1177 gw_trans->action = GW_INDEX;
1179 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1180 gw_trans->page = p->parsed.i;
1182 if (gw_trans->action == GW_BLOB)
1183 gw_trans->mime = KMIME_TEXT_PLAIN;
1185 return error;
1188 static struct gw_dir *
1189 gw_init_gw_dir(char *dir)
1191 struct gw_dir *gw_dir;
1193 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1194 return NULL;
1196 if (asprintf(&gw_dir->name, "%s", dir) == -1)
1197 return NULL;
1199 return gw_dir;
1202 static const struct got_error *
1203 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1205 enum kcgi_err kerr;
1207 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1208 if (kerr != KCGI_OK)
1209 return gw_kcgi_error(kerr);
1210 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1211 khttps[code]);
1212 if (kerr != KCGI_OK)
1213 return gw_kcgi_error(kerr);
1214 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1215 kmimetypes[mime]);
1216 if (kerr != KCGI_OK)
1217 return gw_kcgi_error(kerr);
1218 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1219 if (kerr != KCGI_OK)
1220 return gw_kcgi_error(kerr);
1221 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1222 if (kerr != KCGI_OK)
1223 return gw_kcgi_error(kerr);
1224 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1225 if (kerr != KCGI_OK)
1226 return gw_kcgi_error(kerr);
1228 kerr = khttp_body(gw_trans->gw_req);
1229 return gw_kcgi_error(kerr);
1232 static const struct got_error *
1233 gw_display_index(struct gw_trans *gw_trans)
1235 const struct got_error *error;
1236 enum kcgi_err kerr;
1238 error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1239 if (error)
1240 return error;
1242 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1243 if (kerr)
1244 return gw_kcgi_error(kerr);
1246 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1247 gw_query_funcs[gw_trans->action].template);
1248 if (kerr != KCGI_OK) {
1249 khtml_close(gw_trans->gw_html_req);
1250 return gw_kcgi_error(kerr);
1253 return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1256 static void
1257 gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1259 if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1260 return;
1262 if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1263 return;
1265 khttp_puts(gw_trans->gw_req, err->msg);
1266 khtml_close(gw_trans->gw_html_req);
1269 static int
1270 gw_template(size_t key, void *arg)
1272 const struct got_error *error = NULL;
1273 struct gw_trans *gw_trans = arg;
1274 char *gw_got_link, *gw_site_link;
1275 char *site_owner_name, *site_owner_name_h;
1277 switch (key) {
1278 case (TEMPL_HEAD):
1279 khttp_puts(gw_trans->gw_req, head);
1280 break;
1281 case(TEMPL_HEADER):
1282 gw_got_link = gw_get_got_link(gw_trans);
1283 if (gw_got_link != NULL)
1284 khttp_puts(gw_trans->gw_req, gw_got_link);
1286 free(gw_got_link);
1287 break;
1288 case (TEMPL_SITEPATH):
1289 gw_site_link = gw_get_site_link(gw_trans);
1290 if (gw_site_link != NULL)
1291 khttp_puts(gw_trans->gw_req, gw_site_link);
1293 free(gw_site_link);
1294 break;
1295 case(TEMPL_TITLE):
1296 if (gw_trans->gw_conf->got_site_name != NULL)
1297 khtml_puts(gw_trans->gw_html_req,
1298 gw_trans->gw_conf->got_site_name);
1300 break;
1301 case (TEMPL_SEARCH):
1302 khttp_puts(gw_trans->gw_req, search);
1303 break;
1304 case(TEMPL_SITEOWNER):
1305 if (gw_trans->gw_conf->got_site_owner != NULL &&
1306 gw_trans->gw_conf->got_show_site_owner) {
1307 site_owner_name =
1308 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1309 if (asprintf(&site_owner_name_h, site_owner,
1310 site_owner_name) == -1)
1311 return 0;
1313 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1314 free(site_owner_name);
1315 free(site_owner_name_h);
1317 break;
1318 case(TEMPL_CONTENT):
1319 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1320 if (error)
1321 khttp_puts(gw_trans->gw_req, error->msg);
1322 break;
1323 default:
1324 return 0;
1326 return 1;
1329 static char *
1330 gw_gen_commit_header(char *str1, char *str2)
1332 char *return_html = NULL, *ref_str = NULL;
1334 if (strcmp(str2, "") != 0) {
1335 if (asprintf(&ref_str, "(%s)", str2) == -1) {
1336 return_html = strdup("");
1337 return return_html;
1339 } else
1340 ref_str = strdup("");
1343 if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1344 return_html = strdup("");
1346 free(ref_str);
1347 return return_html;
1350 static char *
1351 gw_gen_diff_header(char *str1, char *str2)
1353 char *return_html = NULL;
1355 if (asprintf(&return_html, header_diff_html, str1, str2) == -1)
1356 return_html = strdup("");
1358 return return_html;
1361 static char *
1362 gw_gen_author_header(char *str)
1364 char *return_html = NULL;
1366 if (asprintf(&return_html, header_author_html, str) == -1)
1367 return_html = strdup("");
1369 return return_html;
1372 static char *
1373 gw_gen_committer_header(char *str)
1375 char *return_html = NULL;
1377 if (asprintf(&return_html, header_committer_html, str) == -1)
1378 return_html = strdup("");
1380 return return_html;
1383 static char *
1384 gw_gen_age_header(char *str)
1386 char *return_html = NULL;
1388 if (asprintf(&return_html, header_age_html, str) == -1)
1389 return_html = strdup("");
1391 return return_html;
1394 static char *
1395 gw_gen_commit_msg_header(char *str)
1397 char *return_html = NULL;
1399 if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1400 return_html = strdup("");
1402 return return_html;
1405 static char *
1406 gw_gen_tree_header(char *str)
1408 char *return_html = NULL;
1410 if (asprintf(&return_html, header_tree_html, str) == -1)
1411 return_html = strdup("");
1413 return return_html;
1416 static char *
1417 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1419 FILE *f;
1420 char *description = NULL, *d_file = NULL;
1421 unsigned int len;
1423 if (gw_trans->gw_conf->got_show_repo_description == 0)
1424 goto err;
1426 if (asprintf(&d_file, "%s/description", dir) == -1)
1427 goto err;
1429 if ((f = fopen(d_file, "r")) == NULL)
1430 goto err;
1432 fseek(f, 0, SEEK_END);
1433 len = ftell(f) + 1;
1434 fseek(f, 0, SEEK_SET);
1435 if ((description = calloc(len, sizeof(char *))) == NULL)
1436 goto err;
1438 fread(description, 1, len, f);
1439 fclose(f);
1440 free(d_file);
1441 return description;
1442 err:
1443 return strdup("");
1446 static char *
1447 gw_get_time_str(time_t committer_time, int ref_tm)
1449 struct tm tm;
1450 time_t diff_time;
1451 char *years = "years ago", *months = "months ago";
1452 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1453 char *minutes = "minutes ago", *seconds = "seconds ago";
1454 char *now = "right now";
1455 char *repo_age, *s;
1456 char datebuf[29];
1458 switch (ref_tm) {
1459 case TM_DIFF:
1460 diff_time = time(NULL) - committer_time;
1461 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1462 if (asprintf(&repo_age, "%lld %s",
1463 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1464 return NULL;
1465 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1466 if (asprintf(&repo_age, "%lld %s",
1467 (diff_time / 60 / 60 / 24 / (365 / 12)),
1468 months) == -1)
1469 return NULL;
1470 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1471 if (asprintf(&repo_age, "%lld %s",
1472 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1473 return NULL;
1474 } else if (diff_time > 60 * 60 * 24 * 2) {
1475 if (asprintf(&repo_age, "%lld %s",
1476 (diff_time / 60 / 60 / 24), days) == -1)
1477 return NULL;
1478 } else if (diff_time > 60 * 60 * 2) {
1479 if (asprintf(&repo_age, "%lld %s",
1480 (diff_time / 60 / 60), hours) == -1)
1481 return NULL;
1482 } else if (diff_time > 60 * 2) {
1483 if (asprintf(&repo_age, "%lld %s", (diff_time / 60),
1484 minutes) == -1)
1485 return NULL;
1486 } else if (diff_time > 2) {
1487 if (asprintf(&repo_age, "%lld %s", diff_time,
1488 seconds) == -1)
1489 return NULL;
1490 } else {
1491 if (asprintf(&repo_age, "%s", now) == -1)
1492 return NULL;
1494 break;
1495 case TM_LONG:
1496 if (gmtime_r(&committer_time, &tm) == NULL)
1497 return NULL;
1499 s = asctime_r(&tm, datebuf);
1500 if (s == NULL)
1501 return NULL;
1503 if (asprintf(&repo_age, "%s UTC", datebuf) == -1)
1504 return NULL;
1505 break;
1507 return repo_age;
1510 static const struct got_error *
1511 gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
1512 char *repo_ref, int ref_tm)
1514 const struct got_error *error = NULL;
1515 struct got_object_id *id = NULL;
1516 struct got_repository *repo = NULL;
1517 struct got_commit_object *commit = NULL;
1518 struct got_reflist_head refs;
1519 struct got_reflist_entry *re;
1520 struct got_reference *head_ref;
1521 int is_head = 0;
1522 time_t committer_time = 0, cmp_time = 0;
1523 const char *refname;
1525 *repo_age = NULL;
1526 SIMPLEQ_INIT(&refs);
1528 if (repo_ref == NULL)
1529 return NULL;
1531 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1532 is_head = 1;
1534 if (gw_trans->gw_conf->got_show_repo_age == 0) {
1535 *repo_age = strdup("");
1536 if (*repo_age == NULL)
1537 return got_error_from_errno("strdup");
1538 return NULL;
1541 error = got_repo_open(&repo, dir, NULL);
1542 if (error)
1543 goto done;
1545 if (is_head)
1546 error = got_ref_list(&refs, repo, "refs/heads",
1547 got_ref_cmp_by_name, NULL);
1548 else
1549 error = got_ref_list(&refs, repo, repo_ref,
1550 got_ref_cmp_by_name, NULL);
1551 if (error)
1552 goto done;
1554 SIMPLEQ_FOREACH(re, &refs, entry) {
1555 if (is_head)
1556 refname = strdup(repo_ref);
1557 else
1558 refname = got_ref_get_name(re->ref);
1559 error = got_ref_open(&head_ref, repo, refname, 0);
1560 if (error)
1561 goto done;
1563 error = got_ref_resolve(&id, repo, head_ref);
1564 got_ref_close(head_ref);
1565 if (error)
1566 goto done;
1568 error = got_object_open_as_commit(&commit, repo, id);
1569 if (error)
1570 goto done;
1572 committer_time =
1573 got_object_commit_get_committer_time(commit);
1575 if (cmp_time < committer_time)
1576 cmp_time = committer_time;
1579 if (cmp_time != 0) {
1580 committer_time = cmp_time;
1581 *repo_age = gw_get_time_str(committer_time, ref_tm);
1582 } else {
1583 *repo_age = strdup("");
1584 if (*repo_age == NULL)
1585 error = got_error_from_errno("strdup");
1587 done:
1588 got_ref_list_free(&refs);
1589 free(id);
1590 return error;
1593 static char *
1594 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1596 const struct got_error *error;
1597 FILE *f = NULL;
1598 struct got_object_id *id1 = NULL, *id2 = NULL;
1599 struct buf *diffbuf = NULL;
1600 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL;
1601 char *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1602 int obj_type;
1603 size_t newsize;
1605 f = got_opentemp();
1606 if (f == NULL)
1607 return NULL;
1609 error = buf_alloc(&diffbuf, 0);
1610 if (error)
1611 return NULL;
1613 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1614 if (error)
1615 goto done;
1617 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1618 error = got_repo_match_object_id(&id1, &label1,
1619 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1620 if (error)
1621 goto done;
1624 error = got_repo_match_object_id(&id2, &label2,
1625 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1626 if (error)
1627 goto done;
1629 error = got_object_get_type(&obj_type, header->repo, id2);
1630 if (error)
1631 goto done;
1632 switch (obj_type) {
1633 case GOT_OBJ_TYPE_BLOB:
1634 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1635 header->repo, f);
1636 break;
1637 case GOT_OBJ_TYPE_TREE:
1638 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1639 header->repo, f);
1640 break;
1641 case GOT_OBJ_TYPE_COMMIT:
1642 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1643 header->repo, f);
1644 break;
1645 default:
1646 error = got_error(GOT_ERR_OBJ_TYPE);
1649 if ((buf = calloc(128, sizeof(char *))) == NULL)
1650 goto done;
1652 fseek(f, 0, SEEK_SET);
1654 while ((fgets(buf, 2048, f)) != NULL) {
1655 n_buf = buf;
1656 while (*n_buf == '\n')
1657 n_buf++;
1658 newline = strchr(n_buf, '\n');
1659 if (newline)
1660 *newline = ' ';
1662 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1663 if (buf_color == NULL)
1664 continue;
1666 error = buf_puts(&newsize, diffbuf, buf_color);
1667 if (error)
1668 return NULL;
1670 error = buf_puts(&newsize, diffbuf, div_end);
1671 if (error)
1672 return NULL;
1675 if (buf_len(diffbuf) > 0) {
1676 error = buf_putc(diffbuf, '\0');
1677 diff_html = strdup(buf_get(diffbuf));
1679 done:
1680 fclose(f);
1681 free(buf_color);
1682 free(buf);
1683 free(diffbuf);
1684 free(label1);
1685 free(label2);
1686 free(id1);
1687 free(id2);
1689 if (error)
1690 return NULL;
1691 else
1692 return diff_html;
1695 static char *
1696 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1698 FILE *f;
1699 char *owner = NULL, *d_file = NULL;
1700 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1701 char *comp, *pos, *buf;
1702 unsigned int i;
1704 if (gw_trans->gw_conf->got_show_repo_owner == 0)
1705 goto err;
1707 if (asprintf(&d_file, "%s/config", dir) == -1)
1708 goto err;
1710 if ((f = fopen(d_file, "r")) == NULL)
1711 goto err;
1713 if ((buf = calloc(128, sizeof(char *))) == NULL)
1714 goto err;
1716 while ((fgets(buf, 128, f)) != NULL) {
1717 if ((pos = strstr(buf, gotweb)) != NULL)
1718 break;
1720 if ((pos = strstr(buf, gitweb)) != NULL)
1721 break;
1724 if (pos == NULL)
1725 goto err;
1727 do {
1728 fgets(buf, 128, f);
1729 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1731 if (comp == NULL)
1732 goto err;
1734 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1735 goto err;
1737 for (i = 0; i < 2; i++) {
1738 owner = strsep(&buf, "\"");
1741 if (owner == NULL)
1742 goto err;
1744 fclose(f);
1745 free(d_file);
1746 return owner;
1747 err:
1748 return strdup("");
1751 static char *
1752 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1754 FILE *f;
1755 char *url = NULL, *d_file = NULL;
1756 unsigned int len;
1758 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
1759 return NULL;
1761 if ((f = fopen(d_file, "r")) == NULL)
1762 return NULL;
1764 fseek(f, 0, SEEK_END);
1765 len = ftell(f) + 1;
1766 fseek(f, 0, SEEK_SET);
1768 if ((url = calloc(len, sizeof(char *))) == NULL)
1769 return NULL;
1771 fread(url, 1, len, f);
1772 fclose(f);
1773 free(d_file);
1774 return url;
1777 static char *
1778 gw_get_repo_tags(struct gw_trans *gw_trans, struct gw_header *header, int limit,
1779 int tag_type)
1781 const struct got_error *error = NULL;
1782 struct got_repository *repo = NULL;
1783 struct got_reflist_head refs;
1784 struct got_reflist_entry *re;
1785 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL;
1786 char *age = NULL, *newline;
1787 struct buf *diffbuf = NULL;
1788 size_t newsize;
1790 SIMPLEQ_INIT(&refs);
1792 error = buf_alloc(&diffbuf, 0);
1793 if (error)
1794 return NULL;
1796 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1797 if (error)
1798 goto done;
1800 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1801 if (error)
1802 goto done;
1804 SIMPLEQ_FOREACH(re, &refs, entry) {
1805 const char *refname;
1806 char *refstr, *tag_commit0, *tag_commit, *id_str;
1807 const char *tagger;
1808 time_t tagger_time;
1809 struct got_object_id *id;
1810 struct got_tag_object *tag;
1812 refname = got_ref_get_name(re->ref);
1813 if (strncmp(refname, "refs/tags/", 10) != 0)
1814 continue;
1815 refname += 10;
1816 refstr = got_ref_to_str(re->ref);
1817 if (refstr == NULL) {
1818 error = got_error_from_errno("got_ref_to_str");
1819 goto done;
1822 error = got_ref_resolve(&id, repo, re->ref);
1823 if (error)
1824 goto done;
1825 error = got_object_open_as_tag(&tag, repo, id);
1826 free(id);
1827 if (error)
1828 goto done;
1830 tagger = got_object_tag_get_tagger(tag);
1831 tagger_time = got_object_tag_get_tagger_time(tag);
1833 error = got_object_id_str(&id_str,
1834 got_object_tag_get_object_id(tag));
1835 if (error)
1836 goto done;
1838 tag_commit0 = strdup(got_object_tag_get_message(tag));
1840 if (tag_commit0 == NULL) {
1841 error = got_error_from_errno("strdup");
1842 goto done;
1845 tag_commit = tag_commit0;
1846 while (*tag_commit == '\n')
1847 tag_commit++;
1849 switch (tag_type) {
1850 case TAGBRIEF:
1851 newline = strchr(tag_commit, '\n');
1852 if (newline)
1853 *newline = '\0';
1855 if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1856 TM_DIFF)) == -1) {
1857 error = got_error_from_errno("asprintf");
1858 goto done;
1861 if (asprintf(&tags_navs_disp, tags_navs,
1862 gw_trans->repo_name, id_str, gw_trans->repo_name,
1863 id_str, gw_trans->repo_name, id_str,
1864 gw_trans->repo_name, id_str) == -1) {
1865 error = got_error_from_errno("asprintf");
1866 goto done;
1869 if (asprintf(&tag_row, tags_row, age, refname,
1870 tag_commit, tags_navs_disp) == -1) {
1871 error = got_error_from_errno("asprintf");
1872 goto done;
1875 free(tags_navs_disp);
1876 break;
1877 case TAGFULL:
1878 if (asprintf(&age, "%s", gw_get_time_str(tagger_time,
1879 TM_LONG)) == -1) {
1880 error = got_error_from_errno("asprintf");
1881 goto done;
1883 if (asprintf(&tag_row, tag_info, age,
1884 gw_html_escape(tagger),
1885 gw_html_escape(tag_commit)) == -1) {
1886 error = got_error_from_errno("asprintf");
1887 goto done;
1889 break;
1890 default:
1891 break;
1894 got_object_tag_close(tag);
1896 error = buf_puts(&newsize, diffbuf, tag_row);
1898 free(id_str);
1899 free(refstr);
1900 free(age);
1901 free(tag_commit0);
1902 free(tag_row);
1904 if (error || (limit && --limit == 0))
1905 break;
1908 if (buf_len(diffbuf) > 0) {
1909 error = buf_putc(diffbuf, '\0');
1910 tags = strdup(buf_get(diffbuf));
1912 done:
1913 buf_free(diffbuf);
1914 got_ref_list_free(&refs);
1915 if (repo)
1916 got_repo_close(repo);
1917 if (error)
1918 return NULL;
1919 else
1920 return tags;
1923 static void
1924 gw_free_headers(struct gw_header *header)
1926 free(header->id);
1927 free(header->path);
1928 if (header->commit != NULL)
1929 got_object_commit_close(header->commit);
1930 if (header->repo)
1931 got_repo_close(header->repo);
1932 free(header->refs_str);
1933 free(header->commit_id);
1934 free(header->parent_id);
1935 free(header->tree_id);
1936 free(header->author);
1937 free(header->committer);
1938 free(header->commit_msg);
1941 static struct gw_header *
1942 gw_init_header()
1944 struct gw_header *header;
1946 header = malloc(sizeof(*header));
1947 if (header == NULL)
1948 return NULL;
1950 header->repo = NULL;
1951 header->commit = NULL;
1952 header->id = NULL;
1953 header->path = NULL;
1954 SIMPLEQ_INIT(&header->refs);
1956 return header;
1959 static const struct got_error *
1960 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1961 int limit)
1963 const struct got_error *error = NULL;
1964 struct got_commit_graph *graph = NULL;
1966 error = got_commit_graph_open(&graph, header->path, 0);
1967 if (error)
1968 goto done;
1970 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1971 NULL, NULL);
1972 if (error)
1973 goto done;
1975 for (;;) {
1976 error = got_commit_graph_iter_next(&header->id, graph,
1977 header->repo, NULL, NULL);
1978 if (error) {
1979 if (error->code == GOT_ERR_ITER_COMPLETED)
1980 error = NULL;
1981 goto done;
1983 if (header->id == NULL)
1984 goto done;
1986 error = got_object_open_as_commit(&header->commit, header->repo,
1987 header->id);
1988 if (error)
1989 goto done;
1991 error = gw_get_commit(gw_trans, header);
1992 if (limit > 1) {
1993 struct gw_header *n_header = NULL;
1994 if ((n_header = gw_init_header()) == NULL) {
1995 error = got_error_from_errno("malloc");
1996 goto done;
1999 n_header->refs_str = strdup(header->refs_str);
2000 n_header->commit_id = strdup(header->commit_id);
2001 n_header->parent_id = strdup(header->parent_id);
2002 n_header->tree_id = strdup(header->tree_id);
2003 n_header->author = strdup(header->author);
2004 n_header->committer = strdup(header->committer);
2005 n_header->commit_msg = strdup(header->commit_msg);
2006 n_header->committer_time = header->committer_time;
2007 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2008 entry);
2010 if (error || (limit && --limit == 0))
2011 break;
2013 done:
2014 if (graph)
2015 got_commit_graph_close(graph);
2016 return error;
2019 static const struct got_error *
2020 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2022 const struct got_error *error = NULL;
2023 struct got_reflist_entry *re;
2024 struct got_object_id *id2 = NULL;
2025 struct got_object_qid *parent_id;
2026 char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2028 /*print commit*/
2029 SIMPLEQ_FOREACH(re, &header->refs, entry) {
2030 char *s;
2031 const char *name;
2032 struct got_tag_object *tag = NULL;
2033 int cmp;
2035 name = got_ref_get_name(re->ref);
2036 if (strcmp(name, GOT_REF_HEAD) == 0)
2037 continue;
2038 if (strncmp(name, "refs/", 5) == 0)
2039 name += 5;
2040 if (strncmp(name, "got/", 4) == 0)
2041 continue;
2042 if (strncmp(name, "heads/", 6) == 0)
2043 name += 6;
2044 if (strncmp(name, "remotes/", 8) == 0)
2045 name += 8;
2046 if (strncmp(name, "tags/", 5) == 0) {
2047 error = got_object_open_as_tag(&tag, header->repo,
2048 re->id);
2049 if (error) {
2050 if (error->code != GOT_ERR_OBJ_TYPE)
2051 continue;
2053 * Ref points at something other
2054 * than a tag.
2056 error = NULL;
2057 tag = NULL;
2060 cmp = got_object_id_cmp(tag ?
2061 got_object_tag_get_object_id(tag) : re->id, header->id);
2062 if (tag)
2063 got_object_tag_close(tag);
2064 if (cmp != 0)
2065 continue;
2066 s = refs_str;
2067 if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2068 s ? ", " : "", name) == -1) {
2069 error = got_error_from_errno("asprintf");
2070 free(s);
2071 return error;
2073 header->refs_str = strdup(refs_str);
2074 free(s);
2077 if (refs_str == NULL)
2078 header->refs_str = strdup("");
2079 free(refs_str);
2081 error = got_object_id_str(&header->commit_id, header->id);
2082 if (error)
2083 return error;
2085 error = got_object_id_str(&header->tree_id,
2086 got_object_commit_get_tree_id(header->commit));
2087 if (error)
2088 return error;
2090 if (gw_trans->action == GW_DIFF) {
2091 parent_id = SIMPLEQ_FIRST(
2092 got_object_commit_get_parent_ids(header->commit));
2093 if (parent_id != NULL) {
2094 id2 = got_object_id_dup(parent_id->id);
2095 free (parent_id);
2096 error = got_object_id_str(&header->parent_id, id2);
2097 if (error)
2098 return error;
2099 free(id2);
2100 } else
2101 header->parent_id = strdup("/dev/null");
2102 } else
2103 header->parent_id = strdup("");
2105 header->committer_time =
2106 got_object_commit_get_committer_time(header->commit);
2108 if (gw_trans->action != GW_BRIEFS && gw_trans->action != GW_SUMMARY) {
2109 header->author = strdup(
2110 gw_html_escape(got_object_commit_get_author(header->commit))
2112 } else {
2113 header->author = strdup(
2114 got_object_commit_get_author(header->commit)
2118 header->committer = strdup(
2119 gw_html_escape(got_object_commit_get_committer(header->commit))
2122 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2123 if (error)
2124 return error;
2126 commit_msg = commit_msg0;
2127 while (*commit_msg == '\n')
2128 commit_msg++;
2130 header->commit_msg = strdup(commit_msg);
2131 free(commit_msg0);
2132 return error;
2135 static const struct got_error *
2136 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2138 const struct got_error *error = NULL;
2139 char *in_repo_path = NULL;
2141 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2142 if (error)
2143 return error;
2145 if (gw_trans->commit == NULL) {
2146 struct got_reference *head_ref;
2147 error = got_ref_open(&head_ref, header->repo,
2148 gw_trans->headref, 0);
2149 if (error)
2150 return error;
2152 error = got_ref_resolve(&header->id, header->repo, head_ref);
2153 got_ref_close(head_ref);
2154 if (error)
2155 return error;
2157 error = got_object_open_as_commit(&header->commit,
2158 header->repo, header->id);
2159 } else {
2160 struct got_reference *ref;
2161 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2162 if (error == NULL) {
2163 int obj_type;
2164 error = got_ref_resolve(&header->id, header->repo, ref);
2165 got_ref_close(ref);
2166 if (error)
2167 return error;
2168 error = got_object_get_type(&obj_type, header->repo,
2169 header->id);
2170 if (error)
2171 return error;
2172 if (obj_type == GOT_OBJ_TYPE_TAG) {
2173 struct got_tag_object *tag;
2174 error = got_object_open_as_tag(&tag,
2175 header->repo, header->id);
2176 if (error)
2177 return error;
2178 if (got_object_tag_get_object_type(tag) !=
2179 GOT_OBJ_TYPE_COMMIT) {
2180 got_object_tag_close(tag);
2181 error = got_error(GOT_ERR_OBJ_TYPE);
2182 return error;
2184 free(header->id);
2185 header->id = got_object_id_dup(
2186 got_object_tag_get_object_id(tag));
2187 if (header->id == NULL)
2188 error = got_error_from_errno(
2189 "got_object_id_dup");
2190 got_object_tag_close(tag);
2191 if (error)
2192 return error;
2193 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2194 error = got_error(GOT_ERR_OBJ_TYPE);
2195 return error;
2197 error = got_object_open_as_commit(&header->commit,
2198 header->repo, header->id);
2199 if (error)
2200 return error;
2202 if (header->commit == NULL) {
2203 error = got_repo_match_object_id_prefix(&header->id,
2204 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2205 header->repo);
2206 if (error)
2207 return error;
2209 error = got_repo_match_object_id_prefix(&header->id,
2210 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2211 header->repo);
2214 error = got_repo_map_path(&in_repo_path, header->repo,
2215 gw_trans->repo_path, 1);
2216 if (error)
2217 return error;
2219 if (in_repo_path) {
2220 header->path = strdup(in_repo_path);
2222 free(in_repo_path);
2224 error = got_ref_list(&header->refs, header->repo, NULL,
2225 got_ref_cmp_by_name, NULL);
2226 if (error)
2227 return error;
2229 error = gw_get_commits(gw_trans, header, limit);
2230 return error;
2233 struct blame_line {
2234 int annotated;
2235 char *id_str;
2236 char *committer;
2237 char datebuf[11]; /* YYYY-MM-DD + NUL */
2240 struct gw_blame_cb_args {
2241 struct blame_line *lines;
2242 int nlines;
2243 int nlines_prec;
2244 int lineno_cur;
2245 off_t *line_offsets;
2246 FILE *f;
2247 struct got_repository *repo;
2248 struct gw_trans *gw_trans;
2249 struct buf *blamebuf;
2252 static const struct got_error *
2253 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2255 const struct got_error *err = NULL;
2256 struct gw_blame_cb_args *a = arg;
2257 struct blame_line *bline;
2258 char *line = NULL;
2259 size_t linesize = 0, newsize;
2260 struct got_commit_object *commit = NULL;
2261 off_t offset;
2262 struct tm tm;
2263 time_t committer_time;
2265 if (nlines != a->nlines ||
2266 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2267 return got_error(GOT_ERR_RANGE);
2269 if (lineno == -1)
2270 return NULL; /* no change in this commit */
2272 /* Annotate this line. */
2273 bline = &a->lines[lineno - 1];
2274 if (bline->annotated)
2275 return NULL;
2276 err = got_object_id_str(&bline->id_str, id);
2277 if (err)
2278 return err;
2280 err = got_object_open_as_commit(&commit, a->repo, id);
2281 if (err)
2282 goto done;
2284 bline->committer = strdup(got_object_commit_get_committer(commit));
2285 if (bline->committer == NULL) {
2286 err = got_error_from_errno("strdup");
2287 goto done;
2290 committer_time = got_object_commit_get_committer_time(commit);
2291 if (localtime_r(&committer_time, &tm) == NULL)
2292 return got_error_from_errno("localtime_r");
2293 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2294 &tm) >= sizeof(bline->datebuf)) {
2295 err = got_error(GOT_ERR_NO_SPACE);
2296 goto done;
2298 bline->annotated = 1;
2300 /* Print lines annotated so far. */
2301 bline = &a->lines[a->lineno_cur - 1];
2302 if (!bline->annotated)
2303 goto done;
2305 offset = a->line_offsets[a->lineno_cur - 1];
2306 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2307 err = got_error_from_errno("fseeko");
2308 goto done;
2311 while (bline->annotated) {
2312 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2313 *line_escape = NULL;
2314 size_t len;
2316 if (getline(&line, &linesize, a->f) == -1) {
2317 if (ferror(a->f))
2318 err = got_error_from_errno("getline");
2319 break;
2322 committer = bline->committer;
2323 smallerthan = strchr(committer, '<');
2324 if (smallerthan && smallerthan[1] != '\0')
2325 committer = smallerthan + 1;
2326 at = strchr(committer, '@');
2327 if (at)
2328 *at = '\0';
2329 len = strlen(committer);
2330 if (len >= 9)
2331 committer[8] = '\0';
2333 nl = strchr(line, '\n');
2334 if (nl)
2335 *nl = '\0';
2337 if (strcmp(line, "") != 0)
2338 line_escape = strdup(gw_html_escape(line));
2339 else
2340 line_escape = strdup("");
2342 asprintf(&blame_row, blame_line, a->nlines_prec,
2343 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2344 line_escape);
2345 a->lineno_cur++;
2346 err = buf_puts(&newsize, a->blamebuf, blame_row);
2347 if (err)
2348 return err;
2350 bline = &a->lines[a->lineno_cur - 1];
2351 free(line_escape);
2352 free(blame_row);
2354 done:
2355 if (commit)
2356 got_object_commit_close(commit);
2357 free(line);
2358 return err;
2361 static char*
2362 gw_get_file_blame_blob(struct gw_trans *gw_trans)
2364 const struct got_error *error = NULL;
2365 struct got_repository *repo = NULL;
2366 struct got_object_id *obj_id = NULL;
2367 struct got_object_id *commit_id = NULL;
2368 struct got_blob_object *blob = NULL;
2369 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL;
2370 char *folder = NULL;
2371 struct gw_blame_cb_args bca;
2372 int i, obj_type;
2373 size_t filesize;
2375 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2376 if (error)
2377 goto done;
2379 if (gw_trans->repo_folder != NULL) {
2380 if (asprintf(&folder, "%s/", gw_trans->repo_folder) == -1) {
2381 error = got_error_from_errno("asprintf");
2382 goto done;
2384 } else
2385 folder = strdup("");
2387 if (asprintf(&path, "%s%s", folder, gw_trans->repo_file) == -1) {
2388 error = got_error_from_errno("asprintf");
2389 goto done;
2391 free(folder);
2393 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2394 if (error)
2395 goto done;
2397 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2398 GOT_OBJ_TYPE_COMMIT, 1, repo);
2399 if (error)
2400 goto done;
2402 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2403 if (error)
2404 goto done;
2406 if (obj_id == NULL) {
2407 error = got_error(GOT_ERR_NO_OBJ);
2408 goto done;
2411 error = got_object_get_type(&obj_type, repo, obj_id);
2412 if (error)
2413 goto done;
2415 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2416 error = got_error(GOT_ERR_OBJ_TYPE);
2417 goto done;
2420 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2421 if (error)
2422 goto done;
2424 error = buf_alloc(&bca.blamebuf, 0);
2425 if (error)
2426 goto done;
2428 bca.f = got_opentemp();
2429 if (bca.f == NULL) {
2430 error = got_error_from_errno("got_opentemp");
2431 goto done;
2433 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2434 &bca.line_offsets, bca.f, blob);
2435 if (error || bca.nlines == 0)
2436 goto done;
2438 if (gw_trans->action == GW_BLOB) {
2439 int len;
2440 fseek(bca.f, 0, SEEK_END);
2441 len = ftell(bca.f) + 1;
2442 fseek(bca.f, 0, SEEK_SET);
2443 if ((blame_html = calloc(len, sizeof(char *))) == NULL)
2444 goto done;
2445 fread(blame_html, 1, len, bca.f);
2446 goto done;
2449 /* Don't include \n at EOF in the blame line count. */
2450 if (bca.line_offsets[bca.nlines - 1] == filesize)
2451 bca.nlines--;
2453 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2454 if (bca.lines == NULL) {
2455 error = got_error_from_errno("calloc");
2456 goto done;
2458 bca.lineno_cur = 1;
2459 bca.nlines_prec = 0;
2460 i = bca.nlines;
2461 while (i > 0) {
2462 i /= 10;
2463 bca.nlines_prec++;
2465 bca.repo = repo;
2466 bca.gw_trans = gw_trans;
2468 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2469 NULL, NULL);
2470 if (error)
2471 goto done;
2472 if (buf_len(bca.blamebuf) > 0) {
2473 error = buf_putc(bca.blamebuf, '\0');
2474 blame_html = strdup(buf_get(bca.blamebuf));
2476 done:
2477 free(bca.line_offsets);
2478 free(bca.blamebuf);
2479 free(in_repo_path);
2480 free(commit_id);
2481 free(obj_id);
2482 free(path);
2484 if (gw_trans->action != GW_BLOB && bca.lines) {
2485 for (i = 0; i < bca.nlines; i++) {
2486 struct blame_line *bline = &bca.lines[i];
2487 free(bline->id_str);
2488 free(bline->committer);
2490 free(bca.lines);
2492 if (error)
2493 return NULL;
2494 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2495 return NULL;
2496 if (blob)
2497 error = got_object_blob_close(blob);
2498 if (error)
2499 return NULL;
2500 if (repo)
2501 error = got_repo_close(repo);
2502 if (error)
2503 return NULL;
2504 else
2505 return blame_html;
2508 static char*
2509 gw_get_repo_tree(struct gw_trans *gw_trans)
2511 const struct got_error *error = NULL;
2512 struct got_repository *repo = NULL;
2513 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2514 struct got_tree_object *tree = NULL;
2515 struct buf *diffbuf = NULL;
2516 size_t newsize;
2517 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2518 *tree_row = NULL, *id_str, *class = NULL;
2519 int nentries, i, class_flip = 0;
2521 error = buf_alloc(&diffbuf, 0);
2522 if (error)
2523 return NULL;
2525 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2526 if (error)
2527 goto done;
2529 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2530 if (error)
2531 goto done;
2533 if (gw_trans->repo_folder != NULL)
2534 path = strdup(gw_trans->repo_folder);
2535 else if (in_repo_path) {
2536 free(path);
2537 path = in_repo_path;
2540 if (gw_trans->commit == NULL) {
2541 struct got_reference *head_ref;
2542 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2543 if (error)
2544 goto done;
2546 error = got_ref_resolve(&commit_id, repo, head_ref);
2547 got_ref_close(head_ref);
2549 } else
2550 error = got_repo_match_object_id(&commit_id, NULL,
2551 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2552 if (error)
2553 goto done;
2555 error = got_object_id_str(&gw_trans->commit, commit_id);
2556 if (error)
2557 goto done;
2559 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2560 if (error)
2561 goto done;
2563 error = got_object_open_as_tree(&tree, repo, tree_id);
2564 if (error)
2565 goto done;
2567 nentries = got_object_tree_get_nentries(tree);
2569 for (i = 0; i < nentries; i++) {
2570 struct got_tree_entry *te;
2571 const char *modestr = "";
2572 char *id = NULL, *url_html = NULL;
2574 te = got_object_tree_get_entry(tree, i);
2576 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2577 if (error)
2578 goto done;
2580 if (asprintf(&id, "%s", id_str) == -1) {
2581 error = got_error_from_errno("asprintf");
2582 free(id_str);
2583 goto done;
2586 mode_t mode = got_tree_entry_get_mode(te);
2588 if (got_object_tree_entry_is_submodule(te))
2589 modestr = "$";
2590 else if (S_ISLNK(mode))
2591 modestr = "@";
2592 else if (S_ISDIR(mode))
2593 modestr = "/";
2594 else if (mode & S_IXUSR)
2595 modestr = "*";
2597 if (class_flip == 0) {
2598 class = strdup("back_lightgray");
2599 class_flip = 1;
2600 } else {
2601 class = strdup("back_white");
2602 class_flip = 0;
2605 char *build_folder = NULL;
2606 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2607 if (gw_trans->repo_folder != NULL) {
2608 if (asprintf(&build_folder, "%s/%s",
2609 gw_trans->repo_folder,
2610 got_tree_entry_get_name(te)) == -1) {
2611 error =
2612 got_error_from_errno("asprintf");
2613 goto done;
2615 } else {
2616 if (asprintf(&build_folder, "%s",
2617 got_tree_entry_get_name(te)) == -1)
2618 goto done;
2621 if (asprintf(&url_html, folder_html,
2622 gw_trans->repo_name, gw_trans->action_name,
2623 gw_trans->commit, build_folder,
2624 got_tree_entry_get_name(te), modestr) == -1) {
2625 error = got_error_from_errno("asprintf");
2626 goto done;
2628 if (asprintf(&tree_row, tree_line, class, url_html,
2629 class) == -1) {
2630 error = got_error_from_errno("asprintf");
2631 goto done;
2634 } else {
2635 if (gw_trans->repo_folder != NULL) {
2636 if (asprintf(&build_folder, "%s",
2637 gw_trans->repo_folder) == -1) {
2638 error =
2639 got_error_from_errno("asprintf");
2640 goto done;
2642 } else
2643 build_folder = strdup("");
2645 if (asprintf(&url_html, file_html, gw_trans->repo_name,
2646 "blob", gw_trans->commit,
2647 got_tree_entry_get_name(te), build_folder,
2648 got_tree_entry_get_name(te), modestr) == -1) {
2649 error = got_error_from_errno("asprintf");
2650 goto done;
2653 if (asprintf(&tree_row, tree_line_with_navs, class,
2654 url_html, class, gw_trans->repo_name, "blob",
2655 gw_trans->commit, got_tree_entry_get_name(te),
2656 build_folder, "blob", gw_trans->repo_name,
2657 "blame", gw_trans->commit,
2658 got_tree_entry_get_name(te), build_folder,
2659 "blame") == -1) {
2660 error = got_error_from_errno("asprintf");
2661 goto done;
2664 free(build_folder);
2666 if (error)
2667 goto done;
2669 error = buf_puts(&newsize, diffbuf, tree_row);
2670 if (error)
2671 goto done;
2673 free(id);
2674 free(id_str);
2675 free(url_html);
2676 free(tree_row);
2679 if (buf_len(diffbuf) > 0) {
2680 error = buf_putc(diffbuf, '\0');
2681 tree_html = strdup(buf_get(diffbuf));
2683 done:
2684 if (tree)
2685 got_object_tree_close(tree);
2686 if (repo)
2687 got_repo_close(repo);
2689 free(in_repo_path);
2690 free(tree_id);
2691 free(diffbuf);
2692 if (error)
2693 return NULL;
2694 else
2695 return tree_html;
2698 static char *
2699 gw_get_repo_heads(struct gw_trans *gw_trans)
2701 const struct got_error *error = NULL;
2702 struct got_repository *repo = NULL;
2703 struct got_reflist_head refs;
2704 struct got_reflist_entry *re;
2705 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2706 struct buf *diffbuf = NULL;
2707 size_t newsize;
2709 SIMPLEQ_INIT(&refs);
2711 error = buf_alloc(&diffbuf, 0);
2712 if (error)
2713 return NULL;
2715 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2716 if (error)
2717 goto done;
2719 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2720 NULL);
2721 if (error)
2722 goto done;
2724 SIMPLEQ_FOREACH(re, &refs, entry) {
2725 char *refname;
2727 refname = strdup(got_ref_get_name(re->ref));
2728 if (refname == NULL) {
2729 error = got_error_from_errno("got_ref_to_str");
2730 goto done;
2733 if (strncmp(refname, "refs/heads/", 11) != 0) {
2734 free(refname);
2735 continue;
2738 error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path, refname,
2739 TM_DIFF);
2740 if (error)
2741 goto done;
2743 if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2744 refname, gw_trans->repo_name, refname,
2745 gw_trans->repo_name, refname, gw_trans->repo_name,
2746 refname) == -1) {
2747 error = got_error_from_errno("asprintf");
2748 goto done;
2751 if (strncmp(refname, "refs/heads/", 11) == 0)
2752 refname += 11;
2754 if (asprintf(&head_row, heads_row, age, refname,
2755 head_navs_disp) == -1) {
2756 error = got_error_from_errno("asprintf");
2757 goto done;
2760 error = buf_puts(&newsize, diffbuf, head_row);
2762 free(head_navs_disp);
2763 free(head_row);
2766 if (buf_len(diffbuf) > 0) {
2767 error = buf_putc(diffbuf, '\0');
2768 heads = strdup(buf_get(diffbuf));
2770 done:
2771 buf_free(diffbuf);
2772 got_ref_list_free(&refs);
2773 if (repo)
2774 got_repo_close(repo);
2775 if (error)
2776 return NULL;
2777 else
2778 return heads;
2781 static char *
2782 gw_get_got_link(struct gw_trans *gw_trans)
2784 char *link;
2786 if (asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2787 gw_trans->gw_conf->got_logo) == -1)
2788 return NULL;
2790 return link;
2793 static char *
2794 gw_get_site_link(struct gw_trans *gw_trans)
2796 char *link, *repo = "", *action = "";
2798 if (gw_trans->repo_name != NULL)
2799 if (asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2800 "</a>", gw_trans->repo_name, gw_trans->repo_name) == -1)
2801 return NULL;
2803 if (gw_trans->action_name != NULL)
2804 if (asprintf(&action, " / %s", gw_trans->action_name) == -1)
2805 return NULL;
2807 if (asprintf(&link, site_link, GOTWEB,
2808 gw_trans->gw_conf->got_site_link, repo, action) == -1)
2809 return NULL;
2811 return link;
2814 static char *
2815 gw_colordiff_line(char *buf)
2817 const struct got_error *error = NULL;
2818 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2819 struct buf *diffbuf = NULL;
2820 size_t newsize;
2822 error = buf_alloc(&diffbuf, 0);
2823 if (error)
2824 return NULL;
2826 if (buf == NULL)
2827 return NULL;
2828 if (strncmp(buf, "-", 1) == 0)
2829 color = "diff_minus";
2830 if (strncmp(buf, "+", 1) == 0)
2831 color = "diff_plus";
2832 if (strncmp(buf, "@@", 2) == 0)
2833 color = "diff_chunk_header";
2834 if (strncmp(buf, "@@", 2) == 0)
2835 color = "diff_chunk_header";
2836 if (strncmp(buf, "commit +", 8) == 0)
2837 color = "diff_meta";
2838 if (strncmp(buf, "commit -", 8) == 0)
2839 color = "diff_meta";
2840 if (strncmp(buf, "blob +", 6) == 0)
2841 color = "diff_meta";
2842 if (strncmp(buf, "blob -", 6) == 0)
2843 color = "diff_meta";
2844 if (strncmp(buf, "file +", 6) == 0)
2845 color = "diff_meta";
2846 if (strncmp(buf, "file -", 6) == 0)
2847 color = "diff_meta";
2848 if (strncmp(buf, "from:", 5) == 0)
2849 color = "diff_author";
2850 if (strncmp(buf, "via:", 4) == 0)
2851 color = "diff_author";
2852 if (strncmp(buf, "date:", 5) == 0)
2853 color = "diff_date";
2855 if (asprintf(&div_diff_line_div, div_diff_line, color) == -1)
2856 return NULL;
2858 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2859 if (error)
2860 return NULL;
2862 error = buf_puts(&newsize, diffbuf, buf);
2863 if (error)
2864 return NULL;
2866 if (buf_len(diffbuf) > 0) {
2867 error = buf_putc(diffbuf, '\0');
2868 colorized_line = strdup(buf_get(diffbuf));
2871 free(diffbuf);
2872 free(div_diff_line_div);
2873 return colorized_line;
2876 static char *
2877 gw_html_escape(const char *html)
2879 char *escaped_str = NULL, *buf;
2880 char c[1];
2881 size_t sz, i, buff_sz = 2048;
2883 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2884 return NULL;
2886 if (html == NULL)
2887 return NULL;
2888 else
2889 if ((sz = strlen(html)) == 0)
2890 return NULL;
2892 /* only work with buff_sz */
2893 if (buff_sz < sz)
2894 sz = buff_sz;
2896 for (i = 0; i < sz; i++) {
2897 c[0] = html[i];
2898 switch (c[0]) {
2899 case ('>'):
2900 strcat(buf, "&gt;");
2901 break;
2902 case ('&'):
2903 strcat(buf, "&amp;");
2904 break;
2905 case ('<'):
2906 strcat(buf, "&lt;");
2907 break;
2908 case ('"'):
2909 strcat(buf, "&quot;");
2910 break;
2911 case ('\''):
2912 strcat(buf, "&apos;");
2913 break;
2914 case ('\n'):
2915 strcat(buf, "<br />");
2916 default:
2917 strcat(buf, &c[0]);
2918 break;
2921 asprintf(&escaped_str, "%s", buf);
2922 free(buf);
2923 return escaped_str;
2926 int
2927 main(int argc, char *argv[])
2929 const struct got_error *error = NULL;
2930 struct gw_trans *gw_trans;
2931 struct gw_dir *dir = NULL, *tdir;
2932 const char *page = "index";
2933 int gw_malloc = 1;
2934 enum kcgi_err kerr;
2936 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2937 errx(1, "malloc");
2939 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2940 errx(1, "malloc");
2942 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2943 errx(1, "malloc");
2945 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2946 errx(1, "malloc");
2948 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2949 if (kerr != KCGI_OK) {
2950 error = gw_kcgi_error(kerr);
2951 goto done;
2954 if ((gw_trans->gw_conf =
2955 malloc(sizeof(struct gotweb_conf))) == NULL) {
2956 gw_malloc = 0;
2957 error = got_error_from_errno("malloc");
2958 goto done;
2961 TAILQ_INIT(&gw_trans->gw_dirs);
2962 TAILQ_INIT(&gw_trans->gw_headers);
2964 gw_trans->page = 0;
2965 gw_trans->repos_total = 0;
2966 gw_trans->repo_path = NULL;
2967 gw_trans->commit = NULL;
2968 gw_trans->headref = strdup(GOT_REF_HEAD);
2969 gw_trans->mime = KMIME_TEXT_HTML;
2970 gw_trans->gw_tmpl->key = gw_templs;
2971 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2972 gw_trans->gw_tmpl->arg = gw_trans;
2973 gw_trans->gw_tmpl->cb = gw_template;
2974 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2975 if (error)
2976 goto done;
2978 error = gw_parse_querystring(gw_trans);
2979 if (error)
2980 goto done;
2982 error = gw_display_index(gw_trans);
2983 done:
2984 if (error) {
2985 gw_trans->mime = KMIME_TEXT_PLAIN;
2986 gw_trans->action = GW_ERR;
2987 gw_display_error(gw_trans, error);
2989 if (gw_malloc) {
2990 free(gw_trans->gw_conf->got_repos_path);
2991 free(gw_trans->gw_conf->got_site_name);
2992 free(gw_trans->gw_conf->got_site_owner);
2993 free(gw_trans->gw_conf->got_site_link);
2994 free(gw_trans->gw_conf->got_logo);
2995 free(gw_trans->gw_conf->got_logo_url);
2996 free(gw_trans->gw_conf);
2997 free(gw_trans->commit);
2998 free(gw_trans->repo_path);
2999 free(gw_trans->repo_name);
3000 free(gw_trans->repo_file);
3001 free(gw_trans->action_name);
3002 free(gw_trans->headref);
3004 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3005 free(dir->name);
3006 free(dir->description);
3007 free(dir->age);
3008 free(dir->url);
3009 free(dir->path);
3010 free(dir);
3015 khttp_free(gw_trans->gw_req);
3016 return 0;