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>
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_reference.h"
33 #include "got_repository.h"
35 #include "got_cancel.h"
37 #include "got_commit_graph.h"
38 #include "got_blame.h"
39 #include "got_privsep.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 *);
56 isbinary(const uint8_t *buf, size_t n)
58 return memchr(buf, '\0', n) != NULL;
62 static const struct got_error *
63 got_gotweb_flushfile(FILE *f, int fd)
65 if (fseek(f, 0, SEEK_SET) == -1)
66 return got_error_from_errno("fseek");
68 if (ftruncate(fd, 0) == -1)
69 return got_error_from_errno("ftruncate");
72 return got_error_from_errno("fsync");
74 if (f && fclose(f) == EOF)
75 return got_error_from_errno("fclose");
77 if (fd != -1 && close(fd) != -1)
78 return got_error_from_errno("close");
83 static const struct got_error *
84 got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
86 const struct got_error *error = NULL;
93 *f = fdopen(*fd, "w+");
96 error = got_error(GOT_ERR_PRIVSEP_NO_FD);
102 static const struct got_error *
103 got_gotweb_dupfd(int *priv_fd, int *fd)
105 const struct got_error *error = NULL;
115 const struct got_error *
116 got_get_repo_owner(char **owner, struct request *c, char *dir)
118 const struct got_error *error = NULL;
119 struct server *srv = c->srv;
120 struct transport *t = c->t;
121 struct got_repository *repo = t->repo;
122 const char *gitconfig_owner;
126 if (srv->show_repo_owner == 0)
129 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
130 if (gitconfig_owner) {
131 *owner = strdup(gitconfig_owner);
133 return got_error_from_errno("strdup");
138 const struct got_error *
139 got_get_repo_age(char **repo_age, struct request *c, char *dir,
140 const char *refname, int ref_tm)
142 const struct got_error *error = NULL;
143 struct server *srv = c->srv;
144 struct transport *t = c->t;
145 struct got_repository *repo = t->repo;
146 struct got_commit_object *commit = NULL;
147 struct got_reflist_head refs;
148 struct got_reflist_entry *re;
149 time_t committer_time = 0, cmp_time = 0;
154 if (srv->show_repo_age == 0)
157 error = got_ref_list(&refs, repo, "refs/heads",
158 got_ref_cmp_by_name, NULL);
163 * Find the youngest branch tip in the repository, or the age of
164 * the a specific branch tip if a name was provided by the caller.
166 TAILQ_FOREACH(re, &refs, entry) {
167 struct got_object_id *id = NULL;
169 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
172 error = got_ref_resolve(&id, repo, re->ref);
176 error = got_object_open_as_commit(&commit, repo, id);
182 got_object_commit_get_committer_time(commit);
183 got_object_commit_close(commit);
184 if (cmp_time < committer_time)
185 cmp_time = committer_time;
192 committer_time = cmp_time;
193 error = gotweb_get_time_str(repo_age, committer_time, ref_tm);
196 got_ref_list_free(&refs);
200 static const struct got_error *
201 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
202 struct got_commit_object *commit, struct got_reflist_head *refs,
203 struct got_object_id *id)
205 const struct got_error *error = NULL;
206 struct got_reflist_entry *re;
207 struct got_object_id *id2 = NULL;
208 struct got_object_qid *parent_id;
209 struct transport *t = c->t;
210 struct querystring *qs = c->t->qs;
211 char *commit_msg = NULL, *commit_msg0;
213 TAILQ_FOREACH(re, refs, entry) {
216 struct got_tag_object *tag = NULL;
217 struct got_object_id *ref_id;
220 if (got_ref_is_symbolic(re->ref))
223 name = got_ref_get_name(re->ref);
224 if (strncmp(name, "refs/", 5) == 0)
226 if (strncmp(name, "got/", 4) == 0)
228 if (strncmp(name, "heads/", 6) == 0)
230 if (strncmp(name, "remotes/", 8) == 0) {
232 s = strstr(name, "/" GOT_REF_HEAD);
233 if (s != NULL && s[strlen(s)] == '\0')
236 error = got_ref_resolve(&ref_id, t->repo, re->ref);
239 if (strncmp(name, "tags/", 5) == 0) {
240 error = got_object_open_as_tag(&tag, t->repo, ref_id);
242 if (error->code != GOT_ERR_OBJ_TYPE) {
247 * Ref points at something other
254 cmp = got_object_id_cmp(tag ?
255 got_object_tag_get_object_id(tag) : ref_id, id);
258 got_object_tag_close(tag);
261 s = repo_commit->refs_str;
262 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
263 s ? ", " : "", name) == -1) {
264 error = got_error_from_errno("asprintf");
266 repo_commit->refs_str = NULL;
272 error = got_object_id_str(&repo_commit->commit_id, id);
276 error = got_object_id_str(&repo_commit->tree_id,
277 got_object_commit_get_tree_id(commit));
281 if (qs->action == DIFF) {
282 parent_id = STAILQ_FIRST(
283 got_object_commit_get_parent_ids(commit));
284 if (parent_id != NULL) {
285 id2 = got_object_id_dup(&parent_id->id);
286 error = got_object_id_str(&repo_commit->parent_id, id2);
291 repo_commit->parent_id = strdup("/dev/null");
292 if (repo_commit->parent_id == NULL) {
293 error = got_error_from_errno("strdup");
299 repo_commit->committer_time =
300 got_object_commit_get_committer_time(commit);
302 repo_commit->author =
303 strdup(got_object_commit_get_author(commit));
304 if (repo_commit->author == NULL) {
305 error = got_error_from_errno("strdup");
308 repo_commit->committer =
309 strdup(got_object_commit_get_committer(commit));
310 if (repo_commit->committer == NULL) {
311 error = got_error_from_errno("strdup");
314 error = got_object_commit_get_logmsg(&commit_msg0, commit);
318 commit_msg = commit_msg0;
319 while (*commit_msg == '\n')
322 repo_commit->commit_msg = strdup(commit_msg);
323 if (repo_commit->commit_msg == NULL)
324 error = got_error_from_errno("strdup");
329 const struct got_error *
330 got_get_repo_commits(struct request *c, int limit)
332 const struct got_error *error = NULL;
333 struct got_object_id *id = NULL;
334 struct got_commit_graph *graph = NULL;
335 struct got_commit_object *commit = NULL;
336 struct got_reflist_head refs;
337 struct got_reference *ref;
338 struct repo_commit *repo_commit = NULL;
339 struct server *srv = c->srv;
340 struct transport *t = c->t;
341 struct got_repository *repo = t->repo;
342 struct querystring *qs = t->qs;
343 struct repo_dir *repo_dir = t->repo_dir;
344 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
345 int chk_next = 0, chk_multi = 0, commit_found = 0;
346 int obj_type, limit_chk = 0;
350 if (qs->file != NULL && strlen(qs->file) > 0)
351 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
353 return got_error_from_errno("asprintf");
355 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
356 repo_dir->name) == -1)
357 return got_error_from_errno("asprintf");
359 error = got_init_repo_commit(&repo_commit);
364 * XXX: jumping directly to a commit id via
365 * got_repo_match_object_id_prefix significantly improves performance,
366 * but does not allow us to create a PREVIOUS button, since commits can
367 * only be itereated forward. So, we have to match as we iterate from
370 if (qs->action == BRIEFS || qs->action == COMMITS ||
371 (qs->action == TREE && qs->commit == NULL)) {
372 error = got_ref_open(&ref, repo, qs->headref, 0);
376 error = got_ref_resolve(&id, repo, ref);
380 } else if (qs->commit != NULL) {
381 error = got_ref_open(&ref, repo, qs->commit, 0);
383 error = got_ref_resolve(&id, repo, ref);
386 error = got_object_get_type(&obj_type, repo, id);
390 if (obj_type == GOT_OBJ_TYPE_TAG) {
391 struct got_tag_object *tag;
392 error = got_object_open_as_tag(&tag, repo, id);
395 if (got_object_tag_get_object_type(tag) !=
396 GOT_OBJ_TYPE_COMMIT) {
397 got_object_tag_close(tag);
398 error = got_error(GOT_ERR_OBJ_TYPE);
402 id = got_object_id_dup(
403 got_object_tag_get_object_id(tag));
405 error = got_error_from_errno(
406 "got_object_id_dup");
407 got_object_tag_close(tag);
410 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
411 error = got_error(GOT_ERR_OBJ_TYPE);
415 error = got_repo_match_object_id_prefix(&id, qs->commit,
416 GOT_OBJ_TYPE_COMMIT, repo);
421 error = got_repo_map_path(&in_repo_path, repo, repo_path);
425 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
429 if (qs->file != NULL && strlen(qs->file) > 0) {
430 error = got_commit_graph_open(&graph, file_path, 0);
434 error = got_commit_graph_open(&graph, in_repo_path, 0);
439 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
444 if (limit_chk == ((limit * qs->page) - (limit - 1)) &&
445 commit_found == 0 && repo_commit->commit_id != NULL) {
446 t->prev_id = strdup(repo_commit->commit_id);
447 if (t->prev_id == NULL) {
448 error = got_error_from_errno("strdup");
453 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
456 if (error->code == GOT_ERR_ITER_COMPLETED)
463 error = got_object_open_as_commit(&commit, repo, id);
467 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
472 error = got_get_repo_commit(c, repo_commit, commit,
477 if (qs->commit != NULL && commit_found == 0 && limit != 1) {
478 if (strcmp(qs->commit, repo_commit->commit_id) == 0)
480 else if (qs->file != NULL && strlen(qs->file) > 0 &&
491 struct repo_commit *new_repo_commit = NULL;
492 error = got_init_repo_commit(&new_repo_commit);
496 TAILQ_INSERT_TAIL(&t->repo_commits, new_repo_commit, entry);
498 error = got_get_repo_commit(c, new_repo_commit, commit,
506 if (limit == 1 && chk_multi == 0 &&
507 srv->max_commits_display != 1)
513 * check for one more commit before breaking,
514 * so we know whether to navigate through briefs
515 * commits and summary
517 if (chk_next && (qs->action == BRIEFS ||
518 qs->action == COMMITS || qs->action == SUMMARY)) {
519 t->next_id = strdup(new_repo_commit->commit_id);
520 if (t->next_id == NULL) {
521 error = got_error_from_errno("strdup");
525 got_object_commit_close(commit);
528 if (t->next_id == NULL) {
529 error = got_error_from_errno("strdup");
532 TAILQ_REMOVE(&t->repo_commits, new_repo_commit,
534 gotweb_free_repo_commit(new_repo_commit);
538 got_ref_list_free(&refs);
539 if (error || (limit && --limit == 0)) {
540 if (commit_found || (qs->file != NULL &&
541 strlen(qs->file) > 0))
547 got_object_commit_close(commit);
552 gotweb_free_repo_commit(repo_commit);
554 got_object_commit_close(commit);
556 got_commit_graph_close(graph);
557 got_ref_list_free(&refs);
564 const struct got_error *
565 got_get_repo_tags(struct request *c, int limit)
567 const struct got_error *error = NULL;
568 struct got_object_id *id = NULL;
569 struct got_commit_object *commit = NULL;
570 struct got_reflist_head refs;
571 struct got_reference *ref;
572 struct got_reflist_entry *re;
573 struct server *srv = c->srv;
574 struct transport *t = c->t;
575 struct got_repository *repo = t->repo;
576 struct querystring *qs = t->qs;
577 struct repo_dir *repo_dir = t->repo_dir;
578 struct got_tag_object *tag = NULL;
579 struct repo_tag *rt = NULL, *trt = NULL;
580 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
581 char *commit_msg = NULL, *commit_msg0 = NULL;
582 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
586 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
587 repo_dir->name) == -1)
588 return got_error_from_errno("asprintf");
593 if (qs->commit == NULL && qs->action == TAGS) {
594 error = got_ref_open(&ref, repo, qs->headref, 0);
597 error = got_ref_resolve(&id, repo, ref);
601 } else if (qs->commit == NULL && qs->action == TAG) {
602 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
605 error = got_repo_match_object_id_prefix(&id, qs->commit,
606 GOT_OBJ_TYPE_COMMIT, repo);
611 if (qs->action != SUMMARY && qs->action != TAGS) {
612 error = got_object_open_as_commit(&commit, repo, id);
615 error = got_object_commit_get_logmsg(&commit_msg0, commit);
619 got_object_commit_close(commit);
624 error = got_repo_map_path(&in_repo_path, repo, repo_path);
628 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
637 * XXX: again, see previous message about caching
640 TAILQ_FOREACH(re, &refs, entry) {
641 struct repo_tag *new_repo_tag = NULL;
642 error = got_init_repo_tag(&new_repo_tag);
646 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
648 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
649 if (new_repo_tag->tag_name == NULL) {
650 error = got_error_from_errno("strdup");
654 error = got_ref_resolve(&id, repo, re->ref);
658 error = got_object_open_as_tag(&tag, repo, id);
660 if (error->code != GOT_ERR_OBJ_TYPE) {
665 /* "lightweight" tag */
666 error = got_object_open_as_commit(&commit, repo, id);
672 new_repo_tag->tagger =
673 strdup(got_object_commit_get_committer(commit));
674 if (new_repo_tag->tagger == NULL) {
675 error = got_error_from_errno("strdup");
678 new_repo_tag->tagger_time =
679 got_object_commit_get_committer_time(commit);
680 error = got_object_id_str(&id_str, id);
688 new_repo_tag->tagger =
689 strdup(got_object_tag_get_tagger(tag));
690 if (new_repo_tag->tagger == NULL) {
691 error = got_error_from_errno("strdup");
694 new_repo_tag->tagger_time =
695 got_object_tag_get_tagger_time(tag);
696 error = got_object_id_str(&id_str,
697 got_object_tag_get_object_id(tag));
702 new_repo_tag->commit_id = strdup(id_str);
703 if (new_repo_tag->commit_id == NULL)
706 if (commit_found == 0 && qs->commit != NULL &&
707 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
715 * check for one more commit before breaking,
716 * so we know whether to navigate through briefs
717 * commits and summary
720 t->next_id = strdup(new_repo_tag->commit_id);
721 if (t->next_id == NULL) {
722 error = got_error_from_errno("strdup");
726 got_object_commit_close(commit);
729 if (t->next_id == NULL) {
730 error = got_error_from_errno("strdup");
733 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
734 gotweb_free_repo_tag(new_repo_tag);
739 error = got_object_commit_get_logmsg(&new_repo_tag->
743 got_object_commit_close(commit);
746 new_repo_tag->tag_commit =
747 strdup(got_object_tag_get_message(tag));
748 if (new_repo_tag->tag_commit == NULL) {
749 error = got_error_from_errno("strdup");
754 while (*new_repo_tag->tag_commit == '\n')
755 new_repo_tag->tag_commit++;
757 if (qs->action != SUMMARY && qs->action != TAGS) {
758 commit_msg = commit_msg0;
759 while (*commit_msg == '\n')
762 new_repo_tag->commit_msg = strdup(commit_msg);
763 if (new_repo_tag->commit_msg == NULL) {
764 error = got_error_from_errno("strdup");
771 if (limit && --limit == 0) {
782 * we have tailq populated, so find previous commit id
783 * for navigation through briefs and commits
785 if (t->tag_count == 0) {
786 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
787 TAILQ_REMOVE(&t->repo_tags, rt, entry);
788 gotweb_free_repo_tag(rt);
791 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
793 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
795 if (commit_found == 0 && rt->commit_id != NULL &&
796 strcmp(qs->commit, rt->commit_id) != 0) {
800 if (c_cnt == srv->max_commits_display ||
801 rt == TAILQ_FIRST(&t->repo_tags)) {
802 t->prev_id = strdup(rt->commit_id);
803 if (t->prev_id == NULL)
804 error = got_error_from_errno("strdup");
812 got_object_commit_close(commit);
813 got_ref_list_free(&refs);
819 const struct got_error *
820 got_output_repo_tree(struct request *c)
822 const struct got_error *error = NULL;
823 struct transport *t = c->t;
824 struct got_commit_object *commit = NULL;
825 struct got_repository *repo = t->repo;
826 struct querystring *qs = t->qs;
827 struct repo_commit *rc = NULL;
828 struct got_object_id *tree_id = NULL, *commit_id = NULL;
829 struct got_reflist_head refs;
830 struct got_tree_object *tree = NULL;
831 struct repo_dir *repo_dir = t->repo_dir;
833 char *path = NULL, *in_repo_path = NULL, *build_folder = NULL;
834 char *modestr = NULL, *name = NULL, *class = NULL;
835 int nentries, i, class_flip = 0;
839 rc = TAILQ_FIRST(&t->repo_commits);
841 if (qs->folder != NULL) {
842 path = strdup(qs->folder);
844 error = got_error_from_errno("strdup");
848 error = got_repo_map_path(&in_repo_path, repo, repo_dir->path);
855 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
856 GOT_OBJ_TYPE_COMMIT, &refs, repo);
860 error = got_object_open_as_commit(&commit, repo, commit_id);
864 error = got_object_id_by_path(&tree_id, repo, commit, path);
868 error = got_object_open_as_tree(&tree, repo, tree_id);
872 nentries = got_object_tree_get_nentries(tree);
874 for (i = 0; i < nentries; i++) {
875 struct got_tree_entry *te;
878 te = got_object_tree_get_entry(tree, i);
880 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
884 modestr = strdup("");
885 if (modestr == NULL) {
886 error = got_error_from_errno("strdup");
889 mode = got_tree_entry_get_mode(te);
890 if (got_object_tree_entry_is_submodule(te)) {
892 modestr = strdup("$");
893 if (modestr == NULL) {
894 error = got_error_from_errno("strdup");
897 } else if (S_ISLNK(mode)) {
899 modestr = strdup("@");
900 if (modestr == NULL) {
901 error = got_error_from_errno("strdup");
904 } else if (S_ISDIR(mode)) {
906 modestr = strdup("/");
907 if (modestr == NULL) {
908 error = got_error_from_errno("strdup");
911 } else if (mode & S_IXUSR) {
913 modestr = strdup("*");
914 if (modestr == NULL) {
915 error = got_error_from_errno("strdup");
920 if (class_flip == 0) {
921 class = strdup("back_lightgray");
923 error = got_error_from_errno("strdup");
928 class = strdup("back_white");
930 error = got_error_from_errno("strdup");
936 name = strdup(got_tree_entry_get_name(te));
938 error = got_error_from_errno("strdup");
942 if (asprintf(&build_folder, "%s/%s",
943 qs->folder ? qs->folder : "",
944 got_tree_entry_get_name(te)) == -1) {
945 error = got_error_from_errno("asprintf");
949 if (fcgi_gen_response(c,
950 "<div id='tree_wrapper'>\n") == -1)
953 if (fcgi_gen_response(c, "<div id='tree_line' "
956 if (fcgi_gen_response(c, class) == -1)
958 if (fcgi_gen_response(c, "'>") == -1)
961 if (fcgi_gen_response(c, "<a class='diff_directory' "
962 "href='?index_page=") == -1)
964 if (fcgi_gen_response(c, qs->index_page_str) == -1)
966 if (fcgi_gen_response(c, "&path=") == -1)
968 if (fcgi_gen_response(c, qs->path) == -1)
970 if (fcgi_gen_response(c, "&action=tree") == -1)
972 if (fcgi_gen_response(c, "&commit=") == -1)
974 if (fcgi_gen_response(c, rc->commit_id) == -1)
976 if (fcgi_gen_response(c, "&folder=") == -1)
978 if (fcgi_gen_response(c, build_folder) == -1)
980 if (fcgi_gen_response(c, "'>") == -1)
982 if (fcgi_gen_response(c, name) == -1)
984 if (fcgi_gen_response(c, modestr) == -1)
986 if (fcgi_gen_response(c, "</a>") == -1)
989 if (fcgi_gen_response(c, "</div>\n") == -1)
992 if (fcgi_gen_response(c, "<div id='tree_line_blank' "
995 if (fcgi_gen_response(c, class) == -1)
997 if (fcgi_gen_response(c, "'>") == -1)
999 if (fcgi_gen_response(c, " ") == -1)
1001 if (fcgi_gen_response(c, "</div>\n") == -1)
1004 if (fcgi_gen_response(c, "</div>\n") == -1)
1009 name = strdup(got_tree_entry_get_name(te));
1011 error = got_error_from_errno("strdup");
1015 if (fcgi_gen_response(c,
1016 "<div id='tree_wrapper'>\n") == -1)
1018 if (fcgi_gen_response(c, "<div id='tree_line' "
1021 if (fcgi_gen_response(c, class) == -1)
1023 if (fcgi_gen_response(c, "'>") == -1)
1026 if (fcgi_gen_response(c,
1027 "<a href='?index_page=") == -1)
1030 if (fcgi_gen_response(c, qs->index_page_str) == -1)
1033 if (fcgi_gen_response(c, "&path=") == -1)
1035 if (fcgi_gen_response(c, qs->path) == -1)
1038 if (fcgi_gen_response(c, "&action=blob") == -1)
1041 if (fcgi_gen_response(c, "&commit=") == -1)
1043 if (fcgi_gen_response(c, rc->commit_id) == -1)
1046 if (fcgi_gen_response(c, "&folder=") == -1)
1048 if (fcgi_gen_response(c, qs->folder) == -1)
1051 if (fcgi_gen_response(c, "&file=") == -1)
1053 if (fcgi_gen_response(c, name) == -1)
1056 if (fcgi_gen_response(c, "'>") == -1)
1058 if (fcgi_gen_response(c, name) == -1)
1060 if (fcgi_gen_response(c, modestr) == -1)
1063 if (fcgi_gen_response(c, "</a>") == -1)
1066 if (fcgi_gen_response(c, "</div>\n") == -1)
1069 if (fcgi_gen_response(c, "<div id='tree_line_blank' "
1072 if (fcgi_gen_response(c, class) == -1)
1074 if (fcgi_gen_response(c, "'>") == -1)
1077 if (fcgi_gen_response(c,
1078 "<a href='?index_page=") == -1)
1081 if (fcgi_gen_response(c, qs->index_page_str) == -1)
1084 if (fcgi_gen_response(c, "&path=") == -1)
1086 if (fcgi_gen_response(c, qs->path) == -1)
1089 if (fcgi_gen_response(c, "&action=commits") == -1)
1092 if (fcgi_gen_response(c, "&commit=") == -1)
1094 if (fcgi_gen_response(c, rc->commit_id) == -1)
1097 if (fcgi_gen_response(c, "&folder=") == -1)
1099 if (fcgi_gen_response(c, qs->folder) == -1)
1102 if (fcgi_gen_response(c, "&file=") == -1)
1104 if (fcgi_gen_response(c, name) == -1)
1107 if (fcgi_gen_response(c, "'>") == -1)
1110 if (fcgi_gen_response(c, "commits") == -1)
1112 if (fcgi_gen_response(c, "</a>\n") == -1)
1115 if (fcgi_gen_response(c, " | \n") == -1)
1118 if (fcgi_gen_response(c,
1119 "<a href='?index_page=") == -1)
1122 if (fcgi_gen_response(c, qs->index_page_str) == -1)
1125 if (fcgi_gen_response(c, "&path=") == -1)
1127 if (fcgi_gen_response(c, qs->path) == -1)
1130 if (fcgi_gen_response(c, "&action=blame") == -1)
1133 if (fcgi_gen_response(c, "&commit=") == -1)
1135 if (fcgi_gen_response(c, rc->commit_id) == -1)
1138 if (fcgi_gen_response(c, "&folder=") == -1)
1140 if (fcgi_gen_response(c, qs->folder) == -1)
1143 if (fcgi_gen_response(c, "&file=") == -1)
1145 if (fcgi_gen_response(c, name) == -1)
1148 if (fcgi_gen_response(c, "'>") == -1)
1151 if (fcgi_gen_response(c, "blame") == -1)
1153 if (fcgi_gen_response(c, "</a>\n") == -1)
1156 if (fcgi_gen_response(c, "</div>\n") == -1)
1158 if (fcgi_gen_response(c, "</div>\n") == -1)
1164 build_folder = NULL;
1179 got_ref_list_free(&refs);
1181 got_object_commit_close(commit);
1187 const struct got_error *
1188 got_output_file_blob(struct request *c)
1190 const struct got_error *error = NULL;
1191 struct transport *t = c->t;
1192 struct got_repository *repo = t->repo;
1193 struct querystring *qs = c->t->qs;
1194 struct got_commit_object *commit = NULL;
1195 struct got_object_id *commit_id = NULL;
1196 struct got_reflist_head refs;
1197 struct got_blob_object *blob = NULL;
1198 char *path = NULL, *in_repo_path = NULL;
1199 int obj_type, set_mime = 0, fd = -1;
1205 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1206 qs->folder ? "/" : "", qs->file) == -1) {
1207 error = got_error_from_errno("asprintf");
1211 error = got_repo_map_path(&in_repo_path, repo, path);
1215 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1216 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1220 error = got_object_open_as_commit(&commit, repo, commit_id);
1224 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
1228 if (commit_id == NULL) {
1229 error = got_error(GOT_ERR_NO_OBJ);
1233 error = got_object_get_type(&obj_type, repo, commit_id);
1237 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1238 error = got_error(GOT_ERR_OBJ_TYPE);
1242 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
1246 error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
1249 hdrlen = got_object_blob_get_hdrlen(blob);
1251 error = got_object_blob_read_block(&len, blob);
1254 buf = got_object_blob_get_read_buf(blob);
1257 * Skip blob object header first time around,
1258 * which also contains a zero byte.
1261 if (set_mime == 0) {
1262 if (isbinary(buf, len - hdrlen)) {
1263 error = gotweb_render_content_type_file(c,
1264 "application/octet-stream",
1267 log_warnx("%s: %s", __func__,
1272 error = gotweb_render_content_type(c,
1275 log_warnx("%s: %s", __func__,
1282 fcgi_gen_binary_response(c, buf, len - hdrlen);
1288 got_object_commit_close(commit);
1289 if (fd != -1 && close(fd) == -1 && error == NULL)
1290 error = got_error_from_errno("close");
1292 got_object_blob_close(blob);
1303 char datebuf[11]; /* YYYY-MM-DD + NUL */
1306 struct blame_cb_args {
1307 struct blame_line *lines;
1311 off_t *line_offsets;
1313 struct got_repository *repo;
1317 static const struct got_error *
1318 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1319 struct got_commit_object *commit, struct got_object_id *id)
1321 const struct got_error *err = NULL;
1322 struct blame_cb_args *a = arg;
1323 struct blame_line *bline;
1324 struct request *c = a->c;
1325 struct transport *t = c->t;
1326 struct querystring *qs = t->qs;
1327 struct repo_dir *repo_dir = t->repo_dir;
1328 char *line = NULL, *eline = NULL;
1329 size_t linesize = 0;
1332 time_t committer_time;
1334 if (nlines != a->nlines ||
1335 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1336 return got_error(GOT_ERR_RANGE);
1339 return NULL; /* no change in this commit */
1341 /* Annotate this line. */
1342 bline = &a->lines[lineno - 1];
1343 if (bline->annotated)
1345 err = got_object_id_str(&bline->id_str, id);
1349 bline->committer = strdup(got_object_commit_get_committer(commit));
1350 if (bline->committer == NULL) {
1351 err = got_error_from_errno("strdup");
1355 committer_time = got_object_commit_get_committer_time(commit);
1356 if (gmtime_r(&committer_time, &tm) == NULL)
1357 return got_error_from_errno("gmtime_r");
1358 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1360 err = got_error(GOT_ERR_NO_SPACE);
1363 bline->annotated = 1;
1365 /* Print lines annotated so far. */
1366 bline = &a->lines[a->lineno_cur - 1];
1367 if (!bline->annotated)
1370 offset = a->line_offsets[a->lineno_cur - 1];
1371 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1372 err = got_error_from_errno("fseeko");
1376 while (bline->annotated) {
1377 int out_buff_size = 100;
1378 char *smallerthan, *at, *nl, *committer;
1379 char out_buff[out_buff_size];
1382 if (getline(&line, &linesize, a->f) == -1) {
1384 err = got_error_from_errno("getline");
1388 committer = bline->committer;
1389 smallerthan = strchr(committer, '<');
1390 if (smallerthan && smallerthan[1] != '\0')
1391 committer = smallerthan + 1;
1392 at = strchr(committer, '@');
1395 len = strlen(committer);
1397 committer[8] = '\0';
1399 nl = strchr(line, '\n');
1403 if (fcgi_gen_response(c, "<div id='blame_wrapper'>") == -1)
1405 if (fcgi_gen_response(c, "<div id='blame_number'>") == -1)
1407 if (snprintf(out_buff, strlen(out_buff), "%.*d", a->nlines_prec,
1410 if (fcgi_gen_response(c, out_buff) == -1)
1412 if (fcgi_gen_response(c, "</div>") == -1)
1415 if (fcgi_gen_response(c, "<div id='blame_hash'>") == -1)
1418 if (fcgi_gen_response(c, "<a href='?index_page=") == -1)
1420 if (fcgi_gen_response(c, qs->index_page_str) == -1)
1422 if (fcgi_gen_response(c, "&path=") == -1)
1424 if (fcgi_gen_response(c, repo_dir->name) == -1)
1426 if (fcgi_gen_response(c, "&action=diff&commit=") == -1)
1428 if (fcgi_gen_response(c, bline->id_str) == -1)
1430 if (fcgi_gen_response(c, "'>") == -1)
1432 if (snprintf(out_buff, 10, "%.8s", bline->id_str) < 0)
1434 if (fcgi_gen_response(c, out_buff) == -1)
1436 if (fcgi_gen_response(c, "</a></div>") == -1)
1439 if (fcgi_gen_response(c, "<div id='blame_date'>") == -1)
1441 if (fcgi_gen_response(c, bline->datebuf) == -1)
1443 if (fcgi_gen_response(c, "</div>") == -1)
1446 if (fcgi_gen_response(c, "<div id='blame_author'>") == -1)
1448 if (fcgi_gen_response(c, committer) == -1)
1450 if (fcgi_gen_response(c, "</div>") == -1)
1453 if (fcgi_gen_response(c, "<div id='blame_code'>") == -1)
1455 err = gotweb_escape_html(&eline, line);
1458 if (fcgi_gen_response(c, eline) == -1)
1460 if (fcgi_gen_response(c, "</div>") == -1)
1463 if (fcgi_gen_response(c, "</div>") == -1)
1466 bline = &a->lines[a->lineno_cur - 1];
1474 const struct got_error *
1475 got_output_file_blame(struct request *c)
1477 const struct got_error *error = NULL;
1478 struct transport *t = c->t;
1479 struct got_repository *repo = t->repo;
1480 struct querystring *qs = c->t->qs;
1481 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1482 struct got_commit_object *commit = NULL;
1483 struct got_reflist_head refs;
1484 struct got_blob_object *blob = NULL;
1485 char *path = NULL, *in_repo_path = NULL;
1486 struct blame_cb_args bca;
1487 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1490 FILE *f1 = NULL, *f2 = NULL;
1496 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1497 qs->folder ? "/" : "", qs->file) == -1) {
1498 error = got_error_from_errno("asprintf");
1502 error = got_repo_map_path(&in_repo_path, repo, path);
1506 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1507 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1511 error = got_object_open_as_commit(&commit, repo, commit_id);
1515 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1519 if (commit_id == NULL) {
1520 error = got_error(GOT_ERR_NO_OBJ);
1524 error = got_object_get_type(&obj_type, repo, obj_id);
1528 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1529 error = got_error(GOT_ERR_OBJ_TYPE);
1533 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1537 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1541 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1545 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1546 &bca.line_offsets, bca.f, blob);
1547 if (error || bca.nlines == 0)
1550 /* Don't include \n at EOF in the blame line count. */
1551 if (bca.line_offsets[bca.nlines - 1] == filesize)
1554 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1555 if (bca.lines == NULL) {
1556 error = got_error_from_errno("calloc");
1560 bca.nlines_prec = 0;
1569 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1573 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1577 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1581 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1585 error = got_blame(in_repo_path, commit_id, repo,
1586 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1590 free(bca.line_offsets);
1591 for (i = 0; i < bca.nlines; i++) {
1592 struct blame_line *bline = &bca.lines[i];
1593 free(bline->id_str);
1594 free(bline->committer);
1599 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1600 error = got_error_from_errno("close");
1601 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1602 error = got_error_from_errno("close");
1603 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1604 error = got_error_from_errno("close");
1606 const struct got_error *bca_err =
1607 got_gotweb_flushfile(bca.f, fd1);
1612 const struct got_error *f1_err =
1613 got_gotweb_flushfile(f1, fd5);
1618 const struct got_error *f2_err =
1619 got_gotweb_flushfile(f2, fd6);
1624 got_object_commit_close(commit);
1626 got_object_blob_close(blob);
1633 const struct got_error *
1634 got_output_repo_diff(struct request *c)
1636 const struct got_error *error = NULL;
1637 struct transport *t = c->t;
1638 struct got_repository *repo = t->repo;
1639 struct repo_commit *rc = NULL;
1640 struct got_object_id *id1 = NULL, *id2 = NULL;
1641 struct got_reflist_head refs;
1642 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1643 char *label1 = NULL, *label2 = NULL, *line = NULL;
1644 char *newline, *eline = NULL, *color = NULL;
1645 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1646 size_t linesize = 0;
1652 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1656 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1660 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1664 rc = TAILQ_FIRST(&t->repo_commits);
1666 if (rc->parent_id != NULL &&
1667 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1668 error = got_repo_match_object_id(&id1, &label1,
1669 rc->parent_id, GOT_OBJ_TYPE_ANY,
1675 error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1676 GOT_OBJ_TYPE_ANY, &refs, repo);
1680 error = got_object_get_type(&obj_type, repo, id2);
1684 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1688 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1693 case GOT_OBJ_TYPE_BLOB:
1694 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1695 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1698 case GOT_OBJ_TYPE_TREE:
1699 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1700 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1703 case GOT_OBJ_TYPE_COMMIT:
1704 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1705 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1709 error = got_error(GOT_ERR_OBJ_TYPE);
1714 if (fseek(f1, 0, SEEK_SET) == -1) {
1715 error = got_ferror(f1, GOT_ERR_IO);
1719 if (fseek(f2, 0, SEEK_SET) == -1) {
1720 error = got_ferror(f2, GOT_ERR_IO);
1724 if (fseek(f3, 0, SEEK_SET) == -1) {
1725 error = got_ferror(f3, GOT_ERR_IO);
1729 while ((linelen = getline(&line, &linesize, f3)) != -1) {
1730 if (strncmp(line, "-", 1) == 0) {
1731 color = strdup("diff_minus");
1732 if (color == NULL) {
1733 error = got_error_from_errno("strdup");
1736 } else if (strncmp(line, "+", 1) == 0) {
1737 color = strdup("diff_plus");
1738 if (color == NULL) {
1739 error = got_error_from_errno("strdup");
1742 } else if (strncmp(line, "@@", 2) == 0) {
1743 color = strdup("diff_chunk_header");
1744 if (color == NULL) {
1745 error = got_error_from_errno("strdup");
1748 } else if (strncmp(line, "@@", 2) == 0) {
1749 color = strdup("diff_chunk_header");
1750 if (color == NULL) {
1751 error = got_error_from_errno("strdup");
1754 } else if (strncmp(line, "commit +", 8) == 0) {
1755 color = strdup("diff_meta");
1756 if (color == NULL) {
1757 error = got_error_from_errno("strdup");
1760 } else if (strncmp(line, "commit -", 8) == 0) {
1761 color = strdup("diff_meta");
1762 if (color == NULL) {
1763 error = got_error_from_errno("strdup");
1766 } else if (strncmp(line, "blob +", 6) == 0) {
1767 color = strdup("diff_meta");
1768 if (color == NULL) {
1769 error = got_error_from_errno("strdup");
1772 } else if (strncmp(line, "blob -", 6) == 0) {
1773 color = strdup("diff_meta");
1774 if (color == NULL) {
1775 error = got_error_from_errno("strdup");
1778 } else if (strncmp(line, "file +", 6) == 0) {
1779 color = strdup("diff_meta");
1780 if (color == NULL) {
1781 error = got_error_from_errno("strdup");
1784 } else if (strncmp(line, "file -", 6) == 0) {
1785 color = strdup("diff_meta");
1786 if (color == NULL) {
1787 error = got_error_from_errno("strdup");
1790 } else if (strncmp(line, "from:", 5) == 0) {
1791 color = strdup("diff_author");
1792 if (color == NULL) {
1793 error = got_error_from_errno("strdup");
1796 } else if (strncmp(line, "via:", 4) == 0) {
1797 color = strdup("diff_author");
1798 if (color == NULL) {
1799 error = got_error_from_errno("strdup");
1802 } else if (strncmp(line, "date:", 5) == 0) {
1803 color = strdup("diff_date");
1804 if (color == NULL) {
1805 error = got_error_from_errno("strdup");
1809 if (fcgi_gen_response(c, "<div id='diff_line' class='") == -1)
1811 if (fcgi_gen_response(c, color ? color : "") == -1)
1813 if (fcgi_gen_response(c, "'>") == -1)
1815 newline = strchr(line, '\n');
1819 error = gotweb_escape_html(&eline, line);
1822 if (fcgi_gen_response(c, eline) == -1)
1827 if (fcgi_gen_response(c, "</div>\n") == -1)
1830 wrlen = wrlen + linelen;
1834 if (linelen == -1 && ferror(f3))
1835 error = got_error_from_errno("getline");
1838 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1839 error = got_error_from_errno("close");
1840 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1841 error = got_error_from_errno("close");
1843 const struct got_error *f1_err =
1844 got_gotweb_flushfile(f1, fd1);
1849 const struct got_error *f2_err =
1850 got_gotweb_flushfile(f2, fd2);
1855 const struct got_error *f3_err =
1856 got_gotweb_flushfile(f3, fd3);
1860 got_ref_list_free(&refs);
1870 static const struct got_error *
1871 got_init_repo_commit(struct repo_commit **rc)
1873 const struct got_error *error = NULL;
1875 *rc = calloc(1, sizeof(**rc));
1877 return got_error_from_errno2("%s: calloc", __func__);
1880 (*rc)->refs_str = NULL;
1881 (*rc)->commit_id = NULL;
1882 (*rc)->committer = NULL;
1883 (*rc)->author = NULL;
1884 (*rc)->parent_id = NULL;
1885 (*rc)->tree_id = NULL;
1886 (*rc)->commit_msg = NULL;
1891 static const struct got_error *
1892 got_init_repo_tag(struct repo_tag **rt)
1894 const struct got_error *error = NULL;
1896 *rt = calloc(1, sizeof(**rt));
1898 return got_error_from_errno2("%s: calloc", __func__);
1900 (*rt)->commit_id = NULL;
1901 (*rt)->tag_name = NULL;
1902 (*rt)->tag_commit = NULL;
1903 (*rt)->commit_msg = NULL;
1904 (*rt)->tagger = NULL;