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 <sha1.h>
25 #include <sha2.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"
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 *);
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_closefile(FILE *f)
56 {
57 const struct got_error *err = NULL;
59 if (fseek(f, 0, SEEK_SET) == -1)
60 err = got_error_from_errno("fseek");
62 if (err == NULL && ftruncate(fileno(f), 0) == -1)
63 err = got_error_from_errno("ftruncate");
65 if (fclose(f) == EOF && err == NULL)
66 err = got_error_from_errno("fclose");
68 return err;
69 }
71 static const struct got_error *
72 got_gotweb_openfile(FILE **f, int *priv_fd)
73 {
74 int fd;
76 fd = dup(*priv_fd);
77 if (fd == -1)
78 return got_error_from_errno("dup");
80 *f = fdopen(fd, "w+");
81 if (*f == NULL) {
82 close(fd);
83 return got_error(GOT_ERR_PRIVSEP_NO_FD);
84 }
86 return NULL;
87 }
89 static const struct got_error *
90 got_gotweb_dupfd(int *priv_fd, int *fd)
91 {
92 *fd = dup(*priv_fd);
94 if (*fd == -1)
95 return got_error_from_errno("dup");
97 return NULL;
98 }
100 const struct got_error *
101 got_get_repo_owner(char **owner, struct request *c)
103 struct server *srv = c->srv;
104 struct transport *t = c->t;
105 struct got_repository *repo = t->repo;
106 const char *gitconfig_owner;
108 *owner = NULL;
110 if (srv->show_repo_owner == 0)
111 return NULL;
113 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
114 if (gitconfig_owner) {
115 *owner = strdup(gitconfig_owner);
116 if (*owner == NULL)
117 return got_error_from_errno("strdup");
118 } else {
119 *owner = strdup("");
120 if (*owner == NULL)
121 return got_error_from_errno("strdup");
123 return NULL;
126 const struct got_error *
127 got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
129 const struct got_error *error = NULL;
130 struct transport *t = c->t;
131 struct got_repository *repo = t->repo;
132 struct got_commit_object *commit = NULL;
133 struct got_reflist_head refs;
134 struct got_reflist_entry *re;
135 time_t committer_time = 0, cmp_time = 0;
137 TAILQ_INIT(&refs);
139 *repo_age = 0;
141 error = got_ref_list(&refs, repo, "refs/heads",
142 got_ref_cmp_by_name, NULL);
143 if (error)
144 goto done;
146 /*
147 * Find the youngest branch tip in the repository, or the age of
148 * the a specific branch tip if a name was provided by the caller.
149 */
150 TAILQ_FOREACH(re, &refs, entry) {
151 struct got_object_id *id = NULL;
153 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
154 continue;
156 error = got_ref_resolve(&id, repo, re->ref);
157 if (error)
158 goto done;
160 error = got_object_open_as_commit(&commit, repo, id);
161 free(id);
162 if (error)
163 goto done;
165 committer_time =
166 got_object_commit_get_committer_time(commit);
167 got_object_commit_close(commit);
168 if (cmp_time < committer_time)
169 cmp_time = committer_time;
171 if (refname)
172 break;
175 if (cmp_time != 0)
176 *repo_age = cmp_time;
177 done:
178 got_ref_list_free(&refs);
179 return error;
182 static const struct got_error *
183 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
184 struct got_commit_object *commit, struct got_reflist_head *refs,
185 struct got_object_id *id)
187 const struct got_error *error = NULL;
188 struct got_reflist_entry *re;
189 struct got_object_id *id2 = NULL;
190 struct got_object_qid *parent_id;
191 struct transport *t = c->t;
192 struct querystring *qs = c->t->qs;
193 char *commit_msg = NULL, *commit_msg0;
195 TAILQ_FOREACH(re, refs, entry) {
196 char *s;
197 const char *name;
198 struct got_tag_object *tag = NULL;
199 struct got_object_id *ref_id;
200 int cmp;
202 if (got_ref_is_symbolic(re->ref))
203 continue;
205 name = got_ref_get_name(re->ref);
206 if (strncmp(name, "refs/", 5) == 0)
207 name += 5;
208 if (strncmp(name, "got/", 4) == 0)
209 continue;
210 if (strncmp(name, "heads/", 6) == 0)
211 name += 6;
212 if (strncmp(name, "remotes/", 8) == 0) {
213 name += 8;
214 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
215 continue;
217 error = got_ref_resolve(&ref_id, t->repo, re->ref);
218 if (error)
219 return error;
220 if (strncmp(name, "tags/", 5) == 0) {
221 error = got_object_open_as_tag(&tag, t->repo, ref_id);
222 if (error) {
223 if (error->code != GOT_ERR_OBJ_TYPE) {
224 free(ref_id);
225 continue;
227 /*
228 * Ref points at something other
229 * than a tag.
230 */
231 error = NULL;
232 tag = NULL;
235 cmp = got_object_id_cmp(tag ?
236 got_object_tag_get_object_id(tag) : ref_id, id);
237 free(ref_id);
238 if (tag)
239 got_object_tag_close(tag);
240 if (cmp != 0)
241 continue;
242 s = repo_commit->refs_str;
243 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
244 s ? ", " : "", name) == -1) {
245 error = got_error_from_errno("asprintf");
246 free(s);
247 repo_commit->refs_str = NULL;
248 return error;
250 free(s);
253 error = got_object_id_str(&repo_commit->commit_id, id);
254 if (error)
255 return error;
257 error = got_object_id_str(&repo_commit->tree_id,
258 got_object_commit_get_tree_id(commit));
259 if (error)
260 return error;
262 if (qs->action == DIFF || qs->action == PATCH) {
263 parent_id = STAILQ_FIRST(
264 got_object_commit_get_parent_ids(commit));
265 if (parent_id != NULL) {
266 id2 = got_object_id_dup(&parent_id->id);
267 error = got_object_id_str(&repo_commit->parent_id, id2);
268 if (error)
269 return error;
270 free(id2);
271 } else {
272 repo_commit->parent_id = strdup("/dev/null");
273 if (repo_commit->parent_id == NULL) {
274 error = got_error_from_errno("strdup");
275 return error;
280 repo_commit->committer_time =
281 got_object_commit_get_committer_time(commit);
283 repo_commit->author =
284 strdup(got_object_commit_get_author(commit));
285 if (repo_commit->author == NULL) {
286 error = got_error_from_errno("strdup");
287 return error;
289 repo_commit->committer =
290 strdup(got_object_commit_get_committer(commit));
291 if (repo_commit->committer == NULL) {
292 error = got_error_from_errno("strdup");
293 return error;
295 error = got_object_commit_get_logmsg(&commit_msg0, commit);
296 if (error)
297 return error;
299 commit_msg = commit_msg0;
300 while (*commit_msg == '\n')
301 commit_msg++;
303 repo_commit->commit_msg = strdup(commit_msg);
304 if (repo_commit->commit_msg == NULL)
305 error = got_error_from_errno("strdup");
306 free(commit_msg0);
307 return error;
310 const struct got_error *
311 got_get_repo_commits(struct request *c, size_t limit)
313 const struct got_error *error = NULL;
314 struct got_object_id *id = NULL;
315 struct got_commit_graph *graph = NULL;
316 struct got_commit_object *commit = NULL;
317 struct got_reflist_head refs;
318 struct got_reference *ref = NULL;
319 struct repo_commit *repo_commit = NULL;
320 struct server *srv = c->srv;
321 struct transport *t = c->t;
322 struct got_repository *repo = t->repo;
323 struct querystring *qs = t->qs;
324 struct repo_dir *repo_dir = t->repo_dir;
325 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
326 int chk_next = 0;
328 if (limit == 0)
329 return got_error(GOT_ERR_RANGE);
331 if (limit > 1) {
332 /*
333 * Traverse one commit more than requested to provide
334 * the next button.
335 */
336 limit++;
337 chk_next = 1;
340 TAILQ_INIT(&refs);
342 if (qs->file != NULL && *qs->file != '\0')
343 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
344 qs->file) == -1)
345 return got_error_from_errno("asprintf");
347 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
348 repo_dir->name) == -1) {
349 error = got_error_from_errno("asprintf");
350 goto done;
353 if (qs->commit) {
354 error = got_repo_match_object_id_prefix(&id, qs->commit,
355 GOT_OBJ_TYPE_COMMIT, repo);
356 if (error)
357 goto done;
358 } else {
359 error = got_ref_open(&ref, repo, qs->headref, 0);
360 if (error)
361 goto done;
363 error = got_ref_resolve(&id, repo, ref);
364 if (error)
365 goto done;
368 error = got_repo_map_path(&in_repo_path, repo, repo_path);
369 if (error)
370 goto done;
372 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
373 if (error)
374 goto done;
376 if (qs->file != NULL && *qs->file != '\0') {
377 error = got_commit_graph_open(&graph, file_path, 0);
378 if (error)
379 goto done;
380 } else {
381 error = got_commit_graph_open(&graph, in_repo_path, 0);
382 if (error)
383 goto done;
386 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
387 if (error)
388 goto done;
390 for (;;) {
391 struct got_object_id next_id;
393 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
394 NULL);
395 if (error) {
396 if (error->code == GOT_ERR_ITER_COMPLETED)
397 error = NULL;
398 goto done;
401 error = got_object_open_as_commit(&commit, repo, &next_id);
402 if (error)
403 goto done;
405 error = got_init_repo_commit(&repo_commit);
406 if (error)
407 goto done;
409 error = got_get_repo_commit(c, repo_commit, commit,
410 &refs, &next_id);
411 if (error) {
412 gotweb_free_repo_commit(repo_commit);
413 goto done;
416 if (--limit == 0 && chk_next) {
417 t->more_id = strdup(repo_commit->commit_id);
418 if (t->more_id == NULL)
419 error = got_error_from_errno("strdup");
420 goto done;
423 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
425 if (limit == 0)
426 goto done;
428 if (commit) {
429 got_object_commit_close(commit);
430 commit = NULL;
433 done:
434 if (ref)
435 got_ref_close(ref);
436 if (commit)
437 got_object_commit_close(commit);
438 if (graph)
439 got_commit_graph_close(graph);
440 got_ref_list_free(&refs);
441 free(in_repo_path);
442 free(file_path);
443 free(repo_path);
444 free(id);
445 return error;
448 const struct got_error *
449 got_get_repo_tags(struct request *c, size_t limit)
451 const struct got_error *error = NULL;
452 struct got_object_id *id = NULL;
453 struct got_commit_object *commit = NULL;
454 struct got_reflist_head refs;
455 struct got_reference *ref;
456 struct got_reflist_entry *re;
457 struct server *srv = c->srv;
458 struct transport *t = c->t;
459 struct got_repository *repo = t->repo;
460 struct querystring *qs = t->qs;
461 struct repo_dir *repo_dir = t->repo_dir;
462 struct got_tag_object *tag = NULL;
463 struct repo_tag *rt = NULL, *trt = NULL;
464 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
465 char *tag_commit = NULL, *tag_commit0 = NULL;
466 char *commit_msg = NULL, *commit_msg0 = NULL;
467 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
469 TAILQ_INIT(&refs);
471 if (limit == 0)
472 return got_error(GOT_ERR_RANGE);
474 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
475 repo_dir->name) == -1)
476 return got_error_from_errno("asprintf");
478 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
479 error = got_ref_open(&ref, repo, qs->headref, 0);
480 if (error)
481 goto err;
482 error = got_ref_resolve(&id, repo, ref);
483 got_ref_close(ref);
484 if (error)
485 goto err;
486 } else if (qs->commit == NULL && qs->action == TAG) {
487 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
488 goto err;
489 } else {
490 error = got_repo_match_object_id_prefix(&id, qs->commit,
491 GOT_OBJ_TYPE_COMMIT, repo);
492 if (error)
493 goto err;
496 if (qs->action != SUMMARY && qs->action != TAGS) {
497 error = got_object_open_as_commit(&commit, repo, id);
498 if (error)
499 goto err;
500 error = got_object_commit_get_logmsg(&commit_msg0, commit);
501 if (error)
502 goto err;
503 if (commit) {
504 got_object_commit_close(commit);
505 commit = NULL;
509 error = got_repo_map_path(&in_repo_path, repo, repo_path);
510 if (error)
511 goto err;
513 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
514 repo);
515 if (error)
516 goto err;
518 if (limit == 1)
519 chk_multi = 0;
521 /*
522 * XXX: again, see previous message about caching
523 */
525 TAILQ_FOREACH(re, &refs, entry) {
526 struct repo_tag *new_repo_tag = NULL;
527 error = got_init_repo_tag(&new_repo_tag);
528 if (error)
529 goto err;
531 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
533 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
534 if (new_repo_tag->tag_name == NULL) {
535 error = got_error_from_errno("strdup");
536 goto err;
539 free(id);
540 id = NULL;
542 free(id_str);
543 id_str = NULL;
545 error = got_ref_resolve(&id, repo, re->ref);
546 if (error)
547 goto done;
549 if (tag)
550 got_object_tag_close(tag);
551 error = got_object_open_as_tag(&tag, repo, id);
552 if (error) {
553 if (error->code != GOT_ERR_OBJ_TYPE)
554 goto done;
555 /* "lightweight" tag */
556 error = got_object_open_as_commit(&commit, repo, id);
557 if (error)
558 goto done;
559 new_repo_tag->tagger =
560 strdup(got_object_commit_get_committer(commit));
561 if (new_repo_tag->tagger == NULL) {
562 error = got_error_from_errno("strdup");
563 goto err;
565 new_repo_tag->tagger_time =
566 got_object_commit_get_committer_time(commit);
567 error = got_object_id_str(&id_str, id);
568 if (error)
569 goto err;
570 } else {
571 new_repo_tag->tagger =
572 strdup(got_object_tag_get_tagger(tag));
573 if (new_repo_tag->tagger == NULL) {
574 error = got_error_from_errno("strdup");
575 goto err;
577 new_repo_tag->tagger_time =
578 got_object_tag_get_tagger_time(tag);
579 error = got_object_id_str(&id_str,
580 got_object_tag_get_object_id(tag));
581 if (error)
582 goto err;
585 new_repo_tag->commit_id = strdup(id_str);
586 if (new_repo_tag->commit_id == NULL)
587 goto err;
589 if (commit_found == 0 && qs->commit != NULL &&
590 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
591 continue;
592 else
593 commit_found = 1;
595 t->tag_count++;
597 /*
598 * check for one more commit before breaking,
599 * so we know whether to navigate through briefs
600 * commits and summary
601 */
602 if (chk_next) {
603 t->next_id = strdup(new_repo_tag->commit_id);
604 if (t->next_id == NULL) {
605 error = got_error_from_errno("strdup");
606 goto err;
608 if (commit) {
609 got_object_commit_close(commit);
610 commit = NULL;
612 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
613 gotweb_free_repo_tag(new_repo_tag);
614 goto done;
617 if (commit) {
618 error = got_object_commit_get_logmsg(&tag_commit0,
619 commit);
620 if (error)
621 goto err;
622 got_object_commit_close(commit);
623 commit = NULL;
624 } else {
625 tag_commit0 = strdup(got_object_tag_get_message(tag));
626 if (tag_commit0 == NULL) {
627 error = got_error_from_errno("strdup");
628 goto err;
632 tag_commit = tag_commit0;
633 while (*tag_commit == '\n')
634 tag_commit++;
635 new_repo_tag->tag_commit = strdup(tag_commit);
636 if (new_repo_tag->tag_commit == NULL) {
637 error = got_error_from_errno("strdup");
638 free(tag_commit0);
639 goto err;
641 free(tag_commit0);
643 if (qs->action != SUMMARY && qs->action != TAGS) {
644 commit_msg = commit_msg0;
645 while (*commit_msg == '\n')
646 commit_msg++;
648 new_repo_tag->commit_msg = strdup(commit_msg);
649 if (new_repo_tag->commit_msg == NULL) {
650 error = got_error_from_errno("strdup");
651 goto err;
655 if (limit && --limit == 0) {
656 if (chk_multi == 0)
657 break;
658 chk_next = 1;
662 done:
663 /*
664 * we have tailq populated, so find previous commit id
665 * for navigation through briefs and commits
666 */
667 if (t->tag_count == 0) {
668 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
669 TAILQ_REMOVE(&t->repo_tags, rt, entry);
670 gotweb_free_repo_tag(rt);
673 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
674 commit_found = 0;
675 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
676 entry) {
677 if (commit_found == 0 && rt->commit_id != NULL &&
678 strcmp(qs->commit, rt->commit_id) != 0) {
679 continue;
680 } else
681 commit_found = 1;
682 if (c_cnt == srv->max_commits_display ||
683 rt == TAILQ_FIRST(&t->repo_tags)) {
684 t->prev_id = strdup(rt->commit_id);
685 if (t->prev_id == NULL)
686 error = got_error_from_errno("strdup");
687 break;
689 c_cnt++;
692 err:
693 if (commit)
694 got_object_commit_close(commit);
695 if (tag)
696 got_object_tag_close(tag);
697 got_ref_list_free(&refs);
698 free(commit_msg0);
699 free(in_repo_path);
700 free(repo_path);
701 free(id);
702 free(id_str);
703 return error;
706 int
707 got_output_repo_tree(struct request *c, char **readme,
708 int (*cb)(struct template *, struct got_tree_entry *))
710 const struct got_error *error = NULL;
711 struct transport *t = c->t;
712 struct got_commit_object *commit = NULL;
713 struct got_repository *repo = t->repo;
714 struct querystring *qs = t->qs;
715 struct repo_commit *rc = NULL;
716 struct got_object_id *tree_id = NULL, *commit_id = NULL;
717 struct got_reflist_head refs;
718 struct got_tree_object *tree = NULL;
719 struct got_tree_entry *te;
720 struct repo_dir *repo_dir = t->repo_dir;
721 const char *name;
722 mode_t mode;
723 char *escaped_name = NULL, *path = NULL;
724 int nentries, i;
726 TAILQ_INIT(&refs);
727 *readme = NULL;
729 rc = TAILQ_FIRST(&t->repo_commits);
731 if (qs->folder != NULL) {
732 path = strdup(qs->folder);
733 if (path == NULL) {
734 error = got_error_from_errno("strdup");
735 goto done;
737 } else {
738 error = got_repo_map_path(&path, repo, repo_dir->path);
739 if (error)
740 goto done;
743 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
744 GOT_OBJ_TYPE_COMMIT, &refs, repo);
745 if (error)
746 goto done;
748 error = got_object_open_as_commit(&commit, repo, commit_id);
749 if (error)
750 goto done;
752 error = got_object_id_by_path(&tree_id, repo, commit, path);
753 if (error)
754 goto done;
756 error = got_object_open_as_tree(&tree, repo, tree_id);
757 if (error)
758 goto done;
760 nentries = got_object_tree_get_nentries(tree);
762 for (i = 0; i < nentries; i++) {
763 te = got_object_tree_get_entry(tree, i);
765 name = got_tree_entry_get_name(te);
766 mode = got_tree_entry_get_mode(te);
767 if (!S_ISDIR(mode) && (!strcasecmp(name, "README") ||
768 !strcasecmp(name, "README.md") ||
769 !strcasecmp(name, "README.txt"))) {
770 free(*readme);
771 *readme = strdup(name);
774 if (cb(c->tp, te) == -1) {
775 error = got_error(GOT_ERR_CANCELLED);
776 break;
779 done:
780 free(escaped_name);
781 free(path);
782 got_ref_list_free(&refs);
783 if (commit)
784 got_object_commit_close(commit);
785 if (tree)
786 got_object_tree_close(tree);
787 free(commit_id);
788 free(tree_id);
789 if (error) {
790 free(*readme);
791 *readme = NULL;
792 if (error->code != GOT_ERR_CANCELLED)
793 log_warnx("%s: %s", __func__, error->msg);
794 return -1;
796 return 0;
799 const struct got_error *
800 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
801 int *binary, struct request *c, const char *directory, const char *file,
802 const char *commitstr)
804 const struct got_error *error = NULL;
805 struct got_repository *repo = c->t->repo;
806 struct got_commit_object *commit = NULL;
807 struct got_object_id *commit_id = NULL;
808 struct got_reflist_head refs;
809 char *path = NULL, *in_repo_path = NULL;
810 int obj_type;
812 TAILQ_INIT(&refs);
814 *blob = NULL;
815 *fd = -1;
816 *binary = 0;
818 error = got_ref_list(&refs, repo, "refs/heads",
819 got_ref_cmp_by_name, NULL);
820 if (error)
821 goto done;
823 if (asprintf(&path, "%s%s%s", directory ? directory : "",
824 directory ? "/" : "", file) == -1) {
825 error = got_error_from_errno("asprintf");
826 goto done;
829 error = got_repo_map_path(&in_repo_path, repo, path);
830 if (error)
831 goto done;
833 if (commitstr == NULL)
834 commitstr = "HEAD";
836 error = got_repo_match_object_id(&commit_id, NULL, commitstr,
837 GOT_OBJ_TYPE_COMMIT, &refs, repo);
838 if (error)
839 goto done;
841 error = got_object_open_as_commit(&commit, repo, commit_id);
842 if (error)
843 goto done;
845 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
846 if (error)
847 goto done;
849 if (commit_id == NULL) {
850 error = got_error(GOT_ERR_NO_OBJ);
851 goto done;
854 error = got_object_get_type(&obj_type, repo, commit_id);
855 if (error)
856 goto done;
858 if (obj_type != GOT_OBJ_TYPE_BLOB) {
859 error = got_error(GOT_ERR_OBJ_TYPE);
860 goto done;
863 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
864 if (error)
865 goto done;
867 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
868 if (error)
869 goto done;
871 error = got_object_blob_is_binary(binary, *blob);
872 if (error)
873 goto done;
875 done:
876 if (commit)
877 got_object_commit_close(commit);
879 if (error) {
880 if (*fd != -1)
881 close(*fd);
882 if (*blob)
883 got_object_blob_close(*blob);
884 *fd = -1;
885 *blob = NULL;
888 got_ref_list_free(&refs);
889 free(in_repo_path);
890 free(commit_id);
891 free(path);
892 return error;
895 int
896 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
897 int (*cb)(struct template *, const char *, size_t))
899 const struct got_error *err;
900 char *line = NULL;
901 size_t linesize = 0;
902 size_t lineno = 0;
903 ssize_t linelen = 0;
905 for (;;) {
906 err = got_object_blob_getline(&line, &linelen, &linesize,
907 blob);
908 if (err || linelen == -1)
909 break;
910 lineno++;
911 if (cb(tp, line, lineno) == -1) {
912 err = got_error(GOT_ERR_CANCELLED);
913 break;
917 free(line);
919 if (err) {
920 if (err->code != GOT_ERR_CANCELLED)
921 log_warnx("%s: got_object_blob_getline failed: %s",
922 __func__, err->msg);
923 return -1;
925 return 0;
928 struct blame_cb_args {
929 struct blame_line *lines;
930 int nlines;
931 int nlines_prec;
932 int lineno_cur;
933 off_t *line_offsets;
934 FILE *f;
935 struct got_repository *repo;
936 struct request *c;
937 got_render_blame_line_cb cb;
938 };
940 static const struct got_error *
941 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
942 struct got_commit_object *commit, struct got_object_id *id)
944 const struct got_error *err = NULL;
945 struct blame_cb_args *a = arg;
946 struct blame_line *bline;
947 struct request *c = a->c;
948 char *line = NULL;
949 size_t linesize = 0;
950 off_t offset;
951 struct tm tm;
952 time_t committer_time;
954 if (nlines != a->nlines ||
955 (lineno != -1 && lineno < 1) || lineno > a->nlines)
956 return got_error(GOT_ERR_RANGE);
958 if (lineno == -1)
959 return NULL; /* no change in this commit */
961 /* Annotate this line. */
962 bline = &a->lines[lineno - 1];
963 if (bline->annotated)
964 return NULL;
965 err = got_object_id_str(&bline->id_str, id);
966 if (err)
967 return err;
969 bline->committer = strdup(got_object_commit_get_committer(commit));
970 if (bline->committer == NULL) {
971 err = got_error_from_errno("strdup");
972 goto done;
975 committer_time = got_object_commit_get_committer_time(commit);
976 if (gmtime_r(&committer_time, &tm) == NULL)
977 return got_error_from_errno("gmtime_r");
978 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
979 &tm) == 0) {
980 err = got_error(GOT_ERR_NO_SPACE);
981 goto done;
983 bline->annotated = 1;
985 /* Print lines annotated so far. */
986 bline = &a->lines[a->lineno_cur - 1];
987 if (!bline->annotated)
988 goto done;
990 offset = a->line_offsets[a->lineno_cur - 1];
991 if (fseeko(a->f, offset, SEEK_SET) == -1) {
992 err = got_error_from_errno("fseeko");
993 goto done;
996 while (a->lineno_cur <= a->nlines && bline->annotated) {
997 if (getline(&line, &linesize, a->f) == -1) {
998 if (ferror(a->f))
999 err = got_error_from_errno("getline");
1000 break;
1003 if (a->cb(c->tp, line, bline, a->nlines_prec,
1004 a->lineno_cur) == -1) {
1005 err = got_error(GOT_ERR_CANCELLED);
1006 break;
1009 a->lineno_cur++;
1010 bline = &a->lines[a->lineno_cur - 1];
1012 done:
1013 free(line);
1014 return err;
1017 const struct got_error *
1018 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1020 const struct got_error *error = NULL;
1021 struct transport *t = c->t;
1022 struct got_repository *repo = t->repo;
1023 struct querystring *qs = c->t->qs;
1024 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1025 struct got_commit_object *commit = NULL;
1026 struct got_reflist_head refs;
1027 struct got_blob_object *blob = NULL;
1028 char *path = NULL, *in_repo_path = NULL;
1029 struct blame_cb_args bca;
1030 int i, obj_type, blobfd = -1, fd1 = -1, fd2 = -1;
1031 off_t filesize;
1032 FILE *f1 = NULL, *f2 = NULL;
1034 TAILQ_INIT(&refs);
1035 bca.f = NULL;
1036 bca.lines = NULL;
1037 bca.cb = cb;
1039 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1040 qs->folder ? "/" : "", qs->file) == -1) {
1041 error = got_error_from_errno("asprintf");
1042 goto done;
1045 error = got_repo_map_path(&in_repo_path, repo, path);
1046 if (error)
1047 goto done;
1049 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1050 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1051 if (error)
1052 goto done;
1054 error = got_object_open_as_commit(&commit, repo, commit_id);
1055 if (error)
1056 goto done;
1058 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1059 if (error)
1060 goto done;
1062 error = got_object_get_type(&obj_type, repo, obj_id);
1063 if (error)
1064 goto done;
1066 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1067 error = got_error(GOT_ERR_OBJ_TYPE);
1068 goto done;
1071 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1]);
1072 if (error)
1073 goto done;
1075 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &blobfd);
1076 if (error)
1077 goto done;
1079 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, blobfd);
1080 if (error)
1081 goto done;
1083 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1084 &bca.line_offsets, bca.f, blob);
1085 if (error || bca.nlines == 0)
1086 goto done;
1088 /* Don't include \n at EOF in the blame line count. */
1089 if (bca.line_offsets[bca.nlines - 1] == filesize)
1090 bca.nlines--;
1092 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1093 if (bca.lines == NULL) {
1094 error = got_error_from_errno("calloc");
1095 goto done;
1097 bca.lineno_cur = 1;
1098 bca.nlines_prec = 0;
1099 i = bca.nlines;
1100 while (i > 0) {
1101 i /= 10;
1102 bca.nlines_prec++;
1104 bca.repo = repo;
1105 bca.c = c;
1107 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd1);
1108 if (error)
1109 goto done;
1111 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd2);
1112 if (error)
1113 goto done;
1115 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5]);
1116 if (error)
1117 goto done;
1119 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6]);
1120 if (error)
1121 goto done;
1123 error = got_blame(in_repo_path, commit_id, repo,
1124 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1125 fd1, fd2, f1, f2);
1127 done:
1128 if (bca.lines) {
1129 free(bca.line_offsets);
1130 for (i = 0; i < bca.nlines; i++) {
1131 struct blame_line *bline = &bca.lines[i];
1132 free(bline->id_str);
1133 free(bline->committer);
1136 free(bca.lines);
1137 if (blobfd != -1 && close(blobfd) == -1 && error == NULL)
1138 error = got_error_from_errno("close");
1139 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1140 error = got_error_from_errno("close");
1141 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1142 error = got_error_from_errno("close");
1143 if (bca.f) {
1144 const struct got_error *bca_err = got_gotweb_closefile(bca.f);
1145 if (error == NULL)
1146 error = bca_err;
1148 if (f1) {
1149 const struct got_error *f1_err = got_gotweb_closefile(f1);
1150 if (error == NULL)
1151 error = f1_err;
1153 if (f2) {
1154 const struct got_error *f2_err = got_gotweb_closefile(f2);
1155 if (error == NULL)
1156 error = f2_err;
1158 if (commit)
1159 got_object_commit_close(commit);
1160 if (blob)
1161 got_object_blob_close(blob);
1162 free(in_repo_path);
1163 free(commit_id);
1164 free(obj_id);
1165 free(path);
1166 got_ref_list_free(&refs);
1167 return error;
1170 const struct got_error *
1171 got_open_diff_for_output(FILE **fp, struct request *c)
1173 const struct got_error *error = NULL;
1174 struct transport *t = c->t;
1175 struct got_repository *repo = t->repo;
1176 struct repo_commit *rc = NULL;
1177 struct got_object_id *id1 = NULL, *id2 = NULL;
1178 struct got_reflist_head refs;
1179 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1180 int obj_type, fd1 = -1, fd2 = -1;
1182 *fp = NULL;
1184 TAILQ_INIT(&refs);
1186 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1]);
1187 if (error)
1188 return error;
1190 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2]);
1191 if (error)
1192 return error;
1194 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3]);
1195 if (error)
1196 return error;
1198 rc = TAILQ_FIRST(&t->repo_commits);
1200 if (rc->parent_id != NULL &&
1201 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1202 error = got_repo_match_object_id(&id1, NULL,
1203 rc->parent_id, GOT_OBJ_TYPE_ANY,
1204 &refs, repo);
1205 if (error)
1206 goto done;
1209 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1210 GOT_OBJ_TYPE_ANY, &refs, repo);
1211 if (error)
1212 goto done;
1214 error = got_object_get_type(&obj_type, repo, id2);
1215 if (error)
1216 goto done;
1218 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd1);
1219 if (error)
1220 goto done;
1222 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd2);
1223 if (error)
1224 goto done;
1226 switch (obj_type) {
1227 case GOT_OBJ_TYPE_BLOB:
1228 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd1, fd2,
1229 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1230 NULL, repo, f3);
1231 break;
1232 case GOT_OBJ_TYPE_TREE:
1233 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
1234 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1235 NULL, repo, f3);
1236 break;
1237 case GOT_OBJ_TYPE_COMMIT:
1238 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd1,
1239 fd2, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1240 NULL, repo, f3);
1241 break;
1242 default:
1243 error = got_error(GOT_ERR_OBJ_TYPE);
1245 if (error)
1246 goto done;
1248 if (fseek(f3, 0, SEEK_SET) == -1) {
1249 error = got_ferror(f3, GOT_ERR_IO);
1250 goto done;
1253 *fp = f3;
1255 done:
1256 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1257 error = got_error_from_errno("close");
1258 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1259 error = got_error_from_errno("close");
1260 if (f1) {
1261 const struct got_error *f1_err = got_gotweb_closefile(f1);
1262 if (error == NULL)
1263 error = f1_err;
1265 if (f2) {
1266 const struct got_error *f2_err = got_gotweb_closefile(f2);
1267 if (error == NULL)
1268 error = f2_err;
1270 if (error && f3) {
1271 got_gotweb_closefile(f3);
1272 *fp = NULL;
1274 got_ref_list_free(&refs);
1275 free(id1);
1276 free(id2);
1277 return error;
1280 static const struct got_error *
1281 got_init_repo_commit(struct repo_commit **rc)
1283 *rc = calloc(1, sizeof(**rc));
1284 if (*rc == NULL)
1285 return got_error_from_errno2(__func__, "calloc");
1287 (*rc)->path = NULL;
1288 (*rc)->refs_str = NULL;
1289 (*rc)->commit_id = NULL;
1290 (*rc)->committer = NULL;
1291 (*rc)->author = NULL;
1292 (*rc)->parent_id = NULL;
1293 (*rc)->tree_id = NULL;
1294 (*rc)->commit_msg = NULL;
1296 return NULL;
1299 static const struct got_error *
1300 got_init_repo_tag(struct repo_tag **rt)
1302 *rt = calloc(1, sizeof(**rt));
1303 if (*rt == NULL)
1304 return got_error_from_errno2(__func__, "calloc");
1306 (*rt)->commit_id = NULL;
1307 (*rt)->tag_name = NULL;
1308 (*rt)->tag_commit = NULL;
1309 (*rt)->commit_msg = NULL;
1310 (*rt)->tagger = NULL;
1312 return NULL;