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 <regex.h>
25 #include <stdarg.h>
26 #include <stdbool.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 trans {
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct gw_dir *gw_dir;
59 struct gotweb_conf *gw_conf;
60 struct ktemplate *gw_tmpl;
61 struct khtmlreq *gw_html_req;
62 struct kreq *gw_req;
63 char *repo_name;
64 char *repo_path;
65 char *commit;
66 char *repo_file;
67 char *repo_folder;
68 char *action_name;
69 char *headref;
70 unsigned int action;
71 unsigned int page;
72 unsigned int repos_total;
73 enum kmime mime;
74 };
76 enum gw_key {
77 KEY_ACTION,
78 KEY_COMMIT_ID,
79 KEY_FILE,
80 KEY_FOLDER,
81 KEY_HEADREF,
82 KEY_PAGE,
83 KEY_PATH,
84 KEY__ZMAX
85 };
87 struct gw_dir {
88 TAILQ_ENTRY(gw_dir) entry;
89 char *name;
90 char *owner;
91 char *description;
92 char *url;
93 char *age;
94 char *path;
95 };
97 enum tmpl {
98 TEMPL_HEAD,
99 TEMPL_HEADER,
100 TEMPL_SITEPATH,
101 TEMPL_SITEOWNER,
102 TEMPL_TITLE,
103 TEMPL_SEARCH,
104 TEMPL_CONTENT,
105 TEMPL__MAX
106 };
108 enum ref_tm {
109 TM_DIFF,
110 TM_LONG,
111 };
113 enum logs {
114 LOGBRIEF,
115 LOGCOMMIT,
116 LOGFULL,
117 LOGTREE,
118 LOGDIFF,
119 LOGBLAME,
120 LOGTAG,
121 };
123 enum tags {
124 TAGBRIEF,
125 TAGFULL,
126 };
128 struct buf {
129 u_char *cb_buf;
130 size_t cb_size;
131 size_t cb_len;
132 };
134 static const char *const templs[TEMPL__MAX] = {
135 "head",
136 "header",
137 "sitepath",
138 "siteowner",
139 "title",
140 "search",
141 "content",
142 };
144 static const struct kvalid gw_keys[KEY__ZMAX] = {
145 { kvalid_stringne, "action" },
146 { kvalid_stringne, "commit" },
147 { kvalid_stringne, "file" },
148 { kvalid_stringne, "folder" },
149 { kvalid_stringne, "headref" },
150 { kvalid_int, "page" },
151 { kvalid_stringne, "path" },
152 };
154 int gw_get_repo_log_count(struct trans *, char *);
156 static struct gw_dir *gw_init_gw_dir(char *);
158 static char *gw_get_repo_description(struct trans *,
159 char *);
160 static char *gw_get_repo_owner(struct trans *,
161 char *);
162 static char *gw_get_time_str(time_t, int);
163 static char *gw_get_repo_age(struct trans *,
164 char *, char *, int);
165 static char *gw_get_repo_log(struct trans *, const char *,
166 char *, int, int);
167 static char *gw_get_file_blame(struct trans *, char *);
168 static char *gw_get_repo_tree(struct trans *, char *);
169 static char *gw_get_repo_diff(struct trans *, char *,
170 char *);
171 static char *gw_get_repo_tags(struct trans *, int, int);
172 static char *gw_get_repo_heads(struct trans *);
173 static char *gw_get_clone_url(struct trans *, char *);
174 static char *gw_get_got_link(struct trans *);
175 static char *gw_get_site_link(struct trans *);
176 static char *gw_html_escape(const char *);
177 static char *color_diff_line(char *);
179 static void gw_display_open(struct trans *, enum khttp,
180 enum kmime);
181 static void gw_display_index(struct trans *,
182 const struct got_error *);
184 static int gw_template(size_t, void *);
186 static const struct got_error* apply_unveil(const char *, const char *);
187 static const struct got_error* cmp_tags(void *, int *,
188 struct got_reference *,
189 struct got_reference *);
190 static const struct got_error* resolve_commit_arg(struct got_object_id **,
191 const char *, struct got_repository *);
192 static const struct got_error* match_object_id(struct got_object_id **,
193 char **, const char *r, int, int,
194 struct got_repository *);
195 static const struct got_error* blame_cb(void *, int, int,
196 struct got_object_id *);
197 static const struct got_error* gw_load_got_paths(struct trans *);
198 static const struct got_error* gw_load_got_path(struct trans *,
199 struct gw_dir *);
200 static const struct got_error* gw_parse_querystring(struct trans *);
201 static const struct got_error* match_logmsg(int *, struct got_object_id *,
202 struct got_commit_object *, regex_t *);
204 static const struct got_error* gw_blame(struct trans *);
205 static const struct got_error* gw_commit(struct trans *);
206 static const struct got_error* gw_commitdiff(struct trans *);
207 static const struct got_error* gw_index(struct trans *);
208 static const struct got_error* gw_log(struct trans *);
209 static const struct got_error* gw_raw(struct trans *);
210 static const struct got_error* gw_logbriefs(struct trans *);
211 static const struct got_error* gw_summary(struct trans *);
212 static const struct got_error* gw_tag(struct trans *);
213 static const struct got_error* gw_tree(struct trans *);
215 struct gw_query_action {
216 unsigned int func_id;
217 const char *func_name;
218 const struct got_error *(*func_main)(struct trans *);
219 char *template;
220 };
222 enum gw_query_actions {
223 GW_BLAME,
224 GW_COMMIT,
225 GW_COMMITDIFF,
226 GW_ERR,
227 GW_INDEX,
228 GW_LOG,
229 GW_RAW,
230 GW_LOGBRIEFS,
231 GW_SUMMARY,
232 GW_TAG,
233 GW_TREE,
234 };
236 static struct gw_query_action gw_query_funcs[] = {
237 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
238 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
239 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
240 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
241 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
242 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
243 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
244 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
245 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
246 { GW_TAG, "tag", gw_tag, "gw_tmpl/index.tmpl" },
247 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
248 };
250 static const struct got_error *
251 apply_unveil(const char *repo_path, const char *repo_file)
253 const struct got_error *err;
255 if (repo_path && repo_file) {
256 char *full_path;
257 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
258 return got_error_from_errno("asprintf unveil");
259 if (unveil(full_path, "r") != 0)
260 return got_error_from_errno2("unveil", full_path);
263 if (repo_path && unveil(repo_path, "r") != 0)
264 return got_error_from_errno2("unveil", repo_path);
266 if (unveil("/tmp", "rwc") != 0)
267 return got_error_from_errno2("unveil", "/tmp");
269 err = got_privsep_unveil_exec_helpers();
270 if (err != NULL)
271 return err;
273 if (unveil(NULL, NULL) != 0)
274 return got_error_from_errno("unveil");
276 return NULL;
279 static const struct got_error *
280 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
281 struct got_reference *ref2)
283 const struct got_error *err = NULL;
284 struct got_repository *repo = arg;
285 struct got_object_id *id1, *id2 = NULL;
286 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
287 time_t time1, time2;
289 *cmp = 0;
291 err = got_ref_resolve(&id1, repo, ref1);
292 if (err)
293 return err;
294 err = got_object_open_as_tag(&tag1, repo, id1);
295 if (err)
296 goto done;
298 err = got_ref_resolve(&id2, repo, ref2);
299 if (err)
300 goto done;
301 err = got_object_open_as_tag(&tag2, repo, id2);
302 if (err)
303 goto done;
305 time1 = got_object_tag_get_tagger_time(tag1);
306 time2 = got_object_tag_get_tagger_time(tag2);
308 /* Put latest tags first. */
309 if (time1 < time2)
310 *cmp = 1;
311 else if (time1 > time2)
312 *cmp = -1;
313 else
314 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
315 done:
316 free(id1);
317 free(id2);
318 if (tag1)
319 got_object_tag_close(tag1);
320 if (tag2)
321 got_object_tag_close(tag2);
322 return err;
325 static const struct got_error *
326 resolve_commit_arg(struct got_object_id **commit_id,
327 const char *commit_id_arg, struct got_repository *repo)
329 const struct got_error *err;
330 struct got_reference *ref;
331 struct got_tag_object *tag;
333 err = got_repo_object_match_tag(&tag, commit_id_arg,
334 GOT_OBJ_TYPE_COMMIT, repo);
335 if (err == NULL) {
336 *commit_id = got_object_id_dup(
337 got_object_tag_get_object_id(tag));
338 if (*commit_id == NULL)
339 err = got_error_from_errno("got_object_id_dup");
340 got_object_tag_close(tag);
341 return err;
342 } else if (err->code != GOT_ERR_NO_OBJ)
343 return err;
345 err = got_ref_open(&ref, repo, commit_id_arg, 0);
346 if (err == NULL) {
347 err = got_ref_resolve(commit_id, repo, ref);
348 got_ref_close(ref);
349 } else {
350 if (err->code != GOT_ERR_NOT_REF)
351 return err;
352 err = got_repo_match_object_id_prefix(commit_id,
353 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
355 return err;
358 static const struct got_error *
359 match_object_id(struct got_object_id **id, char **label,
360 const char *id_str, int obj_type, int resolve_tags,
361 struct got_repository *repo)
363 const struct got_error *err;
364 struct got_tag_object *tag;
365 struct got_reference *ref = NULL;
367 *id = NULL;
368 *label = NULL;
370 if (resolve_tags) {
371 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
372 repo);
373 if (err == NULL) {
374 *id = got_object_id_dup(
375 got_object_tag_get_object_id(tag));
376 if (*id == NULL)
377 err = got_error_from_errno("got_object_id_dup");
378 else if (asprintf(label, "refs/tags/%s",
379 got_object_tag_get_name(tag)) == -1) {
380 err = got_error_from_errno("asprintf");
381 free(*id);
382 *id = NULL;
384 got_object_tag_close(tag);
385 return err;
386 } else if (err->code != GOT_ERR_NO_OBJ)
387 return err;
390 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
391 if (err) {
392 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
393 return err;
394 err = got_ref_open(&ref, repo, id_str, 0);
395 if (err != NULL)
396 goto done;
397 *label = strdup(got_ref_get_name(ref));
398 if (*label == NULL) {
399 err = got_error_from_errno("strdup");
400 goto done;
402 err = got_ref_resolve(id, repo, ref);
403 } else {
404 err = got_object_id_str(label, *id);
405 if (*label == NULL) {
406 err = got_error_from_errno("strdup");
407 goto done;
410 done:
411 if (ref)
412 got_ref_close(ref);
413 return err;
416 int
417 gw_get_repo_log_count(struct trans *gw_trans, char *start_commit)
419 const struct got_error *error;
420 struct got_repository *repo = NULL;
421 struct got_reflist_head refs;
422 struct got_commit_object *commit = NULL;
423 struct got_object_id *id = NULL;
424 struct got_commit_graph *graph = NULL;
425 char *in_repo_path = NULL, *path = NULL;
426 int log_count = 0;
428 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
429 if (error)
430 return 0;
432 SIMPLEQ_INIT(&refs);
434 if (start_commit == NULL) {
435 struct got_reference *head_ref;
436 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
437 if (error)
438 goto done;
440 error = got_ref_resolve(&id, repo, head_ref);
441 got_ref_close(head_ref);
442 if (error)
443 goto done;
445 error = got_object_open_as_commit(&commit, repo, id);
446 } else {
447 struct got_reference *ref;
448 error = got_ref_open(&ref, repo, start_commit, 0);
449 if (error == NULL) {
450 int obj_type;
451 error = got_ref_resolve(&id, repo, ref);
452 got_ref_close(ref);
453 if (error)
454 goto done;
455 error = got_object_get_type(&obj_type, repo, id);
456 if (error)
457 goto done;
458 if (obj_type == GOT_OBJ_TYPE_TAG) {
459 struct got_tag_object *tag;
460 error = got_object_open_as_tag(&tag, repo, id);
461 if (error)
462 goto done;
463 if (got_object_tag_get_object_type(tag) !=
464 GOT_OBJ_TYPE_COMMIT) {
465 got_object_tag_close(tag);
466 error = got_error(GOT_ERR_OBJ_TYPE);
467 goto done;
469 free(id);
470 id = got_object_id_dup(
471 got_object_tag_get_object_id(tag));
472 if (id == NULL)
473 error = got_error_from_errno(
474 "got_object_id_dup");
475 got_object_tag_close(tag);
476 if (error)
477 goto done;
478 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
479 error = got_error(GOT_ERR_OBJ_TYPE);
480 goto done;
482 error = got_object_open_as_commit(&commit, repo, id);
483 if (error)
484 goto done;
486 if (commit == NULL) {
487 error = got_repo_match_object_id_prefix(&id,
488 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
489 if (error)
490 goto done;
492 error = got_repo_match_object_id_prefix(&id,
493 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
494 if (error)
495 goto done;
498 error = got_object_open_as_commit(&commit, repo, id);
499 if (error)
500 goto done;
502 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
503 if (error)
504 goto done;
506 if (in_repo_path) {
507 free(path);
508 path = in_repo_path;
511 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
512 if (error)
513 goto done;
515 error = got_commit_graph_open(&graph, path, 0);
516 if (error)
517 goto done;
519 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
520 if (error)
521 goto done;
523 for (;;) {
524 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
525 NULL);
526 if (error) {
527 if (error->code == GOT_ERR_ITER_COMPLETED)
528 error = NULL;
529 break;
531 if (id == NULL)
532 break;
534 if (error)
535 break;
536 log_count++;
538 done:
539 free(in_repo_path);
540 if (graph)
541 got_commit_graph_close(graph);
542 if (repo) {
543 error = got_repo_close(repo);
544 if (error)
545 return 0;
547 if (error) {
548 khttp_puts(gw_trans->gw_req, "Error: ");
549 khttp_puts(gw_trans->gw_req, error->msg);
550 return 0;
551 } else
552 return log_count;
555 static const struct got_error *
556 gw_blame(struct trans *gw_trans)
558 const struct got_error *error = NULL;
560 char *log, *log_html;
562 error = apply_unveil(gw_trans->gw_dir->path, NULL);
563 if (error)
564 return error;
566 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGBLAME);
568 if (log != NULL && strcmp(log, "") != 0) {
569 if ((asprintf(&log_html, log_blame, log)) == -1)
570 return got_error_from_errno("asprintf");
571 khttp_puts(gw_trans->gw_req, log_html);
572 free(log_html);
573 free(log);
575 return error;
578 static const struct got_error *
579 gw_commit(struct trans *gw_trans)
581 const struct got_error *error = NULL;
582 char *log, *log_html;
584 error = apply_unveil(gw_trans->gw_dir->path, NULL);
585 if (error)
586 return error;
588 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
590 if (log != NULL && strcmp(log, "") != 0) {
591 if ((asprintf(&log_html, log_commit, log)) == -1)
592 return got_error_from_errno("asprintf");
593 khttp_puts(gw_trans->gw_req, log_html);
594 free(log_html);
595 free(log);
597 return error;
600 static const struct got_error *
601 gw_commitdiff(struct trans *gw_trans)
603 const struct got_error *error = NULL;
604 char *log, *log_html;
606 error = apply_unveil(gw_trans->gw_dir->path, NULL);
607 if (error)
608 return error;
610 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
612 if (log != NULL && strcmp(log, "") != 0) {
613 if ((asprintf(&log_html, log_diff, log)) == -1)
614 return got_error_from_errno("asprintf");
615 khttp_puts(gw_trans->gw_req, log_html);
616 free(log_html);
617 free(log);
619 return error;
622 static const struct got_error *
623 gw_index(struct trans *gw_trans)
625 const struct got_error *error = NULL;
626 struct gw_dir *gw_dir = NULL;
627 char *html, *navs, *next, *prev;
628 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
630 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
631 if (error)
632 return error;
634 error = gw_load_got_paths(gw_trans);
635 if (error)
636 return error;
638 khttp_puts(gw_trans->gw_req, index_projects_header);
640 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
641 dir_c++;
643 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
644 if (gw_trans->page > 0 && (gw_trans->page *
645 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
646 prev_disp++;
647 continue;
650 prev_disp++;
651 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
652 gw_dir->name, gw_dir->name)) == -1)
653 return got_error_from_errno("asprintf");
655 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
656 gw_dir->description, gw_dir->owner, gw_dir->age,
657 navs)) == -1)
658 return got_error_from_errno("asprintf");
660 khttp_puts(gw_trans->gw_req, html);
662 free(navs);
663 free(html);
665 if (gw_trans->gw_conf->got_max_repos_display == 0)
666 continue;
668 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
669 khttp_puts(gw_trans->gw_req, np_wrapper_start);
670 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
671 (gw_trans->page > 0) &&
672 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
673 prev_disp == gw_trans->repos_total))
674 khttp_puts(gw_trans->gw_req, np_wrapper_start);
676 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
677 (gw_trans->page > 0) &&
678 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
679 prev_disp == gw_trans->repos_total)) {
680 if ((asprintf(&prev, nav_prev,
681 gw_trans->page - 1)) == -1)
682 return got_error_from_errno("asprintf");
683 khttp_puts(gw_trans->gw_req, prev);
684 free(prev);
687 khttp_puts(gw_trans->gw_req, div_end);
689 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
690 next_disp == gw_trans->gw_conf->got_max_repos_display &&
691 dir_c != (gw_trans->page + 1) *
692 gw_trans->gw_conf->got_max_repos_display) {
693 if ((asprintf(&next, nav_next,
694 gw_trans->page + 1)) == -1)
695 return got_error_from_errno("calloc");
696 khttp_puts(gw_trans->gw_req, next);
697 khttp_puts(gw_trans->gw_req, div_end);
698 free(next);
699 next_disp = 0;
700 break;
703 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
704 (gw_trans->page > 0) &&
705 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
706 prev_disp == gw_trans->repos_total))
707 khttp_puts(gw_trans->gw_req, div_end);
709 next_disp++;
711 return error;
714 static const struct got_error *
715 gw_log(struct trans *gw_trans)
717 const struct got_error *error = NULL;
718 char *log, *log_html;
720 error = apply_unveil(gw_trans->gw_dir->path, NULL);
721 if (error)
722 return error;
724 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
725 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
727 if (log != NULL && strcmp(log, "") != 0) {
728 if ((asprintf(&log_html, logs, log)) == -1)
729 return got_error_from_errno("asprintf");
730 khttp_puts(gw_trans->gw_req, log_html);
731 free(log_html);
732 free(log);
734 return error;
737 static const struct got_error *
738 gw_raw(struct trans *gw_trans)
740 const struct got_error *error = NULL;
742 return error;
745 static const struct got_error *
746 gw_logbriefs(struct trans *gw_trans)
748 const struct got_error *error = NULL;
749 char *log, *log_html;
751 error = apply_unveil(gw_trans->gw_dir->path, NULL);
752 if (error)
753 return error;
755 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
756 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
758 if (log != NULL && strcmp(log, "") != 0) {
759 if ((asprintf(&log_html, summary_logbriefs,
760 log)) == -1)
761 return got_error_from_errno("asprintf");
762 khttp_puts(gw_trans->gw_req, log_html);
763 free(log_html);
764 free(log);
766 return error;
769 static const struct got_error *
770 gw_summary(struct trans *gw_trans)
772 const struct got_error *error = NULL;
773 char *description_html, *repo_owner_html, *repo_age_html,
774 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
775 *heads_html, *age;
777 error = apply_unveil(gw_trans->gw_dir->path, NULL);
778 if (error)
779 return error;
781 khttp_puts(gw_trans->gw_req, summary_wrapper);
782 if (gw_trans->gw_conf->got_show_repo_description) {
783 if (gw_trans->gw_dir->description != NULL &&
784 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
785 if ((asprintf(&description_html, description,
786 gw_trans->gw_dir->description)) == -1)
787 return got_error_from_errno("asprintf");
789 khttp_puts(gw_trans->gw_req, description_html);
790 free(description_html);
794 if (gw_trans->gw_conf->got_show_repo_owner) {
795 if (gw_trans->gw_dir->owner != NULL &&
796 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
797 if ((asprintf(&repo_owner_html, repo_owner,
798 gw_trans->gw_dir->owner)) == -1)
799 return got_error_from_errno("asprintf");
801 khttp_puts(gw_trans->gw_req, repo_owner_html);
802 free(repo_owner_html);
806 if (gw_trans->gw_conf->got_show_repo_age) {
807 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
808 "refs/heads", TM_LONG);
809 if (age != NULL && (strcmp(age, "") != 0)) {
810 if ((asprintf(&repo_age_html, last_change, age)) == -1)
811 return got_error_from_errno("asprintf");
813 khttp_puts(gw_trans->gw_req, repo_age_html);
814 free(repo_age_html);
815 free(age);
819 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
820 if (gw_trans->gw_dir->url != NULL &&
821 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
822 if ((asprintf(&cloneurl_html, cloneurl,
823 gw_trans->gw_dir->url)) == -1)
824 return got_error_from_errno("asprintf");
826 khttp_puts(gw_trans->gw_req, cloneurl_html);
827 free(cloneurl_html);
830 khttp_puts(gw_trans->gw_req, div_end);
832 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
833 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
834 heads = gw_get_repo_heads(gw_trans);
836 if (log != NULL && strcmp(log, "") != 0) {
837 if ((asprintf(&log_html, summary_logbriefs,
838 log)) == -1)
839 return got_error_from_errno("asprintf");
840 khttp_puts(gw_trans->gw_req, log_html);
841 free(log_html);
842 free(log);
845 if (tags != NULL && strcmp(tags, "") != 0) {
846 if ((asprintf(&tags_html, summary_tags,
847 tags)) == -1)
848 return got_error_from_errno("asprintf");
849 khttp_puts(gw_trans->gw_req, tags_html);
850 free(tags_html);
851 free(tags);
854 if (heads != NULL && strcmp(heads, "") != 0) {
855 if ((asprintf(&heads_html, summary_heads,
856 heads)) == -1)
857 return got_error_from_errno("asprintf");
858 khttp_puts(gw_trans->gw_req, heads_html);
859 free(heads_html);
860 free(heads);
862 return error;
865 static const struct got_error *
866 gw_tag(struct trans *gw_trans)
868 const struct got_error *error = NULL;
869 char *log, *log_html;
871 error = apply_unveil(gw_trans->gw_dir->path, NULL);
872 if (error)
873 return error;
875 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTAG);
877 if (log != NULL && strcmp(log, "") != 0) {
878 if ((asprintf(&log_html, log_tag, log)) == -1)
879 return got_error_from_errno("asprintf");
880 khttp_puts(gw_trans->gw_req, log_html);
881 free(log_html);
882 free(log);
884 return error;
887 static const struct got_error *
888 gw_tree(struct trans *gw_trans)
890 const struct got_error *error = NULL;
891 char *log, *log_html;
893 error = apply_unveil(gw_trans->gw_dir->path, NULL);
894 if (error)
895 return error;
897 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
899 if (log != NULL && strcmp(log, "") != 0) {
900 if ((asprintf(&log_html, log_tree, log)) == -1)
901 return got_error_from_errno("asprintf");
902 khttp_puts(gw_trans->gw_req, log_html);
903 free(log_html);
904 free(log);
906 return error;
909 static const struct got_error *
910 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
912 const struct got_error *error = NULL;
913 DIR *dt;
914 char *dir_test;
915 int opened = 0;
917 if ((asprintf(&dir_test, "%s/%s/%s",
918 gw_trans->gw_conf->got_repos_path, gw_dir->name,
919 GOTWEB_GIT_DIR)) == -1)
920 return got_error_from_errno("asprintf");
922 dt = opendir(dir_test);
923 if (dt == NULL) {
924 free(dir_test);
925 } else {
926 gw_dir->path = strdup(dir_test);
927 opened = 1;
928 goto done;
931 if ((asprintf(&dir_test, "%s/%s/%s",
932 gw_trans->gw_conf->got_repos_path, gw_dir->name,
933 GOTWEB_GOT_DIR)) == -1)
934 return got_error_from_errno("asprintf");
936 dt = opendir(dir_test);
937 if (dt == NULL)
938 free(dir_test);
939 else {
940 opened = 1;
941 error = got_error(GOT_ERR_NOT_GIT_REPO);
942 goto errored;
945 if ((asprintf(&dir_test, "%s/%s",
946 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
947 return got_error_from_errno("asprintf");
949 gw_dir->path = strdup(dir_test);
951 done:
952 gw_dir->description = gw_get_repo_description(gw_trans,
953 gw_dir->path);
954 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
955 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
956 TM_DIFF);
957 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
959 errored:
960 free(dir_test);
961 if (opened)
962 closedir(dt);
963 return error;
966 static const struct got_error *
967 gw_load_got_paths(struct trans *gw_trans)
969 const struct got_error *error = NULL;
970 DIR *d;
971 struct dirent **sd_dent;
972 struct gw_dir *gw_dir;
973 struct stat st;
974 unsigned int d_cnt, d_i;
976 d = opendir(gw_trans->gw_conf->got_repos_path);
977 if (d == NULL) {
978 error = got_error_from_errno2("opendir",
979 gw_trans->gw_conf->got_repos_path);
980 return error;
983 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
984 alphasort);
985 if (d_cnt == -1) {
986 error = got_error_from_errno2("scandir",
987 gw_trans->gw_conf->got_repos_path);
988 return error;
991 for (d_i = 0; d_i < d_cnt; d_i++) {
992 if (gw_trans->gw_conf->got_max_repos > 0 &&
993 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
994 break; /* account for parent and self */
996 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
997 strcmp(sd_dent[d_i]->d_name, "..") == 0)
998 continue;
1000 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1001 return got_error_from_errno("gw_dir malloc");
1003 error = gw_load_got_path(gw_trans, gw_dir);
1004 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1005 continue;
1006 else if (error)
1007 return error;
1009 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1010 !got_path_dir_is_empty(gw_dir->path)) {
1011 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1012 entry);
1013 gw_trans->repos_total++;
1017 closedir(d);
1018 return error;
1021 static const struct got_error *
1022 gw_parse_querystring(struct trans *gw_trans)
1024 const struct got_error *error = NULL;
1025 struct kpair *p;
1026 struct gw_query_action *action = NULL;
1027 unsigned int i;
1029 if (gw_trans->gw_req->fieldnmap[0]) {
1030 error = got_error_from_errno("bad parse");
1031 return error;
1032 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1033 /* define gw_trans->repo_path */
1034 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
1035 return got_error_from_errno("asprintf");
1037 if ((asprintf(&gw_trans->repo_path, "%s/%s",
1038 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
1039 return got_error_from_errno("asprintf");
1041 /* get action and set function */
1042 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1043 for (i = 0; i < nitems(gw_query_funcs); i++) {
1044 action = &gw_query_funcs[i];
1045 if (action->func_name == NULL)
1046 continue;
1048 if (strcmp(action->func_name,
1049 p->parsed.s) == 0) {
1050 gw_trans->action = i;
1051 if ((asprintf(&gw_trans->action_name,
1052 "%s", action->func_name)) == -1)
1053 return
1054 got_error_from_errno(
1055 "asprintf");
1057 break;
1060 action = NULL;
1063 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1064 if ((asprintf(&gw_trans->commit, "%s",
1065 p->parsed.s)) == -1)
1066 return got_error_from_errno("asprintf");
1068 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1069 if ((asprintf(&gw_trans->repo_file, "%s",
1070 p->parsed.s)) == -1)
1071 return got_error_from_errno("asprintf");
1073 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1074 if ((asprintf(&gw_trans->repo_folder, "%s",
1075 p->parsed.s)) == -1)
1076 return got_error_from_errno("asprintf");
1078 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1079 if ((asprintf(&gw_trans->headref, "%s",
1080 p->parsed.s)) == -1)
1081 return got_error_from_errno("asprintf");
1083 if (action == NULL) {
1084 error = got_error_from_errno("invalid action");
1085 return error;
1087 if ((gw_trans->gw_dir =
1088 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1089 return got_error_from_errno("gw_dir malloc");
1091 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1092 if (error)
1093 return error;
1094 } else
1095 gw_trans->action = GW_INDEX;
1097 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1098 gw_trans->page = p->parsed.i;
1100 if (gw_trans->action == GW_RAW)
1101 gw_trans->mime = KMIME_TEXT_PLAIN;
1103 return error;
1106 static struct gw_dir *
1107 gw_init_gw_dir(char *dir)
1109 struct gw_dir *gw_dir;
1111 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1112 return NULL;
1114 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1115 return NULL;
1117 return gw_dir;
1120 static const struct got_error*
1121 match_logmsg(int *have_match, struct got_object_id *id,
1122 struct got_commit_object *commit, regex_t *regex)
1124 const struct got_error *err = NULL;
1125 regmatch_t regmatch;
1126 char *id_str = NULL, *logmsg = NULL;
1128 *have_match = 0;
1130 err = got_object_id_str(&id_str, id);
1131 if (err)
1132 return err;
1134 err = got_object_commit_get_logmsg(&logmsg, commit);
1135 if (err)
1136 goto done;
1138 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1139 *have_match = 1;
1140 done:
1141 free(id_str);
1142 free(logmsg);
1143 return err;
1146 static void
1147 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
1149 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1150 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1151 khttps[code]);
1152 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1153 kmimetypes[mime]);
1154 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1155 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1156 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1157 khttp_body(gw_trans->gw_req);
1160 static void
1161 gw_display_index(struct trans *gw_trans, const struct got_error *err)
1163 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1164 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1166 if (err)
1167 khttp_puts(gw_trans->gw_req, err->msg);
1168 else
1169 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1170 gw_query_funcs[gw_trans->action].template);
1172 khtml_close(gw_trans->gw_html_req);
1175 static int
1176 gw_template(size_t key, void *arg)
1178 const struct got_error *error = NULL;
1179 struct trans *gw_trans = arg;
1180 char *gw_got_link, *gw_site_link;
1181 char *site_owner_name, *site_owner_name_h;
1183 switch (key) {
1184 case (TEMPL_HEAD):
1185 khttp_puts(gw_trans->gw_req, head);
1186 break;
1187 case(TEMPL_HEADER):
1188 gw_got_link = gw_get_got_link(gw_trans);
1189 if (gw_got_link != NULL)
1190 khttp_puts(gw_trans->gw_req, gw_got_link);
1192 free(gw_got_link);
1193 break;
1194 case (TEMPL_SITEPATH):
1195 gw_site_link = gw_get_site_link(gw_trans);
1196 if (gw_site_link != NULL)
1197 khttp_puts(gw_trans->gw_req, gw_site_link);
1199 free(gw_site_link);
1200 break;
1201 case(TEMPL_TITLE):
1202 if (gw_trans->gw_conf->got_site_name != NULL)
1203 khtml_puts(gw_trans->gw_html_req,
1204 gw_trans->gw_conf->got_site_name);
1206 break;
1207 case (TEMPL_SEARCH):
1208 khttp_puts(gw_trans->gw_req, search);
1209 break;
1210 case(TEMPL_SITEOWNER):
1211 if (gw_trans->gw_conf->got_site_owner != NULL &&
1212 gw_trans->gw_conf->got_show_site_owner) {
1213 site_owner_name =
1214 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1215 if ((asprintf(&site_owner_name_h, site_owner,
1216 site_owner_name))
1217 == -1)
1218 return 0;
1220 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1221 free(site_owner_name);
1222 free(site_owner_name_h);
1224 break;
1225 case(TEMPL_CONTENT):
1226 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1227 if (error)
1228 khttp_puts(gw_trans->gw_req, error->msg);
1230 break;
1231 default:
1232 return 0;
1233 break;
1235 return 1;
1238 static char *
1239 gw_get_repo_description(struct trans *gw_trans, char *dir)
1241 FILE *f;
1242 char *description = NULL, *d_file = NULL;
1243 unsigned int len;
1245 if (gw_trans->gw_conf->got_show_repo_description == false)
1246 goto err;
1248 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1249 goto err;
1251 if ((f = fopen(d_file, "r")) == NULL)
1252 goto err;
1254 fseek(f, 0, SEEK_END);
1255 len = ftell(f) + 1;
1256 fseek(f, 0, SEEK_SET);
1257 if ((description = calloc(len, sizeof(char *))) == NULL)
1258 goto err;
1260 fread(description, 1, len, f);
1261 fclose(f);
1262 free(d_file);
1263 return description;
1264 err:
1265 if ((asprintf(&description, "%s", "")) == -1)
1266 return NULL;
1268 return description;
1271 static char *
1272 gw_get_time_str(time_t committer_time, int ref_tm)
1274 struct tm tm;
1275 time_t diff_time;
1276 char *years = "years ago", *months = "months ago";
1277 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1278 char *minutes = "minutes ago", *seconds = "seconds ago";
1279 char *now = "right now";
1280 char *repo_age, *s;
1281 char datebuf[29];
1283 switch (ref_tm) {
1284 case TM_DIFF:
1285 diff_time = time(NULL) - committer_time;
1286 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1287 if ((asprintf(&repo_age, "%lld %s",
1288 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1289 return NULL;
1290 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1291 if ((asprintf(&repo_age, "%lld %s",
1292 (diff_time / 60 / 60 / 24 / (365 / 12)),
1293 months)) == -1)
1294 return NULL;
1295 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1296 if ((asprintf(&repo_age, "%lld %s",
1297 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1298 return NULL;
1299 } else if (diff_time > 60 * 60 * 24 * 2) {
1300 if ((asprintf(&repo_age, "%lld %s",
1301 (diff_time / 60 / 60 / 24), days)) == -1)
1302 return NULL;
1303 } else if (diff_time > 60 * 60 * 2) {
1304 if ((asprintf(&repo_age, "%lld %s",
1305 (diff_time / 60 / 60), hours)) == -1)
1306 return NULL;
1307 } else if (diff_time > 60 * 2) {
1308 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1309 minutes)) == -1)
1310 return NULL;
1311 } else if (diff_time > 2) {
1312 if ((asprintf(&repo_age, "%lld %s", diff_time,
1313 seconds)) == -1)
1314 return NULL;
1315 } else {
1316 if ((asprintf(&repo_age, "%s", now)) == -1)
1317 return NULL;
1319 break;
1320 case TM_LONG:
1321 if (gmtime_r(&committer_time, &tm) == NULL)
1322 return NULL;
1324 s = asctime_r(&tm, datebuf);
1325 if (s == NULL)
1326 return NULL;
1328 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1329 return NULL;
1330 break;
1332 return repo_age;
1335 static char *
1336 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
1338 const struct got_error *error = NULL;
1339 struct got_object_id *id = NULL;
1340 struct got_repository *repo = NULL;
1341 struct got_commit_object *commit = NULL;
1342 struct got_reflist_head refs;
1343 struct got_reflist_entry *re;
1344 struct got_reference *head_ref;
1345 int is_head = 0;
1346 time_t committer_time = 0, cmp_time = 0;
1347 const char *refname;
1348 char *repo_age = NULL;
1350 if (repo_ref == NULL)
1351 return NULL;
1353 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1354 is_head = 1;
1356 SIMPLEQ_INIT(&refs);
1357 if (gw_trans->gw_conf->got_show_repo_age == false) {
1358 if ((asprintf(&repo_age, "")) == -1)
1359 return NULL;
1360 return repo_age;
1363 error = got_repo_open(&repo, dir, NULL);
1364 if (error)
1365 goto err;
1367 if (is_head)
1368 error = got_ref_list(&refs, repo, "refs/heads",
1369 got_ref_cmp_by_name, NULL);
1370 else
1371 error = got_ref_list(&refs, repo, repo_ref,
1372 got_ref_cmp_by_name, NULL);
1373 if (error)
1374 goto err;
1376 SIMPLEQ_FOREACH(re, &refs, entry) {
1377 if (is_head)
1378 refname = strdup(repo_ref);
1379 else
1380 refname = got_ref_get_name(re->ref);
1381 error = got_ref_open(&head_ref, repo, refname, 0);
1382 if (error)
1383 goto err;
1385 error = got_ref_resolve(&id, repo, head_ref);
1386 got_ref_close(head_ref);
1387 if (error)
1388 goto err;
1390 error = got_object_open_as_commit(&commit, repo, id);
1391 if (error)
1392 goto err;
1394 committer_time =
1395 got_object_commit_get_committer_time(commit);
1397 if (cmp_time < committer_time)
1398 cmp_time = committer_time;
1401 if (cmp_time != 0) {
1402 committer_time = cmp_time;
1403 repo_age = gw_get_time_str(committer_time, ref_tm);
1404 } else
1405 if ((asprintf(&repo_age, "")) == -1)
1406 return NULL;
1407 got_ref_list_free(&refs);
1408 free(id);
1409 return repo_age;
1410 err:
1411 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1412 return NULL;
1414 return repo_age;
1417 static char *
1418 gw_get_repo_diff(struct trans *gw_trans, char *id_str1, char *id_str2)
1420 const struct got_error *error;
1421 FILE *f = NULL;
1422 struct got_object_id *id1 = NULL, *id2 = NULL;
1423 struct got_repository *repo = NULL;
1424 struct buf *diffbuf = NULL;
1425 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1426 *buf_color = NULL;
1427 int type1, type2;
1428 size_t newsize;
1430 f = got_opentemp();
1431 if (f == NULL)
1432 return NULL;
1434 error = buf_alloc(&diffbuf, 0);
1435 if (error)
1436 return NULL;
1438 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1439 if (error)
1440 goto done;
1442 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
1443 repo);
1444 if (error)
1445 goto done;
1447 if (id_str2) {
1448 error = match_object_id(&id2, &label2, id_str2,
1449 GOT_OBJ_TYPE_ANY, 1, repo);
1450 if (error)
1451 goto done;
1453 error = got_object_get_type(&type2, repo, id2);
1454 if (error)
1455 goto done;
1458 error = got_object_get_type(&type1, repo, id1);
1459 if (error)
1460 goto done;
1462 if (id_str2 && type1 != type2) {
1463 error = got_error(GOT_ERR_OBJ_TYPE);
1464 goto done;
1467 switch (type1) {
1468 case GOT_OBJ_TYPE_BLOB:
1469 error = got_diff_objects_as_blobs(id2, id1, NULL, NULL, 3, 0,
1470 repo, f);
1471 break;
1472 case GOT_OBJ_TYPE_TREE:
1473 error = got_diff_objects_as_trees(id2, id1, "", "", 3, 0, repo,
1474 f);
1475 break;
1476 case GOT_OBJ_TYPE_COMMIT:
1477 error = got_diff_objects_as_commits(id2, id1, 3, 0, repo, f);
1478 break;
1479 default:
1480 error = got_error(GOT_ERR_OBJ_TYPE);
1483 if ((buf = calloc(128, sizeof(char *))) == NULL)
1484 goto done;
1486 fseek(f, 0, SEEK_SET);
1488 while ((fgets(buf, 128, f)) != NULL) {
1489 buf_color = color_diff_line(buf);
1490 error = buf_puts(&newsize, diffbuf, buf_color);
1491 if (error)
1492 return NULL;
1494 error = buf_puts(&newsize, diffbuf, div_end);
1495 if (error)
1496 return NULL;
1499 if (buf_len(diffbuf) > 0) {
1500 error = buf_putc(diffbuf, '\0');
1501 diff_html = strdup(buf_get(diffbuf));
1503 done:
1504 fclose(f);
1505 free(buf_color);
1506 free(buf);
1507 free(diffbuf);
1508 free(label1);
1509 free(label2);
1510 free(id1);
1511 free(id2);
1512 if (repo)
1513 got_repo_close(repo);
1515 if (error)
1516 return NULL;
1517 else
1518 return diff_html;
1521 static char *
1522 gw_get_repo_owner(struct trans *gw_trans, char *dir)
1524 FILE *f;
1525 char *owner = NULL, *d_file = NULL;
1526 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1527 char *comp, *pos, *buf;
1528 unsigned int i;
1530 if (gw_trans->gw_conf->got_show_repo_owner == false)
1531 goto err;
1533 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1534 goto err;
1536 if ((f = fopen(d_file, "r")) == NULL)
1537 goto err;
1539 if ((buf = calloc(128, sizeof(char *))) == NULL)
1540 goto err;
1542 while ((fgets(buf, 128, f)) != NULL) {
1543 if ((pos = strstr(buf, gotweb)) != NULL)
1544 break;
1546 if ((pos = strstr(buf, gitweb)) != NULL)
1547 break;
1550 if (pos == NULL)
1551 goto err;
1553 do {
1554 fgets(buf, 128, f);
1555 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1557 if (comp == NULL)
1558 goto err;
1560 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1561 goto err;
1563 for (i = 0; i < 2; i++) {
1564 owner = strsep(&buf, "\"");
1567 if (owner == NULL)
1568 goto err;
1570 fclose(f);
1571 free(d_file);
1572 return owner;
1573 err:
1574 if ((asprintf(&owner, "%s", "")) == -1)
1575 return NULL;
1577 return owner;
1580 static char *
1581 gw_get_clone_url(struct trans *gw_trans, char *dir)
1583 FILE *f;
1584 char *url = NULL, *d_file = NULL;
1585 unsigned int len;
1587 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1588 return NULL;
1590 if ((f = fopen(d_file, "r")) == NULL)
1591 return NULL;
1593 fseek(f, 0, SEEK_END);
1594 len = ftell(f) + 1;
1595 fseek(f, 0, SEEK_SET);
1597 if ((url = calloc(len, sizeof(char *))) == NULL)
1598 return NULL;
1600 fread(url, 1, len, f);
1601 fclose(f);
1602 free(d_file);
1603 return url;
1606 static char *
1607 gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1608 char *start_commit, int limit, int log_type)
1610 const struct got_error *error;
1611 struct got_repository *repo = NULL;
1612 struct got_reflist_head refs;
1613 struct got_reflist_entry *re;
1614 struct got_commit_object *commit = NULL;
1615 struct got_object_id *id1 = NULL, *id2 = NULL;
1616 struct got_object_qid *parent_id;
1617 struct got_commit_graph *graph = NULL;
1618 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1619 *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1620 *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1621 *commit_commit_disp = NULL, *commit_age_diff = NULL,
1622 *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1623 *commit_age_long_disp = NULL, *commit_author = NULL,
1624 *commit_author_disp = NULL, *commit_committer = NULL,
1625 *commit_committer_disp = NULL, *commit_log = NULL,
1626 *commit_log_disp = NULL, *commit_parent = NULL,
1627 *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1628 *log_tree_html = NULL, *log_commit_html = NULL,
1629 *log_diff_html = NULL, *commit_tree = NULL,
1630 *commit_tree_disp = NULL, *log_tag_html = NULL,
1631 *log_blame_html = NULL;
1632 char *commit_log0, *newline;
1633 regex_t regex;
1634 int have_match, log_count = 0, has_parent = 1;
1635 size_t newsize;
1636 struct buf *diffbuf = NULL;
1637 time_t committer_time;
1639 if (gw_trans->action == GW_LOG || gw_trans->action == GW_LOGBRIEFS)
1640 log_count = gw_get_repo_log_count(gw_trans, start_commit);
1642 error = buf_alloc(&diffbuf, 0);
1643 if (error)
1644 return NULL;
1646 if (search_pattern &&
1647 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB |
1648 REG_NEWLINE))
1649 return NULL;
1651 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1652 if (error)
1653 return NULL;
1655 SIMPLEQ_INIT(&refs);
1657 if (start_commit == NULL) {
1658 struct got_reference *head_ref;
1659 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1660 if (error)
1661 goto done;
1663 error = got_ref_resolve(&id1, repo, head_ref);
1664 got_ref_close(head_ref);
1665 if (error)
1666 goto done;
1668 error = got_object_open_as_commit(&commit, repo, id1);
1669 } else {
1670 struct got_reference *ref;
1671 error = got_ref_open(&ref, repo, start_commit, 0);
1672 if (error == NULL) {
1673 int obj_type;
1674 error = got_ref_resolve(&id1, repo, ref);
1675 got_ref_close(ref);
1676 if (error)
1677 goto done;
1678 error = got_object_get_type(&obj_type, repo, id1);
1679 if (error)
1680 goto done;
1681 if (obj_type == GOT_OBJ_TYPE_TAG) {
1682 struct got_tag_object *tag;
1683 error = got_object_open_as_tag(&tag, repo, id1);
1684 if (error)
1685 goto done;
1686 if (got_object_tag_get_object_type(tag) !=
1687 GOT_OBJ_TYPE_COMMIT) {
1688 got_object_tag_close(tag);
1689 error = got_error(GOT_ERR_OBJ_TYPE);
1690 goto done;
1692 free(id1);
1693 id1 = got_object_id_dup(
1694 got_object_tag_get_object_id(tag));
1695 if (id1 == NULL)
1696 error = got_error_from_errno(
1697 "got_object_id_dup");
1698 got_object_tag_close(tag);
1699 if (error)
1700 goto done;
1701 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1702 error = got_error(GOT_ERR_OBJ_TYPE);
1703 goto done;
1705 error = got_object_open_as_commit(&commit, repo, id1);
1706 if (error)
1707 goto done;
1709 if (commit == NULL) {
1710 error = got_repo_match_object_id_prefix(&id1,
1711 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1712 if (error)
1713 goto done;
1715 error = got_repo_match_object_id_prefix(&id1,
1716 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1719 if (error)
1720 goto done;
1722 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1723 if (error)
1724 goto done;
1726 if (in_repo_path) {
1727 free(path);
1728 path = in_repo_path;
1731 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1732 if (error)
1733 goto done;
1735 error = got_commit_graph_open(&graph, path, 0);
1736 if (error)
1737 goto done;
1739 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1740 if (error)
1741 goto done;
1743 for (;;) {
1744 error = got_commit_graph_iter_next(&id1, graph, repo, NULL,
1745 NULL);
1746 if (error) {
1747 if (error->code == GOT_ERR_ITER_COMPLETED)
1748 error = NULL;
1749 break;
1751 if (id1 == NULL)
1752 break;
1754 error = got_object_open_as_commit(&commit, repo, id1);
1755 if (error)
1756 break;
1758 if (search_pattern) {
1759 error = match_logmsg(&have_match, id1, commit,
1760 &regex);
1761 if (error) {
1762 got_object_commit_close(commit);
1763 break;
1765 if (have_match == 0) {
1766 got_object_commit_close(commit);
1767 continue;
1771 SIMPLEQ_FOREACH(re, &refs, entry) {
1772 char *s;
1773 const char *name;
1774 struct got_tag_object *tag = NULL;
1775 int cmp;
1777 name = got_ref_get_name(re->ref);
1778 if (strcmp(name, GOT_REF_HEAD) == 0)
1779 continue;
1780 if (strncmp(name, "refs/", 5) == 0)
1781 name += 5;
1782 if (strncmp(name, "got/", 4) == 0)
1783 continue;
1784 if (strncmp(name, "heads/", 6) == 0)
1785 name += 6;
1786 if (strncmp(name, "remotes/", 8) == 0)
1787 name += 8;
1788 if (strncmp(name, "tags/", 5) == 0) {
1789 error = got_object_open_as_tag(&tag, repo,
1790 re->id);
1791 if (error) {
1792 if (error->code != GOT_ERR_OBJ_TYPE)
1793 continue;
1795 * Ref points at something other
1796 * than a tag.
1798 error = NULL;
1799 tag = NULL;
1802 cmp = got_object_id_cmp(tag ?
1803 got_object_tag_get_object_id(tag) : re->id, id1);
1804 if (tag)
1805 got_object_tag_close(tag);
1806 if (cmp != 0)
1807 continue;
1808 s = refs_str;
1809 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1810 s ? ", " : "", name)) == -1) {
1811 error = got_error_from_errno("asprintf");
1812 free(s);
1813 goto done;
1815 free(s);
1818 if (refs_str == NULL)
1819 refs_str_disp = strdup("");
1820 else {
1821 if ((asprintf(&refs_str_disp, "(%s)",
1822 refs_str)) == -1) {
1823 error = got_error_from_errno("asprintf");
1824 free(refs_str);
1825 goto done;
1829 error = got_object_id_str(&id_str1, id1);
1830 if (error)
1831 goto done;
1833 error = got_object_id_str(&treeid,
1834 got_object_commit_get_tree_id(commit));
1835 if (error)
1836 goto done;
1838 if (gw_trans->action == GW_COMMIT ||
1839 gw_trans->action == GW_COMMITDIFF) {
1840 parent_id =
1841 SIMPLEQ_FIRST(
1842 got_object_commit_get_parent_ids(commit));
1843 if (parent_id != NULL) {
1844 id2 = got_object_id_dup(parent_id->id);
1845 free (parent_id);
1846 error = got_object_id_str(&id_str2, id2);
1847 if (error)
1848 goto done;
1849 free(id2);
1850 } else {
1851 has_parent = 0;
1852 id_str2 = strdup("/dev/null");
1856 committer_time =
1857 got_object_commit_get_committer_time(commit);
1859 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1860 error = got_error_from_errno("asprintf");
1861 goto done;
1864 if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1865 error = got_error_from_errno("asprintf");
1866 goto done;
1869 if ((asprintf(&commit_tree_disp, commit_tree_html,
1870 treeid)) == -1) {
1871 error = got_error_from_errno("asprintf");
1872 goto done;
1875 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1876 id_str1)) == -1) {
1877 error = got_error_from_errno("asprintf");
1878 goto done;
1881 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1882 error = got_error_from_errno("asprintf");
1883 goto done;
1886 if ((asprintf(&commit_commit_disp, commit_commit_html,
1887 commit_commit, refs_str_disp)) == -1) {
1888 error = got_error_from_errno("asprintf");
1889 goto done;
1892 if ((asprintf(&commit_age_long, "%s",
1893 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1894 error = got_error_from_errno("asprintf");
1895 goto done;
1898 if ((asprintf(&commit_age_long_disp, commit_age_html,
1899 commit_age_long)) == -1) {
1900 error = got_error_from_errno("asprintf");
1901 goto done;
1904 if ((asprintf(&commit_age_diff, "%s",
1905 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1906 error = got_error_from_errno("asprintf");
1907 goto done;
1910 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1911 commit_age_diff)) == -1) {
1912 error = got_error_from_errno("asprintf");
1913 goto done;
1916 if ((asprintf(&commit_author, "%s",
1917 got_object_commit_get_author(commit))) == -1) {
1918 error = got_error_from_errno("asprintf");
1919 goto done;
1922 if ((asprintf(&commit_author_disp, commit_author_html,
1923 gw_html_escape(commit_author))) == -1) {
1924 error = got_error_from_errno("asprintf");
1925 goto done;
1928 if ((asprintf(&commit_committer, "%s",
1929 got_object_commit_get_committer(commit))) == -1) {
1930 error = got_error_from_errno("asprintf");
1931 goto done;
1934 if ((asprintf(&commit_committer_disp, commit_committer_html,
1935 gw_html_escape(commit_committer))) == -1) {
1936 error = got_error_from_errno("asprintf");
1937 goto done;
1940 if (strcmp(commit_author, commit_committer) == 0) {
1941 free(commit_committer_disp);
1942 commit_committer_disp = strdup("");
1945 error = got_object_commit_get_logmsg(&commit_log0, commit);
1946 if (error)
1947 goto done;
1949 commit_log = commit_log0;
1950 while (*commit_log == '\n')
1951 commit_log++;
1953 switch(log_type) {
1954 case (LOGBRIEF):
1955 newline = strchr(commit_log, '\n');
1956 if (newline)
1957 *newline = '\0';
1959 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1960 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1961 id_str1, gw_trans->repo_name, id_str1,
1962 gw_trans->repo_name, id_str1)) == -1) {
1963 error = got_error_from_errno("asprintf");
1964 goto done;
1967 if ((asprintf(&commit_row, logbriefs_row,
1968 commit_age_diff, commit_author, commit_log,
1969 logbriefs_navs_html)) == -1) {
1970 error = got_error_from_errno("asprintf");
1971 goto done;
1974 free(logbriefs_navs_html);
1975 logbriefs_navs_html = NULL;
1976 break;
1977 case (LOGFULL):
1978 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1979 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1980 id_str1, gw_trans->repo_name, id_str1,
1981 gw_trans->repo_name, id_str1)) == -1) {
1982 error = got_error_from_errno("asprintf");
1983 goto done;
1986 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1987 commit_author_disp, commit_committer_disp,
1988 commit_age_long_disp, gw_html_escape(commit_log),
1989 logbriefs_navs_html)) == -1) {
1990 error = got_error_from_errno("asprintf");
1991 goto done;
1994 free(logbriefs_navs_html);
1995 logbriefs_navs_html = NULL;
1996 break;
1997 case (LOGTAG):
1998 log_tag_html = strdup("tag log here");
2000 if ((asprintf(&commit_row, log_tag_row,
2001 gw_html_escape(commit_log), log_tag_html)) == -1) {
2002 error = got_error_from_errno("asprintf");
2003 goto done;
2006 free(log_tag_html);
2007 break;
2008 case (LOGBLAME):
2009 log_blame_html = gw_get_file_blame(gw_trans,
2010 start_commit);
2012 if ((asprintf(&commit_row, log_blame_row,
2013 gw_html_escape(commit_log), log_blame_html)) == -1) {
2014 error = got_error_from_errno("asprintf");
2015 goto done;
2018 free(log_blame_html);
2019 break;
2020 case (LOGTREE):
2021 log_tree_html = gw_get_repo_tree(gw_trans,
2022 start_commit);
2024 if ((asprintf(&commit_row, log_tree_row,
2025 gw_html_escape(commit_log), log_tree_html)) == -1) {
2026 error = got_error_from_errno("asprintf");
2027 goto done;
2030 free(log_tree_html);
2031 break;
2032 case (LOGCOMMIT):
2033 if ((asprintf(&commit_log_disp, commit_log_html,
2034 gw_html_escape(commit_log))) == -1) {
2035 error = got_error_from_errno("asprintf");
2036 goto done;
2039 log_commit_html = strdup("commit here");
2041 if ((asprintf(&commit_row, log_commit_row,
2042 commit_diff_disp, commit_commit_disp,
2043 commit_tree_disp, commit_author_disp,
2044 commit_committer_disp, commit_age_long_disp,
2045 commit_log_disp, log_commit_html)) == -1) {
2046 error = got_error_from_errno("asprintf");
2047 goto done;
2049 free(commit_log_disp);
2050 free(log_commit_html);
2052 break;
2053 case (LOGDIFF):
2054 if ((asprintf(&commit_log_disp, commit_log_html,
2055 gw_html_escape(commit_log))) == -1) {
2056 error = got_error_from_errno("asprintf");
2057 goto done;
2060 if (has_parent)
2061 log_diff_html = gw_get_repo_diff(gw_trans,
2062 commit_commit, commit_parent);
2063 else
2064 log_diff_html = gw_get_repo_diff(gw_trans,
2065 commit_commit, NULL);
2067 if ((asprintf(&commit_row, log_diff_row,
2068 commit_diff_disp, commit_commit_disp,
2069 commit_tree_disp, commit_author_disp,
2070 commit_committer_disp, commit_age_long_disp,
2071 commit_log_disp, log_diff_html)) == -1) {
2072 error = got_error_from_errno("asprintf");
2073 goto done;
2075 free(commit_log_disp);
2076 free(log_diff_html);
2078 break;
2079 default:
2080 return NULL;
2083 error = buf_puts(&newsize, diffbuf, commit_row);
2085 free(commit_parent);
2086 free(commit_diff_disp);
2087 free(commit_tree_disp);
2088 free(commit_age_diff);
2089 free(commit_age_diff_disp);
2090 free(commit_age_long);
2091 free(commit_age_long_disp);
2092 free(commit_author);
2093 free(commit_author_disp);
2094 free(commit_committer);
2095 free(commit_committer_disp);
2096 free(commit_log0);
2097 free(commit_row);
2098 free(refs_str_disp);
2099 free(refs_str);
2100 refs_str = NULL;
2101 free(id_str1);
2102 id_str1 = NULL;
2103 free(id_str2);
2104 id_str2 = NULL;
2106 if (error || (limit && --limit == 0))
2107 break;
2110 if (error)
2111 goto done;
2113 if (buf_len(diffbuf) > 0) {
2114 error = buf_putc(diffbuf, '\0');
2115 logs = strdup(buf_get(diffbuf));
2117 done:
2118 buf_free(diffbuf);
2119 free(in_repo_path);
2120 if (commit != NULL)
2121 got_object_commit_close(commit);
2122 if (search_pattern)
2123 regfree(&regex);
2124 if (graph)
2125 got_commit_graph_close(graph);
2126 if (repo) {
2127 error = got_repo_close(repo);
2128 if (error)
2129 return NULL;
2131 if (error) {
2132 khttp_puts(gw_trans->gw_req, "Error: ");
2133 khttp_puts(gw_trans->gw_req, error->msg);
2134 return NULL;
2135 } else
2136 return logs;
2139 static char *
2140 gw_get_repo_tags(struct trans *gw_trans, int limit, int tag_type)
2142 const struct got_error *error = NULL;
2143 struct got_repository *repo = NULL;
2144 struct got_reflist_head refs;
2145 struct got_reflist_entry *re;
2146 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
2147 *age = NULL;
2148 char *newline;
2149 struct buf *diffbuf = NULL;
2150 size_t newsize;
2152 error = buf_alloc(&diffbuf, 0);
2153 if (error)
2154 return NULL;
2155 SIMPLEQ_INIT(&refs);
2157 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2158 if (error)
2159 goto done;
2161 error = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
2162 if (error)
2163 goto done;
2165 SIMPLEQ_FOREACH(re, &refs, entry) {
2166 const char *refname;
2167 char *refstr, *tag_log0, *tag_log, *id_str;
2168 time_t tagger_time;
2169 struct got_object_id *id;
2170 struct got_tag_object *tag;
2172 refname = got_ref_get_name(re->ref);
2173 if (strncmp(refname, "refs/tags/", 10) != 0)
2174 continue;
2175 refname += 10;
2176 refstr = got_ref_to_str(re->ref);
2177 if (refstr == NULL) {
2178 error = got_error_from_errno("got_ref_to_str");
2179 goto done;
2182 error = got_ref_resolve(&id, repo, re->ref);
2183 if (error)
2184 goto done;
2185 error = got_object_open_as_tag(&tag, repo, id);
2186 free(id);
2187 if (error)
2188 goto done;
2190 tagger_time = got_object_tag_get_tagger_time(tag);
2192 error = got_object_id_str(&id_str,
2193 got_object_tag_get_object_id(tag));
2194 if (error)
2195 goto done;
2197 tag_log0 = strdup(got_object_tag_get_message(tag));
2199 if (tag_log0 == NULL) {
2200 error = got_error_from_errno("strdup");
2201 goto done;
2204 tag_log = tag_log0;
2205 while (*tag_log == '\n')
2206 tag_log++;
2208 switch (tag_type) {
2209 case TAGBRIEF:
2210 newline = strchr(tag_log, '\n');
2211 if (newline)
2212 *newline = '\0';
2214 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
2215 TM_DIFF))) == -1) {
2216 error = got_error_from_errno("asprintf");
2217 goto done;
2220 if ((asprintf(&tags_navs_disp, tags_navs,
2221 gw_trans->repo_name, id_str, gw_trans->repo_name,
2222 id_str, gw_trans->repo_name, id_str,
2223 gw_trans->repo_name, id_str)) == -1) {
2224 error = got_error_from_errno("asprintf");
2225 goto done;
2228 if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
2229 tags_navs_disp)) == -1) {
2230 error = got_error_from_errno("asprintf");
2231 goto done;
2234 free(tags_navs_disp);
2235 break;
2236 case TAGFULL:
2237 break;
2238 default:
2239 break;
2242 got_object_tag_close(tag);
2244 error = buf_puts(&newsize, diffbuf, tag_row);
2246 free(id_str);
2247 free(refstr);
2248 free(age);
2249 free(tag_log0);
2250 free(tag_row);
2252 if (error || (limit && --limit == 0))
2253 break;
2256 if (buf_len(diffbuf) > 0) {
2257 error = buf_putc(diffbuf, '\0');
2258 tags = strdup(buf_get(diffbuf));
2260 done:
2261 buf_free(diffbuf);
2262 got_ref_list_free(&refs);
2263 if (repo)
2264 got_repo_close(repo);
2265 if (error)
2266 return NULL;
2267 else
2268 return tags;
2271 struct blame_line {
2272 int annotated;
2273 char *id_str;
2274 char *committer;
2275 char datebuf[11]; /* YYYY-MM-DD + NUL */
2278 struct blame_cb_args {
2279 struct blame_line *lines;
2280 int nlines;
2281 int nlines_prec;
2282 int lineno_cur;
2283 off_t *line_offsets;
2284 FILE *f;
2285 struct got_repository *repo;
2286 struct trans *gw_trans;
2287 struct buf *blamebuf;
2290 static const struct got_error *
2291 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2293 const struct got_error *err = NULL;
2294 struct blame_cb_args *a = arg;
2295 struct blame_line *bline;
2296 char *line = NULL;
2297 size_t linesize = 0, newsize;
2298 struct got_commit_object *commit = NULL;
2299 off_t offset;
2300 struct tm tm;
2301 time_t committer_time;
2303 if (nlines != a->nlines ||
2304 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2305 return got_error(GOT_ERR_RANGE);
2307 if (lineno == -1)
2308 return NULL; /* no change in this commit */
2310 /* Annotate this line. */
2311 bline = &a->lines[lineno - 1];
2312 if (bline->annotated)
2313 return NULL;
2314 err = got_object_id_str(&bline->id_str, id);
2315 if (err)
2316 return err;
2318 err = got_object_open_as_commit(&commit, a->repo, id);
2319 if (err)
2320 goto done;
2322 bline->committer = strdup(got_object_commit_get_committer(commit));
2323 if (bline->committer == NULL) {
2324 err = got_error_from_errno("strdup");
2325 goto done;
2328 committer_time = got_object_commit_get_committer_time(commit);
2329 if (localtime_r(&committer_time, &tm) == NULL)
2330 return got_error_from_errno("localtime_r");
2331 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2332 &tm) >= sizeof(bline->datebuf)) {
2333 err = got_error(GOT_ERR_NO_SPACE);
2334 goto done;
2336 bline->annotated = 1;
2338 /* Print lines annotated so far. */
2339 bline = &a->lines[a->lineno_cur - 1];
2340 if (!bline->annotated)
2341 goto done;
2343 offset = a->line_offsets[a->lineno_cur - 1];
2344 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2345 err = got_error_from_errno("fseeko");
2346 goto done;
2349 while (bline->annotated) {
2350 char *smallerthan, *at, *nl, *committer, *blame_row = NULL;
2351 size_t len;
2353 if (getline(&line, &linesize, a->f) == -1) {
2354 if (ferror(a->f))
2355 err = got_error_from_errno("getline");
2356 break;
2359 committer = bline->committer;
2360 smallerthan = strchr(committer, '<');
2361 if (smallerthan && smallerthan[1] != '\0')
2362 committer = smallerthan + 1;
2363 at = strchr(committer, '@');
2364 if (at)
2365 *at = '\0';
2366 len = strlen(committer);
2367 if (len >= 9)
2368 committer[8] = '\0';
2370 nl = strchr(line, '\n');
2371 if (nl)
2372 *nl = '\0';
2373 asprintf(&blame_row, log_blame_line, a->nlines_prec,
2374 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2375 line);
2376 a->lineno_cur++;
2377 err = buf_puts(&newsize, a->blamebuf, blame_row);
2378 if (err)
2379 return err;
2381 bline = &a->lines[a->lineno_cur - 1];
2382 free(blame_row);
2384 done:
2385 if (commit)
2386 got_object_commit_close(commit);
2387 free(line);
2388 return err;
2391 static char*
2392 gw_get_file_blame(struct trans *gw_trans, char *commit_str)
2394 const struct got_error *error = NULL;
2395 struct got_repository *repo = NULL;
2396 struct got_object_id *obj_id = NULL;
2397 struct got_object_id *commit_id = NULL;
2398 struct got_blob_object *blob = NULL;
2399 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2400 *folder = NULL;
2401 struct blame_cb_args bca;
2402 int i, obj_type;
2403 size_t filesize;
2405 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2406 if (error)
2407 goto done;
2409 if (gw_trans->repo_folder != NULL) {
2410 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2411 error = got_error_from_errno("asprintf");
2412 goto done;
2414 } else
2415 folder = strdup("");
2417 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2418 error = got_error_from_errno("asprintf");
2419 goto done;
2421 free(folder);
2423 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2424 if (error)
2425 goto done;
2427 error = resolve_commit_arg(&commit_id, commit_str, repo);
2428 if (error)
2429 goto done;
2431 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2432 if (error)
2433 goto done;
2435 if (obj_id == NULL) {
2436 error = got_error(GOT_ERR_NO_OBJ);
2437 goto done;
2440 error = got_object_get_type(&obj_type, repo, obj_id);
2441 if (error)
2442 goto done;
2444 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2445 error = got_error(GOT_ERR_OBJ_TYPE);
2446 goto done;
2449 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2450 if (error)
2451 goto done;
2453 error = buf_alloc(&bca.blamebuf, 0);
2454 if (error)
2455 goto done;
2457 bca.f = got_opentemp();
2458 if (bca.f == NULL) {
2459 error = got_error_from_errno("got_opentemp");
2460 goto done;
2462 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2463 &bca.line_offsets, bca.f, blob);
2464 if (error || bca.nlines == 0)
2465 goto done;
2467 /* Don't include \n at EOF in the blame line count. */
2468 if (bca.line_offsets[bca.nlines - 1] == filesize)
2469 bca.nlines--;
2471 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2472 if (bca.lines == NULL) {
2473 error = got_error_from_errno("calloc");
2474 goto done;
2476 bca.lineno_cur = 1;
2477 bca.nlines_prec = 0;
2478 i = bca.nlines;
2479 while (i > 0) {
2480 i /= 10;
2481 bca.nlines_prec++;
2483 bca.repo = repo;
2484 bca.gw_trans = gw_trans;
2486 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca, NULL,
2487 NULL);
2488 if (buf_len(bca.blamebuf) > 0) {
2489 error = buf_putc(bca.blamebuf, '\0');
2490 blame_html = strdup(buf_get(bca.blamebuf));
2492 done:
2493 free(bca.blamebuf);
2494 free(in_repo_path);
2495 free(commit_id);
2496 free(obj_id);
2497 free(path);
2499 if (blob)
2500 error = got_object_blob_close(blob);
2501 if (repo)
2502 error = got_repo_close(repo);
2503 if (error)
2504 return NULL;
2505 if (bca.lines) {
2506 for (i = 0; i < bca.nlines; i++) {
2507 struct blame_line *bline = &bca.lines[i];
2508 free(bline->id_str);
2509 free(bline->committer);
2511 free(bca.lines);
2513 free(bca.line_offsets);
2514 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2515 error = got_error_from_errno("fclose");
2516 if (error)
2517 return NULL;
2518 else
2519 return blame_html;
2522 static char*
2523 gw_get_repo_tree(struct trans *gw_trans, char *commit_str)
2525 const struct got_error *error = NULL;
2526 struct got_repository *repo = NULL;
2527 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2528 struct got_tree_object *tree = NULL;
2529 struct buf *diffbuf = NULL;
2530 size_t newsize;
2531 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2532 *tree_row = NULL, *id_str;
2533 int nentries, i;
2535 error = buf_alloc(&diffbuf, 0);
2536 if (error)
2537 return NULL;
2539 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2540 if (error)
2541 goto done;
2543 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2544 if (error)
2545 goto done;
2547 if (gw_trans->repo_folder != NULL)
2548 path = strdup(gw_trans->repo_folder);
2549 else if (in_repo_path) {
2550 free(path);
2551 path = in_repo_path;
2554 if (commit_str == NULL) {
2555 struct got_reference *head_ref;
2556 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2557 if (error)
2558 goto done;
2560 error = got_ref_resolve(&commit_id, repo, head_ref);
2561 got_ref_close(head_ref);
2563 } else
2564 error = resolve_commit_arg(&commit_id, commit_str, repo);
2565 if (error)
2566 goto done;
2568 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2569 if (error)
2570 goto done;
2572 error = got_object_open_as_tree(&tree, repo, tree_id);
2573 if (error)
2574 goto done;
2576 nentries = got_object_tree_get_nentries(tree);
2578 for (i = 0; i < nentries; i++) {
2579 struct got_tree_entry *te;
2580 const char *modestr = "";
2581 char *id = NULL, *url_html = NULL;
2583 te = got_object_tree_get_entry(tree, i);
2585 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2586 if (error)
2587 goto done;
2589 if ((asprintf(&id, "%s", id_str)) == -1) {
2590 error = got_error_from_errno("asprintf");
2591 free(id_str);
2592 goto done;
2595 mode_t mode = got_tree_entry_get_mode(te);
2597 if (got_object_tree_entry_is_submodule(te))
2598 modestr = "$";
2599 else if (S_ISLNK(mode))
2600 modestr = "@";
2601 else if (S_ISDIR(mode))
2602 modestr = "/";
2603 else if (mode & S_IXUSR)
2604 modestr = "*";
2606 char *build_folder = NULL;
2607 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2608 if (gw_trans->repo_folder != NULL) {
2609 if ((asprintf(&build_folder, "%s/%s",
2610 gw_trans->repo_folder,
2611 got_tree_entry_get_name(te))) == -1) {
2612 error =
2613 got_error_from_errno("asprintf");
2614 goto done;
2616 } else {
2617 if (asprintf(&build_folder, "%s",
2618 got_tree_entry_get_name(te)) == -1)
2619 goto done;
2622 if ((asprintf(&url_html, folder_html,
2623 gw_trans->repo_name, gw_trans->action_name,
2624 gw_trans->commit, build_folder,
2625 got_tree_entry_get_name(te), modestr)) == -1) {
2626 error = got_error_from_errno("asprintf");
2627 goto done;
2629 } else {
2630 if (gw_trans->repo_folder != NULL) {
2631 if ((asprintf(&build_folder, "%s",
2632 gw_trans->repo_folder)) == -1) {
2633 error =
2634 got_error_from_errno("asprintf");
2635 goto done;
2637 } else
2638 build_folder = strdup("");
2640 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2641 "blame", gw_trans->commit,
2642 got_tree_entry_get_name(te), build_folder,
2643 got_tree_entry_get_name(te), modestr)) == -1) {
2644 error = got_error_from_errno("asprintf");
2645 goto done;
2648 free(build_folder);
2650 if (error)
2651 goto done;
2653 if ((asprintf(&tree_row, trees_row, "", url_html)) == -1) {
2654 error = got_error_from_errno("asprintf");
2655 goto done;
2657 error = buf_puts(&newsize, diffbuf, tree_row);
2658 if (error)
2659 goto done;
2661 free(id);
2662 free(id_str);
2663 free(url_html);
2664 free(tree_row);
2667 if (buf_len(diffbuf) > 0) {
2668 error = buf_putc(diffbuf, '\0');
2669 tree_html = strdup(buf_get(diffbuf));
2671 done:
2672 if (tree)
2673 got_object_tree_close(tree);
2674 if (repo)
2675 got_repo_close(repo);
2677 free(in_repo_path);
2678 free(tree_id);
2679 free(diffbuf);
2680 if (error)
2681 return NULL;
2682 else
2683 return tree_html;
2686 static char *
2687 gw_get_repo_heads(struct trans *gw_trans)
2689 const struct got_error *error = NULL;
2690 struct got_repository *repo = NULL;
2691 struct got_reflist_head refs;
2692 struct got_reflist_entry *re;
2693 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2694 struct buf *diffbuf = NULL;
2695 size_t newsize;
2697 error = buf_alloc(&diffbuf, 0);
2698 if (error)
2699 return NULL;
2701 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2702 if (error)
2703 goto done;
2705 SIMPLEQ_INIT(&refs);
2706 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2707 NULL);
2708 if (error)
2709 goto done;
2711 SIMPLEQ_FOREACH(re, &refs, entry) {
2712 char *refname;
2714 refname = strdup(got_ref_get_name(re->ref));
2715 if (refname == NULL) {
2716 error = got_error_from_errno("got_ref_to_str");
2717 goto done;
2720 if (strncmp(refname, "refs/heads/", 11) != 0) {
2721 free(refname);
2722 continue;
2725 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2726 TM_DIFF);
2728 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2729 refname, gw_trans->repo_name, refname,
2730 gw_trans->repo_name, refname, gw_trans->repo_name,
2731 refname)) == -1) {
2732 error = got_error_from_errno("asprintf");
2733 goto done;
2736 if (strncmp(refname, "refs/heads/", 11) == 0)
2737 refname += 11;
2739 if ((asprintf(&head_row, heads_row, age, refname,
2740 head_navs_disp)) == -1) {
2741 error = got_error_from_errno("asprintf");
2742 goto done;
2745 error = buf_puts(&newsize, diffbuf, head_row);
2747 free(head_navs_disp);
2748 free(head_row);
2751 if (buf_len(diffbuf) > 0) {
2752 error = buf_putc(diffbuf, '\0');
2753 heads = strdup(buf_get(diffbuf));
2755 done:
2756 buf_free(diffbuf);
2757 got_ref_list_free(&refs);
2758 if (repo)
2759 got_repo_close(repo);
2760 if (error)
2761 return NULL;
2762 else
2763 return heads;
2766 static char *
2767 gw_get_got_link(struct trans *gw_trans)
2769 char *link;
2771 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2772 gw_trans->gw_conf->got_logo)) == -1)
2773 return NULL;
2775 return link;
2778 static char *
2779 gw_get_site_link(struct trans *gw_trans)
2781 char *link, *repo = "", *action = "";
2783 if (gw_trans->repo_name != NULL)
2784 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2785 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2786 return NULL;
2788 if (gw_trans->action_name != NULL)
2789 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2790 return NULL;
2792 if ((asprintf(&link, site_link, GOTWEB,
2793 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2794 return NULL;
2796 return link;
2799 static char *
2800 color_diff_line(char *buf)
2802 const struct got_error *error = NULL;
2803 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2804 struct buf *diffbuf = NULL;
2805 size_t newsize;
2807 error = buf_alloc(&diffbuf, 0);
2808 if (error)
2809 return NULL;
2811 if (strncmp(buf, "-", 1) == 0)
2812 color = "diff_minus";
2813 if (strncmp(buf, "+", 1) == 0)
2814 color = "diff_plus";
2815 if (strncmp(buf, "@@", 2) == 0)
2816 color = "diff_chunk_header";
2817 if (strncmp(buf, "@@", 2) == 0)
2818 color = "diff_chunk_header";
2819 if (strncmp(buf, "commit +", 8) == 0)
2820 color = "diff_meta";
2821 if (strncmp(buf, "commit -", 8) == 0)
2822 color = "diff_meta";
2823 if (strncmp(buf, "blob +", 6) == 0)
2824 color = "diff_meta";
2825 if (strncmp(buf, "blob -", 6) == 0)
2826 color = "diff_meta";
2827 if (strncmp(buf, "file +", 6) == 0)
2828 color = "diff_meta";
2829 if (strncmp(buf, "file -", 6) == 0)
2830 color = "diff_meta";
2831 if (strncmp(buf, "from:", 5) == 0)
2832 color = "diff_author";
2833 if (strncmp(buf, "via:", 4) == 0)
2834 color = "diff_author";
2835 if (strncmp(buf, "date:", 5) == 0)
2836 color = "diff_date";
2838 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2839 return NULL;
2841 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2842 if (error)
2843 return NULL;
2845 error = buf_puts(&newsize, diffbuf, buf);
2846 if (error)
2847 return NULL;
2849 if (buf_len(diffbuf) > 0) {
2850 error = buf_putc(diffbuf, '\0');
2851 colorized_line = strdup(buf_get(diffbuf));
2854 free(diffbuf);
2855 free(div_diff_line_div);
2856 return colorized_line;
2859 static char *
2860 gw_html_escape(const char *html)
2862 char *escaped_str = NULL, *buf;
2863 char c[1];
2864 size_t sz, i, buff_sz = 2048;
2866 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2867 return NULL;
2869 if (html == NULL)
2870 return NULL;
2871 else
2872 if ((sz = strlen(html)) == 0)
2873 return NULL;
2875 /* only work with buff_sz */
2876 if (buff_sz < sz)
2877 sz = buff_sz;
2879 for (i = 0; i < sz; i++) {
2880 c[0] = html[i];
2881 switch (c[0]) {
2882 case ('>'):
2883 strcat(buf, "&gt;");
2884 break;
2885 case ('&'):
2886 strcat(buf, "&amp;");
2887 break;
2888 case ('<'):
2889 strcat(buf, "&lt;");
2890 break;
2891 case ('"'):
2892 strcat(buf, "&quot;");
2893 break;
2894 case ('\''):
2895 strcat(buf, "&apos;");
2896 break;
2897 case ('\n'):
2898 strcat(buf, "<br />");
2899 default:
2900 strcat(buf, &c[0]);
2901 break;
2904 asprintf(&escaped_str, "%s", buf);
2905 free(buf);
2906 return escaped_str;
2909 int
2910 main()
2912 const struct got_error *error = NULL;
2913 struct trans *gw_trans;
2914 struct gw_dir *dir = NULL, *tdir;
2915 const char *page = "index";
2916 int gw_malloc = 1;
2918 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
2919 errx(1, "malloc");
2921 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2922 errx(1, "malloc");
2924 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2925 errx(1, "malloc");
2927 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2928 errx(1, "malloc");
2930 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX,
2931 &page, 1, 0))
2932 errx(1, "khttp_parse");
2934 if ((gw_trans->gw_conf =
2935 malloc(sizeof(struct gotweb_conf))) == NULL) {
2936 gw_malloc = 0;
2937 error = got_error_from_errno("malloc");
2938 goto err;
2941 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
2942 NULL) == -1) {
2943 error = got_error_from_errno("pledge");
2944 goto err;
2947 TAILQ_INIT(&gw_trans->gw_dirs);
2949 gw_trans->page = 0;
2950 gw_trans->repos_total = 0;
2951 gw_trans->repo_path = NULL;
2952 gw_trans->commit = NULL;
2953 gw_trans->headref = strdup(GOT_REF_HEAD);
2954 gw_trans->mime = KMIME_TEXT_HTML;
2955 gw_trans->gw_tmpl->key = templs;
2956 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2957 gw_trans->gw_tmpl->arg = gw_trans;
2958 gw_trans->gw_tmpl->cb = gw_template;
2959 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2961 err:
2962 if (error) {
2963 gw_trans->mime = KMIME_TEXT_PLAIN;
2964 gw_trans->action = GW_ERR;
2965 gw_display_index(gw_trans, error);
2966 goto done;
2969 error = gw_parse_querystring(gw_trans);
2970 if (error)
2971 goto err;
2973 gw_display_index(gw_trans, error);
2975 done:
2976 if (gw_malloc) {
2977 free(gw_trans->gw_conf->got_repos_path);
2978 free(gw_trans->gw_conf->got_www_path);
2979 free(gw_trans->gw_conf->got_site_name);
2980 free(gw_trans->gw_conf->got_site_owner);
2981 free(gw_trans->gw_conf->got_site_link);
2982 free(gw_trans->gw_conf->got_logo);
2983 free(gw_trans->gw_conf->got_logo_url);
2984 free(gw_trans->gw_conf);
2985 free(gw_trans->commit);
2986 free(gw_trans->repo_path);
2987 free(gw_trans->repo_name);
2988 free(gw_trans->repo_file);
2989 free(gw_trans->action_name);
2990 free(gw_trans->headref);
2992 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2993 free(dir->name);
2994 free(dir->description);
2995 free(dir->age);
2996 free(dir->url);
2997 free(dir->path);
2998 free(dir);
3003 khttp_free(gw_trans->gw_req);
3004 return EXIT_SUCCESS;