Blob


1 /*
2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
22 #include <dirent.h>
23 #include <err.h>
24 #include <regex.h>
25 #include <stdarg.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include <got_object.h>
34 #include <got_reference.h>
35 #include <got_repository.h>
36 #include <got_path.h>
37 #include <got_cancel.h>
38 #include <got_worktree.h>
39 #include <got_diff.h>
40 #include <got_commit_graph.h>
41 #include <got_blame.h>
42 #include <got_privsep.h>
43 #include <got_opentemp.h>
45 #include <kcgi.h>
46 #include <kcgihtml.h>
48 #include "buf.h"
49 #include "gotweb.h"
50 #include "gotweb_ui.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 struct trans {
57 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 struct gw_dir *gw_dir;
59 struct gotweb_conf *gw_conf;
60 struct ktemplate *gw_tmpl;
61 struct khtmlreq *gw_html_req;
62 struct kreq *gw_req;
63 char *repo_name;
64 char *repo_path;
65 char *commit;
66 char *repo_file;
67 char *repo_folder;
68 char *action_name;
69 char *headref;
70 unsigned int action;
71 unsigned int page;
72 unsigned int repos_total;
73 enum kmime mime;
74 };
76 enum gw_key {
77 KEY_ACTION,
78 KEY_COMMIT_ID,
79 KEY_FILE,
80 KEY_FOLDER,
81 KEY_HEADREF,
82 KEY_PAGE,
83 KEY_PATH,
84 KEY__ZMAX
85 };
87 struct gw_dir {
88 TAILQ_ENTRY(gw_dir) entry;
89 char *name;
90 char *owner;
91 char *description;
92 char *url;
93 char *age;
94 char *path;
95 };
97 enum tmpl {
98 TEMPL_HEAD,
99 TEMPL_HEADER,
100 TEMPL_SITEPATH,
101 TEMPL_SITEOWNER,
102 TEMPL_TITLE,
103 TEMPL_SEARCH,
104 TEMPL_CONTENT,
105 TEMPL__MAX
106 };
108 enum ref_tm {
109 TM_DIFF,
110 TM_LONG,
111 };
113 enum logs {
114 LOGBRIEF,
115 LOGCOMMIT,
116 LOGFULL,
117 LOGTREE,
118 LOGDIFF,
119 LOGBLAME,
120 LOGTAG,
121 };
123 enum tags {
124 TAGBRIEF,
125 TAGFULL,
126 };
128 struct buf {
129 u_char *cb_buf;
130 size_t cb_size;
131 size_t cb_len;
132 };
134 static const char *const templs[TEMPL__MAX] = {
135 "head",
136 "header",
137 "sitepath",
138 "siteowner",
139 "title",
140 "search",
141 "content",
142 };
144 static const struct kvalid gw_keys[KEY__ZMAX] = {
145 { kvalid_stringne, "action" },
146 { kvalid_stringne, "commit" },
147 { kvalid_stringne, "file" },
148 { kvalid_stringne, "folder" },
149 { kvalid_stringne, "headref" },
150 { kvalid_int, "page" },
151 { kvalid_stringne, "path" },
152 };
154 int gw_get_repo_log_count(struct trans *, char *);
156 static struct gw_dir *gw_init_gw_dir(char *);
158 static char *gw_get_repo_description(struct trans *,
159 char *);
160 static char *gw_get_repo_owner(struct trans *,
161 char *);
162 static char *gw_get_time_str(time_t, int);
163 static char *gw_get_repo_age(struct trans *,
164 char *, char *, int);
165 static char *gw_get_repo_log(struct trans *, const char *,
166 char *, int, int);
167 static char *gw_get_file_blame(struct trans *, char *);
168 static char *gw_get_repo_tree(struct trans *, char *);
169 static char *gw_get_repo_diff(struct trans *, char *,
170 char *);
171 static char *gw_get_repo_tags(struct trans *, int, int);
172 static char *gw_get_repo_heads(struct trans *);
173 static char *gw_get_clone_url(struct trans *, char *);
174 static char *gw_get_got_link(struct trans *);
175 static char *gw_get_site_link(struct trans *);
176 static char *gw_html_escape(const char *);
177 static char *color_diff_line(char *);
179 static void gw_display_open(struct trans *, enum khttp,
180 enum kmime);
181 static void gw_display_index(struct trans *,
182 const struct got_error *);
184 static int gw_template(size_t, void *);
186 static const struct got_error* apply_unveil(const char *, const char *);
187 static const struct got_error* cmp_tags(void *, int *,
188 struct got_reference *,
189 struct got_reference *);
190 static const struct got_error* resolve_commit_arg(struct got_object_id **,
191 const char *, struct got_repository *);
192 static const struct got_error* match_object_id(struct got_object_id **,
193 char **, const char *r, int, int,
194 struct got_repository *);
195 static const struct got_error* blame_cb(void *, int, int,
196 struct got_object_id *);
197 static const struct got_error* gw_load_got_paths(struct trans *);
198 static const struct got_error* gw_load_got_path(struct trans *,
199 struct gw_dir *);
200 static const struct got_error* gw_parse_querystring(struct trans *);
201 static const struct got_error* match_logmsg(int *, struct got_object_id *,
202 struct got_commit_object *, regex_t *);
204 static const struct got_error* gw_blame(struct trans *);
205 static const struct got_error* gw_blob(struct trans *);
206 static const struct got_error* gw_blobdiff(struct trans *);
207 static const struct got_error* gw_commit(struct trans *);
208 static const struct got_error* gw_commitdiff(struct trans *);
209 static const struct got_error* gw_history(struct trans *);
210 static const struct got_error* gw_index(struct trans *);
211 static const struct got_error* gw_log(struct trans *);
212 static const struct got_error* gw_raw(struct trans *);
213 static const struct got_error* gw_logbriefs(struct trans *);
214 static const struct got_error* gw_snapshot(struct trans *);
215 static const struct got_error* gw_summary(struct trans *);
216 static const struct got_error* gw_tag(struct trans *);
217 static const struct got_error* gw_tree(struct trans *);
219 struct gw_query_action {
220 unsigned int func_id;
221 const char *func_name;
222 const struct got_error *(*func_main)(struct trans *);
223 char *template;
224 };
226 enum gw_query_actions {
227 GW_BLAME,
228 GW_BLOB,
229 GW_BLOBDIFF,
230 GW_COMMIT,
231 GW_COMMITDIFF,
232 GW_ERR,
233 GW_HISTORY,
234 GW_INDEX,
235 GW_LOG,
236 GW_RAW,
237 GW_LOGBRIEFS,
238 GW_SNAPSHOT,
239 GW_SUMMARY,
240 GW_TAG,
241 GW_TREE,
242 };
244 static struct gw_query_action gw_query_funcs[] = {
245 { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
246 { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
247 { GW_BLOBDIFF, "blobdiff", gw_blobdiff, "gw_tmpl/index.tmpl" },
248 { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
249 { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
250 { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
251 { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
252 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
253 { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
254 { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
255 { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
256 { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
257 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
258 { GW_TAG, "tag", gw_tag, "gw_tmpl/index.tmpl" },
259 { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
260 };
262 static const struct got_error *
263 apply_unveil(const char *repo_path, const char *repo_file)
265 const struct got_error *err;
267 if (repo_path && repo_file) {
268 char *full_path;
269 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
270 return got_error_from_errno("asprintf unveil");
271 if (unveil(full_path, "r") != 0)
272 return got_error_from_errno2("unveil", full_path);
275 if (repo_path && unveil(repo_path, "r") != 0)
276 return got_error_from_errno2("unveil", repo_path);
278 if (unveil("/tmp", "rwc") != 0)
279 return got_error_from_errno2("unveil", "/tmp");
281 err = got_privsep_unveil_exec_helpers();
282 if (err != NULL)
283 return err;
285 if (unveil(NULL, NULL) != 0)
286 return got_error_from_errno("unveil");
288 return NULL;
291 static const struct got_error *
292 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
293 struct got_reference *ref2)
295 const struct got_error *err = NULL;
296 struct got_repository *repo = arg;
297 struct got_object_id *id1, *id2 = NULL;
298 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
299 time_t time1, time2;
301 *cmp = 0;
303 err = got_ref_resolve(&id1, repo, ref1);
304 if (err)
305 return err;
306 err = got_object_open_as_tag(&tag1, repo, id1);
307 if (err)
308 goto done;
310 err = got_ref_resolve(&id2, repo, ref2);
311 if (err)
312 goto done;
313 err = got_object_open_as_tag(&tag2, repo, id2);
314 if (err)
315 goto done;
317 time1 = got_object_tag_get_tagger_time(tag1);
318 time2 = got_object_tag_get_tagger_time(tag2);
320 /* Put latest tags first. */
321 if (time1 < time2)
322 *cmp = 1;
323 else if (time1 > time2)
324 *cmp = -1;
325 else
326 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
327 done:
328 free(id1);
329 free(id2);
330 if (tag1)
331 got_object_tag_close(tag1);
332 if (tag2)
333 got_object_tag_close(tag2);
334 return err;
337 static const struct got_error *
338 resolve_commit_arg(struct got_object_id **commit_id,
339 const char *commit_id_arg, struct got_repository *repo)
341 const struct got_error *err;
342 struct got_reference *ref;
343 struct got_tag_object *tag;
345 err = got_repo_object_match_tag(&tag, commit_id_arg,
346 GOT_OBJ_TYPE_COMMIT, repo);
347 if (err == NULL) {
348 *commit_id = got_object_id_dup(
349 got_object_tag_get_object_id(tag));
350 if (*commit_id == NULL)
351 err = got_error_from_errno("got_object_id_dup");
352 got_object_tag_close(tag);
353 return err;
354 } else if (err->code != GOT_ERR_NO_OBJ)
355 return err;
357 err = got_ref_open(&ref, repo, commit_id_arg, 0);
358 if (err == NULL) {
359 err = got_ref_resolve(commit_id, repo, ref);
360 got_ref_close(ref);
361 } else {
362 if (err->code != GOT_ERR_NOT_REF)
363 return err;
364 err = got_repo_match_object_id_prefix(commit_id,
365 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
367 return err;
370 static const struct got_error *
371 match_object_id(struct got_object_id **id, char **label,
372 const char *id_str, int obj_type, int resolve_tags,
373 struct got_repository *repo)
375 const struct got_error *err;
376 struct got_tag_object *tag;
377 struct got_reference *ref = NULL;
379 *id = NULL;
380 *label = NULL;
382 if (resolve_tags) {
383 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
384 repo);
385 if (err == NULL) {
386 *id = got_object_id_dup(
387 got_object_tag_get_object_id(tag));
388 if (*id == NULL)
389 err = got_error_from_errno("got_object_id_dup");
390 else if (asprintf(label, "refs/tags/%s",
391 got_object_tag_get_name(tag)) == -1) {
392 err = got_error_from_errno("asprintf");
393 free(*id);
394 *id = NULL;
396 got_object_tag_close(tag);
397 return err;
398 } else if (err->code != GOT_ERR_NO_OBJ)
399 return err;
402 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
403 if (err) {
404 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
405 return err;
406 err = got_ref_open(&ref, repo, id_str, 0);
407 if (err != NULL)
408 goto done;
409 *label = strdup(got_ref_get_name(ref));
410 if (*label == NULL) {
411 err = got_error_from_errno("strdup");
412 goto done;
414 err = got_ref_resolve(id, repo, ref);
415 } else {
416 err = got_object_id_str(label, *id);
417 if (*label == NULL) {
418 err = got_error_from_errno("strdup");
419 goto done;
422 done:
423 if (ref)
424 got_ref_close(ref);
425 return err;
428 int
429 gw_get_repo_log_count(struct trans *gw_trans, char *start_commit)
431 const struct got_error *error;
432 struct got_repository *repo = NULL;
433 struct got_reflist_head refs;
434 struct got_commit_object *commit = NULL;
435 struct got_object_id *id = NULL;
436 struct got_commit_graph *graph = NULL;
437 char *in_repo_path = NULL, *path = NULL;
438 int log_count = 0;
440 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
441 if (error)
442 return 0;
444 SIMPLEQ_INIT(&refs);
446 if (start_commit == NULL) {
447 struct got_reference *head_ref;
448 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
449 if (error)
450 goto done;
452 error = got_ref_resolve(&id, repo, head_ref);
453 got_ref_close(head_ref);
454 if (error)
455 goto done;
457 error = got_object_open_as_commit(&commit, repo, id);
458 } else {
459 struct got_reference *ref;
460 error = got_ref_open(&ref, repo, start_commit, 0);
461 if (error == NULL) {
462 int obj_type;
463 error = got_ref_resolve(&id, repo, ref);
464 got_ref_close(ref);
465 if (error)
466 goto done;
467 error = got_object_get_type(&obj_type, repo, id);
468 if (error)
469 goto done;
470 if (obj_type == GOT_OBJ_TYPE_TAG) {
471 struct got_tag_object *tag;
472 error = got_object_open_as_tag(&tag, repo, id);
473 if (error)
474 goto done;
475 if (got_object_tag_get_object_type(tag) !=
476 GOT_OBJ_TYPE_COMMIT) {
477 got_object_tag_close(tag);
478 error = got_error(GOT_ERR_OBJ_TYPE);
479 goto done;
481 free(id);
482 id = got_object_id_dup(
483 got_object_tag_get_object_id(tag));
484 if (id == NULL)
485 error = got_error_from_errno(
486 "got_object_id_dup");
487 got_object_tag_close(tag);
488 if (error)
489 goto done;
490 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
491 error = got_error(GOT_ERR_OBJ_TYPE);
492 goto done;
494 error = got_object_open_as_commit(&commit, repo, id);
495 if (error)
496 goto done;
498 if (commit == NULL) {
499 error = got_repo_match_object_id_prefix(&id,
500 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
501 if (error)
502 goto done;
504 error = got_repo_match_object_id_prefix(&id,
505 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
506 if (error)
507 goto done;
510 error = got_object_open_as_commit(&commit, repo, id);
511 if (error)
512 goto done;
514 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
515 if (error)
516 goto done;
518 if (in_repo_path) {
519 free(path);
520 path = in_repo_path;
523 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
524 if (error)
525 goto done;
527 error = got_commit_graph_open(&graph, path, 0);
528 if (error)
529 goto done;
531 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
532 if (error)
533 goto done;
535 for (;;) {
536 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
537 NULL);
538 if (error) {
539 if (error->code == GOT_ERR_ITER_COMPLETED)
540 error = NULL;
541 break;
543 if (id == NULL)
544 break;
546 if (error)
547 break;
548 log_count++;
550 done:
551 free(in_repo_path);
552 if (graph)
553 got_commit_graph_close(graph);
554 if (repo) {
555 error = got_repo_close(repo);
556 if (error)
557 return 0;
559 if (error) {
560 khttp_puts(gw_trans->gw_req, "Error: ");
561 khttp_puts(gw_trans->gw_req, error->msg);
562 return 0;
563 } else
564 return log_count;
567 static const struct got_error *
568 gw_blame(struct trans *gw_trans)
570 const struct got_error *error = NULL;
572 char *log, *log_html;
574 error = apply_unveil(gw_trans->gw_dir->path, NULL);
575 if (error)
576 return error;
578 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGBLAME);
580 if (log != NULL && strcmp(log, "") != 0) {
581 if ((asprintf(&log_html, log_blame, log)) == -1)
582 return got_error_from_errno("asprintf");
583 khttp_puts(gw_trans->gw_req, log_html);
584 free(log_html);
585 free(log);
587 return error;
590 static const struct got_error *
591 gw_blob(struct trans *gw_trans)
593 const struct got_error *error = NULL;
595 return error;
598 static const struct got_error *
599 gw_blobdiff(struct trans *gw_trans)
601 const struct got_error *error = NULL;
603 return error;
606 static const struct got_error *
607 gw_commit(struct trans *gw_trans)
609 const struct got_error *error = NULL;
610 char *log, *log_html;
612 error = apply_unveil(gw_trans->gw_dir->path, NULL);
613 if (error)
614 return error;
616 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
618 if (log != NULL && strcmp(log, "") != 0) {
619 if ((asprintf(&log_html, log_commit, log)) == -1)
620 return got_error_from_errno("asprintf");
621 khttp_puts(gw_trans->gw_req, log_html);
622 free(log_html);
623 free(log);
625 return error;
628 static const struct got_error *
629 gw_commitdiff(struct trans *gw_trans)
631 const struct got_error *error = NULL;
632 char *log, *log_html;
634 error = apply_unveil(gw_trans->gw_dir->path, NULL);
635 if (error)
636 return error;
638 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
640 if (log != NULL && strcmp(log, "") != 0) {
641 if ((asprintf(&log_html, log_diff, log)) == -1)
642 return got_error_from_errno("asprintf");
643 khttp_puts(gw_trans->gw_req, log_html);
644 free(log_html);
645 free(log);
647 return error;
650 static const struct got_error *
651 gw_history(struct trans *gw_trans)
653 const struct got_error *error = NULL;
655 return error;
658 static const struct got_error *
659 gw_index(struct trans *gw_trans)
661 const struct got_error *error = NULL;
662 struct gw_dir *gw_dir = NULL;
663 char *html, *navs, *next, *prev;
664 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
666 error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
667 if (error)
668 return error;
670 error = gw_load_got_paths(gw_trans);
671 if (error)
672 return error;
674 khttp_puts(gw_trans->gw_req, index_projects_header);
676 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
677 dir_c++;
679 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
680 if (gw_trans->page > 0 && (gw_trans->page *
681 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
682 prev_disp++;
683 continue;
686 prev_disp++;
687 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
688 gw_dir->name, gw_dir->name)) == -1)
689 return got_error_from_errno("asprintf");
691 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
692 gw_dir->description, gw_dir->owner, gw_dir->age,
693 navs)) == -1)
694 return got_error_from_errno("asprintf");
696 khttp_puts(gw_trans->gw_req, html);
698 free(navs);
699 free(html);
701 if (gw_trans->gw_conf->got_max_repos_display == 0)
702 continue;
704 if (next_disp == gw_trans->gw_conf->got_max_repos_display)
705 khttp_puts(gw_trans->gw_req, np_wrapper_start);
706 else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
707 (gw_trans->page > 0) &&
708 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
709 prev_disp == gw_trans->repos_total))
710 khttp_puts(gw_trans->gw_req, np_wrapper_start);
712 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
713 (gw_trans->page > 0) &&
714 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
715 prev_disp == gw_trans->repos_total)) {
716 if ((asprintf(&prev, nav_prev,
717 gw_trans->page - 1)) == -1)
718 return got_error_from_errno("asprintf");
719 khttp_puts(gw_trans->gw_req, prev);
720 free(prev);
723 khttp_puts(gw_trans->gw_req, div_end);
725 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
726 next_disp == gw_trans->gw_conf->got_max_repos_display &&
727 dir_c != (gw_trans->page + 1) *
728 gw_trans->gw_conf->got_max_repos_display) {
729 if ((asprintf(&next, nav_next,
730 gw_trans->page + 1)) == -1)
731 return got_error_from_errno("calloc");
732 khttp_puts(gw_trans->gw_req, next);
733 khttp_puts(gw_trans->gw_req, div_end);
734 free(next);
735 next_disp = 0;
736 break;
739 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
740 (gw_trans->page > 0) &&
741 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
742 prev_disp == gw_trans->repos_total))
743 khttp_puts(gw_trans->gw_req, div_end);
745 next_disp++;
747 return error;
750 static const struct got_error *
751 gw_log(struct trans *gw_trans)
753 const struct got_error *error = NULL;
754 char *log, *log_html;
756 error = apply_unveil(gw_trans->gw_dir->path, NULL);
757 if (error)
758 return error;
760 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
761 gw_trans->gw_conf->got_max_commits_display, LOGFULL);
763 if (log != NULL && strcmp(log, "") != 0) {
764 if ((asprintf(&log_html, logs, log)) == -1)
765 return got_error_from_errno("asprintf");
766 khttp_puts(gw_trans->gw_req, log_html);
767 free(log_html);
768 free(log);
770 return error;
773 static const struct got_error *
774 gw_raw(struct trans *gw_trans)
776 const struct got_error *error = NULL;
778 return error;
781 static const struct got_error *
782 gw_logbriefs(struct trans *gw_trans)
784 const struct got_error *error = NULL;
785 char *log, *log_html;
787 error = apply_unveil(gw_trans->gw_dir->path, NULL);
788 if (error)
789 return error;
791 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
792 gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
794 if (log != NULL && strcmp(log, "") != 0) {
795 if ((asprintf(&log_html, summary_logbriefs,
796 log)) == -1)
797 return got_error_from_errno("asprintf");
798 khttp_puts(gw_trans->gw_req, log_html);
799 free(log_html);
800 free(log);
802 return error;
805 static const struct got_error *
806 gw_snapshot(struct trans *gw_trans)
808 const struct got_error *error = NULL;
810 return error;
813 static const struct got_error *
814 gw_summary(struct trans *gw_trans)
816 const struct got_error *error = NULL;
817 char *description_html, *repo_owner_html, *repo_age_html,
818 *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
819 *heads_html, *age;
821 error = apply_unveil(gw_trans->gw_dir->path, NULL);
822 if (error)
823 return error;
825 khttp_puts(gw_trans->gw_req, summary_wrapper);
826 if (gw_trans->gw_conf->got_show_repo_description) {
827 if (gw_trans->gw_dir->description != NULL &&
828 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
829 if ((asprintf(&description_html, description,
830 gw_trans->gw_dir->description)) == -1)
831 return got_error_from_errno("asprintf");
833 khttp_puts(gw_trans->gw_req, description_html);
834 free(description_html);
838 if (gw_trans->gw_conf->got_show_repo_owner) {
839 if (gw_trans->gw_dir->owner != NULL &&
840 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
841 if ((asprintf(&repo_owner_html, repo_owner,
842 gw_trans->gw_dir->owner)) == -1)
843 return got_error_from_errno("asprintf");
845 khttp_puts(gw_trans->gw_req, repo_owner_html);
846 free(repo_owner_html);
850 if (gw_trans->gw_conf->got_show_repo_age) {
851 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
852 "refs/heads", TM_LONG);
853 if (age != NULL && (strcmp(age, "") != 0)) {
854 if ((asprintf(&repo_age_html, last_change, age)) == -1)
855 return got_error_from_errno("asprintf");
857 khttp_puts(gw_trans->gw_req, repo_age_html);
858 free(repo_age_html);
859 free(age);
863 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
864 if (gw_trans->gw_dir->url != NULL &&
865 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
866 if ((asprintf(&cloneurl_html, cloneurl,
867 gw_trans->gw_dir->url)) == -1)
868 return got_error_from_errno("asprintf");
870 khttp_puts(gw_trans->gw_req, cloneurl_html);
871 free(cloneurl_html);
874 khttp_puts(gw_trans->gw_req, div_end);
876 log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
877 tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
878 heads = gw_get_repo_heads(gw_trans);
880 if (log != NULL && strcmp(log, "") != 0) {
881 if ((asprintf(&log_html, summary_logbriefs,
882 log)) == -1)
883 return got_error_from_errno("asprintf");
884 khttp_puts(gw_trans->gw_req, log_html);
885 free(log_html);
886 free(log);
889 if (tags != NULL && strcmp(tags, "") != 0) {
890 if ((asprintf(&tags_html, summary_tags,
891 tags)) == -1)
892 return got_error_from_errno("asprintf");
893 khttp_puts(gw_trans->gw_req, tags_html);
894 free(tags_html);
895 free(tags);
898 if (heads != NULL && strcmp(heads, "") != 0) {
899 if ((asprintf(&heads_html, summary_heads,
900 heads)) == -1)
901 return got_error_from_errno("asprintf");
902 khttp_puts(gw_trans->gw_req, heads_html);
903 free(heads_html);
904 free(heads);
906 return error;
909 static const struct got_error *
910 gw_tag(struct trans *gw_trans)
912 const struct got_error *error = NULL;
913 char *log, *log_html;
915 error = apply_unveil(gw_trans->gw_dir->path, NULL);
916 if (error)
917 return error;
919 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTAG);
921 if (log != NULL && strcmp(log, "") != 0) {
922 if ((asprintf(&log_html, log_tag, log)) == -1)
923 return got_error_from_errno("asprintf");
924 khttp_puts(gw_trans->gw_req, log_html);
925 free(log_html);
926 free(log);
928 return error;
931 static const struct got_error *
932 gw_tree(struct trans *gw_trans)
934 const struct got_error *error = NULL;
935 char *log, *log_html;
937 error = apply_unveil(gw_trans->gw_dir->path, NULL);
938 if (error)
939 return error;
941 log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
943 if (log != NULL && strcmp(log, "") != 0) {
944 if ((asprintf(&log_html, log_tree, log)) == -1)
945 return got_error_from_errno("asprintf");
946 khttp_puts(gw_trans->gw_req, log_html);
947 free(log_html);
948 free(log);
950 return error;
953 static const struct got_error *
954 gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
956 const struct got_error *error = NULL;
957 DIR *dt;
958 char *dir_test;
959 int opened = 0;
961 if ((asprintf(&dir_test, "%s/%s/%s",
962 gw_trans->gw_conf->got_repos_path, gw_dir->name,
963 GOTWEB_GIT_DIR)) == -1)
964 return got_error_from_errno("asprintf");
966 dt = opendir(dir_test);
967 if (dt == NULL) {
968 free(dir_test);
969 } else {
970 gw_dir->path = strdup(dir_test);
971 opened = 1;
972 goto done;
975 if ((asprintf(&dir_test, "%s/%s/%s",
976 gw_trans->gw_conf->got_repos_path, gw_dir->name,
977 GOTWEB_GOT_DIR)) == -1)
978 return got_error_from_errno("asprintf");
980 dt = opendir(dir_test);
981 if (dt == NULL)
982 free(dir_test);
983 else {
984 opened = 1;
985 error = got_error(GOT_ERR_NOT_GIT_REPO);
986 goto errored;
989 if ((asprintf(&dir_test, "%s/%s",
990 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
991 return got_error_from_errno("asprintf");
993 gw_dir->path = strdup(dir_test);
995 done:
996 gw_dir->description = gw_get_repo_description(gw_trans,
997 gw_dir->path);
998 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
999 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
1000 TM_DIFF);
1001 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
1003 errored:
1004 free(dir_test);
1005 if (opened)
1006 closedir(dt);
1007 return error;
1010 static const struct got_error *
1011 gw_load_got_paths(struct trans *gw_trans)
1013 const struct got_error *error = NULL;
1014 DIR *d;
1015 struct dirent **sd_dent;
1016 struct gw_dir *gw_dir;
1017 struct stat st;
1018 unsigned int d_cnt, d_i;
1020 d = opendir(gw_trans->gw_conf->got_repos_path);
1021 if (d == NULL) {
1022 error = got_error_from_errno2("opendir",
1023 gw_trans->gw_conf->got_repos_path);
1024 return error;
1027 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1028 alphasort);
1029 if (d_cnt == -1) {
1030 error = got_error_from_errno2("scandir",
1031 gw_trans->gw_conf->got_repos_path);
1032 return error;
1035 for (d_i = 0; d_i < d_cnt; d_i++) {
1036 if (gw_trans->gw_conf->got_max_repos > 0 &&
1037 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1038 break; /* account for parent and self */
1040 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1041 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1042 continue;
1044 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1045 return got_error_from_errno("gw_dir malloc");
1047 error = gw_load_got_path(gw_trans, gw_dir);
1048 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1049 continue;
1050 else if (error)
1051 return error;
1053 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1054 !got_path_dir_is_empty(gw_dir->path)) {
1055 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1056 entry);
1057 gw_trans->repos_total++;
1061 closedir(d);
1062 return error;
1065 static const struct got_error *
1066 gw_parse_querystring(struct trans *gw_trans)
1068 const struct got_error *error = NULL;
1069 struct kpair *p;
1070 struct gw_query_action *action = NULL;
1071 unsigned int i;
1073 if (gw_trans->gw_req->fieldnmap[0]) {
1074 error = got_error_from_errno("bad parse");
1075 return error;
1076 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1077 /* define gw_trans->repo_path */
1078 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
1079 return got_error_from_errno("asprintf");
1081 if ((asprintf(&gw_trans->repo_path, "%s/%s",
1082 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
1083 return got_error_from_errno("asprintf");
1085 /* get action and set function */
1086 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1087 for (i = 0; i < nitems(gw_query_funcs); i++) {
1088 action = &gw_query_funcs[i];
1089 if (action->func_name == NULL)
1090 continue;
1092 if (strcmp(action->func_name,
1093 p->parsed.s) == 0) {
1094 gw_trans->action = i;
1095 if ((asprintf(&gw_trans->action_name,
1096 "%s", action->func_name)) == -1)
1097 return
1098 got_error_from_errno(
1099 "asprintf");
1101 break;
1104 action = NULL;
1107 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1108 if ((asprintf(&gw_trans->commit, "%s",
1109 p->parsed.s)) == -1)
1110 return got_error_from_errno("asprintf");
1112 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1113 if ((asprintf(&gw_trans->repo_file, "%s",
1114 p->parsed.s)) == -1)
1115 return got_error_from_errno("asprintf");
1117 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1118 if ((asprintf(&gw_trans->repo_folder, "%s",
1119 p->parsed.s)) == -1)
1120 return got_error_from_errno("asprintf");
1122 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1123 if ((asprintf(&gw_trans->headref, "%s",
1124 p->parsed.s)) == -1)
1125 return got_error_from_errno("asprintf");
1127 if (action == NULL) {
1128 error = got_error_from_errno("invalid action");
1129 return error;
1131 if ((gw_trans->gw_dir =
1132 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1133 return got_error_from_errno("gw_dir malloc");
1135 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1136 if (error)
1137 return error;
1138 } else
1139 gw_trans->action = GW_INDEX;
1141 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1142 gw_trans->page = p->parsed.i;
1144 if (gw_trans->action == GW_RAW)
1145 gw_trans->mime = KMIME_TEXT_PLAIN;
1147 return error;
1150 static struct gw_dir *
1151 gw_init_gw_dir(char *dir)
1153 struct gw_dir *gw_dir;
1155 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1156 return NULL;
1158 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1159 return NULL;
1161 return gw_dir;
1164 static const struct got_error*
1165 match_logmsg(int *have_match, struct got_object_id *id,
1166 struct got_commit_object *commit, regex_t *regex)
1168 const struct got_error *err = NULL;
1169 regmatch_t regmatch;
1170 char *id_str = NULL, *logmsg = NULL;
1172 *have_match = 0;
1174 err = got_object_id_str(&id_str, id);
1175 if (err)
1176 return err;
1178 err = got_object_commit_get_logmsg(&logmsg, commit);
1179 if (err)
1180 goto done;
1182 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1183 *have_match = 1;
1184 done:
1185 free(id_str);
1186 free(logmsg);
1187 return err;
1190 static void
1191 gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
1193 khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1194 khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1195 khttps[code]);
1196 khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1197 kmimetypes[mime]);
1198 khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1199 khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1200 khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1201 khttp_body(gw_trans->gw_req);
1204 static void
1205 gw_display_index(struct trans *gw_trans, const struct got_error *err)
1207 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1208 khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1210 if (err)
1211 khttp_puts(gw_trans->gw_req, err->msg);
1212 else
1213 khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1214 gw_query_funcs[gw_trans->action].template);
1216 khtml_close(gw_trans->gw_html_req);
1219 static int
1220 gw_template(size_t key, void *arg)
1222 const struct got_error *error = NULL;
1223 struct trans *gw_trans = arg;
1224 char *gw_got_link, *gw_site_link;
1225 char *site_owner_name, *site_owner_name_h;
1227 switch (key) {
1228 case (TEMPL_HEAD):
1229 khttp_puts(gw_trans->gw_req, head);
1230 break;
1231 case(TEMPL_HEADER):
1232 gw_got_link = gw_get_got_link(gw_trans);
1233 if (gw_got_link != NULL)
1234 khttp_puts(gw_trans->gw_req, gw_got_link);
1236 free(gw_got_link);
1237 break;
1238 case (TEMPL_SITEPATH):
1239 gw_site_link = gw_get_site_link(gw_trans);
1240 if (gw_site_link != NULL)
1241 khttp_puts(gw_trans->gw_req, gw_site_link);
1243 free(gw_site_link);
1244 break;
1245 case(TEMPL_TITLE):
1246 if (gw_trans->gw_conf->got_site_name != NULL)
1247 khtml_puts(gw_trans->gw_html_req,
1248 gw_trans->gw_conf->got_site_name);
1250 break;
1251 case (TEMPL_SEARCH):
1252 khttp_puts(gw_trans->gw_req, search);
1253 break;
1254 case(TEMPL_SITEOWNER):
1255 if (gw_trans->gw_conf->got_site_owner != NULL &&
1256 gw_trans->gw_conf->got_show_site_owner) {
1257 site_owner_name =
1258 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1259 if ((asprintf(&site_owner_name_h, site_owner,
1260 site_owner_name))
1261 == -1)
1262 return 0;
1264 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1265 free(site_owner_name);
1266 free(site_owner_name_h);
1268 break;
1269 case(TEMPL_CONTENT):
1270 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1271 if (error)
1272 khttp_puts(gw_trans->gw_req, error->msg);
1274 break;
1275 default:
1276 return 0;
1277 break;
1279 return 1;
1282 static char *
1283 gw_get_repo_description(struct trans *gw_trans, char *dir)
1285 FILE *f;
1286 char *description = NULL, *d_file = NULL;
1287 unsigned int len;
1289 if (gw_trans->gw_conf->got_show_repo_description == false)
1290 goto err;
1292 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1293 goto err;
1295 if ((f = fopen(d_file, "r")) == NULL)
1296 goto err;
1298 fseek(f, 0, SEEK_END);
1299 len = ftell(f) + 1;
1300 fseek(f, 0, SEEK_SET);
1301 if ((description = calloc(len, sizeof(char *))) == NULL)
1302 goto err;
1304 fread(description, 1, len, f);
1305 fclose(f);
1306 free(d_file);
1307 return description;
1308 err:
1309 if ((asprintf(&description, "%s", "")) == -1)
1310 return NULL;
1312 return description;
1315 static char *
1316 gw_get_time_str(time_t committer_time, int ref_tm)
1318 struct tm tm;
1319 time_t diff_time;
1320 char *years = "years ago", *months = "months ago";
1321 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1322 char *minutes = "minutes ago", *seconds = "seconds ago";
1323 char *now = "right now";
1324 char *repo_age, *s;
1325 char datebuf[29];
1327 switch (ref_tm) {
1328 case TM_DIFF:
1329 diff_time = time(NULL) - committer_time;
1330 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1331 if ((asprintf(&repo_age, "%lld %s",
1332 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1333 return NULL;
1334 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1335 if ((asprintf(&repo_age, "%lld %s",
1336 (diff_time / 60 / 60 / 24 / (365 / 12)),
1337 months)) == -1)
1338 return NULL;
1339 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1340 if ((asprintf(&repo_age, "%lld %s",
1341 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1342 return NULL;
1343 } else if (diff_time > 60 * 60 * 24 * 2) {
1344 if ((asprintf(&repo_age, "%lld %s",
1345 (diff_time / 60 / 60 / 24), days)) == -1)
1346 return NULL;
1347 } else if (diff_time > 60 * 60 * 2) {
1348 if ((asprintf(&repo_age, "%lld %s",
1349 (diff_time / 60 / 60), hours)) == -1)
1350 return NULL;
1351 } else if (diff_time > 60 * 2) {
1352 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1353 minutes)) == -1)
1354 return NULL;
1355 } else if (diff_time > 2) {
1356 if ((asprintf(&repo_age, "%lld %s", diff_time,
1357 seconds)) == -1)
1358 return NULL;
1359 } else {
1360 if ((asprintf(&repo_age, "%s", now)) == -1)
1361 return NULL;
1363 break;
1364 case TM_LONG:
1365 if (gmtime_r(&committer_time, &tm) == NULL)
1366 return NULL;
1368 s = asctime_r(&tm, datebuf);
1369 if (s == NULL)
1370 return NULL;
1372 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1373 return NULL;
1374 break;
1376 return repo_age;
1379 static char *
1380 gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
1382 const struct got_error *error = NULL;
1383 struct got_object_id *id = NULL;
1384 struct got_repository *repo = NULL;
1385 struct got_commit_object *commit = NULL;
1386 struct got_reflist_head refs;
1387 struct got_reflist_entry *re;
1388 struct got_reference *head_ref;
1389 int is_head = 0;
1390 time_t committer_time = 0, cmp_time = 0;
1391 const char *refname;
1392 char *repo_age = NULL;
1394 if (repo_ref == NULL)
1395 return NULL;
1397 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1398 is_head = 1;
1400 SIMPLEQ_INIT(&refs);
1401 if (gw_trans->gw_conf->got_show_repo_age == false) {
1402 if ((asprintf(&repo_age, "")) == -1)
1403 return NULL;
1404 return repo_age;
1407 error = got_repo_open(&repo, dir, NULL);
1408 if (error)
1409 goto err;
1411 if (is_head)
1412 error = got_ref_list(&refs, repo, "refs/heads",
1413 got_ref_cmp_by_name, NULL);
1414 else
1415 error = got_ref_list(&refs, repo, repo_ref,
1416 got_ref_cmp_by_name, NULL);
1417 if (error)
1418 goto err;
1420 SIMPLEQ_FOREACH(re, &refs, entry) {
1421 if (is_head)
1422 refname = strdup(repo_ref);
1423 else
1424 refname = got_ref_get_name(re->ref);
1425 error = got_ref_open(&head_ref, repo, refname, 0);
1426 if (error)
1427 goto err;
1429 error = got_ref_resolve(&id, repo, head_ref);
1430 got_ref_close(head_ref);
1431 if (error)
1432 goto err;
1434 error = got_object_open_as_commit(&commit, repo, id);
1435 if (error)
1436 goto err;
1438 committer_time =
1439 got_object_commit_get_committer_time(commit);
1441 if (cmp_time < committer_time)
1442 cmp_time = committer_time;
1445 if (cmp_time != 0) {
1446 committer_time = cmp_time;
1447 repo_age = gw_get_time_str(committer_time, ref_tm);
1448 } else
1449 if ((asprintf(&repo_age, "")) == -1)
1450 return NULL;
1451 got_ref_list_free(&refs);
1452 free(id);
1453 return repo_age;
1454 err:
1455 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1456 return NULL;
1458 return repo_age;
1461 static char *
1462 gw_get_repo_diff(struct trans *gw_trans, char *id_str1, char *id_str2)
1464 const struct got_error *error;
1465 FILE *f = NULL;
1466 struct got_object_id *id1 = NULL, *id2 = NULL;
1467 struct got_repository *repo = NULL;
1468 struct buf *diffbuf = NULL;
1469 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1470 *buf_color = NULL;
1471 int type1, type2;
1472 size_t newsize;
1474 f = got_opentemp();
1475 if (f == NULL)
1476 return NULL;
1478 error = buf_alloc(&diffbuf, 0);
1479 if (error)
1480 return NULL;
1482 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1483 if (error)
1484 goto done;
1486 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
1487 repo);
1488 if (error)
1489 goto done;
1491 if (id_str2) {
1492 error = match_object_id(&id2, &label2, id_str2,
1493 GOT_OBJ_TYPE_ANY, 1, repo);
1494 if (error)
1495 goto done;
1497 error = got_object_get_type(&type2, repo, id2);
1498 if (error)
1499 goto done;
1502 error = got_object_get_type(&type1, repo, id1);
1503 if (error)
1504 goto done;
1506 if (id_str2 && type1 != type2) {
1507 error = got_error(GOT_ERR_OBJ_TYPE);
1508 goto done;
1511 switch (type1) {
1512 case GOT_OBJ_TYPE_BLOB:
1513 error = got_diff_objects_as_blobs(id2, id1, NULL, NULL, 3, 0,
1514 repo, f);
1515 break;
1516 case GOT_OBJ_TYPE_TREE:
1517 error = got_diff_objects_as_trees(id2, id1, "", "", 3, 0, repo,
1518 f);
1519 break;
1520 case GOT_OBJ_TYPE_COMMIT:
1521 error = got_diff_objects_as_commits(id2, id1, 3, 0, repo, f);
1522 break;
1523 default:
1524 error = got_error(GOT_ERR_OBJ_TYPE);
1527 if ((buf = calloc(128, sizeof(char *))) == NULL)
1528 goto done;
1530 fseek(f, 0, SEEK_SET);
1532 while ((fgets(buf, 128, f)) != NULL) {
1533 buf_color = color_diff_line(buf);
1534 error = buf_puts(&newsize, diffbuf, buf_color);
1535 if (error)
1536 return NULL;
1538 error = buf_puts(&newsize, diffbuf, div_end);
1539 if (error)
1540 return NULL;
1543 if (buf_len(diffbuf) > 0) {
1544 error = buf_putc(diffbuf, '\0');
1545 diff_html = strdup(buf_get(diffbuf));
1547 done:
1548 fclose(f);
1549 free(buf_color);
1550 free(buf);
1551 free(diffbuf);
1552 free(label1);
1553 free(label2);
1554 free(id1);
1555 free(id2);
1556 if (repo)
1557 got_repo_close(repo);
1559 if (error)
1560 return NULL;
1561 else
1562 return diff_html;
1565 static char *
1566 gw_get_repo_owner(struct trans *gw_trans, char *dir)
1568 FILE *f;
1569 char *owner = NULL, *d_file = NULL;
1570 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1571 char *comp, *pos, *buf;
1572 unsigned int i;
1574 if (gw_trans->gw_conf->got_show_repo_owner == false)
1575 goto err;
1577 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1578 goto err;
1580 if ((f = fopen(d_file, "r")) == NULL)
1581 goto err;
1583 if ((buf = calloc(128, sizeof(char *))) == NULL)
1584 goto err;
1586 while ((fgets(buf, 128, f)) != NULL) {
1587 if ((pos = strstr(buf, gotweb)) != NULL)
1588 break;
1590 if ((pos = strstr(buf, gitweb)) != NULL)
1591 break;
1594 if (pos == NULL)
1595 goto err;
1597 do {
1598 fgets(buf, 128, f);
1599 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1601 if (comp == NULL)
1602 goto err;
1604 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1605 goto err;
1607 for (i = 0; i < 2; i++) {
1608 owner = strsep(&buf, "\"");
1611 if (owner == NULL)
1612 goto err;
1614 fclose(f);
1615 free(d_file);
1616 return owner;
1617 err:
1618 if ((asprintf(&owner, "%s", "")) == -1)
1619 return NULL;
1621 return owner;
1624 static char *
1625 gw_get_clone_url(struct trans *gw_trans, char *dir)
1627 FILE *f;
1628 char *url = NULL, *d_file = NULL;
1629 unsigned int len;
1631 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1632 return NULL;
1634 if ((f = fopen(d_file, "r")) == NULL)
1635 return NULL;
1637 fseek(f, 0, SEEK_END);
1638 len = ftell(f) + 1;
1639 fseek(f, 0, SEEK_SET);
1641 if ((url = calloc(len, sizeof(char *))) == NULL)
1642 return NULL;
1644 fread(url, 1, len, f);
1645 fclose(f);
1646 free(d_file);
1647 return url;
1650 static char *
1651 gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1652 char *start_commit, int limit, int log_type)
1654 const struct got_error *error;
1655 struct got_repository *repo = NULL;
1656 struct got_reflist_head refs;
1657 struct got_reflist_entry *re;
1658 struct got_commit_object *commit = NULL;
1659 struct got_object_id *id1 = NULL, *id2 = NULL;
1660 struct got_object_qid *parent_id;
1661 struct got_commit_graph *graph = NULL;
1662 char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1663 *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1664 *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1665 *commit_commit_disp = NULL, *commit_age_diff = NULL,
1666 *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1667 *commit_age_long_disp = NULL, *commit_author = NULL,
1668 *commit_author_disp = NULL, *commit_committer = NULL,
1669 *commit_committer_disp = NULL, *commit_log = NULL,
1670 *commit_log_disp = NULL, *commit_parent = NULL,
1671 *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1672 *log_tree_html = NULL, *log_commit_html = NULL,
1673 *log_diff_html = NULL, *commit_tree = NULL,
1674 *commit_tree_disp = NULL, *log_tag_html = NULL,
1675 *log_blame_html = NULL;
1676 char *commit_log0, *newline;
1677 regex_t regex;
1678 int have_match, log_count = 0, has_parent = 1;
1679 size_t newsize;
1680 struct buf *diffbuf = NULL;
1681 time_t committer_time;
1683 if (gw_trans->action == GW_LOG || gw_trans->action == GW_LOGBRIEFS)
1684 log_count = gw_get_repo_log_count(gw_trans, start_commit);
1686 error = buf_alloc(&diffbuf, 0);
1687 if (error)
1688 return NULL;
1690 if (search_pattern &&
1691 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB |
1692 REG_NEWLINE))
1693 return NULL;
1695 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1696 if (error)
1697 return NULL;
1699 SIMPLEQ_INIT(&refs);
1701 if (start_commit == NULL) {
1702 struct got_reference *head_ref;
1703 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1704 if (error)
1705 goto done;
1707 error = got_ref_resolve(&id1, repo, head_ref);
1708 got_ref_close(head_ref);
1709 if (error)
1710 goto done;
1712 error = got_object_open_as_commit(&commit, repo, id1);
1713 } else {
1714 struct got_reference *ref;
1715 error = got_ref_open(&ref, repo, start_commit, 0);
1716 if (error == NULL) {
1717 int obj_type;
1718 error = got_ref_resolve(&id1, repo, ref);
1719 got_ref_close(ref);
1720 if (error)
1721 goto done;
1722 error = got_object_get_type(&obj_type, repo, id1);
1723 if (error)
1724 goto done;
1725 if (obj_type == GOT_OBJ_TYPE_TAG) {
1726 struct got_tag_object *tag;
1727 error = got_object_open_as_tag(&tag, repo, id1);
1728 if (error)
1729 goto done;
1730 if (got_object_tag_get_object_type(tag) !=
1731 GOT_OBJ_TYPE_COMMIT) {
1732 got_object_tag_close(tag);
1733 error = got_error(GOT_ERR_OBJ_TYPE);
1734 goto done;
1736 free(id1);
1737 id1 = got_object_id_dup(
1738 got_object_tag_get_object_id(tag));
1739 if (id1 == NULL)
1740 error = got_error_from_errno(
1741 "got_object_id_dup");
1742 got_object_tag_close(tag);
1743 if (error)
1744 goto done;
1745 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1746 error = got_error(GOT_ERR_OBJ_TYPE);
1747 goto done;
1749 error = got_object_open_as_commit(&commit, repo, id1);
1750 if (error)
1751 goto done;
1753 if (commit == NULL) {
1754 error = got_repo_match_object_id_prefix(&id1,
1755 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1756 if (error)
1757 goto done;
1759 error = got_repo_match_object_id_prefix(&id1,
1760 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1763 if (error)
1764 goto done;
1766 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1767 if (error)
1768 goto done;
1770 if (in_repo_path) {
1771 free(path);
1772 path = in_repo_path;
1775 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1776 if (error)
1777 goto done;
1779 error = got_commit_graph_open(&graph, path, 0);
1780 if (error)
1781 goto done;
1783 error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1784 if (error)
1785 goto done;
1787 for (;;) {
1788 error = got_commit_graph_iter_next(&id1, graph, repo, NULL,
1789 NULL);
1790 if (error) {
1791 if (error->code == GOT_ERR_ITER_COMPLETED)
1792 error = NULL;
1793 break;
1795 if (id1 == NULL)
1796 break;
1798 error = got_object_open_as_commit(&commit, repo, id1);
1799 if (error)
1800 break;
1802 if (search_pattern) {
1803 error = match_logmsg(&have_match, id1, commit,
1804 &regex);
1805 if (error) {
1806 got_object_commit_close(commit);
1807 break;
1809 if (have_match == 0) {
1810 got_object_commit_close(commit);
1811 continue;
1815 SIMPLEQ_FOREACH(re, &refs, entry) {
1816 char *s;
1817 const char *name;
1818 struct got_tag_object *tag = NULL;
1819 int cmp;
1821 name = got_ref_get_name(re->ref);
1822 if (strcmp(name, GOT_REF_HEAD) == 0)
1823 continue;
1824 if (strncmp(name, "refs/", 5) == 0)
1825 name += 5;
1826 if (strncmp(name, "got/", 4) == 0)
1827 continue;
1828 if (strncmp(name, "heads/", 6) == 0)
1829 name += 6;
1830 if (strncmp(name, "remotes/", 8) == 0)
1831 name += 8;
1832 if (strncmp(name, "tags/", 5) == 0) {
1833 error = got_object_open_as_tag(&tag, repo,
1834 re->id);
1835 if (error) {
1836 if (error->code != GOT_ERR_OBJ_TYPE)
1837 continue;
1839 * Ref points at something other
1840 * than a tag.
1842 error = NULL;
1843 tag = NULL;
1846 cmp = got_object_id_cmp(tag ?
1847 got_object_tag_get_object_id(tag) : re->id, id1);
1848 if (tag)
1849 got_object_tag_close(tag);
1850 if (cmp != 0)
1851 continue;
1852 s = refs_str;
1853 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1854 s ? ", " : "", name)) == -1) {
1855 error = got_error_from_errno("asprintf");
1856 free(s);
1857 goto done;
1859 free(s);
1862 if (refs_str == NULL)
1863 refs_str_disp = strdup("");
1864 else {
1865 if ((asprintf(&refs_str_disp, "(%s)",
1866 refs_str)) == -1) {
1867 error = got_error_from_errno("asprintf");
1868 free(refs_str);
1869 goto done;
1873 error = got_object_id_str(&id_str1, id1);
1874 if (error)
1875 goto done;
1877 error = got_object_id_str(&treeid,
1878 got_object_commit_get_tree_id(commit));
1879 if (error)
1880 goto done;
1882 if (gw_trans->action == GW_COMMIT ||
1883 gw_trans->action == GW_COMMITDIFF) {
1884 parent_id =
1885 SIMPLEQ_FIRST(
1886 got_object_commit_get_parent_ids(commit));
1887 if (parent_id != NULL) {
1888 id2 = got_object_id_dup(parent_id->id);
1889 free (parent_id);
1890 error = got_object_id_str(&id_str2, id2);
1891 if (error)
1892 goto done;
1893 free(id2);
1894 } else {
1895 has_parent = 0;
1896 id_str2 = strdup("/dev/null");
1900 committer_time =
1901 got_object_commit_get_committer_time(commit);
1903 if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1904 error = got_error_from_errno("asprintf");
1905 goto done;
1908 if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1909 error = got_error_from_errno("asprintf");
1910 goto done;
1913 if ((asprintf(&commit_tree_disp, commit_tree_html,
1914 treeid)) == -1) {
1915 error = got_error_from_errno("asprintf");
1916 goto done;
1919 if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1920 id_str1)) == -1) {
1921 error = got_error_from_errno("asprintf");
1922 goto done;
1925 if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1926 error = got_error_from_errno("asprintf");
1927 goto done;
1930 if ((asprintf(&commit_commit_disp, commit_commit_html,
1931 commit_commit, refs_str_disp)) == -1) {
1932 error = got_error_from_errno("asprintf");
1933 goto done;
1936 if ((asprintf(&commit_age_long, "%s",
1937 gw_get_time_str(committer_time, TM_LONG))) == -1) {
1938 error = got_error_from_errno("asprintf");
1939 goto done;
1942 if ((asprintf(&commit_age_long_disp, commit_age_html,
1943 commit_age_long)) == -1) {
1944 error = got_error_from_errno("asprintf");
1945 goto done;
1948 if ((asprintf(&commit_age_diff, "%s",
1949 gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1950 error = got_error_from_errno("asprintf");
1951 goto done;
1954 if ((asprintf(&commit_age_diff_disp, commit_age_html,
1955 commit_age_diff)) == -1) {
1956 error = got_error_from_errno("asprintf");
1957 goto done;
1960 if ((asprintf(&commit_author, "%s",
1961 got_object_commit_get_author(commit))) == -1) {
1962 error = got_error_from_errno("asprintf");
1963 goto done;
1966 if ((asprintf(&commit_author_disp, commit_author_html,
1967 gw_html_escape(commit_author))) == -1) {
1968 error = got_error_from_errno("asprintf");
1969 goto done;
1972 if ((asprintf(&commit_committer, "%s",
1973 got_object_commit_get_committer(commit))) == -1) {
1974 error = got_error_from_errno("asprintf");
1975 goto done;
1978 if ((asprintf(&commit_committer_disp, commit_committer_html,
1979 gw_html_escape(commit_committer))) == -1) {
1980 error = got_error_from_errno("asprintf");
1981 goto done;
1984 if (strcmp(commit_author, commit_committer) == 0) {
1985 free(commit_committer_disp);
1986 commit_committer_disp = strdup("");
1989 error = got_object_commit_get_logmsg(&commit_log0, commit);
1990 if (error)
1991 goto done;
1993 commit_log = commit_log0;
1994 while (*commit_log == '\n')
1995 commit_log++;
1997 switch(log_type) {
1998 case (LOGBRIEF):
1999 newline = strchr(commit_log, '\n');
2000 if (newline)
2001 *newline = '\0';
2003 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
2004 gw_trans->repo_name, id_str1, gw_trans->repo_name,
2005 id_str1, gw_trans->repo_name, id_str1,
2006 gw_trans->repo_name, id_str1)) == -1) {
2007 error = got_error_from_errno("asprintf");
2008 goto done;
2011 if ((asprintf(&commit_row, logbriefs_row,
2012 commit_age_diff, commit_author, commit_log,
2013 logbriefs_navs_html)) == -1) {
2014 error = got_error_from_errno("asprintf");
2015 goto done;
2018 free(logbriefs_navs_html);
2019 logbriefs_navs_html = NULL;
2020 break;
2021 case (LOGFULL):
2022 if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
2023 gw_trans->repo_name, id_str1, gw_trans->repo_name,
2024 id_str1, gw_trans->repo_name, id_str1,
2025 gw_trans->repo_name, id_str1)) == -1) {
2026 error = got_error_from_errno("asprintf");
2027 goto done;
2030 if ((asprintf(&commit_row, logs_row, commit_commit_disp,
2031 commit_author_disp, commit_committer_disp,
2032 commit_age_long_disp, gw_html_escape(commit_log),
2033 logbriefs_navs_html)) == -1) {
2034 error = got_error_from_errno("asprintf");
2035 goto done;
2038 free(logbriefs_navs_html);
2039 logbriefs_navs_html = NULL;
2040 break;
2041 case (LOGTAG):
2042 log_tag_html = strdup("tag log here");
2044 if ((asprintf(&commit_row, log_tag_row,
2045 gw_html_escape(commit_log), log_tag_html)) == -1) {
2046 error = got_error_from_errno("asprintf");
2047 goto done;
2050 free(log_tag_html);
2051 break;
2052 case (LOGBLAME):
2053 log_blame_html = gw_get_file_blame(gw_trans,
2054 start_commit);
2056 if ((asprintf(&commit_row, log_blame_row,
2057 gw_html_escape(commit_log), log_blame_html)) == -1) {
2058 error = got_error_from_errno("asprintf");
2059 goto done;
2062 free(log_blame_html);
2063 break;
2064 case (LOGTREE):
2065 log_tree_html = gw_get_repo_tree(gw_trans,
2066 start_commit);
2068 if ((asprintf(&commit_row, log_tree_row,
2069 gw_html_escape(commit_log), log_tree_html)) == -1) {
2070 error = got_error_from_errno("asprintf");
2071 goto done;
2074 free(log_tree_html);
2075 break;
2076 case (LOGCOMMIT):
2077 if ((asprintf(&commit_log_disp, commit_log_html,
2078 gw_html_escape(commit_log))) == -1) {
2079 error = got_error_from_errno("asprintf");
2080 goto done;
2083 log_commit_html = strdup("commit here");
2085 if ((asprintf(&commit_row, log_commit_row,
2086 commit_diff_disp, commit_commit_disp,
2087 commit_tree_disp, commit_author_disp,
2088 commit_committer_disp, commit_age_long_disp,
2089 commit_log_disp, log_commit_html)) == -1) {
2090 error = got_error_from_errno("asprintf");
2091 goto done;
2093 free(commit_log_disp);
2094 free(log_commit_html);
2096 break;
2097 case (LOGDIFF):
2098 if ((asprintf(&commit_log_disp, commit_log_html,
2099 gw_html_escape(commit_log))) == -1) {
2100 error = got_error_from_errno("asprintf");
2101 goto done;
2104 if (has_parent)
2105 log_diff_html = gw_get_repo_diff(gw_trans,
2106 commit_commit, commit_parent);
2107 else
2108 log_diff_html = gw_get_repo_diff(gw_trans,
2109 commit_commit, NULL);
2111 if ((asprintf(&commit_row, log_diff_row,
2112 commit_diff_disp, commit_commit_disp,
2113 commit_tree_disp, commit_author_disp,
2114 commit_committer_disp, commit_age_long_disp,
2115 commit_log_disp, log_diff_html)) == -1) {
2116 error = got_error_from_errno("asprintf");
2117 goto done;
2119 free(commit_log_disp);
2120 free(log_diff_html);
2122 break;
2123 default:
2124 return NULL;
2127 error = buf_puts(&newsize, diffbuf, commit_row);
2129 free(commit_parent);
2130 free(commit_diff_disp);
2131 free(commit_tree_disp);
2132 free(commit_age_diff);
2133 free(commit_age_diff_disp);
2134 free(commit_age_long);
2135 free(commit_age_long_disp);
2136 free(commit_author);
2137 free(commit_author_disp);
2138 free(commit_committer);
2139 free(commit_committer_disp);
2140 free(commit_log0);
2141 free(commit_row);
2142 free(refs_str_disp);
2143 free(refs_str);
2144 refs_str = NULL;
2145 free(id_str1);
2146 id_str1 = NULL;
2147 free(id_str2);
2148 id_str2 = NULL;
2150 if (error || (limit && --limit == 0))
2151 break;
2154 if (error)
2155 goto done;
2157 if (buf_len(diffbuf) > 0) {
2158 error = buf_putc(diffbuf, '\0');
2159 logs = strdup(buf_get(diffbuf));
2161 done:
2162 buf_free(diffbuf);
2163 free(in_repo_path);
2164 if (commit != NULL)
2165 got_object_commit_close(commit);
2166 if (search_pattern)
2167 regfree(&regex);
2168 if (graph)
2169 got_commit_graph_close(graph);
2170 if (repo) {
2171 error = got_repo_close(repo);
2172 if (error)
2173 return NULL;
2175 if (error) {
2176 khttp_puts(gw_trans->gw_req, "Error: ");
2177 khttp_puts(gw_trans->gw_req, error->msg);
2178 return NULL;
2179 } else
2180 return logs;
2183 static char *
2184 gw_get_repo_tags(struct trans *gw_trans, int limit, int tag_type)
2186 const struct got_error *error = NULL;
2187 struct got_repository *repo = NULL;
2188 struct got_reflist_head refs;
2189 struct got_reflist_entry *re;
2190 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
2191 *age = NULL;
2192 char *newline;
2193 struct buf *diffbuf = NULL;
2194 size_t newsize;
2196 error = buf_alloc(&diffbuf, 0);
2197 if (error)
2198 return NULL;
2199 SIMPLEQ_INIT(&refs);
2201 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2202 if (error)
2203 goto done;
2205 error = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
2206 if (error)
2207 goto done;
2209 SIMPLEQ_FOREACH(re, &refs, entry) {
2210 const char *refname;
2211 char *refstr, *tag_log0, *tag_log, *id_str;
2212 time_t tagger_time;
2213 struct got_object_id *id;
2214 struct got_tag_object *tag;
2216 refname = got_ref_get_name(re->ref);
2217 if (strncmp(refname, "refs/tags/", 10) != 0)
2218 continue;
2219 refname += 10;
2220 refstr = got_ref_to_str(re->ref);
2221 if (refstr == NULL) {
2222 error = got_error_from_errno("got_ref_to_str");
2223 goto done;
2226 error = got_ref_resolve(&id, repo, re->ref);
2227 if (error)
2228 goto done;
2229 error = got_object_open_as_tag(&tag, repo, id);
2230 free(id);
2231 if (error)
2232 goto done;
2234 tagger_time = got_object_tag_get_tagger_time(tag);
2236 error = got_object_id_str(&id_str,
2237 got_object_tag_get_object_id(tag));
2238 if (error)
2239 goto done;
2241 tag_log0 = strdup(got_object_tag_get_message(tag));
2243 if (tag_log0 == NULL) {
2244 error = got_error_from_errno("strdup");
2245 goto done;
2248 tag_log = tag_log0;
2249 while (*tag_log == '\n')
2250 tag_log++;
2252 switch (tag_type) {
2253 case TAGBRIEF:
2254 newline = strchr(tag_log, '\n');
2255 if (newline)
2256 *newline = '\0';
2258 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
2259 TM_DIFF))) == -1) {
2260 error = got_error_from_errno("asprintf");
2261 goto done;
2264 if ((asprintf(&tags_navs_disp, tags_navs,
2265 gw_trans->repo_name, id_str, gw_trans->repo_name,
2266 id_str, gw_trans->repo_name, id_str,
2267 gw_trans->repo_name, id_str)) == -1) {
2268 error = got_error_from_errno("asprintf");
2269 goto done;
2272 if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
2273 tags_navs_disp)) == -1) {
2274 error = got_error_from_errno("asprintf");
2275 goto done;
2278 free(tags_navs_disp);
2279 break;
2280 case TAGFULL:
2281 break;
2282 default:
2283 break;
2286 got_object_tag_close(tag);
2288 error = buf_puts(&newsize, diffbuf, tag_row);
2290 free(id_str);
2291 free(refstr);
2292 free(age);
2293 free(tag_log0);
2294 free(tag_row);
2296 if (error || (limit && --limit == 0))
2297 break;
2300 if (buf_len(diffbuf) > 0) {
2301 error = buf_putc(diffbuf, '\0');
2302 tags = strdup(buf_get(diffbuf));
2304 done:
2305 buf_free(diffbuf);
2306 got_ref_list_free(&refs);
2307 if (repo)
2308 got_repo_close(repo);
2309 if (error)
2310 return NULL;
2311 else
2312 return tags;
2315 struct blame_line {
2316 int annotated;
2317 char *id_str;
2318 char *committer;
2319 char datebuf[11]; /* YYYY-MM-DD + NUL */
2322 struct blame_cb_args {
2323 struct blame_line *lines;
2324 int nlines;
2325 int nlines_prec;
2326 int lineno_cur;
2327 off_t *line_offsets;
2328 FILE *f;
2329 struct got_repository *repo;
2330 struct trans *gw_trans;
2331 struct buf *blamebuf;
2334 static const struct got_error *
2335 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2337 const struct got_error *err = NULL;
2338 struct blame_cb_args *a = arg;
2339 struct blame_line *bline;
2340 char *line = NULL;
2341 size_t linesize = 0, newsize;
2342 struct got_commit_object *commit = NULL;
2343 off_t offset;
2344 struct tm tm;
2345 time_t committer_time;
2347 if (nlines != a->nlines ||
2348 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2349 return got_error(GOT_ERR_RANGE);
2351 if (lineno == -1)
2352 return NULL; /* no change in this commit */
2354 /* Annotate this line. */
2355 bline = &a->lines[lineno - 1];
2356 if (bline->annotated)
2357 return NULL;
2358 err = got_object_id_str(&bline->id_str, id);
2359 if (err)
2360 return err;
2362 err = got_object_open_as_commit(&commit, a->repo, id);
2363 if (err)
2364 goto done;
2366 bline->committer = strdup(got_object_commit_get_committer(commit));
2367 if (bline->committer == NULL) {
2368 err = got_error_from_errno("strdup");
2369 goto done;
2372 committer_time = got_object_commit_get_committer_time(commit);
2373 if (localtime_r(&committer_time, &tm) == NULL)
2374 return got_error_from_errno("localtime_r");
2375 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2376 &tm) >= sizeof(bline->datebuf)) {
2377 err = got_error(GOT_ERR_NO_SPACE);
2378 goto done;
2380 bline->annotated = 1;
2382 /* Print lines annotated so far. */
2383 bline = &a->lines[a->lineno_cur - 1];
2384 if (!bline->annotated)
2385 goto done;
2387 offset = a->line_offsets[a->lineno_cur - 1];
2388 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2389 err = got_error_from_errno("fseeko");
2390 goto done;
2393 while (bline->annotated) {
2394 char *smallerthan, *at, *nl, *committer, *blame_row = NULL;
2395 size_t len;
2397 if (getline(&line, &linesize, a->f) == -1) {
2398 if (ferror(a->f))
2399 err = got_error_from_errno("getline");
2400 break;
2403 committer = bline->committer;
2404 smallerthan = strchr(committer, '<');
2405 if (smallerthan && smallerthan[1] != '\0')
2406 committer = smallerthan + 1;
2407 at = strchr(committer, '@');
2408 if (at)
2409 *at = '\0';
2410 len = strlen(committer);
2411 if (len >= 9)
2412 committer[8] = '\0';
2414 nl = strchr(line, '\n');
2415 if (nl)
2416 *nl = '\0';
2417 asprintf(&blame_row, log_blame_line, a->nlines_prec,
2418 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2419 line);
2420 a->lineno_cur++;
2421 err = buf_puts(&newsize, a->blamebuf, blame_row);
2422 if (err)
2423 return err;
2425 bline = &a->lines[a->lineno_cur - 1];
2426 free(blame_row);
2428 done:
2429 if (commit)
2430 got_object_commit_close(commit);
2431 free(line);
2432 return err;
2435 static char*
2436 gw_get_file_blame(struct trans *gw_trans, char *commit_str)
2438 const struct got_error *error = NULL;
2439 struct got_repository *repo = NULL;
2440 struct got_object_id *obj_id = NULL;
2441 struct got_object_id *commit_id = NULL;
2442 struct got_blob_object *blob = NULL;
2443 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2444 *blame_row = NULL, *id_str, *folder = NULL;
2445 struct blame_cb_args bca;
2446 int nentries, i, obj_type;
2447 size_t filesize;
2449 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2450 if (error)
2451 goto done;
2453 if (gw_trans->repo_folder != NULL) {
2454 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2455 error = got_error_from_errno("asprintf");
2456 goto done;
2458 } else
2459 folder = strdup("");
2461 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2462 error = got_error_from_errno("asprintf");
2463 goto done;
2465 free(folder);
2467 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2468 if (error)
2469 goto done;
2471 error = resolve_commit_arg(&commit_id, commit_str, repo);
2472 if (error)
2473 goto done;
2475 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2476 if (error)
2477 goto done;
2479 if (obj_id == NULL) {
2480 error = got_error(GOT_ERR_NO_OBJ);
2481 goto done;
2484 error = got_object_get_type(&obj_type, repo, obj_id);
2485 if (error)
2486 goto done;
2488 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2489 error = got_error(GOT_ERR_OBJ_TYPE);
2490 goto done;
2493 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2494 if (error)
2495 goto done;
2497 error = buf_alloc(&bca.blamebuf, 0);
2498 if (error)
2499 goto done;
2501 bca.f = got_opentemp();
2502 if (bca.f == NULL) {
2503 error = got_error_from_errno("got_opentemp");
2504 goto done;
2506 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2507 &bca.line_offsets, bca.f, blob);
2508 if (error || bca.nlines == 0)
2509 goto done;
2511 /* Don't include \n at EOF in the blame line count. */
2512 if (bca.line_offsets[bca.nlines - 1] == filesize)
2513 bca.nlines--;
2515 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2516 if (bca.lines == NULL) {
2517 error = got_error_from_errno("calloc");
2518 goto done;
2520 bca.lineno_cur = 1;
2521 bca.nlines_prec = 0;
2522 i = bca.nlines;
2523 while (i > 0) {
2524 i /= 10;
2525 bca.nlines_prec++;
2527 bca.repo = repo;
2528 bca.gw_trans = gw_trans;
2530 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca, NULL,
2531 NULL);
2532 if (buf_len(bca.blamebuf) > 0) {
2533 error = buf_putc(bca.blamebuf, '\0');
2534 blame_html = strdup(buf_get(bca.blamebuf));
2536 done:
2537 free(bca.blamebuf);
2538 free(in_repo_path);
2539 free(commit_id);
2540 free(obj_id);
2541 free(path);
2543 if (blob)
2544 error = got_object_blob_close(blob);
2545 if (repo)
2546 error = got_repo_close(repo);
2547 if (error)
2548 return NULL;
2549 if (bca.lines) {
2550 for (i = 0; i < bca.nlines; i++) {
2551 struct blame_line *bline = &bca.lines[i];
2552 free(bline->id_str);
2553 free(bline->committer);
2555 free(bca.lines);
2557 free(bca.line_offsets);
2558 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2559 error = got_error_from_errno("fclose");
2560 if (error)
2561 return NULL;
2562 else
2563 return blame_html;
2566 static char*
2567 gw_get_repo_tree(struct trans *gw_trans, char *commit_str)
2569 const struct got_error *error = NULL;
2570 struct got_repository *repo = NULL;
2571 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2572 struct got_tree_object *tree = NULL;
2573 struct buf *diffbuf = NULL;
2574 size_t newsize;
2575 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2576 *tree_row = NULL, *id_str;
2577 int nentries, i;
2579 error = buf_alloc(&diffbuf, 0);
2580 if (error)
2581 return NULL;
2583 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2584 if (error)
2585 goto done;
2587 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2588 if (error)
2589 goto done;
2591 if (gw_trans->repo_folder != NULL)
2592 path = strdup(gw_trans->repo_folder);
2593 else if (in_repo_path) {
2594 free(path);
2595 path = in_repo_path;
2598 if (commit_str == NULL) {
2599 struct got_reference *head_ref;
2600 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2601 if (error)
2602 goto done;
2604 error = got_ref_resolve(&commit_id, repo, head_ref);
2605 got_ref_close(head_ref);
2607 } else
2608 error = resolve_commit_arg(&commit_id, commit_str, repo);
2609 if (error)
2610 goto done;
2612 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2613 if (error)
2614 goto done;
2616 error = got_object_open_as_tree(&tree, repo, tree_id);
2617 if (error)
2618 goto done;
2620 nentries = got_object_tree_get_nentries(tree);
2622 for (i = 0; i < nentries; i++) {
2623 struct got_tree_entry *te;
2624 const char *modestr = "";
2625 char *id = NULL, *url_html = NULL;
2627 te = got_object_tree_get_entry(tree, i);
2629 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2630 if (error)
2631 goto done;
2633 if ((asprintf(&id, "%s", id_str)) == -1) {
2634 error = got_error_from_errno("asprintf");
2635 free(id_str);
2636 goto done;
2639 mode_t mode = got_tree_entry_get_mode(te);
2641 if (got_object_tree_entry_is_submodule(te))
2642 modestr = "$";
2643 else if (S_ISLNK(mode))
2644 modestr = "@";
2645 else if (S_ISDIR(mode))
2646 modestr = "/";
2647 else if (mode & S_IXUSR)
2648 modestr = "*";
2650 char *build_folder = NULL;
2651 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2652 if (gw_trans->repo_folder != NULL) {
2653 if ((asprintf(&build_folder, "%s/%s",
2654 gw_trans->repo_folder,
2655 got_tree_entry_get_name(te))) == -1) {
2656 error =
2657 got_error_from_errno("asprintf");
2658 goto done;
2660 } else {
2661 if (asprintf(&build_folder, "%s",
2662 got_tree_entry_get_name(te)) == -1)
2663 goto done;
2666 if ((asprintf(&url_html, folder_html,
2667 gw_trans->repo_name, gw_trans->action_name,
2668 gw_trans->commit, build_folder,
2669 got_tree_entry_get_name(te), modestr)) == -1) {
2670 error = got_error_from_errno("asprintf");
2671 goto done;
2673 } else {
2674 if (gw_trans->repo_folder != NULL) {
2675 if ((asprintf(&build_folder, "%s",
2676 gw_trans->repo_folder)) == -1) {
2677 error =
2678 got_error_from_errno("asprintf");
2679 goto done;
2681 } else
2682 build_folder = strdup("");
2684 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2685 "blame", gw_trans->commit,
2686 got_tree_entry_get_name(te), build_folder,
2687 got_tree_entry_get_name(te), modestr)) == -1) {
2688 error = got_error_from_errno("asprintf");
2689 goto done;
2692 free(build_folder);
2694 if (error)
2695 goto done;
2697 if ((asprintf(&tree_row, trees_row, "", url_html)) == -1) {
2698 error = got_error_from_errno("asprintf");
2699 goto done;
2701 error = buf_puts(&newsize, diffbuf, tree_row);
2702 if (error)
2703 goto done;
2705 free(id);
2706 free(id_str);
2707 free(url_html);
2708 free(tree_row);
2711 if (buf_len(diffbuf) > 0) {
2712 error = buf_putc(diffbuf, '\0');
2713 tree_html = strdup(buf_get(diffbuf));
2715 done:
2716 if (tree)
2717 got_object_tree_close(tree);
2718 if (repo)
2719 got_repo_close(repo);
2721 free(in_repo_path);
2722 free(tree_id);
2723 free(diffbuf);
2724 if (error)
2725 return NULL;
2726 else
2727 return tree_html;
2730 static char *
2731 gw_get_repo_heads(struct trans *gw_trans)
2733 const struct got_error *error = NULL;
2734 struct got_repository *repo = NULL;
2735 struct got_reflist_head refs;
2736 struct got_reflist_entry *re;
2737 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2738 struct buf *diffbuf = NULL;
2739 size_t newsize;
2741 error = buf_alloc(&diffbuf, 0);
2742 if (error)
2743 return NULL;
2745 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2746 if (error)
2747 goto done;
2749 SIMPLEQ_INIT(&refs);
2750 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2751 NULL);
2752 if (error)
2753 goto done;
2755 SIMPLEQ_FOREACH(re, &refs, entry) {
2756 char *refname;
2758 refname = strdup(got_ref_get_name(re->ref));
2759 if (refname == NULL) {
2760 error = got_error_from_errno("got_ref_to_str");
2761 goto done;
2764 if (strncmp(refname, "refs/heads/", 11) != 0) {
2765 free(refname);
2766 continue;
2769 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2770 TM_DIFF);
2772 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2773 refname, gw_trans->repo_name, refname,
2774 gw_trans->repo_name, refname, gw_trans->repo_name,
2775 refname)) == -1) {
2776 error = got_error_from_errno("asprintf");
2777 goto done;
2780 if (strncmp(refname, "refs/heads/", 11) == 0)
2781 refname += 11;
2783 if ((asprintf(&head_row, heads_row, age, refname,
2784 head_navs_disp)) == -1) {
2785 error = got_error_from_errno("asprintf");
2786 goto done;
2789 error = buf_puts(&newsize, diffbuf, head_row);
2791 free(head_navs_disp);
2792 free(head_row);
2795 if (buf_len(diffbuf) > 0) {
2796 error = buf_putc(diffbuf, '\0');
2797 heads = strdup(buf_get(diffbuf));
2799 done:
2800 buf_free(diffbuf);
2801 got_ref_list_free(&refs);
2802 if (repo)
2803 got_repo_close(repo);
2804 if (error)
2805 return NULL;
2806 else
2807 return heads;
2810 static char *
2811 gw_get_got_link(struct trans *gw_trans)
2813 char *link;
2815 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2816 gw_trans->gw_conf->got_logo)) == -1)
2817 return NULL;
2819 return link;
2822 static char *
2823 gw_get_site_link(struct trans *gw_trans)
2825 char *link, *repo = "", *action = "";
2827 if (gw_trans->repo_name != NULL)
2828 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2829 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2830 return NULL;
2832 if (gw_trans->action_name != NULL)
2833 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2834 return NULL;
2836 if ((asprintf(&link, site_link, GOTWEB,
2837 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2838 return NULL;
2840 return link;
2843 static char *
2844 color_diff_line(char *buf)
2846 const struct got_error *error = NULL;
2847 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2848 struct buf *diffbuf = NULL;
2849 size_t newsize;
2851 error = buf_alloc(&diffbuf, 0);
2852 if (error)
2853 return NULL;
2855 if (strncmp(buf, "-", 1) == 0)
2856 color = "diff_minus";
2857 if (strncmp(buf, "+", 1) == 0)
2858 color = "diff_plus";
2859 if (strncmp(buf, "@@", 2) == 0)
2860 color = "diff_chunk_header";
2861 if (strncmp(buf, "@@", 2) == 0)
2862 color = "diff_chunk_header";
2863 if (strncmp(buf, "commit +", 8) == 0)
2864 color = "diff_meta";
2865 if (strncmp(buf, "commit -", 8) == 0)
2866 color = "diff_meta";
2867 if (strncmp(buf, "blob +", 6) == 0)
2868 color = "diff_meta";
2869 if (strncmp(buf, "blob -", 6) == 0)
2870 color = "diff_meta";
2871 if (strncmp(buf, "file +", 6) == 0)
2872 color = "diff_meta";
2873 if (strncmp(buf, "file -", 6) == 0)
2874 color = "diff_meta";
2875 if (strncmp(buf, "from:", 5) == 0)
2876 color = "diff_author";
2877 if (strncmp(buf, "via:", 4) == 0)
2878 color = "diff_author";
2879 if (strncmp(buf, "date:", 5) == 0)
2880 color = "diff_date";
2882 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2883 return NULL;
2885 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2886 if (error)
2887 return NULL;
2889 error = buf_puts(&newsize, diffbuf, buf);
2890 if (error)
2891 return NULL;
2893 if (buf_len(diffbuf) > 0) {
2894 error = buf_putc(diffbuf, '\0');
2895 colorized_line = strdup(buf_get(diffbuf));
2898 free(diffbuf);
2899 free(div_diff_line_div);
2900 return colorized_line;
2903 static char *
2904 gw_html_escape(const char *html)
2906 char *escaped_str = NULL, *buf;
2907 char c[1];
2908 size_t sz, i, buff_sz = 2048;
2910 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2911 return NULL;
2913 if (html == NULL)
2914 return NULL;
2915 else
2916 if ((sz = strlen(html)) == 0)
2917 return NULL;
2919 /* only work with buff_sz */
2920 if (buff_sz < sz)
2921 sz = buff_sz;
2923 for (i = 0; i < sz; i++) {
2924 c[0] = html[i];
2925 switch (c[0]) {
2926 case ('>'):
2927 strcat(buf, "&gt;");
2928 break;
2929 case ('&'):
2930 strcat(buf, "&amp;");
2931 break;
2932 case ('<'):
2933 strcat(buf, "&lt;");
2934 break;
2935 case ('"'):
2936 strcat(buf, "&quot;");
2937 break;
2938 case ('\''):
2939 strcat(buf, "&apos;");
2940 break;
2941 case ('\n'):
2942 strcat(buf, "<br />");
2943 default:
2944 strcat(buf, &c[0]);
2945 break;
2948 asprintf(&escaped_str, "%s", buf);
2949 free(buf);
2950 return escaped_str;
2953 int
2954 main()
2956 const struct got_error *error = NULL;
2957 struct trans *gw_trans;
2958 struct gw_dir *dir = NULL, *tdir;
2959 const char *page = "index";
2960 int gw_malloc = 1;
2962 if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
2963 errx(1, "malloc");
2965 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2966 errx(1, "malloc");
2968 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2969 errx(1, "malloc");
2971 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2972 errx(1, "malloc");
2974 if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX,
2975 &page, 1, 0))
2976 errx(1, "khttp_parse");
2978 if ((gw_trans->gw_conf =
2979 malloc(sizeof(struct gotweb_conf))) == NULL) {
2980 gw_malloc = 0;
2981 error = got_error_from_errno("malloc");
2982 goto err;
2985 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
2986 NULL) == -1) {
2987 error = got_error_from_errno("pledge");
2988 goto err;
2991 TAILQ_INIT(&gw_trans->gw_dirs);
2993 gw_trans->page = 0;
2994 gw_trans->repos_total = 0;
2995 gw_trans->repo_path = NULL;
2996 gw_trans->commit = NULL;
2997 gw_trans->headref = strdup(GOT_REF_HEAD);
2998 gw_trans->mime = KMIME_TEXT_HTML;
2999 gw_trans->gw_tmpl->key = templs;
3000 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3001 gw_trans->gw_tmpl->arg = gw_trans;
3002 gw_trans->gw_tmpl->cb = gw_template;
3003 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3005 err:
3006 if (error) {
3007 gw_trans->mime = KMIME_TEXT_PLAIN;
3008 gw_trans->action = GW_ERR;
3009 gw_display_index(gw_trans, error);
3010 goto done;
3013 error = gw_parse_querystring(gw_trans);
3014 if (error)
3015 goto err;
3017 gw_display_index(gw_trans, error);
3019 done:
3020 if (gw_malloc) {
3021 free(gw_trans->gw_conf->got_repos_path);
3022 free(gw_trans->gw_conf->got_www_path);
3023 free(gw_trans->gw_conf->got_site_name);
3024 free(gw_trans->gw_conf->got_site_owner);
3025 free(gw_trans->gw_conf->got_site_link);
3026 free(gw_trans->gw_conf->got_logo);
3027 free(gw_trans->gw_conf->got_logo_url);
3028 free(gw_trans->gw_conf);
3029 free(gw_trans->commit);
3030 free(gw_trans->repo_path);
3031 free(gw_trans->repo_name);
3032 free(gw_trans->repo_file);
3033 free(gw_trans->action_name);
3034 free(gw_trans->headref);
3036 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3037 free(dir->name);
3038 free(dir->description);
3039 free(dir->age);
3040 free(dir->url);
3041 free(dir->path);
3042 free(dir);
3047 khttp_free(gw_trans->gw_req);
3048 return EXIT_SUCCESS;