Blob


1 /*
2 * Copyright (c) 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/socket.h>
20 #include <sys/stat.h>
22 #include <event.h>
23 #include <imsg.h>
24 #include <sha2.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_reference.h"
33 #include "got_repository.h"
34 #include "got_path.h"
35 #include "got_cancel.h"
36 #include "got_diff.h"
37 #include "got_commit_graph.h"
38 #include "got_blame.h"
39 #include "got_privsep.h"
41 #include "got_compat.h"
43 #include "proc.h"
44 #include "gotwebd.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 *, 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_flushfile(FILE *f, int fd)
58 {
59 if (fseek(f, 0, SEEK_SET) == -1)
60 return got_error_from_errno("fseek");
62 if (ftruncate(fd, 0) == -1)
63 return got_error_from_errno("ftruncate");
65 if (fsync(fd) == -1)
66 return got_error_from_errno("fsync");
68 if (f && fclose(f) == EOF)
69 return got_error_from_errno("fclose");
71 if (fd != -1 && close(fd) != -1)
72 return got_error_from_errno("close");
74 return NULL;
75 }
77 static const struct got_error *
78 got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
79 {
80 *fd = dup(*priv_fd);
81 if (*fd == -1)
82 return got_error_from_errno("dup");
84 *f = fdopen(*fd, "w+");
85 if (*f == NULL) {
86 close(*fd);
87 return got_error(GOT_ERR_PRIVSEP_NO_FD);
88 }
90 return NULL;
91 }
93 static const struct got_error *
94 got_gotweb_dupfd(int *priv_fd, int *fd)
95 {
96 *fd = dup(*priv_fd);
98 if (*fd == -1)
99 return got_error_from_errno("dup");
101 return NULL;
104 const struct got_error *
105 got_get_repo_owner(char **owner, struct request *c)
107 struct server *srv = c->srv;
108 struct transport *t = c->t;
109 struct got_repository *repo = t->repo;
110 const char *gitconfig_owner;
112 *owner = NULL;
114 if (srv->show_repo_owner == 0)
115 return NULL;
117 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
118 if (gitconfig_owner) {
119 *owner = strdup(gitconfig_owner);
120 if (*owner == NULL)
121 return got_error_from_errno("strdup");
122 } else {
123 *owner = strdup("");
124 if (*owner == NULL)
125 return got_error_from_errno("strdup");
127 return NULL;
130 const struct got_error *
131 got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
133 const struct got_error *error = NULL;
134 struct server *srv = c->srv;
135 struct transport *t = c->t;
136 struct got_repository *repo = t->repo;
137 struct got_commit_object *commit = NULL;
138 struct got_reflist_head refs;
139 struct got_reflist_entry *re;
140 time_t committer_time = 0, cmp_time = 0;
142 TAILQ_INIT(&refs);
144 if (srv->show_repo_age == 0)
145 return NULL;
147 error = got_ref_list(&refs, repo, "refs/heads",
148 got_ref_cmp_by_name, NULL);
149 if (error)
150 goto done;
152 /*
153 * Find the youngest branch tip in the repository, or the age of
154 * the a specific branch tip if a name was provided by the caller.
155 */
156 TAILQ_FOREACH(re, &refs, entry) {
157 struct got_object_id *id = NULL;
159 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
160 continue;
162 error = got_ref_resolve(&id, repo, re->ref);
163 if (error)
164 goto done;
166 error = got_object_open_as_commit(&commit, repo, id);
167 free(id);
168 if (error)
169 goto done;
171 committer_time =
172 got_object_commit_get_committer_time(commit);
173 got_object_commit_close(commit);
174 if (cmp_time < committer_time)
175 cmp_time = committer_time;
177 if (refname)
178 break;
181 if (cmp_time != 0)
182 *repo_age = cmp_time;
183 done:
184 got_ref_list_free(&refs);
185 return error;
188 static const struct got_error *
189 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
190 struct got_commit_object *commit, struct got_reflist_head *refs,
191 struct got_object_id *id)
193 const struct got_error *error = NULL;
194 struct got_reflist_entry *re;
195 struct got_object_id *id2 = NULL;
196 struct got_object_qid *parent_id;
197 struct transport *t = c->t;
198 struct querystring *qs = c->t->qs;
199 char *commit_msg = NULL, *commit_msg0;
201 TAILQ_FOREACH(re, refs, entry) {
202 char *s;
203 const char *name;
204 struct got_tag_object *tag = NULL;
205 struct got_object_id *ref_id;
206 int cmp;
208 if (got_ref_is_symbolic(re->ref))
209 continue;
211 name = got_ref_get_name(re->ref);
212 if (strncmp(name, "refs/", 5) == 0)
213 name += 5;
214 if (strncmp(name, "got/", 4) == 0)
215 continue;
216 if (strncmp(name, "heads/", 6) == 0)
217 name += 6;
218 if (strncmp(name, "remotes/", 8) == 0) {
219 name += 8;
220 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
221 continue;
223 error = got_ref_resolve(&ref_id, t->repo, re->ref);
224 if (error)
225 return error;
226 if (strncmp(name, "tags/", 5) == 0) {
227 error = got_object_open_as_tag(&tag, t->repo, ref_id);
228 if (error) {
229 if (error->code != GOT_ERR_OBJ_TYPE) {
230 free(ref_id);
231 continue;
233 /*
234 * Ref points at something other
235 * than a tag.
236 */
237 error = NULL;
238 tag = NULL;
241 cmp = got_object_id_cmp(tag ?
242 got_object_tag_get_object_id(tag) : ref_id, id);
243 free(ref_id);
244 if (tag)
245 got_object_tag_close(tag);
246 if (cmp != 0)
247 continue;
248 s = repo_commit->refs_str;
249 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
250 s ? ", " : "", name) == -1) {
251 error = got_error_from_errno("asprintf");
252 free(s);
253 repo_commit->refs_str = NULL;
254 return error;
256 free(s);
259 error = got_object_id_str(&repo_commit->commit_id, id);
260 if (error)
261 return error;
263 error = got_object_id_str(&repo_commit->tree_id,
264 got_object_commit_get_tree_id(commit));
265 if (error)
266 return error;
268 if (qs->action == DIFF) {
269 parent_id = STAILQ_FIRST(
270 got_object_commit_get_parent_ids(commit));
271 if (parent_id != NULL) {
272 id2 = got_object_id_dup(&parent_id->id);
273 error = got_object_id_str(&repo_commit->parent_id, id2);
274 if (error)
275 return error;
276 free(id2);
277 } else {
278 repo_commit->parent_id = strdup("/dev/null");
279 if (repo_commit->parent_id == NULL) {
280 error = got_error_from_errno("strdup");
281 return error;
286 repo_commit->committer_time =
287 got_object_commit_get_committer_time(commit);
289 repo_commit->author =
290 strdup(got_object_commit_get_author(commit));
291 if (repo_commit->author == NULL) {
292 error = got_error_from_errno("strdup");
293 return error;
295 repo_commit->committer =
296 strdup(got_object_commit_get_committer(commit));
297 if (repo_commit->committer == NULL) {
298 error = got_error_from_errno("strdup");
299 return error;
301 error = got_object_commit_get_logmsg(&commit_msg0, commit);
302 if (error)
303 return error;
305 commit_msg = commit_msg0;
306 while (*commit_msg == '\n')
307 commit_msg++;
309 repo_commit->commit_msg = strdup(commit_msg);
310 if (repo_commit->commit_msg == NULL)
311 error = got_error_from_errno("strdup");
312 free(commit_msg0);
313 return error;
316 const struct got_error *
317 got_get_repo_commits(struct request *c, int limit)
319 const struct got_error *error = NULL;
320 struct got_object_id *id = NULL;
321 struct got_commit_graph *graph = NULL;
322 struct got_commit_object *commit = NULL;
323 struct got_reflist_head refs;
324 struct got_reference *ref = NULL;
325 struct repo_commit *repo_commit = NULL;
326 struct server *srv = c->srv;
327 struct transport *t = c->t;
328 struct got_repository *repo = t->repo;
329 struct querystring *qs = t->qs;
330 struct repo_dir *repo_dir = t->repo_dir;
331 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
332 int chk_next = 0, chk_multi = 0;
334 TAILQ_INIT(&refs);
336 if (qs->file != NULL && strlen(qs->file) > 0)
337 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
338 qs->file) == -1)
339 return got_error_from_errno("asprintf");
341 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
342 repo_dir->name) == -1) {
343 error = got_error_from_errno("asprintf");
344 goto done;
347 if (qs->commit) {
348 error = got_repo_match_object_id_prefix(&id, qs->commit,
349 GOT_OBJ_TYPE_COMMIT, repo);
350 if (error)
351 goto done;
352 } else {
353 error = got_ref_open(&ref, repo, qs->headref, 0);
354 if (error)
355 goto done;
357 error = got_ref_resolve(&id, repo, ref);
358 if (error)
359 goto done;
362 error = got_repo_map_path(&in_repo_path, repo, repo_path);
363 if (error)
364 goto done;
366 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
367 if (error)
368 goto done;
370 if (qs->file != NULL && strlen(qs->file) > 0) {
371 error = got_commit_graph_open(&graph, file_path, 0);
372 if (error)
373 goto done;
374 } else {
375 error = got_commit_graph_open(&graph, in_repo_path, 0);
376 if (error)
377 goto done;
380 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
381 if (error)
382 goto done;
384 for (;;) {
385 struct got_object_id next_id;
387 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
388 NULL);
389 if (error) {
390 if (error->code == GOT_ERR_ITER_COMPLETED)
391 error = NULL;
392 goto done;
395 error = got_object_open_as_commit(&commit, repo, &next_id);
396 if (error)
397 goto done;
399 error = got_init_repo_commit(&repo_commit);
400 if (error)
401 goto done;
403 error = got_get_repo_commit(c, repo_commit, commit,
404 &refs, &next_id);
405 if (error) {
406 gotweb_free_repo_commit(repo_commit);
407 goto done;
410 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
412 if (!chk_multi || limit != 1 ||
413 srv->max_commits_display == 1) {
414 chk_multi = 1;
416 /*
417 * check for one more commit before breaking,
418 * so we know whether to navigate through briefs
419 * commits and summary
420 */
421 if (chk_next && (qs->action == BRIEFS ||
422 qs->action == COMMITS || qs->action == SUMMARY)) {
423 t->more_id = strdup(repo_commit->commit_id);
424 if (t->more_id == NULL) {
425 error = got_error_from_errno("strdup");
426 goto done;
428 got_object_commit_close(commit);
429 commit = NULL;
430 TAILQ_REMOVE(&t->repo_commits, repo_commit,
431 entry);
432 gotweb_free_repo_commit(repo_commit);
433 goto done;
436 if (error || (limit && --limit == 0)) {
437 if (qs->file != NULL && *qs->file != '\0')
438 if (chk_multi == 0)
439 break;
440 chk_next = 1;
442 if (commit) {
443 got_object_commit_close(commit);
444 commit = NULL;
447 done:
448 if (ref)
449 got_ref_close(ref);
450 if (commit)
451 got_object_commit_close(commit);
452 if (graph)
453 got_commit_graph_close(graph);
454 got_ref_list_free(&refs);
455 free(in_repo_path);
456 free(file_path);
457 free(repo_path);
458 free(id);
459 return error;
462 const struct got_error *
463 got_get_repo_tags(struct request *c, int limit)
465 const struct got_error *error = NULL;
466 struct got_object_id *id = NULL;
467 struct got_commit_object *commit = NULL;
468 struct got_reflist_head refs;
469 struct got_reference *ref;
470 struct got_reflist_entry *re;
471 struct server *srv = c->srv;
472 struct transport *t = c->t;
473 struct got_repository *repo = t->repo;
474 struct querystring *qs = t->qs;
475 struct repo_dir *repo_dir = t->repo_dir;
476 struct got_tag_object *tag = NULL;
477 struct repo_tag *rt = NULL, *trt = NULL;
478 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
479 char *tag_commit = NULL, *tag_commit0 = NULL;
480 char *commit_msg = NULL, *commit_msg0 = NULL;
481 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
483 TAILQ_INIT(&refs);
485 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
486 repo_dir->name) == -1)
487 return got_error_from_errno("asprintf");
489 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
490 error = got_ref_open(&ref, repo, qs->headref, 0);
491 if (error)
492 goto err;
493 error = got_ref_resolve(&id, repo, ref);
494 got_ref_close(ref);
495 if (error)
496 goto err;
497 } else if (qs->commit == NULL && qs->action == TAG) {
498 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
499 goto err;
500 } else {
501 error = got_repo_match_object_id_prefix(&id, qs->commit,
502 GOT_OBJ_TYPE_COMMIT, repo);
503 if (error)
504 goto err;
507 if (qs->action != SUMMARY && qs->action != TAGS) {
508 error = got_object_open_as_commit(&commit, repo, id);
509 if (error)
510 goto err;
511 error = got_object_commit_get_logmsg(&commit_msg0, commit);
512 if (error)
513 goto err;
514 if (commit) {
515 got_object_commit_close(commit);
516 commit = NULL;
520 error = got_repo_map_path(&in_repo_path, repo, repo_path);
521 if (error)
522 goto err;
524 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
525 repo);
526 if (error)
527 goto err;
529 if (limit == 1)
530 chk_multi = 0;
532 /*
533 * XXX: again, see previous message about caching
534 */
536 TAILQ_FOREACH(re, &refs, entry) {
537 struct repo_tag *new_repo_tag = NULL;
538 error = got_init_repo_tag(&new_repo_tag);
539 if (error)
540 goto err;
542 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
544 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
545 if (new_repo_tag->tag_name == NULL) {
546 error = got_error_from_errno("strdup");
547 goto err;
550 free(id);
551 id = NULL;
553 free(id_str);
554 id_str = NULL;
556 error = got_ref_resolve(&id, repo, re->ref);
557 if (error)
558 goto done;
560 if (tag)
561 got_object_tag_close(tag);
562 error = got_object_open_as_tag(&tag, repo, id);
563 if (error) {
564 if (error->code != GOT_ERR_OBJ_TYPE)
565 goto done;
566 /* "lightweight" tag */
567 error = got_object_open_as_commit(&commit, repo, id);
568 if (error)
569 goto done;
570 new_repo_tag->tagger =
571 strdup(got_object_commit_get_committer(commit));
572 if (new_repo_tag->tagger == NULL) {
573 error = got_error_from_errno("strdup");
574 goto err;
576 new_repo_tag->tagger_time =
577 got_object_commit_get_committer_time(commit);
578 error = got_object_id_str(&id_str, id);
579 if (error)
580 goto err;
581 } else {
582 new_repo_tag->tagger =
583 strdup(got_object_tag_get_tagger(tag));
584 if (new_repo_tag->tagger == NULL) {
585 error = got_error_from_errno("strdup");
586 goto err;
588 new_repo_tag->tagger_time =
589 got_object_tag_get_tagger_time(tag);
590 error = got_object_id_str(&id_str,
591 got_object_tag_get_object_id(tag));
592 if (error)
593 goto err;
596 new_repo_tag->commit_id = strdup(id_str);
597 if (new_repo_tag->commit_id == NULL)
598 goto err;
600 if (commit_found == 0 && qs->commit != NULL &&
601 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
602 continue;
603 else
604 commit_found = 1;
606 t->tag_count++;
608 /*
609 * check for one more commit before breaking,
610 * so we know whether to navigate through briefs
611 * commits and summary
612 */
613 if (chk_next) {
614 t->next_id = strdup(new_repo_tag->commit_id);
615 if (t->next_id == NULL) {
616 error = got_error_from_errno("strdup");
617 goto err;
619 if (commit) {
620 got_object_commit_close(commit);
621 commit = NULL;
623 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
624 gotweb_free_repo_tag(new_repo_tag);
625 goto done;
628 if (commit) {
629 error = got_object_commit_get_logmsg(&tag_commit0,
630 commit);
631 if (error)
632 goto err;
633 got_object_commit_close(commit);
634 commit = NULL;
635 } else {
636 tag_commit0 = strdup(got_object_tag_get_message(tag));
637 if (tag_commit0 == NULL) {
638 error = got_error_from_errno("strdup");
639 goto err;
643 tag_commit = tag_commit0;
644 while (*tag_commit == '\n')
645 tag_commit++;
646 new_repo_tag->tag_commit = strdup(tag_commit);
647 if (new_repo_tag->tag_commit == NULL) {
648 error = got_error_from_errno("strdup");
649 free(tag_commit0);
650 goto err;
652 free(tag_commit0);
654 if (qs->action != SUMMARY && qs->action != TAGS) {
655 commit_msg = commit_msg0;
656 while (*commit_msg == '\n')
657 commit_msg++;
659 new_repo_tag->commit_msg = strdup(commit_msg);
660 if (new_repo_tag->commit_msg == NULL) {
661 error = got_error_from_errno("strdup");
662 goto err;
666 if (limit && --limit == 0) {
667 if (chk_multi == 0)
668 break;
669 chk_next = 1;
673 done:
674 /*
675 * we have tailq populated, so find previous commit id
676 * for navigation through briefs and commits
677 */
678 if (t->tag_count == 0) {
679 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
680 TAILQ_REMOVE(&t->repo_tags, rt, entry);
681 gotweb_free_repo_tag(rt);
684 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
685 commit_found = 0;
686 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
687 entry) {
688 if (commit_found == 0 && rt->commit_id != NULL &&
689 strcmp(qs->commit, rt->commit_id) != 0) {
690 continue;
691 } else
692 commit_found = 1;
693 if (c_cnt == srv->max_commits_display ||
694 rt == TAILQ_FIRST(&t->repo_tags)) {
695 t->prev_id = strdup(rt->commit_id);
696 if (t->prev_id == NULL)
697 error = got_error_from_errno("strdup");
698 break;
700 c_cnt++;
703 err:
704 if (commit)
705 got_object_commit_close(commit);
706 if (tag)
707 got_object_tag_close(tag);
708 got_ref_list_free(&refs);
709 free(commit_msg0);
710 free(in_repo_path);
711 free(repo_path);
712 free(id);
713 free(id_str);
714 return error;
717 int
718 got_output_repo_tree(struct request *c,
719 int (*cb)(struct template *, struct got_tree_entry *))
721 const struct got_error *error = NULL;
722 struct transport *t = c->t;
723 struct got_commit_object *commit = NULL;
724 struct got_repository *repo = t->repo;
725 struct querystring *qs = t->qs;
726 struct repo_commit *rc = NULL;
727 struct got_object_id *tree_id = NULL, *commit_id = NULL;
728 struct got_reflist_head refs;
729 struct got_tree_object *tree = NULL;
730 struct got_tree_entry *te;
731 struct repo_dir *repo_dir = t->repo_dir;
732 char *escaped_name = NULL, *path = NULL;
733 int nentries, i;
735 TAILQ_INIT(&refs);
737 rc = TAILQ_FIRST(&t->repo_commits);
739 if (qs->folder != NULL) {
740 path = strdup(qs->folder);
741 if (path == NULL) {
742 error = got_error_from_errno("strdup");
743 goto done;
745 } else {
746 error = got_repo_map_path(&path, repo, repo_dir->path);
747 if (error)
748 goto done;
751 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
752 GOT_OBJ_TYPE_COMMIT, &refs, repo);
753 if (error)
754 goto done;
756 error = got_object_open_as_commit(&commit, repo, commit_id);
757 if (error)
758 goto done;
760 error = got_object_id_by_path(&tree_id, repo, commit, path);
761 if (error)
762 goto done;
764 error = got_object_open_as_tree(&tree, repo, tree_id);
765 if (error)
766 goto done;
768 nentries = got_object_tree_get_nentries(tree);
770 for (i = 0; i < nentries; i++) {
771 te = got_object_tree_get_entry(tree, i);
772 if (cb(c->tp, te) == -1) {
773 error = got_error(GOT_ERR_CANCELLED);
774 break;
777 done:
778 free(escaped_name);
779 free(path);
780 got_ref_list_free(&refs);
781 if (commit)
782 got_object_commit_close(commit);
783 if (tree)
784 got_object_tree_close(tree);
785 free(commit_id);
786 free(tree_id);
787 if (error) {
788 log_warnx("%s: %s", __func__, error->msg);
789 return -1;
791 return 0;
794 const struct got_error *
795 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
796 int *binary, struct request *c)
798 const struct got_error *error = NULL;
799 struct transport *t = c->t;
800 struct got_repository *repo = t->repo;
801 struct querystring *qs = c->t->qs;
802 struct got_commit_object *commit = NULL;
803 struct got_object_id *commit_id = NULL;
804 struct got_reflist_head refs;
805 char *path = NULL, *in_repo_path = NULL;
806 int obj_type;
808 TAILQ_INIT(&refs);
810 *blob = NULL;
811 *fd = -1;
812 *binary = 0;
814 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
815 qs->folder ? "/" : "", qs->file) == -1) {
816 error = got_error_from_errno("asprintf");
817 goto done;
820 error = got_repo_map_path(&in_repo_path, repo, path);
821 if (error)
822 goto done;
824 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
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(&commit_id, repo, commit, in_repo_path);
834 if (error)
835 goto done;
837 if (commit_id == NULL) {
838 error = got_error(GOT_ERR_NO_OBJ);
839 goto done;
842 error = got_object_get_type(&obj_type, repo, commit_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, commit_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 free(in_repo_path);
877 free(commit_id);
878 free(path);
879 return error;
882 int
883 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
884 int (*cb)(struct template *, const char *, size_t))
886 const struct got_error *err;
887 char *line = NULL;
888 size_t linesize = 0;
889 size_t lineno = 0;
890 ssize_t linelen = 0;
892 for (;;) {
893 err = got_object_blob_getline(&line, &linelen, &linesize,
894 blob);
895 if (err || linelen == -1)
896 break;
897 lineno++;
898 if (cb(tp, line, lineno) == -1) {
899 err = got_error(GOT_ERR_CANCELLED);
900 break;
904 free(line);
906 if (err) {
907 log_warnx("%s: got_object_blob_getline failed: %s",
908 __func__, err->msg);
909 return -1;
911 return 0;
914 struct blame_cb_args {
915 struct blame_line *lines;
916 int nlines;
917 int nlines_prec;
918 int lineno_cur;
919 off_t *line_offsets;
920 FILE *f;
921 struct got_repository *repo;
922 struct request *c;
923 got_render_blame_line_cb cb;
924 };
926 static const struct got_error *
927 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
928 struct got_commit_object *commit, struct got_object_id *id)
930 const struct got_error *err = NULL;
931 struct blame_cb_args *a = arg;
932 struct blame_line *bline;
933 struct request *c = a->c;
934 char *line = NULL;
935 size_t linesize = 0;
936 off_t offset;
937 struct tm tm;
938 time_t committer_time;
940 if (nlines != a->nlines ||
941 (lineno != -1 && lineno < 1) || lineno > a->nlines)
942 return got_error(GOT_ERR_RANGE);
944 if (lineno == -1)
945 return NULL; /* no change in this commit */
947 /* Annotate this line. */
948 bline = &a->lines[lineno - 1];
949 if (bline->annotated)
950 return NULL;
951 err = got_object_id_str(&bline->id_str, id);
952 if (err)
953 return err;
955 bline->committer = strdup(got_object_commit_get_committer(commit));
956 if (bline->committer == NULL) {
957 err = got_error_from_errno("strdup");
958 goto done;
961 committer_time = got_object_commit_get_committer_time(commit);
962 if (gmtime_r(&committer_time, &tm) == NULL)
963 return got_error_from_errno("gmtime_r");
964 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
965 &tm) == 0) {
966 err = got_error(GOT_ERR_NO_SPACE);
967 goto done;
969 bline->annotated = 1;
971 /* Print lines annotated so far. */
972 bline = &a->lines[a->lineno_cur - 1];
973 if (!bline->annotated)
974 goto done;
976 offset = a->line_offsets[a->lineno_cur - 1];
977 if (fseeko(a->f, offset, SEEK_SET) == -1) {
978 err = got_error_from_errno("fseeko");
979 goto done;
982 while (a->lineno_cur <= a->nlines && bline->annotated) {
983 if (getline(&line, &linesize, a->f) == -1) {
984 if (ferror(a->f))
985 err = got_error_from_errno("getline");
986 break;
989 if (a->cb(c->tp, line, bline, a->nlines_prec,
990 a->lineno_cur) == -1) {
991 err = got_error(GOT_ERR_CANCELLED);
992 break;
995 a->lineno_cur++;
996 bline = &a->lines[a->lineno_cur - 1];
998 done:
999 free(line);
1000 return err;
1003 const struct got_error *
1004 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1006 const struct got_error *error = NULL;
1007 struct transport *t = c->t;
1008 struct got_repository *repo = t->repo;
1009 struct querystring *qs = c->t->qs;
1010 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1011 struct got_commit_object *commit = NULL;
1012 struct got_reflist_head refs;
1013 struct got_blob_object *blob = NULL;
1014 char *path = NULL, *in_repo_path = NULL;
1015 struct blame_cb_args bca;
1016 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1017 int fd6 = -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%s", qs->folder ? qs->folder : "",
1027 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], &fd1);
1059 if (error)
1060 goto done;
1062 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1063 if (error)
1064 goto done;
1066 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
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], &fd3);
1095 if (error)
1096 goto done;
1098 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1099 if (error)
1100 goto done;
1102 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1103 if (error)
1104 goto done;
1106 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
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 fd3, fd4, 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 (fd2 != -1 && close(fd2) == -1 && error == NULL)
1125 error = got_error_from_errno("close");
1126 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1127 error = got_error_from_errno("close");
1128 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1129 error = got_error_from_errno("close");
1130 if (bca.f) {
1131 const struct got_error *bca_err =
1132 got_gotweb_flushfile(bca.f, fd1);
1133 if (error == NULL)
1134 error = bca_err;
1136 if (f1) {
1137 const struct got_error *f1_err =
1138 got_gotweb_flushfile(f1, fd5);
1139 if (error == NULL)
1140 error = f1_err;
1142 if (f2) {
1143 const struct got_error *f2_err =
1144 got_gotweb_flushfile(f2, fd6);
1145 if (error == NULL)
1146 error = f2_err;
1148 if (commit)
1149 got_object_commit_close(commit);
1150 if (blob)
1151 got_object_blob_close(blob);
1152 free(in_repo_path);
1153 free(commit_id);
1154 free(obj_id);
1155 free(path);
1156 got_ref_list_free(&refs);
1157 return error;
1160 const struct got_error *
1161 got_open_diff_for_output(FILE **fp, int *fd, struct request *c)
1163 const struct got_error *error = NULL;
1164 struct transport *t = c->t;
1165 struct got_repository *repo = t->repo;
1166 struct repo_commit *rc = NULL;
1167 struct got_object_id *id1 = NULL, *id2 = NULL;
1168 struct got_reflist_head refs;
1169 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1170 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1172 *fp = NULL;
1173 *fd = -1;
1175 TAILQ_INIT(&refs);
1177 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1178 if (error)
1179 return error;
1181 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1182 if (error)
1183 return error;
1185 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1186 if (error)
1187 return error;
1189 rc = TAILQ_FIRST(&t->repo_commits);
1191 if (rc->parent_id != NULL &&
1192 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1193 error = got_repo_match_object_id(&id1, NULL,
1194 rc->parent_id, GOT_OBJ_TYPE_ANY,
1195 &refs, repo);
1196 if (error)
1197 goto done;
1200 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1201 GOT_OBJ_TYPE_ANY, &refs, repo);
1202 if (error)
1203 goto done;
1205 error = got_object_get_type(&obj_type, repo, id2);
1206 if (error)
1207 goto done;
1209 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1210 if (error)
1211 goto done;
1213 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1214 if (error)
1215 goto done;
1217 switch (obj_type) {
1218 case GOT_OBJ_TYPE_BLOB:
1219 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1220 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1221 NULL, repo, f3);
1222 break;
1223 case GOT_OBJ_TYPE_TREE:
1224 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1225 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1226 NULL, repo, f3);
1227 break;
1228 case GOT_OBJ_TYPE_COMMIT:
1229 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1230 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1231 NULL, repo, f3);
1232 break;
1233 default:
1234 error = got_error(GOT_ERR_OBJ_TYPE);
1236 if (error)
1237 goto done;
1239 if (fseek(f1, 0, SEEK_SET) == -1) {
1240 error = got_ferror(f1, GOT_ERR_IO);
1241 goto done;
1244 if (fseek(f2, 0, SEEK_SET) == -1) {
1245 error = got_ferror(f2, GOT_ERR_IO);
1246 goto done;
1249 if (fseek(f3, 0, SEEK_SET) == -1) {
1250 error = got_ferror(f3, GOT_ERR_IO);
1251 goto done;
1254 *fp = f3;
1255 *fd = fd3;
1257 done:
1258 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1259 error = got_error_from_errno("close");
1260 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1261 error = got_error_from_errno("close");
1262 if (f1) {
1263 const struct got_error *f1_err =
1264 got_gotweb_flushfile(f1, fd1);
1265 if (error == NULL)
1266 error = f1_err;
1268 if (f2) {
1269 const struct got_error *f2_err =
1270 got_gotweb_flushfile(f2, fd2);
1271 if (error == NULL)
1272 error = f2_err;
1274 if (error && f3) {
1275 got_gotweb_flushfile(f3, fd3);
1276 *fp = NULL;
1277 *fd = -1;
1279 got_ref_list_free(&refs);
1280 free(id1);
1281 free(id2);
1282 return error;
1285 static const struct got_error *
1286 got_init_repo_commit(struct repo_commit **rc)
1288 *rc = calloc(1, sizeof(**rc));
1289 if (*rc == NULL)
1290 return got_error_from_errno2("%s: calloc", __func__);
1292 (*rc)->path = NULL;
1293 (*rc)->refs_str = NULL;
1294 (*rc)->commit_id = NULL;
1295 (*rc)->committer = NULL;
1296 (*rc)->author = NULL;
1297 (*rc)->parent_id = NULL;
1298 (*rc)->tree_id = NULL;
1299 (*rc)->commit_msg = NULL;
1301 return NULL;
1304 static const struct got_error *
1305 got_init_repo_tag(struct repo_tag **rt)
1307 *rt = calloc(1, sizeof(**rt));
1308 if (*rt == NULL)
1309 return got_error_from_errno2("%s: calloc", __func__);
1311 (*rt)->commit_id = NULL;
1312 (*rt)->tag_name = NULL;
1313 (*rt)->tag_commit = NULL;
1314 (*rt)->commit_msg = NULL;
1315 (*rt)->tagger = NULL;
1317 return NULL;