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