portable: build gotsh when --enable-gotd passed
[got-portable.git] / gotwebd / got_operations.c
blob39e6d570aff5f809ccc1326d4431dca03779b3bd
1 /*
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 "got_compat.h"
20 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
24 #include <event.h>
25 #include <imsg.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)
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;
72 static const struct got_error *
73 got_gotweb_openfile(FILE **f, int *priv_fd)
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);
87 return NULL;
90 static const struct got_error *
91 got_gotweb_dupfd(int *priv_fd, int *fd)
93 *fd = dup(*priv_fd);
95 if (*fd == -1)
96 return got_error_from_errno("dup");
98 return NULL;
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 transport *t = c->t;
132 struct got_repository *repo = t->repo;
133 struct got_commit_object *commit = NULL;
134 struct got_reflist_head refs;
135 struct got_reflist_entry *re;
136 time_t committer_time = 0, cmp_time = 0;
138 TAILQ_INIT(&refs);
140 *repo_age = 0;
142 error = got_ref_list(&refs, repo, "refs/heads",
143 got_ref_cmp_by_name, NULL);
144 if (error)
145 goto done;
148 * Find the youngest branch tip in the repository, or the age of
149 * the a specific branch tip if a name was provided by the caller.
151 TAILQ_FOREACH(re, &refs, entry) {
152 struct got_object_id *id = NULL;
154 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
155 continue;
157 error = got_ref_resolve(&id, repo, re->ref);
158 if (error)
159 goto done;
161 error = got_object_open_as_commit(&commit, repo, id);
162 free(id);
163 if (error)
164 goto done;
166 committer_time =
167 got_object_commit_get_committer_time(commit);
168 got_object_commit_close(commit);
169 if (cmp_time < committer_time)
170 cmp_time = committer_time;
172 if (refname)
173 break;
176 if (cmp_time != 0)
177 *repo_age = cmp_time;
178 done:
179 got_ref_list_free(&refs);
180 return error;
183 static const struct got_error *
184 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
185 struct got_commit_object *commit, struct got_reflist_head *refs,
186 struct got_object_id *id)
188 const struct got_error *error = NULL;
189 struct got_reflist_entry *re;
190 struct got_object_id *id2 = NULL;
191 struct got_object_qid *parent_id;
192 struct transport *t = c->t;
193 struct querystring *qs = c->t->qs;
194 char *commit_msg = NULL, *commit_msg0;
196 TAILQ_FOREACH(re, refs, entry) {
197 char *s;
198 const char *name;
199 struct got_tag_object *tag = NULL;
200 struct got_object_id *ref_id;
201 int cmp;
203 if (got_ref_is_symbolic(re->ref))
204 continue;
206 name = got_ref_get_name(re->ref);
207 if (strncmp(name, "refs/", 5) == 0)
208 name += 5;
209 if (strncmp(name, "got/", 4) == 0)
210 continue;
211 if (strncmp(name, "heads/", 6) == 0)
212 name += 6;
213 if (strncmp(name, "remotes/", 8) == 0) {
214 name += 8;
215 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
216 continue;
218 error = got_ref_resolve(&ref_id, t->repo, re->ref);
219 if (error)
220 return error;
221 if (strncmp(name, "tags/", 5) == 0) {
222 error = got_object_open_as_tag(&tag, t->repo, ref_id);
223 if (error) {
224 if (error->code != GOT_ERR_OBJ_TYPE) {
225 free(ref_id);
226 continue;
229 * Ref points at something other
230 * than a tag.
232 error = NULL;
233 tag = NULL;
236 cmp = got_object_id_cmp(tag ?
237 got_object_tag_get_object_id(tag) : ref_id, id);
238 free(ref_id);
239 if (tag)
240 got_object_tag_close(tag);
241 if (cmp != 0)
242 continue;
243 s = repo_commit->refs_str;
244 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
245 s ? ", " : "", name) == -1) {
246 error = got_error_from_errno("asprintf");
247 free(s);
248 repo_commit->refs_str = NULL;
249 return error;
251 free(s);
254 error = got_object_id_str(&repo_commit->commit_id, id);
255 if (error)
256 return error;
258 error = got_object_id_str(&repo_commit->tree_id,
259 got_object_commit_get_tree_id(commit));
260 if (error)
261 return error;
263 if (qs->action == DIFF) {
264 parent_id = STAILQ_FIRST(
265 got_object_commit_get_parent_ids(commit));
266 if (parent_id != NULL) {
267 id2 = got_object_id_dup(&parent_id->id);
268 error = got_object_id_str(&repo_commit->parent_id, id2);
269 if (error)
270 return error;
271 free(id2);
272 } else {
273 repo_commit->parent_id = strdup("/dev/null");
274 if (repo_commit->parent_id == NULL) {
275 error = got_error_from_errno("strdup");
276 return error;
281 repo_commit->committer_time =
282 got_object_commit_get_committer_time(commit);
284 repo_commit->author =
285 strdup(got_object_commit_get_author(commit));
286 if (repo_commit->author == NULL) {
287 error = got_error_from_errno("strdup");
288 return error;
290 repo_commit->committer =
291 strdup(got_object_commit_get_committer(commit));
292 if (repo_commit->committer == NULL) {
293 error = got_error_from_errno("strdup");
294 return error;
296 error = got_object_commit_get_logmsg(&commit_msg0, commit);
297 if (error)
298 return error;
300 commit_msg = commit_msg0;
301 while (*commit_msg == '\n')
302 commit_msg++;
304 repo_commit->commit_msg = strdup(commit_msg);
305 if (repo_commit->commit_msg == NULL)
306 error = got_error_from_errno("strdup");
307 free(commit_msg0);
308 return error;
311 const struct got_error *
312 got_get_repo_commits(struct request *c, size_t limit)
314 const struct got_error *error = NULL;
315 struct got_object_id *id = NULL;
316 struct got_commit_graph *graph = NULL;
317 struct got_commit_object *commit = NULL;
318 struct got_reflist_head refs;
319 struct got_reference *ref = NULL;
320 struct repo_commit *repo_commit = NULL;
321 struct server *srv = c->srv;
322 struct transport *t = c->t;
323 struct got_repository *repo = t->repo;
324 struct querystring *qs = t->qs;
325 struct repo_dir *repo_dir = t->repo_dir;
326 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
327 int chk_next = 0;
329 if (limit == 0)
330 return got_error(GOT_ERR_RANGE);
332 if (limit > 1) {
334 * Traverse one commit more than requested to provide
335 * the next button.
337 limit++;
338 chk_next = 1;
341 TAILQ_INIT(&refs);
343 if (qs->file != NULL && *qs->file != '\0')
344 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
345 qs->file) == -1)
346 return got_error_from_errno("asprintf");
348 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
349 repo_dir->name) == -1) {
350 error = got_error_from_errno("asprintf");
351 goto done;
354 if (qs->commit) {
355 error = got_repo_match_object_id_prefix(&id, qs->commit,
356 GOT_OBJ_TYPE_COMMIT, repo);
357 if (error)
358 goto done;
359 } else {
360 error = got_ref_open(&ref, repo, qs->headref, 0);
361 if (error)
362 goto done;
364 error = got_ref_resolve(&id, repo, ref);
365 if (error)
366 goto done;
369 error = got_repo_map_path(&in_repo_path, repo, repo_path);
370 if (error)
371 goto done;
373 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
374 if (error)
375 goto done;
377 if (qs->file != NULL && *qs->file != '\0') {
378 error = got_commit_graph_open(&graph, file_path, 0);
379 if (error)
380 goto done;
381 } else {
382 error = got_commit_graph_open(&graph, in_repo_path, 0);
383 if (error)
384 goto done;
387 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
388 if (error)
389 goto done;
391 for (;;) {
392 struct got_object_id next_id;
394 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
395 NULL);
396 if (error) {
397 if (error->code == GOT_ERR_ITER_COMPLETED)
398 error = NULL;
399 goto done;
402 error = got_object_open_as_commit(&commit, repo, &next_id);
403 if (error)
404 goto done;
406 error = got_init_repo_commit(&repo_commit);
407 if (error)
408 goto done;
410 error = got_get_repo_commit(c, repo_commit, commit,
411 &refs, &next_id);
412 if (error) {
413 gotweb_free_repo_commit(repo_commit);
414 goto done;
417 if (--limit == 0 && chk_next) {
418 t->more_id = strdup(repo_commit->commit_id);
419 if (t->more_id == NULL)
420 error = got_error_from_errno("strdup");
421 goto done;
424 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
426 if (limit == 0)
427 goto done;
429 if (commit) {
430 got_object_commit_close(commit);
431 commit = NULL;
434 done:
435 if (ref)
436 got_ref_close(ref);
437 if (commit)
438 got_object_commit_close(commit);
439 if (graph)
440 got_commit_graph_close(graph);
441 got_ref_list_free(&refs);
442 free(in_repo_path);
443 free(file_path);
444 free(repo_path);
445 free(id);
446 return error;
449 const struct got_error *
450 got_get_repo_tags(struct request *c, size_t limit)
452 const struct got_error *error = NULL;
453 struct got_object_id *id = NULL;
454 struct got_commit_object *commit = NULL;
455 struct got_reflist_head refs;
456 struct got_reference *ref;
457 struct got_reflist_entry *re;
458 struct server *srv = c->srv;
459 struct transport *t = c->t;
460 struct got_repository *repo = t->repo;
461 struct querystring *qs = t->qs;
462 struct repo_dir *repo_dir = t->repo_dir;
463 struct got_tag_object *tag = NULL;
464 struct repo_tag *rt = NULL, *trt = NULL;
465 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
466 char *tag_commit = NULL, *tag_commit0 = NULL;
467 char *commit_msg = NULL, *commit_msg0 = NULL;
468 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
470 TAILQ_INIT(&refs);
472 if (limit == 0)
473 return got_error(GOT_ERR_RANGE);
475 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
476 repo_dir->name) == -1)
477 return got_error_from_errno("asprintf");
479 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
480 error = got_ref_open(&ref, repo, qs->headref, 0);
481 if (error)
482 goto err;
483 error = got_ref_resolve(&id, repo, ref);
484 got_ref_close(ref);
485 if (error)
486 goto err;
487 } else if (qs->commit == NULL && qs->action == TAG) {
488 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
489 goto err;
490 } else {
491 error = got_repo_match_object_id_prefix(&id, qs->commit,
492 GOT_OBJ_TYPE_COMMIT, repo);
493 if (error)
494 goto err;
497 if (qs->action != SUMMARY && qs->action != TAGS) {
498 error = got_object_open_as_commit(&commit, repo, id);
499 if (error)
500 goto err;
501 error = got_object_commit_get_logmsg(&commit_msg0, commit);
502 if (error)
503 goto err;
504 if (commit) {
505 got_object_commit_close(commit);
506 commit = NULL;
510 error = got_repo_map_path(&in_repo_path, repo, repo_path);
511 if (error)
512 goto err;
514 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
515 repo);
516 if (error)
517 goto err;
519 if (limit == 1)
520 chk_multi = 0;
523 * XXX: again, see previous message about caching
526 TAILQ_FOREACH(re, &refs, entry) {
527 struct repo_tag *new_repo_tag = NULL;
528 error = got_init_repo_tag(&new_repo_tag);
529 if (error)
530 goto err;
532 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
534 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
535 if (new_repo_tag->tag_name == NULL) {
536 error = got_error_from_errno("strdup");
537 goto err;
540 free(id);
541 id = NULL;
543 free(id_str);
544 id_str = NULL;
546 error = got_ref_resolve(&id, repo, re->ref);
547 if (error)
548 goto done;
550 if (tag)
551 got_object_tag_close(tag);
552 error = got_object_open_as_tag(&tag, repo, id);
553 if (error) {
554 if (error->code != GOT_ERR_OBJ_TYPE)
555 goto done;
556 /* "lightweight" tag */
557 error = got_object_open_as_commit(&commit, repo, id);
558 if (error)
559 goto done;
560 new_repo_tag->tagger =
561 strdup(got_object_commit_get_committer(commit));
562 if (new_repo_tag->tagger == NULL) {
563 error = got_error_from_errno("strdup");
564 goto err;
566 new_repo_tag->tagger_time =
567 got_object_commit_get_committer_time(commit);
568 error = got_object_id_str(&id_str, id);
569 if (error)
570 goto err;
571 } else {
572 new_repo_tag->tagger =
573 strdup(got_object_tag_get_tagger(tag));
574 if (new_repo_tag->tagger == NULL) {
575 error = got_error_from_errno("strdup");
576 goto err;
578 new_repo_tag->tagger_time =
579 got_object_tag_get_tagger_time(tag);
580 error = got_object_id_str(&id_str,
581 got_object_tag_get_object_id(tag));
582 if (error)
583 goto err;
586 new_repo_tag->commit_id = strdup(id_str);
587 if (new_repo_tag->commit_id == NULL)
588 goto err;
590 if (commit_found == 0 && qs->commit != NULL &&
591 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
592 continue;
593 else
594 commit_found = 1;
596 t->tag_count++;
599 * check for one more commit before breaking,
600 * so we know whether to navigate through briefs
601 * commits and summary
603 if (chk_next) {
604 t->next_id = strdup(new_repo_tag->commit_id);
605 if (t->next_id == NULL) {
606 error = got_error_from_errno("strdup");
607 goto err;
609 if (commit) {
610 got_object_commit_close(commit);
611 commit = NULL;
613 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
614 gotweb_free_repo_tag(new_repo_tag);
615 goto done;
618 if (commit) {
619 error = got_object_commit_get_logmsg(&tag_commit0,
620 commit);
621 if (error)
622 goto err;
623 got_object_commit_close(commit);
624 commit = NULL;
625 } else {
626 tag_commit0 = strdup(got_object_tag_get_message(tag));
627 if (tag_commit0 == NULL) {
628 error = got_error_from_errno("strdup");
629 goto err;
633 tag_commit = tag_commit0;
634 while (*tag_commit == '\n')
635 tag_commit++;
636 new_repo_tag->tag_commit = strdup(tag_commit);
637 if (new_repo_tag->tag_commit == NULL) {
638 error = got_error_from_errno("strdup");
639 free(tag_commit0);
640 goto err;
642 free(tag_commit0);
644 if (qs->action != SUMMARY && qs->action != TAGS) {
645 commit_msg = commit_msg0;
646 while (*commit_msg == '\n')
647 commit_msg++;
649 new_repo_tag->commit_msg = strdup(commit_msg);
650 if (new_repo_tag->commit_msg == NULL) {
651 error = got_error_from_errno("strdup");
652 goto err;
656 if (limit && --limit == 0) {
657 if (chk_multi == 0)
658 break;
659 chk_next = 1;
663 done:
665 * we have tailq populated, so find previous commit id
666 * for navigation through briefs and commits
668 if (t->tag_count == 0) {
669 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
670 TAILQ_REMOVE(&t->repo_tags, rt, entry);
671 gotweb_free_repo_tag(rt);
674 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
675 commit_found = 0;
676 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
677 entry) {
678 if (commit_found == 0 && rt->commit_id != NULL &&
679 strcmp(qs->commit, rt->commit_id) != 0) {
680 continue;
681 } else
682 commit_found = 1;
683 if (c_cnt == srv->max_commits_display ||
684 rt == TAILQ_FIRST(&t->repo_tags)) {
685 t->prev_id = strdup(rt->commit_id);
686 if (t->prev_id == NULL)
687 error = got_error_from_errno("strdup");
688 break;
690 c_cnt++;
693 err:
694 if (commit)
695 got_object_commit_close(commit);
696 if (tag)
697 got_object_tag_close(tag);
698 got_ref_list_free(&refs);
699 free(commit_msg0);
700 free(in_repo_path);
701 free(repo_path);
702 free(id);
703 free(id_str);
704 return error;
708 got_output_repo_tree(struct request *c,
709 int (*cb)(struct template *, struct got_tree_entry *))
711 const struct got_error *error = NULL;
712 struct transport *t = c->t;
713 struct got_commit_object *commit = NULL;
714 struct got_repository *repo = t->repo;
715 struct querystring *qs = t->qs;
716 struct repo_commit *rc = NULL;
717 struct got_object_id *tree_id = NULL, *commit_id = NULL;
718 struct got_reflist_head refs;
719 struct got_tree_object *tree = NULL;
720 struct got_tree_entry *te;
721 struct repo_dir *repo_dir = t->repo_dir;
722 char *escaped_name = NULL, *path = NULL;
723 int nentries, i;
725 TAILQ_INIT(&refs);
727 rc = TAILQ_FIRST(&t->repo_commits);
729 if (qs->folder != NULL) {
730 path = strdup(qs->folder);
731 if (path == NULL) {
732 error = got_error_from_errno("strdup");
733 goto done;
735 } else {
736 error = got_repo_map_path(&path, repo, repo_dir->path);
737 if (error)
738 goto done;
741 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
742 GOT_OBJ_TYPE_COMMIT, &refs, repo);
743 if (error)
744 goto done;
746 error = got_object_open_as_commit(&commit, repo, commit_id);
747 if (error)
748 goto done;
750 error = got_object_id_by_path(&tree_id, repo, commit, path);
751 if (error)
752 goto done;
754 error = got_object_open_as_tree(&tree, repo, tree_id);
755 if (error)
756 goto done;
758 nentries = got_object_tree_get_nentries(tree);
760 for (i = 0; i < nentries; i++) {
761 te = got_object_tree_get_entry(tree, i);
762 if (cb(c->tp, te) == -1) {
763 error = got_error(GOT_ERR_CANCELLED);
764 break;
767 done:
768 free(escaped_name);
769 free(path);
770 got_ref_list_free(&refs);
771 if (commit)
772 got_object_commit_close(commit);
773 if (tree)
774 got_object_tree_close(tree);
775 free(commit_id);
776 free(tree_id);
777 if (error) {
778 if (error->code != GOT_ERR_CANCELLED)
779 log_warnx("%s: %s", __func__, error->msg);
780 return -1;
782 return 0;
785 const struct got_error *
786 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
787 int *binary, struct request *c)
789 const struct got_error *error = NULL;
790 struct transport *t = c->t;
791 struct got_repository *repo = t->repo;
792 struct querystring *qs = c->t->qs;
793 struct got_commit_object *commit = NULL;
794 struct got_object_id *commit_id = NULL;
795 struct got_reflist_head refs;
796 char *path = NULL, *in_repo_path = NULL;
797 int obj_type;
799 TAILQ_INIT(&refs);
801 *blob = NULL;
802 *fd = -1;
803 *binary = 0;
805 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
806 qs->folder ? "/" : "", qs->file) == -1) {
807 error = got_error_from_errno("asprintf");
808 goto done;
811 error = got_repo_map_path(&in_repo_path, repo, path);
812 if (error)
813 goto done;
815 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
816 GOT_OBJ_TYPE_COMMIT, &refs, repo);
817 if (error)
818 goto done;
820 error = got_object_open_as_commit(&commit, repo, commit_id);
821 if (error)
822 goto done;
824 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
825 if (error)
826 goto done;
828 if (commit_id == NULL) {
829 error = got_error(GOT_ERR_NO_OBJ);
830 goto done;
833 error = got_object_get_type(&obj_type, repo, commit_id);
834 if (error)
835 goto done;
837 if (obj_type != GOT_OBJ_TYPE_BLOB) {
838 error = got_error(GOT_ERR_OBJ_TYPE);
839 goto done;
842 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
843 if (error)
844 goto done;
846 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
847 if (error)
848 goto done;
850 error = got_object_blob_is_binary(binary, *blob);
851 if (error)
852 goto done;
854 done:
855 if (commit)
856 got_object_commit_close(commit);
858 if (error) {
859 if (*fd != -1)
860 close(*fd);
861 if (*blob)
862 got_object_blob_close(*blob);
863 *fd = -1;
864 *blob = NULL;
867 free(in_repo_path);
868 free(commit_id);
869 free(path);
870 return error;
874 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
875 int (*cb)(struct template *, const char *, size_t))
877 const struct got_error *err;
878 char *line = NULL;
879 size_t linesize = 0;
880 size_t lineno = 0;
881 ssize_t linelen = 0;
883 for (;;) {
884 err = got_object_blob_getline(&line, &linelen, &linesize,
885 blob);
886 if (err || linelen == -1)
887 break;
888 lineno++;
889 if (cb(tp, line, lineno) == -1) {
890 err = got_error(GOT_ERR_CANCELLED);
891 break;
895 free(line);
897 if (err) {
898 if (err->code != GOT_ERR_CANCELLED)
899 log_warnx("%s: got_object_blob_getline failed: %s",
900 __func__, err->msg);
901 return -1;
903 return 0;
906 struct blame_cb_args {
907 struct blame_line *lines;
908 int nlines;
909 int nlines_prec;
910 int lineno_cur;
911 off_t *line_offsets;
912 FILE *f;
913 struct got_repository *repo;
914 struct request *c;
915 got_render_blame_line_cb cb;
918 static const struct got_error *
919 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
920 struct got_commit_object *commit, struct got_object_id *id)
922 const struct got_error *err = NULL;
923 struct blame_cb_args *a = arg;
924 struct blame_line *bline;
925 struct request *c = a->c;
926 char *line = NULL;
927 size_t linesize = 0;
928 off_t offset;
929 struct tm tm;
930 time_t committer_time;
932 if (nlines != a->nlines ||
933 (lineno != -1 && lineno < 1) || lineno > a->nlines)
934 return got_error(GOT_ERR_RANGE);
936 if (lineno == -1)
937 return NULL; /* no change in this commit */
939 /* Annotate this line. */
940 bline = &a->lines[lineno - 1];
941 if (bline->annotated)
942 return NULL;
943 err = got_object_id_str(&bline->id_str, id);
944 if (err)
945 return err;
947 bline->committer = strdup(got_object_commit_get_committer(commit));
948 if (bline->committer == NULL) {
949 err = got_error_from_errno("strdup");
950 goto done;
953 committer_time = got_object_commit_get_committer_time(commit);
954 if (gmtime_r(&committer_time, &tm) == NULL)
955 return got_error_from_errno("gmtime_r");
956 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
957 &tm) == 0) {
958 err = got_error(GOT_ERR_NO_SPACE);
959 goto done;
961 bline->annotated = 1;
963 /* Print lines annotated so far. */
964 bline = &a->lines[a->lineno_cur - 1];
965 if (!bline->annotated)
966 goto done;
968 offset = a->line_offsets[a->lineno_cur - 1];
969 if (fseeko(a->f, offset, SEEK_SET) == -1) {
970 err = got_error_from_errno("fseeko");
971 goto done;
974 while (a->lineno_cur <= a->nlines && bline->annotated) {
975 if (getline(&line, &linesize, a->f) == -1) {
976 if (ferror(a->f))
977 err = got_error_from_errno("getline");
978 break;
981 if (a->cb(c->tp, line, bline, a->nlines_prec,
982 a->lineno_cur) == -1) {
983 err = got_error(GOT_ERR_CANCELLED);
984 break;
987 a->lineno_cur++;
988 bline = &a->lines[a->lineno_cur - 1];
990 done:
991 free(line);
992 return err;
995 const struct got_error *
996 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
998 const struct got_error *error = NULL;
999 struct transport *t = c->t;
1000 struct got_repository *repo = t->repo;
1001 struct querystring *qs = c->t->qs;
1002 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1003 struct got_commit_object *commit = NULL;
1004 struct got_reflist_head refs;
1005 struct got_blob_object *blob = NULL;
1006 char *path = NULL, *in_repo_path = NULL;
1007 struct blame_cb_args bca;
1008 int i, obj_type, blobfd = -1, fd1 = -1, fd2 = -1;
1009 off_t filesize;
1010 FILE *f1 = NULL, *f2 = NULL;
1012 TAILQ_INIT(&refs);
1013 bca.f = NULL;
1014 bca.lines = NULL;
1015 bca.cb = cb;
1017 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1018 qs->folder ? "/" : "", qs->file) == -1) {
1019 error = got_error_from_errno("asprintf");
1020 goto done;
1023 error = got_repo_map_path(&in_repo_path, repo, path);
1024 if (error)
1025 goto done;
1027 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1028 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1029 if (error)
1030 goto done;
1032 error = got_object_open_as_commit(&commit, repo, commit_id);
1033 if (error)
1034 goto done;
1036 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1037 if (error)
1038 goto done;
1040 error = got_object_get_type(&obj_type, repo, obj_id);
1041 if (error)
1042 goto done;
1044 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1045 error = got_error(GOT_ERR_OBJ_TYPE);
1046 goto done;
1049 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1]);
1050 if (error)
1051 goto done;
1053 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &blobfd);
1054 if (error)
1055 goto done;
1057 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, blobfd);
1058 if (error)
1059 goto done;
1061 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1062 &bca.line_offsets, bca.f, blob);
1063 if (error || bca.nlines == 0)
1064 goto done;
1066 /* Don't include \n at EOF in the blame line count. */
1067 if (bca.line_offsets[bca.nlines - 1] == filesize)
1068 bca.nlines--;
1070 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1071 if (bca.lines == NULL) {
1072 error = got_error_from_errno("calloc");
1073 goto done;
1075 bca.lineno_cur = 1;
1076 bca.nlines_prec = 0;
1077 i = bca.nlines;
1078 while (i > 0) {
1079 i /= 10;
1080 bca.nlines_prec++;
1082 bca.repo = repo;
1083 bca.c = c;
1085 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd1);
1086 if (error)
1087 goto done;
1089 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd2);
1090 if (error)
1091 goto done;
1093 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5]);
1094 if (error)
1095 goto done;
1097 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6]);
1098 if (error)
1099 goto done;
1101 error = got_blame(in_repo_path, commit_id, repo,
1102 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1103 fd1, fd2, f1, f2);
1105 done:
1106 if (bca.lines) {
1107 free(bca.line_offsets);
1108 for (i = 0; i < bca.nlines; i++) {
1109 struct blame_line *bline = &bca.lines[i];
1110 free(bline->id_str);
1111 free(bline->committer);
1114 free(bca.lines);
1115 if (blobfd != -1 && close(blobfd) == -1 && error == NULL)
1116 error = got_error_from_errno("close");
1117 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1118 error = got_error_from_errno("close");
1119 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1120 error = got_error_from_errno("close");
1121 if (bca.f) {
1122 const struct got_error *bca_err = got_gotweb_closefile(bca.f);
1123 if (error == NULL)
1124 error = bca_err;
1126 if (f1) {
1127 const struct got_error *f1_err = got_gotweb_closefile(f1);
1128 if (error == NULL)
1129 error = f1_err;
1131 if (f2) {
1132 const struct got_error *f2_err = got_gotweb_closefile(f2);
1133 if (error == NULL)
1134 error = f2_err;
1136 if (commit)
1137 got_object_commit_close(commit);
1138 if (blob)
1139 got_object_blob_close(blob);
1140 free(in_repo_path);
1141 free(commit_id);
1142 free(obj_id);
1143 free(path);
1144 got_ref_list_free(&refs);
1145 return error;
1148 const struct got_error *
1149 got_open_diff_for_output(FILE **fp, struct request *c)
1151 const struct got_error *error = NULL;
1152 struct transport *t = c->t;
1153 struct got_repository *repo = t->repo;
1154 struct repo_commit *rc = NULL;
1155 struct got_object_id *id1 = NULL, *id2 = NULL;
1156 struct got_reflist_head refs;
1157 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1158 int obj_type, fd1 = -1, fd2 = -1;
1160 *fp = NULL;
1162 TAILQ_INIT(&refs);
1164 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1]);
1165 if (error)
1166 return error;
1168 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2]);
1169 if (error)
1170 return error;
1172 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3]);
1173 if (error)
1174 return error;
1176 rc = TAILQ_FIRST(&t->repo_commits);
1178 if (rc->parent_id != NULL &&
1179 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1180 error = got_repo_match_object_id(&id1, NULL,
1181 rc->parent_id, GOT_OBJ_TYPE_ANY,
1182 &refs, repo);
1183 if (error)
1184 goto done;
1187 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1188 GOT_OBJ_TYPE_ANY, &refs, repo);
1189 if (error)
1190 goto done;
1192 error = got_object_get_type(&obj_type, repo, id2);
1193 if (error)
1194 goto done;
1196 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd1);
1197 if (error)
1198 goto done;
1200 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd2);
1201 if (error)
1202 goto done;
1204 switch (obj_type) {
1205 case GOT_OBJ_TYPE_BLOB:
1206 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd1, fd2,
1207 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1208 NULL, repo, f3);
1209 break;
1210 case GOT_OBJ_TYPE_TREE:
1211 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
1212 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1213 NULL, repo, f3);
1214 break;
1215 case GOT_OBJ_TYPE_COMMIT:
1216 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd1,
1217 fd2, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1218 NULL, repo, f3);
1219 break;
1220 default:
1221 error = got_error(GOT_ERR_OBJ_TYPE);
1223 if (error)
1224 goto done;
1226 if (fseek(f3, 0, SEEK_SET) == -1) {
1227 error = got_ferror(f3, GOT_ERR_IO);
1228 goto done;
1231 *fp = f3;
1233 done:
1234 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1235 error = got_error_from_errno("close");
1236 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1237 error = got_error_from_errno("close");
1238 if (f1) {
1239 const struct got_error *f1_err = got_gotweb_closefile(f1);
1240 if (error == NULL)
1241 error = f1_err;
1243 if (f2) {
1244 const struct got_error *f2_err = got_gotweb_closefile(f2);
1245 if (error == NULL)
1246 error = f2_err;
1248 if (error && f3) {
1249 got_gotweb_closefile(f3);
1250 *fp = NULL;
1252 got_ref_list_free(&refs);
1253 free(id1);
1254 free(id2);
1255 return error;
1258 static const struct got_error *
1259 got_init_repo_commit(struct repo_commit **rc)
1261 *rc = calloc(1, sizeof(**rc));
1262 if (*rc == NULL)
1263 return got_error_from_errno2(__func__, "calloc");
1265 (*rc)->path = NULL;
1266 (*rc)->refs_str = NULL;
1267 (*rc)->commit_id = NULL;
1268 (*rc)->committer = NULL;
1269 (*rc)->author = NULL;
1270 (*rc)->parent_id = NULL;
1271 (*rc)->tree_id = NULL;
1272 (*rc)->commit_msg = NULL;
1274 return NULL;
1277 static const struct got_error *
1278 got_init_repo_tag(struct repo_tag **rt)
1280 *rt = calloc(1, sizeof(**rt));
1281 if (*rt == NULL)
1282 return got_error_from_errno2(__func__, "calloc");
1284 (*rt)->commit_id = NULL;
1285 (*rt)->tag_name = NULL;
1286 (*rt)->tag_commit = NULL;
1287 (*rt)->commit_msg = NULL;
1288 (*rt)->tagger = NULL;
1290 return NULL;