Blob


1 /*
2 * Copyright (c) 2020-2022 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/socket.h>
20 #include <sys/stat.h>
22 #include <event.h>
23 #include <imsg.h>
24 #include <sha1.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_reference.h"
33 #include "got_repository.h"
34 #include "got_path.h"
35 #include "got_cancel.h"
36 #include "got_diff.h"
37 #include "got_commit_graph.h"
38 #include "got_blame.h"
39 #include "got_privsep.h"
41 #include "proc.h"
42 #include "gotwebd.h"
44 static const struct got_error *got_init_repo_commit(struct repo_commit **);
45 static const struct got_error *got_init_repo_tag(struct repo_tag **);
46 static const struct got_error *got_get_repo_commit(struct request *,
47 struct repo_commit *, struct got_commit_object *, struct got_reflist_head *,
48 struct got_object_id *);
49 static const struct got_error *got_gotweb_dupfd(int *, int *);
50 static const struct got_error *got_gotweb_openfile(FILE **, int *, int *);
51 static const struct got_error *got_gotweb_blame_cb(void *, int, int,
52 struct got_commit_object *,struct got_object_id *);
54 const struct got_error *
55 got_gotweb_flushfile(FILE *f, int fd)
56 {
57 if (fseek(f, 0, SEEK_SET) == -1)
58 return got_error_from_errno("fseek");
60 if (ftruncate(fd, 0) == -1)
61 return got_error_from_errno("ftruncate");
63 if (fsync(fd) == -1)
64 return got_error_from_errno("fsync");
66 if (f && fclose(f) == EOF)
67 return got_error_from_errno("fclose");
69 if (fd != -1 && close(fd) != -1)
70 return got_error_from_errno("close");
72 return NULL;
73 }
75 static const struct got_error *
76 got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
77 {
78 *fd = dup(*priv_fd);
79 if (*fd == -1)
80 return got_error_from_errno("dup");
82 *f = fdopen(*fd, "w+");
83 if (*f == NULL) {
84 close(*fd);
85 return got_error(GOT_ERR_PRIVSEP_NO_FD);
86 }
88 return NULL;
89 }
91 static const struct got_error *
92 got_gotweb_dupfd(int *priv_fd, int *fd)
93 {
94 *fd = dup(*priv_fd);
96 if (*fd < 0)
97 return NULL;
99 return NULL;
102 const struct got_error *
103 got_get_repo_owner(char **owner, struct request *c)
105 struct server *srv = c->srv;
106 struct transport *t = c->t;
107 struct got_repository *repo = t->repo;
108 const char *gitconfig_owner;
110 *owner = NULL;
112 if (srv->show_repo_owner == 0)
113 return NULL;
115 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
116 if (gitconfig_owner) {
117 *owner = strdup(gitconfig_owner);
118 if (*owner == NULL)
119 return got_error_from_errno("strdup");
120 } else {
121 *owner = strdup("");
122 if (*owner == NULL)
123 return got_error_from_errno("strdup");
125 return NULL;
128 const struct got_error *
129 got_get_repo_age(char **repo_age, struct request *c,
130 const char *refname, int ref_tm)
132 const struct got_error *error = NULL;
133 struct server *srv = c->srv;
134 struct transport *t = c->t;
135 struct got_repository *repo = t->repo;
136 struct got_commit_object *commit = NULL;
137 struct got_reflist_head refs;
138 struct got_reflist_entry *re;
139 time_t committer_time = 0, cmp_time = 0;
141 *repo_age = NULL;
142 TAILQ_INIT(&refs);
144 if (srv->show_repo_age == 0)
145 return NULL;
147 error = got_ref_list(&refs, repo, "refs/heads",
148 got_ref_cmp_by_name, NULL);
149 if (error)
150 goto done;
152 /*
153 * Find the youngest branch tip in the repository, or the age of
154 * the a specific branch tip if a name was provided by the caller.
155 */
156 TAILQ_FOREACH(re, &refs, entry) {
157 struct got_object_id *id = NULL;
159 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
160 continue;
162 error = got_ref_resolve(&id, repo, re->ref);
163 if (error)
164 goto done;
166 error = got_object_open_as_commit(&commit, repo, id);
167 free(id);
168 if (error)
169 goto done;
171 committer_time =
172 got_object_commit_get_committer_time(commit);
173 got_object_commit_close(commit);
174 if (cmp_time < committer_time)
175 cmp_time = committer_time;
177 if (refname)
178 break;
181 if (cmp_time != 0) {
182 committer_time = cmp_time;
183 error = gotweb_get_time_str(repo_age, committer_time, ref_tm);
185 done:
186 got_ref_list_free(&refs);
187 return error;
190 static const struct got_error *
191 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
192 struct got_commit_object *commit, struct got_reflist_head *refs,
193 struct got_object_id *id)
195 const struct got_error *error = NULL;
196 struct got_reflist_entry *re;
197 struct got_object_id *id2 = NULL;
198 struct got_object_qid *parent_id;
199 struct transport *t = c->t;
200 struct querystring *qs = c->t->qs;
201 char *commit_msg = NULL, *commit_msg0;
203 TAILQ_FOREACH(re, refs, entry) {
204 char *s;
205 const char *name;
206 struct got_tag_object *tag = NULL;
207 struct got_object_id *ref_id;
208 int cmp;
210 if (got_ref_is_symbolic(re->ref))
211 continue;
213 name = got_ref_get_name(re->ref);
214 if (strncmp(name, "refs/", 5) == 0)
215 name += 5;
216 if (strncmp(name, "got/", 4) == 0)
217 continue;
218 if (strncmp(name, "heads/", 6) == 0)
219 name += 6;
220 if (strncmp(name, "remotes/", 8) == 0) {
221 name += 8;
222 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
223 continue;
225 error = got_ref_resolve(&ref_id, t->repo, re->ref);
226 if (error)
227 return error;
228 if (strncmp(name, "tags/", 5) == 0) {
229 error = got_object_open_as_tag(&tag, t->repo, ref_id);
230 if (error) {
231 if (error->code != GOT_ERR_OBJ_TYPE) {
232 free(ref_id);
233 continue;
235 /*
236 * Ref points at something other
237 * than a tag.
238 */
239 error = NULL;
240 tag = NULL;
243 cmp = got_object_id_cmp(tag ?
244 got_object_tag_get_object_id(tag) : ref_id, id);
245 free(ref_id);
246 if (tag)
247 got_object_tag_close(tag);
248 if (cmp != 0)
249 continue;
250 s = repo_commit->refs_str;
251 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
252 s ? ", " : "", name) == -1) {
253 error = got_error_from_errno("asprintf");
254 free(s);
255 repo_commit->refs_str = NULL;
256 return error;
258 free(s);
261 error = got_object_id_str(&repo_commit->commit_id, id);
262 if (error)
263 return error;
265 error = got_object_id_str(&repo_commit->tree_id,
266 got_object_commit_get_tree_id(commit));
267 if (error)
268 return error;
270 if (qs->action == DIFF) {
271 parent_id = STAILQ_FIRST(
272 got_object_commit_get_parent_ids(commit));
273 if (parent_id != NULL) {
274 id2 = got_object_id_dup(&parent_id->id);
275 error = got_object_id_str(&repo_commit->parent_id, id2);
276 if (error)
277 return error;
278 free(id2);
279 } else {
280 repo_commit->parent_id = strdup("/dev/null");
281 if (repo_commit->parent_id == NULL) {
282 error = got_error_from_errno("strdup");
283 return error;
288 repo_commit->committer_time =
289 got_object_commit_get_committer_time(commit);
291 repo_commit->author =
292 strdup(got_object_commit_get_author(commit));
293 if (repo_commit->author == NULL) {
294 error = got_error_from_errno("strdup");
295 return error;
297 repo_commit->committer =
298 strdup(got_object_commit_get_committer(commit));
299 if (repo_commit->committer == NULL) {
300 error = got_error_from_errno("strdup");
301 return error;
303 error = got_object_commit_get_logmsg(&commit_msg0, commit);
304 if (error)
305 return error;
307 commit_msg = commit_msg0;
308 while (*commit_msg == '\n')
309 commit_msg++;
311 repo_commit->commit_msg = strdup(commit_msg);
312 if (repo_commit->commit_msg == NULL)
313 error = got_error_from_errno("strdup");
314 free(commit_msg0);
315 return error;
318 const struct got_error *
319 got_get_repo_commits(struct request *c, int limit)
321 const struct got_error *error = NULL;
322 struct got_object_id *id = NULL;
323 struct got_commit_graph *graph = NULL;
324 struct got_commit_object *commit = NULL;
325 struct got_reflist_head refs;
326 struct got_reference *ref = NULL;
327 struct repo_commit *repo_commit = NULL;
328 struct server *srv = c->srv;
329 struct transport *t = c->t;
330 struct got_repository *repo = t->repo;
331 struct querystring *qs = t->qs;
332 struct repo_dir *repo_dir = t->repo_dir;
333 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
334 int chk_next = 0, chk_multi = 0, commit_found = 0;
335 int obj_type, limit_chk = 0;
337 TAILQ_INIT(&refs);
339 if (qs->file != NULL && strlen(qs->file) > 0)
340 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
341 qs->file) == -1)
342 return got_error_from_errno("asprintf");
344 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
345 repo_dir->name) == -1) {
346 error = got_error_from_errno("asprintf");
347 goto done;
350 /*
351 * XXX: jumping directly to a commit id via
352 * got_repo_match_object_id_prefix significantly improves performance,
353 * but does not allow us to create a PREVIOUS button, since commits can
354 * only be itereated forward. So, we have to match as we iterate from
355 * the headref.
356 */
357 if (qs->action == BRIEFS || qs->action == COMMITS ||
358 (qs->action == TREE && qs->commit == NULL)) {
359 error = got_ref_open(&ref, repo, qs->headref, 0);
360 if (error)
361 goto done;
363 error = got_ref_resolve(&id, repo, ref);
364 if (error)
365 goto done;
366 } else if (qs->commit != NULL) {
367 error = got_ref_open(&ref, repo, qs->commit, 0);
368 if (error == NULL) {
369 error = got_ref_resolve(&id, repo, ref);
370 if (error)
371 goto done;
372 error = got_object_get_type(&obj_type, repo, id);
373 if (error)
374 goto done;
375 if (obj_type == GOT_OBJ_TYPE_TAG) {
376 struct got_tag_object *tag;
377 error = got_object_open_as_tag(&tag, repo, id);
378 if (error)
379 goto done;
380 if (got_object_tag_get_object_type(tag) !=
381 GOT_OBJ_TYPE_COMMIT) {
382 got_object_tag_close(tag);
383 error = got_error(GOT_ERR_OBJ_TYPE);
384 goto done;
386 free(id);
387 id = got_object_id_dup(
388 got_object_tag_get_object_id(tag));
389 if (id == NULL)
390 error = got_error_from_errno(
391 "got_object_id_dup");
392 got_object_tag_close(tag);
393 if (error)
394 goto done;
395 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
396 error = got_error(GOT_ERR_OBJ_TYPE);
397 goto done;
400 error = got_repo_match_object_id_prefix(&id, qs->commit,
401 GOT_OBJ_TYPE_COMMIT, repo);
402 if (error)
403 goto done;
406 error = got_repo_map_path(&in_repo_path, repo, repo_path);
407 if (error)
408 goto done;
410 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
411 if (error)
412 goto done;
414 if (qs->file != NULL && strlen(qs->file) > 0) {
415 error = got_commit_graph_open(&graph, file_path, 0);
416 if (error)
417 goto done;
418 } else {
419 error = got_commit_graph_open(&graph, in_repo_path, 0);
420 if (error)
421 goto done;
424 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
425 if (error)
426 goto done;
428 for (;;) {
429 struct got_object_id next_id;
431 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
432 NULL);
433 if (error) {
434 if (error->code == GOT_ERR_ITER_COMPLETED)
435 error = NULL;
436 goto done;
439 error = got_object_open_as_commit(&commit, repo, &next_id);
440 if (error)
441 goto done;
443 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
444 NULL);
445 if (error)
446 goto done;
448 error = got_init_repo_commit(&repo_commit);
449 if (error)
450 goto done;
452 error = got_get_repo_commit(c, repo_commit, commit,
453 &refs, &next_id);
454 if (error) {
455 gotweb_free_repo_commit(repo_commit);
456 goto done;
459 if (limit_chk == ((limit * qs->page) - limit) &&
460 commit_found == 0 && repo_commit->commit_id != NULL) {
461 t->prev_id = strdup(repo_commit->commit_id);
462 if (t->prev_id == NULL) {
463 error = got_error_from_errno("strdup");
464 gotweb_free_repo_commit(repo_commit);
465 goto done;
469 if (qs->commit != NULL && commit_found == 0 && limit != 1) {
470 if (strcmp(qs->commit, repo_commit->commit_id) == 0)
471 commit_found = 1;
472 else if (qs->file != NULL && strlen(qs->file) > 0 &&
473 qs->page == 0)
474 commit_found = 1;
475 else {
476 gotweb_free_repo_commit(repo_commit);
477 limit_chk++;
478 continue;
482 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
484 if (limit == 1 && chk_multi == 0 &&
485 srv->max_commits_display != 1)
486 commit_found = 1;
487 else {
488 chk_multi = 1;
490 /*
491 * check for one more commit before breaking,
492 * so we know whether to navigate through briefs
493 * commits and summary
494 */
495 if (chk_next && (qs->action == BRIEFS ||
496 qs->action == COMMITS || qs->action == SUMMARY)) {
497 t->next_id = strdup(repo_commit->commit_id);
498 if (t->next_id == NULL) {
499 error = got_error_from_errno("strdup");
500 goto done;
502 if (commit) {
503 got_object_commit_close(commit);
504 commit = NULL;
506 TAILQ_REMOVE(&t->repo_commits, repo_commit,
507 entry);
508 gotweb_free_repo_commit(repo_commit);
509 goto done;
512 got_ref_list_free(&refs);
513 if (error || (limit && --limit == 0)) {
514 if (commit_found || (qs->file != NULL &&
515 strlen(qs->file) > 0))
516 if (chk_multi == 0)
517 break;
518 chk_next = 1;
520 if (commit) {
521 got_object_commit_close(commit);
522 commit = NULL;
525 done:
526 if (ref)
527 got_ref_close(ref);
528 if (commit)
529 got_object_commit_close(commit);
530 if (graph)
531 got_commit_graph_close(graph);
532 got_ref_list_free(&refs);
533 free(in_repo_path);
534 free(file_path);
535 free(repo_path);
536 free(id);
537 return error;
540 const struct got_error *
541 got_get_repo_tags(struct request *c, int limit)
543 const struct got_error *error = NULL;
544 struct got_object_id *id = NULL;
545 struct got_commit_object *commit = NULL;
546 struct got_reflist_head refs;
547 struct got_reference *ref;
548 struct got_reflist_entry *re;
549 struct server *srv = c->srv;
550 struct transport *t = c->t;
551 struct got_repository *repo = t->repo;
552 struct querystring *qs = t->qs;
553 struct repo_dir *repo_dir = t->repo_dir;
554 struct got_tag_object *tag = NULL;
555 struct repo_tag *rt = NULL, *trt = NULL;
556 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
557 char *tag_commit = NULL, *tag_commit0 = NULL;
558 char *commit_msg = NULL, *commit_msg0 = NULL;
559 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
561 TAILQ_INIT(&refs);
563 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
564 repo_dir->name) == -1)
565 return got_error_from_errno("asprintf");
567 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
568 error = got_ref_open(&ref, repo, qs->headref, 0);
569 if (error)
570 goto err;
571 error = got_ref_resolve(&id, repo, ref);
572 got_ref_close(ref);
573 if (error)
574 goto err;
575 } else if (qs->commit == NULL && qs->action == TAG) {
576 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
577 goto err;
578 } else {
579 error = got_repo_match_object_id_prefix(&id, qs->commit,
580 GOT_OBJ_TYPE_COMMIT, repo);
581 if (error)
582 goto err;
585 if (qs->action != SUMMARY && qs->action != TAGS) {
586 error = got_object_open_as_commit(&commit, repo, id);
587 if (error)
588 goto err;
589 error = got_object_commit_get_logmsg(&commit_msg0, commit);
590 if (error)
591 goto err;
592 if (commit) {
593 got_object_commit_close(commit);
594 commit = NULL;
598 error = got_repo_map_path(&in_repo_path, repo, repo_path);
599 if (error)
600 goto err;
602 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
603 repo);
604 if (error)
605 goto err;
607 if (limit == 1)
608 chk_multi = 0;
610 /*
611 * XXX: again, see previous message about caching
612 */
614 TAILQ_FOREACH(re, &refs, entry) {
615 struct repo_tag *new_repo_tag = NULL;
616 error = got_init_repo_tag(&new_repo_tag);
617 if (error)
618 goto err;
620 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
622 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
623 if (new_repo_tag->tag_name == NULL) {
624 error = got_error_from_errno("strdup");
625 goto err;
628 free(id);
629 id = NULL;
631 free(id_str);
632 id_str = NULL;
634 error = got_ref_resolve(&id, repo, re->ref);
635 if (error)
636 goto done;
638 if (tag)
639 got_object_tag_close(tag);
640 error = got_object_open_as_tag(&tag, repo, id);
641 if (error) {
642 if (error->code != GOT_ERR_OBJ_TYPE)
643 goto done;
644 /* "lightweight" tag */
645 error = got_object_open_as_commit(&commit, repo, id);
646 if (error)
647 goto done;
648 new_repo_tag->tagger =
649 strdup(got_object_commit_get_committer(commit));
650 if (new_repo_tag->tagger == NULL) {
651 error = got_error_from_errno("strdup");
652 goto err;
654 new_repo_tag->tagger_time =
655 got_object_commit_get_committer_time(commit);
656 error = got_object_id_str(&id_str, id);
657 if (error)
658 goto err;
659 } else {
660 new_repo_tag->tagger =
661 strdup(got_object_tag_get_tagger(tag));
662 if (new_repo_tag->tagger == NULL) {
663 error = got_error_from_errno("strdup");
664 goto err;
666 new_repo_tag->tagger_time =
667 got_object_tag_get_tagger_time(tag);
668 error = got_object_id_str(&id_str,
669 got_object_tag_get_object_id(tag));
670 if (error)
671 goto err;
674 new_repo_tag->commit_id = strdup(id_str);
675 if (new_repo_tag->commit_id == NULL)
676 goto err;
678 if (commit_found == 0 && qs->commit != NULL &&
679 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
680 continue;
681 else
682 commit_found = 1;
684 t->tag_count++;
686 /*
687 * check for one more commit before breaking,
688 * so we know whether to navigate through briefs
689 * commits and summary
690 */
691 if (chk_next) {
692 t->next_id = strdup(new_repo_tag->commit_id);
693 if (t->next_id == NULL) {
694 error = got_error_from_errno("strdup");
695 goto err;
697 if (commit) {
698 got_object_commit_close(commit);
699 commit = NULL;
701 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
702 gotweb_free_repo_tag(new_repo_tag);
703 goto done;
706 if (commit) {
707 error = got_object_commit_get_logmsg(&tag_commit0,
708 commit);
709 if (error)
710 goto err;
711 got_object_commit_close(commit);
712 commit = NULL;
713 } else {
714 tag_commit0 = strdup(got_object_tag_get_message(tag));
715 if (tag_commit0 == NULL) {
716 error = got_error_from_errno("strdup");
717 goto err;
721 tag_commit = tag_commit0;
722 while (*tag_commit == '\n')
723 tag_commit++;
724 new_repo_tag->tag_commit = strdup(tag_commit);
725 if (new_repo_tag->tag_commit == NULL) {
726 error = got_error_from_errno("strdup");
727 free(tag_commit0);
728 goto err;
730 free(tag_commit0);
732 if (qs->action != SUMMARY && qs->action != TAGS) {
733 commit_msg = commit_msg0;
734 while (*commit_msg == '\n')
735 commit_msg++;
737 new_repo_tag->commit_msg = strdup(commit_msg);
738 if (new_repo_tag->commit_msg == NULL) {
739 error = got_error_from_errno("strdup");
740 goto err;
744 if (limit && --limit == 0) {
745 if (chk_multi == 0)
746 break;
747 chk_next = 1;
751 done:
752 /*
753 * we have tailq populated, so find previous commit id
754 * for navigation through briefs and commits
755 */
756 if (t->tag_count == 0) {
757 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
758 TAILQ_REMOVE(&t->repo_tags, rt, entry);
759 gotweb_free_repo_tag(rt);
762 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
763 commit_found = 0;
764 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
765 entry) {
766 if (commit_found == 0 && rt->commit_id != NULL &&
767 strcmp(qs->commit, rt->commit_id) != 0) {
768 continue;
769 } else
770 commit_found = 1;
771 if (c_cnt == srv->max_commits_display ||
772 rt == TAILQ_FIRST(&t->repo_tags)) {
773 t->prev_id = strdup(rt->commit_id);
774 if (t->prev_id == NULL)
775 error = got_error_from_errno("strdup");
776 break;
778 c_cnt++;
781 err:
782 if (commit)
783 got_object_commit_close(commit);
784 if (tag)
785 got_object_tag_close(tag);
786 got_ref_list_free(&refs);
787 free(commit_msg0);
788 free(in_repo_path);
789 free(repo_path);
790 free(id);
791 free(id_str);
792 return error;
795 int
796 got_output_repo_tree(struct request *c,
797 int (*cb)(struct template *, struct got_tree_entry *))
799 const struct got_error *error = NULL;
800 struct transport *t = c->t;
801 struct got_commit_object *commit = NULL;
802 struct got_repository *repo = t->repo;
803 struct querystring *qs = t->qs;
804 struct repo_commit *rc = NULL;
805 struct got_object_id *tree_id = NULL, *commit_id = NULL;
806 struct got_reflist_head refs;
807 struct got_tree_object *tree = NULL;
808 struct got_tree_entry *te;
809 struct repo_dir *repo_dir = t->repo_dir;
810 char *escaped_name = NULL, *path = NULL;
811 int nentries, i;
813 TAILQ_INIT(&refs);
815 rc = TAILQ_FIRST(&t->repo_commits);
817 if (qs->folder != NULL) {
818 path = strdup(qs->folder);
819 if (path == NULL) {
820 error = got_error_from_errno("strdup");
821 goto done;
823 } else {
824 error = got_repo_map_path(&path, repo, repo_dir->path);
825 if (error)
826 goto done;
829 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
830 GOT_OBJ_TYPE_COMMIT, &refs, repo);
831 if (error)
832 goto done;
834 error = got_object_open_as_commit(&commit, repo, commit_id);
835 if (error)
836 goto done;
838 error = got_object_id_by_path(&tree_id, repo, commit, path);
839 if (error)
840 goto done;
842 error = got_object_open_as_tree(&tree, repo, tree_id);
843 if (error)
844 goto done;
846 nentries = got_object_tree_get_nentries(tree);
848 for (i = 0; i < nentries; i++) {
849 te = got_object_tree_get_entry(tree, i);
850 if (cb(c->tp, te) == -1)
851 break;
853 done:
854 free(escaped_name);
855 free(path);
856 got_ref_list_free(&refs);
857 if (commit)
858 got_object_commit_close(commit);
859 if (tree)
860 got_object_tree_close(tree);
861 free(commit_id);
862 free(tree_id);
863 if (error) {
864 log_warnx("%s: %s", __func__, error->msg);
865 return -1;
867 return 0;
870 const struct got_error *
871 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
872 int *binary, struct request *c)
874 const struct got_error *error = NULL;
875 struct transport *t = c->t;
876 struct got_repository *repo = t->repo;
877 struct querystring *qs = c->t->qs;
878 struct got_commit_object *commit = NULL;
879 struct got_object_id *commit_id = NULL;
880 struct got_reflist_head refs;
881 char *path = NULL, *in_repo_path = NULL;
882 int obj_type;
884 TAILQ_INIT(&refs);
886 *blob = NULL;
887 *fd = -1;
888 *binary = 0;
890 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
891 qs->folder ? "/" : "", qs->file) == -1) {
892 error = got_error_from_errno("asprintf");
893 goto done;
896 error = got_repo_map_path(&in_repo_path, repo, path);
897 if (error)
898 goto done;
900 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
901 GOT_OBJ_TYPE_COMMIT, &refs, repo);
902 if (error)
903 goto done;
905 error = got_object_open_as_commit(&commit, repo, commit_id);
906 if (error)
907 goto done;
909 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
910 if (error)
911 goto done;
913 if (commit_id == NULL) {
914 error = got_error(GOT_ERR_NO_OBJ);
915 goto done;
918 error = got_object_get_type(&obj_type, repo, commit_id);
919 if (error)
920 goto done;
922 if (obj_type != GOT_OBJ_TYPE_BLOB) {
923 error = got_error(GOT_ERR_OBJ_TYPE);
924 goto done;
927 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
928 if (error)
929 goto done;
931 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
932 if (error)
933 goto done;
935 error = got_object_blob_is_binary(binary, *blob);
936 if (error)
937 goto done;
939 done:
940 if (commit)
941 got_object_commit_close(commit);
943 if (error) {
944 if (*fd != -1)
945 close(*fd);
946 if (*blob)
947 got_object_blob_close(*blob);
948 *fd = -1;
949 *blob = NULL;
952 free(in_repo_path);
953 free(commit_id);
954 free(path);
955 return error;
958 const struct got_error *
959 got_output_file_blob(struct request *c)
961 const struct got_error *error = NULL;
962 struct querystring *qs = c->t->qs;
963 struct got_blob_object *blob = NULL;
964 size_t len;
965 int binary, fd = -1;
966 const uint8_t *buf;
968 error = got_open_blob_for_output(&blob, &fd, &binary, c);
969 if (error)
970 return error;
972 if (binary)
973 error = gotweb_render_content_type_file(c,
974 "application/octet-stream", qs->file, NULL);
975 else
976 error = gotweb_render_content_type(c, "text/plain");
978 if (error) {
979 log_warnx("%s: %s", __func__, error->msg);
980 goto done;
983 for (;;) {
984 error = got_object_blob_read_block(&len, blob);
985 if (error)
986 goto done;
987 if (len == 0)
988 break;
989 buf = got_object_blob_get_read_buf(blob);
990 fcgi_gen_binary_response(c, buf, len);
992 done:
993 if (close(fd) == -1 && error == NULL)
994 error = got_error_from_errno("close");
995 if (blob)
996 got_object_blob_close(blob);
997 return error;
1000 int
1001 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
1002 int (*cb)(struct template *, const char *, size_t))
1004 const struct got_error *err;
1005 char *line = NULL;
1006 size_t linesize = 0;
1007 size_t lineno = 0;
1008 ssize_t linelen = 0;
1010 for (;;) {
1011 err = got_object_blob_getline(&line, &linelen, &linesize,
1012 blob);
1013 if (err || linelen == -1)
1014 break;
1015 lineno++;
1016 if (cb(tp, line, lineno) == -1)
1017 break;
1020 free(line);
1022 if (err) {
1023 log_warnx("%s: got_object_blob_getline failed: %s",
1024 __func__, err->msg);
1025 return -1;
1027 return 0;
1030 struct blame_line {
1031 int annotated;
1032 char *id_str;
1033 char *committer;
1034 char datebuf[11]; /* YYYY-MM-DD + NUL */
1037 struct blame_cb_args {
1038 struct blame_line *lines;
1039 int nlines;
1040 int nlines_prec;
1041 int lineno_cur;
1042 off_t *line_offsets;
1043 FILE *f;
1044 struct got_repository *repo;
1045 struct request *c;
1048 static const struct got_error *
1049 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1050 struct got_commit_object *commit, struct got_object_id *id)
1052 const struct got_error *err = NULL;
1053 struct blame_cb_args *a = arg;
1054 struct blame_line *bline;
1055 struct request *c = a->c;
1056 struct transport *t = c->t;
1057 struct repo_dir *repo_dir = t->repo_dir;
1058 char *line = NULL, *eline = NULL;
1059 size_t linesize = 0;
1060 off_t offset;
1061 struct tm tm;
1062 time_t committer_time;
1063 int r;
1065 if (nlines != a->nlines ||
1066 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1067 return got_error(GOT_ERR_RANGE);
1069 if (lineno == -1)
1070 return NULL; /* no change in this commit */
1072 /* Annotate this line. */
1073 bline = &a->lines[lineno - 1];
1074 if (bline->annotated)
1075 return NULL;
1076 err = got_object_id_str(&bline->id_str, id);
1077 if (err)
1078 return err;
1080 bline->committer = strdup(got_object_commit_get_committer(commit));
1081 if (bline->committer == NULL) {
1082 err = got_error_from_errno("strdup");
1083 goto done;
1086 committer_time = got_object_commit_get_committer_time(commit);
1087 if (gmtime_r(&committer_time, &tm) == NULL)
1088 return got_error_from_errno("gmtime_r");
1089 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1090 &tm) == 0) {
1091 err = got_error(GOT_ERR_NO_SPACE);
1092 goto done;
1094 bline->annotated = 1;
1096 /* Print lines annotated so far. */
1097 bline = &a->lines[a->lineno_cur - 1];
1098 if (!bline->annotated)
1099 goto done;
1101 offset = a->line_offsets[a->lineno_cur - 1];
1102 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1103 err = got_error_from_errno("fseeko");
1104 goto done;
1107 while (a->lineno_cur <= a->nlines && bline->annotated) {
1108 char *smallerthan, *at, *nl, *committer;
1109 size_t len;
1111 if (getline(&line, &linesize, a->f) == -1) {
1112 if (ferror(a->f))
1113 err = got_error_from_errno("getline");
1114 break;
1117 committer = bline->committer;
1118 smallerthan = strchr(committer, '<');
1119 if (smallerthan && smallerthan[1] != '\0')
1120 committer = smallerthan + 1;
1121 at = strchr(committer, '@');
1122 if (at)
1123 *at = '\0';
1124 len = strlen(committer);
1125 if (len >= 9)
1126 committer[8] = '\0';
1128 nl = strchr(line, '\n');
1129 if (nl)
1130 *nl = '\0';
1132 err = gotweb_escape_html(&eline, line);
1133 if (err)
1134 goto done;
1136 if (fcgi_printf(c, "<div class='blame_wrapper'>"
1137 "<div class='blame_number'>%.*d</div>"
1138 "<div class='blame_hash'>",
1139 a->nlines_prec, a->lineno_cur) == -1)
1140 goto done;
1142 r = gotweb_link(c, &(struct gotweb_url){
1143 .action = DIFF,
1144 .index_page = -1,
1145 .page = -1,
1146 .path = repo_dir->name,
1147 .commit = bline->id_str,
1148 }, "%.8s", bline->id_str);
1149 if (r == -1)
1150 goto done;
1152 r = fcgi_printf(c,
1153 "</div>"
1154 "<div class='blame_date'>%s</div>"
1155 "<div class='blame_author'>%s</div>"
1156 "<div class='blame_code'>%s</div>"
1157 "</div>", /* .blame_wrapper */
1158 bline->datebuf,
1159 committer,
1160 eline);
1161 if (r == -1)
1162 goto done;
1164 a->lineno_cur++;
1165 bline = &a->lines[a->lineno_cur - 1];
1167 free(eline);
1168 eline = NULL;
1170 done:
1171 free(line);
1172 free(eline);
1173 return err;
1176 const struct got_error *
1177 got_output_file_blame(struct request *c)
1179 const struct got_error *error = NULL;
1180 struct transport *t = c->t;
1181 struct got_repository *repo = t->repo;
1182 struct querystring *qs = c->t->qs;
1183 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1184 struct got_commit_object *commit = NULL;
1185 struct got_reflist_head refs;
1186 struct got_blob_object *blob = NULL;
1187 char *path = NULL, *in_repo_path = NULL;
1188 struct blame_cb_args bca;
1189 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1190 int fd6 = -1;
1191 off_t filesize;
1192 FILE *f1 = NULL, *f2 = NULL;
1194 TAILQ_INIT(&refs);
1195 bca.f = NULL;
1196 bca.lines = NULL;
1198 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1199 qs->folder ? "/" : "", qs->file) == -1) {
1200 error = got_error_from_errno("asprintf");
1201 goto done;
1204 error = got_repo_map_path(&in_repo_path, repo, path);
1205 if (error)
1206 goto done;
1208 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1209 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1210 if (error)
1211 goto done;
1213 error = got_object_open_as_commit(&commit, repo, commit_id);
1214 if (error)
1215 goto done;
1217 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1218 if (error)
1219 goto done;
1221 error = got_object_get_type(&obj_type, repo, obj_id);
1222 if (error)
1223 goto done;
1225 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1226 error = got_error(GOT_ERR_OBJ_TYPE);
1227 goto done;
1230 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1231 if (error)
1232 goto done;
1234 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1235 if (error)
1236 goto done;
1238 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1239 if (error)
1240 goto done;
1242 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1243 &bca.line_offsets, bca.f, blob);
1244 if (error || bca.nlines == 0)
1245 goto done;
1247 /* Don't include \n at EOF in the blame line count. */
1248 if (bca.line_offsets[bca.nlines - 1] == filesize)
1249 bca.nlines--;
1251 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1252 if (bca.lines == NULL) {
1253 error = got_error_from_errno("calloc");
1254 goto done;
1256 bca.lineno_cur = 1;
1257 bca.nlines_prec = 0;
1258 i = bca.nlines;
1259 while (i > 0) {
1260 i /= 10;
1261 bca.nlines_prec++;
1263 bca.repo = repo;
1264 bca.c = c;
1266 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1267 if (error)
1268 goto done;
1270 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1271 if (error)
1272 goto done;
1274 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1275 if (error)
1276 goto done;
1278 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1279 if (error)
1280 goto done;
1282 error = got_blame(in_repo_path, commit_id, repo,
1283 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1284 fd3, fd4, f1, f2);
1286 done:
1287 if (bca.lines) {
1288 free(bca.line_offsets);
1289 for (i = 0; i < bca.nlines; i++) {
1290 struct blame_line *bline = &bca.lines[i];
1291 free(bline->id_str);
1292 free(bline->committer);
1295 free(bca.lines);
1296 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1297 error = got_error_from_errno("close");
1298 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1299 error = got_error_from_errno("close");
1300 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1301 error = got_error_from_errno("close");
1302 if (bca.f) {
1303 const struct got_error *bca_err =
1304 got_gotweb_flushfile(bca.f, fd1);
1305 if (error == NULL)
1306 error = bca_err;
1308 if (f1) {
1309 const struct got_error *f1_err =
1310 got_gotweb_flushfile(f1, fd5);
1311 if (error == NULL)
1312 error = f1_err;
1314 if (f2) {
1315 const struct got_error *f2_err =
1316 got_gotweb_flushfile(f2, fd6);
1317 if (error == NULL)
1318 error = f2_err;
1320 if (commit)
1321 got_object_commit_close(commit);
1322 if (blob)
1323 got_object_blob_close(blob);
1324 free(in_repo_path);
1325 free(commit_id);
1326 free(obj_id);
1327 free(path);
1328 got_ref_list_free(&refs);
1329 return error;
1332 const struct got_error *
1333 got_open_diff_for_output(FILE **fp, int *fd, struct request *c)
1335 const struct got_error *error = NULL;
1336 struct transport *t = c->t;
1337 struct got_repository *repo = t->repo;
1338 struct repo_commit *rc = NULL;
1339 struct got_object_id *id1 = NULL, *id2 = NULL;
1340 struct got_reflist_head refs;
1341 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1342 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1344 *fp = NULL;
1345 *fd = -1;
1347 TAILQ_INIT(&refs);
1349 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1350 if (error)
1351 return error;
1353 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1354 if (error)
1355 return error;
1357 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1358 if (error)
1359 return error;
1361 rc = TAILQ_FIRST(&t->repo_commits);
1363 if (rc->parent_id != NULL &&
1364 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1365 error = got_repo_match_object_id(&id1, NULL,
1366 rc->parent_id, GOT_OBJ_TYPE_ANY,
1367 &refs, repo);
1368 if (error)
1369 goto done;
1372 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1373 GOT_OBJ_TYPE_ANY, &refs, repo);
1374 if (error)
1375 goto done;
1377 error = got_object_get_type(&obj_type, repo, id2);
1378 if (error)
1379 goto done;
1381 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1382 if (error)
1383 goto done;
1385 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1386 if (error)
1387 goto done;
1389 switch (obj_type) {
1390 case GOT_OBJ_TYPE_BLOB:
1391 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1392 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1393 0, NULL, repo, f3);
1394 break;
1395 case GOT_OBJ_TYPE_TREE:
1396 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1397 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1398 0, NULL, repo, f3);
1399 break;
1400 case GOT_OBJ_TYPE_COMMIT:
1401 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1402 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1403 0, NULL, repo, f3);
1404 break;
1405 default:
1406 error = got_error(GOT_ERR_OBJ_TYPE);
1408 if (error)
1409 goto done;
1411 if (fseek(f1, 0, SEEK_SET) == -1) {
1412 error = got_ferror(f1, GOT_ERR_IO);
1413 goto done;
1416 if (fseek(f2, 0, SEEK_SET) == -1) {
1417 error = got_ferror(f2, GOT_ERR_IO);
1418 goto done;
1421 if (fseek(f3, 0, SEEK_SET) == -1) {
1422 error = got_ferror(f3, GOT_ERR_IO);
1423 goto done;
1426 *fp = f3;
1427 *fd = fd3;
1429 done:
1430 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1431 error = got_error_from_errno("close");
1432 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1433 error = got_error_from_errno("close");
1434 if (f1) {
1435 const struct got_error *f1_err =
1436 got_gotweb_flushfile(f1, fd1);
1437 if (error == NULL)
1438 error = f1_err;
1440 if (f2) {
1441 const struct got_error *f2_err =
1442 got_gotweb_flushfile(f2, fd2);
1443 if (error == NULL)
1444 error = f2_err;
1446 if (error && f3) {
1447 got_gotweb_flushfile(f3, fd3);
1448 *fp = NULL;
1449 *fd = -1;
1451 got_ref_list_free(&refs);
1452 free(id1);
1453 free(id2);
1454 return error;
1457 static const struct got_error *
1458 got_init_repo_commit(struct repo_commit **rc)
1460 *rc = calloc(1, sizeof(**rc));
1461 if (*rc == NULL)
1462 return got_error_from_errno2("%s: calloc", __func__);
1464 (*rc)->path = NULL;
1465 (*rc)->refs_str = NULL;
1466 (*rc)->commit_id = NULL;
1467 (*rc)->committer = NULL;
1468 (*rc)->author = NULL;
1469 (*rc)->parent_id = NULL;
1470 (*rc)->tree_id = NULL;
1471 (*rc)->commit_msg = NULL;
1473 return NULL;
1476 static const struct got_error *
1477 got_init_repo_tag(struct repo_tag **rt)
1479 *rt = calloc(1, sizeof(**rt));
1480 if (*rt == NULL)
1481 return got_error_from_errno2("%s: calloc", __func__);
1483 (*rt)->commit_id = NULL;
1484 (*rt)->tag_name = NULL;
1485 (*rt)->tag_commit = NULL;
1486 (*rt)->commit_msg = NULL;
1487 (*rt)->tagger = NULL;
1489 return NULL;