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_bfsort(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 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
464 char *tag_commit = NULL, *tag_commit0 = NULL;
465 char *commit_msg = NULL, *commit_msg0 = NULL;
466 int chk_next = 0, chk_multi = 1, commit_found = 0;
468 TAILQ_INIT(&refs);
470 if (limit == 0)
471 return got_error(GOT_ERR_RANGE);
473 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
474 repo_dir->name) == -1)
475 return got_error_from_errno("asprintf");
477 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
478 error = got_ref_open(&ref, repo, qs->headref, 0);
479 if (error)
480 goto done;
481 error = got_ref_resolve(&id, repo, ref);
482 got_ref_close(ref);
483 if (error)
484 goto done;
485 } else if (qs->commit == NULL && qs->action == TAG) {
486 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
487 goto done;
488 } else {
489 error = got_repo_match_object_id_prefix(&id, qs->commit,
490 GOT_OBJ_TYPE_COMMIT, repo);
491 if (error)
492 goto done;
495 if (qs->action != SUMMARY && qs->action != TAGS) {
496 error = got_object_open_as_commit(&commit, repo, id);
497 if (error)
498 goto done;
499 error = got_object_commit_get_logmsg(&commit_msg0, commit);
500 if (error)
501 goto done;
502 if (commit) {
503 got_object_commit_close(commit);
504 commit = NULL;
508 error = got_repo_map_path(&in_repo_path, repo, repo_path);
509 if (error)
510 goto done;
512 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
513 repo);
514 if (error)
515 goto done;
517 if (limit == 1)
518 chk_multi = 0;
520 /*
521 * XXX: again, see previous message about caching
522 */
524 TAILQ_FOREACH(re, &refs, entry) {
525 struct repo_tag *new_repo_tag = NULL;
526 error = got_init_repo_tag(&new_repo_tag);
527 if (error)
528 goto done;
530 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
532 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
533 if (new_repo_tag->tag_name == NULL) {
534 error = got_error_from_errno("strdup");
535 goto done;
538 free(id);
539 id = NULL;
541 free(id_str);
542 id_str = NULL;
544 error = got_ref_resolve(&id, repo, re->ref);
545 if (error)
546 goto done;
548 if (tag)
549 got_object_tag_close(tag);
550 error = got_object_open_as_tag(&tag, repo, id);
551 if (error) {
552 if (error->code != GOT_ERR_OBJ_TYPE)
553 goto done;
554 /* "lightweight" tag */
555 error = got_object_open_as_commit(&commit, repo, id);
556 if (error)
557 goto done;
558 new_repo_tag->tagger =
559 strdup(got_object_commit_get_committer(commit));
560 if (new_repo_tag->tagger == NULL) {
561 error = got_error_from_errno("strdup");
562 goto done;
564 new_repo_tag->tagger_time =
565 got_object_commit_get_committer_time(commit);
566 error = got_object_id_str(&id_str, id);
567 if (error)
568 goto done;
569 } else {
570 new_repo_tag->tagger =
571 strdup(got_object_tag_get_tagger(tag));
572 if (new_repo_tag->tagger == NULL) {
573 error = got_error_from_errno("strdup");
574 goto done;
576 new_repo_tag->tagger_time =
577 got_object_tag_get_tagger_time(tag);
578 error = got_object_id_str(&id_str,
579 got_object_tag_get_object_id(tag));
580 if (error)
581 goto done;
584 new_repo_tag->commit_id = strdup(id_str);
585 if (new_repo_tag->commit_id == NULL)
586 goto done;
588 if (commit_found == 0 && qs->commit != NULL &&
589 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
590 continue;
591 else
592 commit_found = 1;
594 t->tag_count++;
596 /*
597 * check for one more commit before breaking,
598 * so we know whether to navigate through briefs
599 * commits and summary
600 */
601 if (chk_next) {
602 t->tags_more_id = strdup(new_repo_tag->commit_id);
603 if (t->tags_more_id == NULL) {
604 error = got_error_from_errno("strdup");
605 goto done;
607 if (commit) {
608 got_object_commit_close(commit);
609 commit = NULL;
611 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
612 gotweb_free_repo_tag(new_repo_tag);
613 goto done;
616 if (commit) {
617 error = got_object_commit_get_logmsg(&tag_commit0,
618 commit);
619 if (error)
620 goto done;
621 got_object_commit_close(commit);
622 commit = NULL;
623 } else {
624 tag_commit0 = strdup(got_object_tag_get_message(tag));
625 if (tag_commit0 == NULL) {
626 error = got_error_from_errno("strdup");
627 goto done;
631 tag_commit = tag_commit0;
632 while (*tag_commit == '\n')
633 tag_commit++;
634 new_repo_tag->tag_commit = strdup(tag_commit);
635 if (new_repo_tag->tag_commit == NULL) {
636 error = got_error_from_errno("strdup");
637 free(tag_commit0);
638 goto done;
640 free(tag_commit0);
642 if (qs->action != SUMMARY && qs->action != TAGS) {
643 commit_msg = commit_msg0;
644 while (*commit_msg == '\n')
645 commit_msg++;
647 new_repo_tag->commit_msg = strdup(commit_msg);
648 if (new_repo_tag->commit_msg == NULL) {
649 error = got_error_from_errno("strdup");
650 goto done;
654 if (limit && --limit == 0) {
655 if (chk_multi == 0)
656 break;
657 chk_next = 1;
661 done:
662 if (commit)
663 got_object_commit_close(commit);
664 if (tag)
665 got_object_tag_close(tag);
666 got_ref_list_free(&refs);
667 free(commit_msg0);
668 free(in_repo_path);
669 free(repo_path);
670 free(id);
671 free(id_str);
672 return error;
675 int
676 got_output_repo_tree(struct request *c, char **readme,
677 int (*cb)(struct template *, struct got_tree_entry *))
679 const struct got_error *error = NULL;
680 struct transport *t = c->t;
681 struct got_commit_object *commit = NULL;
682 struct got_repository *repo = t->repo;
683 struct querystring *qs = t->qs;
684 struct repo_commit *rc = NULL;
685 struct got_object_id *tree_id = NULL, *commit_id = NULL;
686 struct got_reflist_head refs;
687 struct got_tree_object *tree = NULL;
688 struct got_tree_entry *te;
689 struct repo_dir *repo_dir = t->repo_dir;
690 const char *name;
691 mode_t mode;
692 char *escaped_name = NULL, *path = NULL;
693 int nentries, i;
695 TAILQ_INIT(&refs);
696 *readme = NULL;
698 rc = TAILQ_FIRST(&t->repo_commits);
700 if (qs->folder != NULL) {
701 path = strdup(qs->folder);
702 if (path == NULL) {
703 error = got_error_from_errno("strdup");
704 goto done;
706 } else {
707 error = got_repo_map_path(&path, repo, repo_dir->path);
708 if (error)
709 goto done;
712 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
713 GOT_OBJ_TYPE_COMMIT, &refs, repo);
714 if (error)
715 goto done;
717 error = got_object_open_as_commit(&commit, repo, commit_id);
718 if (error)
719 goto done;
721 error = got_object_id_by_path(&tree_id, repo, commit, path);
722 if (error)
723 goto done;
725 error = got_object_open_as_tree(&tree, repo, tree_id);
726 if (error)
727 goto done;
729 nentries = got_object_tree_get_nentries(tree);
731 for (i = 0; i < nentries; i++) {
732 te = got_object_tree_get_entry(tree, i);
734 name = got_tree_entry_get_name(te);
735 mode = got_tree_entry_get_mode(te);
736 if (!S_ISDIR(mode) && (!strcasecmp(name, "README") ||
737 !strcasecmp(name, "README.md") ||
738 !strcasecmp(name, "README.txt"))) {
739 free(*readme);
740 *readme = strdup(name);
743 if (cb(c->tp, te) == -1) {
744 error = got_error(GOT_ERR_CANCELLED);
745 break;
748 done:
749 free(escaped_name);
750 free(path);
751 got_ref_list_free(&refs);
752 if (commit)
753 got_object_commit_close(commit);
754 if (tree)
755 got_object_tree_close(tree);
756 free(commit_id);
757 free(tree_id);
758 if (error) {
759 free(*readme);
760 *readme = NULL;
761 if (error->code != GOT_ERR_CANCELLED)
762 log_warnx("%s: %s", __func__, error->msg);
763 return -1;
765 return 0;
768 const struct got_error *
769 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
770 int *binary, struct request *c, const char *directory, const char *file,
771 const char *commitstr)
773 const struct got_error *error = NULL;
774 struct got_repository *repo = c->t->repo;
775 struct got_commit_object *commit = NULL;
776 struct got_object_id *commit_id = NULL;
777 struct got_reflist_head refs;
778 char *path = NULL, *in_repo_path = NULL;
779 int obj_type;
781 TAILQ_INIT(&refs);
783 *blob = NULL;
784 *fd = -1;
785 *binary = 0;
787 error = got_ref_list(&refs, repo, "refs/heads",
788 got_ref_cmp_by_name, NULL);
789 if (error)
790 goto done;
792 if (asprintf(&path, "%s%s%s", directory ? directory : "",
793 directory ? "/" : "", file) == -1) {
794 error = got_error_from_errno("asprintf");
795 goto done;
798 error = got_repo_map_path(&in_repo_path, repo, path);
799 if (error)
800 goto done;
802 if (commitstr == NULL)
803 commitstr = GOT_REF_HEAD;
805 error = got_repo_match_object_id(&commit_id, NULL, commitstr,
806 GOT_OBJ_TYPE_COMMIT, &refs, repo);
807 if (error)
808 goto done;
810 error = got_object_open_as_commit(&commit, repo, commit_id);
811 if (error)
812 goto done;
814 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
815 if (error)
816 goto done;
818 if (commit_id == NULL) {
819 error = got_error(GOT_ERR_NO_OBJ);
820 goto done;
823 error = got_object_get_type(&obj_type, repo, commit_id);
824 if (error)
825 goto done;
827 if (obj_type != GOT_OBJ_TYPE_BLOB) {
828 error = got_error(GOT_ERR_OBJ_TYPE);
829 goto done;
832 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
833 if (error)
834 goto done;
836 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
837 if (error)
838 goto done;
840 error = got_object_blob_is_binary(binary, *blob);
841 if (error)
842 goto done;
844 done:
845 if (commit)
846 got_object_commit_close(commit);
848 if (error) {
849 if (*fd != -1)
850 close(*fd);
851 if (*blob)
852 got_object_blob_close(*blob);
853 *fd = -1;
854 *blob = NULL;
857 got_ref_list_free(&refs);
858 free(in_repo_path);
859 free(commit_id);
860 free(path);
861 return error;
864 int
865 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
866 int (*cb)(struct template *, const char *, size_t))
868 const struct got_error *err;
869 char *line = NULL;
870 size_t linesize = 0;
871 size_t lineno = 0;
872 ssize_t linelen = 0;
874 for (;;) {
875 err = got_object_blob_getline(&line, &linelen, &linesize,
876 blob);
877 if (err || linelen == -1)
878 break;
879 lineno++;
880 if (cb(tp, line, lineno) == -1) {
881 err = got_error(GOT_ERR_CANCELLED);
882 break;
886 free(line);
888 if (err) {
889 if (err->code != GOT_ERR_CANCELLED)
890 log_warnx("%s: got_object_blob_getline failed: %s",
891 __func__, err->msg);
892 return -1;
894 return 0;
897 struct blame_cb_args {
898 struct blame_line *lines;
899 int nlines;
900 int nlines_prec;
901 int lineno_cur;
902 off_t *line_offsets;
903 FILE *f;
904 struct got_repository *repo;
905 struct request *c;
906 got_render_blame_line_cb cb;
907 };
909 static const struct got_error *
910 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
911 struct got_commit_object *commit, struct got_object_id *id)
913 const struct got_error *err = NULL;
914 struct blame_cb_args *a = arg;
915 struct blame_line *bline;
916 struct request *c = a->c;
917 char *line = NULL;
918 size_t linesize = 0;
919 off_t offset;
920 struct tm tm;
921 time_t committer_time;
923 if (nlines != a->nlines ||
924 (lineno != -1 && lineno < 1) || lineno > a->nlines)
925 return got_error(GOT_ERR_RANGE);
927 if (lineno == -1)
928 return NULL; /* no change in this commit */
930 /* Annotate this line. */
931 bline = &a->lines[lineno - 1];
932 if (bline->annotated)
933 return NULL;
934 err = got_object_id_str(&bline->id_str, id);
935 if (err)
936 return err;
938 bline->committer = strdup(got_object_commit_get_committer(commit));
939 if (bline->committer == NULL) {
940 err = got_error_from_errno("strdup");
941 goto done;
944 committer_time = got_object_commit_get_committer_time(commit);
945 if (gmtime_r(&committer_time, &tm) == NULL)
946 return got_error_from_errno("gmtime_r");
947 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
948 &tm) == 0) {
949 err = got_error(GOT_ERR_NO_SPACE);
950 goto done;
952 bline->annotated = 1;
954 /* Print lines annotated so far. */
955 bline = &a->lines[a->lineno_cur - 1];
956 if (!bline->annotated)
957 goto done;
959 offset = a->line_offsets[a->lineno_cur - 1];
960 if (fseeko(a->f, offset, SEEK_SET) == -1) {
961 err = got_error_from_errno("fseeko");
962 goto done;
965 while (a->lineno_cur <= a->nlines && bline->annotated) {
966 if (getline(&line, &linesize, a->f) == -1) {
967 if (ferror(a->f))
968 err = got_error_from_errno("getline");
969 break;
972 if (a->cb(c->tp, line, bline, a->nlines_prec,
973 a->lineno_cur) == -1) {
974 err = got_error(GOT_ERR_CANCELLED);
975 break;
978 a->lineno_cur++;
979 bline = &a->lines[a->lineno_cur - 1];
981 done:
982 free(line);
983 return err;
986 const struct got_error *
987 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
989 const struct got_error *error = NULL;
990 struct transport *t = c->t;
991 struct got_repository *repo = t->repo;
992 struct querystring *qs = c->t->qs;
993 struct got_object_id *obj_id = NULL, *commit_id = NULL;
994 struct got_commit_object *commit = NULL;
995 struct got_reflist_head refs;
996 struct got_blob_object *blob = NULL;
997 char *path = NULL, *in_repo_path = NULL;
998 struct blame_cb_args bca;
999 int i, obj_type, blobfd = -1, fd1 = -1, fd2 = -1;
1000 off_t filesize;
1001 FILE *f1 = NULL, *f2 = NULL;
1003 TAILQ_INIT(&refs);
1004 bca.f = NULL;
1005 bca.lines = NULL;
1006 bca.cb = cb;
1008 if (asprintf(&path, "%s/%s", qs->folder, qs->file) == -1) {
1009 error = got_error_from_errno("asprintf");
1010 goto done;
1013 error = got_repo_map_path(&in_repo_path, repo, path);
1014 if (error)
1015 goto done;
1017 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1018 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1019 if (error)
1020 goto done;
1022 error = got_object_open_as_commit(&commit, repo, commit_id);
1023 if (error)
1024 goto done;
1026 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1027 if (error)
1028 goto done;
1030 error = got_object_get_type(&obj_type, repo, obj_id);
1031 if (error)
1032 goto done;
1034 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1035 error = got_error(GOT_ERR_OBJ_TYPE);
1036 goto done;
1039 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1]);
1040 if (error)
1041 goto done;
1043 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &blobfd);
1044 if (error)
1045 goto done;
1047 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, blobfd);
1048 if (error)
1049 goto done;
1051 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1052 &bca.line_offsets, bca.f, blob);
1053 if (error || bca.nlines == 0)
1054 goto done;
1056 /* Don't include \n at EOF in the blame line count. */
1057 if (bca.line_offsets[bca.nlines - 1] == filesize)
1058 bca.nlines--;
1060 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1061 if (bca.lines == NULL) {
1062 error = got_error_from_errno("calloc");
1063 goto done;
1065 bca.lineno_cur = 1;
1066 bca.nlines_prec = 0;
1067 i = bca.nlines;
1068 while (i > 0) {
1069 i /= 10;
1070 bca.nlines_prec++;
1072 bca.repo = repo;
1073 bca.c = c;
1075 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd1);
1076 if (error)
1077 goto done;
1079 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd2);
1080 if (error)
1081 goto done;
1083 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5]);
1084 if (error)
1085 goto done;
1087 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6]);
1088 if (error)
1089 goto done;
1091 error = got_blame(in_repo_path, commit_id, repo,
1092 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1093 fd1, fd2, f1, f2);
1095 done:
1096 if (bca.lines) {
1097 free(bca.line_offsets);
1098 for (i = 0; i < bca.nlines; i++) {
1099 struct blame_line *bline = &bca.lines[i];
1100 free(bline->id_str);
1101 free(bline->committer);
1104 free(bca.lines);
1105 if (blobfd != -1 && close(blobfd) == -1 && error == NULL)
1106 error = got_error_from_errno("close");
1107 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1108 error = got_error_from_errno("close");
1109 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1110 error = got_error_from_errno("close");
1111 if (bca.f) {
1112 const struct got_error *bca_err = got_gotweb_closefile(bca.f);
1113 if (error == NULL)
1114 error = bca_err;
1116 if (f1) {
1117 const struct got_error *f1_err = got_gotweb_closefile(f1);
1118 if (error == NULL)
1119 error = f1_err;
1121 if (f2) {
1122 const struct got_error *f2_err = got_gotweb_closefile(f2);
1123 if (error == NULL)
1124 error = f2_err;
1126 if (commit)
1127 got_object_commit_close(commit);
1128 if (blob)
1129 got_object_blob_close(blob);
1130 free(in_repo_path);
1131 free(commit_id);
1132 free(obj_id);
1133 free(path);
1134 got_ref_list_free(&refs);
1135 return error;
1138 const struct got_error *
1139 got_open_diff_for_output(FILE **fp, struct request *c)
1141 const struct got_error *error = NULL;
1142 struct transport *t = c->t;
1143 struct got_repository *repo = t->repo;
1144 struct repo_commit *rc = NULL;
1145 struct got_object_id *id1 = NULL, *id2 = NULL;
1146 struct got_reflist_head refs;
1147 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1148 int obj_type, fd1 = -1, fd2 = -1;
1150 *fp = NULL;
1152 TAILQ_INIT(&refs);
1154 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1]);
1155 if (error)
1156 return error;
1158 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2]);
1159 if (error)
1160 return error;
1162 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3]);
1163 if (error)
1164 return error;
1166 rc = TAILQ_FIRST(&t->repo_commits);
1168 if (rc->parent_id != NULL &&
1169 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1170 error = got_repo_match_object_id(&id1, NULL,
1171 rc->parent_id, GOT_OBJ_TYPE_ANY,
1172 &refs, repo);
1173 if (error)
1174 goto done;
1177 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1178 GOT_OBJ_TYPE_ANY, &refs, repo);
1179 if (error)
1180 goto done;
1182 error = got_object_get_type(&obj_type, repo, id2);
1183 if (error)
1184 goto done;
1186 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd1);
1187 if (error)
1188 goto done;
1190 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd2);
1191 if (error)
1192 goto done;
1194 switch (obj_type) {
1195 case GOT_OBJ_TYPE_BLOB:
1196 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd1, fd2,
1197 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1198 NULL, repo, f3);
1199 break;
1200 case GOT_OBJ_TYPE_TREE:
1201 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
1202 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1203 NULL, repo, f3);
1204 break;
1205 case GOT_OBJ_TYPE_COMMIT:
1206 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd1,
1207 fd2, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1208 NULL, repo, f3);
1209 break;
1210 default:
1211 error = got_error(GOT_ERR_OBJ_TYPE);
1213 if (error)
1214 goto done;
1216 if (fseek(f3, 0, SEEK_SET) == -1) {
1217 error = got_ferror(f3, GOT_ERR_IO);
1218 goto done;
1221 *fp = f3;
1223 done:
1224 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
1225 error = got_error_from_errno("close");
1226 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1227 error = got_error_from_errno("close");
1228 if (f1) {
1229 const struct got_error *f1_err = got_gotweb_closefile(f1);
1230 if (error == NULL)
1231 error = f1_err;
1233 if (f2) {
1234 const struct got_error *f2_err = got_gotweb_closefile(f2);
1235 if (error == NULL)
1236 error = f2_err;
1238 if (error && f3) {
1239 got_gotweb_closefile(f3);
1240 *fp = NULL;
1242 got_ref_list_free(&refs);
1243 free(id1);
1244 free(id2);
1245 return error;
1248 static const struct got_error *
1249 got_init_repo_commit(struct repo_commit **rc)
1251 *rc = calloc(1, sizeof(**rc));
1252 if (*rc == NULL)
1253 return got_error_from_errno2(__func__, "calloc");
1255 (*rc)->path = NULL;
1256 (*rc)->refs_str = NULL;
1257 (*rc)->commit_id = NULL;
1258 (*rc)->committer = NULL;
1259 (*rc)->author = NULL;
1260 (*rc)->parent_id = NULL;
1261 (*rc)->tree_id = NULL;
1262 (*rc)->commit_msg = NULL;
1264 return NULL;
1267 static const struct got_error *
1268 got_init_repo_tag(struct repo_tag **rt)
1270 *rt = calloc(1, sizeof(**rt));
1271 if (*rt == NULL)
1272 return got_error_from_errno2(__func__, "calloc");
1274 (*rt)->commit_id = NULL;
1275 (*rt)->tag_name = NULL;
1276 (*rt)->tag_commit = NULL;
1277 (*rt)->commit_msg = NULL;
1278 (*rt)->tagger = NULL;
1280 return NULL;