2 * Copyright (c) 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include <sys/queue.h>
19 #include <sys/socket.h>
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_reference.h"
33 #include "got_repository.h"
35 #include "got_cancel.h"
37 #include "got_commit_graph.h"
38 #include "got_blame.h"
39 #include "got_privsep.h"
41 #include "got_compat.h"
46 static const struct got_error *got_init_repo_commit(struct repo_commit **);
47 static const struct got_error *got_init_repo_tag(struct repo_tag **);
48 static const struct got_error *got_get_repo_commit(struct request *,
49 struct repo_commit *, struct got_commit_object *, struct got_reflist_head *,
50 struct got_object_id *);
51 static const struct got_error *got_gotweb_dupfd(int *, int *);
52 static const struct got_error *got_gotweb_openfile(FILE **, int *, int *);
53 static const struct got_error *got_gotweb_blame_cb(void *, int, int,
54 struct got_commit_object *,struct got_object_id *);
56 const struct got_error *
57 got_gotweb_flushfile(FILE *f, int fd)
59 if (fseek(f, 0, SEEK_SET) == -1)
60 return got_error_from_errno("fseek");
62 if (ftruncate(fd, 0) == -1)
63 return got_error_from_errno("ftruncate");
66 return got_error_from_errno("fsync");
68 if (f && fclose(f) == EOF)
69 return got_error_from_errno("fclose");
71 if (fd != -1 && close(fd) != -1)
72 return got_error_from_errno("close");
77 static const struct got_error *
78 got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
82 return got_error_from_errno("dup");
84 *f = fdopen(*fd, "w+");
87 return got_error(GOT_ERR_PRIVSEP_NO_FD);
93 static const struct got_error *
94 got_gotweb_dupfd(int *priv_fd, int *fd)
99 return got_error_from_errno("dup");
104 const struct got_error *
105 got_get_repo_owner(char **owner, struct request *c)
107 struct server *srv = c->srv;
108 struct transport *t = c->t;
109 struct got_repository *repo = t->repo;
110 const char *gitconfig_owner;
114 if (srv->show_repo_owner == 0)
117 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
118 if (gitconfig_owner) {
119 *owner = strdup(gitconfig_owner);
121 return got_error_from_errno("strdup");
125 return got_error_from_errno("strdup");
130 const struct got_error *
131 got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
133 const struct got_error *error = NULL;
134 struct server *srv = c->srv;
135 struct transport *t = c->t;
136 struct got_repository *repo = t->repo;
137 struct got_commit_object *commit = NULL;
138 struct got_reflist_head refs;
139 struct got_reflist_entry *re;
140 time_t committer_time = 0, cmp_time = 0;
144 if (srv->show_repo_age == 0)
147 error = got_ref_list(&refs, repo, "refs/heads",
148 got_ref_cmp_by_name, NULL);
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.
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)
162 error = got_ref_resolve(&id, repo, re->ref);
166 error = got_object_open_as_commit(&commit, repo, id);
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;
182 *repo_age = cmp_time;
184 got_ref_list_free(&refs);
188 static const struct got_error *
189 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
190 struct got_commit_object *commit, struct got_reflist_head *refs,
191 struct got_object_id *id)
193 const struct got_error *error = NULL;
194 struct got_reflist_entry *re;
195 struct got_object_id *id2 = NULL;
196 struct got_object_qid *parent_id;
197 struct transport *t = c->t;
198 struct querystring *qs = c->t->qs;
199 char *commit_msg = NULL, *commit_msg0;
201 TAILQ_FOREACH(re, refs, entry) {
204 struct got_tag_object *tag = NULL;
205 struct got_object_id *ref_id;
208 if (got_ref_is_symbolic(re->ref))
211 name = got_ref_get_name(re->ref);
212 if (strncmp(name, "refs/", 5) == 0)
214 if (strncmp(name, "got/", 4) == 0)
216 if (strncmp(name, "heads/", 6) == 0)
218 if (strncmp(name, "remotes/", 8) == 0) {
220 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
223 error = got_ref_resolve(&ref_id, t->repo, re->ref);
226 if (strncmp(name, "tags/", 5) == 0) {
227 error = got_object_open_as_tag(&tag, t->repo, ref_id);
229 if (error->code != GOT_ERR_OBJ_TYPE) {
234 * Ref points at something other
241 cmp = got_object_id_cmp(tag ?
242 got_object_tag_get_object_id(tag) : ref_id, id);
245 got_object_tag_close(tag);
248 s = repo_commit->refs_str;
249 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
250 s ? ", " : "", name) == -1) {
251 error = got_error_from_errno("asprintf");
253 repo_commit->refs_str = NULL;
259 error = got_object_id_str(&repo_commit->commit_id, id);
263 error = got_object_id_str(&repo_commit->tree_id,
264 got_object_commit_get_tree_id(commit));
268 if (qs->action == DIFF) {
269 parent_id = STAILQ_FIRST(
270 got_object_commit_get_parent_ids(commit));
271 if (parent_id != NULL) {
272 id2 = got_object_id_dup(&parent_id->id);
273 error = got_object_id_str(&repo_commit->parent_id, id2);
278 repo_commit->parent_id = strdup("/dev/null");
279 if (repo_commit->parent_id == NULL) {
280 error = got_error_from_errno("strdup");
286 repo_commit->committer_time =
287 got_object_commit_get_committer_time(commit);
289 repo_commit->author =
290 strdup(got_object_commit_get_author(commit));
291 if (repo_commit->author == NULL) {
292 error = got_error_from_errno("strdup");
295 repo_commit->committer =
296 strdup(got_object_commit_get_committer(commit));
297 if (repo_commit->committer == NULL) {
298 error = got_error_from_errno("strdup");
301 error = got_object_commit_get_logmsg(&commit_msg0, commit);
305 commit_msg = commit_msg0;
306 while (*commit_msg == '\n')
309 repo_commit->commit_msg = strdup(commit_msg);
310 if (repo_commit->commit_msg == NULL)
311 error = got_error_from_errno("strdup");
316 const struct got_error *
317 got_get_repo_commits(struct request *c, int limit)
319 const struct got_error *error = NULL;
320 struct got_object_id *id = NULL;
321 struct got_commit_graph *graph = NULL;
322 struct got_commit_object *commit = NULL;
323 struct got_reflist_head refs;
324 struct got_reference *ref = NULL;
325 struct repo_commit *repo_commit = NULL;
326 struct server *srv = c->srv;
327 struct transport *t = c->t;
328 struct got_repository *repo = t->repo;
329 struct querystring *qs = t->qs;
330 struct repo_dir *repo_dir = t->repo_dir;
331 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
332 int chk_next = 0, chk_multi = 0;
336 if (qs->file != NULL && strlen(qs->file) > 0)
337 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
339 return got_error_from_errno("asprintf");
341 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
342 repo_dir->name) == -1) {
343 error = got_error_from_errno("asprintf");
348 error = got_repo_match_object_id_prefix(&id, qs->commit,
349 GOT_OBJ_TYPE_COMMIT, repo);
353 error = got_ref_open(&ref, repo, qs->headref, 0);
357 error = got_ref_resolve(&id, repo, ref);
362 error = got_repo_map_path(&in_repo_path, repo, repo_path);
366 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
370 if (qs->file != NULL && strlen(qs->file) > 0) {
371 error = got_commit_graph_open(&graph, file_path, 0);
375 error = got_commit_graph_open(&graph, in_repo_path, 0);
380 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
385 struct got_object_id next_id;
387 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
390 if (error->code == GOT_ERR_ITER_COMPLETED)
395 error = got_object_open_as_commit(&commit, repo, &next_id);
399 error = got_init_repo_commit(&repo_commit);
403 error = got_get_repo_commit(c, repo_commit, commit,
406 gotweb_free_repo_commit(repo_commit);
410 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
412 if (!chk_multi || limit != 1 ||
413 srv->max_commits_display == 1) {
417 * check for one more commit before breaking,
418 * so we know whether to navigate through briefs
419 * commits and summary
421 if (chk_next && (qs->action == BRIEFS ||
422 qs->action == COMMITS || qs->action == SUMMARY)) {
423 t->more_id = strdup(repo_commit->commit_id);
424 if (t->more_id == NULL) {
425 error = got_error_from_errno("strdup");
428 got_object_commit_close(commit);
430 TAILQ_REMOVE(&t->repo_commits, repo_commit,
432 gotweb_free_repo_commit(repo_commit);
436 if (error || (limit && --limit == 0)) {
437 if (qs->file != NULL && *qs->file != '\0')
443 got_object_commit_close(commit);
451 got_object_commit_close(commit);
453 got_commit_graph_close(graph);
454 got_ref_list_free(&refs);
462 const struct got_error *
463 got_get_repo_tags(struct request *c, int limit)
465 const struct got_error *error = NULL;
466 struct got_object_id *id = NULL;
467 struct got_commit_object *commit = NULL;
468 struct got_reflist_head refs;
469 struct got_reference *ref;
470 struct got_reflist_entry *re;
471 struct server *srv = c->srv;
472 struct transport *t = c->t;
473 struct got_repository *repo = t->repo;
474 struct querystring *qs = t->qs;
475 struct repo_dir *repo_dir = t->repo_dir;
476 struct got_tag_object *tag = NULL;
477 struct repo_tag *rt = NULL, *trt = NULL;
478 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
479 char *tag_commit = NULL, *tag_commit0 = NULL;
480 char *commit_msg = NULL, *commit_msg0 = NULL;
481 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
485 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
486 repo_dir->name) == -1)
487 return got_error_from_errno("asprintf");
489 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
490 error = got_ref_open(&ref, repo, qs->headref, 0);
493 error = got_ref_resolve(&id, repo, ref);
497 } else if (qs->commit == NULL && qs->action == TAG) {
498 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
501 error = got_repo_match_object_id_prefix(&id, qs->commit,
502 GOT_OBJ_TYPE_COMMIT, repo);
507 if (qs->action != SUMMARY && qs->action != TAGS) {
508 error = got_object_open_as_commit(&commit, repo, id);
511 error = got_object_commit_get_logmsg(&commit_msg0, commit);
515 got_object_commit_close(commit);
520 error = got_repo_map_path(&in_repo_path, repo, repo_path);
524 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
533 * XXX: again, see previous message about caching
536 TAILQ_FOREACH(re, &refs, entry) {
537 struct repo_tag *new_repo_tag = NULL;
538 error = got_init_repo_tag(&new_repo_tag);
542 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
544 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
545 if (new_repo_tag->tag_name == NULL) {
546 error = got_error_from_errno("strdup");
556 error = got_ref_resolve(&id, repo, re->ref);
561 got_object_tag_close(tag);
562 error = got_object_open_as_tag(&tag, repo, id);
564 if (error->code != GOT_ERR_OBJ_TYPE)
566 /* "lightweight" tag */
567 error = got_object_open_as_commit(&commit, repo, id);
570 new_repo_tag->tagger =
571 strdup(got_object_commit_get_committer(commit));
572 if (new_repo_tag->tagger == NULL) {
573 error = got_error_from_errno("strdup");
576 new_repo_tag->tagger_time =
577 got_object_commit_get_committer_time(commit);
578 error = got_object_id_str(&id_str, id);
582 new_repo_tag->tagger =
583 strdup(got_object_tag_get_tagger(tag));
584 if (new_repo_tag->tagger == NULL) {
585 error = got_error_from_errno("strdup");
588 new_repo_tag->tagger_time =
589 got_object_tag_get_tagger_time(tag);
590 error = got_object_id_str(&id_str,
591 got_object_tag_get_object_id(tag));
596 new_repo_tag->commit_id = strdup(id_str);
597 if (new_repo_tag->commit_id == NULL)
600 if (commit_found == 0 && qs->commit != NULL &&
601 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
609 * check for one more commit before breaking,
610 * so we know whether to navigate through briefs
611 * commits and summary
614 t->next_id = strdup(new_repo_tag->commit_id);
615 if (t->next_id == NULL) {
616 error = got_error_from_errno("strdup");
620 got_object_commit_close(commit);
623 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
624 gotweb_free_repo_tag(new_repo_tag);
629 error = got_object_commit_get_logmsg(&tag_commit0,
633 got_object_commit_close(commit);
636 tag_commit0 = strdup(got_object_tag_get_message(tag));
637 if (tag_commit0 == NULL) {
638 error = got_error_from_errno("strdup");
643 tag_commit = tag_commit0;
644 while (*tag_commit == '\n')
646 new_repo_tag->tag_commit = strdup(tag_commit);
647 if (new_repo_tag->tag_commit == NULL) {
648 error = got_error_from_errno("strdup");
654 if (qs->action != SUMMARY && qs->action != TAGS) {
655 commit_msg = commit_msg0;
656 while (*commit_msg == '\n')
659 new_repo_tag->commit_msg = strdup(commit_msg);
660 if (new_repo_tag->commit_msg == NULL) {
661 error = got_error_from_errno("strdup");
666 if (limit && --limit == 0) {
675 * we have tailq populated, so find previous commit id
676 * for navigation through briefs and commits
678 if (t->tag_count == 0) {
679 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
680 TAILQ_REMOVE(&t->repo_tags, rt, entry);
681 gotweb_free_repo_tag(rt);
684 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
686 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
688 if (commit_found == 0 && rt->commit_id != NULL &&
689 strcmp(qs->commit, rt->commit_id) != 0) {
693 if (c_cnt == srv->max_commits_display ||
694 rt == TAILQ_FIRST(&t->repo_tags)) {
695 t->prev_id = strdup(rt->commit_id);
696 if (t->prev_id == NULL)
697 error = got_error_from_errno("strdup");
705 got_object_commit_close(commit);
707 got_object_tag_close(tag);
708 got_ref_list_free(&refs);
718 got_output_repo_tree(struct request *c,
719 int (*cb)(struct template *, struct got_tree_entry *))
721 const struct got_error *error = NULL;
722 struct transport *t = c->t;
723 struct got_commit_object *commit = NULL;
724 struct got_repository *repo = t->repo;
725 struct querystring *qs = t->qs;
726 struct repo_commit *rc = NULL;
727 struct got_object_id *tree_id = NULL, *commit_id = NULL;
728 struct got_reflist_head refs;
729 struct got_tree_object *tree = NULL;
730 struct got_tree_entry *te;
731 struct repo_dir *repo_dir = t->repo_dir;
732 char *escaped_name = NULL, *path = NULL;
737 rc = TAILQ_FIRST(&t->repo_commits);
739 if (qs->folder != NULL) {
740 path = strdup(qs->folder);
742 error = got_error_from_errno("strdup");
746 error = got_repo_map_path(&path, repo, repo_dir->path);
751 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
752 GOT_OBJ_TYPE_COMMIT, &refs, repo);
756 error = got_object_open_as_commit(&commit, repo, commit_id);
760 error = got_object_id_by_path(&tree_id, repo, commit, path);
764 error = got_object_open_as_tree(&tree, repo, tree_id);
768 nentries = got_object_tree_get_nentries(tree);
770 for (i = 0; i < nentries; i++) {
771 te = got_object_tree_get_entry(tree, i);
772 if (cb(c->tp, te) == -1) {
773 error = got_error(GOT_ERR_CANCELLED);
780 got_ref_list_free(&refs);
782 got_object_commit_close(commit);
784 got_object_tree_close(tree);
788 log_warnx("%s: %s", __func__, error->msg);
794 const struct got_error *
795 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
796 int *binary, struct request *c)
798 const struct got_error *error = NULL;
799 struct transport *t = c->t;
800 struct got_repository *repo = t->repo;
801 struct querystring *qs = c->t->qs;
802 struct got_commit_object *commit = NULL;
803 struct got_object_id *commit_id = NULL;
804 struct got_reflist_head refs;
805 char *path = NULL, *in_repo_path = NULL;
814 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
815 qs->folder ? "/" : "", qs->file) == -1) {
816 error = got_error_from_errno("asprintf");
820 error = got_repo_map_path(&in_repo_path, repo, path);
824 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
825 GOT_OBJ_TYPE_COMMIT, &refs, repo);
829 error = got_object_open_as_commit(&commit, repo, commit_id);
833 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
837 if (commit_id == NULL) {
838 error = got_error(GOT_ERR_NO_OBJ);
842 error = got_object_get_type(&obj_type, repo, commit_id);
846 if (obj_type != GOT_OBJ_TYPE_BLOB) {
847 error = got_error(GOT_ERR_OBJ_TYPE);
851 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
855 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
859 error = got_object_blob_is_binary(binary, *blob);
865 got_object_commit_close(commit);
871 got_object_blob_close(*blob);
883 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
884 int (*cb)(struct template *, const char *, size_t))
886 const struct got_error *err;
893 err = got_object_blob_getline(&line, &linelen, &linesize,
895 if (err || linelen == -1)
898 if (cb(tp, line, lineno) == -1) {
899 err = got_error(GOT_ERR_CANCELLED);
907 log_warnx("%s: got_object_blob_getline failed: %s",
914 struct blame_cb_args {
915 struct blame_line *lines;
921 struct got_repository *repo;
923 got_render_blame_line_cb cb;
926 static const struct got_error *
927 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
928 struct got_commit_object *commit, struct got_object_id *id)
930 const struct got_error *err = NULL;
931 struct blame_cb_args *a = arg;
932 struct blame_line *bline;
933 struct request *c = a->c;
938 time_t committer_time;
940 if (nlines != a->nlines ||
941 (lineno != -1 && lineno < 1) || lineno > a->nlines)
942 return got_error(GOT_ERR_RANGE);
945 return NULL; /* no change in this commit */
947 /* Annotate this line. */
948 bline = &a->lines[lineno - 1];
949 if (bline->annotated)
951 err = got_object_id_str(&bline->id_str, id);
955 bline->committer = strdup(got_object_commit_get_committer(commit));
956 if (bline->committer == NULL) {
957 err = got_error_from_errno("strdup");
961 committer_time = got_object_commit_get_committer_time(commit);
962 if (gmtime_r(&committer_time, &tm) == NULL)
963 return got_error_from_errno("gmtime_r");
964 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
966 err = got_error(GOT_ERR_NO_SPACE);
969 bline->annotated = 1;
971 /* Print lines annotated so far. */
972 bline = &a->lines[a->lineno_cur - 1];
973 if (!bline->annotated)
976 offset = a->line_offsets[a->lineno_cur - 1];
977 if (fseeko(a->f, offset, SEEK_SET) == -1) {
978 err = got_error_from_errno("fseeko");
982 while (a->lineno_cur <= a->nlines && bline->annotated) {
983 if (getline(&line, &linesize, a->f) == -1) {
985 err = got_error_from_errno("getline");
989 if (a->cb(c->tp, line, bline, a->nlines_prec,
990 a->lineno_cur) == -1) {
991 err = got_error(GOT_ERR_CANCELLED);
996 bline = &a->lines[a->lineno_cur - 1];
1003 const struct got_error *
1004 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1006 const struct got_error *error = NULL;
1007 struct transport *t = c->t;
1008 struct got_repository *repo = t->repo;
1009 struct querystring *qs = c->t->qs;
1010 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1011 struct got_commit_object *commit = NULL;
1012 struct got_reflist_head refs;
1013 struct got_blob_object *blob = NULL;
1014 char *path = NULL, *in_repo_path = NULL;
1015 struct blame_cb_args bca;
1016 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1019 FILE *f1 = NULL, *f2 = NULL;
1026 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1027 qs->folder ? "/" : "", qs->file) == -1) {
1028 error = got_error_from_errno("asprintf");
1032 error = got_repo_map_path(&in_repo_path, repo, path);
1036 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1037 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1041 error = got_object_open_as_commit(&commit, repo, commit_id);
1045 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1049 error = got_object_get_type(&obj_type, repo, obj_id);
1053 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1054 error = got_error(GOT_ERR_OBJ_TYPE);
1058 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1062 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1066 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1070 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1071 &bca.line_offsets, bca.f, blob);
1072 if (error || bca.nlines == 0)
1075 /* Don't include \n at EOF in the blame line count. */
1076 if (bca.line_offsets[bca.nlines - 1] == filesize)
1079 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1080 if (bca.lines == NULL) {
1081 error = got_error_from_errno("calloc");
1085 bca.nlines_prec = 0;
1094 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1098 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1102 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1106 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1110 error = got_blame(in_repo_path, commit_id, repo,
1111 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1116 free(bca.line_offsets);
1117 for (i = 0; i < bca.nlines; i++) {
1118 struct blame_line *bline = &bca.lines[i];
1119 free(bline->id_str);
1120 free(bline->committer);
1124 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1125 error = got_error_from_errno("close");
1126 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1127 error = got_error_from_errno("close");
1128 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1129 error = got_error_from_errno("close");
1131 const struct got_error *bca_err =
1132 got_gotweb_flushfile(bca.f, fd1);
1137 const struct got_error *f1_err =
1138 got_gotweb_flushfile(f1, fd5);
1143 const struct got_error *f2_err =
1144 got_gotweb_flushfile(f2, fd6);
1149 got_object_commit_close(commit);
1151 got_object_blob_close(blob);
1156 got_ref_list_free(&refs);
1160 const struct got_error *
1161 got_open_diff_for_output(FILE **fp, int *fd, struct request *c)
1163 const struct got_error *error = NULL;
1164 struct transport *t = c->t;
1165 struct got_repository *repo = t->repo;
1166 struct repo_commit *rc = NULL;
1167 struct got_object_id *id1 = NULL, *id2 = NULL;
1168 struct got_reflist_head refs;
1169 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1170 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1177 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1181 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1185 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1189 rc = TAILQ_FIRST(&t->repo_commits);
1191 if (rc->parent_id != NULL &&
1192 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1193 error = got_repo_match_object_id(&id1, NULL,
1194 rc->parent_id, GOT_OBJ_TYPE_ANY,
1200 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1201 GOT_OBJ_TYPE_ANY, &refs, repo);
1205 error = got_object_get_type(&obj_type, repo, id2);
1209 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1213 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1218 case GOT_OBJ_TYPE_BLOB:
1219 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1220 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1223 case GOT_OBJ_TYPE_TREE:
1224 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1225 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1228 case GOT_OBJ_TYPE_COMMIT:
1229 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1230 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1234 error = got_error(GOT_ERR_OBJ_TYPE);
1239 if (fseek(f1, 0, SEEK_SET) == -1) {
1240 error = got_ferror(f1, GOT_ERR_IO);
1244 if (fseek(f2, 0, SEEK_SET) == -1) {
1245 error = got_ferror(f2, GOT_ERR_IO);
1249 if (fseek(f3, 0, SEEK_SET) == -1) {
1250 error = got_ferror(f3, GOT_ERR_IO);
1258 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1259 error = got_error_from_errno("close");
1260 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1261 error = got_error_from_errno("close");
1263 const struct got_error *f1_err =
1264 got_gotweb_flushfile(f1, fd1);
1269 const struct got_error *f2_err =
1270 got_gotweb_flushfile(f2, fd2);
1275 got_gotweb_flushfile(f3, fd3);
1279 got_ref_list_free(&refs);
1285 static const struct got_error *
1286 got_init_repo_commit(struct repo_commit **rc)
1288 *rc = calloc(1, sizeof(**rc));
1290 return got_error_from_errno2("%s: calloc", __func__);
1293 (*rc)->refs_str = NULL;
1294 (*rc)->commit_id = NULL;
1295 (*rc)->committer = NULL;
1296 (*rc)->author = NULL;
1297 (*rc)->parent_id = NULL;
1298 (*rc)->tree_id = NULL;
1299 (*rc)->commit_msg = NULL;
1304 static const struct got_error *
1305 got_init_repo_tag(struct repo_tag **rt)
1307 *rt = calloc(1, sizeof(**rt));
1309 return got_error_from_errno2("%s: calloc", __func__);
1311 (*rt)->commit_id = NULL;
1312 (*rt)->tag_name = NULL;
1313 (*rt)->tag_commit = NULL;
1314 (*rt)->commit_msg = NULL;
1315 (*rt)->tagger = NULL;