store ibuf used by got_repo_read_gitconfig() on the stack
[got-portable.git] / gotwebd / got_operations.c
blobec61ebf8c0701ffd8d2ab658042054365f232864
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/tree.h>
22 #include <sys/socket.h>
23 #include <sys/stat.h>
25 #include <event.h>
26 #include <imsg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_reference.h"
35 #include "got_repository.h"
36 #include "got_path.h"
37 #include "got_cancel.h"
38 #include "got_diff.h"
39 #include "got_commit_graph.h"
40 #include "got_blame.h"
41 #include "got_privsep.h"
43 #include "gotwebd.h"
44 #include "log.h"
46 static const struct got_error *got_init_repo_commit(struct repo_commit **);
47 static const struct got_error *got_init_repo_tag(struct repo_tag **);
48 static const struct got_error *got_get_repo_commit(struct request *,
49 struct repo_commit *, struct got_commit_object *, struct got_reflist_head *,
50 struct got_object_id *);
51 static const struct got_error *got_gotweb_dupfd(int *, int *);
52 static const struct got_error *got_gotweb_openfile(FILE **, int *);
53 static const struct got_error *got_gotweb_blame_cb(void *, int, int,
54 struct got_commit_object *,struct got_object_id *);
56 const struct got_error *
57 got_gotweb_closefile(FILE *f)
59 const struct got_error *err = NULL;
61 if (fseek(f, 0, SEEK_SET) == -1)
62 err = got_error_from_errno("fseek");
64 if (ftruncate(fileno(f), 0) == -1 && err == NULL)
65 err = got_error_from_errno("ftruncate");
67 if (fclose(f) == EOF && err == NULL)
68 err = got_error_from_errno("fclose");
70 return err;
73 static const struct got_error *
74 got_gotweb_openfile(FILE **f, int *priv_fd)
76 int fd;
78 fd = dup(*priv_fd);
79 if (fd == -1)
80 return got_error_from_errno("dup");
82 *f = fdopen(fd, "w+");
83 if (*f == NULL) {
84 close(fd);
85 return got_error(GOT_ERR_PRIVSEP_NO_FD);
88 return NULL;
91 static const struct got_error *
92 got_gotweb_dupfd(int *priv_fd, int *fd)
94 *fd = dup(*priv_fd);
96 if (*fd == -1)
97 return got_error_from_errno("dup");
99 return NULL;
102 const struct got_error *
103 got_get_repo_owner(char **owner, struct request *c)
105 struct transport *t = c->t;
106 struct got_repository *repo = t->repo;
107 const char *gitconfig_owner;
109 *owner = NULL;
110 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
111 if (gitconfig_owner) {
112 *owner = strdup(gitconfig_owner);
113 if (*owner == NULL)
114 return got_error_from_errno("strdup");
117 return NULL;
120 static const struct got_error *
121 get_ref_commit_timestamp(time_t *timestamp,
122 struct got_reference *ref, struct got_repository *repo)
124 const struct got_error *error;
125 struct got_object_id *id = NULL;
126 int obj_type;
127 struct got_commit_object *commit = NULL;
129 /* We might have a cached timestamp available. */
130 *timestamp = got_ref_get_cached_committer_time(ref);
131 if (*timestamp != 0)
132 return NULL;
134 error = got_ref_resolve(&id, repo, ref);
135 if (error)
136 return error;
138 error = got_object_get_type(&obj_type, repo, id);
139 if (error || obj_type != GOT_OBJ_TYPE_COMMIT)
140 goto done;
142 error = got_object_open_as_commit(&commit, repo, id);
143 if (error)
144 goto done;
146 *timestamp = got_object_commit_get_committer_time(commit);
147 done:
148 free(id);
149 if (commit)
150 got_object_commit_close(commit);
152 return error;
156 * Find the youngest branch tip in the repository, or the age of
157 * a specific branch tip if a name was provided by the caller.
159 const struct got_error *
160 got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
162 const struct got_error *error = NULL;
163 struct transport *t = c->t;
164 struct got_repository *repo = t->repo;
166 *repo_age = 0;
168 if (refname) {
169 struct got_reference *ref;
171 error = got_ref_open(&ref, repo, refname, 0);
172 if (error)
173 return error;
175 error = get_ref_commit_timestamp(repo_age, ref, repo);
176 got_ref_close(ref);
177 } else {
178 struct got_reflist_head refs;
179 struct got_reflist_entry *re;
181 TAILQ_INIT(&refs);
183 error = got_ref_list(&refs, repo, "refs/heads",
184 got_ref_cmp_by_commit_timestamp_descending, repo);
185 if (error)
186 return error;
188 re = TAILQ_FIRST(&refs);
189 if (re) {
190 error = get_ref_commit_timestamp(repo_age,
191 re->ref, repo);
193 got_ref_list_free(&refs);
196 return error;
199 static const struct got_error *
200 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
201 struct got_commit_object *commit, struct got_reflist_head *refs,
202 struct got_object_id *id)
204 const struct got_error *error = NULL;
205 struct got_reflist_entry *re;
206 struct got_object_id *id2 = NULL;
207 struct got_object_qid *parent_id;
208 struct transport *t = c->t;
209 struct querystring *qs = c->t->qs;
210 char *commit_msg = NULL, *commit_msg0;
212 TAILQ_FOREACH(re, refs, entry) {
213 char *s;
214 const char *name;
215 struct got_tag_object *tag = NULL;
216 struct got_object_id *ref_id;
217 int cmp;
219 if (got_ref_is_symbolic(re->ref))
220 continue;
222 name = got_ref_get_name(re->ref);
223 if (strncmp(name, "refs/", 5) == 0)
224 name += 5;
225 if (strncmp(name, "got/", 4) == 0)
226 continue;
227 if (strncmp(name, "heads/", 6) == 0)
228 name += 6;
229 if (strncmp(name, "remotes/", 8) == 0) {
230 name += 8;
231 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
232 continue;
234 error = got_ref_resolve(&ref_id, t->repo, re->ref);
235 if (error)
236 return error;
237 if (strncmp(name, "tags/", 5) == 0) {
238 error = got_object_open_as_tag(&tag, t->repo, ref_id);
239 if (error) {
240 if (error->code != GOT_ERR_OBJ_TYPE) {
241 free(ref_id);
242 continue;
245 * Ref points at something other
246 * than a tag.
248 error = NULL;
249 tag = NULL;
252 cmp = got_object_id_cmp(tag ?
253 got_object_tag_get_object_id(tag) : ref_id, id);
254 free(ref_id);
255 if (tag)
256 got_object_tag_close(tag);
257 if (cmp != 0)
258 continue;
259 s = repo_commit->refs_str;
260 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
261 s ? ", " : "", name) == -1) {
262 error = got_error_from_errno("asprintf");
263 free(s);
264 repo_commit->refs_str = NULL;
265 return error;
267 free(s);
270 error = got_object_id_str(&repo_commit->commit_id, id);
271 if (error)
272 return error;
274 error = got_object_id_str(&repo_commit->tree_id,
275 got_object_commit_get_tree_id(commit));
276 if (error)
277 return error;
279 if (qs->action == DIFF || qs->action == PATCH) {
280 parent_id = STAILQ_FIRST(
281 got_object_commit_get_parent_ids(commit));
282 if (parent_id != NULL) {
283 id2 = got_object_id_dup(&parent_id->id);
284 error = got_object_id_str(&repo_commit->parent_id, id2);
285 if (error)
286 return error;
287 free(id2);
288 } else {
289 repo_commit->parent_id = strdup("/dev/null");
290 if (repo_commit->parent_id == NULL) {
291 error = got_error_from_errno("strdup");
292 return error;
297 repo_commit->committer_time =
298 got_object_commit_get_committer_time(commit);
300 repo_commit->author =
301 strdup(got_object_commit_get_author(commit));
302 if (repo_commit->author == NULL) {
303 error = got_error_from_errno("strdup");
304 return error;
306 repo_commit->committer =
307 strdup(got_object_commit_get_committer(commit));
308 if (repo_commit->committer == NULL) {
309 error = got_error_from_errno("strdup");
310 return error;
312 error = got_object_commit_get_logmsg(&commit_msg0, commit);
313 if (error)
314 return error;
316 commit_msg = commit_msg0;
317 while (*commit_msg == '\n')
318 commit_msg++;
320 repo_commit->commit_msg = strdup(commit_msg);
321 if (repo_commit->commit_msg == NULL)
322 error = got_error_from_errno("strdup");
323 free(commit_msg0);
324 return error;
327 const struct got_error *
328 got_get_repo_commits(struct request *c, size_t limit)
330 const struct got_error *error = NULL;
331 struct got_object_id *id = NULL;
332 struct got_commit_graph *graph = NULL;
333 struct got_commit_object *commit = NULL;
334 struct got_reflist_head refs;
335 struct got_reference *ref = NULL;
336 struct repo_commit *repo_commit = NULL;
337 struct server *srv = c->srv;
338 struct transport *t = c->t;
339 struct got_repository *repo = t->repo;
340 struct querystring *qs = t->qs;
341 struct repo_dir *repo_dir = t->repo_dir;
342 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
343 int chk_next = 0;
345 if (limit == 0)
346 return got_error(GOT_ERR_RANGE);
348 if (limit > 1) {
350 * Traverse one commit more than requested to provide
351 * the next button.
353 limit++;
354 chk_next = 1;
357 TAILQ_INIT(&refs);
359 if (qs->file != NULL && *qs->file != '\0')
360 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
361 qs->file) == -1)
362 return got_error_from_errno("asprintf");
364 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
365 repo_dir->name) == -1) {
366 error = got_error_from_errno("asprintf");
367 goto done;
370 if (qs->commit) {
371 error = got_repo_match_object_id_prefix(&id, qs->commit,
372 GOT_OBJ_TYPE_COMMIT, repo);
373 if (error)
374 goto done;
375 } else {
376 error = got_ref_open(&ref, repo, qs->headref, 0);
377 if (error)
378 goto done;
380 error = got_ref_resolve(&id, repo, ref);
381 if (error)
382 goto done;
385 error = got_repo_map_path(&in_repo_path, repo, repo_path);
386 if (error)
387 goto done;
389 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
390 if (error)
391 goto done;
393 if (qs->file != NULL && *qs->file != '\0') {
394 error = got_commit_graph_open(&graph, file_path, 0);
395 if (error)
396 goto done;
397 } else {
398 error = got_commit_graph_open(&graph, in_repo_path, 0);
399 if (error)
400 goto done;
403 error = got_commit_graph_bfsort(graph, id, repo, NULL, NULL);
404 if (error)
405 goto done;
407 for (;;) {
408 struct got_object_id next_id;
410 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
411 NULL);
412 if (error) {
413 if (error->code == GOT_ERR_ITER_COMPLETED)
414 error = NULL;
415 goto done;
418 error = got_object_open_as_commit(&commit, repo, &next_id);
419 if (error)
420 goto done;
422 error = got_init_repo_commit(&repo_commit);
423 if (error)
424 goto done;
426 error = got_get_repo_commit(c, repo_commit, commit,
427 &refs, &next_id);
428 if (error) {
429 gotweb_free_repo_commit(repo_commit);
430 goto done;
433 if (--limit == 0 && chk_next) {
434 t->more_id = strdup(repo_commit->commit_id);
435 if (t->more_id == NULL)
436 error = got_error_from_errno("strdup");
437 gotweb_free_repo_commit(repo_commit);
438 goto done;
441 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
443 if (limit == 0)
444 goto done;
446 if (commit) {
447 got_object_commit_close(commit);
448 commit = NULL;
451 done:
452 if (ref)
453 got_ref_close(ref);
454 if (commit)
455 got_object_commit_close(commit);
456 if (graph)
457 got_commit_graph_close(graph);
458 got_ref_list_free(&refs);
459 free(in_repo_path);
460 free(file_path);
461 free(repo_path);
462 free(id);
463 return error;
466 const struct got_error *
467 got_get_repo_tags(struct request *c, size_t limit)
469 const struct got_error *error = NULL;
470 struct got_object_id *id = NULL;
471 struct got_commit_object *commit = NULL;
472 struct got_reflist_head refs;
473 struct got_reference *ref;
474 struct got_reflist_entry *re;
475 struct server *srv = c->srv;
476 struct transport *t = c->t;
477 struct got_repository *repo = t->repo;
478 struct querystring *qs = t->qs;
479 struct repo_dir *repo_dir = t->repo_dir;
480 struct got_tag_object *tag = NULL;
481 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
482 char *tag_commit = NULL, *tag_commit0 = NULL;
483 char *commit_msg = NULL, *commit_msg0 = NULL;
484 int chk_next = 0, chk_multi = 1, commit_found = 0;
486 TAILQ_INIT(&refs);
488 if (limit == 0)
489 return got_error(GOT_ERR_RANGE);
491 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
492 repo_dir->name) == -1)
493 return got_error_from_errno("asprintf");
495 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
496 error = got_ref_open(&ref, repo, qs->headref, 0);
497 if (error)
498 goto done;
499 error = got_ref_resolve(&id, repo, ref);
500 got_ref_close(ref);
501 if (error)
502 goto done;
503 } else if (qs->commit == NULL && qs->action == TAG) {
504 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
505 goto done;
506 } else {
507 error = got_repo_match_object_id_prefix(&id, qs->commit,
508 GOT_OBJ_TYPE_COMMIT, repo);
509 if (error)
510 goto done;
513 if (qs->action != SUMMARY && qs->action != TAGS) {
514 error = got_object_open_as_commit(&commit, repo, id);
515 if (error)
516 goto done;
517 error = got_object_commit_get_logmsg(&commit_msg0, commit);
518 if (error)
519 goto done;
520 if (commit) {
521 got_object_commit_close(commit);
522 commit = NULL;
526 error = got_repo_map_path(&in_repo_path, repo, repo_path);
527 if (error)
528 goto done;
530 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
531 repo);
532 if (error)
533 goto done;
535 if (limit == 1)
536 chk_multi = 0;
538 TAILQ_FOREACH(re, &refs, entry) {
539 struct repo_tag *new_repo_tag = NULL;
540 error = got_init_repo_tag(&new_repo_tag);
541 if (error)
542 goto done;
544 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
546 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
547 if (new_repo_tag->tag_name == NULL) {
548 error = got_error_from_errno("strdup");
549 goto done;
552 free(id);
553 id = NULL;
555 free(id_str);
556 id_str = NULL;
558 error = got_ref_resolve(&id, repo, re->ref);
559 if (error)
560 goto done;
562 if (tag)
563 got_object_tag_close(tag);
564 error = got_object_open_as_tag(&tag, repo, id);
565 if (error) {
566 if (error->code != GOT_ERR_OBJ_TYPE)
567 goto done;
568 /* "lightweight" tag */
569 error = got_object_open_as_commit(&commit, repo, id);
570 if (error)
571 goto done;
572 new_repo_tag->tagger =
573 strdup(got_object_commit_get_committer(commit));
574 if (new_repo_tag->tagger == NULL) {
575 error = got_error_from_errno("strdup");
576 goto done;
578 new_repo_tag->tagger_time =
579 got_object_commit_get_committer_time(commit);
580 error = got_object_id_str(&id_str, id);
581 if (error)
582 goto done;
583 } else {
584 new_repo_tag->tagger =
585 strdup(got_object_tag_get_tagger(tag));
586 if (new_repo_tag->tagger == NULL) {
587 error = got_error_from_errno("strdup");
588 goto done;
590 new_repo_tag->tagger_time =
591 got_object_tag_get_tagger_time(tag);
592 error = got_object_id_str(&id_str,
593 got_object_tag_get_object_id(tag));
594 if (error)
595 goto done;
598 new_repo_tag->commit_id = strdup(id_str);
599 if (new_repo_tag->commit_id == NULL)
600 goto done;
602 if (commit_found == 0 && qs->commit != NULL &&
603 strncmp(id_str, qs->commit, strlen(id_str)) != 0) {
604 if (commit) {
605 got_object_commit_close(commit);
606 commit = NULL;
608 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
609 gotweb_free_repo_tag(new_repo_tag);
610 continue;
611 } else
612 commit_found = 1;
615 * check for one more commit before breaking,
616 * so we know whether to navigate through briefs
617 * commits and summary
619 if (chk_next) {
620 t->tags_more_id = strdup(new_repo_tag->commit_id);
621 if (t->tags_more_id == NULL) {
622 error = got_error_from_errno("strdup");
623 goto done;
625 if (commit) {
626 got_object_commit_close(commit);
627 commit = NULL;
629 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
630 gotweb_free_repo_tag(new_repo_tag);
631 goto done;
634 if (commit) {
635 error = got_object_commit_get_logmsg(&tag_commit0,
636 commit);
637 if (error)
638 goto done;
639 got_object_commit_close(commit);
640 commit = NULL;
641 } else {
642 tag_commit0 = strdup(got_object_tag_get_message(tag));
643 if (tag_commit0 == NULL) {
644 error = got_error_from_errno("strdup");
645 goto done;
649 tag_commit = tag_commit0;
650 while (*tag_commit == '\n')
651 tag_commit++;
652 new_repo_tag->tag_commit = strdup(tag_commit);
653 if (new_repo_tag->tag_commit == NULL) {
654 error = got_error_from_errno("strdup");
655 free(tag_commit0);
656 goto done;
658 free(tag_commit0);
660 if (qs->action != SUMMARY && qs->action != TAGS) {
661 commit_msg = commit_msg0;
662 while (*commit_msg == '\n')
663 commit_msg++;
665 new_repo_tag->commit_msg = strdup(commit_msg);
666 if (new_repo_tag->commit_msg == NULL) {
667 error = got_error_from_errno("strdup");
668 goto done;
672 if (limit && --limit == 0) {
673 if (chk_multi == 0)
674 break;
675 chk_next = 1;
679 done:
680 if (commit)
681 got_object_commit_close(commit);
682 if (tag)
683 got_object_tag_close(tag);
684 got_ref_list_free(&refs);
685 free(commit_msg0);
686 free(in_repo_path);
687 free(repo_path);
688 free(id);
689 free(id_str);
690 return error;
694 got_output_repo_tree(struct request *c, char **readme,
695 int (*cb)(struct template *, struct got_tree_entry *))
697 const struct got_error *error = NULL;
698 struct transport *t = c->t;
699 struct got_commit_object *commit = NULL;
700 struct got_repository *repo = t->repo;
701 struct querystring *qs = t->qs;
702 struct repo_commit *rc = NULL;
703 struct got_object_id *tree_id = NULL, *commit_id = NULL;
704 struct got_reflist_head refs;
705 struct got_tree_object *tree = NULL;
706 struct got_tree_entry *te;
707 struct repo_dir *repo_dir = t->repo_dir;
708 const char *name;
709 mode_t mode;
710 char *escaped_name = NULL, *path = NULL;
711 int nentries, i;
713 TAILQ_INIT(&refs);
714 *readme = NULL;
716 rc = TAILQ_FIRST(&t->repo_commits);
718 if (qs->folder != NULL) {
719 path = strdup(qs->folder);
720 if (path == NULL) {
721 error = got_error_from_errno("strdup");
722 goto done;
724 } else {
725 error = got_repo_map_path(&path, repo, repo_dir->path);
726 if (error)
727 goto done;
730 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
731 GOT_OBJ_TYPE_COMMIT, &refs, repo);
732 if (error)
733 goto done;
735 error = got_object_open_as_commit(&commit, repo, commit_id);
736 if (error)
737 goto done;
739 error = got_object_id_by_path(&tree_id, repo, commit, path);
740 if (error)
741 goto done;
743 error = got_object_open_as_tree(&tree, repo, tree_id);
744 if (error)
745 goto done;
747 nentries = got_object_tree_get_nentries(tree);
749 for (i = 0; i < nentries; i++) {
750 te = got_object_tree_get_entry(tree, i);
752 name = got_tree_entry_get_name(te);
753 mode = got_tree_entry_get_mode(te);
754 if (!S_ISDIR(mode) && (!strcasecmp(name, "README") ||
755 !strcasecmp(name, "README.md") ||
756 !strcasecmp(name, "README.txt"))) {
757 free(*readme);
758 *readme = strdup(name);
761 if (cb(c->tp, te) == -1) {
762 error = got_error(GOT_ERR_CANCELLED);
763 break;
766 done:
767 free(escaped_name);
768 free(path);
769 got_ref_list_free(&refs);
770 if (commit)
771 got_object_commit_close(commit);
772 if (tree)
773 got_object_tree_close(tree);
774 free(commit_id);
775 free(tree_id);
776 if (error) {
777 free(*readme);
778 *readme = NULL;
779 if (error->code != GOT_ERR_CANCELLED)
780 log_warnx("%s: %s", __func__, error->msg);
781 return -1;
783 return 0;
786 const struct got_error *
787 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
788 int *binary, struct request *c, const char *directory, const char *file,
789 const char *commitstr)
791 const struct got_error *error = NULL;
792 struct got_repository *repo = c->t->repo;
793 struct got_commit_object *commit = NULL;
794 struct got_object_id *commit_id = NULL;
795 struct got_object_id *blob_id = NULL;
796 struct got_reflist_head refs;
797 char *path = NULL, *in_repo_path = NULL;
798 int obj_type;
800 TAILQ_INIT(&refs);
802 *blob = NULL;
803 *fd = -1;
804 *binary = 0;
806 error = got_ref_list(&refs, repo, "refs/heads",
807 got_ref_cmp_by_name, NULL);
808 if (error)
809 goto done;
811 if (asprintf(&path, "%s%s%s", directory ? directory : "",
812 directory ? "/" : "", file) == -1) {
813 error = got_error_from_errno("asprintf");
814 goto done;
817 error = got_repo_map_path(&in_repo_path, repo, path);
818 if (error)
819 goto done;
821 if (commitstr == NULL)
822 commitstr = GOT_REF_HEAD;
824 error = got_repo_match_object_id(&commit_id, NULL, commitstr,
825 GOT_OBJ_TYPE_COMMIT, &refs, repo);
826 if (error)
827 goto done;
829 error = got_object_open_as_commit(&commit, repo, commit_id);
830 if (error)
831 goto done;
833 error = got_object_id_by_path(&blob_id, repo, commit, in_repo_path);
834 if (error)
835 goto done;
837 if (blob_id == NULL) {
838 error = got_error(GOT_ERR_NO_OBJ);
839 goto done;
842 error = got_object_get_type(&obj_type, repo, blob_id);
843 if (error)
844 goto done;
846 if (obj_type != GOT_OBJ_TYPE_BLOB) {
847 error = got_error(GOT_ERR_OBJ_TYPE);
848 goto done;
851 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
852 if (error)
853 goto done;
855 error = got_object_open_as_blob(blob, repo, blob_id, BUF, *fd);
856 if (error)
857 goto done;
859 error = got_object_blob_is_binary(binary, *blob);
860 if (error)
861 goto done;
863 done:
864 if (commit)
865 got_object_commit_close(commit);
867 if (error) {
868 if (*fd != -1)
869 close(*fd);
870 if (*blob)
871 got_object_blob_close(*blob);
872 *fd = -1;
873 *blob = NULL;
876 got_ref_list_free(&refs);
877 free(in_repo_path);
878 free(commit_id);
879 free(blob_id);
880 free(path);
881 return error;
885 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
886 int (*cb)(struct template *, const char *, size_t))
888 const struct got_error *err;
889 char *line = NULL;
890 size_t linesize = 0;
891 size_t lineno = 0;
892 ssize_t linelen = 0;
894 for (;;) {
895 err = got_object_blob_getline(&line, &linelen, &linesize,
896 blob);
897 if (err || linelen == -1)
898 break;
899 lineno++;
900 if (cb(tp, line, lineno) == -1) {
901 err = got_error(GOT_ERR_CANCELLED);
902 break;
906 free(line);
908 if (err) {
909 if (err->code != GOT_ERR_CANCELLED)
910 log_warnx("%s: got_object_blob_getline failed: %s",
911 __func__, err->msg);
912 return -1;
914 return 0;
917 struct blame_cb_args {
918 struct blame_line *lines;
919 int nlines;
920 int nlines_prec;
921 int lineno_cur;
922 off_t *line_offsets;
923 FILE *f;
924 struct got_repository *repo;
925 struct request *c;
926 got_render_blame_line_cb cb;
929 static const struct got_error *
930 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
931 struct got_commit_object *commit, struct got_object_id *id)
933 const struct got_error *err = NULL;
934 struct blame_cb_args *a = arg;
935 struct blame_line *bline;
936 struct request *c = a->c;
937 char *line = NULL;
938 size_t linesize = 0;
939 off_t offset;
940 struct tm tm;
941 time_t committer_time;
943 if (nlines != a->nlines ||
944 (lineno != -1 && lineno < 1) || lineno > a->nlines)
945 return got_error(GOT_ERR_RANGE);
947 if (lineno == -1)
948 return NULL; /* no change in this commit */
950 /* Annotate this line. */
951 bline = &a->lines[lineno - 1];
952 if (bline->annotated)
953 return NULL;
954 err = got_object_id_str(&bline->id_str, id);
955 if (err)
956 return err;
958 bline->committer = strdup(got_object_commit_get_committer(commit));
959 if (bline->committer == NULL) {
960 err = got_error_from_errno("strdup");
961 goto done;
964 committer_time = got_object_commit_get_committer_time(commit);
965 if (gmtime_r(&committer_time, &tm) == NULL)
966 return got_error_from_errno("gmtime_r");
967 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%F", &tm) == 0) {
968 err = got_error(GOT_ERR_NO_SPACE);
969 goto done;
971 bline->annotated = 1;
973 /* Print lines annotated so far. */
974 bline = &a->lines[a->lineno_cur - 1];
975 if (!bline->annotated)
976 goto done;
978 offset = a->line_offsets[a->lineno_cur - 1];
979 if (fseeko(a->f, offset, SEEK_SET) == -1) {
980 err = got_error_from_errno("fseeko");
981 goto done;
984 while (a->lineno_cur <= a->nlines && bline->annotated) {
985 if (getline(&line, &linesize, a->f) == -1) {
986 if (ferror(a->f))
987 err = got_error_from_errno("getline");
988 break;
991 if (a->cb(c->tp, line, bline, a->nlines_prec,
992 a->lineno_cur) == -1) {
993 err = got_error(GOT_ERR_CANCELLED);
994 break;
997 a->lineno_cur++;
998 bline = &a->lines[a->lineno_cur - 1];
1000 done:
1001 free(line);
1002 return err;
1005 const struct got_error *
1006 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1008 const struct got_error *error = NULL;
1009 struct transport *t = c->t;
1010 struct got_repository *repo = t->repo;
1011 struct querystring *qs = c->t->qs;
1012 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1013 struct got_commit_object *commit = NULL;
1014 struct got_reflist_head refs;
1015 struct got_blob_object *blob = NULL;
1016 char *path = NULL, *in_repo_path = NULL;
1017 struct blame_cb_args bca;
1018 int i, obj_type, blobfd = -1, fd1 = -1, fd2 = -1;
1019 off_t filesize;
1020 FILE *f1 = NULL, *f2 = NULL;
1022 TAILQ_INIT(&refs);
1023 bca.f = NULL;
1024 bca.lines = NULL;
1025 bca.cb = cb;
1027 if (asprintf(&path, "%s/%s", qs->folder, qs->file) == -1) {
1028 error = got_error_from_errno("asprintf");
1029 goto done;
1032 error = got_repo_map_path(&in_repo_path, repo, path);
1033 if (error)
1034 goto done;
1036 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1037 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1038 if (error)
1039 goto done;
1041 error = got_object_open_as_commit(&commit, repo, commit_id);
1042 if (error)
1043 goto done;
1045 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1046 if (error)
1047 goto done;
1049 error = got_object_get_type(&obj_type, repo, obj_id);
1050 if (error)
1051 goto done;
1053 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1054 error = got_error(GOT_ERR_OBJ_TYPE);
1055 goto done;
1058 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1]);
1059 if (error)
1060 goto done;
1062 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &blobfd);
1063 if (error)
1064 goto done;
1066 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, blobfd);
1067 if (error)
1068 goto done;
1070 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1071 &bca.line_offsets, bca.f, blob);
1072 if (error || bca.nlines == 0)
1073 goto done;
1075 /* Don't include \n at EOF in the blame line count. */
1076 if (bca.line_offsets[bca.nlines - 1] == filesize)
1077 bca.nlines--;
1079 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1080 if (bca.lines == NULL) {
1081 error = got_error_from_errno("calloc");
1082 goto done;
1084 bca.lineno_cur = 1;
1085 bca.nlines_prec = 0;
1086 i = bca.nlines;
1087 while (i > 0) {
1088 i /= 10;
1089 bca.nlines_prec++;
1091 bca.repo = repo;
1092 bca.c = c;
1094 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd1);
1095 if (error)
1096 goto done;
1098 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd2);
1099 if (error)
1100 goto done;
1102 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5]);
1103 if (error)
1104 goto done;
1106 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6]);
1107 if (error)
1108 goto done;
1110 error = got_blame(in_repo_path, commit_id, repo,
1111 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1112 fd1, fd2, f1, f2);
1114 done:
1115 if (bca.lines) {
1116 free(bca.line_offsets);
1117 for (i = 0; i < bca.nlines; i++) {
1118 struct blame_line *bline = &bca.lines[i];
1119 free(bline->id_str);
1120 free(bline->committer);
1123 free(bca.lines);
1124 if (blobfd != -1 && close(blobfd) == -1 && error == NULL)
1125 error = got_error_from_errno("close");
1126 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1127 error = got_error_from_errno("close");
1128 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1129 error = got_error_from_errno("close");
1130 if (bca.f) {
1131 const struct got_error *bca_err = got_gotweb_closefile(bca.f);
1132 if (error == NULL)
1133 error = bca_err;
1135 if (f1) {
1136 const struct got_error *f1_err = got_gotweb_closefile(f1);
1137 if (error == NULL)
1138 error = f1_err;
1140 if (f2) {
1141 const struct got_error *f2_err = got_gotweb_closefile(f2);
1142 if (error == NULL)
1143 error = f2_err;
1145 if (commit)
1146 got_object_commit_close(commit);
1147 if (blob)
1148 got_object_blob_close(blob);
1149 free(in_repo_path);
1150 free(commit_id);
1151 free(obj_id);
1152 free(path);
1153 got_ref_list_free(&refs);
1154 return error;
1157 const struct got_error *
1158 got_open_diff_for_output(FILE **fp, struct request *c)
1160 const struct got_error *error = NULL;
1161 struct transport *t = c->t;
1162 struct got_repository *repo = t->repo;
1163 struct repo_commit *rc = NULL;
1164 struct got_object_id *id1 = NULL, *id2 = NULL;
1165 struct got_reflist_head refs;
1166 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1167 int obj_type, fd1 = -1, fd2 = -1;
1169 *fp = NULL;
1171 TAILQ_INIT(&refs);
1173 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1]);
1174 if (error)
1175 goto done;
1177 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2]);
1178 if (error)
1179 goto done;
1181 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3]);
1182 if (error)
1183 goto done;
1185 rc = TAILQ_FIRST(&t->repo_commits);
1187 if (rc->parent_id != NULL &&
1188 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1189 error = got_repo_match_object_id(&id1, NULL,
1190 rc->parent_id, GOT_OBJ_TYPE_ANY,
1191 &refs, repo);
1192 if (error)
1193 goto done;
1196 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1197 GOT_OBJ_TYPE_ANY, &refs, repo);
1198 if (error)
1199 goto done;
1201 error = got_object_get_type(&obj_type, repo, id2);
1202 if (error)
1203 goto done;
1205 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd1);
1206 if (error)
1207 goto done;
1209 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd2);
1210 if (error)
1211 goto done;
1213 switch (obj_type) {
1214 case GOT_OBJ_TYPE_BLOB:
1215 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd1, fd2,
1216 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1217 NULL, repo, f3);
1218 break;
1219 case GOT_OBJ_TYPE_TREE:
1220 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
1221 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1222 NULL, repo, f3);
1223 break;
1224 case GOT_OBJ_TYPE_COMMIT:
1225 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd1,
1226 fd2, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1227 NULL, repo, f3);
1228 break;
1229 default:
1230 error = got_error(GOT_ERR_OBJ_TYPE);
1232 if (error)
1233 goto done;
1235 if (fseek(f3, 0, SEEK_SET) == -1) {
1236 error = got_ferror(f3, GOT_ERR_IO);
1237 goto done;
1240 *fp = f3;
1242 done:
1243 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1244 error = got_error_from_errno("close");
1245 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1246 error = got_error_from_errno("close");
1247 if (f1) {
1248 const struct got_error *f1_err = got_gotweb_closefile(f1);
1249 if (error == NULL)
1250 error = f1_err;
1252 if (f2) {
1253 const struct got_error *f2_err = got_gotweb_closefile(f2);
1254 if (error == NULL)
1255 error = f2_err;
1257 if (error && f3) {
1258 got_gotweb_closefile(f3);
1259 *fp = NULL;
1261 got_ref_list_free(&refs);
1262 free(id1);
1263 free(id2);
1264 return error;
1267 static const struct got_error *
1268 got_init_repo_commit(struct repo_commit **rc)
1270 *rc = calloc(1, sizeof(**rc));
1271 if (*rc == NULL)
1272 return got_error_from_errno2(__func__, "calloc");
1274 (*rc)->path = NULL;
1275 (*rc)->refs_str = NULL;
1276 (*rc)->commit_id = NULL;
1277 (*rc)->committer = NULL;
1278 (*rc)->author = NULL;
1279 (*rc)->parent_id = NULL;
1280 (*rc)->tree_id = NULL;
1281 (*rc)->commit_msg = NULL;
1283 return NULL;
1286 static const struct got_error *
1287 got_init_repo_tag(struct repo_tag **rt)
1289 *rt = calloc(1, sizeof(**rt));
1290 if (*rt == NULL)
1291 return got_error_from_errno2(__func__, "calloc");
1293 (*rt)->commit_id = NULL;
1294 (*rt)->tag_name = NULL;
1295 (*rt)->tag_commit = NULL;
1296 (*rt)->commit_msg = NULL;
1297 (*rt)->tagger = NULL;
1299 return NULL;