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)
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,
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 error = got_commit_graph_iter_next(&next_id, graph, repo, NULL,
440 NULL);
441 if (error) {
442 if (error->code == GOT_ERR_ITER_COMPLETED)
443 error = NULL;
444 goto done;
447 error = got_object_open_as_commit(&commit, repo, &next_id);
448 if (error)
449 goto done;
451 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
452 NULL);
453 if (error)
454 goto done;
456 error = got_init_repo_commit(&repo_commit);
457 if (error)
458 goto done;
460 error = got_get_repo_commit(c, repo_commit, commit,
461 &refs, &next_id);
462 if (error) {
463 gotweb_free_repo_commit(repo_commit);
464 goto done;
467 if (limit_chk == ((limit * qs->page) - limit) &&
468 commit_found == 0 && repo_commit->commit_id != NULL) {
469 t->prev_id = strdup(repo_commit->commit_id);
470 if (t->prev_id == NULL) {
471 error = got_error_from_errno("strdup");
472 gotweb_free_repo_commit(repo_commit);
473 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 gotweb_free_repo_commit(repo_commit);
485 limit_chk++;
486 continue;
490 TAILQ_INSERT_TAIL(&t->repo_commits, repo_commit, entry);
492 if (limit == 1 && chk_multi == 0 &&
493 srv->max_commits_display != 1)
494 commit_found = 1;
495 else {
496 chk_multi = 1;
498 /*
499 * check for one more commit before breaking,
500 * so we know whether to navigate through briefs
501 * commits and summary
502 */
503 if (chk_next && (qs->action == BRIEFS ||
504 qs->action == COMMITS || qs->action == SUMMARY)) {
505 t->next_id = strdup(repo_commit->commit_id);
506 if (t->next_id == NULL) {
507 error = got_error_from_errno("strdup");
508 goto done;
510 if (commit) {
511 got_object_commit_close(commit);
512 commit = NULL;
514 TAILQ_REMOVE(&t->repo_commits, repo_commit,
515 entry);
516 gotweb_free_repo_commit(repo_commit);
517 goto done;
520 got_ref_list_free(&refs);
521 if (error || (limit && --limit == 0)) {
522 if (commit_found || (qs->file != NULL &&
523 strlen(qs->file) > 0))
524 if (chk_multi == 0)
525 break;
526 chk_next = 1;
528 if (commit) {
529 got_object_commit_close(commit);
530 commit = NULL;
533 done:
534 if (ref)
535 got_ref_close(ref);
536 if (commit)
537 got_object_commit_close(commit);
538 if (graph)
539 got_commit_graph_close(graph);
540 got_ref_list_free(&refs);
541 free(in_repo_path);
542 free(file_path);
543 free(repo_path);
544 free(id);
545 return error;
548 const struct got_error *
549 got_get_repo_tags(struct request *c, int limit)
551 const struct got_error *error = NULL;
552 struct got_object_id *id = NULL;
553 struct got_commit_object *commit = NULL;
554 struct got_reflist_head refs;
555 struct got_reference *ref;
556 struct got_reflist_entry *re;
557 struct server *srv = c->srv;
558 struct transport *t = c->t;
559 struct got_repository *repo = t->repo;
560 struct querystring *qs = t->qs;
561 struct repo_dir *repo_dir = t->repo_dir;
562 struct got_tag_object *tag = NULL;
563 struct repo_tag *rt = NULL, *trt = NULL;
564 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
565 char *tag_commit = NULL, *tag_commit0 = NULL;
566 char *commit_msg = NULL, *commit_msg0 = NULL;
567 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
569 TAILQ_INIT(&refs);
571 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
572 repo_dir->name) == -1)
573 return got_error_from_errno("asprintf");
575 if (qs->commit == NULL && qs->action == TAGS) {
576 error = got_ref_open(&ref, repo, qs->headref, 0);
577 if (error)
578 goto err;
579 error = got_ref_resolve(&id, repo, ref);
580 got_ref_close(ref);
581 if (error)
582 goto err;
583 } else if (qs->commit == NULL && qs->action == TAG) {
584 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
585 goto err;
586 } else {
587 error = got_repo_match_object_id_prefix(&id, qs->commit,
588 GOT_OBJ_TYPE_COMMIT, repo);
589 if (error)
590 goto err;
593 if (qs->action != SUMMARY && qs->action != TAGS) {
594 error = got_object_open_as_commit(&commit, repo, id);
595 if (error)
596 goto err;
597 error = got_object_commit_get_logmsg(&commit_msg0, commit);
598 if (error)
599 goto err;
600 if (commit) {
601 got_object_commit_close(commit);
602 commit = NULL;
606 error = got_repo_map_path(&in_repo_path, repo, repo_path);
607 if (error)
608 goto err;
610 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
611 repo);
612 if (error)
613 goto err;
615 if (limit == 1)
616 chk_multi = 0;
618 /*
619 * XXX: again, see previous message about caching
620 */
622 TAILQ_FOREACH(re, &refs, entry) {
623 struct repo_tag *new_repo_tag = NULL;
624 error = got_init_repo_tag(&new_repo_tag);
625 if (error)
626 goto err;
628 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
630 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
631 if (new_repo_tag->tag_name == NULL) {
632 error = got_error_from_errno("strdup");
633 goto err;
636 free(id);
637 id = NULL;
639 free(id_str);
640 id_str = NULL;
642 error = got_ref_resolve(&id, repo, re->ref);
643 if (error)
644 goto done;
646 if (tag)
647 got_object_tag_close(tag);
648 error = got_object_open_as_tag(&tag, repo, id);
649 if (error) {
650 if (error->code != GOT_ERR_OBJ_TYPE)
651 goto done;
652 /* "lightweight" tag */
653 error = got_object_open_as_commit(&commit, repo, id);
654 if (error)
655 goto done;
656 new_repo_tag->tagger =
657 strdup(got_object_commit_get_committer(commit));
658 if (new_repo_tag->tagger == NULL) {
659 error = got_error_from_errno("strdup");
660 goto err;
662 new_repo_tag->tagger_time =
663 got_object_commit_get_committer_time(commit);
664 error = got_object_id_str(&id_str, id);
665 if (error)
666 goto err;
667 } else {
668 new_repo_tag->tagger =
669 strdup(got_object_tag_get_tagger(tag));
670 if (new_repo_tag->tagger == NULL) {
671 error = got_error_from_errno("strdup");
672 goto err;
674 new_repo_tag->tagger_time =
675 got_object_tag_get_tagger_time(tag);
676 error = got_object_id_str(&id_str,
677 got_object_tag_get_object_id(tag));
678 if (error)
679 goto err;
682 new_repo_tag->commit_id = strdup(id_str);
683 if (new_repo_tag->commit_id == NULL)
684 goto err;
686 if (commit_found == 0 && qs->commit != NULL &&
687 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
688 continue;
689 else
690 commit_found = 1;
692 t->tag_count++;
694 /*
695 * check for one more commit before breaking,
696 * so we know whether to navigate through briefs
697 * commits and summary
698 */
699 if (chk_next) {
700 t->next_id = strdup(new_repo_tag->commit_id);
701 if (t->next_id == NULL) {
702 error = got_error_from_errno("strdup");
703 goto err;
705 if (commit) {
706 got_object_commit_close(commit);
707 commit = NULL;
709 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
710 gotweb_free_repo_tag(new_repo_tag);
711 goto done;
714 if (commit) {
715 error = got_object_commit_get_logmsg(&tag_commit0,
716 commit);
717 if (error)
718 goto err;
719 got_object_commit_close(commit);
720 commit = NULL;
721 } else {
722 tag_commit0 = strdup(got_object_tag_get_message(tag));
723 if (tag_commit0 == NULL) {
724 error = got_error_from_errno("strdup");
725 goto err;
729 tag_commit = tag_commit0;
730 while (*tag_commit == '\n')
731 tag_commit++;
732 new_repo_tag->tag_commit = strdup(tag_commit);
733 if (new_repo_tag->tag_commit == NULL) {
734 error = got_error_from_errno("strdup");
735 free(tag_commit0);
736 goto err;
738 free(tag_commit0);
740 if (qs->action != SUMMARY && qs->action != TAGS) {
741 commit_msg = commit_msg0;
742 while (*commit_msg == '\n')
743 commit_msg++;
745 new_repo_tag->commit_msg = strdup(commit_msg);
746 if (new_repo_tag->commit_msg == NULL) {
747 error = got_error_from_errno("strdup");
748 goto err;
752 if (limit && --limit == 0) {
753 if (chk_multi == 0)
754 break;
755 chk_next = 1;
759 done:
760 /*
761 * we have tailq populated, so find previous commit id
762 * for navigation through briefs and commits
763 */
764 if (t->tag_count == 0) {
765 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
766 TAILQ_REMOVE(&t->repo_tags, rt, entry);
767 gotweb_free_repo_tag(rt);
770 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
771 commit_found = 0;
772 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
773 entry) {
774 if (commit_found == 0 && rt->commit_id != NULL &&
775 strcmp(qs->commit, rt->commit_id) != 0) {
776 continue;
777 } else
778 commit_found = 1;
779 if (c_cnt == srv->max_commits_display ||
780 rt == TAILQ_FIRST(&t->repo_tags)) {
781 t->prev_id = strdup(rt->commit_id);
782 if (t->prev_id == NULL)
783 error = got_error_from_errno("strdup");
784 break;
786 c_cnt++;
789 err:
790 if (commit)
791 got_object_commit_close(commit);
792 if (tag)
793 got_object_tag_close(tag);
794 got_ref_list_free(&refs);
795 free(commit_msg0);
796 free(in_repo_path);
797 free(repo_path);
798 free(id);
799 free(id_str);
800 return error;
803 const struct got_error *
804 got_output_repo_tree(struct request *c)
806 const struct got_error *error = NULL;
807 struct transport *t = c->t;
808 struct got_commit_object *commit = NULL;
809 struct got_repository *repo = t->repo;
810 struct querystring *qs = t->qs;
811 struct repo_commit *rc = NULL;
812 struct got_object_id *tree_id = NULL, *commit_id = NULL;
813 struct got_reflist_head refs;
814 struct got_tree_object *tree = NULL;
815 struct repo_dir *repo_dir = t->repo_dir;
816 const char *name, *folder;
817 char *escaped_name = NULL, *path = NULL;
818 int nentries, i, r;
820 TAILQ_INIT(&refs);
822 rc = TAILQ_FIRST(&t->repo_commits);
824 if (qs->folder != NULL) {
825 path = strdup(qs->folder);
826 if (path == NULL) {
827 error = got_error_from_errno("strdup");
828 goto done;
830 } else {
831 error = got_repo_map_path(&path, repo, repo_dir->path);
832 if (error)
833 goto done;
836 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
837 GOT_OBJ_TYPE_COMMIT, &refs, repo);
838 if (error)
839 goto done;
841 error = got_object_open_as_commit(&commit, repo, commit_id);
842 if (error)
843 goto done;
845 error = got_object_id_by_path(&tree_id, repo, commit, path);
846 if (error)
847 goto done;
849 error = got_object_open_as_tree(&tree, repo, tree_id);
850 if (error)
851 goto done;
853 nentries = got_object_tree_get_nentries(tree);
855 folder = qs->folder ? qs->folder : "";
857 for (i = 0; i < nentries; i++) {
858 const char *modestr;
859 struct got_tree_entry *te;
860 mode_t mode;
862 te = got_object_tree_get_entry(tree, i);
864 mode = got_tree_entry_get_mode(te);
865 if (got_object_tree_entry_is_submodule(te))
866 modestr = "$";
867 else if (S_ISLNK(mode))
868 modestr = "@";
869 else if (S_ISDIR(mode))
870 modestr = "/";
871 else if (mode & S_IXUSR)
872 modestr = "*";
873 else
874 modestr = "";
876 name = got_tree_entry_get_name(te);
877 error = gotweb_escape_html(&escaped_name, name);
878 if (error)
879 goto done;
881 if (S_ISDIR(mode)) {
882 struct gotweb_url url = {
883 .index_page = -1,
884 .page = -1,
885 .action = TREE,
886 .commit = rc->commit_id,
887 .path = qs->path,
888 /* `folder' is filled later */
889 };
890 char *path = NULL;
892 if (fcgi_printf(c,"<div class='tree_wrapper'>\n"
893 "<div class='tree_line'>") == -1)
894 goto done;
896 if (asprintf(&path, "%s/%s", folder, name) == -1) {
897 error = got_error_from_errno("asprintf");
898 goto done;
900 url.folder = path;
901 r = gotweb_link(c, &url, "%s%s", escaped_name,
902 modestr);
903 free(path);
904 if (r == -1)
905 goto done;
907 if (fcgi_printf(c, "</div>\n" /* .tree_line */
908 "<div class='tree_line_blank'>&nbsp;</div>\n"
909 "</div>\n") == -1)
910 goto done;
911 } else {
912 struct gotweb_url url = {
913 .index_page = -1,
914 .page = -1,
915 .path = qs->path,
916 .commit = rc->commit_id,
917 .folder = folder,
918 .file = name,
919 };
921 if (fcgi_printf(c, "<div class='tree_wrapper'>\n"
922 "<div class='tree_line'>") == -1)
923 goto done;
925 url.action = BLOB;
926 r = gotweb_link(c, &url, "%s%s", escaped_name,
927 modestr);
928 if (r == -1)
929 goto done;
931 if (fcgi_printf(c, "</div>\n" /* .tree_line */
932 "<div class='tree_line_blank'>") == -1)
933 goto done;
935 url.action = COMMITS;
936 r = gotweb_link(c, &url, "commits");
937 if (r == -1)
938 goto done;
940 if (fcgi_printf(c, " | ") == -1)
941 goto done;
943 url.action = BLAME;
944 r = gotweb_link(c, &url, "blame");
945 if (r == -1)
946 goto done;
948 if (fcgi_printf(c,
949 "</div>\n" /* .tree_line_blank */
950 "</div>\n") == -1) /* .tree_wrapper */
951 goto done;
953 free(escaped_name);
954 escaped_name = NULL;
956 done:
957 free(escaped_name);
958 free(path);
959 got_ref_list_free(&refs);
960 if (commit)
961 got_object_commit_close(commit);
962 if (tree)
963 got_object_tree_close(tree);
964 free(commit_id);
965 free(tree_id);
966 return error;
969 const struct got_error *
970 got_output_file_blob(struct request *c)
972 const struct got_error *error = NULL;
973 struct transport *t = c->t;
974 struct got_repository *repo = t->repo;
975 struct querystring *qs = c->t->qs;
976 struct got_commit_object *commit = NULL;
977 struct got_object_id *commit_id = NULL;
978 struct got_reflist_head refs;
979 struct got_blob_object *blob = NULL;
980 char *path = NULL, *in_repo_path = NULL;
981 int obj_type, set_mime = 0, fd = -1;
982 size_t len, hdrlen;
983 const uint8_t *buf;
985 TAILQ_INIT(&refs);
987 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
988 qs->folder ? "/" : "", qs->file) == -1) {
989 error = got_error_from_errno("asprintf");
990 goto done;
993 error = got_repo_map_path(&in_repo_path, repo, path);
994 if (error)
995 goto done;
997 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
998 GOT_OBJ_TYPE_COMMIT, &refs, repo);
999 if (error)
1000 goto done;
1002 error = got_object_open_as_commit(&commit, repo, commit_id);
1003 if (error)
1004 goto done;
1006 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
1007 if (error)
1008 goto done;
1010 if (commit_id == NULL) {
1011 error = got_error(GOT_ERR_NO_OBJ);
1012 goto done;
1015 error = got_object_get_type(&obj_type, repo, commit_id);
1016 if (error)
1017 goto done;
1019 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1020 error = got_error(GOT_ERR_OBJ_TYPE);
1021 goto done;
1024 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
1025 if (error)
1026 goto done;
1028 error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
1029 if (error)
1030 goto done;
1031 hdrlen = got_object_blob_get_hdrlen(blob);
1032 do {
1033 error = got_object_blob_read_block(&len, blob);
1034 if (error)
1035 goto done;
1036 buf = got_object_blob_get_read_buf(blob);
1039 * Skip blob object header first time around,
1040 * which also contains a zero byte.
1042 buf += hdrlen;
1043 if (set_mime == 0) {
1044 if (isbinary(buf, len - hdrlen)) {
1045 error = gotweb_render_content_type_file(c,
1046 "application/octet-stream",
1047 qs->file);
1048 if (error) {
1049 log_warnx("%s: %s", __func__,
1050 error->msg);
1051 goto done;
1053 } else {
1054 error = gotweb_render_content_type(c,
1055 "text/plain");
1056 if (error) {
1057 log_warnx("%s: %s", __func__,
1058 error->msg);
1059 goto done;
1063 set_mime = 1;
1064 fcgi_gen_binary_response(c, buf, len - hdrlen);
1066 hdrlen = 0;
1067 } while (len != 0);
1068 done:
1069 if (commit)
1070 got_object_commit_close(commit);
1071 if (fd != -1 && close(fd) == -1 && error == NULL)
1072 error = got_error_from_errno("close");
1073 if (blob)
1074 got_object_blob_close(blob);
1075 free(in_repo_path);
1076 free(commit_id);
1077 free(path);
1078 return error;
1081 struct blame_line {
1082 int annotated;
1083 char *id_str;
1084 char *committer;
1085 char datebuf[11]; /* YYYY-MM-DD + NUL */
1088 struct blame_cb_args {
1089 struct blame_line *lines;
1090 int nlines;
1091 int nlines_prec;
1092 int lineno_cur;
1093 off_t *line_offsets;
1094 FILE *f;
1095 struct got_repository *repo;
1096 struct request *c;
1099 static const struct got_error *
1100 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1101 struct got_commit_object *commit, struct got_object_id *id)
1103 const struct got_error *err = NULL;
1104 struct blame_cb_args *a = arg;
1105 struct blame_line *bline;
1106 struct request *c = a->c;
1107 struct transport *t = c->t;
1108 struct repo_dir *repo_dir = t->repo_dir;
1109 char *line = NULL, *eline = NULL;
1110 size_t linesize = 0;
1111 off_t offset;
1112 struct tm tm;
1113 time_t committer_time;
1114 int r;
1116 if (nlines != a->nlines ||
1117 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1118 return got_error(GOT_ERR_RANGE);
1120 if (lineno == -1)
1121 return NULL; /* no change in this commit */
1123 /* Annotate this line. */
1124 bline = &a->lines[lineno - 1];
1125 if (bline->annotated)
1126 return NULL;
1127 err = got_object_id_str(&bline->id_str, id);
1128 if (err)
1129 return err;
1131 bline->committer = strdup(got_object_commit_get_committer(commit));
1132 if (bline->committer == NULL) {
1133 err = got_error_from_errno("strdup");
1134 goto done;
1137 committer_time = got_object_commit_get_committer_time(commit);
1138 if (gmtime_r(&committer_time, &tm) == NULL)
1139 return got_error_from_errno("gmtime_r");
1140 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1141 &tm) == 0) {
1142 err = got_error(GOT_ERR_NO_SPACE);
1143 goto done;
1145 bline->annotated = 1;
1147 /* Print lines annotated so far. */
1148 bline = &a->lines[a->lineno_cur - 1];
1149 if (!bline->annotated)
1150 goto done;
1152 offset = a->line_offsets[a->lineno_cur - 1];
1153 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1154 err = got_error_from_errno("fseeko");
1155 goto done;
1158 while (a->lineno_cur <= a->nlines && bline->annotated) {
1159 char *smallerthan, *at, *nl, *committer;
1160 size_t len;
1162 if (getline(&line, &linesize, a->f) == -1) {
1163 if (ferror(a->f))
1164 err = got_error_from_errno("getline");
1165 break;
1168 committer = bline->committer;
1169 smallerthan = strchr(committer, '<');
1170 if (smallerthan && smallerthan[1] != '\0')
1171 committer = smallerthan + 1;
1172 at = strchr(committer, '@');
1173 if (at)
1174 *at = '\0';
1175 len = strlen(committer);
1176 if (len >= 9)
1177 committer[8] = '\0';
1179 nl = strchr(line, '\n');
1180 if (nl)
1181 *nl = '\0';
1183 err = gotweb_escape_html(&eline, line);
1184 if (err)
1185 goto done;
1187 if (fcgi_printf(c, "<div class='blame_wrapper'>"
1188 "<div class='blame_number'>%.*d</div>"
1189 "<div class='blame_hash'>",
1190 a->nlines_prec, a->lineno_cur) == -1)
1191 goto done;
1193 r = gotweb_link(c, &(struct gotweb_url){
1194 .action = DIFF,
1195 .index_page = -1,
1196 .page = -1,
1197 .path = repo_dir->name,
1198 .commit = bline->id_str,
1199 }, "%.8s", bline->id_str);
1200 if (r == -1)
1201 goto done;
1203 r = fcgi_printf(c,
1204 "</div>"
1205 "<div class='blame_date'>%s</div>"
1206 "<div class='blame_author'>%s</div>"
1207 "<div class='blame_code'>%s</div>"
1208 "</div>", /* .blame_wrapper */
1209 bline->datebuf,
1210 committer,
1211 eline);
1212 if (r == -1)
1213 goto done;
1215 a->lineno_cur++;
1216 bline = &a->lines[a->lineno_cur - 1];
1218 free(eline);
1219 eline = NULL;
1221 done:
1222 free(line);
1223 free(eline);
1224 return err;
1227 const struct got_error *
1228 got_output_file_blame(struct request *c)
1230 const struct got_error *error = NULL;
1231 struct transport *t = c->t;
1232 struct got_repository *repo = t->repo;
1233 struct querystring *qs = c->t->qs;
1234 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1235 struct got_commit_object *commit = NULL;
1236 struct got_reflist_head refs;
1237 struct got_blob_object *blob = NULL;
1238 char *path = NULL, *in_repo_path = NULL;
1239 struct blame_cb_args bca;
1240 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1241 int fd6 = -1;
1242 off_t filesize;
1243 FILE *f1 = NULL, *f2 = NULL;
1245 TAILQ_INIT(&refs);
1246 bca.f = NULL;
1247 bca.lines = NULL;
1249 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1250 qs->folder ? "/" : "", qs->file) == -1) {
1251 error = got_error_from_errno("asprintf");
1252 goto done;
1255 error = got_repo_map_path(&in_repo_path, repo, path);
1256 if (error)
1257 goto done;
1259 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1260 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1261 if (error)
1262 goto done;
1264 error = got_object_open_as_commit(&commit, repo, commit_id);
1265 if (error)
1266 goto done;
1268 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1269 if (error)
1270 goto done;
1272 error = got_object_get_type(&obj_type, repo, obj_id);
1273 if (error)
1274 goto done;
1276 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1277 error = got_error(GOT_ERR_OBJ_TYPE);
1278 goto done;
1281 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1282 if (error)
1283 goto done;
1285 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1286 if (error)
1287 goto done;
1289 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1290 if (error)
1291 goto done;
1293 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1294 &bca.line_offsets, bca.f, blob);
1295 if (error || bca.nlines == 0)
1296 goto done;
1298 /* Don't include \n at EOF in the blame line count. */
1299 if (bca.line_offsets[bca.nlines - 1] == filesize)
1300 bca.nlines--;
1302 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1303 if (bca.lines == NULL) {
1304 error = got_error_from_errno("calloc");
1305 goto done;
1307 bca.lineno_cur = 1;
1308 bca.nlines_prec = 0;
1309 i = bca.nlines;
1310 while (i > 0) {
1311 i /= 10;
1312 bca.nlines_prec++;
1314 bca.repo = repo;
1315 bca.c = c;
1317 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1318 if (error)
1319 goto done;
1321 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1322 if (error)
1323 goto done;
1325 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1326 if (error)
1327 goto done;
1329 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1330 if (error)
1331 goto done;
1333 error = got_blame(in_repo_path, commit_id, repo,
1334 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1335 fd3, fd4, f1, f2);
1337 done:
1338 if (bca.lines) {
1339 free(bca.line_offsets);
1340 for (i = 0; i < bca.nlines; i++) {
1341 struct blame_line *bline = &bca.lines[i];
1342 free(bline->id_str);
1343 free(bline->committer);
1346 free(bca.lines);
1347 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1348 error = got_error_from_errno("close");
1349 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1350 error = got_error_from_errno("close");
1351 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1352 error = got_error_from_errno("close");
1353 if (bca.f) {
1354 const struct got_error *bca_err =
1355 got_gotweb_flushfile(bca.f, fd1);
1356 if (error == NULL)
1357 error = bca_err;
1359 if (f1) {
1360 const struct got_error *f1_err =
1361 got_gotweb_flushfile(f1, fd5);
1362 if (error == NULL)
1363 error = f1_err;
1365 if (f2) {
1366 const struct got_error *f2_err =
1367 got_gotweb_flushfile(f2, fd6);
1368 if (error == NULL)
1369 error = f2_err;
1371 if (commit)
1372 got_object_commit_close(commit);
1373 if (blob)
1374 got_object_blob_close(blob);
1375 free(in_repo_path);
1376 free(commit_id);
1377 free(obj_id);
1378 free(path);
1379 got_ref_list_free(&refs);
1380 return error;
1383 const struct got_error *
1384 got_output_repo_diff(struct request *c)
1386 const struct got_error *error = NULL;
1387 struct transport *t = c->t;
1388 struct got_repository *repo = t->repo;
1389 struct repo_commit *rc = NULL;
1390 struct got_object_id *id1 = NULL, *id2 = NULL;
1391 struct got_reflist_head refs;
1392 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1393 char *label1 = NULL, *label2 = NULL, *line = NULL;
1394 char *newline, *eline = NULL, *color = NULL;
1395 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1396 size_t linesize = 0;
1397 ssize_t linelen;
1398 int wrlen = 0;
1400 TAILQ_INIT(&refs);
1402 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1403 if (error)
1404 return error;
1406 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1407 if (error)
1408 return error;
1410 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1411 if (error)
1412 return error;
1414 rc = TAILQ_FIRST(&t->repo_commits);
1416 if (rc->parent_id != NULL &&
1417 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1418 error = got_repo_match_object_id(&id1, &label1,
1419 rc->parent_id, GOT_OBJ_TYPE_ANY,
1420 &refs, repo);
1421 if (error)
1422 goto done;
1425 error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1426 GOT_OBJ_TYPE_ANY, &refs, repo);
1427 if (error)
1428 goto done;
1430 error = got_object_get_type(&obj_type, repo, id2);
1431 if (error)
1432 goto done;
1434 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1435 if (error)
1436 goto done;
1438 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1439 if (error)
1440 goto done;
1442 switch (obj_type) {
1443 case GOT_OBJ_TYPE_BLOB:
1444 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1445 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1446 repo, f3);
1447 break;
1448 case GOT_OBJ_TYPE_TREE:
1449 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1450 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1451 repo, f3);
1452 break;
1453 case GOT_OBJ_TYPE_COMMIT:
1454 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1455 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1456 repo, f3);
1457 break;
1458 default:
1459 error = got_error(GOT_ERR_OBJ_TYPE);
1461 if (error)
1462 goto done;
1464 if (fseek(f1, 0, SEEK_SET) == -1) {
1465 error = got_ferror(f1, GOT_ERR_IO);
1466 goto done;
1469 if (fseek(f2, 0, SEEK_SET) == -1) {
1470 error = got_ferror(f2, GOT_ERR_IO);
1471 goto done;
1474 if (fseek(f3, 0, SEEK_SET) == -1) {
1475 error = got_ferror(f3, GOT_ERR_IO);
1476 goto done;
1479 while ((linelen = getline(&line, &linesize, f3)) != -1) {
1480 if (strncmp(line, "-", 1) == 0) {
1481 color = strdup("diff_minus");
1482 if (color == NULL) {
1483 error = got_error_from_errno("strdup");
1484 goto done;
1486 } else if (strncmp(line, "+", 1) == 0) {
1487 color = strdup("diff_plus");
1488 if (color == NULL) {
1489 error = got_error_from_errno("strdup");
1490 goto done;
1492 } else if (strncmp(line, "@@", 2) == 0) {
1493 color = strdup("diff_chunk_header");
1494 if (color == NULL) {
1495 error = got_error_from_errno("strdup");
1496 goto done;
1498 } else if (strncmp(line, "@@", 2) == 0) {
1499 color = strdup("diff_chunk_header");
1500 if (color == NULL) {
1501 error = got_error_from_errno("strdup");
1502 goto done;
1504 } else if (strncmp(line, "commit +", 8) == 0) {
1505 color = strdup("diff_meta");
1506 if (color == NULL) {
1507 error = got_error_from_errno("strdup");
1508 goto done;
1510 } else if (strncmp(line, "commit -", 8) == 0) {
1511 color = strdup("diff_meta");
1512 if (color == NULL) {
1513 error = got_error_from_errno("strdup");
1514 goto done;
1516 } else if (strncmp(line, "blob +", 6) == 0) {
1517 color = strdup("diff_meta");
1518 if (color == NULL) {
1519 error = got_error_from_errno("strdup");
1520 goto done;
1522 } else if (strncmp(line, "blob -", 6) == 0) {
1523 color = strdup("diff_meta");
1524 if (color == NULL) {
1525 error = got_error_from_errno("strdup");
1526 goto done;
1528 } else if (strncmp(line, "file +", 6) == 0) {
1529 color = strdup("diff_meta");
1530 if (color == NULL) {
1531 error = got_error_from_errno("strdup");
1532 goto done;
1534 } else if (strncmp(line, "file -", 6) == 0) {
1535 color = strdup("diff_meta");
1536 if (color == NULL) {
1537 error = got_error_from_errno("strdup");
1538 goto done;
1540 } else if (strncmp(line, "from:", 5) == 0) {
1541 color = strdup("diff_author");
1542 if (color == NULL) {
1543 error = got_error_from_errno("strdup");
1544 goto done;
1546 } else if (strncmp(line, "via:", 4) == 0) {
1547 color = strdup("diff_author");
1548 if (color == NULL) {
1549 error = got_error_from_errno("strdup");
1550 goto done;
1552 } else if (strncmp(line, "date:", 5) == 0) {
1553 color = strdup("diff_date");
1554 if (color == NULL) {
1555 error = got_error_from_errno("strdup");
1556 goto done;
1560 newline = strchr(line, '\n');
1561 if (newline)
1562 *newline = '\0';
1564 error = gotweb_escape_html(&eline, line);
1565 if (error)
1566 goto done;
1568 fcgi_printf(c, "<div class='diff_line %s'>%s</div>\n",
1569 color ? color : "", eline);
1570 free(eline);
1571 eline = NULL;
1573 if (linelen > 0)
1574 wrlen = wrlen + linelen;
1575 free(color);
1576 color = NULL;
1578 if (linelen == -1 && ferror(f3))
1579 error = got_error_from_errno("getline");
1580 done:
1581 free(color);
1582 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1583 error = got_error_from_errno("close");
1584 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1585 error = got_error_from_errno("close");
1586 if (f1) {
1587 const struct got_error *f1_err =
1588 got_gotweb_flushfile(f1, fd1);
1589 if (error == NULL)
1590 error = f1_err;
1592 if (f2) {
1593 const struct got_error *f2_err =
1594 got_gotweb_flushfile(f2, fd2);
1595 if (error == NULL)
1596 error = f2_err;
1598 if (f3) {
1599 const struct got_error *f3_err =
1600 got_gotweb_flushfile(f3, fd3);
1601 if (error == NULL)
1602 error = f3_err;
1604 got_ref_list_free(&refs);
1605 free(line);
1606 free(eline);
1607 free(label1);
1608 free(label2);
1609 free(id1);
1610 free(id2);
1611 return error;
1614 static const struct got_error *
1615 got_init_repo_commit(struct repo_commit **rc)
1617 *rc = calloc(1, sizeof(**rc));
1618 if (*rc == NULL)
1619 return got_error_from_errno2("%s: calloc", __func__);
1621 (*rc)->path = NULL;
1622 (*rc)->refs_str = NULL;
1623 (*rc)->commit_id = NULL;
1624 (*rc)->committer = NULL;
1625 (*rc)->author = NULL;
1626 (*rc)->parent_id = NULL;
1627 (*rc)->tree_id = NULL;
1628 (*rc)->commit_msg = NULL;
1630 return NULL;
1633 static const struct got_error *
1634 got_init_repo_tag(struct repo_tag **rt)
1636 *rt = calloc(1, sizeof(**rt));
1637 if (*rt == NULL)
1638 return got_error_from_errno2("%s: calloc", __func__);
1640 (*rt)->commit_id = NULL;
1641 (*rt)->tag_name = NULL;
1642 (*rt)->tag_commit = NULL;
1643 (*rt)->commit_msg = NULL;
1644 (*rt)->tagger = NULL;
1646 return NULL;