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 error = got_error(GOT_ERR_CANCELLED);
773 break;
776 done:
777 free(escaped_name);
778 free(path);
779 got_ref_list_free(&refs);
780 if (commit)
781 got_object_commit_close(commit);
782 if (tree)
783 got_object_tree_close(tree);
784 free(commit_id);
785 free(tree_id);
786 if (error) {
787 log_warnx("%s: %s", __func__, error->msg);
788 return -1;
790 return 0;
793 const struct got_error *
794 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
795 int *binary, struct request *c)
797 const struct got_error *error = NULL;
798 struct transport *t = c->t;
799 struct got_repository *repo = t->repo;
800 struct querystring *qs = c->t->qs;
801 struct got_commit_object *commit = NULL;
802 struct got_object_id *commit_id = NULL;
803 struct got_reflist_head refs;
804 char *path = NULL, *in_repo_path = NULL;
805 int obj_type;
807 TAILQ_INIT(&refs);
809 *blob = NULL;
810 *fd = -1;
811 *binary = 0;
813 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
814 qs->folder ? "/" : "", qs->file) == -1) {
815 error = got_error_from_errno("asprintf");
816 goto done;
819 error = got_repo_map_path(&in_repo_path, repo, path);
820 if (error)
821 goto done;
823 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
824 GOT_OBJ_TYPE_COMMIT, &refs, repo);
825 if (error)
826 goto done;
828 error = got_object_open_as_commit(&commit, repo, commit_id);
829 if (error)
830 goto done;
832 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
833 if (error)
834 goto done;
836 if (commit_id == NULL) {
837 error = got_error(GOT_ERR_NO_OBJ);
838 goto done;
841 error = got_object_get_type(&obj_type, repo, commit_id);
842 if (error)
843 goto done;
845 if (obj_type != GOT_OBJ_TYPE_BLOB) {
846 error = got_error(GOT_ERR_OBJ_TYPE);
847 goto done;
850 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
851 if (error)
852 goto done;
854 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
855 if (error)
856 goto done;
858 error = got_object_blob_is_binary(binary, *blob);
859 if (error)
860 goto done;
862 done:
863 if (commit)
864 got_object_commit_close(commit);
866 if (error) {
867 if (*fd != -1)
868 close(*fd);
869 if (*blob)
870 got_object_blob_close(*blob);
871 *fd = -1;
872 *blob = NULL;
875 free(in_repo_path);
876 free(commit_id);
877 free(path);
878 return error;
881 int
882 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
883 int (*cb)(struct template *, const char *, size_t))
885 const struct got_error *err;
886 char *line = NULL;
887 size_t linesize = 0;
888 size_t lineno = 0;
889 ssize_t linelen = 0;
891 for (;;) {
892 err = got_object_blob_getline(&line, &linelen, &linesize,
893 blob);
894 if (err || linelen == -1)
895 break;
896 lineno++;
897 if (cb(tp, line, lineno) == -1) {
898 err = got_error(GOT_ERR_CANCELLED);
899 break;
903 free(line);
905 if (err) {
906 log_warnx("%s: got_object_blob_getline failed: %s",
907 __func__, err->msg);
908 return -1;
910 return 0;
913 struct blame_cb_args {
914 struct blame_line *lines;
915 int nlines;
916 int nlines_prec;
917 int lineno_cur;
918 off_t *line_offsets;
919 FILE *f;
920 struct got_repository *repo;
921 struct request *c;
922 got_render_blame_line_cb cb;
923 };
925 static const struct got_error *
926 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
927 struct got_commit_object *commit, struct got_object_id *id)
929 const struct got_error *err = NULL;
930 struct blame_cb_args *a = arg;
931 struct blame_line *bline;
932 struct request *c = a->c;
933 char *line = NULL;
934 size_t linesize = 0;
935 off_t offset;
936 struct tm tm;
937 time_t committer_time;
939 if (nlines != a->nlines ||
940 (lineno != -1 && lineno < 1) || lineno > a->nlines)
941 return got_error(GOT_ERR_RANGE);
943 if (lineno == -1)
944 return NULL; /* no change in this commit */
946 /* Annotate this line. */
947 bline = &a->lines[lineno - 1];
948 if (bline->annotated)
949 return NULL;
950 err = got_object_id_str(&bline->id_str, id);
951 if (err)
952 return err;
954 bline->committer = strdup(got_object_commit_get_committer(commit));
955 if (bline->committer == NULL) {
956 err = got_error_from_errno("strdup");
957 goto done;
960 committer_time = got_object_commit_get_committer_time(commit);
961 if (gmtime_r(&committer_time, &tm) == NULL)
962 return got_error_from_errno("gmtime_r");
963 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
964 &tm) == 0) {
965 err = got_error(GOT_ERR_NO_SPACE);
966 goto done;
968 bline->annotated = 1;
970 /* Print lines annotated so far. */
971 bline = &a->lines[a->lineno_cur - 1];
972 if (!bline->annotated)
973 goto done;
975 offset = a->line_offsets[a->lineno_cur - 1];
976 if (fseeko(a->f, offset, SEEK_SET) == -1) {
977 err = got_error_from_errno("fseeko");
978 goto done;
981 while (a->lineno_cur <= a->nlines && bline->annotated) {
982 if (getline(&line, &linesize, a->f) == -1) {
983 if (ferror(a->f))
984 err = got_error_from_errno("getline");
985 break;
988 if (a->cb(c->tp, line, bline, a->nlines_prec,
989 a->lineno_cur) == -1) {
990 err = got_error(GOT_ERR_CANCELLED);
991 break;
994 a->lineno_cur++;
995 bline = &a->lines[a->lineno_cur - 1];
997 done:
998 free(line);
999 return err;
1002 const struct got_error *
1003 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1005 const struct got_error *error = NULL;
1006 struct transport *t = c->t;
1007 struct got_repository *repo = t->repo;
1008 struct querystring *qs = c->t->qs;
1009 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1010 struct got_commit_object *commit = NULL;
1011 struct got_reflist_head refs;
1012 struct got_blob_object *blob = NULL;
1013 char *path = NULL, *in_repo_path = NULL;
1014 struct blame_cb_args bca;
1015 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1016 int fd6 = -1;
1017 off_t filesize;
1018 FILE *f1 = NULL, *f2 = NULL;
1020 TAILQ_INIT(&refs);
1021 bca.f = NULL;
1022 bca.lines = NULL;
1023 bca.cb = cb;
1025 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1026 qs->folder ? "/" : "", qs->file) == -1) {
1027 error = got_error_from_errno("asprintf");
1028 goto done;
1031 error = got_repo_map_path(&in_repo_path, repo, path);
1032 if (error)
1033 goto done;
1035 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1036 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1037 if (error)
1038 goto done;
1040 error = got_object_open_as_commit(&commit, repo, commit_id);
1041 if (error)
1042 goto done;
1044 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1045 if (error)
1046 goto done;
1048 error = got_object_get_type(&obj_type, repo, obj_id);
1049 if (error)
1050 goto done;
1052 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1053 error = got_error(GOT_ERR_OBJ_TYPE);
1054 goto done;
1057 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1058 if (error)
1059 goto done;
1061 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1062 if (error)
1063 goto done;
1065 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1066 if (error)
1067 goto done;
1069 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1070 &bca.line_offsets, bca.f, blob);
1071 if (error || bca.nlines == 0)
1072 goto done;
1074 /* Don't include \n at EOF in the blame line count. */
1075 if (bca.line_offsets[bca.nlines - 1] == filesize)
1076 bca.nlines--;
1078 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1079 if (bca.lines == NULL) {
1080 error = got_error_from_errno("calloc");
1081 goto done;
1083 bca.lineno_cur = 1;
1084 bca.nlines_prec = 0;
1085 i = bca.nlines;
1086 while (i > 0) {
1087 i /= 10;
1088 bca.nlines_prec++;
1090 bca.repo = repo;
1091 bca.c = c;
1093 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1094 if (error)
1095 goto done;
1097 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1098 if (error)
1099 goto done;
1101 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1102 if (error)
1103 goto done;
1105 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1106 if (error)
1107 goto done;
1109 error = got_blame(in_repo_path, commit_id, repo,
1110 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1111 fd3, fd4, f1, f2);
1113 done:
1114 if (bca.lines) {
1115 free(bca.line_offsets);
1116 for (i = 0; i < bca.nlines; i++) {
1117 struct blame_line *bline = &bca.lines[i];
1118 free(bline->id_str);
1119 free(bline->committer);
1122 free(bca.lines);
1123 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1124 error = got_error_from_errno("close");
1125 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1126 error = got_error_from_errno("close");
1127 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1128 error = got_error_from_errno("close");
1129 if (bca.f) {
1130 const struct got_error *bca_err =
1131 got_gotweb_flushfile(bca.f, fd1);
1132 if (error == NULL)
1133 error = bca_err;
1135 if (f1) {
1136 const struct got_error *f1_err =
1137 got_gotweb_flushfile(f1, fd5);
1138 if (error == NULL)
1139 error = f1_err;
1141 if (f2) {
1142 const struct got_error *f2_err =
1143 got_gotweb_flushfile(f2, fd6);
1144 if (error == NULL)
1145 error = f2_err;
1147 if (commit)
1148 got_object_commit_close(commit);
1149 if (blob)
1150 got_object_blob_close(blob);
1151 free(in_repo_path);
1152 free(commit_id);
1153 free(obj_id);
1154 free(path);
1155 got_ref_list_free(&refs);
1156 return error;
1159 const struct got_error *
1160 got_open_diff_for_output(FILE **fp, int *fd, struct request *c)
1162 const struct got_error *error = NULL;
1163 struct transport *t = c->t;
1164 struct got_repository *repo = t->repo;
1165 struct repo_commit *rc = NULL;
1166 struct got_object_id *id1 = NULL, *id2 = NULL;
1167 struct got_reflist_head refs;
1168 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1169 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1171 *fp = NULL;
1172 *fd = -1;
1174 TAILQ_INIT(&refs);
1176 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1177 if (error)
1178 return error;
1180 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1181 if (error)
1182 return error;
1184 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1185 if (error)
1186 return error;
1188 rc = TAILQ_FIRST(&t->repo_commits);
1190 if (rc->parent_id != NULL &&
1191 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1192 error = got_repo_match_object_id(&id1, NULL,
1193 rc->parent_id, GOT_OBJ_TYPE_ANY,
1194 &refs, repo);
1195 if (error)
1196 goto done;
1199 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1200 GOT_OBJ_TYPE_ANY, &refs, repo);
1201 if (error)
1202 goto done;
1204 error = got_object_get_type(&obj_type, repo, id2);
1205 if (error)
1206 goto done;
1208 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1209 if (error)
1210 goto done;
1212 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1213 if (error)
1214 goto done;
1216 switch (obj_type) {
1217 case GOT_OBJ_TYPE_BLOB:
1218 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1219 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1220 NULL, repo, f3);
1221 break;
1222 case GOT_OBJ_TYPE_TREE:
1223 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1224 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1225 NULL, repo, f3);
1226 break;
1227 case GOT_OBJ_TYPE_COMMIT:
1228 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1229 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1230 NULL, repo, f3);
1231 break;
1232 default:
1233 error = got_error(GOT_ERR_OBJ_TYPE);
1235 if (error)
1236 goto done;
1238 if (fseek(f1, 0, SEEK_SET) == -1) {
1239 error = got_ferror(f1, GOT_ERR_IO);
1240 goto done;
1243 if (fseek(f2, 0, SEEK_SET) == -1) {
1244 error = got_ferror(f2, GOT_ERR_IO);
1245 goto done;
1248 if (fseek(f3, 0, SEEK_SET) == -1) {
1249 error = got_ferror(f3, GOT_ERR_IO);
1250 goto done;
1253 *fp = f3;
1254 *fd = fd3;
1256 done:
1257 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1258 error = got_error_from_errno("close");
1259 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1260 error = got_error_from_errno("close");
1261 if (f1) {
1262 const struct got_error *f1_err =
1263 got_gotweb_flushfile(f1, fd1);
1264 if (error == NULL)
1265 error = f1_err;
1267 if (f2) {
1268 const struct got_error *f2_err =
1269 got_gotweb_flushfile(f2, fd2);
1270 if (error == NULL)
1271 error = f2_err;
1273 if (error && f3) {
1274 got_gotweb_flushfile(f3, fd3);
1275 *fp = NULL;
1276 *fd = -1;
1278 got_ref_list_free(&refs);
1279 free(id1);
1280 free(id2);
1281 return error;
1284 static const struct got_error *
1285 got_init_repo_commit(struct repo_commit **rc)
1287 *rc = calloc(1, sizeof(**rc));
1288 if (*rc == NULL)
1289 return got_error_from_errno2("%s: calloc", __func__);
1291 (*rc)->path = NULL;
1292 (*rc)->refs_str = NULL;
1293 (*rc)->commit_id = NULL;
1294 (*rc)->committer = NULL;
1295 (*rc)->author = NULL;
1296 (*rc)->parent_id = NULL;
1297 (*rc)->tree_id = NULL;
1298 (*rc)->commit_msg = NULL;
1300 return NULL;
1303 static const struct got_error *
1304 got_init_repo_tag(struct repo_tag **rt)
1306 *rt = calloc(1, sizeof(**rt));
1307 if (*rt == NULL)
1308 return got_error_from_errno2("%s: calloc", __func__);
1310 (*rt)->commit_id = NULL;
1311 (*rt)->tag_name = NULL;
1312 (*rt)->tag_commit = NULL;
1313 (*rt)->commit_msg = NULL;
1314 (*rt)->tagger = NULL;
1316 return NULL;