Blob


1 /*
2 * Copyright (c) 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
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.
8 *
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.
16 */
18 #include <sys/queue.h>
19 #include <sys/socket.h>
20 #include <sys/stat.h>
22 #include <event.h>
23 #include <imsg.h>
24 #include <sha1.h>
25 #include <sha2.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_reference.h"
34 #include "got_repository.h"
35 #include "got_path.h"
36 #include "got_cancel.h"
37 #include "got_diff.h"
38 #include "got_commit_graph.h"
39 #include "got_blame.h"
40 #include "got_privsep.h"
42 #include "proc.h"
43 #include "gotwebd.h"
45 static const struct got_error *got_init_repo_commit(struct repo_commit **);
46 static const struct got_error *got_init_repo_tag(struct repo_tag **);
47 static const struct got_error *got_get_repo_commit(struct request *,
48 struct repo_commit *, struct got_commit_object *, struct got_reflist_head *,
49 struct got_object_id *);
50 static const struct got_error *got_gotweb_dupfd(int *, int *);
51 static const struct got_error *got_gotweb_openfile(FILE **, int *);
52 static const struct got_error *got_gotweb_blame_cb(void *, int, int,
53 struct got_commit_object *,struct got_object_id *);
55 const struct got_error *
56 got_gotweb_closefile(FILE *f)
57 {
58 const struct got_error *err = NULL;
60 if (fseek(f, 0, SEEK_SET) == -1)
61 err = got_error_from_errno("fseek");
63 if (err == NULL && ftruncate(fileno(f), 0) == -1)
64 err = got_error_from_errno("ftruncate");
66 if (fclose(f) == EOF && err == NULL)
67 err = got_error_from_errno("fclose");
69 return err;
70 }
72 static const struct got_error *
73 got_gotweb_openfile(FILE **f, int *priv_fd)
74 {
75 int fd;
77 fd = dup(*priv_fd);
78 if (fd == -1)
79 return got_error_from_errno("dup");
81 *f = fdopen(fd, "w+");
82 if (*f == NULL) {
83 close(fd);
84 return got_error(GOT_ERR_PRIVSEP_NO_FD);
85 }
87 return NULL;
88 }
90 static const struct got_error *
91 got_gotweb_dupfd(int *priv_fd, int *fd)
92 {
93 *fd = dup(*priv_fd);
95 if (*fd == -1)
96 return got_error_from_errno("dup");
98 return NULL;
99 }
101 const struct got_error *
102 got_get_repo_owner(char **owner, struct request *c)
104 struct server *srv = c->srv;
105 struct transport *t = c->t;
106 struct got_repository *repo = t->repo;
107 const char *gitconfig_owner;
109 *owner = NULL;
111 if (srv->show_repo_owner == 0)
112 return NULL;
114 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
115 if (gitconfig_owner) {
116 *owner = strdup(gitconfig_owner);
117 if (*owner == NULL)
118 return got_error_from_errno("strdup");
119 } else {
120 *owner = strdup("");
121 if (*owner == NULL)
122 return got_error_from_errno("strdup");
124 return NULL;
127 const struct got_error *
128 got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
130 const struct got_error *error = NULL;
131 struct server *srv = c->srv;
132 struct transport *t = c->t;
133 struct got_repository *repo = t->repo;
134 struct got_commit_object *commit = NULL;
135 struct got_reflist_head refs;
136 struct got_reflist_entry *re;
137 time_t committer_time = 0, cmp_time = 0;
139 TAILQ_INIT(&refs);
141 if (srv->show_repo_age == 0)
142 return NULL;
144 error = got_ref_list(&refs, repo, "refs/heads",
145 got_ref_cmp_by_name, NULL);
146 if (error)
147 goto done;
149 /*
150 * Find the youngest branch tip in the repository, or the age of
151 * the a specific branch tip if a name was provided by the caller.
152 */
153 TAILQ_FOREACH(re, &refs, entry) {
154 struct got_object_id *id = NULL;
156 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
157 continue;
159 error = got_ref_resolve(&id, repo, re->ref);
160 if (error)
161 goto done;
163 error = got_object_open_as_commit(&commit, repo, id);
164 free(id);
165 if (error)
166 goto done;
168 committer_time =
169 got_object_commit_get_committer_time(commit);
170 got_object_commit_close(commit);
171 if (cmp_time < committer_time)
172 cmp_time = committer_time;
174 if (refname)
175 break;
178 if (cmp_time != 0)
179 *repo_age = cmp_time;
180 done:
181 got_ref_list_free(&refs);
182 return error;
185 static const struct got_error *
186 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
187 struct got_commit_object *commit, struct got_reflist_head *refs,
188 struct got_object_id *id)
190 const struct got_error *error = NULL;
191 struct got_reflist_entry *re;
192 struct got_object_id *id2 = NULL;
193 struct got_object_qid *parent_id;
194 struct transport *t = c->t;
195 struct querystring *qs = c->t->qs;
196 char *commit_msg = NULL, *commit_msg0;
198 TAILQ_FOREACH(re, refs, entry) {
199 char *s;
200 const char *name;
201 struct got_tag_object *tag = NULL;
202 struct got_object_id *ref_id;
203 int cmp;
205 if (got_ref_is_symbolic(re->ref))
206 continue;
208 name = got_ref_get_name(re->ref);
209 if (strncmp(name, "refs/", 5) == 0)
210 name += 5;
211 if (strncmp(name, "got/", 4) == 0)
212 continue;
213 if (strncmp(name, "heads/", 6) == 0)
214 name += 6;
215 if (strncmp(name, "remotes/", 8) == 0) {
216 name += 8;
217 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
218 continue;
220 error = got_ref_resolve(&ref_id, t->repo, re->ref);
221 if (error)
222 return error;
223 if (strncmp(name, "tags/", 5) == 0) {
224 error = got_object_open_as_tag(&tag, t->repo, ref_id);
225 if (error) {
226 if (error->code != GOT_ERR_OBJ_TYPE) {
227 free(ref_id);
228 continue;
230 /*
231 * Ref points at something other
232 * than a tag.
233 */
234 error = NULL;
235 tag = NULL;
238 cmp = got_object_id_cmp(tag ?
239 got_object_tag_get_object_id(tag) : ref_id, id);
240 free(ref_id);
241 if (tag)
242 got_object_tag_close(tag);
243 if (cmp != 0)
244 continue;
245 s = repo_commit->refs_str;
246 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
247 s ? ", " : "", name) == -1) {
248 error = got_error_from_errno("asprintf");
249 free(s);
250 repo_commit->refs_str = NULL;
251 return error;
253 free(s);
256 error = got_object_id_str(&repo_commit->commit_id, id);
257 if (error)
258 return error;
260 error = got_object_id_str(&repo_commit->tree_id,
261 got_object_commit_get_tree_id(commit));
262 if (error)
263 return error;
265 if (qs->action == DIFF) {
266 parent_id = STAILQ_FIRST(
267 got_object_commit_get_parent_ids(commit));
268 if (parent_id != NULL) {
269 id2 = got_object_id_dup(&parent_id->id);
270 error = got_object_id_str(&repo_commit->parent_id, id2);
271 if (error)
272 return error;
273 free(id2);
274 } else {
275 repo_commit->parent_id = strdup("/dev/null");
276 if (repo_commit->parent_id == NULL) {
277 error = got_error_from_errno("strdup");
278 return error;
283 repo_commit->committer_time =
284 got_object_commit_get_committer_time(commit);
286 repo_commit->author =
287 strdup(got_object_commit_get_author(commit));
288 if (repo_commit->author == NULL) {
289 error = got_error_from_errno("strdup");
290 return error;
292 repo_commit->committer =
293 strdup(got_object_commit_get_committer(commit));
294 if (repo_commit->committer == NULL) {
295 error = got_error_from_errno("strdup");
296 return error;
298 error = got_object_commit_get_logmsg(&commit_msg0, commit);
299 if (error)
300 return error;
302 commit_msg = commit_msg0;
303 while (*commit_msg == '\n')
304 commit_msg++;
306 repo_commit->commit_msg = strdup(commit_msg);
307 if (repo_commit->commit_msg == NULL)
308 error = got_error_from_errno("strdup");
309 free(commit_msg0);
310 return error;
313 const struct got_error *
314 got_get_repo_commits(struct request *c, int limit)
316 const struct got_error *error = NULL;
317 struct got_object_id *id = NULL;
318 struct got_commit_graph *graph = NULL;
319 struct got_commit_object *commit = NULL;
320 struct got_reflist_head refs;
321 struct got_reference *ref = NULL;
322 struct repo_commit *repo_commit = NULL;
323 struct server *srv = c->srv;
324 struct transport *t = c->t;
325 struct got_repository *repo = t->repo;
326 struct querystring *qs = t->qs;
327 struct repo_dir *repo_dir = t->repo_dir;
328 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
329 int chk_next = 0, chk_multi = 0;
331 TAILQ_INIT(&refs);
333 if (qs->file != NULL && strlen(qs->file) > 0)
334 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
335 qs->file) == -1)
336 return got_error_from_errno("asprintf");
338 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
339 repo_dir->name) == -1) {
340 error = got_error_from_errno("asprintf");
341 goto done;
344 if (qs->commit) {
345 error = got_repo_match_object_id_prefix(&id, qs->commit,
346 GOT_OBJ_TYPE_COMMIT, repo);
347 if (error)
348 goto done;
349 } else {
350 error = got_ref_open(&ref, repo, qs->headref, 0);
351 if (error)
352 goto done;
354 error = got_ref_resolve(&id, repo, ref);
355 if (error)
356 goto done;
359 error = got_repo_map_path(&in_repo_path, repo, repo_path);
360 if (error)
361 goto done;
363 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
364 if (error)
365 goto done;
367 if (qs->file != NULL && strlen(qs->file) > 0) {
368 error = got_commit_graph_open(&graph, file_path, 0);
369 if (error)
370 goto done;
371 } else {
372 error = got_commit_graph_open(&graph, in_repo_path, 0);
373 if (error)
374 goto done;
377 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
378 if (error)
379 goto done;
381 for (;;) {
382 struct got_object_id next_id;
384 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
385 NULL);
386 if (error) {
387 if (error->code == GOT_ERR_ITER_COMPLETED)
388 error = NULL;
389 goto done;
392 error = got_object_open_as_commit(&commit, repo, &next_id);
393 if (error)
394 goto done;
396 error = got_init_repo_commit(&repo_commit);
397 if (error)
398 goto done;
400 error = got_get_repo_commit(c, repo_commit, commit,
401 &refs, &next_id);
402 if (error) {
403 gotweb_free_repo_commit(repo_commit);
404 goto done;
407 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
409 if (!chk_multi || limit != 1 ||
410 srv->max_commits_display == 1) {
411 chk_multi = 1;
413 /*
414 * check for one more commit before breaking,
415 * so we know whether to navigate through briefs
416 * commits and summary
417 */
418 if (chk_next && (qs->action == BRIEFS ||
419 qs->action == COMMITS || qs->action == SUMMARY)) {
420 t->more_id = strdup(repo_commit->commit_id);
421 if (t->more_id == NULL) {
422 error = got_error_from_errno("strdup");
423 goto done;
425 got_object_commit_close(commit);
426 commit = NULL;
427 TAILQ_REMOVE(&t->repo_commits, repo_commit,
428 entry);
429 gotweb_free_repo_commit(repo_commit);
430 goto done;
433 if (error || (limit && --limit == 0)) {
434 if (qs->file != NULL && *qs->file != '\0')
435 if (chk_multi == 0)
436 break;
437 chk_next = 1;
439 if (commit) {
440 got_object_commit_close(commit);
441 commit = NULL;
444 done:
445 if (ref)
446 got_ref_close(ref);
447 if (commit)
448 got_object_commit_close(commit);
449 if (graph)
450 got_commit_graph_close(graph);
451 got_ref_list_free(&refs);
452 free(in_repo_path);
453 free(file_path);
454 free(repo_path);
455 free(id);
456 return error;
459 const struct got_error *
460 got_get_repo_tags(struct request *c, int limit)
462 const struct got_error *error = NULL;
463 struct got_object_id *id = NULL;
464 struct got_commit_object *commit = NULL;
465 struct got_reflist_head refs;
466 struct got_reference *ref;
467 struct got_reflist_entry *re;
468 struct server *srv = c->srv;
469 struct transport *t = c->t;
470 struct got_repository *repo = t->repo;
471 struct querystring *qs = t->qs;
472 struct repo_dir *repo_dir = t->repo_dir;
473 struct got_tag_object *tag = NULL;
474 struct repo_tag *rt = NULL, *trt = NULL;
475 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
476 char *tag_commit = NULL, *tag_commit0 = NULL;
477 char *commit_msg = NULL, *commit_msg0 = NULL;
478 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
480 TAILQ_INIT(&refs);
482 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
483 repo_dir->name) == -1)
484 return got_error_from_errno("asprintf");
486 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
487 error = got_ref_open(&ref, repo, qs->headref, 0);
488 if (error)
489 goto err;
490 error = got_ref_resolve(&id, repo, ref);
491 got_ref_close(ref);
492 if (error)
493 goto err;
494 } else if (qs->commit == NULL && qs->action == TAG) {
495 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
496 goto err;
497 } else {
498 error = got_repo_match_object_id_prefix(&id, qs->commit,
499 GOT_OBJ_TYPE_COMMIT, repo);
500 if (error)
501 goto err;
504 if (qs->action != SUMMARY && qs->action != TAGS) {
505 error = got_object_open_as_commit(&commit, repo, id);
506 if (error)
507 goto err;
508 error = got_object_commit_get_logmsg(&commit_msg0, commit);
509 if (error)
510 goto err;
511 if (commit) {
512 got_object_commit_close(commit);
513 commit = NULL;
517 error = got_repo_map_path(&in_repo_path, repo, repo_path);
518 if (error)
519 goto err;
521 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
522 repo);
523 if (error)
524 goto err;
526 if (limit == 1)
527 chk_multi = 0;
529 /*
530 * XXX: again, see previous message about caching
531 */
533 TAILQ_FOREACH(re, &refs, entry) {
534 struct repo_tag *new_repo_tag = NULL;
535 error = got_init_repo_tag(&new_repo_tag);
536 if (error)
537 goto err;
539 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
541 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
542 if (new_repo_tag->tag_name == NULL) {
543 error = got_error_from_errno("strdup");
544 goto err;
547 free(id);
548 id = NULL;
550 free(id_str);
551 id_str = NULL;
553 error = got_ref_resolve(&id, repo, re->ref);
554 if (error)
555 goto done;
557 if (tag)
558 got_object_tag_close(tag);
559 error = got_object_open_as_tag(&tag, repo, id);
560 if (error) {
561 if (error->code != GOT_ERR_OBJ_TYPE)
562 goto done;
563 /* "lightweight" tag */
564 error = got_object_open_as_commit(&commit, repo, id);
565 if (error)
566 goto done;
567 new_repo_tag->tagger =
568 strdup(got_object_commit_get_committer(commit));
569 if (new_repo_tag->tagger == NULL) {
570 error = got_error_from_errno("strdup");
571 goto err;
573 new_repo_tag->tagger_time =
574 got_object_commit_get_committer_time(commit);
575 error = got_object_id_str(&id_str, id);
576 if (error)
577 goto err;
578 } else {
579 new_repo_tag->tagger =
580 strdup(got_object_tag_get_tagger(tag));
581 if (new_repo_tag->tagger == NULL) {
582 error = got_error_from_errno("strdup");
583 goto err;
585 new_repo_tag->tagger_time =
586 got_object_tag_get_tagger_time(tag);
587 error = got_object_id_str(&id_str,
588 got_object_tag_get_object_id(tag));
589 if (error)
590 goto err;
593 new_repo_tag->commit_id = strdup(id_str);
594 if (new_repo_tag->commit_id == NULL)
595 goto err;
597 if (commit_found == 0 && qs->commit != NULL &&
598 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
599 continue;
600 else
601 commit_found = 1;
603 t->tag_count++;
605 /*
606 * check for one more commit before breaking,
607 * so we know whether to navigate through briefs
608 * commits and summary
609 */
610 if (chk_next) {
611 t->next_id = strdup(new_repo_tag->commit_id);
612 if (t->next_id == NULL) {
613 error = got_error_from_errno("strdup");
614 goto err;
616 if (commit) {
617 got_object_commit_close(commit);
618 commit = NULL;
620 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
621 gotweb_free_repo_tag(new_repo_tag);
622 goto done;
625 if (commit) {
626 error = got_object_commit_get_logmsg(&tag_commit0,
627 commit);
628 if (error)
629 goto err;
630 got_object_commit_close(commit);
631 commit = NULL;
632 } else {
633 tag_commit0 = strdup(got_object_tag_get_message(tag));
634 if (tag_commit0 == NULL) {
635 error = got_error_from_errno("strdup");
636 goto err;
640 tag_commit = tag_commit0;
641 while (*tag_commit == '\n')
642 tag_commit++;
643 new_repo_tag->tag_commit = strdup(tag_commit);
644 if (new_repo_tag->tag_commit == NULL) {
645 error = got_error_from_errno("strdup");
646 free(tag_commit0);
647 goto err;
649 free(tag_commit0);
651 if (qs->action != SUMMARY && qs->action != TAGS) {
652 commit_msg = commit_msg0;
653 while (*commit_msg == '\n')
654 commit_msg++;
656 new_repo_tag->commit_msg = strdup(commit_msg);
657 if (new_repo_tag->commit_msg == NULL) {
658 error = got_error_from_errno("strdup");
659 goto err;
663 if (limit && --limit == 0) {
664 if (chk_multi == 0)
665 break;
666 chk_next = 1;
670 done:
671 /*
672 * we have tailq populated, so find previous commit id
673 * for navigation through briefs and commits
674 */
675 if (t->tag_count == 0) {
676 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
677 TAILQ_REMOVE(&t->repo_tags, rt, entry);
678 gotweb_free_repo_tag(rt);
681 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
682 commit_found = 0;
683 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
684 entry) {
685 if (commit_found == 0 && rt->commit_id != NULL &&
686 strcmp(qs->commit, rt->commit_id) != 0) {
687 continue;
688 } else
689 commit_found = 1;
690 if (c_cnt == srv->max_commits_display ||
691 rt == TAILQ_FIRST(&t->repo_tags)) {
692 t->prev_id = strdup(rt->commit_id);
693 if (t->prev_id == NULL)
694 error = got_error_from_errno("strdup");
695 break;
697 c_cnt++;
700 err:
701 if (commit)
702 got_object_commit_close(commit);
703 if (tag)
704 got_object_tag_close(tag);
705 got_ref_list_free(&refs);
706 free(commit_msg0);
707 free(in_repo_path);
708 free(repo_path);
709 free(id);
710 free(id_str);
711 return error;
714 int
715 got_output_repo_tree(struct request *c,
716 int (*cb)(struct template *, struct got_tree_entry *))
718 const struct got_error *error = NULL;
719 struct transport *t = c->t;
720 struct got_commit_object *commit = NULL;
721 struct got_repository *repo = t->repo;
722 struct querystring *qs = t->qs;
723 struct repo_commit *rc = NULL;
724 struct got_object_id *tree_id = NULL, *commit_id = NULL;
725 struct got_reflist_head refs;
726 struct got_tree_object *tree = NULL;
727 struct got_tree_entry *te;
728 struct repo_dir *repo_dir = t->repo_dir;
729 char *escaped_name = NULL, *path = NULL;
730 int nentries, i;
732 TAILQ_INIT(&refs);
734 rc = TAILQ_FIRST(&t->repo_commits);
736 if (qs->folder != NULL) {
737 path = strdup(qs->folder);
738 if (path == NULL) {
739 error = got_error_from_errno("strdup");
740 goto done;
742 } else {
743 error = got_repo_map_path(&path, repo, repo_dir->path);
744 if (error)
745 goto done;
748 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
749 GOT_OBJ_TYPE_COMMIT, &refs, repo);
750 if (error)
751 goto done;
753 error = got_object_open_as_commit(&commit, repo, commit_id);
754 if (error)
755 goto done;
757 error = got_object_id_by_path(&tree_id, repo, commit, path);
758 if (error)
759 goto done;
761 error = got_object_open_as_tree(&tree, repo, tree_id);
762 if (error)
763 goto done;
765 nentries = got_object_tree_get_nentries(tree);
767 for (i = 0; i < nentries; i++) {
768 te = got_object_tree_get_entry(tree, i);
769 if (cb(c->tp, te) == -1) {
770 error = got_error(GOT_ERR_CANCELLED);
771 break;
774 done:
775 free(escaped_name);
776 free(path);
777 got_ref_list_free(&refs);
778 if (commit)
779 got_object_commit_close(commit);
780 if (tree)
781 got_object_tree_close(tree);
782 free(commit_id);
783 free(tree_id);
784 if (error) {
785 if (error->code != GOT_ERR_CANCELLED)
786 log_warnx("%s: %s", __func__, error->msg);
787 return -1;
789 return 0;
792 const struct got_error *
793 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
794 int *binary, struct request *c)
796 const struct got_error *error = NULL;
797 struct transport *t = c->t;
798 struct got_repository *repo = t->repo;
799 struct querystring *qs = c->t->qs;
800 struct got_commit_object *commit = NULL;
801 struct got_object_id *commit_id = NULL;
802 struct got_reflist_head refs;
803 char *path = NULL, *in_repo_path = NULL;
804 int obj_type;
806 TAILQ_INIT(&refs);
808 *blob = NULL;
809 *fd = -1;
810 *binary = 0;
812 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
813 qs->folder ? "/" : "", qs->file) == -1) {
814 error = got_error_from_errno("asprintf");
815 goto done;
818 error = got_repo_map_path(&in_repo_path, repo, path);
819 if (error)
820 goto done;
822 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
823 GOT_OBJ_TYPE_COMMIT, &refs, repo);
824 if (error)
825 goto done;
827 error = got_object_open_as_commit(&commit, repo, commit_id);
828 if (error)
829 goto done;
831 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
832 if (error)
833 goto done;
835 if (commit_id == NULL) {
836 error = got_error(GOT_ERR_NO_OBJ);
837 goto done;
840 error = got_object_get_type(&obj_type, repo, commit_id);
841 if (error)
842 goto done;
844 if (obj_type != GOT_OBJ_TYPE_BLOB) {
845 error = got_error(GOT_ERR_OBJ_TYPE);
846 goto done;
849 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
850 if (error)
851 goto done;
853 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
854 if (error)
855 goto done;
857 error = got_object_blob_is_binary(binary, *blob);
858 if (error)
859 goto done;
861 done:
862 if (commit)
863 got_object_commit_close(commit);
865 if (error) {
866 if (*fd != -1)
867 close(*fd);
868 if (*blob)
869 got_object_blob_close(*blob);
870 *fd = -1;
871 *blob = NULL;
874 free(in_repo_path);
875 free(commit_id);
876 free(path);
877 return error;
880 int
881 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
882 int (*cb)(struct template *, const char *, size_t))
884 const struct got_error *err;
885 char *line = NULL;
886 size_t linesize = 0;
887 size_t lineno = 0;
888 ssize_t linelen = 0;
890 for (;;) {
891 err = got_object_blob_getline(&line, &linelen, &linesize,
892 blob);
893 if (err || linelen == -1)
894 break;
895 lineno++;
896 if (cb(tp, line, lineno) == -1) {
897 err = got_error(GOT_ERR_CANCELLED);
898 break;
902 free(line);
904 if (err) {
905 if (err->code != GOT_ERR_CANCELLED)
906 log_warnx("%s: got_object_blob_getline failed: %s",
907 __func__, err->msg);
908 return -1;
910 return 0;
913 struct blame_cb_args {
914 struct blame_line *lines;
915 int nlines;
916 int nlines_prec;
917 int lineno_cur;
918 off_t *line_offsets;
919 FILE *f;
920 struct got_repository *repo;
921 struct request *c;
922 got_render_blame_line_cb cb;
923 };
925 static const struct got_error *
926 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
927 struct got_commit_object *commit, struct got_object_id *id)
929 const struct got_error *err = NULL;
930 struct blame_cb_args *a = arg;
931 struct blame_line *bline;
932 struct request *c = a->c;
933 char *line = NULL;
934 size_t linesize = 0;
935 off_t offset;
936 struct tm tm;
937 time_t committer_time;
939 if (nlines != a->nlines ||
940 (lineno != -1 && lineno < 1) || lineno > a->nlines)
941 return got_error(GOT_ERR_RANGE);
943 if (lineno == -1)
944 return NULL; /* no change in this commit */
946 /* Annotate this line. */
947 bline = &a->lines[lineno - 1];
948 if (bline->annotated)
949 return NULL;
950 err = got_object_id_str(&bline->id_str, id);
951 if (err)
952 return err;
954 bline->committer = strdup(got_object_commit_get_committer(commit));
955 if (bline->committer == NULL) {
956 err = got_error_from_errno("strdup");
957 goto done;
960 committer_time = got_object_commit_get_committer_time(commit);
961 if (gmtime_r(&committer_time, &tm) == NULL)
962 return got_error_from_errno("gmtime_r");
963 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
964 &tm) == 0) {
965 err = got_error(GOT_ERR_NO_SPACE);
966 goto done;
968 bline->annotated = 1;
970 /* Print lines annotated so far. */
971 bline = &a->lines[a->lineno_cur - 1];
972 if (!bline->annotated)
973 goto done;
975 offset = a->line_offsets[a->lineno_cur - 1];
976 if (fseeko(a->f, offset, SEEK_SET) == -1) {
977 err = got_error_from_errno("fseeko");
978 goto done;
981 while (a->lineno_cur <= a->nlines && bline->annotated) {
982 if (getline(&line, &linesize, a->f) == -1) {
983 if (ferror(a->f))
984 err = got_error_from_errno("getline");
985 break;
988 if (a->cb(c->tp, line, bline, a->nlines_prec,
989 a->lineno_cur) == -1) {
990 err = got_error(GOT_ERR_CANCELLED);
991 break;
994 a->lineno_cur++;
995 bline = &a->lines[a->lineno_cur - 1];
997 done:
998 free(line);
999 return err;
1002 const struct got_error *
1003 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1005 const struct got_error *error = NULL;
1006 struct transport *t = c->t;
1007 struct got_repository *repo = t->repo;
1008 struct querystring *qs = c->t->qs;
1009 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1010 struct got_commit_object *commit = NULL;
1011 struct got_reflist_head refs;
1012 struct got_blob_object *blob = NULL;
1013 char *path = NULL, *in_repo_path = NULL;
1014 struct blame_cb_args bca;
1015 int i, obj_type, blobfd = -1, fd1 = -1, fd2 = -1;
1016 off_t filesize;
1017 FILE *f1 = NULL, *f2 = NULL;
1019 TAILQ_INIT(&refs);
1020 bca.f = NULL;
1021 bca.lines = NULL;
1022 bca.cb = cb;
1024 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1025 qs->folder ? "/" : "", qs->file) == -1) {
1026 error = got_error_from_errno("asprintf");
1027 goto done;
1030 error = got_repo_map_path(&in_repo_path, repo, path);
1031 if (error)
1032 goto done;
1034 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1035 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1036 if (error)
1037 goto done;
1039 error = got_object_open_as_commit(&commit, repo, commit_id);
1040 if (error)
1041 goto done;
1043 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1044 if (error)
1045 goto done;
1047 error = got_object_get_type(&obj_type, repo, obj_id);
1048 if (error)
1049 goto done;
1051 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1052 error = got_error(GOT_ERR_OBJ_TYPE);
1053 goto done;
1056 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1]);
1057 if (error)
1058 goto done;
1060 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &blobfd);
1061 if (error)
1062 goto done;
1064 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, blobfd);
1065 if (error)
1066 goto done;
1068 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1069 &bca.line_offsets, bca.f, blob);
1070 if (error || bca.nlines == 0)
1071 goto done;
1073 /* Don't include \n at EOF in the blame line count. */
1074 if (bca.line_offsets[bca.nlines - 1] == filesize)
1075 bca.nlines--;
1077 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1078 if (bca.lines == NULL) {
1079 error = got_error_from_errno("calloc");
1080 goto done;
1082 bca.lineno_cur = 1;
1083 bca.nlines_prec = 0;
1084 i = bca.nlines;
1085 while (i > 0) {
1086 i /= 10;
1087 bca.nlines_prec++;
1089 bca.repo = repo;
1090 bca.c = c;
1092 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd1);
1093 if (error)
1094 goto done;
1096 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd2);
1097 if (error)
1098 goto done;
1100 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5]);
1101 if (error)
1102 goto done;
1104 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6]);
1105 if (error)
1106 goto done;
1108 error = got_blame(in_repo_path, commit_id, repo,
1109 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1110 fd1, fd2, f1, f2);
1112 done:
1113 if (bca.lines) {
1114 free(bca.line_offsets);
1115 for (i = 0; i < bca.nlines; i++) {
1116 struct blame_line *bline = &bca.lines[i];
1117 free(bline->id_str);
1118 free(bline->committer);
1121 free(bca.lines);
1122 if (blobfd != -1 && close(blobfd) == -1 && error == NULL)
1123 error = got_error_from_errno("close");
1124 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1125 error = got_error_from_errno("close");
1126 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1127 error = got_error_from_errno("close");
1128 if (bca.f) {
1129 const struct got_error *bca_err = got_gotweb_closefile(bca.f);
1130 if (error == NULL)
1131 error = bca_err;
1133 if (f1) {
1134 const struct got_error *f1_err = got_gotweb_closefile(f1);
1135 if (error == NULL)
1136 error = f1_err;
1138 if (f2) {
1139 const struct got_error *f2_err = got_gotweb_closefile(f2);
1140 if (error == NULL)
1141 error = f2_err;
1143 if (commit)
1144 got_object_commit_close(commit);
1145 if (blob)
1146 got_object_blob_close(blob);
1147 free(in_repo_path);
1148 free(commit_id);
1149 free(obj_id);
1150 free(path);
1151 got_ref_list_free(&refs);
1152 return error;
1155 const struct got_error *
1156 got_open_diff_for_output(FILE **fp, struct request *c)
1158 const struct got_error *error = NULL;
1159 struct transport *t = c->t;
1160 struct got_repository *repo = t->repo;
1161 struct repo_commit *rc = NULL;
1162 struct got_object_id *id1 = NULL, *id2 = NULL;
1163 struct got_reflist_head refs;
1164 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1165 int obj_type, fd1 = -1, fd2 = -1;
1167 *fp = NULL;
1169 TAILQ_INIT(&refs);
1171 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1]);
1172 if (error)
1173 return error;
1175 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2]);
1176 if (error)
1177 return error;
1179 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3]);
1180 if (error)
1181 return error;
1183 rc = TAILQ_FIRST(&t->repo_commits);
1185 if (rc->parent_id != NULL &&
1186 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1187 error = got_repo_match_object_id(&id1, NULL,
1188 rc->parent_id, GOT_OBJ_TYPE_ANY,
1189 &refs, repo);
1190 if (error)
1191 goto done;
1194 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1195 GOT_OBJ_TYPE_ANY, &refs, repo);
1196 if (error)
1197 goto done;
1199 error = got_object_get_type(&obj_type, repo, id2);
1200 if (error)
1201 goto done;
1203 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd1);
1204 if (error)
1205 goto done;
1207 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd2);
1208 if (error)
1209 goto done;
1211 switch (obj_type) {
1212 case GOT_OBJ_TYPE_BLOB:
1213 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd1, fd2,
1214 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1215 NULL, repo, f3);
1216 break;
1217 case GOT_OBJ_TYPE_TREE:
1218 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
1219 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1220 NULL, repo, f3);
1221 break;
1222 case GOT_OBJ_TYPE_COMMIT:
1223 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd1,
1224 fd2, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1225 NULL, repo, f3);
1226 break;
1227 default:
1228 error = got_error(GOT_ERR_OBJ_TYPE);
1230 if (error)
1231 goto done;
1233 if (fseek(f3, 0, SEEK_SET) == -1) {
1234 error = got_ferror(f3, GOT_ERR_IO);
1235 goto done;
1238 *fp = f3;
1240 done:
1241 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1242 error = got_error_from_errno("close");
1243 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1244 error = got_error_from_errno("close");
1245 if (f1) {
1246 const struct got_error *f1_err = got_gotweb_closefile(f1);
1247 if (error == NULL)
1248 error = f1_err;
1250 if (f2) {
1251 const struct got_error *f2_err = got_gotweb_closefile(f2);
1252 if (error == NULL)
1253 error = f2_err;
1255 if (error && f3) {
1256 got_gotweb_closefile(f3);
1257 *fp = NULL;
1259 got_ref_list_free(&refs);
1260 free(id1);
1261 free(id2);
1262 return error;
1265 static const struct got_error *
1266 got_init_repo_commit(struct repo_commit **rc)
1268 *rc = calloc(1, sizeof(**rc));
1269 if (*rc == NULL)
1270 return got_error_from_errno2(__func__, "calloc");
1272 (*rc)->path = NULL;
1273 (*rc)->refs_str = NULL;
1274 (*rc)->commit_id = NULL;
1275 (*rc)->committer = NULL;
1276 (*rc)->author = NULL;
1277 (*rc)->parent_id = NULL;
1278 (*rc)->tree_id = NULL;
1279 (*rc)->commit_msg = NULL;
1281 return NULL;
1284 static const struct got_error *
1285 got_init_repo_tag(struct repo_tag **rt)
1287 *rt = calloc(1, sizeof(**rt));
1288 if (*rt == NULL)
1289 return got_error_from_errno2(__func__, "calloc");
1291 (*rt)->commit_id = NULL;
1292 (*rt)->tag_name = NULL;
1293 (*rt)->tag_commit = NULL;
1294 (*rt)->commit_msg = NULL;
1295 (*rt)->tagger = NULL;
1297 return NULL;