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 *, 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)
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");
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");
76 static const struct got_error *
77 got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
81 return got_error_from_errno("dup");
83 *f = fdopen(*fd, "w+");
86 return got_error(GOT_ERR_PRIVSEP_NO_FD);
92 static const struct got_error *
93 got_gotweb_dupfd(int *priv_fd, int *fd)
98 return got_error_from_errno("dup");
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;
113 if (srv->show_repo_owner == 0)
116 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
117 if (gitconfig_owner) {
118 *owner = strdup(gitconfig_owner);
120 return got_error_from_errno("strdup");
124 return got_error_from_errno("strdup");
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;
143 if (srv->show_repo_age == 0)
146 error = got_ref_list(&refs, repo, "refs/heads",
147 got_ref_cmp_by_name, NULL);
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.
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)
161 error = got_ref_resolve(&id, repo, re->ref);
165 error = got_object_open_as_commit(&commit, repo, id);
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;
181 *repo_age = cmp_time;
183 got_ref_list_free(&refs);
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) {
203 struct got_tag_object *tag = NULL;
204 struct got_object_id *ref_id;
207 if (got_ref_is_symbolic(re->ref))
210 name = got_ref_get_name(re->ref);
211 if (strncmp(name, "refs/", 5) == 0)
213 if (strncmp(name, "got/", 4) == 0)
215 if (strncmp(name, "heads/", 6) == 0)
217 if (strncmp(name, "remotes/", 8) == 0) {
219 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
222 error = got_ref_resolve(&ref_id, t->repo, re->ref);
225 if (strncmp(name, "tags/", 5) == 0) {
226 error = got_object_open_as_tag(&tag, t->repo, ref_id);
228 if (error->code != GOT_ERR_OBJ_TYPE) {
233 * Ref points at something other
240 cmp = got_object_id_cmp(tag ?
241 got_object_tag_get_object_id(tag) : ref_id, id);
244 got_object_tag_close(tag);
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");
252 repo_commit->refs_str = NULL;
258 error = got_object_id_str(&repo_commit->commit_id, id);
262 error = got_object_id_str(&repo_commit->tree_id,
263 got_object_commit_get_tree_id(commit));
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);
277 repo_commit->parent_id = strdup("/dev/null");
278 if (repo_commit->parent_id == NULL) {
279 error = got_error_from_errno("strdup");
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");
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");
300 error = got_object_commit_get_logmsg(&commit_msg0, commit);
304 commit_msg = commit_msg0;
305 while (*commit_msg == '\n')
308 repo_commit->commit_msg = strdup(commit_msg);
309 if (repo_commit->commit_msg == NULL)
310 error = got_error_from_errno("strdup");
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;
335 if (qs->file != NULL && strlen(qs->file) > 0)
336 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
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");
347 error = got_repo_match_object_id_prefix(&id, qs->commit,
348 GOT_OBJ_TYPE_COMMIT, repo);
352 error = got_ref_open(&ref, repo, qs->headref, 0);
356 error = got_ref_resolve(&id, repo, ref);
361 error = got_repo_map_path(&in_repo_path, repo, repo_path);
365 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
369 if (qs->file != NULL && strlen(qs->file) > 0) {
370 error = got_commit_graph_open(&graph, file_path, 0);
374 error = got_commit_graph_open(&graph, in_repo_path, 0);
379 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
384 struct got_object_id next_id;
386 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
389 if (error->code == GOT_ERR_ITER_COMPLETED)
394 error = got_object_open_as_commit(&commit, repo, &next_id);
398 error = got_init_repo_commit(&repo_commit);
402 error = got_get_repo_commit(c, repo_commit, commit,
405 gotweb_free_repo_commit(repo_commit);
409 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
411 if (!chk_multi || limit != 1 ||
412 srv->max_commits_display == 1) {
416 * check for one more commit before breaking,
417 * so we know whether to navigate through briefs
418 * commits and summary
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");
427 got_object_commit_close(commit);
429 TAILQ_REMOVE(&t->repo_commits, repo_commit,
431 gotweb_free_repo_commit(repo_commit);
435 if (error || (limit && --limit == 0)) {
436 if (qs->file != NULL && *qs->file != '\0')
442 got_object_commit_close(commit);
450 got_object_commit_close(commit);
452 got_commit_graph_close(graph);
453 got_ref_list_free(&refs);
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;
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);
492 error = got_ref_resolve(&id, repo, ref);
496 } else if (qs->commit == NULL && qs->action == TAG) {
497 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
500 error = got_repo_match_object_id_prefix(&id, qs->commit,
501 GOT_OBJ_TYPE_COMMIT, repo);
506 if (qs->action != SUMMARY && qs->action != TAGS) {
507 error = got_object_open_as_commit(&commit, repo, id);
510 error = got_object_commit_get_logmsg(&commit_msg0, commit);
514 got_object_commit_close(commit);
519 error = got_repo_map_path(&in_repo_path, repo, repo_path);
523 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
532 * XXX: again, see previous message about caching
535 TAILQ_FOREACH(re, &refs, entry) {
536 struct repo_tag *new_repo_tag = NULL;
537 error = got_init_repo_tag(&new_repo_tag);
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");
555 error = got_ref_resolve(&id, repo, re->ref);
560 got_object_tag_close(tag);
561 error = got_object_open_as_tag(&tag, repo, id);
563 if (error->code != GOT_ERR_OBJ_TYPE)
565 /* "lightweight" tag */
566 error = got_object_open_as_commit(&commit, repo, id);
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");
575 new_repo_tag->tagger_time =
576 got_object_commit_get_committer_time(commit);
577 error = got_object_id_str(&id_str, id);
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");
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));
595 new_repo_tag->commit_id = strdup(id_str);
596 if (new_repo_tag->commit_id == NULL)
599 if (commit_found == 0 && qs->commit != NULL &&
600 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
608 * check for one more commit before breaking,
609 * so we know whether to navigate through briefs
610 * commits and summary
613 t->next_id = strdup(new_repo_tag->commit_id);
614 if (t->next_id == NULL) {
615 error = got_error_from_errno("strdup");
619 got_object_commit_close(commit);
622 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
623 gotweb_free_repo_tag(new_repo_tag);
628 error = got_object_commit_get_logmsg(&tag_commit0,
632 got_object_commit_close(commit);
635 tag_commit0 = strdup(got_object_tag_get_message(tag));
636 if (tag_commit0 == NULL) {
637 error = got_error_from_errno("strdup");
642 tag_commit = tag_commit0;
643 while (*tag_commit == '\n')
645 new_repo_tag->tag_commit = strdup(tag_commit);
646 if (new_repo_tag->tag_commit == NULL) {
647 error = got_error_from_errno("strdup");
653 if (qs->action != SUMMARY && qs->action != TAGS) {
654 commit_msg = commit_msg0;
655 while (*commit_msg == '\n')
658 new_repo_tag->commit_msg = strdup(commit_msg);
659 if (new_repo_tag->commit_msg == NULL) {
660 error = got_error_from_errno("strdup");
665 if (limit && --limit == 0) {
674 * we have tailq populated, so find previous commit id
675 * for navigation through briefs and commits
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) {
685 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
687 if (commit_found == 0 && rt->commit_id != NULL &&
688 strcmp(qs->commit, rt->commit_id) != 0) {
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");
704 got_object_commit_close(commit);
706 got_object_tag_close(tag);
707 got_ref_list_free(&refs);
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;
736 rc = TAILQ_FIRST(&t->repo_commits);
738 if (qs->folder != NULL) {
739 path = strdup(qs->folder);
741 error = got_error_from_errno("strdup");
745 error = got_repo_map_path(&path, repo, repo_dir->path);
750 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
751 GOT_OBJ_TYPE_COMMIT, &refs, repo);
755 error = got_object_open_as_commit(&commit, repo, commit_id);
759 error = got_object_id_by_path(&tree_id, repo, commit, path);
763 error = got_object_open_as_tree(&tree, repo, tree_id);
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)
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)
902 log_warnx("%s: got_object_blob_getline failed: %s",
909 struct blame_cb_args {
910 struct blame_line *lines;
916 struct got_repository *repo;
918 got_render_blame_line_cb cb;
921 static const struct got_error *
922 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
923 struct got_commit_object *commit, struct got_object_id *id)
925 const struct got_error *err = NULL;
926 struct blame_cb_args *a = arg;
927 struct blame_line *bline;
928 struct request *c = a->c;
933 time_t committer_time;
935 if (nlines != a->nlines ||
936 (lineno != -1 && lineno < 1) || lineno > a->nlines)
937 return got_error(GOT_ERR_RANGE);
940 return NULL; /* no change in this commit */
942 /* Annotate this line. */
943 bline = &a->lines[lineno - 1];
944 if (bline->annotated)
946 err = got_object_id_str(&bline->id_str, id);
950 bline->committer = strdup(got_object_commit_get_committer(commit));
951 if (bline->committer == NULL) {
952 err = got_error_from_errno("strdup");
956 committer_time = got_object_commit_get_committer_time(commit);
957 if (gmtime_r(&committer_time, &tm) == NULL)
958 return got_error_from_errno("gmtime_r");
959 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
961 err = got_error(GOT_ERR_NO_SPACE);
964 bline->annotated = 1;
966 /* Print lines annotated so far. */
967 bline = &a->lines[a->lineno_cur - 1];
968 if (!bline->annotated)
971 offset = a->line_offsets[a->lineno_cur - 1];
972 if (fseeko(a->f, offset, SEEK_SET) == -1) {
973 err = got_error_from_errno("fseeko");
977 while (a->lineno_cur <= a->nlines && bline->annotated) {
978 if (getline(&line, &linesize, a->f) == -1) {
980 err = got_error_from_errno("getline");
984 if (a->cb(c->tp, line, bline, a->nlines_prec,
985 a->lineno_cur) == -1)
989 bline = &a->lines[a->lineno_cur - 1];
996 const struct got_error *
997 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
999 const struct got_error *error = NULL;
1000 struct transport *t = c->t;
1001 struct got_repository *repo = t->repo;
1002 struct querystring *qs = c->t->qs;
1003 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1004 struct got_commit_object *commit = NULL;
1005 struct got_reflist_head refs;
1006 struct got_blob_object *blob = NULL;
1007 char *path = NULL, *in_repo_path = NULL;
1008 struct blame_cb_args bca;
1009 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1012 FILE *f1 = NULL, *f2 = NULL;
1019 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1020 qs->folder ? "/" : "", qs->file) == -1) {
1021 error = got_error_from_errno("asprintf");
1025 error = got_repo_map_path(&in_repo_path, repo, path);
1029 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1030 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1034 error = got_object_open_as_commit(&commit, repo, commit_id);
1038 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1042 error = got_object_get_type(&obj_type, repo, obj_id);
1046 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1047 error = got_error(GOT_ERR_OBJ_TYPE);
1051 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1055 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1059 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1063 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1064 &bca.line_offsets, bca.f, blob);
1065 if (error || bca.nlines == 0)
1068 /* Don't include \n at EOF in the blame line count. */
1069 if (bca.line_offsets[bca.nlines - 1] == filesize)
1072 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1073 if (bca.lines == NULL) {
1074 error = got_error_from_errno("calloc");
1078 bca.nlines_prec = 0;
1087 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1091 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1095 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1099 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1103 error = got_blame(in_repo_path, commit_id, repo,
1104 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1109 free(bca.line_offsets);
1110 for (i = 0; i < bca.nlines; i++) {
1111 struct blame_line *bline = &bca.lines[i];
1112 free(bline->id_str);
1113 free(bline->committer);
1117 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1118 error = got_error_from_errno("close");
1119 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1120 error = got_error_from_errno("close");
1121 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1122 error = got_error_from_errno("close");
1124 const struct got_error *bca_err =
1125 got_gotweb_flushfile(bca.f, fd1);
1130 const struct got_error *f1_err =
1131 got_gotweb_flushfile(f1, fd5);
1136 const struct got_error *f2_err =
1137 got_gotweb_flushfile(f2, fd6);
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, int *fd, struct request *c)
1156 const struct got_error *error = NULL;
1157 struct transport *t = c->t;
1158 struct got_repository *repo = t->repo;
1159 struct repo_commit *rc = NULL;
1160 struct got_object_id *id1 = NULL, *id2 = NULL;
1161 struct got_reflist_head refs;
1162 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1163 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1170 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1174 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1178 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1182 rc = TAILQ_FIRST(&t->repo_commits);
1184 if (rc->parent_id != NULL &&
1185 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1186 error = got_repo_match_object_id(&id1, NULL,
1187 rc->parent_id, GOT_OBJ_TYPE_ANY,
1193 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1194 GOT_OBJ_TYPE_ANY, &refs, repo);
1198 error = got_object_get_type(&obj_type, repo, id2);
1202 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1206 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1211 case GOT_OBJ_TYPE_BLOB:
1212 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1213 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1216 case GOT_OBJ_TYPE_TREE:
1217 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1218 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1221 case GOT_OBJ_TYPE_COMMIT:
1222 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1223 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1227 error = got_error(GOT_ERR_OBJ_TYPE);
1232 if (fseek(f1, 0, SEEK_SET) == -1) {
1233 error = got_ferror(f1, GOT_ERR_IO);
1237 if (fseek(f2, 0, SEEK_SET) == -1) {
1238 error = got_ferror(f2, GOT_ERR_IO);
1242 if (fseek(f3, 0, SEEK_SET) == -1) {
1243 error = got_ferror(f3, GOT_ERR_IO);
1251 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1252 error = got_error_from_errno("close");
1253 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1254 error = got_error_from_errno("close");
1256 const struct got_error *f1_err =
1257 got_gotweb_flushfile(f1, fd1);
1262 const struct got_error *f2_err =
1263 got_gotweb_flushfile(f2, fd2);
1268 got_gotweb_flushfile(f3, fd3);
1272 got_ref_list_free(&refs);
1278 static const struct got_error *
1279 got_init_repo_commit(struct repo_commit **rc)
1281 *rc = calloc(1, sizeof(**rc));
1283 return got_error_from_errno2("%s: calloc", __func__);
1286 (*rc)->refs_str = NULL;
1287 (*rc)->commit_id = NULL;
1288 (*rc)->committer = NULL;
1289 (*rc)->author = NULL;
1290 (*rc)->parent_id = NULL;
1291 (*rc)->tree_id = NULL;
1292 (*rc)->commit_msg = NULL;
1297 static const struct got_error *
1298 got_init_repo_tag(struct repo_tag **rt)
1300 *rt = calloc(1, sizeof(**rt));
1302 return got_error_from_errno2("%s: calloc", __func__);
1304 (*rt)->commit_id = NULL;
1305 (*rt)->tag_name = NULL;
1306 (*rt)->tag_commit = NULL;
1307 (*rt)->commit_msg = NULL;
1308 (*rt)->tagger = NULL;