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 "got_compat.h"
20 #include <sys/queue.h>
21 #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 server *srv = c->srv;
132 struct transport *t = c->t;
133 struct got_repository *repo = t->repo;
134 struct got_commit_object *commit = NULL;
135 struct got_reflist_head refs;
136 struct got_reflist_entry *re;
137 time_t committer_time = 0, cmp_time = 0;
141 if (srv->show_repo_age == 0)
144 error = got_ref_list(&refs, repo, "refs/heads",
145 got_ref_cmp_by_name, NULL);
150 * Find the youngest branch tip in the repository, or the age of
151 * the a specific branch tip if a name was provided by the caller.
153 TAILQ_FOREACH(re, &refs, entry) {
154 struct got_object_id *id = NULL;
156 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
159 error = got_ref_resolve(&id, repo, re->ref);
163 error = got_object_open_as_commit(&commit, repo, id);
169 got_object_commit_get_committer_time(commit);
170 got_object_commit_close(commit);
171 if (cmp_time < committer_time)
172 cmp_time = committer_time;
179 *repo_age = cmp_time;
181 got_ref_list_free(&refs);
185 static const struct got_error *
186 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
187 struct got_commit_object *commit, struct got_reflist_head *refs,
188 struct got_object_id *id)
190 const struct got_error *error = NULL;
191 struct got_reflist_entry *re;
192 struct got_object_id *id2 = NULL;
193 struct got_object_qid *parent_id;
194 struct transport *t = c->t;
195 struct querystring *qs = c->t->qs;
196 char *commit_msg = NULL, *commit_msg0;
198 TAILQ_FOREACH(re, refs, entry) {
201 struct got_tag_object *tag = NULL;
202 struct got_object_id *ref_id;
205 if (got_ref_is_symbolic(re->ref))
208 name = got_ref_get_name(re->ref);
209 if (strncmp(name, "refs/", 5) == 0)
211 if (strncmp(name, "got/", 4) == 0)
213 if (strncmp(name, "heads/", 6) == 0)
215 if (strncmp(name, "remotes/", 8) == 0) {
217 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
220 error = got_ref_resolve(&ref_id, t->repo, re->ref);
223 if (strncmp(name, "tags/", 5) == 0) {
224 error = got_object_open_as_tag(&tag, t->repo, ref_id);
226 if (error->code != GOT_ERR_OBJ_TYPE) {
231 * Ref points at something other
238 cmp = got_object_id_cmp(tag ?
239 got_object_tag_get_object_id(tag) : ref_id, id);
242 got_object_tag_close(tag);
245 s = repo_commit->refs_str;
246 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
247 s ? ", " : "", name) == -1) {
248 error = got_error_from_errno("asprintf");
250 repo_commit->refs_str = NULL;
256 error = got_object_id_str(&repo_commit->commit_id, id);
260 error = got_object_id_str(&repo_commit->tree_id,
261 got_object_commit_get_tree_id(commit));
265 if (qs->action == DIFF) {
266 parent_id = STAILQ_FIRST(
267 got_object_commit_get_parent_ids(commit));
268 if (parent_id != NULL) {
269 id2 = got_object_id_dup(&parent_id->id);
270 error = got_object_id_str(&repo_commit->parent_id, id2);
275 repo_commit->parent_id = strdup("/dev/null");
276 if (repo_commit->parent_id == NULL) {
277 error = got_error_from_errno("strdup");
283 repo_commit->committer_time =
284 got_object_commit_get_committer_time(commit);
286 repo_commit->author =
287 strdup(got_object_commit_get_author(commit));
288 if (repo_commit->author == NULL) {
289 error = got_error_from_errno("strdup");
292 repo_commit->committer =
293 strdup(got_object_commit_get_committer(commit));
294 if (repo_commit->committer == NULL) {
295 error = got_error_from_errno("strdup");
298 error = got_object_commit_get_logmsg(&commit_msg0, commit);
302 commit_msg = commit_msg0;
303 while (*commit_msg == '\n')
306 repo_commit->commit_msg = strdup(commit_msg);
307 if (repo_commit->commit_msg == NULL)
308 error = got_error_from_errno("strdup");
313 const struct got_error *
314 got_get_repo_commits(struct request *c, int limit)
316 const struct got_error *error = NULL;
317 struct got_object_id *id = NULL;
318 struct got_commit_graph *graph = NULL;
319 struct got_commit_object *commit = NULL;
320 struct got_reflist_head refs;
321 struct got_reference *ref = NULL;
322 struct repo_commit *repo_commit = NULL;
323 struct server *srv = c->srv;
324 struct transport *t = c->t;
325 struct got_repository *repo = t->repo;
326 struct querystring *qs = t->qs;
327 struct repo_dir *repo_dir = t->repo_dir;
328 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
329 int chk_next = 0, chk_multi = 0;
333 if (qs->file != NULL && strlen(qs->file) > 0)
334 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
336 return got_error_from_errno("asprintf");
338 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
339 repo_dir->name) == -1) {
340 error = got_error_from_errno("asprintf");
345 error = got_repo_match_object_id_prefix(&id, qs->commit,
346 GOT_OBJ_TYPE_COMMIT, repo);
350 error = got_ref_open(&ref, repo, qs->headref, 0);
354 error = got_ref_resolve(&id, repo, ref);
359 error = got_repo_map_path(&in_repo_path, repo, repo_path);
363 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
367 if (qs->file != NULL && strlen(qs->file) > 0) {
368 error = got_commit_graph_open(&graph, file_path, 0);
372 error = got_commit_graph_open(&graph, in_repo_path, 0);
377 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
382 struct got_object_id next_id;
384 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
387 if (error->code == GOT_ERR_ITER_COMPLETED)
392 error = got_object_open_as_commit(&commit, repo, &next_id);
396 error = got_init_repo_commit(&repo_commit);
400 error = got_get_repo_commit(c, repo_commit, commit,
403 gotweb_free_repo_commit(repo_commit);
407 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
409 if (!chk_multi || limit != 1 ||
410 srv->max_commits_display == 1) {
414 * check for one more commit before breaking,
415 * so we know whether to navigate through briefs
416 * commits and summary
418 if (chk_next && (qs->action == BRIEFS ||
419 qs->action == COMMITS || qs->action == SUMMARY)) {
420 t->more_id = strdup(repo_commit->commit_id);
421 if (t->more_id == NULL) {
422 error = got_error_from_errno("strdup");
425 got_object_commit_close(commit);
427 TAILQ_REMOVE(&t->repo_commits, repo_commit,
429 gotweb_free_repo_commit(repo_commit);
433 if (error || (limit && --limit == 0)) {
434 if (qs->file != NULL && *qs->file != '\0')
440 got_object_commit_close(commit);
448 got_object_commit_close(commit);
450 got_commit_graph_close(graph);
451 got_ref_list_free(&refs);
459 const struct got_error *
460 got_get_repo_tags(struct request *c, int limit)
462 const struct got_error *error = NULL;
463 struct got_object_id *id = NULL;
464 struct got_commit_object *commit = NULL;
465 struct got_reflist_head refs;
466 struct got_reference *ref;
467 struct got_reflist_entry *re;
468 struct server *srv = c->srv;
469 struct transport *t = c->t;
470 struct got_repository *repo = t->repo;
471 struct querystring *qs = t->qs;
472 struct repo_dir *repo_dir = t->repo_dir;
473 struct got_tag_object *tag = NULL;
474 struct repo_tag *rt = NULL, *trt = NULL;
475 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
476 char *tag_commit = NULL, *tag_commit0 = NULL;
477 char *commit_msg = NULL, *commit_msg0 = NULL;
478 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
482 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
483 repo_dir->name) == -1)
484 return got_error_from_errno("asprintf");
486 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
487 error = got_ref_open(&ref, repo, qs->headref, 0);
490 error = got_ref_resolve(&id, repo, ref);
494 } else if (qs->commit == NULL && qs->action == TAG) {
495 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
498 error = got_repo_match_object_id_prefix(&id, qs->commit,
499 GOT_OBJ_TYPE_COMMIT, repo);
504 if (qs->action != SUMMARY && qs->action != TAGS) {
505 error = got_object_open_as_commit(&commit, repo, id);
508 error = got_object_commit_get_logmsg(&commit_msg0, commit);
512 got_object_commit_close(commit);
517 error = got_repo_map_path(&in_repo_path, repo, repo_path);
521 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
530 * XXX: again, see previous message about caching
533 TAILQ_FOREACH(re, &refs, entry) {
534 struct repo_tag *new_repo_tag = NULL;
535 error = got_init_repo_tag(&new_repo_tag);
539 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
541 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
542 if (new_repo_tag->tag_name == NULL) {
543 error = got_error_from_errno("strdup");
553 error = got_ref_resolve(&id, repo, re->ref);
558 got_object_tag_close(tag);
559 error = got_object_open_as_tag(&tag, repo, id);
561 if (error->code != GOT_ERR_OBJ_TYPE)
563 /* "lightweight" tag */
564 error = got_object_open_as_commit(&commit, repo, id);
567 new_repo_tag->tagger =
568 strdup(got_object_commit_get_committer(commit));
569 if (new_repo_tag->tagger == NULL) {
570 error = got_error_from_errno("strdup");
573 new_repo_tag->tagger_time =
574 got_object_commit_get_committer_time(commit);
575 error = got_object_id_str(&id_str, id);
579 new_repo_tag->tagger =
580 strdup(got_object_tag_get_tagger(tag));
581 if (new_repo_tag->tagger == NULL) {
582 error = got_error_from_errno("strdup");
585 new_repo_tag->tagger_time =
586 got_object_tag_get_tagger_time(tag);
587 error = got_object_id_str(&id_str,
588 got_object_tag_get_object_id(tag));
593 new_repo_tag->commit_id = strdup(id_str);
594 if (new_repo_tag->commit_id == NULL)
597 if (commit_found == 0 && qs->commit != NULL &&
598 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
606 * check for one more commit before breaking,
607 * so we know whether to navigate through briefs
608 * commits and summary
611 t->next_id = strdup(new_repo_tag->commit_id);
612 if (t->next_id == NULL) {
613 error = got_error_from_errno("strdup");
617 got_object_commit_close(commit);
620 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
621 gotweb_free_repo_tag(new_repo_tag);
626 error = got_object_commit_get_logmsg(&tag_commit0,
630 got_object_commit_close(commit);
633 tag_commit0 = strdup(got_object_tag_get_message(tag));
634 if (tag_commit0 == NULL) {
635 error = got_error_from_errno("strdup");
640 tag_commit = tag_commit0;
641 while (*tag_commit == '\n')
643 new_repo_tag->tag_commit = strdup(tag_commit);
644 if (new_repo_tag->tag_commit == NULL) {
645 error = got_error_from_errno("strdup");
651 if (qs->action != SUMMARY && qs->action != TAGS) {
652 commit_msg = commit_msg0;
653 while (*commit_msg == '\n')
656 new_repo_tag->commit_msg = strdup(commit_msg);
657 if (new_repo_tag->commit_msg == NULL) {
658 error = got_error_from_errno("strdup");
663 if (limit && --limit == 0) {
672 * we have tailq populated, so find previous commit id
673 * for navigation through briefs and commits
675 if (t->tag_count == 0) {
676 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
677 TAILQ_REMOVE(&t->repo_tags, rt, entry);
678 gotweb_free_repo_tag(rt);
681 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
683 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
685 if (commit_found == 0 && rt->commit_id != NULL &&
686 strcmp(qs->commit, rt->commit_id) != 0) {
690 if (c_cnt == srv->max_commits_display ||
691 rt == TAILQ_FIRST(&t->repo_tags)) {
692 t->prev_id = strdup(rt->commit_id);
693 if (t->prev_id == NULL)
694 error = got_error_from_errno("strdup");
702 got_object_commit_close(commit);
704 got_object_tag_close(tag);
705 got_ref_list_free(&refs);
715 got_output_repo_tree(struct request *c,
716 int (*cb)(struct template *, struct got_tree_entry *))
718 const struct got_error *error = NULL;
719 struct transport *t = c->t;
720 struct got_commit_object *commit = NULL;
721 struct got_repository *repo = t->repo;
722 struct querystring *qs = t->qs;
723 struct repo_commit *rc = NULL;
724 struct got_object_id *tree_id = NULL, *commit_id = NULL;
725 struct got_reflist_head refs;
726 struct got_tree_object *tree = NULL;
727 struct got_tree_entry *te;
728 struct repo_dir *repo_dir = t->repo_dir;
729 char *escaped_name = NULL, *path = NULL;
734 rc = TAILQ_FIRST(&t->repo_commits);
736 if (qs->folder != NULL) {
737 path = strdup(qs->folder);
739 error = got_error_from_errno("strdup");
743 error = got_repo_map_path(&path, repo, repo_dir->path);
748 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
749 GOT_OBJ_TYPE_COMMIT, &refs, repo);
753 error = got_object_open_as_commit(&commit, repo, commit_id);
757 error = got_object_id_by_path(&tree_id, repo, commit, path);
761 error = got_object_open_as_tree(&tree, repo, tree_id);
765 nentries = got_object_tree_get_nentries(tree);
767 for (i = 0; i < nentries; i++) {
768 te = got_object_tree_get_entry(tree, i);
769 if (cb(c->tp, te) == -1) {
770 error = got_error(GOT_ERR_CANCELLED);
777 got_ref_list_free(&refs);
779 got_object_commit_close(commit);
781 got_object_tree_close(tree);
785 log_warnx("%s: %s", __func__, error->msg);
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;
811 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
812 qs->folder ? "/" : "", qs->file) == -1) {
813 error = got_error_from_errno("asprintf");
817 error = got_repo_map_path(&in_repo_path, repo, path);
821 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
822 GOT_OBJ_TYPE_COMMIT, &refs, repo);
826 error = got_object_open_as_commit(&commit, repo, commit_id);
830 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
834 if (commit_id == NULL) {
835 error = got_error(GOT_ERR_NO_OBJ);
839 error = got_object_get_type(&obj_type, repo, commit_id);
843 if (obj_type != GOT_OBJ_TYPE_BLOB) {
844 error = got_error(GOT_ERR_OBJ_TYPE);
848 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
852 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
856 error = got_object_blob_is_binary(binary, *blob);
862 got_object_commit_close(commit);
868 got_object_blob_close(*blob);
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;
890 err = got_object_blob_getline(&line, &linelen, &linesize,
892 if (err || linelen == -1)
895 if (cb(tp, line, lineno) == -1) {
896 err = got_error(GOT_ERR_CANCELLED);
904 log_warnx("%s: got_object_blob_getline failed: %s",
911 struct blame_cb_args {
912 struct blame_line *lines;
918 struct got_repository *repo;
920 got_render_blame_line_cb cb;
923 static const struct got_error *
924 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
925 struct got_commit_object *commit, struct got_object_id *id)
927 const struct got_error *err = NULL;
928 struct blame_cb_args *a = arg;
929 struct blame_line *bline;
930 struct request *c = a->c;
935 time_t committer_time;
937 if (nlines != a->nlines ||
938 (lineno != -1 && lineno < 1) || lineno > a->nlines)
939 return got_error(GOT_ERR_RANGE);
942 return NULL; /* no change in this commit */
944 /* Annotate this line. */
945 bline = &a->lines[lineno - 1];
946 if (bline->annotated)
948 err = got_object_id_str(&bline->id_str, id);
952 bline->committer = strdup(got_object_commit_get_committer(commit));
953 if (bline->committer == NULL) {
954 err = got_error_from_errno("strdup");
958 committer_time = got_object_commit_get_committer_time(commit);
959 if (gmtime_r(&committer_time, &tm) == NULL)
960 return got_error_from_errno("gmtime_r");
961 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
963 err = got_error(GOT_ERR_NO_SPACE);
966 bline->annotated = 1;
968 /* Print lines annotated so far. */
969 bline = &a->lines[a->lineno_cur - 1];
970 if (!bline->annotated)
973 offset = a->line_offsets[a->lineno_cur - 1];
974 if (fseeko(a->f, offset, SEEK_SET) == -1) {
975 err = got_error_from_errno("fseeko");
979 while (a->lineno_cur <= a->nlines && bline->annotated) {
980 if (getline(&line, &linesize, a->f) == -1) {
982 err = got_error_from_errno("getline");
986 if (a->cb(c->tp, line, bline, a->nlines_prec,
987 a->lineno_cur) == -1) {
988 err = got_error(GOT_ERR_CANCELLED);
993 bline = &a->lines[a->lineno_cur - 1];
1000 const struct got_error *
1001 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1003 const struct got_error *error = NULL;
1004 struct transport *t = c->t;
1005 struct got_repository *repo = t->repo;
1006 struct querystring *qs = c->t->qs;
1007 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1008 struct got_commit_object *commit = NULL;
1009 struct got_reflist_head refs;
1010 struct got_blob_object *blob = NULL;
1011 char *path = NULL, *in_repo_path = NULL;
1012 struct blame_cb_args bca;
1013 int i, obj_type, blobfd = -1, fd1 = -1, fd2 = -1;
1015 FILE *f1 = NULL, *f2 = NULL;
1022 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1023 qs->folder ? "/" : "", qs->file) == -1) {
1024 error = got_error_from_errno("asprintf");
1028 error = got_repo_map_path(&in_repo_path, repo, path);
1032 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1033 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1037 error = got_object_open_as_commit(&commit, repo, commit_id);
1041 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1045 error = got_object_get_type(&obj_type, repo, obj_id);
1049 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1050 error = got_error(GOT_ERR_OBJ_TYPE);
1054 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1]);
1058 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &blobfd);
1062 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, blobfd);
1066 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1067 &bca.line_offsets, bca.f, blob);
1068 if (error || bca.nlines == 0)
1071 /* Don't include \n at EOF in the blame line count. */
1072 if (bca.line_offsets[bca.nlines - 1] == filesize)
1075 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1076 if (bca.lines == NULL) {
1077 error = got_error_from_errno("calloc");
1081 bca.nlines_prec = 0;
1090 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd1);
1094 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd2);
1098 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5]);
1102 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6]);
1106 error = got_blame(in_repo_path, commit_id, repo,
1107 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1112 free(bca.line_offsets);
1113 for (i = 0; i < bca.nlines; i++) {
1114 struct blame_line *bline = &bca.lines[i];
1115 free(bline->id_str);
1116 free(bline->committer);
1120 if (blobfd != -1 && close(blobfd) == -1 && error == NULL)
1121 error = got_error_from_errno("close");
1122 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1123 error = got_error_from_errno("close");
1124 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1125 error = got_error_from_errno("close");
1127 const struct got_error *bca_err = got_gotweb_closefile(bca.f);
1132 const struct got_error *f1_err = got_gotweb_closefile(f1);
1137 const struct got_error *f2_err = got_gotweb_closefile(f2);
1142 got_object_commit_close(commit);
1144 got_object_blob_close(blob);
1149 got_ref_list_free(&refs);
1153 const struct got_error *
1154 got_open_diff_for_output(FILE **fp, 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 = -1, fd2 = -1;
1169 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1]);
1173 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2]);
1177 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3]);
1181 rc = TAILQ_FIRST(&t->repo_commits);
1183 if (rc->parent_id != NULL &&
1184 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1185 error = got_repo_match_object_id(&id1, NULL,
1186 rc->parent_id, GOT_OBJ_TYPE_ANY,
1192 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1193 GOT_OBJ_TYPE_ANY, &refs, repo);
1197 error = got_object_get_type(&obj_type, repo, id2);
1201 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd1);
1205 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd2);
1210 case GOT_OBJ_TYPE_BLOB:
1211 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd1, fd2,
1212 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1215 case GOT_OBJ_TYPE_TREE:
1216 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
1217 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1220 case GOT_OBJ_TYPE_COMMIT:
1221 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd1,
1222 fd2, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1226 error = got_error(GOT_ERR_OBJ_TYPE);
1231 if (fseek(f1, 0, SEEK_SET) == -1) {
1232 error = got_ferror(f1, GOT_ERR_IO);
1236 if (fseek(f2, 0, SEEK_SET) == -1) {
1237 error = got_ferror(f2, GOT_ERR_IO);
1241 if (fseek(f3, 0, SEEK_SET) == -1) {
1242 error = got_ferror(f3, GOT_ERR_IO);
1249 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1250 error = got_error_from_errno("close");
1251 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1252 error = got_error_from_errno("close");
1254 const struct got_error *f1_err = got_gotweb_closefile(f1);
1259 const struct got_error *f2_err = got_gotweb_closefile(f2);
1264 got_gotweb_closefile(f3);
1267 got_ref_list_free(&refs);
1273 static const struct got_error *
1274 got_init_repo_commit(struct repo_commit **rc)
1276 *rc = calloc(1, sizeof(**rc));
1278 return got_error_from_errno2("%s: calloc", __func__);
1281 (*rc)->refs_str = NULL;
1282 (*rc)->commit_id = NULL;
1283 (*rc)->committer = NULL;
1284 (*rc)->author = NULL;
1285 (*rc)->parent_id = NULL;
1286 (*rc)->tree_id = NULL;
1287 (*rc)->commit_msg = NULL;
1292 static const struct got_error *
1293 got_init_repo_tag(struct repo_tag **rt)
1295 *rt = calloc(1, sizeof(**rt));
1297 return got_error_from_errno2("%s: calloc", __func__);
1299 (*rt)->commit_id = NULL;
1300 (*rt)->tag_name = NULL;
1301 (*rt)->tag_commit = NULL;
1302 (*rt)->commit_msg = NULL;
1303 (*rt)->tagger = NULL;