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 error = got_init_repo_commit(&repo_commit);
359 if (error)
360 goto done;
362 /*
363 * XXX: jumping directly to a commit id via
364 * got_repo_match_object_id_prefix significantly improves performance,
365 * but does not allow us to create a PREVIOUS button, since commits can
366 * only be itereated forward. So, we have to match as we iterate from
367 * the headref.
368 */
369 if (qs->action == BRIEFS || qs->action == COMMITS ||
370 (qs->action == TREE && qs->commit == NULL)) {
371 error = got_ref_open(&ref, repo, qs->headref, 0);
372 if (error)
373 goto done;
375 error = got_ref_resolve(&id, repo, ref);
376 if (error)
377 goto done;
378 } else if (qs->commit != NULL) {
379 error = got_ref_open(&ref, repo, qs->commit, 0);
380 if (error == NULL) {
381 error = got_ref_resolve(&id, repo, ref);
382 if (error)
383 goto done;
384 error = got_object_get_type(&obj_type, repo, id);
385 if (error)
386 goto done;
387 if (obj_type == GOT_OBJ_TYPE_TAG) {
388 struct got_tag_object *tag;
389 error = got_object_open_as_tag(&tag, repo, id);
390 if (error)
391 goto done;
392 if (got_object_tag_get_object_type(tag) !=
393 GOT_OBJ_TYPE_COMMIT) {
394 got_object_tag_close(tag);
395 error = got_error(GOT_ERR_OBJ_TYPE);
396 goto done;
398 free(id);
399 id = got_object_id_dup(
400 got_object_tag_get_object_id(tag));
401 if (id == NULL)
402 error = got_error_from_errno(
403 "got_object_id_dup");
404 got_object_tag_close(tag);
405 if (error)
406 goto done;
407 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
408 error = got_error(GOT_ERR_OBJ_TYPE);
409 goto done;
412 error = got_repo_match_object_id_prefix(&id, qs->commit,
413 GOT_OBJ_TYPE_COMMIT, repo);
414 if (error)
415 goto done;
418 error = got_repo_map_path(&in_repo_path, repo, repo_path);
419 if (error)
420 goto done;
422 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
423 if (error)
424 goto done;
426 if (qs->file != NULL && strlen(qs->file) > 0) {
427 error = got_commit_graph_open(&graph, file_path, 0);
428 if (error)
429 goto done;
430 } else {
431 error = got_commit_graph_open(&graph, in_repo_path, 0);
432 if (error)
433 goto done;
436 error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
437 if (error)
438 goto done;
440 for (;;) {
441 if (limit_chk == ((limit * qs->page) - (limit - 1)) &&
442 commit_found == 0 && repo_commit->commit_id != NULL) {
443 t->prev_id = strdup(repo_commit->commit_id);
444 if (t->prev_id == NULL) {
445 error = got_error_from_errno("strdup");
446 goto done;
450 error = got_commit_graph_iter_next(&id, graph, repo, NULL,
451 NULL);
452 if (error) {
453 if (error->code == GOT_ERR_ITER_COMPLETED)
454 error = NULL;
455 goto done;
457 if (id == NULL)
458 goto done;
460 error = got_object_open_as_commit(&commit, repo, id);
461 if (error)
462 goto done;
464 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
465 NULL);
466 if (error)
467 goto done;
469 error = got_get_repo_commit(c, repo_commit, commit,
470 &refs, id);
471 if (error)
472 goto done;
474 if (qs->commit != NULL && commit_found == 0 && limit != 1) {
475 if (strcmp(qs->commit, repo_commit->commit_id) == 0)
476 commit_found = 1;
477 else if (qs->file != NULL && strlen(qs->file) > 0 &&
478 qs->page == 0)
479 commit_found = 1;
480 else {
481 limit_chk++;
482 free(id);
483 id = NULL;
484 continue;
488 struct repo_commit *new_repo_commit = NULL;
489 error = got_init_repo_commit(&new_repo_commit);
490 if (error)
491 goto done;
493 TAILQ_INSERT_TAIL(&t->repo_commits, new_repo_commit, entry);
495 error = got_get_repo_commit(c, new_repo_commit, commit,
496 &refs, id);
497 if (error)
498 goto done;
500 free(id);
501 id = NULL;
503 if (limit == 1 && chk_multi == 0 &&
504 srv->max_commits_display != 1)
505 commit_found = 1;
506 else {
507 chk_multi = 1;
509 /*
510 * check for one more commit before breaking,
511 * so we know whether to navigate through briefs
512 * commits and summary
513 */
514 if (chk_next && (qs->action == BRIEFS ||
515 qs->action == COMMITS || qs->action == SUMMARY)) {
516 t->next_id = strdup(new_repo_commit->commit_id);
517 if (t->next_id == NULL) {
518 error = got_error_from_errno("strdup");
519 goto done;
521 if (commit) {
522 got_object_commit_close(commit);
523 commit = NULL;
525 TAILQ_REMOVE(&t->repo_commits, new_repo_commit,
526 entry);
527 gotweb_free_repo_commit(new_repo_commit);
528 goto done;
531 got_ref_list_free(&refs);
532 if (error || (limit && --limit == 0)) {
533 if (commit_found || (qs->file != NULL &&
534 strlen(qs->file) > 0))
535 if (chk_multi == 0)
536 break;
537 chk_next = 1;
539 if (commit) {
540 got_object_commit_close(commit);
541 commit = NULL;
544 done:
545 gotweb_free_repo_commit(repo_commit);
546 if (ref)
547 got_ref_close(ref);
548 if (commit)
549 got_object_commit_close(commit);
550 if (graph)
551 got_commit_graph_close(graph);
552 got_ref_list_free(&refs);
553 free(in_repo_path);
554 free(file_path);
555 free(repo_path);
556 free(id);
557 return error;
560 const struct got_error *
561 got_get_repo_tags(struct request *c, int limit)
563 const struct got_error *error = NULL;
564 struct got_object_id *id = NULL;
565 struct got_commit_object *commit = NULL;
566 struct got_reflist_head refs;
567 struct got_reference *ref;
568 struct got_reflist_entry *re;
569 struct server *srv = c->srv;
570 struct transport *t = c->t;
571 struct got_repository *repo = t->repo;
572 struct querystring *qs = t->qs;
573 struct repo_dir *repo_dir = t->repo_dir;
574 struct got_tag_object *tag = NULL;
575 struct repo_tag *rt = NULL, *trt = NULL;
576 char *in_repo_path = NULL, *repo_path = NULL, *id_str = NULL;
577 char *tag_commit = NULL, *tag_commit0 = NULL;
578 char *commit_msg = NULL, *commit_msg0 = NULL;
579 int chk_next = 0, chk_multi = 1, commit_found = 0, c_cnt = 0;
581 TAILQ_INIT(&refs);
583 if (asprintf(&repo_path, "%s/%s", srv->repos_path,
584 repo_dir->name) == -1)
585 return got_error_from_errno("asprintf");
587 if (qs->commit == NULL && qs->action == TAGS) {
588 error = got_ref_open(&ref, repo, qs->headref, 0);
589 if (error)
590 goto err;
591 error = got_ref_resolve(&id, repo, ref);
592 got_ref_close(ref);
593 if (error)
594 goto err;
595 } else if (qs->commit == NULL && qs->action == TAG) {
596 error = got_error_msg(GOT_ERR_EOF, "commit id missing");
597 goto err;
598 } else {
599 error = got_repo_match_object_id_prefix(&id, qs->commit,
600 GOT_OBJ_TYPE_COMMIT, repo);
601 if (error)
602 goto err;
605 if (qs->action != SUMMARY && qs->action != TAGS) {
606 error = got_object_open_as_commit(&commit, repo, id);
607 if (error)
608 goto err;
609 error = got_object_commit_get_logmsg(&commit_msg0, commit);
610 if (error)
611 goto err;
612 if (commit) {
613 got_object_commit_close(commit);
614 commit = NULL;
618 error = got_repo_map_path(&in_repo_path, repo, repo_path);
619 if (error)
620 goto err;
622 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags,
623 repo);
624 if (error)
625 goto err;
627 if (limit == 1)
628 chk_multi = 0;
630 /*
631 * XXX: again, see previous message about caching
632 */
634 TAILQ_FOREACH(re, &refs, entry) {
635 struct repo_tag *new_repo_tag = NULL;
636 error = got_init_repo_tag(&new_repo_tag);
637 if (error)
638 goto err;
640 TAILQ_INSERT_TAIL(&t->repo_tags, new_repo_tag, entry);
642 new_repo_tag->tag_name = strdup(got_ref_get_name(re->ref));
643 if (new_repo_tag->tag_name == NULL) {
644 error = got_error_from_errno("strdup");
645 goto err;
648 free(id);
649 id = NULL;
651 free(id_str);
652 id_str = NULL;
654 error = got_ref_resolve(&id, repo, re->ref);
655 if (error)
656 goto done;
658 if (tag)
659 got_object_tag_close(tag);
660 error = got_object_open_as_tag(&tag, repo, id);
661 if (error) {
662 if (error->code != GOT_ERR_OBJ_TYPE)
663 goto done;
664 /* "lightweight" tag */
665 error = got_object_open_as_commit(&commit, repo, id);
666 if (error)
667 goto done;
668 new_repo_tag->tagger =
669 strdup(got_object_commit_get_committer(commit));
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_commit_get_committer_time(commit);
676 error = got_object_id_str(&id_str, id);
677 if (error)
678 goto err;
679 } else {
680 new_repo_tag->tagger =
681 strdup(got_object_tag_get_tagger(tag));
682 if (new_repo_tag->tagger == NULL) {
683 error = got_error_from_errno("strdup");
684 goto err;
686 new_repo_tag->tagger_time =
687 got_object_tag_get_tagger_time(tag);
688 error = got_object_id_str(&id_str,
689 got_object_tag_get_object_id(tag));
690 if (error)
691 goto err;
694 new_repo_tag->commit_id = strdup(id_str);
695 if (new_repo_tag->commit_id == NULL)
696 goto err;
698 if (commit_found == 0 && qs->commit != NULL &&
699 strncmp(id_str, qs->commit, strlen(id_str)) != 0)
700 continue;
701 else
702 commit_found = 1;
704 t->tag_count++;
706 /*
707 * check for one more commit before breaking,
708 * so we know whether to navigate through briefs
709 * commits and summary
710 */
711 if (chk_next) {
712 t->next_id = strdup(new_repo_tag->commit_id);
713 if (t->next_id == NULL) {
714 error = got_error_from_errno("strdup");
715 goto err;
717 if (commit) {
718 got_object_commit_close(commit);
719 commit = NULL;
721 TAILQ_REMOVE(&t->repo_tags, new_repo_tag, entry);
722 gotweb_free_repo_tag(new_repo_tag);
723 goto done;
726 if (commit) {
727 error = got_object_commit_get_logmsg(&tag_commit0,
728 commit);
729 if (error)
730 goto err;
731 got_object_commit_close(commit);
732 commit = NULL;
733 } else {
734 tag_commit0 = strdup(got_object_tag_get_message(tag));
735 if (tag_commit0 == NULL) {
736 error = got_error_from_errno("strdup");
737 goto err;
741 tag_commit = tag_commit0;
742 while (*tag_commit == '\n')
743 tag_commit++;
744 new_repo_tag->tag_commit = strdup(tag_commit);
745 if (new_repo_tag->tag_commit == NULL) {
746 error = got_error_from_errno("strdup");
747 free(tag_commit0);
748 goto err;
750 free(tag_commit0);
752 if (qs->action != SUMMARY && qs->action != TAGS) {
753 commit_msg = commit_msg0;
754 while (*commit_msg == '\n')
755 commit_msg++;
757 new_repo_tag->commit_msg = strdup(commit_msg);
758 if (new_repo_tag->commit_msg == NULL) {
759 error = got_error_from_errno("strdup");
760 goto err;
764 if (limit && --limit == 0) {
765 if (chk_multi == 0)
766 break;
767 chk_next = 1;
771 done:
772 /*
773 * we have tailq populated, so find previous commit id
774 * for navigation through briefs and commits
775 */
776 if (t->tag_count == 0) {
777 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
778 TAILQ_REMOVE(&t->repo_tags, rt, entry);
779 gotweb_free_repo_tag(rt);
782 if (t->tag_count > 0 && t->prev_id == NULL && qs->commit != NULL) {
783 commit_found = 0;
784 TAILQ_FOREACH_REVERSE(rt, &t->repo_tags, repo_tags_head,
785 entry) {
786 if (commit_found == 0 && rt->commit_id != NULL &&
787 strcmp(qs->commit, rt->commit_id) != 0) {
788 continue;
789 } else
790 commit_found = 1;
791 if (c_cnt == srv->max_commits_display ||
792 rt == TAILQ_FIRST(&t->repo_tags)) {
793 t->prev_id = strdup(rt->commit_id);
794 if (t->prev_id == NULL)
795 error = got_error_from_errno("strdup");
796 break;
798 c_cnt++;
801 err:
802 if (commit)
803 got_object_commit_close(commit);
804 if (tag)
805 got_object_tag_close(tag);
806 got_ref_list_free(&refs);
807 free(commit_msg0);
808 free(in_repo_path);
809 free(repo_path);
810 free(id);
811 free(id_str);
812 return error;
815 const struct got_error *
816 got_output_repo_tree(struct request *c)
818 const struct got_error *error = NULL;
819 struct transport *t = c->t;
820 struct got_commit_object *commit = NULL;
821 struct got_repository *repo = t->repo;
822 struct querystring *qs = t->qs;
823 struct repo_commit *rc = NULL;
824 struct got_object_id *tree_id = NULL, *commit_id = NULL;
825 struct got_reflist_head refs;
826 struct got_tree_object *tree = NULL;
827 struct repo_dir *repo_dir = t->repo_dir;
828 const char *name, *index_page_str, *folder;
829 char *id_str = NULL, *escaped_name = NULL;
830 char *path = NULL, *in_repo_path = NULL, *modestr = NULL;
831 int nentries, i, r;
833 TAILQ_INIT(&refs);
835 rc = TAILQ_FIRST(&t->repo_commits);
837 if (qs->folder != NULL) {
838 path = strdup(qs->folder);
839 if (path == NULL) {
840 error = got_error_from_errno("strdup");
841 goto done;
843 } else {
844 error = got_repo_map_path(&in_repo_path, repo, repo_dir->path);
845 if (error)
846 goto done;
847 free(path);
848 path = in_repo_path;
851 error = got_repo_match_object_id(&commit_id, NULL, rc->commit_id,
852 GOT_OBJ_TYPE_COMMIT, &refs, repo);
853 if (error)
854 goto done;
856 error = got_object_open_as_commit(&commit, repo, commit_id);
857 if (error)
858 goto done;
860 error = got_object_id_by_path(&tree_id, repo, commit, path);
861 if (error)
862 goto done;
864 error = got_object_open_as_tree(&tree, repo, tree_id);
865 if (error)
866 goto done;
868 nentries = got_object_tree_get_nentries(tree);
870 index_page_str = qs->index_page_str ? qs->index_page_str : "";
871 folder = qs->folder ? qs->folder : "";
873 for (i = 0; i < nentries; i++) {
874 struct got_tree_entry *te;
875 mode_t mode;
877 te = got_object_tree_get_entry(tree, i);
879 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
880 if (error)
881 goto done;
883 modestr = strdup("");
884 if (modestr == NULL) {
885 error = got_error_from_errno("strdup");
886 goto done;
888 mode = got_tree_entry_get_mode(te);
889 if (got_object_tree_entry_is_submodule(te)) {
890 free(modestr);
891 modestr = strdup("$");
892 if (modestr == NULL) {
893 error = got_error_from_errno("strdup");
894 goto done;
896 } else if (S_ISLNK(mode)) {
897 free(modestr);
898 modestr = strdup("@");
899 if (modestr == NULL) {
900 error = got_error_from_errno("strdup");
901 goto done;
903 } else if (S_ISDIR(mode)) {
904 free(modestr);
905 modestr = strdup("/");
906 if (modestr == NULL) {
907 error = got_error_from_errno("strdup");
908 goto done;
910 } else if (mode & S_IXUSR) {
911 free(modestr);
912 modestr = strdup("*");
913 if (modestr == NULL) {
914 error = got_error_from_errno("strdup");
915 goto done;
919 name = got_tree_entry_get_name(te);
920 error = gotweb_escape_html(&escaped_name, name);
921 if (error)
922 goto done;
924 if (S_ISDIR(mode)) {
925 r = fcgi_printf(c,
926 "<div class='tree_wrapper'>\n"
927 "<div class='tree_line'>"
928 "<a class='diff_directory' "
929 "href='?index_page=%s&path=%s&action=tree"
930 "&commit=%s&folder=%s/%s'>%s%s</a>"
931 "</div>\n" /* .tree_line */
932 "<div class='tree_line_blank'>&nbsp;</div>\n"
933 "</div>\n", /* .tree_wrapper */
934 index_page_str, qs->path, rc->commit_id,
935 folder, name, escaped_name, modestr);
936 if (r == -1)
937 goto done;
938 } else {
939 r = fcgi_printf(c,
940 "<div class='tree_wrapper'>\n"
941 "<div class='tree_line'>"
942 "<a href='?index_page=%s&path=%s&action=blob"
943 "&commit=%s&folder=%s&file=%s'>%s%s</a>"
944 "</div>\n" /* .tree_line */
945 "<div class='tree_line_blank'>"
946 "<a href='?index_page=%s&path=%s&action=commits"
947 "&commit=%s&folder=%s&file=%s'>commits</a>\n"
948 " | \n"
949 "<a href='?index_page=%s&path=%s&action=blame"
950 "&commit=%s&folder=%s&file=%s'>blame</a>\n"
951 "</div>\n" /* .tree_line_blank */
952 "</div>\n", /* .tree_wrapper */
953 index_page_str, qs->path, rc->commit_id,
954 folder, name, escaped_name, modestr,
955 index_page_str, qs->path, rc->commit_id,
956 folder, name,
957 index_page_str, qs->path, rc->commit_id,
958 folder, name);
959 if (r == -1)
960 goto done;
962 free(id_str);
963 id_str = NULL;
964 free(modestr);
965 modestr = NULL;
966 free(escaped_name);
967 escaped_name = NULL;
969 done:
970 free(escaped_name);
971 free(id_str);
972 free(modestr);
973 free(path);
974 got_ref_list_free(&refs);
975 if (commit)
976 got_object_commit_close(commit);
977 free(commit_id);
978 free(tree_id);
979 return error;
982 const struct got_error *
983 got_output_file_blob(struct request *c)
985 const struct got_error *error = NULL;
986 struct transport *t = c->t;
987 struct got_repository *repo = t->repo;
988 struct querystring *qs = c->t->qs;
989 struct got_commit_object *commit = NULL;
990 struct got_object_id *commit_id = NULL;
991 struct got_reflist_head refs;
992 struct got_blob_object *blob = NULL;
993 char *path = NULL, *in_repo_path = NULL;
994 int obj_type, set_mime = 0, fd = -1;
995 size_t len, hdrlen;
996 const uint8_t *buf;
998 TAILQ_INIT(&refs);
1000 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1001 qs->folder ? "/" : "", qs->file) == -1) {
1002 error = got_error_from_errno("asprintf");
1003 goto done;
1006 error = got_repo_map_path(&in_repo_path, repo, path);
1007 if (error)
1008 goto done;
1010 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1011 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1012 if (error)
1013 goto done;
1015 error = got_object_open_as_commit(&commit, repo, commit_id);
1016 if (error)
1017 goto done;
1019 error = got_object_id_by_path(&commit_id, repo, commit, in_repo_path);
1020 if (error)
1021 goto done;
1023 if (commit_id == NULL) {
1024 error = got_error(GOT_ERR_NO_OBJ);
1025 goto done;
1028 error = got_object_get_type(&obj_type, repo, commit_id);
1029 if (error)
1030 goto done;
1032 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1033 error = got_error(GOT_ERR_OBJ_TYPE);
1034 goto done;
1037 error = got_gotweb_dupfd(&c->priv_fd[BLOB_FD_1], &fd);
1038 if (error)
1039 goto done;
1041 error = got_object_open_as_blob(&blob, repo, commit_id, BUF, fd);
1042 if (error)
1043 goto done;
1044 hdrlen = got_object_blob_get_hdrlen(blob);
1045 do {
1046 error = got_object_blob_read_block(&len, blob);
1047 if (error)
1048 goto done;
1049 buf = got_object_blob_get_read_buf(blob);
1052 * Skip blob object header first time around,
1053 * which also contains a zero byte.
1055 buf += hdrlen;
1056 if (set_mime == 0) {
1057 if (isbinary(buf, len - hdrlen)) {
1058 error = gotweb_render_content_type_file(c,
1059 "application/octet-stream",
1060 qs->file);
1061 if (error) {
1062 log_warnx("%s: %s", __func__,
1063 error->msg);
1064 goto done;
1066 } else {
1067 error = gotweb_render_content_type(c,
1068 "text/plain");
1069 if (error) {
1070 log_warnx("%s: %s", __func__,
1071 error->msg);
1072 goto done;
1076 set_mime = 1;
1077 fcgi_gen_binary_response(c, buf, len - hdrlen);
1079 hdrlen = 0;
1080 } while (len != 0);
1081 done:
1082 if (commit)
1083 got_object_commit_close(commit);
1084 if (fd != -1 && close(fd) == -1 && error == NULL)
1085 error = got_error_from_errno("close");
1086 if (blob)
1087 got_object_blob_close(blob);
1088 free(in_repo_path);
1089 free(commit_id);
1090 free(path);
1091 return error;
1094 struct blame_line {
1095 int annotated;
1096 char *id_str;
1097 char *committer;
1098 char datebuf[11]; /* YYYY-MM-DD + NUL */
1101 struct blame_cb_args {
1102 struct blame_line *lines;
1103 int nlines;
1104 int nlines_prec;
1105 int lineno_cur;
1106 off_t *line_offsets;
1107 FILE *f;
1108 struct got_repository *repo;
1109 struct request *c;
1112 static const struct got_error *
1113 got_gotweb_blame_cb(void *arg, int nlines, int lineno,
1114 struct got_commit_object *commit, struct got_object_id *id)
1116 const struct got_error *err = NULL;
1117 struct blame_cb_args *a = arg;
1118 struct blame_line *bline;
1119 struct request *c = a->c;
1120 struct transport *t = c->t;
1121 struct querystring *qs = t->qs;
1122 struct repo_dir *repo_dir = t->repo_dir;
1123 const char *index_page_str;
1124 char *line = NULL, *eline = NULL;
1125 size_t linesize = 0;
1126 off_t offset;
1127 struct tm tm;
1128 time_t committer_time;
1129 int r;
1131 if (nlines != a->nlines ||
1132 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1133 return got_error(GOT_ERR_RANGE);
1135 if (lineno == -1)
1136 return NULL; /* no change in this commit */
1138 /* Annotate this line. */
1139 bline = &a->lines[lineno - 1];
1140 if (bline->annotated)
1141 return NULL;
1142 err = got_object_id_str(&bline->id_str, id);
1143 if (err)
1144 return err;
1146 bline->committer = strdup(got_object_commit_get_committer(commit));
1147 if (bline->committer == NULL) {
1148 err = got_error_from_errno("strdup");
1149 goto done;
1152 committer_time = got_object_commit_get_committer_time(commit);
1153 if (gmtime_r(&committer_time, &tm) == NULL)
1154 return got_error_from_errno("gmtime_r");
1155 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
1156 &tm) == 0) {
1157 err = got_error(GOT_ERR_NO_SPACE);
1158 goto done;
1160 bline->annotated = 1;
1162 /* Print lines annotated so far. */
1163 bline = &a->lines[a->lineno_cur - 1];
1164 if (!bline->annotated)
1165 goto done;
1167 offset = a->line_offsets[a->lineno_cur - 1];
1168 if (fseeko(a->f, offset, SEEK_SET) == -1) {
1169 err = got_error_from_errno("fseeko");
1170 goto done;
1173 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1175 while (a->lineno_cur <= a->nlines && bline->annotated) {
1176 char *smallerthan, *at, *nl, *committer;
1177 size_t len;
1179 if (getline(&line, &linesize, a->f) == -1) {
1180 if (ferror(a->f))
1181 err = got_error_from_errno("getline");
1182 break;
1185 committer = bline->committer;
1186 smallerthan = strchr(committer, '<');
1187 if (smallerthan && smallerthan[1] != '\0')
1188 committer = smallerthan + 1;
1189 at = strchr(committer, '@');
1190 if (at)
1191 *at = '\0';
1192 len = strlen(committer);
1193 if (len >= 9)
1194 committer[8] = '\0';
1196 nl = strchr(line, '\n');
1197 if (nl)
1198 *nl = '\0';
1200 err = gotweb_escape_html(&eline, line);
1201 if (err)
1202 goto done;
1204 r = fcgi_printf(c, "<div class='blame_wrapper'>"
1205 "<div class='blame_number'>%.*d</div>"
1206 "<div class='blame_hash'>"
1207 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1208 "%.8s</a>"
1209 "</div>"
1210 "<div class='blame_date'>%s</div>"
1211 "<div class='blame_author'>%s</div>"
1212 "<div class='blame_code'>%s</div>"
1213 "</div>", /* .blame_wrapper */
1214 a->nlines_prec, a->lineno_cur,
1215 index_page_str, repo_dir->name, bline->id_str,
1216 bline->id_str,
1217 bline->datebuf,
1218 committer,
1219 eline);
1220 if (r == -1)
1221 goto done;
1223 a->lineno_cur++;
1224 bline = &a->lines[a->lineno_cur - 1];
1226 done:
1227 free(line);
1228 free(eline);
1229 return err;
1232 const struct got_error *
1233 got_output_file_blame(struct request *c)
1235 const struct got_error *error = NULL;
1236 struct transport *t = c->t;
1237 struct got_repository *repo = t->repo;
1238 struct querystring *qs = c->t->qs;
1239 struct got_object_id *obj_id = NULL, *commit_id = NULL;
1240 struct got_commit_object *commit = NULL;
1241 struct got_reflist_head refs;
1242 struct got_blob_object *blob = NULL;
1243 char *path = NULL, *in_repo_path = NULL;
1244 struct blame_cb_args bca;
1245 int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1, fd4 = -1, fd5 = -1;
1246 int fd6 = -1;
1247 off_t filesize;
1248 FILE *f1 = NULL, *f2 = NULL;
1250 TAILQ_INIT(&refs);
1251 bca.f = NULL;
1252 bca.lines = NULL;
1254 if (asprintf(&path, "%s%s%s", qs->folder ? qs->folder : "",
1255 qs->folder ? "/" : "", qs->file) == -1) {
1256 error = got_error_from_errno("asprintf");
1257 goto done;
1260 error = got_repo_map_path(&in_repo_path, repo, path);
1261 if (error)
1262 goto done;
1264 error = got_repo_match_object_id(&commit_id, NULL, qs->commit,
1265 GOT_OBJ_TYPE_COMMIT, &refs, repo);
1266 if (error)
1267 goto done;
1269 error = got_object_open_as_commit(&commit, repo, commit_id);
1270 if (error)
1271 goto done;
1273 error = got_object_id_by_path(&obj_id, repo, commit, in_repo_path);
1274 if (error)
1275 goto done;
1277 if (commit_id == NULL) {
1278 error = got_error(GOT_ERR_NO_OBJ);
1279 goto done;
1282 error = got_object_get_type(&obj_type, repo, obj_id);
1283 if (error)
1284 goto done;
1286 if (obj_type != GOT_OBJ_TYPE_BLOB) {
1287 error = got_error(GOT_ERR_OBJ_TYPE);
1288 goto done;
1291 error = got_gotweb_openfile(&bca.f, &c->priv_fd[BLAME_FD_1], &fd1);
1292 if (error)
1293 goto done;
1295 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_2], &fd2);
1296 if (error)
1297 goto done;
1299 error = got_object_open_as_blob(&blob, repo, obj_id, BUF, fd2);
1300 if (error)
1301 goto done;
1303 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
1304 &bca.line_offsets, bca.f, blob);
1305 if (error || bca.nlines == 0)
1306 goto done;
1308 /* Don't include \n at EOF in the blame line count. */
1309 if (bca.line_offsets[bca.nlines - 1] == filesize)
1310 bca.nlines--;
1312 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
1313 if (bca.lines == NULL) {
1314 error = got_error_from_errno("calloc");
1315 goto done;
1317 bca.lineno_cur = 1;
1318 bca.nlines_prec = 0;
1319 i = bca.nlines;
1320 while (i > 0) {
1321 i /= 10;
1322 bca.nlines_prec++;
1324 bca.repo = repo;
1325 bca.c = c;
1327 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_3], &fd3);
1328 if (error)
1329 goto done;
1331 error = got_gotweb_dupfd(&c->priv_fd[BLAME_FD_4], &fd4);
1332 if (error)
1333 goto done;
1335 error = got_gotweb_openfile(&f1, &c->priv_fd[BLAME_FD_5], &fd5);
1336 if (error)
1337 goto done;
1339 error = got_gotweb_openfile(&f2, &c->priv_fd[BLAME_FD_6], &fd6);
1340 if (error)
1341 goto done;
1343 error = got_blame(in_repo_path, commit_id, repo,
1344 GOT_DIFF_ALGORITHM_MYERS, got_gotweb_blame_cb, &bca, NULL, NULL,
1345 fd3, fd4, f1, f2);
1347 if (blob) {
1348 free(bca.line_offsets);
1349 for (i = 0; i < bca.nlines; i++) {
1350 struct blame_line *bline = &bca.lines[i];
1351 free(bline->id_str);
1352 free(bline->committer);
1355 done:
1356 free(bca.lines);
1357 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
1358 error = got_error_from_errno("close");
1359 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
1360 error = got_error_from_errno("close");
1361 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1362 error = got_error_from_errno("close");
1363 if (bca.f) {
1364 const struct got_error *bca_err =
1365 got_gotweb_flushfile(bca.f, fd1);
1366 if (error == NULL)
1367 error = bca_err;
1369 if (f1) {
1370 const struct got_error *f1_err =
1371 got_gotweb_flushfile(f1, fd5);
1372 if (error == NULL)
1373 error = f1_err;
1375 if (f2) {
1376 const struct got_error *f2_err =
1377 got_gotweb_flushfile(f2, fd6);
1378 if (error == NULL)
1379 error = f2_err;
1381 if (commit)
1382 got_object_commit_close(commit);
1383 if (blob)
1384 got_object_blob_close(blob);
1385 free(in_repo_path);
1386 free(commit_id);
1387 free(path);
1388 return error;
1391 const struct got_error *
1392 got_output_repo_diff(struct request *c)
1394 const struct got_error *error = NULL;
1395 struct transport *t = c->t;
1396 struct got_repository *repo = t->repo;
1397 struct repo_commit *rc = NULL;
1398 struct got_object_id *id1 = NULL, *id2 = NULL;
1399 struct got_reflist_head refs;
1400 FILE *f1 = NULL, *f2 = NULL, *f3 = NULL;
1401 char *label1 = NULL, *label2 = NULL, *line = NULL;
1402 char *newline, *eline = NULL, *color = NULL;
1403 int obj_type, fd1, fd2, fd3, fd4 = -1, fd5 = -1;
1404 size_t linesize = 0;
1405 ssize_t linelen;
1406 int wrlen = 0;
1408 TAILQ_INIT(&refs);
1410 error = got_gotweb_openfile(&f1, &c->priv_fd[DIFF_FD_1], &fd1);
1411 if (error)
1412 return error;
1414 error = got_gotweb_openfile(&f2, &c->priv_fd[DIFF_FD_2], &fd2);
1415 if (error)
1416 return error;
1418 error = got_gotweb_openfile(&f3, &c->priv_fd[DIFF_FD_3], &fd3);
1419 if (error)
1420 return error;
1422 rc = TAILQ_FIRST(&t->repo_commits);
1424 if (rc->parent_id != NULL &&
1425 strncmp(rc->parent_id, "/dev/null", 9) != 0) {
1426 error = got_repo_match_object_id(&id1, &label1,
1427 rc->parent_id, GOT_OBJ_TYPE_ANY,
1428 &refs, repo);
1429 if (error)
1430 goto done;
1433 error = got_repo_match_object_id(&id2, &label2, rc->commit_id,
1434 GOT_OBJ_TYPE_ANY, &refs, repo);
1435 if (error)
1436 goto done;
1438 error = got_object_get_type(&obj_type, repo, id2);
1439 if (error)
1440 goto done;
1442 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_4], &fd4);
1443 if (error)
1444 goto done;
1446 error = got_gotweb_dupfd(&c->priv_fd[DIFF_FD_5], &fd5);
1447 if (error)
1448 goto done;
1450 switch (obj_type) {
1451 case GOT_OBJ_TYPE_BLOB:
1452 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd4, fd5,
1453 id1, id2, NULL, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1454 repo, f3);
1455 break;
1456 case GOT_OBJ_TYPE_TREE:
1457 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd4, fd5,
1458 id1, id2, NULL, "", "", GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1459 repo, f3);
1460 break;
1461 case GOT_OBJ_TYPE_COMMIT:
1462 error = got_diff_objects_as_commits(NULL, NULL, f1, f2, fd4,
1463 fd5, id1, id2, NULL, GOT_DIFF_ALGORITHM_MYERS, 3, 0, 0,
1464 repo, f3);
1465 break;
1466 default:
1467 error = got_error(GOT_ERR_OBJ_TYPE);
1469 if (error)
1470 goto done;
1472 if (fseek(f1, 0, SEEK_SET) == -1) {
1473 error = got_ferror(f1, GOT_ERR_IO);
1474 goto done;
1477 if (fseek(f2, 0, SEEK_SET) == -1) {
1478 error = got_ferror(f2, GOT_ERR_IO);
1479 goto done;
1482 if (fseek(f3, 0, SEEK_SET) == -1) {
1483 error = got_ferror(f3, GOT_ERR_IO);
1484 goto done;
1487 while ((linelen = getline(&line, &linesize, f3)) != -1) {
1488 if (strncmp(line, "-", 1) == 0) {
1489 color = strdup("diff_minus");
1490 if (color == NULL) {
1491 error = got_error_from_errno("strdup");
1492 goto done;
1494 } else if (strncmp(line, "+", 1) == 0) {
1495 color = strdup("diff_plus");
1496 if (color == NULL) {
1497 error = got_error_from_errno("strdup");
1498 goto done;
1500 } else if (strncmp(line, "@@", 2) == 0) {
1501 color = strdup("diff_chunk_header");
1502 if (color == NULL) {
1503 error = got_error_from_errno("strdup");
1504 goto done;
1506 } else if (strncmp(line, "@@", 2) == 0) {
1507 color = strdup("diff_chunk_header");
1508 if (color == NULL) {
1509 error = got_error_from_errno("strdup");
1510 goto done;
1512 } else if (strncmp(line, "commit +", 8) == 0) {
1513 color = strdup("diff_meta");
1514 if (color == NULL) {
1515 error = got_error_from_errno("strdup");
1516 goto done;
1518 } else if (strncmp(line, "commit -", 8) == 0) {
1519 color = strdup("diff_meta");
1520 if (color == NULL) {
1521 error = got_error_from_errno("strdup");
1522 goto done;
1524 } else if (strncmp(line, "blob +", 6) == 0) {
1525 color = strdup("diff_meta");
1526 if (color == NULL) {
1527 error = got_error_from_errno("strdup");
1528 goto done;
1530 } else if (strncmp(line, "blob -", 6) == 0) {
1531 color = strdup("diff_meta");
1532 if (color == NULL) {
1533 error = got_error_from_errno("strdup");
1534 goto done;
1536 } else if (strncmp(line, "file +", 6) == 0) {
1537 color = strdup("diff_meta");
1538 if (color == NULL) {
1539 error = got_error_from_errno("strdup");
1540 goto done;
1542 } else if (strncmp(line, "file -", 6) == 0) {
1543 color = strdup("diff_meta");
1544 if (color == NULL) {
1545 error = got_error_from_errno("strdup");
1546 goto done;
1548 } else if (strncmp(line, "from:", 5) == 0) {
1549 color = strdup("diff_author");
1550 if (color == NULL) {
1551 error = got_error_from_errno("strdup");
1552 goto done;
1554 } else if (strncmp(line, "via:", 4) == 0) {
1555 color = strdup("diff_author");
1556 if (color == NULL) {
1557 error = got_error_from_errno("strdup");
1558 goto done;
1560 } else if (strncmp(line, "date:", 5) == 0) {
1561 color = strdup("diff_date");
1562 if (color == NULL) {
1563 error = got_error_from_errno("strdup");
1564 goto done;
1568 newline = strchr(line, '\n');
1569 if (newline)
1570 *newline = '\0';
1572 error = gotweb_escape_html(&eline, line);
1573 if (error)
1574 goto done;
1576 fcgi_printf(c, "<div class='diff_line %s'>%s</div>\n",
1577 color ? color : "", eline);
1578 free(eline);
1579 eline = NULL;
1581 if (linelen > 0)
1582 wrlen = wrlen + linelen;
1583 free(color);
1584 color = NULL;
1586 if (linelen == -1 && ferror(f3))
1587 error = got_error_from_errno("getline");
1588 done:
1589 free(color);
1590 if (fd4 != -1 && close(fd4) == -1 && error == NULL)
1591 error = got_error_from_errno("close");
1592 if (fd5 != -1 && close(fd5) == -1 && error == NULL)
1593 error = got_error_from_errno("close");
1594 if (f1) {
1595 const struct got_error *f1_err =
1596 got_gotweb_flushfile(f1, fd1);
1597 if (error == NULL)
1598 error = f1_err;
1600 if (f2) {
1601 const struct got_error *f2_err =
1602 got_gotweb_flushfile(f2, fd2);
1603 if (error == NULL)
1604 error = f2_err;
1606 if (f3) {
1607 const struct got_error *f3_err =
1608 got_gotweb_flushfile(f3, fd3);
1609 if (error == NULL)
1610 error = f3_err;
1612 got_ref_list_free(&refs);
1613 free(line);
1614 free(eline);
1615 free(label1);
1616 free(label2);
1617 free(id1);
1618 free(id2);
1619 return error;
1622 static const struct got_error *
1623 got_init_repo_commit(struct repo_commit **rc)
1625 *rc = calloc(1, sizeof(**rc));
1626 if (*rc == NULL)
1627 return got_error_from_errno2("%s: calloc", __func__);
1629 (*rc)->path = NULL;
1630 (*rc)->refs_str = NULL;
1631 (*rc)->commit_id = NULL;
1632 (*rc)->committer = NULL;
1633 (*rc)->author = NULL;
1634 (*rc)->parent_id = NULL;
1635 (*rc)->tree_id = NULL;
1636 (*rc)->commit_msg = NULL;
1638 return NULL;
1641 static const struct got_error *
1642 got_init_repo_tag(struct repo_tag **rt)
1644 *rt = calloc(1, sizeof(**rt));
1645 if (*rt == NULL)
1646 return got_error_from_errno2("%s: calloc", __func__);
1648 (*rt)->commit_id = NULL;
1649 (*rt)->tag_name = NULL;
1650 (*rt)->tag_commit = NULL;
1651 (*rt)->commit_msg = NULL;
1652 (*rt)->tagger = NULL;
1654 return NULL;