add more info about the test suite to README.portable
[got-portable.git] / gotwebd / got_operations.c
blobf35f39931c13d8233ad28e4840e3f5a660021e1b
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 static const struct got_error *
120 get_ref_commit_timestamp(time_t *timestamp,
121 struct got_reference *ref, struct got_repository *repo)
123 const struct got_error *error;
124 struct got_object_id *id = NULL;
125 int obj_type;
126 struct got_commit_object *commit = NULL;
128 /* We might have a cached timestamp available. */
129 *timestamp = got_ref_get_cached_committer_time(ref);
130 if (*timestamp != 0)
131 return NULL;
133 error = got_ref_resolve(&id, repo, ref);
134 if (error)
135 return error;
137 error = got_object_get_type(&obj_type, repo, id);
138 if (error || obj_type != GOT_OBJ_TYPE_COMMIT)
139 goto done;
141 error = got_object_open_as_commit(&commit, repo, id);
142 if (error)
143 goto done;
145 *timestamp = got_object_commit_get_committer_time(commit);
146 done:
147 free(id);
148 if (commit)
149 got_object_commit_close(commit);
151 return error;
155 * Find the youngest branch tip in the repository, or the age of
156 * a specific branch tip if a name was provided by the caller.
158 const struct got_error *
159 got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
161 const struct got_error *error = NULL;
162 struct transport *t = c->t;
163 struct got_repository *repo = t->repo;
165 *repo_age = 0;
167 if (refname) {
168 struct got_reference *ref;
170 error = got_ref_open(&ref, repo, refname, 0);
171 if (error)
172 return error;
174 error = get_ref_commit_timestamp(repo_age, ref, repo);
175 got_ref_close(ref);
176 } else {
177 struct got_reflist_head refs;
178 struct got_reflist_entry *re;
180 TAILQ_INIT(&refs);
182 error = got_ref_list(&refs, repo, "refs/heads",
183 got_ref_cmp_by_commit_timestamp_descending, repo);
184 if (error)
185 return error;
187 re = TAILQ_FIRST(&refs);
188 if (re) {
189 error = get_ref_commit_timestamp(repo_age,
190 re->ref, repo);
192 got_ref_list_free(&refs);
195 return error;
198 static const struct got_error *
199 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
200 struct got_commit_object *commit, struct got_reflist_head *refs,
201 struct got_object_id *id)
203 const struct got_error *error = NULL;
204 struct got_reflist_entry *re;
205 struct got_object_id *id2 = NULL;
206 struct got_object_qid *parent_id;
207 struct transport *t = c->t;
208 struct querystring *qs = c->t->qs;
209 char *commit_msg = NULL, *commit_msg0;
211 TAILQ_FOREACH(re, refs, entry) {
212 char *s;
213 const char *name;
214 struct got_tag_object *tag = NULL;
215 struct got_object_id *ref_id;
216 int cmp;
218 if (got_ref_is_symbolic(re->ref))
219 continue;
221 name = got_ref_get_name(re->ref);
222 if (strncmp(name, "refs/", 5) == 0)
223 name += 5;
224 if (strncmp(name, "got/", 4) == 0)
225 continue;
226 if (strncmp(name, "heads/", 6) == 0)
227 name += 6;
228 if (strncmp(name, "remotes/", 8) == 0) {
229 name += 8;
230 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
231 continue;
233 error = got_ref_resolve(&ref_id, t->repo, re->ref);
234 if (error)
235 return error;
236 if (strncmp(name, "tags/", 5) == 0) {
237 error = got_object_open_as_tag(&tag, t->repo, ref_id);
238 if (error) {
239 if (error->code != GOT_ERR_OBJ_TYPE) {
240 free(ref_id);
241 continue;
244 * Ref points at something other
245 * than a tag.
247 error = NULL;
248 tag = NULL;
251 cmp = got_object_id_cmp(tag ?
252 got_object_tag_get_object_id(tag) : ref_id, id);
253 free(ref_id);
254 if (tag)
255 got_object_tag_close(tag);
256 if (cmp != 0)
257 continue;
258 s = repo_commit->refs_str;
259 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
260 s ? ", " : "", name) == -1) {
261 error = got_error_from_errno("asprintf");
262 free(s);
263 repo_commit->refs_str = NULL;
264 return error;
266 free(s);
269 error = got_object_id_str(&repo_commit->commit_id, id);
270 if (error)
271 return error;
273 error = got_object_id_str(&repo_commit->tree_id,
274 got_object_commit_get_tree_id(commit));
275 if (error)
276 return error;
278 if (qs->action == DIFF || qs->action == PATCH) {
279 parent_id = STAILQ_FIRST(
280 got_object_commit_get_parent_ids(commit));
281 if (parent_id != NULL) {
282 id2 = got_object_id_dup(&parent_id->id);
283 error = got_object_id_str(&repo_commit->parent_id, id2);
284 if (error)
285 return error;
286 free(id2);
287 } else {
288 repo_commit->parent_id = strdup("/dev/null");
289 if (repo_commit->parent_id == NULL) {
290 error = got_error_from_errno("strdup");
291 return error;
296 repo_commit->committer_time =
297 got_object_commit_get_committer_time(commit);
299 repo_commit->author =
300 strdup(got_object_commit_get_author(commit));
301 if (repo_commit->author == NULL) {
302 error = got_error_from_errno("strdup");
303 return error;
305 repo_commit->committer =
306 strdup(got_object_commit_get_committer(commit));
307 if (repo_commit->committer == NULL) {
308 error = got_error_from_errno("strdup");
309 return error;
311 error = got_object_commit_get_logmsg(&commit_msg0, commit);
312 if (error)
313 return error;
315 commit_msg = commit_msg0;
316 while (*commit_msg == '\n')
317 commit_msg++;
319 repo_commit->commit_msg = strdup(commit_msg);
320 if (repo_commit->commit_msg == NULL)
321 error = got_error_from_errno("strdup");
322 free(commit_msg0);
323 return error;
326 const struct got_error *
327 got_get_repo_commits(struct request *c, size_t limit)
329 const struct got_error *error = NULL;
330 struct got_object_id *id = NULL;
331 struct got_commit_graph *graph = NULL;
332 struct got_commit_object *commit = NULL;
333 struct got_reflist_head refs;
334 struct got_reference *ref = NULL;
335 struct repo_commit *repo_commit = NULL;
336 struct server *srv = c->srv;
337 struct transport *t = c->t;
338 struct got_repository *repo = t->repo;
339 struct querystring *qs = t->qs;
340 struct repo_dir *repo_dir = t->repo_dir;
341 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
342 int chk_next = 0;
344 if (limit == 0)
345 return got_error(GOT_ERR_RANGE);
347 if (limit > 1) {
349 * Traverse one commit more than requested to provide
350 * the next button.
352 limit++;
353 chk_next = 1;
356 TAILQ_INIT(&refs);
358 if (qs->file != NULL && *qs->file != '\0')
359 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
360 qs->file) == -1)
361 return got_error_from_errno("asprintf");
363 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
364 repo_dir->name) == -1) {
365 error = got_error_from_errno("asprintf");
366 goto done;
369 if (qs->commit) {
370 error = got_repo_match_object_id_prefix(&id, qs->commit,
371 GOT_OBJ_TYPE_COMMIT, repo);
372 if (error)
373 goto done;
374 } else {
375 error = got_ref_open(&ref, repo, qs->headref, 0);
376 if (error)
377 goto done;
379 error = got_ref_resolve(&id, repo, ref);
380 if (error)
381 goto done;
384 error = got_repo_map_path(&in_repo_path, repo, repo_path);
385 if (error)
386 goto done;
388 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
389 if (error)
390 goto done;
392 if (qs->file != NULL && *qs->file != '\0') {
393 error = got_commit_graph_open(&graph, file_path, 0);
394 if (error)
395 goto done;
396 } else {
397 error = got_commit_graph_open(&graph, in_repo_path, 0);
398 if (error)
399 goto done;
402 error = got_commit_graph_bfsort(graph, id, repo, NULL, NULL);
403 if (error)
404 goto done;
406 for (;;) {
407 struct got_object_id next_id;
409 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
410 NULL);
411 if (error) {
412 if (error->code == GOT_ERR_ITER_COMPLETED)
413 error = NULL;
414 goto done;
417 error = got_object_open_as_commit(&commit, repo, &next_id);
418 if (error)
419 goto done;
421 error = got_init_repo_commit(&repo_commit);
422 if (error)
423 goto done;
425 error = got_get_repo_commit(c, repo_commit, commit,
426 &refs, &next_id);
427 if (error) {
428 gotweb_free_repo_commit(repo_commit);
429 goto done;
432 if (--limit == 0 && chk_next) {
433 t->more_id = strdup(repo_commit->commit_id);
434 if (t->more_id == NULL)
435 error = got_error_from_errno("strdup");
436 gotweb_free_repo_commit(repo_commit);
437 goto done;
440 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
442 if (limit == 0)
443 goto done;
445 if (commit) {
446 got_object_commit_close(commit);
447 commit = NULL;
450 done:
451 if (ref)
452 got_ref_close(ref);
453 if (commit)
454 got_object_commit_close(commit);
455 if (graph)
456 got_commit_graph_close(graph);
457 got_ref_list_free(&refs);
458 free(in_repo_path);
459 free(file_path);
460 free(repo_path);
461 free(id);
462 return error;
465 const struct got_error *
466 got_get_repo_tags(struct request *c, size_t limit)
468 const struct got_error *error = NULL;
469 struct got_object_id *id = NULL;
470 struct got_commit_object *commit = NULL;
471 struct got_reflist_head refs;
472 struct got_reference *ref;
473 struct got_reflist_entry *re;
474 struct server *srv = c->srv;
475 struct transport *t = c->t;
476 struct got_repository *repo = t->repo;
477 struct querystring *qs = t->qs;
478 struct repo_dir *repo_dir = t->repo_dir;
479 struct got_tag_object *tag = NULL;
480 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
481 char *tag_commit = NULL, *tag_commit0 = NULL;
482 char *commit_msg = NULL, *commit_msg0 = NULL;
483 int chk_next = 0, chk_multi = 1, commit_found = 0;
485 TAILQ_INIT(&refs);
487 if (limit == 0)
488 return got_error(GOT_ERR_RANGE);
490 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
491 repo_dir->name) == -1)
492 return got_error_from_errno("asprintf");
494 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
495 error = got_ref_open(&ref, repo, qs->headref, 0);
496 if (error)
497 goto done;
498 error = got_ref_resolve(&id, repo, ref);
499 got_ref_close(ref);
500 if (error)
501 goto done;
502 } else if (qs->commit == NULL && qs->action == TAG) {
503 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
504 goto done;
505 } else {
506 error = got_repo_match_object_id_prefix(&id, qs->commit,
507 GOT_OBJ_TYPE_COMMIT, repo);
508 if (error)
509 goto done;
512 if (qs->action != SUMMARY && qs->action != TAGS) {
513 error = got_object_open_as_commit(&commit, repo, id);
514 if (error)
515 goto done;
516 error = got_object_commit_get_logmsg(&commit_msg0, commit);
517 if (error)
518 goto done;
519 if (commit) {
520 got_object_commit_close(commit);
521 commit = NULL;
525 error = got_repo_map_path(&in_repo_path, repo, repo_path);
526 if (error)
527 goto done;
529 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
530 repo);
531 if (error)
532 goto done;
534 if (limit == 1)
535 chk_multi = 0;
537 TAILQ_FOREACH(re, &refs, entry) {
538 struct repo_tag *new_repo_tag = NULL;
539 error = got_init_repo_tag(&new_repo_tag);
540 if (error)
541 goto done;
543 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
545 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
546 if (new_repo_tag->tag_name == NULL) {
547 error = got_error_from_errno("strdup");
548 goto done;
551 free(id);
552 id = NULL;
554 free(id_str);
555 id_str = NULL;
557 error = got_ref_resolve(&id, repo, re->ref);
558 if (error)
559 goto done;
561 if (tag)
562 got_object_tag_close(tag);
563 error = got_object_open_as_tag(&tag, repo, id);
564 if (error) {
565 if (error->code != GOT_ERR_OBJ_TYPE)
566 goto done;
567 /* "lightweight" tag */
568 error = got_object_open_as_commit(&commit, repo, id);
569 if (error)
570 goto done;
571 new_repo_tag->tagger =
572 strdup(got_object_commit_get_committer(commit));
573 if (new_repo_tag->tagger == NULL) {
574 error = got_error_from_errno("strdup");
575 goto done;
577 new_repo_tag->tagger_time =
578 got_object_commit_get_committer_time(commit);
579 error = got_object_id_str(&id_str, id);
580 if (error)
581 goto done;
582 } else {
583 new_repo_tag->tagger =
584 strdup(got_object_tag_get_tagger(tag));
585 if (new_repo_tag->tagger == NULL) {
586 error = got_error_from_errno("strdup");
587 goto done;
589 new_repo_tag->tagger_time =
590 got_object_tag_get_tagger_time(tag);
591 error = got_object_id_str(&id_str,
592 got_object_tag_get_object_id(tag));
593 if (error)
594 goto done;
597 new_repo_tag->commit_id = strdup(id_str);
598 if (new_repo_tag->commit_id == NULL)
599 goto done;
601 if (commit_found == 0 && qs->commit != NULL &&
602 strncmp(id_str, qs->commit, strlen(id_str)) != 0) {
603 if (commit) {
604 got_object_commit_close(commit);
605 commit = NULL;
607 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
608 gotweb_free_repo_tag(new_repo_tag);
609 continue;
610 } else
611 commit_found = 1;
614 * check for one more commit before breaking,
615 * so we know whether to navigate through briefs
616 * commits and summary
618 if (chk_next) {
619 t->tags_more_id = strdup(new_repo_tag->commit_id);
620 if (t->tags_more_id == NULL) {
621 error = got_error_from_errno("strdup");
622 goto done;
624 if (commit) {
625 got_object_commit_close(commit);
626 commit = NULL;
628 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
629 gotweb_free_repo_tag(new_repo_tag);
630 goto done;
633 if (commit) {
634 error = got_object_commit_get_logmsg(&tag_commit0,
635 commit);
636 if (error)
637 goto done;
638 got_object_commit_close(commit);
639 commit = NULL;
640 } else {
641 tag_commit0 = strdup(got_object_tag_get_message(tag));
642 if (tag_commit0 == NULL) {
643 error = got_error_from_errno("strdup");
644 goto done;
648 tag_commit = tag_commit0;
649 while (*tag_commit == '\n')
650 tag_commit++;
651 new_repo_tag->tag_commit = strdup(tag_commit);
652 if (new_repo_tag->tag_commit == NULL) {
653 error = got_error_from_errno("strdup");
654 free(tag_commit0);
655 goto done;
657 free(tag_commit0);
659 if (qs->action != SUMMARY && qs->action != TAGS) {
660 commit_msg = commit_msg0;
661 while (*commit_msg == '\n')
662 commit_msg++;
664 new_repo_tag->commit_msg = strdup(commit_msg);
665 if (new_repo_tag->commit_msg == NULL) {
666 error = got_error_from_errno("strdup");
667 goto done;
671 if (limit && --limit == 0) {
672 if (chk_multi == 0)
673 break;
674 chk_next = 1;
678 done:
679 if (commit)
680 got_object_commit_close(commit);
681 if (tag)
682 got_object_tag_close(tag);
683 got_ref_list_free(&refs);
684 free(commit_msg0);
685 free(in_repo_path);
686 free(repo_path);
687 free(id);
688 free(id_str);
689 return error;
693 got_output_repo_tree(struct request *c, char **readme,
694 int (*cb)(struct template *, struct got_tree_entry *))
696 const struct got_error *error = NULL;
697 struct transport *t = c->t;
698 struct got_commit_object *commit = NULL;
699 struct got_repository *repo = t->repo;
700 struct querystring *qs = t->qs;
701 struct repo_commit *rc = NULL;
702 struct got_object_id *tree_id = NULL, *commit_id = NULL;
703 struct got_reflist_head refs;
704 struct got_tree_object *tree = NULL;
705 struct got_tree_entry *te;
706 struct repo_dir *repo_dir = t->repo_dir;
707 const char *name;
708 mode_t mode;
709 char *escaped_name = NULL, *path = NULL;
710 int nentries, i;
712 TAILQ_INIT(&refs);
713 *readme = NULL;
715 rc = TAILQ_FIRST(&t->repo_commits);
717 if (qs->folder != NULL) {
718 path = strdup(qs->folder);
719 if (path == NULL) {
720 error = got_error_from_errno("strdup");
721 goto done;
723 } else {
724 error = got_repo_map_path(&path, repo, repo_dir->path);
725 if (error)
726 goto done;
729 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
730 GOT_OBJ_TYPE_COMMIT, &refs, repo);
731 if (error)
732 goto done;
734 error = got_object_open_as_commit(&commit, repo, commit_id);
735 if (error)
736 goto done;
738 error = got_object_id_by_path(&tree_id, repo, commit, path);
739 if (error)
740 goto done;
742 error = got_object_open_as_tree(&tree, repo, tree_id);
743 if (error)
744 goto done;
746 nentries = got_object_tree_get_nentries(tree);
748 for (i = 0; i < nentries; i++) {
749 te = got_object_tree_get_entry(tree, i);
751 name = got_tree_entry_get_name(te);
752 mode = got_tree_entry_get_mode(te);
753 if (!S_ISDIR(mode) && (!strcasecmp(name, "README") ||
754 !strcasecmp(name, "README.md") ||
755 !strcasecmp(name, "README.txt"))) {
756 free(*readme);
757 *readme = strdup(name);
760 if (cb(c->tp, te) == -1) {
761 error = got_error(GOT_ERR_CANCELLED);
762 break;
765 done:
766 free(escaped_name);
767 free(path);
768 got_ref_list_free(&refs);
769 if (commit)
770 got_object_commit_close(commit);
771 if (tree)
772 got_object_tree_close(tree);
773 free(commit_id);
774 free(tree_id);
775 if (error) {
776 free(*readme);
777 *readme = NULL;
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, const char *directory, const char *file,
788 const char *commitstr)
790 const struct got_error *error = NULL;
791 struct got_repository *repo = c->t->repo;
792 struct got_commit_object *commit = NULL;
793 struct got_object_id *commit_id = NULL;
794 struct got_object_id *blob_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 error = got_ref_list(&refs, repo, "refs/heads",
806 got_ref_cmp_by_name, NULL);
807 if (error)
808 goto done;
810 if (asprintf(&path, "%s%s%s", directory ? directory : "",
811 directory ? "/" : "", file) == -1) {
812 error = got_error_from_errno("asprintf");
813 goto done;
816 error = got_repo_map_path(&in_repo_path, repo, path);
817 if (error)
818 goto done;
820 if (commitstr == NULL)
821 commitstr = GOT_REF_HEAD;
823 error = got_repo_match_object_id(&commit_id, NULL, commitstr,
824 GOT_OBJ_TYPE_COMMIT, &refs, repo);
825 if (error)
826 goto done;
828 error = got_object_open_as_commit(&commit, repo, commit_id);
829 if (error)
830 goto done;
832 error = got_object_id_by_path(&blob_id, repo, commit, in_repo_path);
833 if (error)
834 goto done;
836 if (blob_id == NULL) {
837 error = got_error(GOT_ERR_NO_OBJ);
838 goto done;
841 error = got_object_get_type(&obj_type, repo, blob_id);
842 if (error)
843 goto done;
845 if (obj_type != GOT_OBJ_TYPE_BLOB) {
846 error = got_error(GOT_ERR_OBJ_TYPE);
847 goto done;
850 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
851 if (error)
852 goto done;
854 error = got_object_open_as_blob(blob, repo, blob_id, BUF, *fd);
855 if (error)
856 goto done;
858 error = got_object_blob_is_binary(binary, *blob);
859 if (error)
860 goto done;
862 done:
863 if (commit)
864 got_object_commit_close(commit);
866 if (error) {
867 if (*fd != -1)
868 close(*fd);
869 if (*blob)
870 got_object_blob_close(*blob);
871 *fd = -1;
872 *blob = NULL;
875 got_ref_list_free(&refs);
876 free(in_repo_path);
877 free(commit_id);
878 free(blob_id);
879 free(path);
880 return error;
884 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
885 int (*cb)(struct template *, const char *, size_t))
887 const struct got_error *err;
888 char *line = NULL;
889 size_t linesize = 0;
890 size_t lineno = 0;
891 ssize_t linelen = 0;
893 for (;;) {
894 err = got_object_blob_getline(&line, &linelen, &linesize,
895 blob);
896 if (err || linelen == -1)
897 break;
898 lineno++;
899 if (cb(tp, line, lineno) == -1) {
900 err = got_error(GOT_ERR_CANCELLED);
901 break;
905 free(line);
907 if (err) {
908 if (err->code != GOT_ERR_CANCELLED)
909 log_warnx("%s: got_object_blob_getline failed: %s",
910 __func__, err->msg);
911 return -1;
913 return 0;
916 struct blame_cb_args {
917 struct blame_line *lines;
918 int nlines;
919 int nlines_prec;
920 int lineno_cur;
921 off_t *line_offsets;
922 FILE *f;
923 struct got_repository *repo;
924 struct request *c;
925 got_render_blame_line_cb cb;
928 static const struct got_error *
929 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
930 struct got_commit_object *commit, struct got_object_id *id)
932 const struct got_error *err = NULL;
933 struct blame_cb_args *a = arg;
934 struct blame_line *bline;
935 struct request *c = a->c;
936 char *line = NULL;
937 size_t linesize = 0;
938 off_t offset;
939 struct tm tm;
940 time_t committer_time;
942 if (nlines != a->nlines ||
943 (lineno != -1 && lineno < 1) || lineno > a->nlines)
944 return got_error(GOT_ERR_RANGE);
946 if (lineno == -1)
947 return NULL; /* no change in this commit */
949 /* Annotate this line. */
950 bline = &a->lines[lineno - 1];
951 if (bline->annotated)
952 return NULL;
953 err = got_object_id_str(&bline->id_str, id);
954 if (err)
955 return err;
957 bline->committer = strdup(got_object_commit_get_committer(commit));
958 if (bline->committer == NULL) {
959 err = got_error_from_errno("strdup");
960 goto done;
963 committer_time = got_object_commit_get_committer_time(commit);
964 if (gmtime_r(&committer_time, &tm) == NULL)
965 return got_error_from_errno("gmtime_r");
966 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%F", &tm) == 0) {
967 err = got_error(GOT_ERR_NO_SPACE);
968 goto done;
970 bline->annotated = 1;
972 /* Print lines annotated so far. */
973 bline = &a->lines[a->lineno_cur - 1];
974 if (!bline->annotated)
975 goto done;
977 offset = a->line_offsets[a->lineno_cur - 1];
978 if (fseeko(a->f, offset, SEEK_SET) == -1) {
979 err = got_error_from_errno("fseeko");
980 goto done;
983 while (a->lineno_cur <= a->nlines && bline->annotated) {
984 if (getline(&line, &linesize, a->f) == -1) {
985 if (ferror(a->f))
986 err = got_error_from_errno("getline");
987 break;
990 if (a->cb(c->tp, line, bline, a->nlines_prec,
991 a->lineno_cur) == -1) {
992 err = got_error(GOT_ERR_CANCELLED);
993 break;
996 a->lineno_cur++;
997 bline = &a->lines[a->lineno_cur - 1];
999 done:
1000 free(line);
1001 return err;
1004 const struct got_error *
1005 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1007 const struct got_error *error = NULL;
1008 struct transport *t = c->t;
1009 struct got_repository *repo = t->repo;
1010 struct querystring *qs = c->t->qs;
1011 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1012 struct got_commit_object *commit = NULL;
1013 struct got_reflist_head refs;
1014 struct got_blob_object *blob = NULL;
1015 char *path = NULL, *in_repo_path = NULL;
1016 struct blame_cb_args bca;
1017 int i, obj_type, blobfd = -1, fd1 = -1, fd2 = -1;
1018 off_t filesize;
1019 FILE *f1 = NULL, *f2 = NULL;
1021 TAILQ_INIT(&refs);
1022 bca.f = NULL;
1023 bca.lines = NULL;
1024 bca.cb = cb;
1026 if (asprintf(&path, "%s/%s", qs->folder, qs->file) == -1) {
1027 error = got_error_from_errno("asprintf");
1028 goto done;
1031 error = got_repo_map_path(&in_repo_path, repo, path);
1032 if (error)
1033 goto done;
1035 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1036 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1037 if (error)
1038 goto done;
1040 error = got_object_open_as_commit(&commit, repo, commit_id);
1041 if (error)
1042 goto done;
1044 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1045 if (error)
1046 goto done;
1048 error = got_object_get_type(&obj_type, repo, obj_id);
1049 if (error)
1050 goto done;
1052 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1053 error = got_error(GOT_ERR_OBJ_TYPE);
1054 goto done;
1057 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1]);
1058 if (error)
1059 goto done;
1061 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &blobfd);
1062 if (error)
1063 goto done;
1065 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, blobfd);
1066 if (error)
1067 goto done;
1069 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1070 &bca.line_offsets, bca.f, blob);
1071 if (error || bca.nlines == 0)
1072 goto done;
1074 /* Don't include \n at EOF in the blame line count. */
1075 if (bca.line_offsets[bca.nlines - 1] == filesize)
1076 bca.nlines--;
1078 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1079 if (bca.lines == NULL) {
1080 error = got_error_from_errno("calloc");
1081 goto done;
1083 bca.lineno_cur = 1;
1084 bca.nlines_prec = 0;
1085 i = bca.nlines;
1086 while (i > 0) {
1087 i /= 10;
1088 bca.nlines_prec++;
1090 bca.repo = repo;
1091 bca.c = c;
1093 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd1);
1094 if (error)
1095 goto done;
1097 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd2);
1098 if (error)
1099 goto done;
1101 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5]);
1102 if (error)
1103 goto done;
1105 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6]);
1106 if (error)
1107 goto done;
1109 error = got_blame(in_repo_path, commit_id, repo,
1110 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1111 fd1, fd2, f1, f2);
1113 done:
1114 if (bca.lines) {
1115 free(bca.line_offsets);
1116 for (i = 0; i < bca.nlines; i++) {
1117 struct blame_line *bline = &bca.lines[i];
1118 free(bline->id_str);
1119 free(bline->committer);
1122 free(bca.lines);
1123 if (blobfd != -1 && close(blobfd) == -1 && error == NULL)
1124 error = got_error_from_errno("close");
1125 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1126 error = got_error_from_errno("close");
1127 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1128 error = got_error_from_errno("close");
1129 if (bca.f) {
1130 const struct got_error *bca_err = got_gotweb_closefile(bca.f);
1131 if (error == NULL)
1132 error = bca_err;
1134 if (f1) {
1135 const struct got_error *f1_err = got_gotweb_closefile(f1);
1136 if (error == NULL)
1137 error = f1_err;
1139 if (f2) {
1140 const struct got_error *f2_err = got_gotweb_closefile(f2);
1141 if (error == NULL)
1142 error = f2_err;
1144 if (commit)
1145 got_object_commit_close(commit);
1146 if (blob)
1147 got_object_blob_close(blob);
1148 free(in_repo_path);
1149 free(commit_id);
1150 free(obj_id);
1151 free(path);
1152 got_ref_list_free(&refs);
1153 return error;
1156 const struct got_error *
1157 got_open_diff_for_output(FILE **fp, struct request *c)
1159 const struct got_error *error = NULL;
1160 struct transport *t = c->t;
1161 struct got_repository *repo = t->repo;
1162 struct repo_commit *rc = NULL;
1163 struct got_object_id *id1 = NULL, *id2 = NULL;
1164 struct got_reflist_head refs;
1165 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1166 int obj_type, fd1 = -1, fd2 = -1;
1168 *fp = NULL;
1170 TAILQ_INIT(&refs);
1172 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1]);
1173 if (error)
1174 goto done;
1176 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2]);
1177 if (error)
1178 goto done;
1180 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3]);
1181 if (error)
1182 goto done;
1184 rc = TAILQ_FIRST(&t->repo_commits);
1186 if (rc->parent_id != NULL &&
1187 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1188 error = got_repo_match_object_id(&id1, NULL,
1189 rc->parent_id, GOT_OBJ_TYPE_ANY,
1190 &refs, repo);
1191 if (error)
1192 goto done;
1195 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1196 GOT_OBJ_TYPE_ANY, &refs, repo);
1197 if (error)
1198 goto done;
1200 error = got_object_get_type(&obj_type, repo, id2);
1201 if (error)
1202 goto done;
1204 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd1);
1205 if (error)
1206 goto done;
1208 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd2);
1209 if (error)
1210 goto done;
1212 switch (obj_type) {
1213 case GOT_OBJ_TYPE_BLOB:
1214 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd1, fd2,
1215 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1216 NULL, repo, f3);
1217 break;
1218 case GOT_OBJ_TYPE_TREE:
1219 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
1220 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1221 NULL, repo, f3);
1222 break;
1223 case GOT_OBJ_TYPE_COMMIT:
1224 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd1,
1225 fd2, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1226 NULL, repo, f3);
1227 break;
1228 default:
1229 error = got_error(GOT_ERR_OBJ_TYPE);
1231 if (error)
1232 goto done;
1234 if (fseek(f3, 0, SEEK_SET) == -1) {
1235 error = got_ferror(f3, GOT_ERR_IO);
1236 goto done;
1239 *fp = f3;
1241 done:
1242 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1243 error = got_error_from_errno("close");
1244 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1245 error = got_error_from_errno("close");
1246 if (f1) {
1247 const struct got_error *f1_err = got_gotweb_closefile(f1);
1248 if (error == NULL)
1249 error = f1_err;
1251 if (f2) {
1252 const struct got_error *f2_err = got_gotweb_closefile(f2);
1253 if (error == NULL)
1254 error = f2_err;
1256 if (error && f3) {
1257 got_gotweb_closefile(f3);
1258 *fp = NULL;
1260 got_ref_list_free(&refs);
1261 free(id1);
1262 free(id2);
1263 return error;
1266 static const struct got_error *
1267 got_init_repo_commit(struct repo_commit **rc)
1269 *rc = calloc(1, sizeof(**rc));
1270 if (*rc == NULL)
1271 return got_error_from_errno2(__func__, "calloc");
1273 (*rc)->path = NULL;
1274 (*rc)->refs_str = NULL;
1275 (*rc)->commit_id = NULL;
1276 (*rc)->committer = NULL;
1277 (*rc)->author = NULL;
1278 (*rc)->parent_id = NULL;
1279 (*rc)->tree_id = NULL;
1280 (*rc)->commit_msg = NULL;
1282 return NULL;
1285 static const struct got_error *
1286 got_init_repo_tag(struct repo_tag **rt)
1288 *rt = calloc(1, sizeof(**rt));
1289 if (*rt == NULL)
1290 return got_error_from_errno2(__func__, "calloc");
1292 (*rt)->commit_id = NULL;
1293 (*rt)->tag_name = NULL;
1294 (*rt)->tag_commit = NULL;
1295 (*rt)->commit_msg = NULL;
1296 (*rt)->tagger = NULL;
1298 return NULL;