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);
796 const struct got_error *
797 got_output_repo_tree(struct request *c)
799 const struct got_error *error = NULL;
800 struct transport *t = c->t;
801 struct got_commit_object *commit = NULL;
802 struct got_repository *repo = t->repo;
803 struct querystring *qs = t->qs;
804 struct repo_commit *rc = NULL;
805 struct got_object_id *tree_id = NULL, *commit_id = NULL;
806 struct got_reflist_head refs;
807 struct got_tree_object *tree = NULL;
808 struct repo_dir *repo_dir = t->repo_dir;
809 const char *name, *folder;
810 char *escaped_name = NULL, *path = NULL;
815 rc = TAILQ_FIRST(&t->repo_commits);
817 if (qs->folder != NULL) {
818 path = strdup(qs->folder);
820 error = got_error_from_errno("strdup");
824 error = got_repo_map_path(&path, repo, repo_dir->path);
829 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
830 GOT_OBJ_TYPE_COMMIT, &refs, repo);
834 error = got_object_open_as_commit(&commit, repo, commit_id);
838 error = got_object_id_by_path(&tree_id, repo, commit, path);
842 error = got_object_open_as_tree(&tree, repo, tree_id);
846 nentries = got_object_tree_get_nentries(tree);
848 folder = qs->folder ? qs->folder : "";
850 for (i = 0; i < nentries; i++) {
852 struct got_tree_entry *te;
855 te = got_object_tree_get_entry(tree, i);
857 mode = got_tree_entry_get_mode(te);
858 if (got_object_tree_entry_is_submodule(te))
860 else if (S_ISLNK(mode))
862 else if (S_ISDIR(mode))
864 else if (mode & S_IXUSR)
869 name = got_tree_entry_get_name(te);
870 error = gotweb_escape_html(&escaped_name, name);
875 struct gotweb_url url = {
879 .commit = rc->commit_id,
881 /* `folder' is filled later */
885 if (fcgi_printf(c,"<div class='tree_wrapper'>\n"
886 "<div class='tree_line'>") == -1)
889 if (asprintf(&path, "%s/%s", folder, name) == -1) {
890 error = got_error_from_errno("asprintf");
894 r = gotweb_link(c, &url, "%s%s", escaped_name,
900 if (fcgi_printf(c, "</div>\n" /* .tree_line */
901 "<div class='tree_line_blank'> </div>\n"
905 struct gotweb_url url = {
909 .commit = rc->commit_id,
914 if (fcgi_printf(c, "<div class='tree_wrapper'>\n"
915 "<div class='tree_line'>") == -1)
919 r = gotweb_link(c, &url, "%s%s", escaped_name,
924 if (fcgi_printf(c, "</div>\n" /* .tree_line */
925 "<div class='tree_line_blank'>") == -1)
928 url.action = COMMITS;
929 r = gotweb_link(c, &url, "commits");
933 if (fcgi_printf(c, " | ") == -1)
937 r = gotweb_link(c, &url, "blame");
942 "</div>\n" /* .tree_line_blank */
943 "</div>\n") == -1) /* .tree_wrapper */
952 got_ref_list_free(&refs);
954 got_object_commit_close(commit);
956 got_object_tree_close(tree);
962 const struct got_error *
963 got_output_file_blob(struct request *c)
965 const struct got_error *error = NULL;
966 struct transport *t = c->t;
967 struct got_repository *repo = t->repo;
968 struct querystring *qs = c->t->qs;
969 struct got_commit_object *commit = NULL;
970 struct got_object_id *commit_id = NULL;
971 struct got_reflist_head refs;
972 struct got_blob_object *blob = NULL;
973 char *path = NULL, *in_repo_path = NULL;
974 int bin, obj_type, fd = -1;
980 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
981 qs->folder ? "/" : "", qs->file) == -1) {
982 error = got_error_from_errno("asprintf");
986 error = got_repo_map_path(&in_repo_path, repo, path);
990 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
991 GOT_OBJ_TYPE_COMMIT, &refs, repo);
995 error = got_object_open_as_commit(&commit, repo, commit_id);
999 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
1003 if (commit_id == NULL) {
1004 error = got_error(GOT_ERR_NO_OBJ);
1008 error = got_object_get_type(&obj_type, repo, commit_id);
1012 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1013 error = got_error(GOT_ERR_OBJ_TYPE);
1017 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
1021 error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
1025 error = got_object_blob_is_binary(&bin, blob);
1030 error = gotweb_render_content_type_file(c,
1031 "application/octet-stream", qs->file, NULL);
1033 error = gotweb_render_content_type(c, "text/plain");
1036 log_warnx("%s: %s", __func__, error->msg);
1041 error = got_object_blob_read_block(&len, blob);
1046 buf = got_object_blob_get_read_buf(blob);
1047 fcgi_gen_binary_response(c, buf, len);
1051 got_object_commit_close(commit);
1052 if (fd != -1 && close(fd) == -1 && error == NULL)
1053 error = got_error_from_errno("close");
1055 got_object_blob_close(blob);
1066 char datebuf[11]; /* YYYY-MM-DD + NUL */
1069 struct blame_cb_args {
1070 struct blame_line *lines;
1074 off_t *line_offsets;
1076 struct got_repository *repo;
1080 static const struct got_error *
1081 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1082 struct got_commit_object *commit, struct got_object_id *id)
1084 const struct got_error *err = NULL;
1085 struct blame_cb_args *a = arg;
1086 struct blame_line *bline;
1087 struct request *c = a->c;
1088 struct transport *t = c->t;
1089 struct repo_dir *repo_dir = t->repo_dir;
1090 char *line = NULL, *eline = NULL;
1091 size_t linesize = 0;
1094 time_t committer_time;
1097 if (nlines != a->nlines ||
1098 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1099 return got_error(GOT_ERR_RANGE);
1102 return NULL; /* no change in this commit */
1104 /* Annotate this line. */
1105 bline = &a->lines[lineno - 1];
1106 if (bline->annotated)
1108 err = got_object_id_str(&bline->id_str, id);
1112 bline->committer = strdup(got_object_commit_get_committer(commit));
1113 if (bline->committer == NULL) {
1114 err = got_error_from_errno("strdup");
1118 committer_time = got_object_commit_get_committer_time(commit);
1119 if (gmtime_r(&committer_time, &tm) == NULL)
1120 return got_error_from_errno("gmtime_r");
1121 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1123 err = got_error(GOT_ERR_NO_SPACE);
1126 bline->annotated = 1;
1128 /* Print lines annotated so far. */
1129 bline = &a->lines[a->lineno_cur - 1];
1130 if (!bline->annotated)
1133 offset = a->line_offsets[a->lineno_cur - 1];
1134 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1135 err = got_error_from_errno("fseeko");
1139 while (a->lineno_cur <= a->nlines && bline->annotated) {
1140 char *smallerthan, *at, *nl, *committer;
1143 if (getline(&line, &linesize, a->f) == -1) {
1145 err = got_error_from_errno("getline");
1149 committer = bline->committer;
1150 smallerthan = strchr(committer, '<');
1151 if (smallerthan && smallerthan[1] != '\0')
1152 committer = smallerthan + 1;
1153 at = strchr(committer, '@');
1156 len = strlen(committer);
1158 committer[8] = '\0';
1160 nl = strchr(line, '\n');
1164 err = gotweb_escape_html(&eline, line);
1168 if (fcgi_printf(c, "<div class='blame_wrapper'>"
1169 "<div class='blame_number'>%.*d</div>"
1170 "<div class='blame_hash'>",
1171 a->nlines_prec, a->lineno_cur) == -1)
1174 r = gotweb_link(c, &(struct gotweb_url){
1178 .path = repo_dir->name,
1179 .commit = bline->id_str,
1180 }, "%.8s", bline->id_str);
1186 "<div class='blame_date'>%s</div>"
1187 "<div class='blame_author'>%s</div>"
1188 "<div class='blame_code'>%s</div>"
1189 "</div>", /* .blame_wrapper */
1197 bline = &a->lines[a->lineno_cur - 1];
1208 const struct got_error *
1209 got_output_file_blame(struct request *c)
1211 const struct got_error *error = NULL;
1212 struct transport *t = c->t;
1213 struct got_repository *repo = t->repo;
1214 struct querystring *qs = c->t->qs;
1215 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1216 struct got_commit_object *commit = NULL;
1217 struct got_reflist_head refs;
1218 struct got_blob_object *blob = NULL;
1219 char *path = NULL, *in_repo_path = NULL;
1220 struct blame_cb_args bca;
1221 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1224 FILE *f1 = NULL, *f2 = NULL;
1230 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1231 qs->folder ? "/" : "", qs->file) == -1) {
1232 error = got_error_from_errno("asprintf");
1236 error = got_repo_map_path(&in_repo_path, repo, path);
1240 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1241 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1245 error = got_object_open_as_commit(&commit, repo, commit_id);
1249 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1253 error = got_object_get_type(&obj_type, repo, obj_id);
1257 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1258 error = got_error(GOT_ERR_OBJ_TYPE);
1262 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1266 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1270 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1274 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1275 &bca.line_offsets, bca.f, blob);
1276 if (error || bca.nlines == 0)
1279 /* Don't include \n at EOF in the blame line count. */
1280 if (bca.line_offsets[bca.nlines - 1] == filesize)
1283 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1284 if (bca.lines == NULL) {
1285 error = got_error_from_errno("calloc");
1289 bca.nlines_prec = 0;
1298 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1302 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1306 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1310 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1314 error = got_blame(in_repo_path, commit_id, repo,
1315 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1320 free(bca.line_offsets);
1321 for (i = 0; i < bca.nlines; i++) {
1322 struct blame_line *bline = &bca.lines[i];
1323 free(bline->id_str);
1324 free(bline->committer);
1328 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1329 error = got_error_from_errno("close");
1330 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1331 error = got_error_from_errno("close");
1332 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1333 error = got_error_from_errno("close");
1335 const struct got_error *bca_err =
1336 got_gotweb_flushfile(bca.f, fd1);
1341 const struct got_error *f1_err =
1342 got_gotweb_flushfile(f1, fd5);
1347 const struct got_error *f2_err =
1348 got_gotweb_flushfile(f2, fd6);
1353 got_object_commit_close(commit);
1355 got_object_blob_close(blob);
1360 got_ref_list_free(&refs);
1364 const struct got_error *
1365 got_output_repo_diff(struct request *c)
1367 const struct got_error *error = NULL;
1368 struct transport *t = c->t;
1369 struct got_repository *repo = t->repo;
1370 struct repo_commit *rc = NULL;
1371 struct got_object_id *id1 = NULL, *id2 = NULL;
1372 struct got_reflist_head refs;
1373 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1374 char *label1 = NULL, *label2 = NULL, *line = NULL;
1375 char *newline, *eline = NULL, *color = NULL;
1376 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1377 size_t linesize = 0;
1383 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1387 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1391 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1395 rc = TAILQ_FIRST(&t->repo_commits);
1397 if (rc->parent_id != NULL &&
1398 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1399 error = got_repo_match_object_id(&id1, &label1,
1400 rc->parent_id, GOT_OBJ_TYPE_ANY,
1406 error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1407 GOT_OBJ_TYPE_ANY, &refs, repo);
1411 error = got_object_get_type(&obj_type, repo, id2);
1415 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1419 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1424 case GOT_OBJ_TYPE_BLOB:
1425 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1426 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1429 case GOT_OBJ_TYPE_TREE:
1430 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1431 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1434 case GOT_OBJ_TYPE_COMMIT:
1435 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1436 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1440 error = got_error(GOT_ERR_OBJ_TYPE);
1445 if (fseek(f1, 0, SEEK_SET) == -1) {
1446 error = got_ferror(f1, GOT_ERR_IO);
1450 if (fseek(f2, 0, SEEK_SET) == -1) {
1451 error = got_ferror(f2, GOT_ERR_IO);
1455 if (fseek(f3, 0, SEEK_SET) == -1) {
1456 error = got_ferror(f3, GOT_ERR_IO);
1460 while ((linelen = getline(&line, &linesize, f3)) != -1) {
1461 if (strncmp(line, "-", 1) == 0) {
1462 color = strdup("diff_minus");
1463 if (color == NULL) {
1464 error = got_error_from_errno("strdup");
1467 } else if (strncmp(line, "+", 1) == 0) {
1468 color = strdup("diff_plus");
1469 if (color == NULL) {
1470 error = got_error_from_errno("strdup");
1473 } else if (strncmp(line, "@@", 2) == 0) {
1474 color = strdup("diff_chunk_header");
1475 if (color == NULL) {
1476 error = got_error_from_errno("strdup");
1479 } else if (strncmp(line, "@@", 2) == 0) {
1480 color = strdup("diff_chunk_header");
1481 if (color == NULL) {
1482 error = got_error_from_errno("strdup");
1485 } else if (strncmp(line, "commit +", 8) == 0) {
1486 color = strdup("diff_meta");
1487 if (color == NULL) {
1488 error = got_error_from_errno("strdup");
1491 } else if (strncmp(line, "commit -", 8) == 0) {
1492 color = strdup("diff_meta");
1493 if (color == NULL) {
1494 error = got_error_from_errno("strdup");
1497 } else if (strncmp(line, "blob +", 6) == 0) {
1498 color = strdup("diff_meta");
1499 if (color == NULL) {
1500 error = got_error_from_errno("strdup");
1503 } else if (strncmp(line, "blob -", 6) == 0) {
1504 color = strdup("diff_meta");
1505 if (color == NULL) {
1506 error = got_error_from_errno("strdup");
1509 } else if (strncmp(line, "file +", 6) == 0) {
1510 color = strdup("diff_meta");
1511 if (color == NULL) {
1512 error = got_error_from_errno("strdup");
1515 } else if (strncmp(line, "file -", 6) == 0) {
1516 color = strdup("diff_meta");
1517 if (color == NULL) {
1518 error = got_error_from_errno("strdup");
1521 } else if (strncmp(line, "from:", 5) == 0) {
1522 color = strdup("diff_author");
1523 if (color == NULL) {
1524 error = got_error_from_errno("strdup");
1527 } else if (strncmp(line, "via:", 4) == 0) {
1528 color = strdup("diff_author");
1529 if (color == NULL) {
1530 error = got_error_from_errno("strdup");
1533 } else if (strncmp(line, "date:", 5) == 0) {
1534 color = strdup("diff_date");
1535 if (color == NULL) {
1536 error = got_error_from_errno("strdup");
1541 newline = strchr(line, '\n');
1545 error = gotweb_escape_html(&eline, line);
1549 fcgi_printf(c, "<div class='diff_line %s'>%s</div>\n",
1550 color ? color : "", eline);
1555 wrlen = wrlen + linelen;
1559 if (linelen == -1 && ferror(f3))
1560 error = got_error_from_errno("getline");
1563 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1564 error = got_error_from_errno("close");
1565 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1566 error = got_error_from_errno("close");
1568 const struct got_error *f1_err =
1569 got_gotweb_flushfile(f1, fd1);
1574 const struct got_error *f2_err =
1575 got_gotweb_flushfile(f2, fd2);
1580 const struct got_error *f3_err =
1581 got_gotweb_flushfile(f3, fd3);
1585 got_ref_list_free(&refs);
1595 static const struct got_error *
1596 got_init_repo_commit(struct repo_commit **rc)
1598 *rc = calloc(1, sizeof(**rc));
1600 return got_error_from_errno2("%s: calloc", __func__);
1603 (*rc)->refs_str = NULL;
1604 (*rc)->commit_id = NULL;
1605 (*rc)->committer = NULL;
1606 (*rc)->author = NULL;
1607 (*rc)->parent_id = NULL;
1608 (*rc)->tree_id = NULL;
1609 (*rc)->commit_msg = NULL;
1614 static const struct got_error *
1615 got_init_repo_tag(struct repo_tag **rt)
1617 *rt = calloc(1, sizeof(**rt));
1619 return got_error_from_errno2("%s: calloc", __func__);
1621 (*rt)->commit_id = NULL;
1622 (*rt)->tag_name = NULL;
1623 (*rt)->tag_commit = NULL;
1624 (*rt)->commit_msg = NULL;
1625 (*rt)->tagger = NULL;