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>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_reference.h"
31 #include "got_repository.h"
33 #include "got_cancel.h"
35 #include "got_commit_graph.h"
36 #include "got_blame.h"
37 #include "got_privsep.h"
39 #include "got_compat.h"
44 static const struct got_error *got_init_repo_commit(struct repo_commit **);
45 static const struct got_error *got_init_repo_tag(struct repo_tag **);
46 static const struct got_error *got_get_repo_commit(struct request *,
47 struct repo_commit *, struct got_commit_object *, struct got_reflist_head *,
48 struct got_object_id *);
49 static const struct got_error *got_gotweb_dupfd(int *, int *);
50 static const struct got_error *got_gotweb_openfile(FILE **, int *, int *);
51 static const struct got_error *got_gotweb_blame_cb(void *, int, int,
52 struct got_commit_object *,struct got_object_id *);
54 const struct got_error *
55 got_gotweb_flushfile(FILE *f, int fd)
57 if (fseek(f, 0, SEEK_SET) == -1)
58 return got_error_from_errno("fseek");
60 if (ftruncate(fd, 0) == -1)
61 return got_error_from_errno("ftruncate");
64 return got_error_from_errno("fsync");
66 if (f && fclose(f) == EOF)
67 return got_error_from_errno("fclose");
69 if (fd != -1 && close(fd) != -1)
70 return got_error_from_errno("close");
75 static const struct got_error *
76 got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
80 return got_error_from_errno("dup");
82 *f = fdopen(*fd, "w+");
85 return got_error(GOT_ERR_PRIVSEP_NO_FD);
91 static const struct got_error *
92 got_gotweb_dupfd(int *priv_fd, int *fd)
102 const struct got_error *
103 got_get_repo_owner(char **owner, struct request *c)
105 struct server *srv = c->srv;
106 struct transport *t = c->t;
107 struct got_repository *repo = t->repo;
108 const char *gitconfig_owner;
112 if (srv->show_repo_owner == 0)
115 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
116 if (gitconfig_owner) {
117 *owner = strdup(gitconfig_owner);
119 return got_error_from_errno("strdup");
123 return got_error_from_errno("strdup");
128 const struct got_error *
129 got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
131 const struct got_error *error = NULL;
132 struct server *srv = c->srv;
133 struct transport *t = c->t;
134 struct got_repository *repo = t->repo;
135 struct got_commit_object *commit = NULL;
136 struct got_reflist_head refs;
137 struct got_reflist_entry *re;
138 time_t committer_time = 0, cmp_time = 0;
142 if (srv->show_repo_age == 0)
145 error = got_ref_list(&refs, repo, "refs/heads",
146 got_ref_cmp_by_name, NULL);
151 * Find the youngest branch tip in the repository, or the age of
152 * the a specific branch tip if a name was provided by the caller.
154 TAILQ_FOREACH(re, &refs, entry) {
155 struct got_object_id *id = NULL;
157 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
160 error = got_ref_resolve(&id, repo, re->ref);
164 error = got_object_open_as_commit(&commit, repo, id);
170 got_object_commit_get_committer_time(commit);
171 got_object_commit_close(commit);
172 if (cmp_time < committer_time)
173 cmp_time = committer_time;
180 *repo_age = cmp_time;
182 got_ref_list_free(&refs);
186 static const struct got_error *
187 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
188 struct got_commit_object *commit, struct got_reflist_head *refs,
189 struct got_object_id *id)
191 const struct got_error *error = NULL;
192 struct got_reflist_entry *re;
193 struct got_object_id *id2 = NULL;
194 struct got_object_qid *parent_id;
195 struct transport *t = c->t;
196 struct querystring *qs = c->t->qs;
197 char *commit_msg = NULL, *commit_msg0;
199 TAILQ_FOREACH(re, refs, entry) {
202 struct got_tag_object *tag = NULL;
203 struct got_object_id *ref_id;
206 if (got_ref_is_symbolic(re->ref))
209 name = got_ref_get_name(re->ref);
210 if (strncmp(name, "refs/", 5) == 0)
212 if (strncmp(name, "got/", 4) == 0)
214 if (strncmp(name, "heads/", 6) == 0)
216 if (strncmp(name, "remotes/", 8) == 0) {
218 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
221 error = got_ref_resolve(&ref_id, t->repo, re->ref);
224 if (strncmp(name, "tags/", 5) == 0) {
225 error = got_object_open_as_tag(&tag, t->repo, ref_id);
227 if (error->code != GOT_ERR_OBJ_TYPE) {
232 * Ref points at something other
239 cmp = got_object_id_cmp(tag ?
240 got_object_tag_get_object_id(tag) : ref_id, id);
243 got_object_tag_close(tag);
246 s = repo_commit->refs_str;
247 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
248 s ? ", " : "", name) == -1) {
249 error = got_error_from_errno("asprintf");
251 repo_commit->refs_str = NULL;
257 error = got_object_id_str(&repo_commit->commit_id, id);
261 error = got_object_id_str(&repo_commit->tree_id,
262 got_object_commit_get_tree_id(commit));
266 if (qs->action == DIFF) {
267 parent_id = STAILQ_FIRST(
268 got_object_commit_get_parent_ids(commit));
269 if (parent_id != NULL) {
270 id2 = got_object_id_dup(&parent_id->id);
271 error = got_object_id_str(&repo_commit->parent_id, id2);
276 repo_commit->parent_id = strdup("/dev/null");
277 if (repo_commit->parent_id == NULL) {
278 error = got_error_from_errno("strdup");
284 repo_commit->committer_time =
285 got_object_commit_get_committer_time(commit);
287 repo_commit->author =
288 strdup(got_object_commit_get_author(commit));
289 if (repo_commit->author == NULL) {
290 error = got_error_from_errno("strdup");
293 repo_commit->committer =
294 strdup(got_object_commit_get_committer(commit));
295 if (repo_commit->committer == NULL) {
296 error = got_error_from_errno("strdup");
299 error = got_object_commit_get_logmsg(&commit_msg0, commit);
303 commit_msg = commit_msg0;
304 while (*commit_msg == '\n')
307 repo_commit->commit_msg = strdup(commit_msg);
308 if (repo_commit->commit_msg == NULL)
309 error = got_error_from_errno("strdup");
314 const struct got_error *
315 got_get_repo_commits(struct request *c, int limit)
317 const struct got_error *error = NULL;
318 struct got_object_id *id = NULL;
319 struct got_commit_graph *graph = NULL;
320 struct got_commit_object *commit = NULL;
321 struct got_reflist_head refs;
322 struct got_reference *ref = NULL;
323 struct repo_commit *repo_commit = NULL;
324 struct server *srv = c->srv;
325 struct transport *t = c->t;
326 struct got_repository *repo = t->repo;
327 struct querystring *qs = t->qs;
328 struct repo_dir *repo_dir = t->repo_dir;
329 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
330 int chk_next = 0, chk_multi = 0, commit_found = 0;
331 int obj_type, limit_chk = 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 * XXX: jumping directly to a commit id via
348 * got_repo_match_object_id_prefix significantly improves performance,
349 * but does not allow us to create a PREVIOUS button, since commits can
350 * only be itereated forward. So, we have to match as we iterate from
353 if (qs->action == BRIEFS || qs->action == COMMITS ||
354 (qs->action == TREE && qs->commit == NULL)) {
355 error = got_ref_open(&ref, repo, qs->headref, 0);
359 error = got_ref_resolve(&id, repo, ref);
362 } else if (qs->commit != NULL) {
363 error = got_ref_open(&ref, repo, qs->commit, 0);
365 error = got_ref_resolve(&id, repo, ref);
368 error = got_object_get_type(&obj_type, repo, id);
371 if (obj_type == GOT_OBJ_TYPE_TAG) {
372 struct got_tag_object *tag;
373 error = got_object_open_as_tag(&tag, repo, id);
376 if (got_object_tag_get_object_type(tag) !=
377 GOT_OBJ_TYPE_COMMIT) {
378 got_object_tag_close(tag);
379 error = got_error(GOT_ERR_OBJ_TYPE);
383 id = got_object_id_dup(
384 got_object_tag_get_object_id(tag));
386 error = got_error_from_errno(
387 "got_object_id_dup");
388 got_object_tag_close(tag);
391 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
392 error = got_error(GOT_ERR_OBJ_TYPE);
396 error = got_repo_match_object_id_prefix(&id, qs->commit,
397 GOT_OBJ_TYPE_COMMIT, repo);
402 error = got_repo_map_path(&in_repo_path, repo, repo_path);
406 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
410 if (qs->file != NULL && strlen(qs->file) > 0) {
411 error = got_commit_graph_open(&graph, file_path, 0);
415 error = got_commit_graph_open(&graph, in_repo_path, 0);
420 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
425 struct got_object_id next_id;
427 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
430 if (error->code == GOT_ERR_ITER_COMPLETED)
435 error = got_object_open_as_commit(&commit, repo, &next_id);
439 error = got_init_repo_commit(&repo_commit);
443 error = got_get_repo_commit(c, repo_commit, commit,
446 gotweb_free_repo_commit(repo_commit);
450 if (limit_chk == ((limit * qs->page) - limit) &&
451 commit_found == 0 && repo_commit->commit_id != NULL) {
452 t->prev_id = strdup(repo_commit->commit_id);
453 if (t->prev_id == NULL) {
454 error = got_error_from_errno("strdup");
455 gotweb_free_repo_commit(repo_commit);
460 if (qs->commit != NULL && commit_found == 0 && limit != 1) {
461 if (strcmp(qs->commit, repo_commit->commit_id) == 0)
463 else if (qs->file != NULL && strlen(qs->file) > 0 &&
467 gotweb_free_repo_commit(repo_commit);
473 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
475 if (limit == 1 && chk_multi == 0 &&
476 srv->max_commits_display != 1)
482 * check for one more commit before breaking,
483 * so we know whether to navigate through briefs
484 * commits and summary
486 if (chk_next && (qs->action == BRIEFS ||
487 qs->action == COMMITS || qs->action == SUMMARY)) {
488 t->next_id = strdup(repo_commit->commit_id);
489 if (t->next_id == NULL) {
490 error = got_error_from_errno("strdup");
494 got_object_commit_close(commit);
497 TAILQ_REMOVE(&t->repo_commits, repo_commit,
499 gotweb_free_repo_commit(repo_commit);
503 if (error || (limit && --limit == 0)) {
504 if (commit_found || (qs->file != NULL &&
505 strlen(qs->file) > 0))
511 got_object_commit_close(commit);
519 got_object_commit_close(commit);
521 got_commit_graph_close(graph);
522 got_ref_list_free(&refs);
530 const struct got_error *
531 got_get_repo_tags(struct request *c, int limit)
533 const struct got_error *error = NULL;
534 struct got_object_id *id = NULL;
535 struct got_commit_object *commit = NULL;
536 struct got_reflist_head refs;
537 struct got_reference *ref;
538 struct got_reflist_entry *re;
539 struct server *srv = c->srv;
540 struct transport *t = c->t;
541 struct got_repository *repo = t->repo;
542 struct querystring *qs = t->qs;
543 struct repo_dir *repo_dir = t->repo_dir;
544 struct got_tag_object *tag = NULL;
545 struct repo_tag *rt = NULL, *trt = NULL;
546 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
547 char *tag_commit = NULL, *tag_commit0 = NULL;
548 char *commit_msg = NULL, *commit_msg0 = NULL;
549 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
553 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
554 repo_dir->name) == -1)
555 return got_error_from_errno("asprintf");
557 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
558 error = got_ref_open(&ref, repo, qs->headref, 0);
561 error = got_ref_resolve(&id, repo, ref);
565 } else if (qs->commit == NULL && qs->action == TAG) {
566 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
569 error = got_repo_match_object_id_prefix(&id, qs->commit,
570 GOT_OBJ_TYPE_COMMIT, repo);
575 if (qs->action != SUMMARY && qs->action != TAGS) {
576 error = got_object_open_as_commit(&commit, repo, id);
579 error = got_object_commit_get_logmsg(&commit_msg0, commit);
583 got_object_commit_close(commit);
588 error = got_repo_map_path(&in_repo_path, repo, repo_path);
592 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
601 * XXX: again, see previous message about caching
604 TAILQ_FOREACH(re, &refs, entry) {
605 struct repo_tag *new_repo_tag = NULL;
606 error = got_init_repo_tag(&new_repo_tag);
610 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
612 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
613 if (new_repo_tag->tag_name == NULL) {
614 error = got_error_from_errno("strdup");
624 error = got_ref_resolve(&id, repo, re->ref);
629 got_object_tag_close(tag);
630 error = got_object_open_as_tag(&tag, repo, id);
632 if (error->code != GOT_ERR_OBJ_TYPE)
634 /* "lightweight" tag */
635 error = got_object_open_as_commit(&commit, repo, id);
638 new_repo_tag->tagger =
639 strdup(got_object_commit_get_committer(commit));
640 if (new_repo_tag->tagger == NULL) {
641 error = got_error_from_errno("strdup");
644 new_repo_tag->tagger_time =
645 got_object_commit_get_committer_time(commit);
646 error = got_object_id_str(&id_str, id);
650 new_repo_tag->tagger =
651 strdup(got_object_tag_get_tagger(tag));
652 if (new_repo_tag->tagger == NULL) {
653 error = got_error_from_errno("strdup");
656 new_repo_tag->tagger_time =
657 got_object_tag_get_tagger_time(tag);
658 error = got_object_id_str(&id_str,
659 got_object_tag_get_object_id(tag));
664 new_repo_tag->commit_id = strdup(id_str);
665 if (new_repo_tag->commit_id == NULL)
668 if (commit_found == 0 && qs->commit != NULL &&
669 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
677 * check for one more commit before breaking,
678 * so we know whether to navigate through briefs
679 * commits and summary
682 t->next_id = strdup(new_repo_tag->commit_id);
683 if (t->next_id == NULL) {
684 error = got_error_from_errno("strdup");
688 got_object_commit_close(commit);
691 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
692 gotweb_free_repo_tag(new_repo_tag);
697 error = got_object_commit_get_logmsg(&tag_commit0,
701 got_object_commit_close(commit);
704 tag_commit0 = strdup(got_object_tag_get_message(tag));
705 if (tag_commit0 == NULL) {
706 error = got_error_from_errno("strdup");
711 tag_commit = tag_commit0;
712 while (*tag_commit == '\n')
714 new_repo_tag->tag_commit = strdup(tag_commit);
715 if (new_repo_tag->tag_commit == NULL) {
716 error = got_error_from_errno("strdup");
722 if (qs->action != SUMMARY && qs->action != TAGS) {
723 commit_msg = commit_msg0;
724 while (*commit_msg == '\n')
727 new_repo_tag->commit_msg = strdup(commit_msg);
728 if (new_repo_tag->commit_msg == NULL) {
729 error = got_error_from_errno("strdup");
734 if (limit && --limit == 0) {
743 * we have tailq populated, so find previous commit id
744 * for navigation through briefs and commits
746 if (t->tag_count == 0) {
747 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
748 TAILQ_REMOVE(&t->repo_tags, rt, entry);
749 gotweb_free_repo_tag(rt);
752 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
754 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
756 if (commit_found == 0 && rt->commit_id != NULL &&
757 strcmp(qs->commit, rt->commit_id) != 0) {
761 if (c_cnt == srv->max_commits_display ||
762 rt == TAILQ_FIRST(&t->repo_tags)) {
763 t->prev_id = strdup(rt->commit_id);
764 if (t->prev_id == NULL)
765 error = got_error_from_errno("strdup");
773 got_object_commit_close(commit);
775 got_object_tag_close(tag);
776 got_ref_list_free(&refs);
786 got_output_repo_tree(struct request *c,
787 int (*cb)(struct template *, struct got_tree_entry *))
789 const struct got_error *error = NULL;
790 struct transport *t = c->t;
791 struct got_commit_object *commit = NULL;
792 struct got_repository *repo = t->repo;
793 struct querystring *qs = t->qs;
794 struct repo_commit *rc = NULL;
795 struct got_object_id *tree_id = NULL, *commit_id = NULL;
796 struct got_reflist_head refs;
797 struct got_tree_object *tree = NULL;
798 struct got_tree_entry *te;
799 struct repo_dir *repo_dir = t->repo_dir;
800 char *escaped_name = NULL, *path = NULL;
805 rc = TAILQ_FIRST(&t->repo_commits);
807 if (qs->folder != NULL) {
808 path = strdup(qs->folder);
810 error = got_error_from_errno("strdup");
814 error = got_repo_map_path(&path, repo, repo_dir->path);
819 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
820 GOT_OBJ_TYPE_COMMIT, &refs, repo);
824 error = got_object_open_as_commit(&commit, repo, commit_id);
828 error = got_object_id_by_path(&tree_id, repo, commit, path);
832 error = got_object_open_as_tree(&tree, repo, tree_id);
836 nentries = got_object_tree_get_nentries(tree);
838 for (i = 0; i < nentries; i++) {
839 te = got_object_tree_get_entry(tree, i);
840 if (cb(c->tp, te) == -1)
846 got_ref_list_free(&refs);
848 got_object_commit_close(commit);
850 got_object_tree_close(tree);
854 log_warnx("%s: %s", __func__, error->msg);
860 const struct got_error *
861 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
862 int *binary, struct request *c)
864 const struct got_error *error = NULL;
865 struct transport *t = c->t;
866 struct got_repository *repo = t->repo;
867 struct querystring *qs = c->t->qs;
868 struct got_commit_object *commit = NULL;
869 struct got_object_id *commit_id = NULL;
870 struct got_reflist_head refs;
871 char *path = NULL, *in_repo_path = NULL;
880 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
881 qs->folder ? "/" : "", qs->file) == -1) {
882 error = got_error_from_errno("asprintf");
886 error = got_repo_map_path(&in_repo_path, repo, path);
890 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
891 GOT_OBJ_TYPE_COMMIT, &refs, repo);
895 error = got_object_open_as_commit(&commit, repo, commit_id);
899 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
903 if (commit_id == NULL) {
904 error = got_error(GOT_ERR_NO_OBJ);
908 error = got_object_get_type(&obj_type, repo, commit_id);
912 if (obj_type != GOT_OBJ_TYPE_BLOB) {
913 error = got_error(GOT_ERR_OBJ_TYPE);
917 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
921 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
925 error = got_object_blob_is_binary(binary, *blob);
931 got_object_commit_close(commit);
937 got_object_blob_close(*blob);
949 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
950 int (*cb)(struct template *, const char *, size_t))
952 const struct got_error *err;
959 err = got_object_blob_getline(&line, &linelen, &linesize,
961 if (err || linelen == -1)
964 if (cb(tp, line, lineno) == -1)
971 log_warnx("%s: got_object_blob_getline failed: %s",
978 struct blame_cb_args {
979 struct blame_line *lines;
985 struct got_repository *repo;
987 got_render_blame_line_cb cb;
990 static const struct got_error *
991 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
992 struct got_commit_object *commit, struct got_object_id *id)
994 const struct got_error *err = NULL;
995 struct blame_cb_args *a = arg;
996 struct blame_line *bline;
997 struct request *c = a->c;
1002 time_t committer_time;
1004 if (nlines != a->nlines ||
1005 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1006 return got_error(GOT_ERR_RANGE);
1009 return NULL; /* no change in this commit */
1011 /* Annotate this line. */
1012 bline = &a->lines[lineno - 1];
1013 if (bline->annotated)
1015 err = got_object_id_str(&bline->id_str, id);
1019 bline->committer = strdup(got_object_commit_get_committer(commit));
1020 if (bline->committer == NULL) {
1021 err = got_error_from_errno("strdup");
1025 committer_time = got_object_commit_get_committer_time(commit);
1026 if (gmtime_r(&committer_time, &tm) == NULL)
1027 return got_error_from_errno("gmtime_r");
1028 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1030 err = got_error(GOT_ERR_NO_SPACE);
1033 bline->annotated = 1;
1035 /* Print lines annotated so far. */
1036 bline = &a->lines[a->lineno_cur - 1];
1037 if (!bline->annotated)
1040 offset = a->line_offsets[a->lineno_cur - 1];
1041 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1042 err = got_error_from_errno("fseeko");
1046 while (a->lineno_cur <= a->nlines && bline->annotated) {
1047 if (getline(&line, &linesize, a->f) == -1) {
1049 err = got_error_from_errno("getline");
1053 if (a->cb(c->tp, line, bline, a->nlines_prec,
1054 a->lineno_cur) == -1)
1058 bline = &a->lines[a->lineno_cur - 1];
1065 const struct got_error *
1066 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1068 const struct got_error *error = NULL;
1069 struct transport *t = c->t;
1070 struct got_repository *repo = t->repo;
1071 struct querystring *qs = c->t->qs;
1072 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1073 struct got_commit_object *commit = NULL;
1074 struct got_reflist_head refs;
1075 struct got_blob_object *blob = NULL;
1076 char *path = NULL, *in_repo_path = NULL;
1077 struct blame_cb_args bca;
1078 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1081 FILE *f1 = NULL, *f2 = NULL;
1088 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1089 qs->folder ? "/" : "", qs->file) == -1) {
1090 error = got_error_from_errno("asprintf");
1094 error = got_repo_map_path(&in_repo_path, repo, path);
1098 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1099 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1103 error = got_object_open_as_commit(&commit, repo, commit_id);
1107 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1111 error = got_object_get_type(&obj_type, repo, obj_id);
1115 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1116 error = got_error(GOT_ERR_OBJ_TYPE);
1120 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1124 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1128 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1132 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1133 &bca.line_offsets, bca.f, blob);
1134 if (error || bca.nlines == 0)
1137 /* Don't include \n at EOF in the blame line count. */
1138 if (bca.line_offsets[bca.nlines - 1] == filesize)
1141 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1142 if (bca.lines == NULL) {
1143 error = got_error_from_errno("calloc");
1147 bca.nlines_prec = 0;
1156 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1160 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1164 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1168 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1172 error = got_blame(in_repo_path, commit_id, repo,
1173 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1178 free(bca.line_offsets);
1179 for (i = 0; i < bca.nlines; i++) {
1180 struct blame_line *bline = &bca.lines[i];
1181 free(bline->id_str);
1182 free(bline->committer);
1186 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1187 error = got_error_from_errno("close");
1188 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1189 error = got_error_from_errno("close");
1190 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1191 error = got_error_from_errno("close");
1193 const struct got_error *bca_err =
1194 got_gotweb_flushfile(bca.f, fd1);
1199 const struct got_error *f1_err =
1200 got_gotweb_flushfile(f1, fd5);
1205 const struct got_error *f2_err =
1206 got_gotweb_flushfile(f2, fd6);
1211 got_object_commit_close(commit);
1213 got_object_blob_close(blob);
1218 got_ref_list_free(&refs);
1222 const struct got_error *
1223 got_open_diff_for_output(FILE **fp, int *fd, struct request *c)
1225 const struct got_error *error = NULL;
1226 struct transport *t = c->t;
1227 struct got_repository *repo = t->repo;
1228 struct repo_commit *rc = NULL;
1229 struct got_object_id *id1 = NULL, *id2 = NULL;
1230 struct got_reflist_head refs;
1231 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1232 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1239 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1243 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1247 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1251 rc = TAILQ_FIRST(&t->repo_commits);
1253 if (rc->parent_id != NULL &&
1254 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1255 error = got_repo_match_object_id(&id1, NULL,
1256 rc->parent_id, GOT_OBJ_TYPE_ANY,
1262 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1263 GOT_OBJ_TYPE_ANY, &refs, repo);
1267 error = got_object_get_type(&obj_type, repo, id2);
1271 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1275 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1280 case GOT_OBJ_TYPE_BLOB:
1281 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1282 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1285 case GOT_OBJ_TYPE_TREE:
1286 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1287 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1290 case GOT_OBJ_TYPE_COMMIT:
1291 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1292 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1296 error = got_error(GOT_ERR_OBJ_TYPE);
1301 if (fseek(f1, 0, SEEK_SET) == -1) {
1302 error = got_ferror(f1, GOT_ERR_IO);
1306 if (fseek(f2, 0, SEEK_SET) == -1) {
1307 error = got_ferror(f2, GOT_ERR_IO);
1311 if (fseek(f3, 0, SEEK_SET) == -1) {
1312 error = got_ferror(f3, GOT_ERR_IO);
1320 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1321 error = got_error_from_errno("close");
1322 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1323 error = got_error_from_errno("close");
1325 const struct got_error *f1_err =
1326 got_gotweb_flushfile(f1, fd1);
1331 const struct got_error *f2_err =
1332 got_gotweb_flushfile(f2, fd2);
1337 got_gotweb_flushfile(f3, fd3);
1341 got_ref_list_free(&refs);
1347 static const struct got_error *
1348 got_init_repo_commit(struct repo_commit **rc)
1350 *rc = calloc(1, sizeof(**rc));
1352 return got_error_from_errno2("%s: calloc", __func__);
1355 (*rc)->refs_str = NULL;
1356 (*rc)->commit_id = NULL;
1357 (*rc)->committer = NULL;
1358 (*rc)->author = NULL;
1359 (*rc)->parent_id = NULL;
1360 (*rc)->tree_id = NULL;
1361 (*rc)->commit_msg = NULL;
1366 static const struct got_error *
1367 got_init_repo_tag(struct repo_tag **rt)
1369 *rt = calloc(1, sizeof(**rt));
1371 return got_error_from_errno2("%s: calloc", __func__);
1373 (*rt)->commit_id = NULL;
1374 (*rt)->tag_name = NULL;
1375 (*rt)->tag_commit = NULL;
1376 (*rt)->commit_msg = NULL;
1377 (*rt)->tagger = NULL;