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>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_reference.h"
34 #include "got_repository.h"
36 #include "got_cancel.h"
38 #include "got_commit_graph.h"
39 #include "got_blame.h"
40 #include "got_privsep.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 *);
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_closefile(FILE *f)
58 const struct got_error *err = NULL;
60 if (fseek(f, 0, SEEK_SET) == -1)
61 err = got_error_from_errno("fseek");
63 if (err == NULL && ftruncate(fileno(f), 0) == -1)
64 err = got_error_from_errno("ftruncate");
66 if (fclose(f) == EOF && err == NULL)
67 err = got_error_from_errno("fclose");
72 static const struct got_error *
73 got_gotweb_openfile(FILE **f, int *priv_fd)
79 return got_error_from_errno("dup");
81 *f = fdopen(fd, "w+");
84 return got_error(GOT_ERR_PRIVSEP_NO_FD);
90 static const struct got_error *
91 got_gotweb_dupfd(int *priv_fd, int *fd)
96 return got_error_from_errno("dup");
101 const struct got_error *
102 got_get_repo_owner(char **owner, struct request *c)
104 struct server *srv = c->srv;
105 struct transport *t = c->t;
106 struct got_repository *repo = t->repo;
107 const char *gitconfig_owner;
111 if (srv->show_repo_owner == 0)
114 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
115 if (gitconfig_owner) {
116 *owner = strdup(gitconfig_owner);
118 return got_error_from_errno("strdup");
122 return got_error_from_errno("strdup");
127 const struct got_error *
128 got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
130 const struct got_error *error = NULL;
131 struct transport *t = c->t;
132 struct got_repository *repo = t->repo;
133 struct got_commit_object *commit = NULL;
134 struct got_reflist_head refs;
135 struct got_reflist_entry *re;
136 time_t committer_time = 0, cmp_time = 0;
142 error = got_ref_list(&refs, repo, "refs/heads",
143 got_ref_cmp_by_name, NULL);
148 * Find the youngest branch tip in the repository, or the age of
149 * the a specific branch tip if a name was provided by the caller.
151 TAILQ_FOREACH(re, &refs, entry) {
152 struct got_object_id *id = NULL;
154 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
157 error = got_ref_resolve(&id, repo, re->ref);
161 error = got_object_open_as_commit(&commit, repo, id);
167 got_object_commit_get_committer_time(commit);
168 got_object_commit_close(commit);
169 if (cmp_time < committer_time)
170 cmp_time = committer_time;
177 *repo_age = cmp_time;
179 got_ref_list_free(&refs);
183 static const struct got_error *
184 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
185 struct got_commit_object *commit, struct got_reflist_head *refs,
186 struct got_object_id *id)
188 const struct got_error *error = NULL;
189 struct got_reflist_entry *re;
190 struct got_object_id *id2 = NULL;
191 struct got_object_qid *parent_id;
192 struct transport *t = c->t;
193 struct querystring *qs = c->t->qs;
194 char *commit_msg = NULL, *commit_msg0;
196 TAILQ_FOREACH(re, refs, entry) {
199 struct got_tag_object *tag = NULL;
200 struct got_object_id *ref_id;
203 if (got_ref_is_symbolic(re->ref))
206 name = got_ref_get_name(re->ref);
207 if (strncmp(name, "refs/", 5) == 0)
209 if (strncmp(name, "got/", 4) == 0)
211 if (strncmp(name, "heads/", 6) == 0)
213 if (strncmp(name, "remotes/", 8) == 0) {
215 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
218 error = got_ref_resolve(&ref_id, t->repo, re->ref);
221 if (strncmp(name, "tags/", 5) == 0) {
222 error = got_object_open_as_tag(&tag, t->repo, ref_id);
224 if (error->code != GOT_ERR_OBJ_TYPE) {
229 * Ref points at something other
236 cmp = got_object_id_cmp(tag ?
237 got_object_tag_get_object_id(tag) : ref_id, id);
240 got_object_tag_close(tag);
243 s = repo_commit->refs_str;
244 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
245 s ? ", " : "", name) == -1) {
246 error = got_error_from_errno("asprintf");
248 repo_commit->refs_str = NULL;
254 error = got_object_id_str(&repo_commit->commit_id, id);
258 error = got_object_id_str(&repo_commit->tree_id,
259 got_object_commit_get_tree_id(commit));
263 if (qs->action == DIFF) {
264 parent_id = STAILQ_FIRST(
265 got_object_commit_get_parent_ids(commit));
266 if (parent_id != NULL) {
267 id2 = got_object_id_dup(&parent_id->id);
268 error = got_object_id_str(&repo_commit->parent_id, id2);
273 repo_commit->parent_id = strdup("/dev/null");
274 if (repo_commit->parent_id == NULL) {
275 error = got_error_from_errno("strdup");
281 repo_commit->committer_time =
282 got_object_commit_get_committer_time(commit);
284 repo_commit->author =
285 strdup(got_object_commit_get_author(commit));
286 if (repo_commit->author == NULL) {
287 error = got_error_from_errno("strdup");
290 repo_commit->committer =
291 strdup(got_object_commit_get_committer(commit));
292 if (repo_commit->committer == NULL) {
293 error = got_error_from_errno("strdup");
296 error = got_object_commit_get_logmsg(&commit_msg0, commit);
300 commit_msg = commit_msg0;
301 while (*commit_msg == '\n')
304 repo_commit->commit_msg = strdup(commit_msg);
305 if (repo_commit->commit_msg == NULL)
306 error = got_error_from_errno("strdup");
311 const struct got_error *
312 got_get_repo_commits(struct request *c, size_t limit)
314 const struct got_error *error = NULL;
315 struct got_object_id *id = NULL;
316 struct got_commit_graph *graph = NULL;
317 struct got_commit_object *commit = NULL;
318 struct got_reflist_head refs;
319 struct got_reference *ref = NULL;
320 struct repo_commit *repo_commit = NULL;
321 struct server *srv = c->srv;
322 struct transport *t = c->t;
323 struct got_repository *repo = t->repo;
324 struct querystring *qs = t->qs;
325 struct repo_dir *repo_dir = t->repo_dir;
326 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
330 return got_error(GOT_ERR_RANGE);
334 * Traverse one commit more than requested to provide
343 if (qs->file != NULL && *qs->file != '\0')
344 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
346 return got_error_from_errno("asprintf");
348 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
349 repo_dir->name) == -1) {
350 error = got_error_from_errno("asprintf");
355 error = got_repo_match_object_id_prefix(&id, qs->commit,
356 GOT_OBJ_TYPE_COMMIT, repo);
360 error = got_ref_open(&ref, repo, qs->headref, 0);
364 error = got_ref_resolve(&id, repo, ref);
369 error = got_repo_map_path(&in_repo_path, repo, repo_path);
373 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
377 if (qs->file != NULL && *qs->file != '\0') {
378 error = got_commit_graph_open(&graph, file_path, 0);
382 error = got_commit_graph_open(&graph, in_repo_path, 0);
387 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
392 struct got_object_id next_id;
394 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
397 if (error->code == GOT_ERR_ITER_COMPLETED)
402 error = got_object_open_as_commit(&commit, repo, &next_id);
406 error = got_init_repo_commit(&repo_commit);
410 error = got_get_repo_commit(c, repo_commit, commit,
413 gotweb_free_repo_commit(repo_commit);
417 if (--limit == 0 && chk_next) {
418 t->more_id = strdup(repo_commit->commit_id);
419 if (t->more_id == NULL)
420 error = got_error_from_errno("strdup");
424 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
430 got_object_commit_close(commit);
438 got_object_commit_close(commit);
440 got_commit_graph_close(graph);
441 got_ref_list_free(&refs);
449 const struct got_error *
450 got_get_repo_tags(struct request *c, size_t limit)
452 const struct got_error *error = NULL;
453 struct got_object_id *id = NULL;
454 struct got_commit_object *commit = NULL;
455 struct got_reflist_head refs;
456 struct got_reference *ref;
457 struct got_reflist_entry *re;
458 struct server *srv = c->srv;
459 struct transport *t = c->t;
460 struct got_repository *repo = t->repo;
461 struct querystring *qs = t->qs;
462 struct repo_dir *repo_dir = t->repo_dir;
463 struct got_tag_object *tag = NULL;
464 struct repo_tag *rt = NULL, *trt = NULL;
465 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
466 char *tag_commit = NULL, *tag_commit0 = NULL;
467 char *commit_msg = NULL, *commit_msg0 = NULL;
468 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
473 return got_error(GOT_ERR_RANGE);
475 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
476 repo_dir->name) == -1)
477 return got_error_from_errno("asprintf");
479 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
480 error = got_ref_open(&ref, repo, qs->headref, 0);
483 error = got_ref_resolve(&id, repo, ref);
487 } else if (qs->commit == NULL && qs->action == TAG) {
488 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
491 error = got_repo_match_object_id_prefix(&id, qs->commit,
492 GOT_OBJ_TYPE_COMMIT, repo);
497 if (qs->action != SUMMARY && qs->action != TAGS) {
498 error = got_object_open_as_commit(&commit, repo, id);
501 error = got_object_commit_get_logmsg(&commit_msg0, commit);
505 got_object_commit_close(commit);
510 error = got_repo_map_path(&in_repo_path, repo, repo_path);
514 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
523 * XXX: again, see previous message about caching
526 TAILQ_FOREACH(re, &refs, entry) {
527 struct repo_tag *new_repo_tag = NULL;
528 error = got_init_repo_tag(&new_repo_tag);
532 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
534 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
535 if (new_repo_tag->tag_name == NULL) {
536 error = got_error_from_errno("strdup");
546 error = got_ref_resolve(&id, repo, re->ref);
551 got_object_tag_close(tag);
552 error = got_object_open_as_tag(&tag, repo, id);
554 if (error->code != GOT_ERR_OBJ_TYPE)
556 /* "lightweight" tag */
557 error = got_object_open_as_commit(&commit, repo, id);
560 new_repo_tag->tagger =
561 strdup(got_object_commit_get_committer(commit));
562 if (new_repo_tag->tagger == NULL) {
563 error = got_error_from_errno("strdup");
566 new_repo_tag->tagger_time =
567 got_object_commit_get_committer_time(commit);
568 error = got_object_id_str(&id_str, id);
572 new_repo_tag->tagger =
573 strdup(got_object_tag_get_tagger(tag));
574 if (new_repo_tag->tagger == NULL) {
575 error = got_error_from_errno("strdup");
578 new_repo_tag->tagger_time =
579 got_object_tag_get_tagger_time(tag);
580 error = got_object_id_str(&id_str,
581 got_object_tag_get_object_id(tag));
586 new_repo_tag->commit_id = strdup(id_str);
587 if (new_repo_tag->commit_id == NULL)
590 if (commit_found == 0 && qs->commit != NULL &&
591 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
599 * check for one more commit before breaking,
600 * so we know whether to navigate through briefs
601 * commits and summary
604 t->next_id = strdup(new_repo_tag->commit_id);
605 if (t->next_id == NULL) {
606 error = got_error_from_errno("strdup");
610 got_object_commit_close(commit);
613 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
614 gotweb_free_repo_tag(new_repo_tag);
619 error = got_object_commit_get_logmsg(&tag_commit0,
623 got_object_commit_close(commit);
626 tag_commit0 = strdup(got_object_tag_get_message(tag));
627 if (tag_commit0 == NULL) {
628 error = got_error_from_errno("strdup");
633 tag_commit = tag_commit0;
634 while (*tag_commit == '\n')
636 new_repo_tag->tag_commit = strdup(tag_commit);
637 if (new_repo_tag->tag_commit == NULL) {
638 error = got_error_from_errno("strdup");
644 if (qs->action != SUMMARY && qs->action != TAGS) {
645 commit_msg = commit_msg0;
646 while (*commit_msg == '\n')
649 new_repo_tag->commit_msg = strdup(commit_msg);
650 if (new_repo_tag->commit_msg == NULL) {
651 error = got_error_from_errno("strdup");
656 if (limit && --limit == 0) {
665 * we have tailq populated, so find previous commit id
666 * for navigation through briefs and commits
668 if (t->tag_count == 0) {
669 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
670 TAILQ_REMOVE(&t->repo_tags, rt, entry);
671 gotweb_free_repo_tag(rt);
674 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
676 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
678 if (commit_found == 0 && rt->commit_id != NULL &&
679 strcmp(qs->commit, rt->commit_id) != 0) {
683 if (c_cnt == srv->max_commits_display ||
684 rt == TAILQ_FIRST(&t->repo_tags)) {
685 t->prev_id = strdup(rt->commit_id);
686 if (t->prev_id == NULL)
687 error = got_error_from_errno("strdup");
695 got_object_commit_close(commit);
697 got_object_tag_close(tag);
698 got_ref_list_free(&refs);
708 got_output_repo_tree(struct request *c,
709 int (*cb)(struct template *, struct got_tree_entry *))
711 const struct got_error *error = NULL;
712 struct transport *t = c->t;
713 struct got_commit_object *commit = NULL;
714 struct got_repository *repo = t->repo;
715 struct querystring *qs = t->qs;
716 struct repo_commit *rc = NULL;
717 struct got_object_id *tree_id = NULL, *commit_id = NULL;
718 struct got_reflist_head refs;
719 struct got_tree_object *tree = NULL;
720 struct got_tree_entry *te;
721 struct repo_dir *repo_dir = t->repo_dir;
722 char *escaped_name = NULL, *path = NULL;
727 rc = TAILQ_FIRST(&t->repo_commits);
729 if (qs->folder != NULL) {
730 path = strdup(qs->folder);
732 error = got_error_from_errno("strdup");
736 error = got_repo_map_path(&path, repo, repo_dir->path);
741 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
742 GOT_OBJ_TYPE_COMMIT, &refs, repo);
746 error = got_object_open_as_commit(&commit, repo, commit_id);
750 error = got_object_id_by_path(&tree_id, repo, commit, path);
754 error = got_object_open_as_tree(&tree, repo, tree_id);
758 nentries = got_object_tree_get_nentries(tree);
760 for (i = 0; i < nentries; i++) {
761 te = got_object_tree_get_entry(tree, i);
762 if (cb(c->tp, te) == -1) {
763 error = got_error(GOT_ERR_CANCELLED);
770 got_ref_list_free(&refs);
772 got_object_commit_close(commit);
774 got_object_tree_close(tree);
778 if (error->code != GOT_ERR_CANCELLED)
779 log_warnx("%s: %s", __func__, error->msg);
785 const struct got_error *
786 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
787 int *binary, struct request *c)
789 const struct got_error *error = NULL;
790 struct transport *t = c->t;
791 struct got_repository *repo = t->repo;
792 struct querystring *qs = c->t->qs;
793 struct got_commit_object *commit = NULL;
794 struct got_object_id *commit_id = NULL;
795 struct got_reflist_head refs;
796 char *path = NULL, *in_repo_path = NULL;
805 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
806 qs->folder ? "/" : "", qs->file) == -1) {
807 error = got_error_from_errno("asprintf");
811 error = got_repo_map_path(&in_repo_path, repo, path);
815 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
816 GOT_OBJ_TYPE_COMMIT, &refs, repo);
820 error = got_object_open_as_commit(&commit, repo, commit_id);
824 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
828 if (commit_id == NULL) {
829 error = got_error(GOT_ERR_NO_OBJ);
833 error = got_object_get_type(&obj_type, repo, commit_id);
837 if (obj_type != GOT_OBJ_TYPE_BLOB) {
838 error = got_error(GOT_ERR_OBJ_TYPE);
842 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
846 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
850 error = got_object_blob_is_binary(binary, *blob);
856 got_object_commit_close(commit);
862 got_object_blob_close(*blob);
874 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
875 int (*cb)(struct template *, const char *, size_t))
877 const struct got_error *err;
884 err = got_object_blob_getline(&line, &linelen, &linesize,
886 if (err || linelen == -1)
889 if (cb(tp, line, lineno) == -1) {
890 err = got_error(GOT_ERR_CANCELLED);
898 if (err->code != GOT_ERR_CANCELLED)
899 log_warnx("%s: got_object_blob_getline failed: %s",
906 struct blame_cb_args {
907 struct blame_line *lines;
913 struct got_repository *repo;
915 got_render_blame_line_cb cb;
918 static const struct got_error *
919 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
920 struct got_commit_object *commit, struct got_object_id *id)
922 const struct got_error *err = NULL;
923 struct blame_cb_args *a = arg;
924 struct blame_line *bline;
925 struct request *c = a->c;
930 time_t committer_time;
932 if (nlines != a->nlines ||
933 (lineno != -1 && lineno < 1) || lineno > a->nlines)
934 return got_error(GOT_ERR_RANGE);
937 return NULL; /* no change in this commit */
939 /* Annotate this line. */
940 bline = &a->lines[lineno - 1];
941 if (bline->annotated)
943 err = got_object_id_str(&bline->id_str, id);
947 bline->committer = strdup(got_object_commit_get_committer(commit));
948 if (bline->committer == NULL) {
949 err = got_error_from_errno("strdup");
953 committer_time = got_object_commit_get_committer_time(commit);
954 if (gmtime_r(&committer_time, &tm) == NULL)
955 return got_error_from_errno("gmtime_r");
956 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
958 err = got_error(GOT_ERR_NO_SPACE);
961 bline->annotated = 1;
963 /* Print lines annotated so far. */
964 bline = &a->lines[a->lineno_cur - 1];
965 if (!bline->annotated)
968 offset = a->line_offsets[a->lineno_cur - 1];
969 if (fseeko(a->f, offset, SEEK_SET) == -1) {
970 err = got_error_from_errno("fseeko");
974 while (a->lineno_cur <= a->nlines && bline->annotated) {
975 if (getline(&line, &linesize, a->f) == -1) {
977 err = got_error_from_errno("getline");
981 if (a->cb(c->tp, line, bline, a->nlines_prec,
982 a->lineno_cur) == -1) {
983 err = got_error(GOT_ERR_CANCELLED);
988 bline = &a->lines[a->lineno_cur - 1];
995 const struct got_error *
996 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
998 const struct got_error *error = NULL;
999 struct transport *t = c->t;
1000 struct got_repository *repo = t->repo;
1001 struct querystring *qs = c->t->qs;
1002 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1003 struct got_commit_object *commit = NULL;
1004 struct got_reflist_head refs;
1005 struct got_blob_object *blob = NULL;
1006 char *path = NULL, *in_repo_path = NULL;
1007 struct blame_cb_args bca;
1008 int i, obj_type, blobfd = -1, fd1 = -1, fd2 = -1;
1010 FILE *f1 = NULL, *f2 = NULL;
1017 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1018 qs->folder ? "/" : "", qs->file) == -1) {
1019 error = got_error_from_errno("asprintf");
1023 error = got_repo_map_path(&in_repo_path, repo, path);
1027 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1028 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1032 error = got_object_open_as_commit(&commit, repo, commit_id);
1036 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1040 error = got_object_get_type(&obj_type, repo, obj_id);
1044 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1045 error = got_error(GOT_ERR_OBJ_TYPE);
1049 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1]);
1053 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &blobfd);
1057 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, blobfd);
1061 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1062 &bca.line_offsets, bca.f, blob);
1063 if (error || bca.nlines == 0)
1066 /* Don't include \n at EOF in the blame line count. */
1067 if (bca.line_offsets[bca.nlines - 1] == filesize)
1070 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1071 if (bca.lines == NULL) {
1072 error = got_error_from_errno("calloc");
1076 bca.nlines_prec = 0;
1085 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd1);
1089 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd2);
1093 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5]);
1097 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6]);
1101 error = got_blame(in_repo_path, commit_id, repo,
1102 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1107 free(bca.line_offsets);
1108 for (i = 0; i < bca.nlines; i++) {
1109 struct blame_line *bline = &bca.lines[i];
1110 free(bline->id_str);
1111 free(bline->committer);
1115 if (blobfd != -1 && close(blobfd) == -1 && error == NULL)
1116 error = got_error_from_errno("close");
1117 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1118 error = got_error_from_errno("close");
1119 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1120 error = got_error_from_errno("close");
1122 const struct got_error *bca_err = got_gotweb_closefile(bca.f);
1127 const struct got_error *f1_err = got_gotweb_closefile(f1);
1132 const struct got_error *f2_err = got_gotweb_closefile(f2);
1137 got_object_commit_close(commit);
1139 got_object_blob_close(blob);
1144 got_ref_list_free(&refs);
1148 const struct got_error *
1149 got_open_diff_for_output(FILE **fp, struct request *c)
1151 const struct got_error *error = NULL;
1152 struct transport *t = c->t;
1153 struct got_repository *repo = t->repo;
1154 struct repo_commit *rc = NULL;
1155 struct got_object_id *id1 = NULL, *id2 = NULL;
1156 struct got_reflist_head refs;
1157 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1158 int obj_type, fd1 = -1, fd2 = -1;
1164 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1]);
1168 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2]);
1172 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3]);
1176 rc = TAILQ_FIRST(&t->repo_commits);
1178 if (rc->parent_id != NULL &&
1179 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1180 error = got_repo_match_object_id(&id1, NULL,
1181 rc->parent_id, GOT_OBJ_TYPE_ANY,
1187 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1188 GOT_OBJ_TYPE_ANY, &refs, repo);
1192 error = got_object_get_type(&obj_type, repo, id2);
1196 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd1);
1200 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd2);
1205 case GOT_OBJ_TYPE_BLOB:
1206 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd1, fd2,
1207 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1210 case GOT_OBJ_TYPE_TREE:
1211 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
1212 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1215 case GOT_OBJ_TYPE_COMMIT:
1216 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd1,
1217 fd2, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1221 error = got_error(GOT_ERR_OBJ_TYPE);
1226 if (fseek(f3, 0, SEEK_SET) == -1) {
1227 error = got_ferror(f3, GOT_ERR_IO);
1234 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1235 error = got_error_from_errno("close");
1236 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1237 error = got_error_from_errno("close");
1239 const struct got_error *f1_err = got_gotweb_closefile(f1);
1244 const struct got_error *f2_err = got_gotweb_closefile(f2);
1249 got_gotweb_closefile(f3);
1252 got_ref_list_free(&refs);
1258 static const struct got_error *
1259 got_init_repo_commit(struct repo_commit **rc)
1261 *rc = calloc(1, sizeof(**rc));
1263 return got_error_from_errno2(__func__, "calloc");
1266 (*rc)->refs_str = NULL;
1267 (*rc)->commit_id = NULL;
1268 (*rc)->committer = NULL;
1269 (*rc)->author = NULL;
1270 (*rc)->parent_id = NULL;
1271 (*rc)->tree_id = NULL;
1272 (*rc)->commit_msg = NULL;
1277 static const struct got_error *
1278 got_init_repo_tag(struct repo_tag **rt)
1280 *rt = calloc(1, sizeof(**rt));
1282 return got_error_from_errno2(__func__, "calloc");
1284 (*rt)->commit_id = NULL;
1285 (*rt)->tag_name = NULL;
1286 (*rt)->tag_commit = NULL;
1287 (*rt)->commit_msg = NULL;
1288 (*rt)->tagger = NULL;