portable: set next version
[got-portable.git] / gotwebd / got_operations.c
blobc70d5d19072df945e2dd7888a3a26d1a4e07b468
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 "gotwebd.h"
43 #include "log.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 (ftruncate(fileno(f), 0) == -1 && err == NULL)
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 transport *t = c->t;
105 struct got_repository *repo = t->repo;
106 const char *gitconfig_owner;
108 *owner = NULL;
109 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
110 if (gitconfig_owner) {
111 *owner = strdup(gitconfig_owner);
112 if (*owner == NULL)
113 return got_error_from_errno("strdup");
116 return NULL;
119 const struct got_error *
120 got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
122 const struct got_error *error = NULL;
123 struct transport *t = c->t;
124 struct got_repository *repo = t->repo;
125 struct got_commit_object *commit = NULL;
126 struct got_reflist_head refs;
127 struct got_reflist_entry *re;
128 time_t committer_time = 0, cmp_time = 0;
130 TAILQ_INIT(&refs);
132 *repo_age = 0;
134 error = got_ref_list(&refs, repo, "refs/heads",
135 got_ref_cmp_by_name, NULL);
136 if (error)
137 goto done;
140 * Find the youngest branch tip in the repository, or the age of
141 * the a specific branch tip if a name was provided by the caller.
143 TAILQ_FOREACH(re, &refs, entry) {
144 struct got_object_id *id = NULL;
146 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
147 continue;
149 error = got_ref_resolve(&id, repo, re->ref);
150 if (error)
151 goto done;
153 error = got_object_open_as_commit(&commit, repo, id);
154 free(id);
155 if (error)
156 goto done;
158 committer_time =
159 got_object_commit_get_committer_time(commit);
160 got_object_commit_close(commit);
161 if (cmp_time < committer_time)
162 cmp_time = committer_time;
164 if (refname)
165 break;
168 if (cmp_time != 0)
169 *repo_age = cmp_time;
170 done:
171 got_ref_list_free(&refs);
172 return error;
175 static const struct got_error *
176 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
177 struct got_commit_object *commit, struct got_reflist_head *refs,
178 struct got_object_id *id)
180 const struct got_error *error = NULL;
181 struct got_reflist_entry *re;
182 struct got_object_id *id2 = NULL;
183 struct got_object_qid *parent_id;
184 struct transport *t = c->t;
185 struct querystring *qs = c->t->qs;
186 char *commit_msg = NULL, *commit_msg0;
188 TAILQ_FOREACH(re, refs, entry) {
189 char *s;
190 const char *name;
191 struct got_tag_object *tag = NULL;
192 struct got_object_id *ref_id;
193 int cmp;
195 if (got_ref_is_symbolic(re->ref))
196 continue;
198 name = got_ref_get_name(re->ref);
199 if (strncmp(name, "refs/", 5) == 0)
200 name += 5;
201 if (strncmp(name, "got/", 4) == 0)
202 continue;
203 if (strncmp(name, "heads/", 6) == 0)
204 name += 6;
205 if (strncmp(name, "remotes/", 8) == 0) {
206 name += 8;
207 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
208 continue;
210 error = got_ref_resolve(&ref_id, t->repo, re->ref);
211 if (error)
212 return error;
213 if (strncmp(name, "tags/", 5) == 0) {
214 error = got_object_open_as_tag(&tag, t->repo, ref_id);
215 if (error) {
216 if (error->code != GOT_ERR_OBJ_TYPE) {
217 free(ref_id);
218 continue;
221 * Ref points at something other
222 * than a tag.
224 error = NULL;
225 tag = NULL;
228 cmp = got_object_id_cmp(tag ?
229 got_object_tag_get_object_id(tag) : ref_id, id);
230 free(ref_id);
231 if (tag)
232 got_object_tag_close(tag);
233 if (cmp != 0)
234 continue;
235 s = repo_commit->refs_str;
236 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
237 s ? ", " : "", name) == -1) {
238 error = got_error_from_errno("asprintf");
239 free(s);
240 repo_commit->refs_str = NULL;
241 return error;
243 free(s);
246 error = got_object_id_str(&repo_commit->commit_id, id);
247 if (error)
248 return error;
250 error = got_object_id_str(&repo_commit->tree_id,
251 got_object_commit_get_tree_id(commit));
252 if (error)
253 return error;
255 if (qs->action == DIFF || qs->action == PATCH) {
256 parent_id = STAILQ_FIRST(
257 got_object_commit_get_parent_ids(commit));
258 if (parent_id != NULL) {
259 id2 = got_object_id_dup(&parent_id->id);
260 error = got_object_id_str(&repo_commit->parent_id, id2);
261 if (error)
262 return error;
263 free(id2);
264 } else {
265 repo_commit->parent_id = strdup("/dev/null");
266 if (repo_commit->parent_id == NULL) {
267 error = got_error_from_errno("strdup");
268 return error;
273 repo_commit->committer_time =
274 got_object_commit_get_committer_time(commit);
276 repo_commit->author =
277 strdup(got_object_commit_get_author(commit));
278 if (repo_commit->author == NULL) {
279 error = got_error_from_errno("strdup");
280 return error;
282 repo_commit->committer =
283 strdup(got_object_commit_get_committer(commit));
284 if (repo_commit->committer == NULL) {
285 error = got_error_from_errno("strdup");
286 return error;
288 error = got_object_commit_get_logmsg(&commit_msg0, commit);
289 if (error)
290 return error;
292 commit_msg = commit_msg0;
293 while (*commit_msg == '\n')
294 commit_msg++;
296 repo_commit->commit_msg = strdup(commit_msg);
297 if (repo_commit->commit_msg == NULL)
298 error = got_error_from_errno("strdup");
299 free(commit_msg0);
300 return error;
303 const struct got_error *
304 got_get_repo_commits(struct request *c, size_t limit)
306 const struct got_error *error = NULL;
307 struct got_object_id *id = NULL;
308 struct got_commit_graph *graph = NULL;
309 struct got_commit_object *commit = NULL;
310 struct got_reflist_head refs;
311 struct got_reference *ref = NULL;
312 struct repo_commit *repo_commit = NULL;
313 struct server *srv = c->srv;
314 struct transport *t = c->t;
315 struct got_repository *repo = t->repo;
316 struct querystring *qs = t->qs;
317 struct repo_dir *repo_dir = t->repo_dir;
318 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
319 int chk_next = 0;
321 if (limit == 0)
322 return got_error(GOT_ERR_RANGE);
324 if (limit > 1) {
326 * Traverse one commit more than requested to provide
327 * the next button.
329 limit++;
330 chk_next = 1;
333 TAILQ_INIT(&refs);
335 if (qs->file != NULL && *qs->file != '\0')
336 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
337 qs->file) == -1)
338 return got_error_from_errno("asprintf");
340 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
341 repo_dir->name) == -1) {
342 error = got_error_from_errno("asprintf");
343 goto done;
346 if (qs->commit) {
347 error = got_repo_match_object_id_prefix(&id, qs->commit,
348 GOT_OBJ_TYPE_COMMIT, repo);
349 if (error)
350 goto done;
351 } else {
352 error = got_ref_open(&ref, repo, qs->headref, 0);
353 if (error)
354 goto done;
356 error = got_ref_resolve(&id, repo, ref);
357 if (error)
358 goto done;
361 error = got_repo_map_path(&in_repo_path, repo, repo_path);
362 if (error)
363 goto done;
365 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
366 if (error)
367 goto done;
369 if (qs->file != NULL && *qs->file != '\0') {
370 error = got_commit_graph_open(&graph, file_path, 0);
371 if (error)
372 goto done;
373 } else {
374 error = got_commit_graph_open(&graph, in_repo_path, 0);
375 if (error)
376 goto done;
379 error = got_commit_graph_bfsort(graph, id, repo, NULL, NULL);
380 if (error)
381 goto done;
383 for (;;) {
384 struct got_object_id next_id;
386 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
387 NULL);
388 if (error) {
389 if (error->code == GOT_ERR_ITER_COMPLETED)
390 error = NULL;
391 goto done;
394 error = got_object_open_as_commit(&commit, repo, &next_id);
395 if (error)
396 goto done;
398 error = got_init_repo_commit(&repo_commit);
399 if (error)
400 goto done;
402 error = got_get_repo_commit(c, repo_commit, commit,
403 &refs, &next_id);
404 if (error) {
405 gotweb_free_repo_commit(repo_commit);
406 goto done;
409 if (--limit == 0 && chk_next) {
410 t->more_id = strdup(repo_commit->commit_id);
411 if (t->more_id == NULL)
412 error = got_error_from_errno("strdup");
413 goto done;
416 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
418 if (limit == 0)
419 goto done;
421 if (commit) {
422 got_object_commit_close(commit);
423 commit = NULL;
426 done:
427 if (ref)
428 got_ref_close(ref);
429 if (commit)
430 got_object_commit_close(commit);
431 if (graph)
432 got_commit_graph_close(graph);
433 got_ref_list_free(&refs);
434 free(in_repo_path);
435 free(file_path);
436 free(repo_path);
437 free(id);
438 return error;
441 const struct got_error *
442 got_get_repo_tags(struct request *c, size_t limit)
444 const struct got_error *error = NULL;
445 struct got_object_id *id = NULL;
446 struct got_commit_object *commit = NULL;
447 struct got_reflist_head refs;
448 struct got_reference *ref;
449 struct got_reflist_entry *re;
450 struct server *srv = c->srv;
451 struct transport *t = c->t;
452 struct got_repository *repo = t->repo;
453 struct querystring *qs = t->qs;
454 struct repo_dir *repo_dir = t->repo_dir;
455 struct got_tag_object *tag = NULL;
456 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
457 char *tag_commit = NULL, *tag_commit0 = NULL;
458 char *commit_msg = NULL, *commit_msg0 = NULL;
459 int chk_next = 0, chk_multi = 1, commit_found = 0;
461 TAILQ_INIT(&refs);
463 if (limit == 0)
464 return got_error(GOT_ERR_RANGE);
466 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
467 repo_dir->name) == -1)
468 return got_error_from_errno("asprintf");
470 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
471 error = got_ref_open(&ref, repo, qs->headref, 0);
472 if (error)
473 goto done;
474 error = got_ref_resolve(&id, repo, ref);
475 got_ref_close(ref);
476 if (error)
477 goto done;
478 } else if (qs->commit == NULL && qs->action == TAG) {
479 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
480 goto done;
481 } else {
482 error = got_repo_match_object_id_prefix(&id, qs->commit,
483 GOT_OBJ_TYPE_COMMIT, repo);
484 if (error)
485 goto done;
488 if (qs->action != SUMMARY && qs->action != TAGS) {
489 error = got_object_open_as_commit(&commit, repo, id);
490 if (error)
491 goto done;
492 error = got_object_commit_get_logmsg(&commit_msg0, commit);
493 if (error)
494 goto done;
495 if (commit) {
496 got_object_commit_close(commit);
497 commit = NULL;
501 error = got_repo_map_path(&in_repo_path, repo, repo_path);
502 if (error)
503 goto done;
505 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
506 repo);
507 if (error)
508 goto done;
510 if (limit == 1)
511 chk_multi = 0;
513 TAILQ_FOREACH(re, &refs, entry) {
514 struct repo_tag *new_repo_tag = NULL;
515 error = got_init_repo_tag(&new_repo_tag);
516 if (error)
517 goto done;
519 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
521 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
522 if (new_repo_tag->tag_name == NULL) {
523 error = got_error_from_errno("strdup");
524 goto done;
527 free(id);
528 id = NULL;
530 free(id_str);
531 id_str = NULL;
533 error = got_ref_resolve(&id, repo, re->ref);
534 if (error)
535 goto done;
537 if (tag)
538 got_object_tag_close(tag);
539 error = got_object_open_as_tag(&tag, repo, id);
540 if (error) {
541 if (error->code != GOT_ERR_OBJ_TYPE)
542 goto done;
543 /* "lightweight" tag */
544 error = got_object_open_as_commit(&commit, repo, id);
545 if (error)
546 goto done;
547 new_repo_tag->tagger =
548 strdup(got_object_commit_get_committer(commit));
549 if (new_repo_tag->tagger == NULL) {
550 error = got_error_from_errno("strdup");
551 goto done;
553 new_repo_tag->tagger_time =
554 got_object_commit_get_committer_time(commit);
555 error = got_object_id_str(&id_str, id);
556 if (error)
557 goto done;
558 } else {
559 new_repo_tag->tagger =
560 strdup(got_object_tag_get_tagger(tag));
561 if (new_repo_tag->tagger == NULL) {
562 error = got_error_from_errno("strdup");
563 goto done;
565 new_repo_tag->tagger_time =
566 got_object_tag_get_tagger_time(tag);
567 error = got_object_id_str(&id_str,
568 got_object_tag_get_object_id(tag));
569 if (error)
570 goto done;
573 new_repo_tag->commit_id = strdup(id_str);
574 if (new_repo_tag->commit_id == NULL)
575 goto done;
577 if (commit_found == 0 && qs->commit != NULL &&
578 strncmp(id_str, qs->commit, strlen(id_str)) != 0) {
579 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
580 gotweb_free_repo_tag(new_repo_tag);
581 continue;
582 } else
583 commit_found = 1;
586 * check for one more commit before breaking,
587 * so we know whether to navigate through briefs
588 * commits and summary
590 if (chk_next) {
591 t->tags_more_id = strdup(new_repo_tag->commit_id);
592 if (t->tags_more_id == NULL) {
593 error = got_error_from_errno("strdup");
594 goto done;
596 if (commit) {
597 got_object_commit_close(commit);
598 commit = NULL;
600 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
601 gotweb_free_repo_tag(new_repo_tag);
602 goto done;
605 if (commit) {
606 error = got_object_commit_get_logmsg(&tag_commit0,
607 commit);
608 if (error)
609 goto done;
610 got_object_commit_close(commit);
611 commit = NULL;
612 } else {
613 tag_commit0 = strdup(got_object_tag_get_message(tag));
614 if (tag_commit0 == NULL) {
615 error = got_error_from_errno("strdup");
616 goto done;
620 tag_commit = tag_commit0;
621 while (*tag_commit == '\n')
622 tag_commit++;
623 new_repo_tag->tag_commit = strdup(tag_commit);
624 if (new_repo_tag->tag_commit == NULL) {
625 error = got_error_from_errno("strdup");
626 free(tag_commit0);
627 goto done;
629 free(tag_commit0);
631 if (qs->action != SUMMARY && qs->action != TAGS) {
632 commit_msg = commit_msg0;
633 while (*commit_msg == '\n')
634 commit_msg++;
636 new_repo_tag->commit_msg = strdup(commit_msg);
637 if (new_repo_tag->commit_msg == NULL) {
638 error = got_error_from_errno("strdup");
639 goto done;
643 if (limit && --limit == 0) {
644 if (chk_multi == 0)
645 break;
646 chk_next = 1;
650 done:
651 if (commit)
652 got_object_commit_close(commit);
653 if (tag)
654 got_object_tag_close(tag);
655 got_ref_list_free(&refs);
656 free(commit_msg0);
657 free(in_repo_path);
658 free(repo_path);
659 free(id);
660 free(id_str);
661 return error;
665 got_output_repo_tree(struct request *c, char **readme,
666 int (*cb)(struct template *, struct got_tree_entry *))
668 const struct got_error *error = NULL;
669 struct transport *t = c->t;
670 struct got_commit_object *commit = NULL;
671 struct got_repository *repo = t->repo;
672 struct querystring *qs = t->qs;
673 struct repo_commit *rc = NULL;
674 struct got_object_id *tree_id = NULL, *commit_id = NULL;
675 struct got_reflist_head refs;
676 struct got_tree_object *tree = NULL;
677 struct got_tree_entry *te;
678 struct repo_dir *repo_dir = t->repo_dir;
679 const char *name;
680 mode_t mode;
681 char *escaped_name = NULL, *path = NULL;
682 int nentries, i;
684 TAILQ_INIT(&refs);
685 *readme = NULL;
687 rc = TAILQ_FIRST(&t->repo_commits);
689 if (qs->folder != NULL) {
690 path = strdup(qs->folder);
691 if (path == NULL) {
692 error = got_error_from_errno("strdup");
693 goto done;
695 } else {
696 error = got_repo_map_path(&path, repo, repo_dir->path);
697 if (error)
698 goto done;
701 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
702 GOT_OBJ_TYPE_COMMIT, &refs, repo);
703 if (error)
704 goto done;
706 error = got_object_open_as_commit(&commit, repo, commit_id);
707 if (error)
708 goto done;
710 error = got_object_id_by_path(&tree_id, repo, commit, path);
711 if (error)
712 goto done;
714 error = got_object_open_as_tree(&tree, repo, tree_id);
715 if (error)
716 goto done;
718 nentries = got_object_tree_get_nentries(tree);
720 for (i = 0; i < nentries; i++) {
721 te = got_object_tree_get_entry(tree, i);
723 name = got_tree_entry_get_name(te);
724 mode = got_tree_entry_get_mode(te);
725 if (!S_ISDIR(mode) && (!strcasecmp(name, "README") ||
726 !strcasecmp(name, "README.md") ||
727 !strcasecmp(name, "README.txt"))) {
728 free(*readme);
729 *readme = strdup(name);
732 if (cb(c->tp, te) == -1) {
733 error = got_error(GOT_ERR_CANCELLED);
734 break;
737 done:
738 free(escaped_name);
739 free(path);
740 got_ref_list_free(&refs);
741 if (commit)
742 got_object_commit_close(commit);
743 if (tree)
744 got_object_tree_close(tree);
745 free(commit_id);
746 free(tree_id);
747 if (error) {
748 free(*readme);
749 *readme = NULL;
750 if (error->code != GOT_ERR_CANCELLED)
751 log_warnx("%s: %s", __func__, error->msg);
752 return -1;
754 return 0;
757 const struct got_error *
758 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
759 int *binary, struct request *c, const char *directory, const char *file,
760 const char *commitstr)
762 const struct got_error *error = NULL;
763 struct got_repository *repo = c->t->repo;
764 struct got_commit_object *commit = NULL;
765 struct got_object_id *commit_id = NULL;
766 struct got_reflist_head refs;
767 char *path = NULL, *in_repo_path = NULL;
768 int obj_type;
770 TAILQ_INIT(&refs);
772 *blob = NULL;
773 *fd = -1;
774 *binary = 0;
776 error = got_ref_list(&refs, repo, "refs/heads",
777 got_ref_cmp_by_name, NULL);
778 if (error)
779 goto done;
781 if (asprintf(&path, "%s%s%s", directory ? directory : "",
782 directory ? "/" : "", file) == -1) {
783 error = got_error_from_errno("asprintf");
784 goto done;
787 error = got_repo_map_path(&in_repo_path, repo, path);
788 if (error)
789 goto done;
791 if (commitstr == NULL)
792 commitstr = GOT_REF_HEAD;
794 error = got_repo_match_object_id(&commit_id, NULL, commitstr,
795 GOT_OBJ_TYPE_COMMIT, &refs, repo);
796 if (error)
797 goto done;
799 error = got_object_open_as_commit(&commit, repo, commit_id);
800 if (error)
801 goto done;
803 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
804 if (error)
805 goto done;
807 if (commit_id == NULL) {
808 error = got_error(GOT_ERR_NO_OBJ);
809 goto done;
812 error = got_object_get_type(&obj_type, repo, commit_id);
813 if (error)
814 goto done;
816 if (obj_type != GOT_OBJ_TYPE_BLOB) {
817 error = got_error(GOT_ERR_OBJ_TYPE);
818 goto done;
821 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
822 if (error)
823 goto done;
825 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
826 if (error)
827 goto done;
829 error = got_object_blob_is_binary(binary, *blob);
830 if (error)
831 goto done;
833 done:
834 if (commit)
835 got_object_commit_close(commit);
837 if (error) {
838 if (*fd != -1)
839 close(*fd);
840 if (*blob)
841 got_object_blob_close(*blob);
842 *fd = -1;
843 *blob = NULL;
846 got_ref_list_free(&refs);
847 free(in_repo_path);
848 free(commit_id);
849 free(path);
850 return error;
854 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
855 int (*cb)(struct template *, const char *, size_t))
857 const struct got_error *err;
858 char *line = NULL;
859 size_t linesize = 0;
860 size_t lineno = 0;
861 ssize_t linelen = 0;
863 for (;;) {
864 err = got_object_blob_getline(&line, &linelen, &linesize,
865 blob);
866 if (err || linelen == -1)
867 break;
868 lineno++;
869 if (cb(tp, line, lineno) == -1) {
870 err = got_error(GOT_ERR_CANCELLED);
871 break;
875 free(line);
877 if (err) {
878 if (err->code != GOT_ERR_CANCELLED)
879 log_warnx("%s: got_object_blob_getline failed: %s",
880 __func__, err->msg);
881 return -1;
883 return 0;
886 struct blame_cb_args {
887 struct blame_line *lines;
888 int nlines;
889 int nlines_prec;
890 int lineno_cur;
891 off_t *line_offsets;
892 FILE *f;
893 struct got_repository *repo;
894 struct request *c;
895 got_render_blame_line_cb cb;
898 static const struct got_error *
899 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
900 struct got_commit_object *commit, struct got_object_id *id)
902 const struct got_error *err = NULL;
903 struct blame_cb_args *a = arg;
904 struct blame_line *bline;
905 struct request *c = a->c;
906 char *line = NULL;
907 size_t linesize = 0;
908 off_t offset;
909 struct tm tm;
910 time_t committer_time;
912 if (nlines != a->nlines ||
913 (lineno != -1 && lineno < 1) || lineno > a->nlines)
914 return got_error(GOT_ERR_RANGE);
916 if (lineno == -1)
917 return NULL; /* no change in this commit */
919 /* Annotate this line. */
920 bline = &a->lines[lineno - 1];
921 if (bline->annotated)
922 return NULL;
923 err = got_object_id_str(&bline->id_str, id);
924 if (err)
925 return err;
927 bline->committer = strdup(got_object_commit_get_committer(commit));
928 if (bline->committer == NULL) {
929 err = got_error_from_errno("strdup");
930 goto done;
933 committer_time = got_object_commit_get_committer_time(commit);
934 if (gmtime_r(&committer_time, &tm) == NULL)
935 return got_error_from_errno("gmtime_r");
936 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%F", &tm) == 0) {
937 err = got_error(GOT_ERR_NO_SPACE);
938 goto done;
940 bline->annotated = 1;
942 /* Print lines annotated so far. */
943 bline = &a->lines[a->lineno_cur - 1];
944 if (!bline->annotated)
945 goto done;
947 offset = a->line_offsets[a->lineno_cur - 1];
948 if (fseeko(a->f, offset, SEEK_SET) == -1) {
949 err = got_error_from_errno("fseeko");
950 goto done;
953 while (a->lineno_cur <= a->nlines && bline->annotated) {
954 if (getline(&line, &linesize, a->f) == -1) {
955 if (ferror(a->f))
956 err = got_error_from_errno("getline");
957 break;
960 if (a->cb(c->tp, line, bline, a->nlines_prec,
961 a->lineno_cur) == -1) {
962 err = got_error(GOT_ERR_CANCELLED);
963 break;
966 a->lineno_cur++;
967 bline = &a->lines[a->lineno_cur - 1];
969 done:
970 free(line);
971 return err;
974 const struct got_error *
975 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
977 const struct got_error *error = NULL;
978 struct transport *t = c->t;
979 struct got_repository *repo = t->repo;
980 struct querystring *qs = c->t->qs;
981 struct got_object_id *obj_id = NULL, *commit_id = NULL;
982 struct got_commit_object *commit = NULL;
983 struct got_reflist_head refs;
984 struct got_blob_object *blob = NULL;
985 char *path = NULL, *in_repo_path = NULL;
986 struct blame_cb_args bca;
987 int i, obj_type, blobfd = -1, fd1 = -1, fd2 = -1;
988 off_t filesize;
989 FILE *f1 = NULL, *f2 = NULL;
991 TAILQ_INIT(&refs);
992 bca.f = NULL;
993 bca.lines = NULL;
994 bca.cb = cb;
996 if (asprintf(&path, "%s/%s", qs->folder, qs->file) == -1) {
997 error = got_error_from_errno("asprintf");
998 goto done;
1001 error = got_repo_map_path(&in_repo_path, repo, path);
1002 if (error)
1003 goto done;
1005 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1006 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1007 if (error)
1008 goto done;
1010 error = got_object_open_as_commit(&commit, repo, commit_id);
1011 if (error)
1012 goto done;
1014 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1015 if (error)
1016 goto done;
1018 error = got_object_get_type(&obj_type, repo, obj_id);
1019 if (error)
1020 goto done;
1022 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1023 error = got_error(GOT_ERR_OBJ_TYPE);
1024 goto done;
1027 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1]);
1028 if (error)
1029 goto done;
1031 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &blobfd);
1032 if (error)
1033 goto done;
1035 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, blobfd);
1036 if (error)
1037 goto done;
1039 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1040 &bca.line_offsets, bca.f, blob);
1041 if (error || bca.nlines == 0)
1042 goto done;
1044 /* Don't include \n at EOF in the blame line count. */
1045 if (bca.line_offsets[bca.nlines - 1] == filesize)
1046 bca.nlines--;
1048 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1049 if (bca.lines == NULL) {
1050 error = got_error_from_errno("calloc");
1051 goto done;
1053 bca.lineno_cur = 1;
1054 bca.nlines_prec = 0;
1055 i = bca.nlines;
1056 while (i > 0) {
1057 i /= 10;
1058 bca.nlines_prec++;
1060 bca.repo = repo;
1061 bca.c = c;
1063 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd1);
1064 if (error)
1065 goto done;
1067 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd2);
1068 if (error)
1069 goto done;
1071 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5]);
1072 if (error)
1073 goto done;
1075 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6]);
1076 if (error)
1077 goto done;
1079 error = got_blame(in_repo_path, commit_id, repo,
1080 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1081 fd1, fd2, f1, f2);
1083 done:
1084 if (bca.lines) {
1085 free(bca.line_offsets);
1086 for (i = 0; i < bca.nlines; i++) {
1087 struct blame_line *bline = &bca.lines[i];
1088 free(bline->id_str);
1089 free(bline->committer);
1092 free(bca.lines);
1093 if (blobfd != -1 && close(blobfd) == -1 && error == NULL)
1094 error = got_error_from_errno("close");
1095 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1096 error = got_error_from_errno("close");
1097 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1098 error = got_error_from_errno("close");
1099 if (bca.f) {
1100 const struct got_error *bca_err = got_gotweb_closefile(bca.f);
1101 if (error == NULL)
1102 error = bca_err;
1104 if (f1) {
1105 const struct got_error *f1_err = got_gotweb_closefile(f1);
1106 if (error == NULL)
1107 error = f1_err;
1109 if (f2) {
1110 const struct got_error *f2_err = got_gotweb_closefile(f2);
1111 if (error == NULL)
1112 error = f2_err;
1114 if (commit)
1115 got_object_commit_close(commit);
1116 if (blob)
1117 got_object_blob_close(blob);
1118 free(in_repo_path);
1119 free(commit_id);
1120 free(obj_id);
1121 free(path);
1122 got_ref_list_free(&refs);
1123 return error;
1126 const struct got_error *
1127 got_open_diff_for_output(FILE **fp, struct request *c)
1129 const struct got_error *error = NULL;
1130 struct transport *t = c->t;
1131 struct got_repository *repo = t->repo;
1132 struct repo_commit *rc = NULL;
1133 struct got_object_id *id1 = NULL, *id2 = NULL;
1134 struct got_reflist_head refs;
1135 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1136 int obj_type, fd1 = -1, fd2 = -1;
1138 *fp = NULL;
1140 TAILQ_INIT(&refs);
1142 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1]);
1143 if (error)
1144 goto done;
1146 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2]);
1147 if (error)
1148 goto done;
1150 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3]);
1151 if (error)
1152 goto done;
1154 rc = TAILQ_FIRST(&t->repo_commits);
1156 if (rc->parent_id != NULL &&
1157 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1158 error = got_repo_match_object_id(&id1, NULL,
1159 rc->parent_id, GOT_OBJ_TYPE_ANY,
1160 &refs, repo);
1161 if (error)
1162 goto done;
1165 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1166 GOT_OBJ_TYPE_ANY, &refs, repo);
1167 if (error)
1168 goto done;
1170 error = got_object_get_type(&obj_type, repo, id2);
1171 if (error)
1172 goto done;
1174 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd1);
1175 if (error)
1176 goto done;
1178 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd2);
1179 if (error)
1180 goto done;
1182 switch (obj_type) {
1183 case GOT_OBJ_TYPE_BLOB:
1184 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd1, fd2,
1185 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1186 NULL, repo, f3);
1187 break;
1188 case GOT_OBJ_TYPE_TREE:
1189 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
1190 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1191 NULL, repo, f3);
1192 break;
1193 case GOT_OBJ_TYPE_COMMIT:
1194 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd1,
1195 fd2, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1196 NULL, repo, f3);
1197 break;
1198 default:
1199 error = got_error(GOT_ERR_OBJ_TYPE);
1201 if (error)
1202 goto done;
1204 if (fseek(f3, 0, SEEK_SET) == -1) {
1205 error = got_ferror(f3, GOT_ERR_IO);
1206 goto done;
1209 *fp = f3;
1211 done:
1212 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1213 error = got_error_from_errno("close");
1214 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1215 error = got_error_from_errno("close");
1216 if (f1) {
1217 const struct got_error *f1_err = got_gotweb_closefile(f1);
1218 if (error == NULL)
1219 error = f1_err;
1221 if (f2) {
1222 const struct got_error *f2_err = got_gotweb_closefile(f2);
1223 if (error == NULL)
1224 error = f2_err;
1226 if (error && f3) {
1227 got_gotweb_closefile(f3);
1228 *fp = NULL;
1230 got_ref_list_free(&refs);
1231 free(id1);
1232 free(id2);
1233 return error;
1236 static const struct got_error *
1237 got_init_repo_commit(struct repo_commit **rc)
1239 *rc = calloc(1, sizeof(**rc));
1240 if (*rc == NULL)
1241 return got_error_from_errno2(__func__, "calloc");
1243 (*rc)->path = NULL;
1244 (*rc)->refs_str = NULL;
1245 (*rc)->commit_id = NULL;
1246 (*rc)->committer = NULL;
1247 (*rc)->author = NULL;
1248 (*rc)->parent_id = NULL;
1249 (*rc)->tree_id = NULL;
1250 (*rc)->commit_msg = NULL;
1252 return NULL;
1255 static const struct got_error *
1256 got_init_repo_tag(struct repo_tag **rt)
1258 *rt = calloc(1, sizeof(**rt));
1259 if (*rt == NULL)
1260 return got_error_from_errno2(__func__, "calloc");
1262 (*rt)->commit_id = NULL;
1263 (*rt)->tag_name = NULL;
1264 (*rt)->tag_commit = NULL;
1265 (*rt)->commit_msg = NULL;
1266 (*rt)->tagger = NULL;
1268 return NULL;