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 int
56 isbinary(const uint8_t *buf, size_t n)
57 {
58 return memchr(buf, '\0', n) != NULL;
59 }
62 static const struct got_error *
63 got_gotweb_flushfile(FILE *f, int fd)
64 {
65 if (fseek(f, 0, SEEK_SET) == -1)
66 return got_error_from_errno("fseek");
68 if (ftruncate(fd, 0) == -1)
69 return got_error_from_errno("ftruncate");
71 if (fsync(fd) == -1)
72 return got_error_from_errno("fsync");
74 if (f && fclose(f) == EOF)
75 return got_error_from_errno("fclose");
77 if (fd != -1 && close(fd) != -1)
78 return got_error_from_errno("close");
80 return NULL;
81 }
83 static const struct got_error *
84 got_gotweb_openfile(FILE **f, int *priv_fd, int *fd)
85 {
86 *fd = dup(*priv_fd);
87 if (*fd == -1)
88 return got_error_from_errno("dup");
90 *f = fdopen(*fd, "w+");
91 if (*f == NULL) {
92 close(*fd);
93 return got_error(GOT_ERR_PRIVSEP_NO_FD);
94 }
96 return NULL;
97 }
99 static const struct got_error *
100 got_gotweb_dupfd(int *priv_fd, int *fd)
102 *fd = dup(*priv_fd);
104 if (*fd < 0)
105 return NULL;
107 return NULL;
110 const struct got_error *
111 got_get_repo_owner(char **owner, struct request *c, char *dir)
113 struct server *srv = c->srv;
114 struct transport *t = c->t;
115 struct got_repository *repo = t->repo;
116 const char *gitconfig_owner;
118 *owner = NULL;
120 if (srv->show_repo_owner == 0)
121 return NULL;
123 gitconfig_owner = got_repo_get_gitconfig_owner(repo);
124 if (gitconfig_owner) {
125 *owner = strdup(gitconfig_owner);
126 if (*owner == NULL)
127 return got_error_from_errno("strdup");
128 } else {
129 *owner = strdup("");
130 if (*owner == NULL)
131 return got_error_from_errno("strdup");
133 return NULL;
136 const struct got_error *
137 got_get_repo_age(char **repo_age, struct request *c, char *dir,
138 const char *refname, int ref_tm)
140 const struct got_error *error = NULL;
141 struct server *srv = c->srv;
142 struct transport *t = c->t;
143 struct got_repository *repo = t->repo;
144 struct got_commit_object *commit = NULL;
145 struct got_reflist_head refs;
146 struct got_reflist_entry *re;
147 time_t committer_time = 0, cmp_time = 0;
149 *repo_age = NULL;
150 TAILQ_INIT(&refs);
152 if (srv->show_repo_age == 0)
153 return NULL;
155 error = got_ref_list(&refs, repo, "refs/heads",
156 got_ref_cmp_by_name, NULL);
157 if (error)
158 goto done;
160 /*
161 * Find the youngest branch tip in the repository, or the age of
162 * the a specific branch tip if a name was provided by the caller.
163 */
164 TAILQ_FOREACH(re, &refs, entry) {
165 struct got_object_id *id = NULL;
167 if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
168 continue;
170 error = got_ref_resolve(&id, repo, re->ref);
171 if (error)
172 goto done;
174 error = got_object_open_as_commit(&commit, repo, id);
175 free(id);
176 if (error)
177 goto done;
179 committer_time =
180 got_object_commit_get_committer_time(commit);
181 got_object_commit_close(commit);
182 if (cmp_time < committer_time)
183 cmp_time = committer_time;
185 if (refname)
186 break;
189 if (cmp_time != 0) {
190 committer_time = cmp_time;
191 error = gotweb_get_time_str(repo_age, committer_time, ref_tm);
193 done:
194 got_ref_list_free(&refs);
195 return error;
198 static const struct got_error *
199 got_get_repo_commit(struct request *c, struct repo_commit *repo_commit,
200 struct got_commit_object *commit, struct got_reflist_head *refs,
201 struct got_object_id *id)
203 const struct got_error *error = NULL;
204 struct got_reflist_entry *re;
205 struct got_object_id *id2 = NULL;
206 struct got_object_qid *parent_id;
207 struct transport *t = c->t;
208 struct querystring *qs = c->t->qs;
209 char *commit_msg = NULL, *commit_msg0;
211 TAILQ_FOREACH(re, refs, entry) {
212 char *s;
213 const char *name;
214 struct got_tag_object *tag = NULL;
215 struct got_object_id *ref_id;
216 int cmp;
218 if (got_ref_is_symbolic(re->ref))
219 continue;
221 name = got_ref_get_name(re->ref);
222 if (strncmp(name, "refs/", 5) == 0)
223 name += 5;
224 if (strncmp(name, "got/", 4) == 0)
225 continue;
226 if (strncmp(name, "heads/", 6) == 0)
227 name += 6;
228 if (strncmp(name, "remotes/", 8) == 0) {
229 name += 8;
230 if (strstr(name, "/" GOT_REF_HEAD) != NULL)
231 continue;
233 error = got_ref_resolve(&ref_id, t->repo, re->ref);
234 if (error)
235 return error;
236 if (strncmp(name, "tags/", 5) == 0) {
237 error = got_object_open_as_tag(&tag, t->repo, ref_id);
238 if (error) {
239 if (error->code != GOT_ERR_OBJ_TYPE) {
240 free(ref_id);
241 continue;
243 /*
244 * Ref points at something other
245 * than a tag.
246 */
247 error = NULL;
248 tag = NULL;
251 cmp = got_object_id_cmp(tag ?
252 got_object_tag_get_object_id(tag) : ref_id, id);
253 free(ref_id);
254 if (tag)
255 got_object_tag_close(tag);
256 if (cmp != 0)
257 continue;
258 s = repo_commit->refs_str;
259 if (asprintf(&repo_commit->refs_str, "%s%s%s", s ? s : "",
260 s ? ", " : "", name) == -1) {
261 error = got_error_from_errno("asprintf");
262 free(s);
263 repo_commit->refs_str = NULL;
264 return error;
266 free(s);
269 error = got_object_id_str(&repo_commit->commit_id, id);
270 if (error)
271 return error;
273 error = got_object_id_str(&repo_commit->tree_id,
274 got_object_commit_get_tree_id(commit));
275 if (error)
276 return error;
278 if (qs->action == DIFF) {
279 parent_id = STAILQ_FIRST(
280 got_object_commit_get_parent_ids(commit));
281 if (parent_id != NULL) {
282 id2 = got_object_id_dup(&parent_id->id);
283 error = got_object_id_str(&repo_commit->parent_id, id2);
284 if (error)
285 return error;
286 free(id2);
287 } else {
288 repo_commit->parent_id = strdup("/dev/null");
289 if (repo_commit->parent_id == NULL) {
290 error = got_error_from_errno("strdup");
291 return error;
296 repo_commit->committer_time =
297 got_object_commit_get_committer_time(commit);
299 repo_commit->author =
300 strdup(got_object_commit_get_author(commit));
301 if (repo_commit->author == NULL) {
302 error = got_error_from_errno("strdup");
303 return error;
305 repo_commit->committer =
306 strdup(got_object_commit_get_committer(commit));
307 if (repo_commit->committer == NULL) {
308 error = got_error_from_errno("strdup");
309 return error;
311 error = got_object_commit_get_logmsg(&commit_msg0, commit);
312 if (error)
313 return error;
315 commit_msg = commit_msg0;
316 while (*commit_msg == '\n')
317 commit_msg++;
319 repo_commit->commit_msg = strdup(commit_msg);
320 if (repo_commit->commit_msg == NULL)
321 error = got_error_from_errno("strdup");
322 free(commit_msg0);
323 return error;
326 const struct got_error *
327 got_get_repo_commits(struct request *c, int limit)
329 const struct got_error *error = NULL;
330 struct got_object_id *id = NULL;
331 struct got_commit_graph *graph = NULL;
332 struct got_commit_object *commit = NULL;
333 struct got_reflist_head refs;
334 struct got_reference *ref = NULL;
335 struct repo_commit *repo_commit = NULL;
336 struct server *srv = c->srv;
337 struct transport *t = c->t;
338 struct got_repository *repo = t->repo;
339 struct querystring *qs = t->qs;
340 struct repo_dir *repo_dir = t->repo_dir;
341 char *in_repo_path = NULL, *repo_path = NULL, *file_path = NULL;
342 int chk_next = 0, chk_multi = 0, commit_found = 0;
343 int obj_type, limit_chk = 0;
345 TAILQ_INIT(&refs);
347 if (qs->file != NULL && strlen(qs->file) > 0)
348 if (asprintf(&file_path, "%s/%s", qs->folder ? qs->folder : "",
349 qs->file) == -1)
350 return got_error_from_errno("asprintf");
352 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
353 repo_dir->name) == -1) {
354 error = got_error_from_errno("asprintf");
355 goto done;
358 /*
359 * XXX: jumping directly to a commit id via
360 * got_repo_match_object_id_prefix significantly improves performance,
361 * but does not allow us to create a PREVIOUS button, since commits can
362 * only be itereated forward. So, we have to match as we iterate from
363 * the headref.
364 */
365 if (qs->action == BRIEFS || qs->action == COMMITS ||
366 (qs->action == TREE && qs->commit == NULL)) {
367 error = got_ref_open(&ref, repo, qs->headref, 0);
368 if (error)
369 goto done;
371 error = got_ref_resolve(&id, repo, ref);
372 if (error)
373 goto done;
374 } else if (qs->commit != NULL) {
375 error = got_ref_open(&ref, repo, qs->commit, 0);
376 if (error == NULL) {
377 error = got_ref_resolve(&id, repo, ref);
378 if (error)
379 goto done;
380 error = got_object_get_type(&obj_type, repo, id);
381 if (error)
382 goto done;
383 if (obj_type == GOT_OBJ_TYPE_TAG) {
384 struct got_tag_object *tag;
385 error = got_object_open_as_tag(&tag, repo, id);
386 if (error)
387 goto done;
388 if (got_object_tag_get_object_type(tag) !=
389 GOT_OBJ_TYPE_COMMIT) {
390 got_object_tag_close(tag);
391 error = got_error(GOT_ERR_OBJ_TYPE);
392 goto done;
394 free(id);
395 id = got_object_id_dup(
396 got_object_tag_get_object_id(tag));
397 if (id == NULL)
398 error = got_error_from_errno(
399 "got_object_id_dup");
400 got_object_tag_close(tag);
401 if (error)
402 goto done;
403 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
404 error = got_error(GOT_ERR_OBJ_TYPE);
405 goto done;
408 error = got_repo_match_object_id_prefix(&id, qs->commit,
409 GOT_OBJ_TYPE_COMMIT, repo);
410 if (error)
411 goto done;
414 error = got_repo_map_path(&in_repo_path, repo, repo_path);
415 if (error)
416 goto done;
418 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
419 if (error)
420 goto done;
422 if (qs->file != NULL && strlen(qs->file) > 0) {
423 error = got_commit_graph_open(&graph, file_path, 0);
424 if (error)
425 goto done;
426 } else {
427 error = got_commit_graph_open(&graph, in_repo_path, 0);
428 if (error)
429 goto done;
432 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
433 if (error)
434 goto done;
436 for (;;) {
437 struct got_object_id *next_id;
439 if (limit_chk == ((limit * qs->page) - (limit - 1)) &&
440 commit_found == 0 && repo_commit &&
441 repo_commit->commit_id != NULL) {
442 t->prev_id = strdup(repo_commit->commit_id);
443 if (t->prev_id == NULL) {
444 error = got_error_from_errno("strdup");
445 goto done;
449 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
450 NULL);
451 if (error) {
452 if (error->code == GOT_ERR_ITER_COMPLETED)
453 error = NULL;
454 goto done;
457 error = got_object_open_as_commit(&commit, repo, next_id);
458 if (error)
459 goto done;
461 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
462 NULL);
463 if (error)
464 goto done;
466 error = got_init_repo_commit(&repo_commit);
467 if (error)
468 goto done;
470 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
472 error = got_get_repo_commit(c, repo_commit, commit,
473 &refs, next_id);
474 if (error)
475 goto done;
477 if (qs->commit != NULL && commit_found == 0 && limit != 1) {
478 if (strcmp(qs->commit, repo_commit->commit_id) == 0)
479 commit_found = 1;
480 else if (qs->file != NULL && strlen(qs->file) > 0 &&
481 qs->page == 0)
482 commit_found = 1;
483 else {
484 limit_chk++;
485 continue;
489 if (limit == 1 && chk_multi == 0 &&
490 srv->max_commits_display != 1)
491 commit_found = 1;
492 else {
493 chk_multi = 1;
495 /*
496 * check for one more commit before breaking,
497 * so we know whether to navigate through briefs
498 * commits and summary
499 */
500 if (chk_next && (qs->action == BRIEFS ||
501 qs->action == COMMITS || qs->action == SUMMARY)) {
502 t->next_id = strdup(repo_commit->commit_id);
503 if (t->next_id == NULL) {
504 error = got_error_from_errno("strdup");
505 goto done;
507 if (commit) {
508 got_object_commit_close(commit);
509 commit = NULL;
511 TAILQ_REMOVE(&t->repo_commits, repo_commit,
512 entry);
513 gotweb_free_repo_commit(repo_commit);
514 goto done;
517 got_ref_list_free(&refs);
518 if (error || (limit && --limit == 0)) {
519 if (commit_found || (qs->file != NULL &&
520 strlen(qs->file) > 0))
521 if (chk_multi == 0)
522 break;
523 chk_next = 1;
525 if (commit) {
526 got_object_commit_close(commit);
527 commit = NULL;
530 done:
531 if (ref)
532 got_ref_close(ref);
533 if (commit)
534 got_object_commit_close(commit);
535 if (graph)
536 got_commit_graph_close(graph);
537 got_ref_list_free(&refs);
538 free(in_repo_path);
539 free(file_path);
540 free(repo_path);
541 free(id);
542 return error;
545 const struct got_error *
546 got_get_repo_tags(struct request *c, int limit)
548 const struct got_error *error = NULL;
549 struct got_object_id *id = NULL;
550 struct got_commit_object *commit = NULL;
551 struct got_reflist_head refs;
552 struct got_reference *ref;
553 struct got_reflist_entry *re;
554 struct server *srv = c->srv;
555 struct transport *t = c->t;
556 struct got_repository *repo = t->repo;
557 struct querystring *qs = t->qs;
558 struct repo_dir *repo_dir = t->repo_dir;
559 struct got_tag_object *tag = NULL;
560 struct repo_tag *rt = NULL, *trt = NULL;
561 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
562 char *tag_commit = NULL, *tag_commit0 = NULL;
563 char *commit_msg = NULL, *commit_msg0 = NULL;
564 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
566 TAILQ_INIT(&refs);
568 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
569 repo_dir->name) == -1)
570 return got_error_from_errno("asprintf");
572 if (qs->commit == NULL && qs->action == TAGS) {
573 error = got_ref_open(&ref, repo, qs->headref, 0);
574 if (error)
575 goto err;
576 error = got_ref_resolve(&id, repo, ref);
577 got_ref_close(ref);
578 if (error)
579 goto err;
580 } else if (qs->commit == NULL && qs->action == TAG) {
581 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
582 goto err;
583 } else {
584 error = got_repo_match_object_id_prefix(&id, qs->commit,
585 GOT_OBJ_TYPE_COMMIT, repo);
586 if (error)
587 goto err;
590 if (qs->action != SUMMARY && qs->action != TAGS) {
591 error = got_object_open_as_commit(&commit, repo, id);
592 if (error)
593 goto err;
594 error = got_object_commit_get_logmsg(&commit_msg0, commit);
595 if (error)
596 goto err;
597 if (commit) {
598 got_object_commit_close(commit);
599 commit = NULL;
603 error = got_repo_map_path(&in_repo_path, repo, repo_path);
604 if (error)
605 goto err;
607 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
608 repo);
609 if (error)
610 goto err;
612 if (limit == 1)
613 chk_multi = 0;
615 /*
616 * XXX: again, see previous message about caching
617 */
619 TAILQ_FOREACH(re, &refs, entry) {
620 struct repo_tag *new_repo_tag = NULL;
621 error = got_init_repo_tag(&new_repo_tag);
622 if (error)
623 goto err;
625 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
627 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
628 if (new_repo_tag->tag_name == NULL) {
629 error = got_error_from_errno("strdup");
630 goto err;
633 free(id);
634 id = NULL;
636 free(id_str);
637 id_str = NULL;
639 error = got_ref_resolve(&id, repo, re->ref);
640 if (error)
641 goto done;
643 if (tag)
644 got_object_tag_close(tag);
645 error = got_object_open_as_tag(&tag, repo, id);
646 if (error) {
647 if (error->code != GOT_ERR_OBJ_TYPE)
648 goto done;
649 /* "lightweight" tag */
650 error = got_object_open_as_commit(&commit, repo, id);
651 if (error)
652 goto done;
653 new_repo_tag->tagger =
654 strdup(got_object_commit_get_committer(commit));
655 if (new_repo_tag->tagger == NULL) {
656 error = got_error_from_errno("strdup");
657 goto err;
659 new_repo_tag->tagger_time =
660 got_object_commit_get_committer_time(commit);
661 error = got_object_id_str(&id_str, id);
662 if (error)
663 goto err;
664 } else {
665 new_repo_tag->tagger =
666 strdup(got_object_tag_get_tagger(tag));
667 if (new_repo_tag->tagger == NULL) {
668 error = got_error_from_errno("strdup");
669 goto err;
671 new_repo_tag->tagger_time =
672 got_object_tag_get_tagger_time(tag);
673 error = got_object_id_str(&id_str,
674 got_object_tag_get_object_id(tag));
675 if (error)
676 goto err;
679 new_repo_tag->commit_id = strdup(id_str);
680 if (new_repo_tag->commit_id == NULL)
681 goto err;
683 if (commit_found == 0 && qs->commit != NULL &&
684 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
685 continue;
686 else
687 commit_found = 1;
689 t->tag_count++;
691 /*
692 * check for one more commit before breaking,
693 * so we know whether to navigate through briefs
694 * commits and summary
695 */
696 if (chk_next) {
697 t->next_id = strdup(new_repo_tag->commit_id);
698 if (t->next_id == NULL) {
699 error = got_error_from_errno("strdup");
700 goto err;
702 if (commit) {
703 got_object_commit_close(commit);
704 commit = NULL;
706 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
707 gotweb_free_repo_tag(new_repo_tag);
708 goto done;
711 if (commit) {
712 error = got_object_commit_get_logmsg(&tag_commit0,
713 commit);
714 if (error)
715 goto err;
716 got_object_commit_close(commit);
717 commit = NULL;
718 } else {
719 tag_commit0 = strdup(got_object_tag_get_message(tag));
720 if (tag_commit0 == NULL) {
721 error = got_error_from_errno("strdup");
722 goto err;
726 tag_commit = tag_commit0;
727 while (*tag_commit == '\n')
728 tag_commit++;
729 new_repo_tag->tag_commit = strdup(tag_commit);
730 if (new_repo_tag->tag_commit == NULL) {
731 error = got_error_from_errno("strdup");
732 free(tag_commit0);
733 goto err;
735 free(tag_commit0);
737 if (qs->action != SUMMARY && qs->action != TAGS) {
738 commit_msg = commit_msg0;
739 while (*commit_msg == '\n')
740 commit_msg++;
742 new_repo_tag->commit_msg = strdup(commit_msg);
743 if (new_repo_tag->commit_msg == NULL) {
744 error = got_error_from_errno("strdup");
745 goto err;
749 if (limit && --limit == 0) {
750 if (chk_multi == 0)
751 break;
752 chk_next = 1;
756 done:
757 /*
758 * we have tailq populated, so find previous commit id
759 * for navigation through briefs and commits
760 */
761 if (t->tag_count == 0) {
762 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
763 TAILQ_REMOVE(&t->repo_tags, rt, entry);
764 gotweb_free_repo_tag(rt);
767 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
768 commit_found = 0;
769 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
770 entry) {
771 if (commit_found == 0 && rt->commit_id != NULL &&
772 strcmp(qs->commit, rt->commit_id) != 0) {
773 continue;
774 } else
775 commit_found = 1;
776 if (c_cnt == srv->max_commits_display ||
777 rt == TAILQ_FIRST(&t->repo_tags)) {
778 t->prev_id = strdup(rt->commit_id);
779 if (t->prev_id == NULL)
780 error = got_error_from_errno("strdup");
781 break;
783 c_cnt++;
786 err:
787 if (commit)
788 got_object_commit_close(commit);
789 if (tag)
790 got_object_tag_close(tag);
791 got_ref_list_free(&refs);
792 free(commit_msg0);
793 free(in_repo_path);
794 free(repo_path);
795 free(id);
796 free(id_str);
797 return error;
800 const struct got_error *
801 got_output_repo_tree(struct request *c)
803 const struct got_error *error = NULL;
804 struct transport *t = c->t;
805 struct got_commit_object *commit = NULL;
806 struct got_repository *repo = t->repo;
807 struct querystring *qs = t->qs;
808 struct repo_commit *rc = NULL;
809 struct got_object_id *tree_id = NULL, *commit_id = NULL;
810 struct got_reflist_head refs;
811 struct got_tree_object *tree = NULL;
812 struct repo_dir *repo_dir = t->repo_dir;
813 const char *name, *index_page_str, *folder;
814 char *escaped_name = NULL, *path = NULL;
815 int nentries, i, r;
817 TAILQ_INIT(&refs);
819 rc = TAILQ_FIRST(&t->repo_commits);
821 if (qs->folder != NULL) {
822 path = strdup(qs->folder);
823 if (path == NULL) {
824 error = got_error_from_errno("strdup");
825 goto done;
827 } else {
828 error = got_repo_map_path(&path, repo, repo_dir->path);
829 if (error)
830 goto done;
833 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
834 GOT_OBJ_TYPE_COMMIT, &refs, repo);
835 if (error)
836 goto done;
838 error = got_object_open_as_commit(&commit, repo, commit_id);
839 if (error)
840 goto done;
842 error = got_object_id_by_path(&tree_id, repo, commit, path);
843 if (error)
844 goto done;
846 error = got_object_open_as_tree(&tree, repo, tree_id);
847 if (error)
848 goto done;
850 nentries = got_object_tree_get_nentries(tree);
852 index_page_str = qs->index_page_str ? qs->index_page_str : "";
853 folder = qs->folder ? qs->folder : "";
855 for (i = 0; i < nentries; i++) {
856 const char *modestr;
857 struct got_tree_entry *te;
858 mode_t mode;
860 te = got_object_tree_get_entry(tree, i);
862 mode = got_tree_entry_get_mode(te);
863 if (got_object_tree_entry_is_submodule(te))
864 modestr = "$";
865 else if (S_ISLNK(mode))
866 modestr = "@";
867 else if (S_ISDIR(mode))
868 modestr = "/";
869 else if (mode & S_IXUSR)
870 modestr = "*";
871 else
872 modestr = "";
874 name = got_tree_entry_get_name(te);
875 error = gotweb_escape_html(&escaped_name, name);
876 if (error)
877 goto done;
879 if (S_ISDIR(mode)) {
880 r = fcgi_printf(c,
881 "<div class='tree_wrapper'>\n"
882 "<div class='tree_line'>"
883 "<a class='diff_directory' "
884 "href='?index_page=%s&path=%s&action=tree"
885 "&commit=%s&folder=%s/%s'>%s%s</a>"
886 "</div>\n" /* .tree_line */
887 "<div class='tree_line_blank'>&nbsp;</div>\n"
888 "</div>\n", /* .tree_wrapper */
889 index_page_str, qs->path, rc->commit_id,
890 folder, name, escaped_name, modestr);
891 if (r == -1)
892 goto done;
893 } else {
894 r = fcgi_printf(c,
895 "<div class='tree_wrapper'>\n"
896 "<div class='tree_line'>"
897 "<a href='?index_page=%s&path=%s&action=blob"
898 "&commit=%s&folder=%s&file=%s'>%s%s</a>"
899 "</div>\n" /* .tree_line */
900 "<div class='tree_line_blank'>"
901 "<a href='?index_page=%s&path=%s&action=commits"
902 "&commit=%s&folder=%s&file=%s'>commits</a>\n"
903 " | \n"
904 "<a href='?index_page=%s&path=%s&action=blame"
905 "&commit=%s&folder=%s&file=%s'>blame</a>\n"
906 "</div>\n" /* .tree_line_blank */
907 "</div>\n", /* .tree_wrapper */
908 index_page_str, qs->path, rc->commit_id,
909 folder, name, escaped_name, modestr,
910 index_page_str, qs->path, rc->commit_id,
911 folder, name,
912 index_page_str, qs->path, rc->commit_id,
913 folder, name);
914 if (r == -1)
915 goto done;
917 free(escaped_name);
918 escaped_name = NULL;
920 done:
921 free(escaped_name);
922 free(path);
923 got_ref_list_free(&refs);
924 if (commit)
925 got_object_commit_close(commit);
926 if (tree)
927 got_object_tree_close(tree);
928 free(commit_id);
929 free(tree_id);
930 return error;
933 const struct got_error *
934 got_output_file_blob(struct request *c)
936 const struct got_error *error = NULL;
937 struct transport *t = c->t;
938 struct got_repository *repo = t->repo;
939 struct querystring *qs = c->t->qs;
940 struct got_commit_object *commit = NULL;
941 struct got_object_id *commit_id = NULL;
942 struct got_reflist_head refs;
943 struct got_blob_object *blob = NULL;
944 char *path = NULL, *in_repo_path = NULL;
945 int obj_type, set_mime = 0, fd = -1;
946 size_t len, hdrlen;
947 const uint8_t *buf;
949 TAILQ_INIT(&refs);
951 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
952 qs->folder ? "/" : "", qs->file) == -1) {
953 error = got_error_from_errno("asprintf");
954 goto done;
957 error = got_repo_map_path(&in_repo_path, repo, path);
958 if (error)
959 goto done;
961 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
962 GOT_OBJ_TYPE_COMMIT, &refs, repo);
963 if (error)
964 goto done;
966 error = got_object_open_as_commit(&commit, repo, commit_id);
967 if (error)
968 goto done;
970 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
971 if (error)
972 goto done;
974 if (commit_id == NULL) {
975 error = got_error(GOT_ERR_NO_OBJ);
976 goto done;
979 error = got_object_get_type(&obj_type, repo, commit_id);
980 if (error)
981 goto done;
983 if (obj_type != GOT_OBJ_TYPE_BLOB) {
984 error = got_error(GOT_ERR_OBJ_TYPE);
985 goto done;
988 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
989 if (error)
990 goto done;
992 error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
993 if (error)
994 goto done;
995 hdrlen = got_object_blob_get_hdrlen(blob);
996 do {
997 error = got_object_blob_read_block(&len, blob);
998 if (error)
999 goto done;
1000 buf = got_object_blob_get_read_buf(blob);
1003 * Skip blob object header first time around,
1004 * which also contains a zero byte.
1006 buf += hdrlen;
1007 if (set_mime == 0) {
1008 if (isbinary(buf, len - hdrlen)) {
1009 error = gotweb_render_content_type_file(c,
1010 "application/octet-stream",
1011 qs->file);
1012 if (error) {
1013 log_warnx("%s: %s", __func__,
1014 error->msg);
1015 goto done;
1017 } else {
1018 error = gotweb_render_content_type(c,
1019 "text/plain");
1020 if (error) {
1021 log_warnx("%s: %s", __func__,
1022 error->msg);
1023 goto done;
1027 set_mime = 1;
1028 fcgi_gen_binary_response(c, buf, len - hdrlen);
1030 hdrlen = 0;
1031 } while (len != 0);
1032 done:
1033 if (commit)
1034 got_object_commit_close(commit);
1035 if (fd != -1 && close(fd) == -1 && error == NULL)
1036 error = got_error_from_errno("close");
1037 if (blob)
1038 got_object_blob_close(blob);
1039 free(in_repo_path);
1040 free(commit_id);
1041 free(path);
1042 return error;
1045 struct blame_line {
1046 int annotated;
1047 char *id_str;
1048 char *committer;
1049 char datebuf[11]; /* YYYY-MM-DD + NUL */
1052 struct blame_cb_args {
1053 struct blame_line *lines;
1054 int nlines;
1055 int nlines_prec;
1056 int lineno_cur;
1057 off_t *line_offsets;
1058 FILE *f;
1059 struct got_repository *repo;
1060 struct request *c;
1063 static const struct got_error *
1064 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1065 struct got_commit_object *commit, struct got_object_id *id)
1067 const struct got_error *err = NULL;
1068 struct blame_cb_args *a = arg;
1069 struct blame_line *bline;
1070 struct request *c = a->c;
1071 struct transport *t = c->t;
1072 struct querystring *qs = t->qs;
1073 struct repo_dir *repo_dir = t->repo_dir;
1074 const char *index_page_str;
1075 char *line = NULL, *eline = NULL;
1076 size_t linesize = 0;
1077 off_t offset;
1078 struct tm tm;
1079 time_t committer_time;
1080 int r;
1082 if (nlines != a->nlines ||
1083 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1084 return got_error(GOT_ERR_RANGE);
1086 if (lineno == -1)
1087 return NULL; /* no change in this commit */
1089 /* Annotate this line. */
1090 bline = &a->lines[lineno - 1];
1091 if (bline->annotated)
1092 return NULL;
1093 err = got_object_id_str(&bline->id_str, id);
1094 if (err)
1095 return err;
1097 bline->committer = strdup(got_object_commit_get_committer(commit));
1098 if (bline->committer == NULL) {
1099 err = got_error_from_errno("strdup");
1100 goto done;
1103 committer_time = got_object_commit_get_committer_time(commit);
1104 if (gmtime_r(&committer_time, &tm) == NULL)
1105 return got_error_from_errno("gmtime_r");
1106 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1107 &tm) == 0) {
1108 err = got_error(GOT_ERR_NO_SPACE);
1109 goto done;
1111 bline->annotated = 1;
1113 /* Print lines annotated so far. */
1114 bline = &a->lines[a->lineno_cur - 1];
1115 if (!bline->annotated)
1116 goto done;
1118 offset = a->line_offsets[a->lineno_cur - 1];
1119 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1120 err = got_error_from_errno("fseeko");
1121 goto done;
1124 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1126 while (a->lineno_cur <= a->nlines && bline->annotated) {
1127 char *smallerthan, *at, *nl, *committer;
1128 size_t len;
1130 if (getline(&line, &linesize, a->f) == -1) {
1131 if (ferror(a->f))
1132 err = got_error_from_errno("getline");
1133 break;
1136 committer = bline->committer;
1137 smallerthan = strchr(committer, '<');
1138 if (smallerthan && smallerthan[1] != '\0')
1139 committer = smallerthan + 1;
1140 at = strchr(committer, '@');
1141 if (at)
1142 *at = '\0';
1143 len = strlen(committer);
1144 if (len >= 9)
1145 committer[8] = '\0';
1147 nl = strchr(line, '\n');
1148 if (nl)
1149 *nl = '\0';
1151 err = gotweb_escape_html(&eline, line);
1152 if (err)
1153 goto done;
1155 r = fcgi_printf(c, "<div class='blame_wrapper'>"
1156 "<div class='blame_number'>%.*d</div>"
1157 "<div class='blame_hash'>"
1158 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1159 "%.8s</a>"
1160 "</div>"
1161 "<div class='blame_date'>%s</div>"
1162 "<div class='blame_author'>%s</div>"
1163 "<div class='blame_code'>%s</div>"
1164 "</div>", /* .blame_wrapper */
1165 a->nlines_prec, a->lineno_cur,
1166 index_page_str, repo_dir->name, bline->id_str,
1167 bline->id_str,
1168 bline->datebuf,
1169 committer,
1170 eline);
1171 if (r == -1)
1172 goto done;
1174 a->lineno_cur++;
1175 bline = &a->lines[a->lineno_cur - 1];
1177 free(eline);
1178 eline = NULL;
1180 done:
1181 free(line);
1182 free(eline);
1183 return err;
1186 const struct got_error *
1187 got_output_file_blame(struct request *c)
1189 const struct got_error *error = NULL;
1190 struct transport *t = c->t;
1191 struct got_repository *repo = t->repo;
1192 struct querystring *qs = c->t->qs;
1193 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1194 struct got_commit_object *commit = NULL;
1195 struct got_reflist_head refs;
1196 struct got_blob_object *blob = NULL;
1197 char *path = NULL, *in_repo_path = NULL;
1198 struct blame_cb_args bca;
1199 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1200 int fd6 = -1;
1201 off_t filesize;
1202 FILE *f1 = NULL, *f2 = NULL;
1204 TAILQ_INIT(&refs);
1205 bca.f = NULL;
1206 bca.lines = NULL;
1208 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1209 qs->folder ? "/" : "", qs->file) == -1) {
1210 error = got_error_from_errno("asprintf");
1211 goto done;
1214 error = got_repo_map_path(&in_repo_path, repo, path);
1215 if (error)
1216 goto done;
1218 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1219 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1220 if (error)
1221 goto done;
1223 error = got_object_open_as_commit(&commit, repo, commit_id);
1224 if (error)
1225 goto done;
1227 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1228 if (error)
1229 goto done;
1231 error = got_object_get_type(&obj_type, repo, obj_id);
1232 if (error)
1233 goto done;
1235 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1236 error = got_error(GOT_ERR_OBJ_TYPE);
1237 goto done;
1240 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1241 if (error)
1242 goto done;
1244 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1245 if (error)
1246 goto done;
1248 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1249 if (error)
1250 goto done;
1252 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1253 &bca.line_offsets, bca.f, blob);
1254 if (error || bca.nlines == 0)
1255 goto done;
1257 /* Don't include \n at EOF in the blame line count. */
1258 if (bca.line_offsets[bca.nlines - 1] == filesize)
1259 bca.nlines--;
1261 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1262 if (bca.lines == NULL) {
1263 error = got_error_from_errno("calloc");
1264 goto done;
1266 bca.lineno_cur = 1;
1267 bca.nlines_prec = 0;
1268 i = bca.nlines;
1269 while (i > 0) {
1270 i /= 10;
1271 bca.nlines_prec++;
1273 bca.repo = repo;
1274 bca.c = c;
1276 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1277 if (error)
1278 goto done;
1280 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1281 if (error)
1282 goto done;
1284 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1285 if (error)
1286 goto done;
1288 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1289 if (error)
1290 goto done;
1292 error = got_blame(in_repo_path, commit_id, repo,
1293 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1294 fd3, fd4, f1, f2);
1296 done:
1297 if (bca.lines) {
1298 free(bca.line_offsets);
1299 for (i = 0; i < bca.nlines; i++) {
1300 struct blame_line *bline = &bca.lines[i];
1301 free(bline->id_str);
1302 free(bline->committer);
1305 free(bca.lines);
1306 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1307 error = got_error_from_errno("close");
1308 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1309 error = got_error_from_errno("close");
1310 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1311 error = got_error_from_errno("close");
1312 if (bca.f) {
1313 const struct got_error *bca_err =
1314 got_gotweb_flushfile(bca.f, fd1);
1315 if (error == NULL)
1316 error = bca_err;
1318 if (f1) {
1319 const struct got_error *f1_err =
1320 got_gotweb_flushfile(f1, fd5);
1321 if (error == NULL)
1322 error = f1_err;
1324 if (f2) {
1325 const struct got_error *f2_err =
1326 got_gotweb_flushfile(f2, fd6);
1327 if (error == NULL)
1328 error = f2_err;
1330 if (commit)
1331 got_object_commit_close(commit);
1332 if (blob)
1333 got_object_blob_close(blob);
1334 free(in_repo_path);
1335 free(commit_id);
1336 free(obj_id);
1337 free(path);
1338 got_ref_list_free(&refs);
1339 return error;
1342 const struct got_error *
1343 got_output_repo_diff(struct request *c)
1345 const struct got_error *error = NULL;
1346 struct transport *t = c->t;
1347 struct got_repository *repo = t->repo;
1348 struct repo_commit *rc = NULL;
1349 struct got_object_id *id1 = NULL, *id2 = NULL;
1350 struct got_reflist_head refs;
1351 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1352 char *label1 = NULL, *label2 = NULL, *line = NULL;
1353 char *newline, *eline = NULL, *color = NULL;
1354 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1355 size_t linesize = 0;
1356 ssize_t linelen;
1357 int wrlen = 0;
1359 TAILQ_INIT(&refs);
1361 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1362 if (error)
1363 return error;
1365 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1366 if (error)
1367 return error;
1369 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1370 if (error)
1371 return error;
1373 rc = TAILQ_FIRST(&t->repo_commits);
1375 if (rc->parent_id != NULL &&
1376 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1377 error = got_repo_match_object_id(&id1, &label1,
1378 rc->parent_id, GOT_OBJ_TYPE_ANY,
1379 &refs, repo);
1380 if (error)
1381 goto done;
1384 error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1385 GOT_OBJ_TYPE_ANY, &refs, repo);
1386 if (error)
1387 goto done;
1389 error = got_object_get_type(&obj_type, repo, id2);
1390 if (error)
1391 goto done;
1393 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1394 if (error)
1395 goto done;
1397 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1398 if (error)
1399 goto done;
1401 switch (obj_type) {
1402 case GOT_OBJ_TYPE_BLOB:
1403 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1404 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1405 repo, f3);
1406 break;
1407 case GOT_OBJ_TYPE_TREE:
1408 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1409 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1410 repo, f3);
1411 break;
1412 case GOT_OBJ_TYPE_COMMIT:
1413 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1414 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1415 repo, f3);
1416 break;
1417 default:
1418 error = got_error(GOT_ERR_OBJ_TYPE);
1420 if (error)
1421 goto done;
1423 if (fseek(f1, 0, SEEK_SET) == -1) {
1424 error = got_ferror(f1, GOT_ERR_IO);
1425 goto done;
1428 if (fseek(f2, 0, SEEK_SET) == -1) {
1429 error = got_ferror(f2, GOT_ERR_IO);
1430 goto done;
1433 if (fseek(f3, 0, SEEK_SET) == -1) {
1434 error = got_ferror(f3, GOT_ERR_IO);
1435 goto done;
1438 while ((linelen = getline(&line, &linesize, f3)) != -1) {
1439 if (strncmp(line, "-", 1) == 0) {
1440 color = strdup("diff_minus");
1441 if (color == NULL) {
1442 error = got_error_from_errno("strdup");
1443 goto done;
1445 } else if (strncmp(line, "+", 1) == 0) {
1446 color = strdup("diff_plus");
1447 if (color == NULL) {
1448 error = got_error_from_errno("strdup");
1449 goto done;
1451 } else if (strncmp(line, "@@", 2) == 0) {
1452 color = strdup("diff_chunk_header");
1453 if (color == NULL) {
1454 error = got_error_from_errno("strdup");
1455 goto done;
1457 } else if (strncmp(line, "@@", 2) == 0) {
1458 color = strdup("diff_chunk_header");
1459 if (color == NULL) {
1460 error = got_error_from_errno("strdup");
1461 goto done;
1463 } else if (strncmp(line, "commit +", 8) == 0) {
1464 color = strdup("diff_meta");
1465 if (color == NULL) {
1466 error = got_error_from_errno("strdup");
1467 goto done;
1469 } else if (strncmp(line, "commit -", 8) == 0) {
1470 color = strdup("diff_meta");
1471 if (color == NULL) {
1472 error = got_error_from_errno("strdup");
1473 goto done;
1475 } else if (strncmp(line, "blob +", 6) == 0) {
1476 color = strdup("diff_meta");
1477 if (color == NULL) {
1478 error = got_error_from_errno("strdup");
1479 goto done;
1481 } else if (strncmp(line, "blob -", 6) == 0) {
1482 color = strdup("diff_meta");
1483 if (color == NULL) {
1484 error = got_error_from_errno("strdup");
1485 goto done;
1487 } else if (strncmp(line, "file +", 6) == 0) {
1488 color = strdup("diff_meta");
1489 if (color == NULL) {
1490 error = got_error_from_errno("strdup");
1491 goto done;
1493 } else if (strncmp(line, "file -", 6) == 0) {
1494 color = strdup("diff_meta");
1495 if (color == NULL) {
1496 error = got_error_from_errno("strdup");
1497 goto done;
1499 } else if (strncmp(line, "from:", 5) == 0) {
1500 color = strdup("diff_author");
1501 if (color == NULL) {
1502 error = got_error_from_errno("strdup");
1503 goto done;
1505 } else if (strncmp(line, "via:", 4) == 0) {
1506 color = strdup("diff_author");
1507 if (color == NULL) {
1508 error = got_error_from_errno("strdup");
1509 goto done;
1511 } else if (strncmp(line, "date:", 5) == 0) {
1512 color = strdup("diff_date");
1513 if (color == NULL) {
1514 error = got_error_from_errno("strdup");
1515 goto done;
1519 newline = strchr(line, '\n');
1520 if (newline)
1521 *newline = '\0';
1523 error = gotweb_escape_html(&eline, line);
1524 if (error)
1525 goto done;
1527 fcgi_printf(c, "<div class='diff_line %s'>%s</div>\n",
1528 color ? color : "", eline);
1529 free(eline);
1530 eline = NULL;
1532 if (linelen > 0)
1533 wrlen = wrlen + linelen;
1534 free(color);
1535 color = NULL;
1537 if (linelen == -1 && ferror(f3))
1538 error = got_error_from_errno("getline");
1539 done:
1540 free(color);
1541 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1542 error = got_error_from_errno("close");
1543 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1544 error = got_error_from_errno("close");
1545 if (f1) {
1546 const struct got_error *f1_err =
1547 got_gotweb_flushfile(f1, fd1);
1548 if (error == NULL)
1549 error = f1_err;
1551 if (f2) {
1552 const struct got_error *f2_err =
1553 got_gotweb_flushfile(f2, fd2);
1554 if (error == NULL)
1555 error = f2_err;
1557 if (f3) {
1558 const struct got_error *f3_err =
1559 got_gotweb_flushfile(f3, fd3);
1560 if (error == NULL)
1561 error = f3_err;
1563 got_ref_list_free(&refs);
1564 free(line);
1565 free(eline);
1566 free(label1);
1567 free(label2);
1568 free(id1);
1569 free(id2);
1570 return error;
1573 static const struct got_error *
1574 got_init_repo_commit(struct repo_commit **rc)
1576 *rc = calloc(1, sizeof(**rc));
1577 if (*rc == NULL)
1578 return got_error_from_errno2("%s: calloc", __func__);
1580 (*rc)->path = NULL;
1581 (*rc)->refs_str = NULL;
1582 (*rc)->commit_id = NULL;
1583 (*rc)->committer = NULL;
1584 (*rc)->author = NULL;
1585 (*rc)->parent_id = NULL;
1586 (*rc)->tree_id = NULL;
1587 (*rc)->commit_msg = NULL;
1589 return NULL;
1592 static const struct got_error *
1593 got_init_repo_tag(struct repo_tag **rt)
1595 *rt = calloc(1, sizeof(**rt));
1596 if (*rt == NULL)
1597 return got_error_from_errno2("%s: calloc", __func__);
1599 (*rt)->commit_id = NULL;
1600 (*rt)->tag_name = NULL;
1601 (*rt)->tag_commit = NULL;
1602 (*rt)->commit_msg = NULL;
1603 (*rt)->tagger = NULL;
1605 return NULL;