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)
60 for (i = 0; i < n; i++)
67 static const struct got_error *
68 got_gotweb_flushfile(FILE *f, int fd)
70 if (fseek(f, 0, SEEK_SET) == -1)
71 return got_error_from_errno("fseek");
73 if (ftruncate(fd, 0) == -1)
74 return got_error_from_errno("ftruncate");
77 return got_error_from_errno("fsync");
79 if (f && fclose(f) == EOF)
80 return got_error_from_errno("fclose");
82 if (fd != -1 && close(fd) != -1)
83 return got_error_from_errno("close");
88 static const struct got_error *
89 got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
91 const struct got_error *error = NULL;
98 *f = fdopen(*fd, "w+");
101 error = got_error(GOT_ERR_PRIVSEP_NO_FD);
107 static const struct got_error *
108 got_gotweb_dupfd(int *priv_fd, int *fd)
110 const struct got_error *error = NULL;
120 const struct got_error *
121 got_get_repo_owner(char **owner, struct request *c, char *dir)
123 const struct got_error *error = NULL;
124 struct server *srv = c->srv;
125 struct transport *t = c->t;
126 struct got_repository *repo = t->repo;
127 const char *gitconfig_owner;
131 if (srv->show_repo_owner == 0)
134 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
135 if (gitconfig_owner) {
136 *owner = strdup(gitconfig_owner);
138 return got_error_from_errno("strdup");
143 const struct got_error *
144 got_get_repo_age(char **repo_age, struct request *c, char *dir,
145 const char *refname, int ref_tm)
147 const struct got_error *error = NULL;
148 struct server *srv = c->srv;
149 struct transport *t = c->t;
150 struct got_repository *repo = t->repo;
151 struct got_commit_object *commit = NULL;
152 struct got_reflist_head refs;
153 struct got_reflist_entry *re;
154 time_t committer_time = 0, cmp_time = 0;
159 if (srv->show_repo_age == 0)
162 error = got_ref_list(&refs, repo, "refs/heads",
163 got_ref_cmp_by_name, NULL);
168 * Find the youngest branch tip in the repository, or the age of
169 * the a specific branch tip if a name was provided by the caller.
171 TAILQ_FOREACH(re, &refs, entry) {
172 struct got_object_id *id = NULL;
174 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
177 error = got_ref_resolve(&id, repo, re->ref);
181 error = got_object_open_as_commit(&commit, repo, id);
187 got_object_commit_get_committer_time(commit);
188 got_object_commit_close(commit);
189 if (cmp_time < committer_time)
190 cmp_time = committer_time;
197 committer_time = cmp_time;
198 error = gotweb_get_time_str(repo_age, committer_time, ref_tm);
201 got_ref_list_free(&refs);
205 static const struct got_error *
206 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
207 struct got_commit_object *commit, struct got_reflist_head *refs,
208 struct got_object_id *id)
210 const struct got_error *error = NULL;
211 struct got_reflist_entry *re;
212 struct got_object_id *id2 = NULL;
213 struct got_object_qid *parent_id;
214 struct transport *t = c->t;
215 struct querystring *qs = c->t->qs;
216 char *commit_msg = NULL, *commit_msg0;
218 TAILQ_FOREACH(re, refs, entry) {
221 struct got_tag_object *tag = NULL;
222 struct got_object_id *ref_id;
225 if (got_ref_is_symbolic(re->ref))
228 name = got_ref_get_name(re->ref);
229 if (strncmp(name, "refs/", 5) == 0)
231 if (strncmp(name, "got/", 4) == 0)
233 if (strncmp(name, "heads/", 6) == 0)
235 if (strncmp(name, "remotes/", 8) == 0) {
237 s = strstr(name, "/" GOT_REF_HEAD);
238 if (s != NULL && s[strlen(s)] == '\0')
241 error = got_ref_resolve(&ref_id, t->repo, re->ref);
244 if (strncmp(name, "tags/", 5) == 0) {
245 error = got_object_open_as_tag(&tag, t->repo, ref_id);
247 if (error->code != GOT_ERR_OBJ_TYPE) {
252 * Ref points at something other
259 cmp = got_object_id_cmp(tag ?
260 got_object_tag_get_object_id(tag) : ref_id, id);
263 got_object_tag_close(tag);
266 s = repo_commit->refs_str;
267 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
268 s ? ", " : "", name) == -1) {
269 error = got_error_from_errno("asprintf");
271 repo_commit->refs_str = NULL;
277 error = got_object_id_str(&repo_commit->commit_id, id);
281 error = got_object_id_str(&repo_commit->tree_id,
282 got_object_commit_get_tree_id(commit));
286 if (qs->action == DIFF) {
287 parent_id = STAILQ_FIRST(
288 got_object_commit_get_parent_ids(commit));
289 if (parent_id != NULL) {
290 id2 = got_object_id_dup(&parent_id->id);
291 error = got_object_id_str(&repo_commit->parent_id, id2);
296 repo_commit->parent_id = strdup("/dev/null");
297 if (repo_commit->parent_id == NULL) {
298 error = got_error_from_errno("strdup");
304 repo_commit->committer_time =
305 got_object_commit_get_committer_time(commit);
307 repo_commit->author =
308 strdup(got_object_commit_get_author(commit));
309 if (repo_commit->author == NULL) {
310 error = got_error_from_errno("strdup");
313 repo_commit->committer =
314 strdup(got_object_commit_get_committer(commit));
315 if (repo_commit->committer == NULL) {
316 error = got_error_from_errno("strdup");
319 error = got_object_commit_get_logmsg(&commit_msg0, commit);
323 commit_msg = commit_msg0;
324 while (*commit_msg == '\n')
327 repo_commit->commit_msg = strdup(commit_msg);
328 if (repo_commit->commit_msg == NULL)
329 error = got_error_from_errno("strdup");
334 const struct got_error *
335 got_get_repo_commits(struct request *c, int limit)
337 const struct got_error *error = NULL;
338 struct got_object_id *id = NULL;
339 struct got_commit_graph *graph = NULL;
340 struct got_commit_object *commit = NULL;
341 struct got_reflist_head refs;
342 struct got_reference *ref;
343 struct repo_commit *repo_commit = NULL;
344 struct server *srv = c->srv;
345 struct transport *t = c->t;
346 struct got_repository *repo = t->repo;
347 struct querystring *qs = t->qs;
348 struct repo_dir *repo_dir = t->repo_dir;
349 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
350 int chk_next = 0, chk_multi = 0, commit_found = 0;
351 int obj_type, limit_chk = 0;
355 if (qs->file != NULL && strlen(qs->file) > 0)
356 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
358 return got_error_from_errno("asprintf");
360 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
361 repo_dir->name) == -1)
362 return got_error_from_errno("asprintf");
364 error = got_init_repo_commit(&repo_commit);
369 * XXX: jumping directly to a commit id via
370 * got_repo_match_object_id_prefix significantly improves performance,
371 * but does not allow us to create a PREVIOUS button, since commits can
372 * only be itereated forward. So, we have to match as we iterate from
375 if (qs->action == BRIEFS || qs->action == COMMITS ||
376 (qs->action == TREE && qs->commit == NULL)) {
377 error = got_ref_open(&ref, repo, qs->headref, 0);
381 error = got_ref_resolve(&id, repo, ref);
385 } else if (qs->commit != NULL) {
386 error = got_ref_open(&ref, repo, qs->commit, 0);
388 error = got_ref_resolve(&id, repo, ref);
391 error = got_object_get_type(&obj_type, repo, id);
395 if (obj_type == GOT_OBJ_TYPE_TAG) {
396 struct got_tag_object *tag;
397 error = got_object_open_as_tag(&tag, repo, id);
400 if (got_object_tag_get_object_type(tag) !=
401 GOT_OBJ_TYPE_COMMIT) {
402 got_object_tag_close(tag);
403 error = got_error(GOT_ERR_OBJ_TYPE);
407 id = got_object_id_dup(
408 got_object_tag_get_object_id(tag));
410 error = got_error_from_errno(
411 "got_object_id_dup");
412 got_object_tag_close(tag);
415 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
416 error = got_error(GOT_ERR_OBJ_TYPE);
420 error = got_repo_match_object_id_prefix(&id, qs->commit,
421 GOT_OBJ_TYPE_COMMIT, repo);
426 error = got_repo_map_path(&in_repo_path, repo, repo_path);
430 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
434 if (qs->file != NULL && strlen(qs->file) > 0) {
435 error = got_commit_graph_open(&graph, file_path, 0);
439 error = got_commit_graph_open(&graph, in_repo_path, 0);
444 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
449 if (limit_chk == ((limit * qs->page) - (limit - 1)) &&
450 commit_found == 0 && repo_commit->commit_id != NULL) {
451 t->prev_id = strdup(repo_commit->commit_id);
452 if (t->prev_id == NULL) {
453 error = got_error_from_errno("strdup");
458 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
461 if (error->code == GOT_ERR_ITER_COMPLETED)
468 error = got_object_open_as_commit(&commit, repo, id);
472 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
477 error = got_get_repo_commit(c, repo_commit, commit,
482 if (qs->commit != NULL && commit_found == 0 && limit != 1) {
483 if (strcmp(qs->commit, repo_commit->commit_id) == 0)
485 else if (qs->file != NULL && strlen(qs->file) > 0 &&
496 struct repo_commit *new_repo_commit = NULL;
497 error = got_init_repo_commit(&new_repo_commit);
501 TAILQ_INSERT_TAIL(&t->repo_commits, new_repo_commit, entry);
503 error = got_get_repo_commit(c, new_repo_commit, commit,
511 if (limit == 1 && chk_multi == 0 &&
512 srv->max_commits_display != 1)
518 * check for one more commit before breaking,
519 * so we know whether to navigate through briefs
520 * commits and summary
522 if (chk_next && (qs->action == BRIEFS ||
523 qs->action == COMMITS || qs->action == SUMMARY)) {
524 t->next_id = strdup(new_repo_commit->commit_id);
525 if (t->next_id == NULL) {
526 error = got_error_from_errno("strdup");
530 got_object_commit_close(commit);
533 if (t->next_id == NULL) {
534 error = got_error_from_errno("strdup");
537 TAILQ_REMOVE(&t->repo_commits, new_repo_commit,
539 gotweb_free_repo_commit(new_repo_commit);
543 got_ref_list_free(&refs);
544 if (error || (limit && --limit == 0)) {
545 if (commit_found || (qs->file != NULL &&
546 strlen(qs->file) > 0))
552 got_object_commit_close(commit);
557 gotweb_free_repo_commit(repo_commit);
559 got_object_commit_close(commit);
561 got_commit_graph_close(graph);
562 got_ref_list_free(&refs);
569 const struct got_error *
570 got_get_repo_tags(struct request *c, int limit)
572 const struct got_error *error = NULL;
573 struct got_object_id *id = NULL;
574 struct got_commit_object *commit = NULL;
575 struct got_reflist_head refs;
576 struct got_reference *ref;
577 struct got_reflist_entry *re;
578 struct server *srv = c->srv;
579 struct transport *t = c->t;
580 struct got_repository *repo = t->repo;
581 struct querystring *qs = t->qs;
582 struct repo_dir *repo_dir = t->repo_dir;
583 struct got_tag_object *tag = NULL;
584 struct repo_tag *rt = NULL, *trt = NULL;
585 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
586 char *commit_msg = NULL, *commit_msg0 = NULL;
587 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
591 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
592 repo_dir->name) == -1)
593 return got_error_from_errno("asprintf");
598 if (qs->commit == NULL && qs->action == TAGS) {
599 error = got_ref_open(&ref, repo, qs->headref, 0);
602 error = got_ref_resolve(&id, repo, ref);
606 } else if (qs->commit == NULL && qs->action == TAG) {
607 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
610 error = got_repo_match_object_id_prefix(&id, qs->commit,
611 GOT_OBJ_TYPE_COMMIT, repo);
616 if (qs->action != SUMMARY && qs->action != TAGS) {
617 error = got_object_open_as_commit(&commit, repo, id);
620 error = got_object_commit_get_logmsg(&commit_msg0, commit);
624 got_object_commit_close(commit);
629 error = got_repo_map_path(&in_repo_path, repo, repo_path);
633 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
642 * XXX: again, see previous message about caching
645 TAILQ_FOREACH(re, &refs, entry) {
646 struct repo_tag *new_repo_tag = NULL;
647 error = got_init_repo_tag(&new_repo_tag);
651 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
653 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
654 if (new_repo_tag->tag_name == NULL) {
655 error = got_error_from_errno("strdup");
659 error = got_ref_resolve(&id, repo, re->ref);
663 error = got_object_open_as_tag(&tag, repo, id);
665 if (error->code != GOT_ERR_OBJ_TYPE) {
670 /* "lightweight" tag */
671 error = got_object_open_as_commit(&commit, repo, id);
677 new_repo_tag->tagger =
678 strdup(got_object_commit_get_committer(commit));
679 if (new_repo_tag->tagger == NULL) {
680 error = got_error_from_errno("strdup");
683 new_repo_tag->tagger_time =
684 got_object_commit_get_committer_time(commit);
685 error = got_object_id_str(&id_str, id);
693 new_repo_tag->tagger =
694 strdup(got_object_tag_get_tagger(tag));
695 if (new_repo_tag->tagger == NULL) {
696 error = got_error_from_errno("strdup");
699 new_repo_tag->tagger_time =
700 got_object_tag_get_tagger_time(tag);
701 error = got_object_id_str(&id_str,
702 got_object_tag_get_object_id(tag));
707 new_repo_tag->commit_id = strdup(id_str);
708 if (new_repo_tag->commit_id == NULL)
711 if (commit_found == 0 && qs->commit != NULL &&
712 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
720 * check for one more commit before breaking,
721 * so we know whether to navigate through briefs
722 * commits and summary
725 t->next_id = strdup(new_repo_tag->commit_id);
726 if (t->next_id == NULL) {
727 error = got_error_from_errno("strdup");
731 got_object_commit_close(commit);
734 if (t->next_id == NULL) {
735 error = got_error_from_errno("strdup");
738 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
739 gotweb_free_repo_tag(new_repo_tag);
744 error = got_object_commit_get_logmsg(&new_repo_tag->
748 got_object_commit_close(commit);
751 new_repo_tag->tag_commit =
752 strdup(got_object_tag_get_message(tag));
753 if (new_repo_tag->tag_commit == NULL) {
754 error = got_error_from_errno("strdup");
759 while (*new_repo_tag->tag_commit == '\n')
760 new_repo_tag->tag_commit++;
762 if (qs->action != SUMMARY && qs->action != TAGS) {
763 commit_msg = commit_msg0;
764 while (*commit_msg == '\n')
767 new_repo_tag->commit_msg = strdup(commit_msg);
768 if (new_repo_tag->commit_msg == NULL) {
769 error = got_error_from_errno("strdup");
776 if (limit && --limit == 0) {
787 * we have tailq populated, so find previous commit id
788 * for navigation through briefs and commits
790 if (t->tag_count == 0) {
791 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
792 TAILQ_REMOVE(&t->repo_tags, rt, entry);
793 gotweb_free_repo_tag(rt);
796 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
798 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
800 if (commit_found == 0 && rt->commit_id != NULL &&
801 strcmp(qs->commit, rt->commit_id) != 0) {
805 if (c_cnt == srv->max_commits_display ||
806 rt == TAILQ_FIRST(&t->repo_tags)) {
807 t->prev_id = strdup(rt->commit_id);
808 if (t->prev_id == NULL)
809 error = got_error_from_errno("strdup");
817 got_object_commit_close(commit);
818 got_ref_list_free(&refs);
824 const struct got_error *
825 got_output_repo_tree(struct request *c)
827 const struct got_error *error = NULL;
828 struct transport *t = c->t;
829 struct got_commit_object *commit = NULL;
830 struct got_repository *repo = t->repo;
831 struct querystring *qs = t->qs;
832 struct repo_commit *rc = NULL;
833 struct got_object_id *tree_id = NULL, *commit_id = NULL;
834 struct got_reflist_head refs;
835 struct got_tree_object *tree = NULL;
836 struct repo_dir *repo_dir = t->repo_dir;
838 char *path = NULL, *in_repo_path = NULL, *build_folder = NULL;
839 char *modestr = NULL, *name = NULL, *class = NULL;
840 int nentries, i, class_flip = 0;
844 rc = TAILQ_FIRST(&t->repo_commits);
846 if (qs->folder != NULL) {
847 path = strdup(qs->folder);
849 error = got_error_from_errno("strdup");
853 error = got_repo_map_path(&in_repo_path, repo, repo_dir->path);
860 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
861 GOT_OBJ_TYPE_COMMIT, &refs, repo);
865 error = got_object_open_as_commit(&commit, repo, commit_id);
869 error = got_object_id_by_path(&tree_id, repo, commit, path);
873 error = got_object_open_as_tree(&tree, repo, tree_id);
877 nentries = got_object_tree_get_nentries(tree);
879 for (i = 0; i < nentries; i++) {
880 struct got_tree_entry *te;
883 te = got_object_tree_get_entry(tree, i);
885 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
889 modestr = strdup("");
890 if (modestr == NULL) {
891 error = got_error_from_errno("strdup");
894 mode = got_tree_entry_get_mode(te);
895 if (got_object_tree_entry_is_submodule(te)) {
897 modestr = strdup("$");
898 if (modestr == NULL) {
899 error = got_error_from_errno("strdup");
902 } else if (S_ISLNK(mode)) {
904 modestr = strdup("@");
905 if (modestr == NULL) {
906 error = got_error_from_errno("strdup");
909 } else if (S_ISDIR(mode)) {
911 modestr = strdup("/");
912 if (modestr == NULL) {
913 error = got_error_from_errno("strdup");
916 } else if (mode & S_IXUSR) {
918 modestr = strdup("*");
919 if (modestr == NULL) {
920 error = got_error_from_errno("strdup");
925 if (class_flip == 0) {
926 class = strdup("back_lightgray");
928 error = got_error_from_errno("strdup");
933 class = strdup("back_white");
935 error = got_error_from_errno("strdup");
941 name = strdup(got_tree_entry_get_name(te));
943 error = got_error_from_errno("strdup");
947 if (asprintf(&build_folder, "%s/%s",
948 qs->folder ? qs->folder : "",
949 got_tree_entry_get_name(te)) == -1) {
950 error = got_error_from_errno("asprintf");
954 if (fcgi_gen_response(c,
955 "<div id='tree_wrapper'>\n") == -1)
958 if (fcgi_gen_response(c, "<div id='tree_line' "
961 if (fcgi_gen_response(c, class) == -1)
963 if (fcgi_gen_response(c, "'>") == -1)
966 if (fcgi_gen_response(c, "<a class='diff_directory' "
967 "href='?index_page=") == -1)
969 if (fcgi_gen_response(c, qs->index_page_str) == -1)
971 if (fcgi_gen_response(c, "&path=") == -1)
973 if (fcgi_gen_response(c, qs->path) == -1)
975 if (fcgi_gen_response(c, "&action=tree") == -1)
977 if (fcgi_gen_response(c, "&commit=") == -1)
979 if (fcgi_gen_response(c, rc->commit_id) == -1)
981 if (fcgi_gen_response(c, "&folder=") == -1)
983 if (fcgi_gen_response(c, build_folder) == -1)
985 if (fcgi_gen_response(c, "'>") == -1)
987 if (fcgi_gen_response(c, name) == -1)
989 if (fcgi_gen_response(c, modestr) == -1)
991 if (fcgi_gen_response(c, "</a>") == -1)
994 if (fcgi_gen_response(c, "</div>\n") == -1)
997 if (fcgi_gen_response(c, "<div id='tree_line_blank' "
1000 if (fcgi_gen_response(c, class) == -1)
1002 if (fcgi_gen_response(c, "'>") == -1)
1004 if (fcgi_gen_response(c, " ") == -1)
1006 if (fcgi_gen_response(c, "</div>\n") == -1)
1009 if (fcgi_gen_response(c, "</div>\n") == -1)
1014 name = strdup(got_tree_entry_get_name(te));
1016 error = got_error_from_errno("strdup");
1020 if (fcgi_gen_response(c,
1021 "<div id='tree_wrapper'>\n") == -1)
1023 if (fcgi_gen_response(c, "<div id='tree_line' "
1026 if (fcgi_gen_response(c, class) == -1)
1028 if (fcgi_gen_response(c, "'>") == -1)
1031 if (fcgi_gen_response(c,
1032 "<a href='?index_page=") == -1)
1035 if (fcgi_gen_response(c, qs->index_page_str) == -1)
1038 if (fcgi_gen_response(c, "&path=") == -1)
1040 if (fcgi_gen_response(c, qs->path) == -1)
1043 if (fcgi_gen_response(c, "&action=blob") == -1)
1046 if (fcgi_gen_response(c, "&commit=") == -1)
1048 if (fcgi_gen_response(c, rc->commit_id) == -1)
1051 if (fcgi_gen_response(c, "&folder=") == -1)
1053 if (fcgi_gen_response(c, qs->folder) == -1)
1056 if (fcgi_gen_response(c, "&file=") == -1)
1058 if (fcgi_gen_response(c, name) == -1)
1061 if (fcgi_gen_response(c, "'>") == -1)
1063 if (fcgi_gen_response(c, name) == -1)
1065 if (fcgi_gen_response(c, modestr) == -1)
1068 if (fcgi_gen_response(c, "</a>") == -1)
1071 if (fcgi_gen_response(c, "</div>\n") == -1)
1074 if (fcgi_gen_response(c, "<div id='tree_line_blank' "
1077 if (fcgi_gen_response(c, class) == -1)
1079 if (fcgi_gen_response(c, "'>") == -1)
1082 if (fcgi_gen_response(c,
1083 "<a href='?index_page=") == -1)
1086 if (fcgi_gen_response(c, qs->index_page_str) == -1)
1089 if (fcgi_gen_response(c, "&path=") == -1)
1091 if (fcgi_gen_response(c, qs->path) == -1)
1094 if (fcgi_gen_response(c, "&action=commits") == -1)
1097 if (fcgi_gen_response(c, "&commit=") == -1)
1099 if (fcgi_gen_response(c, rc->commit_id) == -1)
1102 if (fcgi_gen_response(c, "&folder=") == -1)
1104 if (fcgi_gen_response(c, qs->folder) == -1)
1107 if (fcgi_gen_response(c, "&file=") == -1)
1109 if (fcgi_gen_response(c, name) == -1)
1112 if (fcgi_gen_response(c, "'>") == -1)
1115 if (fcgi_gen_response(c, "commits") == -1)
1117 if (fcgi_gen_response(c, "</a>\n") == -1)
1120 if (fcgi_gen_response(c, " | \n") == -1)
1123 if (fcgi_gen_response(c,
1124 "<a href='?index_page=") == -1)
1127 if (fcgi_gen_response(c, qs->index_page_str) == -1)
1130 if (fcgi_gen_response(c, "&path=") == -1)
1132 if (fcgi_gen_response(c, qs->path) == -1)
1135 if (fcgi_gen_response(c, "&action=blame") == -1)
1138 if (fcgi_gen_response(c, "&commit=") == -1)
1140 if (fcgi_gen_response(c, rc->commit_id) == -1)
1143 if (fcgi_gen_response(c, "&folder=") == -1)
1145 if (fcgi_gen_response(c, qs->folder) == -1)
1148 if (fcgi_gen_response(c, "&file=") == -1)
1150 if (fcgi_gen_response(c, name) == -1)
1153 if (fcgi_gen_response(c, "'>") == -1)
1156 if (fcgi_gen_response(c, "blame") == -1)
1158 if (fcgi_gen_response(c, "</a>\n") == -1)
1161 if (fcgi_gen_response(c, "</div>\n") == -1)
1163 if (fcgi_gen_response(c, "</div>\n") == -1)
1169 build_folder = NULL;
1184 got_ref_list_free(&refs);
1186 got_object_commit_close(commit);
1192 const struct got_error *
1193 got_output_file_blob(struct request *c)
1195 const struct got_error *error = NULL;
1196 struct transport *t = c->t;
1197 struct got_repository *repo = t->repo;
1198 struct querystring *qs = c->t->qs;
1199 struct got_commit_object *commit = NULL;
1200 struct got_object_id *commit_id = NULL;
1201 struct got_reflist_head refs;
1202 struct got_blob_object *blob = NULL;
1203 char *path = NULL, *in_repo_path = NULL;
1204 int obj_type, set_mime = 0, type = 0, fd = -1;
1205 char *buf_output = NULL;
1211 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1212 qs->folder ? "/" : "", qs->file) == -1) {
1213 error = got_error_from_errno("asprintf");
1217 error = got_repo_map_path(&in_repo_path, repo, path);
1221 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1222 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1226 error = got_object_open_as_commit(&commit, repo, commit_id);
1230 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
1234 if (commit_id == NULL) {
1235 error = got_error(GOT_ERR_NO_OBJ);
1239 error = got_object_get_type(&obj_type, repo, commit_id);
1243 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1244 error = got_error(GOT_ERR_OBJ_TYPE);
1248 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
1252 error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
1255 hdrlen = got_object_blob_get_hdrlen(blob);
1257 error = got_object_blob_read_block(&len, blob);
1260 buf = got_object_blob_get_read_buf(blob);
1263 * Skip blob object header first time around,
1264 * which also contains a zero byte.
1267 if (set_mime == 0) {
1268 if (isbinary(buf, len - hdrlen)) {
1269 error = gotweb_render_content_type_file(c,
1270 "application/octet-stream",
1273 log_warnx("%s: %s", __func__,
1279 error = gotweb_render_content_type(c,
1282 log_warnx("%s: %s", __func__,
1291 buf_output = calloc(len - hdrlen + 1,
1292 sizeof(*buf_output));
1293 if (buf_output == NULL) {
1294 error = got_error_from_errno("calloc");
1297 memcpy(buf_output, buf, len - hdrlen);
1298 fcgi_gen_response(c, buf_output);
1302 fcgi_gen_binary_response(c, buf, len - hdrlen);
1308 got_object_commit_close(commit);
1309 if (fd != -1 && close(fd) == -1 && error == NULL)
1310 error = got_error_from_errno("close");
1312 got_object_blob_close(blob);
1324 char datebuf[11]; /* YYYY-MM-DD + NUL */
1327 struct blame_cb_args {
1328 struct blame_line *lines;
1332 off_t *line_offsets;
1334 struct got_repository *repo;
1338 static const struct got_error *
1339 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1340 struct got_commit_object *commit, struct got_object_id *id)
1342 const struct got_error *err = NULL;
1343 struct blame_cb_args *a = arg;
1344 struct blame_line *bline;
1345 struct request *c = a->c;
1346 struct transport *t = c->t;
1347 struct querystring *qs = t->qs;
1348 struct repo_dir *repo_dir = t->repo_dir;
1349 char *line = NULL, *eline = NULL;
1350 size_t linesize = 0;
1353 time_t committer_time;
1355 if (nlines != a->nlines ||
1356 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1357 return got_error(GOT_ERR_RANGE);
1360 return NULL; /* no change in this commit */
1362 /* Annotate this line. */
1363 bline = &a->lines[lineno - 1];
1364 if (bline->annotated)
1366 err = got_object_id_str(&bline->id_str, id);
1370 bline->committer = strdup(got_object_commit_get_committer(commit));
1371 if (bline->committer == NULL) {
1372 err = got_error_from_errno("strdup");
1376 committer_time = got_object_commit_get_committer_time(commit);
1377 if (gmtime_r(&committer_time, &tm) == NULL)
1378 return got_error_from_errno("gmtime_r");
1379 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1381 err = got_error(GOT_ERR_NO_SPACE);
1384 bline->annotated = 1;
1386 /* Print lines annotated so far. */
1387 bline = &a->lines[a->lineno_cur - 1];
1388 if (!bline->annotated)
1391 offset = a->line_offsets[a->lineno_cur - 1];
1392 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1393 err = got_error_from_errno("fseeko");
1397 while (bline->annotated) {
1398 int out_buff_size = 100;
1399 char *smallerthan, *at, *nl, *committer;
1400 char out_buff[out_buff_size];
1403 if (getline(&line, &linesize, a->f) == -1) {
1405 err = got_error_from_errno("getline");
1409 committer = bline->committer;
1410 smallerthan = strchr(committer, '<');
1411 if (smallerthan && smallerthan[1] != '\0')
1412 committer = smallerthan + 1;
1413 at = strchr(committer, '@');
1416 len = strlen(committer);
1418 committer[8] = '\0';
1420 nl = strchr(line, '\n');
1424 if (fcgi_gen_response(c, "<div id='blame_wrapper'>") == -1)
1426 if (fcgi_gen_response(c, "<div id='blame_number'>") == -1)
1428 if (snprintf(out_buff, strlen(out_buff), "%.*d", a->nlines_prec,
1431 if (fcgi_gen_response(c, out_buff) == -1)
1433 if (fcgi_gen_response(c, "</div>") == -1)
1436 if (fcgi_gen_response(c, "<div id='blame_hash'>") == -1)
1439 if (fcgi_gen_response(c, "<a href='?index_page=") == -1)
1441 if (fcgi_gen_response(c, qs->index_page_str) == -1)
1443 if (fcgi_gen_response(c, "&path=") == -1)
1445 if (fcgi_gen_response(c, repo_dir->name) == -1)
1447 if (fcgi_gen_response(c, "&action=diff&commit=") == -1)
1449 if (fcgi_gen_response(c, bline->id_str) == -1)
1451 if (fcgi_gen_response(c, "'>") == -1)
1453 if (snprintf(out_buff, 10, "%.8s", bline->id_str) < 0)
1455 if (fcgi_gen_response(c, out_buff) == -1)
1457 if (fcgi_gen_response(c, "</a></div>") == -1)
1460 if (fcgi_gen_response(c, "<div id='blame_date'>") == -1)
1462 if (fcgi_gen_response(c, bline->datebuf) == -1)
1464 if (fcgi_gen_response(c, "</div>") == -1)
1467 if (fcgi_gen_response(c, "<div id='blame_author'>") == -1)
1469 if (fcgi_gen_response(c, committer) == -1)
1471 if (fcgi_gen_response(c, "</div>") == -1)
1474 if (fcgi_gen_response(c, "<div id='blame_code'>") == -1)
1476 err = gotweb_escape_html(&eline, line);
1479 if (fcgi_gen_response(c, eline) == -1)
1481 if (fcgi_gen_response(c, "</div>") == -1)
1484 if (fcgi_gen_response(c, "</div>") == -1)
1487 bline = &a->lines[a->lineno_cur - 1];
1495 const struct got_error *
1496 got_output_file_blame(struct request *c)
1498 const struct got_error *error = NULL;
1499 struct transport *t = c->t;
1500 struct got_repository *repo = t->repo;
1501 struct querystring *qs = c->t->qs;
1502 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1503 struct got_commit_object *commit = NULL;
1504 struct got_reflist_head refs;
1505 struct got_blob_object *blob = NULL;
1506 char *path = NULL, *in_repo_path = NULL;
1507 struct blame_cb_args bca;
1508 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1511 FILE *f1 = NULL, *f2 = NULL;
1517 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1518 qs->folder ? "/" : "", qs->file) == -1) {
1519 error = got_error_from_errno("asprintf");
1523 error = got_repo_map_path(&in_repo_path, repo, path);
1527 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1528 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1532 error = got_object_open_as_commit(&commit, repo, commit_id);
1536 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1540 if (commit_id == NULL) {
1541 error = got_error(GOT_ERR_NO_OBJ);
1545 error = got_object_get_type(&obj_type, repo, obj_id);
1549 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1550 error = got_error(GOT_ERR_OBJ_TYPE);
1554 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1558 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1562 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1566 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1567 &bca.line_offsets, bca.f, blob);
1568 if (error || bca.nlines == 0)
1571 /* Don't include \n at EOF in the blame line count. */
1572 if (bca.line_offsets[bca.nlines - 1] == filesize)
1575 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1576 if (bca.lines == NULL) {
1577 error = got_error_from_errno("calloc");
1581 bca.nlines_prec = 0;
1590 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1594 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1598 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1602 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1606 error = got_blame(in_repo_path, commit_id, repo,
1607 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1611 free(bca.line_offsets);
1612 for (i = 0; i < bca.nlines; i++) {
1613 struct blame_line *bline = &bca.lines[i];
1614 free(bline->id_str);
1615 free(bline->committer);
1620 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1621 error = got_error_from_errno("close");
1622 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1623 error = got_error_from_errno("close");
1624 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1625 error = got_error_from_errno("close");
1627 const struct got_error *bca_err =
1628 got_gotweb_flushfile(bca.f, fd1);
1633 const struct got_error *f1_err =
1634 got_gotweb_flushfile(f1, fd5);
1639 const struct got_error *f2_err =
1640 got_gotweb_flushfile(f2, fd6);
1645 got_object_commit_close(commit);
1647 got_object_blob_close(blob);
1654 const struct got_error *
1655 got_output_repo_diff(struct request *c)
1657 const struct got_error *error = NULL;
1658 struct transport *t = c->t;
1659 struct got_repository *repo = t->repo;
1660 struct repo_commit *rc = NULL;
1661 struct got_object_id *id1 = NULL, *id2 = NULL;
1662 struct got_reflist_head refs;
1663 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1664 char *label1 = NULL, *label2 = NULL, *line = NULL;
1665 char *newline, *eline = NULL, *color = NULL;
1666 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1667 size_t linesize = 0;
1673 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1677 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1681 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1685 rc = TAILQ_FIRST(&t->repo_commits);
1687 if (rc->parent_id != NULL &&
1688 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1689 error = got_repo_match_object_id(&id1, &label1,
1690 rc->parent_id, GOT_OBJ_TYPE_ANY,
1696 error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1697 GOT_OBJ_TYPE_ANY, &refs, repo);
1701 error = got_object_get_type(&obj_type, repo, id2);
1705 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1709 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1714 case GOT_OBJ_TYPE_BLOB:
1715 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1716 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1719 case GOT_OBJ_TYPE_TREE:
1720 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1721 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1724 case GOT_OBJ_TYPE_COMMIT:
1725 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1726 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1730 error = got_error(GOT_ERR_OBJ_TYPE);
1735 if (fseek(f1, 0, SEEK_SET) == -1) {
1736 error = got_ferror(f1, GOT_ERR_IO);
1740 if (fseek(f2, 0, SEEK_SET) == -1) {
1741 error = got_ferror(f2, GOT_ERR_IO);
1745 if (fseek(f3, 0, SEEK_SET) == -1) {
1746 error = got_ferror(f3, GOT_ERR_IO);
1750 while ((linelen = getline(&line, &linesize, f3)) != -1) {
1751 if (strncmp(line, "-", 1) == 0) {
1752 color = strdup("diff_minus");
1753 if (color == NULL) {
1754 error = got_error_from_errno("strdup");
1757 } else if (strncmp(line, "+", 1) == 0) {
1758 color = strdup("diff_plus");
1759 if (color == NULL) {
1760 error = got_error_from_errno("strdup");
1763 } else if (strncmp(line, "@@", 2) == 0) {
1764 color = strdup("diff_chunk_header");
1765 if (color == NULL) {
1766 error = got_error_from_errno("strdup");
1769 } else if (strncmp(line, "@@", 2) == 0) {
1770 color = strdup("diff_chunk_header");
1771 if (color == NULL) {
1772 error = got_error_from_errno("strdup");
1775 } else if (strncmp(line, "commit +", 8) == 0) {
1776 color = strdup("diff_meta");
1777 if (color == NULL) {
1778 error = got_error_from_errno("strdup");
1781 } else if (strncmp(line, "commit -", 8) == 0) {
1782 color = strdup("diff_meta");
1783 if (color == NULL) {
1784 error = got_error_from_errno("strdup");
1787 } else if (strncmp(line, "blob +", 6) == 0) {
1788 color = strdup("diff_meta");
1789 if (color == NULL) {
1790 error = got_error_from_errno("strdup");
1793 } else if (strncmp(line, "blob -", 6) == 0) {
1794 color = strdup("diff_meta");
1795 if (color == NULL) {
1796 error = got_error_from_errno("strdup");
1799 } else if (strncmp(line, "file +", 6) == 0) {
1800 color = strdup("diff_meta");
1801 if (color == NULL) {
1802 error = got_error_from_errno("strdup");
1805 } else if (strncmp(line, "file -", 6) == 0) {
1806 color = strdup("diff_meta");
1807 if (color == NULL) {
1808 error = got_error_from_errno("strdup");
1811 } else if (strncmp(line, "from:", 5) == 0) {
1812 color = strdup("diff_author");
1813 if (color == NULL) {
1814 error = got_error_from_errno("strdup");
1817 } else if (strncmp(line, "via:", 4) == 0) {
1818 color = strdup("diff_author");
1819 if (color == NULL) {
1820 error = got_error_from_errno("strdup");
1823 } else if (strncmp(line, "date:", 5) == 0) {
1824 color = strdup("diff_date");
1825 if (color == NULL) {
1826 error = got_error_from_errno("strdup");
1830 if (fcgi_gen_response(c, "<div id='diff_line' class='") == -1)
1832 if (fcgi_gen_response(c, color ? color : "") == -1)
1834 if (fcgi_gen_response(c, "'>") == -1)
1836 newline = strchr(line, '\n');
1840 error = gotweb_escape_html(&eline, line);
1843 if (fcgi_gen_response(c, eline) == -1)
1848 if (fcgi_gen_response(c, "</div>\n") == -1)
1851 wrlen = wrlen + linelen;
1855 if (linelen == -1 && ferror(f3))
1856 error = got_error_from_errno("getline");
1859 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1860 error = got_error_from_errno("close");
1861 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1862 error = got_error_from_errno("close");
1864 const struct got_error *f1_err =
1865 got_gotweb_flushfile(f1, fd1);
1870 const struct got_error *f2_err =
1871 got_gotweb_flushfile(f2, fd2);
1876 const struct got_error *f3_err =
1877 got_gotweb_flushfile(f3, fd3);
1881 got_ref_list_free(&refs);
1891 static const struct got_error *
1892 got_init_repo_commit(struct repo_commit **rc)
1894 const struct got_error *error = NULL;
1896 *rc = calloc(1, sizeof(**rc));
1898 return got_error_from_errno2("%s: calloc", __func__);
1901 (*rc)->refs_str = NULL;
1902 (*rc)->commit_id = NULL;
1903 (*rc)->committer = NULL;
1904 (*rc)->author = NULL;
1905 (*rc)->parent_id = NULL;
1906 (*rc)->tree_id = NULL;
1907 (*rc)->commit_msg = NULL;
1912 static const struct got_error *
1913 got_init_repo_tag(struct repo_tag **rt)
1915 const struct got_error *error = NULL;
1917 *rt = calloc(1, sizeof(**rt));
1919 return got_error_from_errno2("%s: calloc", __func__);
1921 (*rt)->commit_id = NULL;
1922 (*rt)->tag_name = NULL;
1923 (*rt)->tag_commit = NULL;
1924 (*rt)->commit_msg = NULL;
1925 (*rt)->tagger = NULL;