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 *);
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)
88 return got_error_from_errno("dup");
90 *f = fdopen(*fd, "w+");
93 return got_error(GOT_ERR_PRIVSEP_NO_FD);
99 static const struct got_error *
100 got_gotweb_dupfd(int *priv_fd, int *fd)
110 const struct got_error *
111 got_get_repo_owner(char **owner, struct request *c, char *dir)
113 struct server *srv = c->srv;
114 struct transport *t = c->t;
115 struct got_repository *repo = t->repo;
116 const char *gitconfig_owner;
120 if (srv->show_repo_owner == 0)
123 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
124 if (gitconfig_owner) {
125 *owner = strdup(gitconfig_owner);
127 return got_error_from_errno("strdup");
131 return got_error_from_errno("strdup");
136 const struct got_error *
137 got_get_repo_age(char **repo_age, struct request *c, char *dir,
138 const char *refname, int ref_tm)
140 const struct got_error *error = NULL;
141 struct server *srv = c->srv;
142 struct transport *t = c->t;
143 struct got_repository *repo = t->repo;
144 struct got_commit_object *commit = NULL;
145 struct got_reflist_head refs;
146 struct got_reflist_entry *re;
147 time_t committer_time = 0, cmp_time = 0;
152 if (srv->show_repo_age == 0)
155 error = got_ref_list(&refs, repo, "refs/heads",
156 got_ref_cmp_by_name, NULL);
161 * Find the youngest branch tip in the repository, or the age of
162 * the a specific branch tip if a name was provided by the caller.
164 TAILQ_FOREACH(re, &refs, entry) {
165 struct got_object_id *id = NULL;
167 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
170 error = got_ref_resolve(&id, repo, re->ref);
174 error = got_object_open_as_commit(&commit, repo, id);
180 got_object_commit_get_committer_time(commit);
181 got_object_commit_close(commit);
182 if (cmp_time < committer_time)
183 cmp_time = committer_time;
190 committer_time = cmp_time;
191 error = gotweb_get_time_str(repo_age, committer_time, ref_tm);
194 got_ref_list_free(&refs);
198 static const struct got_error *
199 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
200 struct got_commit_object *commit, struct got_reflist_head *refs,
201 struct got_object_id *id)
203 const struct got_error *error = NULL;
204 struct got_reflist_entry *re;
205 struct got_object_id *id2 = NULL;
206 struct got_object_qid *parent_id;
207 struct transport *t = c->t;
208 struct querystring *qs = c->t->qs;
209 char *commit_msg = NULL, *commit_msg0;
211 TAILQ_FOREACH(re, refs, entry) {
214 struct got_tag_object *tag = NULL;
215 struct got_object_id *ref_id;
218 if (got_ref_is_symbolic(re->ref))
221 name = got_ref_get_name(re->ref);
222 if (strncmp(name, "refs/", 5) == 0)
224 if (strncmp(name, "got/", 4) == 0)
226 if (strncmp(name, "heads/", 6) == 0)
228 if (strncmp(name, "remotes/", 8) == 0) {
230 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
233 error = got_ref_resolve(&ref_id, t->repo, re->ref);
236 if (strncmp(name, "tags/", 5) == 0) {
237 error = got_object_open_as_tag(&tag, t->repo, ref_id);
239 if (error->code != GOT_ERR_OBJ_TYPE) {
244 * Ref points at something other
251 cmp = got_object_id_cmp(tag ?
252 got_object_tag_get_object_id(tag) : ref_id, id);
255 got_object_tag_close(tag);
258 s = repo_commit->refs_str;
259 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
260 s ? ", " : "", name) == -1) {
261 error = got_error_from_errno("asprintf");
263 repo_commit->refs_str = NULL;
269 error = got_object_id_str(&repo_commit->commit_id, id);
273 error = got_object_id_str(&repo_commit->tree_id,
274 got_object_commit_get_tree_id(commit));
278 if (qs->action == DIFF) {
279 parent_id = STAILQ_FIRST(
280 got_object_commit_get_parent_ids(commit));
281 if (parent_id != NULL) {
282 id2 = got_object_id_dup(&parent_id->id);
283 error = got_object_id_str(&repo_commit->parent_id, id2);
288 repo_commit->parent_id = strdup("/dev/null");
289 if (repo_commit->parent_id == NULL) {
290 error = got_error_from_errno("strdup");
296 repo_commit->committer_time =
297 got_object_commit_get_committer_time(commit);
299 repo_commit->author =
300 strdup(got_object_commit_get_author(commit));
301 if (repo_commit->author == NULL) {
302 error = got_error_from_errno("strdup");
305 repo_commit->committer =
306 strdup(got_object_commit_get_committer(commit));
307 if (repo_commit->committer == NULL) {
308 error = got_error_from_errno("strdup");
311 error = got_object_commit_get_logmsg(&commit_msg0, commit);
315 commit_msg = commit_msg0;
316 while (*commit_msg == '\n')
319 repo_commit->commit_msg = strdup(commit_msg);
320 if (repo_commit->commit_msg == NULL)
321 error = got_error_from_errno("strdup");
326 const struct got_error *
327 got_get_repo_commits(struct request *c, int limit)
329 const struct got_error *error = NULL;
330 struct got_object_id *id = NULL;
331 struct got_commit_graph *graph = NULL;
332 struct got_commit_object *commit = NULL;
333 struct got_reflist_head refs;
334 struct got_reference *ref;
335 struct repo_commit *repo_commit = NULL;
336 struct server *srv = c->srv;
337 struct transport *t = c->t;
338 struct got_repository *repo = t->repo;
339 struct querystring *qs = t->qs;
340 struct repo_dir *repo_dir = t->repo_dir;
341 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
342 int chk_next = 0, chk_multi = 0, commit_found = 0;
343 int obj_type, limit_chk = 0;
347 if (qs->file != NULL && strlen(qs->file) > 0)
348 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
350 return got_error_from_errno("asprintf");
352 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
353 repo_dir->name) == -1)
354 return got_error_from_errno("asprintf");
356 error = got_init_repo_commit(&repo_commit);
361 * XXX: jumping directly to a commit id via
362 * got_repo_match_object_id_prefix significantly improves performance,
363 * but does not allow us to create a PREVIOUS button, since commits can
364 * only be itereated forward. So, we have to match as we iterate from
367 if (qs->action == BRIEFS || qs->action == COMMITS ||
368 (qs->action == TREE && qs->commit == NULL)) {
369 error = got_ref_open(&ref, repo, qs->headref, 0);
373 error = got_ref_resolve(&id, repo, ref);
377 } else if (qs->commit != NULL) {
378 error = got_ref_open(&ref, repo, qs->commit, 0);
380 error = got_ref_resolve(&id, repo, ref);
383 error = got_object_get_type(&obj_type, repo, id);
387 if (obj_type == GOT_OBJ_TYPE_TAG) {
388 struct got_tag_object *tag;
389 error = got_object_open_as_tag(&tag, repo, id);
392 if (got_object_tag_get_object_type(tag) !=
393 GOT_OBJ_TYPE_COMMIT) {
394 got_object_tag_close(tag);
395 error = got_error(GOT_ERR_OBJ_TYPE);
399 id = got_object_id_dup(
400 got_object_tag_get_object_id(tag));
402 error = got_error_from_errno(
403 "got_object_id_dup");
404 got_object_tag_close(tag);
407 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
408 error = got_error(GOT_ERR_OBJ_TYPE);
412 error = got_repo_match_object_id_prefix(&id, qs->commit,
413 GOT_OBJ_TYPE_COMMIT, repo);
418 error = got_repo_map_path(&in_repo_path, repo, repo_path);
422 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
426 if (qs->file != NULL && strlen(qs->file) > 0) {
427 error = got_commit_graph_open(&graph, file_path, 0);
431 error = got_commit_graph_open(&graph, in_repo_path, 0);
436 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
441 if (limit_chk == ((limit * qs->page) - (limit - 1)) &&
442 commit_found == 0 && repo_commit->commit_id != NULL) {
443 t->prev_id = strdup(repo_commit->commit_id);
444 if (t->prev_id == NULL) {
445 error = got_error_from_errno("strdup");
450 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
453 if (error->code == GOT_ERR_ITER_COMPLETED)
460 error = got_object_open_as_commit(&commit, repo, id);
464 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
469 error = got_get_repo_commit(c, repo_commit, commit,
474 if (qs->commit != NULL && commit_found == 0 && limit != 1) {
475 if (strcmp(qs->commit, repo_commit->commit_id) == 0)
477 else if (qs->file != NULL && strlen(qs->file) > 0 &&
488 struct repo_commit *new_repo_commit = NULL;
489 error = got_init_repo_commit(&new_repo_commit);
493 TAILQ_INSERT_TAIL(&t->repo_commits, new_repo_commit, entry);
495 error = got_get_repo_commit(c, new_repo_commit, commit,
503 if (limit == 1 && chk_multi == 0 &&
504 srv->max_commits_display != 1)
510 * check for one more commit before breaking,
511 * so we know whether to navigate through briefs
512 * commits and summary
514 if (chk_next && (qs->action == BRIEFS ||
515 qs->action == COMMITS || qs->action == SUMMARY)) {
516 t->next_id = strdup(new_repo_commit->commit_id);
517 if (t->next_id == NULL) {
518 error = got_error_from_errno("strdup");
522 got_object_commit_close(commit);
525 TAILQ_REMOVE(&t->repo_commits, new_repo_commit,
527 gotweb_free_repo_commit(new_repo_commit);
531 got_ref_list_free(&refs);
532 if (error || (limit && --limit == 0)) {
533 if (commit_found || (qs->file != NULL &&
534 strlen(qs->file) > 0))
540 got_object_commit_close(commit);
545 gotweb_free_repo_commit(repo_commit);
547 got_object_commit_close(commit);
549 got_commit_graph_close(graph);
550 got_ref_list_free(&refs);
557 const struct got_error *
558 got_get_repo_tags(struct request *c, int limit)
560 const struct got_error *error = NULL;
561 struct got_object_id *id = NULL;
562 struct got_commit_object *commit = NULL;
563 struct got_reflist_head refs;
564 struct got_reference *ref;
565 struct got_reflist_entry *re;
566 struct server *srv = c->srv;
567 struct transport *t = c->t;
568 struct got_repository *repo = t->repo;
569 struct querystring *qs = t->qs;
570 struct repo_dir *repo_dir = t->repo_dir;
571 struct got_tag_object *tag = NULL;
572 struct repo_tag *rt = NULL, *trt = NULL;
573 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
574 char *commit_msg = NULL, *commit_msg0 = NULL;
575 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
579 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
580 repo_dir->name) == -1)
581 return got_error_from_errno("asprintf");
583 if (qs->commit == NULL && qs->action == TAGS) {
584 error = got_ref_open(&ref, repo, qs->headref, 0);
587 error = got_ref_resolve(&id, repo, ref);
591 } else if (qs->commit == NULL && qs->action == TAG) {
592 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
595 error = got_repo_match_object_id_prefix(&id, qs->commit,
596 GOT_OBJ_TYPE_COMMIT, repo);
601 if (qs->action != SUMMARY && qs->action != TAGS) {
602 error = got_object_open_as_commit(&commit, repo, id);
605 error = got_object_commit_get_logmsg(&commit_msg0, commit);
609 got_object_commit_close(commit);
614 error = got_repo_map_path(&in_repo_path, repo, repo_path);
618 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
627 * XXX: again, see previous message about caching
630 TAILQ_FOREACH(re, &refs, entry) {
631 struct repo_tag *new_repo_tag = NULL;
632 error = got_init_repo_tag(&new_repo_tag);
636 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
638 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
639 if (new_repo_tag->tag_name == NULL) {
640 error = got_error_from_errno("strdup");
644 error = got_ref_resolve(&id, repo, re->ref);
648 error = got_object_open_as_tag(&tag, repo, id);
650 if (error->code != GOT_ERR_OBJ_TYPE) {
655 /* "lightweight" tag */
656 error = got_object_open_as_commit(&commit, repo, id);
662 new_repo_tag->tagger =
663 strdup(got_object_commit_get_committer(commit));
664 if (new_repo_tag->tagger == NULL) {
665 error = got_error_from_errno("strdup");
668 new_repo_tag->tagger_time =
669 got_object_commit_get_committer_time(commit);
670 error = got_object_id_str(&id_str, id);
678 new_repo_tag->tagger =
679 strdup(got_object_tag_get_tagger(tag));
680 if (new_repo_tag->tagger == NULL) {
681 error = got_error_from_errno("strdup");
684 new_repo_tag->tagger_time =
685 got_object_tag_get_tagger_time(tag);
686 error = got_object_id_str(&id_str,
687 got_object_tag_get_object_id(tag));
692 new_repo_tag->commit_id = strdup(id_str);
693 if (new_repo_tag->commit_id == NULL)
696 if (commit_found == 0 && qs->commit != NULL &&
697 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
705 * check for one more commit before breaking,
706 * so we know whether to navigate through briefs
707 * commits and summary
710 t->next_id = strdup(new_repo_tag->commit_id);
711 if (t->next_id == NULL) {
712 error = got_error_from_errno("strdup");
716 got_object_commit_close(commit);
719 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
720 gotweb_free_repo_tag(new_repo_tag);
725 error = got_object_commit_get_logmsg(&new_repo_tag->
729 got_object_commit_close(commit);
732 new_repo_tag->tag_commit =
733 strdup(got_object_tag_get_message(tag));
734 if (new_repo_tag->tag_commit == NULL) {
735 error = got_error_from_errno("strdup");
740 while (*new_repo_tag->tag_commit == '\n')
741 new_repo_tag->tag_commit++;
743 if (qs->action != SUMMARY && qs->action != TAGS) {
744 commit_msg = commit_msg0;
745 while (*commit_msg == '\n')
748 new_repo_tag->commit_msg = strdup(commit_msg);
749 if (new_repo_tag->commit_msg == NULL) {
750 error = got_error_from_errno("strdup");
757 if (limit && --limit == 0) {
768 * we have tailq populated, so find previous commit id
769 * for navigation through briefs and commits
771 if (t->tag_count == 0) {
772 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
773 TAILQ_REMOVE(&t->repo_tags, rt, entry);
774 gotweb_free_repo_tag(rt);
777 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
779 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
781 if (commit_found == 0 && rt->commit_id != NULL &&
782 strcmp(qs->commit, rt->commit_id) != 0) {
786 if (c_cnt == srv->max_commits_display ||
787 rt == TAILQ_FIRST(&t->repo_tags)) {
788 t->prev_id = strdup(rt->commit_id);
789 if (t->prev_id == NULL)
790 error = got_error_from_errno("strdup");
798 got_object_commit_close(commit);
799 got_ref_list_free(&refs);
805 const struct got_error *
806 got_output_repo_tree(struct request *c)
808 const struct got_error *error = NULL;
809 struct transport *t = c->t;
810 struct got_commit_object *commit = NULL;
811 struct got_repository *repo = t->repo;
812 struct querystring *qs = t->qs;
813 struct repo_commit *rc = NULL;
814 struct got_object_id *tree_id = NULL, *commit_id = NULL;
815 struct got_reflist_head refs;
816 struct got_tree_object *tree = NULL;
817 struct repo_dir *repo_dir = t->repo_dir;
818 const char *name, *index_page_str, *folder;
819 char *id_str = NULL, *escaped_name = NULL;
820 char *path = NULL, *in_repo_path = NULL, *modestr = NULL;
825 rc = TAILQ_FIRST(&t->repo_commits);
827 if (qs->folder != NULL) {
828 path = strdup(qs->folder);
830 error = got_error_from_errno("strdup");
834 error = got_repo_map_path(&in_repo_path, repo, repo_dir->path);
841 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
842 GOT_OBJ_TYPE_COMMIT, &refs, repo);
846 error = got_object_open_as_commit(&commit, repo, commit_id);
850 error = got_object_id_by_path(&tree_id, repo, commit, path);
854 error = got_object_open_as_tree(&tree, repo, tree_id);
858 nentries = got_object_tree_get_nentries(tree);
860 index_page_str = qs->index_page_str ? qs->index_page_str : "";
861 folder = qs->folder ? qs->folder : "";
863 for (i = 0; i < nentries; i++) {
864 struct got_tree_entry *te;
867 te = got_object_tree_get_entry(tree, i);
869 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
873 modestr = strdup("");
874 if (modestr == NULL) {
875 error = got_error_from_errno("strdup");
878 mode = got_tree_entry_get_mode(te);
879 if (got_object_tree_entry_is_submodule(te)) {
881 modestr = strdup("$");
882 if (modestr == NULL) {
883 error = got_error_from_errno("strdup");
886 } else if (S_ISLNK(mode)) {
888 modestr = strdup("@");
889 if (modestr == NULL) {
890 error = got_error_from_errno("strdup");
893 } else if (S_ISDIR(mode)) {
895 modestr = strdup("/");
896 if (modestr == NULL) {
897 error = got_error_from_errno("strdup");
900 } else if (mode & S_IXUSR) {
902 modestr = strdup("*");
903 if (modestr == NULL) {
904 error = got_error_from_errno("strdup");
909 name = got_tree_entry_get_name(te);
910 error = gotweb_escape_html(&escaped_name, name);
916 "<div class='tree_wrapper'>\n"
917 "<div class='tree_line'>"
918 "<a class='diff_directory' "
919 "href='?index_page=%s&path=%s&action=tree"
920 "&commit=%s&folder=%s/%s'>%s%s</a>"
921 "</div>\n" /* .tree_line */
922 "<div class='tree_line_blank'> </div>\n"
923 "</div>\n", /* .tree_wrapper */
924 index_page_str, qs->path, rc->commit_id,
925 folder, name, escaped_name, modestr);
930 "<div class='tree_wrapper'>\n"
931 "<div class='tree_line'>"
932 "<a href='?index_page=%s&path=%s&action=blob"
933 "&commit=%s&folder=%s&file=%s'>%s%s</a>"
934 "</div>\n" /* .tree_line */
935 "<div class='tree_line_blank'>"
936 "<a href='?index_page=%s&path=%s&action=commits"
937 "&commit=%s&folder=%s&file=%s'>commits</a>\n"
939 "<a href='?index_page=%s&path=%s&action=blame"
940 "&commit=%s&folder=%s&file=%s'>blame</a>\n"
941 "</div>\n" /* .tree_line_blank */
942 "</div>\n", /* .tree_wrapper */
943 index_page_str, qs->path, rc->commit_id,
944 folder, name, escaped_name, modestr,
945 index_page_str, qs->path, rc->commit_id,
947 index_page_str, qs->path, rc->commit_id,
964 got_ref_list_free(&refs);
966 got_object_commit_close(commit);
972 const struct got_error *
973 got_output_file_blob(struct request *c)
975 const struct got_error *error = NULL;
976 struct transport *t = c->t;
977 struct got_repository *repo = t->repo;
978 struct querystring *qs = c->t->qs;
979 struct got_commit_object *commit = NULL;
980 struct got_object_id *commit_id = NULL;
981 struct got_reflist_head refs;
982 struct got_blob_object *blob = NULL;
983 char *path = NULL, *in_repo_path = NULL;
984 int obj_type, set_mime = 0, fd = -1;
990 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
991 qs->folder ? "/" : "", qs->file) == -1) {
992 error = got_error_from_errno("asprintf");
996 error = got_repo_map_path(&in_repo_path, repo, path);
1000 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1001 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1005 error = got_object_open_as_commit(&commit, repo, commit_id);
1009 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
1013 if (commit_id == NULL) {
1014 error = got_error(GOT_ERR_NO_OBJ);
1018 error = got_object_get_type(&obj_type, repo, commit_id);
1022 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1023 error = got_error(GOT_ERR_OBJ_TYPE);
1027 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
1031 error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
1034 hdrlen = got_object_blob_get_hdrlen(blob);
1036 error = got_object_blob_read_block(&len, blob);
1039 buf = got_object_blob_get_read_buf(blob);
1042 * Skip blob object header first time around,
1043 * which also contains a zero byte.
1046 if (set_mime == 0) {
1047 if (isbinary(buf, len - hdrlen)) {
1048 error = gotweb_render_content_type_file(c,
1049 "application/octet-stream",
1052 log_warnx("%s: %s", __func__,
1057 error = gotweb_render_content_type(c,
1060 log_warnx("%s: %s", __func__,
1067 fcgi_gen_binary_response(c, buf, len - hdrlen);
1073 got_object_commit_close(commit);
1074 if (fd != -1 && close(fd) == -1 && error == NULL)
1075 error = got_error_from_errno("close");
1077 got_object_blob_close(blob);
1088 char datebuf[11]; /* YYYY-MM-DD + NUL */
1091 struct blame_cb_args {
1092 struct blame_line *lines;
1096 off_t *line_offsets;
1098 struct got_repository *repo;
1102 static const struct got_error *
1103 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1104 struct got_commit_object *commit, struct got_object_id *id)
1106 const struct got_error *err = NULL;
1107 struct blame_cb_args *a = arg;
1108 struct blame_line *bline;
1109 struct request *c = a->c;
1110 struct transport *t = c->t;
1111 struct querystring *qs = t->qs;
1112 struct repo_dir *repo_dir = t->repo_dir;
1113 const char *index_page_str;
1114 char *line = NULL, *eline = NULL;
1115 size_t linesize = 0;
1118 time_t committer_time;
1121 if (nlines != a->nlines ||
1122 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1123 return got_error(GOT_ERR_RANGE);
1126 return NULL; /* no change in this commit */
1128 /* Annotate this line. */
1129 bline = &a->lines[lineno - 1];
1130 if (bline->annotated)
1132 err = got_object_id_str(&bline->id_str, id);
1136 bline->committer = strdup(got_object_commit_get_committer(commit));
1137 if (bline->committer == NULL) {
1138 err = got_error_from_errno("strdup");
1142 committer_time = got_object_commit_get_committer_time(commit);
1143 if (gmtime_r(&committer_time, &tm) == NULL)
1144 return got_error_from_errno("gmtime_r");
1145 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1147 err = got_error(GOT_ERR_NO_SPACE);
1150 bline->annotated = 1;
1152 /* Print lines annotated so far. */
1153 bline = &a->lines[a->lineno_cur - 1];
1154 if (!bline->annotated)
1157 offset = a->line_offsets[a->lineno_cur - 1];
1158 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1159 err = got_error_from_errno("fseeko");
1163 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1165 while (a->lineno_cur <= a->nlines && bline->annotated) {
1166 char *smallerthan, *at, *nl, *committer;
1169 if (getline(&line, &linesize, a->f) == -1) {
1171 err = got_error_from_errno("getline");
1175 committer = bline->committer;
1176 smallerthan = strchr(committer, '<');
1177 if (smallerthan && smallerthan[1] != '\0')
1178 committer = smallerthan + 1;
1179 at = strchr(committer, '@');
1182 len = strlen(committer);
1184 committer[8] = '\0';
1186 nl = strchr(line, '\n');
1190 err = gotweb_escape_html(&eline, line);
1194 r = fcgi_printf(c, "<div class='blame_wrapper'>"
1195 "<div class='blame_number'>%.*d</div>"
1196 "<div class='blame_hash'>"
1197 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1200 "<div class='blame_date'>%s</div>"
1201 "<div class='blame_author'>%s</div>"
1202 "<div class='blame_code'>%s</div>"
1203 "</div>", /* .blame_wrapper */
1204 a->nlines_prec, a->lineno_cur,
1205 index_page_str, repo_dir->name, bline->id_str,
1214 bline = &a->lines[a->lineno_cur - 1];
1222 const struct got_error *
1223 got_output_file_blame(struct request *c)
1225 const struct got_error *error = NULL;
1226 struct transport *t = c->t;
1227 struct got_repository *repo = t->repo;
1228 struct querystring *qs = c->t->qs;
1229 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1230 struct got_commit_object *commit = NULL;
1231 struct got_reflist_head refs;
1232 struct got_blob_object *blob = NULL;
1233 char *path = NULL, *in_repo_path = NULL;
1234 struct blame_cb_args bca;
1235 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1238 FILE *f1 = NULL, *f2 = NULL;
1244 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1245 qs->folder ? "/" : "", qs->file) == -1) {
1246 error = got_error_from_errno("asprintf");
1250 error = got_repo_map_path(&in_repo_path, repo, path);
1254 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1255 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1259 error = got_object_open_as_commit(&commit, repo, commit_id);
1263 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1267 if (commit_id == NULL) {
1268 error = got_error(GOT_ERR_NO_OBJ);
1272 error = got_object_get_type(&obj_type, repo, obj_id);
1276 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1277 error = got_error(GOT_ERR_OBJ_TYPE);
1281 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1285 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1289 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1293 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1294 &bca.line_offsets, bca.f, blob);
1295 if (error || bca.nlines == 0)
1298 /* Don't include \n at EOF in the blame line count. */
1299 if (bca.line_offsets[bca.nlines - 1] == filesize)
1302 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1303 if (bca.lines == NULL) {
1304 error = got_error_from_errno("calloc");
1308 bca.nlines_prec = 0;
1317 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1321 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1325 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1329 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1333 error = got_blame(in_repo_path, commit_id, repo,
1334 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1338 free(bca.line_offsets);
1339 for (i = 0; i < bca.nlines; i++) {
1340 struct blame_line *bline = &bca.lines[i];
1341 free(bline->id_str);
1342 free(bline->committer);
1347 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1348 error = got_error_from_errno("close");
1349 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1350 error = got_error_from_errno("close");
1351 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1352 error = got_error_from_errno("close");
1354 const struct got_error *bca_err =
1355 got_gotweb_flushfile(bca.f, fd1);
1360 const struct got_error *f1_err =
1361 got_gotweb_flushfile(f1, fd5);
1366 const struct got_error *f2_err =
1367 got_gotweb_flushfile(f2, fd6);
1372 got_object_commit_close(commit);
1374 got_object_blob_close(blob);
1381 const struct got_error *
1382 got_output_repo_diff(struct request *c)
1384 const struct got_error *error = NULL;
1385 struct transport *t = c->t;
1386 struct got_repository *repo = t->repo;
1387 struct repo_commit *rc = NULL;
1388 struct got_object_id *id1 = NULL, *id2 = NULL;
1389 struct got_reflist_head refs;
1390 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1391 char *label1 = NULL, *label2 = NULL, *line = NULL;
1392 char *newline, *eline = NULL, *color = NULL;
1393 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1394 size_t linesize = 0;
1400 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1404 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1408 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1412 rc = TAILQ_FIRST(&t->repo_commits);
1414 if (rc->parent_id != NULL &&
1415 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1416 error = got_repo_match_object_id(&id1, &label1,
1417 rc->parent_id, GOT_OBJ_TYPE_ANY,
1423 error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1424 GOT_OBJ_TYPE_ANY, &refs, repo);
1428 error = got_object_get_type(&obj_type, repo, id2);
1432 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1436 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1441 case GOT_OBJ_TYPE_BLOB:
1442 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1443 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1446 case GOT_OBJ_TYPE_TREE:
1447 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1448 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1451 case GOT_OBJ_TYPE_COMMIT:
1452 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1453 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1457 error = got_error(GOT_ERR_OBJ_TYPE);
1462 if (fseek(f1, 0, SEEK_SET) == -1) {
1463 error = got_ferror(f1, GOT_ERR_IO);
1467 if (fseek(f2, 0, SEEK_SET) == -1) {
1468 error = got_ferror(f2, GOT_ERR_IO);
1472 if (fseek(f3, 0, SEEK_SET) == -1) {
1473 error = got_ferror(f3, GOT_ERR_IO);
1477 while ((linelen = getline(&line, &linesize, f3)) != -1) {
1478 if (strncmp(line, "-", 1) == 0) {
1479 color = strdup("diff_minus");
1480 if (color == NULL) {
1481 error = got_error_from_errno("strdup");
1484 } else if (strncmp(line, "+", 1) == 0) {
1485 color = strdup("diff_plus");
1486 if (color == NULL) {
1487 error = got_error_from_errno("strdup");
1490 } else if (strncmp(line, "@@", 2) == 0) {
1491 color = strdup("diff_chunk_header");
1492 if (color == NULL) {
1493 error = got_error_from_errno("strdup");
1496 } else if (strncmp(line, "@@", 2) == 0) {
1497 color = strdup("diff_chunk_header");
1498 if (color == NULL) {
1499 error = got_error_from_errno("strdup");
1502 } else if (strncmp(line, "commit +", 8) == 0) {
1503 color = strdup("diff_meta");
1504 if (color == NULL) {
1505 error = got_error_from_errno("strdup");
1508 } else if (strncmp(line, "commit -", 8) == 0) {
1509 color = strdup("diff_meta");
1510 if (color == NULL) {
1511 error = got_error_from_errno("strdup");
1514 } else if (strncmp(line, "blob +", 6) == 0) {
1515 color = strdup("diff_meta");
1516 if (color == NULL) {
1517 error = got_error_from_errno("strdup");
1520 } else if (strncmp(line, "blob -", 6) == 0) {
1521 color = strdup("diff_meta");
1522 if (color == NULL) {
1523 error = got_error_from_errno("strdup");
1526 } else if (strncmp(line, "file +", 6) == 0) {
1527 color = strdup("diff_meta");
1528 if (color == NULL) {
1529 error = got_error_from_errno("strdup");
1532 } else if (strncmp(line, "file -", 6) == 0) {
1533 color = strdup("diff_meta");
1534 if (color == NULL) {
1535 error = got_error_from_errno("strdup");
1538 } else if (strncmp(line, "from:", 5) == 0) {
1539 color = strdup("diff_author");
1540 if (color == NULL) {
1541 error = got_error_from_errno("strdup");
1544 } else if (strncmp(line, "via:", 4) == 0) {
1545 color = strdup("diff_author");
1546 if (color == NULL) {
1547 error = got_error_from_errno("strdup");
1550 } else if (strncmp(line, "date:", 5) == 0) {
1551 color = strdup("diff_date");
1552 if (color == NULL) {
1553 error = got_error_from_errno("strdup");
1558 newline = strchr(line, '\n');
1562 error = gotweb_escape_html(&eline, line);
1566 fcgi_printf(c, "<div class='diff_line %s'>%s</div>\n",
1567 color ? color : "", eline);
1572 wrlen = wrlen + linelen;
1576 if (linelen == -1 && ferror(f3))
1577 error = got_error_from_errno("getline");
1580 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1581 error = got_error_from_errno("close");
1582 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1583 error = got_error_from_errno("close");
1585 const struct got_error *f1_err =
1586 got_gotweb_flushfile(f1, fd1);
1591 const struct got_error *f2_err =
1592 got_gotweb_flushfile(f2, fd2);
1597 const struct got_error *f3_err =
1598 got_gotweb_flushfile(f3, fd3);
1602 got_ref_list_free(&refs);
1612 static const struct got_error *
1613 got_init_repo_commit(struct repo_commit **rc)
1615 *rc = calloc(1, sizeof(**rc));
1617 return got_error_from_errno2("%s: calloc", __func__);
1620 (*rc)->refs_str = NULL;
1621 (*rc)->commit_id = NULL;
1622 (*rc)->committer = NULL;
1623 (*rc)->author = NULL;
1624 (*rc)->parent_id = NULL;
1625 (*rc)->tree_id = NULL;
1626 (*rc)->commit_msg = NULL;
1631 static const struct got_error *
1632 got_init_repo_tag(struct repo_tag **rt)
1634 *rt = calloc(1, sizeof(**rt));
1636 return got_error_from_errno2("%s: calloc", __func__);
1638 (*rt)->commit_id = NULL;
1639 (*rt)->tag_name = NULL;
1640 (*rt)->tag_commit = NULL;
1641 (*rt)->commit_msg = NULL;
1642 (*rt)->tagger = NULL;