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 <sha2.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_reference.h"
34 #include "got_repository.h"
35 #include "got_path.h"
36 #include "got_cancel.h"
37 #include "got_diff.h"
38 #include "got_commit_graph.h"
39 #include "got_blame.h"
40 #include "got_privsep.h"
42 #include "proc.h"
43 #include "gotwebd.h"
45 static const struct got_error *got_init_repo_commit(struct repo_commit **);
46 static const struct got_error *got_init_repo_tag(struct repo_tag **);
47 static const struct got_error *got_get_repo_commit(struct request *,
48 struct repo_commit *, struct got_commit_object *, struct got_reflist_head *,
49 struct got_object_id *);
50 static const struct got_error *got_gotweb_dupfd(int *, int *);
51 static const struct got_error *got_gotweb_openfile(FILE **, int *, int *);
52 static const struct got_error *got_gotweb_blame_cb(void *, int, int,
53 struct got_commit_object *,struct got_object_id *);
55 const struct got_error *
56 got_gotweb_flushfile(FILE *f, int fd)
57 {
58 if (fseek(f, 0, SEEK_SET) == -1)
59 return got_error_from_errno("fseek");
61 if (ftruncate(fd, 0) == -1)
62 return got_error_from_errno("ftruncate");
64 if (fsync(fd) == -1)
65 return got_error_from_errno("fsync");
67 if (f && fclose(f) == EOF)
68 return got_error_from_errno("fclose");
70 if (fd != -1 && close(fd) != -1)
71 return got_error_from_errno("close");
73 return NULL;
74 }
76 static const struct got_error *
77 got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
78 {
79 *fd = dup(*priv_fd);
80 if (*fd == -1)
81 return got_error_from_errno("dup");
83 *f = fdopen(*fd, "w+");
84 if (*f == NULL) {
85 close(*fd);
86 return got_error(GOT_ERR_PRIVSEP_NO_FD);
87 }
89 return NULL;
90 }
92 static const struct got_error *
93 got_gotweb_dupfd(int *priv_fd, int *fd)
94 {
95 *fd = dup(*priv_fd);
97 if (*fd == -1)
98 return got_error_from_errno("dup");
100 return NULL;
103 const struct got_error *
104 got_get_repo_owner(char **owner, struct request *c)
106 struct server *srv = c->srv;
107 struct transport *t = c->t;
108 struct got_repository *repo = t->repo;
109 const char *gitconfig_owner;
111 *owner = NULL;
113 if (srv->show_repo_owner == 0)
114 return NULL;
116 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
117 if (gitconfig_owner) {
118 *owner = strdup(gitconfig_owner);
119 if (*owner == NULL)
120 return got_error_from_errno("strdup");
121 } else {
122 *owner = strdup("");
123 if (*owner == NULL)
124 return got_error_from_errno("strdup");
126 return NULL;
129 const struct got_error *
130 got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
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 TAILQ_INIT(&refs);
143 if (srv->show_repo_age == 0)
144 return NULL;
146 error = got_ref_list(&refs, repo, "refs/heads",
147 got_ref_cmp_by_name, NULL);
148 if (error)
149 goto done;
151 /*
152 * Find the youngest branch tip in the repository, or the age of
153 * the a specific branch tip if a name was provided by the caller.
154 */
155 TAILQ_FOREACH(re, &refs, entry) {
156 struct got_object_id *id = NULL;
158 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
159 continue;
161 error = got_ref_resolve(&id, repo, re->ref);
162 if (error)
163 goto done;
165 error = got_object_open_as_commit(&commit, repo, id);
166 free(id);
167 if (error)
168 goto done;
170 committer_time =
171 got_object_commit_get_committer_time(commit);
172 got_object_commit_close(commit);
173 if (cmp_time < committer_time)
174 cmp_time = committer_time;
176 if (refname)
177 break;
180 if (cmp_time != 0)
181 *repo_age = cmp_time;
182 done:
183 got_ref_list_free(&refs);
184 return error;
187 static const struct got_error *
188 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
189 struct got_commit_object *commit, struct got_reflist_head *refs,
190 struct got_object_id *id)
192 const struct got_error *error = NULL;
193 struct got_reflist_entry *re;
194 struct got_object_id *id2 = NULL;
195 struct got_object_qid *parent_id;
196 struct transport *t = c->t;
197 struct querystring *qs = c->t->qs;
198 char *commit_msg = NULL, *commit_msg0;
200 TAILQ_FOREACH(re, refs, entry) {
201 char *s;
202 const char *name;
203 struct got_tag_object *tag = NULL;
204 struct got_object_id *ref_id;
205 int cmp;
207 if (got_ref_is_symbolic(re->ref))
208 continue;
210 name = got_ref_get_name(re->ref);
211 if (strncmp(name, "refs/", 5) == 0)
212 name += 5;
213 if (strncmp(name, "got/", 4) == 0)
214 continue;
215 if (strncmp(name, "heads/", 6) == 0)
216 name += 6;
217 if (strncmp(name, "remotes/", 8) == 0) {
218 name += 8;
219 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
220 continue;
222 error = got_ref_resolve(&ref_id, t->repo, re->ref);
223 if (error)
224 return error;
225 if (strncmp(name, "tags/", 5) == 0) {
226 error = got_object_open_as_tag(&tag, t->repo, ref_id);
227 if (error) {
228 if (error->code != GOT_ERR_OBJ_TYPE) {
229 free(ref_id);
230 continue;
232 /*
233 * Ref points at something other
234 * than a tag.
235 */
236 error = NULL;
237 tag = NULL;
240 cmp = got_object_id_cmp(tag ?
241 got_object_tag_get_object_id(tag) : ref_id, id);
242 free(ref_id);
243 if (tag)
244 got_object_tag_close(tag);
245 if (cmp != 0)
246 continue;
247 s = repo_commit->refs_str;
248 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
249 s ? ", " : "", name) == -1) {
250 error = got_error_from_errno("asprintf");
251 free(s);
252 repo_commit->refs_str = NULL;
253 return error;
255 free(s);
258 error = got_object_id_str(&repo_commit->commit_id, id);
259 if (error)
260 return error;
262 error = got_object_id_str(&repo_commit->tree_id,
263 got_object_commit_get_tree_id(commit));
264 if (error)
265 return error;
267 if (qs->action == DIFF) {
268 parent_id = STAILQ_FIRST(
269 got_object_commit_get_parent_ids(commit));
270 if (parent_id != NULL) {
271 id2 = got_object_id_dup(&parent_id->id);
272 error = got_object_id_str(&repo_commit->parent_id, id2);
273 if (error)
274 return error;
275 free(id2);
276 } else {
277 repo_commit->parent_id = strdup("/dev/null");
278 if (repo_commit->parent_id == NULL) {
279 error = got_error_from_errno("strdup");
280 return error;
285 repo_commit->committer_time =
286 got_object_commit_get_committer_time(commit);
288 repo_commit->author =
289 strdup(got_object_commit_get_author(commit));
290 if (repo_commit->author == NULL) {
291 error = got_error_from_errno("strdup");
292 return error;
294 repo_commit->committer =
295 strdup(got_object_commit_get_committer(commit));
296 if (repo_commit->committer == NULL) {
297 error = got_error_from_errno("strdup");
298 return error;
300 error = got_object_commit_get_logmsg(&commit_msg0, commit);
301 if (error)
302 return error;
304 commit_msg = commit_msg0;
305 while (*commit_msg == '\n')
306 commit_msg++;
308 repo_commit->commit_msg = strdup(commit_msg);
309 if (repo_commit->commit_msg == NULL)
310 error = got_error_from_errno("strdup");
311 free(commit_msg0);
312 return error;
315 const struct got_error *
316 got_get_repo_commits(struct request *c, int limit)
318 const struct got_error *error = NULL;
319 struct got_object_id *id = NULL;
320 struct got_commit_graph *graph = NULL;
321 struct got_commit_object *commit = NULL;
322 struct got_reflist_head refs;
323 struct got_reference *ref = NULL;
324 struct repo_commit *repo_commit = NULL;
325 struct server *srv = c->srv;
326 struct transport *t = c->t;
327 struct got_repository *repo = t->repo;
328 struct querystring *qs = t->qs;
329 struct repo_dir *repo_dir = t->repo_dir;
330 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
331 int chk_next = 0, chk_multi = 0;
333 TAILQ_INIT(&refs);
335 if (qs->file != NULL && strlen(qs->file) > 0)
336 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
337 qs->file) == -1)
338 return got_error_from_errno("asprintf");
340 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
341 repo_dir->name) == -1) {
342 error = got_error_from_errno("asprintf");
343 goto done;
346 if (qs->commit) {
347 error = got_repo_match_object_id_prefix(&id, qs->commit,
348 GOT_OBJ_TYPE_COMMIT, repo);
349 if (error)
350 goto done;
351 } else {
352 error = got_ref_open(&ref, repo, qs->headref, 0);
353 if (error)
354 goto done;
356 error = got_ref_resolve(&id, repo, ref);
357 if (error)
358 goto done;
361 error = got_repo_map_path(&in_repo_path, repo, repo_path);
362 if (error)
363 goto done;
365 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
366 if (error)
367 goto done;
369 if (qs->file != NULL && strlen(qs->file) > 0) {
370 error = got_commit_graph_open(&graph, file_path, 0);
371 if (error)
372 goto done;
373 } else {
374 error = got_commit_graph_open(&graph, in_repo_path, 0);
375 if (error)
376 goto done;
379 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
380 if (error)
381 goto done;
383 for (;;) {
384 struct got_object_id next_id;
386 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
387 NULL);
388 if (error) {
389 if (error->code == GOT_ERR_ITER_COMPLETED)
390 error = NULL;
391 goto done;
394 error = got_object_open_as_commit(&commit, repo, &next_id);
395 if (error)
396 goto done;
398 error = got_init_repo_commit(&repo_commit);
399 if (error)
400 goto done;
402 error = got_get_repo_commit(c, repo_commit, commit,
403 &refs, &next_id);
404 if (error) {
405 gotweb_free_repo_commit(repo_commit);
406 goto done;
409 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
411 if (!chk_multi || limit != 1 ||
412 srv->max_commits_display == 1) {
413 chk_multi = 1;
415 /*
416 * check for one more commit before breaking,
417 * so we know whether to navigate through briefs
418 * commits and summary
419 */
420 if (chk_next && (qs->action == BRIEFS ||
421 qs->action == COMMITS || qs->action == SUMMARY)) {
422 t->more_id = strdup(repo_commit->commit_id);
423 if (t->more_id == NULL) {
424 error = got_error_from_errno("strdup");
425 goto done;
427 got_object_commit_close(commit);
428 commit = NULL;
429 TAILQ_REMOVE(&t->repo_commits, repo_commit,
430 entry);
431 gotweb_free_repo_commit(repo_commit);
432 goto done;
435 if (error || (limit && --limit == 0)) {
436 if (qs->file != NULL && *qs->file != '\0')
437 if (chk_multi == 0)
438 break;
439 chk_next = 1;
441 if (commit) {
442 got_object_commit_close(commit);
443 commit = NULL;
446 done:
447 if (ref)
448 got_ref_close(ref);
449 if (commit)
450 got_object_commit_close(commit);
451 if (graph)
452 got_commit_graph_close(graph);
453 got_ref_list_free(&refs);
454 free(in_repo_path);
455 free(file_path);
456 free(repo_path);
457 free(id);
458 return error;
461 const struct got_error *
462 got_get_repo_tags(struct request *c, int limit)
464 const struct got_error *error = NULL;
465 struct got_object_id *id = NULL;
466 struct got_commit_object *commit = NULL;
467 struct got_reflist_head refs;
468 struct got_reference *ref;
469 struct got_reflist_entry *re;
470 struct server *srv = c->srv;
471 struct transport *t = c->t;
472 struct got_repository *repo = t->repo;
473 struct querystring *qs = t->qs;
474 struct repo_dir *repo_dir = t->repo_dir;
475 struct got_tag_object *tag = NULL;
476 struct repo_tag *rt = NULL, *trt = NULL;
477 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
478 char *tag_commit = NULL, *tag_commit0 = NULL;
479 char *commit_msg = NULL, *commit_msg0 = NULL;
480 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
482 TAILQ_INIT(&refs);
484 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
485 repo_dir->name) == -1)
486 return got_error_from_errno("asprintf");
488 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
489 error = got_ref_open(&ref, repo, qs->headref, 0);
490 if (error)
491 goto err;
492 error = got_ref_resolve(&id, repo, ref);
493 got_ref_close(ref);
494 if (error)
495 goto err;
496 } else if (qs->commit == NULL && qs->action == TAG) {
497 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
498 goto err;
499 } else {
500 error = got_repo_match_object_id_prefix(&id, qs->commit,
501 GOT_OBJ_TYPE_COMMIT, repo);
502 if (error)
503 goto err;
506 if (qs->action != SUMMARY && qs->action != TAGS) {
507 error = got_object_open_as_commit(&commit, repo, id);
508 if (error)
509 goto err;
510 error = got_object_commit_get_logmsg(&commit_msg0, commit);
511 if (error)
512 goto err;
513 if (commit) {
514 got_object_commit_close(commit);
515 commit = NULL;
519 error = got_repo_map_path(&in_repo_path, repo, repo_path);
520 if (error)
521 goto err;
523 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
524 repo);
525 if (error)
526 goto err;
528 if (limit == 1)
529 chk_multi = 0;
531 /*
532 * XXX: again, see previous message about caching
533 */
535 TAILQ_FOREACH(re, &refs, entry) {
536 struct repo_tag *new_repo_tag = NULL;
537 error = got_init_repo_tag(&new_repo_tag);
538 if (error)
539 goto err;
541 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
543 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
544 if (new_repo_tag->tag_name == NULL) {
545 error = got_error_from_errno("strdup");
546 goto err;
549 free(id);
550 id = NULL;
552 free(id_str);
553 id_str = NULL;
555 error = got_ref_resolve(&id, repo, re->ref);
556 if (error)
557 goto done;
559 if (tag)
560 got_object_tag_close(tag);
561 error = got_object_open_as_tag(&tag, repo, id);
562 if (error) {
563 if (error->code != GOT_ERR_OBJ_TYPE)
564 goto done;
565 /* "lightweight" tag */
566 error = got_object_open_as_commit(&commit, repo, id);
567 if (error)
568 goto done;
569 new_repo_tag->tagger =
570 strdup(got_object_commit_get_committer(commit));
571 if (new_repo_tag->tagger == NULL) {
572 error = got_error_from_errno("strdup");
573 goto err;
575 new_repo_tag->tagger_time =
576 got_object_commit_get_committer_time(commit);
577 error = got_object_id_str(&id_str, id);
578 if (error)
579 goto err;
580 } else {
581 new_repo_tag->tagger =
582 strdup(got_object_tag_get_tagger(tag));
583 if (new_repo_tag->tagger == NULL) {
584 error = got_error_from_errno("strdup");
585 goto err;
587 new_repo_tag->tagger_time =
588 got_object_tag_get_tagger_time(tag);
589 error = got_object_id_str(&id_str,
590 got_object_tag_get_object_id(tag));
591 if (error)
592 goto err;
595 new_repo_tag->commit_id = strdup(id_str);
596 if (new_repo_tag->commit_id == NULL)
597 goto err;
599 if (commit_found == 0 && qs->commit != NULL &&
600 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
601 continue;
602 else
603 commit_found = 1;
605 t->tag_count++;
607 /*
608 * check for one more commit before breaking,
609 * so we know whether to navigate through briefs
610 * commits and summary
611 */
612 if (chk_next) {
613 t->next_id = strdup(new_repo_tag->commit_id);
614 if (t->next_id == NULL) {
615 error = got_error_from_errno("strdup");
616 goto err;
618 if (commit) {
619 got_object_commit_close(commit);
620 commit = NULL;
622 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
623 gotweb_free_repo_tag(new_repo_tag);
624 goto done;
627 if (commit) {
628 error = got_object_commit_get_logmsg(&tag_commit0,
629 commit);
630 if (error)
631 goto err;
632 got_object_commit_close(commit);
633 commit = NULL;
634 } else {
635 tag_commit0 = strdup(got_object_tag_get_message(tag));
636 if (tag_commit0 == NULL) {
637 error = got_error_from_errno("strdup");
638 goto err;
642 tag_commit = tag_commit0;
643 while (*tag_commit == '\n')
644 tag_commit++;
645 new_repo_tag->tag_commit = strdup(tag_commit);
646 if (new_repo_tag->tag_commit == NULL) {
647 error = got_error_from_errno("strdup");
648 free(tag_commit0);
649 goto err;
651 free(tag_commit0);
653 if (qs->action != SUMMARY && qs->action != TAGS) {
654 commit_msg = commit_msg0;
655 while (*commit_msg == '\n')
656 commit_msg++;
658 new_repo_tag->commit_msg = strdup(commit_msg);
659 if (new_repo_tag->commit_msg == NULL) {
660 error = got_error_from_errno("strdup");
661 goto err;
665 if (limit && --limit == 0) {
666 if (chk_multi == 0)
667 break;
668 chk_next = 1;
672 done:
673 /*
674 * we have tailq populated, so find previous commit id
675 * for navigation through briefs and commits
676 */
677 if (t->tag_count == 0) {
678 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
679 TAILQ_REMOVE(&t->repo_tags, rt, entry);
680 gotweb_free_repo_tag(rt);
683 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
684 commit_found = 0;
685 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
686 entry) {
687 if (commit_found == 0 && rt->commit_id != NULL &&
688 strcmp(qs->commit, rt->commit_id) != 0) {
689 continue;
690 } else
691 commit_found = 1;
692 if (c_cnt == srv->max_commits_display ||
693 rt == TAILQ_FIRST(&t->repo_tags)) {
694 t->prev_id = strdup(rt->commit_id);
695 if (t->prev_id == NULL)
696 error = got_error_from_errno("strdup");
697 break;
699 c_cnt++;
702 err:
703 if (commit)
704 got_object_commit_close(commit);
705 if (tag)
706 got_object_tag_close(tag);
707 got_ref_list_free(&refs);
708 free(commit_msg0);
709 free(in_repo_path);
710 free(repo_path);
711 free(id);
712 free(id_str);
713 return error;
716 int
717 got_output_repo_tree(struct request *c,
718 int (*cb)(struct template *, struct got_tree_entry *))
720 const struct got_error *error = NULL;
721 struct transport *t = c->t;
722 struct got_commit_object *commit = NULL;
723 struct got_repository *repo = t->repo;
724 struct querystring *qs = t->qs;
725 struct repo_commit *rc = NULL;
726 struct got_object_id *tree_id = NULL, *commit_id = NULL;
727 struct got_reflist_head refs;
728 struct got_tree_object *tree = NULL;
729 struct got_tree_entry *te;
730 struct repo_dir *repo_dir = t->repo_dir;
731 char *escaped_name = NULL, *path = NULL;
732 int nentries, i;
734 TAILQ_INIT(&refs);
736 rc = TAILQ_FIRST(&t->repo_commits);
738 if (qs->folder != NULL) {
739 path = strdup(qs->folder);
740 if (path == NULL) {
741 error = got_error_from_errno("strdup");
742 goto done;
744 } else {
745 error = got_repo_map_path(&path, repo, repo_dir->path);
746 if (error)
747 goto done;
750 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
751 GOT_OBJ_TYPE_COMMIT, &refs, repo);
752 if (error)
753 goto done;
755 error = got_object_open_as_commit(&commit, repo, commit_id);
756 if (error)
757 goto done;
759 error = got_object_id_by_path(&tree_id, repo, commit, path);
760 if (error)
761 goto done;
763 error = got_object_open_as_tree(&tree, repo, tree_id);
764 if (error)
765 goto done;
767 nentries = got_object_tree_get_nentries(tree);
769 for (i = 0; i < nentries; i++) {
770 te = got_object_tree_get_entry(tree, i);
771 if (cb(c->tp, te) == -1)
772 break;
774 done:
775 free(escaped_name);
776 free(path);
777 got_ref_list_free(&refs);
778 if (commit)
779 got_object_commit_close(commit);
780 if (tree)
781 got_object_tree_close(tree);
782 free(commit_id);
783 free(tree_id);
784 if (error) {
785 log_warnx("%s: %s", __func__, error->msg);
786 return -1;
788 return 0;
791 const struct got_error *
792 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
793 int *binary, struct request *c)
795 const struct got_error *error = NULL;
796 struct transport *t = c->t;
797 struct got_repository *repo = t->repo;
798 struct querystring *qs = c->t->qs;
799 struct got_commit_object *commit = NULL;
800 struct got_object_id *commit_id = NULL;
801 struct got_reflist_head refs;
802 char *path = NULL, *in_repo_path = NULL;
803 int obj_type;
805 TAILQ_INIT(&refs);
807 *blob = NULL;
808 *fd = -1;
809 *binary = 0;
811 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
812 qs->folder ? "/" : "", qs->file) == -1) {
813 error = got_error_from_errno("asprintf");
814 goto done;
817 error = got_repo_map_path(&in_repo_path, repo, path);
818 if (error)
819 goto done;
821 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
822 GOT_OBJ_TYPE_COMMIT, &refs, repo);
823 if (error)
824 goto done;
826 error = got_object_open_as_commit(&commit, repo, commit_id);
827 if (error)
828 goto done;
830 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
831 if (error)
832 goto done;
834 if (commit_id == NULL) {
835 error = got_error(GOT_ERR_NO_OBJ);
836 goto done;
839 error = got_object_get_type(&obj_type, repo, commit_id);
840 if (error)
841 goto done;
843 if (obj_type != GOT_OBJ_TYPE_BLOB) {
844 error = got_error(GOT_ERR_OBJ_TYPE);
845 goto done;
848 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
849 if (error)
850 goto done;
852 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
853 if (error)
854 goto done;
856 error = got_object_blob_is_binary(binary, *blob);
857 if (error)
858 goto done;
860 done:
861 if (commit)
862 got_object_commit_close(commit);
864 if (error) {
865 if (*fd != -1)
866 close(*fd);
867 if (*blob)
868 got_object_blob_close(*blob);
869 *fd = -1;
870 *blob = NULL;
873 free(in_repo_path);
874 free(commit_id);
875 free(path);
876 return error;
879 int
880 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
881 int (*cb)(struct template *, const char *, size_t))
883 const struct got_error *err;
884 char *line = NULL;
885 size_t linesize = 0;
886 size_t lineno = 0;
887 ssize_t linelen = 0;
889 for (;;) {
890 err = got_object_blob_getline(&line, &linelen, &linesize,
891 blob);
892 if (err || linelen == -1)
893 break;
894 lineno++;
895 if (cb(tp, line, lineno) == -1)
896 break;
899 free(line);
901 if (err) {
902 log_warnx("%s: got_object_blob_getline failed: %s",
903 __func__, err->msg);
904 return -1;
906 return 0;
909 struct blame_cb_args {
910 struct blame_line *lines;
911 int nlines;
912 int nlines_prec;
913 int lineno_cur;
914 off_t *line_offsets;
915 FILE *f;
916 struct got_repository *repo;
917 struct request *c;
918 got_render_blame_line_cb cb;
919 };
921 static const struct got_error *
922 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
923 struct got_commit_object *commit, struct got_object_id *id)
925 const struct got_error *err = NULL;
926 struct blame_cb_args *a = arg;
927 struct blame_line *bline;
928 struct request *c = a->c;
929 char *line = NULL;
930 size_t linesize = 0;
931 off_t offset;
932 struct tm tm;
933 time_t committer_time;
935 if (nlines != a->nlines ||
936 (lineno != -1 && lineno < 1) || lineno > a->nlines)
937 return got_error(GOT_ERR_RANGE);
939 if (lineno == -1)
940 return NULL; /* no change in this commit */
942 /* Annotate this line. */
943 bline = &a->lines[lineno - 1];
944 if (bline->annotated)
945 return NULL;
946 err = got_object_id_str(&bline->id_str, id);
947 if (err)
948 return err;
950 bline->committer = strdup(got_object_commit_get_committer(commit));
951 if (bline->committer == NULL) {
952 err = got_error_from_errno("strdup");
953 goto done;
956 committer_time = got_object_commit_get_committer_time(commit);
957 if (gmtime_r(&committer_time, &tm) == NULL)
958 return got_error_from_errno("gmtime_r");
959 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
960 &tm) == 0) {
961 err = got_error(GOT_ERR_NO_SPACE);
962 goto done;
964 bline->annotated = 1;
966 /* Print lines annotated so far. */
967 bline = &a->lines[a->lineno_cur - 1];
968 if (!bline->annotated)
969 goto done;
971 offset = a->line_offsets[a->lineno_cur - 1];
972 if (fseeko(a->f, offset, SEEK_SET) == -1) {
973 err = got_error_from_errno("fseeko");
974 goto done;
977 while (a->lineno_cur <= a->nlines && bline->annotated) {
978 if (getline(&line, &linesize, a->f) == -1) {
979 if (ferror(a->f))
980 err = got_error_from_errno("getline");
981 break;
984 if (a->cb(c->tp, line, bline, a->nlines_prec,
985 a->lineno_cur) == -1)
986 break;
988 a->lineno_cur++;
989 bline = &a->lines[a->lineno_cur - 1];
991 done:
992 free(line);
993 return err;
996 const struct got_error *
997 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
999 const struct got_error *error = NULL;
1000 struct transport *t = c->t;
1001 struct got_repository *repo = t->repo;
1002 struct querystring *qs = c->t->qs;
1003 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1004 struct got_commit_object *commit = NULL;
1005 struct got_reflist_head refs;
1006 struct got_blob_object *blob = NULL;
1007 char *path = NULL, *in_repo_path = NULL;
1008 struct blame_cb_args bca;
1009 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1010 int fd6 = -1;
1011 off_t filesize;
1012 FILE *f1 = NULL, *f2 = NULL;
1014 TAILQ_INIT(&refs);
1015 bca.f = NULL;
1016 bca.lines = NULL;
1017 bca.cb = cb;
1019 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1020 qs->folder ? "/" : "", qs->file) == -1) {
1021 error = got_error_from_errno("asprintf");
1022 goto done;
1025 error = got_repo_map_path(&in_repo_path, repo, path);
1026 if (error)
1027 goto done;
1029 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1030 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1031 if (error)
1032 goto done;
1034 error = got_object_open_as_commit(&commit, repo, commit_id);
1035 if (error)
1036 goto done;
1038 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1039 if (error)
1040 goto done;
1042 error = got_object_get_type(&obj_type, repo, obj_id);
1043 if (error)
1044 goto done;
1046 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1047 error = got_error(GOT_ERR_OBJ_TYPE);
1048 goto done;
1051 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1052 if (error)
1053 goto done;
1055 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1056 if (error)
1057 goto done;
1059 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1060 if (error)
1061 goto done;
1063 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1064 &bca.line_offsets, bca.f, blob);
1065 if (error || bca.nlines == 0)
1066 goto done;
1068 /* Don't include \n at EOF in the blame line count. */
1069 if (bca.line_offsets[bca.nlines - 1] == filesize)
1070 bca.nlines--;
1072 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1073 if (bca.lines == NULL) {
1074 error = got_error_from_errno("calloc");
1075 goto done;
1077 bca.lineno_cur = 1;
1078 bca.nlines_prec = 0;
1079 i = bca.nlines;
1080 while (i > 0) {
1081 i /= 10;
1082 bca.nlines_prec++;
1084 bca.repo = repo;
1085 bca.c = c;
1087 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1088 if (error)
1089 goto done;
1091 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1092 if (error)
1093 goto done;
1095 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1096 if (error)
1097 goto done;
1099 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1100 if (error)
1101 goto done;
1103 error = got_blame(in_repo_path, commit_id, repo,
1104 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1105 fd3, fd4, f1, f2);
1107 done:
1108 if (bca.lines) {
1109 free(bca.line_offsets);
1110 for (i = 0; i < bca.nlines; i++) {
1111 struct blame_line *bline = &bca.lines[i];
1112 free(bline->id_str);
1113 free(bline->committer);
1116 free(bca.lines);
1117 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1118 error = got_error_from_errno("close");
1119 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1120 error = got_error_from_errno("close");
1121 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1122 error = got_error_from_errno("close");
1123 if (bca.f) {
1124 const struct got_error *bca_err =
1125 got_gotweb_flushfile(bca.f, fd1);
1126 if (error == NULL)
1127 error = bca_err;
1129 if (f1) {
1130 const struct got_error *f1_err =
1131 got_gotweb_flushfile(f1, fd5);
1132 if (error == NULL)
1133 error = f1_err;
1135 if (f2) {
1136 const struct got_error *f2_err =
1137 got_gotweb_flushfile(f2, fd6);
1138 if (error == NULL)
1139 error = f2_err;
1141 if (commit)
1142 got_object_commit_close(commit);
1143 if (blob)
1144 got_object_blob_close(blob);
1145 free(in_repo_path);
1146 free(commit_id);
1147 free(obj_id);
1148 free(path);
1149 got_ref_list_free(&refs);
1150 return error;
1153 const struct got_error *
1154 got_open_diff_for_output(FILE **fp, int *fd, struct request *c)
1156 const struct got_error *error = NULL;
1157 struct transport *t = c->t;
1158 struct got_repository *repo = t->repo;
1159 struct repo_commit *rc = NULL;
1160 struct got_object_id *id1 = NULL, *id2 = NULL;
1161 struct got_reflist_head refs;
1162 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1163 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1165 *fp = NULL;
1166 *fd = -1;
1168 TAILQ_INIT(&refs);
1170 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1171 if (error)
1172 return error;
1174 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1175 if (error)
1176 return error;
1178 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1179 if (error)
1180 return error;
1182 rc = TAILQ_FIRST(&t->repo_commits);
1184 if (rc->parent_id != NULL &&
1185 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1186 error = got_repo_match_object_id(&id1, NULL,
1187 rc->parent_id, GOT_OBJ_TYPE_ANY,
1188 &refs, repo);
1189 if (error)
1190 goto done;
1193 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1194 GOT_OBJ_TYPE_ANY, &refs, repo);
1195 if (error)
1196 goto done;
1198 error = got_object_get_type(&obj_type, repo, id2);
1199 if (error)
1200 goto done;
1202 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1203 if (error)
1204 goto done;
1206 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1207 if (error)
1208 goto done;
1210 switch (obj_type) {
1211 case GOT_OBJ_TYPE_BLOB:
1212 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1213 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1214 NULL, repo, f3);
1215 break;
1216 case GOT_OBJ_TYPE_TREE:
1217 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1218 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1219 NULL, repo, f3);
1220 break;
1221 case GOT_OBJ_TYPE_COMMIT:
1222 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1223 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1224 NULL, repo, f3);
1225 break;
1226 default:
1227 error = got_error(GOT_ERR_OBJ_TYPE);
1229 if (error)
1230 goto done;
1232 if (fseek(f1, 0, SEEK_SET) == -1) {
1233 error = got_ferror(f1, GOT_ERR_IO);
1234 goto done;
1237 if (fseek(f2, 0, SEEK_SET) == -1) {
1238 error = got_ferror(f2, GOT_ERR_IO);
1239 goto done;
1242 if (fseek(f3, 0, SEEK_SET) == -1) {
1243 error = got_ferror(f3, GOT_ERR_IO);
1244 goto done;
1247 *fp = f3;
1248 *fd = fd3;
1250 done:
1251 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1252 error = got_error_from_errno("close");
1253 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1254 error = got_error_from_errno("close");
1255 if (f1) {
1256 const struct got_error *f1_err =
1257 got_gotweb_flushfile(f1, fd1);
1258 if (error == NULL)
1259 error = f1_err;
1261 if (f2) {
1262 const struct got_error *f2_err =
1263 got_gotweb_flushfile(f2, fd2);
1264 if (error == NULL)
1265 error = f2_err;
1267 if (error && f3) {
1268 got_gotweb_flushfile(f3, fd3);
1269 *fp = NULL;
1270 *fd = -1;
1272 got_ref_list_free(&refs);
1273 free(id1);
1274 free(id2);
1275 return error;
1278 static const struct got_error *
1279 got_init_repo_commit(struct repo_commit **rc)
1281 *rc = calloc(1, sizeof(**rc));
1282 if (*rc == NULL)
1283 return got_error_from_errno2("%s: calloc", __func__);
1285 (*rc)->path = NULL;
1286 (*rc)->refs_str = NULL;
1287 (*rc)->commit_id = NULL;
1288 (*rc)->committer = NULL;
1289 (*rc)->author = NULL;
1290 (*rc)->parent_id = NULL;
1291 (*rc)->tree_id = NULL;
1292 (*rc)->commit_msg = NULL;
1294 return NULL;
1297 static const struct got_error *
1298 got_init_repo_tag(struct repo_tag **rt)
1300 *rt = calloc(1, sizeof(**rt));
1301 if (*rt == NULL)
1302 return got_error_from_errno2("%s: calloc", __func__);
1304 (*rt)->commit_id = NULL;
1305 (*rt)->tag_name = NULL;
1306 (*rt)->tag_commit = NULL;
1307 (*rt)->commit_msg = NULL;
1308 (*rt)->tagger = NULL;
1310 return NULL;