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* gw_blame_cb(void *, int, int,
183 struct got_object_id *);
184 static const struct got_error* gw_load_got_paths(struct gw_trans *);
185 static const struct got_error* gw_load_got_path(struct gw_trans *,
186 struct gw_dir *);
187 static const struct got_error* gw_parse_querystring(struct gw_trans *);
188 static const struct got_error* gw_match_logmsg(int *, struct got_object_id *,
189 struct got_commit_object *, regex_t *);
191 static const struct got_error* gw_blame(struct gw_trans *);
192 static const struct got_error* gw_commit(struct gw_trans *);
193 static const struct got_error* gw_commitdiff(struct gw_trans *);
194 static const struct got_error* gw_index(struct gw_trans *);
195 static const struct got_error* gw_log(struct gw_trans *);
196 static const struct got_error* gw_raw(struct gw_trans *);
197 static const struct got_error* gw_logbriefs(struct gw_trans *);
198 static const struct got_error* gw_summary(struct gw_trans *);
199 static const struct got_error* gw_tag(struct gw_trans *);
200 static const struct got_error* gw_tree(struct gw_trans *);
202 struct gw_query_action {
203 unsigned int func_id;
204 const char *func_name;
205 const struct got_error *(*func_main)(struct gw_trans *);
206 char *template;
207 };
209 enum gw_query_actions {
210 GW_BLAME,
211 GW_COMMIT,
212 GW_COMMITDIFF,
213 GW_ERR,
214 GW_INDEX,
215 GW_LOG,
216 GW_RAW,
217 GW_LOGBRIEFS,
218 GW_SUMMARY,
219 GW_TAG,
220 GW_TREE,
221 };
223 static struct gw_query_action gw_query_funcs[] = {
224 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
225 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
226 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
227 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
228 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
229 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
230 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
231 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
232 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
233 { GW_TAG, "tag", gw_tag, "gw_tmpl/index.tmpl" },
234 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
235 };
237 static const struct got_error *
238 gw_apply_unveil(const char *repo_path, const char *repo_file)
240 const struct got_error *err;
242 if (repo_path && repo_file) {
243 char *full_path;
244 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
245 return got_error_from_errno("asprintf unveil");
246 if (unveil(full_path, "r") != 0)
247 return got_error_from_errno2("unveil", full_path);
250 if (repo_path && unveil(repo_path, "r") != 0)
251 return got_error_from_errno2("unveil", repo_path);
253 if (unveil("/tmp", "rwc") != 0)
254 return got_error_from_errno2("unveil", "/tmp");
256 err = got_privsep_unveil_exec_helpers();
257 if (err != NULL)
258 return err;
260 if (unveil(NULL, NULL) != 0)
261 return got_error_from_errno("unveil");
263 return NULL;
266 int
267 gw_get_repo_log_count(struct gw_trans *gw_trans, char *start_commit)
269 const struct got_error *error;
270 struct got_repository *repo = NULL;
271 struct got_reflist_head refs;
272 struct got_commit_object *commit = NULL;
273 struct got_object_id *id = NULL;
274 struct got_commit_graph *graph = NULL;
275 char *in_repo_path = NULL, *path = NULL;
276 int log_count = 0;
278 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
279 if (error)
280 return 0;
282 SIMPLEQ_INIT(&refs);
284 if (start_commit == NULL) {
285 struct got_reference *head_ref;
286 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
287 if (error)
288 goto done;
290 error = got_ref_resolve(&id, repo, head_ref);
291 got_ref_close(head_ref);
292 if (error)
293 goto done;
295 error = got_object_open_as_commit(&commit, repo, id);
296 } else {
297 struct got_reference *ref;
298 error = got_ref_open(&ref, repo, start_commit, 0);
299 if (error == NULL) {
300 int obj_type;
301 error = got_ref_resolve(&id, repo, ref);
302 got_ref_close(ref);
303 if (error)
304 goto done;
305 error = got_object_get_type(&obj_type, repo, id);
306 if (error)
307 goto done;
308 if (obj_type == GOT_OBJ_TYPE_TAG) {
309 struct got_tag_object *tag;
310 error = got_object_open_as_tag(&tag, repo, id);
311 if (error)
312 goto done;
313 if (got_object_tag_get_object_type(tag) !=
314 GOT_OBJ_TYPE_COMMIT) {
315 got_object_tag_close(tag);
316 error = got_error(GOT_ERR_OBJ_TYPE);
317 goto done;
319 free(id);
320 id = got_object_id_dup(
321 got_object_tag_get_object_id(tag));
322 if (id == NULL)
323 error = got_error_from_errno(
324 "got_object_id_dup");
325 got_object_tag_close(tag);
326 if (error)
327 goto done;
328 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
329 error = got_error(GOT_ERR_OBJ_TYPE);
330 goto done;
332 error = got_object_open_as_commit(&commit, repo, id);
333 if (error)
334 goto done;
336 if (commit == NULL) {
337 error = got_repo_match_object_id_prefix(&id,
338 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
339 if (error)
340 goto done;
342 error = got_repo_match_object_id_prefix(&id,
343 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
344 if (error)
345 goto done;
348 error = got_object_open_as_commit(&commit, repo, id);
349 if (error)
350 goto done;
352 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
353 if (error)
354 goto done;
356 if (in_repo_path) {
357 free(path);
358 path = in_repo_path;
361 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
362 if (error)
363 goto done;
365 error = got_commit_graph_open(&graph, path, 0);
366 if (error)
367 goto done;
369 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
370 if (error)
371 goto done;
373 for (;;) {
374 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
375 NULL);
376 if (error) {
377 if (error->code == GOT_ERR_ITER_COMPLETED)
378 error = NULL;
379 break;
381 if (id == NULL)
382 break;
384 if (error)
385 break;
386 log_count++;
388 done:
389 free(in_repo_path);
390 if (graph)
391 got_commit_graph_close(graph);
392 if (repo) {
393 error = got_repo_close(repo);
394 if (error)
395 return 0;
397 if (error) {
398 khttp_puts(gw_trans->gw_req, "Error: ");
399 khttp_puts(gw_trans->gw_req, error->msg);
400 return 0;
401 } else
402 return log_count;
405 static const struct got_error *
406 gw_blame(struct gw_trans *gw_trans)
408 const struct got_error *error = NULL;
410 char *log, *log_html;
412 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
413 if (error)
414 return error;
416 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGBLAME);
418 if (log != NULL && strcmp(log, "") != 0) {
419 if ((asprintf(&log_html, log_blame, log)) == -1)
420 return got_error_from_errno("asprintf");
421 khttp_puts(gw_trans->gw_req, log_html);
422 free(log_html);
423 free(log);
425 return error;
428 static const struct got_error *
429 gw_commit(struct gw_trans *gw_trans)
431 const struct got_error *error = NULL;
432 char *log, *log_html;
434 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
435 if (error)
436 return error;
438 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
440 if (log != NULL && strcmp(log, "") != 0) {
441 if ((asprintf(&log_html, log_commit, log)) == -1)
442 return got_error_from_errno("asprintf");
443 khttp_puts(gw_trans->gw_req, log_html);
444 free(log_html);
445 free(log);
447 return error;
450 static const struct got_error *
451 gw_commitdiff(struct gw_trans *gw_trans)
453 const struct got_error *error = NULL;
454 char *log, *log_html;
456 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
457 if (error)
458 return error;
460 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
462 if (log != NULL && strcmp(log, "") != 0) {
463 if ((asprintf(&log_html, log_diff, log)) == -1)
464 return got_error_from_errno("asprintf");
465 khttp_puts(gw_trans->gw_req, log_html);
466 free(log_html);
467 free(log);
469 return error;
472 static const struct got_error *
473 gw_index(struct gw_trans *gw_trans)
475 const struct got_error *error = NULL;
476 struct gw_dir *gw_dir = NULL;
477 char *html, *navs, *next, *prev;
478 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
480 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
481 if (error)
482 return error;
484 error = gw_load_got_paths(gw_trans);
485 if (error)
486 return error;
488 khttp_puts(gw_trans->gw_req, index_projects_header);
490 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
491 dir_c++;
493 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
494 if (gw_trans->page > 0 && (gw_trans->page *
495 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
496 prev_disp++;
497 continue;
500 prev_disp++;
501 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
502 gw_dir->name, gw_dir->name)) == -1)
503 return got_error_from_errno("asprintf");
505 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
506 gw_dir->description, gw_dir->owner, gw_dir->age,
507 navs)) == -1)
508 return got_error_from_errno("asprintf");
510 khttp_puts(gw_trans->gw_req, html);
512 free(navs);
513 free(html);
515 if (gw_trans->gw_conf->got_max_repos_display == 0)
516 continue;
518 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
519 khttp_puts(gw_trans->gw_req, np_wrapper_start);
520 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
521 (gw_trans->page > 0) &&
522 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
523 prev_disp == gw_trans->repos_total))
524 khttp_puts(gw_trans->gw_req, np_wrapper_start);
526 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
527 (gw_trans->page > 0) &&
528 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
529 prev_disp == gw_trans->repos_total)) {
530 if ((asprintf(&prev, nav_prev,
531 gw_trans->page - 1)) == -1)
532 return got_error_from_errno("asprintf");
533 khttp_puts(gw_trans->gw_req, prev);
534 free(prev);
537 khttp_puts(gw_trans->gw_req, div_end);
539 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
540 next_disp == gw_trans->gw_conf->got_max_repos_display &&
541 dir_c != (gw_trans->page + 1) *
542 gw_trans->gw_conf->got_max_repos_display) {
543 if ((asprintf(&next, nav_next,
544 gw_trans->page + 1)) == -1)
545 return got_error_from_errno("calloc");
546 khttp_puts(gw_trans->gw_req, next);
547 khttp_puts(gw_trans->gw_req, div_end);
548 free(next);
549 next_disp = 0;
550 break;
553 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
554 (gw_trans->page > 0) &&
555 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
556 prev_disp == gw_trans->repos_total))
557 khttp_puts(gw_trans->gw_req, div_end);
559 next_disp++;
561 return error;
564 static const struct got_error *
565 gw_log(struct gw_trans *gw_trans)
567 const struct got_error *error = NULL;
568 char *log, *log_html;
570 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
571 if (error)
572 return error;
574 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
575 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
577 if (log != NULL && strcmp(log, "") != 0) {
578 if ((asprintf(&log_html, logs, log)) == -1)
579 return got_error_from_errno("asprintf");
580 khttp_puts(gw_trans->gw_req, log_html);
581 free(log_html);
582 free(log);
584 return error;
587 static const struct got_error *
588 gw_raw(struct gw_trans *gw_trans)
590 const struct got_error *error = NULL;
592 return error;
595 static const struct got_error *
596 gw_logbriefs(struct gw_trans *gw_trans)
598 const struct got_error *error = NULL;
599 char *log, *log_html;
601 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
602 if (error)
603 return error;
605 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
606 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
608 if (log != NULL && strcmp(log, "") != 0) {
609 if ((asprintf(&log_html, summary_logbriefs,
610 log)) == -1)
611 return got_error_from_errno("asprintf");
612 khttp_puts(gw_trans->gw_req, log_html);
613 free(log_html);
614 free(log);
616 return error;
619 static const struct got_error *
620 gw_summary(struct gw_trans *gw_trans)
622 const struct got_error *error = NULL;
623 char *description_html, *repo_owner_html, *repo_age_html,
624 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
625 *heads_html, *age;
627 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
628 if (error)
629 return error;
631 khttp_puts(gw_trans->gw_req, summary_wrapper);
632 if (gw_trans->gw_conf->got_show_repo_description) {
633 if (gw_trans->gw_dir->description != NULL &&
634 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
635 if ((asprintf(&description_html, description,
636 gw_trans->gw_dir->description)) == -1)
637 return got_error_from_errno("asprintf");
639 khttp_puts(gw_trans->gw_req, description_html);
640 free(description_html);
644 if (gw_trans->gw_conf->got_show_repo_owner) {
645 if (gw_trans->gw_dir->owner != NULL &&
646 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
647 if ((asprintf(&repo_owner_html, repo_owner,
648 gw_trans->gw_dir->owner)) == -1)
649 return got_error_from_errno("asprintf");
651 khttp_puts(gw_trans->gw_req, repo_owner_html);
652 free(repo_owner_html);
656 if (gw_trans->gw_conf->got_show_repo_age) {
657 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
658 "refs/heads", TM_LONG);
659 if (age != NULL && (strcmp(age, "") != 0)) {
660 if ((asprintf(&repo_age_html, last_change, age)) == -1)
661 return got_error_from_errno("asprintf");
663 khttp_puts(gw_trans->gw_req, repo_age_html);
664 free(repo_age_html);
665 free(age);
669 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
670 if (gw_trans->gw_dir->url != NULL &&
671 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
672 if ((asprintf(&cloneurl_html, cloneurl,
673 gw_trans->gw_dir->url)) == -1)
674 return got_error_from_errno("asprintf");
676 khttp_puts(gw_trans->gw_req, cloneurl_html);
677 free(cloneurl_html);
680 khttp_puts(gw_trans->gw_req, div_end);
682 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
683 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
684 heads = gw_get_repo_heads(gw_trans);
686 if (log != NULL && strcmp(log, "") != 0) {
687 if ((asprintf(&log_html, summary_logbriefs,
688 log)) == -1)
689 return got_error_from_errno("asprintf");
690 khttp_puts(gw_trans->gw_req, log_html);
691 free(log_html);
692 free(log);
695 if (tags != NULL && strcmp(tags, "") != 0) {
696 if ((asprintf(&tags_html, summary_tags,
697 tags)) == -1)
698 return got_error_from_errno("asprintf");
699 khttp_puts(gw_trans->gw_req, tags_html);
700 free(tags_html);
701 free(tags);
704 if (heads != NULL && strcmp(heads, "") != 0) {
705 if ((asprintf(&heads_html, summary_heads,
706 heads)) == -1)
707 return got_error_from_errno("asprintf");
708 khttp_puts(gw_trans->gw_req, heads_html);
709 free(heads_html);
710 free(heads);
712 return error;
715 static const struct got_error *
716 gw_tag(struct gw_trans *gw_trans)
718 const struct got_error *error = NULL;
719 char *log, *log_html;
721 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
722 if (error)
723 return error;
725 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTAG);
727 if (log != NULL && strcmp(log, "") != 0) {
728 if ((asprintf(&log_html, log_tag, 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_tree(struct gw_trans *gw_trans)
740 const struct got_error *error = NULL;
741 char *log, *log_html;
743 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
744 if (error)
745 return error;
747 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
749 if (log != NULL && strcmp(log, "") != 0) {
750 if ((asprintf(&log_html, log_tree, log)) == -1)
751 return got_error_from_errno("asprintf");
752 khttp_puts(gw_trans->gw_req, log_html);
753 free(log_html);
754 free(log);
756 return error;
759 static const struct got_error *
760 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
762 const struct got_error *error = NULL;
763 DIR *dt;
764 char *dir_test;
765 int opened = 0;
767 if ((asprintf(&dir_test, "%s/%s/%s",
768 gw_trans->gw_conf->got_repos_path, gw_dir->name,
769 GOTWEB_GIT_DIR)) == -1)
770 return got_error_from_errno("asprintf");
772 dt = opendir(dir_test);
773 if (dt == NULL) {
774 free(dir_test);
775 } else {
776 gw_dir->path = strdup(dir_test);
777 opened = 1;
778 goto done;
781 if ((asprintf(&dir_test, "%s/%s/%s",
782 gw_trans->gw_conf->got_repos_path, gw_dir->name,
783 GOTWEB_GOT_DIR)) == -1)
784 return got_error_from_errno("asprintf");
786 dt = opendir(dir_test);
787 if (dt == NULL)
788 free(dir_test);
789 else {
790 opened = 1;
791 error = got_error(GOT_ERR_NOT_GIT_REPO);
792 goto errored;
795 if ((asprintf(&dir_test, "%s/%s",
796 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
797 return got_error_from_errno("asprintf");
799 gw_dir->path = strdup(dir_test);
801 done:
802 gw_dir->description = gw_get_repo_description(gw_trans,
803 gw_dir->path);
804 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
805 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
806 TM_DIFF);
807 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
809 errored:
810 free(dir_test);
811 if (opened)
812 closedir(dt);
813 return error;
816 static const struct got_error *
817 gw_load_got_paths(struct gw_trans *gw_trans)
819 const struct got_error *error = NULL;
820 DIR *d;
821 struct dirent **sd_dent;
822 struct gw_dir *gw_dir;
823 struct stat st;
824 unsigned int d_cnt, d_i;
826 d = opendir(gw_trans->gw_conf->got_repos_path);
827 if (d == NULL) {
828 error = got_error_from_errno2("opendir",
829 gw_trans->gw_conf->got_repos_path);
830 return error;
833 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
834 alphasort);
835 if (d_cnt == -1) {
836 error = got_error_from_errno2("scandir",
837 gw_trans->gw_conf->got_repos_path);
838 return error;
841 for (d_i = 0; d_i < d_cnt; d_i++) {
842 if (gw_trans->gw_conf->got_max_repos > 0 &&
843 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
844 break; /* account for parent and self */
846 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
847 strcmp(sd_dent[d_i]->d_name, "..") == 0)
848 continue;
850 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
851 return got_error_from_errno("gw_dir malloc");
853 error = gw_load_got_path(gw_trans, gw_dir);
854 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
855 continue;
856 else if (error)
857 return error;
859 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
860 !got_path_dir_is_empty(gw_dir->path)) {
861 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
862 entry);
863 gw_trans->repos_total++;
867 closedir(d);
868 return error;
871 static const struct got_error *
872 gw_parse_querystring(struct gw_trans *gw_trans)
874 const struct got_error *error = NULL;
875 struct kpair *p;
876 struct gw_query_action *action = NULL;
877 unsigned int i;
879 if (gw_trans->gw_req->fieldnmap[0]) {
880 error = got_error_from_errno("bad parse");
881 return error;
882 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
883 /* define gw_trans->repo_path */
884 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
885 return got_error_from_errno("asprintf");
887 if ((asprintf(&gw_trans->repo_path, "%s/%s",
888 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
889 return got_error_from_errno("asprintf");
891 /* get action and set function */
892 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
893 for (i = 0; i < nitems(gw_query_funcs); i++) {
894 action = &gw_query_funcs[i];
895 if (action->func_name == NULL)
896 continue;
898 if (strcmp(action->func_name,
899 p->parsed.s) == 0) {
900 gw_trans->action = i;
901 if ((asprintf(&gw_trans->action_name,
902 "%s", action->func_name)) == -1)
903 return
904 got_error_from_errno(
905 "asprintf");
907 break;
910 action = NULL;
913 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
914 if ((asprintf(&gw_trans->commit, "%s",
915 p->parsed.s)) == -1)
916 return got_error_from_errno("asprintf");
918 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
919 if ((asprintf(&gw_trans->repo_file, "%s",
920 p->parsed.s)) == -1)
921 return got_error_from_errno("asprintf");
923 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
924 if ((asprintf(&gw_trans->repo_folder, "%s",
925 p->parsed.s)) == -1)
926 return got_error_from_errno("asprintf");
928 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
929 if ((asprintf(&gw_trans->headref, "%s",
930 p->parsed.s)) == -1)
931 return got_error_from_errno("asprintf");
933 if (action == NULL) {
934 error = got_error_from_errno("invalid action");
935 return error;
937 if ((gw_trans->gw_dir =
938 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
939 return got_error_from_errno("gw_dir malloc");
941 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
942 if (error)
943 return error;
944 } else
945 gw_trans->action = GW_INDEX;
947 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
948 gw_trans->page = p->parsed.i;
950 if (gw_trans->action == GW_RAW)
951 gw_trans->mime = KMIME_TEXT_PLAIN;
953 return error;
956 static struct gw_dir *
957 gw_init_gw_dir(char *dir)
959 struct gw_dir *gw_dir;
961 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
962 return NULL;
964 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
965 return NULL;
967 return gw_dir;
970 static const struct got_error*
971 gw_match_logmsg(int *have_match, struct got_object_id *id,
972 struct got_commit_object *commit, regex_t *regex)
974 const struct got_error *err = NULL;
975 regmatch_t regmatch;
976 char *id_str = NULL, *logmsg = NULL;
978 *have_match = 0;
980 err = got_object_id_str(&id_str, id);
981 if (err)
982 return err;
984 err = got_object_commit_get_logmsg(&logmsg, commit);
985 if (err)
986 goto done;
988 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
989 *have_match = 1;
990 done:
991 free(id_str);
992 free(logmsg);
993 return err;
996 static void
997 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
999 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1000 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1001 khttps[code]);
1002 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1003 kmimetypes[mime]);
1004 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1005 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1006 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1007 khttp_body(gw_trans->gw_req);
1010 static void
1011 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
1013 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1014 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1016 if (err)
1017 khttp_puts(gw_trans->gw_req, err->msg);
1018 else
1019 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1020 gw_query_funcs[gw_trans->action].template);
1022 khtml_close(gw_trans->gw_html_req);
1025 static int
1026 gw_template(size_t key, void *arg)
1028 const struct got_error *error = NULL;
1029 struct gw_trans *gw_trans = arg;
1030 char *gw_got_link, *gw_site_link;
1031 char *site_owner_name, *site_owner_name_h;
1033 switch (key) {
1034 case (TEMPL_HEAD):
1035 khttp_puts(gw_trans->gw_req, head);
1036 break;
1037 case(TEMPL_HEADER):
1038 gw_got_link = gw_get_got_link(gw_trans);
1039 if (gw_got_link != NULL)
1040 khttp_puts(gw_trans->gw_req, gw_got_link);
1042 free(gw_got_link);
1043 break;
1044 case (TEMPL_SITEPATH):
1045 gw_site_link = gw_get_site_link(gw_trans);
1046 if (gw_site_link != NULL)
1047 khttp_puts(gw_trans->gw_req, gw_site_link);
1049 free(gw_site_link);
1050 break;
1051 case(TEMPL_TITLE):
1052 if (gw_trans->gw_conf->got_site_name != NULL)
1053 khtml_puts(gw_trans->gw_html_req,
1054 gw_trans->gw_conf->got_site_name);
1056 break;
1057 case (TEMPL_SEARCH):
1058 khttp_puts(gw_trans->gw_req, search);
1059 break;
1060 case(TEMPL_SITEOWNER):
1061 if (gw_trans->gw_conf->got_site_owner != NULL &&
1062 gw_trans->gw_conf->got_show_site_owner) {
1063 site_owner_name =
1064 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1065 if ((asprintf(&site_owner_name_h, site_owner,
1066 site_owner_name))
1067 == -1)
1068 return 0;
1070 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1071 free(site_owner_name);
1072 free(site_owner_name_h);
1074 break;
1075 case(TEMPL_CONTENT):
1076 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1077 if (error)
1078 khttp_puts(gw_trans->gw_req, error->msg);
1080 break;
1081 default:
1082 return 0;
1083 break;
1085 return 1;
1088 static char *
1089 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1091 FILE *f;
1092 char *description = NULL, *d_file = NULL;
1093 unsigned int len;
1095 if (gw_trans->gw_conf->got_show_repo_description == false)
1096 goto err;
1098 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1099 goto err;
1101 if ((f = fopen(d_file, "r")) == NULL)
1102 goto err;
1104 fseek(f, 0, SEEK_END);
1105 len = ftell(f) + 1;
1106 fseek(f, 0, SEEK_SET);
1107 if ((description = calloc(len, sizeof(char *))) == NULL)
1108 goto err;
1110 fread(description, 1, len, f);
1111 fclose(f);
1112 free(d_file);
1113 return description;
1114 err:
1115 if ((asprintf(&description, "%s", "")) == -1)
1116 return NULL;
1118 return description;
1121 static char *
1122 gw_get_time_str(time_t committer_time, int ref_tm)
1124 struct tm tm;
1125 time_t diff_time;
1126 char *years = "years ago", *months = "months ago";
1127 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1128 char *minutes = "minutes ago", *seconds = "seconds ago";
1129 char *now = "right now";
1130 char *repo_age, *s;
1131 char datebuf[29];
1133 switch (ref_tm) {
1134 case TM_DIFF:
1135 diff_time = time(NULL) - committer_time;
1136 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1137 if ((asprintf(&repo_age, "%lld %s",
1138 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1139 return NULL;
1140 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1141 if ((asprintf(&repo_age, "%lld %s",
1142 (diff_time / 60 / 60 / 24 / (365 / 12)),
1143 months)) == -1)
1144 return NULL;
1145 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1146 if ((asprintf(&repo_age, "%lld %s",
1147 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1148 return NULL;
1149 } else if (diff_time > 60 * 60 * 24 * 2) {
1150 if ((asprintf(&repo_age, "%lld %s",
1151 (diff_time / 60 / 60 / 24), days)) == -1)
1152 return NULL;
1153 } else if (diff_time > 60 * 60 * 2) {
1154 if ((asprintf(&repo_age, "%lld %s",
1155 (diff_time / 60 / 60), hours)) == -1)
1156 return NULL;
1157 } else if (diff_time > 60 * 2) {
1158 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1159 minutes)) == -1)
1160 return NULL;
1161 } else if (diff_time > 2) {
1162 if ((asprintf(&repo_age, "%lld %s", diff_time,
1163 seconds)) == -1)
1164 return NULL;
1165 } else {
1166 if ((asprintf(&repo_age, "%s", now)) == -1)
1167 return NULL;
1169 break;
1170 case TM_LONG:
1171 if (gmtime_r(&committer_time, &tm) == NULL)
1172 return NULL;
1174 s = asctime_r(&tm, datebuf);
1175 if (s == NULL)
1176 return NULL;
1178 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1179 return NULL;
1180 break;
1182 return repo_age;
1185 static char *
1186 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1187 int ref_tm)
1189 const struct got_error *error = NULL;
1190 struct got_object_id *id = NULL;
1191 struct got_repository *repo = NULL;
1192 struct got_commit_object *commit = NULL;
1193 struct got_reflist_head refs;
1194 struct got_reflist_entry *re;
1195 struct got_reference *head_ref;
1196 int is_head = 0;
1197 time_t committer_time = 0, cmp_time = 0;
1198 const char *refname;
1199 char *repo_age = NULL;
1201 if (repo_ref == NULL)
1202 return NULL;
1204 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1205 is_head = 1;
1207 SIMPLEQ_INIT(&refs);
1208 if (gw_trans->gw_conf->got_show_repo_age == false) {
1209 if ((asprintf(&repo_age, "")) == -1)
1210 return NULL;
1211 return repo_age;
1214 error = got_repo_open(&repo, dir, NULL);
1215 if (error)
1216 goto err;
1218 if (is_head)
1219 error = got_ref_list(&refs, repo, "refs/heads",
1220 got_ref_cmp_by_name, NULL);
1221 else
1222 error = got_ref_list(&refs, repo, repo_ref,
1223 got_ref_cmp_by_name, NULL);
1224 if (error)
1225 goto err;
1227 SIMPLEQ_FOREACH(re, &refs, entry) {
1228 if (is_head)
1229 refname = strdup(repo_ref);
1230 else
1231 refname = got_ref_get_name(re->ref);
1232 error = got_ref_open(&head_ref, repo, refname, 0);
1233 if (error)
1234 goto err;
1236 error = got_ref_resolve(&id, repo, head_ref);
1237 got_ref_close(head_ref);
1238 if (error)
1239 goto err;
1241 error = got_object_open_as_commit(&commit, repo, id);
1242 if (error)
1243 goto err;
1245 committer_time =
1246 got_object_commit_get_committer_time(commit);
1248 if (cmp_time < committer_time)
1249 cmp_time = committer_time;
1252 if (cmp_time != 0) {
1253 committer_time = cmp_time;
1254 repo_age = gw_get_time_str(committer_time, ref_tm);
1255 } else
1256 if ((asprintf(&repo_age, "")) == -1)
1257 return NULL;
1258 got_ref_list_free(&refs);
1259 free(id);
1260 return repo_age;
1261 err:
1262 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1263 return NULL;
1265 return repo_age;
1268 static char *
1269 gw_get_repo_diff(struct gw_trans *gw_trans, char *id_str1, char *id_str2)
1271 const struct got_error *error;
1272 FILE *f = NULL;
1273 struct got_object_id *id1 = NULL, *id2 = NULL;
1274 struct got_repository *repo = NULL;
1275 struct buf *diffbuf = NULL;
1276 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1277 *buf_color = NULL;
1278 int type1, type2;
1279 size_t newsize;
1281 f = got_opentemp();
1282 if (f == NULL)
1283 return NULL;
1285 error = buf_alloc(&diffbuf, 0);
1286 if (error)
1287 return NULL;
1289 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1290 if (error)
1291 goto done;
1293 error = got_repo_match_object_id(&id1, &label1, id_str1,
1294 GOT_OBJ_TYPE_ANY, 1, repo);
1295 if (error)
1296 goto done;
1298 if (id_str2) {
1299 error = got_repo_match_object_id(&id2, &label2, id_str2,
1300 GOT_OBJ_TYPE_ANY, 1, repo);
1301 if (error)
1302 goto done;
1304 error = got_object_get_type(&type2, repo, id2);
1305 if (error)
1306 goto done;
1309 error = got_object_get_type(&type1, repo, id1);
1310 if (error)
1311 goto done;
1313 if (id_str2 && type1 != type2) {
1314 error = got_error(GOT_ERR_OBJ_TYPE);
1315 goto done;
1318 switch (type1) {
1319 case GOT_OBJ_TYPE_BLOB:
1320 error = got_diff_objects_as_blobs(id2, id1, NULL, NULL, 3, 0,
1321 repo, f);
1322 break;
1323 case GOT_OBJ_TYPE_TREE:
1324 error = got_diff_objects_as_trees(id2, id1, "", "", 3, 0, repo,
1325 f);
1326 break;
1327 case GOT_OBJ_TYPE_COMMIT:
1328 error = got_diff_objects_as_commits(id2, id1, 3, 0, repo, f);
1329 break;
1330 default:
1331 error = got_error(GOT_ERR_OBJ_TYPE);
1334 if ((buf = calloc(128, sizeof(char *))) == NULL)
1335 goto done;
1337 fseek(f, 0, SEEK_SET);
1339 while ((fgets(buf, 128, f)) != NULL) {
1340 buf_color = gw_colordiff_line(buf);
1341 error = buf_puts(&newsize, diffbuf, buf_color);
1342 if (error)
1343 return NULL;
1345 error = buf_puts(&newsize, diffbuf, div_end);
1346 if (error)
1347 return NULL;
1350 if (buf_len(diffbuf) > 0) {
1351 error = buf_putc(diffbuf, '\0');
1352 diff_html = strdup(buf_get(diffbuf));
1354 done:
1355 fclose(f);
1356 free(buf_color);
1357 free(buf);
1358 free(diffbuf);
1359 free(label1);
1360 free(label2);
1361 free(id1);
1362 free(id2);
1363 if (repo)
1364 got_repo_close(repo);
1366 if (error)
1367 return NULL;
1368 else
1369 return diff_html;
1372 static char *
1373 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1375 FILE *f;
1376 char *owner = NULL, *d_file = NULL;
1377 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1378 char *comp, *pos, *buf;
1379 unsigned int i;
1381 if (gw_trans->gw_conf->got_show_repo_owner == false)
1382 goto err;
1384 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1385 goto err;
1387 if ((f = fopen(d_file, "r")) == NULL)
1388 goto err;
1390 if ((buf = calloc(128, sizeof(char *))) == NULL)
1391 goto err;
1393 while ((fgets(buf, 128, f)) != NULL) {
1394 if ((pos = strstr(buf, gotweb)) != NULL)
1395 break;
1397 if ((pos = strstr(buf, gitweb)) != NULL)
1398 break;
1401 if (pos == NULL)
1402 goto err;
1404 do {
1405 fgets(buf, 128, f);
1406 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1408 if (comp == NULL)
1409 goto err;
1411 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1412 goto err;
1414 for (i = 0; i < 2; i++) {
1415 owner = strsep(&buf, "\"");
1418 if (owner == NULL)
1419 goto err;
1421 fclose(f);
1422 free(d_file);
1423 return owner;
1424 err:
1425 if ((asprintf(&owner, "%s", "")) == -1)
1426 return NULL;
1428 return owner;
1431 static char *
1432 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1434 FILE *f;
1435 char *url = NULL, *d_file = NULL;
1436 unsigned int len;
1438 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1439 return NULL;
1441 if ((f = fopen(d_file, "r")) == NULL)
1442 return NULL;
1444 fseek(f, 0, SEEK_END);
1445 len = ftell(f) + 1;
1446 fseek(f, 0, SEEK_SET);
1448 if ((url = calloc(len, sizeof(char *))) == NULL)
1449 return NULL;
1451 fread(url, 1, len, f);
1452 fclose(f);
1453 free(d_file);
1454 return url;
1457 static char *
1458 gw_get_repo_log(struct gw_trans *gw_trans, const char *search_pattern,
1459 char *start_commit, int limit, int log_type)
1461 const struct got_error *error;
1462 struct got_repository *repo = NULL;
1463 struct got_reflist_head refs;
1464 struct got_reflist_entry *re;
1465 struct got_commit_object *commit = NULL;
1466 struct got_object_id *id1 = NULL, *id2 = NULL;
1467 struct got_object_qid *parent_id;
1468 struct got_commit_graph *graph = NULL;
1469 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1470 *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1471 *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1472 *commit_commit_disp = NULL, *commit_age_diff = NULL,
1473 *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1474 *commit_age_long_disp = NULL, *commit_author = NULL,
1475 *commit_author_disp = NULL, *commit_committer = NULL,
1476 *commit_committer_disp = NULL, *commit_log = NULL,
1477 *commit_log_disp = NULL, *commit_parent = NULL,
1478 *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1479 *log_tree_html = NULL, *log_commit_html = NULL,
1480 *log_diff_html = NULL, *commit_tree = NULL,
1481 *commit_tree_disp = NULL, *log_tag_html = NULL,
1482 *log_blame_html = NULL;
1483 char *commit_log0, *newline;
1484 regex_t regex;
1485 int have_match, log_count = 0, has_parent = 1;
1486 size_t newsize;
1487 struct buf *diffbuf = NULL;
1488 time_t committer_time;
1490 if (gw_trans->action == GW_LOG || gw_trans->action == GW_LOGBRIEFS)
1491 log_count = gw_get_repo_log_count(gw_trans, start_commit);
1493 error = buf_alloc(&diffbuf, 0);
1494 if (error)
1495 return NULL;
1497 if (search_pattern &&
1498 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB |
1499 REG_NEWLINE))
1500 return NULL;
1502 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1503 if (error)
1504 return NULL;
1506 SIMPLEQ_INIT(&refs);
1508 if (start_commit == NULL) {
1509 struct got_reference *head_ref;
1510 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1511 if (error)
1512 goto done;
1514 error = got_ref_resolve(&id1, repo, head_ref);
1515 got_ref_close(head_ref);
1516 if (error)
1517 goto done;
1519 error = got_object_open_as_commit(&commit, repo, id1);
1520 } else {
1521 struct got_reference *ref;
1522 error = got_ref_open(&ref, repo, start_commit, 0);
1523 if (error == NULL) {
1524 int obj_type;
1525 error = got_ref_resolve(&id1, repo, ref);
1526 got_ref_close(ref);
1527 if (error)
1528 goto done;
1529 error = got_object_get_type(&obj_type, repo, id1);
1530 if (error)
1531 goto done;
1532 if (obj_type == GOT_OBJ_TYPE_TAG) {
1533 struct got_tag_object *tag;
1534 error = got_object_open_as_tag(&tag, repo, id1);
1535 if (error)
1536 goto done;
1537 if (got_object_tag_get_object_type(tag) !=
1538 GOT_OBJ_TYPE_COMMIT) {
1539 got_object_tag_close(tag);
1540 error = got_error(GOT_ERR_OBJ_TYPE);
1541 goto done;
1543 free(id1);
1544 id1 = got_object_id_dup(
1545 got_object_tag_get_object_id(tag));
1546 if (id1 == NULL)
1547 error = got_error_from_errno(
1548 "got_object_id_dup");
1549 got_object_tag_close(tag);
1550 if (error)
1551 goto done;
1552 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1553 error = got_error(GOT_ERR_OBJ_TYPE);
1554 goto done;
1556 error = got_object_open_as_commit(&commit, repo, id1);
1557 if (error)
1558 goto done;
1560 if (commit == NULL) {
1561 error = got_repo_match_object_id_prefix(&id1,
1562 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1563 if (error)
1564 goto done;
1566 error = got_repo_match_object_id_prefix(&id1,
1567 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1570 if (error)
1571 goto done;
1573 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1574 if (error)
1575 goto done;
1577 if (in_repo_path) {
1578 free(path);
1579 path = in_repo_path;
1582 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1583 if (error)
1584 goto done;
1586 error = got_commit_graph_open(&graph, path, 0);
1587 if (error)
1588 goto done;
1590 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1591 if (error)
1592 goto done;
1594 for (;;) {
1595 error = got_commit_graph_iter_next(&id1, graph, repo, NULL,
1596 NULL);
1597 if (error) {
1598 if (error->code == GOT_ERR_ITER_COMPLETED)
1599 error = NULL;
1600 break;
1602 if (id1 == NULL)
1603 break;
1605 error = got_object_open_as_commit(&commit, repo, id1);
1606 if (error)
1607 break;
1609 if (search_pattern) {
1610 error = gw_match_logmsg(&have_match, id1, commit,
1611 &regex);
1612 if (error) {
1613 got_object_commit_close(commit);
1614 break;
1616 if (have_match == 0) {
1617 got_object_commit_close(commit);
1618 continue;
1622 SIMPLEQ_FOREACH(re, &refs, entry) {
1623 char *s;
1624 const char *name;
1625 struct got_tag_object *tag = NULL;
1626 int cmp;
1628 name = got_ref_get_name(re->ref);
1629 if (strcmp(name, GOT_REF_HEAD) == 0)
1630 continue;
1631 if (strncmp(name, "refs/", 5) == 0)
1632 name += 5;
1633 if (strncmp(name, "got/", 4) == 0)
1634 continue;
1635 if (strncmp(name, "heads/", 6) == 0)
1636 name += 6;
1637 if (strncmp(name, "remotes/", 8) == 0)
1638 name += 8;
1639 if (strncmp(name, "tags/", 5) == 0) {
1640 error = got_object_open_as_tag(&tag, repo,
1641 re->id);
1642 if (error) {
1643 if (error->code != GOT_ERR_OBJ_TYPE)
1644 continue;
1646 * Ref points at something other
1647 * than a tag.
1649 error = NULL;
1650 tag = NULL;
1653 cmp = got_object_id_cmp(tag ?
1654 got_object_tag_get_object_id(tag) : re->id, id1);
1655 if (tag)
1656 got_object_tag_close(tag);
1657 if (cmp != 0)
1658 continue;
1659 s = refs_str;
1660 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1661 s ? ", " : "", name)) == -1) {
1662 error = got_error_from_errno("asprintf");
1663 free(s);
1664 goto done;
1666 free(s);
1669 if (refs_str == NULL)
1670 refs_str_disp = strdup("");
1671 else {
1672 if ((asprintf(&refs_str_disp, "(%s)",
1673 refs_str)) == -1) {
1674 error = got_error_from_errno("asprintf");
1675 free(refs_str);
1676 goto done;
1680 error = got_object_id_str(&id_str1, id1);
1681 if (error)
1682 goto done;
1684 error = got_object_id_str(&treeid,
1685 got_object_commit_get_tree_id(commit));
1686 if (error)
1687 goto done;
1689 if (gw_trans->action == GW_COMMIT ||
1690 gw_trans->action == GW_COMMITDIFF) {
1691 parent_id =
1692 SIMPLEQ_FIRST(
1693 got_object_commit_get_parent_ids(commit));
1694 if (parent_id != NULL) {
1695 id2 = got_object_id_dup(parent_id->id);
1696 free (parent_id);
1697 error = got_object_id_str(&id_str2, id2);
1698 if (error)
1699 goto done;
1700 free(id2);
1701 } else {
1702 has_parent = 0;
1703 id_str2 = strdup("/dev/null");
1707 committer_time =
1708 got_object_commit_get_committer_time(commit);
1710 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1711 error = got_error_from_errno("asprintf");
1712 goto done;
1715 if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1716 error = got_error_from_errno("asprintf");
1717 goto done;
1720 if ((asprintf(&commit_tree_disp, commit_tree_html,
1721 treeid)) == -1) {
1722 error = got_error_from_errno("asprintf");
1723 goto done;
1726 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1727 id_str1)) == -1) {
1728 error = got_error_from_errno("asprintf");
1729 goto done;
1732 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1733 error = got_error_from_errno("asprintf");
1734 goto done;
1737 if ((asprintf(&commit_commit_disp, commit_commit_html,
1738 commit_commit, refs_str_disp)) == -1) {
1739 error = got_error_from_errno("asprintf");
1740 goto done;
1743 if ((asprintf(&commit_age_long, "%s",
1744 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1745 error = got_error_from_errno("asprintf");
1746 goto done;
1749 if ((asprintf(&commit_age_long_disp, commit_age_html,
1750 commit_age_long)) == -1) {
1751 error = got_error_from_errno("asprintf");
1752 goto done;
1755 if ((asprintf(&commit_age_diff, "%s",
1756 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1757 error = got_error_from_errno("asprintf");
1758 goto done;
1761 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1762 commit_age_diff)) == -1) {
1763 error = got_error_from_errno("asprintf");
1764 goto done;
1767 if ((asprintf(&commit_author, "%s",
1768 got_object_commit_get_author(commit))) == -1) {
1769 error = got_error_from_errno("asprintf");
1770 goto done;
1773 if ((asprintf(&commit_author_disp, commit_author_html,
1774 gw_html_escape(commit_author))) == -1) {
1775 error = got_error_from_errno("asprintf");
1776 goto done;
1779 if ((asprintf(&commit_committer, "%s",
1780 got_object_commit_get_committer(commit))) == -1) {
1781 error = got_error_from_errno("asprintf");
1782 goto done;
1785 if ((asprintf(&commit_committer_disp, commit_committer_html,
1786 gw_html_escape(commit_committer))) == -1) {
1787 error = got_error_from_errno("asprintf");
1788 goto done;
1791 if (strcmp(commit_author, commit_committer) == 0) {
1792 free(commit_committer_disp);
1793 commit_committer_disp = strdup("");
1796 error = got_object_commit_get_logmsg(&commit_log0, commit);
1797 if (error)
1798 goto done;
1800 commit_log = commit_log0;
1801 while (*commit_log == '\n')
1802 commit_log++;
1804 switch(log_type) {
1805 case (LOGBRIEF):
1806 newline = strchr(commit_log, '\n');
1807 if (newline)
1808 *newline = '\0';
1810 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1811 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1812 id_str1, gw_trans->repo_name, id_str1,
1813 gw_trans->repo_name, id_str1)) == -1) {
1814 error = got_error_from_errno("asprintf");
1815 goto done;
1818 if ((asprintf(&commit_row, logbriefs_row,
1819 commit_age_diff, commit_author, commit_log,
1820 logbriefs_navs_html)) == -1) {
1821 error = got_error_from_errno("asprintf");
1822 goto done;
1825 free(logbriefs_navs_html);
1826 logbriefs_navs_html = NULL;
1827 break;
1828 case (LOGFULL):
1829 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1830 gw_trans->repo_name, id_str1, gw_trans->repo_name,
1831 id_str1, gw_trans->repo_name, id_str1,
1832 gw_trans->repo_name, id_str1)) == -1) {
1833 error = got_error_from_errno("asprintf");
1834 goto done;
1837 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1838 commit_author_disp, commit_committer_disp,
1839 commit_age_long_disp, gw_html_escape(commit_log),
1840 logbriefs_navs_html)) == -1) {
1841 error = got_error_from_errno("asprintf");
1842 goto done;
1845 free(logbriefs_navs_html);
1846 logbriefs_navs_html = NULL;
1847 break;
1848 case (LOGTAG):
1849 log_tag_html = strdup("tag log here");
1851 if ((asprintf(&commit_row, log_tag_row,
1852 gw_html_escape(commit_log), log_tag_html)) == -1) {
1853 error = got_error_from_errno("asprintf");
1854 goto done;
1857 free(log_tag_html);
1858 break;
1859 case (LOGBLAME):
1860 log_blame_html = gw_get_file_blame(gw_trans,
1861 start_commit);
1863 if ((asprintf(&commit_row, log_blame_row,
1864 gw_html_escape(commit_log), log_blame_html)) == -1) {
1865 error = got_error_from_errno("asprintf");
1866 goto done;
1869 free(log_blame_html);
1870 break;
1871 case (LOGTREE):
1872 log_tree_html = gw_get_repo_tree(gw_trans,
1873 start_commit);
1875 if ((asprintf(&commit_row, log_tree_row,
1876 gw_html_escape(commit_log), log_tree_html)) == -1) {
1877 error = got_error_from_errno("asprintf");
1878 goto done;
1881 free(log_tree_html);
1882 break;
1883 case (LOGCOMMIT):
1884 if ((asprintf(&commit_log_disp, commit_log_html,
1885 gw_html_escape(commit_log))) == -1) {
1886 error = got_error_from_errno("asprintf");
1887 goto done;
1890 log_commit_html = strdup("commit here");
1892 if ((asprintf(&commit_row, log_commit_row,
1893 commit_diff_disp, commit_commit_disp,
1894 commit_tree_disp, commit_author_disp,
1895 commit_committer_disp, commit_age_long_disp,
1896 commit_log_disp, log_commit_html)) == -1) {
1897 error = got_error_from_errno("asprintf");
1898 goto done;
1900 free(commit_log_disp);
1901 free(log_commit_html);
1903 break;
1904 case (LOGDIFF):
1905 if ((asprintf(&commit_log_disp, commit_log_html,
1906 gw_html_escape(commit_log))) == -1) {
1907 error = got_error_from_errno("asprintf");
1908 goto done;
1911 if (has_parent)
1912 log_diff_html = gw_get_repo_diff(gw_trans,
1913 commit_commit, commit_parent);
1914 else
1915 log_diff_html = gw_get_repo_diff(gw_trans,
1916 commit_commit, NULL);
1918 if ((asprintf(&commit_row, log_diff_row,
1919 commit_diff_disp, commit_commit_disp,
1920 commit_tree_disp, commit_author_disp,
1921 commit_committer_disp, commit_age_long_disp,
1922 commit_log_disp, log_diff_html)) == -1) {
1923 error = got_error_from_errno("asprintf");
1924 goto done;
1926 free(commit_log_disp);
1927 free(log_diff_html);
1929 break;
1930 default:
1931 return NULL;
1934 error = buf_puts(&newsize, diffbuf, commit_row);
1936 free(commit_parent);
1937 free(commit_diff_disp);
1938 free(commit_tree_disp);
1939 free(commit_age_diff);
1940 free(commit_age_diff_disp);
1941 free(commit_age_long);
1942 free(commit_age_long_disp);
1943 free(commit_author);
1944 free(commit_author_disp);
1945 free(commit_committer);
1946 free(commit_committer_disp);
1947 free(commit_log0);
1948 free(commit_row);
1949 free(refs_str_disp);
1950 free(refs_str);
1951 refs_str = NULL;
1952 free(id_str1);
1953 id_str1 = NULL;
1954 free(id_str2);
1955 id_str2 = NULL;
1957 if (error || (limit && --limit == 0))
1958 break;
1961 if (error)
1962 goto done;
1964 if (buf_len(diffbuf) > 0) {
1965 error = buf_putc(diffbuf, '\0');
1966 logs = strdup(buf_get(diffbuf));
1968 done:
1969 buf_free(diffbuf);
1970 free(in_repo_path);
1971 if (commit != NULL)
1972 got_object_commit_close(commit);
1973 if (search_pattern)
1974 regfree(&regex);
1975 if (graph)
1976 got_commit_graph_close(graph);
1977 if (repo) {
1978 error = got_repo_close(repo);
1979 if (error)
1980 return NULL;
1982 if (error) {
1983 khttp_puts(gw_trans->gw_req, "Error: ");
1984 khttp_puts(gw_trans->gw_req, error->msg);
1985 return NULL;
1986 } else
1987 return logs;
1990 static char *
1991 gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
1993 const struct got_error *error = NULL;
1994 struct got_repository *repo = NULL;
1995 struct got_reflist_head refs;
1996 struct got_reflist_entry *re;
1997 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1998 *age = NULL;
1999 char *newline;
2000 struct buf *diffbuf = NULL;
2001 size_t newsize;
2003 error = buf_alloc(&diffbuf, 0);
2004 if (error)
2005 return NULL;
2006 SIMPLEQ_INIT(&refs);
2008 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2009 if (error)
2010 goto done;
2012 error = got_ref_list(&refs, repo, "refs/tags", got_repo_cmp_tags, repo);
2013 if (error)
2014 goto done;
2016 SIMPLEQ_FOREACH(re, &refs, entry) {
2017 const char *refname;
2018 char *refstr, *tag_log0, *tag_log, *id_str;
2019 time_t tagger_time;
2020 struct got_object_id *id;
2021 struct got_tag_object *tag;
2023 refname = got_ref_get_name(re->ref);
2024 if (strncmp(refname, "refs/tags/", 10) != 0)
2025 continue;
2026 refname += 10;
2027 refstr = got_ref_to_str(re->ref);
2028 if (refstr == NULL) {
2029 error = got_error_from_errno("got_ref_to_str");
2030 goto done;
2033 error = got_ref_resolve(&id, repo, re->ref);
2034 if (error)
2035 goto done;
2036 error = got_object_open_as_tag(&tag, repo, id);
2037 free(id);
2038 if (error)
2039 goto done;
2041 tagger_time = got_object_tag_get_tagger_time(tag);
2043 error = got_object_id_str(&id_str,
2044 got_object_tag_get_object_id(tag));
2045 if (error)
2046 goto done;
2048 tag_log0 = strdup(got_object_tag_get_message(tag));
2050 if (tag_log0 == NULL) {
2051 error = got_error_from_errno("strdup");
2052 goto done;
2055 tag_log = tag_log0;
2056 while (*tag_log == '\n')
2057 tag_log++;
2059 switch (tag_type) {
2060 case TAGBRIEF:
2061 newline = strchr(tag_log, '\n');
2062 if (newline)
2063 *newline = '\0';
2065 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
2066 TM_DIFF))) == -1) {
2067 error = got_error_from_errno("asprintf");
2068 goto done;
2071 if ((asprintf(&tags_navs_disp, tags_navs,
2072 gw_trans->repo_name, id_str, gw_trans->repo_name,
2073 id_str, gw_trans->repo_name, id_str,
2074 gw_trans->repo_name, id_str)) == -1) {
2075 error = got_error_from_errno("asprintf");
2076 goto done;
2079 if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
2080 tags_navs_disp)) == -1) {
2081 error = got_error_from_errno("asprintf");
2082 goto done;
2085 free(tags_navs_disp);
2086 break;
2087 case TAGFULL:
2088 break;
2089 default:
2090 break;
2093 got_object_tag_close(tag);
2095 error = buf_puts(&newsize, diffbuf, tag_row);
2097 free(id_str);
2098 free(refstr);
2099 free(age);
2100 free(tag_log0);
2101 free(tag_row);
2103 if (error || (limit && --limit == 0))
2104 break;
2107 if (buf_len(diffbuf) > 0) {
2108 error = buf_putc(diffbuf, '\0');
2109 tags = strdup(buf_get(diffbuf));
2111 done:
2112 buf_free(diffbuf);
2113 got_ref_list_free(&refs);
2114 if (repo)
2115 got_repo_close(repo);
2116 if (error)
2117 return NULL;
2118 else
2119 return tags;
2122 struct blame_line {
2123 int annotated;
2124 char *id_str;
2125 char *committer;
2126 char datebuf[11]; /* YYYY-MM-DD + NUL */
2129 struct gw_blame_cb_args {
2130 struct blame_line *lines;
2131 int nlines;
2132 int nlines_prec;
2133 int lineno_cur;
2134 off_t *line_offsets;
2135 FILE *f;
2136 struct got_repository *repo;
2137 struct gw_trans *gw_trans;
2138 struct buf *blamebuf;
2141 static const struct got_error *
2142 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2144 const struct got_error *err = NULL;
2145 struct gw_blame_cb_args *a = arg;
2146 struct blame_line *bline;
2147 char *line = NULL;
2148 size_t linesize = 0, newsize;
2149 struct got_commit_object *commit = NULL;
2150 off_t offset;
2151 struct tm tm;
2152 time_t committer_time;
2154 if (nlines != a->nlines ||
2155 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2156 return got_error(GOT_ERR_RANGE);
2158 if (lineno == -1)
2159 return NULL; /* no change in this commit */
2161 /* Annotate this line. */
2162 bline = &a->lines[lineno - 1];
2163 if (bline->annotated)
2164 return NULL;
2165 err = got_object_id_str(&bline->id_str, id);
2166 if (err)
2167 return err;
2169 err = got_object_open_as_commit(&commit, a->repo, id);
2170 if (err)
2171 goto done;
2173 bline->committer = strdup(got_object_commit_get_committer(commit));
2174 if (bline->committer == NULL) {
2175 err = got_error_from_errno("strdup");
2176 goto done;
2179 committer_time = got_object_commit_get_committer_time(commit);
2180 if (localtime_r(&committer_time, &tm) == NULL)
2181 return got_error_from_errno("localtime_r");
2182 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2183 &tm) >= sizeof(bline->datebuf)) {
2184 err = got_error(GOT_ERR_NO_SPACE);
2185 goto done;
2187 bline->annotated = 1;
2189 /* Print lines annotated so far. */
2190 bline = &a->lines[a->lineno_cur - 1];
2191 if (!bline->annotated)
2192 goto done;
2194 offset = a->line_offsets[a->lineno_cur - 1];
2195 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2196 err = got_error_from_errno("fseeko");
2197 goto done;
2200 while (bline->annotated) {
2201 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2202 *line_escape = NULL;
2203 size_t len;
2205 if (getline(&line, &linesize, a->f) == -1) {
2206 if (ferror(a->f))
2207 err = got_error_from_errno("getline");
2208 break;
2211 committer = bline->committer;
2212 smallerthan = strchr(committer, '<');
2213 if (smallerthan && smallerthan[1] != '\0')
2214 committer = smallerthan + 1;
2215 at = strchr(committer, '@');
2216 if (at)
2217 *at = '\0';
2218 len = strlen(committer);
2219 if (len >= 9)
2220 committer[8] = '\0';
2222 nl = strchr(line, '\n');
2223 if (nl)
2224 *nl = '\0';
2226 if (strcmp(line, "") != 0)
2227 line_escape = strdup(gw_html_escape(line));
2228 else
2229 line_escape = strdup("");
2231 asprintf(&blame_row, log_blame_line, a->nlines_prec,
2232 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2233 line_escape);
2234 a->lineno_cur++;
2235 err = buf_puts(&newsize, a->blamebuf, blame_row);
2236 if (err)
2237 return err;
2239 bline = &a->lines[a->lineno_cur - 1];
2240 free(line_escape);
2241 free(blame_row);
2243 done:
2244 if (commit)
2245 got_object_commit_close(commit);
2246 free(line);
2247 return err;
2250 static char*
2251 gw_get_file_blame(struct gw_trans *gw_trans, char *commit_str)
2253 const struct got_error *error = NULL;
2254 struct got_repository *repo = NULL;
2255 struct got_object_id *obj_id = NULL;
2256 struct got_object_id *commit_id = NULL;
2257 struct got_blob_object *blob = NULL;
2258 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2259 *folder = NULL;
2260 struct gw_blame_cb_args bca;
2261 int i, obj_type;
2262 size_t filesize;
2264 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2265 if (error)
2266 goto done;
2268 if (gw_trans->repo_folder != NULL) {
2269 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2270 error = got_error_from_errno("asprintf");
2271 goto done;
2273 } else
2274 folder = strdup("");
2276 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2277 error = got_error_from_errno("asprintf");
2278 goto done;
2280 free(folder);
2282 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2283 if (error)
2284 goto done;
2286 error = got_repo_match_object_id(&commit_id, NULL, commit_str,
2287 GOT_OBJ_TYPE_COMMIT, 1, repo);
2288 if (error)
2289 goto done;
2291 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2292 if (error)
2293 goto done;
2295 if (obj_id == NULL) {
2296 error = got_error(GOT_ERR_NO_OBJ);
2297 goto done;
2300 error = got_object_get_type(&obj_type, repo, obj_id);
2301 if (error)
2302 goto done;
2304 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2305 error = got_error(GOT_ERR_OBJ_TYPE);
2306 goto done;
2309 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2310 if (error)
2311 goto done;
2313 error = buf_alloc(&bca.blamebuf, 0);
2314 if (error)
2315 goto done;
2317 bca.f = got_opentemp();
2318 if (bca.f == NULL) {
2319 error = got_error_from_errno("got_opentemp");
2320 goto done;
2322 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2323 &bca.line_offsets, bca.f, blob);
2324 if (error || bca.nlines == 0)
2325 goto done;
2327 /* Don't include \n at EOF in the blame line count. */
2328 if (bca.line_offsets[bca.nlines - 1] == filesize)
2329 bca.nlines--;
2331 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2332 if (bca.lines == NULL) {
2333 error = got_error_from_errno("calloc");
2334 goto done;
2336 bca.lineno_cur = 1;
2337 bca.nlines_prec = 0;
2338 i = bca.nlines;
2339 while (i > 0) {
2340 i /= 10;
2341 bca.nlines_prec++;
2343 bca.repo = repo;
2344 bca.gw_trans = gw_trans;
2346 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2347 NULL, NULL);
2348 if (buf_len(bca.blamebuf) > 0) {
2349 error = buf_putc(bca.blamebuf, '\0');
2350 blame_html = strdup(buf_get(bca.blamebuf));
2352 done:
2353 free(bca.blamebuf);
2354 free(in_repo_path);
2355 free(commit_id);
2356 free(obj_id);
2357 free(path);
2359 if (blob)
2360 error = got_object_blob_close(blob);
2361 if (repo)
2362 error = got_repo_close(repo);
2363 if (error)
2364 return NULL;
2365 if (bca.lines) {
2366 for (i = 0; i < bca.nlines; i++) {
2367 struct blame_line *bline = &bca.lines[i];
2368 free(bline->id_str);
2369 free(bline->committer);
2371 free(bca.lines);
2373 free(bca.line_offsets);
2374 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2375 error = got_error_from_errno("fclose");
2376 if (error)
2377 return NULL;
2378 else
2379 return blame_html;
2382 static char*
2383 gw_get_repo_tree(struct gw_trans *gw_trans, char *commit_str)
2385 const struct got_error *error = NULL;
2386 struct got_repository *repo = NULL;
2387 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2388 struct got_tree_object *tree = NULL;
2389 struct buf *diffbuf = NULL;
2390 size_t newsize;
2391 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2392 *tree_row = NULL, *id_str;
2393 int nentries, i;
2395 error = buf_alloc(&diffbuf, 0);
2396 if (error)
2397 return NULL;
2399 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2400 if (error)
2401 goto done;
2403 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2404 if (error)
2405 goto done;
2407 if (gw_trans->repo_folder != NULL)
2408 path = strdup(gw_trans->repo_folder);
2409 else if (in_repo_path) {
2410 free(path);
2411 path = in_repo_path;
2414 if (commit_str == NULL) {
2415 struct got_reference *head_ref;
2416 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2417 if (error)
2418 goto done;
2420 error = got_ref_resolve(&commit_id, repo, head_ref);
2421 got_ref_close(head_ref);
2423 } else
2424 error = got_repo_match_object_id(&commit_id, NULL, commit_str,
2425 GOT_OBJ_TYPE_COMMIT, 1, repo);
2426 if (error)
2427 goto done;
2429 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2430 if (error)
2431 goto done;
2433 error = got_object_open_as_tree(&tree, repo, tree_id);
2434 if (error)
2435 goto done;
2437 nentries = got_object_tree_get_nentries(tree);
2439 for (i = 0; i < nentries; i++) {
2440 struct got_tree_entry *te;
2441 const char *modestr = "";
2442 char *id = NULL, *url_html = NULL;
2444 te = got_object_tree_get_entry(tree, i);
2446 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2447 if (error)
2448 goto done;
2450 if ((asprintf(&id, "%s", id_str)) == -1) {
2451 error = got_error_from_errno("asprintf");
2452 free(id_str);
2453 goto done;
2456 mode_t mode = got_tree_entry_get_mode(te);
2458 if (got_object_tree_entry_is_submodule(te))
2459 modestr = "$";
2460 else if (S_ISLNK(mode))
2461 modestr = "@";
2462 else if (S_ISDIR(mode))
2463 modestr = "/";
2464 else if (mode & S_IXUSR)
2465 modestr = "*";
2467 char *build_folder = NULL;
2468 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2469 if (gw_trans->repo_folder != NULL) {
2470 if ((asprintf(&build_folder, "%s/%s",
2471 gw_trans->repo_folder,
2472 got_tree_entry_get_name(te))) == -1) {
2473 error =
2474 got_error_from_errno("asprintf");
2475 goto done;
2477 } else {
2478 if (asprintf(&build_folder, "%s",
2479 got_tree_entry_get_name(te)) == -1)
2480 goto done;
2483 if ((asprintf(&url_html, folder_html,
2484 gw_trans->repo_name, gw_trans->action_name,
2485 gw_trans->commit, build_folder,
2486 got_tree_entry_get_name(te), modestr)) == -1) {
2487 error = got_error_from_errno("asprintf");
2488 goto done;
2490 } else {
2491 if (gw_trans->repo_folder != NULL) {
2492 if ((asprintf(&build_folder, "%s",
2493 gw_trans->repo_folder)) == -1) {
2494 error =
2495 got_error_from_errno("asprintf");
2496 goto done;
2498 } else
2499 build_folder = strdup("");
2501 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2502 "blame", gw_trans->commit,
2503 got_tree_entry_get_name(te), build_folder,
2504 got_tree_entry_get_name(te), modestr)) == -1) {
2505 error = got_error_from_errno("asprintf");
2506 goto done;
2509 free(build_folder);
2511 if (error)
2512 goto done;
2514 if ((asprintf(&tree_row, trees_row, "", url_html)) == -1) {
2515 error = got_error_from_errno("asprintf");
2516 goto done;
2518 error = buf_puts(&newsize, diffbuf, tree_row);
2519 if (error)
2520 goto done;
2522 free(id);
2523 free(id_str);
2524 free(url_html);
2525 free(tree_row);
2528 if (buf_len(diffbuf) > 0) {
2529 error = buf_putc(diffbuf, '\0');
2530 tree_html = strdup(buf_get(diffbuf));
2532 done:
2533 if (tree)
2534 got_object_tree_close(tree);
2535 if (repo)
2536 got_repo_close(repo);
2538 free(in_repo_path);
2539 free(tree_id);
2540 free(diffbuf);
2541 if (error)
2542 return NULL;
2543 else
2544 return tree_html;
2547 static char *
2548 gw_get_repo_heads(struct gw_trans *gw_trans)
2550 const struct got_error *error = NULL;
2551 struct got_repository *repo = NULL;
2552 struct got_reflist_head refs;
2553 struct got_reflist_entry *re;
2554 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2555 struct buf *diffbuf = NULL;
2556 size_t newsize;
2558 error = buf_alloc(&diffbuf, 0);
2559 if (error)
2560 return NULL;
2562 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2563 if (error)
2564 goto done;
2566 SIMPLEQ_INIT(&refs);
2567 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2568 NULL);
2569 if (error)
2570 goto done;
2572 SIMPLEQ_FOREACH(re, &refs, entry) {
2573 char *refname;
2575 refname = strdup(got_ref_get_name(re->ref));
2576 if (refname == NULL) {
2577 error = got_error_from_errno("got_ref_to_str");
2578 goto done;
2581 if (strncmp(refname, "refs/heads/", 11) != 0) {
2582 free(refname);
2583 continue;
2586 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2587 TM_DIFF);
2589 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2590 refname, gw_trans->repo_name, refname,
2591 gw_trans->repo_name, refname, gw_trans->repo_name,
2592 refname)) == -1) {
2593 error = got_error_from_errno("asprintf");
2594 goto done;
2597 if (strncmp(refname, "refs/heads/", 11) == 0)
2598 refname += 11;
2600 if ((asprintf(&head_row, heads_row, age, refname,
2601 head_navs_disp)) == -1) {
2602 error = got_error_from_errno("asprintf");
2603 goto done;
2606 error = buf_puts(&newsize, diffbuf, head_row);
2608 free(head_navs_disp);
2609 free(head_row);
2612 if (buf_len(diffbuf) > 0) {
2613 error = buf_putc(diffbuf, '\0');
2614 heads = strdup(buf_get(diffbuf));
2616 done:
2617 buf_free(diffbuf);
2618 got_ref_list_free(&refs);
2619 if (repo)
2620 got_repo_close(repo);
2621 if (error)
2622 return NULL;
2623 else
2624 return heads;
2627 static char *
2628 gw_get_got_link(struct gw_trans *gw_trans)
2630 char *link;
2632 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2633 gw_trans->gw_conf->got_logo)) == -1)
2634 return NULL;
2636 return link;
2639 static char *
2640 gw_get_site_link(struct gw_trans *gw_trans)
2642 char *link, *repo = "", *action = "";
2644 if (gw_trans->repo_name != NULL)
2645 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2646 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2647 return NULL;
2649 if (gw_trans->action_name != NULL)
2650 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2651 return NULL;
2653 if ((asprintf(&link, site_link, GOTWEB,
2654 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2655 return NULL;
2657 return link;
2660 static char *
2661 gw_colordiff_line(char *buf)
2663 const struct got_error *error = NULL;
2664 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2665 struct buf *diffbuf = NULL;
2666 size_t newsize;
2668 error = buf_alloc(&diffbuf, 0);
2669 if (error)
2670 return NULL;
2672 if (strncmp(buf, "-", 1) == 0)
2673 color = "diff_minus";
2674 if (strncmp(buf, "+", 1) == 0)
2675 color = "diff_plus";
2676 if (strncmp(buf, "@@", 2) == 0)
2677 color = "diff_chunk_header";
2678 if (strncmp(buf, "@@", 2) == 0)
2679 color = "diff_chunk_header";
2680 if (strncmp(buf, "commit +", 8) == 0)
2681 color = "diff_meta";
2682 if (strncmp(buf, "commit -", 8) == 0)
2683 color = "diff_meta";
2684 if (strncmp(buf, "blob +", 6) == 0)
2685 color = "diff_meta";
2686 if (strncmp(buf, "blob -", 6) == 0)
2687 color = "diff_meta";
2688 if (strncmp(buf, "file +", 6) == 0)
2689 color = "diff_meta";
2690 if (strncmp(buf, "file -", 6) == 0)
2691 color = "diff_meta";
2692 if (strncmp(buf, "from:", 5) == 0)
2693 color = "diff_author";
2694 if (strncmp(buf, "via:", 4) == 0)
2695 color = "diff_author";
2696 if (strncmp(buf, "date:", 5) == 0)
2697 color = "diff_date";
2699 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2700 return NULL;
2702 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2703 if (error)
2704 return NULL;
2706 error = buf_puts(&newsize, diffbuf, buf);
2707 if (error)
2708 return NULL;
2710 if (buf_len(diffbuf) > 0) {
2711 error = buf_putc(diffbuf, '\0');
2712 colorized_line = strdup(buf_get(diffbuf));
2715 free(diffbuf);
2716 free(div_diff_line_div);
2717 return colorized_line;
2720 static char *
2721 gw_html_escape(const char *html)
2723 char *escaped_str = NULL, *buf;
2724 char c[1];
2725 size_t sz, i, buff_sz = 2048;
2727 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2728 return NULL;
2730 if (html == NULL)
2731 return NULL;
2732 else
2733 if ((sz = strlen(html)) == 0)
2734 return NULL;
2736 /* only work with buff_sz */
2737 if (buff_sz < sz)
2738 sz = buff_sz;
2740 for (i = 0; i < sz; i++) {
2741 c[0] = html[i];
2742 switch (c[0]) {
2743 case ('>'):
2744 strcat(buf, "&gt;");
2745 break;
2746 case ('&'):
2747 strcat(buf, "&amp;");
2748 break;
2749 case ('<'):
2750 strcat(buf, "&lt;");
2751 break;
2752 case ('"'):
2753 strcat(buf, "&quot;");
2754 break;
2755 case ('\''):
2756 strcat(buf, "&apos;");
2757 break;
2758 case ('\n'):
2759 strcat(buf, "<br />");
2760 default:
2761 strcat(buf, &c[0]);
2762 break;
2765 asprintf(&escaped_str, "%s", buf);
2766 free(buf);
2767 return escaped_str;
2770 int
2771 main()
2773 const struct got_error *error = NULL;
2774 struct gw_trans *gw_trans;
2775 struct gw_dir *dir = NULL, *tdir;
2776 const char *page = "index";
2777 int gw_malloc = 1;
2779 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2780 errx(1, "malloc");
2782 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2783 errx(1, "malloc");
2785 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2786 errx(1, "malloc");
2788 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2789 errx(1, "malloc");
2791 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX,
2792 &page, 1, 0))
2793 errx(1, "khttp_parse");
2795 if ((gw_trans->gw_conf =
2796 malloc(sizeof(struct gotweb_conf))) == NULL) {
2797 gw_malloc = 0;
2798 error = got_error_from_errno("malloc");
2799 goto err;
2802 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
2803 NULL) == -1) {
2804 error = got_error_from_errno("pledge");
2805 goto err;
2808 TAILQ_INIT(&gw_trans->gw_dirs);
2810 gw_trans->page = 0;
2811 gw_trans->repos_total = 0;
2812 gw_trans->repo_path = NULL;
2813 gw_trans->commit = NULL;
2814 gw_trans->headref = strdup(GOT_REF_HEAD);
2815 gw_trans->mime = KMIME_TEXT_HTML;
2816 gw_trans->gw_tmpl->key = gw_templs;
2817 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2818 gw_trans->gw_tmpl->arg = gw_trans;
2819 gw_trans->gw_tmpl->cb = gw_template;
2820 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2822 err:
2823 if (error) {
2824 gw_trans->mime = KMIME_TEXT_PLAIN;
2825 gw_trans->action = GW_ERR;
2826 gw_display_index(gw_trans, error);
2827 goto done;
2830 error = gw_parse_querystring(gw_trans);
2831 if (error)
2832 goto err;
2834 gw_display_index(gw_trans, error);
2836 done:
2837 if (gw_malloc) {
2838 free(gw_trans->gw_conf->got_repos_path);
2839 free(gw_trans->gw_conf->got_www_path);
2840 free(gw_trans->gw_conf->got_site_name);
2841 free(gw_trans->gw_conf->got_site_owner);
2842 free(gw_trans->gw_conf->got_site_link);
2843 free(gw_trans->gw_conf->got_logo);
2844 free(gw_trans->gw_conf->got_logo_url);
2845 free(gw_trans->gw_conf);
2846 free(gw_trans->commit);
2847 free(gw_trans->repo_path);
2848 free(gw_trans->repo_name);
2849 free(gw_trans->repo_file);
2850 free(gw_trans->action_name);
2851 free(gw_trans->headref);
2853 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2854 free(dir->name);
2855 free(dir->description);
2856 free(dir->age);
2857 free(dir->url);
2858 free(dir->path);
2859 free(dir);
2864 khttp_free(gw_trans->gw_req);
2865 return EXIT_SUCCESS;