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, commit_found = 0;
331 int obj_type, limit_chk = 0;
333 TAILQ_INIT(&refs);
335 if (qs->file != NULL && strlen(qs->file) > 0)
336 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
337 qs->file) == -1)
338 return got_error_from_errno("asprintf");
340 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
341 repo_dir->name) == -1) {
342 error = got_error_from_errno("asprintf");
343 goto done;
346 /*
347 * XXX: jumping directly to a commit id via
348 * got_repo_match_object_id_prefix significantly improves performance,
349 * but does not allow us to create a PREVIOUS button, since commits can
350 * only be itereated forward. So, we have to match as we iterate from
351 * the headref.
352 */
353 if (qs->action == BRIEFS || qs->action == COMMITS ||
354 (qs->action == TREE && qs->commit == NULL)) {
355 error = got_ref_open(&ref, repo, qs->headref, 0);
356 if (error)
357 goto done;
359 error = got_ref_resolve(&id, repo, ref);
360 if (error)
361 goto done;
362 } else if (qs->commit != NULL) {
363 error = got_ref_open(&ref, repo, qs->commit, 0);
364 if (error == NULL) {
365 error = got_ref_resolve(&id, repo, ref);
366 if (error)
367 goto done;
368 error = got_object_get_type(&obj_type, repo, id);
369 if (error)
370 goto done;
371 if (obj_type == GOT_OBJ_TYPE_TAG) {
372 struct got_tag_object *tag;
373 error = got_object_open_as_tag(&tag, repo, id);
374 if (error)
375 goto done;
376 if (got_object_tag_get_object_type(tag) !=
377 GOT_OBJ_TYPE_COMMIT) {
378 got_object_tag_close(tag);
379 error = got_error(GOT_ERR_OBJ_TYPE);
380 goto done;
382 free(id);
383 id = got_object_id_dup(
384 got_object_tag_get_object_id(tag));
385 if (id == NULL)
386 error = got_error_from_errno(
387 "got_object_id_dup");
388 got_object_tag_close(tag);
389 if (error)
390 goto done;
391 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
392 error = got_error(GOT_ERR_OBJ_TYPE);
393 goto done;
396 error = got_repo_match_object_id_prefix(&id, qs->commit,
397 GOT_OBJ_TYPE_COMMIT, repo);
398 if (error)
399 goto done;
402 error = got_repo_map_path(&in_repo_path, repo, repo_path);
403 if (error)
404 goto done;
406 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
407 if (error)
408 goto done;
410 if (qs->file != NULL && strlen(qs->file) > 0) {
411 error = got_commit_graph_open(&graph, file_path, 0);
412 if (error)
413 goto done;
414 } else {
415 error = got_commit_graph_open(&graph, in_repo_path, 0);
416 if (error)
417 goto done;
420 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
421 if (error)
422 goto done;
424 for (;;) {
425 struct got_object_id next_id;
427 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
428 NULL);
429 if (error) {
430 if (error->code == GOT_ERR_ITER_COMPLETED)
431 error = NULL;
432 goto done;
435 error = got_object_open_as_commit(&commit, repo, &next_id);
436 if (error)
437 goto done;
439 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
440 NULL);
441 if (error)
442 goto done;
444 error = got_init_repo_commit(&repo_commit);
445 if (error)
446 goto done;
448 error = got_get_repo_commit(c, repo_commit, commit,
449 &refs, &next_id);
450 if (error) {
451 gotweb_free_repo_commit(repo_commit);
452 goto done;
455 if (limit_chk == ((limit * qs->page) - limit) &&
456 commit_found == 0 && repo_commit->commit_id != NULL) {
457 t->prev_id = strdup(repo_commit->commit_id);
458 if (t->prev_id == NULL) {
459 error = got_error_from_errno("strdup");
460 gotweb_free_repo_commit(repo_commit);
461 goto done;
465 if (qs->commit != NULL && commit_found == 0 && limit != 1) {
466 if (strcmp(qs->commit, repo_commit->commit_id) == 0)
467 commit_found = 1;
468 else if (qs->file != NULL && strlen(qs->file) > 0 &&
469 qs->page == 0)
470 commit_found = 1;
471 else {
472 gotweb_free_repo_commit(repo_commit);
473 limit_chk++;
474 continue;
478 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
480 if (limit == 1 && chk_multi == 0 &&
481 srv->max_commits_display != 1)
482 commit_found = 1;
483 else {
484 chk_multi = 1;
486 /*
487 * check for one more commit before breaking,
488 * so we know whether to navigate through briefs
489 * commits and summary
490 */
491 if (chk_next && (qs->action == BRIEFS ||
492 qs->action == COMMITS || qs->action == SUMMARY)) {
493 t->next_id = strdup(repo_commit->commit_id);
494 if (t->next_id == NULL) {
495 error = got_error_from_errno("strdup");
496 goto done;
498 if (commit) {
499 got_object_commit_close(commit);
500 commit = NULL;
502 TAILQ_REMOVE(&t->repo_commits, repo_commit,
503 entry);
504 gotweb_free_repo_commit(repo_commit);
505 goto done;
508 got_ref_list_free(&refs);
509 if (error || (limit && --limit == 0)) {
510 if (commit_found || (qs->file != NULL &&
511 strlen(qs->file) > 0))
512 if (chk_multi == 0)
513 break;
514 chk_next = 1;
516 if (commit) {
517 got_object_commit_close(commit);
518 commit = NULL;
521 done:
522 if (ref)
523 got_ref_close(ref);
524 if (commit)
525 got_object_commit_close(commit);
526 if (graph)
527 got_commit_graph_close(graph);
528 got_ref_list_free(&refs);
529 free(in_repo_path);
530 free(file_path);
531 free(repo_path);
532 free(id);
533 return error;
536 const struct got_error *
537 got_get_repo_tags(struct request *c, int limit)
539 const struct got_error *error = NULL;
540 struct got_object_id *id = NULL;
541 struct got_commit_object *commit = NULL;
542 struct got_reflist_head refs;
543 struct got_reference *ref;
544 struct got_reflist_entry *re;
545 struct server *srv = c->srv;
546 struct transport *t = c->t;
547 struct got_repository *repo = t->repo;
548 struct querystring *qs = t->qs;
549 struct repo_dir *repo_dir = t->repo_dir;
550 struct got_tag_object *tag = NULL;
551 struct repo_tag *rt = NULL, *trt = NULL;
552 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
553 char *tag_commit = NULL, *tag_commit0 = NULL;
554 char *commit_msg = NULL, *commit_msg0 = NULL;
555 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
557 TAILQ_INIT(&refs);
559 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
560 repo_dir->name) == -1)
561 return got_error_from_errno("asprintf");
563 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
564 error = got_ref_open(&ref, repo, qs->headref, 0);
565 if (error)
566 goto err;
567 error = got_ref_resolve(&id, repo, ref);
568 got_ref_close(ref);
569 if (error)
570 goto err;
571 } else if (qs->commit == NULL && qs->action == TAG) {
572 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
573 goto err;
574 } else {
575 error = got_repo_match_object_id_prefix(&id, qs->commit,
576 GOT_OBJ_TYPE_COMMIT, repo);
577 if (error)
578 goto err;
581 if (qs->action != SUMMARY && qs->action != TAGS) {
582 error = got_object_open_as_commit(&commit, repo, id);
583 if (error)
584 goto err;
585 error = got_object_commit_get_logmsg(&commit_msg0, commit);
586 if (error)
587 goto err;
588 if (commit) {
589 got_object_commit_close(commit);
590 commit = NULL;
594 error = got_repo_map_path(&in_repo_path, repo, repo_path);
595 if (error)
596 goto err;
598 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
599 repo);
600 if (error)
601 goto err;
603 if (limit == 1)
604 chk_multi = 0;
606 /*
607 * XXX: again, see previous message about caching
608 */
610 TAILQ_FOREACH(re, &refs, entry) {
611 struct repo_tag *new_repo_tag = NULL;
612 error = got_init_repo_tag(&new_repo_tag);
613 if (error)
614 goto err;
616 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
618 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
619 if (new_repo_tag->tag_name == NULL) {
620 error = got_error_from_errno("strdup");
621 goto err;
624 free(id);
625 id = NULL;
627 free(id_str);
628 id_str = NULL;
630 error = got_ref_resolve(&id, repo, re->ref);
631 if (error)
632 goto done;
634 if (tag)
635 got_object_tag_close(tag);
636 error = got_object_open_as_tag(&tag, repo, id);
637 if (error) {
638 if (error->code != GOT_ERR_OBJ_TYPE)
639 goto done;
640 /* "lightweight" tag */
641 error = got_object_open_as_commit(&commit, repo, id);
642 if (error)
643 goto done;
644 new_repo_tag->tagger =
645 strdup(got_object_commit_get_committer(commit));
646 if (new_repo_tag->tagger == NULL) {
647 error = got_error_from_errno("strdup");
648 goto err;
650 new_repo_tag->tagger_time =
651 got_object_commit_get_committer_time(commit);
652 error = got_object_id_str(&id_str, id);
653 if (error)
654 goto err;
655 } else {
656 new_repo_tag->tagger =
657 strdup(got_object_tag_get_tagger(tag));
658 if (new_repo_tag->tagger == NULL) {
659 error = got_error_from_errno("strdup");
660 goto err;
662 new_repo_tag->tagger_time =
663 got_object_tag_get_tagger_time(tag);
664 error = got_object_id_str(&id_str,
665 got_object_tag_get_object_id(tag));
666 if (error)
667 goto err;
670 new_repo_tag->commit_id = strdup(id_str);
671 if (new_repo_tag->commit_id == NULL)
672 goto err;
674 if (commit_found == 0 && qs->commit != NULL &&
675 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
676 continue;
677 else
678 commit_found = 1;
680 t->tag_count++;
682 /*
683 * check for one more commit before breaking,
684 * so we know whether to navigate through briefs
685 * commits and summary
686 */
687 if (chk_next) {
688 t->next_id = strdup(new_repo_tag->commit_id);
689 if (t->next_id == NULL) {
690 error = got_error_from_errno("strdup");
691 goto err;
693 if (commit) {
694 got_object_commit_close(commit);
695 commit = NULL;
697 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
698 gotweb_free_repo_tag(new_repo_tag);
699 goto done;
702 if (commit) {
703 error = got_object_commit_get_logmsg(&tag_commit0,
704 commit);
705 if (error)
706 goto err;
707 got_object_commit_close(commit);
708 commit = NULL;
709 } else {
710 tag_commit0 = strdup(got_object_tag_get_message(tag));
711 if (tag_commit0 == NULL) {
712 error = got_error_from_errno("strdup");
713 goto err;
717 tag_commit = tag_commit0;
718 while (*tag_commit == '\n')
719 tag_commit++;
720 new_repo_tag->tag_commit = strdup(tag_commit);
721 if (new_repo_tag->tag_commit == NULL) {
722 error = got_error_from_errno("strdup");
723 free(tag_commit0);
724 goto err;
726 free(tag_commit0);
728 if (qs->action != SUMMARY && qs->action != TAGS) {
729 commit_msg = commit_msg0;
730 while (*commit_msg == '\n')
731 commit_msg++;
733 new_repo_tag->commit_msg = strdup(commit_msg);
734 if (new_repo_tag->commit_msg == NULL) {
735 error = got_error_from_errno("strdup");
736 goto err;
740 if (limit && --limit == 0) {
741 if (chk_multi == 0)
742 break;
743 chk_next = 1;
747 done:
748 /*
749 * we have tailq populated, so find previous commit id
750 * for navigation through briefs and commits
751 */
752 if (t->tag_count == 0) {
753 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
754 TAILQ_REMOVE(&t->repo_tags, rt, entry);
755 gotweb_free_repo_tag(rt);
758 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
759 commit_found = 0;
760 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
761 entry) {
762 if (commit_found == 0 && rt->commit_id != NULL &&
763 strcmp(qs->commit, rt->commit_id) != 0) {
764 continue;
765 } else
766 commit_found = 1;
767 if (c_cnt == srv->max_commits_display ||
768 rt == TAILQ_FIRST(&t->repo_tags)) {
769 t->prev_id = strdup(rt->commit_id);
770 if (t->prev_id == NULL)
771 error = got_error_from_errno("strdup");
772 break;
774 c_cnt++;
777 err:
778 if (commit)
779 got_object_commit_close(commit);
780 if (tag)
781 got_object_tag_close(tag);
782 got_ref_list_free(&refs);
783 free(commit_msg0);
784 free(in_repo_path);
785 free(repo_path);
786 free(id);
787 free(id_str);
788 return error;
791 int
792 got_output_repo_tree(struct request *c,
793 int (*cb)(struct template *, struct got_tree_entry *))
795 const struct got_error *error = NULL;
796 struct transport *t = c->t;
797 struct got_commit_object *commit = NULL;
798 struct got_repository *repo = t->repo;
799 struct querystring *qs = t->qs;
800 struct repo_commit *rc = NULL;
801 struct got_object_id *tree_id = NULL, *commit_id = NULL;
802 struct got_reflist_head refs;
803 struct got_tree_object *tree = NULL;
804 struct got_tree_entry *te;
805 struct repo_dir *repo_dir = t->repo_dir;
806 char *escaped_name = NULL, *path = NULL;
807 int nentries, i;
809 TAILQ_INIT(&refs);
811 rc = TAILQ_FIRST(&t->repo_commits);
813 if (qs->folder != NULL) {
814 path = strdup(qs->folder);
815 if (path == NULL) {
816 error = got_error_from_errno("strdup");
817 goto done;
819 } else {
820 error = got_repo_map_path(&path, repo, repo_dir->path);
821 if (error)
822 goto done;
825 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
826 GOT_OBJ_TYPE_COMMIT, &refs, repo);
827 if (error)
828 goto done;
830 error = got_object_open_as_commit(&commit, repo, commit_id);
831 if (error)
832 goto done;
834 error = got_object_id_by_path(&tree_id, repo, commit, path);
835 if (error)
836 goto done;
838 error = got_object_open_as_tree(&tree, repo, tree_id);
839 if (error)
840 goto done;
842 nentries = got_object_tree_get_nentries(tree);
844 for (i = 0; i < nentries; i++) {
845 te = got_object_tree_get_entry(tree, i);
846 if (cb(c->tp, te) == -1)
847 break;
849 done:
850 free(escaped_name);
851 free(path);
852 got_ref_list_free(&refs);
853 if (commit)
854 got_object_commit_close(commit);
855 if (tree)
856 got_object_tree_close(tree);
857 free(commit_id);
858 free(tree_id);
859 if (error) {
860 log_warnx("%s: %s", __func__, error->msg);
861 return -1;
863 return 0;
866 const struct got_error *
867 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
868 int *binary, struct request *c)
870 const struct got_error *error = NULL;
871 struct transport *t = c->t;
872 struct got_repository *repo = t->repo;
873 struct querystring *qs = c->t->qs;
874 struct got_commit_object *commit = NULL;
875 struct got_object_id *commit_id = NULL;
876 struct got_reflist_head refs;
877 char *path = NULL, *in_repo_path = NULL;
878 int obj_type;
880 TAILQ_INIT(&refs);
882 *blob = NULL;
883 *fd = -1;
884 *binary = 0;
886 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
887 qs->folder ? "/" : "", qs->file) == -1) {
888 error = got_error_from_errno("asprintf");
889 goto done;
892 error = got_repo_map_path(&in_repo_path, repo, path);
893 if (error)
894 goto done;
896 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
897 GOT_OBJ_TYPE_COMMIT, &refs, repo);
898 if (error)
899 goto done;
901 error = got_object_open_as_commit(&commit, repo, commit_id);
902 if (error)
903 goto done;
905 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
906 if (error)
907 goto done;
909 if (commit_id == NULL) {
910 error = got_error(GOT_ERR_NO_OBJ);
911 goto done;
914 error = got_object_get_type(&obj_type, repo, commit_id);
915 if (error)
916 goto done;
918 if (obj_type != GOT_OBJ_TYPE_BLOB) {
919 error = got_error(GOT_ERR_OBJ_TYPE);
920 goto done;
923 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
924 if (error)
925 goto done;
927 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
928 if (error)
929 goto done;
931 error = got_object_blob_is_binary(binary, *blob);
932 if (error)
933 goto done;
935 done:
936 if (commit)
937 got_object_commit_close(commit);
939 if (error) {
940 if (*fd != -1)
941 close(*fd);
942 if (*blob)
943 got_object_blob_close(*blob);
944 *fd = -1;
945 *blob = NULL;
948 free(in_repo_path);
949 free(commit_id);
950 free(path);
951 return error;
954 int
955 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
956 int (*cb)(struct template *, const char *, size_t))
958 const struct got_error *err;
959 char *line = NULL;
960 size_t linesize = 0;
961 size_t lineno = 0;
962 ssize_t linelen = 0;
964 for (;;) {
965 err = got_object_blob_getline(&line, &linelen, &linesize,
966 blob);
967 if (err || linelen == -1)
968 break;
969 lineno++;
970 if (cb(tp, line, lineno) == -1)
971 break;
974 free(line);
976 if (err) {
977 log_warnx("%s: got_object_blob_getline failed: %s",
978 __func__, err->msg);
979 return -1;
981 return 0;
984 struct blame_cb_args {
985 struct blame_line *lines;
986 int nlines;
987 int nlines_prec;
988 int lineno_cur;
989 off_t *line_offsets;
990 FILE *f;
991 struct got_repository *repo;
992 struct request *c;
993 got_render_blame_line_cb cb;
994 };
996 static const struct got_error *
997 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
998 struct got_commit_object *commit, struct got_object_id *id)
1000 const struct got_error *err = NULL;
1001 struct blame_cb_args *a = arg;
1002 struct blame_line *bline;
1003 struct request *c = a->c;
1004 char *line = NULL;
1005 size_t linesize = 0;
1006 off_t offset;
1007 struct tm tm;
1008 time_t committer_time;
1010 if (nlines != a->nlines ||
1011 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1012 return got_error(GOT_ERR_RANGE);
1014 if (lineno == -1)
1015 return NULL; /* no change in this commit */
1017 /* Annotate this line. */
1018 bline = &a->lines[lineno - 1];
1019 if (bline->annotated)
1020 return NULL;
1021 err = got_object_id_str(&bline->id_str, id);
1022 if (err)
1023 return err;
1025 bline->committer = strdup(got_object_commit_get_committer(commit));
1026 if (bline->committer == NULL) {
1027 err = got_error_from_errno("strdup");
1028 goto done;
1031 committer_time = got_object_commit_get_committer_time(commit);
1032 if (gmtime_r(&committer_time, &tm) == NULL)
1033 return got_error_from_errno("gmtime_r");
1034 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1035 &tm) == 0) {
1036 err = got_error(GOT_ERR_NO_SPACE);
1037 goto done;
1039 bline->annotated = 1;
1041 /* Print lines annotated so far. */
1042 bline = &a->lines[a->lineno_cur - 1];
1043 if (!bline->annotated)
1044 goto done;
1046 offset = a->line_offsets[a->lineno_cur - 1];
1047 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1048 err = got_error_from_errno("fseeko");
1049 goto done;
1052 while (a->lineno_cur <= a->nlines && bline->annotated) {
1053 if (getline(&line, &linesize, a->f) == -1) {
1054 if (ferror(a->f))
1055 err = got_error_from_errno("getline");
1056 break;
1059 if (a->cb(c->tp, line, bline, a->nlines_prec,
1060 a->lineno_cur) == -1)
1061 break;
1063 a->lineno_cur++;
1064 bline = &a->lines[a->lineno_cur - 1];
1066 done:
1067 free(line);
1068 return err;
1071 const struct got_error *
1072 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1074 const struct got_error *error = NULL;
1075 struct transport *t = c->t;
1076 struct got_repository *repo = t->repo;
1077 struct querystring *qs = c->t->qs;
1078 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1079 struct got_commit_object *commit = NULL;
1080 struct got_reflist_head refs;
1081 struct got_blob_object *blob = NULL;
1082 char *path = NULL, *in_repo_path = NULL;
1083 struct blame_cb_args bca;
1084 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1085 int fd6 = -1;
1086 off_t filesize;
1087 FILE *f1 = NULL, *f2 = NULL;
1089 TAILQ_INIT(&refs);
1090 bca.f = NULL;
1091 bca.lines = NULL;
1092 bca.cb = cb;
1094 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1095 qs->folder ? "/" : "", qs->file) == -1) {
1096 error = got_error_from_errno("asprintf");
1097 goto done;
1100 error = got_repo_map_path(&in_repo_path, repo, path);
1101 if (error)
1102 goto done;
1104 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1105 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1106 if (error)
1107 goto done;
1109 error = got_object_open_as_commit(&commit, repo, commit_id);
1110 if (error)
1111 goto done;
1113 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1114 if (error)
1115 goto done;
1117 error = got_object_get_type(&obj_type, repo, obj_id);
1118 if (error)
1119 goto done;
1121 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1122 error = got_error(GOT_ERR_OBJ_TYPE);
1123 goto done;
1126 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1127 if (error)
1128 goto done;
1130 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1131 if (error)
1132 goto done;
1134 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1135 if (error)
1136 goto done;
1138 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1139 &bca.line_offsets, bca.f, blob);
1140 if (error || bca.nlines == 0)
1141 goto done;
1143 /* Don't include \n at EOF in the blame line count. */
1144 if (bca.line_offsets[bca.nlines - 1] == filesize)
1145 bca.nlines--;
1147 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1148 if (bca.lines == NULL) {
1149 error = got_error_from_errno("calloc");
1150 goto done;
1152 bca.lineno_cur = 1;
1153 bca.nlines_prec = 0;
1154 i = bca.nlines;
1155 while (i > 0) {
1156 i /= 10;
1157 bca.nlines_prec++;
1159 bca.repo = repo;
1160 bca.c = c;
1162 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1163 if (error)
1164 goto done;
1166 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1167 if (error)
1168 goto done;
1170 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1171 if (error)
1172 goto done;
1174 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1175 if (error)
1176 goto done;
1178 error = got_blame(in_repo_path, commit_id, repo,
1179 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1180 fd3, fd4, f1, f2);
1182 done:
1183 if (bca.lines) {
1184 free(bca.line_offsets);
1185 for (i = 0; i < bca.nlines; i++) {
1186 struct blame_line *bline = &bca.lines[i];
1187 free(bline->id_str);
1188 free(bline->committer);
1191 free(bca.lines);
1192 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1193 error = got_error_from_errno("close");
1194 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1195 error = got_error_from_errno("close");
1196 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1197 error = got_error_from_errno("close");
1198 if (bca.f) {
1199 const struct got_error *bca_err =
1200 got_gotweb_flushfile(bca.f, fd1);
1201 if (error == NULL)
1202 error = bca_err;
1204 if (f1) {
1205 const struct got_error *f1_err =
1206 got_gotweb_flushfile(f1, fd5);
1207 if (error == NULL)
1208 error = f1_err;
1210 if (f2) {
1211 const struct got_error *f2_err =
1212 got_gotweb_flushfile(f2, fd6);
1213 if (error == NULL)
1214 error = f2_err;
1216 if (commit)
1217 got_object_commit_close(commit);
1218 if (blob)
1219 got_object_blob_close(blob);
1220 free(in_repo_path);
1221 free(commit_id);
1222 free(obj_id);
1223 free(path);
1224 got_ref_list_free(&refs);
1225 return error;
1228 const struct got_error *
1229 got_open_diff_for_output(FILE **fp, int *fd, struct request *c)
1231 const struct got_error *error = NULL;
1232 struct transport *t = c->t;
1233 struct got_repository *repo = t->repo;
1234 struct repo_commit *rc = NULL;
1235 struct got_object_id *id1 = NULL, *id2 = NULL;
1236 struct got_reflist_head refs;
1237 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1238 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1240 *fp = NULL;
1241 *fd = -1;
1243 TAILQ_INIT(&refs);
1245 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1246 if (error)
1247 return error;
1249 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1250 if (error)
1251 return error;
1253 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1254 if (error)
1255 return error;
1257 rc = TAILQ_FIRST(&t->repo_commits);
1259 if (rc->parent_id != NULL &&
1260 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1261 error = got_repo_match_object_id(&id1, NULL,
1262 rc->parent_id, GOT_OBJ_TYPE_ANY,
1263 &refs, repo);
1264 if (error)
1265 goto done;
1268 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1269 GOT_OBJ_TYPE_ANY, &refs, repo);
1270 if (error)
1271 goto done;
1273 error = got_object_get_type(&obj_type, repo, id2);
1274 if (error)
1275 goto done;
1277 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1278 if (error)
1279 goto done;
1281 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1282 if (error)
1283 goto done;
1285 switch (obj_type) {
1286 case GOT_OBJ_TYPE_BLOB:
1287 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1288 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1289 NULL, repo, f3);
1290 break;
1291 case GOT_OBJ_TYPE_TREE:
1292 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1293 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1294 NULL, repo, f3);
1295 break;
1296 case GOT_OBJ_TYPE_COMMIT:
1297 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1298 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1299 NULL, repo, f3);
1300 break;
1301 default:
1302 error = got_error(GOT_ERR_OBJ_TYPE);
1304 if (error)
1305 goto done;
1307 if (fseek(f1, 0, SEEK_SET) == -1) {
1308 error = got_ferror(f1, GOT_ERR_IO);
1309 goto done;
1312 if (fseek(f2, 0, SEEK_SET) == -1) {
1313 error = got_ferror(f2, GOT_ERR_IO);
1314 goto done;
1317 if (fseek(f3, 0, SEEK_SET) == -1) {
1318 error = got_ferror(f3, GOT_ERR_IO);
1319 goto done;
1322 *fp = f3;
1323 *fd = fd3;
1325 done:
1326 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1327 error = got_error_from_errno("close");
1328 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1329 error = got_error_from_errno("close");
1330 if (f1) {
1331 const struct got_error *f1_err =
1332 got_gotweb_flushfile(f1, fd1);
1333 if (error == NULL)
1334 error = f1_err;
1336 if (f2) {
1337 const struct got_error *f2_err =
1338 got_gotweb_flushfile(f2, fd2);
1339 if (error == NULL)
1340 error = f2_err;
1342 if (error && f3) {
1343 got_gotweb_flushfile(f3, fd3);
1344 *fp = NULL;
1345 *fd = -1;
1347 got_ref_list_free(&refs);
1348 free(id1);
1349 free(id2);
1350 return error;
1353 static const struct got_error *
1354 got_init_repo_commit(struct repo_commit **rc)
1356 *rc = calloc(1, sizeof(**rc));
1357 if (*rc == NULL)
1358 return got_error_from_errno2("%s: calloc", __func__);
1360 (*rc)->path = NULL;
1361 (*rc)->refs_str = NULL;
1362 (*rc)->commit_id = NULL;
1363 (*rc)->committer = NULL;
1364 (*rc)->author = NULL;
1365 (*rc)->parent_id = NULL;
1366 (*rc)->tree_id = NULL;
1367 (*rc)->commit_msg = NULL;
1369 return NULL;
1372 static const struct got_error *
1373 got_init_repo_tag(struct repo_tag **rt)
1375 *rt = calloc(1, sizeof(**rt));
1376 if (*rt == NULL)
1377 return got_error_from_errno2("%s: calloc", __func__);
1379 (*rt)->commit_id = NULL;
1380 (*rt)->tag_name = NULL;
1381 (*rt)->tag_commit = NULL;
1382 (*rt)->commit_msg = NULL;
1383 (*rt)->tagger = NULL;
1385 return NULL;