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, size_t 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;
331 if (limit == 0)
332 return got_error(GOT_ERR_RANGE);
334 if (limit > 1) {
335 /*
336 * Traverse one commit more than requested to provide
337 * the next button.
338 */
339 limit++;
340 chk_next = 1;
343 TAILQ_INIT(&refs);
345 if (qs->file != NULL && *qs->file != '\0')
346 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
347 qs->file) == -1)
348 return got_error_from_errno("asprintf");
350 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
351 repo_dir->name) == -1) {
352 error = got_error_from_errno("asprintf");
353 goto done;
356 if (qs->commit) {
357 error = got_repo_match_object_id_prefix(&id, qs->commit,
358 GOT_OBJ_TYPE_COMMIT, repo);
359 if (error)
360 goto done;
361 } else {
362 error = got_ref_open(&ref, repo, qs->headref, 0);
363 if (error)
364 goto done;
366 error = got_ref_resolve(&id, repo, ref);
367 if (error)
368 goto done;
371 error = got_repo_map_path(&in_repo_path, repo, repo_path);
372 if (error)
373 goto done;
375 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
376 if (error)
377 goto done;
379 if (qs->file != NULL && *qs->file != '\0') {
380 error = got_commit_graph_open(&graph, file_path, 0);
381 if (error)
382 goto done;
383 } else {
384 error = got_commit_graph_open(&graph, in_repo_path, 0);
385 if (error)
386 goto done;
389 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
390 if (error)
391 goto done;
393 for (;;) {
394 struct got_object_id next_id;
396 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
397 NULL);
398 if (error) {
399 if (error->code == GOT_ERR_ITER_COMPLETED)
400 error = NULL;
401 goto done;
404 error = got_object_open_as_commit(&commit, repo, &next_id);
405 if (error)
406 goto done;
408 error = got_init_repo_commit(&repo_commit);
409 if (error)
410 goto done;
412 error = got_get_repo_commit(c, repo_commit, commit,
413 &refs, &next_id);
414 if (error) {
415 gotweb_free_repo_commit(repo_commit);
416 goto done;
419 if (--limit == 0 && chk_next) {
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;
426 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
428 if (limit == 0)
429 goto done;
431 if (commit) {
432 got_object_commit_close(commit);
433 commit = NULL;
436 done:
437 if (ref)
438 got_ref_close(ref);
439 if (commit)
440 got_object_commit_close(commit);
441 if (graph)
442 got_commit_graph_close(graph);
443 got_ref_list_free(&refs);
444 free(in_repo_path);
445 free(file_path);
446 free(repo_path);
447 free(id);
448 return error;
451 const struct got_error *
452 got_get_repo_tags(struct request *c, size_t limit)
454 const struct got_error *error = NULL;
455 struct got_object_id *id = NULL;
456 struct got_commit_object *commit = NULL;
457 struct got_reflist_head refs;
458 struct got_reference *ref;
459 struct got_reflist_entry *re;
460 struct server *srv = c->srv;
461 struct transport *t = c->t;
462 struct got_repository *repo = t->repo;
463 struct querystring *qs = t->qs;
464 struct repo_dir *repo_dir = t->repo_dir;
465 struct got_tag_object *tag = NULL;
466 struct repo_tag *rt = NULL, *trt = NULL;
467 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
468 char *tag_commit = NULL, *tag_commit0 = NULL;
469 char *commit_msg = NULL, *commit_msg0 = NULL;
470 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
472 TAILQ_INIT(&refs);
474 if (limit == 0)
475 return got_error(GOT_ERR_RANGE);
477 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
478 repo_dir->name) == -1)
479 return got_error_from_errno("asprintf");
481 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
482 error = got_ref_open(&ref, repo, qs->headref, 0);
483 if (error)
484 goto err;
485 error = got_ref_resolve(&id, repo, ref);
486 got_ref_close(ref);
487 if (error)
488 goto err;
489 } else if (qs->commit == NULL && qs->action == TAG) {
490 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
491 goto err;
492 } else {
493 error = got_repo_match_object_id_prefix(&id, qs->commit,
494 GOT_OBJ_TYPE_COMMIT, repo);
495 if (error)
496 goto err;
499 if (qs->action != SUMMARY && qs->action != TAGS) {
500 error = got_object_open_as_commit(&commit, repo, id);
501 if (error)
502 goto err;
503 error = got_object_commit_get_logmsg(&commit_msg0, commit);
504 if (error)
505 goto err;
506 if (commit) {
507 got_object_commit_close(commit);
508 commit = NULL;
512 error = got_repo_map_path(&in_repo_path, repo, repo_path);
513 if (error)
514 goto err;
516 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
517 repo);
518 if (error)
519 goto err;
521 if (limit == 1)
522 chk_multi = 0;
524 /*
525 * XXX: again, see previous message about caching
526 */
528 TAILQ_FOREACH(re, &refs, entry) {
529 struct repo_tag *new_repo_tag = NULL;
530 error = got_init_repo_tag(&new_repo_tag);
531 if (error)
532 goto err;
534 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
536 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
537 if (new_repo_tag->tag_name == NULL) {
538 error = got_error_from_errno("strdup");
539 goto err;
542 free(id);
543 id = NULL;
545 free(id_str);
546 id_str = NULL;
548 error = got_ref_resolve(&id, repo, re->ref);
549 if (error)
550 goto done;
552 if (tag)
553 got_object_tag_close(tag);
554 error = got_object_open_as_tag(&tag, repo, id);
555 if (error) {
556 if (error->code != GOT_ERR_OBJ_TYPE)
557 goto done;
558 /* "lightweight" tag */
559 error = got_object_open_as_commit(&commit, repo, id);
560 if (error)
561 goto done;
562 new_repo_tag->tagger =
563 strdup(got_object_commit_get_committer(commit));
564 if (new_repo_tag->tagger == NULL) {
565 error = got_error_from_errno("strdup");
566 goto err;
568 new_repo_tag->tagger_time =
569 got_object_commit_get_committer_time(commit);
570 error = got_object_id_str(&id_str, id);
571 if (error)
572 goto err;
573 } else {
574 new_repo_tag->tagger =
575 strdup(got_object_tag_get_tagger(tag));
576 if (new_repo_tag->tagger == NULL) {
577 error = got_error_from_errno("strdup");
578 goto err;
580 new_repo_tag->tagger_time =
581 got_object_tag_get_tagger_time(tag);
582 error = got_object_id_str(&id_str,
583 got_object_tag_get_object_id(tag));
584 if (error)
585 goto err;
588 new_repo_tag->commit_id = strdup(id_str);
589 if (new_repo_tag->commit_id == NULL)
590 goto err;
592 if (commit_found == 0 && qs->commit != NULL &&
593 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
594 continue;
595 else
596 commit_found = 1;
598 t->tag_count++;
600 /*
601 * check for one more commit before breaking,
602 * so we know whether to navigate through briefs
603 * commits and summary
604 */
605 if (chk_next) {
606 t->next_id = strdup(new_repo_tag->commit_id);
607 if (t->next_id == NULL) {
608 error = got_error_from_errno("strdup");
609 goto err;
611 if (commit) {
612 got_object_commit_close(commit);
613 commit = NULL;
615 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
616 gotweb_free_repo_tag(new_repo_tag);
617 goto done;
620 if (commit) {
621 error = got_object_commit_get_logmsg(&tag_commit0,
622 commit);
623 if (error)
624 goto err;
625 got_object_commit_close(commit);
626 commit = NULL;
627 } else {
628 tag_commit0 = strdup(got_object_tag_get_message(tag));
629 if (tag_commit0 == NULL) {
630 error = got_error_from_errno("strdup");
631 goto err;
635 tag_commit = tag_commit0;
636 while (*tag_commit == '\n')
637 tag_commit++;
638 new_repo_tag->tag_commit = strdup(tag_commit);
639 if (new_repo_tag->tag_commit == NULL) {
640 error = got_error_from_errno("strdup");
641 free(tag_commit0);
642 goto err;
644 free(tag_commit0);
646 if (qs->action != SUMMARY && qs->action != TAGS) {
647 commit_msg = commit_msg0;
648 while (*commit_msg == '\n')
649 commit_msg++;
651 new_repo_tag->commit_msg = strdup(commit_msg);
652 if (new_repo_tag->commit_msg == NULL) {
653 error = got_error_from_errno("strdup");
654 goto err;
658 if (limit && --limit == 0) {
659 if (chk_multi == 0)
660 break;
661 chk_next = 1;
665 done:
666 /*
667 * we have tailq populated, so find previous commit id
668 * for navigation through briefs and commits
669 */
670 if (t->tag_count == 0) {
671 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
672 TAILQ_REMOVE(&t->repo_tags, rt, entry);
673 gotweb_free_repo_tag(rt);
676 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
677 commit_found = 0;
678 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
679 entry) {
680 if (commit_found == 0 && rt->commit_id != NULL &&
681 strcmp(qs->commit, rt->commit_id) != 0) {
682 continue;
683 } else
684 commit_found = 1;
685 if (c_cnt == srv->max_commits_display ||
686 rt == TAILQ_FIRST(&t->repo_tags)) {
687 t->prev_id = strdup(rt->commit_id);
688 if (t->prev_id == NULL)
689 error = got_error_from_errno("strdup");
690 break;
692 c_cnt++;
695 err:
696 if (commit)
697 got_object_commit_close(commit);
698 if (tag)
699 got_object_tag_close(tag);
700 got_ref_list_free(&refs);
701 free(commit_msg0);
702 free(in_repo_path);
703 free(repo_path);
704 free(id);
705 free(id_str);
706 return error;
709 int
710 got_output_repo_tree(struct request *c,
711 int (*cb)(struct template *, struct got_tree_entry *))
713 const struct got_error *error = NULL;
714 struct transport *t = c->t;
715 struct got_commit_object *commit = NULL;
716 struct got_repository *repo = t->repo;
717 struct querystring *qs = t->qs;
718 struct repo_commit *rc = NULL;
719 struct got_object_id *tree_id = NULL, *commit_id = NULL;
720 struct got_reflist_head refs;
721 struct got_tree_object *tree = NULL;
722 struct got_tree_entry *te;
723 struct repo_dir *repo_dir = t->repo_dir;
724 char *escaped_name = NULL, *path = NULL;
725 int nentries, i;
727 TAILQ_INIT(&refs);
729 rc = TAILQ_FIRST(&t->repo_commits);
731 if (qs->folder != NULL) {
732 path = strdup(qs->folder);
733 if (path == NULL) {
734 error = got_error_from_errno("strdup");
735 goto done;
737 } else {
738 error = got_repo_map_path(&path, repo, repo_dir->path);
739 if (error)
740 goto done;
743 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
744 GOT_OBJ_TYPE_COMMIT, &refs, repo);
745 if (error)
746 goto done;
748 error = got_object_open_as_commit(&commit, repo, commit_id);
749 if (error)
750 goto done;
752 error = got_object_id_by_path(&tree_id, repo, commit, path);
753 if (error)
754 goto done;
756 error = got_object_open_as_tree(&tree, repo, tree_id);
757 if (error)
758 goto done;
760 nentries = got_object_tree_get_nentries(tree);
762 for (i = 0; i < nentries; i++) {
763 te = got_object_tree_get_entry(tree, i);
764 if (cb(c->tp, te) == -1) {
765 error = got_error(GOT_ERR_CANCELLED);
766 break;
769 done:
770 free(escaped_name);
771 free(path);
772 got_ref_list_free(&refs);
773 if (commit)
774 got_object_commit_close(commit);
775 if (tree)
776 got_object_tree_close(tree);
777 free(commit_id);
778 free(tree_id);
779 if (error) {
780 if (error->code != GOT_ERR_CANCELLED)
781 log_warnx("%s: %s", __func__, error->msg);
782 return -1;
784 return 0;
787 const struct got_error *
788 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
789 int *binary, struct request *c)
791 const struct got_error *error = NULL;
792 struct transport *t = c->t;
793 struct got_repository *repo = t->repo;
794 struct querystring *qs = c->t->qs;
795 struct got_commit_object *commit = NULL;
796 struct got_object_id *commit_id = NULL;
797 struct got_reflist_head refs;
798 char *path = NULL, *in_repo_path = NULL;
799 int obj_type;
801 TAILQ_INIT(&refs);
803 *blob = NULL;
804 *fd = -1;
805 *binary = 0;
807 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
808 qs->folder ? "/" : "", qs->file) == -1) {
809 error = got_error_from_errno("asprintf");
810 goto done;
813 error = got_repo_map_path(&in_repo_path, repo, path);
814 if (error)
815 goto done;
817 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
818 GOT_OBJ_TYPE_COMMIT, &refs, repo);
819 if (error)
820 goto done;
822 error = got_object_open_as_commit(&commit, repo, commit_id);
823 if (error)
824 goto done;
826 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
827 if (error)
828 goto done;
830 if (commit_id == NULL) {
831 error = got_error(GOT_ERR_NO_OBJ);
832 goto done;
835 error = got_object_get_type(&obj_type, repo, commit_id);
836 if (error)
837 goto done;
839 if (obj_type != GOT_OBJ_TYPE_BLOB) {
840 error = got_error(GOT_ERR_OBJ_TYPE);
841 goto done;
844 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
845 if (error)
846 goto done;
848 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
849 if (error)
850 goto done;
852 error = got_object_blob_is_binary(binary, *blob);
853 if (error)
854 goto done;
856 done:
857 if (commit)
858 got_object_commit_close(commit);
860 if (error) {
861 if (*fd != -1)
862 close(*fd);
863 if (*blob)
864 got_object_blob_close(*blob);
865 *fd = -1;
866 *blob = NULL;
869 free(in_repo_path);
870 free(commit_id);
871 free(path);
872 return error;
875 int
876 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
877 int (*cb)(struct template *, const char *, size_t))
879 const struct got_error *err;
880 char *line = NULL;
881 size_t linesize = 0;
882 size_t lineno = 0;
883 ssize_t linelen = 0;
885 for (;;) {
886 err = got_object_blob_getline(&line, &linelen, &linesize,
887 blob);
888 if (err || linelen == -1)
889 break;
890 lineno++;
891 if (cb(tp, line, lineno) == -1) {
892 err = got_error(GOT_ERR_CANCELLED);
893 break;
897 free(line);
899 if (err) {
900 if (err->code != GOT_ERR_CANCELLED)
901 log_warnx("%s: got_object_blob_getline failed: %s",
902 __func__, err->msg);
903 return -1;
905 return 0;
908 struct blame_cb_args {
909 struct blame_line *lines;
910 int nlines;
911 int nlines_prec;
912 int lineno_cur;
913 off_t *line_offsets;
914 FILE *f;
915 struct got_repository *repo;
916 struct request *c;
917 got_render_blame_line_cb cb;
918 };
920 static const struct got_error *
921 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
922 struct got_commit_object *commit, struct got_object_id *id)
924 const struct got_error *err = NULL;
925 struct blame_cb_args *a = arg;
926 struct blame_line *bline;
927 struct request *c = a->c;
928 char *line = NULL;
929 size_t linesize = 0;
930 off_t offset;
931 struct tm tm;
932 time_t committer_time;
934 if (nlines != a->nlines ||
935 (lineno != -1 && lineno < 1) || lineno > a->nlines)
936 return got_error(GOT_ERR_RANGE);
938 if (lineno == -1)
939 return NULL; /* no change in this commit */
941 /* Annotate this line. */
942 bline = &a->lines[lineno - 1];
943 if (bline->annotated)
944 return NULL;
945 err = got_object_id_str(&bline->id_str, id);
946 if (err)
947 return err;
949 bline->committer = strdup(got_object_commit_get_committer(commit));
950 if (bline->committer == NULL) {
951 err = got_error_from_errno("strdup");
952 goto done;
955 committer_time = got_object_commit_get_committer_time(commit);
956 if (gmtime_r(&committer_time, &tm) == NULL)
957 return got_error_from_errno("gmtime_r");
958 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
959 &tm) == 0) {
960 err = got_error(GOT_ERR_NO_SPACE);
961 goto done;
963 bline->annotated = 1;
965 /* Print lines annotated so far. */
966 bline = &a->lines[a->lineno_cur - 1];
967 if (!bline->annotated)
968 goto done;
970 offset = a->line_offsets[a->lineno_cur - 1];
971 if (fseeko(a->f, offset, SEEK_SET) == -1) {
972 err = got_error_from_errno("fseeko");
973 goto done;
976 while (a->lineno_cur <= a->nlines && bline->annotated) {
977 if (getline(&line, &linesize, a->f) == -1) {
978 if (ferror(a->f))
979 err = got_error_from_errno("getline");
980 break;
983 if (a->cb(c->tp, line, bline, a->nlines_prec,
984 a->lineno_cur) == -1) {
985 err = got_error(GOT_ERR_CANCELLED);
986 break;
989 a->lineno_cur++;
990 bline = &a->lines[a->lineno_cur - 1];
992 done:
993 free(line);
994 return err;
997 const struct got_error *
998 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1000 const struct got_error *error = NULL;
1001 struct transport *t = c->t;
1002 struct got_repository *repo = t->repo;
1003 struct querystring *qs = c->t->qs;
1004 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1005 struct got_commit_object *commit = NULL;
1006 struct got_reflist_head refs;
1007 struct got_blob_object *blob = NULL;
1008 char *path = NULL, *in_repo_path = NULL;
1009 struct blame_cb_args bca;
1010 int i, obj_type, blobfd = -1, fd1 = -1, fd2 = -1;
1011 off_t filesize;
1012 FILE *f1 = NULL, *f2 = NULL;
1014 TAILQ_INIT(&refs);
1015 bca.f = NULL;
1016 bca.lines = NULL;
1017 bca.cb = cb;
1019 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1020 qs->folder ? "/" : "", qs->file) == -1) {
1021 error = got_error_from_errno("asprintf");
1022 goto done;
1025 error = got_repo_map_path(&in_repo_path, repo, path);
1026 if (error)
1027 goto done;
1029 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1030 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1031 if (error)
1032 goto done;
1034 error = got_object_open_as_commit(&commit, repo, commit_id);
1035 if (error)
1036 goto done;
1038 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1039 if (error)
1040 goto done;
1042 error = got_object_get_type(&obj_type, repo, obj_id);
1043 if (error)
1044 goto done;
1046 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1047 error = got_error(GOT_ERR_OBJ_TYPE);
1048 goto done;
1051 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1]);
1052 if (error)
1053 goto done;
1055 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &blobfd);
1056 if (error)
1057 goto done;
1059 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, blobfd);
1060 if (error)
1061 goto done;
1063 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1064 &bca.line_offsets, bca.f, blob);
1065 if (error || bca.nlines == 0)
1066 goto done;
1068 /* Don't include \n at EOF in the blame line count. */
1069 if (bca.line_offsets[bca.nlines - 1] == filesize)
1070 bca.nlines--;
1072 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1073 if (bca.lines == NULL) {
1074 error = got_error_from_errno("calloc");
1075 goto done;
1077 bca.lineno_cur = 1;
1078 bca.nlines_prec = 0;
1079 i = bca.nlines;
1080 while (i > 0) {
1081 i /= 10;
1082 bca.nlines_prec++;
1084 bca.repo = repo;
1085 bca.c = c;
1087 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd1);
1088 if (error)
1089 goto done;
1091 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd2);
1092 if (error)
1093 goto done;
1095 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5]);
1096 if (error)
1097 goto done;
1099 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6]);
1100 if (error)
1101 goto done;
1103 error = got_blame(in_repo_path, commit_id, repo,
1104 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1105 fd1, fd2, f1, f2);
1107 done:
1108 if (bca.lines) {
1109 free(bca.line_offsets);
1110 for (i = 0; i < bca.nlines; i++) {
1111 struct blame_line *bline = &bca.lines[i];
1112 free(bline->id_str);
1113 free(bline->committer);
1116 free(bca.lines);
1117 if (blobfd != -1 && close(blobfd) == -1 && error == NULL)
1118 error = got_error_from_errno("close");
1119 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1120 error = got_error_from_errno("close");
1121 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1122 error = got_error_from_errno("close");
1123 if (bca.f) {
1124 const struct got_error *bca_err = got_gotweb_closefile(bca.f);
1125 if (error == NULL)
1126 error = bca_err;
1128 if (f1) {
1129 const struct got_error *f1_err = got_gotweb_closefile(f1);
1130 if (error == NULL)
1131 error = f1_err;
1133 if (f2) {
1134 const struct got_error *f2_err = got_gotweb_closefile(f2);
1135 if (error == NULL)
1136 error = f2_err;
1138 if (commit)
1139 got_object_commit_close(commit);
1140 if (blob)
1141 got_object_blob_close(blob);
1142 free(in_repo_path);
1143 free(commit_id);
1144 free(obj_id);
1145 free(path);
1146 got_ref_list_free(&refs);
1147 return error;
1150 const struct got_error *
1151 got_open_diff_for_output(FILE **fp, struct request *c)
1153 const struct got_error *error = NULL;
1154 struct transport *t = c->t;
1155 struct got_repository *repo = t->repo;
1156 struct repo_commit *rc = NULL;
1157 struct got_object_id *id1 = NULL, *id2 = NULL;
1158 struct got_reflist_head refs;
1159 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1160 int obj_type, fd1 = -1, fd2 = -1;
1162 *fp = NULL;
1164 TAILQ_INIT(&refs);
1166 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1]);
1167 if (error)
1168 return error;
1170 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2]);
1171 if (error)
1172 return error;
1174 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3]);
1175 if (error)
1176 return error;
1178 rc = TAILQ_FIRST(&t->repo_commits);
1180 if (rc->parent_id != NULL &&
1181 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1182 error = got_repo_match_object_id(&id1, NULL,
1183 rc->parent_id, GOT_OBJ_TYPE_ANY,
1184 &refs, repo);
1185 if (error)
1186 goto done;
1189 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1190 GOT_OBJ_TYPE_ANY, &refs, repo);
1191 if (error)
1192 goto done;
1194 error = got_object_get_type(&obj_type, repo, id2);
1195 if (error)
1196 goto done;
1198 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd1);
1199 if (error)
1200 goto done;
1202 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd2);
1203 if (error)
1204 goto done;
1206 switch (obj_type) {
1207 case GOT_OBJ_TYPE_BLOB:
1208 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd1, fd2,
1209 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1210 NULL, repo, f3);
1211 break;
1212 case GOT_OBJ_TYPE_TREE:
1213 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
1214 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1215 NULL, repo, f3);
1216 break;
1217 case GOT_OBJ_TYPE_COMMIT:
1218 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd1,
1219 fd2, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1220 NULL, repo, f3);
1221 break;
1222 default:
1223 error = got_error(GOT_ERR_OBJ_TYPE);
1225 if (error)
1226 goto done;
1228 if (fseek(f3, 0, SEEK_SET) == -1) {
1229 error = got_ferror(f3, GOT_ERR_IO);
1230 goto done;
1233 *fp = f3;
1235 done:
1236 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1237 error = got_error_from_errno("close");
1238 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1239 error = got_error_from_errno("close");
1240 if (f1) {
1241 const struct got_error *f1_err = got_gotweb_closefile(f1);
1242 if (error == NULL)
1243 error = f1_err;
1245 if (f2) {
1246 const struct got_error *f2_err = got_gotweb_closefile(f2);
1247 if (error == NULL)
1248 error = f2_err;
1250 if (error && f3) {
1251 got_gotweb_closefile(f3);
1252 *fp = NULL;
1254 got_ref_list_free(&refs);
1255 free(id1);
1256 free(id2);
1257 return error;
1260 static const struct got_error *
1261 got_init_repo_commit(struct repo_commit **rc)
1263 *rc = calloc(1, sizeof(**rc));
1264 if (*rc == NULL)
1265 return got_error_from_errno2(__func__, "calloc");
1267 (*rc)->path = NULL;
1268 (*rc)->refs_str = NULL;
1269 (*rc)->commit_id = NULL;
1270 (*rc)->committer = NULL;
1271 (*rc)->author = NULL;
1272 (*rc)->parent_id = NULL;
1273 (*rc)->tree_id = NULL;
1274 (*rc)->commit_msg = NULL;
1276 return NULL;
1279 static const struct got_error *
1280 got_init_repo_tag(struct repo_tag **rt)
1282 *rt = calloc(1, sizeof(**rt));
1283 if (*rt == NULL)
1284 return got_error_from_errno2(__func__, "calloc");
1286 (*rt)->commit_id = NULL;
1287 (*rt)->tag_name = NULL;
1288 (*rt)->tag_commit = NULL;
1289 (*rt)->commit_msg = NULL;
1290 (*rt)->tagger = NULL;
1292 return NULL;