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 if (limit_chk == ((limit * qs->page) - (limit - 1)) &&
438 commit_found == 0 && repo_commit &&
439 repo_commit->commit_id != NULL) {
440 t->prev_id = strdup(repo_commit->commit_id);
441 if (t->prev_id == NULL) {
442 error = got_error_from_errno("strdup");
443 goto done;
447 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
448 NULL);
449 if (error) {
450 if (error->code == GOT_ERR_ITER_COMPLETED)
451 error = NULL;
452 goto done;
454 if (id == NULL)
455 goto done;
457 error = got_object_open_as_commit(&commit, repo, 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, 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 free(id);
486 id = NULL;
487 continue;
491 free(id);
492 id = NULL;
494 if (limit == 1 && chk_multi == 0 &&
495 srv->max_commits_display != 1)
496 commit_found = 1;
497 else {
498 chk_multi = 1;
500 /*
501 * check for one more commit before breaking,
502 * so we know whether to navigate through briefs
503 * commits and summary
504 */
505 if (chk_next && (qs->action == BRIEFS ||
506 qs->action == COMMITS || qs->action == SUMMARY)) {
507 t->next_id = strdup(repo_commit->commit_id);
508 if (t->next_id == NULL) {
509 error = got_error_from_errno("strdup");
510 goto done;
512 if (commit) {
513 got_object_commit_close(commit);
514 commit = NULL;
516 TAILQ_REMOVE(&t->repo_commits, repo_commit,
517 entry);
518 gotweb_free_repo_commit(repo_commit);
519 goto done;
522 got_ref_list_free(&refs);
523 if (error || (limit && --limit == 0)) {
524 if (commit_found || (qs->file != NULL &&
525 strlen(qs->file) > 0))
526 if (chk_multi == 0)
527 break;
528 chk_next = 1;
530 if (commit) {
531 got_object_commit_close(commit);
532 commit = NULL;
535 done:
536 if (ref)
537 got_ref_close(ref);
538 if (commit)
539 got_object_commit_close(commit);
540 if (graph)
541 got_commit_graph_close(graph);
542 got_ref_list_free(&refs);
543 free(in_repo_path);
544 free(file_path);
545 free(repo_path);
546 free(id);
547 return error;
550 const struct got_error *
551 got_get_repo_tags(struct request *c, int limit)
553 const struct got_error *error = NULL;
554 struct got_object_id *id = NULL;
555 struct got_commit_object *commit = NULL;
556 struct got_reflist_head refs;
557 struct got_reference *ref;
558 struct got_reflist_entry *re;
559 struct server *srv = c->srv;
560 struct transport *t = c->t;
561 struct got_repository *repo = t->repo;
562 struct querystring *qs = t->qs;
563 struct repo_dir *repo_dir = t->repo_dir;
564 struct got_tag_object *tag = NULL;
565 struct repo_tag *rt = NULL, *trt = NULL;
566 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
567 char *tag_commit = NULL, *tag_commit0 = NULL;
568 char *commit_msg = NULL, *commit_msg0 = NULL;
569 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
571 TAILQ_INIT(&refs);
573 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
574 repo_dir->name) == -1)
575 return got_error_from_errno("asprintf");
577 if (qs->commit == NULL && qs->action == TAGS) {
578 error = got_ref_open(&ref, repo, qs->headref, 0);
579 if (error)
580 goto err;
581 error = got_ref_resolve(&id, repo, ref);
582 got_ref_close(ref);
583 if (error)
584 goto err;
585 } else if (qs->commit == NULL && qs->action == TAG) {
586 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
587 goto err;
588 } else {
589 error = got_repo_match_object_id_prefix(&id, qs->commit,
590 GOT_OBJ_TYPE_COMMIT, repo);
591 if (error)
592 goto err;
595 if (qs->action != SUMMARY && qs->action != TAGS) {
596 error = got_object_open_as_commit(&commit, repo, id);
597 if (error)
598 goto err;
599 error = got_object_commit_get_logmsg(&commit_msg0, commit);
600 if (error)
601 goto err;
602 if (commit) {
603 got_object_commit_close(commit);
604 commit = NULL;
608 error = got_repo_map_path(&in_repo_path, repo, repo_path);
609 if (error)
610 goto err;
612 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
613 repo);
614 if (error)
615 goto err;
617 if (limit == 1)
618 chk_multi = 0;
620 /*
621 * XXX: again, see previous message about caching
622 */
624 TAILQ_FOREACH(re, &refs, entry) {
625 struct repo_tag *new_repo_tag = NULL;
626 error = got_init_repo_tag(&new_repo_tag);
627 if (error)
628 goto err;
630 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
632 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
633 if (new_repo_tag->tag_name == NULL) {
634 error = got_error_from_errno("strdup");
635 goto err;
638 free(id);
639 id = NULL;
641 free(id_str);
642 id_str = NULL;
644 error = got_ref_resolve(&id, repo, re->ref);
645 if (error)
646 goto done;
648 if (tag)
649 got_object_tag_close(tag);
650 error = got_object_open_as_tag(&tag, repo, id);
651 if (error) {
652 if (error->code != GOT_ERR_OBJ_TYPE)
653 goto done;
654 /* "lightweight" tag */
655 error = got_object_open_as_commit(&commit, repo, id);
656 if (error)
657 goto done;
658 new_repo_tag->tagger =
659 strdup(got_object_commit_get_committer(commit));
660 if (new_repo_tag->tagger == NULL) {
661 error = got_error_from_errno("strdup");
662 goto err;
664 new_repo_tag->tagger_time =
665 got_object_commit_get_committer_time(commit);
666 error = got_object_id_str(&id_str, id);
667 if (error)
668 goto err;
669 } else {
670 new_repo_tag->tagger =
671 strdup(got_object_tag_get_tagger(tag));
672 if (new_repo_tag->tagger == NULL) {
673 error = got_error_from_errno("strdup");
674 goto err;
676 new_repo_tag->tagger_time =
677 got_object_tag_get_tagger_time(tag);
678 error = got_object_id_str(&id_str,
679 got_object_tag_get_object_id(tag));
680 if (error)
681 goto err;
684 new_repo_tag->commit_id = strdup(id_str);
685 if (new_repo_tag->commit_id == NULL)
686 goto err;
688 if (commit_found == 0 && qs->commit != NULL &&
689 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
690 continue;
691 else
692 commit_found = 1;
694 t->tag_count++;
696 /*
697 * check for one more commit before breaking,
698 * so we know whether to navigate through briefs
699 * commits and summary
700 */
701 if (chk_next) {
702 t->next_id = strdup(new_repo_tag->commit_id);
703 if (t->next_id == NULL) {
704 error = got_error_from_errno("strdup");
705 goto err;
707 if (commit) {
708 got_object_commit_close(commit);
709 commit = NULL;
711 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
712 gotweb_free_repo_tag(new_repo_tag);
713 goto done;
716 if (commit) {
717 error = got_object_commit_get_logmsg(&tag_commit0,
718 commit);
719 if (error)
720 goto err;
721 got_object_commit_close(commit);
722 commit = NULL;
723 } else {
724 tag_commit0 = strdup(got_object_tag_get_message(tag));
725 if (tag_commit0 == NULL) {
726 error = got_error_from_errno("strdup");
727 goto err;
731 tag_commit = tag_commit0;
732 while (*tag_commit == '\n')
733 tag_commit++;
734 new_repo_tag->tag_commit = strdup(tag_commit);
735 if (new_repo_tag->tag_commit == NULL) {
736 error = got_error_from_errno("strdup");
737 free(tag_commit0);
738 goto err;
740 free(tag_commit0);
742 if (qs->action != SUMMARY && qs->action != TAGS) {
743 commit_msg = commit_msg0;
744 while (*commit_msg == '\n')
745 commit_msg++;
747 new_repo_tag->commit_msg = strdup(commit_msg);
748 if (new_repo_tag->commit_msg == NULL) {
749 error = got_error_from_errno("strdup");
750 goto err;
754 if (limit && --limit == 0) {
755 if (chk_multi == 0)
756 break;
757 chk_next = 1;
761 done:
762 /*
763 * we have tailq populated, so find previous commit id
764 * for navigation through briefs and commits
765 */
766 if (t->tag_count == 0) {
767 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
768 TAILQ_REMOVE(&t->repo_tags, rt, entry);
769 gotweb_free_repo_tag(rt);
772 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
773 commit_found = 0;
774 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
775 entry) {
776 if (commit_found == 0 && rt->commit_id != NULL &&
777 strcmp(qs->commit, rt->commit_id) != 0) {
778 continue;
779 } else
780 commit_found = 1;
781 if (c_cnt == srv->max_commits_display ||
782 rt == TAILQ_FIRST(&t->repo_tags)) {
783 t->prev_id = strdup(rt->commit_id);
784 if (t->prev_id == NULL)
785 error = got_error_from_errno("strdup");
786 break;
788 c_cnt++;
791 err:
792 if (commit)
793 got_object_commit_close(commit);
794 if (tag)
795 got_object_tag_close(tag);
796 got_ref_list_free(&refs);
797 free(commit_msg0);
798 free(in_repo_path);
799 free(repo_path);
800 free(id);
801 free(id_str);
802 return error;
805 const struct got_error *
806 got_output_repo_tree(struct request *c)
808 const struct got_error *error = NULL;
809 struct transport *t = c->t;
810 struct got_commit_object *commit = NULL;
811 struct got_repository *repo = t->repo;
812 struct querystring *qs = t->qs;
813 struct repo_commit *rc = NULL;
814 struct got_object_id *tree_id = NULL, *commit_id = NULL;
815 struct got_reflist_head refs;
816 struct got_tree_object *tree = NULL;
817 struct repo_dir *repo_dir = t->repo_dir;
818 const char *name, *index_page_str, *folder;
819 char *id_str = NULL, *escaped_name = NULL, *path = NULL;
820 int nentries, i, r;
822 TAILQ_INIT(&refs);
824 rc = TAILQ_FIRST(&t->repo_commits);
826 if (qs->folder != NULL) {
827 path = strdup(qs->folder);
828 if (path == NULL) {
829 error = got_error_from_errno("strdup");
830 goto done;
832 } else {
833 error = got_repo_map_path(&path, repo, repo_dir->path);
834 if (error)
835 goto done;
838 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
839 GOT_OBJ_TYPE_COMMIT, &refs, repo);
840 if (error)
841 goto done;
843 error = got_object_open_as_commit(&commit, repo, commit_id);
844 if (error)
845 goto done;
847 error = got_object_id_by_path(&tree_id, repo, commit, path);
848 if (error)
849 goto done;
851 error = got_object_open_as_tree(&tree, repo, tree_id);
852 if (error)
853 goto done;
855 nentries = got_object_tree_get_nentries(tree);
857 index_page_str = qs->index_page_str ? qs->index_page_str : "";
858 folder = qs->folder ? qs->folder : "";
860 for (i = 0; i < nentries; i++) {
861 const char *modestr;
862 struct got_tree_entry *te;
863 mode_t mode;
865 te = got_object_tree_get_entry(tree, i);
867 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
868 if (error)
869 goto done;
871 mode = got_tree_entry_get_mode(te);
872 if (got_object_tree_entry_is_submodule(te))
873 modestr = "$";
874 else if (S_ISLNK(mode))
875 modestr = "@";
876 else if (S_ISDIR(mode))
877 modestr = "/";
878 else if (mode & S_IXUSR)
879 modestr = "*";
880 else
881 modestr = "";
883 name = got_tree_entry_get_name(te);
884 error = gotweb_escape_html(&escaped_name, name);
885 if (error)
886 goto done;
888 if (S_ISDIR(mode)) {
889 r = fcgi_printf(c,
890 "<div class='tree_wrapper'>\n"
891 "<div class='tree_line'>"
892 "<a class='diff_directory' "
893 "href='?index_page=%s&path=%s&action=tree"
894 "&commit=%s&folder=%s/%s'>%s%s</a>"
895 "</div>\n" /* .tree_line */
896 "<div class='tree_line_blank'>&nbsp;</div>\n"
897 "</div>\n", /* .tree_wrapper */
898 index_page_str, qs->path, rc->commit_id,
899 folder, name, escaped_name, modestr);
900 if (r == -1)
901 goto done;
902 } else {
903 r = fcgi_printf(c,
904 "<div class='tree_wrapper'>\n"
905 "<div class='tree_line'>"
906 "<a href='?index_page=%s&path=%s&action=blob"
907 "&commit=%s&folder=%s&file=%s'>%s%s</a>"
908 "</div>\n" /* .tree_line */
909 "<div class='tree_line_blank'>"
910 "<a href='?index_page=%s&path=%s&action=commits"
911 "&commit=%s&folder=%s&file=%s'>commits</a>\n"
912 " | \n"
913 "<a href='?index_page=%s&path=%s&action=blame"
914 "&commit=%s&folder=%s&file=%s'>blame</a>\n"
915 "</div>\n" /* .tree_line_blank */
916 "</div>\n", /* .tree_wrapper */
917 index_page_str, qs->path, rc->commit_id,
918 folder, name, escaped_name, modestr,
919 index_page_str, qs->path, rc->commit_id,
920 folder, name,
921 index_page_str, qs->path, rc->commit_id,
922 folder, name);
923 if (r == -1)
924 goto done;
926 free(id_str);
927 id_str = NULL;
928 free(escaped_name);
929 escaped_name = NULL;
931 done:
932 free(escaped_name);
933 free(id_str);
934 free(path);
935 got_ref_list_free(&refs);
936 if (commit)
937 got_object_commit_close(commit);
938 free(commit_id);
939 free(tree_id);
940 return error;
943 const struct got_error *
944 got_output_file_blob(struct request *c)
946 const struct got_error *error = NULL;
947 struct transport *t = c->t;
948 struct got_repository *repo = t->repo;
949 struct querystring *qs = c->t->qs;
950 struct got_commit_object *commit = NULL;
951 struct got_object_id *commit_id = NULL;
952 struct got_reflist_head refs;
953 struct got_blob_object *blob = NULL;
954 char *path = NULL, *in_repo_path = NULL;
955 int obj_type, set_mime = 0, fd = -1;
956 size_t len, hdrlen;
957 const uint8_t *buf;
959 TAILQ_INIT(&refs);
961 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
962 qs->folder ? "/" : "", qs->file) == -1) {
963 error = got_error_from_errno("asprintf");
964 goto done;
967 error = got_repo_map_path(&in_repo_path, repo, path);
968 if (error)
969 goto done;
971 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
972 GOT_OBJ_TYPE_COMMIT, &refs, repo);
973 if (error)
974 goto done;
976 error = got_object_open_as_commit(&commit, repo, commit_id);
977 if (error)
978 goto done;
980 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
981 if (error)
982 goto done;
984 if (commit_id == NULL) {
985 error = got_error(GOT_ERR_NO_OBJ);
986 goto done;
989 error = got_object_get_type(&obj_type, repo, commit_id);
990 if (error)
991 goto done;
993 if (obj_type != GOT_OBJ_TYPE_BLOB) {
994 error = got_error(GOT_ERR_OBJ_TYPE);
995 goto done;
998 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
999 if (error)
1000 goto done;
1002 error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
1003 if (error)
1004 goto done;
1005 hdrlen = got_object_blob_get_hdrlen(blob);
1006 do {
1007 error = got_object_blob_read_block(&len, blob);
1008 if (error)
1009 goto done;
1010 buf = got_object_blob_get_read_buf(blob);
1013 * Skip blob object header first time around,
1014 * which also contains a zero byte.
1016 buf += hdrlen;
1017 if (set_mime == 0) {
1018 if (isbinary(buf, len - hdrlen)) {
1019 error = gotweb_render_content_type_file(c,
1020 "application/octet-stream",
1021 qs->file);
1022 if (error) {
1023 log_warnx("%s: %s", __func__,
1024 error->msg);
1025 goto done;
1027 } else {
1028 error = gotweb_render_content_type(c,
1029 "text/plain");
1030 if (error) {
1031 log_warnx("%s: %s", __func__,
1032 error->msg);
1033 goto done;
1037 set_mime = 1;
1038 fcgi_gen_binary_response(c, buf, len - hdrlen);
1040 hdrlen = 0;
1041 } while (len != 0);
1042 done:
1043 if (commit)
1044 got_object_commit_close(commit);
1045 if (fd != -1 && close(fd) == -1 && error == NULL)
1046 error = got_error_from_errno("close");
1047 if (blob)
1048 got_object_blob_close(blob);
1049 free(in_repo_path);
1050 free(commit_id);
1051 free(path);
1052 return error;
1055 struct blame_line {
1056 int annotated;
1057 char *id_str;
1058 char *committer;
1059 char datebuf[11]; /* YYYY-MM-DD + NUL */
1062 struct blame_cb_args {
1063 struct blame_line *lines;
1064 int nlines;
1065 int nlines_prec;
1066 int lineno_cur;
1067 off_t *line_offsets;
1068 FILE *f;
1069 struct got_repository *repo;
1070 struct request *c;
1073 static const struct got_error *
1074 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1075 struct got_commit_object *commit, struct got_object_id *id)
1077 const struct got_error *err = NULL;
1078 struct blame_cb_args *a = arg;
1079 struct blame_line *bline;
1080 struct request *c = a->c;
1081 struct transport *t = c->t;
1082 struct querystring *qs = t->qs;
1083 struct repo_dir *repo_dir = t->repo_dir;
1084 const char *index_page_str;
1085 char *line = NULL, *eline = NULL;
1086 size_t linesize = 0;
1087 off_t offset;
1088 struct tm tm;
1089 time_t committer_time;
1090 int r;
1092 if (nlines != a->nlines ||
1093 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1094 return got_error(GOT_ERR_RANGE);
1096 if (lineno == -1)
1097 return NULL; /* no change in this commit */
1099 /* Annotate this line. */
1100 bline = &a->lines[lineno - 1];
1101 if (bline->annotated)
1102 return NULL;
1103 err = got_object_id_str(&bline->id_str, id);
1104 if (err)
1105 return err;
1107 bline->committer = strdup(got_object_commit_get_committer(commit));
1108 if (bline->committer == NULL) {
1109 err = got_error_from_errno("strdup");
1110 goto done;
1113 committer_time = got_object_commit_get_committer_time(commit);
1114 if (gmtime_r(&committer_time, &tm) == NULL)
1115 return got_error_from_errno("gmtime_r");
1116 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1117 &tm) == 0) {
1118 err = got_error(GOT_ERR_NO_SPACE);
1119 goto done;
1121 bline->annotated = 1;
1123 /* Print lines annotated so far. */
1124 bline = &a->lines[a->lineno_cur - 1];
1125 if (!bline->annotated)
1126 goto done;
1128 offset = a->line_offsets[a->lineno_cur - 1];
1129 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1130 err = got_error_from_errno("fseeko");
1131 goto done;
1134 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1136 while (a->lineno_cur <= a->nlines && bline->annotated) {
1137 char *smallerthan, *at, *nl, *committer;
1138 size_t len;
1140 if (getline(&line, &linesize, a->f) == -1) {
1141 if (ferror(a->f))
1142 err = got_error_from_errno("getline");
1143 break;
1146 committer = bline->committer;
1147 smallerthan = strchr(committer, '<');
1148 if (smallerthan && smallerthan[1] != '\0')
1149 committer = smallerthan + 1;
1150 at = strchr(committer, '@');
1151 if (at)
1152 *at = '\0';
1153 len = strlen(committer);
1154 if (len >= 9)
1155 committer[8] = '\0';
1157 nl = strchr(line, '\n');
1158 if (nl)
1159 *nl = '\0';
1161 err = gotweb_escape_html(&eline, line);
1162 if (err)
1163 goto done;
1165 r = fcgi_printf(c, "<div class='blame_wrapper'>"
1166 "<div class='blame_number'>%.*d</div>"
1167 "<div class='blame_hash'>"
1168 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1169 "%.8s</a>"
1170 "</div>"
1171 "<div class='blame_date'>%s</div>"
1172 "<div class='blame_author'>%s</div>"
1173 "<div class='blame_code'>%s</div>"
1174 "</div>", /* .blame_wrapper */
1175 a->nlines_prec, a->lineno_cur,
1176 index_page_str, repo_dir->name, bline->id_str,
1177 bline->id_str,
1178 bline->datebuf,
1179 committer,
1180 eline);
1181 if (r == -1)
1182 goto done;
1184 a->lineno_cur++;
1185 bline = &a->lines[a->lineno_cur - 1];
1187 done:
1188 free(line);
1189 free(eline);
1190 return err;
1193 const struct got_error *
1194 got_output_file_blame(struct request *c)
1196 const struct got_error *error = NULL;
1197 struct transport *t = c->t;
1198 struct got_repository *repo = t->repo;
1199 struct querystring *qs = c->t->qs;
1200 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1201 struct got_commit_object *commit = NULL;
1202 struct got_reflist_head refs;
1203 struct got_blob_object *blob = NULL;
1204 char *path = NULL, *in_repo_path = NULL;
1205 struct blame_cb_args bca;
1206 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1207 int fd6 = -1;
1208 off_t filesize;
1209 FILE *f1 = NULL, *f2 = NULL;
1211 TAILQ_INIT(&refs);
1212 bca.f = NULL;
1213 bca.lines = NULL;
1215 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1216 qs->folder ? "/" : "", qs->file) == -1) {
1217 error = got_error_from_errno("asprintf");
1218 goto done;
1221 error = got_repo_map_path(&in_repo_path, repo, path);
1222 if (error)
1223 goto done;
1225 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1226 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1227 if (error)
1228 goto done;
1230 error = got_object_open_as_commit(&commit, repo, commit_id);
1231 if (error)
1232 goto done;
1234 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1235 if (error)
1236 goto done;
1238 if (commit_id == NULL) {
1239 error = got_error(GOT_ERR_NO_OBJ);
1240 goto done;
1243 error = got_object_get_type(&obj_type, repo, obj_id);
1244 if (error)
1245 goto done;
1247 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1248 error = got_error(GOT_ERR_OBJ_TYPE);
1249 goto done;
1252 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1253 if (error)
1254 goto done;
1256 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1257 if (error)
1258 goto done;
1260 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1261 if (error)
1262 goto done;
1264 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1265 &bca.line_offsets, bca.f, blob);
1266 if (error || bca.nlines == 0)
1267 goto done;
1269 /* Don't include \n at EOF in the blame line count. */
1270 if (bca.line_offsets[bca.nlines - 1] == filesize)
1271 bca.nlines--;
1273 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1274 if (bca.lines == NULL) {
1275 error = got_error_from_errno("calloc");
1276 goto done;
1278 bca.lineno_cur = 1;
1279 bca.nlines_prec = 0;
1280 i = bca.nlines;
1281 while (i > 0) {
1282 i /= 10;
1283 bca.nlines_prec++;
1285 bca.repo = repo;
1286 bca.c = c;
1288 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1289 if (error)
1290 goto done;
1292 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1293 if (error)
1294 goto done;
1296 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1297 if (error)
1298 goto done;
1300 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1301 if (error)
1302 goto done;
1304 error = got_blame(in_repo_path, commit_id, repo,
1305 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1306 fd3, fd4, f1, f2);
1308 if (blob) {
1309 free(bca.line_offsets);
1310 for (i = 0; i < bca.nlines; i++) {
1311 struct blame_line *bline = &bca.lines[i];
1312 free(bline->id_str);
1313 free(bline->committer);
1316 done:
1317 free(bca.lines);
1318 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1319 error = got_error_from_errno("close");
1320 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1321 error = got_error_from_errno("close");
1322 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1323 error = got_error_from_errno("close");
1324 if (bca.f) {
1325 const struct got_error *bca_err =
1326 got_gotweb_flushfile(bca.f, fd1);
1327 if (error == NULL)
1328 error = bca_err;
1330 if (f1) {
1331 const struct got_error *f1_err =
1332 got_gotweb_flushfile(f1, fd5);
1333 if (error == NULL)
1334 error = f1_err;
1336 if (f2) {
1337 const struct got_error *f2_err =
1338 got_gotweb_flushfile(f2, fd6);
1339 if (error == NULL)
1340 error = f2_err;
1342 if (commit)
1343 got_object_commit_close(commit);
1344 if (blob)
1345 got_object_blob_close(blob);
1346 free(in_repo_path);
1347 free(commit_id);
1348 free(path);
1349 return error;
1352 const struct got_error *
1353 got_output_repo_diff(struct request *c)
1355 const struct got_error *error = NULL;
1356 struct transport *t = c->t;
1357 struct got_repository *repo = t->repo;
1358 struct repo_commit *rc = NULL;
1359 struct got_object_id *id1 = NULL, *id2 = NULL;
1360 struct got_reflist_head refs;
1361 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1362 char *label1 = NULL, *label2 = NULL, *line = NULL;
1363 char *newline, *eline = NULL, *color = NULL;
1364 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1365 size_t linesize = 0;
1366 ssize_t linelen;
1367 int wrlen = 0;
1369 TAILQ_INIT(&refs);
1371 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1372 if (error)
1373 return error;
1375 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1376 if (error)
1377 return error;
1379 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1380 if (error)
1381 return error;
1383 rc = TAILQ_FIRST(&t->repo_commits);
1385 if (rc->parent_id != NULL &&
1386 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1387 error = got_repo_match_object_id(&id1, &label1,
1388 rc->parent_id, GOT_OBJ_TYPE_ANY,
1389 &refs, repo);
1390 if (error)
1391 goto done;
1394 error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1395 GOT_OBJ_TYPE_ANY, &refs, repo);
1396 if (error)
1397 goto done;
1399 error = got_object_get_type(&obj_type, repo, id2);
1400 if (error)
1401 goto done;
1403 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1404 if (error)
1405 goto done;
1407 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1408 if (error)
1409 goto done;
1411 switch (obj_type) {
1412 case GOT_OBJ_TYPE_BLOB:
1413 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1414 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1415 repo, f3);
1416 break;
1417 case GOT_OBJ_TYPE_TREE:
1418 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1419 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1420 repo, f3);
1421 break;
1422 case GOT_OBJ_TYPE_COMMIT:
1423 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1424 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1425 repo, f3);
1426 break;
1427 default:
1428 error = got_error(GOT_ERR_OBJ_TYPE);
1430 if (error)
1431 goto done;
1433 if (fseek(f1, 0, SEEK_SET) == -1) {
1434 error = got_ferror(f1, GOT_ERR_IO);
1435 goto done;
1438 if (fseek(f2, 0, SEEK_SET) == -1) {
1439 error = got_ferror(f2, GOT_ERR_IO);
1440 goto done;
1443 if (fseek(f3, 0, SEEK_SET) == -1) {
1444 error = got_ferror(f3, GOT_ERR_IO);
1445 goto done;
1448 while ((linelen = getline(&line, &linesize, f3)) != -1) {
1449 if (strncmp(line, "-", 1) == 0) {
1450 color = strdup("diff_minus");
1451 if (color == NULL) {
1452 error = got_error_from_errno("strdup");
1453 goto done;
1455 } else if (strncmp(line, "+", 1) == 0) {
1456 color = strdup("diff_plus");
1457 if (color == NULL) {
1458 error = got_error_from_errno("strdup");
1459 goto done;
1461 } else if (strncmp(line, "@@", 2) == 0) {
1462 color = strdup("diff_chunk_header");
1463 if (color == NULL) {
1464 error = got_error_from_errno("strdup");
1465 goto done;
1467 } else if (strncmp(line, "@@", 2) == 0) {
1468 color = strdup("diff_chunk_header");
1469 if (color == NULL) {
1470 error = got_error_from_errno("strdup");
1471 goto done;
1473 } else if (strncmp(line, "commit +", 8) == 0) {
1474 color = strdup("diff_meta");
1475 if (color == NULL) {
1476 error = got_error_from_errno("strdup");
1477 goto done;
1479 } else if (strncmp(line, "commit -", 8) == 0) {
1480 color = strdup("diff_meta");
1481 if (color == NULL) {
1482 error = got_error_from_errno("strdup");
1483 goto done;
1485 } else if (strncmp(line, "blob +", 6) == 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, "blob -", 6) == 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, "file +", 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, "file -", 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, "from:", 5) == 0) {
1510 color = strdup("diff_author");
1511 if (color == NULL) {
1512 error = got_error_from_errno("strdup");
1513 goto done;
1515 } else if (strncmp(line, "via:", 4) == 0) {
1516 color = strdup("diff_author");
1517 if (color == NULL) {
1518 error = got_error_from_errno("strdup");
1519 goto done;
1521 } else if (strncmp(line, "date:", 5) == 0) {
1522 color = strdup("diff_date");
1523 if (color == NULL) {
1524 error = got_error_from_errno("strdup");
1525 goto done;
1529 newline = strchr(line, '\n');
1530 if (newline)
1531 *newline = '\0';
1533 error = gotweb_escape_html(&eline, line);
1534 if (error)
1535 goto done;
1537 fcgi_printf(c, "<div class='diff_line %s'>%s</div>\n",
1538 color ? color : "", eline);
1539 free(eline);
1540 eline = NULL;
1542 if (linelen > 0)
1543 wrlen = wrlen + linelen;
1544 free(color);
1545 color = NULL;
1547 if (linelen == -1 && ferror(f3))
1548 error = got_error_from_errno("getline");
1549 done:
1550 free(color);
1551 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1552 error = got_error_from_errno("close");
1553 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1554 error = got_error_from_errno("close");
1555 if (f1) {
1556 const struct got_error *f1_err =
1557 got_gotweb_flushfile(f1, fd1);
1558 if (error == NULL)
1559 error = f1_err;
1561 if (f2) {
1562 const struct got_error *f2_err =
1563 got_gotweb_flushfile(f2, fd2);
1564 if (error == NULL)
1565 error = f2_err;
1567 if (f3) {
1568 const struct got_error *f3_err =
1569 got_gotweb_flushfile(f3, fd3);
1570 if (error == NULL)
1571 error = f3_err;
1573 got_ref_list_free(&refs);
1574 free(line);
1575 free(eline);
1576 free(label1);
1577 free(label2);
1578 free(id1);
1579 free(id2);
1580 return error;
1583 static const struct got_error *
1584 got_init_repo_commit(struct repo_commit **rc)
1586 *rc = calloc(1, sizeof(**rc));
1587 if (*rc == NULL)
1588 return got_error_from_errno2("%s: calloc", __func__);
1590 (*rc)->path = NULL;
1591 (*rc)->refs_str = NULL;
1592 (*rc)->commit_id = NULL;
1593 (*rc)->committer = NULL;
1594 (*rc)->author = NULL;
1595 (*rc)->parent_id = NULL;
1596 (*rc)->tree_id = NULL;
1597 (*rc)->commit_msg = NULL;
1599 return NULL;
1602 static const struct got_error *
1603 got_init_repo_tag(struct repo_tag **rt)
1605 *rt = calloc(1, sizeof(**rt));
1606 if (*rt == NULL)
1607 return got_error_from_errno2("%s: calloc", __func__);
1609 (*rt)->commit_id = NULL;
1610 (*rt)->tag_name = NULL;
1611 (*rt)->tag_commit = NULL;
1612 (*rt)->commit_msg = NULL;
1613 (*rt)->tagger = NULL;
1615 return NULL;