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"
43 #include "log.h"
45 static const struct got_error *got_init_repo_commit(struct repo_commit **);
46 static const struct got_error *got_init_repo_tag(struct repo_tag **);
47 static const struct got_error *got_get_repo_commit(struct request *,
48 struct repo_commit *, struct got_commit_object *, struct got_reflist_head *,
49 struct got_object_id *);
50 static const struct got_error *got_gotweb_dupfd(int *, int *);
51 static const struct got_error *got_gotweb_openfile(FILE **, int *);
52 static const struct got_error *got_gotweb_blame_cb(void *, int, int,
53 struct got_commit_object *,struct got_object_id *);
55 const struct got_error *
56 got_gotweb_closefile(FILE *f)
57 {
58 const struct got_error *err = NULL;
60 if (fseek(f, 0, SEEK_SET) == -1)
61 err = got_error_from_errno("fseek");
63 if (err == NULL && ftruncate(fileno(f), 0) == -1)
64 err = got_error_from_errno("ftruncate");
66 if (fclose(f) == EOF && err == NULL)
67 err = got_error_from_errno("fclose");
69 return err;
70 }
72 static const struct got_error *
73 got_gotweb_openfile(FILE **f, int *priv_fd)
74 {
75 int fd;
77 fd = dup(*priv_fd);
78 if (fd == -1)
79 return got_error_from_errno("dup");
81 *f = fdopen(fd, "w+");
82 if (*f == NULL) {
83 close(fd);
84 return got_error(GOT_ERR_PRIVSEP_NO_FD);
85 }
87 return NULL;
88 }
90 static const struct got_error *
91 got_gotweb_dupfd(int *priv_fd, int *fd)
92 {
93 *fd = dup(*priv_fd);
95 if (*fd == -1)
96 return got_error_from_errno("dup");
98 return NULL;
99 }
101 const struct got_error *
102 got_get_repo_owner(char **owner, struct request *c)
104 struct server *srv = c->srv;
105 struct transport *t = c->t;
106 struct got_repository *repo = t->repo;
107 const char *gitconfig_owner;
109 *owner = NULL;
111 if (srv->show_repo_owner == 0)
112 return NULL;
114 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
115 if (gitconfig_owner) {
116 *owner = strdup(gitconfig_owner);
117 if (*owner == NULL)
118 return got_error_from_errno("strdup");
119 } else {
120 *owner = strdup("");
121 if (*owner == NULL)
122 return got_error_from_errno("strdup");
124 return NULL;
127 const struct got_error *
128 got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
130 const struct got_error *error = NULL;
131 struct transport *t = c->t;
132 struct got_repository *repo = t->repo;
133 struct got_commit_object *commit = NULL;
134 struct got_reflist_head refs;
135 struct got_reflist_entry *re;
136 time_t committer_time = 0, cmp_time = 0;
138 TAILQ_INIT(&refs);
140 *repo_age = 0;
142 error = got_ref_list(&refs, repo, "refs/heads",
143 got_ref_cmp_by_name, NULL);
144 if (error)
145 goto done;
147 /*
148 * Find the youngest branch tip in the repository, or the age of
149 * the a specific branch tip if a name was provided by the caller.
150 */
151 TAILQ_FOREACH(re, &refs, entry) {
152 struct got_object_id *id = NULL;
154 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
155 continue;
157 error = got_ref_resolve(&id, repo, re->ref);
158 if (error)
159 goto done;
161 error = got_object_open_as_commit(&commit, repo, id);
162 free(id);
163 if (error)
164 goto done;
166 committer_time =
167 got_object_commit_get_committer_time(commit);
168 got_object_commit_close(commit);
169 if (cmp_time < committer_time)
170 cmp_time = committer_time;
172 if (refname)
173 break;
176 if (cmp_time != 0)
177 *repo_age = cmp_time;
178 done:
179 got_ref_list_free(&refs);
180 return error;
183 static const struct got_error *
184 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
185 struct got_commit_object *commit, struct got_reflist_head *refs,
186 struct got_object_id *id)
188 const struct got_error *error = NULL;
189 struct got_reflist_entry *re;
190 struct got_object_id *id2 = NULL;
191 struct got_object_qid *parent_id;
192 struct transport *t = c->t;
193 struct querystring *qs = c->t->qs;
194 char *commit_msg = NULL, *commit_msg0;
196 TAILQ_FOREACH(re, refs, entry) {
197 char *s;
198 const char *name;
199 struct got_tag_object *tag = NULL;
200 struct got_object_id *ref_id;
201 int cmp;
203 if (got_ref_is_symbolic(re->ref))
204 continue;
206 name = got_ref_get_name(re->ref);
207 if (strncmp(name, "refs/", 5) == 0)
208 name += 5;
209 if (strncmp(name, "got/", 4) == 0)
210 continue;
211 if (strncmp(name, "heads/", 6) == 0)
212 name += 6;
213 if (strncmp(name, "remotes/", 8) == 0) {
214 name += 8;
215 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
216 continue;
218 error = got_ref_resolve(&ref_id, t->repo, re->ref);
219 if (error)
220 return error;
221 if (strncmp(name, "tags/", 5) == 0) {
222 error = got_object_open_as_tag(&tag, t->repo, ref_id);
223 if (error) {
224 if (error->code != GOT_ERR_OBJ_TYPE) {
225 free(ref_id);
226 continue;
228 /*
229 * Ref points at something other
230 * than a tag.
231 */
232 error = NULL;
233 tag = NULL;
236 cmp = got_object_id_cmp(tag ?
237 got_object_tag_get_object_id(tag) : ref_id, id);
238 free(ref_id);
239 if (tag)
240 got_object_tag_close(tag);
241 if (cmp != 0)
242 continue;
243 s = repo_commit->refs_str;
244 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
245 s ? ", " : "", name) == -1) {
246 error = got_error_from_errno("asprintf");
247 free(s);
248 repo_commit->refs_str = NULL;
249 return error;
251 free(s);
254 error = got_object_id_str(&repo_commit->commit_id, id);
255 if (error)
256 return error;
258 error = got_object_id_str(&repo_commit->tree_id,
259 got_object_commit_get_tree_id(commit));
260 if (error)
261 return error;
263 if (qs->action == DIFF || qs->action == PATCH) {
264 parent_id = STAILQ_FIRST(
265 got_object_commit_get_parent_ids(commit));
266 if (parent_id != NULL) {
267 id2 = got_object_id_dup(&parent_id->id);
268 error = got_object_id_str(&repo_commit->parent_id, id2);
269 if (error)
270 return error;
271 free(id2);
272 } else {
273 repo_commit->parent_id = strdup("/dev/null");
274 if (repo_commit->parent_id == NULL) {
275 error = got_error_from_errno("strdup");
276 return error;
281 repo_commit->committer_time =
282 got_object_commit_get_committer_time(commit);
284 repo_commit->author =
285 strdup(got_object_commit_get_author(commit));
286 if (repo_commit->author == NULL) {
287 error = got_error_from_errno("strdup");
288 return error;
290 repo_commit->committer =
291 strdup(got_object_commit_get_committer(commit));
292 if (repo_commit->committer == NULL) {
293 error = got_error_from_errno("strdup");
294 return error;
296 error = got_object_commit_get_logmsg(&commit_msg0, commit);
297 if (error)
298 return error;
300 commit_msg = commit_msg0;
301 while (*commit_msg == '\n')
302 commit_msg++;
304 repo_commit->commit_msg = strdup(commit_msg);
305 if (repo_commit->commit_msg == NULL)
306 error = got_error_from_errno("strdup");
307 free(commit_msg0);
308 return error;
311 const struct got_error *
312 got_get_repo_commits(struct request *c, size_t limit)
314 const struct got_error *error = NULL;
315 struct got_object_id *id = NULL;
316 struct got_commit_graph *graph = NULL;
317 struct got_commit_object *commit = NULL;
318 struct got_reflist_head refs;
319 struct got_reference *ref = NULL;
320 struct repo_commit *repo_commit = NULL;
321 struct server *srv = c->srv;
322 struct transport *t = c->t;
323 struct got_repository *repo = t->repo;
324 struct querystring *qs = t->qs;
325 struct repo_dir *repo_dir = t->repo_dir;
326 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
327 int chk_next = 0;
329 if (limit == 0)
330 return got_error(GOT_ERR_RANGE);
332 if (limit > 1) {
333 /*
334 * Traverse one commit more than requested to provide
335 * the next button.
336 */
337 limit++;
338 chk_next = 1;
341 TAILQ_INIT(&refs);
343 if (qs->file != NULL && *qs->file != '\0')
344 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
345 qs->file) == -1)
346 return got_error_from_errno("asprintf");
348 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
349 repo_dir->name) == -1) {
350 error = got_error_from_errno("asprintf");
351 goto done;
354 if (qs->commit) {
355 error = got_repo_match_object_id_prefix(&id, qs->commit,
356 GOT_OBJ_TYPE_COMMIT, repo);
357 if (error)
358 goto done;
359 } else {
360 error = got_ref_open(&ref, repo, qs->headref, 0);
361 if (error)
362 goto done;
364 error = got_ref_resolve(&id, repo, ref);
365 if (error)
366 goto done;
369 error = got_repo_map_path(&in_repo_path, repo, repo_path);
370 if (error)
371 goto done;
373 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
374 if (error)
375 goto done;
377 if (qs->file != NULL && *qs->file != '\0') {
378 error = got_commit_graph_open(&graph, file_path, 0);
379 if (error)
380 goto done;
381 } else {
382 error = got_commit_graph_open(&graph, in_repo_path, 0);
383 if (error)
384 goto done;
387 error = got_commit_graph_bfsort(graph, id, repo, NULL, NULL);
388 if (error)
389 goto done;
391 for (;;) {
392 struct got_object_id next_id;
394 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
395 NULL);
396 if (error) {
397 if (error->code == GOT_ERR_ITER_COMPLETED)
398 error = NULL;
399 goto done;
402 error = got_object_open_as_commit(&commit, repo, &next_id);
403 if (error)
404 goto done;
406 error = got_init_repo_commit(&repo_commit);
407 if (error)
408 goto done;
410 error = got_get_repo_commit(c, repo_commit, commit,
411 &refs, &next_id);
412 if (error) {
413 gotweb_free_repo_commit(repo_commit);
414 goto done;
417 if (--limit == 0 && chk_next) {
418 t->more_id = strdup(repo_commit->commit_id);
419 if (t->more_id == NULL)
420 error = got_error_from_errno("strdup");
421 goto done;
424 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
426 if (limit == 0)
427 goto done;
429 if (commit) {
430 got_object_commit_close(commit);
431 commit = NULL;
434 done:
435 if (ref)
436 got_ref_close(ref);
437 if (commit)
438 got_object_commit_close(commit);
439 if (graph)
440 got_commit_graph_close(graph);
441 got_ref_list_free(&refs);
442 free(in_repo_path);
443 free(file_path);
444 free(repo_path);
445 free(id);
446 return error;
449 const struct got_error *
450 got_get_repo_tags(struct request *c, size_t limit)
452 const struct got_error *error = NULL;
453 struct got_object_id *id = NULL;
454 struct got_commit_object *commit = NULL;
455 struct got_reflist_head refs;
456 struct got_reference *ref;
457 struct got_reflist_entry *re;
458 struct server *srv = c->srv;
459 struct transport *t = c->t;
460 struct got_repository *repo = t->repo;
461 struct querystring *qs = t->qs;
462 struct repo_dir *repo_dir = t->repo_dir;
463 struct got_tag_object *tag = 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;
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 done;
482 error = got_ref_resolve(&id, repo, ref);
483 got_ref_close(ref);
484 if (error)
485 goto done;
486 } else if (qs->commit == NULL && qs->action == TAG) {
487 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
488 goto done;
489 } else {
490 error = got_repo_match_object_id_prefix(&id, qs->commit,
491 GOT_OBJ_TYPE_COMMIT, repo);
492 if (error)
493 goto done;
496 if (qs->action != SUMMARY && qs->action != TAGS) {
497 error = got_object_open_as_commit(&commit, repo, id);
498 if (error)
499 goto done;
500 error = got_object_commit_get_logmsg(&commit_msg0, commit);
501 if (error)
502 goto done;
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 done;
513 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
514 repo);
515 if (error)
516 goto done;
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 done;
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 done;
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 done;
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 done;
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 done;
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 done;
585 new_repo_tag->commit_id = strdup(id_str);
586 if (new_repo_tag->commit_id == NULL)
587 goto done;
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->tags_more_id = strdup(new_repo_tag->commit_id);
604 if (t->tags_more_id == NULL) {
605 error = got_error_from_errno("strdup");
606 goto done;
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 done;
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 done;
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 done;
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 done;
655 if (limit && --limit == 0) {
656 if (chk_multi == 0)
657 break;
658 chk_next = 1;
662 done:
663 if (commit)
664 got_object_commit_close(commit);
665 if (tag)
666 got_object_tag_close(tag);
667 got_ref_list_free(&refs);
668 free(commit_msg0);
669 free(in_repo_path);
670 free(repo_path);
671 free(id);
672 free(id_str);
673 return error;
676 int
677 got_output_repo_tree(struct request *c, char **readme,
678 int (*cb)(struct template *, struct got_tree_entry *))
680 const struct got_error *error = NULL;
681 struct transport *t = c->t;
682 struct got_commit_object *commit = NULL;
683 struct got_repository *repo = t->repo;
684 struct querystring *qs = t->qs;
685 struct repo_commit *rc = NULL;
686 struct got_object_id *tree_id = NULL, *commit_id = NULL;
687 struct got_reflist_head refs;
688 struct got_tree_object *tree = NULL;
689 struct got_tree_entry *te;
690 struct repo_dir *repo_dir = t->repo_dir;
691 const char *name;
692 mode_t mode;
693 char *escaped_name = NULL, *path = NULL;
694 int nentries, i;
696 TAILQ_INIT(&refs);
697 *readme = NULL;
699 rc = TAILQ_FIRST(&t->repo_commits);
701 if (qs->folder != NULL) {
702 path = strdup(qs->folder);
703 if (path == NULL) {
704 error = got_error_from_errno("strdup");
705 goto done;
707 } else {
708 error = got_repo_map_path(&path, repo, repo_dir->path);
709 if (error)
710 goto done;
713 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
714 GOT_OBJ_TYPE_COMMIT, &refs, repo);
715 if (error)
716 goto done;
718 error = got_object_open_as_commit(&commit, repo, commit_id);
719 if (error)
720 goto done;
722 error = got_object_id_by_path(&tree_id, repo, commit, path);
723 if (error)
724 goto done;
726 error = got_object_open_as_tree(&tree, repo, tree_id);
727 if (error)
728 goto done;
730 nentries = got_object_tree_get_nentries(tree);
732 for (i = 0; i < nentries; i++) {
733 te = got_object_tree_get_entry(tree, i);
735 name = got_tree_entry_get_name(te);
736 mode = got_tree_entry_get_mode(te);
737 if (!S_ISDIR(mode) && (!strcasecmp(name, "README") ||
738 !strcasecmp(name, "README.md") ||
739 !strcasecmp(name, "README.txt"))) {
740 free(*readme);
741 *readme = strdup(name);
744 if (cb(c->tp, te) == -1) {
745 error = got_error(GOT_ERR_CANCELLED);
746 break;
749 done:
750 free(escaped_name);
751 free(path);
752 got_ref_list_free(&refs);
753 if (commit)
754 got_object_commit_close(commit);
755 if (tree)
756 got_object_tree_close(tree);
757 free(commit_id);
758 free(tree_id);
759 if (error) {
760 free(*readme);
761 *readme = NULL;
762 if (error->code != GOT_ERR_CANCELLED)
763 log_warnx("%s: %s", __func__, error->msg);
764 return -1;
766 return 0;
769 const struct got_error *
770 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
771 int *binary, struct request *c, const char *directory, const char *file,
772 const char *commitstr)
774 const struct got_error *error = NULL;
775 struct got_repository *repo = c->t->repo;
776 struct got_commit_object *commit = NULL;
777 struct got_object_id *commit_id = NULL;
778 struct got_reflist_head refs;
779 char *path = NULL, *in_repo_path = NULL;
780 int obj_type;
782 TAILQ_INIT(&refs);
784 *blob = NULL;
785 *fd = -1;
786 *binary = 0;
788 error = got_ref_list(&refs, repo, "refs/heads",
789 got_ref_cmp_by_name, NULL);
790 if (error)
791 goto done;
793 if (asprintf(&path, "%s%s%s", directory ? directory : "",
794 directory ? "/" : "", file) == -1) {
795 error = got_error_from_errno("asprintf");
796 goto done;
799 error = got_repo_map_path(&in_repo_path, repo, path);
800 if (error)
801 goto done;
803 if (commitstr == NULL)
804 commitstr = GOT_REF_HEAD;
806 error = got_repo_match_object_id(&commit_id, NULL, commitstr,
807 GOT_OBJ_TYPE_COMMIT, &refs, repo);
808 if (error)
809 goto done;
811 error = got_object_open_as_commit(&commit, repo, commit_id);
812 if (error)
813 goto done;
815 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
816 if (error)
817 goto done;
819 if (commit_id == NULL) {
820 error = got_error(GOT_ERR_NO_OBJ);
821 goto done;
824 error = got_object_get_type(&obj_type, repo, commit_id);
825 if (error)
826 goto done;
828 if (obj_type != GOT_OBJ_TYPE_BLOB) {
829 error = got_error(GOT_ERR_OBJ_TYPE);
830 goto done;
833 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
834 if (error)
835 goto done;
837 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
838 if (error)
839 goto done;
841 error = got_object_blob_is_binary(binary, *blob);
842 if (error)
843 goto done;
845 done:
846 if (commit)
847 got_object_commit_close(commit);
849 if (error) {
850 if (*fd != -1)
851 close(*fd);
852 if (*blob)
853 got_object_blob_close(*blob);
854 *fd = -1;
855 *blob = NULL;
858 got_ref_list_free(&refs);
859 free(in_repo_path);
860 free(commit_id);
861 free(path);
862 return error;
865 int
866 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
867 int (*cb)(struct template *, const char *, size_t))
869 const struct got_error *err;
870 char *line = NULL;
871 size_t linesize = 0;
872 size_t lineno = 0;
873 ssize_t linelen = 0;
875 for (;;) {
876 err = got_object_blob_getline(&line, &linelen, &linesize,
877 blob);
878 if (err || linelen == -1)
879 break;
880 lineno++;
881 if (cb(tp, line, lineno) == -1) {
882 err = got_error(GOT_ERR_CANCELLED);
883 break;
887 free(line);
889 if (err) {
890 if (err->code != GOT_ERR_CANCELLED)
891 log_warnx("%s: got_object_blob_getline failed: %s",
892 __func__, err->msg);
893 return -1;
895 return 0;
898 struct blame_cb_args {
899 struct blame_line *lines;
900 int nlines;
901 int nlines_prec;
902 int lineno_cur;
903 off_t *line_offsets;
904 FILE *f;
905 struct got_repository *repo;
906 struct request *c;
907 got_render_blame_line_cb cb;
908 };
910 static const struct got_error *
911 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
912 struct got_commit_object *commit, struct got_object_id *id)
914 const struct got_error *err = NULL;
915 struct blame_cb_args *a = arg;
916 struct blame_line *bline;
917 struct request *c = a->c;
918 char *line = NULL;
919 size_t linesize = 0;
920 off_t offset;
921 struct tm tm;
922 time_t committer_time;
924 if (nlines != a->nlines ||
925 (lineno != -1 && lineno < 1) || lineno > a->nlines)
926 return got_error(GOT_ERR_RANGE);
928 if (lineno == -1)
929 return NULL; /* no change in this commit */
931 /* Annotate this line. */
932 bline = &a->lines[lineno - 1];
933 if (bline->annotated)
934 return NULL;
935 err = got_object_id_str(&bline->id_str, id);
936 if (err)
937 return err;
939 bline->committer = strdup(got_object_commit_get_committer(commit));
940 if (bline->committer == NULL) {
941 err = got_error_from_errno("strdup");
942 goto done;
945 committer_time = got_object_commit_get_committer_time(commit);
946 if (gmtime_r(&committer_time, &tm) == NULL)
947 return got_error_from_errno("gmtime_r");
948 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%F", &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;