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_flushfile(FILE *, int);
52 static const struct got_error *got_gotweb_blame_cb(void *, int, int,
53 struct got_commit_object *,struct got_object_id *);
55 static const struct got_error *
56 got_gotweb_flushfile(FILE *f, int fd)
57 {
58 if (fseek(f, 0, SEEK_SET) == -1)
59 return got_error_from_errno("fseek");
61 if (ftruncate(fd, 0) == -1)
62 return got_error_from_errno("ftruncate");
64 if (fsync(fd) == -1)
65 return got_error_from_errno("fsync");
67 if (f && fclose(f) == EOF)
68 return got_error_from_errno("fclose");
70 if (fd != -1 && close(fd) != -1)
71 return got_error_from_errno("close");
73 return NULL;
74 }
76 static const struct got_error *
77 got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
78 {
79 *fd = dup(*priv_fd);
80 if (*fd == -1)
81 return got_error_from_errno("dup");
83 *f = fdopen(*fd, "w+");
84 if (*f == NULL) {
85 close(*fd);
86 return got_error(GOT_ERR_PRIVSEP_NO_FD);
87 }
89 return NULL;
90 }
92 static const struct got_error *
93 got_gotweb_dupfd(int *priv_fd, int *fd)
94 {
95 *fd = dup(*priv_fd);
97 if (*fd < 0)
98 return NULL;
100 return NULL;
103 const struct got_error *
104 got_get_repo_owner(char **owner, struct request *c)
106 struct server *srv = c->srv;
107 struct transport *t = c->t;
108 struct got_repository *repo = t->repo;
109 const char *gitconfig_owner;
111 *owner = NULL;
113 if (srv->show_repo_owner == 0)
114 return NULL;
116 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
117 if (gitconfig_owner) {
118 *owner = strdup(gitconfig_owner);
119 if (*owner == NULL)
120 return got_error_from_errno("strdup");
121 } else {
122 *owner = strdup("");
123 if (*owner == NULL)
124 return got_error_from_errno("strdup");
126 return NULL;
129 const struct got_error *
130 got_get_repo_age(char **repo_age, struct request *c,
131 const char *refname, int ref_tm)
133 const struct got_error *error = NULL;
134 struct server *srv = c->srv;
135 struct transport *t = c->t;
136 struct got_repository *repo = t->repo;
137 struct got_commit_object *commit = NULL;
138 struct got_reflist_head refs;
139 struct got_reflist_entry *re;
140 time_t committer_time = 0, cmp_time = 0;
142 *repo_age = NULL;
143 TAILQ_INIT(&refs);
145 if (srv->show_repo_age == 0)
146 return NULL;
148 error = got_ref_list(&refs, repo, "refs/heads",
149 got_ref_cmp_by_name, NULL);
150 if (error)
151 goto done;
153 /*
154 * Find the youngest branch tip in the repository, or the age of
155 * the a specific branch tip if a name was provided by the caller.
156 */
157 TAILQ_FOREACH(re, &refs, entry) {
158 struct got_object_id *id = NULL;
160 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
161 continue;
163 error = got_ref_resolve(&id, repo, re->ref);
164 if (error)
165 goto done;
167 error = got_object_open_as_commit(&commit, repo, id);
168 free(id);
169 if (error)
170 goto done;
172 committer_time =
173 got_object_commit_get_committer_time(commit);
174 got_object_commit_close(commit);
175 if (cmp_time < committer_time)
176 cmp_time = committer_time;
178 if (refname)
179 break;
182 if (cmp_time != 0) {
183 committer_time = cmp_time;
184 error = gotweb_get_time_str(repo_age, committer_time, ref_tm);
186 done:
187 got_ref_list_free(&refs);
188 return error;
191 static const struct got_error *
192 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
193 struct got_commit_object *commit, struct got_reflist_head *refs,
194 struct got_object_id *id)
196 const struct got_error *error = NULL;
197 struct got_reflist_entry *re;
198 struct got_object_id *id2 = NULL;
199 struct got_object_qid *parent_id;
200 struct transport *t = c->t;
201 struct querystring *qs = c->t->qs;
202 char *commit_msg = NULL, *commit_msg0;
204 TAILQ_FOREACH(re, refs, entry) {
205 char *s;
206 const char *name;
207 struct got_tag_object *tag = NULL;
208 struct got_object_id *ref_id;
209 int cmp;
211 if (got_ref_is_symbolic(re->ref))
212 continue;
214 name = got_ref_get_name(re->ref);
215 if (strncmp(name, "refs/", 5) == 0)
216 name += 5;
217 if (strncmp(name, "got/", 4) == 0)
218 continue;
219 if (strncmp(name, "heads/", 6) == 0)
220 name += 6;
221 if (strncmp(name, "remotes/", 8) == 0) {
222 name += 8;
223 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
224 continue;
226 error = got_ref_resolve(&ref_id, t->repo, re->ref);
227 if (error)
228 return error;
229 if (strncmp(name, "tags/", 5) == 0) {
230 error = got_object_open_as_tag(&tag, t->repo, ref_id);
231 if (error) {
232 if (error->code != GOT_ERR_OBJ_TYPE) {
233 free(ref_id);
234 continue;
236 /*
237 * Ref points at something other
238 * than a tag.
239 */
240 error = NULL;
241 tag = NULL;
244 cmp = got_object_id_cmp(tag ?
245 got_object_tag_get_object_id(tag) : ref_id, id);
246 free(ref_id);
247 if (tag)
248 got_object_tag_close(tag);
249 if (cmp != 0)
250 continue;
251 s = repo_commit->refs_str;
252 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
253 s ? ", " : "", name) == -1) {
254 error = got_error_from_errno("asprintf");
255 free(s);
256 repo_commit->refs_str = NULL;
257 return error;
259 free(s);
262 error = got_object_id_str(&repo_commit->commit_id, id);
263 if (error)
264 return error;
266 error = got_object_id_str(&repo_commit->tree_id,
267 got_object_commit_get_tree_id(commit));
268 if (error)
269 return error;
271 if (qs->action == DIFF) {
272 parent_id = STAILQ_FIRST(
273 got_object_commit_get_parent_ids(commit));
274 if (parent_id != NULL) {
275 id2 = got_object_id_dup(&parent_id->id);
276 error = got_object_id_str(&repo_commit->parent_id, id2);
277 if (error)
278 return error;
279 free(id2);
280 } else {
281 repo_commit->parent_id = strdup("/dev/null");
282 if (repo_commit->parent_id == NULL) {
283 error = got_error_from_errno("strdup");
284 return error;
289 repo_commit->committer_time =
290 got_object_commit_get_committer_time(commit);
292 repo_commit->author =
293 strdup(got_object_commit_get_author(commit));
294 if (repo_commit->author == NULL) {
295 error = got_error_from_errno("strdup");
296 return error;
298 repo_commit->committer =
299 strdup(got_object_commit_get_committer(commit));
300 if (repo_commit->committer == NULL) {
301 error = got_error_from_errno("strdup");
302 return error;
304 error = got_object_commit_get_logmsg(&commit_msg0, commit);
305 if (error)
306 return error;
308 commit_msg = commit_msg0;
309 while (*commit_msg == '\n')
310 commit_msg++;
312 repo_commit->commit_msg = strdup(commit_msg);
313 if (repo_commit->commit_msg == NULL)
314 error = got_error_from_errno("strdup");
315 free(commit_msg0);
316 return error;
319 const struct got_error *
320 got_get_repo_commits(struct request *c, int limit)
322 const struct got_error *error = NULL;
323 struct got_object_id *id = NULL;
324 struct got_commit_graph *graph = NULL;
325 struct got_commit_object *commit = NULL;
326 struct got_reflist_head refs;
327 struct got_reference *ref = NULL;
328 struct repo_commit *repo_commit = NULL;
329 struct server *srv = c->srv;
330 struct transport *t = c->t;
331 struct got_repository *repo = t->repo;
332 struct querystring *qs = t->qs;
333 struct repo_dir *repo_dir = t->repo_dir;
334 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
335 int chk_next = 0, chk_multi = 0, commit_found = 0;
336 int obj_type, limit_chk = 0;
338 TAILQ_INIT(&refs);
340 if (qs->file != NULL && strlen(qs->file) > 0)
341 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
342 qs->file) == -1)
343 return got_error_from_errno("asprintf");
345 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
346 repo_dir->name) == -1) {
347 error = got_error_from_errno("asprintf");
348 goto done;
351 /*
352 * XXX: jumping directly to a commit id via
353 * got_repo_match_object_id_prefix significantly improves performance,
354 * but does not allow us to create a PREVIOUS button, since commits can
355 * only be itereated forward. So, we have to match as we iterate from
356 * the headref.
357 */
358 if (qs->action == BRIEFS || qs->action == COMMITS ||
359 (qs->action == TREE && qs->commit == NULL)) {
360 error = got_ref_open(&ref, repo, qs->headref, 0);
361 if (error)
362 goto done;
364 error = got_ref_resolve(&id, repo, ref);
365 if (error)
366 goto done;
367 } else if (qs->commit != NULL) {
368 error = got_ref_open(&ref, repo, qs->commit, 0);
369 if (error == NULL) {
370 error = got_ref_resolve(&id, repo, ref);
371 if (error)
372 goto done;
373 error = got_object_get_type(&obj_type, repo, id);
374 if (error)
375 goto done;
376 if (obj_type == GOT_OBJ_TYPE_TAG) {
377 struct got_tag_object *tag;
378 error = got_object_open_as_tag(&tag, repo, id);
379 if (error)
380 goto done;
381 if (got_object_tag_get_object_type(tag) !=
382 GOT_OBJ_TYPE_COMMIT) {
383 got_object_tag_close(tag);
384 error = got_error(GOT_ERR_OBJ_TYPE);
385 goto done;
387 free(id);
388 id = got_object_id_dup(
389 got_object_tag_get_object_id(tag));
390 if (id == NULL)
391 error = got_error_from_errno(
392 "got_object_id_dup");
393 got_object_tag_close(tag);
394 if (error)
395 goto done;
396 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
397 error = got_error(GOT_ERR_OBJ_TYPE);
398 goto done;
401 error = got_repo_match_object_id_prefix(&id, qs->commit,
402 GOT_OBJ_TYPE_COMMIT, repo);
403 if (error)
404 goto done;
407 error = got_repo_map_path(&in_repo_path, repo, repo_path);
408 if (error)
409 goto done;
411 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
412 if (error)
413 goto done;
415 if (qs->file != NULL && strlen(qs->file) > 0) {
416 error = got_commit_graph_open(&graph, file_path, 0);
417 if (error)
418 goto done;
419 } else {
420 error = got_commit_graph_open(&graph, in_repo_path, 0);
421 if (error)
422 goto done;
425 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
426 if (error)
427 goto done;
429 for (;;) {
430 struct got_object_id next_id;
432 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
433 NULL);
434 if (error) {
435 if (error->code == GOT_ERR_ITER_COMPLETED)
436 error = NULL;
437 goto done;
440 error = got_object_open_as_commit(&commit, repo, &next_id);
441 if (error)
442 goto done;
444 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
445 NULL);
446 if (error)
447 goto done;
449 error = got_init_repo_commit(&repo_commit);
450 if (error)
451 goto done;
453 error = got_get_repo_commit(c, repo_commit, commit,
454 &refs, &next_id);
455 if (error) {
456 gotweb_free_repo_commit(repo_commit);
457 goto done;
460 if (limit_chk == ((limit * qs->page) - limit) &&
461 commit_found == 0 && repo_commit->commit_id != NULL) {
462 t->prev_id = strdup(repo_commit->commit_id);
463 if (t->prev_id == NULL) {
464 error = got_error_from_errno("strdup");
465 gotweb_free_repo_commit(repo_commit);
466 goto done;
470 if (qs->commit != NULL && commit_found == 0 && limit != 1) {
471 if (strcmp(qs->commit, repo_commit->commit_id) == 0)
472 commit_found = 1;
473 else if (qs->file != NULL && strlen(qs->file) > 0 &&
474 qs->page == 0)
475 commit_found = 1;
476 else {
477 gotweb_free_repo_commit(repo_commit);
478 limit_chk++;
479 continue;
483 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
485 if (limit == 1 && chk_multi == 0 &&
486 srv->max_commits_display != 1)
487 commit_found = 1;
488 else {
489 chk_multi = 1;
491 /*
492 * check for one more commit before breaking,
493 * so we know whether to navigate through briefs
494 * commits and summary
495 */
496 if (chk_next && (qs->action == BRIEFS ||
497 qs->action == COMMITS || qs->action == SUMMARY)) {
498 t->next_id = strdup(repo_commit->commit_id);
499 if (t->next_id == NULL) {
500 error = got_error_from_errno("strdup");
501 goto done;
503 if (commit) {
504 got_object_commit_close(commit);
505 commit = NULL;
507 TAILQ_REMOVE(&t->repo_commits, repo_commit,
508 entry);
509 gotweb_free_repo_commit(repo_commit);
510 goto done;
513 got_ref_list_free(&refs);
514 if (error || (limit && --limit == 0)) {
515 if (commit_found || (qs->file != NULL &&
516 strlen(qs->file) > 0))
517 if (chk_multi == 0)
518 break;
519 chk_next = 1;
521 if (commit) {
522 got_object_commit_close(commit);
523 commit = NULL;
526 done:
527 if (ref)
528 got_ref_close(ref);
529 if (commit)
530 got_object_commit_close(commit);
531 if (graph)
532 got_commit_graph_close(graph);
533 got_ref_list_free(&refs);
534 free(in_repo_path);
535 free(file_path);
536 free(repo_path);
537 free(id);
538 return error;
541 const struct got_error *
542 got_get_repo_tags(struct request *c, int limit)
544 const struct got_error *error = NULL;
545 struct got_object_id *id = NULL;
546 struct got_commit_object *commit = NULL;
547 struct got_reflist_head refs;
548 struct got_reference *ref;
549 struct got_reflist_entry *re;
550 struct server *srv = c->srv;
551 struct transport *t = c->t;
552 struct got_repository *repo = t->repo;
553 struct querystring *qs = t->qs;
554 struct repo_dir *repo_dir = t->repo_dir;
555 struct got_tag_object *tag = NULL;
556 struct repo_tag *rt = NULL, *trt = NULL;
557 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
558 char *tag_commit = NULL, *tag_commit0 = NULL;
559 char *commit_msg = NULL, *commit_msg0 = NULL;
560 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
562 TAILQ_INIT(&refs);
564 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
565 repo_dir->name) == -1)
566 return got_error_from_errno("asprintf");
568 if (qs->commit == NULL && (qs->action == TAGS || qs->action == RSS)) {
569 error = got_ref_open(&ref, repo, qs->headref, 0);
570 if (error)
571 goto err;
572 error = got_ref_resolve(&id, repo, ref);
573 got_ref_close(ref);
574 if (error)
575 goto err;
576 } else if (qs->commit == NULL && qs->action == TAG) {
577 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
578 goto err;
579 } else {
580 error = got_repo_match_object_id_prefix(&id, qs->commit,
581 GOT_OBJ_TYPE_COMMIT, repo);
582 if (error)
583 goto err;
586 if (qs->action != SUMMARY && qs->action != TAGS) {
587 error = got_object_open_as_commit(&commit, repo, id);
588 if (error)
589 goto err;
590 error = got_object_commit_get_logmsg(&commit_msg0, commit);
591 if (error)
592 goto err;
593 if (commit) {
594 got_object_commit_close(commit);
595 commit = NULL;
599 error = got_repo_map_path(&in_repo_path, repo, repo_path);
600 if (error)
601 goto err;
603 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
604 repo);
605 if (error)
606 goto err;
608 if (limit == 1)
609 chk_multi = 0;
611 /*
612 * XXX: again, see previous message about caching
613 */
615 TAILQ_FOREACH(re, &refs, entry) {
616 struct repo_tag *new_repo_tag = NULL;
617 error = got_init_repo_tag(&new_repo_tag);
618 if (error)
619 goto err;
621 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
623 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
624 if (new_repo_tag->tag_name == NULL) {
625 error = got_error_from_errno("strdup");
626 goto err;
629 free(id);
630 id = NULL;
632 free(id_str);
633 id_str = NULL;
635 error = got_ref_resolve(&id, repo, re->ref);
636 if (error)
637 goto done;
639 if (tag)
640 got_object_tag_close(tag);
641 error = got_object_open_as_tag(&tag, repo, id);
642 if (error) {
643 if (error->code != GOT_ERR_OBJ_TYPE)
644 goto done;
645 /* "lightweight" tag */
646 error = got_object_open_as_commit(&commit, repo, id);
647 if (error)
648 goto done;
649 new_repo_tag->tagger =
650 strdup(got_object_commit_get_committer(commit));
651 if (new_repo_tag->tagger == NULL) {
652 error = got_error_from_errno("strdup");
653 goto err;
655 new_repo_tag->tagger_time =
656 got_object_commit_get_committer_time(commit);
657 error = got_object_id_str(&id_str, id);
658 if (error)
659 goto err;
660 } else {
661 new_repo_tag->tagger =
662 strdup(got_object_tag_get_tagger(tag));
663 if (new_repo_tag->tagger == NULL) {
664 error = got_error_from_errno("strdup");
665 goto err;
667 new_repo_tag->tagger_time =
668 got_object_tag_get_tagger_time(tag);
669 error = got_object_id_str(&id_str,
670 got_object_tag_get_object_id(tag));
671 if (error)
672 goto err;
675 new_repo_tag->commit_id = strdup(id_str);
676 if (new_repo_tag->commit_id == NULL)
677 goto err;
679 if (commit_found == 0 && qs->commit != NULL &&
680 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
681 continue;
682 else
683 commit_found = 1;
685 t->tag_count++;
687 /*
688 * check for one more commit before breaking,
689 * so we know whether to navigate through briefs
690 * commits and summary
691 */
692 if (chk_next) {
693 t->next_id = strdup(new_repo_tag->commit_id);
694 if (t->next_id == NULL) {
695 error = got_error_from_errno("strdup");
696 goto err;
698 if (commit) {
699 got_object_commit_close(commit);
700 commit = NULL;
702 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
703 gotweb_free_repo_tag(new_repo_tag);
704 goto done;
707 if (commit) {
708 error = got_object_commit_get_logmsg(&tag_commit0,
709 commit);
710 if (error)
711 goto err;
712 got_object_commit_close(commit);
713 commit = NULL;
714 } else {
715 tag_commit0 = strdup(got_object_tag_get_message(tag));
716 if (tag_commit0 == NULL) {
717 error = got_error_from_errno("strdup");
718 goto err;
722 tag_commit = tag_commit0;
723 while (*tag_commit == '\n')
724 tag_commit++;
725 new_repo_tag->tag_commit = strdup(tag_commit);
726 if (new_repo_tag->tag_commit == NULL) {
727 error = got_error_from_errno("strdup");
728 free(tag_commit0);
729 goto err;
731 free(tag_commit0);
733 if (qs->action != SUMMARY && qs->action != TAGS) {
734 commit_msg = commit_msg0;
735 while (*commit_msg == '\n')
736 commit_msg++;
738 new_repo_tag->commit_msg = strdup(commit_msg);
739 if (new_repo_tag->commit_msg == NULL) {
740 error = got_error_from_errno("strdup");
741 goto err;
745 if (limit && --limit == 0) {
746 if (chk_multi == 0)
747 break;
748 chk_next = 1;
752 done:
753 /*
754 * we have tailq populated, so find previous commit id
755 * for navigation through briefs and commits
756 */
757 if (t->tag_count == 0) {
758 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
759 TAILQ_REMOVE(&t->repo_tags, rt, entry);
760 gotweb_free_repo_tag(rt);
763 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
764 commit_found = 0;
765 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
766 entry) {
767 if (commit_found == 0 && rt->commit_id != NULL &&
768 strcmp(qs->commit, rt->commit_id) != 0) {
769 continue;
770 } else
771 commit_found = 1;
772 if (c_cnt == srv->max_commits_display ||
773 rt == TAILQ_FIRST(&t->repo_tags)) {
774 t->prev_id = strdup(rt->commit_id);
775 if (t->prev_id == NULL)
776 error = got_error_from_errno("strdup");
777 break;
779 c_cnt++;
782 err:
783 if (commit)
784 got_object_commit_close(commit);
785 if (tag)
786 got_object_tag_close(tag);
787 got_ref_list_free(&refs);
788 free(commit_msg0);
789 free(in_repo_path);
790 free(repo_path);
791 free(id);
792 free(id_str);
793 return error;
796 const struct got_error *
797 got_output_repo_tree(struct request *c)
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 repo_dir *repo_dir = t->repo_dir;
809 const char *name, *folder;
810 char *escaped_name = NULL, *path = NULL;
811 int nentries, i, r;
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 folder = qs->folder ? qs->folder : "";
850 for (i = 0; i < nentries; i++) {
851 const char *modestr;
852 struct got_tree_entry *te;
853 mode_t mode;
855 te = got_object_tree_get_entry(tree, i);
857 mode = got_tree_entry_get_mode(te);
858 if (got_object_tree_entry_is_submodule(te))
859 modestr = "$";
860 else if (S_ISLNK(mode))
861 modestr = "@";
862 else if (S_ISDIR(mode))
863 modestr = "/";
864 else if (mode & S_IXUSR)
865 modestr = "*";
866 else
867 modestr = "";
869 name = got_tree_entry_get_name(te);
870 error = gotweb_escape_html(&escaped_name, name);
871 if (error)
872 goto done;
874 if (S_ISDIR(mode)) {
875 struct gotweb_url url = {
876 .index_page = -1,
877 .page = -1,
878 .action = TREE,
879 .commit = rc->commit_id,
880 .path = qs->path,
881 /* `folder' is filled later */
882 };
883 char *path = NULL;
885 if (fcgi_printf(c,"<div class='tree_wrapper'>\n"
886 "<div class='tree_line'>") == -1)
887 goto done;
889 if (asprintf(&path, "%s/%s", folder, name) == -1) {
890 error = got_error_from_errno("asprintf");
891 goto done;
893 url.folder = path;
894 r = gotweb_link(c, &url, "%s%s", escaped_name,
895 modestr);
896 free(path);
897 if (r == -1)
898 goto done;
900 if (fcgi_printf(c, "</div>\n" /* .tree_line */
901 "<div class='tree_line_blank'>&nbsp;</div>\n"
902 "</div>\n") == -1)
903 goto done;
904 } else {
905 struct gotweb_url url = {
906 .index_page = -1,
907 .page = -1,
908 .path = qs->path,
909 .commit = rc->commit_id,
910 .folder = folder,
911 .file = name,
912 };
914 if (fcgi_printf(c, "<div class='tree_wrapper'>\n"
915 "<div class='tree_line'>") == -1)
916 goto done;
918 url.action = BLOB;
919 r = gotweb_link(c, &url, "%s%s", escaped_name,
920 modestr);
921 if (r == -1)
922 goto done;
924 if (fcgi_printf(c, "</div>\n" /* .tree_line */
925 "<div class='tree_line_blank'>") == -1)
926 goto done;
928 url.action = COMMITS;
929 r = gotweb_link(c, &url, "commits");
930 if (r == -1)
931 goto done;
933 if (fcgi_printf(c, " | ") == -1)
934 goto done;
936 url.action = BLAME;
937 r = gotweb_link(c, &url, "blame");
938 if (r == -1)
939 goto done;
941 if (fcgi_printf(c,
942 "</div>\n" /* .tree_line_blank */
943 "</div>\n") == -1) /* .tree_wrapper */
944 goto done;
946 free(escaped_name);
947 escaped_name = NULL;
949 done:
950 free(escaped_name);
951 free(path);
952 got_ref_list_free(&refs);
953 if (commit)
954 got_object_commit_close(commit);
955 if (tree)
956 got_object_tree_close(tree);
957 free(commit_id);
958 free(tree_id);
959 return error;
962 const struct got_error *
963 got_output_file_blob(struct request *c)
965 const struct got_error *error = NULL;
966 struct transport *t = c->t;
967 struct got_repository *repo = t->repo;
968 struct querystring *qs = c->t->qs;
969 struct got_commit_object *commit = NULL;
970 struct got_object_id *commit_id = NULL;
971 struct got_reflist_head refs;
972 struct got_blob_object *blob = NULL;
973 char *path = NULL, *in_repo_path = NULL;
974 int bin, obj_type, fd = -1;
975 size_t len;
976 const uint8_t *buf;
978 TAILQ_INIT(&refs);
980 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
981 qs->folder ? "/" : "", qs->file) == -1) {
982 error = got_error_from_errno("asprintf");
983 goto done;
986 error = got_repo_map_path(&in_repo_path, repo, path);
987 if (error)
988 goto done;
990 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
991 GOT_OBJ_TYPE_COMMIT, &refs, repo);
992 if (error)
993 goto done;
995 error = got_object_open_as_commit(&commit, repo, commit_id);
996 if (error)
997 goto done;
999 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
1000 if (error)
1001 goto done;
1003 if (commit_id == NULL) {
1004 error = got_error(GOT_ERR_NO_OBJ);
1005 goto done;
1008 error = got_object_get_type(&obj_type, repo, commit_id);
1009 if (error)
1010 goto done;
1012 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1013 error = got_error(GOT_ERR_OBJ_TYPE);
1014 goto done;
1017 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
1018 if (error)
1019 goto done;
1021 error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
1022 if (error)
1023 goto done;
1025 error = got_object_blob_is_binary(&bin, blob);
1026 if (error)
1027 goto done;
1029 if (bin)
1030 error = gotweb_render_content_type_file(c,
1031 "application/octet-stream", qs->file, NULL);
1032 else
1033 error = gotweb_render_content_type(c, "text/plain");
1035 if (error) {
1036 log_warnx("%s: %s", __func__, error->msg);
1037 goto done;
1040 for (;;) {
1041 error = got_object_blob_read_block(&len, blob);
1042 if (error)
1043 goto done;
1044 if (len == 0)
1045 break;
1046 buf = got_object_blob_get_read_buf(blob);
1047 fcgi_gen_binary_response(c, buf, len);
1049 done:
1050 if (commit)
1051 got_object_commit_close(commit);
1052 if (fd != -1 && close(fd) == -1 && error == NULL)
1053 error = got_error_from_errno("close");
1054 if (blob)
1055 got_object_blob_close(blob);
1056 free(in_repo_path);
1057 free(commit_id);
1058 free(path);
1059 return error;
1062 struct blame_line {
1063 int annotated;
1064 char *id_str;
1065 char *committer;
1066 char datebuf[11]; /* YYYY-MM-DD + NUL */
1069 struct blame_cb_args {
1070 struct blame_line *lines;
1071 int nlines;
1072 int nlines_prec;
1073 int lineno_cur;
1074 off_t *line_offsets;
1075 FILE *f;
1076 struct got_repository *repo;
1077 struct request *c;
1080 static const struct got_error *
1081 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1082 struct got_commit_object *commit, struct got_object_id *id)
1084 const struct got_error *err = NULL;
1085 struct blame_cb_args *a = arg;
1086 struct blame_line *bline;
1087 struct request *c = a->c;
1088 struct transport *t = c->t;
1089 struct repo_dir *repo_dir = t->repo_dir;
1090 char *line = NULL, *eline = NULL;
1091 size_t linesize = 0;
1092 off_t offset;
1093 struct tm tm;
1094 time_t committer_time;
1095 int r;
1097 if (nlines != a->nlines ||
1098 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1099 return got_error(GOT_ERR_RANGE);
1101 if (lineno == -1)
1102 return NULL; /* no change in this commit */
1104 /* Annotate this line. */
1105 bline = &a->lines[lineno - 1];
1106 if (bline->annotated)
1107 return NULL;
1108 err = got_object_id_str(&bline->id_str, id);
1109 if (err)
1110 return err;
1112 bline->committer = strdup(got_object_commit_get_committer(commit));
1113 if (bline->committer == NULL) {
1114 err = got_error_from_errno("strdup");
1115 goto done;
1118 committer_time = got_object_commit_get_committer_time(commit);
1119 if (gmtime_r(&committer_time, &tm) == NULL)
1120 return got_error_from_errno("gmtime_r");
1121 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1122 &tm) == 0) {
1123 err = got_error(GOT_ERR_NO_SPACE);
1124 goto done;
1126 bline->annotated = 1;
1128 /* Print lines annotated so far. */
1129 bline = &a->lines[a->lineno_cur - 1];
1130 if (!bline->annotated)
1131 goto done;
1133 offset = a->line_offsets[a->lineno_cur - 1];
1134 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1135 err = got_error_from_errno("fseeko");
1136 goto done;
1139 while (a->lineno_cur <= a->nlines && bline->annotated) {
1140 char *smallerthan, *at, *nl, *committer;
1141 size_t len;
1143 if (getline(&line, &linesize, a->f) == -1) {
1144 if (ferror(a->f))
1145 err = got_error_from_errno("getline");
1146 break;
1149 committer = bline->committer;
1150 smallerthan = strchr(committer, '<');
1151 if (smallerthan && smallerthan[1] != '\0')
1152 committer = smallerthan + 1;
1153 at = strchr(committer, '@');
1154 if (at)
1155 *at = '\0';
1156 len = strlen(committer);
1157 if (len >= 9)
1158 committer[8] = '\0';
1160 nl = strchr(line, '\n');
1161 if (nl)
1162 *nl = '\0';
1164 err = gotweb_escape_html(&eline, line);
1165 if (err)
1166 goto done;
1168 if (fcgi_printf(c, "<div class='blame_wrapper'>"
1169 "<div class='blame_number'>%.*d</div>"
1170 "<div class='blame_hash'>",
1171 a->nlines_prec, a->lineno_cur) == -1)
1172 goto done;
1174 r = gotweb_link(c, &(struct gotweb_url){
1175 .action = DIFF,
1176 .index_page = -1,
1177 .page = -1,
1178 .path = repo_dir->name,
1179 .commit = bline->id_str,
1180 }, "%.8s", bline->id_str);
1181 if (r == -1)
1182 goto done;
1184 r = fcgi_printf(c,
1185 "</div>"
1186 "<div class='blame_date'>%s</div>"
1187 "<div class='blame_author'>%s</div>"
1188 "<div class='blame_code'>%s</div>"
1189 "</div>", /* .blame_wrapper */
1190 bline->datebuf,
1191 committer,
1192 eline);
1193 if (r == -1)
1194 goto done;
1196 a->lineno_cur++;
1197 bline = &a->lines[a->lineno_cur - 1];
1199 free(eline);
1200 eline = NULL;
1202 done:
1203 free(line);
1204 free(eline);
1205 return err;
1208 const struct got_error *
1209 got_output_file_blame(struct request *c)
1211 const struct got_error *error = NULL;
1212 struct transport *t = c->t;
1213 struct got_repository *repo = t->repo;
1214 struct querystring *qs = c->t->qs;
1215 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1216 struct got_commit_object *commit = NULL;
1217 struct got_reflist_head refs;
1218 struct got_blob_object *blob = NULL;
1219 char *path = NULL, *in_repo_path = NULL;
1220 struct blame_cb_args bca;
1221 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1222 int fd6 = -1;
1223 off_t filesize;
1224 FILE *f1 = NULL, *f2 = NULL;
1226 TAILQ_INIT(&refs);
1227 bca.f = NULL;
1228 bca.lines = NULL;
1230 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1231 qs->folder ? "/" : "", qs->file) == -1) {
1232 error = got_error_from_errno("asprintf");
1233 goto done;
1236 error = got_repo_map_path(&in_repo_path, repo, path);
1237 if (error)
1238 goto done;
1240 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1241 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1242 if (error)
1243 goto done;
1245 error = got_object_open_as_commit(&commit, repo, commit_id);
1246 if (error)
1247 goto done;
1249 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1250 if (error)
1251 goto done;
1253 error = got_object_get_type(&obj_type, repo, obj_id);
1254 if (error)
1255 goto done;
1257 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1258 error = got_error(GOT_ERR_OBJ_TYPE);
1259 goto done;
1262 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1263 if (error)
1264 goto done;
1266 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1267 if (error)
1268 goto done;
1270 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1271 if (error)
1272 goto done;
1274 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1275 &bca.line_offsets, bca.f, blob);
1276 if (error || bca.nlines == 0)
1277 goto done;
1279 /* Don't include \n at EOF in the blame line count. */
1280 if (bca.line_offsets[bca.nlines - 1] == filesize)
1281 bca.nlines--;
1283 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1284 if (bca.lines == NULL) {
1285 error = got_error_from_errno("calloc");
1286 goto done;
1288 bca.lineno_cur = 1;
1289 bca.nlines_prec = 0;
1290 i = bca.nlines;
1291 while (i > 0) {
1292 i /= 10;
1293 bca.nlines_prec++;
1295 bca.repo = repo;
1296 bca.c = c;
1298 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1299 if (error)
1300 goto done;
1302 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1303 if (error)
1304 goto done;
1306 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1307 if (error)
1308 goto done;
1310 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1311 if (error)
1312 goto done;
1314 error = got_blame(in_repo_path, commit_id, repo,
1315 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1316 fd3, fd4, f1, f2);
1318 done:
1319 if (bca.lines) {
1320 free(bca.line_offsets);
1321 for (i = 0; i < bca.nlines; i++) {
1322 struct blame_line *bline = &bca.lines[i];
1323 free(bline->id_str);
1324 free(bline->committer);
1327 free(bca.lines);
1328 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1329 error = got_error_from_errno("close");
1330 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1331 error = got_error_from_errno("close");
1332 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1333 error = got_error_from_errno("close");
1334 if (bca.f) {
1335 const struct got_error *bca_err =
1336 got_gotweb_flushfile(bca.f, fd1);
1337 if (error == NULL)
1338 error = bca_err;
1340 if (f1) {
1341 const struct got_error *f1_err =
1342 got_gotweb_flushfile(f1, fd5);
1343 if (error == NULL)
1344 error = f1_err;
1346 if (f2) {
1347 const struct got_error *f2_err =
1348 got_gotweb_flushfile(f2, fd6);
1349 if (error == NULL)
1350 error = f2_err;
1352 if (commit)
1353 got_object_commit_close(commit);
1354 if (blob)
1355 got_object_blob_close(blob);
1356 free(in_repo_path);
1357 free(commit_id);
1358 free(obj_id);
1359 free(path);
1360 got_ref_list_free(&refs);
1361 return error;
1364 const struct got_error *
1365 got_output_repo_diff(struct request *c)
1367 const struct got_error *error = NULL;
1368 struct transport *t = c->t;
1369 struct got_repository *repo = t->repo;
1370 struct repo_commit *rc = NULL;
1371 struct got_object_id *id1 = NULL, *id2 = NULL;
1372 struct got_reflist_head refs;
1373 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1374 char *label1 = NULL, *label2 = NULL, *line = NULL;
1375 char *newline, *eline = NULL, *color = NULL;
1376 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1377 size_t linesize = 0;
1378 ssize_t linelen;
1379 int wrlen = 0;
1381 TAILQ_INIT(&refs);
1383 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1384 if (error)
1385 return error;
1387 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1388 if (error)
1389 return error;
1391 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1392 if (error)
1393 return error;
1395 rc = TAILQ_FIRST(&t->repo_commits);
1397 if (rc->parent_id != NULL &&
1398 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1399 error = got_repo_match_object_id(&id1, &label1,
1400 rc->parent_id, GOT_OBJ_TYPE_ANY,
1401 &refs, repo);
1402 if (error)
1403 goto done;
1406 error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1407 GOT_OBJ_TYPE_ANY, &refs, repo);
1408 if (error)
1409 goto done;
1411 error = got_object_get_type(&obj_type, repo, id2);
1412 if (error)
1413 goto done;
1415 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1416 if (error)
1417 goto done;
1419 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1420 if (error)
1421 goto done;
1423 switch (obj_type) {
1424 case GOT_OBJ_TYPE_BLOB:
1425 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1426 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1427 repo, f3);
1428 break;
1429 case GOT_OBJ_TYPE_TREE:
1430 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1431 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1432 repo, f3);
1433 break;
1434 case GOT_OBJ_TYPE_COMMIT:
1435 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1436 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1437 repo, f3);
1438 break;
1439 default:
1440 error = got_error(GOT_ERR_OBJ_TYPE);
1442 if (error)
1443 goto done;
1445 if (fseek(f1, 0, SEEK_SET) == -1) {
1446 error = got_ferror(f1, GOT_ERR_IO);
1447 goto done;
1450 if (fseek(f2, 0, SEEK_SET) == -1) {
1451 error = got_ferror(f2, GOT_ERR_IO);
1452 goto done;
1455 if (fseek(f3, 0, SEEK_SET) == -1) {
1456 error = got_ferror(f3, GOT_ERR_IO);
1457 goto done;
1460 while ((linelen = getline(&line, &linesize, f3)) != -1) {
1461 if (strncmp(line, "-", 1) == 0) {
1462 color = strdup("diff_minus");
1463 if (color == NULL) {
1464 error = got_error_from_errno("strdup");
1465 goto done;
1467 } else if (strncmp(line, "+", 1) == 0) {
1468 color = strdup("diff_plus");
1469 if (color == NULL) {
1470 error = got_error_from_errno("strdup");
1471 goto done;
1473 } else if (strncmp(line, "@@", 2) == 0) {
1474 color = strdup("diff_chunk_header");
1475 if (color == NULL) {
1476 error = got_error_from_errno("strdup");
1477 goto done;
1479 } else if (strncmp(line, "@@", 2) == 0) {
1480 color = strdup("diff_chunk_header");
1481 if (color == NULL) {
1482 error = got_error_from_errno("strdup");
1483 goto done;
1485 } else if (strncmp(line, "commit +", 8) == 0) {
1486 color = strdup("diff_meta");
1487 if (color == NULL) {
1488 error = got_error_from_errno("strdup");
1489 goto done;
1491 } else if (strncmp(line, "commit -", 8) == 0) {
1492 color = strdup("diff_meta");
1493 if (color == NULL) {
1494 error = got_error_from_errno("strdup");
1495 goto done;
1497 } else if (strncmp(line, "blob +", 6) == 0) {
1498 color = strdup("diff_meta");
1499 if (color == NULL) {
1500 error = got_error_from_errno("strdup");
1501 goto done;
1503 } else if (strncmp(line, "blob -", 6) == 0) {
1504 color = strdup("diff_meta");
1505 if (color == NULL) {
1506 error = got_error_from_errno("strdup");
1507 goto done;
1509 } else if (strncmp(line, "file +", 6) == 0) {
1510 color = strdup("diff_meta");
1511 if (color == NULL) {
1512 error = got_error_from_errno("strdup");
1513 goto done;
1515 } else if (strncmp(line, "file -", 6) == 0) {
1516 color = strdup("diff_meta");
1517 if (color == NULL) {
1518 error = got_error_from_errno("strdup");
1519 goto done;
1521 } else if (strncmp(line, "from:", 5) == 0) {
1522 color = strdup("diff_author");
1523 if (color == NULL) {
1524 error = got_error_from_errno("strdup");
1525 goto done;
1527 } else if (strncmp(line, "via:", 4) == 0) {
1528 color = strdup("diff_author");
1529 if (color == NULL) {
1530 error = got_error_from_errno("strdup");
1531 goto done;
1533 } else if (strncmp(line, "date:", 5) == 0) {
1534 color = strdup("diff_date");
1535 if (color == NULL) {
1536 error = got_error_from_errno("strdup");
1537 goto done;
1541 newline = strchr(line, '\n');
1542 if (newline)
1543 *newline = '\0';
1545 error = gotweb_escape_html(&eline, line);
1546 if (error)
1547 goto done;
1549 fcgi_printf(c, "<div class='diff_line %s'>%s</div>\n",
1550 color ? color : "", eline);
1551 free(eline);
1552 eline = NULL;
1554 if (linelen > 0)
1555 wrlen = wrlen + linelen;
1556 free(color);
1557 color = NULL;
1559 if (linelen == -1 && ferror(f3))
1560 error = got_error_from_errno("getline");
1561 done:
1562 free(color);
1563 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1564 error = got_error_from_errno("close");
1565 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1566 error = got_error_from_errno("close");
1567 if (f1) {
1568 const struct got_error *f1_err =
1569 got_gotweb_flushfile(f1, fd1);
1570 if (error == NULL)
1571 error = f1_err;
1573 if (f2) {
1574 const struct got_error *f2_err =
1575 got_gotweb_flushfile(f2, fd2);
1576 if (error == NULL)
1577 error = f2_err;
1579 if (f3) {
1580 const struct got_error *f3_err =
1581 got_gotweb_flushfile(f3, fd3);
1582 if (error == NULL)
1583 error = f3_err;
1585 got_ref_list_free(&refs);
1586 free(line);
1587 free(eline);
1588 free(label1);
1589 free(label2);
1590 free(id1);
1591 free(id2);
1592 return error;
1595 static const struct got_error *
1596 got_init_repo_commit(struct repo_commit **rc)
1598 *rc = calloc(1, sizeof(**rc));
1599 if (*rc == NULL)
1600 return got_error_from_errno2("%s: calloc", __func__);
1602 (*rc)->path = NULL;
1603 (*rc)->refs_str = NULL;
1604 (*rc)->commit_id = NULL;
1605 (*rc)->committer = NULL;
1606 (*rc)->author = NULL;
1607 (*rc)->parent_id = NULL;
1608 (*rc)->tree_id = NULL;
1609 (*rc)->commit_msg = NULL;
1611 return NULL;
1614 static const struct got_error *
1615 got_init_repo_tag(struct repo_tag **rt)
1617 *rt = calloc(1, sizeof(**rt));
1618 if (*rt == NULL)
1619 return got_error_from_errno2("%s: calloc", __func__);
1621 (*rt)->commit_id = NULL;
1622 (*rt)->tag_name = NULL;
1623 (*rt)->tag_commit = NULL;
1624 (*rt)->commit_msg = NULL;
1625 (*rt)->tagger = NULL;
1627 return NULL;