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 gw_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 gw_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 gw_ref_tm {
109 TM_DIFF,
110 TM_LONG,
111 };
113 enum gw_logs {
114 LOGBRIEF,
115 LOGCOMMIT,
116 LOGFULL,
117 LOGTREE,
118 LOGDIFF,
119 LOGBLAME,
120 LOGTAG,
121 };
123 enum gw_tags {
124 TAGBRIEF,
125 TAGFULL,
126 };
128 static const char *const gw_templs[TEMPL__MAX] = {
129 "head",
130 "header",
131 "sitepath",
132 "siteowner",
133 "title",
134 "search",
135 "content",
136 };
138 static const struct kvalid gw_keys[KEY__ZMAX] = {
139 { kvalid_stringne, "action" },
140 { kvalid_stringne, "commit" },
141 { kvalid_stringne, "file" },
142 { kvalid_stringne, "folder" },
143 { kvalid_stringne, "headref" },
144 { kvalid_int, "page" },
145 { kvalid_stringne, "path" },
146 };
148 int gw_get_repo_log_count(struct gw_trans *,
149 char *);
151 static struct gw_dir *gw_init_gw_dir(char *);
153 static char *gw_get_repo_description(struct gw_trans *,
154 char *);
155 static char *gw_get_repo_owner(struct gw_trans *,
156 char *);
157 static char *gw_get_time_str(time_t, int);
158 static char *gw_get_repo_age(struct gw_trans *,
159 char *, char *, int);
160 static char *gw_get_repo_log(struct gw_trans *,
161 const char *, char *, int, int);
162 static char *gw_get_file_blame(struct gw_trans *, char *);
163 static char *gw_get_repo_tree(struct gw_trans *, char *);
164 static char *gw_get_repo_diff(struct gw_trans *, char *,
165 char *);
166 static char *gw_get_repo_tags(struct gw_trans *, int, int);
167 static char *gw_get_repo_heads(struct gw_trans *);
168 static char *gw_get_clone_url(struct gw_trans *, char *);
169 static char *gw_get_got_link(struct gw_trans *);
170 static char *gw_get_site_link(struct gw_trans *);
171 static char *gw_html_escape(const char *);
172 static char *gw_colordiff_line(char *);
174 static void gw_display_open(struct gw_trans *, enum khttp,
175 enum kmime);
176 static void gw_display_index(struct gw_trans *,
177 const struct got_error *);
179 static int gw_template(size_t, void *);
181 static const struct got_error* gw_apply_unveil(const char *, const char *);
182 static const struct got_error* cmp_tags(void *, int *,
183 struct got_reference *,
184 struct got_reference *);
185 static const struct got_error* gw_blame_cb(void *, int, int,
186 struct got_object_id *);
187 static const struct got_error* gw_load_got_paths(struct gw_trans *);
188 static const struct got_error* gw_load_got_path(struct gw_trans *,
189 struct gw_dir *);
190 static const struct got_error* gw_parse_querystring(struct gw_trans *);
191 static const struct got_error* match_logmsg(int *, struct got_object_id *,
192 struct got_commit_object *, regex_t *);
194 static const struct got_error* gw_blame(struct gw_trans *);
195 static const struct got_error* gw_commit(struct gw_trans *);
196 static const struct got_error* gw_commitdiff(struct gw_trans *);
197 static const struct got_error* gw_index(struct gw_trans *);
198 static const struct got_error* gw_log(struct gw_trans *);
199 static const struct got_error* gw_raw(struct gw_trans *);
200 static const struct got_error* gw_logbriefs(struct gw_trans *);
201 static const struct got_error* gw_summary(struct gw_trans *);
202 static const struct got_error* gw_tag(struct gw_trans *);
203 static const struct got_error* gw_tree(struct gw_trans *);
205 struct gw_query_action {
206 unsigned int func_id;
207 const char *func_name;
208 const struct got_error *(*func_main)(struct gw_trans *);
209 char *template;
210 };
212 enum gw_query_actions {
213 GW_BLAME,
214 GW_COMMIT,
215 GW_COMMITDIFF,
216 GW_ERR,
217 GW_INDEX,
218 GW_LOG,
219 GW_RAW,
220 GW_LOGBRIEFS,
221 GW_SUMMARY,
222 GW_TAG,
223 GW_TREE,
224 };
226 static struct gw_query_action gw_query_funcs[] = {
227 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
228 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
229 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
230 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
231 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
232 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
233 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
234 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
235 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
236 { GW_TAG, "tag", gw_tag, "gw_tmpl/index.tmpl" },
237 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
238 };
240 static const struct got_error *
241 gw_apply_unveil(const char *repo_path, const char *repo_file)
243 const struct got_error *err;
245 if (repo_path && repo_file) {
246 char *full_path;
247 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
248 return got_error_from_errno("asprintf unveil");
249 if (unveil(full_path, "r") != 0)
250 return got_error_from_errno2("unveil", full_path);
253 if (repo_path && unveil(repo_path, "r") != 0)
254 return got_error_from_errno2("unveil", repo_path);
256 if (unveil("/tmp", "rwc") != 0)
257 return got_error_from_errno2("unveil", "/tmp");
259 err = got_privsep_unveil_exec_helpers();
260 if (err != NULL)
261 return err;
263 if (unveil(NULL, NULL) != 0)
264 return got_error_from_errno("unveil");
266 return NULL;
269 static const struct got_error *
270 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
271 struct got_reference *ref2)
273 const struct got_error *err = NULL;
274 struct got_repository *repo = arg;
275 struct got_object_id *id1, *id2 = NULL;
276 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
277 time_t time1, time2;
279 *cmp = 0;
281 err = got_ref_resolve(&id1, repo, ref1);
282 if (err)
283 return err;
284 err = got_object_open_as_tag(&tag1, repo, id1);
285 if (err)
286 goto done;
288 err = got_ref_resolve(&id2, repo, ref2);
289 if (err)
290 goto done;
291 err = got_object_open_as_tag(&tag2, repo, id2);
292 if (err)
293 goto done;
295 time1 = got_object_tag_get_tagger_time(tag1);
296 time2 = got_object_tag_get_tagger_time(tag2);
298 /* Put latest tags first. */
299 if (time1 < time2)
300 *cmp = 1;
301 else if (time1 > time2)
302 *cmp = -1;
303 else
304 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
305 done:
306 free(id1);
307 free(id2);
308 if (tag1)
309 got_object_tag_close(tag1);
310 if (tag2)
311 got_object_tag_close(tag2);
312 return err;
315 int
316 gw_get_repo_log_count(struct gw_trans *gw_trans, char *start_commit)
318 const struct got_error *error;
319 struct got_repository *repo = NULL;
320 struct got_reflist_head refs;
321 struct got_commit_object *commit = NULL;
322 struct got_object_id *id = NULL;
323 struct got_commit_graph *graph = NULL;
324 char *in_repo_path = NULL, *path = NULL;
325 int log_count = 0;
327 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
328 if (error)
329 return 0;
331 SIMPLEQ_INIT(&refs);
333 if (start_commit == NULL) {
334 struct got_reference *head_ref;
335 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
336 if (error)
337 goto done;
339 error = got_ref_resolve(&id, repo, head_ref);
340 got_ref_close(head_ref);
341 if (error)
342 goto done;
344 error = got_object_open_as_commit(&commit, repo, id);
345 } else {
346 struct got_reference *ref;
347 error = got_ref_open(&ref, repo, start_commit, 0);
348 if (error == NULL) {
349 int obj_type;
350 error = got_ref_resolve(&id, repo, ref);
351 got_ref_close(ref);
352 if (error)
353 goto done;
354 error = got_object_get_type(&obj_type, repo, id);
355 if (error)
356 goto done;
357 if (obj_type == GOT_OBJ_TYPE_TAG) {
358 struct got_tag_object *tag;
359 error = got_object_open_as_tag(&tag, repo, id);
360 if (error)
361 goto done;
362 if (got_object_tag_get_object_type(tag) !=
363 GOT_OBJ_TYPE_COMMIT) {
364 got_object_tag_close(tag);
365 error = got_error(GOT_ERR_OBJ_TYPE);
366 goto done;
368 free(id);
369 id = got_object_id_dup(
370 got_object_tag_get_object_id(tag));
371 if (id == NULL)
372 error = got_error_from_errno(
373 "got_object_id_dup");
374 got_object_tag_close(tag);
375 if (error)
376 goto done;
377 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
378 error = got_error(GOT_ERR_OBJ_TYPE);
379 goto done;
381 error = got_object_open_as_commit(&commit, repo, id);
382 if (error)
383 goto done;
385 if (commit == NULL) {
386 error = got_repo_match_object_id_prefix(&id,
387 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
388 if (error)
389 goto done;
391 error = got_repo_match_object_id_prefix(&id,
392 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
393 if (error)
394 goto done;
397 error = got_object_open_as_commit(&commit, repo, id);
398 if (error)
399 goto done;
401 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
402 if (error)
403 goto done;
405 if (in_repo_path) {
406 free(path);
407 path = in_repo_path;
410 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
411 if (error)
412 goto done;
414 error = got_commit_graph_open(&graph, path, 0);
415 if (error)
416 goto done;
418 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
419 if (error)
420 goto done;
422 for (;;) {
423 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
424 NULL);
425 if (error) {
426 if (error->code == GOT_ERR_ITER_COMPLETED)
427 error = NULL;
428 break;
430 if (id == NULL)
431 break;
433 if (error)
434 break;
435 log_count++;
437 done:
438 free(in_repo_path);
439 if (graph)
440 got_commit_graph_close(graph);
441 if (repo) {
442 error = got_repo_close(repo);
443 if (error)
444 return 0;
446 if (error) {
447 khttp_puts(gw_trans->gw_req, "Error: ");
448 khttp_puts(gw_trans->gw_req, error->msg);
449 return 0;
450 } else
451 return log_count;
454 static const struct got_error *
455 gw_blame(struct gw_trans *gw_trans)
457 const struct got_error *error = NULL;
459 char *log, *log_html;
461 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
462 if (error)
463 return error;
465 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGBLAME);
467 if (log != NULL && strcmp(log, "") != 0) {
468 if ((asprintf(&log_html, log_blame, log)) == -1)
469 return got_error_from_errno("asprintf");
470 khttp_puts(gw_trans->gw_req, log_html);
471 free(log_html);
472 free(log);
474 return error;
477 static const struct got_error *
478 gw_commit(struct gw_trans *gw_trans)
480 const struct got_error *error = NULL;
481 char *log, *log_html;
483 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
484 if (error)
485 return error;
487 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
489 if (log != NULL && strcmp(log, "") != 0) {
490 if ((asprintf(&log_html, log_commit, log)) == -1)
491 return got_error_from_errno("asprintf");
492 khttp_puts(gw_trans->gw_req, log_html);
493 free(log_html);
494 free(log);
496 return error;
499 static const struct got_error *
500 gw_commitdiff(struct gw_trans *gw_trans)
502 const struct got_error *error = NULL;
503 char *log, *log_html;
505 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
506 if (error)
507 return error;
509 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
511 if (log != NULL && strcmp(log, "") != 0) {
512 if ((asprintf(&log_html, log_diff, log)) == -1)
513 return got_error_from_errno("asprintf");
514 khttp_puts(gw_trans->gw_req, log_html);
515 free(log_html);
516 free(log);
518 return error;
521 static const struct got_error *
522 gw_index(struct gw_trans *gw_trans)
524 const struct got_error *error = NULL;
525 struct gw_dir *gw_dir = NULL;
526 char *html, *navs, *next, *prev;
527 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
529 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
530 if (error)
531 return error;
533 error = gw_load_got_paths(gw_trans);
534 if (error)
535 return error;
537 khttp_puts(gw_trans->gw_req, index_projects_header);
539 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
540 dir_c++;
542 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
543 if (gw_trans->page > 0 && (gw_trans->page *
544 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
545 prev_disp++;
546 continue;
549 prev_disp++;
550 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
551 gw_dir->name, gw_dir->name)) == -1)
552 return got_error_from_errno("asprintf");
554 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
555 gw_dir->description, gw_dir->owner, gw_dir->age,
556 navs)) == -1)
557 return got_error_from_errno("asprintf");
559 khttp_puts(gw_trans->gw_req, html);
561 free(navs);
562 free(html);
564 if (gw_trans->gw_conf->got_max_repos_display == 0)
565 continue;
567 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
568 khttp_puts(gw_trans->gw_req, np_wrapper_start);
569 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
570 (gw_trans->page > 0) &&
571 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
572 prev_disp == gw_trans->repos_total))
573 khttp_puts(gw_trans->gw_req, np_wrapper_start);
575 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
576 (gw_trans->page > 0) &&
577 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
578 prev_disp == gw_trans->repos_total)) {
579 if ((asprintf(&prev, nav_prev,
580 gw_trans->page - 1)) == -1)
581 return got_error_from_errno("asprintf");
582 khttp_puts(gw_trans->gw_req, prev);
583 free(prev);
586 khttp_puts(gw_trans->gw_req, div_end);
588 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
589 next_disp == gw_trans->gw_conf->got_max_repos_display &&
590 dir_c != (gw_trans->page + 1) *
591 gw_trans->gw_conf->got_max_repos_display) {
592 if ((asprintf(&next, nav_next,
593 gw_trans->page + 1)) == -1)
594 return got_error_from_errno("calloc");
595 khttp_puts(gw_trans->gw_req, next);
596 khttp_puts(gw_trans->gw_req, div_end);
597 free(next);
598 next_disp = 0;
599 break;
602 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
603 (gw_trans->page > 0) &&
604 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
605 prev_disp == gw_trans->repos_total))
606 khttp_puts(gw_trans->gw_req, div_end);
608 next_disp++;
610 return error;
613 static const struct got_error *
614 gw_log(struct gw_trans *gw_trans)
616 const struct got_error *error = NULL;
617 char *log, *log_html;
619 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
620 if (error)
621 return error;
623 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
624 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
626 if (log != NULL && strcmp(log, "") != 0) {
627 if ((asprintf(&log_html, logs, log)) == -1)
628 return got_error_from_errno("asprintf");
629 khttp_puts(gw_trans->gw_req, log_html);
630 free(log_html);
631 free(log);
633 return error;
636 static const struct got_error *
637 gw_raw(struct gw_trans *gw_trans)
639 const struct got_error *error = NULL;
641 return error;
644 static const struct got_error *
645 gw_logbriefs(struct gw_trans *gw_trans)
647 const struct got_error *error = NULL;
648 char *log, *log_html;
650 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
651 if (error)
652 return error;
654 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
655 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
657 if (log != NULL && strcmp(log, "") != 0) {
658 if ((asprintf(&log_html, summary_logbriefs,
659 log)) == -1)
660 return got_error_from_errno("asprintf");
661 khttp_puts(gw_trans->gw_req, log_html);
662 free(log_html);
663 free(log);
665 return error;
668 static const struct got_error *
669 gw_summary(struct gw_trans *gw_trans)
671 const struct got_error *error = NULL;
672 char *description_html, *repo_owner_html, *repo_age_html,
673 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
674 *heads_html, *age;
676 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
677 if (error)
678 return error;
680 khttp_puts(gw_trans->gw_req, summary_wrapper);
681 if (gw_trans->gw_conf->got_show_repo_description) {
682 if (gw_trans->gw_dir->description != NULL &&
683 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
684 if ((asprintf(&description_html, description,
685 gw_trans->gw_dir->description)) == -1)
686 return got_error_from_errno("asprintf");
688 khttp_puts(gw_trans->gw_req, description_html);
689 free(description_html);
693 if (gw_trans->gw_conf->got_show_repo_owner) {
694 if (gw_trans->gw_dir->owner != NULL &&
695 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
696 if ((asprintf(&repo_owner_html, repo_owner,
697 gw_trans->gw_dir->owner)) == -1)
698 return got_error_from_errno("asprintf");
700 khttp_puts(gw_trans->gw_req, repo_owner_html);
701 free(repo_owner_html);
705 if (gw_trans->gw_conf->got_show_repo_age) {
706 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
707 "refs/heads", TM_LONG);
708 if (age != NULL && (strcmp(age, "") != 0)) {
709 if ((asprintf(&repo_age_html, last_change, age)) == -1)
710 return got_error_from_errno("asprintf");
712 khttp_puts(gw_trans->gw_req, repo_age_html);
713 free(repo_age_html);
714 free(age);
718 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
719 if (gw_trans->gw_dir->url != NULL &&
720 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
721 if ((asprintf(&cloneurl_html, cloneurl,
722 gw_trans->gw_dir->url)) == -1)
723 return got_error_from_errno("asprintf");
725 khttp_puts(gw_trans->gw_req, cloneurl_html);
726 free(cloneurl_html);
729 khttp_puts(gw_trans->gw_req, div_end);
731 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
732 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
733 heads = gw_get_repo_heads(gw_trans);
735 if (log != NULL && strcmp(log, "") != 0) {
736 if ((asprintf(&log_html, summary_logbriefs,
737 log)) == -1)
738 return got_error_from_errno("asprintf");
739 khttp_puts(gw_trans->gw_req, log_html);
740 free(log_html);
741 free(log);
744 if (tags != NULL && strcmp(tags, "") != 0) {
745 if ((asprintf(&tags_html, summary_tags,
746 tags)) == -1)
747 return got_error_from_errno("asprintf");
748 khttp_puts(gw_trans->gw_req, tags_html);
749 free(tags_html);
750 free(tags);
753 if (heads != NULL && strcmp(heads, "") != 0) {
754 if ((asprintf(&heads_html, summary_heads,
755 heads)) == -1)
756 return got_error_from_errno("asprintf");
757 khttp_puts(gw_trans->gw_req, heads_html);
758 free(heads_html);
759 free(heads);
761 return error;
764 static const struct got_error *
765 gw_tag(struct gw_trans *gw_trans)
767 const struct got_error *error = NULL;
768 char *log, *log_html;
770 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
771 if (error)
772 return error;
774 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTAG);
776 if (log != NULL && strcmp(log, "") != 0) {
777 if ((asprintf(&log_html, log_tag, log)) == -1)
778 return got_error_from_errno("asprintf");
779 khttp_puts(gw_trans->gw_req, log_html);
780 free(log_html);
781 free(log);
783 return error;
786 static const struct got_error *
787 gw_tree(struct gw_trans *gw_trans)
789 const struct got_error *error = NULL;
790 char *log, *log_html;
792 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
793 if (error)
794 return error;
796 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
798 if (log != NULL && strcmp(log, "") != 0) {
799 if ((asprintf(&log_html, log_tree, log)) == -1)
800 return got_error_from_errno("asprintf");
801 khttp_puts(gw_trans->gw_req, log_html);
802 free(log_html);
803 free(log);
805 return error;
808 static const struct got_error *
809 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
811 const struct got_error *error = NULL;
812 DIR *dt;
813 char *dir_test;
814 int opened = 0;
816 if ((asprintf(&dir_test, "%s/%s/%s",
817 gw_trans->gw_conf->got_repos_path, gw_dir->name,
818 GOTWEB_GIT_DIR)) == -1)
819 return got_error_from_errno("asprintf");
821 dt = opendir(dir_test);
822 if (dt == NULL) {
823 free(dir_test);
824 } else {
825 gw_dir->path = strdup(dir_test);
826 opened = 1;
827 goto done;
830 if ((asprintf(&dir_test, "%s/%s/%s",
831 gw_trans->gw_conf->got_repos_path, gw_dir->name,
832 GOTWEB_GOT_DIR)) == -1)
833 return got_error_from_errno("asprintf");
835 dt = opendir(dir_test);
836 if (dt == NULL)
837 free(dir_test);
838 else {
839 opened = 1;
840 error = got_error(GOT_ERR_NOT_GIT_REPO);
841 goto errored;
844 if ((asprintf(&dir_test, "%s/%s",
845 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
846 return got_error_from_errno("asprintf");
848 gw_dir->path = strdup(dir_test);
850 done:
851 gw_dir->description = gw_get_repo_description(gw_trans,
852 gw_dir->path);
853 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
854 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
855 TM_DIFF);
856 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
858 errored:
859 free(dir_test);
860 if (opened)
861 closedir(dt);
862 return error;
865 static const struct got_error *
866 gw_load_got_paths(struct gw_trans *gw_trans)
868 const struct got_error *error = NULL;
869 DIR *d;
870 struct dirent **sd_dent;
871 struct gw_dir *gw_dir;
872 struct stat st;
873 unsigned int d_cnt, d_i;
875 d = opendir(gw_trans->gw_conf->got_repos_path);
876 if (d == NULL) {
877 error = got_error_from_errno2("opendir",
878 gw_trans->gw_conf->got_repos_path);
879 return error;
882 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
883 alphasort);
884 if (d_cnt == -1) {
885 error = got_error_from_errno2("scandir",
886 gw_trans->gw_conf->got_repos_path);
887 return error;
890 for (d_i = 0; d_i < d_cnt; d_i++) {
891 if (gw_trans->gw_conf->got_max_repos > 0 &&
892 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
893 break; /* account for parent and self */
895 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
896 strcmp(sd_dent[d_i]->d_name, "..") == 0)
897 continue;
899 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
900 return got_error_from_errno("gw_dir malloc");
902 error = gw_load_got_path(gw_trans, gw_dir);
903 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
904 continue;
905 else if (error)
906 return error;
908 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
909 !got_path_dir_is_empty(gw_dir->path)) {
910 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
911 entry);
912 gw_trans->repos_total++;
916 closedir(d);
917 return error;
920 static const struct got_error *
921 gw_parse_querystring(struct gw_trans *gw_trans)
923 const struct got_error *error = NULL;
924 struct kpair *p;
925 struct gw_query_action *action = NULL;
926 unsigned int i;
928 if (gw_trans->gw_req->fieldnmap[0]) {
929 error = got_error_from_errno("bad parse");
930 return error;
931 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
932 /* define gw_trans->repo_path */
933 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
934 return got_error_from_errno("asprintf");
936 if ((asprintf(&gw_trans->repo_path, "%s/%s",
937 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
938 return got_error_from_errno("asprintf");
940 /* get action and set function */
941 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
942 for (i = 0; i < nitems(gw_query_funcs); i++) {
943 action = &gw_query_funcs[i];
944 if (action->func_name == NULL)
945 continue;
947 if (strcmp(action->func_name,
948 p->parsed.s) == 0) {
949 gw_trans->action = i;
950 if ((asprintf(&gw_trans->action_name,
951 "%s", action->func_name)) == -1)
952 return
953 got_error_from_errno(
954 "asprintf");
956 break;
959 action = NULL;
962 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
963 if ((asprintf(&gw_trans->commit, "%s",
964 p->parsed.s)) == -1)
965 return got_error_from_errno("asprintf");
967 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
968 if ((asprintf(&gw_trans->repo_file, "%s",
969 p->parsed.s)) == -1)
970 return got_error_from_errno("asprintf");
972 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
973 if ((asprintf(&gw_trans->repo_folder, "%s",
974 p->parsed.s)) == -1)
975 return got_error_from_errno("asprintf");
977 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
978 if ((asprintf(&gw_trans->headref, "%s",
979 p->parsed.s)) == -1)
980 return got_error_from_errno("asprintf");
982 if (action == NULL) {
983 error = got_error_from_errno("invalid action");
984 return error;
986 if ((gw_trans->gw_dir =
987 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
988 return got_error_from_errno("gw_dir malloc");
990 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
991 if (error)
992 return error;
993 } else
994 gw_trans->action = GW_INDEX;
996 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
997 gw_trans->page = p->parsed.i;
999 if (gw_trans->action == GW_RAW)
1000 gw_trans->mime = KMIME_TEXT_PLAIN;
1002 return error;
1005 static struct gw_dir *
1006 gw_init_gw_dir(char *dir)
1008 struct gw_dir *gw_dir;
1010 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1011 return NULL;
1013 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1014 return NULL;
1016 return gw_dir;
1019 static const struct got_error*
1020 match_logmsg(int *have_match, struct got_object_id *id,
1021 struct got_commit_object *commit, regex_t *regex)
1023 const struct got_error *err = NULL;
1024 regmatch_t regmatch;
1025 char *id_str = NULL, *logmsg = NULL;
1027 *have_match = 0;
1029 err = got_object_id_str(&id_str, id);
1030 if (err)
1031 return err;
1033 err = got_object_commit_get_logmsg(&logmsg, commit);
1034 if (err)
1035 goto done;
1037 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1038 *have_match = 1;
1039 done:
1040 free(id_str);
1041 free(logmsg);
1042 return err;
1045 static void
1046 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1048 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1049 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1050 khttps[code]);
1051 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1052 kmimetypes[mime]);
1053 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1054 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1055 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1056 khttp_body(gw_trans->gw_req);
1059 static void
1060 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
1062 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1063 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1065 if (err)
1066 khttp_puts(gw_trans->gw_req, err->msg);
1067 else
1068 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1069 gw_query_funcs[gw_trans->action].template);
1071 khtml_close(gw_trans->gw_html_req);
1074 static int
1075 gw_template(size_t key, void *arg)
1077 const struct got_error *error = NULL;
1078 struct gw_trans *gw_trans = arg;
1079 char *gw_got_link, *gw_site_link;
1080 char *site_owner_name, *site_owner_name_h;
1082 switch (key) {
1083 case (TEMPL_HEAD):
1084 khttp_puts(gw_trans->gw_req, head);
1085 break;
1086 case(TEMPL_HEADER):
1087 gw_got_link = gw_get_got_link(gw_trans);
1088 if (gw_got_link != NULL)
1089 khttp_puts(gw_trans->gw_req, gw_got_link);
1091 free(gw_got_link);
1092 break;
1093 case (TEMPL_SITEPATH):
1094 gw_site_link = gw_get_site_link(gw_trans);
1095 if (gw_site_link != NULL)
1096 khttp_puts(gw_trans->gw_req, gw_site_link);
1098 free(gw_site_link);
1099 break;
1100 case(TEMPL_TITLE):
1101 if (gw_trans->gw_conf->got_site_name != NULL)
1102 khtml_puts(gw_trans->gw_html_req,
1103 gw_trans->gw_conf->got_site_name);
1105 break;
1106 case (TEMPL_SEARCH):
1107 khttp_puts(gw_trans->gw_req, search);
1108 break;
1109 case(TEMPL_SITEOWNER):
1110 if (gw_trans->gw_conf->got_site_owner != NULL &&
1111 gw_trans->gw_conf->got_show_site_owner) {
1112 site_owner_name =
1113 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1114 if ((asprintf(&site_owner_name_h, site_owner,
1115 site_owner_name))
1116 == -1)
1117 return 0;
1119 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1120 free(site_owner_name);
1121 free(site_owner_name_h);
1123 break;
1124 case(TEMPL_CONTENT):
1125 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1126 if (error)
1127 khttp_puts(gw_trans->gw_req, error->msg);
1129 break;
1130 default:
1131 return 0;
1132 break;
1134 return 1;
1137 static char *
1138 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1140 FILE *f;
1141 char *description = NULL, *d_file = NULL;
1142 unsigned int len;
1144 if (gw_trans->gw_conf->got_show_repo_description == false)
1145 goto err;
1147 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1148 goto err;
1150 if ((f = fopen(d_file, "r")) == NULL)
1151 goto err;
1153 fseek(f, 0, SEEK_END);
1154 len = ftell(f) + 1;
1155 fseek(f, 0, SEEK_SET);
1156 if ((description = calloc(len, sizeof(char *))) == NULL)
1157 goto err;
1159 fread(description, 1, len, f);
1160 fclose(f);
1161 free(d_file);
1162 return description;
1163 err:
1164 if ((asprintf(&description, "%s", "")) == -1)
1165 return NULL;
1167 return description;
1170 static char *
1171 gw_get_time_str(time_t committer_time, int ref_tm)
1173 struct tm tm;
1174 time_t diff_time;
1175 char *years = "years ago", *months = "months ago";
1176 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1177 char *minutes = "minutes ago", *seconds = "seconds ago";
1178 char *now = "right now";
1179 char *repo_age, *s;
1180 char datebuf[29];
1182 switch (ref_tm) {
1183 case TM_DIFF:
1184 diff_time = time(NULL) - committer_time;
1185 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1186 if ((asprintf(&repo_age, "%lld %s",
1187 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1188 return NULL;
1189 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1190 if ((asprintf(&repo_age, "%lld %s",
1191 (diff_time / 60 / 60 / 24 / (365 / 12)),
1192 months)) == -1)
1193 return NULL;
1194 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1195 if ((asprintf(&repo_age, "%lld %s",
1196 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1197 return NULL;
1198 } else if (diff_time > 60 * 60 * 24 * 2) {
1199 if ((asprintf(&repo_age, "%lld %s",
1200 (diff_time / 60 / 60 / 24), days)) == -1)
1201 return NULL;
1202 } else if (diff_time > 60 * 60 * 2) {
1203 if ((asprintf(&repo_age, "%lld %s",
1204 (diff_time / 60 / 60), hours)) == -1)
1205 return NULL;
1206 } else if (diff_time > 60 * 2) {
1207 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1208 minutes)) == -1)
1209 return NULL;
1210 } else if (diff_time > 2) {
1211 if ((asprintf(&repo_age, "%lld %s", diff_time,
1212 seconds)) == -1)
1213 return NULL;
1214 } else {
1215 if ((asprintf(&repo_age, "%s", now)) == -1)
1216 return NULL;
1218 break;
1219 case TM_LONG:
1220 if (gmtime_r(&committer_time, &tm) == NULL)
1221 return NULL;
1223 s = asctime_r(&tm, datebuf);
1224 if (s == NULL)
1225 return NULL;
1227 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1228 return NULL;
1229 break;
1231 return repo_age;
1234 static char *
1235 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1236 int ref_tm)
1238 const struct got_error *error = NULL;
1239 struct got_object_id *id = NULL;
1240 struct got_repository *repo = NULL;
1241 struct got_commit_object *commit = NULL;
1242 struct got_reflist_head refs;
1243 struct got_reflist_entry *re;
1244 struct got_reference *head_ref;
1245 int is_head = 0;
1246 time_t committer_time = 0, cmp_time = 0;
1247 const char *refname;
1248 char *repo_age = NULL;
1250 if (repo_ref == NULL)
1251 return NULL;
1253 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1254 is_head = 1;
1256 SIMPLEQ_INIT(&refs);
1257 if (gw_trans->gw_conf->got_show_repo_age == false) {
1258 if ((asprintf(&repo_age, "")) == -1)
1259 return NULL;
1260 return repo_age;
1263 error = got_repo_open(&repo, dir, NULL);
1264 if (error)
1265 goto err;
1267 if (is_head)
1268 error = got_ref_list(&refs, repo, "refs/heads",
1269 got_ref_cmp_by_name, NULL);
1270 else
1271 error = got_ref_list(&refs, repo, repo_ref,
1272 got_ref_cmp_by_name, NULL);
1273 if (error)
1274 goto err;
1276 SIMPLEQ_FOREACH(re, &refs, entry) {
1277 if (is_head)
1278 refname = strdup(repo_ref);
1279 else
1280 refname = got_ref_get_name(re->ref);
1281 error = got_ref_open(&head_ref, repo, refname, 0);
1282 if (error)
1283 goto err;
1285 error = got_ref_resolve(&id, repo, head_ref);
1286 got_ref_close(head_ref);
1287 if (error)
1288 goto err;
1290 error = got_object_open_as_commit(&commit, repo, id);
1291 if (error)
1292 goto err;
1294 committer_time =
1295 got_object_commit_get_committer_time(commit);
1297 if (cmp_time < committer_time)
1298 cmp_time = committer_time;
1301 if (cmp_time != 0) {
1302 committer_time = cmp_time;
1303 repo_age = gw_get_time_str(committer_time, ref_tm);
1304 } else
1305 if ((asprintf(&repo_age, "")) == -1)
1306 return NULL;
1307 got_ref_list_free(&refs);
1308 free(id);
1309 return repo_age;
1310 err:
1311 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1312 return NULL;
1314 return repo_age;
1317 static char *
1318 gw_get_repo_diff(struct gw_trans *gw_trans, char *id_str1, char *id_str2)
1320 const struct got_error *error;
1321 FILE *f = NULL;
1322 struct got_object_id *id1 = NULL, *id2 = NULL;
1323 struct got_repository *repo = NULL;
1324 struct buf *diffbuf = NULL;
1325 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1326 *buf_color = NULL;
1327 int type1, type2;
1328 size_t newsize;
1330 f = got_opentemp();
1331 if (f == NULL)
1332 return NULL;
1334 error = buf_alloc(&diffbuf, 0);
1335 if (error)
1336 return NULL;
1338 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1339 if (error)
1340 goto done;
1342 error = got_repo_match_object_id(&id1, &label1, id_str1,
1343 GOT_OBJ_TYPE_ANY, 1, repo);
1344 if (error)
1345 goto done;
1347 if (id_str2) {
1348 error = got_repo_match_object_id(&id2, &label2, id_str2,
1349 GOT_OBJ_TYPE_ANY, 1, repo);
1350 if (error)
1351 goto done;
1353 error = got_object_get_type(&type2, repo, id2);
1354 if (error)
1355 goto done;
1358 error = got_object_get_type(&type1, repo, id1);
1359 if (error)
1360 goto done;
1362 if (id_str2 && type1 != type2) {
1363 error = got_error(GOT_ERR_OBJ_TYPE);
1364 goto done;
1367 switch (type1) {
1368 case GOT_OBJ_TYPE_BLOB:
1369 error = got_diff_objects_as_blobs(id2, id1, NULL, NULL, 3, 0,
1370 repo, f);
1371 break;
1372 case GOT_OBJ_TYPE_TREE:
1373 error = got_diff_objects_as_trees(id2, id1, "", "", 3, 0, repo,
1374 f);
1375 break;
1376 case GOT_OBJ_TYPE_COMMIT:
1377 error = got_diff_objects_as_commits(id2, id1, 3, 0, repo, f);
1378 break;
1379 default:
1380 error = got_error(GOT_ERR_OBJ_TYPE);
1383 if ((buf = calloc(128, sizeof(char *))) == NULL)
1384 goto done;
1386 fseek(f, 0, SEEK_SET);
1388 while ((fgets(buf, 128, f)) != NULL) {
1389 buf_color = gw_colordiff_line(buf);
1390 error = buf_puts(&newsize, diffbuf, buf_color);
1391 if (error)
1392 return NULL;
1394 error = buf_puts(&newsize, diffbuf, div_end);
1395 if (error)
1396 return NULL;
1399 if (buf_len(diffbuf) > 0) {
1400 error = buf_putc(diffbuf, '\0');
1401 diff_html = strdup(buf_get(diffbuf));
1403 done:
1404 fclose(f);
1405 free(buf_color);
1406 free(buf);
1407 free(diffbuf);
1408 free(label1);
1409 free(label2);
1410 free(id1);
1411 free(id2);
1412 if (repo)
1413 got_repo_close(repo);
1415 if (error)
1416 return NULL;
1417 else
1418 return diff_html;
1421 static char *
1422 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1424 FILE *f;
1425 char *owner = NULL, *d_file = NULL;
1426 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1427 char *comp, *pos, *buf;
1428 unsigned int i;
1430 if (gw_trans->gw_conf->got_show_repo_owner == false)
1431 goto err;
1433 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1434 goto err;
1436 if ((f = fopen(d_file, "r")) == NULL)
1437 goto err;
1439 if ((buf = calloc(128, sizeof(char *))) == NULL)
1440 goto err;
1442 while ((fgets(buf, 128, f)) != NULL) {
1443 if ((pos = strstr(buf, gotweb)) != NULL)
1444 break;
1446 if ((pos = strstr(buf, gitweb)) != NULL)
1447 break;
1450 if (pos == NULL)
1451 goto err;
1453 do {
1454 fgets(buf, 128, f);
1455 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1457 if (comp == NULL)
1458 goto err;
1460 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1461 goto err;
1463 for (i = 0; i < 2; i++) {
1464 owner = strsep(&buf, "\"");
1467 if (owner == NULL)
1468 goto err;
1470 fclose(f);
1471 free(d_file);
1472 return owner;
1473 err:
1474 if ((asprintf(&owner, "%s", "")) == -1)
1475 return NULL;
1477 return owner;
1480 static char *
1481 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1483 FILE *f;
1484 char *url = NULL, *d_file = NULL;
1485 unsigned int len;
1487 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1488 return NULL;
1490 if ((f = fopen(d_file, "r")) == NULL)
1491 return NULL;
1493 fseek(f, 0, SEEK_END);
1494 len = ftell(f) + 1;
1495 fseek(f, 0, SEEK_SET);
1497 if ((url = calloc(len, sizeof(char *))) == NULL)
1498 return NULL;
1500 fread(url, 1, len, f);
1501 fclose(f);
1502 free(d_file);
1503 return url;
1506 static char *
1507 gw_get_repo_log(struct gw_trans *gw_trans, const char *search_pattern,
1508 char *start_commit, int limit, int log_type)
1510 const struct got_error *error;
1511 struct got_repository *repo = NULL;
1512 struct got_reflist_head refs;
1513 struct got_reflist_entry *re;
1514 struct got_commit_object *commit = NULL;
1515 struct got_object_id *id1 = NULL, *id2 = NULL;
1516 struct got_object_qid *parent_id;
1517 struct got_commit_graph *graph = NULL;
1518 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1519 *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1520 *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1521 *commit_commit_disp = NULL, *commit_age_diff = NULL,
1522 *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1523 *commit_age_long_disp = NULL, *commit_author = NULL,
1524 *commit_author_disp = NULL, *commit_committer = NULL,
1525 *commit_committer_disp = NULL, *commit_log = NULL,
1526 *commit_log_disp = NULL, *commit_parent = NULL,
1527 *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1528 *log_tree_html = NULL, *log_commit_html = NULL,
1529 *log_diff_html = NULL, *commit_tree = NULL,
1530 *commit_tree_disp = NULL, *log_tag_html = NULL,
1531 *log_blame_html = NULL;
1532 char *commit_log0, *newline;
1533 regex_t regex;
1534 int have_match, log_count = 0, has_parent = 1;
1535 size_t newsize;
1536 struct buf *diffbuf = NULL;
1537 time_t committer_time;
1539 if (gw_trans->action == GW_LOG || gw_trans->action == GW_LOGBRIEFS)
1540 log_count = gw_get_repo_log_count(gw_trans, start_commit);
1542 error = buf_alloc(&diffbuf, 0);
1543 if (error)
1544 return NULL;
1546 if (search_pattern &&
1547 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB |
1548 REG_NEWLINE))
1549 return NULL;
1551 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1552 if (error)
1553 return NULL;
1555 SIMPLEQ_INIT(&refs);
1557 if (start_commit == NULL) {
1558 struct got_reference *head_ref;
1559 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1560 if (error)
1561 goto done;
1563 error = got_ref_resolve(&id1, repo, head_ref);
1564 got_ref_close(head_ref);
1565 if (error)
1566 goto done;
1568 error = got_object_open_as_commit(&commit, repo, id1);
1569 } else {
1570 struct got_reference *ref;
1571 error = got_ref_open(&ref, repo, start_commit, 0);
1572 if (error == NULL) {
1573 int obj_type;
1574 error = got_ref_resolve(&id1, repo, ref);
1575 got_ref_close(ref);
1576 if (error)
1577 goto done;
1578 error = got_object_get_type(&obj_type, repo, id1);
1579 if (error)
1580 goto done;
1581 if (obj_type == GOT_OBJ_TYPE_TAG) {
1582 struct got_tag_object *tag;
1583 error = got_object_open_as_tag(&tag, repo, id1);
1584 if (error)
1585 goto done;
1586 if (got_object_tag_get_object_type(tag) !=
1587 GOT_OBJ_TYPE_COMMIT) {
1588 got_object_tag_close(tag);
1589 error = got_error(GOT_ERR_OBJ_TYPE);
1590 goto done;
1592 free(id1);
1593 id1 = got_object_id_dup(
1594 got_object_tag_get_object_id(tag));
1595 if (id1 == NULL)
1596 error = got_error_from_errno(
1597 "got_object_id_dup");
1598 got_object_tag_close(tag);
1599 if (error)
1600 goto done;
1601 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1602 error = got_error(GOT_ERR_OBJ_TYPE);
1603 goto done;
1605 error = got_object_open_as_commit(&commit, repo, id1);
1606 if (error)
1607 goto done;
1609 if (commit == NULL) {
1610 error = got_repo_match_object_id_prefix(&id1,
1611 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1612 if (error)
1613 goto done;
1615 error = got_repo_match_object_id_prefix(&id1,
1616 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1619 if (error)
1620 goto done;
1622 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1623 if (error)
1624 goto done;
1626 if (in_repo_path) {
1627 free(path);
1628 path = in_repo_path;
1631 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1632 if (error)
1633 goto done;
1635 error = got_commit_graph_open(&graph, path, 0);
1636 if (error)
1637 goto done;
1639 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1640 if (error)
1641 goto done;
1643 for (;;) {
1644 error = got_commit_graph_iter_next(&id1, graph, repo, NULL,
1645 NULL);
1646 if (error) {
1647 if (error->code == GOT_ERR_ITER_COMPLETED)
1648 error = NULL;
1649 break;
1651 if (id1 == NULL)
1652 break;
1654 error = got_object_open_as_commit(&commit, repo, id1);
1655 if (error)
1656 break;
1658 if (search_pattern) {
1659 error = match_logmsg(&have_match, id1, commit,
1660 &regex);
1661 if (error) {
1662 got_object_commit_close(commit);
1663 break;
1665 if (have_match == 0) {
1666 got_object_commit_close(commit);
1667 continue;
1671 SIMPLEQ_FOREACH(re, &refs, entry) {
1672 char *s;
1673 const char *name;
1674 struct got_tag_object *tag = NULL;
1675 int cmp;
1677 name = got_ref_get_name(re->ref);
1678 if (strcmp(name, GOT_REF_HEAD) == 0)
1679 continue;
1680 if (strncmp(name, "refs/", 5) == 0)
1681 name += 5;
1682 if (strncmp(name, "got/", 4) == 0)
1683 continue;
1684 if (strncmp(name, "heads/", 6) == 0)
1685 name += 6;
1686 if (strncmp(name, "remotes/", 8) == 0)
1687 name += 8;
1688 if (strncmp(name, "tags/", 5) == 0) {
1689 error = got_object_open_as_tag(&tag, repo,
1690 re->id);
1691 if (error) {
1692 if (error->code != GOT_ERR_OBJ_TYPE)
1693 continue;
1695 * Ref points at something other
1696 * than a tag.
1698 error = NULL;
1699 tag = NULL;
1702 cmp = got_object_id_cmp(tag ?
1703 got_object_tag_get_object_id(tag) : re->id, id1);
1704 if (tag)
1705 got_object_tag_close(tag);
1706 if (cmp != 0)
1707 continue;
1708 s = refs_str;
1709 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1710 s ? ", " : "", name)) == -1) {
1711 error = got_error_from_errno("asprintf");
1712 free(s);
1713 goto done;
1715 free(s);
1718 if (refs_str == NULL)
1719 refs_str_disp = strdup("");
1720 else {
1721 if ((asprintf(&refs_str_disp, "(%s)",
1722 refs_str)) == -1) {
1723 error = got_error_from_errno("asprintf");
1724 free(refs_str);
1725 goto done;
1729 error = got_object_id_str(&id_str1, id1);
1730 if (error)
1731 goto done;
1733 error = got_object_id_str(&treeid,
1734 got_object_commit_get_tree_id(commit));
1735 if (error)
1736 goto done;
1738 if (gw_trans->action == GW_COMMIT ||
1739 gw_trans->action == GW_COMMITDIFF) {
1740 parent_id =
1741 SIMPLEQ_FIRST(
1742 got_object_commit_get_parent_ids(commit));
1743 if (parent_id != NULL) {
1744 id2 = got_object_id_dup(parent_id->id);
1745 free (parent_id);
1746 error = got_object_id_str(&id_str2, id2);
1747 if (error)
1748 goto done;
1749 free(id2);
1750 } else {
1751 has_parent = 0;
1752 id_str2 = strdup("/dev/null");
1756 committer_time =
1757 got_object_commit_get_committer_time(commit);
1759 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1760 error = got_error_from_errno("asprintf");
1761 goto done;
1764 if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1765 error = got_error_from_errno("asprintf");
1766 goto done;
1769 if ((asprintf(&commit_tree_disp, commit_tree_html,
1770 treeid)) == -1) {
1771 error = got_error_from_errno("asprintf");
1772 goto done;
1775 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1776 id_str1)) == -1) {
1777 error = got_error_from_errno("asprintf");
1778 goto done;
1781 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1782 error = got_error_from_errno("asprintf");
1783 goto done;
1786 if ((asprintf(&commit_commit_disp, commit_commit_html,
1787 commit_commit, refs_str_disp)) == -1) {
1788 error = got_error_from_errno("asprintf");
1789 goto done;
1792 if ((asprintf(&commit_age_long, "%s",
1793 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1794 error = got_error_from_errno("asprintf");
1795 goto done;
1798 if ((asprintf(&commit_age_long_disp, commit_age_html,
1799 commit_age_long)) == -1) {
1800 error = got_error_from_errno("asprintf");
1801 goto done;
1804 if ((asprintf(&commit_age_diff, "%s",
1805 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1806 error = got_error_from_errno("asprintf");
1807 goto done;
1810 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1811 commit_age_diff)) == -1) {
1812 error = got_error_from_errno("asprintf");
1813 goto done;
1816 if ((asprintf(&commit_author, "%s",
1817 got_object_commit_get_author(commit))) == -1) {
1818 error = got_error_from_errno("asprintf");
1819 goto done;
1822 if ((asprintf(&commit_author_disp, commit_author_html,
1823 gw_html_escape(commit_author))) == -1) {
1824 error = got_error_from_errno("asprintf");
1825 goto done;
1828 if ((asprintf(&commit_committer, "%s",
1829 got_object_commit_get_committer(commit))) == -1) {
1830 error = got_error_from_errno("asprintf");
1831 goto done;
1834 if ((asprintf(&commit_committer_disp, commit_committer_html,
1835 gw_html_escape(commit_committer))) == -1) {
1836 error = got_error_from_errno("asprintf");
1837 goto done;
1840 if (strcmp(commit_author, commit_committer) == 0) {
1841 free(commit_committer_disp);
1842 commit_committer_disp = strdup("");
1845 error = got_object_commit_get_logmsg(&commit_log0, commit);
1846 if (error)
1847 goto done;
1849 commit_log = commit_log0;
1850 while (*commit_log == '\n')
1851 commit_log++;
1853 switch(log_type) {
1854 case (LOGBRIEF):
1855 newline = strchr(commit_log, '\n');
1856 if (newline)
1857 *newline = '\0';
1859 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1860 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1861 id_str1, gw_trans->repo_name, id_str1,
1862 gw_trans->repo_name, id_str1)) == -1) {
1863 error = got_error_from_errno("asprintf");
1864 goto done;
1867 if ((asprintf(&commit_row, logbriefs_row,
1868 commit_age_diff, commit_author, commit_log,
1869 logbriefs_navs_html)) == -1) {
1870 error = got_error_from_errno("asprintf");
1871 goto done;
1874 free(logbriefs_navs_html);
1875 logbriefs_navs_html = NULL;
1876 break;
1877 case (LOGFULL):
1878 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1879 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1880 id_str1, gw_trans->repo_name, id_str1,
1881 gw_trans->repo_name, id_str1)) == -1) {
1882 error = got_error_from_errno("asprintf");
1883 goto done;
1886 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1887 commit_author_disp, commit_committer_disp,
1888 commit_age_long_disp, gw_html_escape(commit_log),
1889 logbriefs_navs_html)) == -1) {
1890 error = got_error_from_errno("asprintf");
1891 goto done;
1894 free(logbriefs_navs_html);
1895 logbriefs_navs_html = NULL;
1896 break;
1897 case (LOGTAG):
1898 log_tag_html = strdup("tag log here");
1900 if ((asprintf(&commit_row, log_tag_row,
1901 gw_html_escape(commit_log), log_tag_html)) == -1) {
1902 error = got_error_from_errno("asprintf");
1903 goto done;
1906 free(log_tag_html);
1907 break;
1908 case (LOGBLAME):
1909 log_blame_html = gw_get_file_blame(gw_trans,
1910 start_commit);
1912 if ((asprintf(&commit_row, log_blame_row,
1913 gw_html_escape(commit_log), log_blame_html)) == -1) {
1914 error = got_error_from_errno("asprintf");
1915 goto done;
1918 free(log_blame_html);
1919 break;
1920 case (LOGTREE):
1921 log_tree_html = gw_get_repo_tree(gw_trans,
1922 start_commit);
1924 if ((asprintf(&commit_row, log_tree_row,
1925 gw_html_escape(commit_log), log_tree_html)) == -1) {
1926 error = got_error_from_errno("asprintf");
1927 goto done;
1930 free(log_tree_html);
1931 break;
1932 case (LOGCOMMIT):
1933 if ((asprintf(&commit_log_disp, commit_log_html,
1934 gw_html_escape(commit_log))) == -1) {
1935 error = got_error_from_errno("asprintf");
1936 goto done;
1939 log_commit_html = strdup("commit here");
1941 if ((asprintf(&commit_row, log_commit_row,
1942 commit_diff_disp, commit_commit_disp,
1943 commit_tree_disp, commit_author_disp,
1944 commit_committer_disp, commit_age_long_disp,
1945 commit_log_disp, log_commit_html)) == -1) {
1946 error = got_error_from_errno("asprintf");
1947 goto done;
1949 free(commit_log_disp);
1950 free(log_commit_html);
1952 break;
1953 case (LOGDIFF):
1954 if ((asprintf(&commit_log_disp, commit_log_html,
1955 gw_html_escape(commit_log))) == -1) {
1956 error = got_error_from_errno("asprintf");
1957 goto done;
1960 if (has_parent)
1961 log_diff_html = gw_get_repo_diff(gw_trans,
1962 commit_commit, commit_parent);
1963 else
1964 log_diff_html = gw_get_repo_diff(gw_trans,
1965 commit_commit, NULL);
1967 if ((asprintf(&commit_row, log_diff_row,
1968 commit_diff_disp, commit_commit_disp,
1969 commit_tree_disp, commit_author_disp,
1970 commit_committer_disp, commit_age_long_disp,
1971 commit_log_disp, log_diff_html)) == -1) {
1972 error = got_error_from_errno("asprintf");
1973 goto done;
1975 free(commit_log_disp);
1976 free(log_diff_html);
1978 break;
1979 default:
1980 return NULL;
1983 error = buf_puts(&newsize, diffbuf, commit_row);
1985 free(commit_parent);
1986 free(commit_diff_disp);
1987 free(commit_tree_disp);
1988 free(commit_age_diff);
1989 free(commit_age_diff_disp);
1990 free(commit_age_long);
1991 free(commit_age_long_disp);
1992 free(commit_author);
1993 free(commit_author_disp);
1994 free(commit_committer);
1995 free(commit_committer_disp);
1996 free(commit_log0);
1997 free(commit_row);
1998 free(refs_str_disp);
1999 free(refs_str);
2000 refs_str = NULL;
2001 free(id_str1);
2002 id_str1 = NULL;
2003 free(id_str2);
2004 id_str2 = NULL;
2006 if (error || (limit && --limit == 0))
2007 break;
2010 if (error)
2011 goto done;
2013 if (buf_len(diffbuf) > 0) {
2014 error = buf_putc(diffbuf, '\0');
2015 logs = strdup(buf_get(diffbuf));
2017 done:
2018 buf_free(diffbuf);
2019 free(in_repo_path);
2020 if (commit != NULL)
2021 got_object_commit_close(commit);
2022 if (search_pattern)
2023 regfree(&regex);
2024 if (graph)
2025 got_commit_graph_close(graph);
2026 if (repo) {
2027 error = got_repo_close(repo);
2028 if (error)
2029 return NULL;
2031 if (error) {
2032 khttp_puts(gw_trans->gw_req, "Error: ");
2033 khttp_puts(gw_trans->gw_req, error->msg);
2034 return NULL;
2035 } else
2036 return logs;
2039 static char *
2040 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
2042 const struct got_error *error = NULL;
2043 struct got_repository *repo = NULL;
2044 struct got_reflist_head refs;
2045 struct got_reflist_entry *re;
2046 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
2047 *age = NULL;
2048 char *newline;
2049 struct buf *diffbuf = NULL;
2050 size_t newsize;
2052 error = buf_alloc(&diffbuf, 0);
2053 if (error)
2054 return NULL;
2055 SIMPLEQ_INIT(&refs);
2057 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2058 if (error)
2059 goto done;
2061 error = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
2062 if (error)
2063 goto done;
2065 SIMPLEQ_FOREACH(re, &refs, entry) {
2066 const char *refname;
2067 char *refstr, *tag_log0, *tag_log, *id_str;
2068 time_t tagger_time;
2069 struct got_object_id *id;
2070 struct got_tag_object *tag;
2072 refname = got_ref_get_name(re->ref);
2073 if (strncmp(refname, "refs/tags/", 10) != 0)
2074 continue;
2075 refname += 10;
2076 refstr = got_ref_to_str(re->ref);
2077 if (refstr == NULL) {
2078 error = got_error_from_errno("got_ref_to_str");
2079 goto done;
2082 error = got_ref_resolve(&id, repo, re->ref);
2083 if (error)
2084 goto done;
2085 error = got_object_open_as_tag(&tag, repo, id);
2086 free(id);
2087 if (error)
2088 goto done;
2090 tagger_time = got_object_tag_get_tagger_time(tag);
2092 error = got_object_id_str(&id_str,
2093 got_object_tag_get_object_id(tag));
2094 if (error)
2095 goto done;
2097 tag_log0 = strdup(got_object_tag_get_message(tag));
2099 if (tag_log0 == NULL) {
2100 error = got_error_from_errno("strdup");
2101 goto done;
2104 tag_log = tag_log0;
2105 while (*tag_log == '\n')
2106 tag_log++;
2108 switch (tag_type) {
2109 case TAGBRIEF:
2110 newline = strchr(tag_log, '\n');
2111 if (newline)
2112 *newline = '\0';
2114 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
2115 TM_DIFF))) == -1) {
2116 error = got_error_from_errno("asprintf");
2117 goto done;
2120 if ((asprintf(&tags_navs_disp, tags_navs,
2121 gw_trans->repo_name, id_str, gw_trans->repo_name,
2122 id_str, gw_trans->repo_name, id_str,
2123 gw_trans->repo_name, id_str)) == -1) {
2124 error = got_error_from_errno("asprintf");
2125 goto done;
2128 if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
2129 tags_navs_disp)) == -1) {
2130 error = got_error_from_errno("asprintf");
2131 goto done;
2134 free(tags_navs_disp);
2135 break;
2136 case TAGFULL:
2137 break;
2138 default:
2139 break;
2142 got_object_tag_close(tag);
2144 error = buf_puts(&newsize, diffbuf, tag_row);
2146 free(id_str);
2147 free(refstr);
2148 free(age);
2149 free(tag_log0);
2150 free(tag_row);
2152 if (error || (limit && --limit == 0))
2153 break;
2156 if (buf_len(diffbuf) > 0) {
2157 error = buf_putc(diffbuf, '\0');
2158 tags = strdup(buf_get(diffbuf));
2160 done:
2161 buf_free(diffbuf);
2162 got_ref_list_free(&refs);
2163 if (repo)
2164 got_repo_close(repo);
2165 if (error)
2166 return NULL;
2167 else
2168 return tags;
2171 struct blame_line {
2172 int annotated;
2173 char *id_str;
2174 char *committer;
2175 char datebuf[11]; /* YYYY-MM-DD + NUL */
2178 struct gw_blame_cb_args {
2179 struct blame_line *lines;
2180 int nlines;
2181 int nlines_prec;
2182 int lineno_cur;
2183 off_t *line_offsets;
2184 FILE *f;
2185 struct got_repository *repo;
2186 struct gw_trans *gw_trans;
2187 struct buf *blamebuf;
2190 static const struct got_error *
2191 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2193 const struct got_error *err = NULL;
2194 struct gw_blame_cb_args *a = arg;
2195 struct blame_line *bline;
2196 char *line = NULL;
2197 size_t linesize = 0, newsize;
2198 struct got_commit_object *commit = NULL;
2199 off_t offset;
2200 struct tm tm;
2201 time_t committer_time;
2203 if (nlines != a->nlines ||
2204 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2205 return got_error(GOT_ERR_RANGE);
2207 if (lineno == -1)
2208 return NULL; /* no change in this commit */
2210 /* Annotate this line. */
2211 bline = &a->lines[lineno - 1];
2212 if (bline->annotated)
2213 return NULL;
2214 err = got_object_id_str(&bline->id_str, id);
2215 if (err)
2216 return err;
2218 err = got_object_open_as_commit(&commit, a->repo, id);
2219 if (err)
2220 goto done;
2222 bline->committer = strdup(got_object_commit_get_committer(commit));
2223 if (bline->committer == NULL) {
2224 err = got_error_from_errno("strdup");
2225 goto done;
2228 committer_time = got_object_commit_get_committer_time(commit);
2229 if (localtime_r(&committer_time, &tm) == NULL)
2230 return got_error_from_errno("localtime_r");
2231 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2232 &tm) >= sizeof(bline->datebuf)) {
2233 err = got_error(GOT_ERR_NO_SPACE);
2234 goto done;
2236 bline->annotated = 1;
2238 /* Print lines annotated so far. */
2239 bline = &a->lines[a->lineno_cur - 1];
2240 if (!bline->annotated)
2241 goto done;
2243 offset = a->line_offsets[a->lineno_cur - 1];
2244 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2245 err = got_error_from_errno("fseeko");
2246 goto done;
2249 while (bline->annotated) {
2250 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2251 *line_escape = NULL;
2252 size_t len;
2254 if (getline(&line, &linesize, a->f) == -1) {
2255 if (ferror(a->f))
2256 err = got_error_from_errno("getline");
2257 break;
2260 committer = bline->committer;
2261 smallerthan = strchr(committer, '<');
2262 if (smallerthan && smallerthan[1] != '\0')
2263 committer = smallerthan + 1;
2264 at = strchr(committer, '@');
2265 if (at)
2266 *at = '\0';
2267 len = strlen(committer);
2268 if (len >= 9)
2269 committer[8] = '\0';
2271 nl = strchr(line, '\n');
2272 if (nl)
2273 *nl = '\0';
2275 if (strcmp(line, "") != 0)
2276 line_escape = strdup(gw_html_escape(line));
2277 else
2278 line_escape = strdup("");
2280 asprintf(&blame_row, log_blame_line, a->nlines_prec,
2281 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2282 line_escape);
2283 a->lineno_cur++;
2284 err = buf_puts(&newsize, a->blamebuf, blame_row);
2285 if (err)
2286 return err;
2288 bline = &a->lines[a->lineno_cur - 1];
2289 free(line_escape);
2290 free(blame_row);
2292 done:
2293 if (commit)
2294 got_object_commit_close(commit);
2295 free(line);
2296 return err;
2299 static char*
2300 gw_get_file_blame(struct gw_trans *gw_trans, char *commit_str)
2302 const struct got_error *error = NULL;
2303 struct got_repository *repo = NULL;
2304 struct got_object_id *obj_id = NULL;
2305 struct got_object_id *commit_id = NULL;
2306 struct got_blob_object *blob = NULL;
2307 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2308 *folder = NULL;
2309 struct gw_blame_cb_args bca;
2310 int i, obj_type;
2311 size_t filesize;
2313 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2314 if (error)
2315 goto done;
2317 if (gw_trans->repo_folder != NULL) {
2318 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2319 error = got_error_from_errno("asprintf");
2320 goto done;
2322 } else
2323 folder = strdup("");
2325 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2326 error = got_error_from_errno("asprintf");
2327 goto done;
2329 free(folder);
2331 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2332 if (error)
2333 goto done;
2335 error = got_repo_match_object_id(&commit_id, NULL, commit_str,
2336 GOT_OBJ_TYPE_COMMIT, 1, repo);
2337 if (error)
2338 goto done;
2340 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2341 if (error)
2342 goto done;
2344 if (obj_id == NULL) {
2345 error = got_error(GOT_ERR_NO_OBJ);
2346 goto done;
2349 error = got_object_get_type(&obj_type, repo, obj_id);
2350 if (error)
2351 goto done;
2353 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2354 error = got_error(GOT_ERR_OBJ_TYPE);
2355 goto done;
2358 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2359 if (error)
2360 goto done;
2362 error = buf_alloc(&bca.blamebuf, 0);
2363 if (error)
2364 goto done;
2366 bca.f = got_opentemp();
2367 if (bca.f == NULL) {
2368 error = got_error_from_errno("got_opentemp");
2369 goto done;
2371 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2372 &bca.line_offsets, bca.f, blob);
2373 if (error || bca.nlines == 0)
2374 goto done;
2376 /* Don't include \n at EOF in the blame line count. */
2377 if (bca.line_offsets[bca.nlines - 1] == filesize)
2378 bca.nlines--;
2380 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2381 if (bca.lines == NULL) {
2382 error = got_error_from_errno("calloc");
2383 goto done;
2385 bca.lineno_cur = 1;
2386 bca.nlines_prec = 0;
2387 i = bca.nlines;
2388 while (i > 0) {
2389 i /= 10;
2390 bca.nlines_prec++;
2392 bca.repo = repo;
2393 bca.gw_trans = gw_trans;
2395 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2396 NULL, NULL);
2397 if (buf_len(bca.blamebuf) > 0) {
2398 error = buf_putc(bca.blamebuf, '\0');
2399 blame_html = strdup(buf_get(bca.blamebuf));
2401 done:
2402 free(bca.blamebuf);
2403 free(in_repo_path);
2404 free(commit_id);
2405 free(obj_id);
2406 free(path);
2408 if (blob)
2409 error = got_object_blob_close(blob);
2410 if (repo)
2411 error = got_repo_close(repo);
2412 if (error)
2413 return NULL;
2414 if (bca.lines) {
2415 for (i = 0; i < bca.nlines; i++) {
2416 struct blame_line *bline = &bca.lines[i];
2417 free(bline->id_str);
2418 free(bline->committer);
2420 free(bca.lines);
2422 free(bca.line_offsets);
2423 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2424 error = got_error_from_errno("fclose");
2425 if (error)
2426 return NULL;
2427 else
2428 return blame_html;
2431 static char*
2432 gw_get_repo_tree(struct gw_trans *gw_trans, char *commit_str)
2434 const struct got_error *error = NULL;
2435 struct got_repository *repo = NULL;
2436 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2437 struct got_tree_object *tree = NULL;
2438 struct buf *diffbuf = NULL;
2439 size_t newsize;
2440 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2441 *tree_row = NULL, *id_str;
2442 int nentries, i;
2444 error = buf_alloc(&diffbuf, 0);
2445 if (error)
2446 return NULL;
2448 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2449 if (error)
2450 goto done;
2452 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2453 if (error)
2454 goto done;
2456 if (gw_trans->repo_folder != NULL)
2457 path = strdup(gw_trans->repo_folder);
2458 else if (in_repo_path) {
2459 free(path);
2460 path = in_repo_path;
2463 if (commit_str == NULL) {
2464 struct got_reference *head_ref;
2465 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2466 if (error)
2467 goto done;
2469 error = got_ref_resolve(&commit_id, repo, head_ref);
2470 got_ref_close(head_ref);
2472 } else
2473 error = got_repo_match_object_id(&commit_id, NULL, commit_str,
2474 GOT_OBJ_TYPE_COMMIT, 1, repo);
2475 if (error)
2476 goto done;
2478 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2479 if (error)
2480 goto done;
2482 error = got_object_open_as_tree(&tree, repo, tree_id);
2483 if (error)
2484 goto done;
2486 nentries = got_object_tree_get_nentries(tree);
2488 for (i = 0; i < nentries; i++) {
2489 struct got_tree_entry *te;
2490 const char *modestr = "";
2491 char *id = NULL, *url_html = NULL;
2493 te = got_object_tree_get_entry(tree, i);
2495 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2496 if (error)
2497 goto done;
2499 if ((asprintf(&id, "%s", id_str)) == -1) {
2500 error = got_error_from_errno("asprintf");
2501 free(id_str);
2502 goto done;
2505 mode_t mode = got_tree_entry_get_mode(te);
2507 if (got_object_tree_entry_is_submodule(te))
2508 modestr = "$";
2509 else if (S_ISLNK(mode))
2510 modestr = "@";
2511 else if (S_ISDIR(mode))
2512 modestr = "/";
2513 else if (mode & S_IXUSR)
2514 modestr = "*";
2516 char *build_folder = NULL;
2517 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2518 if (gw_trans->repo_folder != NULL) {
2519 if ((asprintf(&build_folder, "%s/%s",
2520 gw_trans->repo_folder,
2521 got_tree_entry_get_name(te))) == -1) {
2522 error =
2523 got_error_from_errno("asprintf");
2524 goto done;
2526 } else {
2527 if (asprintf(&build_folder, "%s",
2528 got_tree_entry_get_name(te)) == -1)
2529 goto done;
2532 if ((asprintf(&url_html, folder_html,
2533 gw_trans->repo_name, gw_trans->action_name,
2534 gw_trans->commit, build_folder,
2535 got_tree_entry_get_name(te), modestr)) == -1) {
2536 error = got_error_from_errno("asprintf");
2537 goto done;
2539 } else {
2540 if (gw_trans->repo_folder != NULL) {
2541 if ((asprintf(&build_folder, "%s",
2542 gw_trans->repo_folder)) == -1) {
2543 error =
2544 got_error_from_errno("asprintf");
2545 goto done;
2547 } else
2548 build_folder = strdup("");
2550 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2551 "blame", gw_trans->commit,
2552 got_tree_entry_get_name(te), build_folder,
2553 got_tree_entry_get_name(te), modestr)) == -1) {
2554 error = got_error_from_errno("asprintf");
2555 goto done;
2558 free(build_folder);
2560 if (error)
2561 goto done;
2563 if ((asprintf(&tree_row, trees_row, "", url_html)) == -1) {
2564 error = got_error_from_errno("asprintf");
2565 goto done;
2567 error = buf_puts(&newsize, diffbuf, tree_row);
2568 if (error)
2569 goto done;
2571 free(id);
2572 free(id_str);
2573 free(url_html);
2574 free(tree_row);
2577 if (buf_len(diffbuf) > 0) {
2578 error = buf_putc(diffbuf, '\0');
2579 tree_html = strdup(buf_get(diffbuf));
2581 done:
2582 if (tree)
2583 got_object_tree_close(tree);
2584 if (repo)
2585 got_repo_close(repo);
2587 free(in_repo_path);
2588 free(tree_id);
2589 free(diffbuf);
2590 if (error)
2591 return NULL;
2592 else
2593 return tree_html;
2596 static char *
2597 gw_get_repo_heads(struct gw_trans *gw_trans)
2599 const struct got_error *error = NULL;
2600 struct got_repository *repo = NULL;
2601 struct got_reflist_head refs;
2602 struct got_reflist_entry *re;
2603 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2604 struct buf *diffbuf = NULL;
2605 size_t newsize;
2607 error = buf_alloc(&diffbuf, 0);
2608 if (error)
2609 return NULL;
2611 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2612 if (error)
2613 goto done;
2615 SIMPLEQ_INIT(&refs);
2616 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2617 NULL);
2618 if (error)
2619 goto done;
2621 SIMPLEQ_FOREACH(re, &refs, entry) {
2622 char *refname;
2624 refname = strdup(got_ref_get_name(re->ref));
2625 if (refname == NULL) {
2626 error = got_error_from_errno("got_ref_to_str");
2627 goto done;
2630 if (strncmp(refname, "refs/heads/", 11) != 0) {
2631 free(refname);
2632 continue;
2635 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2636 TM_DIFF);
2638 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2639 refname, gw_trans->repo_name, refname,
2640 gw_trans->repo_name, refname, gw_trans->repo_name,
2641 refname)) == -1) {
2642 error = got_error_from_errno("asprintf");
2643 goto done;
2646 if (strncmp(refname, "refs/heads/", 11) == 0)
2647 refname += 11;
2649 if ((asprintf(&head_row, heads_row, age, refname,
2650 head_navs_disp)) == -1) {
2651 error = got_error_from_errno("asprintf");
2652 goto done;
2655 error = buf_puts(&newsize, diffbuf, head_row);
2657 free(head_navs_disp);
2658 free(head_row);
2661 if (buf_len(diffbuf) > 0) {
2662 error = buf_putc(diffbuf, '\0');
2663 heads = strdup(buf_get(diffbuf));
2665 done:
2666 buf_free(diffbuf);
2667 got_ref_list_free(&refs);
2668 if (repo)
2669 got_repo_close(repo);
2670 if (error)
2671 return NULL;
2672 else
2673 return heads;
2676 static char *
2677 gw_get_got_link(struct gw_trans *gw_trans)
2679 char *link;
2681 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2682 gw_trans->gw_conf->got_logo)) == -1)
2683 return NULL;
2685 return link;
2688 static char *
2689 gw_get_site_link(struct gw_trans *gw_trans)
2691 char *link, *repo = "", *action = "";
2693 if (gw_trans->repo_name != NULL)
2694 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2695 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2696 return NULL;
2698 if (gw_trans->action_name != NULL)
2699 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2700 return NULL;
2702 if ((asprintf(&link, site_link, GOTWEB,
2703 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2704 return NULL;
2706 return link;
2709 static char *
2710 gw_colordiff_line(char *buf)
2712 const struct got_error *error = NULL;
2713 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2714 struct buf *diffbuf = NULL;
2715 size_t newsize;
2717 error = buf_alloc(&diffbuf, 0);
2718 if (error)
2719 return NULL;
2721 if (strncmp(buf, "-", 1) == 0)
2722 color = "diff_minus";
2723 if (strncmp(buf, "+", 1) == 0)
2724 color = "diff_plus";
2725 if (strncmp(buf, "@@", 2) == 0)
2726 color = "diff_chunk_header";
2727 if (strncmp(buf, "@@", 2) == 0)
2728 color = "diff_chunk_header";
2729 if (strncmp(buf, "commit +", 8) == 0)
2730 color = "diff_meta";
2731 if (strncmp(buf, "commit -", 8) == 0)
2732 color = "diff_meta";
2733 if (strncmp(buf, "blob +", 6) == 0)
2734 color = "diff_meta";
2735 if (strncmp(buf, "blob -", 6) == 0)
2736 color = "diff_meta";
2737 if (strncmp(buf, "file +", 6) == 0)
2738 color = "diff_meta";
2739 if (strncmp(buf, "file -", 6) == 0)
2740 color = "diff_meta";
2741 if (strncmp(buf, "from:", 5) == 0)
2742 color = "diff_author";
2743 if (strncmp(buf, "via:", 4) == 0)
2744 color = "diff_author";
2745 if (strncmp(buf, "date:", 5) == 0)
2746 color = "diff_date";
2748 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2749 return NULL;
2751 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2752 if (error)
2753 return NULL;
2755 error = buf_puts(&newsize, diffbuf, buf);
2756 if (error)
2757 return NULL;
2759 if (buf_len(diffbuf) > 0) {
2760 error = buf_putc(diffbuf, '\0');
2761 colorized_line = strdup(buf_get(diffbuf));
2764 free(diffbuf);
2765 free(div_diff_line_div);
2766 return colorized_line;
2769 static char *
2770 gw_html_escape(const char *html)
2772 char *escaped_str = NULL, *buf;
2773 char c[1];
2774 size_t sz, i, buff_sz = 2048;
2776 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2777 return NULL;
2779 if (html == NULL)
2780 return NULL;
2781 else
2782 if ((sz = strlen(html)) == 0)
2783 return NULL;
2785 /* only work with buff_sz */
2786 if (buff_sz < sz)
2787 sz = buff_sz;
2789 for (i = 0; i < sz; i++) {
2790 c[0] = html[i];
2791 switch (c[0]) {
2792 case ('>'):
2793 strcat(buf, "&gt;");
2794 break;
2795 case ('&'):
2796 strcat(buf, "&amp;");
2797 break;
2798 case ('<'):
2799 strcat(buf, "&lt;");
2800 break;
2801 case ('"'):
2802 strcat(buf, "&quot;");
2803 break;
2804 case ('\''):
2805 strcat(buf, "&apos;");
2806 break;
2807 case ('\n'):
2808 strcat(buf, "<br />");
2809 default:
2810 strcat(buf, &c[0]);
2811 break;
2814 asprintf(&escaped_str, "%s", buf);
2815 free(buf);
2816 return escaped_str;
2819 int
2820 main()
2822 const struct got_error *error = NULL;
2823 struct gw_trans *gw_trans;
2824 struct gw_dir *dir = NULL, *tdir;
2825 const char *page = "index";
2826 int gw_malloc = 1;
2828 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2829 errx(1, "malloc");
2831 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2832 errx(1, "malloc");
2834 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2835 errx(1, "malloc");
2837 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2838 errx(1, "malloc");
2840 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX,
2841 &page, 1, 0))
2842 errx(1, "khttp_parse");
2844 if ((gw_trans->gw_conf =
2845 malloc(sizeof(struct gotweb_conf))) == NULL) {
2846 gw_malloc = 0;
2847 error = got_error_from_errno("malloc");
2848 goto err;
2851 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
2852 NULL) == -1) {
2853 error = got_error_from_errno("pledge");
2854 goto err;
2857 TAILQ_INIT(&gw_trans->gw_dirs);
2859 gw_trans->page = 0;
2860 gw_trans->repos_total = 0;
2861 gw_trans->repo_path = NULL;
2862 gw_trans->commit = NULL;
2863 gw_trans->headref = strdup(GOT_REF_HEAD);
2864 gw_trans->mime = KMIME_TEXT_HTML;
2865 gw_trans->gw_tmpl->key = gw_templs;
2866 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2867 gw_trans->gw_tmpl->arg = gw_trans;
2868 gw_trans->gw_tmpl->cb = gw_template;
2869 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2871 err:
2872 if (error) {
2873 gw_trans->mime = KMIME_TEXT_PLAIN;
2874 gw_trans->action = GW_ERR;
2875 gw_display_index(gw_trans, error);
2876 goto done;
2879 error = gw_parse_querystring(gw_trans);
2880 if (error)
2881 goto err;
2883 gw_display_index(gw_trans, error);
2885 done:
2886 if (gw_malloc) {
2887 free(gw_trans->gw_conf->got_repos_path);
2888 free(gw_trans->gw_conf->got_www_path);
2889 free(gw_trans->gw_conf->got_site_name);
2890 free(gw_trans->gw_conf->got_site_owner);
2891 free(gw_trans->gw_conf->got_site_link);
2892 free(gw_trans->gw_conf->got_logo);
2893 free(gw_trans->gw_conf->got_logo_url);
2894 free(gw_trans->gw_conf);
2895 free(gw_trans->commit);
2896 free(gw_trans->repo_path);
2897 free(gw_trans->repo_name);
2898 free(gw_trans->repo_file);
2899 free(gw_trans->action_name);
2900 free(gw_trans->headref);
2902 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2903 free(dir->name);
2904 free(dir->description);
2905 free(dir->age);
2906 free(dir->url);
2907 free(dir->path);
2908 free(dir);
2913 khttp_free(gw_trans->gw_req);
2914 return EXIT_SUCCESS;