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(char **repo_age, struct request *c,
130 const char *refname, int ref_tm)
132 const struct got_error *error = NULL;
133 struct server *srv = c->srv;
134 struct transport *t = c->t;
135 struct got_repository *repo = t->repo;
136 struct got_commit_object *commit = NULL;
137 struct got_reflist_head refs;
138 struct got_reflist_entry *re;
139 time_t committer_time = 0, cmp_time = 0;
141 *repo_age = NULL;
142 TAILQ_INIT(&refs);
144 if (srv->show_repo_age == 0)
145 return NULL;
147 error = got_ref_list(&refs, repo, "refs/heads",
148 got_ref_cmp_by_name, NULL);
149 if (error)
150 goto done;
152 /*
153 * Find the youngest branch tip in the repository, or the age of
154 * the a specific branch tip if a name was provided by the caller.
155 */
156 TAILQ_FOREACH(re, &refs, entry) {
157 struct got_object_id *id = NULL;
159 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
160 continue;
162 error = got_ref_resolve(&id, repo, re->ref);
163 if (error)
164 goto done;
166 error = got_object_open_as_commit(&commit, repo, id);
167 free(id);
168 if (error)
169 goto done;
171 committer_time =
172 got_object_commit_get_committer_time(commit);
173 got_object_commit_close(commit);
174 if (cmp_time < committer_time)
175 cmp_time = committer_time;
177 if (refname)
178 break;
181 if (cmp_time != 0) {
182 committer_time = cmp_time;
183 error = gotweb_get_time_str(repo_age, committer_time, ref_tm);
185 done:
186 got_ref_list_free(&refs);
187 return error;
190 static const struct got_error *
191 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
192 struct got_commit_object *commit, struct got_reflist_head *refs,
193 struct got_object_id *id)
195 const struct got_error *error = NULL;
196 struct got_reflist_entry *re;
197 struct got_object_id *id2 = NULL;
198 struct got_object_qid *parent_id;
199 struct transport *t = c->t;
200 struct querystring *qs = c->t->qs;
201 char *commit_msg = NULL, *commit_msg0;
203 TAILQ_FOREACH(re, refs, entry) {
204 char *s;
205 const char *name;
206 struct got_tag_object *tag = NULL;
207 struct got_object_id *ref_id;
208 int cmp;
210 if (got_ref_is_symbolic(re->ref))
211 continue;
213 name = got_ref_get_name(re->ref);
214 if (strncmp(name, "refs/", 5) == 0)
215 name += 5;
216 if (strncmp(name, "got/", 4) == 0)
217 continue;
218 if (strncmp(name, "heads/", 6) == 0)
219 name += 6;
220 if (strncmp(name, "remotes/", 8) == 0) {
221 name += 8;
222 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
223 continue;
225 error = got_ref_resolve(&ref_id, t->repo, re->ref);
226 if (error)
227 return error;
228 if (strncmp(name, "tags/", 5) == 0) {
229 error = got_object_open_as_tag(&tag, t->repo, ref_id);
230 if (error) {
231 if (error->code != GOT_ERR_OBJ_TYPE) {
232 free(ref_id);
233 continue;
235 /*
236 * Ref points at something other
237 * than a tag.
238 */
239 error = NULL;
240 tag = NULL;
243 cmp = got_object_id_cmp(tag ?
244 got_object_tag_get_object_id(tag) : ref_id, id);
245 free(ref_id);
246 if (tag)
247 got_object_tag_close(tag);
248 if (cmp != 0)
249 continue;
250 s = repo_commit->refs_str;
251 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
252 s ? ", " : "", name) == -1) {
253 error = got_error_from_errno("asprintf");
254 free(s);
255 repo_commit->refs_str = NULL;
256 return error;
258 free(s);
261 error = got_object_id_str(&repo_commit->commit_id, id);
262 if (error)
263 return error;
265 error = got_object_id_str(&repo_commit->tree_id,
266 got_object_commit_get_tree_id(commit));
267 if (error)
268 return error;
270 if (qs->action == DIFF) {
271 parent_id = STAILQ_FIRST(
272 got_object_commit_get_parent_ids(commit));
273 if (parent_id != NULL) {
274 id2 = got_object_id_dup(&parent_id->id);
275 error = got_object_id_str(&repo_commit->parent_id, id2);
276 if (error)
277 return error;
278 free(id2);
279 } else {
280 repo_commit->parent_id = strdup("/dev/null");
281 if (repo_commit->parent_id == NULL) {
282 error = got_error_from_errno("strdup");
283 return error;
288 repo_commit->committer_time =
289 got_object_commit_get_committer_time(commit);
291 repo_commit->author =
292 strdup(got_object_commit_get_author(commit));
293 if (repo_commit->author == NULL) {
294 error = got_error_from_errno("strdup");
295 return error;
297 repo_commit->committer =
298 strdup(got_object_commit_get_committer(commit));
299 if (repo_commit->committer == NULL) {
300 error = got_error_from_errno("strdup");
301 return error;
303 error = got_object_commit_get_logmsg(&commit_msg0, commit);
304 if (error)
305 return error;
307 commit_msg = commit_msg0;
308 while (*commit_msg == '\n')
309 commit_msg++;
311 repo_commit->commit_msg = strdup(commit_msg);
312 if (repo_commit->commit_msg == NULL)
313 error = got_error_from_errno("strdup");
314 free(commit_msg0);
315 return error;
318 const struct got_error *
319 got_get_repo_commits(struct request *c, int limit)
321 const struct got_error *error = NULL;
322 struct got_object_id *id = NULL;
323 struct got_commit_graph *graph = NULL;
324 struct got_commit_object *commit = NULL;
325 struct got_reflist_head refs;
326 struct got_reference *ref = NULL;
327 struct repo_commit *repo_commit = NULL;
328 struct server *srv = c->srv;
329 struct transport *t = c->t;
330 struct got_repository *repo = t->repo;
331 struct querystring *qs = t->qs;
332 struct repo_dir *repo_dir = t->repo_dir;
333 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
334 int chk_next = 0, chk_multi = 0, commit_found = 0;
335 int obj_type, limit_chk = 0;
337 TAILQ_INIT(&refs);
339 if (qs->file != NULL && strlen(qs->file) > 0)
340 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
341 qs->file) == -1)
342 return got_error_from_errno("asprintf");
344 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
345 repo_dir->name) == -1) {
346 error = got_error_from_errno("asprintf");
347 goto done;
350 /*
351 * XXX: jumping directly to a commit id via
352 * got_repo_match_object_id_prefix significantly improves performance,
353 * but does not allow us to create a PREVIOUS button, since commits can
354 * only be itereated forward. So, we have to match as we iterate from
355 * the headref.
356 */
357 if (qs->action == BRIEFS || qs->action == COMMITS ||
358 (qs->action == TREE && qs->commit == NULL)) {
359 error = got_ref_open(&ref, repo, qs->headref, 0);
360 if (error)
361 goto done;
363 error = got_ref_resolve(&id, repo, ref);
364 if (error)
365 goto done;
366 } else if (qs->commit != NULL) {
367 error = got_ref_open(&ref, repo, qs->commit, 0);
368 if (error == NULL) {
369 error = got_ref_resolve(&id, repo, ref);
370 if (error)
371 goto done;
372 error = got_object_get_type(&obj_type, repo, id);
373 if (error)
374 goto done;
375 if (obj_type == GOT_OBJ_TYPE_TAG) {
376 struct got_tag_object *tag;
377 error = got_object_open_as_tag(&tag, repo, id);
378 if (error)
379 goto done;
380 if (got_object_tag_get_object_type(tag) !=
381 GOT_OBJ_TYPE_COMMIT) {
382 got_object_tag_close(tag);
383 error = got_error(GOT_ERR_OBJ_TYPE);
384 goto done;
386 free(id);
387 id = got_object_id_dup(
388 got_object_tag_get_object_id(tag));
389 if (id == NULL)
390 error = got_error_from_errno(
391 "got_object_id_dup");
392 got_object_tag_close(tag);
393 if (error)
394 goto done;
395 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
396 error = got_error(GOT_ERR_OBJ_TYPE);
397 goto done;
400 error = got_repo_match_object_id_prefix(&id, qs->commit,
401 GOT_OBJ_TYPE_COMMIT, repo);
402 if (error)
403 goto done;
406 error = got_repo_map_path(&in_repo_path, repo, repo_path);
407 if (error)
408 goto done;
410 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
411 if (error)
412 goto done;
414 if (qs->file != NULL && strlen(qs->file) > 0) {
415 error = got_commit_graph_open(&graph, file_path, 0);
416 if (error)
417 goto done;
418 } else {
419 error = got_commit_graph_open(&graph, in_repo_path, 0);
420 if (error)
421 goto done;
424 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
425 if (error)
426 goto done;
428 for (;;) {
429 struct got_object_id next_id;
431 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
432 NULL);
433 if (error) {
434 if (error->code == GOT_ERR_ITER_COMPLETED)
435 error = NULL;
436 goto done;
439 error = got_object_open_as_commit(&commit, repo, &next_id);
440 if (error)
441 goto done;
443 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
444 NULL);
445 if (error)
446 goto done;
448 error = got_init_repo_commit(&repo_commit);
449 if (error)
450 goto done;
452 error = got_get_repo_commit(c, repo_commit, commit,
453 &refs, &next_id);
454 if (error) {
455 gotweb_free_repo_commit(repo_commit);
456 goto done;
459 if (limit_chk == ((limit * qs->page) - limit) &&
460 commit_found == 0 && repo_commit->commit_id != NULL) {
461 t->prev_id = strdup(repo_commit->commit_id);
462 if (t->prev_id == NULL) {
463 error = got_error_from_errno("strdup");
464 gotweb_free_repo_commit(repo_commit);
465 goto done;
469 if (qs->commit != NULL && commit_found == 0 && limit != 1) {
470 if (strcmp(qs->commit, repo_commit->commit_id) == 0)
471 commit_found = 1;
472 else if (qs->file != NULL && strlen(qs->file) > 0 &&
473 qs->page == 0)
474 commit_found = 1;
475 else {
476 gotweb_free_repo_commit(repo_commit);
477 limit_chk++;
478 continue;
482 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
484 if (limit == 1 && chk_multi == 0 &&
485 srv->max_commits_display != 1)
486 commit_found = 1;
487 else {
488 chk_multi = 1;
490 /*
491 * check for one more commit before breaking,
492 * so we know whether to navigate through briefs
493 * commits and summary
494 */
495 if (chk_next && (qs->action == BRIEFS ||
496 qs->action == COMMITS || qs->action == SUMMARY)) {
497 t->next_id = strdup(repo_commit->commit_id);
498 if (t->next_id == NULL) {
499 error = got_error_from_errno("strdup");
500 goto done;
502 if (commit) {
503 got_object_commit_close(commit);
504 commit = NULL;
506 TAILQ_REMOVE(&t->repo_commits, repo_commit,
507 entry);
508 gotweb_free_repo_commit(repo_commit);
509 goto done;
512 got_ref_list_free(&refs);
513 if (error || (limit && --limit == 0)) {
514 if (commit_found || (qs->file != NULL &&
515 strlen(qs->file) > 0))
516 if (chk_multi == 0)
517 break;
518 chk_next = 1;
520 if (commit) {
521 got_object_commit_close(commit);
522 commit = NULL;
525 done:
526 if (ref)
527 got_ref_close(ref);
528 if (commit)
529 got_object_commit_close(commit);
530 if (graph)
531 got_commit_graph_close(graph);
532 got_ref_list_free(&refs);
533 free(in_repo_path);
534 free(file_path);
535 free(repo_path);
536 free(id);
537 return error;
540 const struct got_error *
541 got_get_repo_tags(struct request *c, int limit)
543 const struct got_error *error = NULL;
544 struct got_object_id *id = NULL;
545 struct got_commit_object *commit = NULL;
546 struct got_reflist_head refs;
547 struct got_reference *ref;
548 struct got_reflist_entry *re;
549 struct server *srv = c->srv;
550 struct transport *t = c->t;
551 struct got_repository *repo = t->repo;
552 struct querystring *qs = t->qs;
553 struct repo_dir *repo_dir = t->repo_dir;
554 struct got_tag_object *tag = NULL;
555 struct repo_tag *rt = NULL, *trt = NULL;
556 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
557 char *tag_commit = NULL, *tag_commit0 = NULL;
558 char *commit_msg = NULL, *commit_msg0 = NULL;
559 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
561 TAILQ_INIT(&refs);
563 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
564 repo_dir->name) == -1)
565 return got_error_from_errno("asprintf");
567 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
568 error = got_ref_open(&ref, repo, qs->headref, 0);
569 if (error)
570 goto err;
571 error = got_ref_resolve(&id, repo, ref);
572 got_ref_close(ref);
573 if (error)
574 goto err;
575 } else if (qs->commit == NULL && qs->action == TAG) {
576 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
577 goto err;
578 } else {
579 error = got_repo_match_object_id_prefix(&id, qs->commit,
580 GOT_OBJ_TYPE_COMMIT, repo);
581 if (error)
582 goto err;
585 if (qs->action != SUMMARY && qs->action != TAGS) {
586 error = got_object_open_as_commit(&commit, repo, id);
587 if (error)
588 goto err;
589 error = got_object_commit_get_logmsg(&commit_msg0, commit);
590 if (error)
591 goto err;
592 if (commit) {
593 got_object_commit_close(commit);
594 commit = NULL;
598 error = got_repo_map_path(&in_repo_path, repo, repo_path);
599 if (error)
600 goto err;
602 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
603 repo);
604 if (error)
605 goto err;
607 if (limit == 1)
608 chk_multi = 0;
610 /*
611 * XXX: again, see previous message about caching
612 */
614 TAILQ_FOREACH(re, &refs, entry) {
615 struct repo_tag *new_repo_tag = NULL;
616 error = got_init_repo_tag(&new_repo_tag);
617 if (error)
618 goto err;
620 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
622 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
623 if (new_repo_tag->tag_name == NULL) {
624 error = got_error_from_errno("strdup");
625 goto err;
628 free(id);
629 id = NULL;
631 free(id_str);
632 id_str = NULL;
634 error = got_ref_resolve(&id, repo, re->ref);
635 if (error)
636 goto done;
638 if (tag)
639 got_object_tag_close(tag);
640 error = got_object_open_as_tag(&tag, repo, id);
641 if (error) {
642 if (error->code != GOT_ERR_OBJ_TYPE)
643 goto done;
644 /* "lightweight" tag */
645 error = got_object_open_as_commit(&commit, repo, id);
646 if (error)
647 goto done;
648 new_repo_tag->tagger =
649 strdup(got_object_commit_get_committer(commit));
650 if (new_repo_tag->tagger == NULL) {
651 error = got_error_from_errno("strdup");
652 goto err;
654 new_repo_tag->tagger_time =
655 got_object_commit_get_committer_time(commit);
656 error = got_object_id_str(&id_str, id);
657 if (error)
658 goto err;
659 } else {
660 new_repo_tag->tagger =
661 strdup(got_object_tag_get_tagger(tag));
662 if (new_repo_tag->tagger == NULL) {
663 error = got_error_from_errno("strdup");
664 goto err;
666 new_repo_tag->tagger_time =
667 got_object_tag_get_tagger_time(tag);
668 error = got_object_id_str(&id_str,
669 got_object_tag_get_object_id(tag));
670 if (error)
671 goto err;
674 new_repo_tag->commit_id = strdup(id_str);
675 if (new_repo_tag->commit_id == NULL)
676 goto err;
678 if (commit_found == 0 && qs->commit != NULL &&
679 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
680 continue;
681 else
682 commit_found = 1;
684 t->tag_count++;
686 /*
687 * check for one more commit before breaking,
688 * so we know whether to navigate through briefs
689 * commits and summary
690 */
691 if (chk_next) {
692 t->next_id = strdup(new_repo_tag->commit_id);
693 if (t->next_id == NULL) {
694 error = got_error_from_errno("strdup");
695 goto err;
697 if (commit) {
698 got_object_commit_close(commit);
699 commit = NULL;
701 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
702 gotweb_free_repo_tag(new_repo_tag);
703 goto done;
706 if (commit) {
707 error = got_object_commit_get_logmsg(&tag_commit0,
708 commit);
709 if (error)
710 goto err;
711 got_object_commit_close(commit);
712 commit = NULL;
713 } else {
714 tag_commit0 = strdup(got_object_tag_get_message(tag));
715 if (tag_commit0 == NULL) {
716 error = got_error_from_errno("strdup");
717 goto err;
721 tag_commit = tag_commit0;
722 while (*tag_commit == '\n')
723 tag_commit++;
724 new_repo_tag->tag_commit = strdup(tag_commit);
725 if (new_repo_tag->tag_commit == NULL) {
726 error = got_error_from_errno("strdup");
727 free(tag_commit0);
728 goto err;
730 free(tag_commit0);
732 if (qs->action != SUMMARY && qs->action != TAGS) {
733 commit_msg = commit_msg0;
734 while (*commit_msg == '\n')
735 commit_msg++;
737 new_repo_tag->commit_msg = strdup(commit_msg);
738 if (new_repo_tag->commit_msg == NULL) {
739 error = got_error_from_errno("strdup");
740 goto err;
744 if (limit && --limit == 0) {
745 if (chk_multi == 0)
746 break;
747 chk_next = 1;
751 done:
752 /*
753 * we have tailq populated, so find previous commit id
754 * for navigation through briefs and commits
755 */
756 if (t->tag_count == 0) {
757 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
758 TAILQ_REMOVE(&t->repo_tags, rt, entry);
759 gotweb_free_repo_tag(rt);
762 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
763 commit_found = 0;
764 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
765 entry) {
766 if (commit_found == 0 && rt->commit_id != NULL &&
767 strcmp(qs->commit, rt->commit_id) != 0) {
768 continue;
769 } else
770 commit_found = 1;
771 if (c_cnt == srv->max_commits_display ||
772 rt == TAILQ_FIRST(&t->repo_tags)) {
773 t->prev_id = strdup(rt->commit_id);
774 if (t->prev_id == NULL)
775 error = got_error_from_errno("strdup");
776 break;
778 c_cnt++;
781 err:
782 if (commit)
783 got_object_commit_close(commit);
784 if (tag)
785 got_object_tag_close(tag);
786 got_ref_list_free(&refs);
787 free(commit_msg0);
788 free(in_repo_path);
789 free(repo_path);
790 free(id);
791 free(id_str);
792 return error;
795 int
796 got_output_repo_tree(struct request *c,
797 int (*cb)(struct template *, struct got_tree_entry *))
799 const struct got_error *error = NULL;
800 struct transport *t = c->t;
801 struct got_commit_object *commit = NULL;
802 struct got_repository *repo = t->repo;
803 struct querystring *qs = t->qs;
804 struct repo_commit *rc = NULL;
805 struct got_object_id *tree_id = NULL, *commit_id = NULL;
806 struct got_reflist_head refs;
807 struct got_tree_object *tree = NULL;
808 struct got_tree_entry *te;
809 struct repo_dir *repo_dir = t->repo_dir;
810 char *escaped_name = NULL, *path = NULL;
811 int nentries, i;
813 TAILQ_INIT(&refs);
815 rc = TAILQ_FIRST(&t->repo_commits);
817 if (qs->folder != NULL) {
818 path = strdup(qs->folder);
819 if (path == NULL) {
820 error = got_error_from_errno("strdup");
821 goto done;
823 } else {
824 error = got_repo_map_path(&path, repo, repo_dir->path);
825 if (error)
826 goto done;
829 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
830 GOT_OBJ_TYPE_COMMIT, &refs, repo);
831 if (error)
832 goto done;
834 error = got_object_open_as_commit(&commit, repo, commit_id);
835 if (error)
836 goto done;
838 error = got_object_id_by_path(&tree_id, repo, commit, path);
839 if (error)
840 goto done;
842 error = got_object_open_as_tree(&tree, repo, tree_id);
843 if (error)
844 goto done;
846 nentries = got_object_tree_get_nentries(tree);
848 for (i = 0; i < nentries; i++) {
849 te = got_object_tree_get_entry(tree, i);
850 if (cb(c->tp, te) == -1)
851 break;
853 done:
854 free(escaped_name);
855 free(path);
856 got_ref_list_free(&refs);
857 if (commit)
858 got_object_commit_close(commit);
859 if (tree)
860 got_object_tree_close(tree);
861 free(commit_id);
862 free(tree_id);
863 if (error) {
864 log_warnx("%s: %s", __func__, error->msg);
865 return -1;
867 return 0;
870 const struct got_error *
871 got_open_blob_for_output(struct got_blob_object **blob, int *fd,
872 int *binary, struct request *c)
874 const struct got_error *error = NULL;
875 struct transport *t = c->t;
876 struct got_repository *repo = t->repo;
877 struct querystring *qs = c->t->qs;
878 struct got_commit_object *commit = NULL;
879 struct got_object_id *commit_id = NULL;
880 struct got_reflist_head refs;
881 char *path = NULL, *in_repo_path = NULL;
882 int obj_type;
884 TAILQ_INIT(&refs);
886 *blob = NULL;
887 *fd = -1;
888 *binary = 0;
890 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
891 qs->folder ? "/" : "", qs->file) == -1) {
892 error = got_error_from_errno("asprintf");
893 goto done;
896 error = got_repo_map_path(&in_repo_path, repo, path);
897 if (error)
898 goto done;
900 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
901 GOT_OBJ_TYPE_COMMIT, &refs, repo);
902 if (error)
903 goto done;
905 error = got_object_open_as_commit(&commit, repo, commit_id);
906 if (error)
907 goto done;
909 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
910 if (error)
911 goto done;
913 if (commit_id == NULL) {
914 error = got_error(GOT_ERR_NO_OBJ);
915 goto done;
918 error = got_object_get_type(&obj_type, repo, commit_id);
919 if (error)
920 goto done;
922 if (obj_type != GOT_OBJ_TYPE_BLOB) {
923 error = got_error(GOT_ERR_OBJ_TYPE);
924 goto done;
927 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], fd);
928 if (error)
929 goto done;
931 error = got_object_open_as_blob(blob, repo, commit_id, BUF, *fd);
932 if (error)
933 goto done;
935 error = got_object_blob_is_binary(binary, *blob);
936 if (error)
937 goto done;
939 done:
940 if (commit)
941 got_object_commit_close(commit);
943 if (error) {
944 if (*fd != -1)
945 close(*fd);
946 if (*blob)
947 got_object_blob_close(*blob);
948 *fd = -1;
949 *blob = NULL;
952 free(in_repo_path);
953 free(commit_id);
954 free(path);
955 return error;
958 int
959 got_output_blob_by_lines(struct template *tp, struct got_blob_object *blob,
960 int (*cb)(struct template *, const char *, size_t))
962 const struct got_error *err;
963 char *line = NULL;
964 size_t linesize = 0;
965 size_t lineno = 0;
966 ssize_t linelen = 0;
968 for (;;) {
969 err = got_object_blob_getline(&line, &linelen, &linesize,
970 blob);
971 if (err || linelen == -1)
972 break;
973 lineno++;
974 if (cb(tp, line, lineno) == -1)
975 break;
978 free(line);
980 if (err) {
981 log_warnx("%s: got_object_blob_getline failed: %s",
982 __func__, err->msg);
983 return -1;
985 return 0;
988 struct blame_cb_args {
989 struct blame_line *lines;
990 int nlines;
991 int nlines_prec;
992 int lineno_cur;
993 off_t *line_offsets;
994 FILE *f;
995 struct got_repository *repo;
996 struct request *c;
997 got_render_blame_line_cb cb;
998 };
1000 static const struct got_error *
1001 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1002 struct got_commit_object *commit, struct got_object_id *id)
1004 const struct got_error *err = NULL;
1005 struct blame_cb_args *a = arg;
1006 struct blame_line *bline;
1007 struct request *c = a->c;
1008 char *line = NULL;
1009 size_t linesize = 0;
1010 off_t offset;
1011 struct tm tm;
1012 time_t committer_time;
1014 if (nlines != a->nlines ||
1015 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1016 return got_error(GOT_ERR_RANGE);
1018 if (lineno == -1)
1019 return NULL; /* no change in this commit */
1021 /* Annotate this line. */
1022 bline = &a->lines[lineno - 1];
1023 if (bline->annotated)
1024 return NULL;
1025 err = got_object_id_str(&bline->id_str, id);
1026 if (err)
1027 return err;
1029 bline->committer = strdup(got_object_commit_get_committer(commit));
1030 if (bline->committer == NULL) {
1031 err = got_error_from_errno("strdup");
1032 goto done;
1035 committer_time = got_object_commit_get_committer_time(commit);
1036 if (gmtime_r(&committer_time, &tm) == NULL)
1037 return got_error_from_errno("gmtime_r");
1038 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1039 &tm) == 0) {
1040 err = got_error(GOT_ERR_NO_SPACE);
1041 goto done;
1043 bline->annotated = 1;
1045 /* Print lines annotated so far. */
1046 bline = &a->lines[a->lineno_cur - 1];
1047 if (!bline->annotated)
1048 goto done;
1050 offset = a->line_offsets[a->lineno_cur - 1];
1051 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1052 err = got_error_from_errno("fseeko");
1053 goto done;
1056 while (a->lineno_cur <= a->nlines && bline->annotated) {
1057 if (getline(&line, &linesize, a->f) == -1) {
1058 if (ferror(a->f))
1059 err = got_error_from_errno("getline");
1060 break;
1063 if (a->cb(c->tp, line, bline, a->nlines_prec,
1064 a->lineno_cur) == -1)
1065 break;
1067 a->lineno_cur++;
1068 bline = &a->lines[a->lineno_cur - 1];
1070 done:
1071 free(line);
1072 return err;
1075 const struct got_error *
1076 got_output_file_blame(struct request *c, got_render_blame_line_cb cb)
1078 const struct got_error *error = NULL;
1079 struct transport *t = c->t;
1080 struct got_repository *repo = t->repo;
1081 struct querystring *qs = c->t->qs;
1082 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1083 struct got_commit_object *commit = NULL;
1084 struct got_reflist_head refs;
1085 struct got_blob_object *blob = NULL;
1086 char *path = NULL, *in_repo_path = NULL;
1087 struct blame_cb_args bca;
1088 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1089 int fd6 = -1;
1090 off_t filesize;
1091 FILE *f1 = NULL, *f2 = NULL;
1093 TAILQ_INIT(&refs);
1094 bca.f = NULL;
1095 bca.lines = NULL;
1096 bca.cb = cb;
1098 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1099 qs->folder ? "/" : "", qs->file) == -1) {
1100 error = got_error_from_errno("asprintf");
1101 goto done;
1104 error = got_repo_map_path(&in_repo_path, repo, path);
1105 if (error)
1106 goto done;
1108 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1109 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1110 if (error)
1111 goto done;
1113 error = got_object_open_as_commit(&commit, repo, commit_id);
1114 if (error)
1115 goto done;
1117 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1118 if (error)
1119 goto done;
1121 error = got_object_get_type(&obj_type, repo, obj_id);
1122 if (error)
1123 goto done;
1125 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1126 error = got_error(GOT_ERR_OBJ_TYPE);
1127 goto done;
1130 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1131 if (error)
1132 goto done;
1134 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1135 if (error)
1136 goto done;
1138 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1139 if (error)
1140 goto done;
1142 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1143 &bca.line_offsets, bca.f, blob);
1144 if (error || bca.nlines == 0)
1145 goto done;
1147 /* Don't include \n at EOF in the blame line count. */
1148 if (bca.line_offsets[bca.nlines - 1] == filesize)
1149 bca.nlines--;
1151 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1152 if (bca.lines == NULL) {
1153 error = got_error_from_errno("calloc");
1154 goto done;
1156 bca.lineno_cur = 1;
1157 bca.nlines_prec = 0;
1158 i = bca.nlines;
1159 while (i > 0) {
1160 i /= 10;
1161 bca.nlines_prec++;
1163 bca.repo = repo;
1164 bca.c = c;
1166 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1167 if (error)
1168 goto done;
1170 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1171 if (error)
1172 goto done;
1174 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1175 if (error)
1176 goto done;
1178 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1179 if (error)
1180 goto done;
1182 error = got_blame(in_repo_path, commit_id, repo,
1183 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1184 fd3, fd4, f1, f2);
1186 done:
1187 if (bca.lines) {
1188 free(bca.line_offsets);
1189 for (i = 0; i < bca.nlines; i++) {
1190 struct blame_line *bline = &bca.lines[i];
1191 free(bline->id_str);
1192 free(bline->committer);
1195 free(bca.lines);
1196 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1197 error = got_error_from_errno("close");
1198 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1199 error = got_error_from_errno("close");
1200 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1201 error = got_error_from_errno("close");
1202 if (bca.f) {
1203 const struct got_error *bca_err =
1204 got_gotweb_flushfile(bca.f, fd1);
1205 if (error == NULL)
1206 error = bca_err;
1208 if (f1) {
1209 const struct got_error *f1_err =
1210 got_gotweb_flushfile(f1, fd5);
1211 if (error == NULL)
1212 error = f1_err;
1214 if (f2) {
1215 const struct got_error *f2_err =
1216 got_gotweb_flushfile(f2, fd6);
1217 if (error == NULL)
1218 error = f2_err;
1220 if (commit)
1221 got_object_commit_close(commit);
1222 if (blob)
1223 got_object_blob_close(blob);
1224 free(in_repo_path);
1225 free(commit_id);
1226 free(obj_id);
1227 free(path);
1228 got_ref_list_free(&refs);
1229 return error;
1232 const struct got_error *
1233 got_open_diff_for_output(FILE **fp, int *fd, struct request *c)
1235 const struct got_error *error = NULL;
1236 struct transport *t = c->t;
1237 struct got_repository *repo = t->repo;
1238 struct repo_commit *rc = NULL;
1239 struct got_object_id *id1 = NULL, *id2 = NULL;
1240 struct got_reflist_head refs;
1241 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1242 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1244 *fp = NULL;
1245 *fd = -1;
1247 TAILQ_INIT(&refs);
1249 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1250 if (error)
1251 return error;
1253 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1254 if (error)
1255 return error;
1257 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1258 if (error)
1259 return error;
1261 rc = TAILQ_FIRST(&t->repo_commits);
1263 if (rc->parent_id != NULL &&
1264 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1265 error = got_repo_match_object_id(&id1, NULL,
1266 rc->parent_id, GOT_OBJ_TYPE_ANY,
1267 &refs, repo);
1268 if (error)
1269 goto done;
1272 error = got_repo_match_object_id(&id2, NULL, rc->commit_id,
1273 GOT_OBJ_TYPE_ANY, &refs, repo);
1274 if (error)
1275 goto done;
1277 error = got_object_get_type(&obj_type, repo, id2);
1278 if (error)
1279 goto done;
1281 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1282 if (error)
1283 goto done;
1285 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1286 if (error)
1287 goto done;
1289 switch (obj_type) {
1290 case GOT_OBJ_TYPE_BLOB:
1291 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1292 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1293 NULL, repo, f3);
1294 break;
1295 case GOT_OBJ_TYPE_TREE:
1296 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1297 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1298 NULL, repo, f3);
1299 break;
1300 case GOT_OBJ_TYPE_COMMIT:
1301 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1302 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1303 NULL, repo, f3);
1304 break;
1305 default:
1306 error = got_error(GOT_ERR_OBJ_TYPE);
1308 if (error)
1309 goto done;
1311 if (fseek(f1, 0, SEEK_SET) == -1) {
1312 error = got_ferror(f1, GOT_ERR_IO);
1313 goto done;
1316 if (fseek(f2, 0, SEEK_SET) == -1) {
1317 error = got_ferror(f2, GOT_ERR_IO);
1318 goto done;
1321 if (fseek(f3, 0, SEEK_SET) == -1) {
1322 error = got_ferror(f3, GOT_ERR_IO);
1323 goto done;
1326 *fp = f3;
1327 *fd = fd3;
1329 done:
1330 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1331 error = got_error_from_errno("close");
1332 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1333 error = got_error_from_errno("close");
1334 if (f1) {
1335 const struct got_error *f1_err =
1336 got_gotweb_flushfile(f1, fd1);
1337 if (error == NULL)
1338 error = f1_err;
1340 if (f2) {
1341 const struct got_error *f2_err =
1342 got_gotweb_flushfile(f2, fd2);
1343 if (error == NULL)
1344 error = f2_err;
1346 if (error && f3) {
1347 got_gotweb_flushfile(f3, fd3);
1348 *fp = NULL;
1349 *fd = -1;
1351 got_ref_list_free(&refs);
1352 free(id1);
1353 free(id2);
1354 return error;
1357 static const struct got_error *
1358 got_init_repo_commit(struct repo_commit **rc)
1360 *rc = calloc(1, sizeof(**rc));
1361 if (*rc == NULL)
1362 return got_error_from_errno2("%s: calloc", __func__);
1364 (*rc)->path = NULL;
1365 (*rc)->refs_str = NULL;
1366 (*rc)->commit_id = NULL;
1367 (*rc)->committer = NULL;
1368 (*rc)->author = NULL;
1369 (*rc)->parent_id = NULL;
1370 (*rc)->tree_id = NULL;
1371 (*rc)->commit_msg = NULL;
1373 return NULL;
1376 static const struct got_error *
1377 got_init_repo_tag(struct repo_tag **rt)
1379 *rt = calloc(1, sizeof(**rt));
1380 if (*rt == NULL)
1381 return got_error_from_errno2("%s: calloc", __func__);
1383 (*rt)->commit_id = NULL;
1384 (*rt)->tag_name = NULL;
1385 (*rt)->tag_commit = NULL;
1386 (*rt)->commit_msg = NULL;
1387 (*rt)->tagger = NULL;
1389 return NULL;