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_flushfile(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 static 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)
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(char **repo_age, struct request *c,
131 const char *refname, int ref_tm)
133 const struct got_error *error = NULL;
134 struct server *srv = c->srv;
135 struct transport *t = c->t;
136 struct got_repository *repo = t->repo;
137 struct got_commit_object *commit = NULL;
138 struct got_reflist_head refs;
139 struct got_reflist_entry *re;
140 time_t committer_time = 0, cmp_time = 0;
145 if (srv->show_repo_age == 0)
148 error = got_ref_list(&refs, repo, "refs/heads",
149 got_ref_cmp_by_name, NULL);
154 * Find the youngest branch tip in the repository, or the age of
155 * the a specific branch tip if a name was provided by the caller.
157 TAILQ_FOREACH(re, &refs, entry) {
158 struct got_object_id *id = NULL;
160 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
163 error = got_ref_resolve(&id, repo, re->ref);
167 error = got_object_open_as_commit(&commit, repo, id);
173 got_object_commit_get_committer_time(commit);
174 got_object_commit_close(commit);
175 if (cmp_time < committer_time)
176 cmp_time = committer_time;
183 committer_time = cmp_time;
184 error = gotweb_get_time_str(repo_age, committer_time, ref_tm);
187 got_ref_list_free(&refs);
191 static const struct got_error *
192 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
193 struct got_commit_object *commit, struct got_reflist_head *refs,
194 struct got_object_id *id)
196 const struct got_error *error = NULL;
197 struct got_reflist_entry *re;
198 struct got_object_id *id2 = NULL;
199 struct got_object_qid *parent_id;
200 struct transport *t = c->t;
201 struct querystring *qs = c->t->qs;
202 char *commit_msg = NULL, *commit_msg0;
204 TAILQ_FOREACH(re, refs, entry) {
207 struct got_tag_object *tag = NULL;
208 struct got_object_id *ref_id;
211 if (got_ref_is_symbolic(re->ref))
214 name = got_ref_get_name(re->ref);
215 if (strncmp(name, "refs/", 5) == 0)
217 if (strncmp(name, "got/", 4) == 0)
219 if (strncmp(name, "heads/", 6) == 0)
221 if (strncmp(name, "remotes/", 8) == 0) {
223 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
226 error = got_ref_resolve(&ref_id, t->repo, re->ref);
229 if (strncmp(name, "tags/", 5) == 0) {
230 error = got_object_open_as_tag(&tag, t->repo, ref_id);
232 if (error->code != GOT_ERR_OBJ_TYPE) {
237 * Ref points at something other
244 cmp = got_object_id_cmp(tag ?
245 got_object_tag_get_object_id(tag) : ref_id, id);
248 got_object_tag_close(tag);
251 s = repo_commit->refs_str;
252 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
253 s ? ", " : "", name) == -1) {
254 error = got_error_from_errno("asprintf");
256 repo_commit->refs_str = NULL;
262 error = got_object_id_str(&repo_commit->commit_id, id);
266 error = got_object_id_str(&repo_commit->tree_id,
267 got_object_commit_get_tree_id(commit));
271 if (qs->action == DIFF) {
272 parent_id = STAILQ_FIRST(
273 got_object_commit_get_parent_ids(commit));
274 if (parent_id != NULL) {
275 id2 = got_object_id_dup(&parent_id->id);
276 error = got_object_id_str(&repo_commit->parent_id, id2);
281 repo_commit->parent_id = strdup("/dev/null");
282 if (repo_commit->parent_id == NULL) {
283 error = got_error_from_errno("strdup");
289 repo_commit->committer_time =
290 got_object_commit_get_committer_time(commit);
292 repo_commit->author =
293 strdup(got_object_commit_get_author(commit));
294 if (repo_commit->author == NULL) {
295 error = got_error_from_errno("strdup");
298 repo_commit->committer =
299 strdup(got_object_commit_get_committer(commit));
300 if (repo_commit->committer == NULL) {
301 error = got_error_from_errno("strdup");
304 error = got_object_commit_get_logmsg(&commit_msg0, commit);
308 commit_msg = commit_msg0;
309 while (*commit_msg == '\n')
312 repo_commit->commit_msg = strdup(commit_msg);
313 if (repo_commit->commit_msg == NULL)
314 error = got_error_from_errno("strdup");
319 const struct got_error *
320 got_get_repo_commits(struct request *c, int limit)
322 const struct got_error *error = NULL;
323 struct got_object_id *id = NULL;
324 struct got_commit_graph *graph = NULL;
325 struct got_commit_object *commit = NULL;
326 struct got_reflist_head refs;
327 struct got_reference *ref = NULL;
328 struct repo_commit *repo_commit = NULL;
329 struct server *srv = c->srv;
330 struct transport *t = c->t;
331 struct got_repository *repo = t->repo;
332 struct querystring *qs = t->qs;
333 struct repo_dir *repo_dir = t->repo_dir;
334 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
335 int chk_next = 0, chk_multi = 0, commit_found = 0;
336 int obj_type, limit_chk = 0;
340 if (qs->file != NULL && strlen(qs->file) > 0)
341 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
343 return got_error_from_errno("asprintf");
345 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
346 repo_dir->name) == -1) {
347 error = got_error_from_errno("asprintf");
352 * XXX: jumping directly to a commit id via
353 * got_repo_match_object_id_prefix significantly improves performance,
354 * but does not allow us to create a PREVIOUS button, since commits can
355 * only be itereated forward. So, we have to match as we iterate from
358 if (qs->action == BRIEFS || qs->action == COMMITS ||
359 (qs->action == TREE && qs->commit == NULL)) {
360 error = got_ref_open(&ref, repo, qs->headref, 0);
364 error = got_ref_resolve(&id, repo, ref);
367 } else if (qs->commit != NULL) {
368 error = got_ref_open(&ref, repo, qs->commit, 0);
370 error = got_ref_resolve(&id, repo, ref);
373 error = got_object_get_type(&obj_type, repo, id);
376 if (obj_type == GOT_OBJ_TYPE_TAG) {
377 struct got_tag_object *tag;
378 error = got_object_open_as_tag(&tag, repo, id);
381 if (got_object_tag_get_object_type(tag) !=
382 GOT_OBJ_TYPE_COMMIT) {
383 got_object_tag_close(tag);
384 error = got_error(GOT_ERR_OBJ_TYPE);
388 id = got_object_id_dup(
389 got_object_tag_get_object_id(tag));
391 error = got_error_from_errno(
392 "got_object_id_dup");
393 got_object_tag_close(tag);
396 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
397 error = got_error(GOT_ERR_OBJ_TYPE);
401 error = got_repo_match_object_id_prefix(&id, qs->commit,
402 GOT_OBJ_TYPE_COMMIT, repo);
407 error = got_repo_map_path(&in_repo_path, repo, repo_path);
411 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
415 if (qs->file != NULL && strlen(qs->file) > 0) {
416 error = got_commit_graph_open(&graph, file_path, 0);
420 error = got_commit_graph_open(&graph, in_repo_path, 0);
425 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
430 struct got_object_id next_id;
432 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
435 if (error->code == GOT_ERR_ITER_COMPLETED)
440 error = got_object_open_as_commit(&commit, repo, &next_id);
444 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
449 error = got_init_repo_commit(&repo_commit);
453 error = got_get_repo_commit(c, repo_commit, commit,
456 gotweb_free_repo_commit(repo_commit);
460 if (limit_chk == ((limit * qs->page) - limit) &&
461 commit_found == 0 && repo_commit->commit_id != NULL) {
462 t->prev_id = strdup(repo_commit->commit_id);
463 if (t->prev_id == NULL) {
464 error = got_error_from_errno("strdup");
465 gotweb_free_repo_commit(repo_commit);
470 if (qs->commit != NULL && commit_found == 0 && limit != 1) {
471 if (strcmp(qs->commit, repo_commit->commit_id) == 0)
473 else if (qs->file != NULL && strlen(qs->file) > 0 &&
477 gotweb_free_repo_commit(repo_commit);
483 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
485 if (limit == 1 && chk_multi == 0 &&
486 srv->max_commits_display != 1)
492 * check for one more commit before breaking,
493 * so we know whether to navigate through briefs
494 * commits and summary
496 if (chk_next && (qs->action == BRIEFS ||
497 qs->action == COMMITS || qs->action == SUMMARY)) {
498 t->next_id = strdup(repo_commit->commit_id);
499 if (t->next_id == NULL) {
500 error = got_error_from_errno("strdup");
504 got_object_commit_close(commit);
507 TAILQ_REMOVE(&t->repo_commits, repo_commit,
509 gotweb_free_repo_commit(repo_commit);
513 got_ref_list_free(&refs);
514 if (error || (limit && --limit == 0)) {
515 if (commit_found || (qs->file != NULL &&
516 strlen(qs->file) > 0))
522 got_object_commit_close(commit);
530 got_object_commit_close(commit);
532 got_commit_graph_close(graph);
533 got_ref_list_free(&refs);
541 const struct got_error *
542 got_get_repo_tags(struct request *c, int limit)
544 const struct got_error *error = NULL;
545 struct got_object_id *id = NULL;
546 struct got_commit_object *commit = NULL;
547 struct got_reflist_head refs;
548 struct got_reference *ref;
549 struct got_reflist_entry *re;
550 struct server *srv = c->srv;
551 struct transport *t = c->t;
552 struct got_repository *repo = t->repo;
553 struct querystring *qs = t->qs;
554 struct repo_dir *repo_dir = t->repo_dir;
555 struct got_tag_object *tag = NULL;
556 struct repo_tag *rt = NULL, *trt = NULL;
557 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
558 char *tag_commit = NULL, *tag_commit0 = NULL;
559 char *commit_msg = NULL, *commit_msg0 = NULL;
560 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
564 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
565 repo_dir->name) == -1)
566 return got_error_from_errno("asprintf");
568 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
569 error = got_ref_open(&ref, repo, qs->headref, 0);
572 error = got_ref_resolve(&id, repo, ref);
576 } else if (qs->commit == NULL && qs->action == TAG) {
577 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
580 error = got_repo_match_object_id_prefix(&id, qs->commit,
581 GOT_OBJ_TYPE_COMMIT, repo);
586 if (qs->action != SUMMARY && qs->action != TAGS) {
587 error = got_object_open_as_commit(&commit, repo, id);
590 error = got_object_commit_get_logmsg(&commit_msg0, commit);
594 got_object_commit_close(commit);
599 error = got_repo_map_path(&in_repo_path, repo, repo_path);
603 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
612 * XXX: again, see previous message about caching
615 TAILQ_FOREACH(re, &refs, entry) {
616 struct repo_tag *new_repo_tag = NULL;
617 error = got_init_repo_tag(&new_repo_tag);
621 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
623 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
624 if (new_repo_tag->tag_name == NULL) {
625 error = got_error_from_errno("strdup");
635 error = got_ref_resolve(&id, repo, re->ref);
640 got_object_tag_close(tag);
641 error = got_object_open_as_tag(&tag, repo, id);
643 if (error->code != GOT_ERR_OBJ_TYPE)
645 /* "lightweight" tag */
646 error = got_object_open_as_commit(&commit, repo, id);
649 new_repo_tag->tagger =
650 strdup(got_object_commit_get_committer(commit));
651 if (new_repo_tag->tagger == NULL) {
652 error = got_error_from_errno("strdup");
655 new_repo_tag->tagger_time =
656 got_object_commit_get_committer_time(commit);
657 error = got_object_id_str(&id_str, id);
661 new_repo_tag->tagger =
662 strdup(got_object_tag_get_tagger(tag));
663 if (new_repo_tag->tagger == NULL) {
664 error = got_error_from_errno("strdup");
667 new_repo_tag->tagger_time =
668 got_object_tag_get_tagger_time(tag);
669 error = got_object_id_str(&id_str,
670 got_object_tag_get_object_id(tag));
675 new_repo_tag->commit_id = strdup(id_str);
676 if (new_repo_tag->commit_id == NULL)
679 if (commit_found == 0 && qs->commit != NULL &&
680 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
688 * check for one more commit before breaking,
689 * so we know whether to navigate through briefs
690 * commits and summary
693 t->next_id = strdup(new_repo_tag->commit_id);
694 if (t->next_id == NULL) {
695 error = got_error_from_errno("strdup");
699 got_object_commit_close(commit);
702 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
703 gotweb_free_repo_tag(new_repo_tag);
708 error = got_object_commit_get_logmsg(&tag_commit0,
712 got_object_commit_close(commit);
715 tag_commit0 = strdup(got_object_tag_get_message(tag));
716 if (tag_commit0 == NULL) {
717 error = got_error_from_errno("strdup");
722 tag_commit = tag_commit0;
723 while (*tag_commit == '\n')
725 new_repo_tag->tag_commit = strdup(tag_commit);
726 if (new_repo_tag->tag_commit == NULL) {
727 error = got_error_from_errno("strdup");
733 if (qs->action != SUMMARY && qs->action != TAGS) {
734 commit_msg = commit_msg0;
735 while (*commit_msg == '\n')
738 new_repo_tag->commit_msg = strdup(commit_msg);
739 if (new_repo_tag->commit_msg == NULL) {
740 error = got_error_from_errno("strdup");
745 if (limit && --limit == 0) {
754 * we have tailq populated, so find previous commit id
755 * for navigation through briefs and commits
757 if (t->tag_count == 0) {
758 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
759 TAILQ_REMOVE(&t->repo_tags, rt, entry);
760 gotweb_free_repo_tag(rt);
763 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
765 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
767 if (commit_found == 0 && rt->commit_id != NULL &&
768 strcmp(qs->commit, rt->commit_id) != 0) {
772 if (c_cnt == srv->max_commits_display ||
773 rt == TAILQ_FIRST(&t->repo_tags)) {
774 t->prev_id = strdup(rt->commit_id);
775 if (t->prev_id == NULL)
776 error = got_error_from_errno("strdup");
784 got_object_commit_close(commit);
786 got_object_tag_close(tag);
787 got_ref_list_free(&refs);
797 got_output_repo_tree(struct request *c,
798 int (*cb)(struct template *, struct got_tree_entry *))
800 const struct got_error *error = NULL;
801 struct transport *t = c->t;
802 struct got_commit_object *commit = NULL;
803 struct got_repository *repo = t->repo;
804 struct querystring *qs = t->qs;
805 struct repo_commit *rc = NULL;
806 struct got_object_id *tree_id = NULL, *commit_id = NULL;
807 struct got_reflist_head refs;
808 struct got_tree_object *tree = NULL;
809 struct got_tree_entry *te;
810 struct repo_dir *repo_dir = t->repo_dir;
811 char *escaped_name = NULL, *path = NULL;
816 rc = TAILQ_FIRST(&t->repo_commits);
818 if (qs->folder != NULL) {
819 path = strdup(qs->folder);
821 error = got_error_from_errno("strdup");
825 error = got_repo_map_path(&path, repo, repo_dir->path);
830 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
831 GOT_OBJ_TYPE_COMMIT, &refs, repo);
835 error = got_object_open_as_commit(&commit, repo, commit_id);
839 error = got_object_id_by_path(&tree_id, repo, commit, path);
843 error = got_object_open_as_tree(&tree, repo, tree_id);
847 nentries = got_object_tree_get_nentries(tree);
849 for (i = 0; i < nentries; i++) {
850 te = got_object_tree_get_entry(tree, i);
851 if (cb(c->tp, te) == -1)
857 got_ref_list_free(&refs);
859 got_object_commit_close(commit);
861 got_object_tree_close(tree);
865 log_warnx("%s: %s", __func__, error->msg);
871 const struct got_error *
872 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
873 int *binary, struct request *c)
875 const struct got_error *error = NULL;
876 struct transport *t = c->t;
877 struct got_repository *repo = t->repo;
878 struct querystring *qs = c->t->qs;
879 struct got_commit_object *commit = NULL;
880 struct got_object_id *commit_id = NULL;
881 struct got_reflist_head refs;
882 char *path = NULL, *in_repo_path = NULL;
891 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
892 qs->folder ? "/" : "", qs->file) == -1) {
893 error = got_error_from_errno("asprintf");
897 error = got_repo_map_path(&in_repo_path, repo, path);
901 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
902 GOT_OBJ_TYPE_COMMIT, &refs, repo);
906 error = got_object_open_as_commit(&commit, repo, commit_id);
910 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
914 if (commit_id == NULL) {
915 error = got_error(GOT_ERR_NO_OBJ);
919 error = got_object_get_type(&obj_type, repo, commit_id);
923 if (obj_type != GOT_OBJ_TYPE_BLOB) {
924 error = got_error(GOT_ERR_OBJ_TYPE);
928 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
932 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
936 error = got_object_blob_is_binary(binary, *blob);
942 got_object_commit_close(commit);
948 got_object_blob_close(*blob);
959 const struct got_error *
960 got_output_file_blob(struct request *c)
962 const struct got_error *error = NULL;
963 struct querystring *qs = c->t->qs;
964 struct got_blob_object *blob = NULL;
969 error = got_open_blob_for_output(&blob, &fd, &binary, c);
974 error = gotweb_render_content_type_file(c,
975 "application/octet-stream", qs->file, NULL);
977 error = gotweb_render_content_type(c, "text/plain");
980 log_warnx("%s: %s", __func__, error->msg);
985 error = got_object_blob_read_block(&len, blob);
990 buf = got_object_blob_get_read_buf(blob);
991 fcgi_gen_binary_response(c, buf, len);
994 if (close(fd) == -1 && error == NULL)
995 error = got_error_from_errno("close");
997 got_object_blob_close(blob);
1002 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
1003 int (*cb)(struct template *, const char *, size_t))
1005 const struct got_error *err;
1007 size_t linesize = 0;
1009 ssize_t linelen = 0;
1012 err = got_object_blob_getline(&line, &linelen, &linesize,
1014 if (err || linelen == -1)
1017 if (cb(tp, line, lineno) == -1)
1024 log_warnx("%s: got_object_blob_getline failed: %s",
1025 __func__, err->msg);
1035 char datebuf[11]; /* YYYY-MM-DD + NUL */
1038 struct blame_cb_args {
1039 struct blame_line *lines;
1043 off_t *line_offsets;
1045 struct got_repository *repo;
1049 static const struct got_error *
1050 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1051 struct got_commit_object *commit, struct got_object_id *id)
1053 const struct got_error *err = NULL;
1054 struct blame_cb_args *a = arg;
1055 struct blame_line *bline;
1056 struct request *c = a->c;
1057 struct transport *t = c->t;
1058 struct repo_dir *repo_dir = t->repo_dir;
1059 char *line = NULL, *eline = NULL;
1060 size_t linesize = 0;
1063 time_t committer_time;
1066 if (nlines != a->nlines ||
1067 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1068 return got_error(GOT_ERR_RANGE);
1071 return NULL; /* no change in this commit */
1073 /* Annotate this line. */
1074 bline = &a->lines[lineno - 1];
1075 if (bline->annotated)
1077 err = got_object_id_str(&bline->id_str, id);
1081 bline->committer = strdup(got_object_commit_get_committer(commit));
1082 if (bline->committer == NULL) {
1083 err = got_error_from_errno("strdup");
1087 committer_time = got_object_commit_get_committer_time(commit);
1088 if (gmtime_r(&committer_time, &tm) == NULL)
1089 return got_error_from_errno("gmtime_r");
1090 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1092 err = got_error(GOT_ERR_NO_SPACE);
1095 bline->annotated = 1;
1097 /* Print lines annotated so far. */
1098 bline = &a->lines[a->lineno_cur - 1];
1099 if (!bline->annotated)
1102 offset = a->line_offsets[a->lineno_cur - 1];
1103 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1104 err = got_error_from_errno("fseeko");
1108 while (a->lineno_cur <= a->nlines && bline->annotated) {
1109 char *smallerthan, *at, *nl, *committer;
1112 if (getline(&line, &linesize, a->f) == -1) {
1114 err = got_error_from_errno("getline");
1118 committer = bline->committer;
1119 smallerthan = strchr(committer, '<');
1120 if (smallerthan && smallerthan[1] != '\0')
1121 committer = smallerthan + 1;
1122 at = strchr(committer, '@');
1125 len = strlen(committer);
1127 committer[8] = '\0';
1129 nl = strchr(line, '\n');
1133 err = gotweb_escape_html(&eline, line);
1137 if (fcgi_printf(c, "<div class='blame_wrapper'>"
1138 "<div class='blame_number'>%.*d</div>"
1139 "<div class='blame_hash'>",
1140 a->nlines_prec, a->lineno_cur) == -1)
1143 r = gotweb_link(c, &(struct gotweb_url){
1147 .path = repo_dir->name,
1148 .commit = bline->id_str,
1149 }, "%.8s", bline->id_str);
1155 "<div class='blame_date'>%s</div>"
1156 "<div class='blame_author'>%s</div>"
1157 "<div class='blame_code'>%s</div>"
1158 "</div>", /* .blame_wrapper */
1166 bline = &a->lines[a->lineno_cur - 1];
1177 const struct got_error *
1178 got_output_file_blame(struct request *c)
1180 const struct got_error *error = NULL;
1181 struct transport *t = c->t;
1182 struct got_repository *repo = t->repo;
1183 struct querystring *qs = c->t->qs;
1184 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1185 struct got_commit_object *commit = NULL;
1186 struct got_reflist_head refs;
1187 struct got_blob_object *blob = NULL;
1188 char *path = NULL, *in_repo_path = NULL;
1189 struct blame_cb_args bca;
1190 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1193 FILE *f1 = NULL, *f2 = NULL;
1199 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1200 qs->folder ? "/" : "", qs->file) == -1) {
1201 error = got_error_from_errno("asprintf");
1205 error = got_repo_map_path(&in_repo_path, repo, path);
1209 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1210 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1214 error = got_object_open_as_commit(&commit, repo, commit_id);
1218 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1222 error = got_object_get_type(&obj_type, repo, obj_id);
1226 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1227 error = got_error(GOT_ERR_OBJ_TYPE);
1231 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1235 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1239 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1243 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1244 &bca.line_offsets, bca.f, blob);
1245 if (error || bca.nlines == 0)
1248 /* Don't include \n at EOF in the blame line count. */
1249 if (bca.line_offsets[bca.nlines - 1] == filesize)
1252 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1253 if (bca.lines == NULL) {
1254 error = got_error_from_errno("calloc");
1258 bca.nlines_prec = 0;
1267 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1271 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1275 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1279 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1283 error = got_blame(in_repo_path, commit_id, repo,
1284 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1289 free(bca.line_offsets);
1290 for (i = 0; i < bca.nlines; i++) {
1291 struct blame_line *bline = &bca.lines[i];
1292 free(bline->id_str);
1293 free(bline->committer);
1297 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1298 error = got_error_from_errno("close");
1299 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1300 error = got_error_from_errno("close");
1301 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1302 error = got_error_from_errno("close");
1304 const struct got_error *bca_err =
1305 got_gotweb_flushfile(bca.f, fd1);
1310 const struct got_error *f1_err =
1311 got_gotweb_flushfile(f1, fd5);
1316 const struct got_error *f2_err =
1317 got_gotweb_flushfile(f2, fd6);
1322 got_object_commit_close(commit);
1324 got_object_blob_close(blob);
1329 got_ref_list_free(&refs);
1333 const struct got_error *
1334 got_output_repo_diff(struct request *c)
1336 const struct got_error *error = NULL;
1337 struct transport *t = c->t;
1338 struct got_repository *repo = t->repo;
1339 struct repo_commit *rc = NULL;
1340 struct got_object_id *id1 = NULL, *id2 = NULL;
1341 struct got_reflist_head refs;
1342 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1343 char *label1 = NULL, *label2 = NULL, *line = NULL;
1344 char *newline, *eline = NULL, *color = NULL;
1345 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1346 size_t linesize = 0;
1352 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1356 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1360 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1364 rc = TAILQ_FIRST(&t->repo_commits);
1366 if (rc->parent_id != NULL &&
1367 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1368 error = got_repo_match_object_id(&id1, &label1,
1369 rc->parent_id, GOT_OBJ_TYPE_ANY,
1375 error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1376 GOT_OBJ_TYPE_ANY, &refs, repo);
1380 error = got_object_get_type(&obj_type, repo, id2);
1384 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1388 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1393 case GOT_OBJ_TYPE_BLOB:
1394 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1395 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1398 case GOT_OBJ_TYPE_TREE:
1399 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1400 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1403 case GOT_OBJ_TYPE_COMMIT:
1404 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1405 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1409 error = got_error(GOT_ERR_OBJ_TYPE);
1414 if (fseek(f1, 0, SEEK_SET) == -1) {
1415 error = got_ferror(f1, GOT_ERR_IO);
1419 if (fseek(f2, 0, SEEK_SET) == -1) {
1420 error = got_ferror(f2, GOT_ERR_IO);
1424 if (fseek(f3, 0, SEEK_SET) == -1) {
1425 error = got_ferror(f3, GOT_ERR_IO);
1429 while ((linelen = getline(&line, &linesize, f3)) != -1) {
1430 if (strncmp(line, "-", 1) == 0) {
1431 color = strdup("diff_minus");
1432 if (color == NULL) {
1433 error = got_error_from_errno("strdup");
1436 } else if (strncmp(line, "+", 1) == 0) {
1437 color = strdup("diff_plus");
1438 if (color == NULL) {
1439 error = got_error_from_errno("strdup");
1442 } else if (strncmp(line, "@@", 2) == 0) {
1443 color = strdup("diff_chunk_header");
1444 if (color == NULL) {
1445 error = got_error_from_errno("strdup");
1448 } else if (strncmp(line, "@@", 2) == 0) {
1449 color = strdup("diff_chunk_header");
1450 if (color == NULL) {
1451 error = got_error_from_errno("strdup");
1454 } else if (strncmp(line, "commit +", 8) == 0) {
1455 color = strdup("diff_meta");
1456 if (color == NULL) {
1457 error = got_error_from_errno("strdup");
1460 } else if (strncmp(line, "commit -", 8) == 0) {
1461 color = strdup("diff_meta");
1462 if (color == NULL) {
1463 error = got_error_from_errno("strdup");
1466 } else if (strncmp(line, "blob +", 6) == 0) {
1467 color = strdup("diff_meta");
1468 if (color == NULL) {
1469 error = got_error_from_errno("strdup");
1472 } else if (strncmp(line, "blob -", 6) == 0) {
1473 color = strdup("diff_meta");
1474 if (color == NULL) {
1475 error = got_error_from_errno("strdup");
1478 } else if (strncmp(line, "file +", 6) == 0) {
1479 color = strdup("diff_meta");
1480 if (color == NULL) {
1481 error = got_error_from_errno("strdup");
1484 } else if (strncmp(line, "file -", 6) == 0) {
1485 color = strdup("diff_meta");
1486 if (color == NULL) {
1487 error = got_error_from_errno("strdup");
1490 } else if (strncmp(line, "from:", 5) == 0) {
1491 color = strdup("diff_author");
1492 if (color == NULL) {
1493 error = got_error_from_errno("strdup");
1496 } else if (strncmp(line, "via:", 4) == 0) {
1497 color = strdup("diff_author");
1498 if (color == NULL) {
1499 error = got_error_from_errno("strdup");
1502 } else if (strncmp(line, "date:", 5) == 0) {
1503 color = strdup("diff_date");
1504 if (color == NULL) {
1505 error = got_error_from_errno("strdup");
1510 newline = strchr(line, '\n');
1514 error = gotweb_escape_html(&eline, line);
1518 fcgi_printf(c, "<div class='diff_line %s'>%s</div>\n",
1519 color ? color : "", eline);
1524 wrlen = wrlen + linelen;
1528 if (linelen == -1 && ferror(f3))
1529 error = got_error_from_errno("getline");
1532 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1533 error = got_error_from_errno("close");
1534 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1535 error = got_error_from_errno("close");
1537 const struct got_error *f1_err =
1538 got_gotweb_flushfile(f1, fd1);
1543 const struct got_error *f2_err =
1544 got_gotweb_flushfile(f2, fd2);
1549 const struct got_error *f3_err =
1550 got_gotweb_flushfile(f3, fd3);
1554 got_ref_list_free(&refs);
1564 static const struct got_error *
1565 got_init_repo_commit(struct repo_commit **rc)
1567 *rc = calloc(1, sizeof(**rc));
1569 return got_error_from_errno2("%s: calloc", __func__);
1572 (*rc)->refs_str = NULL;
1573 (*rc)->commit_id = NULL;
1574 (*rc)->committer = NULL;
1575 (*rc)->author = NULL;
1576 (*rc)->parent_id = NULL;
1577 (*rc)->tree_id = NULL;
1578 (*rc)->commit_msg = NULL;
1583 static const struct got_error *
1584 got_init_repo_tag(struct repo_tag **rt)
1586 *rt = calloc(1, sizeof(**rt));
1588 return got_error_from_errno2("%s: calloc", __func__);
1590 (*rt)->commit_id = NULL;
1591 (*rt)->tag_name = NULL;
1592 (*rt)->tag_commit = NULL;
1593 (*rt)->commit_msg = NULL;
1594 (*rt)->tagger = NULL;