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 <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_reference.h"
33 #include "got_repository.h"
34 #include "got_path.h"
35 #include "got_cancel.h"
36 #include "got_diff.h"
37 #include "got_commit_graph.h"
38 #include "got_blame.h"
39 #include "got_privsep.h"
41 #include "proc.h"
42 #include "gotwebd.h"
44 static const struct got_error *got_init_repo_commit(struct repo_commit **);
45 static const struct got_error *got_init_repo_tag(struct repo_tag **);
46 static const struct got_error *got_get_repo_commit(struct request *,
47 struct repo_commit *, struct got_commit_object *, struct got_reflist_head *,
48 struct got_object_id *);
49 static const struct got_error *got_gotweb_dupfd(int *, int *);
50 static const struct got_error *got_gotweb_openfile(FILE **, int *, int *);
51 static const struct got_error *got_gotweb_blame_cb(void *, int, int,
52 struct got_commit_object *,struct got_object_id *);
54 const struct got_error *
55 got_gotweb_flushfile(FILE *f, int fd)
56 {
57 if (fseek(f, 0, SEEK_SET) == -1)
58 return got_error_from_errno("fseek");
60 if (ftruncate(fd, 0) == -1)
61 return got_error_from_errno("ftruncate");
63 if (fsync(fd) == -1)
64 return got_error_from_errno("fsync");
66 if (f && fclose(f) == EOF)
67 return got_error_from_errno("fclose");
69 if (fd != -1 && close(fd) != -1)
70 return got_error_from_errno("close");
72 return NULL;
73 }
75 static const struct got_error *
76 got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
77 {
78 *fd = dup(*priv_fd);
79 if (*fd == -1)
80 return got_error_from_errno("dup");
82 *f = fdopen(*fd, "w+");
83 if (*f == NULL) {
84 close(*fd);
85 return got_error(GOT_ERR_PRIVSEP_NO_FD);
86 }
88 return NULL;
89 }
91 static const struct got_error *
92 got_gotweb_dupfd(int *priv_fd, int *fd)
93 {
94 *fd = dup(*priv_fd);
96 if (*fd < 0)
97 return NULL;
99 return NULL;
102 const struct got_error *
103 got_get_repo_owner(char **owner, struct request *c)
105 struct server *srv = c->srv;
106 struct transport *t = c->t;
107 struct got_repository *repo = t->repo;
108 const char *gitconfig_owner;
110 *owner = NULL;
112 if (srv->show_repo_owner == 0)
113 return NULL;
115 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
116 if (gitconfig_owner) {
117 *owner = strdup(gitconfig_owner);
118 if (*owner == NULL)
119 return got_error_from_errno("strdup");
120 } else {
121 *owner = strdup("");
122 if (*owner == NULL)
123 return got_error_from_errno("strdup");
125 return NULL;
128 const struct got_error *
129 got_get_repo_age(time_t *repo_age, struct request *c, const char *refname)
131 const struct got_error *error = NULL;
132 struct server *srv = c->srv;
133 struct transport *t = c->t;
134 struct got_repository *repo = t->repo;
135 struct got_commit_object *commit = NULL;
136 struct got_reflist_head refs;
137 struct got_reflist_entry *re;
138 time_t committer_time = 0, cmp_time = 0;
140 TAILQ_INIT(&refs);
142 if (srv->show_repo_age == 0)
143 return NULL;
145 error = got_ref_list(&refs, repo, "refs/heads",
146 got_ref_cmp_by_name, NULL);
147 if (error)
148 goto done;
150 /*
151 * Find the youngest branch tip in the repository, or the age of
152 * the a specific branch tip if a name was provided by the caller.
153 */
154 TAILQ_FOREACH(re, &refs, entry) {
155 struct got_object_id *id = NULL;
157 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
158 continue;
160 error = got_ref_resolve(&id, repo, re->ref);
161 if (error)
162 goto done;
164 error = got_object_open_as_commit(&commit, repo, id);
165 free(id);
166 if (error)
167 goto done;
169 committer_time =
170 got_object_commit_get_committer_time(commit);
171 got_object_commit_close(commit);
172 if (cmp_time < committer_time)
173 cmp_time = committer_time;
175 if (refname)
176 break;
179 if (cmp_time != 0)
180 *repo_age = cmp_time;
181 done:
182 got_ref_list_free(&refs);
183 return error;
186 static const struct got_error *
187 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
188 struct got_commit_object *commit, struct got_reflist_head *refs,
189 struct got_object_id *id)
191 const struct got_error *error = NULL;
192 struct got_reflist_entry *re;
193 struct got_object_id *id2 = NULL;
194 struct got_object_qid *parent_id;
195 struct transport *t = c->t;
196 struct querystring *qs = c->t->qs;
197 char *commit_msg = NULL, *commit_msg0;
199 TAILQ_FOREACH(re, refs, entry) {
200 char *s;
201 const char *name;
202 struct got_tag_object *tag = NULL;
203 struct got_object_id *ref_id;
204 int cmp;
206 if (got_ref_is_symbolic(re->ref))
207 continue;
209 name = got_ref_get_name(re->ref);
210 if (strncmp(name, "refs/", 5) == 0)
211 name += 5;
212 if (strncmp(name, "got/", 4) == 0)
213 continue;
214 if (strncmp(name, "heads/", 6) == 0)
215 name += 6;
216 if (strncmp(name, "remotes/", 8) == 0) {
217 name += 8;
218 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
219 continue;
221 error = got_ref_resolve(&ref_id, t->repo, re->ref);
222 if (error)
223 return error;
224 if (strncmp(name, "tags/", 5) == 0) {
225 error = got_object_open_as_tag(&tag, t->repo, ref_id);
226 if (error) {
227 if (error->code != GOT_ERR_OBJ_TYPE) {
228 free(ref_id);
229 continue;
231 /*
232 * Ref points at something other
233 * than a tag.
234 */
235 error = NULL;
236 tag = NULL;
239 cmp = got_object_id_cmp(tag ?
240 got_object_tag_get_object_id(tag) : ref_id, id);
241 free(ref_id);
242 if (tag)
243 got_object_tag_close(tag);
244 if (cmp != 0)
245 continue;
246 s = repo_commit->refs_str;
247 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
248 s ? ", " : "", name) == -1) {
249 error = got_error_from_errno("asprintf");
250 free(s);
251 repo_commit->refs_str = NULL;
252 return error;
254 free(s);
257 error = got_object_id_str(&repo_commit->commit_id, id);
258 if (error)
259 return error;
261 error = got_object_id_str(&repo_commit->tree_id,
262 got_object_commit_get_tree_id(commit));
263 if (error)
264 return error;
266 if (qs->action == DIFF) {
267 parent_id = STAILQ_FIRST(
268 got_object_commit_get_parent_ids(commit));
269 if (parent_id != NULL) {
270 id2 = got_object_id_dup(&parent_id->id);
271 error = got_object_id_str(&repo_commit->parent_id, id2);
272 if (error)
273 return error;
274 free(id2);
275 } else {
276 repo_commit->parent_id = strdup("/dev/null");
277 if (repo_commit->parent_id == NULL) {
278 error = got_error_from_errno("strdup");
279 return error;
284 repo_commit->committer_time =
285 got_object_commit_get_committer_time(commit);
287 repo_commit->author =
288 strdup(got_object_commit_get_author(commit));
289 if (repo_commit->author == NULL) {
290 error = got_error_from_errno("strdup");
291 return error;
293 repo_commit->committer =
294 strdup(got_object_commit_get_committer(commit));
295 if (repo_commit->committer == NULL) {
296 error = got_error_from_errno("strdup");
297 return error;
299 error = got_object_commit_get_logmsg(&commit_msg0, commit);
300 if (error)
301 return error;
303 commit_msg = commit_msg0;
304 while (*commit_msg == '\n')
305 commit_msg++;
307 repo_commit->commit_msg = strdup(commit_msg);
308 if (repo_commit->commit_msg == NULL)
309 error = got_error_from_errno("strdup");
310 free(commit_msg0);
311 return error;
314 const struct got_error *
315 got_get_repo_commits(struct request *c, int limit)
317 const struct got_error *error = NULL;
318 struct got_object_id *id = NULL;
319 struct got_commit_graph *graph = NULL;
320 struct got_commit_object *commit = NULL;
321 struct got_reflist_head refs;
322 struct got_reference *ref = NULL;
323 struct repo_commit *repo_commit = NULL;
324 struct server *srv = c->srv;
325 struct transport *t = c->t;
326 struct got_repository *repo = t->repo;
327 struct querystring *qs = t->qs;
328 struct repo_dir *repo_dir = t->repo_dir;
329 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
330 int chk_next = 0, chk_multi = 0;
332 TAILQ_INIT(&refs);
334 if (qs->file != NULL && strlen(qs->file) > 0)
335 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
336 qs->file) == -1)
337 return got_error_from_errno("asprintf");
339 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
340 repo_dir->name) == -1) {
341 error = got_error_from_errno("asprintf");
342 goto done;
345 if (qs->commit) {
346 error = got_repo_match_object_id_prefix(&id, qs->commit,
347 GOT_OBJ_TYPE_COMMIT, repo);
348 if (error)
349 goto done;
350 } else {
351 error = got_ref_open(&ref, repo, qs->headref, 0);
352 if (error)
353 goto done;
355 error = got_ref_resolve(&id, repo, ref);
356 if (error)
357 goto done;
360 error = got_repo_map_path(&in_repo_path, repo, repo_path);
361 if (error)
362 goto done;
364 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
365 if (error)
366 goto done;
368 if (qs->file != NULL && strlen(qs->file) > 0) {
369 error = got_commit_graph_open(&graph, file_path, 0);
370 if (error)
371 goto done;
372 } else {
373 error = got_commit_graph_open(&graph, in_repo_path, 0);
374 if (error)
375 goto done;
378 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
379 if (error)
380 goto done;
382 for (;;) {
383 struct got_object_id next_id;
385 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
386 NULL);
387 if (error) {
388 if (error->code == GOT_ERR_ITER_COMPLETED)
389 error = NULL;
390 goto done;
393 error = got_object_open_as_commit(&commit, repo, &next_id);
394 if (error)
395 goto done;
397 error = got_init_repo_commit(&repo_commit);
398 if (error)
399 goto done;
401 error = got_get_repo_commit(c, repo_commit, commit,
402 &refs, &next_id);
403 if (error) {
404 gotweb_free_repo_commit(repo_commit);
405 goto done;
408 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
410 if (!chk_multi || limit != 1 ||
411 srv->max_commits_display == 1) {
412 chk_multi = 1;
414 /*
415 * check for one more commit before breaking,
416 * so we know whether to navigate through briefs
417 * commits and summary
418 */
419 if (chk_next && (qs->action == BRIEFS ||
420 qs->action == COMMITS || qs->action == SUMMARY)) {
421 t->next_id = strdup(repo_commit->commit_id);
422 if (t->next_id == NULL) {
423 error = got_error_from_errno("strdup");
424 goto done;
426 if (commit) {
427 got_object_commit_close(commit);
428 commit = NULL;
430 TAILQ_REMOVE(&t->repo_commits, repo_commit,
431 entry);
432 gotweb_free_repo_commit(repo_commit);
433 goto done;
436 if (error || (limit && --limit == 0)) {
437 if (qs->file != NULL && *qs->file != '\0')
438 if (chk_multi == 0)
439 break;
440 chk_next = 1;
442 if (commit) {
443 got_object_commit_close(commit);
444 commit = NULL;
447 done:
448 if (ref)
449 got_ref_close(ref);
450 if (commit)
451 got_object_commit_close(commit);
452 if (graph)
453 got_commit_graph_close(graph);
454 got_ref_list_free(&refs);
455 free(in_repo_path);
456 free(file_path);
457 free(repo_path);
458 free(id);
459 return error;
462 const struct got_error *
463 got_get_repo_tags(struct request *c, int limit)
465 const struct got_error *error = NULL;
466 struct got_object_id *id = NULL;
467 struct got_commit_object *commit = NULL;
468 struct got_reflist_head refs;
469 struct got_reference *ref;
470 struct got_reflist_entry *re;
471 struct server *srv = c->srv;
472 struct transport *t = c->t;
473 struct got_repository *repo = t->repo;
474 struct querystring *qs = t->qs;
475 struct repo_dir *repo_dir = t->repo_dir;
476 struct got_tag_object *tag = NULL;
477 struct repo_tag *rt = NULL, *trt = NULL;
478 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
479 char *tag_commit = NULL, *tag_commit0 = NULL;
480 char *commit_msg = NULL, *commit_msg0 = NULL;
481 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
483 TAILQ_INIT(&refs);
485 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
486 repo_dir->name) == -1)
487 return got_error_from_errno("asprintf");
489 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
490 error = got_ref_open(&ref, repo, qs->headref, 0);
491 if (error)
492 goto err;
493 error = got_ref_resolve(&id, repo, ref);
494 got_ref_close(ref);
495 if (error)
496 goto err;
497 } else if (qs->commit == NULL && qs->action == TAG) {
498 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
499 goto err;
500 } else {
501 error = got_repo_match_object_id_prefix(&id, qs->commit,
502 GOT_OBJ_TYPE_COMMIT, repo);
503 if (error)
504 goto err;
507 if (qs->action != SUMMARY && qs->action != TAGS) {
508 error = got_object_open_as_commit(&commit, repo, id);
509 if (error)
510 goto err;
511 error = got_object_commit_get_logmsg(&commit_msg0, commit);
512 if (error)
513 goto err;
514 if (commit) {
515 got_object_commit_close(commit);
516 commit = NULL;
520 error = got_repo_map_path(&in_repo_path, repo, repo_path);
521 if (error)
522 goto err;
524 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
525 repo);
526 if (error)
527 goto err;
529 if (limit == 1)
530 chk_multi = 0;
532 /*
533 * XXX: again, see previous message about caching
534 */
536 TAILQ_FOREACH(re, &refs, entry) {
537 struct repo_tag *new_repo_tag = NULL;
538 error = got_init_repo_tag(&new_repo_tag);
539 if (error)
540 goto err;
542 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
544 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
545 if (new_repo_tag->tag_name == NULL) {
546 error = got_error_from_errno("strdup");
547 goto err;
550 free(id);
551 id = NULL;
553 free(id_str);
554 id_str = NULL;
556 error = got_ref_resolve(&id, repo, re->ref);
557 if (error)
558 goto done;
560 if (tag)
561 got_object_tag_close(tag);
562 error = got_object_open_as_tag(&tag, repo, id);
563 if (error) {
564 if (error->code != GOT_ERR_OBJ_TYPE)
565 goto done;
566 /* "lightweight" tag */
567 error = got_object_open_as_commit(&commit, repo, id);
568 if (error)
569 goto done;
570 new_repo_tag->tagger =
571 strdup(got_object_commit_get_committer(commit));
572 if (new_repo_tag->tagger == NULL) {
573 error = got_error_from_errno("strdup");
574 goto err;
576 new_repo_tag->tagger_time =
577 got_object_commit_get_committer_time(commit);
578 error = got_object_id_str(&id_str, id);
579 if (error)
580 goto err;
581 } else {
582 new_repo_tag->tagger =
583 strdup(got_object_tag_get_tagger(tag));
584 if (new_repo_tag->tagger == NULL) {
585 error = got_error_from_errno("strdup");
586 goto err;
588 new_repo_tag->tagger_time =
589 got_object_tag_get_tagger_time(tag);
590 error = got_object_id_str(&id_str,
591 got_object_tag_get_object_id(tag));
592 if (error)
593 goto err;
596 new_repo_tag->commit_id = strdup(id_str);
597 if (new_repo_tag->commit_id == NULL)
598 goto err;
600 if (commit_found == 0 && qs->commit != NULL &&
601 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
602 continue;
603 else
604 commit_found = 1;
606 t->tag_count++;
608 /*
609 * check for one more commit before breaking,
610 * so we know whether to navigate through briefs
611 * commits and summary
612 */
613 if (chk_next) {
614 t->next_id = strdup(new_repo_tag->commit_id);
615 if (t->next_id == NULL) {
616 error = got_error_from_errno("strdup");
617 goto err;
619 if (commit) {
620 got_object_commit_close(commit);
621 commit = NULL;
623 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
624 gotweb_free_repo_tag(new_repo_tag);
625 goto done;
628 if (commit) {
629 error = got_object_commit_get_logmsg(&tag_commit0,
630 commit);
631 if (error)
632 goto err;
633 got_object_commit_close(commit);
634 commit = NULL;
635 } else {
636 tag_commit0 = strdup(got_object_tag_get_message(tag));
637 if (tag_commit0 == NULL) {
638 error = got_error_from_errno("strdup");
639 goto err;
643 tag_commit = tag_commit0;
644 while (*tag_commit == '\n')
645 tag_commit++;
646 new_repo_tag->tag_commit = strdup(tag_commit);
647 if (new_repo_tag->tag_commit == NULL) {
648 error = got_error_from_errno("strdup");
649 free(tag_commit0);
650 goto err;
652 free(tag_commit0);
654 if (qs->action != SUMMARY && qs->action != TAGS) {
655 commit_msg = commit_msg0;
656 while (*commit_msg == '\n')
657 commit_msg++;
659 new_repo_tag->commit_msg = strdup(commit_msg);
660 if (new_repo_tag->commit_msg == NULL) {
661 error = got_error_from_errno("strdup");
662 goto err;
666 if (limit && --limit == 0) {
667 if (chk_multi == 0)
668 break;
669 chk_next = 1;
673 done:
674 /*
675 * we have tailq populated, so find previous commit id
676 * for navigation through briefs and commits
677 */
678 if (t->tag_count == 0) {
679 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
680 TAILQ_REMOVE(&t->repo_tags, rt, entry);
681 gotweb_free_repo_tag(rt);
684 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
685 commit_found = 0;
686 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
687 entry) {
688 if (commit_found == 0 && rt->commit_id != NULL &&
689 strcmp(qs->commit, rt->commit_id) != 0) {
690 continue;
691 } else
692 commit_found = 1;
693 if (c_cnt == srv->max_commits_display ||
694 rt == TAILQ_FIRST(&t->repo_tags)) {
695 t->prev_id = strdup(rt->commit_id);
696 if (t->prev_id == NULL)
697 error = got_error_from_errno("strdup");
698 break;
700 c_cnt++;
703 err:
704 if (commit)
705 got_object_commit_close(commit);
706 if (tag)
707 got_object_tag_close(tag);
708 got_ref_list_free(&refs);
709 free(commit_msg0);
710 free(in_repo_path);
711 free(repo_path);
712 free(id);
713 free(id_str);
714 return error;
717 int
718 got_output_repo_tree(struct request *c,
719 int (*cb)(struct template *, struct got_tree_entry *))
721 const struct got_error *error = NULL;
722 struct transport *t = c->t;
723 struct got_commit_object *commit = NULL;
724 struct got_repository *repo = t->repo;
725 struct querystring *qs = t->qs;
726 struct repo_commit *rc = NULL;
727 struct got_object_id *tree_id = NULL, *commit_id = NULL;
728 struct got_reflist_head refs;
729 struct got_tree_object *tree = NULL;
730 struct got_tree_entry *te;
731 struct repo_dir *repo_dir = t->repo_dir;
732 char *escaped_name = NULL, *path = NULL;
733 int nentries, i;
735 TAILQ_INIT(&refs);
737 rc = TAILQ_FIRST(&t->repo_commits);
739 if (qs->folder != NULL) {
740 path = strdup(qs->folder);
741 if (path == NULL) {
742 error = got_error_from_errno("strdup");
743 goto done;
745 } else {
746 error = got_repo_map_path(&path, repo, repo_dir->path);
747 if (error)
748 goto done;
751 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
752 GOT_OBJ_TYPE_COMMIT, &refs, repo);
753 if (error)
754 goto done;
756 error = got_object_open_as_commit(&commit, repo, commit_id);
757 if (error)
758 goto done;
760 error = got_object_id_by_path(&tree_id, repo, commit, path);
761 if (error)
762 goto done;
764 error = got_object_open_as_tree(&tree, repo, tree_id);
765 if (error)
766 goto done;
768 nentries = got_object_tree_get_nentries(tree);
770 for (i = 0; i < nentries; i++) {
771 te = got_object_tree_get_entry(tree, i);
772 if (cb(c->tp, te) == -1)
773 break;
775 done:
776 free(escaped_name);
777 free(path);
778 got_ref_list_free(&refs);
779 if (commit)
780 got_object_commit_close(commit);
781 if (tree)
782 got_object_tree_close(tree);
783 free(commit_id);
784 free(tree_id);
785 if (error) {
786 log_warnx("%s: %s", __func__, error->msg);
787 return -1;
789 return 0;
792 const struct got_error *
793 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
794 int *binary, struct request *c)
796 const struct got_error *error = NULL;
797 struct transport *t = c->t;
798 struct got_repository *repo = t->repo;
799 struct querystring *qs = c->t->qs;
800 struct got_commit_object *commit = NULL;
801 struct got_object_id *commit_id = NULL;
802 struct got_reflist_head refs;
803 char *path = NULL, *in_repo_path = NULL;
804 int obj_type;
806 TAILQ_INIT(&refs);
808 *blob = NULL;
809 *fd = -1;
810 *binary = 0;
812 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
813 qs->folder ? "/" : "", qs->file) == -1) {
814 error = got_error_from_errno("asprintf");
815 goto done;
818 error = got_repo_map_path(&in_repo_path, repo, path);
819 if (error)
820 goto done;
822 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
823 GOT_OBJ_TYPE_COMMIT, &refs, repo);
824 if (error)
825 goto done;
827 error = got_object_open_as_commit(&commit, repo, commit_id);
828 if (error)
829 goto done;
831 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
832 if (error)
833 goto done;
835 if (commit_id == NULL) {
836 error = got_error(GOT_ERR_NO_OBJ);
837 goto done;
840 error = got_object_get_type(&obj_type, repo, commit_id);
841 if (error)
842 goto done;
844 if (obj_type != GOT_OBJ_TYPE_BLOB) {
845 error = got_error(GOT_ERR_OBJ_TYPE);
846 goto done;
849 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
850 if (error)
851 goto done;
853 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
854 if (error)
855 goto done;
857 error = got_object_blob_is_binary(binary, *blob);
858 if (error)
859 goto done;
861 done:
862 if (commit)
863 got_object_commit_close(commit);
865 if (error) {
866 if (*fd != -1)
867 close(*fd);
868 if (*blob)
869 got_object_blob_close(*blob);
870 *fd = -1;
871 *blob = NULL;
874 free(in_repo_path);
875 free(commit_id);
876 free(path);
877 return error;
880 int
881 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
882 int (*cb)(struct template *, const char *, size_t))
884 const struct got_error *err;
885 char *line = NULL;
886 size_t linesize = 0;
887 size_t lineno = 0;
888 ssize_t linelen = 0;
890 for (;;) {
891 err = got_object_blob_getline(&line, &linelen, &linesize,
892 blob);
893 if (err || linelen == -1)
894 break;
895 lineno++;
896 if (cb(tp, line, lineno) == -1)
897 break;
900 free(line);
902 if (err) {
903 log_warnx("%s: got_object_blob_getline failed: %s",
904 __func__, err->msg);
905 return -1;
907 return 0;
910 struct blame_cb_args {
911 struct blame_line *lines;
912 int nlines;
913 int nlines_prec;
914 int lineno_cur;
915 off_t *line_offsets;
916 FILE *f;
917 struct got_repository *repo;
918 struct request *c;
919 got_render_blame_line_cb cb;
920 };
922 static const struct got_error *
923 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
924 struct got_commit_object *commit, struct got_object_id *id)
926 const struct got_error *err = NULL;
927 struct blame_cb_args *a = arg;
928 struct blame_line *bline;
929 struct request *c = a->c;
930 char *line = NULL;
931 size_t linesize = 0;
932 off_t offset;
933 struct tm tm;
934 time_t committer_time;
936 if (nlines != a->nlines ||
937 (lineno != -1 && lineno < 1) || lineno > a->nlines)
938 return got_error(GOT_ERR_RANGE);
940 if (lineno == -1)
941 return NULL; /* no change in this commit */
943 /* Annotate this line. */
944 bline = &a->lines[lineno - 1];
945 if (bline->annotated)
946 return NULL;
947 err = got_object_id_str(&bline->id_str, id);
948 if (err)
949 return err;
951 bline->committer = strdup(got_object_commit_get_committer(commit));
952 if (bline->committer == NULL) {
953 err = got_error_from_errno("strdup");
954 goto done;
957 committer_time = got_object_commit_get_committer_time(commit);
958 if (gmtime_r(&committer_time, &tm) == NULL)
959 return got_error_from_errno("gmtime_r");
960 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
961 &tm) == 0) {
962 err = got_error(GOT_ERR_NO_SPACE);
963 goto done;
965 bline->annotated = 1;
967 /* Print lines annotated so far. */
968 bline = &a->lines[a->lineno_cur - 1];
969 if (!bline->annotated)
970 goto done;
972 offset = a->line_offsets[a->lineno_cur - 1];
973 if (fseeko(a->f, offset, SEEK_SET) == -1) {
974 err = got_error_from_errno("fseeko");
975 goto done;
978 while (a->lineno_cur <= a->nlines && bline->annotated) {
979 if (getline(&line, &linesize, a->f) == -1) {
980 if (ferror(a->f))
981 err = got_error_from_errno("getline");
982 break;
985 if (a->cb(c->tp, line, bline, a->nlines_prec,
986 a->lineno_cur) == -1)
987 break;
989 a->lineno_cur++;
990 bline = &a->lines[a->lineno_cur - 1];
992 done:
993 free(line);
994 return err;
997 const struct got_error *
998 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1000 const struct got_error *error = NULL;
1001 struct transport *t = c->t;
1002 struct got_repository *repo = t->repo;
1003 struct querystring *qs = c->t->qs;
1004 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1005 struct got_commit_object *commit = NULL;
1006 struct got_reflist_head refs;
1007 struct got_blob_object *blob = NULL;
1008 char *path = NULL, *in_repo_path = NULL;
1009 struct blame_cb_args bca;
1010 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1011 int fd6 = -1;
1012 off_t filesize;
1013 FILE *f1 = NULL, *f2 = NULL;
1015 TAILQ_INIT(&refs);
1016 bca.f = NULL;
1017 bca.lines = NULL;
1018 bca.cb = cb;
1020 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1021 qs->folder ? "/" : "", qs->file) == -1) {
1022 error = got_error_from_errno("asprintf");
1023 goto done;
1026 error = got_repo_map_path(&in_repo_path, repo, path);
1027 if (error)
1028 goto done;
1030 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1031 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1032 if (error)
1033 goto done;
1035 error = got_object_open_as_commit(&commit, repo, commit_id);
1036 if (error)
1037 goto done;
1039 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1040 if (error)
1041 goto done;
1043 error = got_object_get_type(&obj_type, repo, obj_id);
1044 if (error)
1045 goto done;
1047 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1048 error = got_error(GOT_ERR_OBJ_TYPE);
1049 goto done;
1052 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1053 if (error)
1054 goto done;
1056 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1057 if (error)
1058 goto done;
1060 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1061 if (error)
1062 goto done;
1064 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1065 &bca.line_offsets, bca.f, blob);
1066 if (error || bca.nlines == 0)
1067 goto done;
1069 /* Don't include \n at EOF in the blame line count. */
1070 if (bca.line_offsets[bca.nlines - 1] == filesize)
1071 bca.nlines--;
1073 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1074 if (bca.lines == NULL) {
1075 error = got_error_from_errno("calloc");
1076 goto done;
1078 bca.lineno_cur = 1;
1079 bca.nlines_prec = 0;
1080 i = bca.nlines;
1081 while (i > 0) {
1082 i /= 10;
1083 bca.nlines_prec++;
1085 bca.repo = repo;
1086 bca.c = c;
1088 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1089 if (error)
1090 goto done;
1092 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1093 if (error)
1094 goto done;
1096 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1097 if (error)
1098 goto done;
1100 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1101 if (error)
1102 goto done;
1104 error = got_blame(in_repo_path, commit_id, repo,
1105 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1106 fd3, fd4, f1, f2);
1108 done:
1109 if (bca.lines) {
1110 free(bca.line_offsets);
1111 for (i = 0; i < bca.nlines; i++) {
1112 struct blame_line *bline = &bca.lines[i];
1113 free(bline->id_str);
1114 free(bline->committer);
1117 free(bca.lines);
1118 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1119 error = got_error_from_errno("close");
1120 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1121 error = got_error_from_errno("close");
1122 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1123 error = got_error_from_errno("close");
1124 if (bca.f) {
1125 const struct got_error *bca_err =
1126 got_gotweb_flushfile(bca.f, fd1);
1127 if (error == NULL)
1128 error = bca_err;
1130 if (f1) {
1131 const struct got_error *f1_err =
1132 got_gotweb_flushfile(f1, fd5);
1133 if (error == NULL)
1134 error = f1_err;
1136 if (f2) {
1137 const struct got_error *f2_err =
1138 got_gotweb_flushfile(f2, fd6);
1139 if (error == NULL)
1140 error = f2_err;
1142 if (commit)
1143 got_object_commit_close(commit);
1144 if (blob)
1145 got_object_blob_close(blob);
1146 free(in_repo_path);
1147 free(commit_id);
1148 free(obj_id);
1149 free(path);
1150 got_ref_list_free(&refs);
1151 return error;
1154 const struct got_error *
1155 got_open_diff_for_output(FILE **fp, int *fd, struct request *c)
1157 const struct got_error *error = NULL;
1158 struct transport *t = c->t;
1159 struct got_repository *repo = t->repo;
1160 struct repo_commit *rc = NULL;
1161 struct got_object_id *id1 = NULL, *id2 = NULL;
1162 struct got_reflist_head refs;
1163 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1164 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1166 *fp = NULL;
1167 *fd = -1;
1169 TAILQ_INIT(&refs);
1171 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1172 if (error)
1173 return error;
1175 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1176 if (error)
1177 return error;
1179 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1180 if (error)
1181 return error;
1183 rc = TAILQ_FIRST(&t->repo_commits);
1185 if (rc->parent_id != NULL &&
1186 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1187 error = got_repo_match_object_id(&id1, NULL,
1188 rc->parent_id, GOT_OBJ_TYPE_ANY,
1189 &refs, repo);
1190 if (error)
1191 goto done;
1194 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1195 GOT_OBJ_TYPE_ANY, &refs, repo);
1196 if (error)
1197 goto done;
1199 error = got_object_get_type(&obj_type, repo, id2);
1200 if (error)
1201 goto done;
1203 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1204 if (error)
1205 goto done;
1207 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1208 if (error)
1209 goto done;
1211 switch (obj_type) {
1212 case GOT_OBJ_TYPE_BLOB:
1213 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1214 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1215 NULL, repo, f3);
1216 break;
1217 case GOT_OBJ_TYPE_TREE:
1218 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1219 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1220 NULL, repo, f3);
1221 break;
1222 case GOT_OBJ_TYPE_COMMIT:
1223 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1224 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1225 NULL, repo, f3);
1226 break;
1227 default:
1228 error = got_error(GOT_ERR_OBJ_TYPE);
1230 if (error)
1231 goto done;
1233 if (fseek(f1, 0, SEEK_SET) == -1) {
1234 error = got_ferror(f1, GOT_ERR_IO);
1235 goto done;
1238 if (fseek(f2, 0, SEEK_SET) == -1) {
1239 error = got_ferror(f2, GOT_ERR_IO);
1240 goto done;
1243 if (fseek(f3, 0, SEEK_SET) == -1) {
1244 error = got_ferror(f3, GOT_ERR_IO);
1245 goto done;
1248 *fp = f3;
1249 *fd = fd3;
1251 done:
1252 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1253 error = got_error_from_errno("close");
1254 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1255 error = got_error_from_errno("close");
1256 if (f1) {
1257 const struct got_error *f1_err =
1258 got_gotweb_flushfile(f1, fd1);
1259 if (error == NULL)
1260 error = f1_err;
1262 if (f2) {
1263 const struct got_error *f2_err =
1264 got_gotweb_flushfile(f2, fd2);
1265 if (error == NULL)
1266 error = f2_err;
1268 if (error && f3) {
1269 got_gotweb_flushfile(f3, fd3);
1270 *fp = NULL;
1271 *fd = -1;
1273 got_ref_list_free(&refs);
1274 free(id1);
1275 free(id2);
1276 return error;
1279 static const struct got_error *
1280 got_init_repo_commit(struct repo_commit **rc)
1282 *rc = calloc(1, sizeof(**rc));
1283 if (*rc == NULL)
1284 return got_error_from_errno2("%s: calloc", __func__);
1286 (*rc)->path = NULL;
1287 (*rc)->refs_str = NULL;
1288 (*rc)->commit_id = NULL;
1289 (*rc)->committer = NULL;
1290 (*rc)->author = NULL;
1291 (*rc)->parent_id = NULL;
1292 (*rc)->tree_id = NULL;
1293 (*rc)->commit_msg = NULL;
1295 return NULL;
1298 static const struct got_error *
1299 got_init_repo_tag(struct repo_tag **rt)
1301 *rt = calloc(1, sizeof(**rt));
1302 if (*rt == NULL)
1303 return got_error_from_errno2("%s: calloc", __func__);
1305 (*rt)->commit_id = NULL;
1306 (*rt)->tag_name = NULL;
1307 (*rt)->tag_commit = NULL;
1308 (*rt)->commit_msg = NULL;
1309 (*rt)->tagger = NULL;
1311 return NULL;