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 <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_reference.h"
31 #include "got_repository.h"
32 #include "got_path.h"
33 #include "got_cancel.h"
34 #include "got_diff.h"
35 #include "got_commit_graph.h"
36 #include "got_blame.h"
37 #include "got_privsep.h"
39 #include "got_compat.h"
41 #include "proc.h"
42 #include "gotwebd.h"
44 static const struct got_error *got_init_repo_commit(struct repo_commit **);
45 static const struct got_error *got_init_repo_tag(struct repo_tag **);
46 static const struct got_error *got_get_repo_commit(struct request *,
47 struct repo_commit *, struct got_commit_object *, struct got_reflist_head *,
48 struct got_object_id *);
49 static const struct got_error *got_gotweb_dupfd(int *, int *);
50 static const struct got_error *got_gotweb_openfile(FILE **, int *, int *);
51 static const struct got_error *got_gotweb_blame_cb(void *, int, int,
52 struct got_commit_object *,struct got_object_id *);
54 const struct got_error *
55 got_gotweb_flushfile(FILE *f, int fd)
56 {
57 if (fseek(f, 0, SEEK_SET) == -1)
58 return got_error_from_errno("fseek");
60 if (ftruncate(fd, 0) == -1)
61 return got_error_from_errno("ftruncate");
63 if (fsync(fd) == -1)
64 return got_error_from_errno("fsync");
66 if (f && fclose(f) == EOF)
67 return got_error_from_errno("fclose");
69 if (fd != -1 && close(fd) != -1)
70 return got_error_from_errno("close");
72 return NULL;
73 }
75 static const struct got_error *
76 got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
77 {
78 *fd = dup(*priv_fd);
79 if (*fd == -1)
80 return got_error_from_errno("dup");
82 *f = fdopen(*fd, "w+");
83 if (*f == NULL) {
84 close(*fd);
85 return got_error(GOT_ERR_PRIVSEP_NO_FD);
86 }
88 return NULL;
89 }
91 static const struct got_error *
92 got_gotweb_dupfd(int *priv_fd, int *fd)
93 {
94 *fd = dup(*priv_fd);
96 if (*fd == -1)
97 return got_error_from_errno("dup");
99 return NULL;
102 const struct got_error *
103 got_get_repo_owner(char **owner, struct request *c)
105 struct server *srv = c->srv;
106 struct transport *t = c->t;
107 struct got_repository *repo = t->repo;
108 const char *gitconfig_owner;
110 *owner = NULL;
112 if (srv->show_repo_owner == 0)
113 return NULL;
115 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
116 if (gitconfig_owner) {
117 *owner = strdup(gitconfig_owner);
118 if (*owner == NULL)
119 return got_error_from_errno("strdup");
120 } else {
121 *owner = strdup("");
122 if (*owner == NULL)
123 return got_error_from_errno("strdup");
125 return NULL;
128 const struct got_error *
129 got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
131 const struct got_error *error = NULL;
132 struct server *srv = c->srv;
133 struct transport *t = c->t;
134 struct got_repository *repo = t->repo;
135 struct got_commit_object *commit = NULL;
136 struct got_reflist_head refs;
137 struct got_reflist_entry *re;
138 time_t committer_time = 0, cmp_time = 0;
140 TAILQ_INIT(&refs);
142 if (srv->show_repo_age == 0)
143 return NULL;
145 error = got_ref_list(&refs, repo, "refs/heads",
146 got_ref_cmp_by_name, NULL);
147 if (error)
148 goto done;
150 /*
151 * Find the youngest branch tip in the repository, or the age of
152 * the a specific branch tip if a name was provided by the caller.
153 */
154 TAILQ_FOREACH(re, &refs, entry) {
155 struct got_object_id *id = NULL;
157 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
158 continue;
160 error = got_ref_resolve(&id, repo, re->ref);
161 if (error)
162 goto done;
164 error = got_object_open_as_commit(&commit, repo, id);
165 free(id);
166 if (error)
167 goto done;
169 committer_time =
170 got_object_commit_get_committer_time(commit);
171 got_object_commit_close(commit);
172 if (cmp_time < committer_time)
173 cmp_time = committer_time;
175 if (refname)
176 break;
179 if (cmp_time != 0)
180 *repo_age = cmp_time;
181 done:
182 got_ref_list_free(&refs);
183 return error;
186 static const struct got_error *
187 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
188 struct got_commit_object *commit, struct got_reflist_head *refs,
189 struct got_object_id *id)
191 const struct got_error *error = NULL;
192 struct got_reflist_entry *re;
193 struct got_object_id *id2 = NULL;
194 struct got_object_qid *parent_id;
195 struct transport *t = c->t;
196 struct querystring *qs = c->t->qs;
197 char *commit_msg = NULL, *commit_msg0;
199 TAILQ_FOREACH(re, refs, entry) {
200 char *s;
201 const char *name;
202 struct got_tag_object *tag = NULL;
203 struct got_object_id *ref_id;
204 int cmp;
206 if (got_ref_is_symbolic(re->ref))
207 continue;
209 name = got_ref_get_name(re->ref);
210 if (strncmp(name, "refs/", 5) == 0)
211 name += 5;
212 if (strncmp(name, "got/", 4) == 0)
213 continue;
214 if (strncmp(name, "heads/", 6) == 0)
215 name += 6;
216 if (strncmp(name, "remotes/", 8) == 0) {
217 name += 8;
218 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
219 continue;
221 error = got_ref_resolve(&ref_id, t->repo, re->ref);
222 if (error)
223 return error;
224 if (strncmp(name, "tags/", 5) == 0) {
225 error = got_object_open_as_tag(&tag, t->repo, ref_id);
226 if (error) {
227 if (error->code != GOT_ERR_OBJ_TYPE) {
228 free(ref_id);
229 continue;
231 /*
232 * Ref points at something other
233 * than a tag.
234 */
235 error = NULL;
236 tag = NULL;
239 cmp = got_object_id_cmp(tag ?
240 got_object_tag_get_object_id(tag) : ref_id, id);
241 free(ref_id);
242 if (tag)
243 got_object_tag_close(tag);
244 if (cmp != 0)
245 continue;
246 s = repo_commit->refs_str;
247 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
248 s ? ", " : "", name) == -1) {
249 error = got_error_from_errno("asprintf");
250 free(s);
251 repo_commit->refs_str = NULL;
252 return error;
254 free(s);
257 error = got_object_id_str(&repo_commit->commit_id, id);
258 if (error)
259 return error;
261 error = got_object_id_str(&repo_commit->tree_id,
262 got_object_commit_get_tree_id(commit));
263 if (error)
264 return error;
266 if (qs->action == DIFF) {
267 parent_id = STAILQ_FIRST(
268 got_object_commit_get_parent_ids(commit));
269 if (parent_id != NULL) {
270 id2 = got_object_id_dup(&parent_id->id);
271 error = got_object_id_str(&repo_commit->parent_id, id2);
272 if (error)
273 return error;
274 free(id2);
275 } else {
276 repo_commit->parent_id = strdup("/dev/null");
277 if (repo_commit->parent_id == NULL) {
278 error = got_error_from_errno("strdup");
279 return error;
284 repo_commit->committer_time =
285 got_object_commit_get_committer_time(commit);
287 repo_commit->author =
288 strdup(got_object_commit_get_author(commit));
289 if (repo_commit->author == NULL) {
290 error = got_error_from_errno("strdup");
291 return error;
293 repo_commit->committer =
294 strdup(got_object_commit_get_committer(commit));
295 if (repo_commit->committer == NULL) {
296 error = got_error_from_errno("strdup");
297 return error;
299 error = got_object_commit_get_logmsg(&commit_msg0, commit);
300 if (error)
301 return error;
303 commit_msg = commit_msg0;
304 while (*commit_msg == '\n')
305 commit_msg++;
307 repo_commit->commit_msg = strdup(commit_msg);
308 if (repo_commit->commit_msg == NULL)
309 error = got_error_from_errno("strdup");
310 free(commit_msg0);
311 return error;
314 const struct got_error *
315 got_get_repo_commits(struct request *c, int limit)
317 const struct got_error *error = NULL;
318 struct got_object_id *id = NULL;
319 struct got_commit_graph *graph = NULL;
320 struct got_commit_object *commit = NULL;
321 struct got_reflist_head refs;
322 struct got_reference *ref = NULL;
323 struct repo_commit *repo_commit = NULL;
324 struct server *srv = c->srv;
325 struct transport *t = c->t;
326 struct got_repository *repo = t->repo;
327 struct querystring *qs = t->qs;
328 struct repo_dir *repo_dir = t->repo_dir;
329 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
330 int chk_next = 0, chk_multi = 0;
332 TAILQ_INIT(&refs);
334 if (qs->file != NULL && strlen(qs->file) > 0)
335 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
336 qs->file) == -1)
337 return got_error_from_errno("asprintf");
339 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
340 repo_dir->name) == -1) {
341 error = got_error_from_errno("asprintf");
342 goto done;
345 if (qs->commit) {
346 error = got_repo_match_object_id_prefix(&id, qs->commit,
347 GOT_OBJ_TYPE_COMMIT, repo);
348 if (error)
349 goto done;
350 } else {
351 error = got_ref_open(&ref, repo, qs->headref, 0);
352 if (error)
353 goto done;
355 error = got_ref_resolve(&id, repo, ref);
356 if (error)
357 goto done;
360 error = got_repo_map_path(&in_repo_path, repo, repo_path);
361 if (error)
362 goto done;
364 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
365 if (error)
366 goto done;
368 if (qs->file != NULL && strlen(qs->file) > 0) {
369 error = got_commit_graph_open(&graph, file_path, 0);
370 if (error)
371 goto done;
372 } else {
373 error = got_commit_graph_open(&graph, in_repo_path, 0);
374 if (error)
375 goto done;
378 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
379 if (error)
380 goto done;
382 for (;;) {
383 struct got_object_id next_id;
385 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
386 NULL);
387 if (error) {
388 if (error->code == GOT_ERR_ITER_COMPLETED)
389 error = NULL;
390 goto done;
393 error = got_object_open_as_commit(&commit, repo, &next_id);
394 if (error)
395 goto done;
397 error = got_init_repo_commit(&repo_commit);
398 if (error)
399 goto done;
401 error = got_get_repo_commit(c, repo_commit, commit,
402 &refs, &next_id);
403 if (error) {
404 gotweb_free_repo_commit(repo_commit);
405 goto done;
408 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
410 if (!chk_multi || limit != 1 ||
411 srv->max_commits_display == 1) {
412 chk_multi = 1;
414 /*
415 * check for one more commit before breaking,
416 * so we know whether to navigate through briefs
417 * commits and summary
418 */
419 if (chk_next && (qs->action == BRIEFS ||
420 qs->action == COMMITS || qs->action == SUMMARY)) {
421 t->more_id = strdup(repo_commit->commit_id);
422 if (t->more_id == NULL) {
423 error = got_error_from_errno("strdup");
424 goto done;
426 got_object_commit_close(commit);
427 commit = NULL;
428 TAILQ_REMOVE(&t->repo_commits, repo_commit,
429 entry);
430 gotweb_free_repo_commit(repo_commit);
431 goto done;
434 if (error || (limit && --limit == 0)) {
435 if (qs->file != NULL && *qs->file != '\0')
436 if (chk_multi == 0)
437 break;
438 chk_next = 1;
440 if (commit) {
441 got_object_commit_close(commit);
442 commit = NULL;
445 done:
446 if (ref)
447 got_ref_close(ref);
448 if (commit)
449 got_object_commit_close(commit);
450 if (graph)
451 got_commit_graph_close(graph);
452 got_ref_list_free(&refs);
453 free(in_repo_path);
454 free(file_path);
455 free(repo_path);
456 free(id);
457 return error;
460 const struct got_error *
461 got_get_repo_tags(struct request *c, int limit)
463 const struct got_error *error = NULL;
464 struct got_object_id *id = NULL;
465 struct got_commit_object *commit = NULL;
466 struct got_reflist_head refs;
467 struct got_reference *ref;
468 struct got_reflist_entry *re;
469 struct server *srv = c->srv;
470 struct transport *t = c->t;
471 struct got_repository *repo = t->repo;
472 struct querystring *qs = t->qs;
473 struct repo_dir *repo_dir = t->repo_dir;
474 struct got_tag_object *tag = NULL;
475 struct repo_tag *rt = NULL, *trt = NULL;
476 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
477 char *tag_commit = NULL, *tag_commit0 = NULL;
478 char *commit_msg = NULL, *commit_msg0 = NULL;
479 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
481 TAILQ_INIT(&refs);
483 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
484 repo_dir->name) == -1)
485 return got_error_from_errno("asprintf");
487 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
488 error = got_ref_open(&ref, repo, qs->headref, 0);
489 if (error)
490 goto err;
491 error = got_ref_resolve(&id, repo, ref);
492 got_ref_close(ref);
493 if (error)
494 goto err;
495 } else if (qs->commit == NULL && qs->action == TAG) {
496 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
497 goto err;
498 } else {
499 error = got_repo_match_object_id_prefix(&id, qs->commit,
500 GOT_OBJ_TYPE_COMMIT, repo);
501 if (error)
502 goto err;
505 if (qs->action != SUMMARY && qs->action != TAGS) {
506 error = got_object_open_as_commit(&commit, repo, id);
507 if (error)
508 goto err;
509 error = got_object_commit_get_logmsg(&commit_msg0, commit);
510 if (error)
511 goto err;
512 if (commit) {
513 got_object_commit_close(commit);
514 commit = NULL;
518 error = got_repo_map_path(&in_repo_path, repo, repo_path);
519 if (error)
520 goto err;
522 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
523 repo);
524 if (error)
525 goto err;
527 if (limit == 1)
528 chk_multi = 0;
530 /*
531 * XXX: again, see previous message about caching
532 */
534 TAILQ_FOREACH(re, &refs, entry) {
535 struct repo_tag *new_repo_tag = NULL;
536 error = got_init_repo_tag(&new_repo_tag);
537 if (error)
538 goto err;
540 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
542 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
543 if (new_repo_tag->tag_name == NULL) {
544 error = got_error_from_errno("strdup");
545 goto err;
548 free(id);
549 id = NULL;
551 free(id_str);
552 id_str = NULL;
554 error = got_ref_resolve(&id, repo, re->ref);
555 if (error)
556 goto done;
558 if (tag)
559 got_object_tag_close(tag);
560 error = got_object_open_as_tag(&tag, repo, id);
561 if (error) {
562 if (error->code != GOT_ERR_OBJ_TYPE)
563 goto done;
564 /* "lightweight" tag */
565 error = got_object_open_as_commit(&commit, repo, id);
566 if (error)
567 goto done;
568 new_repo_tag->tagger =
569 strdup(got_object_commit_get_committer(commit));
570 if (new_repo_tag->tagger == NULL) {
571 error = got_error_from_errno("strdup");
572 goto err;
574 new_repo_tag->tagger_time =
575 got_object_commit_get_committer_time(commit);
576 error = got_object_id_str(&id_str, id);
577 if (error)
578 goto err;
579 } else {
580 new_repo_tag->tagger =
581 strdup(got_object_tag_get_tagger(tag));
582 if (new_repo_tag->tagger == NULL) {
583 error = got_error_from_errno("strdup");
584 goto err;
586 new_repo_tag->tagger_time =
587 got_object_tag_get_tagger_time(tag);
588 error = got_object_id_str(&id_str,
589 got_object_tag_get_object_id(tag));
590 if (error)
591 goto err;
594 new_repo_tag->commit_id = strdup(id_str);
595 if (new_repo_tag->commit_id == NULL)
596 goto err;
598 if (commit_found == 0 && qs->commit != NULL &&
599 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
600 continue;
601 else
602 commit_found = 1;
604 t->tag_count++;
606 /*
607 * check for one more commit before breaking,
608 * so we know whether to navigate through briefs
609 * commits and summary
610 */
611 if (chk_next) {
612 t->next_id = strdup(new_repo_tag->commit_id);
613 if (t->next_id == NULL) {
614 error = got_error_from_errno("strdup");
615 goto err;
617 if (commit) {
618 got_object_commit_close(commit);
619 commit = NULL;
621 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
622 gotweb_free_repo_tag(new_repo_tag);
623 goto done;
626 if (commit) {
627 error = got_object_commit_get_logmsg(&tag_commit0,
628 commit);
629 if (error)
630 goto err;
631 got_object_commit_close(commit);
632 commit = NULL;
633 } else {
634 tag_commit0 = strdup(got_object_tag_get_message(tag));
635 if (tag_commit0 == NULL) {
636 error = got_error_from_errno("strdup");
637 goto err;
641 tag_commit = tag_commit0;
642 while (*tag_commit == '\n')
643 tag_commit++;
644 new_repo_tag->tag_commit = strdup(tag_commit);
645 if (new_repo_tag->tag_commit == NULL) {
646 error = got_error_from_errno("strdup");
647 free(tag_commit0);
648 goto err;
650 free(tag_commit0);
652 if (qs->action != SUMMARY && qs->action != TAGS) {
653 commit_msg = commit_msg0;
654 while (*commit_msg == '\n')
655 commit_msg++;
657 new_repo_tag->commit_msg = strdup(commit_msg);
658 if (new_repo_tag->commit_msg == NULL) {
659 error = got_error_from_errno("strdup");
660 goto err;
664 if (limit && --limit == 0) {
665 if (chk_multi == 0)
666 break;
667 chk_next = 1;
671 done:
672 /*
673 * we have tailq populated, so find previous commit id
674 * for navigation through briefs and commits
675 */
676 if (t->tag_count == 0) {
677 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
678 TAILQ_REMOVE(&t->repo_tags, rt, entry);
679 gotweb_free_repo_tag(rt);
682 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
683 commit_found = 0;
684 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
685 entry) {
686 if (commit_found == 0 && rt->commit_id != NULL &&
687 strcmp(qs->commit, rt->commit_id) != 0) {
688 continue;
689 } else
690 commit_found = 1;
691 if (c_cnt == srv->max_commits_display ||
692 rt == TAILQ_FIRST(&t->repo_tags)) {
693 t->prev_id = strdup(rt->commit_id);
694 if (t->prev_id == NULL)
695 error = got_error_from_errno("strdup");
696 break;
698 c_cnt++;
701 err:
702 if (commit)
703 got_object_commit_close(commit);
704 if (tag)
705 got_object_tag_close(tag);
706 got_ref_list_free(&refs);
707 free(commit_msg0);
708 free(in_repo_path);
709 free(repo_path);
710 free(id);
711 free(id_str);
712 return error;
715 int
716 got_output_repo_tree(struct request *c,
717 int (*cb)(struct template *, struct got_tree_entry *))
719 const struct got_error *error = NULL;
720 struct transport *t = c->t;
721 struct got_commit_object *commit = NULL;
722 struct got_repository *repo = t->repo;
723 struct querystring *qs = t->qs;
724 struct repo_commit *rc = NULL;
725 struct got_object_id *tree_id = NULL, *commit_id = NULL;
726 struct got_reflist_head refs;
727 struct got_tree_object *tree = NULL;
728 struct got_tree_entry *te;
729 struct repo_dir *repo_dir = t->repo_dir;
730 char *escaped_name = NULL, *path = NULL;
731 int nentries, i;
733 TAILQ_INIT(&refs);
735 rc = TAILQ_FIRST(&t->repo_commits);
737 if (qs->folder != NULL) {
738 path = strdup(qs->folder);
739 if (path == NULL) {
740 error = got_error_from_errno("strdup");
741 goto done;
743 } else {
744 error = got_repo_map_path(&path, repo, repo_dir->path);
745 if (error)
746 goto done;
749 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
750 GOT_OBJ_TYPE_COMMIT, &refs, repo);
751 if (error)
752 goto done;
754 error = got_object_open_as_commit(&commit, repo, commit_id);
755 if (error)
756 goto done;
758 error = got_object_id_by_path(&tree_id, repo, commit, path);
759 if (error)
760 goto done;
762 error = got_object_open_as_tree(&tree, repo, tree_id);
763 if (error)
764 goto done;
766 nentries = got_object_tree_get_nentries(tree);
768 for (i = 0; i < nentries; i++) {
769 te = got_object_tree_get_entry(tree, i);
770 if (cb(c->tp, te) == -1)
771 break;
773 done:
774 free(escaped_name);
775 free(path);
776 got_ref_list_free(&refs);
777 if (commit)
778 got_object_commit_close(commit);
779 if (tree)
780 got_object_tree_close(tree);
781 free(commit_id);
782 free(tree_id);
783 if (error) {
784 log_warnx("%s: %s", __func__, error->msg);
785 return -1;
787 return 0;
790 const struct got_error *
791 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
792 int *binary, struct request *c)
794 const struct got_error *error = NULL;
795 struct transport *t = c->t;
796 struct got_repository *repo = t->repo;
797 struct querystring *qs = c->t->qs;
798 struct got_commit_object *commit = NULL;
799 struct got_object_id *commit_id = NULL;
800 struct got_reflist_head refs;
801 char *path = NULL, *in_repo_path = NULL;
802 int obj_type;
804 TAILQ_INIT(&refs);
806 *blob = NULL;
807 *fd = -1;
808 *binary = 0;
810 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
811 qs->folder ? "/" : "", qs->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 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
821 GOT_OBJ_TYPE_COMMIT, &refs, repo);
822 if (error)
823 goto done;
825 error = got_object_open_as_commit(&commit, repo, commit_id);
826 if (error)
827 goto done;
829 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
830 if (error)
831 goto done;
833 if (commit_id == NULL) {
834 error = got_error(GOT_ERR_NO_OBJ);
835 goto done;
838 error = got_object_get_type(&obj_type, repo, commit_id);
839 if (error)
840 goto done;
842 if (obj_type != GOT_OBJ_TYPE_BLOB) {
843 error = got_error(GOT_ERR_OBJ_TYPE);
844 goto done;
847 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
848 if (error)
849 goto done;
851 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
852 if (error)
853 goto done;
855 error = got_object_blob_is_binary(binary, *blob);
856 if (error)
857 goto done;
859 done:
860 if (commit)
861 got_object_commit_close(commit);
863 if (error) {
864 if (*fd != -1)
865 close(*fd);
866 if (*blob)
867 got_object_blob_close(*blob);
868 *fd = -1;
869 *blob = NULL;
872 free(in_repo_path);
873 free(commit_id);
874 free(path);
875 return error;
878 int
879 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
880 int (*cb)(struct template *, const char *, size_t))
882 const struct got_error *err;
883 char *line = NULL;
884 size_t linesize = 0;
885 size_t lineno = 0;
886 ssize_t linelen = 0;
888 for (;;) {
889 err = got_object_blob_getline(&line, &linelen, &linesize,
890 blob);
891 if (err || linelen == -1)
892 break;
893 lineno++;
894 if (cb(tp, line, lineno) == -1)
895 break;
898 free(line);
900 if (err) {
901 log_warnx("%s: got_object_blob_getline failed: %s",
902 __func__, err->msg);
903 return -1;
905 return 0;
908 struct blame_cb_args {
909 struct blame_line *lines;
910 int nlines;
911 int nlines_prec;
912 int lineno_cur;
913 off_t *line_offsets;
914 FILE *f;
915 struct got_repository *repo;
916 struct request *c;
917 got_render_blame_line_cb cb;
918 };
920 static const struct got_error *
921 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
922 struct got_commit_object *commit, struct got_object_id *id)
924 const struct got_error *err = NULL;
925 struct blame_cb_args *a = arg;
926 struct blame_line *bline;
927 struct request *c = a->c;
928 char *line = NULL;
929 size_t linesize = 0;
930 off_t offset;
931 struct tm tm;
932 time_t committer_time;
934 if (nlines != a->nlines ||
935 (lineno != -1 && lineno < 1) || lineno > a->nlines)
936 return got_error(GOT_ERR_RANGE);
938 if (lineno == -1)
939 return NULL; /* no change in this commit */
941 /* Annotate this line. */
942 bline = &a->lines[lineno - 1];
943 if (bline->annotated)
944 return NULL;
945 err = got_object_id_str(&bline->id_str, id);
946 if (err)
947 return err;
949 bline->committer = strdup(got_object_commit_get_committer(commit));
950 if (bline->committer == NULL) {
951 err = got_error_from_errno("strdup");
952 goto done;
955 committer_time = got_object_commit_get_committer_time(commit);
956 if (gmtime_r(&committer_time, &tm) == NULL)
957 return got_error_from_errno("gmtime_r");
958 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
959 &tm) == 0) {
960 err = got_error(GOT_ERR_NO_SPACE);
961 goto done;
963 bline->annotated = 1;
965 /* Print lines annotated so far. */
966 bline = &a->lines[a->lineno_cur - 1];
967 if (!bline->annotated)
968 goto done;
970 offset = a->line_offsets[a->lineno_cur - 1];
971 if (fseeko(a->f, offset, SEEK_SET) == -1) {
972 err = got_error_from_errno("fseeko");
973 goto done;
976 while (a->lineno_cur <= a->nlines && bline->annotated) {
977 if (getline(&line, &linesize, a->f) == -1) {
978 if (ferror(a->f))
979 err = got_error_from_errno("getline");
980 break;
983 if (a->cb(c->tp, line, bline, a->nlines_prec,
984 a->lineno_cur) == -1)
985 break;
987 a->lineno_cur++;
988 bline = &a->lines[a->lineno_cur - 1];
990 done:
991 free(line);
992 return err;
995 const struct got_error *
996 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
998 const struct got_error *error = NULL;
999 struct transport *t = c->t;
1000 struct got_repository *repo = t->repo;
1001 struct querystring *qs = c->t->qs;
1002 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1003 struct got_commit_object *commit = NULL;
1004 struct got_reflist_head refs;
1005 struct got_blob_object *blob = NULL;
1006 char *path = NULL, *in_repo_path = NULL;
1007 struct blame_cb_args bca;
1008 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1009 int fd6 = -1;
1010 off_t filesize;
1011 FILE *f1 = NULL, *f2 = NULL;
1013 TAILQ_INIT(&refs);
1014 bca.f = NULL;
1015 bca.lines = NULL;
1016 bca.cb = cb;
1018 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1019 qs->folder ? "/" : "", qs->file) == -1) {
1020 error = got_error_from_errno("asprintf");
1021 goto done;
1024 error = got_repo_map_path(&in_repo_path, repo, path);
1025 if (error)
1026 goto done;
1028 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1029 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1030 if (error)
1031 goto done;
1033 error = got_object_open_as_commit(&commit, repo, commit_id);
1034 if (error)
1035 goto done;
1037 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1038 if (error)
1039 goto done;
1041 error = got_object_get_type(&obj_type, repo, obj_id);
1042 if (error)
1043 goto done;
1045 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1046 error = got_error(GOT_ERR_OBJ_TYPE);
1047 goto done;
1050 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1051 if (error)
1052 goto done;
1054 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1055 if (error)
1056 goto done;
1058 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1059 if (error)
1060 goto done;
1062 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1063 &bca.line_offsets, bca.f, blob);
1064 if (error || bca.nlines == 0)
1065 goto done;
1067 /* Don't include \n at EOF in the blame line count. */
1068 if (bca.line_offsets[bca.nlines - 1] == filesize)
1069 bca.nlines--;
1071 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1072 if (bca.lines == NULL) {
1073 error = got_error_from_errno("calloc");
1074 goto done;
1076 bca.lineno_cur = 1;
1077 bca.nlines_prec = 0;
1078 i = bca.nlines;
1079 while (i > 0) {
1080 i /= 10;
1081 bca.nlines_prec++;
1083 bca.repo = repo;
1084 bca.c = c;
1086 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1087 if (error)
1088 goto done;
1090 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1091 if (error)
1092 goto done;
1094 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1095 if (error)
1096 goto done;
1098 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1099 if (error)
1100 goto done;
1102 error = got_blame(in_repo_path, commit_id, repo,
1103 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1104 fd3, fd4, f1, f2);
1106 done:
1107 if (bca.lines) {
1108 free(bca.line_offsets);
1109 for (i = 0; i < bca.nlines; i++) {
1110 struct blame_line *bline = &bca.lines[i];
1111 free(bline->id_str);
1112 free(bline->committer);
1115 free(bca.lines);
1116 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1117 error = got_error_from_errno("close");
1118 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1119 error = got_error_from_errno("close");
1120 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1121 error = got_error_from_errno("close");
1122 if (bca.f) {
1123 const struct got_error *bca_err =
1124 got_gotweb_flushfile(bca.f, fd1);
1125 if (error == NULL)
1126 error = bca_err;
1128 if (f1) {
1129 const struct got_error *f1_err =
1130 got_gotweb_flushfile(f1, fd5);
1131 if (error == NULL)
1132 error = f1_err;
1134 if (f2) {
1135 const struct got_error *f2_err =
1136 got_gotweb_flushfile(f2, fd6);
1137 if (error == NULL)
1138 error = f2_err;
1140 if (commit)
1141 got_object_commit_close(commit);
1142 if (blob)
1143 got_object_blob_close(blob);
1144 free(in_repo_path);
1145 free(commit_id);
1146 free(obj_id);
1147 free(path);
1148 got_ref_list_free(&refs);
1149 return error;
1152 const struct got_error *
1153 got_open_diff_for_output(FILE **fp, int *fd, struct request *c)
1155 const struct got_error *error = NULL;
1156 struct transport *t = c->t;
1157 struct got_repository *repo = t->repo;
1158 struct repo_commit *rc = NULL;
1159 struct got_object_id *id1 = NULL, *id2 = NULL;
1160 struct got_reflist_head refs;
1161 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1162 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1164 *fp = NULL;
1165 *fd = -1;
1167 TAILQ_INIT(&refs);
1169 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1170 if (error)
1171 return error;
1173 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1174 if (error)
1175 return error;
1177 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1178 if (error)
1179 return error;
1181 rc = TAILQ_FIRST(&t->repo_commits);
1183 if (rc->parent_id != NULL &&
1184 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1185 error = got_repo_match_object_id(&id1, NULL,
1186 rc->parent_id, GOT_OBJ_TYPE_ANY,
1187 &refs, repo);
1188 if (error)
1189 goto done;
1192 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1193 GOT_OBJ_TYPE_ANY, &refs, repo);
1194 if (error)
1195 goto done;
1197 error = got_object_get_type(&obj_type, repo, id2);
1198 if (error)
1199 goto done;
1201 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1202 if (error)
1203 goto done;
1205 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1206 if (error)
1207 goto done;
1209 switch (obj_type) {
1210 case GOT_OBJ_TYPE_BLOB:
1211 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1212 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1213 NULL, repo, f3);
1214 break;
1215 case GOT_OBJ_TYPE_TREE:
1216 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1217 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1218 NULL, repo, f3);
1219 break;
1220 case GOT_OBJ_TYPE_COMMIT:
1221 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1222 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1223 NULL, repo, f3);
1224 break;
1225 default:
1226 error = got_error(GOT_ERR_OBJ_TYPE);
1228 if (error)
1229 goto done;
1231 if (fseek(f1, 0, SEEK_SET) == -1) {
1232 error = got_ferror(f1, GOT_ERR_IO);
1233 goto done;
1236 if (fseek(f2, 0, SEEK_SET) == -1) {
1237 error = got_ferror(f2, GOT_ERR_IO);
1238 goto done;
1241 if (fseek(f3, 0, SEEK_SET) == -1) {
1242 error = got_ferror(f3, GOT_ERR_IO);
1243 goto done;
1246 *fp = f3;
1247 *fd = fd3;
1249 done:
1250 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1251 error = got_error_from_errno("close");
1252 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1253 error = got_error_from_errno("close");
1254 if (f1) {
1255 const struct got_error *f1_err =
1256 got_gotweb_flushfile(f1, fd1);
1257 if (error == NULL)
1258 error = f1_err;
1260 if (f2) {
1261 const struct got_error *f2_err =
1262 got_gotweb_flushfile(f2, fd2);
1263 if (error == NULL)
1264 error = f2_err;
1266 if (error && f3) {
1267 got_gotweb_flushfile(f3, fd3);
1268 *fp = NULL;
1269 *fd = -1;
1271 got_ref_list_free(&refs);
1272 free(id1);
1273 free(id2);
1274 return error;
1277 static const struct got_error *
1278 got_init_repo_commit(struct repo_commit **rc)
1280 *rc = calloc(1, sizeof(**rc));
1281 if (*rc == NULL)
1282 return got_error_from_errno2("%s: calloc", __func__);
1284 (*rc)->path = NULL;
1285 (*rc)->refs_str = NULL;
1286 (*rc)->commit_id = NULL;
1287 (*rc)->committer = NULL;
1288 (*rc)->author = NULL;
1289 (*rc)->parent_id = NULL;
1290 (*rc)->tree_id = NULL;
1291 (*rc)->commit_msg = NULL;
1293 return NULL;
1296 static const struct got_error *
1297 got_init_repo_tag(struct repo_tag **rt)
1299 *rt = calloc(1, sizeof(**rt));
1300 if (*rt == NULL)
1301 return got_error_from_errno2("%s: calloc", __func__);
1303 (*rt)->commit_id = NULL;
1304 (*rt)->tag_name = NULL;
1305 (*rt)->tag_commit = NULL;
1306 (*rt)->commit_msg = NULL;
1307 (*rt)->tagger = NULL;
1309 return NULL;